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