17 Commits

Author SHA1 Message Date
Eric Wild
ccceb0ffba cardem: choose a more reasonable default ATR
PCSCd does not like invalid ATRs

Change-Id: I1eebfdc06be55931c2e80e2b515ac3a559737c38
2020-04-10 02:09:40 +02:00
Eric Wild
e0f4d9216e firmware: allow verbose builds
make V=1 can be used to echo all compilation commands, which is useful
because it allows IDEs to parse the gcc output in oder to properly index
the source files using the actual defines passed to the compiler.

Change-Id: I25c41dff89302a73ddd2a4aaba7cb14912fac3b8
2020-04-10 02:09:40 +02:00
Eric Wild
22adb8acd5 Revert "cardem: disable upload for simtrace2"
This reverts commit baa62777c8.
cardem w/ simtrae board should now work.

Change-Id: I1bce9a57ad2960695c31b8879f5d4ded2dddd7bc
2020-04-10 02:09:40 +02:00
Eric Wild
b59fb8fc92 firmware: do not allow undefined symbols
For some reason undefined symbols were downgraded to warnings, which
means building a firmware that calls missing functions  (= address zero)
was perfectly fine, which of course made development more exciting....

This applies to builtins, too, printf of one char gets downgraded to
putchar, which we don't have, so disable builtins.

Change-Id: I492f41ad4162b9d07b1881ae4aed019db2dff8b5
2020-04-10 02:09:40 +02:00
Kévin Redon
70bd983e56 cardem: ignore set ATR
the PPS procedure with baud rate change has not been tested.
by keeping the default ATR instead of applying the ATR sent by
the host software, no other baud rate choice is offered.

Change-Id: Ibf7c6b83d2cf68172c7aa25116d838e24a95d5fe
2020-04-10 02:09:40 +02:00
Kévin Redon
c3d9fe78c7 cardem: fix TPDU state check
this change allows to initialize the TPDU state while in
ISO_S_WAIT_TPDU, before actually entering ISO_S_TPDU

Change-Id: I505d3add32a43de31499b800bc53442f99f65931
2020-04-10 02:09:40 +02:00
Kévin Redon
6268322221 cardem: ensure VCC_PHONE is floating
this change is mainly relevant for the SIMtrace board, but also
affects the others.
First we ensure VCC_PHONE is not forwarded to VCC_SIM because the
card could affect the signal (card could draw too much current or
feed back current in).
next we disable VCC_SIM. the card slot does not need to be
powered, and the FPF2019 leaks current back to VCC_PHONE, even
with forwarding disabled (the reverse current protection only
kicks in when VCC_PHONE is briefly shorted to ground, but still
leaks 0.6V).
enable the ADC channels normally used to measure VCC, even if
not used. the dedicated ADC pins leak current when left
unconfigured. enabling them puts them in high impedance.

Change-Id: If1487c1c191838aaa08b654e49cd31c7180ffc19

XX pin reconfig

Change-Id: Ib261073e3779ae7d98de18ce78b34ff37eafeaa2
2020-04-10 02:09:39 +02:00
Kévin Redon
5eaf5f8e3a cardem: use USART timeout for waiting time
the reset/ATR handling has been heavily updated/fixed.
instead of using the timer counter peripheral to handle
the waiting time and corresponding timeout, the USART peripheral
internal timeout mechanism is used.
this is particularly important for the SIMtrace board since the
clock signal is not connected to the timer counter.
thus this change adds card emulation support for SIMtrace boards.

Fi and Di have been properly rename to F and D since the "i"
stands only for an "indicated" value, not the actual value.
this does not change the USB protocol (the variable have just been
renamed).
additional variables store more information about the card
capabilities

NOTE: it has only be tested for the SIMtrace board

Change-Id: Ibcb2c8cace9137695adf5fb3de43566f7cfb93b5
2020-04-10 02:09:39 +02:00
Kévin Redon
cd62c46e6d cardem: currently simtrace does not support cardem
the SIMtrace board does not support the current card emulation
application because this uses a timer counter to handle the
timeouts, but on the SIMtrace board this is not connected to the
CLK signal

Change-Id: Idd09ea534179f0ede705573e1373dbd045c9828a
2020-04-10 02:09:39 +02:00
Kévin Redon
c5635780bb make sim switch board specific
the simtrace board uses a bus switch not used on qmod and owhw to
switch the SIM between physical and virtual

Change-Id: Ieaf2ed4761fc3e04f33f9aac5c04a768c9a6f71e
2020-04-10 02:09:39 +02:00
Kévin Redon
2d0d1e8cb3 cardemu: different init for simtrace board which uses uart timers
Change-Id: I464f0ce1ec5e60be5f9377186b7d13a5e6dc637e
2020-04-10 02:09:33 +02:00
Kévin Redon
b0d789e09d different voltage comparison on powerup
Change-Id: I71b703162219484e43638f1f2f692e9dd554ef55
2020-04-10 02:09:21 +02:00
Kévin Redon
ae28d5a4c5 minor add comments
this is just to better understand the flow

Change-Id: I045286836176da729cc8c863866d6f6aa3836592
2020-04-10 02:08:59 +02:00
Kévin Redon
9dd4dc287d rename PIN_PHONE_IO to PIN_USIM1_IO
this matches the naming scheme used for USIM2

Change-Id: I486b14260faec897e8c8698c4b7987bf36492497
2020-03-26 02:23:26 +01:00
Kévin Redon
ed7b838392 add ISO 7816-3 library to remsim project
Change-Id: I99f3fecbc00d2379c3a6dc457b047c6fee41c292
2020-03-26 02:23:26 +01:00
Kévin Redon
656fda7ddd add library providing ISO 7816-3 utilities
this will become part of libosmocore since it it common to smart
card related projects (such as osmo-ccid-firmware)

Change-Id: I3d4c65d137fc4555fcb256443feadd1c695de73d
2020-03-26 02:23:26 +01:00
Kévin Redon
4a76fe50ea minor: use same LED pattern for cardem as other opplications
Change-Id: I5608c3312b648c0d59f79338ef1f97b6fe08f5b9
2020-03-26 02:23:26 +01:00
44 changed files with 1311 additions and 774 deletions

5
.gitignore vendored
View File

@@ -18,8 +18,7 @@ tags
*.bin
*.p
host/simtrace2-list
host/simtrace2-cardem-pcsc
host/contrib/simtrace2.spec
host/simtrace2-remsim
host/simtrace2-remsim-usb2udp
usb_strings_generated.h
firmware/usbstring/usbstring
firmware/apps/*/usb_strings.txt.patched

View File

@@ -5,6 +5,9 @@ This is the repository for the next-generation SIMtrace devices,
providing abilities to trace the communication between (U)SIM card and
phone, remote (U)SIM card forward, (U)SIM man-in-the-middle, and more.
This is under heavy development, and right now it is not surprising if
things still break on a daily basis.
NOTE: Nothing in this repository applies to the SIMtrace v1.x hardware
or its associated firmware. SIMtrace v1.x is based on a different CPU /
microcontroller architecture and uses a completely different software
@@ -13,6 +16,12 @@ stack and host software.
Supported Hardware
------------------
At this point, the primary development target is still the OWHW + sysmoQMOD
device, but we expect to add support for a SAM3 based SIMtrace hardware
board soon.
The goal is to support the following devices:
* Osmocom SIMtrace 1.x with SAM3 controller
** this is open hardware and schematics / PCB design is published
* sysmocom sysmoQMOD (with 4 Modems, 4 SIM slots and 2 SAM3)
@@ -28,11 +37,3 @@ This repository contains several directory
* firmware - the firmware to run on the actual devices
* hardware - some information related to the hardware
* host - Programs to use on the USB host to interface with the hardware
The host software includes
* libosmo-simtrace2 - a shared library to talk to devices running the simtrace2 firmware
* simtrace2-list - list any USB-attached devices running simtrace2 firmware
* simtrace2-sniff - interface the 'trace' firmware to obtain card protocol traces
* simtrace2-cardem-pcsc - interface the 'cardem' fimrware to use a SIM in a PC/SC reader

View File

@@ -1,10 +0,0 @@
# When cleaning up this file: bump API version in corresponding Makefile.am and rename corresponding debian/lib*.install
# according to https://osmocom.org/projects/cellular-infrastructure/wiki/Make_a_new_release
# In short: https://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html#Updating-version-info
# LIBVERSION=c:r:a
# If the library source code has changed at all since the last update, then increment revision: c:r + 1:a.
# If any interfaces have been added, removed, or changed since the last update: c + 1:0:0.
# If any interfaces have been added since the last public release: c:r:a + 1.
# If any interfaces have been removed or changed since the last public release: c:r:0.
#library what description / commit summary line
simtrace2 API/ABI change osmo_st2_transport new member

View File

@@ -28,7 +28,7 @@ export PKG_CONFIG_PATH="$inst/lib/pkgconfig:$PKG_CONFIG_PATH"
export LD_LIBRARY_PATH="$inst/lib"
BUILDS=""
BUILDS+="simtrace/dfu simtrace/trace "
BUILDS+="simtrace/dfu simtrace/trace simtrace/cardem "
BUILDS+="qmod/dfu qmod/cardem "
BUILDS+="owhw/dfu owhw/cardem "
@@ -63,12 +63,9 @@ make dist
# make -C "$base/doc/manuals" publish
#fi
rm -rf $TOPDIR/firmware/bin/simtrace-cardem*
if [ "x$publish" = "x--publish" ]; then
echo
echo "=============== UPLOAD BUILD =============="
$TOPDIR/contrib/prepare_upload.sh
cat > "/build/known_hosts" <<EOF
[rita.osmocom.org]:48 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDDgQ9HntlpWNmh953a2Gc8NysKE4orOatVT1wQkyzhARnfYUerRuwyNr1GqMyBKdSI9amYVBXJIOUFcpV81niA7zQRUs66bpIMkE9/rHxBd81SkorEPOIS84W4vm3SZtuNqa+fADcqe88Hcb0ZdTzjKILuwi19gzrQyME2knHY71EOETe9Yow5RD2hTIpB5ecNxI0LUKDq+Ii8HfBvndPBIr0BWYDugckQ3Bocf+yn/tn2/GZieFEyFpBGF/MnLbAAfUKIdeyFRX7ufaiWWz5yKAfEhtziqdAGZaXNaLG6gkpy3EixOAy6ZXuTAk3b3Y0FUmDjhOHllbPmTOcKMry9

View File

@@ -1,16 +0,0 @@
#!/bin/sh -e
# Create copies of binaries with -latest, -$GIT_VERSION (OS#4413, OS#3452)
cd "$(dirname "$0")/.."
GIT_VERSION="$(./git-version-gen .tarball-version)"
echo "Copying binaries with "-latest" and "-$GIT_VERSION" appended..."
cd firmware/bin
for ext in bin elf; do
for file in *."$ext"; do
without_ext="${file%.*}"
cp -v "$file" "$without_ext-latest.$ext"
cp -v "$file" "$without_ext-$GIT_VERSION.$ext"
done
done

View File

@@ -1 +0,0 @@
3.0 (native)

View File

@@ -28,7 +28,7 @@
# Makefile for compiling the Getting Started with SAM3S Microcontrollers project
GIT_VERSION=$(shell $(TOP)/git-version-gen $(TOP)/.tarball-version)
GIT_VERSION=$(shell $(TOP)/git-version-gen $(TOP)/.tarvers)
#-------------------------------------------------------------------------------
# User-modifiable options
#-------------------------------------------------------------------------------
@@ -108,7 +108,7 @@ C_LIBUSB = USBDescriptors.c USBRequests.c USBD.c USBDCallbacks.c USBDDriver.
C_LIBUSB_RT = dfu.c dfu_runtime.c
C_LIBUSB_DFU = dfu.c dfu_desc.c dfu_driver.c
C_LIBCOMMON = string.c stdio.c fputs.c usb_buf.c ringbuffer.c pseudo_talloc.c host_communication.c \
main_common.c stack_check.c
main_common.c tc_etu.c
C_BOARD = $(notdir $(wildcard libboard/common/source/*.c))
C_BOARD += $(notdir $(wildcard libboard/$(BOARD)/source/*.c))
@@ -219,8 +219,12 @@ ASM_OBJECTS_$(1) = $(addprefix $(OBJ)/$(1)_, $(ASM_OBJECTS))
$(1): $$(ASM_OBJECTS_$(1)) $$(C_OBJECTS_$(1))
$(SILENT)$(CC) $(LDFLAGS) $(LD_OPTIONAL) -T"libboard/common/resources/$(CHIP)/$$@.ld" -Wl,-Map,$(OUTPUT)-$$@.map -o $(OUTPUT)-$$@.elf $$^ $(LIBS)
cp $(OUTPUT)-$$@.elf $(OUTPUT)-$$@-$(GIT_VERSION).elf
cp $(OUTPUT)-$$@.elf $(OUTPUT)-$$@-latest.elf
$(SILENT)$(NM) $(OUTPUT)-$$@.elf >$(OUTPUT)-$$@.elf.txt
$(SILENT)$(OBJCOPY) -O binary $(OUTPUT)-$$@.elf $(OUTPUT)-$$@.bin
cp $(OUTPUT)-$$@.bin $(OUTPUT)-$$@-$(GIT_VERSION).bin
cp $(OUTPUT)-$$@.bin $(OUTPUT)-$$@-latest.bin
$(SILENT)$(SIZE) $$^ $(OUTPUT)-$$@.elf
$$(C_OBJECTS_$(1)): $(OBJ)/$(1)_%.o: %.c Makefile $(OBJ) $(BIN)

View File

@@ -1,3 +1,3 @@
C_FILES += $(C_LIBUSB_RT)
C_FILES += card_emu.c cciddriver.c iso7816_4.c iso7816_fidi.c mitm.c mode_cardemu.c mode_ccid.c simtrace_iso7816.c sniffer.c usb.c
C_FILES += card_emu.c iso7816_fidi.c iso7816_3.c iso7816_4.c mode_cardemu.c simtrace_iso7816.c usb.c

View File

@@ -165,7 +165,7 @@ extern int main(void)
while (USBD_GetState() < USBD_STATE_CONFIGURED) {
WDT_Restart(WDT);
check_exec_dbg_cmd();
#if 0
#if 1
if (i >= MAX_USB_ITER * 3) {
TRACE_ERROR("Resetting board (USB could "
"not be configured)\n\r");

View File

@@ -243,17 +243,6 @@ static void check_exec_dbg_cmd(void)
//board_exec_dbg_cmd(ch);
}
/* print a horizontal line of '=' characters; Doing this in a loop vs. using a 'const'
* string saves us ~60 bytes of executable size (matters particularly for DFU loader) */
static void print_line(void)
{
int i;
for (i = 0; i < 78; i++)
fputc('=', stdout);
fputc('\n', stdout);
fputc('\r', stdout);
}
/*------------------------------------------------------------------------------
* Main
*------------------------------------------------------------------------------*/
@@ -276,14 +265,16 @@ extern int main(void)
PIO_Clear(&pinsLeds[LED_NUM_GREEN]);
#endif
PIO_InitializeInterrupts(0);
EEFC_ReadUniqueID(g_unique_id);
printf("\n\r\n\r");
print_line();
printf("DFU bootloader %s for board %s\n\r"
"(C) 2010-2017 by Harald Welte, 2018-2019 by Kevin Redon\n\r",
printf("\n\r\n\r"
"=============================================================================\n\r"
"DFU bootloader %s for board %s\n\r"
"(C) 2010-2017 by Harald Welte, 2018-2019 by Kevin Redon\n\r"
"=============================================================================\n\r",
manifest_revision, manifest_board);
print_line();
#if (TRACE_LEVEL >= TRACE_LEVEL_INFO)
TRACE_INFO("Chip ID: 0x%08lx (Ext 0x%08lx)\n\r", CHIPID->CHIPID_CIDR, CHIPID->CHIPID_EXID);

View File

@@ -1,3 +1,3 @@
C_FILES += $(C_LIBUSB_RT)
C_FILES += card_emu.c cciddriver.c iso7816_4.c iso7816_fidi.c mitm.c mode_cardemu.c mode_ccid.c simtrace_iso7816.c sniffer.c usb.c
C_FILES += iso7816_4.c iso7816_fidi.c simtrace_iso7816.c sniffer.c usb.c

View File

@@ -1,3 +1,3 @@
C_FILES += $(C_LIBUSB_RT)
C_FILES += card_emu.c iso7816_4.c iso7816_fidi.c mitm.c mode_cardemu.c mode_ccid.c simtrace_iso7816.c sniffer.c usb.c
C_FILES += card_emu.c iso7816_4.c iso7816_fidi.c mitm.c mode_cardemu.c mode_ccid.c simtrace_iso7816.c sniffer.c tc_etu.c usb.c

View File

@@ -39,8 +39,8 @@ struct dfu_desc {
#define DFU_FUNC_DESC { \
.bLength = USB_DT_DFU_SIZE, \
.bDescriptorType = USB_DT_DFU, \
.bmAttributes = USB_DFU_CAN_UPLOAD | USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH, \
.wDetachTimeOut = 0x00, \
.bmAttributes = USB_DFU_CAN_UPLOAD | USB_DFU_CAN_DOWNLOAD, \
.wDetachTimeOut = 0xff00, \
.wTransferSize = BOARD_DFU_PAGE_SIZE, \
.bcdDFUVersion = 0x0100, \
}

View File

@@ -165,8 +165,6 @@ void USBDFU_Runtime_RequestHandler(const USBGenericRequest *request)
* will then trigger DFURT_SwitchToDFU() below */
TRACE_DEBUG("\r\n====dfu_detach\n\r");
g_dfu->state = DFU_STATE_appDETACH;
USBD_Write(0, 0, 0, 0, 0);
DFURT_SwitchToDFU();
ret = DFU_RET_ZLP;
goto out;
break;
@@ -211,14 +209,13 @@ out:
void DFURT_SwitchToDFU(void)
{
__disable_irq();
/* store the magic value that the DFU loader can detect and
* activate itself, rather than boot into the application */
g_dfu->magic = USB_DFU_MAGIC;
__DMB();
/* Disconnect the USB by removing the pull-up */
USBD_Disconnect();
__disable_irq();
/* reset the processor, we will start execution with the
* ResetVector of the bootloader */

View File

@@ -111,4 +111,7 @@
#define CARDEMU_SECOND_UART
#define DETECT_VCC_BY_ADC
/** sysmoQMOD only supports card emulation */
#ifdef APPLICATION_cardem
#define HAVE_CARDEM
#endif

View File

@@ -80,9 +80,9 @@
/* Phone I/O data signal input/output (I/O_PHONE in schematic) */
#define PIN_USIM1_IO {PIO_PA22A_TXD1, PIOA, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT}
/* Phone CLK clock input (CLK_PHONE in schematic) */
#define PIN_USIM1_CLK {PIO_PA23A_SCK1, PIOA, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT}
#define PIN_PHONE_CLK {PIO_PA23A_SCK1, PIOA, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT}
/* Pin used for phone USIM slot 1 communication */
#define PINS_USIM1 PIN_USIM1_IO, PIN_USIM1_CLK, PIN_PHONE_CLK_INPUT, PIN_USIM1_VCC, PIN_PHONE_IO_INPUT, PIN_USIM1_nRST
#define PINS_USIM1 PIN_USIM1_IO, PIN_PHONE_CLK, PIN_PHONE_CLK_INPUT, PIN_USIM1_VCC, PIN_PHONE_IO_INPUT, PIN_USIM1_nRST
/* Phone I/O data signal input/output (unused USART RX input; connected to I/O_PHONE in schematic) */
#define PIN_PHONE_IO_INPUT {PIO_PA21A_RXD1, PIOA, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT}
/* Pin used as clock input (to measure the ETU duration; connected to CLK_PHONE in schematic) */
@@ -120,6 +120,14 @@
/* ISO7816-communication related pins */
#define PINS_ISO7816 PIN_SIM_IO, PIN_SIM_CLK, PIN_ISO7816_RSTMC // SIM_PWEN_PIN, PIN_SIM_IO2, PIN_SIM_CLK2
/** card emulation configuration */
/* Disable power converter 4.5-6V to 3.3V (active high) */
#define PIN_SIM_PWEN_CARDEMU {PIO_PA5, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}
/* Disable power switch to forward VCC_PHONE to VCC_SIM (active high) */
#define PIN_VCC_FWD_CARDEMU {PIO_PA26, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}
/* Disable power to SIM */
#define PINS_PWR_CARDEMU PIN_SIM_PWEN_CARDEMU, PIN_VCC_FWD_CARDEMU
/** External SPI flash interface **/
/* SPI MISO pin definition */
#define PIN_SPI_MISO {PIO_PA12A_MISO, PIOA, PIOA, PIO_PERIPH_A, PIO_PULLUP}
@@ -153,10 +161,18 @@
/** Supported modes */
/* SIMtrace board supports sniffer mode */
#ifdef APPLICATION_trace
#define HAVE_SNIFFER
#endif
/* SIMtrace board supports CCID mode */
#ifdef APPLICATION_ccid
//#define HAVE_CCID
#endif
/* SIMtrace board supports card emulation mode */
//#define HAVE_CARDEM
#ifdef APPLICATION_cardem
#define HAVE_CARDEM
#endif
/* SIMtrace board supports man-in-the-middle mode */
#ifdef APPLICATION_mitm
//#define HAVE_MITM
#endif

View File

@@ -22,6 +22,15 @@
#include "led.h"
#include "sim_switch.h"
#ifdef PIN_SIM_SWITCH1
static const Pin pin_conn_usim1 = {PIO_PA20, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
#endif
#ifdef PIN_SIM_SWITCH2
static const Pin pin_conn_usim2 = {PIO_PA28, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
#endif
static int initialized = 0;
int sim_switch_use_physical(unsigned int nr, int physical)
{
const Pin pin_sc = PIN_SC_SW_DEFAULT; // pin to control bus switch for VCC/RST/CLK signals

View File

@@ -89,7 +89,7 @@
/// \param condition Condition to verify.
#define ASSERT(condition) { \
if (!(condition)) { \
printf_sync("-F- ASSERT: %s %s:%d\n\r", #condition, __BASE_FILE__, __LINE__); \
printf("-F- ASSERT: %s %s:%d\n\r", #condition, __BASE_FILE__, __LINE__); \
while (1); \
} \
}

View File

@@ -31,6 +31,7 @@ enum card_io {
/** initialise card slot
* @param[in] slot_num slot number (arbitrary number)
* @param[in] tc_chan timer counter channel (to measure the ETU)
* @param[in] uart_chan UART peripheral channel
* @param[in] in_ep USB IN end point number
* @param[in] irq_ep USB INTerrupt end point number
@@ -39,7 +40,7 @@ enum card_io {
* @param[in] clocked initial CLK signat state (true = active)
* @return main card handle reference
*/
struct card_handle *card_emu_init(uint8_t slot_num, uint8_t uart_chan, uint8_t in_ep, uint8_t irq_ep, bool vcc_active, bool in_reset, bool clocked);
struct card_handle *card_emu_init(uint8_t slot_num, uint8_t tc_chan, uint8_t uart_chan, uint8_t in_ep, uint8_t irq_ep, bool vcc_active, bool in_reset, bool clocked);
/* process a single byte received from the reader */
void card_emu_process_rx_byte(struct card_handle *ch, uint8_t byte);
@@ -57,17 +58,43 @@ struct llist_head *card_emu_get_uart_tx_queue(struct card_handle *ch);
void card_emu_have_new_uart_tx(struct card_handle *ch);
void card_emu_report_status(struct card_handle *ch, bool report_on_irq);
void card_emu_wtime_half_expired(void *ch);
void card_emu_wtime_expired(void *ch);
/*! call when the waiting time has half-expired
* param[in] ch card for which the waiting time half expired
*/
void card_emu_wt_halfed(void *ch);
/*! call when the waiting time has expired
* param[in] ch card for which the waiting time expired
*/
void card_emu_wt_expired(void *ch);
#define ENABLE_TX 0x01
#define ENABLE_RX 0x02
#define ENABLE_TX 0x01
#define ENABLE_RX 0x02
#define ENABLE_TX_TIMER_ONLY 0x03
// the following functions are callbacks implement in mode_cardemu.c
int card_emu_uart_update_fidi(uint8_t uart_chan, unsigned int fidi);
/*! update F and D on USART peripheral
* @param[in] usart USART peripheral to configure
* @param[in] f clock rate conversion integer F value
* @param[in] d baud rate adjustment factor D value
* @note this should happen after reset and protocol select (through PPS or implicit)
*/
void card_emu_uart_update_fd(uint8_t uart_chan, uint16_t f, uint8_t d);
/*! update WT on USART peripheral
* @param[in] usart USART peripheral to configure
* @param[in] wt inactivity Waiting Time before card_emu_wt_expired is called (0 to disable)
*/
void card_emu_uart_update_wt(uint8_t uart_chan, uint32_t wt);
/*! reset waiting timeout count down on USART peripheral
* @param[in] usart USART peripheral to configure
*/
void card_emu_uart_reset_wt(uint8_t uart_chan);
/*! set I/O line high
* @param[in] usart USART peripheral to configure
* @param[in] set if I/O line should be set high (true), or cleared low (false)
*/
void card_emu_uart_io_set(uint8_t uart_chan, bool set);
int card_emu_uart_tx(uint8_t uart_chan, uint8_t byte);
void card_emu_uart_enable(uint8_t uart_chan, uint8_t rxtx);
void card_emu_uart_wait_tx_idle(uint8_t uart_chan);

View File

@@ -0,0 +1,98 @@
/*
* Copyright (C) 2019 sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/* this library provides utilities to handle the ISO-7816 part 3 communication aspects (e.g. related to F and D) */
#pragma once
#include <stdint.h>
#include <stdbool.h>
/** default clock rate conversion integer Fd
* @implements ISO/IEC 7816-3:2006(E) section 8.1
*/
#define ISO7816_3_DEFAULT_FD 372
/** default baud rate adjustment factor Dd
* @implements ISO/IEC 7816-3:2006(E) section 8.1
*/
#define ISO7816_3_DEFAULT_DD 1
/** default clock rate conversion integer Fi
* @implements ISO/IEC 7816-3:2006(E) section 8.3
* @note non-default value is optionally specified in TA1
*/
#define ISO7816_3_DEFAULT_FI 372
/** default baud rate adjustment factor Di
* @implements ISO/IEC 7816-3:2006(E) section 8.3
* @note non-default value is optionally specified in TA1
*/
#define ISO7816_3_DEFAULT_DI 1
/** default maximum clock frequency, in Hz
* @implements ISO/IEC 7816-3:2006(E) section 8.3
* @note non-default value is optionally specified in TA1
*/
#define ISO7816_3_DEFAULT_FMAX 5000000UL
/** default Waiting Integer (WI) value for T=0
* @implements ISO/IEC 7816-3:2006(E) section 10.2
* @note non-default value is optionally specified in TC2
*/
#define ISO7816_3_DEFAULT_WI 10
/** default Waiting Time (WT) value, in ETU
* @implements ISO/IEC 7816-3:2006(E) section 8.1
* @note depends on Fi, Di, and WI if protocol T=0 is selected
*/
#define ISO7816_3_DEFAULT_WT 9600
/** Table encoding the clock rate conversion integer Fi
* @note Fi is indicated in TA1, but the same table is used for F and Fn during PPS
* @implements ISO/IEC 7816-3:2006(E) table 7
*/
extern const uint16_t iso7816_3_fi_table[];
/** Table encoding the maximum clock frequency f_max in Hz
* @implements ISO/IEC 7816-3:2006(E) table 7
* @note f_max is indicated in TA1, but the same table is used for F and Fn during PPS
*/
extern const uint32_t iso7816_3_fmax_table[];
/** Table encoding the baud rate adjust integer Di
* @implements ISO/IEC 7816-3:2006(E) table 8
* @note Di is indicated in TA1, but the same table is used for D and Dn during PPS
*/
extern const uint8_t iso7816_3_di_table[];
/* verify if the clock rate conversion integer F value is valid
* @param[in] f F value to be validated
* @return if F value is valid
* @note only values in ISO/IEC 7816-3:2006(E) table 7 are valid
*/
bool iso7816_3_valid_f(uint16_t f);
/* verify if the baud rate adjustment factor D value is valid
* @param[in] d D value to be validated
* @return if D value is valid
* @note only values in ISO/IEC 7816-3:2006(E) table 8 are valid
*/
bool iso7816_3_valid_d(uint8_t d);
/** calculate Waiting Time (WT)
* @param[in] wi Waiting Integer
* @param[in] fi clock rate conversion integer Fi value
* @param[in] di baud rate adjustment factor Di value
* @param[in] f clock rate conversion integer F value
* @param[in] d baud rate adjustment factor D value
* @return Waiting Time WT, in ETU, or < 0 on error (see code for return codes)
* @note this should happen after reset and T=0 protocol select (through PPS or implicit)
* @implements ISO/IEC 7816-3:2006(E) section 8.1 and 10.2
*/
int32_t iso7816_3_calculate_wt(uint8_t wi, uint16_t fi, uint8_t di, uint16_t f, uint8_t d);

View File

@@ -21,10 +21,10 @@
#include <stdint.h>
/* Table 7 of ISO 7816-3:2006 */
extern const uint16_t iso7816_3_fi_table[16];
extern const uint16_t fi_table[];
/* Table 8 from ISO 7816-3:2006 */
extern const uint8_t iso7816_3_di_table[16];
extern const uint8_t di_table[];
/* compute the F/D ratio based on F_index and D_index values */
int iso7816_3_compute_fd_ratio(uint8_t f_index, uint8_t d_index);
/* compute the F/D ratio based on Fi and Di values */
int compute_fidi_ratio(uint8_t fi, uint8_t di);

View File

@@ -230,17 +230,10 @@ struct cardemu_usb_msg_status {
uint32_t flags;
/* phone-applied target voltage in mV */
uint16_t voltage_mv;
/* F/D related information. Not actual Fn/Dn values but indexes into tables! */
union {
uint8_t F_index; /* <! Index to ISO7816-3 Table 7 (F and f_max values) */
uint8_t fi; /* <! old, wrong name for API compatibility */
};
union {
uint8_t D_index; /* <! Index to ISO7816-3 Table 8 (D value) */
uint8_t di; /* <! old, wrong name for API compatibility */
};
uint8_t wi; /* <! Waiting Integer as defined in ISO7816-3 Section 10.2 */
uint32_t waiting_time; /* <! Waiting Time in etu as defined in ISO7816-3 Section 8.1 */
uint8_t f; /*!< index of F and f_max values as encoded in ISO/IEC 7816-3:2006(E) Table 7 */
uint8_t d; /*!< index of D value as encoded in ISO/IEC 7816-3:2006(E) Table 8 */
uint8_t wi; /*!< Waiting Integer as defined in ISO/IEC 7816-3:2006(E) Section 10.2 */
uint32_t wt; /*!< Waiting Time in ETU as defined in ISO/IEC 7816-3:2006(E) Section 8.1 */
} __attribute__ ((packed));
/* CEMU_USB_MSGT_DO_PTS */

View File

@@ -7,5 +7,3 @@ void tc_etu_init(uint8_t chan_nr, void *handle);
void tc_etu_enable(uint8_t chan_nr);
void tc_etu_disable(uint8_t chan_nr);
extern void tc_etu_wtime_half_expired(void *handle);
extern void tc_etu_wtime_expired(void *handle);

View File

@@ -1,7 +1,7 @@
/* ISO7816-3 state machine for the card side
*
* (C) 2010-2021 by Harald Welte <laforge@gnumonks.org>
* (C) 2018 by sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
* (C) 2010-2019 by Harald Welte <laforge@gnumonks.org>
* (C) 2018-2019 by sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -26,7 +26,9 @@
#include "utils.h"
#include "trace.h"
#include "iso7816_3.h"
#include "iso7816_fidi.h"
#include "tc_etu.h"
#include "card_emu.h"
#include "simtrace_prot.h"
#include "usb_buf.h"
@@ -153,34 +155,54 @@ struct card_handle {
bool in_reset; /*< if card is in reset (true = RST low/asserted, false = RST high/ released) */
bool clocked; /*< if clock is active ( true = active, false = inactive) */
/* All below variables with _index suffix are indexes from 0..15 into Tables 7 + 8
* of ISO7816-3. */
/*! Index to clock rate conversion integer Fi (ISO7816-3 Table 7).
* \note this represents the maximum value supported by the card, and can be indicated in TA1 */
uint8_t Fi_index;
/*! Current value of index to clock rate conversion integer F (ISO 7816-3 Section 7.1). */
uint8_t F_index;
/*! Index to baud rate adjustment factor Di (ISO7816-3 Table 8).
* \note this represents the maximum value supported by the card, and can be indicated in TA1 */
uint8_t Di_index;
/*! Current value of index to baud rate adjustment factor D (ISO 7816-3 Section 7.1). */
uint8_t D_index;
/*! Waiting Integer (ISO7816-3 Section 10.2).
* \note this value can be set in TA2 */
uint8_t wi;
/*! Waiting Time, in ETU (ISO7816-3 Section 8.1).
* \note this depends on Fi, Di, and WI if T=0 is used */
uint32_t waiting_time; /* in etu */
uint8_t tc_chan; /* TC channel number */
uint8_t uart_chan; /* UART channel */
uint8_t in_ep; /* USB IN EP */
uint8_t irq_ep; /* USB IN EP */
/*! clock rate conversion integer F
* @implements ISO/IEC 7816-3:2006(E) section 7.1
* @note this represents the current value used
*/
uint16_t f;
/*! baud rate adjustment factor D
* @implements ISO/IEC 7816-3:2006(E) section 7.1
* @note this represents the current value used
*/
uint8_t d;
/*! clock frequency in Hz
* @implements ISO/IEC 7816-3:2006(E) section 7.1
* @note the USART peripheral in slave mode does not provide the current value. we could measure it but this is not really useful. instead we remember the maximum possible value corresponding to the selected F value
*/
uint32_t f_cur;
/*! clock rate conversion integer Fi
* @implements ISO/IEC 7816-3:2006(E) Table 7
* @note this represents the maximum value supported by the card, and can be indicated in TA1
* @note this value can be set in TA1
*/
uint16_t fi;
/*! baud rate adjustment factor Di
* @implements ISO/IEC 7816-3:2006(E) Table 8
* @note this represents the maximum value supported by the card, and can be indicated in TA1
*/
uint8_t di;
/*! clock frequency, in Hz
* @implements ISO/IEC 7816-3:2006(E) Table 7
* @note this represents the maximum value supported by the card, and can be indicated in TA1
*/
uint32_t f_max;
/*! Waiting Integer
* @implements ISO/IEC 7816-3:2006(E) Section 10.2
* @note this value can be set in TA2
*/
uint8_t wi;
/*! Waiting Time, in ETU
* @implements ISO/IEC 7816-3:2006(E) Section 8.1
* @note this depends on Fi, Di, and WI if T=0 is used
*/
uint32_t wt;
/* ATR state machine */
struct {
uint8_t idx;
@@ -220,7 +242,9 @@ static void card_handle_reset(struct card_handle *ch)
{
struct msgb *msg;
card_emu_uart_update_wt(ch->uart_chan, 0);
#ifndef BOARD_simtrace
tc_etu_disable(ch->tc_chan);
#endif
/* release any buffers we may still own */
if (ch->uart_tx_msg) {
@@ -375,14 +399,16 @@ static void emu_update_fidi(struct card_handle *ch)
{
int rc;
rc = iso7816_3_compute_fd_ratio(ch->F_index, ch->D_index);
rc = compute_fidi_ratio(ch->fi, ch->di);
if (rc > 0 && rc < 0x400) {
TRACE_INFO("%u: computed F(%u)/D(%u) ratio: %d\r\n", ch->num,
ch->F_index, ch->D_index, rc);
TRACE_INFO("%u: computed Fi(%u) Di(%u) ratio: %d\r\n",
ch->num, ch->fi, ch->di, rc);
/* make sure UART uses new F/D ratio */
card_emu_uart_update_fidi(ch->uart_chan, rc);
/* notify ETU timer about this */
tc_etu_set_etu(ch->tc_chan, rc);
} else
TRACE_INFO("%u: computed F/D ratio %d unsupported\r\n",
TRACE_INFO("%u: computed FiDi ration %d unsupported\r\n",
ch->num, rc);
}
@@ -402,35 +428,66 @@ static void card_set_state(struct card_handle *ch,
case ISO_S_WAIT_POWER:
case ISO_S_WAIT_CLK:
case ISO_S_WAIT_RST:
/* disable Rx and Tx of UART */
card_emu_uart_enable(ch->uart_chan, 0);
/* disable timeout */
card_emu_uart_update_wt(ch->uart_chan, 0);
card_emu_uart_enable(ch->uart_chan, 0); // disable Rx and Tx of UART
#ifdef BOARD_simtrace
card_emu_uart_update_wt(ch->uart_chan, 0); // disable timeout
if (ISO_S_WAIT_POWER == new_state) {
card_emu_uart_io_set(ch->uart_chan, false); // pull I/O line low
} else {
card_emu_uart_io_set(ch->uart_chan, true); // pull I/O line high
}
#endif
break;
case ISO_S_WAIT_ATR:
/* Reset to initial Fi / Di ratio */
ch->Fi_index = ch->F_index = 1;
ch->Di_index = ch->D_index = 1;
// reset the ETU-related values
ch->f = ISO7816_3_DEFAULT_FD;
ch->d = ISO7816_3_DEFAULT_DD;
#ifdef BOARD_simtrace
card_emu_uart_update_fd(ch->uart_chan, ch->f, ch->d); // set baud rate
// reset values optionally specified in the ATR
ch->fi = ISO7816_3_DEFAULT_FI;
ch->di = ISO7816_3_DEFAULT_DI;
ch->wi = ISO7816_3_DEFAULT_WI;
ch->waiting_time = ISO7816_3_INIT_WTIME;
emu_update_fidi(ch);
/* enable TX to be able to use the timeout */
card_emu_uart_enable(ch->uart_chan, ENABLE_TX_TIMER_ONLY);
int32_t wt = iso7816_3_calculate_wt(ch->wi, ch->fi, ch->di, ch->f, ch->d); // get default waiting time
if (wt <= 0) {
TRACE_FATAL("%u: invalid WT %ld\r\n", ch->num, wt);
}
ch->wt = wt;
card_emu_uart_enable(ch->uart_chan, ENABLE_TX); // enable TX to be able to use the timeout
/* the ATR should only be sent 400 to 40k clock cycles after the RESET.
* we use the UART timeout mechanism to wait this time.
* since the initial ETU is Fd=372/Dd=1 clock cycles long, we have to wait 2-107 ETU.
*/
card_emu_uart_update_wt(ch->uart_chan, 2);
#else
/* Reset to initial Fi / Di ratio */
ch->f = 1;
ch->d = 1;
emu_update_fidi(ch);
/* the ATR should only be sent 400 to 40k clock cycles after the RESET.
* we use the tc_etu mechanism to wait this time.
* since the initial ETU is Fd=372/Dd=1 clock cycles long, we have to wait 2-107 ETU.
*/
tc_etu_set_wtime(ch->tc_chan, 2);
/* enable the TC/ETU counter once reset has been released */
tc_etu_enable(ch->tc_chan);
#endif
break;
case ISO_S_IN_ATR:
#ifndef BOARD_simtrace
/* initialize to default WI, this will be overwritten if we
* send TC2, and it will be programmed into hardware after
* ATR is finished */
ch->wi = ISO7816_3_DEFAULT_WI;
/* update waiting time to initial waiting time */
ch->waiting_time = ISO7816_3_INIT_WTIME;
ch->wt = ISO7816_3_INIT_WTIME;
/* set initial waiting time */
card_emu_uart_update_wt(ch->uart_chan, ch->waiting_time);
tc_etu_set_wtime(ch->tc_chan, ch->wt);
#endif
/* Set ATR sub-state to initial state */
ch->atr.idx = 0;
/* enable USART transmission to reader */
@@ -505,11 +562,15 @@ static int tx_byte_atr(struct card_handle *ch)
}
}
}
/* update waiting time (see ISO 7816-3 10.2). We can drop the Fi
* multiplier as we store the waiting time in units of 'etu', and
* don't really care what the number of clock cycles or the absolute
* wall clock time is */
ch->waiting_time = ch->wi * 960;
#ifdef BOARD_simtrace
/* FIXME update waiting time in case of card is specific mode */
/* reset PTS to initial state */
set_pts_state(ch, PTS_S_WAIT_REQ_PTSS);
#else
/* update waiting time (see ISO 7816-3 10.2) */
ch->wt = ch->wi * 960 * ch->fi;
tc_etu_set_wtime(ch->tc_chan, ch->wt);
#endif
/* go to next state */
card_set_state(ch, ISO_S_WAIT_TPDU);
return 0;
@@ -575,9 +636,12 @@ from_pts3:
return PTS_S_WAIT_REQ_PCK | is_resp;
}
static int
process_byte_pts(struct card_handle *ch, uint8_t byte)
/*! process incoming PTS byte
* @param[in] ch card handle on which the byte has been received
* @param[in] byte received PTS byte
* @return new iso7816_3_card_state or -1 at the end of PTS request
*/
static int process_byte_pts(struct card_handle *ch, uint8_t byte)
{
switch (ch->pts.state) {
case PTS_S_WAIT_REQ_PTSS:
@@ -643,12 +707,17 @@ static int tx_byte_pts(struct card_handle *ch)
break;
case PTS_S_WAIT_RESP_PTS1:
byte = ch->pts.resp[_PTS1];
/* This must be TA1 */
ch->F_index = byte >> 4;
ch->D_index = byte & 0xf;
TRACE_DEBUG("%u: found F=%u D=%u\r\n", ch->num,
iso7816_3_fi_table[ch->F_index], iso7816_3_di_table[ch->D_index]);
/* FIXME: if F or D are 0, become unresponsive to signal error condition */
// TODO the value should have been validated when receiving the request
ch->f = iso7816_3_fi_table[byte >> 4]; // save selected Fn
if (0 == ch->f) {
TRACE_ERROR("%u: invalid F index in PPS response: %u\r\n", ch->num, byte >> 4);
// TODO become unresponsive to signal error condition
}
ch->d = iso7816_3_di_table[byte & 0xf]; // save selected Dn
if (0 == ch->d) {
TRACE_ERROR("%u: invalid D index in PPS response: %u\r\n", ch->num, byte & 0xf);
// TODO become unresponsive to signal error condition
}
break;
case PTS_S_WAIT_RESP_PTS2:
byte = ch->pts.resp[_PTS2];
@@ -673,8 +742,20 @@ static int tx_byte_pts(struct card_handle *ch)
switch (ch->pts.state) {
case PTS_S_WAIT_RESP_PCK:
card_emu_uart_wait_tx_idle(ch->uart_chan);
/* update baud rate generator with F/D */
#ifdef BOARD_simtrace
card_emu_uart_update_fd(ch->uart_chan, ch->f, ch->d); // set selected baud rate
int32_t wt = iso7816_3_calculate_wt(ch->wi, ch->fi, ch->di, ch->f, ch->d); // get new waiting time
if (wt <= 0) {
TRACE_ERROR("%u: invalid WT calculated: %ld\r\n", ch->num, wt);
// TODO become unresponsive to signal error condition
} else {
ch->wt = wt;
}
// FIXME disable WT
#else
/* update baud rate generator with Fi/Di */
emu_update_fidi(ch);
#endif
/* Wait for the next TPDU */
card_set_state(ch, ISO_S_WAIT_TPDU);
set_pts_state(ch, PTS_S_WAIT_REQ_PTSS);
@@ -745,6 +826,10 @@ static void set_tpdu_state(struct card_handle *ch, enum tpdu_state new_ts)
{
if (ch->tpdu.state == new_ts)
return;
if (ISO_S_IN_TPDU != ch->state && ISO_S_WAIT_TPDU != ch->state) {
TRACE_ERROR("%u: setting TPDU state in %s state\r\n", ch->num,
get_value_string(iso7816_3_card_state_names, ch->state));
}
TRACE_DEBUG("%u: 7816 TPDU state %s -> %s\r\n", ch->num,
get_value_string(tpdu_state_names, ch->tpdu.state),
@@ -752,29 +837,20 @@ static void set_tpdu_state(struct card_handle *ch, enum tpdu_state new_ts)
ch->tpdu.state = new_ts;
switch (new_ts) {
case TPDU_S_WAIT_CLA:
/* switch back to receiving mode */
card_emu_uart_enable(ch->uart_chan, ENABLE_RX);
/* disable waiting time since we don't expect any data */
card_emu_uart_update_wt(ch->uart_chan, 0);
case TPDU_S_WAIT_CLA: // we will be waiting for the next incoming TDPU
card_emu_uart_enable(ch->uart_chan, ENABLE_RX); // switch back to receiving mode
card_emu_uart_update_wt(ch->uart_chan, 0); // disable waiting time since we don't expect any data
break;
case TPDU_S_WAIT_INS:
/* start waiting for the rest of the header/body */
card_emu_uart_update_wt(ch->uart_chan, ch->waiting_time);
case TPDU_S_WAIT_INS: // the reader started sending the TPDU header
card_emu_uart_update_wt(ch->uart_chan, ch->wt); // start waiting for the rest of the header/body
break;
case TPDU_S_WAIT_RX:
/* switch to receive mode to receive the body */
card_emu_uart_enable(ch->uart_chan, ENABLE_RX);
/* start waiting for the body */
card_emu_uart_update_wt(ch->uart_chan, ch->waiting_time);
case TPDU_S_WAIT_RX: // the reader should send us the TPDU body data
card_emu_uart_enable(ch->uart_chan, ENABLE_RX); // switch to receive mode to receive the body
card_emu_uart_update_wt(ch->uart_chan, ch->wt); // start waiting for the rest body
break;
case TPDU_S_WAIT_PB:
/* we just completed the TPDU header from reader to card
* and now need to disable the receiver, enable the
* transmitter and transmit the procedure byte */
card_emu_uart_enable(ch->uart_chan, ENABLE_TX);
/* prepare to extend the waiting time once half of it is reached */
card_emu_uart_update_wt(ch->uart_chan, ch->waiting_time);
card_emu_uart_enable(ch->uart_chan, ENABLE_TX); // header is completely received, now we need to transmit the procedure byte
card_emu_uart_update_wt(ch->uart_chan, ch->wt); // prepare to extend the waiting time once half of it is reached
break;
default:
break;
@@ -1058,11 +1134,11 @@ void card_emu_report_status(struct card_handle *ch, bool report_on_irq)
sts->flags |= CEMU_STATUS_F_CLK_ACTIVE;
if (ch->in_reset)
sts->flags |= CEMU_STATUS_F_RESET_ACTIVE;
/* FIXME: voltage + card insert */
sts->F_index = ch->F_index;
sts->D_index = ch->D_index;
/* FIXME set voltage and card insert values */
sts->f = ch->f;
sts->d = ch->d;
sts->wi = ch->wi;
sts->waiting_time = ch->waiting_time;
sts->wt = ch->wt;
usb_buf_upd_len_and_submit(msg);
}
@@ -1117,7 +1193,7 @@ void card_emu_io_statechg(struct card_handle *ch, enum card_io io, int active)
case CARD_IO_RST:
if (active == 0 && ch->in_reset) {
TRACE_INFO("%u: RST released\r\n", ch->num);
if (ch->vcc_active && ch->clocked && ch->state == ISO_S_WAIT_RST) {
if (ch->vcc_active && ch->clocked && ISO_S_WAIT_RST == ch->state) {
/* prepare to send the ATR */
card_set_state(ch, ISO_S_WAIT_ATR);
}
@@ -1126,10 +1202,14 @@ void card_emu_io_statechg(struct card_handle *ch, enum card_io io, int active)
TRACE_INFO("%u: RST asserted\r\n", ch->num);
card_handle_reset(ch);
chg_mask |= CEMU_STATUS_F_RESET_ACTIVE;
#ifdef BOARD_simtrace
card_set_state(ch, ISO_S_WAIT_RST);
#endif
}
ch->in_reset = active;
break;
default:
break;
}
switch (ch->state) {
@@ -1158,17 +1238,20 @@ int card_emu_set_atr(struct card_handle *ch, const uint8_t *atr, uint8_t len)
if (len > sizeof(ch->atr.atr))
return -1;
/* ignore new ATR for now since we PPS has not been tested
memcpy(ch->atr.atr, atr, len);
ch->atr.len = len;
ch->atr.idx = 0;
*/
#if TRACE_LEVEL >= TRACE_LEVEL_INFO
uint8_t i;
TRACE_INFO("%u: ATR set: ", ch->num);
for (i = 0; i < ch->atr.len; i++) {
for (i = 0; i < len; i++) {
TRACE_INFO_WP("%02x ", atr[i]);
}
TRACE_INFO_WP("\n\r");
TRACE_INFO("%u: ATR set currently ignored\n\r", ch->num);
#endif
/* FIXME: race condition with transmitting ATR to reader? */
@@ -1176,7 +1259,7 @@ int card_emu_set_atr(struct card_handle *ch, const uint8_t *atr, uint8_t len)
}
/* hardware driver informs us that one (more) ETU has expired */
void card_emu_wtime_half_expired(void *handle)
void card_emu_wt_halfed(void *handle)
{
struct card_handle *ch = handle;
/* transmit NULL procedure byte well before waiting time expires */
@@ -1186,10 +1269,8 @@ void card_emu_wtime_half_expired(void *handle)
case TPDU_S_WAIT_PB:
case TPDU_S_WAIT_TX:
putchar('N');
/* we are waiting for data from the user. Send a procedure byte to ask the
* reader to wait more time */
card_emu_uart_tx(ch->uart_chan, ISO7816_3_PB_NULL);
card_emu_uart_reset_wt(ch->uart_chan);
card_emu_uart_tx(ch->uart_chan, ISO7816_3_PB_NULL); // we are waiting for data from the user. send a procedure byte to ask the reader to wait more time
card_emu_uart_reset_wt(ch->uart_chan); // reset WT
break;
default:
break;
@@ -1201,7 +1282,7 @@ void card_emu_wtime_half_expired(void *handle)
}
/* hardware driver informs us that one (more) ETU has expired */
void card_emu_wtime_expired(void *handle)
void card_emu_wt_expired(void *handle)
{
struct card_handle *ch = handle;
switch (ch->state) {
@@ -1210,6 +1291,7 @@ void card_emu_wtime_expired(void *handle)
card_set_state(ch, ISO_S_IN_ATR);
break;
default:
// TODO become unresponsive
TRACE_ERROR("%u: wtime_exp\r\n", ch->num);
break;
}
@@ -1217,21 +1299,22 @@ void card_emu_wtime_expired(void *handle)
/* reasonable ATR offering all protocols and voltages
* smartphones might not care, but other readers do
*
* TS = 0x3B Direct Convention
* T0 = 0x80 Y(1): b1000, K: 0 (historical bytes)
* TD(1) = 0x80 Y(i+1) = b1000, Protocol T=0
* ----
* TD(2) = 0x81 Y(i+1) = b1000, Protocol T=1
* ----
* TD(3) = 0x1F Y(i+1) = b0001, Protocol T=15
* ----
* TA(4) = 0xC7 Clock stop: no preference - Class accepted by the card: (3G) A 5V B 3V C 1.8V
* ----
* Historical bytes
* TCK = 0x59 correct checksum
*/
static const uint8_t default_atr[] = { 0x3B, 0x80, 0x80, 0x81 , 0x1F, 0xC7, 0x59 };
TS = 0x3B Direct Convention
T0 = 0x80 Y(1): b1000, K: 0 (historical bytes)
TD(1) = 0x80 Y(i+1) = b1000, Protocol T=0
----
TD(2) = 0x81 Y(i+1) = b1000, Protocol T=1
----
TD(3) = 0x1F Y(i+1) = b0001, Protocol T=15
----
TA(4) = 0xC7 Clock stop: no preference - Class accepted by the card: (3G) A 5V B 3V C 1.8V
----
Historical bytes
TCK = 0x59 correct checksum
* */
static const uint8_t default_atr[] = { 0x3B, 0x80, 0x80, 0x81 , 0x1F, 0xC7, 0x59};
static struct card_handle card_handles[NUM_SLOTS];
@@ -1247,7 +1330,7 @@ int card_emu_set_config(struct card_handle *ch, const struct cardemu_usb_msg_con
return 0;
}
struct card_handle *card_emu_init(uint8_t slot_num, uint8_t uart_chan, uint8_t in_ep, uint8_t irq_ep, bool vcc_active, bool in_reset, bool clocked)
struct card_handle *card_emu_init(uint8_t slot_num, uint8_t tc_chan, uint8_t uart_chan, uint8_t in_ep, uint8_t irq_ep, bool vcc_active, bool in_reset, bool clocked)
{
struct card_handle *ch;
@@ -1260,6 +1343,7 @@ struct card_handle *card_emu_init(uint8_t slot_num, uint8_t uart_chan, uint8_t i
INIT_LLIST_HEAD(&ch->uart_tx_queue);
/* initialize the card_handle with reasonable defaults */
ch->num = slot_num;
ch->irq_ep = irq_ep;
ch->in_ep = in_ep;
@@ -1268,12 +1352,13 @@ struct card_handle *card_emu_init(uint8_t slot_num, uint8_t uart_chan, uint8_t i
ch->in_reset = in_reset;
ch->clocked = clocked;
ch->Fi_index = ch->F_index = 1;
ch->Di_index = ch->D_index = 1;
ch->fi = ISO7816_3_DEFAULT_FI;
ch->di = ISO7816_3_DEFAULT_DI;
ch->wi = ISO7816_3_DEFAULT_WI;
ch->wt = ISO7816_3_DEFAULT_WT;;
ch->tc_chan = tc_chan;
ch->uart_chan = uart_chan;
ch->waiting_time = ISO7816_3_INIT_WTIME;
ch->atr.idx = 0;
ch->atr.len = sizeof(default_atr);
@@ -1283,6 +1368,10 @@ struct card_handle *card_emu_init(uint8_t slot_num, uint8_t uart_chan, uint8_t i
ch->tpdu.state = TPDU_S_WAIT_CLA;
card_handle_reset(ch);
#ifndef BOARD_simtrace
/* simtrace uses uart timer instead */
tc_etu_init(ch->tc_chan, ch);
#endif
return ch;
}

View File

@@ -0,0 +1,123 @@
/*
* Copyright (C) 2019 sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdint.h>
#include <stddef.h>
#include "utils.h"
#include "iso7816_3.h"
const uint16_t iso7816_3_fi_table[16] = {
372, 372, 558, 744, 1116, 1488, 1860, 0,
0, 512, 768, 1024, 1536, 2048, 0, 0
};
const uint32_t iso7816_3_fmax_table[16] = {
4000000, 5000000, 6000000, 8000000, 12000000, 16000000, 20000000, 0,
0, 5000000, 7500000, 10000000, 15000000, 20000000, 0, 0
};
const uint8_t iso7816_3_di_table[16] = {
0, 1, 2, 4, 8, 16, 32, 64,
12, 20, 0, 0, 0, 0, 0, 0,
};
/* all values are based on the Elementary Time Unit (ETU), defined in ISO/IEC 7816-3 section 7.1
* this is the time required to transmit a bit, and is calculated as follows: 1 ETU = (F / D) x (1 / f) where:
* - F is the clock rate conversion integer
* - D is the baud rate adjustment factor
* - f is the clock frequency
* the possible F, f(max), and D values are defined in ISO/IEC 7816-3 table 7 and 8
* - the initial value for F (after reset) is Fd = 372
* - the initial value for D (after reset) is Dd = 1
* - the initial maximum frequency f(max) is 5 MHz
* the card must measure the ETU based on the clock signal provided by the reader
* one ETU (e.g. 1 bit) takes F/D clock cycles, which the card must count
*
* the card can indicate an alternative set of supported values Fi (with corresponding f(max)) and Di for higher baud rate in TA1 in the ATR (see ISO/IEC 7816-3 section 8.3)
* these values are selected according to ISO/IEC 7816-3 section 6.3.1:
* - card in specific mode: they are enforced if TA2 is present (the reader can deactivate the card if it does not support these values)
* - card in negotiable mode:
* -- they can be selected by the reader using the Protocol and Parameters Selection (PPS) procedure
* -- the first offered protocol and default values are used when no PPS is started
*
* PPS is done with Fd and Dd (see ISO/IEC 7816-3 section 9)
* the reader can propose any F and D values between from Fd to Fi, and from Dd to Di (Fi and Di are indicated in TA1)
* the in PPS agreed values F and D are called Fn and Dn and are applied after a successful exchange, corresponding to PPS1_Response bit 5
*
* the F and D values must be provided to the SAM3S USART peripheral (after reset and PPS)
*/
bool iso7816_3_valid_f(uint16_t f)
{
if (0 == f) {
return false;
}
uint8_t i = 0;
for (i = 0; i < ARRAY_SIZE(iso7816_3_fi_table) && iso7816_3_fi_table[i] != f; i++);
return (i < ARRAY_SIZE(iso7816_3_fi_table) && iso7816_3_fi_table[i] == f);
}
bool iso7816_3_valid_d(uint8_t d)
{
if (0 == d) {
return false;
}
uint8_t i = 0;
for (i = 0; i < ARRAY_SIZE(iso7816_3_di_table) && iso7816_3_di_table[i] != d; i++);
return (i < ARRAY_SIZE(iso7816_3_di_table) && iso7816_3_di_table[i] == d);
}
/*
* the ETU is not only used to define the baud rate, but also the Waiting Time (WT) (see ISO/IEC 7816-3 section 8.1)
* when exceeding WT without card response, the reader flags the card as unresponsive, and resets it
* this can be used by the card to indicate errors or unsupported operations
* if the card requires more time to respond, it shall send a procedure byte to restart WT
* WT is calculated as follows (for T=0, see ISO/IEC 7816-3 section 10.2): WT = WI x 960 x (Fi / f(max)) where
* - WI is encoded in TC2 in the ATR (10 if absent)
* - WI does not depend on D/Di (used for the ETU)
* - after reset WT is 9600 ETU
* - WI (e.g. the new WT) is applied when T=0 is used (after 6.3.1), even if Fi is not Fn (this WT extension is important to know for the reader so to have the right timeout)
*/
int32_t iso7816_3_calculate_wt(uint8_t wi, uint16_t fi, uint8_t di, uint16_t f, uint8_t d)
{
// sanity checks
if (0 == wi) {
return -1;
}
if (!iso7816_3_valid_f(fi)) {
return -2;
}
if (!iso7816_3_valid_d(di)) {
return -3;
}
if (!iso7816_3_valid_f(f)) {
return -4;
}
if (!iso7816_3_valid_d(d)) {
return -5;
}
if (f > fi) {
return -6;
}
if (d > di) {
return -7;
}
return wi * 960UL * (fi/f) * (di/d); // calculate timeout value in ETU
}

View File

@@ -23,38 +23,38 @@
#include "iso7816_fidi.h"
/* Table 7 of ISO 7816-3:2006 */
const uint16_t iso7816_3_fi_table[] = {
const uint16_t fi_table[] = {
372, 372, 558, 744, 1116, 1488, 1860, 0,
0, 512, 768, 1024, 1536, 2048, 0, 0
};
/* Table 8 from ISO 7816-3:2006 */
const uint8_t iso7816_3_di_table[] = {
const uint8_t di_table[] = {
0, 1, 2, 4, 8, 16, 32, 64,
12, 20, 2, 4, 8, 16, 32, 64,
};
/* compute the F/D ratio based on Fi and Di values */
int iso7816_3_compute_fd_ratio(uint8_t f_index, uint8_t d_index)
int compute_fidi_ratio(uint8_t fi, uint8_t di)
{
uint16_t f, d;
int ret;
if (f_index >= ARRAY_SIZE(iso7816_3_fi_table) ||
d_index >= ARRAY_SIZE(iso7816_3_di_table))
if (fi >= ARRAY_SIZE(fi_table) ||
di >= ARRAY_SIZE(di_table))
return -EINVAL;
f = iso7816_3_fi_table[f_index];
f = fi_table[fi];
if (f == 0)
return -EINVAL;
d = iso7816_3_di_table[d_index];
d = di_table[di];
if (d == 0)
return -EINVAL;
/* See table 7 of ISO 7816-3: From 1000 on we divide by 1/d,
* which equals a multiplication by d */
if (d_index < 8)
if (di < 8)
ret = f / d;
else
ret = f * d;

View File

@@ -22,6 +22,7 @@
#include "simtrace.h"
#include "ringbuffer.h"
#include "card_emu.h"
#include "iso7816_3.h"
#include "iso7816_fidi.h"
#include "utils.h"
#include <osmocom/core/linuxlist.h>
@@ -54,19 +55,15 @@ struct cardem_inst {
struct card_handle *ch;
struct llist_head usb_out_queue;
struct ringbuf rb;
uint32_t wt; /*!< receiver waiting time to trigger timeout (0 to deactivate it) */
uint32_t wt_remaining; /*!< remaining waiting time */
bool wt_halfed; /*!< if at least half of the waiting time passed */
struct Usart_info usart_info;
struct {
/*! receiver waiting time to trigger timeout (0 to deactivate it) */
uint32_t total;
/*! remaining waiting time (we may need multiple timer runs to reach total */
uint32_t remaining;
/*! did we already notify about half the time having expired? */
bool half_time_notified;
} wt;
int usb_pending_old;
uint8_t ep_out;
uint8_t ep_in;
uint8_t ep_int;
const Pin pin_io;
const Pin pin_insert;
#ifdef DETECT_VCC_BY_ADC
uint32_t vcc_uv;
@@ -89,6 +86,7 @@ struct cardem_inst cardem_inst[] = {
.ep_out = SIMTRACE_CARDEM_USB_EP_USIM1_DATAOUT,
.ep_in = SIMTRACE_CARDEM_USB_EP_USIM1_DATAIN,
.ep_int = SIMTRACE_CARDEM_USB_EP_USIM1_INT,
.pin_io = PIN_USIM1_IO,
#ifdef PIN_SET_USIM1_PRES
.pin_insert = PIN_SET_USIM1_PRES,
#endif
@@ -104,6 +102,7 @@ struct cardem_inst cardem_inst[] = {
.ep_out = SIMTRACE_CARDEM_USB_EP_USIM2_DATAOUT,
.ep_in = SIMTRACE_CARDEM_USB_EP_USIM2_DATAIN,
.ep_int = SIMTRACE_CARDEM_USB_EP_USIM2_INT,
.pin_io = PIN_USIM2_IO,
#ifdef PIN_SET_USIM2_PRES
.pin_insert = PIN_SET_USIM2_PRES,
#endif
@@ -149,23 +148,16 @@ void card_emu_uart_enable(uint8_t uart_chan, uint8_t rxtx)
Usart *usart = get_usart_by_chan(uart_chan);
switch (rxtx) {
case ENABLE_TX:
USART_DisableIt(usart, ~(US_IER_TXRDY | US_IER_TIMEOUT));
USART_DisableIt(usart, ~US_IER_TXRDY);
/* as irritating as it is, we actually want to keep the
* receiver enabled during transmit */
USART_SetReceiverEnabled(usart, 1);
usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
#ifdef BOARD_simtrace
USART_EnableIt(usart, US_IER_TXRDY | US_IER_TIMEOUT);
USART_SetTransmitterEnabled(usart, 1);
break;
case ENABLE_TX_TIMER_ONLY:
/* enable the transmitter without generating TXRDY interrupts
* just so that the timer can run */
USART_DisableIt(usart, ~US_IER_TIMEOUT);
/* as irritating as it is, we actually want to keep the
* receiver enabled during transmit */
USART_SetReceiverEnabled(usart, 1);
usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
USART_EnableIt(usart, US_IER_TIMEOUT);
#else
USART_EnableIt(usart, US_IER_TXRDY);
#endif
USART_SetTransmitterEnabled(usart, 1);
break;
case ENABLE_RX:
@@ -175,7 +167,11 @@ void card_emu_uart_enable(uint8_t uart_chan, uint8_t rxtx)
USART_SetTransmitterEnabled(usart, 1);
wait_tx_idle(usart);
usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
#ifdef BOARD_simtrace
USART_EnableIt(usart, US_IER_RXRDY | US_IER_TIMEOUT);
#else
USART_EnableIt(usart, US_IER_RXRDY);
#endif
USART_SetReceiverEnabled(usart, 1);
break;
case 0:
@@ -213,101 +209,63 @@ int card_emu_uart_tx(uint8_t uart_chan, uint8_t byte)
return 1;
}
static uint16_t compute_next_timeout(struct cardem_inst *ci)
{
uint32_t want_to_expire;
if (ci->wt.total == 0)
return 0;
if (!ci->wt.half_time_notified) {
/* we need to make sure to expire after half the total waiting time */
OSMO_ASSERT(ci->wt.remaining > (ci->wt.total / 2));
want_to_expire = ci->wt.remaining - (ci->wt.total / 2);
} else
want_to_expire = ci->wt.remaining;
/* if value exceeds the USART TO range, use the maximum possible value for one round */
return OSMO_MIN(want_to_expire, 0xffff);
}
/*! common handler if interrupt was received.
* \param[in] inst_num Instance number, range 0..1 (some boards only '0' permitted) */
/* FIXME: integrate this with actual irq handler */
static void usart_irq_rx(uint8_t inst_num)
{
OSMO_ASSERT(inst_num < ARRAY_SIZE(cardem_inst));
if (inst_num >= ARRAY_SIZE(cardem_inst)) {
TRACE_ERROR("%u: UART channel out of bounds\r\n", inst_num);
return;
}
Usart *usart = get_usart_by_chan(inst_num);
struct cardem_inst *ci = &cardem_inst[inst_num];
uint32_t csr;
uint8_t byte = 0;
uint32_t errflags = (US_CSR_OVRE | US_CSR_FRAME | US_CSR_PARE | US_CSR_NACK | (1 << 10));
#ifndef BOARD_simtrace
errflags |= US_CSR_TIMEOUT;
#endif
/* get one atomic snapshot of state/flags before they get changed */
csr = usart->US_CSR & usart->US_IMR;
csr = usart->US_CSR & usart->US_IMR; // save state/flags before they get changed
/* check if one byte has been completely received and is now in the holding register */
if (csr & US_CSR_RXRDY) {
/* read the bye from the holding register */
byte = (usart->US_RHR) & 0xFF;
/* append it to the buffer */
if (rbuf_write(&ci->rb, byte) < 0)
TRACE_ERROR("rbuf overrun\r\n");
if (csr & US_CSR_RXRDY) { // bytes has been received
byte = (usart->US_RHR) & 0xFF; // ready out byte
if (rbuf_write(&ci->rb, byte) < 0) // store byte in buffer
TRACE_ERROR("rbuf overrun\r\n"); // error if could not store in buffer
}
/* check if the transmitter is ready for the next byte */
if (csr & US_CSR_TXRDY) {
/* transmit next byte and check if more bytes are to be transmitted */
if (card_emu_tx_byte(ci->ch) == 0) {
/* stop the TX ready interrupt of no more bytes to transmit */
USART_DisableIt(usart, US_IER_TXRDY);
}
if (csr & US_CSR_TXRDY) { // ready to transmit the next byte
if (card_emu_tx_byte(ci->ch) == 0) // transmit next byte, and check if a byte is being transmitted
USART_DisableIt(usart, US_IER_TXRDY); // stop the TX ready signal if not byte has been transmitted
}
/* check if any error flags are set */
if (csr & (US_CSR_OVRE|US_CSR_FRAME|US_CSR_PARE|US_CSR_NACK|(1<<10))) {
/* clear any error flags */
usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
TRACE_ERROR("%u USART error on 0x%x status: 0x%lx\n", ci->num, byte, csr);
if (csr & errflags) { // error flag set
usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK; // reset UART state to clear flag
TRACE_ERROR("%u USART error on 0x%x status: 0x%lx\n", ci->num, byte, csr); // warn user about error
}
/* check if the timeout has expired. We "abuse" the receive timer for tracking
* how many etu have expired since we last sent a byte. See section
* 33.7.3.11 "Receiver Time-out" of the SAM3S8 Data Sheet */
if (csr & US_CSR_TIMEOUT) {
/* clear timeout flag (and stop timeout until next character is received) */
usart->US_CR |= US_CR_STTTO;
/* RX has been inactive for some time */
if (ci->wt.remaining <= (usart->US_RTOR & 0xffff)) {
/* waiting time is over; will stop the timer */
ci->wt.remaining = 0;
#ifdef BOARD_simtrace
// handle timeout
if (csr & US_CSR_TIMEOUT) { // RX has been inactive for some time
if (ci->wt_remaining <= (usart->US_RTOR & 0xffff)) { // waiting time has passed
ci->wt_remaining = 0; // timeout reached (will stop the timer)
} else {
/* subtract the actual timeout since the new might not have been set and
* reloaded yet */
ci->wt.remaining -= (usart->US_RTOR & 0xffff);
ci->wt_remaining -= (usart->US_RTOR & 0xffff); // be sure to subtract the actual timeout since the new might not have been set and reloaded yet
}
if (ci->wt.remaining == 0) {
/* let the FSM know that WT has expired */
card_emu_wtime_expired(ci->ch);
/* don't automatically re-start in this case */
if (0 == ci->wt_remaining) {
card_emu_wt_expired(ci->ch); // let the state know WT has expired
} else if (ci->wt_remaining <= ci->wt / 2 && !ci->wt_halfed) {
ci->wt_halfed = true;
card_emu_wt_halfed(ci->ch); // let the state know WT has half expired
}
if (ci->wt_remaining > 0xffff) { // value exceeds the USART TO range
usart->US_RTOR = 0xffff; // use the MAX
} else {
bool half_time_just_reached = false;
if (ci->wt.remaining <= ci->wt.total / 2 && !ci->wt.half_time_notified) {
ci->wt.half_time_notified = true;
/* don't immediately call card_emu_wtime_half_expired(), as that
* in turn may calls card_emu_uart_update_wt() which will change
* the timeout but would be overridden 4 lines below */
half_time_just_reached = true;
}
/* update the counter no matter if we reached half time or not */
usart->US_RTOR = compute_next_timeout(ci);
/* restart the counter (if wt is 0, the timeout is not started) */
usart->US_CR |= US_CR_RETTO;
if (half_time_just_reached)
card_emu_wtime_half_expired(ci->ch);
usart->US_RTOR = ci->wt_remaining;
}
usart->US_CR |= US_CR_STTTO; // clear timeout flag (and stop timeout until next character is received)
usart->US_CR |= US_CR_RETTO; // restart the counter (it wt is 0, the timeout is not started)
}
#endif
}
/*! ISR called for USART0 */
@@ -336,39 +294,89 @@ int card_emu_uart_update_fidi(uint8_t uart_chan, unsigned int fidi)
return 0;
}
/*! Update WT on USART peripheral. Will automatically re-start timer with new value.
* \param[in] usart USART peripheral to configure
* \param[in] wt inactivity Waiting Time before card_emu_wtime_expired is called (0 to disable) */
void card_emu_uart_update_wt(uint8_t uart_chan, uint32_t wt)
// call-back from card_emu.c to change UART baud rate
void card_emu_uart_update_fd(uint8_t uart_chan, uint16_t f, uint8_t d)
{
OSMO_ASSERT(uart_chan < ARRAY_SIZE(cardem_inst));
struct cardem_inst *ci = &cardem_inst[uart_chan];
Usart *usart = get_usart_by_chan(uart_chan);
if (ci->wt.total != wt) {
TRACE_DEBUG("%u: USART WT changed from %lu to %lu ETU\r\n", uart_chan,
ci->wt.total, wt);
Usart *usart = get_usart_by_chan(uart_chan); // get the USART based on the card handle
if (NULL == usart) {
TRACE_ERROR("%u: USART not found by chan\r\n", uart_chan);
return;
}
if (!iso7816_3_valid_f(f)) {
TRACE_ERROR("%u: invalid F: %u\r\n", uart_chan, f);
return;
}
if (!iso7816_3_valid_d(d)) {
TRACE_ERROR("%u: invalid D: %u\r\n", uart_chan, d);
return;
}
ci->wt.total = wt;
/* reset and start the timer */
card_emu_uart_reset_wt(uart_chan);
uint16_t ratio = f / d;
if (ratio > 0 && ratio < 2048) {
/* make sure USART uses new F/D ratio */
usart->US_CR |= US_CR_RXDIS | US_CR_RSTRX; // disable USART before changing baud rate
usart->US_FIDI = (ratio & 0x7ff); // change baud rate (ratio)
usart->US_CR |= US_CR_RXEN | US_CR_STTTO; // re-enable USART (and stop timeout)
TRACE_INFO("%u: USART F/D set to %u/%u\r\n", uart_chan, f, d);
} else {
TRACE_ERROR("%u: USART could not set F/D to %u/%u\r\n", uart_chan, f, d);
// TODO become unresponsive
}
}
void card_emu_uart_update_wt(uint8_t uart_chan, uint32_t wt)
{
if (uart_chan >= ARRAY_SIZE(cardem_inst)) {
TRACE_ERROR("%u: UART channel out of bounds\r\n", uart_chan);
return;
}
struct cardem_inst *ci = &cardem_inst[uart_chan];
Usart *usart = get_usart_by_chan(uart_chan); // get the USART based on the card handle
if (NULL == usart) {
TRACE_ERROR("%u: USART not found by chan\r\n", uart_chan);
return;
}
ci->wt = wt; // save value
card_emu_uart_reset_wt(uart_chan); // reset and start timer
TRACE_INFO("%u: USART WT set to %lu ETU\r\n", uart_chan, wt);
}
/*! Reset and re-start waiting timeout count down on USART peripheral.
* \param[in] usart USART peripheral to configure */
void card_emu_uart_reset_wt(uint8_t uart_chan)
{
OSMO_ASSERT(uart_chan < ARRAY_SIZE(cardem_inst));
if (uart_chan >= ARRAY_SIZE(cardem_inst)) {
TRACE_ERROR("%u: UART channel out of bounds\r\n", uart_chan);
return;
}
struct cardem_inst *ci = &cardem_inst[uart_chan];
Usart *usart = get_usart_by_chan(uart_chan);
Usart *usart = get_usart_by_chan(uart_chan); // get the USART based on the card handle
if (NULL == usart) {
TRACE_ERROR("%u: USART not found by chan\r\n", uart_chan);
return;
}
/* FIXME: guard against race with interrupt handler */
ci->wt.remaining = ci->wt.total;
ci->wt.half_time_notified = false;
usart->US_RTOR = compute_next_timeout(ci);
/* restart the counter (if wt is 0, the timeout is not started) */
usart->US_CR |= US_CR_RETTO;
ci->wt_remaining = ci->wt; // reload WT value
ci->wt_halfed = false; // reset half expired
if (ci->wt_remaining > 0xffff) { // value exceeds the USART TO range
usart->US_RTOR = 0xffff; // use the MAX
} else {
usart->US_RTOR = ci->wt_remaining;
}
usart->US_CR |= US_CR_RETTO; // restart the counter (if wt is 0, the timeout is not started)
}
void card_emu_uart_io_set(uint8_t uart_chan, bool set)
{
if (uart_chan >= ARRAY_SIZE(cardem_inst)) {
TRACE_ERROR("%u: UART channel out of bounds\r\n", uart_chan);
return;
}
struct cardem_inst *ci = &cardem_inst[uart_chan];
if (set) {
PIO_Set(&ci->pin_io);
} else {
PIO_Clear(&ci->pin_io);
}
}
/* call-back from card_emu.c to force a USART interrupt */
@@ -538,12 +546,20 @@ void mode_cardemu_init(void)
TRACE_ENTRY();
#ifdef PINS_PWR_CARDEMU
// enable power on required peripherals, else disable
Pin pins_pwr_cardemu[] = { PINS_PWR_CARDEMU };
PIO_Configure(pins_pwr_cardemu, PIO_LISTSIZE(pins_pwr_cardemu));
#endif /* PINS_PWR_CARDEMU */
#ifdef PINS_CARDSIM
PIO_Configure(pins_cardsim, PIO_LISTSIZE(pins_cardsim));
#endif
// ADC channel 6 and 7 are used to measure VCC (else they are grounded)
ADC->ADC_CHER |= ADC_CHER_CH6 | ADC_CHER_CH7; // enable the ADC channels to put them in high impedance (else they leak current)
#ifdef DETECT_VCC_BY_ADC
card_vcc_adc_init();
card_vcc_adc_init(); // configure the ADC to measure VCC
#endif /* DETECT_VCC_BY_ADC */
// TODO pull SIMtrace board SIM lines low, else they can leak current back to VCC
INIT_LLIST_HEAD(&cardem_inst[0].usb_out_queue);
rbuf_reset(&cardem_inst[0].rb);
@@ -551,7 +567,20 @@ void mode_cardemu_init(void)
/* configure USART as ISO-7816 slave (e.g. card) */
ISO7816_Init(&cardem_inst[0].usart_info, CLK_SLAVE);
#ifdef BOARD_simtrace
/* simtrace board uses uart timeouts */
/* don't use receive timeout timer for now */
cardem_inst[0].usart_info.base->US_RTOR = 0;
/* enable interrupts to indicate when data has been received or timeout occurred */
USART_EnableIt(cardem_inst[0].usart_info.base, US_IER_RXRDY | US_IER_TIMEOUT);
#else
/* enable interrupts to indicate when data has been received */
USART_EnableIt(cardem_inst[0].usart_info.base, US_IER_RXRDY );
#endif
/* enable interrupt requests for the USART peripheral */
NVIC_EnableIRQ(USART1_IRQn);
PIO_ConfigureIt(&pin_usim1_rst, usim1_rst_irqhandler);
PIO_EnableIt(&pin_usim1_rst);
@@ -567,17 +596,21 @@ void mode_cardemu_init(void)
do {} while (!adc_triggered); /* wait for first ADC reading */
#endif /* DETECT_VCC_BY_ADC */
cardem_inst[0].ch = card_emu_init(0, 0, SIMTRACE_CARDEM_USB_EP_USIM1_DATAIN,
cardem_inst[0].ch = card_emu_init(0, 2, 0, SIMTRACE_CARDEM_USB_EP_USIM1_DATAIN,
SIMTRACE_CARDEM_USB_EP_USIM1_INT, cardem_inst[0].vcc_active,
cardem_inst[0].rst_active, cardem_inst[0].vcc_active);
sim_switch_use_physical(0, 1);
#ifndef DETECT_VCC_BY_ADC
usim1_vcc_irqhandler(NULL); // check VCC/CLK state
#endif
usim1_rst_irqhandler(NULL); // force RST state
#ifdef CARDEMU_SECOND_UART
INIT_LLIST_HEAD(&cardem_inst[1].usb_out_queue);
rbuf_reset(&cardem_inst[1].rb);
PIO_Configure(pins_usim2, PIO_LISTSIZE(pins_usim2));
ISO7816_Init(&cardem_inst[1].usart_info, CLK_SLAVE);
/* TODO enable timeout */
// TODO enable timeout
NVIC_EnableIRQ(USART0_IRQn);
PIO_ConfigureIt(&pin_usim2_rst, usim2_rst_irqhandler);
PIO_EnableIt(&pin_usim2_rst);
@@ -590,11 +623,11 @@ void mode_cardemu_init(void)
do {} while (!adc_triggered); /* wait for first ADC reading */
#endif /* DETECT_VCC_BY_ADC */
cardem_inst[1].ch = card_emu_init(1, 1, SIMTRACE_CARDEM_USB_EP_USIM2_DATAIN,
cardem_inst[1].ch = card_emu_init(1, 0, 1, SIMTRACE_CARDEM_USB_EP_USIM2_DATAIN,
SIMTRACE_CARDEM_USB_EP_USIM2_INT, cardem_inst[1].vcc_active,
cardem_inst[1].rst_active, cardem_inst[1].vcc_active);
sim_switch_use_physical(1, 1);
/* TODO check RST and VCC */
// TODO check rst and vcc
#endif /* CARDEMU_SECOND_UART */
}

View File

@@ -125,7 +125,7 @@ void update_fidi(Usart_info *usart, uint8_t fidi)
uint8_t fi = fidi >> 4;
uint8_t di = fidi & 0xf;
int ratio = iso7816_3_compute_fd_ratio(fi, di);
int ratio = compute_fidi_ratio(fi, di);
if (ratio > 0 && ratio < 0x8000) {
/* make sure USART uses new F/D ratio */

View File

@@ -658,10 +658,9 @@ static void process_byte_pps(uint8_t byte)
fn = 1;
dn = 1;
}
TRACE_INFO("PPS negotiation successful: Fn=%u Dn=%u\n\r",
iso7816_3_fi_table[fn], iso7816_3_di_table[dn]);
TRACE_INFO("PPS negotiation successful: Fn=%u Dn=%u\n\r", fi_table[fn], di_table[dn]);
update_fidi(&sniff_usart, pps_cur[2]);
update_wt(0, iso7816_3_di_table[dn]);
update_wt(0, di_table[dn]);
usb_send_fidi(pps_cur[2]); /* send Fi/Di change notification to host software over USB */
} else { /* checksum is invalid */
TRACE_INFO("PPS negotiation failed\n\r");

View File

@@ -1,14 +0,0 @@
#include <stdint.h>
#include <osmocom/core/panic.h>
/* This is what's minimally required to fix builds on Ubuntu 20.04,
* where stack smashing protection is enabled by default when using dpkg
* - even when cross-compiling: https://osmocom.org/issues/4687
*/
uintptr_t __stack_chk_guard = 0xdeadbeef;
void __stack_chk_fail(void)
{
osmo_panic("Stack smashing detected!\r\n");
}

View File

@@ -23,6 +23,9 @@
#include "chip.h"
void card_emu_wt_halfed(void *handle);
void card_emu_wt_expired(void *handle);
/* pins for Channel 0 of TC-block 0, we only use TCLK + TIOB */
#define PIN_TCLK0 {PIO_PA4, PIOA, ID_PIOA, PIO_PERIPH_B, PIO_DEFAULT }
#define PIN_TIOA0 {PIO_PA0, PIOA, ID_PIOA, PIO_PERIPH_B, PIO_DEFAULT}
@@ -85,7 +88,7 @@ static void tc_etu_irq(struct tc_etu_state *te)
te->nr_events++;
if (te->nr_events == te->wait_events/2) {
/* Indicate that half the waiting tim has expired */
tc_etu_wtime_half_expired(te->handle);
card_emu_wt_halfed(te->handle);
}
if (te->nr_events >= te->wait_events) {
TcChannel *chan = te->chan;
@@ -96,7 +99,7 @@ static void tc_etu_irq(struct tc_etu_state *te)
chan->TC_CCR = TC_CCR_CLKEN;
/* Indicate that the waiting tim has expired */
tc_etu_wtime_expired(te->handle);
card_emu_wt_expired(te->handle);
}
}
}

View File

@@ -36,7 +36,7 @@ const char *get_value_string_or_null(const struct value_string *vs,
int get_string_value(const struct value_string *vs, const char *str);
char osmo_bcd2char(uint8_t bcd);
/* only works for numbers in ascci */
/* only works for numbers in ASCII */
uint8_t osmo_char2bcd(char c);
int osmo_hexparse(const char *str, uint8_t *b, int max_len);
@@ -60,7 +60,7 @@ do { \
rem -= ret; \
} while (0)
/*! Helper macro to terminate when an assertion failes
/*! Helper macro to terminate when an assertion fails
* \param[in] exp Predicate to verify
* This function will generate a backtrace and terminate the program if
* the predicate evaluates to false (0).
@@ -75,7 +75,7 @@ do { \
/*! duplicate a string using talloc and release its prior content (if any)
* \param[in] ctx Talloc context to use for allocation
* \param[out] dst pointer to string, will be updated with ptr to new string
* \param[in] newstr String that will be copieed to newly allocated string */
* \param[in] newstr String that will be copied to newly allocated string */
static inline void osmo_talloc_replace_string(void *ctx, char **dst, const char *newstr)
{
if (*dst)

View File

@@ -46,7 +46,7 @@ static osmo_panic_handler_t osmo_panic_handler = (void*)0;
__attribute__ ((format (printf, 1, 0)))
static void osmo_panic_default(const char *fmt, va_list args)
{
vfprintf_sync(stderr, fmt, args);
vfprintf(stderr, fmt, args);
osmo_generate_backtrace();
assert(0);
}

3
host/.gitignore vendored
View File

@@ -34,4 +34,5 @@ libtool
simtrace2-list
simtrace2-sniff
simtrace2-cardem-pcsc
simtrace2-remsim
simtrace2-remsim-usb2udp

View File

@@ -3,7 +3,7 @@ AUTOMAKE_OPTIONS = foreign dist-bzip2 1.6
AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include
SUBDIRS = include lib src contrib #tests examples doc
EXTRA_DIST = .version
EXTRA_DIST = .version git-version-gen
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = libosmo-simtrace2.pc

28
host/Makefile.old Normal file
View File

@@ -0,0 +1,28 @@
LDFLAGS+=`pkg-config --libs libusb-1.0 libosmocore` -pthread
CFLAGS=-Wall -g
APPS=simtrace2-remsim simtrace2-remsim-usb2udp simtrace2-list simtrace2-sniff
all: $(APPS)
simtrace2-remsim: simtrace2-remsim.o apdu_dispatch.o simtrace2-discovery.o simtrace2_api.o libusb_util.o
$(CC) -o $@ $^ $(LDFLAGS) `pkg-config --libs libosmosim libpcsclite`
simtrace2-remsim-usb2udp: usb2udp.o simtrace2-discovery.o
$(CC) -o $@ $^ $(LDFLAGS)
simtrace2-list: simtrace2_usb.o libusb_util.o
$(CC) -o $@ $^ $(LDFLAGS)
simtrace2-sniff: simtrace2-sniff.o simtrace2-discovery.o libusb_util.o
$(CC) -o $@ $^ $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) `pkg-config --cflags libusb-1.0 libosmocore` -o $@ -c $^
clean:
@rm -f *.o $(APPS)
install: $(APPS)
mkdir -p $(DESTDIR)/usr/bin
cp $(APPS) $(DESTDIR)/usr/bin/

View File

@@ -1,5 +1,5 @@
AC_INIT([simtrace2],
m4_esyscmd([../git-version-gen ../.tarball-version]),
m4_esyscmd([./git-version-gen .tarball-version]),
[simtrace@lists.osmocom.org])
dnl *This* is the root dir, even if an install-sh exists in ../ or ../../
@@ -100,5 +100,4 @@ AC_OUTPUT(
src/Makefile
lib/Makefile
contrib/Makefile
contrib/simtrace2.spec
Makefile)

View File

@@ -1,99 +0,0 @@
#
# spec file for package simtrace2
#
# Copyright (c) 2018, Martin Hauke <mardnh@gmx.de>
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.
Name: simtrace2
Version: @VERSION@
Release: 0
Summary: Osmocom SIMtrace host utility
License: GPL-2.0-or-later
Group: Productivity/Telephony/Utilities
URL: https://osmocom.org/projects/simtrace2/wiki
Source: %{name}-%{version}.tar.xz
BuildRequires: autoconf
BuildRequires: automake
BuildRequires: libtool
BuildRequires: pkgconfig
BuildRequires: pkgconfig(libosmocore)
BuildRequires: pkgconfig(libosmosim)
BuildRequires: pkgconfig(libpcsclite)
BuildRequires: pkgconfig(libusb-1.0)
BuildRequires: pkgconfig(libosmousb) >= 0.0.0
BuildRequires: pkgconfig(udev)
%description
Osmocom SIMtrace 2 is a software and hardware system for passively
tracing SIM-ME communication between the SIM card and the mobile phone,
and remote SIM operation.
This package contains SIMtrace 2 host utility.
%package -n libosmo-simtrace2-0
Summary: Shared Library part of libosmo-simtrace2
Group: System/Libraries
%description -n libosmo-simtrace2-0
This library contains core "driver" functionality to interface with the
Osmocom SIMtrace2 (and compatible) USB device firmware. It enables
applications to implement SIM card / smart card tracing as well as
SIM / smart card emulation functions.
%package -n libosmo-simtrace2-devel
Summary: Development files for the Osmocom SIMtrace2 library
Group: Development/Libraries/C and C++
Requires: libosmo-simtrace2-0 = %{version}
%description -n libosmo-simtrace2-devel
Osmocom SIMtrace2 (and compatible) USB device firmware. It enables
applications to implement SIM card / smart card tracing as well as
SIM / smart card emulation functions.
This subpackage contains libraries and header files for developing
applications that want to make use of libosmo-simtrace2.
%prep
%setup -q
%build
cd host
echo "%{version}" >.tarball-version
autoreconf -fiv
%configure --disable-static
make %{?_smp_mflags}
%install
%make_install -C host
install -Dm0644 host/contrib/99-simtrace2.rules %{buildroot}/%{_udevrulesdir}/99-simtrace2.rules
find %{buildroot} -type f -name "*.la" -delete -print
%post -n libosmo-simtrace2-0 -p /sbin/ldconfig
%postun -n libosmo-simtrace2-0 -p /sbin/ldconfig
%files
%doc README.md
%{_bindir}/simtrace2-cardem-pcsc
%{_bindir}/simtrace2-list
%{_bindir}/simtrace2-sniff
%{_udevrulesdir}/99-simtrace2.rules
%files -n libosmo-simtrace2-0
%{_libdir}/libosmo-simtrace2.so.0*
%files -n libosmo-simtrace2-devel
%dir %{_includedir}/osmocom/
%dir %{_includedir}/osmocom/simtrace2/
%{_includedir}/osmocom/simtrace2/*.h
%{_libdir}/libosmo-simtrace2.so
%{_libdir}/pkgconfig/libosmo-simtrace2.pc
%changelog

151
host/git-version-gen Executable file
View File

@@ -0,0 +1,151 @@
#!/bin/sh
# Print a version string.
scriptversion=2010-01-28.01
# Copyright (C) 2007-2010 Free Software Foundation, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This script is derived from GIT-VERSION-GEN from GIT: http://git.or.cz/.
# It may be run two ways:
# - from a git repository in which the "git describe" command below
# produces useful output (thus requiring at least one signed tag)
# - from a non-git-repo directory containing a .tarball-version file, which
# presumes this script is invoked like "./git-version-gen .tarball-version".
# In order to use intra-version strings in your project, you will need two
# separate generated version string files:
#
# .tarball-version - present only in a distribution tarball, and not in
# a checked-out repository. Created with contents that were learned at
# the last time autoconf was run, and used by git-version-gen. Must not
# be present in either $(srcdir) or $(builddir) for git-version-gen to
# give accurate answers during normal development with a checked out tree,
# but must be present in a tarball when there is no version control system.
# Therefore, it cannot be used in any dependencies. GNUmakefile has
# hooks to force a reconfigure at distribution time to get the value
# correct, without penalizing normal development with extra reconfigures.
#
# .version - present in a checked-out repository and in a distribution
# tarball. Usable in dependencies, particularly for files that don't
# want to depend on config.h but do want to track version changes.
# Delete this file prior to any autoconf run where you want to rebuild
# files to pick up a version string change; and leave it stale to
# minimize rebuild time after unrelated changes to configure sources.
#
# It is probably wise to add these two files to .gitignore, so that you
# don't accidentally commit either generated file.
#
# Use the following line in your configure.ac, so that $(VERSION) will
# automatically be up-to-date each time configure is run (and note that
# since configure.ac no longer includes a version string, Makefile rules
# should not depend on configure.ac for version updates).
#
# AC_INIT([GNU project],
# m4_esyscmd([build-aux/git-version-gen .tarball-version]),
# [bug-project@example])
#
# Then use the following lines in your Makefile.am, so that .version
# will be present for dependencies, and so that .tarball-version will
# exist in distribution tarballs.
#
# BUILT_SOURCES = $(top_srcdir)/.version
# $(top_srcdir)/.version:
# echo $(VERSION) > $@-t && mv $@-t $@
# dist-hook:
# echo $(VERSION) > $(distdir)/.tarball-version
case $# in
1) ;;
*) echo 1>&2 "Usage: $0 \$srcdir/.tarball-version"; exit 1;;
esac
tarball_version_file=$1
nl='
'
# First see if there is a tarball-only version file.
# then try "git describe", then default.
if test -f $tarball_version_file
then
v=`cat $tarball_version_file` || exit 1
case $v in
*$nl*) v= ;; # reject multi-line output
[0-9]*) ;;
*) v= ;;
esac
test -z "$v" \
&& echo "$0: WARNING: $tarball_version_file seems to be damaged" 1>&2
fi
if test -n "$v"
then
: # use $v
elif
v=`git describe --abbrev=4 --match='v*' HEAD 2>/dev/null \
|| git describe --abbrev=4 HEAD 2>/dev/null` \
&& case $v in
[0-9]*) ;;
v[0-9]*) ;;
*) (exit 1) ;;
esac
then
# Is this a new git that lists number of commits since the last
# tag or the previous older version that did not?
# Newer: v6.10-77-g0f8faeb
# Older: v6.10-g0f8faeb
case $v in
*-*-*) : git describe is okay three part flavor ;;
*-*)
: git describe is older two part flavor
# Recreate the number of commits and rewrite such that the
# result is the same as if we were using the newer version
# of git describe.
vtag=`echo "$v" | sed 's/-.*//'`
numcommits=`git rev-list "$vtag"..HEAD | wc -l`
v=`echo "$v" | sed "s/\(.*\)-\(.*\)/\1-$numcommits-\2/"`;
;;
esac
# Change the first '-' to a '.', so version-comparing tools work properly.
# Remove the "g" in git describe's output string, to save a byte.
v=`echo "$v" | sed 's/-/./;s/\(.*\)-g/\1-/'`;
else
v=UNKNOWN
fi
v=`echo "$v" |sed 's/^v//'`
# Don't declare a version "dirty" merely because a time stamp has changed.
git status > /dev/null 2>&1
dirty=`sh -c 'git diff-index --name-only HEAD' 2>/dev/null` || dirty=
case "$dirty" in
'') ;;
*) # Append the suffix only if there isn't one already.
case $v in
*-dirty) ;;
*) v="$v-dirty" ;;
esac ;;
esac
# Omit the trailing newline, so that m4_esyscmd can use the result directly.
echo "$v" | tr -d '\012'
# Local variables:
# eval: (add-hook 'write-file-hooks 'time-stamp)
# time-stamp-start: "scriptversion="
# time-stamp-format: "%:y-%02m-%02d.%02H"
# time-stamp-end: "$"
# End:

View File

@@ -12,8 +12,6 @@ struct osmo_st2_transport {
uint8_t out;
uint8_t irq_in;
} usb_ep;
/* use non-blocking / asynchronous libusb I/O */
bool usb_async;
/* UDP */
int udp_fd;
@@ -41,6 +39,8 @@ struct osmo_st2_cardem_inst {
void *priv;
};
int osmo_st2_transp_tx_msg(struct osmo_st2_transport *transp, struct msgb *msg);
int osmo_st2_slot_tx_msg(struct osmo_st2_slot *slot, struct msgb *msg,
uint8_t msg_class, uint8_t msg_type);

View File

@@ -57,60 +57,23 @@ static struct msgb *st_msgb_alloc(void)
return msgb_alloc_headroom(1024+32, 32, "SIMtrace");
}
static void usb_out_xfer_cb(struct libusb_transfer *xfer)
/*! \brief Transmit a given command to the SIMtrace2 device */
int osmo_st2_transp_tx_msg(struct osmo_st2_transport *transp, struct msgb *msg)
{
struct msgb *msg = xfer->user_data;
int rc;
switch (xfer->status) {
case LIBUSB_TRANSFER_COMPLETED:
break;
case LIBUSB_TRANSFER_NO_DEVICE:
fprintf(stderr, "USB device disappeared\n");
exit(1);
break;
default:
fprintf(stderr, "USB OUT transfer failed, status=%u\n", xfer->status);
exit(1);
break;
printf("<- %s\n", msgb_hexdump(msg));
if (transp->udp_fd < 0) {
int xfer_len;
rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.out,
msgb_data(msg), msgb_length(msg),
&xfer_len, 100000);
} else {
rc = write(transp->udp_fd, msgb_data(msg), msgb_length(msg));
}
msgb_free(msg);
libusb_free_transfer(xfer);
}
static int st2_transp_tx_msg_usb_async(struct osmo_st2_transport *transp, struct msgb *msg)
{
struct libusb_transfer *xfer;
int rc;
xfer = libusb_alloc_transfer(0);
OSMO_ASSERT(xfer);
xfer->dev_handle = transp->usb_devh;
xfer->flags = 0;
xfer->type = LIBUSB_TRANSFER_TYPE_BULK;
xfer->endpoint = transp->usb_ep.out;
xfer->timeout = 100000;
xfer->user_data = msg;
xfer->length = msgb_length(msg);
xfer->buffer = msgb_data(msg);
xfer->callback = usb_out_xfer_cb;
rc = libusb_submit_transfer(xfer);
OSMO_ASSERT(rc == 0);
return rc;
}
/*! \brief Transmit a given command to the SIMtrace2 device */
static int st2_transp_tx_msg_usb_sync(struct osmo_st2_transport *transp, struct msgb *msg)
{
int rc;
int xfer_len;
rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.out,
msgb_data(msg), msgb_length(msg),
&xfer_len, 100000);
msgb_free(msg);
return rc;
}
@@ -135,24 +98,9 @@ static struct simtrace_msg_hdr *st_push_hdr(struct msgb *msg, uint8_t msg_class,
int osmo_st2_slot_tx_msg(struct osmo_st2_slot *slot, struct msgb *msg,
uint8_t msg_class, uint8_t msg_type)
{
struct osmo_st2_transport *transp = slot->transp;
int rc;
OSMO_ASSERT(transp);
st_push_hdr(msg, msg_class, msg_type, slot->slot_nr);
printf("SIMtrace <- %s\n", msgb_hexdump(msg));
if (transp->udp_fd < 0) {
if (transp->usb_async)
rc = st2_transp_tx_msg_usb_async(transp, msg);
else
rc = st2_transp_tx_msg_usb_sync(transp, msg);
} else {
rc = write(transp->udp_fd, msgb_data(msg), msgb_length(msg));
msgb_free(msg);
}
return rc;
return osmo_st2_transp_tx_msg(slot->transp, msg);
}
/***********************************************************************

View File

@@ -5,9 +5,11 @@ AM_LDFLAGS=$(COVERAGE_LDFLAGS)
LDADD= $(top_builddir)/lib/libosmo-simtrace2.la \
$(LIBOSMOCORE_LIBS) $(LIBOSMOSIM_LIBS) $(LIBOSMOUSB_LIBS) $(LIBUSB_LIBS)
bin_PROGRAMS = simtrace2-cardem-pcsc simtrace2-list simtrace2-sniff
bin_PROGRAMS = simtrace2-remsim simtrace2-remsim-usb2udp simtrace2-list simtrace2-sniff
simtrace2_cardem_pcsc_SOURCES = simtrace2-cardem-pcsc.c
simtrace2_remsim_SOURCES = simtrace2-remsim.c
simtrace2_remsim_usb2udp_SOURCES = usb2udp.c
simtrace2_list_SOURCES = simtrace2_usb.c

View File

@@ -1,7 +1,7 @@
/* simtrace2-cardem-pcsc - main program for the host PC to provide a remote SIM
/* simtrace2-remsim - main program for the host PC to provide a remote SIM
* using the SIMtrace 2 firmware in card emulation mode
*
* (C) 2016-2020 by Harald Welte <hwelte@hmw-consulting.de>
* (C) 2016-2017 by Harald Welte <hwelte@hmw-consulting.de>
* (C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
*
* This program is free software; you can redistribute it and/or
@@ -47,31 +47,27 @@
#include <osmocom/core/utils.h>
#include <osmocom/core/socket.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/select.h>
#include <osmocom/sim/class_tables.h>
#include <osmocom/sim/sim.h>
#define ATR_MAX_LEN 33
/*
reasonable ATR offering all protocols and voltages
smartphones might not care, but other readers do
#define LOGCI(ci, lvl, fmt, args ...) printf(fmt, ## args)
/* reasonable ATR offering all protocols and voltages
* smartphones might not care, but other readers do
*
* TS = 0x3B Direct Convention
* T0 = 0x80 Y(1): b1000, K: 0 (historical bytes)
* TD(1) = 0x80 Y(i+1) = b1000, Protocol T=0
* ----
* TD(2) = 0x81 Y(i+1) = b1000, Protocol T=1
* ----
* TD(3) = 0x1F Y(i+1) = b0001, Protocol T=15
* ----
* TA(4) = 0xC7 Clock stop: no preference - Class accepted by the card: (3G) A 5V B 3V C 1.8V
* ----
* Historical bytes
* TCK = 0x59 correct checksum
*/
#define DEFAULT_ATR_STR "3B8080811FC759"
TS = 0x3B Direct Convention
T0 = 0x80 Y(1): b1000, K: 0 (historical bytes)
TD(1) = 0x80 Y(i+1) = b1000, Protocol T=0
----
TD(2) = 0x81 Y(i+1) = b1000, Protocol T=1
----
TD(3) = 0x1F Y(i+1) = b0001, Protocol T=15
----
TA(4) = 0xC7 Clock stop: no preference - Class accepted by the card: (3G) A 5V B 3V C 1.8V
----
Historical bytes
TCK = 0x59 correct checksum
*/
static uint8_t real_atr[] = { 0x3B, 0x80, 0x80, 0x81 , 0x1F, 0xC7, 0x59};
static void atr_update_csum(uint8_t *atr, unsigned int atr_len)
@@ -95,9 +91,9 @@ static int process_do_status(struct osmo_st2_cardem_inst *ci, uint8_t *buf, int
struct cardemu_usb_msg_status *status;
status = (struct cardemu_usb_msg_status *) buf;
printf("=> STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
status->flags, status->fi, status->di, status->wi,
status->waiting_time);
printf("=> STATUS: flags=0x%x, F=%u, D=%u, WI=%u WT=%u\n",
status->flags, status->f, status->d, status->wi,
status->wt);
return 0;
}
@@ -182,9 +178,6 @@ static int process_usb_msg(struct osmo_st2_cardem_inst *ci, uint8_t *buf, int le
case SIMTRACE_MSGT_DO_CEMU_RX_DATA:
rc = process_do_rx_da(ci, buf, len);
break;
case SIMTRACE_MSGT_BD_CEMU_CONFIG:
/* firmware confirms configuration change; ignore */
break;
default:
printf("unknown simtrace msg type 0x%02x\n", sh->msg_type);
rc = -1;
@@ -194,157 +187,20 @@ static int process_usb_msg(struct osmo_st2_cardem_inst *ci, uint8_t *buf, int le
return rc;
}
/*! \brief Process a STATUS message on IRQ endpoint from the SIMtrace2 */
static int process_irq_status(struct osmo_st2_cardem_inst *ci, const uint8_t *buf, int len)
{
const struct cardemu_usb_msg_status *status = (struct cardemu_usb_msg_status *) buf;
LOGCI(ci, LOGL_INFO, "SIMtrace IRQ STATUS: flags=0x%x, fi=%u, di=%u, wi=%u wtime=%u\n",
status->flags, status->fi, status->di, status->wi,
status->waiting_time);
return 0;
}
static int process_usb_msg_irq(struct osmo_st2_cardem_inst *ci, const uint8_t *buf, unsigned int len)
{
struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *)buf;
int rc;
LOGCI(ci, LOGL_INFO, "SIMtrace IRQ %s\n", osmo_hexdump(buf, len));
buf += sizeof(*sh);
switch (sh->msg_type) {
case SIMTRACE_MSGT_BD_CEMU_STATUS:
rc = process_irq_status(ci, buf, len);
break;
default:
LOGCI(ci, LOGL_ERROR, "unknown simtrace msg type 0x%02x\n", sh->msg_type);
rc = -1;
break;
}
return rc;
}
static void usb_in_xfer_cb(struct libusb_transfer *xfer)
{
struct osmo_st2_cardem_inst *ci = xfer->user_data;
int rc;
switch (xfer->status) {
case LIBUSB_TRANSFER_COMPLETED:
/* hand the message up the stack */
process_usb_msg(ci, xfer->buffer, xfer->actual_length);
break;
case LIBUSB_TRANSFER_NO_DEVICE:
LOGCI(ci, LOGL_FATAL, "USB device disappeared\n");
exit(1);
break;
default:
LOGCI(ci, LOGL_FATAL, "USB IN transfer failed, status=%u\n", xfer->status);
exit(1);
break;
}
/* re-submit the IN transfer */
rc = libusb_submit_transfer(xfer);
OSMO_ASSERT(rc == 0);
}
static void allocate_and_submit_in(struct osmo_st2_cardem_inst *ci)
{
struct osmo_st2_transport *transp = ci->slot->transp;
struct libusb_transfer *xfer;
int rc;
xfer = libusb_alloc_transfer(0);
OSMO_ASSERT(xfer);
xfer->dev_handle = transp->usb_devh;
xfer->flags = 0;
xfer->type = LIBUSB_TRANSFER_TYPE_BULK;
xfer->endpoint = transp->usb_ep.in;
xfer->timeout = 0;
xfer->user_data = ci;
xfer->length = 16*256;
xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length);
OSMO_ASSERT(xfer->buffer);
xfer->callback = usb_in_xfer_cb;
/* submit the IN transfer */
rc = libusb_submit_transfer(xfer);
OSMO_ASSERT(rc == 0);
}
static void usb_irq_xfer_cb(struct libusb_transfer *xfer)
{
struct osmo_st2_cardem_inst *ci = xfer->user_data;
int rc;
switch (xfer->status) {
case LIBUSB_TRANSFER_COMPLETED:
process_usb_msg_irq(ci, xfer->buffer, xfer->actual_length);
break;
case LIBUSB_TRANSFER_NO_DEVICE:
LOGCI(ci, LOGL_FATAL, "USB device disappeared\n");
exit(1);
break;
default:
LOGCI(ci, LOGL_FATAL, "USB IN transfer failed, status=%u\n", xfer->status);
exit(1);
break;
}
/* re-submit the IN transfer */
rc = libusb_submit_transfer(xfer);
OSMO_ASSERT(rc == 0);
}
static void allocate_and_submit_irq(struct osmo_st2_cardem_inst *ci)
{
struct osmo_st2_transport *transp = ci->slot->transp;
struct libusb_transfer *xfer;
int rc;
xfer = libusb_alloc_transfer(0);
OSMO_ASSERT(xfer);
xfer->dev_handle = transp->usb_devh;
xfer->flags = 0;
xfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
xfer->endpoint = transp->usb_ep.irq_in;
xfer->timeout = 0;
xfer->user_data = ci;
xfer->length = 64;
xfer->buffer = libusb_dev_mem_alloc(xfer->dev_handle, xfer->length);
OSMO_ASSERT(xfer->buffer);
xfer->callback = usb_irq_xfer_cb;
/* submit the IN transfer */
rc = libusb_submit_transfer(xfer);
OSMO_ASSERT(rc == 0);
}
static void print_welcome(void)
{
printf("simtrace2-cardem-pcsc - Using PC/SC reader as SIM\n"
"(C) 2010-2020, Harald Welte <laforge@gnumonks.org>\n"
printf("simtrace2-remsim - Remote SIM card forwarding\n"
"(C) 2010-2017, Harald Welte <laforge@gnumonks.org>\n"
"(C) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>\n\n");
}
static void print_help(void)
{
printf( "\t-h\t--help\n"
printf( "\t-r\t--remote-udp-host HOST\n"
"\t-p\t--remote-udp-port PORT\n"
"\t-h\t--help\n"
"\t-i\t--gsmtap-ip\tA.B.C.D\n"
"\t-a\t--skip-atr\n"
"\t-t\t--set-atr\tATR-STRING in HEX\n"
"\t-k\t--keep-running\n"
"\t-n\t--pcsc-reader-num\n"
"\t-V\t--usb-vendor\tVENDOR_ID\n"
@@ -359,9 +215,10 @@ static void print_help(void)
}
static const struct option opts[] = {
{ "remote-udp-host", 1, 0, 'r' },
{ "remote-udp-port", 1, 0, 'p' },
{ "gsmtap-ip", 1, 0, 'i' },
{ "skip-atr", 0, 0, 'a' },
{ "set-atr", 1, 0, 't' },
{ "help", 0, 0, 'h' },
{ "keep-running", 0, 0, 'k' },
{ "pcsc-reader-num", 1, 0, 'n' },
@@ -377,9 +234,40 @@ static const struct option opts[] = {
static void run_mainloop(struct osmo_st2_cardem_inst *ci)
{
struct osmo_st2_transport *transp = ci->slot->transp;
unsigned int msg_count, byte_count = 0;
uint8_t buf[16*265];
int xfer_len;
int rc;
printf("Entering main loop\n");
while (1) {
osmo_select_main(0);
/* read data from SIMtrace2 device (local or via USB) */
if (transp->udp_fd < 0) {
rc = libusb_bulk_transfer(transp->usb_devh, transp->usb_ep.in,
buf, sizeof(buf), &xfer_len, 100);
if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT &&
rc != LIBUSB_ERROR_INTERRUPTED &&
rc != LIBUSB_ERROR_IO) {
fprintf(stderr, "BULK IN transfer error; rc=%d\n", rc);
return;
}
} else {
rc = read(transp->udp_fd, buf, sizeof(buf));
if (rc <= 0) {
fprintf(stderr, "shor read from UDP\n");
return;
}
xfer_len = rc;
}
/* dispatch any incoming data */
if (xfer_len > 0) {
printf("URB: %s\n", osmo_hexdump(buf, xfer_len));
process_usb_msg(ci, buf, xfer_len);
msg_count++;
byte_count += xfer_len;
}
}
}
@@ -415,32 +303,31 @@ int main(int argc, char **argv)
int rc;
int c, ret = 1;
int skip_atr = 0;
char *atr = DEFAULT_ATR_STR;
uint8_t real_atr[ATR_MAX_LEN];
int atr_len;
int keep_running = 0;
int remote_udp_port = 52342;
int if_num = 0, vendor_id = -1, product_id = -1;
int config_id = -1, altsetting = 0, addr = -1;
int reader_num = 0;
char *remote_udp_host = NULL;
char *path = NULL;
struct osim_reader_hdl *reader;
struct osim_card_hdl *card;
print_welcome();
rc = osmo_libusb_init(NULL);
if (rc < 0) {
fprintf(stderr, "libusb initialization failed\n");
return rc;
}
while (1) {
int option_index = 0;
c = getopt_long(argc, argv, "hi:V:P:C:I:S:A:H:akn:t:", opts, &option_index);
c = getopt_long(argc, argv, "r:p:hi:V:P:C:I:S:A:H:akn:", opts, &option_index);
if (c == -1)
break;
switch (c) {
case 'r':
remote_udp_host = optarg;
break;
case 'p':
remote_udp_port = atoi(optarg);
break;
case 'h':
print_help();
exit(0);
@@ -451,9 +338,6 @@ int main(int argc, char **argv)
case 'a':
skip_atr = 1;
break;
case 't':
atr = optarg;
break;
case 'k':
keep_running = 1;
break;
@@ -484,24 +368,29 @@ int main(int argc, char **argv)
}
}
atr_len = osmo_hexparse(atr,real_atr,ATR_MAX_LEN);
if (atr_len < 2) {
fprintf(stderr, "Invalid ATR - please omit a leading 0x and only use valid hex "
"digits and whitespace. ATRs need to be between 2 and 33 bytes long.\n");
goto do_exit;
}
if (vendor_id < 0 || product_id < 0) {
if (!remote_udp_host && (vendor_id < 0 || product_id < 0)) {
fprintf(stderr, "You have to specify the vendor and product ID\n");
goto do_exit;
}
transp->udp_fd = -1;
ci->card_prof = &osim_uicc_sim_cic_profile;
rc = libusb_init(NULL);
if (rc < 0) {
fprintf(stderr, "libusb initialization failed\n");
goto do_exit;
if (!remote_udp_host) {
rc = libusb_init(NULL);
if (rc < 0) {
fprintf(stderr, "libusb initialization failed\n");
goto do_exit;
}
} else {
transp->udp_fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
remote_udp_host, remote_udp_port+if_num,
OSMO_SOCK_F_CONNECT);
if (transp->udp_fd < 0) {
fprintf(stderr, "error binding UDP port\n");
goto do_exit;
}
}
rc = osmo_st2_gsmtap_init(gsmtap_host);
@@ -531,43 +420,36 @@ int main(int argc, char **argv)
signal(SIGINT, &signal_handler);
do {
struct usb_interface_match _ifm, *ifm = &_ifm;
ifm->vendor = vendor_id;
ifm->product = product_id;
ifm->configuration = config_id;
ifm->interface = if_num;
ifm->altsetting = altsetting;
ifm->addr = addr;
if (path)
osmo_strlcpy(ifm->path, path, sizeof(ifm->path));
transp->udp_fd = -1;
transp->usb_async = true;
transp->usb_devh = osmo_libusb_open_claim_interface(NULL, NULL, ifm);
if (!transp->usb_devh) {
fprintf(stderr, "can't open USB device\n");
goto close_exit;
if (transp->udp_fd < 0) {
struct usb_interface_match _ifm, *ifm = &_ifm;
ifm->vendor = vendor_id;
ifm->product = product_id;
ifm->configuration = config_id;
ifm->interface = if_num;
ifm->altsetting = altsetting;
ifm->addr = addr;
if (path)
osmo_strlcpy(ifm->path, path, sizeof(ifm->path));
transp->usb_devh = osmo_libusb_open_claim_interface(NULL, NULL, ifm);
if (!transp->usb_devh) {
fprintf(stderr, "can't open USB device\n");
goto close_exit;
}
rc = libusb_claim_interface(transp->usb_devh, if_num);
if (rc < 0) {
fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc);
goto close_exit;
}
rc = osmo_libusb_get_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
&transp->usb_ep.in, &transp->usb_ep.irq_in);
if (rc < 0) {
fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
goto close_exit;
}
}
rc = libusb_claim_interface(transp->usb_devh, if_num);
if (rc < 0) {
fprintf(stderr, "can't claim interface %d; rc=%d\n", if_num, rc);
goto close_exit;
}
rc = osmo_libusb_get_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
&transp->usb_ep.in, &transp->usb_ep.irq_in);
if (rc < 0) {
fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
goto close_exit;
}
allocate_and_submit_irq(ci);
for (int i = 0; i < 4; i++)
allocate_and_submit_in(ci);
/* request firmware to generate STATUS on IRQ endpoint */
osmo_st2_cardem_request_config(ci, CEMU_FEAT_F_STATUS_IRQ);
/* simulate card-insert to modem (owhw, not qmod) */
osmo_st2_cardem_request_card_insert(ci, true);
@@ -575,9 +457,8 @@ int main(int argc, char **argv)
osmo_st2_modem_sim_select_remote(ci->slot);
if (!skip_atr) {
/* set the ATR */
atr_update_csum(real_atr, atr_len);
osmo_st2_cardem_request_set_atr(ci, real_atr, atr_len);
atr_update_csum(real_atr, sizeof(real_atr));
osmo_st2_cardem_request_set_atr(ci, real_atr, sizeof(real_atr));
}
/* select remote (forwarded) SIM */
@@ -586,7 +467,8 @@ int main(int argc, char **argv)
run_mainloop(ci);
ret = 0;
libusb_release_interface(transp->usb_devh, 0);
if (transp->udp_fd < 0)
libusb_release_interface(transp->usb_devh, 0);
close_exit:
if (transp->usb_devh)
libusb_close(transp->usb_devh);
@@ -594,7 +476,8 @@ close_exit:
sleep(1);
} while (keep_running);
libusb_exit(NULL);
if (transp->udp_fd < 0)
libusb_exit(NULL);
do_exit:
return ret;
}

285
host/src/usb2udp.c Normal file
View File

@@ -0,0 +1,285 @@
/* simtrace - main program for the host PC
*
* (C) 2010-2016 by Harald Welte <hwelte@hmw-consulting.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <time.h>
#define _GNU_SOURCE
#include <getopt.h>
#include <poll.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <libusb.h>
#include <osmocom/usb/libusb.h>
#include <osmocom/simtrace2/simtrace_usb.h>
#include <osmocom/simtrace2/simtrace_prot.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/socket.h>
#include <osmocom/core/select.h>
struct libusb_device_handle *g_devh;
static struct sockaddr_in g_sa_remote;
static struct osmo_fd g_udp_ofd;
static void print_welcome(void)
{
printf("usb2udp - UDP/IP forwarding of SIMtrace card emulation\n"
"(C) 2016 by Harald Welte <laforge@gnumonks.org>\n\n");
}
static void print_help(void)
{
printf( "\t-h\t--help\n"
"\t-i\t--interface <0-255>\n"
"\n"
);
}
struct ep_buf {
uint8_t ep;
uint8_t buf[1024];
struct libusb_transfer *xfer;
};
static struct ep_buf g_buf_in;
static struct ep_buf g_buf_out;
static void usb_in_xfer_cb(struct libusb_transfer *xfer)
{
int rc;
printf("xfer_cb(ep=%02x): status=%d, flags=0x%x, type=%u, len=%u, act_len=%u\n",
xfer->endpoint, xfer->status, xfer->flags, xfer->type, xfer->length, xfer->actual_length);
switch (xfer->status) {
case LIBUSB_TRANSFER_COMPLETED:
if (xfer->endpoint == g_buf_in.ep) {
/* process the data */
printf("read %d bytes from SIMTRACE, forwarding to UDP\n", xfer->actual_length);
rc = sendto(g_udp_ofd.fd, xfer->buffer, xfer->actual_length, 0, (struct sockaddr *)&g_sa_remote, sizeof(g_sa_remote));
if (rc <= 0) {
fprintf(stderr, "error writing to UDP\n");
}
/* and re-submit the URB */
libusb_submit_transfer(xfer);
} else if (xfer->endpoint == g_buf_out.ep) {
/* re-enable reading from the UDP side */
g_udp_ofd.when |= BSC_FD_READ;
}
break;
default:
fprintf(stderr, "xfer_cb(ERROR '%s')\n", osmo_hexdump_nospc(xfer->buffer, xfer->actual_length));
break;
}
}
static void init_ep_buf(struct ep_buf *epb)
{
if (!epb->xfer)
epb->xfer = libusb_alloc_transfer(0);
epb->xfer->flags = 0;
libusb_fill_bulk_transfer(epb->xfer, g_devh, epb->ep, epb->buf, sizeof(epb->buf), usb_in_xfer_cb, NULL, 0);
}
/***********************************************************************
* libosmocore main loop integration of libusb async I/O
***********************************************************************/
static int g_libusb_pending = 0;
static int ofd_libusb_cb(struct osmo_fd *ofd, unsigned int what)
{
/* FIXME */
g_libusb_pending = 1;
return 0;
}
/* call-back when libusb adds a FD */
static void libusb_fd_added_cb(int fd, short events, void *user_data)
{
struct osmo_fd *ofd = talloc_zero(NULL, struct osmo_fd);
printf("%s(%u, %x)\n", __func__, fd, events);
ofd->fd = fd;
ofd->cb = &ofd_libusb_cb;
if (events & POLLIN)
ofd->when |= BSC_FD_READ;
if (events & POLLOUT)
ofd->when |= BSC_FD_WRITE;
osmo_fd_register(ofd);
}
/* call-back when libusb removes a FD */
static void libusb_fd_removed_cb(int fd, void *user_data)
{
printf("%s(%u)\n", __func__, fd);
#if 0
struct osmo_fd *ofd;
/* FIXME: This needs new export in libosmocore! */
ofd = osmo_fd_get_by_fd(fd);
if (ofd) {
osmo_fd_unregister(ofd);
talloc_free(ofd);
}
#endif
}
/* call-back when the UDP socket is readable */
static int ofd_udp_cb(struct osmo_fd *ofd, unsigned int what)
{
int rc;
socklen_t addrlen = sizeof(g_sa_remote);
rc = recvfrom(ofd->fd, g_buf_out.buf, sizeof(g_buf_out.buf), 0,
(struct sockaddr *)&g_sa_remote, &addrlen);
if (rc <= 0) {
fprintf(stderr, "error reading from UDP\n");
return 0;
}
printf("read %d bytes from UDP, forwarding to SIMTRACE\n", rc);
g_buf_out.xfer->length = rc;
/* disable further READ interest for the UDP socket */
ofd->when &= ~BSC_FD_READ;
/* submit the URB on the OUT end point */
libusb_submit_transfer(g_buf_out.xfer);
return 0;
}
static void run_mainloop(void)
{
int rc;
printf("Entering main loop\n");
while (1) {
osmo_select_main(0);
if (g_libusb_pending) {
struct timeval tv;
memset(&tv, 0, sizeof(tv));
rc = libusb_handle_events_timeout_completed(NULL, &tv, NULL);
if (rc != 0) {
fprintf(stderr, "handle_events_timeout_completed == %d\n", rc);
break;
}
}
}
}
int main(int argc, char **argv)
{
int rc;
int c, ret = 1;
int local_udp_port = 52342;
unsigned int if_num = 0;
print_welcome();
while (1) {
int option_index = 0;
static const struct option opts[] = {
{ "udp-port", 1, 0, 'u' },
{ "interface", 1, 0, 'I' },
{ "help", 0, 0, 'h' },
{ NULL, 0, 0, 0 }
};
c = getopt_long(argc, argv, "u:I:h", opts, &option_index);
if (c == -1)
break;
switch (c) {
case 'u':
local_udp_port = atoi(optarg);
break;
case 'I':
if_num = atoi(optarg);
break;
case 'h':
print_help();
exit(0);
break;
}
}
rc = libusb_init(NULL);
if (rc < 0) {
fprintf(stderr, "libusb initialization failed\n");
goto close_exit;
}
libusb_set_pollfd_notifiers(NULL, &libusb_fd_added_cb, &libusb_fd_removed_cb, NULL);
g_devh = libusb_open_device_with_vid_pid(NULL, USB_VENDOR_OPENMOKO, USB_PRODUCT_OWHW_SAM3);
if (!g_devh) {
fprintf(stderr, "can't open USB device\n");
goto close_exit;
}
rc = libusb_claim_interface(g_devh, if_num);
if (rc < 0) {
fprintf(stderr, "can't claim interface %u; rc=%d\n", if_num, rc);
goto close_exit;
}
/* open UDP socket, register with select handling and mark it
* readable */
g_udp_ofd.cb = ofd_udp_cb;
osmo_sock_init_ofd(&g_udp_ofd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, local_udp_port + if_num, OSMO_SOCK_F_BIND);
rc = osmo_libusb_get_ep_addrs(g_devh, if_num, &g_buf_out.ep, &g_buf_in.ep, NULL);
if (rc < 0) {
fprintf(stderr, "couldn't find enpdoint addresses; rc=%d\n", rc);
goto close_exit;
}
/* initialize USB buffers / transfers */
init_ep_buf(&g_buf_out);
init_ep_buf(&g_buf_in);
/* submit the first transfer for the IN endpoint */
libusb_submit_transfer(g_buf_in.xfer);
run_mainloop();
ret = 0;
libusb_release_interface(g_devh, 0);
close_exit:
if (g_devh)
libusb_close(g_devh);
libusb_exit(NULL);
return ret;
}