Reorganizing core0/core1 split.
Now CBOR and APDU (i.e., intensive processing) areas are executed on core1, while core0 is dedicated for hardware tasks (usb, button, led, etc.).
This commit is contained in:
parent
847005d94f
commit
8b97791d8f
6 changed files with 64 additions and 44 deletions
32
src/apdu.c
32
src/apdu.c
|
|
@ -125,6 +125,38 @@ uint16_t set_res_sw(uint8_t sw1, uint8_t sw2) {
|
|||
return make_uint16_t(sw1, sw2);
|
||||
}
|
||||
|
||||
void apdu_thread() {
|
||||
|
||||
while (1) {
|
||||
uint32_t m;
|
||||
queue_remove_blocking(&usb_to_card_q, &m);
|
||||
|
||||
if (m == EV_VERIFY_CMD_AVAILABLE || m == EV_MODIFY_CMD_AVAILABLE)
|
||||
{
|
||||
set_res_sw (0x6f, 0x00);
|
||||
goto done;
|
||||
}
|
||||
else if (m == EV_EXIT) {
|
||||
if (current_app && current_app->unload) {
|
||||
current_app->unload();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
process_apdu();
|
||||
|
||||
done:;
|
||||
|
||||
apdu_finish();
|
||||
finished_data_size = apdu_next();
|
||||
uint32_t flag = EV_EXEC_FINISHED;
|
||||
queue_add_blocking(&card_to_usb_q, &flag);
|
||||
}
|
||||
//printf("EXIT !!!!!!\r\n");
|
||||
if (current_app && current_app->unload)
|
||||
current_app->unload();
|
||||
}
|
||||
|
||||
void apdu_finish() {
|
||||
apdu.rdata[apdu.rlen] = apdu.sw >> 8;
|
||||
apdu.rdata[apdu.rlen+1] = apdu.sw & 0xff;
|
||||
|
|
|
|||
|
|
@ -81,5 +81,6 @@ extern int process_apdu();
|
|||
extern size_t apdu_process(const uint8_t *buffer, size_t buffer_size);
|
||||
extern void apdu_finish();
|
||||
extern size_t apdu_next();
|
||||
extern void apdu_thread();
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -174,7 +174,7 @@ int driver_process_usb_packet(uint16_t rx_read) {
|
|||
ccid_response->abRFU1 = 0;
|
||||
//printf("1 %x %x %x || %x %x %x\r\n",ccid_response->apdu,apdu.rdata,ccid_response,ccid_header,ccid_header->apdu,apdu.data);
|
||||
memcpy(apdu.rdata, ccid_atr+1, size_atr);
|
||||
card_start();
|
||||
card_start(apdu_thread);
|
||||
ccid_status = 0;
|
||||
ccid_write(size_atr);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -140,6 +140,10 @@ uint8_t last_seq = 0;
|
|||
CTAPHID_FRAME last_req = { 0 };
|
||||
uint32_t lock = 0;
|
||||
|
||||
uint8_t thread_type = 0; //1 is APDU, 2 is CBOR
|
||||
extern void cbor_thread();
|
||||
extern void init_fido();
|
||||
|
||||
int driver_process_usb_packet(uint16_t read) {
|
||||
int apdu_sent = 0;
|
||||
if (read >= 5)
|
||||
|
|
@ -199,9 +203,8 @@ int driver_process_usb_packet(uint16_t read) {
|
|||
ctap_resp->init.bcntl = 17;
|
||||
ctap_resp->init.bcnth = 0;
|
||||
hid_write(64);
|
||||
current_app = apps[0].select_aid(&apps[0]);
|
||||
card_start();
|
||||
DEBUG_PAYLOAD((uint8_t *)ctap_resp, ctap_resp->init.bcntl+7);
|
||||
DEBUG_PAYLOAD((uint8_t *)ctap_resp, ctap_resp->init.bcntl + 7);
|
||||
init_fido();
|
||||
}
|
||||
else if (ctap_req->init.cmd == CTAPHID_WINK) {
|
||||
if (MSG_LEN(ctap_req) != 0) {
|
||||
|
|
@ -230,6 +233,12 @@ int driver_process_usb_packet(uint16_t read) {
|
|||
hid_write(64);
|
||||
}
|
||||
else if (last_cmd == CTAPHID_MSG && (msg_packet.len == 0 || (msg_packet.len == msg_packet.current_len && msg_packet.len > 0))) {
|
||||
|
||||
current_app = apps[0].select_aid(&apps[0]);
|
||||
if (thread_type != 1)
|
||||
card_start(apdu_thread);
|
||||
thread_type = 1;
|
||||
|
||||
if (msg_packet.current_len == msg_packet.len && msg_packet.len > 0)
|
||||
apdu_sent = apdu_process(msg_packet.data, msg_packet.len);
|
||||
else
|
||||
|
|
@ -238,6 +247,10 @@ int driver_process_usb_packet(uint16_t read) {
|
|||
msg_packet.len = msg_packet.current_len = 0; //Reset the transaction
|
||||
}
|
||||
else if (last_cmd == CTAPHID_CBOR && (msg_packet.len == 0 || (msg_packet.len == msg_packet.current_len && msg_packet.len > 0))) {
|
||||
|
||||
if (thread_type != 2)
|
||||
card_start(cbor_thread);
|
||||
thread_type = 2;
|
||||
if (msg_packet.current_len == msg_packet.len && msg_packet.len > 0)
|
||||
apdu_sent = cbor_process(msg_packet.data, msg_packet.len);
|
||||
else
|
||||
|
|
@ -270,7 +283,10 @@ uint8_t *driver_prepare_response() {
|
|||
}
|
||||
|
||||
void driver_exec_finished(size_t size_next) {
|
||||
driver_exec_finished_cont(size_next, 7);
|
||||
if (apdu.sw != 0)
|
||||
ctap_error(apdu.sw & 0xff);
|
||||
else
|
||||
driver_exec_finished_cont(size_next, 7);
|
||||
}
|
||||
|
||||
void driver_exec_finished_cont(size_t size_next, size_t offset) {
|
||||
|
|
|
|||
|
|
@ -140,46 +140,18 @@ static int usb_event_handle() {
|
|||
return 0;
|
||||
}
|
||||
|
||||
extern void low_flash_init();
|
||||
static void card_init_core1(void) {
|
||||
//gpg_data_scan (flash_do_start, flash_do_end);
|
||||
low_flash_init_core1();
|
||||
low_flash_init();
|
||||
}
|
||||
|
||||
void card_thread() {
|
||||
card_init_core1();
|
||||
size_t finished_data_size = 0;
|
||||
|
||||
while (1) {
|
||||
uint32_t m;
|
||||
queue_remove_blocking(&usb_to_card_q, &m);
|
||||
|
||||
if (m == EV_VERIFY_CMD_AVAILABLE || m == EV_MODIFY_CMD_AVAILABLE)
|
||||
{
|
||||
set_res_sw (0x6f, 0x00);
|
||||
goto done;
|
||||
}
|
||||
else if (m == EV_EXIT) {
|
||||
if (current_app && current_app->unload) {
|
||||
current_app->unload();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
process_apdu();
|
||||
|
||||
done:;
|
||||
uint32_t flag = EV_EXEC_FINISHED;
|
||||
queue_add_blocking(&card_to_usb_q, &flag);
|
||||
}
|
||||
//printf("EXIT !!!!!!\r\n");
|
||||
if (current_app && current_app->unload)
|
||||
current_app->unload();
|
||||
}
|
||||
|
||||
void card_thread();
|
||||
void card_start()
|
||||
{
|
||||
void apdu_thread();
|
||||
void card_start(void (*func)(void)) {
|
||||
multicore_reset_core1();
|
||||
multicore_launch_core1(card_thread);
|
||||
card_init_core1();
|
||||
multicore_launch_core1(func);
|
||||
led_set_blink(BLINK_MOUNTED);
|
||||
}
|
||||
|
||||
|
|
@ -201,9 +173,7 @@ void usb_task() {
|
|||
// printf("\r\n ------ M = %lu\r\n",m);
|
||||
if (has_m) {
|
||||
if (m == EV_EXEC_FINISHED) {
|
||||
apdu_finish();
|
||||
size_t size_next = apdu_next();
|
||||
driver_exec_finished(size_next);
|
||||
driver_exec_finished(finished_data_size);
|
||||
led_set_blink(BLINK_MOUNTED);
|
||||
}
|
||||
else if (m == EV_PRESS_BUTTON) {
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ extern void driver_exec_timeout();
|
|||
extern bool driver_mounted();
|
||||
extern uint8_t *driver_prepare_response();
|
||||
|
||||
extern void card_start();
|
||||
extern void card_start(void (*func)(void));
|
||||
extern void card_exit();
|
||||
extern void usb_init();
|
||||
extern uint8_t *usb_prepare_response();
|
||||
|
|
@ -56,4 +56,5 @@ extern uint8_t *usb_get_rx();
|
|||
extern uint8_t *usb_get_tx();
|
||||
extern uint32_t usb_write_offset(uint16_t len, uint16_t offset);
|
||||
extern void usb_clear_rx();
|
||||
extern size_t finished_data_size;
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue