diff --git a/src/flashcart/flashcart.c b/src/flashcart/flashcart.c index 0b241421..81052ca6 100644 --- a/src/flashcart/flashcart.c +++ b/src/flashcart/flashcart.c @@ -112,12 +112,16 @@ flashcart_error_t flashcart_load_rom (char *rom_path, bool byte_swap, flashcart_ return error; } -flashcart_error_t flashcart_load_file (char *file_path, uint32_t start_offset_address) { +flashcart_error_t flashcart_load_file (char *file_path, uint32_t rom_offset, uint32_t file_offset) { if ((file_path == NULL) || (!file_exists(file_path))) { return FLASHCART_ERROR_ARGS; } - return flashcart->load_file(file_path, start_offset_address); + if ((file_offset % FS_SECTOR_SIZE) != 0) { + return FLASHCART_ERROR_ARGS; + } + + return flashcart->load_file(file_path, rom_offset, file_offset); } flashcart_error_t flashcart_load_save (char *save_path, flashcart_save_type_t save_type) { diff --git a/src/flashcart/flashcart.h b/src/flashcart/flashcart.h index 6f99f45a..436aaf94 100644 --- a/src/flashcart/flashcart.h +++ b/src/flashcart/flashcart.h @@ -42,7 +42,7 @@ typedef struct { flashcart_error_t (*init) (void); flashcart_error_t (*deinit) (void); flashcart_error_t (*load_rom) (char *rom_path, flashcart_progress_callback_t *progress); - flashcart_error_t (*load_file) (char *file_path, uint32_t start_offset_address); + flashcart_error_t (*load_file) (char *file_path, uint32_t rom_offset, uint32_t file_offset); flashcart_error_t (*load_save) (char *save_path); flashcart_error_t (*set_save_type) (flashcart_save_type_t save_type); flashcart_error_t (*set_save_writeback) (uint32_t *sectors); @@ -52,7 +52,7 @@ typedef struct { flashcart_error_t flashcart_init (void); flashcart_error_t flashcart_deinit (void); flashcart_error_t flashcart_load_rom (char *rom_path, bool byte_swap, flashcart_progress_callback_t *progress); -flashcart_error_t flashcart_load_file (char *file_path, uint32_t start_offset_address); +flashcart_error_t flashcart_load_file (char *file_path, uint32_t rom_offset, uint32_t file_offset); flashcart_error_t flashcart_load_save (char *save_path, flashcart_save_type_t save_type); diff --git a/src/flashcart/sc64/sc64.c b/src/flashcart/sc64/sc64.c index b26d7a7c..9cc554c4 100644 --- a/src/flashcart/sc64/sc64.c +++ b/src/flashcart/sc64/sc64.c @@ -196,7 +196,7 @@ static flashcart_error_t sc64_load_rom (char *rom_path, flashcart_progress_callb return FLASHCART_OK; } -static flashcart_error_t sc64_load_file (char *file_path, uint32_t start_offset_address) { +static flashcart_error_t sc64_load_file (char *file_path, uint32_t rom_offset, uint32_t file_offset) { FIL fil; UINT br; @@ -206,14 +206,19 @@ static flashcart_error_t sc64_load_file (char *file_path, uint32_t start_offset_ fix_file_size(&fil); - size_t file_size = f_size(&fil); + size_t file_size = f_size(&fil) - file_offset; - if (file_size > (MiB(64) - start_offset_address)) { + if (file_size > (MiB(64) - rom_offset)) { f_close(&fil); return FLASHCART_ERROR_ARGS; } - if (f_read(&fil, (void *) (ROM_ADDRESS + start_offset_address), file_size, &br) != FR_OK) { + if (f_lseek(&fil, file_offset) != FR_OK) { + f_close(&fil); + return FLASHCART_ERROR_LOAD; + } + + if (f_read(&fil, (void *) (ROM_ADDRESS + rom_offset), file_size, &br) != FR_OK) { f_close(&fil); return FLASHCART_ERROR_LOAD; } diff --git a/src/menu/cart_load.c b/src/menu/cart_load.c index 07a60a15..921aef64 100644 --- a/src/menu/cart_load.c +++ b/src/menu/cart_load.c @@ -81,6 +81,7 @@ cart_load_err_t cart_load_emulator (menu_t *menu, cart_load_emu_type_t emu_type, path_t *path = path_init("sd:/", EMU_LOCATION); flashcart_save_type_t save_type = FLASHCART_SAVE_TYPE_NONE; uint32_t emulated_rom_offset = 0x200000; + uint32_t emulated_file_offset = 0; switch (emu_type) { case CART_LOAD_EMU_TYPE_NES: @@ -112,7 +113,7 @@ cart_load_err_t cart_load_emulator (menu_t *menu, cart_load_emu_type_t emu_type, path = path_clone_push(menu->browser.directory, menu->browser.entry->name); - menu->flashcart_error = flashcart_load_file(path_get(path), emulated_rom_offset); + menu->flashcart_error = flashcart_load_file(path_get(path), emulated_rom_offset, emulated_file_offset); if (menu->flashcart_error != FLASHCART_OK) { path_free(path); return CART_LOAD_ERR_EMU_ROM;