diff --git a/loader/inc/memory.h b/loader/inc/memory.h index e60fb76..5ceeab4 100644 --- a/loader/inc/memory.h +++ b/loader/inc/memory.h @@ -32,7 +32,7 @@ void mem_read_memory(unsigned int* address, uint8_t* buffer, uint32_t length); void mem_write_word(unsigned int* address, uint32_t word); -void mem_write_sector(unsigned int* address, uint8_t* buffer); +void mem_write_sector(unsigned int* address, unsigned int* buffer); void mem_erase_sector(unsigned int* address); diff --git a/loader/src/flash.c b/loader/src/flash.c index 2d48a13..bcd610c 100644 --- a/loader/src/flash.c +++ b/loader/src/flash.c @@ -155,9 +155,9 @@ void update_checksums(const uint32_t* nvm, uint32_t* ram, int sectors) int i; for (i = 0; i < sectors; i++, - nvm += SECTOR_SIZE, ram += SECTOR_SIZE) { + nvm += 64, ram += 64) { mem_erase_sector((unsigned int*)nvm); - mem_write_sector((unsigned int*)nvm, (uint8_t*)ram); + mem_write_sector((unsigned int*)nvm, (unsigned int*)ram); kick_the_watchdog(); } @@ -182,7 +182,7 @@ uint32_t check_and_repair_memory(void) /* foreach sector */ for (i = 0; i < D1_SECTORS; i++, - address1 += SECTOR_SIZE, address2 += SECTOR_SIZE) { + address1 += 64, address2 += 64) { /* calculate checksums */ check1 = checksum_sector(address1); check2 = checksum_sector(address2); @@ -196,7 +196,7 @@ uint32_t check_and_repair_memory(void) (check2 == d2_checksums_ram[i])) { /* restore d1 */ mem_erase_sector(address1); - mem_write_sector(address1, (uint8_t*)address2); + mem_write_sector(address1, address2); /* update d1 checksum */ d1_checksums_ram[i] = check2; @@ -209,7 +209,7 @@ uint32_t check_and_repair_memory(void) (check2 != d2_checksums_ram[i])) { /* restore d2 */ mem_erase_sector(address2); - mem_write_sector(address2, (uint8_t*)address1); + mem_write_sector(address2, address1); /* update d2 checksum */ d2_checksums_ram[i] = check1; @@ -222,7 +222,7 @@ uint32_t check_and_repair_memory(void) /* both bad, restore d2 and calculate both checksums */ /* restore d2 */ mem_erase_sector(address2); - mem_write_sector(address2, (uint8_t*)address1); + mem_write_sector(address2, address1); /* update both checksums */ d1_checksums_ram[i] = check1; diff --git a/loader/src/memory.c b/loader/src/memory.c index d3ef172..8e77d17 100644 --- a/loader/src/memory.c +++ b/loader/src/memory.c @@ -86,10 +86,9 @@ void mem_write_word(unsigned int* address, uint32_t word) /** * Write 256-byte sector. Address must be sector aligned */ -void mem_write_sector(unsigned int* address, uint8_t* buffer) +void mem_write_sector(unsigned int* address, unsigned int* buffer) { int i; - uint8_t tempbuf[0x40]; #ifdef FIX_ERRATA_REV_C_FLASH_10804 /* save CTRLB and disable cache */ @@ -103,9 +102,12 @@ void mem_write_sector(unsigned int* address, uint8_t* buffer) for (i = 0; i < 4; i++) { /* write by page */ /* write address */ NVMCTRL->ADDR.reg = (uint32_t)(address) >> 1; + /* write page. length must be multiple of two */ - memcpy((void*)tempbuf, buffer, 0x40); - memcpy((void*)address, tempbuf, 0x40); + for (int j = 0; j < 0x10; j++) { + address[j] = buffer[j]; + } + /* unlock */ NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY | NVMCTRL_CTRLA_CMD_UR; /* write page */ @@ -115,8 +117,8 @@ void mem_write_sector(unsigned int* address, uint8_t* buffer) NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY | NVMCTRL_CTRLA_CMD_LR; /* next page */ - address += 0x40; - buffer += 0x40; + address += 0x10; + buffer += 0x10; }