splash_source.c 9.8 KB

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