storage_cli.c 16 KB

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