storage_cli.c 20 KB

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