bl1_fwu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <platform_def.h>
  10. #include <arch_helpers.h>
  11. #include <bl1/bl1.h>
  12. #include <common/bl_common.h>
  13. #include <common/debug.h>
  14. #include <context.h>
  15. #include <drivers/auth/auth_mod.h>
  16. #include <lib/el3_runtime/context_mgmt.h>
  17. #include <lib/utils.h>
  18. #include <plat/common/platform.h>
  19. #include <smccc_helpers.h>
  20. #include "bl1_private.h"
  21. /*
  22. * Function declarations.
  23. */
  24. static int bl1_fwu_image_copy(unsigned int image_id,
  25. uintptr_t image_src,
  26. unsigned int block_size,
  27. unsigned int image_size,
  28. unsigned int flags);
  29. static int bl1_fwu_image_auth(unsigned int image_id,
  30. uintptr_t image_src,
  31. unsigned int image_size,
  32. unsigned int flags);
  33. static int bl1_fwu_image_execute(unsigned int image_id,
  34. void **handle,
  35. unsigned int flags);
  36. static register_t bl1_fwu_image_resume(register_t image_param,
  37. void **handle,
  38. unsigned int flags);
  39. static int bl1_fwu_sec_image_done(void **handle,
  40. unsigned int flags);
  41. static int bl1_fwu_image_reset(unsigned int image_id,
  42. unsigned int flags);
  43. __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved);
  44. /*
  45. * This keeps track of last executed secure image id.
  46. */
  47. static unsigned int sec_exec_image_id = INVALID_IMAGE_ID;
  48. /*******************************************************************************
  49. * Top level handler for servicing FWU SMCs.
  50. ******************************************************************************/
  51. u_register_t bl1_fwu_smc_handler(unsigned int smc_fid,
  52. u_register_t x1,
  53. u_register_t x2,
  54. u_register_t x3,
  55. u_register_t x4,
  56. void *cookie,
  57. void *handle,
  58. unsigned int flags)
  59. {
  60. switch (smc_fid) {
  61. case FWU_SMC_IMAGE_COPY:
  62. SMC_RET1(handle, bl1_fwu_image_copy((uint32_t)x1, x2,
  63. (uint32_t)x3, (uint32_t)x4, flags));
  64. case FWU_SMC_IMAGE_AUTH:
  65. SMC_RET1(handle, bl1_fwu_image_auth((uint32_t)x1, x2,
  66. (uint32_t)x3, flags));
  67. case FWU_SMC_IMAGE_EXECUTE:
  68. SMC_RET1(handle, bl1_fwu_image_execute((uint32_t)x1, &handle,
  69. flags));
  70. case FWU_SMC_IMAGE_RESUME:
  71. SMC_RET1(handle, bl1_fwu_image_resume((register_t)x1, &handle,
  72. flags));
  73. case FWU_SMC_SEC_IMAGE_DONE:
  74. SMC_RET1(handle, bl1_fwu_sec_image_done(&handle, flags));
  75. case FWU_SMC_IMAGE_RESET:
  76. SMC_RET1(handle, bl1_fwu_image_reset((uint32_t)x1, flags));
  77. case FWU_SMC_UPDATE_DONE:
  78. bl1_fwu_done((void *)x1, NULL);
  79. default:
  80. assert(false); /* Unreachable */
  81. break;
  82. }
  83. SMC_RET1(handle, SMC_UNK);
  84. }
  85. /*******************************************************************************
  86. * Utility functions to keep track of the images that are loaded at any time.
  87. ******************************************************************************/
  88. #ifdef PLAT_FWU_MAX_SIMULTANEOUS_IMAGES
  89. #define FWU_MAX_SIMULTANEOUS_IMAGES PLAT_FWU_MAX_SIMULTANEOUS_IMAGES
  90. #else
  91. #define FWU_MAX_SIMULTANEOUS_IMAGES 10
  92. #endif
  93. static unsigned int bl1_fwu_loaded_ids[FWU_MAX_SIMULTANEOUS_IMAGES] = {
  94. [0 ... FWU_MAX_SIMULTANEOUS_IMAGES-1] = INVALID_IMAGE_ID
  95. };
  96. /*
  97. * Adds an image_id to the bl1_fwu_loaded_ids array.
  98. * Returns 0 on success, 1 on error.
  99. */
  100. static int bl1_fwu_add_loaded_id(unsigned int image_id)
  101. {
  102. int i;
  103. /* Check if the ID is already in the list */
  104. for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
  105. if (bl1_fwu_loaded_ids[i] == image_id)
  106. return 0;
  107. }
  108. /* Find an empty slot */
  109. for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
  110. if (bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) {
  111. bl1_fwu_loaded_ids[i] = image_id;
  112. return 0;
  113. }
  114. }
  115. return 1;
  116. }
  117. /*
  118. * Removes an image_id from the bl1_fwu_loaded_ids array.
  119. * Returns 0 on success, 1 on error.
  120. */
  121. static int bl1_fwu_remove_loaded_id(unsigned int image_id)
  122. {
  123. int i;
  124. /* Find the ID */
  125. for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
  126. if (bl1_fwu_loaded_ids[i] == image_id) {
  127. bl1_fwu_loaded_ids[i] = INVALID_IMAGE_ID;
  128. return 0;
  129. }
  130. }
  131. return 1;
  132. }
  133. /*******************************************************************************
  134. * This function checks if the specified image overlaps another image already
  135. * loaded. It returns 0 if there is no overlap, a negative error code otherwise.
  136. ******************************************************************************/
  137. static int bl1_fwu_image_check_overlaps(unsigned int image_id)
  138. {
  139. const image_desc_t *desc, *checked_desc;
  140. const image_info_t *info, *checked_info;
  141. uintptr_t image_base, image_end;
  142. uintptr_t checked_image_base, checked_image_end;
  143. checked_desc = bl1_plat_get_image_desc(image_id);
  144. checked_info = &checked_desc->image_info;
  145. /* Image being checked mustn't be empty. */
  146. assert(checked_info->image_size != 0);
  147. checked_image_base = checked_info->image_base;
  148. checked_image_end = checked_image_base + checked_info->image_size - 1;
  149. /* No need to check for overflows, it's done in bl1_fwu_image_copy(). */
  150. for (int i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
  151. /* Skip INVALID_IMAGE_IDs and don't check image against itself */
  152. if ((bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) ||
  153. (bl1_fwu_loaded_ids[i] == image_id))
  154. continue;
  155. desc = bl1_plat_get_image_desc(bl1_fwu_loaded_ids[i]);
  156. /* Only check images that are loaded or being loaded. */
  157. assert ((desc != NULL) && (desc->state != IMAGE_STATE_RESET));
  158. info = &desc->image_info;
  159. /* There cannot be overlaps with an empty image. */
  160. if (info->image_size == 0)
  161. continue;
  162. image_base = info->image_base;
  163. image_end = image_base + info->image_size - 1;
  164. /*
  165. * Overflows cannot happen. It is checked in
  166. * bl1_fwu_image_copy() when the image goes from RESET to
  167. * COPYING or COPIED.
  168. */
  169. assert (image_end > image_base);
  170. /* Check if there are overlaps. */
  171. if (!((image_end < checked_image_base) ||
  172. (checked_image_end < image_base))) {
  173. VERBOSE("Image with ID %d overlaps existing image with ID %d",
  174. checked_desc->image_id, desc->image_id);
  175. return -EPERM;
  176. }
  177. }
  178. return 0;
  179. }
  180. /*******************************************************************************
  181. * This function is responsible for copying secure images in AP Secure RAM.
  182. ******************************************************************************/
  183. static int bl1_fwu_image_copy(unsigned int image_id,
  184. uintptr_t image_src,
  185. unsigned int block_size,
  186. unsigned int image_size,
  187. unsigned int flags)
  188. {
  189. uintptr_t dest_addr;
  190. unsigned int remaining;
  191. image_desc_t *desc;
  192. /* Get the image descriptor. */
  193. desc = bl1_plat_get_image_desc(image_id);
  194. if (desc == NULL) {
  195. WARN("BL1-FWU: Invalid image ID %u\n", image_id);
  196. return -EPERM;
  197. }
  198. /*
  199. * The request must originate from a non-secure caller and target a
  200. * secure image. Any other scenario is invalid.
  201. */
  202. if (GET_SECURITY_STATE(flags) == SECURE) {
  203. WARN("BL1-FWU: Copy not allowed from secure world.\n");
  204. return -EPERM;
  205. }
  206. if (GET_SECURITY_STATE(desc->ep_info.h.attr) == NON_SECURE) {
  207. WARN("BL1-FWU: Copy not allowed for non-secure images.\n");
  208. return -EPERM;
  209. }
  210. /* Check whether the FWU state machine is in the correct state. */
  211. if ((desc->state != IMAGE_STATE_RESET) &&
  212. (desc->state != IMAGE_STATE_COPYING)) {
  213. WARN("BL1-FWU: Copy not allowed at this point of the FWU"
  214. " process.\n");
  215. return -EPERM;
  216. }
  217. if ((image_src == 0U) || (block_size == 0U) ||
  218. check_uptr_overflow(image_src, block_size - 1)) {
  219. WARN("BL1-FWU: Copy not allowed due to invalid image source"
  220. " or block size\n");
  221. return -ENOMEM;
  222. }
  223. if (desc->state == IMAGE_STATE_COPYING) {
  224. /*
  225. * There must have been at least 1 copy operation for this image
  226. * previously.
  227. */
  228. assert(desc->copied_size != 0U);
  229. /*
  230. * The image size must have been recorded in the 1st copy
  231. * operation.
  232. */
  233. image_size = desc->image_info.image_size;
  234. assert(image_size != 0);
  235. assert(desc->copied_size < image_size);
  236. INFO("BL1-FWU: Continuing image copy in blocks\n");
  237. } else { /* desc->state == IMAGE_STATE_RESET */
  238. INFO("BL1-FWU: Initial call to copy an image\n");
  239. /*
  240. * image_size is relevant only for the 1st copy request, it is
  241. * then ignored for subsequent calls for this image.
  242. */
  243. if (image_size == 0) {
  244. WARN("BL1-FWU: Copy not allowed due to invalid image"
  245. " size\n");
  246. return -ENOMEM;
  247. }
  248. /* Check that the image size to load is within limit */
  249. if (image_size > desc->image_info.image_max_size) {
  250. WARN("BL1-FWU: Image size out of bounds\n");
  251. return -ENOMEM;
  252. }
  253. /* Save the given image size. */
  254. desc->image_info.image_size = image_size;
  255. /* Make sure the image doesn't overlap other images. */
  256. if (bl1_fwu_image_check_overlaps(image_id) != 0) {
  257. desc->image_info.image_size = 0;
  258. WARN("BL1-FWU: This image overlaps another one\n");
  259. return -EPERM;
  260. }
  261. /*
  262. * copied_size must be explicitly initialized here because the
  263. * FWU code doesn't necessarily do it when it resets the state
  264. * machine.
  265. */
  266. desc->copied_size = 0;
  267. }
  268. /*
  269. * If the given block size is more than the total image size
  270. * then clip the former to the latter.
  271. */
  272. remaining = image_size - desc->copied_size;
  273. if (block_size > remaining) {
  274. WARN("BL1-FWU: Block size is too big, clipping it.\n");
  275. block_size = remaining;
  276. }
  277. /* Make sure the source image is mapped in memory. */
  278. if (bl1_plat_mem_check(image_src, block_size, flags) != 0) {
  279. WARN("BL1-FWU: Source image is not mapped.\n");
  280. return -ENOMEM;
  281. }
  282. if (bl1_fwu_add_loaded_id(image_id) != 0) {
  283. WARN("BL1-FWU: Too many images loaded at the same time.\n");
  284. return -ENOMEM;
  285. }
  286. /* Allow the platform to handle pre-image load before copying */
  287. if (desc->state == IMAGE_STATE_RESET) {
  288. if (bl1_plat_handle_pre_image_load(image_id) != 0) {
  289. ERROR("BL1-FWU: Failure in pre-image load of image id %d\n",
  290. image_id);
  291. return -EPERM;
  292. }
  293. }
  294. /* Everything looks sane. Go ahead and copy the block of data. */
  295. dest_addr = desc->image_info.image_base + desc->copied_size;
  296. (void)memcpy((void *) dest_addr, (const void *) image_src, block_size);
  297. flush_dcache_range(dest_addr, block_size);
  298. desc->copied_size += block_size;
  299. desc->state = (block_size == remaining) ?
  300. IMAGE_STATE_COPIED : IMAGE_STATE_COPYING;
  301. INFO("BL1-FWU: Copy operation successful.\n");
  302. return 0;
  303. }
  304. /*******************************************************************************
  305. * This function is responsible for authenticating Normal/Secure images.
  306. ******************************************************************************/
  307. static int bl1_fwu_image_auth(unsigned int image_id,
  308. uintptr_t image_src,
  309. unsigned int image_size,
  310. unsigned int flags)
  311. {
  312. int result;
  313. uintptr_t base_addr;
  314. unsigned int total_size;
  315. image_desc_t *desc;
  316. /* Get the image descriptor. */
  317. desc = bl1_plat_get_image_desc(image_id);
  318. if (desc == NULL)
  319. return -EPERM;
  320. if (GET_SECURITY_STATE(flags) == SECURE) {
  321. if (desc->state != IMAGE_STATE_RESET) {
  322. WARN("BL1-FWU: Authentication from secure world "
  323. "while in invalid state\n");
  324. return -EPERM;
  325. }
  326. } else {
  327. if (GET_SECURITY_STATE(desc->ep_info.h.attr) == SECURE) {
  328. if (desc->state != IMAGE_STATE_COPIED) {
  329. WARN("BL1-FWU: Authentication of secure image "
  330. "from non-secure world while not in copied state\n");
  331. return -EPERM;
  332. }
  333. } else {
  334. if (desc->state != IMAGE_STATE_RESET) {
  335. WARN("BL1-FWU: Authentication of non-secure image "
  336. "from non-secure world while in invalid state\n");
  337. return -EPERM;
  338. }
  339. }
  340. }
  341. if (desc->state == IMAGE_STATE_COPIED) {
  342. /*
  343. * Image is in COPIED state.
  344. * Use the stored address and size.
  345. */
  346. base_addr = desc->image_info.image_base;
  347. total_size = desc->image_info.image_size;
  348. } else {
  349. if ((image_src == 0U) || (image_size == 0U) ||
  350. check_uptr_overflow(image_src, image_size - 1)) {
  351. WARN("BL1-FWU: Auth not allowed due to invalid"
  352. " image source/size\n");
  353. return -ENOMEM;
  354. }
  355. /*
  356. * Image is in RESET state.
  357. * Check the parameters and authenticate the source image in place.
  358. */
  359. if (bl1_plat_mem_check(image_src, image_size,
  360. desc->ep_info.h.attr) != 0) {
  361. WARN("BL1-FWU: Authentication arguments source/size not mapped\n");
  362. return -ENOMEM;
  363. }
  364. if (bl1_fwu_add_loaded_id(image_id) != 0) {
  365. WARN("BL1-FWU: Too many images loaded at the same time.\n");
  366. return -ENOMEM;
  367. }
  368. base_addr = image_src;
  369. total_size = image_size;
  370. /* Update the image size in the descriptor. */
  371. desc->image_info.image_size = total_size;
  372. }
  373. /*
  374. * Authenticate the image.
  375. */
  376. INFO("BL1-FWU: Authenticating image_id:%d\n", image_id);
  377. result = auth_mod_verify_img(image_id, (void *)base_addr, total_size);
  378. if (result != 0) {
  379. WARN("BL1-FWU: Authentication Failed err=%d\n", result);
  380. /*
  381. * Authentication has failed.
  382. * Clear the memory if the image was copied.
  383. * This is to prevent an attack where this contains
  384. * some malicious code that can somehow be executed later.
  385. */
  386. if (desc->state == IMAGE_STATE_COPIED) {
  387. /* Clear the memory.*/
  388. zero_normalmem((void *)base_addr, total_size);
  389. flush_dcache_range(base_addr, total_size);
  390. /* Indicate that image can be copied again*/
  391. desc->state = IMAGE_STATE_RESET;
  392. }
  393. /*
  394. * Even if this fails it's ok because the ID isn't in the array.
  395. * The image cannot be in RESET state here, it is checked at the
  396. * beginning of the function.
  397. */
  398. (void)bl1_fwu_remove_loaded_id(image_id);
  399. return -EAUTH;
  400. }
  401. /* Indicate that image is in authenticated state. */
  402. desc->state = IMAGE_STATE_AUTHENTICATED;
  403. /* Allow the platform to handle post-image load */
  404. result = bl1_plat_handle_post_image_load(image_id);
  405. if (result != 0) {
  406. ERROR("BL1-FWU: Failure %d in post-image load of image id %d\n",
  407. result, image_id);
  408. /*
  409. * Panic here as the platform handling of post-image load is
  410. * not correct.
  411. */
  412. plat_error_handler(result);
  413. }
  414. /*
  415. * Flush image_info to memory so that other
  416. * secure world images can see changes.
  417. */
  418. flush_dcache_range((uintptr_t)&desc->image_info,
  419. sizeof(image_info_t));
  420. INFO("BL1-FWU: Authentication was successful\n");
  421. return 0;
  422. }
  423. /*******************************************************************************
  424. * This function is responsible for executing Secure images.
  425. ******************************************************************************/
  426. static int bl1_fwu_image_execute(unsigned int image_id,
  427. void **handle,
  428. unsigned int flags)
  429. {
  430. /* Get the image descriptor. */
  431. image_desc_t *desc = bl1_plat_get_image_desc(image_id);
  432. /*
  433. * Execution is NOT allowed if:
  434. * image_id is invalid OR
  435. * Caller is from Secure world OR
  436. * Image is Non-Secure OR
  437. * Image is Non-Executable OR
  438. * Image is NOT in AUTHENTICATED state.
  439. */
  440. if ((desc == NULL) ||
  441. (GET_SECURITY_STATE(flags) == SECURE) ||
  442. (GET_SECURITY_STATE(desc->ep_info.h.attr) == NON_SECURE) ||
  443. (EP_GET_EXE(desc->ep_info.h.attr) == NON_EXECUTABLE) ||
  444. (desc->state != IMAGE_STATE_AUTHENTICATED)) {
  445. WARN("BL1-FWU: Execution not allowed due to invalid state/args\n");
  446. return -EPERM;
  447. }
  448. INFO("BL1-FWU: Executing Secure image\n");
  449. #ifdef __aarch64__
  450. /* Save NS-EL1 system registers. */
  451. cm_el1_sysregs_context_save(NON_SECURE);
  452. #endif
  453. /* Prepare the image for execution. */
  454. bl1_prepare_next_image(image_id);
  455. /* Update the secure image id. */
  456. sec_exec_image_id = image_id;
  457. #ifdef __aarch64__
  458. *handle = cm_get_context(SECURE);
  459. #else
  460. *handle = smc_get_ctx(SECURE);
  461. #endif
  462. return 0;
  463. }
  464. /*******************************************************************************
  465. * This function is responsible for resuming execution in the other security
  466. * world
  467. ******************************************************************************/
  468. static register_t bl1_fwu_image_resume(register_t image_param,
  469. void **handle,
  470. unsigned int flags)
  471. {
  472. image_desc_t *desc;
  473. unsigned int resume_sec_state;
  474. unsigned int caller_sec_state = GET_SECURITY_STATE(flags);
  475. /* Get the image descriptor for last executed secure image id. */
  476. desc = bl1_plat_get_image_desc(sec_exec_image_id);
  477. if (caller_sec_state == NON_SECURE) {
  478. if (desc == NULL) {
  479. WARN("BL1-FWU: Resume not allowed due to no available"
  480. "secure image\n");
  481. return -EPERM;
  482. }
  483. } else {
  484. /* desc must be valid for secure world callers */
  485. assert(desc != NULL);
  486. }
  487. assert(GET_SECURITY_STATE(desc->ep_info.h.attr) == SECURE);
  488. assert(EP_GET_EXE(desc->ep_info.h.attr) == EXECUTABLE);
  489. if (caller_sec_state == SECURE) {
  490. assert(desc->state == IMAGE_STATE_EXECUTED);
  491. /* Update the flags. */
  492. desc->state = IMAGE_STATE_INTERRUPTED;
  493. resume_sec_state = NON_SECURE;
  494. } else {
  495. assert(desc->state == IMAGE_STATE_INTERRUPTED);
  496. /* Update the flags. */
  497. desc->state = IMAGE_STATE_EXECUTED;
  498. resume_sec_state = SECURE;
  499. }
  500. INFO("BL1-FWU: Resuming %s world context\n",
  501. (resume_sec_state == SECURE) ? "secure" : "normal");
  502. #ifdef __aarch64__
  503. /* Save the EL1 system registers of calling world. */
  504. cm_el1_sysregs_context_save(caller_sec_state);
  505. /* Restore the EL1 system registers of resuming world. */
  506. cm_el1_sysregs_context_restore(resume_sec_state);
  507. /* Update the next context. */
  508. cm_set_next_eret_context(resume_sec_state);
  509. *handle = cm_get_context(resume_sec_state);
  510. #else
  511. /* Update the next context. */
  512. cm_set_next_context(cm_get_context(resume_sec_state));
  513. /* Prepare the smc context for the next BL image. */
  514. smc_set_next_ctx(resume_sec_state);
  515. *handle = smc_get_ctx(resume_sec_state);
  516. #endif
  517. return image_param;
  518. }
  519. /*******************************************************************************
  520. * This function is responsible for resuming normal world context.
  521. ******************************************************************************/
  522. static int bl1_fwu_sec_image_done(void **handle, unsigned int flags)
  523. {
  524. image_desc_t *desc;
  525. /* Make sure caller is from the secure world */
  526. if (GET_SECURITY_STATE(flags) == NON_SECURE) {
  527. WARN("BL1-FWU: Image done not allowed from normal world\n");
  528. return -EPERM;
  529. }
  530. /* Get the image descriptor for last executed secure image id */
  531. desc = bl1_plat_get_image_desc(sec_exec_image_id);
  532. /* desc must correspond to a valid secure executing image */
  533. assert(desc != NULL);
  534. assert(GET_SECURITY_STATE(desc->ep_info.h.attr) == SECURE);
  535. assert(EP_GET_EXE(desc->ep_info.h.attr) == EXECUTABLE);
  536. assert(desc->state == IMAGE_STATE_EXECUTED);
  537. #if ENABLE_ASSERTIONS
  538. int rc = bl1_fwu_remove_loaded_id(sec_exec_image_id);
  539. assert(rc == 0);
  540. #else
  541. bl1_fwu_remove_loaded_id(sec_exec_image_id);
  542. #endif
  543. /* Update the flags. */
  544. desc->state = IMAGE_STATE_RESET;
  545. sec_exec_image_id = INVALID_IMAGE_ID;
  546. INFO("BL1-FWU: Resuming Normal world context\n");
  547. #ifdef __aarch64__
  548. /*
  549. * Secure world is done so no need to save the context.
  550. * Just restore the Non-Secure context.
  551. */
  552. cm_el1_sysregs_context_restore(NON_SECURE);
  553. /* Update the next context. */
  554. cm_set_next_eret_context(NON_SECURE);
  555. *handle = cm_get_context(NON_SECURE);
  556. #else
  557. /* Update the next context. */
  558. cm_set_next_context(cm_get_context(NON_SECURE));
  559. /* Prepare the smc context for the next BL image. */
  560. smc_set_next_ctx(NON_SECURE);
  561. *handle = smc_get_ctx(NON_SECURE);
  562. #endif
  563. return 0;
  564. }
  565. /*******************************************************************************
  566. * This function provides the opportunity for users to perform any
  567. * platform specific handling after the Firmware update is done.
  568. ******************************************************************************/
  569. __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved)
  570. {
  571. NOTICE("BL1-FWU: *******FWU Process Completed*******\n");
  572. /*
  573. * Call platform done function.
  574. */
  575. bl1_plat_fwu_done(client_cookie, reserved);
  576. assert(false);
  577. }
  578. /*******************************************************************************
  579. * This function resets an image to IMAGE_STATE_RESET. It fails if the image is
  580. * being executed.
  581. ******************************************************************************/
  582. static int bl1_fwu_image_reset(unsigned int image_id, unsigned int flags)
  583. {
  584. image_desc_t *desc = bl1_plat_get_image_desc(image_id);
  585. if ((desc == NULL) || (GET_SECURITY_STATE(flags) == SECURE)) {
  586. WARN("BL1-FWU: Reset not allowed due to invalid args\n");
  587. return -EPERM;
  588. }
  589. switch (desc->state) {
  590. case IMAGE_STATE_RESET:
  591. /* Nothing to do. */
  592. break;
  593. case IMAGE_STATE_INTERRUPTED:
  594. case IMAGE_STATE_AUTHENTICATED:
  595. case IMAGE_STATE_COPIED:
  596. case IMAGE_STATE_COPYING:
  597. if (bl1_fwu_remove_loaded_id(image_id) != 0) {
  598. WARN("BL1-FWU: Image reset couldn't find the image ID\n");
  599. return -EPERM;
  600. }
  601. if (desc->copied_size != 0U) {
  602. /* Clear the memory if the image is copied */
  603. assert(GET_SECURITY_STATE(desc->ep_info.h.attr)
  604. == SECURE);
  605. zero_normalmem((void *)desc->image_info.image_base,
  606. desc->copied_size);
  607. flush_dcache_range(desc->image_info.image_base,
  608. desc->copied_size);
  609. }
  610. /* Reset status variables */
  611. desc->copied_size = 0;
  612. desc->image_info.image_size = 0;
  613. desc->state = IMAGE_STATE_RESET;
  614. /* Clear authentication state */
  615. auth_img_flags[image_id] = 0;
  616. break;
  617. case IMAGE_STATE_EXECUTED:
  618. default:
  619. assert(false); /* Unreachable */
  620. break;
  621. }
  622. return 0;
  623. }