splash_source.c 11 KB

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