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> |
| 27 | #include <cbfs.h> |
| 28 | #include <string.h> |
| 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) { |
| 91 | printk(BIOS_ERR, "ERROR: %s node failed!\n", comp_name); |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | prog_segment_loaded(region->offset, true_size, 0); |
| 96 | |
| 97 | return false; |
| 98 | } |
| 99 | |
Philipp Deppenwiese | 84258db | 2018-08-16 00:31:26 +0200 | [diff] [blame^] | 100 | /** |
| 101 | * Add coreboot tables, CBMEM information and optional board specific strapping |
| 102 | * IDs to the device tree loaded via FIT. |
| 103 | */ |
| 104 | static void add_cb_fdt_data(struct device_tree *tree) |
| 105 | { |
| 106 | u32 addr_cells = 1, size_cells = 1; |
| 107 | u64 reg_addrs[2], reg_sizes[2]; |
| 108 | void *baseptr = NULL; |
| 109 | size_t size = 0; |
| 110 | |
| 111 | static const char *firmware_path[] = {"firmware", NULL}; |
| 112 | struct device_tree_node *firmware_node = dt_find_node(tree->root, |
| 113 | firmware_path, &addr_cells, &size_cells, 1); |
| 114 | |
| 115 | /* Need to add 'ranges' to the intermediate node to make 'reg' work. */ |
| 116 | dt_add_bin_prop(firmware_node, "ranges", NULL, 0); |
| 117 | |
| 118 | static const char *coreboot_path[] = {"coreboot", NULL}; |
| 119 | struct device_tree_node *coreboot_node = dt_find_node(firmware_node, |
| 120 | coreboot_path, &addr_cells, &size_cells, 1); |
| 121 | |
| 122 | dt_add_string_prop(coreboot_node, "compatible", strdup("coreboot")); |
| 123 | |
| 124 | /* Fetch CB tables from cbmem */ |
| 125 | void *cbtable = cbmem_find(CBMEM_ID_CBTABLE); |
| 126 | if (!cbtable) { |
| 127 | printk(BIOS_WARNING, "FIT: No coreboot table found!\n"); |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | /* First 'reg' address range is the coreboot table. */ |
| 132 | const struct lb_header *header = cbtable; |
| 133 | reg_addrs[0] = (uintptr_t)header; |
| 134 | reg_sizes[0] = header->header_bytes + header->table_bytes; |
| 135 | |
| 136 | /* Second is the CBMEM area (which usually includes the coreboot |
| 137 | table). */ |
| 138 | cbmem_get_region(&baseptr, &size); |
| 139 | if (!baseptr || size == 0) { |
| 140 | printk(BIOS_WARNING, "FIT: CBMEM pointer/size not found!\n"); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | reg_addrs[1] = (uintptr_t)baseptr; |
| 145 | reg_sizes[1] = size; |
| 146 | |
| 147 | dt_add_reg_prop(coreboot_node, reg_addrs, reg_sizes, 2, addr_cells, |
| 148 | size_cells); |
| 149 | |
| 150 | /* Expose board ID, SKU ID, and RAM code to payload.*/ |
| 151 | if (board_id() != UNDEFINED_STRAPPING_ID) |
| 152 | dt_add_u32_prop(coreboot_node, "board-id", board_id()); |
| 153 | |
| 154 | if (sku_id() != UNDEFINED_STRAPPING_ID) |
| 155 | dt_add_u32_prop(coreboot_node, "sku-id", sku_id()); |
| 156 | |
| 157 | if (ram_code() != UNDEFINED_STRAPPING_ID) |
| 158 | dt_add_u32_prop(coreboot_node, "ram-code", ram_code()); |
| 159 | } |
| 160 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 161 | /* |
| 162 | * Parse the uImage FIT, choose a configuration and extract images. |
| 163 | */ |
| 164 | void fit_payload(struct prog *payload) |
| 165 | { |
| 166 | struct device_tree *dt = NULL; |
| 167 | struct region kernel = {0}, fdt = {0}, initrd = {0}; |
| 168 | void *data; |
| 169 | |
| 170 | data = rdev_mmap_full(prog_rdev(payload)); |
| 171 | |
| 172 | if (data == NULL) |
| 173 | return; |
| 174 | |
| 175 | printk(BIOS_INFO, "FIT: Examine payload %s\n", payload->name); |
| 176 | |
| 177 | struct fit_config_node *config = fit_load(data); |
| 178 | |
| 179 | if (!config || !config->kernel_node) { |
| 180 | printk(BIOS_ERR, "ERROR: Could not load FIT\n"); |
| 181 | rdev_munmap(prog_rdev(payload), data); |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | if (config->fdt_node) { |
| 186 | dt = fdt_unflatten(config->fdt_node->data); |
| 187 | if (!dt) { |
| 188 | printk(BIOS_ERR, |
| 189 | "ERROR: Failed to unflatten the FDT.\n"); |
| 190 | rdev_munmap(prog_rdev(payload), data); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | dt_apply_fixups(dt); |
| 195 | |
Philipp Deppenwiese | 84258db | 2018-08-16 00:31:26 +0200 | [diff] [blame^] | 196 | /* Insert coreboot specific information */ |
| 197 | add_cb_fdt_data(dt); |
| 198 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 199 | /* Update device_tree */ |
| 200 | #if defined(CONFIG_LINUX_COMMAND_LINE) |
| 201 | fit_update_chosen(dt, (char *)CONFIG_LINUX_COMMAND_LINE); |
| 202 | #endif |
| 203 | fit_update_memory(dt); |
| 204 | } |
| 205 | |
| 206 | /* Collect infos for fit_payload_arch */ |
| 207 | kernel.size = config->kernel_node->size; |
| 208 | fdt.size = dt ? dt_flat_size(dt) : 0; |
| 209 | initrd.size = config->ramdisk_node ? config->ramdisk_node->size : 0; |
| 210 | |
| 211 | /* Invoke arch specific payload placement and fixups */ |
| 212 | if (!fit_payload_arch(payload, config, &kernel, &fdt, &initrd)) { |
| 213 | printk(BIOS_ERR, "ERROR: Failed to find free memory region\n"); |
| 214 | bootmem_dump_ranges(); |
| 215 | rdev_munmap(prog_rdev(payload), data); |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | /* Load the images to given position */ |
| 220 | if (config->fdt_node) { |
| 221 | /* Update device_tree */ |
| 222 | if (config->ramdisk_node) |
| 223 | fit_add_ramdisk(dt, (void *)initrd.offset, initrd.size); |
| 224 | |
| 225 | pack_fdt(&fdt, dt); |
| 226 | } |
| 227 | |
| 228 | if (config->ramdisk_node && |
| 229 | extract(&initrd, config->ramdisk_node)) { |
| 230 | printk(BIOS_ERR, "ERROR: Failed to extract initrd\n"); |
Patrick Rudolph | dfc3013 | 2018-08-09 09:08:05 +0200 | [diff] [blame] | 231 | prog_set_entry(payload, NULL, NULL); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 232 | rdev_munmap(prog_rdev(payload), data); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | timestamp_add_now(TS_KERNEL_DECOMPRESSION); |
| 237 | |
| 238 | if (extract(&kernel, config->kernel_node)) { |
| 239 | printk(BIOS_ERR, "ERROR: Failed to extract kernel\n"); |
Patrick Rudolph | dfc3013 | 2018-08-09 09:08:05 +0200 | [diff] [blame] | 240 | prog_set_entry(payload, NULL, NULL); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 241 | rdev_munmap(prog_rdev(payload), data); |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | timestamp_add_now(TS_START_KERNEL); |
| 246 | |
| 247 | rdev_munmap(prog_rdev(payload), data); |
| 248 | } |