blob: bd83ea5c911d3efd0666ca7486f18d5ddecdb1c4 [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>
Stefan Tauner37e86862012-08-11 16:07:08 +000027#include <ctype.h>
Ollie Lho184a4042005-11-26 21:55:36 +000028#include <string.h>
Adam Kaufman064b1f22007-02-06 19:47:50 +000029#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000030#include "programmer.h"
Stefan Reinauer2fbe6242008-01-18 16:17:44 +000031#include "coreboot_tables.h"
Ollie Lho184a4042005-11-26 21:55:36 +000032
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000033static char *cb_vendor = NULL, *cb_model = NULL;
Ollie Lho184a4042005-11-26 21:55:36 +000034
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000035/* Tries to find coreboot IDs in the supplied image and compares them to the current IDs.
36 * Returns...
37 * -1 if IDs in the image do not match the IDs embedded in the current firmware,
38 * 0 if the IDs could not be found in the image or if they match correctly.
Carl-Daniel Hailfinger2d927fb2012-01-04 00:48:27 +000039 */
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000040int cb_check_image(uint8_t *image, int size)
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000041{
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000042 const char *image_vendor = NULL;
43 const char *image_model = NULL;
Stefan Tauner37e86862012-08-11 16:07:08 +000044 unsigned int *walk;
45 unsigned int mb_part_offset, mb_vendor_offset;
46 char *mb_part, *mb_vendor;
47
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000048 walk = (unsigned int *)(image + size - 0x10);
Stefan Tauner37e86862012-08-11 16:07:08 +000049 walk--;
50
51 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000052 /* Some NVIDIA chipsets store chipset soft straps (IIRC Hypertransport init info etc.) in
53 * flash at exactly the location where coreboot image size, coreboot vendor name pointer and
54 * coreboot board name pointer are usually stored. In this case coreboot uses an alternate
55 * location for the coreboot image data. */
56 walk = (unsigned int *)(image + size - 0x80);
Stefan Tauner37e86862012-08-11 16:07:08 +000057 walk--;
58 }
59
60 /*
61 * Check if coreboot last image size is 0 or not a multiple of 1k or
62 * bigger than the chip or if the pointers to vendor ID or mainboard ID
63 * are outside the image of if the start of ID strings are nonsensical
64 * (nonprintable and not \0).
65 */
66 mb_part_offset = *(walk - 1);
67 mb_vendor_offset = *(walk - 2);
68 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0 || (*walk) > size ||
69 mb_part_offset > size || mb_vendor_offset > size) {
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000070 msg_pdbg("Flash image seems to be a legacy BIOS. Disabling coreboot-related checks.\n");
Stefan Tauner37e86862012-08-11 16:07:08 +000071 return 0;
72 }
73
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000074 mb_part = (char *)(image + size - mb_part_offset);
75 mb_vendor = (char *)(image + size - mb_vendor_offset);
Stefan Tauner37e86862012-08-11 16:07:08 +000076 if (!isprint((unsigned char)*mb_part) ||
77 !isprint((unsigned char)*mb_vendor)) {
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000078 msg_pdbg("Flash image seems to have garbage in the ID location. "
79 "Disabling coreboot-related checks.\n");
Stefan Tauner37e86862012-08-11 16:07:08 +000080 return 0;
81 }
82
83 msg_pdbg("coreboot last image size (not ROM size) is %d bytes.\n", *walk);
84
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000085 image_vendor = strdup(mb_vendor);
86 image_model = strdup(mb_part);
87 msg_pdbg("Manufacturer: %s\n", image_vendor);
88 msg_pdbg("Mainboard ID: %s\n", image_model);
Stefan Tauner37e86862012-08-11 16:07:08 +000089
90 /* If these are not set, the coreboot table was not found. */
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000091 if (!cb_vendor || !cb_model)
Stefan Tauner37e86862012-08-11 16:07:08 +000092 return 0;
Stefan Tauner37e86862012-08-11 16:07:08 +000093
94 /* These comparisons are case insensitive to make things a little less user^Werror prone. */
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000095 if (!strcasecmp(image_vendor, cb_vendor) && !strcasecmp(image_model, cb_model)) {
96 msg_pdbg2("This coreboot image matches this mainboard.\n");
Stefan Tauner37e86862012-08-11 16:07:08 +000097 } else {
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000098 msg_pinfo("WARNING: This coreboot image (%s:%s) does not appear to\n"
99 " be correct for the detected mainboard (%s:%s).\n",
100 image_vendor, image_model, cb_vendor, cb_model);
101 return -1;
Stefan Tauner37e86862012-08-11 16:07:08 +0000102 }
103
104 return 0;
105}
106
Ollie Lho184a4042005-11-26 21:55:36 +0000107static unsigned long compute_checksum(void *addr, unsigned long length)
108{
109 uint8_t *ptr;
110 volatile union {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000111 uint8_t byte[2];
Ollie Lho184a4042005-11-26 21:55:36 +0000112 uint16_t word;
Michael Karchere7f32092010-01-12 15:36:24 +0000113 } chksum;
Uwe Hermannac309342007-10-10 17:42:20 +0000114 unsigned long sum;
115 unsigned long i;
Uwe Hermannffec5f32007-08-23 16:08:21 +0000116
Ollie Lho184a4042005-11-26 21:55:36 +0000117 /* In the most straight forward way possible,
118 * compute an ip style checksum.
119 */
120 sum = 0;
121 ptr = addr;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000122 for (i = 0; i < length; i++) {
Ollie Lho184a4042005-11-26 21:55:36 +0000123 unsigned long value;
124 value = ptr[i];
Uwe Hermannac309342007-10-10 17:42:20 +0000125 if (i & 1) {
Ollie Lho184a4042005-11-26 21:55:36 +0000126 value <<= 8;
Uwe Hermannac309342007-10-10 17:42:20 +0000127 }
Ollie Lho184a4042005-11-26 21:55:36 +0000128 /* Add the new value */
129 sum += value;
130 /* Wrap around the carry */
Uwe Hermannac309342007-10-10 17:42:20 +0000131 if (sum > 0xFFFF) {
Ollie Lho184a4042005-11-26 21:55:36 +0000132 sum = (sum + (sum >> 16)) & 0xFFFF;
Uwe Hermannac309342007-10-10 17:42:20 +0000133 }
Ollie Lho184a4042005-11-26 21:55:36 +0000134 }
Michael Karchere7f32092010-01-12 15:36:24 +0000135 chksum.byte[0] = sum & 0xff;
136 chksum.byte[1] = (sum >> 8) & 0xff;
Uwe Hermannffec5f32007-08-23 16:08:21 +0000137
Michael Karchere7f32092010-01-12 15:36:24 +0000138 return (~chksum.word) & 0xFFFF;
Ollie Lho184a4042005-11-26 21:55:36 +0000139}
140
141#define for_each_lbrec(head, rec) \
142 for(rec = (struct lb_record *)(((char *)head) + sizeof(*head)); \
143 (((char *)rec) < (((char *)head) + sizeof(*head) + head->table_bytes)) && \
144 (rec->size >= 1) && \
145 ((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \
Uwe Hermanna7e05482007-05-09 10:17:44 +0000146 rec = (struct lb_record *)(((char *)rec) + rec->size))
Ollie Lho184a4042005-11-26 21:55:36 +0000147
Uwe Hermanna7e05482007-05-09 10:17:44 +0000148static int count_lb_records(struct lb_header *head)
Ollie Lho184a4042005-11-26 21:55:36 +0000149{
150 struct lb_record *rec;
151 int count;
Uwe Hermannffec5f32007-08-23 16:08:21 +0000152
Ollie Lho184a4042005-11-26 21:55:36 +0000153 count = 0;
154 for_each_lbrec(head, rec) {
155 count++;
156 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000157
Ollie Lho184a4042005-11-26 21:55:36 +0000158 return count;
159}
160
Uwe Hermanna7e05482007-05-09 10:17:44 +0000161static struct lb_header *find_lb_table(void *base, unsigned long start,
162 unsigned long end)
Ollie Lho184a4042005-11-26 21:55:36 +0000163{
164 unsigned long addr;
Uwe Hermannffec5f32007-08-23 16:08:21 +0000165
Ollie Lho184a4042005-11-26 21:55:36 +0000166 /* For now be stupid.... */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000167 for (addr = start; addr < end; addr += 16) {
168 struct lb_header *head =
169 (struct lb_header *)(((char *)base) + addr);
170 struct lb_record *recs =
171 (struct lb_record *)(((char *)base) + addr + sizeof(*head));
Ollie Lho184a4042005-11-26 21:55:36 +0000172 if (memcmp(head->signature, "LBIO", 4) != 0)
173 continue;
Sean Nelson316a29f2010-05-07 20:09:04 +0000174 msg_pdbg("Found candidate at: %08lx-%08lx\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000175 addr, addr + head->table_bytes);
Ollie Lho184a4042005-11-26 21:55:36 +0000176 if (head->header_bytes != sizeof(*head)) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000177 msg_perr("Header bytes of %d are incorrect.\n",
Ollie Lho184a4042005-11-26 21:55:36 +0000178 head->header_bytes);
179 continue;
180 }
181 if (count_lb_records(head) != head->table_entries) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000182 msg_perr("Bad record count: %d.\n",
Ollie Lho184a4042005-11-26 21:55:36 +0000183 head->table_entries);
184 continue;
185 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000186 if (compute_checksum((uint8_t *) head, sizeof(*head)) != 0) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000187 msg_perr("Bad header checksum.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000188 continue;
189 }
190 if (compute_checksum(recs, head->table_bytes)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000191 != head->table_checksum) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000192 msg_perr("Bad table checksum: %04x.\n",
Ollie Lho184a4042005-11-26 21:55:36 +0000193 head->table_checksum);
194 continue;
195 }
Sean Nelson316a29f2010-05-07 20:09:04 +0000196 msg_pdbg("Found coreboot table at 0x%08lx.\n", addr);
Ollie Lho184a4042005-11-26 21:55:36 +0000197 return head;
198
199 };
Uwe Hermannffec5f32007-08-23 16:08:21 +0000200
Peter Huewe73f8ec82011-01-24 19:15:51 +0000201 return NULL;
Ollie Lho184a4042005-11-26 21:55:36 +0000202}
203
204static void find_mainboard(struct lb_record *ptr, unsigned long addr)
205{
206 struct lb_mainboard *rec;
207 int max_size;
208 char vendor[256], part[256];
Uwe Hermannffec5f32007-08-23 16:08:21 +0000209
Ollie Lho184a4042005-11-26 21:55:36 +0000210 rec = (struct lb_mainboard *)ptr;
211 max_size = rec->size - sizeof(*rec);
Sean Nelson316a29f2010-05-07 20:09:04 +0000212 msg_pdbg("Vendor ID: %.*s, part ID: %.*s\n",
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000213 max_size - rec->vendor_idx,
214 rec->strings + rec->vendor_idx,
215 max_size - rec->part_number_idx,
216 rec->strings + rec->part_number_idx);
217 snprintf(vendor, 255, "%.*s", max_size - rec->vendor_idx, rec->strings + rec->vendor_idx);
218 snprintf(part, 255, "%.*s", max_size - rec->part_number_idx, rec->strings + rec->part_number_idx);
Ollie Lho184a4042005-11-26 21:55:36 +0000219
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000220 cb_vendor = strdup(vendor);
221 cb_model = strdup(part);
Ollie Lho184a4042005-11-26 21:55:36 +0000222}
223
224static struct lb_record *next_record(struct lb_record *rec)
225{
226 return (struct lb_record *)(((char *)rec) + rec->size);
227}
228
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000229static void search_lb_records(struct lb_record *rec, struct lb_record *last, unsigned long addr)
Ollie Lho184a4042005-11-26 21:55:36 +0000230{
231 struct lb_record *next;
232 int count;
233 count = 0;
234
Uwe Hermanna7e05482007-05-09 10:17:44 +0000235 for (next = next_record(rec); (rec < last) && (next <= last);
236 rec = next, addr += rec->size) {
Ollie Lho184a4042005-11-26 21:55:36 +0000237 next = next_record(rec);
238 count++;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000239 if (rec->tag == LB_TAG_MAINBOARD) {
240 find_mainboard(rec, addr);
Ollie Lho184a4042005-11-26 21:55:36 +0000241 break;
242 }
243 }
244}
245
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000246#define BYTES_TO_MAP (1024*1024)
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000247/* returns 0 if the table was parsed successfully and cb_vendor/cb_model have been set. */
248int cb_parse_table(const char **vendor, const char **model)
Ollie Lho184a4042005-11-26 21:55:36 +0000249{
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000250 uint8_t *table_area;
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000251 unsigned long addr, start;
Ollie Lho184a4042005-11-26 21:55:36 +0000252 struct lb_header *lb_table;
253 struct lb_record *rec, *last;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000254
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +0000255#if defined(__MACH__) && defined(__APPLE__)
Carl-Daniel Hailfingerf992c192010-10-06 23:16:10 +0000256 /* This is a hack. DirectHW fails to map physical address 0x00000000.
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000257 * Why?
258 */
259 start = 0x400;
260#else
261 start = 0x0;
262#endif
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000263 table_area = physmap_try_ro("low megabyte", start, BYTES_TO_MAP - start);
Patrick Georgied7a9642010-09-25 22:53:44 +0000264 if (ERROR_PTR == table_area) {
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000265 msg_perr("Failed getting access to coreboot low tables.\n");
266 return -1;
267 }
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000268
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000269 lb_table = find_lb_table(table_area, 0x00000, 0x1000);
Ollie Lho184a4042005-11-26 21:55:36 +0000270 if (!lb_table)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000271 lb_table = find_lb_table(table_area, 0xf0000 - start, BYTES_TO_MAP - start);
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000272 if (lb_table) {
273 struct lb_forward *forward = (struct lb_forward *)
274 (((char *)lb_table) + lb_table->header_bytes);
275 if (forward->tag == LB_TAG_FORWARD) {
276 start = forward->forward;
Uwe Hermann7b2969b2009-04-15 10:52:49 +0000277 start &= ~(getpagesize() - 1);
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000278 physunmap(table_area, BYTES_TO_MAP);
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000279 table_area = physmap_try_ro("high tables", start, BYTES_TO_MAP);
Patrick Georgied7a9642010-09-25 22:53:44 +0000280 if (ERROR_PTR == table_area) {
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000281 msg_perr("Failed getting access to coreboot high tables.\n");
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000282 return -1;
283 }
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000284 lb_table = find_lb_table(table_area, 0x00000, 0x1000);
285 }
286 }
287
Peter Stuge2dc3aaa2009-01-26 00:15:56 +0000288 if (!lb_table) {
Stefan Reinauer12a04eb2011-04-01 18:05:20 +0000289 msg_pdbg("No coreboot table found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000290 return -1;
291 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000292
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000293 addr = ((char *)lb_table) - ((char *)table_area) + start;
Sean Nelson316a29f2010-05-07 20:09:04 +0000294 msg_pinfo("coreboot table found at 0x%lx.\n",
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000295 (unsigned long)lb_table - (unsigned long)table_area + start);
Peter Stuge2dc3aaa2009-01-26 00:15:56 +0000296 rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes);
297 last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes);
Sean Nelson316a29f2010-05-07 20:09:04 +0000298 msg_pdbg("coreboot header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
Peter Stuge2dc3aaa2009-01-26 00:15:56 +0000299 lb_table->header_bytes, lb_table->header_checksum,
300 lb_table->table_bytes, lb_table->table_checksum,
301 lb_table->table_entries);
302 search_lb_records(rec, last, addr + lb_table->header_bytes);
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000303 *vendor = cb_vendor;
304 *model = cb_model;
Ollie Lho184a4042005-11-26 21:55:36 +0000305 return 0;
306}