|
| 1 | +/***************************************************************************** |
| 2 | + * (c) 2025 Ledger SAS. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + *****************************************************************************/ |
| 16 | + |
| 17 | +/* Includes ------------------------------------------------------------------*/ |
| 18 | +#include "ledger_protocol.h" |
| 19 | +#include "usbd_ledger.h" |
| 20 | +#include "usb_iap.h" |
| 21 | +#include "seproxyhal_protocol.h" |
| 22 | + |
| 23 | +/* Private enumerations ------------------------------------------------------*/ |
| 24 | + |
| 25 | +/* Private defines------------------------------------------------------------*/ |
| 26 | + |
| 27 | +/* Private types, structures, unions -----------------------------------------*/ |
| 28 | + |
| 29 | +/* Private macros-------------------------------------------------------------*/ |
| 30 | + |
| 31 | +/* Private functions prototypes ----------------------------------------------*/ |
| 32 | + |
| 33 | +/* Private variables ---------------------------------------------------------*/ |
| 34 | +static ledger_protocol_t protocol_data = {}; |
| 35 | + |
| 36 | +/* Exported variables --------------------------------------------------------*/ |
| 37 | + |
| 38 | +/* Private functions ---------------------------------------------------------*/ |
| 39 | +static void USB_LEDGER_IAP_send_packet(uint8_t *buffer, uint16_t length) |
| 40 | +{ |
| 41 | + if (length) { |
| 42 | + unsigned char hdr[3]; |
| 43 | + hdr[0] = SEPROXYHAL_TAG_USB_IAP_SEND; |
| 44 | + hdr[1] = length >> 8; |
| 45 | + hdr[2] = length; |
| 46 | + os_io_tx_cmd(OS_IO_PACKET_TYPE_SEPH, hdr, 3, NULL); |
| 47 | + os_io_tx_cmd(OS_IO_PACKET_TYPE_SEPH, buffer, length, NULL); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/* Exported functions --------------------------------------------------------*/ |
| 52 | + |
| 53 | +void USB_LEDGER_IAP_init(void) |
| 54 | +{ |
| 55 | + memset(&protocol_data, 0, sizeof(protocol_data)); |
| 56 | + protocol_data.mtu = sizeof(USBD_LEDGER_protocol_chunk_buffer); |
| 57 | + |
| 58 | + LEDGER_PROTOCOL_init(&protocol_data, OS_IO_PACKET_TYPE_USB_IAP_APDU); |
| 59 | +} |
| 60 | + |
| 61 | +int USB_LEDGER_IAP_send_apdu(const uint8_t *apdu_buf, uint16_t apdu_buf_length) |
| 62 | +{ |
| 63 | + uint32_t status = -1; |
| 64 | + |
| 65 | + ledger_protocol_result_t result = LEDGER_PROTOCOL_tx(&protocol_data, |
| 66 | + apdu_buf, |
| 67 | + apdu_buf_length, |
| 68 | + USBD_LEDGER_protocol_chunk_buffer, |
| 69 | + sizeof(USBD_LEDGER_protocol_chunk_buffer), |
| 70 | + 0); |
| 71 | + |
| 72 | + if (result != LP_SUCCESS) { |
| 73 | + goto error; |
| 74 | + } |
| 75 | + if (protocol_data.tx_chunk_length >= 2) { |
| 76 | + USB_LEDGER_IAP_send_packet(USBD_LEDGER_protocol_chunk_buffer, |
| 77 | + protocol_data.tx_chunk_length); |
| 78 | + } |
| 79 | + |
| 80 | + while (protocol_data.tx_apdu_buffer) { |
| 81 | + result = LEDGER_PROTOCOL_tx(&protocol_data, |
| 82 | + NULL, |
| 83 | + 0, |
| 84 | + USBD_LEDGER_protocol_chunk_buffer, |
| 85 | + sizeof(USBD_LEDGER_protocol_chunk_buffer), |
| 86 | + 0); |
| 87 | + if (result != LP_SUCCESS) { |
| 88 | + goto error; |
| 89 | + } |
| 90 | + if (protocol_data.tx_chunk_length >= 2) { |
| 91 | + USB_LEDGER_IAP_send_packet(USBD_LEDGER_protocol_chunk_buffer, |
| 92 | + protocol_data.tx_chunk_length); |
| 93 | + } |
| 94 | + } |
| 95 | + status = 0; |
| 96 | + |
| 97 | +error: |
| 98 | + return status; |
| 99 | +} |
| 100 | + |
| 101 | +int USB_LEDGER_iap_rx_seph_evt(uint8_t *seph_buffer, |
| 102 | + uint16_t seph_buffer_length, |
| 103 | + uint8_t *apdu_buffer, |
| 104 | + uint16_t apdu_buffer_max_length) |
| 105 | +{ |
| 106 | + int status = -1; |
| 107 | + |
| 108 | + if (seph_buffer[1] != SEPROXYHAL_TAG_USB_IAP_EVENT || seph_buffer_length < 4) { |
| 109 | + goto error; |
| 110 | + } |
| 111 | + |
| 112 | + ledger_protocol_result_t result = LEDGER_PROTOCOL_rx(&protocol_data, |
| 113 | + &seph_buffer[4], |
| 114 | + seph_buffer_length - 4, |
| 115 | + USBD_LEDGER_protocol_chunk_buffer, |
| 116 | + sizeof(USBD_LEDGER_protocol_chunk_buffer), |
| 117 | + apdu_buffer, |
| 118 | + apdu_buffer_max_length, |
| 119 | + 0); |
| 120 | + if (result != LP_SUCCESS) { |
| 121 | + goto error; |
| 122 | + } |
| 123 | + |
| 124 | + if (protocol_data.tx_chunk_length > 0) { |
| 125 | + // Tx chunck was generated while processing rx |
| 126 | + // For example MTU request -> MTU response |
| 127 | + USB_LEDGER_IAP_send_packet(USBD_LEDGER_protocol_chunk_buffer, |
| 128 | + protocol_data.tx_chunk_length); |
| 129 | + protocol_data.tx_chunk_length = 0; |
| 130 | + } |
| 131 | + |
| 132 | + if (protocol_data.rx_apdu_status == APDU_STATUS_COMPLETE) { |
| 133 | + // Should not happen as it is verified by LEDGER_PROTOCOL_rx() already |
| 134 | + if (apdu_buffer_max_length < protocol_data.rx_apdu_length) { |
| 135 | + status = -1; |
| 136 | + } |
| 137 | + else { |
| 138 | + status = protocol_data.rx_apdu_length; |
| 139 | + } |
| 140 | + protocol_data.rx_apdu_status = APDU_STATUS_WAITING; |
| 141 | + } |
| 142 | + |
| 143 | +error: |
| 144 | + return status; |
| 145 | +} |
0 commit comments