Skip to content

Commit

Permalink
[add] Enable NFC for Flex & Stax + minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lpascal-ledger committed Nov 18, 2024
1 parent a673c1c commit 71160c9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [0.12.0] 2024-??-??

### Added

- NFC always enabled on Stax & Flex OSes
- Starting Speculos with the `--nfc` argument will funel all communications as NFC

## [0.11.0] 2024-11-12

### Added
Expand Down
32 changes: 12 additions & 20 deletions speculos/mcu/nfc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
Forward NFC packets between the MCU and the SE
"""

from abc import ABC, abstractmethod
from construct import Int8ub, Int16ub, Int16ul, Struct
import binascii
import enum
import logging
from typing import List, Optional


class SephNfcTag(enum.IntEnum):
Expand All @@ -18,42 +16,36 @@ class NFC:
def __init__(self, _queue_event_packet):
self._queue_event_packet = _queue_event_packet
self.packets_to_send = []
self.MTU=140
self.MTU = 140
self.rx_sequence = 0
self.rx_size = 0
self.rx_data = []

self.rx_data: bytes = b''
self.logger = logging.getLogger("nfc")


def handle_rapdu_chunk(self, data):
def handle_rapdu_chunk(self, data: bytes) -> Optional[List[bytes]]:
"""concatenate apdu chunks into full apdu"""

# example of data
# 0000050000002b3330000409312e302e302d72633104e600000008362e312e302d646508352e312e302d6465010001009000

# only APDU packets are suported

Check failure on line 30 in speculos/mcu/nfc.py

View workflow job for this annotation

GitHub Actions / Check misspellings

suported ==> supported
if data[2] != 0x05:
return None
return

sequence = int.from_bytes(data[3:5], 'big')
if self.rx_sequence != sequence:
print(f"Unexpected sequence number:{sequence}")
return None
assert self.rx_sequence == sequence, f"Unexpected sequence number:{sequence}"

if sequence == 0:
self.rx_size = int.from_bytes(data[5:7], "big")
self.rx_data = data[7:]
else:
self.rx_data.append(data[5:])
self.rx_data += data[5:]

if len(self.rx_data) == self.rx_size:
#prepare for next call
# prepare for next call
self.rx_sequence = 0
return self.rx_data

return None

else:
self.rx_sequence += 1

def apdu(self, data):
chunks: List[bytes] = []
Expand All @@ -65,11 +57,11 @@ def apdu(self, data):
data = data[size:]

for i, chunk in enumerate(chunks):
# ledger protocol header
# Ledger protocol header
header = bytes([0x00, 0x00, 0x05]) # APDU
header += i.to_bytes(2, "big")
# first packet contains the size of full buffer
if i == 0:
header += data_len.to_bytes(2, "big")

self._queue_event_packet(SephNfcTag.NFC_APDU_EVENT, header+chunk)
self._queue_event_packet(SephNfcTag.NFC_APDU_EVENT, header + chunk)
2 changes: 1 addition & 1 deletion speculos/mcu/seproxyhal.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def can_read(self, screen: DisplayNotifier):

elif tag == SephTag.NFC_RAPDU:
data = self.nfc.handle_rapdu_chunk(data)
if data:
if data is not None:
for c in self.apdu_callbacks:
c(data)
screen.display.forward_to_apdu_client(data)
Expand Down
3 changes: 2 additions & 1 deletion src/bolos/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define OS_SETTING_PLANEMODE_OLD 5
#define OS_SETTING_PLANEMODE_NEW 6
#define OS_SETTING_SOUND 9
#define OS_SETTING_FEATURES 14

#undef PATH_MAX
#define PATH_MAX 1024
Expand Down Expand Up @@ -54,7 +55,7 @@ unsigned long sys_os_setting_get(unsigned int setting_id,
return 1;
}
if (((hw_model == MODEL_STAX) || (hw_model == MODEL_FLEX)) &&
setting_id == OS_SETTING_SOUND) {
(setting_id == OS_SETTING_SOUND || setting_id == OS_SETTING_FEATURES)) {
return 0xff;
}
}
Expand Down

0 comments on commit 71160c9

Please sign in to comment.