blob: b3a03a1942caac8f6ac58f98983b3aa7154ea98b [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 <cbfs.h>
4#include <commonlib/bsd/compression.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +02005#include <console/console.h>
6#include <bootmem.h>
7#include <cbmem.h>
8#include <device/resource.h>
9#include <stdlib.h>
10#include <commonlib/region.h>
11#include <fit.h>
12#include <program_loading.h>
13#include <timestamp.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020014#include <string.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020015#include <lib.h>
16#include <fit_payload.h>
Philipp Deppenwiese84258db2018-08-16 00:31:26 +020017#include <boardid.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020018
19/* Pack the device_tree and place it at given position. */
20static void pack_fdt(struct region *fdt, struct device_tree *dt)
21{
22 printk(BIOS_INFO, "FIT: Flattening FDT to %p\n",
23 (void *)fdt->offset);
24
25 dt_flatten(dt, (void *)fdt->offset);
26 prog_segment_loaded(fdt->offset, fdt->size, 0);
27}
28
29/**
30 * Extract a node to given regions.
31 * Returns true on error, false on success.
32 */
33static bool extract(struct region *region, struct fit_image_node *node)
34{
35 void *dst = (void *)region->offset;
36 const char *comp_name;
37 size_t true_size = 0;
38
Asami Doi02547c52019-07-24 16:04:20 +090039 if (node->size == 0) {
40 printk(BIOS_ERR, "ERROR: The %s size is 0\n", node->name);
41 return true;
42 }
43
Patrick Rudolpha892cde2018-04-19 14:39:07 +020044 switch (node->compression) {
45 case CBFS_COMPRESS_NONE:
46 comp_name = "Relocating uncompressed";
47 break;
48 case CBFS_COMPRESS_LZMA:
49 comp_name = "Decompressing LZMA";
50 break;
51 case CBFS_COMPRESS_LZ4:
52 comp_name = "Decompressing LZ4";
53 break;
54 default:
55 printk(BIOS_ERR, "ERROR: Unsupported compression\n");
56 return true;
57 }
58
59 printk(BIOS_INFO, "FIT: %s %s to %p\n", comp_name, node->name, dst);
60
61 switch (node->compression) {
62 case CBFS_COMPRESS_NONE:
63 memcpy(dst, node->data, node->size);
64 true_size = node->size;
65 break;
66 case CBFS_COMPRESS_LZMA:
67 timestamp_add_now(TS_START_ULZMA);
68 true_size = ulzman(node->data, node->size, dst, region->size);
69 timestamp_add_now(TS_END_ULZMA);
70 break;
71 case CBFS_COMPRESS_LZ4:
72 timestamp_add_now(TS_START_ULZ4F);
73 true_size = ulz4fn(node->data, node->size, dst, region->size);
74 timestamp_add_now(TS_END_ULZ4F);
75 break;
76 default:
77 return true;
78 }
79
80 if (!true_size) {
Julius Werner2855a0c2019-05-16 13:51:31 -070081 printk(BIOS_ERR, "ERROR: %s decompression failed!\n",
82 comp_name);
Patrick Rudolpha892cde2018-04-19 14:39:07 +020083 return true;
84 }
85
Patrick Rudolpha892cde2018-04-19 14:39:07 +020086 return false;
87}
88
Julius Wernerfec42062019-05-16 16:04:19 -070089static struct device_tree *unpack_fdt(struct fit_image_node *image_node)
90{
91 void *data = image_node->data;
92
93 if (image_node->compression != CBFS_COMPRESS_NONE) {
94 /* TODO: This is an ugly heuristic for how much the size will
95 expand on decompression, fix once FIT images support storing
96 the real uncompressed size. */
97 struct region r = { .offset = 0, .size = image_node->size * 5 };
98 data = malloc(r.size);
99 r.offset = (uintptr_t)data;
100 if (!data || extract(&r, image_node))
101 return NULL;
102 }
103
104 return fdt_unflatten(data);
105}
106
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200107/**
108 * Add coreboot tables, CBMEM information and optional board specific strapping
109 * IDs to the device tree loaded via FIT.
110 */
111static void add_cb_fdt_data(struct device_tree *tree)
112{
113 u32 addr_cells = 1, size_cells = 1;
114 u64 reg_addrs[2], reg_sizes[2];
115 void *baseptr = NULL;
116 size_t size = 0;
117
118 static const char *firmware_path[] = {"firmware", NULL};
119 struct device_tree_node *firmware_node = dt_find_node(tree->root,
120 firmware_path, &addr_cells, &size_cells, 1);
121
122 /* Need to add 'ranges' to the intermediate node to make 'reg' work. */
123 dt_add_bin_prop(firmware_node, "ranges", NULL, 0);
124
125 static const char *coreboot_path[] = {"coreboot", NULL};
126 struct device_tree_node *coreboot_node = dt_find_node(firmware_node,
127 coreboot_path, &addr_cells, &size_cells, 1);
128
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200129 dt_add_string_prop(coreboot_node, "compatible", "coreboot");
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200130
131 /* Fetch CB tables from cbmem */
132 void *cbtable = cbmem_find(CBMEM_ID_CBTABLE);
133 if (!cbtable) {
134 printk(BIOS_WARNING, "FIT: No coreboot table found!\n");
135 return;
136 }
137
138 /* First 'reg' address range is the coreboot table. */
139 const struct lb_header *header = cbtable;
140 reg_addrs[0] = (uintptr_t)header;
141 reg_sizes[0] = header->header_bytes + header->table_bytes;
142
143 /* Second is the CBMEM area (which usually includes the coreboot
144 table). */
145 cbmem_get_region(&baseptr, &size);
146 if (!baseptr || size == 0) {
147 printk(BIOS_WARNING, "FIT: CBMEM pointer/size not found!\n");
148 return;
149 }
150
151 reg_addrs[1] = (uintptr_t)baseptr;
152 reg_sizes[1] = size;
153
154 dt_add_reg_prop(coreboot_node, reg_addrs, reg_sizes, 2, addr_cells,
155 size_cells);
156
157 /* Expose board ID, SKU ID, and RAM code to payload.*/
158 if (board_id() != UNDEFINED_STRAPPING_ID)
159 dt_add_u32_prop(coreboot_node, "board-id", board_id());
160
161 if (sku_id() != UNDEFINED_STRAPPING_ID)
162 dt_add_u32_prop(coreboot_node, "sku-id", sku_id());
163
164 if (ram_code() != UNDEFINED_STRAPPING_ID)
165 dt_add_u32_prop(coreboot_node, "ram-code", ram_code());
166}
167
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200168/*
169 * Parse the uImage FIT, choose a configuration and extract images.
170 */
171void fit_payload(struct prog *payload)
172{
173 struct device_tree *dt = NULL;
174 struct region kernel = {0}, fdt = {0}, initrd = {0};
175 void *data;
176
177 data = rdev_mmap_full(prog_rdev(payload));
178
179 if (data == NULL)
180 return;
181
182 printk(BIOS_INFO, "FIT: Examine payload %s\n", payload->name);
183
184 struct fit_config_node *config = fit_load(data);
185
Julius Wernerb379f192019-05-13 16:34:16 -0700186 if (!config) {
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200187 printk(BIOS_ERR, "ERROR: Could not load FIT\n");
188 rdev_munmap(prog_rdev(payload), data);
189 return;
190 }
191
Julius Wernerfec42062019-05-16 16:04:19 -0700192 dt = unpack_fdt(config->fdt);
Julius Wernerb379f192019-05-13 16:34:16 -0700193 if (!dt) {
194 printk(BIOS_ERR, "ERROR: Failed to unflatten the FDT.\n");
195 rdev_munmap(prog_rdev(payload), data);
196 return;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200197 }
198
Julius Werner21b0b1a2019-05-16 16:12:04 -0700199 struct fit_overlay_chain *chain;
200 list_for_each(chain, config->overlays, list_node) {
201 struct device_tree *overlay = unpack_fdt(chain->overlay);
202 if (!overlay || dt_apply_overlay(dt, overlay)) {
203 printk(BIOS_ERR, "ERROR: Failed to apply overlay %s!\n",
204 chain->overlay->name);
205 }
206 }
207
Julius Wernerb379f192019-05-13 16:34:16 -0700208 dt_apply_fixups(dt);
209
210 /* Insert coreboot specific information */
211 add_cb_fdt_data(dt);
212
213 /* Update device_tree */
214#if defined(CONFIG_LINUX_COMMAND_LINE)
215 fit_update_chosen(dt, (char *)CONFIG_LINUX_COMMAND_LINE);
216#endif
217 fit_update_memory(dt);
218
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200219 /* Collect infos for fit_payload_arch */
Julius Wernerb379f192019-05-13 16:34:16 -0700220 kernel.size = config->kernel->size;
Julius Wernerf6410ba2019-07-10 13:16:40 -0700221 fdt.size = dt_flat_size(dt);
Julius Wernerb379f192019-05-13 16:34:16 -0700222 initrd.size = config->ramdisk ? config->ramdisk->size : 0;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200223
224 /* Invoke arch specific payload placement and fixups */
225 if (!fit_payload_arch(payload, config, &kernel, &fdt, &initrd)) {
226 printk(BIOS_ERR, "ERROR: Failed to find free memory region\n");
227 bootmem_dump_ranges();
228 rdev_munmap(prog_rdev(payload), data);
229 return;
230 }
231
Julius Wernerb379f192019-05-13 16:34:16 -0700232 /* Update ramdisk location in FDT */
233 if (config->ramdisk)
234 fit_add_ramdisk(dt, (void *)initrd.offset, initrd.size);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200235
Julius Wernerb379f192019-05-13 16:34:16 -0700236 /* Repack FDT for handoff to kernel */
237 pack_fdt(&fdt, dt);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200238
Julius Wernerb379f192019-05-13 16:34:16 -0700239 if (config->ramdisk &&
240 extract(&initrd, config->ramdisk)) {
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200241 printk(BIOS_ERR, "ERROR: Failed to extract initrd\n");
Patrick Rudolphdfc30132018-08-09 09:08:05 +0200242 prog_set_entry(payload, NULL, NULL);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200243 rdev_munmap(prog_rdev(payload), data);
244 return;
245 }
246
247 timestamp_add_now(TS_KERNEL_DECOMPRESSION);
248
Julius Wernerb379f192019-05-13 16:34:16 -0700249 if (extract(&kernel, config->kernel)) {
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200250 printk(BIOS_ERR, "ERROR: Failed to extract kernel\n");
Patrick Rudolphdfc30132018-08-09 09:08:05 +0200251 prog_set_entry(payload, NULL, NULL);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200252 rdev_munmap(prog_rdev(payload), data);
253 return;
254 }
255
256 timestamp_add_now(TS_START_KERNEL);
257
258 rdev_munmap(prog_rdev(payload), data);
259}