fwu.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <common/debug.h>
  8. #include <common/tf_crc32.h>
  9. #include <common/tbbr/tbbr_img_def.h>
  10. #include <drivers/fwu/fwu.h>
  11. #include <drivers/fwu/fwu_metadata.h>
  12. #include <drivers/io/io_storage.h>
  13. #include <plat/common/platform.h>
  14. /*
  15. * Assert that crc_32 is the first member of fwu_metadata structure.
  16. * It avoids accessing data outside of the metadata structure during
  17. * CRC32 computation if the crc_32 field gets moved due the structure
  18. * member(s) addition in the future.
  19. */
  20. CASSERT((offsetof(struct fwu_metadata, crc_32) == 0),
  21. crc_32_must_be_first_member_of_structure);
  22. /*
  23. * Ensure that the NR_OF_FW_BANKS selected by the platform is not
  24. * zero and not greater than the maximum number of banks allowed
  25. * by the specification.
  26. */
  27. CASSERT((NR_OF_FW_BANKS > 0) && (NR_OF_FW_BANKS <= NR_OF_MAX_FW_BANKS),
  28. assert_fwu_num_banks_invalid_value);
  29. #define FWU_METADATA_VERSION 2U
  30. #define FWU_FW_STORE_DESC_OFFSET 0x20U
  31. static struct fwu_metadata metadata;
  32. static bool is_metadata_initialized __unused;
  33. /*******************************************************************************
  34. * Compute CRC32 of the FWU metadata, and check it against the CRC32 value
  35. * present in the FWU metadata.
  36. *
  37. * return -1 on error, otherwise 0
  38. ******************************************************************************/
  39. static int fwu_metadata_crc_check(void)
  40. {
  41. unsigned char *data = (unsigned char *)&metadata;
  42. uint32_t calc_crc = tf_crc32(0U, data + sizeof(metadata.crc_32),
  43. (sizeof(metadata) -
  44. sizeof(metadata.crc_32)));
  45. if (metadata.crc_32 != calc_crc) {
  46. return -1;
  47. }
  48. return 0;
  49. }
  50. /*******************************************************************************
  51. * Check the sanity of FWU metadata.
  52. *
  53. * return -EINVAL on error, otherwise 0
  54. ******************************************************************************/
  55. static int fwu_metadata_sanity_check(void)
  56. {
  57. if (metadata.version != FWU_METADATA_VERSION) {
  58. WARN("Incorrect FWU Metadata version of %u\n",
  59. metadata.version);
  60. return -EINVAL;
  61. }
  62. if (metadata.active_index >= NR_OF_FW_BANKS) {
  63. WARN("Active Index value(%u) greater than the configured value(%d)",
  64. metadata.active_index, NR_OF_FW_BANKS);
  65. return -EINVAL;
  66. }
  67. if (metadata.previous_active_index >= NR_OF_FW_BANKS) {
  68. WARN("Previous Active Index value(%u) greater than the configured value(%d)",
  69. metadata.previous_active_index, NR_OF_FW_BANKS);
  70. return -EINVAL;
  71. }
  72. #if PSA_FWU_METADATA_FW_STORE_DESC
  73. if (metadata.fw_desc.num_banks != NR_OF_FW_BANKS) {
  74. WARN("Number of Banks(%u) in FWU Metadata different from the configured value(%d)",
  75. metadata.fw_desc.num_banks, NR_OF_FW_BANKS);
  76. return -EINVAL;
  77. }
  78. if (metadata.fw_desc.num_images != NR_OF_IMAGES_IN_FW_BANK) {
  79. WARN("Number of Images(%u) in FWU Metadata different from the configured value(%d)",
  80. metadata.fw_desc.num_images, NR_OF_IMAGES_IN_FW_BANK);
  81. return -EINVAL;
  82. }
  83. if (metadata.desc_offset != FWU_FW_STORE_DESC_OFFSET) {
  84. WARN("Descriptor Offset(0x%x) in the FWU Metadata not equal to 0x20\n",
  85. metadata.desc_offset);
  86. return -EINVAL;
  87. }
  88. #else
  89. if (metadata.desc_offset != 0U) {
  90. WARN("Descriptor offset has non zero value of 0x%x\n",
  91. metadata.desc_offset);
  92. return -EINVAL;
  93. }
  94. #endif
  95. return 0;
  96. }
  97. /*******************************************************************************
  98. * Verify and load specified FWU metadata image to local FWU metadata structure.
  99. *
  100. * @image_id: FWU metadata image id (either FWU_METADATA_IMAGE_ID or
  101. * BKUP_FWU_METADATA_IMAGE_ID)
  102. *
  103. * return a negative value on error, otherwise 0
  104. ******************************************************************************/
  105. static int fwu_metadata_load(unsigned int image_id)
  106. {
  107. int result;
  108. uintptr_t dev_handle, image_handle, image_spec;
  109. size_t bytes_read;
  110. assert((image_id == FWU_METADATA_IMAGE_ID) ||
  111. (image_id == BKUP_FWU_METADATA_IMAGE_ID));
  112. result = plat_fwu_set_metadata_image_source(image_id,
  113. &dev_handle,
  114. &image_spec);
  115. if (result != 0) {
  116. WARN("Failed to set reference to image id=%u (%i)\n",
  117. image_id, result);
  118. return result;
  119. }
  120. result = io_open(dev_handle, image_spec, &image_handle);
  121. if (result != 0) {
  122. WARN("Failed to load image id id=%u (%i)\n",
  123. image_id, result);
  124. return result;
  125. }
  126. result = io_read(image_handle, (uintptr_t)&metadata,
  127. sizeof(struct fwu_metadata), &bytes_read);
  128. if (result != 0) {
  129. WARN("Failed to read image id=%u (%i)\n", image_id, result);
  130. goto exit;
  131. }
  132. if (sizeof(struct fwu_metadata) != bytes_read) {
  133. /* return -1 in case of partial/no read */
  134. result = -1;
  135. WARN("Read bytes (%zu) instead of expected (%zu) bytes\n",
  136. bytes_read, sizeof(struct fwu_metadata));
  137. goto exit;
  138. }
  139. /* sanity check on loaded parameters */
  140. result = fwu_metadata_sanity_check();
  141. if (result != 0) {
  142. WARN("Sanity %s\n", "check failed on FWU metadata");
  143. goto exit;
  144. }
  145. /* CRC check on loaded parameters */
  146. result = fwu_metadata_crc_check();
  147. if (result != 0) {
  148. WARN("CRC %s\n", "check failed on FWU metadata");
  149. }
  150. exit:
  151. (void)io_close(image_handle);
  152. return result;
  153. }
  154. /*******************************************************************************
  155. * Check for an alternate bank for the platform to boot from. This function will
  156. * mostly be called whenever the count of the number of times a platform boots
  157. * in the Trial State exceeds a pre-set limit.
  158. * The function first checks if the platform can boot from the previously active
  159. * bank. If not, it tries to find another bank in the accepted state.
  160. * And finally, if both the checks fail, as a last resort, it tries to find
  161. * a valid bank.
  162. *
  163. * Returns the index of a bank to boot, else returns invalid index
  164. * INVALID_BOOT_IDX.
  165. ******************************************************************************/
  166. uint32_t fwu_get_alternate_boot_bank(void)
  167. {
  168. uint32_t i;
  169. /* First check if the previously active bank can be used */
  170. if (metadata.bank_state[metadata.previous_active_index] ==
  171. FWU_BANK_STATE_ACCEPTED) {
  172. return metadata.previous_active_index;
  173. }
  174. /* Now check for any other bank in the accepted state */
  175. for (i = 0U; i < NR_OF_FW_BANKS; i++) {
  176. if (i == metadata.active_index ||
  177. i == metadata.previous_active_index) {
  178. continue;
  179. }
  180. if (metadata.bank_state[i] == FWU_BANK_STATE_ACCEPTED) {
  181. return i;
  182. }
  183. }
  184. /*
  185. * No accepted bank found. Now try booting from a valid bank.
  186. * Give priority to the previous active bank.
  187. */
  188. if (metadata.bank_state[metadata.previous_active_index] ==
  189. FWU_BANK_STATE_VALID) {
  190. return metadata.previous_active_index;
  191. }
  192. for (i = 0U; i < NR_OF_FW_BANKS; i++) {
  193. if (i == metadata.active_index ||
  194. i == metadata.previous_active_index) {
  195. continue;
  196. }
  197. if (metadata.bank_state[i] == FWU_BANK_STATE_VALID) {
  198. return i;
  199. }
  200. }
  201. return INVALID_BOOT_IDX;
  202. }
  203. /*******************************************************************************
  204. * The platform can be in one of Valid, Invalid or Accepted states.
  205. *
  206. * Invalid - One or more images in the bank are corrupted, or partially
  207. * overwritten. The bank is not to be used for booting.
  208. *
  209. * Valid - All images of the bank are valid but at least one image has not
  210. * been accepted. This implies that the platform is in Trial State.
  211. *
  212. * Accepted - All images of the bank are valid and accepted.
  213. *
  214. * Returns the state of the current active bank
  215. ******************************************************************************/
  216. uint32_t fwu_get_active_bank_state(void)
  217. {
  218. assert(is_metadata_initialized);
  219. return metadata.bank_state[metadata.active_index];
  220. }
  221. const struct fwu_metadata *fwu_get_metadata(void)
  222. {
  223. assert(is_metadata_initialized);
  224. return &metadata;
  225. }
  226. /*******************************************************************************
  227. * Load verified copy of FWU metadata image kept in the platform NV storage
  228. * into local FWU metadata structure.
  229. * Also, update platform I/O policies with the offset address and length of
  230. * firmware-updated images kept in the platform NV storage.
  231. ******************************************************************************/
  232. void fwu_init(void)
  233. {
  234. /* Load FWU metadata which will be used to load the images in the
  235. * active bank as per PSA FWU specification
  236. */
  237. int result = fwu_metadata_load(FWU_METADATA_IMAGE_ID);
  238. if (result != 0) {
  239. WARN("loading of FWU-Metadata failed, "
  240. "using Bkup-FWU-Metadata\n");
  241. result = fwu_metadata_load(BKUP_FWU_METADATA_IMAGE_ID);
  242. if (result != 0) {
  243. ERROR("loading of Bkup-FWU-Metadata failed\n");
  244. panic();
  245. }
  246. }
  247. is_metadata_initialized = true;
  248. plat_fwu_set_images_source(&metadata);
  249. }