aa7aee2528804f48ffb317435ce662322d9bbf2c
[coreboot.git] / util / flashrom / 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 <stdint.h>
26 #include "flash.h"
27
28 char *mainboard_vendor = NULL;
29 char *mainboard_part = NULL;
30 int romimages = 0;
31
32 #define MAX_ROMLAYOUT   16
33
34 typedef struct {
35         unsigned int start;
36         unsigned int end;
37         unsigned int included;
38         char name[256];
39 } romlayout_t;
40
41 romlayout_t rom_entries[MAX_ROMLAYOUT];
42
43 static char *def_name = "DEFAULT";
44
45 int show_id(uint8_t *bios, int size, int force)
46 {
47         unsigned int *walk;
48
49         walk = (unsigned int *)(bios + size - 0x10);
50         walk--;
51
52         if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
53                 /* We might have an NVIDIA chipset BIOS which stores the ID
54                  * information at a different location.
55                  */
56                 walk = (unsigned int *)(bios + size - 0x80);
57                 walk--;
58         }
59
60         /*
61          * Check if coreboot last image size is 0 or not a multiple of 1k or
62          * bigger than the chip or if the pointers to vendor ID or mainboard ID
63          * are outside the image of if the start of ID strings are nonsensical
64          * (nonprintable and not \0).
65          */
66         if ((*walk) == 0 || ((*walk) & 0x3ff) != 0 || *walk > size ||
67                 *(walk - 1) > size || *(walk - 2) > size ||
68                 (!isprint((const char *)(bios + size - *(walk - 1))) &&
69                 ((const char *)(bios + size - *(walk - 1)))) ||
70                 (!isprint((const char *)(bios + size - *(walk - 2))) &&
71                 ((const char *)(bios + size - *(walk - 2))))) {
72                 printf("Flash image seems to be a legacy BIOS. Disabling checks.\n");
73                 mainboard_vendor = def_name;
74                 mainboard_part = def_name;
75                 return 0;
76         }
77
78         printf_debug("coreboot last image size "
79                      "(not ROM size) is %d bytes.\n", *walk);
80
81         walk--;
82         mainboard_part = strdup((const char *)(bios + size - *walk));
83         walk--;
84         mainboard_vendor = strdup((const char *)(bios + size - *walk));
85         printf_debug("Manufacturer: %s\n", mainboard_vendor);
86         printf_debug("Mainboard ID: %s\n", mainboard_part);
87
88         /*
89          * If lb_vendor is not set, the coreboot table was
90          * not found. Nor was -m VENDOR:PART specified.
91          */
92         if (!lb_vendor || !lb_part) {
93                 printf("Note: If the following flash access fails, "
94                        "try -m <vendor>:<mainboard>.\n");
95                 return 0;
96         }
97
98         /* These comparisons are case insensitive to make things
99          * a little less user^Werror prone. 
100          */
101         if (!strcasecmp(mainboard_vendor, lb_vendor) &&
102             !strcasecmp(mainboard_part, lb_part)) {
103                 printf_debug("This firmware image matches "
104                              "this motherboard.\n");
105         } else {
106                 if (force) {
107                         printf("WARNING: This firmware image does not "
108                                "seem to fit to this machine - forcing it.\n");
109                 } else {
110                         printf("ERROR: Your firmware image (%s:%s) does not "
111                                "appear to\n       be correct for the detected "
112                                "mainboard (%s:%s)\n\nOverride with --force if you "
113                                "are absolutely sure that you\nare using a correct "
114                                "image for this mainboard or override\nthe detected "
115                                "values with --mainboard <vendor>:<mainboard>.\n\n",
116                                mainboard_vendor, mainboard_part, lb_vendor,
117                                lb_part);
118                         exit(1);
119                 }
120         }
121
122         return 0;
123 }
124
125 int read_romlayout(char *name)
126 {
127         FILE *romlayout;
128         char tempstr[256];
129         int i;
130
131         romlayout = fopen(name, "r");
132
133         if (!romlayout) {
134                 fprintf(stderr, "ERROR: Could not open ROM layout (%s).\n",
135                         name);
136                 return -1;
137         }
138
139         while (!feof(romlayout)) {
140                 char *tstr1, *tstr2;
141                 fscanf(romlayout, "%s %s\n", tempstr,
142                        rom_entries[romimages].name);
143 #if 0
144                 // fscanf does not like arbitrary comments like that :( later
145                 if (tempstr[0] == '#') {
146                         continue;
147                 }
148 #endif
149                 tstr1 = strtok(tempstr, ":");
150                 tstr2 = strtok(NULL, ":");
151                 rom_entries[romimages].start = strtol(tstr1, (char **)NULL, 16);
152                 rom_entries[romimages].end = strtol(tstr2, (char **)NULL, 16);
153                 rom_entries[romimages].included = 0;
154                 romimages++;
155         }
156
157         for (i = 0; i < romimages; i++) {
158                 printf_debug("romlayout %08x - %08x named %s\n",
159                              rom_entries[i].start,
160                              rom_entries[i].end, rom_entries[i].name);
161         }
162
163         fclose(romlayout);
164
165         return 0;
166 }
167
168 int find_romentry(char *name)
169 {
170         int i;
171
172         if (!romimages)
173                 return -1;
174
175         printf("Looking for \"%s\"... ", name);
176
177         for (i = 0; i < romimages; i++) {
178                 if (!strcmp(rom_entries[i].name, name)) {
179                         rom_entries[i].included = 1;
180                         printf("found.\n");
181                         return i;
182                 }
183         }
184         printf("not found.\n"); // Not found. Error.
185
186         return -1;
187 }
188
189 int handle_romentries(uint8_t *buffer, uint8_t *content)
190 {
191         int i;
192
193         // This function does not safe flash write cycles.
194         // 
195         // Also it does not cope with overlapping rom layout
196         // sections. 
197         // example:
198         // 00000000:00008fff gfxrom
199         // 00009000:0003ffff normal
200         // 00040000:0007ffff fallback
201         // 00000000:0007ffff all
202         //
203         // If you'd specify -i all the included flag of all other
204         // sections is still 0, so no changes will be made to the
205         // flash. Same thing if you specify -i normal -i all only 
206         // normal will be updated and the rest will be kept.
207
208         for (i = 0; i < romimages; i++) {
209
210                 if (rom_entries[i].included)
211                         continue;
212
213                 memcpy(buffer + rom_entries[i].start,
214                        content + rom_entries[i].start,
215                        rom_entries[i].end - rom_entries[i].start);
216         }
217
218         return 0;
219 }