Skip to content

Commit

Permalink
fixup! chore(core, legacy, storage): refactor flash drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
cepetr committed Nov 15, 2023
1 parent 1bd60f7 commit 1f645a3
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions storage/tests/c/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,29 +111,45 @@ secbool flash_sector_erase(uint16_t sector) {
return sectrue;
}

secbool flash_write_byte(uint16_t sector, uint32_t offset, uint8_t data) {
uint8_t *flash = (uint8_t *)flash_get_address(sector, offset, 1);
if (!flash) {
static secbool flash_write(uint16_t sector, uint32_t offset,
const uint8_t *data, size_t data_size) {
// check proper alignment
if ((offset % data_size) != 0) {
return secfalse;
}
if ((flash[0] & data) != data) {
return secfalse; // we cannot change zeroes to ones
}
flash[0] = data;
return sectrue;
}

secbool flash_write_word(uint16_t sector, uint32_t offset, uint32_t data) {
if (offset % 4) { // we write only at 4-byte boundary
return secfalse;
}
uint32_t *flash = (uint32_t *)flash_get_address(sector, offset, sizeof(data));
if (!flash) {
uint8_t *flash = (uint8_t *)flash_get_address(sector, offset, data_size);

if (flash == NULL) {
return secfalse;
}
if ((flash[0] & data) != data) {
return secfalse; // we cannot change zeroes to ones

// check if not writing ones to zeroes
for (size_t i = 0; i < data_size; i++) {
if (data[i] != (data[i] & flash[i])) {
return secfalse;
}
}
flash[0] = data;

memcpy(flash, data, data_size);

return sectrue;
}

secbool flash_write_byte(uint16_t sector, uint32_t offset, uint8_t data) {
return flash_write(sector, offset, (uint8_t *)&data, sizeof(uint8_t));
}

secbool flash_write_word(uint16_t sector, uint32_t offset, uint32_t data) {
return flash_write(sector, offset, (uint8_t *)&data, sizeof(uint32_t));
}

secbool flash_write_quadword(uint16_t sector, uint32_t offset,
const uint32_t *data) {
return flash_write(sector, offset, (uint8_t *)data, 4 * sizeof(uint32_t));
}

secbool flash_write_burst(uint16_t sector, uint32_t offset,
const uint32_t *data) {
return flash_write(sector, offset, (uint8_t *)data, 32 * sizeof(uint32_t));
}

0 comments on commit 1f645a3

Please sign in to comment.