From 95d1d5646f5006e5b1705fb7ec40e70c1a9c9b7c Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Thu, 14 Dec 2023 23:29:43 +0100 Subject: [PATCH] an attempt to reduce some stack memory. No need for the debug printing to be 512 bytes. Now limited to 200 chars --- CHANGELOG.md | 1 + armsrc/BigBuf.c | 4 ++-- armsrc/appmain.c | 11 +++++++---- armsrc/cmd.c | 13 ++++++++----- armsrc/dbprint.c | 8 ++++---- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed361cc65..b588c69d4 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] + - Change `dbprint` on device side to use max 200 chars strings. (@iceman1001) - Fixed bootloader to correctly clear bss segment on start. Fixes USB serial number sometimes not working in the bootloader (@nvx) - Change `notes on downgrade attacks` - reworked the original text follow repo style (@iceman1001) - Added `hf mf info` command and static encrypted nonce detection (@merlokk) diff --git a/armsrc/BigBuf.c b/armsrc/BigBuf.c index 0c629b50f..cce710ffa 100644 --- a/armsrc/BigBuf.c +++ b/armsrc/BigBuf.c @@ -328,9 +328,9 @@ uint8_t emlGet(uint8_t *out, uint32_t offset, uint32_t length) { // get the address of the ToSend buffer. Allocate part of Bigbuf for it, if not yet done tosend_t *get_tosend(void) { - if (toSend.buf == NULL) + if (toSend.buf == NULL) { toSend.buf = BigBuf_malloc(TOSEND_BUFFER_SIZE); - + } return &toSend; } diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 9267ecffd..456d721db 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -2197,8 +2197,9 @@ static void PacketReceived(PacketCommandNG *packet) { reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF, PM3_SUCCESS, NULL, 0); break; case 2: - if (button_status == BUTTON_SINGLE_CLICK) + if (button_status == BUTTON_SINGLE_CLICK) { reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF, PM3_EOPABORTED, NULL, 0); + } uint16_t volt = MeasureAntennaTuningHfData(); reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF, PM3_SUCCESS, (uint8_t *)&volt, sizeof(volt)); break; @@ -2225,8 +2226,9 @@ static void PacketReceived(PacketCommandNG *packet) { reply_ng(CMD_MEASURE_ANTENNA_TUNING_LF, PM3_SUCCESS, NULL, 0); break; case 2: - if (button_status == BUTTON_SINGLE_CLICK) + if (button_status == BUTTON_SINGLE_CLICK) { reply_ng(CMD_MEASURE_ANTENNA_TUNING_LF, PM3_EOPABORTED, NULL, 0); + } uint32_t volt = MeasureAntennaTuningLfData(); reply_ng(CMD_MEASURE_ANTENNA_TUNING_LF, PM3_SUCCESS, (uint8_t *)&volt, sizeof(volt)); @@ -2839,8 +2841,9 @@ void __attribute__((noreturn)) AppMain(void) { WDT_HIT(); if (*_stack_start != 0xdeadbeef) { - Dbprintf("Stack overflow detected! Please increase stack size, currently %d bytes", (uint32_t)_stack_end - (uint32_t)_stack_start); - Dbprintf("Unplug your device now."); + Dbprintf("DEBUG: increase stack size, currently " _YELLOW_("%d") " bytes", (uint32_t)_stack_end - (uint32_t)_stack_start); + Dbprintf("Stack overflow detected"); + Dbprintf("--> Unplug your device now! <--"); hf_field_off(); while (1); } diff --git a/armsrc/cmd.c b/armsrc/cmd.c index f2b432d33..ee2565cd2 100644 --- a/armsrc/cmd.c +++ b/armsrc/cmd.c @@ -18,6 +18,7 @@ #include "usart.h" #include "crc16.h" #include "string.h" +#include "BigBuf.h" // Flags to tell where to add CRC on sent replies bool g_reply_with_crc_on_usb = false; @@ -29,8 +30,8 @@ bool g_reply_via_usb = false; int reply_old(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, const void *data, size_t len) { PacketResponseOLD txcmd = {CMD_UNKNOWN, {0, 0, 0}, {{0}}}; -// for (size_t i = 0; i < sizeof(PacketResponseOLD); i++) -// ((uint8_t *)&txcmd)[i] = 0x00; + for (size_t i = 0; i < sizeof(PacketResponseOLD); i++) + ((uint8_t *)&txcmd)[i] = 0x00; // Compose the outgoing command frame txcmd.cmd = cmd; @@ -147,7 +148,8 @@ int reply_mix(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, const v if (len && data) memcpy(cmddata + sizeof(arg), data, (int)len); - return reply_ng_internal((cmd & 0xFFFF), status, cmddata, len + sizeof(arg), false); + int res = reply_ng_internal((cmd & 0xFFFF), status, cmddata, len + sizeof(arg), false); + return res; } static int receive_ng_internal(PacketCommandNG *rx, uint32_t read_ng(uint8_t *data, size_t len), bool usb, bool fpc) { @@ -178,9 +180,10 @@ static int receive_ng_internal(PacketCommandNG *rx, uint32_t read_ng(uint8_t *da memcpy(rx->data.asBytes, rx_raw.data, length); rx->length = length; } else { - uint64_t arg[3]; - if (length < sizeof(arg)) + uint64_t arg[3] = {0}; + if (length < sizeof(arg)) { return PM3_EIO; + } memcpy(arg, rx_raw.data, sizeof(arg)); rx->oldarg[0] = arg[0]; diff --git a/armsrc/dbprint.c b/armsrc/dbprint.c index 5caaafb0f..afb83da4c 100644 --- a/armsrc/dbprint.c +++ b/armsrc/dbprint.c @@ -22,7 +22,7 @@ #include "printf.h" #define DEBUG 1 - +#define DEBUG_MAX_MSG_SIZE 200 //============================================================================= // Debug print functions, to go out over USB, to the usual PC-side client. //============================================================================= @@ -31,7 +31,7 @@ void DbpStringEx(uint32_t flags, const char *src, size_t srclen) { #if DEBUG struct { uint16_t flag; - uint8_t buf[PM3_CMD_DATA_SIZE - sizeof(uint16_t)]; + uint8_t buf[DEBUG_MAX_MSG_SIZE]; } PACKED data; data.flag = flags; uint16_t len = MIN(srclen, sizeof(data.buf)); @@ -49,7 +49,7 @@ void DbpString(const char *str) { void DbprintfEx(uint32_t flags, const char *fmt, ...) { #if DEBUG // should probably limit size here; oh well, let's just use a big buffer - char s[PM3_CMD_DATA_SIZE] = {0x00}; + char s[DEBUG_MAX_MSG_SIZE] = {0x00}; va_list ap; va_start(ap, fmt); kvsprintf(fmt, s, 10, ap); @@ -62,7 +62,7 @@ void DbprintfEx(uint32_t flags, const char *fmt, ...) { void Dbprintf(const char *fmt, ...) { #if DEBUG // should probably limit size here; oh well, let's just use a big buffer - char output_string[PM3_CMD_DATA_SIZE] = {0x00}; + char output_string[DEBUG_MAX_MSG_SIZE] = {0x00}; va_list ap; va_start(ap, fmt);