blob: 9db083e21d7e0ab0a235c03b5211128efa1dc724 [file] [log] [blame]
Patrick Georgiafd4c872020-05-05 23:43:18 +02001/* Taken from depthcharge: src/boot/fit.c */
Patrick Georgiac959032020-05-05 22:49:26 +02002/* SPDX-License-Identifier: GPL-2.0-or-later */
Patrick Rudolpha45e9f82018-04-19 14:39:07 +02003
4#include <assert.h>
Elyes HAOUAS351e3e52019-04-05 18:11:19 +02005#include <console/console.h>
Joel Kitching393c71c2019-06-16 16:09:42 +08006#include <ctype.h>
Patrick Rudolpha45e9f82018-04-19 14:39:07 +02007#include <endian.h>
Kyösti Mälkkib78e4622022-12-15 22:12:29 +02008#include <identity.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +02009#include <bootmem.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020010#include <string.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020011#include <program_loading.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020012#include <memrange.h>
13#include <fit.h>
14#include <boardid.h>
Julius Werner9636a102019-05-03 17:36:43 -070015#include <commonlib/stdlib.h>
Elyes HAOUAS93a195c2021-12-31 18:46:13 +010016#include <types.h>
Patrick Rudolpha45e9f82018-04-19 14:39:07 +020017
Patrick Rudolpha892cde2018-04-19 14:39:07 +020018static struct list_node image_nodes;
19static struct list_node config_nodes;
20static struct list_node compat_strings;
Patrick Rudolpha45e9f82018-04-19 14:39:07 +020021
Patrick Rudolpha892cde2018-04-19 14:39:07 +020022struct compat_string_entry {
23 const char *compat_string;
24 struct list_node list_node;
25};
Patrick Rudolpha45e9f82018-04-19 14:39:07 +020026
Jonathan Neuschäferbbaae952018-12-12 01:12:13 +010027/* Convert string to lowercase and replace '_' and spaces with '-'. */
Patrick Rudolpha892cde2018-04-19 14:39:07 +020028static char *clean_compat_string(char *str)
Patrick Rudolpha45e9f82018-04-19 14:39:07 +020029{
Patrick Rudolpha892cde2018-04-19 14:39:07 +020030 for (size_t i = 0; i < strlen(str); i++) {
31 str[i] = tolower(str[i]);
Jonathan Neuschäferbbaae952018-12-12 01:12:13 +010032 if (str[i] == '_' || str[i] == ' ')
Patrick Rudolpha892cde2018-04-19 14:39:07 +020033 str[i] = '-';
34 }
35
36 return str;
Patrick Rudolpha45e9f82018-04-19 14:39:07 +020037}
38
Patrick Rudolpha892cde2018-04-19 14:39:07 +020039static void fit_add_default_compat_strings(void)
Patrick Rudolpha45e9f82018-04-19 14:39:07 +020040{
Patrick Rudolpha892cde2018-04-19 14:39:07 +020041 char compat_string[80] = {};
Patrick Rudolpha45e9f82018-04-19 14:39:07 +020042
Patrick Rudolpha892cde2018-04-19 14:39:07 +020043 if ((board_id() != UNDEFINED_STRAPPING_ID) &&
44 (sku_id() != UNDEFINED_STRAPPING_ID)) {
45 snprintf(compat_string, sizeof(compat_string),
Kyösti Mälkkib78e4622022-12-15 22:12:29 +020046 "%s,%s-rev%u-sku%u", mainboard_vendor, mainboard_part_number,
47 board_id(), sku_id());
Patrick Rudolpha45e9f82018-04-19 14:39:07 +020048
Patrick Rudolpha892cde2018-04-19 14:39:07 +020049 fit_add_compat_string(compat_string);
50 }
Patrick Rudolpha45e9f82018-04-19 14:39:07 +020051
Patrick Rudolpha892cde2018-04-19 14:39:07 +020052 if (board_id() != UNDEFINED_STRAPPING_ID) {
53 snprintf(compat_string, sizeof(compat_string), "%s,%s-rev%u",
Kyösti Mälkkib78e4622022-12-15 22:12:29 +020054 mainboard_vendor, mainboard_part_number, board_id());
Patrick Rudolpha45e9f82018-04-19 14:39:07 +020055
Patrick Rudolpha892cde2018-04-19 14:39:07 +020056 fit_add_compat_string(compat_string);
57 }
Patrick Rudolpha45e9f82018-04-19 14:39:07 +020058
Julius Werner11217de2020-05-21 10:39:58 -070059 if (sku_id() != UNDEFINED_STRAPPING_ID) {
60 snprintf(compat_string, sizeof(compat_string), "%s,%s-sku%u",
Kyösti Mälkkib78e4622022-12-15 22:12:29 +020061 mainboard_vendor, mainboard_part_number, sku_id());
Julius Werner11217de2020-05-21 10:39:58 -070062
63 fit_add_compat_string(compat_string);
64 }
65
Patrick Rudolpha892cde2018-04-19 14:39:07 +020066 snprintf(compat_string, sizeof(compat_string), "%s,%s",
Kyösti Mälkkib78e4622022-12-15 22:12:29 +020067 mainboard_vendor, mainboard_part_number);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +020068
Patrick Rudolpha892cde2018-04-19 14:39:07 +020069 fit_add_compat_string(compat_string);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +020070}
71
Julius Wernerb379f192019-05-13 16:34:16 -070072static struct fit_image_node *find_image(const char *name)
73{
74 struct fit_image_node *image;
75 list_for_each(image, image_nodes, list_node) {
76 if (!strcmp(image->name, name))
77 return image;
78 }
Julius Wernere9665952022-01-21 17:06:20 -080079 printk(BIOS_ERR, "Cannot find image node %s!\n", name);
Julius Wernerb379f192019-05-13 16:34:16 -070080 return NULL;
81}
82
Julius Werner21b0b1a2019-05-16 16:12:04 -070083static struct fit_image_node *find_image_with_overlays(const char *name,
84 int bytes, struct list_node *prev)
85{
86 struct fit_image_node *base = find_image(name);
87 if (!base)
88 return NULL;
89
90 int len = strnlen(name, bytes) + 1;
91 bytes -= len;
92 name += len;
93 while (bytes > 0) {
94 struct fit_overlay_chain *next = xzalloc(sizeof(*next));
95 next->overlay = find_image(name);
96 if (!next->overlay)
97 return NULL;
98 list_insert_after(&next->list_node, prev);
99 prev = &next->list_node;
100 len = strnlen(name, bytes) + 1;
101 bytes -= len;
102 name += len;
103 }
104
105 return base;
106}
107
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200108static void image_node(struct device_tree_node *node)
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200109{
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200110 struct fit_image_node *image = xzalloc(sizeof(*image));
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200111
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200112 image->compression = CBFS_COMPRESS_NONE;
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200113 image->name = node->name;
114
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200115 struct device_tree_property *prop;
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200116 list_for_each(prop, node->properties, list_node) {
117 if (!strcmp("data", prop->prop.name)) {
118 image->data = prop->prop.data;
119 image->size = prop->prop.size;
120 } else if (!strcmp("compression", prop->prop.name)) {
121 if (!strcmp("none", prop->prop.data))
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200122 image->compression = CBFS_COMPRESS_NONE;
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200123 else if (!strcmp("lzma", prop->prop.data))
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200124 image->compression = CBFS_COMPRESS_LZMA;
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200125 else if (!strcmp("lz4", prop->prop.data))
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200126 image->compression = CBFS_COMPRESS_LZ4;
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200127 else
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200128 image->compression = -1;
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200129 }
130 }
131
132 list_insert_after(&image->list_node, &image_nodes);
133}
134
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200135static void config_node(struct device_tree_node *node)
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200136{
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200137 struct fit_config_node *config = xzalloc(sizeof(*config));
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200138 config->name = node->name;
139
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200140 struct device_tree_property *prop;
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200141 list_for_each(prop, node->properties, list_node) {
142 if (!strcmp("kernel", prop->prop.name))
Julius Wernerb379f192019-05-13 16:34:16 -0700143 config->kernel = find_image(prop->prop.data);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200144 else if (!strcmp("fdt", prop->prop.name))
Julius Werner21b0b1a2019-05-16 16:12:04 -0700145 config->fdt = find_image_with_overlays(prop->prop.data,
146 prop->prop.size, &config->overlays);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200147 else if (!strcmp("ramdisk", prop->prop.name))
Julius Wernerb379f192019-05-13 16:34:16 -0700148 config->ramdisk = find_image(prop->prop.data);
Julius Wernerfec42062019-05-16 16:04:19 -0700149 else if (!strcmp("compatible", prop->prop.name))
150 config->compat = prop->prop;
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200151 }
152
153 list_insert_after(&config->list_node, &config_nodes);
154}
155
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200156static void fit_unpack(struct device_tree *tree, const char **default_config)
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200157{
Julius Wernerb379f192019-05-13 16:34:16 -0700158 struct device_tree_node *child;
159 struct device_tree_node *images = dt_find_node_by_path(tree, "/images",
160 NULL, NULL, 0);
161 if (images)
162 list_for_each(child, images->children, list_node)
163 image_node(child);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200164
Julius Wernerb379f192019-05-13 16:34:16 -0700165 struct device_tree_node *configs = dt_find_node_by_path(tree,
166 "/configurations", NULL, NULL, 0);
167 if (configs) {
168 *default_config = dt_find_string_prop(configs, "default");
169 list_for_each(child, configs->children, list_node)
170 config_node(child);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200171 }
172}
173
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200174static int fdt_find_compat(const void *blob, uint32_t start_offset,
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200175 struct fdt_property *prop)
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200176{
177 int offset = start_offset;
178 int size;
179
180 size = fdt_node_name(blob, offset, NULL);
181 if (!size)
182 return -1;
183 offset += size;
184
185 while ((size = fdt_next_property(blob, offset, prop))) {
186 if (!strcmp("compatible", prop->name))
187 return 0;
188
189 offset += size;
190 }
191
192 prop->name = NULL;
193 return -1;
194}
195
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200196static int fit_check_compat(struct fdt_property *compat_prop,
197 const char *compat_name)
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200198{
199 int bytes = compat_prop->size;
200 const char *compat_str = compat_prop->data;
201
202 for (int pos = 0; bytes && compat_str[0]; pos++) {
203 if (!strncmp(compat_str, compat_name, bytes))
204 return pos;
205 int len = strlen(compat_str) + 1;
206 compat_str += len;
207 bytes -= len;
208 }
209 return -1;
210}
211
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200212void fit_update_chosen(struct device_tree *tree, const char *cmd_line)
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200213{
214 const char *path[] = { "chosen", NULL };
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200215 struct device_tree_node *node;
216 node = dt_find_node(tree->root, path, NULL, NULL, 1);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200217
218 dt_add_string_prop(node, "bootargs", cmd_line);
219}
220
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200221void fit_add_ramdisk(struct device_tree *tree, void *ramdisk_addr,
222 size_t ramdisk_size)
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200223{
224 const char *path[] = { "chosen", NULL };
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200225 struct device_tree_node *node;
226 node = dt_find_node(tree->root, path, NULL, NULL, 1);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200227
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200228 u64 start = (uintptr_t)ramdisk_addr;
229 u64 end = start + ramdisk_size;
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200230
Patrick Rudolph3fca4ed2018-08-10 10:12:35 +0200231 dt_add_u64_prop(node, "linux,initrd-start", start);
232 dt_add_u64_prop(node, "linux,initrd-end", end);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200233}
234
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200235static void update_reserve_map(uint64_t start, uint64_t end,
236 struct device_tree *tree)
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200237{
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200238 struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry));
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200239
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200240 entry->start = start;
241 entry->size = end - start;
242
243 list_insert_after(&entry->list_node, &tree->reserve_map);
244}
245
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200246struct entry_params {
Martin Roth38ddbfb2019-10-23 21:41:00 -0600247 unsigned int addr_cells;
248 unsigned int size_cells;
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200249 void *data;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200250};
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200251
Martin Roth38ddbfb2019-10-23 21:41:00 -0600252static uint64_t max_range(unsigned int size_cells)
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200253{
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200254 /*
255 * Split up ranges who's sizes are too large to fit in #size-cells.
256 * The largest value we can store isn't a power of two, so we'll round
257 * down to make the math easier.
258 */
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200259 return 0x1ULL << (size_cells * 32 - 1);
260}
261
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200262static void update_mem_property(u64 start, u64 end, struct entry_params *params)
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200263{
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200264 u8 *data = (u8 *)params->data;
265 u64 full_size = end - start;
266 while (full_size) {
267 const u64 max_size = max_range(params->size_cells);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200268 const u64 size = MIN(max_size, full_size);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200269
270 dt_write_int(data, start, params->addr_cells * sizeof(u32));
271 data += params->addr_cells * sizeof(uint32_t);
272 start += size;
273
274 dt_write_int(data, size, params->size_cells * sizeof(u32));
275 data += params->size_cells * sizeof(uint32_t);
276 full_size -= size;
277 }
278 params->data = data;
279}
280
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200281struct mem_map {
282 struct memranges mem;
283 struct memranges reserved;
284};
285
286static bool walk_memory_table(const struct range_entry *r, void *arg)
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200287{
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200288 struct mem_map *arg_map = arg;
Arthur Heymans1793eb42022-07-01 15:45:56 +0200289 struct memranges *ranges;
290 enum bootmem_type tag;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200291
Arthur Heymans1793eb42022-07-01 15:45:56 +0200292 ranges = range_entry_tag(r) == BM_MEM_RAM ? &arg_map->mem : &arg_map->reserved;
293 tag = range_entry_tag(r) == BM_MEM_RAM ? BM_MEM_RAM : BM_MEM_RESERVED;
294 memranges_insert(ranges, range_entry_base(r), range_entry_size(r), tag);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200295 return true;
296}
297
298void fit_add_compat_string(const char *str)
299{
300 struct compat_string_entry *compat_node;
301
302 compat_node = xzalloc(sizeof(*compat_node));
303 compat_node->compat_string = strdup(str);
304
305 clean_compat_string((char *)compat_node->compat_string);
306
307 list_insert_after(&compat_node->list_node, &compat_strings);
308}
309
310void fit_update_memory(struct device_tree *tree)
311{
312 const struct range_entry *r;
313 struct device_tree_node *node;
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200314 u32 addr_cells = 1, size_cells = 1;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200315 struct mem_map map;
316
317 printk(BIOS_INFO, "FIT: Updating devicetree memory entries\n");
318
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200319 dt_read_cell_props(tree->root, &addr_cells, &size_cells);
320
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200321 /*
322 * First remove all existing device_type="memory" nodes, then add ours.
323 */
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200324 list_for_each(node, tree->root->children, list_node) {
325 const char *devtype = dt_find_string_prop(node, "device_type");
326 if (devtype && !strcmp(devtype, "memory"))
327 list_remove(&node->list_node);
328 }
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200329
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200330 node = xzalloc(sizeof(*node));
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200331
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200332 node->name = "memory";
333 list_insert_after(&node->list_node, &tree->root->children);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200334 dt_add_string_prop(node, "device_type", (char *)"memory");
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200335
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200336 memranges_init_empty(&map.mem, NULL, 0);
337 memranges_init_empty(&map.reserved, NULL, 0);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200338
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200339 bootmem_walk_os_mem(walk_memory_table, &map);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200340
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200341 /* CBMEM regions are both carved out and explicitly reserved. */
342 memranges_each_entry(r, &map.reserved) {
343 update_reserve_map(range_entry_base(r), range_entry_end(r),
344 tree);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200345 }
346
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200347 /*
348 * Count the amount of 'reg' entries we need (account for size limits).
349 */
350 size_t count = 0;
351 memranges_each_entry(r, &map.mem) {
352 uint64_t size = range_entry_size(r);
353 uint64_t max_size = max_range(size_cells);
354 count += DIV_ROUND_UP(size, max_size);
355 }
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200356
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200357 /* Allocate the right amount of space and fill up the entries. */
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200358 size_t length = count * (addr_cells + size_cells) * sizeof(u32);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200359
360 void *data = xzalloc(length);
361
362 struct entry_params add_params = { addr_cells, size_cells, data };
363 memranges_each_entry(r, &map.mem) {
364 update_mem_property(range_entry_base(r), range_entry_end(r),
365 &add_params);
366 }
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200367 assert(add_params.data - data == length);
368
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200369 /* Assemble the final property and add it to the device tree. */
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200370 dt_add_bin_prop(node, "reg", data, length);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200371
372 memranges_teardown(&map.mem);
373 memranges_teardown(&map.reserved);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200374}
375
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200376/*
377 * Finds a compat string and updates the compat position and rank.
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200378 * @param config The current config node to operate on
Julius Wernerb379f192019-05-13 16:34:16 -0700379 * @return 0 if compat updated, -1 if this FDT cannot be used.
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200380 */
Julius Wernerb379f192019-05-13 16:34:16 -0700381static int fit_update_compat(struct fit_config_node *config)
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200382{
Julius Werner23df4772019-05-17 22:50:18 -0700383 /* If there was no "compatible" property in config node, this is a
384 legacy FIT image. Must extract compat prop from FDT itself. */
Julius Wernerfec42062019-05-16 16:04:19 -0700385 if (!config->compat.name) {
386 void *fdt_blob = config->fdt->data;
387 const struct fdt_header *fdt_header = fdt_blob;
388 uint32_t fdt_offset = be32_to_cpu(fdt_header->structure_offset);
Julius Wernerb379f192019-05-13 16:34:16 -0700389
Julius Wernerfec42062019-05-16 16:04:19 -0700390 if (config->fdt->compression != CBFS_COMPRESS_NONE) {
Julius Wernere9665952022-01-21 17:06:20 -0800391 printk(BIOS_ERR, "config %s has a compressed FDT without "
Julius Wernerfec42062019-05-16 16:04:19 -0700392 "external compatible property, skipping.\n",
393 config->name);
394 return -1;
395 }
396
Julius Werner23df4772019-05-17 22:50:18 -0700397 /* FDT overlays are not supported in legacy FIT images. */
Julius Werner21b0b1a2019-05-16 16:12:04 -0700398 if (config->overlays.next) {
Julius Wernere9665952022-01-21 17:06:20 -0800399 printk(BIOS_ERR, "config %s has overlay but no compat!\n",
Julius Werner21b0b1a2019-05-16 16:12:04 -0700400 config->name);
401 return -1;
402 }
403
Julius Wernerfec42062019-05-16 16:04:19 -0700404 if (fdt_find_compat(fdt_blob, fdt_offset, &config->compat)) {
Julius Wernere9665952022-01-21 17:06:20 -0800405 printk(BIOS_ERR, "Can't find compat string in FDT %s "
Julius Wernerfec42062019-05-16 16:04:19 -0700406 "for config %s, skipping.\n",
407 config->fdt->name, config->name);
408 return -1;
409 }
410 }
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200411
Julius Wernerb379f192019-05-13 16:34:16 -0700412 config->compat_pos = -1;
413 config->compat_rank = -1;
Julius Wernerfec42062019-05-16 16:04:19 -0700414 size_t i = 0;
415 struct compat_string_entry *compat_node;
416 list_for_each(compat_node, compat_strings, list_node) {
417 int pos = fit_check_compat(&config->compat,
418 compat_node->compat_string);
419 if (pos >= 0) {
420 config->compat_pos = pos;
421 config->compat_rank = i;
422 config->compat_string =
423 compat_node->compat_string;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200424 }
Julius Wernerfec42062019-05-16 16:04:19 -0700425 i++;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200426 }
Julius Wernerb379f192019-05-13 16:34:16 -0700427
428 return 0;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200429}
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200430
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200431struct fit_config_node *fit_load(void *fit)
432{
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200433 struct fit_image_node *image;
434 struct fit_config_node *config;
435 struct compat_string_entry *compat_node;
Julius Werner21b0b1a2019-05-16 16:12:04 -0700436 struct fit_overlay_chain *overlay_chain;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200437
438 printk(BIOS_DEBUG, "FIT: Loading FIT from %p\n", fit);
439
Julius Werner73eaec82019-05-03 17:58:07 -0700440 struct device_tree *tree = fdt_unflatten(fit);
441 if (!tree) {
Julius Wernere9665952022-01-21 17:06:20 -0800442 printk(BIOS_ERR, "Failed to unflatten FIT image!\n");
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200443 return NULL;
444 }
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200445
446 const char *default_config_name = NULL;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200447 struct fit_config_node *default_config = NULL;
448 struct fit_config_node *compat_config = NULL;
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200449
450 fit_unpack(tree, &default_config_name);
451
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200452 /* List the images we found. */
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200453 list_for_each(image, image_nodes, list_node)
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200454 printk(BIOS_DEBUG, "FIT: Image %s has %d bytes.\n", image->name,
455 image->size);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200456
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200457 fit_add_default_compat_strings();
458
459 printk(BIOS_DEBUG, "FIT: Compat preference "
460 "(lowest to highest priority) :");
461
462 list_for_each(compat_node, compat_strings, list_node) {
463 printk(BIOS_DEBUG, " %s", compat_node->compat_string);
464 }
465 printk(BIOS_DEBUG, "\n");
466 /* Process and list the configs. */
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200467 list_for_each(config, config_nodes, list_node) {
Julius Wernerb379f192019-05-13 16:34:16 -0700468 if (!config->kernel) {
Julius Wernere9665952022-01-21 17:06:20 -0800469 printk(BIOS_ERR, "config %s has no kernel, skipping.\n",
Julius Wernerb379f192019-05-13 16:34:16 -0700470 config->name);
471 continue;
472 }
473 if (!config->fdt) {
Julius Wernere9665952022-01-21 17:06:20 -0800474 printk(BIOS_ERR, "config %s has no FDT, skipping.\n",
Julius Wernerb379f192019-05-13 16:34:16 -0700475 config->name);
476 continue;
477 }
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200478
Julius Wernerb379f192019-05-13 16:34:16 -0700479 if (config->ramdisk &&
480 config->ramdisk->compression < 0) {
Julius Wernere9665952022-01-21 17:06:20 -0800481 printk(BIOS_WARNING, "Ramdisk is compressed with "
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200482 "an unsupported algorithm, discarding config %s."
483 "\n", config->name);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200484 continue;
485 }
486
Julius Wernerb379f192019-05-13 16:34:16 -0700487 if (fit_update_compat(config))
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200488 continue;
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200489
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200490 printk(BIOS_DEBUG, "FIT: config %s", config->name);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200491 if (default_config_name &&
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200492 !strcmp(config->name, default_config_name)) {
493 printk(BIOS_DEBUG, " (default)");
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200494 default_config = config;
495 }
Julius Wernerb379f192019-05-13 16:34:16 -0700496 printk(BIOS_DEBUG, ", kernel %s", config->kernel->name);
497 printk(BIOS_DEBUG, ", fdt %s", config->fdt->name);
Julius Werner21b0b1a2019-05-16 16:12:04 -0700498 list_for_each(overlay_chain, config->overlays, list_node)
499 printk(BIOS_DEBUG, " %s", overlay_chain->overlay->name);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200500 if (config->ramdisk)
Julius Wernerb379f192019-05-13 16:34:16 -0700501 printk(BIOS_DEBUG, ", ramdisk %s",
502 config->ramdisk->name);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200503 if (config->compat.name) {
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200504 printk(BIOS_DEBUG, ", compat");
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200505 int bytes = config->compat.size;
506 const char *compat_str = config->compat.data;
507 for (int pos = 0; bytes && compat_str[0]; pos++) {
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200508 printk(BIOS_DEBUG, " %s", compat_str);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200509 if (pos == config->compat_pos)
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200510 printk(BIOS_DEBUG, " (match)");
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200511 int len = strlen(compat_str) + 1;
512 compat_str += len;
513 bytes -= len;
514 }
515
516 if (config->compat_rank >= 0 && (!compat_config ||
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200517 config->compat_rank > compat_config->compat_rank))
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200518 compat_config = config;
519 }
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200520 printk(BIOS_DEBUG, "\n");
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200521 }
522
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200523 struct fit_config_node *to_boot = NULL;
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200524 if (compat_config) {
525 to_boot = compat_config;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200526 printk(BIOS_INFO, "FIT: Choosing best match %s for compat "
527 "%s.\n", to_boot->name, to_boot->compat_string);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200528 } else if (default_config) {
529 to_boot = default_config;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200530 printk(BIOS_INFO, "FIT: No match, choosing default %s.\n",
531 to_boot->name);
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200532 } else {
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200533 printk(BIOS_ERR, "FIT: No compatible or default configs. "
534 "Giving up.\n");
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200535 return NULL;
536 }
537
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200538 return to_boot;
Patrick Rudolpha45e9f82018-04-19 14:39:07 +0200539}