splash_source.c 6.4 KB

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