blob: f75778335c24a4146aa2a1554f0d10dc93a35556 [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];
27static struct flashrom_layout layout = { entries, 0 };
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000028
29/* include_args holds the arguments specified at the command line with -i. They must be processed at some point
Nico Huber3a9939b2016-04-27 15:56:14 +020030 * so that desired regions are marked as "included" in the layout. */
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000031static char *include_args[MAX_ROMLAYOUT];
32static int num_include_args = 0; /* the number of valid include_args. */
Ollie Lho184a4042005-11-26 21:55:36 +000033
Nico Huber305f4172013-06-14 11:55:26 +020034struct flashrom_layout *get_global_layout(void)
Nico Huber3a9939b2016-04-27 15:56:14 +020035{
36 return &layout;
37}
38
dhendrixbeaefe02017-09-03 18:06:53 -070039const struct flashrom_layout *get_layout(const struct flashrom_flashctx *const flashctx)
40{
41 if (flashctx->layout && flashctx->layout->num_entries)
42 return flashctx->layout;
43 else
44 return &flashctx->fallback_layout.base;
45}
46
Patrick Georgia9095a92010-09-30 17:03:32 +000047#ifndef __LIBPAYLOAD__
Mark Marshallf20b7be2014-05-09 21:16:21 +000048int read_romlayout(const char *name)
Ollie Lho184a4042005-11-26 21:55:36 +000049{
50 FILE *romlayout;
51 char tempstr[256];
52 int i;
53
Uwe Hermanna7e05482007-05-09 10:17:44 +000054 romlayout = fopen(name, "r");
55
56 if (!romlayout) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000057 msg_gerr("ERROR: Could not open ROM layout (%s).\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +000058 name);
Ollie Lho184a4042005-11-26 21:55:36 +000059 return -1;
60 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000061
62 while (!feof(romlayout)) {
Ollie Lho184a4042005-11-26 21:55:36 +000063 char *tstr1, *tstr2;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000064
Nico Huber3a9939b2016-04-27 15:56:14 +020065 if (layout.num_entries >= MAX_ROMLAYOUT) {
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000066 msg_gerr("Maximum number of ROM images (%i) in layout "
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +000067 "file reached.\n", MAX_ROMLAYOUT);
Stefan Tauner16687702015-12-25 21:59:45 +000068 (void)fclose(romlayout);
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +000069 return 1;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000070 }
Nico Huber3a9939b2016-04-27 15:56:14 +020071 if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, layout.entries[layout.num_entries].name))
Peter Stuge1fec0f32009-01-12 21:00:35 +000072 continue;
Ollie Lho184a4042005-11-26 21:55:36 +000073#if 0
74 // fscanf does not like arbitrary comments like that :( later
Uwe Hermanna7e05482007-05-09 10:17:44 +000075 if (tempstr[0] == '#') {
Ollie Lho184a4042005-11-26 21:55:36 +000076 continue;
77 }
78#endif
Uwe Hermanna7e05482007-05-09 10:17:44 +000079 tstr1 = strtok(tempstr, ":");
80 tstr2 = strtok(NULL, ":");
Uwe Hermann58783e32008-12-22 16:42:59 +000081 if (!tstr1 || !tstr2) {
Stefan Taunereb582572012-09-21 12:52:50 +000082 msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr);
Stefan Tauner16687702015-12-25 21:59:45 +000083 (void)fclose(romlayout);
Uwe Hermann58783e32008-12-22 16:42:59 +000084 return 1;
85 }
Nico Huber3a9939b2016-04-27 15:56:14 +020086 layout.entries[layout.num_entries].start = strtol(tstr1, (char **)NULL, 16);
87 layout.entries[layout.num_entries].end = strtol(tstr2, (char **)NULL, 16);
88 layout.entries[layout.num_entries].included = 0;
89 layout.num_entries++;
Ollie Lho184a4042005-11-26 21:55:36 +000090 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000091
Nico Huber3a9939b2016-04-27 15:56:14 +020092 for (i = 0; i < layout.num_entries; i++) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000093 msg_gdbg("romlayout %08x - %08x named %s\n",
Nico Huber3a9939b2016-04-27 15:56:14 +020094 layout.entries[i].start,
95 layout.entries[i].end, layout.entries[i].name);
Ollie Lho184a4042005-11-26 21:55:36 +000096 }
97
Stefan Tauner16687702015-12-25 21:59:45 +000098 (void)fclose(romlayout);
Uwe Hermannffec5f32007-08-23 16:08:21 +000099
Uwe Hermanna7e05482007-05-09 10:17:44 +0000100 return 0;
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
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000104/* returns the index of the entry (or a negative value if it is not found) */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000105static int find_include_arg(const char *const name)
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000106{
107 unsigned int i;
108 for (i = 0; i < num_include_args; i++) {
109 if (!strcmp(include_args[i], name))
110 return i;
111 }
112 return -1;
113}
114
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000115/* register an include argument (-i) for later processing */
116int register_include_arg(char *name)
117{
118 if (num_include_args >= MAX_ROMLAYOUT) {
119 msg_gerr("Too many regions included (%i).\n", num_include_args);
120 return 1;
121 }
122
123 if (name == NULL) {
124 msg_gerr("<NULL> is a bad region name.\n");
125 return 1;
126 }
127
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000128 if (find_include_arg(name) != -1) {
129 msg_gerr("Duplicate region name: \"%s\".\n", name);
130 return 1;
131 }
132
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000133 include_args[num_include_args] = name;
134 num_include_args++;
135 return 0;
136}
137
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100138/* returns -1 if an entry is not found, 0 if found. */
Nico Huber305f4172013-06-14 11:55:26 +0200139static int find_romentry(struct flashrom_layout *const l, char *name)
Ollie Lho184a4042005-11-26 21:55:36 +0000140{
Nico Huber305f4172013-06-14 11:55:26 +0200141 if (l->num_entries == 0)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000142 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000143
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000144 msg_gspew("Looking for region \"%s\"... ", name);
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100145 if (flashrom_layout_include_region(l, name)) {
146 msg_gspew("not found.\n");
147 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000148 }
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100149 msg_gspew("found.\n");
150 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000151}
152
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000153/* process -i arguments
154 * returns 0 to indicate success, >0 to indicate failure
155 */
Nico Huber305f4172013-06-14 11:55:26 +0200156int process_include_args(struct flashrom_layout *const l)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000157{
158 int i;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000159
160 if (num_include_args == 0)
161 return 0;
162
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000163 /* User has specified an area, but no layout file is loaded. */
Nico Huber305f4172013-06-14 11:55:26 +0200164 if (l->num_entries == 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000165 msg_gerr("Region requested (with -i \"%s\"), "
166 "but no layout data is available.\n",
167 include_args[0]);
168 return 1;
169 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000170
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000171 for (i = 0; i < num_include_args; i++) {
Nico Huber305f4172013-06-14 11:55:26 +0200172 if (find_romentry(l, include_args[i]) < 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000173 msg_gerr("Invalid region specified: \"%s\".\n",
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000174 include_args[i]);
175 return 1;
176 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000177 }
178
179 msg_ginfo("Using region%s: \"%s\"", num_include_args > 1 ? "s" : "",
180 include_args[0]);
181 for (i = 1; i < num_include_args; i++)
182 msg_ginfo(", \"%s\"", include_args[i]);
183 msg_ginfo(".\n");
184 return 0;
185}
186
Stefan Tauner949ccc82013-09-15 14:01:06 +0000187void layout_cleanup(void)
188{
189 int i;
190 for (i = 0; i < num_include_args; i++) {
191 free(include_args[i]);
192 include_args[i] = NULL;
193 }
194 num_include_args = 0;
195
Nico Huber3a9939b2016-04-27 15:56:14 +0200196 for (i = 0; i < layout.num_entries; i++) {
197 layout.entries[i].included = 0;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000198 }
Nico Huber3a9939b2016-04-27 15:56:14 +0200199 layout.num_entries = 0;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000200}
201
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000202/* Validate and - if needed - normalize layout entries. */
203int normalize_romentries(const struct flashctx *flash)
204{
205 chipsize_t total_size = flash->chip->total_size * 1024;
206 int ret = 0;
207
208 int i;
Nico Huber3a9939b2016-04-27 15:56:14 +0200209 for (i = 0; i < layout.num_entries; i++) {
210 if (layout.entries[i].start >= total_size || layout.entries[i].end >= total_size) {
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000211 msg_gwarn("Warning: Address range of region \"%s\" exceeds the current chip's "
Nico Huber3a9939b2016-04-27 15:56:14 +0200212 "address space.\n", layout.entries[i].name);
213 if (layout.entries[i].included)
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000214 ret = 1;
215 }
Nico Huber3a9939b2016-04-27 15:56:14 +0200216 if (layout.entries[i].start > layout.entries[i].end) {
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000217 msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n",
Nico Huber3a9939b2016-04-27 15:56:14 +0200218 layout.entries[i].name);
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000219 ret = 1;
220 }
221 }
222
223 return ret;
224}
Nico Huber3d7b1e32018-12-22 00:53:14 +0100225
226const struct romentry *layout_next_included_region(
227 const struct flashrom_layout *const l, const chipoff_t where)
228{
229 unsigned int i;
230 const struct romentry *lowest = NULL;
231
232 for (i = 0; i < l->num_entries; ++i) {
233 if (!l->entries[i].included)
234 continue;
235 if (l->entries[i].end < where)
236 continue;
237 if (!lowest || lowest->start > l->entries[i].start)
238 lowest = &l->entries[i];
239 }
240
241 return lowest;
242}