blob: 4d1dd560f1cf6bdf4b87e439f0e33fd69d3c67b5 [file] [log] [blame]
uwef562d972008-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)
Edward O'Callaghan627ca372019-09-07 18:52:30 +10006 * Copyright (C) 2011-2013 Stefan Tauner
uwef562d972008-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.
uwef562d972008-03-04 16:29:54 +000016 */
17
Daniel Campello83752f82021-04-16 14:54:27 -060018#include <ctype.h>
Edward O'Callaghan9494cba2020-07-24 22:35:22 +100019#include <errno.h>
hailfingera83a5fe2010-05-30 22:24:40 +000020#include <stdio.h>
ollie6a600992005-11-26 21:55:36 +000021#include <stdlib.h>
22#include <string.h>
hailfinger05857ac2010-11-02 03:12:51 +000023#include <limits.h>
uwe4529d202007-08-23 13:34:59 +000024#include "flash.h"
hailfinger428f6852010-07-27 22:41:39 +000025#include "programmer.h"
Edward O'Callaghan8bc8b092020-12-04 15:31:56 +110026#include "layout.h"
Jack Rosenthal8fd4bc42020-05-29 14:55:41 -060027
Edward O'Callaghan2520fac2019-06-24 19:10:49 +100028static struct romentry entries[MAX_ROMLAYOUT];
Edward O'Callaghanffb17a32019-09-07 19:00:33 +100029static struct flashrom_layout global_layout = { entries, 0 };
ollie6a600992005-11-26 21:55:36 +000030
Edward O'Callaghanffb17a32019-09-07 19:00:33 +100031struct flashrom_layout *get_global_layout(void)
Edward O'Callaghan2520fac2019-06-24 19:10:49 +100032{
Edward O'Callaghanffb17a32019-09-07 19:00:33 +100033 return &global_layout;
Edward O'Callaghan2520fac2019-06-24 19:10:49 +100034}
ollie6a600992005-11-26 21:55:36 +000035
Daniel Campello2fdc8372021-04-16 17:52:51 -060036const struct flashrom_layout *get_layout(const struct flashrom_flashctx *const flashctx)
Edward O'Callaghan743904d2020-12-17 13:06:56 +110037{
38 if (flashctx->layout && flashctx->layout->num_entries)
39 return flashctx->layout;
40 else
41 return &flashctx->fallback_layout.base;
42}
43
oxygene50275892010-09-30 17:03:32 +000044#ifndef __LIBPAYLOAD__
Edward O'Callaghan9494cba2020-07-24 22:35:22 +100045int read_romlayout(const char *name)
ollie6a600992005-11-26 21:55:36 +000046{
Edward O'Callaghanffb17a32019-09-07 19:00:33 +100047 struct flashrom_layout *const layout = get_global_layout();
ollie6a600992005-11-26 21:55:36 +000048 FILE *romlayout;
Edward O'Callaghan9494cba2020-07-24 22:35:22 +100049 char tempstr[256], tempname[256];
50 unsigned int i;
51 int ret = 1;
ollie6a600992005-11-26 21:55:36 +000052
uwef6641642007-05-09 10:17:44 +000053 romlayout = fopen(name, "r");
54
55 if (!romlayout) {
Daniel Campello2fdc8372021-04-16 17:52:51 -060056 msg_gerr("ERROR: Could not open layout file (%s).\n",
uwef6641642007-05-09 10:17:44 +000057 name);
ollie6a600992005-11-26 21:55:36 +000058 return -1;
59 }
uwef6641642007-05-09 10:17:44 +000060
61 while (!feof(romlayout)) {
ollie6a600992005-11-26 21:55:36 +000062 char *tstr1, *tstr2;
hailfingercc4459c2010-12-04 11:56:52 +000063
Edward O'Callaghanffb17a32019-09-07 19:00:33 +100064 if (layout->num_entries >= MAX_ROMLAYOUT) {
hailfingercc4459c2010-12-04 11:56:52 +000065 msg_gerr("Maximum number of ROM images (%i) in layout "
Edward O'Callaghan9494cba2020-07-24 22:35:22 +100066 "file reached.\n", MAX_ROMLAYOUT);
67 goto _close_ret;
hailfingercc4459c2010-12-04 11:56:52 +000068 }
Edward O'Callaghan9494cba2020-07-24 22:35:22 +100069 if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, tempname))
stugebbcc2f12009-01-12 21:00:35 +000070 continue;
ollie6a600992005-11-26 21:55:36 +000071#if 0
72 // fscanf does not like arbitrary comments like that :( later
uwef6641642007-05-09 10:17:44 +000073 if (tempstr[0] == '#') {
ollie6a600992005-11-26 21:55:36 +000074 continue;
75 }
76#endif
uwef6641642007-05-09 10:17:44 +000077 tstr1 = strtok(tempstr, ":");
78 tstr2 = strtok(NULL, ":");
uwe3aee1dd2008-12-22 16:42:59 +000079 if (!tstr1 || !tstr2) {
Edward O'Callaghan9494cba2020-07-24 22:35:22 +100080 msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr);
81 goto _close_ret;
uwe3aee1dd2008-12-22 16:42:59 +000082 }
Edward O'Callaghanffb17a32019-09-07 19:00:33 +100083 layout->entries[layout->num_entries].start = strtol(tstr1, (char **)NULL, 16);
84 layout->entries[layout->num_entries].end = strtol(tstr2, (char **)NULL, 16);
Daniel Campello2fdc8372021-04-16 17:52:51 -060085 layout->entries[layout->num_entries].included = false;
Edward O'Callaghan8b0f1772020-12-16 20:19:08 +110086 layout->entries[layout->num_entries].file = NULL;
Edward O'Callaghan9494cba2020-07-24 22:35:22 +100087 layout->entries[layout->num_entries].name = strdup(tempname);
88 if (!layout->entries[layout->num_entries].name) {
89 msg_gerr("Error adding layout entry: %s\n", strerror(errno));
90 goto _close_ret;
91 }
Edward O'Callaghanffb17a32019-09-07 19:00:33 +100092 layout->num_entries++;
ollie6a600992005-11-26 21:55:36 +000093 }
uwef6641642007-05-09 10:17:44 +000094
Edward O'Callaghanffb17a32019-09-07 19:00:33 +100095 for (i = 0; i < layout->num_entries; i++) {
hailfingera83a5fe2010-05-30 22:24:40 +000096 msg_gdbg("romlayout %08x - %08x named %s\n",
Edward O'Callaghanffb17a32019-09-07 19:00:33 +100097 layout->entries[i].start,
98 layout->entries[i].end, layout->entries[i].name);
ollie6a600992005-11-26 21:55:36 +000099 }
100
Edward O'Callaghan9494cba2020-07-24 22:35:22 +1000101 ret = 0;
uwebe4477b2007-08-23 16:08:21 +0000102
Edward O'Callaghan9494cba2020-07-24 22:35:22 +1000103_close_ret:
104 (void)fclose(romlayout);
105 return ret;
ollie6a600992005-11-26 21:55:36 +0000106}
oxygene50275892010-09-30 17:03:32 +0000107#endif
ollie6a600992005-11-26 21:55:36 +0000108
David Hendricksd0ea9ed2011-03-04 17:31:57 -0800109/* register an include argument (-i) for later processing */
Daniel Campello2fdc8372021-04-16 17:52:51 -0600110int register_include_arg(struct layout_include_args **args, const char *arg)
David Hendricksd0ea9ed2011-03-04 17:31:57 -0800111{
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100112 struct layout_include_args *tmp;
Daniel Campello2fdc8372021-04-16 17:52:51 -0600113 char *colon;
114 char *name;
115 char *file;
116
117 if (arg == NULL) {
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100118 msg_gerr("<NULL> is a bad region name.\n");
119 return 1;
David Hendricksd0ea9ed2011-03-04 17:31:57 -0800120 }
121
Daniel Campello2fdc8372021-04-16 17:52:51 -0600122 /* -i <image>[:<file>] */
123 colon = strchr(arg, ':');
124 if (colon && !colon[1]) {
125 msg_gerr("Missing filename parameter in %s\n", arg);
126 return 1;
127 }
128 name = colon ? strndup(arg, colon - arg) : strdup(arg);
129 file = colon ? strdup(colon + 1) : NULL;
130
131 for (tmp = *args; tmp; tmp = tmp->next) {
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100132 if (!strcmp(tmp->name, name)) {
133 msg_gerr("Duplicate region name: \"%s\".\n", name);
Daniel Campello2fdc8372021-04-16 17:52:51 -0600134 goto error;
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100135 }
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100136 }
137
138 tmp = malloc(sizeof(struct layout_include_args));
139 if (tmp == NULL) {
140 msg_gerr("Could not allocate memory");
Daniel Campello2fdc8372021-04-16 17:52:51 -0600141 goto error;
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100142 }
143
Jack Rosenthal14f24e52021-04-16 23:22:04 +0000144 tmp->name = name;
Daniel Campello2fdc8372021-04-16 17:52:51 -0600145 tmp->file = file;
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100146 tmp->next = *args;
147 *args = tmp;
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100148 return 0;
Daniel Campello2fdc8372021-04-16 17:52:51 -0600149
150error:
151 free(name);
152 free(file);
153 return 1;
David Hendricksd0ea9ed2011-03-04 17:31:57 -0800154}
155
Daniel Campello2fdc8372021-04-16 17:52:51 -0600156/* returns 0 to indicate success, 1 to indicate failure */
157static int include_region(struct flashrom_layout *const l, const char *name,
158 const char *file)
Daniel Campello30efd0e2021-03-19 13:23:42 -0600159{
160 size_t i;
Daniel Campello2fdc8372021-04-16 17:52:51 -0600161 for (i = 0; i < l->num_entries; ++i) {
162 if (!strcmp(l->entries[i].name, name)) {
163 l->entries[i].included = true;
Daniel Campello30efd0e2021-03-19 13:23:42 -0600164 if (file)
Daniel Campello2fdc8372021-04-16 17:52:51 -0600165 l->entries[i].file = strdup(file);
166 return 0;
Daniel Campello30efd0e2021-03-19 13:23:42 -0600167 }
168 }
Daniel Campello2fdc8372021-04-16 17:52:51 -0600169 return 1;
Daniel Campello30efd0e2021-03-19 13:23:42 -0600170}
Edward O'Callaghan13e8ed82020-06-01 13:22:19 +1000171
Daniel Campello2fdc8372021-04-16 17:52:51 -0600172/* returns -1 if an entry is not found, 0 if found. */
173static int find_romentry(struct flashrom_layout *const l, char *name, char *file)
ollie6a600992005-11-26 21:55:36 +0000174{
Edward O'Callaghan13e8ed82020-06-01 13:22:19 +1000175 if (l->num_entries == 0)
uwef6641642007-05-09 10:17:44 +0000176 return -1;
ollie6a600992005-11-26 21:55:36 +0000177
Edward O'Callaghan13e8ed82020-06-01 13:22:19 +1000178 msg_gspew("Looking for region \"%s\"... ", name);
Daniel Campello2fdc8372021-04-16 17:52:51 -0600179 if (include_region(l, name, file)) {
Edward O'Callaghan13e8ed82020-06-01 13:22:19 +1000180 msg_gspew("not found.\n");
181 return -1;
ollie6a600992005-11-26 21:55:36 +0000182 }
Edward O'Callaghan13e8ed82020-06-01 13:22:19 +1000183 msg_gspew("found.\n");
Daniel Campello2fdc8372021-04-16 17:52:51 -0600184 return 0;
ollie6a600992005-11-26 21:55:36 +0000185}
186
Daniel Campello2fdc8372021-04-16 17:52:51 -0600187int get_region_range(struct flashrom_layout *const l, const char *name,
188 unsigned int *start, unsigned int *len)
David Hendricksc36685a2016-09-12 20:41:05 -0700189{
Daniel Campello2fdc8372021-04-16 17:52:51 -0600190 size_t i;
191 for (i = 0; i < l->num_entries; ++i) {
192 if (!strcmp(l->entries[i].name, name)) {
193 *start = l->entries[i].start;
194 *len = l->entries[i].end - l->entries[i].start + 1;
195 return 0;
196 }
197 }
198 return 1;
David Hendricksc36685a2016-09-12 20:41:05 -0700199}
200
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100201/* process -i arguments
202 * returns 0 to indicate success, >0 to indicate failure
David Hendricksd0ea9ed2011-03-04 17:31:57 -0800203 */
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100204int process_include_args(struct flashrom_layout *l, const struct layout_include_args *const args)
205{
206 unsigned int found = 0;
207 const struct layout_include_args *tmp;
David Hendricksd0ea9ed2011-03-04 17:31:57 -0800208
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100209 if (args == NULL)
210 return 0;
Louis Yung-Chieh Lo8fc07402011-06-15 15:02:45 +0800211
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100212 /* User has specified an include argument, but no layout is loaded. */
213 if (l->num_entries == 0) {
214 msg_gerr("Region requested (with -i \"%s\"), "
215 "but no layout data is available.\n",
216 args->name);
217 return 1;
David Hendricksd0ea9ed2011-03-04 17:31:57 -0800218 }
219
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100220 tmp = args;
221 while (tmp) {
Daniel Campello2fdc8372021-04-16 17:52:51 -0600222 if (find_romentry(l, tmp->name, tmp->file) < 0) {
223 msg_gerr("Invalid region specified: \"%s\".\n",
224 tmp->name);
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100225 return 1;
226 }
227 tmp = tmp->next;
228 found++;
229 }
230
Daniel Campello2fdc8372021-04-16 17:52:51 -0600231 msg_ginfo("Using region%s: ", found > 1 ? "s" : "");
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100232 tmp = args;
233 while (tmp) {
Daniel Campello2fdc8372021-04-16 17:52:51 -0600234 msg_ginfo("\"%s\"", tmp->name);
235 if (tmp->file)
236 msg_ginfo(":\"%s\"", tmp->file);
237 if (found > 1)
238 msg_ginfo(", ");
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100239 found--;
240 tmp = tmp->next;
241 }
242 msg_ginfo(".\n");
David Hendricksd0ea9ed2011-03-04 17:31:57 -0800243 return 0;
244}
245
Daniel Campello2fdc8372021-04-16 17:52:51 -0600246/* returns boolean 1 if any regions overlap, 0 otherwise */
247int included_regions_overlap(const struct flashrom_layout *const l)
248{
249 size_t i;
250 int overlap_detected = 0;
251
252 for (i = 0; i < l->num_entries; i++) {
253 size_t j;
254
255 if (!l->entries[i].included)
256 continue;
257
258 for (j = i + 1; j < l->num_entries; j++) {
259 if (!l->entries[j].included)
260 continue;
261
262 if (l->entries[i].start > l->entries[j].end)
263 continue;
264
265 if (l->entries[i].end < l->entries[j].start)
266 continue;
267
268 msg_gdbg("Regions %s [0x%08x-0x%08x] and "
269 "%s [0x%08x-0x%08x] overlap\n",
270 l->entries[i].name, l->entries[i].start,
271 l->entries[i].end, l->entries[j].name,
272 l->entries[j].start, l->entries[j].end);
273 overlap_detected = 1;
274 }
275 }
276 return overlap_detected;
277}
278
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100279void layout_cleanup(struct layout_include_args **args)
Edward O'Callaghan627ca372019-09-07 18:52:30 +1000280{
Edward O'Callaghanffb17a32019-09-07 19:00:33 +1000281 struct flashrom_layout *const layout = get_global_layout();
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100282 unsigned int i;
283 struct layout_include_args *tmp;
284
285 while (*args) {
286 tmp = (*args)->next;
Daniel Campello2fdc8372021-04-16 17:52:51 -0600287 free((*args)->name);
288 free((*args)->file);
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100289 free(*args);
290 *args = tmp;
291 }
292
Edward O'Callaghanffb17a32019-09-07 19:00:33 +1000293 for (i = 0; i < layout->num_entries; i++) {
Edward O'Callaghan9494cba2020-07-24 22:35:22 +1000294 free(layout->entries[i].name);
Edward O'Callaghan8b0f1772020-12-16 20:19:08 +1100295 free(layout->entries[i].file);
Daniel Campello2fdc8372021-04-16 17:52:51 -0600296 layout->entries[i].included = false;
Edward O'Callaghan627ca372019-09-07 18:52:30 +1000297 }
Edward O'Callaghanffb17a32019-09-07 19:00:33 +1000298 layout->num_entries = 0;
Edward O'Callaghan627ca372019-09-07 18:52:30 +1000299}
300
Edward O'Callaghana2f3e2a2020-07-26 16:49:30 +1000301/* Validate and - if needed - normalize layout entries. */
302int normalize_romentries(const struct flashctx *flash)
303{
Edward O'Callaghana6a6f7b2020-07-30 22:57:58 +1000304 struct flashrom_layout *const layout = get_global_layout();
Edward O'Callaghana2f3e2a2020-07-26 16:49:30 +1000305 chipsize_t total_size = flash->chip->total_size * 1024;
306 int ret = 0;
Edward O'Callaghana2f3e2a2020-07-26 16:49:30 +1000307
Edward O'Callaghana6a6f7b2020-07-30 22:57:58 +1000308 unsigned int i;
Edward O'Callaghana2f3e2a2020-07-26 16:49:30 +1000309 for (i = 0; i < layout->num_entries; i++) {
Edward O'Callaghana6a6f7b2020-07-30 22:57:58 +1000310 if (layout->entries[i].start >= total_size || layout->entries[i].end >= total_size) {
311 msg_gwarn("Warning: Address range of region \"%s\" exceeds the current chip's "
312 "address space.\n", layout->entries[i].name);
313 if (layout->entries[i].included)
Edward O'Callaghana2f3e2a2020-07-26 16:49:30 +1000314 ret = 1;
315 }
Edward O'Callaghana6a6f7b2020-07-30 22:57:58 +1000316 if (layout->entries[i].start > layout->entries[i].end) {
317 msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n",
318 layout->entries[i].name);
Edward O'Callaghana2f3e2a2020-07-26 16:49:30 +1000319 ret = 1;
320 }
321 }
322
323 return ret;
324}
325
Daniel Campello83752f82021-04-16 14:54:27 -0600326void prepare_layout_for_extraction(struct flashctx *flash)
Simon Glass9ad06c12013-07-03 22:08:17 +0900327{
Daniel Campello83752f82021-04-16 14:54:27 -0600328 const struct flashrom_layout *const l = get_layout(flash);
329 unsigned int i, j;
Simon Glass9ad06c12013-07-03 22:08:17 +0900330
Daniel Campello83752f82021-04-16 14:54:27 -0600331 for (i = 0; i < l->num_entries; ++i) {
332 l->entries[i].included = true;
Simon Glass9ad06c12013-07-03 22:08:17 +0900333
Daniel Campello83752f82021-04-16 14:54:27 -0600334 if (!l->entries[i].file)
335 l->entries[i].file = strdup(l->entries[i].name);
Simon Glass9ad06c12013-07-03 22:08:17 +0900336
Daniel Campello83752f82021-04-16 14:54:27 -0600337 for (j = 0; l->entries[i].file[j]; j++) {
338 if (isspace(l->entries[i].file[j]))
339 l->entries[i].file[j] = '_';
Simon Glass9ad06c12013-07-03 22:08:17 +0900340 }
Simon Glass9ad06c12013-07-03 22:08:17 +0900341 }
Simon Glass9ad06c12013-07-03 22:08:17 +0900342}
Vadim Bendeburycc9577d2018-05-09 11:52:00 -0700343
Edward O'Callaghan4a288872020-12-18 10:26:04 +1100344const struct romentry *layout_next_included_region(
345 const struct flashrom_layout *const l, const chipoff_t where)
346{
347 unsigned int i;
348 const struct romentry *lowest = NULL;
349
350 for (i = 0; i < l->num_entries; ++i) {
351 if (!l->entries[i].included)
352 continue;
353 if (l->entries[i].end < where)
354 continue;
355 if (!lowest || lowest->start > l->entries[i].start)
356 lowest = &l->entries[i];
357 }
358
359 return lowest;
360}
361
362const struct romentry *layout_next_included(
363 const struct flashrom_layout *const layout, const struct romentry *iterator)
364{
365 const struct romentry *const end = layout->entries + layout->num_entries;
366
367 if (iterator)
368 ++iterator;
369 else
370 iterator = &layout->entries[0];
371
372 for (; iterator < end; ++iterator) {
373 if (!iterator->included)
374 continue;
375 return iterator;
376 }
377 return NULL;
378}
379
Edward O'Callaghan13e8ed82020-06-01 13:22:19 +1000380/**
381 * @addtogroup flashrom-layout
382 * @{
383 */
384
385/**
386 * @brief Mark given region as included.
387 *
388 * @param layout The layout to alter.
389 * @param name The name of the region to include.
390 *
Daniel Campello30efd0e2021-03-19 13:23:42 -0600391 * @return 0 on success,
392 * 1 if the given name can't be found.
Edward O'Callaghan13e8ed82020-06-01 13:22:19 +1000393 */
Daniel Campello30efd0e2021-03-19 13:23:42 -0600394int flashrom_layout_include_region(struct flashrom_layout *const layout, const char *name)
Edward O'Callaghan13e8ed82020-06-01 13:22:19 +1000395{
Daniel Campello2fdc8372021-04-16 17:52:51 -0600396 return include_region(layout, name, NULL);
Edward O'Callaghan13e8ed82020-06-01 13:22:19 +1000397}
398
Edward O'Callaghan5d0a4b12020-12-19 11:46:50 +1100399/**
400 * @brief Free a layout.
401 *
402 * @param layout Layout to free.
403 */
404void flashrom_layout_release(struct flashrom_layout *const layout)
405{
406 unsigned int i;
407
408 if (!layout || layout == get_global_layout())
409 return;
410
411 for (i = 0; i < layout->num_entries; ++i) {
412 free(layout->entries[i].name);
413 free(layout->entries[i].file);
414 }
415 free(layout);
416}
417
Edward O'Callaghan13e8ed82020-06-01 13:22:19 +1000418/** @} */ /* end flashrom-layout */