chen
2024-11-01 631a90c1116fa33382a88a747c89bf761bc0fa9b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * Copyright (c) 2019-2023 Beijing Hanwei Innovation Technology Ltd. Co. and
 * its subsidiaries and affiliates (collectly called MKSEMI).
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into an MKSEMI
 *    integrated circuit in a product or a software update for such product,
 *    must reproduce the above copyright notice, this list of conditions and
 *    the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of MKSEMI nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    MKSEMI integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be
 *    reverse engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY MKSEMI "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL MKSEMI OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include "se_api.h"
#include "mk_trace.h"
#include "uart_in_spi.h"
#include "mk_gpio.h"
#include "mk_spi.h"
 
#define SE_PRINT_DEBUG (0)
 
#if SE_PRINT_DEBUG == 1
#include "mk_uwb.h"
#include "RTTDEBUG.h"
#endif
 
static uint8_t g_uart_data[1024];
 
uint16_t passthrough_apdu_to_t1(uint8_t *cmd_buf, uint16_t cmd_len, uint32_t *resp_buf_addr, uint16_t *resp_len)
{
    SE_data cmd_apdu;
    SE_data rsp_apdu;
    ESESTATUS status = ESESTATUS_FAILED;
 
    // memset(&cmd_apdu, 0x00, sizeof(SE_data));
    // memset(&rsp_apdu, 0x00, sizeof(SE_data));
 
    cmd_apdu.p_data = cmd_buf;
    cmd_apdu.len = cmd_len;
    status = se_transmit_receive(&cmd_apdu, &rsp_apdu);
 
    if (status == ESESTATUS_SUCCESS)
    {
        if (rsp_apdu.len > 0)
        {
            int i = 0;
            LOG_INFO(TRACE_MODULE_SE, "[TX][%d]", rsp_apdu.len);
            while (i < rsp_apdu.len)
            {
                LOG_INFO(TRACE_NO_OPTION | TRACE_MODULE_SE, "%02X", rsp_apdu.p_data[i]);
                i++;
            }
            LOG_INFO(TRACE_NO_OPTION | TRACE_MODULE_SE, "\r\n");
        }
 
        *resp_buf_addr = (uint32_t)rsp_apdu.p_data;
        *resp_len = rsp_apdu.len;
    }
    else
    {
        LOG_INFO(TRACE_MODULE_SE, "response status=%d\r\n", status);
    }
    return (uint16_t)status;
}
 
static void __NO_RETURN cmd_from_uart_to_t1(void)
{
    int len;
    uint32_t resp_addr;
    uint16_t resp_len;
    uint8_t *p_data;
 
#if SE_PRINT_DEBUG == 1
    uint32_t timer1, timer2;
    uint8_t ins;
#endif
 
    while (1)
    {
        do
        {
            // memset(g_uart_data, 0, sizeof(g_uart_data));
 
            len = uart_receive_in_spi(g_uart_data, (uint16_t)sizeof(g_uart_data), NULL);
            if (len >= 5)
            {
                break;
            }
        } while (1);
 
#if SE_PRINT_DEBUG == 1
        timer1 = phy_timer_count_get();
        ins = g_uart_data[1];
#endif
 
        passthrough_apdu_to_t1(g_uart_data, (uint16_t)len, &resp_addr, (uint16_t *)&resp_len);
        p_data = (uint8_t *)resp_addr;
 
#if SE_PRINT_DEBUG == 1
        timer2 = phy_timer_count_get();
        SEGGER_RTT_printf(0, "T %u, ins = %02x\r\n", PHY_TIMER_COUNT_TO_MS(timer2 - timer1), ins);
#endif
 
        do
        {
            if (uart_send_in_spi(p_data, resp_len, NULL) == DRV_OK)
            {
                break;
            }
        } while (1);
    };
}
 
int tm_main(void)
{
    if (se_init(ESE_MODE_NORMAL) != ESESTATUS_SUCCESS)
    {
        return 0;
    }
 
    uart_in_spi_init();
 
#if SE_PRINT_DEBUG == 1
    RTTInit();
#endif
 
    // SEGGER_RTT_printf(0,"\n\rinstall applet\n\r");
    cmd_from_uart_to_t1();
 
    // se_deinit ();
 
    // return 1;
}
 
uint16_t transmit_only_apdu_to_t1(uint8_t *cmd_buf, uint16_t cmd_len)
{
    SE_data cmd_apdu;
    ESESTATUS status = ESESTATUS_FAILED;
 
    // memset(&cmd_apdu, 0x00, sizeof(SE_data));
 
    cmd_apdu.p_data = cmd_buf;
    cmd_apdu.len = cmd_len;
    status = se_transmit_only(&cmd_apdu);
 
    if (status == ESESTATUS_SUCCESS)
    {
    }
    else
    {
        LOG_INFO(TRACE_MODULE_SE, "response status=%d\r\n", status);
    }
    return (uint16_t)status;
}
 
uint16_t receive_only_resp_from_t1(uint32_t *resp_buf_addr, uint16_t *resp_len)
{
    SE_data rsp_apdu;
    ESESTATUS status = ESESTATUS_FAILED;
 
    status = se_receive_only(&rsp_apdu);
 
    /*
    if (status == ESESTATUS_NO_DATA_TO_RECEIVE)
    {
 
    }
    else */
    if (status == ESESTATUS_SUCCESS)
    {
        *resp_buf_addr = (uint32_t)rsp_apdu.p_data;
        *resp_len = rsp_apdu.len;
    }
    else
    {
        // LOG_INFO(TRACE_MODULE_SE, "response status=%d\r\n", status);
    }
    return (uint16_t)status;
}
 
uint8_t uwbs_check_se(uint8_t *p_des_data, uint8_t *p_des_len);
uint8_t uwbs_check_se(uint8_t *p_des_data, uint8_t *p_des_len)
{
    struct SPI_CFG_T usr_spi_cfg = {
        .bit_rate = 1000000,
        .data_bits = 8,
        .slave = 0,
        .clk_phase = 0,
        .clk_polarity = 0,
        .ti_mode = 0,
        .dma_rx = false,
        .dma_tx = false,
        .int_rx = false,
        .int_tx = false,
    };
    // get default AID from SE
    SE_data cmd_apdu = {
        .p_data = (uint8_t[]){0x00, 0xA4, 0x04, 0x00, 0x00},
        .len = 5,
    };
    SE_data rsp_apdu;
    uint8_t ret = DRV_OK;
 
    if (NULL == p_des_data || NULL == p_des_len)
    {
        LOG_ERROR(TRACE_MODULE_SE, "%s: input error!\r\n", __FUNCTION__);
        *p_des_len = 1;
        return DRV_ERROR;
    }
    // memset(&rsp_apdu, 0, sizeof(rsp_apdu));
    if (DRV_OK != (uint8_t)spi_open(SPI_ID1, &usr_spi_cfg))
    {
        LOG_ERROR(TRACE_MODULE_SE, "%s: spi open error!\r\n", __FUNCTION__);
        *p_des_len = 1;
        return DRV_ERROR;
    }
    gpio_pin_set_dir(IO_PIN_8, GPIO_DIR_OUT, 1);
    ESESTATUS status = se_init(ESE_MODE_NORMAL);
    if (status != ESESTATUS_SUCCESS)
    {
        ret = (uint8_t)status;
        LOG_ERROR(TRACE_MODULE_SE, "%s:se init error, ret=%02x\r\n", __FUNCTION__, ret);
        *p_des_len = 1;
        goto Errout;
    }
    status = se_transmit_receive(&cmd_apdu, &rsp_apdu);
    if (status != ESESTATUS_SUCCESS)
    {
        ret = (uint8_t)status;
        LOG_ERROR(TRACE_MODULE_SE, "%s: se transceive error! ret=%02x\r\n", __FUNCTION__, ret);
        *p_des_len = 1;
        goto Errout;
    }
    else
    {
        *p_des_len = (uint8_t)rsp_apdu.len;
        memcpy(p_des_data, rsp_apdu.p_data, rsp_apdu.len);
    }
    status = se_init(ESE_MODE_NORMAL);
    if (status != ESESTATUS_SUCCESS)
    {
        LOG_ERROR(TRACE_MODULE_SE, "%s: last se init error, status=%02x\r\n", __FUNCTION__, (uint8_t)status);
    }
    if (DRV_OK != spi_close(SPI_ID1))
    {
        LOG_ERROR(TRACE_MODULE_SE, "%s: spi close error!\r\n", __FUNCTION__);
        *p_des_len = 1;
        return DRV_ERROR;
    }
    return DRV_OK;
 
Errout:
    se_init(ESE_MODE_NORMAL);
    spi_close(SPI_ID1);
    return ret;
}