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