blob: c977903534c6de6763daf1c2c9af2fa2c2c47c9f [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
Philipp Deppenwiese84258db2018-08-16 00:31:26 +020099/**
100 * Add coreboot tables, CBMEM information and optional board specific strapping
101 * IDs to the device tree loaded via FIT.
102 */
103static void add_cb_fdt_data(struct device_tree *tree)
104{
105 u32 addr_cells = 1, size_cells = 1;
106 u64 reg_addrs[2], reg_sizes[2];
107 void *baseptr = NULL;
108 size_t size = 0;
109
110 static const char *firmware_path[] = {"firmware", NULL};
111 struct device_tree_node *firmware_node = dt_find_node(tree->root,
112 firmware_path, &addr_cells, &size_cells, 1);
113
114 /* Need to add 'ranges' to the intermediate node to make 'reg' work. */
115 dt_add_bin_prop(firmware_node, "ranges", NULL, 0);
116
117 static const char *coreboot_path[] = {"coreboot", NULL};
118 struct device_tree_node *coreboot_node = dt_find_node(firmware_node,
119 coreboot_path, &addr_cells, &size_cells, 1);
120
Patrick Rudolph0a7d6902018-08-22 09:55:15 +0200121 dt_add_string_prop(coreboot_node, "compatible", "coreboot");
Philipp Deppenwiese84258db2018-08-16 00:31:26 +0200122
123 /* Fetch CB tables from cbmem */
124 void *cbtable = cbmem_find(CBMEM_ID_CBTABLE);
125 if (!cbtable) {
126 printk(BIOS_WARNING, "FIT: No coreboot table found!\n");
127 return;
128 }
129
130 /* First 'reg' address range is the coreboot table. */
131 const struct lb_header *header = cbtable;
132 reg_addrs[0] = (uintptr_t)header;
133 reg_sizes[0] = header->header_bytes + header->table_bytes;
134
135 /* Second is the CBMEM area (which usually includes the coreboot
136 table). */
137 cbmem_get_region(&baseptr, &size);
138 if (!baseptr || size == 0) {
139 printk(BIOS_WARNING, "FIT: CBMEM pointer/size not found!\n");
140 return;
141 }
142
143 reg_addrs[1] = (uintptr_t)baseptr;
144 reg_sizes[1] = size;
145
146 dt_add_reg_prop(coreboot_node, reg_addrs, reg_sizes, 2, addr_cells,
147 size_cells);
148
149 /* Expose board ID, SKU ID, and RAM code to payload.*/
150 if (board_id() != UNDEFINED_STRAPPING_ID)
151 dt_add_u32_prop(coreboot_node, "board-id", board_id());
152
153 if (sku_id() != UNDEFINED_STRAPPING_ID)
154 dt_add_u32_prop(coreboot_node, "sku-id", sku_id());
155
156 if (ram_code() != UNDEFINED_STRAPPING_ID)
157 dt_add_u32_prop(coreboot_node, "ram-code", ram_code());
158}
159
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200160/*
161 * Parse the uImage FIT, choose a configuration and extract images.
162 */
163void fit_payload(struct prog *payload)
164{
165 struct device_tree *dt = NULL;
166 struct region kernel = {0}, fdt = {0}, initrd = {0};
167 void *data;
168
169 data = rdev_mmap_full(prog_rdev(payload));
170
171 if (data == NULL)
172 return;
173
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) {
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200179 printk(BIOS_ERR, "ERROR: Could not load FIT\n");
180 rdev_munmap(prog_rdev(payload), data);
181 return;
182 }
183
Julius Wernerb379f192019-05-13 16:34:16 -0700184 dt = fdt_unflatten(config->fdt->data);
185 if (!dt) {
186 printk(BIOS_ERR, "ERROR: Failed to unflatten the FDT.\n");
187 rdev_munmap(prog_rdev(payload), data);
188 return;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200189 }
190
Julius Wernerb379f192019-05-13 16:34:16 -0700191 dt_apply_fixups(dt);
192
193 /* Insert coreboot specific information */
194 add_cb_fdt_data(dt);
195
196 /* Update device_tree */
197#if defined(CONFIG_LINUX_COMMAND_LINE)
198 fit_update_chosen(dt, (char *)CONFIG_LINUX_COMMAND_LINE);
199#endif
200 fit_update_memory(dt);
201
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200202 /* Collect infos for fit_payload_arch */
Julius Wernerb379f192019-05-13 16:34:16 -0700203 kernel.size = config->kernel->size;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200204 fdt.size = dt ? dt_flat_size(dt) : 0;
Julius Wernerb379f192019-05-13 16:34:16 -0700205 initrd.size = config->ramdisk ? config->ramdisk->size : 0;
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200206
207 /* Invoke arch specific payload placement and fixups */
208 if (!fit_payload_arch(payload, config, &kernel, &fdt, &initrd)) {
209 printk(BIOS_ERR, "ERROR: Failed to find free memory region\n");
210 bootmem_dump_ranges();
211 rdev_munmap(prog_rdev(payload), data);
212 return;
213 }
214
Julius Wernerb379f192019-05-13 16:34:16 -0700215 /* Update ramdisk location in FDT */
216 if (config->ramdisk)
217 fit_add_ramdisk(dt, (void *)initrd.offset, initrd.size);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200218
Julius Wernerb379f192019-05-13 16:34:16 -0700219 /* Repack FDT for handoff to kernel */
220 pack_fdt(&fdt, dt);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200221
Julius Wernerb379f192019-05-13 16:34:16 -0700222 if (config->ramdisk &&
223 extract(&initrd, config->ramdisk)) {
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200224 printk(BIOS_ERR, "ERROR: Failed to extract initrd\n");
Patrick Rudolphdfc30132018-08-09 09:08:05 +0200225 prog_set_entry(payload, NULL, NULL);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200226 rdev_munmap(prog_rdev(payload), data);
227 return;
228 }
229
230 timestamp_add_now(TS_KERNEL_DECOMPRESSION);
231
Julius Wernerb379f192019-05-13 16:34:16 -0700232 if (extract(&kernel, config->kernel)) {
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200233 printk(BIOS_ERR, "ERROR: Failed to extract kernel\n");
Patrick Rudolphdfc30132018-08-09 09:08:05 +0200234 prog_set_entry(payload, NULL, NULL);
Patrick Rudolpha892cde2018-04-19 14:39:07 +0200235 rdev_munmap(prog_rdev(payload), data);
236 return;
237 }
238
239 timestamp_add_now(TS_START_KERNEL);
240
241 rdev_munmap(prog_rdev(payload), data);
242}