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