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> |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 25 | #include <limits.h> |
Uwe Hermann | 0846f89 | 2007-08-23 13:34:59 +0000 | [diff] [blame] | 26 | #include "flash.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 27 | #include "programmer.h" |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 28 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 29 | #if CONFIG_INTERNAL == 1 |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 30 | char *mainboard_vendor = NULL; |
| 31 | char *mainboard_part = NULL; |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 32 | #endif |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 33 | static int romimages = 0; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 34 | |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 35 | #define MAX_ROMLAYOUT 32 |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 36 | |
| 37 | typedef struct { |
| 38 | unsigned int start; |
| 39 | unsigned int end; |
| 40 | unsigned int included; |
| 41 | char name[256]; |
| 42 | } romlayout_t; |
| 43 | |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 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. */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 50 | static romlayout_t rom_entries[MAX_ROMLAYOUT]; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 51 | |
Carl-Daniel Hailfinger | 7112772 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 52 | #if CONFIG_INTERNAL == 1 /* FIXME: Move the whole block to cbtable.c? */ |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 53 | static char *def_name = "DEFAULT"; |
| 54 | |
Peter Stuge | 7ffbc6f | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 55 | int show_id(uint8_t *bios, int size, int force) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 56 | { |
| 57 | unsigned int *walk; |
Carl-Daniel Hailfinger | 85f8a17 | 2008-07-11 00:06:38 +0000 | [diff] [blame] | 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; |
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 - 0x10); |
| 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 | if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) { |
Uwe Hermann | 6c8866c | 2008-07-03 19:26:44 +0000 | [diff] [blame] | 68 | /* We might have an NVIDIA chipset BIOS which stores the ID |
| 69 | * information at a different location. |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 70 | */ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 71 | walk = (unsigned int *)(bios + size - 0x80); |
| 72 | walk--; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 73 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 74 | |
Carl-Daniel Hailfinger | ecab4fc | 2008-07-03 14:40:06 +0000 | [diff] [blame] | 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 | */ |
Carl-Daniel Hailfinger | 85f8a17 | 2008-07-11 00:06:38 +0000 | [diff] [blame] | 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) { |
Stefan Tauner | faf01fb | 2011-05-18 01:31:53 +0000 | [diff] [blame] | 85 | msg_pinfo("Flash image seems to be a legacy BIOS. " |
| 86 | "Disabling coreboot-related checks.\n"); |
Carl-Daniel Hailfinger | 85f8a17 | 2008-07-11 00:06:38 +0000 | [diff] [blame] | 87 | return 0; |
| 88 | } |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 89 | |
Carl-Daniel Hailfinger | 85f8a17 | 2008-07-11 00:06:38 +0000 | [diff] [blame] | 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)) { |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 94 | msg_pinfo("Flash image seems to have garbage in the ID location." |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 95 | " Disabling checks.\n"); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 96 | return 0; |
| 97 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 98 | |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 99 | msg_pdbg("coreboot last image size " |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 100 | "(not ROM size) is %d bytes.\n", *walk); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 101 | |
Carl-Daniel Hailfinger | 85f8a17 | 2008-07-11 00:06:38 +0000 | [diff] [blame] | 102 | mainboard_part = strdup(mb_part); |
| 103 | mainboard_vendor = strdup(mb_vendor); |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 104 | msg_pdbg("Manufacturer: %s\n", mainboard_vendor); |
| 105 | msg_pdbg("Mainboard ID: %s\n", mainboard_part); |
Stefan Reinauer | 3a43160 | 2005-12-18 18:40:46 +0000 | [diff] [blame] | 106 | |
| 107 | /* |
Stefan Reinauer | e3f3e2e | 2008-01-18 15:33:10 +0000 | [diff] [blame] | 108 | * If lb_vendor is not set, the coreboot table was |
Carl-Daniel Hailfinger | 2d927fb | 2012-01-04 00:48:27 +0000 | [diff] [blame] | 109 | * not found. Nor was -p internal:mainboard=VENDOR:PART specified. |
Stefan Reinauer | 3a43160 | 2005-12-18 18:40:46 +0000 | [diff] [blame] | 110 | */ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 111 | if (!lb_vendor || !lb_part) { |
Carl-Daniel Hailfinger | 2d927fb | 2012-01-04 00:48:27 +0000 | [diff] [blame] | 112 | msg_pinfo("Note: If the following flash access fails, try " |
| 113 | "-p internal:mainboard=<vendor>:<mainboard>.\n"); |
Stefan Reinauer | 3a43160 | 2005-12-18 18:40:46 +0000 | [diff] [blame] | 114 | return 0; |
| 115 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 116 | |
Stefan Reinauer | e370528 | 2005-12-18 16:41:10 +0000 | [diff] [blame] | 117 | /* These comparisons are case insensitive to make things |
| 118 | * a little less user^Werror prone. |
| 119 | */ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 120 | if (!strcasecmp(mainboard_vendor, lb_vendor) && |
| 121 | !strcasecmp(mainboard_part, lb_part)) { |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 122 | msg_pdbg("This firmware image matches this mainboard.\n"); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 123 | } else { |
Carl-Daniel Hailfinger | 2702376 | 2010-04-28 15:22:14 +0000 | [diff] [blame] | 124 | if (force_boardmismatch) { |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 125 | msg_pinfo("WARNING: This firmware image does not " |
Uwe Hermann | ac30934 | 2007-10-10 17:42:20 +0000 | [diff] [blame] | 126 | "seem to fit to this machine - forcing it.\n"); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 127 | } else { |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 128 | msg_pinfo("ERROR: Your firmware image (%s:%s) does not " |
Carl-Daniel Hailfinger | 2d927fb | 2012-01-04 00:48:27 +0000 | [diff] [blame] | 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); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 140 | exit(1); |
| 141 | } |
| 142 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 143 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 144 | return 0; |
| 145 | } |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 146 | #endif |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 147 | |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 148 | #ifndef __LIBPAYLOAD__ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 149 | int read_romlayout(char *name) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 150 | { |
| 151 | FILE *romlayout; |
| 152 | char tempstr[256]; |
| 153 | int i; |
| 154 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 155 | romlayout = fopen(name, "r"); |
| 156 | |
| 157 | if (!romlayout) { |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 158 | msg_gerr("ERROR: Could not open ROM layout (%s).\n", |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 159 | name); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 160 | return -1; |
| 161 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 162 | |
| 163 | while (!feof(romlayout)) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 164 | char *tstr1, *tstr2; |
Carl-Daniel Hailfinger | da53ada | 2010-12-04 11:56:52 +0000 | [diff] [blame] | 165 | |
| 166 | if (romimages >= MAX_ROMLAYOUT) { |
| 167 | msg_gerr("Maximum number of ROM images (%i) in layout " |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 168 | "file reached.\n", MAX_ROMLAYOUT); |
| 169 | return 1; |
Carl-Daniel Hailfinger | da53ada | 2010-12-04 11:56:52 +0000 | [diff] [blame] | 170 | } |
Peter Stuge | 1fec0f3 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 171 | if (2 != fscanf(romlayout, "%s %s\n", tempstr, rom_entries[romimages].name)) |
| 172 | continue; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 173 | #if 0 |
| 174 | // fscanf does not like arbitrary comments like that :( later |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 175 | if (tempstr[0] == '#') { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 176 | continue; |
| 177 | } |
| 178 | #endif |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 179 | tstr1 = strtok(tempstr, ":"); |
| 180 | tstr2 = strtok(NULL, ":"); |
Uwe Hermann | 58783e3 | 2008-12-22 16:42:59 +0000 | [diff] [blame] | 181 | if (!tstr1 || !tstr2) { |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 182 | msg_gerr("Error parsing layout file.\n"); |
Uwe Hermann | 58783e3 | 2008-12-22 16:42:59 +0000 | [diff] [blame] | 183 | fclose(romlayout); |
| 184 | return 1; |
| 185 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 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; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 189 | romimages++; |
| 190 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 191 | |
| 192 | for (i = 0; i < romimages; i++) { |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 193 | msg_gdbg("romlayout %08x - %08x named %s\n", |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 194 | rom_entries[i].start, |
| 195 | rom_entries[i].end, rom_entries[i].name); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | fclose(romlayout); |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 199 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 200 | return 0; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 201 | } |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 202 | #endif |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 203 | |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 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) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 224 | { |
| 225 | int i; |
| 226 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 227 | if (!romimages) |
| 228 | return -1; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 229 | |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 230 | msg_gspew("Looking for region \"%s\"... ", name); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 231 | for (i = 0; i < romimages; i++) { |
| 232 | if (!strcmp(rom_entries[i].name, name)) { |
| 233 | rom_entries[i].included = 1; |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 234 | msg_gspew("found.\n"); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 235 | return i; |
| 236 | } |
| 237 | } |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 238 | msg_gspew("not found.\n"); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 239 | return -1; |
| 240 | } |
| 241 | |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 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 | |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 278 | romlayout_t *get_next_included_romentry(unsigned int start) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 279 | { |
| 280 | int i; |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 281 | unsigned int best_start = UINT_MAX; |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 282 | romlayout_t *best_entry = NULL; |
| 283 | romlayout_t *cur; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 284 | |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 285 | /* First come, first serve for overlapping regions. */ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 286 | for (i = 0; i < romimages; i++) { |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 287 | cur = &rom_entries[i]; |
| 288 | if (!cur->included) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 289 | continue; |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 290 | /* Already past the current entry? */ |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 291 | if (start > cur->end) |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 292 | continue; |
| 293 | /* Inside the current entry? */ |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 294 | if (start >= cur->start) |
| 295 | return cur; |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 296 | /* Entry begins after start. */ |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 297 | if (best_start > cur->start) { |
| 298 | best_start = cur->start; |
| 299 | best_entry = cur; |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 300 | } |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 301 | } |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 302 | return best_entry; |
| 303 | } |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 304 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 305 | int handle_romentries(struct flashctx *flash, uint8_t *oldcontents, uint8_t *newcontents) |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 306 | { |
| 307 | unsigned int start = 0; |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 308 | romlayout_t *entry; |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 309 | unsigned int size = flash->total_size * 1024; |
| 310 | |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 311 | /* If no regions were specified for inclusion, assume |
| 312 | * that the user wants to write the complete new image. |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 313 | */ |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 314 | if (num_include_args == 0) |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 315 | return 0; |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 316 | |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 317 | /* Non-included romentries are ignored. |
| 318 | * The union of all included romentries is used from the new image. |
| 319 | */ |
| 320 | while (start < size) { |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 321 | entry = get_next_included_romentry(start); |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 322 | /* No more romentries for remaining region? */ |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 323 | if (!entry) { |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 324 | memcpy(newcontents + start, oldcontents + start, |
| 325 | size - start); |
| 326 | break; |
| 327 | } |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 328 | /* For non-included region, copy from old content. */ |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 329 | if (entry->start > start) |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 330 | memcpy(newcontents + start, oldcontents + start, |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 331 | entry->start - start); |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 332 | /* Skip to location after current romentry. */ |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 333 | start = entry->end + 1; |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 334 | /* Catch overflow. */ |
| 335 | if (!start) |
| 336 | break; |
| 337 | } |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 338 | return 0; |
| 339 | } |