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