fix little endian vs big endian in the macros... again... but this time correct
[RRG-proxmark3.git] / client / src / cmdflashmemspiffs.c
bloba3695e658b569d53c4385e9966f6107c7fe1babd
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2018 iceman
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // Proxmark3 RDV40 Flash memory commands
9 //-----------------------------------------------------------------------------
10 #include "cmdflashmemspiffs.h"
11 #include <ctype.h>
12 #include "cmdparser.h" // command_t
13 #include "pmflash.h"
14 #include "fileutils.h" //saveFile
15 #include "comms.h" //getfromdevice
16 #include "cliparser.h"
18 static int CmdHelp(const char *Cmd);
20 int flashmem_spiffs_load(char *destfn, uint8_t *data, size_t datalen) {
22 int ret_val = PM3_SUCCESS;
24 // We want to mount before multiple operation so the lazy writes/append will not
25 // trigger a mount + umount each loop iteration (lazy ops device side)
26 SendCommandNG(CMD_SPIFFS_MOUNT, NULL, 0);
28 // Send to device
29 uint32_t bytes_sent = 0;
30 uint32_t bytes_remaining = datalen;
32 // fast push mode
33 conn.block_after_ACK = true;
35 while (bytes_remaining > 0) {
37 uint32_t bytes_in_packet = MIN(FLASH_MEM_BLOCK_SIZE, bytes_remaining);
39 flashmem_write_t *payload = calloc(1, sizeof(flashmem_write_t) + bytes_in_packet);
41 payload->append = (bytes_sent > 0);
43 uint8_t fnlen = MIN(sizeof(payload->fn), strlen(destfn));
45 payload->fnlen = fnlen;
46 memcpy(payload->fn, destfn, fnlen);
48 payload->bytes_in_packet = bytes_in_packet;
49 memset(payload->data, 0, bytes_in_packet);
50 memcpy(payload->data, data + bytes_sent, bytes_in_packet);
52 PacketResponseNG resp;
53 clearCommandBuffer();
54 SendCommandNG(CMD_SPIFFS_WRITE, (uint8_t *)payload, sizeof(flashmem_write_t) + bytes_in_packet);
56 free(payload);
58 bytes_remaining -= bytes_in_packet;
59 bytes_sent += bytes_in_packet;
61 uint8_t retry = 3;
62 while (WaitForResponseTimeout(CMD_SPIFFS_WRITE, &resp, 2000) == false) {
63 PrintAndLogEx(WARNING, "timeout while waiting for reply.");
64 retry--;
65 if (retry == 0) {
66 ret_val = PM3_ETIMEOUT;
67 goto out;
72 out:
73 clearCommandBuffer();
75 // turn off fast push mode
76 conn.block_after_ACK = false;
78 // We want to unmount after these to set things back to normal but more than this
79 // unmouting ensure that SPIFFS CACHES are all flushed so our file is actually written on memory
80 SendCommandNG(CMD_SPIFFS_UNMOUNT, NULL, 0);
81 return ret_val;
84 static int CmdFlashMemSpiFFSMount(const char *Cmd) {
85 CLIParserContext *ctx;
86 CLIParserInit(&ctx, "mem spiffs mount",
87 "Mount the SPIFFS file system if not already mounted",
88 "mem spiffs mount");
90 void *argtable[] = {
91 arg_param_begin,
92 arg_param_end
94 CLIExecWithReturn(ctx, Cmd, argtable, true);
95 CLIParserFree(ctx);
96 clearCommandBuffer();
97 SendCommandNG(CMD_SPIFFS_MOUNT, NULL, 0);
98 return PM3_SUCCESS;
101 static int CmdFlashMemSpiFFSUnmount(const char *Cmd) {
102 CLIParserContext *ctx;
103 CLIParserInit(&ctx, "mem spiffs unmount",
104 "Un-mount the SPIFFS file system",
105 "mem spiffs unmount");
107 void *argtable[] = {
108 arg_param_begin,
109 arg_param_end
111 CLIExecWithReturn(ctx, Cmd, argtable, true);
112 CLIParserFree(ctx);
113 clearCommandBuffer();
114 SendCommandNG(CMD_SPIFFS_UNMOUNT, NULL, 0);
115 return PM3_SUCCESS;
118 static int CmdFlashMemSpiFFSTest(const char *Cmd) {
119 CLIParserContext *ctx;
120 CLIParserInit(&ctx, "mem spiffs test",
121 "Test SPIFFS Operations, require wiping pages 0 and 1",
122 "mem spiffs test");
124 void *argtable[] = {
125 arg_param_begin,
126 arg_param_end
128 CLIExecWithReturn(ctx, Cmd, argtable, true);
129 CLIParserFree(ctx);
130 clearCommandBuffer();
131 SendCommandNG(CMD_SPIFFS_TEST, NULL, 0);
132 return PM3_SUCCESS;
135 static int CmdFlashMemSpiFFSCheck(const char *Cmd) {
136 CLIParserContext *ctx;
137 CLIParserInit(&ctx, "mem spiffs check",
138 "Check/try to defrag faulty/fragmented SPIFFS file system",
139 "mem spiffs check");
141 void *argtable[] = {
142 arg_param_begin,
143 arg_param_end
145 CLIExecWithReturn(ctx, Cmd, argtable, true);
146 CLIParserFree(ctx);
147 clearCommandBuffer();
148 SendCommandNG(CMD_SPIFFS_CHECK, NULL, 0);
149 return PM3_SUCCESS;
152 static int CmdFlashMemSpiFFSTree(const char *Cmd) {
153 CLIParserContext *ctx;
154 CLIParserInit(&ctx, "mem spiffs tree",
155 "Print the Flash memory file system tree",
156 "mem spiffs tree");
158 void *argtable[] = {
159 arg_param_begin,
160 arg_param_end
162 CLIExecWithReturn(ctx, Cmd, argtable, true);
163 CLIParserFree(ctx);
165 PrintAndLogEx(INFO, "--- " _CYAN_("Flash Memory tree (SPIFFS)") " -----------------");
166 clearCommandBuffer();
167 SendCommandNG(CMD_SPIFFS_PRINT_TREE, NULL, 0);
168 return PM3_SUCCESS;
171 static int CmdFlashMemSpiFFSInfo(const char *Cmd) {
172 CLIParserContext *ctx;
173 CLIParserInit(&ctx, "mem spiffs info",
174 "Print file system info and usage statistics",
175 "mem spiffs info");
177 void *argtable[] = {
178 arg_param_begin,
179 arg_param_end
181 CLIExecWithReturn(ctx, Cmd, argtable, true);
182 CLIParserFree(ctx);
184 PrintAndLogEx(INFO, "--- " _CYAN_("Flash Memory info (SPIFFS)") " -----------------");
185 clearCommandBuffer();
186 SendCommandNG(CMD_SPIFFS_PRINT_FSINFO, NULL, 0);
187 return PM3_SUCCESS;
190 static int CmdFlashMemSpiFFSRemove(const char *Cmd) {
191 CLIParserContext *ctx;
192 CLIParserInit(&ctx, "mem spiffs remove",
193 "Remove a file from SPIFFS filesystem",
194 "mem spiffs remove -f lasttag.bin"
197 void *argtable[] = {
198 arg_param_begin,
199 arg_str1("f", "filename", "<fn>", "file to remove"),
200 arg_param_end
202 CLIExecWithReturn(ctx, Cmd, argtable, false);
204 int fnlen = 0;
205 char filename[32] = {0};
206 CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)filename, 32, &fnlen);
207 CLIParserFree(ctx);
209 PrintAndLogEx(DEBUG, "Removing `" _YELLOW_("%s") "`", filename);
210 struct {
211 uint8_t len;
212 uint8_t fn[32];
213 } PACKED payload;
214 payload.len = fnlen;
215 memcpy(payload.fn, filename, fnlen);
217 PacketResponseNG resp;
218 clearCommandBuffer();
219 SendCommandNG(CMD_SPIFFS_REMOVE, (uint8_t *)&payload, sizeof(payload));
220 WaitForResponse(CMD_SPIFFS_REMOVE, &resp);
221 if (resp.status == PM3_SUCCESS)
222 PrintAndLogEx(INFO, "Done!");
224 return PM3_SUCCESS;
227 static int CmdFlashMemSpiFFSRename(const char *Cmd) {
228 CLIParserContext *ctx;
229 CLIParserInit(&ctx, "mem spiffs rename",
230 "Rename/move a file from SPIFFS filesystem.",
231 "mem spiffs rename -s aaa.bin -d bbb.bin"
234 void *argtable[] = {
235 arg_param_begin,
236 arg_str1("s", "src", "<fn>", "source file name"),
237 arg_str1("d", "dest", "<fn>", "destination file name"),
238 arg_param_end
240 CLIExecWithReturn(ctx, Cmd, argtable, false);
242 int slen = 0;
243 char src[32] = {0};
244 CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)src, 32, &slen);
246 int dlen = 0;
247 char dest[32] = {0};
248 CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t *)dest, 32, &dlen);
249 CLIParserFree(ctx);
251 PrintAndLogEx(DEBUG, "Rename from `" _YELLOW_("%s") "` -> `" _YELLOW_("%s") "`", src, dest);
253 struct {
254 uint8_t slen;
255 uint8_t src[32];
256 uint8_t dlen;
257 uint8_t dest[32];
258 } PACKED payload;
259 payload.slen = slen;
260 payload.dlen = dlen;
262 memcpy(payload.src, src, slen);
263 memcpy(payload.dest, dest, dlen);
265 PacketResponseNG resp;
266 clearCommandBuffer();
267 SendCommandNG(CMD_SPIFFS_RENAME, (uint8_t *)&payload, sizeof(payload));
268 WaitForResponse(CMD_SPIFFS_RENAME, &resp);
269 if (resp.status == PM3_SUCCESS)
270 PrintAndLogEx(INFO, "Done!");
272 PrintAndLogEx(HINT, "Try `" _YELLOW_("mem spiffs tree") "` to verify");
273 return PM3_SUCCESS;
276 static int CmdFlashMemSpiFFSCopy(const char *Cmd) {
278 CLIParserContext *ctx;
279 CLIParserInit(&ctx, "mem spiffs copy",
280 "Copy a file to another (destructively) in SPIFFS file system",
281 "mem spiffs copy -s aaa.bin -d aaa_cpy.bin"
284 void *argtable[] = {
285 arg_param_begin,
286 arg_str1("s", "src", "<fn>", "source file name"),
287 arg_str1("d", "dest", "<fn>", "destination file name"),
288 arg_param_end
290 CLIExecWithReturn(ctx, Cmd, argtable, false);
292 int slen = 0;
293 char src[32] = {0};
294 CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)src, 32, &slen);
296 int dlen = 0;
297 char dest[32] = {0};
298 CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t *)dest, 32, &dlen);
299 CLIParserFree(ctx);
301 struct {
302 uint8_t slen;
303 uint8_t src[32];
304 uint8_t dlen;
305 uint8_t dest[32];
306 } PACKED payload;
307 payload.slen = slen;
308 payload.dlen = dlen;
310 memcpy(payload.src, src, slen);
311 memcpy(payload.dest, dest, dlen);
313 PacketResponseNG resp;
314 clearCommandBuffer();
315 SendCommandNG(CMD_SPIFFS_COPY, (uint8_t *)&payload, sizeof(payload));
316 WaitForResponse(CMD_SPIFFS_COPY, &resp);
317 if (resp.status == PM3_SUCCESS)
318 PrintAndLogEx(INFO, "Done!");
320 PrintAndLogEx(HINT, "Try `" _YELLOW_("mem spiffs tree") "` to verify");
321 return PM3_SUCCESS;
324 static int CmdFlashMemSpiFFSDump(const char *Cmd) {
325 CLIParserContext *ctx;
326 CLIParserInit(&ctx, "mem spiffs dump",
327 "Dumps device SPIFFS file to a local file\n"
328 "Size is handled by first sending a STAT command against file to verify existence",
329 "mem spiffs dump -s tag.bin --> download binary file from device\n"
330 "mem spiffs dump -s tag.bin -d aaa -e --> download tag.bin, save as aaa.eml format"
333 void *argtable[] = {
334 arg_param_begin,
335 arg_str1("s", "src", "<fn>", "SPIFFS file to save"),
336 arg_str0("d", "dest", "<fn>", "file name to save to <w/o .bin>"),
337 arg_lit0("e", "eml", "also save in EML format"),
338 arg_param_end
340 CLIExecWithReturn(ctx, Cmd, argtable, false);
342 int slen = 0;
343 char src[32] = {0};
344 CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)src, 32, &slen);
346 int dlen = 0;
347 char dest[FILE_PATH_SIZE] = {0};
348 CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t *)dest, FILE_PATH_SIZE, &dlen);
350 bool eml = arg_get_lit(ctx, 3);
351 CLIParserFree(ctx);
353 // get size from spiffs itself !
354 clearCommandBuffer();
355 SendCommandNG(CMD_SPIFFS_STAT, (uint8_t *)src, slen);
356 PacketResponseNG resp;
357 if (WaitForResponseTimeout(CMD_SPIFFS_STAT, &resp, 2000) == false) {
358 PrintAndLogEx(WARNING, "timeout while waiting for reply.");
359 return PM3_ETIMEOUT;
362 uint32_t len = resp.data.asDwords[0];
363 uint8_t *dump = calloc(len, sizeof(uint8_t));
364 if (!dump) {
365 PrintAndLogEx(ERR, "error, cannot allocate memory ");
366 return PM3_EMALLOC;
369 // download from device
370 uint32_t start_index = 0;
371 PrintAndLogEx(INFO, "downloading "_YELLOW_("%u") " bytes from `" _YELLOW_("%s") "` (spiffs)", len, src);
372 if (!GetFromDevice(SPIFFS, dump, len, start_index, (uint8_t *)src, slen, NULL, -1, true)) {
373 PrintAndLogEx(FAILED, "error, downloading from spiffs");
374 free(dump);
375 return PM3_EFLASH;
378 // save to file
379 char fn[FILE_PATH_SIZE] = {0};
380 if (dlen == 0) {
381 strncpy(fn, src, slen);
382 } else {
383 strncpy(fn, dest, dlen);
386 saveFile(fn, ".bin", dump, len);
387 if (eml) {
388 uint8_t eml_len = 16;
389 if (strstr(fn, "class") != NULL)
390 eml_len = 8;
391 else if (strstr(fn, "mfu") != NULL)
392 eml_len = 4;
394 saveFileEML(fn, dump, len, eml_len);
396 free(dump);
397 return PM3_SUCCESS;
400 static int CmdFlashMemSpiFFSWipe(const char *Cmd) {
401 CLIParserContext *ctx;
402 CLIParserInit(&ctx, "mem spiffs wipe",
403 _RED_("* * * Warning * * *") " \n"
404 _CYAN_("This command wipes all files on the device SPIFFS file system"),
405 "mem spiffs wipe");
407 void *argtable[] = {
408 arg_param_begin,
409 arg_param_end
411 CLIExecWithReturn(ctx, Cmd, argtable, true);
412 CLIParserFree(ctx);
414 PrintAndLogEx(INFO, "Wiping all files from SPIFFS file system");
415 PacketResponseNG resp;
416 clearCommandBuffer();
417 SendCommandNG(CMD_SPIFFS_WIPE, NULL, 0);
418 WaitForResponse(CMD_SPIFFS_WIPE, &resp);
419 if (resp.status == PM3_SUCCESS)
420 PrintAndLogEx(INFO, "Done!");
422 PrintAndLogEx(HINT, "Try `" _YELLOW_("mem spiffs tree") "` to verify");
423 return PM3_SUCCESS;
426 static int CmdFlashMemSpiFFSUpload(const char *Cmd) {
427 CLIParserContext *ctx;
428 CLIParserInit(&ctx, "mem spiffs upload",
429 "Uploads binary-wise file into device file system\n"
430 "Warning: mem area to be written must have been wiped first.\n"
431 "This is already taken care when loading dictionaries.\n"
432 "File names can only be 32 bytes long on device SPIFFS",
433 "mem spiffs upload -s local.bin -d dest.bin"
436 void *argtable[] = {
437 arg_param_begin,
438 arg_str1("s", "src", "<fn>", "source file name"),
439 arg_str1("d", "dest", "<fn>", "destination file name"),
440 arg_param_end
442 CLIExecWithReturn(ctx, Cmd, argtable, false);
444 int slen = 0;
445 char src[FILE_PATH_SIZE] = {0};
446 CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)src, FILE_PATH_SIZE, &slen);
448 int dlen = 0;
449 char dest[32] = {0};
450 CLIParamStrToBuf(arg_get_str(ctx, 2), (uint8_t *)dest, 32, &dlen);
451 CLIParserFree(ctx);
453 PrintAndLogEx(DEBUG, "Upload `" _YELLOW_("%s") "` -> `" _YELLOW_("%s") "`", src, dest);
455 size_t datalen = 0;
456 uint8_t *data = NULL;
458 int res = loadFile_safe(src, "", (void **)&data, &datalen);
459 if (res != PM3_SUCCESS) {
460 free(data);
461 return PM3_EFILE;
464 res = flashmem_spiffs_load(dest, data, datalen);
465 free(data);
467 if (res == PM3_SUCCESS)
468 PrintAndLogEx(SUCCESS, "Wrote "_GREEN_("%zu") " bytes to file "_GREEN_("%s"), datalen, dest);
470 PrintAndLogEx(HINT, "Try `" _YELLOW_("mem spiffs tree") "` to verify");
471 return res;
474 static int CmdFlashMemSpiFFSView(const char *Cmd) {
476 CLIParserContext *ctx;
477 CLIParserInit(&ctx, "mem spiffs view",
478 "View a file on flash memory on devicer in console",
479 "mem spiffs view -f tag.bin"
482 void *argtable[] = {
483 arg_param_begin,
484 arg_str1("f", "file", "<fn>", "SPIFFS file to view"),
485 arg_int0("c", "cols", "<dec>", "column breaks (def 32)"),
486 arg_param_end
488 CLIExecWithReturn(ctx, Cmd, argtable, false);
490 int slen = 0;
491 char src[32] = {0};
492 CLIParamStrToBuf(arg_get_str(ctx, 1), (uint8_t *)src, 32, &slen);
494 int breaks = arg_get_int_def(ctx, 2, 32);
495 CLIParserFree(ctx);
497 // get size from spiffs itself !
498 clearCommandBuffer();
499 SendCommandNG(CMD_SPIFFS_STAT, (uint8_t *)src, slen);
500 PacketResponseNG resp;
501 if (WaitForResponseTimeout(CMD_SPIFFS_STAT, &resp, 2000) == false) {
502 PrintAndLogEx(WARNING, "timeout while waiting for reply.");
503 return PM3_ETIMEOUT;
506 uint32_t len = resp.data.asDwords[0];
507 if (len == 0) {
508 PrintAndLogEx(ERR, "error, failed to retrieve file stats on SPIFFSS");
509 return PM3_EFAILED;
512 uint8_t *dump = calloc(len, sizeof(uint8_t));
513 if (!dump) {
514 PrintAndLogEx(ERR, "error, cannot allocate memory ");
515 return PM3_EMALLOC;
518 uint32_t start_index = 0;
519 PrintAndLogEx(INFO, "downloading "_YELLOW_("%u") " bytes from `" _YELLOW_("%s") "` (spiffs)", len, src);
521 if (!GetFromDevice(SPIFFS, dump, len, start_index, (uint8_t *)src, slen, NULL, -1, true)) {
522 PrintAndLogEx(FAILED, "error, downloading from spiffs");
523 free(dump);
524 return PM3_EFLASH;
527 PrintAndLogEx(NORMAL, "");
528 print_hex_break(dump, len, breaks);
529 PrintAndLogEx(NORMAL, "");
530 free(dump);
531 return PM3_SUCCESS;
534 static command_t CommandTable[] = {
535 {"help", CmdHelp, AlwaysAvailable, "This help"},
536 {"copy", CmdFlashMemSpiFFSCopy, IfPm3Flash, "Copy a file to another (destructively) in SPIFFS file system"},
537 {"check", CmdFlashMemSpiFFSCheck, IfPm3Flash, "Check/try to defrag faulty/fragmented file system"},
538 {"dump", CmdFlashMemSpiFFSDump, IfPm3Flash, "Dump a file from SPIFFS file system"},
539 {"info", CmdFlashMemSpiFFSInfo, IfPm3Flash, "Print file system info and usage statistics"},
540 {"mount", CmdFlashMemSpiFFSMount, IfPm3Flash, "Mount the SPIFFS file system if not already mounted"},
541 {"remove", CmdFlashMemSpiFFSRemove, IfPm3Flash, "Remove a file from SPIFFS file system"},
542 {"rename", CmdFlashMemSpiFFSRename, IfPm3Flash, "Rename/move a file in SPIFFS file system"},
543 {"test", CmdFlashMemSpiFFSTest, IfPm3Flash, "Test SPIFFS Operations"},
544 {"tree", CmdFlashMemSpiFFSTree, IfPm3Flash, "Print the Flash memory file system tree"},
545 {"unmount", CmdFlashMemSpiFFSUnmount, IfPm3Flash, "Un-mount the SPIFFS file system"},
546 {"upload", CmdFlashMemSpiFFSUpload, IfPm3Flash, "Upload file into SPIFFS file system"},
547 {"view", CmdFlashMemSpiFFSView, IfPm3Flash, "View file on SPIFFS file system"},
548 {"wipe", CmdFlashMemSpiFFSWipe, IfPm3Flash, "Wipe all files from SPIFFS file system * " _RED_("dangerous") " *" },
549 {NULL, NULL, NULL, NULL}
552 static int CmdHelp(const char *Cmd) {
553 (void)Cmd; // Cmd is not used so far
554 CmdsHelp(CommandTable);
555 return PM3_SUCCESS;
558 int CmdFlashMemSpiFFS(const char *Cmd) {
559 clearCommandBuffer();
560 return CmdsParse(CommandTable, Cmd);