]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
f4a40f05 IG |
2 | /* |
3 | * (C) Copyright 2014 CompuLab, Ltd. <www.compulab.co.il> | |
4 | * | |
5 | * Authors: Igor Grinberg <[email protected]> | |
f4a40f05 IG |
6 | */ |
7 | ||
8 | #include <common.h> | |
7583f1f5 | 9 | #include <bmp_layout.h> |
6947e3f5 | 10 | #include <errno.h> |
7583f1f5 | 11 | #include <fs.h> |
db1b79b8 | 12 | #include <fdt_support.h> |
7583f1f5 | 13 | #include <image.h> |
14 | #include <nand.h> | |
15 | #include <sata.h> | |
7e8d7f2a | 16 | #include <spi.h> |
7583f1f5 | 17 | #include <spi_flash.h> |
18 | #include <splash.h> | |
9bb4e947 | 19 | #include <usb.h> |
f4a40f05 IG |
20 | |
21 | DECLARE_GLOBAL_DATA_PTR; | |
22 | ||
7e8d7f2a NK |
23 | #ifdef CONFIG_SPI_FLASH |
24 | static struct spi_flash *sf; | |
bcbb6448 | 25 | static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size) |
7e8d7f2a NK |
26 | { |
27 | if (!sf) { | |
28 | sf = spi_flash_probe(CONFIG_SF_DEFAULT_BUS, | |
29 | CONFIG_SF_DEFAULT_CS, | |
30 | CONFIG_SF_DEFAULT_SPEED, | |
31 | CONFIG_SF_DEFAULT_MODE); | |
32 | if (!sf) | |
33 | return -ENODEV; | |
34 | } | |
35 | ||
36 | return spi_flash_read(sf, offset, read_size, (void *)bmp_load_addr); | |
37 | } | |
38 | #else | |
bcbb6448 | 39 | static int splash_sf_read_raw(u32 bmp_load_addr, int offset, size_t read_size) |
7e8d7f2a NK |
40 | { |
41 | debug("%s: sf support not available\n", __func__); | |
42 | return -ENOSYS; | |
43 | } | |
44 | #endif | |
45 | ||
f4a40f05 | 46 | #ifdef CONFIG_CMD_NAND |
bcbb6448 | 47 | static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size) |
7be4cd2c | 48 | { |
edba8cc4 GS |
49 | struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device); |
50 | return nand_read_skip_bad(mtd, offset, | |
7be4cd2c | 51 | &read_size, NULL, |
edba8cc4 | 52 | mtd->size, |
7be4cd2c NK |
53 | (u_char *)bmp_load_addr); |
54 | } | |
55 | #else | |
bcbb6448 | 56 | static int splash_nand_read_raw(u32 bmp_load_addr, int offset, size_t read_size) |
7be4cd2c NK |
57 | { |
58 | debug("%s: nand support not available\n", __func__); | |
59 | return -ENOSYS; | |
60 | } | |
61 | #endif | |
62 | ||
bcbb6448 | 63 | static int splash_storage_read_raw(struct splash_location *location, |
fd29dd55 | 64 | u32 bmp_load_addr, size_t read_size) |
7be4cd2c | 65 | { |
fd29dd55 NK |
66 | u32 offset; |
67 | ||
68 | if (!location) | |
69 | return -EINVAL; | |
70 | ||
71 | offset = location->offset; | |
72 | switch (location->storage) { | |
73 | case SPLASH_STORAGE_NAND: | |
bcbb6448 | 74 | return splash_nand_read_raw(bmp_load_addr, offset, read_size); |
7e8d7f2a | 75 | case SPLASH_STORAGE_SF: |
bcbb6448 | 76 | return splash_sf_read_raw(bmp_load_addr, offset, read_size); |
fd29dd55 NK |
77 | default: |
78 | printf("Unknown splash location\n"); | |
79 | } | |
80 | ||
81 | return -EINVAL; | |
7be4cd2c NK |
82 | } |
83 | ||
fd29dd55 | 84 | static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr) |
f4a40f05 IG |
85 | { |
86 | struct bmp_header *bmp_hdr; | |
87 | int res; | |
88 | size_t bmp_size, bmp_header_size = sizeof(struct bmp_header); | |
89 | ||
90 | if (bmp_load_addr + bmp_header_size >= gd->start_addr_sp) | |
91 | goto splash_address_too_high; | |
92 | ||
bcbb6448 | 93 | res = splash_storage_read_raw(location, bmp_load_addr, bmp_header_size); |
f4a40f05 IG |
94 | if (res < 0) |
95 | return res; | |
96 | ||
97 | bmp_hdr = (struct bmp_header *)bmp_load_addr; | |
98 | bmp_size = le32_to_cpu(bmp_hdr->file_size); | |
99 | ||
100 | if (bmp_load_addr + bmp_size >= gd->start_addr_sp) | |
101 | goto splash_address_too_high; | |
102 | ||
bcbb6448 | 103 | return splash_storage_read_raw(location, bmp_load_addr, bmp_size); |
f4a40f05 IG |
104 | |
105 | splash_address_too_high: | |
f82eb2fa | 106 | printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n"); |
f4a40f05 | 107 | |
6947e3f5 | 108 | return -EFAULT; |
f4a40f05 | 109 | } |
f4a40f05 | 110 | |
870dd309 NK |
111 | static int splash_select_fs_dev(struct splash_location *location) |
112 | { | |
113 | int res; | |
114 | ||
115 | switch (location->storage) { | |
116 | case SPLASH_STORAGE_MMC: | |
117 | res = fs_set_blk_dev("mmc", location->devpart, FS_TYPE_ANY); | |
118 | break; | |
9bb4e947 NK |
119 | case SPLASH_STORAGE_USB: |
120 | res = fs_set_blk_dev("usb", location->devpart, FS_TYPE_ANY); | |
121 | break; | |
50c2d2e1 NK |
122 | case SPLASH_STORAGE_SATA: |
123 | res = fs_set_blk_dev("sata", location->devpart, FS_TYPE_ANY); | |
124 | break; | |
1cb075c6 EM |
125 | case SPLASH_STORAGE_NAND: |
126 | if (location->ubivol != NULL) | |
127 | res = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS); | |
128 | else | |
129 | res = -ENODEV; | |
130 | break; | |
870dd309 NK |
131 | default: |
132 | printf("Error: unsupported location storage.\n"); | |
133 | return -ENODEV; | |
134 | } | |
135 | ||
136 | if (res) | |
137 | printf("Error: could not access storage.\n"); | |
138 | ||
139 | return res; | |
140 | } | |
141 | ||
9bb4e947 NK |
142 | #ifdef CONFIG_USB_STORAGE |
143 | static int splash_init_usb(void) | |
144 | { | |
145 | int err; | |
146 | ||
147 | err = usb_init(); | |
148 | if (err) | |
149 | return err; | |
150 | ||
d7b60fbf AB |
151 | #ifndef CONFIG_DM_USB |
152 | err = usb_stor_scan(1) < 0 ? -ENODEV : 0; | |
153 | #endif | |
154 | ||
155 | return err; | |
9bb4e947 NK |
156 | } |
157 | #else | |
158 | static inline int splash_init_usb(void) | |
159 | { | |
160 | printf("Cannot load splash image: no USB support\n"); | |
161 | return -ENOSYS; | |
162 | } | |
163 | #endif | |
164 | ||
10e40d54 | 165 | #ifdef CONFIG_SATA |
50c2d2e1 NK |
166 | static int splash_init_sata(void) |
167 | { | |
f19f1ecb | 168 | return sata_probe(0); |
50c2d2e1 NK |
169 | } |
170 | #else | |
171 | static inline int splash_init_sata(void) | |
172 | { | |
173 | printf("Cannot load splash image: no SATA support\n"); | |
174 | return -ENOSYS; | |
175 | } | |
176 | #endif | |
177 | ||
1cb075c6 EM |
178 | #ifdef CONFIG_CMD_UBIFS |
179 | static int splash_mount_ubifs(struct splash_location *location) | |
180 | { | |
181 | int res; | |
182 | char cmd[32]; | |
183 | ||
184 | sprintf(cmd, "ubi part %s", location->mtdpart); | |
185 | res = run_command(cmd, 0); | |
186 | if (res) | |
187 | return res; | |
188 | ||
189 | sprintf(cmd, "ubifsmount %s", location->ubivol); | |
190 | res = run_command(cmd, 0); | |
191 | ||
192 | return res; | |
193 | } | |
194 | ||
195 | static inline int splash_umount_ubifs(void) | |
196 | { | |
197 | return run_command("ubifsumount", 0); | |
198 | } | |
199 | #else | |
200 | static inline int splash_mount_ubifs(struct splash_location *location) | |
201 | { | |
202 | printf("Cannot load splash image: no UBIFS support\n"); | |
203 | return -ENOSYS; | |
204 | } | |
205 | ||
206 | static inline int splash_umount_ubifs(void) | |
207 | { | |
208 | printf("Cannot unmount UBIFS: no UBIFS support\n"); | |
209 | return -ENOSYS; | |
210 | } | |
211 | #endif | |
212 | ||
870dd309 NK |
213 | #define SPLASH_SOURCE_DEFAULT_FILE_NAME "splash.bmp" |
214 | ||
215 | static int splash_load_fs(struct splash_location *location, u32 bmp_load_addr) | |
216 | { | |
9bb4e947 | 217 | int res = 0; |
870dd309 | 218 | loff_t bmp_size; |
3cc6e707 | 219 | loff_t actread; |
870dd309 NK |
220 | char *splash_file; |
221 | ||
00caae6d | 222 | splash_file = env_get("splashfile"); |
870dd309 NK |
223 | if (!splash_file) |
224 | splash_file = SPLASH_SOURCE_DEFAULT_FILE_NAME; | |
225 | ||
9bb4e947 NK |
226 | if (location->storage == SPLASH_STORAGE_USB) |
227 | res = splash_init_usb(); | |
228 | ||
50c2d2e1 NK |
229 | if (location->storage == SPLASH_STORAGE_SATA) |
230 | res = splash_init_sata(); | |
231 | ||
1cb075c6 EM |
232 | if (location->ubivol != NULL) |
233 | res = splash_mount_ubifs(location); | |
234 | ||
9bb4e947 NK |
235 | if (res) |
236 | return res; | |
237 | ||
870dd309 NK |
238 | res = splash_select_fs_dev(location); |
239 | if (res) | |
1cb075c6 | 240 | goto out; |
870dd309 NK |
241 | |
242 | res = fs_size(splash_file, &bmp_size); | |
243 | if (res) { | |
244 | printf("Error (%d): cannot determine file size\n", res); | |
1cb075c6 | 245 | goto out; |
870dd309 NK |
246 | } |
247 | ||
248 | if (bmp_load_addr + bmp_size >= gd->start_addr_sp) { | |
249 | printf("Error: splashimage address too high. Data overwrites U-Boot and/or placed beyond DRAM boundaries.\n"); | |
1cb075c6 EM |
250 | res = -EFAULT; |
251 | goto out; | |
870dd309 NK |
252 | } |
253 | ||
254 | splash_select_fs_dev(location); | |
3cc6e707 | 255 | res = fs_read(splash_file, bmp_load_addr, 0, 0, &actread); |
1cb075c6 EM |
256 | |
257 | out: | |
258 | if (location->ubivol != NULL) | |
259 | splash_umount_ubifs(); | |
260 | ||
261 | return res; | |
870dd309 NK |
262 | } |
263 | ||
fd29dd55 NK |
264 | /** |
265 | * select_splash_location - return the splash location based on board support | |
266 | * and env variable "splashsource". | |
267 | * | |
268 | * @locations: An array of supported splash locations. | |
269 | * @size: Size of splash_locations array. | |
270 | * | |
271 | * @return: If a null set of splash locations is given, or | |
272 | * splashsource env variable is set to unsupported value | |
273 | * return NULL. | |
274 | * If splashsource env variable is not defined | |
275 | * return the first entry in splash_locations as default. | |
276 | * If splashsource env variable contains a supported value | |
277 | * return the location selected by splashsource. | |
278 | */ | |
279 | static struct splash_location *select_splash_location( | |
280 | struct splash_location *locations, uint size) | |
281 | { | |
282 | int i; | |
283 | char *env_splashsource; | |
284 | ||
285 | if (!locations || size == 0) | |
286 | return NULL; | |
287 | ||
00caae6d | 288 | env_splashsource = env_get("splashsource"); |
fd29dd55 NK |
289 | if (env_splashsource == NULL) |
290 | return &locations[0]; | |
291 | ||
292 | for (i = 0; i < size; i++) { | |
293 | if (!strcmp(locations[i].name, env_splashsource)) | |
294 | return &locations[i]; | |
295 | } | |
296 | ||
297 | printf("splashsource env variable set to unsupported value\n"); | |
298 | return NULL; | |
299 | } | |
300 | ||
db1b79b8 | 301 | #ifdef CONFIG_FIT |
302 | static int splash_load_fit(struct splash_location *location, u32 bmp_load_addr) | |
303 | { | |
304 | int res; | |
305 | int node_offset; | |
306 | int splash_offset; | |
307 | int splash_size; | |
308 | struct image_header *img_header; | |
309 | const u32 *fit_header; | |
310 | u32 fit_size; | |
311 | const size_t header_size = sizeof(struct image_header); | |
312 | ||
313 | /* Read in image header */ | |
314 | res = splash_storage_read_raw(location, bmp_load_addr, header_size); | |
315 | if (res < 0) | |
316 | return res; | |
317 | ||
318 | img_header = (struct image_header *)bmp_load_addr; | |
a7126edc NM |
319 | if (image_get_magic(img_header) != FDT_MAGIC) { |
320 | printf("Could not find FDT magic\n"); | |
321 | return -EINVAL; | |
322 | } | |
323 | ||
db1b79b8 | 324 | fit_size = fdt_totalsize(img_header); |
325 | ||
326 | /* Read in entire FIT */ | |
327 | fit_header = (const u32 *)(bmp_load_addr + header_size); | |
328 | res = splash_storage_read_raw(location, (u32)fit_header, fit_size); | |
329 | if (res < 0) | |
330 | return res; | |
331 | ||
332 | res = fit_check_format(fit_header); | |
333 | if (!res) { | |
334 | debug("Could not find valid FIT image\n"); | |
335 | return -EINVAL; | |
336 | } | |
337 | ||
338 | node_offset = fit_image_get_node(fit_header, location->name); | |
339 | if (node_offset < 0) { | |
340 | debug("Could not find splash image '%s' in FIT\n", | |
341 | location->name); | |
342 | return -ENOENT; | |
343 | } | |
344 | ||
345 | res = fit_image_get_data_offset(fit_header, node_offset, | |
346 | &splash_offset); | |
347 | if (res < 0) { | |
348 | printf("Failed to load splash image (err=%d)\n", res); | |
349 | return res; | |
350 | } | |
351 | ||
352 | res = fit_image_get_data_size(fit_header, node_offset, &splash_size); | |
353 | if (res < 0) { | |
354 | printf("Failed to load splash image (err=%d)\n", res); | |
355 | return res; | |
356 | } | |
357 | ||
358 | /* Align data offset to 4-byte boundrary */ | |
359 | fit_size = fdt_totalsize(fit_header); | |
360 | fit_size = (fit_size + 3) & ~3; | |
361 | ||
362 | /* Read in the splash data */ | |
363 | location->offset = (location->offset + fit_size + splash_offset); | |
364 | res = splash_storage_read_raw(location, bmp_load_addr , splash_size); | |
365 | if (res < 0) | |
366 | return res; | |
367 | ||
368 | return 0; | |
369 | } | |
370 | #endif /* CONFIG_FIT */ | |
371 | ||
f82eb2fa NK |
372 | /** |
373 | * splash_source_load - load splash image from a supported location. | |
374 | * | |
375 | * Select a splash image location based on the value of splashsource environment | |
376 | * variable and the board supported splash source locations, and load a | |
377 | * splashimage to the address pointed to by splashimage environment variable. | |
378 | * | |
379 | * @locations: An array of supported splash locations. | |
380 | * @size: Size of splash_locations array. | |
381 | * | |
382 | * @return: 0 on success, negative value on failure. | |
383 | */ | |
384 | int splash_source_load(struct splash_location *locations, uint size) | |
f4a40f05 | 385 | { |
fd29dd55 | 386 | struct splash_location *splash_location; |
f4a40f05 IG |
387 | char *env_splashimage_value; |
388 | u32 bmp_load_addr; | |
389 | ||
00caae6d | 390 | env_splashimage_value = env_get("splashimage"); |
f4a40f05 | 391 | if (env_splashimage_value == NULL) |
6947e3f5 | 392 | return -ENOENT; |
f4a40f05 IG |
393 | |
394 | bmp_load_addr = simple_strtoul(env_splashimage_value, 0, 16); | |
395 | if (bmp_load_addr == 0) { | |
396 | printf("Error: bad splashimage address specified\n"); | |
6947e3f5 | 397 | return -EFAULT; |
f4a40f05 IG |
398 | } |
399 | ||
fd29dd55 NK |
400 | splash_location = select_splash_location(locations, size); |
401 | if (!splash_location) | |
402 | return -EINVAL; | |
403 | ||
3b593f90 | 404 | if (splash_location->flags == SPLASH_STORAGE_RAW) |
870dd309 | 405 | return splash_load_raw(splash_location, bmp_load_addr); |
3b593f90 | 406 | else if (splash_location->flags == SPLASH_STORAGE_FS) |
870dd309 | 407 | return splash_load_fs(splash_location, bmp_load_addr); |
db1b79b8 | 408 | #ifdef CONFIG_FIT |
409 | else if (splash_location->flags == SPLASH_STORAGE_FIT) | |
410 | return splash_load_fit(splash_location, bmp_load_addr); | |
411 | #endif | |
870dd309 | 412 | return -EINVAL; |
f4a40f05 | 413 | } |