Added file offset to flashcart_load_file

This commit is contained in:
Mateusz Faderewski 2023-08-20 20:26:48 +02:00
parent bbc4901403
commit 3db859b0f7
4 changed files with 19 additions and 9 deletions

View File

@ -112,12 +112,16 @@ flashcart_error_t flashcart_load_rom (char *rom_path, bool byte_swap, flashcart_
return error; 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))) { if ((file_path == NULL) || (!file_exists(file_path))) {
return FLASHCART_ERROR_ARGS; 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) { flashcart_error_t flashcart_load_save (char *save_path, flashcart_save_type_t save_type) {

View File

@ -42,7 +42,7 @@ typedef struct {
flashcart_error_t (*init) (void); flashcart_error_t (*init) (void);
flashcart_error_t (*deinit) (void); flashcart_error_t (*deinit) (void);
flashcart_error_t (*load_rom) (char *rom_path, flashcart_progress_callback_t *progress); 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 (*load_save) (char *save_path);
flashcart_error_t (*set_save_type) (flashcart_save_type_t save_type); flashcart_error_t (*set_save_type) (flashcart_save_type_t save_type);
flashcart_error_t (*set_save_writeback) (uint32_t *sectors); 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_init (void);
flashcart_error_t flashcart_deinit (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_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); flashcart_error_t flashcart_load_save (char *save_path, flashcart_save_type_t save_type);

View File

@ -196,7 +196,7 @@ static flashcart_error_t sc64_load_rom (char *rom_path, flashcart_progress_callb
return FLASHCART_OK; 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; FIL fil;
UINT br; UINT br;
@ -206,14 +206,19 @@ static flashcart_error_t sc64_load_file (char *file_path, uint32_t start_offset_
fix_file_size(&fil); 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); f_close(&fil);
return FLASHCART_ERROR_ARGS; 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); f_close(&fil);
return FLASHCART_ERROR_LOAD; return FLASHCART_ERROR_LOAD;
} }

View File

@ -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); path_t *path = path_init("sd:/", EMU_LOCATION);
flashcart_save_type_t save_type = FLASHCART_SAVE_TYPE_NONE; flashcart_save_type_t save_type = FLASHCART_SAVE_TYPE_NONE;
uint32_t emulated_rom_offset = 0x200000; uint32_t emulated_rom_offset = 0x200000;
uint32_t emulated_file_offset = 0;
switch (emu_type) { switch (emu_type) {
case CART_LOAD_EMU_TYPE_NES: 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); 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) { if (menu->flashcart_error != FLASHCART_OK) {
path_free(path); path_free(path);
return CART_LOAD_ERR_EMU_ROM; return CART_LOAD_ERR_EMU_ROM;