blob: 6963e61a12b0d7ed2dce73ddaeb9bdad26b30e5f [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
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000018#include <stdio.h>
Ollie Lho184a4042005-11-26 21:55:36 +000019#include <stdlib.h>
20#include <string.h>
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +000021#include <limits.h>
Uwe Hermann0846f892007-08-23 13:34:59 +000022#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000023#include "programmer.h"
Nico Huber3a9939b2016-04-27 15:56:14 +020024#include "layout.h"
Ollie Lho184a4042005-11-26 21:55:36 +000025
Nico Huber3a9939b2016-04-27 15:56:14 +020026struct romentry entries[MAX_ROMLAYOUT];
Nico Huber2b94cdb2019-06-15 18:19:26 +020027static struct flashrom_layout global_layout = { entries, 0 };
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000028
Nico Huber305f4172013-06-14 11:55:26 +020029struct flashrom_layout *get_global_layout(void)
Nico Huber3a9939b2016-04-27 15:56:14 +020030{
Nico Huber2b94cdb2019-06-15 18:19:26 +020031 return &global_layout;
Nico Huber3a9939b2016-04-27 15:56:14 +020032}
33
dhendrixbeaefe02017-09-03 18:06:53 -070034const struct flashrom_layout *get_layout(const struct flashrom_flashctx *const flashctx)
35{
36 if (flashctx->layout && flashctx->layout->num_entries)
37 return flashctx->layout;
38 else
39 return &flashctx->fallback_layout.base;
40}
41
Patrick Georgia9095a92010-09-30 17:03:32 +000042#ifndef __LIBPAYLOAD__
Mark Marshallf20b7be2014-05-09 21:16:21 +000043int read_romlayout(const char *name)
Ollie Lho184a4042005-11-26 21:55:36 +000044{
Nico Huber2b94cdb2019-06-15 18:19:26 +020045 struct flashrom_layout *const layout = get_global_layout();
Ollie Lho184a4042005-11-26 21:55:36 +000046 FILE *romlayout;
47 char tempstr[256];
48 int i;
49
Uwe Hermanna7e05482007-05-09 10:17:44 +000050 romlayout = fopen(name, "r");
51
52 if (!romlayout) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000053 msg_gerr("ERROR: Could not open ROM layout (%s).\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +000054 name);
Ollie Lho184a4042005-11-26 21:55:36 +000055 return -1;
56 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000057
58 while (!feof(romlayout)) {
Ollie Lho184a4042005-11-26 21:55:36 +000059 char *tstr1, *tstr2;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000060
Nico Huber2b94cdb2019-06-15 18:19:26 +020061 if (layout->num_entries >= MAX_ROMLAYOUT) {
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000062 msg_gerr("Maximum number of ROM images (%i) in layout "
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +000063 "file reached.\n", MAX_ROMLAYOUT);
Stefan Tauner16687702015-12-25 21:59:45 +000064 (void)fclose(romlayout);
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +000065 return 1;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000066 }
Nico Huber2b94cdb2019-06-15 18:19:26 +020067 if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, layout->entries[layout->num_entries].name))
Peter Stuge1fec0f32009-01-12 21:00:35 +000068 continue;
Ollie Lho184a4042005-11-26 21:55:36 +000069#if 0
70 // fscanf does not like arbitrary comments like that :( later
Uwe Hermanna7e05482007-05-09 10:17:44 +000071 if (tempstr[0] == '#') {
Ollie Lho184a4042005-11-26 21:55:36 +000072 continue;
73 }
74#endif
Uwe Hermanna7e05482007-05-09 10:17:44 +000075 tstr1 = strtok(tempstr, ":");
76 tstr2 = strtok(NULL, ":");
Uwe Hermann58783e32008-12-22 16:42:59 +000077 if (!tstr1 || !tstr2) {
Stefan Taunereb582572012-09-21 12:52:50 +000078 msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr);
Stefan Tauner16687702015-12-25 21:59:45 +000079 (void)fclose(romlayout);
Uwe Hermann58783e32008-12-22 16:42:59 +000080 return 1;
81 }
Nico Huber2b94cdb2019-06-15 18:19:26 +020082 layout->entries[layout->num_entries].start = strtol(tstr1, (char **)NULL, 16);
83 layout->entries[layout->num_entries].end = strtol(tstr2, (char **)NULL, 16);
84 layout->entries[layout->num_entries].included = 0;
85 layout->num_entries++;
Ollie Lho184a4042005-11-26 21:55:36 +000086 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000087
Nico Huber2b94cdb2019-06-15 18:19:26 +020088 for (i = 0; i < layout->num_entries; i++) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000089 msg_gdbg("romlayout %08x - %08x named %s\n",
Nico Huber2b94cdb2019-06-15 18:19:26 +020090 layout->entries[i].start,
91 layout->entries[i].end, layout->entries[i].name);
Ollie Lho184a4042005-11-26 21:55:36 +000092 }
93
Stefan Tauner16687702015-12-25 21:59:45 +000094 (void)fclose(romlayout);
Uwe Hermannffec5f32007-08-23 16:08:21 +000095
Uwe Hermanna7e05482007-05-09 10:17:44 +000096 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +000097}
Patrick Georgia9095a92010-09-30 17:03:32 +000098#endif
Ollie Lho184a4042005-11-26 21:55:36 +000099
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000100/* register an include argument (-i) for later processing */
Arthur Heymansb04fef92019-02-05 17:35:05 +0100101int register_include_arg(struct layout_include_args **args, char *name)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000102{
Arthur Heymansb04fef92019-02-05 17:35:05 +0100103 struct layout_include_args *tmp;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000104 if (name == NULL) {
105 msg_gerr("<NULL> is a bad region name.\n");
106 return 1;
107 }
108
Arthur Heymansb04fef92019-02-05 17:35:05 +0100109 tmp = *args;
110 while (tmp) {
111 if (!strcmp(tmp->name, name)) {
112 msg_gerr("Duplicate region name: \"%s\".\n", name);
113 return 1;
114 }
115 tmp = tmp->next;
116 }
117
118 tmp = malloc(sizeof(struct layout_include_args));
119 if (tmp == NULL) {
120 msg_gerr("Could not allocate memory");
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000121 return 1;
122 }
123
Arthur Heymansb04fef92019-02-05 17:35:05 +0100124 tmp->name = name;
125 tmp->next = *args;
126 *args = tmp;
127
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000128 return 0;
129}
130
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100131/* returns -1 if an entry is not found, 0 if found. */
Nico Huber305f4172013-06-14 11:55:26 +0200132static int find_romentry(struct flashrom_layout *const l, char *name)
Ollie Lho184a4042005-11-26 21:55:36 +0000133{
Nico Huber305f4172013-06-14 11:55:26 +0200134 if (l->num_entries == 0)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000135 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000136
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000137 msg_gspew("Looking for region \"%s\"... ", name);
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100138 if (flashrom_layout_include_region(l, name)) {
139 msg_gspew("not found.\n");
140 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000141 }
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100142 msg_gspew("found.\n");
143 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000144}
145
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000146/* process -i arguments
147 * returns 0 to indicate success, >0 to indicate failure
148 */
Arthur Heymansb04fef92019-02-05 17:35:05 +0100149int process_include_args(struct flashrom_layout *l, const struct layout_include_args *const args)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000150{
Arthur Heymansb04fef92019-02-05 17:35:05 +0100151 int found = 0;
152 const struct layout_include_args *tmp;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000153
Arthur Heymansb04fef92019-02-05 17:35:05 +0100154 if (args == NULL)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000155 return 0;
156
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000157 /* User has specified an area, but no layout file is loaded. */
Nico Huber305f4172013-06-14 11:55:26 +0200158 if (l->num_entries == 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000159 msg_gerr("Region requested (with -i \"%s\"), "
160 "but no layout data is available.\n",
Arthur Heymansb04fef92019-02-05 17:35:05 +0100161 args->name);
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000162 return 1;
163 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000164
Arthur Heymansb04fef92019-02-05 17:35:05 +0100165 tmp = args;
166 while (tmp) {
167 if (find_romentry(l, tmp->name) < 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000168 msg_gerr("Invalid region specified: \"%s\".\n",
Arthur Heymansb04fef92019-02-05 17:35:05 +0100169 tmp->name);
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000170 return 1;
171 }
Arthur Heymansb04fef92019-02-05 17:35:05 +0100172 tmp = tmp->next;
173 found++;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000174 }
175
Arthur Heymansb04fef92019-02-05 17:35:05 +0100176 msg_ginfo("Using region%s:", found > 1 ? "s" : "");
177 tmp = args;
178 while (tmp) {
179 msg_ginfo(" \"%s\"%s", tmp->name, found > 1 ? "," : "");
180 found--;
181 tmp = tmp->next;
182 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000183 msg_ginfo(".\n");
184 return 0;
185}
186
Arthur Heymansb04fef92019-02-05 17:35:05 +0100187void layout_cleanup(struct layout_include_args **args)
Stefan Tauner949ccc82013-09-15 14:01:06 +0000188{
Nico Huber2b94cdb2019-06-15 18:19:26 +0200189 struct flashrom_layout *const layout = get_global_layout();
Stefan Tauner949ccc82013-09-15 14:01:06 +0000190 int i;
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
Nico Huber2b94cdb2019-06-15 18:19:26 +0200199 for (i = 0; i < layout->num_entries; i++) {
200 layout->entries[i].included = 0;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000201 }
Nico Huber2b94cdb2019-06-15 18:19:26 +0200202 layout->num_entries = 0;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000203}
204
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000205/* Validate and - if needed - normalize layout entries. */
206int normalize_romentries(const struct flashctx *flash)
207{
Nico Huber2b94cdb2019-06-15 18:19:26 +0200208 struct flashrom_layout *const layout = get_global_layout();
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000209 chipsize_t total_size = flash->chip->total_size * 1024;
210 int ret = 0;
211
212 int i;
Nico Huber2b94cdb2019-06-15 18:19:26 +0200213 for (i = 0; i < layout->num_entries; i++) {
214 if (layout->entries[i].start >= total_size || layout->entries[i].end >= total_size) {
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000215 msg_gwarn("Warning: Address range of region \"%s\" exceeds the current chip's "
Nico Huber2b94cdb2019-06-15 18:19:26 +0200216 "address space.\n", layout->entries[i].name);
217 if (layout->entries[i].included)
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000218 ret = 1;
219 }
Nico Huber2b94cdb2019-06-15 18:19:26 +0200220 if (layout->entries[i].start > layout->entries[i].end) {
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000221 msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n",
Nico Huber2b94cdb2019-06-15 18:19:26 +0200222 layout->entries[i].name);
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000223 ret = 1;
224 }
225 }
226
227 return ret;
228}
Nico Huber3d7b1e32018-12-22 00:53:14 +0100229
230const struct romentry *layout_next_included_region(
231 const struct flashrom_layout *const l, const chipoff_t where)
232{
233 unsigned int i;
234 const struct romentry *lowest = NULL;
235
236 for (i = 0; i < l->num_entries; ++i) {
237 if (!l->entries[i].included)
238 continue;
239 if (l->entries[i].end < where)
240 continue;
241 if (!lowest || lowest->start > l->entries[i].start)
242 lowest = &l->entries[i];
243 }
244
245 return lowest;
246}
Nico Huber4f213282019-06-15 17:33:49 +0200247
248/**
249 * @addtogroup flashrom-layout
250 * @{
251 */
252
253/**
254 * @brief Mark given region as included.
255 *
256 * @param layout The layout to alter.
257 * @param name The name of the region to include.
258 *
259 * @return 0 on success,
260 * 1 if the given name can't be found.
261 */
262int flashrom_layout_include_region(struct flashrom_layout *const layout, const char *name)
263{
264 size_t i;
265 for (i = 0; i < layout->num_entries; ++i) {
266 if (!strcmp(layout->entries[i].name, name)) {
267 layout->entries[i].included = true;
268 return 0;
269 }
270 }
271 return 1;
272}
273
274/**
275 * @brief Free a layout.
276 *
277 * @param layout Layout to free.
278 */
279void flashrom_layout_release(struct flashrom_layout *const layout)
280{
281 if (layout == get_global_layout())
282 return;
283
284 free(layout);
285}
286
287/** @} */ /* end flashrom-layout */