splash_source.c 11 KB

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