Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1 | #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 | |
| 9 | char * mainboard_vendor=NULL; |
| 10 | char * mainboard_part=NULL; |
| 11 | int romimages=0; |
| 12 | |
| 13 | extern int force; |
| 14 | |
| 15 | #define MAX_ROMLAYOUT 16 |
| 16 | |
| 17 | typedef struct { |
| 18 | unsigned int start; |
| 19 | unsigned int end; |
| 20 | unsigned int included; |
| 21 | char name[256]; |
| 22 | } romlayout_t; |
| 23 | |
| 24 | romlayout_t rom_entries[MAX_ROMLAYOUT]; |
| 25 | |
| 26 | static char *def_name = "DEFAULT"; |
| 27 | |
| 28 | |
| 29 | int 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 Reinauer | 3a43160 | 2005-12-18 18:40:46 +0000 | [diff] [blame] | 53 | printf_debug("LinuxBIOS last image size " |
| 54 | "(not rom size) is %d bytes.\n", *walk); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 55 | |
| 56 | walk--; mainboard_part=strdup((const char *)(bios+size-*walk)); |
| 57 | walk--; mainboard_vendor=strdup((const char *)(bios+size-*walk)); |
Stefan Reinauer | 3a43160 | 2005-12-18 18:40:46 +0000 | [diff] [blame] | 58 | 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 Reinauer | e370528 | 2005-12-18 16:41:10 +0000 | [diff] [blame] | 72 | |
| 73 | /* These comparisons are case insensitive to make things |
| 74 | * a little less user^Werror prone. |
| 75 | */ |
Stefan Reinauer | 3a43160 | 2005-12-18 18:40:46 +0000 | [diff] [blame] | 76 | |
| 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 Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 81 | } else { |
| 82 | if(force) { |
| 83 | printf("WARNING: This firmware image does not " |
Stefan Reinauer | 3a43160 | 2005-12-18 18:40:46 +0000 | [diff] [blame] | 84 | "seem to fit to this machine - forcing it.\n"); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 85 | } else { |
Stefan Reinauer | 3a43160 | 2005-12-18 18:40:46 +0000 | [diff] [blame] | 86 | 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 Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 93 | exit(1); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | int 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 Reinauer | 3a43160 | 2005-12-18 18:40:46 +0000 | [diff] [blame] | 109 | printf("Error while opening rom layout (%s).\n", name); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 110 | 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 Reinauer | 3a43160 | 2005-12-18 18:40:46 +0000 | [diff] [blame] | 131 | printf_debug("romlayout %08x - %08x named %s\n", |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 132 | rom_entries[i].start, |
| 133 | rom_entries[i].end, |
| 134 | rom_entries[i].name); |
| 135 | } |
| 136 | |
| 137 | fclose(romlayout); |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | int 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 | |
| 161 | int 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 | } |