blob: 22190962469da72f8bbdd0378ba560102e086612 [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
53 printf("LinuxBIOS image size=%d\n", *walk);
54
55 walk--; mainboard_part=strdup((const char *)(bios+size-*walk));
56 walk--; mainboard_vendor=strdup((const char *)(bios+size-*walk));
57 printf("MANUFACTURER: %s\n", mainboard_vendor);
58 printf("MAINBOARD ID: %s\n", mainboard_part);
59 if(lb_vendor && !strcmp(mainboard_vendor, lb_vendor) &&
60 lb_part && !strcmp(mainboard_part, lb_part)) {
61 printf ("This firmware image matches "
62 "this motherboard.\n");
63 } else {
64 if(force) {
65 printf("WARNING: This firmware image does not "
66 "fit to this machine - forcing it.\n");
67 } else {
68 printf("ERROR: This firmware image does not "
69 "fit to this machine\nOverride with -m if"
70 "you know exactly what you are doing.\n");
71 exit(1);
72 }
73 }
74
75 return 0;
76}
77
78int read_romlayout(char *name)
79{
80 FILE *romlayout;
81 char tempstr[256];
82 int i;
83
84 romlayout=fopen (name, "r");
85
86 if(!romlayout) {
87 printf("Error while opening rom layout.\n");
88 return -1;
89 }
90
91 while(!feof(romlayout)) {
92 char *tstr1, *tstr2;
93 fscanf(romlayout,"%s %s\n", tempstr, rom_entries[romimages].name);
94#if 0
95 // fscanf does not like arbitrary comments like that :( later
96 if (tempstr[0]=='#') {
97 continue;
98 }
99#endif
100 tstr1=strtok(tempstr,":");
101 tstr2=strtok(NULL,":");
102 rom_entries[romimages].start=strtol(tstr1, (char **)NULL, 16);
103 rom_entries[romimages].end=strtol(tstr2, (char **)NULL, 16);
104 rom_entries[romimages].included=0;
105 romimages++;
106 }
107
108 for(i=0; i<romimages; i++) {
109 printf("romlayout %08x - %08x named %s\n",
110 rom_entries[i].start,
111 rom_entries[i].end,
112 rom_entries[i].name);
113 }
114
115 fclose(romlayout);
116 return 0;
117}
118
119int find_romentry(char *name)
120{
121 int i;
122
123 if(!romimages) return -1;
124
125 printf("Looking for \"%s\"... ", name);
126
127 for (i=0; i<romimages; i++) {
128 if(!strcmp(rom_entries[i].name, name)) {
129 rom_entries[i].included=1;
130 printf("found.\n");
131 return i;
132 }
133 }
134 printf("not found.\n");
135 // Not found. Error.
136 return -1;
137}
138
139int handle_romentries(uint8_t *buffer, uint8_t *content)
140{
141 int i;
142
143 // This function does not safe flash write cycles.
144 //
145 // Also it does not cope with overlapping rom layout
146 // sections.
147 // example:
148 // 00000000:00008fff gfxrom
149 // 00009000:0003ffff normal
150 // 00040000:0007ffff fallback
151 // 00000000:0007ffff all
152 //
153 // If you'd specify -i all the included flag of all other
154 // sections is still 0, so no changes will be made to the
155 // flash. Same thing if you specify -i normal -i all only
156 // normal will be updated and the rest will be kept.
157
158
159 for (i=0; i<romimages; i++) {
160
161 if (rom_entries[i].included)
162 continue;
163
164 memcpy (buffer+rom_entries[i].start,
165 content+rom_entries[i].start,
166 rom_entries[i].end-rom_entries[i].start);
167 }
168
169 return 0;
170}