8010 loader: want mechanism to avoid RA with bcache
[unleashed.git] / usr / src / boot / sys / boot / usb / storage / umass_loader.c
blob963e00b61b8b7fce051bbc248229ef108fdd7ec5
1 /* $FreeBSD$ */
2 /*-
3 * Copyright (c) 2014 Hans Petter Selasky <hselasky@FreeBSD.org>
4 * All rights reserved.
6 * This software was developed by SRI International and the University of
7 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
8 * ("CTSRD"), as part of the DARPA CRASH research programme.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/param.h>
34 #include <bootstrap.h>
35 #include <stdarg.h>
37 #include <stand.h>
38 #include <disk.h>
40 #define HAVE_STANDARD_DEFS
42 #include USB_GLOBAL_INCLUDE_FILE
44 #include "umass_common.h"
46 static int umass_disk_init(void);
47 static int umass_disk_open(struct open_file *,...);
48 static int umass_disk_close(struct open_file *);
49 static void umass_disk_cleanup(void);
50 static int umass_disk_ioctl(struct open_file *, u_long, void *);
51 static int umass_disk_strategy(void *, int, daddr_t, size_t, char *, size_t *);
52 static int umass_disk_print(int);
54 struct devsw umass_disk = {
55 .dv_name = "umass",
56 .dv_type = DEVT_DISK,
57 .dv_init = umass_disk_init,
58 .dv_strategy = umass_disk_strategy,
59 .dv_open = umass_disk_open,
60 .dv_close = umass_disk_close,
61 .dv_ioctl = umass_disk_ioctl,
62 .dv_print = umass_disk_print,
63 .dv_cleanup = umass_disk_cleanup,
66 static int
67 umass_disk_init(void)
69 uint32_t time;
71 usb_init();
72 usb_needs_explore_all();
74 /* wait 8 seconds for a USB mass storage device to appear */
75 for (time = 0; time < (8 * hz); time++) {
76 usb_idle();
77 delay(1000000 / hz);
78 time++;
79 callout_process(1);
80 if (umass_uaa.device != NULL)
81 return (0);
83 return (0);
86 static int
87 umass_disk_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
88 char *buf, size_t *rsizep)
90 if (umass_uaa.device == NULL)
91 return (ENXIO);
92 if (rsizep != NULL)
93 *rsizep = 0;
95 flag &= F_MASK;
96 if (flag == F_WRITE) {
97 if (usb_msc_write_10(umass_uaa.device, 0, dblk, size >> 9, buf) != 0)
98 return (EINVAL);
99 } else if (flag == F_READ) {
100 if (usb_msc_read_10(umass_uaa.device, 0, dblk, size >> 9, buf) != 0)
101 return (EINVAL);
102 } else {
103 return (EROFS);
106 if (rsizep != NULL)
107 *rsizep = size;
108 return (0);
111 static int
112 umass_disk_open_sub(struct disk_devdesc *dev)
114 uint32_t nblock;
115 uint32_t blocksize;
117 if (usb_msc_read_capacity(umass_uaa.device, 0, &nblock, &blocksize) != 0)
118 return (EINVAL);
120 return (disk_open(dev, ((uint64_t)nblock + 1) * (uint64_t)blocksize, blocksize));
123 static int
124 umass_disk_open(struct open_file *f,...)
126 va_list ap;
127 struct disk_devdesc *dev;
129 va_start(ap, f);
130 dev = va_arg(ap, struct disk_devdesc *);
131 va_end(ap);
133 if (umass_uaa.device == NULL)
134 return (ENXIO);
135 if (dev->d_unit != 0)
136 return (EIO);
137 return (umass_disk_open_sub(dev));
140 static int
141 umass_disk_ioctl(struct open_file *f __unused, u_long cmd, void *buf)
143 uint32_t nblock;
144 uint32_t blocksize;
146 switch (cmd) {
147 case IOCTL_GET_BLOCK_SIZE:
148 case IOCTL_GET_BLOCKS:
149 if (usb_msc_read_capacity(umass_uaa.device, 0,
150 &nblock, &blocksize) != 0)
151 return (EINVAL);
153 if (cmd == IOCTL_GET_BLOCKS)
154 *(uint32_t*)buf = nblock;
155 else
156 *(uint32_t*)buf = blocksize;
158 return (0);
159 default:
160 return (ENXIO);
164 static int
165 umass_disk_close(struct open_file *f)
167 struct disk_devdesc *dev;
169 dev = (struct disk_devdesc *)f->f_devdata;
170 return (disk_close(dev));
173 static int
174 umass_disk_print(int verbose)
176 struct disk_devdesc dev;
178 printf("%s devices:", umass_disk.dv_name);
179 if (pager_output("\n") != 0)
180 return (1);
182 memset(&dev, 0, sizeof(dev));
184 pager_output(" umass0 UMASS device\n");
185 dev.d_dev = &umass_disk;
186 dev.d_unit = 0;
187 dev.d_slice = -1;
188 dev.d_partition = -1;
190 if (umass_disk_open_sub(&dev) == 0) {
191 disk_print(&dev, " umass0", verbose);
192 disk_close(&dev);
196 static void
197 umass_disk_cleanup(void)
199 disk_cleanup(&umass_disk);
201 usb_uninit();
205 /* USB specific functions */
207 extern void callout_process(int);
208 extern void usb_idle(void);
209 extern void usb_init(void);
210 extern void usb_uninit(void);
212 void
213 DELAY(unsigned int usdelay)
215 delay(usdelay);
219 pause(const char *what, int timeout)
221 if (timeout == 0)
222 timeout = 1;
224 delay((1000000 / hz) * timeout);
226 return (0);