blob: a4d370540efe54a225cb8fbdbf896e559a711958 [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
18#include <console/console.h>
19#include <bootmem.h>
20#include <cbmem.h>
21#include <device/resource.h>
22#include <stdlib.h>
23#include <commonlib/region.h>
24#include <fit.h>
25#include <program_loading.h>
26#include <timestamp.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020027#include <string.h>
Elyes HAOUAS28b38cd2019-03-18 11:30:08 +010028#include <commonlib/cbfs_serialized.h>
Patrick Rudolpha892cde2018-04-19 14:39:07 +020029#include <commonlib/compression.h>
30#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
54 switch (node->compression) {
55 case CBFS_COMPRESS_NONE:
56 comp_name = "Relocating uncompressed";
57 break;
58 case CBFS_COMPRESS_LZMA:
59 comp_name = "Decompressing LZMA";
60 break;
61 case CBFS_COMPRESS_LZ4:
62 comp_name = "Decompressing LZ4";
63 break;
64 default:
65 printk(BIOS_ERR, "ERROR: Unsupported compression\n");
66 return true;
67 }
68
69 printk(BIOS_INFO, "FIT: %s %s to %p\n", comp_name, node->name, dst);
70
71 switch (node->compression) {
72 case CBFS_COMPRESS_NONE:
73 memcpy(dst, node->data, node->size);
74 true_size = node->size;
75 break;
76 case CBFS_COMPRESS_LZMA:
77 timestamp_add_now(TS_START_ULZMA);
78 true_size = ulzman(node->data, node->size, dst, region->size);
79 timestamp_add_now(TS_END_ULZMA);
80 break;
81 case CBFS_COMPRESS_LZ4:
82 timestamp_add_now(TS_START_ULZ4F);
83 true_size = ulz4fn(node->data, node->size, dst, region->size);
84 timestamp_add_now(TS_END_ULZ4F);
85 break;
86 default:
87 return true;
88 }
89
90 if (!true_size) {
Julius Werner2855a0c2019-05-16 13:51:31 -070091 printk(BIOS_ERR, "ERROR: %s decompression failed!\n",
92 comp_name);
Patrick Rudolpha892cde2018-04-19 14:39:07 +020093 return true;
94 }
95
Patrick Rudolpha892cde2018-04-19 14:39:07 +020096 return false;
97}
98
Julius Wernerfec42062019-05-16 16:04:19 -070099static struct device_tree *unpack_fdt(struct fit_image_node *image_node)
100{
101 void *data = image_node->data;
102
103 if (image_node->compression != CBFS_COMPRESS_NONE) {
104 /* TODO: This is an ugly heuristic for how much the size will
105 expand on decompression, fix once FIT images support storing
106 the real uncompressed size. */
107 struct region r = { .offset = 0, .size = image_node->size * 5 };
108 data = malloc(r.size);
109 r.offset = (uintptr_t)data;
110 if (!data || extract(&r, image_node))
111 return NULL;
112 }
113
114 return fdt_unflatten(data);
115}
116
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200117/**
118 * Add coreboot tables, CBMEM information and optional board specific strapping
119 * IDs to the device tree loaded via FIT.
120 */
121static void add_cb_fdt_data(struct device_tree *tree)
122{
123 u32 addr_cells = 1, size_cells = 1;
124 u64 reg_addrs[2], reg_sizes[2];
125 void *baseptr = NULL;
126 size_t size = 0;
127
128 static const char *firmware_path[] = {"firmware", NULL};
129 struct device_tree_node *firmware_node = dt_find_node(tree->root,
130 firmware_path, &addr_cells, &size_cells, 1);
131
132 /* Need to add 'ranges' to the intermediate node to make 'reg' work. */
133 dt_add_bin_prop(firmware_node, "ranges", NULL, 0);
134
135 static const char *coreboot_path[] = {"coreboot", NULL};
136 struct device_tree_node *coreboot_node = dt_find_node(firmware_node,
137 coreboot_path, &addr_cells, &size_cells, 1);
138
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200139 dt_add_string_prop(coreboot_node, "compatible", "coreboot");
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200140
141 /* Fetch CB tables from cbmem */
142 void *cbtable = cbmem_find(CBMEM_ID_CBTABLE);
143 if (!cbtable) {
144 printk(BIOS_WARNING, "FIT: No coreboot table found!\n");
145 return;
146 }
147
148 /* First 'reg' address range is the coreboot table. */
149 const struct lb_header *header = cbtable;
150 reg_addrs[0] = (uintptr_t)header;
151 reg_sizes[0] = header->header_bytes + header->table_bytes;
152
153 /* Second is the CBMEM area (which usually includes the coreboot
154 table). */
155 cbmem_get_region(&baseptr, &size);
156 if (!baseptr || size == 0) {
157 printk(BIOS_WARNING, "FIT: CBMEM pointer/size not found!\n");
158 return;
159 }
160
161 reg_addrs[1] = (uintptr_t)baseptr;
162 reg_sizes[1] = size;
163
164 dt_add_reg_prop(coreboot_node, reg_addrs, reg_sizes, 2, addr_cells,
165 size_cells);
166
167 /* Expose board ID, SKU ID, and RAM code to payload.*/
168 if (board_id() != UNDEFINED_STRAPPING_ID)
169 dt_add_u32_prop(coreboot_node, "board-id", board_id());
170
171 if (sku_id() != UNDEFINED_STRAPPING_ID)
172 dt_add_u32_prop(coreboot_node, "sku-id", sku_id());
173
174 if (ram_code() != UNDEFINED_STRAPPING_ID)
175 dt_add_u32_prop(coreboot_node, "ram-code", ram_code());
176}
177
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200178/*
179 * Parse the uImage FIT, choose a configuration and extract images.
180 */
181void fit_payload(struct prog *payload)
182{
183 struct device_tree *dt = NULL;
184 struct region kernel = {0}, fdt = {0}, initrd = {0};
185 void *data;
186
187 data = rdev_mmap_full(prog_rdev(payload));
188
189 if (data == NULL)
190 return;
191
192 printk(BIOS_INFO, "FIT: Examine payload %s\n", payload->name);
193
194 struct fit_config_node *config = fit_load(data);
195
Julius Wernerb379f192019-05-13 16:34:16 -0700196 if (!config) {
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200197 printk(BIOS_ERR, "ERROR: Could not load FIT\n");
198 rdev_munmap(prog_rdev(payload), data);
199 return;
200 }
201
Julius Wernerfec42062019-05-16 16:04:19 -0700202 dt = unpack_fdt(config->fdt);
Julius Wernerb379f192019-05-13 16:34:16 -0700203 if (!dt) {
204 printk(BIOS_ERR, "ERROR: Failed to unflatten the FDT.\n");
205 rdev_munmap(prog_rdev(payload), data);
206 return;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200207 }
208
Julius Werner21b0b1a2019-05-16 16:12:04 -0700209 struct fit_overlay_chain *chain;
210 list_for_each(chain, config->overlays, list_node) {
211 struct device_tree *overlay = unpack_fdt(chain->overlay);
212 if (!overlay || dt_apply_overlay(dt, overlay)) {
213 printk(BIOS_ERR, "ERROR: Failed to apply overlay %s!\n",
214 chain->overlay->name);
215 }
216 }
217
Julius Wernerb379f192019-05-13 16:34:16 -0700218 dt_apply_fixups(dt);
219
220 /* Insert coreboot specific information */
221 add_cb_fdt_data(dt);
222
223 /* Update device_tree */
224#if defined(CONFIG_LINUX_COMMAND_LINE)
225 fit_update_chosen(dt, (char *)CONFIG_LINUX_COMMAND_LINE);
226#endif
227 fit_update_memory(dt);
228
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200229 /* Collect infos for fit_payload_arch */
Julius Wernerb379f192019-05-13 16:34:16 -0700230 kernel.size = config->kernel->size;
Julius Wernerf6410ba2019-07-10 13:16:40 -0700231 fdt.size = dt_flat_size(dt);
Julius Wernerb379f192019-05-13 16:34:16 -0700232 initrd.size = config->ramdisk ? config->ramdisk->size : 0;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200233
234 /* Invoke arch specific payload placement and fixups */
235 if (!fit_payload_arch(payload, config, &kernel, &fdt, &initrd)) {
236 printk(BIOS_ERR, "ERROR: Failed to find free memory region\n");
237 bootmem_dump_ranges();
238 rdev_munmap(prog_rdev(payload), data);
239 return;
240 }
241
Julius Wernerb379f192019-05-13 16:34:16 -0700242 /* Update ramdisk location in FDT */
243 if (config->ramdisk)
244 fit_add_ramdisk(dt, (void *)initrd.offset, initrd.size);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200245
Julius Wernerb379f192019-05-13 16:34:16 -0700246 /* Repack FDT for handoff to kernel */
247 pack_fdt(&fdt, dt);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200248
Julius Wernerb379f192019-05-13 16:34:16 -0700249 if (config->ramdisk &&
250 extract(&initrd, config->ramdisk)) {
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200251 printk(BIOS_ERR, "ERROR: Failed to extract initrd\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_KERNEL_DECOMPRESSION);
258
Julius Wernerb379f192019-05-13 16:34:16 -0700259 if (extract(&kernel, config->kernel)) {
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200260 printk(BIOS_ERR, "ERROR: Failed to extract kernel\n");
Patrick Rudolphdfc30132018-08-09 09:08:05 +0200261 prog_set_entry(payload, NULL, NULL);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200262 rdev_munmap(prog_rdev(payload), data);
263 return;
264 }
265
266 timestamp_add_now(TS_START_KERNEL);
267
268 rdev_munmap(prog_rdev(payload), data);
269}