-
-
Notifications
You must be signed in to change notification settings - Fork 677
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Update to new prodtest structure + add support for board …
…rev.B
- Loading branch information
kopecdav
committed
Jan 31, 2025
1 parent
3488569
commit 4c189eb
Showing
5 changed files
with
299 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
/* | ||
* This file is part of the Trezor project, https://trezor.io/ | ||
* | ||
* Copyright (c) SatoshiLabs | ||
* | ||
* 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/>. | ||
*/ | ||
|
||
|
||
#include <trezor_rtl.h> | ||
|
||
#include <io/nfc.h> | ||
#include <sys/systick.h> | ||
#include <rtl/cli.h> | ||
|
||
static bool nfc_dev_activated = false; | ||
static nfc_dev_info_t dev_info = {0}; | ||
|
||
static void nfc_activated_callback() { | ||
|
||
nfc_dev_read_info(&dev_info); | ||
nfc_dev_activated = true; | ||
|
||
} | ||
|
||
static void nfc_write_tag_callback() { | ||
|
||
nfc_dev_info_t dev_info; | ||
nfc_dev_read_info(&dev_info); | ||
|
||
nfc_dev_activated = true; | ||
} | ||
|
||
|
||
static void prodtest_nfc_read_card(cli_t* cli){ | ||
|
||
uint32_t timeout = 0; | ||
memset(&dev_info, 0, sizeof(dev_info)); | ||
|
||
if (cli_has_arg(cli, "timeout") && !cli_arg_uint32(cli, "timeout", &timeout)) { | ||
cli_error_arg(cli, "Expecting timeout argument."); | ||
return; | ||
} | ||
|
||
if (cli_arg_count(cli) > 1) { | ||
cli_error_arg_count(cli); | ||
return; | ||
} | ||
|
||
nfc_dev_activated = false; | ||
|
||
nfc_status_t ret = nfc_init(); | ||
if (ret != NFC_OK) { | ||
cli_error(cli, CLI_ERROR_FATAL, "NFC init failed"); | ||
} else { | ||
cli_trace(cli, "NFC activated in reader mode for %d seconds.", timeout); | ||
} | ||
|
||
nfc_register_tech(NFC_POLLER_TECH_A | NFC_POLLER_TECH_B | | ||
NFC_POLLER_TECH_F | NFC_POLLER_TECH_V); | ||
nfc_register_event_callback(NFC_STATE_ACTIVATED, nfc_activated_callback); | ||
nfc_activate_stm(); | ||
|
||
uint32_t tick = systick_ms(); | ||
|
||
while (!nfc_dev_activated) { // Todo, while timeout or device found | ||
|
||
if ((systick_ms() - tick) > (timeout * 1000)) { | ||
nfc_deinit(); | ||
cli_error(cli, CLI_ERROR_TIMEOUT, "NFC timeout"); | ||
return; | ||
} | ||
|
||
nfc_feed_worker(); | ||
} | ||
|
||
cli_trace(cli, "NFC card detected."); | ||
|
||
switch (dev_info.type) { | ||
case NFC_DEV_TYPE_A: | ||
cli_trace(cli,"NFC Type A: UID: %s", dev_info.uid); | ||
break; | ||
case NFC_DEV_TYPE_B: | ||
cli_trace(cli,"NFC Type B: UID: %s", dev_info.uid); | ||
break; | ||
case NFC_DEV_TYPE_F: | ||
cli_trace(cli,"NFC Type F: UID: %s", dev_info.uid); | ||
break; | ||
case NFC_DEV_TYPE_V: | ||
cli_trace(cli,"NFC Type V: UID: %s", dev_info.uid); | ||
break; | ||
case NFC_DEV_TYPE_ST25TB: | ||
cli_trace(cli,"NFC Type ST25TB: UID: %s", dev_info.uid); | ||
break; | ||
case NFC_DEV_TYPE_AP2P: | ||
cli_trace(cli,"NFC Type AP2P: UID: %s", dev_info.uid); | ||
break; | ||
case NFC_DEV_TYPE_UNKNOWN: | ||
cli_error(cli, CLI_ERROR, "NFC Type UNKNOWN"); | ||
break; | ||
|
||
default: | ||
cli_error(cli, CLI_ERROR, "NFC Type UNKNOWN"); | ||
break; | ||
} | ||
|
||
nfc_deinit(); | ||
|
||
cli_ok(cli, ""); | ||
|
||
} | ||
|
||
|
||
static void prodtest_nfc_emulate_card(cli_t* cli){ | ||
|
||
uint32_t timeout = 0; | ||
|
||
if (cli_has_arg(cli, "timeout") && !cli_arg_uint32(cli, "timeout", &timeout)) { | ||
cli_error_arg(cli, "Expecting timeout argument."); | ||
return; | ||
} | ||
|
||
if (cli_arg_count(cli) > 1) { | ||
cli_error_arg_count(cli); | ||
return; | ||
} | ||
|
||
nfc_status_t ret = nfc_init(); | ||
|
||
if (ret != NFC_OK) { | ||
cli_error(cli, CLI_ERROR_FATAL, "NFC init failed"); | ||
} else { | ||
cli_trace(cli, "Emulation started for %d seconds", timeout); | ||
} | ||
|
||
nfc_register_tech(NFC_CARD_EMU_TECH_A); | ||
nfc_activate_stm(); | ||
|
||
uint32_t tick = systick_ms(); | ||
|
||
while ((systick_ms() - tick) < (timeout * 1000)) { | ||
nfc_feed_worker(); | ||
} | ||
|
||
cli_trace(cli, "Emulation over"); | ||
|
||
nfc_deinit(); | ||
|
||
cli_ok(cli, ""); | ||
|
||
} | ||
|
||
static void prodtest_nfc_write_card(cli_t* cli){ | ||
|
||
uint32_t timeout = 0; | ||
memset(&dev_info, 0, sizeof(dev_info)); | ||
|
||
if (cli_has_arg(cli, "timeout") && !cli_arg_uint32(cli, "timeout", &timeout)) { | ||
cli_error_arg(cli, "Expecting timeout argument."); | ||
return; | ||
} | ||
|
||
if (cli_arg_count(cli) > 1) { | ||
cli_error_arg_count(cli); | ||
return; | ||
} | ||
|
||
nfc_dev_activated = false; | ||
|
||
nfc_status_t ret = nfc_init(); | ||
|
||
if (ret != NFC_OK) { | ||
cli_error(cli, CLI_ERROR_FATAL, "NFC init failed"); | ||
} else { | ||
cli_trace(cli, "NFC reader on put the card on the reader (timeout %d s)", | ||
timeout); | ||
} | ||
|
||
nfc_register_tech(NFC_POLLER_TECH_A); | ||
nfc_register_event_callback(NFC_STATE_ACTIVATED, nfc_write_tag_callback); | ||
nfc_activate_stm(); | ||
|
||
uint32_t tick = systick_ms(); | ||
|
||
while (!nfc_dev_activated) { // Todo, while timeout or device found | ||
|
||
if ((systick_ms() - tick) > timeout * 1000) { | ||
nfc_deinit(); | ||
cli_error(cli, CLI_ERROR_TIMEOUT, "NFC timeout"); | ||
return; | ||
} | ||
|
||
nfc_feed_worker(); | ||
} | ||
|
||
if (dev_info.type != NFC_DEV_TYPE_A) { | ||
cli_error(cli, CLI_ERROR, "Only NFC type A cards supported"); | ||
return; | ||
} | ||
|
||
cli_trace(cli, "Writting URI to NFC tag %s", dev_info.uid); | ||
nfc_def_write_ndef_uri(); | ||
|
||
nfc_deinit(); | ||
|
||
cli_ok(cli, ""); | ||
|
||
} | ||
|
||
PRODTEST_CLI_CMD( | ||
.name = "nfc-read-card", | ||
.func = prodtest_nfc_read_card, | ||
.info = "Activate NFC in reader mode", | ||
.args = "<timeout>" | ||
); | ||
|
||
PRODTEST_CLI_CMD( | ||
.name = "nfc-emulate-card", | ||
.func = prodtest_nfc_emulate_card, | ||
.info = "Activate NFC in card emulation (CE) mode", | ||
.args = "<timeout>" | ||
); | ||
|
||
PRODTEST_CLI_CMD( | ||
.name = "nfc-write-card", | ||
.func = prodtest_nfc_write_card, | ||
.info = "Activate NFC in reader mode and write a URI to the attached card", | ||
.args = "<timeout>" | ||
); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters