From 96d462aceebbf84b13cd8f084527bcddd4ba9528 Mon Sep 17 00:00:00 2001 From: douniwan5788 Date: Tue, 24 Sep 2024 02:21:01 +0800 Subject: [PATCH] add: split PacketResponseNG status to status and reason --- CHANGELOG.md | 1 + armsrc/cmd.c | 15 ++++++++++----- armsrc/cmd.h | 3 ++- armsrc/thinfilm.c | 2 +- client/src/comms.c | 1 + client/src/scripting.c | 3 +++ doc/new_frame_format.md | 16 ++++++++++------ include/pm3_cmd.h | 11 ++++++++--- 8 files changed, 36 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11ca8e8fc..99217ff3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Changed split PacketResponseNG status into status and reason(@douniwan5788) - Print LUA and Python versions in `hw version` command (@jmichelp) - Updated LUA to v5.4.7 which adds utf-8 support (@jmichelp) - Changed `lf search` - it now tries to read and decode paxton id (@iceman1001) diff --git a/armsrc/cmd.c b/armsrc/cmd.c index 61353304c..0f68719d4 100644 --- a/armsrc/cmd.c +++ b/armsrc/cmd.c @@ -72,7 +72,7 @@ int reply_old(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, const v return PM3_SUCCESS; } -static int reply_ng_internal(uint16_t cmd, int16_t status, const uint8_t *data, size_t len, bool ng) { +static int reply_ng_internal(uint16_t cmd, int8_t status, uint8_t reason, const uint8_t *data, size_t len, bool ng) { PacketResponseNGRaw txBufferNG; size_t txBufferNGLen; @@ -80,6 +80,7 @@ static int reply_ng_internal(uint16_t cmd, int16_t status, const uint8_t *data, txBufferNG.pre.magic = RESPONSENG_PREAMBLE_MAGIC; txBufferNG.pre.cmd = cmd; txBufferNG.pre.status = status; + txBufferNG.pre.reason = reason; txBufferNG.pre.ng = ng; if (len > PM3_CMD_DATA_SIZE) { len = PM3_CMD_DATA_SIZE; @@ -136,12 +137,12 @@ static int reply_ng_internal(uint16_t cmd, int16_t status, const uint8_t *data, return PM3_SUCCESS; } -int reply_ng(uint16_t cmd, int16_t status, const uint8_t *data, size_t len) { - return reply_ng_internal(cmd, status, data, len, true); +int reply_ng(uint16_t cmd, int8_t status, const uint8_t *data, size_t len) { + return reply_ng_internal(cmd, status, -1, data, len, true); } int reply_mix(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, const void *data, size_t len) { - int16_t status = PM3_SUCCESS; + int8_t status = PM3_SUCCESS; uint64_t arg[3] = {arg0, arg1, arg2}; if (len > PM3_CMD_DATA_SIZE - sizeof(arg)) { len = PM3_CMD_DATA_SIZE - sizeof(arg); @@ -153,7 +154,11 @@ int reply_mix(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, const v memcpy(cmddata + sizeof(arg), data, (int)len); } - return reply_ng_internal((cmd & 0xFFFF), status, cmddata, len + sizeof(arg), false); + return reply_ng_internal((cmd & 0xFFFF), status, -1, cmddata, len + sizeof(arg), false); +} + +int reply_reason(uint16_t cmd, int8_t status, int8_t reason, const uint8_t *data, size_t len) { + return reply_ng_internal(cmd, status, reason, data, len, true); } static int receive_ng_internal(PacketCommandNG *rx, uint32_t read_ng(uint8_t *data, size_t len), bool usb, bool fpc) { diff --git a/armsrc/cmd.h b/armsrc/cmd.h index 22a79ce16..c96024bc2 100644 --- a/armsrc/cmd.h +++ b/armsrc/cmd.h @@ -28,8 +28,9 @@ extern bool g_reply_via_fpc; extern bool g_reply_via_usb; int reply_old(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, const void *data, size_t len); -int reply_ng(uint16_t cmd, int16_t status, const uint8_t *data, size_t len); +int reply_ng(uint16_t cmd, int8_t status, const uint8_t *data, size_t len); int reply_mix(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, const void *data, size_t len); +int reply_reason(uint16_t cmd, int8_t status, int8_t reason, const uint8_t *data, size_t len); int receive_ng(PacketCommandNG *rx); #endif // _PROXMARK_CMD_H_ diff --git a/armsrc/thinfilm.c b/armsrc/thinfilm.c index 150189f8f..79063ba7f 100644 --- a/armsrc/thinfilm.c +++ b/armsrc/thinfilm.c @@ -140,7 +140,7 @@ void SimulateThinFilm(uint8_t *data, size_t len) { uint16_t hf_baseline = ReadReaderField(); - int16_t status = PM3_SUCCESS; + int8_t status = PM3_SUCCESS; CodeThinfilmAsTag(data, len); tosend_t *ts = get_tosend(); diff --git a/client/src/comms.c b/client/src/comms.c index 21e63402c..2ed929d17 100644 --- a/client/src/comms.c +++ b/client/src/comms.c @@ -481,6 +481,7 @@ __attribute__((force_align_arg_pointer)) uint16_t length = rx_raw.pre.length; rx.ng = rx_raw.pre.ng; rx.status = rx_raw.pre.status; + rx.reason = rx_raw.pre.reason; rx.cmd = rx_raw.pre.cmd; if (rx.magic == RESPONSENG_PREAMBLE_MAGIC) { // New style NG reply diff --git a/client/src/scripting.c b/client/src/scripting.c index 93e68d123..9b1542d54 100644 --- a/client/src/scripting.c +++ b/client/src/scripting.c @@ -371,6 +371,9 @@ static int l_WaitForResponseTimeout(lua_State *L) { memcpy(foo + n, &resp.status, sizeof(resp.status)); n += sizeof(resp.status); + memcpy(foo + n, &resp.reason, sizeof(resp.reason)); + n += sizeof(resp.reason); + memcpy(foo + n, &resp.crc, sizeof(resp.crc)); n += sizeof(resp.crc); diff --git a/doc/new_frame_format.md b/doc/new_frame_format.md index 6f267163e..76c914c9d 100644 --- a/doc/new_frame_format.md +++ b/doc/new_frame_format.md @@ -70,8 +70,9 @@ For responses from the Proxmark3: uint32_t magic; uint16_t length : 15; - bool ng : 1; - int16_t status; + bool ng : 1; + int8_t status; + int8_t reason; uint16_t cmd; uint8_t data[length]; uint16_t crc; @@ -80,6 +81,7 @@ For responses from the Proxmark3: * `length`: length of the variable payload, 0 if none, max 512 (PM3_CMD_DATA_SIZE) for now. * `ng`: flag to tell if the data is following the new format (ng) or the old one, see transition notes below * `status`: a field to send back the status of the command execution +* `reason`: details about what the status indicates for the specified command * `cmd`: as previously, on 16b as it's enough * `data`: variable length payload * `crc`: either an actual CRC (crc14a) or a Magic placeholder (`b3`) @@ -130,7 +132,8 @@ After the full transition, we might remove the fields `oldarg` and `ng`. uint16_t cmd; uint16_t length; uint32_t magic; // NG - int16_t status; // NG + int8_t status; // NG + int8_t reason; // NG uint16_t crc; // NG uint64_t oldarg[3]; // OLD union { @@ -177,9 +180,10 @@ Old handlers will still find their stuff in `PacketCommandNG.oldarg` field. (`common/cmd.c`) - int16_t reply_ng(uint16_t cmd, int16_t status, uint8_t *data, size_t len) - int16_t reply_old(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, void *data, size_t len) - int16_t reply_mix(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, void *data, size_t len) + int reply_old(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, const void *data, size_t len); + int reply_ng(uint16_t cmd, int8_t status, const uint8_t *data, size_t len); + int reply_mix(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, const void *data, size_t len); + int reply_reason(uint16_t cmd, int8_t status, int8_t reason, const uint8_t *data, size_t len); So replies should make the transition from `reply_old` to `reply_ng` to benefit from smaller frames (and client reception adjusted accordingly of course). `reply_mix` is a transition fct: it uses the same API as reply_old but benefits somehow from variable length frames. It occupies at least 24b of data for the oldargs and real data is therefore limited to PM3_CMD_DATA_SIZE - 24. Besides the size limitation, the client command doesn't know if this was an OLD frame or a MIX frame, it gets its oldargs and data as usual. diff --git a/include/pm3_cmd.h b/include/pm3_cmd.h index 8399d50f7..7d5111327 100644 --- a/include/pm3_cmd.h +++ b/include/pm3_cmd.h @@ -84,8 +84,9 @@ typedef struct { typedef struct { uint32_t magic; uint16_t length : 15; // length of the variable part, 0 if none. - bool ng : 1; - int16_t status; + bool ng : 1; + int8_t status; + int8_t reason; uint16_t cmd; } PACKED PacketResponseNGPreamble; @@ -101,7 +102,8 @@ typedef struct { uint16_t cmd; uint16_t length; uint32_t magic; // NG - int16_t status; // NG + int8_t status; // NG + int8_t reason; // NG uint16_t crc; // NG uint64_t oldarg[3]; // OLD union { @@ -869,6 +871,9 @@ typedef struct { // Regular quit #define PM3_SQUIT -100 +// reserved for future protocol change +#define PM3_RESERVED -128 + // LF #define LF_FREQ2DIV(f) ((int)(((12000.0 + (f)/2.0)/(f))-1)) #define LF_DIVISOR_125 LF_FREQ2DIV(125)