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