1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2021 LG Electronics.
5 * Author(s): Hyunchul Lee <hyc.lee@gmail.com>
19 #include "exfat_ondisk.h"
22 #include "exfat_dir.h"
24 #define EXFAT_MAX_UPCASE_CHARS 0x10000
26 struct exfat2img_hdr {
31 __le32 heap_clus_offset;
37 #define EI_MAGIC 0xB67598DB
38 #define EI_CC_PAYLOAD_LEN 4
43 EI_CC_COPY_2, /* followed by cluster count(4-byte) */
45 EI_CC_SKIP_2, /* followed by cluster count(4-byte) */
53 struct exfat_blk_dev bdev;
55 struct buffer_desc *dump_bdesc;
56 struct buffer_desc *scan_bdesc;
57 struct exfat_de_iter de_iter;
64 uint64_t written_bytes;
67 static struct exfat2img_hdr ei_hdr;
68 static struct exfat2img ei;
69 static struct exfat_stat exfat_stat;
70 static struct path_resolve_ctx path_resolve_ctx;
72 static struct option opts[] = {
73 {"output", required_argument, NULL, 'o' },
74 {"version", no_argument, NULL, 'V' },
75 {"help", no_argument, NULL, 'h' },
79 static void usage(const char *name)
81 fprintf(stderr, "Usage: %s <device> [image-file]\n", name);
82 fprintf(stderr, "\t-o | --output <image-file> Specify destination file\n");
83 fprintf(stderr, "\t-V | --version Show version\n");
84 fprintf(stderr, "\t-h | --help Show help\n");
88 #define ei_err(parent, inode, fmt, ...) \
90 exfat_resolve_path_parent(&path_resolve_ctx, \
92 exfat_err("ERROR: %s: " fmt, \
93 path_resolve_ctx.local_path, \
97 static void free_exfat2img(struct exfat2img *ei)
100 exfat_free_exfat(ei->exfat);
102 exfat_free_buffer(ei->dump_bdesc, 2);
104 exfat_free_buffer(ei->scan_bdesc, 2);
108 close(ei->bdev.dev_fd);
111 static int create_exfat2img(struct exfat2img *ei,
113 const char *out_path)
117 ei->exfat = exfat_alloc_exfat(&ei->bdev, bs);
121 ei->dump_bdesc = exfat_alloc_buffer(2,
122 ei->exfat->clus_size,
123 ei->exfat->sect_size);
124 if (!ei->dump_bdesc) {
129 ei->scan_bdesc = exfat_alloc_buffer(2,
130 ei->exfat->clus_size,
131 ei->exfat->sect_size);
132 if (!ei->scan_bdesc) {
137 if (strcmp(out_path, "-")) {
138 ei->out_fd = open(out_path, O_CREAT | O_TRUNC | O_RDWR, 0664);
140 ei->is_stdout = true;
141 ei->out_fd = fileno(stdout);
144 if (ei->out_fd < 0) {
145 exfat_err("failed to open %s: %s\n", out_path,
160 static ssize_t dump_range(struct exfat2img *ei, off_t start, off_t end)
162 struct exfat *exfat = ei->exfat;
163 size_t len, total_len = 0;
167 unsigned int sc, sc_offset;
168 unsigned int ec, ec_offset;
170 if (exfat_o2c(ei->exfat, start, &sc, &sc_offset) < 0)
172 if (exfat_o2c(ei->exfat, end - 1, &ec, &ec_offset) < 0)
174 exfat_bitmap_set_range(ei->exfat, exfat->alloc_bitmap,
179 while (start < end) {
180 len = (size_t)MIN(end - start, exfat->clus_size);
182 ret = exfat_read(exfat->blk_dev->dev_fd,
183 ei->dump_bdesc[0].buffer,
185 if (ret != (ssize_t)len) {
186 exfat_err("failed to read %llu bytes at %llu\n",
187 (unsigned long long)len,
188 (unsigned long long)start);
192 ret = pwrite(ei->out_fd, ei->dump_bdesc[0].buffer,
194 if (ret != (ssize_t)len) {
195 exfat_err("failed to write %llu bytes at %llu\n",
196 (unsigned long long)len,
197 (unsigned long long)start);
203 exfat_stat.written_bytes += len;
208 static int dump_sectors(struct exfat2img *ei,
212 struct exfat *exfat = ei->exfat;
215 s = exfat_s2o(exfat, start_sect);
216 e = exfat_s2o(exfat, end_sect_excl);
217 return dump_range(ei, s, e) <= 0 ? -EIO : 0;
220 static int dump_clusters(struct exfat2img *ei,
222 clus_t end_clus_excl)
224 struct exfat *exfat = ei->exfat;
227 s = exfat_c2o(exfat, start_clus);
228 e = exfat_c2o(exfat, end_clus_excl);
229 return dump_range(ei, s, e) <= 0 ? -EIO : 0;
232 static int dump_directory(struct exfat2img *ei,
233 struct exfat_inode *inode, size_t size,
234 clus_t *out_clus_count)
236 struct exfat *exfat = ei->exfat;
237 clus_t clus, possible_count;
240 off_t start_off, end_off;
245 if (!(inode->attr & ATTR_SUBDIR))
248 clus = inode->first_clus;
250 max_count = DIV_ROUND_UP(inode->size, exfat->clus_size);
252 possible_count = (256 * MB) >> (exfat->bs->bsx.sect_per_clus_bits +
253 exfat->bs->bsx.sect_size_bits);
254 possible_count = MIN(possible_count, exfat->clus_count);
256 while (exfat_heap_clus(exfat, clus) && *out_clus_count < possible_count) {
257 dump_size = MIN(size, exfat->clus_size);
258 start_off = exfat_c2o(exfat, clus);
259 end_off = start_off + DIV_ROUND_UP(dump_size, 512) * 512;
261 if (dump_range(ei, start_off, end_off) < 0)
264 *out_clus_count += 1;
269 if (inode->is_contiguous) {
270 if (*out_clus_count >= max_count)
273 if (exfat_get_inode_next_clus(exfat, inode, clus, &clus))
279 static int dump_root(struct exfat2img *ei)
281 struct exfat *exfat = ei->exfat;
282 struct exfat_inode *root;
283 clus_t clus_count = 0;
285 root = exfat_alloc_inode(ATTR_SUBDIR);
289 root->first_clus = le32_to_cpu(exfat->bs->bsx.root_cluster);
290 dump_directory(ei, root, (size_t)-1, &clus_count);
291 root->size = clus_count * exfat->clus_size;
293 ei->exfat->root = root;
297 static int read_file_dentry_set(struct exfat_de_iter *iter,
298 struct exfat_inode **new_node, int *skip_dentries)
300 struct exfat_dentry *file_de, *stream_de, *dentry;
301 struct exfat_inode *node = NULL;
304 ret = exfat_de_iter_get(iter, 0, &file_de);
305 if (ret || file_de->type != EXFAT_FILE) {
306 exfat_debug("failed to get file dentry\n");
310 ret = exfat_de_iter_get(iter, 1, &stream_de);
311 if (ret || stream_de->type != EXFAT_STREAM) {
312 exfat_debug("failed to get stream dentry\n");
318 node = exfat_alloc_inode(le16_to_cpu(file_de->file_attr));
322 for (i = 2; i <= MIN(file_de->file_num_ext, 1 + MAX_NAME_DENTRIES); i++) {
323 ret = exfat_de_iter_get(iter, i, &dentry);
324 if (ret || dentry->type != EXFAT_NAME)
327 (i - 2) * ENTRY_NAME_MAX, dentry->name_unicode,
328 sizeof(dentry->name_unicode));
331 node->first_clus = le32_to_cpu(stream_de->stream_start_clu);
332 node->is_contiguous =
333 ((stream_de->stream_flags & EXFAT_SF_CONTIGUOUS) != 0);
334 node->size = le64_to_cpu(stream_de->stream_size);
341 exfat_free_inode(node);
345 static int read_file(struct exfat_de_iter *de_iter,
346 struct exfat_inode **new_node, int *dentry_count)
348 struct exfat_inode *node;
353 ret = read_file_dentry_set(de_iter, &node, dentry_count);
357 if (node->attr & ATTR_SUBDIR)
358 exfat_stat.dir_count++;
360 exfat_stat.file_count++;
365 static int read_bitmap(struct exfat2img *ei, struct exfat_de_iter *iter)
367 struct exfat *exfat = ei->exfat;
368 struct exfat_dentry *dentry;
371 ret = exfat_de_iter_get(iter, 0, &dentry);
372 if (ret || dentry->type != EXFAT_BITMAP) {
373 exfat_debug("failed to get bimtap dentry\n");
377 exfat_debug("start cluster %#x, size %#" PRIx64 "\n",
378 le32_to_cpu(dentry->bitmap_start_clu),
379 le64_to_cpu(dentry->bitmap_size));
381 if (!exfat_heap_clus(exfat, le32_to_cpu(dentry->bitmap_start_clu))) {
382 exfat_err("invalid start cluster of allocate bitmap. 0x%x\n",
383 le32_to_cpu(dentry->bitmap_start_clu));
387 exfat->disk_bitmap_clus = le32_to_cpu(dentry->bitmap_start_clu);
388 exfat->disk_bitmap_size = DIV_ROUND_UP(exfat->clus_count, 8);
390 return dump_clusters(ei,
391 exfat->disk_bitmap_clus,
392 exfat->disk_bitmap_clus +
393 DIV_ROUND_UP(exfat->disk_bitmap_size,
397 static int read_upcase_table(struct exfat2img *ei,
398 struct exfat_de_iter *iter)
400 struct exfat *exfat = ei->exfat;
401 struct exfat_dentry *dentry = NULL;
405 retval = exfat_de_iter_get(iter, 0, &dentry);
406 if (retval || dentry->type != EXFAT_UPCASE) {
407 exfat_debug("failed to get upcase dentry\n");
411 if (!exfat_heap_clus(exfat, le32_to_cpu(dentry->upcase_start_clu))) {
412 exfat_err("invalid start cluster of upcase table. 0x%x\n",
413 le32_to_cpu(dentry->upcase_start_clu));
417 size = EXFAT_MAX_UPCASE_CHARS * sizeof(__le16);
418 return dump_clusters(ei, le32_to_cpu(dentry->upcase_start_clu),
419 le32_to_cpu(dentry->upcase_start_clu) +
420 DIV_ROUND_UP(size, exfat->clus_size));
423 static int read_children(struct exfat2img *ei, struct exfat_inode *dir,
424 off_t *end_file_offset)
426 struct exfat *exfat = ei->exfat;
427 struct exfat_inode *node = NULL;
428 struct exfat_dentry *dentry;
429 struct exfat_de_iter *de_iter;
433 *end_file_offset = 0;
434 de_iter = &ei->de_iter;
435 ret = exfat_de_iter_init(de_iter, exfat, dir, ei->scan_bdesc);
442 ret = exfat_de_iter_get(de_iter, 0, &dentry);
446 ei_err(dir->parent, dir,
447 "failed to get a dentry. %d\n", ret);
452 switch (dentry->type) {
454 ret = read_file(de_iter, &node, &dentry_count);
456 exfat_stat.error_count++;
461 if ((node->attr & ATTR_SUBDIR) && node->size) {
463 list_add_tail(&node->sibling,
465 list_add_tail(&node->list,
468 exfat_free_inode(node);
475 if (dir == exfat->root) {
476 ret = read_bitmap(ei, de_iter);
478 exfat_debug("failed to read bitmap\n");
482 if (dir == exfat->root) {
483 ret = read_upcase_table(ei, de_iter);
485 exfat_debug("failed to upcase table\n");
493 ret = exfat_de_iter_advance(de_iter, dentry_count);
496 *end_file_offset = exfat_de_iter_file_offset(de_iter);
497 exfat_de_iter_flush(de_iter);
500 exfat_free_children(dir, false);
501 INIT_LIST_HEAD(&dir->children);
502 exfat_de_iter_flush(de_iter);
506 static int dump_filesystem(struct exfat2img *ei)
508 struct exfat *exfat = ei->exfat;
509 struct exfat_inode *dir;
510 int ret = 0, dir_errors;
512 off_t end_file_offset;
515 exfat_err("root is NULL\n");
519 list_add(&exfat->root->list, &exfat->dir_list);
521 while (!list_empty(&exfat->dir_list)) {
522 dir = list_entry(exfat->dir_list.next,
523 struct exfat_inode, list);
526 if (!(dir->attr & ATTR_SUBDIR)) {
527 ei_err(dir->parent, dir,
528 "failed to travel directories. the node is not directory\n");
533 dir_errors = read_children(ei, dir, &end_file_offset);
535 dump_directory(ei, dir, (size_t)end_file_offset,
537 } else if (dir_errors) {
538 dump_directory(ei, dir, (size_t)-1,
540 exfat_resolve_path(&path_resolve_ctx, dir);
541 exfat_debug("failed to check dentries: %s\n",
542 path_resolve_ctx.local_path);
546 list_del(&dir->list);
547 exfat_free_ancestors(dir);
550 exfat_free_dir_list(exfat);
554 static int dump_bytes_to_stdout(struct exfat2img *ei,
555 off_t start, off_t end_excl, bool fill_zero)
557 struct exfat *exfat = ei->exfat;
561 if (start != ei->stdout_offset) {
562 exfat_err("try to skip for stdout at %llu, expected: %llu\n",
563 (unsigned long long)start,
564 (unsigned long long)ei->stdout_offset);
568 while (start < end_excl) {
569 len = (size_t)MIN(end_excl - start, exfat->clus_size);
571 ret = exfat_read(exfat->blk_dev->dev_fd,
572 ei->dump_bdesc[0].buffer,
574 if (ret != (ssize_t)len) {
575 exfat_err("failed to read %llu bytes at %llu\n",
576 (unsigned long long)len,
577 (unsigned long long)start);
581 ret = write(ei->out_fd, ei->dump_bdesc[0].buffer, len);
582 if (ret != (ssize_t)len) {
583 exfat_err("failed to write %llu bytes at %llu\n",
584 (unsigned long long)len,
585 (unsigned long long)start);
589 ret = write(ei->out_fd, exfat->zero_cluster, len);
590 if (ret != (ssize_t)len) {
591 exfat_err("failed to write %llu bytes at %llu\n",
592 (unsigned long long)len,
593 (unsigned long long)start);
599 ei->stdout_offset += len;
600 exfat_stat.written_bytes += len;
605 static int dump_clusters_to_stdout(struct exfat2img *ei,
606 unsigned int start_clu, unsigned int end_clu,
609 unsigned int clu, clu_count;
611 unsigned int cc_clu_count, cc_len;
612 off_t start_off, end_off_excl;
613 char buf[1 + EI_CC_PAYLOAD_LEN];
616 clu_count = end_clu - start_clu + 1;
619 /* if the count of clusters is less than 5, use SKIP_1 or COPY_2 */
620 cc_clu_count = clu_count < 5 ? 1 : clu_count;
621 cc_len = cc_clu_count == 1 ? 1 : 1 + EI_CC_PAYLOAD_LEN;
623 cc = cc_clu_count == 1 ? EI_CC_SKIP_1 : EI_CC_SKIP_2;
625 cc = cc_clu_count == 1 ? EI_CC_COPY_1 : EI_CC_COPY_2;
628 cc_clu_count = clu_count;
631 while (clu <= end_clu) {
632 if (cc != EI_CC_INVALID) {
634 *((__le32 *)&buf[1]) =
635 cpu_to_le32(cc_clu_count);
636 if (write(ei->out_fd, buf, cc_len) != (ssize_t)cc_len) {
637 exfat_err("failed to write cc %d : %u\n for %u ~ %u clusters\n",
639 start_clu, start_clu + cc_clu_count - 1);
643 if (cc == EI_CC_COPY_1 || cc == EI_CC_COPY_2) {
644 start_off = exfat_c2o(ei->exfat, clu);
645 end_off_excl = exfat_c2o(ei->exfat, clu + cc_clu_count);
647 if (dump_bytes_to_stdout(ei, start_off, end_off_excl,
651 ei->stdout_offset += (off_t)cc_clu_count * ei->exfat->clus_size;
659 static int dump_to_stdout(struct exfat2img *ei)
661 struct exfat *exfat = ei->exfat;
662 off_t start_off, end_off;
663 unsigned int clu, last_clu, next_clu;
664 unsigned int start_clu, end_clu;
667 end_off = exfat_s2o(exfat, le32_to_cpu(exfat->bs->bsx.clu_offset));
668 if (dump_bytes_to_stdout(ei, start_off, end_off, false) < 0) {
669 exfat_err("failed to dump boot sectors and FAT tables\n");
673 clu = EXFAT_FIRST_CLUSTER;
674 last_clu = clu + exfat->clus_count;
675 while (clu < last_clu) {
676 /* read and write clusters for allocated ones */
678 while (clu < last_clu &&
679 exfat_bitmap_get(exfat->alloc_bitmap, clu)) {
687 if (dump_clusters_to_stdout(ei, start_clu, end_clu, false) < 0) {
688 start_off = exfat_c2o(exfat, start_clu);
689 end_off = exfat_c2o(exfat, end_clu);
690 exfat_err("failed to dump range from %llx to %llx\n",
691 (unsigned long long)start_off,
692 (unsigned long long)end_off);
697 /* exit if all of the remaining clusters are free */
700 if (exfat_bitmap_find_one(exfat, exfat->alloc_bitmap,
702 next_clu = EXFAT_FIRST_CLUSTER + exfat->clus_count;
704 /* write zeroes for free clusters */
706 end_clu = next_clu - 1;
707 if (dump_clusters_to_stdout(ei, start_clu, end_clu, true) < 0) {
708 start_off = exfat_c2o(exfat, start_clu);
709 end_off = exfat_c2o(exfat, end_clu);
710 exfat_err("failed to dump zero range from %llx to %llx\n",
711 (unsigned long long)start_off,
712 (unsigned long long)end_off);
722 static int dump_header(struct exfat2img *ei)
724 struct exfat *exfat = ei->exfat;
726 ei_hdr.magic = cpu_to_le32(EI_MAGIC);
727 ei_hdr.major_version = cpu_to_le32(1);
728 ei_hdr.minor_version = cpu_to_le32(0);
729 ei_hdr.data_offset = cpu_to_le32(sizeof(struct exfat2img_hdr));
730 ei_hdr.heap_clus_offset =
731 cpu_to_le32(le32_to_cpu(exfat->bs->bsx.clu_offset) *
733 ei_hdr.cluster_size = cpu_to_le32(exfat->clus_size);
734 ei_hdr.cluster_count = cpu_to_le32(exfat->clus_count);
736 if (write(ei->out_fd, &ei_hdr, sizeof(ei_hdr)) != (ssize_t)sizeof(ei_hdr)) {
737 exfat_err("failed to write exfat2img header\n");
743 static ssize_t read_stream(int fd, void *buf, size_t len)
748 while (read_len < len) {
749 ret = read(fd, buf, len - read_len);
751 if (errno != -EAGAIN && errno != -EINTR)
754 } else if (ret == 0) {
757 buf = (char *)buf + (size_t)ret;
758 read_len += (size_t)ret;
763 static int restore_from_stdin(struct exfat2img *ei)
767 unsigned int clu, end_clu;
768 unsigned int cc_clu_count;
769 unsigned int clus_size;
770 __le32 t_cc_clu_count;
771 off_t out_start_off, out_end_off_excl;
775 in_fd = fileno(stdin);
777 exfat_err("failed to get fd from stdin\n");
781 if (read_stream(in_fd, &ei_hdr, sizeof(ei_hdr)) != (ssize_t)sizeof(ei_hdr)) {
782 exfat_err("failed to read a header\n");
786 if (le32_to_cpu(ei_hdr.magic) != EI_MAGIC) {
787 exfat_err("header has invalid magic %#x, expected %#x\n",
788 le32_to_cpu(ei_hdr.magic), EI_MAGIC);
792 clus_size = le32_to_cpu(ei_hdr.cluster_size);
794 ei->out_fd = ei->bdev.dev_fd;
795 ei->dump_bdesc = exfat_alloc_buffer(2, clus_size, 512);
799 /* restore boot regions, and FAT tables */
800 in_start_off = le32_to_cpu(ei_hdr.data_offset);
802 out_end_off_excl = le32_to_cpu(ei_hdr.heap_clus_offset);
803 while (out_start_off < out_end_off_excl) {
804 len = MIN(out_end_off_excl - out_start_off, clus_size);
805 if (read_stream(in_fd, ei->dump_bdesc[0].buffer, len) != (ssize_t)len) {
806 exfat_err("failed to read first meta region. %llu ~ %llu\n",
807 (unsigned long long)in_start_off,
808 (unsigned long long)in_start_off + len);
813 if (pwrite(ei->out_fd, ei->dump_bdesc[0].buffer, len, out_start_off)
815 exfat_err("failed to write first meta region. %llu ~ %llu\n",
816 (unsigned long long)out_start_off,
817 (unsigned long long)out_start_off + len);
822 out_start_off += len;
826 /* restore heap clusters */
828 while (clu < le32_to_cpu(ei_hdr.cluster_count)) {
829 if (read_stream(in_fd, &cc, sizeof(cc)) != (ssize_t)sizeof(cc)) {
830 exfat_err("failed to read cc at %llu\n",
831 (unsigned long long)in_start_off);
837 if (cc == EI_CC_COPY_2 || cc == EI_CC_SKIP_2) {
838 if (read_stream(in_fd, &t_cc_clu_count, EI_CC_PAYLOAD_LEN) !=
839 (ssize_t)EI_CC_PAYLOAD_LEN) {
840 exfat_err("failed to read cc cluster count at %llu\n",
841 (unsigned long long)in_start_off);
845 cc_clu_count = le32_to_cpu(t_cc_clu_count);
846 in_start_off += EI_CC_PAYLOAD_LEN;
847 } else if (cc == EI_CC_COPY_1 || cc == EI_CC_SKIP_1) {
850 exfat_err("unexpected cc %d at %llu\n",
851 cc, (unsigned long long)in_start_off);
856 if (cc == EI_CC_COPY_1 || cc == EI_CC_COPY_2) {
857 end_clu = clu + cc_clu_count;
858 while (clu < end_clu) {
859 if (read_stream(in_fd, ei->dump_bdesc[0].buffer,
860 clus_size) != (ssize_t)clus_size) {
861 exfat_err("failed to read range %llu ~ %llu\n",
862 (unsigned long long)in_start_off,
863 (unsigned long long)in_start_off + clus_size);
867 if (pwrite(ei->out_fd, ei->dump_bdesc[0].buffer,
868 clus_size, out_start_off) != (ssize_t)clus_size) {
869 exfat_err("failed to write range %llu ~ %llu\n",
870 (unsigned long long)out_start_off,
871 (unsigned long long)out_start_off + clus_size);
876 out_start_off += clus_size;
877 in_start_off += clus_size;
881 out_start_off += (off_t)cc_clu_count * clus_size;
882 in_start_off += (off_t)cc_clu_count * clus_size;
883 if (lseek(ei->out_fd, out_start_off, SEEK_SET) == (off_t)-1) {
884 exfat_err("failed to seek to %llu\n",
885 (unsigned long long)out_start_off);
894 exfat_free_buffer(ei->dump_bdesc, 2);
898 int main(int argc, char * const argv[])
901 const char *in_path, *out_path = NULL, *blkdev_path;
903 struct exfat_user_input ui;
907 print_level = EXFAT_ERROR;
910 while ((c = getopt_long(argc, argv, "o:Vh", opts, NULL)) != EOF) {
927 if (!(optind == argc - 1 && out_path) &&
928 !(optind == argc - 2 && !out_path))
931 in_path = argv[optind++];
933 out_path = argv[optind++];
935 if (!strcmp(in_path, "-")) {
937 blkdev_path = out_path;
940 blkdev_path = in_path;
943 memset(&ui, 0, sizeof(ui));
944 snprintf(ui.dev_name, sizeof(ui.dev_name), "%s", blkdev_path);
948 ui.writeable = false;
950 if (exfat_get_blk_dev_info(&ui, &ei.bdev)) {
951 exfat_err("failed to open %s\n", ui.dev_name);
956 return restore_from_stdin(&ei);
958 err = read_boot_sect(&ei.bdev, &bs);
960 close(ei.bdev.dev_fd);
964 err = create_exfat2img(&ei, bs, out_path);
969 err = dump_sectors(&ei, 0, le32_to_cpu(ei.exfat->bs->bsx.clu_offset));
971 exfat_err("failed to dump boot sectors, fat\n");
975 last_sect = (off_t)le32_to_cpu(ei.exfat->bs->bsx.clu_offset) +
976 (le32_to_cpu(ei.exfat->bs->bsx.clu_count) <<
977 ei.exfat->bs->bsx.sect_per_clus_bits) - 1;
978 err = dump_sectors(&ei, last_sect, last_sect + 1);
980 exfat_err("failed to dump last sector\n");
985 err = dump_root(&ei);
987 exfat_err("failed to dump root\n");
991 dump_filesystem(&ei);
994 err = dump_header(&ei);
997 err = dump_to_stdout(&ei);
1001 err = fsync(ei.out_fd);
1003 exfat_err("failed to fsync %s. %d\n", out_path, errno);
1009 printf("%ld files found, %ld directories dumped, %llu kbytes written\n",
1010 exfat_stat.file_count,
1011 exfat_stat.dir_count,
1012 (unsigned long long)DIV_ROUND_UP(exfat_stat.written_bytes, 1024));
1015 free_exfat2img(&ei);
1016 return err == 0 ? EXIT_SUCCESS : EXIT_FAILURE;