blob: 3e97ed7b5931a27a55756b163ae2dfaa5a9186b6 [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)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +00009 * Copyright (C) 2010 Carl-Daniel Hailfinger
Stefan Reinauer5380d512007-05-24 09:08:36 +000010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
Uwe Hermannd1107642007-08-29 17:52:32 +000022 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Stefan Reinauer5380d512007-05-24 09:08:36 +000023 */
24
Ollie Lho184a4042005-11-26 21:55:36 +000025#include <stdlib.h>
Ollie Lho184a4042005-11-26 21:55:36 +000026#include <sys/types.h>
27#include <string.h>
Adam Kaufman064b1f22007-02-06 19:47:50 +000028#include "flash.h"
Stefan Reinauer2fbe6242008-01-18 16:17:44 +000029#include "coreboot_tables.h"
Ollie Lho184a4042005-11-26 21:55:36 +000030
Uwe Hermanna7e05482007-05-09 10:17:44 +000031char *lb_part = NULL, *lb_vendor = NULL;
Carl-Daniel Hailfingerbc25f942009-07-30 13:30:17 +000032int partvendor_from_cbtable = 0;
Ollie Lho184a4042005-11-26 21:55:36 +000033
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000034void lb_vendor_dev_from_string(char *boardstring)
35{
36 char *tempstr2 = NULL;
37 strtok(boardstring, ":");
38 tempstr2 = strtok(NULL, ":");
39 if (tempstr2) {
40 lb_vendor = boardstring;
41 lb_part = tempstr2;
42 } else {
43 lb_vendor = NULL;
44 lb_part = boardstring;
45 }
46}
47
Ollie Lho184a4042005-11-26 21:55:36 +000048static unsigned long compute_checksum(void *addr, unsigned long length)
49{
50 uint8_t *ptr;
51 volatile union {
Uwe Hermanna7e05482007-05-09 10:17:44 +000052 uint8_t byte[2];
Ollie Lho184a4042005-11-26 21:55:36 +000053 uint16_t word;
Michael Karchere7f32092010-01-12 15:36:24 +000054 } chksum;
Uwe Hermannac309342007-10-10 17:42:20 +000055 unsigned long sum;
56 unsigned long i;
Uwe Hermannffec5f32007-08-23 16:08:21 +000057
Ollie Lho184a4042005-11-26 21:55:36 +000058 /* In the most straight forward way possible,
59 * compute an ip style checksum.
60 */
61 sum = 0;
62 ptr = addr;
Uwe Hermanna7e05482007-05-09 10:17:44 +000063 for (i = 0; i < length; i++) {
Ollie Lho184a4042005-11-26 21:55:36 +000064 unsigned long value;
65 value = ptr[i];
Uwe Hermannac309342007-10-10 17:42:20 +000066 if (i & 1) {
Ollie Lho184a4042005-11-26 21:55:36 +000067 value <<= 8;
Uwe Hermannac309342007-10-10 17:42:20 +000068 }
Ollie Lho184a4042005-11-26 21:55:36 +000069 /* Add the new value */
70 sum += value;
71 /* Wrap around the carry */
Uwe Hermannac309342007-10-10 17:42:20 +000072 if (sum > 0xFFFF) {
Ollie Lho184a4042005-11-26 21:55:36 +000073 sum = (sum + (sum >> 16)) & 0xFFFF;
Uwe Hermannac309342007-10-10 17:42:20 +000074 }
Ollie Lho184a4042005-11-26 21:55:36 +000075 }
Michael Karchere7f32092010-01-12 15:36:24 +000076 chksum.byte[0] = sum & 0xff;
77 chksum.byte[1] = (sum >> 8) & 0xff;
Uwe Hermannffec5f32007-08-23 16:08:21 +000078
Michael Karchere7f32092010-01-12 15:36:24 +000079 return (~chksum.word) & 0xFFFF;
Ollie Lho184a4042005-11-26 21:55:36 +000080}
81
82#define for_each_lbrec(head, rec) \
83 for(rec = (struct lb_record *)(((char *)head) + sizeof(*head)); \
84 (((char *)rec) < (((char *)head) + sizeof(*head) + head->table_bytes)) && \
85 (rec->size >= 1) && \
86 ((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \
Uwe Hermanna7e05482007-05-09 10:17:44 +000087 rec = (struct lb_record *)(((char *)rec) + rec->size))
Ollie Lho184a4042005-11-26 21:55:36 +000088
Uwe Hermanna7e05482007-05-09 10:17:44 +000089static int count_lb_records(struct lb_header *head)
Ollie Lho184a4042005-11-26 21:55:36 +000090{
91 struct lb_record *rec;
92 int count;
Uwe Hermannffec5f32007-08-23 16:08:21 +000093
Ollie Lho184a4042005-11-26 21:55:36 +000094 count = 0;
95 for_each_lbrec(head, rec) {
96 count++;
97 }
Uwe Hermannffec5f32007-08-23 16:08:21 +000098
Ollie Lho184a4042005-11-26 21:55:36 +000099 return count;
100}
101
Uwe Hermanna7e05482007-05-09 10:17:44 +0000102static struct lb_header *find_lb_table(void *base, unsigned long start,
103 unsigned long end)
Ollie Lho184a4042005-11-26 21:55:36 +0000104{
105 unsigned long addr;
Uwe Hermannffec5f32007-08-23 16:08:21 +0000106
Ollie Lho184a4042005-11-26 21:55:36 +0000107 /* For now be stupid.... */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000108 for (addr = start; addr < end; addr += 16) {
109 struct lb_header *head =
110 (struct lb_header *)(((char *)base) + addr);
111 struct lb_record *recs =
112 (struct lb_record *)(((char *)base) + addr + sizeof(*head));
Ollie Lho184a4042005-11-26 21:55:36 +0000113 if (memcmp(head->signature, "LBIO", 4) != 0)
114 continue;
Sean Nelson316a29f2010-05-07 20:09:04 +0000115 msg_pdbg("Found candidate at: %08lx-%08lx\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000116 addr, addr + head->table_bytes);
Ollie Lho184a4042005-11-26 21:55:36 +0000117 if (head->header_bytes != sizeof(*head)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000118 msg_perr("Header bytes of %d are incorrect.\n",
Ollie Lho184a4042005-11-26 21:55:36 +0000119 head->header_bytes);
120 continue;
121 }
122 if (count_lb_records(head) != head->table_entries) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000123 msg_perr("Bad record count: %d.\n",
Ollie Lho184a4042005-11-26 21:55:36 +0000124 head->table_entries);
125 continue;
126 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000127 if (compute_checksum((uint8_t *) head, sizeof(*head)) != 0) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000128 msg_perr("Bad header checksum.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000129 continue;
130 }
131 if (compute_checksum(recs, head->table_bytes)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000132 != head->table_checksum) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000133 msg_perr("Bad table checksum: %04x.\n",
Ollie Lho184a4042005-11-26 21:55:36 +0000134 head->table_checksum);
135 continue;
136 }
Sean Nelson316a29f2010-05-07 20:09:04 +0000137 msg_pdbg("Found coreboot table at 0x%08lx.\n", addr);
Ollie Lho184a4042005-11-26 21:55:36 +0000138 return head;
139
140 };
Uwe Hermannffec5f32007-08-23 16:08:21 +0000141
Ollie Lho184a4042005-11-26 21:55:36 +0000142 return 0;
143}
144
145static void find_mainboard(struct lb_record *ptr, unsigned long addr)
146{
147 struct lb_mainboard *rec;
148 int max_size;
149 char vendor[256], part[256];
Uwe Hermannffec5f32007-08-23 16:08:21 +0000150
Ollie Lho184a4042005-11-26 21:55:36 +0000151 rec = (struct lb_mainboard *)ptr;
152 max_size = rec->size - sizeof(*rec);
Sean Nelson316a29f2010-05-07 20:09:04 +0000153 msg_pdbg("Vendor ID: %.*s, part ID: %.*s\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000154 max_size - rec->vendor_idx,
155 rec->strings + rec->vendor_idx,
156 max_size - rec->part_number_idx,
157 rec->strings + rec->part_number_idx);
158 snprintf(vendor, 255, "%.*s", max_size - rec->vendor_idx,
159 rec->strings + rec->vendor_idx);
Ollie Lho184a4042005-11-26 21:55:36 +0000160 snprintf(part, 255, "%.*s", max_size - rec->part_number_idx,
Uwe Hermanna7e05482007-05-09 10:17:44 +0000161 rec->strings + rec->part_number_idx);
Ollie Lho184a4042005-11-26 21:55:36 +0000162
Uwe Hermanna7e05482007-05-09 10:17:44 +0000163 if (lb_part) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000164 msg_pdbg("Overwritten by command line, vendor ID: %s, part ID: %s.\n", lb_vendor, lb_part);
Ollie Lho184a4042005-11-26 21:55:36 +0000165 } else {
Carl-Daniel Hailfingerbc25f942009-07-30 13:30:17 +0000166 partvendor_from_cbtable = 1;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000167 lb_part = strdup(part);
168 lb_vendor = strdup(vendor);
Ollie Lho184a4042005-11-26 21:55:36 +0000169 }
170}
171
172static struct lb_record *next_record(struct lb_record *rec)
173{
174 return (struct lb_record *)(((char *)rec) + rec->size);
175}
176
Uwe Hermanna7e05482007-05-09 10:17:44 +0000177static void search_lb_records(struct lb_record *rec, struct lb_record *last,
178 unsigned long addr)
Ollie Lho184a4042005-11-26 21:55:36 +0000179{
180 struct lb_record *next;
181 int count;
182 count = 0;
183
Uwe Hermanna7e05482007-05-09 10:17:44 +0000184 for (next = next_record(rec); (rec < last) && (next <= last);
185 rec = next, addr += rec->size) {
Ollie Lho184a4042005-11-26 21:55:36 +0000186 next = next_record(rec);
187 count++;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000188 if (rec->tag == LB_TAG_MAINBOARD) {
189 find_mainboard(rec, addr);
Ollie Lho184a4042005-11-26 21:55:36 +0000190 break;
191 }
192 }
193}
194
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000195#define BYTES_TO_MAP (1024*1024)
Stefan Reinauere3f3e2e2008-01-18 15:33:10 +0000196int coreboot_init(void)
Ollie Lho184a4042005-11-26 21:55:36 +0000197{
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000198 uint8_t *table_area;
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000199 unsigned long addr, start;
Ollie Lho184a4042005-11-26 21:55:36 +0000200 struct lb_header *lb_table;
201 struct lb_record *rec, *last;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000202
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000203#ifdef __DARWIN__
204 /* This is a hack. DirectIO fails to map physical address 0x00000000.
205 * Why?
206 */
207 start = 0x400;
208#else
209 start = 0x0;
210#endif
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000211 table_area = physmap_try_ro("low megabyte", start, BYTES_TO_MAP - start);
212 if (!table_area) {
213 msg_perr("Failed getting access to coreboot low tables.\n");
214 return -1;
215 }
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000216
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000217 lb_table = find_lb_table(table_area, 0x00000, 0x1000);
Ollie Lho184a4042005-11-26 21:55:36 +0000218 if (!lb_table)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000219 lb_table = find_lb_table(table_area, 0xf0000 - start, BYTES_TO_MAP - start);
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000220 if (lb_table) {
221 struct lb_forward *forward = (struct lb_forward *)
222 (((char *)lb_table) + lb_table->header_bytes);
223 if (forward->tag == LB_TAG_FORWARD) {
224 start = forward->forward;
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000225 start &= ~(getpagesize() - 1);
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000226 physunmap(table_area, BYTES_TO_MAP);
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000227 table_area = physmap_try_ro("high tables", start, BYTES_TO_MAP);
228 if (!table_area) {
229 msg_perr("Failed getting access to coreboot "
230 "high tables.\n");
231 return -1;
232 }
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000233 lb_table = find_lb_table(table_area, 0x00000, 0x1000);
234 }
235 }
236
Peter Stuge2dc3aaa2009-01-26 00:15:56 +0000237 if (!lb_table) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000238 msg_pinfo("No coreboot table found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000239 return -1;
240 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000241
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000242 addr = ((char *)lb_table) - ((char *)table_area) + start;
Sean Nelson316a29f2010-05-07 20:09:04 +0000243 msg_pinfo("coreboot table found at 0x%lx.\n",
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000244 (unsigned long)lb_table - (unsigned long)table_area + start);
Peter Stuge2dc3aaa2009-01-26 00:15:56 +0000245 rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes);
246 last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes);
Sean Nelson316a29f2010-05-07 20:09:04 +0000247 msg_pdbg("coreboot header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
Peter Stuge2dc3aaa2009-01-26 00:15:56 +0000248 lb_table->header_bytes, lb_table->header_checksum,
249 lb_table->table_bytes, lb_table->table_checksum,
250 lb_table->table_entries);
251 search_lb_records(rec, last, addr + lb_table->header_bytes);
252
Ollie Lho184a4042005-11-26 21:55:36 +0000253 return 0;
254}