1 /*-
2 * Copyright (c) 2008 Semihalf, Rafal Jaworowski
3 * Copyright (c) 2009 Semihalf, Piotr Ziecik
4 * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 */
29
30 /*
31 * Block storage I/O routines for U-Boot
32 */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/disk.h>
39 #include <machine/stdarg.h>
40 #include <stand.h>
41
42 #include "api_public.h"
43 #include "bootstrap.h"
44 #include "disk.h"
45 #include "glue.h"
46 #include "libuboot.h"
47
48 #define stor_printf(fmt, args...) do { \
49 printf("%s%d: ", dev->d_dev->dv_name, dev->d_unit); \
50 printf(fmt, ##args); \
51 } while (0)
52
53 #ifdef DEBUG
54 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
55 printf(fmt,##args); } while (0)
56 #else
57 #define debugf(fmt, args...)
58 #endif
59
60 static struct {
61 int opened; /* device is opened */
62 int handle; /* storage device handle */
63 int type; /* storage type */
64 off_t blocks; /* block count */
65 u_int bsize; /* block size */
66 } stor_info[UB_MAX_DEV];
67
68 #define SI(dev) (stor_info[(dev)->d_unit])
69
70 static int stor_info_no = 0;
71 static int stor_opendev(struct disk_devdesc *);
72 static int stor_readdev(struct disk_devdesc *, daddr_t, size_t, char *);
73
74 /* devsw I/F */
75 static int stor_init(void);
76 static int stor_strategy(void *, int, daddr_t, size_t, char *, size_t *);
77 static int stor_open(struct open_file *, ...);
78 static int stor_close(struct open_file *);
79 static int stor_ioctl(struct open_file *f, u_long cmd, void *data);
80 static void stor_print(int);
81 static void stor_cleanup(void);
82
83 struct devsw uboot_storage = {
84 "disk",
85 DEVT_DISK,
86 stor_init,
87 stor_strategy,
88 stor_open,
89 stor_close,
90 stor_ioctl,
91 stor_print,
92 stor_cleanup
93 };
94
95 static int
stor_init(void)96 stor_init(void)
97 {
98 struct device_info *di;
99 int i;
100
101 if (devs_no == 0) {
102 printf("No U-Boot devices! Really enumerated?\n");
103 return (-1);
104 }
105
106 for (i = 0; i < devs_no; i++) {
107 di = ub_dev_get(i);
108 if ((di != NULL) && (di->type & DEV_TYP_STOR)) {
109 if (stor_info_no >= UB_MAX_DEV) {
110 printf("Too many storage devices: %d\n",
111 stor_info_no);
112 return (-1);
113 }
114 stor_info[stor_info_no].handle = i;
115 stor_info[stor_info_no].opened = 0;
116 stor_info[stor_info_no].type = di->type;
117 stor_info[stor_info_no].blocks =
118 di->di_stor.block_count;
119 stor_info[stor_info_no].bsize =
120 di->di_stor.block_size;
121 stor_info_no++;
122 }
123 }
124
125 if (!stor_info_no) {
126 debugf("No storage devices\n");
127 return (-1);
128 }
129
130 debugf("storage devices found: %d\n", stor_info_no);
131 return (0);
132 }
133
134 static void
stor_cleanup(void)135 stor_cleanup(void)
136 {
137 int i;
138
139 for (i = 0; i < stor_info_no; i++)
140 if (stor_info[i].opened > 0)
141 ub_dev_close(stor_info[i].handle);
142 disk_cleanup(&uboot_storage);
143 }
144
145 static int
stor_strategy(void * devdata,int rw,daddr_t blk,size_t size,char * buf,size_t * rsize)146 stor_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf,
147 size_t *rsize)
148 {
149 struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
150 daddr_t bcount;
151 int err;
152
153 if (rw != F_READ) {
154 stor_printf("write attempt, operation not supported!\n");
155 return (EROFS);
156 }
157
158 if (size % SI(dev).bsize) {
159 stor_printf("size=%d not multiple of device block size=%d\n",
160 size, SI(dev).bsize);
161 return (EIO);
162 }
163 bcount = size / SI(dev).bsize;
164 if (rsize)
165 *rsize = 0;
166
167 err = stor_readdev(dev, blk + dev->d_offset, bcount, buf);
168 if (!err && rsize)
169 *rsize = size;
170
171 return (err);
172 }
173
174 static int
stor_open(struct open_file * f,...)175 stor_open(struct open_file *f, ...)
176 {
177 va_list ap;
178 struct disk_devdesc *dev;
179
180 va_start(ap, f);
181 dev = va_arg(ap, struct disk_devdesc *);
182 va_end(ap);
183
184 return (stor_opendev(dev));
185 }
186
187 static int
stor_opendev(struct disk_devdesc * dev)188 stor_opendev(struct disk_devdesc *dev)
189 {
190 int err;
191
192 if (dev->d_unit < 0 || dev->d_unit >= stor_info_no)
193 return (EIO);
194
195 if (SI(dev).opened == 0) {
196 err = ub_dev_open(SI(dev).handle);
197 if (err != 0) {
198 stor_printf("device open failed with error=%d, "
199 "handle=%d\n", err, SI(dev).handle);
200 return (ENXIO);
201 }
202 SI(dev).opened++;
203 }
204 return (disk_open(dev, SI(dev).blocks * SI(dev).bsize,
205 SI(dev).bsize, 0));
206 }
207
208 static int
stor_close(struct open_file * f)209 stor_close(struct open_file *f)
210 {
211 struct disk_devdesc *dev;
212
213 dev = (struct disk_devdesc *)(f->f_devdata);
214 return (disk_close(dev));
215 }
216
217 static int
stor_readdev(struct disk_devdesc * dev,daddr_t blk,size_t size,char * buf)218 stor_readdev(struct disk_devdesc *dev, daddr_t blk, size_t size, char *buf)
219 {
220 lbasize_t real_size;
221 int err;
222
223 debugf("reading blk=%d size=%d @ 0x%08x\n", (int)blk, size, (uint32_t)buf);
224
225 err = ub_dev_read(SI(dev).handle, buf, size, blk, &real_size);
226 if (err != 0) {
227 stor_printf("read failed, error=%d\n", err);
228 return (EIO);
229 }
230
231 if (real_size != size) {
232 stor_printf("real size != size\n");
233 err = EIO;
234 }
235
236 return (err);
237 }
238
239 static void
stor_print(int verbose)240 stor_print(int verbose)
241 {
242 struct disk_devdesc dev;
243 static char line[80];
244 int i;
245
246 for (i = 0; i < stor_info_no; i++) {
247 dev.d_dev = &uboot_storage;
248 dev.d_unit = i;
249 dev.d_slice = -1;
250 dev.d_partition = -1;
251 sprintf(line, "\tdisk%d (%s)\n", i,
252 ub_stor_type(SI(&dev).type));
253 pager_output(line);
254 if (stor_opendev(&dev) == 0) {
255 sprintf(line, "\tdisk%d", i);
256 disk_print(&dev, line, verbose);
257 disk_close(&dev);
258 }
259 }
260 }
261
262 static int
stor_ioctl(struct open_file * f,u_long cmd,void * data)263 stor_ioctl(struct open_file *f, u_long cmd, void *data)
264 {
265 struct disk_devdesc *dev;
266
267 dev = (struct disk_devdesc *)f->f_devdata;
268 switch (cmd) {
269 case DIOCGSECTORSIZE:
270 *(u_int *)data = SI(dev).bsize;
271 break;
272 case DIOCGMEDIASIZE:
273 *(off_t *)data = SI(dev).bsize * SI(dev).blocks;
274 break;
275 default:
276 return (ENOTTY);
277 }
278 return (0);
279 }
280
281
282 /*
283 * Return the device unit number for the given type and type-relative unit
284 * number.
285 */
286 int
uboot_diskgetunit(int type,int type_unit)287 uboot_diskgetunit(int type, int type_unit)
288 {
289 int local_type_unit;
290 int i;
291
292 local_type_unit = 0;
293 for (i = 0; i < stor_info_no; i++) {
294 if ((stor_info[i].type & type) == type) {
295 if (local_type_unit == type_unit) {
296 return (i);
297 }
298 local_type_unit++;
299 }
300 }
301
302 return (-1);
303 }
304