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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
From 96c73de212c84caa1cc2796980e762321e0acdc3 Mon Sep 17 00:00:00 2001
From: David Bauer <david.bauer@uniberg.com>
Date: Wed, 27 Mar 2024 22:20:16 +0100
Subject: [PATCH] driver: add uqmi backend
This commit adds a backend to interface with an eUICC connected to a
compatible modem using the uqmi application.
This allows OpenWrt to manage an eUICC including the download and
installation of SIM profiles.
This was previously not possible with most modems, as the APDU
operations were aborted due to exceeding the timeout imposed
on the AT interface by the modem firmware.
Tested-on: Quectel EC25 / Quectel EP06 / Quectel RG520N
Signed-off-by: David Bauer <david.bauer@uniberg.com>
---
driver/CMakeLists.txt | 3 +
driver/apdu/uqmi.c | 273 ++++++++++++++++++++++++++++++++++++++++++
driver/apdu/uqmi.h | 7 ++
driver/driver.c | 6 +
4 files changed, 289 insertions(+)
create mode 100644 driver/apdu/uqmi.c
create mode 100644 driver/apdu/uqmi.h
--- a/driver/CMakeLists.txt
+++ b/driver/CMakeLists.txt
@@ -52,6 +52,9 @@ if(LPAC_WITH_APDU_AT)
target_sources(euicc-drivers PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/apdu/at.c)
endif()
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DLPAC_WITH_APDU_UQMI")
+target_sources(euicc-drivers PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/apdu/uqmi.c)
+
if(LPAC_WITH_APDU_GBINDER)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DLPAC_WITH_APDU_GBINDER")
target_sources(euicc-drivers PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/apdu/gbinder_hidl.c)
--- /dev/null
+++ b/driver/apdu/uqmi.c
@@ -0,0 +1,273 @@
+// SPDX-License-Identifier: MIT
+/* Copyright (c) 2024 David Bauer <david.bauer@uniberg.com> */
+
+#include "at.h"
+
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <cjson/cJSON_ex.h>
+#include <euicc/interface.h>
+#include <euicc/hexutil.h>
+
+static FILE *fuart;
+static int client_id = 0;
+static int logic_channel = 0;
+static char *devpath = NULL;
+
+
+static int uqmi_execute_command(const char *command, char *buf, size_t bufsize)
+{
+ char *debug_str = getenv("LPAC_QMI_DEBUG");
+ char final_command[2048] = {};
+ FILE *fp;
+ char *remaining;
+ int debug = debug_str ? atoi(debug_str) : 0;
+
+ if (debug)
+ printf("UQMI_DEBUG_TX: %s\n", command);
+
+ if (snprintf(final_command, sizeof(final_command), "uqmi -s -d %s %s", devpath, command) >= sizeof(final_command))
+ {
+ fprintf(stderr, "Command too long\n");
+ return -1;
+ }
+
+ fp = popen(final_command, "r");
+ if (fp == NULL)
+ {
+ fprintf(stderr, "Failed to execute command: %s\n", command);
+ return -1;
+ }
+
+ remaining = buf;
+ if (buf) {
+ while (fgets(remaining, bufsize - (remaining - buf), fp) != NULL)
+ {
+ /* Read to buffer */
+ }
+ if (debug)
+ printf("UQMI_DEBUG_RX: %s", buf);
+ }
+
+ pclose(fp);
+ return 0;
+}
+
+static int uqmi_open_client()
+{
+ char buffer[2048] = {};
+ int ret;
+
+ ret = uqmi_execute_command("uqmi -s -d /dev/cdc-wdm0 --get-client-id uim", buffer, sizeof(buffer));
+ if (ret)
+ {
+ return -1;
+ }
+
+ client_id = atoi(buffer);
+ if (client_id == 0)
+ {
+ return -1;
+ }
+
+ return 0;
+}
+
+static int apdu_interface_connect(struct euicc_ctx *ctx)
+{
+ const char *device;
+
+ client_id = 0;
+ logic_channel = 0;
+ devpath = getenv("LPAC_QMI_DEV");
+
+ return uqmi_open_client();
+}
+
+static void apdu_interface_disconnect(struct euicc_ctx *ctx)
+{
+ char command[64] = {};
+
+ snprintf(command, sizeof(command), "--set-client-id uim,%d --release-client-id uim", client_id);
+ uqmi_execute_command(command, NULL, 0);
+
+ client_id = 0;
+ logic_channel = 0;
+}
+
+static int apdu_interface_transmit(struct euicc_ctx *ctx, uint8_t **rx, uint32_t *rx_len, const uint8_t *tx, uint32_t tx_len)
+{
+ cJSON *root = NULL, *jtmp = NULL;
+ char cmd[2048] = {};
+ char buf[2048] = {};
+ char *json;
+
+ *rx = NULL;
+ *rx_len = 0;
+
+ if (!client_id)
+ {
+ return -1;
+ }
+
+ if (!logic_channel)
+ {
+ return -1;
+ }
+
+ for (int i = 0; i < tx_len; i++)
+ {
+ snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%02X", (uint8_t)(tx[i] & 0xFF));
+ }
+
+ snprintf(cmd, sizeof(cmd), "--set-client-id uim,%d --keep-client-id uim --uim-slot 1 --uim-channel-id %d --uim-apdu-send \"%s\"",
+ client_id, logic_channel, buf);
+ uqmi_execute_command(cmd, buf, sizeof(buf));
+
+ json = strstr(buf, "{");
+ if (!json)
+ {
+ printf("No JSON found\n");
+ goto fail;
+ }
+
+ root = cJSON_Parse(json);
+ if (!root)
+ {
+ printf("Failed to parse JSON\n");
+ goto fail;
+ }
+
+ jtmp = cJSON_GetObjectItem(root, "response");
+ if (!jtmp || !cJSON_IsString(jtmp))
+ {
+ printf("Failed to get response\n");
+ goto fail;
+ }
+
+ *rx_len = strlen(jtmp->valuestring) / 2;
+ *rx = malloc(*rx_len);
+ if (euicc_hexutil_hex2bin_r(*rx, *rx_len, jtmp->valuestring, strlen(jtmp->valuestring)) < 0)
+ {
+ printf("Failed to convert hex '%s' to binary\n", jtmp->valuestring);
+ goto fail;
+ }
+
+ cJSON_Delete(root);
+ return 0;
+fail:
+ if (root)
+ {
+ cJSON_Delete(root);
+ }
+
+ if (*rx)
+ {
+ free(*rx);
+ }
+ *rx_len = 0;
+ return -1;
+}
+
+static int apdu_interface_logic_channel_open(struct euicc_ctx *ctx, const uint8_t *aid, uint8_t aid_len)
+{
+ char *response, *json;
+ char buf[2048] = {};
+ char cmd[2048] = {};
+ cJSON *root, *jtmp;
+
+ if (!client_id)
+ {
+ return -1;
+ }
+
+ if (logic_channel)
+ {
+ return logic_channel;
+ }
+
+ snprintf(cmd, sizeof(cmd), "--set-client-id uim,%d --keep-client-id uim --uim-slot 1 --uim-channel-open ", client_id);
+ for (int i = 0; i < aid_len; i++)
+ {
+ snprintf(cmd + strlen(cmd), sizeof(cmd) - strlen(cmd), "%02X", (uint8_t)(aid[i] & 0xFF));
+ }
+
+ uqmi_execute_command(cmd, buf, sizeof(buf));
+
+ json = strstr(buf, "{");
+ if (!json)
+ {
+ printf("No JSON found\n");
+ return -1;
+ }
+
+ root = cJSON_Parse(json);
+ if (!root)
+ {
+ printf("Failed to parse JSON\n");
+ return -1;
+ }
+
+ jtmp = cJSON_GetObjectItem(root, "channel_id");
+ if (!jtmp || !cJSON_IsNumber(jtmp))
+ {
+ printf("Failed to get channel_id\n");
+ cJSON_Delete(root);
+ return -1;
+ }
+
+ logic_channel = jtmp->valueint;
+
+ cJSON_Delete(root);
+ return logic_channel;
+}
+
+static void apdu_interface_logic_channel_close(struct euicc_ctx *ctx, uint8_t channel)
+{
+ char cmd[2048] = {};
+
+ if (!logic_channel)
+ {
+ return;
+ }
+
+ snprintf(cmd, sizeof(cmd), "--set-client-id uim,%d --keep-client-id uim --uim-slot 1 --uim-channel-id %d --uim-channel-close", client_id, logic_channel);
+ uqmi_execute_command(cmd, NULL, 0);
+ logic_channel = 0;
+ return;
+}
+
+static int libapduinterface_init(struct euicc_apdu_interface *ifstruct)
+{
+ memset(ifstruct, 0, sizeof(struct euicc_apdu_interface));
+
+ ifstruct->connect = apdu_interface_connect;
+ ifstruct->disconnect = apdu_interface_disconnect;
+ ifstruct->logic_channel_open = apdu_interface_logic_channel_open;
+ ifstruct->logic_channel_close = apdu_interface_logic_channel_close;
+ ifstruct->transmit = apdu_interface_transmit;
+
+ return 0;
+}
+
+static int libapduinterface_main(int argc, char **argv)
+{
+ return 0;
+}
+
+static void libapduinterface_fini(void*)
+{
+}
+
+const struct euicc_driver driver_apdu_uqmi = {
+ .type = DRIVER_APDU,
+ .name = "uqmi",
+ .init = (int (*)(void *))libapduinterface_init,
+ .main = libapduinterface_main,
+ .fini = libapduinterface_fini,
+};
--- /dev/null
+++ b/driver/apdu/uqmi.h
@@ -0,0 +1,7 @@
+// SPDX-License-Identifier: MIT
+/* Copyright (c) 2024 David Bauer <david.bauer@uniberg.com> */
+
+#pragma once
+#include <driver.private.h>
+
+extern const struct euicc_driver driver_apdu_uqmi;
--- a/driver/driver.c
+++ b/driver/driver.c
@@ -26,6 +26,9 @@
#ifdef LPAC_WITH_APDU_AT
#include "driver/apdu/at.h"
#endif
+#ifdef LPAC_WITH_APDU_UQMI
+#include "driver/apdu/uqmi.h"
+#endif
#ifdef LPAC_WITH_HTTP_CURL
#include "driver/http/curl.h"
#endif
@@ -51,6 +54,9 @@ static const struct euicc_driver *driver
#ifdef LPAC_WITH_APDU_AT
&driver_apdu_at,
#endif
+#ifdef LPAC_WITH_APDU_UQMI
+ &driver_apdu_uqmi,
+#endif
#ifdef LPAC_WITH_HTTP_CURL
&driver_http_curl,
#endif
|