11#include <main.h>
22
3+ #include <libopencm3/cm3/scb.h>
34#include <services/chainloader/chainloader.h>
45#include <interfaces/flash.h>
56
@@ -19,11 +20,14 @@ static const char *bl_states[] = {
1920 "check-signature" ,
2021 "find-update" ,
2122 "validate-update" ,
23+ "flash-update" ,
24+ "disable-update" ,
25+ "reset" ,
2226};
2327
2428
2529static void bl_set_state (App * self , enum bl_state state ) {
26- u_log (system_log , LOG_TYPE_DEBUG , U_LOG_MODULE_PREFIX ("state '%s' -> '%s'" ), bl_states [self -> state ], bl_states [state ]);
30+ u_log (system_log , LOG_TYPE_INFO , U_LOG_MODULE_PREFIX ("\x1b[1mstate '%s' -> '%s'" ), bl_states [self -> state ], bl_states [state ]);
2731 self -> state = state ;
2832}
2933
@@ -78,8 +82,6 @@ static app_ret_t bl_step(App *self) {
7882 }
7983
8084 case BL_STATE_FIND_UPDATE : {
81- u_log (system_log , LOG_TYPE_INFO , U_LOG_MODULE_PREFIX ("Trying to initialize flash-updater" ));
82-
8385 Flash * target = NULL ;
8486 if (iservicelocator_query_name_type (locator , "app" , ISERVICELOCATOR_TYPE_FLASH , (Interface * * )& target ) != ISERVICELOCATOR_RET_OK ) {
8587 u_log (system_log , LOG_TYPE_INFO , U_LOG_MODULE_PREFIX ("no update target found, skipping update check" ));
@@ -92,26 +94,72 @@ static app_ret_t bl_step(App *self) {
9294 bl_set_state (self , BL_STATE_FIND_APP );
9395 }
9496
95- if (flash_updater_init (& self -> updater , target ) == FLASH_UPDATER_RET_OK &&
96- flash_updater_set_source_flash (& self -> updater , update ) == FLASH_UPDATER_RET_OK ) {
97- bl_set_state (self , BL_STATE_VALIDATE_UPDATE );
97+ if (flash_updater_init (& self -> updater , target ) != FLASH_UPDATER_RET_OK ||
98+ flash_updater_set_source_flash (& self -> updater , update ) != FLASH_UPDATER_RET_OK ) {
99+ bl_set_state (self , BL_STATE_FIND_APP );
100+
101+ }
98102
103+ Stream * console = NULL ;
104+ if (iservicelocator_query_name_type (locator , "console" , ISERVICELOCATOR_TYPE_STREAM , (Interface * * )& console ) == ISERVICELOCATOR_RET_OK ) {
105+ /* Set only if found. */
106+ if (flash_updater_set_console (& self -> updater , console ) != FLASH_UPDATER_RET_OK ) {
107+ bl_set_state (self , BL_STATE_FIND_APP );
108+ }
99109 }
110+
111+ bl_set_state (self , BL_STATE_VALIDATE_UPDATE );
100112 break ;
101113 }
102114
103115 case BL_STATE_VALIDATE_UPDATE : {
104- u_log (system_log , LOG_TYPE_INFO , U_LOG_MODULE_PREFIX ("validating update sources" ));
105- if (flash_updater_validate_source (& self -> updater ) == FLASH_UPDATER_RET_OK ) {
116+ const char pubkey_b64 [] = CONFIG_BL_PUBKEY ;
117+ size_t keylen = 32 ;
118+ uint8_t pubkey [32 ] = {0 };
119+ base64decode (pubkey_b64 , strlen (pubkey_b64 ), pubkey , & keylen );
120+ if (keylen != 32 ) {
121+ u_log (system_log , LOG_TYPE_ERROR , U_LOG_MODULE_PREFIX ("wrong pubkey size %d" ), keylen );
122+ bl_set_state (self , BL_STATE_ALL_FAILED );
123+ break ;
124+ }
125+
126+ if (flash_updater_validate_source (& self -> updater ) == FLASH_UPDATER_RET_OK &&
127+ flash_updater_find_signature (& self -> updater ) == FLASH_UPDATER_RET_OK &&
128+ flash_updater_check_signature (& self -> updater , pubkey ) == FLASH_UPDATER_RET_OK ) {
106129 /* Continue with the update process. */
107- bl_set_state (self , BL_STATE_FIND_APP );
130+ bl_set_state (self , BL_STATE_FLASH_UPDATE );
108131 break ;
109132 }
110- u_log (system_log , LOG_TYPE_INFO , U_LOG_MODULE_PREFIX ("cannot find update image, continuing boot" ));
133+ u_log (system_log , LOG_TYPE_INFO , U_LOG_MODULE_PREFIX ("cannot validate update image, continuing boot" ));
111134 bl_set_state (self , BL_STATE_FIND_APP );
112135 break ;
113136 }
114137
138+ case BL_STATE_FLASH_UPDATE : {
139+ /* If something failed, we cannot do more. */
140+ flash_updater_write (& self -> updater );
141+ bl_set_state (self , BL_STATE_DISABLE_UPDATE );
142+
143+ break ;
144+ }
145+
146+ case BL_STATE_DISABLE_UPDATE : {
147+ flash_updater_disable_update (& self -> updater );
148+ bl_set_state (self , BL_STATE_FIND_APP );
149+
150+ break ;
151+ }
152+
153+
154+ case BL_STATE_RESET : {
155+ SCB_AIRCR = (SCB_AIRCR_VECTKEY | SCB_AIRCR_SYSRESETREQ );
156+ while (true) {
157+ ;
158+ }
159+
160+ break ;
161+ }
162+
115163 case BL_STATE_INIT :
116164 default :
117165 bl_set_state (self , BL_STATE_FIND_UPDATE );
0 commit comments