rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 1 | /* |
stepan | e9134fb | 2007-04-11 23:31:45 +0000 | [diff] [blame] | 2 | * flashrom.c: Flash programming utility |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2000 Silicon Integrated System Corporation |
yhlu | 5d4383a | 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 |
stepan | 782fb17 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 7 | * Copyright 2005-2007 coresystems GmbH |
stepan | c98b80b | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 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 | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 11 | * |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +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 | * |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
| 30 | #include <sys/mman.h> |
stepan | 1da96c0 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include <sys/stat.h> |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 33 | #include <unistd.h> |
| 34 | #include <stdio.h> |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 35 | #include <string.h> |
| 36 | #include <stdlib.h> |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 37 | #include <getopt.h> |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 38 | #include <pci/pci.h> |
| 39 | |
| 40 | /* for iopl */ |
| 41 | #if defined (__sun) && (defined(__i386) || defined(__amd64)) |
| 42 | #include <strings.h> |
| 43 | #include <sys/sysi86.h> |
| 44 | #include <sys/psw.h> |
| 45 | #include <asm/sunddi.h> |
| 46 | #endif |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 47 | |
| 48 | #include "flash.h" |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 49 | #include "lbtable.h" |
| 50 | #include "layout.h" |
| 51 | #include "debug.h" |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 52 | |
| 53 | char *chip_to_probe = NULL; |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 54 | struct pci_access *pacc; /* For board and chipset_enable */ |
stepan | 193c9c2 | 2005-12-18 16:41:10 +0000 | [diff] [blame] | 55 | int exclude_start_page, exclude_end_page; |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 56 | int force = 0, verbose = 0; |
stepan | 193c9c2 | 2005-12-18 16:41:10 +0000 | [diff] [blame] | 57 | |
stepan | 782fb17 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 58 | int fd_mem; |
| 59 | |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 60 | /* |
| 61 | * |
| 62 | */ |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 63 | struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device) |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 64 | { |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 65 | struct pci_dev *temp; |
| 66 | struct pci_filter filter; |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 67 | |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 68 | pci_filter_init(NULL, &filter); |
| 69 | filter.vendor = vendor; |
| 70 | filter.device = device; |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 71 | |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 72 | for (temp = pacc->devices; temp; temp = temp->next) |
| 73 | if (pci_filter_match(&filter, temp)) |
| 74 | return temp; |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 75 | |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 76 | return NULL; |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /* |
| 80 | * |
| 81 | */ |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 82 | struct pci_dev *pci_card_find(uint16_t vendor, uint16_t device, |
| 83 | uint16_t card_vendor, uint16_t card_device) |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 84 | { |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 85 | struct pci_dev *temp; |
| 86 | struct pci_filter filter; |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 87 | |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 88 | pci_filter_init(NULL, &filter); |
| 89 | filter.vendor = vendor; |
| 90 | filter.device = device; |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 91 | |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 92 | for (temp = pacc->devices; temp; temp = temp->next) |
| 93 | if (pci_filter_match(&filter, temp)) { |
| 94 | if ((card_vendor == pci_read_word(temp, 0x2C)) && |
| 95 | (card_device == pci_read_word(temp, 0x2E))) |
| 96 | return temp; |
| 97 | } |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 98 | |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 99 | return NULL; |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 100 | } |
| 101 | |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 102 | struct flashchip *probe_flash(struct flashchip *flash) |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 103 | { |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 104 | volatile uint8_t *bios; |
stepan | 782fb17 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 105 | unsigned long flash_baseaddr, size; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 106 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 107 | while (flash->name != NULL) { |
ollie | f1845bd | 2004-03-27 00:18:15 +0000 | [diff] [blame] | 108 | if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) { |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 109 | flash++; |
| 110 | continue; |
| 111 | } |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 112 | printf_debug("Probing for %s, %d KB\n", |
| 113 | flash->name, flash->total_size); |
stepan | 782fb17 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 114 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 115 | size = flash->total_size * 1024; |
stepan | 782fb17 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 116 | |
| 117 | #ifdef TS5300 |
| 118 | // FIXME: Wrong place for this decision |
| 119 | flash_baseaddr = 0x9400000; |
| 120 | #else |
| 121 | flash_baseaddr = (0xffffffff - size + 1); |
| 122 | #endif |
| 123 | |
| 124 | /* If getpagesize() > size -> |
| 125 | * `Error MMAP /dev/mem: Invalid argument' |
| 126 | * This should never happen as we don't support any flash chips |
| 127 | * smaller than 4k or 8k yet. |
| 128 | */ |
| 129 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 130 | if (getpagesize() > size) { |
| 131 | size = getpagesize(); |
stepan | 782fb17 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 132 | printf("WARNING: size: %d -> %ld (page size)\n", |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 133 | flash->total_size * 1024, (unsigned long)size); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 134 | } |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 135 | |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 136 | bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED, |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 137 | fd_mem, (off_t) flash_baseaddr); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 138 | if (bios == MAP_FAILED) { |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 139 | perror("Error: Can't mmap " MEM_DEV "."); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 140 | exit(1); |
| 141 | } |
| 142 | flash->virt_addr = bios; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 143 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 144 | if (flash->probe(flash) == 1) { |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 145 | printf("%s found at physical address: 0x%lx\n", |
stepan | 782fb17 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 146 | flash->name, flash_baseaddr); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 147 | return flash; |
| 148 | } |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 149 | munmap((void *)bios, size); |
stepan | c98b80b | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 150 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 151 | flash++; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 152 | } |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 153 | return NULL; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 154 | } |
| 155 | |
stepan | 193c9c2 | 2005-12-18 16:41:10 +0000 | [diff] [blame] | 156 | int verify_flash(struct flashchip *flash, uint8_t *buf) |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 157 | { |
stepan | c98b80b | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 158 | int idx; |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 159 | int total_size = flash->total_size * 1024; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 160 | volatile uint8_t *bios = flash->virt_addr; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 161 | |
stepan | c98b80b | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 162 | printf("Verifying flash "); |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 163 | |
| 164 | if (verbose) |
| 165 | printf("address: 0x00000000\b\b\b\b\b\b\b\b\b\b"); |
| 166 | |
stepan | c98b80b | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 167 | for (idx = 0; idx < total_size; idx++) { |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 168 | if (verbose && ((idx & 0xfff) == 0xfff)) |
stepan | c98b80b | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 169 | printf("0x%08x", idx); |
| 170 | |
| 171 | if (*(bios + idx) != *(buf + idx)) { |
| 172 | if (verbose) { |
| 173 | printf("0x%08x ", idx); |
| 174 | } |
| 175 | printf("- FAILED\n"); |
| 176 | return 1; |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 177 | } |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 178 | |
| 179 | if (verbose && ((idx & 0xfff) == 0xfff)) |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 180 | printf("\b\b\b\b\b\b\b\b\b\b"); |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 181 | } |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 182 | if (verbose) |
stepan | c98b80b | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 183 | printf("\b\b\b\b\b\b\b\b\b\b "); |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 184 | |
stepan | c98b80b | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 185 | printf("- VERIFIED \n"); |
| 186 | return 0; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 187 | } |
| 188 | |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 189 | void usage(const char *name) |
| 190 | { |
stepan | c211b47 | 2006-08-03 10:49:09 +0000 | [diff] [blame] | 191 | printf("usage: %s [-rwvEVfh] [-c chipname] [-s exclude_start]\n", name); |
| 192 | printf(" [-e exclude_end] [-m vendor:part] [-l file.layout] [-i imagename] [file]\n"); |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 193 | printf |
| 194 | (" -r | --read: read flash and save into file\n" |
| 195 | " -w | --write: write file into flash (default when\n" |
| 196 | " file is specified)\n" |
| 197 | " -v | --verify: verify flash against file\n" |
| 198 | " -E | --erase: erase flash device\n" |
| 199 | " -V | --verbose: more verbose output\n" |
| 200 | " -c | --chip <chipname>: probe only for specified flash chip\n" |
| 201 | " -s | --estart <addr>: exclude start position\n" |
| 202 | " -e | --eend <addr>: exclude end postion\n" |
| 203 | " -m | --mainboard <vendor:part>: override mainboard settings\n" |
| 204 | " -f | --force: force write without checking image\n" |
| 205 | " -l | --layout <file.layout>: read rom layout from file\n" |
| 206 | " -i | --image <name>: only flash image name from flash layout\n" |
| 207 | "\n" " If no file is specified, then all that happens\n" |
| 208 | " is that flash info is dumped.\n\n"); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 209 | exit(1); |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 210 | } |
| 211 | |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 212 | int main(int argc, char *argv[]) |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 213 | { |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 214 | uint8_t *buf; |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 215 | unsigned long size; |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 216 | FILE *image; |
| 217 | struct flashchip *flash; |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 218 | int opt; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 219 | int option_index = 0; |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 220 | int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0; |
stepan | 83eca25 | 2006-01-04 16:42:57 +0000 | [diff] [blame] | 221 | int ret = 0; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 222 | |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 223 | static struct option long_options[] = { |
| 224 | {"read", 0, 0, 'r'}, |
| 225 | {"write", 0, 0, 'w'}, |
| 226 | {"erase", 0, 0, 'E'}, |
| 227 | {"verify", 0, 0, 'v'}, |
| 228 | {"chip", 1, 0, 'c'}, |
| 229 | {"estart", 1, 0, 's'}, |
| 230 | {"eend", 1, 0, 'e'}, |
| 231 | {"mainboard", 1, 0, 'm'}, |
| 232 | {"verbose", 0, 0, 'V'}, |
| 233 | {"force", 0, 0, 'f'}, |
| 234 | {"layout", 1, 0, 'l'}, |
| 235 | {"image", 1, 0, 'i'}, |
| 236 | {"help", 0, 0, 'h'}, |
| 237 | {0, 0, 0, 0} |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 238 | }; |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 239 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 240 | char *filename = NULL; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 241 | |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 242 | unsigned int exclude_start_position = 0, exclude_end_position = 0; // [x,y) |
| 243 | char *tempstr = NULL, *tempstr2 = NULL; |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 244 | |
| 245 | if (argc > 1) { |
| 246 | /* Yes, print them. */ |
| 247 | int i; |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 248 | printf_debug("The arguments are:\n"); |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 249 | for (i = 1; i < argc; ++i) |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 250 | printf_debug("%s\n", argv[i]); |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 251 | } |
| 252 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 253 | setbuf(stdout, NULL); |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 254 | while ((opt = getopt_long(argc, argv, "rwvVEfc:s:e:m:l:i:h", |
| 255 | long_options, &option_index)) != EOF) { |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 256 | switch (opt) { |
| 257 | case 'r': |
| 258 | read_it = 1; |
| 259 | break; |
| 260 | case 'w': |
| 261 | write_it = 1; |
| 262 | break; |
| 263 | case 'v': |
| 264 | verify_it = 1; |
| 265 | break; |
| 266 | case 'c': |
| 267 | chip_to_probe = strdup(optarg); |
| 268 | break; |
ollie | 077017b | 2004-03-18 20:27:33 +0000 | [diff] [blame] | 269 | case 'V': |
| 270 | verbose = 1; |
| 271 | break; |
ollie | 0eb62d6 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 272 | case 'E': |
| 273 | erase_it = 1; |
| 274 | break; |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 275 | case 's': |
| 276 | tempstr = strdup(optarg); |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 277 | sscanf(tempstr, "%x", &exclude_start_position); |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 278 | break; |
| 279 | case 'e': |
ollie | a430280 | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 280 | tempstr = strdup(optarg); |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 281 | sscanf(tempstr, "%x", &exclude_end_position); |
ollie | a430280 | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 282 | break; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 283 | case 'm': |
| 284 | tempstr = strdup(optarg); |
| 285 | strtok(tempstr, ":"); |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 286 | tempstr2 = strtok(NULL, ":"); |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 287 | if (tempstr2) { |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 288 | lb_vendor = tempstr; |
| 289 | lb_part = tempstr2; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 290 | } else { |
| 291 | printf("warning: ignored wrong format of" |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 292 | " mainboard: %s\n", tempstr); |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 293 | } |
| 294 | break; |
| 295 | case 'f': |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 296 | force = 1; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 297 | break; |
| 298 | case 'l': |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 299 | tempstr = strdup(optarg); |
stepan | 14566ee | 2007-04-14 16:32:59 +0000 | [diff] [blame] | 300 | if (read_romlayout(tempstr)) |
| 301 | exit(1); |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 302 | break; |
| 303 | case 'i': |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 304 | tempstr = strdup(optarg); |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 305 | find_romentry(tempstr); |
| 306 | break; |
| 307 | case 'h': |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 308 | default: |
| 309 | usage(argv[0]); |
| 310 | break; |
| 311 | } |
| 312 | } |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 313 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 314 | if (read_it && write_it) { |
| 315 | printf("-r and -w are mutually exclusive\n"); |
| 316 | usage(argv[0]); |
| 317 | } |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 318 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 319 | if (optind < argc) |
| 320 | filename = argv[optind++]; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 321 | |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 322 | /* First get full io access */ |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 323 | #if defined (__sun) && (defined(__i386) || defined(__amd64)) |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 324 | if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) { |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 325 | #else |
| 326 | if (iopl(3) != 0) { |
| 327 | #endif |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 328 | fprintf(stderr, "ERROR: iopl failed: \"%s\"\n", |
| 329 | strerror(errno)); |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 330 | exit(1); |
| 331 | } |
| 332 | |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 333 | /* Initialize PCI access for flash enables */ |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 334 | pacc = pci_alloc(); /* Get the pci_access structure */ |
| 335 | /* Set all options you want -- here we stick with the defaults */ |
| 336 | pci_init(pacc); /* Initialize the PCI library */ |
| 337 | pci_scan_bus(pacc); /* We want to get the list of devices */ |
| 338 | |
stepan | 782fb17 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 339 | /* Open the memory device. A lot of functions need it */ |
| 340 | if ((fd_mem = open(MEM_DEV, O_RDWR)) < 0) { |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 341 | perror("Error: Can not access memory using " MEM_DEV |
| 342 | ". You need to be root."); |
stepan | 782fb17 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 343 | exit(1); |
| 344 | } |
| 345 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 346 | myusec_calibrate_delay(); |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 347 | |
| 348 | /* We look at the lbtable first to see if we need a |
| 349 | * mainboard specific flash enable sequence. |
| 350 | */ |
| 351 | linuxbios_init(); |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 352 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 353 | /* try to enable it. Failure IS an option, since not all motherboards |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 354 | * really need this to be done, etc., etc. |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 355 | */ |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 356 | ret = chipset_flash_enable(); |
| 357 | if (ret == -2) { |
| 358 | printf("WARNING: No chipset found. Flash detection " |
| 359 | "will most likely fail.\n"); |
| 360 | } |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 361 | |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 362 | board_flash_enable(lb_vendor, lb_part); |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 363 | |
| 364 | if ((flash = probe_flash(flashchips)) == NULL) { |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 365 | printf("No EEPROM/flash device found.\n"); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 366 | exit(1); |
| 367 | } |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 368 | |
stepan | 603f955 | 2006-10-07 00:23:51 +0000 | [diff] [blame] | 369 | printf("Flash part is %s (%d KB)\n", flash->name, flash->total_size); |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 370 | |
ollie | 0eb62d6 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 371 | if (!filename && !erase_it) { |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 372 | // FIXME: Do we really want this feature implicitly? |
| 373 | printf("OK, only ENABLING flash write, but NOT FLASHING.\n"); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 374 | return 0; |
| 375 | } |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 376 | |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 377 | size = flash->total_size * 1024; |
| 378 | buf = (uint8_t *) calloc(size, sizeof(char)); |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 379 | |
ollie | 0eb62d6 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 380 | if (erase_it) { |
| 381 | printf("Erasing flash chip\n"); |
| 382 | flash->erase(flash); |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 383 | exit(0); |
ollie | 0eb62d6 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 384 | } else if (read_it) { |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 385 | if ((image = fopen(filename, "w")) == NULL) { |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 386 | perror(filename); |
| 387 | exit(1); |
| 388 | } |
| 389 | printf("Reading Flash..."); |
ollie | a3def63 | 2004-03-19 22:10:07 +0000 | [diff] [blame] | 390 | if (flash->read == NULL) |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 391 | memcpy(buf, (const char *)flash->virt_addr, size); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 392 | else |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 393 | flash->read(flash, buf); |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 394 | |
ollie | a430280 | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 395 | if (exclude_end_position - exclude_start_position > 0) |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 396 | memset(buf + exclude_start_position, 0, |
| 397 | exclude_end_position - exclude_start_position); |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 398 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 399 | fwrite(buf, sizeof(char), size, image); |
| 400 | fclose(image); |
| 401 | printf("done\n"); |
| 402 | } else { |
stepan | 1da96c0 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 403 | struct stat image_stat; |
| 404 | |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 405 | if ((image = fopen(filename, "r")) == NULL) { |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 406 | perror(filename); |
| 407 | exit(1); |
| 408 | } |
stepan | 1da96c0 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 409 | if (fstat(fileno(image), &image_stat) != 0) { |
| 410 | perror(filename); |
| 411 | exit(1); |
| 412 | } |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 413 | if (image_stat.st_size != flash->total_size * 1024) { |
stepan | 6ef8775 | 2007-03-22 14:51:45 +0000 | [diff] [blame] | 414 | fprintf(stderr, "Error: Image size doesnt match\n"); |
stepan | 1da96c0 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 415 | exit(1); |
| 416 | } |
| 417 | |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 418 | fread(buf, sizeof(char), size, image); |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 419 | show_id(buf, size); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 420 | fclose(image); |
| 421 | } |
| 422 | |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 423 | /* exclude range stuff. Nice idea, but at the moment it is only |
| 424 | * supported in hardware by the pm49fl004 chips. |
| 425 | * Instead of implementing this for all chips I suggest advancing |
| 426 | * it to the rom layout feature below and drop exclude range |
| 427 | * completely once all flash chips can do rom layouts. stepan |
| 428 | */ |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 429 | |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 430 | // //////////////////////////////////////////////////////////// |
ollie | a430280 | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 431 | if (exclude_end_position - exclude_start_position > 0) |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 432 | memcpy(buf + exclude_start_position, |
| 433 | (const char *)flash->virt_addr + exclude_start_position, |
| 434 | exclude_end_position - exclude_start_position); |
ollie | a430280 | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 435 | |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 436 | exclude_start_page = exclude_start_position / flash->page_size; |
| 437 | if ((exclude_start_position % flash->page_size) != 0) { |
ollie | a430280 | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 438 | exclude_start_page++; |
| 439 | } |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 440 | exclude_end_page = exclude_end_position / flash->page_size; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 441 | // //////////////////////////////////////////////////////////// |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 442 | |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 443 | // This should be moved into each flash part's code to do it |
| 444 | // cleanly. This does the job. |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 445 | handle_romentries(buf, (uint8_t *) flash->virt_addr); |
| 446 | |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 447 | // //////////////////////////////////////////////////////////// |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 448 | |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 449 | if (write_it) |
stepan | 83eca25 | 2006-01-04 16:42:57 +0000 | [diff] [blame] | 450 | ret |= flash->write(flash, buf); |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 451 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 452 | if (verify_it) |
stepan | 83eca25 | 2006-01-04 16:42:57 +0000 | [diff] [blame] | 453 | ret |= verify_flash(flash, buf); |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 454 | |
stepan | 83eca25 | 2006-01-04 16:42:57 +0000 | [diff] [blame] | 455 | return ret; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 456 | } |