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