blob: 6f87dd6fe162bf94c6f7c8cf1fa62818f9f70660 [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 Reinauer3a431602005-12-18 18:40:46 +000049 printf_debug("LinuxBIOS last image size "
Uwe Hermannac309342007-10-10 17:42:20 +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 Hermannac309342007-10-10 17:42:20 +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 /*
60 * If lb_vendor is not set, the linuxbios table was
61 * 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 Hermanna7e05482007-05-09 10:17:44 +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 Hermannac309342007-10-10 17:42:20 +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 Hermannac309342007-10-10 17:42:20 +0000157 printf("not found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000158 // Not found. Error.
Uwe Hermannffec5f32007-08-23 16:08:21 +0000159
Ollie Lho184a4042005-11-26 21:55:36 +0000160 return -1;
161}
162
163int handle_romentries(uint8_t *buffer, uint8_t *content)
164{
165 int i;
166
167 // This function does not safe flash write cycles.
168 //
169 // Also it does not cope with overlapping rom layout
170 // sections.
171 // example:
172 // 00000000:00008fff gfxrom
173 // 00009000:0003ffff normal
174 // 00040000:0007ffff fallback
175 // 00000000:0007ffff all
176 //
177 // If you'd specify -i all the included flag of all other
178 // sections is still 0, so no changes will be made to the
179 // flash. Same thing if you specify -i normal -i all only
180 // normal will be updated and the rest will be kept.
181
Uwe Hermanna7e05482007-05-09 10:17:44 +0000182 for (i = 0; i < romimages; i++) {
183
184 if (rom_entries[i].included)
Ollie Lho184a4042005-11-26 21:55:36 +0000185 continue;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000186
187 memcpy(buffer + rom_entries[i].start,
188 content + rom_entries[i].start,
189 rom_entries[i].end - rom_entries[i].start);
Ollie Lho184a4042005-11-26 21:55:36 +0000190 }
191
192 return 0;
193}