Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactoring of bootloader functions #399

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions targets/stm32l432/bootloader/bootloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,26 @@ static void erase_application()
}
}

static void disable_bootloader()
/**
* Clear 4 bytes of the last 8 bytes of the last application page-1,
* which is 108th.
* @param offset byte offset - skip the first offset bytes
*/
static void clear_page_bytes(uint8_t offset)
{
// Clear last 4 bytes of the last application page-1, which is 108th
uint8_t page[PAGE_SIZE];
memmove(page, (uint8_t*)LAST_ADDR, PAGE_SIZE);
memset(page+PAGE_SIZE -4, 0, 4);
memset(page+PAGE_SIZE-8+offset, 0, 4);
flash_erase_page(LAST_PAGE);
flash_write(LAST_ADDR, page, PAGE_SIZE);
}

static void disable_bootloader()
{
// Clear last 4 bytes of the page 108.
clear_page_bytes(4);
}

static void authorize_application()
{
// Do nothing, if is_authorized_to_boot() returns true, otherwise
Expand All @@ -84,14 +94,9 @@ static void authorize_application()
// uint32_t * ptr;
// ptr = (uint32_t *)AUTH_WORD_ADDR;
// flash_write((uint32_t)ptr, (uint8_t *)&zero, 4);
uint8_t page[PAGE_SIZE];
if (is_authorized_to_boot())
return;
// FIXME refactor: code same as in disable_bootloader(), except clearing start address (-8)
memmove(page, (uint8_t*)LAST_ADDR, PAGE_SIZE);
memset(page+PAGE_SIZE -8, 0, 4);
flash_erase_page(LAST_PAGE);
flash_write(LAST_ADDR, page, PAGE_SIZE);
clear_page_bytes(0);
}

int is_authorized_to_boot()
Expand Down Expand Up @@ -336,4 +341,4 @@ uint32_t ctap_atomic_count(uint32_t amount)
static uint32_t count = 1000;
count += (amount + 1);
return count;
}
}