@mikrojs/native 0.22.0 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CMakeLists.txt +19 -0
- package/dist/runtime/result/native-result.node-shim.d.ts +2 -0
- package/dist/runtime/result/native-result.node-shim.d.ts.map +1 -1
- package/dist/runtime/result/native-result.node-shim.js +57 -0
- package/dist/runtime/result/native-result.node-shim.js.map +1 -1
- package/dist/runtime/result/types.d.ts +33 -0
- package/dist/runtime/result/types.d.ts.map +1 -1
- package/include/mikrojs/app_store.h +16 -0
- package/include/mikrojs/ble_port.h +50 -0
- package/include/mikrojs/build_install.h +36 -0
- package/include/mikrojs/device_store.h +84 -0
- package/include/mikrojs/http_native.h +52 -0
- package/include/mikrojs/mikrojs.h +19 -9
- package/include/mikrojs/ota.h +75 -0
- package/include/mikrojs/ota_env.h +1 -1
- package/include/mikrojs/platform.h +29 -0
- package/include/mikrojs/private.h +4 -1
- package/include/mikrojs/utils.h +5 -0
- package/package.json +4 -4
- package/prebuilds/darwin-arm64/mikrojs.napi.node +0 -0
- package/prebuilds/linux-arm64/mikrojs.napi.node +0 -0
- package/prebuilds/linux-x64/mikrojs.napi.node +0 -0
- package/runtime/http/server-impl.ts +5 -60
- package/runtime/http/server.ts +0 -1
- package/runtime/internal.d.ts +2 -0
- package/runtime/result/native-result.node-shim.ts +60 -0
- package/runtime/result/result.ts +1 -1
- package/runtime/result/types.ts +61 -0
- package/runtime/sys/sys.ts +1 -2
- package/src/mik_app_store.cpp +32 -0
- package/src/mik_ble.cpp +1573 -0
- package/src/mik_build_install.cpp +785 -0
- package/src/mik_config.cpp +330 -0
- package/src/mik_console.cpp +15 -24
- package/src/mik_deploy.cpp +759 -0
- package/src/mik_ota.cpp +1403 -0
- package/src/mik_ota_client_module.cpp +1142 -0
- package/src/mik_ota_env.cpp +318 -0
- package/src/mik_repl.cpp +12 -4
- package/src/mik_result.cpp +159 -0
- package/src/mik_sys.cpp +25 -51
- package/src/mik_test_supervisor.cpp +151 -0
- package/src/mik_watchdog.cpp +4 -5
- package/src/mik_wifi_client.cpp +1 -2
- package/src/mikrojs.cpp +15 -6
- package/src/modules.cpp +1 -1
|
@@ -0,0 +1,759 @@
|
|
|
1
|
+
#include <errno.h>
|
|
2
|
+
#include <stdio.h>
|
|
3
|
+
#include <stdlib.h>
|
|
4
|
+
#include <string.h>
|
|
5
|
+
#include <sys/stat.h>
|
|
6
|
+
#include <unistd.h>
|
|
7
|
+
|
|
8
|
+
#include "mikrojs/app_store.h"
|
|
9
|
+
#include "mikrojs/device_store.h"
|
|
10
|
+
#include "mikrojs/mikrojs.h"
|
|
11
|
+
#include "mikrojs/platform.h"
|
|
12
|
+
#include "mikrojs/private.h"
|
|
13
|
+
|
|
14
|
+
static const MIKDeviceStore* s_store = nullptr;
|
|
15
|
+
|
|
16
|
+
void MIK_SetDeviceStore(const MIKDeviceStore* store) { s_store = store; }
|
|
17
|
+
|
|
18
|
+
const MIKDeviceStore* mik__device_store(void) { return s_store; }
|
|
19
|
+
|
|
20
|
+
/* Paths under the store's fs_base. They share one heap block, rebuilt when the
|
|
21
|
+
* base changes. */
|
|
22
|
+
static const char* FS_BASE = "";
|
|
23
|
+
static const char* DEPLOY_TMP = "";
|
|
24
|
+
static const char* APP_DIR = "";
|
|
25
|
+
static const char* CHECKSUMS_PATH = "";
|
|
26
|
+
/* Where DEPLOY_PUT("/.build.tgz", ...) streams the OTA build (DEPLOY_TMP +
|
|
27
|
+
* "/.build.tgz"). Staged for boot install by MIK_CMD_DEPLOY_BUILD. */
|
|
28
|
+
static const char* BUILD_TGZ_PATH = "";
|
|
29
|
+
static char* s_paths = nullptr;
|
|
30
|
+
|
|
31
|
+
/* NULL when the paths are ready, else why they are not. */
|
|
32
|
+
static const char* init_paths() {
|
|
33
|
+
const char* base = s_store ? s_store->fs_base : nullptr;
|
|
34
|
+
if (!base) return "no app filesystem on this device";
|
|
35
|
+
if (s_paths && strcmp(FS_BASE, base) == 0) return nullptr;
|
|
36
|
+
|
|
37
|
+
static const char* const suffixes[] = {"", "/.deploy-tmp", "/app", "/app/.checksums",
|
|
38
|
+
"/.deploy-tmp/.build.tgz"};
|
|
39
|
+
const char** const slots[] = {&FS_BASE, &DEPLOY_TMP, &APP_DIR, &CHECKSUMS_PATH,
|
|
40
|
+
&BUILD_TGZ_PATH};
|
|
41
|
+
char* block = mik__join_paths(base, suffixes, sizeof(suffixes) / sizeof(suffixes[0]), slots);
|
|
42
|
+
if (!block) return "out of memory";
|
|
43
|
+
free(s_paths);
|
|
44
|
+
s_paths = block;
|
|
45
|
+
return nullptr;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
static const char* firmware_hash() {
|
|
49
|
+
return s_store && s_store->firmware_hash ? s_store->firmware_hash() : nullptr;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/* ── Checksums manifest ─────────────────────────────────────────── */
|
|
53
|
+
|
|
54
|
+
/*
|
|
55
|
+
* Text format (sha256sum-style), one entry per line:
|
|
56
|
+
* <64-char hex hash> <filename>\n
|
|
57
|
+
* The device appends a "#firmware:<elf_sha256>" line after each deploy.
|
|
58
|
+
* On load, if the firmware hash doesn't match, the manifest is discarded.
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
static constexpr const char* FIRMWARE_PREFIX = "#firmware:";
|
|
62
|
+
static constexpr size_t FIRMWARE_PREFIX_LEN = 10;
|
|
63
|
+
|
|
64
|
+
struct ChecksumsManifest {
|
|
65
|
+
char* data;
|
|
66
|
+
size_t len;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
static ChecksumsManifest load_checksums_manifest() {
|
|
70
|
+
ChecksumsManifest m = {nullptr, 0};
|
|
71
|
+
|
|
72
|
+
FILE* f = fopen(CHECKSUMS_PATH, "r");
|
|
73
|
+
if (!f) return m;
|
|
74
|
+
|
|
75
|
+
fseek(f, 0, SEEK_END);
|
|
76
|
+
long size = ftell(f);
|
|
77
|
+
fseek(f, 0, SEEK_SET);
|
|
78
|
+
|
|
79
|
+
if (size <= 0) {
|
|
80
|
+
fclose(f);
|
|
81
|
+
return m;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
auto* buf = static_cast<char*>(malloc(size + 1));
|
|
85
|
+
if (!buf) {
|
|
86
|
+
fclose(f);
|
|
87
|
+
return m;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
size_t n = fread(buf, 1, size, f);
|
|
91
|
+
fclose(f);
|
|
92
|
+
buf[n] = '\0';
|
|
93
|
+
|
|
94
|
+
/* Check firmware hash — if it doesn't match, discard the manifest */
|
|
95
|
+
const char* fw_hash = firmware_hash();
|
|
96
|
+
bool fw_match = fw_hash == nullptr;
|
|
97
|
+
const char* p = buf;
|
|
98
|
+
const char* end = buf + n;
|
|
99
|
+
while (fw_hash && p < end) {
|
|
100
|
+
const char* nl = static_cast<const char*>(memchr(p, '\n', end - p));
|
|
101
|
+
size_t line_len = nl ? static_cast<size_t>(nl - p) : static_cast<size_t>(end - p);
|
|
102
|
+
if (line_len > FIRMWARE_PREFIX_LEN &&
|
|
103
|
+
memcmp(p, FIRMWARE_PREFIX, FIRMWARE_PREFIX_LEN) == 0) {
|
|
104
|
+
const char* stored = p + FIRMWARE_PREFIX_LEN;
|
|
105
|
+
size_t hash_len = line_len - FIRMWARE_PREFIX_LEN;
|
|
106
|
+
if (hash_len == strlen(fw_hash) && memcmp(stored, fw_hash, hash_len) == 0) {
|
|
107
|
+
fw_match = true;
|
|
108
|
+
}
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
p = nl ? nl + 1 : end;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (!fw_match) {
|
|
115
|
+
free(buf);
|
|
116
|
+
return m;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
m.data = buf;
|
|
120
|
+
m.len = n;
|
|
121
|
+
return m;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Append the current firmware hash to the checksums manifest so it can
|
|
126
|
+
* be validated on next load.
|
|
127
|
+
*/
|
|
128
|
+
static void stamp_checksums_manifest() {
|
|
129
|
+
const char* hash = firmware_hash();
|
|
130
|
+
if (!hash) return;
|
|
131
|
+
FILE* f = fopen(CHECKSUMS_PATH, "a");
|
|
132
|
+
if (!f) return;
|
|
133
|
+
fprintf(f, "%s%s\n", FIRMWARE_PREFIX, hash);
|
|
134
|
+
fclose(f);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
static void hash_to_hex(const uint8_t* hash, char* out) {
|
|
138
|
+
static const char hex[] = "0123456789abcdef";
|
|
139
|
+
for (int i = 0; i < 32; i++) {
|
|
140
|
+
out[i * 2] = hex[hash[i] >> 4];
|
|
141
|
+
out[i * 2 + 1] = hex[hash[i] & 0x0f];
|
|
142
|
+
}
|
|
143
|
+
out[64] = '\0';
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Look up a filename in the text manifest. Returns true if the given
|
|
148
|
+
* 32-byte hash matches the stored hex hash for that filename.
|
|
149
|
+
*/
|
|
150
|
+
static bool manifest_matches(const ChecksumsManifest& m, const char* name, uint16_t name_len,
|
|
151
|
+
const uint8_t* hash) {
|
|
152
|
+
if (!m.data) return false;
|
|
153
|
+
|
|
154
|
+
char hex[65];
|
|
155
|
+
hash_to_hex(hash, hex);
|
|
156
|
+
|
|
157
|
+
/* Scan lines: each is "<64 hex> <filename>\n" */
|
|
158
|
+
const char* p = m.data;
|
|
159
|
+
const char* end = m.data + m.len;
|
|
160
|
+
while (p < end) {
|
|
161
|
+
const char* nl = static_cast<const char*>(memchr(p, '\n', end - p));
|
|
162
|
+
size_t line_len = nl ? static_cast<size_t>(nl - p) : static_cast<size_t>(end - p);
|
|
163
|
+
|
|
164
|
+
/* Line must be at least 64 (hash) + 2 (" ") + 1 (filename) */
|
|
165
|
+
if (line_len >= 67) {
|
|
166
|
+
/* Compare hex hash (first 64 chars) */
|
|
167
|
+
if (memcmp(p, hex, 64) == 0 && p[64] == ' ' && p[65] == ' ') {
|
|
168
|
+
/* Compare filename */
|
|
169
|
+
const char* fname = p + 66;
|
|
170
|
+
size_t fname_len = line_len - 66;
|
|
171
|
+
if (fname_len == name_len && memcmp(fname, name, name_len) == 0) {
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
p = nl ? nl + 1 : end;
|
|
178
|
+
}
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** Number of file entries in the manifest (the "#firmware:" line is not one). */
|
|
183
|
+
static uint16_t manifest_file_count(const ChecksumsManifest& m) {
|
|
184
|
+
if (!m.data) return 0;
|
|
185
|
+
|
|
186
|
+
uint16_t count = 0;
|
|
187
|
+
const char* p = m.data;
|
|
188
|
+
const char* end = m.data + m.len;
|
|
189
|
+
while (p < end) {
|
|
190
|
+
const char* nl = static_cast<const char*>(memchr(p, '\n', end - p));
|
|
191
|
+
size_t line_len = nl ? static_cast<size_t>(nl - p) : static_cast<size_t>(end - p);
|
|
192
|
+
if (line_len >= 67 && p[0] != '#' && count < UINT16_MAX) count++;
|
|
193
|
+
p = nl ? nl + 1 : end;
|
|
194
|
+
}
|
|
195
|
+
return count;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/* ── Filesystem helpers ──────────────────────────────────────────── */
|
|
199
|
+
|
|
200
|
+
/* Deploy handlers run on the main task without returning to MIK_Loop, so
|
|
201
|
+
* loops that touch flash per iteration feed the task watchdog themselves. */
|
|
202
|
+
static void feed_watchdog(void) {
|
|
203
|
+
const MIKPlatform* platform = MIK_GetPlatform();
|
|
204
|
+
if (platform->feed_watchdog) platform->feed_watchdog();
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/* Create the directory that will hold `file_path`. errno is set on failure. */
|
|
208
|
+
static bool make_parent_dir(const char* file_path) {
|
|
209
|
+
char dir_path[512];
|
|
210
|
+
snprintf(dir_path, sizeof(dir_path), "%s", file_path);
|
|
211
|
+
char* last_slash = strrchr(dir_path, '/');
|
|
212
|
+
if (!last_slash || last_slash == dir_path) return true;
|
|
213
|
+
*last_slash = '\0';
|
|
214
|
+
return mik__mkdirs(dir_path);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/* Drop the staging dir. What stays behind holds blocks the next deploy needs. */
|
|
218
|
+
static void discard_staging(void) {
|
|
219
|
+
if (mik__rmdir_recursive(DEPLOY_TMP)) return;
|
|
220
|
+
MIK_GetPlatform()->log(MIK_LOG_WARN, "deploy", "could not remove the staging dir: %s",
|
|
221
|
+
strerror(errno));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/* Buffered bytes reach flash on flush and close, so a full filesystem can
|
|
225
|
+
* first show up here. errno is set on failure. */
|
|
226
|
+
static bool close_file(FILE* f) {
|
|
227
|
+
int err = 0;
|
|
228
|
+
if (fflush(f) != 0) err = errno;
|
|
229
|
+
if (fclose(f) != 0 && err == 0) err = errno;
|
|
230
|
+
if (err == 0) return true;
|
|
231
|
+
errno = err;
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
static void send_errno(MIKReplTransport* transport, const char* step, int err) {
|
|
236
|
+
char msg[128];
|
|
237
|
+
snprintf(msg, sizeof(msg), "%s: %s", step, strerror(err));
|
|
238
|
+
mik__proto_send_err(transport, msg);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
static bool path_exists(const char* path) {
|
|
242
|
+
struct stat st;
|
|
243
|
+
return stat(path, &st) == 0;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/* ── File copy helper ────────────────────────────────────────────── */
|
|
247
|
+
|
|
248
|
+
/* Returns NULL on success, otherwise the step that failed, with errno set. */
|
|
249
|
+
static const char* copy_file(const char* src, const char* dst) {
|
|
250
|
+
FILE* in = fopen(src, "r");
|
|
251
|
+
if (!in) return "open source failed";
|
|
252
|
+
|
|
253
|
+
FILE* out = fopen(dst, "w");
|
|
254
|
+
if (!out) {
|
|
255
|
+
int saved = errno;
|
|
256
|
+
fclose(in);
|
|
257
|
+
errno = saved;
|
|
258
|
+
return "create file failed";
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
uint8_t buf[512];
|
|
262
|
+
size_t n;
|
|
263
|
+
const char* failed = nullptr;
|
|
264
|
+
int saved_errno = 0;
|
|
265
|
+
while ((n = fread(buf, 1, sizeof(buf), in)) > 0) {
|
|
266
|
+
feed_watchdog();
|
|
267
|
+
errno = 0;
|
|
268
|
+
if (fwrite(buf, 1, n, out) != n) {
|
|
269
|
+
saved_errno = errno ? errno : EIO;
|
|
270
|
+
failed = "write failed";
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
if (!failed && ferror(in)) {
|
|
275
|
+
saved_errno = errno;
|
|
276
|
+
failed = "read failed";
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (!close_file(out) && !failed) {
|
|
280
|
+
saved_errno = errno;
|
|
281
|
+
failed = "write failed";
|
|
282
|
+
}
|
|
283
|
+
fclose(in);
|
|
284
|
+
if (failed) errno = saved_errno;
|
|
285
|
+
return failed;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/* Stage a live file for the next commit. Returns NULL on success, otherwise
|
|
289
|
+
* the step that failed, with errno set. */
|
|
290
|
+
static const char* keep_file(const char* name) {
|
|
291
|
+
char src_path[512], dst_path[512];
|
|
292
|
+
snprintf(src_path, sizeof(src_path), "%s%s", FS_BASE, name);
|
|
293
|
+
snprintf(dst_path, sizeof(dst_path), "%s%s", DEPLOY_TMP, name);
|
|
294
|
+
if (!make_parent_dir(dst_path)) return "create directory failed";
|
|
295
|
+
return copy_file(src_path, dst_path);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/* ── Deploy recovery ─────────────────────────────────────────────── */
|
|
299
|
+
|
|
300
|
+
void MIK_DeployRecover(void) {
|
|
301
|
+
if (const char* why = init_paths()) {
|
|
302
|
+
const MIKPlatform* platform = MIK_GetPlatform();
|
|
303
|
+
if (platform && platform->log) {
|
|
304
|
+
platform->log(MIK_LOG_WARN, "deploy", "no deploy recovery: %s", why);
|
|
305
|
+
}
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
mik__app_recover(FS_BASE);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/* ── Unified protocol deploy handler ────────────────────────────── */
|
|
312
|
+
|
|
313
|
+
/* Deploy state — persists across multiple deploy commands within one session.
|
|
314
|
+
* Lazily initialized on the first deploy command. */
|
|
315
|
+
static bool s_deploy_active = false;
|
|
316
|
+
static bool s_deploy_erased = false;
|
|
317
|
+
static bool s_deploy_prev_paused = false;
|
|
318
|
+
static ChecksumsManifest s_deploy_manifest = {nullptr, 0};
|
|
319
|
+
static uint16_t s_deploy_manifest_files = 0;
|
|
320
|
+
|
|
321
|
+
/* In-progress streaming PUT. Set by CMD_DEPLOY_PUT (open file + record size),
|
|
322
|
+
* appended to by CMD_DEPLOY_PUT_CHUNK, cleared when total_remaining hits zero
|
|
323
|
+
* or when the deploy session is reset. f is non-null iff a PUT is currently
|
|
324
|
+
* receiving body bytes. */
|
|
325
|
+
static FILE* s_put_file = nullptr;
|
|
326
|
+
static uint32_t s_put_remaining = 0;
|
|
327
|
+
|
|
328
|
+
/* Returns false, with errno set, when the staged file could not be closed. */
|
|
329
|
+
static bool put_close_and_clear() {
|
|
330
|
+
bool ok = true;
|
|
331
|
+
if (s_put_file) {
|
|
332
|
+
ok = close_file(s_put_file);
|
|
333
|
+
s_put_file = nullptr;
|
|
334
|
+
}
|
|
335
|
+
s_put_remaining = 0;
|
|
336
|
+
return ok;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
static void deploy_ensure_init() {
|
|
340
|
+
if (s_deploy_active) return;
|
|
341
|
+
s_deploy_active = true;
|
|
342
|
+
s_deploy_erased = false;
|
|
343
|
+
/* Freeze user code for the duration of the deploy: timers and loop
|
|
344
|
+
* consumers stop firing between protocol commands so an app crash
|
|
345
|
+
* (or a WiFi/HTTP callback touching the fs) can't race the file
|
|
346
|
+
* swap. Microtasks still drain. Prior state is restored on cleanup
|
|
347
|
+
* so a manual /pause sticks across a deploy. */
|
|
348
|
+
s_deploy_prev_paused = mik__repl_is_paused();
|
|
349
|
+
mik__repl_set_paused(true);
|
|
350
|
+
discard_staging();
|
|
351
|
+
s_deploy_manifest = load_checksums_manifest();
|
|
352
|
+
s_deploy_manifest_files = manifest_file_count(s_deploy_manifest);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
static void deploy_cleanup() {
|
|
356
|
+
put_close_and_clear();
|
|
357
|
+
if (s_deploy_manifest.data) {
|
|
358
|
+
free(s_deploy_manifest.data);
|
|
359
|
+
s_deploy_manifest = {nullptr, 0};
|
|
360
|
+
}
|
|
361
|
+
mik__repl_set_paused(s_deploy_prev_paused);
|
|
362
|
+
s_deploy_active = false;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Reset any in-flight deploy session. Called from the REPL protocol
|
|
367
|
+
* session_end hook when the transport drops without a DONE/ABORT (e.g.
|
|
368
|
+
* CLI killed mid-upload). Discards the staging dir so the next session
|
|
369
|
+
* starts from a clean slate and restores the prior pause state.
|
|
370
|
+
*/
|
|
371
|
+
void MIK_DeploySessionReset(void) {
|
|
372
|
+
if (!s_deploy_active) return;
|
|
373
|
+
put_close_and_clear();
|
|
374
|
+
discard_staging();
|
|
375
|
+
deploy_cleanup();
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Handle a deploy command from the unified protocol loop.
|
|
380
|
+
* The payload bytes have NOT been read yet — this handler reads them
|
|
381
|
+
* directly from the transport, enabling streaming for large files.
|
|
382
|
+
*/
|
|
383
|
+
bool MIK_HandleDeployCommand(MIKReplTransport* transport, uint8_t cmd_type, uint32_t payload_len) {
|
|
384
|
+
if (const char* why = init_paths()) {
|
|
385
|
+
mik__proto_drain(transport, payload_len);
|
|
386
|
+
mik__proto_send_err(transport, why);
|
|
387
|
+
return true;
|
|
388
|
+
}
|
|
389
|
+
deploy_ensure_init();
|
|
390
|
+
|
|
391
|
+
switch (cmd_type) {
|
|
392
|
+
case MIK_CMD_DEPLOY_CHECKSUM: {
|
|
393
|
+
/* Payload: u16le name_len | name | sha256[32] */
|
|
394
|
+
uint8_t hdr[2];
|
|
395
|
+
if (!mik__proto_read_exact(transport, hdr, 2)) return false;
|
|
396
|
+
uint16_t name_len = hdr[0] | (hdr[1] << 8);
|
|
397
|
+
|
|
398
|
+
char name[256];
|
|
399
|
+
if (name_len >= sizeof(name)) {
|
|
400
|
+
mik__proto_drain(transport, payload_len - 2);
|
|
401
|
+
mik__proto_send_err(transport, "checksum filename too long");
|
|
402
|
+
return true;
|
|
403
|
+
}
|
|
404
|
+
if (!mik__proto_read_exact(transport, name, name_len)) return false;
|
|
405
|
+
|
|
406
|
+
uint8_t hash[32];
|
|
407
|
+
if (!mik__proto_read_exact(transport, hash, 32)) return false;
|
|
408
|
+
|
|
409
|
+
bool match = manifest_matches(s_deploy_manifest, name, name_len, hash);
|
|
410
|
+
|
|
411
|
+
/* Verify the file is actually on disk. A stale manifest (e.g. after
|
|
412
|
+
* a partial deploy or FS glitch) would otherwise trick the client
|
|
413
|
+
* into sending KEEP for a file that no longer exists, and KEEP
|
|
414
|
+
* would then fail. Report "no match" so the client re-PUTs it. */
|
|
415
|
+
if (match) {
|
|
416
|
+
char terminated[257];
|
|
417
|
+
memcpy(terminated, name, name_len);
|
|
418
|
+
terminated[name_len] = '\0';
|
|
419
|
+
char src_path[512];
|
|
420
|
+
snprintf(src_path, sizeof(src_path), "%s%s", FS_BASE, terminated);
|
|
421
|
+
if (!path_exists(src_path)) match = false;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/* Payload: u8 match | u16le manifest file count. The count lets the
|
|
425
|
+
* client see files this device holds that the build no longer has. */
|
|
426
|
+
uint8_t result[3] = {static_cast<uint8_t>(match ? 0x01 : 0x00),
|
|
427
|
+
static_cast<uint8_t>(s_deploy_manifest_files & 0xff),
|
|
428
|
+
static_cast<uint8_t>(s_deploy_manifest_files >> 8)};
|
|
429
|
+
mik__proto_send(transport, MIK_MSG_CHECKSUM_RESULT, result, sizeof(result));
|
|
430
|
+
return true;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
case MIK_CMD_DEPLOY_ERASE:
|
|
434
|
+
mik__proto_drain(transport, payload_len);
|
|
435
|
+
if (!mik__rmdir_recursive(APP_DIR)) {
|
|
436
|
+
send_errno(transport, "remove app failed", errno);
|
|
437
|
+
return true;
|
|
438
|
+
}
|
|
439
|
+
s_deploy_erased = true;
|
|
440
|
+
mik__proto_send_ok(transport);
|
|
441
|
+
return true;
|
|
442
|
+
|
|
443
|
+
case MIK_CMD_DEPLOY_KEEP: {
|
|
444
|
+
/* Payload: u16le name_len | name */
|
|
445
|
+
uint8_t nl[2];
|
|
446
|
+
if (!mik__proto_read_exact(transport, nl, 2)) return false;
|
|
447
|
+
uint16_t name_len = nl[0] | (nl[1] << 8);
|
|
448
|
+
|
|
449
|
+
char name[256];
|
|
450
|
+
if (name_len >= sizeof(name)) {
|
|
451
|
+
mik__proto_drain(transport, name_len);
|
|
452
|
+
mik__proto_send_err(transport, "keep filename too long");
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
if (!mik__proto_read_exact(transport, name, name_len)) return false;
|
|
456
|
+
name[name_len] = '\0';
|
|
457
|
+
|
|
458
|
+
const char* failed = keep_file(name);
|
|
459
|
+
if (failed) {
|
|
460
|
+
send_errno(transport, failed, errno);
|
|
461
|
+
} else {
|
|
462
|
+
mik__proto_send_ok(transport);
|
|
463
|
+
}
|
|
464
|
+
return true;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
case MIK_CMD_DEPLOY_KEEP_MANY: {
|
|
468
|
+
/* Payload: repeated u16le name_len | name. Each file is staged as
|
|
469
|
+
* KEEP stages it. The first failure ends the batch: the rest of
|
|
470
|
+
* the payload is dropped and the error names the file. */
|
|
471
|
+
uint32_t consumed = 0;
|
|
472
|
+
while (consumed < payload_len) {
|
|
473
|
+
if (payload_len - consumed < 2) {
|
|
474
|
+
mik__proto_drain(transport, payload_len - consumed);
|
|
475
|
+
mik__proto_send_err(transport, "keep list truncated");
|
|
476
|
+
return true;
|
|
477
|
+
}
|
|
478
|
+
uint8_t nl[2];
|
|
479
|
+
if (!mik__proto_read_exact(transport, nl, 2)) return false;
|
|
480
|
+
consumed += 2;
|
|
481
|
+
uint16_t name_len = nl[0] | (nl[1] << 8);
|
|
482
|
+
|
|
483
|
+
char name[256];
|
|
484
|
+
if (name_len >= sizeof(name) || name_len > payload_len - consumed) {
|
|
485
|
+
mik__proto_drain(transport, payload_len - consumed);
|
|
486
|
+
mik__proto_send_err(transport, "keep filename too long");
|
|
487
|
+
return true;
|
|
488
|
+
}
|
|
489
|
+
if (!mik__proto_read_exact(transport, name, name_len)) return false;
|
|
490
|
+
consumed += name_len;
|
|
491
|
+
name[name_len] = '\0';
|
|
492
|
+
|
|
493
|
+
const char* failed = keep_file(name);
|
|
494
|
+
if (failed) {
|
|
495
|
+
int err = errno;
|
|
496
|
+
mik__proto_drain(transport, payload_len - consumed);
|
|
497
|
+
char msg[384];
|
|
498
|
+
snprintf(msg, sizeof(msg), "%s: %s: %s", name, failed, strerror(err));
|
|
499
|
+
mik__proto_send_err(transport, msg);
|
|
500
|
+
return true;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
mik__proto_send_ok(transport);
|
|
504
|
+
return true;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
case MIK_CMD_DEPLOY_CHECKSUM_LIST: {
|
|
508
|
+
/* Reply: u16le file_count | the manifest lines whose file is on
|
|
509
|
+
* disk, so the host never KEEPs a file a stale manifest still
|
|
510
|
+
* names (see CHECKSUM). file_count counts every manifest entry. */
|
|
511
|
+
mik__proto_drain(transport, payload_len);
|
|
512
|
+
const ChecksumsManifest& m = s_deploy_manifest;
|
|
513
|
+
auto* reply = static_cast<uint8_t*>(malloc(2 + m.len));
|
|
514
|
+
if (!reply) {
|
|
515
|
+
mik__proto_send_err(transport, "out of memory");
|
|
516
|
+
return true;
|
|
517
|
+
}
|
|
518
|
+
reply[0] = static_cast<uint8_t>(s_deploy_manifest_files & 0xff);
|
|
519
|
+
reply[1] = static_cast<uint8_t>(s_deploy_manifest_files >> 8);
|
|
520
|
+
size_t out = 2;
|
|
521
|
+
if (m.data) {
|
|
522
|
+
const char* p = m.data;
|
|
523
|
+
const char* end = m.data + m.len;
|
|
524
|
+
while (p < end) {
|
|
525
|
+
const char* nl = static_cast<const char*>(memchr(p, '\n', end - p));
|
|
526
|
+
const char* next = nl ? nl + 1 : end;
|
|
527
|
+
size_t entry_len = static_cast<size_t>((nl ? nl : end) - p);
|
|
528
|
+
/* "<64 hex> <name>", the name no longer than KEEP accepts. */
|
|
529
|
+
if (entry_len >= 67 && entry_len < 66 + 256 && p[0] != '#') {
|
|
530
|
+
char path[512];
|
|
531
|
+
snprintf(path, sizeof(path), "%s%.*s", FS_BASE,
|
|
532
|
+
static_cast<int>(entry_len - 66), p + 66);
|
|
533
|
+
feed_watchdog();
|
|
534
|
+
if (path_exists(path)) {
|
|
535
|
+
size_t line_len = static_cast<size_t>(next - p);
|
|
536
|
+
memcpy(reply + out, p, line_len);
|
|
537
|
+
out += line_len;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
p = next;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
mik__proto_send(transport, MIK_MSG_OK, reply, out);
|
|
544
|
+
free(reply);
|
|
545
|
+
return true;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
case MIK_CMD_DEPLOY_PUT: {
|
|
549
|
+
/* Begin a streaming PUT.
|
|
550
|
+
* Payload: u16le name_len | name | u32le total_size
|
|
551
|
+
* Opens the staged file, records the expected body size, and
|
|
552
|
+
* replies OK. The body itself arrives in subsequent
|
|
553
|
+
* CMD_DEPLOY_PUT_CHUNK frames. */
|
|
554
|
+
put_close_and_clear();
|
|
555
|
+
|
|
556
|
+
if (payload_len < 2 + 4) {
|
|
557
|
+
mik__proto_drain(transport, payload_len);
|
|
558
|
+
mik__proto_send_err(transport, "put header too short");
|
|
559
|
+
return true;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
uint8_t nl[2];
|
|
563
|
+
if (!mik__proto_read_exact(transport, nl, 2)) return false;
|
|
564
|
+
uint16_t name_len = nl[0] | (nl[1] << 8);
|
|
565
|
+
|
|
566
|
+
if (name_len >= 256 || (uint32_t)name_len + 6 > payload_len) {
|
|
567
|
+
mik__proto_drain(transport, payload_len - 2);
|
|
568
|
+
mik__proto_send_err(transport, "put filename too long");
|
|
569
|
+
return true;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
char name[256];
|
|
573
|
+
if (!mik__proto_read_exact(transport, name, name_len)) return false;
|
|
574
|
+
name[name_len] = '\0';
|
|
575
|
+
|
|
576
|
+
uint8_t sz[4];
|
|
577
|
+
if (!mik__proto_read_exact(transport, sz, 4)) return false;
|
|
578
|
+
uint32_t total_size = (uint32_t)sz[0] | ((uint32_t)sz[1] << 8) |
|
|
579
|
+
((uint32_t)sz[2] << 16) | ((uint32_t)sz[3] << 24);
|
|
580
|
+
|
|
581
|
+
/* Drain any trailing bytes the host accidentally tacked on
|
|
582
|
+
* (defensive — the wire format is fixed-size after the name). */
|
|
583
|
+
uint32_t consumed = 2 + name_len + 4;
|
|
584
|
+
if (payload_len > consumed) {
|
|
585
|
+
mik__proto_drain(transport, payload_len - consumed);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
char full_path[512];
|
|
589
|
+
snprintf(full_path, sizeof(full_path), "%s%s", DEPLOY_TMP, name);
|
|
590
|
+
|
|
591
|
+
if (!make_parent_dir(full_path)) {
|
|
592
|
+
send_errno(transport, "create directory failed", errno);
|
|
593
|
+
return true;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
FILE* f = fopen(full_path, "w");
|
|
597
|
+
if (!f) {
|
|
598
|
+
send_errno(transport, "create file failed", errno);
|
|
599
|
+
return true;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
if (total_size == 0) {
|
|
603
|
+
/* Empty file: nothing to chunk, close now. */
|
|
604
|
+
if (!close_file(f)) {
|
|
605
|
+
send_errno(transport, "write failed", errno);
|
|
606
|
+
return true;
|
|
607
|
+
}
|
|
608
|
+
} else {
|
|
609
|
+
s_put_file = f;
|
|
610
|
+
s_put_remaining = total_size;
|
|
611
|
+
}
|
|
612
|
+
mik__proto_send_ok(transport);
|
|
613
|
+
return true;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
case MIK_CMD_DEPLOY_PUT_CHUNK: {
|
|
617
|
+
/* Append payload bytes to the file opened by the preceding PUT.
|
|
618
|
+
* Streams in a small fixed buffer — never holds the chunk in
|
|
619
|
+
* full, regardless of chunk size. Closes the file and clears
|
|
620
|
+
* state when total_remaining reaches zero. */
|
|
621
|
+
if (!s_put_file) {
|
|
622
|
+
mik__proto_drain(transport, payload_len);
|
|
623
|
+
mik__proto_send_err(transport, "put chunk without active put");
|
|
624
|
+
return true;
|
|
625
|
+
}
|
|
626
|
+
if (payload_len > s_put_remaining) {
|
|
627
|
+
mik__proto_drain(transport, payload_len);
|
|
628
|
+
put_close_and_clear();
|
|
629
|
+
mik__proto_send_err(transport, "put chunk overruns declared size");
|
|
630
|
+
return true;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
uint8_t buf[512];
|
|
634
|
+
uint32_t remaining = payload_len;
|
|
635
|
+
int write_errno = 0;
|
|
636
|
+
while (remaining > 0) {
|
|
637
|
+
uint32_t chunk = remaining > sizeof(buf) ? sizeof(buf) : remaining;
|
|
638
|
+
if (!mik__proto_read_exact(transport, buf, chunk)) return false;
|
|
639
|
+
feed_watchdog();
|
|
640
|
+
/* The transport read above may have left its own errno. */
|
|
641
|
+
errno = 0;
|
|
642
|
+
if (fwrite(buf, 1, chunk, s_put_file) != chunk) {
|
|
643
|
+
/* Write failure: keep the protocol in sync by draining
|
|
644
|
+
* the rest of this chunk's bytes, then abort the PUT. */
|
|
645
|
+
write_errno = errno ? errno : EIO;
|
|
646
|
+
remaining -= chunk;
|
|
647
|
+
if (remaining > 0) mik__proto_drain(transport, remaining);
|
|
648
|
+
break;
|
|
649
|
+
}
|
|
650
|
+
remaining -= chunk;
|
|
651
|
+
s_put_remaining -= chunk;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
if (write_errno) {
|
|
655
|
+
put_close_and_clear();
|
|
656
|
+
send_errno(transport, "write failed", write_errno);
|
|
657
|
+
return true;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
if (s_put_remaining == 0 && !put_close_and_clear()) {
|
|
661
|
+
send_errno(transport, "write failed", errno);
|
|
662
|
+
return true;
|
|
663
|
+
}
|
|
664
|
+
mik__proto_send_ok(transport);
|
|
665
|
+
return true;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
case MIK_CMD_DEPLOY_DONE: {
|
|
669
|
+
mik__proto_drain(transport, payload_len);
|
|
670
|
+
|
|
671
|
+
/* Guard against a CLI bug where DONE arrives mid-stream: the
|
|
672
|
+
* half-written file would otherwise be promoted into APP_DIR.
|
|
673
|
+
* Drop the in-flight file, nuke staging, and surface the error
|
|
674
|
+
* to the host so the caller sees a real failure. */
|
|
675
|
+
if (s_put_file) {
|
|
676
|
+
put_close_and_clear();
|
|
677
|
+
discard_staging();
|
|
678
|
+
mik__proto_send_err(transport, "deploy done while put in progress");
|
|
679
|
+
deploy_cleanup();
|
|
680
|
+
return true;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/* Atomic swap: staging dir → app dir */
|
|
684
|
+
MIKAppCommitResult commit = mik__app_commit(FS_BASE, s_deploy_erased);
|
|
685
|
+
if (commit == MIK_APP_COMMIT_STASH_FAILED) {
|
|
686
|
+
mik__proto_send_err(transport, "rename app to old failed");
|
|
687
|
+
deploy_cleanup();
|
|
688
|
+
return true;
|
|
689
|
+
}
|
|
690
|
+
if (commit == MIK_APP_COMMIT_SWAP_FAILED) {
|
|
691
|
+
mik__proto_send_err(transport, "rename staged to app failed");
|
|
692
|
+
deploy_cleanup();
|
|
693
|
+
return true;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
stamp_checksums_manifest();
|
|
697
|
+
mik__proto_send_ok(transport);
|
|
698
|
+
deploy_cleanup();
|
|
699
|
+
return true;
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
case MIK_CMD_DEPLOY_BUILD: {
|
|
703
|
+
/* Stage a streamed .tgz for install at the next boot. The install
|
|
704
|
+
* runs from the boot reconcile on a clean heap; installing here
|
|
705
|
+
* with the app live needs a 32 KB contiguous inflate window a
|
|
706
|
+
* fragmented heap cannot provide. The checksum is verified now so
|
|
707
|
+
* a corrupt upload still fails synchronously. The build was
|
|
708
|
+
* streamed via PUT("/.build.tgz") + chunks into BUILD_TGZ_PATH.
|
|
709
|
+
* Payload: u16le checksum_len | checksum. */
|
|
710
|
+
if (payload_len < 2) {
|
|
711
|
+
mik__proto_drain(transport, payload_len);
|
|
712
|
+
mik__proto_send_err(transport, "build header too short");
|
|
713
|
+
deploy_cleanup();
|
|
714
|
+
return true;
|
|
715
|
+
}
|
|
716
|
+
uint8_t cl[2];
|
|
717
|
+
if (!mik__proto_read_exact(transport, cl, 2)) return false;
|
|
718
|
+
uint16_t chk_len = cl[0] | (cl[1] << 8);
|
|
719
|
+
|
|
720
|
+
char checksum[96];
|
|
721
|
+
if (chk_len >= sizeof(checksum) || (uint32_t)chk_len + 2 > payload_len) {
|
|
722
|
+
mik__proto_drain(transport, payload_len - 2);
|
|
723
|
+
mik__proto_send_err(transport, "build checksum too long");
|
|
724
|
+
deploy_cleanup();
|
|
725
|
+
return true;
|
|
726
|
+
}
|
|
727
|
+
if (!mik__proto_read_exact(transport, checksum, chk_len)) return false;
|
|
728
|
+
checksum[chk_len] = '\0';
|
|
729
|
+
uint32_t consumed = 2 + (uint32_t)chk_len;
|
|
730
|
+
if (payload_len > consumed) mik__proto_drain(transport, payload_len - consumed);
|
|
731
|
+
|
|
732
|
+
/* Flush the staged .tgz to disk before staging verifies it. */
|
|
733
|
+
put_close_and_clear();
|
|
734
|
+
|
|
735
|
+
const char* err = nullptr;
|
|
736
|
+
if (!s_store || !s_store->stage_build) {
|
|
737
|
+
mik__proto_send_err(transport, "builds are not supported on this device");
|
|
738
|
+
} else if (s_store->stage_build(BUILD_TGZ_PATH, checksum, &err)) {
|
|
739
|
+
mik__proto_send_ok(transport);
|
|
740
|
+
} else {
|
|
741
|
+
mik__proto_send_err(transport, err ? err : "build stage failed");
|
|
742
|
+
}
|
|
743
|
+
deploy_cleanup();
|
|
744
|
+
return true;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
case MIK_CMD_DEPLOY_ABORT:
|
|
748
|
+
mik__proto_drain(transport, payload_len);
|
|
749
|
+
put_close_and_clear();
|
|
750
|
+
discard_staging();
|
|
751
|
+
mik__proto_send_ok(transport);
|
|
752
|
+
deploy_cleanup();
|
|
753
|
+
return true;
|
|
754
|
+
|
|
755
|
+
default:
|
|
756
|
+
mik__proto_drain(transport, payload_len);
|
|
757
|
+
return false;
|
|
758
|
+
}
|
|
759
|
+
}
|