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