rump_allserver.c revision 1.29 1 1.29 bad /* $NetBSD: rump_allserver.c,v 1.29 2013/12/16 23:27:33 bad Exp $ */
2 1.1 pooka
3 1.1 pooka /*-
4 1.16 pooka * Copyright (c) 2010, 2011 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Redistribution and use in source and binary forms, with or without
7 1.1 pooka * modification, are permitted provided that the following conditions
8 1.1 pooka * are met:
9 1.1 pooka * 1. Redistributions of source code must retain the above copyright
10 1.1 pooka * notice, this list of conditions and the following disclaimer.
11 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer in the
13 1.1 pooka * documentation and/or other materials provided with the distribution.
14 1.1 pooka *
15 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 pooka * SUCH DAMAGE.
26 1.1 pooka */
27 1.1 pooka
28 1.24 pooka #include <rump/rumpuser_port.h>
29 1.24 pooka
30 1.1 pooka #ifndef lint
31 1.29 bad __RCSID("$NetBSD: rump_allserver.c,v 1.29 2013/12/16 23:27:33 bad Exp $");
32 1.1 pooka #endif /* !lint */
33 1.1 pooka
34 1.1 pooka #include <sys/types.h>
35 1.4 pooka #include <sys/signal.h>
36 1.24 pooka #include <sys/stat.h>
37 1.24 pooka
38 1.6 pooka #include <dlfcn.h>
39 1.1 pooka #include <errno.h>
40 1.9 pooka #include <fcntl.h>
41 1.4 pooka #include <semaphore.h>
42 1.1 pooka #include <stdio.h>
43 1.1 pooka #include <stdlib.h>
44 1.24 pooka #include <stdint.h>
45 1.1 pooka #include <string.h>
46 1.1 pooka #include <unistd.h>
47 1.24 pooka
48 1.24 pooka #include <rump/rump.h>
49 1.24 pooka #include <rump/rump_syscalls.h>
50 1.27 pooka #include <rump/rumpdefs.h>
51 1.1 pooka
52 1.22 joerg __dead static void
53 1.1 pooka usage(void)
54 1.1 pooka {
55 1.1 pooka
56 1.24 pooka #ifndef PLATFORM_HAS_SETGETPROGNAME
57 1.24 pooka #define getprogname() "rump_server"
58 1.24 pooka #endif
59 1.13 wiz fprintf(stderr, "usage: %s [-s] [-c ncpu] [-d drivespec] [-l libs] "
60 1.12 pooka "[-m modules] bindurl\n", getprogname());
61 1.1 pooka exit(1);
62 1.1 pooka }
63 1.1 pooka
64 1.22 joerg __dead static void
65 1.2 pooka die(int sflag, int error, const char *reason)
66 1.2 pooka {
67 1.2 pooka
68 1.29 bad if (reason != NULL)
69 1.29 bad fputs(reason, stderr);
70 1.29 bad if (error > 0)
71 1.29 bad fprintf(stderr, ": %s", strerror(error));
72 1.29 bad fputc('\n', stderr);
73 1.2 pooka if (!sflag)
74 1.2 pooka rump_daemonize_done(error);
75 1.2 pooka exit(1);
76 1.2 pooka }
77 1.2 pooka
78 1.4 pooka static sem_t sigsem;
79 1.4 pooka static void
80 1.4 pooka sigreboot(int sig)
81 1.4 pooka {
82 1.4 pooka
83 1.4 pooka sem_post(&sigsem);
84 1.4 pooka }
85 1.4 pooka
86 1.9 pooka static const char *const disktokens[] = {
87 1.9 pooka #define DKEY 0
88 1.9 pooka "key",
89 1.9 pooka #define DFILE 1
90 1.9 pooka "hostpath",
91 1.9 pooka #define DSIZE 2
92 1.18 pooka #define DSIZE_E -1
93 1.9 pooka "size",
94 1.14 pooka #define DOFFSET 3
95 1.14 pooka "offset",
96 1.16 pooka #define DLABEL 4
97 1.16 pooka "disklabel",
98 1.17 pooka #define DTYPE 5
99 1.17 pooka "type",
100 1.9 pooka NULL
101 1.9 pooka };
102 1.9 pooka
103 1.9 pooka struct etfsreg {
104 1.9 pooka const char *key;
105 1.9 pooka const char *hostpath;
106 1.9 pooka off_t flen;
107 1.14 pooka off_t foffset;
108 1.16 pooka char partition;
109 1.9 pooka enum rump_etfs_type type;
110 1.9 pooka };
111 1.9 pooka
112 1.17 pooka struct etfstype {
113 1.17 pooka const char *name;
114 1.17 pooka enum rump_etfs_type type;
115 1.17 pooka } etfstypes[] = {
116 1.17 pooka { "blk", RUMP_ETFS_BLK },
117 1.17 pooka { "chr", RUMP_ETFS_CHR },
118 1.17 pooka { "reg", RUMP_ETFS_REG },
119 1.17 pooka };
120 1.17 pooka
121 1.28 pooka static void processlabel(int, int, int, off_t *, off_t *);
122 1.28 pooka
123 1.1 pooka int
124 1.1 pooka main(int argc, char *argv[])
125 1.1 pooka {
126 1.2 pooka const char *serverurl;
127 1.9 pooka struct etfsreg *etfs = NULL;
128 1.9 pooka unsigned netfs = 0, curetfs = 0;
129 1.1 pooka int error;
130 1.2 pooka int ch, sflag;
131 1.24 pooka unsigned i;
132 1.24 pooka char **modarray = NULL;
133 1.24 pooka unsigned nmods = 0, curmod = 0;
134 1.24 pooka
135 1.24 pooka #ifdef PLATFORM_HAS_SETGETPROGNAME
136 1.1 pooka setprogname(argv[0]);
137 1.24 pooka #endif
138 1.1 pooka
139 1.2 pooka sflag = 0;
140 1.20 pooka while ((ch = getopt(argc, argv, "c:d:l:m:r:sv")) != -1) {
141 1.2 pooka switch (ch) {
142 1.12 pooka case 'c':
143 1.12 pooka setenv("RUMP_NCPU", optarg, 1);
144 1.12 pooka break;
145 1.9 pooka case 'd': {
146 1.9 pooka char *options, *value;
147 1.9 pooka char *key, *hostpath;
148 1.14 pooka long long flen, foffset;
149 1.16 pooka char partition;
150 1.17 pooka int ftype;
151 1.9 pooka
152 1.14 pooka flen = foffset = 0;
153 1.16 pooka partition = 0;
154 1.9 pooka key = hostpath = NULL;
155 1.17 pooka ftype = -1;
156 1.9 pooka options = optarg;
157 1.9 pooka while (*options) {
158 1.9 pooka switch (getsubopt(&options,
159 1.9 pooka __UNCONST(disktokens), &value)) {
160 1.9 pooka case DKEY:
161 1.11 pooka if (key != NULL) {
162 1.11 pooka fprintf(stderr,
163 1.11 pooka "key already given\n");
164 1.11 pooka usage();
165 1.11 pooka }
166 1.9 pooka key = value;
167 1.9 pooka break;
168 1.16 pooka
169 1.9 pooka case DFILE:
170 1.11 pooka if (hostpath != NULL) {
171 1.11 pooka fprintf(stderr,
172 1.11 pooka "hostpath already given\n");
173 1.11 pooka usage();
174 1.11 pooka }
175 1.9 pooka hostpath = value;
176 1.9 pooka break;
177 1.16 pooka
178 1.9 pooka case DSIZE:
179 1.11 pooka if (flen != 0) {
180 1.11 pooka fprintf(stderr,
181 1.11 pooka "size already given\n");
182 1.11 pooka usage();
183 1.11 pooka }
184 1.19 pooka if (strcmp(value, "host") == 0) {
185 1.18 pooka if (foffset != 0) {
186 1.18 pooka fprintf(stderr,
187 1.18 pooka "cannot specify "
188 1.18 pooka "offset with "
189 1.21 pooka "size=host\n");
190 1.18 pooka usage();
191 1.18 pooka }
192 1.18 pooka flen = DSIZE_E;
193 1.18 pooka } else {
194 1.24 pooka #ifdef PLATFORM_HAS_STRSUFTOLL
195 1.18 pooka /* XXX: off_t max? */
196 1.18 pooka flen = strsuftoll("-d size",
197 1.18 pooka value, 0, LLONG_MAX);
198 1.24 pooka #else
199 1.24 pooka flen = strtoull(value,
200 1.24 pooka NULL, 10);
201 1.24 pooka #endif
202 1.18 pooka }
203 1.9 pooka break;
204 1.14 pooka case DOFFSET:
205 1.14 pooka if (foffset != 0) {
206 1.14 pooka fprintf(stderr,
207 1.14 pooka "offset already given\n");
208 1.14 pooka usage();
209 1.14 pooka }
210 1.18 pooka if (flen == DSIZE_E) {
211 1.18 pooka fprintf(stderr, "cannot "
212 1.18 pooka "specify offset with "
213 1.21 pooka "size=host\n");
214 1.18 pooka usage();
215 1.18 pooka }
216 1.24 pooka #ifdef PLATFORM_HAS_STRSUFTOLL
217 1.14 pooka /* XXX: off_t max? */
218 1.14 pooka foffset = strsuftoll("-d offset", value,
219 1.14 pooka 0, LLONG_MAX);
220 1.24 pooka #else
221 1.24 pooka foffset = strtoull(value, NULL, 10);
222 1.24 pooka #endif
223 1.14 pooka break;
224 1.16 pooka
225 1.16 pooka case DLABEL:
226 1.16 pooka if (foffset != 0 || flen != 0) {
227 1.16 pooka fprintf(stderr,
228 1.16 pooka "disklabel needs to be "
229 1.16 pooka "used alone\n");
230 1.16 pooka usage();
231 1.16 pooka }
232 1.16 pooka if (strlen(value) != 1 ||
233 1.16 pooka *value < 'a' || *value > 'z') {
234 1.16 pooka fprintf(stderr,
235 1.16 pooka "invalid label part\n");
236 1.16 pooka usage();
237 1.16 pooka }
238 1.16 pooka partition = *value;
239 1.16 pooka break;
240 1.16 pooka
241 1.17 pooka case DTYPE:
242 1.17 pooka if (ftype != -1) {
243 1.17 pooka fprintf(stderr,
244 1.17 pooka "type already specified\n");
245 1.17 pooka usage();
246 1.17 pooka }
247 1.17 pooka
248 1.17 pooka for (i = 0;
249 1.17 pooka i < __arraycount(etfstypes);
250 1.17 pooka i++) {
251 1.17 pooka if (strcmp(etfstypes[i].name,
252 1.17 pooka value) == 0)
253 1.17 pooka break;
254 1.17 pooka }
255 1.17 pooka if (i == __arraycount(etfstypes)) {
256 1.17 pooka fprintf(stderr,
257 1.17 pooka "invalid type %s\n", value);
258 1.17 pooka usage();
259 1.17 pooka }
260 1.17 pooka ftype = etfstypes[i].type;
261 1.17 pooka break;
262 1.17 pooka
263 1.9 pooka default:
264 1.9 pooka fprintf(stderr, "invalid dtoken\n");
265 1.9 pooka usage();
266 1.9 pooka break;
267 1.9 pooka }
268 1.9 pooka }
269 1.9 pooka
270 1.16 pooka if (key == NULL || hostpath == NULL ||
271 1.16 pooka (flen == 0 && partition == 0)) {
272 1.9 pooka fprintf(stderr, "incomplete drivespec\n");
273 1.9 pooka usage();
274 1.9 pooka }
275 1.17 pooka if (ftype == -1)
276 1.17 pooka ftype = RUMP_ETFS_BLK;
277 1.9 pooka
278 1.9 pooka if (netfs - curetfs == 0) {
279 1.9 pooka etfs = realloc(etfs, (netfs+16)*sizeof(*etfs));
280 1.9 pooka if (etfs == NULL)
281 1.26 pooka die(1, errno, "realloc etfs");
282 1.9 pooka netfs += 16;
283 1.9 pooka }
284 1.9 pooka
285 1.9 pooka etfs[curetfs].key = key;
286 1.9 pooka etfs[curetfs].hostpath = hostpath;
287 1.9 pooka etfs[curetfs].flen = flen;
288 1.14 pooka etfs[curetfs].foffset = foffset;
289 1.16 pooka etfs[curetfs].partition = partition;
290 1.17 pooka etfs[curetfs].type = ftype;
291 1.9 pooka curetfs++;
292 1.9 pooka
293 1.9 pooka break;
294 1.9 pooka }
295 1.6 pooka case 'l':
296 1.8 pooka if (dlopen(optarg, RTLD_LAZY|RTLD_GLOBAL) == NULL) {
297 1.8 pooka char pb[MAXPATHLEN];
298 1.8 pooka /* try to mimic linker -l syntax */
299 1.8 pooka
300 1.8 pooka snprintf(pb, sizeof(pb), "lib%s.so", optarg);
301 1.8 pooka if (dlopen(pb, RTLD_LAZY|RTLD_GLOBAL) == NULL) {
302 1.29 bad fprintf(stderr, "dlopen: %s", dlerror());
303 1.29 bad die(1, 0, NULL);
304 1.8 pooka }
305 1.8 pooka }
306 1.6 pooka break;
307 1.24 pooka case 'm': {
308 1.24 pooka
309 1.7 pooka if (nmods - curmod == 0) {
310 1.7 pooka modarray = realloc(modarray,
311 1.7 pooka (nmods+16) * sizeof(char *));
312 1.7 pooka if (modarray == NULL)
313 1.26 pooka die(1, errno, "realloc");
314 1.7 pooka nmods += 16;
315 1.7 pooka }
316 1.7 pooka modarray[curmod++] = optarg;
317 1.24 pooka break; }
318 1.20 pooka case 'r':
319 1.20 pooka setenv("RUMP_MEMLIMIT", optarg, 1);
320 1.20 pooka break;
321 1.2 pooka case 's':
322 1.2 pooka sflag = 1;
323 1.2 pooka break;
324 1.20 pooka case 'v':
325 1.20 pooka setenv("RUMP_VERBOSE", "1", 1);
326 1.20 pooka break;
327 1.2 pooka default:
328 1.2 pooka usage();
329 1.2 pooka /*NOTREACHED*/
330 1.2 pooka }
331 1.2 pooka }
332 1.2 pooka
333 1.2 pooka argc -= optind;
334 1.2 pooka argv += optind;
335 1.2 pooka
336 1.2 pooka if (argc != 1)
337 1.1 pooka usage();
338 1.1 pooka
339 1.2 pooka serverurl = argv[0];
340 1.2 pooka
341 1.2 pooka if (!sflag) {
342 1.2 pooka error = rump_daemonize_begin();
343 1.2 pooka if (error)
344 1.26 pooka die(1, error, "rump daemonize");
345 1.2 pooka }
346 1.2 pooka
347 1.1 pooka error = rump_init();
348 1.1 pooka if (error)
349 1.2 pooka die(sflag, error, "rump init failed");
350 1.7 pooka
351 1.9 pooka /* load modules */
352 1.7 pooka for (i = 0; i < curmod; i++) {
353 1.27 pooka struct rump_modctl_load ml;
354 1.7 pooka
355 1.7 pooka #define ETFSKEY "/module.mod"
356 1.7 pooka if ((error = rump_pub_etfs_register(ETFSKEY,
357 1.7 pooka modarray[0], RUMP_ETFS_REG)) != 0)
358 1.7 pooka die(sflag, error, "module etfs register failed");
359 1.7 pooka memset(&ml, 0, sizeof(ml));
360 1.7 pooka ml.ml_filename = ETFSKEY;
361 1.27 pooka if (rump_sys_modctl(RUMP_MODCTL_LOAD, &ml) == -1)
362 1.7 pooka die(sflag, errno, "module load failed");
363 1.7 pooka rump_pub_etfs_remove(ETFSKEY);
364 1.9 pooka #undef ETFSKEY
365 1.9 pooka }
366 1.9 pooka
367 1.9 pooka /* register host drives */
368 1.9 pooka for (i = 0; i < curetfs; i++) {
369 1.14 pooka struct stat sb;
370 1.16 pooka off_t foffset, flen, fendoff;
371 1.18 pooka int fd, oflags;
372 1.9 pooka
373 1.18 pooka oflags = etfs[i].flen == DSIZE_E ? 0 : O_CREAT;
374 1.18 pooka fd = open(etfs[i].hostpath, O_RDWR | oflags, 0644);
375 1.9 pooka if (fd == -1)
376 1.18 pooka die(sflag, errno, "etfs hostpath open");
377 1.16 pooka
378 1.16 pooka if (etfs[i].partition) {
379 1.28 pooka processlabel(sflag, fd, etfs[i].partition - 'a',
380 1.28 pooka &foffset, &flen);
381 1.16 pooka } else {
382 1.16 pooka foffset = etfs[i].foffset;
383 1.16 pooka flen = etfs[i].flen;
384 1.16 pooka }
385 1.16 pooka
386 1.14 pooka if (fstat(fd, &sb) == -1)
387 1.14 pooka die(sflag, errno, "fstat etfs hostpath");
388 1.18 pooka if (flen == DSIZE_E) {
389 1.21 pooka if (sb.st_size == 0)
390 1.21 pooka die(sflag, EINVAL, "size=host, but cannot "
391 1.21 pooka "query non-zero size");
392 1.18 pooka flen = sb.st_size;
393 1.18 pooka }
394 1.18 pooka fendoff = foffset + flen;
395 1.16 pooka if (S_ISREG(sb.st_mode) && sb.st_size < fendoff) {
396 1.16 pooka if (ftruncate(fd, fendoff) == -1)
397 1.14 pooka die(sflag, errno, "truncate");
398 1.14 pooka }
399 1.9 pooka close(fd);
400 1.9 pooka
401 1.14 pooka if ((error = rump_pub_etfs_register_withsize(etfs[i].key,
402 1.16 pooka etfs[i].hostpath, etfs[i].type, foffset, flen)) != 0)
403 1.9 pooka die(sflag, error, "etfs register");
404 1.7 pooka }
405 1.7 pooka
406 1.2 pooka error = rump_init_server(serverurl);
407 1.1 pooka if (error)
408 1.2 pooka die(sflag, error, "rump server init failed");
409 1.2 pooka
410 1.2 pooka if (!sflag)
411 1.3 pooka rump_daemonize_done(RUMP_DAEMONIZE_SUCCESS);
412 1.2 pooka
413 1.4 pooka sem_init(&sigsem, 0, 0);
414 1.4 pooka signal(SIGTERM, sigreboot);
415 1.4 pooka signal(SIGINT, sigreboot);
416 1.4 pooka sem_wait(&sigsem);
417 1.4 pooka
418 1.4 pooka rump_sys_reboot(0, NULL);
419 1.5 pooka /*NOTREACHED*/
420 1.5 pooka
421 1.4 pooka return 0;
422 1.1 pooka }
423 1.28 pooka
424 1.28 pooka /*
425 1.28 pooka * Copyright (c) 1987, 1988, 1993
426 1.28 pooka * The Regents of the University of California. All rights reserved.
427 1.28 pooka *
428 1.28 pooka * Redistribution and use in source and binary forms, with or without
429 1.28 pooka * modification, are permitted provided that the following conditions
430 1.28 pooka * are met:
431 1.28 pooka * 1. Redistributions of source code must retain the above copyright
432 1.28 pooka * notice, this list of conditions and the following disclaimer.
433 1.28 pooka * 2. Redistributions in binary form must reproduce the above copyright
434 1.28 pooka * notice, this list of conditions and the following disclaimer in the
435 1.28 pooka * documentation and/or other materials provided with the distribution.
436 1.28 pooka * 3. Neither the name of the University nor the names of its contributors
437 1.28 pooka * may be used to endorse or promote products derived from this software
438 1.28 pooka * without specific prior written permission.
439 1.28 pooka *
440 1.28 pooka * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
441 1.28 pooka * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
442 1.28 pooka * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
443 1.28 pooka * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
444 1.28 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
445 1.28 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
446 1.28 pooka * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
447 1.28 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
448 1.28 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
449 1.28 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
450 1.28 pooka * SUCH DAMAGE.
451 1.28 pooka *
452 1.28 pooka * @(#)disklabel.h 8.2 (Berkeley) 7/10/94
453 1.28 pooka */
454 1.28 pooka
455 1.28 pooka #define RUMPSERVER_MAXPARTITIONS 22
456 1.28 pooka #define RUMPSERVER_DISKMAGIC ((uint32_t)0x82564557) /* magic */
457 1.28 pooka #define RUMPSERVER_DEVSHIFT 9
458 1.28 pooka
459 1.28 pooka struct rumpserver_disklabel {
460 1.28 pooka uint32_t d_magic; /* the magic number */
461 1.28 pooka uint16_t d_type; /* drive type */
462 1.28 pooka uint16_t d_subtype; /* controller/d_type specific */
463 1.28 pooka char d_typename[16]; /* type name, e.g. "eagle" */
464 1.28 pooka
465 1.28 pooka /*
466 1.28 pooka * d_packname contains the pack identifier and is returned when
467 1.28 pooka * the disklabel is read off the disk or in-core copy.
468 1.28 pooka * d_boot0 and d_boot1 are the (optional) names of the
469 1.28 pooka * primary (block 0) and secondary (block 1-15) bootstraps
470 1.28 pooka * as found in /usr/mdec. These are returned when using
471 1.28 pooka * getdiskbyname(3) to retrieve the values from /etc/disktab.
472 1.28 pooka */
473 1.28 pooka union {
474 1.28 pooka char un_d_packname[16]; /* pack identifier */
475 1.28 pooka struct {
476 1.28 pooka char *un_d_boot0; /* primary bootstrap name */
477 1.28 pooka char *un_d_boot1; /* secondary bootstrap name */
478 1.28 pooka } un_b;
479 1.28 pooka } d_un;
480 1.28 pooka #define d_packname d_un.un_d_packname
481 1.28 pooka #define d_boot0 d_un.un_b.un_d_boot0
482 1.28 pooka #define d_boot1 d_un.un_b.un_d_boot1
483 1.28 pooka
484 1.28 pooka /* disk geometry: */
485 1.28 pooka uint32_t d_secsize; /* # of bytes per sector */
486 1.28 pooka uint32_t d_nsectors; /* # of data sectors per track */
487 1.28 pooka uint32_t d_ntracks; /* # of tracks per cylinder */
488 1.28 pooka uint32_t d_ncylinders; /* # of data cylinders per unit */
489 1.28 pooka uint32_t d_secpercyl; /* # of data sectors per cylinder */
490 1.28 pooka uint32_t d_secperunit; /* # of data sectors per unit */
491 1.28 pooka
492 1.28 pooka /*
493 1.28 pooka * Spares (bad sector replacements) below are not counted in
494 1.28 pooka * d_nsectors or d_secpercyl. Spare sectors are assumed to
495 1.28 pooka * be physical sectors which occupy space at the end of each
496 1.28 pooka * track and/or cylinder.
497 1.28 pooka */
498 1.28 pooka uint16_t d_sparespertrack; /* # of spare sectors per track */
499 1.28 pooka uint16_t d_sparespercyl; /* # of spare sectors per cylinder */
500 1.28 pooka /*
501 1.28 pooka * Alternative cylinders include maintenance, replacement,
502 1.28 pooka * configuration description areas, etc.
503 1.28 pooka */
504 1.28 pooka uint32_t d_acylinders; /* # of alt. cylinders per unit */
505 1.28 pooka
506 1.28 pooka /* hardware characteristics: */
507 1.28 pooka /*
508 1.28 pooka * d_interleave, d_trackskew and d_cylskew describe perturbations
509 1.28 pooka * in the media format used to compensate for a slow controller.
510 1.28 pooka * Interleave is physical sector interleave, set up by the
511 1.28 pooka * formatter or controller when formatting. When interleaving is
512 1.28 pooka * in use, logically adjacent sectors are not physically
513 1.28 pooka * contiguous, but instead are separated by some number of
514 1.28 pooka * sectors. It is specified as the ratio of physical sectors
515 1.28 pooka * traversed per logical sector. Thus an interleave of 1:1
516 1.28 pooka * implies contiguous layout, while 2:1 implies that logical
517 1.28 pooka * sector 0 is separated by one sector from logical sector 1.
518 1.28 pooka * d_trackskew is the offset of sector 0 on track N relative to
519 1.28 pooka * sector 0 on track N-1 on the same cylinder. Finally, d_cylskew
520 1.28 pooka * is the offset of sector 0 on cylinder N relative to sector 0
521 1.28 pooka * on cylinder N-1.
522 1.28 pooka */
523 1.28 pooka uint16_t d_rpm; /* rotational speed */
524 1.28 pooka uint16_t d_interleave; /* hardware sector interleave */
525 1.28 pooka uint16_t d_trackskew; /* sector 0 skew, per track */
526 1.28 pooka uint16_t d_cylskew; /* sector 0 skew, per cylinder */
527 1.28 pooka uint32_t d_headswitch; /* head switch time, usec */
528 1.28 pooka uint32_t d_trkseek; /* track-to-track seek, usec */
529 1.28 pooka uint32_t d_flags; /* generic flags */
530 1.28 pooka #define NDDATA 5
531 1.28 pooka uint32_t d_drivedata[NDDATA]; /* drive-type specific information */
532 1.28 pooka #define NSPARE 5
533 1.28 pooka uint32_t d_spare[NSPARE]; /* reserved for future use */
534 1.28 pooka uint32_t d_magic2; /* the magic number (again) */
535 1.28 pooka uint16_t d_checksum; /* xor of data incl. partitions */
536 1.28 pooka
537 1.28 pooka /* filesystem and partition information: */
538 1.28 pooka uint16_t d_npartitions; /* number of partitions in following */
539 1.28 pooka uint32_t d_bbsize; /* size of boot area at sn0, bytes */
540 1.28 pooka uint32_t d_sbsize; /* max size of fs superblock, bytes */
541 1.28 pooka struct rumpserver_partition { /* the partition table */
542 1.28 pooka uint32_t p_size; /* number of sectors in partition */
543 1.28 pooka uint32_t p_offset; /* starting sector */
544 1.28 pooka union {
545 1.28 pooka uint32_t fsize; /* FFS, ADOS:
546 1.28 pooka filesystem basic fragment size */
547 1.28 pooka uint32_t cdsession; /* ISO9660: session offset */
548 1.28 pooka } __partition_u2;
549 1.28 pooka #define p_fsize __partition_u2.fsize
550 1.28 pooka #define p_cdsession __partition_u2.cdsession
551 1.28 pooka uint8_t p_fstype; /* filesystem type, see below */
552 1.28 pooka uint8_t p_frag; /* filesystem fragments per block */
553 1.28 pooka union {
554 1.28 pooka uint16_t cpg; /* UFS: FS cylinders per group */
555 1.28 pooka uint16_t sgs; /* LFS: FS segment shift */
556 1.28 pooka } __partition_u1;
557 1.28 pooka #define p_cpg __partition_u1.cpg
558 1.28 pooka #define p_sgs __partition_u1.sgs
559 1.28 pooka } d_partitions[RUMPSERVER_MAXPARTITIONS]; /* actually may be more */
560 1.28 pooka };
561 1.28 pooka
562 1.28 pooka
563 1.28 pooka /* for swapping disklabel, so don't care about perf, just portability */
564 1.28 pooka #define bs32(x) \
565 1.28 pooka ((((x) & 0xff000000) >> 24)| \
566 1.28 pooka (((x) & 0x00ff0000) >> 8) | \
567 1.28 pooka (((x) & 0x0000ff00) << 8) | \
568 1.28 pooka (((x) & 0x000000ff) << 24))
569 1.28 pooka #define bs16(x) ((((x) & 0xff00) >> 8) | (((x) & 0x00ff) << 8))
570 1.28 pooka
571 1.28 pooka /*
572 1.28 pooka * From:
573 1.28 pooka * $NetBSD: disklabel_dkcksum.c,v 1.4 2005/05/15 21:01:34 thorpej Exp
574 1.28 pooka */
575 1.28 pooka
576 1.28 pooka /*-
577 1.28 pooka * Copyright (c) 1991, 1993
578 1.28 pooka * The Regents of the University of California. All rights reserved.
579 1.28 pooka *
580 1.28 pooka * Redistribution and use in source and binary forms, with or without
581 1.28 pooka * modification, are permitted provided that the following conditions
582 1.28 pooka * are met:
583 1.28 pooka * 1. Redistributions of source code must retain the above copyright
584 1.28 pooka * notice, this list of conditions and the following disclaimer.
585 1.28 pooka * 2. Redistributions in binary form must reproduce the above copyright
586 1.28 pooka * notice, this list of conditions and the following disclaimer in the
587 1.28 pooka * documentation and/or other materials provided with the distribution.
588 1.28 pooka * 3. Neither the name of the University nor the names of its contributors
589 1.28 pooka * may be used to endorse or promote products derived from this software
590 1.28 pooka * without specific prior written permission.
591 1.28 pooka *
592 1.28 pooka * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
593 1.28 pooka * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
594 1.28 pooka * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
595 1.28 pooka * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
596 1.28 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
597 1.28 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
598 1.28 pooka * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
599 1.28 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
600 1.28 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
601 1.28 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
602 1.28 pooka * SUCH DAMAGE.
603 1.28 pooka */
604 1.28 pooka
605 1.28 pooka static uint16_t
606 1.28 pooka rs_dl_dkcksum(struct rumpserver_disklabel *lp, int imswapped)
607 1.28 pooka {
608 1.28 pooka uint16_t *start, *end;
609 1.28 pooka uint16_t sum;
610 1.28 pooka uint16_t npart;
611 1.28 pooka
612 1.28 pooka if (imswapped)
613 1.28 pooka npart = bs16(lp->d_npartitions);
614 1.28 pooka else
615 1.28 pooka npart = lp->d_npartitions;
616 1.28 pooka
617 1.28 pooka sum = 0;
618 1.28 pooka start = (uint16_t *)(void *)lp;
619 1.28 pooka end = (uint16_t *)(void *)&lp->d_partitions[npart];
620 1.28 pooka while (start < end) {
621 1.28 pooka if (imswapped)
622 1.28 pooka sum ^= bs16(*start);
623 1.28 pooka else
624 1.28 pooka sum ^= *start;
625 1.28 pooka start++;
626 1.28 pooka }
627 1.28 pooka return (sum);
628 1.28 pooka }
629 1.28 pooka
630 1.28 pooka /*
631 1.28 pooka * From:
632 1.28 pooka * NetBSD: disklabel_scan.c,v 1.3 2009/01/18 12:13:03 lukem Exp
633 1.28 pooka */
634 1.28 pooka
635 1.28 pooka /*-
636 1.28 pooka * Copyright (c) 2002 The NetBSD Foundation, Inc.
637 1.28 pooka * All rights reserved.
638 1.28 pooka *
639 1.28 pooka * This code is derived from software contributed to The NetBSD Foundation
640 1.28 pooka * by Roland C. Dowdeswell.
641 1.28 pooka *
642 1.28 pooka * Redistribution and use in source and binary forms, with or without
643 1.28 pooka * modification, are permitted provided that the following conditions
644 1.28 pooka * are met:
645 1.28 pooka * 1. Redistributions of source code must retain the above copyright
646 1.28 pooka * notice, this list of conditions and the following disclaimer.
647 1.28 pooka * 2. Redistributions in binary form must reproduce the above copyright
648 1.28 pooka * notice, this list of conditions and the following disclaimer in the
649 1.28 pooka * documentation and/or other materials provided with the distribution.
650 1.28 pooka *
651 1.28 pooka * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
652 1.28 pooka * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
653 1.28 pooka * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
654 1.28 pooka * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
655 1.28 pooka * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
656 1.28 pooka * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
657 1.28 pooka * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
658 1.28 pooka * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
659 1.28 pooka * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
660 1.28 pooka * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
661 1.28 pooka * POSSIBILITY OF SUCH DAMAGE.
662 1.28 pooka */
663 1.28 pooka
664 1.28 pooka static int
665 1.28 pooka rs_dl_scan(struct rumpserver_disklabel *lp, int *isswapped,
666 1.28 pooka char *buf, size_t buflen)
667 1.28 pooka {
668 1.28 pooka size_t i;
669 1.28 pooka int imswapped;
670 1.28 pooka uint16_t npart;
671 1.28 pooka
672 1.28 pooka /* scan for the correct magic numbers. */
673 1.28 pooka
674 1.28 pooka for (i=0; i <= buflen - sizeof(*lp); i += 4) {
675 1.28 pooka memcpy(lp, buf + i, sizeof(*lp));
676 1.28 pooka if (lp->d_magic == RUMPSERVER_DISKMAGIC &&
677 1.28 pooka lp->d_magic2 == RUMPSERVER_DISKMAGIC) {
678 1.28 pooka imswapped = 0;
679 1.28 pooka goto sanity;
680 1.28 pooka }
681 1.28 pooka if (lp->d_magic == bs32(RUMPSERVER_DISKMAGIC) &&
682 1.28 pooka lp->d_magic2 == bs32(RUMPSERVER_DISKMAGIC)) {
683 1.28 pooka imswapped = 1;
684 1.28 pooka goto sanity;
685 1.28 pooka }
686 1.28 pooka }
687 1.28 pooka
688 1.28 pooka return 1;
689 1.28 pooka
690 1.28 pooka sanity:
691 1.28 pooka if (imswapped)
692 1.28 pooka npart = bs16(lp->d_npartitions);
693 1.28 pooka else
694 1.28 pooka npart = lp->d_npartitions;
695 1.28 pooka /* we've found something, let's sanity check it */
696 1.28 pooka if (npart > RUMPSERVER_MAXPARTITIONS
697 1.28 pooka || rs_dl_dkcksum(lp, imswapped))
698 1.28 pooka return 1;
699 1.28 pooka
700 1.28 pooka *isswapped = imswapped;
701 1.28 pooka return 0;
702 1.28 pooka }
703 1.28 pooka
704 1.28 pooka static void
705 1.28 pooka processlabel(int sflag, int fd, int partition, off_t *foffp, off_t *flenp)
706 1.28 pooka {
707 1.28 pooka struct rumpserver_disklabel dl;
708 1.28 pooka char buf[1<<16];
709 1.28 pooka uint32_t foffset, flen;
710 1.28 pooka int imswapped;
711 1.28 pooka
712 1.28 pooka if (pread(fd, buf, sizeof(buf), 0) == -1)
713 1.28 pooka die(sflag, errno, "could not read disk device");
714 1.28 pooka if (rs_dl_scan(&dl, &imswapped, buf, sizeof(buf)))
715 1.28 pooka die(sflag, ENOENT, "disklabel not found");
716 1.28 pooka
717 1.28 pooka if (partition >= dl.d_npartitions)
718 1.28 pooka die(sflag, ENOENT, "partition not available");
719 1.28 pooka
720 1.28 pooka foffset = dl.d_partitions[partition].p_offset << RUMPSERVER_DEVSHIFT;
721 1.28 pooka flen = dl.d_partitions[partition].p_size << RUMPSERVER_DEVSHIFT;
722 1.28 pooka if (imswapped) {
723 1.28 pooka foffset = bs32(foffset);
724 1.28 pooka flen = bs32(flen);
725 1.28 pooka }
726 1.28 pooka
727 1.28 pooka *foffp = (off_t)foffset;
728 1.28 pooka *flenp = (off_t)flen;
729 1.28 pooka }
730