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 |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 7 | * 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. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 11 | * 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. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 25 | */ |
| 26 | |
| 27 | #include <errno.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <sys/mman.h> |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 30 | #include <unistd.h> |
| 31 | #include <stdio.h> |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 32 | #include <string.h> |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 33 | #include <stdlib.h> |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 34 | #include <getopt.h> |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 35 | |
| 36 | #include "flash.h" |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 37 | #include "lbtable.h" |
| 38 | #include "layout.h" |
| 39 | #include "debug.h" |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 40 | |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 41 | char *chip_to_probe = NULL; |
Ronald G. Minnich | 7bd1dee | 2003-05-08 19:33:19 +0000 | [diff] [blame] | 42 | |
Stefan Reinauer | e370528 | 2005-12-18 16:41:10 +0000 | [diff] [blame] | 43 | int exclude_start_page, exclude_end_page; |
| 44 | int force=0, verbose=0; |
| 45 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 46 | struct flashchip *probe_flash(struct flashchip *flash) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 47 | { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 48 | int fd_mem; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 49 | volatile uint8_t *bios; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 50 | unsigned long size; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 51 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 52 | if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) { |
| 53 | perror("Can not open /dev/mem"); |
| 54 | exit(1); |
| 55 | } |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 56 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 57 | while (flash->name != NULL) { |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 58 | if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 59 | flash++; |
| 60 | continue; |
| 61 | } |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 62 | printf_debug("Trying %s, %d KB\n", flash->name, flash->total_size); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 63 | 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 Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 68 | printf("%s: warning: size: %d -> %ld\n", |
| 69 | __FUNCTION__, flash->total_size * 1024, |
| 70 | (unsigned long) size); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 71 | } |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 72 | bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED, |
| 73 | fd_mem, (off_t) (0xffffffff - size + 1)); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 74 | 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. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 80 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 81 | if (flash->probe(flash) == 1) { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 82 | printf("%s found at physical address: 0x%lx\n", |
| 83 | flash->name, (0xffffffff - size + 1)); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 84 | return flash; |
| 85 | } |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 86 | munmap((void *) bios, size); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 87 | flash++; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 88 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 89 | return NULL; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Stefan Reinauer | e370528 | 2005-12-18 16:41:10 +0000 | [diff] [blame] | 92 | int verify_flash(struct flashchip *flash, uint8_t *buf) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 93 | { |
Ollie Lho | f51a766 | 2004-03-20 17:05:01 +0000 | [diff] [blame] | 94 | int i; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 95 | int total_size = flash->total_size * 1024; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 96 | volatile uint8_t *bios = flash->virt_addr; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 97 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 98 | printf("Verifying address: "); |
Ollie Lho | f51a766 | 2004-03-20 17:05:01 +0000 | [diff] [blame] | 99 | for (i = 0; i < total_size; i++) { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 100 | if (verbose) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 101 | printf("0x%08x", i); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 102 | if (*(bios + i) != *(buf + i)) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 103 | printf("FAILED\n"); |
| 104 | return 0; |
| 105 | } |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 106 | if (verbose) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 107 | printf("\b\b\b\b\b\b\b\b\b\b"); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 108 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 109 | if (verbose) |
| 110 | printf("\n"); |
| 111 | else |
| 112 | printf("VERIFIED\n"); |
| 113 | return 1; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 116 | |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 117 | void usage(const char *name) |
| 118 | { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 119 | 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 Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 133 | " If no file is specified, then all that happens\n" |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 134 | " is that flash info is dumped\n\n"); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 135 | exit(1); |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 138 | int main(int argc, char *argv[]) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 139 | { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 140 | uint8_t *buf; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 141 | unsigned long size; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 142 | FILE *image; |
| 143 | struct flashchip *flash; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 144 | int opt; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 145 | int option_index = 0; |
| 146 | int read_it = 0, |
| 147 | write_it = 0, |
| 148 | erase_it = 0, |
| 149 | verify_it = 0; |
Stefan Reinauer | 143da0b | 2006-01-04 16:42:57 +0000 | [diff] [blame] | 150 | int ret = 0; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 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 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 169 | char *filename = NULL; |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 170 | |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 171 | |
| 172 | unsigned int exclude_start_position=0, exclude_end_position=0; // [x,y) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 173 | char *tempstr=NULL, *tempstr2=NULL; |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 174 | |
| 175 | if (argc > 1) { |
| 176 | /* Yes, print them. */ |
| 177 | int i; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 178 | printf_debug ("The arguments are:\n"); |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 179 | for (i = 1; i < argc; ++i) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 180 | printf_debug ("%s\n", argv[i]); |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 183 | setbuf(stdout, NULL); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 184 | while ((opt = getopt_long(argc, argv, "rwvVEfc:s:e:m:l:i:h", long_options, |
| 185 | &option_index)) != EOF) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 186 | 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; |
Ollie Lho | 789ad3d | 2004-03-18 20:27:33 +0000 | [diff] [blame] | 199 | case 'V': |
| 200 | verbose = 1; |
| 201 | break; |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 202 | case 'E': |
| 203 | erase_it = 1; |
| 204 | break; |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 205 | case 's': |
| 206 | tempstr = strdup(optarg); |
| 207 | sscanf(tempstr,"%x",&exclude_start_position); |
| 208 | break; |
| 209 | case 'e': |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 210 | tempstr = strdup(optarg); |
| 211 | sscanf(tempstr,"%x",&exclude_end_position); |
| 212 | break; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 213 | 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': |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 237 | default: |
| 238 | usage(argv[0]); |
| 239 | break; |
| 240 | } |
| 241 | } |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 242 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 243 | if (read_it && write_it) { |
| 244 | printf("-r and -w are mutually exclusive\n"); |
| 245 | usage(argv[0]); |
| 246 | } |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 247 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 248 | if (optind < argc) |
| 249 | filename = argv[optind++]; |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 250 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 251 | printf("Calibrating delay loop... "); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 252 | myusec_calibrate_delay(); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 253 | 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(); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 259 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 260 | /* try to enable it. Failure IS an option, since not all motherboards |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 261 | * really need this to be done, etc., etc. |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 262 | */ |
| 263 | (void) enable_flash_write(); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 264 | |
| 265 | if ((flash = probe_flash(flashchips)) == NULL) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 266 | printf("No EEPROM/flash device found.\n"); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 267 | exit(1); |
| 268 | } |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 269 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 270 | printf("Flash part is %s\n", flash->name); |
| 271 | |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 272 | if (!filename && !erase_it) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 273 | // FIXME: Do we really want this feature implicitly? |
| 274 | printf("OK, only ENABLING flash write, but NOT FLASHING.\n"); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 275 | return 0; |
| 276 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 277 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 278 | size = flash->total_size * 1024; |
| 279 | buf = (uint8_t *) calloc(size, sizeof(char)); |
| 280 | |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 281 | if (erase_it) { |
| 282 | printf("Erasing flash chip\n"); |
| 283 | flash->erase(flash); |
| 284 | exit(0); |
| 285 | } else if (read_it) { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 286 | if ((image = fopen(filename, "w")) == NULL) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 287 | perror(filename); |
| 288 | exit(1); |
| 289 | } |
| 290 | printf("Reading Flash..."); |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 291 | if (flash->read == NULL) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 292 | memcpy(buf, (const char *) flash->virt_addr, size); |
| 293 | else |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 294 | flash->read(flash, buf); |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 295 | |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 296 | if (exclude_end_position - exclude_start_position > 0) |
| 297 | memset(buf+exclude_start_position, 0, |
| 298 | exclude_end_position-exclude_start_position); |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 299 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 300 | fwrite(buf, sizeof(char), size, image); |
| 301 | fclose(image); |
| 302 | printf("done\n"); |
| 303 | } else { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 304 | if ((image = fopen(filename, "r")) == NULL) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 305 | perror(filename); |
| 306 | exit(1); |
| 307 | } |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 308 | fread(buf, sizeof(char), size, image); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 309 | show_id(buf, size); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 310 | fclose(image); |
| 311 | } |
| 312 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 313 | /* 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 | // //////////////////////////////////////////////////////////// |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 321 | if (exclude_end_position - exclude_start_position > 0) |
Ollie Lho | d11f361 | 2004-12-07 17:19:04 +0000 | [diff] [blame] | 322 | memcpy(buf+exclude_start_position, |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 323 | (const char *) flash->virt_addr+exclude_start_position, |
| 324 | exclude_end_position-exclude_start_position); |
| 325 | |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 326 | exclude_start_page = exclude_start_position/flash->page_size; |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 327 | if ((exclude_start_position%flash->page_size) != 0) { |
| 328 | exclude_start_page++; |
| 329 | } |
| 330 | exclude_end_page = exclude_end_position/flash->page_size; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 331 | // //////////////////////////////////////////////////////////// |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 332 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 333 | // 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) |
Stefan Reinauer | 143da0b | 2006-01-04 16:42:57 +0000 | [diff] [blame] | 340 | ret |= flash->write(flash, buf); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 341 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 342 | if (verify_it) |
Stefan Reinauer | 143da0b | 2006-01-04 16:42:57 +0000 | [diff] [blame] | 343 | ret |= verify_flash(flash, buf); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 344 | |
Stefan Reinauer | 143da0b | 2006-01-04 16:42:57 +0000 | [diff] [blame] | 345 | return ret; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 346 | } |