blob: dde12acd2ddad0482fb826747a208a00a4a95852 [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
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000025#include <unistd.h>
26#include <stdio.h>
Ollie Lho184a4042005-11-26 21:55:36 +000027#include <stdlib.h>
Stefan Tauner37e86862012-08-11 16:07:08 +000028#include <ctype.h>
Ollie Lho184a4042005-11-26 21:55:36 +000029#include <string.h>
Adam Kaufman064b1f22007-02-06 19:47:50 +000030#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000031#include "programmer.h"
Stefan Reinauer2fbe6242008-01-18 16:17:44 +000032#include "coreboot_tables.h"
Ollie Lho184a4042005-11-26 21:55:36 +000033
Uwe Hermanna7e05482007-05-09 10:17:44 +000034char *lb_part = NULL, *lb_vendor = NULL;
Carl-Daniel Hailfingerbc25f942009-07-30 13:30:17 +000035int partvendor_from_cbtable = 0;
Stefan Tauner37e86862012-08-11 16:07:08 +000036static char *def_name = "DEFAULT";
37static char *mainboard_vendor = NULL;
38static char *mainboard_part = NULL;
Ollie Lho184a4042005-11-26 21:55:36 +000039
Carl-Daniel Hailfinger2d927fb2012-01-04 00:48:27 +000040/* Parse the [<vendor>:]<board> string specified by the user as part of
41 * -p internal:mainboard=[<vendor>:]<board> and set lb_vendor and lb_part
42 * to the extracted values.
43 * Note: strtok modifies the original string, so we work on a copy and allocate
44 * memory for lb_vendor and lb_part with strdup.
45 */
46void lb_vendor_dev_from_string(const char *boardstring)
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000047{
Carl-Daniel Hailfinger2d927fb2012-01-04 00:48:27 +000048 /* strtok may modify the original string. */
49 char *tempstr = strdup(boardstring);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000050 char *tempstr2 = NULL;
Carl-Daniel Hailfinger2d927fb2012-01-04 00:48:27 +000051 strtok(tempstr, ":");
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000052 tempstr2 = strtok(NULL, ":");
53 if (tempstr2) {
Carl-Daniel Hailfinger2d927fb2012-01-04 00:48:27 +000054 lb_vendor = strdup(tempstr);
55 lb_part = strdup(tempstr2);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000056 } else {
57 lb_vendor = NULL;
Carl-Daniel Hailfinger2d927fb2012-01-04 00:48:27 +000058 lb_part = strdup(tempstr);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000059 }
Carl-Daniel Hailfinger2d927fb2012-01-04 00:48:27 +000060 free(tempstr);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000061}
62
Stefan Tauner37e86862012-08-11 16:07:08 +000063int show_id(uint8_t *bios, int size)
64{
65 unsigned int *walk;
66 unsigned int mb_part_offset, mb_vendor_offset;
67 char *mb_part, *mb_vendor;
68
69 mainboard_vendor = def_name;
70 mainboard_part = def_name;
71
72 walk = (unsigned int *)(bios + size - 0x10);
73 walk--;
74
75 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
76 /* We might have an NVIDIA chipset BIOS which stores the ID information somewhere else. */
77 walk = (unsigned int *)(bios + size - 0x80);
78 walk--;
79 }
80
81 /*
82 * Check if coreboot last image size is 0 or not a multiple of 1k or
83 * bigger than the chip or if the pointers to vendor ID or mainboard ID
84 * are outside the image of if the start of ID strings are nonsensical
85 * (nonprintable and not \0).
86 */
87 mb_part_offset = *(walk - 1);
88 mb_vendor_offset = *(walk - 2);
89 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0 || (*walk) > size ||
90 mb_part_offset > size || mb_vendor_offset > size) {
91 msg_pinfo("Flash image seems to be a legacy BIOS. Disabling coreboot-related checks.\n");
92 return 0;
93 }
94
95 mb_part = (char *)(bios + size - mb_part_offset);
96 mb_vendor = (char *)(bios + size - mb_vendor_offset);
97 if (!isprint((unsigned char)*mb_part) ||
98 !isprint((unsigned char)*mb_vendor)) {
99 msg_pinfo("Flash image seems to have garbage in the ID location. Disabling checks.\n");
100 return 0;
101 }
102
103 msg_pdbg("coreboot last image size (not ROM size) is %d bytes.\n", *walk);
104
105 mainboard_part = strdup(mb_part);
106 mainboard_vendor = strdup(mb_vendor);
107 msg_pdbg("Manufacturer: %s\n", mainboard_vendor);
108 msg_pdbg("Mainboard ID: %s\n", mainboard_part);
109
110 /* If these are not set, the coreboot table was not found. */
111 if (!lb_vendor || !lb_part) {
112 msg_pinfo("Note: If the following flash access fails, try "
113 "-p internal:mainboard=<vendor>:<mainboard>.\n");
114 return 0;
115 }
116
117 /* These comparisons are case insensitive to make things a little less user^Werror prone. */
118 if (!strcasecmp(mainboard_vendor, lb_vendor) &&
119 !strcasecmp(mainboard_part, lb_part)) {
120 msg_pdbg("This firmware image matches this mainboard.\n");
121 } else {
122 if (force_boardmismatch) {
123 msg_pinfo("WARNING: This firmware image does not "
124 "seem to fit to this machine - forcing it.\n");
125 } else {
126 msg_pinfo("ERROR: Your firmware image (%s:%s) does not appear to\n"
127 " be correct for the detected mainboard (%s:%s)\n\n"
128 "Override with -p internal:boardmismatch=force to ignore the board name\n"
129 "in the firmware image or override the detected mainboard with\n"
130 "-p internal:mainboard=<vendor>:<mainboard>.\n\n",
131 mainboard_vendor, mainboard_part, lb_vendor, lb_part);
132 return 1;
133 }
134 }
135
136 return 0;
137}
138
Ollie Lho184a4042005-11-26 21:55:36 +0000139static unsigned long compute_checksum(void *addr, unsigned long length)
140{
141 uint8_t *ptr;
142 volatile union {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000143 uint8_t byte[2];
Ollie Lho184a4042005-11-26 21:55:36 +0000144 uint16_t word;
Michael Karchere7f32092010-01-12 15:36:24 +0000145 } chksum;
Uwe Hermannac309342007-10-10 17:42:20 +0000146 unsigned long sum;
147 unsigned long i;
Uwe Hermannffec5f32007-08-23 16:08:21 +0000148
Ollie Lho184a4042005-11-26 21:55:36 +0000149 /* In the most straight forward way possible,
150 * compute an ip style checksum.
151 */
152 sum = 0;
153 ptr = addr;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000154 for (i = 0; i < length; i++) {
Ollie Lho184a4042005-11-26 21:55:36 +0000155 unsigned long value;
156 value = ptr[i];
Uwe Hermannac309342007-10-10 17:42:20 +0000157 if (i & 1) {
Ollie Lho184a4042005-11-26 21:55:36 +0000158 value <<= 8;
Uwe Hermannac309342007-10-10 17:42:20 +0000159 }
Ollie Lho184a4042005-11-26 21:55:36 +0000160 /* Add the new value */
161 sum += value;
162 /* Wrap around the carry */
Uwe Hermannac309342007-10-10 17:42:20 +0000163 if (sum > 0xFFFF) {
Ollie Lho184a4042005-11-26 21:55:36 +0000164 sum = (sum + (sum >> 16)) & 0xFFFF;
Uwe Hermannac309342007-10-10 17:42:20 +0000165 }
Ollie Lho184a4042005-11-26 21:55:36 +0000166 }
Michael Karchere7f32092010-01-12 15:36:24 +0000167 chksum.byte[0] = sum & 0xff;
168 chksum.byte[1] = (sum >> 8) & 0xff;
Uwe Hermannffec5f32007-08-23 16:08:21 +0000169
Michael Karchere7f32092010-01-12 15:36:24 +0000170 return (~chksum.word) & 0xFFFF;
Ollie Lho184a4042005-11-26 21:55:36 +0000171}
172
173#define for_each_lbrec(head, rec) \
174 for(rec = (struct lb_record *)(((char *)head) + sizeof(*head)); \
175 (((char *)rec) < (((char *)head) + sizeof(*head) + head->table_bytes)) && \
176 (rec->size >= 1) && \
177 ((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \
Uwe Hermanna7e05482007-05-09 10:17:44 +0000178 rec = (struct lb_record *)(((char *)rec) + rec->size))
Ollie Lho184a4042005-11-26 21:55:36 +0000179
Uwe Hermanna7e05482007-05-09 10:17:44 +0000180static int count_lb_records(struct lb_header *head)
Ollie Lho184a4042005-11-26 21:55:36 +0000181{
182 struct lb_record *rec;
183 int count;
Uwe Hermannffec5f32007-08-23 16:08:21 +0000184
Ollie Lho184a4042005-11-26 21:55:36 +0000185 count = 0;
186 for_each_lbrec(head, rec) {
187 count++;
188 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000189
Ollie Lho184a4042005-11-26 21:55:36 +0000190 return count;
191}
192
Uwe Hermanna7e05482007-05-09 10:17:44 +0000193static struct lb_header *find_lb_table(void *base, unsigned long start,
194 unsigned long end)
Ollie Lho184a4042005-11-26 21:55:36 +0000195{
196 unsigned long addr;
Uwe Hermannffec5f32007-08-23 16:08:21 +0000197
Ollie Lho184a4042005-11-26 21:55:36 +0000198 /* For now be stupid.... */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000199 for (addr = start; addr < end; addr += 16) {
200 struct lb_header *head =
201 (struct lb_header *)(((char *)base) + addr);
202 struct lb_record *recs =
203 (struct lb_record *)(((char *)base) + addr + sizeof(*head));
Ollie Lho184a4042005-11-26 21:55:36 +0000204 if (memcmp(head->signature, "LBIO", 4) != 0)
205 continue;
Sean Nelson316a29f2010-05-07 20:09:04 +0000206 msg_pdbg("Found candidate at: %08lx-%08lx\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000207 addr, addr + head->table_bytes);
Ollie Lho184a4042005-11-26 21:55:36 +0000208 if (head->header_bytes != sizeof(*head)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000209 msg_perr("Header bytes of %d are incorrect.\n",
Ollie Lho184a4042005-11-26 21:55:36 +0000210 head->header_bytes);
211 continue;
212 }
213 if (count_lb_records(head) != head->table_entries) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000214 msg_perr("Bad record count: %d.\n",
Ollie Lho184a4042005-11-26 21:55:36 +0000215 head->table_entries);
216 continue;
217 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000218 if (compute_checksum((uint8_t *) head, sizeof(*head)) != 0) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000219 msg_perr("Bad header checksum.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000220 continue;
221 }
222 if (compute_checksum(recs, head->table_bytes)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000223 != head->table_checksum) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000224 msg_perr("Bad table checksum: %04x.\n",
Ollie Lho184a4042005-11-26 21:55:36 +0000225 head->table_checksum);
226 continue;
227 }
Sean Nelson316a29f2010-05-07 20:09:04 +0000228 msg_pdbg("Found coreboot table at 0x%08lx.\n", addr);
Ollie Lho184a4042005-11-26 21:55:36 +0000229 return head;
230
231 };
Uwe Hermannffec5f32007-08-23 16:08:21 +0000232
Peter Huewe73f8ec82011-01-24 19:15:51 +0000233 return NULL;
Ollie Lho184a4042005-11-26 21:55:36 +0000234}
235
236static void find_mainboard(struct lb_record *ptr, unsigned long addr)
237{
238 struct lb_mainboard *rec;
239 int max_size;
240 char vendor[256], part[256];
Uwe Hermannffec5f32007-08-23 16:08:21 +0000241
Ollie Lho184a4042005-11-26 21:55:36 +0000242 rec = (struct lb_mainboard *)ptr;
243 max_size = rec->size - sizeof(*rec);
Sean Nelson316a29f2010-05-07 20:09:04 +0000244 msg_pdbg("Vendor ID: %.*s, part ID: %.*s\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000245 max_size - rec->vendor_idx,
246 rec->strings + rec->vendor_idx,
247 max_size - rec->part_number_idx,
248 rec->strings + rec->part_number_idx);
249 snprintf(vendor, 255, "%.*s", max_size - rec->vendor_idx,
250 rec->strings + rec->vendor_idx);
Ollie Lho184a4042005-11-26 21:55:36 +0000251 snprintf(part, 255, "%.*s", max_size - rec->part_number_idx,
Uwe Hermanna7e05482007-05-09 10:17:44 +0000252 rec->strings + rec->part_number_idx);
Ollie Lho184a4042005-11-26 21:55:36 +0000253
Uwe Hermanna7e05482007-05-09 10:17:44 +0000254 if (lb_part) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000255 msg_pdbg("Overwritten by command line, vendor ID: %s, part ID: %s.\n", lb_vendor, lb_part);
Ollie Lho184a4042005-11-26 21:55:36 +0000256 } else {
Carl-Daniel Hailfingerbc25f942009-07-30 13:30:17 +0000257 partvendor_from_cbtable = 1;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000258 lb_part = strdup(part);
259 lb_vendor = strdup(vendor);
Ollie Lho184a4042005-11-26 21:55:36 +0000260 }
261}
262
263static struct lb_record *next_record(struct lb_record *rec)
264{
265 return (struct lb_record *)(((char *)rec) + rec->size);
266}
267
Uwe Hermanna7e05482007-05-09 10:17:44 +0000268static void search_lb_records(struct lb_record *rec, struct lb_record *last,
269 unsigned long addr)
Ollie Lho184a4042005-11-26 21:55:36 +0000270{
271 struct lb_record *next;
272 int count;
273 count = 0;
274
Uwe Hermanna7e05482007-05-09 10:17:44 +0000275 for (next = next_record(rec); (rec < last) && (next <= last);
276 rec = next, addr += rec->size) {
Ollie Lho184a4042005-11-26 21:55:36 +0000277 next = next_record(rec);
278 count++;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000279 if (rec->tag == LB_TAG_MAINBOARD) {
280 find_mainboard(rec, addr);
Ollie Lho184a4042005-11-26 21:55:36 +0000281 break;
282 }
283 }
284}
285
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000286#define BYTES_TO_MAP (1024*1024)
Stefan Reinauere3f3e2e2008-01-18 15:33:10 +0000287int coreboot_init(void)
Ollie Lho184a4042005-11-26 21:55:36 +0000288{
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000289 uint8_t *table_area;
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000290 unsigned long addr, start;
Ollie Lho184a4042005-11-26 21:55:36 +0000291 struct lb_header *lb_table;
292 struct lb_record *rec, *last;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000293
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +0000294#if defined(__MACH__) && defined(__APPLE__)
Carl-Daniel Hailfingerf992c192010-10-06 23:16:10 +0000295 /* This is a hack. DirectHW fails to map physical address 0x00000000.
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000296 * Why?
297 */
298 start = 0x400;
299#else
300 start = 0x0;
301#endif
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000302 table_area = physmap_try_ro("low megabyte", start, BYTES_TO_MAP - start);
Patrick Georgied7a9642010-09-25 22:53:44 +0000303 if (ERROR_PTR == table_area) {
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000304 msg_perr("Failed getting access to coreboot low tables.\n");
305 return -1;
306 }
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000307
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000308 lb_table = find_lb_table(table_area, 0x00000, 0x1000);
Ollie Lho184a4042005-11-26 21:55:36 +0000309 if (!lb_table)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000310 lb_table = find_lb_table(table_area, 0xf0000 - start, BYTES_TO_MAP - start);
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000311 if (lb_table) {
312 struct lb_forward *forward = (struct lb_forward *)
313 (((char *)lb_table) + lb_table->header_bytes);
314 if (forward->tag == LB_TAG_FORWARD) {
315 start = forward->forward;
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000316 start &= ~(getpagesize() - 1);
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000317 physunmap(table_area, BYTES_TO_MAP);
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000318 table_area = physmap_try_ro("high tables", start, BYTES_TO_MAP);
Patrick Georgied7a9642010-09-25 22:53:44 +0000319 if (ERROR_PTR == table_area) {
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000320 msg_perr("Failed getting access to coreboot "
321 "high tables.\n");
322 return -1;
323 }
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000324 lb_table = find_lb_table(table_area, 0x00000, 0x1000);
325 }
326 }
327
Peter Stuge2dc3aaa2009-01-26 00:15:56 +0000328 if (!lb_table) {
Stefan Reinauer12a04eb2011-04-01 18:05:20 +0000329 msg_pdbg("No coreboot table found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000330 return -1;
331 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000332
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000333 addr = ((char *)lb_table) - ((char *)table_area) + start;
Sean Nelson316a29f2010-05-07 20:09:04 +0000334 msg_pinfo("coreboot table found at 0x%lx.\n",
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000335 (unsigned long)lb_table - (unsigned long)table_area + start);
Peter Stuge2dc3aaa2009-01-26 00:15:56 +0000336 rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes);
337 last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes);
Sean Nelson316a29f2010-05-07 20:09:04 +0000338 msg_pdbg("coreboot header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
Peter Stuge2dc3aaa2009-01-26 00:15:56 +0000339 lb_table->header_bytes, lb_table->header_checksum,
340 lb_table->table_bytes, lb_table->table_checksum,
341 lb_table->table_entries);
342 search_lb_records(rec, last, addr + lb_table->header_bytes);
343
Ollie Lho184a4042005-11-26 21:55:36 +0000344 return 0;
345}