]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/boot/efi/libefi/efipart.c
Import NetBSD's blacklist source from vendor tree
[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;
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
107         bzero(aliases, nin * sizeof(EFI_HANDLE));
108         pdinfo = malloc(nin * sizeof(*pdinfo));
109         if (pdinfo == NULL)
110                 return (ENOMEM);
111
112         for (n = 0; n < nin; n++) {
113                 devpath = efi_lookup_devpath(hin[n]);
114                 if (devpath == NULL) {
115                         continue;
116                 }
117
118                 status = BS->HandleProtocol(hin[n], &blkio_guid,
119                     (void**)&blkio);
120                 if (EFI_ERROR(status))
121                         continue;
122                 if (!blkio->Media->LogicalPartition)
123                         continue;
124
125                 /*
126                  * If we come across a logical partition of subtype CDROM
127                  * it doesn't refer to the CD filesystem itself, but rather
128                  * to any usable El Torito boot image on it. In this case
129                  * we try to find the parent device and add that instead as
130                  * that will be the CD filesystem.
131                  */
132                 node = efi_devpath_last_node(devpath);
133                 if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
134                     DevicePathSubType(node) == MEDIA_CDROM_DP) {
135                         devpathcpy = efi_devpath_trim(devpath);
136                         tmpdevpath = devpathcpy;
137                         status = BS->LocateDevicePath(&blkio_guid, &tmpdevpath,
138                             &handle);
139                         free(devpathcpy);
140                         if (EFI_ERROR(status))
141                                 continue;
142                         hout[nout] = handle;
143                         aliases[nout] = hin[n];
144                 } else
145                         hout[nout] = hin[n];
146                 nout++;
147                 pdinfo[npdinfo].pd_open = 0;
148                 pdinfo[npdinfo].pd_bcache = NULL;
149                 pdinfo[npdinfo].pd_unit = npdinfo;
150                 npdinfo++;
151         }
152
153         bcache_add_dev(npdinfo);
154         err = efi_register_handles(&efipart_dev, hout, aliases, nout);
155         free(hin);
156         return (err);
157 }
158
159 static void
160 efipart_print(int verbose)
161 {
162         char line[80];
163         EFI_BLOCK_IO *blkio;
164         EFI_HANDLE h;
165         EFI_STATUS status;
166         u_int unit;
167
168         pager_open();
169         for (unit = 0, h = efi_find_handle(&efipart_dev, 0);
170             h != NULL; h = efi_find_handle(&efipart_dev, ++unit)) {
171                 sprintf(line, "    %s%d:", efipart_dev.dv_name, unit);
172                 if (pager_output(line))
173                         break;
174
175                 status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio);
176                 if (!EFI_ERROR(status)) {
177                         sprintf(line, "    %llu blocks",
178                             (unsigned long long)(blkio->Media->LastBlock + 1));
179                         if (pager_output(line))
180                                 break;
181                         if (blkio->Media->RemovableMedia)
182                                 if (pager_output(" (removable)"))
183                                         break;
184                 }
185                 if (pager_output("\n"))
186                         break;
187         }
188         pager_close();
189 }
190
191 static int
192 efipart_open(struct open_file *f, ...)
193 {
194         va_list args;
195         struct devdesc *dev;
196         EFI_BLOCK_IO *blkio;
197         EFI_HANDLE h;
198         EFI_STATUS status;
199
200         va_start(args, f);
201         dev = va_arg(args, struct devdesc*);
202         va_end(args);
203
204         h = efi_find_handle(&efipart_dev, dev->d_unit);
205         if (h == NULL)
206                 return (EINVAL);
207
208         status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio);
209         if (EFI_ERROR(status))
210                 return (efi_status_to_errno(status));
211
212         if (!blkio->Media->MediaPresent)
213                 return (EAGAIN);
214
215         dev->d_opendata = blkio;
216         PD(dev).pd_open++;
217         if (PD(dev).pd_bcache == NULL)
218                 PD(dev).pd_bcache = bcache_allocate();
219         return (0);
220 }
221
222 static int
223 efipart_close(struct open_file *f)
224 {
225         struct devdesc *dev;
226
227         dev = (struct devdesc *)(f->f_devdata);
228         if (dev->d_opendata == NULL)
229                 return (EINVAL);
230
231         dev->d_opendata = NULL;
232         PD(dev).pd_open--;
233         if (PD(dev).pd_open == 0) {
234                 bcache_free(PD(dev).pd_bcache);
235                 PD(dev).pd_bcache = NULL;
236         }
237         return (0);
238 }
239
240 /*
241  * efipart_readwrite()
242  * Internal equivalent of efipart_strategy(), which operates on the
243  * media-native block size. This function expects all I/O requests
244  * to be within the media size and returns an error if such is not
245  * the case.
246  */
247 static int
248 efipart_readwrite(EFI_BLOCK_IO *blkio, int rw, daddr_t blk, daddr_t nblks,
249     char *buf)
250 {
251         EFI_STATUS status;
252
253         if (blkio == NULL)
254                 return (ENXIO);
255         if (blk < 0 || blk > blkio->Media->LastBlock)
256                 return (EIO);
257         if ((blk + nblks - 1) > blkio->Media->LastBlock)
258                 return (EIO);
259
260         switch (rw) {
261         case F_READ:
262                 status = blkio->ReadBlocks(blkio, blkio->Media->MediaId, blk,
263                     nblks * blkio->Media->BlockSize, buf);
264                 break;
265         case F_WRITE:
266                 if (blkio->Media->ReadOnly)
267                         return (EROFS);
268                 status = blkio->WriteBlocks(blkio, blkio->Media->MediaId, blk,
269                     nblks * blkio->Media->BlockSize, buf);
270                 break;
271         default:
272                 return (ENOSYS);
273         }
274
275         if (EFI_ERROR(status))
276                 printf("%s: rw=%d, status=%lu\n", __func__, rw, (u_long)status);
277         return (efi_status_to_errno(status));
278 }
279
280 static int
281 efipart_strategy(void *devdata, int rw, daddr_t blk, size_t offset,
282     size_t size, char *buf, size_t *rsize)
283 {
284         struct bcache_devdata bcd;
285         struct devdesc *dev;
286
287         dev = (struct devdesc *)devdata;
288         bcd.dv_strategy = efipart_realstrategy;
289         bcd.dv_devdata = devdata;
290         bcd.dv_cache = PD(dev).pd_bcache;
291         return (bcache_strategy(&bcd, rw, blk, offset, size,
292             buf, rsize));
293 }
294
295 static int
296 efipart_realstrategy(void *devdata, int rw, daddr_t blk, size_t offset,
297     size_t size, char *buf, size_t *rsize)
298 {
299         struct devdesc *dev = (struct devdesc *)devdata;
300         EFI_BLOCK_IO *blkio;
301         off_t off;
302         char *blkbuf;
303         size_t blkoff, blksz;
304         int error;
305
306         if (dev == NULL || blk < 0)
307                 return (EINVAL);
308
309         blkio = dev->d_opendata;
310         if (blkio == NULL)
311                 return (ENXIO);
312
313         if (size == 0 || (size % 512) != 0)
314                 return (EIO);
315
316         off = blk * 512;
317         /* make sure we don't read past disk end */
318         if ((off + size) / blkio->Media->BlockSize - 1 >
319             blkio->Media->LastBlock) {
320                 size = blkio->Media->LastBlock + 1 -
321                     off / blkio->Media->BlockSize;
322                 size = size * blkio->Media->BlockSize;
323         }
324
325         if (rsize != NULL)
326                 *rsize = size;
327
328         if ((size % blkio->Media->BlockSize == 0) &&
329             ((blk * 512) % blkio->Media->BlockSize == 0))
330                 return (efipart_readwrite(blkio, rw,
331                     blk * 512 / blkio->Media->BlockSize,
332                     size / blkio->Media->BlockSize, buf));
333
334         /*
335          * The block size of the media is not a multiple of I/O.
336          */
337         blkbuf = malloc(blkio->Media->BlockSize);
338         if (blkbuf == NULL)
339                 return (ENOMEM);
340
341         error = 0;
342         blk = off / blkio->Media->BlockSize;
343         blkoff = off % blkio->Media->BlockSize;
344         blksz = blkio->Media->BlockSize - blkoff;
345         while (size > 0) {
346                 error = efipart_readwrite(blkio, rw, blk, 1, blkbuf);
347                 if (error)
348                         break;
349                 if (size < blksz)
350                         blksz = size;
351                 bcopy(blkbuf + blkoff, buf, blksz);
352                 buf += blksz;
353                 size -= blksz;
354                 blk++;
355                 blkoff = 0;
356                 blksz = blkio->Media->BlockSize;
357         }
358
359         free(blkbuf);
360         return (error);
361 }