blob: 2cc2560d338eb13b2440afc3438ae8ff7e2875e6 [file] [log] [blame]
Lee Leahyb5ad8272015-04-20 15:29:16 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2015 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.
18 */
19
20#include <console/console.h>
21#include <cbmem.h>
Aaron Durbin789f2b62015-09-09 17:05:06 -050022#include <fsp/util.h>
Lee Leahyb5ad8272015-04-20 15:29:16 -070023#include <stdlib.h>
24#include <stdint.h>
25#include <string.h>
Lee Leahyb5ad8272015-04-20 15:29:16 -070026
27#define FSP_DBG_LVL BIOS_NEVER
28
29static const EFI_GUID ffs2_guid = EFI_FIRMWARE_FILE_SYSTEM2_GUID;
30static const EFI_GUID fih_guid = FSP_INFO_HEADER_GUID;
31
32struct fsp_patch_table {
33 uint32_t signature;
34 uint16_t header_length;
35 uint8_t header_revision;
36 uint8_t reserved;
37 uint32_t patch_entry_num;
38 uint32_t patch_entries[0];
39} __attribute__((packed));
40
41#define FSPP_SIG 0x50505346
42
43static void *relative_offset(void *base, ssize_t offset)
44{
45 uintptr_t loc;
46
47 loc = (uintptr_t)base;
48 loc += offset;
49
50 return (void *)loc;
51}
52
53static uint32_t *fspp_reloc(void *fsp, size_t fsp_size, uint32_t e)
54{
55 size_t offset;
56
57 /* Offsets live in bits 23:0. */
58 offset = e & 0xffffff;
59
60 /* If bit 31 is set then the offset is considered a negative value
61 * relative to the end of the image using 16MiB as the offset's
62 * reference. */
63 if (e & (1 << 31))
64 offset = fsp_size - (16 * MiB - offset);
65
66 /* Determine if offset falls within fsp_size for a 32 bit relocation. */
67 if (offset > fsp_size - sizeof(uint32_t))
68 return NULL;
69
70 return relative_offset(fsp, offset);
71}
72
73static int reloc_type(uint16_t reloc_entry)
74{
75 /* Reloc type in upper 4 bits */
76 return reloc_entry >> 12;
77}
78
79static size_t reloc_offset(uint16_t reloc_entry)
80{
81 /* Offsets are in low 12 bits. */
82 return reloc_entry & ((1 << 12) - 1);
83}
84
85static int te_relocate_in_place(void *te, size_t size)
86{
87 EFI_TE_IMAGE_HEADER *teih;
88 EFI_IMAGE_DATA_DIRECTORY *relocd;
89 EFI_IMAGE_BASE_RELOCATION *relocb;
90 size_t fixup_offset;
91 size_t num_relocs;
92 uint16_t *reloc;
93 size_t relocd_offset;
94 uint8_t *te_base;
95 uint32_t adj;
96
97 teih = te;
98
99 if (teih->Signature != EFI_TE_IMAGE_HEADER_SIGNATURE) {
100 printk(BIOS_ERR, "TE Signature mismatch: %x vs %x\n",
101 teih->Signature, EFI_TE_IMAGE_HEADER_SIGNATURE);
102 return -1;
103 }
104
105 /*
106 * A TE image is created by converting a PE file. Because of this
107 * the offsets within the headers are off. In order to calculate
108 * the correct releative offets one needs to subtract fixup_offset
109 * from the encoded offets. Similarly, the linked address of the
110 * program is found by adding the fixup_offset to the ImageBase.
111 */
112 fixup_offset = teih->StrippedSize - sizeof(EFI_TE_IMAGE_HEADER);
113 /* Keep track of a base that is correctly adjusted so that offsets
114 * can be used directly. */
115 te_base = te;
116 te_base -= fixup_offset;
117
118 adj = (uintptr_t)te - (teih->ImageBase + fixup_offset);
119
120 printk(FSP_DBG_LVL, "TE Image %p -> %p adjust value: %x\n",
121 (void *)(uintptr_t)(teih->ImageBase + fixup_offset),
122 te, adj);
123
124 /* Adjust ImageBase for consistency. */
125 teih->ImageBase = (uint32_t)(teih->ImageBase + adj);
126
127 relocd = &teih->DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC];
128
129 relocd_offset = 0;
130 /* Though the field name is VirtualAddress it's actually relative to
131 * the beginning of the image which is linked at ImageBase. */
132 relocb = relative_offset(te, relocd->VirtualAddress - fixup_offset);
133 while (relocd_offset < relocd->Size) {
134 size_t rva_offset = relocb->VirtualAddress;
135
136 printk(FSP_DBG_LVL, "Relocs for RVA offset %zx\n", rva_offset);
137 num_relocs = relocb->SizeOfBlock - sizeof(*relocb);
138 num_relocs /= sizeof(uint16_t);
139 reloc = relative_offset(relocb, sizeof(*relocb));
140
141 printk(FSP_DBG_LVL, "Num relocs in block: %zx\n", num_relocs);
142
143 while (num_relocs > 0) {
144 int type = reloc_type(*reloc);
145 size_t offset = reloc_offset(*reloc);
146
147 printk(FSP_DBG_LVL, "reloc type %x offset %zx\n",
148 type, offset);
149
150 if (type == EFI_IMAGE_REL_BASED_HIGHLOW) {
151 uint32_t *reloc_addr;
152
153 offset += rva_offset;
154 reloc_addr = (void *)&te_base[offset];
155
156 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
157 reloc_addr, *reloc_addr,
158 *reloc_addr + adj);
159 *reloc_addr += adj;
160 } else if (type != EFI_IMAGE_REL_BASED_ABSOLUTE) {
161 printk(BIOS_ERR, "Unknown reloc type: %x\n",
162 type);
163 return -1;
164 }
165 num_relocs--;
166 reloc++;
167 }
168
169 /* Track consumption of relocation directory contents. */
170 relocd_offset += relocb->SizeOfBlock;
171 /* Get next relocation block to process. */
172 relocb = relative_offset(relocb, relocb->SizeOfBlock);
173 }
174
175 return 0;
176}
177
178static size_t csh_size(const EFI_COMMON_SECTION_HEADER *csh)
179{
180 size_t size;
181
182 /* Unpack the array into a type that can be used. */
183 size = 0;
184 size |= csh->Size[0] << 0;
185 size |= csh->Size[1] << 8;
186 size |= csh->Size[2] << 16;
187
188 return size;
189}
190
191static size_t section_data_offset(const EFI_COMMON_SECTION_HEADER *csh)
192{
193 if (csh_size(csh) == 0x00ffffff)
194 return sizeof(EFI_COMMON_SECTION_HEADER2);
195 else
196 return sizeof(EFI_COMMON_SECTION_HEADER);
197}
198
199static size_t section_data_size(const EFI_COMMON_SECTION_HEADER *csh)
200{
201 size_t section_size;
202
203 if (csh_size(csh) == 0x00ffffff)
204 section_size = SECTION2_SIZE(csh);
205 else
206 section_size = csh_size(csh);
207
208 return section_size - section_data_offset(csh);
209}
210
211static size_t file_section_offset(const EFI_FFS_FILE_HEADER *ffsfh)
212{
213 if (IS_FFS_FILE2(ffsfh))
214 return sizeof(EFI_FFS_FILE_HEADER2);
215 else
216 return sizeof(EFI_FFS_FILE_HEADER);
217}
218
219static size_t ffs_file_size(const EFI_FFS_FILE_HEADER *ffsfh)
220{
221 size_t size;
222
223 if (IS_FFS_FILE2(ffsfh))
224 size = FFS_FILE2_SIZE(ffsfh);
225 else {
226 size = ffsfh->Size[0] << 0;
227 size |= ffsfh->Size[1] << 8;
228 size |= ffsfh->Size[2] << 16;
229 }
230 return size;
231}
232
233static int relocate_patch_table(void *fsp, size_t size, size_t offset,
234 ssize_t adjustment)
235{
236 struct fsp_patch_table *table;
237 uint32_t num;
238
239 table = relative_offset(fsp, offset);
240
241 if ((offset + sizeof(*table) > size) ||
242 (table->header_length + offset) > size) {
243 printk(BIOS_ERR, "FSPP not entirely contained in region.\n");
244 return -1;
245 }
246
247 printk(FSP_DBG_LVL, "FSPP relocs: %x\n", table->patch_entry_num);
248
249 for (num = 0; num < table->patch_entry_num; num++) {
250 uint32_t *reloc;
251
252 reloc = fspp_reloc(fsp, size, table->patch_entries[num]);
253
254 if (reloc == NULL) {
255 printk(BIOS_ERR, "Ignoring FSPP entry: %x\n",
256 table->patch_entries[num]);
257 continue;
258 }
259
260 printk(FSP_DBG_LVL, "Adjusting %p %x -> %x\n",
261 reloc, *reloc, (unsigned int)(*reloc + adjustment));
262
263 *reloc += adjustment;
264 }
265
266 return 0;
267}
268
269static void *relocate_remaining_items(void *fsp, size_t size, size_t fih_offset)
270{
271 EFI_FFS_FILE_HEADER *ffsfh;
272 EFI_COMMON_SECTION_HEADER *csh;
273 FSP_INFO_HEADER *fih;
274 ssize_t adjustment;
275 size_t offset;
276
277 printk(FSP_DBG_LVL, "FSP_INFO_HEADER offset is %zx\n", fih_offset);
278
279 if (fih_offset == 0) {
280 printk(BIOS_ERR, "FSP_INFO_HEADER offset is 0.\n");
281 return NULL;
282 }
283
284 /* FSP_INFO_HEADER at first file in FV within first RAW section. */
285 ffsfh = relative_offset(fsp, fih_offset);
286 fih_offset += file_section_offset(ffsfh);
287 csh = relative_offset(fsp, fih_offset);
288 fih_offset += section_data_offset(csh);
289 fih = relative_offset(fsp, fih_offset);
290
291 if (memcmp(&ffsfh->Name, &fih_guid, sizeof(fih_guid))) {
292 printk(BIOS_ERR, "Bad FIH GUID.\n");
293 return NULL;
294 }
295
296 if (csh->Type != EFI_SECTION_RAW) {
297 printk(BIOS_ERR, "FIH file should have raw section: %x\n",
298 csh->Type);
299 return NULL;
300 }
301
302 if (fih->Signature != FSP_SIG) {
303 printk(BIOS_ERR, "Unexpected FIH signature: %08x\n",
304 fih->Signature);
305 return NULL;
306 }
307
308 adjustment = (intptr_t)fsp - fih->ImageBase;
309
310 /* Update ImageBase to reflect FSP's new home. */
311 fih->ImageBase += adjustment;
312
313 /* Need to find patch table and adjust each entry. The tables
314 * following FSP_INFO_HEADER have a 32-bit signature and header
315 * length. The patch table is denoted as having a 'FSPP' signature;
316 * the table format doesn't follow the other tables. */
317 offset = fih_offset + fih->HeaderLength;
318 while (offset + 2 * sizeof(uint32_t) <= size) {
319 uint32_t *table_headers;
320
321 table_headers = relative_offset(fsp, offset);
322
323 printk(FSP_DBG_LVL, "Checking offset %zx for 'FSPP'\n",
324 offset);
325
326 if (table_headers[0] != FSPP_SIG) {
327 offset += table_headers[1];
328 continue;
329 }
330
331 if (relocate_patch_table(fsp, size, offset, adjustment)) {
332 printk(BIOS_ERR, "FSPP relocation failed.\n");
333 return NULL;
334 }
335
336 return fih;
337 }
338
339 printk(BIOS_ERR, "Could not find the FSP patch table.\n");
340 return NULL;
341}
342
343static ssize_t relocate_fvh(void *fsp, size_t fsp_size, size_t fvh_offset,
344 size_t *fih_offset)
345{
346 EFI_FIRMWARE_VOLUME_HEADER *fvh;
347 EFI_FFS_FILE_HEADER *ffsfh;
348 EFI_COMMON_SECTION_HEADER *csh;
349 size_t offset;
350 size_t file_offset;
351 size_t size;
352
353 offset = fvh_offset;
354 fvh = relative_offset(fsp, offset);
355
356 if (fvh->Signature != EFI_FVH_SIGNATURE)
357 return -1;
358
359 printk(FSP_DBG_LVL, "FVH length: %zx Offset: %zx Mapping length: %zx\n",
360 (size_t)fvh->FvLength, offset, fsp_size);
361
362 if (fvh->FvLength + offset > fsp_size)
363 return -1;
364
365 /* Parse only this FV. However, the algorithm uses offsets into the
366 * entire FSP region so make size include the starting offset. */
367 size = fvh->FvLength + offset;
368
369 if (memcmp(&fvh->FileSystemGuid, &ffs2_guid, sizeof(ffs2_guid))) {
370 printk(BIOS_ERR, "FVH not an FFS2 type.\n");
371 return -1;
372 }
373
374 if (fvh->ExtHeaderOffset != 0) {
375 EFI_FIRMWARE_VOLUME_EXT_HEADER *fveh;
376
377 offset += fvh->ExtHeaderOffset;
378 fveh = relative_offset(fsp, offset);
379 printk(FSP_DBG_LVL, "Extended Header Offset: %zx Size: %zx\n",
380 (size_t)fvh->ExtHeaderOffset,
381 (size_t)fveh->ExtHeaderSize);
382 offset += fveh->ExtHeaderSize;
383 /* FFS files are 8 byte aligned after extended header. */
384 offset = ALIGN_UP(offset, 8);
385 } else {
386 offset += fvh->HeaderLength;
387 }
388
389 file_offset = offset;
390 while (file_offset + sizeof(*ffsfh) < size) {
391 offset = file_offset;
392 printk(FSP_DBG_LVL, "file offset: %zx\n", file_offset);
393
394 /* First file and section should be FSP info header. */
395 if (fih_offset != NULL && *fih_offset == 0)
396 *fih_offset = file_offset;
397
398 ffsfh = relative_offset(fsp, file_offset);
399
400 printk(FSP_DBG_LVL, "file type = %x\n", ffsfh->Type);
401 printk(FSP_DBG_LVL, "file attribs = %x\n", ffsfh->Attributes);
402
403 /* Exit FV relocation when empty space found */
404 if (ffsfh->Type == EFI_FV_FILETYPE_FFS_MAX)
405 break;
406
407 /* Next file on 8 byte alignment. */
408 file_offset += ffs_file_size(ffsfh);
409 file_offset = ALIGN_UP(file_offset, 8);
410
411 /* Padding files have no section information. */
412 if (ffsfh->Type == EFI_FV_FILETYPE_FFS_PAD)
413 continue;
414
415 offset += file_section_offset(ffsfh);
416
417 while (offset + sizeof(*csh) < file_offset) {
418 size_t data_size;
419 size_t data_offset;
420
421 csh = relative_offset(fsp, offset);
422
423 printk(FSP_DBG_LVL, "section offset: %zx\n", offset);
424 printk(FSP_DBG_LVL, "section type: %x\n", csh->Type);
425
426 data_size = section_data_size(csh);
427 data_offset = section_data_offset(csh);
428
429 if (data_size + data_offset + offset > file_offset) {
430 printk(BIOS_ERR, "Section exceeds FV size.\n");
431 return -1;
432 }
433
434 if (csh->Type == EFI_SECTION_TE) {
435 void *te;
436 size_t te_offset = offset + data_offset;
437
438 printk(FSP_DBG_LVL, "TE image at offset %zx\n",
439 te_offset);
440 te = relative_offset(fsp, te_offset);
441 te_relocate_in_place(te, data_size);
442 }
443
444 offset += data_size + data_offset;
445 /* Sections are aligned to 4 bytes. */
446 offset = ALIGN_UP(offset, 4);
447 }
448 }
449
450 /* Return amount of buffer parsed: FV size. */
451 return fvh->FvLength;
452}
453
454static FSP_INFO_HEADER *fsp_relocate_in_place(void *fsp, size_t size)
455{
456 size_t offset;
457 size_t fih_offset;
458
459 offset = 0;
460 fih_offset = 0;
461 while (offset < size) {
462 ssize_t nparsed;
463
464 /* Relocate each FV within the FSP region. The FSP_INFO_HEADER
465 * should only be located in the first FV. */
466 if (offset == 0)
467 nparsed = relocate_fvh(fsp, size, offset, &fih_offset);
468 else
469 nparsed = relocate_fvh(fsp, size, offset, NULL);
470
471 /* FV should be larger than 0 or failed to parse. */
472 if (nparsed <= 0) {
473 printk(BIOS_ERR, "FV @ offset %zx relocation failed\n",
474 offset);
475 return NULL;
476 }
477
478 offset += nparsed;
479 }
480
481 return relocate_remaining_items(fsp, size, fih_offset);
482}
483
Aaron Durbin22ea0072015-08-05 10:17:33 -0500484int fsp_relocate(struct prog *fsp_relocd, const struct region_device *fsp_src)
Lee Leahyb5ad8272015-04-20 15:29:16 -0700485{
486 void *new_loc;
Aaron Durbin22ea0072015-08-05 10:17:33 -0500487 void *fih;
488 size_t size = region_device_sz(fsp_src);
Lee Leahyb5ad8272015-04-20 15:29:16 -0700489
490 new_loc = cbmem_add(CBMEM_ID_REFCODE, size);
Aaron Durbin22ea0072015-08-05 10:17:33 -0500491
Lee Leahyb5ad8272015-04-20 15:29:16 -0700492 if (new_loc == NULL) {
Aaron Durbin22ea0072015-08-05 10:17:33 -0500493 printk(BIOS_ERR, "ERROR: Unable to load FSP into memory.\n");
494 return -1;
Lee Leahyb5ad8272015-04-20 15:29:16 -0700495 }
Aaron Durbin22ea0072015-08-05 10:17:33 -0500496
497 if (rdev_readat(fsp_src, new_loc, 0, size) != size) {
498 printk(BIOS_ERR, "ERROR: Can't read FSP's region device.\n");
499 return -1;
500 }
501
502 fih = fsp_relocate_in_place(new_loc, size);
503
504 if (fih == NULL) {
505 printk(BIOS_ERR, "ERROR: FSP relocation faiulre.\n");
506 return -1;
507 }
508
509 prog_set_area(fsp_relocd, new_loc, size);
510 prog_set_entry(fsp_relocd, fih, NULL);
511
512 return 0;
Lee Leahyb5ad8272015-04-20 15:29:16 -0700513}