1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2012 Chalk <chalk.secu at gmail.com>
3 // 2015 Dake <thomas.cayrou at gmail.com>
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
8 //-----------------------------------------------------------------------------
9 // Low frequency PCF7931 commands
10 //-----------------------------------------------------------------------------
13 #include "proxmark3.h"
16 #include "cmdparser.h"
20 #include "cmdlfpcf7931.h"
22 static int CmdHelp(const char *Cmd
);
24 #define PCF7931_DEFAULT_INITDELAY 17500
25 #define PCF7931_DEFAULT_OFFSET_WIDTH 0
26 #define PCF7931_DEFAULT_OFFSET_POSITION 0
28 // Default values - Configuration
29 struct pcf7931_config configPcf
= {
30 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
31 PCF7931_DEFAULT_INITDELAY
,
33 PCF7931_DEFAULT_OFFSET_WIDTH
,
34 PCF7931_DEFAULT_OFFSET_POSITION
38 // Resets the configuration settings to default values.
39 int pcf7931_resetConfig(){
40 memset(configPcf
.Pwd
, 0xFF, sizeof(configPcf
.Pwd
) );
41 configPcf
.InitDelay
= PCF7931_DEFAULT_INITDELAY
;
42 configPcf
.Offset
[0] = PCF7931_DEFAULT_OFFSET_WIDTH
;
43 configPcf
.Offset
[1] = PCF7931_DEFAULT_OFFSET_POSITION
;
47 int pcf7931_printConfig(){
48 PrintAndLog("Password (LSB first on bytes) : %s", sprint_hex( configPcf
.Pwd
, sizeof(configPcf
.Pwd
)));
49 PrintAndLog("Tag initialization delay : %d us", configPcf
.InitDelay
);
50 PrintAndLog("Offset low pulses width : %d us", configPcf
.Offset
[0]);
51 PrintAndLog("Offset low pulses position : %d us", configPcf
.Offset
[1]);
55 int usage_pcf7931_read(){
56 PrintAndLog("Usage: lf pcf7931 read [h] ");
57 PrintAndLog("This command tries to read a PCF7931 tag.");
58 PrintAndLog("Options:");
59 PrintAndLog(" h This help");
60 PrintAndLog("Examples:");
61 PrintAndLog(" lf pcf7931 read");
65 int usage_pcf7931_write(){
66 PrintAndLog("Usage: lf pcf7931 write [h] <block address> <byte address> <data>");
67 PrintAndLog("This command tries to write a PCF7931 tag.");
68 PrintAndLog("Options:");
69 PrintAndLog(" h This help");
70 PrintAndLog(" blockaddress Block to save");
71 PrintAndLog(" byteaddress Index of byte inside block to overwrite");
72 PrintAndLog(" data one byte of data");
73 PrintAndLog("Examples:");
74 PrintAndLog(" lf pcf7931 write 10 1 FF");
78 int usage_pcf7931_config(){
79 PrintAndLog("Usage: lf pcf7931 config [h] [r] <pwd> <delay> <offset width> <offset position>");
80 PrintAndLog("This command tries to set the configuration used with PCF7931 commands");
81 PrintAndLog("The time offsets could be useful to correct slew rate generated by the antenna");
82 PrintAndLog("Caling without some parameter will print the current configuration.");
83 PrintAndLog("Options:");
84 PrintAndLog(" h This help");
85 PrintAndLog(" r Reset configuration to default values");
86 PrintAndLog(" pwd Password, hex, 7bytes, LSB-order");
87 PrintAndLog(" delay Tag initialization delay (in us) decimal");
88 PrintAndLog(" offset Low pulses width (in us) decimal");
89 PrintAndLog(" offset Low pulses position (in us) decimal");
90 PrintAndLog("Examples:");
91 PrintAndLog(" lf pcf7931 config");
92 PrintAndLog(" lf pcf7931 config r");
93 PrintAndLog(" lf pcf7931 config 11223344556677 20000");
94 PrintAndLog(" lf pcf7931 config 11223344556677 17500 -10 30");
98 int CmdLFPCF7931Read(const char *Cmd
){
100 uint8_t ctmp
= param_getchar(Cmd
, 0);
101 if ( ctmp
== 'H' || ctmp
== 'h' ) return usage_pcf7931_read();
104 UsbCommand c
= {CMD_PCF7931_READ
, {0, 0, 0}};
105 clearCommandBuffer();
107 if ( !WaitForResponseTimeout(CMD_ACK
, &resp
, 2500) ) {
108 PrintAndLog("command execution time out");
114 int CmdLFPCF7931Config(const char *Cmd
){
116 uint8_t ctmp
= param_getchar(Cmd
, 0);
117 if ( ctmp
== 0) return pcf7931_printConfig();
118 if ( ctmp
== 'H' || ctmp
== 'h' ) return usage_pcf7931_config();
119 if ( ctmp
== 'R' || ctmp
== 'r' ) return pcf7931_resetConfig();
121 if ( param_gethex(Cmd
, 0, configPcf
.Pwd
, 14) ) return usage_pcf7931_config();
123 configPcf
.InitDelay
= (param_get32ex(Cmd
,1,0,10) & 0xFFFF);
124 configPcf
.Offset
[0] = (int)(param_get32ex(Cmd
,2,0,10) & 0xFFFF);
125 configPcf
.Offset
[1] = (int)(param_get32ex(Cmd
,3,0,10) & 0xFFFF);
127 pcf7931_printConfig();
131 int CmdLFPCF7931Write(const char *Cmd
){
133 uint8_t ctmp
= param_getchar(Cmd
, 0);
134 if (strlen(Cmd
) < 1 || ctmp
== 'h' || ctmp
== 'H') return usage_pcf7931_write();
136 uint64_t blockaddress
= param_get64ex(Cmd
, 0, 0, 16);
137 uint64_t byteaddress
= param_get64ex(Cmd
, 1, 0, 16);
138 uint8_t data
= param_get8ex(Cmd
,2,0,16);
140 PrintAndLog("Please specify the block address in hex");
141 PrintAndLog("Please specify the byte address in hex");
142 PrintAndLog("Please specify the data in hex (1 byte)");
144 PrintAndLog("", blockaddress
, byteaddress
, data
);
147 UsbCommand c
= {CMD_PCF7931_WRITE
, { blockaddress
, byteaddress
, data
} };
148 memcpy(c
.d
.asDwords
, configPcf
.Pwd
, 7);
149 c
.d
.asDwords
[7] = (configPcf
.Offset
[0]+128);
150 c
.d
.asDwords
[8] = (configPcf
.Offset
[1]+128);
151 c
.d
.asDwords
[9] = configPcf
.InitDelay
;
153 clearCommandBuffer();
159 static command_t CommandTable
[] =
161 {"help", CmdHelp
, 1, "This help"},
162 {"read", CmdLFPCF7931Read
, 1, "Read content of a PCF7931 transponder"},
163 {"write", CmdLFPCF7931Write
, 1, "Write data on a PCF7931 transponder."},
164 {"config", CmdLFPCF7931Config
, 1, "Configure the password, the tags initialization delay and time offsets (optional)"},
165 {NULL
, NULL
, 0, NULL
}
168 int CmdLFPCF7931(const char *Cmd
)
170 CmdsParse(CommandTable
, Cmd
);
174 int CmdHelp(const char *Cmd
)
176 CmdsHelp(CommandTable
);