From 8ee15dbc2a44642d2124ab4f92f11e5a1009bfe6 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sun, 20 Mar 2016 16:00:39 +0100 Subject: [PATCH] read SAM3S unique serial number at start and print it --- firmware/Makefile | 2 +- firmware/include_sam3s/chip.h | 1 + firmware/include_sam3s/unique_id.h | 3 +++ firmware/src_sam3s/unique_id.c | 43 ++++++++++++++++++++++++++++++ firmware/src_simtrace/main.c | 8 ++++++ 5 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 firmware/include_sam3s/unique_id.h create mode 100644 firmware/src_sam3s/unique_id.c diff --git a/firmware/Makefile b/firmware/Makefile index 905bd8ad..a32ddc65 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -146,7 +146,7 @@ VPATH += src_board src_sam3s cmsis $(USB_PATHS) src_simtrace # Objects built from C source files C_CMSIS = core_cm3.o C_LOWLEVEL = board_cstartup_gnu.o board_lowlevel.o syscalls.o exceptions.o -C_LIBLEVEL = spi.o pio.o pmc.o usart.o pio_it.o pio_capture.o uart_console.o iso7816_4.o wdt.o led.o tc.o +C_LIBLEVEL = spi.o pio.o pmc.o usart.o pio_it.o pio_capture.o uart_console.o iso7816_4.o wdt.o led.o tc.o unique_id.o C_CCID = cciddriver.o USBD.o USBDDriver.o USBD_HAL.o USBRequests.o USBDCallbacks.o USBDescriptors.o USBDDriverCallbacks.o C_SIMTRACE = simtrace_iso7816.o usb.o ccid.o sniffer.o mitm.o ringbuffer.o host_communication.o iso7816_fidi.o tc_etu.o req_ctx.o card_emu.o mode_cardemu.o C_APPLEVEL = main.o diff --git a/firmware/include_sam3s/chip.h b/firmware/include_sam3s/chip.h index ae11f55d..3c25ce3b 100644 --- a/firmware/include_sam3s/chip.h +++ b/firmware/include_sam3s/chip.h @@ -52,5 +52,6 @@ #include "trace.h" #include "wdt.h" +#include "unique_id.h" #endif /* _LIB_SAM3S_ */ diff --git a/firmware/include_sam3s/unique_id.h b/firmware/include_sam3s/unique_id.h new file mode 100644 index 00000000..a0851fdb --- /dev/null +++ b/firmware/include_sam3s/unique_id.h @@ -0,0 +1,3 @@ +#pragma once + +void EEFC_ReadUniqueID(unsigned int *pdwUniqueID); diff --git a/firmware/src_sam3s/unique_id.c b/firmware/src_sam3s/unique_id.c new file mode 100644 index 00000000..460eb9c7 --- /dev/null +++ b/firmware/src_sam3s/unique_id.c @@ -0,0 +1,43 @@ +#include "chip.h" + +#define EFC_FCMD_STUI 0x0E +#define EFC_FCMD_SPUI 0x0F + +__attribute__ ((section(".ramfunc"))) +void EEFC_ReadUniqueID(unsigned int *pdwUniqueID) +{ + unsigned int status; + + /* Errata / Workaround: Set bit 16 of EEFC Flash Mode Register + * to 1 */ + EFC->EEFC_FMR |= (1 << 16); + + /* Send the Start Read unique Identifier command (STUI) by + * writing the Flash Command Register with the STUI command. */ + EFC->EEFC_FCR = (0x5A << 24) | EFC_FCMD_STUI; + + /* Wait for the FRDY bit to fall */ + do { + status = EFC->EEFC_FSR; + } while ((status & EEFC_FSR_FRDY) == EEFC_FSR_FRDY); + + /* The Unique Identifier is located in the first 128 bits of the + * Flash memory mapping. So, at the address 0x400000-0x400003. + * */ + pdwUniqueID[0] = *(uint32_t *) IFLASH_ADDR; + pdwUniqueID[1] = *(uint32_t *) (IFLASH_ADDR + 4); + pdwUniqueID[2] = *(uint32_t *) (IFLASH_ADDR + 8); + pdwUniqueID[3] = *(uint32_t *) (IFLASH_ADDR + 12); + + /* To stop the Unique Identifier mode, the user needs to send + * the Stop Read unique Identifier command (SPUI) by writing the + * Flash Command Register with the SPUI command. */ + EFC->EEFC_FCR = (0x5A << 24) | EFC_FCMD_SPUI; + + /* When the Stop read Unique Unique Identifier command (SPUI) + * has been performed, the FRDY bit in the Flash Programming + * Status Register (EEFC_FSR) rises. */ + do { + status = EFC->EEFC_FSR; + } while ((status & EEFC_FSR_FRDY) != EEFC_FSR_FRDY); +} diff --git a/firmware/src_simtrace/main.c b/firmware/src_simtrace/main.c index 16ea2927..bca47cda 100644 --- a/firmware/src_simtrace/main.c +++ b/firmware/src_simtrace/main.c @@ -8,6 +8,8 @@ #include "utils.h" #include "req_ctx.h" +uint32_t g_unique_id[4]; + /*------------------------------------------------------------------------------ * Internal variables *------------------------------------------------------------------------------*/ @@ -102,11 +104,17 @@ extern int main(void) SIMtrace_USB_Initialize(); + EEFC_ReadUniqueID(g_unique_id); + printf("\r\n\r\n" "=============================================================================\r\n" "SIMtrace2 firmware " GIT_VERSION " (C) 2010-2016 by Harald Welte\r\n" "=============================================================================\r\n"); + TRACE_INFO("Serial Nr. %08x-%08x-%08x-%08x\r\n", + g_unique_id[0], g_unique_id[1], + g_unique_id[2], g_unique_id[3]); + TRACE_INFO("USB init...\r\n"); while (USBD_GetState() < USBD_STATE_CONFIGURED) { if (i >= MAX_USB_ITER * 3) {