blob: 7b38c8a37eb4cbc7022573951c925e7e34083a7c [file] [log] [blame]
rminnich8d3ff912003-10-25 17:01:29 +00001/*
ollie6a600992005-11-26 21:55:36 +00002 * flash_rom.c: Flash programming utility
rminnich8d3ff912003-10-25 17:01:29 +00003 *
4 * Copyright 2000 Silicon Integrated System Corporation
yhlu5d4383a2004-10-20 05:07:16 +00005 * Copyright 2004 Tyan Corp
6 * yhlu yhlu@tyan.com add exclude start and end option
ollie6a600992005-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 *
rminnich8d3ff912003-10-25 17:01:29 +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 *
rminnich8d3ff912003-10-25 17:01:29 +000025 */
26
27#include <errno.h>
28#include <fcntl.h>
29#include <sys/mman.h>
rminnich8d3ff912003-10-25 17:01:29 +000030#include <unistd.h>
31#include <stdio.h>
rminnich8d3ff912003-10-25 17:01:29 +000032#include <string.h>
33#include <stdlib.h>
ollie6a600992005-11-26 21:55:36 +000034#include <getopt.h>
rminnich8d3ff912003-10-25 17:01:29 +000035
36#include "flash.h"
ollie6a600992005-11-26 21:55:36 +000037#include "lbtable.h"
38#include "layout.h"
39#include "debug.h"
rminnich8d3ff912003-10-25 17:01:29 +000040
41char *chip_to_probe = NULL;
rminnich8d3ff912003-10-25 17:01:29 +000042
ollie5b621572004-03-20 16:46:10 +000043struct flashchip *probe_flash(struct flashchip *flash)
rminnich8d3ff912003-10-25 17:01:29 +000044{
ollie5672ac62004-03-17 22:22:08 +000045 int fd_mem;
ollie6a600992005-11-26 21:55:36 +000046 volatile uint8_t *bios;
ollie5672ac62004-03-17 22:22:08 +000047 unsigned long size;
rminnich8d3ff912003-10-25 17:01:29 +000048
ollie5672ac62004-03-17 22:22:08 +000049 if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
50 perror("Can not open /dev/mem");
51 exit(1);
52 }
rminnich8d3ff912003-10-25 17:01:29 +000053
ollie5672ac62004-03-17 22:22:08 +000054 while (flash->name != NULL) {
ollief1845bd2004-03-27 00:18:15 +000055 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) {
ollie5672ac62004-03-17 22:22:08 +000056 flash++;
57 continue;
58 }
ollie6a600992005-11-26 21:55:36 +000059 printf_debug("Trying %s, %d KB\n", flash->name, flash->total_size);
ollie5672ac62004-03-17 22:22:08 +000060 size = flash->total_size * 1024;
61 /* BUG? what happens if getpagesize() > size!?
62 -> ``Error MMAP /dev/mem: Invalid argument'' NIKI */
63 if (getpagesize() > size) {
64 size = getpagesize();
ollie5b621572004-03-20 16:46:10 +000065 printf("%s: warning: size: %d -> %ld\n",
66 __FUNCTION__, flash->total_size * 1024,
67 (unsigned long) size);
ollie5672ac62004-03-17 22:22:08 +000068 }
ollie5b621572004-03-20 16:46:10 +000069 bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
70 fd_mem, (off_t) (0xffffffff - size + 1));
ollie5672ac62004-03-17 22:22:08 +000071 if (bios == MAP_FAILED) {
72 perror("Error MMAP /dev/mem");
73 exit(1);
74 }
75 flash->virt_addr = bios;
76 flash->fd_mem = fd_mem;
rminnich8d3ff912003-10-25 17:01:29 +000077
ollie5672ac62004-03-17 22:22:08 +000078 if (flash->probe(flash) == 1) {
ollie5b621572004-03-20 16:46:10 +000079 printf("%s found at physical address: 0x%lx\n",
80 flash->name, (0xffffffff - size + 1));
ollie5672ac62004-03-17 22:22:08 +000081 return flash;
82 }
ollie5b621572004-03-20 16:46:10 +000083 munmap((void *) bios, size);
ollie5672ac62004-03-17 22:22:08 +000084 flash++;
rminnich8d3ff912003-10-25 17:01:29 +000085 }
ollie5672ac62004-03-17 22:22:08 +000086 return NULL;
rminnich8d3ff912003-10-25 17:01:29 +000087}
88
ollie6a600992005-11-26 21:55:36 +000089int verify_flash(struct flashchip *flash, uint8_t *buf, int verbose)
rminnich8d3ff912003-10-25 17:01:29 +000090{
ollie4df7da42004-03-20 17:05:01 +000091 int i;
ollie5b621572004-03-20 16:46:10 +000092 int total_size = flash->total_size * 1024;
ollie6a600992005-11-26 21:55:36 +000093 volatile uint8_t *bios = flash->virt_addr;
rminnich8d3ff912003-10-25 17:01:29 +000094
ollie5672ac62004-03-17 22:22:08 +000095 printf("Verifying address: ");
ollie4df7da42004-03-20 17:05:01 +000096 for (i = 0; i < total_size; i++) {
ollie5b621572004-03-20 16:46:10 +000097 if (verbose)
ollie5672ac62004-03-17 22:22:08 +000098 printf("0x%08x", i);
ollie5b621572004-03-20 16:46:10 +000099 if (*(bios + i) != *(buf + i)) {
ollie5672ac62004-03-17 22:22:08 +0000100 printf("FAILED\n");
101 return 0;
102 }
ollie5b621572004-03-20 16:46:10 +0000103 if (verbose)
ollie5672ac62004-03-17 22:22:08 +0000104 printf("\b\b\b\b\b\b\b\b\b\b");
rminnich8d3ff912003-10-25 17:01:29 +0000105 }
ollie5672ac62004-03-17 22:22:08 +0000106 if (verbose)
107 printf("\n");
108 else
109 printf("VERIFIED\n");
110 return 1;
rminnich8d3ff912003-10-25 17:01:29 +0000111}
112
rminnich8d3ff912003-10-25 17:01:29 +0000113
114void usage(const char *name)
115{
ollie6a600992005-11-26 21:55:36 +0000116 printf("usage: %s [-rwvE] [-V] [-c chipname] [-s exclude_start] [-e exclude_end] [file]\n", name);
117 printf(" -r | --read: read flash and save into file\n"
118 " -w | --write: write file into flash (default when file is specified)\n"
119 " -v | --verify: verify flash against file\n"
120 " -E | --erase: Erase flash device\n"
121 " -V | --verbose: more verbose output\n\n"
122 " -c | --chip <chipname>: probe only for specified flash chip\n"
123 " -s | --estart <addr>: exclude start position\n"
124 " -e | --eend <addr>: exclude end postion\n"
125 " -m | --mainboard <vendor:part>: override mainboard settings\n"
126 " -f | --force: force write without checking image\n"
127 " -l | --layout <file.layout>: read rom layout from file\n"
128 " -i | --image <name>: only flash image name from flash layout\n"
129 "\n"
ollie5672ac62004-03-17 22:22:08 +0000130 " If no file is specified, then all that happens\n"
ollie6a600992005-11-26 21:55:36 +0000131 " is that flash info is dumped\n\n");
ollie5672ac62004-03-17 22:22:08 +0000132 exit(1);
rminnich8d3ff912003-10-25 17:01:29 +0000133}
134
yhlu5d4383a2004-10-20 05:07:16 +0000135int exclude_start_page, exclude_end_page;
ollie6a600992005-11-26 21:55:36 +0000136int force=0;
yhlu5d4383a2004-10-20 05:07:16 +0000137
ollie5b621572004-03-20 16:46:10 +0000138int main(int argc, char *argv[])
rminnich8d3ff912003-10-25 17:01:29 +0000139{
ollie6a600992005-11-26 21:55:36 +0000140 uint8_t *buf;
ollie5672ac62004-03-17 22:22:08 +0000141 unsigned long size;
ollie5b621572004-03-20 16:46:10 +0000142 FILE *image;
143 struct flashchip *flash;
ollie5672ac62004-03-17 22:22:08 +0000144 int opt;
ollie6a600992005-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;
150 int verbose = 0;
151
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
ollie5672ac62004-03-17 22:22:08 +0000169 char *filename = NULL;
rminnich8d3ff912003-10-25 17:01:29 +0000170
yhlu5d4383a2004-10-20 05:07:16 +0000171
172 unsigned int exclude_start_position=0, exclude_end_position=0; // [x,y)
ollie6a600992005-11-26 21:55:36 +0000173 char *tempstr=NULL, *tempstr2=NULL;
yhlu5d4383a2004-10-20 05:07:16 +0000174
175 if (argc > 1) {
176 /* Yes, print them. */
177 int i;
ollie6a600992005-11-26 21:55:36 +0000178 printf_debug ("The arguments are:\n");
yhlu5d4383a2004-10-20 05:07:16 +0000179 for (i = 1; i < argc; ++i)
ollie6a600992005-11-26 21:55:36 +0000180 printf_debug ("%s\n", argv[i]);
yhlu5d4383a2004-10-20 05:07:16 +0000181 }
182
ollie5672ac62004-03-17 22:22:08 +0000183 setbuf(stdout, NULL);
ollie6a600992005-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) {
ollie5672ac62004-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;
ollie077017b2004-03-18 20:27:33 +0000199 case 'V':
200 verbose = 1;
201 break;
ollie0eb62d62004-12-08 20:10:01 +0000202 case 'E':
203 erase_it = 1;
204 break;
yhlu5d4383a2004-10-20 05:07:16 +0000205 case 's':
206 tempstr = strdup(optarg);
207 sscanf(tempstr,"%x",&exclude_start_position);
208 break;
209 case 'e':
olliea4302802004-12-07 03:15:51 +0000210 tempstr = strdup(optarg);
211 sscanf(tempstr,"%x",&exclude_end_position);
212 break;
ollie6a600992005-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':
ollie5672ac62004-03-17 22:22:08 +0000237 default:
238 usage(argv[0]);
239 break;
240 }
241 }
yhlu5d4383a2004-10-20 05:07:16 +0000242
ollie5672ac62004-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 }
rminnich8d3ff912003-10-25 17:01:29 +0000247
ollie5672ac62004-03-17 22:22:08 +0000248 if (optind < argc)
249 filename = argv[optind++];
rminnich8d3ff912003-10-25 17:01:29 +0000250
ollie6a600992005-11-26 21:55:36 +0000251 printf("Calibrating delay loop... ");
ollie5672ac62004-03-17 22:22:08 +0000252 myusec_calibrate_delay();
ollie6a600992005-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();
rminnich8d3ff912003-10-25 17:01:29 +0000259
ollie5672ac62004-03-17 22:22:08 +0000260 /* try to enable it. Failure IS an option, since not all motherboards
ollie6a600992005-11-26 21:55:36 +0000261 * really need this to be done, etc., etc.
ollie5672ac62004-03-17 22:22:08 +0000262 */
263 (void) enable_flash_write();
ollie5b621572004-03-20 16:46:10 +0000264
265 if ((flash = probe_flash(flashchips)) == NULL) {
ollie6a600992005-11-26 21:55:36 +0000266 printf("No EEPROM/flash device found.\n");
ollie5672ac62004-03-17 22:22:08 +0000267 exit(1);
268 }
rminnich8d3ff912003-10-25 17:01:29 +0000269
ollie6a600992005-11-26 21:55:36 +0000270 printf("Flash part is %s\n", flash->name);
271
ollie0eb62d62004-12-08 20:10:01 +0000272 if (!filename && !erase_it) {
ollie6a600992005-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");
ollie5672ac62004-03-17 22:22:08 +0000275 return 0;
276 }
ollie5672ac62004-03-17 22:22:08 +0000277
ollie6a600992005-11-26 21:55:36 +0000278 size = flash->total_size * 1024;
279 buf = (uint8_t *) calloc(size, sizeof(char));
280
ollie0eb62d62004-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) {
ollie5b621572004-03-20 16:46:10 +0000286 if ((image = fopen(filename, "w")) == NULL) {
ollie5672ac62004-03-17 22:22:08 +0000287 perror(filename);
288 exit(1);
289 }
290 printf("Reading Flash...");
olliea3def632004-03-19 22:10:07 +0000291 if (flash->read == NULL)
ollie5672ac62004-03-17 22:22:08 +0000292 memcpy(buf, (const char *) flash->virt_addr, size);
293 else
ollie5b621572004-03-20 16:46:10 +0000294 flash->read(flash, buf);
yhlu5d4383a2004-10-20 05:07:16 +0000295
olliea4302802004-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);
yhlu5d4383a2004-10-20 05:07:16 +0000299
ollie5672ac62004-03-17 22:22:08 +0000300 fwrite(buf, sizeof(char), size, image);
301 fclose(image);
302 printf("done\n");
303 } else {
ollie5b621572004-03-20 16:46:10 +0000304 if ((image = fopen(filename, "r")) == NULL) {
ollie5672ac62004-03-17 22:22:08 +0000305 perror(filename);
306 exit(1);
307 }
ollie5b621572004-03-20 16:46:10 +0000308 fread(buf, sizeof(char), size, image);
ollie6a600992005-11-26 21:55:36 +0000309 show_id(buf, size);
ollie5672ac62004-03-17 22:22:08 +0000310 fclose(image);
311 }
312
ollie6a600992005-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 // ////////////////////////////////////////////////////////////
olliea4302802004-12-07 03:15:51 +0000321 if (exclude_end_position - exclude_start_position > 0)
olliefc9a03b2004-12-07 17:19:04 +0000322 memcpy(buf+exclude_start_position,
olliea4302802004-12-07 03:15:51 +0000323 (const char *) flash->virt_addr+exclude_start_position,
324 exclude_end_position-exclude_start_position);
325
yhlu5d4383a2004-10-20 05:07:16 +0000326 exclude_start_page = exclude_start_position/flash->page_size;
olliea4302802004-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;
ollie6a600992005-11-26 21:55:36 +0000331 // ////////////////////////////////////////////////////////////
yhlu5d4383a2004-10-20 05:07:16 +0000332
ollie6a600992005-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)
ollie5b621572004-03-20 16:46:10 +0000340 flash->write(flash, buf);
ollie6a600992005-11-26 21:55:36 +0000341
ollie5672ac62004-03-17 22:22:08 +0000342 if (verify_it)
ollie5b621572004-03-20 16:46:10 +0000343 verify_flash(flash, buf, verbose);
ollie6a600992005-11-26 21:55:36 +0000344
rminnich8d3ff912003-10-25 17:01:29 +0000345 return 0;
rminnich8d3ff912003-10-25 17:01:29 +0000346}