]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/boot/userboot/userboot/userboot_disk.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / boot / userboot / userboot / userboot_disk.c
1 /*-
2  * Copyright (c) 2011 Google, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 /*
31  * Userboot disk image handling.
32  */
33
34 #include <sys/disk.h>
35 #include <stand.h>
36 #include <stdarg.h>
37 #include <bootstrap.h>
38
39 #include "disk.h"
40 #include "libuserboot.h"
41
42 struct userdisk_info {
43         uint64_t        mediasize;
44         uint16_t        sectorsize;
45 };
46
47 int userboot_disk_maxunit = 0;
48
49 static int userdisk_maxunit = 0;
50 static struct userdisk_info     *ud_info;
51
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);
60
61 struct devsw userboot_disk = {
62         "disk",
63         DEVT_DISK,
64         userdisk_init,
65         userdisk_strategy,
66         userdisk_open,
67         userdisk_close,
68         userdisk_ioctl,
69         userdisk_print,
70         userdisk_cleanup
71 };
72
73 /*
74  * Initialize userdisk_info structure for each disk.
75  */
76 static int
77 userdisk_init(void)
78 {
79         off_t mediasize;
80         u_int sectorsize;
81         int i;
82
83         userdisk_maxunit = userboot_disk_maxunit;
84         if (userdisk_maxunit > 0) {
85                 ud_info = malloc(sizeof(*ud_info) * userdisk_maxunit);
86                 if (ud_info == NULL)
87                         return (ENOMEM);
88                 for (i = 0; i < userdisk_maxunit; i++) {
89                         if (CALLBACK(diskioctl, i, DIOCGSECTORSIZE,
90                             &sectorsize) != 0 || CALLBACK(diskioctl, i,
91                             DIOCGMEDIASIZE, &mediasize) != 0)
92                                 return (ENXIO);
93                         ud_info[i].mediasize = mediasize;
94                         ud_info[i].sectorsize = sectorsize;
95                 }
96         }
97
98         return(0);
99 }
100
101 static void
102 userdisk_cleanup(void)
103 {
104
105         if (userdisk_maxunit > 0)
106                 free(ud_info);
107         disk_cleanup(&userboot_disk);
108 }
109
110 /*
111  * Print information about disks
112  */
113 static void
114 userdisk_print(int verbose)
115 {
116         struct disk_devdesc dev;
117         char line[80];
118         int i;
119
120         for (i = 0; i < userdisk_maxunit; i++) {
121                 sprintf(line, "    disk%d:   Guest drive image\n", i);
122                 pager_output(line);
123                 dev.d_dev = &userboot_disk;
124                 dev.d_unit = i;
125                 dev.d_slice = -1;
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);
131                         disk_close(&dev);
132                 }
133         }
134 }
135
136 /*
137  * Attempt to open the disk described by (dev) for use by (f).
138  */
139 static int
140 userdisk_open(struct open_file *f, ...)
141 {
142         va_list                 ap;
143         struct disk_devdesc     *dev;
144
145         va_start(ap, f);
146         dev = va_arg(ap, struct disk_devdesc *);
147         va_end(ap);
148
149         if (dev->d_unit < 0 || dev->d_unit >= userdisk_maxunit)
150                 return (EIO);
151
152         return (disk_open(dev, ud_info[dev->d_unit].mediasize,
153             ud_info[dev->d_unit].sectorsize, 0));
154 }
155
156 static int
157 userdisk_close(struct open_file *f)
158 {
159         struct disk_devdesc *dev;
160
161         dev = (struct disk_devdesc *)f->f_devdata;
162         return (disk_close(dev));
163 }
164
165 static int
166 userdisk_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
167     char *buf, size_t *rsize)
168 {
169         struct disk_devdesc *dev = devdata;
170         uint64_t        off;
171         size_t          resid;
172         int             rc;
173
174         if (rw == F_WRITE)
175                 return (EROFS);
176         if (rw != F_READ)
177                 return (EINVAL);
178         if (rsize)
179                 *rsize = 0;
180         off = (dblk + dev->d_offset) * ud_info[dev->d_unit].sectorsize;
181         rc = CALLBACK(diskread, dev->d_unit, off, buf, size, &resid);
182         if (rc)
183                 return (rc);
184         if (rsize)
185                 *rsize = size - resid;
186         return (0);
187 }
188
189 static int
190 userdisk_ioctl(struct open_file *f, u_long cmd, void *data)
191 {
192         struct disk_devdesc *dev;
193
194         dev = (struct disk_devdesc *)f->f_devdata;
195         return (CALLBACK(diskioctl, dev->d_unit, cmd, data));
196 }