Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1 | /* |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 2 | * flash_rom.c: Flash programming utility |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2000 Silicon Integrated System Corporation |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 5 | * Copyright 2004 Tyan Corp |
| 6 | * yhlu yhlu@tyan.com add exclude start and end option |
Stefan Reinauer | fcb6368 | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 7 | * 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 Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 11 | * |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 12 | * 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. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <sys/mman.h> |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include <sys/stat.h> |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 33 | #include <unistd.h> |
| 34 | #include <stdio.h> |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 35 | #include <string.h> |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 36 | #include <stdlib.h> |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 37 | #include <getopt.h> |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 38 | |
| 39 | #include "flash.h" |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 40 | #include "lbtable.h" |
| 41 | #include "layout.h" |
| 42 | #include "debug.h" |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 43 | |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 44 | char *chip_to_probe = NULL; |
Ronald G. Minnich | 7bd1dee | 2003-05-08 19:33:19 +0000 | [diff] [blame] | 45 | |
Stefan Reinauer | e370528 | 2005-12-18 16:41:10 +0000 | [diff] [blame] | 46 | int exclude_start_page, exclude_end_page; |
| 47 | int force=0, verbose=0; |
| 48 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 49 | struct flashchip *probe_flash(struct flashchip *flash) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 50 | { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 51 | int fd_mem; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 52 | volatile uint8_t *bios; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 53 | unsigned long size; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 54 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 55 | if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) { |
Stefan Reinauer | f8337dd | 2006-08-03 10:49:09 +0000 | [diff] [blame] | 56 | perror("Error: Can not open /dev/mem. You need to be root."); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 57 | exit(1); |
| 58 | } |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 59 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 60 | while (flash->name != NULL) { |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 61 | if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 62 | flash++; |
| 63 | continue; |
| 64 | } |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 65 | printf_debug("Trying %s, %d KB\n", flash->name, flash->total_size); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 66 | size = flash->total_size * 1024; |
| 67 | /* BUG? what happens if getpagesize() > size!? |
| 68 | -> ``Error MMAP /dev/mem: Invalid argument'' NIKI */ |
| 69 | if (getpagesize() > size) { |
| 70 | size = getpagesize(); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 71 | printf("%s: warning: size: %d -> %ld\n", |
| 72 | __FUNCTION__, flash->total_size * 1024, |
| 73 | (unsigned long) size); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 74 | } |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 75 | bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED, |
| 76 | fd_mem, (off_t) (0xffffffff - size + 1)); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 77 | if (bios == MAP_FAILED) { |
Stefan Reinauer | f8337dd | 2006-08-03 10:49:09 +0000 | [diff] [blame] | 78 | perror("Error: Can't mmap /dev/mem."); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 79 | exit(1); |
| 80 | } |
| 81 | flash->virt_addr = bios; |
| 82 | flash->fd_mem = fd_mem; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 83 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 84 | if (flash->probe(flash) == 1) { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 85 | printf("%s found at physical address: 0x%lx\n", |
| 86 | flash->name, (0xffffffff - size + 1)); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 87 | return flash; |
| 88 | } |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 89 | munmap((void *) bios, size); |
Stefan Reinauer | fcb6368 | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 90 | #ifdef TS5300 |
| 91 | /* TS-5300 */ |
| 92 | bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED, |
| 93 | fd_mem, (off_t) (0x9400000)); |
| 94 | if (bios == MAP_FAILED) { |
Stefan Reinauer | f8337dd | 2006-08-03 10:49:09 +0000 | [diff] [blame] | 95 | perror("Error: Can't mmap /dev/mem."); |
Stefan Reinauer | fcb6368 | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 96 | exit(1); |
| 97 | } |
| 98 | flash->virt_addr = bios; |
| 99 | flash->fd_mem = fd_mem; |
| 100 | |
| 101 | if (flash->probe(flash) == 1) { |
| 102 | printf("TS-5300 %s found at physical address: 0x%lx\n", |
| 103 | flash->name, 0x9400000UL); |
| 104 | return flash; |
| 105 | } |
| 106 | munmap((void *) bios, size); |
| 107 | /* TS-5300 */ |
| 108 | #endif |
| 109 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 110 | flash++; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 111 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 112 | return NULL; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Stefan Reinauer | e370528 | 2005-12-18 16:41:10 +0000 | [diff] [blame] | 115 | int verify_flash(struct flashchip *flash, uint8_t *buf) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 116 | { |
Stefan Reinauer | fcb6368 | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 117 | int idx; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 118 | int total_size = flash->total_size * 1024; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 119 | volatile uint8_t *bios = flash->virt_addr; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 120 | |
Stefan Reinauer | fcb6368 | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 121 | printf("Verifying flash "); |
| 122 | |
| 123 | if(verbose) printf("address: 0x00000000\b\b\b\b\b\b\b\b\b\b"); |
| 124 | |
| 125 | for (idx = 0; idx < total_size; idx++) { |
| 126 | if (verbose && ( (idx & 0xfff) == 0xfff )) |
| 127 | printf("0x%08x", idx); |
| 128 | |
| 129 | if (*(bios + idx) != *(buf + idx)) { |
| 130 | if (verbose) { |
| 131 | printf("0x%08x ", idx); |
| 132 | } |
| 133 | printf("- FAILED\n"); |
| 134 | return 1; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 135 | } |
Stefan Reinauer | fcb6368 | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 136 | |
| 137 | if (verbose && ( (idx & 0xfff) == 0xfff )) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 138 | printf("\b\b\b\b\b\b\b\b\b\b"); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 139 | } |
Stefan Reinauer | fcb6368 | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 140 | if (verbose) |
| 141 | printf("\b\b\b\b\b\b\b\b\b\b "); |
| 142 | |
| 143 | printf("- VERIFIED \n"); |
| 144 | return 0; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 147 | |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 148 | void usage(const char *name) |
| 149 | { |
Stefan Reinauer | f8337dd | 2006-08-03 10:49:09 +0000 | [diff] [blame] | 150 | printf("usage: %s [-rwvEVfh] [-c chipname] [-s exclude_start]\n", name); |
| 151 | printf(" [-e exclude_end] [-m vendor:part] [-l file.layout] [-i imagename] [file]\n"); |
| 152 | printf(" -r | --read: read flash and save into file\n" |
| 153 | " -w | --write: write file into flash (default when\n" |
| 154 | " file is specified)\n" |
| 155 | " -v | --verify: verify flash against file\n" |
| 156 | " -E | --erase: erase flash device\n" |
| 157 | " -V | --verbose: more verbose output\n" |
| 158 | " -c | --chip <chipname>: probe only for specified flash chip\n" |
| 159 | " -s | --estart <addr>: exclude start position\n" |
| 160 | " -e | --eend <addr>: exclude end postion\n" |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 161 | " -m | --mainboard <vendor:part>: override mainboard settings\n" |
Stefan Reinauer | f8337dd | 2006-08-03 10:49:09 +0000 | [diff] [blame] | 162 | " -f | --force: force write without checking image\n" |
| 163 | " -l | --layout <file.layout>: read rom layout from file\n" |
| 164 | " -i | --image <name>: only flash image name from flash layout\n" |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 165 | "\n" |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 166 | " If no file is specified, then all that happens\n" |
Stefan Reinauer | f8337dd | 2006-08-03 10:49:09 +0000 | [diff] [blame] | 167 | " is that flash info is dumped.\n\n"); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 168 | exit(1); |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 171 | int main(int argc, char *argv[]) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 172 | { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 173 | uint8_t *buf; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 174 | unsigned long size; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 175 | FILE *image; |
| 176 | struct flashchip *flash; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 177 | int opt; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 178 | int option_index = 0; |
| 179 | int read_it = 0, |
| 180 | write_it = 0, |
| 181 | erase_it = 0, |
| 182 | verify_it = 0; |
Stefan Reinauer | 143da0b | 2006-01-04 16:42:57 +0000 | [diff] [blame] | 183 | int ret = 0; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 184 | |
| 185 | static struct option long_options[]= { |
| 186 | { "read", 0, 0, 'r' }, |
| 187 | { "write", 0, 0, 'w' }, |
| 188 | { "erase", 0, 0, 'E' }, |
| 189 | { "verify", 0, 0, 'v' }, |
| 190 | { "chip", 1, 0, 'c' }, |
| 191 | { "estart", 1, 0, 's' }, |
| 192 | { "eend", 1, 0, 'e' }, |
| 193 | { "mainboard", 1, 0, 'm' }, |
| 194 | { "verbose", 0, 0, 'V' }, |
| 195 | { "force", 0, 0, 'f' }, |
| 196 | { "layout", 1, 0, 'l' }, |
| 197 | { "image", 1, 0, 'i' }, |
| 198 | { "help", 0, 0, 'h' }, |
| 199 | { 0, 0, 0, 0 } |
| 200 | }; |
| 201 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 202 | char *filename = NULL; |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 203 | |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 204 | |
| 205 | unsigned int exclude_start_position=0, exclude_end_position=0; // [x,y) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 206 | char *tempstr=NULL, *tempstr2=NULL; |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 207 | |
| 208 | if (argc > 1) { |
| 209 | /* Yes, print them. */ |
| 210 | int i; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 211 | printf_debug ("The arguments are:\n"); |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 212 | for (i = 1; i < argc; ++i) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 213 | printf_debug ("%s\n", argv[i]); |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 216 | setbuf(stdout, NULL); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 217 | while ((opt = getopt_long(argc, argv, "rwvVEfc:s:e:m:l:i:h", long_options, |
| 218 | &option_index)) != EOF) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 219 | switch (opt) { |
| 220 | case 'r': |
| 221 | read_it = 1; |
| 222 | break; |
| 223 | case 'w': |
| 224 | write_it = 1; |
| 225 | break; |
| 226 | case 'v': |
| 227 | verify_it = 1; |
| 228 | break; |
| 229 | case 'c': |
| 230 | chip_to_probe = strdup(optarg); |
| 231 | break; |
Ollie Lho | 789ad3d | 2004-03-18 20:27:33 +0000 | [diff] [blame] | 232 | case 'V': |
| 233 | verbose = 1; |
| 234 | break; |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 235 | case 'E': |
| 236 | erase_it = 1; |
| 237 | break; |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 238 | case 's': |
| 239 | tempstr = strdup(optarg); |
| 240 | sscanf(tempstr,"%x",&exclude_start_position); |
| 241 | break; |
| 242 | case 'e': |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 243 | tempstr = strdup(optarg); |
| 244 | sscanf(tempstr,"%x",&exclude_end_position); |
| 245 | break; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 246 | case 'm': |
| 247 | tempstr = strdup(optarg); |
| 248 | strtok(tempstr, ":"); |
| 249 | tempstr2=strtok(NULL, ":"); |
| 250 | if (tempstr2) { |
| 251 | lb_vendor=tempstr; |
| 252 | lb_part=tempstr2; |
| 253 | } else { |
| 254 | printf("warning: ignored wrong format of" |
| 255 | " mainboard: %s\n", tempstr); |
| 256 | } |
| 257 | break; |
| 258 | case 'f': |
| 259 | force=1; |
| 260 | break; |
| 261 | case 'l': |
| 262 | tempstr=strdup(optarg); |
| 263 | read_romlayout(tempstr); |
| 264 | break; |
| 265 | case 'i': |
| 266 | tempstr=strdup(optarg); |
| 267 | find_romentry(tempstr); |
| 268 | break; |
| 269 | case 'h': |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 270 | default: |
| 271 | usage(argv[0]); |
| 272 | break; |
| 273 | } |
| 274 | } |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 275 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 276 | if (read_it && write_it) { |
| 277 | printf("-r and -w are mutually exclusive\n"); |
| 278 | usage(argv[0]); |
| 279 | } |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 280 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 281 | if (optind < argc) |
| 282 | filename = argv[optind++]; |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 283 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 284 | printf("Calibrating delay loop... "); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 285 | myusec_calibrate_delay(); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 286 | printf("ok\n"); |
| 287 | |
| 288 | /* We look at the lbtable first to see if we need a |
| 289 | * mainboard specific flash enable sequence. |
| 290 | */ |
| 291 | linuxbios_init(); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 292 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 293 | /* try to enable it. Failure IS an option, since not all motherboards |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 294 | * really need this to be done, etc., etc. |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 295 | */ |
| 296 | (void) enable_flash_write(); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 297 | |
| 298 | if ((flash = probe_flash(flashchips)) == NULL) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 299 | printf("No EEPROM/flash device found.\n"); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 300 | exit(1); |
| 301 | } |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 302 | |
Uwe Hermann | d1915c1 | 2006-10-07 00:23:51 +0000 | [diff] [blame] | 303 | printf("Flash part is %s (%d KB)\n", flash->name, flash->total_size); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 304 | |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 305 | if (!filename && !erase_it) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 306 | // FIXME: Do we really want this feature implicitly? |
| 307 | printf("OK, only ENABLING flash write, but NOT FLASHING.\n"); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 308 | return 0; |
| 309 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 310 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 311 | size = flash->total_size * 1024; |
| 312 | buf = (uint8_t *) calloc(size, sizeof(char)); |
| 313 | |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 314 | if (erase_it) { |
| 315 | printf("Erasing flash chip\n"); |
| 316 | flash->erase(flash); |
| 317 | exit(0); |
| 318 | } else if (read_it) { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 319 | if ((image = fopen(filename, "w")) == NULL) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 320 | perror(filename); |
| 321 | exit(1); |
| 322 | } |
| 323 | printf("Reading Flash..."); |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 324 | if (flash->read == NULL) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 325 | memcpy(buf, (const char *) flash->virt_addr, size); |
| 326 | else |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 327 | flash->read(flash, buf); |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 328 | |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 329 | if (exclude_end_position - exclude_start_position > 0) |
| 330 | memset(buf+exclude_start_position, 0, |
| 331 | exclude_end_position-exclude_start_position); |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 332 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 333 | fwrite(buf, sizeof(char), size, image); |
| 334 | fclose(image); |
| 335 | printf("done\n"); |
| 336 | } else { |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 337 | struct stat image_stat; |
| 338 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 339 | if ((image = fopen(filename, "r")) == NULL) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 340 | perror(filename); |
| 341 | exit(1); |
| 342 | } |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 343 | if (fstat(fileno(image), &image_stat) != 0) { |
| 344 | perror(filename); |
| 345 | exit(1); |
| 346 | } |
| 347 | if(image_stat.st_size!=flash->total_size*1024) { |
| 348 | perror("Image size doesnt match"); |
| 349 | exit(1); |
| 350 | } |
| 351 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 352 | fread(buf, sizeof(char), size, image); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 353 | show_id(buf, size); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 354 | fclose(image); |
| 355 | } |
| 356 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 357 | /* exclude range stuff. Nice idea, but at the moment it is only |
| 358 | * supported in hardware by the pm49fl004 chips. |
| 359 | * Instead of implementing this for all chips I suggest advancing |
| 360 | * it to the rom layout feature below and drop exclude range |
| 361 | * completely once all flash chips can do rom layouts. stepan |
| 362 | */ |
| 363 | |
| 364 | // //////////////////////////////////////////////////////////// |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 365 | if (exclude_end_position - exclude_start_position > 0) |
Ollie Lho | d11f361 | 2004-12-07 17:19:04 +0000 | [diff] [blame] | 366 | memcpy(buf+exclude_start_position, |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 367 | (const char *) flash->virt_addr+exclude_start_position, |
| 368 | exclude_end_position-exclude_start_position); |
| 369 | |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 370 | exclude_start_page = exclude_start_position/flash->page_size; |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 371 | if ((exclude_start_position%flash->page_size) != 0) { |
| 372 | exclude_start_page++; |
| 373 | } |
| 374 | exclude_end_page = exclude_end_position/flash->page_size; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 375 | // //////////////////////////////////////////////////////////// |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 376 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 377 | // This should be moved into each flash part's code to do it |
| 378 | // cleanly. This does the job. |
| 379 | handle_romentries(buf, (uint8_t *)flash->virt_addr); |
| 380 | |
| 381 | // //////////////////////////////////////////////////////////// |
| 382 | |
| 383 | if (write_it) |
Stefan Reinauer | 143da0b | 2006-01-04 16:42:57 +0000 | [diff] [blame] | 384 | ret |= flash->write(flash, buf); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 385 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 386 | if (verify_it) |
Stefan Reinauer | 143da0b | 2006-01-04 16:42:57 +0000 | [diff] [blame] | 387 | ret |= verify_flash(flash, buf); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 388 | |
Stefan Reinauer | 143da0b | 2006-01-04 16:42:57 +0000 | [diff] [blame] | 389 | return ret; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 390 | } |