FreeCalypso > hg > freecalypso-sw
annotate ffstools/tiffs-rd/main.c @ 225:c04aa85559ed
TIFFS in vitro reader started
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Mon, 13 Jan 2014 09:05:01 +0000 |
parents | |
children | 24ed817dd25d |
rev | line source |
---|---|
225
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
1 /* |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
2 * This C module contains the main() function for the tiffs utility, |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
3 * dispatching control to different operation commands. |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
4 */ |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
5 |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
6 #include <sys/types.h> |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
7 #include <ctype.h> |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
8 #include <stdio.h> |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
9 #include <stdlib.h> |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
10 #include <string.h> |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
11 #include <strings.h> |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
12 #include "types.h" |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
13 |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
14 u8 tiffs_header[6] = {'F', 'f', 's', '#', 0x10, 0x02}; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
15 u8 blank_flash_line[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
16 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
17 |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
18 char *imgfile; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
19 u32 eraseblk_size; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
20 int total_blocks; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
21 u32 total_ffs_size; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
22 int index_blk_num = -1, root_node_no; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
23 int offset_blocks; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
24 int verbose; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
25 |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
26 parse_org_arg(arg) |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
27 char *arg; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
28 { |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
29 char *cp; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
30 |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
31 cp = index(arg, 'x'); |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
32 if (!cp || !isdigit(cp[1]) || !isdigit(arg[0])) { |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
33 fprintf(stderr, |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
34 "error: TIFFS organization argument \"%s\" is invalid\n", arg); |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
35 exit(1); |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
36 } |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
37 *cp++ = '\0'; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
38 if (!strcmp(arg, "16")) |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
39 eraseblk_size = 0x4000; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
40 else if (!strcmp(arg, "32")) |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
41 eraseblk_size = 0x8000; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
42 else if (!strcmp(arg, "64")) |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
43 eraseblk_size = 0x10000; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
44 else if (!strcmp(arg, "128")) |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
45 eraseblk_size = 0x20000; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
46 else if (!strcmp(arg, "256")) |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
47 eraseblk_size = 0x40000; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
48 else { |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
49 fprintf(stderr, |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
50 "error: \"%s\" is not a recognized flash sector size\n", |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
51 arg); |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
52 exit(1); |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
53 } |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
54 total_blocks = atoi(cp); |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
55 if (total_blocks < 1 || total_blocks > 128) { |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
56 fprintf(stderr, |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
57 "error: \"%s\" is not a reasonable number of FFS sectors\n", |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
58 cp); |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
59 exit(1); |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
60 } |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
61 total_ffs_size = eraseblk_size * total_blocks; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
62 } |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
63 |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
64 static struct cmdtab { |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
65 char *cmd; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
66 int (*func)(); |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
67 } cmdtab[] = { |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
68 {"cat", NULL}, |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
69 {"fsck", NULL}, |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
70 {"ls", NULL}, |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
71 {"xtr", NULL}, |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
72 {NULL, NULL} |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
73 }; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
74 |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
75 main(argc, argv) |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
76 char **argv; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
77 { |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
78 extern int optind; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
79 extern char *optarg; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
80 int c; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
81 char *cmd; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
82 struct cmdtab *tp; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
83 |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
84 while ((c = getopt(argc, argv, "+a:o:r:v")) != EOF) |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
85 switch (c) { |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
86 case 'a': |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
87 index_blk_num = atoi(optarg); |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
88 continue; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
89 case 'o': |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
90 offset_blocks = atoi(optarg); |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
91 continue; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
92 case 'r': |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
93 root_node_no = atoi(optarg); |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
94 continue; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
95 case 'v': |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
96 verbose++; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
97 continue; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
98 default: |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
99 usage: fprintf(stderr, |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
100 "usage: %s [global-options] <imgfile> <org> <op> ...\n", |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
101 argv[0]); |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
102 exit(1); |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
103 } |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
104 if (argc - optind < 3) |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
105 goto usage; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
106 imgfile = argv[optind]; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
107 parse_org_arg(argv[optind+1]); |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
108 cmd = argv[optind+2]; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
109 |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
110 for (tp = cmdtab; tp->cmd; tp++) |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
111 if (!strcmp(tp->cmd, cmd)) |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
112 break; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
113 if (!tp->func) { |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
114 fprintf(stderr, |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
115 "%s: operation \"%s\" is unknown or unimplemented\n", |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
116 argv[0], cmd); |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
117 exit(1); |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
118 } |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
119 optind += 2; |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
120 return tp->func(argc - optind, argv + optind); |
c04aa85559ed
TIFFS in vitro reader started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
121 } |