Home | History | Annotate | Line # | Download | only in ffs
      1 /*	$NetBSD: ffs_appleufs.c,v 1.16 2025/12/24 15:06:59 nia Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2002 Darrin B. Jewell
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: ffs_appleufs.c,v 1.16 2025/12/24 15:06:59 nia Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/endian.h>
     33 #include <sys/time.h>
     34 #if defined(_KERNEL)
     35 #include <sys/kernel.h>
     36 #include <sys/systm.h>
     37 #include <sys/cprng.h>
     38 #endif
     39 
     40 #include <ufs/ufs/dinode.h>
     41 #include <ufs/ufs/ufs_bswap.h>
     42 #include <ufs/ffs/fs.h>
     43 #include <ufs/ffs/ffs_extern.h>
     44 
     45 #if !defined(_KERNEL) && !defined(STANDALONE)
     46 #include <stddef.h>
     47 #include <stdio.h>
     48 #include <stdlib.h>
     49 #include <string.h>
     50 #include <errno.h>
     51 #include <assert.h>
     52 #define KASSERT(x) assert(x)
     53 #endif
     54 
     55 /*
     56  * This is the same calculation as in_cksum.
     57  */
     58 u_int16_t
     59 ffs_appleufs_cksum(const struct appleufslabel *appleufs)
     60 {
     61 	const u_int16_t *p = (const u_int16_t *)appleufs;
     62 	int len = APPLEUFS_LABEL_SIZE; /* sizeof(struct appleufslabel) */
     63 	long res = 0;
     64 	while (len > 1)  {
     65 		res += *p++;
     66 		len -= 2;
     67 	}
     68 #if 0 /* APPLEUFS_LABEL_SIZE is guaranteed to be even */
     69 	if (len == 1)
     70 		res += htobe16(*(u_char *)p<<8);
     71 #endif
     72 	res = (res >> 16) + (res & 0xffff);
     73 	res += (res >> 16);
     74 	return (~res);
     75 }
     76 
     77 /*
     78  * Copies o to n, validating and byteswapping along the way. Returns 0 if ok,
     79  * EINVAL if not valid.
     80  */
     81 int
     82 ffs_appleufs_validate(const char *name, const struct appleufslabel *o,
     83     struct appleufslabel *n)
     84 {
     85 	struct appleufslabel tmp;
     86 
     87 	if (!n)
     88 		n = &tmp;
     89 	if (o->ul_magic != be32toh(APPLEUFS_LABEL_MAGIC))
     90 		return EINVAL;
     91 
     92 	*n = *o;
     93 	n->ul_checksum = 0;
     94 	n->ul_checksum = ffs_appleufs_cksum(n);
     95 	n->ul_magic = be32toh(o->ul_magic);
     96 	n->ul_version = be32toh(o->ul_version);
     97 	n->ul_time = be32toh(o->ul_time);
     98 	n->ul_namelen = be16toh(o->ul_namelen);
     99 
    100 	if (n->ul_checksum != o->ul_checksum)
    101 		return EINVAL;
    102 	if (n->ul_namelen == 0)
    103 		return EINVAL;
    104 	if (n->ul_namelen > APPLEUFS_MAX_LABEL_NAME)
    105 		n->ul_namelen = APPLEUFS_MAX_LABEL_NAME;
    106 
    107 	n->ul_name[n->ul_namelen - 1] = '\0';
    108 
    109 #ifdef DEBUG
    110 	printf("%s: found APPLE UFS label v%d: \"%s\"\n", name,
    111 	    n->ul_version, n->ul_name);
    112 #endif
    113 	n->ul_uuid = be64toh(o->ul_uuid);
    114 
    115 	return 0;
    116 }
    117 
    118 void
    119 ffs_appleufs_set(struct appleufslabel *appleufs, const char *name, time_t t,
    120     uint64_t uuid)
    121 {
    122 	size_t namelen;
    123 
    124 	if (!name)
    125 		name = "untitled";
    126 	if (t == ((time_t)-1)) {
    127 #if defined(_KERNEL)
    128 		t = time_second;
    129 #elif defined(STANDALONE)
    130 		t = 0;
    131 #else
    132 		(void)time(&t);
    133 #endif
    134 	}
    135 	if (uuid == 0) {
    136 #if defined(_KERNEL) && !defined(STANDALONE)
    137 		uuid = cprng_fast64();
    138 #endif
    139 	}
    140 	namelen = strlen(name);
    141 	if (namelen > APPLEUFS_MAX_LABEL_NAME)
    142 		namelen = APPLEUFS_MAX_LABEL_NAME;
    143 	memset(appleufs, 0, APPLEUFS_LABEL_SIZE);
    144 	appleufs->ul_magic   = htobe32(APPLEUFS_LABEL_MAGIC);
    145 	appleufs->ul_version = htobe32(APPLEUFS_LABEL_VERSION);
    146 	appleufs->ul_time    = htobe32((u_int32_t)t);
    147 	appleufs->ul_namelen = htobe16(namelen);
    148 	strncpy(appleufs->ul_name, name, namelen);
    149 	appleufs->ul_uuid    = htobe64(uuid);
    150 	appleufs->ul_checksum = ffs_appleufs_cksum(appleufs);
    151 }
    152