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