Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Firmware Interface Table support. |
| 3 | * |
| 4 | * Copyright (C) 2012 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <stdint.h> |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 17 | #include <stdio.h> |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 20 | |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 21 | #include "fit.h" |
| 22 | |
| 23 | /* FIXME: This code assumes it is being executed on a little endian machine. */ |
| 24 | |
| 25 | #define FIT_POINTER_LOCATION 0xffffffc0 |
| 26 | #define FIT_TABLE_LOWEST_ADDRESS ((uint32_t)(-(16 << 20))) |
| 27 | #define FIT_ENTRY_CHECKSUM_VALID 0x80 |
| 28 | #define FIT_TYPE_HEADER 0x0 |
| 29 | #define FIT_HEADER_VERSION 0x0100 |
| 30 | #define FIT_HEADER_ADDRESS "_FIT_ " |
| 31 | #define FIT_TYPE_MICROCODE 0x1 |
| 32 | #define FIT_MICROCODE_VERSION 0x0100 |
| 33 | |
| 34 | struct fit_entry { |
| 35 | uint64_t address; |
| 36 | uint32_t size_reserved; |
| 37 | uint16_t version; |
| 38 | uint8_t type_checksum_valid; |
| 39 | uint8_t checksum; |
| 40 | } __attribute__ ((packed)); |
| 41 | |
| 42 | struct fit_table { |
| 43 | struct fit_entry header; |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 44 | struct fit_entry entries[]; |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 45 | } __attribute__ ((packed)); |
| 46 | |
| 47 | struct microcode_header { |
| 48 | uint32_t version; |
| 49 | uint32_t revision; |
| 50 | uint32_t date; |
| 51 | uint32_t processor_signature; |
| 52 | uint32_t checksum; |
| 53 | uint32_t loader_revision; |
| 54 | uint32_t processor_flags; |
| 55 | uint32_t data_size; |
| 56 | uint32_t total_size; |
| 57 | uint8_t reserved[12]; |
| 58 | } __attribute__ ((packed)); |
| 59 | |
| 60 | struct microcode_entry { |
| 61 | int offset; |
| 62 | int size; |
| 63 | }; |
| 64 | |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 65 | static inline void *rom_buffer_pointer(struct buffer *buffer, int offset) |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 66 | { |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 67 | return &buffer->data[offset]; |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static inline int fit_entry_size_bytes(struct fit_entry *entry) |
| 71 | { |
| 72 | return (entry->size_reserved & 0xffffff) << 4; |
| 73 | } |
| 74 | |
| 75 | static inline void fit_entry_update_size(struct fit_entry *entry, |
| 76 | int size_bytes) |
| 77 | { |
| 78 | /* Size is multiples of 16 bytes. */ |
| 79 | entry->size_reserved = (size_bytes >> 4) & 0xffffff; |
| 80 | } |
| 81 | |
| 82 | static inline void fit_entry_add_size(struct fit_entry *entry, |
| 83 | int size_bytes) |
| 84 | { |
| 85 | int size = fit_entry_size_bytes(entry); |
| 86 | size += size_bytes; |
| 87 | fit_entry_update_size(entry, size); |
| 88 | } |
| 89 | |
| 90 | static inline int fit_entry_type(struct fit_entry *entry) |
| 91 | { |
| 92 | return entry->type_checksum_valid & ~FIT_ENTRY_CHECKSUM_VALID; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Get an offset from a host pointer. This function assumes the ROM is located |
| 97 | * in the host address space at [4G - romsize -> 4G). It also assume all |
| 98 | * pointers have values within this address range. |
| 99 | */ |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 100 | static inline int ptr_to_offset(fit_offset_converter_t helper, |
| 101 | const struct buffer *region, uint32_t host_ptr) |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 102 | { |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 103 | return helper(region, -host_ptr); |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Get a pointer from an offset. This function assumes the ROM is located |
| 108 | * in the host address space at [4G - romsize -> 4G). It also assume all |
| 109 | * pointers have values within this address range. |
| 110 | */ |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 111 | static inline uint32_t offset_to_ptr(fit_offset_converter_t helper, |
| 112 | const struct buffer *region, int offset) |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 113 | { |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 114 | return -helper(region, offset); |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 115 | } |
| 116 | |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 117 | static struct fit_table *locate_fit_table(fit_offset_converter_t offset_helper, |
| 118 | struct buffer *buffer) |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 119 | { |
| 120 | struct fit_table *table; |
| 121 | uint32_t *fit_pointer; |
| 122 | |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 123 | fit_pointer = rom_buffer_pointer(buffer, |
| 124 | ptr_to_offset(offset_helper, buffer, |
| 125 | FIT_POINTER_LOCATION)); |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 126 | |
| 127 | /* Ensure pointer is below 4GiB and within 16MiB of 4GiB */ |
| 128 | if (fit_pointer[1] != 0 || fit_pointer[0] < FIT_TABLE_LOWEST_ADDRESS) |
| 129 | return NULL; |
| 130 | |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 131 | table = rom_buffer_pointer(buffer, |
| 132 | ptr_to_offset(offset_helper, buffer, *fit_pointer)); |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 133 | |
| 134 | /* Check that the address field has the proper signature. */ |
| 135 | if (strncmp((const char *)&table->header.address, FIT_HEADER_ADDRESS, |
| 136 | sizeof(table->header.address))) |
| 137 | return NULL; |
| 138 | |
| 139 | if (table->header.version != FIT_HEADER_VERSION) |
| 140 | return NULL; |
| 141 | |
| 142 | if (fit_entry_type(&table->header) != FIT_TYPE_HEADER) |
| 143 | return NULL; |
| 144 | |
| 145 | /* Assume that the FIT table only contains the header */ |
| 146 | if (fit_entry_size_bytes(&table->header) != sizeof(struct fit_entry)) |
| 147 | return NULL; |
| 148 | |
| 149 | return table; |
| 150 | } |
| 151 | |
| 152 | static void update_fit_checksum(struct fit_table *fit) |
| 153 | { |
| 154 | int size_bytes; |
| 155 | uint8_t *buffer; |
| 156 | uint8_t result; |
| 157 | int i; |
| 158 | |
| 159 | fit->header.checksum = 0; |
| 160 | size_bytes = fit_entry_size_bytes(&fit->header); |
| 161 | result = 0; |
| 162 | buffer = (void *)fit; |
| 163 | for (i = 0; i < size_bytes; i++) |
| 164 | result += buffer[i]; |
| 165 | fit->header.checksum = -result; |
| 166 | } |
| 167 | |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 168 | static void add_microcodde_entries(struct fit_table *fit, |
| 169 | const struct cbfs_image *image, |
| 170 | int num_mcus, struct microcode_entry *mcus, |
| 171 | fit_offset_converter_t offset_helper) |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 172 | { |
| 173 | int i; |
| 174 | |
| 175 | for (i = 0; i < num_mcus; i++) { |
| 176 | struct fit_entry *entry = &fit->entries[i]; |
| 177 | struct microcode_entry *mcu = &mcus[i]; |
| 178 | |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 179 | entry->address = offset_to_ptr(offset_helper, &image->buffer, |
| 180 | mcu->offset); |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 181 | fit_entry_update_size(entry, mcu->size); |
| 182 | entry->version = FIT_MICROCODE_VERSION; |
| 183 | entry->type_checksum_valid = FIT_TYPE_MICROCODE; |
| 184 | entry->checksum = 0; |
| 185 | fit_entry_add_size(&fit->header, sizeof(struct fit_entry)); |
| 186 | } |
| 187 | } |
| 188 | |
Furquan Shaikh | 1ac194e | 2016-05-23 08:44:46 -0700 | [diff] [blame] | 189 | static void cbfs_file_get_header(struct buffer *buf, struct cbfs_file *file) |
| 190 | { |
| 191 | bgets(buf, &file->magic, sizeof(file->magic)); |
| 192 | file->len = xdr_be.get32(buf); |
| 193 | file->type = xdr_be.get32(buf); |
| 194 | file->attributes_offset = xdr_be.get32(buf); |
| 195 | file->offset = xdr_be.get32(buf); |
| 196 | } |
| 197 | |
Ronald G. Minnich | 3fcde22 | 2014-02-04 17:35:44 -0800 | [diff] [blame] | 198 | static int fit_header(void *ptr, uint32_t *current_offset, uint32_t *file_length) |
| 199 | { |
| 200 | struct buffer buf; |
| 201 | struct cbfs_file header; |
| 202 | buf.data = ptr; |
| 203 | buf.size = sizeof(header); |
| 204 | cbfs_file_get_header(&buf, &header); |
| 205 | *current_offset = header.offset; |
| 206 | *file_length = header.len; |
| 207 | return 0; |
| 208 | } |
| 209 | |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 210 | static int parse_microcode_blob(struct cbfs_image *image, |
| 211 | struct cbfs_file *mcode_file, |
| 212 | struct microcode_entry *mcus, int *total_mcus) |
| 213 | { |
| 214 | int num_mcus; |
Ronald G. Minnich | 3fcde22 | 2014-02-04 17:35:44 -0800 | [diff] [blame] | 215 | uint32_t current_offset; |
| 216 | uint32_t file_length; |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 217 | |
Ronald G. Minnich | 3fcde22 | 2014-02-04 17:35:44 -0800 | [diff] [blame] | 218 | fit_header(mcode_file, ¤t_offset, &file_length); |
Kyösti Mälkki | f9b8ed8 | 2014-12-27 13:08:54 +0200 | [diff] [blame] | 219 | current_offset += (int)((char *)mcode_file - image->buffer.data); |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 220 | |
| 221 | num_mcus = 0; |
| 222 | while (file_length > sizeof(struct microcode_header)) |
| 223 | { |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 224 | const struct microcode_header *mcu_header; |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 225 | |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 226 | mcu_header = rom_buffer_pointer(&image->buffer, current_offset); |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 227 | |
Aaron Durbin | 8a0cb8d | 2013-05-07 11:14:01 -0500 | [diff] [blame] | 228 | /* Quickly sanity check a prospective microcode update. */ |
| 229 | if (mcu_header->total_size < sizeof(*mcu_header)) |
| 230 | break; |
| 231 | |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 232 | /* FIXME: Should the checksum be validated? */ |
| 233 | mcus[num_mcus].offset = current_offset; |
| 234 | mcus[num_mcus].size = mcu_header->total_size; |
| 235 | |
| 236 | /* Proceed to next payload. */ |
| 237 | current_offset += mcus[num_mcus].size; |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 238 | file_length -= mcus[num_mcus].size; |
Alexandru Gagniuc | f87c20a | 2013-12-08 17:46:40 -0600 | [diff] [blame] | 239 | num_mcus++; |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 240 | |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 241 | /* Reached limit of FIT entries. */ |
| 242 | if (num_mcus == *total_mcus) |
| 243 | break; |
| 244 | if (file_length < sizeof(struct microcode_header)) |
| 245 | break; |
| 246 | } |
| 247 | |
| 248 | /* Update how many microcode updates we found. */ |
| 249 | *total_mcus = num_mcus; |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 254 | int fit_update_table(struct buffer *bootblock, struct cbfs_image *image, |
| 255 | const char *microcode_blob_name, int empty_entries, |
| 256 | fit_offset_converter_t offset_fn) |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 257 | { |
| 258 | struct fit_table *fit; |
| 259 | struct cbfs_file *mcode_file; |
| 260 | struct microcode_entry *mcus; |
| 261 | int ret = 0; |
| 262 | // struct rom_image image = { .rom = rom, .size = romsize, }; |
| 263 | |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 264 | fit = locate_fit_table(offset_fn, bootblock); |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 265 | |
| 266 | if (!fit) { |
| 267 | ERROR("FIT not found.\n"); |
| 268 | return 1; |
| 269 | } |
| 270 | |
| 271 | mcode_file = cbfs_get_entry(image, microcode_blob_name); |
| 272 | if (!mcode_file) { |
| 273 | ERROR("File '%s' not found in CBFS.\n", |
| 274 | microcode_blob_name); |
| 275 | return 1; |
| 276 | } |
| 277 | |
| 278 | mcus = malloc(sizeof(*mcus) * empty_entries); |
| 279 | |
| 280 | if (!mcus) { |
| 281 | ERROR("Couldn't allocate memory for microcode update entries.\n"); |
| 282 | return 1; |
| 283 | } |
| 284 | |
| 285 | if (parse_microcode_blob(image, mcode_file, mcus, &empty_entries)) { |
| 286 | ERROR("Couldn't parse microcode blob.\n"); |
| 287 | ret = 1; |
| 288 | goto out; |
| 289 | } |
| 290 | |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 291 | add_microcodde_entries(fit, image, empty_entries, mcus, offset_fn); |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 292 | update_fit_checksum(fit); |
| 293 | |
| 294 | out: |
| 295 | free(mcus); |
| 296 | return ret; |
| 297 | } |