rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * flash_rom.c: Flash programming utility for SiS 630/950 M/Bs |
| 3 | * |
| 4 | * |
| 5 | * Copyright 2000 Silicon Integrated System Corporation |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame^] | 6 | * Copyright 2004 Tyan Corp |
| 7 | * yhlu yhlu@tyan.com add exclude start and end option |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | * |
| 23 | * |
| 24 | * Reference: |
| 25 | * 1. SiS 630 Specification |
| 26 | * 2. SiS 950 Specification |
| 27 | * |
| 28 | * $Id$ |
| 29 | */ |
| 30 | |
| 31 | #include <errno.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <sys/mman.h> |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | #include <stdio.h> |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 36 | #include <string.h> |
| 37 | #include <stdlib.h> |
| 38 | |
| 39 | #include "flash.h" |
| 40 | #include "jedec.h" |
| 41 | #include "m29f400bt.h" |
| 42 | #include "82802ab.h" |
| 43 | #include "msys_doc.h" |
| 44 | #include "am29f040b.h" |
| 45 | #include "sst28sf040.h" |
| 46 | #include "w49f002u.h" |
| 47 | #include "sst39sf020.h" |
dhendricks | 81524b7 | 2004-03-17 21:47:30 +0000 | [diff] [blame] | 48 | #include "sst49lf040.h" |
rminnich | be1ace1 | 2004-02-10 21:34:18 +0000 | [diff] [blame] | 49 | #include "pm49fl004.h" |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 50 | #include "mx29f002.h" |
rminnich | dfcbaa7 | 2004-09-30 16:37:01 +0000 | [diff] [blame] | 51 | #include "sst_fwhub.h" |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 52 | |
| 53 | struct flashchip flashchips[] = { |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame^] | 54 | #if 1 |
ollie | f1845bd | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 55 | {"Am29F040B", AMD_ID, AM_29F040B, NULL, 512, 64 * 1024, |
| 56 | probe_29f040b, erase_29f040b, write_29f040b, NULL}, |
| 57 | {"At29C040A", ATMEL_ID, AT_29C040A, NULL, 512, 256, |
| 58 | probe_jedec, erase_chip_jedec, write_jedec, NULL}, |
| 59 | {"Mx29f002", MX_ID, MX_29F002, NULL, 256, 64 * 1024, |
| 60 | probe_29f002, erase_29f002, write_29f002, NULL}, |
| 61 | {"SST29EE020A", SST_ID, SST_29EE020A, NULL, 256, 128, |
| 62 | probe_jedec, erase_chip_jedec, write_jedec, NULL}, |
| 63 | {"SST28SF040A", SST_ID, SST_28SF040, NULL, 512, 256, |
| 64 | probe_28sf040, erase_28sf040, write_28sf040, NULL}, |
| 65 | {"SST39SF020A", SST_ID, SST_39SF020, NULL, 256, 4096, |
| 66 | probe_jedec, erase_chip_jedec, write_39sf020,NULL}, |
| 67 | {"SST39VF020", SST_ID, SST_39VF020, NULL, 256, 4096, |
| 68 | probe_jedec, erase_chip_jedec, write_39sf020,NULL}, |
| 69 | {"SST49LF040", SST_ID, SST_49LF040, NULL, 512, 4096, |
| 70 | probe_jedec, erase_chip_jedec, write_49lf040,NULL}, |
| 71 | {"SST49LF080A", SST_ID, SST_49LF080A, NULL, 1024, 4096, |
| 72 | probe_jedec, erase_chip_jedec, write_49lf040,NULL}, |
| 73 | {"SST49LF002A", SST_ID, SST_49LF002A, NULL, 256, 4096, |
| 74 | probe_jedec, erase_chip_jedec, write_49lf040,NULL}, |
| 75 | {"SST49LF003A", SST_ID, SST_49LF003A, NULL, 384, 4096, |
| 76 | probe_jedec, erase_chip_jedec, write_49lf040,NULL}, |
| 77 | {"SST49LF004A", SST_ID, SST_49LF004A, NULL, 512, 4096, |
| 78 | probe_jedec, erase_chip_jedec, write_49lf040,NULL}, |
| 79 | {"SST49LF008A", SST_ID, SST_49LF008A, NULL, 1024, 4096, |
rminnich | dfcbaa7 | 2004-09-30 16:37:01 +0000 | [diff] [blame] | 80 | probe_sst_fwhub, erase_sst_fwhub, write_sst_fwhub, NULL}, |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame^] | 81 | #endif |
ollie | f1845bd | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 82 | {"Pm49FL004", PMC_ID, PMC_49FL004, NULL, 512, 64 * 1024, |
| 83 | probe_jedec, erase_chip_jedec, write_49fl004,NULL}, |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame^] | 84 | #if 1 |
ollie | f1845bd | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 85 | {"W29C011", WINBOND_ID, W_29C011, NULL, 128, 128, |
| 86 | probe_jedec, erase_chip_jedec, write_jedec, NULL}, |
| 87 | {"W29C020C", WINBOND_ID, W_29C020C, NULL, 256, 128, |
| 88 | probe_jedec, erase_chip_jedec, write_jedec, NULL}, |
| 89 | {"W49F002U", WINBOND_ID, W_49F002U, NULL, 256, 128, |
| 90 | probe_jedec, erase_chip_jedec, write_49f002, NULL}, |
| 91 | {"M29F400BT", ST_ID, ST_M29F400BT, NULL, 512, 64 * 1024, |
| 92 | probe_m29f400bt, erase_m29f400bt, write_linuxbios_m29f400bt, NULL}, |
| 93 | {"82802ab", 137, 173, NULL, 512, 64 * 1024, |
| 94 | probe_82802ab, erase_82802ab, write_82802ab, NULL}, |
| 95 | {"82802ac", 137, 172, NULL, 1024, 64 * 1024, |
| 96 | probe_82802ab, erase_82802ab, write_82802ab, NULL}, |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 97 | {"MD-2802 (M-Systems DiskOnChip Millennium Module)", |
| 98 | MSYSTEMS_ID, MSYSTEMS_MD2802, |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 99 | NULL, 8, 8 * 1024, |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 100 | probe_md2802, erase_md2802, write_md2802, read_md2802}, |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame^] | 101 | #endif |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 102 | {NULL,} |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | char *chip_to_probe = NULL; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 106 | |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 107 | struct flashchip *probe_flash(struct flashchip *flash) |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 108 | { |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 109 | int fd_mem; |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 110 | volatile char *bios; |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 111 | unsigned long size; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 112 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 113 | if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) { |
| 114 | perror("Can not open /dev/mem"); |
| 115 | exit(1); |
| 116 | } |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 117 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 118 | while (flash->name != NULL) { |
ollie | f1845bd | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 119 | if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) { |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 120 | flash++; |
| 121 | continue; |
| 122 | } |
ollie | f1845bd | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 123 | printf("Trying %s, %d KB\n", flash->name, flash->total_size); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 124 | size = flash->total_size * 1024; |
| 125 | /* BUG? what happens if getpagesize() > size!? |
| 126 | -> ``Error MMAP /dev/mem: Invalid argument'' NIKI */ |
| 127 | if (getpagesize() > size) { |
| 128 | size = getpagesize(); |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 129 | printf("%s: warning: size: %d -> %ld\n", |
| 130 | __FUNCTION__, flash->total_size * 1024, |
| 131 | (unsigned long) size); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 132 | } |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 133 | bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED, |
| 134 | fd_mem, (off_t) (0xffffffff - size + 1)); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 135 | if (bios == MAP_FAILED) { |
| 136 | perror("Error MMAP /dev/mem"); |
| 137 | exit(1); |
| 138 | } |
| 139 | flash->virt_addr = bios; |
| 140 | flash->fd_mem = fd_mem; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 141 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 142 | if (flash->probe(flash) == 1) { |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 143 | printf("%s found at physical address: 0x%lx\n", |
| 144 | flash->name, (0xffffffff - size + 1)); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 145 | return flash; |
| 146 | } |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 147 | munmap((void *) bios, size); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 148 | flash++; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 149 | } |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 150 | return NULL; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 151 | } |
| 152 | |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 153 | int verify_flash(struct flashchip *flash, char *buf, int verbose) |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 154 | { |
ollie | 4df7da4 | 2004-03-20 17:05:01 +0000 | [diff] [blame] | 155 | int i; |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 156 | int total_size = flash->total_size * 1024; |
| 157 | volatile char *bios = flash->virt_addr; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 158 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 159 | printf("Verifying address: "); |
ollie | 4df7da4 | 2004-03-20 17:05:01 +0000 | [diff] [blame] | 160 | for (i = 0; i < total_size; i++) { |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 161 | if (verbose) |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 162 | printf("0x%08x", i); |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 163 | if (*(bios + i) != *(buf + i)) { |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 164 | printf("FAILED\n"); |
| 165 | return 0; |
| 166 | } |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 167 | if (verbose) |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 168 | printf("\b\b\b\b\b\b\b\b\b\b"); |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 169 | } |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 170 | if (verbose) |
| 171 | printf("\n"); |
| 172 | else |
| 173 | printf("VERIFIED\n"); |
| 174 | return 1; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 175 | } |
| 176 | |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 177 | |
| 178 | void usage(const char *name) |
| 179 | { |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame^] | 180 | printf("usage: %s [-rwv] [-c chipname] [-s exclude_start] [-e exclude_end] [file]\n", name); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 181 | printf("-r: read flash and save into file\n" |
| 182 | "-w: write file into flash (default when file is specified)\n" |
| 183 | "-v: verify flash against file\n" |
| 184 | "-c: probe only for specified flash chip\n" |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame^] | 185 | "-s: exclude start position\n" |
| 186 | "-e: exclude end postion\n" |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 187 | " If no file is specified, then all that happens\n" |
| 188 | " is that flash info is dumped\n"); |
| 189 | exit(1); |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 190 | } |
| 191 | |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame^] | 192 | int exclude_start_page, exclude_end_page; |
| 193 | |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 194 | int main(int argc, char *argv[]) |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 195 | { |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 196 | char *buf; |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 197 | unsigned long size; |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 198 | FILE *image; |
| 199 | struct flashchip *flash; |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 200 | int opt; |
ollie | 077017b | 2004-03-18 20:27:33 +0000 | [diff] [blame] | 201 | int read_it = 0, write_it = 0, verify_it = 0, verbose = 0; |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 202 | char *filename = NULL; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 203 | |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame^] | 204 | |
| 205 | unsigned int exclude_start_position=0, exclude_end_position=0; // [x,y) |
| 206 | char *tempstr=NULL; |
| 207 | #if 0 |
| 208 | |
| 209 | #if 1 |
| 210 | /* Keep fallback image */ |
| 211 | exclude_start_position = 0x60000; |
| 212 | exclude_end_position = 0x80000; |
| 213 | #else |
| 214 | /* Keep DMI etc. */ |
| 215 | exclude_start_position = 0x60000; |
| 216 | exclude_end_position = 0x70000; |
| 217 | #endif |
| 218 | |
| 219 | #endif |
| 220 | |
| 221 | if (argc > 1) { |
| 222 | /* Yes, print them. */ |
| 223 | int i; |
| 224 | printf ("The arguments are:\n"); |
| 225 | for (i = 1; i < argc; ++i) |
| 226 | printf ("%s\n", argv[i]); |
| 227 | } |
| 228 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 229 | setbuf(stdout, NULL); |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 230 | |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame^] | 231 | while ((opt = getopt(argc, argv, "rwvVc:s:e:")) != EOF) { |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 232 | switch (opt) { |
| 233 | case 'r': |
| 234 | read_it = 1; |
| 235 | break; |
| 236 | case 'w': |
| 237 | write_it = 1; |
| 238 | break; |
| 239 | case 'v': |
| 240 | verify_it = 1; |
| 241 | break; |
| 242 | case 'c': |
| 243 | chip_to_probe = strdup(optarg); |
| 244 | break; |
ollie | 077017b | 2004-03-18 20:27:33 +0000 | [diff] [blame] | 245 | case 'V': |
| 246 | verbose = 1; |
| 247 | break; |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame^] | 248 | case 's': |
| 249 | tempstr = strdup(optarg); |
| 250 | sscanf(tempstr,"%x",&exclude_start_position); |
| 251 | break; |
| 252 | case 'e': |
| 253 | tempstr = strdup(optarg); |
| 254 | sscanf(tempstr,"%x",&exclude_end_position); |
| 255 | break; |
| 256 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 257 | default: |
| 258 | usage(argv[0]); |
| 259 | break; |
| 260 | } |
| 261 | } |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame^] | 262 | |
| 263 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 264 | if (read_it && write_it) { |
| 265 | printf("-r and -w are mutually exclusive\n"); |
| 266 | usage(argv[0]); |
| 267 | } |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 268 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 269 | if (optind < argc) |
| 270 | filename = argv[optind++]; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 271 | |
ollie | f1845bd | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 272 | printf("Calibrating timer since microsleep sucks ... takes a second\n"); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 273 | myusec_calibrate_delay(); |
| 274 | printf("OK, calibrated, now do the deed\n"); |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 275 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 276 | /* try to enable it. Failure IS an option, since not all motherboards |
| 277 | * really need this to be done, etc., etc. It sucks. |
| 278 | */ |
| 279 | (void) enable_flash_write(); |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 280 | |
| 281 | if ((flash = probe_flash(flashchips)) == NULL) { |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 282 | printf("EEPROM not found\n"); |
| 283 | exit(1); |
| 284 | } |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 285 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 286 | printf("Part is %s\n", flash->name); |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 287 | if (!filename) { |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame^] | 288 | printf |
| 289 | ("OK, only ENABLING flash write, but NOT FLASHING\n"); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 290 | return 0; |
| 291 | } |
| 292 | size = flash->total_size * 1024; |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 293 | buf = (char *) calloc(size, sizeof(char)); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 294 | |
| 295 | if (read_it) { |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 296 | if ((image = fopen(filename, "w")) == NULL) { |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 297 | perror(filename); |
| 298 | exit(1); |
| 299 | } |
| 300 | printf("Reading Flash..."); |
ollie | a3def63 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 301 | if (flash->read == NULL) |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 302 | memcpy(buf, (const char *) flash->virt_addr, size); |
| 303 | else |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 304 | flash->read(flash, buf); |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame^] | 305 | |
| 306 | if(exclude_end_position - exclude_start_position > 0) |
| 307 | memset(buf+exclude_start_position, 0, exclude_end_position-exclude_start_position); |
| 308 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 309 | fwrite(buf, sizeof(char), size, image); |
| 310 | fclose(image); |
| 311 | printf("done\n"); |
| 312 | } else { |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 313 | if ((image = fopen(filename, "r")) == NULL) { |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 314 | perror(filename); |
| 315 | exit(1); |
| 316 | } |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 317 | fread(buf, sizeof(char), size, image); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 318 | fclose(image); |
| 319 | } |
| 320 | |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame^] | 321 | if(exclude_end_position - exclude_start_position > 0) |
| 322 | memcpy(buf+exclude_start_position, (const char *) flash->virt_addr+exclude_start_position, |
| 323 | exclude_end_position-exclude_start_position); |
| 324 | |
| 325 | exclude_start_page = exclude_start_position/flash->page_size; |
| 326 | if((exclude_start_position%flash->page_size) != 0) { |
| 327 | exclude_start_page++; |
| 328 | } |
| 329 | exclude_end_page = exclude_end_position/flash->page_size; |
| 330 | |
| 331 | if (write_it || (!read_it && !verify_it)) { |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 332 | flash->write(flash, buf); |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame^] | 333 | } |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 334 | if (verify_it) |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 335 | verify_flash(flash, buf, verbose); |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 336 | return 0; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 337 | } |