blob: 660b487b7da2e165ff60eed73c8c7647ea918724 [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.
Stefan Reinauer5380d512007-05-24 09:08:36 +000019 */
20
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000021#include <unistd.h>
22#include <stdio.h>
Stefan Tauner37e86862012-08-11 16:07:08 +000023#include <ctype.h>
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +000024#include <strings.h>
Ollie Lho184a4042005-11-26 21:55:36 +000025#include <string.h>
Adam Kaufman064b1f22007-02-06 19:47:50 +000026#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000027#include "programmer.h"
Stefan Reinauer2fbe6242008-01-18 16:17:44 +000028#include "coreboot_tables.h"
Thomas Heijligen74b4aa02021-12-14 17:52:30 +010029#include "hwaccess_physmap.h"
Ollie Lho184a4042005-11-26 21:55:36 +000030
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000031static char *cb_vendor = NULL, *cb_model = NULL;
Ollie Lho184a4042005-11-26 21:55:36 +000032
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000033/* Tries to find coreboot IDs in the supplied image and compares them to the current IDs.
34 * Returns...
Elyes HAOUASac01baa2018-05-28 16:52:21 +020035 * -1 if IDs in the image do not match the IDs embedded in the current firmware,
36 * 0 if the IDs could not be found in the image or if they match correctly.
Carl-Daniel Hailfinger2d927fb2012-01-04 00:48:27 +000037 */
Nico Huber519be662018-12-23 20:03:35 +010038int cb_check_image(const uint8_t *image, unsigned int size)
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000039{
Nico Huber441d2a42016-05-02 11:39:35 +020040 const unsigned int *walk;
Stefan Tauner37e86862012-08-11 16:07:08 +000041 unsigned int mb_part_offset, mb_vendor_offset;
Nico Huber441d2a42016-05-02 11:39:35 +020042 const char *mb_part, *mb_vendor;
Stefan Tauner37e86862012-08-11 16:07:08 +000043
Nico Huber441d2a42016-05-02 11:39:35 +020044 walk = (const unsigned int *)(image + size - 0x10);
Stefan Tauner37e86862012-08-11 16:07:08 +000045 walk--;
46
47 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000048 /* Some NVIDIA chipsets store chipset soft straps (IIRC Hypertransport init info etc.) in
49 * flash at exactly the location where coreboot image size, coreboot vendor name pointer and
50 * coreboot board name pointer are usually stored. In this case coreboot uses an alternate
51 * location for the coreboot image data. */
Nico Huber441d2a42016-05-02 11:39:35 +020052 walk = (const unsigned int *)(image + size - 0x80);
Stefan Tauner37e86862012-08-11 16:07:08 +000053 walk--;
54 }
55
56 /*
57 * Check if coreboot last image size is 0 or not a multiple of 1k or
58 * bigger than the chip or if the pointers to vendor ID or mainboard ID
59 * are outside the image of if the start of ID strings are nonsensical
60 * (nonprintable and not \0).
61 */
62 mb_part_offset = *(walk - 1);
63 mb_vendor_offset = *(walk - 2);
64 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0 || (*walk) > size ||
65 mb_part_offset > size || mb_vendor_offset > size) {
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000066 msg_pdbg("Flash image seems to be a legacy BIOS. Disabling coreboot-related checks.\n");
Stefan Tauner37e86862012-08-11 16:07:08 +000067 return 0;
68 }
69
Nico Huber441d2a42016-05-02 11:39:35 +020070 mb_part = (const char *)(image + size - mb_part_offset);
71 mb_vendor = (const char *)(image + size - mb_vendor_offset);
Stefan Tauner37e86862012-08-11 16:07:08 +000072 if (!isprint((unsigned char)*mb_part) ||
73 !isprint((unsigned char)*mb_vendor)) {
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000074 msg_pdbg("Flash image seems to have garbage in the ID location. "
75 "Disabling coreboot-related checks.\n");
Stefan Tauner37e86862012-08-11 16:07:08 +000076 return 0;
77 }
78
79 msg_pdbg("coreboot last image size (not ROM size) is %d bytes.\n", *walk);
80
Paul Menzel592d99c2014-11-01 23:12:33 +000081 msg_pdbg("Manufacturer: %s\n", mb_vendor);
82 msg_pdbg("Mainboard ID: %s\n", mb_part);
Stefan Tauner37e86862012-08-11 16:07:08 +000083
84 /* If these are not set, the coreboot table was not found. */
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000085 if (!cb_vendor || !cb_model)
Stefan Tauner37e86862012-08-11 16:07:08 +000086 return 0;
Stefan Tauner37e86862012-08-11 16:07:08 +000087
88 /* These comparisons are case insensitive to make things a little less user^Werror prone. */
Paul Menzel592d99c2014-11-01 23:12:33 +000089 if (!strcasecmp(mb_vendor, cb_vendor) && !strcasecmp(mb_part, cb_model)) {
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000090 msg_pdbg2("This coreboot image matches this mainboard.\n");
Stefan Tauner37e86862012-08-11 16:07:08 +000091 } else {
Stefan Taunerc6fa32d2013-01-04 22:54:07 +000092 msg_perr("This coreboot image (%s:%s) does not appear to\n"
93 "be correct for the detected mainboard (%s:%s).\n",
Paul Menzel592d99c2014-11-01 23:12:33 +000094 mb_vendor, mb_part, cb_vendor, cb_model);
Stefan Taunerb4e06bd2012-08-20 00:24:22 +000095 return -1;
Stefan Tauner37e86862012-08-11 16:07:08 +000096 }
97
98 return 0;
99}
100
Ollie Lho184a4042005-11-26 21:55:36 +0000101static unsigned long compute_checksum(void *addr, unsigned long length)
102{
103 uint8_t *ptr;
104 volatile union {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000105 uint8_t byte[2];
Ollie Lho184a4042005-11-26 21:55:36 +0000106 uint16_t word;
Michael Karchere7f32092010-01-12 15:36:24 +0000107 } chksum;
Uwe Hermannac309342007-10-10 17:42:20 +0000108 unsigned long sum;
109 unsigned long i;
Uwe Hermannffec5f32007-08-23 16:08:21 +0000110
Ollie Lho184a4042005-11-26 21:55:36 +0000111 /* In the most straight forward way possible,
112 * compute an ip style checksum.
113 */
114 sum = 0;
115 ptr = addr;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000116 for (i = 0; i < length; i++) {
Ollie Lho184a4042005-11-26 21:55:36 +0000117 unsigned long value;
118 value = ptr[i];
Uwe Hermannac309342007-10-10 17:42:20 +0000119 if (i & 1) {
Ollie Lho184a4042005-11-26 21:55:36 +0000120 value <<= 8;
Uwe Hermannac309342007-10-10 17:42:20 +0000121 }
Ollie Lho184a4042005-11-26 21:55:36 +0000122 /* Add the new value */
123 sum += value;
124 /* Wrap around the carry */
Uwe Hermannac309342007-10-10 17:42:20 +0000125 if (sum > 0xFFFF) {
Ollie Lho184a4042005-11-26 21:55:36 +0000126 sum = (sum + (sum >> 16)) & 0xFFFF;
Uwe Hermannac309342007-10-10 17:42:20 +0000127 }
Ollie Lho184a4042005-11-26 21:55:36 +0000128 }
Michael Karchere7f32092010-01-12 15:36:24 +0000129 chksum.byte[0] = sum & 0xff;
130 chksum.byte[1] = (sum >> 8) & 0xff;
Uwe Hermannffec5f32007-08-23 16:08:21 +0000131
Michael Karchere7f32092010-01-12 15:36:24 +0000132 return (~chksum.word) & 0xFFFF;
Ollie Lho184a4042005-11-26 21:55:36 +0000133}
134
135#define for_each_lbrec(head, rec) \
136 for(rec = (struct lb_record *)(((char *)head) + sizeof(*head)); \
137 (((char *)rec) < (((char *)head) + sizeof(*head) + head->table_bytes)) && \
138 (rec->size >= 1) && \
139 ((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \
Uwe Hermanna7e05482007-05-09 10:17:44 +0000140 rec = (struct lb_record *)(((char *)rec) + rec->size))
Ollie Lho184a4042005-11-26 21:55:36 +0000141
Nico Huber519be662018-12-23 20:03:35 +0100142static unsigned int count_lb_records(struct lb_header *head)
Ollie Lho184a4042005-11-26 21:55:36 +0000143{
144 struct lb_record *rec;
Nico Huber519be662018-12-23 20:03:35 +0100145 unsigned int count;
Uwe Hermannffec5f32007-08-23 16:08:21 +0000146
Ollie Lho184a4042005-11-26 21:55:36 +0000147 count = 0;
148 for_each_lbrec(head, rec) {
149 count++;
150 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000151
Ollie Lho184a4042005-11-26 21:55:36 +0000152 return count;
153}
154
Edward O'Callaghan4a55e682019-11-26 23:28:05 +1100155static int lb_header_valid(struct lb_header *head, unsigned long addr)
156{
157 if (memcmp(head->signature, "LBIO", 4) != 0)
158 return 0;
159 msg_pdbg("Found candidate at: %08lx-%08lx\n",
160 addr, addr + sizeof(*head) + head->table_bytes);
161 if (head->header_bytes != sizeof(*head)) {
162 msg_perr("Header bytes of %d are incorrect.\n",
163 head->header_bytes);
164 return 0;
165 }
166 if (compute_checksum((uint8_t *) head, sizeof(*head)) != 0) {
167 msg_perr("Bad header checksum.\n");
168 return 0;
169 }
170
171 return 1;
172}
173
Edward O'Callaghan1d80d642019-11-26 23:31:06 +1100174static int lb_table_valid(struct lb_header *head, struct lb_record *recs)
175{
176 if (compute_checksum(recs, head->table_bytes)
177 != head->table_checksum) {
178 msg_perr("Bad table checksum: %04x.\n",
179 head->table_checksum);
180 return 0;
181 }
182 if (count_lb_records(head) != head->table_entries) {
183 msg_perr("Bad record count: %d.\n",
184 head->table_entries);
185 return 0;
186 }
187
188 return 1;
189}
190
Uwe Hermanna7e05482007-05-09 10:17:44 +0000191static struct lb_header *find_lb_table(void *base, unsigned long start,
192 unsigned long end)
Ollie Lho184a4042005-11-26 21:55:36 +0000193{
194 unsigned long addr;
Uwe Hermannffec5f32007-08-23 16:08:21 +0000195
Ollie Lho184a4042005-11-26 21:55:36 +0000196 /* For now be stupid.... */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000197 for (addr = start; addr < end; addr += 16) {
198 struct lb_header *head =
199 (struct lb_header *)(((char *)base) + addr);
200 struct lb_record *recs =
201 (struct lb_record *)(((char *)base) + addr + sizeof(*head));
Edward O'Callaghan4a55e682019-11-26 23:28:05 +1100202 if (!lb_header_valid(head, addr))
Ollie Lho184a4042005-11-26 21:55:36 +0000203 continue;
Edward O'Callaghan1d80d642019-11-26 23:31:06 +1100204 if (!lb_table_valid(head, recs))
Ollie Lho184a4042005-11-26 21:55:36 +0000205 continue;
Sean Nelson316a29f2010-05-07 20:09:04 +0000206 msg_pdbg("Found coreboot table at 0x%08lx.\n", addr);
Ollie Lho184a4042005-11-26 21:55:36 +0000207 return head;
208
David Hendrickscdb290e2020-06-23 14:16:26 -0700209 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000210
Peter Huewe73f8ec82011-01-24 19:15:51 +0000211 return NULL;
Ollie Lho184a4042005-11-26 21:55:36 +0000212}
213
Aaron Durbin9eeae332017-09-27 01:02:08 -0600214static struct lb_header *find_lb_table_remap(unsigned long start_addr,
215 uint8_t **table_area)
216{
217 size_t offset;
218 unsigned long end;
219 size_t mapping_size;
220 void *base;
221
222 mapping_size = getpagesize();
223 offset = start_addr % getpagesize();
224 start_addr -= offset;
225
226 base = physmap_ro("high tables", start_addr, mapping_size);
227 if (ERROR_PTR == base) {
228 msg_perr("Failed getting access to coreboot high tables.\n");
229 return NULL;
230 }
231
232 for (end = getpagesize(); offset < end; offset += 16) {
233 struct lb_record *recs;
234 struct lb_header *head;
235
236 /* No more headers to check. */
237 if (end - offset < sizeof(*head))
238 return NULL;
239
240 head = (struct lb_header *)(((char *)base) + offset);
241
242 if (!lb_header_valid(head, offset))
243 continue;
244
245 if (mapping_size - offset < head->table_bytes + sizeof(*head)) {
246 size_t prev_mapping_size = mapping_size;
247 mapping_size = head->table_bytes + sizeof(*head);
248 mapping_size += offset;
249 mapping_size += getpagesize() - (mapping_size % getpagesize());
250 physunmap(base, prev_mapping_size);
251 base = physmap_ro("high tables", start_addr, mapping_size);
252 if (ERROR_PTR == base) {
253 msg_perr("Failed getting access to coreboot high tables.\n");
254 return NULL;
255 }
256 head = (struct lb_header *)(((char *)base) + offset);
257 }
258
259 recs = (struct lb_record *)(((char *)base) + offset + sizeof(*head));
260 if (!lb_table_valid(head, recs))
261 continue;
262 msg_pdbg("Found coreboot table at 0x%08zx.\n", offset);
263 *table_area = base;
264 return head;
265 }
266
267 physunmap(base, mapping_size);
268 return NULL;
269}
270
Ollie Lho184a4042005-11-26 21:55:36 +0000271static void find_mainboard(struct lb_record *ptr, unsigned long addr)
272{
273 struct lb_mainboard *rec;
274 int max_size;
275 char vendor[256], part[256];
Uwe Hermannffec5f32007-08-23 16:08:21 +0000276
Ollie Lho184a4042005-11-26 21:55:36 +0000277 rec = (struct lb_mainboard *)ptr;
278 max_size = rec->size - sizeof(*rec);
Sean Nelson316a29f2010-05-07 20:09:04 +0000279 msg_pdbg("Vendor ID: %.*s, part ID: %.*s\n",
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000280 max_size - rec->vendor_idx,
281 rec->strings + rec->vendor_idx,
282 max_size - rec->part_number_idx,
283 rec->strings + rec->part_number_idx);
284 snprintf(vendor, 255, "%.*s", max_size - rec->vendor_idx, rec->strings + rec->vendor_idx);
285 snprintf(part, 255, "%.*s", max_size - rec->part_number_idx, rec->strings + rec->part_number_idx);
Ollie Lho184a4042005-11-26 21:55:36 +0000286
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000287 cb_vendor = strdup(vendor);
288 cb_model = strdup(part);
Ollie Lho184a4042005-11-26 21:55:36 +0000289}
290
291static struct lb_record *next_record(struct lb_record *rec)
292{
293 return (struct lb_record *)(((char *)rec) + rec->size);
294}
295
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000296static void search_lb_records(struct lb_record *rec, struct lb_record *last, unsigned long addr)
Ollie Lho184a4042005-11-26 21:55:36 +0000297{
298 struct lb_record *next;
Ollie Lho184a4042005-11-26 21:55:36 +0000299
Uwe Hermanna7e05482007-05-09 10:17:44 +0000300 for (next = next_record(rec); (rec < last) && (next <= last);
301 rec = next, addr += rec->size) {
Ollie Lho184a4042005-11-26 21:55:36 +0000302 next = next_record(rec);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000303 if (rec->tag == LB_TAG_MAINBOARD) {
304 find_mainboard(rec, addr);
Ollie Lho184a4042005-11-26 21:55:36 +0000305 break;
306 }
307 }
308}
309
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000310#define BYTES_TO_MAP (1024*1024)
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000311/* returns 0 if the table was parsed successfully and cb_vendor/cb_model have been set. */
312int cb_parse_table(const char **vendor, const char **model)
Ollie Lho184a4042005-11-26 21:55:36 +0000313{
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000314 uint8_t *table_area;
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000315 unsigned long addr, start;
Ollie Lho184a4042005-11-26 21:55:36 +0000316 struct lb_header *lb_table;
317 struct lb_record *rec, *last;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000318
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +0000319#if defined(__MACH__) && defined(__APPLE__)
Carl-Daniel Hailfingerf992c192010-10-06 23:16:10 +0000320 /* This is a hack. DirectHW fails to map physical address 0x00000000.
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000321 * Why?
322 */
323 start = 0x400;
324#else
325 start = 0x0;
326#endif
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000327 table_area = physmap_ro_unaligned("low megabyte", start, BYTES_TO_MAP - start);
Patrick Georgied7a9642010-09-25 22:53:44 +0000328 if (ERROR_PTR == table_area) {
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000329 msg_perr("Failed getting access to coreboot low tables.\n");
330 return -1;
331 }
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000332
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000333 lb_table = find_lb_table(table_area, 0x00000, 0x1000);
Ollie Lho184a4042005-11-26 21:55:36 +0000334 if (!lb_table)
Carl-Daniel Hailfingerbaaffe02010-02-02 11:09:03 +0000335 lb_table = find_lb_table(table_area, 0xf0000 - start, BYTES_TO_MAP - start);
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000336 if (lb_table) {
337 struct lb_forward *forward = (struct lb_forward *)
338 (((char *)lb_table) + lb_table->header_bytes);
339 if (forward->tag == LB_TAG_FORWARD) {
340 start = forward->forward;
Carl-Daniel Hailfinger43eac032014-03-05 00:16:16 +0000341 physunmap_unaligned(table_area, BYTES_TO_MAP);
342 // FIXME: table_area is never unmapped below, nor is it unmapped above in the no-forward case
Aaron Durbin9eeae332017-09-27 01:02:08 -0600343 lb_table = find_lb_table_remap(start, &table_area);
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000344 }
345 }
346
Peter Stuge2dc3aaa2009-01-26 00:15:56 +0000347 if (!lb_table) {
Stefan Reinauer12a04eb2011-04-01 18:05:20 +0000348 msg_pdbg("No coreboot table found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000349 return -1;
350 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000351
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000352 addr = ((char *)lb_table) - ((char *)table_area) + start;
Elyes HAOUAS124ef382018-03-27 12:15:09 +0200353 msg_pinfo("coreboot table found at 0x%lx.\n",
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000354 (unsigned long)lb_table - (unsigned long)table_area + start);
Peter Stuge2dc3aaa2009-01-26 00:15:56 +0000355 rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes);
356 last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes);
Sean Nelson316a29f2010-05-07 20:09:04 +0000357 msg_pdbg("coreboot header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
Peter Stuge2dc3aaa2009-01-26 00:15:56 +0000358 lb_table->header_bytes, lb_table->header_checksum,
359 lb_table->table_bytes, lb_table->table_checksum,
360 lb_table->table_entries);
361 search_lb_records(rec, last, addr + lb_table->header_bytes);
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000362 *vendor = cb_vendor;
363 *model = cb_model;
Ollie Lho184a4042005-11-26 21:55:36 +0000364 return 0;
365}