Skip to content

Conversation

@cedwies
Copy link
Collaborator

@cedwies cedwies commented Dec 9, 2025

memory_reset_hww combined the CHUNK_1 write result and the shared BLE chunk write using |=, so a later successful write could hide an earlier failure. Change both writes to be checked individually and return false if any of the two write operations fails, but ensuring both operations are attempted.

What could go wrong in the old code:

when _write_chunk(CHUNK_1, chunk.bytes); return false and _write_to_address(FLASH_SHARED_DATA_START, 0, chunk_shared.bytes) returns true, then bool memory_reset_hww(void) returns true due to the |= operator.

Copy link
Collaborator

@benma benma left a comment

Choose a reason for hiding this comment

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

Nice catch!

rust_noise_generate_static_private_key(rust_util_bytes_mut(
chunk.fields.noise_static_private_key, sizeof(chunk.fields.noise_static_private_key)));
bool res = _write_chunk(CHUNK_1, chunk.bytes);
if (!_write_chunk(CHUNK_1, chunk.bytes)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess the original intention was to run the below BLE stuff even if this fails, so to preserve this, just assign bool res1 and bool res2 and return res1 && res2?

@cedwies cedwies force-pushed the cedwies/memory-reset-hww-flash-result branch from 55104dd to 16b777b Compare December 10, 2025 15:06
@cedwies cedwies requested a review from benma December 11, 2025 09:01
@cedwies cedwies marked this pull request as ready for review December 11, 2025 09:02
@NickeZ
Copy link
Collaborator

NickeZ commented Dec 11, 2025

@codex review

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 404 to 407
chunk_shared.fields.ble_identity_address[0] |= 0xc;

memset(&chunk_shared.fields.ble_bond_db, 0xff, sizeof(chunk_shared.fields.ble_bond_db));
res |= _write_to_address(FLASH_SHARED_DATA_START, 0, chunk_shared.bytes);
res = res && _write_to_address(FLASH_SHARED_DATA_START, 0, chunk_shared.bytes);

Choose a reason for hiding this comment

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

P2 Badge Ensure shared flash reset executes even after chunk write failure

Using res = res && _write_to_address(...) short-circuits the shared BLE reset when _write_chunk(CHUNK_1, ...) fails because res is already false. The commit message says both writes should still be attempted, but this change prevents the second write from running and leaves shared BLE data unchanged on partial failures. Consider invoking the shared write unconditionally and combining results afterward so both operations are attempted while still returning false if either fails.

Useful? React with 👍 / 👎.

memory_reset_hww combined the CHUNK_1 write result and the
shared BLE chunk write using |=, so a later successful
write could hide an earlier failure. Change both writes to
be checked individually and return false on the first
failure, making the function only report success when all
flash writes succeed.
@cedwies cedwies force-pushed the cedwies/memory-reset-hww-flash-result branch from 16b777b to 42b1a4c Compare December 11, 2025 14:07

memset(&chunk_shared.fields.ble_bond_db, 0xff, sizeof(chunk_shared.fields.ble_bond_db));
res |= _write_to_address(FLASH_SHARED_DATA_START, 0, chunk_shared.bytes);
res = _write_to_address(FLASH_SHARED_DATA_START, 0, chunk_shared.bytes) && res;
Copy link
Collaborator

Choose a reason for hiding this comment

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

relevant: https://c0x.shape-of-code.com/6.5.13.html

first operand always executes

Copy link
Collaborator

@benma benma left a comment

Choose a reason for hiding this comment

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

utACK

@cedwies cedwies merged commit bea3a85 into BitBoxSwiss:master Dec 11, 2025
36 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.

3 participants