FreeCalypso > hg > fc-am-toolkit
annotate bootutil/c139_main.c @ 12:fe5f7ba7f154
c139-analyze-boot utility put together, compiles
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 10 Jun 2023 04:58:26 +0000 |
parents | |
children |
rev | line source |
---|---|
12
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This C module is the main for c139-analyze-boot utility. |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 */ |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <sys/types.h> |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <sys/file.h> |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <sys/stat.h> |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <stdio.h> |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <stdlib.h> |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <string.h> |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <strings.h> |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include <unistd.h> |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 #include "../bootmatch/bootmatch.h" |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 extern struct bootmatch bootmatch_c11x_lockable[]; |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 extern struct bootmatch bootmatch_c11x_nolock[]; |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 extern struct bootmatch bootmatch_c139_lockable[]; |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 extern struct bootmatch bootmatch_c139_nolock[]; |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 extern struct bootmatch bootmatch_fc_patch[]; |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 #define LENGTH_OF_INTEREST 0x2064 |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 static u_char image[LENGTH_OF_INTEREST]; |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 static void |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 read_bin_file(filename) |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 char *filename; |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 { |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 int fd; |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 struct stat st; |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 fd = open(filename, O_RDONLY); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 if (fd < 0) { |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 perror(filename); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 exit(1); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 } |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 fstat(fd, &st); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 if (!S_ISREG(st.st_mode)) { |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 fprintf(stderr, "error: %s is not a regular file\n", filename); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 exit(1); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 } |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 if (st.st_size < LENGTH_OF_INTEREST) { |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 fprintf(stderr, "error: %s is too short\n", filename); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 exit(1); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 } |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 read(fd, image, LENGTH_OF_INTEREST); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 close(fd); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 } |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 static int |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 check_810_signature() |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 { |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 if (image[0x810] != '1') |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 return(0); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 if (image[0x811] != '0') |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 return(0); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 if (image[0x812] != '0') |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 return(0); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 if (image[0x813] == '3' || image[0x813] == '4') |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 return(1); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 else |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 return(0); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 } |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 static void |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 classify_by_lock_word() |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 { |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 unsigned lword; |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 lword = ((unsigned) image[0x2060]) | |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 ((unsigned) image[0x2061] << 8) | |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 ((unsigned) image[0x2062] << 16) | |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 ((unsigned) image[0x2063] << 24); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 if (lword == 0xDDDDDDDD) |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 puts("unlocked"); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 else |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 puts("locked"); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 } |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 main(argc, argv) |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 char **argv; |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 { |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 if (argc != 2) { |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 fprintf(stderr, "usage: %s flashdump.bin\n", argv[0]); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 exit(1); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 } |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 read_bin_file(argv[1]); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 if (check_for_match(image, bootmatch_fc_patch)) { |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 puts("fc"); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 exit(0); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 } |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 if (check_for_match(image, bootmatch_c11x_nolock)) { |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 puts("unlocked"); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 exit(0); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
95 } |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
96 if (check_for_match(image, bootmatch_c11x_lockable)) { |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 classify_by_lock_word(); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 exit(0); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 } |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 if (check_for_match(image, bootmatch_c139_nolock)) { |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
101 if (!check_810_signature()) { |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 puts("unknown"); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 exit(0); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 } |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
105 puts("unlocked"); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
106 exit(0); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
107 } |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
108 if (check_for_match(image, bootmatch_c139_lockable)) { |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
109 if (!check_810_signature()) { |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
110 puts("unknown"); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
111 exit(0); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
112 } |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
113 classify_by_lock_word(); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
114 exit(0); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
115 } |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
116 puts("unknown"); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
117 exit(0); |
fe5f7ba7f154
c139-analyze-boot utility put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
118 } |