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;
85 u_int n, nin, nout, nrdisk;
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;
108 bzero(aliases, nin * sizeof(EFI_HANDLE));
109 pdinfo = malloc(nin * sizeof(*pdinfo));
113 for (n = 0; n < nin; n++) {
114 devpath = efi_lookup_devpath(hin[n]);
115 if (devpath == NULL) {
119 status = BS->HandleProtocol(hin[n], &blkio_guid,
121 if (EFI_ERROR(status))
123 if (!blkio->Media->LogicalPartition) {
129 * If we come across a logical partition of subtype CDROM
130 * it doesn't refer to the CD filesystem itself, but rather
131 * to any usable El Torito boot image on it. In this case
132 * we try to find the parent device and add that instead as
133 * that will be the CD filesystem.
135 node = efi_devpath_last_node(devpath);
136 if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
137 DevicePathSubType(node) == MEDIA_CDROM_DP) {
138 devpathcpy = efi_devpath_trim(devpath);
139 tmpdevpath = devpathcpy;
140 status = BS->LocateDevicePath(&blkio_guid, &tmpdevpath,
143 if (EFI_ERROR(status))
146 aliases[nout] = hin[n];
150 pdinfo[npdinfo].pd_open = 0;
151 pdinfo[npdinfo].pd_bcache = NULL;
152 pdinfo[npdinfo].pd_unit = npdinfo;
156 bcache_add_dev(npdinfo);
157 err = efi_register_handles(&efipart_dev, hout, aliases, nout);
160 if (nout == 0 && nrdisk > 0)
161 printf("Found %d disk(s) but no logical partition\n", nrdisk);
166 efipart_print(int verbose)
175 for (unit = 0, h = efi_find_handle(&efipart_dev, 0);
176 h != NULL; h = efi_find_handle(&efipart_dev, ++unit)) {
177 sprintf(line, " %s%d:", efipart_dev.dv_name, unit);
178 if (pager_output(line))
181 status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio);
182 if (!EFI_ERROR(status)) {
183 sprintf(line, " %llu blocks",
184 (unsigned long long)(blkio->Media->LastBlock + 1));
185 if (pager_output(line))
187 if (blkio->Media->RemovableMedia)
188 if (pager_output(" (removable)"))
191 if (pager_output("\n"))
198 efipart_open(struct open_file *f, ...)
207 dev = va_arg(args, struct devdesc*);
210 h = efi_find_handle(&efipart_dev, dev->d_unit);
214 status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio);
215 if (EFI_ERROR(status))
216 return (efi_status_to_errno(status));
218 if (!blkio->Media->MediaPresent)
221 dev->d_opendata = blkio;
223 if (PD(dev).pd_bcache == NULL)
224 PD(dev).pd_bcache = bcache_allocate();
229 efipart_close(struct open_file *f)
233 dev = (struct devdesc *)(f->f_devdata);
234 if (dev->d_opendata == NULL)
237 dev->d_opendata = NULL;
239 if (PD(dev).pd_open == 0) {
240 bcache_free(PD(dev).pd_bcache);
241 PD(dev).pd_bcache = NULL;
247 * efipart_readwrite()
248 * Internal equivalent of efipart_strategy(), which operates on the
249 * media-native block size. This function expects all I/O requests
250 * to be within the media size and returns an error if such is not
254 efipart_readwrite(EFI_BLOCK_IO *blkio, int rw, daddr_t blk, daddr_t nblks,
261 if (blk < 0 || blk > blkio->Media->LastBlock)
263 if ((blk + nblks - 1) > blkio->Media->LastBlock)
268 status = blkio->ReadBlocks(blkio, blkio->Media->MediaId, blk,
269 nblks * blkio->Media->BlockSize, buf);
272 if (blkio->Media->ReadOnly)
274 status = blkio->WriteBlocks(blkio, blkio->Media->MediaId, blk,
275 nblks * blkio->Media->BlockSize, buf);
281 if (EFI_ERROR(status))
282 printf("%s: rw=%d, status=%lu\n", __func__, rw, (u_long)status);
283 return (efi_status_to_errno(status));
287 efipart_strategy(void *devdata, int rw, daddr_t blk, size_t offset,
288 size_t size, char *buf, size_t *rsize)
290 struct bcache_devdata bcd;
293 dev = (struct devdesc *)devdata;
294 bcd.dv_strategy = efipart_realstrategy;
295 bcd.dv_devdata = devdata;
296 bcd.dv_cache = PD(dev).pd_bcache;
297 return (bcache_strategy(&bcd, rw, blk, offset, size,
302 efipart_realstrategy(void *devdata, int rw, daddr_t blk, size_t offset,
303 size_t size, char *buf, size_t *rsize)
305 struct devdesc *dev = (struct devdesc *)devdata;
309 size_t blkoff, blksz;
312 if (dev == NULL || blk < 0)
315 blkio = dev->d_opendata;
319 if (size == 0 || (size % 512) != 0)
323 /* make sure we don't read past disk end */
324 if ((off + size) / blkio->Media->BlockSize - 1 >
325 blkio->Media->LastBlock) {
326 size = blkio->Media->LastBlock + 1 -
327 off / blkio->Media->BlockSize;
328 size = size * blkio->Media->BlockSize;
334 if ((size % blkio->Media->BlockSize == 0) &&
335 ((blk * 512) % blkio->Media->BlockSize == 0))
336 return (efipart_readwrite(blkio, rw,
337 blk * 512 / blkio->Media->BlockSize,
338 size / blkio->Media->BlockSize, buf));
341 * The block size of the media is not a multiple of I/O.
343 blkbuf = malloc(blkio->Media->BlockSize);
348 blk = off / blkio->Media->BlockSize;
349 blkoff = off % blkio->Media->BlockSize;
350 blksz = blkio->Media->BlockSize - blkoff;
352 error = efipart_readwrite(blkio, rw, blk, 1, blkbuf);
357 bcopy(blkbuf + blkoff, buf, blksz);
362 blksz = blkio->Media->BlockSize;