Flashrom

Flashrom Svn Source Tree

Root/trunk/layout.c

1/*
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
21#include <stdio.h>
22#include <stdlib.h>
23#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
30char *mainboard_vendor = NULL;
31char *mainboard_part = NULL;
32#endif
33static int romimages = 0;
34
35#define MAX_ROMLAYOUT32
36
37typedef struct {
38unsigned int start;
39unsigned int end;
40unsigned int included;
41char name[256];
42} romlayout_t;
43
44/* include_args lists arguments specified at the command line with -i. They
45 * must be processed at some point so that desired regions are marked as
46 * "included" in the rom_entries list.
47 */
48static char *include_args[MAX_ROMLAYOUT];
49static int num_include_args = 0; /* the number of valid entries. */
50static romlayout_t rom_entries[MAX_ROMLAYOUT];
51
52#if CONFIG_INTERNAL == 1 /* FIXME: Move the whole block to cbtable.c? */
53static char *def_name = "DEFAULT";
54
55int show_id(uint8_t *bios, int size, int force)
56{
57unsigned int *walk;
58unsigned int mb_part_offset, mb_vendor_offset;
59char *mb_part, *mb_vendor;
60
61mainboard_vendor = def_name;
62mainboard_part = def_name;
63
64walk = (unsigned int *)(bios + size - 0x10);
65walk--;
66
67if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
68/* We might have an NVIDIA chipset BIOS which stores the ID
69 * information at a different location.
70 */
71walk = (unsigned int *)(bios + size - 0x80);
72walk--;
73}
74
75/*
76 * Check if coreboot last image size is 0 or not a multiple of 1k or
77 * bigger than the chip or if the pointers to vendor ID or mainboard ID
78 * are outside the image of if the start of ID strings are nonsensical
79 * (nonprintable and not \0).
80 */
81mb_part_offset = *(walk - 1);
82mb_vendor_offset = *(walk - 2);
83if ((*walk) == 0 || ((*walk) & 0x3ff) != 0 || (*walk) > size ||
84 mb_part_offset > size || mb_vendor_offset > size) {
85msg_pinfo("Flash image seems to be a legacy BIOS. "
86 "Disabling coreboot-related checks.\n");
87return 0;
88}
89
90mb_part = (char *)(bios + size - mb_part_offset);
91mb_vendor = (char *)(bios + size - mb_vendor_offset);
92if (!isprint((unsigned char)*mb_part) ||
93 !isprint((unsigned char)*mb_vendor)) {
94msg_pinfo("Flash image seems to have garbage in the ID location."
95 " Disabling checks.\n");
96return 0;
97}
98
99msg_pdbg("coreboot last image size "
100 "(not ROM size) is %d bytes.\n", *walk);
101
102mainboard_part = strdup(mb_part);
103mainboard_vendor = strdup(mb_vendor);
104msg_pdbg("Manufacturer: %s\n", mainboard_vendor);
105msg_pdbg("Mainboard ID: %s\n", mainboard_part);
106
107/*
108 * If lb_vendor is not set, the coreboot table was
109 * not found. Nor was -p internal:mainboard=VENDOR:PART specified.
110 */
111if (!lb_vendor || !lb_part) {
112msg_pinfo("Note: If the following flash access fails, try "
113 "-p internal:mainboard=<vendor>:<mainboard>.\n");
114return 0;
115}
116
117/* These comparisons are case insensitive to make things
118 * a little less user^Werror prone.
119 */
120if (!strcasecmp(mainboard_vendor, lb_vendor) &&
121 !strcasecmp(mainboard_part, lb_part)) {
122msg_pdbg("This firmware image matches this mainboard.\n");
123} else {
124if (force_boardmismatch) {
125msg_pinfo("WARNING: This firmware image does not "
126 "seem to fit to this machine - forcing it.\n");
127} else {
128msg_pinfo("ERROR: Your firmware image (%s:%s) does not "
129 "appear to\n"
130 " be correct for the detected "
131 "mainboard (%s:%s)\n\n"
132 "Override with -p internal:boardmismatch="
133 "force to ignore the board name in the\n"
134 "firmware image or override the detected "
135 "mainboard with\n"
136 "-p internal:mainboard=<vendor>:<mainboard>."
137 "\n\n",
138 mainboard_vendor, mainboard_part, lb_vendor,
139 lb_part);
140exit(1);
141}
142}
143
144return 0;
145}
146#endif
147
148#ifndef __LIBPAYLOAD__
149int read_romlayout(char *name)
150{
151FILE *romlayout;
152char tempstr[256];
153int i;
154
155romlayout = fopen(name, "r");
156
157if (!romlayout) {
158msg_gerr("ERROR: Could not open ROM layout (%s).\n",
159name);
160return -1;
161}
162
163while (!feof(romlayout)) {
164char *tstr1, *tstr2;
165
166if (romimages >= MAX_ROMLAYOUT) {
167msg_gerr("Maximum number of ROM images (%i) in layout "
168 "file reached.\n", MAX_ROMLAYOUT);
169return 1;
170}
171if (2 != fscanf(romlayout, "%s %s\n", tempstr, rom_entries[romimages].name))
172continue;
173#if 0
174// fscanf does not like arbitrary comments like that :( later
175if (tempstr[0] == '#') {
176continue;
177}
178#endif
179tstr1 = strtok(tempstr, ":");
180tstr2 = strtok(NULL, ":");
181if (!tstr1 || !tstr2) {
182msg_gerr("Error parsing layout file.\n");
183fclose(romlayout);
184return 1;
185}
186rom_entries[romimages].start = strtol(tstr1, (char **)NULL, 16);
187rom_entries[romimages].end = strtol(tstr2, (char **)NULL, 16);
188rom_entries[romimages].included = 0;
189romimages++;
190}
191
192for (i = 0; i < romimages; i++) {
193msg_gdbg("romlayout %08x - %08x named %s\n",
194 rom_entries[i].start,
195 rom_entries[i].end, rom_entries[i].name);
196}
197
198fclose(romlayout);
199
200return 0;
201}
202#endif
203
204/* register an include argument (-i) for later processing */
205int register_include_arg(char *name)
206{
207if (num_include_args >= MAX_ROMLAYOUT) {
208msg_gerr("Too many regions included (%i).\n", num_include_args);
209return 1;
210}
211
212if (name == NULL) {
213msg_gerr("<NULL> is a bad region name.\n");
214return 1;
215}
216
217include_args[num_include_args] = name;
218num_include_args++;
219return 0;
220}
221
222/* returns the index of the entry (or a negative value if it is not found) */
223static int find_romentry(char *name)
224{
225int i;
226
227if (!romimages)
228return -1;
229
230msg_gspew("Looking for region \"%s\"... ", name);
231for (i = 0; i < romimages; i++) {
232if (!strcmp(rom_entries[i].name, name)) {
233rom_entries[i].included = 1;
234msg_gspew("found.\n");
235return i;
236}
237}
238msg_gspew("not found.\n");
239return -1;
240}
241
242/* process -i arguments
243 * returns 0 to indicate success, >0 to indicate failure
244 */
245int process_include_args(void)
246{
247int i;
248unsigned int found = 0;
249
250if (num_include_args == 0)
251return 0;
252
253for (i = 0; i < num_include_args; i++) {
254/* User has specified an area, but no layout file is loaded. */
255if (!romimages) {
256msg_gerr("Region requested (with -i \"%s\"), "
257 "but no layout data is available.\n",
258 include_args[i]);
259return 1;
260}
261
262if (find_romentry(include_args[i]) < 0) {
263msg_gerr("Invalid region specified: \"%s\"\n",
264 include_args[i]);
265return 1;
266}
267found++;
268}
269
270msg_ginfo("Using region%s: \"%s\"", num_include_args > 1 ? "s" : "",
271 include_args[0]);
272for (i = 1; i < num_include_args; i++)
273msg_ginfo(", \"%s\"", include_args[i]);
274msg_ginfo(".\n");
275return 0;
276}
277
278romlayout_t *get_next_included_romentry(unsigned int start)
279{
280int i;
281unsigned int best_start = UINT_MAX;
282romlayout_t *best_entry = NULL;
283romlayout_t *cur;
284
285/* First come, first serve for overlapping regions. */
286for (i = 0; i < romimages; i++) {
287cur = &rom_entries[i];
288if (!cur->included)
289continue;
290/* Already past the current entry? */
291if (start > cur->end)
292continue;
293/* Inside the current entry? */
294if (start >= cur->start)
295return cur;
296/* Entry begins after start. */
297if (best_start > cur->start) {
298best_start = cur->start;
299best_entry = cur;
300}
301}
302return best_entry;
303}
304
305int handle_romentries(struct flashctx *flash, uint8_t *oldcontents, uint8_t *newcontents)
306{
307unsigned int start = 0;
308romlayout_t *entry;
309unsigned int size = flash->total_size * 1024;
310
311/* If no regions were specified for inclusion, assume
312 * that the user wants to write the complete new image.
313 */
314if (num_include_args == 0)
315return 0;
316
317/* Non-included romentries are ignored.
318 * The union of all included romentries is used from the new image.
319 */
320while (start < size) {
321entry = get_next_included_romentry(start);
322/* No more romentries for remaining region? */
323if (!entry) {
324memcpy(newcontents + start, oldcontents + start,
325 size - start);
326break;
327}
328/* For non-included region, copy from old content. */
329if (entry->start > start)
330memcpy(newcontents + start, oldcontents + start,
331 entry->start - start);
332/* Skip to location after current romentry. */
333start = entry->end + 1;
334/* Catch overflow. */
335if (!start)
336break;
337}
338return 0;
339}
340

Archive Download this file

Revision: HEAD