]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/uboot/uboot_disk.c
MFV: zlib 1.2.13.
[FreeBSD/FreeBSD.git] / stand / uboot / uboot_disk.c
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->dd.d_dev->dv_name, dev->dd.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)->dd.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 int stor_print(int);
81 static void stor_cleanup(void);
82
83 struct devsw uboot_storage = {
84         .dv_name = "disk",
85         .dv_type = DEVT_DISK,
86         .dv_init = stor_init,
87         .dv_strategy = stor_strategy,
88         .dv_open = stor_open,
89         .dv_close = stor_close,
90         .dv_ioctl = stor_ioctl,
91         .dv_print = stor_print,
92         .dv_cleanup = stor_cleanup,
93         .dv_fmtdev = disk_fmtdev,
94         .dv_parsedev = disk_parsedev,
95 };
96
97 static int
98 stor_init(void)
99 {
100         struct device_info *di;
101         int i;
102
103         if (devs_no == 0) {
104                 printf("No U-Boot devices! Really enumerated?\n");
105                 return (-1);
106         }
107
108         for (i = 0; i < devs_no; i++) {
109                 di = ub_dev_get(i);
110                 if ((di != NULL) && (di->type & DEV_TYP_STOR)) {
111                         if (stor_info_no >= UB_MAX_DEV) {
112                                 printf("Too many storage devices: %d\n",
113                                     stor_info_no);
114                                 return (-1);
115                         }
116                         stor_info[stor_info_no].handle = i;
117                         stor_info[stor_info_no].opened = 0;
118                         stor_info[stor_info_no].type = di->type;
119                         stor_info[stor_info_no].blocks =
120                             di->di_stor.block_count;
121                         stor_info[stor_info_no].bsize =
122                             di->di_stor.block_size;
123                         stor_info_no++;
124                 }
125         }
126
127         if (!stor_info_no) {
128                 debugf("No storage devices\n");
129                 return (-1);
130         }
131
132         debugf("storage devices found: %d\n", stor_info_no);
133         return (0);
134 }
135
136 static void
137 stor_cleanup(void)
138 {
139         int i;
140
141         for (i = 0; i < stor_info_no; i++)
142                 if (stor_info[i].opened > 0)
143                         ub_dev_close(stor_info[i].handle);
144 }
145
146 static int
147 stor_strategy(void *devdata, int rw, daddr_t blk, size_t size,
148     char *buf, size_t *rsize)
149 {
150         struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
151         daddr_t bcount;
152         int err;
153
154         rw &= F_MASK;
155         if (rw != F_READ) {
156                 stor_printf("write attempt, operation not supported!\n");
157                 return (EROFS);
158         }
159
160         if (size % SI(dev).bsize) {
161                 stor_printf("size=%zu not multiple of device "
162                     "block size=%d\n",
163                     size, SI(dev).bsize);
164                 return (EIO);
165         }
166         bcount = size / SI(dev).bsize;
167         if (rsize)
168                 *rsize = 0;
169
170         err = stor_readdev(dev, blk + dev->d_offset, bcount, buf);
171         if (!err && rsize)
172                 *rsize = size;
173
174         return (err);
175 }
176
177 static int
178 stor_open(struct open_file *f, ...)
179 {
180         va_list ap;
181         struct disk_devdesc *dev;
182
183         va_start(ap, f);
184         dev = va_arg(ap, struct disk_devdesc *);
185         va_end(ap);
186
187         return (stor_opendev(dev));
188 }
189
190 static int
191 stor_opendev(struct disk_devdesc *dev)
192 {
193         int err;
194
195         if (dev->dd.d_unit < 0 || dev->dd.d_unit >= stor_info_no)
196                 return (EIO);
197
198         if (SI(dev).opened == 0) {
199                 err = ub_dev_open(SI(dev).handle);
200                 if (err != 0) {
201                         stor_printf("device open failed with error=%d, "
202                             "handle=%d\n", err, SI(dev).handle);
203                         return (ENXIO);
204                 }
205                 SI(dev).opened++;
206         }
207         return (disk_open(dev, SI(dev).blocks * SI(dev).bsize,
208             SI(dev).bsize));
209 }
210
211 static int
212 stor_close(struct open_file *f)
213 {
214         struct disk_devdesc *dev;
215
216         dev = (struct disk_devdesc *)(f->f_devdata);
217         return (disk_close(dev));
218 }
219
220 static int
221 stor_readdev(struct disk_devdesc *dev, daddr_t blk, size_t size, char *buf)
222 {
223         lbasize_t real_size;
224         int err;
225
226         debugf("reading blk=%d size=%d @ 0x%08x\n", (int)blk, size, (uint32_t)buf);
227
228         err = ub_dev_read(SI(dev).handle, buf, size, blk, &real_size);
229         if (err != 0) {
230                 stor_printf("read failed, error=%d\n", err);
231                 return (EIO);
232         }
233
234         if (real_size != size) {
235                 stor_printf("real size != size\n");
236                 err = EIO;
237         }
238
239         return (err);
240 }
241
242 static int
243 stor_print(int verbose)
244 {
245         struct disk_devdesc dev;
246         static char line[80];
247         int i, ret = 0;
248
249         if (stor_info_no == 0)
250                 return (ret);
251
252         printf("%s devices:", uboot_storage.dv_name);
253         if ((ret = pager_output("\n")) != 0)
254                 return (ret);
255
256         for (i = 0; i < stor_info_no; i++) {
257                 dev.dd.d_dev = &uboot_storage;
258                 dev.dd.d_unit = i;
259                 dev.d_slice = D_SLICENONE;
260                 dev.d_partition = D_PARTNONE;
261                 snprintf(line, sizeof(line), "\tdisk%d (%s)\n", i,
262                     ub_stor_type(SI(&dev).type));
263                 if ((ret = pager_output(line)) != 0)
264                         break;
265                 if (stor_opendev(&dev) == 0) {
266                         sprintf(line, "\tdisk%d", i);
267                         ret = disk_print(&dev, line, verbose);
268                         disk_close(&dev);
269                         if (ret != 0)
270                                 break;
271                 }
272         }
273         return (ret);
274 }
275
276 static int
277 stor_ioctl(struct open_file *f, u_long cmd, void *data)
278 {
279         struct disk_devdesc *dev;
280         int rc;
281
282         dev = (struct disk_devdesc *)f->f_devdata;
283         rc = disk_ioctl(dev, cmd, data);
284         if (rc != ENOTTY)
285                 return (rc);
286
287         switch (cmd) {
288         case DIOCGSECTORSIZE:
289                 *(u_int *)data = SI(dev).bsize;
290                 break;
291         case DIOCGMEDIASIZE:
292                 *(uint64_t *)data = SI(dev).bsize * SI(dev).blocks;
293                 break;
294         default:
295                 return (ENOTTY);
296         }
297         return (0);
298 }
299
300
301 /*
302  * Return the device unit number for the given type and type-relative unit
303  * number.
304  */
305 int
306 uboot_diskgetunit(int type, int type_unit)
307 {
308         int local_type_unit;
309         int i;
310
311         local_type_unit = 0;
312         for (i = 0; i < stor_info_no; i++) {
313                 if ((stor_info[i].type & type) == type) {
314                         if (local_type_unit == type_unit) {
315                                 return (i);
316                         }
317                         local_type_unit++;
318                 }
319         }
320
321         return (-1);
322 }