blob: d2a290833be556e561dd39018226fe9b8025fb0a [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
31static struct flashrom_layout *global_layout;
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000032
Nico Huber305f4172013-06-14 11:55:26 +020033struct flashrom_layout *get_global_layout(void)
Nico Huber3a9939b2016-04-27 15:56:14 +020034{
Nico Huber49258612019-06-15 21:41:21 +020035 if (!global_layout)
Nico Huber671c0f02019-06-16 20:17:19 +020036 flashrom_layout_new(&global_layout);
Nico Huber49258612019-06-15 21:41:21 +020037 return global_layout;
Nico Huber3a9939b2016-04-27 15:56:14 +020038}
39
Nico Huber8edcd152019-06-16 03:22:58 +020040const struct flashrom_layout *get_default_layout(const struct flashrom_flashctx *const flashctx)
41{
42 return flashctx->default_layout;
43}
44
dhendrixbeaefe02017-09-03 18:06:53 -070045const struct flashrom_layout *get_layout(const struct flashrom_flashctx *const flashctx)
46{
Nico Huber49258612019-06-15 21:41:21 +020047 if (flashctx->layout)
dhendrixbeaefe02017-09-03 18:06:53 -070048 return flashctx->layout;
49 else
Nico Huber8edcd152019-06-16 03:22:58 +020050 return get_default_layout(flashctx);
dhendrixbeaefe02017-09-03 18:06:53 -070051}
52
Nico Huber354766b2019-06-16 19:28:35 +020053static struct romentry *mutable_layout_next(
54 const struct flashrom_layout *const layout, struct romentry *iterator)
55{
Nico Huber49258612019-06-15 21:41:21 +020056 return iterator ? iterator->next : layout->head;
Nico Huber354766b2019-06-16 19:28:35 +020057}
58
Patrick Georgia9095a92010-09-30 17:03:32 +000059#ifndef __LIBPAYLOAD__
Mark Marshallf20b7be2014-05-09 21:16:21 +000060int read_romlayout(const char *name)
Ollie Lho184a4042005-11-26 21:55:36 +000061{
Nico Huber2b94cdb2019-06-15 18:19:26 +020062 struct flashrom_layout *const layout = get_global_layout();
Ollie Lho184a4042005-11-26 21:55:36 +000063 FILE *romlayout;
Nico Huber70461a92019-06-15 14:56:19 +020064 char tempstr[256], tempname[256];
Nico Huber519be662018-12-23 20:03:35 +010065 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 Huber70461a92019-06-15 14:56:19 +020078 if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, tempname))
Peter Stuge1fec0f32009-01-12 21:00:35 +000079 continue;
Ollie Lho184a4042005-11-26 21:55:36 +000080#if 0
81 // fscanf does not like arbitrary comments like that :( later
Uwe Hermanna7e05482007-05-09 10:17:44 +000082 if (tempstr[0] == '#') {
Ollie Lho184a4042005-11-26 21:55:36 +000083 continue;
84 }
85#endif
Uwe Hermanna7e05482007-05-09 10:17:44 +000086 tstr1 = strtok(tempstr, ":");
87 tstr2 = strtok(NULL, ":");
Uwe Hermann58783e32008-12-22 16:42:59 +000088 if (!tstr1 || !tstr2) {
Stefan Taunereb582572012-09-21 12:52:50 +000089 msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr);
Nico Huber70461a92019-06-15 14:56:19 +020090 goto _close_ret;
Uwe Hermann58783e32008-12-22 16:42:59 +000091 }
Nico Huber92e0b622019-06-15 15:55:11 +020092 if (flashrom_layout_add_region(layout,
93 strtol(tstr1, NULL, 16), strtol(tstr2, NULL, 16), tempname))
Nico Huber70461a92019-06-15 14:56:19 +020094 goto _close_ret;
Ollie Lho184a4042005-11-26 21:55:36 +000095 }
Nico Huber70461a92019-06-15 14:56:19 +020096 ret = 0;
Uwe Hermannffec5f32007-08-23 16:08:21 +000097
Nico Huber70461a92019-06-15 14:56:19 +020098_close_ret:
99 (void)fclose(romlayout);
100 return ret;
Ollie Lho184a4042005-11-26 21:55:36 +0000101}
Patrick Georgia9095a92010-09-30 17:03:32 +0000102#endif
Ollie Lho184a4042005-11-26 21:55:36 +0000103
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000104/* register an include argument (-i) for later processing */
Arthur Heymansb04fef92019-02-05 17:35:05 +0100105int register_include_arg(struct layout_include_args **args, char *name)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000106{
Arthur Heymansb04fef92019-02-05 17:35:05 +0100107 struct layout_include_args *tmp;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000108 if (name == NULL) {
109 msg_gerr("<NULL> is a bad region name.\n");
110 return 1;
111 }
112
Arthur Heymansb04fef92019-02-05 17:35:05 +0100113 tmp = *args;
114 while (tmp) {
115 if (!strcmp(tmp->name, name)) {
116 msg_gerr("Duplicate region name: \"%s\".\n", name);
117 return 1;
118 }
119 tmp = tmp->next;
120 }
121
Angel Pons690a9442021-06-07 12:33:53 +0200122 tmp = malloc(sizeof(*tmp));
Arthur Heymansb04fef92019-02-05 17:35:05 +0100123 if (tmp == NULL) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100124 msg_gerr("Out of memory");
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000125 return 1;
126 }
127
Arthur Heymansb04fef92019-02-05 17:35:05 +0100128 tmp->name = name;
129 tmp->next = *args;
130 *args = tmp;
131
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000132 return 0;
133}
134
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100135/* returns -1 if an entry is not found, 0 if found. */
Nico Huber305f4172013-06-14 11:55:26 +0200136static int find_romentry(struct flashrom_layout *const l, char *name)
Ollie Lho184a4042005-11-26 21:55:36 +0000137{
Nico Huber49258612019-06-15 21:41:21 +0200138 if (!l->head)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000139 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000140
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000141 msg_gspew("Looking for region \"%s\"... ", name);
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100142 if (flashrom_layout_include_region(l, name)) {
143 msg_gspew("not found.\n");
144 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000145 }
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100146 msg_gspew("found.\n");
147 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000148}
149
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000150/* process -i arguments
151 * returns 0 to indicate success, >0 to indicate failure
152 */
Arthur Heymansb04fef92019-02-05 17:35:05 +0100153int process_include_args(struct flashrom_layout *l, const struct layout_include_args *const args)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000154{
Nico Huber519be662018-12-23 20:03:35 +0100155 unsigned int found = 0;
Arthur Heymansb04fef92019-02-05 17:35:05 +0100156 const struct layout_include_args *tmp;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000157
Arthur Heymansb04fef92019-02-05 17:35:05 +0100158 if (args == NULL)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000159 return 0;
160
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000161 /* User has specified an area, but no layout file is loaded. */
Nico Huber49258612019-06-15 21:41:21 +0200162 if (!l->head) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000163 msg_gerr("Region requested (with -i \"%s\"), "
164 "but no layout data is available.\n",
Arthur Heymansb04fef92019-02-05 17:35:05 +0100165 args->name);
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000166 return 1;
167 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000168
Arthur Heymansb04fef92019-02-05 17:35:05 +0100169 tmp = args;
170 while (tmp) {
171 if (find_romentry(l, tmp->name) < 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000172 msg_gerr("Invalid region specified: \"%s\".\n",
Arthur Heymansb04fef92019-02-05 17:35:05 +0100173 tmp->name);
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000174 return 1;
175 }
Arthur Heymansb04fef92019-02-05 17:35:05 +0100176 tmp = tmp->next;
177 found++;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000178 }
179
Arthur Heymansb04fef92019-02-05 17:35:05 +0100180 msg_ginfo("Using region%s:", found > 1 ? "s" : "");
181 tmp = args;
182 while (tmp) {
183 msg_ginfo(" \"%s\"%s", tmp->name, found > 1 ? "," : "");
184 found--;
185 tmp = tmp->next;
186 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000187 msg_ginfo(".\n");
188 return 0;
189}
190
Arthur Heymansb04fef92019-02-05 17:35:05 +0100191void layout_cleanup(struct layout_include_args **args)
Stefan Tauner949ccc82013-09-15 14:01:06 +0000192{
Nico Huber2b94cdb2019-06-15 18:19:26 +0200193 struct flashrom_layout *const layout = get_global_layout();
Arthur Heymansb04fef92019-02-05 17:35:05 +0100194 struct layout_include_args *tmp;
195
196 while (*args) {
197 tmp = (*args)->next;
198 free(*args);
199 *args = tmp;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000200 }
Stefan Tauner949ccc82013-09-15 14:01:06 +0000201
Nico Huber49258612019-06-15 21:41:21 +0200202 global_layout = NULL;
203 flashrom_layout_release(layout);
Stefan Tauner949ccc82013-09-15 14:01:06 +0000204}
205
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000206/* Validate and - if needed - normalize layout entries. */
207int normalize_romentries(const struct flashctx *flash)
208{
Nico Huber2b94cdb2019-06-15 18:19:26 +0200209 struct flashrom_layout *const layout = get_global_layout();
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000210 chipsize_t total_size = flash->chip->total_size * 1024;
211 int ret = 0;
212
Nico Huber354766b2019-06-16 19:28:35 +0200213 const struct romentry *entry = NULL;
214 while ((entry = layout_next(layout, entry))) {
215 if (entry->start >= total_size || entry->end >= total_size) {
216 msg_gwarn("Warning: Address range of region \"%s\" "
217 "exceeds the current chip's address space.\n", entry->name);
218 if (entry->included)
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000219 ret = 1;
220 }
Nico Huber354766b2019-06-16 19:28:35 +0200221 if (entry->start > entry->end) {
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000222 msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n",
Nico Huber354766b2019-06-16 19:28:35 +0200223 entry->name);
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000224 ret = 1;
225 }
226 }
227
228 return ret;
229}
Nico Huber3d7b1e32018-12-22 00:53:14 +0100230
231const struct romentry *layout_next_included_region(
232 const struct flashrom_layout *const l, const chipoff_t where)
233{
Nico Huber354766b2019-06-16 19:28:35 +0200234 const struct romentry *entry = NULL, *lowest = NULL;
Nico Huber3d7b1e32018-12-22 00:53:14 +0100235
Nico Huber354766b2019-06-16 19:28:35 +0200236 while ((entry = layout_next(l, entry))) {
237 if (!entry->included)
Nico Huber3d7b1e32018-12-22 00:53:14 +0100238 continue;
Nico Huber354766b2019-06-16 19:28:35 +0200239 if (entry->end < where)
Nico Huber3d7b1e32018-12-22 00:53:14 +0100240 continue;
Nico Huber354766b2019-06-16 19:28:35 +0200241 if (!lowest || lowest->start > entry->start)
242 lowest = entry;
Nico Huber3d7b1e32018-12-22 00:53:14 +0100243 }
244
245 return lowest;
246}
Nico Huber4f213282019-06-15 17:33:49 +0200247
Nico Huber5ca55232019-06-15 22:29:08 +0200248const struct romentry *layout_next_included(
249 const struct flashrom_layout *const layout, const struct romentry *iterator)
250{
Nico Huber354766b2019-06-16 19:28:35 +0200251 while ((iterator = layout_next(layout, iterator))) {
252 if (iterator->included)
253 break;
Nico Huber5ca55232019-06-15 22:29:08 +0200254 }
Nico Huber354766b2019-06-16 19:28:35 +0200255 return iterator;
256}
257
258const struct romentry *layout_next(
259 const struct flashrom_layout *const layout, const struct romentry *iterator)
260{
Nico Huber49258612019-06-15 21:41:21 +0200261 return iterator ? iterator->next : layout->head;
Nico Huber5ca55232019-06-15 22:29:08 +0200262}
263
Nico Huber4f213282019-06-15 17:33:49 +0200264/**
265 * @addtogroup flashrom-layout
266 * @{
267 */
268
269/**
Nico Huber5bd990c2019-06-16 19:46:46 +0200270 * @brief Create a new, empty layout.
271 *
272 * @param layout Pointer to returned layout reference.
Nico Huber5bd990c2019-06-16 19:46:46 +0200273 *
274 * @return 0 on success,
275 * 1 if out of memory.
276 */
Nico Huber671c0f02019-06-16 20:17:19 +0200277int flashrom_layout_new(struct flashrom_layout **const layout)
Nico Huber5bd990c2019-06-16 19:46:46 +0200278{
Nico Huber49258612019-06-15 21:41:21 +0200279 *layout = malloc(sizeof(**layout));
Nico Huber5bd990c2019-06-16 19:46:46 +0200280 if (!*layout) {
281 msg_gerr("Error creating layout: %s\n", strerror(errno));
282 return 1;
283 }
284
Nico Huber49258612019-06-15 21:41:21 +0200285 const struct flashrom_layout tmp = { 0 };
Nico Huber5bd990c2019-06-16 19:46:46 +0200286 **layout = tmp;
287 return 0;
288}
289
290/**
Nico Huber92e0b622019-06-15 15:55:11 +0200291 * @brief Add another region to an existing layout.
292 *
293 * @param layout The existing layout.
294 * @param start Start address of the region.
295 * @param end End address (inclusive) of the region.
296 * @param name Name of the region.
297 *
298 * @return 0 on success,
Nico Huber49258612019-06-15 21:41:21 +0200299 * 1 if out of memory.
Nico Huber92e0b622019-06-15 15:55:11 +0200300 */
301int flashrom_layout_add_region(
302 struct flashrom_layout *const layout,
303 const size_t start, const size_t end, const char *const name)
304{
Nico Huber49258612019-06-15 21:41:21 +0200305 struct romentry *const entry = malloc(sizeof(*entry));
306 if (!entry)
307 goto _err_ret;
Nico Huber92e0b622019-06-15 15:55:11 +0200308
Nico Huber49258612019-06-15 21:41:21 +0200309 const struct romentry tmp = {
310 .next = layout->head,
311 .start = start,
312 .end = end,
313 .included = false,
314 .name = strdup(name),
315 };
316 *entry = tmp;
317 if (!entry->name)
318 goto _err_ret;
Nico Huber92e0b622019-06-15 15:55:11 +0200319
320 msg_gdbg("Added layout entry %08zx - %08zx named %s\n", start, end, name);
Nico Huber49258612019-06-15 21:41:21 +0200321 layout->head = entry;
Nico Huber92e0b622019-06-15 15:55:11 +0200322 return 0;
Nico Huber49258612019-06-15 21:41:21 +0200323
324_err_ret:
325 msg_gerr("Error adding layout entry: %s\n", strerror(errno));
326 free(entry);
327 return 1;
Nico Huber92e0b622019-06-15 15:55:11 +0200328}
329
330/**
Nico Huber4f213282019-06-15 17:33:49 +0200331 * @brief Mark given region as included.
332 *
333 * @param layout The layout to alter.
334 * @param name The name of the region to include.
335 *
336 * @return 0 on success,
337 * 1 if the given name can't be found.
338 */
339int flashrom_layout_include_region(struct flashrom_layout *const layout, const char *name)
340{
Nico Huber354766b2019-06-16 19:28:35 +0200341 struct romentry *entry = NULL;
342 while ((entry = mutable_layout_next(layout, entry))) {
343 if (!strcmp(entry->name, name)) {
344 entry->included = true;
Nico Huber4f213282019-06-15 17:33:49 +0200345 return 0;
346 }
347 }
348 return 1;
349}
350
351/**
352 * @brief Free a layout.
353 *
354 * @param layout Layout to free.
355 */
356void flashrom_layout_release(struct flashrom_layout *const layout)
357{
Nico Huber49258612019-06-15 21:41:21 +0200358 if (layout == global_layout)
Nico Huber4f213282019-06-15 17:33:49 +0200359 return;
360
Nico Huber49258612019-06-15 21:41:21 +0200361 if (!layout)
362 return;
363
364 while (layout->head) {
365 struct romentry *const entry = layout->head;
366 layout->head = entry->next;
367 free(entry->name);
368 free(entry);
369 }
Nico Huber4f213282019-06-15 17:33:49 +0200370 free(layout);
371}
372
373/** @} */ /* end flashrom-layout */