blob: d65e370850e08f716ec1204f5af9309b75947ce2 [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>
Uwe Hermann0846f892007-08-23 13:34:59 +000025#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000026#include "programmer.h"
Ollie Lho184a4042005-11-26 21:55:36 +000027
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000028#if CONFIG_INTERNAL == 1
Uwe Hermanna7e05482007-05-09 10:17:44 +000029char *mainboard_vendor = NULL;
30char *mainboard_part = NULL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000031#endif
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000032static int romimages = 0;
Ollie Lho184a4042005-11-26 21:55:36 +000033
Ollie Lho184a4042005-11-26 21:55:36 +000034#define MAX_ROMLAYOUT 16
35
36typedef struct {
37 unsigned int start;
38 unsigned int end;
39 unsigned int included;
40 char name[256];
41} romlayout_t;
42
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000043static romlayout_t rom_entries[MAX_ROMLAYOUT];
Ollie Lho184a4042005-11-26 21:55:36 +000044
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000045#if CONFIG_INTERNAL == 1 /* FIXME: Move the whole block to cbtable.c? */
Ollie Lho184a4042005-11-26 21:55:36 +000046static char *def_name = "DEFAULT";
47
Peter Stuge7ffbc6f2008-06-18 02:08:40 +000048int show_id(uint8_t *bios, int size, int force)
Ollie Lho184a4042005-11-26 21:55:36 +000049{
50 unsigned int *walk;
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000051 unsigned int mb_part_offset, mb_vendor_offset;
52 char *mb_part, *mb_vendor;
53
54 mainboard_vendor = def_name;
55 mainboard_part = def_name;
Ollie Lho184a4042005-11-26 21:55:36 +000056
Uwe Hermanna7e05482007-05-09 10:17:44 +000057 walk = (unsigned int *)(bios + size - 0x10);
58 walk--;
Ollie Lho184a4042005-11-26 21:55:36 +000059
Uwe Hermanna7e05482007-05-09 10:17:44 +000060 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
Uwe Hermann6c8866c2008-07-03 19:26:44 +000061 /* We might have an NVIDIA chipset BIOS which stores the ID
62 * information at a different location.
Ollie Lho184a4042005-11-26 21:55:36 +000063 */
Uwe Hermanna7e05482007-05-09 10:17:44 +000064 walk = (unsigned int *)(bios + size - 0x80);
65 walk--;
Ollie Lho184a4042005-11-26 21:55:36 +000066 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000067
Carl-Daniel Hailfingerecab4fc2008-07-03 14:40:06 +000068 /*
69 * Check if coreboot last image size is 0 or not a multiple of 1k or
70 * bigger than the chip or if the pointers to vendor ID or mainboard ID
71 * are outside the image of if the start of ID strings are nonsensical
72 * (nonprintable and not \0).
73 */
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000074 mb_part_offset = *(walk - 1);
75 mb_vendor_offset = *(walk - 2);
76 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0 || (*walk) > size ||
77 mb_part_offset > size || mb_vendor_offset > size) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000078 msg_pinfo("Flash image seems to be a legacy BIOS. Disabling checks.\n");
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000079 return 0;
80 }
Uwe Hermann394131e2008-10-18 21:14:13 +000081
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000082 mb_part = (char *)(bios + size - mb_part_offset);
83 mb_vendor = (char *)(bios + size - mb_vendor_offset);
84 if (!isprint((unsigned char)*mb_part) ||
85 !isprint((unsigned char)*mb_vendor)) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000086 msg_pinfo("Flash image seems to have garbage in the ID location."
Uwe Hermann394131e2008-10-18 21:14:13 +000087 " Disabling checks.\n");
Ollie Lho184a4042005-11-26 21:55:36 +000088 return 0;
89 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000090
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000091 msg_pdbg("coreboot last image size "
Uwe Hermanna502dce2007-10-17 23:55:15 +000092 "(not ROM size) is %d bytes.\n", *walk);
Uwe Hermanna7e05482007-05-09 10:17:44 +000093
Carl-Daniel Hailfinger85f8a172008-07-11 00:06:38 +000094 mainboard_part = strdup(mb_part);
95 mainboard_vendor = strdup(mb_vendor);
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000096 msg_pdbg("Manufacturer: %s\n", mainboard_vendor);
97 msg_pdbg("Mainboard ID: %s\n", mainboard_part);
Stefan Reinauer3a431602005-12-18 18:40:46 +000098
99 /*
Stefan Reinauere3f3e2e2008-01-18 15:33:10 +0000100 * If lb_vendor is not set, the coreboot table was
Uwe Hermann6c8866c2008-07-03 19:26:44 +0000101 * not found. Nor was -m VENDOR:PART specified.
Stefan Reinauer3a431602005-12-18 18:40:46 +0000102 */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000103 if (!lb_vendor || !lb_part) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000104 msg_pinfo("Note: If the following flash access fails, "
Uwe Hermann6c8866c2008-07-03 19:26:44 +0000105 "try -m <vendor>:<mainboard>.\n");
Stefan Reinauer3a431602005-12-18 18:40:46 +0000106 return 0;
107 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000108
Stefan Reinauere3705282005-12-18 16:41:10 +0000109 /* These comparisons are case insensitive to make things
110 * a little less user^Werror prone.
111 */
Uwe Hermanna7e05482007-05-09 10:17:44 +0000112 if (!strcasecmp(mainboard_vendor, lb_vendor) &&
113 !strcasecmp(mainboard_part, lb_part)) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000114 msg_pdbg("This firmware image matches this mainboard.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000115 } else {
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000116 if (force_boardmismatch) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000117 msg_pinfo("WARNING: This firmware image does not "
Uwe Hermannac309342007-10-10 17:42:20 +0000118 "seem to fit to this machine - forcing it.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000119 } else {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000120 msg_pinfo("ERROR: Your firmware image (%s:%s) does not "
Uwe Hermanna7e05482007-05-09 10:17:44 +0000121 "appear to\n be correct for the detected "
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000122 "mainboard (%s:%s)\n\nOverride with -p internal:"
123 "boardmismatch=force if you are absolutely sure "
124 "that\nyou are using a correct "
Uwe Hermanna7e05482007-05-09 10:17:44 +0000125 "image for this mainboard or override\nthe detected "
Uwe Hermannac309342007-10-10 17:42:20 +0000126 "values with --mainboard <vendor>:<mainboard>.\n\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000127 mainboard_vendor, mainboard_part, lb_vendor,
128 lb_part);
Ollie Lho184a4042005-11-26 21:55:36 +0000129 exit(1);
130 }
131 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000132
Ollie Lho184a4042005-11-26 21:55:36 +0000133 return 0;
134}
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000135#endif
Ollie Lho184a4042005-11-26 21:55:36 +0000136
Uwe Hermanna7e05482007-05-09 10:17:44 +0000137int read_romlayout(char *name)
Ollie Lho184a4042005-11-26 21:55:36 +0000138{
139 FILE *romlayout;
140 char tempstr[256];
141 int i;
142
Uwe Hermanna7e05482007-05-09 10:17:44 +0000143 romlayout = fopen(name, "r");
144
145 if (!romlayout) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000146 msg_gerr("ERROR: Could not open ROM layout (%s).\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000147 name);
Ollie Lho184a4042005-11-26 21:55:36 +0000148 return -1;
149 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000150
151 while (!feof(romlayout)) {
Ollie Lho184a4042005-11-26 21:55:36 +0000152 char *tstr1, *tstr2;
Peter Stuge1fec0f32009-01-12 21:00:35 +0000153 if (2 != fscanf(romlayout, "%s %s\n", tempstr, rom_entries[romimages].name))
154 continue;
Ollie Lho184a4042005-11-26 21:55:36 +0000155#if 0
156 // fscanf does not like arbitrary comments like that :( later
Uwe Hermanna7e05482007-05-09 10:17:44 +0000157 if (tempstr[0] == '#') {
Ollie Lho184a4042005-11-26 21:55:36 +0000158 continue;
159 }
160#endif
Uwe Hermanna7e05482007-05-09 10:17:44 +0000161 tstr1 = strtok(tempstr, ":");
162 tstr2 = strtok(NULL, ":");
Uwe Hermann58783e32008-12-22 16:42:59 +0000163 if (!tstr1 || !tstr2) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000164 msg_gerr("Error parsing layout file.\n");
Uwe Hermann58783e32008-12-22 16:42:59 +0000165 fclose(romlayout);
166 return 1;
167 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000168 rom_entries[romimages].start = strtol(tstr1, (char **)NULL, 16);
169 rom_entries[romimages].end = strtol(tstr2, (char **)NULL, 16);
170 rom_entries[romimages].included = 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000171 romimages++;
172 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000173
174 for (i = 0; i < romimages; i++) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000175 msg_gdbg("romlayout %08x - %08x named %s\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000176 rom_entries[i].start,
177 rom_entries[i].end, rom_entries[i].name);
Ollie Lho184a4042005-11-26 21:55:36 +0000178 }
179
180 fclose(romlayout);
Uwe Hermannffec5f32007-08-23 16:08:21 +0000181
Uwe Hermanna7e05482007-05-09 10:17:44 +0000182 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000183}
184
185int find_romentry(char *name)
186{
187 int i;
188
Uwe Hermanna7e05482007-05-09 10:17:44 +0000189 if (!romimages)
190 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000191
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000192 msg_ginfo("Looking for \"%s\"... ", name);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000193
194 for (i = 0; i < romimages; i++) {
195 if (!strcmp(rom_entries[i].name, name)) {
196 rom_entries[i].included = 1;
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000197 msg_ginfo("found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000198 return i;
199 }
200 }
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000201 msg_ginfo("not found.\n"); // Not found. Error.
Uwe Hermannffec5f32007-08-23 16:08:21 +0000202
Ollie Lho184a4042005-11-26 21:55:36 +0000203 return -1;
204}
205
Carl-Daniel Hailfingerf5fb51c2009-08-19 15:19:18 +0000206int handle_romentries(uint8_t *buffer, struct flashchip *flash)
Ollie Lho184a4042005-11-26 21:55:36 +0000207{
208 int i;
209
Carl-Daniel Hailfingerf5fb51c2009-08-19 15:19:18 +0000210 // This function does not save flash write cycles.
Ollie Lho184a4042005-11-26 21:55:36 +0000211 //
212 // Also it does not cope with overlapping rom layout
213 // sections.
214 // example:
215 // 00000000:00008fff gfxrom
216 // 00009000:0003ffff normal
217 // 00040000:0007ffff fallback
218 // 00000000:0007ffff all
219 //
220 // If you'd specify -i all the included flag of all other
221 // sections is still 0, so no changes will be made to the
222 // flash. Same thing if you specify -i normal -i all only
223 // normal will be updated and the rest will be kept.
224
Uwe Hermanna7e05482007-05-09 10:17:44 +0000225 for (i = 0; i < romimages; i++) {
226
227 if (rom_entries[i].included)
Ollie Lho184a4042005-11-26 21:55:36 +0000228 continue;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000229
Carl-Daniel Hailfingerf5fb51c2009-08-19 15:19:18 +0000230 flash->read(flash, buffer + rom_entries[i].start,
231 rom_entries[i].start,
232 rom_entries[i].end - rom_entries[i].start + 1);
Ollie Lho184a4042005-11-26 21:55:36 +0000233 }
234
235 return 0;
236}