FreeCalypso > hg > freecalypso-reveng
annotate miscprog/grokdsn.c @ 192:5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
author | Michael Spacefalcon <falcon@ivan.Harhan.ORG> |
---|---|
date | Wed, 07 Jan 2015 07:12:51 +0000 |
parents | 0c631396b8ce |
children | 37f78f986a0a |
rev | line source |
---|---|
191
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
1 /* |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
2 * We have TI's Leonardo reference schematics in the form of 3 different PDF |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
3 * versions and an OrCAD DSN file corresponding to one of them. The latter |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
4 * appears (based on a cursory strings(1) inspection) to contain more |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
5 * juicy information than is present in the PDF prints. We need to extract |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
6 * as much of this information as we can in order to replicate the lost |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
7 * Leonardo board as closely as possible. |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
8 * |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
9 * The top level structure of this DSN file appears to be Microsoft CDF. |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
10 * Therefore, I shall begin by parsing this "archive" structure and |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
11 * extracting the individual files contained therein, in an effort to gain |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
12 * more insight as to what goes with what than can be gleaned from strings(1) |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
13 * on the raw DSN file. This hack-utility is my CDF parser written for |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
14 * this specific purpose. |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
15 */ |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
16 |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
17 #include <sys/types.h> |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
18 #include <sys/file.h> |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
19 #include <sys/stat.h> |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
20 #include <sys/mman.h> |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
21 #include <stdio.h> |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
22 #include <stdint.h> |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
23 #include <endian.h> |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
24 #include <stdlib.h> |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
25 #include <unistd.h> |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
26 #include <string.h> |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
27 #include <strings.h> |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
28 |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
29 #define HEADER_MFAT_ENTRIES 109 |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
30 struct cdf_header { |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
31 u_char magic[8]; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
32 u_char uid[16]; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
33 uint16_t fmt_minor; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
34 uint16_t fmt_major; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
35 uint16_t byte_order; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
36 uint16_t sector_size; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
37 uint16_t subsec_size; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
38 u_char rsvd1[10]; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
39 uint32_t fat_sectors; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
40 uint32_t dir_start; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
41 u_char rsvd2[4]; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
42 uint32_t min_large_file; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
43 uint32_t subfat_start; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
44 uint32_t subfat_sectors; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
45 uint32_t mfat_start; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
46 uint32_t mfat_sectors; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
47 uint32_t fat_secids[HEADER_MFAT_ENTRIES]; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
48 }; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
49 |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
50 char *dsnfilename; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
51 u_char *filemapping; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
52 struct cdf_header *cdf_header; |
192
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
53 unsigned total_sectors, fat_nsectors; |
191
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
54 |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
55 open_and_mmap_file() |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
56 { |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
57 int fd; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
58 struct stat st; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
59 |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
60 fd = open(dsnfilename, O_RDONLY); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
61 if (fd < 0) { |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
62 perror(dsnfilename); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
63 exit(1); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
64 } |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
65 fstat(fd, &st); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
66 if (!S_ISREG(st.st_mode)) { |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
67 fprintf(stderr, "error: %s is not a regular file\n", |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
68 dsnfilename); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
69 exit(1); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
70 } |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
71 if (st.st_size < 512) { |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
72 fprintf(stderr, "error: %s is shorter than 512 bytes\n", |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
73 dsnfilename); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
74 exit(1); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
75 } |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
76 if (st.st_size % 512) { |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
77 fprintf(stderr, "error: %s is not a multiple of 512 bytes\n", |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
78 dsnfilename); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
79 exit(1); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
80 } |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
81 filemapping = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
82 if (filemapping == MAP_FAILED) { |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
83 perror("mmap"); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
84 exit(1); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
85 } |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
86 close(fd); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
87 cdf_header = (struct cdf_header *) filemapping; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
88 total_sectors = st.st_size / 512 - 1; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
89 return(0); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
90 } |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
91 |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
92 dump_cdf_header() |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
93 { |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
94 printf("Magic: %02X %02X %02X %02X %02X %02X %02X %02X\n", |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
95 cdf_header->magic[0], cdf_header->magic[1], |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
96 cdf_header->magic[2], cdf_header->magic[3], |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
97 cdf_header->magic[4], cdf_header->magic[5], |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
98 cdf_header->magic[6], cdf_header->magic[7]); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
99 printf("Format version: %04X.%04X\n", le16toh(cdf_header->fmt_major), |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
100 le16toh(cdf_header->fmt_minor)); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
101 printf("Sector / subsector shift: %u / %u\n", |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
102 le16toh(cdf_header->sector_size), |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
103 le16toh(cdf_header->subsec_size)); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
104 printf("Total FAT sectors: %u\n", le32toh(cdf_header->fat_sectors)); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
105 printf("Directory start sector: %u\n", le32toh(cdf_header->dir_start)); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
106 printf("File size threshold: %u\n", |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
107 le32toh(cdf_header->min_large_file)); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
108 return(0); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
109 } |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
110 |
192
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
111 u_char * |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
112 get_sector_ptr(secid) |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
113 unsigned secid; |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
114 { |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
115 if (secid > total_sectors) { |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
116 fprintf(stderr, |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
117 "error: request for sector #%u; we only have %u in total\n", |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
118 secid, total_sectors); |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
119 exit(1); |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
120 } |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
121 return filemapping + (secid + 1) * 512; |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
122 } |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
123 |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
124 init_fat_access() |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
125 { |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
126 fat_nsectors = le32toh(cdf_header->fat_sectors); |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
127 if (fat_nsectors > HEADER_MFAT_ENTRIES) { |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
128 fprintf(stderr, "error: large FAT not supported\n"); |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
129 exit(1); |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
130 } |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
131 return(0); |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
132 } |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
133 |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
134 u_char * |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
135 get_fat_sector(fat_sec_no) |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
136 unsigned fat_sec_no; |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
137 { |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
138 unsigned secid; |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
139 |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
140 if (fat_sec_no > fat_nsectors) { |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
141 fprintf(stderr, |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
142 "error: request for FAT sector #%u; we only have %u in total\n", |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
143 fat_sec_no, fat_nsectors); |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
144 exit(1); |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
145 } |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
146 secid = le32toh(cdf_header->fat_secids[fat_sec_no]); |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
147 return get_sector_ptr(secid); |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
148 } |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
149 |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
150 int32_t |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
151 get_fat_entry(entry_no) |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
152 unsigned entry_no; |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
153 { |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
154 unsigned fat_sec_no, entry_in_sec; |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
155 uint32_t *fat_sector; |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
156 |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
157 fat_sec_no = entry_no / 128; |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
158 entry_in_sec = entry_no % 128; |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
159 fat_sector = (uint32_t *) get_fat_sector(fat_sec_no); |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
160 return le32toh(fat_sector[entry_in_sec]); |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
161 } |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
162 |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
163 dump_fat_chain(sarg) |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
164 char *sarg; |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
165 { |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
166 int i, n; |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
167 |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
168 init_fat_access(); |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
169 i = atoi(sarg); |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
170 while (i >= 0) { |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
171 n = get_fat_entry(i); |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
172 printf("FAT[%d] = %d\n", i, n); |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
173 i = n; |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
174 } |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
175 return(0); |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
176 } |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
177 |
191
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
178 main(argc, argv) |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
179 char **argv; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
180 { |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
181 if (sizeof(struct cdf_header) != 512) { |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
182 fprintf(stderr, "error: struct cdf_header is misdefined\n"); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
183 exit(1); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
184 } |
192
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
185 if (argc < 3) { |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
186 fprintf(stderr, "usage: %s binfile.dsn <op> [args]\n", argv[0]); |
191
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
187 exit(1); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
188 } |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
189 dsnfilename = argv[1]; |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
190 open_and_mmap_file(); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
191 if (!strcmp(argv[2], "hdr")) |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
192 return dump_cdf_header(); |
192
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
193 if (!strcmp(argv[2], "fatchain")) |
5d84f63eff72
grokdsn: able to follow the FAT chain for the directory
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
191
diff
changeset
|
194 return dump_fat_chain(argv[3]); |
191
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
195 fprintf(stderr, "error: \"%s\" is not a recognized command\n", argv[2]); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
196 exit(1); |
0c631396b8ce
started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
197 } |