| 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 | ␊ |
| 21 | #include <stdio.h>␊ |
| 22 | #include <stdlib.h>␊ |
| 23 | #include <string.h>␊ |
| 24 | #include <ctype.h>␊ |
| 25 | #include <limits.h>␊ |
| 26 | #include "flash.h"␊ |
| 27 | #include "programmer.h"␊ |
| 28 | ␊ |
| 29 | #if CONFIG_INTERNAL == 1␊ |
| 30 | char *mainboard_vendor = NULL;␊ |
| 31 | char *mainboard_part = NULL;␊ |
| 32 | #endif␊ |
| 33 | static int romimages = 0;␊ |
| 34 | ␊ |
| 35 | #define MAX_ROMLAYOUT␉32␊ |
| 36 | ␊ |
| 37 | typedef struct {␊ |
| 38 | ␉unsigned int start;␊ |
| 39 | ␉unsigned int end;␊ |
| 40 | ␉unsigned int included;␊ |
| 41 | ␉char name[256];␊ |
| 42 | } romlayout_t;␊ |
| 43 | ␊ |
| 44 | /* include_args lists arguments specified at the command line with -i. They␊ |
| 45 | * must be processed at some point so that desired regions are marked as␊ |
| 46 | * "included" in the rom_entries list.␊ |
| 47 | */␊ |
| 48 | static char *include_args[MAX_ROMLAYOUT];␊ |
| 49 | static int num_include_args = 0; /* the number of valid entries. */␊ |
| 50 | static romlayout_t rom_entries[MAX_ROMLAYOUT];␊ |
| 51 | ␊ |
| 52 | #if CONFIG_INTERNAL == 1 /* FIXME: Move the whole block to cbtable.c? */␊ |
| 53 | static char *def_name = "DEFAULT";␊ |
| 54 | ␊ |
| 55 | int show_id(uint8_t *bios, int size, int force)␊ |
| 56 | {␊ |
| 57 | ␉unsigned int *walk;␊ |
| 58 | ␉unsigned int mb_part_offset, mb_vendor_offset;␊ |
| 59 | ␉char *mb_part, *mb_vendor;␊ |
| 60 | ␊ |
| 61 | ␉mainboard_vendor = def_name;␊ |
| 62 | ␉mainboard_part = def_name;␊ |
| 63 | ␊ |
| 64 | ␉walk = (unsigned int *)(bios + size - 0x10);␊ |
| 65 | ␉walk--;␊ |
| 66 | ␊ |
| 67 | ␉if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {␊ |
| 68 | ␉␉/* We might have an NVIDIA chipset BIOS which stores the ID␊ |
| 69 | ␉␉ * information at a different location.␊ |
| 70 | ␉␉ */␊ |
| 71 | ␉␉walk = (unsigned int *)(bios + size - 0x80);␊ |
| 72 | ␉␉walk--;␊ |
| 73 | ␉}␊ |
| 74 | ␊ |
| 75 | ␉/*␊ |
| 76 | ␉ * Check if coreboot last image size is 0 or not a multiple of 1k or␊ |
| 77 | ␉ * bigger than the chip or if the pointers to vendor ID or mainboard ID␊ |
| 78 | ␉ * are outside the image of if the start of ID strings are nonsensical␊ |
| 79 | ␉ * (nonprintable and not \0).␊ |
| 80 | ␉ */␊ |
| 81 | ␉mb_part_offset = *(walk - 1);␊ |
| 82 | ␉mb_vendor_offset = *(walk - 2);␊ |
| 83 | ␉if ((*walk) == 0 || ((*walk) & 0x3ff) != 0 || (*walk) > size ||␊ |
| 84 | ␉ mb_part_offset > size || mb_vendor_offset > size) {␊ |
| 85 | ␉␉msg_pinfo("Flash image seems to be a legacy BIOS. "␊ |
| 86 | ␉␉ "Disabling coreboot-related checks.\n");␊ |
| 87 | ␉␉return 0;␊ |
| 88 | ␉}␊ |
| 89 | ␊ |
| 90 | ␉mb_part = (char *)(bios + size - mb_part_offset);␊ |
| 91 | ␉mb_vendor = (char *)(bios + size - mb_vendor_offset);␊ |
| 92 | ␉if (!isprint((unsigned char)*mb_part) ||␊ |
| 93 | ␉ !isprint((unsigned char)*mb_vendor)) {␊ |
| 94 | ␉␉msg_pinfo("Flash image seems to have garbage in the ID location."␊ |
| 95 | ␉␉ " Disabling checks.\n");␊ |
| 96 | ␉␉return 0;␊ |
| 97 | ␉}␊ |
| 98 | ␊ |
| 99 | ␉msg_pdbg("coreboot last image size "␊ |
| 100 | ␉␉ "(not ROM size) is %d bytes.\n", *walk);␊ |
| 101 | ␊ |
| 102 | ␉mainboard_part = strdup(mb_part);␊ |
| 103 | ␉mainboard_vendor = strdup(mb_vendor);␊ |
| 104 | ␉msg_pdbg("Manufacturer: %s\n", mainboard_vendor);␊ |
| 105 | ␉msg_pdbg("Mainboard ID: %s\n", mainboard_part);␊ |
| 106 | ␊ |
| 107 | ␉/*␊ |
| 108 | ␉ * If lb_vendor is not set, the coreboot table was␊ |
| 109 | ␉ * not found. Nor was -p internal:mainboard=VENDOR:PART specified.␊ |
| 110 | ␉ */␊ |
| 111 | ␉if (!lb_vendor || !lb_part) {␊ |
| 112 | ␉␉msg_pinfo("Note: If the following flash access fails, try "␊ |
| 113 | ␉␉␉ "-p internal:mainboard=<vendor>:<mainboard>.\n");␊ |
| 114 | ␉␉return 0;␊ |
| 115 | ␉}␊ |
| 116 | ␊ |
| 117 | ␉/* These comparisons are case insensitive to make things␊ |
| 118 | ␉ * a little less user^Werror prone. ␊ |
| 119 | ␉ */␊ |
| 120 | ␉if (!strcasecmp(mainboard_vendor, lb_vendor) &&␊ |
| 121 | ␉ !strcasecmp(mainboard_part, lb_part)) {␊ |
| 122 | ␉␉msg_pdbg("This firmware image matches this mainboard.\n");␊ |
| 123 | ␉} else {␊ |
| 124 | ␉␉if (force_boardmismatch) {␊ |
| 125 | ␉␉␉msg_pinfo("WARNING: This firmware image does not "␊ |
| 126 | ␉␉␉ "seem to fit to this machine - forcing it.\n");␊ |
| 127 | ␉␉} else {␊ |
| 128 | ␉␉␉msg_pinfo("ERROR: Your firmware image (%s:%s) does not "␊ |
| 129 | ␉␉␉␉ "appear to\n"␊ |
| 130 | ␉␉␉␉ " be correct for the detected "␊ |
| 131 | ␉␉␉␉ "mainboard (%s:%s)\n\n"␊ |
| 132 | ␉␉␉␉ "Override with -p internal:boardmismatch="␊ |
| 133 | ␉␉␉␉ "force to ignore the board name in the\n"␊ |
| 134 | ␉␉␉␉ "firmware image or override the detected "␊ |
| 135 | ␉␉␉␉ "mainboard with\n"␊ |
| 136 | ␉␉␉␉ "-p internal:mainboard=<vendor>:<mainboard>."␊ |
| 137 | ␉␉␉␉ "\n\n",␊ |
| 138 | ␉␉␉␉ mainboard_vendor, mainboard_part, lb_vendor,␊ |
| 139 | ␉␉␉␉ lb_part);␊ |
| 140 | ␉␉␉exit(1);␊ |
| 141 | ␉␉}␊ |
| 142 | ␉}␊ |
| 143 | ␊ |
| 144 | ␉return 0;␊ |
| 145 | }␊ |
| 146 | #endif␊ |
| 147 | ␊ |
| 148 | #ifndef __LIBPAYLOAD__␊ |
| 149 | int read_romlayout(char *name)␊ |
| 150 | {␊ |
| 151 | ␉FILE *romlayout;␊ |
| 152 | ␉char tempstr[256];␊ |
| 153 | ␉int i;␊ |
| 154 | ␊ |
| 155 | ␉romlayout = fopen(name, "r");␊ |
| 156 | ␊ |
| 157 | ␉if (!romlayout) {␊ |
| 158 | ␉␉msg_gerr("ERROR: Could not open ROM layout (%s).\n",␊ |
| 159 | ␉␉␉name);␊ |
| 160 | ␉␉return -1;␊ |
| 161 | ␉}␊ |
| 162 | ␊ |
| 163 | ␉while (!feof(romlayout)) {␊ |
| 164 | ␉␉char *tstr1, *tstr2;␊ |
| 165 | ␊ |
| 166 | ␉␉if (romimages >= MAX_ROMLAYOUT) {␊ |
| 167 | ␉␉␉msg_gerr("Maximum number of ROM images (%i) in layout "␊ |
| 168 | ␉␉␉␉ "file reached.\n", MAX_ROMLAYOUT);␊ |
| 169 | ␉␉␉return 1;␊ |
| 170 | ␉␉}␊ |
| 171 | ␉␉if (2 != fscanf(romlayout, "%s %s\n", tempstr, rom_entries[romimages].name))␊ |
| 172 | ␉␉␉continue;␊ |
| 173 | #if 0␊ |
| 174 | ␉␉// fscanf does not like arbitrary comments like that :( later␊ |
| 175 | ␉␉if (tempstr[0] == '#') {␊ |
| 176 | ␉␉␉continue;␊ |
| 177 | ␉␉}␊ |
| 178 | #endif␊ |
| 179 | ␉␉tstr1 = strtok(tempstr, ":");␊ |
| 180 | ␉␉tstr2 = strtok(NULL, ":");␊ |
| 181 | ␉␉if (!tstr1 || !tstr2) {␊ |
| 182 | ␉␉␉msg_gerr("Error parsing layout file.\n");␊ |
| 183 | ␉␉␉fclose(romlayout);␊ |
| 184 | ␉␉␉return 1;␊ |
| 185 | ␉␉}␊ |
| 186 | ␉␉rom_entries[romimages].start = strtol(tstr1, (char **)NULL, 16);␊ |
| 187 | ␉␉rom_entries[romimages].end = strtol(tstr2, (char **)NULL, 16);␊ |
| 188 | ␉␉rom_entries[romimages].included = 0;␊ |
| 189 | ␉␉romimages++;␊ |
| 190 | ␉}␊ |
| 191 | ␊ |
| 192 | ␉for (i = 0; i < romimages; i++) {␊ |
| 193 | ␉␉msg_gdbg("romlayout %08x - %08x named %s\n",␊ |
| 194 | ␉␉␉ rom_entries[i].start,␊ |
| 195 | ␉␉␉ rom_entries[i].end, rom_entries[i].name);␊ |
| 196 | ␉}␊ |
| 197 | ␊ |
| 198 | ␉fclose(romlayout);␊ |
| 199 | ␊ |
| 200 | ␉return 0;␊ |
| 201 | }␊ |
| 202 | #endif␊ |
| 203 | ␊ |
| 204 | /* register an include argument (-i) for later processing */␊ |
| 205 | int register_include_arg(char *name)␊ |
| 206 | {␊ |
| 207 | ␉if (num_include_args >= MAX_ROMLAYOUT) {␊ |
| 208 | ␉␉msg_gerr("Too many regions included (%i).\n", num_include_args);␊ |
| 209 | ␉␉return 1;␊ |
| 210 | ␉}␊ |
| 211 | ␊ |
| 212 | ␉if (name == NULL) {␊ |
| 213 | ␉␉msg_gerr("<NULL> is a bad region name.\n");␊ |
| 214 | ␉␉return 1;␊ |
| 215 | ␉}␊ |
| 216 | ␊ |
| 217 | ␉include_args[num_include_args] = name;␊ |
| 218 | ␉num_include_args++;␊ |
| 219 | ␉return 0;␊ |
| 220 | }␊ |
| 221 | ␊ |
| 222 | /* returns the index of the entry (or a negative value if it is not found) */␊ |
| 223 | static int find_romentry(char *name)␊ |
| 224 | {␊ |
| 225 | ␉int i;␊ |
| 226 | ␊ |
| 227 | ␉if (!romimages)␊ |
| 228 | ␉␉return -1;␊ |
| 229 | ␊ |
| 230 | ␉msg_gspew("Looking for region \"%s\"... ", name);␊ |
| 231 | ␉for (i = 0; i < romimages; i++) {␊ |
| 232 | ␉␉if (!strcmp(rom_entries[i].name, name)) {␊ |
| 233 | ␉␉␉rom_entries[i].included = 1;␊ |
| 234 | ␉␉␉msg_gspew("found.\n");␊ |
| 235 | ␉␉␉return i;␊ |
| 236 | ␉␉}␊ |
| 237 | ␉}␊ |
| 238 | ␉msg_gspew("not found.\n");␊ |
| 239 | ␉return -1;␊ |
| 240 | }␊ |
| 241 | ␊ |
| 242 | /* process -i arguments␊ |
| 243 | * returns 0 to indicate success, >0 to indicate failure␊ |
| 244 | */␊ |
| 245 | int process_include_args(void)␊ |
| 246 | {␊ |
| 247 | ␉int i;␊ |
| 248 | ␉unsigned int found = 0;␊ |
| 249 | ␊ |
| 250 | ␉if (num_include_args == 0)␊ |
| 251 | ␉␉return 0;␊ |
| 252 | ␊ |
| 253 | ␉for (i = 0; i < num_include_args; i++) {␊ |
| 254 | ␉␉/* User has specified an area, but no layout file is loaded. */␊ |
| 255 | ␉␉if (!romimages) {␊ |
| 256 | ␉␉␉msg_gerr("Region requested (with -i \"%s\"), "␊ |
| 257 | ␉␉␉␉ "but no layout data is available.\n",␊ |
| 258 | ␉␉␉␉ include_args[i]);␊ |
| 259 | ␉␉␉return 1;␊ |
| 260 | ␉␉}␊ |
| 261 | ␊ |
| 262 | ␉␉if (find_romentry(include_args[i]) < 0) {␊ |
| 263 | ␉␉␉msg_gerr("Invalid region specified: \"%s\"\n",␊ |
| 264 | ␉␉␉␉ include_args[i]);␊ |
| 265 | ␉␉␉return 1;␊ |
| 266 | ␉␉}␊ |
| 267 | ␉␉found++;␊ |
| 268 | ␉}␊ |
| 269 | ␊ |
| 270 | ␉msg_ginfo("Using region%s: \"%s\"", num_include_args > 1 ? "s" : "",␊ |
| 271 | ␉␉ include_args[0]);␊ |
| 272 | ␉for (i = 1; i < num_include_args; i++)␊ |
| 273 | ␉␉msg_ginfo(", \"%s\"", include_args[i]);␊ |
| 274 | ␉msg_ginfo(".\n");␊ |
| 275 | ␉return 0;␊ |
| 276 | }␊ |
| 277 | ␊ |
| 278 | romlayout_t *get_next_included_romentry(unsigned int start)␊ |
| 279 | {␊ |
| 280 | ␉int i;␊ |
| 281 | ␉unsigned int best_start = UINT_MAX;␊ |
| 282 | ␉romlayout_t *best_entry = NULL;␊ |
| 283 | ␉romlayout_t *cur;␊ |
| 284 | ␊ |
| 285 | ␉/* First come, first serve for overlapping regions. */␊ |
| 286 | ␉for (i = 0; i < romimages; i++) {␊ |
| 287 | ␉␉cur = &rom_entries[i];␊ |
| 288 | ␉␉if (!cur->included)␊ |
| 289 | ␉␉␉continue;␊ |
| 290 | ␉␉/* Already past the current entry? */␊ |
| 291 | ␉␉if (start > cur->end)␊ |
| 292 | ␉␉␉continue;␊ |
| 293 | ␉␉/* Inside the current entry? */␊ |
| 294 | ␉␉if (start >= cur->start)␊ |
| 295 | ␉␉␉return cur;␊ |
| 296 | ␉␉/* Entry begins after start. */␊ |
| 297 | ␉␉if (best_start > cur->start) {␊ |
| 298 | ␉␉␉best_start = cur->start;␊ |
| 299 | ␉␉␉best_entry = cur;␊ |
| 300 | ␉␉}␊ |
| 301 | ␉}␊ |
| 302 | ␉return best_entry;␊ |
| 303 | }␊ |
| 304 | ␊ |
| 305 | int handle_romentries(struct flashctx *flash, uint8_t *oldcontents, uint8_t *newcontents)␊ |
| 306 | {␊ |
| 307 | ␉unsigned int start = 0;␊ |
| 308 | ␉romlayout_t *entry;␊ |
| 309 | ␉unsigned int size = flash->total_size * 1024;␊ |
| 310 | ␊ |
| 311 | ␉/* If no regions were specified for inclusion, assume␊ |
| 312 | ␉ * that the user wants to write the complete new image.␊ |
| 313 | ␉ */␊ |
| 314 | ␉if (num_include_args == 0)␊ |
| 315 | ␉␉return 0;␊ |
| 316 | ␊ |
| 317 | ␉/* Non-included romentries are ignored.␊ |
| 318 | ␉ * The union of all included romentries is used from the new image.␊ |
| 319 | ␉ */␊ |
| 320 | ␉while (start < size) {␊ |
| 321 | ␉␉entry = get_next_included_romentry(start);␊ |
| 322 | ␉␉/* No more romentries for remaining region? */␊ |
| 323 | ␉␉if (!entry) {␊ |
| 324 | ␉␉␉memcpy(newcontents + start, oldcontents + start,␊ |
| 325 | ␉␉␉ size - start);␊ |
| 326 | ␉␉␉break;␊ |
| 327 | ␉␉}␊ |
| 328 | ␉␉/* For non-included region, copy from old content. */␊ |
| 329 | ␉␉if (entry->start > start)␊ |
| 330 | ␉␉␉memcpy(newcontents + start, oldcontents + start,␊ |
| 331 | ␉␉␉ entry->start - start);␊ |
| 332 | ␉␉/* Skip to location after current romentry. */␊ |
| 333 | ␉␉start = entry->end + 1;␊ |
| 334 | ␉␉/* Catch overflow. */␊ |
| 335 | ␉␉if (!start)␊ |
| 336 | ␉␉␉break;␊ |
| 337 | ␉}␊ |
| 338 | ␉return 0;␊ |
| 339 | }␊ |
| 340 | |