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