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