]> CyberLeo.Net >> Repos - FreeBSD/releng/10.3.git/blob - sys/boot/efi/libefi/efipart.c
- Copy stable/10@296371 to releng/10.3 in preparation for 10.3-RC1
[FreeBSD/releng/10.3.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 static EFI_GUID devpath_guid = DEVICE_PATH_PROTOCOL;
43
44 static int efipart_init(void);
45 static int efipart_strategy(void *, int, daddr_t, size_t, char *, size_t *);
46 static int efipart_open(struct open_file *, ...);
47 static int efipart_close(struct open_file *);
48 static void efipart_print(int);
49
50 struct devsw efipart_dev = {
51         .dv_name = "part",
52         .dv_type = DEVT_DISK,
53         .dv_init = efipart_init,
54         .dv_strategy = efipart_strategy,
55         .dv_open = efipart_open,
56         .dv_close = efipart_close,
57         .dv_ioctl = noioctl,
58         .dv_print = efipart_print,
59         .dv_cleanup = NULL
60 };
61
62 static int
63 efipart_init(void) 
64 {
65         EFI_BLOCK_IO *blkio;
66         EFI_DEVICE_PATH *devpath, *devpathcpy, *tmpdevpath, *node;
67         EFI_HANDLE *hin, *hout, *aliases, handle;
68         EFI_STATUS status;
69         UINTN sz;
70         u_int n, nin, nout;
71         int err;
72         size_t devpathlen;
73
74         sz = 0;
75         hin = NULL;
76         status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz, 0);
77         if (status == EFI_BUFFER_TOO_SMALL) {
78                 hin = (EFI_HANDLE *)malloc(sz * 3);
79                 status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz,
80                     hin);
81                 if (EFI_ERROR(status))
82                         free(hin);
83         }
84         if (EFI_ERROR(status))
85                 return (efi_status_to_errno(status));
86
87         /* Filter handles to only include FreeBSD partitions. */
88         nin = sz / sizeof(EFI_HANDLE);
89         hout = hin + nin;
90         aliases = hout + nin;
91         nout = 0;
92
93         bzero(aliases, nin * sizeof(EFI_HANDLE));
94
95         for (n = 0; n < nin; n++) {
96                 status = BS->HandleProtocol(hin[n], &devpath_guid,
97                     (void **)&devpath);
98                 if (EFI_ERROR(status)) {
99                         continue;
100                 }
101
102                 node = devpath;
103                 devpathlen = DevicePathNodeLength(node);
104                 while (!IsDevicePathEnd(NextDevicePathNode(node))) {
105                         node = NextDevicePathNode(node);
106                         devpathlen += DevicePathNodeLength(node);
107                 }
108                 devpathlen += DevicePathNodeLength(NextDevicePathNode(node));
109
110                 status = BS->HandleProtocol(hin[n], &blkio_guid,
111                     (void**)&blkio);
112                 if (EFI_ERROR(status))
113                         continue;
114                 if (!blkio->Media->LogicalPartition)
115                         continue;
116
117                 /*
118                  * If we come across a logical partition of subtype CDROM
119                  * it doesn't refer to the CD filesystem itself, but rather
120                  * to any usable El Torito boot image on it. In this case
121                  * we try to find the parent device and add that instead as
122                  * that will be the CD filesystem.
123                  */
124                 if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
125                     DevicePathSubType(node) == MEDIA_CDROM_DP) {
126                         devpathcpy = malloc(devpathlen);
127                         memcpy(devpathcpy, devpath, devpathlen);
128                         node = devpathcpy;
129                         while (!IsDevicePathEnd(NextDevicePathNode(node)))
130                                 node = NextDevicePathNode(node);
131                         SetDevicePathEndNode(node);
132                         tmpdevpath = devpathcpy;
133                         status = BS->LocateDevicePath(&blkio_guid, &tmpdevpath,
134                             &handle);
135                         free(devpathcpy);
136                         if (EFI_ERROR(status))
137                                 continue;
138                         hout[nout] = handle;
139                         aliases[nout] = hin[n];
140                 } else
141                         hout[nout] = hin[n];
142                 nout++;
143         }
144
145         err = efi_register_handles(&efipart_dev, hout, aliases, nout);
146         free(hin);
147         return (err);
148 }
149
150 static void
151 efipart_print(int verbose)
152 {
153         char line[80];
154         EFI_BLOCK_IO *blkio;
155         EFI_HANDLE h;
156         EFI_STATUS status;
157         u_int unit;
158
159         for (unit = 0, h = efi_find_handle(&efipart_dev, 0);
160             h != NULL; h = efi_find_handle(&efipart_dev, ++unit)) {
161                 sprintf(line, "    %s%d:", efipart_dev.dv_name, unit);
162                 pager_output(line);
163
164                 status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio);
165                 if (!EFI_ERROR(status)) {
166                         sprintf(line, "    %llu blocks",
167                             (unsigned long long)(blkio->Media->LastBlock + 1));
168                         pager_output(line);
169                         if (blkio->Media->RemovableMedia)
170                                 pager_output(" (removable)");
171                 }
172                 pager_output("\n");
173         }
174 }
175
176 static int 
177 efipart_open(struct open_file *f, ...)
178 {
179         va_list args;
180         struct devdesc *dev;
181         EFI_BLOCK_IO *blkio;
182         EFI_HANDLE h;
183         EFI_STATUS status;
184
185         va_start(args, f);
186         dev = va_arg(args, struct devdesc*);
187         va_end(args);
188
189         h = efi_find_handle(&efipart_dev, dev->d_unit);
190         if (h == NULL)
191                 return (EINVAL);
192
193         status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio);
194         if (EFI_ERROR(status))
195                 return (efi_status_to_errno(status));
196
197         if (!blkio->Media->MediaPresent)
198                 return (EAGAIN);
199
200         dev->d_opendata = blkio;
201         return (0);
202 }
203
204 static int 
205 efipart_close(struct open_file *f)
206 {
207         struct devdesc *dev;
208
209         dev = (struct devdesc *)(f->f_devdata);
210         if (dev->d_opendata == NULL)
211                 return (EINVAL);
212
213         dev->d_opendata = NULL;
214         return (0);
215 }
216
217 /*
218  * efipart_readwrite()
219  * Internal equivalent of efipart_strategy(), which operates on the
220  * media-native block size. This function expects all I/O requests
221  * to be within the media size and returns an error if such is not
222  * the case.
223  */
224 static int
225 efipart_readwrite(EFI_BLOCK_IO *blkio, int rw, daddr_t blk, daddr_t nblks,
226     char *buf)
227 {
228         EFI_STATUS status;
229
230         if (blkio == NULL)
231                 return (ENXIO);
232         if (blk < 0 || blk > blkio->Media->LastBlock)
233                 return (EIO);
234         if ((blk + nblks - 1) > blkio->Media->LastBlock)
235                 return (EIO);
236
237         switch (rw) {
238         case F_READ:
239                 status = blkio->ReadBlocks(blkio, blkio->Media->MediaId, blk,
240                     nblks * blkio->Media->BlockSize, buf);
241                 break;
242         case F_WRITE:
243                 if (blkio->Media->ReadOnly)
244                         return (EROFS);
245                 status = blkio->WriteBlocks(blkio, blkio->Media->MediaId, blk,
246                     nblks * blkio->Media->BlockSize, buf);
247                 break;
248         default:
249                 return (ENOSYS);
250         }
251
252         if (EFI_ERROR(status))
253                 printf("%s: rw=%d, status=%lu\n", __func__, rw, (u_long)status);
254         return (efi_status_to_errno(status));
255 }
256
257 static int 
258 efipart_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf,
259     size_t *rsize)
260 {
261         struct devdesc *dev = (struct devdesc *)devdata;
262         EFI_BLOCK_IO *blkio;
263         off_t off;
264         char *blkbuf;
265         size_t blkoff, blksz;
266         int error;
267
268         if (dev == NULL || blk < 0)
269                 return (EINVAL);
270
271         blkio = dev->d_opendata;
272         if (blkio == NULL)
273                 return (ENXIO);
274
275         if (size == 0 || (size % 512) != 0)
276                 return (EIO);
277
278         if (rsize != NULL)
279                 *rsize = size;
280
281         if (blkio->Media->BlockSize == 512)
282                 return (efipart_readwrite(blkio, rw, blk, size / 512, buf));
283
284         /*
285          * The block size of the media is not 512B per sector.
286          */
287         blkbuf = malloc(blkio->Media->BlockSize);
288         if (blkbuf == NULL)
289                 return (ENOMEM);
290
291         error = 0;
292         off = blk * 512;
293         blk = off / blkio->Media->BlockSize;
294         blkoff = off % blkio->Media->BlockSize;
295         blksz = blkio->Media->BlockSize - blkoff;
296         while (size > 0) {
297                 error = efipart_readwrite(blkio, rw, blk, 1, blkbuf);
298                 if (error)
299                         break;
300                 if (size < blksz)
301                         blksz = size;
302                 bcopy(blkbuf + blkoff, buf, blksz);
303                 buf += blksz;
304                 size -= blksz;
305                 blk++;
306                 blkoff = 0;
307                 blksz = blkio->Media->BlockSize;
308         }
309
310         free(blkbuf);
311         return (error);
312 }