blob: 3d24b25af609c216e1e9a2e9a7b09b1e36e3c6d0 [file] [log] [blame]
Ollie Lho184a4042005-11-26 21:55:36 +00001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <stdint.h>
Uwe Hermann0846f892007-08-23 13:34:59 +00005#include "flash.h"
Ollie Lho184a4042005-11-26 21:55:36 +00006
Uwe Hermanna7e05482007-05-09 10:17:44 +00007char *mainboard_vendor = NULL;
8char *mainboard_part = NULL;
9int romimages = 0;
Ollie Lho184a4042005-11-26 21:55:36 +000010
11extern int force;
12
13#define MAX_ROMLAYOUT 16
14
15typedef struct {
16 unsigned int start;
17 unsigned int end;
18 unsigned int included;
19 char name[256];
20} romlayout_t;
21
22romlayout_t rom_entries[MAX_ROMLAYOUT];
23
24static char *def_name = "DEFAULT";
25
Ollie Lho184a4042005-11-26 21:55:36 +000026int show_id(uint8_t *bios, int size)
27{
28 unsigned int *walk;
29
Uwe Hermanna7e05482007-05-09 10:17:44 +000030 walk = (unsigned int *)(bios + size - 0x10);
31 walk--;
Ollie Lho184a4042005-11-26 21:55:36 +000032
Uwe Hermanna7e05482007-05-09 10:17:44 +000033 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
Ollie Lho184a4042005-11-26 21:55:36 +000034 /* We might have an Nvidia chipset bios
35 * which stores the id information at a
36 * different location.
37 */
Uwe Hermanna7e05482007-05-09 10:17:44 +000038 walk = (unsigned int *)(bios + size - 0x80);
39 walk--;
Ollie Lho184a4042005-11-26 21:55:36 +000040 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000041
42 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
Uwe Hermannac309342007-10-10 17:42:20 +000043 printf("Flash image seems to be a legacy BIOS. Disabling checks.\n");
Uwe Hermanna7e05482007-05-09 10:17:44 +000044 mainboard_vendor = def_name;
45 mainboard_part = def_name;
Ollie Lho184a4042005-11-26 21:55:36 +000046 return 0;
47 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000048
Stefan Reinauere3f3e2e2008-01-18 15:33:10 +000049 printf_debug("coreboot last image size "
Uwe Hermanna502dce2007-10-17 23:55:15 +000050 "(not ROM size) is %d bytes.\n", *walk);
Uwe Hermanna7e05482007-05-09 10:17:44 +000051
52 walk--;
53 mainboard_part = strdup((const char *)(bios + size - *walk));
54 walk--;
55 mainboard_vendor = strdup((const char *)(bios + size - *walk));
Uwe Hermanna502dce2007-10-17 23:55:15 +000056 printf_debug("Manufacturer: %s\n", mainboard_vendor);
57 printf_debug("Mainboard ID: %s\n", mainboard_part);
Stefan Reinauer3a431602005-12-18 18:40:46 +000058
59 /*
Stefan Reinauere3f3e2e2008-01-18 15:33:10 +000060 * If lb_vendor is not set, the coreboot table was
Stefan Reinauer3a431602005-12-18 18:40:46 +000061 * not found. Nor was -mVENDOR:PART specified
62 */
63
Uwe Hermanna7e05482007-05-09 10:17:44 +000064 if (!lb_vendor || !lb_part) {
Stefan Reinauer3a431602005-12-18 18:40:46 +000065 printf("Note: If the following flash access fails, "
Uwe Hermanna502dce2007-10-17 23:55:15 +000066 "you might need to specify -m <vendor>:<mainboard>.\n");
Stefan Reinauer3a431602005-12-18 18:40:46 +000067 return 0;
68 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000069
Stefan Reinauere3705282005-12-18 16:41:10 +000070 /* These comparisons are case insensitive to make things
71 * a little less user^Werror prone.
72 */
Stefan Reinauer3a431602005-12-18 18:40:46 +000073
Uwe Hermanna7e05482007-05-09 10:17:44 +000074 if (!strcasecmp(mainboard_vendor, lb_vendor) &&
75 !strcasecmp(mainboard_part, lb_part)) {
Stefan Reinauer3a431602005-12-18 18:40:46 +000076 printf_debug("This firmware image matches "
Uwe Hermannac309342007-10-10 17:42:20 +000077 "this motherboard.\n");
Ollie Lho184a4042005-11-26 21:55:36 +000078 } else {
Uwe Hermanna7e05482007-05-09 10:17:44 +000079 if (force) {
Ollie Lho184a4042005-11-26 21:55:36 +000080 printf("WARNING: This firmware image does not "
Uwe Hermannac309342007-10-10 17:42:20 +000081 "seem to fit to this machine - forcing it.\n");
Ollie Lho184a4042005-11-26 21:55:36 +000082 } else {
Stefan Reinauer3a431602005-12-18 18:40:46 +000083 printf("ERROR: Your firmware image (%s:%s) does not "
Uwe Hermanna7e05482007-05-09 10:17:44 +000084 "appear to\n be correct for the detected "
85 "mainboard (%s:%s)\n\nOverride with --force if you "
86 "are absolutely sure that you\nare using a correct "
87 "image for this mainboard or override\nthe detected "
Uwe Hermannac309342007-10-10 17:42:20 +000088 "values with --mainboard <vendor>:<mainboard>.\n\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +000089 mainboard_vendor, mainboard_part, lb_vendor,
90 lb_part);
Ollie Lho184a4042005-11-26 21:55:36 +000091 exit(1);
92 }
93 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000094
Ollie Lho184a4042005-11-26 21:55:36 +000095 return 0;
96}
97
Uwe Hermanna7e05482007-05-09 10:17:44 +000098int read_romlayout(char *name)
Ollie Lho184a4042005-11-26 21:55:36 +000099{
100 FILE *romlayout;
101 char tempstr[256];
102 int i;
103
Uwe Hermanna7e05482007-05-09 10:17:44 +0000104 romlayout = fopen(name, "r");
105
106 if (!romlayout) {
Uwe Hermanna502dce2007-10-17 23:55:15 +0000107 fprintf(stderr, "ERROR: Could not open ROM layout (%s).\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000108 name);
Ollie Lho184a4042005-11-26 21:55:36 +0000109 return -1;
110 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000111
112 while (!feof(romlayout)) {
Ollie Lho184a4042005-11-26 21:55:36 +0000113 char *tstr1, *tstr2;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000114 fscanf(romlayout, "%s %s\n", tempstr,
115 rom_entries[romimages].name);
Ollie Lho184a4042005-11-26 21:55:36 +0000116#if 0
117 // fscanf does not like arbitrary comments like that :( later
Uwe Hermanna7e05482007-05-09 10:17:44 +0000118 if (tempstr[0] == '#') {
Ollie Lho184a4042005-11-26 21:55:36 +0000119 continue;
120 }
121#endif
Uwe Hermanna7e05482007-05-09 10:17:44 +0000122 tstr1 = strtok(tempstr, ":");
123 tstr2 = strtok(NULL, ":");
124 rom_entries[romimages].start = strtol(tstr1, (char **)NULL, 16);
125 rom_entries[romimages].end = strtol(tstr2, (char **)NULL, 16);
126 rom_entries[romimages].included = 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000127 romimages++;
128 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000129
130 for (i = 0; i < romimages; i++) {
131 printf_debug("romlayout %08x - %08x named %s\n",
132 rom_entries[i].start,
133 rom_entries[i].end, rom_entries[i].name);
Ollie Lho184a4042005-11-26 21:55:36 +0000134 }
135
136 fclose(romlayout);
Uwe Hermannffec5f32007-08-23 16:08:21 +0000137
Uwe Hermanna7e05482007-05-09 10:17:44 +0000138 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000139}
140
141int find_romentry(char *name)
142{
143 int i;
144
Uwe Hermanna7e05482007-05-09 10:17:44 +0000145 if (!romimages)
146 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000147
148 printf("Looking for \"%s\"... ", name);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000149
150 for (i = 0; i < romimages; i++) {
151 if (!strcmp(rom_entries[i].name, name)) {
152 rom_entries[i].included = 1;
Uwe Hermannac309342007-10-10 17:42:20 +0000153 printf("found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000154 return i;
155 }
156 }
Uwe Hermanna502dce2007-10-17 23:55:15 +0000157 printf("not found.\n"); // Not found. Error.
Uwe Hermannffec5f32007-08-23 16:08:21 +0000158
Ollie Lho184a4042005-11-26 21:55:36 +0000159 return -1;
160}
161
162int handle_romentries(uint8_t *buffer, uint8_t *content)
163{
164 int i;
165
166 // This function does not safe flash write cycles.
167 //
168 // Also it does not cope with overlapping rom layout
169 // sections.
170 // example:
171 // 00000000:00008fff gfxrom
172 // 00009000:0003ffff normal
173 // 00040000:0007ffff fallback
174 // 00000000:0007ffff all
175 //
176 // If you'd specify -i all the included flag of all other
177 // sections is still 0, so no changes will be made to the
178 // flash. Same thing if you specify -i normal -i all only
179 // normal will be updated and the rest will be kept.
180
Uwe Hermanna7e05482007-05-09 10:17:44 +0000181 for (i = 0; i < romimages; i++) {
182
183 if (rom_entries[i].included)
Ollie Lho184a4042005-11-26 21:55:36 +0000184 continue;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000185
186 memcpy(buffer + rom_entries[i].start,
187 content + rom_entries[i].start,
188 rom_entries[i].end - rom_entries[i].start);
Ollie Lho184a4042005-11-26 21:55:36 +0000189 }
190
191 return 0;
192}