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