blob: 21bc4e87ed523e0c045d401d70d741b87847884a [file] [log] [blame]
Angel Pons118a9c72020-04-02 23:48:34 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Rudolpha892cde2018-04-19 14:39:07 +02002
Julius Werner98eeb962019-12-11 15:47:42 -08003#include <commonlib/bsd/compression.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +02004#include <console/console.h>
5#include <bootmem.h>
6#include <cbmem.h>
7#include <device/resource.h>
8#include <stdlib.h>
9#include <commonlib/region.h>
10#include <fit.h>
11#include <program_loading.h>
12#include <timestamp.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020013#include <string.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020014#include <lib.h>
Philipp Deppenwiese84258db2018-08-16 00:31:26 +020015#include <boardid.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020016
17/* Pack the device_tree and place it at given position. */
18static void pack_fdt(struct region *fdt, struct device_tree *dt)
19{
20 printk(BIOS_INFO, "FIT: Flattening FDT to %p\n",
21 (void *)fdt->offset);
22
23 dt_flatten(dt, (void *)fdt->offset);
24 prog_segment_loaded(fdt->offset, fdt->size, 0);
25}
26
27/**
28 * Extract a node to given regions.
29 * Returns true on error, false on success.
30 */
31static bool extract(struct region *region, struct fit_image_node *node)
32{
33 void *dst = (void *)region->offset;
34 const char *comp_name;
35 size_t true_size = 0;
36
Asami Doi02547c52019-07-24 16:04:20 +090037 if (node->size == 0) {
Julius Wernere9665952022-01-21 17:06:20 -080038 printk(BIOS_ERR, "The %s size is 0\n", node->name);
Asami Doi02547c52019-07-24 16:04:20 +090039 return true;
40 }
41
Patrick Rudolpha892cde2018-04-19 14:39:07 +020042 switch (node->compression) {
43 case CBFS_COMPRESS_NONE:
44 comp_name = "Relocating uncompressed";
45 break;
46 case CBFS_COMPRESS_LZMA:
47 comp_name = "Decompressing LZMA";
48 break;
49 case CBFS_COMPRESS_LZ4:
50 comp_name = "Decompressing LZ4";
51 break;
52 default:
Julius Wernere9665952022-01-21 17:06:20 -080053 printk(BIOS_ERR, "Unsupported compression\n");
Patrick Rudolpha892cde2018-04-19 14:39:07 +020054 return true;
55 }
56
57 printk(BIOS_INFO, "FIT: %s %s to %p\n", comp_name, node->name, dst);
58
59 switch (node->compression) {
60 case CBFS_COMPRESS_NONE:
61 memcpy(dst, node->data, node->size);
62 true_size = node->size;
63 break;
64 case CBFS_COMPRESS_LZMA:
Jakub Czapigaad6157e2022-02-15 11:50:31 +010065 timestamp_add_now(TS_ULZMA_START);
Patrick Rudolpha892cde2018-04-19 14:39:07 +020066 true_size = ulzman(node->data, node->size, dst, region->size);
Jakub Czapigaad6157e2022-02-15 11:50:31 +010067 timestamp_add_now(TS_ULZMA_END);
Patrick Rudolpha892cde2018-04-19 14:39:07 +020068 break;
69 case CBFS_COMPRESS_LZ4:
Jakub Czapigaad6157e2022-02-15 11:50:31 +010070 timestamp_add_now(TS_ULZ4F_START);
Patrick Rudolpha892cde2018-04-19 14:39:07 +020071 true_size = ulz4fn(node->data, node->size, dst, region->size);
Jakub Czapigaad6157e2022-02-15 11:50:31 +010072 timestamp_add_now(TS_ULZ4F_END);
Patrick Rudolpha892cde2018-04-19 14:39:07 +020073 break;
74 default:
75 return true;
76 }
77
78 if (!true_size) {
Julius Wernere9665952022-01-21 17:06:20 -080079 printk(BIOS_ERR, "%s decompression failed!\n",
Julius Werner2855a0c2019-05-16 13:51:31 -070080 comp_name);
Patrick Rudolpha892cde2018-04-19 14:39:07 +020081 return true;
82 }
83
Patrick Rudolpha892cde2018-04-19 14:39:07 +020084 return false;
85}
86
Julius Wernerfec42062019-05-16 16:04:19 -070087static struct device_tree *unpack_fdt(struct fit_image_node *image_node)
88{
89 void *data = image_node->data;
90
91 if (image_node->compression != CBFS_COMPRESS_NONE) {
92 /* TODO: This is an ugly heuristic for how much the size will
93 expand on decompression, fix once FIT images support storing
94 the real uncompressed size. */
95 struct region r = { .offset = 0, .size = image_node->size * 5 };
96 data = malloc(r.size);
97 r.offset = (uintptr_t)data;
98 if (!data || extract(&r, image_node))
99 return NULL;
100 }
101
102 return fdt_unflatten(data);
103}
104
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200105/**
106 * Add coreboot tables, CBMEM information and optional board specific strapping
107 * IDs to the device tree loaded via FIT.
108 */
109static void add_cb_fdt_data(struct device_tree *tree)
110{
111 u32 addr_cells = 1, size_cells = 1;
112 u64 reg_addrs[2], reg_sizes[2];
113 void *baseptr = NULL;
114 size_t size = 0;
115
116 static const char *firmware_path[] = {"firmware", NULL};
117 struct device_tree_node *firmware_node = dt_find_node(tree->root,
118 firmware_path, &addr_cells, &size_cells, 1);
119
120 /* Need to add 'ranges' to the intermediate node to make 'reg' work. */
121 dt_add_bin_prop(firmware_node, "ranges", NULL, 0);
122
123 static const char *coreboot_path[] = {"coreboot", NULL};
124 struct device_tree_node *coreboot_node = dt_find_node(firmware_node,
125 coreboot_path, &addr_cells, &size_cells, 1);
126
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200127 dt_add_string_prop(coreboot_node, "compatible", "coreboot");
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200128
129 /* Fetch CB tables from cbmem */
130 void *cbtable = cbmem_find(CBMEM_ID_CBTABLE);
131 if (!cbtable) {
132 printk(BIOS_WARNING, "FIT: No coreboot table found!\n");
133 return;
134 }
135
136 /* First 'reg' address range is the coreboot table. */
137 const struct lb_header *header = cbtable;
138 reg_addrs[0] = (uintptr_t)header;
139 reg_sizes[0] = header->header_bytes + header->table_bytes;
140
141 /* Second is the CBMEM area (which usually includes the coreboot
142 table). */
143 cbmem_get_region(&baseptr, &size);
144 if (!baseptr || size == 0) {
145 printk(BIOS_WARNING, "FIT: CBMEM pointer/size not found!\n");
146 return;
147 }
148
149 reg_addrs[1] = (uintptr_t)baseptr;
150 reg_sizes[1] = size;
151
152 dt_add_reg_prop(coreboot_node, reg_addrs, reg_sizes, 2, addr_cells,
153 size_cells);
154
155 /* Expose board ID, SKU ID, and RAM code to payload.*/
156 if (board_id() != UNDEFINED_STRAPPING_ID)
157 dt_add_u32_prop(coreboot_node, "board-id", board_id());
158
159 if (sku_id() != UNDEFINED_STRAPPING_ID)
160 dt_add_u32_prop(coreboot_node, "sku-id", sku_id());
161
162 if (ram_code() != UNDEFINED_STRAPPING_ID)
163 dt_add_u32_prop(coreboot_node, "ram-code", ram_code());
164}
165
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200166/*
167 * Parse the uImage FIT, choose a configuration and extract images.
168 */
Julius Werner965846f2021-01-11 16:07:02 -0800169void fit_payload(struct prog *payload, void *data)
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200170{
171 struct device_tree *dt = NULL;
172 struct region kernel = {0}, fdt = {0}, initrd = {0};
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200173
174 printk(BIOS_INFO, "FIT: Examine payload %s\n", payload->name);
175
176 struct fit_config_node *config = fit_load(data);
177
Julius Wernerb379f192019-05-13 16:34:16 -0700178 if (!config) {
Julius Wernere9665952022-01-21 17:06:20 -0800179 printk(BIOS_ERR, "Could not load FIT\n");
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200180 return;
181 }
182
Julius Wernerfec42062019-05-16 16:04:19 -0700183 dt = unpack_fdt(config->fdt);
Julius Wernerb379f192019-05-13 16:34:16 -0700184 if (!dt) {
Julius Wernere9665952022-01-21 17:06:20 -0800185 printk(BIOS_ERR, "Failed to unflatten the FDT.\n");
Julius Wernerb379f192019-05-13 16:34:16 -0700186 return;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200187 }
188
Julius Werner21b0b1a2019-05-16 16:12:04 -0700189 struct fit_overlay_chain *chain;
190 list_for_each(chain, config->overlays, list_node) {
191 struct device_tree *overlay = unpack_fdt(chain->overlay);
192 if (!overlay || dt_apply_overlay(dt, overlay)) {
Julius Wernere9665952022-01-21 17:06:20 -0800193 printk(BIOS_ERR, "Failed to apply overlay %s!\n",
Julius Werner21b0b1a2019-05-16 16:12:04 -0700194 chain->overlay->name);
195 }
196 }
197
Julius Wernerb379f192019-05-13 16:34:16 -0700198 dt_apply_fixups(dt);
199
200 /* Insert coreboot specific information */
201 add_cb_fdt_data(dt);
202
203 /* Update device_tree */
204#if defined(CONFIG_LINUX_COMMAND_LINE)
205 fit_update_chosen(dt, (char *)CONFIG_LINUX_COMMAND_LINE);
206#endif
207 fit_update_memory(dt);
208
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200209 /* Collect infos for fit_payload_arch */
Julius Wernerb379f192019-05-13 16:34:16 -0700210 kernel.size = config->kernel->size;
Julius Wernerf6410ba2019-07-10 13:16:40 -0700211 fdt.size = dt_flat_size(dt);
Julius Wernerb379f192019-05-13 16:34:16 -0700212 initrd.size = config->ramdisk ? config->ramdisk->size : 0;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200213
214 /* Invoke arch specific payload placement and fixups */
215 if (!fit_payload_arch(payload, config, &kernel, &fdt, &initrd)) {
Julius Wernere9665952022-01-21 17:06:20 -0800216 printk(BIOS_ERR, "Failed to find free memory region\n");
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200217 bootmem_dump_ranges();
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200218 return;
219 }
220
Julius Wernerb379f192019-05-13 16:34:16 -0700221 /* Update ramdisk location in FDT */
222 if (config->ramdisk)
223 fit_add_ramdisk(dt, (void *)initrd.offset, initrd.size);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200224
Julius Wernerb379f192019-05-13 16:34:16 -0700225 /* Repack FDT for handoff to kernel */
226 pack_fdt(&fdt, dt);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200227
Julius Wernerb379f192019-05-13 16:34:16 -0700228 if (config->ramdisk &&
229 extract(&initrd, config->ramdisk)) {
Julius Wernere9665952022-01-21 17:06:20 -0800230 printk(BIOS_ERR, "Failed to extract initrd\n");
Patrick Rudolphdfc30132018-08-09 09:08:05 +0200231 prog_set_entry(payload, NULL, NULL);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200232 return;
233 }
234
235 timestamp_add_now(TS_KERNEL_DECOMPRESSION);
236
Julius Wernerb379f192019-05-13 16:34:16 -0700237 if (extract(&kernel, config->kernel)) {
Julius Wernere9665952022-01-21 17:06:20 -0800238 printk(BIOS_ERR, "Failed to extract kernel\n");
Patrick Rudolphdfc30132018-08-09 09:08:05 +0200239 prog_set_entry(payload, NULL, NULL);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200240 return;
241 }
242
Jakub Czapigaad6157e2022-02-15 11:50:31 +0100243 timestamp_add_now(TS_KERNEL_START);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200244}