OLD | NEW |
---|---|
1 /* | 1 /* |
2 * This file is part of the flashrom project. | 2 * This file is part of the flashrom project. |
3 * | 3 * |
4 * Copyright (C) 2005-2008 coresystems GmbH | 4 * Copyright (C) 2005-2008 coresystems GmbH |
5 * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH) | 5 * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH) |
6 * | 6 * |
7 * This program is free software; you can redistribute it and/or modify | 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 | 8 * it under the terms of the GNU General Public License as published by |
9 * the Free Software Foundation; version 2 of the License. | 9 * the Free Software Foundation; version 2 of the License. |
10 * | 10 * |
(...skipping 21 matching lines...) Expand all Loading... | |
32 #endif | 32 #endif |
33 static int romimages = 0; | 33 static int romimages = 0; |
34 | 34 |
35 #define MAX_ROMLAYOUT 64 | 35 #define MAX_ROMLAYOUT 64 |
36 | 36 |
37 typedef struct { | 37 typedef struct { |
38 unsigned int start; | 38 unsigned int start; |
39 unsigned int end; | 39 unsigned int end; |
40 unsigned int included; | 40 unsigned int included; |
41 char name[256]; | 41 char name[256]; |
42 char file[256]; /* file[0]=='\0' means not specified. */ | |
42 } romlayout_t; | 43 } romlayout_t; |
43 | 44 |
44 static romlayout_t rom_entries[MAX_ROMLAYOUT]; | 45 static romlayout_t rom_entries[MAX_ROMLAYOUT]; |
45 | 46 |
46 #if CONFIG_INTERNAL == 1 /* FIXME: Move the whole block to cbtable.c? */ | 47 #if CONFIG_INTERNAL == 1 /* FIXME: Move the whole block to cbtable.c? */ |
47 static char *def_name = "DEFAULT"; | 48 static char *def_name = "DEFAULT"; |
48 | 49 |
49 int show_id(uint8_t *bios, int size, int force) | 50 int show_id(uint8_t *bios, int size, int force) |
50 { | 51 { |
51 unsigned int *walk; | 52 unsigned int *walk; |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 | 190 |
190 fclose(romlayout); | 191 fclose(romlayout); |
191 | 192 |
192 return 0; | 193 return 0; |
193 } | 194 } |
194 #endif | 195 #endif |
195 | 196 |
196 int find_romentry(char *name) | 197 int find_romentry(char *name) |
197 { | 198 { |
198 int i; | 199 int i; |
200 char *file; | |
199 | 201 |
200 if (!romimages) | 202 if (!romimages) |
201 return -1; | 203 return -1; |
202 | 204 |
203 » msg_gdbg("Looking for \"%s\"... ", name); | 205 » /* -i <image>[:<file>] */ |
206 » if (strtok(name, ":")) { | |
207 » » file = strtok(NULL, ""); | |
208 » } | |
209 » msg_gdbg("Looking for \"%s\" (file=\"%s\")... ", | |
210 » name, file ? file : "<not specified>"); | |
Hung-Te
2011/03/03 11:03:27
file may be not initialized, if name does not cont
dhendrix
2011/03/04 00:43:09
agreed. i think you need to initialize file to NUL
Louis
2011/03/04 03:46:30
Done.
Louis
2011/03/04 03:46:30
Done.
| |
204 | 211 |
205 for (i = 0; i < romimages; i++) { | 212 for (i = 0; i < romimages; i++) { |
206 if (!strcmp(rom_entries[i].name, name)) { | 213 if (!strcmp(rom_entries[i].name, name)) { |
207 rom_entries[i].included = 1; | 214 rom_entries[i].included = 1; |
215 strcpy(rom_entries[i].file, file ? file : ""); | |
208 msg_gdbg("found.\n"); | 216 msg_gdbg("found.\n"); |
209 return i; | 217 return i; |
210 } | 218 } |
211 } | 219 } |
212 msg_gdbg("not found.\n"); // Not found. Error. | 220 msg_gdbg("not found.\n"); // Not found. Error. |
213 | 221 |
214 return -1; | 222 return -1; |
215 } | 223 } |
216 | 224 |
217 int find_next_included_romentry(unsigned int start) | 225 int find_next_included_romentry(unsigned int start) |
(...skipping 14 matching lines...) Expand all Loading... | |
232 return i; | 240 return i; |
233 /* Entry begins after start. */ | 241 /* Entry begins after start. */ |
234 if (best_start > rom_entries[i].start) { | 242 if (best_start > rom_entries[i].start) { |
235 best_start = rom_entries[i].start; | 243 best_start = rom_entries[i].start; |
236 best_entry = i; | 244 best_entry = i; |
237 } | 245 } |
238 } | 246 } |
239 return best_entry; | 247 return best_entry; |
240 } | 248 } |
241 | 249 |
250 static int read_content_from_file(int entry, uint8_t *newcontents) { | |
251 char *file; | |
252 FILE *fp; | |
253 int len; | |
254 | |
255 /* If file name is specified for this partition, read file | |
256 * content to overwrite. */ | |
257 file = rom_entries[entry].file; | |
258 len = rom_entries[entry].end - rom_entries[entry].start + 1; | |
259 if (file[0]) { | |
dhendrix
2011/03/04 00:43:09
Change to "if (file)". I think file[0] might cause
Hung-Te
2011/03/04 01:09:42
should better be if(file && *file)
Louis
2011/03/04 03:46:30
Hm... actually "file" is static array: char file[2
Louis
2011/03/04 03:46:30
As above.
On 2011/03/04 01:09:42, Hung-Te wrote:
| |
260 int numbytes; | |
261 if ((fp = fopen(file, "rb")) == NULL) { | |
262 perror(file); | |
263 return -1; | |
264 } | |
265 numbytes = fread(newcontents + rom_entries[entry].start, | |
266 1, len, fp); | |
267 fclose(fp); | |
268 if (numbytes == -1) { | |
269 perror(file); | |
270 return -1; | |
271 } | |
272 } | |
273 return 0; | |
274 } | |
275 | |
242 int handle_romentries(struct flashchip *flash, uint8_t *oldcontents, uint8_t *ne wcontents) | 276 int handle_romentries(struct flashchip *flash, uint8_t *oldcontents, uint8_t *ne wcontents) |
243 { | 277 { |
244 unsigned int start = 0; | 278 unsigned int start = 0; |
245 int entry; | 279 int entry; |
246 unsigned int size = flash->total_size * 1024; | 280 unsigned int size = flash->total_size * 1024; |
247 | 281 |
248 /* If no layout file was specified or the layout file was empty, assume | 282 /* If no layout file was specified or the layout file was empty, assume |
249 * that the user wants to flash the complete new image. | 283 * that the user wants to flash the complete new image. |
250 */ | 284 */ |
251 if (!romimages) | 285 if (!romimages) |
252 return 0; | 286 return 0; |
253 /* Non-included romentries are ignored. | 287 /* Non-included romentries are ignored. |
254 * The union of all included romentries is used from the new image. | 288 * The union of all included romentries is used from the new image. |
255 */ | 289 */ |
256 while (start < size) { | 290 while (start < size) { |
291 | |
257 entry = find_next_included_romentry(start); | 292 entry = find_next_included_romentry(start); |
258 /* No more romentries for remaining region? */ | 293 /* No more romentries for remaining region? */ |
259 if (entry < 0) { | 294 if (entry < 0) { |
260 memcpy(newcontents + start, oldcontents + start, | 295 memcpy(newcontents + start, oldcontents + start, |
261 size - start); | 296 size - start); |
262 break; | 297 break; |
263 } | 298 } |
299 | |
300 /* For non-included region, copy from old content. */ | |
264 if (rom_entries[entry].start > start) | 301 if (rom_entries[entry].start > start) |
265 memcpy(newcontents + start, oldcontents + start, | 302 memcpy(newcontents + start, oldcontents + start, |
266 rom_entries[entry].start - start); | 303 rom_entries[entry].start - start); |
304 /* For included region, copy from file if specified. */ | |
305 if (read_content_from_file(entry, newcontents) < 0) return -1; | |
306 | |
267 /* Skip to location after current romentry. */ | 307 /* Skip to location after current romentry. */ |
268 start = rom_entries[entry].end + 1; | 308 start = rom_entries[entry].end + 1; |
269 /* Catch overflow. */ | 309 /* Catch overflow. */ |
270 if (!start) | 310 if (!start) |
271 break; | 311 break; |
272 } | 312 } |
273 | 313 |
274 return 0; | 314 return 0; |
275 } | 315 } |
316 | |
317 static int write_content_to_file(int entry, uint8_t *buf) { | |
318 char *file; | |
319 FILE *fp; | |
320 int len = rom_entries[entry].end - rom_entries[entry].start + 1; | |
321 | |
322 file = rom_entries[entry].file; | |
323 if (file[0]) { /* save to file if name is specified. */ | |
dhendrix
2011/03/04 00:43:09
same comment as above
Louis
2011/03/04 03:46:30
As above reply. :-)
On 2011/03/04 00:43:09, dhendr
| |
324 int numbytes; | |
325 if ((fp = fopen(file, "wb")) == NULL) { | |
326 perror(file); | |
327 return -1; | |
328 } | |
329 numbytes = fwrite(buf + rom_entries[entry].start, 1, len, fp); | |
330 fclose(fp); | |
331 if (numbytes != len) { | |
332 perror(file); | |
333 return -1; | |
334 } | |
335 } | |
336 return 0; | |
337 } | |
338 | |
339 int handle_partial_read( | |
340 struct flashchip *flash, | |
341 uint8_t *buf, | |
342 int (*read) (struct flashchip *flash, uint8_t *buf, int start, int len)) { | |
343 | |
344 unsigned int start = 0; | |
345 int entry; | |
346 unsigned int size = flash->total_size * 1024; | |
347 int count = 0; | |
348 | |
349 /* If no layout file was specified or the layout file was empty, assume | |
350 * that the user wants to flash the complete new image. | |
351 */ | |
352 if (!romimages) | |
353 return 0; | |
354 /* Walk through the table and write content to file for those included | |
355 * partition. */ | |
356 while (start < size) { | |
357 int len; | |
358 | |
359 entry = find_next_included_romentry(start); | |
360 /* No more romentries for remaining region? */ | |
361 if (entry < 0) { | |
362 break; | |
363 } | |
364 ++count; | |
365 | |
366 /* read content from flash. */ | |
367 len = rom_entries[entry].end - rom_entries[entry].start + 1; | |
368 if (read(flash, buf + rom_entries[entry].start, | |
369 rom_entries[entry].start, len)) { | |
370 perror("flash partial read failed."); | |
371 return -1; | |
372 } | |
373 /* If file is specified, write this partition to file. */ | |
374 if (write_content_to_file(entry, buf) < 0) return -1; | |
375 | |
376 /* Skip to location after current romentry. */ | |
377 start = rom_entries[entry].end + 1; | |
378 /* Catch overflow. */ | |
379 if (!start) | |
380 break; | |
381 } | |
382 | |
383 return count; | |
384 } | |
OLD | NEW |