annotate bootutil/c139-patch-dmagic.c @ 30:2d60f9a3032b default tip

LICENSE: public domain, copied from freecalypso-tools
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 24 Jun 2023 04:24:38 +0000
parents 6b0d533046e5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This program patches DD DD DD DD magic bytes into the given binary file
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * at offset 0x2060, thereby turning a locked C11x/12x or C139/140 flash image
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * into an unlocked one.
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 */
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/types.h>
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <sys/file.h>
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <sys/stat.h>
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdio.h>
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdlib.h>
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <unistd.h>
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 static u_char magic[4] = {0xDD, 0xDD, 0xDD, 0xDD};
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 main(argc, argv)
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 char **argv;
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 {
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 int fd;
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 struct stat st;
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 if (argc != 2) {
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 fprintf(stderr, "usage: %s flashimage.bin\n", argv[0]);
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 exit(1);
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 }
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 fd = open(argv[1], O_RDWR);
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 if (fd < 0) {
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 perror(argv[1]);
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 exit(1);
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 }
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 fstat(fd, &st);
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 if (!S_ISREG(st.st_mode)) {
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 fprintf(stderr, "error: %s is not a regular file\n", argv[1]);
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 exit(1);
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 }
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 if (st.st_size < 0x2064) {
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 fprintf(stderr, "error: %s is too short\n", argv[1]);
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 exit(1);
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 lseek(fd, 0x2060, SEEK_SET);
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 if (write(fd, magic, 4) != 4) {
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 perror("error writing to file");
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 exit(1);
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 }
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 exit(0);
6b0d533046e5 bootutil: add c139-patch-dmagic
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 }