annotate miscprog/grokdsn.c @ 191:0c631396b8ce

started grokdsn utility, parses header successfully
author Michael Spacefalcon <falcon@ivan.Harhan.ORG>
date Wed, 07 Jan 2015 06:55:33 +0000
parents
children 5d84f63eff72
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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;
0c631396b8ce started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
53 unsigned total_sectors;
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
0c631396b8ce started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
111 main(argc, argv)
0c631396b8ce started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
112 char **argv;
0c631396b8ce started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
113 {
0c631396b8ce started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
114 if (sizeof(struct cdf_header) != 512) {
0c631396b8ce started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
115 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
116 exit(1);
0c631396b8ce started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
117 }
0c631396b8ce started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
118 if (argc != 3) {
0c631396b8ce started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
119 fprintf(stderr, "usage: %s binfile.dsn <operation>\n", argv[0]);
0c631396b8ce started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
120 exit(1);
0c631396b8ce started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
121 }
0c631396b8ce started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
122 dsnfilename = argv[1];
0c631396b8ce started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
123 open_and_mmap_file();
0c631396b8ce started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
124 if (!strcmp(argv[2], "hdr"))
0c631396b8ce started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
125 return dump_cdf_header();
0c631396b8ce started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
126 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
127 exit(1);
0c631396b8ce started grokdsn utility, parses header successfully
Michael Spacefalcon <falcon@ivan.Harhan.ORG>
parents:
diff changeset
128 }