blob: 8ba2e4153e85fb7c6b8ab1c8fe7a15551e4c3e07 [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 Reinauer0a05d672007-04-14 16:32:59 +0000109 fprintf(stderr, "ERROR: Could not open rom layout (%s).\n",
110 name);
Ollie Lho184a4042005-11-26 21:55:36 +0000111 return -1;
112 }
113
114 while(!feof(romlayout)) {
115 char *tstr1, *tstr2;
116 fscanf(romlayout,"%s %s\n", tempstr, rom_entries[romimages].name);
117#if 0
118 // fscanf does not like arbitrary comments like that :( later
119 if (tempstr[0]=='#') {
120 continue;
121 }
122#endif
123 tstr1=strtok(tempstr,":");
124 tstr2=strtok(NULL,":");
125 rom_entries[romimages].start=strtol(tstr1, (char **)NULL, 16);
126 rom_entries[romimages].end=strtol(tstr2, (char **)NULL, 16);
127 rom_entries[romimages].included=0;
128 romimages++;
129 }
130
131 for(i=0; i<romimages; i++) {
Stefan Reinauer3a431602005-12-18 18:40:46 +0000132 printf_debug("romlayout %08x - %08x named %s\n",
Ollie Lho184a4042005-11-26 21:55:36 +0000133 rom_entries[i].start,
134 rom_entries[i].end,
135 rom_entries[i].name);
136 }
137
138 fclose(romlayout);
139 return 0;
140}
141
142int find_romentry(char *name)
143{
144 int i;
145
146 if(!romimages) return -1;
147
148 printf("Looking for \"%s\"... ", name);
149
150 for (i=0; i<romimages; i++) {
151 if(!strcmp(rom_entries[i].name, name)) {
152 rom_entries[i].included=1;
153 printf("found.\n");
154 return i;
155 }
156 }
157 printf("not found.\n");
158 // Not found. Error.
159 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
181
182 for (i=0; i<romimages; i++) {
183
184 if (rom_entries[i].included)
185 continue;
186
187 memcpy (buffer+rom_entries[i].start,
188 content+rom_entries[i].start,
189 rom_entries[i].end-rom_entries[i].start);
190 }
191
192 return 0;
193}