storage_cli.c 18 KB

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