2 * This file is part of the flashrom project.
4 * Copyright (C) 2002 Steven James <pyro@linuxlabs.com>
5 * Copyright (C) 2002 Linux Networx
6 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
7 * Copyright (C) 2006-2007 coresystems GmbH
8 * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <sys/types.h>
34 #include "coreboot_tables.h"
36 char *lb_part = NULL, *lb_vendor = NULL;
38 static unsigned long compute_checksum(void *addr, unsigned long length)
48 /* In the most straight forward way possible,
49 * compute an ip style checksum.
53 for (i = 0; i < length; i++) {
59 /* Add the new value */
61 /* Wrap around the carry */
63 sum = (sum + (sum >> 16)) & 0xFFFF;
66 value.byte[0] = sum & 0xff;
67 value.byte[1] = (sum >> 8) & 0xff;
69 return (~value.word) & 0xFFFF;
72 #define for_each_lbrec(head, rec) \
73 for(rec = (struct lb_record *)(((char *)head) + sizeof(*head)); \
74 (((char *)rec) < (((char *)head) + sizeof(*head) + head->table_bytes)) && \
76 ((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \
77 rec = (struct lb_record *)(((char *)rec) + rec->size))
79 static int count_lb_records(struct lb_header *head)
81 struct lb_record *rec;
85 for_each_lbrec(head, rec) {
92 static struct lb_header *find_lb_table(void *base, unsigned long start,
97 /* For now be stupid.... */
98 for (addr = start; addr < end; addr += 16) {
99 struct lb_header *head =
100 (struct lb_header *)(((char *)base) + addr);
101 struct lb_record *recs =
102 (struct lb_record *)(((char *)base) + addr + sizeof(*head));
103 if (memcmp(head->signature, "LBIO", 4) != 0)
105 printf_debug("Found canidate at: %08lx-%08lx\n",
106 addr, addr + head->table_bytes);
107 if (head->header_bytes != sizeof(*head)) {
108 fprintf(stderr, "Header bytes of %d are incorrect.\n",
112 if (count_lb_records(head) != head->table_entries) {
113 fprintf(stderr, "Bad record count: %d.\n",
114 head->table_entries);
117 if (compute_checksum((uint8_t *) head, sizeof(*head)) != 0) {
118 fprintf(stderr, "Bad header checksum.\n");
121 if (compute_checksum(recs, head->table_bytes)
122 != head->table_checksum) {
123 fprintf(stderr, "Bad table checksum: %04x.\n",
124 head->table_checksum);
127 fprintf(stdout, "Found coreboot table at 0x%08lx.\n", addr);
135 static void find_mainboard(struct lb_record *ptr, unsigned long addr)
137 struct lb_mainboard *rec;
139 char vendor[256], part[256];
141 rec = (struct lb_mainboard *)ptr;
142 max_size = rec->size - sizeof(*rec);
143 printf("Vendor ID: %.*s, part ID: %.*s\n",
144 max_size - rec->vendor_idx,
145 rec->strings + rec->vendor_idx,
146 max_size - rec->part_number_idx,
147 rec->strings + rec->part_number_idx);
148 snprintf(vendor, 255, "%.*s", max_size - rec->vendor_idx,
149 rec->strings + rec->vendor_idx);
150 snprintf(part, 255, "%.*s", max_size - rec->part_number_idx,
151 rec->strings + rec->part_number_idx);
154 printf("Overwritten by command line, vendor ID: %s, part ID: %s.\n", lb_vendor, lb_part);
156 lb_part = strdup(part);
157 lb_vendor = strdup(vendor);
161 static struct lb_record *next_record(struct lb_record *rec)
163 return (struct lb_record *)(((char *)rec) + rec->size);
166 static void search_lb_records(struct lb_record *rec, struct lb_record *last,
169 struct lb_record *next;
173 for (next = next_record(rec); (rec < last) && (next <= last);
174 rec = next, addr += rec->size) {
175 next = next_record(rec);
177 if (rec->tag == LB_TAG_MAINBOARD) {
178 find_mainboard(rec, addr);
184 int coreboot_init(void)
187 struct lb_header *lb_table;
188 struct lb_record *rec, *last;
190 low_1MB = mmap(0, 1024 * 1024, PROT_READ, MAP_SHARED, fd_mem,
192 if (low_1MB == MAP_FAILED) {
193 perror("Can't mmap memory using " MEM_DEV);
198 lb_table = find_lb_table(low_1MB, 0x00000, 0x1000);
200 lb_table = find_lb_table(low_1MB, 0xf0000, 1024 * 1024);
203 addr = ((char *)lb_table) - ((char *)low_1MB);
204 printf_debug("Coreboot table found at %p.\n", lb_table);
205 rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes);
206 last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes);
207 printf_debug("Coreboot header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
208 lb_table->header_bytes, lb_table->header_checksum,
209 lb_table->table_bytes, lb_table->table_checksum,
210 lb_table->table_entries);
211 search_lb_records(rec, last, addr + lb_table->header_bytes);
213 printf("No coreboot table found.\n");