2 Generate readable C-source structures from binary page-table arrays found in hboot images
4 Author: TJ <linux@tjworld.net>
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 3 of the License.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <sys/types.h>
30 usage(const char *name) {
31 printf("Usage: %s: [-d -f <pagetable>\n", name);
32 printf("Copyright 2010 TJ <linux@tjworld.net>\n"
33 "Licensed under the terms of the GNU GPL v3\n\n"
34 "\t-d\tprint addresses in decimal not hexadecimal\n"
35 "\t-f\tpath to the file containing the needle\n\n"
37 "\tInformation messages are written to stderr and only matches are written to stdout.\n"
38 "\tThis allows the output to be piped through other filters.\n"
43 main(int argc, char *argv[], char *env[]) {
45 int c, use_decimal, pagetable_fd;
48 struct stat pagetable_stat;
50 pagetable_name = pagetable_mmap = NULL;
53 if (argc > 1) { // must have some parameters
56 while ((c = getopt(argc, argv, "f:d")) != -1 ) { // iterate the options
59 pagetable_name = optarg;
69 if ((pagetable_fd = open(pagetable_name, O_RDONLY)) == -1) {
70 perror("Unable to open pagetable");
76 fstat(pagetable_fd, &pagetable_stat);
77 fprintf(stderr, "Base:\t%s\n", use_decimal == 1 ? "decimal" : "hexadecimal");
78 fprintf(stderr, "Pagetable:\t\t%12ld\t%s\n", pagetable_stat.st_size, pagetable_name);
80 if(MAP_FAILED == (pagetable_mmap = mmap(pagetable_mmap, pagetable_stat.st_size, PROT_READ, MAP_SHARED, pagetable_fd, 0))) {
81 perror("Unable to memory-map pagetable");
85 void *f = pagetable_mmap; // convenient short names
87 unsigned long end = pagetable_stat.st_size;
89 unsigned int physical;
91 fprintf(stderr, "Processing...\n"
92 " Virtual Physical Count\n");
93 for (offset = 0; offset <= end; offset += 12) {
94 virtual = *(unsigned int *)(f + offset);
95 physical = *(unsigned int *)(f + offset + 4);
96 count = *(unsigned int *)(f + offset + 8);
97 if (use_decimal) fprintf(stdout, "{%010u, %010u, %010u},\n", virtual, physical, count);
98 else fprintf(stdout, "{0x%08X, 0x%08X, 0x%08X},\n", virtual, physical, count);
100 fprintf(stderr, "Done.\n");
107 if (pagetable_mmap > 0) munmap(pagetable_mmap, pagetable_stat.st_size);
108 if (pagetable_fd > 0) close(pagetable_fd);