annotate ffstools/tiffs-rd/basics.c @ 229:24ed817dd25d

tiffs IVA: reads image via mmap and displays block headers
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sat, 25 Jan 2014 23:56:47 +0000
parents
children ffaa033e7643
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
229
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
2 * This C module implements the "basics" of TIFFS image analysis.
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 */
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5 #include <sys/types.h>
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 #include <sys/file.h>
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7 #include <sys/stat.h>
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
8 #include <sys/mman.h>
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <stdio.h>
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10 #include <stdlib.h>
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 #include <unistd.h>
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12 #include <string.h>
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
13 #include <strings.h>
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14 #include "types.h"
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
16 u8 tiffs_header[6] = {'F', 'f', 's', '#', 0x10, 0x02};
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17 u8 blank_flash_line[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
18 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
19
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
20 extern char *imgfile;
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
21 extern u32 eraseblk_size;
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
22 extern int total_blocks;
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
23 extern u32 total_ffs_size;
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
24 extern int index_blk_num, root_node_no;
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
25 extern int verbose;
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
26
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
27 u_char *image;
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
28
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
29 read_ffs_image()
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
30 {
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
31 int fd;
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
32 struct stat st;
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
33
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34 fd = open(imgfile, O_RDONLY);
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35 if (fd < 0) {
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 perror(imgfile);
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
37 exit(1);
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
38 }
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
39 fstat(fd, &st);
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
40 if (!S_ISREG(st.st_mode)) {
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
41 fprintf(stderr, "error: %s is not a regular file\n", imgfile);
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
42 exit(1);
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
43 }
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
44 if (st.st_size < total_ffs_size) {
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45 fprintf(stderr,
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46 "error: %s is shorter than FFS size of 0x%lx bytes\n",
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
47 imgfile, (u_long)total_ffs_size);
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
48 exit(1);
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
49 }
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
50 image = mmap(NULL, total_ffs_size, PROT_READ, MAP_PRIVATE, fd, 0);
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
51 if (image == MAP_FAILED) {
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
52 perror("mmap");
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
53 exit(1);
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
54 }
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
55 }
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
56
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
57 cmd_blkhdr()
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
58 {
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
59 int blk;
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
60 u8 *blkhdr;
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
61
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
62 read_ffs_image();
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
63 for (blk = 0; blk < total_blocks; blk++) {
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
64 printf("Block %3d: ", blk);
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
65 blkhdr = image + blk * eraseblk_size;
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
66 if (bcmp(blkhdr, tiffs_header, sizeof tiffs_header)) {
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
67 printf("No TIFFS header\n");
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
68 continue;
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
69 }
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
70 printf("age %02X%02X, type/status %02X\n",
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
71 blkhdr[7], blkhdr[6], blkhdr[8]);
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
72 }
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
73 exit(0);
24ed817dd25d tiffs IVA: reads image via mmap and displays block headers
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
74 }