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) |
Stefan Tauner | 949ccc8 | 2013-09-15 14:01:06 +0000 | [diff] [blame] | 6 | * Copyright (C) 2011-2013 Stefan Tauner |
Uwe Hermann | 75f5107 | 2008-03-04 16:29:54 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 22 | #include <stdio.h> |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <string.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" |
Nico Huber | 3a9939b | 2016-04-27 15:56:14 +0200 | [diff] [blame] | 28 | #include "layout.h" |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 29 | |
Nico Huber | 3a9939b | 2016-04-27 15:56:14 +0200 | [diff] [blame] | 30 | struct romentry entries[MAX_ROMLAYOUT]; |
| 31 | static struct flashrom_layout layout = { entries, 0 }; |
Stefan Tauner | c70bc8a | 2013-08-30 22:22:57 +0000 | [diff] [blame] | 32 | |
| 33 | /* include_args holds the arguments specified at the command line with -i. They must be processed at some point |
Nico Huber | 3a9939b | 2016-04-27 15:56:14 +0200 | [diff] [blame] | 34 | * so that desired regions are marked as "included" in the layout. */ |
Stefan Tauner | c70bc8a | 2013-08-30 22:22:57 +0000 | [diff] [blame] | 35 | static char *include_args[MAX_ROMLAYOUT]; |
| 36 | static int num_include_args = 0; /* the number of valid include_args. */ |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 37 | |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 38 | struct flashrom_layout *get_global_layout(void) |
Nico Huber | 3a9939b | 2016-04-27 15:56:14 +0200 | [diff] [blame] | 39 | { |
| 40 | return &layout; |
| 41 | } |
| 42 | |
dhendrix | beaefe0 | 2017-09-03 18:06:53 -0700 | [diff] [blame^] | 43 | const struct flashrom_layout *get_layout(const struct flashrom_flashctx *const flashctx) |
| 44 | { |
| 45 | if (flashctx->layout && flashctx->layout->num_entries) |
| 46 | return flashctx->layout; |
| 47 | else |
| 48 | return &flashctx->fallback_layout.base; |
| 49 | } |
| 50 | |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 51 | #ifndef __LIBPAYLOAD__ |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 52 | int read_romlayout(const char *name) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 53 | { |
| 54 | FILE *romlayout; |
| 55 | char tempstr[256]; |
| 56 | int i; |
| 57 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 58 | romlayout = fopen(name, "r"); |
| 59 | |
| 60 | if (!romlayout) { |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 61 | msg_gerr("ERROR: Could not open ROM layout (%s).\n", |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 62 | name); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 63 | return -1; |
| 64 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 65 | |
| 66 | while (!feof(romlayout)) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 67 | char *tstr1, *tstr2; |
Carl-Daniel Hailfinger | da53ada | 2010-12-04 11:56:52 +0000 | [diff] [blame] | 68 | |
Nico Huber | 3a9939b | 2016-04-27 15:56:14 +0200 | [diff] [blame] | 69 | if (layout.num_entries >= MAX_ROMLAYOUT) { |
Carl-Daniel Hailfinger | da53ada | 2010-12-04 11:56:52 +0000 | [diff] [blame] | 70 | msg_gerr("Maximum number of ROM images (%i) in layout " |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 71 | "file reached.\n", MAX_ROMLAYOUT); |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 72 | (void)fclose(romlayout); |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 73 | return 1; |
Carl-Daniel Hailfinger | da53ada | 2010-12-04 11:56:52 +0000 | [diff] [blame] | 74 | } |
Nico Huber | 3a9939b | 2016-04-27 15:56:14 +0200 | [diff] [blame] | 75 | if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, layout.entries[layout.num_entries].name)) |
Peter Stuge | 1fec0f3 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 76 | continue; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 77 | #if 0 |
| 78 | // fscanf does not like arbitrary comments like that :( later |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 79 | if (tempstr[0] == '#') { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 80 | continue; |
| 81 | } |
| 82 | #endif |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 83 | tstr1 = strtok(tempstr, ":"); |
| 84 | tstr2 = strtok(NULL, ":"); |
Uwe Hermann | 58783e3 | 2008-12-22 16:42:59 +0000 | [diff] [blame] | 85 | if (!tstr1 || !tstr2) { |
Stefan Tauner | eb58257 | 2012-09-21 12:52:50 +0000 | [diff] [blame] | 86 | msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr); |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 87 | (void)fclose(romlayout); |
Uwe Hermann | 58783e3 | 2008-12-22 16:42:59 +0000 | [diff] [blame] | 88 | return 1; |
| 89 | } |
Nico Huber | 3a9939b | 2016-04-27 15:56:14 +0200 | [diff] [blame] | 90 | layout.entries[layout.num_entries].start = strtol(tstr1, (char **)NULL, 16); |
| 91 | layout.entries[layout.num_entries].end = strtol(tstr2, (char **)NULL, 16); |
| 92 | layout.entries[layout.num_entries].included = 0; |
| 93 | layout.num_entries++; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 94 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 95 | |
Nico Huber | 3a9939b | 2016-04-27 15:56:14 +0200 | [diff] [blame] | 96 | for (i = 0; i < layout.num_entries; i++) { |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 97 | msg_gdbg("romlayout %08x - %08x named %s\n", |
Nico Huber | 3a9939b | 2016-04-27 15:56:14 +0200 | [diff] [blame] | 98 | layout.entries[i].start, |
| 99 | layout.entries[i].end, layout.entries[i].name); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 102 | (void)fclose(romlayout); |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 103 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 104 | return 0; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 105 | } |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 106 | #endif |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 107 | |
Stefan Tauner | 23bb6d5 | 2012-04-15 14:09:16 +0000 | [diff] [blame] | 108 | /* returns the index of the entry (or a negative value if it is not found) */ |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 109 | static int find_include_arg(const char *const name) |
Stefan Tauner | 23bb6d5 | 2012-04-15 14:09:16 +0000 | [diff] [blame] | 110 | { |
| 111 | unsigned int i; |
| 112 | for (i = 0; i < num_include_args; i++) { |
| 113 | if (!strcmp(include_args[i], name)) |
| 114 | return i; |
| 115 | } |
| 116 | return -1; |
| 117 | } |
| 118 | |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 119 | /* register an include argument (-i) for later processing */ |
| 120 | int register_include_arg(char *name) |
| 121 | { |
| 122 | if (num_include_args >= MAX_ROMLAYOUT) { |
| 123 | msg_gerr("Too many regions included (%i).\n", num_include_args); |
| 124 | return 1; |
| 125 | } |
| 126 | |
| 127 | if (name == NULL) { |
| 128 | msg_gerr("<NULL> is a bad region name.\n"); |
| 129 | return 1; |
| 130 | } |
| 131 | |
Stefan Tauner | 23bb6d5 | 2012-04-15 14:09:16 +0000 | [diff] [blame] | 132 | if (find_include_arg(name) != -1) { |
| 133 | msg_gerr("Duplicate region name: \"%s\".\n", name); |
| 134 | return 1; |
| 135 | } |
| 136 | |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 137 | include_args[num_include_args] = name; |
| 138 | num_include_args++; |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | /* returns the index of the entry (or a negative value if it is not found) */ |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 143 | static int find_romentry(struct flashrom_layout *const l, char *name) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 144 | { |
| 145 | int i; |
| 146 | |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 147 | if (l->num_entries == 0) |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 148 | return -1; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 149 | |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 150 | msg_gspew("Looking for region \"%s\"... ", name); |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 151 | for (i = 0; i < l->num_entries; i++) { |
| 152 | if (!strcmp(l->entries[i].name, name)) { |
| 153 | l->entries[i].included = 1; |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 154 | msg_gspew("found.\n"); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 155 | return i; |
| 156 | } |
| 157 | } |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 158 | msg_gspew("not found.\n"); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 159 | return -1; |
| 160 | } |
| 161 | |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 162 | /* process -i arguments |
| 163 | * returns 0 to indicate success, >0 to indicate failure |
| 164 | */ |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 165 | int process_include_args(struct flashrom_layout *const l) |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 166 | { |
| 167 | int i; |
| 168 | unsigned int found = 0; |
| 169 | |
| 170 | if (num_include_args == 0) |
| 171 | return 0; |
| 172 | |
Stefan Tauner | 23bb6d5 | 2012-04-15 14:09:16 +0000 | [diff] [blame] | 173 | /* User has specified an area, but no layout file is loaded. */ |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 174 | if (l->num_entries == 0) { |
Stefan Tauner | 23bb6d5 | 2012-04-15 14:09:16 +0000 | [diff] [blame] | 175 | msg_gerr("Region requested (with -i \"%s\"), " |
| 176 | "but no layout data is available.\n", |
| 177 | include_args[0]); |
| 178 | return 1; |
| 179 | } |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 180 | |
Stefan Tauner | 23bb6d5 | 2012-04-15 14:09:16 +0000 | [diff] [blame] | 181 | for (i = 0; i < num_include_args; i++) { |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 182 | if (find_romentry(l, include_args[i]) < 0) { |
Stefan Tauner | 23bb6d5 | 2012-04-15 14:09:16 +0000 | [diff] [blame] | 183 | msg_gerr("Invalid region specified: \"%s\".\n", |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 184 | include_args[i]); |
| 185 | return 1; |
| 186 | } |
| 187 | found++; |
| 188 | } |
| 189 | |
| 190 | msg_ginfo("Using region%s: \"%s\"", num_include_args > 1 ? "s" : "", |
| 191 | include_args[0]); |
| 192 | for (i = 1; i < num_include_args; i++) |
| 193 | msg_ginfo(", \"%s\"", include_args[i]); |
| 194 | msg_ginfo(".\n"); |
| 195 | return 0; |
| 196 | } |
| 197 | |
Stefan Tauner | 949ccc8 | 2013-09-15 14:01:06 +0000 | [diff] [blame] | 198 | void layout_cleanup(void) |
| 199 | { |
| 200 | int i; |
| 201 | for (i = 0; i < num_include_args; i++) { |
| 202 | free(include_args[i]); |
| 203 | include_args[i] = NULL; |
| 204 | } |
| 205 | num_include_args = 0; |
| 206 | |
Nico Huber | 3a9939b | 2016-04-27 15:56:14 +0200 | [diff] [blame] | 207 | for (i = 0; i < layout.num_entries; i++) { |
| 208 | layout.entries[i].included = 0; |
Stefan Tauner | 949ccc8 | 2013-09-15 14:01:06 +0000 | [diff] [blame] | 209 | } |
Nico Huber | 3a9939b | 2016-04-27 15:56:14 +0200 | [diff] [blame] | 210 | layout.num_entries = 0; |
Stefan Tauner | 949ccc8 | 2013-09-15 14:01:06 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Stefan Tauner | 8268fdb | 2013-09-23 14:21:06 +0000 | [diff] [blame] | 213 | /* Validate and - if needed - normalize layout entries. */ |
| 214 | int normalize_romentries(const struct flashctx *flash) |
| 215 | { |
| 216 | chipsize_t total_size = flash->chip->total_size * 1024; |
| 217 | int ret = 0; |
| 218 | |
| 219 | int i; |
Nico Huber | 3a9939b | 2016-04-27 15:56:14 +0200 | [diff] [blame] | 220 | for (i = 0; i < layout.num_entries; i++) { |
| 221 | if (layout.entries[i].start >= total_size || layout.entries[i].end >= total_size) { |
Stefan Tauner | 8268fdb | 2013-09-23 14:21:06 +0000 | [diff] [blame] | 222 | msg_gwarn("Warning: Address range of region \"%s\" exceeds the current chip's " |
Nico Huber | 3a9939b | 2016-04-27 15:56:14 +0200 | [diff] [blame] | 223 | "address space.\n", layout.entries[i].name); |
| 224 | if (layout.entries[i].included) |
Stefan Tauner | 8268fdb | 2013-09-23 14:21:06 +0000 | [diff] [blame] | 225 | ret = 1; |
| 226 | } |
Nico Huber | 3a9939b | 2016-04-27 15:56:14 +0200 | [diff] [blame] | 227 | if (layout.entries[i].start > layout.entries[i].end) { |
Stefan Tauner | 8268fdb | 2013-09-23 14:21:06 +0000 | [diff] [blame] | 228 | msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n", |
Nico Huber | 3a9939b | 2016-04-27 15:56:14 +0200 | [diff] [blame] | 229 | layout.entries[i].name); |
Stefan Tauner | 8268fdb | 2013-09-23 14:21:06 +0000 | [diff] [blame] | 230 | ret = 1; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return ret; |
| 235 | } |