blob: 5478782f42f3428c1f33a3a5e00469c955e19c93 [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
9char * mainboard_vendor=NULL;
10char * mainboard_part=NULL;
11int romimages=0;
12
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
28
29int show_id(uint8_t *bios, int size)
30{
31 unsigned int *walk;
32
33
34 walk=(unsigned int *)(bios+size-0x10);
35 walk--;
36
37 if((*walk)==0 || ((*walk)&0x3ff) != 0) {
38 /* We might have an Nvidia chipset bios
39 * which stores the id information at a
40 * different location.
41 */
42 walk=(unsigned int *)(bios+size-0x80);
43 walk--;
44 }
45
46 if((*walk)==0 || ((*walk)&0x3ff) != 0) {
47 printf("Flash image seems to be a legacy BIOS. Disabling checks.\n");
48 mainboard_vendor=def_name;
49 mainboard_part=def_name;
50 return 0;
51 }
52
Stefan Reinauer3a431602005-12-18 18:40:46 +000053 printf_debug("LinuxBIOS last image size "
54 "(not rom size) is %d bytes.\n", *walk);
Ollie Lho184a4042005-11-26 21:55:36 +000055
56 walk--; mainboard_part=strdup((const char *)(bios+size-*walk));
57 walk--; 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);
60
61
62 /*
63 * If lb_vendor is not set, the linuxbios table was
64 * not found. Nor was -mVENDOR:PART specified
65 */
66
67 if(!lb_vendor || !lb_part) {
68 printf("Note: If the following flash access fails, "
69 "you might need to specify -m <vendor>:<mainboard>\n");
70 return 0;
71 }
Stefan Reinauere3705282005-12-18 16:41:10 +000072
73 /* These comparisons are case insensitive to make things
74 * a little less user^Werror prone.
75 */
Stefan Reinauer3a431602005-12-18 18:40:46 +000076
77 if(!strcasecmp(mainboard_vendor, lb_vendor) &&
78 !strcasecmp(mainboard_part, lb_part)) {
79 printf_debug("This firmware image matches "
80 "this motherboard.\n");
Ollie Lho184a4042005-11-26 21:55:36 +000081 } else {
82 if(force) {
83 printf("WARNING: This firmware image does not "
Stefan Reinauer3a431602005-12-18 18:40:46 +000084 "seem to fit to this machine - forcing it.\n");
Ollie Lho184a4042005-11-26 21:55:36 +000085 } else {
Stefan Reinauer3a431602005-12-18 18:40:46 +000086 printf("ERROR: Your firmware image (%s:%s) does not "
87 "appear to\n be correct for the detected "
88 "mainboard (%s:%s)\n\nOverride with --force if you "
89 "are absolutely sure that you\nare using a correct "
90 "image for this mainboard or override\nthe detected "
91 "values with --mainboard <vendor>:<mainboard>.\n\n",
92 mainboard_vendor, mainboard_part, lb_vendor, lb_part);
Ollie Lho184a4042005-11-26 21:55:36 +000093 exit(1);
94 }
95 }
96
97 return 0;
98}
99
100int read_romlayout(char *name)
101{
102 FILE *romlayout;
103 char tempstr[256];
104 int i;
105
106 romlayout=fopen (name, "r");
107
108 if(!romlayout) {
Stefan Reinauer3a431602005-12-18 18:40:46 +0000109 printf("Error while opening rom layout (%s).\n", name);
Ollie Lho184a4042005-11-26 21:55:36 +0000110 return -1;
111 }
112
113 while(!feof(romlayout)) {
114 char *tstr1, *tstr2;
115 fscanf(romlayout,"%s %s\n", tempstr, rom_entries[romimages].name);
116#if 0
117 // fscanf does not like arbitrary comments like that :( later
118 if (tempstr[0]=='#') {
119 continue;
120 }
121#endif
122 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;
127 romimages++;
128 }
129
130 for(i=0; i<romimages; i++) {
Stefan Reinauer3a431602005-12-18 18:40:46 +0000131 printf_debug("romlayout %08x - %08x named %s\n",
Ollie Lho184a4042005-11-26 21:55:36 +0000132 rom_entries[i].start,
133 rom_entries[i].end,
134 rom_entries[i].name);
135 }
136
137 fclose(romlayout);
138 return 0;
139}
140
141int find_romentry(char *name)
142{
143 int i;
144
145 if(!romimages) return -1;
146
147 printf("Looking for \"%s\"... ", name);
148
149 for (i=0; i<romimages; i++) {
150 if(!strcmp(rom_entries[i].name, name)) {
151 rom_entries[i].included=1;
152 printf("found.\n");
153 return i;
154 }
155 }
156 printf("not found.\n");
157 // Not found. Error.
158 return -1;
159}
160
161int handle_romentries(uint8_t *buffer, uint8_t *content)
162{
163 int i;
164
165 // This function does not safe flash write cycles.
166 //
167 // Also it does not cope with overlapping rom layout
168 // sections.
169 // example:
170 // 00000000:00008fff gfxrom
171 // 00009000:0003ffff normal
172 // 00040000:0007ffff fallback
173 // 00000000:0007ffff all
174 //
175 // If you'd specify -i all the included flag of all other
176 // sections is still 0, so no changes will be made to the
177 // flash. Same thing if you specify -i normal -i all only
178 // normal will be updated and the rest will be kept.
179
180
181 for (i=0; i<romimages; i++) {
182
183 if (rom_entries[i].included)
184 continue;
185
186 memcpy (buffer+rom_entries[i].start,
187 content+rom_entries[i].start,
188 rom_entries[i].end-rom_entries[i].start);
189 }
190
191 return 0;
192}