diff --git a/CHANGELOG.md b/CHANGELOG.md index bf8bb7a75..3a564283d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ 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] +- Fixed `hf 14a sim` - a dynamic response now gets a modulation buffer sized to fit it, instead of one 68 bytes too small (or 1788 too large for ST25TA) (@iceman1001) +- Fixed `BigBuf_malloc` - takes a uint32_t, so a request of 64KB or more fails instead of wrapping to zero (@iceman1001) +- Fixed `BigBuf_max_traceLen` - returns a uint32_t, so LF sampling on a device with more than 64KB of BigBuf is not handed a truncated buffer (@iceman1001) - Changed `hf mf view` - now show which VIGIK fields are RSA signed (@iceman1001) - Changed `hw status` - now reports EMULATOR memory (@iceman1001) - Changed `hf mf view` - Urmet Captiv cards now decode their number field and data regions (@iceman1001) diff --git a/armsrc/BigBuf.c b/armsrc/BigBuf.c index bbdd0e93b..a68bf7a07 100644 --- a/armsrc/BigBuf.c +++ b/armsrc/BigBuf.c @@ -151,10 +151,16 @@ void BigBuf_Clear_keep_EM(void) { // allocate a chunk of memory from BigBuf. We allocate high memory first. The unallocated memory // at the beginning of BigBuf is always for traces/samples -uint8_t *BigBuf_malloc(uint16_t chunksize) { +// +// chunksize is a uint32_t on purpose. It used to be a uint16_t, which silently +// wrapped anything from 64 kbyte up -- a request for exactly 65536 came through +// as 0 -- and on a PM5, where BigBuf is several hundred kbyte, that is a size a +// caller can reasonably ask for. Oversized requests now fail the check below and +// return NULL like any other allocation that does not fit. +uint8_t *BigBuf_malloc(uint32_t chunksize) { chunksize = (chunksize + BIGBUF_ALIGN_BYTES - 1) & BIGBUF_ALIGN_MASK; // round up to next multiple of 4 - if (s_bigbuf_hi - s_trace_len < chunksize || chunksize == 0) { + if (chunksize == 0 || chunksize > s_bigbuf_hi || s_bigbuf_hi - s_trace_len < chunksize) { // no memory left or chunksize too large return NULL; } @@ -165,7 +171,7 @@ uint8_t *BigBuf_malloc(uint16_t chunksize) { // allocate a chunk of memory from BigBuf, and returns a pointer to it. // sets the memory to zero -uint8_t *BigBuf_calloc(uint16_t chunksize) { +uint8_t *BigBuf_calloc(uint32_t chunksize) { uint8_t *mem = BigBuf_malloc(chunksize); if (mem != NULL) { memset(mem, 0x00, ((chunksize + BIGBUF_ALIGN_BYTES - 1) & BIGBUF_ALIGN_MASK)); // round up to next multiple of 4 @@ -229,7 +235,11 @@ void BigBuf_print_status(void) { } // return the maximum trace length (i.e. the unallocated size of BigBuf) -uint16_t BigBuf_max_traceLen(void) { +// Room left for traces and samples, ie everything below the lowest allocation. +// s_bigbuf_hi is a uint32_t and on a PM5 BigBuf is several hundred kbyte, so this +// must not be narrowed -- truncating it to 16 bits would silently hand LF +// sampling a fraction of the buffer it actually has +uint32_t BigBuf_max_traceLen(void) { return s_bigbuf_hi & BIGBUF_ALIGN_MASK; } diff --git a/armsrc/BigBuf.h b/armsrc/BigBuf.h index 63bf4bb77..8a9110b15 100644 --- a/armsrc/BigBuf.h +++ b/armsrc/BigBuf.h @@ -44,7 +44,7 @@ uint32_t BigBuf_get_size(void); uint8_t *BigBuf_get_EM_addr(void); uint32_t BigBuf_get_EM_size(void); bool BigBuf_is_EM_allocated(void); -uint16_t BigBuf_max_traceLen(void); +uint32_t BigBuf_max_traceLen(void); uint32_t BigBuf_get_hi(void); void BigBuf_initialize(void); @@ -52,8 +52,8 @@ void BigBuf_Clear(void); void BigBuf_Clear_ext(bool verbose); void BigBuf_Clear_keep_EM(void); void BigBuf_Clear_EM(void); -uint8_t *BigBuf_malloc(uint16_t); -uint8_t *BigBuf_calloc(uint16_t); +uint8_t *BigBuf_malloc(uint32_t); +uint8_t *BigBuf_calloc(uint32_t); void BigBuf_free(void); void BigBuf_free_keep_EM(void); void BigBuf_print_status(void); diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 219bee189..18279feb2 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -3441,8 +3441,9 @@ static void PacketReceived(PacketCommandNG *packet) { if (g_dbglevel >= DBG_DEBUG) Dbprintf("Filename received for spiffs dump : %s", filename); // a file can be bigger than BigBuf, so it is streamed out one frame at a - // time. Reading it into BigBuf first wrapped the uint16_t BigBuf_calloc() - // takes at 64KB and then read the whole file into the short buffer + // time. Reading it into BigBuf first used to wrap at 64KB, back when + // BigBuf_calloc() took a uint16_t, and read the whole file into the + // short buffer that produced const uint16_t dl_chunk = reply_ng_max_data_size() - sizeof(download_chunk_t); uint8_t *chunkbuf = BigBuf_calloc(dl_chunk); if (chunkbuf == NULL) { diff --git a/armsrc/iso14443a.c b/armsrc/iso14443a.c index 0c9899182..071b5b9fe 100644 --- a/armsrc/iso14443a.c +++ b/armsrc/iso14443a.c @@ -1795,18 +1795,28 @@ void SimulateIso14443aTagEx(uint8_t tagType, uint16_t flags, uint8_t *useruid, u // pool and BigBuf_malloc() won't hand out memory the trace still occupies. clear_trace(); - // Allocate 512 bytes for the dynamic modulation, created when the reader queries for it + // Buffers for the dynamic response, created when the reader queries for it. // Such a response is less time critical, so we can prepare them on the fly #define DYNAMIC_RESPONSE_BUFFER_SIZE 64 -#define DYNAMIC_MODULATION_BUFFER_SIZE 512 uint16_t dynamic_response_buffer_size = DYNAMIC_RESPONSE_BUFFER_SIZE; - uint16_t dynamic_modulation_buffer_size = DYNAMIC_MODULATION_BUFFER_SIZE; if (tagType == 10) { dynamic_response_buffer_size = ST25TA_EML_NDEF_MAX + 4; - dynamic_modulation_buffer_size = 4096; } + // prepare_tag_modulation() memcpy's the encoded answer out of the ToSend + // buffer, and tosend_stuffbit() hard caps that at TOSEND_BUFFER_SIZE, so the + // modulation can never be larger than that however big the response is. + // Encoding costs 9 bytes per response byte -- 8 data bits plus a parity bit -- + // and 4 more for start, stop and the correction bit. + // + // ST25TA used to ask for a flat 4096 here, 1788 bytes more than ToSend can + // ever hand it, which made it the largest single allocation of any tag + // simulation. The 512 the other tag types got was 68 bytes short of what a + // full 64 byte dynamic response needs, so those were refused at modulation + // time with an out-of-bound message + uint16_t dynamic_modulation_buffer_size = MIN((9 * dynamic_response_buffer_size) + 4, TOSEND_BUFFER_SIZE); + uint8_t *dynamic_response_buffer = BigBuf_calloc(dynamic_response_buffer_size); if (dynamic_response_buffer == NULL) { BigBuf_free_keep_EM(); @@ -2222,7 +2232,7 @@ void SimulateIso14443aTagEx(uint8_t tagType, uint16_t flags, uint8_t *useruid, u num_to_bytes(nonce, 4, dynamic_response_info.response); dynamic_response_info.response_n = 4; - prepare_tag_modulation(&dynamic_response_info, DYNAMIC_MODULATION_BUFFER_SIZE); + prepare_tag_modulation(&dynamic_response_info, dynamic_modulation_buffer_size); p_response = &dynamic_response_info; order = ORDER_AUTH; } else if (receivedCmd[0] == ISO14443A_CMD_RATS && len == 4) { // Received a RATS request @@ -2260,7 +2270,7 @@ void SimulateIso14443aTagEx(uint8_t tagType, uint16_t flags, uint8_t *useruid, u // prepare to send dynamic_response_info.response_n = 1 + 8 + 2; - prepare_tag_modulation(&dynamic_response_info, DYNAMIC_MODULATION_BUFFER_SIZE); + prepare_tag_modulation(&dynamic_response_info, dynamic_modulation_buffer_size); p_response = &dynamic_response_info; order = ORDER_AUTH; @@ -2311,7 +2321,7 @@ void SimulateIso14443aTagEx(uint8_t tagType, uint16_t flags, uint8_t *useruid, u dynamic_response_info.response_n = 1 + 8 + 2; - prepare_tag_modulation(&dynamic_response_info, DYNAMIC_MODULATION_BUFFER_SIZE); + prepare_tag_modulation(&dynamic_response_info, dynamic_modulation_buffer_size); p_response = &dynamic_response_info; order = ORDER_NONE; @@ -2342,7 +2352,7 @@ void SimulateIso14443aTagEx(uint8_t tagType, uint16_t flags, uint8_t *useruid, u // prepare to send dynamic_response_info.response_n = 1 + 16 + 2; - prepare_tag_modulation(&dynamic_response_info, DYNAMIC_MODULATION_BUFFER_SIZE); + prepare_tag_modulation(&dynamic_response_info, dynamic_modulation_buffer_size); p_response = &dynamic_response_info; order = ORDER_AUTH; @@ -2396,7 +2406,7 @@ void SimulateIso14443aTagEx(uint8_t tagType, uint16_t flags, uint8_t *useruid, u dynamic_response_info.response_n = 1 + 16 + 2; - prepare_tag_modulation(&dynamic_response_info, DYNAMIC_MODULATION_BUFFER_SIZE); + prepare_tag_modulation(&dynamic_response_info, dynamic_modulation_buffer_size); p_response = &dynamic_response_info; order = ORDER_NONE; @@ -2426,7 +2436,7 @@ void SimulateIso14443aTagEx(uint8_t tagType, uint16_t flags, uint8_t *useruid, u dynamic_response_info.response[1] = pack_live[1]; AddCrc14A(dynamic_response_info.response, 2); dynamic_response_info.response_n = 4; // 2 PACK + 2 CRC - prepare_tag_modulation(&dynamic_response_info, DYNAMIC_MODULATION_BUFFER_SIZE); + prepare_tag_modulation(&dynamic_response_info, dynamic_modulation_buffer_size); p_response = &dynamic_response_info; } else { if (g_dbglevel >= DBG_DEBUG) Dbprintf("Password did not match, NACK_IV."); diff --git a/armsrc/lfops.c b/armsrc/lfops.c index 559793272..21e3ad89d 100644 --- a/armsrc/lfops.c +++ b/armsrc/lfops.c @@ -536,7 +536,7 @@ void ReadTItag(bool ledcontrol) { #define FREQHI 134200 signed char *dest = (signed char *)BigBuf_get_addr(); - uint16_t n = BigBuf_max_traceLen(); + uint32_t n = BigBuf_max_traceLen(); // 128 bit shift register [shift3:shift2:shift1:shift0] uint32_t shift3 = 0, shift2 = 0, shift1 = 0, shift0 = 0; diff --git a/armsrc/lfsampling.c b/armsrc/lfsampling.c index ed6f32658..b4ae75411 100644 --- a/armsrc/lfsampling.c +++ b/armsrc/lfsampling.c @@ -598,7 +598,7 @@ void doT55x7Acquisition(size_t sample_size, bool ledcontrol) { #define T55xx_READ_TOL 5 uint8_t *dest = BigBuf_get_addr(); - uint16_t bufsize = BigBuf_max_traceLen(); + uint32_t bufsize = BigBuf_max_traceLen(); if (bufsize > sample_size) bufsize = sample_size; @@ -685,7 +685,7 @@ void doT55x7Acquisition(size_t sample_size, bool ledcontrol) { #endif void doCotagAcquisition(void) { - uint16_t bufsize = BigBuf_max_traceLen(); + uint32_t bufsize = BigBuf_max_traceLen(); uint8_t *dest = BigBuf_calloc(bufsize); dest[0] = 0; diff --git a/armsrc/pcf7931.c b/armsrc/pcf7931.c index 1a69eb0b6..a39fa7065 100644 --- a/armsrc/pcf7931.c +++ b/armsrc/pcf7931.c @@ -60,7 +60,7 @@ size_t DemodPCF7931(uint8_t **outBlocks, bool ledcontrol) { uint8_t bits[256] = {0x00}; uint8_t blocks[8][16]; uint8_t *dest = BigBuf_get_addr(); - uint16_t g_GraphTraceLen = BigBuf_max_traceLen(); + uint32_t g_GraphTraceLen = BigBuf_max_traceLen(); // limit g_GraphTraceLen to a little more than 2 data frames. // To make sure a complete dataframe is in the dataset. // 1 Frame is 16 Byte -> 128byte. at a T0 of 64 -> 8129 Samples per frame. diff --git a/armsrc/spiffs.c b/armsrc/spiffs.c index 20413d528..e7a6cbebf 100644 --- a/armsrc/spiffs.c +++ b/armsrc/spiffs.c @@ -448,9 +448,9 @@ static void copy_in_spiffs(const char *src, const char *dst) { spiffs_close_cached(); - // no BigBuf_calloc(filesize) here: a file can be bigger than BigBuf, and bigger - // than the uint16_t BigBuf_calloc() takes. Copy it in SPIFFS_WRITE_CHUNK_SIZE - // chunks instead, which is also what rdv40_spiffs_write() writes in + // no BigBuf_calloc(filesize) here: a file can be bigger than BigBuf. Copy it + // in SPIFFS_WRITE_CHUNK_SIZE chunks instead, which is also what + // rdv40_spiffs_write() writes in uint8_t *mem = BigBuf_calloc(SPIFFS_WRITE_CHUNK_SIZE); if (mem == NULL) { Dbprintf("error, cannot allocate copy buffer"); @@ -854,10 +854,10 @@ static int stream_from_spiffs(const char *filename, uint32_t offset, uint32_t si // Stream `size` bytes from `offset` of a file, one `chunklen` chunk at a time. // // The download path used to read the whole file into BigBuf first. That caps a -// download at what BigBuf holds and, since BigBuf_calloc() takes a uint16_t, the -// request wrapped for files >= 64KB: a short buffer, then a full size read run -// straight past the end of it. Here the file is opened once and only one chunk -// is ever buffered, so file size no longer enters into it. +// download at what BigBuf holds, and back when BigBuf_calloc() took a uint16_t +// the request wrapped for files >= 64KB: a short buffer, then a full size read +// run straight past the end of it. Here the file is opened once and only one +// chunk is ever buffered, so file size no longer enters into it. // // Returns the number of bytes streamed, or a negative PM3_E* on failure. //