2 * Copyright (c) 2011 Google, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * Userboot disk image handling.
37 #include <bootstrap.h>
40 #include "libuserboot.h"
42 struct userdisk_info {
47 int userboot_disk_maxunit = 0;
49 static int userdisk_maxunit = 0;
50 static struct userdisk_info *ud_info;
52 static int userdisk_init(void);
53 static void userdisk_cleanup(void);
54 static int userdisk_strategy(void *devdata, int flag, daddr_t dblk,
55 size_t size, char *buf, size_t *rsize);
56 static int userdisk_open(struct open_file *f, ...);
57 static int userdisk_close(struct open_file *f);
58 static int userdisk_ioctl(struct open_file *f, u_long cmd, void *data);
59 static void userdisk_print(int verbose);
61 struct devsw userboot_disk = {
74 * Initialize userdisk_info structure for each disk.
83 userdisk_maxunit = userboot_disk_maxunit;
84 if (userdisk_maxunit > 0) {
85 ud_info = malloc(sizeof(*ud_info) * userdisk_maxunit);
88 for (i = 0; i < userdisk_maxunit; i++) {
89 if (CALLBACK(diskioctl, i, DIOCGSECTORSIZE,
90 §orsize) != 0 || CALLBACK(diskioctl, i,
91 DIOCGMEDIASIZE, &mediasize) != 0)
93 ud_info[i].mediasize = mediasize;
94 ud_info[i].sectorsize = sectorsize;
102 userdisk_cleanup(void)
105 if (userdisk_maxunit > 0)
107 disk_cleanup(&userboot_disk);
111 * Print information about disks
114 userdisk_print(int verbose)
116 struct disk_devdesc dev;
120 for (i = 0; i < userdisk_maxunit; i++) {
121 sprintf(line, " disk%d: Guest drive image\n", i);
123 dev.d_dev = &userboot_disk;
126 dev.d_partition = -1;
127 if (disk_open(&dev, ud_info[i].mediasize,
128 ud_info[i].sectorsize, 0) == 0) {
129 sprintf(line, " disk%d", i);
130 disk_print(&dev, line, verbose);
137 * Attempt to open the disk described by (dev) for use by (f).
140 userdisk_open(struct open_file *f, ...)
143 struct disk_devdesc *dev;
146 dev = va_arg(ap, struct disk_devdesc *);
149 if (dev->d_unit < 0 || dev->d_unit >= userdisk_maxunit)
152 return (disk_open(dev, ud_info[dev->d_unit].mediasize,
153 ud_info[dev->d_unit].sectorsize, 0));
157 userdisk_close(struct open_file *f)
159 struct disk_devdesc *dev;
161 dev = (struct disk_devdesc *)f->f_devdata;
162 return (disk_close(dev));
166 userdisk_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
167 char *buf, size_t *rsize)
169 struct disk_devdesc *dev = devdata;
180 off = (dblk + dev->d_offset) * ud_info[dev->d_unit].sectorsize;
181 rc = CALLBACK(diskread, dev->d_unit, off, buf, size, &resid);
185 *rsize = size - resid;
190 userdisk_ioctl(struct open_file *f, u_long cmd, void *data)
192 struct disk_devdesc *dev;
194 dev = (struct disk_devdesc *)f->f_devdata;
195 return (CALLBACK(diskioctl, dev->d_unit, cmd, data));