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;
42 static EFI_GUID devpath_guid = DEVICE_PATH_PROTOCOL;
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);
50 struct devsw efipart_dev = {
53 .dv_init = efipart_init,
54 .dv_strategy = efipart_strategy,
55 .dv_open = efipart_open,
56 .dv_close = efipart_close,
58 .dv_print = efipart_print,
66 EFI_DEVICE_PATH *devpath, *devpathcpy, *tmpdevpath, *node;
67 EFI_HANDLE *hin, *hout, *aliases, handle;
77 status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz, 0);
78 if (status == EFI_BUFFER_TOO_SMALL) {
79 hin = (EFI_HANDLE *)malloc(sz * 3);
80 status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz,
82 if (EFI_ERROR(status))
85 if (EFI_ERROR(status))
86 return (efi_status_to_errno(status));
88 /* Filter handles to only include FreeBSD partitions. */
89 nin = sz / sizeof(EFI_HANDLE);
94 bzero(aliases, nin * sizeof(EFI_HANDLE));
96 for (n = 0; n < nin; n++) {
97 status = BS->HandleProtocol(hin[n], &devpath_guid,
99 if (EFI_ERROR(status)) {
104 devpathlen = DevicePathNodeLength(node);
105 while (!IsDevicePathEnd(NextDevicePathNode(node))) {
106 node = NextDevicePathNode(node);
107 devpathlen += DevicePathNodeLength(node);
109 devpathlen += DevicePathNodeLength(NextDevicePathNode(node));
111 status = BS->HandleProtocol(hin[n], &blkio_guid,
113 if (EFI_ERROR(status))
115 if (!blkio->Media->LogicalPartition)
119 * If we come across a logical partition of subtype CDROM
120 * it doesn't refer to the CD filesystem itself, but rather
121 * to any usable El Torito boot image on it. In this case
122 * we try to find the parent device and add that instead as
123 * that will be the CD filesystem.
125 if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
126 DevicePathSubType(node) == MEDIA_CDROM_DP) {
127 devpathcpy = malloc(devpathlen);
128 memcpy(devpathcpy, devpath, devpathlen);
130 while (!IsDevicePathEnd(NextDevicePathNode(node)))
131 node = NextDevicePathNode(node);
132 SetDevicePathEndNode(node);
133 tmpdevpath = devpathcpy;
134 status = BS->LocateDevicePath(&blkio_guid, &tmpdevpath,
137 if (EFI_ERROR(status))
140 aliases[nout] = hin[n];
146 err = efi_register_handles(&efipart_dev, hout, aliases, nout);
152 efipart_print(int verbose)
160 for (unit = 0, h = efi_find_handle(&efipart_dev, 0);
161 h != NULL; h = efi_find_handle(&efipart_dev, ++unit)) {
162 sprintf(line, " %s%d:", efipart_dev.dv_name, unit);
165 status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio);
166 if (!EFI_ERROR(status)) {
167 sprintf(line, " %llu blocks",
168 (unsigned long long)(blkio->Media->LastBlock + 1));
170 if (blkio->Media->RemovableMedia)
171 pager_output(" (removable)");
178 efipart_open(struct open_file *f, ...)
187 dev = va_arg(args, struct devdesc*);
190 h = efi_find_handle(&efipart_dev, dev->d_unit);
194 status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio);
195 if (EFI_ERROR(status))
196 return (efi_status_to_errno(status));
198 if (!blkio->Media->MediaPresent)
201 dev->d_opendata = blkio;
206 efipart_close(struct open_file *f)
210 dev = (struct devdesc *)(f->f_devdata);
211 if (dev->d_opendata == NULL)
214 dev->d_opendata = NULL;
219 * efipart_readwrite()
220 * Internal equivalent of efipart_strategy(), which operates on the
221 * media-native block size. This function expects all I/O requests
222 * to be within the media size and returns an error if such is not
226 efipart_readwrite(EFI_BLOCK_IO *blkio, int rw, daddr_t blk, daddr_t nblks,
233 if (blk < 0 || blk > blkio->Media->LastBlock)
235 if ((blk + nblks - 1) > blkio->Media->LastBlock)
240 status = blkio->ReadBlocks(blkio, blkio->Media->MediaId, blk,
241 nblks * blkio->Media->BlockSize, buf);
244 if (blkio->Media->ReadOnly)
246 status = blkio->WriteBlocks(blkio, blkio->Media->MediaId, blk,
247 nblks * blkio->Media->BlockSize, buf);
253 if (EFI_ERROR(status))
254 printf("%s: rw=%d, status=%lu\n", __func__, rw, (u_long)status);
255 return (efi_status_to_errno(status));
259 efipart_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf,
262 struct devdesc *dev = (struct devdesc *)devdata;
266 size_t blkoff, blksz;
269 if (dev == NULL || blk < 0)
272 blkio = dev->d_opendata;
276 if (size == 0 || (size % 512) != 0)
282 if (blkio->Media->BlockSize == 512)
283 return (efipart_readwrite(blkio, rw, blk, size / 512, buf));
286 * The block size of the media is not 512B per sector.
288 blkbuf = malloc(blkio->Media->BlockSize);
294 blk = off / blkio->Media->BlockSize;
295 blkoff = off % blkio->Media->BlockSize;
296 blksz = blkio->Media->BlockSize - blkoff;
298 error = efipart_readwrite(blkio, rw, blk, 1, blkbuf);
303 bcopy(blkbuf + blkoff, buf, blksz);
308 blksz = blkio->Media->BlockSize;