blob: 86351b8a232d3c8a15198d167de15aaf3a4835d7 [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.
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
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000022#include <stdio.h>
Ollie Lho184a4042005-11-26 21:55:36 +000023#include <stdlib.h>
24#include <string.h>
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +000025#include <limits.h>
Uwe Hermann0846f892007-08-23 13:34:59 +000026#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000027#include "programmer.h"
Ollie Lho184a4042005-11-26 21:55:36 +000028
David Hendricks444cefc2010-10-29 20:17:41 +000029#define MAX_ROMLAYOUT 32
Ollie Lho184a4042005-11-26 21:55:36 +000030
31typedef struct {
32 unsigned int start;
33 unsigned int end;
34 unsigned int included;
35 char name[256];
Stefan Tauner97b6c112013-08-30 22:23:02 +000036} romentry_t;
Ollie Lho184a4042005-11-26 21:55:36 +000037
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000038/* rom_entries store the entries specified in a layout file and associated run-time data */
Stefan Tauner97b6c112013-08-30 22:23:02 +000039static romentry_t rom_entries[MAX_ROMLAYOUT];
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000040static int num_rom_entries = 0; /* the number of valid 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. */
Ollie Lho184a4042005-11-26 21:55:36 +000046
Patrick Georgia9095a92010-09-30 17:03:32 +000047#ifndef __LIBPAYLOAD__
Uwe Hermanna7e05482007-05-09 10:17:44 +000048int read_romlayout(char *name)
Ollie Lho184a4042005-11-26 21:55:36 +000049{
50 FILE *romlayout;
51 char tempstr[256];
52 int i;
53
Uwe Hermanna7e05482007-05-09 10:17:44 +000054 romlayout = fopen(name, "r");
55
56 if (!romlayout) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000057 msg_gerr("ERROR: Could not open ROM layout (%s).\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +000058 name);
Ollie Lho184a4042005-11-26 21:55:36 +000059 return -1;
60 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000061
62 while (!feof(romlayout)) {
Ollie Lho184a4042005-11-26 21:55:36 +000063 char *tstr1, *tstr2;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000064
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000065 if (num_rom_entries >= MAX_ROMLAYOUT) {
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000066 msg_gerr("Maximum number of ROM images (%i) in layout "
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +000067 "file reached.\n", MAX_ROMLAYOUT);
68 return 1;
Carl-Daniel Hailfingerda53ada2010-12-04 11:56:52 +000069 }
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000070 if (2 != fscanf(romlayout, "%s %s\n", tempstr, rom_entries[num_rom_entries].name))
Peter Stuge1fec0f32009-01-12 21:00:35 +000071 continue;
Ollie Lho184a4042005-11-26 21:55:36 +000072#if 0
73 // fscanf does not like arbitrary comments like that :( later
Uwe Hermanna7e05482007-05-09 10:17:44 +000074 if (tempstr[0] == '#') {
Ollie Lho184a4042005-11-26 21:55:36 +000075 continue;
76 }
77#endif
Uwe Hermanna7e05482007-05-09 10:17:44 +000078 tstr1 = strtok(tempstr, ":");
79 tstr2 = strtok(NULL, ":");
Uwe Hermann58783e32008-12-22 16:42:59 +000080 if (!tstr1 || !tstr2) {
Stefan Taunereb582572012-09-21 12:52:50 +000081 msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr);
Uwe Hermann58783e32008-12-22 16:42:59 +000082 fclose(romlayout);
83 return 1;
84 }
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000085 rom_entries[num_rom_entries].start = strtol(tstr1, (char **)NULL, 16);
86 rom_entries[num_rom_entries].end = strtol(tstr2, (char **)NULL, 16);
87 rom_entries[num_rom_entries].included = 0;
88 num_rom_entries++;
Ollie Lho184a4042005-11-26 21:55:36 +000089 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000090
Stefan Taunerc70bc8a2013-08-30 22:22:57 +000091 for (i = 0; i < num_rom_entries; i++) {
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000092 msg_gdbg("romlayout %08x - %08x named %s\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +000093 rom_entries[i].start,
94 rom_entries[i].end, rom_entries[i].name);
Ollie Lho184a4042005-11-26 21:55:36 +000095 }
96
97 fclose(romlayout);
Uwe Hermannffec5f32007-08-23 16:08:21 +000098
Uwe Hermanna7e05482007-05-09 10:17:44 +000099 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000100}
Patrick Georgia9095a92010-09-30 17:03:32 +0000101#endif
Ollie Lho184a4042005-11-26 21:55:36 +0000102
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000103/* returns the index of the entry (or a negative value if it is not found) */
104int find_include_arg(const char *const name)
105{
106 unsigned int i;
107 for (i = 0; i < num_include_args; i++) {
108 if (!strcmp(include_args[i], name))
109 return i;
110 }
111 return -1;
112}
113
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000114/* register an include argument (-i) for later processing */
115int register_include_arg(char *name)
116{
117 if (num_include_args >= MAX_ROMLAYOUT) {
118 msg_gerr("Too many regions included (%i).\n", num_include_args);
119 return 1;
120 }
121
122 if (name == NULL) {
123 msg_gerr("<NULL> is a bad region name.\n");
124 return 1;
125 }
126
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000127 if (find_include_arg(name) != -1) {
128 msg_gerr("Duplicate region name: \"%s\".\n", name);
129 return 1;
130 }
131
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000132 include_args[num_include_args] = name;
133 num_include_args++;
134 return 0;
135}
136
137/* returns the index of the entry (or a negative value if it is not found) */
138static int find_romentry(char *name)
Ollie Lho184a4042005-11-26 21:55:36 +0000139{
140 int i;
141
Stefan Taunerc70bc8a2013-08-30 22:22:57 +0000142 if (num_rom_entries == 0)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000143 return -1;
Ollie Lho184a4042005-11-26 21:55:36 +0000144
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000145 msg_gspew("Looking for region \"%s\"... ", name);
Stefan Taunerc70bc8a2013-08-30 22:22:57 +0000146 for (i = 0; i < num_rom_entries; i++) {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000147 if (!strcmp(rom_entries[i].name, name)) {
148 rom_entries[i].included = 1;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000149 msg_gspew("found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000150 return i;
151 }
152 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000153 msg_gspew("not found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000154 return -1;
155}
156
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000157/* process -i arguments
158 * returns 0 to indicate success, >0 to indicate failure
159 */
160int process_include_args(void)
161{
162 int i;
163 unsigned int found = 0;
164
165 if (num_include_args == 0)
166 return 0;
167
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000168 /* User has specified an area, but no layout file is loaded. */
Stefan Taunerc70bc8a2013-08-30 22:22:57 +0000169 if (num_rom_entries == 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000170 msg_gerr("Region requested (with -i \"%s\"), "
171 "but no layout data is available.\n",
172 include_args[0]);
173 return 1;
174 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000175
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000176 for (i = 0; i < num_include_args; i++) {
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000177 if (find_romentry(include_args[i]) < 0) {
Stefan Tauner23bb6d52012-04-15 14:09:16 +0000178 msg_gerr("Invalid region specified: \"%s\".\n",
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000179 include_args[i]);
180 return 1;
181 }
182 found++;
183 }
184
185 msg_ginfo("Using region%s: \"%s\"", num_include_args > 1 ? "s" : "",
186 include_args[0]);
187 for (i = 1; i < num_include_args; i++)
188 msg_ginfo(", \"%s\"", include_args[i]);
189 msg_ginfo(".\n");
190 return 0;
191}
192
Stefan Tauner949ccc82013-09-15 14:01:06 +0000193void layout_cleanup(void)
194{
195 int i;
196 for (i = 0; i < num_include_args; i++) {
197 free(include_args[i]);
198 include_args[i] = NULL;
199 }
200 num_include_args = 0;
201
202 for (i = 0; i < num_rom_entries; i++) {
203 rom_entries[i].included = 0;
204 }
205 num_rom_entries = 0;
206}
207
Stefan Tauner97b6c112013-08-30 22:23:02 +0000208romentry_t *get_next_included_romentry(unsigned int start)
Ollie Lho184a4042005-11-26 21:55:36 +0000209{
210 int i;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000211 unsigned int best_start = UINT_MAX;
Stefan Tauner97b6c112013-08-30 22:23:02 +0000212 romentry_t *best_entry = NULL;
213 romentry_t *cur;
Ollie Lho184a4042005-11-26 21:55:36 +0000214
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000215 /* First come, first serve for overlapping regions. */
Stefan Taunerc70bc8a2013-08-30 22:22:57 +0000216 for (i = 0; i < num_rom_entries; i++) {
Stefan Tauner104b0d92011-12-25 09:07:59 +0000217 cur = &rom_entries[i];
218 if (!cur->included)
Ollie Lho184a4042005-11-26 21:55:36 +0000219 continue;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000220 /* Already past the current entry? */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000221 if (start > cur->end)
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000222 continue;
223 /* Inside the current entry? */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000224 if (start >= cur->start)
225 return cur;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000226 /* Entry begins after start. */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000227 if (best_start > cur->start) {
228 best_start = cur->start;
229 best_entry = cur;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000230 }
Ollie Lho184a4042005-11-26 21:55:36 +0000231 }
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000232 return best_entry;
233}
Ollie Lho184a4042005-11-26 21:55:36 +0000234
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000235int handle_romentries(const struct flashctx *flash, uint8_t *oldcontents, uint8_t *newcontents)
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000236{
237 unsigned int start = 0;
Stefan Tauner97b6c112013-08-30 22:23:02 +0000238 romentry_t *entry;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000239 unsigned int size = flash->chip->total_size * 1024;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000240
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000241 /* If no regions were specified for inclusion, assume
242 * that the user wants to write the complete new image.
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000243 */
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000244 if (num_include_args == 0)
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000245 return 0;
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000246
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000247 /* Non-included romentries are ignored.
248 * The union of all included romentries is used from the new image.
249 */
250 while (start < size) {
Stefan Tauner104b0d92011-12-25 09:07:59 +0000251 entry = get_next_included_romentry(start);
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000252 /* No more romentries for remaining region? */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000253 if (!entry) {
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000254 memcpy(newcontents + start, oldcontents + start,
255 size - start);
256 break;
257 }
Louis Yung-Chieh Lo9bcf2682011-12-25 09:12:16 +0000258 /* For non-included region, copy from old content. */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000259 if (entry->start > start)
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000260 memcpy(newcontents + start, oldcontents + start,
Stefan Tauner104b0d92011-12-25 09:07:59 +0000261 entry->start - start);
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000262 /* Skip to location after current romentry. */
Stefan Tauner104b0d92011-12-25 09:07:59 +0000263 start = entry->end + 1;
Carl-Daniel Hailfingercb6ad162010-11-02 03:12:51 +0000264 /* Catch overflow. */
265 if (!start)
266 break;
267 }
Ollie Lho184a4042005-11-26 21:55:36 +0000268 return 0;
269}