2005-11-26 22:55:36 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
2007-08-23 15:34:59 +02:00
|
|
|
#include "flash.h"
|
2005-11-26 22:55:36 +01:00
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
char *mainboard_vendor = NULL;
|
|
|
|
char *mainboard_part = NULL;
|
|
|
|
int romimages = 0;
|
2005-11-26 22:55:36 +01:00
|
|
|
|
|
|
|
extern int force;
|
|
|
|
|
|
|
|
#define MAX_ROMLAYOUT 16
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned int start;
|
|
|
|
unsigned int end;
|
|
|
|
unsigned int included;
|
|
|
|
char name[256];
|
|
|
|
} romlayout_t;
|
|
|
|
|
|
|
|
romlayout_t rom_entries[MAX_ROMLAYOUT];
|
|
|
|
|
|
|
|
static char *def_name = "DEFAULT";
|
|
|
|
|
|
|
|
int show_id(uint8_t *bios, int size)
|
|
|
|
{
|
|
|
|
unsigned int *walk;
|
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
walk = (unsigned int *)(bios + size - 0x10);
|
|
|
|
walk--;
|
2005-11-26 22:55:36 +01:00
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
|
2005-11-26 22:55:36 +01:00
|
|
|
/* We might have an Nvidia chipset bios
|
|
|
|
* which stores the id information at a
|
|
|
|
* different location.
|
|
|
|
*/
|
2007-05-09 12:17:44 +02:00
|
|
|
walk = (unsigned int *)(bios + size - 0x80);
|
|
|
|
walk--;
|
2005-11-26 22:55:36 +01:00
|
|
|
}
|
2007-05-09 12:17:44 +02:00
|
|
|
|
|
|
|
if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
|
2007-10-10 19:42:20 +02:00
|
|
|
printf("Flash image seems to be a legacy BIOS. Disabling checks.\n");
|
2007-05-09 12:17:44 +02:00
|
|
|
mainboard_vendor = def_name;
|
|
|
|
mainboard_part = def_name;
|
2005-11-26 22:55:36 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2007-05-09 12:17:44 +02:00
|
|
|
|
2005-12-18 19:40:46 +01:00
|
|
|
printf_debug("LinuxBIOS last image size "
|
2007-10-18 01:55:15 +02:00
|
|
|
"(not ROM size) is %d bytes.\n", *walk);
|
2007-05-09 12:17:44 +02:00
|
|
|
|
|
|
|
walk--;
|
|
|
|
mainboard_part = strdup((const char *)(bios + size - *walk));
|
|
|
|
walk--;
|
|
|
|
mainboard_vendor = strdup((const char *)(bios + size - *walk));
|
2007-10-18 01:55:15 +02:00
|
|
|
printf_debug("Manufacturer: %s\n", mainboard_vendor);
|
|
|
|
printf_debug("Mainboard ID: %s\n", mainboard_part);
|
2005-12-18 19:40:46 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If lb_vendor is not set, the linuxbios table was
|
|
|
|
* not found. Nor was -mVENDOR:PART specified
|
|
|
|
*/
|
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
if (!lb_vendor || !lb_part) {
|
2005-12-18 19:40:46 +01:00
|
|
|
printf("Note: If the following flash access fails, "
|
2007-10-18 01:55:15 +02:00
|
|
|
"you might need to specify -m <vendor>:<mainboard>.\n");
|
2005-12-18 19:40:46 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2007-05-09 12:17:44 +02:00
|
|
|
|
2005-12-18 17:41:10 +01:00
|
|
|
/* These comparisons are case insensitive to make things
|
|
|
|
* a little less user^Werror prone.
|
|
|
|
*/
|
2005-12-18 19:40:46 +01:00
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
if (!strcasecmp(mainboard_vendor, lb_vendor) &&
|
|
|
|
!strcasecmp(mainboard_part, lb_part)) {
|
2005-12-18 19:40:46 +01:00
|
|
|
printf_debug("This firmware image matches "
|
2007-10-10 19:42:20 +02:00
|
|
|
"this motherboard.\n");
|
2005-11-26 22:55:36 +01:00
|
|
|
} else {
|
2007-05-09 12:17:44 +02:00
|
|
|
if (force) {
|
2005-11-26 22:55:36 +01:00
|
|
|
printf("WARNING: This firmware image does not "
|
2007-10-10 19:42:20 +02:00
|
|
|
"seem to fit to this machine - forcing it.\n");
|
2005-11-26 22:55:36 +01:00
|
|
|
} else {
|
2005-12-18 19:40:46 +01:00
|
|
|
printf("ERROR: Your firmware image (%s:%s) does not "
|
2007-05-09 12:17:44 +02:00
|
|
|
"appear to\n be correct for the detected "
|
|
|
|
"mainboard (%s:%s)\n\nOverride with --force if you "
|
|
|
|
"are absolutely sure that you\nare using a correct "
|
|
|
|
"image for this mainboard or override\nthe detected "
|
2007-10-10 19:42:20 +02:00
|
|
|
"values with --mainboard <vendor>:<mainboard>.\n\n",
|
2007-05-09 12:17:44 +02:00
|
|
|
mainboard_vendor, mainboard_part, lb_vendor,
|
|
|
|
lb_part);
|
2005-11-26 22:55:36 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2007-05-09 12:17:44 +02:00
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
int read_romlayout(char *name)
|
2005-11-26 22:55:36 +01:00
|
|
|
{
|
|
|
|
FILE *romlayout;
|
|
|
|
char tempstr[256];
|
|
|
|
int i;
|
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
romlayout = fopen(name, "r");
|
|
|
|
|
|
|
|
if (!romlayout) {
|
2007-10-18 01:55:15 +02:00
|
|
|
fprintf(stderr, "ERROR: Could not open ROM layout (%s).\n",
|
2007-05-09 12:17:44 +02:00
|
|
|
name);
|
2005-11-26 22:55:36 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2007-05-09 12:17:44 +02:00
|
|
|
|
|
|
|
while (!feof(romlayout)) {
|
2005-11-26 22:55:36 +01:00
|
|
|
char *tstr1, *tstr2;
|
2007-05-09 12:17:44 +02:00
|
|
|
fscanf(romlayout, "%s %s\n", tempstr,
|
|
|
|
rom_entries[romimages].name);
|
2005-11-26 22:55:36 +01:00
|
|
|
#if 0
|
|
|
|
// fscanf does not like arbitrary comments like that :( later
|
2007-05-09 12:17:44 +02:00
|
|
|
if (tempstr[0] == '#') {
|
2005-11-26 22:55:36 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2007-05-09 12:17:44 +02:00
|
|
|
tstr1 = strtok(tempstr, ":");
|
|
|
|
tstr2 = strtok(NULL, ":");
|
|
|
|
rom_entries[romimages].start = strtol(tstr1, (char **)NULL, 16);
|
|
|
|
rom_entries[romimages].end = strtol(tstr2, (char **)NULL, 16);
|
|
|
|
rom_entries[romimages].included = 0;
|
2005-11-26 22:55:36 +01:00
|
|
|
romimages++;
|
|
|
|
}
|
2007-05-09 12:17:44 +02:00
|
|
|
|
|
|
|
for (i = 0; i < romimages; i++) {
|
|
|
|
printf_debug("romlayout %08x - %08x named %s\n",
|
|
|
|
rom_entries[i].start,
|
|
|
|
rom_entries[i].end, rom_entries[i].name);
|
2005-11-26 22:55:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(romlayout);
|
2007-08-23 18:08:21 +02:00
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
return 0;
|
2005-11-26 22:55:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int find_romentry(char *name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
if (!romimages)
|
|
|
|
return -1;
|
2005-11-26 22:55:36 +01:00
|
|
|
|
|
|
|
printf("Looking for \"%s\"... ", name);
|
2007-05-09 12:17:44 +02:00
|
|
|
|
|
|
|
for (i = 0; i < romimages; i++) {
|
|
|
|
if (!strcmp(rom_entries[i].name, name)) {
|
|
|
|
rom_entries[i].included = 1;
|
2007-10-10 19:42:20 +02:00
|
|
|
printf("found.\n");
|
2005-11-26 22:55:36 +01:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2007-10-18 01:55:15 +02:00
|
|
|
printf("not found.\n"); // Not found. Error.
|
2007-08-23 18:08:21 +02:00
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int handle_romentries(uint8_t *buffer, uint8_t *content)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// This function does not safe flash write cycles.
|
|
|
|
//
|
|
|
|
// Also it does not cope with overlapping rom layout
|
|
|
|
// sections.
|
|
|
|
// example:
|
|
|
|
// 00000000:00008fff gfxrom
|
|
|
|
// 00009000:0003ffff normal
|
|
|
|
// 00040000:0007ffff fallback
|
|
|
|
// 00000000:0007ffff all
|
|
|
|
//
|
|
|
|
// If you'd specify -i all the included flag of all other
|
|
|
|
// sections is still 0, so no changes will be made to the
|
|
|
|
// flash. Same thing if you specify -i normal -i all only
|
|
|
|
// normal will be updated and the rest will be kept.
|
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
for (i = 0; i < romimages; i++) {
|
|
|
|
|
|
|
|
if (rom_entries[i].included)
|
2005-11-26 22:55:36 +01:00
|
|
|
continue;
|
2007-05-09 12:17:44 +02:00
|
|
|
|
|
|
|
memcpy(buffer + rom_entries[i].start,
|
|
|
|
content + rom_entries[i].start,
|
|
|
|
rom_entries[i].end - rom_entries[i].start);
|
2005-11-26 22:55:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|