proxmark3/client/cmdparser.c

279 lines
7.7 KiB
C
Raw Normal View History

//-----------------------------------------------------------------------------
// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
//
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
// at your option, any later version. See the LICENSE.txt file for the text of
// the license.
//-----------------------------------------------------------------------------
// Command parser
//-----------------------------------------------------------------------------
#include "cmdparser.h"
Client cleanup and restructuring. Stage 1... Next Step is refactoring some of the giant functions which are just copy/paste of some other ones with just a few line changes, removing unnecessary 'goto' etc. The MS Windows version is broken with this commit but will be fixed soon. Everything can't be done all at once :P The commands are now hierarchical, for example: "hf 14a read" vs. "hf 14b read". You can also request help: "hf help", "data help", "hf 15 help" etc. Indents are now space-based, not tab-based anymore. Hopefully no one will be trolling about it, considering the suicide-prone work being done here ;) client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h, client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h, client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c, client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h, client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h, client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h, client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h, client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h, client/cmdhflegic.c, client/cmdhflegic.c: New files. client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c, client/proxmark3.h, client/Makefile: Update accordingly. client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes. client/translate.h, client/command.c, client/gui.c, client/usb.c, client/prox.h: Remove. include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd. common/crc16.h: New file. common/crc16.c: Modify accordingly. common/iso14443crc.h: New file. common/iso14443_crc.c: Rename to common/iso14443crc.c: and modify accordingly. armsrc/lfops.c, armsrc/iso14443.c, armsrc/iso14443a.c: include .h files from the common directory instead of including the c files. common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
#include <stdio.h>
#include <string.h>
Client cleanup and restructuring. Stage 1... Next Step is refactoring some of the giant functions which are just copy/paste of some other ones with just a few line changes, removing unnecessary 'goto' etc. The MS Windows version is broken with this commit but will be fixed soon. Everything can't be done all at once :P The commands are now hierarchical, for example: "hf 14a read" vs. "hf 14b read". You can also request help: "hf help", "data help", "hf 15 help" etc. Indents are now space-based, not tab-based anymore. Hopefully no one will be trolling about it, considering the suicide-prone work being done here ;) client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h, client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h, client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c, client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h, client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h, client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h, client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h, client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h, client/cmdhflegic.c, client/cmdhflegic.c: New files. client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c, client/proxmark3.h, client/Makefile: Update accordingly. client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes. client/translate.h, client/command.c, client/gui.c, client/usb.c, client/prox.h: Remove. include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd. common/crc16.h: New file. common/crc16.c: Modify accordingly. common/iso14443crc.h: New file. common/iso14443_crc.c: Rename to common/iso14443crc.c: and modify accordingly. armsrc/lfops.c, armsrc/iso14443.c, armsrc/iso14443a.c: include .h files from the common directory instead of including the c files. common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
#include "ui.h"
#include "comms.h"
Client cleanup and restructuring. Stage 1... Next Step is refactoring some of the giant functions which are just copy/paste of some other ones with just a few line changes, removing unnecessary 'goto' etc. The MS Windows version is broken with this commit but will be fixed soon. Everything can't be done all at once :P The commands are now hierarchical, for example: "hf 14a read" vs. "hf 14b read". You can also request help: "hf help", "data help", "hf 15 help" etc. Indents are now space-based, not tab-based anymore. Hopefully no one will be trolling about it, considering the suicide-prone work being done here ;) client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h, client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h, client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c, client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h, client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h, client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h, client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h, client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h, client/cmdhflegic.c, client/cmdhflegic.c: New files. client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c, client/proxmark3.h, client/Makefile: Update accordingly. client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes. client/translate.h, client/command.c, client/gui.c, client/usb.c, client/prox.h: Remove. include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd. common/crc16.h: New file. common/crc16.c: Modify accordingly. common/iso14443crc.h: New file. common/iso14443_crc.c: Rename to common/iso14443crc.c: and modify accordingly. armsrc/lfops.c, armsrc/iso14443.c, armsrc/iso14443a.c: include .h files from the common directory instead of including the c files. common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
bool AlwaysAvailable(void) {
return true;
}
bool IfPm3Present(void) {
if (session.help_dump_mode)
return false;
return session.pm3_present;
}
2019-05-02 03:09:23 +08:00
bool IfPm3Flash(void) {
if (!IfPm3Present())
return false;
if (!pm3_capabilities.compiled_with_flash)
return false;
return pm3_capabilities.hw_available_flash;
2019-05-02 03:09:23 +08:00
}
bool IfPm3Smartcard(void) {
if (!IfPm3Present())
return false;
if (!pm3_capabilities.compiled_with_smartcard)
return false;
return pm3_capabilities.hw_available_smartcard;
2019-05-02 03:09:23 +08:00
}
bool IfPm3FpcUsart(void) {
2019-05-02 03:09:23 +08:00
if (!IfPm3Present())
return false;
return pm3_capabilities.compiled_with_fpc_usart;
2019-05-02 03:09:23 +08:00
}
bool IfPm3FpcUsartHost(void) {
2019-05-02 03:09:23 +08:00
if (!IfPm3Present())
return false;
return pm3_capabilities.compiled_with_fpc_usart_host;
}
bool IfPm3FpcUsartHostFromUsb(void) {
// true if FPC USART Host support and if talking from USB-CDC interface
if (!IfPm3Present())
return false;
if (!pm3_capabilities.compiled_with_fpc_usart_host)
return false;
return !conn.send_via_fpc_usart;
}
bool IfPm3FpcUsartDevFromUsb(void) {
// true if FPC USART developer support and if talking from USB-CDC interface
if (!IfPm3Present())
return false;
if (!pm3_capabilities.compiled_with_fpc_usart_dev)
return false;
return !conn.send_via_fpc_usart;
2019-05-02 03:09:23 +08:00
}
bool IfPm3FpcUsartFromUsb(void) {
// true if FPC USART Host or developer support and if talking from USB-CDC interface
return IfPm3FpcUsartHostFromUsb() || IfPm3FpcUsartDevFromUsb();
}
2019-05-02 03:09:23 +08:00
bool IfPm3Lf(void) {
if (!IfPm3Present())
return false;
return pm3_capabilities.compiled_with_lf;
}
bool IfPm3Hitag(void) {
if (!IfPm3Present())
return false;
return pm3_capabilities.compiled_with_hitag;
}
bool IfPm3Hfsniff(void) {
if (!IfPm3Present())
return false;
return pm3_capabilities.compiled_with_hfsniff;
}
2019-05-02 03:09:23 +08:00
bool IfPm3Iso14443a(void) {
if (!IfPm3Present())
return false;
return pm3_capabilities.compiled_with_iso14443a;
}
2019-05-02 03:31:34 +08:00
bool IfPm3Iso14443b(void) {
2019-05-02 03:09:23 +08:00
if (!IfPm3Present())
return false;
return pm3_capabilities.compiled_with_iso14443b;
}
bool IfPm3Iso14443(void) {
if (!IfPm3Present())
return false;
return pm3_capabilities.compiled_with_iso14443a || pm3_capabilities.compiled_with_iso14443b;
}
2019-05-02 03:09:23 +08:00
bool IfPm3Iso15693(void) {
if (!IfPm3Present())
return false;
return pm3_capabilities.compiled_with_iso15693;
}
bool IfPm3Felica(void) {
if (!IfPm3Present())
return false;
return pm3_capabilities.compiled_with_felica;
}
bool IfPm3Legicrf(void) {
if (!IfPm3Present())
return false;
return pm3_capabilities.compiled_with_legicrf;
}
bool IfPm3Iclass(void) {
if (!IfPm3Present())
return false;
return pm3_capabilities.compiled_with_iclass;
}
2019-08-04 03:17:52 +08:00
bool IfPm3NfcBarcode(void) {
if (!IfPm3Present())
return false;
return pm3_capabilities.compiled_with_nfcbarcode;
}
2019-05-02 03:09:23 +08:00
bool IfPm3Lcd(void) {
if (!IfPm3Present())
return false;
return pm3_capabilities.compiled_with_lcd;
}
void CmdsHelp(const command_t Commands[]) {
2019-03-10 06:35:06 +08:00
if (Commands[0].Name == NULL) return;
int i = 0;
while (Commands[i].Name) {
if (Commands[i].IsAvailable()) {
g_printAndLog = PRINTANDLOG_PRINT;
PrintAndLogEx(NORMAL, _GREEN_("%-16s")" %s", Commands[i].Name, Commands[i].Help);
g_printAndLog = PRINTANDLOG_PRINT | PRINTANDLOG_LOG;
}
2019-03-10 06:35:06 +08:00
++i;
}
Client cleanup and restructuring. Stage 1... Next Step is refactoring some of the giant functions which are just copy/paste of some other ones with just a few line changes, removing unnecessary 'goto' etc. The MS Windows version is broken with this commit but will be fixed soon. Everything can't be done all at once :P The commands are now hierarchical, for example: "hf 14a read" vs. "hf 14b read". You can also request help: "hf help", "data help", "hf 15 help" etc. Indents are now space-based, not tab-based anymore. Hopefully no one will be trolling about it, considering the suicide-prone work being done here ;) client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h, client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h, client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c, client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h, client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h, client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h, client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h, client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h, client/cmdhflegic.c, client/cmdhflegic.c: New files. client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c, client/proxmark3.h, client/Makefile: Update accordingly. client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes. client/translate.h, client/command.c, client/gui.c, client/usb.c, client/prox.h: Remove. include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd. common/crc16.h: New file. common/crc16.c: Modify accordingly. common/iso14443crc.h: New file. common/iso14443_crc.c: Rename to common/iso14443crc.c: and modify accordingly. armsrc/lfops.c, armsrc/iso14443.c, armsrc/iso14443a.c: include .h files from the common directory instead of including the c files. common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
}
int CmdsParse(const command_t Commands[], const char *Cmd) {
2019-03-10 06:35:06 +08:00
// Help dump children
2019-03-10 07:00:59 +08:00
if (strcmp(Cmd, "XX_internal_command_dump_XX") == 0) {
2019-03-10 06:35:06 +08:00
dumpCommandsRecursive(Commands, 0);
2019-04-19 05:26:12 +08:00
return PM3_SUCCESS;
2019-03-10 06:35:06 +08:00
}
// Markdown help dump children
2019-03-10 07:00:59 +08:00
if (strcmp(Cmd, "XX_internal_command_dump_markdown_XX") == 0) {
2019-03-10 06:35:06 +08:00
dumpCommandsRecursive(Commands, 1);
2019-04-19 05:26:12 +08:00
return PM3_SUCCESS;
2019-03-10 06:35:06 +08:00
}
char cmd_name[128];
int len = 0;
memset(cmd_name, 0, sizeof(cmd_name));
sscanf(Cmd, "%127s%n", cmd_name, &len);
str_lower(cmd_name);
2019-09-05 05:13:51 +08:00
// Comment
if (cmd_name[0] == '#')
return PM3_SUCCESS;
2019-03-10 06:35:06 +08:00
int i = 0;
2019-05-01 05:52:40 +08:00
while (Commands[i].Name) {
if (0 == strcmp(Commands[i].Name, cmd_name)) {
if (Commands[i].IsAvailable()) {
2019-05-01 05:52:40 +08:00
break;
} else {
PrintAndLogEx(WARNING, "This command is " _YELLOW_("not available") "in this mode");
2019-05-01 05:52:40 +08:00
return PM3_ENOTIMPL;
}
}
2019-03-10 06:35:06 +08:00
++i;
2019-05-01 05:52:40 +08:00
}
2019-03-10 06:35:06 +08:00
/* try to find exactly one prefix-match */
if (!Commands[i].Name) {
int last_match = 0;
int matches = 0;
2019-03-10 07:00:59 +08:00
for (i = 0; Commands[i].Name; i++) {
if (!strncmp(Commands[i].Name, cmd_name, strlen(cmd_name)) && Commands[i].IsAvailable()) {
2019-03-10 06:35:06 +08:00
last_match = i;
matches++;
}
}
if (matches == 1) i = last_match;
}
if (Commands[i].Name) {
while (Cmd[len] == ' ')
2019-03-10 07:00:59 +08:00
++len;
2019-03-10 06:35:06 +08:00
return Commands[i].Parse(Cmd + len);
} else {
// show help for selected hierarchy or if command not recognised
CmdsHelp(Commands);
}
2019-04-19 05:26:12 +08:00
return PM3_SUCCESS;
Client cleanup and restructuring. Stage 1... Next Step is refactoring some of the giant functions which are just copy/paste of some other ones with just a few line changes, removing unnecessary 'goto' etc. The MS Windows version is broken with this commit but will be fixed soon. Everything can't be done all at once :P The commands are now hierarchical, for example: "hf 14a read" vs. "hf 14b read". You can also request help: "hf help", "data help", "hf 15 help" etc. Indents are now space-based, not tab-based anymore. Hopefully no one will be trolling about it, considering the suicide-prone work being done here ;) client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h, client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h, client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c, client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h, client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h, client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h, client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h, client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h, client/cmdhflegic.c, client/cmdhflegic.c: New files. client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c, client/proxmark3.h, client/Makefile: Update accordingly. client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes. client/translate.h, client/command.c, client/gui.c, client/usb.c, client/prox.h: Remove. include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd. common/crc16.h: New file. common/crc16.c: Modify accordingly. common/iso14443crc.h: New file. common/iso14443_crc.c: Rename to common/iso14443crc.c: and modify accordingly. armsrc/lfops.c, armsrc/iso14443.c, armsrc/iso14443a.c: include .h files from the common directory instead of including the c files. common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
}
char pparent[512] = {0};
char *parent = pparent;
void dumpCommandsRecursive(const command_t cmds[], int markdown) {
2019-03-10 06:35:06 +08:00
if (cmds[0].Name == NULL) return;
int i = 0;
int w_cmd = 25;
int w_off = 8;
// First, dump all single commands, which are not a container for
// other commands
if (markdown) {
PrintAndLogEx(NORMAL, "|%-*s|%-*s|%s", w_cmd, "command", w_off, "offline", "description");
PrintAndLogEx(NORMAL, "|%-*s|%-*s|%s", w_cmd, "-------", w_off, "-------", "-----------");
2019-03-10 06:35:06 +08:00
} else {
PrintAndLogEx(NORMAL, "%-*s|%-*s|%s", w_cmd, "command", w_off, "offline", "description");
PrintAndLogEx(NORMAL, "%-*s|%-*s|%s", w_cmd, "-------", w_off, "-------", "-----------");
2019-03-10 06:35:06 +08:00
}
while (cmds[i].Name) {
const char *cmd_offline = "N";
2019-03-10 06:35:06 +08:00
if (cmds[i].Help[0] == '{' && ++i) continue;
if (cmds[i].IsAvailable())
2019-03-10 06:35:06 +08:00
cmd_offline = "Y";
if (markdown)
PrintAndLogEx(NORMAL, "|`%s%-*s`|%-*s|`%s`", parent, w_cmd - (int)strlen(parent) - 2, cmds[i].Name, w_off, cmd_offline, cmds[i].Help);
2019-03-10 06:35:06 +08:00
else
PrintAndLogEx(NORMAL, "%s%-*s|%-*s|%s", parent, w_cmd - (int)strlen(parent), cmds[i].Name, w_off, cmd_offline, cmds[i].Help);
2019-03-10 06:35:06 +08:00
++i;
}
PrintAndLogEx(NORMAL, "\n");
2019-03-10 06:35:06 +08:00
i = 0;
// Then, print the categories. These will go into subsections with their own tables
while (cmds[i].Name) {
2019-03-10 07:00:59 +08:00
if (cmds[i].Help[0] != '{' && ++i) continue;
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "### %s%s\n\n %s\n", parent, cmds[i].Name, cmds[i].Help);
2019-03-10 06:35:06 +08:00
char currentparent[512] = {0};
snprintf(currentparent, sizeof currentparent, "%s%s ", parent, cmds[i].Name);
char *old_parent = parent;
parent = currentparent;
// This is what causes the recursion, since commands Parse-implementation
// in turn calls the CmdsParse above.
if (markdown)
2019-03-10 07:00:59 +08:00
cmds[i].Parse("XX_internal_command_dump_markdown_XX");
2019-03-10 06:35:06 +08:00
else
2019-03-10 07:00:59 +08:00
cmds[i].Parse("XX_internal_command_dump_XX");
2019-03-10 06:35:06 +08:00
parent = old_parent;
++i;
}
}