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, *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)) {
102 while (!IsDevicePathEnd(NextDevicePathNode(node)))
103 node = NextDevicePathNode(node);
104 status = BS->HandleProtocol(hin[n], &blkio_guid,
106 if (EFI_ERROR(status))
108 if (!blkio->Media->LogicalPartition)
112 * If we come across a logical partition of subtype CDROM
113 * it doesn't refer to the CD filesystem itself, but rather
114 * to any usable El Torito boot image on it. In this case
115 * we try to find the parent device and add that instead as
116 * that will be the CD filesystem.
118 if (DevicePathType(node) == MEDIA_DEVICE_PATH &&
119 DevicePathSubType(node) == MEDIA_CDROM_DP) {
120 node->Type = END_DEVICE_PATH_TYPE;
121 node->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
122 status = BS->LocateDevicePath(&blkio_guid, &devpath,
124 if (EFI_ERROR(status))
127 aliases[nout] = hin[n];
133 err = efi_register_handles(&efipart_dev, hout, aliases, nout);
139 efipart_print(int verbose)
147 for (unit = 0, h = efi_find_handle(&efipart_dev, 0);
148 h != NULL; h = efi_find_handle(&efipart_dev, ++unit)) {
149 sprintf(line, " %s%d:", efipart_dev.dv_name, unit);
152 status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio);
153 if (!EFI_ERROR(status)) {
154 sprintf(line, " %llu blocks",
155 (unsigned long long)(blkio->Media->LastBlock + 1));
157 if (blkio->Media->RemovableMedia)
158 pager_output(" (removable)");
165 efipart_open(struct open_file *f, ...)
174 dev = va_arg(args, struct devdesc*);
177 h = efi_find_handle(&efipart_dev, dev->d_unit);
181 status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio);
182 if (EFI_ERROR(status))
183 return (efi_status_to_errno(status));
185 if (!blkio->Media->MediaPresent)
188 dev->d_opendata = blkio;
193 efipart_close(struct open_file *f)
197 dev = (struct devdesc *)(f->f_devdata);
198 if (dev->d_opendata == NULL)
201 dev->d_opendata = NULL;
206 * efipart_readwrite()
207 * Internal equivalent of efipart_strategy(), which operates on the
208 * media-native block size. This function expects all I/O requests
209 * to be within the media size and returns an error if such is not
213 efipart_readwrite(EFI_BLOCK_IO *blkio, int rw, daddr_t blk, daddr_t nblks,
220 if (blk < 0 || blk > blkio->Media->LastBlock)
222 if ((blk + nblks - 1) > blkio->Media->LastBlock)
227 status = blkio->ReadBlocks(blkio, blkio->Media->MediaId, blk,
228 nblks * blkio->Media->BlockSize, buf);
231 if (blkio->Media->ReadOnly)
233 status = blkio->WriteBlocks(blkio, blkio->Media->MediaId, blk,
234 nblks * blkio->Media->BlockSize, buf);
240 if (EFI_ERROR(status))
241 printf("%s: rw=%d, status=%lu\n", __func__, rw, (u_long)status);
242 return (efi_status_to_errno(status));
246 efipart_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf,
249 struct devdesc *dev = (struct devdesc *)devdata;
253 size_t blkoff, blksz;
256 if (dev == NULL || blk < 0)
259 blkio = dev->d_opendata;
263 if (size == 0 || (size % 512) != 0)
269 if (blkio->Media->BlockSize == 512)
270 return (efipart_readwrite(blkio, rw, blk, size / 512, buf));
273 * The block size of the media is not 512B per sector.
275 blkbuf = malloc(blkio->Media->BlockSize);
281 blk = off / blkio->Media->BlockSize;
282 blkoff = off % blkio->Media->BlockSize;
283 blksz = blkio->Media->BlockSize - blkoff;
285 error = efipart_readwrite(blkio, rw, blk, 1, blkbuf);
290 bcopy(blkbuf + blkoff, buf, blksz);
295 blksz = blkio->Media->BlockSize;