splash_source.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. default:
  105. printf("Error: unsupported location storage.\n");
  106. return -ENODEV;
  107. }
  108. if (res)
  109. printf("Error: could not access storage.\n");
  110. return res;
  111. }
  112. #ifdef CONFIG_USB_STORAGE
  113. static int splash_init_usb(void)
  114. {
  115. int err;
  116. err = usb_init();
  117. if (err)
  118. return err;
  119. return usb_stor_scan(1) < 0 ? -ENODEV : 0;
  120. }
  121. #else
  122. static inline int splash_init_usb(void)
  123. {
  124. printf("Cannot load splash image: no USB support\n");
  125. return -ENOSYS;
  126. }
  127. #endif
  128. #ifdef CONFIG_CMD_SATA
  129. static int splash_init_sata(void)
  130. {
  131. return sata_initialize();
  132. }
  133. #else
  134. static inline int splash_init_sata(void)
  135. {
  136. printf("Cannot load splash image: no SATA support\n");
  137. return -ENOSYS;
  138. }
  139. #endif
  140. #define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp"
  141. static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr)
  142. {
  143. int res = 0;
  144. loff_t bmp_size;
  145. char *splash_file;
  146. splash_file = getenv("splashfile");
  147. if (!splash_file)
  148. splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME;
  149. if (location->storage == SPLASH_STORAGE_USB)
  150. res = splash_init_usb();
  151. if (location->storage == SPLASH_STORAGE_SATA)
  152. res = splash_init_sata();
  153. if (res)
  154. return res;
  155. res = splash_select_fs_dev(location);
  156. if (res)
  157. return res;
  158. res = fs_size(splash_file, &bmp_size);
  159. if (res) {
  160. printf("Error (%d): cannot determine file size\n", res);
  161. return res;
  162. }
  163. if (bmp_load_addr + bmp_size >= gd->start_addr_sp) {
  164. printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n");
  165. return -EFAULT;
  166. }
  167. splash_select_fs_dev(location);
  168. return fs_read(splash_file, bmp_load_addr, 0, 0, NULL);
  169. }
  170. /**
  171. * select_splash_location - return the splash location based on board support
  172. * and env variable "splashsource".
  173. *
  174. * @locations: An array of supported splash locations.
  175. * @size: Size of splash_locations array.
  176. *
  177. * @return: If a null set of splash locations is given, or
  178. * splashsource env variable is set to unsupported value
  179. * return NULL.
  180. * If splashsource env variable is not defined
  181. * return the first entry in splash_locations as default.
  182. * If splashsource env variable contains a supported value
  183. * return the location selected by splashsource.
  184. */
  185. static struct splash_location *select_splash_location(
  186. struct splash_location *locations, uint size)
  187. {
  188. int i;
  189. char *env_splashsource;
  190. if (!locations || size == 0)
  191. return NULL;
  192. env_splashsource = getenv("splashsource");
  193. if (env_splashsource == NULL)
  194. return &locations[0];
  195. for (i = 0; i < size; i++) {
  196. if (!strcmp(locations[i].name, env_splashsource))
  197. return &locations[i];
  198. }
  199. printf("splashsource env variable set to unsupported value\n");
  200. return NULL;
  201. }
  202. /**
  203. * splash_source_load - load splash image from a supported location.
  204. *
  205. * Select a splash image location based on the value of splashsource environment
  206. * variable and the board supported splash source locations, and load a
  207. * splashimage to the address pointed to by splashimage environment variable.
  208. *
  209. * @locations: An array of supported splash locations.
  210. * @size: Size of splash_locations array.
  211. *
  212. * @return: 0 on success, negative value on failure.
  213. */
  214. int splash_source_load(struct splash_location *locations, uint size)
  215. {
  216. struct splash_location *splash_location;
  217. char *env_splashimage_value;
  218. u32 bmp_load_addr;
  219. env_splashimage_value = getenv("splashimage");
  220. if (env_splashimage_value == NULL)
  221. return -ENOENT;
  222. bmp_load_addr = simple_strtoul(env_splashimage_value, 0, 16);
  223. if (bmp_load_addr == 0) {
  224. printf("Error: bad splashimage address specified\n");
  225. return -EFAULT;
  226. }
  227. splash_location = select_splash_location(locations, size);
  228. if (!splash_location)
  229. return -EINVAL;
  230. if (splash_location->flags & SPLASH_STORAGE_RAW)
  231. return splash_load_raw(splash_location, bmp_load_addr);
  232. else if (splash_location->flags & SPLASH_STORAGE_FS)
  233. return splash_load_fs(splash_location, bmp_load_addr);
  234. return -EINVAL;
  235. }