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;
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,
81 if (EFI_ERROR(status))
84 if (EFI_ERROR(status))
85 return (efi_status_to_errno(status));
87 /* Filter handles to only include FreeBSD partitions. */
88 nin = sz / sizeof(EFI_HANDLE);
93 bzero(aliases, nin * sizeof(EFI_HANDLE));
95 for (n = 0; n < nin; n++) {
96 status = BS->HandleProtocol(hin[n], &devpath_guid,
98 if (EFI_ERROR(status)) {
103 devpathlen = DevicePathNodeLength(node);
104 while (!IsDevicePathEnd(NextDevicePathNode(node))) {
105 node = NextDevicePathNode(node);
106 devpathlen += DevicePathNodeLength(node);
108 devpathlen += DevicePathNodeLength(NextDevicePathNode(node));
110 status = BS->HandleProtocol(hin[n], &blkio_guid,
112 if (EFI_ERROR(status))
114 if (!blkio->Media->LogicalPartition)
118 * If we come across a logical partition of subtype CDROM
119 * it doesn't refer to the CD filesystem itself, but rather
120 * to any usable El Torito boot image on it. In this case
121 * we try to find the parent device and add that instead as
122 * that will be the CD filesystem.
124 if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
125 DevicePathSubType(node) == MEDIA_CDROM_DP) {
126 devpathcpy = malloc(devpathlen);
127 memcpy(devpathcpy, devpath, devpathlen);
129 while (!IsDevicePathEnd(NextDevicePathNode(node)))
130 node = NextDevicePathNode(node);
131 SetDevicePathEndNode(node);
132 tmpdevpath = devpathcpy;
133 status = BS->LocateDevicePath(&blkio_guid, &tmpdevpath,
136 if (EFI_ERROR(status))
139 aliases[nout] = hin[n];
145 err = efi_register_handles(&efipart_dev, hout, aliases, nout);
151 efipart_print(int verbose)
159 for (unit = 0, h = efi_find_handle(&efipart_dev, 0);
160 h != NULL; h = efi_find_handle(&efipart_dev, ++unit)) {
161 sprintf(line, " %s%d:", efipart_dev.dv_name, unit);
164 status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio);
165 if (!EFI_ERROR(status)) {
166 sprintf(line, " %llu blocks",
167 (unsigned long long)(blkio->Media->LastBlock + 1));
169 if (blkio->Media->RemovableMedia)
170 pager_output(" (removable)");
177 efipart_open(struct open_file *f, ...)
186 dev = va_arg(args, struct devdesc*);
189 h = efi_find_handle(&efipart_dev, dev->d_unit);
193 status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio);
194 if (EFI_ERROR(status))
195 return (efi_status_to_errno(status));
197 if (!blkio->Media->MediaPresent)
200 dev->d_opendata = blkio;
205 efipart_close(struct open_file *f)
209 dev = (struct devdesc *)(f->f_devdata);
210 if (dev->d_opendata == NULL)
213 dev->d_opendata = NULL;
218 * efipart_readwrite()
219 * Internal equivalent of efipart_strategy(), which operates on the
220 * media-native block size. This function expects all I/O requests
221 * to be within the media size and returns an error if such is not
225 efipart_readwrite(EFI_BLOCK_IO *blkio, int rw, daddr_t blk, daddr_t nblks,
232 if (blk < 0 || blk > blkio->Media->LastBlock)
234 if ((blk + nblks - 1) > blkio->Media->LastBlock)
239 status = blkio->ReadBlocks(blkio, blkio->Media->MediaId, blk,
240 nblks * blkio->Media->BlockSize, buf);
243 if (blkio->Media->ReadOnly)
245 status = blkio->WriteBlocks(blkio, blkio->Media->MediaId, blk,
246 nblks * blkio->Media->BlockSize, buf);
252 if (EFI_ERROR(status))
253 printf("%s: rw=%d, status=%lu\n", __func__, rw, (u_long)status);
254 return (efi_status_to_errno(status));
258 efipart_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf,
261 struct devdesc *dev = (struct devdesc *)devdata;
265 size_t blkoff, blksz;
268 if (dev == NULL || blk < 0)
271 blkio = dev->d_opendata;
275 if (size == 0 || (size % 512) != 0)
281 if (blkio->Media->BlockSize == 512)
282 return (efipart_readwrite(blkio, rw, blk, size / 512, buf));
285 * The block size of the media is not 512B per sector.
287 blkbuf = malloc(blkio->Media->BlockSize);
293 blk = off / blkio->Media->BlockSize;
294 blkoff = off % blkio->Media->BlockSize;
295 blksz = blkio->Media->BlockSize - blkoff;
297 error = efipart_readwrite(blkio, rw, blk, 1, blkbuf);
302 bcopy(blkbuf + blkoff, buf, blksz);
307 blksz = blkio->Media->BlockSize;