Sherry Zhang | 07b4241 | 2021-01-07 14:19:41 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2021, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | #include <string.h> |
| 8 | #include "bootutil/boot_record.h" |
| 9 | #include "bootutil/boot_status.h" |
| 10 | #include "bootutil/image.h" |
| 11 | #include "flash_map/flash_map.h" |
| 12 | #include "sysflash/sysflash.h" |
| 13 | |
| 14 | /* Firmware Update specific macros */ |
| 15 | #define TLV_MAJOR_FWU 0x2 |
| 16 | #define MODULE_MASK 0x3F /* 6 bit */ |
| 17 | #define CLAIM_MASK 0x3F /* 6 bit */ |
| 18 | |
| 19 | #define SET_FWU_MINOR(sw_module, claim) \ |
| 20 | ((uint16_t)((sw_module & MODULE_MASK) << 6) | \ |
| 21 | (uint16_t)(claim & CLAIM_MASK)) |
| 22 | |
| 23 | extern int |
| 24 | boot_add_data_to_shared_area(uint8_t major_type, |
| 25 | uint16_t minor_type, |
| 26 | size_t size, |
| 27 | const uint8_t *data); |
| 28 | |
| 29 | /** |
| 30 | * Add application specific data to the shared memory area between the |
| 31 | * bootloader and runtime SW. |
| 32 | * |
| 33 | * @param[in] hdr Pointer to the image header stored in RAM. |
| 34 | * @param[in] fap Pointer to the flash area where image is stored. |
| 35 | * |
| 36 | * @return 0 on success; nonzero on failure. |
| 37 | */ |
| 38 | int boot_save_shared_data(const struct image_header *hdr, |
| 39 | const struct flash_area *fap) |
| 40 | { |
| 41 | uint16_t fwu_minor; |
| 42 | struct image_version image_ver; |
| 43 | const struct flash_area *temp_fap; |
| 44 | uint8_t mcuboot_image_id = 0; |
| 45 | uint8_t i; |
| 46 | |
| 47 | if (hdr == NULL || fap == NULL) { |
| 48 | return -1; |
| 49 | } |
| 50 | |
| 51 | for (i = 0; i < MCUBOOT_IMAGE_NUMBER; i++) { |
| 52 | if (flash_area_open(FLASH_AREA_IMAGE_PRIMARY(i), |
| 53 | &temp_fap) != 0) { |
| 54 | return -1; |
| 55 | } |
| 56 | |
| 57 | if (fap == temp_fap) { |
| 58 | mcuboot_image_id = i; |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (i == MCUBOOT_IMAGE_NUMBER) { |
| 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | image_ver = hdr->ih_ver; |
| 68 | |
| 69 | /* TODO: add the module identifier into the fwu_minor after the module |
| 70 | * arg is supported. |
| 71 | */ |
| 72 | /* Currently hardcode it to 0 which indicates the full image. */ |
| 73 | fwu_minor = SET_FWU_MINOR(mcuboot_image_id, SW_VERSION); |
| 74 | return boot_add_data_to_shared_area(TLV_MAJOR_FWU, |
| 75 | fwu_minor, |
| 76 | sizeof(image_ver), |
| 77 | (const uint8_t *)&image_ver); |
| 78 | } |