blob: 4245a8f92b028598b4ee40057287958747224b79 [file] [log] [blame]
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001/*
Ollie Lho184a4042005-11-26 21:55:36 +00002 * flash_rom.c: Flash programming utility
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00003 *
4 * Copyright 2000 Silicon Integrated System Corporation
Yinghai Luad8ffd22004-10-20 05:07:16 +00005 * Copyright 2004 Tyan Corp
6 * yhlu yhlu@tyan.com add exclude start and end option
Stefan Reinauerfcb63682006-03-16 16:57:41 +00007 * Copyright 2005-2006 coresystems GmbH
8 * Stefan Reinauer <stepan@coresystems.de> added rom layout
9 * support, and checking for suitable rom image, various fixes
10 * support for flashing the Technologic Systems 5300.
Ollie Lho184a4042005-11-26 21:55:36 +000011 *
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000026 */
27
28#include <errno.h>
29#include <fcntl.h>
30#include <sys/mman.h>
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000031#include <unistd.h>
32#include <stdio.h>
Ronald G. Minnichceec4202003-07-25 04:37:41 +000033#include <string.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000034#include <stdlib.h>
Ollie Lho184a4042005-11-26 21:55:36 +000035#include <getopt.h>
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000036
37#include "flash.h"
Ollie Lho184a4042005-11-26 21:55:36 +000038#include "lbtable.h"
39#include "layout.h"
40#include "debug.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000041
Ronald G. Minnichceec4202003-07-25 04:37:41 +000042char *chip_to_probe = NULL;
Ronald G. Minnich7bd1dee2003-05-08 19:33:19 +000043
Stefan Reinauere3705282005-12-18 16:41:10 +000044int exclude_start_page, exclude_end_page;
45int force=0, verbose=0;
46
Ollie Lho761bf1b2004-03-20 16:46:10 +000047struct flashchip *probe_flash(struct flashchip *flash)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000048{
Ollie Lhocbbf1252004-03-17 22:22:08 +000049 int fd_mem;
Ollie Lho184a4042005-11-26 21:55:36 +000050 volatile uint8_t *bios;
Ollie Lhocbbf1252004-03-17 22:22:08 +000051 unsigned long size;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000052
Ollie Lhocbbf1252004-03-17 22:22:08 +000053 if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
54 perror("Can not open /dev/mem");
55 exit(1);
56 }
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000057
Ollie Lhocbbf1252004-03-17 22:22:08 +000058 while (flash->name != NULL) {
Ollie Lho8b8897a2004-03-27 00:18:15 +000059 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) {
Ollie Lhocbbf1252004-03-17 22:22:08 +000060 flash++;
61 continue;
62 }
Ollie Lho184a4042005-11-26 21:55:36 +000063 printf_debug("Trying %s, %d KB\n", flash->name, flash->total_size);
Ollie Lhocbbf1252004-03-17 22:22:08 +000064 size = flash->total_size * 1024;
65 /* BUG? what happens if getpagesize() > size!?
66 -> ``Error MMAP /dev/mem: Invalid argument'' NIKI */
67 if (getpagesize() > size) {
68 size = getpagesize();
Ollie Lho761bf1b2004-03-20 16:46:10 +000069 printf("%s: warning: size: %d -> %ld\n",
70 __FUNCTION__, flash->total_size * 1024,
71 (unsigned long) size);
Ollie Lhocbbf1252004-03-17 22:22:08 +000072 }
Ollie Lho761bf1b2004-03-20 16:46:10 +000073 bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
74 fd_mem, (off_t) (0xffffffff - size + 1));
Ollie Lhocbbf1252004-03-17 22:22:08 +000075 if (bios == MAP_FAILED) {
76 perror("Error MMAP /dev/mem");
77 exit(1);
78 }
79 flash->virt_addr = bios;
80 flash->fd_mem = fd_mem;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000081
Ollie Lhocbbf1252004-03-17 22:22:08 +000082 if (flash->probe(flash) == 1) {
Ollie Lho761bf1b2004-03-20 16:46:10 +000083 printf("%s found at physical address: 0x%lx\n",
84 flash->name, (0xffffffff - size + 1));
Ollie Lhocbbf1252004-03-17 22:22:08 +000085 return flash;
86 }
Ollie Lho761bf1b2004-03-20 16:46:10 +000087 munmap((void *) bios, size);
Stefan Reinauerfcb63682006-03-16 16:57:41 +000088#ifdef TS5300
89 /* TS-5300 */
90 bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
91 fd_mem, (off_t) (0x9400000));
92 if (bios == MAP_FAILED) {
93 perror("Error MMAP /dev/mem");
94 exit(1);
95 }
96 flash->virt_addr = bios;
97 flash->fd_mem = fd_mem;
98
99 if (flash->probe(flash) == 1) {
100 printf("TS-5300 %s found at physical address: 0x%lx\n",
101 flash->name, 0x9400000UL);
102 return flash;
103 }
104 munmap((void *) bios, size);
105 /* TS-5300 */
106#endif
107
Ollie Lhocbbf1252004-03-17 22:22:08 +0000108 flash++;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000109 }
Ollie Lhocbbf1252004-03-17 22:22:08 +0000110 return NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000111}
112
Stefan Reinauere3705282005-12-18 16:41:10 +0000113int verify_flash(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000114{
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000115 int idx;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000116 int total_size = flash->total_size * 1024;
Ollie Lho184a4042005-11-26 21:55:36 +0000117 volatile uint8_t *bios = flash->virt_addr;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000118
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000119 printf("Verifying flash ");
120
121 if(verbose) printf("address: 0x00000000\b\b\b\b\b\b\b\b\b\b");
122
123 for (idx = 0; idx < total_size; idx++) {
124 if (verbose && ( (idx & 0xfff) == 0xfff ))
125 printf("0x%08x", idx);
126
127 if (*(bios + idx) != *(buf + idx)) {
128 if (verbose) {
129 printf("0x%08x ", idx);
130 }
131 printf("- FAILED\n");
132 return 1;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000133 }
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000134
135 if (verbose && ( (idx & 0xfff) == 0xfff ))
Ollie Lhocbbf1252004-03-17 22:22:08 +0000136 printf("\b\b\b\b\b\b\b\b\b\b");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000137 }
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000138 if (verbose)
139 printf("\b\b\b\b\b\b\b\b\b\b ");
140
141 printf("- VERIFIED \n");
142 return 0;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000143}
144
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000145
Ronald G. Minniche5559572003-03-28 03:29:43 +0000146void usage(const char *name)
147{
Ollie Lho184a4042005-11-26 21:55:36 +0000148 printf("usage: %s [-rwvE] [-V] [-c chipname] [-s exclude_start] [-e exclude_end] [file]\n", name);
149 printf(" -r | --read: read flash and save into file\n"
150 " -w | --write: write file into flash (default when file is specified)\n"
151 " -v | --verify: verify flash against file\n"
152 " -E | --erase: Erase flash device\n"
153 " -V | --verbose: more verbose output\n\n"
154 " -c | --chip <chipname>: probe only for specified flash chip\n"
155 " -s | --estart <addr>: exclude start position\n"
156 " -e | --eend <addr>: exclude end postion\n"
157 " -m | --mainboard <vendor:part>: override mainboard settings\n"
158 " -f | --force: force write without checking image\n"
159 " -l | --layout <file.layout>: read rom layout from file\n"
160 " -i | --image <name>: only flash image name from flash layout\n"
161 "\n"
Ollie Lhocbbf1252004-03-17 22:22:08 +0000162 " If no file is specified, then all that happens\n"
Ollie Lho184a4042005-11-26 21:55:36 +0000163 " is that flash info is dumped\n\n");
Ollie Lhocbbf1252004-03-17 22:22:08 +0000164 exit(1);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000165}
166
Ollie Lho761bf1b2004-03-20 16:46:10 +0000167int main(int argc, char *argv[])
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000168{
Ollie Lho184a4042005-11-26 21:55:36 +0000169 uint8_t *buf;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000170 unsigned long size;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000171 FILE *image;
172 struct flashchip *flash;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000173 int opt;
Ollie Lho184a4042005-11-26 21:55:36 +0000174 int option_index = 0;
175 int read_it = 0,
176 write_it = 0,
177 erase_it = 0,
178 verify_it = 0;
Stefan Reinauer143da0b2006-01-04 16:42:57 +0000179 int ret = 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000180
181 static struct option long_options[]= {
182 { "read", 0, 0, 'r' },
183 { "write", 0, 0, 'w' },
184 { "erase", 0, 0, 'E' },
185 { "verify", 0, 0, 'v' },
186 { "chip", 1, 0, 'c' },
187 { "estart", 1, 0, 's' },
188 { "eend", 1, 0, 'e' },
189 { "mainboard", 1, 0, 'm' },
190 { "verbose", 0, 0, 'V' },
191 { "force", 0, 0, 'f' },
192 { "layout", 1, 0, 'l' },
193 { "image", 1, 0, 'i' },
194 { "help", 0, 0, 'h' },
195 { 0, 0, 0, 0 }
196 };
197
Ollie Lhocbbf1252004-03-17 22:22:08 +0000198 char *filename = NULL;
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000199
Yinghai Luad8ffd22004-10-20 05:07:16 +0000200
201 unsigned int exclude_start_position=0, exclude_end_position=0; // [x,y)
Ollie Lho184a4042005-11-26 21:55:36 +0000202 char *tempstr=NULL, *tempstr2=NULL;
Yinghai Luad8ffd22004-10-20 05:07:16 +0000203
204 if (argc > 1) {
205 /* Yes, print them. */
206 int i;
Ollie Lho184a4042005-11-26 21:55:36 +0000207 printf_debug ("The arguments are:\n");
Yinghai Luad8ffd22004-10-20 05:07:16 +0000208 for (i = 1; i < argc; ++i)
Ollie Lho184a4042005-11-26 21:55:36 +0000209 printf_debug ("%s\n", argv[i]);
Yinghai Luad8ffd22004-10-20 05:07:16 +0000210 }
211
Ollie Lhocbbf1252004-03-17 22:22:08 +0000212 setbuf(stdout, NULL);
Ollie Lho184a4042005-11-26 21:55:36 +0000213 while ((opt = getopt_long(argc, argv, "rwvVEfc:s:e:m:l:i:h", long_options,
214 &option_index)) != EOF) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000215 switch (opt) {
216 case 'r':
217 read_it = 1;
218 break;
219 case 'w':
220 write_it = 1;
221 break;
222 case 'v':
223 verify_it = 1;
224 break;
225 case 'c':
226 chip_to_probe = strdup(optarg);
227 break;
Ollie Lho789ad3d2004-03-18 20:27:33 +0000228 case 'V':
229 verbose = 1;
230 break;
Ollie Lhoefa28582004-12-08 20:10:01 +0000231 case 'E':
232 erase_it = 1;
233 break;
Yinghai Luad8ffd22004-10-20 05:07:16 +0000234 case 's':
235 tempstr = strdup(optarg);
236 sscanf(tempstr,"%x",&exclude_start_position);
237 break;
238 case 'e':
Ollie Lho98bea8a2004-12-07 03:15:51 +0000239 tempstr = strdup(optarg);
240 sscanf(tempstr,"%x",&exclude_end_position);
241 break;
Ollie Lho184a4042005-11-26 21:55:36 +0000242 case 'm':
243 tempstr = strdup(optarg);
244 strtok(tempstr, ":");
245 tempstr2=strtok(NULL, ":");
246 if (tempstr2) {
247 lb_vendor=tempstr;
248 lb_part=tempstr2;
249 } else {
250 printf("warning: ignored wrong format of"
251 " mainboard: %s\n", tempstr);
252 }
253 break;
254 case 'f':
255 force=1;
256 break;
257 case 'l':
258 tempstr=strdup(optarg);
259 read_romlayout(tempstr);
260 break;
261 case 'i':
262 tempstr=strdup(optarg);
263 find_romentry(tempstr);
264 break;
265 case 'h':
Ollie Lhocbbf1252004-03-17 22:22:08 +0000266 default:
267 usage(argv[0]);
268 break;
269 }
270 }
Yinghai Luad8ffd22004-10-20 05:07:16 +0000271
Ollie Lhocbbf1252004-03-17 22:22:08 +0000272 if (read_it && write_it) {
273 printf("-r and -w are mutually exclusive\n");
274 usage(argv[0]);
275 }
Ronald G. Minniche5559572003-03-28 03:29:43 +0000276
Ollie Lhocbbf1252004-03-17 22:22:08 +0000277 if (optind < argc)
278 filename = argv[optind++];
Ronald G. Minniche5559572003-03-28 03:29:43 +0000279
Ollie Lho184a4042005-11-26 21:55:36 +0000280 printf("Calibrating delay loop... ");
Ollie Lhocbbf1252004-03-17 22:22:08 +0000281 myusec_calibrate_delay();
Ollie Lho184a4042005-11-26 21:55:36 +0000282 printf("ok\n");
283
284 /* We look at the lbtable first to see if we need a
285 * mainboard specific flash enable sequence.
286 */
287 linuxbios_init();
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000288
Ollie Lhocbbf1252004-03-17 22:22:08 +0000289 /* try to enable it. Failure IS an option, since not all motherboards
Ollie Lho184a4042005-11-26 21:55:36 +0000290 * really need this to be done, etc., etc.
Ollie Lhocbbf1252004-03-17 22:22:08 +0000291 */
292 (void) enable_flash_write();
Ollie Lho761bf1b2004-03-20 16:46:10 +0000293
294 if ((flash = probe_flash(flashchips)) == NULL) {
Ollie Lho184a4042005-11-26 21:55:36 +0000295 printf("No EEPROM/flash device found.\n");
Ollie Lhocbbf1252004-03-17 22:22:08 +0000296 exit(1);
297 }
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000298
Ollie Lho184a4042005-11-26 21:55:36 +0000299 printf("Flash part is %s\n", flash->name);
300
Ollie Lhoefa28582004-12-08 20:10:01 +0000301 if (!filename && !erase_it) {
Ollie Lho184a4042005-11-26 21:55:36 +0000302 // FIXME: Do we really want this feature implicitly?
303 printf("OK, only ENABLING flash write, but NOT FLASHING.\n");
Ollie Lhocbbf1252004-03-17 22:22:08 +0000304 return 0;
305 }
Ollie Lhocbbf1252004-03-17 22:22:08 +0000306
Ollie Lho184a4042005-11-26 21:55:36 +0000307 size = flash->total_size * 1024;
308 buf = (uint8_t *) calloc(size, sizeof(char));
309
Ollie Lhoefa28582004-12-08 20:10:01 +0000310 if (erase_it) {
311 printf("Erasing flash chip\n");
312 flash->erase(flash);
313 exit(0);
314 } else if (read_it) {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000315 if ((image = fopen(filename, "w")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000316 perror(filename);
317 exit(1);
318 }
319 printf("Reading Flash...");
Ollie Lho73eca802004-03-19 22:10:07 +0000320 if (flash->read == NULL)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000321 memcpy(buf, (const char *) flash->virt_addr, size);
322 else
Ollie Lho761bf1b2004-03-20 16:46:10 +0000323 flash->read(flash, buf);
Yinghai Luad8ffd22004-10-20 05:07:16 +0000324
Ollie Lho98bea8a2004-12-07 03:15:51 +0000325 if (exclude_end_position - exclude_start_position > 0)
326 memset(buf+exclude_start_position, 0,
327 exclude_end_position-exclude_start_position);
Yinghai Luad8ffd22004-10-20 05:07:16 +0000328
Ollie Lhocbbf1252004-03-17 22:22:08 +0000329 fwrite(buf, sizeof(char), size, image);
330 fclose(image);
331 printf("done\n");
332 } else {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000333 if ((image = fopen(filename, "r")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000334 perror(filename);
335 exit(1);
336 }
Ollie Lho761bf1b2004-03-20 16:46:10 +0000337 fread(buf, sizeof(char), size, image);
Ollie Lho184a4042005-11-26 21:55:36 +0000338 show_id(buf, size);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000339 fclose(image);
340 }
341
Ollie Lho184a4042005-11-26 21:55:36 +0000342 /* exclude range stuff. Nice idea, but at the moment it is only
343 * supported in hardware by the pm49fl004 chips.
344 * Instead of implementing this for all chips I suggest advancing
345 * it to the rom layout feature below and drop exclude range
346 * completely once all flash chips can do rom layouts. stepan
347 */
348
349 // ////////////////////////////////////////////////////////////
Ollie Lho98bea8a2004-12-07 03:15:51 +0000350 if (exclude_end_position - exclude_start_position > 0)
Ollie Lhod11f3612004-12-07 17:19:04 +0000351 memcpy(buf+exclude_start_position,
Ollie Lho98bea8a2004-12-07 03:15:51 +0000352 (const char *) flash->virt_addr+exclude_start_position,
353 exclude_end_position-exclude_start_position);
354
Yinghai Luad8ffd22004-10-20 05:07:16 +0000355 exclude_start_page = exclude_start_position/flash->page_size;
Ollie Lho98bea8a2004-12-07 03:15:51 +0000356 if ((exclude_start_position%flash->page_size) != 0) {
357 exclude_start_page++;
358 }
359 exclude_end_page = exclude_end_position/flash->page_size;
Ollie Lho184a4042005-11-26 21:55:36 +0000360 // ////////////////////////////////////////////////////////////
Yinghai Luad8ffd22004-10-20 05:07:16 +0000361
Ollie Lho184a4042005-11-26 21:55:36 +0000362 // This should be moved into each flash part's code to do it
363 // cleanly. This does the job.
364 handle_romentries(buf, (uint8_t *)flash->virt_addr);
365
366 // ////////////////////////////////////////////////////////////
367
368 if (write_it)
Stefan Reinauer143da0b2006-01-04 16:42:57 +0000369 ret |= flash->write(flash, buf);
Ollie Lho184a4042005-11-26 21:55:36 +0000370
Ollie Lhocbbf1252004-03-17 22:22:08 +0000371 if (verify_it)
Stefan Reinauer143da0b2006-01-04 16:42:57 +0000372 ret |= verify_flash(flash, buf);
Ollie Lho184a4042005-11-26 21:55:36 +0000373
Stefan Reinauer143da0b2006-01-04 16:42:57 +0000374 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000375}