blob: ef0958286f532f51bde3352d84fad876df4296dd [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>
Carl-Daniel Hailfingerecab4fc2008-07-03 14:40:06 +000024#include <ctype.h>
Ollie Lho184a4042005-11-26 21:55:36 +000025#include <stdint.h>
Uwe Hermann0846f892007-08-23 13:34:59 +000026#include "flash.h"
Ollie Lho184a4042005-11-26 21:55:36 +000027
Uwe Hermanna7e05482007-05-09 10:17:44 +000028char *mainboard_vendor = NULL;
29char *mainboard_part = NULL;
30int 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
43static char *def_name = "DEFAULT";
44
Peter Stuge7ffbc6f2008-06-18 02:08:40 +000045int show_id(uint8_t *bios, int size, int force)
Ollie Lho184a4042005-11-26 21:55:36 +000046{
47 unsigned int *walk;
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000048 unsigned int mb_part_offset, mb_vendor_offset;
49 char *mb_part, *mb_vendor;
50
51 mainboard_vendor = def_name;
52 mainboard_part = def_name;
Ollie Lho184a4042005-11-26 21:55:36 +000053
Uwe Hermanna7e05482007-05-09 10:17:44 +000054 walk = (unsigned int *)(bios + size - 0x10);
55 walk--;
Ollie Lho184a4042005-11-26 21:55:36 +000056
Uwe Hermanna7e05482007-05-09 10:17:44 +000057 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
Uwe Hermann6c8866c2008-07-03 19:26:44 +000058 /* We might have an NVIDIA chipset BIOS which stores the ID
59 * information at a different location.
Ollie Lho184a4042005-11-26 21:55:36 +000060 */
Uwe Hermanna7e05482007-05-09 10:17:44 +000061 walk = (unsigned int *)(bios + size - 0x80);
62 walk--;
Ollie Lho184a4042005-11-26 21:55:36 +000063 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000064
Carl-Daniel Hailfingerecab4fc2008-07-03 14:40:06 +000065 /*
66 * Check if coreboot last image size is 0 or not a multiple of 1k or
67 * bigger than the chip or if the pointers to vendor ID or mainboard ID
68 * are outside the image of if the start of ID strings are nonsensical
69 * (nonprintable and not \0).
70 */
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000071 mb_part_offset = *(walk - 1);
72 mb_vendor_offset = *(walk - 2);
73 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0 || (*walk) > size ||
74 mb_part_offset > size || mb_vendor_offset > size) {
Uwe Hermannac309342007-10-10 17:42:20 +000075 printf("Flash image seems to be a legacy BIOS. Disabling checks.\n");
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000076 return 0;
77 }
Uwe Hermann394131e2008-10-18 21:14:13 +000078
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000079 mb_part = (char *)(bios + size - mb_part_offset);
80 mb_vendor = (char *)(bios + size - mb_vendor_offset);
81 if (!isprint((unsigned char)*mb_part) ||
82 !isprint((unsigned char)*mb_vendor)) {
83 printf("Flash image seems to have garbage in the ID location."
Uwe Hermann394131e2008-10-18 21:14:13 +000084 " Disabling checks.\n");
Ollie Lho184a4042005-11-26 21:55:36 +000085 return 0;
86 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000087
Stefan Reinauere3f3e2e2008-01-18 15:33:10 +000088 printf_debug("coreboot last image size "
Uwe Hermanna502dce2007-10-17 23:55:15 +000089 "(not ROM size) is %d bytes.\n", *walk);
Uwe Hermanna7e05482007-05-09 10:17:44 +000090
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000091 mainboard_part = strdup(mb_part);
92 mainboard_vendor = strdup(mb_vendor);
Uwe Hermanna502dce2007-10-17 23:55:15 +000093 printf_debug("Manufacturer: %s\n", mainboard_vendor);
94 printf_debug("Mainboard ID: %s\n", mainboard_part);
Stefan Reinauer3a431602005-12-18 18:40:46 +000095
96 /*
Stefan Reinauere3f3e2e2008-01-18 15:33:10 +000097 * If lb_vendor is not set, the coreboot table was
Uwe Hermann6c8866c2008-07-03 19:26:44 +000098 * not found. Nor was -m VENDOR:PART specified.
Stefan Reinauer3a431602005-12-18 18:40:46 +000099 */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000100 if (!lb_vendor || !lb_part) {
Stefan Reinauer3a431602005-12-18 18:40:46 +0000101 printf("Note: If the following flash access fails, "
Uwe Hermann6c8866c2008-07-03 19:26:44 +0000102 "try -m <vendor>:<mainboard>.\n");
Stefan Reinauer3a431602005-12-18 18:40:46 +0000103 return 0;
104 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000105
Stefan Reinauere3705282005-12-18 16:41:10 +0000106 /* These comparisons are case insensitive to make things
107 * a little less user^Werror prone.
108 */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000109 if (!strcasecmp(mainboard_vendor, lb_vendor) &&
110 !strcasecmp(mainboard_part, lb_part)) {
Stefan Reinauer3a431602005-12-18 18:40:46 +0000111 printf_debug("This firmware image matches "
Uwe Hermannac309342007-10-10 17:42:20 +0000112 "this motherboard.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000113 } else {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000114 if (force) {
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 "
120 "mainboard (%s:%s)\n\nOverride with --force if you "
121 "are absolutely sure that you\nare using a correct "
122 "image for this mainboard or override\nthe detected "
Uwe Hermannac309342007-10-10 17:42:20 +0000123 "values with --mainboard <vendor>:<mainboard>.\n\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000124 mainboard_vendor, mainboard_part, lb_vendor,
125 lb_part);
Ollie Lho184a4042005-11-26 21:55:36 +0000126 exit(1);
127 }
128 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000129
Ollie Lho184a4042005-11-26 21:55:36 +0000130 return 0;
131}
132
Uwe Hermanna7e05482007-05-09 10:17:44 +0000133int read_romlayout(char *name)
Ollie Lho184a4042005-11-26 21:55:36 +0000134{
135 FILE *romlayout;
136 char tempstr[256];
137 int i;
138
Uwe Hermanna7e05482007-05-09 10:17:44 +0000139 romlayout = fopen(name, "r");
140
141 if (!romlayout) {
Uwe Hermanna502dce2007-10-17 23:55:15 +0000142 fprintf(stderr, "ERROR: Could not open ROM layout (%s).\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000143 name);
Ollie Lho184a4042005-11-26 21:55:36 +0000144 return -1;
145 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000146
147 while (!feof(romlayout)) {
Ollie Lho184a4042005-11-26 21:55:36 +0000148 char *tstr1, *tstr2;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000149 fscanf(romlayout, "%s %s\n", tempstr,
150 rom_entries[romimages].name);
Ollie Lho184a4042005-11-26 21:55:36 +0000151#if 0
152 // fscanf does not like arbitrary comments like that :( later
Uwe Hermanna7e05482007-05-09 10:17:44 +0000153 if (tempstr[0] == '#') {
Ollie Lho184a4042005-11-26 21:55:36 +0000154 continue;
155 }
156#endif
Uwe Hermanna7e05482007-05-09 10:17:44 +0000157 tstr1 = strtok(tempstr, ":");
158 tstr2 = strtok(NULL, ":");
159 rom_entries[romimages].start = strtol(tstr1, (char **)NULL, 16);
160 rom_entries[romimages].end = strtol(tstr2, (char **)NULL, 16);
161 rom_entries[romimages].included = 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000162 romimages++;
163 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000164
165 for (i = 0; i < romimages; i++) {
166 printf_debug("romlayout %08x - %08x named %s\n",
167 rom_entries[i].start,
168 rom_entries[i].end, rom_entries[i].name);
Ollie Lho184a4042005-11-26 21:55:36 +0000169 }
170
171 fclose(romlayout);
Uwe Hermannffec5f32007-08-23 16:08:21 +0000172
Uwe Hermanna7e05482007-05-09 10:17:44 +0000173 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000174}
175
176int find_romentry(char *name)
177{
178 int i;
179
Uwe Hermanna7e05482007-05-09 10:17:44 +0000180 if (!romimages)
181 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000182
183 printf("Looking for \"%s\"... ", name);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000184
185 for (i = 0; i < romimages; i++) {
186 if (!strcmp(rom_entries[i].name, name)) {
187 rom_entries[i].included = 1;
Uwe Hermannac309342007-10-10 17:42:20 +0000188 printf("found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000189 return i;
190 }
191 }
Uwe Hermann394131e2008-10-18 21:14:13 +0000192 printf("not found.\n"); // Not found. Error.
Uwe Hermannffec5f32007-08-23 16:08:21 +0000193
Ollie Lho184a4042005-11-26 21:55:36 +0000194 return -1;
195}
196
197int handle_romentries(uint8_t *buffer, uint8_t *content)
198{
199 int i;
200
201 // This function does not safe flash write cycles.
202 //
203 // Also it does not cope with overlapping rom layout
204 // sections.
205 // example:
206 // 00000000:00008fff gfxrom
207 // 00009000:0003ffff normal
208 // 00040000:0007ffff fallback
209 // 00000000:0007ffff all
210 //
211 // If you'd specify -i all the included flag of all other
212 // sections is still 0, so no changes will be made to the
213 // flash. Same thing if you specify -i normal -i all only
214 // normal will be updated and the rest will be kept.
215
Uwe Hermanna7e05482007-05-09 10:17:44 +0000216 for (i = 0; i < romimages; i++) {
217
218 if (rom_entries[i].included)
Ollie Lho184a4042005-11-26 21:55:36 +0000219 continue;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000220
221 memcpy(buffer + rom_entries[i].start,
222 content + rom_entries[i].start,
223 rom_entries[i].end - rom_entries[i].start);
Ollie Lho184a4042005-11-26 21:55:36 +0000224 }
225
226 return 0;
227}