blob: 52a0c2eed8a1ecf3c488d131b10adda3124c7140 [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
Nico Huber305f4172013-06-14 11:55:26 +020029struct flashrom_layout *get_global_layout(void)
Nico Huber3a9939b2016-04-27 15:56:14 +020030{
31 return &layout;
32}
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{
45 FILE *romlayout;
46 char tempstr[256];
47 int i;
48
Uwe Hermanna7e05482007-05-09 10:17:44 +000049 romlayout = fopen(name, "r");
50
51 if (!romlayout) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000052 msg_gerr("ERROR: Could not open ROM layout (%s).\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +000053 name);
Ollie Lho184a4042005-11-26 21:55:36 +000054 return -1;
55 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000056
57 while (!feof(romlayout)) {
Ollie Lho184a4042005-11-26 21:55:36 +000058 char *tstr1, *tstr2;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000059
Nico Huber3a9939b2016-04-27 15:56:14 +020060 if (layout.num_entries >= MAX_ROMLAYOUT) {
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000061 msg_gerr("Maximum number of ROM images (%i) in layout "
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +000062 "file reached.\n", MAX_ROMLAYOUT);
Stefan Tauner16687702015-12-25 21:59:45 +000063 (void)fclose(romlayout);
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +000064 return 1;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000065 }
Nico Huber3a9939b2016-04-27 15:56:14 +020066 if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, layout.entries[layout.num_entries].name))
Peter Stuge1fec0f32009-01-12 21:00:35 +000067 continue;
Ollie Lho184a4042005-11-26 21:55:36 +000068#if 0
69 // fscanf does not like arbitrary comments like that :( later
Uwe Hermanna7e05482007-05-09 10:17:44 +000070 if (tempstr[0] == '#') {
Ollie Lho184a4042005-11-26 21:55:36 +000071 continue;
72 }
73#endif
Uwe Hermanna7e05482007-05-09 10:17:44 +000074 tstr1 = strtok(tempstr, ":");
75 tstr2 = strtok(NULL, ":");
Uwe Hermann58783e32008-12-22 16:42:59 +000076 if (!tstr1 || !tstr2) {
Stefan Taunereb582572012-09-21 12:52:50 +000077 msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr);
Stefan Tauner16687702015-12-25 21:59:45 +000078 (void)fclose(romlayout);
Uwe Hermann58783e32008-12-22 16:42:59 +000079 return 1;
80 }
Nico Huber3a9939b2016-04-27 15:56:14 +020081 layout.entries[layout.num_entries].start = strtol(tstr1, (char **)NULL, 16);
82 layout.entries[layout.num_entries].end = strtol(tstr2, (char **)NULL, 16);
83 layout.entries[layout.num_entries].included = 0;
84 layout.num_entries++;
Ollie Lho184a4042005-11-26 21:55:36 +000085 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000086
Nico Huber3a9939b2016-04-27 15:56:14 +020087 for (i = 0; i < layout.num_entries; i++) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000088 msg_gdbg("romlayout %08x - %08x named %s\n",
Nico Huber3a9939b2016-04-27 15:56:14 +020089 layout.entries[i].start,
90 layout.entries[i].end, layout.entries[i].name);
Ollie Lho184a4042005-11-26 21:55:36 +000091 }
92
Stefan Tauner16687702015-12-25 21:59:45 +000093 (void)fclose(romlayout);
Uwe Hermannffec5f32007-08-23 16:08:21 +000094
Uwe Hermanna7e05482007-05-09 10:17:44 +000095 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +000096}
Patrick Georgia9095a92010-09-30 17:03:32 +000097#endif
Ollie Lho184a4042005-11-26 21:55:36 +000098
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +000099/* register an include argument (-i) for later processing */
Arthur Heymansb04fef92019-02-05 17:35:05 +0100100int register_include_arg(struct layout_include_args **args, char *name)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000101{
Arthur Heymansb04fef92019-02-05 17:35:05 +0100102 struct layout_include_args *tmp;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000103 if (name == NULL) {
104 msg_gerr("<NULL> is a bad region name.\n");
105 return 1;
106 }
107
Arthur Heymansb04fef92019-02-05 17:35:05 +0100108 tmp = *args;
109 while (tmp) {
110 if (!strcmp(tmp->name, name)) {
111 msg_gerr("Duplicate region name: \"%s\".\n", name);
112 return 1;
113 }
114 tmp = tmp->next;
115 }
116
117 tmp = malloc(sizeof(struct layout_include_args));
118 if (tmp == NULL) {
119 msg_gerr("Could not allocate memory");
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000120 return 1;
121 }
122
Arthur Heymansb04fef92019-02-05 17:35:05 +0100123 tmp->name = name;
124 tmp->next = *args;
125 *args = tmp;
126
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000127 return 0;
128}
129
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100130/* returns -1 if an entry is not found, 0 if found. */
Nico Huber305f4172013-06-14 11:55:26 +0200131static int find_romentry(struct flashrom_layout *const l, char *name)
Ollie Lho184a4042005-11-26 21:55:36 +0000132{
Nico Huber305f4172013-06-14 11:55:26 +0200133 if (l->num_entries == 0)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000134 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000135
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000136 msg_gspew("Looking for region \"%s\"... ", name);
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100137 if (flashrom_layout_include_region(l, name)) {
138 msg_gspew("not found.\n");
139 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000140 }
Arthur Heymans32b9f5c2019-02-05 16:14:55 +0100141 msg_gspew("found.\n");
142 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000143}
144
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000145/* process -i arguments
146 * returns 0 to indicate success, >0 to indicate failure
147 */
Arthur Heymansb04fef92019-02-05 17:35:05 +0100148int process_include_args(struct flashrom_layout *l, const struct layout_include_args *const args)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000149{
Arthur Heymansb04fef92019-02-05 17:35:05 +0100150 int found = 0;
151 const struct layout_include_args *tmp;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000152
Arthur Heymansb04fef92019-02-05 17:35:05 +0100153 if (args == NULL)
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000154 return 0;
155
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000156 /* User has specified an area, but no layout file is loaded. */
Nico Huber305f4172013-06-14 11:55:26 +0200157 if (l->num_entries == 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000158 msg_gerr("Region requested (with -i \"%s\"), "
159 "but no layout data is available.\n",
Arthur Heymansb04fef92019-02-05 17:35:05 +0100160 args->name);
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000161 return 1;
162 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000163
Arthur Heymansb04fef92019-02-05 17:35:05 +0100164 tmp = args;
165 while (tmp) {
166 if (find_romentry(l, tmp->name) < 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000167 msg_gerr("Invalid region specified: \"%s\".\n",
Arthur Heymansb04fef92019-02-05 17:35:05 +0100168 tmp->name);
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000169 return 1;
170 }
Arthur Heymansb04fef92019-02-05 17:35:05 +0100171 tmp = tmp->next;
172 found++;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000173 }
174
Arthur Heymansb04fef92019-02-05 17:35:05 +0100175 msg_ginfo("Using region%s:", found > 1 ? "s" : "");
176 tmp = args;
177 while (tmp) {
178 msg_ginfo(" \"%s\"%s", tmp->name, found > 1 ? "," : "");
179 found--;
180 tmp = tmp->next;
181 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000182 msg_ginfo(".\n");
183 return 0;
184}
185
Arthur Heymansb04fef92019-02-05 17:35:05 +0100186void layout_cleanup(struct layout_include_args **args)
Stefan Tauner949ccc82013-09-15 14:01:06 +0000187{
188 int i;
Arthur Heymansb04fef92019-02-05 17:35:05 +0100189 struct layout_include_args *tmp;
190
191 while (*args) {
192 tmp = (*args)->next;
193 free(*args);
194 *args = tmp;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000195 }
Stefan Tauner949ccc82013-09-15 14:01:06 +0000196
Nico Huber3a9939b2016-04-27 15:56:14 +0200197 for (i = 0; i < layout.num_entries; i++) {
198 layout.entries[i].included = 0;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000199 }
Nico Huber3a9939b2016-04-27 15:56:14 +0200200 layout.num_entries = 0;
Stefan Tauner949ccc82013-09-15 14:01:06 +0000201}
202
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000203/* Validate and - if needed - normalize layout entries. */
204int normalize_romentries(const struct flashctx *flash)
205{
206 chipsize_t total_size = flash->chip->total_size * 1024;
207 int ret = 0;
208
209 int i;
Nico Huber3a9939b2016-04-27 15:56:14 +0200210 for (i = 0; i < layout.num_entries; i++) {
211 if (layout.entries[i].start >= total_size || layout.entries[i].end >= total_size) {
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000212 msg_gwarn("Warning: Address range of region \"%s\" exceeds the current chip's "
Nico Huber3a9939b2016-04-27 15:56:14 +0200213 "address space.\n", layout.entries[i].name);
214 if (layout.entries[i].included)
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000215 ret = 1;
216 }
Nico Huber3a9939b2016-04-27 15:56:14 +0200217 if (layout.entries[i].start > layout.entries[i].end) {
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000218 msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n",
Nico Huber3a9939b2016-04-27 15:56:14 +0200219 layout.entries[i].name);
Stefan Tauner8268fdb2013-09-23 14:21:06 +0000220 ret = 1;
221 }
222 }
223
224 return ret;
225}
Nico Huber3d7b1e32018-12-22 00:53:14 +0100226
227const struct romentry *layout_next_included_region(
228 const struct flashrom_layout *const l, const chipoff_t where)
229{
230 unsigned int i;
231 const struct romentry *lowest = NULL;
232
233 for (i = 0; i < l->num_entries; ++i) {
234 if (!l->entries[i].included)
235 continue;
236 if (l->entries[i].end < where)
237 continue;
238 if (!lowest || lowest->start > l->entries[i].start)
239 lowest = &l->entries[i];
240 }
241
242 return lowest;
243}