splash_source.c 7.8 KB

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