Debian Codegraph @

src/lib/coreboot_table.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <acpi/acpi.h>
4 #include <arch/cbconfig.h>
5 #include <commonlib/bsd/ipchksum.h>
6 #include <console/console.h>
7 #include <console/uart.h>
8 #include <identity.h>
9 #include <boot/coreboot_tables.h>
10 #include <boot/tables.h>
11 #include <boot_device.h>
12 #include <string.h>
13 #include <boardid.h>
14 #include <device/device.h>
15 #include <drivers/tpm/tpm_ppi.h>
16 #include <drivers/option/cfr_frontend.h>
17 #include <fmap.h>
18 #include <fw_config.h>
19 #include <cbfs.h>
20 #include <cbmem.h>
21 #include <bootmem.h>
22 #include <bootsplash.h>
23 #include <inttypes.h>
24 #include <spi_flash.h>
25 #include <smmstore.h>
26 #include <types.h>
27 #include <version.h>
28
29 #if CONFIG(USE_OPTION_TABLE)
30 #include <option_table.h>
31 #endif
32 #if CONFIG(PLATFORM_USES_FSP2_0)
33 #include <fsp/util.h>
34 #else
35 void lb_string_platform_blob_version(struct lb_header *header);
36 #endif
37
38 __weak enum cb_err fill_lb_pcie(struct lb_pcie *pcie)
39 {
40 return CB_ERR_NOT_IMPLEMENTED;
41 }
42
43 static struct lb_header *lb_table_init(unsigned long addr)
44 {
45 struct lb_header *header;
46
47 addr = ALIGN_UP(addr, 16);
48
49 header = (void *)addr;
50 header->signature[0] = 'L';
51 header->signature[1] = 'B';
52 header->signature[2] = 'I';
53 header->signature[3] = 'O';
54 header->header_bytes = sizeof(*header);
55 header->header_checksum = 0;
56 header->table_bytes = 0;
57 header->table_checksum = 0;
58 header->table_entries = 0;
59 return header;
60 }
61
62 static struct lb_record *lb_first_record(struct lb_header *header)
63 {
64 struct lb_record *rec;
65 rec = (void *)(((char *)header) + sizeof(*header));
66 return rec;
67 }
68
69 static struct lb_record *lb_last_record(struct lb_header *header)
70 {
71 struct lb_record *rec;
72 rec = (void *)(((char *)header) + sizeof(*header)
73 + header->table_bytes);
74 return rec;
75 }
76
77 struct lb_record *lb_new_record(struct lb_header *header)
78 {
79 struct lb_record *rec;
80 rec = lb_last_record(header);
81 if (header->table_entries) {
82 assert(IS_ALIGNED(rec->size, LB_ENTRY_ALIGN));
83 header->table_bytes += rec->size;
84 rec = lb_last_record(header);
85 }
86 header->table_entries++;
87 rec->tag = LB_TAG_UNUSED;
88 rec->size = sizeof(*rec);
89 return rec;
90 }
91
92 static struct lb_memory *lb_memory(struct lb_header *header)
93 {
94 struct lb_record *rec;
95 struct lb_memory *mem;
96 rec = lb_new_record(header);
97 mem = (struct lb_memory *)rec;
98 mem->tag = LB_TAG_MEMORY;
99 mem->size = sizeof(*mem);
100 return mem;
101 }
102
103 static void lb_add_serial(struct lb_header *header)
104 {
105 struct lb_serial new_serial = { .tag = LB_TAG_SERIAL,
106 .size = sizeof(struct lb_serial),
107 };
108 if (fill_lb_serial(&new_serial) != CB_SUCCESS)
109 return;
110
111 struct lb_serial *serial = (struct lb_serial *)lb_new_record(header);
112 memcpy(serial, &new_serial, sizeof(*serial));
113 assert(serial->type == LB_SERIAL_TYPE_IO_MAPPED
114 || serial->type == LB_SERIAL_TYPE_MEMORY_MAPPED)
115 if (serial->type == LB_SERIAL_TYPE_IO_MAPPED)
116 lb_add_console(LB_TAG_CONSOLE_SERIAL8250, header);
117 else
118 lb_add_console(LB_TAG_CONSOLE_SERIAL8250MEM, header);
119 }
120
121 void lb_add_console(uint16_t consoletype, void *data)
122 {
123 struct lb_header *header = (struct lb_header *)data;
124 struct lb_console *console;
125
126 console = (struct lb_console *)lb_new_record(header);
127 console->tag = LB_TAG_CONSOLE;
128 console->size = sizeof(*console);
129 console->type = consoletype;
130 }
131
132 static void lb_pcie(struct lb_header *header)
133 {
134 struct lb_pcie pcie = { .tag = LB_TAG_PCIE, .size = sizeof(pcie) };
135
136 if (fill_lb_pcie(&pcie) != CB_SUCCESS)
137 return;
138
139 memcpy(lb_new_record(header), &pcie, sizeof(pcie));
140 }
141
142 static void lb_framebuffer(struct lb_header *header)
143 {
144 struct lb_framebuffer *framebuffer;
145 struct lb_framebuffer fb = {0};
146
147 if (!CONFIG(LINEAR_FRAMEBUFFER) || fill_lb_framebuffer(&fb))
148 return;
149
150 framebuffer = (struct lb_framebuffer *)lb_new_record(header);
151 memcpy(framebuffer, &fb, sizeof(*framebuffer));
152 framebuffer->tag = LB_TAG_FRAMEBUFFER;
153 framebuffer->size = sizeof(*framebuffer);
154
155 if (CONFIG(BOOTSPLASH)) {
156 uint8_t *fb_ptr = (uint8_t *)(uintptr_t)framebuffer->physical_address;
157 unsigned int width = framebuffer->x_resolution;
158 unsigned int height = framebuffer->y_resolution;
159 unsigned int bytes_per_line = framebuffer->bytes_per_line;
160 unsigned int depth = framebuffer->bits_per_pixel;
161 set_bootsplash(fb_ptr, width, height, bytes_per_line, depth);
162 }
163 }
164
165 void lb_add_gpios(struct lb_gpios *gpios, const struct lb_gpio *gpio_table,
166 size_t count)
167 {
168 size_t table_size = count * sizeof(struct lb_gpio);
169
170 memcpy(&gpios->gpios[gpios->count], gpio_table, table_size);
171 gpios->count += count;
172 gpios->size += table_size;
173 }
174
175 static void lb_gpios(struct lb_header *header)
176 {
177 struct lb_gpios *gpios;
178 struct lb_gpio *g;
179
180 gpios = (struct lb_gpios *)lb_new_record(header);
181 gpios->tag = LB_TAG_GPIO;
182 gpios->size = sizeof(*gpios);
183 gpios->count = 0;
184 fill_lb_gpios(gpios);
185
186 printk(BIOS_INFO, "Passing %u GPIOs to payload:\n"
187 " NAME | PORT | POLARITY | VALUE\n",
188 gpios->count);
189 for (g = &gpios->gpios[0]; g < &gpios->gpios[gpios->count]; g++) {
190 printk(BIOS_INFO, "%16.16s | ", g->name);
191 if (g->port == -1)
192 printk(BIOS_INFO, " undefined | ");
193 else
194 printk(BIOS_INFO, "%#.8x | ", g->port);
195 if (g->polarity == ACTIVE_HIGH)
196 printk(BIOS_INFO, " high | ");
197 else
198 printk(BIOS_INFO, " low | ");
199 switch (g->value) {
200 case 0:
201 printk(BIOS_INFO, " low\n");
202 break;
203 case 1:
204 printk(BIOS_INFO, " high\n");
205 break;
206 default:
207 printk(BIOS_INFO, "undefined\n");
208 break;
209 }
210 }
211 }
212
213 __weak uint32_t board_id(void) { return UNDEFINED_STRAPPING_ID; }
214 __weak uint32_t ram_code(void) { return UNDEFINED_STRAPPING_ID; }
215 __weak uint32_t sku_id(void) { return UNDEFINED_STRAPPING_ID; }
216 __weak uint64_t fw_config_get(void) { return UNDEFINED_FW_CONFIG; }
217
218 static void lb_boot_media_params(struct lb_header *header)
219 {
220 struct lb_boot_media_params *bmp;
221 const struct region_device *boot_dev;
222 const struct cbfs_boot_device *cbd = cbfs_get_boot_device(false);
223 if (!cbd)
224 return;
225
226 boot_dev = boot_device_ro();
227 if (boot_dev == NULL)
228 return;
229
230 bmp = (struct lb_boot_media_params *)lb_new_record(header);
231 bmp->tag = LB_TAG_BOOT_MEDIA_PARAMS;
232 bmp->size = sizeof(*bmp);
233
234 bmp->cbfs_offset = region_device_offset(&cbd->rdev);
235 bmp->cbfs_size = region_device_sz(&cbd->rdev);
236 bmp->boot_media_size = region_device_sz(boot_dev);
237
238 bmp->fmap_offset = get_fmap_flash_offset();
239 }
240
241 static void lb_mmc_info(struct lb_header *header)
242 {
243 struct lb_mmc_info *rec;
244 int32_t *ms_cbmem;
245
246 ms_cbmem = cbmem_find(CBMEM_ID_MMC_STATUS);
247 if (!ms_cbmem)
248 return;
249
250 rec = (struct lb_mmc_info *)lb_new_record(header);
251
252 rec->tag = LB_TAG_MMC_INFO;
253 rec->size = sizeof(*rec);
254 rec->early_cmd1_status = *ms_cbmem;
255 }
256
257 static void add_cbmem_pointers(struct lb_header *header)
258 {
259 /*
260 * These CBMEM sections' addresses are included in the coreboot table
261 * with the appropriate tags.
262 */
263 const struct section_id {
264 int cbmem_id;
265 int table_tag;
266 } section_ids[] = {
267 {CBMEM_ID_TIMESTAMP, LB_TAG_TIMESTAMPS},
268 {CBMEM_ID_CONSOLE, LB_TAG_CBMEM_CONSOLE},
269 {CBMEM_ID_ACPI_GNVS, LB_TAG_ACPI_GNVS},
270 {CBMEM_ID_ACPI_CNVS, LB_TAG_ACPI_CNVS},
271 {CBMEM_ID_VPD, LB_TAG_VPD},
272 {CBMEM_ID_WIFI_CALIBRATION, LB_TAG_WIFI_CALIBRATION},
273 {CBMEM_ID_TPM_CB_LOG, LB_TAG_TPM_CB_LOG},
274 {CBMEM_ID_FMAP, LB_TAG_FMAP},
275 {CBMEM_ID_VBOOT_WORKBUF, LB_TAG_VBOOT_WORKBUF},
276 {CBMEM_ID_TYPE_C_INFO, LB_TAG_TYPE_C_INFO},
277 };
278 int i;
279
280 for (i = 0; i < ARRAY_SIZE(section_ids); i++) {
281 const struct section_id *sid = section_ids + i;
282 struct lb_cbmem_ref *cbmem_ref;
283 void *cbmem_addr = cbmem_find(sid->cbmem_id);
284
285 if (!cbmem_addr)
286 continue; /* This section is not present */
287
288 cbmem_ref = (struct lb_cbmem_ref *)lb_new_record(header);
289 if (!cbmem_ref) {
290 printk(BIOS_ERR, "No more room in coreboot table!\n");
291 break;
292 }
293 cbmem_ref->tag = sid->table_tag;
294 cbmem_ref->size = sizeof(*cbmem_ref);
295 cbmem_ref->cbmem_addr = (unsigned long)cbmem_addr;
296 }
297 }
298
299 static struct lb_mainboard *lb_mainboard(struct lb_header *header)
300 {
301 struct lb_record *rec;
302 struct lb_mainboard *mainboard;
303 rec = lb_new_record(header);
304 mainboard = (struct lb_mainboard *)rec;
305 mainboard->tag = LB_TAG_MAINBOARD;
306
307 mainboard->size = ALIGN_UP(sizeof(*mainboard) +
308 strlen(mainboard_vendor) + 1 +
309 strlen(mainboard_part_number) + 1, LB_ENTRY_ALIGN);
310
311 mainboard->vendor_idx = 0;
312 mainboard->part_number_idx = strlen(mainboard_vendor) + 1;
313
314 memcpy(mainboard->strings + mainboard->vendor_idx,
315 mainboard_vendor, strlen(mainboard_vendor) + 1);
316 memcpy(mainboard->strings + mainboard->part_number_idx,
317 mainboard_part_number, strlen(mainboard_part_number) + 1);
318
319 return mainboard;
320 }
321
322 static struct lb_board_config *lb_board_config(struct lb_header *header)
323 {
324 struct lb_record *rec;
325 struct lb_board_config *config;
326 rec = lb_new_record(header);
327 config = (struct lb_board_config *)rec;
328
329 config->tag = LB_TAG_BOARD_CONFIG;
330 config->size = sizeof(*config);
331
332 const uint64_t fw_config = fw_config_get();
333 config->board_id = board_id();
334 config->ram_code = ram_code();
335 config->sku_id = sku_id();
336 config->fw_config = fw_config;
337
338 if (config->board_id != UNDEFINED_STRAPPING_ID)
339 printk(BIOS_INFO, "Board ID: %d\n", config->board_id);
340 if (config->ram_code != UNDEFINED_STRAPPING_ID)
341 printk(BIOS_INFO, "RAM code: %d\n", config->ram_code);
342 if (config->sku_id != UNDEFINED_STRAPPING_ID)
343 printk(BIOS_INFO, "SKU ID: %d\n", config->sku_id);
344 if (fw_config != UNDEFINED_FW_CONFIG)
345 printk(BIOS_INFO, "FW config: %#" PRIx64 "\n", fw_config);
346
347 return config;
348 }
349
350 void __weak mb_cfr_setup_menu(struct lb_cfr *cfr_root)
351 {
352 cfr_write_setup_menu(cfr_root, NULL);
353 }
354
355 static void lb_cfr_setup_menu(struct lb_header *header)
356 {
357 char *current = (char *)lb_new_record(header);
358 struct lb_cfr *cfr_root = (struct lb_cfr *)current;
359
360 mb_cfr_setup_menu(cfr_root);
361 }
362
363 #if CONFIG(USE_OPTION_TABLE)
364 static struct cmos_checksum *lb_cmos_checksum(struct lb_header *header)
365 {
366 struct lb_record *rec;
367 struct cmos_checksum *cmos_checksum;
368 rec = lb_new_record(header);
369 cmos_checksum = (struct cmos_checksum *)rec;
370 cmos_checksum->tag = LB_TAG_OPTION_CHECKSUM;
371
372 cmos_checksum->size = (sizeof(*cmos_checksum));
373
374 cmos_checksum->range_start = LB_CKS_RANGE_START * 8;
375 cmos_checksum->range_end = (LB_CKS_RANGE_END * 8) + 7;
376 cmos_checksum->location = LB_CKS_LOC * 8;
377 cmos_checksum->type = CHECKSUM_PCBIOS;
378
379 return cmos_checksum;
380 }
381 #endif
382
383 static void lb_strings(struct lb_header *header)
384 {
385 static const struct {
386 uint32_t tag;
387 const char *string;
388 } strings[] = {
389 { LB_TAG_VERSION, coreboot_version, },
390 { LB_TAG_EXTRA_VERSION, coreboot_extra_version, },
391 { LB_TAG_BUILD, coreboot_build, },
392 { LB_TAG_COMPILE_TIME, coreboot_compile_time, },
393 };
394 unsigned int i;
395 for (i = 0; i < ARRAY_SIZE(strings); i++) {
396 struct lb_string *rec;
397 size_t len;
398 rec = (struct lb_string *)lb_new_record(header);
399 len = strlen(strings[i].string);
400 rec->tag = strings[i].tag;
401 rec->size = ALIGN_UP(sizeof(*rec) + len + 1, LB_ENTRY_ALIGN);
402 memcpy(rec->string, strings[i].string, len+1);
403 }
404 }
405
406 static void lb_record_version_timestamp(struct lb_header *header)
407 {
408 struct lb_timestamp *rec;
409 rec = (struct lb_timestamp *)lb_new_record(header);
410 rec->tag = LB_TAG_VERSION_TIMESTAMP;
411 rec->size = sizeof(*rec);
412 rec->timestamp = coreboot_version_timestamp;
413 }
414
415 void __weak lb_board(struct lb_header *header) { /* NOOP */ }
416
417 /*
418 * It's possible that the system is using a SPI flash as the boot device,
419 * however it is not probing for devices to fill in specifics. In that
420 * case don't provide any information as the correct information is
421 * not known.
422 */
423 void __weak lb_spi_flash(struct lb_header *header) { /* NOOP */ }
424
425 static struct lb_forward *lb_forward(struct lb_header *header,
426 struct lb_header *next_header)
427 {
428 struct lb_record *rec;
429 struct lb_forward *forward;
430 rec = lb_new_record(header);
431 forward = (struct lb_forward *)rec;
432 forward->tag = LB_TAG_FORWARD;
433 forward->size = sizeof(*forward);
434 forward->forward = (uint64_t)(unsigned long)next_header;
435 return forward;
436 }
437
438 static unsigned long lb_table_fini(struct lb_header *head)
439 {
440 struct lb_record *rec, *first_rec;
441 rec = lb_last_record(head);
442 if (head->table_entries) {
443 assert(IS_ALIGNED(rec->size, LB_ENTRY_ALIGN));
444 head->table_bytes += rec->size;
445 }
446
447 first_rec = lb_first_record(head);
448 head->table_checksum = ipchksum(first_rec, head->table_bytes);
449 head->header_checksum = 0;
450 head->header_checksum = ipchksum(head, sizeof(*head));
451 printk(BIOS_DEBUG,
452 "Wrote coreboot table at: %p, 0x%x bytes, checksum %x\n",
453 head, head->table_bytes, head->table_checksum);
454 return (unsigned long)rec + rec->size;
455 }
456
457 static void lb_add_acpi_rsdp(struct lb_header *head)
458 {
459 struct lb_acpi_rsdp *acpi_rsdp;
460 struct lb_record *rec = lb_new_record(head);
461 acpi_rsdp = (struct lb_acpi_rsdp *)rec;
462 acpi_rsdp->tag = LB_TAG_ACPI_RSDP;
463 acpi_rsdp->size = sizeof(*acpi_rsdp);
464 acpi_rsdp->rsdp_pointer = get_coreboot_rsdp();
465 }
466
467 /*
468 * Not all platform would need to fill in the boot mode information.
469 * This is useful for platform that would like to implement battery
470 * charging solution in AP firmware.
471 */
472 void __weak lb_add_boot_mode(struct lb_header *header) { /* NOOP */ }
473
474 size_t write_coreboot_forwarding_table(uintptr_t entry, uintptr_t target)
475 {
476 struct lb_header *head;
477
478 printk(BIOS_DEBUG, "Writing table forward entry at %p\n",
479 (void *)entry);
480
481 head = lb_table_init(entry);
482 lb_forward(head, (struct lb_header *)target);
483
484 return (uintptr_t)lb_table_fini(head) - entry;
485 }
486
487 static uintptr_t write_coreboot_table(uintptr_t rom_table_end)
488 {
489 struct lb_header *head;
490
491 printk(BIOS_DEBUG, "Writing coreboot table at 0x%08lx\n",
492 (long)rom_table_end);
493
494 head = lb_table_init(rom_table_end);
495
496 #if CONFIG(USE_OPTION_TABLE)
497 {
498 struct cmos_option_table *option_table =
499 cbfs_map("cmos_layout.bin", NULL);
500 if (option_table) {
501 struct lb_record *rec_dest = lb_new_record(head);
502 /* Copy the option config table, it's already a
503 * lb_record...
504 */
505 memcpy(rec_dest, option_table, option_table->size);
506 /* Create CMOS checksum entry in coreboot table */
507 lb_cmos_checksum(head);
508 } else {
509 printk(BIOS_ERR,
510 "cmos_layout.bin could not be found!\n");
511 }
512 }
513 #endif
514
515 /* Generate CFR entry for setup menus */
516 if (CONFIG(DRIVERS_OPTION_CFR))
517 lb_cfr_setup_menu(head);
518
519 /* Serialize resource map into mem table types (LB_MEM_*) */
520 bootmem_write_memory_table(lb_memory(head));
521
522 /* Record our motherboard */
523 lb_mainboard(head);
524
525 /* Record the serial ports and consoles */
526 if (CONFIG(CONSOLE_SERIAL))
527 lb_add_serial(head);
528
529 if (CONFIG(CONSOLE_USB))
530 lb_add_console(LB_TAG_CONSOLE_EHCI, head);
531
532 if (CONFIG(PCI))
533 lb_pcie(head);
534
535 /* Record our various random string information */
536 lb_strings(head);
537 if (CONFIG(PLATFORM_USES_FSP2_0))
538 lb_string_platform_blob_version(head);
539 lb_record_version_timestamp(head);
540 /* Record our framebuffer */
541 lb_framebuffer(head);
542
543 /* Record our GPIO settings (ChromeOS specific) */
544 if (CONFIG(CHROMEOS))
545 lb_gpios(head);
546
547 /* pass along VBNV offsets in CMOS */
548 if (CONFIG(VBOOT_VBNV_CMOS))
549 lb_table_add_vbnv_cmos(head);
550
551 /* Pass mmc early init status */
552 lb_mmc_info(head);
553
554 /* Add SPI flash description if available */
555 if (CONFIG(BOOT_DEVICE_SPI_FLASH))
556 lb_spi_flash(head);
557
558 add_cbmem_pointers(head);
559
560 /* SMMSTORE v2 */
561 if (CONFIG(SMMSTORE_V2))
562 lb_smmstorev2(head);
563
564 /* Add information about firmware in form suitable for EFI updates. */
565 if (CONFIG(DRIVERS_EFI_FW_INFO))
566 lb_efi_fw_info(head);
567
568 /* Add board-specific table entries, if any. */
569 lb_board(head);
570
571 /* Possibly add UEFI capsules. */
572 if (CONFIG(DRIVERS_EFI_UPDATE_CAPSULES))
573 lb_efi_capsules(head);
574
575 if (CONFIG(CHROMEOS_RAMOOPS))
576 lb_ramoops(head);
577
578 lb_boot_media_params(head);
579
580 /* Board configuration information (including straps) */
581 lb_board_config(head);
582
583 if (CONFIG(TPM_PPI))
584 lb_tpm_ppi(head);
585
586 /* Add architecture records. */
587 lb_arch_add_records(head);
588
589 /* Add all cbmem entries into the coreboot tables. */
590 cbmem_add_records_to_cbtable(head);
591
592 if (CONFIG(HAVE_ACPI_TABLES))
593 lb_add_acpi_rsdp(head);
594
595 lb_add_boot_mode(head);
596
597 /* Remember where my valid memory ranges are */
598 return lb_table_fini(head);
599 }
600
601 void *write_tables(void)
602 {
603 uintptr_t cbtable_start;
604 uintptr_t cbtable_end;
605 size_t cbtable_size;
606 const size_t max_table_size = COREBOOT_TABLE_SIZE;
607
608 cbtable_start = (uintptr_t)cbmem_add(CBMEM_ID_CBTABLE, max_table_size);
609
610 if (!cbtable_start) {
611 printk(BIOS_ERR, "Could not add CBMEM for coreboot table.\n");
612 return NULL;
613 }
614
615 /* Add architecture specific tables. */
616 arch_write_tables(cbtable_start);
617
618 /* Write the coreboot table. */
619 cbtable_end = write_coreboot_table(cbtable_start);
620 cbtable_size = cbtable_end - cbtable_start;
621
622 if (cbtable_size > max_table_size) {
623 printk(BIOS_ERR, "%s: coreboot table didn't fit (%zx/%zx)\n",
624 __func__, cbtable_size, max_table_size);
625 }
626
627 printk(BIOS_DEBUG, "coreboot table: %zd bytes.\n", cbtable_size);
628
629 /* Print CBMEM sections */
630 cbmem_list();
631 return (void *)cbtable_start;
632 }