storage_cli.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. uint8_t* data = malloc(buffer_size);
  233. uint64_t file_size = storage_file_size(file);
  234. printf("Size: %lu\r\n", (uint32_t)file_size);
  235. while(file_size > 0) {
  236. printf("\r\nReady?\r\n");
  237. cli_getc(cli);
  238. uint16_t read_size = storage_file_read(file, data, buffer_size);
  239. for(uint16_t i = 0; i < read_size; i++) {
  240. putchar(data[i]);
  241. }
  242. file_size -= read_size;
  243. }
  244. printf("\r\n");
  245. free(data);
  246. } else {
  247. storage_cli_print_error(storage_file_get_error(file));
  248. }
  249. storage_file_close(file);
  250. storage_file_free(file);
  251. furi_record_close(RECORD_STORAGE);
  252. }
  253. static void storage_cli_write_chunk(Cli* cli, string_t path, string_t args) {
  254. Storage* api = furi_record_open(RECORD_STORAGE);
  255. File* file = storage_file_alloc(api);
  256. uint32_t buffer_size;
  257. int parsed_count = sscanf(string_get_cstr(args), "%lu", &buffer_size);
  258. if(parsed_count == EOF || parsed_count != 1) {
  259. storage_cli_print_usage();
  260. } else {
  261. if(storage_file_open(file, string_get_cstr(path), FSAM_WRITE, FSOM_OPEN_APPEND)) {
  262. printf("Ready\r\n");
  263. uint8_t* buffer = malloc(buffer_size);
  264. for(uint32_t i = 0; i < buffer_size; i++) {
  265. buffer[i] = cli_getc(cli);
  266. }
  267. uint16_t written_size = storage_file_write(file, buffer, buffer_size);
  268. if(written_size != buffer_size) {
  269. storage_cli_print_error(storage_file_get_error(file));
  270. }
  271. free(buffer);
  272. } else {
  273. storage_cli_print_error(storage_file_get_error(file));
  274. }
  275. storage_file_close(file);
  276. }
  277. storage_file_free(file);
  278. furi_record_close(RECORD_STORAGE);
  279. }
  280. static void storage_cli_stat(Cli* cli, string_t path) {
  281. UNUSED(cli);
  282. Storage* api = furi_record_open(RECORD_STORAGE);
  283. if(string_cmp_str(path, "/") == 0) {
  284. printf("Storage\r\n");
  285. } else if(
  286. string_cmp_str(path, STORAGE_EXT_PATH_PREFIX) == 0 ||
  287. string_cmp_str(path, STORAGE_INT_PATH_PREFIX) == 0 ||
  288. string_cmp_str(path, STORAGE_ANY_PATH_PREFIX) == 0) {
  289. uint64_t total_space;
  290. uint64_t free_space;
  291. FS_Error error =
  292. storage_common_fs_info(api, string_get_cstr(path), &total_space, &free_space);
  293. if(error != FSE_OK) {
  294. storage_cli_print_error(error);
  295. } else {
  296. printf(
  297. "Storage, %luKB total, %luKB free\r\n",
  298. (uint32_t)(total_space / 1024),
  299. (uint32_t)(free_space / 1024));
  300. }
  301. } else {
  302. FileInfo fileinfo;
  303. FS_Error error = storage_common_stat(api, string_get_cstr(path), &fileinfo);
  304. if(error == FSE_OK) {
  305. if(fileinfo.flags & FSF_DIRECTORY) {
  306. printf("Directory\r\n");
  307. } else {
  308. printf("File, size: %lub\r\n", (uint32_t)(fileinfo.size));
  309. }
  310. } else {
  311. storage_cli_print_error(error);
  312. }
  313. }
  314. furi_record_close(RECORD_STORAGE);
  315. }
  316. static void storage_cli_copy(Cli* cli, string_t old_path, string_t args) {
  317. UNUSED(cli);
  318. Storage* api = furi_record_open(RECORD_STORAGE);
  319. string_t new_path;
  320. string_init(new_path);
  321. if(!args_read_probably_quoted_string_and_trim(args, new_path)) {
  322. storage_cli_print_usage();
  323. } else {
  324. FS_Error error =
  325. storage_common_copy(api, string_get_cstr(old_path), string_get_cstr(new_path));
  326. if(error != FSE_OK) {
  327. storage_cli_print_error(error);
  328. }
  329. }
  330. string_clear(new_path);
  331. furi_record_close(RECORD_STORAGE);
  332. }
  333. static void storage_cli_remove(Cli* cli, string_t path) {
  334. UNUSED(cli);
  335. Storage* api = furi_record_open(RECORD_STORAGE);
  336. FS_Error error = storage_common_remove(api, string_get_cstr(path));
  337. if(error != FSE_OK) {
  338. storage_cli_print_error(error);
  339. }
  340. furi_record_close(RECORD_STORAGE);
  341. }
  342. static void storage_cli_rename(Cli* cli, string_t old_path, string_t args) {
  343. UNUSED(cli);
  344. Storage* api = furi_record_open(RECORD_STORAGE);
  345. string_t new_path;
  346. string_init(new_path);
  347. if(!args_read_probably_quoted_string_and_trim(args, new_path)) {
  348. storage_cli_print_usage();
  349. } else {
  350. FS_Error error =
  351. storage_common_rename(api, string_get_cstr(old_path), string_get_cstr(new_path));
  352. if(error != FSE_OK) {
  353. storage_cli_print_error(error);
  354. }
  355. }
  356. string_clear(new_path);
  357. furi_record_close(RECORD_STORAGE);
  358. }
  359. static void storage_cli_mkdir(Cli* cli, string_t path) {
  360. UNUSED(cli);
  361. Storage* api = furi_record_open(RECORD_STORAGE);
  362. FS_Error error = storage_common_mkdir(api, string_get_cstr(path));
  363. if(error != FSE_OK) {
  364. storage_cli_print_error(error);
  365. }
  366. furi_record_close(RECORD_STORAGE);
  367. }
  368. static void storage_cli_md5(Cli* cli, string_t path) {
  369. UNUSED(cli);
  370. Storage* api = furi_record_open(RECORD_STORAGE);
  371. File* file = storage_file_alloc(api);
  372. if(storage_file_open(file, string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  373. const uint16_t buffer_size = 512;
  374. const uint8_t hash_size = 16;
  375. uint8_t* data = malloc(buffer_size);
  376. uint8_t* hash = malloc(sizeof(uint8_t) * hash_size);
  377. md5_context* md5_ctx = malloc(sizeof(md5_context));
  378. md5_starts(md5_ctx);
  379. while(true) {
  380. uint16_t read_size = storage_file_read(file, data, buffer_size);
  381. if(read_size == 0) break;
  382. md5_update(md5_ctx, data, read_size);
  383. }
  384. md5_finish(md5_ctx, hash);
  385. free(md5_ctx);
  386. for(uint8_t i = 0; i < hash_size; i++) {
  387. printf("%02x", hash[i]);
  388. }
  389. printf("\r\n");
  390. free(hash);
  391. free(data);
  392. } else {
  393. storage_cli_print_error(storage_file_get_error(file));
  394. }
  395. storage_file_close(file);
  396. storage_file_free(file);
  397. furi_record_close(RECORD_STORAGE);
  398. }
  399. void storage_cli(Cli* cli, string_t args, void* context) {
  400. UNUSED(context);
  401. string_t cmd;
  402. string_t path;
  403. string_init(cmd);
  404. string_init(path);
  405. do {
  406. if(!args_read_string_and_trim(args, cmd)) {
  407. storage_cli_print_usage();
  408. break;
  409. }
  410. if(!args_read_probably_quoted_string_and_trim(args, path)) {
  411. storage_cli_print_usage();
  412. break;
  413. }
  414. if(string_cmp_str(cmd, "info") == 0) {
  415. storage_cli_info(cli, path);
  416. break;
  417. }
  418. if(string_cmp_str(cmd, "format") == 0) {
  419. storage_cli_format(cli, path);
  420. break;
  421. }
  422. if(string_cmp_str(cmd, "list") == 0) {
  423. storage_cli_list(cli, path);
  424. break;
  425. }
  426. if(string_cmp_str(cmd, "tree") == 0) {
  427. storage_cli_tree(cli, path);
  428. break;
  429. }
  430. if(string_cmp_str(cmd, "read") == 0) {
  431. storage_cli_read(cli, path);
  432. break;
  433. }
  434. if(string_cmp_str(cmd, "read_chunks") == 0) {
  435. storage_cli_read_chunks(cli, path, args);
  436. break;
  437. }
  438. if(string_cmp_str(cmd, "write") == 0) {
  439. storage_cli_write(cli, path);
  440. break;
  441. }
  442. if(string_cmp_str(cmd, "write_chunk") == 0) {
  443. storage_cli_write_chunk(cli, path, args);
  444. break;
  445. }
  446. if(string_cmp_str(cmd, "copy") == 0) {
  447. storage_cli_copy(cli, path, args);
  448. break;
  449. }
  450. if(string_cmp_str(cmd, "remove") == 0) {
  451. storage_cli_remove(cli, path);
  452. break;
  453. }
  454. if(string_cmp_str(cmd, "rename") == 0) {
  455. storage_cli_rename(cli, path, args);
  456. break;
  457. }
  458. if(string_cmp_str(cmd, "mkdir") == 0) {
  459. storage_cli_mkdir(cli, path);
  460. break;
  461. }
  462. if(string_cmp_str(cmd, "md5") == 0) {
  463. storage_cli_md5(cli, path);
  464. break;
  465. }
  466. if(string_cmp_str(cmd, "stat") == 0) {
  467. storage_cli_stat(cli, path);
  468. break;
  469. }
  470. storage_cli_print_usage();
  471. } while(false);
  472. string_clear(path);
  473. string_clear(cmd);
  474. }
  475. static void storage_cli_factory_reset(Cli* cli, string_t args, void* context) {
  476. UNUSED(args);
  477. UNUSED(context);
  478. printf("All data will be lost. Are you sure (y/n)?\r\n");
  479. char c = cli_getc(cli);
  480. if(c == 'y' || c == 'Y') {
  481. printf("Data will be wiped after reboot.\r\n");
  482. furi_hal_rtc_set_flag(FuriHalRtcFlagFactoryReset);
  483. power_reboot(PowerBootModeNormal);
  484. } else {
  485. printf("Safe choice.\r\n");
  486. }
  487. }
  488. void storage_on_system_start() {
  489. #ifdef SRV_CLI
  490. Cli* cli = furi_record_open(RECORD_CLI);
  491. cli_add_command(cli, RECORD_STORAGE, CliCommandFlagParallelSafe, storage_cli, NULL);
  492. cli_add_command(
  493. cli, "factory_reset", CliCommandFlagParallelSafe, storage_cli_factory_reset, NULL);
  494. furi_record_close(RECORD_CLI);
  495. #else
  496. UNUSED(storage_cli_factory_reset);
  497. #endif
  498. }