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