source: trunk/layout.c @ 1523

Revision 1523, 9.1 KB checked in by stefanct, 5 weeks ago (diff)

Check for duplicate -i arguments.

And a tiny cleanup.

Signed-off-by: Stefan Tauner <stefan.tauner@…>
Acked-by: Uwe Hermann <uwe@…>

Line 
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_ROMLAYOUT   32
36
37typedef struct {
38        unsigned int start;
39        unsigned int end;
40        unsigned int included;
41        char 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{
57        unsigned int *walk;
58        unsigned int mb_part_offset, mb_vendor_offset;
59        char *mb_part, *mb_vendor;
60
61        mainboard_vendor = def_name;
62        mainboard_part = def_name;
63
64        walk = (unsigned int *)(bios + size - 0x10);
65        walk--;
66
67        if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
68                /* We might have an NVIDIA chipset BIOS which stores the ID
69                 * information at a different location.
70                 */
71                walk = (unsigned int *)(bios + size - 0x80);
72                walk--;
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         */
81        mb_part_offset = *(walk - 1);
82        mb_vendor_offset = *(walk - 2);
83        if ((*walk) == 0 || ((*walk) & 0x3ff) != 0 || (*walk) > size ||
84            mb_part_offset > size || mb_vendor_offset > size) {
85                msg_pinfo("Flash image seems to be a legacy BIOS. "
86                          "Disabling coreboot-related checks.\n");
87                return 0;
88        }
89
90        mb_part = (char *)(bios + size - mb_part_offset);
91        mb_vendor = (char *)(bios + size - mb_vendor_offset);
92        if (!isprint((unsigned char)*mb_part) ||
93            !isprint((unsigned char)*mb_vendor)) {
94                msg_pinfo("Flash image seems to have garbage in the ID location."
95                       " Disabling checks.\n");
96                return 0;
97        }
98
99        msg_pdbg("coreboot last image size "
100                     "(not ROM size) is %d bytes.\n", *walk);
101
102        mainboard_part = strdup(mb_part);
103        mainboard_vendor = strdup(mb_vendor);
104        msg_pdbg("Manufacturer: %s\n", mainboard_vendor);
105        msg_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         */
111        if (!lb_vendor || !lb_part) {
112                msg_pinfo("Note: If the following flash access fails, try "
113                          "-p internal:mainboard=<vendor>:<mainboard>.\n");
114                return 0;
115        }
116
117        /* These comparisons are case insensitive to make things
118         * a little less user^Werror prone.
119         */
120        if (!strcasecmp(mainboard_vendor, lb_vendor) &&
121            !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 "
126                               "seem to fit to this machine - forcing it.\n");
127                } else {
128                        msg_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);
140                        exit(1);
141                }
142        }
143
144        return 0;
145}
146#endif
147
148#ifndef __LIBPAYLOAD__
149int read_romlayout(char *name)
150{
151        FILE *romlayout;
152        char tempstr[256];
153        int i;
154
155        romlayout = fopen(name, "r");
156
157        if (!romlayout) {
158                msg_gerr("ERROR: Could not open ROM layout (%s).\n",
159                        name);
160                return -1;
161        }
162
163        while (!feof(romlayout)) {
164                char *tstr1, *tstr2;
165
166                if (romimages >= MAX_ROMLAYOUT) {
167                        msg_gerr("Maximum number of ROM images (%i) in layout "
168                                 "file reached.\n", MAX_ROMLAYOUT);
169                        return 1;
170                }
171                if (2 != fscanf(romlayout, "%s %s\n", tempstr, rom_entries[romimages].name))
172                        continue;
173#if 0
174                // fscanf does not like arbitrary comments like that :( later
175                if (tempstr[0] == '#') {
176                        continue;
177                }
178#endif
179                tstr1 = strtok(tempstr, ":");
180                tstr2 = strtok(NULL, ":");
181                if (!tstr1 || !tstr2) {
182                        msg_gerr("Error parsing layout file.\n");
183                        fclose(romlayout);
184                        return 1;
185                }
186                rom_entries[romimages].start = strtol(tstr1, (char **)NULL, 16);
187                rom_entries[romimages].end = strtol(tstr2, (char **)NULL, 16);
188                rom_entries[romimages].included = 0;
189                romimages++;
190        }
191
192        for (i = 0; i < romimages; i++) {
193                msg_gdbg("romlayout %08x - %08x named %s\n",
194                             rom_entries[i].start,
195                             rom_entries[i].end, rom_entries[i].name);
196        }
197
198        fclose(romlayout);
199
200        return 0;
201}
202#endif
203
204/* returns the index of the entry (or a negative value if it is not found) */
205int find_include_arg(const char *const name)
206{
207        unsigned int i;
208        for (i = 0; i < num_include_args; i++) {
209                if (!strcmp(include_args[i], name))
210                        return i;
211        }
212        return -1;
213}
214
215/* register an include argument (-i) for later processing */
216int register_include_arg(char *name)
217{
218        if (num_include_args >= MAX_ROMLAYOUT) {
219                msg_gerr("Too many regions included (%i).\n", num_include_args);
220                return 1;
221        }
222
223        if (name == NULL) {
224                msg_gerr("<NULL> is a bad region name.\n");
225                return 1;
226        }
227
228        if (find_include_arg(name) != -1) {
229                msg_gerr("Duplicate region name: \"%s\".\n", name);
230                return 1;
231        }
232
233        include_args[num_include_args] = name;
234        num_include_args++;
235        return 0;
236}
237
238/* returns the index of the entry (or a negative value if it is not found) */
239static int find_romentry(char *name)
240{
241        int i;
242
243        if (!romimages)
244                return -1;
245
246        msg_gspew("Looking for region \"%s\"... ", name);
247        for (i = 0; i < romimages; i++) {
248                if (!strcmp(rom_entries[i].name, name)) {
249                        rom_entries[i].included = 1;
250                        msg_gspew("found.\n");
251                        return i;
252                }
253        }
254        msg_gspew("not found.\n");
255        return -1;
256}
257
258/* process -i arguments
259 * returns 0 to indicate success, >0 to indicate failure
260 */
261int process_include_args(void)
262{
263        int i;
264        unsigned int found = 0;
265
266        if (num_include_args == 0)
267                return 0;
268
269        /* User has specified an area, but no layout file is loaded. */
270        if (!romimages) {
271                msg_gerr("Region requested (with -i \"%s\"), "
272                         "but no layout data is available.\n",
273                         include_args[0]);
274                return 1;
275        }
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                                 include_args[i]);
281                        return 1;
282                }
283                found++;
284        }
285
286        msg_ginfo("Using region%s: \"%s\"", num_include_args > 1 ? "s" : "",
287                  include_args[0]);
288        for (i = 1; i < num_include_args; i++)
289                msg_ginfo(", \"%s\"", include_args[i]);
290        msg_ginfo(".\n");
291        return 0;
292}
293
294romlayout_t *get_next_included_romentry(unsigned int start)
295{
296        int i;
297        unsigned int best_start = UINT_MAX;
298        romlayout_t *best_entry = NULL;
299        romlayout_t *cur;
300
301        /* First come, first serve for overlapping regions. */
302        for (i = 0; i < romimages; i++) {
303                cur = &rom_entries[i];
304                if (!cur->included)
305                        continue;
306                /* Already past the current entry? */
307                if (start > cur->end)
308                        continue;
309                /* Inside the current entry? */
310                if (start >= cur->start)
311                        return cur;
312                /* Entry begins after start. */
313                if (best_start > cur->start) {
314                        best_start = cur->start;
315                        best_entry = cur;
316                }
317        }
318        return best_entry;
319}
320
321int 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        /* If no regions were specified for inclusion, assume
328         * that the user wants to write the complete new image.
329         */
330        if (num_include_args == 0)
331                return 0;
332
333        /* Non-included romentries are ignored.
334         * The union of all included romentries is used from the new image.
335         */
336        while (start < size) {
337                entry = get_next_included_romentry(start);
338                /* No more romentries for remaining region? */
339                if (!entry) {
340                        memcpy(newcontents + start, oldcontents + start,
341                               size - start);
342                        break;
343                }
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                /* Catch overflow. */
351                if (!start)
352                        break;
353        }
354        return 0;
355}
Note: See TracBrowser for help on using the repository browser.