splash_source.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. return nand_read_skip_bad(nand_info[nand_curr_device], offset,
  46. &read_size, NULL,
  47. nand_info[nand_curr_device]->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_CMD_SATA
  140. static int splash_init_sata(void)
  141. {
  142. return sata_initialize();
  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. char *splash_file;
  186. splash_file = getenv("splashfile");
  187. if (!splash_file)
  188. splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
  189. if (location->storage == SPLASH_STORAGE_USB)
  190. res = splash_init_usb();
  191. if (location->storage == SPLASH_STORAGE_SATA)
  192. res = splash_init_sata();
  193. if (location->ubivol != NULL)
  194. res = splash_mount_ubifs(location);
  195. if (res)
  196. return res;
  197. res = splash_select_fs_dev(location);
  198. if (res)
  199. goto out;
  200. res = fs_size(splash_file, &bmp_size);
  201. if (res) {
  202. printf("Error (%d): cannot determine file size\n", res);
  203. goto out;
  204. }
  205. if (bmp_load_addr + bmp_size >= gd->start_addr_sp) {
  206. printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
  207. res = -EFAULT;
  208. goto out;
  209. }
  210. splash_select_fs_dev(location);
  211. res = fs_read(splash_file, bmp_load_addr, 0, 0, NULL);
  212. out:
  213. if (location->ubivol != NULL)
  214. splash_umount_ubifs();
  215. return res;
  216. }
  217. /**
  218. * select_splash_location - return the splash location based on board support
  219. * and env variable "splashsource".
  220. *
  221. * @locations: An array of supported splash locations.
  222. * @size: Size of splash_locations array.
  223. *
  224. * @return: If a null set of splash locations is given, or
  225. * splashsource env variable is set to unsupported value
  226. * return NULL.
  227. * If splashsource env variable is not defined
  228. * return the first entry in splash_locations as default.
  229. * If splashsource env variable contains a supported value
  230. * return the location selected by splashsource.
  231. */
  232. static struct splash_location *select_splash_location(
  233. struct splash_location *locations, uint size)
  234. {
  235. int i;
  236. char *env_splashsource;
  237. if (!locations || size == 0)
  238. return NULL;
  239. env_splashsource = getenv("splashsource");
  240. if (env_splashsource == NULL)
  241. return &locations[0];
  242. for (i = 0; i < size; i++) {
  243. if (!strcmp(locations[i].name, env_splashsource))
  244. return &locations[i];
  245. }
  246. printf("splashsource env variable set to unsupported value\n");
  247. return NULL;
  248. }
  249. #ifdef CONFIG_FIT
  250. static int splash_load_fit(struct splash_location *location, u32 bmp_load_addr)
  251. {
  252. int res;
  253. int node_offset;
  254. int splash_offset;
  255. int splash_size;
  256. struct image_header *img_header;
  257. const u32 *fit_header;
  258. u32 fit_size;
  259. const size_t header_size = sizeof(struct image_header);
  260. /* Read in image header */
  261. res = splash_storage_read_raw(location, bmp_load_addr, header_size);
  262. if (res < 0)
  263. return res;
  264. img_header = (struct image_header *)bmp_load_addr;
  265. fit_size = fdt_totalsize(img_header);
  266. /* Read in entire FIT */
  267. fit_header = (const u32 *)(bmp_load_addr + header_size);
  268. res = splash_storage_read_raw(location, (u32)fit_header, fit_size);
  269. if (res < 0)
  270. return res;
  271. res = fit_check_format(fit_header);
  272. if (!res) {
  273. debug("Could not find valid FIT image\n");
  274. return -EINVAL;
  275. }
  276. node_offset = fit_image_get_node(fit_header, location->name);
  277. if (node_offset < 0) {
  278. debug("Could not find splash image '%s' in FIT\n",
  279. location->name);
  280. return -ENOENT;
  281. }
  282. res = fit_image_get_data_offset(fit_header, node_offset,
  283. &splash_offset);
  284. if (res < 0) {
  285. printf("Failed to load splash image (err=%d)\n", res);
  286. return res;
  287. }
  288. res = fit_image_get_data_size(fit_header, node_offset, &splash_size);
  289. if (res < 0) {
  290. printf("Failed to load splash image (err=%d)\n", res);
  291. return res;
  292. }
  293. /* Align data offset to 4-byte boundrary */
  294. fit_size = fdt_totalsize(fit_header);
  295. fit_size = (fit_size + 3) & ~3;
  296. /* Read in the splash data */
  297. location->offset = (location->offset + fit_size + splash_offset);
  298. res = splash_storage_read_raw(location, bmp_load_addr , splash_size);
  299. if (res < 0)
  300. return res;
  301. return 0;
  302. }
  303. #endif /* CONFIG_FIT */
  304. /**
  305. * splash_source_load - load splash image from a supported location.
  306. *
  307. * Select a splash image location based on the value of splashsource environment
  308. * variable and the board supported splash source locations, and load a
  309. * splashimage to the address pointed to by splashimage environment variable.
  310. *
  311. * @locations: An array of supported splash locations.
  312. * @size: Size of splash_locations array.
  313. *
  314. * @return: 0 on success, negative value on failure.
  315. */
  316. int splash_source_load(struct splash_location *locations, uint size)
  317. {
  318. struct splash_location *splash_location;
  319. char *env_splashimage_value;
  320. u32 bmp_load_addr;
  321. env_splashimage_value = getenv("splashimage");
  322. if (env_splashimage_value == NULL)
  323. return -ENOENT;
  324. bmp_load_addr = simple_strtoul(env_splashimage_value, 0, 16);
  325. if (bmp_load_addr == 0) {
  326. printf("Error: bad splashimage address specified\n");
  327. return -EFAULT;
  328. }
  329. splash_location = select_splash_location(locations, size);
  330. if (!splash_location)
  331. return -EINVAL;
  332. if (splash_location->flags == SPLASH_STORAGE_RAW)
  333. return splash_load_raw(splash_location, bmp_load_addr);
  334. else if (splash_location->flags == SPLASH_STORAGE_FS)
  335. return splash_load_fs(splash_location, bmp_load_addr);
  336. #ifdef CONFIG_FIT
  337. else if (splash_location->flags == SPLASH_STORAGE_FIT)
  338. return splash_load_fit(splash_location, bmp_load_addr);
  339. #endif
  340. return -EINVAL;
  341. }