usart the rest - now uses cliparser

This commit is contained in:
iceman1001 2021-01-07 11:33:36 +01:00
commit eba87bdd4b
2 changed files with 135 additions and 246 deletions

View file

@ -21,89 +21,8 @@
#include "usart_defs.h"
#include "ui.h" // PrintAndLog
static int CmdHelp(const char *Cmd);
static int usage_usart_txhex(void) {
PrintAndLogEx(NORMAL, "Send bytes over USART");
PrintAndLogEx(NORMAL, _RED_("WARNING: it will have side-effects if used in USART HOST mode!"));
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Usage: usart txhex [h] d <bytes>");
PrintAndLogEx(NORMAL, "Options:");
PrintAndLogEx(NORMAL, " h This help");
PrintAndLogEx(NORMAL, " d <bytes> bytes to send");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Examples:");
PrintAndLogEx(NORMAL, " usart txhex d 504d33620a80000000010100f09f988ef09fa5b36233");
PrintAndLogEx(NORMAL, "expected output: nothing");
return PM3_SUCCESS;
}
static int usage_usart_rx(void) {
PrintAndLogEx(NORMAL, "Receive string over USART [t <timeout>]");
PrintAndLogEx(NORMAL, _RED_("WARNING: it will have side-effects if used in USART HOST mode!"));
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Usage: usart rx [h]");
PrintAndLogEx(NORMAL, "Options:");
PrintAndLogEx(NORMAL, " h This help");
PrintAndLogEx(NORMAL, " t <timeout> timeout in ms, default is 0ms");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "expected output: Received string");
return PM3_SUCCESS;
}
static int usage_usart_rxhex(void) {
PrintAndLogEx(NORMAL, "Receive bytes over USART");
PrintAndLogEx(NORMAL, _RED_("WARNING: it will have side-effects if used in USART HOST mode!"));
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Usage: usart rxhex [h] [t <timeout>]");
PrintAndLogEx(NORMAL, "Options:");
PrintAndLogEx(NORMAL, " h This help");
PrintAndLogEx(NORMAL, " t <timeout> timeout in ms, default is 0ms");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "expected output: Received bytes");
return PM3_SUCCESS;
}
static int usage_usart_txrx(void) {
PrintAndLogEx(NORMAL, "Send string over USART and wait for response");
PrintAndLogEx(NORMAL, _YELLOW_("WARNING: if used in USART HOST mode, you can only send AT commands"));
PrintAndLogEx(NORMAL, _YELLOW_("to add-on when BT connection is not established (LED needs to be blinking)"));
PrintAndLogEx(NORMAL, _RED_("Any other usage in USART HOST mode will have side-effects!"));
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Usage: usart txrx [h] [t <timeout>] d \"string\"");
PrintAndLogEx(NORMAL, "Options:");
PrintAndLogEx(NORMAL, " h This help");
PrintAndLogEx(NORMAL, " t <timeout> timeout in ms, default is 1000ms");
PrintAndLogEx(NORMAL, " d string string to send");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Examples:");
PrintAndLogEx(NORMAL, " Talking to the BT add-on (when no connection):");
PrintAndLogEx(NORMAL, " usart txrx d \"AT+VERSION\"");
PrintAndLogEx(NORMAL, " Talking to a target requiring longer time and end-of-line chars:");
PrintAndLogEx(NORMAL, " usart txrx t 2000 d \"AT+SOMESTUFF\\r\\n\"");
PrintAndLogEx(NORMAL, "expected output: Received string");
return PM3_SUCCESS;
}
static int usage_usart_config(void) {
PrintAndLogEx(NORMAL, "Configure USART");
PrintAndLogEx(NORMAL, _RED_("WARNING: it will have side-effects if used in USART HOST mode!"));
PrintAndLogEx(NORMAL, "The changes are not permanent, restart Proxmark3 to get default settings back.");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Usage: usart config [h] [b <baudrate>] [p <N|O|E>]");
PrintAndLogEx(NORMAL, "Options:");
PrintAndLogEx(NORMAL, " h This help");
PrintAndLogEx(NORMAL, " b <baudrate> Baudrate");
PrintAndLogEx(NORMAL, " p <N|O|E> Parity (None/Odd/Even)");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Examples:");
PrintAndLogEx(NORMAL, " usart config b 9600");
PrintAndLogEx(NORMAL, " usart config b 9600 p N");
PrintAndLogEx(NORMAL, " usart config p E");
return PM3_SUCCESS;
}
static int usart_tx(uint8_t *data, size_t len) {
clearCommandBuffer();
SendCommandNG(CMD_USART_TX, data, len);
@ -174,51 +93,45 @@ static int set_usart_config(uint32_t baudrate, uint8_t parity) {
}
static int CmdUsartConfig(const char *Cmd) {
uint8_t cmdp = 0;
bool errors = false;
uint32_t baudrate = 0;
uint8_t parity = 0;
while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch (tolower(param_getchar(Cmd, cmdp))) {
case 'h':
return usage_usart_config();
case 'p':
switch (tolower(param_getchar(Cmd, cmdp + 1))) {
case 'n':
parity = 'N';
break;
case 'o':
parity = 'O';
break;
case 'e':
parity = 'E';
break;
default:
PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp + 1));
errors = true;
break;
}
cmdp += 2;
break;
case 'b':
baudrate = param_get32ex(Cmd, cmdp + 1, 0, 10);
if (baudrate == 0) {
PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp + 1));
errors = true;
}
cmdp += 2;
break;
default:
PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
errors = true;
break;
}
}
//Validations
if (errors || ((baudrate == 0) && (parity == 0))) {
usage_usart_config();
CLIParserContext *ctx;
CLIParserInit(&ctx, "usart config",
"Configure USART.\n"
"WARNING: it will have side-effects if used in USART HOST mode!\n"
"The changes are not permanent, restart Proxmark3 to get default settings back.",
"usart config -b 9600\n"
"usart config -b 9600 --none\n"
"usart config -E"
);
void *argtable[] = {
arg_param_begin,
arg_u64_0("b", "baud", "<dec>", "baudrate"),
arg_lit0("N", "none", "mone parity"),
arg_lit0("E", "even", "even parity"),
arg_lit0("O", "odd", "odd parity"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, false);
uint32_t baudrate = arg_get_u32_def(ctx, 1, 0);
bool pn = arg_get_lit(ctx, 2);
bool pe = arg_get_lit(ctx, 3);
bool po = arg_get_lit(ctx, 4);
CLIParserFree(ctx);
if ((pn + pe + po) > 1 ) {
PrintAndLogEx(WARNING, "Only one parity can be used at a time");
return PM3_EINVARG;
}
uint8_t parity = 0;
if (pn)
parity = 'N';
else if (po)
parity = 'O';
else if (pe)
parity = 'E';
return set_usart_config(baudrate, parity);
}
@ -543,162 +456,143 @@ static int CmdUsartTX(const char *Cmd) {
}
static int CmdUsartRX(const char *Cmd) {
uint8_t cmdp = 0;
bool errors = false;
uint32_t waittime = 0;
while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch (tolower(param_getchar(Cmd, cmdp))) {
case 'h':
return usage_usart_rx();
case 't':
waittime = param_get32ex(Cmd, cmdp + 1, 0, 10);
cmdp += 2;
break;
default:
PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
errors = true;
break;
}
}
//Validations
if (errors) {
usage_usart_rx();
return PM3_EINVARG;
}
CLIParserContext *ctx;
CLIParserInit(&ctx, "usart rx",
"Receive string over USART.\n"
"WARNING: it will have side-effects if used in USART HOST mode!\n",
"usart rx -t 2000 -> 2 second timeout"
);
void *argtable[] = {
arg_param_begin,
arg_u64_0("t", "timeout", "<dec>", "timeout in ms, default is 0ms"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
uint32_t waittime = arg_get_u32_def(ctx, 1, 0);
CLIParserFree(ctx);
uint8_t data[PM3_CMD_DATA_SIZE] = {0x00};
size_t len = 0;
int ret = usart_rx(data, &len, waittime);
if (ret != PM3_SUCCESS)
return ret;
PrintAndLogEx(NORMAL, "RX:%.*s", (int)len, data);
PrintAndLogEx(SUCCESS, "RX:%.*s", (int)len, data);
return PM3_SUCCESS;
}
static int CmdUsartTXRX(const char *Cmd) {
uint8_t cmdp = 0;
bool errors = false;
char string[PM3_CMD_DATA_SIZE] = {0};
uint32_t waittime = 1000;
while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch (tolower(param_getchar(Cmd, cmdp))) {
case 'h':
return usage_usart_txrx();
case 'd':
if (param_getstr(Cmd, cmdp + 1, string, sizeof(string)) >= sizeof(string)) {
PrintAndLogEx(FAILED, "String too long");
errors = true;
break;
}
cmdp += 2;
break;
case 't':
waittime = param_get32ex(Cmd, cmdp + 1, 1000, 10);
cmdp += 2;
break;
default:
PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
errors = true;
break;
}
}
//Validations
if (errors || cmdp == 0) {
usage_usart_txrx();
return PM3_EINVARG;
}
char string2[PM3_CMD_DATA_SIZE] = {0};
size_t i2 = 0;
size_t n = strlen(string);
CLIParserContext *ctx;
CLIParserInit(&ctx, "usart txrx",
"Send string over USART and wait for response.\n"
"WARNING: if used in USART HOST mode, you can only send AT commands\n"
"to add-on when BT connection is not established (LED needs to be blinking)\n"
_RED_("Any other usage in USART HOST mode will have side-effects!"),
"usart txrx -d \"AT+VERSION\" -> Talking to BT add-on (when no connection)\n"
"usart txrx -t 2000 -d \"AT+SOMESTUFF\\r\\n\" -> Talking to a target requiring longer time and end-of-line chars"
);
void *argtable[] = {
arg_param_begin,
arg_u64_0("t", "timeout", "<dec>", "timeout in ms, default is 1000 ms"),
arg_str1("d", "data", NULL, "string to send"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
uint32_t waittime = arg_get_u32_def(ctx, 1, 1000);
int slen = 0;
char s[PM3_CMD_DATA_SIZE] = {0};
CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t *)s, sizeof(s), &slen);
CLIParserFree(ctx);
char clean[PM3_CMD_DATA_SIZE] = {0};
size_t j = 0;
size_t n = strlen(s);
for (size_t i = 0; i < n; i++) {
if ((i < n - 1) && (string[i] == '\\') && (string[i + 1] == '\\')) {
if ((i < n - 1) && (s[i] == '\\') && (s[i + 1] == '\\')) {
i++;
string2[i2++] = '\\';
clean[j++] = '\\';
continue;
}
if ((i < n - 1) && (string[i] == '\\') && (string[i + 1] == '"')) {
if ((i < n - 1) && (s[i] == '\\') && (s[i + 1] == '"')) {
i++;
string2[i2++] = '"';
clean[j++] = '"';
continue;
}
if (string[i] == '"') {
if (s[i] == '"') {
continue;
}
if ((i < n - 1) && (string[i] == '\\') && (string[i + 1] == 'r')) {
if ((i < n - 1) && (s[i] == '\\') && (s[i + 1] == 'r')) {
i++;
string2[i2++] = '\r';
clean[j++] = '\r';
continue;
}
if ((i < n - 1) && (string[i] == '\\') && (string[i + 1] == 'n')) {
if ((i < n - 1) && (s[i] == '\\') && (s[i + 1] == 'n')) {
i++;
string2[i2++] = '\n';
clean[j++] = '\n';
continue;
}
string2[i2++] = string[i];
clean[j++] = s[i];
}
uint8_t data[PM3_CMD_DATA_SIZE] = {0x00};
size_t len = 0;
PrintAndLogEx(NORMAL, "TX (%3zu):%.*s", strlen(string2), (int)strlen(string2), string2);
int ret = usart_txrx((uint8_t *)string2, strlen(string2), data, &len, waittime);
PrintAndLogEx(SUCCESS, "TX (%3zu):%.*s", strlen(clean), (int)strlen(clean), clean);
int ret = usart_txrx((uint8_t *)clean, strlen(clean), data, &len, waittime);
if (ret != PM3_SUCCESS)
return ret;
PrintAndLogEx(NORMAL, "RX (%3zu):%.*s", len, (int)len, data);
PrintAndLogEx(SUCCESS, "RX (%3zu):%.*s", len, (int)len, data);
return PM3_SUCCESS;
}
static int CmdUsartTXhex(const char *Cmd) {
int hexlen, len = 0;
uint8_t cmdp = 0;
bool errors = false;
CLIParserContext *ctx;
CLIParserInit(&ctx, "usart txhex",
"Send bytes over USART.\n"
"WARNING: it will have side-effects if used in USART HOST mode!",
"usart txhex -d 504d33620a80000000010100f09f988ef09fa5b36233"
);
void *argtable[] = {
arg_param_begin,
arg_str1("d", "data", "<hex>", "bytes to send"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
int dlen = 0;
uint8_t data[PM3_CMD_DATA_SIZE] = {0x00};
while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch (tolower(param_getchar(Cmd, cmdp))) {
case 'h':
return usage_usart_txhex();
case 'd':
if (param_gethex_ex(Cmd, cmdp + 1, data, &hexlen)) {
PrintAndLogEx(ERR, "Error parsing bytes");
return PM3_EINVARG;
}
len = hexlen >> 1;
cmdp += 2;
break;
default:
PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
errors = true;
break;
}
}
//Validations
if (errors || cmdp == 0) {
usage_usart_txhex();
int res = CLIParamHexToBuf(arg_get_str(ctx, 1), data, sizeof(data), &dlen);
CLIParserFree(ctx);
if (res) {
PrintAndLogEx(FAILED, "Error parsing bytes");
return PM3_EINVARG;
}
return usart_tx(data, len);
return usart_tx(data, dlen);
}
static int CmdUsartRXhex(const char *Cmd) {
uint8_t cmdp = 0;
bool errors = false;
uint32_t waittime = 0;
while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch (tolower(param_getchar(Cmd, cmdp))) {
case 'h':
return usage_usart_rxhex();
case 't':
waittime = param_get32ex(Cmd, cmdp + 1, 0, 10);
cmdp += 2;
break;
default:
PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
errors = true;
break;
}
}
//Validations
if (errors) {
usage_usart_rxhex();
return PM3_EINVARG;
}
CLIParserContext *ctx;
CLIParserInit(&ctx, "usart rxhex",
"Receive bytes over USART.\n"
"WARNING: it will have side-effects if used in USART HOST mode!\n",
"usart rxhex -t 2000 -> 2 second timeout"
);
void *argtable[] = {
arg_param_begin,
arg_u64_0("t", "timeout", "<dec>", "timeout in ms, default is 0ms"),
arg_param_end
};
CLIExecWithReturn(ctx, Cmd, argtable, true);
uint32_t waittime = arg_get_u32_def(ctx, 1, 0);
CLIParserFree(ctx);
uint8_t data[PM3_CMD_DATA_SIZE] = {0x00};
size_t len = 0;