1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/* $NetBSD: filecore_utils.c,v 1.12 2020/09/29 02:58:52 msaitoh Exp $ */
/*-
* Copyright (c) 1994 The Regents of the University of California.
* All rights reserved.
*
* This code includes code derived from software contributed to the
* NetBSD project by Mark Brinicombe.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* filecore_utils.c 1.1 1998/6/26
*/
/*-
* Copyright (c) 1998 Andrew McMurry
*
* This code includes code derived from software contributed to the
* NetBSD project by Mark Brinicombe.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* filecore_utils.c 1.1 1998/6/26
*/
/*
* Copyright (c) 1998 Christopher G. Demetriou. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Christopher G. Demetriou
* for the NetBSD Project.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: filecore_utils.c,v 1.12 2020/09/29 02:58:52 msaitoh Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/stat.h>
#include <sys/buf.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#include <sys/dirent.h>
#include <sys/kauth.h>
#include <fs/filecorefs/filecore.h>
#include <fs/filecorefs/filecore_extern.h>
#include <fs/filecorefs/filecore_node.h>
#include <fs/filecorefs/filecore_mount.h>
/*
* int filecore_bbchecksum(u_char *bootblock)
*
* Calculates the filecore boot block checksum. This is used to validate
* a filecore boot block on the disk. If a boot block is validated then
* it is used to locate the partition table. If the boot block is not
* validated, it is assumed that the whole disk is NetBSD.
*
* The basic algorithm is:
*
* for (each byte in block, excluding checksum) {
* sum += byte;
* if (sum > 255)
* sum -= 255;
* }
*
* That's equivalent to summing all of the bytes in the block
* (excluding the checksum byte, of course), then calculating the
* checksum as "cksum = sum - ((sum - 1) / 255) * 255)". That
* expression may or may not yield a faster checksum function,
* but it's easier to reason about.
*
* Note that if you have a block filled with bytes of a single
* value "X" (regardless of that value!) and calculate the cksum
* of the block (excluding the checksum byte), you will _always_
* end up with a checksum of X. (Do the math; that can be derived
* from the checksum calculation function!) That means that
* blocks which contain bytes which all have the same value will
* always checksum properly. That's a _very_ unlikely occurrence
* (probably impossible, actually) for a valid filecore boot block,
* so we treat such blocks as invalid.
*/
int
filecore_bbchecksum(void *bb)
{
u_char *bootblock = bb;
u_char byte0, accum_diff;
u_int sum;
int i;
sum = 0;
accum_diff = 0;
byte0 = bootblock[0];
/*
* Sum the contents of the block, keeping track of whether
* or not all bytes are the same. If 'accum_diff' ends up
* being zero, all of the bytes are, in fact, the same.
*/
for (i = 0; i < 511; ++i) {
sum += bootblock[i];
accum_diff |= bootblock[i] ^ byte0;
}
/*
* Check to see if the checksum byte is the same as the
* rest of the bytes, too. (Note that if all of the bytes
* are the same except the checksum, a checksum compare
* won't succeed, but that's not our problem.)
*/
accum_diff |= bootblock[i] ^ byte0;
sum = (sum - ((sum - 1) / 255) * 255);
/*
* If all bytes in block are the same
* or the checksum does not match ; call it invalid.
*/
if (accum_diff == 0 || sum != bootblock[511])
return -1;
return 0;
}
mode_t
filecore_mode(struct filecore_node *ip)
{
mode_t m = 0;
int rf = 0;
if ((ip->i_dirent.attr & FILECORE_ATTR_READ) ||
(ip->i_mnt->fc_mntflags & FILECOREMNT_OWNREAD) ||
(ip->i_dirent.attr & FILECORE_ATTR_DIR))
rf = 1;
if (ip->i_mnt->fc_mntflags & FILECOREMNT_ALLACCESS) {
m |= S_IRUSR | S_IXUSR;
if (rf || (ip->i_dirent.attr & FILECORE_ATTR_OREAD))
m |= S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
} else if (ip->i_mnt->fc_mntflags & FILECOREMNT_OWNACCESS) {
if (rf) m |= S_IRUSR | S_IXUSR;
if (ip->i_dirent.attr & FILECORE_ATTR_OREAD)
m |= S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
} else {
m |= S_IRUSR | S_IXUSR;
if (rf) m |= S_IRGRP | S_IXGRP;
if (ip->i_dirent.attr & FILECORE_ATTR_OREAD)
m |= S_IROTH | S_IXOTH;
}
if (ip->i_dirent.attr & FILECORE_ATTR_DIR) {
m |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
} else
m |= S_IFREG;
return m;
}
struct timespec
filecore_time(struct filecore_node *ip)
{
struct timespec ts;
u_int64_t cs;
cs = (((u_int64_t)(ip->i_dirent.load & 0xFF)) << 32)
+ ip->i_dirent.exec - ((u_int64_t)1725772500 << 7);
ts.tv_sec = cs / 100;
ts.tv_nsec = (cs % 100) * 10000000;
return ts;
}
ino_t
filecore_getparent(struct filecore_node *ip)
{
struct buf *pbp;
u_int32_t addr;
u_int32_t paddr;
int error = 0;
int i = 0;
#ifdef FILECORE_DEBUG
printf("filecore_getparent(ino=%llx)\n", (long long)ip->i_number);
#endif
if (ip->i_parent != -2) {
return ip->i_parent;
}
if (ip->i_number == FILECORE_ROOTINO) {
ip->i_parent = ip->i_number;
return ip->i_number;
}
addr = ip->i_number & FILECORE_INO_MASK;
/* Read directory data for parent dir to find its parent */
#ifdef FILECORE_DEBUG
printf("filecore_getparent() read parent dir contents\n");
#endif
error = filecore_bread(ip->i_mnt, addr, FILECORE_DIR_SIZE,
NOCRED, &pbp);
if (error) {
return error;
}
paddr = fcdirtail(pbp->b_data)->parent1
| fcdirtail(pbp->b_data)->parent2 << 16;
#ifdef FILECORE_DEBUG_BR
printf("brelse(%p) ut1\n", pbp);
#endif
brelse(pbp, 0);
/* If parent's parent is the parent then parent is root dir */
if (paddr == addr) {
ip->i_parent = FILECORE_ROOTINO;
return FILECORE_ROOTINO;
}
#ifdef FILECORE_DEBUG
printf("filecore_getparent() read grand-parent dir contents\n");
#endif
error = filecore_bread(ip->i_mnt, paddr, FILECORE_DIR_SIZE,
NOCRED, &pbp);
if (error) {
return error;
}
while (fcdirentry(pbp->b_data,i)->addr != addr) {
if (fcdirentry(pbp->b_data, i++)->name[0] == 0) {
#ifdef FILECORE_DEBUG_BR
printf("brelse(%p) ut2\n", pbp);
#endif
brelse(pbp, 0);
return FILECORE_ROOTINO;
}
}
#ifdef FILECORE_DEBUG_BR
printf("brelse(%p) ut3\n", pbp);
#endif
brelse(pbp, 0);
ip->i_parent = paddr + (i << FILECORE_INO_INDEX);
return (paddr + (i << FILECORE_INO_INDEX));
}
int
filecore_fn2unix(char *fcfn, char *ufn, u_int16_t *len)
{
int i = 0;
if (*fcfn == 0)
return (-1);
while (i++ < 10 && *fcfn >= ' ') {
if (*fcfn == '/')
*ufn++ = '.';
else
*ufn++ = *fcfn;
fcfn++;
}
#ifdef notdef
if (ip->i_mnt->fc_mntflags & FILECOREMNT_FILETYPE) {
*ufn++ = ',';
*ufn++ = hexdigits[(ip->i_dirent.load >> 10) & 15];
*ufn++ = hexdigits[(ip->i_dirent.load >> 9) & 15];
*ufn++ = hexdigits[(ip->i_dirent.load >> 8) & 15];
}
#endif
*ufn = 0;
*len = i - 1;
return 0;
}
int
filecore_fncmp(const char *fcfn, const char *ufn, u_short len)
{
char f, u;
int i = 0;
if (*fcfn == 0 || len > 10)
return -1;
while (i++ < len) {
if (*fcfn < ' ')
return 1;
f = *fcfn++;
u = *ufn++;
if (u == '.')
u = '/';
if (u >= 'a' && u <= 'z') u -= 'a' - 'A';
if (f >= 'a' && f <= 'z') f -= 'a' - 'A';
if (f < u)
return 1;
else if (f > u)
return -1;
}
if (len == 10 || *fcfn < ' ')
return 0;
return -1;
}
|