splash_source.c 9.9 KB

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