splash_source.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. return usb_stor_scan(1) < 0 ? -ENODEV : 0;
  126. }
  127. #else
  128. static inline int splash_init_usb(void)
  129. {
  130. printf("Cannot load splash image: no USB support\n");
  131. return -ENOSYS;
  132. }
  133. #endif
  134. #ifdef CONFIG_CMD_SATA
  135. static int splash_init_sata(void)
  136. {
  137. return sata_initialize();
  138. }
  139. #else
  140. static inline int splash_init_sata(void)
  141. {
  142. printf("Cannot load splash image: no SATA support\n");
  143. return -ENOSYS;
  144. }
  145. #endif
  146. #ifdef CONFIG_CMD_UBIFS
  147. static int splash_mount_ubifs(struct splash_location *location)
  148. {
  149. int res;
  150. char cmd[32];
  151. sprintf(cmd, "ubi part %s", location->mtdpart);
  152. res = run_command(cmd, 0);
  153. if (res)
  154. return res;
  155. sprintf(cmd, "ubifsmount %s", location->ubivol);
  156. res = run_command(cmd, 0);
  157. return res;
  158. }
  159. static inline int splash_umount_ubifs(void)
  160. {
  161. return run_command("ubifsumount", 0);
  162. }
  163. #else
  164. static inline int splash_mount_ubifs(struct splash_location *location)
  165. {
  166. printf("Cannot load splash image: no UBIFS support\n");
  167. return -ENOSYS;
  168. }
  169. static inline int splash_umount_ubifs(void)
  170. {
  171. printf("Cannot unmount UBIFS: no UBIFS support\n");
  172. return -ENOSYS;
  173. }
  174. #endif
  175. #define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp"
  176. static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr)
  177. {
  178. int res = 0;
  179. loff_t bmp_size;
  180. char *splash_file;
  181. splash_file = getenv("splashfile");
  182. if (!splash_file)
  183. splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
  184. if (location->storage == SPLASH_STORAGE_USB)
  185. res = splash_init_usb();
  186. if (location->storage == SPLASH_STORAGE_SATA)
  187. res = splash_init_sata();
  188. if (location->ubivol != NULL)
  189. res = splash_mount_ubifs(location);
  190. if (res)
  191. return res;
  192. res = splash_select_fs_dev(location);
  193. if (res)
  194. goto out;
  195. res = fs_size(splash_file, &bmp_size);
  196. if (res) {
  197. printf("Error (%d): cannot determine file size\n", res);
  198. goto out;
  199. }
  200. if (bmp_load_addr + bmp_size >= gd->start_addr_sp) {
  201. printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
  202. res = -EFAULT;
  203. goto out;
  204. }
  205. splash_select_fs_dev(location);
  206. res = fs_read(splash_file, bmp_load_addr, 0, 0, NULL);
  207. out:
  208. if (location->ubivol != NULL)
  209. splash_umount_ubifs();
  210. return res;
  211. }
  212. /**
  213. * select_splash_location - return the splash location based on board support
  214. * and env variable "splashsource".
  215. *
  216. * @locations: An array of supported splash locations.
  217. * @size: Size of splash_locations array.
  218. *
  219. * @return: If a null set of splash locations is given, or
  220. * splashsource env variable is set to unsupported value
  221. * return NULL.
  222. * If splashsource env variable is not defined
  223. * return the first entry in splash_locations as default.
  224. * If splashsource env variable contains a supported value
  225. * return the location selected by splashsource.
  226. */
  227. static struct splash_location *select_splash_location(
  228. struct splash_location *locations, uint size)
  229. {
  230. int i;
  231. char *env_splashsource;
  232. if (!locations || size == 0)
  233. return NULL;
  234. env_splashsource = getenv("splashsource");
  235. if (env_splashsource == NULL)
  236. return &locations[0];
  237. for (i = 0; i < size; i++) {
  238. if (!strcmp(locations[i].name, env_splashsource))
  239. return &locations[i];
  240. }
  241. printf("splashsource env variable set to unsupported value\n");
  242. return NULL;
  243. }
  244. /**
  245. * splash_source_load - load splash image from a supported location.
  246. *
  247. * Select a splash image location based on the value of splashsource environment
  248. * variable and the board supported splash source locations, and load a
  249. * splashimage to the address pointed to by splashimage environment variable.
  250. *
  251. * @locations: An array of supported splash locations.
  252. * @size: Size of splash_locations array.
  253. *
  254. * @return: 0 on success, negative value on failure.
  255. */
  256. int splash_source_load(struct splash_location *locations, uint size)
  257. {
  258. struct splash_location *splash_location;
  259. char *env_splashimage_value;
  260. u32 bmp_load_addr;
  261. env_splashimage_value = getenv("splashimage");
  262. if (env_splashimage_value == NULL)
  263. return -ENOENT;
  264. bmp_load_addr = simple_strtoul(env_splashimage_value, 0, 16);
  265. if (bmp_load_addr == 0) {
  266. printf("Error: bad splashimage address specified\n");
  267. return -EFAULT;
  268. }
  269. splash_location = select_splash_location(locations, size);
  270. if (!splash_location)
  271. return -EINVAL;
  272. if (splash_location->flags & SPLASH_STORAGE_RAW)
  273. return splash_load_raw(splash_location, bmp_load_addr);
  274. else if (splash_location->flags & SPLASH_STORAGE_FS)
  275. return splash_load_fs(splash_location, bmp_load_addr);
  276. return -EINVAL;
  277. }