From 59a173c994716d15ae9c6b8a52b5dc63d371dab9 Mon Sep 17 00:00:00 2001 From: Christoph Baumann Date: Wed, 7 May 2025 16:46:36 +0200 Subject: [PATCH] add support for emummc ums, this is terrible plz fix --- bdk/usb/usb_gadget_ums.c | 96 +++++++++++++++++++++++++++++++++------- 1 file changed, 81 insertions(+), 15 deletions(-) diff --git a/bdk/usb/usb_gadget_ums.c b/bdk/usb/usb_gadget_ums.c index 3c4a2727..c27153c4 100644 --- a/bdk/usb/usb_gadget_ums.c +++ b/bdk/usb/usb_gadget_ums.c @@ -19,6 +19,8 @@ * along with this program. If not, see . */ +#include +#include #include #include @@ -495,8 +497,14 @@ static int _scsi_read(usbd_gadget_ums_t *ums, bulk_ctxt_t *bulk_ctxt) } // Do the SDMMC read. - if (!sdmmc_storage_read(ums->lun.storage, ums->lun.offset + lba_offset, amount, sdmmc_buf)) - amount = 0; + if(ums->lun.storage){ + if (!sdmmc_storage_read(ums->lun.storage, ums->lun.offset + lba_offset, amount, sdmmc_buf)) + amount = 0; + }else{ + if(!emummc_storage_file_based_read(ums->lun.offset + lba_offset, amount, sdmmc_buf)){ + amount = 0; + } + } // Wait for the async USB transfer to finish. if (!first_read) @@ -650,9 +658,15 @@ static int _scsi_write(usbd_gadget_ums_t *ums, bulk_ctxt_t *bulk_ctxt) goto empty_write; // Perform the write. - if (!sdmmc_storage_write(ums->lun.storage, ums->lun.offset + lba_offset, - amount >> UMS_DISK_LBA_SHIFT, (u8 *)bulk_ctxt->bulk_out_buf)) - amount = 0; + if(ums->lun.storage){ + if (!sdmmc_storage_write(ums->lun.storage, ums->lun.offset + lba_offset, + amount >> UMS_DISK_LBA_SHIFT, (u8 *)bulk_ctxt->bulk_out_buf)) + amount = 0; + }else{ + if(!emummc_storage_file_based_read(ums->lun.offset + lba_offset, amount >> UMS_DISK_LBA_SHIFT, (u8*)bulk_ctxt->bulk_out_buf)){ + amount = 0; + } + } DPRINTF("file write %X @ %X\n", amount, lba_offset); @@ -722,8 +736,14 @@ static int _scsi_verify(usbd_gadget_ums_t *ums, bulk_ctxt_t *bulk_ctxt) break; } - if (!sdmmc_storage_read(ums->lun.storage, ums->lun.offset + lba_offset, amount, bulk_ctxt->bulk_in_buf)) - amount = 0; + if(ums->lun.storage){ + if (!sdmmc_storage_read(ums->lun.storage, ums->lun.offset + lba_offset, amount, bulk_ctxt->bulk_in_buf)) + amount = 0; + }else{ + if(!emummc_storage_file_based_read(ums->lun.offset + lba_offset, amount, bulk_ctxt->bulk_in_buf)){ + amount = 0; + } + } DPRINTF("File read %X @ %X\n", amount, lba_offset); @@ -756,8 +776,12 @@ static int _scsi_inquiry(usbd_gadget_ums_t *ums, bulk_ctxt_t *bulk_ctxt) buf[3] = 20; // Additional length. buf += 4; - s_printf((char *)buf, "%04X%s", - ums->lun.storage->cid.serial, ums->lun.type == MMC_SD ? " SD " : " eMMC "); + if(ums->lun.storage){ + s_printf((char *)buf, "%04X%s", + ums->lun.storage->cid.serial, ums->lun.type == MMC_SD ? " SD " : " eMMC "); + }else{ + strcpy((char*)buf, "0000 emuMMC"); + } switch (ums->lun.partition) { @@ -1871,9 +1895,38 @@ int usb_device_gadget_ums(usb_ctxt_t *usbs) ums.lun.sdmmc = &sd_sdmmc; ums.lun.storage = &sd_storage; + }else if(usbs->type == MMC_EMUMMC_FILE){ + // sd must be already mounted and emummc file based initialized + if(!sd_get_card_mounted()){ + ums.set_text(ums.label, "#FFDD00 Failed to init SD!#"); + res = 1; + goto init_fail; + } + ums.lun.storage = NULL; + ums.lun.sdmmc = NULL; } - else - { + else if(usbs->type == MMC_EMUMMC_RAW_EMMC){ + if (!emmc_initialize(false)) + { + ums.set_text(ums.label, "#FFDD00 Failed to init eMMC!#"); + res = 1; + goto init_fail; + } + emmc_set_partition(EMMC_GPP); + + ums.lun.sdmmc = &emmc_sdmmc; + ums.lun.storage = &emmc_storage; + }else if(usbs->type == MMC_EMUMMC_RAW_SD){ + if (!sd_initialize(false)) + { + ums.set_text(ums.label, "#FFDD00 Failed to init SD!#"); + res = 1; + goto init_fail; + } + + ums.lun.sdmmc = &emmc_sdmmc; + ums.lun.storage = &emmc_storage; + }else{ if (!emmc_initialize(false)) { ums.set_text(ums.label, "#FFDD00 Failed to init eMMC!#"); @@ -1902,10 +1955,23 @@ int usb_device_gadget_ums(usb_ctxt_t *usbs) // If partition sectors are not set get them from hardware. if (!ums.lun.num_sectors) { - if (usbs->type == MMC_EMMC && (ums.lun.partition - 1)) // eMMC BOOT0/1. - ums.lun.num_sectors = emmc_storage.ext_csd.boot_mult << 8; - else - ums.lun.num_sectors = ums.lun.storage->sec_cnt; // eMMC GPP or SD. + switch(usbs->type){ + case MMC_EMMC: + if(ums.lun.partition - 1){ + ums.lun.num_sectors = emmc_storage.ext_csd.boot_mult << 8; + }else{ + ums.lun.num_sectors = ums.lun.storage->sec_cnt; + } + break; + case MMC_SD: + ums.lun.num_sectors = ums.lun.storage->sec_cnt; + break; + case MMC_EMUMMC_FILE: + case MMC_EMUMMC_RAW_SD: + case MMC_EMUMMC_RAW_EMMC: + ums.set_text(ums.label, "#FFDD00 No sector count set for emuMMC!#"); + break; + } } do