Rename __attribute__((packed)) --> __packed
[coreboot.git] / util / cbfstool / fit.c
blobe3e6c32a6f1034831887ba4de8c5170bcdcac026
1 /*
2 * Firmware Interface Table support.
4 * Copyright (C) 2012 Google Inc.
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.
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.
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <compiler.h>
22 #include "fit.h"
24 /* FIXME: This code assumes it is being executed on a little endian machine. */
26 #define FIT_POINTER_LOCATION 0xffffffc0
27 #define FIT_TABLE_LOWEST_ADDRESS ((uint32_t)(-(16 << 20)))
28 #define FIT_ENTRY_CHECKSUM_VALID 0x80
29 #define FIT_TYPE_HEADER 0x0
30 #define FIT_HEADER_VERSION 0x0100
31 #define FIT_HEADER_ADDRESS "_FIT_ "
32 #define FIT_TYPE_MICROCODE 0x1
33 #define FIT_MICROCODE_VERSION 0x0100
35 struct fit_entry {
36 uint64_t address;
37 uint32_t size_reserved;
38 uint16_t version;
39 uint8_t type_checksum_valid;
40 uint8_t checksum;
41 } __packed;
43 struct fit_table {
44 struct fit_entry header;
45 struct fit_entry entries[];
46 } __packed;
48 struct microcode_header {
49 uint32_t version;
50 uint32_t revision;
51 uint32_t date;
52 uint32_t processor_signature;
53 uint32_t checksum;
54 uint32_t loader_revision;
55 uint32_t processor_flags;
56 uint32_t data_size;
57 uint32_t total_size;
58 uint8_t reserved[12];
59 } __packed;
61 struct microcode_entry {
62 int offset;
63 int size;
66 static inline void *rom_buffer_pointer(struct buffer *buffer, int offset)
68 return &buffer->data[offset];
71 static inline int fit_entry_size_bytes(struct fit_entry *entry)
73 return (entry->size_reserved & 0xffffff) << 4;
76 static inline void fit_entry_update_size(struct fit_entry *entry,
77 int size_bytes)
79 /* Size is multiples of 16 bytes. */
80 entry->size_reserved = (size_bytes >> 4) & 0xffffff;
83 static inline void fit_entry_add_size(struct fit_entry *entry,
84 int size_bytes)
86 int size = fit_entry_size_bytes(entry);
87 size += size_bytes;
88 fit_entry_update_size(entry, size);
91 static inline int fit_entry_type(struct fit_entry *entry)
93 return entry->type_checksum_valid & ~FIT_ENTRY_CHECKSUM_VALID;
97 * Get an offset from a host pointer. This function assumes the ROM is located
98 * in the host address space at [4G - romsize -> 4G). It also assume all
99 * pointers have values within this address range.
101 static inline int ptr_to_offset(fit_offset_converter_t helper,
102 const struct buffer *region, uint32_t host_ptr)
104 return helper(region, -host_ptr);
108 * Get a pointer from an offset. This function assumes the ROM is located
109 * in the host address space at [4G - romsize -> 4G). It also assume all
110 * pointers have values within this address range.
112 static inline uint32_t offset_to_ptr(fit_offset_converter_t helper,
113 const struct buffer *region, int offset)
115 return -helper(region, offset);
118 static struct fit_table *locate_fit_table(fit_offset_converter_t offset_helper,
119 struct buffer *buffer)
121 struct fit_table *table;
122 uint32_t *fit_pointer;
124 fit_pointer = rom_buffer_pointer(buffer,
125 ptr_to_offset(offset_helper, buffer,
126 FIT_POINTER_LOCATION));
128 /* Ensure pointer is below 4GiB and within 16MiB of 4GiB */
129 if (fit_pointer[1] != 0 || fit_pointer[0] < FIT_TABLE_LOWEST_ADDRESS)
130 return NULL;
132 table = rom_buffer_pointer(buffer,
133 ptr_to_offset(offset_helper, buffer, *fit_pointer));
135 /* Check that the address field has the proper signature. */
136 if (strncmp((const char *)&table->header.address, FIT_HEADER_ADDRESS,
137 sizeof(table->header.address)))
138 return NULL;
140 if (table->header.version != FIT_HEADER_VERSION)
141 return NULL;
143 if (fit_entry_type(&table->header) != FIT_TYPE_HEADER)
144 return NULL;
146 /* Assume that the FIT table only contains the header */
147 if (fit_entry_size_bytes(&table->header) != sizeof(struct fit_entry))
148 return NULL;
150 return table;
153 static void update_fit_checksum(struct fit_table *fit)
155 int size_bytes;
156 uint8_t *buffer;
157 uint8_t result;
158 int i;
160 fit->header.checksum = 0;
161 size_bytes = fit_entry_size_bytes(&fit->header);
162 result = 0;
163 buffer = (void *)fit;
164 for (i = 0; i < size_bytes; i++)
165 result += buffer[i];
166 fit->header.checksum = -result;
169 static void add_microcodde_entries(struct fit_table *fit,
170 const struct cbfs_image *image,
171 int num_mcus, struct microcode_entry *mcus,
172 fit_offset_converter_t offset_helper)
174 int i;
176 for (i = 0; i < num_mcus; i++) {
177 struct fit_entry *entry = &fit->entries[i];
178 struct microcode_entry *mcu = &mcus[i];
180 entry->address = offset_to_ptr(offset_helper, &image->buffer,
181 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));
190 static void cbfs_file_get_header(struct buffer *buf, struct cbfs_file *file)
192 bgets(buf, &file->magic, sizeof(file->magic));
193 file->len = xdr_be.get32(buf);
194 file->type = xdr_be.get32(buf);
195 file->attributes_offset = xdr_be.get32(buf);
196 file->offset = xdr_be.get32(buf);
199 static int fit_header(void *ptr, uint32_t *current_offset, uint32_t *file_length)
201 struct buffer buf;
202 struct cbfs_file header;
203 buf.data = ptr;
204 buf.size = sizeof(header);
205 cbfs_file_get_header(&buf, &header);
206 *current_offset = header.offset;
207 *file_length = header.len;
208 return 0;
211 static int parse_microcode_blob(struct cbfs_image *image,
212 struct cbfs_file *mcode_file,
213 struct microcode_entry *mcus, int *total_mcus)
215 int num_mcus;
216 uint32_t current_offset;
217 uint32_t file_length;
219 fit_header(mcode_file, &current_offset, &file_length);
220 current_offset += (int)((char *)mcode_file - image->buffer.data);
222 num_mcus = 0;
223 while (file_length > sizeof(struct microcode_header))
225 const struct microcode_header *mcu_header;
227 mcu_header = rom_buffer_pointer(&image->buffer, current_offset);
229 /* Quickly sanity check a prospective microcode update. */
230 if (mcu_header->total_size < sizeof(*mcu_header))
231 break;
233 /* FIXME: Should the checksum be validated? */
234 mcus[num_mcus].offset = current_offset;
235 mcus[num_mcus].size = mcu_header->total_size;
237 /* Proceed to next payload. */
238 current_offset += mcus[num_mcus].size;
239 file_length -= mcus[num_mcus].size;
240 num_mcus++;
242 /* Reached limit of FIT entries. */
243 if (num_mcus == *total_mcus)
244 break;
245 if (file_length < sizeof(struct microcode_header))
246 break;
249 /* Update how many microcode updates we found. */
250 *total_mcus = num_mcus;
252 return 0;
255 int fit_update_table(struct buffer *bootblock, struct cbfs_image *image,
256 const char *microcode_blob_name, int empty_entries,
257 fit_offset_converter_t offset_fn)
259 struct fit_table *fit;
260 struct cbfs_file *mcode_file;
261 struct microcode_entry *mcus;
262 int ret = 0;
263 // struct rom_image image = { .rom = rom, .size = romsize, };
265 fit = locate_fit_table(offset_fn, bootblock);
267 if (!fit) {
268 ERROR("FIT not found.\n");
269 return 1;
272 mcode_file = cbfs_get_entry(image, microcode_blob_name);
273 if (!mcode_file) {
274 ERROR("File '%s' not found in CBFS.\n",
275 microcode_blob_name);
276 return 1;
279 mcus = malloc(sizeof(*mcus) * empty_entries);
281 if (!mcus) {
282 ERROR("Couldn't allocate memory for microcode update entries.\n");
283 return 1;
286 if (parse_microcode_blob(image, mcode_file, mcus, &empty_entries)) {
287 ERROR("Couldn't parse microcode blob.\n");
288 ret = 1;
289 goto out;
292 add_microcodde_entries(fit, image, empty_entries, mcus, offset_fn);
293 update_fit_checksum(fit);
295 out:
296 free(mcus);
297 return ret;