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 * $FreeBSD: head/sys/boot/efi/libefi/efipart.c 293724 2016-01-12 02:17:39Z smh $ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/time.h> 31 #include <sys/disklabel64.h> 32 #include <sys/dtype.h> 33 #include <stddef.h> 34 #include <stdarg.h> 35 36 #include <bootstrap.h> 37 38 #include <efi.h> 39 #include <efilib.h> 40 41 static EFI_GUID blkio_guid = BLOCK_IO_PROTOCOL; 42 static EFI_GUID devpath_guid = DEVICE_PATH_PROTOCOL; 43 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); 49 50 struct 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 62 static int 63 efipart_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 u_int n, nin, nout; 71 int err; 72 size_t devpathlen; 73 74 sz = 0; 75 hin = NULL; 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, 80 hin); 81 if (EFI_ERROR(status)) 82 free(hin); 83 } 84 if (EFI_ERROR(status)) 85 return (efi_status_to_errno(status)); 86 87 /* Filter handles to only include FreeBSD partitions. */ 88 nin = sz / sizeof(EFI_HANDLE); 89 hout = hin + nin; 90 aliases = hout + nin; 91 nout = 0; 92 93 bzero(aliases, nin * sizeof(EFI_HANDLE)); 94 95 for (n = 0; n < nin; n++) { 96 status = OpenProtocolByHandle(hin[n], &devpath_guid, 97 (void **)&devpath); 98 if (EFI_ERROR(status)) { 99 continue; 100 } 101 102 node = devpath; 103 devpathlen = DevicePathNodeLength(node); 104 while (!IsDevicePathEnd(NextDevicePathNode(node))) { 105 node = NextDevicePathNode(node); 106 devpathlen += DevicePathNodeLength(node); 107 } 108 devpathlen += DevicePathNodeLength(NextDevicePathNode(node)); 109 110 status = OpenProtocolByHandle(hin[n], &blkio_guid, 111 (void**)&blkio); 112 if (EFI_ERROR(status)) 113 continue; 114 if (!blkio->Media->LogicalPartition) 115 continue; 116 117 /* 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. 123 */ 124 if (DevicePathType(node) == MEDIA_DEVICE_PATH && 125 DevicePathSubType(node) == MEDIA_CDROM_DP) { 126 devpathcpy = malloc(devpathlen); 127 memcpy(devpathcpy, devpath, devpathlen); 128 node = devpathcpy; 129 while (!IsDevicePathEnd(NextDevicePathNode(node))) 130 node = NextDevicePathNode(node); 131 SetDevicePathEndNode(node); 132 tmpdevpath = devpathcpy; 133 status = BS->LocateDevicePath(&blkio_guid, &tmpdevpath, 134 &handle); 135 free(devpathcpy); 136 if (EFI_ERROR(status)) 137 continue; 138 hout[nout] = handle; 139 aliases[nout] = hin[n]; 140 } else 141 hout[nout] = hin[n]; 142 nout++; 143 } 144 145 err = efi_register_handles(&efipart_dev, hout, aliases, nout); 146 free(hin); 147 return (err); 148 } 149 150 static void 151 efipart_print(int verbose) 152 { 153 char line[80]; 154 EFI_BLOCK_IO *blkio; 155 EFI_HANDLE h; 156 EFI_STATUS status; 157 u_int unit; 158 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); 162 pager_output(line); 163 164 status = OpenProtocolByHandle(h, &blkio_guid, (void **)&blkio); 165 if (!EFI_ERROR(status)) { 166 sprintf(line, " %llu blocks", 167 (unsigned long long)(blkio->Media->LastBlock + 1)); 168 pager_output(line); 169 if (blkio->Media->RemovableMedia) 170 pager_output(" (removable)"); 171 } 172 pager_output("\n"); 173 } 174 } 175 176 static int 177 efipart_open(struct open_file *f, ...) 178 { 179 va_list args; 180 struct efi_devdesc *dev; 181 EFI_BLOCK_IO *blkio; 182 EFI_HANDLE h; 183 struct disklabel64 *label; 184 EFI_STATUS status; 185 size_t label_size; 186 187 label_size = (sizeof(*label) + 2047) & ~(size_t)2047; 188 189 va_start(args, f); 190 dev = va_arg(args, struct efi_devdesc*); 191 va_end(args); 192 193 h = efi_find_handle(&efipart_dev, dev->d_kind.efidisk.unit); 194 if (h == NULL) 195 return (EINVAL); 196 197 status = OpenProtocolByHandle(h, &blkio_guid, (void **)&blkio); 198 if (EFI_ERROR(status)) 199 return (efi_status_to_errno(status)); 200 201 if (!blkio->Media->MediaPresent) 202 return (EAGAIN); 203 204 dev->d_kind.efidisk.data = blkio; 205 dev->d_kind.efidisk.label_offset = 0; 206 207 /* 208 * If this is a DragonFlyBSD label64 automatically dive partition 'a' 209 * for the UFS /boot. 210 */ 211 status = BS->AllocatePool(EfiLoaderData, label_size, (void **)&label); 212 if (status == EFI_SUCCESS) { 213 label->d_magic = -1; 214 label->d_npartitions = -1; 215 label->d_partitions[0].p_fstype = -1; 216 if (efipart_strategy(dev, F_READ, 0, label_size, 217 (char *)label, NULL) == 0 && 218 label->d_magic == DISKMAGIC64 && 219 label->d_npartitions > 0 && 220 label->d_partitions[0].p_fstype == FS_BSDFFS) { 221 dev->d_kind.efidisk.label_offset = 222 label->d_partitions[0].p_boffset; 223 } 224 BS->FreePool(label); 225 } 226 return (0); 227 } 228 229 static int 230 efipart_close(struct open_file *f) 231 { 232 struct efi_devdesc *dev; 233 234 dev = (struct efi_devdesc *)(f->f_devdata); 235 if (dev->d_kind.efidisk.data == NULL) 236 return (EINVAL); 237 238 dev->d_kind.efidisk.data = NULL; 239 return (0); 240 } 241 242 /* 243 * efipart_readwrite() 244 * Internal equivalent of efipart_strategy(), which operates on the 245 * media-native block size. This function expects all I/O requests 246 * to be within the media size and returns an error if such is not 247 * the case. 248 */ 249 static int 250 efipart_readwrite(EFI_BLOCK_IO *blkio, int rw, daddr_t blk, daddr_t nblks, 251 char *buf) 252 { 253 EFI_STATUS status; 254 255 if (blkio == NULL) 256 return (ENXIO); 257 if (blk < 0 || blk > blkio->Media->LastBlock) 258 return (EIO); 259 if ((blk + nblks - 1) > blkio->Media->LastBlock) 260 return (EIO); 261 262 switch (rw) { 263 case F_READ: 264 status = blkio->ReadBlocks(blkio, blkio->Media->MediaId, blk, 265 nblks * blkio->Media->BlockSize, buf); 266 break; 267 case F_WRITE: 268 if (blkio->Media->ReadOnly) 269 return (EROFS); 270 status = blkio->WriteBlocks(blkio, blkio->Media->MediaId, blk, 271 nblks * blkio->Media->BlockSize, buf); 272 break; 273 default: 274 return (ENOSYS); 275 } 276 277 if (EFI_ERROR(status)) 278 printf("%s: rw=%d, status=%lu\n", __func__, rw, (u_long)status); 279 return (efi_status_to_errno(status)); 280 } 281 282 static int 283 efipart_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf, 284 size_t *rsize) 285 { 286 struct efi_devdesc *dev = (struct efi_devdesc *)devdata; 287 EFI_BLOCK_IO *blkio; 288 off_t off; 289 char *blkbuf; 290 size_t blkoff, blksz; 291 int error; 292 293 if (dev == NULL || blk < 0) 294 return (EINVAL); 295 296 blkio = dev->d_kind.efidisk.data; 297 if (blkio == NULL) 298 return (ENXIO); 299 300 if (size == 0 || (size % 512) != 0) 301 return (EIO); 302 303 if (rsize != NULL) 304 *rsize = size; 305 306 /* 307 * Adjust for disklabel64 308 */ 309 blk += dev->d_kind.efidisk.label_offset / DEV_BSIZE; 310 311 if (blkio->Media->BlockSize == 512) 312 return (efipart_readwrite(blkio, rw, blk, size / 512, buf)); 313 314 /* 315 * The block size of the media is not 512B per sector. 316 */ 317 blkbuf = malloc(blkio->Media->BlockSize); 318 if (blkbuf == NULL) 319 return (ENOMEM); 320 321 error = 0; 322 off = blk * 512; 323 blk = off / blkio->Media->BlockSize; 324 blkoff = off % blkio->Media->BlockSize; 325 blksz = blkio->Media->BlockSize - blkoff; 326 while (size > 0) { 327 error = efipart_readwrite(blkio, rw, blk, 1, blkbuf); 328 if (error) 329 break; 330 if (size < blksz) 331 blksz = size; 332 bcopy(blkbuf + blkoff, buf, blksz); 333 buf += blksz; 334 size -= blksz; 335 blk++; 336 blkoff = 0; 337 blksz = blkio->Media->BlockSize; 338 } 339 340 free(blkbuf); 341 return (error); 342 } 343