From 0af8989a82d6893d5b71e7d21d232758ab7d7cf7 Mon Sep 17 00:00:00 2001 From: Polprzewodnikowy Date: Fri, 10 Dec 2021 17:15:43 +0100 Subject: [PATCH] storage 2 --- sw/n64/N64.ld | 2 +- sw/n64/src/boot.c | 9 ++++--- sw/n64/src/main.c | 16 ++++++------ sw/n64/src/storage.c | 59 +++++++++++++++++++++++++++++++++++++++++++- sw/riscv/SC64.ld | 2 +- 5 files changed, 73 insertions(+), 15 deletions(-) diff --git a/sw/n64/N64.ld b/sw/n64/N64.ld index fe7c24e..f69881b 100644 --- a/sw/n64/N64.ld +++ b/sw/n64/N64.ld @@ -1,5 +1,5 @@ MEMORY { - rdram (rwx) : org = 0x80300000, len = 1M + rdram (rwx) : org = 0x80000400, len = 4M - 0x400 flash (r) : org = 0xB0000000, len = 90k } diff --git a/sw/n64/src/boot.c b/sw/n64/src/boot.c index a5132db..c86efc8 100644 --- a/sw/n64/src/boot.c +++ b/sw/n64/src/boot.c @@ -21,10 +21,11 @@ static const ipl3_crc32_t ipl3_crc32[] = { { .crc32 = 0x0B050EE0, .seed = 0x78, .version = 0 }, // CICx103 { .crc32 = 0x98BC2C86, .seed = 0x91, .version = 0 }, // CICx105 { .crc32 = 0xACC8580A, .seed = 0x85, .version = 0 }, // CICx106 - { .crc32 = 0x0C965795, .seed = 0xDD, .version = 0 }, // JP 64DD - { .crc32 = 0x10C68B18, .seed = 0xDD, .version = 0 }, // JP 64DD dev - { .crc32 = 0x0E018159, .seed = 0xDD, .version = 0 }, // JP 64DD retail - { .crc32 = 0x8FEBA21E, .seed = 0xDE, .version = 0 }, // US 64DD retail + { .crc32 = 0x10C68B18, .seed = 0xDD, .version = 0 }, // NDXJ0 + { .crc32 = 0xBC605D0A, .seed = 0xDD, .version = 0 }, // NDDJ0 + { .crc32 = 0x502C4466, .seed = 0xDD, .version = 0 }, // NDDJ1 + { .crc32 = 0x0C965795, .seed = 0xDD, .version = 0 }, // NDDJ2 + { .crc32 = 0x8FEBA21E, .seed = 0xDE, .version = 0 }, // NDDE0 }; diff --git a/sw/n64/src/main.c b/sw/n64/src/main.c index 87a9ab1..520572f 100644 --- a/sw/n64/src/main.c +++ b/sw/n64/src/main.c @@ -7,6 +7,8 @@ void main (void) { boot_info_t boot_info; sc64_info_t sc64_info; + boot_info.reset_type = OS_INFO->reset_type; + sc64_get_info(&sc64_info); LOG_I("Bootloader version: %.32s\r\n", sc64_info.bootloader_version); @@ -18,17 +20,17 @@ void main (void) { break; case BOOT_MODE_ROM: - boot_info.device_type = BOOT_DEVICE_TYPE_ROM; LOG_I("Running ROM from SDRAM\r\n"); + boot_info.device_type = BOOT_DEVICE_TYPE_ROM; break; case BOOT_MODE_DDIPL: - boot_info.device_type = BOOT_DEVICE_TYPE_DD; LOG_I("Running DDIPL from SDRAM\r\n"); + boot_info.device_type = BOOT_DEVICE_TYPE_DD; break; case BOOT_MODE_DIRECT: - LOG_I("Running bootloader from SDRAM, running menu from FSD\r\n"); + LOG_I("Running bootloader from SDRAM - running menu from FSD\r\n"); storage_run_menu(STORAGE_BACKEND_USB, &boot_info, &sc64_info); break; @@ -37,8 +39,6 @@ void main (void) { while (1); } - boot_info.reset_type = OS_INFO->reset_type; - if (sc64_info.tv_type != TV_TYPE_UNKNOWN) { boot_info.tv_type = sc64_info.tv_type; LOG_I("Using provided TV type: %d\r\n", boot_info.tv_type); @@ -54,14 +54,14 @@ void main (void) { if (sc64_info.cic_seed != 0xFFFF) { boot_info.cic_seed = sc64_info.cic_seed & 0xFF; boot_info.version = (sc64_info.cic_seed >> 8) & 0x01; - LOG_I("Using provided CIC seed and version: 0x%02X / %d\r\n", boot_info.cic_seed, boot_info.version); + LOG_I("Using provided CIC seed and version: 0x%02X.%d\r\n", boot_info.cic_seed, boot_info.version); } else { if (boot_get_cic_seed_version(&boot_info)) { - LOG_I("Using CIC seed and version guessed from IPL3: 0x%02X / %d\r\n", boot_info.cic_seed, boot_info.version); + LOG_I("Using CIC seed and version guessed from IPL3: 0x%02X.%d\r\n", boot_info.cic_seed, boot_info.version); } else { boot_info.cic_seed = 0x3F; boot_info.version = 0; - LOG_I("Using 6102/7101 CIC seed and version: 0x%02X / %d\r\n", boot_info.cic_seed, boot_info.version); + LOG_I("Using 6102/7101 CIC seed and version: 0x%02X.%d\r\n", boot_info.cic_seed, boot_info.version); } } diff --git a/sw/n64/src/storage.c b/sw/n64/src/storage.c index 89abd89..ff22ace 100644 --- a/sw/n64/src/storage.c +++ b/sw/n64/src/storage.c @@ -1,7 +1,64 @@ #include "storage.h" +#include "sc64.h" +#include "fatfs/ff.h" + + +static const char *fatfs_error_codes[] = { + "(0) Succeeded", + "(1) A hard error occurred in the low level disk I/O layer", + "(2) Assertion failed", + "(3) The physical drive cannot work", + "(4) Could not find the file", + "(5) Could not find the path", + "(6) The path name format is invalid", + "(7) Access denied due to prohibited access or directory full", + "(8) Access denied due to prohibited access", + "(9) The file/directory object is invalid", + "(10) The physical drive is write protected", + "(11) The logical drive number is invalid", + "(12) The volume has no work area", + "(13) There is no valid FAT volume", + "(14) The f_mkfs() aborted due to any problem", + "(15) Could not get a grant to access the volume within defined period", + "(16) The operation is rejected according to the file sharing policy", + "(17) LFN working buffer could not be allocated", + "(18) Number of open files > FF_FS_LOCK", + "(19) Given parameter is invalid", +}; + + +#define FF_CHECK(x) { \ + FRESULT fatfs_result = x; \ + if (fatfs_result) { \ + LOG_E("fatfs error \"%s\" at [%s:%d] in expr: %s\r\n", fatfs_error_codes[fatfs_result], __FILE__, __LINE__, #x); \ + while (1); \ + } \ +} void storage_run_menu (storage_backend_t storage_backend, boot_info_t *boot_info, sc64_info_t *sc64_info) { + FATFS fs; + FIL fil; + + if (storage_backend == STORAGE_BACKEND_SD) { + FF_CHECK(f_mount(&fs, "0:", 1)); + FF_CHECK(f_chdrive("0:")); + } else if (storage_backend == STORAGE_BACKEND_USB) { + FF_CHECK(f_mount(&fs, "1:", 1)); + FF_CHECK(f_chdrive("1:")); + } else { + LOG_E("Unknown storage backend %d\r\n", storage_backend); + while (1); + } + + FF_CHECK(f_open(&fil, "sc64menu.elf", FA_READ)); + // TODO: Implement ELF loader here - while (1); + + FF_CHECK(f_close(&fil)); + + // TODO: Execute ELF here + // menu(&boot_info, &sc64_info); + + boot_info->device_type = BOOT_DEVICE_TYPE_ROM; } diff --git a/sw/riscv/SC64.ld b/sw/riscv/SC64.ld index 2e8471e..bae2abd 100644 --- a/sw/riscv/SC64.ld +++ b/sw/riscv/SC64.ld @@ -15,7 +15,7 @@ SECTIONS { . = ALIGN(4); _stext = .; *(.text .text.* .gnu.linkonce.t.*) - *(.rdata .rodata .rodata.* .gnu.linkonce.r.*) + *(.rodata .rodata.* .gnu.linkonce.r.*) . = ALIGN(4); _etext = .; } > ram AT > rom