blob: 6dbbd9c0d70dd1e9add6a7078346cd1e99fdd55d [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>
5#include "layout.h"
6#include "lbtable.h"
7#include "debug.h"
8
Uwe Hermanna7e05482007-05-09 10:17:44 +00009char *mainboard_vendor = NULL;
10char *mainboard_part = NULL;
11int romimages = 0;
Ollie Lho184a4042005-11-26 21:55:36 +000012
13extern int force;
14
15#define MAX_ROMLAYOUT 16
16
17typedef struct {
18 unsigned int start;
19 unsigned int end;
20 unsigned int included;
21 char name[256];
22} romlayout_t;
23
24romlayout_t rom_entries[MAX_ROMLAYOUT];
25
26static char *def_name = "DEFAULT";
27
Ollie Lho184a4042005-11-26 21:55:36 +000028int show_id(uint8_t *bios, int size)
29{
30 unsigned int *walk;
31
Uwe Hermanna7e05482007-05-09 10:17:44 +000032 walk = (unsigned int *)(bios + size - 0x10);
33 walk--;
Ollie Lho184a4042005-11-26 21:55:36 +000034
Uwe Hermanna7e05482007-05-09 10:17:44 +000035 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
Ollie Lho184a4042005-11-26 21:55:36 +000036 /* We might have an Nvidia chipset bios
37 * which stores the id information at a
38 * different location.
39 */
Uwe Hermanna7e05482007-05-09 10:17:44 +000040 walk = (unsigned int *)(bios + size - 0x80);
41 walk--;
Ollie Lho184a4042005-11-26 21:55:36 +000042 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000043
44 if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
Ollie Lho184a4042005-11-26 21:55:36 +000045 printf("Flash image seems to be a legacy BIOS. Disabling checks.\n");
Uwe Hermanna7e05482007-05-09 10:17:44 +000046 mainboard_vendor = def_name;
47 mainboard_part = def_name;
Ollie Lho184a4042005-11-26 21:55:36 +000048 return 0;
49 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000050
Stefan Reinauer3a431602005-12-18 18:40:46 +000051 printf_debug("LinuxBIOS last image size "
52 "(not rom size) is %d bytes.\n", *walk);
Uwe Hermanna7e05482007-05-09 10:17:44 +000053
54 walk--;
55 mainboard_part = strdup((const char *)(bios + size - *walk));
56 walk--;
57 mainboard_vendor = strdup((const char *)(bios + size - *walk));
Stefan Reinauer3a431602005-12-18 18:40:46 +000058 printf_debug("MANUFACTURER: %s\n", mainboard_vendor);
59 printf_debug("MAINBOARD ID: %s\n", mainboard_part);
Stefan Reinauer3a431602005-12-18 18:40:46 +000060
61 /*
62 * If lb_vendor is not set, the linuxbios table was
63 * not found. Nor was -mVENDOR:PART specified
64 */
65
Uwe Hermanna7e05482007-05-09 10:17:44 +000066 if (!lb_vendor || !lb_part) {
Stefan Reinauer3a431602005-12-18 18:40:46 +000067 printf("Note: If the following flash access fails, "
Uwe Hermanna7e05482007-05-09 10:17:44 +000068 "you might need to specify -m <vendor>:<mainboard>\n");
Stefan Reinauer3a431602005-12-18 18:40:46 +000069 return 0;
70 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000071
Stefan Reinauere3705282005-12-18 16:41:10 +000072 /* These comparisons are case insensitive to make things
73 * a little less user^Werror prone.
74 */
Stefan Reinauer3a431602005-12-18 18:40:46 +000075
Uwe Hermanna7e05482007-05-09 10:17:44 +000076 if (!strcasecmp(mainboard_vendor, lb_vendor) &&
77 !strcasecmp(mainboard_part, lb_part)) {
Stefan Reinauer3a431602005-12-18 18:40:46 +000078 printf_debug("This firmware image matches "
79 "this motherboard.\n");
Ollie Lho184a4042005-11-26 21:55:36 +000080 } else {
Uwe Hermanna7e05482007-05-09 10:17:44 +000081 if (force) {
Ollie Lho184a4042005-11-26 21:55:36 +000082 printf("WARNING: This firmware image does not "
Uwe Hermanna7e05482007-05-09 10:17:44 +000083 "seem to fit to this machine - forcing it.\n");
Ollie Lho184a4042005-11-26 21:55:36 +000084 } else {
Stefan Reinauer3a431602005-12-18 18:40:46 +000085 printf("ERROR: Your firmware image (%s:%s) does not "
Uwe Hermanna7e05482007-05-09 10:17:44 +000086 "appear to\n be correct for the detected "
87 "mainboard (%s:%s)\n\nOverride with --force if you "
88 "are absolutely sure that you\nare using a correct "
89 "image for this mainboard or override\nthe detected "
90 "values with --mainboard <vendor>:<mainboard>.\n\n",
91 mainboard_vendor, mainboard_part, lb_vendor,
92 lb_part);
Ollie Lho184a4042005-11-26 21:55:36 +000093 exit(1);
94 }
95 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000096
Ollie Lho184a4042005-11-26 21:55:36 +000097 return 0;
98}
99
Uwe Hermanna7e05482007-05-09 10:17:44 +0000100int read_romlayout(char *name)
Ollie Lho184a4042005-11-26 21:55:36 +0000101{
102 FILE *romlayout;
103 char tempstr[256];
104 int i;
105
Uwe Hermanna7e05482007-05-09 10:17:44 +0000106 romlayout = fopen(name, "r");
107
108 if (!romlayout) {
109 fprintf(stderr, "ERROR: Could not open rom layout (%s).\n",
110 name);
Ollie Lho184a4042005-11-26 21:55:36 +0000111 return -1;
112 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000113
114 while (!feof(romlayout)) {
Ollie Lho184a4042005-11-26 21:55:36 +0000115 char *tstr1, *tstr2;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000116 fscanf(romlayout, "%s %s\n", tempstr,
117 rom_entries[romimages].name);
Ollie Lho184a4042005-11-26 21:55:36 +0000118#if 0
119 // fscanf does not like arbitrary comments like that :( later
Uwe Hermanna7e05482007-05-09 10:17:44 +0000120 if (tempstr[0] == '#') {
Ollie Lho184a4042005-11-26 21:55:36 +0000121 continue;
122 }
123#endif
Uwe Hermanna7e05482007-05-09 10:17:44 +0000124 tstr1 = strtok(tempstr, ":");
125 tstr2 = strtok(NULL, ":");
126 rom_entries[romimages].start = strtol(tstr1, (char **)NULL, 16);
127 rom_entries[romimages].end = strtol(tstr2, (char **)NULL, 16);
128 rom_entries[romimages].included = 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000129 romimages++;
130 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000131
132 for (i = 0; i < romimages; i++) {
133 printf_debug("romlayout %08x - %08x named %s\n",
134 rom_entries[i].start,
135 rom_entries[i].end, rom_entries[i].name);
Ollie Lho184a4042005-11-26 21:55:36 +0000136 }
137
138 fclose(romlayout);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000139 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000140}
141
142int find_romentry(char *name)
143{
144 int i;
145
Uwe Hermanna7e05482007-05-09 10:17:44 +0000146 if (!romimages)
147 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000148
149 printf("Looking for \"%s\"... ", name);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000150
151 for (i = 0; i < romimages; i++) {
152 if (!strcmp(rom_entries[i].name, name)) {
153 rom_entries[i].included = 1;
Ollie Lho184a4042005-11-26 21:55:36 +0000154 printf("found.\n");
155 return i;
156 }
157 }
158 printf("not found.\n");
159 // Not found. Error.
160 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}