layout.c 6.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * This file is part of the flashrom project.
 *
 * Copyright (C) 2005-2008 coresystems GmbH
 * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
 *
 * 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
 */

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

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

typedef struct {
	unsigned int start;
	unsigned int end;
	unsigned int included;
	char name[256];
35
} romentry_t;
36

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

/* 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. */
45

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

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

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

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

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

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

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

98
	return 0;
99
}
100
#endif
101

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

113 114 115 116 117 118 119 120 121 122 123 124 125
/* 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;
	}

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

131 132 133 134 135 136 137
	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)
138 139 140
{
	int i;

141
	if (num_rom_entries == 0)
142
		return -1;
143

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

156 157 158 159 160 161 162 163 164 165 166
/* 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;

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

175
	for (i = 0; i < num_include_args; i++) {
176
		if (find_romentry(include_args[i]) < 0) {
177
			msg_gerr("Invalid region specified: \"%s\".\n",
178 179 180 181 182 183 184 185 186 187 188 189 190 191
				 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;
}

192
romentry_t *get_next_included_romentry(unsigned int start)
193 194
{
	int i;
195
	unsigned int best_start = UINT_MAX;
196 197
	romentry_t *best_entry = NULL;
	romentry_t *cur;
198

199
	/* First come, first serve for overlapping regions. */
200
	for (i = 0; i < num_rom_entries; i++) {
201 202
		cur = &rom_entries[i];
		if (!cur->included)
203
			continue;
204
		/* Already past the current entry? */
205
		if (start > cur->end)
206 207
			continue;
		/* Inside the current entry? */
208 209
		if (start >= cur->start)
			return cur;
210
		/* Entry begins after start. */
211 212 213
		if (best_start > cur->start) {
			best_start = cur->start;
			best_entry = cur;
214
		}
215
	}
216 217 218
	return best_entry;
}

219
int handle_romentries(const struct flashctx *flash, uint8_t *oldcontents, uint8_t *newcontents)
220 221
{
	unsigned int start = 0;
222
	romentry_t *entry;
223
	unsigned int size = flash->chip->total_size * 1024;
224

225 226
	/* If no regions were specified for inclusion, assume
	 * that the user wants to write the complete new image.
227
	 */
228
	if (num_include_args == 0)
229
		return 0;
230

231 232 233 234
	/* Non-included romentries are ignored.
	 * The union of all included romentries is used from the new image.
	 */
	while (start < size) {
235
		entry = get_next_included_romentry(start);
236
		/* No more romentries for remaining region? */
237
		if (!entry) {
238 239 240 241
			memcpy(newcontents + start, oldcontents + start,
			       size - start);
			break;
		}
242
		/* For non-included region, copy from old content. */
243
		if (entry->start > start)
244
			memcpy(newcontents + start, oldcontents + start,
245
			       entry->start - start);
246
		/* Skip to location after current romentry. */
247
		start = entry->end + 1;
248 249 250 251
		/* Catch overflow. */
		if (!start)
			break;
	}
252 253
	return 0;
}