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

bootloader refactoring #4572

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

bootloader refactoring #4572

wants to merge 13 commits into from

Conversation

TychoVrahe
Copy link
Contributor

@TychoVrahe TychoVrahe commented Feb 4, 2025

This PR implements major bootloader refactoring - as preparation for adding BLE.

  • overall structure should be close to how things are done in firmware/micropython
  • cleaner separation of communication layers
  • introducing workflows, separate of communication
  • workflows run from menu use same code as workflows triggered by communication
  • introducing unified polling mechanism
  • some error handling in communication is improved/introduced

@TychoVrahe TychoVrahe self-assigned this Feb 4, 2025
Copy link

github-actions bot commented Feb 4, 2025

core UI changes device test click test persistence test
T2T1 Model T test(screens) main(screens) test(screens) main(screens) test(screens) main(screens)
T3B1 Safe 3 test(screens) main(screens) test(screens) main(screens) test(screens) main(screens)
T3T1 Safe 5 test(screens) main(screens) test(screens) main(screens) test(screens) main(screens)
All main(screens)

@TychoVrahe TychoVrahe force-pushed the tychovrahe/bootloader/reorg branch 5 times, most recently from bedf3bb to 97363e2 Compare February 6, 2025 15:14
@TychoVrahe TychoVrahe force-pushed the tychovrahe/bootloader/reorg branch from f37dfa0 to 8e4557e Compare February 7, 2025 20:51
@TychoVrahe TychoVrahe marked this pull request as ready for review February 9, 2025 20:56
@TychoVrahe TychoVrahe requested a review from prusnak as a code owner February 9, 2025 20:56
@TychoVrahe TychoVrahe requested review from matejcik, hiviah and cepetr and removed request for prusnak February 9, 2025 20:56
Copy link
Contributor

@hiviah hiviah left a comment

Choose a reason for hiding this comment

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

Would be nicer without the hardcoded ui_result constants in workflow_bootloader, but they were there before he refactor.

core/embed/projects/bootloader/main.c Outdated Show resolved Hide resolved
core/embed/projects/bootloader/bootui.h Outdated Show resolved Hide resolved
const image_header *const hdr) {
workflow_reset_jump();
ui_set_initial_setup(true);
ui_screen_connect();
Copy link
Contributor

Choose a reason for hiding this comment

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

If we called redraw_screen_wait() at the beginning of workflow_host_control(), we would not need to call ui_screen_connect() here. The same applies to workflow_bootloader and workflow_empty_device.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed 387bba0

Copy link
Contributor

Choose a reason for hiding this comment

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

It seems to me that we cannot call ui_screen_connect() at the beginning of workflow_host_control(), as it doesn’t work well for workflow_empty_device(). I think we can simply call redraw_screen_wait() that will work well for all workflows.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in 016ac04

core/embed/projects/bootloader/workflow/wf_auto_update.c Outdated Show resolved Hide resolved
core/embed/projects/bootloader/poll.c Outdated Show resolved Hide resolved
ensure(dont_optimize_out_true * (workflow_is_jump_allowed_1() ==
workflow_is_jump_allowed_2()),
NULL);
#ifdef USE_RESET_TO_BOOT
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we don’t need to set firmware_jump_fn here, as it is set a bit later, right after the switch statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

tried to simplify 387bba0

Copy link
Contributor

Choose a reason for hiding this comment

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

It seems to me that now jump_to_fw_through_reset is never called.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

seems to me that we can actually always jump to fw through reset after installation.. which would look like this and is nice simplification: 016ac04

but please check my logic

core/embed/projects/bootloader/main.c Outdated Show resolved Hide resolved
core/embed/projects/bootloader/workflow/workflow.h Outdated Show resolved Hide resolved
core/embed/projects/bootloader/main.c Outdated Show resolved Hide resolved
secbool send_msg_request_firmware(protob_iface_t *iface, uint32_t offset,
uint32_t length) {
if (iface == NULL) {
return sectrue;
Copy link
Contributor

Choose a reason for hiding this comment

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

The recv_msg_xxx() functions return secfalse if iface == NULL, while the send_msg_xxx() function returns sectrue for the same condition. Shouldn’t we unify this?

Copy link
Contributor Author

@TychoVrahe TychoVrahe Feb 13, 2025

Choose a reason for hiding this comment

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

unified at sectrue - as i am using NULL for workflow run from menu, where no communication is needed 3fecd5e

Copy link
Contributor

Choose a reason for hiding this comment

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

As we discussed in person, we could remove all if (iface == NULL) conditions and introduce a “null/blackhole” interface instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

here: 372eae4

not entirely satisfied though. seems like wire is the wrong layer, the protob iface should actually be the null one, but its api isn't really ready for that.

// read function pointer
int (*read)(uint8_t *buffer, size_t buffer_size);

void (*error)(void);
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like the error handler is called by the codec in the event of a fatal communication error. Shouldn’t the codec signal the error to a higher level instead? It seems to me that the error handler is compensating for something missing between the codec and the state machines above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

basically we use this for RSOD when there is such communication error. We could signal this above but then we need to add the RSOD everywhere where there is receiving a message, which seems just work with little effect.

But i agree that we will need to rethink this - but lets postpone it until we add BLE to bootloader, as RSOD on BLE comm error might be a bit too much, so lets handle this in BLE context in separate PR.

core/embed/projects/bootloader/wire/codec_v1.h Outdated Show resolved Hide resolved
core/embed/projects/bootloader/wire/wire_iface_usb.h Outdated Show resolved Hide resolved
@TychoVrahe TychoVrahe linked an issue Feb 13, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🏃‍♀️ In progress
Development

Successfully merging this pull request may close these issues.

bootloader and boardloader code reorganization
3 participants