flashrom 

flashrom Svn Source Tree

Root/trunk/layout.c

1/*
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)
6 * Copyright (C) 2011-2013 Stefan Tauner
7 *
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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <limits.h>
26#include "flash.h"
27#include "programmer.h"
28
29#define MAX_ROMLAYOUT32
30
31typedef struct {
32chipoff_t start;
33chipoff_t end;
34unsigned int included;
35char name[256];
36} romentry_t;
37
38/* rom_entries store the entries specified in a layout file and associated run-time data */
39static romentry_t rom_entries[MAX_ROMLAYOUT];
40static int num_rom_entries = 0; /* the number of successfully parsed rom_entries */
41
42/* include_args holds the arguments specified at the command line with -i. They must be processed at some point
43 * so that desired regions are marked as "included" in the rom_entries list. */
44static char *include_args[MAX_ROMLAYOUT];
45static int num_include_args = 0; /* the number of valid include_args. */
46
47#ifndef __LIBPAYLOAD__
48int read_romlayout(const char *name)
49{
50FILE *romlayout;
51char tempstr[256];
52int i;
53
54romlayout = fopen(name, "r");
55
56if (!romlayout) {
57msg_gerr("ERROR: Could not open ROM layout (%s).\n",
58name);
59return -1;
60}
61
62while (!feof(romlayout)) {
63char *tstr1, *tstr2;
64
65if (num_rom_entries >= MAX_ROMLAYOUT) {
66msg_gerr("Maximum number of ROM images (%i) in layout "
67 "file reached.\n", MAX_ROMLAYOUT);
68(void)fclose(romlayout);
69return 1;
70}
71if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, rom_entries[num_rom_entries].name))
72continue;
73#if 0
74// fscanf does not like arbitrary comments like that :( later
75if (tempstr[0] == '#') {
76continue;
77}
78#endif
79tstr1 = strtok(tempstr, ":");
80tstr2 = strtok(NULL, ":");
81if (!tstr1 || !tstr2) {
82msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr);
83(void)fclose(romlayout);
84return 1;
85}
86rom_entries[num_rom_entries].start = strtol(tstr1, (char **)NULL, 16);
87rom_entries[num_rom_entries].end = strtol(tstr2, (char **)NULL, 16);
88rom_entries[num_rom_entries].included = 0;
89num_rom_entries++;
90}
91
92for (i = 0; i < num_rom_entries; i++) {
93msg_gdbg("romlayout %08x - %08x named %s\n",
94 rom_entries[i].start,
95 rom_entries[i].end, rom_entries[i].name);
96}
97
98(void)fclose(romlayout);
99
100return 0;
101}
102#endif
103
104/* returns the index of the entry (or a negative value if it is not found) */
105static int find_include_arg(const char *const name)
106{
107unsigned int i;
108for (i = 0; i < num_include_args; i++) {
109if (!strcmp(include_args[i], name))
110return i;
111}
112return -1;
113}
114
115/* register an include argument (-i) for later processing */
116int register_include_arg(char *name)
117{
118if (num_include_args >= MAX_ROMLAYOUT) {
119msg_gerr("Too many regions included (%i).\n", num_include_args);
120return 1;
121}
122
123if (name == NULL) {
124msg_gerr("<NULL> is a bad region name.\n");
125return 1;
126}
127
128if (find_include_arg(name) != -1) {
129msg_gerr("Duplicate region name: \"%s\".\n", name);
130return 1;
131}
132
133include_args[num_include_args] = name;
134num_include_args++;
135return 0;
136}
137
138/* returns the index of the entry (or a negative value if it is not found) */
139static int find_romentry(char *name)
140{
141int i;
142
143if (num_rom_entries == 0)
144return -1;
145
146msg_gspew("Looking for region \"%s\"... ", name);
147for (i = 0; i < num_rom_entries; i++) {
148if (!strcmp(rom_entries[i].name, name)) {
149rom_entries[i].included = 1;
150msg_gspew("found.\n");
151return i;
152}
153}
154msg_gspew("not found.\n");
155return -1;
156}
157
158/* process -i arguments
159 * returns 0 to indicate success, >0 to indicate failure
160 */
161int process_include_args(void)
162{
163int i;
164unsigned int found = 0;
165
166if (num_include_args == 0)
167return 0;
168
169/* User has specified an area, but no layout file is loaded. */
170if (num_rom_entries == 0) {
171msg_gerr("Region requested (with -i \"%s\"), "
172 "but no layout data is available.\n",
173 include_args[0]);
174return 1;
175}
176
177for (i = 0; i < num_include_args; i++) {
178if (find_romentry(include_args[i]) < 0) {
179msg_gerr("Invalid region specified: \"%s\".\n",
180 include_args[i]);
181return 1;
182}
183found++;
184}
185
186msg_ginfo("Using region%s: \"%s\"", num_include_args > 1 ? "s" : "",
187 include_args[0]);
188for (i = 1; i < num_include_args; i++)
189msg_ginfo(", \"%s\"", include_args[i]);
190msg_ginfo(".\n");
191return 0;
192}
193
194void layout_cleanup(void)
195{
196int i;
197for (i = 0; i < num_include_args; i++) {
198free(include_args[i]);
199include_args[i] = NULL;
200}
201num_include_args = 0;
202
203for (i = 0; i < num_rom_entries; i++) {
204rom_entries[i].included = 0;
205}
206num_rom_entries = 0;
207}
208
209romentry_t *get_next_included_romentry(unsigned int start)
210{
211int i;
212unsigned int best_start = UINT_MAX;
213romentry_t *best_entry = NULL;
214romentry_t *cur;
215
216/* First come, first serve for overlapping regions. */
217for (i = 0; i < num_rom_entries; i++) {
218cur = &rom_entries[i];
219if (!cur->included)
220continue;
221/* Already past the current entry? */
222if (start > cur->end)
223continue;
224/* Inside the current entry? */
225if (start >= cur->start)
226return cur;
227/* Entry begins after start. */
228if (best_start > cur->start) {
229best_start = cur->start;
230best_entry = cur;
231}
232}
233return best_entry;
234}
235
236/* Validate and - if needed - normalize layout entries. */
237int normalize_romentries(const struct flashctx *flash)
238{
239chipsize_t total_size = flash->chip->total_size * 1024;
240int ret = 0;
241
242int i;
243for (i = 0; i < num_rom_entries; i++) {
244if (rom_entries[i].start >= total_size || rom_entries[i].end >= total_size) {
245msg_gwarn("Warning: Address range of region \"%s\" exceeds the current chip's "
246 "address space.\n", rom_entries[i].name);
247if (rom_entries[i].included)
248ret = 1;
249}
250if (rom_entries[i].start > rom_entries[i].end) {
251msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n",
252 rom_entries[i].name);
253ret = 1;
254}
255}
256
257return ret;
258}
259
260static int copy_old_content(struct flashctx *flash, int oldcontents_valid, uint8_t *oldcontents, uint8_t *newcontents, unsigned int start, unsigned int size)
261{
262if (!oldcontents_valid) {
263/* oldcontents is a zero-filled buffer. By reading the current data into oldcontents here, we
264 * avoid a rewrite of identical regions even if an initial full chip read didn't happen. */
265msg_gdbg2("Read a chunk starting at 0x%06x (len=0x%06x).\n", start, size);
266int ret = flash->chip->read(flash, oldcontents + start, start, size);
267if (ret != 0) {
268msg_gerr("Failed to read chunk 0x%06x-0x%06x.\n", start, start + size - 1);
269return 1;
270}
271}
272memcpy(newcontents + start, oldcontents + start, size);
273return 0;
274}
275
276/**
277 * Modify @newcontents so that it contains the data that should be on the chip eventually. In the case the user
278 * wants to update only parts of it, copy the chunks to be preserved from @oldcontents to @newcontents. If
279 * @oldcontents is not valid, we need to fetch the current data from the chip first.
280 */
281int build_new_image(struct flashctx *flash, bool oldcontents_valid, uint8_t *oldcontents, uint8_t *newcontents)
282{
283unsigned int start = 0;
284romentry_t *entry;
285unsigned int size = flash->chip->total_size * 1024;
286
287/* If no regions were specified for inclusion, assume
288 * that the user wants to write the complete new image.
289 */
290if (num_include_args == 0)
291return 0;
292
293/* Non-included romentries are ignored.
294 * The union of all included romentries is used from the new image.
295 */
296while (start < size) {
297entry = get_next_included_romentry(start);
298/* No more romentries for remaining region? */
299if (!entry) {
300copy_old_content(flash, oldcontents_valid, oldcontents, newcontents, start,
301 size - start);
302break;
303}
304/* For non-included region, copy from old content. */
305if (entry->start > start)
306copy_old_content(flash, oldcontents_valid, oldcontents, newcontents, start,
307 entry->start - start);
308/* Skip to location after current romentry. */
309start = entry->end + 1;
310/* Catch overflow. */
311if (!start)
312break;
313}
314return 0;
315}

Archive Download this file

Revision: HEAD