storage_cli.c 18 KB

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