splash_source.c 11 KB

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