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