Commit Graph

48 Commits

Author SHA1 Message Date
Harald Welte c472e33320 firmware/sniffer: Log cause of WT change
Change-Id: I14245c0ca96a258146e48bb9909efd9f8150f5ac
2022-11-19 09:02:23 +00:00
Harald Welte 716fe6cefa firmware/sniffer: Group global variables in structs
This is a purely cosmetic change that groups PPS, TPDU and ATR related
global variables into structs.  The structs get g_ prefixes to indicate
a global variable.  This avoids confusion between very short/generic
variable names that might clash with local variables.

Change-Id: I3e02f6c6b063ebc860b7a2a54dfc6051f1ea584f
2022-11-19 09:02:23 +00:00
Harald Welte 432c7b5058 firmware/sniffer: Make all global variables 'static'
None of those variables are used outside sniffer.c, so they can all be
static.

Change-Id: I8946acb6189d5ade57214295f0ba87f0608bad92
2022-11-19 09:02:23 +00:00
Harald Welte db1e37b93b firmware/sniffer: Fix programming error in PPS
process_byte_pps() would never enter the error path in which the
first byte would be != 0xff.  However, the caller already verified
this before calling process_byte_pps() so the error path should
never be hit anyway.

Change-Id: Ia74b6338219a6965e6bd35a6efcf369890e02d81
2022-11-19 09:02:23 +00:00
Harald Welte cc295f6945 firmware/sniffer: Avoid extra call for rbuf_is_full
rbuf_write() will tell us in the return value if the buffer was full
(error) or not (success).  Let's use that return value rather than a
theoretically race-y call to rbuf_is_full() before.

It's theoretical as the write happens from IRQ context and the read from
normal process context, so the fill-level cannot really change while
we're in the USART interrupt.  So it doesn't fix a bug, just improves
coding style and also avoids an extra function call + irq-disable/re-enable.

Change-Id: Icf570d0aa48d67a19e63c6e2b6caa14438fe88e3
2022-11-19 09:02:23 +00:00
Harald Welte 4836f23fa3 firmware/sniffer: Log old and new state in ISO7816-3 state changes
Change-Id: Iddb460cc2ad02c11a74de10dab127bb14cee9605
2022-11-19 09:02:23 +00:00
Harald Welte c343995b2d firmware/sniffer: refactor setting TPDU state
In low-level debugging it might be useful to trace the TPDU state
changes, so let's factor-out the state-setting as a function that
can be amended with printf() or GPIO toggles or the like.

No logical change is introduced here, just assignments replaced with
calling a function that does the assignment. compiler should inline
that.

Change-Id: Ie61321404f3686234c61c68a07d6cb9f5830ddc1
2022-11-19 09:02:23 +00:00
Harald Welte 0190e45305 firmware/sniffer: Log parity errors, just like overruns and framing errors
Reading of code + datasheet showed that we did enable parity checking
but never actually checked if the USART has the PARE bit in CSR set.

Let's change that.  Plus also avoid possible race conditions due to
multiple status resets via US_CR_RSTSTA.  Let's only reset that once
per interrupt handler.

TODO: actually do something useful at that point.  We currently don't
report those to the host, nor do we attempt to recover in any way.  The
data sheet also doesn't tell us what it actually does in such
situations; it appears the character is *not* returned from the USART,
so we're missing one byte in the stream at that point.

Change-Id: I5f012d86c61a2377d355396e7b95d078952bee7c
Related: OS#5464
2022-11-19 09:02:23 +00:00
Harald Welte 5523faf61f firmware/sniffer: Fix copy+paste when logging invalid INS bytes
Change-Id: I2679415f1853d4b4a33fca33791fb0bfc6908a1b
2022-11-15 21:19:26 +01:00
Oliver Smith f721e69bc1 treewide: remove FSF address
Remove the paragraph about writing to the Free Software Foundation's
mailing address. The FSF has changed addresses in the past, and may do
so again. In 2021 this is not useful, let's rather have a bit less
boilerplate at the start of source files.

Change-Id: Ie0a3b2273383adbb3303faffd6ff96be7f4cae99
2021-12-14 11:47:21 +01:00
Harald Welte c1ffc8a603 iso7816_fidi: Add iso7816_3_ prefix to symbols; fix terminology
Fi/Di are not the index into the table, but the contents of the table
as resolved by Fi_index / Di_index.  Let's clarify the terminology.

Change-Id: If364e08e7c9a3a9707e6d54b9267c6a7c088e415
2021-04-08 21:28:37 +00:00
Harald Welte bfd27afd5f firmware: Reformat value_string to pass our validation scripts
This is a purely cosmetic change in terms of coding style, but it
fixes an [invalid] detection for unterminated value_string arrays
by our verify_value_string_arrays_are_terminated.py script:

Change-Id: I2f2370a673074f6bf5380106b6254b4aa1e8a792
ERROR: file contains unterminated value_string 'struct value_string iso7816_3_card_state_names[]': './deps/simtrace2/firmware/libcommon/source/card_emu.c'
ERROR: file contains unterminated value_string 'struct value_string tpdu_state_names[]': './deps/simtrace2/firmware/libcommon/source/card_emu.c'
2019-12-17 12:57:09 +01:00
Kevin Redon 3864a794b4 minor: improve debug output
Change-Id: Ibfc22b95c0be3ac78bd50a40e31cc7d8546d81d9
2019-12-02 19:28:00 +01:00
Harald Welte c394109964 firmware: Enable -Wformat and resolve all related compiler warnings
There have been tons of format-string related bugs in our code which
we never discovered due to disabling -Wformat.  Let's fix that.

Change-Id: I5ec466361bcc526fac1f4897673264ee5af3458b
2018-08-26 10:20:05 +02:00
Kevin Redon dd36d9b010 sniff: send incomplete TPDU when reset is asserted
Change-Id: I8d7e4d604cded535e40d27c2be872268e0f24c20
2018-08-01 10:35:55 +02:00
Kevin Redon 2a44dc598d sniff: increase debug output
Change-Id: Ife0bbcf4a25aaa445a36768c00004e6e0d9a4947
2018-07-29 09:58:28 +02:00
Kevin Redon ec396bf402 sniff: fix ATR checksum calculation
this issue also cause the sent ATR to be empty

Change-Id: I6edff7aeb3185c835656acde75886fb6c90c0582
2018-07-28 20:59:44 +02:00
Kevin Redon 697199676e sniff: ensure the checksum error flag is also printed
the firmware now also use a generic value_string array (as does the
host application)

Change-Id: I861bd8b52e8f2f2a4786bbe1cc834917119dc394
2018-07-28 20:59:41 +02:00
Kevin Redon 8e84f8125c sniff: rename reset hold/release to assert/deassert
this change is to match the nomenclature used in cardem

Change-Id: Ide99e731cad26aec949ad14d54f46fa611a0b7f8
2018-07-26 15:34:08 +02:00
Kevin Redon f66af0c640 sniff: add checksum verification for ATR and PPS
a checksum error is only signaled in the USB message with the
corresponding flag.

Change-Id: I277868267c3199eea216ab47bdd09fb2fb944b06
2018-07-11 22:33:16 +02:00
Kevin Redon f000831d72 sniffer: fix inverted reset meaning
Change-Id: I860e2e29c059e1e0ec126695bb68d489891a17da
2018-07-11 22:01:58 +02:00
Kevin Redon b37bda0b55 sniffer: only allocate USB message if queue is not too long
When the host does not retrieved the USB messages, they keep getting
queue in the firmware, filling the RAM, and preventing further memory
to be allocated.

Change-Id: I0e447acdf757dcb5dc5c4d8b84a4235866914cad
2018-07-11 22:01:58 +02:00
Kevin Redon c6b968067d sniffer: also send malformed message over USB
Change-Id: I124c8db1d2f836e185630bbe8b5e52eb085d702a
2018-07-11 22:01:58 +02:00
Kevin Redon a95bb1e85d minor: fix linker warning
linker warning:
obj/simtrace/dfu_sniffer.o: In function `usb_send_data':
/media/data/work/simtrace2/simtrace2/firmware/libcommon/source/sniffer.c:338: warning: undefined reference to `putchar'

Change-Id: I934f3fcb474d4046e921511a024c3a0b8533c782
2018-07-11 22:01:58 +02:00
Kevin Redon da5578bd85 sniffer: set LED pattern
The red and green LEDs are now used as follows:
- red on = power (firmware is running)
- red blink = error detected (e.g. during ISO-7816 parsing)
- green on = running (sniffer mode is running)
- green blink = activity (e.g. USB message sent)

Change-Id: Ib1b6ff87fb92d6ed3ecae4bc89386206aaab508e
2018-07-11 22:01:58 +02:00
Kevin Redon a2fccba96c sniffer: move reset handling from ISR to main loop
Change-Id: Ia88fb4baf49017524bb201123da3511e5342ba48
2018-07-11 22:01:58 +02:00
Kevin Redon 411428eb5e sniffer: move data print before USB send
when the host software does not retrieve the USB messages the memory
is filled and no new USB messages can be allocated, preventing the
following code to print the message

Change-Id: Ie8946894a84cb853b45555f7abb1ca6f32111425
2018-07-11 22:01:58 +02:00
Kevin Redon f82f0f6eff USB: add flags for sniff data and centralise transfer
Also fix issue in usb_msg_alloc_hdr and add cosmetic spaces around
operations.

Change-Id: I768a0ad639aa5e648a630af72d01f7b68082b6b6
2018-07-11 22:01:58 +02:00
Kevin Redon 55f0612c8e cosmetic: put spaces around operations
Change-Id: If5ba5f8d16680e8403944274336ea595017063db
2018-07-11 22:01:58 +02:00
Kevin Redon 9a12d68c74 firmware: set license to GPLv2+
Change-Id: Id4c00159f984976b6d8641900fb64e36a3a30407
2018-07-11 22:01:58 +02:00
King Kévin 1200a5228f sniffer: add waiting time WT update
the waiting time (WT) is used to detect timeouts (e.g. for
unresponsive card or just to signal an error/NAK). it is essential
to detect to timeout to end current data transfer (e.g. change state).
by default (after a reset) to timeout is 9600 ETU, but this can change
at two places:
- after the ATR using the value of TC2 (if present)
- after a PPS (only F is used, and not D)

because the timeout value can be larger than the 16-bit of the USART
TO register, an external variable needs to be used for the count down.

Change-Id: I9735660ffce161cec8d4e63fa60a66fc8ef57525
2018-07-04 17:55:20 +02:00
Kevin Redon fe763b7698 sniffing: decrease USB IRQ prioprity to prevent USART overrun
Handling the USB message queue is done in an ISR and take quite some time.
This can cause a USART/SIM sniffing buffer overrun, resulting in data loss.
By setting the USB IRQ lower than the USART IRQ, the USB ISR can be
interrupted (for short) and no data gets lost.

Change-Id: I870a0aa8e251bbb53249c54bfcaa45de5b5a9486
2018-07-04 17:55:20 +02:00
Kevin Redon c9bd715289 sniffing: fix procedure byte handling and make TPDU parsing more strict
Change-Id: If991152f11c4b864ab1386f21dc13c335e6b281f
2018-07-04 17:55:20 +02:00
Kevin Redon 35e8bdf879 sniffer: also send incomplete (e.g. timeout) data (PPS/ATR/TPDU)
Change-Id: Ib070aca181042b477f1ffec48d63dc56c1e4609a
2018-07-04 17:55:20 +02:00
Kevin Redon 709a431ab9 sniffing: move conversion convertion and flag processing from ISR to main loop to keep ISR fast and focus on data capture
Change-Id: Ieefa8a5f81dbcc12c1ad3059660dbffa0c1a4961
2018-07-04 17:55:20 +02:00
Kevin Redon 7406337a7f sniffer: display F and D values frim PPS
Change-Id: I3641dcb6c24695a6d3dd3a1ee4333f56a07c99f0
2018-07-04 17:10:08 +02:00
Kevin Redon de97fd25bd sniffer USB: implement USB communication and send parsed messages
Change-Id: Ice7817480705f2124b08c1ff9a8826558b6d8b2b
2018-07-04 17:10:08 +02:00
Kevin Redon 638cec820f sniff: add WT timeout detection using USART timeout (TC is not required)
Change-Id: I4ec6e812e7e1eb91005027d2e864fc315550d79c
2018-07-04 16:33:00 +02:00
Kevin Redon 00ec89d73f sniff: add TPDU parsing (TPDUs become APDUs on the upper layer)
Change-Id: I09d050d95bd2ab140fe6b4926a37278eb08cc347
2018-07-04 16:33:00 +02:00
Kevin Redon cf59919494 sniff: print parsed ATR and PPS; use red LED to show main application is running; use green LED to indicate activity (message parsed)
Change-Id: I8e906bdbf2c91e608757ae442dfb241f981b8f1e
2018-07-04 16:33:00 +02:00
Kevin Redon 8fa6ff5979 sniffer: use ISR to store sniffed data in buffer, add ATR and PPS parsing, and PPS related FiDi update
Change-Id: I4b38ce1d80e370fda6aa181e959ba3f1286bb922
2018-07-04 14:29:28 +02:00
Kevin Redon d7a6de57d2 sniffer: add state definitions, improve IRQ handling, update pins configuration
Change-Id: I6b5dfd6d573ac122a60585a24c8a620642805ad5
2018-07-04 14:29:27 +02:00
Kevin Redon ee62a9da56 simtrace: add dedicated power pins configuration for sniffing
Change-Id: Ie37edbae6ea42d2b0c2a30eb5590afd0be797d39
2018-07-04 14:28:17 +02:00
Kevin Redon 45ad62d8d4 sniff: use USART 0 instead of USART 1
Use USART 0 connected to the SIM card side to sniff the communication.
The card side can also measure ETU times.
Do proper pin initialization.
This code can already capture the ATR communication between phone and card.

Change-Id: I0597ec723cb2225540c89c3821b91d8a45adfcd6
2018-07-04 11:24:13 +00:00
Kevin Redon 7b73462442 sniff mode: handle USART 1 RX interrupt to show sniffer data
Change-Id: I5504369aed39844b7cf21e5b3ccb542daa6b946e
2018-07-04 11:24:13 +00:00
Kevin Redon 36abece0b1 enable (empty) sniffer support for SIMtrace board
Change-Id: I51619135fd6f5423542f261e7d76bab1a5713e2b
2018-07-04 11:24:13 +00:00
Harald Welte ed75c62acf {ccid,sniffer}.c: Add comments on USB callbacks 2017-11-28 21:23:12 +01:00
Harald Welte d8a003dfd7 Structure build system to build for multiple boards/apps/environments 2017-02-27 22:18:45 +01:00