sdread.c revision 1.3
11.3Spooka/*	$NetBSD: sdread.c,v 1.3 2010/02/17 20:43:35 pooka Exp $	*/
21.1Spooka
31.1Spooka/*
41.1Spooka * Copyright (c) 2009 Antti Kantee.  All Rights Reserved.
51.1Spooka *
61.1Spooka * Redistribution and use in source and binary forms, with or without
71.1Spooka * modification, are permitted provided that the following conditions
81.1Spooka * are met:
91.1Spooka * 1. Redistributions of source code must retain the above copyright
101.1Spooka *    notice, this list of conditions and the following disclaimer.
111.1Spooka * 2. Redistributions in binary form must reproduce the above copyright
121.1Spooka *    notice, this list of conditions and the following disclaimer in the
131.1Spooka *    documentation and/or other materials provided with the distribution.
141.1Spooka *
151.1Spooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
161.1Spooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
171.1Spooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
181.1Spooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
191.1Spooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201.1Spooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
211.1Spooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
221.1Spooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
231.1Spooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241.1Spooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251.1Spooka * SUCH DAMAGE.
261.1Spooka */
271.1Spooka
281.1Spooka#include <sys/types.h>
291.1Spooka#include <sys/dirent.h>
301.1Spooka#include <sys/mount.h>
311.1Spooka
321.2Spooka#include <ufs/ufs/ufsmount.h>
331.1Spooka#include <msdosfs/msdosfsmount.h>
341.3Spooka#include <isofs/cd9660/cd9660_mount.h>
351.1Spooka
361.1Spooka#include <rump/rump.h>
371.1Spooka#include <rump/rump_syscalls.h>
381.1Spooka
391.1Spooka#include <err.h>
401.1Spooka#include <errno.h>
411.1Spooka#include <fcntl.h>
421.1Spooka#include <stdio.h>
431.1Spooka#include <stdlib.h>
441.1Spooka#include <string.h>
451.1Spooka#include <unistd.h>
461.1Spooka
471.1Spooka/*
481.1Spooka * Proof-of-concept program:
491.1Spooka *
501.1Spooka * Mount rump file system from device driver stack included in the
511.1Spooka * rump kernel.  Optionally copy a file out of the mounted file system.
521.1Spooka */
531.1Spooka
541.1Spookaint
551.1Spookamain(int argc, char *argv[])
561.1Spooka{
571.1Spooka	char buf[2048];
581.1Spooka	struct msdosfs_args args;
591.2Spooka	struct ufs_args uargs;
601.3Spooka	struct iso_args iargs;
611.1Spooka	struct dirent *dp;
621.1Spooka	const char *msg = NULL;
631.1Spooka	int fd, n, fd_h, sverrno;
641.3Spooka	int probeonly = 0;
651.1Spooka
661.3Spooka	if (argc > 1) {
671.3Spooka		if (argc == 2 && strcmp(argv[1], "probe") == 0) {
681.3Spooka			probeonly = 1;
691.3Spooka		} else if (argc != 3) {
701.3Spooka			fprintf(stderr, "usage: a.out [src hostdest]\n");
711.3Spooka			exit(1);
721.3Spooka		}
731.1Spooka	}
741.1Spooka
751.1Spooka	memset(&args, 0, sizeof(args));
761.1Spooka	args.fspec = strdup("/dev/sd0e");
771.1Spooka	args.version = MSDOSFSMNT_VERSION;
781.1Spooka
791.2Spooka	memset(&uargs, 0, sizeof(uargs));
801.2Spooka	uargs.fspec = strdup("/dev/sd0e");
811.2Spooka
821.3Spooka	memset(&iargs, 0, sizeof(iargs));
831.3Spooka	iargs.fspec = strdup("/dev/cd0a");
841.3Spooka
851.3Spooka	if (probeonly)
861.3Spooka		rump_boot_sethowto(RUMP_AB_VERBOSE);
871.1Spooka	rump_init();
881.3Spooka	if (probeonly) {
891.3Spooka		exit(0);
901.3Spooka	}
911.1Spooka
921.1Spooka	if (rump_sys_mkdir("/mp", 0777) == -1)
931.1Spooka		err(1, "mkdir");
941.1Spooka	if (rump_sys_mount(MOUNT_MSDOS, "/mp", MNT_RDONLY,
951.2Spooka	    &args, sizeof(args)) == -1) {
961.2Spooka		if (rump_sys_mount(MOUNT_FFS, "/mp", MNT_RDONLY,
971.2Spooka		    &uargs, sizeof(uargs)) == -1) {
981.3Spooka			printf("Trying CD.  This might fail with "
991.3Spooka			    "\"media not present\".\n");
1001.3Spooka			printf("If that happens, wait a while without "
1011.3Spooka			    "unplugging the device and re-run.\n");
1021.3Spooka			printf("Some devices apparently need a long time "
1031.3Spooka			    "to settle after they are initialized.\n\n");
1041.3Spooka
1051.3Spooka			if (rump_sys_mount(MOUNT_CD9660, "/mp", MNT_RDONLY,
1061.3Spooka			    &iargs, sizeof(iargs)) == -1) {
1071.3Spooka				err(1, "mount");
1081.3Spooka			}
1091.2Spooka		}
1101.2Spooka	}
1111.1Spooka
1121.1Spooka	fd = rump_sys_open("/mp", O_RDONLY, 0);
1131.1Spooka	if (fd == -1) {
1141.1Spooka		msg = "open dir";
1151.1Spooka		goto out;
1161.1Spooka	}
1171.1Spooka
1181.1Spooka	while ((n = rump_sys_getdents(fd, buf, sizeof(buf))) > 0) {
1191.1Spooka		for (dp = (struct dirent *)buf;
1201.1Spooka		    (char *)dp - buf < n;
1211.1Spooka		    dp = _DIRENT_NEXT(dp)) {
1221.1Spooka			printf("%" PRIu64 ": %s\n", dp->d_fileno, dp->d_name);
1231.1Spooka		}
1241.1Spooka	}
1251.1Spooka	rump_sys_close(fd);
1261.1Spooka	if (argc == 1)
1271.1Spooka		goto out;
1281.1Spooka
1291.1Spooka	rump_sys_chdir("/mp");
1301.1Spooka	fd = rump_sys_open(argv[1], O_RDONLY, 0);
1311.1Spooka	if (fd == -1) {
1321.1Spooka		msg = "open fs file";
1331.1Spooka		goto out;
1341.1Spooka	}
1351.1Spooka
1361.1Spooka	fd_h = open(argv[2], O_RDWR | O_CREAT, 0777);
1371.1Spooka	if (fd_h == -1) {
1381.1Spooka		msg = "open host file";
1391.1Spooka		goto out;
1401.1Spooka	}
1411.1Spooka
1421.1Spooka	while ((n = rump_sys_read(fd, buf, sizeof(buf))) == sizeof(buf)) {
1431.1Spooka		if (write(fd_h, buf, sizeof(buf)) != sizeof(buf)) {
1441.1Spooka			msg = "write host file";
1451.1Spooka			goto out;
1461.1Spooka		}
1471.1Spooka	}
1481.1Spooka	if (n == -1) {
1491.1Spooka		msg = "read fs file";
1501.1Spooka		goto out;
1511.1Spooka	}
1521.1Spooka
1531.1Spooka	if (n > 0) {
1541.1Spooka		if (write(fd_h, buf, n) == -1)
1551.1Spooka			msg = "write tail";
1561.1Spooka	}
1571.1Spooka
1581.1Spooka out:
1591.1Spooka	sverrno = errno;
1601.1Spooka	rump_sys_chdir("/");
1611.1Spooka	rump_sys_close(fd);
1621.1Spooka	close(fd_h);
1631.1Spooka	if (rump_sys_unmount("/mp", 0) == -1)
1641.1Spooka		err(1, "unmount");
1651.1Spooka
1661.1Spooka	if (msg) {
1671.1Spooka		errno = sverrno;
1681.1Spooka		err(1, "%s", msg);
1691.1Spooka	}
1701.1Spooka
1711.1Spooka	return 0;
1721.1Spooka}
173