blob: 2c455e2ba111ec09b1e683e94c4680306c4e5878 [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
Nico Huber49258612019-06-15 21:41:21 +020027struct flashrom_layout {
28 struct romentry *head;
29};
30
Nico Huber6e985442019-06-16 20:22:41 +020031struct layout_include_args {
32 char *name;
33 struct layout_include_args *next;
34};
35
Nico Huber49258612019-06-15 21:41:21 +020036static struct flashrom_layout *global_layout;
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000037
Nico Huber305f4172013-06-14 11:55:26 +020038struct flashrom_layout *get_global_layout(void)
Nico Huber3a9939b2016-04-27 15:56:14 +020039{
Nico Huber49258612019-06-15 21:41:21 +020040 if (!global_layout)
Nico Huber671c0f02019-06-16 20:17:19 +020041 flashrom_layout_new(&global_layout);
Nico Huber49258612019-06-15 21:41:21 +020042 return global_layout;
Nico Huber3a9939b2016-04-27 15:56:14 +020043}
44
Nico Huber8edcd152019-06-16 03:22:58 +020045const struct flashrom_layout *get_default_layout(const struct flashrom_flashctx *const flashctx)
46{
47 return flashctx->default_layout;
48}
49
dhendrixbeaefe02017-09-03 18:06:53 -070050const struct flashrom_layout *get_layout(const struct flashrom_flashctx *const flashctx)
51{
Nico Huber49258612019-06-15 21:41:21 +020052 if (flashctx->layout)
dhendrixbeaefe02017-09-03 18:06:53 -070053 return flashctx->layout;
54 else
Nico Huber8edcd152019-06-16 03:22:58 +020055 return get_default_layout(flashctx);
dhendrixbeaefe02017-09-03 18:06:53 -070056}
57
Nico Huber354766b2019-06-16 19:28:35 +020058static struct romentry *mutable_layout_next(
59 const struct flashrom_layout *const layout, struct romentry *iterator)
60{
Nico Huber49258612019-06-15 21:41:21 +020061 return iterator ? iterator->next : layout->head;
Nico Huber354766b2019-06-16 19:28:35 +020062}
63
Patrick Georgia9095a92010-09-30 17:03:32 +000064#ifndef __LIBPAYLOAD__
Mark Marshallf20b7be2014-05-09 21:16:21 +000065int read_romlayout(const char *name)
Ollie Lho184a4042005-11-26 21:55:36 +000066{
Nico Huber2b94cdb2019-06-15 18:19:26 +020067 struct flashrom_layout *const layout = get_global_layout();
Ollie Lho184a4042005-11-26 21:55:36 +000068 FILE *romlayout;
Nico Huber70461a92019-06-15 14:56:19 +020069 char tempstr[256], tempname[256];
Nico Huber519be662018-12-23 20:03:35 +010070 int ret = 1;
Ollie Lho184a4042005-11-26 21:55:36 +000071
Uwe Hermanna7e05482007-05-09 10:17:44 +000072 romlayout = fopen(name, "r");
73
74 if (!romlayout) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000075 msg_gerr("ERROR: Could not open ROM layout (%s).\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +000076 name);
Ollie Lho184a4042005-11-26 21:55:36 +000077 return -1;
78 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000079
80 while (!feof(romlayout)) {
Ollie Lho184a4042005-11-26 21:55:36 +000081 char *tstr1, *tstr2;
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 Huber92e0b622019-06-15 15:55:11 +020097 if (flashrom_layout_add_region(layout,
98 strtol(tstr1, NULL, 16), strtol(tstr2, NULL, 16), tempname))
Nico Huber70461a92019-06-15 14:56:19 +020099 goto _close_ret;
Ollie Lho184a4042005-11-26 21:55:36 +0000100 }
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 */
Arthur Heymansb04fef92019-02-05 17:35:05 +0100110int register_include_arg(struct layout_include_args **args, char *name)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000111{
Arthur Heymansb04fef92019-02-05 17:35:05 +0100112 struct layout_include_args *tmp;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000113 if (name == NULL) {
114 msg_gerr("<NULL> is a bad region name.\n");
115 return 1;
116 }
117
Arthur Heymansb04fef92019-02-05 17:35:05 +0100118 tmp = *args;
119 while (tmp) {
120 if (!strcmp(tmp->name, name)) {
121 msg_gerr("Duplicate region name: \"%s\".\n", name);
122 return 1;
123 }
124 tmp = tmp->next;
125 }
126
Angel Pons690a9442021-06-07 12:33:53 +0200127 tmp = malloc(sizeof(*tmp));
Arthur Heymansb04fef92019-02-05 17:35:05 +0100128 if (tmp == NULL) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100129 msg_gerr("Out of memory");
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000130 return 1;
131 }
132
Arthur Heymansb04fef92019-02-05 17:35:05 +0100133 tmp->name = name;
134 tmp->next = *args;
135 *args = tmp;
136
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000137 return 0;
138}
139
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100140/* returns -1 if an entry is not found, 0 if found. */
Nico Huber305f4172013-06-14 11:55:26 +0200141static int find_romentry(struct flashrom_layout *const l, char *name)
Ollie Lho184a4042005-11-26 21:55:36 +0000142{
Nico Huber49258612019-06-15 21:41:21 +0200143 if (!l->head)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000144 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000145
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000146 msg_gspew("Looking for region \"%s\"... ", name);
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100147 if (flashrom_layout_include_region(l, name)) {
148 msg_gspew("not found.\n");
149 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000150 }
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100151 msg_gspew("found.\n");
152 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000153}
154
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000155/* process -i arguments
156 * returns 0 to indicate success, >0 to indicate failure
157 */
Arthur Heymansb04fef92019-02-05 17:35:05 +0100158int process_include_args(struct flashrom_layout *l, const struct layout_include_args *const args)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000159{
Nico Huber519be662018-12-23 20:03:35 +0100160 unsigned int found = 0;
Arthur Heymansb04fef92019-02-05 17:35:05 +0100161 const struct layout_include_args *tmp;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000162
Arthur Heymansb04fef92019-02-05 17:35:05 +0100163 if (args == NULL)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000164 return 0;
165
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000166 /* User has specified an area, but no layout file is loaded. */
Nico Huber49258612019-06-15 21:41:21 +0200167 if (!l->head) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000168 msg_gerr("Region requested (with -i \"%s\"), "
169 "but no layout data is available.\n",
Arthur Heymansb04fef92019-02-05 17:35:05 +0100170 args->name);
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000171 return 1;
172 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000173
Arthur Heymansb04fef92019-02-05 17:35:05 +0100174 tmp = args;
175 while (tmp) {
176 if (find_romentry(l, tmp->name) < 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000177 msg_gerr("Invalid region specified: \"%s\".\n",
Arthur Heymansb04fef92019-02-05 17:35:05 +0100178 tmp->name);
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000179 return 1;
180 }
Arthur Heymansb04fef92019-02-05 17:35:05 +0100181 tmp = tmp->next;
182 found++;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000183 }
184
Arthur Heymansb04fef92019-02-05 17:35:05 +0100185 msg_ginfo("Using region%s:", found > 1 ? "s" : "");
186 tmp = args;
187 while (tmp) {
188 msg_ginfo(" \"%s\"%s", tmp->name, found > 1 ? "," : "");
189 found--;
190 tmp = tmp->next;
191 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000192 msg_ginfo(".\n");
193 return 0;
194}
195
Arthur Heymansb04fef92019-02-05 17:35:05 +0100196void layout_cleanup(struct layout_include_args **args)
Stefan Tauner949ccc82013-09-15 14:01:06 +0000197{
Nico Huber2b94cdb2019-06-15 18:19:26 +0200198 struct flashrom_layout *const layout = get_global_layout();
Arthur Heymansb04fef92019-02-05 17:35:05 +0100199 struct layout_include_args *tmp;
200
201 while (*args) {
202 tmp = (*args)->next;
203 free(*args);
204 *args = tmp;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000205 }
Stefan Tauner949ccc82013-09-15 14:01:06 +0000206
Nico Huber49258612019-06-15 21:41:21 +0200207 global_layout = NULL;
208 flashrom_layout_release(layout);
Stefan Tauner949ccc82013-09-15 14:01:06 +0000209}
210
Nico Hubere0ed4122021-05-14 00:48:28 +0200211int layout_sanity_checks(const struct flashrom_flashctx *const flash)
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000212{
Nico Hubere0ed4122021-05-14 00:48:28 +0200213 const struct flashrom_layout *const layout = get_layout(flash);
214 const chipsize_t total_size = flash->chip->total_size * 1024;
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000215 int ret = 0;
216
Nico Huber354766b2019-06-16 19:28:35 +0200217 const struct romentry *entry = NULL;
218 while ((entry = layout_next(layout, entry))) {
219 if (entry->start >= total_size || entry->end >= total_size) {
220 msg_gwarn("Warning: Address range of region \"%s\" "
221 "exceeds the current chip's address space.\n", entry->name);
222 if (entry->included)
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000223 ret = 1;
224 }
Nico Huber354766b2019-06-16 19:28:35 +0200225 if (entry->start > entry->end) {
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000226 msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n",
Nico Huber354766b2019-06-16 19:28:35 +0200227 entry->name);
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000228 ret = 1;
229 }
230 }
231
232 return ret;
233}
Nico Huber3d7b1e32018-12-22 00:53:14 +0100234
235const struct romentry *layout_next_included_region(
236 const struct flashrom_layout *const l, const chipoff_t where)
237{
Nico Huber354766b2019-06-16 19:28:35 +0200238 const struct romentry *entry = NULL, *lowest = NULL;
Nico Huber3d7b1e32018-12-22 00:53:14 +0100239
Nico Huber354766b2019-06-16 19:28:35 +0200240 while ((entry = layout_next(l, entry))) {
241 if (!entry->included)
Nico Huber3d7b1e32018-12-22 00:53:14 +0100242 continue;
Nico Huber354766b2019-06-16 19:28:35 +0200243 if (entry->end < where)
Nico Huber3d7b1e32018-12-22 00:53:14 +0100244 continue;
Nico Huber354766b2019-06-16 19:28:35 +0200245 if (!lowest || lowest->start > entry->start)
246 lowest = entry;
Nico Huber3d7b1e32018-12-22 00:53:14 +0100247 }
248
249 return lowest;
250}
Nico Huber4f213282019-06-15 17:33:49 +0200251
Nico Huber5ca55232019-06-15 22:29:08 +0200252const struct romentry *layout_next_included(
253 const struct flashrom_layout *const layout, const struct romentry *iterator)
254{
Nico Huber354766b2019-06-16 19:28:35 +0200255 while ((iterator = layout_next(layout, iterator))) {
256 if (iterator->included)
257 break;
Nico Huber5ca55232019-06-15 22:29:08 +0200258 }
Nico Huber354766b2019-06-16 19:28:35 +0200259 return iterator;
260}
261
262const struct romentry *layout_next(
263 const struct flashrom_layout *const layout, const struct romentry *iterator)
264{
Nico Huber49258612019-06-15 21:41:21 +0200265 return iterator ? iterator->next : layout->head;
Nico Huber5ca55232019-06-15 22:29:08 +0200266}
267
Nico Huber4f213282019-06-15 17:33:49 +0200268/**
269 * @addtogroup flashrom-layout
270 * @{
271 */
272
273/**
Nico Huber5bd990c2019-06-16 19:46:46 +0200274 * @brief Create a new, empty layout.
275 *
276 * @param layout Pointer to returned layout reference.
Nico Huber5bd990c2019-06-16 19:46:46 +0200277 *
278 * @return 0 on success,
279 * 1 if out of memory.
280 */
Nico Huber671c0f02019-06-16 20:17:19 +0200281int flashrom_layout_new(struct flashrom_layout **const layout)
Nico Huber5bd990c2019-06-16 19:46:46 +0200282{
Nico Huber49258612019-06-15 21:41:21 +0200283 *layout = malloc(sizeof(**layout));
Nico Huber5bd990c2019-06-16 19:46:46 +0200284 if (!*layout) {
285 msg_gerr("Error creating layout: %s\n", strerror(errno));
286 return 1;
287 }
288
Nico Huber49258612019-06-15 21:41:21 +0200289 const struct flashrom_layout tmp = { 0 };
Nico Huber5bd990c2019-06-16 19:46:46 +0200290 **layout = tmp;
291 return 0;
292}
293
294/**
Nico Huber92e0b622019-06-15 15:55:11 +0200295 * @brief Add another region to an existing layout.
296 *
297 * @param layout The existing layout.
298 * @param start Start address of the region.
299 * @param end End address (inclusive) of the region.
300 * @param name Name of the region.
301 *
302 * @return 0 on success,
Nico Huber49258612019-06-15 21:41:21 +0200303 * 1 if out of memory.
Nico Huber92e0b622019-06-15 15:55:11 +0200304 */
305int flashrom_layout_add_region(
306 struct flashrom_layout *const layout,
307 const size_t start, const size_t end, const char *const name)
308{
Nico Huber49258612019-06-15 21:41:21 +0200309 struct romentry *const entry = malloc(sizeof(*entry));
310 if (!entry)
311 goto _err_ret;
Nico Huber92e0b622019-06-15 15:55:11 +0200312
Nico Huber49258612019-06-15 21:41:21 +0200313 const struct romentry tmp = {
314 .next = layout->head,
315 .start = start,
316 .end = end,
317 .included = false,
318 .name = strdup(name),
319 };
320 *entry = tmp;
321 if (!entry->name)
322 goto _err_ret;
Nico Huber92e0b622019-06-15 15:55:11 +0200323
324 msg_gdbg("Added layout entry %08zx - %08zx named %s\n", start, end, name);
Nico Huber49258612019-06-15 21:41:21 +0200325 layout->head = entry;
Nico Huber92e0b622019-06-15 15:55:11 +0200326 return 0;
Nico Huber49258612019-06-15 21:41:21 +0200327
328_err_ret:
329 msg_gerr("Error adding layout entry: %s\n", strerror(errno));
330 free(entry);
331 return 1;
Nico Huber92e0b622019-06-15 15:55:11 +0200332}
333
334/**
Nico Huber4f213282019-06-15 17:33:49 +0200335 * @brief Mark given region as included.
336 *
337 * @param layout The layout to alter.
338 * @param name The name of the region to include.
339 *
340 * @return 0 on success,
341 * 1 if the given name can't be found.
342 */
343int flashrom_layout_include_region(struct flashrom_layout *const layout, const char *name)
344{
Nico Huber354766b2019-06-16 19:28:35 +0200345 struct romentry *entry = NULL;
346 while ((entry = mutable_layout_next(layout, entry))) {
347 if (!strcmp(entry->name, name)) {
348 entry->included = true;
Nico Huber4f213282019-06-15 17:33:49 +0200349 return 0;
350 }
351 }
352 return 1;
353}
354
355/**
356 * @brief Free a layout.
357 *
358 * @param layout Layout to free.
359 */
360void flashrom_layout_release(struct flashrom_layout *const layout)
361{
Nico Huber49258612019-06-15 21:41:21 +0200362 if (layout == global_layout)
Nico Huber4f213282019-06-15 17:33:49 +0200363 return;
364
Nico Huber49258612019-06-15 21:41:21 +0200365 if (!layout)
366 return;
367
368 while (layout->head) {
369 struct romentry *const entry = layout->head;
370 layout->head = entry->next;
371 free(entry->name);
372 free(entry);
373 }
Nico Huber4f213282019-06-15 17:33:49 +0200374 free(layout);
375}
376
377/** @} */ /* end flashrom-layout */