trezorhal: use const where possible for flash

pull/25/head
Pavol Rusnak 7 years ago
parent 30363305f4
commit ee7d260d2f
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

@ -13,7 +13,7 @@
// see docs/memory.md for more information
const uint32_t FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT + 1] = {
static const uint32_t FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT + 1] = {
[ 0] = 0x08000000, // - 0x08003FFF | 16 KiB
[ 1] = 0x08004000, // - 0x08007FFF | 16 KiB
[ 2] = 0x08008000, // - 0x0800BFFF | 16 KiB
@ -66,7 +66,7 @@ const void *flash_get_address(uint8_t sector, uint32_t offset, uint32_t size)
}
uint32_t addr = FLASH_SECTOR_TABLE[sector];
uint32_t next = FLASH_SECTOR_TABLE[sector + 1];
if (offset + size > next - addr) {
if (addr + offset + size > next) {
return NULL;
}
return (const uint8_t *)addr + offset;
@ -92,7 +92,7 @@ secbool flash_erase_sectors(const uint8_t *sectors, int len, void (*progress)(in
return secfalse;
}
// check whether the sector was really deleted (contains only 0xFF)
uint32_t addr_start = FLASH_SECTOR_TABLE[sectors[i]], addr_end = FLASH_SECTOR_TABLE[sectors[i] + 1];
const uint32_t addr_start = FLASH_SECTOR_TABLE[sectors[i]], addr_end = FLASH_SECTOR_TABLE[sectors[i] + 1];
for (uint32_t addr = addr_start; addr < addr_end; addr += 4) {
if (*((const uint32_t *)addr) != 0xFFFFFFFF) {
flash_lock();
@ -135,7 +135,7 @@ secbool flash_read_word_rel(uint8_t sector, uint32_t offset, uint32_t *data)
if (offset % 4 != 0) {
return secfalse;
}
*data = *((uint32_t *) FLASH_SECTOR_TABLE[sector] + offset);
*data = *((const uint32_t *)FLASH_SECTOR_TABLE[sector] + offset);
return sectrue;
}

@ -43,8 +43,6 @@
// note: FLASH_SR_RDERR is STM32F42xxx and STM32F43xxx specific (STM32F427) (reference RM0090 section 3.7.5)
#define FLASH_STATUS_ALL_FLAGS (FLASH_SR_RDERR | FLASH_SR_PGSERR | FLASH_SR_PGPERR | FLASH_SR_PGAERR | FLASH_SR_WRPERR | FLASH_SR_SOP | FLASH_SR_EOP)
extern const uint32_t FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT + 1];
secbool flash_init(void);
secbool flash_unlock(void);

@ -144,7 +144,10 @@ secbool check_image_contents(const image_header * const hdr, uint32_t firstskip,
if (0 == sectors || blocks < 1) {
return secfalse;
}
const void *data = (const void *)(FLASH_SECTOR_TABLE[sectors[0]] + firstskip);
const void *data = flash_get_address(sectors[0], firstskip, IMAGE_CHUNK_SIZE - firstskip);
if (!data) {
return secfalse;
}
int remaining = hdr->codelen;
if (sectrue != check_single_hash(hdr->hashes, data, MIN(remaining, IMAGE_CHUNK_SIZE - firstskip))) {
return secfalse;
@ -155,7 +158,10 @@ secbool check_image_contents(const image_header * const hdr, uint32_t firstskip,
if (block >= blocks) {
return secfalse;
}
data = (const void *)FLASH_SECTOR_TABLE[sectors[block]];
data = flash_get_address(sectors[block], 0, IMAGE_CHUNK_SIZE);
if (!data) {
return secfalse;
}
if (sectrue != check_single_hash(hdr->hashes + block * 32, data, MIN(remaining, IMAGE_CHUNK_SIZE))) {
return secfalse;
}

@ -86,11 +86,11 @@ const void *flash_get_address(uint8_t sector, uint32_t offset, uint32_t size)
if (sector >= SECTOR_COUNT) {
return NULL;
}
uint32_t sector_size = sector_table[sector + 1] - sector_table[sector];
const uint32_t sector_size = sector_table[sector + 1] - sector_table[sector];
if (offset + size > sector_size) {
return NULL;
}
uint32_t sector_offset = sector_table[sector] - sector_table[0];
const uint32_t sector_offset = sector_table[sector] - sector_table[0];
return flash_buffer + sector_offset + offset;
}
@ -100,9 +100,9 @@ secbool flash_erase_sectors(const uint8_t *sectors, int len, void (*progress)(in
progress(0, len);
}
for (int i = 0; i < len; i++) {
uint8_t sector = sectors[i];
uint32_t offset = sector_table[sector] - sector_table[0];
uint32_t size = sector_table[sector + 1] - sector_table[sector];
const uint8_t sector = sectors[i];
const uint32_t offset = sector_table[sector] - sector_table[0];
const uint32_t size = sector_table[sector + 1] - sector_table[sector];
memset(flash_buffer + offset, 0xFF, size);
if (progress) {
progress(i + 1, len);
@ -148,7 +148,7 @@ secbool flash_read_word_rel(uint8_t sector, uint32_t offset, uint32_t *data)
if (offset % 4) { // we read only at 4-byte boundary
return secfalse;
}
uint32_t *flash = (uint32_t *)flash_get_address(sector, offset, sizeof(data));
const uint32_t *flash = (const uint32_t *)flash_get_address(sector, offset, sizeof(data));
if (!flash) {
return secfalse;
}

Loading…
Cancel
Save