blob: d719a051bb3919b51e1f21f80f4228e032703070 [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
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000044static romlayout_t rom_entries[MAX_ROMLAYOUT];
Ollie Lho184a4042005-11-26 21:55:36 +000045
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000046#if CONFIG_INTERNAL == 1 /* FIXME: Move the whole block to cbtable.c? */
Ollie Lho184a4042005-11-26 21:55:36 +000047static char *def_name = "DEFAULT";
48
Peter Stuge7ffbc6f2008-06-18 02:08:40 +000049int show_id(uint8_t *bios, int size, int force)
Ollie Lho184a4042005-11-26 21:55:36 +000050{
51 unsigned int *walk;
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000052 unsigned int mb_part_offset, mb_vendor_offset;
53 char *mb_part, *mb_vendor;
54
55 mainboard_vendor = def_name;
56 mainboard_part = def_name;
Ollie Lho184a4042005-11-26 21:55:36 +000057
Uwe Hermanna7e05482007-05-09 10:17:44 +000058 walk = (unsigned int *)(bios + size - 0x10);
59 walk--;
Ollie Lho184a4042005-11-26 21:55:36 +000060
Uwe Hermanna7e05482007-05-09 10:17:44 +000061 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
Uwe Hermann6c8866c2008-07-03 19:26:44 +000062 /* We might have an NVIDIA chipset BIOS which stores the ID
63 * information at a different location.
Ollie Lho184a4042005-11-26 21:55:36 +000064 */
Uwe Hermanna7e05482007-05-09 10:17:44 +000065 walk = (unsigned int *)(bios + size - 0x80);
66 walk--;
Ollie Lho184a4042005-11-26 21:55:36 +000067 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000068
Carl-Daniel Hailfingerecab4fc2008-07-03 14:40:06 +000069 /*
70 * Check if coreboot last image size is 0 or not a multiple of 1k or
71 * bigger than the chip or if the pointers to vendor ID or mainboard ID
72 * are outside the image of if the start of ID strings are nonsensical
73 * (nonprintable and not \0).
74 */
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000075 mb_part_offset = *(walk - 1);
76 mb_vendor_offset = *(walk - 2);
77 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0 || (*walk) > size ||
78 mb_part_offset > size || mb_vendor_offset > size) {
Stefan Taunerfaf01fb2011-05-18 01:31:53 +000079 msg_pinfo("Flash image seems to be a legacy BIOS. "
80 "Disabling coreboot-related checks.\n");
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000081 return 0;
82 }
Uwe Hermann394131e2008-10-18 21:14:13 +000083
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000084 mb_part = (char *)(bios + size - mb_part_offset);
85 mb_vendor = (char *)(bios + size - mb_vendor_offset);
86 if (!isprint((unsigned char)*mb_part) ||
87 !isprint((unsigned char)*mb_vendor)) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000088 msg_pinfo("Flash image seems to have garbage in the ID location."
Uwe Hermann394131e2008-10-18 21:14:13 +000089 " Disabling checks.\n");
Ollie Lho184a4042005-11-26 21:55:36 +000090 return 0;
91 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000092
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000093 msg_pdbg("coreboot last image size "
Uwe Hermanna502dce2007-10-17 23:55:15 +000094 "(not ROM size) is %d bytes.\n", *walk);
Uwe Hermanna7e05482007-05-09 10:17:44 +000095
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000096 mainboard_part = strdup(mb_part);
97 mainboard_vendor = strdup(mb_vendor);
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000098 msg_pdbg("Manufacturer: %s\n", mainboard_vendor);
99 msg_pdbg("Mainboard ID: %s\n", mainboard_part);
Stefan Reinauer3a431602005-12-18 18:40:46 +0000100
101 /*
Stefan Reinauere3f3e2e2008-01-18 15:33:10 +0000102 * If lb_vendor is not set, the coreboot table was
Uwe Hermann6c8866c2008-07-03 19:26:44 +0000103 * not found. Nor was -m VENDOR:PART specified.
Stefan Reinauer3a431602005-12-18 18:40:46 +0000104 */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000105 if (!lb_vendor || !lb_part) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000106 msg_pinfo("Note: If the following flash access fails, "
Uwe Hermann6c8866c2008-07-03 19:26:44 +0000107 "try -m <vendor>:<mainboard>.\n");
Stefan Reinauer3a431602005-12-18 18:40:46 +0000108 return 0;
109 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000110
Stefan Reinauere3705282005-12-18 16:41:10 +0000111 /* These comparisons are case insensitive to make things
112 * a little less user^Werror prone.
113 */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000114 if (!strcasecmp(mainboard_vendor, lb_vendor) &&
115 !strcasecmp(mainboard_part, lb_part)) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000116 msg_pdbg("This firmware image matches this mainboard.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000117 } else {
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000118 if (force_boardmismatch) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000119 msg_pinfo("WARNING: This firmware image does not "
Uwe Hermannac309342007-10-10 17:42:20 +0000120 "seem to fit to this machine - forcing it.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000121 } else {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000122 msg_pinfo("ERROR: Your firmware image (%s:%s) does not "
Uwe Hermanna7e05482007-05-09 10:17:44 +0000123 "appear to\n be correct for the detected "
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000124 "mainboard (%s:%s)\n\nOverride with -p internal:"
125 "boardmismatch=force if you are absolutely sure "
126 "that\nyou are using a correct "
Uwe Hermanna7e05482007-05-09 10:17:44 +0000127 "image for this mainboard or override\nthe detected "
Uwe Hermannac309342007-10-10 17:42:20 +0000128 "values with --mainboard <vendor>:<mainboard>.\n\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000129 mainboard_vendor, mainboard_part, lb_vendor,
130 lb_part);
Ollie Lho184a4042005-11-26 21:55:36 +0000131 exit(1);
132 }
133 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000134
Ollie Lho184a4042005-11-26 21:55:36 +0000135 return 0;
136}
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000137#endif
Ollie Lho184a4042005-11-26 21:55:36 +0000138
Patrick Georgia9095a92010-09-30 17:03:32 +0000139#ifndef __LIBPAYLOAD__
Uwe Hermanna7e05482007-05-09 10:17:44 +0000140int read_romlayout(char *name)
Ollie Lho184a4042005-11-26 21:55:36 +0000141{
142 FILE *romlayout;
143 char tempstr[256];
144 int i;
145
Uwe Hermanna7e05482007-05-09 10:17:44 +0000146 romlayout = fopen(name, "r");
147
148 if (!romlayout) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000149 msg_gerr("ERROR: Could not open ROM layout (%s).\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000150 name);
Ollie Lho184a4042005-11-26 21:55:36 +0000151 return -1;
152 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000153
154 while (!feof(romlayout)) {
Ollie Lho184a4042005-11-26 21:55:36 +0000155 char *tstr1, *tstr2;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +0000156
157 if (romimages >= MAX_ROMLAYOUT) {
158 msg_gerr("Maximum number of ROM images (%i) in layout "
159 "file reached before end of layout file.\n",
160 MAX_ROMLAYOUT);
161 msg_gerr("Ignoring the rest of the layout file.\n");
162 break;
163 }
Peter Stuge1fec0f32009-01-12 21:00:35 +0000164 if (2 != fscanf(romlayout, "%s %s\n", tempstr, rom_entries[romimages].name))
165 continue;
Ollie Lho184a4042005-11-26 21:55:36 +0000166#if 0
167 // fscanf does not like arbitrary comments like that :( later
Uwe Hermanna7e05482007-05-09 10:17:44 +0000168 if (tempstr[0] == '#') {
Ollie Lho184a4042005-11-26 21:55:36 +0000169 continue;
170 }
171#endif
Uwe Hermanna7e05482007-05-09 10:17:44 +0000172 tstr1 = strtok(tempstr, ":");
173 tstr2 = strtok(NULL, ":");
Uwe Hermann58783e32008-12-22 16:42:59 +0000174 if (!tstr1 || !tstr2) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000175 msg_gerr("Error parsing layout file.\n");
Uwe Hermann58783e32008-12-22 16:42:59 +0000176 fclose(romlayout);
177 return 1;
178 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000179 rom_entries[romimages].start = strtol(tstr1, (char **)NULL, 16);
180 rom_entries[romimages].end = strtol(tstr2, (char **)NULL, 16);
181 rom_entries[romimages].included = 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000182 romimages++;
183 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000184
185 for (i = 0; i < romimages; i++) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000186 msg_gdbg("romlayout %08x - %08x named %s\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000187 rom_entries[i].start,
188 rom_entries[i].end, rom_entries[i].name);
Ollie Lho184a4042005-11-26 21:55:36 +0000189 }
190
191 fclose(romlayout);
Uwe Hermannffec5f32007-08-23 16:08:21 +0000192
Uwe Hermanna7e05482007-05-09 10:17:44 +0000193 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000194}
Patrick Georgia9095a92010-09-30 17:03:32 +0000195#endif
Ollie Lho184a4042005-11-26 21:55:36 +0000196
197int find_romentry(char *name)
198{
199 int i;
200
Uwe Hermanna7e05482007-05-09 10:17:44 +0000201 if (!romimages)
202 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000203
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000204 msg_ginfo("Looking for \"%s\"... ", name);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000205
206 for (i = 0; i < romimages; i++) {
207 if (!strcmp(rom_entries[i].name, name)) {
208 rom_entries[i].included = 1;
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000209 msg_ginfo("found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000210 return i;
211 }
212 }
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000213 msg_ginfo("not found.\n"); // Not found. Error.
Uwe Hermannffec5f32007-08-23 16:08:21 +0000214
Ollie Lho184a4042005-11-26 21:55:36 +0000215 return -1;
216}
217
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000218int find_next_included_romentry(unsigned int start)
Ollie Lho184a4042005-11-26 21:55:36 +0000219{
220 int i;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000221 unsigned int best_start = UINT_MAX;
222 int best_entry = -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000223
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000224 /* First come, first serve for overlapping regions. */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000225 for (i = 0; i < romimages; i++) {
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000226 if (!rom_entries[i].included)
Ollie Lho184a4042005-11-26 21:55:36 +0000227 continue;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000228 /* Already past the current entry? */
229 if (start > rom_entries[i].end)
230 continue;
231 /* Inside the current entry? */
232 if (start >= rom_entries[i].start)
233 return i;
234 /* Entry begins after start. */
235 if (best_start > rom_entries[i].start) {
236 best_start = rom_entries[i].start;
237 best_entry = i;
238 }
Ollie Lho184a4042005-11-26 21:55:36 +0000239 }
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000240 return best_entry;
241}
Ollie Lho184a4042005-11-26 21:55:36 +0000242
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000243int handle_romentries(struct flashchip *flash, uint8_t *oldcontents, uint8_t *newcontents)
244{
245 unsigned int start = 0;
246 int entry;
247 unsigned int size = flash->total_size * 1024;
248
249 /* If no layout file was specified or the layout file was empty, assume
250 * that the user wants to flash the complete new image.
251 */
252 if (!romimages)
253 return 0;
254 /* Non-included romentries are ignored.
255 * The union of all included romentries is used from the new image.
256 */
257 while (start < size) {
258 entry = find_next_included_romentry(start);
259 /* No more romentries for remaining region? */
260 if (entry < 0) {
261 memcpy(newcontents + start, oldcontents + start,
262 size - start);
263 break;
264 }
265 if (rom_entries[entry].start > start)
266 memcpy(newcontents + start, oldcontents + start,
267 rom_entries[entry].start - start);
268 /* Skip to location after current romentry. */
269 start = rom_entries[entry].end + 1;
270 /* Catch overflow. */
271 if (!start)
272 break;
273 }
274
Ollie Lho184a4042005-11-26 21:55:36 +0000275 return 0;
276}