OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 2 * Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
gauravsh
2011/02/10 20:02:05
2011
Che-Liang Chiou
2011/02/14 02:27:53
Done.
| |
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; |
gauravsh
2011/02/10 20:02:05
(optional nit): for consistency with the rest of v
Che-Liang Chiou
2011/02/14 02:27:53
Done.
| |
21 static void *base_of_rom; | 23 static void *base_of_rom; |
22 | 24 |
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 | |
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) { |
gauravsh
2011/02/10 20:02:05
const void*
Che-Liang Chiou
2011/02/14 02:27:53
Done.
| |
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; |
Randall Spangler
2011/02/11 17:26:04
const FmapHeader* fmh
Che-Liang Chiou
2011/02/14 02:27:53
Done.
| |
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_SIGLEN+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 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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; |
gauravsh
2011/02/10 20:02:05
const char*
Che-Liang Chiou
2011/02/14 02:27:53
Done.
| |
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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 = FindFmap((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 |