storage_cli.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <cli/cli.h>
  4. #include <lib/toolbox/args.h>
  5. #include <lib/toolbox/md5.h>
  6. #include <lib/toolbox/dir_walk.h>
  7. #include <storage/storage.h>
  8. #include <storage/storage_sd_api.h>
  9. #include <power/power_service/power.h>
  10. #define MAX_NAME_LENGTH 255
  11. static void storage_cli_print_usage() {
  12. printf("Usage:\r\n");
  13. printf("storage <cmd> <path> <args>\r\n");
  14. printf("The path must start with /int or /ext\r\n");
  15. printf("Cmd list:\r\n");
  16. printf("\tinfo\t - get FS info\r\n");
  17. printf("\tformat\t - format filesystem\r\n");
  18. printf("\tlist\t - list files and dirs\r\n");
  19. printf("\ttree\t - list files and dirs, recursive\r\n");
  20. printf("\tremove\t - delete the file or directory\r\n");
  21. printf("\tread\t - read text from file and print file size and content to cli\r\n");
  22. printf(
  23. "\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");
  24. printf("\twrite\t - read text from cli and append it to file, stops by ctrl+c\r\n");
  25. printf(
  26. "\twrite_chunk\t - read data from cli and append it to file, <args> should contain how many bytes you want to write\r\n");
  27. printf("\tcopy\t - copy file to new file, <args> must contain new path\r\n");
  28. printf("\trename\t - move file to new file, <args> must contain new path\r\n");
  29. printf("\tmkdir\t - creates a new directory\r\n");
  30. printf("\tmd5\t - md5 hash of the file\r\n");
  31. printf("\tstat\t - info about file or dir\r\n");
  32. };
  33. static void storage_cli_print_error(FS_Error error) {
  34. printf("Storage error: %s\r\n", storage_error_get_desc(error));
  35. }
  36. static void storage_cli_info(Cli* cli, FuriString* path) {
  37. UNUSED(cli);
  38. Storage* api = furi_record_open(RECORD_STORAGE);
  39. if(furi_string_cmp_str(path, STORAGE_INT_PATH_PREFIX) == 0) {
  40. uint64_t total_space;
  41. uint64_t free_space;
  42. FS_Error error =
  43. storage_common_fs_info(api, STORAGE_INT_PATH_PREFIX, &total_space, &free_space);
  44. if(error != FSE_OK) {
  45. storage_cli_print_error(error);
  46. } else {
  47. printf(
  48. "Label: %s\r\nType: LittleFS\r\n%luKiB total\r\n%luKiB free\r\n",
  49. furi_hal_version_get_name_ptr() ? furi_hal_version_get_name_ptr() : "Unknown",
  50. (uint32_t)(total_space / 1024),
  51. (uint32_t)(free_space / 1024));
  52. }
  53. } else if(furi_string_cmp_str(path, STORAGE_EXT_PATH_PREFIX) == 0) {
  54. SDInfo sd_info;
  55. FS_Error error = storage_sd_info(api, &sd_info);
  56. if(error != FSE_OK) {
  57. storage_cli_print_error(error);
  58. } else {
  59. printf(
  60. "Label: %s\r\nType: %s\r\n%luKiB total\r\n%luKiB free\r\n",
  61. sd_info.label,
  62. sd_api_get_fs_type_text(sd_info.fs_type),
  63. sd_info.kb_total,
  64. sd_info.kb_free);
  65. }
  66. } else {
  67. storage_cli_print_usage();
  68. }
  69. furi_record_close(RECORD_STORAGE);
  70. };
  71. static void storage_cli_format(Cli* cli, FuriString* path) {
  72. if(furi_string_cmp_str(path, STORAGE_INT_PATH_PREFIX) == 0) {
  73. storage_cli_print_error(FSE_NOT_IMPLEMENTED);
  74. } else if(furi_string_cmp_str(path, STORAGE_EXT_PATH_PREFIX) == 0) {
  75. printf("Formatting SD card, All data will be lost! Are you sure (y/n)?\r\n");
  76. char answer = cli_getc(cli);
  77. if(answer == 'y' || answer == 'Y') {
  78. Storage* api = furi_record_open(RECORD_STORAGE);
  79. printf("Formatting, please wait...\r\n");
  80. FS_Error error = storage_sd_format(api);
  81. if(error != FSE_OK) {
  82. storage_cli_print_error(error);
  83. } else {
  84. printf("SD card was successfully formatted.\r\n");
  85. }
  86. furi_record_close(RECORD_STORAGE);
  87. } else {
  88. printf("Cancelled.\r\n");
  89. }
  90. } else {
  91. storage_cli_print_usage();
  92. }
  93. };
  94. static void storage_cli_list(Cli* cli, FuriString* path) {
  95. UNUSED(cli);
  96. if(furi_string_cmp_str(path, "/") == 0) {
  97. printf("\t[D] int\r\n");
  98. printf("\t[D] ext\r\n");
  99. printf("\t[D] any\r\n");
  100. } else {
  101. Storage* api = furi_record_open(RECORD_STORAGE);
  102. File* file = storage_file_alloc(api);
  103. if(storage_dir_open(file, furi_string_get_cstr(path))) {
  104. FileInfo fileinfo;
  105. char name[MAX_NAME_LENGTH];
  106. bool read_done = false;
  107. while(storage_dir_read(file, &fileinfo, name, MAX_NAME_LENGTH)) {
  108. read_done = true;
  109. if(fileinfo.flags & FSF_DIRECTORY) {
  110. printf("\t[D] %s\r\n", name);
  111. } else {
  112. printf("\t[F] %s %lub\r\n", name, (uint32_t)(fileinfo.size));
  113. }
  114. }
  115. if(!read_done) {
  116. printf("\tEmpty\r\n");
  117. }
  118. } else {
  119. storage_cli_print_error(storage_file_get_error(file));
  120. }
  121. storage_dir_close(file);
  122. storage_file_free(file);
  123. furi_record_close(RECORD_STORAGE);
  124. }
  125. }
  126. static void storage_cli_tree(Cli* cli, FuriString* path) {
  127. if(furi_string_cmp_str(path, "/") == 0) {
  128. furi_string_set(path, STORAGE_INT_PATH_PREFIX);
  129. storage_cli_tree(cli, path);
  130. furi_string_set(path, STORAGE_EXT_PATH_PREFIX);
  131. storage_cli_tree(cli, path);
  132. } else {
  133. Storage* api = furi_record_open(RECORD_STORAGE);
  134. DirWalk* dir_walk = dir_walk_alloc(api);
  135. FuriString* name;
  136. name = furi_string_alloc();
  137. if(dir_walk_open(dir_walk, furi_string_get_cstr(path))) {
  138. FileInfo fileinfo;
  139. bool read_done = false;
  140. while(dir_walk_read(dir_walk, name, &fileinfo) == DirWalkOK) {
  141. read_done = true;
  142. if(fileinfo.flags & FSF_DIRECTORY) {
  143. printf("\t[D] %s\r\n", furi_string_get_cstr(name));
  144. } else {
  145. printf(
  146. "\t[F] %s %lub\r\n",
  147. furi_string_get_cstr(name),
  148. (uint32_t)(fileinfo.size));
  149. }
  150. }
  151. if(!read_done) {
  152. printf("\tEmpty\r\n");
  153. }
  154. } else {
  155. storage_cli_print_error(dir_walk_get_error(dir_walk));
  156. }
  157. furi_string_free(name);
  158. dir_walk_free(dir_walk);
  159. furi_record_close(RECORD_STORAGE);
  160. }
  161. }
  162. static void storage_cli_read(Cli* cli, FuriString* path) {
  163. UNUSED(cli);
  164. Storage* api = furi_record_open(RECORD_STORAGE);
  165. File* file = storage_file_alloc(api);
  166. if(storage_file_open(file, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  167. const uint16_t buffer_size = 128;
  168. uint16_t read_size = 0;
  169. uint8_t* data = malloc(buffer_size);
  170. printf("Size: %lu\r\n", (uint32_t)storage_file_size(file));
  171. do {
  172. read_size = storage_file_read(file, data, buffer_size);
  173. for(uint16_t i = 0; i < read_size; i++) {
  174. printf("%c", data[i]);
  175. }
  176. } while(read_size > 0);
  177. printf("\r\n");
  178. free(data);
  179. } else {
  180. storage_cli_print_error(storage_file_get_error(file));
  181. }
  182. storage_file_close(file);
  183. storage_file_free(file);
  184. furi_record_close(RECORD_STORAGE);
  185. }
  186. static void storage_cli_write(Cli* cli, FuriString* path) {
  187. Storage* api = furi_record_open(RECORD_STORAGE);
  188. File* file = storage_file_alloc(api);
  189. const uint16_t buffer_size = 512;
  190. uint8_t* buffer = malloc(buffer_size);
  191. if(storage_file_open(file, furi_string_get_cstr(path), FSAM_WRITE, FSOM_OPEN_APPEND)) {
  192. printf("Just write your text data. New line by Ctrl+Enter, exit by Ctrl+C.\r\n");
  193. uint32_t read_index = 0;
  194. while(true) {
  195. uint8_t symbol = cli_getc(cli);
  196. if(symbol == CliSymbolAsciiETX) {
  197. uint16_t write_size = read_index % buffer_size;
  198. if(write_size > 0) {
  199. uint16_t written_size = storage_file_write(file, buffer, write_size);
  200. if(written_size != write_size) {
  201. storage_cli_print_error(storage_file_get_error(file));
  202. }
  203. break;
  204. }
  205. }
  206. buffer[read_index % buffer_size] = symbol;
  207. printf("%c", buffer[read_index % buffer_size]);
  208. fflush(stdout);
  209. read_index++;
  210. if(((read_index % buffer_size) == 0)) {
  211. uint16_t written_size = storage_file_write(file, buffer, buffer_size);
  212. if(written_size != buffer_size) {
  213. storage_cli_print_error(storage_file_get_error(file));
  214. break;
  215. }
  216. }
  217. }
  218. printf("\r\n");
  219. } else {
  220. storage_cli_print_error(storage_file_get_error(file));
  221. }
  222. storage_file_close(file);
  223. free(buffer);
  224. storage_file_free(file);
  225. furi_record_close(RECORD_STORAGE);
  226. }
  227. static void storage_cli_read_chunks(Cli* cli, FuriString* path, FuriString* args) {
  228. Storage* api = furi_record_open(RECORD_STORAGE);
  229. File* file = storage_file_alloc(api);
  230. uint32_t buffer_size;
  231. int parsed_count = sscanf(furi_string_get_cstr(args), "%lu", &buffer_size);
  232. if(parsed_count == EOF || parsed_count != 1) {
  233. storage_cli_print_usage();
  234. } else if(storage_file_open(file, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  235. uint64_t file_size = storage_file_size(file);
  236. printf("Size: %lu\r\n", (uint32_t)file_size);
  237. if(buffer_size) {
  238. uint8_t* data = malloc(buffer_size);
  239. while(file_size > 0) {
  240. printf("\r\nReady?\r\n");
  241. cli_getc(cli);
  242. uint16_t read_size = storage_file_read(file, data, buffer_size);
  243. for(uint16_t i = 0; i < read_size; i++) {
  244. putchar(data[i]);
  245. }
  246. file_size -= read_size;
  247. }
  248. free(data);
  249. }
  250. printf("\r\n");
  251. } else {
  252. storage_cli_print_error(storage_file_get_error(file));
  253. }
  254. storage_file_close(file);
  255. storage_file_free(file);
  256. furi_record_close(RECORD_STORAGE);
  257. }
  258. static void storage_cli_write_chunk(Cli* cli, FuriString* path, FuriString* args) {
  259. Storage* api = furi_record_open(RECORD_STORAGE);
  260. File* file = storage_file_alloc(api);
  261. uint32_t buffer_size;
  262. int parsed_count = sscanf(furi_string_get_cstr(args), "%lu", &buffer_size);
  263. if(parsed_count == EOF || parsed_count != 1) {
  264. storage_cli_print_usage();
  265. } else {
  266. if(storage_file_open(file, furi_string_get_cstr(path), FSAM_WRITE, FSOM_OPEN_APPEND)) {
  267. printf("Ready\r\n");
  268. if(buffer_size) {
  269. uint8_t* buffer = malloc(buffer_size);
  270. for(uint32_t i = 0; i < buffer_size; i++) {
  271. buffer[i] = cli_getc(cli);
  272. }
  273. uint16_t written_size = storage_file_write(file, buffer, buffer_size);
  274. if(written_size != buffer_size) {
  275. storage_cli_print_error(storage_file_get_error(file));
  276. }
  277. free(buffer);
  278. }
  279. } else {
  280. storage_cli_print_error(storage_file_get_error(file));
  281. }
  282. storage_file_close(file);
  283. }
  284. storage_file_free(file);
  285. furi_record_close(RECORD_STORAGE);
  286. }
  287. static void storage_cli_stat(Cli* cli, FuriString* path) {
  288. UNUSED(cli);
  289. Storage* api = furi_record_open(RECORD_STORAGE);
  290. if(furi_string_cmp_str(path, "/") == 0) {
  291. printf("Storage\r\n");
  292. } else if(
  293. furi_string_cmp_str(path, STORAGE_EXT_PATH_PREFIX) == 0 ||
  294. furi_string_cmp_str(path, STORAGE_INT_PATH_PREFIX) == 0 ||
  295. furi_string_cmp_str(path, STORAGE_ANY_PATH_PREFIX) == 0) {
  296. uint64_t total_space;
  297. uint64_t free_space;
  298. FS_Error error =
  299. storage_common_fs_info(api, furi_string_get_cstr(path), &total_space, &free_space);
  300. if(error != FSE_OK) {
  301. storage_cli_print_error(error);
  302. } else {
  303. printf(
  304. "Storage, %luKiB total, %luKiB free\r\n",
  305. (uint32_t)(total_space / 1024),
  306. (uint32_t)(free_space / 1024));
  307. }
  308. } else {
  309. FileInfo fileinfo;
  310. FS_Error error = storage_common_stat(api, furi_string_get_cstr(path), &fileinfo);
  311. if(error == FSE_OK) {
  312. if(fileinfo.flags & FSF_DIRECTORY) {
  313. printf("Directory\r\n");
  314. } else {
  315. printf("File, size: %lub\r\n", (uint32_t)(fileinfo.size));
  316. }
  317. } else {
  318. storage_cli_print_error(error);
  319. }
  320. }
  321. furi_record_close(RECORD_STORAGE);
  322. }
  323. static void storage_cli_copy(Cli* cli, FuriString* old_path, FuriString* args) {
  324. UNUSED(cli);
  325. Storage* api = furi_record_open(RECORD_STORAGE);
  326. FuriString* new_path;
  327. new_path = furi_string_alloc();
  328. if(!args_read_probably_quoted_string_and_trim(args, new_path)) {
  329. storage_cli_print_usage();
  330. } else {
  331. FS_Error error = storage_common_copy(
  332. api, furi_string_get_cstr(old_path), furi_string_get_cstr(new_path));
  333. if(error != FSE_OK) {
  334. storage_cli_print_error(error);
  335. }
  336. }
  337. furi_string_free(new_path);
  338. furi_record_close(RECORD_STORAGE);
  339. }
  340. static void storage_cli_remove(Cli* cli, FuriString* path) {
  341. UNUSED(cli);
  342. Storage* api = furi_record_open(RECORD_STORAGE);
  343. FS_Error error = storage_common_remove(api, furi_string_get_cstr(path));
  344. if(error != FSE_OK) {
  345. storage_cli_print_error(error);
  346. }
  347. furi_record_close(RECORD_STORAGE);
  348. }
  349. static void storage_cli_rename(Cli* cli, FuriString* old_path, FuriString* args) {
  350. UNUSED(cli);
  351. Storage* api = furi_record_open(RECORD_STORAGE);
  352. FuriString* new_path;
  353. new_path = furi_string_alloc();
  354. if(!args_read_probably_quoted_string_and_trim(args, new_path)) {
  355. storage_cli_print_usage();
  356. } else {
  357. FS_Error error = storage_common_rename(
  358. api, furi_string_get_cstr(old_path), furi_string_get_cstr(new_path));
  359. if(error != FSE_OK) {
  360. storage_cli_print_error(error);
  361. }
  362. }
  363. furi_string_free(new_path);
  364. furi_record_close(RECORD_STORAGE);
  365. }
  366. static void storage_cli_mkdir(Cli* cli, FuriString* path) {
  367. UNUSED(cli);
  368. Storage* api = furi_record_open(RECORD_STORAGE);
  369. FS_Error error = storage_common_mkdir(api, furi_string_get_cstr(path));
  370. if(error != FSE_OK) {
  371. storage_cli_print_error(error);
  372. }
  373. furi_record_close(RECORD_STORAGE);
  374. }
  375. static void storage_cli_md5(Cli* cli, FuriString* path) {
  376. UNUSED(cli);
  377. Storage* api = furi_record_open(RECORD_STORAGE);
  378. File* file = storage_file_alloc(api);
  379. if(storage_file_open(file, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  380. const uint16_t buffer_size = 512;
  381. const uint8_t hash_size = 16;
  382. uint8_t* data = malloc(buffer_size);
  383. uint8_t* hash = malloc(sizeof(uint8_t) * hash_size);
  384. md5_context* md5_ctx = malloc(sizeof(md5_context));
  385. md5_starts(md5_ctx);
  386. while(true) {
  387. uint16_t read_size = storage_file_read(file, data, buffer_size);
  388. if(read_size == 0) break;
  389. md5_update(md5_ctx, data, read_size);
  390. }
  391. md5_finish(md5_ctx, hash);
  392. free(md5_ctx);
  393. for(uint8_t i = 0; i < hash_size; i++) {
  394. printf("%02x", hash[i]);
  395. }
  396. printf("\r\n");
  397. free(hash);
  398. free(data);
  399. } else {
  400. storage_cli_print_error(storage_file_get_error(file));
  401. }
  402. storage_file_close(file);
  403. storage_file_free(file);
  404. furi_record_close(RECORD_STORAGE);
  405. }
  406. void storage_cli(Cli* cli, FuriString* args, void* context) {
  407. UNUSED(context);
  408. FuriString* cmd;
  409. FuriString* path;
  410. cmd = furi_string_alloc();
  411. path = furi_string_alloc();
  412. do {
  413. if(!args_read_string_and_trim(args, cmd)) {
  414. storage_cli_print_usage();
  415. break;
  416. }
  417. if(!args_read_probably_quoted_string_and_trim(args, path)) {
  418. storage_cli_print_usage();
  419. break;
  420. }
  421. if(furi_string_cmp_str(cmd, "info") == 0) {
  422. storage_cli_info(cli, path);
  423. break;
  424. }
  425. if(furi_string_cmp_str(cmd, "format") == 0) {
  426. storage_cli_format(cli, path);
  427. break;
  428. }
  429. if(furi_string_cmp_str(cmd, "list") == 0) {
  430. storage_cli_list(cli, path);
  431. break;
  432. }
  433. if(furi_string_cmp_str(cmd, "tree") == 0) {
  434. storage_cli_tree(cli, path);
  435. break;
  436. }
  437. if(furi_string_cmp_str(cmd, "read") == 0) {
  438. storage_cli_read(cli, path);
  439. break;
  440. }
  441. if(furi_string_cmp_str(cmd, "read_chunks") == 0) {
  442. storage_cli_read_chunks(cli, path, args);
  443. break;
  444. }
  445. if(furi_string_cmp_str(cmd, "write") == 0) {
  446. storage_cli_write(cli, path);
  447. break;
  448. }
  449. if(furi_string_cmp_str(cmd, "write_chunk") == 0) {
  450. storage_cli_write_chunk(cli, path, args);
  451. break;
  452. }
  453. if(furi_string_cmp_str(cmd, "copy") == 0) {
  454. storage_cli_copy(cli, path, args);
  455. break;
  456. }
  457. if(furi_string_cmp_str(cmd, "remove") == 0) {
  458. storage_cli_remove(cli, path);
  459. break;
  460. }
  461. if(furi_string_cmp_str(cmd, "rename") == 0) {
  462. storage_cli_rename(cli, path, args);
  463. break;
  464. }
  465. if(furi_string_cmp_str(cmd, "mkdir") == 0) {
  466. storage_cli_mkdir(cli, path);
  467. break;
  468. }
  469. if(furi_string_cmp_str(cmd, "md5") == 0) {
  470. storage_cli_md5(cli, path);
  471. break;
  472. }
  473. if(furi_string_cmp_str(cmd, "stat") == 0) {
  474. storage_cli_stat(cli, path);
  475. break;
  476. }
  477. storage_cli_print_usage();
  478. } while(false);
  479. furi_string_free(path);
  480. furi_string_free(cmd);
  481. }
  482. static void storage_cli_factory_reset(Cli* cli, FuriString* args, void* context) {
  483. UNUSED(args);
  484. UNUSED(context);
  485. printf("All data will be lost! Are you sure (y/n)?\r\n");
  486. char c = cli_getc(cli);
  487. if(c == 'y' || c == 'Y') {
  488. printf("Data will be wiped after reboot.\r\n");
  489. furi_hal_rtc_set_flag(FuriHalRtcFlagFactoryReset);
  490. power_reboot(PowerBootModeNormal);
  491. } else {
  492. printf("Safe choice.\r\n");
  493. }
  494. }
  495. void storage_on_system_start() {
  496. #ifdef SRV_CLI
  497. Cli* cli = furi_record_open(RECORD_CLI);
  498. cli_add_command(cli, RECORD_STORAGE, CliCommandFlagParallelSafe, storage_cli, NULL);
  499. cli_add_command(
  500. cli, "factory_reset", CliCommandFlagParallelSafe, storage_cli_factory_reset, NULL);
  501. furi_record_close(RECORD_CLI);
  502. #else
  503. UNUSED(storage_cli_factory_reset);
  504. #endif
  505. }