layout.c 9.09 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * This file is part of the flashrom project.
 *
 * Copyright (C) 2005-2008 coresystems GmbH
 * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

21
#include <stdio.h>
22 23
#include <stdlib.h>
#include <string.h>
24
#include <ctype.h>
25
#include <limits.h>
26
#include "flash.h"
27
#include "programmer.h"
28

29
#if CONFIG_INTERNAL == 1
30 31
char *mainboard_vendor = NULL;
char *mainboard_part = NULL;
32
#endif
33
static int romimages = 0;
34

David Hendricks's avatar
David Hendricks committed
35
#define MAX_ROMLAYOUT	32
36 37 38 39 40 41 42 43

typedef struct {
	unsigned int start;
	unsigned int end;
	unsigned int included;
	char name[256];
} romlayout_t;

44 45 46 47 48 49
/* include_args lists arguments specified at the command line with -i. They
 * must be processed at some point so that desired regions are marked as
 * "included" in the rom_entries list.
 */
static char *include_args[MAX_ROMLAYOUT];
static int num_include_args = 0; /* the number of valid entries. */
50
static romlayout_t rom_entries[MAX_ROMLAYOUT];
51

52
#if CONFIG_INTERNAL == 1 /* FIXME: Move the whole block to cbtable.c? */
53 54
static char *def_name = "DEFAULT";

Peter Stuge's avatar
Peter Stuge committed
55
int show_id(uint8_t *bios, int size, int force)
56 57
{
	unsigned int *walk;
58 59 60 61 62
	unsigned int mb_part_offset, mb_vendor_offset;
	char *mb_part, *mb_vendor;

	mainboard_vendor = def_name;
	mainboard_part = def_name;
63

64 65
	walk = (unsigned int *)(bios + size - 0x10);
	walk--;
66

67
	if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
Uwe Hermann's avatar
Uwe Hermann committed
68 69
		/* We might have an NVIDIA chipset BIOS which stores the ID
		 * information at a different location.
70
		 */
71 72
		walk = (unsigned int *)(bios + size - 0x80);
		walk--;
73
	}
74

75 76 77 78 79 80
	/*
	 * Check if coreboot last image size is 0 or not a multiple of 1k or
	 * bigger than the chip or if the pointers to vendor ID or mainboard ID
	 * are outside the image of if the start of ID strings are nonsensical
	 * (nonprintable and not \0).
	 */
81 82 83 84
	mb_part_offset = *(walk - 1);
	mb_vendor_offset = *(walk - 2);
	if ((*walk) == 0 || ((*walk) & 0x3ff) != 0 || (*walk) > size ||
	    mb_part_offset > size || mb_vendor_offset > size) {
85 86
		msg_pinfo("Flash image seems to be a legacy BIOS. "
		          "Disabling coreboot-related checks.\n");
87 88
		return 0;
	}
89

90 91 92 93
	mb_part = (char *)(bios + size - mb_part_offset);
	mb_vendor = (char *)(bios + size - mb_vendor_offset);
	if (!isprint((unsigned char)*mb_part) ||
	    !isprint((unsigned char)*mb_vendor)) {
94
		msg_pinfo("Flash image seems to have garbage in the ID location."
95
		       " Disabling checks.\n");
96 97
		return 0;
	}
98

99
	msg_pdbg("coreboot last image size "
100
		     "(not ROM size) is %d bytes.\n", *walk);
101

102 103
	mainboard_part = strdup(mb_part);
	mainboard_vendor = strdup(mb_vendor);
104 105
	msg_pdbg("Manufacturer: %s\n", mainboard_vendor);
	msg_pdbg("Mainboard ID: %s\n", mainboard_part);
106 107

	/*
Stefan Reinauer's avatar
Stefan Reinauer committed
108
	 * If lb_vendor is not set, the coreboot table was
109
	 * not found. Nor was -p internal:mainboard=VENDOR:PART specified.
110
	 */
111
	if (!lb_vendor || !lb_part) {
112 113
		msg_pinfo("Note: If the following flash access fails, try "
			  "-p internal:mainboard=<vendor>:<mainboard>.\n");
114 115
		return 0;
	}
116

Stefan Reinauer's avatar
Stefan Reinauer committed
117 118 119
	/* These comparisons are case insensitive to make things
	 * a little less user^Werror prone. 
	 */
120 121
	if (!strcasecmp(mainboard_vendor, lb_vendor) &&
	    !strcasecmp(mainboard_part, lb_part)) {
122
		msg_pdbg("This firmware image matches this mainboard.\n");
123
	} else {
124
		if (force_boardmismatch) {
125
			msg_pinfo("WARNING: This firmware image does not "
Uwe Hermann's avatar
Uwe Hermann committed
126
			       "seem to fit to this machine - forcing it.\n");
127
		} else {
128
			msg_pinfo("ERROR: Your firmware image (%s:%s) does not "
129 130 131 132 133 134 135 136 137 138 139
				  "appear to\n"
				  "       be correct for the detected "
				  "mainboard (%s:%s)\n\n"
				  "Override with -p internal:boardmismatch="
				  "force to ignore the board name in the\n"
				  "firmware image or override the detected "
				  "mainboard with\n"
				  "-p internal:mainboard=<vendor>:<mainboard>."
				  "\n\n",
				  mainboard_vendor, mainboard_part, lb_vendor,
				  lb_part);
140
			return 1;
141 142
		}
	}
143

144 145
	return 0;
}
146
#endif
147

148
#ifndef __LIBPAYLOAD__
149
int read_romlayout(char *name)
150 151 152 153 154
{
	FILE *romlayout;
	char tempstr[256];
	int i;

155 156 157
	romlayout = fopen(name, "r");

	if (!romlayout) {
158
		msg_gerr("ERROR: Could not open ROM layout (%s).\n",
159
			name);
160 161
		return -1;
	}
162 163

	while (!feof(romlayout)) {
164
		char *tstr1, *tstr2;
165 166 167

		if (romimages >= MAX_ROMLAYOUT) {
			msg_gerr("Maximum number of ROM images (%i) in layout "
168 169
				 "file reached.\n", MAX_ROMLAYOUT);
			return 1;
170
		}
171 172
		if (2 != fscanf(romlayout, "%s %s\n", tempstr, rom_entries[romimages].name))
			continue;
173 174
#if 0
		// fscanf does not like arbitrary comments like that :( later
175
		if (tempstr[0] == '#') {
176 177 178
			continue;
		}
#endif
179 180
		tstr1 = strtok(tempstr, ":");
		tstr2 = strtok(NULL, ":");
181
		if (!tstr1 || !tstr2) {
182
			msg_gerr("Error parsing layout file.\n");
183 184 185
			fclose(romlayout);
			return 1;
		}
186 187 188
		rom_entries[romimages].start = strtol(tstr1, (char **)NULL, 16);
		rom_entries[romimages].end = strtol(tstr2, (char **)NULL, 16);
		rom_entries[romimages].included = 0;
189 190
		romimages++;
	}
191 192

	for (i = 0; i < romimages; i++) {
193
		msg_gdbg("romlayout %08x - %08x named %s\n",
194 195
			     rom_entries[i].start,
			     rom_entries[i].end, rom_entries[i].name);
196 197 198
	}

	fclose(romlayout);
Uwe Hermann's avatar
Uwe Hermann committed
199

200
	return 0;
201
}
202
#endif
203

204 205 206 207 208 209 210 211 212 213 214
/* returns the index of the entry (or a negative value if it is not found) */
int find_include_arg(const char *const name)
{
	unsigned int i;
	for (i = 0; i < num_include_args; i++) {
		if (!strcmp(include_args[i], name))
			return i;
	}
	return -1;
}

215 216 217 218 219 220 221 222 223 224 225 226 227
/* register an include argument (-i) for later processing */
int register_include_arg(char *name)
{
	if (num_include_args >= MAX_ROMLAYOUT) {
		msg_gerr("Too many regions included (%i).\n", num_include_args);
		return 1;
	}

	if (name == NULL) {
		msg_gerr("<NULL> is a bad region name.\n");
		return 1;
	}

228 229 230 231 232
	if (find_include_arg(name) != -1) {
		msg_gerr("Duplicate region name: \"%s\".\n", name);
		return 1;
	}

233 234 235 236 237 238 239
	include_args[num_include_args] = name;
	num_include_args++;
	return 0;
}

/* returns the index of the entry (or a negative value if it is not found) */
static int find_romentry(char *name)
240 241 242
{
	int i;

243 244
	if (!romimages)
		return -1;
245

246
	msg_gspew("Looking for region \"%s\"... ", name);
247 248 249
	for (i = 0; i < romimages; i++) {
		if (!strcmp(rom_entries[i].name, name)) {
			rom_entries[i].included = 1;
250
			msg_gspew("found.\n");
251 252 253
			return i;
		}
	}
254
	msg_gspew("not found.\n");
255 256 257
	return -1;
}

258 259 260 261 262 263 264 265 266 267 268
/* process -i arguments
 * returns 0 to indicate success, >0 to indicate failure
 */
int process_include_args(void)
{
	int i;
	unsigned int found = 0;

	if (num_include_args == 0)
		return 0;

269 270 271 272 273 274 275
	/* User has specified an area, but no layout file is loaded. */
	if (!romimages) {
		msg_gerr("Region requested (with -i \"%s\"), "
			 "but no layout data is available.\n",
			 include_args[0]);
		return 1;
	}
276

277
	for (i = 0; i < num_include_args; i++) {
278
		if (find_romentry(include_args[i]) < 0) {
279
			msg_gerr("Invalid region specified: \"%s\".\n",
280 281 282 283 284 285 286 287 288 289 290 291 292 293
				 include_args[i]);
			return 1;
		}
		found++;
	}

	msg_ginfo("Using region%s: \"%s\"", num_include_args > 1 ? "s" : "",
		  include_args[0]);
	for (i = 1; i < num_include_args; i++)
		msg_ginfo(", \"%s\"", include_args[i]);
	msg_ginfo(".\n");
	return 0;
}

294
romlayout_t *get_next_included_romentry(unsigned int start)
295 296
{
	int i;
297
	unsigned int best_start = UINT_MAX;
298 299
	romlayout_t *best_entry = NULL;
	romlayout_t *cur;
300

301
	/* First come, first serve for overlapping regions. */
302
	for (i = 0; i < romimages; i++) {
303 304
		cur = &rom_entries[i];
		if (!cur->included)
305
			continue;
306
		/* Already past the current entry? */
307
		if (start > cur->end)
308 309
			continue;
		/* Inside the current entry? */
310 311
		if (start >= cur->start)
			return cur;
312
		/* Entry begins after start. */
313 314 315
		if (best_start > cur->start) {
			best_start = cur->start;
			best_entry = cur;
316
		}
317
	}
318 319 320
	return best_entry;
}

321
int handle_romentries(struct flashctx *flash, uint8_t *oldcontents, uint8_t *newcontents)
322 323
{
	unsigned int start = 0;
324
	romlayout_t *entry;
325
	unsigned int size = flash->total_size * 1024;
326

327 328
	/* If no regions were specified for inclusion, assume
	 * that the user wants to write the complete new image.
329
	 */
330
	if (num_include_args == 0)
331
		return 0;
332

333 334 335 336
	/* Non-included romentries are ignored.
	 * The union of all included romentries is used from the new image.
	 */
	while (start < size) {
337
		entry = get_next_included_romentry(start);
338
		/* No more romentries for remaining region? */
339
		if (!entry) {
340 341 342 343
			memcpy(newcontents + start, oldcontents + start,
			       size - start);
			break;
		}
344
		/* For non-included region, copy from old content. */
345
		if (entry->start > start)
346
			memcpy(newcontents + start, oldcontents + start,
347
			       entry->start - start);
348
		/* Skip to location after current romentry. */
349
		start = entry->end + 1;
350 351 352 353
		/* Catch overflow. */
		if (!start)
			break;
	}
354 355
	return 0;
}