marvell_io_storage.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (C) 2018 Marvell International Ltd.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. * https://spdx.org/licenses
  6. */
  7. #include <assert.h>
  8. #include <string.h>
  9. #include <platform_def.h>
  10. #include <common/bl_common.h>
  11. #include <common/debug.h>
  12. #include <drivers/io/io_driver.h>
  13. #include <drivers/io/io_fip.h>
  14. #include <drivers/io/io_memmap.h>
  15. #include <drivers/io/io_storage.h>
  16. #include <tools_share/firmware_image_package.h>
  17. /* IO devices */
  18. static const io_dev_connector_t *fip_dev_con;
  19. static uintptr_t fip_dev_handle;
  20. static const io_dev_connector_t *memmap_dev_con;
  21. static uintptr_t memmap_dev_handle;
  22. static const io_block_spec_t fip_block_spec = {
  23. .offset = PLAT_MARVELL_FIP_BASE,
  24. .length = PLAT_MARVELL_FIP_MAX_SIZE
  25. };
  26. static const io_uuid_spec_t bl2_uuid_spec = {
  27. .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
  28. };
  29. static const io_uuid_spec_t scp_bl2_uuid_spec = {
  30. .uuid = UUID_SCP_FIRMWARE_SCP_BL2,
  31. };
  32. static const io_uuid_spec_t bl31_uuid_spec = {
  33. .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
  34. };
  35. static const io_uuid_spec_t bl32_uuid_spec = {
  36. .uuid = UUID_SECURE_PAYLOAD_BL32,
  37. };
  38. static const io_uuid_spec_t bl32_extra1_uuid_spec = {
  39. .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1,
  40. };
  41. static const io_uuid_spec_t bl32_extra2_uuid_spec = {
  42. .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2,
  43. };
  44. static const io_uuid_spec_t bl33_uuid_spec = {
  45. .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
  46. };
  47. static int open_fip(const uintptr_t spec);
  48. static int open_memmap(const uintptr_t spec);
  49. struct plat_io_policy {
  50. uintptr_t *dev_handle;
  51. uintptr_t image_spec;
  52. int (*check)(const uintptr_t spec);
  53. };
  54. /* By default, Marvell platforms load images from the FIP */
  55. static const struct plat_io_policy policies[] = {
  56. [FIP_IMAGE_ID] = {
  57. &memmap_dev_handle,
  58. (uintptr_t)&fip_block_spec,
  59. open_memmap
  60. },
  61. [BL2_IMAGE_ID] = {
  62. &fip_dev_handle,
  63. (uintptr_t)&bl2_uuid_spec,
  64. open_fip
  65. },
  66. [SCP_BL2_IMAGE_ID] = {
  67. &fip_dev_handle,
  68. (uintptr_t)&scp_bl2_uuid_spec,
  69. open_fip
  70. },
  71. [BL31_IMAGE_ID] = {
  72. &fip_dev_handle,
  73. (uintptr_t)&bl31_uuid_spec,
  74. open_fip
  75. },
  76. [BL32_IMAGE_ID] = {
  77. &fip_dev_handle,
  78. (uintptr_t)&bl32_uuid_spec,
  79. open_fip
  80. },
  81. [BL32_EXTRA1_IMAGE_ID] = {
  82. &fip_dev_handle,
  83. (uintptr_t)&bl32_extra1_uuid_spec,
  84. open_fip
  85. },
  86. [BL32_EXTRA2_IMAGE_ID] = {
  87. &fip_dev_handle,
  88. (uintptr_t)&bl32_extra2_uuid_spec,
  89. open_fip
  90. },
  91. [BL33_IMAGE_ID] = {
  92. &fip_dev_handle,
  93. (uintptr_t)&bl33_uuid_spec,
  94. open_fip
  95. },
  96. };
  97. /* Weak definitions may be overridden in specific ARM standard platform */
  98. #pragma weak plat_marvell_io_setup
  99. #pragma weak plat_marvell_get_alt_image_source
  100. static int open_fip(const uintptr_t spec)
  101. {
  102. int result;
  103. uintptr_t local_image_handle;
  104. /* See if a Firmware Image Package is available */
  105. result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
  106. if (result == 0) {
  107. result = io_open(fip_dev_handle, spec, &local_image_handle);
  108. if (result == 0) {
  109. VERBOSE("Using FIP\n");
  110. io_close(local_image_handle);
  111. }
  112. }
  113. return result;
  114. }
  115. static int open_memmap(const uintptr_t spec)
  116. {
  117. int result;
  118. uintptr_t local_image_handle;
  119. result = io_dev_init(memmap_dev_handle, (uintptr_t)NULL);
  120. if (result == 0) {
  121. result = io_open(memmap_dev_handle, spec, &local_image_handle);
  122. if (result == 0) {
  123. VERBOSE("Using Memmap\n");
  124. io_close(local_image_handle);
  125. }
  126. }
  127. return result;
  128. }
  129. void marvell_io_setup(void)
  130. {
  131. int io_result;
  132. io_result = register_io_dev_fip(&fip_dev_con);
  133. assert(io_result == 0);
  134. io_result = register_io_dev_memmap(&memmap_dev_con);
  135. assert(io_result == 0);
  136. /* Open connections to devices and cache the handles */
  137. io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
  138. &fip_dev_handle);
  139. assert(io_result == 0);
  140. io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL,
  141. &memmap_dev_handle);
  142. assert(io_result == 0);
  143. /* Ignore improbable errors in release builds */
  144. (void)io_result;
  145. }
  146. void plat_marvell_io_setup(void)
  147. {
  148. marvell_io_setup();
  149. }
  150. int plat_marvell_get_alt_image_source(
  151. unsigned int image_id __attribute__((unused)),
  152. uintptr_t *dev_handle __attribute__((unused)),
  153. uintptr_t *image_spec __attribute__((unused)))
  154. {
  155. /* By default do not try an alternative */
  156. return -ENOENT;
  157. }
  158. /*
  159. * Return an IO device handle and specification which can be used to access
  160. * an image. Use this to enforce platform load policy
  161. */
  162. int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
  163. uintptr_t *image_spec)
  164. {
  165. int result;
  166. const struct plat_io_policy *policy;
  167. assert(image_id < ARRAY_SIZE(policies));
  168. policy = &policies[image_id];
  169. result = policy->check(policy->image_spec);
  170. if (result == 0) {
  171. *image_spec = policy->image_spec;
  172. *dev_handle = *(policy->dev_handle);
  173. } else {
  174. VERBOSE("Trying alternative IO\n");
  175. result = plat_marvell_get_alt_image_source(image_id, dev_handle,
  176. image_spec);
  177. }
  178. return result;
  179. }
  180. /*
  181. * See if a Firmware Image Package is available,
  182. * by checking if TOC is valid or not.
  183. */
  184. int marvell_io_is_toc_valid(void)
  185. {
  186. int result;
  187. result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
  188. return result == 0;
  189. }