blob: a8d9a32fd1067ffd3b8bfa0338a99ca827e6ce10 [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
Nico Huber70461a92019-06-15 14:56:19 +020018#include <errno.h>
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000019#include <stdio.h>
Ollie Lho184a4042005-11-26 21:55:36 +000020#include <stdlib.h>
21#include <string.h>
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +000022#include <limits.h>
Uwe Hermann0846f892007-08-23 13:34:59 +000023#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000024#include "programmer.h"
Nico Huber3a9939b2016-04-27 15:56:14 +020025#include "layout.h"
Ollie Lho184a4042005-11-26 21:55:36 +000026
Jacob Garberafc3ad62019-06-24 16:05:28 -060027static struct romentry entries[MAX_ROMLAYOUT];
Nico Huber2b94cdb2019-06-15 18:19:26 +020028static struct flashrom_layout global_layout = { entries, 0 };
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000029
Nico Huber305f4172013-06-14 11:55:26 +020030struct flashrom_layout *get_global_layout(void)
Nico Huber3a9939b2016-04-27 15:56:14 +020031{
Nico Huber2b94cdb2019-06-15 18:19:26 +020032 return &global_layout;
Nico Huber3a9939b2016-04-27 15:56:14 +020033}
34
dhendrixbeaefe02017-09-03 18:06:53 -070035const struct flashrom_layout *get_layout(const struct flashrom_flashctx *const flashctx)
36{
37 if (flashctx->layout && flashctx->layout->num_entries)
38 return flashctx->layout;
39 else
40 return &flashctx->fallback_layout.base;
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{
Nico Huber2b94cdb2019-06-15 18:19:26 +020046 struct flashrom_layout *const layout = get_global_layout();
Ollie Lho184a4042005-11-26 21:55:36 +000047 FILE *romlayout;
Nico Huber70461a92019-06-15 14:56:19 +020048 char tempstr[256], tempname[256];
Nico Huber519be662018-12-23 20:03:35 +010049 unsigned int i;
50 int ret = 1;
Ollie Lho184a4042005-11-26 21:55:36 +000051
Uwe Hermanna7e05482007-05-09 10:17:44 +000052 romlayout = fopen(name, "r");
53
54 if (!romlayout) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000055 msg_gerr("ERROR: Could not open ROM layout (%s).\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +000056 name);
Ollie Lho184a4042005-11-26 21:55:36 +000057 return -1;
58 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000059
60 while (!feof(romlayout)) {
Ollie Lho184a4042005-11-26 21:55:36 +000061 char *tstr1, *tstr2;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000062
Nico Huber2b94cdb2019-06-15 18:19:26 +020063 if (layout->num_entries >= MAX_ROMLAYOUT) {
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000064 msg_gerr("Maximum number of ROM images (%i) in layout "
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +000065 "file reached.\n", MAX_ROMLAYOUT);
Nico Huber70461a92019-06-15 14:56:19 +020066 goto _close_ret;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000067 }
Nico Huber70461a92019-06-15 14:56:19 +020068 if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, tempname))
Peter Stuge1fec0f32009-01-12 21:00:35 +000069 continue;
Ollie Lho184a4042005-11-26 21:55:36 +000070#if 0
71 // fscanf does not like arbitrary comments like that :( later
Uwe Hermanna7e05482007-05-09 10:17:44 +000072 if (tempstr[0] == '#') {
Ollie Lho184a4042005-11-26 21:55:36 +000073 continue;
74 }
75#endif
Uwe Hermanna7e05482007-05-09 10:17:44 +000076 tstr1 = strtok(tempstr, ":");
77 tstr2 = strtok(NULL, ":");
Uwe Hermann58783e32008-12-22 16:42:59 +000078 if (!tstr1 || !tstr2) {
Stefan Taunereb582572012-09-21 12:52:50 +000079 msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr);
Nico Huber70461a92019-06-15 14:56:19 +020080 goto _close_ret;
Uwe Hermann58783e32008-12-22 16:42:59 +000081 }
Nico Huber2b94cdb2019-06-15 18:19:26 +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;
Nico Huber70461a92019-06-15 14:56:19 +020085 layout->entries[layout->num_entries].name = strdup(tempname);
86 if (!layout->entries[layout->num_entries].name) {
87 msg_gerr("Error adding layout entry: %s\n", strerror(errno));
88 goto _close_ret;
89 }
Nico Huber2b94cdb2019-06-15 18:19:26 +020090 layout->num_entries++;
Ollie Lho184a4042005-11-26 21:55:36 +000091 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000092
Nico Huber2b94cdb2019-06-15 18:19:26 +020093 for (i = 0; i < layout->num_entries; i++) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000094 msg_gdbg("romlayout %08x - %08x named %s\n",
Nico Huber2b94cdb2019-06-15 18:19:26 +020095 layout->entries[i].start,
96 layout->entries[i].end, layout->entries[i].name);
Ollie Lho184a4042005-11-26 21:55:36 +000097 }
98
Nico Huber70461a92019-06-15 14:56:19 +020099 ret = 0;
Uwe Hermannffec5f32007-08-23 16:08:21 +0000100
Nico Huber70461a92019-06-15 14:56:19 +0200101_close_ret:
102 (void)fclose(romlayout);
103 return ret;
Ollie Lho184a4042005-11-26 21:55:36 +0000104}
Patrick Georgia9095a92010-09-30 17:03:32 +0000105#endif
Ollie Lho184a4042005-11-26 21:55:36 +0000106
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000107/* register an include argument (-i) for later processing */
Arthur Heymansb04fef92019-02-05 17:35:05 +0100108int register_include_arg(struct layout_include_args **args, char *name)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000109{
Arthur Heymansb04fef92019-02-05 17:35:05 +0100110 struct layout_include_args *tmp;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000111 if (name == NULL) {
112 msg_gerr("<NULL> is a bad region name.\n");
113 return 1;
114 }
115
Arthur Heymansb04fef92019-02-05 17:35:05 +0100116 tmp = *args;
117 while (tmp) {
118 if (!strcmp(tmp->name, name)) {
119 msg_gerr("Duplicate region name: \"%s\".\n", name);
120 return 1;
121 }
122 tmp = tmp->next;
123 }
124
Angel Pons690a9442021-06-07 12:33:53 +0200125 tmp = malloc(sizeof(*tmp));
Arthur Heymansb04fef92019-02-05 17:35:05 +0100126 if (tmp == NULL) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100127 msg_gerr("Out of memory");
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000128 return 1;
129 }
130
Arthur Heymansb04fef92019-02-05 17:35:05 +0100131 tmp->name = name;
132 tmp->next = *args;
133 *args = tmp;
134
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000135 return 0;
136}
137
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100138/* returns -1 if an entry is not found, 0 if found. */
Nico Huber305f4172013-06-14 11:55:26 +0200139static int find_romentry(struct flashrom_layout *const l, char *name)
Ollie Lho184a4042005-11-26 21:55:36 +0000140{
Nico Huber305f4172013-06-14 11:55:26 +0200141 if (l->num_entries == 0)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000142 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000143
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000144 msg_gspew("Looking for region \"%s\"... ", name);
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100145 if (flashrom_layout_include_region(l, name)) {
146 msg_gspew("not found.\n");
147 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000148 }
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100149 msg_gspew("found.\n");
150 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000151}
152
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000153/* process -i arguments
154 * returns 0 to indicate success, >0 to indicate failure
155 */
Arthur Heymansb04fef92019-02-05 17:35:05 +0100156int process_include_args(struct flashrom_layout *l, const struct layout_include_args *const args)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000157{
Nico Huber519be662018-12-23 20:03:35 +0100158 unsigned int found = 0;
Arthur Heymansb04fef92019-02-05 17:35:05 +0100159 const struct layout_include_args *tmp;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000160
Arthur Heymansb04fef92019-02-05 17:35:05 +0100161 if (args == NULL)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000162 return 0;
163
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000164 /* User has specified an area, but no layout file is loaded. */
Nico Huber305f4172013-06-14 11:55:26 +0200165 if (l->num_entries == 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000166 msg_gerr("Region requested (with -i \"%s\"), "
167 "but no layout data is available.\n",
Arthur Heymansb04fef92019-02-05 17:35:05 +0100168 args->name);
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000169 return 1;
170 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000171
Arthur Heymansb04fef92019-02-05 17:35:05 +0100172 tmp = args;
173 while (tmp) {
174 if (find_romentry(l, tmp->name) < 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000175 msg_gerr("Invalid region specified: \"%s\".\n",
Arthur Heymansb04fef92019-02-05 17:35:05 +0100176 tmp->name);
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000177 return 1;
178 }
Arthur Heymansb04fef92019-02-05 17:35:05 +0100179 tmp = tmp->next;
180 found++;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000181 }
182
Arthur Heymansb04fef92019-02-05 17:35:05 +0100183 msg_ginfo("Using region%s:", found > 1 ? "s" : "");
184 tmp = args;
185 while (tmp) {
186 msg_ginfo(" \"%s\"%s", tmp->name, found > 1 ? "," : "");
187 found--;
188 tmp = tmp->next;
189 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000190 msg_ginfo(".\n");
191 return 0;
192}
193
Arthur Heymansb04fef92019-02-05 17:35:05 +0100194void layout_cleanup(struct layout_include_args **args)
Stefan Tauner949ccc82013-09-15 14:01:06 +0000195{
Nico Huber2b94cdb2019-06-15 18:19:26 +0200196 struct flashrom_layout *const layout = get_global_layout();
Nico Huber519be662018-12-23 20:03:35 +0100197 unsigned int i;
Arthur Heymansb04fef92019-02-05 17:35:05 +0100198 struct layout_include_args *tmp;
199
200 while (*args) {
201 tmp = (*args)->next;
202 free(*args);
203 *args = tmp;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000204 }
Stefan Tauner949ccc82013-09-15 14:01:06 +0000205
Nico Huber2b94cdb2019-06-15 18:19:26 +0200206 for (i = 0; i < layout->num_entries; i++) {
Nico Huber70461a92019-06-15 14:56:19 +0200207 free(layout->entries[i].name);
Nico Huber2b94cdb2019-06-15 18:19:26 +0200208 layout->entries[i].included = 0;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000209 }
Nico Huber2b94cdb2019-06-15 18:19:26 +0200210 layout->num_entries = 0;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000211}
212
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000213/* Validate and - if needed - normalize layout entries. */
214int normalize_romentries(const struct flashctx *flash)
215{
Nico Huber2b94cdb2019-06-15 18:19:26 +0200216 struct flashrom_layout *const layout = get_global_layout();
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000217 chipsize_t total_size = flash->chip->total_size * 1024;
218 int ret = 0;
219
Nico Huber519be662018-12-23 20:03:35 +0100220 unsigned int i;
Nico Huber2b94cdb2019-06-15 18:19:26 +0200221 for (i = 0; i < layout->num_entries; i++) {
222 if (layout->entries[i].start >= total_size || layout->entries[i].end >= total_size) {
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000223 msg_gwarn("Warning: Address range of region \"%s\" exceeds the current chip's "
Nico Huber2b94cdb2019-06-15 18:19:26 +0200224 "address space.\n", layout->entries[i].name);
225 if (layout->entries[i].included)
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000226 ret = 1;
227 }
Nico Huber2b94cdb2019-06-15 18:19:26 +0200228 if (layout->entries[i].start > layout->entries[i].end) {
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000229 msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n",
Nico Huber2b94cdb2019-06-15 18:19:26 +0200230 layout->entries[i].name);
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000231 ret = 1;
232 }
233 }
234
235 return ret;
236}
Nico Huber3d7b1e32018-12-22 00:53:14 +0100237
238const struct romentry *layout_next_included_region(
239 const struct flashrom_layout *const l, const chipoff_t where)
240{
241 unsigned int i;
242 const struct romentry *lowest = NULL;
243
244 for (i = 0; i < l->num_entries; ++i) {
245 if (!l->entries[i].included)
246 continue;
247 if (l->entries[i].end < where)
248 continue;
249 if (!lowest || lowest->start > l->entries[i].start)
250 lowest = &l->entries[i];
251 }
252
253 return lowest;
254}
Nico Huber4f213282019-06-15 17:33:49 +0200255
Nico Huber5ca55232019-06-15 22:29:08 +0200256const struct romentry *layout_next_included(
257 const struct flashrom_layout *const layout, const struct romentry *iterator)
258{
259 const struct romentry *const end = layout->entries + layout->num_entries;
260
261 if (iterator)
262 ++iterator;
263 else
264 iterator = &layout->entries[0];
265
266 for (; iterator < end; ++iterator) {
267 if (!iterator->included)
268 continue;
269 return iterator;
270 }
271 return NULL;
272}
273
Nico Huber4f213282019-06-15 17:33:49 +0200274/**
275 * @addtogroup flashrom-layout
276 * @{
277 */
278
279/**
280 * @brief Mark given region as included.
281 *
282 * @param layout The layout to alter.
283 * @param name The name of the region to include.
284 *
285 * @return 0 on success,
286 * 1 if the given name can't be found.
287 */
288int flashrom_layout_include_region(struct flashrom_layout *const layout, const char *name)
289{
290 size_t i;
291 for (i = 0; i < layout->num_entries; ++i) {
292 if (!strcmp(layout->entries[i].name, name)) {
293 layout->entries[i].included = true;
294 return 0;
295 }
296 }
297 return 1;
298}
299
300/**
301 * @brief Free a layout.
302 *
303 * @param layout Layout to free.
304 */
305void flashrom_layout_release(struct flashrom_layout *const layout)
306{
Nico Huber70461a92019-06-15 14:56:19 +0200307 unsigned int i;
308
309 if (!layout || layout == get_global_layout())
Nico Huber4f213282019-06-15 17:33:49 +0200310 return;
311
Nico Huber70461a92019-06-15 14:56:19 +0200312 for (i = 0; i < layout->num_entries; ++i)
313 free(layout->entries[i].name);
Nico Huber4f213282019-06-15 17:33:49 +0200314 free(layout);
315}
316
317/** @} */ /* end flashrom-layout */