1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2018 iceman
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
7 //-----------------------------------------------------------------------------
8 // Proxmark3 RDV40 Flash memory commands
9 //-----------------------------------------------------------------------------
10 #include "cmdflashmemspiffs.h"
12 #include "cmdparser.h" // command_t
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);
29 uint32_t bytes_sent
= 0;
30 uint32_t bytes_remaining
= datalen
;
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
;
54 SendCommandNG(CMD_SPIFFS_WRITE
, (uint8_t *)payload
, sizeof(flashmem_write_t
) + bytes_in_packet
);
58 bytes_remaining
-= bytes_in_packet
;
59 bytes_sent
+= bytes_in_packet
;
62 while (WaitForResponseTimeout(CMD_SPIFFS_WRITE
, &resp
, 2000) == false) {
63 PrintAndLogEx(WARNING
, "timeout while waiting for reply.");
66 ret_val
= PM3_ETIMEOUT
;
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);
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",
94 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
97 SendCommandNG(CMD_SPIFFS_MOUNT
, NULL
, 0);
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");
111 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
113 clearCommandBuffer();
114 SendCommandNG(CMD_SPIFFS_UNMOUNT
, NULL
, 0);
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",
128 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
130 clearCommandBuffer();
131 SendCommandNG(CMD_SPIFFS_TEST
, NULL
, 0);
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",
145 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
147 clearCommandBuffer();
148 SendCommandNG(CMD_SPIFFS_CHECK
, NULL
, 0);
152 static int CmdFlashMemSpiFFSTree(const char *Cmd
) {
153 CLIParserContext
*ctx
;
154 CLIParserInit(&ctx
, "mem spiffs tree",
155 "Print the Flash memory file system tree",
162 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
165 PrintAndLogEx(INFO
, "--- " _CYAN_("Flash Memory tree (SPIFFS)") " -----------------");
166 clearCommandBuffer();
167 SendCommandNG(CMD_SPIFFS_PRINT_TREE
, NULL
, 0);
171 static int CmdFlashMemSpiFFSInfo(const char *Cmd
) {
172 CLIParserContext
*ctx
;
173 CLIParserInit(&ctx
, "mem spiffs info",
174 "Print file system info and usage statistics",
181 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
184 PrintAndLogEx(INFO
, "--- " _CYAN_("Flash Memory info (SPIFFS)") " -----------------");
185 clearCommandBuffer();
186 SendCommandNG(CMD_SPIFFS_PRINT_FSINFO
, NULL
, 0);
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"
199 arg_str1("f", "filename", "<fn>", "file to remove"),
202 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
205 char filename
[32] = {0};
206 CLIParamStrToBuf(arg_get_str(ctx
, 1), (uint8_t *)filename
, 32, &fnlen
);
209 PrintAndLogEx(DEBUG
, "Removing `" _YELLOW_("%s") "`", filename
);
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!");
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"
236 arg_str1("s", "src", "<fn>", "source file name"),
237 arg_str1("d", "dest", "<fn>", "destination file name"),
240 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
244 CLIParamStrToBuf(arg_get_str(ctx
, 1), (uint8_t *)src
, 32, &slen
);
248 CLIParamStrToBuf(arg_get_str(ctx
, 2), (uint8_t *)dest
, 32, &dlen
);
251 PrintAndLogEx(DEBUG
, "Rename from `" _YELLOW_("%s") "` -> `" _YELLOW_("%s") "`", src
, dest
);
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");
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"
286 arg_str1("s", "src", "<fn>", "source file name"),
287 arg_str1("d", "dest", "<fn>", "destination file name"),
290 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
294 CLIParamStrToBuf(arg_get_str(ctx
, 1), (uint8_t *)src
, 32, &slen
);
298 CLIParamStrToBuf(arg_get_str(ctx
, 2), (uint8_t *)dest
, 32, &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");
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"
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"),
340 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
344 CLIParamStrToBuf(arg_get_str(ctx
, 1), (uint8_t *)src
, 32, &slen
);
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);
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.");
362 uint32_t len
= resp
.data
.asDwords
[0];
363 uint8_t *dump
= calloc(len
, sizeof(uint8_t));
365 PrintAndLogEx(ERR
, "error, cannot allocate memory ");
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");
379 char fn
[FILE_PATH_SIZE
] = {0};
381 strncpy(fn
, src
, slen
);
383 strncpy(fn
, dest
, dlen
);
386 saveFile(fn
, ".bin", dump
, len
);
388 uint8_t eml_len
= 16;
389 if (strstr(fn
, "class") != NULL
)
391 else if (strstr(fn
, "mfu") != NULL
)
394 saveFileEML(fn
, dump
, len
, eml_len
);
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"),
411 CLIExecWithReturn(ctx
, Cmd
, argtable
, true);
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");
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"
438 arg_str1("s", "src", "<fn>", "source file name"),
439 arg_str1("d", "dest", "<fn>", "destination file name"),
442 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
445 char src
[FILE_PATH_SIZE
] = {0};
446 CLIParamStrToBuf(arg_get_str(ctx
, 1), (uint8_t *)src
, FILE_PATH_SIZE
, &slen
);
450 CLIParamStrToBuf(arg_get_str(ctx
, 2), (uint8_t *)dest
, 32, &dlen
);
453 PrintAndLogEx(DEBUG
, "Upload `" _YELLOW_("%s") "` -> `" _YELLOW_("%s") "`", src
, dest
);
456 uint8_t *data
= NULL
;
458 int res
= loadFile_safe(src
, "", (void **)&data
, &datalen
);
459 if (res
!= PM3_SUCCESS
) {
464 res
= flashmem_spiffs_load(dest
, data
, datalen
);
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");
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"
484 arg_str1("f", "file", "<fn>", "SPIFFS file to view"),
485 arg_int0("c", "cols", "<dec>", "column breaks (def 32)"),
488 CLIExecWithReturn(ctx
, Cmd
, argtable
, false);
492 CLIParamStrToBuf(arg_get_str(ctx
, 1), (uint8_t *)src
, 32, &slen
);
494 int breaks
= arg_get_int_def(ctx
, 2, 32);
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.");
506 uint32_t len
= resp
.data
.asDwords
[0];
508 PrintAndLogEx(ERR
, "error, failed to retrieve file stats on SPIFFSS");
512 uint8_t *dump
= calloc(len
, sizeof(uint8_t));
514 PrintAndLogEx(ERR
, "error, cannot allocate memory ");
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");
527 PrintAndLogEx(NORMAL
, "");
528 print_hex_break(dump
, len
, breaks
);
529 PrintAndLogEx(NORMAL
, "");
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
);
558 int CmdFlashMemSpiFFS(const char *Cmd
) {
559 clearCommandBuffer();
560 return CmdsParse(CommandTable
, Cmd
);