storage-cli.c 16 KB

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