Welcome to mirror list, hosted at ThFree Co, Russian Federation.

storage-cli.c « storage « applications - github.com/ClusterM/flipperzero-firmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ad232cae02ad76eda883c929dbfb9bf722a8fae6 (plain)
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#include <furi.h>
#include <furi-hal.h>

#include <cli/cli.h>
#include <lib/toolbox/args.h>
#include <lib/toolbox/md5.h>
#include <storage/storage.h>
#include <storage/storage-sd-api.h>
#include <power/power_service/power.h>

#define MAX_NAME_LENGTH 255

static void storage_cli_print_usage() {
    printf("Usage:\r\n");
    printf("storage <cmd> <path> <args>\r\n");
    printf("The path must start with /int or /ext\r\n");
    printf("Cmd list:\r\n");
    printf("\tinfo\t - get FS info\r\n");
    printf("\tformat\t - format filesystem\r\n");
    printf("\tlist\t - list files and dirs\r\n");
    printf("\tremove\t - delete the file or directory\r\n");
    printf("\tread\t - read text from file and print file size and content to cli\r\n");
    printf(
        "\tread_chunks\t - read data from file and print file size and content to cli, <args> should contain how many bytes you want to read in block\r\n");
    printf("\twrite\t - read text from cli and append it to file, stops by ctrl+c\r\n");
    printf(
        "\twrite_chunk\t - read data from cli and append it to file, <args> should contain how many bytes you want to write\r\n");
    printf("\tcopy\t - copy file to new file, <args> must contain new path\r\n");
    printf("\trename\t - move file to new file, <args> must contain new path\r\n");
    printf("\tmkdir\t - creates a new directory\r\n");
    printf("\tmd5\t - md5 hash of the file\r\n");
    printf("\tstat\t - info about file or dir\r\n");
};

static void storage_cli_print_error(FS_Error error) {
    printf("Storage error: %s\r\n", storage_error_get_desc(error));
}

static void storage_cli_info(Cli* cli, string_t path) {
    Storage* api = furi_record_open("storage");

    if(string_cmp_str(path, "/int") == 0) {
        uint64_t total_space;
        uint64_t free_space;
        FS_Error error = storage_common_fs_info(api, "/int", &total_space, &free_space);

        if(error != FSE_OK) {
            storage_cli_print_error(error);
        } else {
            printf(
                "Label: %s\r\nType: LittleFS\r\n%luKB total\r\n%luKB free\r\n",
                furi_hal_version_get_name_ptr() ? furi_hal_version_get_name_ptr() : "Unknown",
                (uint32_t)(total_space / 1024),
                (uint32_t)(free_space / 1024));
        }
    } else if(string_cmp_str(path, "/ext") == 0) {
        SDInfo sd_info;
        FS_Error error = storage_sd_info(api, &sd_info);

        if(error != FSE_OK) {
            storage_cli_print_error(error);
        } else {
            printf(
                "Label: %s\r\nType: %s\r\n%luKB total\r\n%luKB free\r\n",
                sd_info.label,
                sd_api_get_fs_type_text(sd_info.fs_type),
                sd_info.kb_total,
                sd_info.kb_free);
        }
    } else {
        storage_cli_print_usage();
    }

    furi_record_close("storage");
};

static void storage_cli_format(Cli* cli, string_t path) {
    if(string_cmp_str(path, "/int") == 0) {
        storage_cli_print_error(FSE_NOT_IMPLEMENTED);
    } else if(string_cmp_str(path, "/ext") == 0) {
        printf("Formatting SD card, all data will be lost. Are you sure (y/n)?\r\n");
        char answer = cli_getc(cli);
        if(answer == 'y' || answer == 'Y') {
            Storage* api = furi_record_open("storage");
            printf("Formatting, please wait...\r\n");

            FS_Error error = storage_sd_format(api);

            if(error != FSE_OK) {
                storage_cli_print_error(error);
            } else {
                printf("SD card was successfully formatted.\r\n");
            }
            furi_record_close("storage");
        } else {
            printf("Cancelled.\r\n");
        }
    } else {
        storage_cli_print_usage();
    }
};

static void storage_cli_list(Cli* cli, string_t path) {
    if(string_cmp_str(path, "/") == 0) {
        printf("\t[D] int\r\n");
        printf("\t[D] ext\r\n");
        printf("\t[D] any\r\n");
    } else {
        Storage* api = furi_record_open("storage");
        File* file = storage_file_alloc(api);

        if(storage_dir_open(file, string_get_cstr(path))) {
            FileInfo fileinfo;
            char name[MAX_NAME_LENGTH];
            bool readed = false;

            while(storage_dir_read(file, &fileinfo, name, MAX_NAME_LENGTH)) {
                readed = true;
                if(fileinfo.flags & FSF_DIRECTORY) {
                    printf("\t[D] %s\r\n", name);
                } else {
                    printf("\t[F] %s %lub\r\n", name, (uint32_t)(fileinfo.size));
                }
            }

            if(!readed) {
                printf("\tEmpty\r\n");
            }
        } else {
            storage_cli_print_error(storage_file_get_error(file));
        }

        storage_dir_close(file);
        storage_file_free(file);
        furi_record_close("storage");
    }
}

static void storage_cli_read(Cli* cli, string_t path) {
    Storage* api = furi_record_open("storage");
    File* file = storage_file_alloc(api);

    if(storage_file_open(file, string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
        const uint16_t read_size = 128;
        uint16_t readed_size = 0;
        uint8_t* data = furi_alloc(read_size);

        printf("Size: %lu\r\n", (uint32_t)storage_file_size(file));

        do {
            readed_size = storage_file_read(file, data, read_size);
            for(uint16_t i = 0; i < readed_size; i++) {
                printf("%c", data[i]);
            }
        } while(readed_size > 0);
        printf("\r\n");

        free(data);
    } else {
        storage_cli_print_error(storage_file_get_error(file));
    }

    storage_file_close(file);
    storage_file_free(file);

    furi_record_close("storage");
}

static void storage_cli_write(Cli* cli, string_t path) {
    Storage* api = furi_record_open("storage");
    File* file = storage_file_alloc(api);

    const uint16_t buffer_size = 512;
    uint8_t* buffer = furi_alloc(buffer_size);

    if(storage_file_open(file, string_get_cstr(path), FSAM_WRITE, FSOM_OPEN_APPEND)) {
        printf("Just write your text data. New line by Ctrl+Enter, exit by Ctrl+C.\r\n");

        uint32_t readed_index = 0;

        while(true) {
            uint8_t symbol = cli_getc(cli);

            if(symbol == CliSymbolAsciiETX) {
                uint16_t write_size = readed_index % buffer_size;

                if(write_size > 0) {
                    uint16_t writed_size = storage_file_write(file, buffer, write_size);

                    if(writed_size != write_size) {
                        storage_cli_print_error(storage_file_get_error(file));
                    }
                    break;
                }
            }

            buffer[readed_index % buffer_size] = symbol;
            printf("%c", buffer[readed_index % buffer_size]);
            fflush(stdout);
            readed_index++;

            if(((readed_index % buffer_size) == 0)) {
                uint16_t writed_size = storage_file_write(file, buffer, buffer_size);

                if(writed_size != buffer_size) {
                    storage_cli_print_error(storage_file_get_error(file));
                    break;
                }
            }
        }
        printf("\r\n");

    } else {
        storage_cli_print_error(storage_file_get_error(file));
    }
    storage_file_close(file);

    free(buffer);
    storage_file_free(file);
    furi_record_close("storage");
}

static void storage_cli_read_chunks(Cli* cli, string_t path, string_t args) {
    Storage* api = furi_record_open("storage");
    File* file = storage_file_alloc(api);

    uint32_t buffer_size;
    int parsed_count = sscanf(string_get_cstr(args), "%lu", &buffer_size);

    if(parsed_count == EOF || parsed_count != 1) {
        storage_cli_print_usage();
    } else if(storage_file_open(file, string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
        uint8_t* data = furi_alloc(buffer_size);
        uint64_t file_size = storage_file_size(file);

        printf("Size: %lu\r\n", (uint32_t)file_size);

        while(file_size > 0) {
            printf("\r\nReady?\r\n");
            cli_getc(cli);

            uint16_t readed_size = storage_file_read(file, data, buffer_size);
            for(uint16_t i = 0; i < readed_size; i++) {
                putchar(data[i]);
            }
            file_size -= readed_size;
        }
        printf("\r\n");

        free(data);
    } else {
        storage_cli_print_error(storage_file_get_error(file));
    }

    storage_file_close(file);
    storage_file_free(file);

    furi_record_close("storage");
}

static void storage_cli_write_chunk(Cli* cli, string_t path, string_t args) {
    Storage* api = furi_record_open("storage");
    File* file = storage_file_alloc(api);

    uint32_t buffer_size;
    int parsed_count = sscanf(string_get_cstr(args), "%lu", &buffer_size);

    if(parsed_count == EOF || parsed_count != 1) {
        storage_cli_print_usage();
    } else {
        if(storage_file_open(file, string_get_cstr(path), FSAM_WRITE, FSOM_OPEN_APPEND)) {
            printf("Ready\r\n");

            uint8_t* buffer = furi_alloc(buffer_size);

            for(uint32_t i = 0; i < buffer_size; i++) {
                buffer[i] = cli_getc(cli);
            }

            uint16_t writed_size = storage_file_write(file, buffer, buffer_size);

            if(writed_size != buffer_size) {
                storage_cli_print_error(storage_file_get_error(file));
            }

            free(buffer);
        } else {
            storage_cli_print_error(storage_file_get_error(file));
        }
        storage_file_close(file);
    }

    storage_file_free(file);
    furi_record_close("storage");
}

static void storage_cli_stat(Cli* cli, string_t path) {
    Storage* api = furi_record_open("storage");

    if(string_cmp_str(path, "/") == 0) {
        printf("Storage\r\n");
    } else if(
        string_cmp_str(path, "/ext") == 0 || string_cmp_str(path, "/int") == 0 ||
        string_cmp_str(path, "/any") == 0) {
        uint64_t total_space;
        uint64_t free_space;
        FS_Error error =
            storage_common_fs_info(api, string_get_cstr(path), &total_space, &free_space);

        if(error != FSE_OK) {
            storage_cli_print_error(error);
        } else {
            printf(
                "Storage, %luKB total, %luKB free\r\n",
                (uint32_t)(total_space / 1024),
                (uint32_t)(free_space / 1024));
        }
    } else {
        FileInfo fileinfo;
        FS_Error error = storage_common_stat(api, string_get_cstr(path), &fileinfo);

        if(error == FSE_OK) {
            if(fileinfo.flags & FSF_DIRECTORY) {
                printf("Directory\r\n");
            } else {
                printf("File, size: %lub\r\n", (uint32_t)(fileinfo.size));
            }
        } else {
            storage_cli_print_error(error);
        }
    }

    furi_record_close("storage");
}

static void storage_cli_copy(Cli* cli, string_t old_path, string_t args) {
    Storage* api = furi_record_open("storage");
    string_t new_path;
    string_init(new_path);

    if(!args_read_probably_quoted_string_and_trim(args, new_path)) {
        storage_cli_print_usage();
    } else {
        FS_Error error =
            storage_common_copy(api, string_get_cstr(old_path), string_get_cstr(new_path));

        if(error != FSE_OK) {
            storage_cli_print_error(error);
        }
    }

    string_clear(new_path);
    furi_record_close("storage");
}

static void storage_cli_remove(Cli* cli, string_t path) {
    Storage* api = furi_record_open("storage");
    FS_Error error = storage_common_remove(api, string_get_cstr(path));

    if(error != FSE_OK) {
        storage_cli_print_error(error);
    }

    furi_record_close("storage");
}

static void storage_cli_rename(Cli* cli, string_t old_path, string_t args) {
    Storage* api = furi_record_open("storage");
    string_t new_path;
    string_init(new_path);

    if(!args_read_probably_quoted_string_and_trim(args, new_path)) {
        storage_cli_print_usage();
    } else {
        FS_Error error =
            storage_common_rename(api, string_get_cstr(old_path), string_get_cstr(new_path));

        if(error != FSE_OK) {
            storage_cli_print_error(error);
        }
    }

    string_clear(new_path);
    furi_record_close("storage");
}

static void storage_cli_mkdir(Cli* cli, string_t path) {
    Storage* api = furi_record_open("storage");
    FS_Error error = storage_common_mkdir(api, string_get_cstr(path));

    if(error != FSE_OK) {
        storage_cli_print_error(error);
    }

    furi_record_close("storage");
}

static void storage_cli_md5(Cli* cli, string_t path) {
    Storage* api = furi_record_open("storage");
    File* file = storage_file_alloc(api);

    if(storage_file_open(file, string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
        const uint16_t read_size = 512;
        const uint8_t hash_size = 16;
        uint8_t* data = malloc(read_size);
        uint8_t* hash = malloc(sizeof(uint8_t) * hash_size);
        md5_context* md5_ctx = malloc(sizeof(md5_context));

        md5_starts(md5_ctx);
        while(true) {
            uint16_t readed_size = storage_file_read(file, data, read_size);
            if(readed_size == 0) break;
            md5_update(md5_ctx, data, readed_size);
        }
        md5_finish(md5_ctx, hash);
        free(md5_ctx);

        for(uint8_t i = 0; i < hash_size; i++) {
            printf("%02x", hash[i]);
        }
        printf("\r\n");

        free(hash);
        free(data);
    } else {
        storage_cli_print_error(storage_file_get_error(file));
    }

    storage_file_close(file);
    storage_file_free(file);

    furi_record_close("storage");
}

void storage_cli(Cli* cli, string_t args, void* context) {
    string_t cmd;
    string_t path;
    string_init(cmd);
    string_init(path);

    do {
        if(!args_read_string_and_trim(args, cmd)) {
            storage_cli_print_usage();
            break;
        }

        if(!args_read_probably_quoted_string_and_trim(args, path)) {
            storage_cli_print_usage();
            break;
        }

        if(string_cmp_str(cmd, "info") == 0) {
            storage_cli_info(cli, path);
            break;
        }

        if(string_cmp_str(cmd, "format") == 0) {
            storage_cli_format(cli, path);
            break;
        }

        if(string_cmp_str(cmd, "list") == 0) {
            storage_cli_list(cli, path);
            break;
        }

        if(string_cmp_str(cmd, "read") == 0) {
            storage_cli_read(cli, path);
            break;
        }

        if(string_cmp_str(cmd, "read_chunks") == 0) {
            storage_cli_read_chunks(cli, path, args);
            break;
        }

        if(string_cmp_str(cmd, "write") == 0) {
            storage_cli_write(cli, path);
            break;
        }

        if(string_cmp_str(cmd, "write_chunk") == 0) {
            storage_cli_write_chunk(cli, path, args);
            break;
        }

        if(string_cmp_str(cmd, "copy") == 0) {
            storage_cli_copy(cli, path, args);
            break;
        }

        if(string_cmp_str(cmd, "remove") == 0) {
            storage_cli_remove(cli, path);
            break;
        }

        if(string_cmp_str(cmd, "rename") == 0) {
            storage_cli_rename(cli, path, args);
            break;
        }

        if(string_cmp_str(cmd, "mkdir") == 0) {
            storage_cli_mkdir(cli, path);
            break;
        }

        if(string_cmp_str(cmd, "md5") == 0) {
            storage_cli_md5(cli, path);
            break;
        }

        if(string_cmp_str(cmd, "stat") == 0) {
            storage_cli_stat(cli, path);
            break;
        }

        storage_cli_print_usage();
    } while(false);

    string_clear(path);
    string_clear(cmd);
}

void storage_cli_factory_reset(Cli* cli, string_t args, void* context) {
    printf("All data will be lost. Are you sure (y/n)?\r\n");
    char c = cli_getc(cli);
    if(c == 'y' || c == 'Y') {
        printf("Data will be wiped after reboot.\r\n");
        furi_hal_rtc_set_flag(FuriHalRtcFlagFactoryReset);
        power_reboot(PowerBootModeNormal);
    } else {
        printf("Safe choice.\r\n");
    }
}

void storage_on_system_start() {
#ifdef SRV_CLI
    Cli* cli = furi_record_open("cli");
    cli_add_command(cli, "storage", CliCommandFlagDefault, storage_cli, NULL);
    cli_add_command(
        cli, "factory_reset", CliCommandFlagParallelSafe, storage_cli_factory_reset, NULL);
    furi_record_close("cli");
#endif
}