4 * Copyright (c) 2002 Darrin B. Jewell
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Darrin B. Jewell
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD$");
36 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
43 #include <ufs/ufs/dinode.h>
44 #include <ufs/ufs/ufs_bswap.h>
45 #include <ufs/ffs/fs.h>
46 #include <ufs/ffs/ffs_extern.h>
48 #if !defined(_KERNEL) && !defined(STANDALONE)
55 #define KASSERT(x) assert(x)
59 * this is the same calculation as in_cksum
62 ffs_appleufs_cksum(const struct appleufslabel
*appleufs
)
64 const u_int16_t
*p
= (const u_int16_t
*)appleufs
;
65 int len
= APPLEUFS_LABEL_SIZE
; /* sizeof(struct appleufslabel) */
71 #if 0 /* APPLEUFS_LABEL_SIZE is guaranteed to be even */
73 res
+= htobe16(*(u_char
*)p
<<8);
75 res
= (res
>> 16) + (res
& 0xffff);
80 /* copies o to n, validating and byteswapping along the way
81 * returns 0 if ok, EINVAL if not valid
84 ffs_appleufs_validate(const char *name
, const struct appleufslabel
*o
,
85 struct appleufslabel
*n
)
87 struct appleufslabel tmp
;
90 if (o
->ul_magic
!= be32toh(APPLEUFS_LABEL_MAGIC
)) {
95 n
->ul_checksum
= ffs_appleufs_cksum(n
);
96 if (n
->ul_checksum
!= o
->ul_checksum
) {
97 #if defined(DIAGNOSTIC) || !defined(_KERNEL)
98 printf("%s: invalid APPLE UFS checksum. found 0x%x, expecting 0x%x",
99 name
,o
->ul_checksum
,n
->ul_checksum
);
103 n
->ul_magic
= be32toh(o
->ul_magic
);
104 n
->ul_version
= be32toh(o
->ul_version
);
105 n
->ul_time
= be32toh(o
->ul_time
);
106 n
->ul_namelen
= be16toh(o
->ul_namelen
);
108 if (n
->ul_namelen
> APPLEUFS_MAX_LABEL_NAME
) {
109 #if defined(DIAGNOSTIC) || !defined(_KERNEL)
110 printf("%s: APPLE UFS label name too long, truncated.\n",
113 n
->ul_namelen
= APPLEUFS_MAX_LABEL_NAME
;
115 /* if len is max, will set ul_unused1 */
116 n
->ul_name
[n
->ul_namelen
] = '\0';
119 printf("%s: found APPLE UFS label v%d: \"%s\"\n",
120 name
,n
->ul_version
,n
->ul_name
);
122 n
->ul_uuid
= be64toh(o
->ul_uuid
);
128 ffs_appleufs_set(struct appleufslabel
*appleufs
, const char *name
, time_t t
,
132 if (!name
) name
= "untitled";
133 if (t
== ((time_t)-1)) {
136 #elif defined(STANDALONE)
143 #if defined(_KERNEL) && !defined(STANDALONE)
146 uuid
|= arc4random();
149 namelen
= strlen(name
);
150 if (namelen
> APPLEUFS_MAX_LABEL_NAME
)
151 namelen
= APPLEUFS_MAX_LABEL_NAME
;
152 memset(appleufs
, 0, APPLEUFS_LABEL_SIZE
);
153 appleufs
->ul_magic
= htobe32(APPLEUFS_LABEL_MAGIC
);
154 appleufs
->ul_version
= htobe32(APPLEUFS_LABEL_VERSION
);
155 appleufs
->ul_time
= htobe32((u_int32_t
)t
);
156 appleufs
->ul_namelen
= htobe16(namelen
);
157 strncpy(appleufs
->ul_name
,name
,namelen
);
158 appleufs
->ul_uuid
= htobe64(uuid
);
159 appleufs
->ul_checksum
= ffs_appleufs_cksum(appleufs
);