Skip to content

Commit

Permalink
Merge branch 'nxtgen-palloc' of https://github.com/jlitewski/proxmark3
Browse files Browse the repository at this point in the history
…into proxmark3-nxtgen
  • Loading branch information
jlitewski committed May 27, 2024
2 parents 9d61456 + e48d8c0 commit aabb7d3
Show file tree
Hide file tree
Showing 103 changed files with 5,485 additions and 3,804 deletions.
6 changes: 3 additions & 3 deletions .coverity.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ COVTOKEN=aAbBcCdDeEfFgGhHiIjJkK
COVBINDIR="/opt/cov-analysis-linux64-2020.09/bin"
# Nickname included in scan description:
NICKNAME=myself
HOSTCC=gcc-10
HOSTCXX=g++-10
HOSTLD=g++-10
HOSTCC=cc
HOSTCXX=c++
HOSTLD=c++

# Do not change it:
COVDIR=cov-int
Expand Down
21 changes: 12 additions & 9 deletions Makefile.defs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ TAR = tar
TARFLAGS ?= -v --ignore-failed-read -r
TARFLAGS += -C .. -f
CROSS ?= arm-none-eabi-
CC ?= gcc
CXX ?= g++
CC ?= cc
CXX ?= c++
SH = sh
BASH = bash
PERL = perl
Expand Down Expand Up @@ -107,6 +107,13 @@ ifeq ($(USE_MACPORTS),1)
MACPORTS_PREFIX ?= /opt/local
endif

# If DEBUG_ARM is set, we want to build the client in debug mode as well
ifeq ($(DEBUG_ARM),1)
DEBUG=1
APP_CFLAGS += -g -DDEBUG_ARM
SKIP_COMPRESSION=1
endif

ifeq ($(DEBUG),1)
DEFCXXFLAGS = -g -O0 -pipe
DEFCFLAGS = -g -O0 -fstrict-aliasing -pipe
Expand All @@ -117,15 +124,11 @@ else
DEFLDFLAGS =
endif

ifeq ($(DEBUG_ARM),1)
APP_CFLAGS += -g
SKIP_COMPRESSION=1
endif
# Next ones are activated only if SANITIZE=1
ifeq ($(SANITIZE),1)
DEFCFLAGS += -g -fsanitize=address -fno-omit-frame-pointer
DEFCXXFLAGS += -g -fsanitize=address -fno-omit-frame-pointer
DEFLDFLAGS += -g -fsanitize=address
DEFCFLAGS += -g -fsanitize=undefined,address -fno-omit-frame-pointer
DEFCXXFLAGS += -g -fsanitize=undefined,address -fno-omit-frame-pointer
DEFLDFLAGS += -g -fsanitize=undefined,address
endif
# Some more warnings we want as errors:
DEFCFLAGS += -Wbad-function-cast -Wredundant-decls -Wmissing-prototypes -Wchar-subscripts -Wshadow -Wundef -Wwrite-strings -Wunused -Wuninitialized -Wpointer-arith -Winline -Wformat -Wformat-security -Winit-self -Wmissing-include-dirs -Wnested-externs -Wmissing-declarations -Wempty-body -Wignored-qualifiers -Wmissing-field-initializers -Wtype-limits -Wold-style-definition
Expand Down
4 changes: 2 additions & 2 deletions armsrc/BigBuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void BigBuf_print_status(void) {
Dbprintf(" tracing ................ %d", tracing);
Dbprintf(" traceLen ............... %d", trace_len);

if (g_dbglevel >= DBG_DEBUG) {
if (PRINT_DEBUG) {
DbpString(_CYAN_("Sending buffers"));

uint16_t d8 = 0;
Expand Down Expand Up @@ -266,7 +266,7 @@ bool RAMFUNC LogTrace(const uint8_t *btBytes, uint16_t iLen, uint32_t timestamp_

if (duration > 0xFFFF) {
/*
if (g_dbglevel >= DBG_DEBUG) {
if (PRINT_DEBUG) {
Dbprintf("Error in LogTrace: duration too long for 16 bits encoding: 0x%08x start: 0x%08x end: 0x%08x", duration, timestamp_start, timestamp_end);
}
*/
Expand Down
6 changes: 4 additions & 2 deletions armsrc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ THUMBSRC = start.c \
commonutil.c \
util.c \
string.c \
BigBuf.c \
ticks.c \
clocks.c \
hfsnoop.c \
generator.c
generator.c \
palloc.c \
tracer.c \
cardemu.c


# These are to be compiled in ARM mode
Expand Down
6 changes: 3 additions & 3 deletions armsrc/Standalone/hf_14asniff.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
#include "appmain.h"
#include "dbprint.h"
#include "ticks.h"
#include "BigBuf.h"
#include "tracer.h"

#define HF_14ASNIFF_LOGFILE "hf_14asniff.trace"

Expand Down Expand Up @@ -104,7 +104,7 @@ void RunMod(void) {
Dbprintf("Stopped sniffing");
SpinDelay(200);

uint32_t trace_len = BigBuf_get_traceLen();
uint32_t trace_len = get_trace_length();
#ifndef WITH_FLASH
// Keep stuff in BigBuf for USB/BT dumping
if (trace_len > 0)
Expand All @@ -114,7 +114,7 @@ void RunMod(void) {
if (trace_len > 0) {
Dbprintf("[!] Trace length (bytes) = %u", trace_len);

uint8_t *trace_buffer = BigBuf_get_addr();
uint8_t *trace_buffer = (uint8_t*)get_current_trace();
if (!exists_in_spiffs(HF_14ASNIFF_LOGFILE)) {
rdv40_spiffs_write(
HF_14ASNIFF_LOGFILE, trace_buffer, trace_len, RDV40_SPIFFS_SAFETY_SAFE);
Expand Down
6 changes: 3 additions & 3 deletions armsrc/Standalone/hf_14bsniff.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#include "appmain.h"
#include "dbprint.h"
#include "ticks.h"
#include "BigBuf.h"
#include "tracer.h"

#define HF_14BSNIFF_LOGFILE "hf_14bsniff.trace"

Expand Down Expand Up @@ -77,7 +77,7 @@ void RunMod(void) {
Dbprintf("Stopped sniffing");
SpinDelay(200);

uint32_t trace_len = BigBuf_get_traceLen();
uint32_t trace_len = get_trace_length();
#ifndef WITH_FLASH
// Keep stuff in BigBuf for USB/BT dumping
if (trace_len > 0)
Expand All @@ -87,7 +87,7 @@ void RunMod(void) {
if (trace_len > 0) {
Dbprintf("[!] Trace length (bytes) = %u", trace_len);

uint8_t *trace_buffer = BigBuf_get_addr();
uint8_t *trace_buffer = (uint8_t*)get_current_trace();
if (!exists_in_spiffs(HF_14BSNIFF_LOGFILE)) {
rdv40_spiffs_write(
HF_14BSNIFF_LOGFILE, trace_buffer, trace_len, RDV40_SPIFFS_SAFETY_SAFE);
Expand Down
9 changes: 5 additions & 4 deletions armsrc/Standalone/hf_15sim.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
#include "appmain.h"
#include "dbprint.h"
#include "ticks.h"
#include "BigBuf.h"
#include "tracer.h"
#include "cardemu.h"
#include "crc16.h"

#define AddCrc15(data, len) compute_crc(CRC_15693, (data), (len), (data)+(len), (data)+(len)+1)
Expand Down Expand Up @@ -69,7 +70,7 @@ void RunMod(void) {

FpgaDownloadAndGo(FPGA_BITSTREAM_HF_15);

iso15_tag_t *tag = (iso15_tag_t *) BigBuf_get_EM_addr();
iso15_tag_t *tag = (iso15_tag_t *) get_emulator_address();
if (tag == NULL) return;

uint8_t cmd[8] = {0};
Expand Down Expand Up @@ -189,7 +190,7 @@ void RunMod(void) {
Dbprintf("Simulation stopped");
SpinDelay(200);

uint32_t trace_len = BigBuf_get_traceLen();
uint32_t trace_len = get_trace_length();
#ifndef WITH_FLASH
// Keep stuff in BigBuf for USB/BT dumping
if (trace_len > 0)
Expand All @@ -199,7 +200,7 @@ void RunMod(void) {
if (trace_len > 0) {
Dbprintf("[!] Trace length (bytes) = %u", trace_len);

uint8_t *trace_buffer = BigBuf_get_addr();
uint8_t *trace_buffer = (uint8_t*)get_current_trace();
if (!exists_in_spiffs(HF_15693SIM_LOGFILE)) {
rdv40_spiffs_write(
HF_15693SIM_LOGFILE,
Expand Down
6 changes: 3 additions & 3 deletions armsrc/Standalone/hf_15sniff.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
#include "appmain.h"
#include "dbprint.h"
#include "ticks.h"
#include "BigBuf.h"
#include "tracer.h"



Expand Down Expand Up @@ -108,7 +108,7 @@ void RunMod(void) {
Dbprintf("Stopped sniffing");
SpinDelay(200);

uint32_t trace_len = BigBuf_get_traceLen();
uint32_t trace_len = get_trace_length();
#ifndef WITH_FLASH
// Keep stuff in BigBuf for USB/BT dumping
if (trace_len > 0)
Expand All @@ -118,7 +118,7 @@ void RunMod(void) {
if (trace_len > 0) {
Dbprintf("[!] Trace length (bytes) = %u", trace_len);

uint8_t *trace_buffer = BigBuf_get_addr();
uint8_t *trace_buffer = (uint8_t*)get_current_trace();
if (!exists_in_spiffs(HF_15693SNIFF_LOGFILE)) {
rdv40_spiffs_write(
HF_15693SNIFF_LOGFILE, trace_buffer, trace_len, RDV40_SPIFFS_SAFETY_SAFE);
Expand Down
73 changes: 48 additions & 25 deletions armsrc/Standalone/hf_bog.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ from the client to view the stored quadlets.
#include "fpgaloader.h"
#include "dbprint.h"
#include "ticks.h"
#include "BigBuf.h"
#include "palloc.h"
#include "tracer.h"
#include "string.h"

#define DELAY_READER_AIR2ARM_AS_SNIFFER (2 + 3 + 8)
Expand All @@ -56,26 +57,35 @@ static void RAMFUNC SniffAndStore(uint8_t param) {

iso14443a_setup(FPGA_HF_ISO14443A_SNIFFER);

// Allocate memory from BigBuf for some buffers
// free all previous allocations first
BigBuf_free();
BigBuf_Clear_ext(false);
clear_trace();
set_tracing(true);
release_trace();
start_tracing();

// Array to store the authpwds
uint8_t *capturedPwds = BigBuf_malloc(4 * MAX_PWDS_PER_SESSION);
uint8_t *capturedPwds = palloc(1, 4 * MAX_PWDS_PER_SESSION);

// The command (reader -> tag) that we're receiving.
uint8_t *receivedCmd = BigBuf_malloc(MAX_FRAME_SIZE);
uint8_t *receivedCmdPar = BigBuf_malloc(MAX_PARITY_SIZE);
uint8_t *receivedCmd = palloc(1, MAX_FRAME_SIZE);
uint8_t *receivedCmdPar = palloc(1, MAX_PARITY_SIZE);

// The response (tag -> reader) that we're receiving.
uint8_t *receivedResp = BigBuf_malloc(MAX_FRAME_SIZE);
uint8_t *receivedRespPar = BigBuf_malloc(MAX_PARITY_SIZE);
uint8_t *receivedResp = palloc(1, MAX_FRAME_SIZE);
uint8_t *receivedRespPar = palloc(1, MAX_PARITY_SIZE);

// The DMA buffer, used to stream samples from the FPGA
uint8_t *dmaBuf = BigBuf_malloc(DMA_BUFFER_SIZE);
uint8_t *dmaBuf = palloc(1, DMA_BUFFER_SIZE); //TODO: Move over to buffer8u_t

if(capturedPwds == nullptr || receivedCmd == nullptr || receivedCmdPar == nullptr ||
receivedResp == nullptr || receivedRespPar == nullptr || dmaBuf == nullptr) {
if (PRINT_ERROR) Dbprintf("Memory Allocation failed. Exiting");
palloc_free(capturedPwds);
palloc_free(receivedCmd);
palloc_free(receivedCmdPar);
palloc_free(receivedResp);
palloc_free(receivedRespPar);
palloc_free(dmaBuf);
return;
}

uint8_t *data = dmaBuf;

uint8_t previous_data = 0;
Expand All @@ -91,13 +101,18 @@ static void RAMFUNC SniffAndStore(uint8_t param) {

// Setup and start DMA.
if (!FpgaSetupSscDma((uint8_t *)dmaBuf, DMA_BUFFER_SIZE)) {
if (g_dbglevel > 1)
Dbprintf("FpgaSetupSscDma failed. Exiting");
if (PRINT_ERROR) Dbprintf("FpgaSetupSscDma failed. Exiting");
palloc_free(capturedPwds);
palloc_free(receivedCmd);
palloc_free(receivedCmdPar);
palloc_free(receivedResp);
palloc_free(receivedRespPar);
palloc_free(dmaBuf);
return;
}

tUart14a *uart = GetUart14a();
tDemod14a *demod = GetDemod14a();
uart_14a_t *uart = GetUart14a();
demod_14a_t *demod = GetDemod14a();

// We won't start recording the frames that we acquire until we trigger;
// a good trigger condition to get started is probably when we see a
Expand All @@ -119,6 +134,7 @@ static void RAMFUNC SniffAndStore(uint8_t param) {

int register readBufDataP = data - dmaBuf;
int register dmaBufDataP = DMA_BUFFER_SIZE - AT91C_BASE_PDC_SSC->PDC_RCR;

if (readBufDataP <= dmaBufDataP)
dataLen = dmaBufDataP - readBufDataP;
else
Expand All @@ -129,8 +145,8 @@ static void RAMFUNC SniffAndStore(uint8_t param) {
Dbprintf("[!] blew circular buffer! | datalen %u", dataLen);
break;
}
if (dataLen < 1)
continue;

if (dataLen < 1) continue;

// primary buffer was stopped( <-- we lost data!
if (!AT91C_BASE_PDC_SSC->PDC_RCR) {
Expand Down Expand Up @@ -161,7 +177,7 @@ static void RAMFUNC SniffAndStore(uint8_t param) {
if (triggered) {
if ((receivedCmd) &&
((receivedCmd[0] == MIFARE_ULEV1_AUTH) || (receivedCmd[0] == MIFARE_ULC_AUTH_1))) {
if (g_dbglevel > 1)
if (PRINT_INFO)
Dbprintf("PWD-AUTH KEY: 0x%02x%02x%02x%02x", receivedCmd[1], receivedCmd[2],
receivedCmd[3], receivedCmd[4]);

Expand All @@ -170,7 +186,7 @@ static void RAMFUNC SniffAndStore(uint8_t param) {
auth_attempts++;
}

if (!LogTrace(receivedCmd, uart->len, uart->startTime * 16 - DELAY_READER_AIR2ARM_AS_SNIFFER,
if (!log_trace(receivedCmd, uart->len, uart->startTime * 16 - DELAY_READER_AIR2ARM_AS_SNIFFER,
uart->endTime * 16 - DELAY_READER_AIR2ARM_AS_SNIFFER, uart->parity, true))
break;
}
Expand All @@ -190,7 +206,7 @@ static void RAMFUNC SniffAndStore(uint8_t param) {
if (ManchesterDecoding(tagdata, 0, (my_rsamples - 1) * 4)) {
LED_B_ON();

if (!LogTrace(receivedResp, demod->len, demod->startTime * 16 - DELAY_TAG_AIR2ARM_AS_SNIFFER,
if (!log_trace(receivedResp, demod->len, demod->startTime * 16 - DELAY_TAG_AIR2ARM_AS_SNIFFER,
demod->endTime * 16 - DELAY_TAG_AIR2ARM_AS_SNIFFER, demod->parity, false))
break;

Expand All @@ -217,15 +233,15 @@ static void RAMFUNC SniffAndStore(uint8_t param) {
} // end main loop

FpgaDisableSscDma();
set_tracing(false);
stop_tracing();

Dbprintf("Stopped sniffing");

SpinDelay(200);

// Write stuff to spiffs logfile
if (auth_attempts > 0) {
if (g_dbglevel > 1)
if (PRINT_INFO)
Dbprintf("[!] Authentication attempts = %u", auth_attempts);

if (!exists_in_spiffs((char *)HF_BOG_LOGFILE)) {
Expand All @@ -235,7 +251,14 @@ static void RAMFUNC SniffAndStore(uint8_t param) {
}
}

if (g_dbglevel > 1)
palloc_free(capturedPwds);
palloc_free(receivedCmd);
palloc_free(receivedCmdPar);
palloc_free(receivedResp);
palloc_free(receivedRespPar);
palloc_free(dmaBuf);

if (PRINT_INFO)
Dbprintf("[!] Wrote %u Authentication attempts into logfile", auth_attempts);

SpinErr(LED_A, 200, 5);
Expand Down
7 changes: 4 additions & 3 deletions armsrc/Standalone/hf_cardhopper.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
#include <string.h>

#include "appmain.h"
#include "BigBuf.h"
#include "palloc.h"
#include "tracer.h"
#include "dbprint.h"
#include "fpgaloader.h"
#include "iso14443a.h"
Expand Down Expand Up @@ -88,8 +89,8 @@ void RunMod(void) {
DbpString(_CYAN_("[@]") " CardHopper has started - waiting for mode");
FpgaDownloadAndGo(FPGA_BITSTREAM_HF);

clear_trace();
set_tracing(true);
release_trace();
start_tracing();

// Indicate we are alive and in CardHopper
LEDsoff();
Expand Down
Loading

0 comments on commit aabb7d3

Please sign in to comment.