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