Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +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 |
Yinghai Lu | ad8ffd2 | 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 |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +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> |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | #include <stdio.h> |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 36 | #include <string.h> |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 37 | #include <stdlib.h> |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 38 | |
| 39 | #include "flash.h" |
| 40 | #include "jedec.h" |
Ronald G. Minnich | 3c910ed | 2002-05-28 23:29:17 +0000 | [diff] [blame] | 41 | #include "m29f400bt.h" |
Ronald G. Minnich | 5643942 | 2002-09-06 16:58:14 +0000 | [diff] [blame] | 42 | #include "82802ab.h" |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 43 | #include "msys_doc.h" |
| 44 | #include "am29f040b.h" |
| 45 | #include "sst28sf040.h" |
| 46 | #include "w49f002u.h" |
| 47 | #include "sst39sf020.h" |
David Hendricks | 3770a11 | 2004-03-17 21:47:30 +0000 | [diff] [blame] | 48 | #include "sst49lf040.h" |
Ronald G. Minnich | e3dad01 | 2004-02-10 21:34:18 +0000 | [diff] [blame] | 49 | #include "pm49fl004.h" |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 50 | #include "mx29f002.h" |
Ronald G. Minnich | 1f4d653 | 2004-09-30 16:37:01 +0000 | [diff] [blame] | 51 | #include "sst_fwhub.h" |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 52 | |
| 53 | struct flashchip flashchips[] = { |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 54 | {"Am29F040B", AMD_ID, AM_29F040B, NULL, 512, 64 * 1024, |
| 55 | probe_29f040b, erase_29f040b, write_29f040b, NULL}, |
| 56 | {"At29C040A", ATMEL_ID, AT_29C040A, NULL, 512, 256, |
| 57 | probe_jedec, erase_chip_jedec, write_jedec, NULL}, |
| 58 | {"Mx29f002", MX_ID, MX_29F002, NULL, 256, 64 * 1024, |
| 59 | probe_29f002, erase_29f002, write_29f002, NULL}, |
| 60 | {"SST29EE020A", SST_ID, SST_29EE020A, NULL, 256, 128, |
| 61 | probe_jedec, erase_chip_jedec, write_jedec, NULL}, |
| 62 | {"SST28SF040A", SST_ID, SST_28SF040, NULL, 512, 256, |
| 63 | probe_28sf040, erase_28sf040, write_28sf040, NULL}, |
| 64 | {"SST39SF020A", SST_ID, SST_39SF020, NULL, 256, 4096, |
| 65 | probe_jedec, erase_chip_jedec, write_39sf020,NULL}, |
| 66 | {"SST39VF020", SST_ID, SST_39VF020, NULL, 256, 4096, |
| 67 | probe_jedec, erase_chip_jedec, write_39sf020,NULL}, |
| 68 | {"SST49LF040", SST_ID, SST_49LF040, NULL, 512, 4096, |
Ollie Lho | a1439cf | 2005-01-11 02:48:22 +0000 | [diff] [blame^] | 69 | probe_jedec, erase_49lf040, write_49lf040,NULL}, |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 70 | {"SST49LF080A", SST_ID, SST_49LF080A, NULL, 1024, 4096, |
| 71 | probe_jedec, erase_chip_jedec, write_49lf040,NULL}, |
Ollie Lho | d11f361 | 2004-12-07 17:19:04 +0000 | [diff] [blame] | 72 | {"SST49LF002A/B", SST_ID, SST_49LF002A, NULL, 256, 16 * 1024, |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 73 | probe_sst_fwhub, erase_sst_fwhub, write_sst_fwhub, NULL}, |
Ollie Lho | d11f361 | 2004-12-07 17:19:04 +0000 | [diff] [blame] | 74 | {"SST49LF003A/B", SST_ID, SST_49LF003A, NULL, 384, 64 * 1024, |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 75 | probe_sst_fwhub, erase_sst_fwhub, write_sst_fwhub,NULL}, |
Ollie Lho | d11f361 | 2004-12-07 17:19:04 +0000 | [diff] [blame] | 76 | {"SST49LF004A/B", SST_ID, SST_49LF004A, NULL, 512, 64 * 1024, |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 77 | probe_sst_fwhub, erase_sst_fwhub, write_sst_fwhub,NULL}, |
Ollie Lho | d11f361 | 2004-12-07 17:19:04 +0000 | [diff] [blame] | 78 | {"SST49LF008A", SST_ID, SST_49LF008A, NULL, 1024, 64 * 1024 , |
Ronald G. Minnich | 1f4d653 | 2004-09-30 16:37:01 +0000 | [diff] [blame] | 79 | probe_sst_fwhub, erase_sst_fwhub, write_sst_fwhub, NULL}, |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 80 | {"Pm49FL004", PMC_ID, PMC_49FL004, NULL, 512, 64 * 1024, |
| 81 | probe_jedec, erase_chip_jedec, write_49fl004,NULL}, |
| 82 | {"W29C011", WINBOND_ID, W_29C011, NULL, 128, 128, |
| 83 | probe_jedec, erase_chip_jedec, write_jedec, NULL}, |
| 84 | {"W29C020C", WINBOND_ID, W_29C020C, NULL, 256, 128, |
| 85 | probe_jedec, erase_chip_jedec, write_jedec, NULL}, |
| 86 | {"W49F002U", WINBOND_ID, W_49F002U, NULL, 256, 128, |
| 87 | probe_jedec, erase_chip_jedec, write_49f002, NULL}, |
| 88 | {"M29F400BT", ST_ID, ST_M29F400BT, NULL, 512, 64 * 1024, |
| 89 | probe_m29f400bt, erase_m29f400bt, write_linuxbios_m29f400bt, NULL}, |
| 90 | {"82802ab", 137, 173, NULL, 512, 64 * 1024, |
| 91 | probe_82802ab, erase_82802ab, write_82802ab, NULL}, |
| 92 | {"82802ac", 137, 172, NULL, 1024, 64 * 1024, |
| 93 | probe_82802ab, erase_82802ab, write_82802ab, NULL}, |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 94 | {"MD-2802 (M-Systems DiskOnChip Millennium Module)", |
| 95 | MSYSTEMS_ID, MSYSTEMS_MD2802, |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 96 | NULL, 8, 8 * 1024, |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 97 | probe_md2802, erase_md2802, write_md2802, read_md2802}, |
| 98 | {NULL,} |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 101 | char *chip_to_probe = NULL; |
Ronald G. Minnich | 7bd1dee | 2003-05-08 19:33:19 +0000 | [diff] [blame] | 102 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 103 | struct flashchip *probe_flash(struct flashchip *flash) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 104 | { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 105 | int fd_mem; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 106 | volatile char *bios; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 107 | unsigned long size; |
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 ((fd_mem = open("/dev/mem", O_RDWR)) < 0) { |
| 110 | perror("Can not open /dev/mem"); |
| 111 | exit(1); |
| 112 | } |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 113 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 114 | while (flash->name != NULL) { |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 115 | if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 116 | flash++; |
| 117 | continue; |
| 118 | } |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 119 | printf("Trying %s, %d KB\n", flash->name, flash->total_size); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 120 | size = flash->total_size * 1024; |
| 121 | /* BUG? what happens if getpagesize() > size!? |
| 122 | -> ``Error MMAP /dev/mem: Invalid argument'' NIKI */ |
| 123 | if (getpagesize() > size) { |
| 124 | size = getpagesize(); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 125 | printf("%s: warning: size: %d -> %ld\n", |
| 126 | __FUNCTION__, flash->total_size * 1024, |
| 127 | (unsigned long) size); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 128 | } |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 129 | bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED, |
| 130 | fd_mem, (off_t) (0xffffffff - size + 1)); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 131 | if (bios == MAP_FAILED) { |
| 132 | perror("Error MMAP /dev/mem"); |
| 133 | exit(1); |
| 134 | } |
| 135 | flash->virt_addr = bios; |
| 136 | flash->fd_mem = fd_mem; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 137 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 138 | if (flash->probe(flash) == 1) { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 139 | printf("%s found at physical address: 0x%lx\n", |
| 140 | flash->name, (0xffffffff - size + 1)); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 141 | return flash; |
| 142 | } |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 143 | munmap((void *) bios, size); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 144 | flash++; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 145 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 146 | return NULL; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 149 | int verify_flash(struct flashchip *flash, char *buf, int verbose) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 150 | { |
Ollie Lho | f51a766 | 2004-03-20 17:05:01 +0000 | [diff] [blame] | 151 | int i; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 152 | int total_size = flash->total_size * 1024; |
| 153 | volatile char *bios = flash->virt_addr; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 154 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 155 | printf("Verifying address: "); |
Ollie Lho | f51a766 | 2004-03-20 17:05:01 +0000 | [diff] [blame] | 156 | for (i = 0; i < total_size; i++) { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 157 | if (verbose) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 158 | printf("0x%08x", i); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 159 | if (*(bios + i) != *(buf + i)) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 160 | printf("FAILED\n"); |
| 161 | return 0; |
| 162 | } |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 163 | if (verbose) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 164 | printf("\b\b\b\b\b\b\b\b\b\b"); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 165 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 166 | if (verbose) |
| 167 | printf("\n"); |
| 168 | else |
| 169 | printf("VERIFIED\n"); |
| 170 | return 1; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 173 | |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 174 | void usage(const char *name) |
| 175 | { |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 176 | printf("usage: %s [-rwv] [-c chipname] [-s exclude_start] [-e exclude_end] [file]\n", name); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 177 | printf("-r: read flash and save into file\n" |
| 178 | "-w: write file into flash (default when file is specified)\n" |
| 179 | "-v: verify flash against file\n" |
| 180 | "-c: probe only for specified flash chip\n" |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 181 | "-s: exclude start position\n" |
| 182 | "-e: exclude end postion\n" |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 183 | " If no file is specified, then all that happens\n" |
| 184 | " is that flash info is dumped\n"); |
| 185 | exit(1); |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 188 | int exclude_start_page, exclude_end_page; |
| 189 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 190 | int main(int argc, char *argv[]) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 191 | { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 192 | char *buf; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 193 | unsigned long size; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 194 | FILE *image; |
| 195 | struct flashchip *flash; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 196 | int opt; |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 197 | int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0, |
| 198 | verbose = 0; |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 199 | char *filename = NULL; |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 200 | |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 201 | |
| 202 | unsigned int exclude_start_position=0, exclude_end_position=0; // [x,y) |
| 203 | char *tempstr=NULL; |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 204 | |
| 205 | if (argc > 1) { |
| 206 | /* Yes, print them. */ |
| 207 | int i; |
| 208 | printf ("The arguments are:\n"); |
| 209 | for (i = 1; i < argc; ++i) |
| 210 | printf ("%s\n", argv[i]); |
| 211 | } |
| 212 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 213 | setbuf(stdout, NULL); |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 214 | |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 215 | while ((opt = getopt(argc, argv, "rwvVEc:s:e:")) != EOF) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 216 | switch (opt) { |
| 217 | case 'r': |
| 218 | read_it = 1; |
| 219 | break; |
| 220 | case 'w': |
| 221 | write_it = 1; |
| 222 | break; |
| 223 | case 'v': |
| 224 | verify_it = 1; |
| 225 | break; |
| 226 | case 'c': |
| 227 | chip_to_probe = strdup(optarg); |
| 228 | break; |
Ollie Lho | 789ad3d | 2004-03-18 20:27:33 +0000 | [diff] [blame] | 229 | case 'V': |
| 230 | verbose = 1; |
| 231 | break; |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 232 | case 'E': |
| 233 | erase_it = 1; |
| 234 | break; |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 235 | case 's': |
| 236 | tempstr = strdup(optarg); |
| 237 | sscanf(tempstr,"%x",&exclude_start_position); |
| 238 | break; |
| 239 | case 'e': |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 240 | tempstr = strdup(optarg); |
| 241 | sscanf(tempstr,"%x",&exclude_end_position); |
| 242 | break; |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 243 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 244 | default: |
| 245 | usage(argv[0]); |
| 246 | break; |
| 247 | } |
| 248 | } |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 249 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 250 | if (read_it && write_it) { |
| 251 | printf("-r and -w are mutually exclusive\n"); |
| 252 | usage(argv[0]); |
| 253 | } |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 254 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 255 | if (optind < argc) |
| 256 | filename = argv[optind++]; |
Ronald G. Minnich | e555957 | 2003-03-28 03:29:43 +0000 | [diff] [blame] | 257 | |
Ollie Lho | 8b8897a | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 258 | printf("Calibrating timer since microsleep sucks ... takes a second\n"); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 259 | myusec_calibrate_delay(); |
| 260 | printf("OK, calibrated, now do the deed\n"); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 261 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 262 | /* try to enable it. Failure IS an option, since not all motherboards |
| 263 | * really need this to be done, etc., etc. It sucks. |
| 264 | */ |
| 265 | (void) enable_flash_write(); |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 266 | |
| 267 | if ((flash = probe_flash(flashchips)) == NULL) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 268 | printf("EEPROM not found\n"); |
| 269 | exit(1); |
| 270 | } |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 271 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 272 | printf("Part is %s\n", flash->name); |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 273 | if (!filename && !erase_it) { |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 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 | } |
| 277 | size = flash->total_size * 1024; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 278 | buf = (char *) calloc(size, sizeof(char)); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 279 | |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 280 | if (erase_it) { |
| 281 | printf("Erasing flash chip\n"); |
| 282 | flash->erase(flash); |
| 283 | exit(0); |
| 284 | } else if (read_it) { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 285 | if ((image = fopen(filename, "w")) == NULL) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 286 | perror(filename); |
| 287 | exit(1); |
| 288 | } |
| 289 | printf("Reading Flash..."); |
Ollie Lho | 73eca80 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 290 | if (flash->read == NULL) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 291 | memcpy(buf, (const char *) flash->virt_addr, size); |
| 292 | else |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 293 | flash->read(flash, buf); |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 294 | |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 295 | if (exclude_end_position - exclude_start_position > 0) |
| 296 | memset(buf+exclude_start_position, 0, |
| 297 | exclude_end_position-exclude_start_position); |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 298 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 299 | fwrite(buf, sizeof(char), size, image); |
| 300 | fclose(image); |
| 301 | printf("done\n"); |
| 302 | } else { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 303 | if ((image = fopen(filename, "r")) == NULL) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 304 | perror(filename); |
| 305 | exit(1); |
| 306 | } |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 307 | fread(buf, sizeof(char), size, image); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 308 | fclose(image); |
| 309 | } |
| 310 | |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 311 | if (exclude_end_position - exclude_start_position > 0) |
Ollie Lho | d11f361 | 2004-12-07 17:19:04 +0000 | [diff] [blame] | 312 | memcpy(buf+exclude_start_position, |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 313 | (const char *) flash->virt_addr+exclude_start_position, |
| 314 | exclude_end_position-exclude_start_position); |
| 315 | |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 316 | exclude_start_page = exclude_start_position/flash->page_size; |
Ollie Lho | 98bea8a | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 317 | if ((exclude_start_position%flash->page_size) != 0) { |
| 318 | exclude_start_page++; |
| 319 | } |
| 320 | exclude_end_page = exclude_end_position/flash->page_size; |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 321 | |
| 322 | if (write_it || (!read_it && !verify_it)) { |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 323 | flash->write(flash, buf); |
Yinghai Lu | ad8ffd2 | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 324 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 325 | if (verify_it) |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 326 | verify_flash(flash, buf, verbose); |
Ronald G. Minnich | c73ad98 | 2003-02-11 21:06:09 +0000 | [diff] [blame] | 327 | return 0; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 328 | } |