blob: 3d09011f35917e909772cf7db926a02e21c889fb [file] [log] [blame]
Uwe Hermann75f51072008-03-04 16:29:54 +00001/*
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 Tauner949ccc82013-09-15 14:01:06 +00006 * Copyright (C) 2011-2013 Stefan Tauner
Uwe Hermann75f51072008-03-04 16:29:54 +00007 *
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 Hailfinger831e8f42010-05-30 22:24:40 +000022#include <stdio.h>
Ollie Lho184a4042005-11-26 21:55:36 +000023#include <stdlib.h>
24#include <string.h>
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +000025#include <limits.h>
Uwe Hermann0846f892007-08-23 13:34:59 +000026#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000027#include "programmer.h"
Nico Huber3a9939b2016-04-27 15:56:14 +020028#include "layout.h"
Ollie Lho184a4042005-11-26 21:55:36 +000029
Nico Huber3a9939b2016-04-27 15:56:14 +020030struct romentry entries[MAX_ROMLAYOUT];
31static struct flashrom_layout layout = { entries, 0 };
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000032
33/* include_args holds the arguments specified at the command line with -i. They must be processed at some point
Nico Huber3a9939b2016-04-27 15:56:14 +020034 * so that desired regions are marked as "included" in the layout. */
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000035static char *include_args[MAX_ROMLAYOUT];
36static int num_include_args = 0; /* the number of valid include_args. */
Ollie Lho184a4042005-11-26 21:55:36 +000037
Nico Huber3a9939b2016-04-27 15:56:14 +020038const struct flashrom_layout *get_global_layout(void)
39{
40 return &layout;
41}
42
Patrick Georgia9095a92010-09-30 17:03:32 +000043#ifndef __LIBPAYLOAD__
Mark Marshallf20b7be2014-05-09 21:16:21 +000044int read_romlayout(const char *name)
Ollie Lho184a4042005-11-26 21:55:36 +000045{
46 FILE *romlayout;
47 char tempstr[256];
48 int i;
49
Uwe Hermanna7e05482007-05-09 10:17:44 +000050 romlayout = fopen(name, "r");
51
52 if (!romlayout) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000053 msg_gerr("ERROR: Could not open ROM layout (%s).\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +000054 name);
Ollie Lho184a4042005-11-26 21:55:36 +000055 return -1;
56 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000057
58 while (!feof(romlayout)) {
Ollie Lho184a4042005-11-26 21:55:36 +000059 char *tstr1, *tstr2;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000060
Nico Huber3a9939b2016-04-27 15:56:14 +020061 if (layout.num_entries >= MAX_ROMLAYOUT) {
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000062 msg_gerr("Maximum number of ROM images (%i) in layout "
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +000063 "file reached.\n", MAX_ROMLAYOUT);
Stefan Tauner16687702015-12-25 21:59:45 +000064 (void)fclose(romlayout);
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +000065 return 1;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000066 }
Nico Huber3a9939b2016-04-27 15:56:14 +020067 if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, layout.entries[layout.num_entries].name))
Peter Stuge1fec0f32009-01-12 21:00:35 +000068 continue;
Ollie Lho184a4042005-11-26 21:55:36 +000069#if 0
70 // fscanf does not like arbitrary comments like that :( later
Uwe Hermanna7e05482007-05-09 10:17:44 +000071 if (tempstr[0] == '#') {
Ollie Lho184a4042005-11-26 21:55:36 +000072 continue;
73 }
74#endif
Uwe Hermanna7e05482007-05-09 10:17:44 +000075 tstr1 = strtok(tempstr, ":");
76 tstr2 = strtok(NULL, ":");
Uwe Hermann58783e32008-12-22 16:42:59 +000077 if (!tstr1 || !tstr2) {
Stefan Taunereb582572012-09-21 12:52:50 +000078 msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr);
Stefan Tauner16687702015-12-25 21:59:45 +000079 (void)fclose(romlayout);
Uwe Hermann58783e32008-12-22 16:42:59 +000080 return 1;
81 }
Nico Huber3a9939b2016-04-27 15:56:14 +020082 layout.entries[layout.num_entries].start = strtol(tstr1, (char **)NULL, 16);
83 layout.entries[layout.num_entries].end = strtol(tstr2, (char **)NULL, 16);
84 layout.entries[layout.num_entries].included = 0;
85 layout.num_entries++;
Ollie Lho184a4042005-11-26 21:55:36 +000086 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000087
Nico Huber3a9939b2016-04-27 15:56:14 +020088 for (i = 0; i < layout.num_entries; i++) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000089 msg_gdbg("romlayout %08x - %08x named %s\n",
Nico Huber3a9939b2016-04-27 15:56:14 +020090 layout.entries[i].start,
91 layout.entries[i].end, layout.entries[i].name);
Ollie Lho184a4042005-11-26 21:55:36 +000092 }
93
Stefan Tauner16687702015-12-25 21:59:45 +000094 (void)fclose(romlayout);
Uwe Hermannffec5f32007-08-23 16:08:21 +000095
Uwe Hermanna7e05482007-05-09 10:17:44 +000096 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +000097}
Patrick Georgia9095a92010-09-30 17:03:32 +000098#endif
Ollie Lho184a4042005-11-26 21:55:36 +000099
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000100/* returns the index of the entry (or a negative value if it is not found) */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000101static int find_include_arg(const char *const name)
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000102{
103 unsigned int i;
104 for (i = 0; i < num_include_args; i++) {
105 if (!strcmp(include_args[i], name))
106 return i;
107 }
108 return -1;
109}
110
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000111/* register an include argument (-i) for later processing */
112int register_include_arg(char *name)
113{
114 if (num_include_args >= MAX_ROMLAYOUT) {
115 msg_gerr("Too many regions included (%i).\n", num_include_args);
116 return 1;
117 }
118
119 if (name == NULL) {
120 msg_gerr("<NULL> is a bad region name.\n");
121 return 1;
122 }
123
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000124 if (find_include_arg(name) != -1) {
125 msg_gerr("Duplicate region name: \"%s\".\n", name);
126 return 1;
127 }
128
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000129 include_args[num_include_args] = name;
130 num_include_args++;
131 return 0;
132}
133
134/* returns the index of the entry (or a negative value if it is not found) */
135static int find_romentry(char *name)
Ollie Lho184a4042005-11-26 21:55:36 +0000136{
137 int i;
138
Nico Huber3a9939b2016-04-27 15:56:14 +0200139 if (layout.num_entries == 0)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000140 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000141
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000142 msg_gspew("Looking for region \"%s\"... ", name);
Nico Huber3a9939b2016-04-27 15:56:14 +0200143 for (i = 0; i < layout.num_entries; i++) {
144 if (!strcmp(layout.entries[i].name, name)) {
145 layout.entries[i].included = 1;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000146 msg_gspew("found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000147 return i;
148 }
149 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000150 msg_gspew("not found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000151 return -1;
152}
153
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000154/* process -i arguments
155 * returns 0 to indicate success, >0 to indicate failure
156 */
157int process_include_args(void)
158{
159 int i;
160 unsigned int found = 0;
161
162 if (num_include_args == 0)
163 return 0;
164
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000165 /* User has specified an area, but no layout file is loaded. */
Nico Huber3a9939b2016-04-27 15:56:14 +0200166 if (layout.num_entries == 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000167 msg_gerr("Region requested (with -i \"%s\"), "
168 "but no layout data is available.\n",
169 include_args[0]);
170 return 1;
171 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000172
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000173 for (i = 0; i < num_include_args; i++) {
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000174 if (find_romentry(include_args[i]) < 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000175 msg_gerr("Invalid region specified: \"%s\".\n",
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000176 include_args[i]);
177 return 1;
178 }
179 found++;
180 }
181
182 msg_ginfo("Using region%s: \"%s\"", num_include_args > 1 ? "s" : "",
183 include_args[0]);
184 for (i = 1; i < num_include_args; i++)
185 msg_ginfo(", \"%s\"", include_args[i]);
186 msg_ginfo(".\n");
187 return 0;
188}
189
Stefan Tauner949ccc82013-09-15 14:01:06 +0000190void layout_cleanup(void)
191{
192 int i;
193 for (i = 0; i < num_include_args; i++) {
194 free(include_args[i]);
195 include_args[i] = NULL;
196 }
197 num_include_args = 0;
198
Nico Huber3a9939b2016-04-27 15:56:14 +0200199 for (i = 0; i < layout.num_entries; i++) {
200 layout.entries[i].included = 0;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000201 }
Nico Huber3a9939b2016-04-27 15:56:14 +0200202 layout.num_entries = 0;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000203}
204
Nico Huber3a9939b2016-04-27 15:56:14 +0200205struct romentry *get_next_included_romentry(unsigned int start)
Ollie Lho184a4042005-11-26 21:55:36 +0000206{
207 int i;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000208 unsigned int best_start = UINT_MAX;
Nico Huber3a9939b2016-04-27 15:56:14 +0200209 struct romentry *best_entry = NULL;
210 struct romentry *cur;
Ollie Lho184a4042005-11-26 21:55:36 +0000211
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000212 /* First come, first serve for overlapping regions. */
Nico Huber3a9939b2016-04-27 15:56:14 +0200213 for (i = 0; i < layout.num_entries; i++) {
214 cur = &layout.entries[i];
Stefan Tauner104b0d92011-12-25 09:07:59 +0000215 if (!cur->included)
Ollie Lho184a4042005-11-26 21:55:36 +0000216 continue;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000217 /* Already past the current entry? */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000218 if (start > cur->end)
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000219 continue;
220 /* Inside the current entry? */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000221 if (start >= cur->start)
222 return cur;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000223 /* Entry begins after start. */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000224 if (best_start > cur->start) {
225 best_start = cur->start;
226 best_entry = cur;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000227 }
Ollie Lho184a4042005-11-26 21:55:36 +0000228 }
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000229 return best_entry;
230}
Ollie Lho184a4042005-11-26 21:55:36 +0000231
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000232/* Validate and - if needed - normalize layout entries. */
233int normalize_romentries(const struct flashctx *flash)
234{
235 chipsize_t total_size = flash->chip->total_size * 1024;
236 int ret = 0;
237
238 int i;
Nico Huber3a9939b2016-04-27 15:56:14 +0200239 for (i = 0; i < layout.num_entries; i++) {
240 if (layout.entries[i].start >= total_size || layout.entries[i].end >= total_size) {
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000241 msg_gwarn("Warning: Address range of region \"%s\" exceeds the current chip's "
Nico Huber3a9939b2016-04-27 15:56:14 +0200242 "address space.\n", layout.entries[i].name);
243 if (layout.entries[i].included)
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000244 ret = 1;
245 }
Nico Huber3a9939b2016-04-27 15:56:14 +0200246 if (layout.entries[i].start > layout.entries[i].end) {
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000247 msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n",
Nico Huber3a9939b2016-04-27 15:56:14 +0200248 layout.entries[i].name);
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000249 ret = 1;
250 }
251 }
252
253 return ret;
254}
255
Stefan Tauner73f5bda2014-10-19 07:53:45 +0000256static int copy_old_content(struct flashctx *flash, int oldcontents_valid, uint8_t *oldcontents, uint8_t *newcontents, unsigned int start, unsigned int size)
257{
258 if (!oldcontents_valid) {
259 /* oldcontents is a zero-filled buffer. By reading the current data into oldcontents here, we
260 * avoid a rewrite of identical regions even if an initial full chip read didn't happen. */
261 msg_gdbg2("Read a chunk starting at 0x%06x (len=0x%06x).\n", start, size);
262 int ret = flash->chip->read(flash, oldcontents + start, start, size);
263 if (ret != 0) {
264 msg_gerr("Failed to read chunk 0x%06x-0x%06x.\n", start, start + size - 1);
265 return 1;
266 }
267 }
268 memcpy(newcontents + start, oldcontents + start, size);
269 return 0;
270}
271
272/**
273 * Modify @newcontents so that it contains the data that should be on the chip eventually. In the case the user
274 * wants to update only parts of it, copy the chunks to be preserved from @oldcontents to @newcontents. If
275 * @oldcontents is not valid, we need to fetch the current data from the chip first.
276 */
277int build_new_image(struct flashctx *flash, bool oldcontents_valid, uint8_t *oldcontents, uint8_t *newcontents)
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000278{
279 unsigned int start = 0;
Nico Huber3a9939b2016-04-27 15:56:14 +0200280 struct romentry *entry;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000281 unsigned int size = flash->chip->total_size * 1024;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000282
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000283 /* If no regions were specified for inclusion, assume
284 * that the user wants to write the complete new image.
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000285 */
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000286 if (num_include_args == 0)
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000287 return 0;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000288
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000289 /* Non-included romentries are ignored.
290 * The union of all included romentries is used from the new image.
291 */
292 while (start < size) {
Stefan Tauner104b0d92011-12-25 09:07:59 +0000293 entry = get_next_included_romentry(start);
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000294 /* No more romentries for remaining region? */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000295 if (!entry) {
Stefan Tauner73f5bda2014-10-19 07:53:45 +0000296 copy_old_content(flash, oldcontents_valid, oldcontents, newcontents, start,
297 size - start);
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000298 break;
299 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000300 /* For non-included region, copy from old content. */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000301 if (entry->start > start)
Stefan Tauner73f5bda2014-10-19 07:53:45 +0000302 copy_old_content(flash, oldcontents_valid, oldcontents, newcontents, start,
303 entry->start - start);
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000304 /* Skip to location after current romentry. */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000305 start = entry->end + 1;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000306 /* Catch overflow. */
307 if (!start)
308 break;
309 }
Ollie Lho184a4042005-11-26 21:55:36 +0000310 return 0;
311}