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