blob: df77c2fe3c7965162b40766ed205764d3918bf98 [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
Ollie Lho184a4042005-11-26 21:55:36 +000021#include <stdlib.h>
22#include <string.h>
Carl-Daniel Hailfingerecab4fc2008-07-03 14:40:06 +000023#include <ctype.h>
Uwe Hermann0846f892007-08-23 13:34:59 +000024#include "flash.h"
Ollie Lho184a4042005-11-26 21:55:36 +000025
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000026#if INTERNAL_SUPPORT == 1
Uwe Hermanna7e05482007-05-09 10:17:44 +000027char *mainboard_vendor = NULL;
28char *mainboard_part = NULL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000029#endif
Uwe Hermanna7e05482007-05-09 10:17:44 +000030int romimages = 0;
Ollie Lho184a4042005-11-26 21:55:36 +000031
Ollie Lho184a4042005-11-26 21:55:36 +000032#define MAX_ROMLAYOUT 16
33
34typedef struct {
35 unsigned int start;
36 unsigned int end;
37 unsigned int included;
38 char name[256];
39} romlayout_t;
40
41romlayout_t rom_entries[MAX_ROMLAYOUT];
42
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000043#if INTERNAL_SUPPORT == 1 /* FIXME: Move the whole block to cbtable.c? */
Ollie Lho184a4042005-11-26 21:55:36 +000044static char *def_name = "DEFAULT";
45
Peter Stuge7ffbc6f2008-06-18 02:08:40 +000046int show_id(uint8_t *bios, int size, int force)
Ollie Lho184a4042005-11-26 21:55:36 +000047{
48 unsigned int *walk;
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000049 unsigned int mb_part_offset, mb_vendor_offset;
50 char *mb_part, *mb_vendor;
51
52 mainboard_vendor = def_name;
53 mainboard_part = def_name;
Ollie Lho184a4042005-11-26 21:55:36 +000054
Uwe Hermanna7e05482007-05-09 10:17:44 +000055 walk = (unsigned int *)(bios + size - 0x10);
56 walk--;
Ollie Lho184a4042005-11-26 21:55:36 +000057
Uwe Hermanna7e05482007-05-09 10:17:44 +000058 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
Uwe Hermann6c8866c2008-07-03 19:26:44 +000059 /* We might have an NVIDIA chipset BIOS which stores the ID
60 * information at a different location.
Ollie Lho184a4042005-11-26 21:55:36 +000061 */
Uwe Hermanna7e05482007-05-09 10:17:44 +000062 walk = (unsigned int *)(bios + size - 0x80);
63 walk--;
Ollie Lho184a4042005-11-26 21:55:36 +000064 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000065
Carl-Daniel Hailfingerecab4fc2008-07-03 14:40:06 +000066 /*
67 * Check if coreboot last image size is 0 or not a multiple of 1k or
68 * bigger than the chip or if the pointers to vendor ID or mainboard ID
69 * are outside the image of if the start of ID strings are nonsensical
70 * (nonprintable and not \0).
71 */
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000072 mb_part_offset = *(walk - 1);
73 mb_vendor_offset = *(walk - 2);
74 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0 || (*walk) > size ||
75 mb_part_offset > size || mb_vendor_offset > size) {
Uwe Hermannac309342007-10-10 17:42:20 +000076 printf("Flash image seems to be a legacy BIOS. Disabling checks.\n");
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000077 return 0;
78 }
Uwe Hermann394131e2008-10-18 21:14:13 +000079
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000080 mb_part = (char *)(bios + size - mb_part_offset);
81 mb_vendor = (char *)(bios + size - mb_vendor_offset);
82 if (!isprint((unsigned char)*mb_part) ||
83 !isprint((unsigned char)*mb_vendor)) {
84 printf("Flash image seems to have garbage in the ID location."
Uwe Hermann394131e2008-10-18 21:14:13 +000085 " Disabling checks.\n");
Ollie Lho184a4042005-11-26 21:55:36 +000086 return 0;
87 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000088
Stefan Reinauere3f3e2e2008-01-18 15:33:10 +000089 printf_debug("coreboot last image size "
Uwe Hermanna502dce2007-10-17 23:55:15 +000090 "(not ROM size) is %d bytes.\n", *walk);
Uwe Hermanna7e05482007-05-09 10:17:44 +000091
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000092 mainboard_part = strdup(mb_part);
93 mainboard_vendor = strdup(mb_vendor);
Uwe Hermanna502dce2007-10-17 23:55:15 +000094 printf_debug("Manufacturer: %s\n", mainboard_vendor);
95 printf_debug("Mainboard ID: %s\n", mainboard_part);
Stefan Reinauer3a431602005-12-18 18:40:46 +000096
97 /*
Stefan Reinauere3f3e2e2008-01-18 15:33:10 +000098 * If lb_vendor is not set, the coreboot table was
Uwe Hermann6c8866c2008-07-03 19:26:44 +000099 * not found. Nor was -m VENDOR:PART specified.
Stefan Reinauer3a431602005-12-18 18:40:46 +0000100 */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000101 if (!lb_vendor || !lb_part) {
Stefan Reinauer3a431602005-12-18 18:40:46 +0000102 printf("Note: If the following flash access fails, "
Uwe Hermann6c8866c2008-07-03 19:26:44 +0000103 "try -m <vendor>:<mainboard>.\n");
Stefan Reinauer3a431602005-12-18 18:40:46 +0000104 return 0;
105 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000106
Stefan Reinauere3705282005-12-18 16:41:10 +0000107 /* These comparisons are case insensitive to make things
108 * a little less user^Werror prone.
109 */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000110 if (!strcasecmp(mainboard_vendor, lb_vendor) &&
111 !strcasecmp(mainboard_part, lb_part)) {
Uwe Hermann3d5f96c2009-04-23 14:57:55 +0000112 printf_debug("This firmware image matches this mainboard.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000113 } else {
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000114 if (force_boardmismatch) {
Ollie Lho184a4042005-11-26 21:55:36 +0000115 printf("WARNING: This firmware image does not "
Uwe Hermannac309342007-10-10 17:42:20 +0000116 "seem to fit to this machine - forcing it.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000117 } else {
Stefan Reinauer3a431602005-12-18 18:40:46 +0000118 printf("ERROR: Your firmware image (%s:%s) does not "
Uwe Hermanna7e05482007-05-09 10:17:44 +0000119 "appear to\n be correct for the detected "
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000120 "mainboard (%s:%s)\n\nOverride with -p internal:"
121 "boardmismatch=force if you are absolutely sure "
122 "that\nyou are using a correct "
Uwe Hermanna7e05482007-05-09 10:17:44 +0000123 "image for this mainboard or override\nthe detected "
Uwe Hermannac309342007-10-10 17:42:20 +0000124 "values with --mainboard <vendor>:<mainboard>.\n\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000125 mainboard_vendor, mainboard_part, lb_vendor,
126 lb_part);
Ollie Lho184a4042005-11-26 21:55:36 +0000127 exit(1);
128 }
129 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000130
Ollie Lho184a4042005-11-26 21:55:36 +0000131 return 0;
132}
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000133#endif
Ollie Lho184a4042005-11-26 21:55:36 +0000134
Uwe Hermanna7e05482007-05-09 10:17:44 +0000135int read_romlayout(char *name)
Ollie Lho184a4042005-11-26 21:55:36 +0000136{
137 FILE *romlayout;
138 char tempstr[256];
139 int i;
140
Uwe Hermanna7e05482007-05-09 10:17:44 +0000141 romlayout = fopen(name, "r");
142
143 if (!romlayout) {
Uwe Hermanna502dce2007-10-17 23:55:15 +0000144 fprintf(stderr, "ERROR: Could not open ROM layout (%s).\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000145 name);
Ollie Lho184a4042005-11-26 21:55:36 +0000146 return -1;
147 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000148
149 while (!feof(romlayout)) {
Ollie Lho184a4042005-11-26 21:55:36 +0000150 char *tstr1, *tstr2;
Peter Stuge1fec0f32009-01-12 21:00:35 +0000151 if (2 != fscanf(romlayout, "%s %s\n", tempstr, rom_entries[romimages].name))
152 continue;
Ollie Lho184a4042005-11-26 21:55:36 +0000153#if 0
154 // fscanf does not like arbitrary comments like that :( later
Uwe Hermanna7e05482007-05-09 10:17:44 +0000155 if (tempstr[0] == '#') {
Ollie Lho184a4042005-11-26 21:55:36 +0000156 continue;
157 }
158#endif
Uwe Hermanna7e05482007-05-09 10:17:44 +0000159 tstr1 = strtok(tempstr, ":");
160 tstr2 = strtok(NULL, ":");
Uwe Hermann58783e32008-12-22 16:42:59 +0000161 if (!tstr1 || !tstr2) {
162 fprintf(stderr, "Error parsing layout file.\n");
163 fclose(romlayout);
164 return 1;
165 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000166 rom_entries[romimages].start = strtol(tstr1, (char **)NULL, 16);
167 rom_entries[romimages].end = strtol(tstr2, (char **)NULL, 16);
168 rom_entries[romimages].included = 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000169 romimages++;
170 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000171
172 for (i = 0; i < romimages; i++) {
173 printf_debug("romlayout %08x - %08x named %s\n",
174 rom_entries[i].start,
175 rom_entries[i].end, rom_entries[i].name);
Ollie Lho184a4042005-11-26 21:55:36 +0000176 }
177
178 fclose(romlayout);
Uwe Hermannffec5f32007-08-23 16:08:21 +0000179
Uwe Hermanna7e05482007-05-09 10:17:44 +0000180 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000181}
182
183int find_romentry(char *name)
184{
185 int i;
186
Uwe Hermanna7e05482007-05-09 10:17:44 +0000187 if (!romimages)
188 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000189
190 printf("Looking for \"%s\"... ", name);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000191
192 for (i = 0; i < romimages; i++) {
193 if (!strcmp(rom_entries[i].name, name)) {
194 rom_entries[i].included = 1;
Uwe Hermannac309342007-10-10 17:42:20 +0000195 printf("found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000196 return i;
197 }
198 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000199 printf("not found.\n"); // Not found. Error.
Uwe Hermannffec5f32007-08-23 16:08:21 +0000200
Ollie Lho184a4042005-11-26 21:55:36 +0000201 return -1;
202}
203
Carl-Daniel Hailfingerf5fb51c2009-08-19 15:19:18 +0000204int handle_romentries(uint8_t *buffer, struct flashchip *flash)
Ollie Lho184a4042005-11-26 21:55:36 +0000205{
206 int i;
207
Carl-Daniel Hailfingerf5fb51c2009-08-19 15:19:18 +0000208 // This function does not save flash write cycles.
Ollie Lho184a4042005-11-26 21:55:36 +0000209 //
210 // Also it does not cope with overlapping rom layout
211 // sections.
212 // example:
213 // 00000000:00008fff gfxrom
214 // 00009000:0003ffff normal
215 // 00040000:0007ffff fallback
216 // 00000000:0007ffff all
217 //
218 // If you'd specify -i all the included flag of all other
219 // sections is still 0, so no changes will be made to the
220 // flash. Same thing if you specify -i normal -i all only
221 // normal will be updated and the rest will be kept.
222
Uwe Hermanna7e05482007-05-09 10:17:44 +0000223 for (i = 0; i < romimages; i++) {
224
225 if (rom_entries[i].included)
Ollie Lho184a4042005-11-26 21:55:36 +0000226 continue;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000227
Carl-Daniel Hailfingerf5fb51c2009-08-19 15:19:18 +0000228 flash->read(flash, buffer + rom_entries[i].start,
229 rom_entries[i].start,
230 rom_entries[i].end - rom_entries[i].start + 1);
Ollie Lho184a4042005-11-26 21:55:36 +0000231 }
232
233 return 0;
234}