blob: f230d6db42b76ea0382639623a96bfdd1f05950d [file] [log] [blame]
Igor Grinberg86ec16b2014-11-03 11:32:20 +02001/*
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>
Nikita Kiryanovf58c5092015-01-14 10:42:49 +020011#include <errno.h>
Igor Grinberg86ec16b2014-11-03 11:32:20 +020012#include <bmp_layout.h>
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020013#include "common.h"
Igor Grinberg86ec16b2014-11-03 11:32:20 +020014
15DECLARE_GLOBAL_DATA_PTR;
16
17#ifdef CONFIG_CMD_NAND
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020018static 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
26static 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
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +020033static int splash_storage_read(struct splash_location *location,
34 u32 bmp_load_addr, size_t read_size)
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020035{
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +020036 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;
Nikita Kiryanov4b4bfd62015-01-14 10:42:50 +020050}
51
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +020052static int splash_load_raw(struct splash_location *location, u32 bmp_load_addr)
Igor Grinberg86ec16b2014-11-03 11:32:20 +020053{
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
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +020061 res = splash_storage_read(location, bmp_load_addr, bmp_header_size);
Igor Grinberg86ec16b2014-11-03 11:32:20 +020062 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
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +020071 return splash_storage_read(location, bmp_load_addr, bmp_size);
Igor Grinberg86ec16b2014-11-03 11:32:20 +020072
73splash_address_too_high:
74 printf("Error: splashimage address too high. Data overwrites U-Boot "
75 "and/or placed beyond DRAM boundaries.\n");
76
Nikita Kiryanovf58c5092015-01-14 10:42:49 +020077 return -EFAULT;
Igor Grinberg86ec16b2014-11-03 11:32:20 +020078}
Igor Grinberg86ec16b2014-11-03 11:32:20 +020079
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +020080/**
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 */
95static struct splash_location *select_splash_location(
96 struct splash_location *locations, uint size)
Igor Grinberg86ec16b2014-11-03 11:32:20 +020097{
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +020098 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
117int cl_splash_screen_prepare(struct splash_location *locations, uint size)
118{
119 struct splash_location *splash_location;
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200120 char *env_splashimage_value;
121 u32 bmp_load_addr;
122
123 env_splashimage_value = getenv("splashimage");
124 if (env_splashimage_value == NULL)
Nikita Kiryanovf58c5092015-01-14 10:42:49 +0200125 return -ENOENT;
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200126
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");
Nikita Kiryanovf58c5092015-01-14 10:42:49 +0200130 return -EFAULT;
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200131 }
132
Nikita Kiryanovc2a07e32015-01-14 10:42:51 +0200133 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);
Igor Grinberg86ec16b2014-11-03 11:32:20 +0200138}