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 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 9 | char *mainboard_vendor = NULL; |
| 10 | char *mainboard_part = NULL; |
| 11 | int romimages = 0; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 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 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 28 | int show_id(uint8_t *bios, int size) |
| 29 | { |
| 30 | unsigned int *walk; |
| 31 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 32 | walk = (unsigned int *)(bios + size - 0x10); |
| 33 | walk--; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 34 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 35 | if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 36 | /* We might have an Nvidia chipset bios |
| 37 | * which stores the id information at a |
| 38 | * different location. |
| 39 | */ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 40 | walk = (unsigned int *)(bios + size - 0x80); |
| 41 | walk--; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 42 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 43 | |
| 44 | if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 45 | printf("Flash image seems to be a legacy BIOS. Disabling checks.\n"); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 46 | mainboard_vendor = def_name; |
| 47 | mainboard_part = def_name; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 48 | return 0; |
| 49 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 50 | |
Stefan Reinauer | 3a43160 | 2005-12-18 18:40:46 +0000 | [diff] [blame] | 51 | printf_debug("LinuxBIOS last image size " |
| 52 | "(not rom size) is %d bytes.\n", *walk); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 53 | |
| 54 | walk--; |
| 55 | mainboard_part = strdup((const char *)(bios + size - *walk)); |
| 56 | walk--; |
| 57 | 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); |
Stefan Reinauer | 3a43160 | 2005-12-18 18:40:46 +0000 | [diff] [blame] | 60 | |
| 61 | /* |
| 62 | * If lb_vendor is not set, the linuxbios table was |
| 63 | * not found. Nor was -mVENDOR:PART specified |
| 64 | */ |
| 65 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 66 | if (!lb_vendor || !lb_part) { |
Stefan Reinauer | 3a43160 | 2005-12-18 18:40:46 +0000 | [diff] [blame] | 67 | printf("Note: If the following flash access fails, " |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 68 | "you might need to specify -m <vendor>:<mainboard>\n"); |
Stefan Reinauer | 3a43160 | 2005-12-18 18:40:46 +0000 | [diff] [blame] | 69 | return 0; |
| 70 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 71 | |
Stefan Reinauer | e370528 | 2005-12-18 16:41:10 +0000 | [diff] [blame] | 72 | /* These comparisons are case insensitive to make things |
| 73 | * a little less user^Werror prone. |
| 74 | */ |
Stefan Reinauer | 3a43160 | 2005-12-18 18:40:46 +0000 | [diff] [blame] | 75 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 76 | if (!strcasecmp(mainboard_vendor, lb_vendor) && |
| 77 | !strcasecmp(mainboard_part, lb_part)) { |
Stefan Reinauer | 3a43160 | 2005-12-18 18:40:46 +0000 | [diff] [blame] | 78 | printf_debug("This firmware image matches " |
| 79 | "this motherboard.\n"); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 80 | } else { |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 81 | if (force) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 82 | printf("WARNING: This firmware image does not " |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 83 | "seem to fit to this machine - forcing it.\n"); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 84 | } else { |
Stefan Reinauer | 3a43160 | 2005-12-18 18:40:46 +0000 | [diff] [blame] | 85 | printf("ERROR: Your firmware image (%s:%s) does not " |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 86 | "appear to\n be correct for the detected " |
| 87 | "mainboard (%s:%s)\n\nOverride with --force if you " |
| 88 | "are absolutely sure that you\nare using a correct " |
| 89 | "image for this mainboard or override\nthe detected " |
| 90 | "values with --mainboard <vendor>:<mainboard>.\n\n", |
| 91 | mainboard_vendor, mainboard_part, lb_vendor, |
| 92 | lb_part); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 93 | exit(1); |
| 94 | } |
| 95 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 96 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 100 | int read_romlayout(char *name) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 101 | { |
| 102 | FILE *romlayout; |
| 103 | char tempstr[256]; |
| 104 | int i; |
| 105 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 106 | romlayout = fopen(name, "r"); |
| 107 | |
| 108 | if (!romlayout) { |
| 109 | fprintf(stderr, "ERROR: Could not open rom layout (%s).\n", |
| 110 | name); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 111 | return -1; |
| 112 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 113 | |
| 114 | while (!feof(romlayout)) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 115 | char *tstr1, *tstr2; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 116 | fscanf(romlayout, "%s %s\n", tempstr, |
| 117 | rom_entries[romimages].name); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 118 | #if 0 |
| 119 | // fscanf does not like arbitrary comments like that :( later |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 120 | if (tempstr[0] == '#') { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 121 | continue; |
| 122 | } |
| 123 | #endif |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 124 | tstr1 = strtok(tempstr, ":"); |
| 125 | tstr2 = strtok(NULL, ":"); |
| 126 | rom_entries[romimages].start = strtol(tstr1, (char **)NULL, 16); |
| 127 | rom_entries[romimages].end = strtol(tstr2, (char **)NULL, 16); |
| 128 | rom_entries[romimages].included = 0; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 129 | romimages++; |
| 130 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 131 | |
| 132 | for (i = 0; i < romimages; i++) { |
| 133 | printf_debug("romlayout %08x - %08x named %s\n", |
| 134 | rom_entries[i].start, |
| 135 | rom_entries[i].end, rom_entries[i].name); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | fclose(romlayout); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 139 | return 0; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | int find_romentry(char *name) |
| 143 | { |
| 144 | int i; |
| 145 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 146 | if (!romimages) |
| 147 | return -1; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 148 | |
| 149 | printf("Looking for \"%s\"... ", name); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 150 | |
| 151 | for (i = 0; i < romimages; i++) { |
| 152 | if (!strcmp(rom_entries[i].name, name)) { |
| 153 | rom_entries[i].included = 1; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 154 | printf("found.\n"); |
| 155 | return i; |
| 156 | } |
| 157 | } |
| 158 | printf("not found.\n"); |
| 159 | // Not found. Error. |
| 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | int handle_romentries(uint8_t *buffer, uint8_t *content) |
| 164 | { |
| 165 | int i; |
| 166 | |
| 167 | // This function does not safe flash write cycles. |
| 168 | // |
| 169 | // Also it does not cope with overlapping rom layout |
| 170 | // sections. |
| 171 | // example: |
| 172 | // 00000000:00008fff gfxrom |
| 173 | // 00009000:0003ffff normal |
| 174 | // 00040000:0007ffff fallback |
| 175 | // 00000000:0007ffff all |
| 176 | // |
| 177 | // If you'd specify -i all the included flag of all other |
| 178 | // sections is still 0, so no changes will be made to the |
| 179 | // flash. Same thing if you specify -i normal -i all only |
| 180 | // normal will be updated and the rest will be kept. |
| 181 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 182 | for (i = 0; i < romimages; i++) { |
| 183 | |
| 184 | if (rom_entries[i].included) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 185 | continue; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 186 | |
| 187 | memcpy(buffer + rom_entries[i].start, |
| 188 | content + rom_entries[i].start, |
| 189 | rom_entries[i].end - rom_entries[i].start); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } |