Home | History | Annotate | Line # | Download | only in fstyp
      1  1.3  christos /*	$NetBSD: hfsplus.c,v 1.3 2021/09/17 21:06:35 christos Exp $	*/
      2  1.1   tkusumi /*
      3  1.1   tkusumi  * Copyright (c) 2019 Conrad Meyer <cem (at) FreeBSD.org>.  All rights reserved.
      4  1.1   tkusumi  *
      5  1.1   tkusumi  * Redistribution and use in source and binary forms, with or without
      6  1.1   tkusumi  * modification, are permitted provided that the following conditions
      7  1.1   tkusumi  * are met:
      8  1.1   tkusumi  * 1. Redistributions of source code must retain the above copyright
      9  1.1   tkusumi  *    notice, this list of conditions and the following disclaimer.
     10  1.1   tkusumi  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1   tkusumi  *    notice, this list of conditions and the following disclaimer in the
     12  1.1   tkusumi  *    documentation and/or other materials provided with the distribution.
     13  1.1   tkusumi  *
     14  1.1   tkusumi  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     15  1.1   tkusumi  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  1.1   tkusumi  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     17  1.1   tkusumi  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     18  1.1   tkusumi  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     19  1.1   tkusumi  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     20  1.1   tkusumi  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     21  1.1   tkusumi  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     22  1.1   tkusumi  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     23  1.1   tkusumi  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     24  1.1   tkusumi  * SUCH DAMAGE.
     25  1.1   tkusumi  */
     26  1.1   tkusumi #include <sys/cdefs.h>
     27  1.3  christos __RCSID("$NetBSD: hfsplus.c,v 1.3 2021/09/17 21:06:35 christos Exp $");
     28  1.1   tkusumi 
     29  1.1   tkusumi #include <assert.h>
     30  1.1   tkusumi #include <err.h>
     31  1.1   tkusumi #include <errno.h>
     32  1.1   tkusumi #include <stdbool.h>
     33  1.1   tkusumi #include <stdint.h>
     34  1.1   tkusumi #include <stdio.h>
     35  1.1   tkusumi #include <stdlib.h>
     36  1.1   tkusumi #include <string.h>
     37  1.1   tkusumi 
     38  1.1   tkusumi #include "fstyp.h"
     39  1.1   tkusumi 
     40  1.1   tkusumi /*
     41  1.1   tkusumi  * https://developer.apple.com/library/archive/technotes/tn/tn1150.html
     42  1.1   tkusumi  */
     43  1.1   tkusumi 
     44  1.1   tkusumi #define	VOL_HDR_OFF	1024
     45  1.1   tkusumi 
     46  1.1   tkusumi typedef uint32_t hfsp_cat_nodeid;
     47  1.1   tkusumi 
     48  1.1   tkusumi typedef struct hfsp_ext_desc {
     49  1.1   tkusumi 	uint32_t	ex_startBlock;
     50  1.1   tkusumi 	uint32_t	ex_blockCount;
     51  1.1   tkusumi } hfsp_ext_desc;
     52  1.1   tkusumi 
     53  1.1   tkusumi typedef struct hfsp_fork_data {
     54  1.1   tkusumi 	uint64_t	fd_logicalSz;
     55  1.1   tkusumi 	uint32_t	fd_clumpSz;
     56  1.1   tkusumi 	uint32_t	fd_totalBlocks;
     57  1.1   tkusumi 	hfsp_ext_desc	fd_extents[8];
     58  1.1   tkusumi } hfsp_fork_data;
     59  1.1   tkusumi 
     60  1.1   tkusumi struct hfsp_vol_hdr {
     61  1.1   tkusumi 	char		hp_signature[2];
     62  1.1   tkusumi 	uint16_t	hp_version;
     63  1.1   tkusumi 	uint32_t	hp_attributes;
     64  1.1   tkusumi 	uint32_t	hp_lastMounted;
     65  1.1   tkusumi 	uint32_t	hp_journalInfoBlock;
     66  1.1   tkusumi 
     67  1.1   tkusumi 	/* Creation / etc dates. */
     68  1.1   tkusumi 	uint32_t	hp_create;
     69  1.1   tkusumi 	uint32_t	hp_modify;
     70  1.1   tkusumi 	uint32_t	hp_backup;
     71  1.1   tkusumi 	uint32_t	hp_checked;
     72  1.1   tkusumi 
     73  1.1   tkusumi 	/* Stats */
     74  1.1   tkusumi 	uint32_t	hp_files;
     75  1.1   tkusumi 	uint32_t	hp_folders;
     76  1.1   tkusumi 
     77  1.1   tkusumi 	/* Parameters */
     78  1.1   tkusumi 	uint32_t	hp_blockSize;
     79  1.1   tkusumi 	uint32_t	hp_totalBlocks;
     80  1.1   tkusumi 	uint32_t	hp_freeBlocks;
     81  1.1   tkusumi 
     82  1.1   tkusumi 	uint32_t	hp_nextAlloc;
     83  1.1   tkusumi 	uint32_t	hp_rsrcClumpSz;
     84  1.1   tkusumi 	uint32_t	hp_dataClumpSz;
     85  1.1   tkusumi 
     86  1.1   tkusumi 	hfsp_cat_nodeid	hp_nextCatID;
     87  1.1   tkusumi 
     88  1.1   tkusumi 	uint32_t	hp_writeCount;
     89  1.1   tkusumi 	uint64_t	hp_encodingsBM;
     90  1.1   tkusumi 
     91  1.1   tkusumi 	uint32_t	hp_finderInfo[8];
     92  1.1   tkusumi 
     93  1.1   tkusumi 	hfsp_fork_data	hp_allocationFile;
     94  1.1   tkusumi 	hfsp_fork_data	hp_extentsFile;
     95  1.1   tkusumi 	hfsp_fork_data	hp_catalogFile;
     96  1.1   tkusumi 	hfsp_fork_data	hp_attributesFile;
     97  1.1   tkusumi 	hfsp_fork_data	hp_startupFile;
     98  1.1   tkusumi };
     99  1.1   tkusumi _Static_assert(sizeof(struct hfsp_vol_hdr) == 512, "");
    100  1.1   tkusumi 
    101  1.1   tkusumi int
    102  1.1   tkusumi fstyp_hfsp(FILE *fp, char *label, size_t size)
    103  1.1   tkusumi {
    104  1.1   tkusumi 	struct hfsp_vol_hdr *hdr;
    105  1.1   tkusumi 	int retval;
    106  1.1   tkusumi 
    107  1.1   tkusumi 	retval = 1;
    108  1.1   tkusumi 	hdr = read_buf(fp, VOL_HDR_OFF, sizeof(*hdr));
    109  1.1   tkusumi 	if (hdr == NULL)
    110  1.1   tkusumi 		goto fail;
    111  1.1   tkusumi 
    112  1.1   tkusumi 	if ((strncmp(hdr->hp_signature, "H+", 2) != 0 || hdr->hp_version != 4)
    113  1.1   tkusumi 	    &&
    114  1.1   tkusumi 	    (strncmp(hdr->hp_signature, "HX", 2) != 0 || hdr->hp_version != 5))
    115  1.1   tkusumi 		goto fail;
    116  1.1   tkusumi 
    117  1.1   tkusumi 	/* This is an HFS+ volume. */
    118  1.1   tkusumi 	retval = 0;
    119  1.1   tkusumi 
    120  1.1   tkusumi 	/* No label support yet. */
    121  1.1   tkusumi 
    122  1.1   tkusumi fail:
    123  1.1   tkusumi 	free(hdr);
    124  1.1   tkusumi 	return (retval);
    125  1.1   tkusumi }
    126