fwu.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. static struct fwu_metadata metadata;
  23. static bool is_metadata_initialized __unused;
  24. /*******************************************************************************
  25. * Compute CRC32 of the FWU metadata, and check it against the CRC32 value
  26. * present in the FWU metadata.
  27. *
  28. * return -1 on error, otherwise 0
  29. ******************************************************************************/
  30. static int fwu_metadata_crc_check(void)
  31. {
  32. unsigned char *data = (unsigned char *)&metadata;
  33. uint32_t calc_crc = tf_crc32(0U, data + sizeof(metadata.crc_32),
  34. (sizeof(metadata) -
  35. sizeof(metadata.crc_32)));
  36. if (metadata.crc_32 != calc_crc) {
  37. return -1;
  38. }
  39. return 0;
  40. }
  41. /*******************************************************************************
  42. * Check the sanity of FWU metadata.
  43. *
  44. * return -1 on error, otherwise 0
  45. ******************************************************************************/
  46. static int fwu_metadata_sanity_check(void)
  47. {
  48. /* ToDo: add more conditions for sanity check */
  49. if ((metadata.active_index >= NR_OF_FW_BANKS) ||
  50. (metadata.previous_active_index >= NR_OF_FW_BANKS)) {
  51. return -1;
  52. }
  53. return 0;
  54. }
  55. /*******************************************************************************
  56. * Verify and load specified FWU metadata image to local FWU metadata structure.
  57. *
  58. * @image_id: FWU metadata image id (either FWU_METADATA_IMAGE_ID or
  59. * BKUP_FWU_METADATA_IMAGE_ID)
  60. *
  61. * return a negative value on error, otherwise 0
  62. ******************************************************************************/
  63. static int fwu_metadata_load(unsigned int image_id)
  64. {
  65. int result;
  66. uintptr_t dev_handle, image_handle, image_spec;
  67. size_t bytes_read;
  68. assert((image_id == FWU_METADATA_IMAGE_ID) ||
  69. (image_id == BKUP_FWU_METADATA_IMAGE_ID));
  70. result = plat_fwu_set_metadata_image_source(image_id,
  71. &dev_handle,
  72. &image_spec);
  73. if (result != 0) {
  74. WARN("Failed to set reference to image id=%u (%i)\n",
  75. image_id, result);
  76. return result;
  77. }
  78. result = io_open(dev_handle, image_spec, &image_handle);
  79. if (result != 0) {
  80. WARN("Failed to load image id id=%u (%i)\n",
  81. image_id, result);
  82. return result;
  83. }
  84. result = io_read(image_handle, (uintptr_t)&metadata,
  85. sizeof(struct fwu_metadata), &bytes_read);
  86. if (result != 0) {
  87. WARN("Failed to read image id=%u (%i)\n", image_id, result);
  88. goto exit;
  89. }
  90. if (sizeof(struct fwu_metadata) != bytes_read) {
  91. /* return -1 in case of partial/no read */
  92. result = -1;
  93. WARN("Read bytes (%zu) instead of expected (%zu) bytes\n",
  94. bytes_read, sizeof(struct fwu_metadata));
  95. goto exit;
  96. }
  97. /* sanity check on loaded parameters */
  98. result = fwu_metadata_sanity_check();
  99. if (result != 0) {
  100. WARN("Sanity %s\n", "check failed on FWU metadata");
  101. goto exit;
  102. }
  103. /* CRC check on loaded parameters */
  104. result = fwu_metadata_crc_check();
  105. if (result != 0) {
  106. WARN("CRC %s\n", "check failed on FWU metadata");
  107. }
  108. exit:
  109. (void)io_close(image_handle);
  110. return result;
  111. }
  112. /*******************************************************************************
  113. * The system runs in the trial run state if any of the images in the active
  114. * firmware bank has not been accepted yet.
  115. *
  116. * Returns true if the system is running in the trial state.
  117. ******************************************************************************/
  118. bool fwu_is_trial_run_state(void)
  119. {
  120. bool trial_run = false;
  121. assert(is_metadata_initialized);
  122. for (unsigned int i = 0U; i < NR_OF_IMAGES_IN_FW_BANK; i++) {
  123. struct fwu_image_entry *entry = &metadata.img_entry[i];
  124. struct fwu_image_properties *img_props =
  125. &entry->img_props[metadata.active_index];
  126. if (img_props->accepted == 0) {
  127. trial_run = true;
  128. break;
  129. }
  130. }
  131. return trial_run;
  132. }
  133. const struct fwu_metadata *fwu_get_metadata(void)
  134. {
  135. assert(is_metadata_initialized);
  136. return &metadata;
  137. }
  138. /*******************************************************************************
  139. * Load verified copy of FWU metadata image kept in the platform NV storage
  140. * into local FWU metadata structure.
  141. * Also, update platform I/O policies with the offset address and length of
  142. * firmware-updated images kept in the platform NV storage.
  143. ******************************************************************************/
  144. void fwu_init(void)
  145. {
  146. /* Load FWU metadata which will be used to load the images in the
  147. * active bank as per PSA FWU specification
  148. */
  149. int result = fwu_metadata_load(FWU_METADATA_IMAGE_ID);
  150. if (result != 0) {
  151. WARN("loading of FWU-Metadata failed, "
  152. "using Bkup-FWU-Metadata\n");
  153. result = fwu_metadata_load(BKUP_FWU_METADATA_IMAGE_ID);
  154. if (result != 0) {
  155. ERROR("loading of Bkup-FWU-Metadata failed\n");
  156. panic();
  157. }
  158. }
  159. is_metadata_initialized = true;
  160. plat_fwu_set_images_source(&metadata);
  161. }