splash_source.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2014 CompuLab, Ltd. <www.compulab.co.il>
  4. *
  5. * Authors: Igor Grinberg <grinberg@compulab.co.il>
  6. */
  7. #include <common.h>
  8. #include <bmp_layout.h>
  9. #include <command.h>
  10. #include <env.h>
  11. #include <errno.h>
  12. #include <fs.h>
  13. #include <fdt_support.h>
  14. #include <image.h>
  15. #include <log.h>
  16. #include <nand.h>
  17. #include <sata.h>
  18. #include <spi.h>
  19. #include <spi_flash.h>
  20. #include <splash.h>
  21. #include <usb.h>
  22. #include <asm/global_data.h>
  23. DECLARE_GLOBAL_DATA_PTR;
  24. #ifdef CONFIG_SPI_FLASH
  25. static struct spi_flash *sf;
  26. static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
  27. {
  28. if (!sf) {
  29. sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS,
  30. CONFIG_SF_DEFAULT_CS,
  31. CONFIG_SF_DEFAULT_SPEED,
  32. CONFIG_SF_DEFAULT_MODE);
  33. if (!sf)
  34. return -ENODEV;
  35. }
  36. return spi_flash_read(sf, offset, read_size, (void *)bmp_load_addr);
  37. }
  38. #else
  39. static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
  40. {
  41. debug("%s: sf support not available\n", __func__);
  42. return -ENOSYS;
  43. }
  44. #endif
  45. #ifdef CONFIG_CMD_NAND
  46. static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
  47. {
  48. struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
  49. return nand_read_skip_bad(mtd, offset,
  50. &read_size, NULL,
  51. mtd->size,
  52. (u_char *)bmp_load_addr);
  53. }
  54. #else
  55. static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size)
  56. {
  57. debug("%s: nand support not available\n", __func__);
  58. return -ENOSYS;
  59. }
  60. #endif
  61. static int splash_storage_read_raw(struct splash_location *location,
  62. u32 bmp_load_addr, size_t read_size)
  63. {
  64. u32 offset;
  65. if (!location)
  66. return -EINVAL;
  67. offset = location->offset;
  68. switch (location->storage) {
  69. case SPLASH_STORAGE_NAND:
  70. return splash_nand_read_raw(bmp_load_addr, offset, read_size);
  71. case SPLASH_STORAGE_SF:
  72. return splash_sf_read_raw(bmp_load_addr, offset, read_size);
  73. default:
  74. printf("Unknown splash location\n");
  75. }
  76. return -EINVAL;
  77. }
  78. static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr)
  79. {
  80. struct bmp_header *bmp_hdr;
  81. int res;
  82. size_t bmp_size, bmp_header_size = sizeof(struct bmp_header);
  83. if (bmp_load_addr + bmp_header_size >= gd->start_addr_sp)
  84. goto splash_address_too_high;
  85. res = splash_storage_read_raw(location, bmp_load_addr, bmp_header_size);
  86. if (res < 0)
  87. return res;
  88. bmp_hdr = (struct bmp_header *)bmp_load_addr;
  89. bmp_size = le32_to_cpu(bmp_hdr->file_size);
  90. if (bmp_load_addr + bmp_size >= gd->start_addr_sp)
  91. goto splash_address_too_high;
  92. return splash_storage_read_raw(location, bmp_load_addr, bmp_size);
  93. splash_address_too_high:
  94. printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
  95. return -EFAULT;
  96. }
  97. static int splash_select_fs_dev(struct splash_location *location)
  98. {
  99. int res;
  100. switch (location->storage) {
  101. case SPLASH_STORAGE_MMC:
  102. res = fs_set_blk_dev("mmc", location->devpart, FS_TYPE_ANY);
  103. break;
  104. case SPLASH_STORAGE_USB:
  105. res = fs_set_blk_dev("usb", location->devpart, FS_TYPE_ANY);
  106. break;
  107. case SPLASH_STORAGE_SATA:
  108. res = fs_set_blk_dev("sata", location->devpart, FS_TYPE_ANY);
  109. break;
  110. case SPLASH_STORAGE_NAND:
  111. if (location->ubivol != NULL)
  112. res = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
  113. else
  114. res = -ENODEV;
  115. break;
  116. default:
  117. printf("Error: unsupported location storage.\n");
  118. return -ENODEV;
  119. }
  120. if (res)
  121. printf("Error: could not access storage.\n");
  122. return res;
  123. }
  124. #ifdef CONFIG_USB_STORAGE
  125. static int splash_init_usb(void)
  126. {
  127. int err;
  128. err = usb_init();
  129. if (err)
  130. return err;
  131. #ifndef CONFIG_DM_USB
  132. err = usb_stor_scan(1) < 0 ? -ENODEV : 0;
  133. #endif
  134. return err;
  135. }
  136. #else
  137. static inline int splash_init_usb(void)
  138. {
  139. printf("Cannot load splash image: no USB support\n");
  140. return -ENOSYS;
  141. }
  142. #endif
  143. #ifdef CONFIG_SATA
  144. static int splash_init_sata(void)
  145. {
  146. return sata_probe(0);
  147. }
  148. #else
  149. static inline int splash_init_sata(void)
  150. {
  151. printf("Cannot load splash image: no SATA support\n");
  152. return -ENOSYS;
  153. }
  154. #endif
  155. #ifdef CONFIG_CMD_UBIFS
  156. static int splash_mount_ubifs(struct splash_location *location)
  157. {
  158. int res;
  159. char cmd[32];
  160. sprintf(cmd, "ubi part %s", location->mtdpart);
  161. res = run_command(cmd, 0);
  162. if (res)
  163. return res;
  164. sprintf(cmd, "ubifsmount %s", location->ubivol);
  165. res = run_command(cmd, 0);
  166. return res;
  167. }
  168. static inline int splash_umount_ubifs(void)
  169. {
  170. return run_command("ubifsumount", 0);
  171. }
  172. #else
  173. static inline int splash_mount_ubifs(struct splash_location *location)
  174. {
  175. printf("Cannot load splash image: no UBIFS support\n");
  176. return -ENOSYS;
  177. }
  178. static inline int splash_umount_ubifs(void)
  179. {
  180. printf("Cannot unmount UBIFS: no UBIFS support\n");
  181. return -ENOSYS;
  182. }
  183. #endif
  184. #define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp"
  185. static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr)
  186. {
  187. int res = 0;
  188. loff_t bmp_size;
  189. loff_t actread;
  190. char *splash_file;
  191. splash_file = env_get("splashfile");
  192. if (!splash_file)
  193. splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
  194. if (location->storage == SPLASH_STORAGE_USB)
  195. res = splash_init_usb();
  196. if (location->storage == SPLASH_STORAGE_SATA)
  197. res = splash_init_sata();
  198. if (location->ubivol != NULL)
  199. res = splash_mount_ubifs(location);
  200. if (res)
  201. return res;
  202. res = splash_select_fs_dev(location);
  203. if (res)
  204. goto out;
  205. res = fs_size(splash_file, &bmp_size);
  206. if (res) {
  207. printf("Error (%d): cannot determine file size\n", res);
  208. goto out;
  209. }
  210. if (bmp_load_addr + bmp_size >= gd->start_addr_sp) {
  211. printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
  212. res = -EFAULT;
  213. goto out;
  214. }
  215. splash_select_fs_dev(location);
  216. res = fs_read(splash_file, bmp_load_addr, 0, 0, &actread);
  217. out:
  218. if (location->ubivol != NULL)
  219. splash_umount_ubifs();
  220. return res;
  221. }
  222. /**
  223. * select_splash_location - return the splash location based on board support
  224. * and env variable "splashsource".
  225. *
  226. * @locations: An array of supported splash locations.
  227. * @size: Size of splash_locations array.
  228. *
  229. * @return: If a null set of splash locations is given, or
  230. * splashsource env variable is set to unsupported value
  231. * return NULL.
  232. * If splashsource env variable is not defined
  233. * return the first entry in splash_locations as default.
  234. * If splashsource env variable contains a supported value
  235. * return the location selected by splashsource.
  236. */
  237. static struct splash_location *select_splash_location(
  238. struct splash_location *locations, uint size)
  239. {
  240. int i;
  241. char *env_splashsource;
  242. if (!locations || size == 0)
  243. return NULL;
  244. env_splashsource = env_get("splashsource");
  245. if (env_splashsource == NULL)
  246. return &locations[0];
  247. for (i = 0; i < size; i++) {
  248. if (!strcmp(locations[i].name, env_splashsource))
  249. return &locations[i];
  250. }
  251. printf("splashsource env variable set to unsupported value\n");
  252. return NULL;
  253. }
  254. #ifdef CONFIG_FIT
  255. static int splash_load_fit(struct splash_location *location, u32 bmp_load_addr)
  256. {
  257. int res;
  258. int node_offset;
  259. const char *splash_file;
  260. const void *internal_splash_data;
  261. size_t internal_splash_size;
  262. int external_splash_addr;
  263. int external_splash_size;
  264. bool is_splash_external = false;
  265. struct image_header *img_header;
  266. const u32 *fit_header;
  267. u32 fit_size;
  268. const size_t header_size = sizeof(struct image_header);
  269. /* Read in image header */
  270. res = splash_storage_read_raw(location, bmp_load_addr, header_size);
  271. if (res < 0)
  272. return res;
  273. img_header = (struct image_header *)bmp_load_addr;
  274. if (image_get_magic(img_header) != FDT_MAGIC) {
  275. printf("Could not find FDT magic\n");
  276. return -EINVAL;
  277. }
  278. fit_size = fdt_totalsize(img_header);
  279. /* Read in entire FIT */
  280. fit_header = (const u32 *)(bmp_load_addr + header_size);
  281. res = splash_storage_read_raw(location, (u32)fit_header, fit_size);
  282. if (res < 0)
  283. return res;
  284. res = fit_check_format(fit_header, IMAGE_SIZE_INVAL);
  285. if (res) {
  286. debug("Could not find valid FIT image\n");
  287. return res;
  288. }
  289. /* Get the splash image node */
  290. splash_file = env_get("splashfile");
  291. if (!splash_file)
  292. splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
  293. node_offset = fit_image_get_node(fit_header, splash_file);
  294. if (node_offset < 0) {
  295. debug("Could not find splash image '%s' in FIT\n",
  296. splash_file);
  297. return -ENOENT;
  298. }
  299. /* Extract the splash data from FIT */
  300. /* 1. Test if splash is in FIT internal data. */
  301. if (!fit_image_get_data(fit_header, node_offset, &internal_splash_data, &internal_splash_size))
  302. memmove((void *)bmp_load_addr, internal_splash_data, internal_splash_size);
  303. /* 2. Test if splash is in FIT external data with fixed position. */
  304. else if (!fit_image_get_data_position(fit_header, node_offset, &external_splash_addr))
  305. is_splash_external = true;
  306. /* 3. Test if splash is in FIT external data with offset. */
  307. else if (!fit_image_get_data_offset(fit_header, node_offset, &external_splash_addr)) {
  308. /* Align data offset to 4-byte boundary */
  309. fit_size = ALIGN(fdt_totalsize(fit_header), 4);
  310. /* External splash offset means the offset by end of FIT header */
  311. external_splash_addr += location->offset + fit_size;
  312. is_splash_external = true;
  313. } else {
  314. printf("Failed to get splash image from FIT\n");
  315. return -ENODATA;
  316. }
  317. if (is_splash_external) {
  318. res = fit_image_get_data_size(fit_header, node_offset, &external_splash_size);
  319. if (res < 0) {
  320. printf("Failed to get size of splash image (err=%d)\n", res);
  321. return res;
  322. }
  323. /* Read in the splash data */
  324. location->offset = external_splash_addr;
  325. res = splash_storage_read_raw(location, bmp_load_addr, external_splash_size);
  326. if (res < 0)
  327. return res;
  328. }
  329. return 0;
  330. }
  331. #endif /* CONFIG_FIT */
  332. /**
  333. * splash_source_load - load splash image from a supported location.
  334. *
  335. * Select a splash image location based on the value of splashsource environment
  336. * variable and the board supported splash source locations, and load a
  337. * splashimage to the address pointed to by splashimage environment variable.
  338. *
  339. * @locations: An array of supported splash locations.
  340. * @size: Size of splash_locations array.
  341. *
  342. * @return: 0 on success, negative value on failure.
  343. */
  344. int splash_source_load(struct splash_location *locations, uint size)
  345. {
  346. struct splash_location *splash_location;
  347. char *env_splashimage_value;
  348. u32 bmp_load_addr;
  349. env_splashimage_value = env_get("splashimage");
  350. if (env_splashimage_value == NULL)
  351. return -ENOENT;
  352. bmp_load_addr = simple_strtoul(env_splashimage_value, 0, 16);
  353. if (bmp_load_addr == 0) {
  354. printf("Error: bad splashimage address specified\n");
  355. return -EFAULT;
  356. }
  357. splash_location = select_splash_location(locations, size);
  358. if (!splash_location)
  359. return -EINVAL;
  360. if (splash_location->flags == SPLASH_STORAGE_RAW)
  361. return splash_load_raw(splash_location, bmp_load_addr);
  362. else if (splash_location->flags == SPLASH_STORAGE_FS)
  363. return splash_load_fs(splash_location, bmp_load_addr);
  364. #ifdef CONFIG_FIT
  365. else if (splash_location->flags == SPLASH_STORAGE_FIT)
  366. return splash_load_fit(splash_location, bmp_load_addr);
  367. #endif
  368. return -EINVAL;
  369. }