Commit Graph

1113 Commits

Author SHA1 Message Date
Eric Wild 0718caae23 host: simtrace2-sniff: init exit code
Early failure i.e. of libusb return random values as exit code.

Change-Id: If920824d35c1f37efe16440b5fb190ca0fb19148
2026-08-10 16:13:59 +00:00
Eric Wild dffc6601b2 host: simtrace2_api: do not log random memory
tx_cfg->features is logged before the memcpy() that fills it.
Looks like no one is using config.ac --enable-sanitize?

Change-Id: Id8369d312c8600ba9eea80c8f7782f196d7e20d1
2026-08-10 16:13:59 +00:00
Eric Wild 481a738a2d firmware: qmod: fix '@' debug command switching the wrong channel
Apparently copy paste mistake.

Change-Id: I9383adcfffab07ffa5f1fd3505e3aa244fb393ff
2026-08-10 16:13:59 +00:00
Eric Wild 670078b332 firmware: sniffer: honor the ep argument of usb_msg_alloc_hdr()
Even though all callers pass the same endpoint anyway the arg should
be used and not discarded.

Change-Id: I1fa0097b9eef531900b359c7293a6c60040254e4
2026-08-10 16:13:59 +00:00
Eric Wild 028f3b6bf5 firmware: sniffer: do not drop maximum-length ATRs
7816-3 8.1/8.2.1 allow TS plus 32 bytes.
atr_i is a byte count, not an index, process_byte_atr() guards its own
store with the same condition before incrementing, so atr_i reaches 33.

Change-Id: Ic8398cbefc0b522946b6470fd0268fa70662dab1
2026-08-10 16:13:59 +00:00
Eric Wild 2ade068fb6 firmware: sniffer: fix ~INS procedure byte comparison
(~g_tpdu.packet[1]) == byte can never be true.
Unary ~ applies the integer promotions first,
so for tpdu INS = 0xA4 lhs should be 0x5B but as int
it gets zero extended to at least 16 bits and then flipped,
so it is 0xFFFFFF5B = -165, byte promotes to 0..255.

The ack was therefore dead code -> fallthrough to SW1
branch, fails 0x6x/0x9x test, TPDU gets flagged
SNIFF_DATA_FLAG_ERROR_MALFORMED from what I can tell.
But I am losing track of all these arcane issues to be honest.

Narrow the complement back to 8 bits.
Fyi this is unrelated to signedness and not specific to ARM.

Change-Id: I800f50ef35356429d07aa685ea919e70ec34946e
2026-08-10 16:13:59 +00:00
Eric Wild 58438bfdbc firmware: card_emu: fix 7816-3 10.3.2 0=256 case
add_tpdu_byte() accumulates bytes received from the reader, but passes
reader_to_card = 0, but ISO 7816-3 10.3.2 says P3 == 0 means 256 not 0

Only reachable with P3 == 0 in a receive data phase, but
simtrace2-cardem-pcsc only requests PB_AND_RX when there is command data.

Fix this anyway.

Change-Id: I0fa4741bc1293549816595e6b4e8af9e22bcfbc1
2026-08-10 16:13:59 +00:00
Eric Wild f7170aff42 firmware: protect uart_tx_queue against ISR
cardem:
- dispatch_usb_command_cardem() appends to uart_tx_queue from the main loop
- tx_byte_tpdu() dequeues from the USART IRQ handler @ NVIC prio 0

card_handle_reset() has the same issue, drains queue and
frees uart_tx_msg from main loop while the ISR may own them.

All of this needs protection against the irq.

Needs a fixed llist_add_tail_irqsafe(), which called __enable_irq() instead of
restoring the saved PRIMASK for some unknown reason?!?!?!?

Change-Id: I7d9cdcc56263b27dfd4649dfb1da1d67761ee923
2026-08-10 16:13:59 +00:00
Eric Wild b36adff672 firmware: card_emu: use Di in the waiting time
ISO 7816-3 section 10.2 defines WT = WI x 960 x Fi/f seconds,
store as etu, etu = Fi / (D x f) seconds, so the Fi cancels,
but the D does not:

	WT [etu] = WI x 960 x D

cemu dropped both (?!) -> WI x 960.
The old comment explains why Fi can be dropped, which is right, but
what about Di ?!
sniffer gets it right (wt_wi * 960UL * wt_d), so the two state machines
disagreed here again, by up to a factor of 64???!!?!!?

This was fixed in osmo-ccid-firmware in 066489d in 2020 but not ported
to st2.

Additionally the waiting time was only recalculated at the end of the ATR,
where D is still 1 by definition, so a PPS increasing D reprogrammed the
baud rate but left the waiting time untouched??!

etu duration shrinks with D by the same factor, wall clock WT is
independent of D, which is the whole point.
The old code decreased the waiting time by a factor of D:
after a PPS to D=8 the card emitted its NULL procedure byte at ~0.09s
instead of ~0.71s with a reader deadline of ~1.43s,
and the inactivity timeout fires 8x too early, which probably led to
unexplained wtime_exp errors.

Update wt when WI becomes known (end of ATR) and
where D changes (after the PPS response) + tests.

Change-Id: I4263176d6073029d01f9ff5b11a6311617956af6
2026-08-10 16:13:59 +00:00
Eric Wild 88a6a782a2 firmware: use the full 11 bit US_FIDI.FI_DI_RATIO
US_FIDI_FI_DI_RATIO_Msk is 0x7ff,
cemu rejected >= 0x400 in emu_update_fidi() and masked with 0x3ff
in card_emu_uart_update_fidi(), but update_fidi() used by
the sniffer already used 0x7ff.

-> ratios 1024..2047 are unusable in cemu, which is the entire upper
half of ISO 7816-3 Table 7 at Di=1.

A reader trying one of those in a PPS gets the proposal echoed and
accepted, after which the card keeps transmitting at the old rate.

FI_DI_RATIO is clock periods per bit -> larger ratio is a SLOWER link.
The old check rejected slow values but accepted Fi=372/Di=64, ratio 5 !?

Unify and use the register mask (= shifed by 0 so usable as value) and
reject ratios that do not fit rather than truncating to garbage dividers.

Change-Id: I6211dd5be7c5c5d2150af2aa37a403b33e6d340d
2026-08-10 16:13:59 +00:00
Alexander Couzens 889b8d8927 cosmetic: fix typo in comment
Change-Id: I85939afd424c957ae872fa99f40b9cd528375537
2026-08-07 16:01:43 +02:00
Eric Wild d55729cba7 firmware: iso7816_3: fix F/D ratio for Di 8 and 9
iso7816_3_compute_fd_ratio() multiplied F by D for every d_index >= 8,
presumably because the upper half of ISO 7816-3 Table 8 encodes 1/D.

But 7816-3 2006 and 1997 differ!
That assumption is only true for the range 1010..1111, which in the
2006 version is RFU. Indices 1000 and 1001 are Di = 12 and Di = 20,
see iso7816_3_di_table[].

So right now Fi=372/Di=12 -> 372 * 12 = 4464 instead of 372 / 12 =
31.
In the cemu  value is rejected in emu_update_fidi()
and the old baud rate is silently kept.
In the sniffer update_fidi() programs US_FIDI as
4464 & 0x7ff = 368, which is garbage.

Use F/D for indices 1..9 and keep the legacy 1/D reading only for the RFU
range, where we cant really do anything useful anyway.

Change-Id: I44d6451d8b04aea2b0db7291b06a812afe84e52f
2026-07-28 18:22:02 +02:00
Alexander Couzens 567c03c83f firmware: card_emu.h: declare usb_buf_upd_len_and_submit()
usb_buf_upd_len_and_submit() is already a non-static function and should be
accessible.

Change-Id: I734d4b2d782223dd2f735f140d8c9bc6fc00a086
2026-07-14 18:24:44 +02:00
Alexander Couzens 1b43e1db48 firmware: card_emu.h: add missing header stdbool.h.
Change-Id: I4ad6d5cf0b4477cbd5f761d70c4cd92fb3ca1fcd
2026-07-14 18:24:15 +02:00
Alexander Couzens 01f36081fc README.md: describe the debug uart
Change-Id: I4cf929fded962e7ffb4c01628279385b2af04157
2026-07-14 18:24:10 +02:00
Alexander Couzens 7949165a4a README.md: fix typo in firmware
Change-Id: I16721418fce55322c8730920ceaa4e65f91a8a7e
2026-07-11 20:06:22 +02:00
Oliver Smith 5b9be4a52b Bump version: 0.9.0.4-3248-dirty → 0.9.1
Change-Id: I0d06c6f15aa99be7609eac9afb6b9df8e6cf3dc5
0.9.1
2026-07-08 08:47:20 +02:00
Oliver Smith 3248cdb416 firmware/Makefile: remove -Wredundant-decls
picolibc has redundant declarations of free and strdtold, which results
in lots of warnings with -Wredundant-decls. Adjust CFLAGS to remove
-Wredundant-decls to make the compiler log more readable.

  /usr/lib/picolibc/arm-none-eabi/include/stdlib.h:147:9: warning: redundant redeclaration of 'free' [-Wredundant-decls]
    147 | void    free (void *) __nothrow;
        |         ^~~~
  /usr/lib/picolibc/arm-none-eabi/include/stdlib.h:99:9: note: previous declaration of 'free' with type 'void(void *)'
     99 | void    free (void *) __nothrow;
        |         ^~~~
  /usr/lib/picolibc/arm-none-eabi/include/stdlib.h:307:9: warning: redundant redeclaration of 'strtold' [-Wredundant-decls]
    307 |         strtold (const char *__restrict, char **__restrict);
        |         ^~~~~~~
  /usr/lib/picolibc/arm-none-eabi/include/stdlib.h:286:9: note: previous declaration of 'strtold' with type 'long double(const char * restrict,  char ** restrict)'
    286 |         strtold (const char *__restrict __n, char **__restrict __end_PTR);
        |         ^~~~~~~

Change-Id: I32ff78f2a27191d1c6e114429d2e0713674732fe
2026-05-27 14:10:09 +02:00
Oliver Smith fffd7d5fac firmware: replace libnewlib with picolibc
libnewlib-arm-none-eabi has been removed in debian unstable, build the
firmware with picolibc instead.

Fixes: OS#7017
Change-Id: If136397b71435ea0850002ef6406067962e8d4d7
2026-05-27 14:08:25 +02:00
Harald Welte e1961beea1 Use gsmtap_inst_fd2() to remove compiler warning
gsmtap.c: In function 'osmo_st2_gsmtap_send_apdu':
gsmtap.c:69:9: warning: 'gsmtap_inst_fd' is deprecated: Use gsmtap_inst_fd2() instead [-Wdeprecated-declarations]
   69 |         rc = write(gsmtap_inst_fd(g_gti), buf, gross_len);
      |         ^~

Change-Id: I62de38175e63cb563635a01b025b805981e4b7a9
2026-02-27 11:18:45 +01:00
Oliver Smith d0ad985eb0 Generate a version.h file
Related: OS#6626
Change-Id: Ia894a9d9a20e343755d8060822d33b8a1c59b4db
2025-06-23 16:00:59 +02:00
Oliver Smith c38ad746f2 Bump version: 0.8.1 → 0.9.0
Change-Id: I6965017fcd5323677ce11fd9860d4355eb8f770f
0.9.0
2025-02-12 16:09:26 +01:00
Harald Welte 64e0adedb0 README.md: Add note to avoid using a VM
Change-Id: I59602e6a05600fa3e100e17c10e48977391361e3
2024-08-27 16:10:45 +02:00
Alexander Couzens f9885fe019 simtrace2-cardem-pcsc: allow to set sim presence polarity
Add api call osmo_st2_cardem_request_config2(struct cardemu_usb_msg_config *)
which contains a full cardemu_usb_msg_config.

Related: SYS#6946
Change-Id: Iacde63e667472a85a5f18cb4ca99dcfa1a84dd59
2024-06-23 18:47:42 +02:00
Alexander Couzens ec9b0b5254 firmware: allow to change the sim presence pin polarity
The sim presence pin allows the simtrace2 to inform the modem
about the presence of the simcard. On a generic simcard slot a
button is pressed by the simcard which generate a high or low voltage level.
Even though there are specifications of minipcie or ngff defining this signal, certain
modems behave different.
Certain modems require different signals from the specification.

Extend the usb protocol to set the behaviour at runtime.

Related: SYS#6946
Change-Id: I77118114ba873fdf0778137402888b40f2442456
2024-06-23 18:47:40 +02:00
Alexander Couzens 0cd501ef80 dissector: add support for sim polarity
Needs testing.

Related: SYS#6946
Change-Id: If3ae350d0f4d737bc9e80c4f5d73cc1a39d076af
2024-06-13 23:58:44 +02:00
Alexander Couzens d405133672 ngff_cardem: cosmetic: fix superflous space
Change-Id: Ib155d5778a0df7865958ab65fe2ce48b44f4326d
2024-05-22 15:09:23 +01:00
Oliver Smith 69deee859f contrib: remove rpm spec file
Related: https://osmocom.org/news/255
Related: OS#6446
Change-Id: I0cc8d753b26648efa7c9fb5798f7c4227c328547
2024-05-08 14:41:11 +02:00
Harald Welte 3fbed27b7a README.md: Add section on ngff_carem
This board has been around for ages and is supported from fimware + host
software.

Change-Id: I586b170fd6314af53a90d09020e41114714f1fec
2024-03-23 12:05:10 +01:00
Harald Welte b0d66ebfb0 README.md: Add links to where hardware can be bought
Change-Id: I0b990513153d60c0d70ca29b7243cf7a10c49575
2024-03-23 12:04:10 +01:00
Harald Welte 26517d89de README.md: add links to SIMtrace2 and QMOD
Change-Id: I6279765db0fdf92487a47f3ada28e4f49dd6e881
2024-03-23 12:03:45 +01:00
Harald Welte 6d2d1eeb36 README.md: Fix mark-down nested bullet syntax
Change-Id: I0b5a4cc00be4a8adba2797092fca41b99b336b79
2024-03-23 12:03:07 +01:00
Harald Welte 651987ae62 README.md: Fix mark-down nested bullet syntax
Change-Id: I252fa946010a2750d268b604d72e070ada99931c
2024-03-23 11:33:03 +01:00
Harald Welte ec157679d6 Add funding link to github mirror
see https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/displaying-a-sponsor-button-in-your-repository

Change-Id: I8be9c11198d09be628994ab0f530b5666fd25a7f
2024-03-23 11:32:23 +01:00
Oliver Smith e6e77399d6 firmware/Makefile: don't use distribution's CFLAGS
Set CFLAGS on top of the Makefile, to avoid using the distribution's
default CFLAGS. In debian testing and unstable, -fcf-protection was
recently added to the default CFLAGS, but it cannot be used with the
cross compiler:

  [COMPILING libosmocore/source/backtrace.c]
  cc1: error: '-fcf-protection=full' is not supported for this target
  make[2]: *** [Makefile:270: obj/simtrace/flash_backtrace.o] Error 1

Change-Id: I5f7cd0402f9bb85dd600204fdf8855773ddbeb70
2023-09-13 15:33:48 +00:00
Philipp Maier 1f77d673e2 simtrace2-cardem-pcsc: mark reset events in GSMTAP trace
At the moment only APDUs are logged to GSMTAP. It is not uncommon that a
card is resetted by the UE multiple times during normal operation. When
the trace lacks the reset events (ATR) it becomes difficult to follow in
which state the card actually is. Let't mark reset events by sending the
ATR via GSMTAP (like simtrace2_sniff already does it)

Related: OS#6094
Change-Id: I6b4d82b6ee369c95eeca8f7d59478452395fbe54
2023-07-21 13:03:38 +02:00
Harald Welte da078c090b Fix unchecked return value of osmo_libusb_init()
Fixes: CID#307501: Unchecked return value (CHECKED_RETURN)
Change-Id: Ic1f600dfd44d15da165a17d84f0faab6f6fd69c3
2023-07-18 15:04:24 +02:00
Oliver Smith 968d0b94f6 contrib/jenkins: tweak shell logic
Move the logic down to where make gets called, so we don't need the
variable. Print whether we use CLANG or GCC.

Don't put /opt/llvm-arm/bin infront of PATH unless building with CLANG.
Right now it doesn't seem to have e.g. an override for gcc, but the
files in that path may change when we update
LLVM-embedded-toolchain-for-Arm.

Related: OS#6026
Change-Id: Id768e0dbed9265f042b942e6699683723ca40a7c
2023-05-16 10:13:30 +02:00
Eric Wild 749dcdc277 fw: only build the bl with clang
-Oz breaks cardem, so just build the bootloader with clang.

Closes: OS#6026
Change-Id: Idb9e9a024fb8bfec28ff479c254ea73be0c8ef82
2023-05-15 16:37:35 +02:00
Oliver Smith f52b8b1a2e debian: set compat level to 10
Set --no-parallel, as in v10 debhelper defaults to parallel building.
This is apparently not supported by simtrace2's Makefile, it leads to
multiple non-trivial build errors. In contrib/jenkins.sh we also don't
build multiple firmwares in parallel.

Related: OS#5958
Related: https://manpages.debian.org/testing/debhelper/debhelper-compat-upgrade-checklist.7.en.html
Change-Id: I49fcc4fe9d3e795f8c3514d35ff3e2beca2917d1
2023-04-26 11:33:51 +02:00
Oliver Smith 68b072bcc8 debian/rules: remove override_dh_autoreconf
The Makefile already runs autoreconf -fi in the "utils" target:

  utils:
  	(cd host && \
  	 autoreconf -fi && \
  	 ./configure --prefix=/usr --disable-werror && \
  	 make)

The override is not useful, as it leads to debhelper running autoreconf
as well, but without running ./configure and make in the host directory
afterwards. So autoreconf just runs twice.

I've also considered to change debian/rules to only run the "fw"
target, and not the "utils" target of the Makefile. But that only makes
it more complex, as debhelper would then need to run make twice, once in
the root directory (as "make fw"), and once in the host directory. We
would need to add several lines to debian/rules to do effectively the
same thing.

Make this change now, as "cd host && dh_autoreconf" doesn't work with
debhelper compat level v10 anymore (--sourcedir could be used instead,
but as mentioned above, it's not useful).

Related: OS#5958
Change-Id: I12f379b4ec2de6adc86557d89319ab1d04ed5e73
2023-04-26 11:33:43 +02:00
Oliver Smith 6778c3f46e Cosmetic: fix CI errors
* Fix UTF-8 encoding. This caused the endianness check to fail, which
  reads all .c and .h files.

* Add .checkpatch.conf to skip linting for:
  firmware/atmel_softpack_libraries

Change-Id: Ibb2e42e9b4307275a33e4000c201847a6bd60137
2023-04-25 17:53:11 +02:00
Eric Wild 773d314142 contrib/jenkins.sh : build and publish combined targets
This makes production a bit easier.

Related: OS#5722
Change-Id: I32f9a0213aaefa50232a3d8dc2e7a4f2f44dbae1
2022-11-22 21:54:26 +00:00
Harald Welte 0c8cb51849 firmware/sniffer: Enable interrupts for overrun/parity/frame errors
We so far didn't have interrupts enabled for those, and just caught
them "by accident" if a byte was received or if a timeout happened.

Let's explicitly enable those interrupts so we also catch those
conditions by themselves.

Change-Id: Ia27f537706b9a6252dd18175545c6f27a7d17d0e
2022-11-19 09:02:23 +00:00
Harald Welte 89da837c23 firmware/sniffer: introduce #define for interrupt enable flags
Change-Id: Id4bc720a1db31b4433ff7b10d7a57d0ddb7d7180
2022-11-19 09:02:23 +00:00
Harald Welte 67a6d5724c firmware/sniffer: Handle WT timeouts via ring-buffer
Before this patch, all UART characters went through a fifo/ringbuffer
of depth 512, while events like timeout were delivered directly via
a global flags variable from ISR to main code.  This means that one or
more correct/complete TPDUs could theoretically still be in the FIFO,
but the "Fast path" of the timeout handling is pre-empting that and
messing with the state machines.

All events from the UART should be delivered via the ring-buffer to make
sure they arrive in order at the main function.

The old "report timeout via change flags in separate USB message" code
is left in place.  On the USB protocol we should keep it for
compatibility.  Internally we should probably also migrate that over
to the new ring-buffer method in a second step.

Change-Id: I4434c6fcd59d1a425e9ded734bbc8b0411a0a0d8
2022-11-19 09:02:23 +00:00
Harald Welte 61394cde29 firmware/sniffer: Pass PARITY/OVERRUN/FRAMING error via ringbuffer
those kind of errors should be passed to the main loop for further
processing, in-order together with the byte stream received from the
USART.

Change-Id: Iebd9bbc97c2a5a0c402e7a2711520299a1ade568
2022-11-19 09:02:23 +00:00
Harald Welte 92c44c572e firmware/sniffer: Add + use 16bit ringbuffer
So far, we use a uint8_t ring buffer as "FIFO" between USART Rx
interrupt and main context.  That's fine for expressing the bytes we
receive.  However, if we also want to report USART errors synchronously
in that stream, we actually need more bits to express those.

Reporting USART errors via the ring buffer is the only way how the
sniffer code can know in which TPDU the error occurred.  Reporting them
any other way (global variable, ...) would loose the timing relationship
where in the received stream the error occurred.

This change just changes the ringbuffer from 1024-entry 8bit to
512-entry 16bit and doesn't add any error reporting.

Change-Id: Ifde054fbfe7f753b61e7d3409c56eca6e0faeb4b
2022-11-19 09:02:23 +00:00
Harald Welte 4237c99fa2 firmware/sniffer: Disable TIMEOUT interrupts in USART IER on exit
Not critical (we disable the USART interrupts in NVIC anyway), but
if Sniffer_init() enables this flag, it's good style for Sniffer_exit()
to disable it.

Change-Id: I92e16a160d60fcab33c81e0cf074088b9f20b9ae
2022-11-19 09:02:23 +00:00
Harald Welte 3812317fba firmware/sniffer: Rename global variable 'wt' to 'g_wt'
It's a bad idea to have a two-character global variable which might
easily clash with local variable names.

Change-Id: Ic2fac64129d2772a1923f35e48582be3b130a0f2
2022-11-19 09:02:23 +00:00