]>
Commit | Line | Data |
---|---|---|
1 | //----------------------------------------------------------------------------- | |
2 | // Copyright (C) 2012 Chalk <chalk.secu at gmail.com> | |
3 | // 2015 Dake <thomas.cayrou at gmail.com> | |
4 | ||
5 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
6 | // at your option, any later version. See the LICENSE.txt file for the text of | |
7 | // the license. | |
8 | //----------------------------------------------------------------------------- | |
9 | // Low frequency PCF7931 commands | |
10 | //----------------------------------------------------------------------------- | |
11 | ||
12 | #include "cmdlfpcf7931.h" | |
13 | ||
14 | #include <stdio.h> | |
15 | #include <string.h> | |
16 | #include "comms.h" | |
17 | #include "ui.h" | |
18 | #include "util.h" | |
19 | #include "graph.h" | |
20 | #include "cmdparser.h" | |
21 | #include "cmddata.h" | |
22 | #include "cmdmain.h" | |
23 | #include "cmdlf.h" | |
24 | ||
25 | static int CmdHelp(const char *Cmd); | |
26 | ||
27 | #define PCF7931_DEFAULT_INITDELAY 17500 | |
28 | #define PCF7931_DEFAULT_OFFSET_WIDTH 0 | |
29 | #define PCF7931_DEFAULT_OFFSET_POSITION 0 | |
30 | ||
31 | // Default values - Configuration | |
32 | struct pcf7931_config configPcf = { | |
33 | {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}, | |
34 | PCF7931_DEFAULT_INITDELAY, | |
35 | PCF7931_DEFAULT_OFFSET_WIDTH, | |
36 | PCF7931_DEFAULT_OFFSET_POSITION | |
37 | }; | |
38 | ||
39 | // Resets the configuration settings to default values. | |
40 | int pcf7931_resetConfig(){ | |
41 | memset(configPcf.Pwd, 0xFF, sizeof(configPcf.Pwd) ); | |
42 | configPcf.InitDelay = PCF7931_DEFAULT_INITDELAY; | |
43 | configPcf.OffsetWidth = PCF7931_DEFAULT_OFFSET_WIDTH; | |
44 | configPcf.OffsetPosition = PCF7931_DEFAULT_OFFSET_POSITION; | |
45 | return 0; | |
46 | } | |
47 | ||
48 | int pcf7931_printConfig(){ | |
49 | PrintAndLog("Password (LSB first on bytes) : %s", sprint_hex( configPcf.Pwd, sizeof(configPcf.Pwd))); | |
50 | PrintAndLog("Tag initialization delay : %d us", configPcf.InitDelay); | |
51 | PrintAndLog("Offset low pulses width : %d us", configPcf.OffsetWidth); | |
52 | PrintAndLog("Offset low pulses position : %d us", configPcf.OffsetPosition); | |
53 | return 0; | |
54 | } | |
55 | ||
56 | int usage_pcf7931_read(){ | |
57 | PrintAndLog("Usage: lf pcf7931 read [h] "); | |
58 | PrintAndLog("This command tries to read a PCF7931 tag."); | |
59 | PrintAndLog("Options:"); | |
60 | PrintAndLog(" h This help"); | |
61 | PrintAndLog("Examples:"); | |
62 | PrintAndLog(" lf pcf7931 read"); | |
63 | return 0; | |
64 | } | |
65 | ||
66 | int usage_pcf7931_write(){ | |
67 | PrintAndLog("Usage: lf pcf7931 write [h] <block address> <byte address> <data>"); | |
68 | PrintAndLog("This command tries to write a PCF7931 tag."); | |
69 | PrintAndLog("Options:"); | |
70 | PrintAndLog(" h This help"); | |
71 | PrintAndLog(" blockaddress Block to save [0-7]"); | |
72 | PrintAndLog(" byteaddress Index of byte inside block to write [0-15]"); | |
73 | PrintAndLog(" data one byte of data (hex)"); | |
74 | PrintAndLog("Examples:"); | |
75 | PrintAndLog(" lf pcf7931 write 2 1 FF"); | |
76 | return 0; | |
77 | } | |
78 | ||
79 | int usage_pcf7931_config(){ | |
80 | PrintAndLog("Usage: lf pcf7931 config [h] [r] <pwd> <delay> <offset width> <offset position>"); | |
81 | PrintAndLog("This command tries to set the configuration used with PCF7931 commands"); | |
82 | PrintAndLog("The time offsets could be useful to correct slew rate generated by the antenna"); | |
83 | PrintAndLog("Caling without some parameter will print the current configuration."); | |
84 | PrintAndLog("Options:"); | |
85 | PrintAndLog(" h This help"); | |
86 | PrintAndLog(" r Reset configuration to default values"); | |
87 | PrintAndLog(" pwd Password, hex, 7bytes, LSB-order"); | |
88 | PrintAndLog(" delay Tag initialization delay (in us) decimal"); | |
89 | PrintAndLog(" offset Low pulses width (in us) decimal"); | |
90 | PrintAndLog(" offset Low pulses position (in us) decimal"); | |
91 | PrintAndLog("Examples:"); | |
92 | PrintAndLog(" lf pcf7931 config"); | |
93 | PrintAndLog(" lf pcf7931 config r"); | |
94 | PrintAndLog(" lf pcf7931 config 11223344556677 20000"); | |
95 | PrintAndLog(" lf pcf7931 config 11223344556677 17500 -10 30"); | |
96 | return 0; | |
97 | } | |
98 | ||
99 | int CmdLFPCF7931Read(const char *Cmd){ | |
100 | ||
101 | uint8_t ctmp = param_getchar(Cmd, 0); | |
102 | if ( ctmp == 'H' || ctmp == 'h' ) return usage_pcf7931_read(); | |
103 | ||
104 | UsbCommand resp; | |
105 | UsbCommand c = {CMD_PCF7931_READ, {0, 0, 0}}; | |
106 | clearCommandBuffer(); | |
107 | SendCommand(&c); | |
108 | if ( !WaitForResponseTimeout(CMD_ACK, &resp, 2500) ) { | |
109 | PrintAndLog("command execution time out"); | |
110 | return 1; | |
111 | } | |
112 | return 0; | |
113 | } | |
114 | ||
115 | int CmdLFPCF7931Config(const char *Cmd){ | |
116 | ||
117 | uint8_t ctmp = param_getchar(Cmd, 0); | |
118 | if ( ctmp == 0) return pcf7931_printConfig(); | |
119 | if ( ctmp == 'H' || ctmp == 'h' ) return usage_pcf7931_config(); | |
120 | if ( ctmp == 'R' || ctmp == 'r' ) return pcf7931_resetConfig(); | |
121 | ||
122 | if ( param_gethex(Cmd, 0, configPcf.Pwd, 14) ) return usage_pcf7931_config(); | |
123 | ||
124 | configPcf.InitDelay = (param_get32ex(Cmd,1,0,10) & 0xFFFF); | |
125 | configPcf.OffsetWidth = (int)(param_get32ex(Cmd,2,0,10) & 0xFFFF); | |
126 | configPcf.OffsetPosition = (int)(param_get32ex(Cmd,3,0,10) & 0xFFFF); | |
127 | ||
128 | pcf7931_printConfig(); | |
129 | return 0; | |
130 | } | |
131 | ||
132 | int CmdLFPCF7931Write(const char *Cmd){ | |
133 | ||
134 | uint8_t ctmp = param_getchar(Cmd, 0); | |
135 | if (strlen(Cmd) < 1 || ctmp == 'h' || ctmp == 'H') return usage_pcf7931_write(); | |
136 | ||
137 | uint8_t block = 0, bytepos = 0, data = 0; | |
138 | ||
139 | if ( param_getdec(Cmd, 0, &block) ) return usage_pcf7931_write(); | |
140 | if ( param_getdec(Cmd, 1, &bytepos) ) return usage_pcf7931_write(); | |
141 | ||
142 | if ( (block > 7) || (bytepos > 15) ) return usage_pcf7931_write(); | |
143 | ||
144 | data = param_get8ex(Cmd, 2, 0, 16); | |
145 | ||
146 | PrintAndLog("Writing block: %d", block); | |
147 | PrintAndLog(" pos: %d", bytepos); | |
148 | PrintAndLog(" data: 0x%02X", data); | |
149 | ||
150 | UsbCommand c = {CMD_PCF7931_WRITE, { block, bytepos, data} }; | |
151 | memcpy(c.d.asDwords, configPcf.Pwd, sizeof(configPcf.Pwd) ); | |
152 | c.d.asDwords[7] = (configPcf.OffsetWidth + 128); | |
153 | c.d.asDwords[8] = (configPcf.OffsetPosition + 128); | |
154 | c.d.asDwords[9] = configPcf.InitDelay; | |
155 | ||
156 | clearCommandBuffer(); | |
157 | SendCommand(&c); | |
158 | //no ack? | |
159 | return 0; | |
160 | } | |
161 | ||
162 | static command_t CommandTable[] = | |
163 | { | |
164 | {"help", CmdHelp, 1, "This help"}, | |
165 | {"read", CmdLFPCF7931Read, 0, "Read content of a PCF7931 transponder"}, | |
166 | {"write", CmdLFPCF7931Write, 0, "Write data on a PCF7931 transponder."}, | |
167 | {"config", CmdLFPCF7931Config, 1, "Configure the password, the tags initialization delay and time offsets (optional)"}, | |
168 | {NULL, NULL, 0, NULL} | |
169 | }; | |
170 | ||
171 | int CmdLFPCF7931(const char *Cmd) | |
172 | { | |
173 | CmdsParse(CommandTable, Cmd); | |
174 | return 0; | |
175 | } | |
176 | ||
177 | int CmdHelp(const char *Cmd) | |
178 | { | |
179 | CmdsHelp(CommandTable); | |
180 | return 0; | |
181 | } |