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