blob: 4bcfa886f7caeac47f9b6a489051cde57baf9cbc [file] [log] [blame]
Stefan Reinauer5380d512007-05-24 09:08:36 +00001/*
Uwe Hermannd1107642007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
Stefan Reinauer5380d512007-05-24 09:08:36 +00003 *
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)
Stefan Reinauer2d853bb2009-03-17 14:39:25 +00007 * Copyright (C) 2006-2009 coresystems GmbH
Stefan Reinauer5380d512007-05-24 09:08:36 +00008 * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
9 *
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.
13 *
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.
18 *
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
Uwe Hermannd1107642007-08-29 17:52:32 +000021 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Stefan Reinauer5380d512007-05-24 09:08:36 +000022 */
23
Ollie Lho184a4042005-11-26 21:55:36 +000024#include <stdlib.h>
25#include <fcntl.h>
Ollie Lho184a4042005-11-26 21:55:36 +000026#include <sys/types.h>
27#include <string.h>
28#include <errno.h>
29#include <sys/mman.h>
Adam Kaufman064b1f22007-02-06 19:47:50 +000030#include "flash.h"
Stefan Reinauer2fbe6242008-01-18 16:17:44 +000031#include "coreboot_tables.h"
Ollie Lho184a4042005-11-26 21:55:36 +000032
Uwe Hermanna7e05482007-05-09 10:17:44 +000033char *lb_part = NULL, *lb_vendor = NULL;
Ollie Lho184a4042005-11-26 21:55:36 +000034
35static unsigned long compute_checksum(void *addr, unsigned long length)
36{
37 uint8_t *ptr;
38 volatile union {
Uwe Hermanna7e05482007-05-09 10:17:44 +000039 uint8_t byte[2];
Ollie Lho184a4042005-11-26 21:55:36 +000040 uint16_t word;
41 } value;
Uwe Hermannac309342007-10-10 17:42:20 +000042 unsigned long sum;
43 unsigned long i;
Uwe Hermannffec5f32007-08-23 16:08:21 +000044
Ollie Lho184a4042005-11-26 21:55:36 +000045 /* In the most straight forward way possible,
46 * compute an ip style checksum.
47 */
48 sum = 0;
49 ptr = addr;
Uwe Hermanna7e05482007-05-09 10:17:44 +000050 for (i = 0; i < length; i++) {
Ollie Lho184a4042005-11-26 21:55:36 +000051 unsigned long value;
52 value = ptr[i];
Uwe Hermannac309342007-10-10 17:42:20 +000053 if (i & 1) {
Ollie Lho184a4042005-11-26 21:55:36 +000054 value <<= 8;
Uwe Hermannac309342007-10-10 17:42:20 +000055 }
Ollie Lho184a4042005-11-26 21:55:36 +000056 /* Add the new value */
57 sum += value;
58 /* Wrap around the carry */
Uwe Hermannac309342007-10-10 17:42:20 +000059 if (sum > 0xFFFF) {
Ollie Lho184a4042005-11-26 21:55:36 +000060 sum = (sum + (sum >> 16)) & 0xFFFF;
Uwe Hermannac309342007-10-10 17:42:20 +000061 }
Ollie Lho184a4042005-11-26 21:55:36 +000062 }
63 value.byte[0] = sum & 0xff;
64 value.byte[1] = (sum >> 8) & 0xff;
Uwe Hermannffec5f32007-08-23 16:08:21 +000065
Ollie Lho184a4042005-11-26 21:55:36 +000066 return (~value.word) & 0xFFFF;
67}
68
69#define for_each_lbrec(head, rec) \
70 for(rec = (struct lb_record *)(((char *)head) + sizeof(*head)); \
71 (((char *)rec) < (((char *)head) + sizeof(*head) + head->table_bytes)) && \
72 (rec->size >= 1) && \
73 ((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \
Uwe Hermanna7e05482007-05-09 10:17:44 +000074 rec = (struct lb_record *)(((char *)rec) + rec->size))
Ollie Lho184a4042005-11-26 21:55:36 +000075
Uwe Hermanna7e05482007-05-09 10:17:44 +000076static int count_lb_records(struct lb_header *head)
Ollie Lho184a4042005-11-26 21:55:36 +000077{
78 struct lb_record *rec;
79 int count;
Uwe Hermannffec5f32007-08-23 16:08:21 +000080
Ollie Lho184a4042005-11-26 21:55:36 +000081 count = 0;
82 for_each_lbrec(head, rec) {
83 count++;
84 }
Uwe Hermannffec5f32007-08-23 16:08:21 +000085
Ollie Lho184a4042005-11-26 21:55:36 +000086 return count;
87}
88
Uwe Hermanna7e05482007-05-09 10:17:44 +000089static struct lb_header *find_lb_table(void *base, unsigned long start,
90 unsigned long end)
Ollie Lho184a4042005-11-26 21:55:36 +000091{
92 unsigned long addr;
Uwe Hermannffec5f32007-08-23 16:08:21 +000093
Ollie Lho184a4042005-11-26 21:55:36 +000094 /* For now be stupid.... */
Uwe Hermanna7e05482007-05-09 10:17:44 +000095 for (addr = start; addr < end; addr += 16) {
96 struct lb_header *head =
97 (struct lb_header *)(((char *)base) + addr);
98 struct lb_record *recs =
99 (struct lb_record *)(((char *)base) + addr + sizeof(*head));
Ollie Lho184a4042005-11-26 21:55:36 +0000100 if (memcmp(head->signature, "LBIO", 4) != 0)
101 continue;
Daniel McLelland02b73f2009-05-14 12:41:00 +0000102 printf_debug("Found candidate at: %08lx-%08lx\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000103 addr, addr + head->table_bytes);
Ollie Lho184a4042005-11-26 21:55:36 +0000104 if (head->header_bytes != sizeof(*head)) {
Uwe Hermanna502dce2007-10-17 23:55:15 +0000105 fprintf(stderr, "Header bytes of %d are incorrect.\n",
Ollie Lho184a4042005-11-26 21:55:36 +0000106 head->header_bytes);
107 continue;
108 }
109 if (count_lb_records(head) != head->table_entries) {
Uwe Hermanna502dce2007-10-17 23:55:15 +0000110 fprintf(stderr, "Bad record count: %d.\n",
Ollie Lho184a4042005-11-26 21:55:36 +0000111 head->table_entries);
112 continue;
113 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000114 if (compute_checksum((uint8_t *) head, sizeof(*head)) != 0) {
Uwe Hermanna502dce2007-10-17 23:55:15 +0000115 fprintf(stderr, "Bad header checksum.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000116 continue;
117 }
118 if (compute_checksum(recs, head->table_bytes)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000119 != head->table_checksum) {
Uwe Hermanna502dce2007-10-17 23:55:15 +0000120 fprintf(stderr, "Bad table checksum: %04x.\n",
Ollie Lho184a4042005-11-26 21:55:36 +0000121 head->table_checksum);
122 continue;
123 }
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000124 printf_debug("Found coreboot table at 0x%08lx.\n", addr);
Ollie Lho184a4042005-11-26 21:55:36 +0000125 return head;
126
127 };
Uwe Hermannffec5f32007-08-23 16:08:21 +0000128
Ollie Lho184a4042005-11-26 21:55:36 +0000129 return 0;
130}
131
132static void find_mainboard(struct lb_record *ptr, unsigned long addr)
133{
134 struct lb_mainboard *rec;
135 int max_size;
136 char vendor[256], part[256];
Uwe Hermannffec5f32007-08-23 16:08:21 +0000137
Ollie Lho184a4042005-11-26 21:55:36 +0000138 rec = (struct lb_mainboard *)ptr;
139 max_size = rec->size - sizeof(*rec);
Uwe Hermanna502dce2007-10-17 23:55:15 +0000140 printf("Vendor ID: %.*s, part ID: %.*s\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000141 max_size - rec->vendor_idx,
142 rec->strings + rec->vendor_idx,
143 max_size - rec->part_number_idx,
144 rec->strings + rec->part_number_idx);
145 snprintf(vendor, 255, "%.*s", max_size - rec->vendor_idx,
146 rec->strings + rec->vendor_idx);
Ollie Lho184a4042005-11-26 21:55:36 +0000147 snprintf(part, 255, "%.*s", max_size - rec->part_number_idx,
Uwe Hermanna7e05482007-05-09 10:17:44 +0000148 rec->strings + rec->part_number_idx);
Ollie Lho184a4042005-11-26 21:55:36 +0000149
Uwe Hermanna7e05482007-05-09 10:17:44 +0000150 if (lb_part) {
Uwe Hermanna502dce2007-10-17 23:55:15 +0000151 printf("Overwritten by command line, vendor ID: %s, part ID: %s.\n", lb_vendor, lb_part);
Ollie Lho184a4042005-11-26 21:55:36 +0000152 } else {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000153 lb_part = strdup(part);
154 lb_vendor = strdup(vendor);
Ollie Lho184a4042005-11-26 21:55:36 +0000155 }
156}
157
158static struct lb_record *next_record(struct lb_record *rec)
159{
160 return (struct lb_record *)(((char *)rec) + rec->size);
161}
162
Uwe Hermanna7e05482007-05-09 10:17:44 +0000163static void search_lb_records(struct lb_record *rec, struct lb_record *last,
164 unsigned long addr)
Ollie Lho184a4042005-11-26 21:55:36 +0000165{
166 struct lb_record *next;
167 int count;
168 count = 0;
169
Uwe Hermanna7e05482007-05-09 10:17:44 +0000170 for (next = next_record(rec); (rec < last) && (next <= last);
171 rec = next, addr += rec->size) {
Ollie Lho184a4042005-11-26 21:55:36 +0000172 next = next_record(rec);
173 count++;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000174 if (rec->tag == LB_TAG_MAINBOARD) {
175 find_mainboard(rec, addr);
Ollie Lho184a4042005-11-26 21:55:36 +0000176 break;
177 }
178 }
179}
180
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000181#define BYTES_TO_MAP (1024*1024)
Stefan Reinauere3f3e2e2008-01-18 15:33:10 +0000182int coreboot_init(void)
Ollie Lho184a4042005-11-26 21:55:36 +0000183{
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000184 uint8_t *table_area;
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000185 unsigned long addr, start;
Ollie Lho184a4042005-11-26 21:55:36 +0000186 struct lb_header *lb_table;
187 struct lb_record *rec, *last;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000188
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000189#ifdef __DARWIN__
190 /* This is a hack. DirectIO fails to map physical address 0x00000000.
191 * Why?
192 */
193 start = 0x400;
194#else
195 start = 0x0;
196#endif
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000197 table_area = physmap("low megabyte", start, BYTES_TO_MAP);
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000198
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000199 lb_table = find_lb_table(table_area, 0x00000, 0x1000);
Ollie Lho184a4042005-11-26 21:55:36 +0000200 if (!lb_table)
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000201 lb_table = find_lb_table(table_area, 0xf0000, BYTES_TO_MAP);
202 if (lb_table) {
203 struct lb_forward *forward = (struct lb_forward *)
204 (((char *)lb_table) + lb_table->header_bytes);
205 if (forward->tag == LB_TAG_FORWARD) {
206 start = forward->forward;
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000207 start &= ~(getpagesize() - 1);
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000208 physunmap(table_area, BYTES_TO_MAP);
209 table_area = physmap("high tables", start, BYTES_TO_MAP);
210 lb_table = find_lb_table(table_area, 0x00000, 0x1000);
211 }
212 }
213
Peter Stuge2dc3aaa2009-01-26 00:15:56 +0000214 if (!lb_table) {
Stefan Reinauere3f3e2e2008-01-18 15:33:10 +0000215 printf("No coreboot table found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000216 return -1;
217 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000218
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000219 addr = ((char *)lb_table) - ((char *)table_area) + start;
220 fprintf(stdout, "coreboot table found at 0x%lx.\n",
221 (unsigned long)lb_table - (unsigned long)table_area + start);
Peter Stuge2dc3aaa2009-01-26 00:15:56 +0000222 rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes);
223 last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes);
Peter Stugea0d75a02009-01-26 00:19:36 +0000224 printf_debug("coreboot header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
Peter Stuge2dc3aaa2009-01-26 00:15:56 +0000225 lb_table->header_bytes, lb_table->header_checksum,
226 lb_table->table_bytes, lb_table->table_checksum,
227 lb_table->table_entries);
228 search_lb_records(rec, last, addr + lb_table->header_bytes);
229
Ollie Lho184a4042005-11-26 21:55:36 +0000230 return 0;
231}