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 | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 24 | #include <limits.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 | |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 28 | #define MAX_ROMLAYOUT 32 |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 29 | |
| 30 | typedef struct { |
| 31 | unsigned int start; |
| 32 | unsigned int end; |
| 33 | unsigned int included; |
| 34 | char name[256]; |
Stefan Tauner | 97b6c11 | 2013-08-30 22:23:02 +0000 | [diff] [blame] | 35 | } romentry_t; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 36 | |
Stefan Tauner | c70bc8a | 2013-08-30 22:22:57 +0000 | [diff] [blame] | 37 | /* rom_entries store the entries specified in a layout file and associated run-time data */ |
Stefan Tauner | 97b6c11 | 2013-08-30 22:23:02 +0000 | [diff] [blame] | 38 | static romentry_t rom_entries[MAX_ROMLAYOUT]; |
Stefan Tauner | c70bc8a | 2013-08-30 22:22:57 +0000 | [diff] [blame] | 39 | static int num_rom_entries = 0; /* the number of valid rom_entries */ |
| 40 | |
| 41 | /* include_args holds the arguments specified at the command line with -i. They must be processed at some point |
| 42 | * so that desired regions are marked as "included" in the rom_entries list. */ |
| 43 | static char *include_args[MAX_ROMLAYOUT]; |
| 44 | static int num_include_args = 0; /* the number of valid include_args. */ |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 45 | |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 46 | #ifndef __LIBPAYLOAD__ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 47 | int read_romlayout(char *name) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 48 | { |
| 49 | FILE *romlayout; |
| 50 | char tempstr[256]; |
| 51 | int i; |
| 52 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 53 | romlayout = fopen(name, "r"); |
| 54 | |
| 55 | if (!romlayout) { |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 56 | msg_gerr("ERROR: Could not open ROM layout (%s).\n", |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 57 | name); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 58 | return -1; |
| 59 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 60 | |
| 61 | while (!feof(romlayout)) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 62 | char *tstr1, *tstr2; |
Carl-Daniel Hailfinger | da53ada | 2010-12-04 11:56:52 +0000 | [diff] [blame] | 63 | |
Stefan Tauner | c70bc8a | 2013-08-30 22:22:57 +0000 | [diff] [blame] | 64 | if (num_rom_entries >= MAX_ROMLAYOUT) { |
Carl-Daniel Hailfinger | da53ada | 2010-12-04 11:56:52 +0000 | [diff] [blame] | 65 | msg_gerr("Maximum number of ROM images (%i) in layout " |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 66 | "file reached.\n", MAX_ROMLAYOUT); |
| 67 | return 1; |
Carl-Daniel Hailfinger | da53ada | 2010-12-04 11:56:52 +0000 | [diff] [blame] | 68 | } |
Stefan Tauner | c70bc8a | 2013-08-30 22:22:57 +0000 | [diff] [blame] | 69 | if (2 != fscanf(romlayout, "%s %s\n", tempstr, rom_entries[num_rom_entries].name)) |
Peter Stuge | 1fec0f3 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 70 | continue; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 71 | #if 0 |
| 72 | // fscanf does not like arbitrary comments like that :( later |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 73 | if (tempstr[0] == '#') { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 74 | continue; |
| 75 | } |
| 76 | #endif |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 77 | tstr1 = strtok(tempstr, ":"); |
| 78 | tstr2 = strtok(NULL, ":"); |
Uwe Hermann | 58783e3 | 2008-12-22 16:42:59 +0000 | [diff] [blame] | 79 | if (!tstr1 || !tstr2) { |
Stefan Tauner | eb58257 | 2012-09-21 12:52:50 +0000 | [diff] [blame] | 80 | msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr); |
Uwe Hermann | 58783e3 | 2008-12-22 16:42:59 +0000 | [diff] [blame] | 81 | fclose(romlayout); |
| 82 | return 1; |
| 83 | } |
Stefan Tauner | c70bc8a | 2013-08-30 22:22:57 +0000 | [diff] [blame] | 84 | rom_entries[num_rom_entries].start = strtol(tstr1, (char **)NULL, 16); |
| 85 | rom_entries[num_rom_entries].end = strtol(tstr2, (char **)NULL, 16); |
| 86 | rom_entries[num_rom_entries].included = 0; |
| 87 | num_rom_entries++; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 88 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 89 | |
Stefan Tauner | c70bc8a | 2013-08-30 22:22:57 +0000 | [diff] [blame] | 90 | for (i = 0; i < num_rom_entries; i++) { |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 91 | msg_gdbg("romlayout %08x - %08x named %s\n", |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 92 | rom_entries[i].start, |
| 93 | rom_entries[i].end, rom_entries[i].name); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | fclose(romlayout); |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 97 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 98 | return 0; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 99 | } |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 100 | #endif |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 101 | |
Stefan Tauner | 23bb6d5 | 2012-04-15 14:09:16 +0000 | [diff] [blame] | 102 | /* returns the index of the entry (or a negative value if it is not found) */ |
| 103 | int find_include_arg(const char *const name) |
| 104 | { |
| 105 | unsigned int i; |
| 106 | for (i = 0; i < num_include_args; i++) { |
| 107 | if (!strcmp(include_args[i], name)) |
| 108 | return i; |
| 109 | } |
| 110 | return -1; |
| 111 | } |
| 112 | |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 113 | /* register an include argument (-i) for later processing */ |
| 114 | int register_include_arg(char *name) |
| 115 | { |
| 116 | if (num_include_args >= MAX_ROMLAYOUT) { |
| 117 | msg_gerr("Too many regions included (%i).\n", num_include_args); |
| 118 | return 1; |
| 119 | } |
| 120 | |
| 121 | if (name == NULL) { |
| 122 | msg_gerr("<NULL> is a bad region name.\n"); |
| 123 | return 1; |
| 124 | } |
| 125 | |
Stefan Tauner | 23bb6d5 | 2012-04-15 14:09:16 +0000 | [diff] [blame] | 126 | if (find_include_arg(name) != -1) { |
| 127 | msg_gerr("Duplicate region name: \"%s\".\n", name); |
| 128 | return 1; |
| 129 | } |
| 130 | |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 131 | include_args[num_include_args] = name; |
| 132 | num_include_args++; |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | /* returns the index of the entry (or a negative value if it is not found) */ |
| 137 | static int find_romentry(char *name) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 138 | { |
| 139 | int i; |
| 140 | |
Stefan Tauner | c70bc8a | 2013-08-30 22:22:57 +0000 | [diff] [blame] | 141 | if (num_rom_entries == 0) |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 142 | return -1; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 143 | |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 144 | msg_gspew("Looking for region \"%s\"... ", name); |
Stefan Tauner | c70bc8a | 2013-08-30 22:22:57 +0000 | [diff] [blame] | 145 | for (i = 0; i < num_rom_entries; i++) { |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 146 | if (!strcmp(rom_entries[i].name, name)) { |
| 147 | rom_entries[i].included = 1; |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 148 | msg_gspew("found.\n"); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 149 | return i; |
| 150 | } |
| 151 | } |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 152 | msg_gspew("not found.\n"); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 153 | return -1; |
| 154 | } |
| 155 | |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 156 | /* process -i arguments |
| 157 | * returns 0 to indicate success, >0 to indicate failure |
| 158 | */ |
| 159 | int process_include_args(void) |
| 160 | { |
| 161 | int i; |
| 162 | unsigned int found = 0; |
| 163 | |
| 164 | if (num_include_args == 0) |
| 165 | return 0; |
| 166 | |
Stefan Tauner | 23bb6d5 | 2012-04-15 14:09:16 +0000 | [diff] [blame] | 167 | /* User has specified an area, but no layout file is loaded. */ |
Stefan Tauner | c70bc8a | 2013-08-30 22:22:57 +0000 | [diff] [blame] | 168 | if (num_rom_entries == 0) { |
Stefan Tauner | 23bb6d5 | 2012-04-15 14:09:16 +0000 | [diff] [blame] | 169 | msg_gerr("Region requested (with -i \"%s\"), " |
| 170 | "but no layout data is available.\n", |
| 171 | include_args[0]); |
| 172 | return 1; |
| 173 | } |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 174 | |
Stefan Tauner | 23bb6d5 | 2012-04-15 14:09:16 +0000 | [diff] [blame] | 175 | for (i = 0; i < num_include_args; i++) { |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 176 | if (find_romentry(include_args[i]) < 0) { |
Stefan Tauner | 23bb6d5 | 2012-04-15 14:09:16 +0000 | [diff] [blame] | 177 | msg_gerr("Invalid region specified: \"%s\".\n", |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 178 | include_args[i]); |
| 179 | return 1; |
| 180 | } |
| 181 | found++; |
| 182 | } |
| 183 | |
| 184 | msg_ginfo("Using region%s: \"%s\"", num_include_args > 1 ? "s" : "", |
| 185 | include_args[0]); |
| 186 | for (i = 1; i < num_include_args; i++) |
| 187 | msg_ginfo(", \"%s\"", include_args[i]); |
| 188 | msg_ginfo(".\n"); |
| 189 | return 0; |
| 190 | } |
| 191 | |
Stefan Tauner | 97b6c11 | 2013-08-30 22:23:02 +0000 | [diff] [blame] | 192 | romentry_t *get_next_included_romentry(unsigned int start) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 193 | { |
| 194 | int i; |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 195 | unsigned int best_start = UINT_MAX; |
Stefan Tauner | 97b6c11 | 2013-08-30 22:23:02 +0000 | [diff] [blame] | 196 | romentry_t *best_entry = NULL; |
| 197 | romentry_t *cur; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 198 | |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 199 | /* First come, first serve for overlapping regions. */ |
Stefan Tauner | c70bc8a | 2013-08-30 22:22:57 +0000 | [diff] [blame] | 200 | for (i = 0; i < num_rom_entries; i++) { |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 201 | cur = &rom_entries[i]; |
| 202 | if (!cur->included) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 203 | continue; |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 204 | /* Already past the current entry? */ |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 205 | if (start > cur->end) |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 206 | continue; |
| 207 | /* Inside the current entry? */ |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 208 | if (start >= cur->start) |
| 209 | return cur; |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 210 | /* Entry begins after start. */ |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 211 | if (best_start > cur->start) { |
| 212 | best_start = cur->start; |
| 213 | best_entry = cur; |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 214 | } |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 215 | } |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 216 | return best_entry; |
| 217 | } |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 218 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 219 | int handle_romentries(const struct flashctx *flash, uint8_t *oldcontents, uint8_t *newcontents) |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 220 | { |
| 221 | unsigned int start = 0; |
Stefan Tauner | 97b6c11 | 2013-08-30 22:23:02 +0000 | [diff] [blame] | 222 | romentry_t *entry; |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 223 | unsigned int size = flash->chip->total_size * 1024; |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 224 | |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 225 | /* If no regions were specified for inclusion, assume |
| 226 | * that the user wants to write the complete new image. |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 227 | */ |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 228 | if (num_include_args == 0) |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 229 | return 0; |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 230 | |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 231 | /* Non-included romentries are ignored. |
| 232 | * The union of all included romentries is used from the new image. |
| 233 | */ |
| 234 | while (start < size) { |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 235 | entry = get_next_included_romentry(start); |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 236 | /* No more romentries for remaining region? */ |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 237 | if (!entry) { |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 238 | memcpy(newcontents + start, oldcontents + start, |
| 239 | size - start); |
| 240 | break; |
| 241 | } |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 242 | /* For non-included region, copy from old content. */ |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 243 | if (entry->start > start) |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 244 | memcpy(newcontents + start, oldcontents + start, |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 245 | entry->start - start); |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 246 | /* Skip to location after current romentry. */ |
Stefan Tauner | 104b0d9 | 2011-12-25 09:07:59 +0000 | [diff] [blame] | 247 | start = entry->end + 1; |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 248 | /* Catch overflow. */ |
| 249 | if (!start) |
| 250 | break; |
| 251 | } |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 252 | return 0; |
| 253 | } |