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