blob: d50a0cfc5ad40c15dc5935d76966552985788813 [file] [log] [blame]
stepan8fa1ff32007-05-24 09:08:36 +00001/*
uweb25f1ea2007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
stepan8fa1ff32007-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)
stepan03879132009-03-17 14:39:25 +00007 * Copyright (C) 2006-2009 coresystems GmbH
stepan8fa1ff32007-05-24 09:08:36 +00008 * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
hailfinger336a92d2010-02-02 11:09:03 +00009 * Copyright (C) 2010 Carl-Daniel Hailfinger
stepan8fa1ff32007-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.
stepan8fa1ff32007-05-24 09:08:36 +000019 */
20
hailfingera83a5fe2010-05-30 22:24:40 +000021#include <unistd.h>
22#include <stdio.h>
Edward O'Callaghan0d105752020-09-18 12:15:41 +100023#include <ctype.h>
Edward O'Callaghanb4300ca2019-09-03 16:15:21 +100024#include <strings.h>
ollie6a600992005-11-26 21:55:36 +000025#include <string.h>
stepan5c3f1382007-02-06 19:47:50 +000026#include "flash.h"
hailfinger428f6852010-07-27 22:41:39 +000027#include "programmer.h"
stepan1b445c12008-01-18 16:17:44 +000028#include "coreboot_tables.h"
ollie6a600992005-11-26 21:55:36 +000029
Edward O'Callaghan481cce82019-05-31 15:03:50 +100030static char *cb_vendor = NULL, *cb_model = NULL;
ollie6a600992005-11-26 21:55:36 +000031
Edward O'Callaghan0d105752020-09-18 12:15:41 +100032/* Tries to find coreboot IDs in the supplied image and compares them to the current IDs.
33 * Returns...
34 * -1 if IDs in the image do not match the IDs embedded in the current firmware,
35 * 0 if the IDs could not be found in the image or if they match correctly.
36 */
37int cb_check_image(const uint8_t *image, unsigned int size)
38{
39 const unsigned int *walk;
40 unsigned int mb_part_offset, mb_vendor_offset;
41 const char *mb_part, *mb_vendor;
42
43 walk = (const unsigned int *)(image + size - 0x10);
44 walk--;
45
46 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
47 /* Some NVIDIA chipsets store chipset soft straps (IIRC Hypertransport init info etc.) in
48 * flash at exactly the location where coreboot image size, coreboot vendor name pointer and
49 * coreboot board name pointer are usually stored. In this case coreboot uses an alternate
50 * location for the coreboot image data. */
51 walk = (const unsigned int *)(image + size - 0x80);
52 walk--;
53 }
54
55 /*
56 * Check if coreboot last image size is 0 or not a multiple of 1k or
57 * bigger than the chip or if the pointers to vendor ID or mainboard ID
58 * are outside the image of if the start of ID strings are nonsensical
59 * (nonprintable and not \0).
60 */
61 mb_part_offset = *(walk - 1);
62 mb_vendor_offset = *(walk - 2);
63 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0 || (*walk) > size ||
64 mb_part_offset > size || mb_vendor_offset > size) {
65 msg_pdbg("Flash image seems to be a legacy BIOS. Disabling coreboot-related checks.\n");
66 return 0;
67 }
68
69 mb_part = (const char *)(image + size - mb_part_offset);
70 mb_vendor = (const char *)(image + size - mb_vendor_offset);
71 if (!isprint((unsigned char)*mb_part) ||
72 !isprint((unsigned char)*mb_vendor)) {
73 msg_pdbg("Flash image seems to have garbage in the ID location. "
74 "Disabling coreboot-related checks.\n");
75 return 0;
76 }
77
78 msg_pdbg("coreboot last image size (not ROM size) is %d bytes.\n", *walk);
79
80 msg_pdbg("Manufacturer: %s\n", mb_vendor);
81 msg_pdbg("Mainboard ID: %s\n", mb_part);
82
83 /* If these are not set, the coreboot table was not found. */
84 if (!cb_vendor || !cb_model)
85 return 0;
86
87 /* These comparisons are case insensitive to make things a little less user^Werror prone. */
88 if (!strcasecmp(mb_vendor, cb_vendor) && !strcasecmp(mb_part, cb_model)) {
89 msg_pdbg2("This coreboot image matches this mainboard.\n");
90 } else {
91 msg_perr("This coreboot image (%s:%s) does not appear to\n"
92 "be correct for the detected mainboard (%s:%s).\n",
93 mb_vendor, mb_part, cb_vendor, cb_model);
94 return -1;
95 }
96
97 return 0;
98}
99
ollie6a600992005-11-26 21:55:36 +0000100static unsigned long compute_checksum(void *addr, unsigned long length)
101{
102 uint8_t *ptr;
103 volatile union {
uwef6641642007-05-09 10:17:44 +0000104 uint8_t byte[2];
ollie6a600992005-11-26 21:55:36 +0000105 uint16_t word;
mkarcherb682ab92010-01-12 15:36:24 +0000106 } chksum;
uwe143ef302007-10-10 17:42:20 +0000107 unsigned long sum;
108 unsigned long i;
uwebe4477b2007-08-23 16:08:21 +0000109
ollie6a600992005-11-26 21:55:36 +0000110 /* In the most straight forward way possible,
111 * compute an ip style checksum.
112 */
113 sum = 0;
114 ptr = addr;
uwef6641642007-05-09 10:17:44 +0000115 for (i = 0; i < length; i++) {
ollie6a600992005-11-26 21:55:36 +0000116 unsigned long value;
117 value = ptr[i];
uwe143ef302007-10-10 17:42:20 +0000118 if (i & 1) {
ollie6a600992005-11-26 21:55:36 +0000119 value <<= 8;
uwe143ef302007-10-10 17:42:20 +0000120 }
ollie6a600992005-11-26 21:55:36 +0000121 /* Add the new value */
122 sum += value;
123 /* Wrap around the carry */
uwe143ef302007-10-10 17:42:20 +0000124 if (sum > 0xFFFF) {
ollie6a600992005-11-26 21:55:36 +0000125 sum = (sum + (sum >> 16)) & 0xFFFF;
uwe143ef302007-10-10 17:42:20 +0000126 }
ollie6a600992005-11-26 21:55:36 +0000127 }
mkarcherb682ab92010-01-12 15:36:24 +0000128 chksum.byte[0] = sum & 0xff;
129 chksum.byte[1] = (sum >> 8) & 0xff;
uwebe4477b2007-08-23 16:08:21 +0000130
mkarcherb682ab92010-01-12 15:36:24 +0000131 return (~chksum.word) & 0xFFFF;
ollie6a600992005-11-26 21:55:36 +0000132}
133
134#define for_each_lbrec(head, rec) \
135 for(rec = (struct lb_record *)(((char *)head) + sizeof(*head)); \
136 (((char *)rec) < (((char *)head) + sizeof(*head) + head->table_bytes)) && \
137 (rec->size >= 1) && \
138 ((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \
uwef6641642007-05-09 10:17:44 +0000139 rec = (struct lb_record *)(((char *)rec) + rec->size))
ollie6a600992005-11-26 21:55:36 +0000140
Edward O'Callaghanaba9d312019-10-29 14:27:57 +1100141static unsigned int count_lb_records(struct lb_header *head)
ollie6a600992005-11-26 21:55:36 +0000142{
143 struct lb_record *rec;
Edward O'Callaghanaba9d312019-10-29 14:27:57 +1100144 unsigned int count;
uwebe4477b2007-08-23 16:08:21 +0000145
ollie6a600992005-11-26 21:55:36 +0000146 count = 0;
147 for_each_lbrec(head, rec) {
148 count++;
149 }
uwebe4477b2007-08-23 16:08:21 +0000150
ollie6a600992005-11-26 21:55:36 +0000151 return count;
152}
153
Aaron Durbinb17e9e42017-09-27 01:02:08 -0600154static int lb_header_valid(struct lb_header *head, unsigned long addr)
155{
156 if (memcmp(head->signature, "LBIO", 4) != 0)
157 return 0;
158 msg_pdbg("Found candidate at: %08lx-%08lx\n",
159 addr, addr + sizeof(*head) + head->table_bytes);
160 if (head->header_bytes != sizeof(*head)) {
161 msg_perr("Header bytes of %d are incorrect.\n",
162 head->header_bytes);
163 return 0;
164 }
165 if (compute_checksum((uint8_t *) head, sizeof(*head)) != 0) {
166 msg_perr("Bad header checksum.\n");
167 return 0;
168 }
169
170 return 1;
171}
172
173static int lb_table_valid(struct lb_header *head, struct lb_record *recs)
174{
175 if (compute_checksum(recs, head->table_bytes)
176 != head->table_checksum) {
177 msg_perr("Bad table checksum: %04x.\n",
178 head->table_checksum);
179 return 0;
180 }
181 if (count_lb_records(head) != head->table_entries) {
182 msg_perr("Bad record count: %d.\n",
183 head->table_entries);
184 return 0;
185 }
186
187 return 1;
188}
189
uwef6641642007-05-09 10:17:44 +0000190static struct lb_header *find_lb_table(void *base, unsigned long start,
191 unsigned long end)
ollie6a600992005-11-26 21:55:36 +0000192{
193 unsigned long addr;
uwebe4477b2007-08-23 16:08:21 +0000194
ollie6a600992005-11-26 21:55:36 +0000195 /* For now be stupid.... */
uwef6641642007-05-09 10:17:44 +0000196 for (addr = start; addr < end; addr += 16) {
197 struct lb_header *head =
198 (struct lb_header *)(((char *)base) + addr);
199 struct lb_record *recs =
200 (struct lb_record *)(((char *)base) + addr + sizeof(*head));
Aaron Durbinb17e9e42017-09-27 01:02:08 -0600201 if (!lb_header_valid(head, addr))
ollie6a600992005-11-26 21:55:36 +0000202 continue;
Aaron Durbinb17e9e42017-09-27 01:02:08 -0600203 if (!lb_table_valid(head, recs))
ollie6a600992005-11-26 21:55:36 +0000204 continue;
snelsone42c3802010-05-07 20:09:04 +0000205 msg_pdbg("Found coreboot table at 0x%08lx.\n", addr);
ollie6a600992005-11-26 21:55:36 +0000206 return head;
207
Edward O'Callaghan0f0673d2020-09-18 12:54:12 +1000208 }
uwebe4477b2007-08-23 16:08:21 +0000209
stepand0d220f2011-01-24 19:15:51 +0000210 return NULL;
ollie6a600992005-11-26 21:55:36 +0000211}
212
Aaron Durbinb17e9e42017-09-27 01:02:08 -0600213static struct lb_header *find_lb_table_remap(unsigned long start_addr,
214 uint8_t **table_area)
215{
216 size_t offset;
Edward O'Callaghana708d632019-11-26 23:48:51 +1100217 unsigned long end;
Aaron Durbinb17e9e42017-09-27 01:02:08 -0600218 size_t mapping_size;
219 void *base;
220
221 mapping_size = getpagesize();
222 offset = start_addr % getpagesize();
223 start_addr -= offset;
224
Edward O'Callaghan64a4db22019-05-30 03:13:07 -0400225 base = physmap_ro("high tables", start_addr, mapping_size);
Aaron Durbinb17e9e42017-09-27 01:02:08 -0600226 if (ERROR_PTR == base) {
227 msg_perr("Failed getting access to coreboot high tables.\n");
228 return NULL;
229 }
230
Edward O'Callaghana708d632019-11-26 23:48:51 +1100231 for (end = getpagesize(); offset < end; offset += 16) {
Aaron Durbinb17e9e42017-09-27 01:02:08 -0600232 struct lb_record *recs;
233 struct lb_header *head;
234
235 /* No more headers to check. */
Edward O'Callaghana708d632019-11-26 23:48:51 +1100236 if (end - offset < sizeof(*head))
Aaron Durbinb17e9e42017-09-27 01:02:08 -0600237 return NULL;
238
Edward O'Callaghana708d632019-11-26 23:48:51 +1100239 head = (struct lb_header *)(((char *)base) + offset);
Aaron Durbinb17e9e42017-09-27 01:02:08 -0600240
Edward O'Callaghana708d632019-11-26 23:48:51 +1100241 if (!lb_header_valid(head, offset))
Aaron Durbinb17e9e42017-09-27 01:02:08 -0600242 continue;
243
Edward O'Callaghana708d632019-11-26 23:48:51 +1100244 if (mapping_size - offset < head->table_bytes + sizeof(*head)) {
Aaron Durbin312b3182017-10-11 16:32:20 -0600245 size_t prev_mapping_size = mapping_size;
Aaron Durbinb17e9e42017-09-27 01:02:08 -0600246 mapping_size = head->table_bytes + sizeof(*head);
Edward O'Callaghana708d632019-11-26 23:48:51 +1100247 mapping_size += offset;
248 mapping_size += getpagesize() - (mapping_size % getpagesize());
Aaron Durbin312b3182017-10-11 16:32:20 -0600249 physunmap(base, prev_mapping_size);
Edward O'Callaghana708d632019-11-26 23:48:51 +1100250 base = physmap_ro("high tables", start_addr, mapping_size);
251 if (ERROR_PTR == base)
Aaron Durbinb17e9e42017-09-27 01:02:08 -0600252 msg_perr("Failed getting access to coreboot high tables.\n");
Edward O'Callaghana708d632019-11-26 23:48:51 +1100253 else
254 head = (struct lb_header *)(((char *)base) + offset);
Aaron Durbinb17e9e42017-09-27 01:02:08 -0600255 }
256
Edward O'Callaghana708d632019-11-26 23:48:51 +1100257 recs = (struct lb_record *)(((char *)base) + offset + sizeof(*head));
Aaron Durbinb17e9e42017-09-27 01:02:08 -0600258 if (!lb_table_valid(head, recs))
259 continue;
Angel Pons12247992021-03-13 21:42:40 +0100260 msg_pdbg("Found coreboot table at 0x%08zx.\n", offset);
Aaron Durbinb17e9e42017-09-27 01:02:08 -0600261 *table_area = base;
262 return head;
263 }
264
265 physunmap(base, mapping_size);
266 return NULL;
267}
268
ollie6a600992005-11-26 21:55:36 +0000269static void find_mainboard(struct lb_record *ptr, unsigned long addr)
270{
271 struct lb_mainboard *rec;
272 int max_size;
273 char vendor[256], part[256];
uwebe4477b2007-08-23 16:08:21 +0000274
ollie6a600992005-11-26 21:55:36 +0000275 rec = (struct lb_mainboard *)ptr;
276 max_size = rec->size - sizeof(*rec);
snelsone42c3802010-05-07 20:09:04 +0000277 msg_pdbg("Vendor ID: %.*s, part ID: %.*s\n",
Patrick Georgi82c9a0f2017-02-03 16:49:09 +0100278 max_size - rec->vendor_idx,
279 rec->strings + rec->vendor_idx,
280 max_size - rec->part_number_idx,
281 rec->strings + rec->part_number_idx);
282 snprintf(vendor, 255, "%.*s", max_size - rec->vendor_idx, rec->strings + rec->vendor_idx);
283 snprintf(part, 255, "%.*s", max_size - rec->part_number_idx, rec->strings + rec->part_number_idx);
ollie6a600992005-11-26 21:55:36 +0000284
Edward O'Callaghanf85623c2020-10-09 23:24:19 +1100285 cb_vendor = strdup(vendor);
286 cb_model = strdup(part);
ollie6a600992005-11-26 21:55:36 +0000287}
288
289static struct lb_record *next_record(struct lb_record *rec)
290{
291 return (struct lb_record *)(((char *)rec) + rec->size);
292}
293
Patrick Georgi82c9a0f2017-02-03 16:49:09 +0100294static void search_lb_records(struct lb_record *rec, struct lb_record *last, unsigned long addr)
ollie6a600992005-11-26 21:55:36 +0000295{
296 struct lb_record *next;
297 int count;
298 count = 0;
299
uwef6641642007-05-09 10:17:44 +0000300 for (next = next_record(rec); (rec < last) && (next <= last);
301 rec = next, addr += rec->size) {
ollie6a600992005-11-26 21:55:36 +0000302 next = next_record(rec);
303 count++;
uwef6641642007-05-09 10:17:44 +0000304 if (rec->tag == LB_TAG_MAINBOARD) {
305 find_mainboard(rec, addr);
ollie6a600992005-11-26 21:55:36 +0000306 break;
307 }
308 }
309}
310
stepan03879132009-03-17 14:39:25 +0000311#define BYTES_TO_MAP (1024*1024)
Edward O'Callaghan481cce82019-05-31 15:03:50 +1000312/* returns 0 if the table was parsed successfully and cb_vendor/cb_model have been set. */
313int cb_parse_table(const char **vendor, const char **model)
ollie6a600992005-11-26 21:55:36 +0000314{
stepan03879132009-03-17 14:39:25 +0000315 uint8_t *table_area;
stuge96960832009-01-26 01:23:31 +0000316 unsigned long addr, start;
ollie6a600992005-11-26 21:55:36 +0000317 struct lb_header *lb_table;
318 struct lb_record *rec, *last;
uwef6641642007-05-09 10:17:44 +0000319
Patrick Georgi0c6d1782017-02-03 16:49:50 +0100320#if defined(__MACH__) && defined(__APPLE__)
hailfinger906ea8b2010-10-06 23:16:10 +0000321 /* This is a hack. DirectHW fails to map physical address 0x00000000.
stuge96960832009-01-26 01:23:31 +0000322 * Why?
323 */
324 start = 0x400;
325#else
326 start = 0x0;
327#endif
Edward O'Callaghan7d799182019-10-29 14:04:28 +1100328 table_area = physmap_ro_unaligned("low megabyte", start, BYTES_TO_MAP - start);
hailfingerf294fa22010-09-25 22:53:44 +0000329 if (ERROR_PTR == table_area) {
hailfinger336a92d2010-02-02 11:09:03 +0000330 msg_perr("Failed getting access to coreboot low tables.\n");
331 return -1;
332 }
stuge96960832009-01-26 01:23:31 +0000333
stepan03879132009-03-17 14:39:25 +0000334 lb_table = find_lb_table(table_area, 0x00000, 0x1000);
ollie6a600992005-11-26 21:55:36 +0000335 if (!lb_table)
hailfinger336a92d2010-02-02 11:09:03 +0000336 lb_table = find_lb_table(table_area, 0xf0000 - start, BYTES_TO_MAP - start);
stepan03879132009-03-17 14:39:25 +0000337 if (lb_table) {
338 struct lb_forward *forward = (struct lb_forward *)
339 (((char *)lb_table) + lb_table->header_bytes);
340 if (forward->tag == LB_TAG_FORWARD) {
341 start = forward->forward;
Edward O'Callaghan7d799182019-10-29 14:04:28 +1100342 physunmap_unaligned(table_area, BYTES_TO_MAP);
Aaron Durbinb17e9e42017-09-27 01:02:08 -0600343 lb_table = find_lb_table_remap(start, &table_area);
stepan03879132009-03-17 14:39:25 +0000344 }
345 }
346
stuge7a4a19d2009-01-26 00:15:56 +0000347 if (!lb_table) {
stepane6c891b2011-04-01 18:05:20 +0000348 msg_pdbg("No coreboot table found.\n");
ollie6a600992005-11-26 21:55:36 +0000349 return -1;
350 }
uwebe4477b2007-08-23 16:08:21 +0000351
stepan03879132009-03-17 14:39:25 +0000352 addr = ((char *)lb_table) - ((char *)table_area) + start;
Edward O'Callaghan7d799182019-10-29 14:04:28 +1100353 msg_pinfo("coreboot table found at 0x%lx.\n",
stepan03879132009-03-17 14:39:25 +0000354 (unsigned long)lb_table - (unsigned long)table_area + start);
stuge7a4a19d2009-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);
snelsone42c3802010-05-07 20:09:04 +0000357 msg_pdbg("coreboot header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
stuge7a4a19d2009-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);
Edward O'Callaghan481cce82019-05-31 15:03:50 +1000362 *vendor = cb_vendor;
363 *model = cb_model;
ollie6a600992005-11-26 21:55:36 +0000364 return 0;
365}