SummerCart64/sw/controller/src/update.c

322 lines
11 KiB
C
Raw Normal View History

[SC64][FW][HW][SW] New version based on LCMXO2 FPGA (#19) * isv support + usb/dd improvements * make room for saves * update offset * fixed debug address * idk * exception * ironed out all broken stuff * cleanup * return epc fix * better * more cleanup * even more cleanup * mooore cleanup * fixed printf * no assert * improved docker build, pyft232 instead of pyserial * fixed displaying long message strings description test * just straight cleanup * smallest cleanup * PAL * cpu buffer * n64 bootloader done * super slow usb storage reading implemented * reduced buffer size * usb gets fast * little cleanup * double buffered reads * removed separate event id * ISV in hardware finally * small exception changes * mac testing * py spacing * fsd write, rtc, isv and reset fixes * fixxx * good stopping point * usb fixed? * pretend we have 128 MB sdram * backup * chmod * test * test done * more tests * user rm * help * final fix * updated component values * nice asset names * cic 64dd support * ddipl enable separation * pre DMA rewrite, created dedicated buffer memory space, simplified code * dma rewrite, needs testing * moved xml * dd basics * timing * 64dd working yet again, isv brought back, dma fixes, usb path rewrite, pc code rewrite * added usb read functionality, general cleanup * changed mem addressing * added fpga flash update access * added mcu update * chmod * little cleanup * update format and stuff * fixes * uninitialized fix * small fixes * update fixes * update stuff done * fpga update tested * build time fix * boot fix * test timing * readme test * test 2 * reports * testseet * final * build test * forgot * button and naming * General cleanup And multiline commit message test * Exception screen UI touch ups * display separation and tests beginning * pc software update * pc software done * timing test * delete launch.json * sw fixes * fixed button hole diameter in shell * small cleanup, rpi testing * shell fillet fix, pc rtc printing * added cfg lock mechanism * moved lock to cfg address space * extended ROM and ISV fixes * preliminary sd card support * little sd card cleanup * sd menu fixes * 5 second limit * reduced shell thickness * basic led act blinking * faster sd menu loading * inst cache invalidate * sd card writing is working * SD card CSD and CID registers * wait for previous command * led error codes * fixed cfg_translate_address use * 64dd from sd card working * 64dd speedup and button handling * delayed address latching cycle - might break other builds, needs testing * bootloader improvements * small fixes * return previous cfg when setting new * cache stuff * unfloader debug protocol support * UNFLoader style debug command line support * requirements.txt * shell groove fillet * reset state inside controller * fixed fast PI read, added PI R/W fifo debug info * PI access prioritize * SD clock stop when RX FIFO is more than half full * flash erase method change * CFG error handling, TLOZ MM debug ISV support * CIC5167 support * general fixes * USB unplugged cable handling * turn off led when changing between error/act modes * rtc 2 bit clock stop support * line endings * Revert "line endings" This reverts commit d0ddfe5ec716d2db7c72561703f51a94bf34e6bb. * PI address debug * readme test * diagram update * diagram background * diagram background * diagram background * updated readme
2022-11-10 11:46:54 +01:00
#include "flash.h"
#include "fpga.h"
#include "hw.h"
#include "update.h"
#include "usb.h"
#include "vendor.h"
#define UPDATE_MAGIC_START (0x54535055UL)
#define BOOTLOADER_ADDRESS (0x04E00000UL)
#define BOOTLOADER_LENGTH (0x001E0000UL)
typedef enum {
UPDATE_STATUS_MCU = 1,
UPDATE_STATUS_FPGA = 2,
UPDATE_STATUS_BOOTLOADER = 3,
UPDATE_STATUS_DONE = 0x80,
UPDATE_STATUS_ERROR = 0xFF,
} update_status_t;
typedef enum {
CHUNK_ID_UPDATE_INFO = 1,
CHUNK_ID_MCU_DATA = 2,
CHUNK_ID_FPGA_DATA = 3,
CHUNK_ID_BOOTLOADER_DATA = 4,
} chunk_id_t;
static loader_parameters_t parameters;
static const uint8_t update_token[16] = "SC64 Update v2.0";
static uint8_t status_data[12] = {
'P', 'K', 'T', PACKET_CMD_UPDATE_STATUS,
0, 0, 0, 4,
0, 0, 0, UPDATE_STATUS_ERROR,
};
static uint32_t update_align (uint32_t value) {
if ((value % 16) != 0) {
value += (16 - (value % 16));
}
return value;
}
static uint32_t update_checksum (uint32_t address, uint32_t length) {
uint8_t buffer[128];
uint32_t block_size;
uint32_t checksum = 0;
hw_crc32_reset();
while (length > 0) {
block_size = (length > sizeof(buffer)) ? sizeof(buffer) : length;
fpga_mem_read(address, block_size, buffer);
checksum = hw_crc32_calculate(buffer, block_size);
address += block_size;
length -= block_size;
}
return checksum;
}
static uint32_t update_write_token (uint32_t *address) {
uint32_t length = sizeof(update_token);
fpga_mem_write(*address, sizeof(update_token), (uint8_t *) (update_token));
*address += length;
return length;
}
static uint32_t update_prepare_chunk (uint32_t *address, chunk_id_t chunk_id) {
uint32_t id = (uint32_t) (chunk_id);
uint32_t length = (4 * sizeof(uint32_t));
fpga_mem_write(*address, sizeof(id), (uint8_t *) (&id));
*address += length;
return length;
}
static uint32_t update_finalize_chunk (uint32_t *address, uint32_t length) {
uint32_t chunk_length = ((4 * sizeof(uint32_t)) + length);
uint32_t aligned_chunk_length = update_align(chunk_length);
uint32_t aligned_length = aligned_chunk_length - (2 * sizeof(uint32_t));
uint32_t checksum = update_checksum(*address, length);
fpga_mem_write(*address - (3 * sizeof(uint32_t)), sizeof(aligned_length), (uint8_t *) (&aligned_length));
fpga_mem_write(*address - (2 * sizeof(uint32_t)), sizeof(checksum), (uint8_t *) (&checksum));
fpga_mem_write(*address - sizeof(uint32_t), sizeof(length), (uint8_t *) (&length));
length += (aligned_chunk_length - chunk_length);
*address += length;
return length;
}
static bool update_check_token (uint32_t *address) {
uint8_t buffer[sizeof(update_token)];
fpga_mem_read(*address, sizeof(update_token), buffer);
for (int i = 0; i < sizeof(update_token); i++) {
if (buffer[i] != update_token[i]) {
return true;
}
}
*address += sizeof(update_token);
return false;
}
static bool update_get_chunk (uint32_t *address, chunk_id_t *chunk_id, uint32_t *data_address, uint32_t *data_length) {
uint32_t id;
uint32_t chunk_length;
uint32_t checksum;
fpga_mem_read(*address, sizeof(id), (uint8_t *) (&id));
*chunk_id = (chunk_id_t) (id);
*address += sizeof(id);
fpga_mem_read(*address, sizeof(chunk_length), (uint8_t *) (&chunk_length));
*address += sizeof(chunk_length);
fpga_mem_read(*address, sizeof(checksum), (uint8_t *) (&checksum));
*address += sizeof(checksum);
fpga_mem_read(*address, sizeof(*data_length), (uint8_t *) (data_length));
*address += sizeof(*data_length);
*data_address = *address;
*address += (chunk_length - (2 * sizeof(uint32_t)));
if (checksum != update_checksum(*data_address, *data_length)) {
return true;
}
return false;
}
static void update_blink_led (uint32_t on, uint32_t off, int repeat) {
for (int i = 0; i < repeat; i++) {
hw_gpio_set(GPIO_ID_LED);
hw_delay_ms(on);
hw_gpio_reset(GPIO_ID_LED);
hw_delay_ms(off);
}
}
static void update_status_notify (update_status_t status) {
status_data[sizeof(status_data) - 1] = (uint8_t) (status);
for (int i = 0; i < sizeof(status_data); i++) {
while (!(fpga_usb_status_get() & USB_STATUS_TXE));
fpga_usb_push(status_data[i]);
}
fpga_reg_set(REG_USB_SCR, USB_SCR_WRITE_FLUSH);
if (status == UPDATE_STATUS_DONE) {
update_blink_led(15, 85, 10);
} else if (status == UPDATE_STATUS_ERROR) {
update_blink_led(1000, 1000, 30);
} else {
update_blink_led(15, 185, 2);
hw_delay_ms(500);
}
}
static bool mcu_update (uint32_t address, uint32_t length) {
hw_flash_t buffer;
hw_flash_erase();
for (uint32_t offset = 0; offset < length; offset += sizeof(hw_flash_t)) {
fpga_mem_read(address + offset, sizeof(hw_flash_t), (uint8_t *) (&buffer));
hw_flash_program(offset, buffer);
if (hw_flash_read(offset) != buffer) {
return true;
}
}
return false;
}
static bool bootloader_update (uint32_t address, uint32_t length) {
uint8_t update_buffer[FPGA_MAX_MEM_TRANSFER];
uint8_t verify_buffer[FPGA_MAX_MEM_TRANSFER];
for (uint32_t offset = 0; offset < BOOTLOADER_LENGTH; offset += FLASH_ERASE_BLOCK_SIZE) {
if (flash_erase_block(BOOTLOADER_ADDRESS + offset)) {
return true;
}
[SC64][FW][HW][SW] New version based on LCMXO2 FPGA (#19) * isv support + usb/dd improvements * make room for saves * update offset * fixed debug address * idk * exception * ironed out all broken stuff * cleanup * return epc fix * better * more cleanup * even more cleanup * mooore cleanup * fixed printf * no assert * improved docker build, pyft232 instead of pyserial * fixed displaying long message strings description test * just straight cleanup * smallest cleanup * PAL * cpu buffer * n64 bootloader done * super slow usb storage reading implemented * reduced buffer size * usb gets fast * little cleanup * double buffered reads * removed separate event id * ISV in hardware finally * small exception changes * mac testing * py spacing * fsd write, rtc, isv and reset fixes * fixxx * good stopping point * usb fixed? * pretend we have 128 MB sdram * backup * chmod * test * test done * more tests * user rm * help * final fix * updated component values * nice asset names * cic 64dd support * ddipl enable separation * pre DMA rewrite, created dedicated buffer memory space, simplified code * dma rewrite, needs testing * moved xml * dd basics * timing * 64dd working yet again, isv brought back, dma fixes, usb path rewrite, pc code rewrite * added usb read functionality, general cleanup * changed mem addressing * added fpga flash update access * added mcu update * chmod * little cleanup * update format and stuff * fixes * uninitialized fix * small fixes * update fixes * update stuff done * fpga update tested * build time fix * boot fix * test timing * readme test * test 2 * reports * testseet * final * build test * forgot * button and naming * General cleanup And multiline commit message test * Exception screen UI touch ups * display separation and tests beginning * pc software update * pc software done * timing test * delete launch.json * sw fixes * fixed button hole diameter in shell * small cleanup, rpi testing * shell fillet fix, pc rtc printing * added cfg lock mechanism * moved lock to cfg address space * extended ROM and ISV fixes * preliminary sd card support * little sd card cleanup * sd menu fixes * 5 second limit * reduced shell thickness * basic led act blinking * faster sd menu loading * inst cache invalidate * sd card writing is working * SD card CSD and CID registers * wait for previous command * led error codes * fixed cfg_translate_address use * 64dd from sd card working * 64dd speedup and button handling * delayed address latching cycle - might break other builds, needs testing * bootloader improvements * small fixes * return previous cfg when setting new * cache stuff * unfloader debug protocol support * UNFLoader style debug command line support * requirements.txt * shell groove fillet * reset state inside controller * fixed fast PI read, added PI R/W fifo debug info * PI access prioritize * SD clock stop when RX FIFO is more than half full * flash erase method change * CFG error handling, TLOZ MM debug ISV support * CIC5167 support * general fixes * USB unplugged cable handling * turn off led when changing between error/act modes * rtc 2 bit clock stop support * line endings * Revert "line endings" This reverts commit d0ddfe5ec716d2db7c72561703f51a94bf34e6bb. * PI address debug * readme test * diagram update * diagram background * diagram background * diagram background * updated readme
2022-11-10 11:46:54 +01:00
}
if (flash_program(address, BOOTLOADER_ADDRESS, length)) {
return true;
[SC64][FW][HW][SW] New version based on LCMXO2 FPGA (#19) * isv support + usb/dd improvements * make room for saves * update offset * fixed debug address * idk * exception * ironed out all broken stuff * cleanup * return epc fix * better * more cleanup * even more cleanup * mooore cleanup * fixed printf * no assert * improved docker build, pyft232 instead of pyserial * fixed displaying long message strings description test * just straight cleanup * smallest cleanup * PAL * cpu buffer * n64 bootloader done * super slow usb storage reading implemented * reduced buffer size * usb gets fast * little cleanup * double buffered reads * removed separate event id * ISV in hardware finally * small exception changes * mac testing * py spacing * fsd write, rtc, isv and reset fixes * fixxx * good stopping point * usb fixed? * pretend we have 128 MB sdram * backup * chmod * test * test done * more tests * user rm * help * final fix * updated component values * nice asset names * cic 64dd support * ddipl enable separation * pre DMA rewrite, created dedicated buffer memory space, simplified code * dma rewrite, needs testing * moved xml * dd basics * timing * 64dd working yet again, isv brought back, dma fixes, usb path rewrite, pc code rewrite * added usb read functionality, general cleanup * changed mem addressing * added fpga flash update access * added mcu update * chmod * little cleanup * update format and stuff * fixes * uninitialized fix * small fixes * update fixes * update stuff done * fpga update tested * build time fix * boot fix * test timing * readme test * test 2 * reports * testseet * final * build test * forgot * button and naming * General cleanup And multiline commit message test * Exception screen UI touch ups * display separation and tests beginning * pc software update * pc software done * timing test * delete launch.json * sw fixes * fixed button hole diameter in shell * small cleanup, rpi testing * shell fillet fix, pc rtc printing * added cfg lock mechanism * moved lock to cfg address space * extended ROM and ISV fixes * preliminary sd card support * little sd card cleanup * sd menu fixes * 5 second limit * reduced shell thickness * basic led act blinking * faster sd menu loading * inst cache invalidate * sd card writing is working * SD card CSD and CID registers * wait for previous command * led error codes * fixed cfg_translate_address use * 64dd from sd card working * 64dd speedup and button handling * delayed address latching cycle - might break other builds, needs testing * bootloader improvements * small fixes * return previous cfg when setting new * cache stuff * unfloader debug protocol support * UNFLoader style debug command line support * requirements.txt * shell groove fillet * reset state inside controller * fixed fast PI read, added PI R/W fifo debug info * PI access prioritize * SD clock stop when RX FIFO is more than half full * flash erase method change * CFG error handling, TLOZ MM debug ISV support * CIC5167 support * general fixes * USB unplugged cable handling * turn off led when changing between error/act modes * rtc 2 bit clock stop support * line endings * Revert "line endings" This reverts commit d0ddfe5ec716d2db7c72561703f51a94bf34e6bb. * PI address debug * readme test * diagram update * diagram background * diagram background * diagram background * updated readme
2022-11-10 11:46:54 +01:00
}
for (uint32_t offset = 0; offset < length; offset += sizeof(verify_buffer)) {
fpga_mem_read(address + offset, sizeof(update_buffer), update_buffer);
fpga_mem_read(BOOTLOADER_ADDRESS + offset, sizeof(verify_buffer), verify_buffer);
for (int i = 0; i < sizeof(verify_buffer); i++) {
if ((offset + i) >= length) {
break;
}
[SC64][FW][HW][SW] New version based on LCMXO2 FPGA (#19) * isv support + usb/dd improvements * make room for saves * update offset * fixed debug address * idk * exception * ironed out all broken stuff * cleanup * return epc fix * better * more cleanup * even more cleanup * mooore cleanup * fixed printf * no assert * improved docker build, pyft232 instead of pyserial * fixed displaying long message strings description test * just straight cleanup * smallest cleanup * PAL * cpu buffer * n64 bootloader done * super slow usb storage reading implemented * reduced buffer size * usb gets fast * little cleanup * double buffered reads * removed separate event id * ISV in hardware finally * small exception changes * mac testing * py spacing * fsd write, rtc, isv and reset fixes * fixxx * good stopping point * usb fixed? * pretend we have 128 MB sdram * backup * chmod * test * test done * more tests * user rm * help * final fix * updated component values * nice asset names * cic 64dd support * ddipl enable separation * pre DMA rewrite, created dedicated buffer memory space, simplified code * dma rewrite, needs testing * moved xml * dd basics * timing * 64dd working yet again, isv brought back, dma fixes, usb path rewrite, pc code rewrite * added usb read functionality, general cleanup * changed mem addressing * added fpga flash update access * added mcu update * chmod * little cleanup * update format and stuff * fixes * uninitialized fix * small fixes * update fixes * update stuff done * fpga update tested * build time fix * boot fix * test timing * readme test * test 2 * reports * testseet * final * build test * forgot * button and naming * General cleanup And multiline commit message test * Exception screen UI touch ups * display separation and tests beginning * pc software update * pc software done * timing test * delete launch.json * sw fixes * fixed button hole diameter in shell * small cleanup, rpi testing * shell fillet fix, pc rtc printing * added cfg lock mechanism * moved lock to cfg address space * extended ROM and ISV fixes * preliminary sd card support * little sd card cleanup * sd menu fixes * 5 second limit * reduced shell thickness * basic led act blinking * faster sd menu loading * inst cache invalidate * sd card writing is working * SD card CSD and CID registers * wait for previous command * led error codes * fixed cfg_translate_address use * 64dd from sd card working * 64dd speedup and button handling * delayed address latching cycle - might break other builds, needs testing * bootloader improvements * small fixes * return previous cfg when setting new * cache stuff * unfloader debug protocol support * UNFLoader style debug command line support * requirements.txt * shell groove fillet * reset state inside controller * fixed fast PI read, added PI R/W fifo debug info * PI access prioritize * SD clock stop when RX FIFO is more than half full * flash erase method change * CFG error handling, TLOZ MM debug ISV support * CIC5167 support * general fixes * USB unplugged cable handling * turn off led when changing between error/act modes * rtc 2 bit clock stop support * line endings * Revert "line endings" This reverts commit d0ddfe5ec716d2db7c72561703f51a94bf34e6bb. * PI address debug * readme test * diagram update * diagram background * diagram background * diagram background * updated readme
2022-11-10 11:46:54 +01:00
if (update_buffer[i] != verify_buffer[i]) {
return true;
}
}
}
return false;
}
update_error_t update_backup (uint32_t address, uint32_t *length) {
uint32_t mcu_length;
uint32_t fpga_length;
uint32_t bootloader_length;
*length = update_write_token(&address);
*length += update_prepare_chunk(&address, CHUNK_ID_MCU_DATA);
mcu_length = hw_flash_size();
for (uint32_t offset = 0; offset < mcu_length; offset += sizeof(hw_flash_t)) {
hw_flash_t buffer = hw_flash_read(offset);
fpga_mem_write(address + offset, sizeof(hw_flash_t), (uint8_t *) (&buffer));
}
*length += update_finalize_chunk(&address, mcu_length);
*length += update_prepare_chunk(&address, CHUNK_ID_FPGA_DATA);
if (vendor_backup(address, &fpga_length) != VENDOR_OK) {
return UPDATE_ERROR_READ;
}
*length += update_finalize_chunk(&address, fpga_length);
*length += update_prepare_chunk(&address, CHUNK_ID_BOOTLOADER_DATA);
bootloader_length = BOOTLOADER_LENGTH;
for (uint32_t offset = 0; offset < bootloader_length; offset += FPGA_MAX_MEM_TRANSFER) {
fpga_mem_copy(BOOTLOADER_ADDRESS + offset, address + offset, FPGA_MAX_MEM_TRANSFER);
}
*length += update_finalize_chunk(&address, bootloader_length);
return UPDATE_OK;
}
update_error_t update_prepare (uint32_t address, uint32_t length) {
uint32_t end_address = (address + length);
chunk_id_t id;
uint32_t data_address;
uint32_t data_length;
if (update_check_token(&address)) {
return UPDATE_ERROR_TOKEN;
}
parameters.flags = 0;
parameters.mcu_address = 0;
parameters.fpga_address = 0;
parameters.bootloader_address = 0;
while (address < end_address) {
if (update_get_chunk(&address, &id, &data_address, &data_length)) {
return UPDATE_ERROR_CHECKSUM;
}
switch (id) {
case CHUNK_ID_UPDATE_INFO:
break;
case CHUNK_ID_MCU_DATA:
if (data_length > hw_flash_size()) {
return UPDATE_ERROR_SIZE;
}
parameters.flags |= LOADER_FLAGS_UPDATE_MCU;
parameters.mcu_address = data_address;
break;
case CHUNK_ID_FPGA_DATA:
if (data_length > vendor_flash_size()) {
return UPDATE_ERROR_SIZE;
}
parameters.flags |= LOADER_FLAGS_UPDATE_FPGA;
parameters.fpga_address = data_address;
break;
case CHUNK_ID_BOOTLOADER_DATA:
if (data_length > BOOTLOADER_LENGTH) {
return UPDATE_ERROR_SIZE;
}
parameters.flags |= LOADER_FLAGS_UPDATE_BOOTLOADER;
parameters.bootloader_address = data_address;
break;
default:
return UPDATE_ERROR_UNKNOWN_CHUNK;
}
}
return UPDATE_OK;
}
void update_start (void) {
parameters.magic = UPDATE_MAGIC_START;
hw_loader_reset(&parameters);
}
bool update_check (void) {
hw_loader_get_parameters(&parameters);
return (parameters.magic == UPDATE_MAGIC_START);
}
void update_perform (void) {
uint32_t length;
if (parameters.flags & LOADER_FLAGS_UPDATE_MCU) {
update_status_notify(UPDATE_STATUS_MCU);
fpga_mem_read(parameters.mcu_address - 4, sizeof(length), (uint8_t *) (&length));
if (mcu_update(parameters.mcu_address, length)) {
update_status_notify(UPDATE_STATUS_ERROR);
while (1);
}
}
if (parameters.flags & LOADER_FLAGS_UPDATE_FPGA) {
update_status_notify(UPDATE_STATUS_FPGA);
fpga_mem_read(parameters.fpga_address - 4, sizeof(length), (uint8_t *) (&length));
if (vendor_update(parameters.fpga_address, length) != VENDOR_OK) {
update_status_notify(UPDATE_STATUS_ERROR);
while (1);
}
}
if (parameters.flags & LOADER_FLAGS_UPDATE_BOOTLOADER) {
update_status_notify(UPDATE_STATUS_BOOTLOADER);
fpga_mem_read(parameters.bootloader_address - 4, sizeof(length), (uint8_t *) (&length));
if (bootloader_update(parameters.bootloader_address, length)) {
update_status_notify(UPDATE_STATUS_ERROR);
while (1);
}
}
update_status_notify(UPDATE_STATUS_DONE);
vendor_reconfigure();
parameters.magic = 0;
hw_loader_reset(&parameters);
}