blob: aeffcd7ea02b5c069d3698575851729486dfb3a8 [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
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000211/* Validate and - if needed - normalize layout entries. */
212int normalize_romentries(const struct flashctx *flash)
213{
Nico Huber2b94cdb2019-06-15 18:19:26 +0200214 struct flashrom_layout *const layout = get_global_layout();
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000215 chipsize_t total_size = flash->chip->total_size * 1024;
216 int ret = 0;
217
Nico Huber354766b2019-06-16 19:28:35 +0200218 const struct romentry *entry = NULL;
219 while ((entry = layout_next(layout, entry))) {
220 if (entry->start >= total_size || entry->end >= total_size) {
221 msg_gwarn("Warning: Address range of region \"%s\" "
222 "exceeds the current chip's address space.\n", entry->name);
223 if (entry->included)
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000224 ret = 1;
225 }
Nico Huber354766b2019-06-16 19:28:35 +0200226 if (entry->start > entry->end) {
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000227 msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n",
Nico Huber354766b2019-06-16 19:28:35 +0200228 entry->name);
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000229 ret = 1;
230 }
231 }
232
233 return ret;
234}
Nico Huber3d7b1e32018-12-22 00:53:14 +0100235
236const struct romentry *layout_next_included_region(
237 const struct flashrom_layout *const l, const chipoff_t where)
238{
Nico Huber354766b2019-06-16 19:28:35 +0200239 const struct romentry *entry = NULL, *lowest = NULL;
Nico Huber3d7b1e32018-12-22 00:53:14 +0100240
Nico Huber354766b2019-06-16 19:28:35 +0200241 while ((entry = layout_next(l, entry))) {
242 if (!entry->included)
Nico Huber3d7b1e32018-12-22 00:53:14 +0100243 continue;
Nico Huber354766b2019-06-16 19:28:35 +0200244 if (entry->end < where)
Nico Huber3d7b1e32018-12-22 00:53:14 +0100245 continue;
Nico Huber354766b2019-06-16 19:28:35 +0200246 if (!lowest || lowest->start > entry->start)
247 lowest = entry;
Nico Huber3d7b1e32018-12-22 00:53:14 +0100248 }
249
250 return lowest;
251}
Nico Huber4f213282019-06-15 17:33:49 +0200252
Nico Huber5ca55232019-06-15 22:29:08 +0200253const struct romentry *layout_next_included(
254 const struct flashrom_layout *const layout, const struct romentry *iterator)
255{
Nico Huber354766b2019-06-16 19:28:35 +0200256 while ((iterator = layout_next(layout, iterator))) {
257 if (iterator->included)
258 break;
Nico Huber5ca55232019-06-15 22:29:08 +0200259 }
Nico Huber354766b2019-06-16 19:28:35 +0200260 return iterator;
261}
262
263const struct romentry *layout_next(
264 const struct flashrom_layout *const layout, const struct romentry *iterator)
265{
Nico Huber49258612019-06-15 21:41:21 +0200266 return iterator ? iterator->next : layout->head;
Nico Huber5ca55232019-06-15 22:29:08 +0200267}
268
Nico Huber4f213282019-06-15 17:33:49 +0200269/**
270 * @addtogroup flashrom-layout
271 * @{
272 */
273
274/**
Nico Huber5bd990c2019-06-16 19:46:46 +0200275 * @brief Create a new, empty layout.
276 *
277 * @param layout Pointer to returned layout reference.
Nico Huber5bd990c2019-06-16 19:46:46 +0200278 *
279 * @return 0 on success,
280 * 1 if out of memory.
281 */
Nico Huber671c0f02019-06-16 20:17:19 +0200282int flashrom_layout_new(struct flashrom_layout **const layout)
Nico Huber5bd990c2019-06-16 19:46:46 +0200283{
Nico Huber49258612019-06-15 21:41:21 +0200284 *layout = malloc(sizeof(**layout));
Nico Huber5bd990c2019-06-16 19:46:46 +0200285 if (!*layout) {
286 msg_gerr("Error creating layout: %s\n", strerror(errno));
287 return 1;
288 }
289
Nico Huber49258612019-06-15 21:41:21 +0200290 const struct flashrom_layout tmp = { 0 };
Nico Huber5bd990c2019-06-16 19:46:46 +0200291 **layout = tmp;
292 return 0;
293}
294
295/**
Nico Huber92e0b622019-06-15 15:55:11 +0200296 * @brief Add another region to an existing layout.
297 *
298 * @param layout The existing layout.
299 * @param start Start address of the region.
300 * @param end End address (inclusive) of the region.
301 * @param name Name of the region.
302 *
303 * @return 0 on success,
Nico Huber49258612019-06-15 21:41:21 +0200304 * 1 if out of memory.
Nico Huber92e0b622019-06-15 15:55:11 +0200305 */
306int flashrom_layout_add_region(
307 struct flashrom_layout *const layout,
308 const size_t start, const size_t end, const char *const name)
309{
Nico Huber49258612019-06-15 21:41:21 +0200310 struct romentry *const entry = malloc(sizeof(*entry));
311 if (!entry)
312 goto _err_ret;
Nico Huber92e0b622019-06-15 15:55:11 +0200313
Nico Huber49258612019-06-15 21:41:21 +0200314 const struct romentry tmp = {
315 .next = layout->head,
316 .start = start,
317 .end = end,
318 .included = false,
319 .name = strdup(name),
320 };
321 *entry = tmp;
322 if (!entry->name)
323 goto _err_ret;
Nico Huber92e0b622019-06-15 15:55:11 +0200324
325 msg_gdbg("Added layout entry %08zx - %08zx named %s\n", start, end, name);
Nico Huber49258612019-06-15 21:41:21 +0200326 layout->head = entry;
Nico Huber92e0b622019-06-15 15:55:11 +0200327 return 0;
Nico Huber49258612019-06-15 21:41:21 +0200328
329_err_ret:
330 msg_gerr("Error adding layout entry: %s\n", strerror(errno));
331 free(entry);
332 return 1;
Nico Huber92e0b622019-06-15 15:55:11 +0200333}
334
335/**
Nico Huber4f213282019-06-15 17:33:49 +0200336 * @brief Mark given region as included.
337 *
338 * @param layout The layout to alter.
339 * @param name The name of the region to include.
340 *
341 * @return 0 on success,
342 * 1 if the given name can't be found.
343 */
344int flashrom_layout_include_region(struct flashrom_layout *const layout, const char *name)
345{
Nico Huber354766b2019-06-16 19:28:35 +0200346 struct romentry *entry = NULL;
347 while ((entry = mutable_layout_next(layout, entry))) {
348 if (!strcmp(entry->name, name)) {
349 entry->included = true;
Nico Huber4f213282019-06-15 17:33:49 +0200350 return 0;
351 }
352 }
353 return 1;
354}
355
356/**
357 * @brief Free a layout.
358 *
359 * @param layout Layout to free.
360 */
361void flashrom_layout_release(struct flashrom_layout *const layout)
362{
Nico Huber49258612019-06-15 21:41:21 +0200363 if (layout == global_layout)
Nico Huber4f213282019-06-15 17:33:49 +0200364 return;
365
Nico Huber49258612019-06-15 21:41:21 +0200366 if (!layout)
367 return;
368
369 while (layout->head) {
370 struct romentry *const entry = layout->head;
371 layout->head = entry->next;
372 free(entry->name);
373 free(entry);
374 }
Nico Huber4f213282019-06-15 17:33:49 +0200375 free(layout);
376}
377
378/** @} */ /* end flashrom-layout */