blob: a604a7b8b1b9093cdf6f051bfaf5815032c1b64c [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
Nico Huber354766b2019-06-16 19:28:35 +020043static struct romentry *mutable_layout_next(
44 const struct flashrom_layout *const layout, struct romentry *iterator)
45{
46 const struct romentry *const end = layout->entries + layout->num_entries;
47
48 if (iterator)
49 ++iterator;
50 else
51 iterator = &layout->entries[0];
52
53 if (iterator < end)
54 return iterator;
55 return NULL;
56}
57
Patrick Georgia9095a92010-09-30 17:03:32 +000058#ifndef __LIBPAYLOAD__
Mark Marshallf20b7be2014-05-09 21:16:21 +000059int read_romlayout(const char *name)
Ollie Lho184a4042005-11-26 21:55:36 +000060{
Nico Huber2b94cdb2019-06-15 18:19:26 +020061 struct flashrom_layout *const layout = get_global_layout();
Ollie Lho184a4042005-11-26 21:55:36 +000062 FILE *romlayout;
Nico Huber70461a92019-06-15 14:56:19 +020063 char tempstr[256], tempname[256];
Nico Huber519be662018-12-23 20:03:35 +010064 unsigned int i;
65 int ret = 1;
Ollie Lho184a4042005-11-26 21:55:36 +000066
Uwe Hermanna7e05482007-05-09 10:17:44 +000067 romlayout = fopen(name, "r");
68
69 if (!romlayout) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000070 msg_gerr("ERROR: Could not open ROM layout (%s).\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +000071 name);
Ollie Lho184a4042005-11-26 21:55:36 +000072 return -1;
73 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000074
75 while (!feof(romlayout)) {
Ollie Lho184a4042005-11-26 21:55:36 +000076 char *tstr1, *tstr2;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000077
Nico Huber2b94cdb2019-06-15 18:19:26 +020078 if (layout->num_entries >= MAX_ROMLAYOUT) {
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000079 msg_gerr("Maximum number of ROM images (%i) in layout "
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +000080 "file reached.\n", MAX_ROMLAYOUT);
Nico Huber70461a92019-06-15 14:56:19 +020081 goto _close_ret;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000082 }
Nico Huber70461a92019-06-15 14:56:19 +020083 if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, tempname))
Peter Stuge1fec0f32009-01-12 21:00:35 +000084 continue;
Ollie Lho184a4042005-11-26 21:55:36 +000085#if 0
86 // fscanf does not like arbitrary comments like that :( later
Uwe Hermanna7e05482007-05-09 10:17:44 +000087 if (tempstr[0] == '#') {
Ollie Lho184a4042005-11-26 21:55:36 +000088 continue;
89 }
90#endif
Uwe Hermanna7e05482007-05-09 10:17:44 +000091 tstr1 = strtok(tempstr, ":");
92 tstr2 = strtok(NULL, ":");
Uwe Hermann58783e32008-12-22 16:42:59 +000093 if (!tstr1 || !tstr2) {
Stefan Taunereb582572012-09-21 12:52:50 +000094 msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr);
Nico Huber70461a92019-06-15 14:56:19 +020095 goto _close_ret;
Uwe Hermann58783e32008-12-22 16:42:59 +000096 }
Nico Huber2b94cdb2019-06-15 18:19:26 +020097 layout->entries[layout->num_entries].start = strtol(tstr1, (char **)NULL, 16);
98 layout->entries[layout->num_entries].end = strtol(tstr2, (char **)NULL, 16);
Edward O'Callaghan405e72a2020-12-19 11:21:49 +110099 layout->entries[layout->num_entries].included = false;
Nico Huber70461a92019-06-15 14:56:19 +0200100 layout->entries[layout->num_entries].name = strdup(tempname);
101 if (!layout->entries[layout->num_entries].name) {
102 msg_gerr("Error adding layout entry: %s\n", strerror(errno));
103 goto _close_ret;
104 }
Nico Huber2b94cdb2019-06-15 18:19:26 +0200105 layout->num_entries++;
Ollie Lho184a4042005-11-26 21:55:36 +0000106 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000107
Nico Huber2b94cdb2019-06-15 18:19:26 +0200108 for (i = 0; i < layout->num_entries; i++) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +0000109 msg_gdbg("romlayout %08x - %08x named %s\n",
Nico Huber2b94cdb2019-06-15 18:19:26 +0200110 layout->entries[i].start,
111 layout->entries[i].end, layout->entries[i].name);
Ollie Lho184a4042005-11-26 21:55:36 +0000112 }
113
Nico Huber70461a92019-06-15 14:56:19 +0200114 ret = 0;
Uwe Hermannffec5f32007-08-23 16:08:21 +0000115
Nico Huber70461a92019-06-15 14:56:19 +0200116_close_ret:
117 (void)fclose(romlayout);
118 return ret;
Ollie Lho184a4042005-11-26 21:55:36 +0000119}
Patrick Georgia9095a92010-09-30 17:03:32 +0000120#endif
Ollie Lho184a4042005-11-26 21:55:36 +0000121
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000122/* register an include argument (-i) for later processing */
Arthur Heymansb04fef92019-02-05 17:35:05 +0100123int register_include_arg(struct layout_include_args **args, char *name)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000124{
Arthur Heymansb04fef92019-02-05 17:35:05 +0100125 struct layout_include_args *tmp;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000126 if (name == NULL) {
127 msg_gerr("<NULL> is a bad region name.\n");
128 return 1;
129 }
130
Arthur Heymansb04fef92019-02-05 17:35:05 +0100131 tmp = *args;
132 while (tmp) {
133 if (!strcmp(tmp->name, name)) {
134 msg_gerr("Duplicate region name: \"%s\".\n", name);
135 return 1;
136 }
137 tmp = tmp->next;
138 }
139
Angel Pons690a9442021-06-07 12:33:53 +0200140 tmp = malloc(sizeof(*tmp));
Arthur Heymansb04fef92019-02-05 17:35:05 +0100141 if (tmp == NULL) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100142 msg_gerr("Out of memory");
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000143 return 1;
144 }
145
Arthur Heymansb04fef92019-02-05 17:35:05 +0100146 tmp->name = name;
147 tmp->next = *args;
148 *args = tmp;
149
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000150 return 0;
151}
152
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100153/* returns -1 if an entry is not found, 0 if found. */
Nico Huber305f4172013-06-14 11:55:26 +0200154static int find_romentry(struct flashrom_layout *const l, char *name)
Ollie Lho184a4042005-11-26 21:55:36 +0000155{
Nico Huber305f4172013-06-14 11:55:26 +0200156 if (l->num_entries == 0)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000157 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000158
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000159 msg_gspew("Looking for region \"%s\"... ", name);
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100160 if (flashrom_layout_include_region(l, name)) {
161 msg_gspew("not found.\n");
162 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000163 }
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100164 msg_gspew("found.\n");
165 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000166}
167
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000168/* process -i arguments
169 * returns 0 to indicate success, >0 to indicate failure
170 */
Arthur Heymansb04fef92019-02-05 17:35:05 +0100171int process_include_args(struct flashrom_layout *l, const struct layout_include_args *const args)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000172{
Nico Huber519be662018-12-23 20:03:35 +0100173 unsigned int found = 0;
Arthur Heymansb04fef92019-02-05 17:35:05 +0100174 const struct layout_include_args *tmp;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000175
Arthur Heymansb04fef92019-02-05 17:35:05 +0100176 if (args == NULL)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000177 return 0;
178
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000179 /* User has specified an area, but no layout file is loaded. */
Nico Huber305f4172013-06-14 11:55:26 +0200180 if (l->num_entries == 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000181 msg_gerr("Region requested (with -i \"%s\"), "
182 "but no layout data is available.\n",
Arthur Heymansb04fef92019-02-05 17:35:05 +0100183 args->name);
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000184 return 1;
185 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000186
Arthur Heymansb04fef92019-02-05 17:35:05 +0100187 tmp = args;
188 while (tmp) {
189 if (find_romentry(l, tmp->name) < 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000190 msg_gerr("Invalid region specified: \"%s\".\n",
Arthur Heymansb04fef92019-02-05 17:35:05 +0100191 tmp->name);
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000192 return 1;
193 }
Arthur Heymansb04fef92019-02-05 17:35:05 +0100194 tmp = tmp->next;
195 found++;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000196 }
197
Arthur Heymansb04fef92019-02-05 17:35:05 +0100198 msg_ginfo("Using region%s:", found > 1 ? "s" : "");
199 tmp = args;
200 while (tmp) {
201 msg_ginfo(" \"%s\"%s", tmp->name, found > 1 ? "," : "");
202 found--;
203 tmp = tmp->next;
204 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000205 msg_ginfo(".\n");
206 return 0;
207}
208
Arthur Heymansb04fef92019-02-05 17:35:05 +0100209void layout_cleanup(struct layout_include_args **args)
Stefan Tauner949ccc82013-09-15 14:01:06 +0000210{
Nico Huber2b94cdb2019-06-15 18:19:26 +0200211 struct flashrom_layout *const layout = get_global_layout();
Nico Huber519be662018-12-23 20:03:35 +0100212 unsigned int i;
Arthur Heymansb04fef92019-02-05 17:35:05 +0100213 struct layout_include_args *tmp;
214
215 while (*args) {
216 tmp = (*args)->next;
217 free(*args);
218 *args = tmp;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000219 }
Stefan Tauner949ccc82013-09-15 14:01:06 +0000220
Nico Huber2b94cdb2019-06-15 18:19:26 +0200221 for (i = 0; i < layout->num_entries; i++) {
Nico Huber70461a92019-06-15 14:56:19 +0200222 free(layout->entries[i].name);
Edward O'Callaghan405e72a2020-12-19 11:21:49 +1100223 layout->entries[i].included = false;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000224 }
Nico Huber2b94cdb2019-06-15 18:19:26 +0200225 layout->num_entries = 0;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000226}
227
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000228/* Validate and - if needed - normalize layout entries. */
229int normalize_romentries(const struct flashctx *flash)
230{
Nico Huber2b94cdb2019-06-15 18:19:26 +0200231 struct flashrom_layout *const layout = get_global_layout();
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000232 chipsize_t total_size = flash->chip->total_size * 1024;
233 int ret = 0;
234
Nico Huber354766b2019-06-16 19:28:35 +0200235 const struct romentry *entry = NULL;
236 while ((entry = layout_next(layout, entry))) {
237 if (entry->start >= total_size || entry->end >= total_size) {
238 msg_gwarn("Warning: Address range of region \"%s\" "
239 "exceeds the current chip's address space.\n", entry->name);
240 if (entry->included)
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000241 ret = 1;
242 }
Nico Huber354766b2019-06-16 19:28:35 +0200243 if (entry->start > entry->end) {
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000244 msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n",
Nico Huber354766b2019-06-16 19:28:35 +0200245 entry->name);
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000246 ret = 1;
247 }
248 }
249
250 return ret;
251}
Nico Huber3d7b1e32018-12-22 00:53:14 +0100252
253const struct romentry *layout_next_included_region(
254 const struct flashrom_layout *const l, const chipoff_t where)
255{
Nico Huber354766b2019-06-16 19:28:35 +0200256 const struct romentry *entry = NULL, *lowest = NULL;
Nico Huber3d7b1e32018-12-22 00:53:14 +0100257
Nico Huber354766b2019-06-16 19:28:35 +0200258 while ((entry = layout_next(l, entry))) {
259 if (!entry->included)
Nico Huber3d7b1e32018-12-22 00:53:14 +0100260 continue;
Nico Huber354766b2019-06-16 19:28:35 +0200261 if (entry->end < where)
Nico Huber3d7b1e32018-12-22 00:53:14 +0100262 continue;
Nico Huber354766b2019-06-16 19:28:35 +0200263 if (!lowest || lowest->start > entry->start)
264 lowest = entry;
Nico Huber3d7b1e32018-12-22 00:53:14 +0100265 }
266
267 return lowest;
268}
Nico Huber4f213282019-06-15 17:33:49 +0200269
Nico Huber5ca55232019-06-15 22:29:08 +0200270const struct romentry *layout_next_included(
271 const struct flashrom_layout *const layout, const struct romentry *iterator)
272{
Nico Huber354766b2019-06-16 19:28:35 +0200273 while ((iterator = layout_next(layout, iterator))) {
274 if (iterator->included)
275 break;
Nico Huber5ca55232019-06-15 22:29:08 +0200276 }
Nico Huber354766b2019-06-16 19:28:35 +0200277 return iterator;
278}
279
280const struct romentry *layout_next(
281 const struct flashrom_layout *const layout, const struct romentry *iterator)
282{
283 return mutable_layout_next(layout, (struct romentry *)iterator);
Nico Huber5ca55232019-06-15 22:29:08 +0200284}
285
Nico Huber4f213282019-06-15 17:33:49 +0200286/**
287 * @addtogroup flashrom-layout
288 * @{
289 */
290
291/**
292 * @brief Mark given region as included.
293 *
294 * @param layout The layout to alter.
295 * @param name The name of the region to include.
296 *
297 * @return 0 on success,
298 * 1 if the given name can't be found.
299 */
300int flashrom_layout_include_region(struct flashrom_layout *const layout, const char *name)
301{
Nico Huber354766b2019-06-16 19:28:35 +0200302 struct romentry *entry = NULL;
303 while ((entry = mutable_layout_next(layout, entry))) {
304 if (!strcmp(entry->name, name)) {
305 entry->included = true;
Nico Huber4f213282019-06-15 17:33:49 +0200306 return 0;
307 }
308 }
309 return 1;
310}
311
312/**
313 * @brief Free a layout.
314 *
315 * @param layout Layout to free.
316 */
317void flashrom_layout_release(struct flashrom_layout *const layout)
318{
Nico Huber70461a92019-06-15 14:56:19 +0200319 unsigned int i;
320
321 if (!layout || layout == get_global_layout())
Nico Huber4f213282019-06-15 17:33:49 +0200322 return;
323
Nico Huber70461a92019-06-15 14:56:19 +0200324 for (i = 0; i < layout->num_entries; ++i)
325 free(layout->entries[i].name);
Nico Huber4f213282019-06-15 17:33:49 +0200326 free(layout);
327}
328
329/** @} */ /* end flashrom-layout */