2 * This file is part of the coreboot project.
4 * Copyright (C) 2003-2004 Eric Biederman
5 * Copyright (C) 2005-2009 coresystems GmbH
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; version 2 of
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
23 #include <console/console.h>
24 #include <ip_checksum.h>
25 #include <boot/tables.h>
26 #include <boot/coreboot_tables.h>
27 #include "coreboot_table.h"
30 #include <device/device.h>
33 static struct lb_header *lb_table_init(unsigned long addr)
35 struct lb_header *header;
37 /* 16 byte align the address */
41 header = (void *)addr;
42 header->signature[0] = 'L';
43 header->signature[1] = 'B';
44 header->signature[2] = 'I';
45 header->signature[3] = 'O';
46 header->header_bytes = sizeof(*header);
47 header->header_checksum = 0;
48 header->table_bytes = 0;
49 header->table_checksum = 0;
50 header->table_entries = 0;
54 static struct lb_record *lb_first_record(struct lb_header *header)
56 struct lb_record *rec;
57 rec = (void *)(((char *)header) + sizeof(*header));
61 static struct lb_record *lb_last_record(struct lb_header *header)
63 struct lb_record *rec;
64 rec = (void *)(((char *)header) + sizeof(*header) + header->table_bytes);
68 static struct lb_record *lb_next_record(struct lb_record *rec)
70 rec = (void *)(((char *)rec) + rec->size);
74 static struct lb_record *lb_new_record(struct lb_header *header)
76 struct lb_record *rec;
77 rec = lb_last_record(header);
78 if (header->table_entries) {
79 header->table_bytes += rec->size;
81 rec = lb_last_record(header);
82 header->table_entries++;
83 rec->tag = LB_TAG_UNUSED;
84 rec->size = sizeof(*rec);
89 static struct lb_memory *lb_memory(struct lb_header *header)
91 struct lb_record *rec;
92 struct lb_memory *mem;
93 rec = lb_new_record(header);
94 mem = (struct lb_memory *)rec;
95 mem->tag = LB_TAG_MEMORY;
96 mem->size = sizeof(*mem);
100 static struct lb_serial *lb_serial(struct lb_header *header)
102 #if defined(CONFIG_TTYS0_BASE)
103 struct lb_record *rec;
104 struct lb_serial *serial;
105 rec = lb_new_record(header);
106 serial = (struct lb_serial *)rec;
107 serial->tag = LB_TAG_SERIAL;
108 serial->size = sizeof(*serial);
109 serial->ioport = CONFIG_TTYS0_BASE;
110 serial->baud = CONFIG_TTYS0_BAUD;
117 static void add_console(struct lb_header *header, u16 consoletype)
119 struct lb_console *console;
121 console = (struct lb_console *)lb_new_record(header);
122 console->tag = LB_TAG_CONSOLE;
123 console->size = sizeof(*console);
124 console->type = consoletype;
127 static void lb_console(struct lb_header *header)
129 #ifdef CONFIG_CONSOLE_SERIAL8250
130 add_console(header, LB_TAG_CONSOLE_SERIAL8250);
132 #ifdef CONFIG_CONSOLE_VGA
133 add_console(header, LB_TAG_CONSOLE_VGA);
135 #ifdef CONFIG_CONSOLE_BTEXT
136 add_console(header, LB_TAG_CONSOLE_BTEXT);
138 #ifdef CONFIG_CONSOLE_LOGBUF
139 add_console(header, LB_TAG_CONSOLE_LOGBUF);
141 #ifdef CONFIG_CONSOLE_SROM
142 add_console(header, LB_TAG_CONSOLE_SROM);
144 #ifdef CONFIG_USBDEBUG_DIRECT
145 add_console(header, LB_TAG_CONSOLE_EHCI);
149 struct lb_mainboard *lb_mainboard(struct lb_header *header)
151 struct lb_record *rec;
152 struct lb_mainboard *mainboard;
153 rec = lb_new_record(header);
154 mainboard = (struct lb_mainboard *)rec;
155 mainboard->tag = LB_TAG_MAINBOARD;
157 mainboard->size = (sizeof(*mainboard) +
158 strlen(mainboard_vendor) + 1 +
159 strlen(mainboard_part_number) + 1 +
162 mainboard->vendor_idx = 0;
163 mainboard->part_number_idx = strlen(mainboard_vendor) + 1;
165 memcpy(mainboard->strings + mainboard->vendor_idx,
166 mainboard_vendor, strlen(mainboard_vendor) + 1);
167 memcpy(mainboard->strings + mainboard->part_number_idx,
168 mainboard_part_number, strlen(mainboard_part_number) + 1);
173 static struct cmos_checksum *lb_cmos_checksum(struct lb_header *header)
175 struct lb_record *rec;
176 struct cmos_checksum *cmos_checksum;
177 rec = lb_new_record(header);
178 cmos_checksum = (struct cmos_checksum *)rec;
179 cmos_checksum->tag = LB_TAG_OPTION_CHECKSUM;
181 cmos_checksum->size = (sizeof(*cmos_checksum));
183 cmos_checksum->range_start = CONFIG_LB_CKS_RANGE_START * 8;
184 cmos_checksum->range_end = ( CONFIG_LB_CKS_RANGE_END * 8 ) + 7;
185 cmos_checksum->location = CONFIG_LB_CKS_LOC * 8;
186 cmos_checksum->type = CHECKSUM_PCBIOS;
188 return cmos_checksum;
191 static void lb_strings(struct lb_header *header)
193 static const struct {
197 { LB_TAG_VERSION, coreboot_version, },
198 { LB_TAG_EXTRA_VERSION, coreboot_extra_version, },
199 { LB_TAG_BUILD, coreboot_build, },
200 { LB_TAG_COMPILE_TIME, coreboot_compile_time, },
201 { LB_TAG_COMPILE_BY, coreboot_compile_by, },
202 { LB_TAG_COMPILE_HOST, coreboot_compile_host, },
203 { LB_TAG_COMPILE_DOMAIN, coreboot_compile_domain, },
204 { LB_TAG_COMPILER, coreboot_compiler, },
205 { LB_TAG_LINKER, coreboot_linker, },
206 { LB_TAG_ASSEMBLER, coreboot_assembler, },
209 for(i = 0; i < ARRAY_SIZE(strings); i++) {
210 struct lb_string *rec;
212 rec = (struct lb_string *)lb_new_record(header);
213 len = strlen(strings[i].string);
214 rec->tag = strings[i].tag;
215 rec->size = (sizeof(*rec) + len + 1 + 3) & ~3;
216 memcpy(rec->string, strings[i].string, len+1);
221 static struct lb_forward *lb_forward(struct lb_header *header, struct lb_header *next_header)
223 struct lb_record *rec;
224 struct lb_forward *forward;
225 rec = lb_new_record(header);
226 forward = (struct lb_forward *)rec;
227 forward->tag = LB_TAG_FORWARD;
228 forward->size = sizeof(*forward);
229 forward->forward = (uint64_t)(unsigned long)next_header;
233 void lb_memory_range(struct lb_memory *mem,
234 uint32_t type, uint64_t start, uint64_t size)
237 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
238 mem->map[entries].start = pack_lb64(start);
239 mem->map[entries].size = pack_lb64(size);
240 mem->map[entries].type = type;
241 mem->size += sizeof(mem->map[0]);
244 static void lb_reserve_table_memory(struct lb_header *head)
246 struct lb_record *last_rec;
247 struct lb_memory *mem;
252 last_rec = lb_last_record(head);
254 start = (unsigned long)head;
255 end = (unsigned long)last_rec;
256 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
257 /* Resize the right two memory areas so this table is in
258 * a reserved area of memory. Everything has been carefully
259 * setup so that is all we need to do.
261 for(i = 0; i < entries; i++ ) {
262 uint64_t map_start = unpack_lb64(mem->map[i].start);
263 uint64_t map_end = map_start + unpack_lb64(mem->map[i].size);
264 /* Does this area need to be expanded? */
265 if (map_end == start) {
266 mem->map[i].size = pack_lb64(end - map_start);
268 /* Does this area need to be contracted? */
269 else if (map_start == start) {
270 mem->map[i].start = pack_lb64(end);
271 mem->map[i].size = pack_lb64(map_end - end);
276 static unsigned long lb_table_fini(struct lb_header *head, int fixup)
278 struct lb_record *rec, *first_rec;
279 rec = lb_last_record(head);
280 if (head->table_entries) {
281 head->table_bytes += rec->size;
285 lb_reserve_table_memory(head);
287 first_rec = lb_first_record(head);
288 head->table_checksum = compute_ip_checksum(first_rec, head->table_bytes);
289 head->header_checksum = 0;
290 head->header_checksum = compute_ip_checksum(head, sizeof(*head));
291 printk_debug("Wrote coreboot table at: %p - %p checksum %x\n",
292 head, rec, head->table_checksum);
293 return (unsigned long)rec;
296 static void lb_cleanup_memory_ranges(struct lb_memory *mem)
300 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
302 /* Sort the lb memory ranges */
303 for(i = 0; i < entries; i++) {
304 uint64_t entry_start = unpack_lb64(mem->map[i].start);
305 for(j = i; j < entries; j++) {
306 uint64_t temp_start = unpack_lb64(mem->map[j].start);
307 if (temp_start < entry_start) {
308 struct lb_memory_range tmp;
310 mem->map[i] = mem->map[j];
316 /* Merge adjacent entries */
317 for(i = 0; (i + 1) < entries; i++) {
318 uint64_t start, end, nstart, nend;
319 if (mem->map[i].type != mem->map[i + 1].type) {
322 start = unpack_lb64(mem->map[i].start);
323 end = start + unpack_lb64(mem->map[i].size);
324 nstart = unpack_lb64(mem->map[i + 1].start);
325 nend = nstart + unpack_lb64(mem->map[i + 1].size);
326 if ((start <= nstart) && (end > nstart)) {
327 if (start > nstart) {
333 /* Record the new region size */
334 mem->map[i].start = pack_lb64(start);
335 mem->map[i].size = pack_lb64(end - start);
337 /* Delete the entry I have merged with */
338 memmove(&mem->map[i + 1], &mem->map[i + 2],
339 ((entries - i - 2) * sizeof(mem->map[0])));
340 mem->size -= sizeof(mem->map[0]);
342 /* See if I can merge with the next entry as well */
348 static void lb_remove_memory_range(struct lb_memory *mem,
349 uint64_t start, uint64_t size)
356 entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
358 /* Remove a reserved area from the memory map */
359 for(i = 0; i < entries; i++) {
360 uint64_t map_start = unpack_lb64(mem->map[i].start);
361 uint64_t map_end = map_start + unpack_lb64(mem->map[i].size);
362 if ((start <= map_start) && (end >= map_end)) {
363 /* Remove the completely covered range */
364 memmove(&mem->map[i], &mem->map[i + 1],
365 ((entries - i - 1) * sizeof(mem->map[0])));
366 mem->size -= sizeof(mem->map[0]);
368 /* Since the index will disappear revisit what will appear here */
371 else if ((start > map_start) && (end < map_end)) {
372 /* Split the memory range */
373 memmove(&mem->map[i + 1], &mem->map[i],
374 ((entries - i) * sizeof(mem->map[0])));
375 mem->size += sizeof(mem->map[0]);
377 /* Update the first map entry */
378 mem->map[i].size = pack_lb64(start - map_start);
379 /* Update the second map entry */
380 mem->map[i + 1].start = pack_lb64(end);
381 mem->map[i + 1].size = pack_lb64(map_end - end);
382 /* Don't bother with this map entry again */
385 else if ((start <= map_start) && (end > map_start)) {
386 /* Shrink the start of the memory range */
387 mem->map[i].start = pack_lb64(end);
388 mem->map[i].size = pack_lb64(map_end - end);
390 else if ((start < map_end) && (start > map_start)) {
391 /* Shrink the end of the memory range */
392 mem->map[i].size = pack_lb64(start - map_start);
397 /* This function is used in mainboard specific code, too */
398 void lb_add_memory_range(struct lb_memory *mem,
399 uint32_t type, uint64_t start, uint64_t size)
401 lb_remove_memory_range(mem, start, size);
402 lb_memory_range(mem, type, start, size);
403 lb_cleanup_memory_ranges(mem);
406 /* Routines to extract part so the coreboot table or
407 * information from the coreboot table after we have written it.
408 * Currently get_lb_mem relies on a global we can change the
411 static struct lb_memory *mem_ranges = 0;
412 struct lb_memory *get_lb_mem(void)
417 static void build_lb_mem_range(void *gp, struct device *dev, struct resource *res)
419 struct lb_memory *mem = gp;
420 lb_memory_range(mem, LB_MEM_RAM, res->base, res->size);
423 static struct lb_memory *build_lb_mem(struct lb_header *head)
425 struct lb_memory *mem;
427 /* Record where the lb memory ranges will live */
428 mem = lb_memory(head);
431 /* Build the raw table of memory */
432 search_global_resources(
433 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
434 build_lb_mem_range, mem);
435 lb_cleanup_memory_ranges(mem);
439 #if CONFIG_WRITE_HIGH_TABLES == 1
440 extern uint64_t high_tables_base, high_tables_size;
443 unsigned long write_coreboot_table(
444 unsigned long low_table_start, unsigned long low_table_end,
445 unsigned long rom_table_start, unsigned long rom_table_end)
447 struct lb_header *head;
448 struct lb_memory *mem;
450 #if CONFIG_WRITE_HIGH_TABLES == 1
451 printk_debug("Writing high table forward entry at 0x%08lx\n",
453 head = lb_table_init(low_table_end);
454 lb_forward(head, (struct lb_header*)rom_table_end);
456 low_table_end = (unsigned long) lb_table_fini(head, 0);
457 printk_debug("New low_table_end: 0x%08lx\n", low_table_end);
458 printk_debug("Now going to write high coreboot table at 0x%08lx\n",
461 head = lb_table_init(rom_table_end);
462 rom_table_end = (unsigned long)head;
463 printk_debug("rom_table_end = 0x%08lx\n", rom_table_end);
465 if(low_table_end > (0x1000 - sizeof(struct lb_header))) { /* after 4K */
466 /* We need to put lbtable on to [0xf0000,0x100000) */
467 head = lb_table_init(rom_table_end);
468 rom_table_end = (unsigned long)head;
470 head = lb_table_init(low_table_end);
471 low_table_end = (unsigned long)head;
475 printk_debug("Adjust low_table_end from 0x%08lx to ", low_table_end);
476 low_table_end += 0xfff; // 4K aligned
477 low_table_end &= ~0xfff;
478 printk_debug("0x%08lx \n", low_table_end);
480 /* The Linux kernel assumes this region is reserved */
481 printk_debug("Adjust rom_table_end from 0x%08lx to ", rom_table_end);
482 rom_table_end += 0xffff; // 64K align
483 rom_table_end &= ~0xffff;
484 printk_debug("0x%08lx \n", rom_table_end);
486 #if (CONFIG_HAVE_OPTION_TABLE == 1)
488 struct lb_record *rec_dest, *rec_src;
489 /* Write the option config table... */
490 rec_dest = lb_new_record(head);
491 rec_src = (struct lb_record *)(void *)&option_table;
492 memcpy(rec_dest, rec_src, rec_src->size);
493 /* Create cmos checksum entry in coreboot table */
494 lb_cmos_checksum(head);
497 /* Record where RAM is located */
498 mem = build_lb_mem(head);
500 /* Record the mptable and the the lb_table (This will be adjusted later) */
501 lb_add_memory_range(mem, LB_MEM_TABLE,
502 low_table_start, low_table_end - low_table_start);
504 /* Record the pirq table, acpi tables, and maybe the mptable */
505 lb_add_memory_range(mem, LB_MEM_TABLE,
506 rom_table_start, rom_table_end-rom_table_start);
508 #if CONFIG_WRITE_HIGH_TABLES == 1
509 printk_debug("Adding high table area\n");
510 // should this be LB_MEM_ACPI?
511 lb_add_memory_range(mem, LB_MEM_TABLE,
512 high_tables_base, high_tables_size);
515 #if (CONFIG_HAVE_MAINBOARD_RESOURCES == 1)
516 add_mainboard_resources(mem);
520 * I assume that there is always memory at immediately after
521 * the low_table_end. This means that after I setup the coreboot table.
522 * I can trivially fixup the reserved memory ranges to hold the correct
523 * size of the coreboot table.
526 /* Record our motherboard */
528 /* Record the serial port, if present */
530 /* Record our console setup */
532 /* Record our various random string information */
535 /* Remember where my valid memory ranges are */
536 return lb_table_fini(head, 1);