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