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, char *, size_t *);
45 static int efipart_open(struct open_file *, ...);
46 static int efipart_close(struct open_file *);
47 static void efipart_print(int);
49 struct devsw efipart_dev = {
52 .dv_init = efipart_init,
53 .dv_strategy = efipart_strategy,
54 .dv_open = efipart_open,
55 .dv_close = efipart_close,
57 .dv_print = efipart_print,
65 EFI_HANDLE *hin, *hout;
72 status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz, 0);
73 if (status == EFI_BUFFER_TOO_SMALL) {
74 hin = (EFI_HANDLE *)malloc(sz * 2);
75 status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz,
77 if (EFI_ERROR(status))
80 if (EFI_ERROR(status))
81 return (efi_status_to_errno(status));
83 /* Filter handles to only include FreeBSD partitions. */
84 nin = sz / sizeof(EFI_HANDLE);
88 for (n = 0; n < nin; n++) {
89 status = BS->HandleProtocol(hin[n], &blkio_guid, &blkio);
90 if (EFI_ERROR(status))
92 if (!blkio->Media->LogicalPartition)
98 err = efi_register_handles(&efipart_dev, hout, nout);
104 efipart_print(int verbose)
112 for (unit = 0, h = efi_find_handle(&efipart_dev, 0);
113 h != NULL; h = efi_find_handle(&efipart_dev, ++unit)) {
114 sprintf(line, " %s%d:", efipart_dev.dv_name, unit);
117 status = BS->HandleProtocol(h, &blkio_guid, &blkio);
118 if (!EFI_ERROR(status)) {
119 sprintf(line, " %llu blocks",
120 (unsigned long long)(blkio->Media->LastBlock + 1));
122 if (blkio->Media->RemovableMedia)
123 pager_output(" (removable)");
130 efipart_open(struct open_file *f, ...)
139 dev = va_arg(args, struct devdesc*);
142 h = efi_find_handle(&efipart_dev, dev->d_unit);
146 status = BS->HandleProtocol(h, &blkio_guid, &blkio);
147 if (EFI_ERROR(status))
148 return (efi_status_to_errno(status));
150 if (!blkio->Media->MediaPresent)
153 dev->d_opendata = blkio;
158 efipart_close(struct open_file *f)
162 dev = (struct devdesc *)(f->f_devdata);
163 if (dev->d_opendata == NULL)
166 dev->d_opendata = NULL;
171 * efipart_readwrite()
172 * Internal equivalent of efipart_strategy(), which operates on the
173 * media-native block size. This function expects all I/O requests
174 * to be within the media size and returns an error if such is not
178 efipart_readwrite(EFI_BLOCK_IO *blkio, int rw, daddr_t blk, daddr_t nblks,
185 if (blk < 0 || blk > blkio->Media->LastBlock)
187 if ((blk + nblks - 1) > blkio->Media->LastBlock)
192 status = blkio->ReadBlocks(blkio, blkio->Media->MediaId, blk,
193 nblks * blkio->Media->BlockSize, buf);
196 if (blkio->Media->ReadOnly)
198 status = blkio->WriteBlocks(blkio, blkio->Media->MediaId, blk,
199 nblks * blkio->Media->BlockSize, buf);
205 if (EFI_ERROR(status))
206 printf("%s: rw=%d, status=%lu\n", __func__, rw, status);
207 return (efi_status_to_errno(status));
211 efipart_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf,
214 struct devdesc *dev = (struct devdesc *)devdata;
218 size_t blkoff, blksz;
221 if (dev == NULL || blk < 0)
224 blkio = dev->d_opendata;
228 if (size == 0 || (size % 512) != 0)
234 if (blkio->Media->BlockSize == 512)
235 return (efipart_readwrite(blkio, rw, blk, size / 512, buf));
238 * The block size of the media is not 512B per sector.
240 blkbuf = malloc(blkio->Media->BlockSize);
246 blk = off / blkio->Media->BlockSize;
247 blkoff = off % blkio->Media->BlockSize;
248 blksz = blkio->Media->BlockSize - blkoff;
250 error = efipart_readwrite(blkio, rw, blk, 1, blkbuf);
255 bcopy(blkbuf + blkoff, buf, blksz);
260 blksz = blkio->Media->BlockSize;