bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1 | /* |
| 2 | main.c (08.11.10) |
| 3 | Prints detailed information about exFAT volume. |
| 4 | |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 5 | Free exFAT implementation. |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 6 | Copyright (C) 2011-2013 Andrew Nayenko |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 7 | |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 8 | This program is free software; you can redistribute it and/or modify |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 9 | it under the terms of the GNU General Public License as published by |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 10 | the Free Software Foundation, either version 2 of the License, or |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 18 | You should have received a copy of the GNU General Public License along |
| 19 | with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <fcntl.h> |
| 24 | #include <unistd.h> |
| 25 | #include <inttypes.h> |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include <exfat.h> |
| 29 | |
| 30 | static void print_generic_info(const struct exfat_super_block* sb) |
| 31 | { |
| 32 | printf("Volume serial number 0x%08x\n", |
| 33 | le32_to_cpu(sb->volume_serial)); |
| 34 | printf("FS version %hhu.%hhu\n", |
| 35 | sb->version.major, sb->version.minor); |
| 36 | printf("Sector size %10u\n", |
| 37 | SECTOR_SIZE(*sb)); |
| 38 | printf("Cluster size %10u\n", |
| 39 | CLUSTER_SIZE(*sb)); |
| 40 | } |
| 41 | |
| 42 | static void print_sector_info(const struct exfat_super_block* sb) |
| 43 | { |
| 44 | printf("Sectors count %10"PRIu64"\n", |
| 45 | le64_to_cpu(sb->sector_count)); |
| 46 | } |
| 47 | |
| 48 | static void print_cluster_info(const struct exfat_super_block* sb) |
| 49 | { |
| 50 | printf("Clusters count %10u\n", |
| 51 | le32_to_cpu(sb->cluster_count)); |
| 52 | } |
| 53 | |
| 54 | static void print_other_info(const struct exfat_super_block* sb) |
| 55 | { |
| 56 | printf("First sector %10"PRIu64"\n", |
| 57 | le64_to_cpu(sb->sector_start)); |
| 58 | printf("FAT first sector %10u\n", |
| 59 | le32_to_cpu(sb->fat_sector_start)); |
| 60 | printf("FAT sectors count %10u\n", |
| 61 | le32_to_cpu(sb->fat_sector_count)); |
| 62 | printf("First cluster sector %10u\n", |
| 63 | le32_to_cpu(sb->cluster_sector_start)); |
| 64 | printf("Root directory cluster %10u\n", |
| 65 | le32_to_cpu(sb->rootdir_cluster)); |
| 66 | printf("Volume state 0x%04hx\n", |
| 67 | le16_to_cpu(sb->volume_state)); |
| 68 | printf("FATs count %10hhu\n", |
| 69 | sb->fat_count); |
| 70 | printf("Drive number 0x%02hhx\n", |
| 71 | sb->drive_no); |
| 72 | printf("Allocated space %9hhu%%\n", |
| 73 | sb->allocated_percent); |
| 74 | } |
| 75 | |
| 76 | static int dump_sb(const char* spec) |
| 77 | { |
| 78 | struct exfat_dev* dev; |
| 79 | struct exfat_super_block sb; |
| 80 | |
| 81 | dev = exfat_open(spec, EXFAT_MODE_RO); |
| 82 | if (dev == NULL) |
| 83 | return 1; |
| 84 | |
| 85 | if (exfat_read(dev, &sb, sizeof(struct exfat_super_block)) < 0) |
| 86 | { |
| 87 | exfat_close(dev); |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 88 | exfat_error("failed to read from `%s'", spec); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 89 | return 1; |
| 90 | } |
| 91 | if (memcmp(sb.oem_name, "EXFAT ", sizeof(sb.oem_name)) != 0) |
| 92 | { |
| 93 | exfat_close(dev); |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 94 | exfat_error("exFAT file system is not found on `%s'", spec); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | print_generic_info(&sb); |
| 99 | print_sector_info(&sb); |
| 100 | print_cluster_info(&sb); |
| 101 | print_other_info(&sb); |
| 102 | |
| 103 | exfat_close(dev); |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static void dump_sectors(struct exfat* ef) |
| 108 | { |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 109 | off_t a = 0, b = 0; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 110 | |
| 111 | printf("Used sectors "); |
| 112 | while (exfat_find_used_sectors(ef, &a, &b) == 0) |
| 113 | printf(" %"PRIu64"-%"PRIu64, a, b); |
| 114 | puts(""); |
| 115 | } |
| 116 | |
| 117 | static int dump_full(const char* spec, bool used_sectors) |
| 118 | { |
| 119 | struct exfat ef; |
| 120 | uint32_t free_clusters; |
| 121 | uint64_t free_sectors; |
| 122 | |
| 123 | if (exfat_mount(&ef, spec, "ro") != 0) |
| 124 | return 1; |
| 125 | |
| 126 | free_clusters = exfat_count_free_clusters(&ef); |
| 127 | free_sectors = (uint64_t) free_clusters << ef.sb->spc_bits; |
| 128 | |
| 129 | printf("Volume label %15s\n", exfat_get_label(&ef)); |
| 130 | print_generic_info(ef.sb); |
| 131 | print_sector_info(ef.sb); |
| 132 | printf("Free sectors %10"PRIu64"\n", free_sectors); |
| 133 | print_cluster_info(ef.sb); |
| 134 | printf("Free clusters %10u\n", free_clusters); |
| 135 | print_other_info(ef.sb); |
| 136 | if (used_sectors) |
| 137 | dump_sectors(&ef); |
| 138 | |
| 139 | exfat_unmount(&ef); |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static void usage(const char* prog) |
| 144 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 145 | fprintf(stderr, "Usage: %s [-s] [-u] [-V] <device>\n", prog); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 146 | exit(1); |
| 147 | } |
| 148 | |
| 149 | int main(int argc, char* argv[]) |
| 150 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 151 | int opt; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 152 | const char* spec = NULL; |
| 153 | bool sb_only = false; |
| 154 | bool used_sectors = false; |
| 155 | |
| 156 | printf("dumpexfat %u.%u.%u\n", |
| 157 | EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH); |
| 158 | |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 159 | while ((opt = getopt(argc, argv, "suV")) != -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 160 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 161 | switch (opt) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 162 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 163 | case 's': |
| 164 | sb_only = true; |
| 165 | break; |
| 166 | case 'u': |
| 167 | used_sectors = true; |
| 168 | break; |
| 169 | case 'V': |
bigbiff | c40c1c5 | 2014-11-01 09:34:57 -0400 | [diff] [blame] | 170 | puts("Copyright (C) 2011-2013 Andrew Nayenko"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 171 | return 0; |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 172 | default: |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 173 | usage(argv[0]); |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 174 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 175 | } |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 176 | if (argc - optind != 1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 177 | usage(argv[0]); |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 178 | spec = argv[optind]; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 179 | |
| 180 | if (sb_only) |
| 181 | return dump_sb(spec); |
| 182 | |
| 183 | return dump_full(spec, used_sectors); |
| 184 | } |