annotate loadtools/flprotreg.c @ 845:aec320fe2653

fc-tmsync: new madc command reads all 8 MADC channels
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 31 Jul 2021 22:23:05 +0000
parents cf7bd5e705ed
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
712
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements commands dealing with Intel flash protection
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * register, which holds the IMEI on Compal phones.
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdio.h>
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdint.h>
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <string.h>
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <strings.h>
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdlib.h>
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include "flash.h"
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 extern struct flash_bank_info flash_bank_info[2];
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 flashcmd_protreg(argc, argv, bank)
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 char **argv;
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 {
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 struct flash_bank_info *bi;
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 uint16_t data[9];
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 int rc;
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 if (argc > 2) {
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 fprintf(stderr, "error: too many arguments\n");
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 return(-1);
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 }
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 if (flash_detect(bank, 0) < 0)
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 return(-1);
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 bi = flash_bank_info + bank;
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 rc = bi->ops->read_prot_reg(bi, data);
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 if (rc)
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 return(rc);
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 printf("Lock word: %04X\n", data[0]);
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 printf("Factory words: %04X %04X %04X %04X\n", data[1], data[2],
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 data[3], data[4]);
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 printf("User words: %04X %04X %04X %04X\n", data[5], data[6], data[7],
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 data[8]);
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 return(0);
a167d7b376b7 fc-loadtool code: flprotreg.c factored out
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
713
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
40
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
41 static int
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
42 compute_cd(digits)
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
43 u_char *digits;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
44 {
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
45 int i, dig, sum;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
46
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
47 sum = 0;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
48 for (i = 0; i < 14; i++) {
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
49 dig = digits[i];
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
50 if (i & 1) {
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
51 dig *= 2;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
52 if (dig > 9)
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
53 dig -= 9;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
54 }
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
55 sum += dig;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
56 }
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
57 dig = sum % 10;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
58 if (dig)
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
59 dig = 10 - dig;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
60 return dig;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
61 }
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
62
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
63 static void
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
64 format_imei(digits, strout)
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
65 u_char *digits;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
66 char *strout;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
67 {
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
68 int i;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
69 char *dp;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
70
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
71 dp = strout;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
72 for (i = 0; i < 15; i++) {
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
73 if (i == 8 || i == 14)
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
74 *dp++ = '-';
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
75 *dp++ = digits[i] + '0';
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
76 }
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
77 *dp = '\0';
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
78 }
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
79
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
80 flashcmd_compal_imei(argc, argv, bank)
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
81 char **argv;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
82 {
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
83 struct flash_bank_info *bi;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
84 uint16_t data[9];
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
85 u_char nibbles[16], *dp;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
86 char imei_str[18];
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
87 int rc, i, j;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
88 FILE *of;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
89
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
90 if (argc > 3) {
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
91 fprintf(stderr, "error: too many arguments\n");
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
92 return(-1);
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
93 }
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
94 if (flash_detect(bank, 0) < 0)
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
95 return(-1);
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
96 bi = flash_bank_info + bank;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
97 rc = bi->ops->read_prot_reg(bi, data);
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
98 if (rc)
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
99 return(rc);
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
100 dp = nibbles;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
101 for (i = 0; i < 4; i++) {
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
102 rc = data[i + 5];
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
103 for (j = 0; j < 4; j++) {
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
104 *dp++ = rc & 0xF;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
105 rc >>= 4;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
106 }
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
107 }
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
108 for (i = 1; i < 16; i++) {
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
109 if (nibbles[i] > 9) {
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
110 bad_imei: fprintf(stderr, "No Compal IMEI found\n");
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
111 return(-1);
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
112 }
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
113 }
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
114 if (compute_cd(nibbles+1) != nibbles[15])
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
115 goto bad_imei;
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
116 format_imei(nibbles+1, imei_str);
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
117 puts(imei_str);
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
118 if (argc < 3)
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
119 return(0);
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
120 of = fopen(argv[2], "w");
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
121 if (!of) {
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
122 perror(argv[2]);
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
123 return(-1);
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
124 }
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
125 fprintf(of, "%s\n", imei_str);
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
126 fclose(of);
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
127 printf("Saved to %s\n", argv[2]);
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
128 return(0);
cf7bd5e705ed fc-loadtool: flash compal-imei command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 712
diff changeset
129 }