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