2 * Copyright (c) 2010 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
35 #include <bootstrap.h>
41 static EFI_GUID blkio_guid = BLOCK_IO_PROTOCOL;
43 static int efipart_init(void);
44 static int efipart_strategy(void *, int, daddr_t, size_t, size_t, char *,
46 static int efipart_realstrategy(void *, int, daddr_t, size_t, size_t, char *,
48 static int efipart_open(struct open_file *, ...);
49 static int efipart_close(struct open_file *);
50 static void efipart_print(int);
52 struct devsw efipart_dev = {
55 .dv_init = efipart_init,
56 .dv_strategy = efipart_strategy,
57 .dv_open = efipart_open,
58 .dv_close = efipart_close,
60 .dv_print = efipart_print,
65 * info structure to support bcache
68 int pd_unit; /* unit number */
69 int pd_open; /* reference counter */
70 void *pd_bcache; /* buffer cache data */
72 static struct pdinfo *pdinfo;
73 static int npdinfo = 0;
75 #define PD(dev) (pdinfo[(dev)->d_unit])
81 EFI_DEVICE_PATH *devpath, *devpathcpy, *tmpdevpath, *node;
82 EFI_HANDLE *hin, *hout, *aliases, handle;
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,
95 if (EFI_ERROR(status))
98 if (EFI_ERROR(status))
99 return (efi_status_to_errno(status));
101 /* Filter handles to only include FreeBSD partitions. */
102 nin = sz / sizeof(EFI_HANDLE);
104 aliases = hout + nin;
107 bzero(aliases, nin * sizeof(EFI_HANDLE));
108 pdinfo = malloc(nin * sizeof(*pdinfo));
112 for (n = 0; n < nin; n++) {
113 devpath = efi_lookup_devpath(hin[n]);
114 if (devpath == NULL) {
118 status = BS->HandleProtocol(hin[n], &blkio_guid,
120 if (EFI_ERROR(status))
122 if (!blkio->Media->LogicalPartition)
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.
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,
140 if (EFI_ERROR(status))
143 aliases[nout] = hin[n];
147 pdinfo[npdinfo].pd_open = 0;
148 pdinfo[npdinfo].pd_bcache = NULL;
149 pdinfo[npdinfo].pd_unit = npdinfo;
153 bcache_add_dev(npdinfo);
154 err = efi_register_handles(&efipart_dev, hout, aliases, nout);
160 efipart_print(int verbose)
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))
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))
181 if (blkio->Media->RemovableMedia)
182 if (pager_output(" (removable)"))
185 if (pager_output("\n"))
192 efipart_open(struct open_file *f, ...)
201 dev = va_arg(args, struct devdesc*);
204 h = efi_find_handle(&efipart_dev, dev->d_unit);
208 status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio);
209 if (EFI_ERROR(status))
210 return (efi_status_to_errno(status));
212 if (!blkio->Media->MediaPresent)
215 dev->d_opendata = blkio;
217 if (PD(dev).pd_bcache == NULL)
218 PD(dev).pd_bcache = bcache_allocate();
223 efipart_close(struct open_file *f)
227 dev = (struct devdesc *)(f->f_devdata);
228 if (dev->d_opendata == NULL)
231 dev->d_opendata = NULL;
233 if (PD(dev).pd_open == 0) {
234 bcache_free(PD(dev).pd_bcache);
235 PD(dev).pd_bcache = NULL;
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
248 efipart_readwrite(EFI_BLOCK_IO *blkio, int rw, daddr_t blk, daddr_t nblks,
255 if (blk < 0 || blk > blkio->Media->LastBlock)
257 if ((blk + nblks - 1) > blkio->Media->LastBlock)
262 status = blkio->ReadBlocks(blkio, blkio->Media->MediaId, blk,
263 nblks * blkio->Media->BlockSize, buf);
266 if (blkio->Media->ReadOnly)
268 status = blkio->WriteBlocks(blkio, blkio->Media->MediaId, blk,
269 nblks * blkio->Media->BlockSize, buf);
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));
281 efipart_strategy(void *devdata, int rw, daddr_t blk, size_t offset,
282 size_t size, char *buf, size_t *rsize)
284 struct bcache_devdata bcd;
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,
296 efipart_realstrategy(void *devdata, int rw, daddr_t blk, size_t offset,
297 size_t size, char *buf, size_t *rsize)
299 struct devdesc *dev = (struct devdesc *)devdata;
303 size_t blkoff, blksz;
306 if (dev == NULL || blk < 0)
309 blkio = dev->d_opendata;
313 if (size == 0 || (size % 512) != 0)
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;
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));
335 * The block size of the media is not a multiple of I/O.
337 blkbuf = malloc(blkio->Media->BlockSize);
342 blk = off / blkio->Media->BlockSize;
343 blkoff = off % blkio->Media->BlockSize;
344 blksz = blkio->Media->BlockSize - blkoff;
346 error = efipart_readwrite(blkio, rw, blk, 1, blkbuf);
351 bcopy(blkbuf + blkoff, buf, blksz);
356 blksz = blkio->Media->BlockSize;