1717 * under the License.
1818 */
1919
20+ /*
21+ * Modifications are Copyright (c) 2019 Arm Limited.
22+ */
23+
2024#include <assert.h>
2125#include <string.h>
2226#include <inttypes.h>
@@ -168,13 +172,12 @@ boot_magic_off(const struct flash_area *fap)
168172int
169173boot_status_entries (const struct flash_area * fap )
170174{
171- switch (fap -> fa_id ) {
172- case FLASH_AREA_IMAGE_PRIMARY :
173- case FLASH_AREA_IMAGE_SECONDARY :
174- return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES ;
175- case FLASH_AREA_IMAGE_SCRATCH :
175+ if (fap -> fa_id == FLASH_AREA_IMAGE_SCRATCH ) {
176176 return BOOT_STATUS_STATE_COUNT ;
177- default :
177+ } else if ((fap -> fa_id == FLASH_AREA_IMAGE_PRIMARY ) ||
178+ (fap -> fa_id == FLASH_AREA_IMAGE_SECONDARY )) {
179+ return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES ;
180+ } else {
178181 return BOOT_EBADARGS ;
179182 }
180183}
@@ -194,7 +197,7 @@ boot_status_off(const struct flash_area *fap)
194197}
195198
196199uint32_t
197- boot_swap_type_off (const struct flash_area * fap )
200+ boot_swap_info_off (const struct flash_area * fap )
198201{
199202 return fap -> fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3 ;
200203}
@@ -232,6 +235,7 @@ boot_read_swap_state(const struct flash_area *fap,
232235{
233236 uint32_t magic [BOOT_MAGIC_ARR_SZ ];
234237 uint32_t off ;
238+ uint8_t swap_info ;
235239 int rc ;
236240
237241 off = boot_magic_off (fap );
@@ -245,14 +249,19 @@ boot_read_swap_state(const struct flash_area *fap,
245249 state -> magic = boot_magic_decode (magic );
246250 }
247251
248- off = boot_swap_type_off (fap );
249- rc = flash_area_read_is_empty (fap , off , & state -> swap_type ,
250- sizeof state -> swap_type );
252+ off = boot_swap_info_off (fap );
253+ rc = flash_area_read_is_empty (fap , off , & swap_info , sizeof swap_info );
251254 if (rc < 0 ) {
252255 return BOOT_EFLASH ;
253256 }
257+
258+ /* Extract the swap type and image number */
259+ state -> swap_type = BOOT_GET_SWAP_TYPE (swap_info );
260+ state -> image_num = BOOT_GET_IMAGE_NUM (swap_info );
261+
254262 if (rc == 1 || state -> swap_type > BOOT_SWAP_TYPE_REVERT ) {
255263 state -> swap_type = BOOT_SWAP_TYPE_NONE ;
264+ state -> image_num = 0 ;
256265 }
257266
258267 off = boot_copy_done_off (fap );
@@ -291,16 +300,14 @@ boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
291300 const struct flash_area * fap ;
292301 int rc ;
293302
294- switch (flash_area_id ) {
295- case FLASH_AREA_IMAGE_SCRATCH :
296- case FLASH_AREA_IMAGE_PRIMARY :
297- case FLASH_AREA_IMAGE_SECONDARY :
303+ if (flash_area_id == FLASH_AREA_IMAGE_SCRATCH ||
304+ flash_area_id == FLASH_AREA_IMAGE_PRIMARY ||
305+ flash_area_id == FLASH_AREA_IMAGE_SECONDARY ) {
298306 rc = flash_area_open (flash_area_id , & fap );
299307 if (rc != 0 ) {
300308 return BOOT_EFLASH ;
301309 }
302- break ;
303- default :
310+ } else {
304311 return BOOT_EBADARGS ;
305312 }
306313
@@ -494,14 +501,18 @@ boot_write_image_ok(const struct flash_area *fap)
494501 * resume in case of an unexpected reset.
495502 */
496503int
497- boot_write_swap_type (const struct flash_area * fap , uint8_t swap_type )
504+ boot_write_swap_info (const struct flash_area * fap , uint8_t swap_type ,
505+ uint8_t image_num )
498506{
499507 uint32_t off ;
500-
501- off = boot_swap_type_off (fap );
502- BOOT_LOG_DBG ("writing swap_type; fa_id=%d off=0x%x (0x%x), swap_type=0x%x" ,
503- fap -> fa_id , off , fap -> fa_off + off , swap_type );
504- return boot_write_trailer_byte (fap , off , swap_type );
508+ uint8_t swap_info ;
509+
510+ BOOT_SET_SWAP_INFO (swap_info , image_num , swap_type );
511+ off = boot_swap_info_off (fap );
512+ BOOT_LOG_DBG ("writing swap_info; fa_id=%d off=0x%x (0x%x), swap_type=0x%x"
513+ " image_num=0x%x" ,
514+ fap -> fa_id , off , fap -> fa_off + off , swap_type , image_num );
515+ return boot_write_trailer_byte (fap , off , swap_info );
505516}
506517
507518int
@@ -648,7 +659,7 @@ boot_set_pending(int permanent)
648659 } else {
649660 swap_type = BOOT_SWAP_TYPE_TEST ;
650661 }
651- rc = boot_write_swap_type (fap , swap_type );
662+ rc = boot_write_swap_info (fap , swap_type , 0 );
652663 }
653664
654665 flash_area_close (fap );
@@ -730,3 +741,38 @@ boot_set_confirmed(void)
730741 flash_area_close (fap );
731742 return rc ;
732743}
744+
745+ #if (BOOT_IMAGE_NUMBER > 1 )
746+ /**
747+ * Check if the version of the image is not older than required.
748+ *
749+ * @param req Required minimal image version.
750+ * @param ver Version of the image to be checked.
751+ *
752+ * @return 0 if the version is sufficient, nonzero otherwise.
753+ */
754+ int
755+ boot_is_version_sufficient (struct image_version * req ,
756+ struct image_version * ver )
757+ {
758+ if (ver -> iv_major > req -> iv_major ) {
759+ return 0 ;
760+ }
761+ if (ver -> iv_major < req -> iv_major ) {
762+ return BOOT_EBADVERSION ;
763+ }
764+ /* The major version numbers are equal. */
765+ if (ver -> iv_minor > req -> iv_minor ) {
766+ return 0 ;
767+ }
768+ if (ver -> iv_minor < req -> iv_minor ) {
769+ return BOOT_EBADVERSION ;
770+ }
771+ /* The minor version numbers are equal. */
772+ if (ver -> iv_revision < req -> iv_revision ) {
773+ return BOOT_EBADVERSION ;
774+ }
775+
776+ return 0 ;
777+ }
778+ #endif /* BOOT_IMAGE_NUMBER > 1 */
0 commit comments