199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2011 Google, Inc.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * $FreeBSD$
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/types.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/disk.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/ioctl.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/stat.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <dirent.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <dlfcn.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <err.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <errno.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <fcntl.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <getopt.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <inttypes.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <limits.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stdio.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stdlib.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <string.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <termios.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <unistd.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <boot/userboot/userboot.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomechar *host_base = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct termios term, oldterm;
199767f8919635c4928607450d9e0abb932109ceToomas Soomechar *image;
199767f8919635c4928607450d9e0abb932109ceToomas Soomesize_t image_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint disk_fd = -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeuint64_t regs[16];
199767f8919635c4928607450d9e0abb932109ceToomas Soomeuint64_t pc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid test_exit(void *arg, int v);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Console i/o
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_putc(void *arg, int ch)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char c = ch;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome write(1, &c, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_getc(void *arg)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (read(0, &c, 1) == 1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_poll(void *arg)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int n;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (ioctl(0, FIONREAD, &n) >= 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (n > 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Host filesystem i/o
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct test_file {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int tf_isdir;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t tf_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct stat tf_stat;
199767f8919635c4928607450d9e0abb932109ceToomas Soome union {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int fd;
199767f8919635c4928607450d9e0abb932109ceToomas Soome DIR *dir;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } tf_u;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_open(void *arg, const char *filename, void **h_return)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct stat st;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct test_file *tf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char path[PATH_MAX];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!host_base)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOENT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome strlcpy(path, host_base, PATH_MAX);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (path[strlen(path) - 1] == '/')
199767f8919635c4928607450d9e0abb932109ceToomas Soome path[strlen(path) - 1] = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome strlcat(path, filename, PATH_MAX);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tf = malloc(sizeof(struct test_file));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (stat(path, &tf->tf_stat) < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(tf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (errno);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome tf->tf_size = st.st_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (S_ISDIR(tf->tf_stat.st_mode)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome tf->tf_isdir = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome tf->tf_u.dir = opendir(path);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!tf->tf_u.dir)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *h_return = tf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (S_ISREG(tf->tf_stat.st_mode)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome tf->tf_isdir = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome tf->tf_u.fd = open(path, O_RDONLY);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tf->tf_u.fd < 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *h_return = tf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeout:
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(tf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_close(void *arg, void *h)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct test_file *tf = h;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tf->tf_isdir)
199767f8919635c4928607450d9e0abb932109ceToomas Soome closedir(tf->tf_u.dir);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome close(tf->tf_u.fd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(tf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_isdir(void *arg, void *h)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct test_file *tf = h;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (tf->tf_isdir);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_read(void *arg, void *h, void *dst, size_t size, size_t *resid_return)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct test_file *tf = h;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t sz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tf->tf_isdir)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sz = read(tf->tf_u.fd, dst, size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sz < 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome *resid_return = size - sz;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_readdir(void *arg, void *h, uint32_t *fileno_return, uint8_t *type_return,
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t *namelen_return, char *name)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct test_file *tf = h;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct dirent *dp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!tf->tf_isdir)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome dp = readdir(tf->tf_u.dir);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!dp)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOENT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note: d_namlen is in the range 0..255 and therefore less
199767f8919635c4928607450d9e0abb932109ceToomas Soome * than PATH_MAX so we don't need to test before copying.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome *fileno_return = dp->d_fileno;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *type_return = dp->d_type;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *namelen_return = dp->d_namlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(name, dp->d_name, dp->d_namlen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome name[dp->d_namlen] = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_seek(void *arg, void *h, uint64_t offset, int whence)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct test_file *tf = h;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (tf->tf_isdir)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EINVAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (lseek(tf->tf_u.fd, offset, whence) < 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (errno);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_stat(void *arg, void *h, int *mode_return, int *uid_return, int *gid_return,
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t *size_return)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct test_file *tf = h;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome *mode_return = tf->tf_stat.st_mode;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *uid_return = tf->tf_stat.st_uid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *gid_return = tf->tf_stat.st_gid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *size_return = tf->tf_stat.st_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Disk image i/o
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_diskread(void *arg, int unit, uint64_t offset, void *dst, size_t size,
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t *resid_return)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t n;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (unit != 0 || disk_fd == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EIO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome n = pread(disk_fd, dst, size, offset);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (n < 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (errno);
199767f8919635c4928607450d9e0abb932109ceToomas Soome *resid_return = size - n;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_diskioctl(void *arg, int unit, u_long cmd, void *data)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct stat sb;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (unit != 0 || disk_fd == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EBADF);
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch (cmd) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case DIOCGSECTORSIZE:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(u_int *)data = 512;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case DIOCGMEDIASIZE:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fstat(disk_fd, &sb) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome *(off_t *)data = sb.st_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOTTY);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ENOTTY);
199767f8919635c4928607450d9e0abb932109ceToomas Soome };
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Guest virtual machine i/o
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note: guest addresses are kernel virtual
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_copyin(void *arg, const void *from, uint64_t to, size_t size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome to &= 0x7fffffff;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (to > image_size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EFAULT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (to + size > image_size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = image_size - to;
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(&image[to], from, size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_copyout(void *arg, uint64_t from, void *to, size_t size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome from &= 0x7fffffff;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (from > image_size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (EFAULT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (from + size > image_size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = image_size - from;
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(to, &image[from], size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_setreg(void *arg, int r, uint64_t v)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (r < 0 || r >= 16)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome regs[r] = v;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_setmsr(void *arg, int r, uint64_t v)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_setcr(void *arg, int r, uint64_t v)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_setgdt(void *arg, uint64_t v, size_t sz)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_exec(void *arg, uint64_t pc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Execute at 0x%"PRIu64"\n", pc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome test_exit(arg, 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Misc
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_delay(void *arg, int usec)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome usleep(usec);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_exit(void *arg, int v)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome tcsetattr(0, TCSAFLUSH, &oldterm);
199767f8919635c4928607450d9e0abb932109ceToomas Soome exit(v);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_getmem(void *arg, uint64_t *lowmem, uint64_t *highmem)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome *lowmem = 128*1024*1024;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *highmem = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeconst char *
199767f8919635c4928607450d9e0abb932109ceToomas Soometest_getenv(void *arg, int idx)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome static const char *vars[] = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome "foo=bar",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "bar=barbar",
199767f8919635c4928607450d9e0abb932109ceToomas Soome NULL
199767f8919635c4928607450d9e0abb932109ceToomas Soome };
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (vars[idx]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct loader_callbacks cb = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome .putc = test_putc,
199767f8919635c4928607450d9e0abb932109ceToomas Soome .getc = test_getc,
199767f8919635c4928607450d9e0abb932109ceToomas Soome .poll = test_poll,
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome .open = test_open,
199767f8919635c4928607450d9e0abb932109ceToomas Soome .close = test_close,
199767f8919635c4928607450d9e0abb932109ceToomas Soome .isdir = test_isdir,
199767f8919635c4928607450d9e0abb932109ceToomas Soome .read = test_read,
199767f8919635c4928607450d9e0abb932109ceToomas Soome .readdir = test_readdir,
199767f8919635c4928607450d9e0abb932109ceToomas Soome .seek = test_seek,
199767f8919635c4928607450d9e0abb932109ceToomas Soome .stat = test_stat,
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome .diskread = test_diskread,
199767f8919635c4928607450d9e0abb932109ceToomas Soome .diskioctl = test_diskioctl,
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome .copyin = test_copyin,
199767f8919635c4928607450d9e0abb932109ceToomas Soome .copyout = test_copyout,
199767f8919635c4928607450d9e0abb932109ceToomas Soome .setreg = test_setreg,
199767f8919635c4928607450d9e0abb932109ceToomas Soome .setmsr = test_setmsr,
199767f8919635c4928607450d9e0abb932109ceToomas Soome .setcr = test_setcr,
199767f8919635c4928607450d9e0abb932109ceToomas Soome .setgdt = test_setgdt,
199767f8919635c4928607450d9e0abb932109ceToomas Soome .exec = test_exec,
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome .delay = test_delay,
199767f8919635c4928607450d9e0abb932109ceToomas Soome .exit = test_exit,
199767f8919635c4928607450d9e0abb932109ceToomas Soome .getmem = test_getmem,
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome .getenv = test_getenv,
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomeusage()
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("usage: [-b <userboot shared object>] [-d <disk image path>] [-h <host filesystem path>\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome exit(1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomemain(int argc, char** argv)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome void *h;
199767f8919635c4928607450d9e0abb932109ceToomas Soome void (*func)(struct loader_callbacks *, void *, int, int);
199767f8919635c4928607450d9e0abb932109ceToomas Soome int opt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *disk_image = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *userboot_obj = "/boot/userboot.so";
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while ((opt = getopt(argc, argv, "b:d:h:")) != -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch (opt) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'b':
199767f8919635c4928607450d9e0abb932109ceToomas Soome userboot_obj = optarg;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'd':
199767f8919635c4928607450d9e0abb932109ceToomas Soome disk_image = optarg;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'h':
199767f8919635c4928607450d9e0abb932109ceToomas Soome host_base = optarg;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '?':
199767f8919635c4928607450d9e0abb932109ceToomas Soome usage();
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome h = dlopen(userboot_obj, RTLD_LOCAL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!h) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s\n", dlerror());
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome func = dlsym(h, "loader_main");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!func) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s\n", dlerror());
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome image_size = 128*1024*1024;
199767f8919635c4928607450d9e0abb932109ceToomas Soome image = malloc(image_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (disk_image) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome disk_fd = open(disk_image, O_RDONLY);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (disk_fd < 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome err(1, "Can't open disk image '%s'", disk_image);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome tcgetattr(0, &term);
199767f8919635c4928607450d9e0abb932109ceToomas Soome oldterm = term;
199767f8919635c4928607450d9e0abb932109ceToomas Soome term.c_iflag &= ~(ICRNL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome term.c_lflag &= ~(ICANON|ECHO);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tcsetattr(0, TCSAFLUSH, &term);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome func(&cb, NULL, USERBOOT_VERSION_3, disk_fd >= 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}