Skip to content

Commit cd4c4cc

Browse files
authored
new bootloader support upgrading multiple files && adjust touchscreen sensitivity. (#287)
* Support upgrading multiple files in bootloader. * Adjust touchscreen sensitivity. * Enable double-tap to wake screen. * Optimize app list display. * Don't disable interrupt in boot since legacy firmware doesn't enable interrupt.
1 parent 09b3718 commit cd4c4cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1235
-473
lines changed

core/embed/bootloader/bootui.c

Lines changed: 128 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ const char* format_ver(const char* format, uint32_t version) {
118118
static uint16_t boot_background;
119119
static bool ble_name_show = false;
120120
static int ui_bootloader_page_current = 0;
121+
122+
static int current_progress_value = 0;
123+
124+
int get_current_progress_value(void) { return current_progress_value; }
125+
126+
char* format_progress_value(char* prefix) {
127+
static char buf[128] = {0};
128+
mini_snprintf(buf, sizeof(buf), "%s %d", prefix, current_progress_value);
129+
return buf;
130+
}
131+
121132
int get_ui_bootloader_page_current(void) { return ui_bootloader_page_current; }
122133

123134
void ui_logo_onekey(void) {
@@ -252,9 +263,7 @@ void ui_screen_firmware_fingerprint(const image_header* const hdr) {
252263

253264
// install UI
254265

255-
void ui_screen_install_confirm_newvendor_or_downgrade_wipe(
256-
const vendor_header* const vhdr, const image_header* const hdr,
257-
secbool downgrade_wipe) {
266+
void ui_screen_install_confirm_newvendor_or_downgrade_wipe(char* new_version) {
258267
vendor_header current_vhdr;
259268
image_header current_hdr;
260269
// char str[128] = {0};
@@ -272,10 +281,8 @@ void ui_screen_install_confirm_newvendor_or_downgrade_wipe(
272281
ui_statusbar_update();
273282
ui_logo_warning();
274283

275-
display_text_center(
276-
MAX_DISPLAY_RESX / 2, TITLE_OFFSET_Y,
277-
(sectrue == downgrade_wipe) ? "Firmware Downgrade" : "Vendor Change", -1,
278-
FONT_PJKS_BOLD_38, COLOR_BL_FG, COLOR_BL_BG);
284+
display_text_center(MAX_DISPLAY_RESX / 2, TITLE_OFFSET_Y, "Vendor Change", -1,
285+
FONT_PJKS_BOLD_38, COLOR_BL_FG, COLOR_BL_BG);
279286
display_text_center(DISPLAY_RESX / 2, SUBTITLE_OFFSET_Y,
280287
"Still installing this firmware?", -1, FONT_NORMAL,
281288
COLOR_BL_SUBTITLE, COLOR_BL_BG);
@@ -293,17 +300,10 @@ void ui_screen_install_confirm_newvendor_or_downgrade_wipe(
293300
// } while (split);
294301
display_bar_radius_ex(BOARD_OFFSET_X, 295, BUTTON_FULL_WIDTH, BUTTON_HEIGHT,
295302
COLOR_BL_PANEL, COLOR_BL_BG, BUTTON_RADIUS);
296-
const char* ver_str = format_ver("%d.%d.%d", current_hdr.onekey_version);
297-
display_text_right(MAX_DISPLAY_RESX / 2 - 25, 350, ver_str, -1, FONT_NORMAL,
298-
COLOR_BL_SUBTITLE, COLOR_BL_DARK);
299-
ver_str = format_ver("%d.%d.%d", hdr->onekey_version);
300303

301-
display_text(MAX_DISPLAY_RESX / 2 + 25, 350, ver_str, -1, FONT_NORMAL,
304+
display_text(MAX_DISPLAY_RESX / 2 + 25, 350, new_version, -1, FONT_NORMAL,
302305
COLOR_BL_SUBTITLE, COLOR_BL_DARK);
303306

304-
display_image(227, 330, 25, 23, toi_icon_arrow_right + 12,
305-
sizeof(toi_icon_arrow_right) - 12);
306-
307307
ui_confirm_cancel_buttons("Cancel", "Install", COLOR_BL_DARK, COLOR_BL_FAIL);
308308
}
309309

@@ -333,20 +333,31 @@ void ui_screen_confirm(char* title, char* note_l1, char* note_l2, char* note_l3,
333333
ui_confirm_cancel_buttons("Back", "OK", COLOR_BL_DARK, COLOR_BL_FAIL);
334334
}
335335

336+
void ui_screen_progress_bar_init(char* title, char* notes, int progress) {
337+
ui_statusbar_update();
338+
ui_logo_onekey();
339+
if (title != NULL) {
340+
display_text_center(DISPLAY_RESX / 2, TITLE_OFFSET_Y, title, -1,
341+
FONT_PJKS_BOLD_38, COLOR_BL_FG, COLOR_BL_BG);
342+
}
343+
display_progress(notes ? notes : "Keep connected.", progress);
344+
}
345+
336346
void ui_screen_progress_bar_prepare(char* title, char* notes) {
337347
ui_statusbar_update();
338348
ui_logo_onekey();
339349
ui_screen_progress_bar_update(title, notes, -1);
340350
}
341351

342352
void ui_screen_progress_bar_update(char* title, char* notes, int progress) {
343-
if (title != NULL)
353+
if (title != NULL) {
344354
display_text_center(DISPLAY_RESX / 2, TITLE_OFFSET_Y, title, -1,
345355
FONT_PJKS_BOLD_38, COLOR_BL_FG, COLOR_BL_BG);
356+
}
346357

347358
if ((progress >= 0) && (progress <= 100)) {
348359
if (progress > 0) {
349-
display_progress(NULL, progress * 10);
360+
display_progress(NULL, progress);
350361
}
351362
} else {
352363
display_progress(notes ? notes : "Keep connected.", 0);
@@ -368,10 +379,13 @@ void ui_screen_install_progress_erase(int pos, int len) {
368379
display_text_center(DISPLAY_RESX / 2, TITLE_OFFSET_Y, "Installing", -1,
369380
FONT_PJKS_BOLD_38, COLOR_BL_FG, COLOR_BL_BG);
370381

371-
display_progress("Keep connected.", 250 * pos / len);
382+
display_progress("Keep connected.", 25 * pos / len);
372383
}
373384

374-
void ui_screen_install_progress_upload(int pos) { display_progress(NULL, pos); }
385+
void ui_screen_install_progress_upload(int pos) {
386+
current_progress_value = pos;
387+
display_progress(NULL, pos);
388+
}
375389

376390
// wipe UI
377391

@@ -403,7 +417,7 @@ void ui_screen_wipe(void) {
403417
}
404418

405419
void ui_screen_wipe_progress(int pos, int len) {
406-
display_progress(NULL, 1000 * pos / len);
420+
display_progress(NULL, 100 * pos / len);
407421
}
408422

409423
void ui_screen_wipe_done(void) {
@@ -657,6 +671,20 @@ void ui_wipe_confirm(const image_header* const hdr) {
657671
ui_confirm_cancel_buttons("Cancel", "Wipe", COLOR_BL_DARK, COLOR_BL_FAIL);
658672
}
659673

674+
void ui_screen_install_title_clear(void) {
675+
display_bar(0, TITLE_OFFSET_Y - 30, DISPLAY_RESX, TITLE_OFFSET_Y + 90,
676+
COLOR_BLACK);
677+
}
678+
679+
void ui_show_version_info(int y, char* current_ver, char* new_ver) {
680+
display_text_right(3 * DISPLAY_RESX / 4 - 15, y, current_ver, -1, FONT_NORMAL,
681+
COLOR_WHITE, COLOR_BL_PANEL);
682+
display_text(3 * DISPLAY_RESX / 4 + 20, y, new_ver, -1, FONT_NORMAL,
683+
COLOR_WHITE, COLOR_BL_PANEL);
684+
display_image(350, y - 20, 25, 23, toi_icon_arrow_right + 12,
685+
sizeof(toi_icon_arrow_right) - 12);
686+
}
687+
660688
void ui_install_confirm(image_header* current_hdr,
661689
const image_header* const new_hdr) {
662690
if ((current_hdr == NULL) || (new_hdr == NULL)) return;
@@ -722,11 +750,89 @@ void ui_install_thd89_confirm(const char* old_ver, const char* boot_ver) {
722750
ui_confirm_cancel_buttons("Cancel", "Install", COLOR_BL_DARK, COLOR_BL_DONE);
723751
}
724752

753+
void ui_update_info_show(update_info_t update_info) {
754+
int offset_y = SUBTITLE_OFFSET_Y;
755+
int offset_x = 32;
756+
int bar_height = 66;
757+
int font_offset = 8;
758+
759+
ui_statusbar_update();
760+
ui_logo_onekey();
761+
762+
display_text_center(DISPLAY_RESX / 2, TITLE_OFFSET_Y, "Update Firmware", -1,
763+
FONT_PJKS_BOLD_38, COLOR_BL_FG, COLOR_BL_BG);
764+
765+
if (update_info.boot.type == UPDATE_BOOTLOADER) {
766+
display_bar_radius_ex(BUTTON_LEFT_OFFSET_X, offset_y - bar_height / 2,
767+
BUTTON_FULL_WIDTH, bar_height, COLOR_BL_PANEL,
768+
COLOR_BL_BG, bar_height / 2);
769+
770+
display_text(offset_x, offset_y + font_offset, "Bootloader", -1,
771+
FONT_NORMAL, COLOR_BL_FG, COLOR_BL_BG);
772+
773+
ui_show_version_info(offset_y + font_offset,
774+
update_info.boot.current_version,
775+
update_info.boot.new_version);
776+
offset_y += 80;
777+
}
778+
779+
if (update_info.mcu_location) {
780+
display_bar_radius_ex(BUTTON_LEFT_OFFSET_X, offset_y - bar_height / 2,
781+
BUTTON_FULL_WIDTH, bar_height, COLOR_BL_PANEL,
782+
COLOR_BL_BG, bar_height / 2);
783+
784+
display_text(offset_x, offset_y + font_offset, "System", -1, FONT_NORMAL,
785+
COLOR_BL_FG, COLOR_BL_BG);
786+
787+
ui_show_version_info(
788+
offset_y + font_offset,
789+
update_info.items[update_info.mcu_location - 1].current_version,
790+
update_info.items[update_info.mcu_location - 1].new_version);
791+
offset_y += 80;
792+
}
793+
794+
if (update_info.se_count) {
795+
display_bar_radius_ex(BUTTON_LEFT_OFFSET_X, offset_y - bar_height / 2,
796+
BUTTON_FULL_WIDTH, bar_height, COLOR_BL_PANEL,
797+
COLOR_BL_BG, bar_height / 2);
798+
799+
display_text(offset_x, offset_y + font_offset, "SE", -1, FONT_NORMAL,
800+
COLOR_BL_FG, COLOR_BL_BG);
801+
ui_show_version_info(
802+
offset_y + font_offset,
803+
update_info.items[update_info.se_location[0] - 1].current_version,
804+
update_info.items[update_info.se_location[0] - 1].new_version);
805+
offset_y += 80;
806+
}
807+
808+
if (update_info.ble_location) {
809+
display_bar_radius_ex(BUTTON_LEFT_OFFSET_X, offset_y - bar_height / 2,
810+
BUTTON_FULL_WIDTH, bar_height, COLOR_BL_PANEL,
811+
COLOR_BL_BG, bar_height / 2);
812+
813+
display_text(offset_x, offset_y + font_offset, "Bluetooth", -1, FONT_NORMAL,
814+
COLOR_BL_FG, COLOR_BL_BG);
815+
ui_show_version_info(
816+
offset_y + font_offset,
817+
update_info.items[update_info.ble_location - 1].current_version,
818+
update_info.items[update_info.ble_location - 1].new_version);
819+
offset_y += 80;
820+
}
821+
822+
ui_confirm_cancel_buttons("Cancel", "Install", COLOR_BL_DARK, COLOR_BL_DONE);
823+
}
824+
725825
void ui_bootloader_first(const image_header* const hdr) {
726826
ui_bootloader_page_current = 0;
727827
uint8_t se_state;
728828
char se_info[64] = {0};
729829

830+
static image_header* current_hdr = NULL;
831+
832+
if (current_hdr == NULL && hdr) {
833+
current_hdr = (image_header*)hdr;
834+
}
835+
730836
ui_statusbar_update();
731837
// info icon 48 * 48 - the entry point of the bootloader details
732838
display_icon(INFO_ICON_OFFSET_X, INFO_ICON_OFFSET_Y, INFO_ICON_SIZE,
@@ -747,8 +853,8 @@ void ui_bootloader_first(const image_header* const hdr) {
747853

748854
display_text_center(DISPLAY_RESX / 2, DISPLAY_RESY - 92, "SafeOS", -1,
749855
FONT_PJKS_BOLD_38, COLOR_BL_FG, COLOR_BL_BG);
750-
if (hdr) {
751-
const char* ver_str = format_ver("%d.%d.%d", (hdr->onekey_version));
856+
if (current_hdr) {
857+
const char* ver_str = format_ver("%d.%d.%d", (current_hdr->onekey_version));
752858
display_text_center(DISPLAY_RESX / 2, DISPLAY_RESY - 50, ver_str, -1,
753859
FONT_NORMAL, COLOR_BL_SUBTITLE, COLOR_BL_BG);
754860
}

core/embed/bootloader/bootui.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "secbool.h"
2525
#include "stdbool.h"
2626

27+
char* format_progress_value(char* prefix);
28+
2729
const char* format_ver(const char* format, uint32_t version);
2830
void ui_screen_boot(const vendor_header* const vhdr,
2931
const image_header* const hdr);
@@ -42,15 +44,14 @@ void ui_screen_firmware_fingerprint(const image_header* const hdr);
4244

4345
void ui_screen_install_confirm_upgrade(const vendor_header* const vhdr,
4446
const image_header* const hdr);
45-
void ui_screen_install_confirm_newvendor_or_downgrade_wipe(
46-
const vendor_header* const vhdr, const image_header* const hdr,
47-
secbool downgrade_wipe);
47+
void ui_screen_install_confirm_newvendor_or_downgrade_wipe(char* new_version);
4848
void ui_screen_install_start(void);
4949
void ui_screen_install_progress_erase(int pos, int len);
5050
void ui_screen_install_progress_upload(int pos);
5151

5252
void ui_screen_confirm(char* title, char* note_l1, char* note_l2, char* note_l3,
5353
char* note_l4);
54+
void ui_screen_progress_bar_init(char* title, char* notes, int progress);
5455
void ui_screen_progress_bar_prepare(char* title, char* notes);
5556
void ui_screen_progress_bar_update(char* msg_status, char* notes, int progress);
5657

@@ -86,10 +87,13 @@ void ui_bootloader_simple(void);
8687
void ui_bootloader_first(const image_header* const hdr);
8788
void ui_bootloader_view_details(const image_header* const hdr);
8889
void ui_wipe_confirm(const image_header* const hdr);
90+
void ui_show_version_info(int y, char* current_ver, char* new_ver);
91+
void ui_screen_install_title_clear(void);
8992
void ui_install_confirm(image_header* current_hdr,
9093
const image_header* const new_hdr);
9194
void ui_install_ble_confirm(void);
9295
void ui_install_thd89_confirm(const char* old_ver, const char* boot_ver);
96+
void ui_update_info_show(update_info_t update_info);
9397
void ui_install_progress(image_header* current_hdr,
9498
const image_header* const new_hdr);
9599
void ui_bootloader_page_switch(const image_header* const hdr);

core/embed/bootloader/main.c

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@
6666
#include "camera.h"
6767
#include "emmc_wrapper.h"
6868

69-
#define USB_IFACE_NUM 0
70-
7169
#if !PRODUCTION
7270

7371
// DO NOT USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING
@@ -309,6 +307,50 @@ static void charge_switch(void) {
309307
}
310308
}
311309

310+
void bootloader_usb_loop_tiny(uint32_t tick) {
311+
static uint32_t tick_start = 0;
312+
if (tick_start == 0) {
313+
tick_start = tick;
314+
}
315+
// 500ms
316+
if (tick - tick_start > 500) {
317+
tick_start = tick;
318+
uint8_t buf[USB_PACKET_SIZE];
319+
bool cmd_received = false;
320+
if (USB_PACKET_SIZE == spi_slave_poll(buf)) {
321+
host_channel = CHANNEL_SLAVE;
322+
cmd_received = true;
323+
} else if (USB_PACKET_SIZE ==
324+
usb_webusb_read(USB_IFACE_NUM, buf, USB_PACKET_SIZE)) {
325+
host_channel = CHANNEL_USB;
326+
cmd_received = true;
327+
}
328+
if (cmd_received) {
329+
if (buf[0] != '?' || buf[1] != '#' || buf[2] != '#') {
330+
return;
331+
}
332+
if (buf[3] == 0 && buf[4] == 0) {
333+
send_msg_features_simple(USB_IFACE_NUM);
334+
} else {
335+
send_failure(USB_IFACE_NUM, FailureType_Failure_ProcessError,
336+
format_progress_value("Update mode"));
337+
}
338+
}
339+
}
340+
}
341+
342+
void enable_usb_tiny_task(bool init_usb) {
343+
if (init_usb) {
344+
usb_init_all(secfalse);
345+
usb_start();
346+
}
347+
systick_enable_dispatch(SYSTICK_DISPATCH_USB_TINY, bootloader_usb_loop_tiny);
348+
}
349+
350+
void disable_usb_tiny_task(void) {
351+
systick_disable_dispatch(SYSTICK_DISPATCH_USB_TINY);
352+
}
353+
312354
static secbool bootloader_usb_loop(const vendor_header* const vhdr,
313355
const image_header* const hdr) {
314356
// if both are NULL, we don't have a firmware installed
@@ -595,6 +637,10 @@ static void check_bootloader_version(void) {
595637

596638
#endif
597639

640+
static bool enter_boot_forced(void) {
641+
return *BOOT_TARGET_FLAG_ADDR == BOOT_TARGET_BOOTLOADER;
642+
}
643+
598644
static BOOT_TARGET decide_boot_target(vendor_header* const vhdr,
599645
image_header* const hdr,
600646
secbool* vhdr_valid, secbool* hdr_valid,
@@ -753,6 +799,10 @@ int main(void) {
753799

754800
#endif
755801

802+
if (!enter_boot_forced()) {
803+
check_firmware_from_file(USB_IFACE_NULL);
804+
}
805+
756806
vendor_header vhdr;
757807
image_header hdr;
758808
secbool vhdr_valid = secfalse;
@@ -800,17 +850,20 @@ int main(void) {
800850

801851
if ((vhdr.vtrust & VTRUST_CLICK) == 0) {
802852
ui_screen_boot_click();
803-
while (touch_read() == 0)
804-
;
853+
int counter = 0;
854+
while (touch_read() == 0) {
855+
hal_delay(10);
856+
counter++;
857+
if (counter > 200) {
858+
break;
859+
}
860+
}
805861
}
806862
}
807863

808864
display_clear();
809865
bus_fault_disable();
810866

811-
__disable_irq();
812-
__disable_fault_irq();
813-
814867
// enable firmware region
815868
mpu_config_firmware(sectrue, sectrue);
816869

0 commit comments

Comments
 (0)