layout.c 8.44 KB
Newer Older
1 2 3 4 5
/*
 * This file is part of the flashrom project.
 *
 * Copyright (C) 2005-2008 coresystems GmbH
 * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
6
 * Copyright (C) 2011-2013 Stefan Tauner
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

22
#include <stdio.h>
23 24
#include <stdlib.h>
#include <string.h>
25
#include <limits.h>
26
#include "flash.h"
27
#include "programmer.h"
28

David Hendricks's avatar
David Hendricks committed
29
#define MAX_ROMLAYOUT	32
30 31

typedef struct {
32 33
	chipoff_t start;
	chipoff_t end;
34 35
	unsigned int included;
	char name[256];
36
} romentry_t;
37

38
/* rom_entries store the entries specified in a layout file and associated run-time data */
39
static romentry_t rom_entries[MAX_ROMLAYOUT];
40
static int num_rom_entries = 0; /* the number of successfully parsed rom_entries */
41 42 43 44 45

/* include_args holds the arguments specified at the command line with -i. They must be processed at some point
 * so that desired regions are marked as "included" in the rom_entries list. */
static char *include_args[MAX_ROMLAYOUT];
static int num_include_args = 0; /* the number of valid include_args. */
46

47
#ifndef __LIBPAYLOAD__
48
int read_romlayout(const char *name)
49 50 51 52 53
{
	FILE *romlayout;
	char tempstr[256];
	int i;

54 55 56
	romlayout = fopen(name, "r");

	if (!romlayout) {
57
		msg_gerr("ERROR: Could not open ROM layout (%s).\n",
58
			name);
59 60
		return -1;
	}
61 62

	while (!feof(romlayout)) {
63
		char *tstr1, *tstr2;
64

65
		if (num_rom_entries >= MAX_ROMLAYOUT) {
66
			msg_gerr("Maximum number of ROM images (%i) in layout "
67
				 "file reached.\n", MAX_ROMLAYOUT);
68
			fclose(romlayout);
69
			return 1;
70
		}
71
		if (2 != fscanf(romlayout, "%s %s\n", tempstr, rom_entries[num_rom_entries].name))
72
			continue;
73 74
#if 0
		// fscanf does not like arbitrary comments like that :( later
75
		if (tempstr[0] == '#') {
76 77 78
			continue;
		}
#endif
79 80
		tstr1 = strtok(tempstr, ":");
		tstr2 = strtok(NULL, ":");
81
		if (!tstr1 || !tstr2) {
82
			msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr);
83 84 85
			fclose(romlayout);
			return 1;
		}
86 87 88 89
		rom_entries[num_rom_entries].start = strtol(tstr1, (char **)NULL, 16);
		rom_entries[num_rom_entries].end = strtol(tstr2, (char **)NULL, 16);
		rom_entries[num_rom_entries].included = 0;
		num_rom_entries++;
90
	}
91

92
	for (i = 0; i < num_rom_entries; i++) {
93
		msg_gdbg("romlayout %08x - %08x named %s\n",
94 95
			     rom_entries[i].start,
			     rom_entries[i].end, rom_entries[i].name);
96 97 98
	}

	fclose(romlayout);
Uwe Hermann's avatar
Uwe Hermann committed
99

100
	return 0;
101
}
102
#endif
103

104
/* returns the index of the entry (or a negative value if it is not found) */
105
static int find_include_arg(const char *const name)
106 107 108 109 110 111 112 113 114
{
	unsigned int i;
	for (i = 0; i < num_include_args; i++) {
		if (!strcmp(include_args[i], name))
			return i;
	}
	return -1;
}

115 116 117 118 119 120 121 122 123 124 125 126 127
/* register an include argument (-i) for later processing */
int register_include_arg(char *name)
{
	if (num_include_args >= MAX_ROMLAYOUT) {
		msg_gerr("Too many regions included (%i).\n", num_include_args);
		return 1;
	}

	if (name == NULL) {
		msg_gerr("<NULL> is a bad region name.\n");
		return 1;
	}

128 129 130 131 132
	if (find_include_arg(name) != -1) {
		msg_gerr("Duplicate region name: \"%s\".\n", name);
		return 1;
	}

133 134 135 136 137 138 139
	include_args[num_include_args] = name;
	num_include_args++;
	return 0;
}

/* returns the index of the entry (or a negative value if it is not found) */
static int find_romentry(char *name)
140 141 142
{
	int i;

143
	if (num_rom_entries == 0)
144
		return -1;
145

146
	msg_gspew("Looking for region \"%s\"... ", name);
147
	for (i = 0; i < num_rom_entries; i++) {
148 149
		if (!strcmp(rom_entries[i].name, name)) {
			rom_entries[i].included = 1;
150
			msg_gspew("found.\n");
151 152 153
			return i;
		}
	}
154
	msg_gspew("not found.\n");
155 156 157
	return -1;
}

158 159 160 161 162 163 164 165 166 167 168
/* process -i arguments
 * returns 0 to indicate success, >0 to indicate failure
 */
int process_include_args(void)
{
	int i;
	unsigned int found = 0;

	if (num_include_args == 0)
		return 0;

169
	/* User has specified an area, but no layout file is loaded. */
170
	if (num_rom_entries == 0) {
171 172 173 174 175
		msg_gerr("Region requested (with -i \"%s\"), "
			 "but no layout data is available.\n",
			 include_args[0]);
		return 1;
	}
176

177
	for (i = 0; i < num_include_args; i++) {
178
		if (find_romentry(include_args[i]) < 0) {
179
			msg_gerr("Invalid region specified: \"%s\".\n",
180 181 182 183 184 185 186 187 188 189 190 191 192 193
				 include_args[i]);
			return 1;
		}
		found++;
	}

	msg_ginfo("Using region%s: \"%s\"", num_include_args > 1 ? "s" : "",
		  include_args[0]);
	for (i = 1; i < num_include_args; i++)
		msg_ginfo(", \"%s\"", include_args[i]);
	msg_ginfo(".\n");
	return 0;
}

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
void layout_cleanup(void)
{
	int i;
	for (i = 0; i < num_include_args; i++) {
		free(include_args[i]);
		include_args[i] = NULL;
	}
	num_include_args = 0;

	for (i = 0; i < num_rom_entries; i++) {
		rom_entries[i].included = 0;
	}
	num_rom_entries = 0;
}

209
romentry_t *get_next_included_romentry(unsigned int start)
210 211
{
	int i;
212
	unsigned int best_start = UINT_MAX;
213 214
	romentry_t *best_entry = NULL;
	romentry_t *cur;
215

216
	/* First come, first serve for overlapping regions. */
217
	for (i = 0; i < num_rom_entries; i++) {
218 219
		cur = &rom_entries[i];
		if (!cur->included)
220
			continue;
221
		/* Already past the current entry? */
222
		if (start > cur->end)
223 224
			continue;
		/* Inside the current entry? */
225 226
		if (start >= cur->start)
			return cur;
227
		/* Entry begins after start. */
228 229 230
		if (best_start > cur->start) {
			best_start = cur->start;
			best_entry = cur;
231
		}
232
	}
233 234 235
	return best_entry;
}

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
/* Validate and - if needed - normalize layout entries. */
int normalize_romentries(const struct flashctx *flash)
{
	chipsize_t total_size = flash->chip->total_size * 1024;
	int ret = 0;

	int i;
	for (i = 0; i < num_rom_entries; i++) {
		if (rom_entries[i].start >= total_size || rom_entries[i].end >= total_size) {
			msg_gwarn("Warning: Address range of region \"%s\" exceeds the current chip's "
				  "address space.\n", rom_entries[i].name);
			if (rom_entries[i].included)
				ret = 1;
		}
		if (rom_entries[i].start > rom_entries[i].end) {
			msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n",
				  rom_entries[i].name);
			ret = 1;
		}
	}

	return ret;
}

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
static int copy_old_content(struct flashctx *flash, int oldcontents_valid, uint8_t *oldcontents, uint8_t *newcontents, unsigned int start, unsigned int size)
{
	if (!oldcontents_valid) {
		/* oldcontents is a zero-filled buffer. By reading the current data into oldcontents here, we
		 * avoid a rewrite of identical regions even if an initial full chip read didn't happen. */
		msg_gdbg2("Read a chunk starting at 0x%06x (len=0x%06x).\n", start, size);
		int ret = flash->chip->read(flash, oldcontents + start, start, size);
		if (ret != 0) {
			msg_gerr("Failed to read chunk 0x%06x-0x%06x.\n", start, start + size - 1);
			return 1;
		}
	}
	memcpy(newcontents + start, oldcontents + start, size);
	return 0;
}

/**
 * Modify @newcontents so that it contains the data that should be on the chip eventually. In the case the user
 * wants to update only parts of it, copy the chunks to be preserved from @oldcontents to @newcontents. If
 * @oldcontents is not valid, we need to fetch the current data from the chip first.
 */
int build_new_image(struct flashctx *flash, bool oldcontents_valid, uint8_t *oldcontents, uint8_t *newcontents)
282 283
{
	unsigned int start = 0;
284
	romentry_t *entry;
285
	unsigned int size = flash->chip->total_size * 1024;
286

287 288
	/* If no regions were specified for inclusion, assume
	 * that the user wants to write the complete new image.
289
	 */
290
	if (num_include_args == 0)
291
		return 0;
292

293 294 295 296
	/* Non-included romentries are ignored.
	 * The union of all included romentries is used from the new image.
	 */
	while (start < size) {
297
		entry = get_next_included_romentry(start);
298
		/* No more romentries for remaining region? */
299
		if (!entry) {
300 301
			copy_old_content(flash, oldcontents_valid, oldcontents, newcontents, start,
					 size - start);
302 303
			break;
		}
304
		/* For non-included region, copy from old content. */
305
		if (entry->start > start)
306 307
			copy_old_content(flash, oldcontents_valid, oldcontents, newcontents, start,
					 entry->start - start);
308
		/* Skip to location after current romentry. */
309
		start = entry->end + 1;
310 311 312 313
		/* Catch overflow. */
		if (!start)
			break;
	}
314 315
	return 0;
}