| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 2 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 #include <errno.h> | 6 #include <errno.h> |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <inttypes.h> | 8 #include <inttypes.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 #include <stdio.h> | 10 #include <stdio.h> |
| 11 #include <stdlib.h> | 11 #include <stdlib.h> |
| 12 #include <string.h> | 12 #include <string.h> |
| 13 #include <sys/mman.h> | 13 #include <sys/mman.h> |
| 14 #include <sys/stat.h> | 14 #include <sys/stat.h> |
| 15 #include <sys/types.h> | 15 #include <sys/types.h> |
| 16 #include <unistd.h> | 16 #include <unistd.h> |
| 17 | 17 |
| 18 #include "fmap.h" |
| 19 |
| 18 /* global variables */ | 20 /* global variables */ |
| 19 static int opt_extract = 0; | 21 static int opt_extract = 0; |
| 20 static char *progname; | 22 static char* progname; |
| 21 static void *base_of_rom; | 23 static void* base_of_rom; |
| 22 | |
| 23 /* FMAP structs. See http://code.google.com/p/flashmap/wiki/FmapSpec */ | |
| 24 #define FMAP_SIGLEN 8 | |
| 25 #define FMAP_NAMELEN 32 | |
| 26 #define FMAP_SEARCH_STRIDE 4 | |
| 27 typedef struct _FmapHeader { | |
| 28 char fmap_signature[FMAP_SIGLEN]; /* avoiding endian issues */ | |
| 29 uint8_t fmap_ver_major; | |
| 30 uint8_t fmap_ver_minor; | |
| 31 uint64_t fmap_base; | |
| 32 uint32_t fmap_size; | |
| 33 char fmap_name[FMAP_NAMELEN]; | |
| 34 uint16_t fmap_nareas; | |
| 35 } __attribute__((packed)) FmapHeader; | |
| 36 | |
| 37 typedef struct _AreaHeader { | |
| 38 uint32_t area_offset; | |
| 39 uint32_t area_size; | |
| 40 char area_name[FMAP_NAMELEN]; | |
| 41 uint16_t area_flags; | |
| 42 } __attribute__((packed)) AreaHeader; | |
| 43 | 24 |
| 44 | 25 |
| 45 /* Return 0 if successful */ | 26 /* Return 0 if successful */ |
| 46 static int dump_fmap(void *ptr) { | 27 static int dump_fmap(const void* ptr) { |
| 47 int i,retval = 0; | 28 int i,retval = 0; |
| 48 char buf[80]; // DWR: magic number | 29 char buf[80]; // DWR: magic number |
| 49 FmapHeader *fmh = (FmapHeader *)ptr; | 30 const FmapHeader* fmh = (const FmapHeader*) ptr; |
| 50 AreaHeader *ah = (AreaHeader *)(ptr + sizeof(FmapHeader)); | 31 const FmapAreaHeader* ah = (const FmapAreaHeader*) (ptr + sizeof(FmapHeader)); |
| 51 | 32 |
| 52 snprintf(buf, FMAP_SIGLEN+1, "%s", fmh->fmap_signature); | 33 snprintf(buf, FMAP_SIGNATURE_SIZE+1, "%s", fmh->fmap_signature); |
| 53 printf("fmap_signature %s\n", buf); | 34 printf("fmap_signature %s\n", buf); |
| 54 printf("fmap_version: %d.%d\n", fmh->fmap_ver_major, fmh->fmap_ver_minor); | 35 printf("fmap_version: %d.%d\n", fmh->fmap_ver_major, fmh->fmap_ver_minor); |
| 55 printf("fmap_base: 0x%" PRIx64 "\n", fmh->fmap_base); | 36 printf("fmap_base: 0x%" PRIx64 "\n", fmh->fmap_base); |
| 56 printf("fmap_size: 0x%08x (%d)\n", fmh->fmap_size, fmh->fmap_size); | 37 printf("fmap_size: 0x%08x (%d)\n", fmh->fmap_size, fmh->fmap_size); |
| 57 snprintf(buf, FMAP_NAMELEN+1, "%s", fmh->fmap_name); | 38 snprintf(buf, FMAP_NAMELEN+1, "%s", fmh->fmap_name); |
| 58 printf("fmap_name: %s\n", buf); | 39 printf("fmap_name: %s\n", buf); |
| 59 printf("fmap_nareas: %d\n", fmh->fmap_nareas); | 40 printf("fmap_nareas: %d\n", fmh->fmap_nareas); |
| 60 | 41 |
| 61 for (i=0; i<fmh->fmap_nareas; i++) { | 42 for (i=0; i<fmh->fmap_nareas; i++) { |
| 62 printf("area: %d\n", i+1); | 43 printf("area: %d\n", i+1); |
| 63 printf("area_offset: 0x%08x\n", ah->area_offset); | 44 printf("area_offset: 0x%08x\n", ah->area_offset); |
| 64 printf("area_size: 0x%08x (%d)\n", ah->area_size, ah->area_size); | 45 printf("area_size: 0x%08x (%d)\n", ah->area_size, ah->area_size); |
| 65 snprintf(buf, FMAP_NAMELEN+1, "%s", ah->area_name); | 46 snprintf(buf, FMAP_NAMELEN+1, "%s", ah->area_name); |
| 66 printf("area_name: %s\n", buf); | 47 printf("area_name: %s\n", buf); |
| 67 | 48 |
| 68 if (opt_extract) { | 49 if (opt_extract) { |
| 69 char *s; | 50 char* s; |
| 70 for (s=buf; *s; s++) | 51 for (s=buf;* s; s++) |
| 71 if (*s == ' ') | 52 if (*s == ' ') |
| 72 *s = '_'; | 53 *s = '_'; |
| 73 FILE *fp = fopen(buf,"wb"); | 54 FILE* fp = fopen(buf,"wb"); |
| 74 if (!fp) { | 55 if (!fp) { |
| 75 fprintf(stderr, "%s: can't open %s: %s\n", | 56 fprintf(stderr, "%s: can't open %s: %s\n", |
| 76 progname, buf, strerror(errno)); | 57 progname, buf, strerror(errno)); |
| 77 retval = 1; | 58 retval = 1; |
| 78 } else { | 59 } else { |
| 79 if (1 != fwrite(base_of_rom + ah->area_offset, ah->area_size, 1, fp)) { | 60 if (1 != fwrite(base_of_rom + ah->area_offset, ah->area_size, 1, fp)) { |
| 80 fprintf(stderr, "%s: can't write %s: %s\n", | 61 fprintf(stderr, "%s: can't write %s: %s\n", |
| 81 progname, buf, strerror(errno)); | 62 progname, buf, strerror(errno)); |
| 82 retval = 1; | 63 retval = 1; |
| 83 } else { | 64 } else { |
| 84 printf("saved as \"%s\"\n", buf); | 65 printf("saved as \"%s\"\n", buf); |
| 85 } | 66 } |
| 86 fclose(fp); | 67 fclose(fp); |
| 87 } | 68 } |
| 88 } | 69 } |
| 89 | 70 |
| 90 ah++; | 71 ah++; |
| 91 } | 72 } |
| 92 | 73 |
| 93 return retval; | 74 return retval; |
| 94 } | 75 } |
| 95 | 76 |
| 96 | 77 |
| 97 int main(int argc, char *argv[]) { | 78 int main(int argc, char* argv[]) { |
| 98 int c; | 79 int c; |
| 99 int errorcnt = 0; | 80 int errorcnt = 0; |
| 100 struct stat sb; | 81 struct stat sb; |
| 101 int fd; | 82 int fd; |
| 102 char *s; | 83 const char* fmap; |
| 103 size_t i; | |
| 104 int retval = 1; | 84 int retval = 1; |
| 105 | 85 |
| 106 progname = strrchr(argv[0], '/'); | 86 progname = strrchr(argv[0], '/'); |
| 107 if (progname) | 87 if (progname) |
| 108 progname++; | 88 progname++; |
| 109 else | 89 else |
| 110 progname = argv[0]; | 90 progname = argv[0]; |
| 111 | 91 |
| 112 opterr = 0; /* quiet, you */ | 92 opterr = 0; /* quiet, you */ |
| 113 while ((c=getopt(argc, argv, ":x")) != -1) { | 93 while ((c=getopt(argc, argv, ":x")) != -1) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 if (fd < 0) { | 133 if (fd < 0) { |
| 154 fprintf(stderr, "%s: can't open %s: %s\n", | 134 fprintf(stderr, "%s: can't open %s: %s\n", |
| 155 progname, | 135 progname, |
| 156 argv[optind], | 136 argv[optind], |
| 157 strerror(errno)); | 137 strerror(errno)); |
| 158 return 1; | 138 return 1; |
| 159 } | 139 } |
| 160 printf("opened %s\n", argv[optind]); | 140 printf("opened %s\n", argv[optind]); |
| 161 | 141 |
| 162 base_of_rom = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); | 142 base_of_rom = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 163 if (base_of_rom == (char *)-1) { | 143 if (base_of_rom == (char*)-1) { |
| 164 fprintf(stderr, "%s: can't mmap %s: %s\n", | 144 fprintf(stderr, "%s: can't mmap %s: %s\n", |
| 165 progname, | 145 progname, |
| 166 argv[optind], | 146 argv[optind], |
| 167 strerror(errno)); | 147 strerror(errno)); |
| 168 close(fd); | 148 close(fd); |
| 169 return 1; | 149 return 1; |
| 170 } | 150 } |
| 171 close(fd); /* done with this now */ | 151 close(fd); /* done with this now */ |
| 172 | 152 |
| 173 s = (char *)base_of_rom; | 153 fmap = FmapFind((char*) base_of_rom, sb.st_size); |
| 174 for (i=0; i<sb.st_size; i += FMAP_SEARCH_STRIDE) { | 154 if (fmap) { |
| 175 if (0 == strncmp(s, "__FMAP__", 8)) { | 155 printf("hit at 0x%08x\n", (uint32_t) (fmap - (char*) base_of_rom)); |
| 176 printf("hit at 0x%08x\n", (uint32_t)i); | 156 retval = dump_fmap(fmap); |
| 177 retval = dump_fmap(s); | |
| 178 break; | |
| 179 } | |
| 180 s += FMAP_SEARCH_STRIDE; | |
| 181 } | 157 } |
| 182 | 158 |
| 183 if (0 != munmap(base_of_rom, sb.st_size)) { | 159 if (0 != munmap(base_of_rom, sb.st_size)) { |
| 184 fprintf(stderr, "%s: can't munmap %s: %s\n", | 160 fprintf(stderr, "%s: can't munmap %s: %s\n", |
| 185 progname, | 161 progname, |
| 186 argv[optind], | 162 argv[optind], |
| 187 strerror(errno)); | 163 strerror(errno)); |
| 188 return 1; | 164 return 1; |
| 189 } | 165 } |
| 190 | 166 |
| 191 return retval; | 167 return retval; |
| 192 } | 168 } |
| OLD | NEW |