]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/compulab/common/splash.c
compulab: splash: support multiple splash sources
[karo-tx-uboot.git] / board / compulab / common / splash.c
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
9 #include <common.h>
10 #include <nand.h>
11 #include <errno.h>
12 #include <bmp_layout.h>
13 #include "common.h"
14
15 DECLARE_GLOBAL_DATA_PTR;
16
17 #ifdef CONFIG_CMD_NAND
18 static int splash_nand_read(u32 bmp_load_addr, int offset, size_t read_size)
19 {
20         return nand_read_skip_bad(&nand_info[nand_curr_device], offset,
21                                   &read_size, NULL,
22                                   nand_info[nand_curr_device].size,
23                                   (u_char *)bmp_load_addr);
24 }
25 #else
26 static int splash_nand_read(u32 bmp_load_addr, int offset, size_t read_size)
27 {
28         debug("%s: nand support not available\n", __func__);
29         return -ENOSYS;
30 }
31 #endif
32
33 static int splash_storage_read(struct splash_location *location,
34                                u32 bmp_load_addr, size_t read_size)
35 {
36         u32 offset;
37
38         if (!location)
39                 return -EINVAL;
40
41         offset = location->offset;
42         switch (location->storage) {
43         case SPLASH_STORAGE_NAND:
44                 return splash_nand_read(bmp_load_addr, offset, read_size);
45         default:
46                 printf("Unknown splash location\n");
47         }
48
49         return -EINVAL;
50 }
51
52 static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr)
53 {
54         struct bmp_header *bmp_hdr;
55         int res;
56         size_t bmp_size, bmp_header_size = sizeof(struct bmp_header);
57
58         if (bmp_load_addr + bmp_header_size >= gd->start_addr_sp)
59                 goto splash_address_too_high;
60
61         res = splash_storage_read(location, bmp_load_addr, bmp_header_size);
62         if (res < 0)
63                 return res;
64
65         bmp_hdr = (struct bmp_header *)bmp_load_addr;
66         bmp_size = le32_to_cpu(bmp_hdr->file_size);
67
68         if (bmp_load_addr + bmp_size >= gd->start_addr_sp)
69                 goto splash_address_too_high;
70
71         return splash_storage_read(location, bmp_load_addr, bmp_size);
72
73 splash_address_too_high:
74         printf("Error: splashimage address too high. Data overwrites U-Boot "
75                 "and/or placed beyond DRAM boundaries.\n");
76
77         return -EFAULT;
78 }
79
80 /**
81  * select_splash_location - return the splash location based on board support
82  *                          and env variable "splashsource".
83  *
84  * @locations:          An array of supported splash locations.
85  * @size:               Size of splash_locations array.
86  *
87  * @return: If a null set of splash locations is given, or
88  *          splashsource env variable is set to unsupported value
89  *                      return NULL.
90  *          If splashsource env variable is not defined
91  *                      return the first entry in splash_locations as default.
92  *          If splashsource env variable contains a supported value
93  *                      return the location selected by splashsource.
94  */
95 static struct splash_location *select_splash_location(
96                             struct splash_location *locations, uint size)
97 {
98         int i;
99         char *env_splashsource;
100
101         if (!locations || size == 0)
102                 return NULL;
103
104         env_splashsource = getenv("splashsource");
105         if (env_splashsource == NULL)
106                 return &locations[0];
107
108         for (i = 0; i < size; i++) {
109                 if (!strcmp(locations[i].name, env_splashsource))
110                         return &locations[i];
111         }
112
113         printf("splashsource env variable set to unsupported value\n");
114         return NULL;
115 }
116
117 int cl_splash_screen_prepare(struct splash_location *locations, uint size)
118 {
119         struct splash_location *splash_location;
120         char *env_splashimage_value;
121         u32 bmp_load_addr;
122
123         env_splashimage_value = getenv("splashimage");
124         if (env_splashimage_value == NULL)
125                 return -ENOENT;
126
127         bmp_load_addr = simple_strtoul(env_splashimage_value, 0, 16);
128         if (bmp_load_addr == 0) {
129                 printf("Error: bad splashimage address specified\n");
130                 return -EFAULT;
131         }
132
133         splash_location = select_splash_location(locations, size);
134         if (!splash_location)
135                 return -EINVAL;
136
137         return splash_load_raw(splash_location, bmp_load_addr);
138 }