blob: db3c2c909533f73d2a8fa2ad3f59b943704902a5 [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
Ollie Lho184a4042005-11-26 21:55:36 +00007 * Copyright 2005 coresystems GmbH
8 * Stefan Reinauer <stepan@core-systems.de> added rom layout
9 * support, and checking for suitable rom image
10 *
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000025 */
26
27#include <errno.h>
28#include <fcntl.h>
29#include <sys/mman.h>
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000030#include <unistd.h>
31#include <stdio.h>
Ronald G. Minnichceec4202003-07-25 04:37:41 +000032#include <string.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000033#include <stdlib.h>
Ollie Lho184a4042005-11-26 21:55:36 +000034#include <getopt.h>
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000035
36#include "flash.h"
Ollie Lho184a4042005-11-26 21:55:36 +000037#include "lbtable.h"
38#include "layout.h"
39#include "debug.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000040
Ronald G. Minnichceec4202003-07-25 04:37:41 +000041char *chip_to_probe = NULL;
Ronald G. Minnich7bd1dee2003-05-08 19:33:19 +000042
Stefan Reinauere3705282005-12-18 16:41:10 +000043int exclude_start_page, exclude_end_page;
44int force=0, verbose=0;
45
Ollie Lho761bf1b2004-03-20 16:46:10 +000046struct flashchip *probe_flash(struct flashchip *flash)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000047{
Ollie Lhocbbf1252004-03-17 22:22:08 +000048 int fd_mem;
Ollie Lho184a4042005-11-26 21:55:36 +000049 volatile uint8_t *bios;
Ollie Lhocbbf1252004-03-17 22:22:08 +000050 unsigned long size;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000051
Ollie Lhocbbf1252004-03-17 22:22:08 +000052 if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
53 perror("Can not open /dev/mem");
54 exit(1);
55 }
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000056
Ollie Lhocbbf1252004-03-17 22:22:08 +000057 while (flash->name != NULL) {
Ollie Lho8b8897a2004-03-27 00:18:15 +000058 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) {
Ollie Lhocbbf1252004-03-17 22:22:08 +000059 flash++;
60 continue;
61 }
Ollie Lho184a4042005-11-26 21:55:36 +000062 printf_debug("Trying %s, %d KB\n", flash->name, flash->total_size);
Ollie Lhocbbf1252004-03-17 22:22:08 +000063 size = flash->total_size * 1024;
64 /* BUG? what happens if getpagesize() > size!?
65 -> ``Error MMAP /dev/mem: Invalid argument'' NIKI */
66 if (getpagesize() > size) {
67 size = getpagesize();
Ollie Lho761bf1b2004-03-20 16:46:10 +000068 printf("%s: warning: size: %d -> %ld\n",
69 __FUNCTION__, flash->total_size * 1024,
70 (unsigned long) size);
Ollie Lhocbbf1252004-03-17 22:22:08 +000071 }
Ollie Lho761bf1b2004-03-20 16:46:10 +000072 bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
73 fd_mem, (off_t) (0xffffffff - size + 1));
Ollie Lhocbbf1252004-03-17 22:22:08 +000074 if (bios == MAP_FAILED) {
75 perror("Error MMAP /dev/mem");
76 exit(1);
77 }
78 flash->virt_addr = bios;
79 flash->fd_mem = fd_mem;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000080
Ollie Lhocbbf1252004-03-17 22:22:08 +000081 if (flash->probe(flash) == 1) {
Ollie Lho761bf1b2004-03-20 16:46:10 +000082 printf("%s found at physical address: 0x%lx\n",
83 flash->name, (0xffffffff - size + 1));
Ollie Lhocbbf1252004-03-17 22:22:08 +000084 return flash;
85 }
Ollie Lho761bf1b2004-03-20 16:46:10 +000086 munmap((void *) bios, size);
Ollie Lhocbbf1252004-03-17 22:22:08 +000087 flash++;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000088 }
Ollie Lhocbbf1252004-03-17 22:22:08 +000089 return NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000090}
91
Stefan Reinauere3705282005-12-18 16:41:10 +000092int verify_flash(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000093{
Ollie Lhof51a7662004-03-20 17:05:01 +000094 int i;
Ollie Lho761bf1b2004-03-20 16:46:10 +000095 int total_size = flash->total_size * 1024;
Ollie Lho184a4042005-11-26 21:55:36 +000096 volatile uint8_t *bios = flash->virt_addr;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000097
Ollie Lhocbbf1252004-03-17 22:22:08 +000098 printf("Verifying address: ");
Ollie Lhof51a7662004-03-20 17:05:01 +000099 for (i = 0; i < total_size; i++) {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000100 if (verbose)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000101 printf("0x%08x", i);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000102 if (*(bios + i) != *(buf + i)) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000103 printf("FAILED\n");
104 return 0;
105 }
Ollie Lho761bf1b2004-03-20 16:46:10 +0000106 if (verbose)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000107 printf("\b\b\b\b\b\b\b\b\b\b");
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000108 }
Ollie Lhocbbf1252004-03-17 22:22:08 +0000109 if (verbose)
110 printf("\n");
111 else
112 printf("VERIFIED\n");
113 return 1;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000114}
115
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000116
Ronald G. Minniche5559572003-03-28 03:29:43 +0000117void usage(const char *name)
118{
Ollie Lho184a4042005-11-26 21:55:36 +0000119 printf("usage: %s [-rwvE] [-V] [-c chipname] [-s exclude_start] [-e exclude_end] [file]\n", name);
120 printf(" -r | --read: read flash and save into file\n"
121 " -w | --write: write file into flash (default when file is specified)\n"
122 " -v | --verify: verify flash against file\n"
123 " -E | --erase: Erase flash device\n"
124 " -V | --verbose: more verbose output\n\n"
125 " -c | --chip <chipname>: probe only for specified flash chip\n"
126 " -s | --estart <addr>: exclude start position\n"
127 " -e | --eend <addr>: exclude end postion\n"
128 " -m | --mainboard <vendor:part>: override mainboard settings\n"
129 " -f | --force: force write without checking image\n"
130 " -l | --layout <file.layout>: read rom layout from file\n"
131 " -i | --image <name>: only flash image name from flash layout\n"
132 "\n"
Ollie Lhocbbf1252004-03-17 22:22:08 +0000133 " If no file is specified, then all that happens\n"
Ollie Lho184a4042005-11-26 21:55:36 +0000134 " is that flash info is dumped\n\n");
Ollie Lhocbbf1252004-03-17 22:22:08 +0000135 exit(1);
Ronald G. Minniche5559572003-03-28 03:29:43 +0000136}
137
Ollie Lho761bf1b2004-03-20 16:46:10 +0000138int main(int argc, char *argv[])
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000139{
Ollie Lho184a4042005-11-26 21:55:36 +0000140 uint8_t *buf;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000141 unsigned long size;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000142 FILE *image;
143 struct flashchip *flash;
Ollie Lhocbbf1252004-03-17 22:22:08 +0000144 int opt;
Ollie Lho184a4042005-11-26 21:55:36 +0000145 int option_index = 0;
146 int read_it = 0,
147 write_it = 0,
148 erase_it = 0,
149 verify_it = 0;
Stefan Reinauer143da0b2006-01-04 16:42:57 +0000150 int ret = 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000151
152 static struct option long_options[]= {
153 { "read", 0, 0, 'r' },
154 { "write", 0, 0, 'w' },
155 { "erase", 0, 0, 'E' },
156 { "verify", 0, 0, 'v' },
157 { "chip", 1, 0, 'c' },
158 { "estart", 1, 0, 's' },
159 { "eend", 1, 0, 'e' },
160 { "mainboard", 1, 0, 'm' },
161 { "verbose", 0, 0, 'V' },
162 { "force", 0, 0, 'f' },
163 { "layout", 1, 0, 'l' },
164 { "image", 1, 0, 'i' },
165 { "help", 0, 0, 'h' },
166 { 0, 0, 0, 0 }
167 };
168
Ollie Lhocbbf1252004-03-17 22:22:08 +0000169 char *filename = NULL;
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000170
Yinghai Luad8ffd22004-10-20 05:07:16 +0000171
172 unsigned int exclude_start_position=0, exclude_end_position=0; // [x,y)
Ollie Lho184a4042005-11-26 21:55:36 +0000173 char *tempstr=NULL, *tempstr2=NULL;
Yinghai Luad8ffd22004-10-20 05:07:16 +0000174
175 if (argc > 1) {
176 /* Yes, print them. */
177 int i;
Ollie Lho184a4042005-11-26 21:55:36 +0000178 printf_debug ("The arguments are:\n");
Yinghai Luad8ffd22004-10-20 05:07:16 +0000179 for (i = 1; i < argc; ++i)
Ollie Lho184a4042005-11-26 21:55:36 +0000180 printf_debug ("%s\n", argv[i]);
Yinghai Luad8ffd22004-10-20 05:07:16 +0000181 }
182
Ollie Lhocbbf1252004-03-17 22:22:08 +0000183 setbuf(stdout, NULL);
Ollie Lho184a4042005-11-26 21:55:36 +0000184 while ((opt = getopt_long(argc, argv, "rwvVEfc:s:e:m:l:i:h", long_options,
185 &option_index)) != EOF) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000186 switch (opt) {
187 case 'r':
188 read_it = 1;
189 break;
190 case 'w':
191 write_it = 1;
192 break;
193 case 'v':
194 verify_it = 1;
195 break;
196 case 'c':
197 chip_to_probe = strdup(optarg);
198 break;
Ollie Lho789ad3d2004-03-18 20:27:33 +0000199 case 'V':
200 verbose = 1;
201 break;
Ollie Lhoefa28582004-12-08 20:10:01 +0000202 case 'E':
203 erase_it = 1;
204 break;
Yinghai Luad8ffd22004-10-20 05:07:16 +0000205 case 's':
206 tempstr = strdup(optarg);
207 sscanf(tempstr,"%x",&exclude_start_position);
208 break;
209 case 'e':
Ollie Lho98bea8a2004-12-07 03:15:51 +0000210 tempstr = strdup(optarg);
211 sscanf(tempstr,"%x",&exclude_end_position);
212 break;
Ollie Lho184a4042005-11-26 21:55:36 +0000213 case 'm':
214 tempstr = strdup(optarg);
215 strtok(tempstr, ":");
216 tempstr2=strtok(NULL, ":");
217 if (tempstr2) {
218 lb_vendor=tempstr;
219 lb_part=tempstr2;
220 } else {
221 printf("warning: ignored wrong format of"
222 " mainboard: %s\n", tempstr);
223 }
224 break;
225 case 'f':
226 force=1;
227 break;
228 case 'l':
229 tempstr=strdup(optarg);
230 read_romlayout(tempstr);
231 break;
232 case 'i':
233 tempstr=strdup(optarg);
234 find_romentry(tempstr);
235 break;
236 case 'h':
Ollie Lhocbbf1252004-03-17 22:22:08 +0000237 default:
238 usage(argv[0]);
239 break;
240 }
241 }
Yinghai Luad8ffd22004-10-20 05:07:16 +0000242
Ollie Lhocbbf1252004-03-17 22:22:08 +0000243 if (read_it && write_it) {
244 printf("-r and -w are mutually exclusive\n");
245 usage(argv[0]);
246 }
Ronald G. Minniche5559572003-03-28 03:29:43 +0000247
Ollie Lhocbbf1252004-03-17 22:22:08 +0000248 if (optind < argc)
249 filename = argv[optind++];
Ronald G. Minniche5559572003-03-28 03:29:43 +0000250
Ollie Lho184a4042005-11-26 21:55:36 +0000251 printf("Calibrating delay loop... ");
Ollie Lhocbbf1252004-03-17 22:22:08 +0000252 myusec_calibrate_delay();
Ollie Lho184a4042005-11-26 21:55:36 +0000253 printf("ok\n");
254
255 /* We look at the lbtable first to see if we need a
256 * mainboard specific flash enable sequence.
257 */
258 linuxbios_init();
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000259
Ollie Lhocbbf1252004-03-17 22:22:08 +0000260 /* try to enable it. Failure IS an option, since not all motherboards
Ollie Lho184a4042005-11-26 21:55:36 +0000261 * really need this to be done, etc., etc.
Ollie Lhocbbf1252004-03-17 22:22:08 +0000262 */
263 (void) enable_flash_write();
Ollie Lho761bf1b2004-03-20 16:46:10 +0000264
265 if ((flash = probe_flash(flashchips)) == NULL) {
Ollie Lho184a4042005-11-26 21:55:36 +0000266 printf("No EEPROM/flash device found.\n");
Ollie Lhocbbf1252004-03-17 22:22:08 +0000267 exit(1);
268 }
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000269
Ollie Lho184a4042005-11-26 21:55:36 +0000270 printf("Flash part is %s\n", flash->name);
271
Ollie Lhoefa28582004-12-08 20:10:01 +0000272 if (!filename && !erase_it) {
Ollie Lho184a4042005-11-26 21:55:36 +0000273 // FIXME: Do we really want this feature implicitly?
274 printf("OK, only ENABLING flash write, but NOT FLASHING.\n");
Ollie Lhocbbf1252004-03-17 22:22:08 +0000275 return 0;
276 }
Ollie Lhocbbf1252004-03-17 22:22:08 +0000277
Ollie Lho184a4042005-11-26 21:55:36 +0000278 size = flash->total_size * 1024;
279 buf = (uint8_t *) calloc(size, sizeof(char));
280
Ollie Lhoefa28582004-12-08 20:10:01 +0000281 if (erase_it) {
282 printf("Erasing flash chip\n");
283 flash->erase(flash);
284 exit(0);
285 } else if (read_it) {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000286 if ((image = fopen(filename, "w")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000287 perror(filename);
288 exit(1);
289 }
290 printf("Reading Flash...");
Ollie Lho73eca802004-03-19 22:10:07 +0000291 if (flash->read == NULL)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000292 memcpy(buf, (const char *) flash->virt_addr, size);
293 else
Ollie Lho761bf1b2004-03-20 16:46:10 +0000294 flash->read(flash, buf);
Yinghai Luad8ffd22004-10-20 05:07:16 +0000295
Ollie Lho98bea8a2004-12-07 03:15:51 +0000296 if (exclude_end_position - exclude_start_position > 0)
297 memset(buf+exclude_start_position, 0,
298 exclude_end_position-exclude_start_position);
Yinghai Luad8ffd22004-10-20 05:07:16 +0000299
Ollie Lhocbbf1252004-03-17 22:22:08 +0000300 fwrite(buf, sizeof(char), size, image);
301 fclose(image);
302 printf("done\n");
303 } else {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000304 if ((image = fopen(filename, "r")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000305 perror(filename);
306 exit(1);
307 }
Ollie Lho761bf1b2004-03-20 16:46:10 +0000308 fread(buf, sizeof(char), size, image);
Ollie Lho184a4042005-11-26 21:55:36 +0000309 show_id(buf, size);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000310 fclose(image);
311 }
312
Ollie Lho184a4042005-11-26 21:55:36 +0000313 /* exclude range stuff. Nice idea, but at the moment it is only
314 * supported in hardware by the pm49fl004 chips.
315 * Instead of implementing this for all chips I suggest advancing
316 * it to the rom layout feature below and drop exclude range
317 * completely once all flash chips can do rom layouts. stepan
318 */
319
320 // ////////////////////////////////////////////////////////////
Ollie Lho98bea8a2004-12-07 03:15:51 +0000321 if (exclude_end_position - exclude_start_position > 0)
Ollie Lhod11f3612004-12-07 17:19:04 +0000322 memcpy(buf+exclude_start_position,
Ollie Lho98bea8a2004-12-07 03:15:51 +0000323 (const char *) flash->virt_addr+exclude_start_position,
324 exclude_end_position-exclude_start_position);
325
Yinghai Luad8ffd22004-10-20 05:07:16 +0000326 exclude_start_page = exclude_start_position/flash->page_size;
Ollie Lho98bea8a2004-12-07 03:15:51 +0000327 if ((exclude_start_position%flash->page_size) != 0) {
328 exclude_start_page++;
329 }
330 exclude_end_page = exclude_end_position/flash->page_size;
Ollie Lho184a4042005-11-26 21:55:36 +0000331 // ////////////////////////////////////////////////////////////
Yinghai Luad8ffd22004-10-20 05:07:16 +0000332
Ollie Lho184a4042005-11-26 21:55:36 +0000333 // This should be moved into each flash part's code to do it
334 // cleanly. This does the job.
335 handle_romentries(buf, (uint8_t *)flash->virt_addr);
336
337 // ////////////////////////////////////////////////////////////
338
339 if (write_it)
Stefan Reinauer143da0b2006-01-04 16:42:57 +0000340 ret |= flash->write(flash, buf);
Ollie Lho184a4042005-11-26 21:55:36 +0000341
Ollie Lhocbbf1252004-03-17 22:22:08 +0000342 if (verify_it)
Stefan Reinauer143da0b2006-01-04 16:42:57 +0000343 ret |= verify_flash(flash, buf);
Ollie Lho184a4042005-11-26 21:55:36 +0000344
Stefan Reinauer143da0b2006-01-04 16:42:57 +0000345 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000346}