]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/boot/efi/libefi/efipart.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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, char *, size_t *);
45 static int efipart_open(struct open_file *, ...);
46 static int efipart_close(struct open_file *);
47 static void efipart_print(int);
48
49 struct devsw efipart_dev = {
50         .dv_name = "part",
51         .dv_type = DEVT_DISK,
52         .dv_init = efipart_init,
53         .dv_strategy = efipart_strategy,
54         .dv_open = efipart_open,
55         .dv_close = efipart_close,
56         .dv_ioctl = noioctl,
57         .dv_print = efipart_print,
58         .dv_cleanup = NULL
59 };
60
61 static int
62 efipart_init(void) 
63 {
64         EFI_BLOCK_IO *blkio;
65         EFI_HANDLE *hin, *hout;
66         EFI_STATUS status;
67         UINTN sz;
68         u_int n, nin, nout;
69         int err;
70
71         sz = 0;
72         status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz, 0);
73         if (status == EFI_BUFFER_TOO_SMALL) {
74                 hin = (EFI_HANDLE *)malloc(sz * 2);
75                 status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz,
76                     hin);
77                 if (EFI_ERROR(status))
78                         free(hin);
79         }
80         if (EFI_ERROR(status))
81                 return (efi_status_to_errno(status));
82
83         /* Filter handles to only include FreeBSD partitions. */
84         nin = sz / sizeof(EFI_HANDLE);
85         hout = hin + nin;
86         nout = 0;
87
88         for (n = 0; n < nin; n++) {
89                 status = BS->HandleProtocol(hin[n], &blkio_guid, &blkio);
90                 if (EFI_ERROR(status))
91                         continue;
92                 if (!blkio->Media->LogicalPartition)
93                         continue;
94                 hout[nout] = hin[n];
95                 nout++;
96         }
97
98         err = efi_register_handles(&efipart_dev, hout, nout);
99         free(hin);
100         return (err);
101 }
102
103 static void
104 efipart_print(int verbose)
105 {
106         char line[80];
107         EFI_BLOCK_IO *blkio;
108         EFI_HANDLE h;
109         EFI_STATUS status;
110         u_int unit;
111
112         for (unit = 0, h = efi_find_handle(&efipart_dev, 0);
113             h != NULL; h = efi_find_handle(&efipart_dev, ++unit)) {
114                 sprintf(line, "    %s%d:", efipart_dev.dv_name, unit);
115                 pager_output(line);
116
117                 status = BS->HandleProtocol(h, &blkio_guid, &blkio);
118                 if (!EFI_ERROR(status)) {
119                         sprintf(line, "    %llu blocks",
120                             (unsigned long long)(blkio->Media->LastBlock + 1));
121                         pager_output(line);
122                         if (blkio->Media->RemovableMedia)
123                                 pager_output(" (removable)");
124                 }
125                 pager_output("\n");
126         }
127 }
128
129 static int 
130 efipart_open(struct open_file *f, ...)
131 {
132         va_list args;
133         struct devdesc *dev;
134         EFI_BLOCK_IO *blkio;
135         EFI_HANDLE h;
136         EFI_STATUS status;
137
138         va_start(args, f);
139         dev = va_arg(args, struct devdesc*);
140         va_end(args);
141
142         h = efi_find_handle(&efipart_dev, dev->d_unit);
143         if (h == NULL)
144                 return (EINVAL);
145
146         status = BS->HandleProtocol(h, &blkio_guid, &blkio);
147         if (EFI_ERROR(status))
148                 return (efi_status_to_errno(status));
149
150         if (!blkio->Media->MediaPresent)
151                 return (EAGAIN);
152
153         dev->d_opendata = blkio;
154         return (0);
155 }
156
157 static int 
158 efipart_close(struct open_file *f)
159 {
160         struct devdesc *dev;
161
162         dev = (struct devdesc *)(f->f_devdata);
163         if (dev->d_opendata == NULL)
164                 return (EINVAL);
165
166         dev->d_opendata = NULL;
167         return (0);
168 }
169
170 /*
171  * efipart_readwrite()
172  * Internal equivalent of efipart_strategy(), which operates on the
173  * media-native block size. This function expects all I/O requests
174  * to be within the media size and returns an error if such is not
175  * the case.
176  */
177 static int
178 efipart_readwrite(EFI_BLOCK_IO *blkio, int rw, daddr_t blk, daddr_t nblks,
179     char *buf)
180 {
181         EFI_STATUS status;
182
183         if (blkio == NULL)
184                 return (ENXIO);
185         if (blk < 0 || blk > blkio->Media->LastBlock)
186                 return (EIO);
187         if ((blk + nblks - 1) > blkio->Media->LastBlock)
188                 return (EIO);
189
190         switch (rw) {
191         case F_READ:
192                 status = blkio->ReadBlocks(blkio, blkio->Media->MediaId, blk,
193                     nblks * blkio->Media->BlockSize, buf);
194                 break;
195         case F_WRITE:
196                 if (blkio->Media->ReadOnly)
197                         return (EROFS);
198                 status = blkio->WriteBlocks(blkio, blkio->Media->MediaId, blk,
199                     nblks * blkio->Media->BlockSize, buf);
200                 break;
201         default:
202                 return (ENOSYS);
203         }
204
205         if (EFI_ERROR(status))
206                 printf("%s: rw=%d, status=%lu\n", __func__, rw, status);
207         return (efi_status_to_errno(status));
208 }
209
210 static int 
211 efipart_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf,
212     size_t *rsize)
213 {
214         struct devdesc *dev = (struct devdesc *)devdata;
215         EFI_BLOCK_IO *blkio;
216         off_t off;
217         char *blkbuf;
218         size_t blkoff, blksz;
219         int error;
220
221         if (dev == NULL || blk < 0)
222                 return (EINVAL);
223
224         blkio = dev->d_opendata;
225         if (blkio == NULL)
226                 return (ENXIO);
227
228         if (size == 0 || (size % 512) != 0)
229                 return (EIO);
230
231         if (rsize != NULL)
232                 *rsize = size;
233
234         if (blkio->Media->BlockSize == 512)
235                 return (efipart_readwrite(blkio, rw, blk, size / 512, buf));
236
237         /*
238          * The block size of the media is not 512B per sector.
239          */
240         blkbuf = malloc(blkio->Media->BlockSize);
241         if (blkbuf == NULL)
242                 return (ENOMEM);
243
244         error = 0;
245         off = blk * 512;
246         blk = off / blkio->Media->BlockSize;
247         blkoff = off % blkio->Media->BlockSize;
248         blksz = blkio->Media->BlockSize - blkoff;
249         while (size > 0) {
250                 error = efipart_readwrite(blkio, rw, blk, 1, blkbuf);
251                 if (error)
252                         break;
253                 if (size < blksz)
254                         blksz = size;
255                 bcopy(blkbuf + blkoff, buf, blksz);
256                 buf += blksz;
257                 size -= blksz;
258                 blk++;
259                 blkoff = 0;
260                 blksz = blkio->Media->BlockSize;
261         }
262
263         free(blkbuf);
264         return (error);
265 }