]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/uboot/uboot_disk.c
Merge llvm-project release/17.x llvmorg-17.0.6-0-g6009708b4367
[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/param.h>
35 #include <sys/disk.h>
36 #include <machine/stdarg.h>
37 #include <stand.h>
38
39 #include "api_public.h"
40 #include "bootstrap.h"
41 #include "disk.h"
42 #include "glue.h"
43 #include "libuboot.h"
44
45 #define stor_printf(fmt, args...) do {                  \
46     printf("%s%d: ", dev->dd.d_dev->dv_name, dev->dd.d_unit);   \
47     printf(fmt, ##args);                                \
48 } while (0)
49
50 #ifdef DEBUG
51 #define debugf(fmt, args...) do { printf("%s(): ", __func__);   \
52     printf(fmt,##args); } while (0)
53 #else
54 #define debugf(fmt, args...)
55 #endif
56
57 static struct {
58         int             opened; /* device is opened */
59         int             handle; /* storage device handle */
60         int             type;   /* storage type */
61         off_t           blocks; /* block count */
62         u_int           bsize;  /* block size */
63 } stor_info[UB_MAX_DEV];
64
65 #define SI(dev)         (stor_info[(dev)->dd.d_unit])
66
67 static int stor_info_no = 0;
68 static int stor_opendev(struct disk_devdesc *);
69 static int stor_readdev(struct disk_devdesc *, daddr_t, size_t, char *);
70
71 /* devsw I/F */
72 static int stor_init(void);
73 static int stor_strategy(void *, int, daddr_t, size_t, char *, size_t *);
74 static int stor_open(struct open_file *, ...);
75 static int stor_close(struct open_file *);
76 static int stor_ioctl(struct open_file *f, u_long cmd, void *data);
77 static int stor_print(int);
78 static void stor_cleanup(void);
79
80 struct devsw uboot_storage = {
81         .dv_name = "disk",
82         .dv_type = DEVT_DISK,
83         .dv_init = stor_init,
84         .dv_strategy = stor_strategy,
85         .dv_open = stor_open,
86         .dv_close = stor_close,
87         .dv_ioctl = stor_ioctl,
88         .dv_print = stor_print,
89         .dv_cleanup = stor_cleanup,
90         .dv_fmtdev = disk_fmtdev,
91         .dv_parsedev = disk_parsedev,
92 };
93
94 static int
95 stor_init(void)
96 {
97         struct device_info *di;
98         int i;
99
100         if (devs_no == 0) {
101                 printf("No U-Boot devices! Really enumerated?\n");
102                 return (-1);
103         }
104
105         for (i = 0; i < devs_no; i++) {
106                 di = ub_dev_get(i);
107                 if ((di != NULL) && (di->type & DEV_TYP_STOR)) {
108                         if (stor_info_no >= UB_MAX_DEV) {
109                                 printf("Too many storage devices: %d\n",
110                                     stor_info_no);
111                                 return (-1);
112                         }
113                         stor_info[stor_info_no].handle = i;
114                         stor_info[stor_info_no].opened = 0;
115                         stor_info[stor_info_no].type = di->type;
116                         stor_info[stor_info_no].blocks =
117                             di->di_stor.block_count;
118                         stor_info[stor_info_no].bsize =
119                             di->di_stor.block_size;
120                         stor_info_no++;
121                 }
122         }
123
124         if (!stor_info_no) {
125                 debugf("No storage devices\n");
126                 return (-1);
127         }
128
129         debugf("storage devices found: %d\n", stor_info_no);
130         return (0);
131 }
132
133 static void
134 stor_cleanup(void)
135 {
136         int i;
137
138         for (i = 0; i < stor_info_no; i++)
139                 if (stor_info[i].opened > 0)
140                         ub_dev_close(stor_info[i].handle);
141 }
142
143 static int
144 stor_strategy(void *devdata, int rw, daddr_t blk, size_t size,
145     char *buf, size_t *rsize)
146 {
147         struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
148         daddr_t bcount;
149         int err;
150
151         rw &= F_MASK;
152         if (rw != F_READ) {
153                 stor_printf("write attempt, operation not supported!\n");
154                 return (EROFS);
155         }
156
157         if (size % SI(dev).bsize) {
158                 stor_printf("size=%zu not multiple of device "
159                     "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
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
188 stor_opendev(struct disk_devdesc *dev)
189 {
190         int err;
191
192         if (dev->dd.d_unit < 0 || dev->dd.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));
206 }
207
208 static int
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
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 int
240 stor_print(int verbose)
241 {
242         struct disk_devdesc dev;
243         static char line[80];
244         int i, ret = 0;
245
246         if (stor_info_no == 0)
247                 return (ret);
248
249         printf("%s devices:", uboot_storage.dv_name);
250         if ((ret = pager_output("\n")) != 0)
251                 return (ret);
252
253         for (i = 0; i < stor_info_no; i++) {
254                 dev.dd.d_dev = &uboot_storage;
255                 dev.dd.d_unit = i;
256                 dev.d_slice = D_SLICENONE;
257                 dev.d_partition = D_PARTNONE;
258                 snprintf(line, sizeof(line), "\tdisk%d (%s)\n", i,
259                     ub_stor_type(SI(&dev).type));
260                 if ((ret = pager_output(line)) != 0)
261                         break;
262                 if (stor_opendev(&dev) == 0) {
263                         sprintf(line, "\tdisk%d", i);
264                         ret = disk_print(&dev, line, verbose);
265                         disk_close(&dev);
266                         if (ret != 0)
267                                 break;
268                 }
269         }
270         return (ret);
271 }
272
273 static int
274 stor_ioctl(struct open_file *f, u_long cmd, void *data)
275 {
276         struct disk_devdesc *dev;
277         int rc;
278
279         dev = (struct disk_devdesc *)f->f_devdata;
280         rc = disk_ioctl(dev, cmd, data);
281         if (rc != ENOTTY)
282                 return (rc);
283
284         switch (cmd) {
285         case DIOCGSECTORSIZE:
286                 *(u_int *)data = SI(dev).bsize;
287                 break;
288         case DIOCGMEDIASIZE:
289                 *(uint64_t *)data = SI(dev).bsize * SI(dev).blocks;
290                 break;
291         default:
292                 return (ENOTTY);
293         }
294         return (0);
295 }
296
297
298 /*
299  * Return the device unit number for the given type and type-relative unit
300  * number.
301  */
302 int
303 uboot_diskgetunit(int type, int type_unit)
304 {
305         int local_type_unit;
306         int i;
307
308         local_type_unit = 0;
309         for (i = 0; i < stor_info_no; i++) {
310                 if ((stor_info[i].type & type) == type) {
311                         if (local_type_unit == type_unit) {
312                                 return (i);
313                         }
314                         local_type_unit++;
315                 }
316         }
317
318         return (-1);
319 }