annotate miscprog/memwrite-grep.c @ 219:e3bbb8cfbbc0

pircalextr: added second argument for output directory
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 28 May 2017 20:33:26 +0000
parents 8bdc87c0fc03
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
205
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
2 * This utility performs a memmem() binary "grep", checking to see if a given
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
3 * binary file (firmware image) contains the $l1tm_mem_write implementation
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
4 * code found in our TCS211 objects and in that one C11x fw version for which
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
5 * we have the map file.
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
6 */
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
7
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
8 #define _GNU_SOURCE
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <sys/types.h>
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
10 #include <sys/file.h>
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
11 #include <sys/stat.h>
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
12 #include <stdio.h>
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
13 #include <string.h>
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
14 #include <stdlib.h>
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
15 #include <unistd.h>
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
16
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
17 u_char needle[50] = {
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
18 0x30, 0xb5,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
19 0x45, 0x68,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
20 0x42, 0x78,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
21 0x04, 0x3a,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
22 0x12, 0x06,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
23 0x13, 0x0e,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
24 0x00, 0x2b,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
25 0x09, 0xd0,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
26 0x00, 0x22,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
27 0x14, 0x18,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
28 0x24, 0x7a,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
29 0x54, 0x55,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
30 0x52, 0x1c,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
31 0x12, 0x04,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
32 0x12, 0x0c,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
33 0x01, 0x3b,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
34 0x00, 0x2b,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
35 0xf6, 0xd1,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
36 0x00, 0x20,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
37 0x88, 0x80,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
38 0x00, 0x06,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
39 0x00, 0x0e,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
40 0x48, 0x70,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
41 0x88, 0x70,
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
42 0x30, 0xbd
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
43 };
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
44
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
45 u_char *haystack;
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
46 size_t haystack_size;
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
47
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
48 read_file(filename)
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
49 char *filename;
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
50 {
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
51 int fd;
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
52 struct stat st;
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
53
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
54 fd = open(filename, O_RDONLY);
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
55 if (fd < 0) {
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
56 perror(filename);
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
57 exit(1);
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
58 }
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
59 fstat(fd, &st);
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
60 if (!S_ISREG(st.st_mode)) {
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
61 fprintf(stderr, "error: %s is not a regular file\n", filename);
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
62 exit(1);
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
63 }
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
64 haystack_size = st.st_size;
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
65 haystack = malloc(haystack_size);
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
66 if (!haystack) {
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
67 fprintf(stderr, "unable to malloc buffer for %s\n", filename);
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
68 exit(1);
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
69 }
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
70 read(fd, haystack, haystack_size);
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
71 close(fd);
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
72 }
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
73
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
74 main(argc, argv)
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
75 char **argv;
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
76 {
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
77 u_char *result;
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
78
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
79 if (argc != 2) {
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
80 fprintf(stderr, "usage: %s firmware.bin\n", argv[0]);
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
81 exit(1);
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
82 }
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
83 read_file(argv[1]);
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
84 result = memmem(haystack, haystack_size, needle, sizeof needle);
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
85 if (result)
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
86 printf("Found the needle bytes at offset 0x%x\n",
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
87 result - haystack);
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
88 else
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
89 printf("Needle bytes not found in this image\n");
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
90 exit(0);
8bdc87c0fc03 memwrite-grep hack-utility written
Mychaela Falconia <falcon@ivan.Harhan.ORG>
parents:
diff changeset
91 }