]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/boot/efi/libefi/efipart.c
MFV r302218: file 5.28.
[FreeBSD/FreeBSD.git] / sys / boot / efi / libefi / efipart.c
1 /*-
2  * Copyright (c) 2010 Marcel Moolenaar
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 #include <sys/param.h>
31 #include <sys/time.h>
32 #include <stddef.h>
33 #include <stdarg.h>
34
35 #include <bootstrap.h>
36
37 #include <efi.h>
38 #include <efilib.h>
39 #include <efiprot.h>
40
41 static EFI_GUID blkio_guid = BLOCK_IO_PROTOCOL;
42
43 static int efipart_init(void);
44 static int efipart_strategy(void *, int, daddr_t, size_t, size_t, char *,
45     size_t *);
46 static int efipart_realstrategy(void *, int, daddr_t, size_t, size_t, char *,
47     size_t *);
48 static int efipart_open(struct open_file *, ...);
49 static int efipart_close(struct open_file *);
50 static void efipart_print(int);
51
52 struct devsw efipart_dev = {
53         .dv_name = "part",
54         .dv_type = DEVT_DISK,
55         .dv_init = efipart_init,
56         .dv_strategy = efipart_strategy,
57         .dv_open = efipart_open,
58         .dv_close = efipart_close,
59         .dv_ioctl = noioctl,
60         .dv_print = efipart_print,
61         .dv_cleanup = NULL
62 };
63
64 /*
65  * info structure to support bcache
66  */
67 struct pdinfo {
68         int     pd_unit;        /* unit number */
69         int     pd_open;        /* reference counter */
70         void    *pd_bcache;     /* buffer cache data */
71 };
72 static struct pdinfo *pdinfo;
73 static int npdinfo = 0;
74
75 #define PD(dev)         (pdinfo[(dev)->d_unit])
76
77 static int
78 efipart_init(void) 
79 {
80         EFI_BLOCK_IO *blkio;
81         EFI_DEVICE_PATH *devpath, *devpathcpy, *tmpdevpath, *node;
82         EFI_HANDLE *hin, *hout, *aliases, handle;
83         EFI_STATUS status;
84         UINTN sz;
85         u_int n, nin, nout, nrdisk;
86         int err;
87
88         sz = 0;
89         hin = NULL;
90         status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz, 0);
91         if (status == EFI_BUFFER_TOO_SMALL) {
92                 hin = (EFI_HANDLE *)malloc(sz * 3);
93                 status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz,
94                     hin);
95                 if (EFI_ERROR(status))
96                         free(hin);
97         }
98         if (EFI_ERROR(status))
99                 return (efi_status_to_errno(status));
100
101         /* Filter handles to only include FreeBSD partitions. */
102         nin = sz / sizeof(EFI_HANDLE);
103         hout = hin + nin;
104         aliases = hout + nin;
105         nout = 0;
106         nrdisk = 0;
107
108         bzero(aliases, nin * sizeof(EFI_HANDLE));
109         pdinfo = malloc(nin * sizeof(*pdinfo));
110         if (pdinfo == NULL)
111                 return (ENOMEM);
112
113         for (n = 0; n < nin; n++) {
114                 devpath = efi_lookup_devpath(hin[n]);
115                 if (devpath == NULL) {
116                         continue;
117                 }
118
119                 status = BS->HandleProtocol(hin[n], &blkio_guid,
120                     (void**)&blkio);
121                 if (EFI_ERROR(status))
122                         continue;
123                 if (!blkio->Media->LogicalPartition) {
124                         nrdisk++;
125                         continue;
126                 }
127
128                 /*
129                  * If we come across a logical partition of subtype CDROM
130                  * it doesn't refer to the CD filesystem itself, but rather
131                  * to any usable El Torito boot image on it. In this case
132                  * we try to find the parent device and add that instead as
133                  * that will be the CD filesystem.
134                  */
135                 node = efi_devpath_last_node(devpath);
136                 if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
137                     DevicePathSubType(node) == MEDIA_CDROM_DP) {
138                         devpathcpy = efi_devpath_trim(devpath);
139                         tmpdevpath = devpathcpy;
140                         status = BS->LocateDevicePath(&blkio_guid, &tmpdevpath,
141                             &handle);
142                         free(devpathcpy);
143                         if (EFI_ERROR(status))
144                                 continue;
145                         hout[nout] = handle;
146                         aliases[nout] = hin[n];
147                 } else
148                         hout[nout] = hin[n];
149                 nout++;
150                 pdinfo[npdinfo].pd_open = 0;
151                 pdinfo[npdinfo].pd_bcache = NULL;
152                 pdinfo[npdinfo].pd_unit = npdinfo;
153                 npdinfo++;
154         }
155
156         bcache_add_dev(npdinfo);
157         err = efi_register_handles(&efipart_dev, hout, aliases, nout);
158         free(hin);
159
160         if (nout == 0 && nrdisk > 0)
161                 printf("Found %d disk(s) but no logical partition\n", nrdisk);
162         return (err);
163 }
164
165 static void
166 efipart_print(int verbose)
167 {
168         char line[80];
169         EFI_BLOCK_IO *blkio;
170         EFI_HANDLE h;
171         EFI_STATUS status;
172         u_int unit;
173
174         pager_open();
175         for (unit = 0, h = efi_find_handle(&efipart_dev, 0);
176             h != NULL; h = efi_find_handle(&efipart_dev, ++unit)) {
177                 sprintf(line, "    %s%d:", efipart_dev.dv_name, unit);
178                 if (pager_output(line))
179                         break;
180
181                 status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio);
182                 if (!EFI_ERROR(status)) {
183                         sprintf(line, "    %llu blocks",
184                             (unsigned long long)(blkio->Media->LastBlock + 1));
185                         if (pager_output(line))
186                                 break;
187                         if (blkio->Media->RemovableMedia)
188                                 if (pager_output(" (removable)"))
189                                         break;
190                 }
191                 if (pager_output("\n"))
192                         break;
193         }
194         pager_close();
195 }
196
197 static int
198 efipart_open(struct open_file *f, ...)
199 {
200         va_list args;
201         struct devdesc *dev;
202         EFI_BLOCK_IO *blkio;
203         EFI_HANDLE h;
204         EFI_STATUS status;
205
206         va_start(args, f);
207         dev = va_arg(args, struct devdesc*);
208         va_end(args);
209
210         h = efi_find_handle(&efipart_dev, dev->d_unit);
211         if (h == NULL)
212                 return (EINVAL);
213
214         status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio);
215         if (EFI_ERROR(status))
216                 return (efi_status_to_errno(status));
217
218         if (!blkio->Media->MediaPresent)
219                 return (EAGAIN);
220
221         dev->d_opendata = blkio;
222         PD(dev).pd_open++;
223         if (PD(dev).pd_bcache == NULL)
224                 PD(dev).pd_bcache = bcache_allocate();
225         return (0);
226 }
227
228 static int
229 efipart_close(struct open_file *f)
230 {
231         struct devdesc *dev;
232
233         dev = (struct devdesc *)(f->f_devdata);
234         if (dev->d_opendata == NULL)
235                 return (EINVAL);
236
237         dev->d_opendata = NULL;
238         PD(dev).pd_open--;
239         if (PD(dev).pd_open == 0) {
240                 bcache_free(PD(dev).pd_bcache);
241                 PD(dev).pd_bcache = NULL;
242         }
243         return (0);
244 }
245
246 /*
247  * efipart_readwrite()
248  * Internal equivalent of efipart_strategy(), which operates on the
249  * media-native block size. This function expects all I/O requests
250  * to be within the media size and returns an error if such is not
251  * the case.
252  */
253 static int
254 efipart_readwrite(EFI_BLOCK_IO *blkio, int rw, daddr_t blk, daddr_t nblks,
255     char *buf)
256 {
257         EFI_STATUS status;
258
259         if (blkio == NULL)
260                 return (ENXIO);
261         if (blk < 0 || blk > blkio->Media->LastBlock)
262                 return (EIO);
263         if ((blk + nblks - 1) > blkio->Media->LastBlock)
264                 return (EIO);
265
266         switch (rw) {
267         case F_READ:
268                 status = blkio->ReadBlocks(blkio, blkio->Media->MediaId, blk,
269                     nblks * blkio->Media->BlockSize, buf);
270                 break;
271         case F_WRITE:
272                 if (blkio->Media->ReadOnly)
273                         return (EROFS);
274                 status = blkio->WriteBlocks(blkio, blkio->Media->MediaId, blk,
275                     nblks * blkio->Media->BlockSize, buf);
276                 break;
277         default:
278                 return (ENOSYS);
279         }
280
281         if (EFI_ERROR(status))
282                 printf("%s: rw=%d, status=%lu\n", __func__, rw, (u_long)status);
283         return (efi_status_to_errno(status));
284 }
285
286 static int
287 efipart_strategy(void *devdata, int rw, daddr_t blk, size_t offset,
288     size_t size, char *buf, size_t *rsize)
289 {
290         struct bcache_devdata bcd;
291         struct devdesc *dev;
292
293         dev = (struct devdesc *)devdata;
294         bcd.dv_strategy = efipart_realstrategy;
295         bcd.dv_devdata = devdata;
296         bcd.dv_cache = PD(dev).pd_bcache;
297         return (bcache_strategy(&bcd, rw, blk, offset, size,
298             buf, rsize));
299 }
300
301 static int
302 efipart_realstrategy(void *devdata, int rw, daddr_t blk, size_t offset,
303     size_t size, char *buf, size_t *rsize)
304 {
305         struct devdesc *dev = (struct devdesc *)devdata;
306         EFI_BLOCK_IO *blkio;
307         off_t off;
308         char *blkbuf;
309         size_t blkoff, blksz;
310         int error;
311
312         if (dev == NULL || blk < 0)
313                 return (EINVAL);
314
315         blkio = dev->d_opendata;
316         if (blkio == NULL)
317                 return (ENXIO);
318
319         if (size == 0 || (size % 512) != 0)
320                 return (EIO);
321
322         off = blk * 512;
323         /* make sure we don't read past disk end */
324         if ((off + size) / blkio->Media->BlockSize - 1 >
325             blkio->Media->LastBlock) {
326                 size = blkio->Media->LastBlock + 1 -
327                     off / blkio->Media->BlockSize;
328                 size = size * blkio->Media->BlockSize;
329         }
330
331         if (rsize != NULL)
332                 *rsize = size;
333
334         if ((size % blkio->Media->BlockSize == 0) &&
335             ((blk * 512) % blkio->Media->BlockSize == 0))
336                 return (efipart_readwrite(blkio, rw,
337                     blk * 512 / blkio->Media->BlockSize,
338                     size / blkio->Media->BlockSize, buf));
339
340         /*
341          * The block size of the media is not a multiple of I/O.
342          */
343         blkbuf = malloc(blkio->Media->BlockSize);
344         if (blkbuf == NULL)
345                 return (ENOMEM);
346
347         error = 0;
348         blk = off / blkio->Media->BlockSize;
349         blkoff = off % blkio->Media->BlockSize;
350         blksz = blkio->Media->BlockSize - blkoff;
351         while (size > 0) {
352                 error = efipart_readwrite(blkio, rw, blk, 1, blkbuf);
353                 if (error)
354                         break;
355                 if (size < blksz)
356                         blksz = size;
357                 bcopy(blkbuf + blkoff, buf, blksz);
358                 buf += blksz;
359                 size -= blksz;
360                 blk++;
361                 blkoff = 0;
362                 blksz = blkio->Media->BlockSize;
363         }
364
365         free(blkbuf);
366         return (error);
367 }