blob: fb7d03002aaa29a18cfbc21e0106c0b2c97d6251 [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;
Ollie Lho184a4042005-11-26 21:55:36 +0000150
151 static struct option long_options[]= {
152 { "read", 0, 0, 'r' },
153 { "write", 0, 0, 'w' },
154 { "erase", 0, 0, 'E' },
155 { "verify", 0, 0, 'v' },
156 { "chip", 1, 0, 'c' },
157 { "estart", 1, 0, 's' },
158 { "eend", 1, 0, 'e' },
159 { "mainboard", 1, 0, 'm' },
160 { "verbose", 0, 0, 'V' },
161 { "force", 0, 0, 'f' },
162 { "layout", 1, 0, 'l' },
163 { "image", 1, 0, 'i' },
164 { "help", 0, 0, 'h' },
165 { 0, 0, 0, 0 }
166 };
167
Ollie Lhocbbf1252004-03-17 22:22:08 +0000168 char *filename = NULL;
Ronald G. Minnichceec4202003-07-25 04:37:41 +0000169
Yinghai Luad8ffd22004-10-20 05:07:16 +0000170
171 unsigned int exclude_start_position=0, exclude_end_position=0; // [x,y)
Ollie Lho184a4042005-11-26 21:55:36 +0000172 char *tempstr=NULL, *tempstr2=NULL;
Yinghai Luad8ffd22004-10-20 05:07:16 +0000173
174 if (argc > 1) {
175 /* Yes, print them. */
176 int i;
Ollie Lho184a4042005-11-26 21:55:36 +0000177 printf_debug ("The arguments are:\n");
Yinghai Luad8ffd22004-10-20 05:07:16 +0000178 for (i = 1; i < argc; ++i)
Ollie Lho184a4042005-11-26 21:55:36 +0000179 printf_debug ("%s\n", argv[i]);
Yinghai Luad8ffd22004-10-20 05:07:16 +0000180 }
181
Ollie Lhocbbf1252004-03-17 22:22:08 +0000182 setbuf(stdout, NULL);
Ollie Lho184a4042005-11-26 21:55:36 +0000183 while ((opt = getopt_long(argc, argv, "rwvVEfc:s:e:m:l:i:h", long_options,
184 &option_index)) != EOF) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000185 switch (opt) {
186 case 'r':
187 read_it = 1;
188 break;
189 case 'w':
190 write_it = 1;
191 break;
192 case 'v':
193 verify_it = 1;
194 break;
195 case 'c':
196 chip_to_probe = strdup(optarg);
197 break;
Ollie Lho789ad3d2004-03-18 20:27:33 +0000198 case 'V':
199 verbose = 1;
200 break;
Ollie Lhoefa28582004-12-08 20:10:01 +0000201 case 'E':
202 erase_it = 1;
203 break;
Yinghai Luad8ffd22004-10-20 05:07:16 +0000204 case 's':
205 tempstr = strdup(optarg);
206 sscanf(tempstr,"%x",&exclude_start_position);
207 break;
208 case 'e':
Ollie Lho98bea8a2004-12-07 03:15:51 +0000209 tempstr = strdup(optarg);
210 sscanf(tempstr,"%x",&exclude_end_position);
211 break;
Ollie Lho184a4042005-11-26 21:55:36 +0000212 case 'm':
213 tempstr = strdup(optarg);
214 strtok(tempstr, ":");
215 tempstr2=strtok(NULL, ":");
216 if (tempstr2) {
217 lb_vendor=tempstr;
218 lb_part=tempstr2;
219 } else {
220 printf("warning: ignored wrong format of"
221 " mainboard: %s\n", tempstr);
222 }
223 break;
224 case 'f':
225 force=1;
226 break;
227 case 'l':
228 tempstr=strdup(optarg);
229 read_romlayout(tempstr);
230 break;
231 case 'i':
232 tempstr=strdup(optarg);
233 find_romentry(tempstr);
234 break;
235 case 'h':
Ollie Lhocbbf1252004-03-17 22:22:08 +0000236 default:
237 usage(argv[0]);
238 break;
239 }
240 }
Yinghai Luad8ffd22004-10-20 05:07:16 +0000241
Ollie Lhocbbf1252004-03-17 22:22:08 +0000242 if (read_it && write_it) {
243 printf("-r and -w are mutually exclusive\n");
244 usage(argv[0]);
245 }
Ronald G. Minniche5559572003-03-28 03:29:43 +0000246
Ollie Lhocbbf1252004-03-17 22:22:08 +0000247 if (optind < argc)
248 filename = argv[optind++];
Ronald G. Minniche5559572003-03-28 03:29:43 +0000249
Ollie Lho184a4042005-11-26 21:55:36 +0000250 printf("Calibrating delay loop... ");
Ollie Lhocbbf1252004-03-17 22:22:08 +0000251 myusec_calibrate_delay();
Ollie Lho184a4042005-11-26 21:55:36 +0000252 printf("ok\n");
253
254 /* We look at the lbtable first to see if we need a
255 * mainboard specific flash enable sequence.
256 */
257 linuxbios_init();
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000258
Ollie Lhocbbf1252004-03-17 22:22:08 +0000259 /* try to enable it. Failure IS an option, since not all motherboards
Ollie Lho184a4042005-11-26 21:55:36 +0000260 * really need this to be done, etc., etc.
Ollie Lhocbbf1252004-03-17 22:22:08 +0000261 */
262 (void) enable_flash_write();
Ollie Lho761bf1b2004-03-20 16:46:10 +0000263
264 if ((flash = probe_flash(flashchips)) == NULL) {
Ollie Lho184a4042005-11-26 21:55:36 +0000265 printf("No EEPROM/flash device found.\n");
Ollie Lhocbbf1252004-03-17 22:22:08 +0000266 exit(1);
267 }
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000268
Ollie Lho184a4042005-11-26 21:55:36 +0000269 printf("Flash part is %s\n", flash->name);
270
Ollie Lhoefa28582004-12-08 20:10:01 +0000271 if (!filename && !erase_it) {
Ollie Lho184a4042005-11-26 21:55:36 +0000272 // FIXME: Do we really want this feature implicitly?
273 printf("OK, only ENABLING flash write, but NOT FLASHING.\n");
Ollie Lhocbbf1252004-03-17 22:22:08 +0000274 return 0;
275 }
Ollie Lhocbbf1252004-03-17 22:22:08 +0000276
Ollie Lho184a4042005-11-26 21:55:36 +0000277 size = flash->total_size * 1024;
278 buf = (uint8_t *) calloc(size, sizeof(char));
279
Ollie Lhoefa28582004-12-08 20:10:01 +0000280 if (erase_it) {
281 printf("Erasing flash chip\n");
282 flash->erase(flash);
283 exit(0);
284 } else if (read_it) {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000285 if ((image = fopen(filename, "w")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000286 perror(filename);
287 exit(1);
288 }
289 printf("Reading Flash...");
Ollie Lho73eca802004-03-19 22:10:07 +0000290 if (flash->read == NULL)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000291 memcpy(buf, (const char *) flash->virt_addr, size);
292 else
Ollie Lho761bf1b2004-03-20 16:46:10 +0000293 flash->read(flash, buf);
Yinghai Luad8ffd22004-10-20 05:07:16 +0000294
Ollie Lho98bea8a2004-12-07 03:15:51 +0000295 if (exclude_end_position - exclude_start_position > 0)
296 memset(buf+exclude_start_position, 0,
297 exclude_end_position-exclude_start_position);
Yinghai Luad8ffd22004-10-20 05:07:16 +0000298
Ollie Lhocbbf1252004-03-17 22:22:08 +0000299 fwrite(buf, sizeof(char), size, image);
300 fclose(image);
301 printf("done\n");
302 } else {
Ollie Lho761bf1b2004-03-20 16:46:10 +0000303 if ((image = fopen(filename, "r")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +0000304 perror(filename);
305 exit(1);
306 }
Ollie Lho761bf1b2004-03-20 16:46:10 +0000307 fread(buf, sizeof(char), size, image);
Ollie Lho184a4042005-11-26 21:55:36 +0000308 show_id(buf, size);
Ollie Lhocbbf1252004-03-17 22:22:08 +0000309 fclose(image);
310 }
311
Ollie Lho184a4042005-11-26 21:55:36 +0000312 /* exclude range stuff. Nice idea, but at the moment it is only
313 * supported in hardware by the pm49fl004 chips.
314 * Instead of implementing this for all chips I suggest advancing
315 * it to the rom layout feature below and drop exclude range
316 * completely once all flash chips can do rom layouts. stepan
317 */
318
319 // ////////////////////////////////////////////////////////////
Ollie Lho98bea8a2004-12-07 03:15:51 +0000320 if (exclude_end_position - exclude_start_position > 0)
Ollie Lhod11f3612004-12-07 17:19:04 +0000321 memcpy(buf+exclude_start_position,
Ollie Lho98bea8a2004-12-07 03:15:51 +0000322 (const char *) flash->virt_addr+exclude_start_position,
323 exclude_end_position-exclude_start_position);
324
Yinghai Luad8ffd22004-10-20 05:07:16 +0000325 exclude_start_page = exclude_start_position/flash->page_size;
Ollie Lho98bea8a2004-12-07 03:15:51 +0000326 if ((exclude_start_position%flash->page_size) != 0) {
327 exclude_start_page++;
328 }
329 exclude_end_page = exclude_end_position/flash->page_size;
Ollie Lho184a4042005-11-26 21:55:36 +0000330 // ////////////////////////////////////////////////////////////
Yinghai Luad8ffd22004-10-20 05:07:16 +0000331
Ollie Lho184a4042005-11-26 21:55:36 +0000332 // This should be moved into each flash part's code to do it
333 // cleanly. This does the job.
334 handle_romentries(buf, (uint8_t *)flash->virt_addr);
335
336 // ////////////////////////////////////////////////////////////
337
338 if (write_it)
Ollie Lho761bf1b2004-03-20 16:46:10 +0000339 flash->write(flash, buf);
Ollie Lho184a4042005-11-26 21:55:36 +0000340
Ollie Lhocbbf1252004-03-17 22:22:08 +0000341 if (verify_it)
Stefan Reinauere3705282005-12-18 16:41:10 +0000342 verify_flash(flash, buf);
Ollie Lho184a4042005-11-26 21:55:36 +0000343
Ronald G. Minnichc73ad982003-02-11 21:06:09 +0000344 return 0;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000345}