plat_storage.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (c) 2017, 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 <platform_def.h>
  9. #include <arch_helpers.h>
  10. #include <common/debug.h>
  11. #include <common/tbbr/tbbr_img_def.h>
  12. #include <drivers/io/io_block.h>
  13. #include <drivers/io/io_driver.h>
  14. #include <drivers/io/io_fip.h>
  15. #include <drivers/io/io_memmap.h>
  16. #include <drivers/io/io_storage.h>
  17. #include <drivers/mmc.h>
  18. #include <drivers/partition/partition.h>
  19. #include <lib/mmio.h>
  20. #include <lib/semihosting.h>
  21. #include <lib/utils.h>
  22. #include <tools_share/firmware_image_package.h>
  23. #if !POPLAR_RECOVERY
  24. static const io_dev_connector_t *emmc_dev_con;
  25. static uintptr_t emmc_dev_handle;
  26. static int open_emmc(const uintptr_t spec);
  27. static const io_block_spec_t emmc_fip_spec = {
  28. .offset = FIP_BASE_EMMC,
  29. .length = FIP_SIZE
  30. };
  31. static const io_block_dev_spec_t emmc_dev_spec = {
  32. .buffer = {
  33. .offset = POPLAR_EMMC_DATA_BASE,
  34. .length = POPLAR_EMMC_DATA_SIZE,
  35. },
  36. .ops = {
  37. .read = mmc_read_blocks,
  38. .write = mmc_write_blocks,
  39. },
  40. .block_size = MMC_BLOCK_SIZE,
  41. };
  42. #else
  43. static const io_dev_connector_t *mmap_dev_con;
  44. static uintptr_t mmap_dev_handle;
  45. static int open_mmap(const uintptr_t spec);
  46. static const io_block_spec_t loader_fip_spec = {
  47. .offset = FIP_BASE,
  48. .length = FIP_SIZE
  49. };
  50. #endif
  51. static const io_dev_connector_t *fip_dev_con;
  52. static uintptr_t fip_dev_handle;
  53. static int open_fip(const uintptr_t spec);
  54. static const io_uuid_spec_t bl2_uuid_spec = {
  55. .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
  56. };
  57. static const io_uuid_spec_t bl31_uuid_spec = {
  58. .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
  59. };
  60. static const io_uuid_spec_t bl32_uuid_spec = {
  61. .uuid = UUID_SECURE_PAYLOAD_BL32,
  62. };
  63. static const io_uuid_spec_t bl32_extra1_uuid_spec = {
  64. .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1,
  65. };
  66. static const io_uuid_spec_t bl32_extra2_uuid_spec = {
  67. .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2,
  68. };
  69. static const io_uuid_spec_t bl33_uuid_spec = {
  70. .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
  71. };
  72. struct plat_io_policy {
  73. uintptr_t *dev_handle;
  74. uintptr_t image_spec;
  75. int (*check)(const uintptr_t spec);
  76. };
  77. static const struct plat_io_policy policies[] = {
  78. #if !POPLAR_RECOVERY
  79. [FIP_IMAGE_ID] = {
  80. &emmc_dev_handle,
  81. (uintptr_t)&emmc_fip_spec,
  82. open_emmc
  83. },
  84. #else
  85. [FIP_IMAGE_ID] = {
  86. &mmap_dev_handle,
  87. (uintptr_t)&loader_fip_spec,
  88. open_mmap
  89. },
  90. #endif
  91. [BL2_IMAGE_ID] = {
  92. &fip_dev_handle,
  93. (uintptr_t)&bl2_uuid_spec,
  94. open_fip
  95. },
  96. [BL31_IMAGE_ID] = {
  97. &fip_dev_handle,
  98. (uintptr_t)&bl31_uuid_spec,
  99. open_fip
  100. },
  101. [BL32_IMAGE_ID] = {
  102. &fip_dev_handle,
  103. (uintptr_t)&bl32_uuid_spec,
  104. open_fip
  105. },
  106. [BL32_EXTRA1_IMAGE_ID] = {
  107. &fip_dev_handle,
  108. (uintptr_t)&bl32_extra1_uuid_spec,
  109. open_fip
  110. },
  111. [BL32_EXTRA2_IMAGE_ID] = {
  112. &fip_dev_handle,
  113. (uintptr_t)&bl32_extra2_uuid_spec,
  114. open_fip
  115. },
  116. [BL33_IMAGE_ID] = {
  117. &fip_dev_handle,
  118. (uintptr_t)&bl33_uuid_spec,
  119. open_fip
  120. },
  121. };
  122. #if !POPLAR_RECOVERY
  123. static int open_emmc(const uintptr_t spec)
  124. {
  125. int result;
  126. uintptr_t local_image_handle;
  127. result = io_dev_init(emmc_dev_handle, (uintptr_t)NULL);
  128. if (result == 0) {
  129. result = io_open(emmc_dev_handle, spec, &local_image_handle);
  130. if (result == 0) {
  131. INFO("Using eMMC\n");
  132. io_close(local_image_handle);
  133. } else {
  134. ERROR("error opening emmc\n");
  135. }
  136. } else {
  137. ERROR("error initializing emmc\n");
  138. }
  139. return result;
  140. }
  141. #else
  142. static int open_mmap(const uintptr_t spec)
  143. {
  144. int result;
  145. uintptr_t local_image_handle;
  146. result = io_dev_init(mmap_dev_handle, (uintptr_t)NULL);
  147. if (result == 0) {
  148. result = io_open(mmap_dev_handle, spec, &local_image_handle);
  149. if (result == 0) {
  150. INFO("Using mmap\n");
  151. io_close(local_image_handle);
  152. } else {
  153. ERROR("error opening mmap\n");
  154. }
  155. } else {
  156. ERROR("error initializing mmap\n");
  157. }
  158. return result;
  159. }
  160. #endif
  161. static int open_fip(const uintptr_t spec)
  162. {
  163. uintptr_t local_image_handle;
  164. int result;
  165. result = io_dev_init(fip_dev_handle, (uintptr_t) FIP_IMAGE_ID);
  166. if (result == 0) {
  167. result = io_open(fip_dev_handle, spec, &local_image_handle);
  168. if (result == 0) {
  169. INFO("Using FIP\n");
  170. io_close(local_image_handle);
  171. } else {
  172. ERROR("error opening fip\n");
  173. }
  174. } else {
  175. ERROR("error initializing fip\n");
  176. }
  177. return result;
  178. }
  179. int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
  180. uintptr_t *image_spec)
  181. {
  182. const struct plat_io_policy *policy;
  183. int result;
  184. assert(image_id < ARRAY_SIZE(policies));
  185. policy = &policies[image_id];
  186. result = policy->check(policy->image_spec);
  187. assert(result == 0);
  188. *image_spec = policy->image_spec;
  189. *dev_handle = *(policy->dev_handle);
  190. return result;
  191. }
  192. void plat_io_setup(void)
  193. {
  194. int result;
  195. #if !POPLAR_RECOVERY
  196. result = register_io_dev_block(&emmc_dev_con);
  197. #else
  198. result = register_io_dev_memmap(&mmap_dev_con);
  199. #endif
  200. assert(result == 0);
  201. result = register_io_dev_fip(&fip_dev_con);
  202. assert(result == 0);
  203. #if !POPLAR_RECOVERY
  204. result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
  205. &fip_dev_handle);
  206. #else
  207. result = io_dev_open(fip_dev_con, (uintptr_t)&loader_fip_spec,
  208. &fip_dev_handle);
  209. #endif
  210. assert(result == 0);
  211. #if !POPLAR_RECOVERY
  212. result = io_dev_open(emmc_dev_con, (uintptr_t)&emmc_dev_spec,
  213. &emmc_dev_handle);
  214. #else
  215. result = io_dev_open(mmap_dev_con, (uintptr_t)NULL, &mmap_dev_handle);
  216. #endif
  217. assert(result == 0);
  218. (void) result;
  219. }