Patrick Georgi | afd4c87 | 2020-05-05 23:43:18 +0200 | [diff] [blame] | 1 | /* Taken from depthcharge: src/boot/fit.c */ |
Patrick Georgi | ac95903 | 2020-05-05 22:49:26 +0200 | [diff] [blame] | 2 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 3 | |
| 4 | #include <assert.h> |
Elyes HAOUAS | 351e3e5 | 2019-04-05 18:11:19 +0200 | [diff] [blame] | 5 | #include <console/console.h> |
Joel Kitching | 393c71c | 2019-06-16 16:09:42 +0800 | [diff] [blame] | 6 | #include <ctype.h> |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 7 | #include <endian.h> |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 8 | #include <stdint.h> |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 9 | #include <bootmem.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 12 | #include <program_loading.h> |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 13 | #include <memrange.h> |
| 14 | #include <fit.h> |
| 15 | #include <boardid.h> |
Julius Werner | 9636a10 | 2019-05-03 17:36:43 -0700 | [diff] [blame] | 16 | #include <commonlib/stdlib.h> |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 17 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 18 | static struct list_node image_nodes; |
| 19 | static struct list_node config_nodes; |
| 20 | static struct list_node compat_strings; |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 21 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 22 | struct compat_string_entry { |
| 23 | const char *compat_string; |
| 24 | struct list_node list_node; |
| 25 | }; |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 26 | |
Jonathan Neuschäfer | bbaae95 | 2018-12-12 01:12:13 +0100 | [diff] [blame] | 27 | /* Convert string to lowercase and replace '_' and spaces with '-'. */ |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 28 | static char *clean_compat_string(char *str) |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 29 | { |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 30 | for (size_t i = 0; i < strlen(str); i++) { |
| 31 | str[i] = tolower(str[i]); |
Jonathan Neuschäfer | bbaae95 | 2018-12-12 01:12:13 +0100 | [diff] [blame] | 32 | if (str[i] == '_' || str[i] == ' ') |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 33 | str[i] = '-'; |
| 34 | } |
| 35 | |
| 36 | return str; |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 37 | } |
| 38 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 39 | static void fit_add_default_compat_strings(void) |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 40 | { |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 41 | char compat_string[80] = {}; |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 42 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 43 | if ((board_id() != UNDEFINED_STRAPPING_ID) && |
| 44 | (sku_id() != UNDEFINED_STRAPPING_ID)) { |
| 45 | snprintf(compat_string, sizeof(compat_string), |
| 46 | "%s,%s-rev%u-sku%u", CONFIG_MAINBOARD_VENDOR, |
| 47 | CONFIG_MAINBOARD_PART_NUMBER, board_id(), sku_id()); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 48 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 49 | fit_add_compat_string(compat_string); |
| 50 | } |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 51 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 52 | if (board_id() != UNDEFINED_STRAPPING_ID) { |
| 53 | snprintf(compat_string, sizeof(compat_string), "%s,%s-rev%u", |
| 54 | CONFIG_MAINBOARD_VENDOR, CONFIG_MAINBOARD_PART_NUMBER, |
| 55 | board_id()); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 56 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 57 | fit_add_compat_string(compat_string); |
| 58 | } |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 59 | |
Julius Werner | 11217de | 2020-05-21 10:39:58 -0700 | [diff] [blame] | 60 | if (sku_id() != UNDEFINED_STRAPPING_ID) { |
| 61 | snprintf(compat_string, sizeof(compat_string), "%s,%s-sku%u", |
| 62 | CONFIG_MAINBOARD_VENDOR, CONFIG_MAINBOARD_PART_NUMBER, |
| 63 | sku_id()); |
| 64 | |
| 65 | fit_add_compat_string(compat_string); |
| 66 | } |
| 67 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 68 | snprintf(compat_string, sizeof(compat_string), "%s,%s", |
| 69 | CONFIG_MAINBOARD_VENDOR, CONFIG_MAINBOARD_PART_NUMBER); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 70 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 71 | fit_add_compat_string(compat_string); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 74 | static struct fit_image_node *find_image(const char *name) |
| 75 | { |
| 76 | struct fit_image_node *image; |
| 77 | list_for_each(image, image_nodes, list_node) { |
| 78 | if (!strcmp(image->name, name)) |
| 79 | return image; |
| 80 | } |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 81 | printk(BIOS_ERR, "Cannot find image node %s!\n", name); |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 82 | return NULL; |
| 83 | } |
| 84 | |
Julius Werner | 21b0b1a | 2019-05-16 16:12:04 -0700 | [diff] [blame] | 85 | static struct fit_image_node *find_image_with_overlays(const char *name, |
| 86 | int bytes, struct list_node *prev) |
| 87 | { |
| 88 | struct fit_image_node *base = find_image(name); |
| 89 | if (!base) |
| 90 | return NULL; |
| 91 | |
| 92 | int len = strnlen(name, bytes) + 1; |
| 93 | bytes -= len; |
| 94 | name += len; |
| 95 | while (bytes > 0) { |
| 96 | struct fit_overlay_chain *next = xzalloc(sizeof(*next)); |
| 97 | next->overlay = find_image(name); |
| 98 | if (!next->overlay) |
| 99 | return NULL; |
| 100 | list_insert_after(&next->list_node, prev); |
| 101 | prev = &next->list_node; |
| 102 | len = strnlen(name, bytes) + 1; |
| 103 | bytes -= len; |
| 104 | name += len; |
| 105 | } |
| 106 | |
| 107 | return base; |
| 108 | } |
| 109 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 110 | static void image_node(struct device_tree_node *node) |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 111 | { |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 112 | struct fit_image_node *image = xzalloc(sizeof(*image)); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 113 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 114 | image->compression = CBFS_COMPRESS_NONE; |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 115 | image->name = node->name; |
| 116 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 117 | struct device_tree_property *prop; |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 118 | list_for_each(prop, node->properties, list_node) { |
| 119 | if (!strcmp("data", prop->prop.name)) { |
| 120 | image->data = prop->prop.data; |
| 121 | image->size = prop->prop.size; |
| 122 | } else if (!strcmp("compression", prop->prop.name)) { |
| 123 | if (!strcmp("none", prop->prop.data)) |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 124 | image->compression = CBFS_COMPRESS_NONE; |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 125 | else if (!strcmp("lzma", prop->prop.data)) |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 126 | image->compression = CBFS_COMPRESS_LZMA; |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 127 | else if (!strcmp("lz4", prop->prop.data)) |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 128 | image->compression = CBFS_COMPRESS_LZ4; |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 129 | else |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 130 | image->compression = -1; |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | list_insert_after(&image->list_node, &image_nodes); |
| 135 | } |
| 136 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 137 | static void config_node(struct device_tree_node *node) |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 138 | { |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 139 | struct fit_config_node *config = xzalloc(sizeof(*config)); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 140 | config->name = node->name; |
| 141 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 142 | struct device_tree_property *prop; |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 143 | list_for_each(prop, node->properties, list_node) { |
| 144 | if (!strcmp("kernel", prop->prop.name)) |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 145 | config->kernel = find_image(prop->prop.data); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 146 | else if (!strcmp("fdt", prop->prop.name)) |
Julius Werner | 21b0b1a | 2019-05-16 16:12:04 -0700 | [diff] [blame] | 147 | config->fdt = find_image_with_overlays(prop->prop.data, |
| 148 | prop->prop.size, &config->overlays); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 149 | else if (!strcmp("ramdisk", prop->prop.name)) |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 150 | config->ramdisk = find_image(prop->prop.data); |
Julius Werner | fec4206 | 2019-05-16 16:04:19 -0700 | [diff] [blame] | 151 | else if (!strcmp("compatible", prop->prop.name)) |
| 152 | config->compat = prop->prop; |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | list_insert_after(&config->list_node, &config_nodes); |
| 156 | } |
| 157 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 158 | static void fit_unpack(struct device_tree *tree, const char **default_config) |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 159 | { |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 160 | struct device_tree_node *child; |
| 161 | struct device_tree_node *images = dt_find_node_by_path(tree, "/images", |
| 162 | NULL, NULL, 0); |
| 163 | if (images) |
| 164 | list_for_each(child, images->children, list_node) |
| 165 | image_node(child); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 166 | |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 167 | struct device_tree_node *configs = dt_find_node_by_path(tree, |
| 168 | "/configurations", NULL, NULL, 0); |
| 169 | if (configs) { |
| 170 | *default_config = dt_find_string_prop(configs, "default"); |
| 171 | list_for_each(child, configs->children, list_node) |
| 172 | config_node(child); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 176 | static int fdt_find_compat(const void *blob, uint32_t start_offset, |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 177 | struct fdt_property *prop) |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 178 | { |
| 179 | int offset = start_offset; |
| 180 | int size; |
| 181 | |
| 182 | size = fdt_node_name(blob, offset, NULL); |
| 183 | if (!size) |
| 184 | return -1; |
| 185 | offset += size; |
| 186 | |
| 187 | while ((size = fdt_next_property(blob, offset, prop))) { |
| 188 | if (!strcmp("compatible", prop->name)) |
| 189 | return 0; |
| 190 | |
| 191 | offset += size; |
| 192 | } |
| 193 | |
| 194 | prop->name = NULL; |
| 195 | return -1; |
| 196 | } |
| 197 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 198 | static int fit_check_compat(struct fdt_property *compat_prop, |
| 199 | const char *compat_name) |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 200 | { |
| 201 | int bytes = compat_prop->size; |
| 202 | const char *compat_str = compat_prop->data; |
| 203 | |
| 204 | for (int pos = 0; bytes && compat_str[0]; pos++) { |
| 205 | if (!strncmp(compat_str, compat_name, bytes)) |
| 206 | return pos; |
| 207 | int len = strlen(compat_str) + 1; |
| 208 | compat_str += len; |
| 209 | bytes -= len; |
| 210 | } |
| 211 | return -1; |
| 212 | } |
| 213 | |
Patrick Rudolph | 0a7d690 | 2018-08-22 09:55:15 +0200 | [diff] [blame] | 214 | void fit_update_chosen(struct device_tree *tree, const char *cmd_line) |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 215 | { |
| 216 | const char *path[] = { "chosen", NULL }; |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 217 | struct device_tree_node *node; |
| 218 | node = dt_find_node(tree->root, path, NULL, NULL, 1); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 219 | |
| 220 | dt_add_string_prop(node, "bootargs", cmd_line); |
| 221 | } |
| 222 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 223 | void fit_add_ramdisk(struct device_tree *tree, void *ramdisk_addr, |
| 224 | size_t ramdisk_size) |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 225 | { |
| 226 | const char *path[] = { "chosen", NULL }; |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 227 | struct device_tree_node *node; |
| 228 | node = dt_find_node(tree->root, path, NULL, NULL, 1); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 229 | |
Patrick Rudolph | 3fca4ed | 2018-08-10 10:12:35 +0200 | [diff] [blame] | 230 | u64 start = (uintptr_t)ramdisk_addr; |
| 231 | u64 end = start + ramdisk_size; |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 232 | |
Patrick Rudolph | 3fca4ed | 2018-08-10 10:12:35 +0200 | [diff] [blame] | 233 | dt_add_u64_prop(node, "linux,initrd-start", start); |
| 234 | dt_add_u64_prop(node, "linux,initrd-end", end); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 237 | static void update_reserve_map(uint64_t start, uint64_t end, |
| 238 | struct device_tree *tree) |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 239 | { |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 240 | struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry)); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 241 | |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 242 | entry->start = start; |
| 243 | entry->size = end - start; |
| 244 | |
| 245 | list_insert_after(&entry->list_node, &tree->reserve_map); |
| 246 | } |
| 247 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 248 | struct entry_params { |
Martin Roth | 38ddbfb | 2019-10-23 21:41:00 -0600 | [diff] [blame] | 249 | unsigned int addr_cells; |
| 250 | unsigned int size_cells; |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 251 | void *data; |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 252 | }; |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 253 | |
Martin Roth | 38ddbfb | 2019-10-23 21:41:00 -0600 | [diff] [blame] | 254 | static uint64_t max_range(unsigned int size_cells) |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 255 | { |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 256 | /* |
| 257 | * Split up ranges who's sizes are too large to fit in #size-cells. |
| 258 | * The largest value we can store isn't a power of two, so we'll round |
| 259 | * down to make the math easier. |
| 260 | */ |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 261 | return 0x1ULL << (size_cells * 32 - 1); |
| 262 | } |
| 263 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 264 | static void update_mem_property(u64 start, u64 end, struct entry_params *params) |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 265 | { |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 266 | u8 *data = (u8 *)params->data; |
| 267 | u64 full_size = end - start; |
| 268 | while (full_size) { |
| 269 | const u64 max_size = max_range(params->size_cells); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 270 | const u64 size = MIN(max_size, full_size); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 271 | |
| 272 | dt_write_int(data, start, params->addr_cells * sizeof(u32)); |
| 273 | data += params->addr_cells * sizeof(uint32_t); |
| 274 | start += size; |
| 275 | |
| 276 | dt_write_int(data, size, params->size_cells * sizeof(u32)); |
| 277 | data += params->size_cells * sizeof(uint32_t); |
| 278 | full_size -= size; |
| 279 | } |
| 280 | params->data = data; |
| 281 | } |
| 282 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 283 | struct mem_map { |
| 284 | struct memranges mem; |
| 285 | struct memranges reserved; |
| 286 | }; |
| 287 | |
| 288 | static bool walk_memory_table(const struct range_entry *r, void *arg) |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 289 | { |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 290 | struct mem_map *arg_map = arg; |
| 291 | |
| 292 | /* |
| 293 | * Kernel likes its available memory areas at least 1MB |
| 294 | * aligned, let's trim the regions such that unaligned padding |
| 295 | * is added to reserved memory. |
| 296 | */ |
| 297 | if (range_entry_tag(r) == BM_MEM_RAM) { |
| 298 | uint64_t new_start = ALIGN_UP(range_entry_base(r), 1 * MiB); |
| 299 | uint64_t new_end = ALIGN_DOWN(range_entry_end(r), 1 * MiB); |
| 300 | |
| 301 | if (new_start != range_entry_base(r)) |
| 302 | memranges_insert(&arg_map->reserved, |
| 303 | range_entry_base(r), |
| 304 | new_start - range_entry_base(r), |
| 305 | BM_MEM_RESERVED); |
| 306 | |
| 307 | if (new_start != new_end) |
| 308 | memranges_insert(&arg_map->mem, new_start, |
| 309 | new_end - new_start, BM_MEM_RAM); |
| 310 | |
| 311 | if (new_end != range_entry_end(r)) |
| 312 | memranges_insert(&arg_map->reserved, new_end, |
| 313 | range_entry_end(r) - new_end, |
| 314 | BM_MEM_RESERVED); |
| 315 | } else |
| 316 | memranges_insert(&arg_map->reserved, range_entry_base(r), |
| 317 | range_entry_size(r), |
| 318 | BM_MEM_RESERVED); |
| 319 | |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | void fit_add_compat_string(const char *str) |
| 324 | { |
| 325 | struct compat_string_entry *compat_node; |
| 326 | |
| 327 | compat_node = xzalloc(sizeof(*compat_node)); |
| 328 | compat_node->compat_string = strdup(str); |
| 329 | |
| 330 | clean_compat_string((char *)compat_node->compat_string); |
| 331 | |
| 332 | list_insert_after(&compat_node->list_node, &compat_strings); |
| 333 | } |
| 334 | |
| 335 | void fit_update_memory(struct device_tree *tree) |
| 336 | { |
| 337 | const struct range_entry *r; |
| 338 | struct device_tree_node *node; |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 339 | u32 addr_cells = 1, size_cells = 1; |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 340 | struct mem_map map; |
| 341 | |
| 342 | printk(BIOS_INFO, "FIT: Updating devicetree memory entries\n"); |
| 343 | |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 344 | dt_read_cell_props(tree->root, &addr_cells, &size_cells); |
| 345 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 346 | /* |
| 347 | * First remove all existing device_type="memory" nodes, then add ours. |
| 348 | */ |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 349 | list_for_each(node, tree->root->children, list_node) { |
| 350 | const char *devtype = dt_find_string_prop(node, "device_type"); |
| 351 | if (devtype && !strcmp(devtype, "memory")) |
| 352 | list_remove(&node->list_node); |
| 353 | } |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 354 | |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 355 | node = xzalloc(sizeof(*node)); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 356 | |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 357 | node->name = "memory"; |
| 358 | list_insert_after(&node->list_node, &tree->root->children); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 359 | dt_add_string_prop(node, "device_type", (char *)"memory"); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 360 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 361 | memranges_init_empty(&map.mem, NULL, 0); |
| 362 | memranges_init_empty(&map.reserved, NULL, 0); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 363 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 364 | bootmem_walk_os_mem(walk_memory_table, &map); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 365 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 366 | /* CBMEM regions are both carved out and explicitly reserved. */ |
| 367 | memranges_each_entry(r, &map.reserved) { |
| 368 | update_reserve_map(range_entry_base(r), range_entry_end(r), |
| 369 | tree); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 370 | } |
| 371 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 372 | /* |
| 373 | * Count the amount of 'reg' entries we need (account for size limits). |
| 374 | */ |
| 375 | size_t count = 0; |
| 376 | memranges_each_entry(r, &map.mem) { |
| 377 | uint64_t size = range_entry_size(r); |
| 378 | uint64_t max_size = max_range(size_cells); |
| 379 | count += DIV_ROUND_UP(size, max_size); |
| 380 | } |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 381 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 382 | /* Allocate the right amount of space and fill up the entries. */ |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 383 | size_t length = count * (addr_cells + size_cells) * sizeof(u32); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 384 | |
| 385 | void *data = xzalloc(length); |
| 386 | |
| 387 | struct entry_params add_params = { addr_cells, size_cells, data }; |
| 388 | memranges_each_entry(r, &map.mem) { |
| 389 | update_mem_property(range_entry_base(r), range_entry_end(r), |
| 390 | &add_params); |
| 391 | } |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 392 | assert(add_params.data - data == length); |
| 393 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 394 | /* Assemble the final property and add it to the device tree. */ |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 395 | dt_add_bin_prop(node, "reg", data, length); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 396 | |
| 397 | memranges_teardown(&map.mem); |
| 398 | memranges_teardown(&map.reserved); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 399 | } |
| 400 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 401 | /* |
| 402 | * Finds a compat string and updates the compat position and rank. |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 403 | * @param config The current config node to operate on |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 404 | * @return 0 if compat updated, -1 if this FDT cannot be used. |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 405 | */ |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 406 | static int fit_update_compat(struct fit_config_node *config) |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 407 | { |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 408 | /* If there was no "compatible" property in config node, this is a |
| 409 | legacy FIT image. Must extract compat prop from FDT itself. */ |
Julius Werner | fec4206 | 2019-05-16 16:04:19 -0700 | [diff] [blame] | 410 | if (!config->compat.name) { |
| 411 | void *fdt_blob = config->fdt->data; |
| 412 | const struct fdt_header *fdt_header = fdt_blob; |
| 413 | uint32_t fdt_offset = be32_to_cpu(fdt_header->structure_offset); |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 414 | |
Julius Werner | fec4206 | 2019-05-16 16:04:19 -0700 | [diff] [blame] | 415 | if (config->fdt->compression != CBFS_COMPRESS_NONE) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 416 | printk(BIOS_ERR, "config %s has a compressed FDT without " |
Julius Werner | fec4206 | 2019-05-16 16:04:19 -0700 | [diff] [blame] | 417 | "external compatible property, skipping.\n", |
| 418 | config->name); |
| 419 | return -1; |
| 420 | } |
| 421 | |
Julius Werner | 23df477 | 2019-05-17 22:50:18 -0700 | [diff] [blame] | 422 | /* FDT overlays are not supported in legacy FIT images. */ |
Julius Werner | 21b0b1a | 2019-05-16 16:12:04 -0700 | [diff] [blame] | 423 | if (config->overlays.next) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 424 | printk(BIOS_ERR, "config %s has overlay but no compat!\n", |
Julius Werner | 21b0b1a | 2019-05-16 16:12:04 -0700 | [diff] [blame] | 425 | config->name); |
| 426 | return -1; |
| 427 | } |
| 428 | |
Julius Werner | fec4206 | 2019-05-16 16:04:19 -0700 | [diff] [blame] | 429 | if (fdt_find_compat(fdt_blob, fdt_offset, &config->compat)) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 430 | printk(BIOS_ERR, "Can't find compat string in FDT %s " |
Julius Werner | fec4206 | 2019-05-16 16:04:19 -0700 | [diff] [blame] | 431 | "for config %s, skipping.\n", |
| 432 | config->fdt->name, config->name); |
| 433 | return -1; |
| 434 | } |
| 435 | } |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 436 | |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 437 | config->compat_pos = -1; |
| 438 | config->compat_rank = -1; |
Julius Werner | fec4206 | 2019-05-16 16:04:19 -0700 | [diff] [blame] | 439 | size_t i = 0; |
| 440 | struct compat_string_entry *compat_node; |
| 441 | list_for_each(compat_node, compat_strings, list_node) { |
| 442 | int pos = fit_check_compat(&config->compat, |
| 443 | compat_node->compat_string); |
| 444 | if (pos >= 0) { |
| 445 | config->compat_pos = pos; |
| 446 | config->compat_rank = i; |
| 447 | config->compat_string = |
| 448 | compat_node->compat_string; |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 449 | } |
Julius Werner | fec4206 | 2019-05-16 16:04:19 -0700 | [diff] [blame] | 450 | i++; |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 451 | } |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 452 | |
| 453 | return 0; |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 454 | } |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 455 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 456 | struct fit_config_node *fit_load(void *fit) |
| 457 | { |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 458 | struct fit_image_node *image; |
| 459 | struct fit_config_node *config; |
| 460 | struct compat_string_entry *compat_node; |
Julius Werner | 21b0b1a | 2019-05-16 16:12:04 -0700 | [diff] [blame] | 461 | struct fit_overlay_chain *overlay_chain; |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 462 | |
| 463 | printk(BIOS_DEBUG, "FIT: Loading FIT from %p\n", fit); |
| 464 | |
Julius Werner | 73eaec8 | 2019-05-03 17:58:07 -0700 | [diff] [blame] | 465 | struct device_tree *tree = fdt_unflatten(fit); |
| 466 | if (!tree) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 467 | printk(BIOS_ERR, "Failed to unflatten FIT image!\n"); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 468 | return NULL; |
| 469 | } |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 470 | |
| 471 | const char *default_config_name = NULL; |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 472 | struct fit_config_node *default_config = NULL; |
| 473 | struct fit_config_node *compat_config = NULL; |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 474 | |
| 475 | fit_unpack(tree, &default_config_name); |
| 476 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 477 | /* List the images we found. */ |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 478 | list_for_each(image, image_nodes, list_node) |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 479 | printk(BIOS_DEBUG, "FIT: Image %s has %d bytes.\n", image->name, |
| 480 | image->size); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 481 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 482 | fit_add_default_compat_strings(); |
| 483 | |
| 484 | printk(BIOS_DEBUG, "FIT: Compat preference " |
| 485 | "(lowest to highest priority) :"); |
| 486 | |
| 487 | list_for_each(compat_node, compat_strings, list_node) { |
| 488 | printk(BIOS_DEBUG, " %s", compat_node->compat_string); |
| 489 | } |
| 490 | printk(BIOS_DEBUG, "\n"); |
| 491 | /* Process and list the configs. */ |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 492 | list_for_each(config, config_nodes, list_node) { |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 493 | if (!config->kernel) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 494 | printk(BIOS_ERR, "config %s has no kernel, skipping.\n", |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 495 | config->name); |
| 496 | continue; |
| 497 | } |
| 498 | if (!config->fdt) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 499 | printk(BIOS_ERR, "config %s has no FDT, skipping.\n", |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 500 | config->name); |
| 501 | continue; |
| 502 | } |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 503 | |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 504 | if (config->ramdisk && |
| 505 | config->ramdisk->compression < 0) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 506 | printk(BIOS_WARNING, "Ramdisk is compressed with " |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 507 | "an unsupported algorithm, discarding config %s." |
| 508 | "\n", config->name); |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 509 | continue; |
| 510 | } |
| 511 | |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 512 | if (fit_update_compat(config)) |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 513 | continue; |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 514 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 515 | printk(BIOS_DEBUG, "FIT: config %s", config->name); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 516 | if (default_config_name && |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 517 | !strcmp(config->name, default_config_name)) { |
| 518 | printk(BIOS_DEBUG, " (default)"); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 519 | default_config = config; |
| 520 | } |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 521 | printk(BIOS_DEBUG, ", kernel %s", config->kernel->name); |
| 522 | printk(BIOS_DEBUG, ", fdt %s", config->fdt->name); |
Julius Werner | 21b0b1a | 2019-05-16 16:12:04 -0700 | [diff] [blame] | 523 | list_for_each(overlay_chain, config->overlays, list_node) |
| 524 | printk(BIOS_DEBUG, " %s", overlay_chain->overlay->name); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 525 | if (config->ramdisk) |
Julius Werner | b379f19 | 2019-05-13 16:34:16 -0700 | [diff] [blame] | 526 | printk(BIOS_DEBUG, ", ramdisk %s", |
| 527 | config->ramdisk->name); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 528 | if (config->compat.name) { |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 529 | printk(BIOS_DEBUG, ", compat"); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 530 | int bytes = config->compat.size; |
| 531 | const char *compat_str = config->compat.data; |
| 532 | for (int pos = 0; bytes && compat_str[0]; pos++) { |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 533 | printk(BIOS_DEBUG, " %s", compat_str); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 534 | if (pos == config->compat_pos) |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 535 | printk(BIOS_DEBUG, " (match)"); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 536 | int len = strlen(compat_str) + 1; |
| 537 | compat_str += len; |
| 538 | bytes -= len; |
| 539 | } |
| 540 | |
| 541 | if (config->compat_rank >= 0 && (!compat_config || |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 542 | config->compat_rank > compat_config->compat_rank)) |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 543 | compat_config = config; |
| 544 | } |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 545 | printk(BIOS_DEBUG, "\n"); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 546 | } |
| 547 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 548 | struct fit_config_node *to_boot = NULL; |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 549 | if (compat_config) { |
| 550 | to_boot = compat_config; |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 551 | printk(BIOS_INFO, "FIT: Choosing best match %s for compat " |
| 552 | "%s.\n", to_boot->name, to_boot->compat_string); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 553 | } else if (default_config) { |
| 554 | to_boot = default_config; |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 555 | printk(BIOS_INFO, "FIT: No match, choosing default %s.\n", |
| 556 | to_boot->name); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 557 | } else { |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 558 | printk(BIOS_ERR, "FIT: No compatible or default configs. " |
| 559 | "Giving up.\n"); |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 560 | return NULL; |
| 561 | } |
| 562 | |
Patrick Rudolph | a892cde | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 563 | return to_boot; |
Patrick Rudolph | a45e9f8 | 2018-04-19 14:39:07 +0200 | [diff] [blame] | 564 | } |