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

add emergency fallback test, currently fails due to #463

Merged
merged 1 commit into from
Jul 10, 2024

Conversation

jpbland1
Copy link
Contributor

the sector flags needing to be reset by update trigger

Copy link
Member

@danielinux danielinux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's modify hal_flash_write as discussed on slack, to ensure we simulate the same behavior as an actual FLASH memory (& operation on existing content). That will make the test fail upon invalid state changes

hal/sim.c Show resolved Hide resolved
@jpbland1 jpbland1 requested a review from danielinux July 1, 2024 04:56
@dgarske dgarske removed their assignment Jul 1, 2024
Copy link
Member

@danielinux danielinux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Please fix style and squash.

* we're updating or not */
if (flag != SECT_FLAG_NEW) {
resume = 1;
if (cur_v < up_v && fallback_allowed == 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parenthesis

src/update_flash.c Show resolved Hide resolved
if (updateRet || updateState != IMG_STATE_UPDATING) {
wolfBoot_update_trigger();
if (resumedFinalErase != 0) {
if (bootRet == 0 && bootState == IMG_STATE_TESTING) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: parenthesis ((bootRet == 0) && (bootState == IMG_STATE_TESTING))

/* Check for new updates in the UPDATE partition or if we were
* interrupted during the flags setting */
}
else if (updateRet == 0 && updateState == IMG_STATE_UPDATING) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parenthesis

* header we can't determine the direction by version numbers. instead
* use the update partition state, updating means regular, new means
* reverting */
if (stateRet == 0 && (flag != SECT_FLAG_NEW || cur_v == 0)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parenthesis around "==" conditions please

/* Sanity check to prevent dereferencing unaligned half-words */
if ((((size_t)p) & 0x01) != 0) {
if (*p == HDR_PADDING || (((size_t)p) & 0x01) != 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: parenthesys around "==" conditions

@dgarske
Copy link
Contributor

dgarske commented Jul 10, 2024

@jpbland1 please work on cleaning up these minor issues and it will be ready for merge.

@@ -257,16 +257,16 @@ extern "C" {
#ifndef WOLFBOOT_FLAGS_INVERT
#define IMG_STATE_NEW 0xFF
#define IMG_STATE_UPDATING 0x70
#define IMG_STATE_FINAL_FLAGS 0x30
#define IMG_STATE_FINAL_SWAP 0x30
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This value cannot be renamed because it will break customers. Either use the old name or add a new one.

@jpbland1 jpbland1 force-pushed the bad-image-recovery branch 2 times, most recently from 26bc20b to 94848de Compare July 10, 2024 18:39
@jpbland1 jpbland1 requested a review from dgarske July 10, 2024 19:06
/* Sanity check to prevent dereferencing unaligned half-words */
if ((((size_t)p) & 0x01) != 0) {
if ((*p == HDR_PADDING) || ((((size_t)p) & 0x01) != 0)) {
/* Padding byte (skip one position) */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combing this logic is okay, but please update the comment. This is skipping a byte if its a padding byte or if its not an aligned half word.

@@ -710,13 +708,7 @@ static void key_sha384(uint8_t key_slot, uint8_t *hash)
return;

wc_InitSha384(&sha384_ctx);
while (i < (uint32_t)(pubkey_sz)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you removing this logic? Guessing it is not required since the public key buffer is fully available? Is it possible that WOLFBOOT_SHA_BLOCK_SIZE might be useful for other reasons?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wolfCrypts internal logic already loops over in the correct block size increments, the keybuffer is fully available so it's just wasted code size

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a different block size value. This is used to break the SHA processing into smaller chunks. This is typically used when the value being hashed comes from flash. This would read (by default) 128 bytes at a time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good to know, then it's almost certainly being re-adjusted by

        if ((i + blksz) > (uint32_t)pubkey_sz)
            blksz = pubkey_sz - i;

for ed and ecc keys, maybe rsa has bigger keys but in any case the entire key is still available and wc_Sha384Update can handle it all in a single function call

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree and I am okay removing this logic. Please make sure you do it consistently for the other algorithms too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No fun! I wanted to make an intern do that

{
hal_flash_erase(addr_write, NVM_CACHE_SIZE);
}
/* Ensure that the destination was erased */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure always doing the erase of the trailing here (vs conditional before) is the right assumption?

Copy link
Contributor Author

@jpbland1 jpbland1 Jul 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was part of code size reduction, worst case scenario from the old code we call erase on an already erased sector

uint8_t st;
int eraseLen = WOLFBOOT_SECTOR_SIZE
#ifdef NVM_FLASH_WRITEONCE
/* need to erase the redundant sector too */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment indent should match code below.

st = IMG_STATE_TESTING;
wolfBoot_set_partition_state(PART_BOOT, st);
/* start re-entrant final erase */
wolfBoot_swap_and_final_erase(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the return code not checked?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return code is only to tell wolfBoot_start if the final swap was resumed or not

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then please document this in the code at this line.... why we don't need to check the return code.

@@ -52,6 +52,9 @@ int do_cmd(const char *cmd)
if (strcmp(cmd, "powerfail") == 0) {
return 1;
}
if (strcmp(cmd, "emergency") == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update documentation for Sim to explain "emergency"

@@ -194,6 +194,91 @@ static int RAMFUNCTION wolfBoot_copy_sector(struct wolfBoot_image *src,
return pos;
}

static int wolfBoot_swap_and_final_erase(int resume)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function always needed? Should it be wrapped with #ifndef DISABLE_BACKUP?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had thought it was never called for DISABLE_BACKUP but it's also called in wolfBoot_start, good point

tools/test.mk Outdated
@@ -977,29 +977,29 @@ test-all: clean


test-size-all:
make test-size SIGN=NONE LIMIT=4776
make test-size SIGN=NONE LIMIT=4917
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these the actual increased values? Would you like me to recompute updated actuals? On my Ubuntu 24 box I can reproduce the values that match GitHub CI.

@dgarske dgarske merged commit b9dc7ee into wolfSSL:master Jul 10, 2024
94 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants