splash_source.c 7.8 KB

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