1 /* 2 * Copyright (c) 2017 Conrad Meyer <cem@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/param.h> 28 #include <sys/endian.h> 29 30 #include <assert.h> 31 #include <err.h> 32 #include <errno.h> 33 #include <iconv.h> 34 #include <stdbool.h> 35 #include <stdint.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 40 #include "fstyp.h" 41 42 /* 43 * https://docs.microsoft.com/en-us/windows/win32/fileio/exfat-specification 44 */ 45 46 struct exfat_vbr { 47 char ev_jmp[3]; 48 char ev_fsname[8]; 49 char ev_zeros[53]; 50 uint64_t ev_part_offset; 51 uint64_t ev_vol_length; 52 uint32_t ev_fat_offset; 53 uint32_t ev_fat_length; 54 uint32_t ev_cluster_offset; 55 uint32_t ev_cluster_count; 56 uint32_t ev_rootdir_cluster; 57 uint32_t ev_vol_serial; 58 uint16_t ev_fs_revision; 59 uint16_t ev_vol_flags; 60 uint8_t ev_log_bytes_per_sect; 61 uint8_t ev_log_sect_per_clust; 62 uint8_t ev_num_fats; 63 uint8_t ev_drive_sel; 64 uint8_t ev_percent_used; 65 } __packed; 66 67 struct exfat_dirent { 68 uint8_t xde_type; 69 #define XDE_TYPE_INUSE_MASK 0x80 /* 1=in use */ 70 #define XDE_TYPE_INUSE_SHIFT 7 71 #define XDE_TYPE_CATEGORY_MASK 0x40 /* 0=primary */ 72 #define XDE_TYPE_CATEGORY_SHIFT 6 73 #define XDE_TYPE_IMPORTNC_MASK 0x20 /* 0=critical */ 74 #define XDE_TYPE_IMPORTNC_SHIFT 5 75 #define XDE_TYPE_CODE_MASK 0x1f 76 /* InUse=0, ..., TypeCode=0: EOD. */ 77 #define XDE_TYPE_EOD 0x00 78 #define XDE_TYPE_ALLOC_BITMAP (XDE_TYPE_INUSE_MASK | 0x01) 79 #define XDE_TYPE_UPCASE_TABLE (XDE_TYPE_INUSE_MASK | 0x02) 80 #define XDE_TYPE_VOL_LABEL (XDE_TYPE_INUSE_MASK | 0x03) 81 #define XDE_TYPE_FILE (XDE_TYPE_INUSE_MASK | 0x05) 82 #define XDE_TYPE_VOL_GUID (XDE_TYPE_INUSE_MASK | XDE_TYPE_IMPORTNC_MASK) 83 #define XDE_TYPE_STREAM_EXT (XDE_TYPE_INUSE_MASK | XDE_TYPE_CATEGORY_MASK) 84 #define XDE_TYPE_FILE_NAME (XDE_TYPE_INUSE_MASK | XDE_TYPE_CATEGORY_MASK | 0x01) 85 #define XDE_TYPE_VENDOR (XDE_TYPE_INUSE_MASK | XDE_TYPE_CATEGORY_MASK | XDE_TYPE_IMPORTNC_MASK) 86 #define XDE_TYPE_VENDOR_ALLOC (XDE_TYPE_INUSE_MASK | XDE_TYPE_CATEGORY_MASK | XDE_TYPE_IMPORTNC_MASK | 0x01) 87 union { 88 uint8_t xde_generic_[19]; 89 struct exde_primary { 90 /* 91 * Count of "secondary" dirents following this one. 92 * 93 * A single logical entity may be composed of a 94 * sequence of several dirents, starting with a primary 95 * one; the rest are secondary dirents. 96 */ 97 uint8_t xde_secondary_count_; 98 uint16_t xde_set_chksum_; 99 uint16_t xde_prim_flags_; 100 uint8_t xde_prim_generic_[14]; 101 } __packed xde_primary_; 102 struct exde_secondary { 103 uint8_t xde_sec_flags_; 104 uint8_t xde_sec_generic_[18]; 105 } __packed xde_secondary_; 106 } u; 107 uint32_t xde_first_cluster; 108 uint64_t xde_data_len; 109 } __packed; 110 #define xde_generic u.xde_generic_ 111 #define xde_secondary_count u.xde_primary_.xde_secondary_count 112 #define xde_set_chksum u.xde_primary_.xde_set_chksum_ 113 #define xde_prim_flags u.xde_primary_.xde_prim_flags_ 114 #define xde_sec_flags u.xde_secondary_.xde_sec_flags_ 115 _Static_assert(sizeof(struct exfat_dirent) == 32, "spec"); 116 117 struct exfat_de_label { 118 uint8_t xdel_type; /* XDE_TYPE_VOL_LABEL */ 119 uint8_t xdel_char_cnt; /* Length of UCS-2 label */ 120 uint16_t xdel_vol_lbl[11]; 121 uint8_t xdel_reserved[8]; 122 } __packed; 123 _Static_assert(sizeof(struct exfat_de_label) == 32, "spec"); 124 125 #define MAIN_BOOT_REGION_SECT 0 126 #define BACKUP_BOOT_REGION_SECT 12 127 128 #define SUBREGION_CHKSUM_SECT 11 129 130 #define FIRST_CLUSTER 2 131 #define BAD_BLOCK_SENTINEL 0xfffffff7u 132 #define END_CLUSTER_SENTINEL 0xffffffffu 133 134 static inline void * 135 read_sectn(FILE *fp, off_t sect, unsigned count, unsigned bytespersec) 136 { 137 return (read_buf(fp, sect * bytespersec, bytespersec * count)); 138 } 139 140 static inline void * 141 read_sect(FILE *fp, off_t sect, unsigned bytespersec) 142 { 143 return (read_sectn(fp, sect, 1, bytespersec)); 144 } 145 146 /* 147 * Compute the byte-by-byte multi-sector checksum of the given boot region 148 * (MAIN or BACKUP), for a given bytespersec (typically 512 or 4096). 149 * 150 * Endian-safe; result is host endian. 151 */ 152 static int 153 exfat_compute_boot_chksum(FILE *fp, unsigned region, unsigned bytespersec, 154 uint32_t *result) 155 { 156 unsigned char *sector; 157 unsigned n, sect; 158 uint32_t checksum; 159 160 checksum = 0; 161 for (sect = 0; sect < 11; sect++) { 162 sector = read_sect(fp, region + sect, bytespersec); 163 if (sector == NULL) 164 return (ENXIO); 165 for (n = 0; n < bytespersec; n++) { 166 if (sect == 0) { 167 switch (n) { 168 case 106: 169 case 107: 170 case 112: 171 continue; 172 } 173 } 174 checksum = ((checksum & 1) ? 0x80000000u : 0u) + 175 (checksum >> 1) + (uint32_t)sector[n]; 176 } 177 free(sector); 178 } 179 180 *result = checksum; 181 return (0); 182 } 183 184 static void 185 convert_label(const uint16_t *ucs2label /* LE */, unsigned ucs2len, char 186 *label_out, size_t label_sz) 187 { 188 const char *label; 189 char *label_out_orig; 190 iconv_t cd; 191 size_t srcleft, rc; 192 193 /* Currently hardcoded in fstyp.c as 256 or so. */ 194 assert(label_sz > 1); 195 196 if (ucs2len == 0) { 197 /* 198 * Kind of seems bogus, but the spec allows an empty label 199 * entry with the same meaning as no label. 200 */ 201 return; 202 } 203 204 if (ucs2len > 11) { 205 warnx("exfat: Bogus volume label length: %u", ucs2len); 206 return; 207 } 208 209 /* dstname="" means convert to the current locale. */ 210 cd = iconv_open("", EXFAT_ENC); 211 if (cd == (iconv_t)-1) { 212 warn("exfat: Could not open iconv"); 213 return; 214 } 215 216 label_out_orig = label_out; 217 218 /* Dummy up the byte pointer and byte length iconv's API wants. */ 219 label = (const void *)ucs2label; 220 srcleft = ucs2len * sizeof(*ucs2label); 221 222 rc = iconv(cd, __DECONST(char **, &label), &srcleft, &label_out, 223 &label_sz); 224 if (rc == (size_t)-1) { 225 warn("exfat: iconv()"); 226 *label_out_orig = '\0'; 227 } else { 228 /* NUL-terminate result (iconv advances label_out). */ 229 if (label_sz == 0) 230 label_out--; 231 *label_out = '\0'; 232 } 233 234 iconv_close(cd); 235 } 236 237 /* 238 * Using the FAT table, look up the next cluster in this chain. 239 */ 240 static uint32_t 241 exfat_fat_next(FILE *fp, const struct exfat_vbr *ev, unsigned BPS, 242 uint32_t cluster) 243 { 244 uint32_t fat_offset_sect, clsect, clsectoff; 245 uint32_t *fatsect, nextclust; 246 247 fat_offset_sect = le32toh(ev->ev_fat_offset); 248 clsect = fat_offset_sect + (cluster / (BPS / sizeof(cluster))); 249 clsectoff = (cluster % (BPS / sizeof(cluster))); 250 251 /* XXX This is pretty wasteful without a block cache for the FAT. */ 252 fatsect = read_sect(fp, clsect, BPS); 253 nextclust = le32toh(fatsect[clsectoff]); 254 free(fatsect); 255 256 return (nextclust); 257 } 258 259 static void 260 exfat_find_label(FILE *fp, const struct exfat_vbr *ev, unsigned BPS, 261 char *label_out, size_t label_sz) 262 { 263 uint32_t rootdir_cluster, sects_per_clust, cluster_offset_sect; 264 off_t rootdir_sect; 265 struct exfat_dirent *declust, *it; 266 267 cluster_offset_sect = le32toh(ev->ev_cluster_offset); 268 rootdir_cluster = le32toh(ev->ev_rootdir_cluster); 269 sects_per_clust = (1u << ev->ev_log_sect_per_clust); 270 271 if (rootdir_cluster < FIRST_CLUSTER) { 272 warnx("%s: invalid rootdir cluster %u < %d", __func__, 273 rootdir_cluster, FIRST_CLUSTER); 274 return; 275 } 276 277 278 for (; rootdir_cluster != END_CLUSTER_SENTINEL; 279 rootdir_cluster = exfat_fat_next(fp, ev, BPS, rootdir_cluster)) { 280 if (rootdir_cluster == BAD_BLOCK_SENTINEL) { 281 warnx("%s: Bogus bad block in root directory chain", 282 __func__); 283 return; 284 } 285 286 rootdir_sect = (rootdir_cluster - FIRST_CLUSTER) * 287 sects_per_clust + cluster_offset_sect; 288 declust = read_sectn(fp, rootdir_sect, sects_per_clust, BPS); 289 for (it = declust; 290 it < declust + (sects_per_clust * BPS / sizeof(*it)); it++) { 291 bool eod = false; 292 293 /* 294 * Simplistic directory traversal; doesn't do any 295 * validation of "MUST" requirements in spec. 296 */ 297 switch (it->xde_type) { 298 case XDE_TYPE_EOD: 299 eod = true; 300 break; 301 case XDE_TYPE_VOL_LABEL: { 302 struct exfat_de_label *lde = (void*)it; 303 convert_label(lde->xdel_vol_lbl, 304 lde->xdel_char_cnt, label_out, label_sz); 305 free(declust); 306 return; 307 } 308 } 309 310 if (eod) 311 break; 312 } 313 free(declust); 314 } 315 } 316 317 int 318 fstyp_exfat(FILE *fp, char *label, size_t size, const char *devpath) 319 { 320 struct exfat_vbr *ev; 321 uint32_t *cksect; 322 unsigned bytespersec; 323 uint32_t chksum; 324 int error; 325 326 cksect = NULL; 327 328 ev = (struct exfat_vbr *)read_buf(fp, 0, 512); 329 if (ev == NULL || strncmp(ev->ev_fsname, "EXFAT ", 8) != 0) 330 goto fail; 331 332 if (ev->ev_log_bytes_per_sect < 9 || ev->ev_log_bytes_per_sect > 12) { 333 warnx("exfat: Invalid BytesPerSectorShift"); 334 goto done; 335 } 336 337 bytespersec = (1u << ev->ev_log_bytes_per_sect); 338 339 error = exfat_compute_boot_chksum(fp, MAIN_BOOT_REGION_SECT, 340 bytespersec, &chksum); 341 if (error != 0) 342 goto done; 343 344 cksect = read_sect(fp, MAIN_BOOT_REGION_SECT + SUBREGION_CHKSUM_SECT, 345 bytespersec); 346 347 /* 348 * Technically the entire sector should be full of repeating 4-byte 349 * checksum pattern, but we only verify the first. 350 */ 351 if (chksum != le32toh(cksect[0])) { 352 warnx("exfat: Found checksum 0x%08x != computed 0x%08x", 353 le32toh(cksect[0]), chksum); 354 goto done; 355 } 356 357 if (show_label) 358 exfat_find_label(fp, ev, bytespersec, label, size); 359 360 done: 361 free(cksect); 362 free(ev); 363 return (0); 364 365 fail: 366 free(ev); 367 return (1); 368 } 369