[FL-1682] Faster file receiving function. Storage management scripts. (#647)
* Storage-cli: the file write function is splitted into a function for working with text and function for raw data * Storage-cli: read_chunks, renamed write_raw to write_chunk * Storage-cli: fix typo * SD Hal: fixed wrong read/write block address * HAL-console: printf * Storage benchmark: more popular sizes * Toolbox: md5 * Storage-cli: better read_chunks function, md5 hash function * Notification: fixed incorrect error message when loading settings * Storage-cli: stat command * Storage-cli: stat, "/" is also storage * Scripts: add storage managment script * Scripts, storage lib: send command with known response syntax instead of SOH * Scripts: python3 from env * Storage-cli: fixed mess with error texts * Storage-cli: write, show welcome message only if we didn't have any errors * Storage: poorly fixed folders copying * Storage: close an old file if an error occurred while opening a new file * Storage-cli: fixed storage info spacing * Scripts: use positional arguments in storage. * Scripts: explicit string encoding and decoding, documentation in comments. Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
parent
d04deb48b8
commit
9d38f28de7
@ -333,7 +333,7 @@ static bool notification_load_settings(NotificationApp* app) {
|
||||
FURI_LOG_E(
|
||||
"notification",
|
||||
"version(%d != %d) mismatch",
|
||||
app->settings.version,
|
||||
settings.version,
|
||||
NOTIFICATION_SETTINGS_VERSION);
|
||||
} else {
|
||||
osKernelLock();
|
||||
|
@ -81,7 +81,7 @@ static void storage_settings_benchmark(StorageSettings* app) {
|
||||
bench_data[i] = (uint8_t)i;
|
||||
}
|
||||
|
||||
uint16_t bench_size[BENCH_COUNT] = {1, 8, 32, 256, 1024, 4096};
|
||||
uint16_t bench_size[BENCH_COUNT] = {1, 8, 32, 256, 512, 1024};
|
||||
uint32_t bench_w_speed[BENCH_COUNT] = {0, 0, 0, 0, 0, 0};
|
||||
uint32_t bench_r_speed[BENCH_COUNT] = {0, 0, 0, 0, 0, 0};
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include <furi.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 <furi-hal-version.h>
|
||||
@ -25,32 +26,23 @@ void storage_cli_print_usage() {
|
||||
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 data from file and print file size and content to cli\r\n");
|
||||
printf("\tread\t - read text from file and print file size and content to cli\r\n");
|
||||
printf(
|
||||
"\twrite\t - read data from cli and append it to file, <args> should contain how many bytes you want to write\r\n");
|
||||
"\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");
|
||||
};
|
||||
|
||||
void storage_cli_print_error(FS_Error error) {
|
||||
printf("Storage error: %s\r\n", storage_error_get_desc(error));
|
||||
}
|
||||
|
||||
void storage_cli_print_path_error(string_t path, FS_Error error) {
|
||||
printf(
|
||||
"Storage error for path \"%s\": %s\r\n",
|
||||
string_get_cstr(path),
|
||||
storage_error_get_desc(error));
|
||||
}
|
||||
|
||||
void storage_cli_print_file_error(string_t path, File* file) {
|
||||
printf(
|
||||
"Storage error for path \"%s\": %s\r\n",
|
||||
string_get_cstr(path),
|
||||
storage_file_get_error_desc(file));
|
||||
}
|
||||
|
||||
void storage_cli_info(Cli* cli, string_t path) {
|
||||
Storage* api = furi_record_open("storage");
|
||||
|
||||
@ -60,10 +52,10 @@ void storage_cli_info(Cli* cli, string_t path) {
|
||||
FS_Error error = storage_common_fs_info(api, "/int", &total_space, &free_space);
|
||||
|
||||
if(error != FSE_OK) {
|
||||
storage_cli_print_path_error(path, error);
|
||||
storage_cli_print_error(error);
|
||||
} else {
|
||||
printf(
|
||||
"Label: %s\r\nType: LittleFS\r\n%lu KB total\r\n%lu KB free\r\n",
|
||||
"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));
|
||||
@ -73,10 +65,10 @@ void storage_cli_info(Cli* cli, string_t path) {
|
||||
FS_Error error = storage_sd_info(api, &sd_info);
|
||||
|
||||
if(error != FSE_OK) {
|
||||
storage_cli_print_path_error(path, error);
|
||||
storage_cli_print_error(error);
|
||||
} else {
|
||||
printf(
|
||||
"Label: %s\r\nType: %s\r\n%lu KB total\r\n%lu KB free\r\n",
|
||||
"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,
|
||||
@ -91,7 +83,7 @@ void storage_cli_info(Cli* cli, string_t path) {
|
||||
|
||||
void storage_cli_format(Cli* cli, string_t path) {
|
||||
if(string_cmp_str(path, "/int") == 0) {
|
||||
storage_cli_print_path_error(path, FSE_NOT_IMPLEMENTED);
|
||||
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);
|
||||
@ -102,7 +94,7 @@ void storage_cli_format(Cli* cli, string_t path) {
|
||||
FS_Error error = storage_sd_format(api);
|
||||
|
||||
if(error != FSE_OK) {
|
||||
storage_cli_print_path_error(path, error);
|
||||
storage_cli_print_error(error);
|
||||
} else {
|
||||
printf("SD card was successfully formatted.\r\n");
|
||||
}
|
||||
@ -142,7 +134,7 @@ void storage_cli_list(Cli* cli, string_t path) {
|
||||
printf("\tEmpty\r\n");
|
||||
}
|
||||
} else {
|
||||
storage_cli_print_file_error(path, file);
|
||||
storage_cli_print_error(storage_file_get_error(file));
|
||||
}
|
||||
|
||||
storage_dir_close(file);
|
||||
@ -172,7 +164,7 @@ void storage_cli_read(Cli* cli, string_t path) {
|
||||
|
||||
free(data);
|
||||
} else {
|
||||
storage_cli_print_file_error(path, file);
|
||||
storage_cli_print_error(storage_file_get_error(file));
|
||||
}
|
||||
|
||||
storage_file_close(file);
|
||||
@ -181,52 +173,126 @@ void storage_cli_read(Cli* cli, string_t path) {
|
||||
furi_record_close("storage");
|
||||
}
|
||||
|
||||
void storage_cli_write(Cli* cli, string_t path, string_t args) {
|
||||
void storage_cli_write(Cli* cli, string_t path) {
|
||||
Storage* api = furi_record_open("storage");
|
||||
File* file = storage_file_alloc(api);
|
||||
|
||||
uint32_t size;
|
||||
int parsed_count = sscanf(string_get_cstr(args), "%lu", &size);
|
||||
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");
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
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)) {
|
||||
const uint16_t write_size = 8;
|
||||
uint32_t readed_index = 0;
|
||||
uint8_t* data = furi_alloc(write_size);
|
||||
printf("Ready\r\n");
|
||||
|
||||
while(true) {
|
||||
data[readed_index % write_size] = cli_getc(cli);
|
||||
printf("%c", data[readed_index % write_size]);
|
||||
fflush(stdout);
|
||||
readed_index++;
|
||||
uint8_t* buffer = furi_alloc(buffer_size);
|
||||
|
||||
if(((readed_index % write_size) == 0)) {
|
||||
uint16_t writed_size = storage_file_write(file, data, write_size);
|
||||
|
||||
if(writed_size != write_size) {
|
||||
storage_cli_print_file_error(path, file);
|
||||
break;
|
||||
}
|
||||
} else if(readed_index == size) {
|
||||
uint16_t writed_size = storage_file_write(file, data, size % write_size);
|
||||
|
||||
if(writed_size != (size % write_size)) {
|
||||
storage_cli_print_file_error(path, file);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(readed_index == size) {
|
||||
break;
|
||||
}
|
||||
for(uint32_t i = 0; i < buffer_size; i++) {
|
||||
buffer[i] = cli_getc(cli);
|
||||
}
|
||||
printf("\r\n");
|
||||
|
||||
free(data);
|
||||
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_file_error(path, file);
|
||||
storage_cli_print_error(storage_file_get_error(file));
|
||||
}
|
||||
storage_file_close(file);
|
||||
}
|
||||
@ -235,6 +301,45 @@ void storage_cli_write(Cli* cli, string_t path, string_t args) {
|
||||
furi_record_close("storage");
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
void storage_cli_copy(Cli* cli, string_t old_path, string_t args) {
|
||||
Storage* api = furi_record_open("storage");
|
||||
string_t new_path;
|
||||
@ -297,6 +402,43 @@ void storage_cli_mkdir(Cli* cli, string_t path) {
|
||||
furi_record_close("storage");
|
||||
}
|
||||
|
||||
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;
|
||||
@ -334,8 +476,18 @@ void storage_cli(Cli* cli, string_t args, void* context) {
|
||||
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, args);
|
||||
storage_cli_write(cli, path);
|
||||
break;
|
||||
}
|
||||
|
||||
if(string_cmp_str(cmd, "write_chunk") == 0) {
|
||||
storage_cli_write_chunk(cli, path, args);
|
||||
break;
|
||||
}
|
||||
|
||||
@ -359,6 +511,16 @@ void storage_cli(Cli* cli, string_t args, void* context) {
|
||||
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);
|
||||
|
||||
|
@ -307,43 +307,67 @@ static FS_Error storage_process_common_remove(Storage* app, const char* path) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
static FS_Error storage_process_common_mkdir(Storage* app, const char* path) {
|
||||
FS_Error ret = FSE_OK;
|
||||
StorageType type = storage_get_type_by_path(path);
|
||||
|
||||
if(storage_type_is_not_valid(type)) {
|
||||
ret = FSE_INVALID_NAME;
|
||||
} else {
|
||||
StorageData* storage = storage_get_storage_by_type(app, type);
|
||||
FS_CALL(storage, common.mkdir(storage, remove_vfs(path)));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static FS_Error storage_process_common_copy(Storage* app, const char* old, const char* new) {
|
||||
FS_Error ret = FSE_INTERNAL;
|
||||
File file_old;
|
||||
File file_new;
|
||||
|
||||
do {
|
||||
if(!storage_process_file_open(app, &file_old, old, FSAM_READ, FSOM_OPEN_EXISTING)) {
|
||||
ret = storage_file_get_error(&file_old);
|
||||
storage_process_file_close(app, &file_old);
|
||||
break;
|
||||
FileInfo fileinfo;
|
||||
ret = storage_process_common_stat(app, old, &fileinfo);
|
||||
|
||||
if(ret == FSE_OK) {
|
||||
if(fileinfo.flags & FSF_DIRECTORY) {
|
||||
ret = storage_process_common_mkdir(app, new);
|
||||
} else {
|
||||
do {
|
||||
if(!storage_process_file_open(app, &file_old, old, FSAM_READ, FSOM_OPEN_EXISTING)) {
|
||||
ret = storage_file_get_error(&file_old);
|
||||
storage_process_file_close(app, &file_old);
|
||||
break;
|
||||
}
|
||||
|
||||
if(!storage_process_file_open(app, &file_new, new, FSAM_WRITE, FSOM_CREATE_NEW)) {
|
||||
ret = storage_file_get_error(&file_new);
|
||||
storage_process_file_close(app, &file_new);
|
||||
storage_process_file_close(app, &file_old);
|
||||
break;
|
||||
}
|
||||
|
||||
const uint16_t buffer_size = 64;
|
||||
uint8_t* buffer = malloc(buffer_size);
|
||||
uint16_t readed_size = 0;
|
||||
uint16_t writed_size = 0;
|
||||
|
||||
while(true) {
|
||||
readed_size = storage_process_file_read(app, &file_old, buffer, buffer_size);
|
||||
ret = storage_file_get_error(&file_old);
|
||||
if(readed_size == 0) break;
|
||||
|
||||
writed_size = storage_process_file_write(app, &file_new, buffer, readed_size);
|
||||
ret = storage_file_get_error(&file_new);
|
||||
if(writed_size < readed_size) break;
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
storage_process_file_close(app, &file_old);
|
||||
storage_process_file_close(app, &file_new);
|
||||
} while(false);
|
||||
}
|
||||
|
||||
if(!storage_process_file_open(app, &file_new, new, FSAM_WRITE, FSOM_CREATE_NEW)) {
|
||||
ret = storage_file_get_error(&file_new);
|
||||
storage_process_file_close(app, &file_new);
|
||||
break;
|
||||
}
|
||||
|
||||
const uint16_t buffer_size = 64;
|
||||
uint8_t* buffer = malloc(buffer_size);
|
||||
uint16_t readed_size = 0;
|
||||
uint16_t writed_size = 0;
|
||||
|
||||
while(true) {
|
||||
readed_size = storage_process_file_read(app, &file_old, buffer, buffer_size);
|
||||
ret = storage_file_get_error(&file_old);
|
||||
if(readed_size == 0) break;
|
||||
|
||||
writed_size = storage_process_file_write(app, &file_new, buffer, readed_size);
|
||||
ret = storage_file_get_error(&file_new);
|
||||
if(writed_size < readed_size) break;
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
storage_process_file_close(app, &file_old);
|
||||
storage_process_file_close(app, &file_new);
|
||||
} while(false);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@ -370,20 +394,6 @@ static FS_Error storage_process_common_rename(Storage* app, const char* old, con
|
||||
return ret;
|
||||
}
|
||||
|
||||
static FS_Error storage_process_common_mkdir(Storage* app, const char* path) {
|
||||
FS_Error ret = FSE_OK;
|
||||
StorageType type = storage_get_type_by_path(path);
|
||||
|
||||
if(storage_type_is_not_valid(type)) {
|
||||
ret = FSE_INVALID_NAME;
|
||||
} else {
|
||||
StorageData* storage = storage_get_storage_by_type(app, type);
|
||||
FS_CALL(storage, common.mkdir(storage, remove_vfs(path)));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static FS_Error storage_process_common_fs_info(
|
||||
Storage* app,
|
||||
const char* fs_path,
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <stm32wbxx_ll_gpio.h>
|
||||
#include <stm32wbxx_ll_usart.h>
|
||||
#include <m-string.h>
|
||||
|
||||
#include <furi.h>
|
||||
|
||||
@ -55,4 +56,14 @@ void furi_hal_console_tx(const uint8_t* buffer, size_t buffer_size) {
|
||||
|
||||
/* Wait for TC flag to be raised for last char */
|
||||
while (!LL_USART_IsActiveFlag_TC(USART1));
|
||||
}
|
||||
|
||||
void furi_hal_console_printf(const char format[], ...) {
|
||||
string_t string;
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
string_init_vprintf(string, format, args);
|
||||
va_end(args);
|
||||
furi_hal_console_tx((const uint8_t*)string_get_cstr(string), string_size(string));
|
||||
string_clear(string);
|
||||
}
|
@ -11,6 +11,8 @@ void furi_hal_console_init();
|
||||
|
||||
void furi_hal_console_tx(const uint8_t* buffer, size_t buffer_size);
|
||||
|
||||
void furi_hal_console_printf(const char format[], ...);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
299
lib/toolbox/md5.c
Normal file
299
lib/toolbox/md5.c
Normal file
@ -0,0 +1,299 @@
|
||||
/*******************************************************************************
|
||||
* Portions COPYRIGHT 2015 STMicroelectronics *
|
||||
* Portions Copyright (C) 2006-2013, Brainspark B.V. *
|
||||
*******************************************************************************/
|
||||
|
||||
/*
|
||||
* RFC 1321 compliant MD5 implementation
|
||||
*
|
||||
* Copyright (C) 2006-2013, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
/*
|
||||
* The MD5 algorithm was designed by Ron Rivest in 1991.
|
||||
*
|
||||
* http://www.ietf.org/rfc/rfc1321.txt
|
||||
*/
|
||||
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file md5.c
|
||||
* @author MCD Application Team
|
||||
* @brief This file has been modified to support the hardware Cryptographic and
|
||||
* Hash processors embedded in STM32F415xx/417xx/437xx/439xx/756xx devices.
|
||||
* This support is activated by defining the "USE_STM32F4XX_HW_CRYPTO"
|
||||
* or "USE_STM32F7XX_HW_CRYPTO" macro in PolarSSL config.h file.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
|
||||
* You may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.st.com/software_license_agreement_liberty_v2
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include "md5.h"
|
||||
|
||||
/*
|
||||
* 32-bit integer manipulation macros (little endian)
|
||||
*/
|
||||
#ifndef GET_UINT32_LE
|
||||
#define GET_UINT32_LE(n, b, i) \
|
||||
{ \
|
||||
(n) = ((uint32_t)(b)[(i)]) | ((uint32_t)(b)[(i) + 1] << 8) | \
|
||||
((uint32_t)(b)[(i) + 2] << 16) | ((uint32_t)(b)[(i) + 3] << 24); \
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef PUT_UINT32_LE
|
||||
#define PUT_UINT32_LE(n, b, i) \
|
||||
{ \
|
||||
(b)[(i)] = (unsigned char)((n)); \
|
||||
(b)[(i) + 1] = (unsigned char)((n) >> 8); \
|
||||
(b)[(i) + 2] = (unsigned char)((n) >> 16); \
|
||||
(b)[(i) + 3] = (unsigned char)((n) >> 24); \
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* MD5 context setup
|
||||
*/
|
||||
void md5_starts(md5_context* ctx) {
|
||||
ctx->total[0] = 0;
|
||||
ctx->total[1] = 0;
|
||||
|
||||
ctx->state[0] = 0x67452301;
|
||||
ctx->state[1] = 0xEFCDAB89;
|
||||
ctx->state[2] = 0x98BADCFE;
|
||||
ctx->state[3] = 0x10325476;
|
||||
}
|
||||
|
||||
void md5_process(md5_context* ctx, const unsigned char data[64]) {
|
||||
uint32_t X[16], A, B, C, D;
|
||||
|
||||
GET_UINT32_LE(X[0], data, 0);
|
||||
GET_UINT32_LE(X[1], data, 4);
|
||||
GET_UINT32_LE(X[2], data, 8);
|
||||
GET_UINT32_LE(X[3], data, 12);
|
||||
GET_UINT32_LE(X[4], data, 16);
|
||||
GET_UINT32_LE(X[5], data, 20);
|
||||
GET_UINT32_LE(X[6], data, 24);
|
||||
GET_UINT32_LE(X[7], data, 28);
|
||||
GET_UINT32_LE(X[8], data, 32);
|
||||
GET_UINT32_LE(X[9], data, 36);
|
||||
GET_UINT32_LE(X[10], data, 40);
|
||||
GET_UINT32_LE(X[11], data, 44);
|
||||
GET_UINT32_LE(X[12], data, 48);
|
||||
GET_UINT32_LE(X[13], data, 52);
|
||||
GET_UINT32_LE(X[14], data, 56);
|
||||
GET_UINT32_LE(X[15], data, 60);
|
||||
|
||||
#define S(x, n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
|
||||
|
||||
#define P(a, b, c, d, k, s, t) \
|
||||
{ \
|
||||
a += F(b, c, d) + X[k] + t; \
|
||||
a = S(a, s) + b; \
|
||||
}
|
||||
|
||||
A = ctx->state[0];
|
||||
B = ctx->state[1];
|
||||
C = ctx->state[2];
|
||||
D = ctx->state[3];
|
||||
|
||||
#define F(x, y, z) (z ^ (x & (y ^ z)))
|
||||
|
||||
P(A, B, C, D, 0, 7, 0xD76AA478);
|
||||
P(D, A, B, C, 1, 12, 0xE8C7B756);
|
||||
P(C, D, A, B, 2, 17, 0x242070DB);
|
||||
P(B, C, D, A, 3, 22, 0xC1BDCEEE);
|
||||
P(A, B, C, D, 4, 7, 0xF57C0FAF);
|
||||
P(D, A, B, C, 5, 12, 0x4787C62A);
|
||||
P(C, D, A, B, 6, 17, 0xA8304613);
|
||||
P(B, C, D, A, 7, 22, 0xFD469501);
|
||||
P(A, B, C, D, 8, 7, 0x698098D8);
|
||||
P(D, A, B, C, 9, 12, 0x8B44F7AF);
|
||||
P(C, D, A, B, 10, 17, 0xFFFF5BB1);
|
||||
P(B, C, D, A, 11, 22, 0x895CD7BE);
|
||||
P(A, B, C, D, 12, 7, 0x6B901122);
|
||||
P(D, A, B, C, 13, 12, 0xFD987193);
|
||||
P(C, D, A, B, 14, 17, 0xA679438E);
|
||||
P(B, C, D, A, 15, 22, 0x49B40821);
|
||||
|
||||
#undef F
|
||||
|
||||
#define F(x, y, z) (y ^ (z & (x ^ y)))
|
||||
|
||||
P(A, B, C, D, 1, 5, 0xF61E2562);
|
||||
P(D, A, B, C, 6, 9, 0xC040B340);
|
||||
P(C, D, A, B, 11, 14, 0x265E5A51);
|
||||
P(B, C, D, A, 0, 20, 0xE9B6C7AA);
|
||||
P(A, B, C, D, 5, 5, 0xD62F105D);
|
||||
P(D, A, B, C, 10, 9, 0x02441453);
|
||||
P(C, D, A, B, 15, 14, 0xD8A1E681);
|
||||
P(B, C, D, A, 4, 20, 0xE7D3FBC8);
|
||||
P(A, B, C, D, 9, 5, 0x21E1CDE6);
|
||||
P(D, A, B, C, 14, 9, 0xC33707D6);
|
||||
P(C, D, A, B, 3, 14, 0xF4D50D87);
|
||||
P(B, C, D, A, 8, 20, 0x455A14ED);
|
||||
P(A, B, C, D, 13, 5, 0xA9E3E905);
|
||||
P(D, A, B, C, 2, 9, 0xFCEFA3F8);
|
||||
P(C, D, A, B, 7, 14, 0x676F02D9);
|
||||
P(B, C, D, A, 12, 20, 0x8D2A4C8A);
|
||||
|
||||
#undef F
|
||||
|
||||
#define F(x, y, z) (x ^ y ^ z)
|
||||
|
||||
P(A, B, C, D, 5, 4, 0xFFFA3942);
|
||||
P(D, A, B, C, 8, 11, 0x8771F681);
|
||||
P(C, D, A, B, 11, 16, 0x6D9D6122);
|
||||
P(B, C, D, A, 14, 23, 0xFDE5380C);
|
||||
P(A, B, C, D, 1, 4, 0xA4BEEA44);
|
||||
P(D, A, B, C, 4, 11, 0x4BDECFA9);
|
||||
P(C, D, A, B, 7, 16, 0xF6BB4B60);
|
||||
P(B, C, D, A, 10, 23, 0xBEBFBC70);
|
||||
P(A, B, C, D, 13, 4, 0x289B7EC6);
|
||||
P(D, A, B, C, 0, 11, 0xEAA127FA);
|
||||
P(C, D, A, B, 3, 16, 0xD4EF3085);
|
||||
P(B, C, D, A, 6, 23, 0x04881D05);
|
||||
P(A, B, C, D, 9, 4, 0xD9D4D039);
|
||||
P(D, A, B, C, 12, 11, 0xE6DB99E5);
|
||||
P(C, D, A, B, 15, 16, 0x1FA27CF8);
|
||||
P(B, C, D, A, 2, 23, 0xC4AC5665);
|
||||
|
||||
#undef F
|
||||
|
||||
#define F(x, y, z) (y ^ (x | ~z))
|
||||
|
||||
P(A, B, C, D, 0, 6, 0xF4292244);
|
||||
P(D, A, B, C, 7, 10, 0x432AFF97);
|
||||
P(C, D, A, B, 14, 15, 0xAB9423A7);
|
||||
P(B, C, D, A, 5, 21, 0xFC93A039);
|
||||
P(A, B, C, D, 12, 6, 0x655B59C3);
|
||||
P(D, A, B, C, 3, 10, 0x8F0CCC92);
|
||||
P(C, D, A, B, 10, 15, 0xFFEFF47D);
|
||||
P(B, C, D, A, 1, 21, 0x85845DD1);
|
||||
P(A, B, C, D, 8, 6, 0x6FA87E4F);
|
||||
P(D, A, B, C, 15, 10, 0xFE2CE6E0);
|
||||
P(C, D, A, B, 6, 15, 0xA3014314);
|
||||
P(B, C, D, A, 13, 21, 0x4E0811A1);
|
||||
P(A, B, C, D, 4, 6, 0xF7537E82);
|
||||
P(D, A, B, C, 11, 10, 0xBD3AF235);
|
||||
P(C, D, A, B, 2, 15, 0x2AD7D2BB);
|
||||
P(B, C, D, A, 9, 21, 0xEB86D391);
|
||||
|
||||
#undef F
|
||||
|
||||
ctx->state[0] += A;
|
||||
ctx->state[1] += B;
|
||||
ctx->state[2] += C;
|
||||
ctx->state[3] += D;
|
||||
}
|
||||
|
||||
/*
|
||||
* MD5 process buffer
|
||||
*/
|
||||
void md5_update(md5_context* ctx, const unsigned char* input, size_t ilen) {
|
||||
size_t fill;
|
||||
uint32_t left;
|
||||
|
||||
if(ilen <= 0) return;
|
||||
|
||||
left = ctx->total[0] & 0x3F;
|
||||
fill = 64 - left;
|
||||
|
||||
ctx->total[0] += (uint32_t)ilen;
|
||||
ctx->total[0] &= 0xFFFFFFFF;
|
||||
|
||||
if(ctx->total[0] < (uint32_t)ilen) ctx->total[1]++;
|
||||
|
||||
if(left && ilen >= fill) {
|
||||
memcpy((void*)(ctx->buffer + left), input, fill);
|
||||
md5_process(ctx, ctx->buffer);
|
||||
input += fill;
|
||||
ilen -= fill;
|
||||
left = 0;
|
||||
}
|
||||
|
||||
while(ilen >= 64) {
|
||||
md5_process(ctx, input);
|
||||
input += 64;
|
||||
ilen -= 64;
|
||||
}
|
||||
|
||||
if(ilen > 0) {
|
||||
memcpy((void*)(ctx->buffer + left), input, ilen);
|
||||
}
|
||||
}
|
||||
|
||||
static const unsigned char md5_padding[64] = {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
|
||||
/*
|
||||
* MD5 final digest
|
||||
*/
|
||||
void md5_finish(md5_context* ctx, unsigned char output[16]) {
|
||||
uint32_t last, padn;
|
||||
uint32_t high, low;
|
||||
unsigned char msglen[8];
|
||||
|
||||
high = (ctx->total[0] >> 29) | (ctx->total[1] << 3);
|
||||
low = (ctx->total[0] << 3);
|
||||
|
||||
PUT_UINT32_LE(low, msglen, 0);
|
||||
PUT_UINT32_LE(high, msglen, 4);
|
||||
|
||||
last = ctx->total[0] & 0x3F;
|
||||
padn = (last < 56) ? (56 - last) : (120 - last);
|
||||
|
||||
md5_update(ctx, md5_padding, padn);
|
||||
md5_update(ctx, msglen, 8);
|
||||
|
||||
PUT_UINT32_LE(ctx->state[0], output, 0);
|
||||
PUT_UINT32_LE(ctx->state[1], output, 4);
|
||||
PUT_UINT32_LE(ctx->state[2], output, 8);
|
||||
PUT_UINT32_LE(ctx->state[3], output, 12);
|
||||
}
|
||||
|
||||
/*
|
||||
* output = MD5( input buffer )
|
||||
*/
|
||||
void md5(const unsigned char* input, size_t ilen, unsigned char output[16]) {
|
||||
md5_context ctx;
|
||||
|
||||
md5_starts(&ctx);
|
||||
md5_update(&ctx, input, ilen);
|
||||
md5_finish(&ctx, output);
|
||||
|
||||
memset(&ctx, 0, sizeof(md5_context));
|
||||
}
|
83
lib/toolbox/md5.h
Normal file
83
lib/toolbox/md5.h
Normal file
@ -0,0 +1,83 @@
|
||||
/**
|
||||
* \file md5.h
|
||||
*
|
||||
* \brief MD5 message digest algorithm (hash function)
|
||||
*
|
||||
* Copyright (C) 2006-2013, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* \brief MD5 context structure
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t total[2]; /*!< number of bytes processed */
|
||||
uint32_t state[4]; /*!< intermediate digest state */
|
||||
unsigned char buffer[64]; /*!< data block being processed */
|
||||
} md5_context;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief MD5 context setup
|
||||
*
|
||||
* \param ctx context to be initialized
|
||||
*/
|
||||
void md5_starts(md5_context* ctx);
|
||||
|
||||
/**
|
||||
* \brief MD5 process buffer
|
||||
*
|
||||
* \param ctx MD5 context
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
*/
|
||||
void md5_update(md5_context* ctx, const unsigned char* input, size_t ilen);
|
||||
|
||||
/**
|
||||
* \brief MD5 final digest
|
||||
*
|
||||
* \param ctx MD5 context
|
||||
* \param output MD5 checksum result
|
||||
*/
|
||||
void md5_finish(md5_context* ctx, unsigned char output[16]);
|
||||
|
||||
/* Internal use */
|
||||
void md5_process(md5_context* ctx, const unsigned char data[64]);
|
||||
|
||||
/**
|
||||
* \brief Output = MD5( input buffer )
|
||||
*
|
||||
* \param input buffer holding the data
|
||||
* \param ilen length of the input data
|
||||
* \param output MD5 checksum result
|
||||
*/
|
||||
void md5(const unsigned char* input, size_t ilen, unsigned char output[16]);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
0
scripts/flipper/__init__.py
Normal file
0
scripts/flipper/__init__.py
Normal file
375
scripts/flipper/storage.py
Normal file
375
scripts/flipper/storage.py
Normal file
@ -0,0 +1,375 @@
|
||||
import os
|
||||
import serial
|
||||
import time
|
||||
import hashlib
|
||||
import math
|
||||
|
||||
|
||||
def timing(func):
|
||||
"""
|
||||
Speedometer decorator
|
||||
"""
|
||||
|
||||
def wrapper(*args, **kwargs):
|
||||
time1 = time.monotonic()
|
||||
ret = func(*args, **kwargs)
|
||||
time2 = time.monotonic()
|
||||
print(
|
||||
"{:s} function took {:.3f} ms".format(
|
||||
func.__name__, (time2 - time1) * 1000.0
|
||||
)
|
||||
)
|
||||
return ret
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
class BufferedRead:
|
||||
def __init__(self, stream):
|
||||
self.buffer = bytearray()
|
||||
self.stream = stream
|
||||
|
||||
def until(self, eol="\n", cut_eol=True):
|
||||
eol = eol.encode("ascii")
|
||||
while True:
|
||||
# search in buffer
|
||||
i = self.buffer.find(eol)
|
||||
if i >= 0:
|
||||
if cut_eol:
|
||||
read = self.buffer[:i]
|
||||
else:
|
||||
read = self.buffer[: i + len(eol)]
|
||||
self.buffer = self.buffer[i + len(eol) :]
|
||||
return read
|
||||
|
||||
# read and append to buffer
|
||||
i = max(1, self.stream.in_waiting)
|
||||
data = self.stream.read(i)
|
||||
self.buffer.extend(data)
|
||||
|
||||
|
||||
class FlipperStorage:
|
||||
CLI_PROMPT = ">: "
|
||||
CLI_EOL = "\r\n"
|
||||
|
||||
def __init__(self, portname: str):
|
||||
self.port = serial.Serial()
|
||||
self.port.port = portname
|
||||
self.port.timeout = 2
|
||||
self.port.baudrate = 115200
|
||||
self.read = BufferedRead(self.port)
|
||||
self.last_error = ""
|
||||
|
||||
def start(self):
|
||||
self.port.open()
|
||||
self.port.reset_input_buffer()
|
||||
# Send a command with a known syntax to make sure the buffer is flushed
|
||||
self.send("device_info\r")
|
||||
self.read.until("hardware_model :")
|
||||
# And read buffer until we get prompt
|
||||
self.read.until(self.CLI_PROMPT)
|
||||
|
||||
def stop(self):
|
||||
self.port.close()
|
||||
|
||||
def send(self, line):
|
||||
self.port.write(line.encode("ascii"))
|
||||
|
||||
def send_and_wait_eol(self, line):
|
||||
self.send(line)
|
||||
return self.read.until(self.CLI_EOL)
|
||||
|
||||
def send_and_wait_prompt(self, line):
|
||||
self.send(line)
|
||||
return self.read.until(self.CLI_PROMPT)
|
||||
|
||||
def has_error(self, data):
|
||||
"""Is data has error"""
|
||||
if data.find(b"Storage error") != -1:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def get_error(self, data):
|
||||
"""Extract error text from data and print it"""
|
||||
error, error_text = data.decode("ascii").split(": ")
|
||||
return error_text.strip()
|
||||
|
||||
def list_tree(self, path="/", level=0):
|
||||
"""List files and dirs on Flipper"""
|
||||
path = path.replace("//", "/")
|
||||
|
||||
self.send_and_wait_eol('storage list "' + path + '"\r')
|
||||
|
||||
data = self.read.until(self.CLI_PROMPT)
|
||||
lines = data.split(b"\r\n")
|
||||
|
||||
for line in lines:
|
||||
try:
|
||||
# TODO: better decoding, considering non-ascii characters
|
||||
line = line.decode("ascii")
|
||||
except:
|
||||
continue
|
||||
|
||||
line = line.strip()
|
||||
|
||||
if len(line) == 0:
|
||||
continue
|
||||
|
||||
if self.has_error(line.encode("ascii")):
|
||||
print(self.get_error(line.encode("ascii")))
|
||||
continue
|
||||
|
||||
if line == "Empty":
|
||||
continue
|
||||
|
||||
type, info = line.split(" ", 1)
|
||||
if type == "[D]":
|
||||
# Print directory name
|
||||
print((path + "/" + info).replace("//", "/"))
|
||||
# And recursively go inside
|
||||
self.list_tree(path + "/" + info, level + 1)
|
||||
elif type == "[F]":
|
||||
name, size = info.rsplit(" ", 1)
|
||||
# Print file name and size
|
||||
print((path + "/" + name).replace("//", "/") + ", size " + size)
|
||||
else:
|
||||
# Something wrong, pass
|
||||
pass
|
||||
|
||||
def walk(self, path="/"):
|
||||
dirs = []
|
||||
nondirs = []
|
||||
walk_dirs = []
|
||||
|
||||
path = path.replace("//", "/")
|
||||
self.send_and_wait_eol('storage list "' + path + '"\r')
|
||||
data = self.read.until(self.CLI_PROMPT)
|
||||
lines = data.split(b"\r\n")
|
||||
|
||||
for line in lines:
|
||||
try:
|
||||
# TODO: better decoding, considering non-ascii characters
|
||||
line = line.decode("ascii")
|
||||
except:
|
||||
continue
|
||||
|
||||
line = line.strip()
|
||||
|
||||
if len(line) == 0:
|
||||
continue
|
||||
|
||||
if self.has_error(line.encode("ascii")):
|
||||
continue
|
||||
|
||||
if line == "Empty":
|
||||
continue
|
||||
|
||||
type, info = line.split(" ", 1)
|
||||
if type == "[D]":
|
||||
# Print directory name
|
||||
dirs.append(info)
|
||||
walk_dirs.append((path + "/" + info).replace("//", "/"))
|
||||
|
||||
elif type == "[F]":
|
||||
name, size = info.rsplit(" ", 1)
|
||||
# Print file name and size
|
||||
nondirs.append(name)
|
||||
else:
|
||||
# Something wrong, pass
|
||||
pass
|
||||
|
||||
# topdown walk, yield before recursy
|
||||
yield path, dirs, nondirs
|
||||
for new_path in walk_dirs:
|
||||
yield from self.walk(new_path)
|
||||
|
||||
def send_file(self, filename_from, filename_to):
|
||||
"""Send file from local device to Flipper"""
|
||||
self.remove(filename_to)
|
||||
|
||||
file = open(filename_from, "rb")
|
||||
filesize = os.fstat(file.fileno()).st_size
|
||||
|
||||
buffer_size = 512
|
||||
while True:
|
||||
filedata = file.read(buffer_size)
|
||||
size = len(filedata)
|
||||
if size == 0:
|
||||
break
|
||||
|
||||
self.send_and_wait_eol(
|
||||
'storage write_chunk "' + filename_to + '" ' + str(size) + "\r"
|
||||
)
|
||||
answer = self.read.until(self.CLI_EOL)
|
||||
if self.has_error(answer):
|
||||
self.last_error = self.get_error(answer)
|
||||
self.read.until(self.CLI_PROMPT)
|
||||
file.close()
|
||||
return False
|
||||
|
||||
self.port.write(filedata)
|
||||
self.read.until(self.CLI_PROMPT)
|
||||
|
||||
percent = str(math.ceil(file.tell() / filesize * 100))
|
||||
total_chunks = str(math.ceil(filesize / buffer_size))
|
||||
current_chunk = str(math.ceil(file.tell() / buffer_size))
|
||||
print(
|
||||
percent + "%, chunk " + current_chunk + " of " + total_chunks, end="\r"
|
||||
)
|
||||
file.close()
|
||||
print()
|
||||
return True
|
||||
|
||||
def read_file(self, filename):
|
||||
"""Receive file from Flipper, and get filedata (bytes)"""
|
||||
buffer_size = 512
|
||||
self.send_and_wait_eol(
|
||||
'storage read_chunks "' + filename + '" ' + str(buffer_size) + "\r"
|
||||
)
|
||||
answer = self.read.until(self.CLI_EOL)
|
||||
filedata = bytearray()
|
||||
if self.has_error(answer):
|
||||
self.last_error = self.get_error(answer)
|
||||
self.read.until(self.CLI_PROMPT)
|
||||
return filedata
|
||||
size = int(answer.split(b": ")[1])
|
||||
readed_size = 0
|
||||
|
||||
while readed_size < size:
|
||||
self.read.until("Ready?" + self.CLI_EOL)
|
||||
self.send("y")
|
||||
read_size = min(size - readed_size, buffer_size)
|
||||
filedata.extend(self.port.read(read_size))
|
||||
readed_size = readed_size + read_size
|
||||
|
||||
percent = str(math.ceil(readed_size / size * 100))
|
||||
total_chunks = str(math.ceil(size / buffer_size))
|
||||
current_chunk = str(math.ceil(readed_size / buffer_size))
|
||||
print(
|
||||
percent + "%, chunk " + current_chunk + " of " + total_chunks, end="\r"
|
||||
)
|
||||
print()
|
||||
self.read.until(self.CLI_PROMPT)
|
||||
return filedata
|
||||
|
||||
def receive_file(self, filename_from, filename_to):
|
||||
"""Receive file from Flipper to local storage"""
|
||||
with open(filename_to, "wb") as file:
|
||||
data = self.read_file(filename_from)
|
||||
if not data:
|
||||
return False
|
||||
else:
|
||||
file.write(data)
|
||||
return True
|
||||
|
||||
def exist(self, path):
|
||||
"""Is file or dir exist on Flipper"""
|
||||
self.send_and_wait_eol('storage stat "' + path + '"\r')
|
||||
answer = self.read.until(self.CLI_EOL)
|
||||
self.read.until(self.CLI_PROMPT)
|
||||
|
||||
if self.has_error(answer):
|
||||
self.last_error = self.get_error(answer)
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
def exist_dir(self, path):
|
||||
"""Is dir exist on Flipper"""
|
||||
self.send_and_wait_eol('storage stat "' + path + '"\r')
|
||||
answer = self.read.until(self.CLI_EOL)
|
||||
self.read.until(self.CLI_PROMPT)
|
||||
|
||||
if self.has_error(answer):
|
||||
self.last_error = self.get_error(answer)
|
||||
return False
|
||||
else:
|
||||
if answer.find(b"Directory") != -1:
|
||||
return True
|
||||
elif answer.find(b"Storage") != -1:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def exist_file(self, path):
|
||||
"""Is file exist on Flipper"""
|
||||
self.send_and_wait_eol('storage stat "' + path + '"\r')
|
||||
answer = self.read.until(self.CLI_EOL)
|
||||
self.read.until(self.CLI_PROMPT)
|
||||
|
||||
if self.has_error(answer):
|
||||
self.last_error = self.get_error(answer)
|
||||
return False
|
||||
else:
|
||||
if answer.find(b"File, size:") != -1:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def size(self, path):
|
||||
"""file size on Flipper"""
|
||||
self.send_and_wait_eol('storage stat "' + path + '"\r')
|
||||
answer = self.read.until(self.CLI_EOL)
|
||||
self.read.until(self.CLI_PROMPT)
|
||||
|
||||
if self.has_error(answer):
|
||||
self.last_error = self.get_error(answer)
|
||||
return False
|
||||
else:
|
||||
if answer.find(b"File, size:") != -1:
|
||||
size = int(
|
||||
"".join(
|
||||
ch
|
||||
for ch in answer.split(b": ")[1].decode("ascii")
|
||||
if ch.isdigit()
|
||||
)
|
||||
)
|
||||
return size
|
||||
else:
|
||||
self.last_error = "access denied"
|
||||
return -1
|
||||
|
||||
def mkdir(self, path):
|
||||
"""Create a directory on Flipper"""
|
||||
self.send_and_wait_eol('storage mkdir "' + path + '"\r')
|
||||
answer = self.read.until(self.CLI_EOL)
|
||||
self.read.until(self.CLI_PROMPT)
|
||||
|
||||
if self.has_error(answer):
|
||||
self.last_error = self.get_error(answer)
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
def remove(self, path):
|
||||
"""Remove file or directory on Flipper"""
|
||||
self.send_and_wait_eol('storage remove "' + path + '"\r')
|
||||
answer = self.read.until(self.CLI_EOL)
|
||||
self.read.until(self.CLI_PROMPT)
|
||||
|
||||
if self.has_error(answer):
|
||||
self.last_error = self.get_error(answer)
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
def hash_local(self, filename):
|
||||
"""Hash of local file"""
|
||||
hash_md5 = hashlib.md5()
|
||||
with open(filename, "rb") as f:
|
||||
for chunk in iter(lambda: f.read(4096), b""):
|
||||
hash_md5.update(chunk)
|
||||
return hash_md5.hexdigest()
|
||||
|
||||
def hash_flipper(self, filename):
|
||||
"""Get hash of file on Flipper"""
|
||||
self.send_and_wait_eol('storage md5 "' + filename + '"\r')
|
||||
hash = self.read.until(self.CLI_EOL)
|
||||
self.read.until(self.CLI_PROMPT)
|
||||
|
||||
if self.has_error(hash):
|
||||
self.last_error = self.get_error(hash)
|
||||
return ""
|
||||
else:
|
||||
return hash.decode("ascii")
|
267
scripts/storage.py
Executable file
267
scripts/storage.py
Executable file
@ -0,0 +1,267 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from flipper.storage import FlipperStorage
|
||||
import logging
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
import binascii
|
||||
import posixpath
|
||||
|
||||
|
||||
class Main:
|
||||
def __init__(self):
|
||||
# command args
|
||||
self.parser = argparse.ArgumentParser()
|
||||
self.parser.add_argument("-d", "--debug", action="store_true", help="Debug")
|
||||
self.parser.add_argument("-p", "--port", help="CDC Port", required=True)
|
||||
self.subparsers = self.parser.add_subparsers(help="sub-command help")
|
||||
|
||||
self.parser_mkdir = self.subparsers.add_parser("mkdir", help="Create directory")
|
||||
self.parser_mkdir.add_argument("flipper_path", help="Flipper path")
|
||||
self.parser_mkdir.set_defaults(func=self.mkdir)
|
||||
|
||||
self.parser_remove = self.subparsers.add_parser(
|
||||
"remove", help="Remove file/directory"
|
||||
)
|
||||
self.parser_remove.add_argument("flipper_path", help="Flipper path")
|
||||
self.parser_remove.set_defaults(func=self.remove)
|
||||
|
||||
self.parser_read = self.subparsers.add_parser("read", help="Read file")
|
||||
self.parser_read.add_argument("flipper_path", help="Flipper path")
|
||||
self.parser_read.set_defaults(func=self.read)
|
||||
|
||||
self.parser_size = self.subparsers.add_parser("size", help="Size of file")
|
||||
self.parser_size.add_argument("flipper_path", help="Flipper path")
|
||||
self.parser_size.set_defaults(func=self.size)
|
||||
|
||||
self.parser_receive = self.subparsers.add_parser("receive", help="Receive file")
|
||||
self.parser_receive.add_argument("flipper_path", help="Flipper path")
|
||||
self.parser_receive.add_argument("local_path", help="Local path")
|
||||
self.parser_receive.set_defaults(func=self.receive)
|
||||
|
||||
self.parser_send = self.subparsers.add_parser(
|
||||
"send", help="Send file or directory"
|
||||
)
|
||||
self.parser_send.add_argument(
|
||||
"-f", "--force", help="Force sending", action="store_true"
|
||||
)
|
||||
self.parser_send.add_argument("local_path", help="Local path")
|
||||
self.parser_send.add_argument("flipper_path", help="Flipper path")
|
||||
self.parser_send.set_defaults(func=self.send)
|
||||
|
||||
self.parser_list = self.subparsers.add_parser(
|
||||
"list", help="Recursively list files and dirs"
|
||||
)
|
||||
self.parser_list.add_argument("flipper_path", help="Flipper path", default="/")
|
||||
self.parser_list.set_defaults(func=self.list)
|
||||
|
||||
# logging
|
||||
self.logger = logging.getLogger()
|
||||
|
||||
def __call__(self):
|
||||
self.args = self.parser.parse_args()
|
||||
if "func" not in self.args:
|
||||
self.parser.error("Choose something to do")
|
||||
# configure log output
|
||||
self.log_level = logging.DEBUG if self.args.debug else logging.INFO
|
||||
self.logger.setLevel(self.log_level)
|
||||
self.handler = logging.StreamHandler(sys.stdout)
|
||||
self.handler.setLevel(self.log_level)
|
||||
self.formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s")
|
||||
self.handler.setFormatter(self.formatter)
|
||||
self.logger.addHandler(self.handler)
|
||||
# execute requested function
|
||||
self.args.func()
|
||||
|
||||
def mkdir(self):
|
||||
storage = FlipperStorage(self.args.port)
|
||||
storage.start()
|
||||
self.logger.debug(f'Creating "{self.args.flipper_path}"')
|
||||
if not storage.mkdir(self.args.flipper_path):
|
||||
self.logger.error(f"Error: {storage.last_error}")
|
||||
storage.stop()
|
||||
|
||||
def remove(self):
|
||||
storage = FlipperStorage(self.args.port)
|
||||
storage.start()
|
||||
self.logger.debug(f'Removing "{self.args.flipper_path}"')
|
||||
if not storage.remove(self.args.flipper_path):
|
||||
self.logger.error(f"Error: {storage.last_error}")
|
||||
storage.stop()
|
||||
|
||||
def receive(self):
|
||||
storage = FlipperStorage(self.args.port)
|
||||
storage.start()
|
||||
|
||||
if storage.exist_dir(self.args.flipper_path):
|
||||
for dirpath, dirnames, filenames in storage.walk(self.args.flipper_path):
|
||||
self.logger.debug(
|
||||
f'Processing directory "{os.path.normpath(dirpath)}"'.replace(
|
||||
os.sep, "/"
|
||||
)
|
||||
)
|
||||
dirnames.sort()
|
||||
filenames.sort()
|
||||
|
||||
rel_path = os.path.relpath(dirpath, self.args.flipper_path)
|
||||
|
||||
for dirname in dirnames:
|
||||
local_dir_path = os.path.join(
|
||||
self.args.local_path, rel_path, dirname
|
||||
)
|
||||
local_dir_path = os.path.normpath(local_dir_path)
|
||||
os.makedirs(local_dir_path, exist_ok=True)
|
||||
|
||||
for filename in filenames:
|
||||
local_file_path = os.path.join(
|
||||
self.args.local_path, rel_path, filename
|
||||
)
|
||||
local_file_path = os.path.normpath(local_file_path)
|
||||
flipper_file_path = os.path.normpath(
|
||||
os.path.join(dirpath, filename)
|
||||
).replace(os.sep, "/")
|
||||
self.logger.info(
|
||||
f'Receiving "{flipper_file_path}" to "{local_file_path}"'
|
||||
)
|
||||
if not storage.receive_file(flipper_file_path, local_file_path):
|
||||
self.logger.error(f"Error: {storage.last_error}")
|
||||
|
||||
else:
|
||||
self.logger.info(
|
||||
f'Receiving "{self.args.flipper_path}" to "{self.args.local_path}"'
|
||||
)
|
||||
if not storage.receive_file(self.args.flipper_path, self.args.local_path):
|
||||
self.logger.error(f"Error: {storage.last_error}")
|
||||
storage.stop()
|
||||
|
||||
def send(self):
|
||||
storage = FlipperStorage(self.args.port)
|
||||
storage.start()
|
||||
self.send_to_storage(
|
||||
storage, self.args.flipper_path, self.args.local_path, self.args.force
|
||||
)
|
||||
storage.stop()
|
||||
|
||||
# send file or folder recursively
|
||||
def send_to_storage(self, storage, flipper_path, local_path, force):
|
||||
if not os.path.exists(local_path):
|
||||
self.logger.error(f'Error: "{local_path}" is not exist')
|
||||
|
||||
if os.path.isdir(local_path):
|
||||
# create parent dir
|
||||
self.mkdir_on_storage(storage, flipper_path)
|
||||
|
||||
for dirpath, dirnames, filenames in os.walk(local_path):
|
||||
self.logger.debug(f'Processing directory "{os.path.normpath(dirpath)}"')
|
||||
dirnames.sort()
|
||||
filenames.sort()
|
||||
rel_path = os.path.relpath(dirpath, local_path)
|
||||
|
||||
# create subdirs
|
||||
for dirname in dirnames:
|
||||
flipper_dir_path = os.path.join(flipper_path, rel_path, dirname)
|
||||
flipper_dir_path = os.path.normpath(flipper_dir_path).replace(
|
||||
os.sep, "/"
|
||||
)
|
||||
self.mkdir_on_storage(storage, flipper_dir_path)
|
||||
|
||||
# send files
|
||||
for filename in filenames:
|
||||
flipper_file_path = os.path.join(flipper_path, rel_path, filename)
|
||||
flipper_file_path = os.path.normpath(flipper_file_path).replace(
|
||||
os.sep, "/"
|
||||
)
|
||||
local_file_path = os.path.normpath(os.path.join(dirpath, filename))
|
||||
self.send_file_to_storage(
|
||||
storage, flipper_file_path, local_file_path, force
|
||||
)
|
||||
else:
|
||||
self.send_file_to_storage(storage, flipper_path, local_path, force)
|
||||
|
||||
# make directory with exist check
|
||||
def mkdir_on_storage(self, storage, flipper_dir_path):
|
||||
if not storage.exist_dir(flipper_dir_path):
|
||||
self.logger.debug(f'"{flipper_dir_path}" not exist, creating')
|
||||
if not storage.mkdir(flipper_dir_path):
|
||||
self.logger.error(f"Error: {storage.last_error}")
|
||||
else:
|
||||
self.logger.debug(f'"{flipper_dir_path}" already exist')
|
||||
|
||||
# send file with exist check and hash check
|
||||
def send_file_to_storage(self, storage, flipper_file_path, local_file_path, force):
|
||||
if not storage.exist_file(flipper_file_path):
|
||||
self.logger.debug(
|
||||
f'"{flipper_file_path}" not exist, sending "{local_file_path}"'
|
||||
)
|
||||
self.logger.info(f'Sending "{local_file_path}" to "{flipper_file_path}"')
|
||||
if not storage.send_file(local_file_path, flipper_file_path):
|
||||
self.logger.error(f"Error: {storage.last_error}")
|
||||
elif force:
|
||||
self.logger.debug(
|
||||
f'"{flipper_file_path}" exist, but will be overwritten by "{local_file_path}"'
|
||||
)
|
||||
self.logger.info(f'Sending "{local_file_path}" to "{flipper_file_path}"')
|
||||
if not storage.send_file(local_file_path, flipper_file_path):
|
||||
self.logger.error(f"Error: {storage.last_error}")
|
||||
else:
|
||||
self.logger.debug(
|
||||
f'"{flipper_file_path}" exist, compare hash with "{local_file_path}"'
|
||||
)
|
||||
hash_local = storage.hash_local(local_file_path)
|
||||
hash_flipper = storage.hash_flipper(flipper_file_path)
|
||||
|
||||
if not hash_flipper:
|
||||
self.logger.error(f"Error: {storage.last_error}")
|
||||
|
||||
if hash_local == hash_flipper:
|
||||
self.logger.debug(
|
||||
f'"{flipper_file_path}" are equal to "{local_file_path}"'
|
||||
)
|
||||
else:
|
||||
self.logger.debug(
|
||||
f'"{flipper_file_path}" are not equal to "{local_file_path}"'
|
||||
)
|
||||
self.logger.info(
|
||||
f'Sending "{local_file_path}" to "{flipper_file_path}"'
|
||||
)
|
||||
if not storage.send_file(local_file_path, flipper_file_path):
|
||||
self.logger.error(f"Error: {storage.last_error}")
|
||||
|
||||
def read(self):
|
||||
storage = FlipperStorage(self.args.port)
|
||||
storage.start()
|
||||
self.logger.debug(f'Reading "{self.args.flipper_path}"')
|
||||
data = storage.read_file(self.args.flipper_path)
|
||||
if not data:
|
||||
self.logger.error(f"Error: {storage.last_error}")
|
||||
else:
|
||||
try:
|
||||
print("Text data:")
|
||||
print(data.decode())
|
||||
except:
|
||||
print("Binary hexadecimal data:")
|
||||
print(binascii.hexlify(data).decode())
|
||||
storage.stop()
|
||||
|
||||
def size(self):
|
||||
storage = FlipperStorage(self.args.port)
|
||||
storage.start()
|
||||
self.logger.debug(f'Getting size of "{self.args.flipper_path}"')
|
||||
size = storage.size(self.args.flipper_path)
|
||||
if size < 0:
|
||||
self.logger.error(f"Error: {storage.last_error}")
|
||||
else:
|
||||
print(size)
|
||||
storage.stop()
|
||||
|
||||
def list(self):
|
||||
storage = FlipperStorage(self.args.port)
|
||||
storage.start()
|
||||
self.logger.debug(f'Listing "{self.args.flipper_path}"')
|
||||
storage.list_tree(self.args.flipper_path)
|
||||
storage.stop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
Main()()
|
Loading…
Reference in New Issue
Block a user