1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2021 LG Electronics.
4 *
5 * Author(s): Hyunchul Lee <[email protected]>
6 */
7
8 #include <unistd.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <getopt.h>
15 #include <inttypes.h>
16 #include <string.h>
17 #include <errno.h>
18
19 #include "exfat_ondisk.h"
20 #include "libexfat.h"
21 #include "exfat_fs.h"
22 #include "exfat_dir.h"
23
24 #define EXFAT_MAX_UPCASE_CHARS 0x10000
25
26 struct exfat2img_hdr {
27 __le32 magic;
28 __le32 major_version;
29 __le32 minor_version;
30 __le32 data_offset;
31 __le32 heap_clus_offset;
32 __le32 cluster_size;
33 __le32 cluster_count;
34 __le32 reserved[20];
35 } __packed;
36
37 #define EI_MAGIC 0xB67598DB
38 #define EI_CC_PAYLOAD_LEN 4
39
40 enum {
41 EI_CC_INVALID,
42 EI_CC_COPY_1,
43 EI_CC_COPY_2, /* followed by cluster count(4-byte) */
44 EI_CC_SKIP_1,
45 EI_CC_SKIP_2, /* followed by cluster count(4-byte) */
46 };
47
48 struct exfat2img {
49 int out_fd;
50 bool is_stdout;
51 off_t stdout_offset;
52 bool save_cc;
53 struct exfat_blk_dev bdev;
54 struct exfat *exfat;
55 struct buffer_desc *dump_bdesc;
56 struct buffer_desc *scan_bdesc;
57 struct exfat_de_iter de_iter;
58 };
59
60 struct exfat_stat {
61 long dir_count;
62 long file_count;
63 long error_count;
64 uint64_t written_bytes;
65 };
66
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;
71
72 static struct option opts[] = {
73 {"output", required_argument, NULL, 'o' },
74 {"version", no_argument, NULL, 'V' },
75 {"help", no_argument, NULL, 'h' },
76 {NULL, 0, NULL, 0 }
77 };
78
usage(const char * name)79 static void usage(const char *name)
80 {
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");
85 exit(EXIT_FAILURE);
86 }
87
88 #define ei_err(parent, inode, fmt, ...) \
89 ({ \
90 exfat_resolve_path_parent(&path_resolve_ctx, \
91 parent, inode); \
92 exfat_err("ERROR: %s: " fmt, \
93 path_resolve_ctx.local_path, \
94 ##__VA_ARGS__); \
95 })
96
free_exfat2img(struct exfat2img * ei)97 static void free_exfat2img(struct exfat2img *ei)
98 {
99 if (ei->exfat)
100 exfat_free_exfat(ei->exfat);
101 if (ei->dump_bdesc)
102 exfat_free_buffer(ei->dump_bdesc, 2);
103 if (ei->scan_bdesc)
104 exfat_free_buffer(ei->scan_bdesc, 2);
105 if (ei->out_fd)
106 close(ei->out_fd);
107 if (ei->bdev.dev_fd)
108 close(ei->bdev.dev_fd);
109 }
110
create_exfat2img(struct exfat2img * ei,struct pbr * bs,const char * out_path)111 static int create_exfat2img(struct exfat2img *ei,
112 struct pbr *bs,
113 const char *out_path)
114 {
115 int err;
116
117 ei->exfat = exfat_alloc_exfat(&ei->bdev, bs);
118 if (!ei->exfat)
119 return -ENOMEM;
120
121 ei->dump_bdesc = exfat_alloc_buffer(2,
122 ei->exfat->clus_size,
123 ei->exfat->sect_size);
124 if (!ei->dump_bdesc) {
125 err = -ENOMEM;
126 goto err;
127 }
128
129 ei->scan_bdesc = exfat_alloc_buffer(2,
130 ei->exfat->clus_size,
131 ei->exfat->sect_size);
132 if (!ei->scan_bdesc) {
133 err = -ENOMEM;
134 goto err;
135 }
136
137 if (strcmp(out_path, "-")) {
138 ei->out_fd = open(out_path, O_CREAT | O_TRUNC | O_RDWR, 0664);
139 } else {
140 ei->is_stdout = true;
141 ei->out_fd = fileno(stdout);
142 ei->save_cc = true;
143 }
144 if (ei->out_fd < 0) {
145 exfat_err("failed to open %s: %s\n", out_path,
146 strerror(errno));
147 err = -errno;
148 goto err;
149 }
150
151 return 0;
152 err:
153 free_exfat2img(ei);
154 return err;
155 }
156
157 /**
158 * @end: excluded.
159 */
dump_range(struct exfat2img * ei,off_t start,off_t end)160 static ssize_t dump_range(struct exfat2img *ei, off_t start, off_t end)
161 {
162 struct exfat *exfat = ei->exfat;
163 size_t len, total_len = 0;
164 ssize_t ret;
165
166 if (ei->is_stdout) {
167 unsigned int sc, sc_offset;
168 unsigned int ec, ec_offset;
169
170 if (exfat_o2c(ei->exfat, start, &sc, &sc_offset) < 0)
171 return -ERANGE;
172 if (exfat_o2c(ei->exfat, end - 1, &ec, &ec_offset) < 0)
173 return -ERANGE;
174 exfat_bitmap_set_range(ei->exfat, exfat->alloc_bitmap,
175 sc, ec - sc + 1);
176 return end - start;
177 }
178
179 while (start < end) {
180 len = (size_t)MIN(end - start, exfat->clus_size);
181
182 ret = exfat_read(exfat->blk_dev->dev_fd,
183 ei->dump_bdesc[0].buffer,
184 len, start);
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);
189 return -EIO;
190 }
191
192 ret = pwrite(ei->out_fd, ei->dump_bdesc[0].buffer,
193 len, start);
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);
198 return -EIO;
199 }
200
201 start += len;
202 total_len += len;
203 exfat_stat.written_bytes += len;
204 }
205 return total_len;
206 }
207
dump_sectors(struct exfat2img * ei,off_t start_sect,off_t end_sect_excl)208 static int dump_sectors(struct exfat2img *ei,
209 off_t start_sect,
210 off_t end_sect_excl)
211 {
212 struct exfat *exfat = ei->exfat;
213 off_t s, e;
214
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;
218 }
219
dump_clusters(struct exfat2img * ei,clus_t start_clus,clus_t end_clus_excl)220 static int dump_clusters(struct exfat2img *ei,
221 clus_t start_clus,
222 clus_t end_clus_excl)
223 {
224 struct exfat *exfat = ei->exfat;
225 off_t s, e;
226
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;
230 }
231
dump_directory(struct exfat2img * ei,struct exfat_inode * inode,size_t size,clus_t * out_clus_count)232 static int dump_directory(struct exfat2img *ei,
233 struct exfat_inode *inode, size_t size,
234 clus_t *out_clus_count)
235 {
236 struct exfat *exfat = ei->exfat;
237 clus_t clus, possible_count;
238 uint64_t max_count;
239 size_t dump_size;
240 off_t start_off, end_off;
241
242 if (size == 0)
243 return -EINVAL;
244
245 if (!(inode->attr & ATTR_SUBDIR))
246 return -EINVAL;
247
248 clus = inode->first_clus;
249 *out_clus_count = 0;
250 max_count = DIV_ROUND_UP(inode->size, exfat->clus_size);
251
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);
255
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;
260
261 if (dump_range(ei, start_off, end_off) < 0)
262 return -EIO;
263
264 *out_clus_count += 1;
265 size -= dump_size;
266 if (size == 0)
267 break;
268
269 if (inode->is_contiguous) {
270 if (*out_clus_count >= max_count)
271 break;
272 }
273 if (exfat_get_inode_next_clus(exfat, inode, clus, &clus))
274 return -EINVAL;
275 }
276 return 0;
277 }
278
dump_root(struct exfat2img * ei)279 static int dump_root(struct exfat2img *ei)
280 {
281 struct exfat *exfat = ei->exfat;
282 struct exfat_inode *root;
283 clus_t clus_count = 0;
284
285 root = exfat_alloc_inode(ATTR_SUBDIR);
286 if (!root)
287 return -ENOMEM;
288
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;
292
293 ei->exfat->root = root;
294 return 0;
295 }
296
read_file_dentry_set(struct exfat_de_iter * iter,struct exfat_inode ** new_node,int * skip_dentries)297 static int read_file_dentry_set(struct exfat_de_iter *iter,
298 struct exfat_inode **new_node, int *skip_dentries)
299 {
300 struct exfat_dentry *file_de, *stream_de, *dentry;
301 struct exfat_inode *node = NULL;
302 int i, ret;
303
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");
307 return -EINVAL;
308 }
309
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");
313 *skip_dentries = 2;
314 goto skip_dset;
315 }
316
317 *new_node = NULL;
318 node = exfat_alloc_inode(le16_to_cpu(file_de->file_attr));
319 if (!node)
320 return -ENOMEM;
321
322 for (i = 2; i <= file_de->file_num_ext; i++) {
323 ret = exfat_de_iter_get(iter, i, &dentry);
324 if (ret || dentry->type != EXFAT_NAME)
325 break;
326 memcpy(node->name +
327 (i - 2) * ENTRY_NAME_MAX, dentry->name_unicode,
328 sizeof(dentry->name_unicode));
329 }
330
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);
335
336 *skip_dentries = i;
337 *new_node = node;
338 return 0;
339 skip_dset:
340 *new_node = NULL;
341 exfat_free_inode(node);
342 return -EINVAL;
343 }
344
read_file(struct exfat_de_iter * de_iter,struct exfat_inode ** new_node,int * dentry_count)345 static int read_file(struct exfat_de_iter *de_iter,
346 struct exfat_inode **new_node, int *dentry_count)
347 {
348 struct exfat_inode *node;
349 int ret;
350
351 *new_node = NULL;
352
353 ret = read_file_dentry_set(de_iter, &node, dentry_count);
354 if (ret)
355 return ret;
356
357 if (node->attr & ATTR_SUBDIR)
358 exfat_stat.dir_count++;
359 else
360 exfat_stat.file_count++;
361 *new_node = node;
362 return ret;
363 }
364
read_bitmap(struct exfat2img * ei,struct exfat_de_iter * iter)365 static int read_bitmap(struct exfat2img *ei, struct exfat_de_iter *iter)
366 {
367 struct exfat *exfat = ei->exfat;
368 struct exfat_dentry *dentry;
369 int ret;
370
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");
374 return -EINVAL;
375 }
376
377 exfat_debug("start cluster %#x, size %#" PRIx64 "\n",
378 le32_to_cpu(dentry->bitmap_start_clu),
379 le64_to_cpu(dentry->bitmap_size));
380
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));
384 return -EINVAL;
385 }
386
387 exfat->disk_bitmap_clus = le32_to_cpu(dentry->bitmap_start_clu);
388 exfat->disk_bitmap_size = DIV_ROUND_UP(exfat->clus_count, 8);
389
390 return dump_clusters(ei,
391 exfat->disk_bitmap_clus,
392 exfat->disk_bitmap_clus +
393 DIV_ROUND_UP(exfat->disk_bitmap_size,
394 exfat->clus_size));
395 }
396
read_upcase_table(struct exfat2img * ei,struct exfat_de_iter * iter)397 static int read_upcase_table(struct exfat2img *ei,
398 struct exfat_de_iter *iter)
399 {
400 struct exfat *exfat = ei->exfat;
401 struct exfat_dentry *dentry = NULL;
402 int retval;
403 ssize_t size;
404
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");
408 return -EINVAL;
409 }
410
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));
414 return -EINVAL;
415 }
416
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));
421 }
422
read_children(struct exfat2img * ei,struct exfat_inode * dir,off_t * end_file_offset)423 static int read_children(struct exfat2img *ei, struct exfat_inode *dir,
424 off_t *end_file_offset)
425 {
426 struct exfat *exfat = ei->exfat;
427 struct exfat_inode *node = NULL;
428 struct exfat_dentry *dentry;
429 struct exfat_de_iter *de_iter;
430 int dentry_count;
431 int ret;
432
433 *end_file_offset = 0;
434 de_iter = &ei->de_iter;
435 ret = exfat_de_iter_init(de_iter, exfat, dir, ei->scan_bdesc);
436 if (ret == EOF)
437 return 0;
438 else if (ret)
439 return ret;
440
441 while (1) {
442 ret = exfat_de_iter_get(de_iter, 0, &dentry);
443 if (ret == EOF) {
444 break;
445 } else if (ret) {
446 ei_err(dir->parent, dir,
447 "failed to get a dentry. %d\n", ret);
448 goto err;
449 }
450 dentry_count = 1;
451
452 switch (dentry->type) {
453 case EXFAT_FILE:
454 ret = read_file(de_iter, &node, &dentry_count);
455 if (ret < 0) {
456 exfat_stat.error_count++;
457 break;
458 }
459
460 if (node) {
461 if ((node->attr & ATTR_SUBDIR) && node->size) {
462 node->parent = dir;
463 list_add_tail(&node->sibling,
464 &dir->children);
465 list_add_tail(&node->list,
466 &exfat->dir_list);
467 } else {
468 exfat_free_inode(node);
469 }
470 }
471 break;
472 case EXFAT_LAST:
473 goto out;
474 case EXFAT_BITMAP:
475 if (dir == exfat->root) {
476 ret = read_bitmap(ei, de_iter);
477 if (ret)
478 exfat_debug("failed to read bitmap\n");
479 }
480 break;
481 case EXFAT_UPCASE:
482 if (dir == exfat->root) {
483 ret = read_upcase_table(ei, de_iter);
484 if (ret)
485 exfat_debug("failed to upcase table\n");
486 }
487 break;
488 case EXFAT_VOLUME:
489 default:
490 break;
491 }
492
493 ret = exfat_de_iter_advance(de_iter, dentry_count);
494 }
495 out:
496 *end_file_offset = exfat_de_iter_file_offset(de_iter);
497 exfat_de_iter_flush(de_iter);
498 return 0;
499 err:
500 exfat_free_children(dir, false);
501 INIT_LIST_HEAD(&dir->children);
502 exfat_de_iter_flush(de_iter);
503 return ret;
504 }
505
dump_filesystem(struct exfat2img * ei)506 static int dump_filesystem(struct exfat2img *ei)
507 {
508 struct exfat *exfat = ei->exfat;
509 struct exfat_inode *dir;
510 int ret = 0, dir_errors;
511 clus_t clus_count;
512 off_t end_file_offset;
513
514 if (!exfat->root) {
515 exfat_err("root is NULL\n");
516 return -ENOENT;
517 }
518
519 list_add(&exfat->root->list, &exfat->dir_list);
520
521 while (!list_empty(&exfat->dir_list)) {
522 dir = list_entry(exfat->dir_list.next,
523 struct exfat_inode, list);
524 clus_count = 0;
525
526 if (!(dir->attr & ATTR_SUBDIR)) {
527 ei_err(dir->parent, dir,
528 "failed to travel directories. the node is not directory\n");
529 ret = -EINVAL;
530 goto out;
531 }
532
533 dir_errors = read_children(ei, dir, &end_file_offset);
534 if (!dir_errors) {
535 dump_directory(ei, dir, (size_t)end_file_offset,
536 &clus_count);
537 } else if (dir_errors) {
538 dump_directory(ei, dir, (size_t)-1,
539 &clus_count);
540 exfat_resolve_path(&path_resolve_ctx, dir);
541 exfat_debug("failed to check dentries: %s\n",
542 path_resolve_ctx.local_path);
543 ret = dir_errors;
544 }
545
546 list_del(&dir->list);
547 exfat_free_ancestors(dir);
548 }
549 out:
550 exfat_free_dir_list(exfat);
551 return ret;
552 }
553
dump_bytes_to_stdout(struct exfat2img * ei,off_t start,off_t end_excl,bool fill_zero)554 static int dump_bytes_to_stdout(struct exfat2img *ei,
555 off_t start, off_t end_excl, bool fill_zero)
556 {
557 struct exfat *exfat = ei->exfat;
558 size_t len;
559 ssize_t ret;
560
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);
565 return -EINVAL;
566 }
567
568 while (start < end_excl) {
569 len = (size_t)MIN(end_excl - start, exfat->clus_size);
570 if (!fill_zero) {
571 ret = exfat_read(exfat->blk_dev->dev_fd,
572 ei->dump_bdesc[0].buffer,
573 len, start);
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);
578 return -EIO;
579 }
580
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);
586 return -EIO;
587 }
588 } else {
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);
594 return -EIO;
595 }
596 }
597
598 start += len;
599 ei->stdout_offset += len;
600 exfat_stat.written_bytes += len;
601 }
602 return 0;
603 }
604
dump_clusters_to_stdout(struct exfat2img * ei,unsigned int start_clu,unsigned int end_clu,bool fill_zero)605 static int dump_clusters_to_stdout(struct exfat2img *ei,
606 unsigned int start_clu, unsigned int end_clu,
607 bool fill_zero)
608 {
609 unsigned int clu, clu_count;
610 unsigned char cc;
611 unsigned int cc_clu_count, cc_len;
612 off_t start_off, end_off_excl;
613 char buf[1 + EI_CC_PAYLOAD_LEN];
614
615 clu = start_clu;
616 clu_count = end_clu - start_clu + 1;
617
618 if (ei->save_cc) {
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;
622 if (fill_zero)
623 cc = cc_clu_count == 1 ? EI_CC_SKIP_1 : EI_CC_SKIP_2;
624 else
625 cc = cc_clu_count == 1 ? EI_CC_COPY_1 : EI_CC_COPY_2;
626 } else {
627 cc = EI_CC_INVALID;
628 cc_clu_count = clu_count;
629 }
630
631 while (clu <= end_clu) {
632 if (cc != EI_CC_INVALID) {
633 buf[0] = cc;
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",
638 cc, cc_clu_count,
639 start_clu, start_clu + cc_clu_count - 1);
640 }
641 }
642
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);
646
647 if (dump_bytes_to_stdout(ei, start_off, end_off_excl,
648 false) < 0)
649 return -EIO;
650 } else {
651 ei->stdout_offset += (off_t)cc_clu_count * ei->exfat->clus_size;
652 }
653 clu += cc_clu_count;
654 }
655
656 return 0;
657 }
658
dump_to_stdout(struct exfat2img * ei)659 static int dump_to_stdout(struct exfat2img *ei)
660 {
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;
665
666 start_off = 0;
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");
670 return -EIO;
671 }
672
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 */
677 start_clu = 0;
678 while (clu < last_clu &&
679 exfat_bitmap_get(exfat->alloc_bitmap, clu)) {
680 if (!start_clu)
681 start_clu = clu;
682 end_clu = clu;
683 clu++;
684 }
685
686 if (start_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);
693 return -EIO;
694 }
695 }
696
697 /* exit if all of the remaining clusters are free */
698 if (clu >= last_clu)
699 break;
700 if (exfat_bitmap_find_one(exfat, exfat->alloc_bitmap,
701 clu, &next_clu))
702 next_clu = EXFAT_FIRST_CLUSTER + exfat->clus_count;
703
704 /* write zeroes for free clusters */
705 start_clu = clu;
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);
713 return -EIO;
714 }
715
716 clu = next_clu;
717 }
718
719 return 0;
720 }
721
dump_header(struct exfat2img * ei)722 static int dump_header(struct exfat2img *ei)
723 {
724 struct exfat *exfat = ei->exfat;
725
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) *
732 exfat->sect_size);
733 ei_hdr.cluster_size = cpu_to_le32(exfat->clus_size);
734 ei_hdr.cluster_count = cpu_to_le32(exfat->clus_count);
735
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");
738 return -EIO;
739 }
740 return 0;
741 }
742
read_stream(int fd,void * buf,size_t len)743 static ssize_t read_stream(int fd, void *buf, size_t len)
744 {
745 size_t read_len = 0;
746 ssize_t ret;
747
748 while (read_len < len) {
749 ret = read(fd, buf, len - read_len);
750 if (ret < 0) {
751 if (errno != -EAGAIN && errno != -EINTR)
752 return -1;
753 ret = 0;
754 } else if (ret == 0) {
755 return 0;
756 }
757 buf = (char *)buf + (size_t)ret;
758 read_len += (size_t)ret;
759 }
760 return read_len;
761 }
762
restore_from_stdin(struct exfat2img * ei)763 static int restore_from_stdin(struct exfat2img *ei)
764 {
765 int in_fd, ret = 0;
766 unsigned char cc;
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;
772 off_t in_start_off;
773 size_t len;
774
775 in_fd = fileno(stdin);
776 if (in_fd < 0) {
777 exfat_err("failed to get fd from stdin\n");
778 return in_fd;
779 }
780
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");
783 return -EIO;
784 }
785
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);
789 return -EINVAL;
790 }
791
792 clus_size = le32_to_cpu(ei_hdr.cluster_size);
793
794 ei->out_fd = ei->bdev.dev_fd;
795 ei->dump_bdesc = exfat_alloc_buffer(2, clus_size, 512);
796 if (!ei->dump_bdesc)
797 return -ENOMEM;
798
799 /* restore boot regions, and FAT tables */
800 in_start_off = le32_to_cpu(ei_hdr.data_offset);
801 out_start_off = 0;
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);
809 ret = -EIO;
810 goto out;
811 }
812
813 if (pwrite(ei->out_fd, ei->dump_bdesc[0].buffer, len, out_start_off)
814 != (ssize_t)len) {
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);
818 ret = -EIO;
819 goto out;
820 }
821
822 out_start_off += len;
823 in_start_off += len;
824 }
825
826 /* restore heap clusters */
827 clu = 0;
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);
832 ret = -EIO;
833 goto out;
834 }
835 in_start_off += 1;
836
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);
842 ret = -EIO;
843 goto out;
844 }
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) {
848 cc_clu_count = 1;
849 } else {
850 exfat_err("unexpected cc %d at %llu\n",
851 cc, (unsigned long long)in_start_off);
852 ret = -EINVAL;
853 goto out;
854 }
855
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);
864 ret = -EIO;
865 goto out;
866 }
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);
872 ret = -EIO;
873 goto out;
874 }
875
876 out_start_off += clus_size;
877 in_start_off += clus_size;
878 clu++;
879 }
880 } else {
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);
886 ret = -EIO;
887 goto out;
888 }
889 clu += cc_clu_count;
890 }
891 }
892 out:
893 fsync(ei->out_fd);
894 exfat_free_buffer(ei->dump_bdesc, 2);
895 return ret;
896 }
897
main(int argc,char * const argv[])898 int main(int argc, char * const argv[])
899 {
900 int err = 0, c;
901 const char *in_path, *out_path = NULL, *blkdev_path;
902 struct pbr *bs;
903 struct exfat_user_input ui;
904 off_t last_sect;
905 bool restore;
906
907 print_level = EXFAT_ERROR;
908
909 opterr = 0;
910 while ((c = getopt_long(argc, argv, "o:Vh", opts, NULL)) != EOF) {
911 switch (c) {
912 case 'o':
913 out_path = optarg;
914 break;
915 case 'V':
916 show_version();
917 return 0;
918 case 'h':
919 /* Fall through */
920 default:
921 usage(argv[0]);
922 break;
923 }
924 }
925
926 show_version();
927 if (!(optind == argc - 1 && out_path) &&
928 !(optind == argc - 2 && !out_path))
929 usage(argv[0]);
930
931 in_path = argv[optind++];
932 if (!out_path)
933 out_path = argv[optind++];
934
935 if (!strcmp(in_path, "-")) {
936 restore = true;
937 blkdev_path = out_path;
938 } else {
939 restore = false;
940 blkdev_path = in_path;
941 }
942
943 memset(&ui, 0, sizeof(ui));
944 snprintf(ui.dev_name, sizeof(ui.dev_name), "%s", blkdev_path);
945 if (restore)
946 ui.writeable = true;
947 else
948 ui.writeable = false;
949
950 if (exfat_get_blk_dev_info(&ui, &ei.bdev)) {
951 exfat_err("failed to open %s\n", ui.dev_name);
952 return EXIT_FAILURE;
953 }
954
955 if (restore)
956 return restore_from_stdin(&ei);
957
958 err = read_boot_sect(&ei.bdev, &bs);
959 if (err) {
960 close(ei.bdev.dev_fd);
961 return EXIT_FAILURE;
962 }
963
964 err = create_exfat2img(&ei, bs, out_path);
965 if (err)
966 return EXIT_FAILURE;
967
968 if (!ei.is_stdout) {
969 err = dump_sectors(&ei, 0, le32_to_cpu(ei.exfat->bs->bsx.clu_offset));
970 if (err) {
971 exfat_err("failed to dump boot sectors, fat\n");
972 goto out;
973 }
974
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);
979 if (err) {
980 exfat_err("failed to dump last sector\n");
981 goto out;
982 }
983 }
984
985 err = dump_root(&ei);
986 if (err) {
987 exfat_err("failed to dump root\n");
988 goto out;
989 }
990
991 dump_filesystem(&ei);
992
993 if (ei.is_stdout) {
994 err = dump_header(&ei);
995 if (err)
996 goto out;
997 err = dump_to_stdout(&ei);
998 if (err)
999 goto out;
1000 } else {
1001 err = fsync(ei.out_fd);
1002 if (err) {
1003 exfat_err("failed to fsync %s. %d\n", out_path, errno);
1004 goto out;
1005 }
1006 close(ei.out_fd);
1007 }
1008
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));
1013
1014 out:
1015 free_exfat2img(&ei);
1016 return err == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
1017 }
1018