spl_imx_romapi.c 6.3 KB

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