blob: 83e9b8e90128e14a0a08b1993a5ccd6e08723b91 [file] [log] [blame]
Patrick Rudolpha892cde2018-04-19 14:39:07 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2003-2004 Eric Biederman
5 * Copyright (C) 2005-2010 coresystems GmbH
6 * Copyright (C) 2014 Google Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
Julius Werner98eeb962019-12-11 15:47:42 -080018#include <cbfs.h>
19#include <commonlib/bsd/compression.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020020#include <console/console.h>
21#include <bootmem.h>
22#include <cbmem.h>
23#include <device/resource.h>
24#include <stdlib.h>
25#include <commonlib/region.h>
26#include <fit.h>
27#include <program_loading.h>
28#include <timestamp.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020029#include <string.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020030#include <lib.h>
31#include <fit_payload.h>
Philipp Deppenwiese84258db2018-08-16 00:31:26 +020032#include <boardid.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020033
34/* Pack the device_tree and place it at given position. */
35static void pack_fdt(struct region *fdt, struct device_tree *dt)
36{
37 printk(BIOS_INFO, "FIT: Flattening FDT to %p\n",
38 (void *)fdt->offset);
39
40 dt_flatten(dt, (void *)fdt->offset);
41 prog_segment_loaded(fdt->offset, fdt->size, 0);
42}
43
44/**
45 * Extract a node to given regions.
46 * Returns true on error, false on success.
47 */
48static bool extract(struct region *region, struct fit_image_node *node)
49{
50 void *dst = (void *)region->offset;
51 const char *comp_name;
52 size_t true_size = 0;
53
Asami Doi02547c52019-07-24 16:04:20 +090054 if (node->size == 0) {
55 printk(BIOS_ERR, "ERROR: The %s size is 0\n", node->name);
56 return true;
57 }
58
Patrick Rudolpha892cde2018-04-19 14:39:07 +020059 switch (node->compression) {
60 case CBFS_COMPRESS_NONE:
61 comp_name = "Relocating uncompressed";
62 break;
63 case CBFS_COMPRESS_LZMA:
64 comp_name = "Decompressing LZMA";
65 break;
66 case CBFS_COMPRESS_LZ4:
67 comp_name = "Decompressing LZ4";
68 break;
69 default:
70 printk(BIOS_ERR, "ERROR: Unsupported compression\n");
71 return true;
72 }
73
74 printk(BIOS_INFO, "FIT: %s %s to %p\n", comp_name, node->name, dst);
75
76 switch (node->compression) {
77 case CBFS_COMPRESS_NONE:
78 memcpy(dst, node->data, node->size);
79 true_size = node->size;
80 break;
81 case CBFS_COMPRESS_LZMA:
82 timestamp_add_now(TS_START_ULZMA);
83 true_size = ulzman(node->data, node->size, dst, region->size);
84 timestamp_add_now(TS_END_ULZMA);
85 break;
86 case CBFS_COMPRESS_LZ4:
87 timestamp_add_now(TS_START_ULZ4F);
88 true_size = ulz4fn(node->data, node->size, dst, region->size);
89 timestamp_add_now(TS_END_ULZ4F);
90 break;
91 default:
92 return true;
93 }
94
95 if (!true_size) {
Julius Werner2855a0c2019-05-16 13:51:31 -070096 printk(BIOS_ERR, "ERROR: %s decompression failed!\n",
97 comp_name);
Patrick Rudolpha892cde2018-04-19 14:39:07 +020098 return true;
99 }
100
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200101 return false;
102}
103
Julius Wernerfec42062019-05-16 16:04:19 -0700104static struct device_tree *unpack_fdt(struct fit_image_node *image_node)
105{
106 void *data = image_node->data;
107
108 if (image_node->compression != CBFS_COMPRESS_NONE) {
109 /* TODO: This is an ugly heuristic for how much the size will
110 expand on decompression, fix once FIT images support storing
111 the real uncompressed size. */
112 struct region r = { .offset = 0, .size = image_node->size * 5 };
113 data = malloc(r.size);
114 r.offset = (uintptr_t)data;
115 if (!data || extract(&r, image_node))
116 return NULL;
117 }
118
119 return fdt_unflatten(data);
120}
121
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200122/**
123 * Add coreboot tables, CBMEM information and optional board specific strapping
124 * IDs to the device tree loaded via FIT.
125 */
126static void add_cb_fdt_data(struct device_tree *tree)
127{
128 u32 addr_cells = 1, size_cells = 1;
129 u64 reg_addrs[2], reg_sizes[2];
130 void *baseptr = NULL;
131 size_t size = 0;
132
133 static const char *firmware_path[] = {"firmware", NULL};
134 struct device_tree_node *firmware_node = dt_find_node(tree->root,
135 firmware_path, &addr_cells, &size_cells, 1);
136
137 /* Need to add 'ranges' to the intermediate node to make 'reg' work. */
138 dt_add_bin_prop(firmware_node, "ranges", NULL, 0);
139
140 static const char *coreboot_path[] = {"coreboot", NULL};
141 struct device_tree_node *coreboot_node = dt_find_node(firmware_node,
142 coreboot_path, &addr_cells, &size_cells, 1);
143
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200144 dt_add_string_prop(coreboot_node, "compatible", "coreboot");
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200145
146 /* Fetch CB tables from cbmem */
147 void *cbtable = cbmem_find(CBMEM_ID_CBTABLE);
148 if (!cbtable) {
149 printk(BIOS_WARNING, "FIT: No coreboot table found!\n");
150 return;
151 }
152
153 /* First 'reg' address range is the coreboot table. */
154 const struct lb_header *header = cbtable;
155 reg_addrs[0] = (uintptr_t)header;
156 reg_sizes[0] = header->header_bytes + header->table_bytes;
157
158 /* Second is the CBMEM area (which usually includes the coreboot
159 table). */
160 cbmem_get_region(&baseptr, &size);
161 if (!baseptr || size == 0) {
162 printk(BIOS_WARNING, "FIT: CBMEM pointer/size not found!\n");
163 return;
164 }
165
166 reg_addrs[1] = (uintptr_t)baseptr;
167 reg_sizes[1] = size;
168
169 dt_add_reg_prop(coreboot_node, reg_addrs, reg_sizes, 2, addr_cells,
170 size_cells);
171
172 /* Expose board ID, SKU ID, and RAM code to payload.*/
173 if (board_id() != UNDEFINED_STRAPPING_ID)
174 dt_add_u32_prop(coreboot_node, "board-id", board_id());
175
176 if (sku_id() != UNDEFINED_STRAPPING_ID)
177 dt_add_u32_prop(coreboot_node, "sku-id", sku_id());
178
179 if (ram_code() != UNDEFINED_STRAPPING_ID)
180 dt_add_u32_prop(coreboot_node, "ram-code", ram_code());
181}
182
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200183/*
184 * Parse the uImage FIT, choose a configuration and extract images.
185 */
186void fit_payload(struct prog *payload)
187{
188 struct device_tree *dt = NULL;
189 struct region kernel = {0}, fdt = {0}, initrd = {0};
190 void *data;
191
192 data = rdev_mmap_full(prog_rdev(payload));
193
194 if (data == NULL)
195 return;
196
197 printk(BIOS_INFO, "FIT: Examine payload %s\n", payload->name);
198
199 struct fit_config_node *config = fit_load(data);
200
Julius Wernerb379f192019-05-13 16:34:16 -0700201 if (!config) {
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200202 printk(BIOS_ERR, "ERROR: Could not load FIT\n");
203 rdev_munmap(prog_rdev(payload), data);
204 return;
205 }
206
Julius Wernerfec42062019-05-16 16:04:19 -0700207 dt = unpack_fdt(config->fdt);
Julius Wernerb379f192019-05-13 16:34:16 -0700208 if (!dt) {
209 printk(BIOS_ERR, "ERROR: Failed to unflatten the FDT.\n");
210 rdev_munmap(prog_rdev(payload), data);
211 return;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200212 }
213
Julius Werner21b0b1a2019-05-16 16:12:04 -0700214 struct fit_overlay_chain *chain;
215 list_for_each(chain, config->overlays, list_node) {
216 struct device_tree *overlay = unpack_fdt(chain->overlay);
217 if (!overlay || dt_apply_overlay(dt, overlay)) {
218 printk(BIOS_ERR, "ERROR: Failed to apply overlay %s!\n",
219 chain->overlay->name);
220 }
221 }
222
Julius Wernerb379f192019-05-13 16:34:16 -0700223 dt_apply_fixups(dt);
224
225 /* Insert coreboot specific information */
226 add_cb_fdt_data(dt);
227
228 /* Update device_tree */
229#if defined(CONFIG_LINUX_COMMAND_LINE)
230 fit_update_chosen(dt, (char *)CONFIG_LINUX_COMMAND_LINE);
231#endif
232 fit_update_memory(dt);
233
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200234 /* Collect infos for fit_payload_arch */
Julius Wernerb379f192019-05-13 16:34:16 -0700235 kernel.size = config->kernel->size;
Julius Wernerf6410ba2019-07-10 13:16:40 -0700236 fdt.size = dt_flat_size(dt);
Julius Wernerb379f192019-05-13 16:34:16 -0700237 initrd.size = config->ramdisk ? config->ramdisk->size : 0;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200238
239 /* Invoke arch specific payload placement and fixups */
240 if (!fit_payload_arch(payload, config, &kernel, &fdt, &initrd)) {
241 printk(BIOS_ERR, "ERROR: Failed to find free memory region\n");
242 bootmem_dump_ranges();
243 rdev_munmap(prog_rdev(payload), data);
244 return;
245 }
246
Julius Wernerb379f192019-05-13 16:34:16 -0700247 /* Update ramdisk location in FDT */
248 if (config->ramdisk)
249 fit_add_ramdisk(dt, (void *)initrd.offset, initrd.size);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200250
Julius Wernerb379f192019-05-13 16:34:16 -0700251 /* Repack FDT for handoff to kernel */
252 pack_fdt(&fdt, dt);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200253
Julius Wernerb379f192019-05-13 16:34:16 -0700254 if (config->ramdisk &&
255 extract(&initrd, config->ramdisk)) {
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200256 printk(BIOS_ERR, "ERROR: Failed to extract initrd\n");
Patrick Rudolphdfc30132018-08-09 09:08:05 +0200257 prog_set_entry(payload, NULL, NULL);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200258 rdev_munmap(prog_rdev(payload), data);
259 return;
260 }
261
262 timestamp_add_now(TS_KERNEL_DECOMPRESSION);
263
Julius Wernerb379f192019-05-13 16:34:16 -0700264 if (extract(&kernel, config->kernel)) {
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200265 printk(BIOS_ERR, "ERROR: Failed to extract kernel\n");
Patrick Rudolphdfc30132018-08-09 09:08:05 +0200266 prog_set_entry(payload, NULL, NULL);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200267 rdev_munmap(prog_rdev(payload), data);
268 return;
269 }
270
271 timestamp_add_now(TS_START_KERNEL);
272
273 rdev_munmap(prog_rdev(payload), data);
274}