Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 1 | /* |
| 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 Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 27 | #include <string.h> |
Elyes HAOUAS | 28b38cd | 2019-03-18 11:30:08 +0100 | [diff] [blame] | 28 | #include <commonlib/cbfs_serialized.h> |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 29 | #include <commonlib/compression.h> |
| 30 | #include <lib.h> |
| 31 | #include <fit_payload.h> |
Philipp Deppenwiese | 84258db | 2018-08-16 00:31:26 +0200 | [diff] [blame] | 32 | #include <boardid.h> |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 33 | |
| 34 | /* Pack the device_tree and place it at given position. */ |
| 35 | static 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 | */ |
| 48 | static 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 Werner | 2855a0c | 2019-05-16 13:51:31 -0700 | [diff] [blame] | 91 | printk(BIOS_ERR, "ERROR: %s decompression failed!\n", |
| 92 | comp_name); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | |
Julius Werner | fec4206 | 2019-05-16 16:04:19 -0700 | [diff] [blame] | 99 | static 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 Deppenwiese | 84258db | 2018-08-16 00:31:26 +0200 | [diff] [blame] | 117 | /** |
| 118 | * Add coreboot tables, CBMEM information and optional board specific strapping |
| 119 | * IDs to the device tree loaded via FIT. |
| 120 | */ |
| 121 | static 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 Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 139 | dt_add_string_prop(coreboot_node, "compatible", "coreboot"); |
Philipp Deppenwiese | 84258db | 2018-08-16 00:31:26 +0200 | [diff] [blame] | 140 | |
| 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 Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 178 | /* |
| 179 | * Parse the uImage FIT, choose a configuration and extract images. |
| 180 | */ |
| 181 | void 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 Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 196 | if (!config) { |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 197 | printk(BIOS_ERR, "ERROR: Could not load FIT\n"); |
| 198 | rdev_munmap(prog_rdev(payload), data); |
| 199 | return; |
| 200 | } |
| 201 | |
Julius Werner | fec4206 | 2019-05-16 16:04:19 -0700 | [diff] [blame] | 202 | dt = unpack_fdt(config->fdt); |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 203 | if (!dt) { |
| 204 | printk(BIOS_ERR, "ERROR: Failed to unflatten the FDT.\n"); |
| 205 | rdev_munmap(prog_rdev(payload), data); |
| 206 | return; |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Julius Werner | 21b0b1a | 2019-05-16 16:12:04 -0700 | [diff] [blame] | 209 | 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 Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 218 | 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 Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 229 | /* Collect infos for fit_payload_arch */ |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 230 | kernel.size = config->kernel->size; |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 231 | fdt.size = dt ? dt_flat_size(dt) : 0; |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 232 | initrd.size = config->ramdisk ? config->ramdisk->size : 0; |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 233 | |
| 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 Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 242 | /* Update ramdisk location in FDT */ |
| 243 | if (config->ramdisk) |
| 244 | fit_add_ramdisk(dt, (void *)initrd.offset, initrd.size); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 245 | |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 246 | /* Repack FDT for handoff to kernel */ |
| 247 | pack_fdt(&fdt, dt); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 248 | |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 249 | if (config->ramdisk && |
| 250 | extract(&initrd, config->ramdisk)) { |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 251 | printk(BIOS_ERR, "ERROR: Failed to extract initrd\n"); |
Patrick Rudolph | dfc3013 | 2018-08-09 09:08:05 +0200 | [diff] [blame] | 252 | prog_set_entry(payload, NULL, NULL); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 253 | rdev_munmap(prog_rdev(payload), data); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | timestamp_add_now(TS_KERNEL_DECOMPRESSION); |
| 258 | |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 259 | if (extract(&kernel, config->kernel)) { |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 260 | printk(BIOS_ERR, "ERROR: Failed to extract kernel\n"); |
Patrick Rudolph | dfc3013 | 2018-08-09 09:08:05 +0200 | [diff] [blame] | 261 | prog_set_entry(payload, NULL, NULL); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 262 | 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 | } |