splash_source.c 12 KB

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