spl_imx_romapi.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2019 NXP
  4. */
  5. #include <common.h>
  6. #include <errno.h>
  7. #include <image.h>
  8. #include <log.h>
  9. #include <linux/libfdt.h>
  10. #include <spl.h>
  11. #include <asm/arch/sys_proto.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. static int is_boot_from_stream_device(u32 boot)
  14. {
  15. u32 interface;
  16. interface = boot >> 16;
  17. if (interface >= BT_DEV_TYPE_USB)
  18. return 1;
  19. if (interface == BT_DEV_TYPE_MMC && (boot & 1))
  20. return 1;
  21. return 0;
  22. }
  23. static ulong spl_romapi_read_seekable(struct spl_load_info *load,
  24. ulong sector, ulong count,
  25. void *buf)
  26. {
  27. u32 pagesize = *(u32 *)load->priv;
  28. volatile gd_t *pgd = gd;
  29. ulong byte = count * pagesize;
  30. int ret;
  31. u32 offset;
  32. offset = sector * pagesize;
  33. debug("ROM API load from 0x%x, size 0x%x\n", offset, (u32)byte);
  34. ret = g_rom_api->download_image(buf, offset, byte,
  35. ((uintptr_t)buf) ^ offset ^ byte);
  36. gd = pgd;
  37. if (ret == ROM_API_OKAY)
  38. return count;
  39. printf("ROM API Failure when load 0x%x\n", offset);
  40. return 0;
  41. }
  42. static int spl_romapi_load_image_seekable(struct spl_image_info *spl_image,
  43. struct spl_boot_device *bootdev,
  44. u32 rom_bt_dev)
  45. {
  46. volatile gd_t *pgd = gd;
  47. int ret;
  48. u32 offset;
  49. u32 pagesize, size;
  50. struct image_header *header;
  51. u32 image_offset;
  52. ret = g_rom_api->query_boot_infor(QUERY_IVT_OFF, &offset,
  53. ((uintptr_t)&offset) ^ QUERY_IVT_OFF);
  54. ret |= g_rom_api->query_boot_infor(QUERY_PAGE_SZ, &pagesize,
  55. ((uintptr_t)&pagesize) ^ QUERY_PAGE_SZ);
  56. ret |= g_rom_api->query_boot_infor(QUERY_IMG_OFF, &image_offset,
  57. ((uintptr_t)&image_offset) ^ QUERY_IMG_OFF);
  58. gd = pgd;
  59. if (ret != ROM_API_OKAY) {
  60. puts("ROMAPI: Failure query boot infor pagesize/offset\n");
  61. return -1;
  62. }
  63. header = (struct image_header *)(CONFIG_SPL_IMX_ROMAPI_LOADADDR);
  64. printf("image offset 0x%x, pagesize 0x%x, ivt offset 0x%x\n",
  65. image_offset, pagesize, offset);
  66. if (((rom_bt_dev >> 16) & 0xff) == BT_DEV_TYPE_FLEXSPINOR)
  67. offset = CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR * 512;
  68. else
  69. offset = image_offset +
  70. CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR * 512 - 0x8000;
  71. size = ALIGN(sizeof(struct image_header), pagesize);
  72. ret = g_rom_api->download_image((u8 *)header, offset, size,
  73. ((uintptr_t)header) ^ offset ^ size);
  74. gd = pgd;
  75. if (ret != ROM_API_OKAY) {
  76. printf("ROMAPI: download failure offset 0x%x size 0x%x\n",
  77. offset, size);
  78. return -1;
  79. }
  80. if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
  81. image_get_magic(header) == FDT_MAGIC) {
  82. struct spl_load_info load;
  83. memset(&load, 0, sizeof(load));
  84. load.bl_len = pagesize;
  85. load.read = spl_romapi_read_seekable;
  86. load.priv = &pagesize;
  87. return spl_load_simple_fit(spl_image, &load,
  88. offset / pagesize, header);
  89. } else {
  90. /* TODO */
  91. puts("Can't support legacy image\n");
  92. return -1;
  93. }
  94. return 0;
  95. }
  96. static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector,
  97. ulong count, void *buf)
  98. {
  99. memcpy(buf, (void *)(sector), count);
  100. if (load->priv) {
  101. ulong *p = (ulong *)load->priv;
  102. ulong total = sector + count;
  103. if (total > *p)
  104. *p = total;
  105. }
  106. return count;
  107. }
  108. static ulong get_fit_image_size(void *fit)
  109. {
  110. struct spl_image_info spl_image;
  111. struct spl_load_info spl_load_info;
  112. ulong last = (ulong)fit;
  113. memset(&spl_load_info, 0, sizeof(spl_load_info));
  114. spl_load_info.bl_len = 1;
  115. spl_load_info.read = spl_ram_load_read;
  116. spl_load_info.priv = &last;
  117. spl_load_simple_fit(&spl_image, &spl_load_info,
  118. (uintptr_t)fit, fit);
  119. return last - (ulong)fit;
  120. }
  121. u8 *search_fit_header(u8 *p, int size)
  122. {
  123. int i;
  124. for (i = 0; i < size; i += 4)
  125. if (genimg_get_format(p + i) == IMAGE_FORMAT_FIT)
  126. return p + i;
  127. return NULL;
  128. }
  129. static int spl_romapi_load_image_stream(struct spl_image_info *spl_image,
  130. struct spl_boot_device *bootdev)
  131. {
  132. struct spl_load_info load;
  133. volatile gd_t *pgd = gd;
  134. u32 pagesize, pg;
  135. int ret;
  136. int i = 0;
  137. u8 *p = (u8 *)CONFIG_SPL_IMX_ROMAPI_LOADADDR;
  138. u8 *pfit = NULL;
  139. int imagesize;
  140. int total;
  141. ret = g_rom_api->query_boot_infor(QUERY_PAGE_SZ, &pagesize,
  142. ((uintptr_t)&pagesize) ^ QUERY_PAGE_SZ);
  143. gd = pgd;
  144. if (ret != ROM_API_OKAY)
  145. puts("failure at query_boot_info\n");
  146. pg = pagesize;
  147. if (pg < 1024)
  148. pg = 1024;
  149. for (i = 0; i < 640; i++) {
  150. ret = g_rom_api->download_image(p, 0, pg,
  151. ((uintptr_t)p) ^ pg);
  152. gd = pgd;
  153. if (ret != ROM_API_OKAY) {
  154. puts("Steam(USB) download failure\n");
  155. return -1;
  156. }
  157. pfit = search_fit_header(p, pg);
  158. p += pg;
  159. if (pfit)
  160. break;
  161. }
  162. if (!pfit) {
  163. puts("Can't found uboot FIT image in 640K range \n");
  164. return -1;
  165. }
  166. if (p - pfit < sizeof(struct fdt_header)) {
  167. ret = g_rom_api->download_image(p, 0, pg, ((uintptr_t)p) ^ pg);
  168. gd = pgd;
  169. if (ret != ROM_API_OKAY) {
  170. puts("Steam(USB) download failure\n");
  171. return -1;
  172. }
  173. p += pg;
  174. }
  175. imagesize = fit_get_size(pfit);
  176. printf("Find FIT header 0x&%p, size %d\n", pfit, imagesize);
  177. if (p - pfit < imagesize) {
  178. imagesize -= p - pfit;
  179. /*need pagesize hear after ROM fix USB problme*/
  180. imagesize += pg - 1;
  181. imagesize /= pg;
  182. imagesize *= pg;
  183. printf("Need continue download %d\n", imagesize);
  184. ret = g_rom_api->download_image(p, 0, imagesize,
  185. ((uintptr_t)p) ^ imagesize);
  186. gd = pgd;
  187. p += imagesize;
  188. if (ret != ROM_API_OKAY) {
  189. printf("Failure download %d\n", imagesize);
  190. return -1;
  191. }
  192. }
  193. total = get_fit_image_size(pfit);
  194. total += 3;
  195. total &= ~0x3;
  196. imagesize = total - (p - pfit);
  197. imagesize += pagesize - 1;
  198. imagesize /= pagesize;
  199. imagesize *= pagesize;
  200. printf("Download %d, total fit %d\n", imagesize, total);
  201. ret = g_rom_api->download_image(p, 0, imagesize,
  202. ((uintptr_t)p) ^ imagesize);
  203. if (ret != ROM_API_OKAY)
  204. printf("ROM download failure %d\n", imagesize);
  205. memset(&load, 0, sizeof(load));
  206. load.bl_len = 1;
  207. load.read = spl_ram_load_read;
  208. return spl_load_simple_fit(spl_image, &load, (ulong)pfit, pfit);
  209. }
  210. int board_return_to_bootrom(struct spl_image_info *spl_image,
  211. struct spl_boot_device *bootdev)
  212. {
  213. volatile gd_t *pgd = gd;
  214. int ret;
  215. u32 boot;
  216. ret = g_rom_api->query_boot_infor(QUERY_BT_DEV, &boot,
  217. ((uintptr_t)&boot) ^ QUERY_BT_DEV);
  218. gd = pgd;
  219. if (ret != ROM_API_OKAY) {
  220. puts("ROMAPI: failure at query_boot_info\n");
  221. return -1;
  222. }
  223. if (is_boot_from_stream_device(boot))
  224. return spl_romapi_load_image_stream(spl_image, bootdev);
  225. return spl_romapi_load_image_seekable(spl_image, bootdev, boot);
  226. }