efipart.c revision 285951
1/*- 2 * Copyright (c) 2010 Marcel Moolenaar 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: stable/10/sys/boot/efi/libefi/efipart.c 285951 2015-07-28 12:45:08Z emaste $"); 29 30#include <sys/param.h> 31#include <sys/time.h> 32#include <stddef.h> 33#include <stdarg.h> 34 35#include <bootstrap.h> 36 37#include <efi.h> 38#include <efilib.h> 39#include <efiprot.h> 40 41static EFI_GUID blkio_guid = BLOCK_IO_PROTOCOL; 42static EFI_GUID devpath_guid = DEVICE_PATH_PROTOCOL; 43 44static int efipart_init(void); 45static int efipart_strategy(void *, int, daddr_t, size_t, char *, size_t *); 46static int efipart_open(struct open_file *, ...); 47static int efipart_close(struct open_file *); 48static void efipart_print(int); 49 50struct devsw efipart_dev = { 51 .dv_name = "part", 52 .dv_type = DEVT_DISK, 53 .dv_init = efipart_init, 54 .dv_strategy = efipart_strategy, 55 .dv_open = efipart_open, 56 .dv_close = efipart_close, 57 .dv_ioctl = noioctl, 58 .dv_print = efipart_print, 59 .dv_cleanup = NULL 60}; 61 62static int 63efipart_init(void) 64{ 65 EFI_BLOCK_IO *blkio; 66 EFI_DEVICE_PATH *devpath, *devpathcpy, *tmpdevpath, *node; 67 EFI_HANDLE *hin, *hout, *aliases, handle; 68 EFI_STATUS status; 69 UINTN sz; 70 CHAR16 *path; 71 u_int n, nin, nout; 72 int err; 73 size_t devpathlen; 74 75 sz = 0; 76 hin = NULL; 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, 81 hin); 82 if (EFI_ERROR(status)) 83 free(hin); 84 } 85 if (EFI_ERROR(status)) 86 return (efi_status_to_errno(status)); 87 88 /* Filter handles to only include FreeBSD partitions. */ 89 nin = sz / sizeof(EFI_HANDLE); 90 hout = hin + nin; 91 aliases = hout + nin; 92 nout = 0; 93 94 bzero(aliases, nin * sizeof(EFI_HANDLE)); 95 96 for (n = 0; n < nin; n++) { 97 status = BS->HandleProtocol(hin[n], &devpath_guid, 98 (void **)&devpath); 99 if (EFI_ERROR(status)) { 100 continue; 101 } 102 103 node = devpath; 104 devpathlen = DevicePathNodeLength(node); 105 while (!IsDevicePathEnd(NextDevicePathNode(node))) { 106 node = NextDevicePathNode(node); 107 devpathlen += DevicePathNodeLength(node); 108 } 109 devpathlen += DevicePathNodeLength(NextDevicePathNode(node)); 110 111 status = BS->HandleProtocol(hin[n], &blkio_guid, 112 (void**)&blkio); 113 if (EFI_ERROR(status)) 114 continue; 115 if (!blkio->Media->LogicalPartition) 116 continue; 117 118 /* 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. 124 */ 125 if (DevicePathType(node) == MEDIA_DEVICE_PATH && 126 DevicePathSubType(node) == MEDIA_CDROM_DP) { 127 devpathcpy = malloc(devpathlen); 128 memcpy(devpathcpy, devpath, devpathlen); 129 node = devpathcpy; 130 while (!IsDevicePathEnd(NextDevicePathNode(node))) 131 node = NextDevicePathNode(node); 132 SetDevicePathEndNode(node); 133 tmpdevpath = devpathcpy; 134 status = BS->LocateDevicePath(&blkio_guid, &tmpdevpath, 135 &handle); 136 free(devpathcpy); 137 if (EFI_ERROR(status)) 138 continue; 139 hout[nout] = handle; 140 aliases[nout] = hin[n]; 141 } else 142 hout[nout] = hin[n]; 143 nout++; 144 } 145 146 err = efi_register_handles(&efipart_dev, hout, aliases, nout); 147 free(hin); 148 return (err); 149} 150 151static void 152efipart_print(int verbose) 153{ 154 char line[80]; 155 EFI_BLOCK_IO *blkio; 156 EFI_HANDLE h; 157 EFI_STATUS status; 158 u_int unit; 159 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); 163 pager_output(line); 164 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)); 169 pager_output(line); 170 if (blkio->Media->RemovableMedia) 171 pager_output(" (removable)"); 172 } 173 pager_output("\n"); 174 } 175} 176 177static int 178efipart_open(struct open_file *f, ...) 179{ 180 va_list args; 181 struct devdesc *dev; 182 EFI_BLOCK_IO *blkio; 183 EFI_HANDLE h; 184 EFI_STATUS status; 185 186 va_start(args, f); 187 dev = va_arg(args, struct devdesc*); 188 va_end(args); 189 190 h = efi_find_handle(&efipart_dev, dev->d_unit); 191 if (h == NULL) 192 return (EINVAL); 193 194 status = BS->HandleProtocol(h, &blkio_guid, (void **)&blkio); 195 if (EFI_ERROR(status)) 196 return (efi_status_to_errno(status)); 197 198 if (!blkio->Media->MediaPresent) 199 return (EAGAIN); 200 201 dev->d_opendata = blkio; 202 return (0); 203} 204 205static int 206efipart_close(struct open_file *f) 207{ 208 struct devdesc *dev; 209 210 dev = (struct devdesc *)(f->f_devdata); 211 if (dev->d_opendata == NULL) 212 return (EINVAL); 213 214 dev->d_opendata = NULL; 215 return (0); 216} 217 218/* 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 223 * the case. 224 */ 225static int 226efipart_readwrite(EFI_BLOCK_IO *blkio, int rw, daddr_t blk, daddr_t nblks, 227 char *buf) 228{ 229 EFI_STATUS status; 230 231 if (blkio == NULL) 232 return (ENXIO); 233 if (blk < 0 || blk > blkio->Media->LastBlock) 234 return (EIO); 235 if ((blk + nblks - 1) > blkio->Media->LastBlock) 236 return (EIO); 237 238 switch (rw) { 239 case F_READ: 240 status = blkio->ReadBlocks(blkio, blkio->Media->MediaId, blk, 241 nblks * blkio->Media->BlockSize, buf); 242 break; 243 case F_WRITE: 244 if (blkio->Media->ReadOnly) 245 return (EROFS); 246 status = blkio->WriteBlocks(blkio, blkio->Media->MediaId, blk, 247 nblks * blkio->Media->BlockSize, buf); 248 break; 249 default: 250 return (ENOSYS); 251 } 252 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)); 256} 257 258static int 259efipart_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf, 260 size_t *rsize) 261{ 262 struct devdesc *dev = (struct devdesc *)devdata; 263 EFI_BLOCK_IO *blkio; 264 off_t off; 265 char *blkbuf; 266 size_t blkoff, blksz; 267 int error; 268 269 if (dev == NULL || blk < 0) 270 return (EINVAL); 271 272 blkio = dev->d_opendata; 273 if (blkio == NULL) 274 return (ENXIO); 275 276 if (size == 0 || (size % 512) != 0) 277 return (EIO); 278 279 if (rsize != NULL) 280 *rsize = size; 281 282 if (blkio->Media->BlockSize == 512) 283 return (efipart_readwrite(blkio, rw, blk, size / 512, buf)); 284 285 /* 286 * The block size of the media is not 512B per sector. 287 */ 288 blkbuf = malloc(blkio->Media->BlockSize); 289 if (blkbuf == NULL) 290 return (ENOMEM); 291 292 error = 0; 293 off = blk * 512; 294 blk = off / blkio->Media->BlockSize; 295 blkoff = off % blkio->Media->BlockSize; 296 blksz = blkio->Media->BlockSize - blkoff; 297 while (size > 0) { 298 error = efipart_readwrite(blkio, rw, blk, 1, blkbuf); 299 if (error) 300 break; 301 if (size < blksz) 302 blksz = size; 303 bcopy(blkbuf + blkoff, buf, blksz); 304 buf += blksz; 305 size -= blksz; 306 blk++; 307 blkoff = 0; 308 blksz = blkio->Media->BlockSize; 309 } 310 311 free(blkbuf); 312 return (error); 313} 314