storage_cli.c 16 KB

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