blob: 0512638e64e7f440b64647a4e3e6196b25f5cb44 [file] [log] [blame]
Uwe Hermann75f51072008-03-04 16:29:54 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2005-2008 coresystems GmbH
5 * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000021#include <stdio.h>
Ollie Lho184a4042005-11-26 21:55:36 +000022#include <stdlib.h>
23#include <string.h>
Carl-Daniel Hailfingerecab4fc2008-07-03 14:40:06 +000024#include <ctype.h>
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +000025#include <limits.h>
Uwe Hermann0846f892007-08-23 13:34:59 +000026#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000027#include "programmer.h"
Ollie Lho184a4042005-11-26 21:55:36 +000028
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000029#if CONFIG_INTERNAL == 1
Uwe Hermanna7e05482007-05-09 10:17:44 +000030char *mainboard_vendor = NULL;
31char *mainboard_part = NULL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000032#endif
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000033static int romimages = 0;
Ollie Lho184a4042005-11-26 21:55:36 +000034
David Hendricks444cefc2010-10-29 20:17:41 +000035#define MAX_ROMLAYOUT 32
Ollie Lho184a4042005-11-26 21:55:36 +000036
37typedef struct {
38 unsigned int start;
39 unsigned int end;
40 unsigned int included;
41 char name[256];
42} romlayout_t;
43
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +000044/* include_args lists arguments specified at the command line with -i. They
45 * must be processed at some point so that desired regions are marked as
46 * "included" in the rom_entries list.
47 */
48static char *include_args[MAX_ROMLAYOUT];
49static int num_include_args = 0; /* the number of valid entries. */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000050static romlayout_t rom_entries[MAX_ROMLAYOUT];
Ollie Lho184a4042005-11-26 21:55:36 +000051
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000052#if CONFIG_INTERNAL == 1 /* FIXME: Move the whole block to cbtable.c? */
Ollie Lho184a4042005-11-26 21:55:36 +000053static char *def_name = "DEFAULT";
54
Peter Stuge7ffbc6f2008-06-18 02:08:40 +000055int show_id(uint8_t *bios, int size, int force)
Ollie Lho184a4042005-11-26 21:55:36 +000056{
57 unsigned int *walk;
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000058 unsigned int mb_part_offset, mb_vendor_offset;
59 char *mb_part, *mb_vendor;
60
61 mainboard_vendor = def_name;
62 mainboard_part = def_name;
Ollie Lho184a4042005-11-26 21:55:36 +000063
Uwe Hermanna7e05482007-05-09 10:17:44 +000064 walk = (unsigned int *)(bios + size - 0x10);
65 walk--;
Ollie Lho184a4042005-11-26 21:55:36 +000066
Uwe Hermanna7e05482007-05-09 10:17:44 +000067 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
Uwe Hermann6c8866c2008-07-03 19:26:44 +000068 /* We might have an NVIDIA chipset BIOS which stores the ID
69 * information at a different location.
Ollie Lho184a4042005-11-26 21:55:36 +000070 */
Uwe Hermanna7e05482007-05-09 10:17:44 +000071 walk = (unsigned int *)(bios + size - 0x80);
72 walk--;
Ollie Lho184a4042005-11-26 21:55:36 +000073 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000074
Carl-Daniel Hailfingerecab4fc2008-07-03 14:40:06 +000075 /*
76 * Check if coreboot last image size is 0 or not a multiple of 1k or
77 * bigger than the chip or if the pointers to vendor ID or mainboard ID
78 * are outside the image of if the start of ID strings are nonsensical
79 * (nonprintable and not \0).
80 */
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000081 mb_part_offset = *(walk - 1);
82 mb_vendor_offset = *(walk - 2);
83 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0 || (*walk) > size ||
84 mb_part_offset > size || mb_vendor_offset > size) {
Stefan Taunerfaf01fb2011-05-18 01:31:53 +000085 msg_pinfo("Flash image seems to be a legacy BIOS. "
86 "Disabling coreboot-related checks.\n");
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000087 return 0;
88 }
Uwe Hermann394131e2008-10-18 21:14:13 +000089
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000090 mb_part = (char *)(bios + size - mb_part_offset);
91 mb_vendor = (char *)(bios + size - mb_vendor_offset);
92 if (!isprint((unsigned char)*mb_part) ||
93 !isprint((unsigned char)*mb_vendor)) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000094 msg_pinfo("Flash image seems to have garbage in the ID location."
Uwe Hermann394131e2008-10-18 21:14:13 +000095 " Disabling checks.\n");
Ollie Lho184a4042005-11-26 21:55:36 +000096 return 0;
97 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000098
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000099 msg_pdbg("coreboot last image size "
Uwe Hermanna502dce2007-10-17 23:55:15 +0000100 "(not ROM size) is %d bytes.\n", *walk);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000101
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +0000102 mainboard_part = strdup(mb_part);
103 mainboard_vendor = strdup(mb_vendor);
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000104 msg_pdbg("Manufacturer: %s\n", mainboard_vendor);
105 msg_pdbg("Mainboard ID: %s\n", mainboard_part);
Stefan Reinauer3a431602005-12-18 18:40:46 +0000106
107 /*
Stefan Reinauere3f3e2e2008-01-18 15:33:10 +0000108 * If lb_vendor is not set, the coreboot table was
Carl-Daniel Hailfinger2d927fb2012-01-04 00:48:27 +0000109 * not found. Nor was -p internal:mainboard=VENDOR:PART specified.
Stefan Reinauer3a431602005-12-18 18:40:46 +0000110 */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000111 if (!lb_vendor || !lb_part) {
Carl-Daniel Hailfinger2d927fb2012-01-04 00:48:27 +0000112 msg_pinfo("Note: If the following flash access fails, try "
113 "-p internal:mainboard=<vendor>:<mainboard>.\n");
Stefan Reinauer3a431602005-12-18 18:40:46 +0000114 return 0;
115 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000116
Stefan Reinauere3705282005-12-18 16:41:10 +0000117 /* These comparisons are case insensitive to make things
118 * a little less user^Werror prone.
119 */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000120 if (!strcasecmp(mainboard_vendor, lb_vendor) &&
121 !strcasecmp(mainboard_part, lb_part)) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000122 msg_pdbg("This firmware image matches this mainboard.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000123 } else {
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000124 if (force_boardmismatch) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000125 msg_pinfo("WARNING: This firmware image does not "
Uwe Hermannac309342007-10-10 17:42:20 +0000126 "seem to fit to this machine - forcing it.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000127 } else {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000128 msg_pinfo("ERROR: Your firmware image (%s:%s) does not "
Carl-Daniel Hailfinger2d927fb2012-01-04 00:48:27 +0000129 "appear to\n"
130 " be correct for the detected "
131 "mainboard (%s:%s)\n\n"
132 "Override with -p internal:boardmismatch="
133 "force to ignore the board name in the\n"
134 "firmware image or override the detected "
135 "mainboard with\n"
136 "-p internal:mainboard=<vendor>:<mainboard>."
137 "\n\n",
138 mainboard_vendor, mainboard_part, lb_vendor,
139 lb_part);
Niklas Söderlund9e423762012-06-16 00:11:16 +0000140 return 1;
Ollie Lho184a4042005-11-26 21:55:36 +0000141 }
142 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000143
Ollie Lho184a4042005-11-26 21:55:36 +0000144 return 0;
145}
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000146#endif
Ollie Lho184a4042005-11-26 21:55:36 +0000147
Patrick Georgia9095a92010-09-30 17:03:32 +0000148#ifndef __LIBPAYLOAD__
Uwe Hermanna7e05482007-05-09 10:17:44 +0000149int read_romlayout(char *name)
Ollie Lho184a4042005-11-26 21:55:36 +0000150{
151 FILE *romlayout;
152 char tempstr[256];
153 int i;
154
Uwe Hermanna7e05482007-05-09 10:17:44 +0000155 romlayout = fopen(name, "r");
156
157 if (!romlayout) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000158 msg_gerr("ERROR: Could not open ROM layout (%s).\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000159 name);
Ollie Lho184a4042005-11-26 21:55:36 +0000160 return -1;
161 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000162
163 while (!feof(romlayout)) {
Ollie Lho184a4042005-11-26 21:55:36 +0000164 char *tstr1, *tstr2;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +0000165
166 if (romimages >= MAX_ROMLAYOUT) {
167 msg_gerr("Maximum number of ROM images (%i) in layout "
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000168 "file reached.\n", MAX_ROMLAYOUT);
169 return 1;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +0000170 }
Peter Stuge1fec0f32009-01-12 21:00:35 +0000171 if (2 != fscanf(romlayout, "%s %s\n", tempstr, rom_entries[romimages].name))
172 continue;
Ollie Lho184a4042005-11-26 21:55:36 +0000173#if 0
174 // fscanf does not like arbitrary comments like that :( later
Uwe Hermanna7e05482007-05-09 10:17:44 +0000175 if (tempstr[0] == '#') {
Ollie Lho184a4042005-11-26 21:55:36 +0000176 continue;
177 }
178#endif
Uwe Hermanna7e05482007-05-09 10:17:44 +0000179 tstr1 = strtok(tempstr, ":");
180 tstr2 = strtok(NULL, ":");
Uwe Hermann58783e32008-12-22 16:42:59 +0000181 if (!tstr1 || !tstr2) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000182 msg_gerr("Error parsing layout file.\n");
Uwe Hermann58783e32008-12-22 16:42:59 +0000183 fclose(romlayout);
184 return 1;
185 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000186 rom_entries[romimages].start = strtol(tstr1, (char **)NULL, 16);
187 rom_entries[romimages].end = strtol(tstr2, (char **)NULL, 16);
188 rom_entries[romimages].included = 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000189 romimages++;
190 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000191
192 for (i = 0; i < romimages; i++) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000193 msg_gdbg("romlayout %08x - %08x named %s\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000194 rom_entries[i].start,
195 rom_entries[i].end, rom_entries[i].name);
Ollie Lho184a4042005-11-26 21:55:36 +0000196 }
197
198 fclose(romlayout);
Uwe Hermannffec5f32007-08-23 16:08:21 +0000199
Uwe Hermanna7e05482007-05-09 10:17:44 +0000200 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000201}
Patrick Georgia9095a92010-09-30 17:03:32 +0000202#endif
Ollie Lho184a4042005-11-26 21:55:36 +0000203
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000204/* returns the index of the entry (or a negative value if it is not found) */
205int find_include_arg(const char *const name)
206{
207 unsigned int i;
208 for (i = 0; i < num_include_args; i++) {
209 if (!strcmp(include_args[i], name))
210 return i;
211 }
212 return -1;
213}
214
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000215/* register an include argument (-i) for later processing */
216int register_include_arg(char *name)
217{
218 if (num_include_args >= MAX_ROMLAYOUT) {
219 msg_gerr("Too many regions included (%i).\n", num_include_args);
220 return 1;
221 }
222
223 if (name == NULL) {
224 msg_gerr("<NULL> is a bad region name.\n");
225 return 1;
226 }
227
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000228 if (find_include_arg(name) != -1) {
229 msg_gerr("Duplicate region name: \"%s\".\n", name);
230 return 1;
231 }
232
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000233 include_args[num_include_args] = name;
234 num_include_args++;
235 return 0;
236}
237
238/* returns the index of the entry (or a negative value if it is not found) */
239static int find_romentry(char *name)
Ollie Lho184a4042005-11-26 21:55:36 +0000240{
241 int i;
242
Uwe Hermanna7e05482007-05-09 10:17:44 +0000243 if (!romimages)
244 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000245
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000246 msg_gspew("Looking for region \"%s\"... ", name);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000247 for (i = 0; i < romimages; i++) {
248 if (!strcmp(rom_entries[i].name, name)) {
249 rom_entries[i].included = 1;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000250 msg_gspew("found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000251 return i;
252 }
253 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000254 msg_gspew("not found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000255 return -1;
256}
257
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000258/* process -i arguments
259 * returns 0 to indicate success, >0 to indicate failure
260 */
261int process_include_args(void)
262{
263 int i;
264 unsigned int found = 0;
265
266 if (num_include_args == 0)
267 return 0;
268
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000269 /* User has specified an area, but no layout file is loaded. */
270 if (!romimages) {
271 msg_gerr("Region requested (with -i \"%s\"), "
272 "but no layout data is available.\n",
273 include_args[0]);
274 return 1;
275 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000276
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000277 for (i = 0; i < num_include_args; i++) {
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000278 if (find_romentry(include_args[i]) < 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000279 msg_gerr("Invalid region specified: \"%s\".\n",
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000280 include_args[i]);
281 return 1;
282 }
283 found++;
284 }
285
286 msg_ginfo("Using region%s: \"%s\"", num_include_args > 1 ? "s" : "",
287 include_args[0]);
288 for (i = 1; i < num_include_args; i++)
289 msg_ginfo(", \"%s\"", include_args[i]);
290 msg_ginfo(".\n");
291 return 0;
292}
293
Stefan Tauner104b0d92011-12-25 09:07:59 +0000294romlayout_t *get_next_included_romentry(unsigned int start)
Ollie Lho184a4042005-11-26 21:55:36 +0000295{
296 int i;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000297 unsigned int best_start = UINT_MAX;
Stefan Tauner104b0d92011-12-25 09:07:59 +0000298 romlayout_t *best_entry = NULL;
299 romlayout_t *cur;
Ollie Lho184a4042005-11-26 21:55:36 +0000300
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000301 /* First come, first serve for overlapping regions. */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000302 for (i = 0; i < romimages; i++) {
Stefan Tauner104b0d92011-12-25 09:07:59 +0000303 cur = &rom_entries[i];
304 if (!cur->included)
Ollie Lho184a4042005-11-26 21:55:36 +0000305 continue;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000306 /* Already past the current entry? */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000307 if (start > cur->end)
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000308 continue;
309 /* Inside the current entry? */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000310 if (start >= cur->start)
311 return cur;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000312 /* Entry begins after start. */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000313 if (best_start > cur->start) {
314 best_start = cur->start;
315 best_entry = cur;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000316 }
Ollie Lho184a4042005-11-26 21:55:36 +0000317 }
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000318 return best_entry;
319}
Ollie Lho184a4042005-11-26 21:55:36 +0000320
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000321int handle_romentries(struct flashctx *flash, uint8_t *oldcontents, uint8_t *newcontents)
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000322{
323 unsigned int start = 0;
Stefan Tauner104b0d92011-12-25 09:07:59 +0000324 romlayout_t *entry;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000325 unsigned int size = flash->total_size * 1024;
326
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000327 /* If no regions were specified for inclusion, assume
328 * that the user wants to write the complete new image.
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000329 */
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000330 if (num_include_args == 0)
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000331 return 0;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000332
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000333 /* Non-included romentries are ignored.
334 * The union of all included romentries is used from the new image.
335 */
336 while (start < size) {
Stefan Tauner104b0d92011-12-25 09:07:59 +0000337 entry = get_next_included_romentry(start);
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000338 /* No more romentries for remaining region? */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000339 if (!entry) {
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000340 memcpy(newcontents + start, oldcontents + start,
341 size - start);
342 break;
343 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000344 /* For non-included region, copy from old content. */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000345 if (entry->start > start)
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000346 memcpy(newcontents + start, oldcontents + start,
Stefan Tauner104b0d92011-12-25 09:07:59 +0000347 entry->start - start);
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000348 /* Skip to location after current romentry. */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000349 start = entry->end + 1;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000350 /* Catch overflow. */
351 if (!start)
352 break;
353 }
Ollie Lho184a4042005-11-26 21:55:36 +0000354 return 0;
355}