blob: a738fb22d505b32e0df2585017dc73048780fdd6 [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 <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <stdint.h>
Uwe Hermann0846f892007-08-23 13:34:59 +000025#include "flash.h"
Ollie Lho184a4042005-11-26 21:55:36 +000026
Uwe Hermanna7e05482007-05-09 10:17:44 +000027char *mainboard_vendor = NULL;
28char *mainboard_part = NULL;
29int romimages = 0;
Ollie Lho184a4042005-11-26 21:55:36 +000030
Ollie Lho184a4042005-11-26 21:55:36 +000031#define MAX_ROMLAYOUT 16
32
33typedef struct {
34 unsigned int start;
35 unsigned int end;
36 unsigned int included;
37 char name[256];
38} romlayout_t;
39
40romlayout_t rom_entries[MAX_ROMLAYOUT];
41
42static char *def_name = "DEFAULT";
43
Peter Stuge7ffbc6f2008-06-18 02:08:40 +000044int show_id(uint8_t *bios, int size, int force)
Ollie Lho184a4042005-11-26 21:55:36 +000045{
46 unsigned int *walk;
47
Uwe Hermanna7e05482007-05-09 10:17:44 +000048 walk = (unsigned int *)(bios + size - 0x10);
49 walk--;
Ollie Lho184a4042005-11-26 21:55:36 +000050
Uwe Hermanna7e05482007-05-09 10:17:44 +000051 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
Ollie Lho184a4042005-11-26 21:55:36 +000052 /* We might have an Nvidia chipset bios
53 * which stores the id information at a
54 * different location.
55 */
Uwe Hermanna7e05482007-05-09 10:17:44 +000056 walk = (unsigned int *)(bios + size - 0x80);
57 walk--;
Ollie Lho184a4042005-11-26 21:55:36 +000058 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000059
60 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
Uwe Hermannac309342007-10-10 17:42:20 +000061 printf("Flash image seems to be a legacy BIOS. Disabling checks.\n");
Uwe Hermanna7e05482007-05-09 10:17:44 +000062 mainboard_vendor = def_name;
63 mainboard_part = def_name;
Ollie Lho184a4042005-11-26 21:55:36 +000064 return 0;
65 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000066
Stefan Reinauere3f3e2e2008-01-18 15:33:10 +000067 printf_debug("coreboot last image size "
Uwe Hermanna502dce2007-10-17 23:55:15 +000068 "(not ROM size) is %d bytes.\n", *walk);
Uwe Hermanna7e05482007-05-09 10:17:44 +000069
70 walk--;
71 mainboard_part = strdup((const char *)(bios + size - *walk));
72 walk--;
73 mainboard_vendor = strdup((const char *)(bios + size - *walk));
Uwe Hermanna502dce2007-10-17 23:55:15 +000074 printf_debug("Manufacturer: %s\n", mainboard_vendor);
75 printf_debug("Mainboard ID: %s\n", mainboard_part);
Stefan Reinauer3a431602005-12-18 18:40:46 +000076
77 /*
Stefan Reinauere3f3e2e2008-01-18 15:33:10 +000078 * If lb_vendor is not set, the coreboot table was
Stefan Reinauer3a431602005-12-18 18:40:46 +000079 * not found. Nor was -mVENDOR:PART specified
80 */
81
Uwe Hermanna7e05482007-05-09 10:17:44 +000082 if (!lb_vendor || !lb_part) {
Stefan Reinauer3a431602005-12-18 18:40:46 +000083 printf("Note: If the following flash access fails, "
Uwe Hermanna502dce2007-10-17 23:55:15 +000084 "you might need to specify -m <vendor>:<mainboard>.\n");
Stefan Reinauer3a431602005-12-18 18:40:46 +000085 return 0;
86 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000087
Stefan Reinauere3705282005-12-18 16:41:10 +000088 /* These comparisons are case insensitive to make things
89 * a little less user^Werror prone.
90 */
Stefan Reinauer3a431602005-12-18 18:40:46 +000091
Uwe Hermanna7e05482007-05-09 10:17:44 +000092 if (!strcasecmp(mainboard_vendor, lb_vendor) &&
93 !strcasecmp(mainboard_part, lb_part)) {
Stefan Reinauer3a431602005-12-18 18:40:46 +000094 printf_debug("This firmware image matches "
Uwe Hermannac309342007-10-10 17:42:20 +000095 "this motherboard.\n");
Ollie Lho184a4042005-11-26 21:55:36 +000096 } else {
Uwe Hermanna7e05482007-05-09 10:17:44 +000097 if (force) {
Ollie Lho184a4042005-11-26 21:55:36 +000098 printf("WARNING: This firmware image does not "
Uwe Hermannac309342007-10-10 17:42:20 +000099 "seem to fit to this machine - forcing it.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000100 } else {
Stefan Reinauer3a431602005-12-18 18:40:46 +0000101 printf("ERROR: Your firmware image (%s:%s) does not "
Uwe Hermanna7e05482007-05-09 10:17:44 +0000102 "appear to\n be correct for the detected "
103 "mainboard (%s:%s)\n\nOverride with --force if you "
104 "are absolutely sure that you\nare using a correct "
105 "image for this mainboard or override\nthe detected "
Uwe Hermannac309342007-10-10 17:42:20 +0000106 "values with --mainboard <vendor>:<mainboard>.\n\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000107 mainboard_vendor, mainboard_part, lb_vendor,
108 lb_part);
Ollie Lho184a4042005-11-26 21:55:36 +0000109 exit(1);
110 }
111 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000112
Ollie Lho184a4042005-11-26 21:55:36 +0000113 return 0;
114}
115
Uwe Hermanna7e05482007-05-09 10:17:44 +0000116int read_romlayout(char *name)
Ollie Lho184a4042005-11-26 21:55:36 +0000117{
118 FILE *romlayout;
119 char tempstr[256];
120 int i;
121
Uwe Hermanna7e05482007-05-09 10:17:44 +0000122 romlayout = fopen(name, "r");
123
124 if (!romlayout) {
Uwe Hermanna502dce2007-10-17 23:55:15 +0000125 fprintf(stderr, "ERROR: Could not open ROM layout (%s).\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000126 name);
Ollie Lho184a4042005-11-26 21:55:36 +0000127 return -1;
128 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000129
130 while (!feof(romlayout)) {
Ollie Lho184a4042005-11-26 21:55:36 +0000131 char *tstr1, *tstr2;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000132 fscanf(romlayout, "%s %s\n", tempstr,
133 rom_entries[romimages].name);
Ollie Lho184a4042005-11-26 21:55:36 +0000134#if 0
135 // fscanf does not like arbitrary comments like that :( later
Uwe Hermanna7e05482007-05-09 10:17:44 +0000136 if (tempstr[0] == '#') {
Ollie Lho184a4042005-11-26 21:55:36 +0000137 continue;
138 }
139#endif
Uwe Hermanna7e05482007-05-09 10:17:44 +0000140 tstr1 = strtok(tempstr, ":");
141 tstr2 = strtok(NULL, ":");
142 rom_entries[romimages].start = strtol(tstr1, (char **)NULL, 16);
143 rom_entries[romimages].end = strtol(tstr2, (char **)NULL, 16);
144 rom_entries[romimages].included = 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000145 romimages++;
146 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000147
148 for (i = 0; i < romimages; i++) {
149 printf_debug("romlayout %08x - %08x named %s\n",
150 rom_entries[i].start,
151 rom_entries[i].end, rom_entries[i].name);
Ollie Lho184a4042005-11-26 21:55:36 +0000152 }
153
154 fclose(romlayout);
Uwe Hermannffec5f32007-08-23 16:08:21 +0000155
Uwe Hermanna7e05482007-05-09 10:17:44 +0000156 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000157}
158
159int find_romentry(char *name)
160{
161 int i;
162
Uwe Hermanna7e05482007-05-09 10:17:44 +0000163 if (!romimages)
164 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000165
166 printf("Looking for \"%s\"... ", name);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000167
168 for (i = 0; i < romimages; i++) {
169 if (!strcmp(rom_entries[i].name, name)) {
170 rom_entries[i].included = 1;
Uwe Hermannac309342007-10-10 17:42:20 +0000171 printf("found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000172 return i;
173 }
174 }
Uwe Hermanna502dce2007-10-17 23:55:15 +0000175 printf("not found.\n"); // Not found. Error.
Uwe Hermannffec5f32007-08-23 16:08:21 +0000176
Ollie Lho184a4042005-11-26 21:55:36 +0000177 return -1;
178}
179
180int handle_romentries(uint8_t *buffer, uint8_t *content)
181{
182 int i;
183
184 // This function does not safe flash write cycles.
185 //
186 // Also it does not cope with overlapping rom layout
187 // sections.
188 // example:
189 // 00000000:00008fff gfxrom
190 // 00009000:0003ffff normal
191 // 00040000:0007ffff fallback
192 // 00000000:0007ffff all
193 //
194 // If you'd specify -i all the included flag of all other
195 // sections is still 0, so no changes will be made to the
196 // flash. Same thing if you specify -i normal -i all only
197 // normal will be updated and the rest will be kept.
198
Uwe Hermanna7e05482007-05-09 10:17:44 +0000199 for (i = 0; i < romimages; i++) {
200
201 if (rom_entries[i].included)
Ollie Lho184a4042005-11-26 21:55:36 +0000202 continue;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000203
204 memcpy(buffer + rom_entries[i].start,
205 content + rom_entries[i].start,
206 rom_entries[i].end - rom_entries[i].start);
Ollie Lho184a4042005-11-26 21:55:36 +0000207 }
208
209 return 0;
210}