1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2021 Namjae Jeon <linkinjeon@kernel.org>
15 #include "exfat_ondisk.h"
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
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*/
39 static void usage(void)
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");
48 static struct option opts[] = {
49 {"version", no_argument, NULL, 'V' },
50 {"help", no_argument, NULL, 'h' },
51 {"?", no_argument, NULL, '?' },
55 static unsigned int exfat_count_used_clusters(unsigned char *bitmap,
56 unsigned long long bitmap_len)
58 unsigned int count = 0;
61 for (i = 0; i < bitmap_len; i++)
62 count += used_bit[bitmap[i]];
67 static int exfat_show_ondisk_all_info(struct exfat_blk_dev *bd)
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;
76 unsigned char *bitmap;
79 ppbr = malloc(EXFAT_MAX_SECTOR_SIZE);
81 exfat_err("Cannot allocate pbr: out of memory\n");
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");
95 if (pbsx->sect_size_bits < EXFAT_MIN_SECT_SIZE_BITS ||
96 pbsx->sect_size_bits > EXFAT_MAX_SECT_SIZE_BITS) {
97 exfat_err("bogus sector size bits : %u\n",
98 pbsx->sect_size_bits);
103 if (pbsx->sect_per_clus_bits > 25 - pbsx->sect_size_bits) {
104 exfat_err("bogus sectors bits per cluster : %u\n",
105 pbsx->sect_per_clus_bits);
110 bd->sector_size_bits = pbsx->sect_size_bits;
111 bd->sector_size = 1 << pbsx->sect_size_bits;
113 clu_offset = le32_to_cpu(pbsx->clu_offset);
114 total_clus = le32_to_cpu(pbsx->clu_count);
115 root_clu = le32_to_cpu(pbsx->root_cluster);
117 exfat_info("-------------- Dump Boot sector region --------------\n");
118 exfat_info("Volume Length(sectors): \t\t%" PRIu64 "\n",
119 le64_to_cpu(pbsx->vol_length));
120 exfat_info("FAT Offset(sector offset): \t\t%u\n",
121 le32_to_cpu(pbsx->fat_offset));
122 exfat_info("FAT Length(sectors): \t\t\t%u\n",
123 le32_to_cpu(pbsx->fat_length));
124 exfat_info("Cluster Heap Offset (sector offset): \t%u\n", clu_offset);
125 exfat_info("Cluster Count: \t\t\t\t%u\n", total_clus);
126 exfat_info("Root Cluster (cluster offset): \t\t%u\n", root_clu);
127 exfat_info("Volume Serial: \t\t\t\t0x%x\n", le32_to_cpu(pbsx->vol_serial));
128 exfat_info("Sector Size Bits: \t\t\t%u\n", pbsx->sect_size_bits);
129 exfat_info("Sector per Cluster bits: \t\t%u\n\n", pbsx->sect_per_clus_bits);
132 1 << (pbsx->sect_per_clus_bits + pbsx->sect_size_bits);
133 root_clu_off = exfat_clus_to_blk_dev_off(bd, clu_offset, root_clu);
135 ed = malloc(sizeof(struct exfat_dentry)*3);
137 exfat_err("failed to allocate memory\n");
142 ret = exfat_read(bd->dev_fd, ed, sizeof(struct exfat_dentry)*3,
145 exfat_err("bitmap entry read failed: %d\n", errno);
150 volume_label = exfat_conv_volume_label(&ed[0]);
156 bitmap_clu = le32_to_cpu(ed[1].bitmap_start_clu);
157 bitmap_clu_off = exfat_clus_to_blk_dev_off(bd, clu_offset,
159 bitmap_len = le64_to_cpu(ed[1].bitmap_size);
161 exfat_info("----------------- Dump Root entries -----------------\n");
162 exfat_info("Volume entry type: \t\t\t0x%x\n", ed[0].type);
163 exfat_info("Volume label: \t\t\t\t%s\n", volume_label);
164 exfat_info("Volume label character count: \t\t%u\n", ed[0].vol_char_cnt);
166 exfat_info("Bitmap entry type: \t\t\t0x%x\n", ed[1].type);
167 exfat_info("Bitmap start cluster: \t\t\t%x\n", bitmap_clu);
168 exfat_info("Bitmap size: \t\t\t\t%llu\n", bitmap_len);
170 exfat_info("Upcase table entry type: \t\t0x%x\n", ed[2].type);
171 exfat_info("Upcase table start cluster: \t\t%x\n",
172 le32_to_cpu(ed[2].upcase_start_clu));
173 exfat_info("Upcase table size: \t\t\t%" PRIu64 "\n\n",
174 le64_to_cpu(ed[2].upcase_size));
176 bitmap = malloc(bitmap_len);
178 exfat_err("bitmap allocation failed\n");
180 goto free_volume_label;
183 ret = exfat_read(bd->dev_fd, bitmap, bitmap_len, bitmap_clu_off);
185 exfat_err("bitmap entry read failed: %d\n", errno);
188 goto free_volume_label;
191 total_clus = le32_to_cpu(pbsx->clu_count);
192 used_clus = exfat_count_used_clusters(bitmap, bitmap_len);
194 exfat_info("---------------- Show the statistics ----------------\n");
195 exfat_info("Cluster size: \t\t\t\t%u\n", bd->cluster_size);
196 exfat_info("Total Clusters: \t\t\t%u\n", total_clus);
197 exfat_info("Free Clusters: \t\t\t\t%u\n", total_clus-used_clus);
211 int main(int argc, char *argv[])
214 int ret = EXIT_FAILURE;
215 struct exfat_blk_dev bd;
216 struct exfat_user_input ui;
217 bool version_only = false;
219 init_user_input(&ui);
221 if (!setlocale(LC_CTYPE, ""))
222 exfat_err("failed to init locale/codeset\n");
225 while ((c = getopt_long(argc, argv, "iVh", opts, NULL)) != EOF)
243 memset(ui.dev_name, 0, sizeof(ui.dev_name));
244 snprintf(ui.dev_name, sizeof(ui.dev_name), "%s", argv[1]);
246 ret = exfat_get_blk_dev_info(&ui, &bd);
250 ret = exfat_show_ondisk_all_info(&bd);