comparison ffstools/tiffs-rd/basics.c @ 0:e7502631a0f9

initial import from freecalypso-sw rev 1033:5ab737ac3ad7
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 11 Jun 2016 00:13:35 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e7502631a0f9
1 /*
2 * This C module implements the "basics" of TIFFS image analysis.
3 */
4
5 #include <sys/types.h>
6 #include <sys/file.h>
7 #include <sys/stat.h>
8 #include <sys/mman.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <string.h>
13 #include <strings.h>
14 #include "types.h"
15 #include "struct.h"
16 #include "globals.h"
17
18 u8 tiffs_header[6] = {'F', 'f', 's', '#', 0x10, 0x02};
19
20 read_ffs_image()
21 {
22 int fd;
23 struct stat st;
24
25 fd = open(imgfile, O_RDONLY);
26 if (fd < 0) {
27 perror(imgfile);
28 exit(1);
29 }
30 fstat(fd, &st);
31 if (!S_ISREG(st.st_mode)) {
32 fprintf(stderr, "error: %s is not a regular file\n", imgfile);
33 exit(1);
34 }
35 if (st.st_size < imgfile_offset) {
36 fprintf(stderr,
37 "error: offset given with -o exceeds the size of the file\n");
38 exit(1);
39 }
40 if (st.st_size - imgfile_offset < total_ffs_size) {
41 fprintf(stderr,
42 "error: %s is shorter than FFS size of 0x%lx bytes\n",
43 imgfile, (u_long)total_ffs_size);
44 exit(1);
45 }
46 image = mmap(NULL, total_ffs_size, PROT_READ, MAP_PRIVATE, fd,
47 imgfile_offset);
48 if (image == MAP_FAILED) {
49 perror("mmap");
50 exit(1);
51 }
52 close(fd);
53 }
54
55 cmd_blkhdr()
56 {
57 int blk;
58 u8 *blkhdr;
59
60 read_ffs_image();
61 for (blk = 0; blk < total_blocks; blk++) {
62 printf("Block %3d: ", blk);
63 blkhdr = image + blk * eraseblk_size;
64 if (bcmp(blkhdr, tiffs_header, sizeof tiffs_header)) {
65 printf("No TIFFS header\n");
66 continue;
67 }
68 printf("age %02X%02X, type/status %02X\n",
69 blkhdr[7], blkhdr[6], blkhdr[8]);
70 }
71 exit(0);
72 }
73
74 find_inode_block()
75 {
76 int i, abcnt;
77 u8 *ptr;
78
79 if (index_blk_num >= 0) {
80 if (index_blk_num >= total_blocks) {
81 fprintf(stderr,
82 "invalid block # given with the -a option\n");
83 exit(1);
84 }
85 ptr = image + index_blk_num * eraseblk_size;
86 if (bcmp(ptr, tiffs_header, sizeof tiffs_header)) {
87 fprintf(stderr,
88 "error: block specified with -a has no TIFFS header\n");
89 exit(1);
90 }
91 if (ptr[8] != 0xAB) {
92 fprintf(stderr,
93 "error: block specified with -a is not an AB block\n");
94 exit(1);
95 }
96 inode_block = ptr;
97 return(0);
98 }
99 abcnt = 0;
100 for (ptr = image, i = 0; i < total_blocks; i++, ptr += eraseblk_size) {
101 if (bcmp(ptr, tiffs_header, sizeof tiffs_header)) {
102 fprintf(stderr,
103 "warning: no TIFFS signature in erase block #%d (offset %x)\n",
104 i, ptr - image);
105 continue;
106 }
107 switch (ptr[8]) {
108 case 0xAB:
109 if (verbose)
110 fprintf(stderr,
111 "Found AB index in erase block #%d (offset %x)\n",
112 i, ptr - image);
113 index_blk_num = i;
114 inode_block = ptr;
115 abcnt++;
116 continue;
117 case 0xBD:
118 case 0xBF:
119 continue;
120 }
121 fprintf(stderr,
122 "warning: unexpected block type/status %02X at offset %x\n",
123 ptr[8], ptr - image);
124 }
125 if (!inode_block) {
126 fprintf(stderr,
127 "error: could not find an active inode block in %s\n",
128 imgfile);
129 exit(1);
130 }
131 if (abcnt > 1) {
132 fprintf(stderr,
133 "error: found more than one AB block; use -a\n");
134 exit(1);
135 }
136 return(0);
137 }
138
139 cmd_fsinfo()
140 {
141 read_ffs_image();
142 find_inode_block();
143 printf("Active inode block (AB) is block #%d\n", index_blk_num);
144 alloc_inode_table();
145 find_root_inode();
146 printf("Root inode is #%x\n", root_inode);
147 if (validate_obj_name(root_inode, 1)) {
148 printf("Root inode (format) name: %s\n",
149 inode_info[root_inode]->dataptr);
150 exit(0);
151 } else {
152 printf("No valid name found in the root inode!\n");
153 exit(1);
154 }
155 }