blob: d6450785deb10cd071223cf47073c2bef76def7e [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 Huber8edcd152019-06-16 03:22:58 +020036const struct flashrom_layout *get_default_layout(const struct flashrom_flashctx *const flashctx)
37{
38 return flashctx->default_layout;
39}
40
dhendrixbeaefe02017-09-03 18:06:53 -070041const struct flashrom_layout *get_layout(const struct flashrom_flashctx *const flashctx)
42{
Nico Huber49258612019-06-15 21:41:21 +020043 if (flashctx->layout)
dhendrixbeaefe02017-09-03 18:06:53 -070044 return flashctx->layout;
45 else
Nico Huber8edcd152019-06-16 03:22:58 +020046 return get_default_layout(flashctx);
dhendrixbeaefe02017-09-03 18:06:53 -070047}
48
Nico Huber354766b2019-06-16 19:28:35 +020049static struct romentry *mutable_layout_next(
50 const struct flashrom_layout *const layout, struct romentry *iterator)
51{
Nico Huber49258612019-06-15 21:41:21 +020052 return iterator ? iterator->next : layout->head;
Nico Huber354766b2019-06-16 19:28:35 +020053}
54
Patrick Georgia9095a92010-09-30 17:03:32 +000055#ifndef __LIBPAYLOAD__
Nico Huberd9b57712021-05-14 01:07:28 +020056int layout_from_file(struct flashrom_layout **layout, const char *name)
Ollie Lho184a4042005-11-26 21:55:36 +000057{
58 FILE *romlayout;
Nico Huber70461a92019-06-15 14:56:19 +020059 char tempstr[256], tempname[256];
Nico Huber519be662018-12-23 20:03:35 +010060 int ret = 1;
Ollie Lho184a4042005-11-26 21:55:36 +000061
Nico Huberd9b57712021-05-14 01:07:28 +020062 if (flashrom_layout_new(layout))
63 return 1;
64
Uwe Hermanna7e05482007-05-09 10:17:44 +000065 romlayout = fopen(name, "r");
66
67 if (!romlayout) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000068 msg_gerr("ERROR: Could not open ROM layout (%s).\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +000069 name);
Ollie Lho184a4042005-11-26 21:55:36 +000070 return -1;
71 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000072
73 while (!feof(romlayout)) {
Ollie Lho184a4042005-11-26 21:55:36 +000074 char *tstr1, *tstr2;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000075
Nico Huber70461a92019-06-15 14:56:19 +020076 if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, tempname))
Peter Stuge1fec0f32009-01-12 21:00:35 +000077 continue;
Ollie Lho184a4042005-11-26 21:55:36 +000078#if 0
79 // fscanf does not like arbitrary comments like that :( later
Uwe Hermanna7e05482007-05-09 10:17:44 +000080 if (tempstr[0] == '#') {
Ollie Lho184a4042005-11-26 21:55:36 +000081 continue;
82 }
83#endif
Uwe Hermanna7e05482007-05-09 10:17:44 +000084 tstr1 = strtok(tempstr, ":");
85 tstr2 = strtok(NULL, ":");
Uwe Hermann58783e32008-12-22 16:42:59 +000086 if (!tstr1 || !tstr2) {
Stefan Taunereb582572012-09-21 12:52:50 +000087 msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr);
Nico Huber70461a92019-06-15 14:56:19 +020088 goto _close_ret;
Uwe Hermann58783e32008-12-22 16:42:59 +000089 }
Nico Huberd9b57712021-05-14 01:07:28 +020090 if (flashrom_layout_add_region(*layout,
Nico Huber92e0b622019-06-15 15:55:11 +020091 strtol(tstr1, NULL, 16), strtol(tstr2, NULL, 16), tempname))
Nico Huber70461a92019-06-15 14:56:19 +020092 goto _close_ret;
Ollie Lho184a4042005-11-26 21:55:36 +000093 }
Nico Huber70461a92019-06-15 14:56:19 +020094 ret = 0;
Uwe Hermannffec5f32007-08-23 16:08:21 +000095
Nico Huber70461a92019-06-15 14:56:19 +020096_close_ret:
97 (void)fclose(romlayout);
98 return ret;
Ollie Lho184a4042005-11-26 21:55:36 +000099}
Patrick Georgia9095a92010-09-30 17:03:32 +0000100#endif
Ollie Lho184a4042005-11-26 21:55:36 +0000101
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000102/* register an include argument (-i) for later processing */
Arthur Heymansb04fef92019-02-05 17:35:05 +0100103int register_include_arg(struct layout_include_args **args, char *name)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000104{
Arthur Heymansb04fef92019-02-05 17:35:05 +0100105 struct layout_include_args *tmp;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000106 if (name == NULL) {
107 msg_gerr("<NULL> is a bad region name.\n");
108 return 1;
109 }
110
Arthur Heymansb04fef92019-02-05 17:35:05 +0100111 tmp = *args;
112 while (tmp) {
113 if (!strcmp(tmp->name, name)) {
114 msg_gerr("Duplicate region name: \"%s\".\n", name);
115 return 1;
116 }
117 tmp = tmp->next;
118 }
119
Angel Pons690a9442021-06-07 12:33:53 +0200120 tmp = malloc(sizeof(*tmp));
Arthur Heymansb04fef92019-02-05 17:35:05 +0100121 if (tmp == NULL) {
Anastasia Klimchuk7310f192022-12-10 19:38:52 +1100122 msg_gerr("Out of memory\n");
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000123 return 1;
124 }
125
Arthur Heymansb04fef92019-02-05 17:35:05 +0100126 tmp->name = name;
127 tmp->next = *args;
128 *args = tmp;
129
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000130 return 0;
131}
132
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100133/* returns -1 if an entry is not found, 0 if found. */
Nico Huber305f4172013-06-14 11:55:26 +0200134static int find_romentry(struct flashrom_layout *const l, char *name)
Ollie Lho184a4042005-11-26 21:55:36 +0000135{
Nico Huber49258612019-06-15 21:41:21 +0200136 if (!l->head)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000137 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000138
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000139 msg_gspew("Looking for region \"%s\"... ", name);
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100140 if (flashrom_layout_include_region(l, name)) {
141 msg_gspew("not found.\n");
142 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000143 }
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100144 msg_gspew("found.\n");
145 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000146}
147
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000148/* process -i arguments
149 * returns 0 to indicate success, >0 to indicate failure
150 */
Arthur Heymansb04fef92019-02-05 17:35:05 +0100151int process_include_args(struct flashrom_layout *l, const struct layout_include_args *const args)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000152{
Nico Huber519be662018-12-23 20:03:35 +0100153 unsigned int found = 0;
Arthur Heymansb04fef92019-02-05 17:35:05 +0100154 const struct layout_include_args *tmp;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000155
Arthur Heymansb04fef92019-02-05 17:35:05 +0100156 if (args == NULL)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000157 return 0;
158
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000159 /* User has specified an area, but no layout file is loaded. */
Nico Huberd9b57712021-05-14 01:07:28 +0200160 if (!l || !l->head) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000161 msg_gerr("Region requested (with -i \"%s\"), "
162 "but no layout data is available.\n",
Arthur Heymansb04fef92019-02-05 17:35:05 +0100163 args->name);
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000164 return 1;
165 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000166
Arthur Heymansb04fef92019-02-05 17:35:05 +0100167 tmp = args;
168 while (tmp) {
169 if (find_romentry(l, tmp->name) < 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000170 msg_gerr("Invalid region specified: \"%s\".\n",
Arthur Heymansb04fef92019-02-05 17:35:05 +0100171 tmp->name);
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000172 return 1;
173 }
Arthur Heymansb04fef92019-02-05 17:35:05 +0100174 tmp = tmp->next;
175 found++;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000176 }
177
Arthur Heymansb04fef92019-02-05 17:35:05 +0100178 msg_ginfo("Using region%s:", found > 1 ? "s" : "");
179 tmp = args;
180 while (tmp) {
181 msg_ginfo(" \"%s\"%s", tmp->name, found > 1 ? "," : "");
182 found--;
183 tmp = tmp->next;
184 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000185 msg_ginfo(".\n");
186 return 0;
187}
188
Nico Huber345f65a2021-05-14 01:11:08 +0200189void cleanup_include_args(struct layout_include_args **args)
Stefan Tauner949ccc82013-09-15 14:01:06 +0000190{
Arthur Heymansb04fef92019-02-05 17:35:05 +0100191 struct layout_include_args *tmp;
192
193 while (*args) {
194 tmp = (*args)->next;
195 free(*args);
196 *args = tmp;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000197 }
Stefan Tauner949ccc82013-09-15 14:01:06 +0000198}
199
Nico Hubere0ed4122021-05-14 00:48:28 +0200200int layout_sanity_checks(const struct flashrom_flashctx *const flash)
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000201{
Nico Hubere0ed4122021-05-14 00:48:28 +0200202 const struct flashrom_layout *const layout = get_layout(flash);
203 const chipsize_t total_size = flash->chip->total_size * 1024;
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000204 int ret = 0;
205
Nico Huber354766b2019-06-16 19:28:35 +0200206 const struct romentry *entry = NULL;
207 while ((entry = layout_next(layout, entry))) {
208 if (entry->start >= total_size || entry->end >= total_size) {
209 msg_gwarn("Warning: Address range of region \"%s\" "
210 "exceeds the current chip's address space.\n", entry->name);
211 if (entry->included)
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000212 ret = 1;
213 }
Nico Huber354766b2019-06-16 19:28:35 +0200214 if (entry->start > entry->end) {
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000215 msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n",
Nico Huber354766b2019-06-16 19:28:35 +0200216 entry->name);
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000217 ret = 1;
218 }
219 }
220
221 return ret;
222}
Nico Huber3d7b1e32018-12-22 00:53:14 +0100223
224const struct romentry *layout_next_included_region(
225 const struct flashrom_layout *const l, const chipoff_t where)
226{
Nico Huber354766b2019-06-16 19:28:35 +0200227 const struct romentry *entry = NULL, *lowest = NULL;
Nico Huber3d7b1e32018-12-22 00:53:14 +0100228
Nico Huber354766b2019-06-16 19:28:35 +0200229 while ((entry = layout_next(l, entry))) {
230 if (!entry->included)
Nico Huber3d7b1e32018-12-22 00:53:14 +0100231 continue;
Nico Huber354766b2019-06-16 19:28:35 +0200232 if (entry->end < where)
Nico Huber3d7b1e32018-12-22 00:53:14 +0100233 continue;
Nico Huber354766b2019-06-16 19:28:35 +0200234 if (!lowest || lowest->start > entry->start)
235 lowest = entry;
Nico Huber3d7b1e32018-12-22 00:53:14 +0100236 }
237
238 return lowest;
239}
Nico Huber4f213282019-06-15 17:33:49 +0200240
Nico Huber5ca55232019-06-15 22:29:08 +0200241const struct romentry *layout_next_included(
242 const struct flashrom_layout *const layout, const struct romentry *iterator)
243{
Nico Huber354766b2019-06-16 19:28:35 +0200244 while ((iterator = layout_next(layout, iterator))) {
245 if (iterator->included)
246 break;
Nico Huber5ca55232019-06-15 22:29:08 +0200247 }
Nico Huber354766b2019-06-16 19:28:35 +0200248 return iterator;
249}
250
251const struct romentry *layout_next(
252 const struct flashrom_layout *const layout, const struct romentry *iterator)
253{
Nico Huber49258612019-06-15 21:41:21 +0200254 return iterator ? iterator->next : layout->head;
Nico Huber5ca55232019-06-15 22:29:08 +0200255}
256
Nico Huber4f213282019-06-15 17:33:49 +0200257/**
258 * @addtogroup flashrom-layout
259 * @{
260 */
261
262/**
Nico Huber5bd990c2019-06-16 19:46:46 +0200263 * @brief Create a new, empty layout.
264 *
265 * @param layout Pointer to returned layout reference.
Nico Huber5bd990c2019-06-16 19:46:46 +0200266 *
267 * @return 0 on success,
268 * 1 if out of memory.
269 */
Nico Huber671c0f02019-06-16 20:17:19 +0200270int flashrom_layout_new(struct flashrom_layout **const layout)
Nico Huber5bd990c2019-06-16 19:46:46 +0200271{
Nico Huber49258612019-06-15 21:41:21 +0200272 *layout = malloc(sizeof(**layout));
Nico Huber5bd990c2019-06-16 19:46:46 +0200273 if (!*layout) {
274 msg_gerr("Error creating layout: %s\n", strerror(errno));
275 return 1;
276 }
277
Nico Huber49258612019-06-15 21:41:21 +0200278 const struct flashrom_layout tmp = { 0 };
Nico Huber5bd990c2019-06-16 19:46:46 +0200279 **layout = tmp;
280 return 0;
281}
282
283/**
Nico Huber92e0b622019-06-15 15:55:11 +0200284 * @brief Add another region to an existing layout.
285 *
286 * @param layout The existing layout.
287 * @param start Start address of the region.
288 * @param end End address (inclusive) of the region.
289 * @param name Name of the region.
290 *
291 * @return 0 on success,
Nico Huber49258612019-06-15 21:41:21 +0200292 * 1 if out of memory.
Nico Huber92e0b622019-06-15 15:55:11 +0200293 */
294int flashrom_layout_add_region(
295 struct flashrom_layout *const layout,
296 const size_t start, const size_t end, const char *const name)
297{
Nico Huber49258612019-06-15 21:41:21 +0200298 struct romentry *const entry = malloc(sizeof(*entry));
299 if (!entry)
300 goto _err_ret;
Nico Huber92e0b622019-06-15 15:55:11 +0200301
Nico Huber49258612019-06-15 21:41:21 +0200302 const struct romentry tmp = {
303 .next = layout->head,
304 .start = start,
305 .end = end,
306 .included = false,
307 .name = strdup(name),
308 };
309 *entry = tmp;
310 if (!entry->name)
311 goto _err_ret;
Nico Huber92e0b622019-06-15 15:55:11 +0200312
313 msg_gdbg("Added layout entry %08zx - %08zx named %s\n", start, end, name);
Nico Huber49258612019-06-15 21:41:21 +0200314 layout->head = entry;
Nico Huber92e0b622019-06-15 15:55:11 +0200315 return 0;
Nico Huber49258612019-06-15 21:41:21 +0200316
317_err_ret:
318 msg_gerr("Error adding layout entry: %s\n", strerror(errno));
319 free(entry);
320 return 1;
Nico Huber92e0b622019-06-15 15:55:11 +0200321}
322
323/**
Nico Huber4f213282019-06-15 17:33:49 +0200324 * @brief Mark given region as included.
325 *
326 * @param layout The layout to alter.
327 * @param name The name of the region to include.
328 *
329 * @return 0 on success,
330 * 1 if the given name can't be found.
331 */
332int flashrom_layout_include_region(struct flashrom_layout *const layout, const char *name)
333{
Nico Huber354766b2019-06-16 19:28:35 +0200334 struct romentry *entry = NULL;
335 while ((entry = mutable_layout_next(layout, entry))) {
336 if (!strcmp(entry->name, name)) {
337 entry->included = true;
Nico Huber4f213282019-06-15 17:33:49 +0200338 return 0;
339 }
340 }
341 return 1;
342}
343
344/**
345 * @brief Free a layout.
346 *
347 * @param layout Layout to free.
348 */
349void flashrom_layout_release(struct flashrom_layout *const layout)
350{
Nico Huber49258612019-06-15 21:41:21 +0200351 if (!layout)
352 return;
353
354 while (layout->head) {
355 struct romentry *const entry = layout->head;
356 layout->head = entry->next;
357 free(entry->name);
358 free(entry);
359 }
Nico Huber4f213282019-06-15 17:33:49 +0200360 free(layout);
361}
362
363/** @} */ /* end flashrom-layout */