From 39f6863ce7ea2988c930de76b0783309f9ad1a98 Mon Sep 17 00:00:00 2001 From: jlitewski Date: Tue, 21 May 2024 22:48:02 -0400 Subject: [PATCH] Maybe I fixed it? I need to find some wood to knock on... --- armsrc/appmain.c | 48 +++++++++++++++++++++++++++--------------------- armsrc/appmain.h | 2 +- armsrc/palloc.c | 14 ++++++-------- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index ec75c87c38..ab6a3eb66b 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -46,7 +46,6 @@ #include "em4x70.h" #include "iclass.h" #include "legicrfsim.h" -//#include "cryptorfsim.h" #include "epa.h" #include "hfsnoop.h" #include "lfops.h" @@ -241,9 +240,7 @@ static void MeasureAntennaTuning(void) { // Measure HF in milliVolt static uint16_t MeasureAntennaTuningHfData(void) { - return (MAX_ADC_HF_VOLTAGE * SumAdc(ADC_CHAN_HF, 32)) >> 15; - } // Measure LF in milliVolt @@ -261,7 +258,7 @@ void print_stack_usage(void) { } } -void ReadMem(int addr) { +void ReadMem(size_t addr) { const uint8_t *data = ((uint8_t *)addr); Dbprintf("%x: %02x %02x %02x %02x %02x %02x %02x %02x", addr, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]); @@ -377,6 +374,13 @@ static void printConnSpeed(uint32_t wait) { Dbprintf(" Sending packets to client..."); uint8_t *test_data = (uint8_t*)palloc(2, PM3_CMD_DATA_SIZE); + + if(test_data == nullptr) { + Dbprintf(" " _RED_("Error allocating data for speed test!")); + reply_ng(CMD_DOWNLOADED_TRACE, PM3_EMALLOC, nullptr, 0); + return; + } + uint32_t start_time = GetTickCount(); uint32_t delta_time = 0; uint32_t bytes_transferred = 0; @@ -388,6 +392,7 @@ static void printConnSpeed(uint32_t wait) { bytes_transferred += PM3_CMD_DATA_SIZE; delta_time = GetTickCountDelta(start_time); } + LED_B_OFF(); Dbprintf(" Time elapsed................... %dms", delta_time); @@ -421,7 +426,6 @@ static void SendStatus(uint32_t wait) { #endif printConnSpeed(wait); DbpString(_CYAN_("Various")); - print_debug_level(); fpga_queue_t *fpga_queue = get_fpga_queue(); @@ -448,7 +452,7 @@ static void SendStatus(uint32_t wait) { Flashmem_print_info(); #endif DbpString(""); - reply_ng(CMD_STATUS, PM3_SUCCESS, NULL, 0); + reply_ng(CMD_STATUS, PM3_SUCCESS, nullptr, 0); } static void SendCapabilities(void) { @@ -809,12 +813,11 @@ static void PacketReceived(PacketCommandNG *packet) { reply_ng(CMD_SET_FPGAMODE, PM3_EINVARG, NULL, 0); break; } - // emulator case CMD_SET_DBGMODE: { g_dbglevel = packet->data.asBytes[0]; if (packet->length == 1 || packet->data.asBytes[1] != 0) print_debug_level(); - reply_ng(CMD_SET_DBGMODE, PM3_SUCCESS, NULL, 0); + reply_ng(CMD_SET_DBGMODE, PM3_SUCCESS, nullptr, 0); break; } case CMD_GET_DBGMODE: { @@ -827,8 +830,11 @@ static void PacketReceived(PacketCommandNG *packet) { bool on; bool off; } PACKED; + struct p *payload = (struct p *)packet->data.asBytes; + if (payload->on && payload->off) { + if(PRINT_DEBUG) Dbprintf(" - CMD_SET_TEAROFF: Cannot have both 'on' and 'off' set at the same time!"); reply_ng(CMD_SET_TEAROFF, PM3_EINVARG, NULL, 0); } @@ -843,6 +849,7 @@ static void PacketReceived(PacketCommandNG *packet) { if (payload->delay_us > 0) { g_tearoff_delay_us = payload->delay_us; } + reply_ng(CMD_SET_TEAROFF, PM3_SUCCESS, NULL, 0); break; } @@ -2937,32 +2944,31 @@ static void PacketReceived(PacketCommandNG *packet) { reply_ng(CMD_PING, PM3_SUCCESS, packet->data.asBytes, packet->length); break; } + case CMD_START_FLASH: { + if (g_common_area.flags.bootrom_present) { + g_common_area.command = COMMON_AREA_COMMAND_ENTER_FLASH_MODE; + } + + // This should flow into the following section, since the code was identical before + } case CMD_FINISH_WRITE: case CMD_HARDWARE_RESET: { usb_disable(); // (iceman) why this wait? - SpinDelay(1000); + SpinDelay(100); // Reduced wait to 100ms from 1000ms AT91C_BASE_RSTC->RSTC_RCR = RST_CONTROL_KEY | AT91C_RSTC_PROCRST; // We're going to reset, and the bootrom will take control. for (;;) {} break; } - case CMD_START_FLASH: { - if (g_common_area.flags.bootrom_present) { - g_common_area.command = COMMON_AREA_COMMAND_ENTER_FLASH_MODE; - } - usb_disable(); - AT91C_BASE_RSTC->RSTC_RCR = RST_CONTROL_KEY | AT91C_RSTC_PROCRST; - // We're going to flash, and the bootrom will take control. - for (;;) {} - break; - } case CMD_DEVICE_INFO: { uint32_t dev_info = DEVICE_INFO_FLAG_OSIMAGE_PRESENT | DEVICE_INFO_FLAG_CURRENT_MODE_OS; + if (g_common_area.flags.bootrom_present) { dev_info |= DEVICE_INFO_FLAG_BOOTROM_PRESENT; } + reply_old(CMD_DEVICE_INFO, dev_info, 0, 0, 0, 0); break; } @@ -3052,8 +3058,8 @@ void __attribute__((noreturn)) AppMain(void) { WDT_HIT(); if (*_stack_start != 0xdeadbeef) { - Dbprintf("DEBUG: increase stack size, currently " _YELLOW_("%d") " bytes", (uint32_t)_stack_end - (uint32_t)_stack_start); - Dbprintf("Stack overflow detected"); + if(PRINT_DEBUG) Dbprintf("DEBUG: increase stack size, currently " _YELLOW_("%d") " bytes", (uint32_t)_stack_end - (uint32_t)_stack_start); + Dbprintf(_BACK_BRIGHT_RED_("Stack overflow detected!")); Dbprintf("--> Unplug your device now! <--"); hf_field_off(); while (1); diff --git a/armsrc/appmain.h b/armsrc/appmain.h index fdc520f5f6..3f10b2ca80 100644 --- a/armsrc/appmain.h +++ b/armsrc/appmain.h @@ -44,7 +44,7 @@ extern int ToSendMax; extern uint8_t ToSend[]; void send_wtx(uint16_t wtx); -void ReadMem(int addr); +void ReadMem(size_t addr); void __attribute__((noreturn)) AppMain(void); uint16_t AvgAdc(uint8_t ch); diff --git a/armsrc/palloc.c b/armsrc/palloc.c index b258ca985b..ec7b764047 100644 --- a/armsrc/palloc.c +++ b/armsrc/palloc.c @@ -39,8 +39,9 @@ extern uint32_t _stack_start[], __bss_start__[], __bss_end__[]; // Memory defines -#define MEM_SIZE 65536 // Total memory size (in bytes) of the Atmel SAM7S series MCU we use +#define MEM_SIZE 65536 // Total memory size (in bytes) of the Atmel SAM7S series MCU we use #define MEM_USABLE ((size_t)_stack_start - (size_t)__bss_end__) // The memory (in bytes) we can use +#define MEM_GUARD 32 // Guard size at the top of the heap // Block configuration #define BLOCK_SPLIT_THRESHOLD 16 @@ -88,11 +89,11 @@ static size_t free_space = 0; */ void palloc_init(void) { // Set up the heap - heap = (pHeap*)(__bss_start__); + heap = (pHeap*)(__bss_end__); heap->free = nullptr; heap->used = nullptr; heap->fresh = (pBlock*)(heap + 1); - heap->top = (size_t)(heap->fresh + MAX_BLOCKS); + heap->top = (size_t)(heap->fresh + (MAX_BLOCKS * 1)); // Set up the fresh blocks to use pBlock *block = heap->fresh; @@ -279,11 +280,7 @@ memptr_t *palloc(uint16_t numElement, const uint16_t size) { size_t allocSize = numElement * size; - if(allocSize & ALIGN_MASK) { // Make sure we align our sizes - allocSize += (allocSize + ALIGN_BYTES - 1) & ~ALIGN_MASK; - } - - if(PRINT_DEBUG) Dbprintf("Allocation size: %u", allocSize); + if(PRINT_DEBUG) Dbprintf(" - - Alloc size: %u", allocSize); if((allocSize > MAX_BLOCK_SIZE) || (allocSize > free_space)) { // We would overflow if we attempted to allocate this memory if(PRINT_ERROR) Dbprintf(" - Palloc: "_RED_("Allocation size is too big!") " (%u)", allocSize); @@ -298,6 +295,7 @@ memptr_t *palloc(uint16_t numElement, const uint16_t size) { if(blk != nullptr) { palloc_set(blk->address, 0, blk->size); // Zero the memory free_space -= blk->size; // Remove the space we took up with this allocation + if(PRINT_DEBUG) Dbprintf(" - Palloc: Allocated block of memory at %x with size of %u", blk->address, blk->size); return blk->address; }