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