bl2_io_storage.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. /*
  2. * Copyright (c) 2015-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <arch_helpers.h>
  9. #include <common/debug.h>
  10. #include <common/desc_image_load.h>
  11. #include <drivers/fwu/fwu.h>
  12. #include <drivers/fwu/fwu_metadata.h>
  13. #include <drivers/io/io_block.h>
  14. #include <drivers/io/io_driver.h>
  15. #include <drivers/io/io_encrypted.h>
  16. #include <drivers/io/io_fip.h>
  17. #include <drivers/io/io_memmap.h>
  18. #include <drivers/io/io_mtd.h>
  19. #include <drivers/io/io_storage.h>
  20. #include <drivers/mmc.h>
  21. #include <drivers/partition/efi.h>
  22. #include <drivers/partition/partition.h>
  23. #include <drivers/raw_nand.h>
  24. #include <drivers/spi_nand.h>
  25. #include <drivers/spi_nor.h>
  26. #include <drivers/st/stm32_fmc2_nand.h>
  27. #include <drivers/st/stm32_qspi.h>
  28. #include <drivers/st/stm32_sdmmc2.h>
  29. #include <drivers/usb_device.h>
  30. #include <lib/fconf/fconf.h>
  31. #include <lib/mmio.h>
  32. #include <lib/utils.h>
  33. #include <plat/common/platform.h>
  34. #include <tools_share/firmware_image_package.h>
  35. #include <platform_def.h>
  36. #include <stm32cubeprogrammer.h>
  37. #include <stm32mp_efi.h>
  38. #include <stm32mp_fconf_getter.h>
  39. #include <stm32mp_io_storage.h>
  40. #include <usb_dfu.h>
  41. /* IO devices */
  42. uintptr_t fip_dev_handle;
  43. uintptr_t storage_dev_handle;
  44. static const io_dev_connector_t *fip_dev_con;
  45. static uint32_t nand_block_sz __maybe_unused;
  46. #ifndef DECRYPTION_SUPPORT_none
  47. static const io_dev_connector_t *enc_dev_con;
  48. uintptr_t enc_dev_handle;
  49. #endif
  50. #if STM32MP_SDMMC || STM32MP_EMMC
  51. static struct mmc_device_info mmc_info;
  52. static uint8_t block_buffer[MMC_BLOCK_SIZE] __aligned(MMC_BLOCK_SIZE);
  53. static io_block_dev_spec_t mmc_block_dev_spec = {
  54. /* It's used as temp buffer in block driver */
  55. .buffer = {
  56. .offset = (size_t)&block_buffer,
  57. .length = MMC_BLOCK_SIZE,
  58. },
  59. .ops = {
  60. .read = mmc_read_blocks,
  61. .write = NULL,
  62. },
  63. .block_size = MMC_BLOCK_SIZE,
  64. };
  65. static const io_dev_connector_t *mmc_dev_con;
  66. #endif /* STM32MP_SDMMC || STM32MP_EMMC */
  67. #if STM32MP_SPI_NOR
  68. static io_mtd_dev_spec_t spi_nor_dev_spec = {
  69. .ops = {
  70. .init = spi_nor_init,
  71. .read = spi_nor_read,
  72. },
  73. };
  74. #endif
  75. #if STM32MP_RAW_NAND
  76. static io_mtd_dev_spec_t nand_dev_spec = {
  77. .ops = {
  78. .init = nand_raw_init,
  79. .read = nand_read,
  80. .seek = nand_seek_bb
  81. },
  82. };
  83. static const io_dev_connector_t *nand_dev_con;
  84. #endif
  85. #if STM32MP_SPI_NAND
  86. static io_mtd_dev_spec_t spi_nand_dev_spec = {
  87. .ops = {
  88. .init = spi_nand_init,
  89. .read = nand_read,
  90. .seek = nand_seek_bb
  91. },
  92. };
  93. #endif
  94. #if STM32MP_SPI_NAND || STM32MP_SPI_NOR
  95. static const io_dev_connector_t *spi_dev_con;
  96. #endif
  97. #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
  98. static const io_dev_connector_t *memmap_dev_con;
  99. #endif
  100. io_block_spec_t image_block_spec = {
  101. .offset = 0U,
  102. .length = 0U,
  103. };
  104. int open_fip(const uintptr_t spec)
  105. {
  106. return io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
  107. }
  108. #ifndef DECRYPTION_SUPPORT_none
  109. int open_enc_fip(const uintptr_t spec)
  110. {
  111. int result;
  112. uintptr_t local_image_handle;
  113. result = io_dev_init(enc_dev_handle, (uintptr_t)ENC_IMAGE_ID);
  114. if (result != 0) {
  115. return result;
  116. }
  117. result = io_open(enc_dev_handle, spec, &local_image_handle);
  118. if (result != 0) {
  119. return result;
  120. }
  121. VERBOSE("Using encrypted FIP\n");
  122. io_close(local_image_handle);
  123. return 0;
  124. }
  125. #endif
  126. int open_storage(const uintptr_t spec)
  127. {
  128. return io_dev_init(storage_dev_handle, 0);
  129. }
  130. #if STM32MP_EMMC_BOOT
  131. static uint32_t get_boot_part_fip_header(void)
  132. {
  133. io_block_spec_t emmc_boot_fip_block_spec = {
  134. .offset = STM32MP_EMMC_BOOT_FIP_OFFSET,
  135. .length = MMC_BLOCK_SIZE, /* We are interested only in first 4 bytes */
  136. };
  137. uint32_t magic = 0U;
  138. int io_result;
  139. size_t bytes_read;
  140. uintptr_t fip_hdr_handle;
  141. io_result = io_open(storage_dev_handle, (uintptr_t)&emmc_boot_fip_block_spec,
  142. &fip_hdr_handle);
  143. assert(io_result == 0);
  144. io_result = io_read(fip_hdr_handle, (uintptr_t)&magic, sizeof(magic),
  145. &bytes_read);
  146. if ((io_result != 0) || (bytes_read != sizeof(magic))) {
  147. panic();
  148. }
  149. io_close(fip_hdr_handle);
  150. VERBOSE("%s: eMMC boot magic at offset 256K: %08x\n",
  151. __func__, magic);
  152. return magic;
  153. }
  154. #endif
  155. static void print_boot_device(boot_api_context_t *boot_context)
  156. {
  157. switch (boot_context->boot_interface_selected) {
  158. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
  159. INFO("Using SDMMC\n");
  160. break;
  161. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
  162. INFO("Using EMMC\n");
  163. break;
  164. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI:
  165. INFO("Using SPI NOR\n");
  166. break;
  167. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
  168. INFO("Using FMC NAND\n");
  169. break;
  170. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_SPI:
  171. INFO("Using SPI NAND\n");
  172. break;
  173. case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART:
  174. INFO("Using UART\n");
  175. break;
  176. case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB:
  177. INFO("Using USB\n");
  178. break;
  179. default:
  180. ERROR("Boot interface %u not found\n",
  181. boot_context->boot_interface_selected);
  182. panic();
  183. break;
  184. }
  185. if (boot_context->boot_interface_instance != 0U) {
  186. INFO(" Instance %d\n", boot_context->boot_interface_instance);
  187. }
  188. }
  189. #if STM32MP_SDMMC || STM32MP_EMMC
  190. static void boot_mmc(enum mmc_device_type mmc_dev_type,
  191. uint16_t boot_interface_instance)
  192. {
  193. int io_result __maybe_unused;
  194. struct stm32_sdmmc2_params params;
  195. zeromem(&params, sizeof(struct stm32_sdmmc2_params));
  196. mmc_info.mmc_dev_type = mmc_dev_type;
  197. switch (boot_interface_instance) {
  198. case 1:
  199. params.reg_base = STM32MP_SDMMC1_BASE;
  200. break;
  201. case 2:
  202. params.reg_base = STM32MP_SDMMC2_BASE;
  203. break;
  204. case 3:
  205. params.reg_base = STM32MP_SDMMC3_BASE;
  206. break;
  207. default:
  208. WARN("SDMMC instance not found, using default\n");
  209. if (mmc_dev_type == MMC_IS_SD) {
  210. params.reg_base = STM32MP_SDMMC1_BASE;
  211. } else {
  212. params.reg_base = STM32MP_SDMMC2_BASE;
  213. }
  214. break;
  215. }
  216. if (mmc_dev_type != MMC_IS_EMMC) {
  217. params.flags = MMC_FLAG_SD_CMD6;
  218. }
  219. params.device_info = &mmc_info;
  220. if (stm32_sdmmc2_mmc_init(&params) != 0) {
  221. ERROR("SDMMC%u init failed\n", boot_interface_instance);
  222. panic();
  223. }
  224. /* Open MMC as a block device to read FIP */
  225. io_result = register_io_dev_block(&mmc_dev_con);
  226. if (io_result != 0) {
  227. panic();
  228. }
  229. io_result = io_dev_open(mmc_dev_con, (uintptr_t)&mmc_block_dev_spec,
  230. &storage_dev_handle);
  231. assert(io_result == 0);
  232. #if STM32MP_EMMC_BOOT
  233. if (mmc_dev_type == MMC_IS_EMMC) {
  234. io_result = mmc_part_switch_current_boot();
  235. assert(io_result == 0);
  236. if (get_boot_part_fip_header() != TOC_HEADER_NAME) {
  237. WARN("%s: Can't find FIP header on eMMC boot partition. Trying GPT\n",
  238. __func__);
  239. io_result = mmc_part_switch_user();
  240. assert(io_result == 0);
  241. return;
  242. }
  243. VERBOSE("%s: FIP header found on eMMC boot partition\n",
  244. __func__);
  245. image_block_spec.offset = STM32MP_EMMC_BOOT_FIP_OFFSET;
  246. image_block_spec.length = mmc_boot_part_size() - STM32MP_EMMC_BOOT_FIP_OFFSET;
  247. }
  248. #endif
  249. }
  250. #endif /* STM32MP_SDMMC || STM32MP_EMMC */
  251. #if STM32MP_SPI_NOR
  252. static void boot_spi_nor(boot_api_context_t *boot_context)
  253. {
  254. int io_result __maybe_unused;
  255. io_result = stm32_qspi_init();
  256. assert(io_result == 0);
  257. io_result = register_io_dev_mtd(&spi_dev_con);
  258. assert(io_result == 0);
  259. /* Open connections to device */
  260. io_result = io_dev_open(spi_dev_con,
  261. (uintptr_t)&spi_nor_dev_spec,
  262. &storage_dev_handle);
  263. assert(io_result == 0);
  264. }
  265. #endif /* STM32MP_SPI_NOR */
  266. #if STM32MP_RAW_NAND || STM32MP_SPI_NAND
  267. /*
  268. * This function returns 0 if it can find an alternate
  269. * image to be loaded or a negative errno otherwise.
  270. */
  271. static int try_nand_backup_partitions(unsigned int image_id)
  272. {
  273. static unsigned int backup_id;
  274. static unsigned int backup_block_nb;
  275. /* Check if NAND storage used */
  276. if (nand_block_sz == 0U) {
  277. return -ENODEV;
  278. }
  279. if (backup_id != image_id) {
  280. backup_block_nb = PLATFORM_MTD_MAX_PART_SIZE / nand_block_sz;
  281. backup_id = image_id;
  282. }
  283. if (backup_block_nb-- == 0U) {
  284. return -ENOSPC;
  285. }
  286. #if PSA_FWU_SUPPORT
  287. if (((image_block_spec.offset < STM32MP_NAND_FIP_B_OFFSET) &&
  288. ((image_block_spec.offset + nand_block_sz) >= STM32MP_NAND_FIP_B_OFFSET)) ||
  289. (image_block_spec.offset + nand_block_sz >= STM32MP_NAND_FIP_B_MAX_OFFSET)) {
  290. return 0;
  291. }
  292. #endif
  293. image_block_spec.offset += nand_block_sz;
  294. return 0;
  295. }
  296. static const struct plat_try_images_ops try_img_ops = {
  297. .next_instance = try_nand_backup_partitions,
  298. };
  299. #endif /* STM32MP_RAW_NAND || STM32MP_SPI_NAND */
  300. #if STM32MP_RAW_NAND
  301. static void boot_fmc2_nand(boot_api_context_t *boot_context)
  302. {
  303. int io_result __maybe_unused;
  304. plat_setup_try_img_ops(&try_img_ops);
  305. io_result = stm32_fmc2_init();
  306. assert(io_result == 0);
  307. /* Register the IO device on this platform */
  308. io_result = register_io_dev_mtd(&nand_dev_con);
  309. assert(io_result == 0);
  310. /* Open connections to device */
  311. io_result = io_dev_open(nand_dev_con, (uintptr_t)&nand_dev_spec,
  312. &storage_dev_handle);
  313. assert(io_result == 0);
  314. nand_block_sz = nand_dev_spec.erase_size;
  315. }
  316. #endif /* STM32MP_RAW_NAND */
  317. #if STM32MP_SPI_NAND
  318. static void boot_spi_nand(boot_api_context_t *boot_context)
  319. {
  320. int io_result __maybe_unused;
  321. plat_setup_try_img_ops(&try_img_ops);
  322. io_result = stm32_qspi_init();
  323. assert(io_result == 0);
  324. io_result = register_io_dev_mtd(&spi_dev_con);
  325. assert(io_result == 0);
  326. /* Open connections to device */
  327. io_result = io_dev_open(spi_dev_con,
  328. (uintptr_t)&spi_nand_dev_spec,
  329. &storage_dev_handle);
  330. assert(io_result == 0);
  331. nand_block_sz = spi_nand_dev_spec.erase_size;
  332. }
  333. #endif /* STM32MP_SPI_NAND */
  334. #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
  335. static void mmap_io_setup(void)
  336. {
  337. int io_result __maybe_unused;
  338. io_result = register_io_dev_memmap(&memmap_dev_con);
  339. assert(io_result == 0);
  340. io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL,
  341. &storage_dev_handle);
  342. assert(io_result == 0);
  343. }
  344. #if STM32MP_UART_PROGRAMMER
  345. static void stm32cubeprogrammer_uart(void)
  346. {
  347. int ret __maybe_unused;
  348. boot_api_context_t *boot_context =
  349. (boot_api_context_t *)stm32mp_get_boot_ctx_address();
  350. uintptr_t uart_base;
  351. uart_base = get_uart_address(boot_context->boot_interface_instance);
  352. ret = stm32cubeprog_uart_load(uart_base, DWL_BUFFER_BASE, DWL_BUFFER_SIZE);
  353. assert(ret == 0);
  354. }
  355. #endif
  356. #if STM32MP_USB_PROGRAMMER
  357. static void stm32cubeprogrammer_usb(void)
  358. {
  359. int ret __maybe_unused;
  360. struct usb_handle *pdev;
  361. /* Init USB on platform */
  362. pdev = usb_dfu_plat_init();
  363. ret = stm32cubeprog_usb_load(pdev, DWL_BUFFER_BASE, DWL_BUFFER_SIZE);
  364. assert(ret == 0);
  365. }
  366. #endif
  367. #endif /* STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER */
  368. void stm32mp_io_setup(void)
  369. {
  370. int io_result __maybe_unused;
  371. boot_api_context_t *boot_context =
  372. (boot_api_context_t *)stm32mp_get_boot_ctx_address();
  373. print_boot_device(boot_context);
  374. if ((boot_context->boot_partition_used_toboot == 1U) ||
  375. (boot_context->boot_partition_used_toboot == 2U)) {
  376. INFO("Boot used partition fsbl%u\n",
  377. boot_context->boot_partition_used_toboot);
  378. }
  379. io_result = register_io_dev_fip(&fip_dev_con);
  380. assert(io_result == 0);
  381. io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
  382. &fip_dev_handle);
  383. #ifndef DECRYPTION_SUPPORT_none
  384. io_result = register_io_dev_enc(&enc_dev_con);
  385. assert(io_result == 0);
  386. io_result = io_dev_open(enc_dev_con, (uintptr_t)NULL,
  387. &enc_dev_handle);
  388. assert(io_result == 0);
  389. #endif
  390. switch (boot_context->boot_interface_selected) {
  391. #if STM32MP_SDMMC
  392. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
  393. dmbsy();
  394. boot_mmc(MMC_IS_SD, boot_context->boot_interface_instance);
  395. break;
  396. #endif
  397. #if STM32MP_EMMC
  398. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
  399. dmbsy();
  400. boot_mmc(MMC_IS_EMMC, boot_context->boot_interface_instance);
  401. break;
  402. #endif
  403. #if STM32MP_SPI_NOR
  404. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI:
  405. dmbsy();
  406. boot_spi_nor(boot_context);
  407. break;
  408. #endif
  409. #if STM32MP_RAW_NAND
  410. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
  411. dmbsy();
  412. boot_fmc2_nand(boot_context);
  413. break;
  414. #endif
  415. #if STM32MP_SPI_NAND
  416. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_SPI:
  417. dmbsy();
  418. boot_spi_nand(boot_context);
  419. break;
  420. #endif
  421. #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER
  422. #if STM32MP_UART_PROGRAMMER
  423. case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART:
  424. #endif
  425. #if STM32MP_USB_PROGRAMMER
  426. case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB:
  427. #endif
  428. dmbsy();
  429. mmap_io_setup();
  430. break;
  431. #endif
  432. default:
  433. ERROR("Boot interface %d not supported\n",
  434. boot_context->boot_interface_selected);
  435. panic();
  436. break;
  437. }
  438. }
  439. int bl2_plat_handle_pre_image_load(unsigned int image_id)
  440. {
  441. static bool gpt_init_done __maybe_unused;
  442. uint16_t boot_itf = stm32mp_get_boot_itf_selected();
  443. switch (boot_itf) {
  444. #if STM32MP_SDMMC || STM32MP_EMMC
  445. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
  446. #if STM32MP_EMMC_BOOT
  447. if (image_block_spec.offset == STM32MP_EMMC_BOOT_FIP_OFFSET) {
  448. break;
  449. }
  450. #endif
  451. /* fallthrough */
  452. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
  453. if (!gpt_init_done) {
  454. /*
  455. * With FWU Multi Bank feature enabled, the selection of
  456. * the image to boot will be done by fwu_init calling the
  457. * platform hook, plat_fwu_set_images_source.
  458. */
  459. #if !PSA_FWU_SUPPORT
  460. const partition_entry_t *entry;
  461. const struct efi_guid fip_guid = STM32MP_FIP_GUID;
  462. partition_init(GPT_IMAGE_ID);
  463. entry = get_partition_entry_by_type(&fip_guid);
  464. if (entry == NULL) {
  465. entry = get_partition_entry(FIP_IMAGE_NAME);
  466. if (entry == NULL) {
  467. ERROR("Could NOT find the %s partition!\n",
  468. FIP_IMAGE_NAME);
  469. return -ENOENT;
  470. }
  471. }
  472. image_block_spec.offset = entry->start;
  473. image_block_spec.length = entry->length;
  474. #endif
  475. gpt_init_done = true;
  476. } else {
  477. bl_mem_params_node_t *bl_mem_params = get_bl_mem_params_node(image_id);
  478. assert(bl_mem_params != NULL);
  479. mmc_block_dev_spec.buffer.offset = bl_mem_params->image_info.image_base;
  480. mmc_block_dev_spec.buffer.length = bl_mem_params->image_info.image_max_size;
  481. }
  482. break;
  483. #endif
  484. #if STM32MP_RAW_NAND || STM32MP_SPI_NAND
  485. #if STM32MP_RAW_NAND
  486. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
  487. #endif
  488. #if STM32MP_SPI_NAND
  489. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_SPI:
  490. #endif
  491. /*
  492. * With FWU Multi Bank feature enabled, the selection of
  493. * the image to boot will be done by fwu_init calling the
  494. * platform hook, plat_fwu_set_images_source.
  495. */
  496. #if !PSA_FWU_SUPPORT
  497. image_block_spec.offset = STM32MP_NAND_FIP_OFFSET;
  498. #endif
  499. break;
  500. #endif
  501. #if STM32MP_SPI_NOR
  502. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI:
  503. /*
  504. * With FWU Multi Bank feature enabled, the selection of
  505. * the image to boot will be done by fwu_init calling the
  506. * platform hook, plat_fwu_set_images_source.
  507. */
  508. #if !PSA_FWU_SUPPORT
  509. image_block_spec.offset = STM32MP_NOR_FIP_OFFSET;
  510. #endif
  511. break;
  512. #endif
  513. #if STM32MP_UART_PROGRAMMER
  514. case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART:
  515. if (image_id == FW_CONFIG_ID) {
  516. stm32cubeprogrammer_uart();
  517. /* FIP loaded at DWL address */
  518. image_block_spec.offset = DWL_BUFFER_BASE;
  519. image_block_spec.length = DWL_BUFFER_SIZE;
  520. }
  521. break;
  522. #endif
  523. #if STM32MP_USB_PROGRAMMER
  524. case BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB:
  525. if (image_id == FW_CONFIG_ID) {
  526. stm32cubeprogrammer_usb();
  527. /* FIP loaded at DWL address */
  528. image_block_spec.offset = DWL_BUFFER_BASE;
  529. image_block_spec.length = DWL_BUFFER_SIZE;
  530. }
  531. break;
  532. #endif
  533. default:
  534. ERROR("FIP Not found\n");
  535. panic();
  536. }
  537. return 0;
  538. }
  539. /*
  540. * Return an IO device handle and specification which can be used to access
  541. * an image. Use this to enforce platform load policy.
  542. */
  543. int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
  544. uintptr_t *image_spec)
  545. {
  546. int rc;
  547. const struct plat_io_policy *policy;
  548. policy = FCONF_GET_PROPERTY(stm32mp, io_policies, image_id);
  549. rc = policy->check(policy->image_spec);
  550. if (rc == 0) {
  551. *image_spec = policy->image_spec;
  552. *dev_handle = *(policy->dev_handle);
  553. }
  554. return rc;
  555. }
  556. #if PSA_FWU_SUPPORT
  557. /*
  558. * In each boot in non-trial mode, we set the BKP register to
  559. * FWU_MAX_TRIAL_REBOOT, and return the active_index from metadata.
  560. *
  561. * As long as the update agent didn't update the "accepted" field in metadata
  562. * (i.e. we are in trial mode), we select the new active_index.
  563. * To avoid infinite boot loop at trial boot we decrement a BKP register.
  564. * If this counter is 0:
  565. * - an unexpected TAMPER event raised (that resets the BKP registers to 0)
  566. * - a power-off occurs before the update agent was able to update the
  567. * "accepted' field
  568. * - we already boot FWU_MAX_TRIAL_REBOOT times in trial mode.
  569. * we select the previous_active_index.
  570. */
  571. uint32_t plat_fwu_get_boot_idx(void)
  572. {
  573. /*
  574. * Select boot index and update boot counter only once per boot
  575. * even if this function is called several times.
  576. */
  577. static uint32_t boot_idx = INVALID_BOOT_IDX;
  578. if (boot_idx == INVALID_BOOT_IDX) {
  579. const struct fwu_metadata *data = fwu_get_metadata();
  580. boot_idx = data->active_index;
  581. if (data->bank_state[boot_idx] == FWU_BANK_STATE_VALID) {
  582. if (stm32_get_and_dec_fwu_trial_boot_cnt() == 0U) {
  583. WARN("Trial FWU fails %u times\n",
  584. FWU_MAX_TRIAL_REBOOT);
  585. boot_idx = fwu_get_alternate_boot_bank();
  586. }
  587. } else if (data->bank_state[boot_idx] ==
  588. FWU_BANK_STATE_ACCEPTED) {
  589. stm32_set_max_fwu_trial_boot_cnt();
  590. } else {
  591. ERROR("The active bank(%u) of the platform is in Invalid State.\n",
  592. boot_idx);
  593. boot_idx = fwu_get_alternate_boot_bank();
  594. stm32_clear_fwu_trial_boot_cnt();
  595. }
  596. }
  597. return boot_idx;
  598. }
  599. static void *stm32_get_image_spec(const struct efi_guid *img_type_guid)
  600. {
  601. unsigned int i;
  602. for (i = 0U; i < MAX_NUMBER_IDS; i++) {
  603. if ((guidcmp(&policies[i].img_type_guid, img_type_guid)) == 0) {
  604. return (void *)policies[i].image_spec;
  605. }
  606. }
  607. return NULL;
  608. }
  609. void plat_fwu_set_images_source(const struct fwu_metadata *metadata)
  610. {
  611. unsigned int i;
  612. uint32_t boot_idx;
  613. const partition_entry_t *entry __maybe_unused;
  614. const struct fwu_image_entry *img_entry;
  615. const void *img_type_guid;
  616. const void *img_guid;
  617. io_block_spec_t *image_spec;
  618. const uint16_t boot_itf = stm32mp_get_boot_itf_selected();
  619. boot_idx = plat_fwu_get_boot_idx();
  620. assert(boot_idx < NR_OF_FW_BANKS);
  621. VERBOSE("Selecting to boot from bank %u\n", boot_idx);
  622. img_entry = (void *)&metadata->fw_desc.img_entry;
  623. for (i = 0U; i < NR_OF_IMAGES_IN_FW_BANK; i++) {
  624. img_type_guid = &img_entry[i].img_type_guid;
  625. img_guid = &img_entry[i].img_bank_info[boot_idx].img_guid;
  626. image_spec = stm32_get_image_spec(img_type_guid);
  627. if (image_spec == NULL) {
  628. ERROR("Unable to get image spec for the image in the metadata\n");
  629. panic();
  630. }
  631. switch (boot_itf) {
  632. #if (STM32MP_SDMMC || STM32MP_EMMC)
  633. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
  634. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
  635. entry = get_partition_entry_by_guid(img_guid);
  636. if (entry == NULL) {
  637. ERROR("No partition with the uuid mentioned in metadata\n");
  638. panic();
  639. }
  640. image_spec->offset = entry->start;
  641. image_spec->length = entry->length;
  642. break;
  643. #endif
  644. #if STM32MP_SPI_NOR
  645. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI:
  646. if (guidcmp(img_guid, &STM32MP_NOR_FIP_A_GUID) == 0) {
  647. image_spec->offset = STM32MP_NOR_FIP_A_OFFSET;
  648. } else if (guidcmp(img_guid, &STM32MP_NOR_FIP_B_GUID) == 0) {
  649. image_spec->offset = STM32MP_NOR_FIP_B_OFFSET;
  650. } else {
  651. ERROR("Invalid uuid mentioned in metadata\n");
  652. panic();
  653. }
  654. break;
  655. #endif
  656. #if (STM32MP_RAW_NAND || STM32MP_SPI_NAND)
  657. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
  658. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_SPI:
  659. if (guidcmp(img_guid, &STM32MP_NAND_FIP_A_GUID) == 0) {
  660. image_spec->offset = STM32MP_NAND_FIP_A_OFFSET;
  661. } else if (guidcmp(img_guid, &STM32MP_NAND_FIP_B_GUID) == 0) {
  662. image_spec->offset = STM32MP_NAND_FIP_B_OFFSET;
  663. } else {
  664. ERROR("Invalid uuid mentioned in metadata\n");
  665. panic();
  666. }
  667. break;
  668. #endif
  669. default:
  670. panic();
  671. break;
  672. }
  673. }
  674. }
  675. static int set_metadata_image_source(unsigned int image_id,
  676. uintptr_t *handle,
  677. uintptr_t *image_spec)
  678. {
  679. struct plat_io_policy *policy;
  680. io_block_spec_t *spec __maybe_unused;
  681. const partition_entry_t *entry __maybe_unused;
  682. const uint16_t boot_itf = stm32mp_get_boot_itf_selected();
  683. policy = &policies[image_id];
  684. spec = (io_block_spec_t *)policy->image_spec;
  685. switch (boot_itf) {
  686. #if (STM32MP_SDMMC || STM32MP_EMMC)
  687. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_SD:
  688. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_EMMC:
  689. partition_init(GPT_IMAGE_ID);
  690. if (image_id == FWU_METADATA_IMAGE_ID) {
  691. entry = get_partition_entry(METADATA_PART_1);
  692. } else {
  693. entry = get_partition_entry(METADATA_PART_2);
  694. }
  695. if (entry == NULL) {
  696. ERROR("Unable to find a metadata partition\n");
  697. return -ENOENT;
  698. }
  699. spec->offset = entry->start;
  700. spec->length = entry->length;
  701. break;
  702. #endif
  703. #if STM32MP_SPI_NOR
  704. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NOR_SPI:
  705. if (image_id == FWU_METADATA_IMAGE_ID) {
  706. spec->offset = STM32MP_NOR_METADATA1_OFFSET;
  707. } else {
  708. spec->offset = STM32MP_NOR_METADATA2_OFFSET;
  709. }
  710. spec->length = sizeof(struct fwu_metadata);
  711. break;
  712. #endif
  713. #if (STM32MP_RAW_NAND || STM32MP_SPI_NAND)
  714. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_FMC:
  715. case BOOT_API_CTX_BOOT_INTERFACE_SEL_FLASH_NAND_SPI:
  716. if (image_id == FWU_METADATA_IMAGE_ID) {
  717. spec->offset = STM32MP_NAND_METADATA1_OFFSET;
  718. } else {
  719. spec->offset = STM32MP_NAND_METADATA2_OFFSET;
  720. }
  721. spec->length = sizeof(struct fwu_metadata);
  722. break;
  723. #endif
  724. default:
  725. panic();
  726. break;
  727. }
  728. *image_spec = policy->image_spec;
  729. *handle = *policy->dev_handle;
  730. return 0;
  731. }
  732. int plat_fwu_set_metadata_image_source(unsigned int image_id,
  733. uintptr_t *handle,
  734. uintptr_t *image_spec)
  735. {
  736. assert((image_id == FWU_METADATA_IMAGE_ID) ||
  737. (image_id == BKUP_FWU_METADATA_IMAGE_ID));
  738. return set_metadata_image_source(image_id, handle, image_spec);
  739. }
  740. #endif /* PSA_FWU_SUPPORT */