]> git.sven.stormbind.net Git - sven/exfatprogs.git/blob - dump/dump.c
New upstream version 1.2.1
[sven/exfatprogs.git] / dump / dump.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (C) 2021 Namjae Jeon <linkinjeon@kernel.org>
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <getopt.h>
11 #include <errno.h>
12 #include <locale.h>
13 #include <inttypes.h>
14
15 #include "exfat_ondisk.h"
16 #include "libexfat.h"
17
18 #define EXFAT_MIN_SECT_SIZE_BITS                9
19 #define EXFAT_MAX_SECT_SIZE_BITS                12
20 #define BITS_PER_BYTE                           8
21 #define BITS_PER_BYTE_MASK                      0x7
22
23 static const unsigned char used_bit[] = {
24         0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3,/*  0 ~  19*/
25         2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4,/* 20 ~  39*/
26         2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5,/* 40 ~  59*/
27         4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,/* 60 ~  79*/
28         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4,/* 80 ~  99*/
29         3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,/*100 ~ 119*/
30         4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4,/*120 ~ 139*/
31         3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,/*140 ~ 159*/
32         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5,/*160 ~ 179*/
33         4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5,/*180 ~ 199*/
34         3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6,/*200 ~ 219*/
35         5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,/*220 ~ 239*/
36         4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8             /*240 ~ 255*/
37 };
38
39 static void usage(void)
40 {
41         fprintf(stderr, "Usage: dump.exfat\n");
42         fprintf(stderr, "\t-V | --version                        Show version\n");
43         fprintf(stderr, "\t-h | --help                           Show help\n");
44
45         exit(EXIT_FAILURE);
46 }
47
48 static struct option opts[] = {
49         {"version",             no_argument,            NULL,   'V' },
50         {"help",                no_argument,            NULL,   'h' },
51         {"?",                   no_argument,            NULL,   '?' },
52         {NULL,                  0,                      NULL,    0  }
53 };
54
55 static unsigned int exfat_count_used_clusters(unsigned char *bitmap,
56                 unsigned long long bitmap_len)
57 {
58         unsigned int count = 0;
59         unsigned long long i;
60
61         for (i = 0; i < bitmap_len; i++)
62                 count += used_bit[bitmap[i]];
63
64         return count;
65 }
66
67 static int exfat_show_ondisk_all_info(struct exfat_blk_dev *bd)
68 {
69         struct pbr *ppbr;
70         struct bsx64 *pbsx;
71         struct exfat_dentry *ed;
72         unsigned int root_clu_off, bitmap_clu_off, bitmap_clu;
73         unsigned int total_clus, used_clus, clu_offset, root_clu;
74         unsigned long long bitmap_len;
75         int ret;
76         unsigned char *bitmap;
77         char *volume_label;
78
79         ppbr = malloc(EXFAT_MAX_SECTOR_SIZE);
80         if (!ppbr) {
81                 exfat_err("Cannot allocate pbr: out of memory\n");
82                 return -ENOMEM;
83         }
84
85         /* read main boot sector */
86         if (exfat_read(bd->dev_fd, (char *)ppbr, EXFAT_MAX_SECTOR_SIZE,
87                         0) != (ssize_t)EXFAT_MAX_SECTOR_SIZE) {
88                 exfat_err("main boot sector read failed\n");
89                 ret = -EIO;
90                 goto free_ppbr;
91         }
92
93         if (memcmp(ppbr->bpb.oem_name, "EXFAT   ", 8) != 0) {
94                 exfat_err("Bad fs_name in boot sector, which does not describe a valid exfat filesystem\n");
95                 ret = -EINVAL;
96                 goto free_ppbr;
97         }
98
99         pbsx = &ppbr->bsx;
100
101         if (pbsx->sect_size_bits < EXFAT_MIN_SECT_SIZE_BITS ||
102             pbsx->sect_size_bits > EXFAT_MAX_SECT_SIZE_BITS) {
103                 exfat_err("bogus sector size bits : %u\n",
104                                 pbsx->sect_size_bits);
105                 ret = -EINVAL;
106                 goto free_ppbr;
107         }
108
109         if (pbsx->sect_per_clus_bits > 25 - pbsx->sect_size_bits) {
110                 exfat_err("bogus sectors bits per cluster : %u\n",
111                                 pbsx->sect_per_clus_bits);
112                 ret = -EINVAL;
113                 goto free_ppbr;
114         }
115
116         bd->sector_size_bits = pbsx->sect_size_bits;
117         bd->sector_size = 1 << pbsx->sect_size_bits;
118
119         clu_offset = le32_to_cpu(pbsx->clu_offset);
120         total_clus = le32_to_cpu(pbsx->clu_count);
121         root_clu = le32_to_cpu(pbsx->root_cluster);
122
123         exfat_info("-------------- Dump Boot sector region --------------\n");
124         exfat_info("Volume Length(sectors): \t\t%" PRIu64 "\n",
125                         le64_to_cpu(pbsx->vol_length));
126         exfat_info("FAT Offset(sector offset): \t\t%u\n",
127                         le32_to_cpu(pbsx->fat_offset));
128         exfat_info("FAT Length(sectors): \t\t\t%u\n",
129                         le32_to_cpu(pbsx->fat_length));
130         exfat_info("Cluster Heap Offset (sector offset): \t%u\n", clu_offset);
131         exfat_info("Cluster Count: \t\t\t\t%u\n", total_clus);
132         exfat_info("Root Cluster (cluster offset): \t\t%u\n", root_clu);
133         exfat_info("Volume Serial: \t\t\t\t0x%x\n", le32_to_cpu(pbsx->vol_serial));
134         exfat_info("Sector Size Bits: \t\t\t%u\n", pbsx->sect_size_bits);
135         exfat_info("Sector per Cluster bits: \t\t%u\n\n", pbsx->sect_per_clus_bits);
136
137         bd->cluster_size =
138                 1 << (pbsx->sect_per_clus_bits + pbsx->sect_size_bits);
139         root_clu_off = exfat_clus_to_blk_dev_off(bd, clu_offset, root_clu);
140
141         ed = malloc(sizeof(struct exfat_dentry)*3);
142         if (!ed) {
143                 exfat_err("failed to allocate memory\n");
144                 ret = -ENOMEM;
145                 goto free_ppbr;
146         }
147
148         ret = exfat_read(bd->dev_fd, ed, sizeof(struct exfat_dentry)*3,
149                         root_clu_off);
150         if (ret < 0) {
151                 exfat_err("bitmap entry read failed: %d\n", errno);
152                 ret = -EIO;
153                 goto free_entry;
154         }
155
156         volume_label = exfat_conv_volume_label(&ed[0]);
157         if (!volume_label) {
158                 ret = -EINVAL;
159                 goto free_entry;
160         }
161
162         bitmap_clu = le32_to_cpu(ed[1].bitmap_start_clu);
163         bitmap_clu_off = exfat_clus_to_blk_dev_off(bd, clu_offset,
164                         bitmap_clu);
165         bitmap_len = le64_to_cpu(ed[1].bitmap_size);
166
167         exfat_info("----------------- Dump Root entries -----------------\n");
168         exfat_info("Volume entry type: \t\t\t0x%x\n", ed[0].type);
169         exfat_info("Volume label: \t\t\t\t%s\n", volume_label);
170         exfat_info("Volume label character count: \t\t%u\n", ed[0].vol_char_cnt);
171
172         exfat_info("Bitmap entry type: \t\t\t0x%x\n", ed[1].type);
173         exfat_info("Bitmap start cluster: \t\t\t%x\n", bitmap_clu);
174         exfat_info("Bitmap size: \t\t\t\t%llu\n", bitmap_len);
175
176         exfat_info("Upcase table entry type: \t\t0x%x\n", ed[2].type);
177         exfat_info("Upcase table start cluster: \t\t%x\n",
178                         le32_to_cpu(ed[2].upcase_start_clu));
179         exfat_info("Upcase table size: \t\t\t%" PRIu64 "\n\n",
180                         le64_to_cpu(ed[2].upcase_size));
181
182         bitmap = malloc(bitmap_len);
183         if (!bitmap) {
184                 exfat_err("bitmap allocation failed\n");
185                 ret = -ENOMEM;
186                 goto free_volume_label;
187         }
188
189         ret = exfat_read(bd->dev_fd, bitmap, bitmap_len, bitmap_clu_off);
190         if (ret < 0) {
191                 exfat_err("bitmap entry read failed: %d\n", errno);
192                 ret = -EIO;
193                 free(bitmap);
194                 goto free_volume_label;
195         }
196
197         total_clus = le32_to_cpu(pbsx->clu_count);
198         used_clus = exfat_count_used_clusters(bitmap, bitmap_len);
199
200         exfat_info("---------------- Show the statistics ----------------\n");
201         exfat_info("Cluster size:  \t\t\t\t%u\n", bd->cluster_size);
202         exfat_info("Total Clusters: \t\t\t%u\n", total_clus);
203         exfat_info("Free Clusters: \t\t\t\t%u\n", total_clus-used_clus);
204         ret = 0;
205
206         free(bitmap);
207
208 free_volume_label:
209         free(volume_label);
210 free_entry:
211         free(ed);
212 free_ppbr:
213         free(ppbr);
214         return ret;
215 }
216
217 int main(int argc, char *argv[])
218 {
219         int c;
220         int ret = EXIT_FAILURE;
221         struct exfat_blk_dev bd;
222         struct exfat_user_input ui;
223         bool version_only = false;
224
225         init_user_input(&ui);
226
227         if (!setlocale(LC_CTYPE, ""))
228                 exfat_err("failed to init locale/codeset\n");
229
230         opterr = 0;
231         while ((c = getopt_long(argc, argv, "iVh", opts, NULL)) != EOF)
232                 switch (c) {
233                 case 'V':
234                         version_only = true;
235                         break;
236                 case '?':
237                 case 'h':
238                 default:
239                         usage();
240         }
241
242         show_version();
243         if (version_only)
244                 exit(EXIT_FAILURE);
245
246         if (argc < 2)
247                 usage();
248
249         memset(ui.dev_name, 0, sizeof(ui.dev_name));
250         snprintf(ui.dev_name, sizeof(ui.dev_name), "%s", argv[1]);
251
252         ret = exfat_get_blk_dev_info(&ui, &bd);
253         if (ret < 0)
254                 goto out;
255
256         ret = exfat_show_ondisk_all_info(&bd);
257         close(bd.dev_fd);
258
259 out:
260         return ret;
261 }