blob: 4d1dd560f1cf6bdf4b87e439f0e33fd69d3c67b5 [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.
Uwe Hermann75f51072008-03-04 16:29:54 +000016 */
17
Daniel Campelloce983bc2021-04-16 15:26:42 -060018#include <ctype.h>
Nico Huber70461a92019-06-15 14:56:19 +020019#include <errno.h>
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000020#include <stdio.h>
Ollie Lho184a4042005-11-26 21:55:36 +000021#include <stdlib.h>
22#include <string.h>
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +000023#include <limits.h>
Uwe Hermann0846f892007-08-23 13:34:59 +000024#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000025#include "programmer.h"
Nico Huber3a9939b2016-04-27 15:56:14 +020026#include "layout.h"
Ollie Lho184a4042005-11-26 21:55:36 +000027
Jacob Garberafc3ad62019-06-24 16:05:28 -060028static struct romentry entries[MAX_ROMLAYOUT];
Nico Huber2b94cdb2019-06-15 18:19:26 +020029static struct flashrom_layout global_layout = { entries, 0 };
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000030
Nico Huber305f4172013-06-14 11:55:26 +020031struct flashrom_layout *get_global_layout(void)
Nico Huber3a9939b2016-04-27 15:56:14 +020032{
Nico Huber2b94cdb2019-06-15 18:19:26 +020033 return &global_layout;
Nico Huber3a9939b2016-04-27 15:56:14 +020034}
35
dhendrixbeaefe02017-09-03 18:06:53 -070036const struct flashrom_layout *get_layout(const struct flashrom_flashctx *const flashctx)
37{
38 if (flashctx->layout && flashctx->layout->num_entries)
39 return flashctx->layout;
40 else
41 return &flashctx->fallback_layout.base;
42}
43
Patrick Georgia9095a92010-09-30 17:03:32 +000044#ifndef __LIBPAYLOAD__
Mark Marshallf20b7be2014-05-09 21:16:21 +000045int read_romlayout(const char *name)
Ollie Lho184a4042005-11-26 21:55:36 +000046{
Nico Huber2b94cdb2019-06-15 18:19:26 +020047 struct flashrom_layout *const layout = get_global_layout();
Ollie Lho184a4042005-11-26 21:55:36 +000048 FILE *romlayout;
Nico Huber70461a92019-06-15 14:56:19 +020049 char tempstr[256], tempname[256];
Nico Huber519be662018-12-23 20:03:35 +010050 unsigned int i;
51 int ret = 1;
Ollie Lho184a4042005-11-26 21:55:36 +000052
Uwe Hermanna7e05482007-05-09 10:17:44 +000053 romlayout = fopen(name, "r");
54
55 if (!romlayout) {
Daniel Campello45d50a12021-04-13 10:47:25 -060056 msg_gerr("ERROR: Could not open layout file (%s).\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +000057 name);
Ollie Lho184a4042005-11-26 21:55:36 +000058 return -1;
59 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000060
61 while (!feof(romlayout)) {
Ollie Lho184a4042005-11-26 21:55:36 +000062 char *tstr1, *tstr2;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000063
Nico Huber2b94cdb2019-06-15 18:19:26 +020064 if (layout->num_entries >= MAX_ROMLAYOUT) {
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000065 msg_gerr("Maximum number of ROM images (%i) in layout "
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +000066 "file reached.\n", MAX_ROMLAYOUT);
Nico Huber70461a92019-06-15 14:56:19 +020067 goto _close_ret;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000068 }
Nico Huber70461a92019-06-15 14:56:19 +020069 if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, tempname))
Peter Stuge1fec0f32009-01-12 21:00:35 +000070 continue;
Ollie Lho184a4042005-11-26 21:55:36 +000071#if 0
72 // fscanf does not like arbitrary comments like that :( later
Uwe Hermanna7e05482007-05-09 10:17:44 +000073 if (tempstr[0] == '#') {
Ollie Lho184a4042005-11-26 21:55:36 +000074 continue;
75 }
76#endif
Uwe Hermanna7e05482007-05-09 10:17:44 +000077 tstr1 = strtok(tempstr, ":");
78 tstr2 = strtok(NULL, ":");
Uwe Hermann58783e32008-12-22 16:42:59 +000079 if (!tstr1 || !tstr2) {
Stefan Taunereb582572012-09-21 12:52:50 +000080 msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr);
Nico Huber70461a92019-06-15 14:56:19 +020081 goto _close_ret;
Uwe Hermann58783e32008-12-22 16:42:59 +000082 }
Nico Huber2b94cdb2019-06-15 18:19:26 +020083 layout->entries[layout->num_entries].start = strtol(tstr1, (char **)NULL, 16);
84 layout->entries[layout->num_entries].end = strtol(tstr2, (char **)NULL, 16);
Edward O'Callaghan358a3502020-12-19 11:21:49 +110085 layout->entries[layout->num_entries].included = false;
Daniel Campello45d50a12021-04-13 10:47:25 -060086 layout->entries[layout->num_entries].file = NULL;
Nico Huber70461a92019-06-15 14:56:19 +020087 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 }
Nico Huber2b94cdb2019-06-15 18:19:26 +020092 layout->num_entries++;
Ollie Lho184a4042005-11-26 21:55:36 +000093 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000094
Nico Huber2b94cdb2019-06-15 18:19:26 +020095 for (i = 0; i < layout->num_entries; i++) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000096 msg_gdbg("romlayout %08x - %08x named %s\n",
Nico Huber2b94cdb2019-06-15 18:19:26 +020097 layout->entries[i].start,
98 layout->entries[i].end, layout->entries[i].name);
Ollie Lho184a4042005-11-26 21:55:36 +000099 }
100
Nico Huber70461a92019-06-15 14:56:19 +0200101 ret = 0;
Uwe Hermannffec5f32007-08-23 16:08:21 +0000102
Nico Huber70461a92019-06-15 14:56:19 +0200103_close_ret:
104 (void)fclose(romlayout);
105 return ret;
Ollie Lho184a4042005-11-26 21:55:36 +0000106}
Patrick Georgia9095a92010-09-30 17:03:32 +0000107#endif
Ollie Lho184a4042005-11-26 21:55:36 +0000108
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000109/* register an include argument (-i) for later processing */
Daniel Campello45d50a12021-04-13 10:47:25 -0600110int register_include_arg(struct layout_include_args **args, const char *arg)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000111{
Arthur Heymansb04fef92019-02-05 17:35:05 +0100112 struct layout_include_args *tmp;
Daniel Campello45d50a12021-04-13 10:47:25 -0600113 char *colon;
114 char *name;
115 char *file;
116
117 if (arg == NULL) {
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000118 msg_gerr("<NULL> is a bad region name.\n");
119 return 1;
120 }
121
Daniel Campello45d50a12021-04-13 10:47:25 -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) {
Arthur Heymansb04fef92019-02-05 17:35:05 +0100132 if (!strcmp(tmp->name, name)) {
133 msg_gerr("Duplicate region name: \"%s\".\n", name);
Daniel Campello45d50a12021-04-13 10:47:25 -0600134 goto error;
Arthur Heymansb04fef92019-02-05 17:35:05 +0100135 }
Arthur Heymansb04fef92019-02-05 17:35:05 +0100136 }
137
138 tmp = malloc(sizeof(struct layout_include_args));
139 if (tmp == NULL) {
140 msg_gerr("Could not allocate memory");
Daniel Campello45d50a12021-04-13 10:47:25 -0600141 goto error;
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000142 }
143
Arthur Heymansb04fef92019-02-05 17:35:05 +0100144 tmp->name = name;
Daniel Campello45d50a12021-04-13 10:47:25 -0600145 tmp->file = file;
Arthur Heymansb04fef92019-02-05 17:35:05 +0100146 tmp->next = *args;
147 *args = tmp;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000148 return 0;
Daniel Campello45d50a12021-04-13 10:47:25 -0600149
150error:
151 free(name);
152 free(file);
153 return 1;
154}
155
156/* 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)
159{
160 size_t i;
161 for (i = 0; i < l->num_entries; ++i) {
162 if (!strcmp(l->entries[i].name, name)) {
163 l->entries[i].included = true;
164 if (file)
165 l->entries[i].file = strdup(file);
166 return 0;
167 }
168 }
169 return 1;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000170}
171
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100172/* returns -1 if an entry is not found, 0 if found. */
Daniel Campello45d50a12021-04-13 10:47:25 -0600173static int find_romentry(struct flashrom_layout *const l, char *name, char *file)
Ollie Lho184a4042005-11-26 21:55:36 +0000174{
Nico Huber305f4172013-06-14 11:55:26 +0200175 if (l->num_entries == 0)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000176 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000177
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000178 msg_gspew("Looking for region \"%s\"... ", name);
Daniel Campello45d50a12021-04-13 10:47:25 -0600179 if (include_region(l, name, file)) {
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100180 msg_gspew("not found.\n");
181 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000182 }
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100183 msg_gspew("found.\n");
184 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000185}
186
Daniel Campello7367f772021-04-19 18:43:09 -0600187int get_region_range(struct flashrom_layout *const l, const char *name,
188 unsigned int *start, unsigned int *len)
189{
190 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;
199}
200
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000201/* process -i arguments
202 * returns 0 to indicate success, >0 to indicate failure
203 */
Arthur Heymansb04fef92019-02-05 17:35:05 +0100204int process_include_args(struct flashrom_layout *l, const struct layout_include_args *const args)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000205{
Nico Huber519be662018-12-23 20:03:35 +0100206 unsigned int found = 0;
Arthur Heymansb04fef92019-02-05 17:35:05 +0100207 const struct layout_include_args *tmp;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000208
Arthur Heymansb04fef92019-02-05 17:35:05 +0100209 if (args == NULL)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000210 return 0;
211
Daniel Campello45d50a12021-04-13 10:47:25 -0600212 /* User has specified an include argument, but no layout is loaded. */
Nico Huber305f4172013-06-14 11:55:26 +0200213 if (l->num_entries == 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000214 msg_gerr("Region requested (with -i \"%s\"), "
215 "but no layout data is available.\n",
Arthur Heymansb04fef92019-02-05 17:35:05 +0100216 args->name);
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000217 return 1;
218 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000219
Arthur Heymansb04fef92019-02-05 17:35:05 +0100220 tmp = args;
221 while (tmp) {
Daniel Campello45d50a12021-04-13 10:47:25 -0600222 if (find_romentry(l, tmp->name, tmp->file) < 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000223 msg_gerr("Invalid region specified: \"%s\".\n",
Arthur Heymansb04fef92019-02-05 17:35:05 +0100224 tmp->name);
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000225 return 1;
226 }
Arthur Heymansb04fef92019-02-05 17:35:05 +0100227 tmp = tmp->next;
228 found++;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000229 }
230
Daniel Campello45d50a12021-04-13 10:47:25 -0600231 msg_ginfo("Using region%s: ", found > 1 ? "s" : "");
Arthur Heymansb04fef92019-02-05 17:35:05 +0100232 tmp = args;
233 while (tmp) {
Daniel Campello45d50a12021-04-13 10:47:25 -0600234 msg_ginfo("\"%s\"", tmp->name);
235 if (tmp->file)
236 msg_ginfo(":\"%s\"", tmp->file);
237 if (found > 1)
238 msg_ginfo(", ");
Arthur Heymansb04fef92019-02-05 17:35:05 +0100239 found--;
240 tmp = tmp->next;
241 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000242 msg_ginfo(".\n");
243 return 0;
244}
245
Daniel Campello45d50a12021-04-13 10:47:25 -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
Arthur Heymansb04fef92019-02-05 17:35:05 +0100279void layout_cleanup(struct layout_include_args **args)
Stefan Tauner949ccc82013-09-15 14:01:06 +0000280{
Nico Huber2b94cdb2019-06-15 18:19:26 +0200281 struct flashrom_layout *const layout = get_global_layout();
Nico Huber519be662018-12-23 20:03:35 +0100282 unsigned int i;
Arthur Heymansb04fef92019-02-05 17:35:05 +0100283 struct layout_include_args *tmp;
284
285 while (*args) {
286 tmp = (*args)->next;
Daniel Campello45d50a12021-04-13 10:47:25 -0600287 free((*args)->name);
288 free((*args)->file);
Arthur Heymansb04fef92019-02-05 17:35:05 +0100289 free(*args);
290 *args = tmp;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000291 }
Stefan Tauner949ccc82013-09-15 14:01:06 +0000292
Nico Huber2b94cdb2019-06-15 18:19:26 +0200293 for (i = 0; i < layout->num_entries; i++) {
Nico Huber70461a92019-06-15 14:56:19 +0200294 free(layout->entries[i].name);
Daniel Campello45d50a12021-04-13 10:47:25 -0600295 free(layout->entries[i].file);
Edward O'Callaghan358a3502020-12-19 11:21:49 +1100296 layout->entries[i].included = false;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000297 }
Nico Huber2b94cdb2019-06-15 18:19:26 +0200298 layout->num_entries = 0;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000299}
300
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000301/* Validate and - if needed - normalize layout entries. */
302int normalize_romentries(const struct flashctx *flash)
303{
Nico Huber2b94cdb2019-06-15 18:19:26 +0200304 struct flashrom_layout *const layout = get_global_layout();
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000305 chipsize_t total_size = flash->chip->total_size * 1024;
306 int ret = 0;
307
Nico Huber519be662018-12-23 20:03:35 +0100308 unsigned int i;
Nico Huber2b94cdb2019-06-15 18:19:26 +0200309 for (i = 0; i < layout->num_entries; i++) {
310 if (layout->entries[i].start >= total_size || layout->entries[i].end >= total_size) {
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000311 msg_gwarn("Warning: Address range of region \"%s\" exceeds the current chip's "
Nico Huber2b94cdb2019-06-15 18:19:26 +0200312 "address space.\n", layout->entries[i].name);
313 if (layout->entries[i].included)
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000314 ret = 1;
315 }
Nico Huber2b94cdb2019-06-15 18:19:26 +0200316 if (layout->entries[i].start > layout->entries[i].end) {
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000317 msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n",
Nico Huber2b94cdb2019-06-15 18:19:26 +0200318 layout->entries[i].name);
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000319 ret = 1;
320 }
321 }
322
323 return ret;
324}
Nico Huber3d7b1e32018-12-22 00:53:14 +0100325
Daniel Campelloce983bc2021-04-16 15:26:42 -0600326void prepare_layout_for_extraction(struct flashctx *flash)
327{
328 const struct flashrom_layout *const l = get_layout(flash);
329 unsigned int i, j;
330
331 for (i = 0; i < l->num_entries; ++i) {
332 l->entries[i].included = true;
333
334 if (!l->entries[i].file)
335 l->entries[i].file = strdup(l->entries[i].name);
336
337 for (j = 0; l->entries[i].file[j]; j++) {
338 if (isspace(l->entries[i].file[j]))
339 l->entries[i].file[j] = '_';
340 }
341 }
342}
343
Nico Huber3d7b1e32018-12-22 00:53:14 +0100344const 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}
Nico Huber4f213282019-06-15 17:33:49 +0200361
Nico Huber5ca55232019-06-15 22:29:08 +0200362const 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
Nico Huber4f213282019-06-15 17:33:49 +0200380/**
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 *
391 * @return 0 on success,
392 * 1 if the given name can't be found.
393 */
394int flashrom_layout_include_region(struct flashrom_layout *const layout, const char *name)
395{
Daniel Campello45d50a12021-04-13 10:47:25 -0600396 return include_region(layout, name, NULL);
Nico Huber4f213282019-06-15 17:33:49 +0200397}
398
399/**
400 * @brief Free a layout.
401 *
402 * @param layout Layout to free.
403 */
404void flashrom_layout_release(struct flashrom_layout *const layout)
405{
Nico Huber70461a92019-06-15 14:56:19 +0200406 unsigned int i;
407
408 if (!layout || layout == get_global_layout())
Nico Huber4f213282019-06-15 17:33:49 +0200409 return;
410
Daniel Campello45d50a12021-04-13 10:47:25 -0600411 for (i = 0; i < layout->num_entries; ++i) {
Nico Huber70461a92019-06-15 14:56:19 +0200412 free(layout->entries[i].name);
Daniel Campello45d50a12021-04-13 10:47:25 -0600413 free(layout->entries[i].file);
414 }
Nico Huber4f213282019-06-15 17:33:49 +0200415 free(layout);
416}
417
418/** @} */ /* end flashrom-layout */