blob: 8bb1666a0421676e4ae2795e0e5e5ef2f13d9148 [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)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000021#include <stdio.h>
Ollie Lho184a4042005-11-26 21:55:36 +000022#include <stdlib.h>
23#include <string.h>
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +000024#include <limits.h>
Uwe Hermann0846f892007-08-23 13:34:59 +000025#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000026#include "programmer.h"
Ollie Lho184a4042005-11-26 21:55:36 +000027
David Hendricks444cefc2010-10-29 20:17:41 +000028#define MAX_ROMLAYOUT 32
Ollie Lho184a4042005-11-26 21:55:36 +000029
30typedef struct {
31 unsigned int start;
32 unsigned int end;
33 unsigned int included;
34 char name[256];
Stefan Tauner97b6c112013-08-30 22:23:02 +000035} romentry_t;
Ollie Lho184a4042005-11-26 21:55:36 +000036
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000037/* rom_entries store the entries specified in a layout file and associated run-time data */
Stefan Tauner97b6c112013-08-30 22:23:02 +000038static romentry_t rom_entries[MAX_ROMLAYOUT];
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000039static int num_rom_entries = 0; /* the number of valid rom_entries */
40
41/* include_args holds the arguments specified at the command line with -i. They must be processed at some point
42 * so that desired regions are marked as "included" in the rom_entries list. */
43static char *include_args[MAX_ROMLAYOUT];
44static int num_include_args = 0; /* the number of valid include_args. */
Ollie Lho184a4042005-11-26 21:55:36 +000045
Patrick Georgia9095a92010-09-30 17:03:32 +000046#ifndef __LIBPAYLOAD__
Uwe Hermanna7e05482007-05-09 10:17:44 +000047int read_romlayout(char *name)
Ollie Lho184a4042005-11-26 21:55:36 +000048{
49 FILE *romlayout;
50 char tempstr[256];
51 int i;
52
Uwe Hermanna7e05482007-05-09 10:17:44 +000053 romlayout = fopen(name, "r");
54
55 if (!romlayout) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000056 msg_gerr("ERROR: Could not open ROM layout (%s).\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +000057 name);
Ollie Lho184a4042005-11-26 21:55:36 +000058 return -1;
59 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000060
61 while (!feof(romlayout)) {
Ollie Lho184a4042005-11-26 21:55:36 +000062 char *tstr1, *tstr2;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000063
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000064 if (num_rom_entries >= MAX_ROMLAYOUT) {
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000065 msg_gerr("Maximum number of ROM images (%i) in layout "
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +000066 "file reached.\n", MAX_ROMLAYOUT);
67 return 1;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000068 }
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000069 if (2 != fscanf(romlayout, "%s %s\n", tempstr, rom_entries[num_rom_entries].name))
Peter Stuge1fec0f32009-01-12 21:00:35 +000070 continue;
Ollie Lho184a4042005-11-26 21:55:36 +000071#if 0
72 // fscanf does not like arbitrary comments like that :( later
Uwe Hermanna7e05482007-05-09 10:17:44 +000073 if (tempstr[0] == '#') {
Ollie Lho184a4042005-11-26 21:55:36 +000074 continue;
75 }
76#endif
Uwe Hermanna7e05482007-05-09 10:17:44 +000077 tstr1 = strtok(tempstr, ":");
78 tstr2 = strtok(NULL, ":");
Uwe Hermann58783e32008-12-22 16:42:59 +000079 if (!tstr1 || !tstr2) {
Stefan Taunereb582572012-09-21 12:52:50 +000080 msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr);
Uwe Hermann58783e32008-12-22 16:42:59 +000081 fclose(romlayout);
82 return 1;
83 }
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000084 rom_entries[num_rom_entries].start = strtol(tstr1, (char **)NULL, 16);
85 rom_entries[num_rom_entries].end = strtol(tstr2, (char **)NULL, 16);
86 rom_entries[num_rom_entries].included = 0;
87 num_rom_entries++;
Ollie Lho184a4042005-11-26 21:55:36 +000088 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000089
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000090 for (i = 0; i < num_rom_entries; i++) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000091 msg_gdbg("romlayout %08x - %08x named %s\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +000092 rom_entries[i].start,
93 rom_entries[i].end, rom_entries[i].name);
Ollie Lho184a4042005-11-26 21:55:36 +000094 }
95
96 fclose(romlayout);
Uwe Hermannffec5f32007-08-23 16:08:21 +000097
Uwe Hermanna7e05482007-05-09 10:17:44 +000098 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +000099}
Patrick Georgia9095a92010-09-30 17:03:32 +0000100#endif
Ollie Lho184a4042005-11-26 21:55:36 +0000101
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000102/* returns the index of the entry (or a negative value if it is not found) */
103int find_include_arg(const char *const name)
104{
105 unsigned int i;
106 for (i = 0; i < num_include_args; i++) {
107 if (!strcmp(include_args[i], name))
108 return i;
109 }
110 return -1;
111}
112
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000113/* register an include argument (-i) for later processing */
114int register_include_arg(char *name)
115{
116 if (num_include_args >= MAX_ROMLAYOUT) {
117 msg_gerr("Too many regions included (%i).\n", num_include_args);
118 return 1;
119 }
120
121 if (name == NULL) {
122 msg_gerr("<NULL> is a bad region name.\n");
123 return 1;
124 }
125
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000126 if (find_include_arg(name) != -1) {
127 msg_gerr("Duplicate region name: \"%s\".\n", name);
128 return 1;
129 }
130
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000131 include_args[num_include_args] = name;
132 num_include_args++;
133 return 0;
134}
135
136/* returns the index of the entry (or a negative value if it is not found) */
137static int find_romentry(char *name)
Ollie Lho184a4042005-11-26 21:55:36 +0000138{
139 int i;
140
Stefan Taunerc70bc8a2013-08-30 22:22:57 +0000141 if (num_rom_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);
Stefan Taunerc70bc8a2013-08-30 22:22:57 +0000145 for (i = 0; i < num_rom_entries; i++) {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000146 if (!strcmp(rom_entries[i].name, name)) {
147 rom_entries[i].included = 1;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000148 msg_gspew("found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000149 return i;
150 }
151 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000152 msg_gspew("not found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000153 return -1;
154}
155
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000156/* process -i arguments
157 * returns 0 to indicate success, >0 to indicate failure
158 */
159int process_include_args(void)
160{
161 int i;
162 unsigned int found = 0;
163
164 if (num_include_args == 0)
165 return 0;
166
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000167 /* User has specified an area, but no layout file is loaded. */
Stefan Taunerc70bc8a2013-08-30 22:22:57 +0000168 if (num_rom_entries == 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000169 msg_gerr("Region requested (with -i \"%s\"), "
170 "but no layout data is available.\n",
171 include_args[0]);
172 return 1;
173 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000174
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000175 for (i = 0; i < num_include_args; i++) {
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000176 if (find_romentry(include_args[i]) < 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000177 msg_gerr("Invalid region specified: \"%s\".\n",
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000178 include_args[i]);
179 return 1;
180 }
181 found++;
182 }
183
184 msg_ginfo("Using region%s: \"%s\"", num_include_args > 1 ? "s" : "",
185 include_args[0]);
186 for (i = 1; i < num_include_args; i++)
187 msg_ginfo(", \"%s\"", include_args[i]);
188 msg_ginfo(".\n");
189 return 0;
190}
191
Stefan Tauner97b6c112013-08-30 22:23:02 +0000192romentry_t *get_next_included_romentry(unsigned int start)
Ollie Lho184a4042005-11-26 21:55:36 +0000193{
194 int i;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000195 unsigned int best_start = UINT_MAX;
Stefan Tauner97b6c112013-08-30 22:23:02 +0000196 romentry_t *best_entry = NULL;
197 romentry_t *cur;
Ollie Lho184a4042005-11-26 21:55:36 +0000198
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000199 /* First come, first serve for overlapping regions. */
Stefan Taunerc70bc8a2013-08-30 22:22:57 +0000200 for (i = 0; i < num_rom_entries; i++) {
Stefan Tauner104b0d92011-12-25 09:07:59 +0000201 cur = &rom_entries[i];
202 if (!cur->included)
Ollie Lho184a4042005-11-26 21:55:36 +0000203 continue;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000204 /* Already past the current entry? */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000205 if (start > cur->end)
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000206 continue;
207 /* Inside the current entry? */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000208 if (start >= cur->start)
209 return cur;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000210 /* Entry begins after start. */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000211 if (best_start > cur->start) {
212 best_start = cur->start;
213 best_entry = cur;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000214 }
Ollie Lho184a4042005-11-26 21:55:36 +0000215 }
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000216 return best_entry;
217}
Ollie Lho184a4042005-11-26 21:55:36 +0000218
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000219int handle_romentries(const struct flashctx *flash, uint8_t *oldcontents, uint8_t *newcontents)
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000220{
221 unsigned int start = 0;
Stefan Tauner97b6c112013-08-30 22:23:02 +0000222 romentry_t *entry;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000223 unsigned int size = flash->chip->total_size * 1024;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000224
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000225 /* If no regions were specified for inclusion, assume
226 * that the user wants to write the complete new image.
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000227 */
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000228 if (num_include_args == 0)
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000229 return 0;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000230
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000231 /* Non-included romentries are ignored.
232 * The union of all included romentries is used from the new image.
233 */
234 while (start < size) {
Stefan Tauner104b0d92011-12-25 09:07:59 +0000235 entry = get_next_included_romentry(start);
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000236 /* No more romentries for remaining region? */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000237 if (!entry) {
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000238 memcpy(newcontents + start, oldcontents + start,
239 size - start);
240 break;
241 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000242 /* For non-included region, copy from old content. */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000243 if (entry->start > start)
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000244 memcpy(newcontents + start, oldcontents + start,
Stefan Tauner104b0d92011-12-25 09:07:59 +0000245 entry->start - start);
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000246 /* Skip to location after current romentry. */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000247 start = entry->end + 1;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000248 /* Catch overflow. */
249 if (!start)
250 break;
251 }
Ollie Lho184a4042005-11-26 21:55:36 +0000252 return 0;
253}