Sun Jan 24 14:37:32 2021 UTC ()
fstyp: Fix exfat detection

taken-from FreeBSD ddf61156132b610915325769cbb93ea11be0d433


(tkusumi)
diff -r1.3 -r1.4 src/usr.sbin/fstyp/exfat.c

cvs diff -r1.3 -r1.4 src/usr.sbin/fstyp/exfat.c (expand / switch to unified diff)

--- src/usr.sbin/fstyp/exfat.c 2020/02/08 12:56:56 1.3
+++ src/usr.sbin/fstyp/exfat.c 2021/01/24 14:37:32 1.4
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: exfat.c,v 1.3 2020/02/08 12:56:56 fox Exp $ */ 1/* $NetBSD: exfat.c,v 1.4 2021/01/24 14:37:32 tkusumi Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2017 Conrad Meyer <cem@FreeBSD.org> 4 * Copyright (c) 2017 Conrad Meyer <cem@FreeBSD.org>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -16,27 +16,27 @@ @@ -16,27 +16,27 @@
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE. 26 * SUCH DAMAGE.
27 */ 27 */
28#include <sys/cdefs.h> 28#include <sys/cdefs.h>
29__RCSID("$NetBSD: exfat.c,v 1.3 2020/02/08 12:56:56 fox Exp $"); 29__RCSID("$NetBSD: exfat.c,v 1.4 2021/01/24 14:37:32 tkusumi Exp $");
30 30
31#include <sys/param.h> 31#include <sys/param.h>
32#include <sys/endian.h> 32#include <sys/endian.h>
33 33
34#include <assert.h> 34#include <assert.h>
35#include <err.h> 35#include <err.h>
36#include <errno.h> 36#include <errno.h>
37#include <iconv.h> 37#include <iconv.h>
38#include <stdbool.h> 38#include <stdbool.h>
39#include <stdint.h> 39#include <stdint.h>
40#include <stdio.h> 40#include <stdio.h>
41#include <stdlib.h> 41#include <stdlib.h>
42#include <string.h> 42#include <string.h>
@@ -317,56 +317,53 @@ exfat_find_label(FILE *fp, const struct  @@ -317,56 +317,53 @@ exfat_find_label(FILE *fp, const struct
317 free(declust); 317 free(declust);
318 } 318 }
319} 319}
320 320
321int 321int
322fstyp_exfat(FILE *fp, char *label, size_t size) 322fstyp_exfat(FILE *fp, char *label, size_t size)
323{ 323{
324 struct exfat_vbr *ev; 324 struct exfat_vbr *ev;
325 uint32_t *cksect; 325 uint32_t *cksect;
326 unsigned bytespersec; 326 unsigned bytespersec;
327 uint32_t chksum; 327 uint32_t chksum;
328 int error; 328 int error;
329 329
 330 error = 1;
330 cksect = NULL; 331 cksect = NULL;
331 
332 ev = (struct exfat_vbr *)read_buf(fp, 0, 512); 332 ev = (struct exfat_vbr *)read_buf(fp, 0, 512);
333 if (ev == NULL || strncmp(ev->ev_fsname, "EXFAT ", 8) != 0) 333 if (ev == NULL || strncmp(ev->ev_fsname, "EXFAT ", 8) != 0)
334 goto fail; 334 goto out;
335 335
336 if (ev->ev_log_bytes_per_sect < 9 || ev->ev_log_bytes_per_sect > 12) { 336 if (ev->ev_log_bytes_per_sect < 9 || ev->ev_log_bytes_per_sect > 12) {
337 warnx("exfat: Invalid BytesPerSectorShift"); 337 warnx("exfat: Invalid BytesPerSectorShift");
338 goto done; 338 goto out;
339 } 339 }
340 340
341 bytespersec = (1u << ev->ev_log_bytes_per_sect); 341 bytespersec = (1u << ev->ev_log_bytes_per_sect);
342 342
343 error = exfat_compute_boot_chksum(fp, MAIN_BOOT_REGION_SECT, 343 error = exfat_compute_boot_chksum(fp, MAIN_BOOT_REGION_SECT,
344 bytespersec, &chksum); 344 bytespersec, &chksum);
345 if (error != 0) 345 if (error != 0)
346 goto done; 346 goto out;
347 347
348 cksect = read_sect(fp, MAIN_BOOT_REGION_SECT + SUBREGION_CHKSUM_SECT, 348 cksect = read_sect(fp, MAIN_BOOT_REGION_SECT + SUBREGION_CHKSUM_SECT,
349 bytespersec); 349 bytespersec);
350 350
351 /* 351 /*
352 * Technically the entire sector should be full of repeating 4-byte 352 * Technically the entire sector should be full of repeating 4-byte
353 * checksum pattern, but we only verify the first. 353 * checksum pattern, but we only verify the first.
354 */ 354 */
355 if (chksum != le32toh(cksect[0])) { 355 if (chksum != le32toh(cksect[0])) {
356 warnx("exfat: Found checksum 0x%08x != computed 0x%08x", 356 warnx("exfat: Found checksum 0x%08x != computed 0x%08x",
357 le32toh(cksect[0]), chksum); 357 le32toh(cksect[0]), chksum);
358 goto done; 358 error = 1;
 359 goto out;
359 } 360 }
360 361
361 if (show_label) 362 if (show_label)
362 exfat_find_label(fp, ev, bytespersec, label, size); 363 exfat_find_label(fp, ev, bytespersec, label, size);
363 364
364done: 365out:
365 free(cksect); 366 free(cksect);
366 free(ev); 367 free(ev);
367 return (0); 368 return (error != 0);
368 
369fail: 
370 free(ev); 
371 return (1); 
372} 369}