diff --git a/stand/common/md.c b/stand/common/md.c index 9f95d3cdfd2d..a3243beb17dd 100644 --- a/stand/common/md.c +++ b/stand/common/md.c @@ -1,158 +1,159 @@ /*- * Copyright (c) 2009 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include "bootstrap.h" #define MD_BLOCK_SIZE 512 #ifndef MD_IMAGE_SIZE #error Must be compiled with MD_IMAGE_SIZE defined #endif #if (MD_IMAGE_SIZE == 0 || MD_IMAGE_SIZE % MD_BLOCK_SIZE) #error Image size must be a multiple of 512. #endif /* * Preloaded image gets put here. * Applications that patch the object with the image can determine * the size looking at the start and end markers (strings), * so we want them contiguous. */ static struct { u_char start[MD_IMAGE_SIZE]; u_char end[128]; } md_image = { .start = "MFS Filesystem goes here", .end = "MFS Filesystem had better STOP here", }; /* devsw I/F */ static int md_init(void); static int md_strategy(void *, int, daddr_t, size_t, char *, size_t *); static int md_open(struct open_file *, ...); static int md_close(struct open_file *); static int md_print(int); struct devsw md_dev = { .dv_name = "md", .dv_type = DEVT_DISK, .dv_init = md_init, .dv_strategy = md_strategy, .dv_open = md_open, .dv_close = md_close, .dv_ioctl = noioctl, .dv_print = md_print, .dv_cleanup = nullsys, .dv_fmtdev = disk_fmtdev, + .dv_parsedev = disk_parsedev, }; static int md_init(void) { return (0); } static int md_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf, size_t *rsize) { struct devdesc *dev = (struct devdesc *)devdata; size_t ofs; if (dev->d_unit != 0) return (ENXIO); if (blk < 0 || blk >= (MD_IMAGE_SIZE / MD_BLOCK_SIZE)) return (EIO); if (size % MD_BLOCK_SIZE) return (EIO); ofs = blk * MD_BLOCK_SIZE; if ((ofs + size) > MD_IMAGE_SIZE) size = MD_IMAGE_SIZE - ofs; if (rsize != NULL) *rsize = size; switch (rw & F_MASK) { case F_READ: bcopy(md_image.start + ofs, buf, size); return (0); case F_WRITE: bcopy(buf, md_image.start + ofs, size); return (0); } return (ENODEV); } static int md_open(struct open_file *f, ...) { va_list ap; struct devdesc *dev; va_start(ap, f); dev = va_arg(ap, struct devdesc *); va_end(ap); if (dev->d_unit != 0) return (ENXIO); return (0); } static int md_close(struct open_file *f) { struct devdesc *dev; dev = (struct devdesc *)(f->f_devdata); return ((dev->d_unit != 0) ? ENXIO : 0); } static int md_print(int verbose) { printf("%s devices:", md_dev.dv_name); if (pager_output("\n") != 0) return (1); printf("MD (%u bytes)", MD_IMAGE_SIZE); return (pager_output("\n")); } diff --git a/stand/common/vdisk.c b/stand/common/vdisk.c index c4220787b1ac..5c08a524b4d3 100644 --- a/stand/common/vdisk.c +++ b/stand/common/vdisk.c @@ -1,418 +1,419 @@ /*- * Copyright 2019 Toomas Soome * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include static int vdisk_init(void); static int vdisk_strategy(void *, int, daddr_t, size_t, char *, size_t *); static int vdisk_open(struct open_file *, ...); static int vdisk_close(struct open_file *); static int vdisk_ioctl(struct open_file *, u_long, void *); static int vdisk_print(int); struct devsw vdisk_dev = { .dv_name = "vdisk", .dv_type = DEVT_DISK, .dv_init = vdisk_init, .dv_strategy = vdisk_strategy, .dv_open = vdisk_open, .dv_close = vdisk_close, .dv_ioctl = vdisk_ioctl, .dv_print = vdisk_print, .dv_cleanup = nullsys, .dv_fmtdev = disk_fmtdev, + .dv_parsedev = disk_parsedev, }; typedef STAILQ_HEAD(vdisk_info_list, vdisk_info) vdisk_info_list_t; typedef struct vdisk_info { STAILQ_ENTRY(vdisk_info) vdisk_link; /* link in device list */ char *vdisk_path; int vdisk_unit; int vdisk_fd; uint64_t vdisk_size; /* size in bytes */ uint32_t vdisk_sectorsz; uint32_t vdisk_open; /* reference counter */ } vdisk_info_t; static vdisk_info_list_t vdisk_list; /* list of mapped vdisks. */ static vdisk_info_t * vdisk_get_info(struct devdesc *dev) { vdisk_info_t *vd; STAILQ_FOREACH(vd, &vdisk_list, vdisk_link) { if (vd->vdisk_unit == dev->d_unit) return (vd); } return (vd); } COMMAND_SET(map_vdisk, "map-vdisk", "map file as virtual disk", command_mapvd); static int command_mapvd(int argc, char *argv[]) { vdisk_info_t *vd, *p; struct stat sb; if (argc != 2) { printf("usage: %s filename\n", argv[0]); return (CMD_ERROR); } STAILQ_FOREACH(vd, &vdisk_list, vdisk_link) { if (strcmp(vd->vdisk_path, argv[1]) == 0) { printf("%s: file %s is already mapped as %s%d\n", argv[0], argv[1], vdisk_dev.dv_name, vd->vdisk_unit); return (CMD_ERROR); } } if (stat(argv[1], &sb) < 0) { /* * ENOSYS is really ENOENT because we did try to walk * through devsw list to try to open this file. */ if (errno == ENOSYS) errno = ENOENT; printf("%s: stat failed: %s\n", argv[0], strerror(errno)); return (CMD_ERROR); } /* * Avoid mapping small files. */ if (sb.st_size < 1024 * 1024) { printf("%s: file %s is too small.\n", argv[0], argv[1]); return (CMD_ERROR); } vd = calloc(1, sizeof (*vd)); if (vd == NULL) { printf("%s: out of memory\n", argv[0]); return (CMD_ERROR); } vd->vdisk_path = strdup(argv[1]); if (vd->vdisk_path == NULL) { free (vd); printf("%s: out of memory\n", argv[0]); return (CMD_ERROR); } vd->vdisk_fd = open(vd->vdisk_path, O_RDONLY); if (vd->vdisk_fd < 0) { printf("%s: open failed: %s\n", argv[0], strerror(errno)); free(vd->vdisk_path); free(vd); return (CMD_ERROR); } vd->vdisk_size = sb.st_size; vd->vdisk_sectorsz = DEV_BSIZE; STAILQ_FOREACH(p, &vdisk_list, vdisk_link) { vdisk_info_t *n; if (p->vdisk_unit == vd->vdisk_unit) { vd->vdisk_unit++; continue; } n = STAILQ_NEXT(p, vdisk_link); if (p->vdisk_unit < vd->vdisk_unit) { if (n == NULL) { /* p is last elem */ STAILQ_INSERT_TAIL(&vdisk_list, vd, vdisk_link); break; } if (n->vdisk_unit > vd->vdisk_unit) { /* p < vd < n */ STAILQ_INSERT_AFTER(&vdisk_list, p, vd, vdisk_link); break; } /* else n < vd or n == vd */ vd->vdisk_unit++; continue; } /* p > vd only if p is the first element */ STAILQ_INSERT_HEAD(&vdisk_list, vd, vdisk_link); break; } /* if the list was empty or contiguous */ if (p == NULL) STAILQ_INSERT_TAIL(&vdisk_list, vd, vdisk_link); printf("%s: file %s is mapped as %s%d\n", argv[0], vd->vdisk_path, vdisk_dev.dv_name, vd->vdisk_unit); return (CMD_OK); } COMMAND_SET(unmap_vdisk, "unmap-vdisk", "unmap virtual disk", command_unmapvd); /* * unmap-vdisk vdiskX */ static int command_unmapvd(int argc, char *argv[]) { size_t len; vdisk_info_t *vd; long unit; char *end; if (argc != 2) { printf("usage: %s %sN\n", argv[0], vdisk_dev.dv_name); return (CMD_ERROR); } len = strlen(vdisk_dev.dv_name); if (strncmp(vdisk_dev.dv_name, argv[1], len) != 0) { printf("%s: unknown device %s\n", argv[0], argv[1]); return (CMD_ERROR); } errno = 0; unit = strtol(argv[1] + len, &end, 10); if (errno != 0 || (*end != '\0' && strcmp(end, ":") != 0)) { printf("%s: unknown device %s\n", argv[0], argv[1]); return (CMD_ERROR); } STAILQ_FOREACH(vd, &vdisk_list, vdisk_link) { if (vd->vdisk_unit == unit) break; } if (vd == NULL) { printf("%s: unknown device %s\n", argv[0], argv[1]); return (CMD_ERROR); } if (vd->vdisk_open != 0) { printf("%s: %s is in use, unable to unmap.\n", argv[0], argv[1]); return (CMD_ERROR); } STAILQ_REMOVE(&vdisk_list, vd, vdisk_info, vdisk_link); (void) close(vd->vdisk_fd); printf("%s (%s) unmapped\n", argv[1], vd->vdisk_path); free(vd->vdisk_path); free(vd); return (CMD_OK); } static int vdisk_init(void) { STAILQ_INIT(&vdisk_list); return (0); } static int vdisk_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf, size_t *rsize) { struct disk_devdesc *dev; vdisk_info_t *vd; ssize_t rv; dev = devdata; if (dev == NULL) return (EINVAL); vd = vdisk_get_info((struct devdesc *)dev); if (vd == NULL) return (EINVAL); if (size == 0 || (size % 512) != 0) return (EIO); if (dev->dd.d_dev->dv_type == DEVT_DISK) { daddr_t offset; offset = dev->d_offset * vd->vdisk_sectorsz; offset /= 512; blk += offset; } if (lseek(vd->vdisk_fd, blk << 9, SEEK_SET) == -1) return (EIO); errno = 0; switch (rw & F_MASK) { case F_READ: rv = read(vd->vdisk_fd, buf, size); break; case F_WRITE: rv = write(vd->vdisk_fd, buf, size); break; default: return (ENOSYS); } if (errno == 0 && rsize != NULL) { *rsize = rv; } return (errno); } static int vdisk_open(struct open_file *f, ...) { va_list args; struct disk_devdesc *dev; vdisk_info_t *vd; int rc = 0; va_start(args, f); dev = va_arg(args, struct disk_devdesc *); va_end(args); if (dev == NULL) return (EINVAL); vd = vdisk_get_info((struct devdesc *)dev); if (vd == NULL) return (EINVAL); if (dev->dd.d_dev->dv_type == DEVT_DISK) { rc = disk_open(dev, vd->vdisk_size, vd->vdisk_sectorsz); } if (rc == 0) vd->vdisk_open++; return (rc); } static int vdisk_close(struct open_file *f) { struct disk_devdesc *dev; vdisk_info_t *vd; dev = (struct disk_devdesc *)(f->f_devdata); if (dev == NULL) return (EINVAL); vd = vdisk_get_info((struct devdesc *)dev); if (vd == NULL) return (EINVAL); vd->vdisk_open--; if (dev->dd.d_dev->dv_type == DEVT_DISK) return (disk_close(dev)); return (0); } static int vdisk_ioctl(struct open_file *f, u_long cmd, void *data) { struct disk_devdesc *dev; vdisk_info_t *vd; int rc; dev = (struct disk_devdesc *)(f->f_devdata); if (dev == NULL) return (EINVAL); vd = vdisk_get_info((struct devdesc *)dev); if (vd == NULL) return (EINVAL); if (dev->dd.d_dev->dv_type == DEVT_DISK) { rc = disk_ioctl(dev, cmd, data); if (rc != ENOTTY) return (rc); } switch (cmd) { case DIOCGSECTORSIZE: *(u_int *)data = vd->vdisk_sectorsz; break; case DIOCGMEDIASIZE: *(uint64_t *)data = vd->vdisk_size; break; default: return (ENOTTY); } return (0); } static int vdisk_print(int verbose) { int ret = 0; vdisk_info_t *vd; char line[80]; if (STAILQ_EMPTY(&vdisk_list)) return (ret); printf("%s devices:", vdisk_dev.dv_name); if ((ret = pager_output("\n")) != 0) return (ret); STAILQ_FOREACH(vd, &vdisk_list, vdisk_link) { struct disk_devdesc vd_dev; if (verbose) { printf(" %s", vd->vdisk_path); if ((ret = pager_output("\n")) != 0) break; } snprintf(line, sizeof(line), " %s%d", vdisk_dev.dv_name, vd->vdisk_unit); printf("%s: %" PRIu64 " X %u blocks", line, vd->vdisk_size / vd->vdisk_sectorsz, vd->vdisk_sectorsz); if ((ret = pager_output("\n")) != 0) break; vd_dev.dd.d_dev = &vdisk_dev; vd_dev.dd.d_unit = vd->vdisk_unit; vd_dev.d_slice = -1; vd_dev.d_partition = -1; ret = disk_open(&vd_dev, vd->vdisk_size, vd->vdisk_sectorsz); if (ret == 0) { ret = disk_print(&vd_dev, line, verbose); disk_close(&vd_dev); if (ret != 0) break; } else { ret = 0; } } return (ret); } diff --git a/stand/efi/libefi/efipart.c b/stand/efi/libefi/efipart.c index 88587192a1b7..9a4d2b22ed20 100644 --- a/stand/efi/libefi/efipart.c +++ b/stand/efi/libefi/efipart.c @@ -1,1253 +1,1254 @@ /*- * Copyright (c) 2010 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include static EFI_GUID blkio_guid = BLOCK_IO_PROTOCOL; typedef bool (*pd_test_cb_t)(pdinfo_t *, pdinfo_t *); static int efipart_initfd(void); static int efipart_initcd(void); static int efipart_inithd(void); static void efipart_cdinfo_add(pdinfo_t *); static int efipart_strategy(void *, int, daddr_t, size_t, char *, size_t *); static int efipart_realstrategy(void *, int, daddr_t, size_t, char *, size_t *); static int efipart_open(struct open_file *, ...); static int efipart_close(struct open_file *); static int efipart_ioctl(struct open_file *, u_long, void *); static int efipart_printfd(int); static int efipart_printcd(int); static int efipart_printhd(int); /* EISA PNP ID's for floppy controllers */ #define PNP0604 0x604 #define PNP0700 0x700 #define PNP0701 0x701 /* Bounce buffer max size */ #define BIO_BUFFER_SIZE 0x4000 struct devsw efipart_fddev = { .dv_name = "fd", .dv_type = DEVT_FD, .dv_init = efipart_initfd, .dv_strategy = efipart_strategy, .dv_open = efipart_open, .dv_close = efipart_close, .dv_ioctl = efipart_ioctl, .dv_print = efipart_printfd, .dv_cleanup = nullsys, }; struct devsw efipart_cddev = { .dv_name = "cd", .dv_type = DEVT_CD, .dv_init = efipart_initcd, .dv_strategy = efipart_strategy, .dv_open = efipart_open, .dv_close = efipart_close, .dv_ioctl = efipart_ioctl, .dv_print = efipart_printcd, .dv_cleanup = nullsys, }; struct devsw efipart_hddev = { .dv_name = "disk", .dv_type = DEVT_DISK, .dv_init = efipart_inithd, .dv_strategy = efipart_strategy, .dv_open = efipart_open, .dv_close = efipart_close, .dv_ioctl = efipart_ioctl, .dv_print = efipart_printhd, .dv_cleanup = nullsys, .dv_fmtdev = disk_fmtdev, + .dv_parsedev = disk_parsedev, }; static pdinfo_list_t fdinfo = STAILQ_HEAD_INITIALIZER(fdinfo); static pdinfo_list_t cdinfo = STAILQ_HEAD_INITIALIZER(cdinfo); static pdinfo_list_t hdinfo = STAILQ_HEAD_INITIALIZER(hdinfo); /* * efipart_inithandles() is used to build up the pdinfo list from * block device handles. Then each devsw init callback is used to * pick items from pdinfo and move to proper device list. * In ideal world, we should end up with empty pdinfo once all * devsw initializers are called. */ static pdinfo_list_t pdinfo = STAILQ_HEAD_INITIALIZER(pdinfo); pdinfo_list_t * efiblk_get_pdinfo_list(struct devsw *dev) { if (dev->dv_type == DEVT_DISK) return (&hdinfo); if (dev->dv_type == DEVT_CD) return (&cdinfo); if (dev->dv_type == DEVT_FD) return (&fdinfo); return (NULL); } /* XXX this gets called way way too often, investigate */ pdinfo_t * efiblk_get_pdinfo(struct devdesc *dev) { pdinfo_list_t *pdi; pdinfo_t *pd = NULL; pdi = efiblk_get_pdinfo_list(dev->d_dev); if (pdi == NULL) return (pd); STAILQ_FOREACH(pd, pdi, pd_link) { if (pd->pd_unit == dev->d_unit) return (pd); } return (pd); } pdinfo_t * efiblk_get_pdinfo_by_device_path(EFI_DEVICE_PATH *path) { EFI_HANDLE h; EFI_STATUS status; EFI_DEVICE_PATH *devp = path; status = BS->LocateDevicePath(&blkio_guid, &devp, &h); if (EFI_ERROR(status)) return (NULL); return (efiblk_get_pdinfo_by_handle(h)); } static bool same_handle(pdinfo_t *pd, EFI_HANDLE h) { return (pd->pd_handle == h || pd->pd_alias == h); } pdinfo_t * efiblk_get_pdinfo_by_handle(EFI_HANDLE h) { pdinfo_t *dp, *pp; /* * Check hard disks, then cd, then floppy */ STAILQ_FOREACH(dp, &hdinfo, pd_link) { if (same_handle(dp, h)) return (dp); STAILQ_FOREACH(pp, &dp->pd_part, pd_link) { if (same_handle(pp, h)) return (pp); } } STAILQ_FOREACH(dp, &cdinfo, pd_link) { if (same_handle(dp, h)) return (dp); STAILQ_FOREACH(pp, &dp->pd_part, pd_link) { if (same_handle(pp, h)) return (pp); } } STAILQ_FOREACH(dp, &fdinfo, pd_link) { if (same_handle(dp, h)) return (dp); } return (NULL); } static int efiblk_pdinfo_count(pdinfo_list_t *pdi) { pdinfo_t *pd; int i = 0; STAILQ_FOREACH(pd, pdi, pd_link) { i++; } return (i); } static pdinfo_t * efipart_find_parent(pdinfo_list_t *pdi, EFI_DEVICE_PATH *devpath) { pdinfo_t *pd; EFI_DEVICE_PATH *parent; /* We want to find direct parent */ parent = efi_devpath_trim(devpath); /* We should not get out of memory here but be careful. */ if (parent == NULL) return (NULL); STAILQ_FOREACH(pd, pdi, pd_link) { /* We must have exact match. */ if (efi_devpath_match(pd->pd_devpath, parent)) break; } free(parent); return (pd); } /* * Return true when we should ignore this device. */ static bool efipart_ignore_device(EFI_HANDLE h, EFI_BLOCK_IO *blkio, EFI_DEVICE_PATH *devpath) { EFI_DEVICE_PATH *node, *parent; /* * We assume the block size 512 or greater power of 2. * Also skip devices with block size > 64k (16 is max * ashift supported by zfs). * iPXE is known to insert stub BLOCK IO device with * BlockSize 1. */ if (blkio->Media->BlockSize < 512 || blkio->Media->BlockSize > (1 << 16) || !powerof2(blkio->Media->BlockSize)) { efi_close_devpath(h); return (true); } /* Allowed values are 0, 1 and power of 2. */ if (blkio->Media->IoAlign > 1 && !powerof2(blkio->Media->IoAlign)) { efi_close_devpath(h); return (true); } /* * With device tree setup: * PciRoot(0x0)/Pci(0x14,0x0)/USB(0x5,0)/USB(0x2,0x0) * PciRoot(0x0)/Pci(0x14,0x0)/USB(0x5,0)/USB(0x2,0x0)/Unit(0x1) * PciRoot(0x0)/Pci(0x14,0x0)/USB(0x5,0)/USB(0x2,0x0)/Unit(0x2) * PciRoot(0x0)/Pci(0x14,0x0)/USB(0x5,0)/USB(0x2,0x0)/Unit(0x3) * PciRoot(0x0)/Pci(0x14,0x0)/USB(0x5,0)/USB(0x2,0x0)/Unit(0x3)/CDROM.. * PciRoot(0x0)/Pci(0x14,0x0)/USB(0x5,0)/USB(0x2,0x0)/Unit(0x3)/CDROM.. * PciRoot(0x0)/Pci(0x14,0x0)/USB(0x5,0)/USB(0x2,0x0)/Unit(0x4) * PciRoot(0x0)/Pci(0x14,0x0)/USB(0x5,0)/USB(0x2,0x0)/Unit(0x5) * PciRoot(0x0)/Pci(0x14,0x0)/USB(0x5,0)/USB(0x2,0x0)/Unit(0x6) * PciRoot(0x0)/Pci(0x14,0x0)/USB(0x5,0)/USB(0x2,0x0)/Unit(0x7) * * In above exmple only Unit(0x3) has media, all other nodes are * missing media and should not be used. * * No media does not always mean there is no device, but in above * case, we can not really assume there is any device. * Therefore, if this node is USB, or this node is Unit (LUN) and * direct parent is USB and we have no media, we will ignore this * device. * * Variation of the same situation, but with SCSI devices: * PciRoot(0x0)/Pci(0x1a,0x0)/USB(0x1,0)/USB(0x3,0x0)/SCSI(0x0,0x1) * PciRoot(0x0)/Pci(0x1a,0x0)/USB(0x1,0)/USB(0x3,0x0)/SCSI(0x0,0x2) * PciRoot(0x0)/Pci(0x1a,0x0)/USB(0x1,0)/USB(0x3,0x0)/SCSI(0x0,0x3) * PciRoot(0x0)/Pci(0x1a,0x0)/USB(0x1,0)/USB(0x3,0x0)/SCSI(0x0,0x3)/CD.. * PciRoot(0x0)/Pci(0x1a,0x0)/USB(0x1,0)/USB(0x3,0x0)/SCSI(0x0,0x3)/CD.. * PciRoot(0x0)/Pci(0x1a,0x0)/USB(0x1,0)/USB(0x3,0x0)/SCSI(0x0,0x4) * * Here above the SCSI luns 1,2 and 4 have no media. */ /* Do not ignore device with media. */ if (blkio->Media->MediaPresent) return (false); node = efi_devpath_last_node(devpath); if (node == NULL) return (false); /* USB without media present */ if (DevicePathType(node) == MESSAGING_DEVICE_PATH && DevicePathSubType(node) == MSG_USB_DP) { efi_close_devpath(h); return (true); } parent = efi_devpath_trim(devpath); if (parent != NULL) { bool parent_is_usb = false; node = efi_devpath_last_node(parent); if (node == NULL) { free(parent); return (false); } if (DevicePathType(node) == MESSAGING_DEVICE_PATH && DevicePathSubType(node) == MSG_USB_DP) parent_is_usb = true; free(parent); node = efi_devpath_last_node(devpath); if (node == NULL) return (false); if (parent_is_usb && DevicePathType(node) == MESSAGING_DEVICE_PATH) { /* * no media, parent is USB and devicepath is * LUN or SCSI. */ if (DevicePathSubType(node) == MSG_DEVICE_LOGICAL_UNIT_DP || DevicePathSubType(node) == MSG_SCSI_DP) { efi_close_devpath(h); return (true); } } } return (false); } int efipart_inithandles(void) { unsigned i, nin; UINTN sz; EFI_HANDLE *hin; EFI_DEVICE_PATH *devpath; EFI_BLOCK_IO *blkio; EFI_STATUS status; pdinfo_t *pd; if (!STAILQ_EMPTY(&pdinfo)) return (0); sz = 0; hin = NULL; status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz, hin); if (status == EFI_BUFFER_TOO_SMALL) { hin = malloc(sz); if (hin == NULL) return (ENOMEM); status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz, hin); if (EFI_ERROR(status)) free(hin); } if (EFI_ERROR(status)) return (efi_status_to_errno(status)); nin = sz / sizeof(*hin); #ifdef EFIPART_DEBUG printf("%s: Got %d BLOCK IO MEDIA handle(s)\n", __func__, nin); #endif for (i = 0; i < nin; i++) { /* * Get devpath and open protocol. * We should not get errors here */ if ((devpath = efi_lookup_devpath(hin[i])) == NULL) continue; status = OpenProtocolByHandle(hin[i], &blkio_guid, (void **)&blkio); if (EFI_ERROR(status)) { printf("error %lu\n", EFI_ERROR_CODE(status)); continue; } if (efipart_ignore_device(hin[i], blkio, devpath)) continue; /* This is bad. */ if ((pd = calloc(1, sizeof(*pd))) == NULL) { printf("efipart_inithandles: Out of memory.\n"); free(hin); return (ENOMEM); } STAILQ_INIT(&pd->pd_part); pd->pd_handle = hin[i]; pd->pd_devpath = devpath; pd->pd_blkio = blkio; STAILQ_INSERT_TAIL(&pdinfo, pd, pd_link); } /* * Walk pdinfo and set parents based on device path. */ STAILQ_FOREACH(pd, &pdinfo, pd_link) { pd->pd_parent = efipart_find_parent(&pdinfo, pd->pd_devpath); } free(hin); return (0); } /* * Get node identified by pd_test() from plist. */ static pdinfo_t * efipart_get_pd(pdinfo_list_t *plist, pd_test_cb_t pd_test, pdinfo_t *data) { pdinfo_t *pd; STAILQ_FOREACH(pd, plist, pd_link) { if (pd_test(pd, data)) break; } return (pd); } static ACPI_HID_DEVICE_PATH * efipart_floppy(EFI_DEVICE_PATH *node) { ACPI_HID_DEVICE_PATH *acpi; if (DevicePathType(node) == ACPI_DEVICE_PATH && DevicePathSubType(node) == ACPI_DP) { acpi = (ACPI_HID_DEVICE_PATH *) node; if (acpi->HID == EISA_PNP_ID(PNP0604) || acpi->HID == EISA_PNP_ID(PNP0700) || acpi->HID == EISA_PNP_ID(PNP0701)) { return (acpi); } } return (NULL); } static bool efipart_testfd(pdinfo_t *fd, pdinfo_t *data __unused) { EFI_DEVICE_PATH *node; node = efi_devpath_last_node(fd->pd_devpath); if (node == NULL) return (false); if (efipart_floppy(node) != NULL) return (true); return (false); } static int efipart_initfd(void) { EFI_DEVICE_PATH *node; ACPI_HID_DEVICE_PATH *acpi; pdinfo_t *parent, *fd; while ((fd = efipart_get_pd(&pdinfo, efipart_testfd, NULL)) != NULL) { if ((node = efi_devpath_last_node(fd->pd_devpath)) == NULL) continue; if ((acpi = efipart_floppy(node)) == NULL) continue; STAILQ_REMOVE(&pdinfo, fd, pdinfo, pd_link); parent = fd->pd_parent; if (parent != NULL) { STAILQ_REMOVE(&pdinfo, parent, pdinfo, pd_link); parent->pd_alias = fd->pd_handle; parent->pd_unit = acpi->UID; free(fd); fd = parent; } else { fd->pd_unit = acpi->UID; } fd->pd_devsw = &efipart_fddev; STAILQ_INSERT_TAIL(&fdinfo, fd, pd_link); } bcache_add_dev(efiblk_pdinfo_count(&fdinfo)); return (0); } /* * Add or update entries with new handle data. */ static void efipart_cdinfo_add(pdinfo_t *cd) { pdinfo_t *parent, *pd, *last; if (cd == NULL) return; parent = cd->pd_parent; /* Make sure we have parent added */ efipart_cdinfo_add(parent); STAILQ_FOREACH(pd, &pdinfo, pd_link) { if (efi_devpath_match(pd->pd_devpath, cd->pd_devpath)) { STAILQ_REMOVE(&pdinfo, cd, pdinfo, pd_link); break; } } if (pd == NULL) { /* This device is already added. */ return; } if (parent != NULL) { last = STAILQ_LAST(&parent->pd_part, pdinfo, pd_link); if (last != NULL) cd->pd_unit = last->pd_unit + 1; else cd->pd_unit = 0; cd->pd_devsw = &efipart_cddev; STAILQ_INSERT_TAIL(&parent->pd_part, cd, pd_link); return; } last = STAILQ_LAST(&cdinfo, pdinfo, pd_link); if (last != NULL) cd->pd_unit = last->pd_unit + 1; else cd->pd_unit = 0; cd->pd_devsw = &efipart_cddev; STAILQ_INSERT_TAIL(&cdinfo, cd, pd_link); } static bool efipart_testcd(pdinfo_t *cd, pdinfo_t *data __unused) { EFI_DEVICE_PATH *node; node = efi_devpath_last_node(cd->pd_devpath); if (node == NULL) return (false); if (efipart_floppy(node) != NULL) return (false); if (DevicePathType(node) == MEDIA_DEVICE_PATH && DevicePathSubType(node) == MEDIA_CDROM_DP) { return (true); } /* cd drive without the media. */ if (cd->pd_blkio->Media->RemovableMedia && !cd->pd_blkio->Media->MediaPresent) { return (true); } return (false); } /* * Test if pd is parent for device. */ static bool efipart_testchild(pdinfo_t *dev, pdinfo_t *pd) { /* device with no parent. */ if (dev->pd_parent == NULL) return (false); if (efi_devpath_match(dev->pd_parent->pd_devpath, pd->pd_devpath)) { return (true); } return (false); } static int efipart_initcd(void) { pdinfo_t *cd; while ((cd = efipart_get_pd(&pdinfo, efipart_testcd, NULL)) != NULL) efipart_cdinfo_add(cd); /* Find all children of CD devices we did add above. */ STAILQ_FOREACH(cd, &cdinfo, pd_link) { pdinfo_t *child; for (child = efipart_get_pd(&pdinfo, efipart_testchild, cd); child != NULL; child = efipart_get_pd(&pdinfo, efipart_testchild, cd)) efipart_cdinfo_add(child); } bcache_add_dev(efiblk_pdinfo_count(&cdinfo)); return (0); } static void efipart_hdinfo_add_node(pdinfo_t *hd, EFI_DEVICE_PATH *node) { pdinfo_t *parent, *ptr; if (node == NULL) return; parent = hd->pd_parent; /* * If the node is not MEDIA_HARDDRIVE_DP, it is sub-partition. * This can happen with Vendor nodes, and since we do not know * the more about those nodes, we just count them. */ if (DevicePathSubType(node) != MEDIA_HARDDRIVE_DP) { ptr = STAILQ_LAST(&parent->pd_part, pdinfo, pd_link); if (ptr != NULL) hd->pd_unit = ptr->pd_unit + 1; else hd->pd_unit = 0; } else { hd->pd_unit = ((HARDDRIVE_DEVICE_PATH *)node)->PartitionNumber; } hd->pd_devsw = &efipart_hddev; STAILQ_INSERT_TAIL(&parent->pd_part, hd, pd_link); } /* * The MEDIA_FILEPATH_DP has device name. * From U-Boot sources it looks like names are in the form * of typeN:M, where type is interface type, N is disk id * and M is partition id. */ static void efipart_hdinfo_add_filepath(pdinfo_t *hd, FILEPATH_DEVICE_PATH *node) { char *pathname, *p; int len; pdinfo_t *last; last = STAILQ_LAST(&hdinfo, pdinfo, pd_link); if (last != NULL) hd->pd_unit = last->pd_unit + 1; else hd->pd_unit = 0; /* FILEPATH_DEVICE_PATH has 0 terminated string */ len = ucs2len(node->PathName); if ((pathname = malloc(len + 1)) == NULL) { printf("Failed to add disk, out of memory\n"); free(hd); return; } cpy16to8(node->PathName, pathname, len + 1); p = strchr(pathname, ':'); /* * Assume we are receiving handles in order, first disk handle, * then partitions for this disk. If this assumption proves * false, this code would need update. */ if (p == NULL) { /* no colon, add the disk */ hd->pd_devsw = &efipart_hddev; STAILQ_INSERT_TAIL(&hdinfo, hd, pd_link); free(pathname); return; } p++; /* skip the colon */ errno = 0; hd->pd_unit = (int)strtol(p, NULL, 0); if (errno != 0) { printf("Bad unit number for partition \"%s\"\n", pathname); free(pathname); free(hd); return; } /* * We should have disk registered, if not, we are receiving * handles out of order, and this code should be reworked * to create "blank" disk for partition, and to find the * disk based on PathName compares. */ if (last == NULL) { printf("BUG: No disk for partition \"%s\"\n", pathname); free(pathname); free(hd); return; } /* Add the partition. */ hd->pd_parent = last; hd->pd_devsw = &efipart_hddev; STAILQ_INSERT_TAIL(&last->pd_part, hd, pd_link); free(pathname); } static void efipart_hdinfo_add(pdinfo_t *hd) { pdinfo_t *parent, *pd, *last; EFI_DEVICE_PATH *node; if (hd == NULL) return; parent = hd->pd_parent; /* Make sure we have parent added */ efipart_hdinfo_add(parent); STAILQ_FOREACH(pd, &pdinfo, pd_link) { if (efi_devpath_match(pd->pd_devpath, hd->pd_devpath)) { STAILQ_REMOVE(&pdinfo, hd, pdinfo, pd_link); break; } } if (pd == NULL) { /* This device is already added. */ return; } if ((node = efi_devpath_last_node(hd->pd_devpath)) == NULL) return; if (DevicePathType(node) == MEDIA_DEVICE_PATH && DevicePathSubType(node) == MEDIA_FILEPATH_DP) { efipart_hdinfo_add_filepath(hd, (FILEPATH_DEVICE_PATH *)node); return; } if (parent != NULL) { efipart_hdinfo_add_node(hd, node); return; } last = STAILQ_LAST(&hdinfo, pdinfo, pd_link); if (last != NULL) hd->pd_unit = last->pd_unit + 1; else hd->pd_unit = 0; /* Add the disk. */ hd->pd_devsw = &efipart_hddev; STAILQ_INSERT_TAIL(&hdinfo, hd, pd_link); } static bool efipart_testhd(pdinfo_t *hd, pdinfo_t *data __unused) { if (efipart_testfd(hd, NULL)) return (false); if (efipart_testcd(hd, NULL)) return (false); /* Anything else must be HD. */ return (true); } static int efipart_inithd(void) { pdinfo_t *hd; while ((hd = efipart_get_pd(&pdinfo, efipart_testhd, NULL)) != NULL) efipart_hdinfo_add(hd); bcache_add_dev(efiblk_pdinfo_count(&hdinfo)); return (0); } static int efipart_print_common(struct devsw *dev, pdinfo_list_t *pdlist, int verbose) { int ret = 0; EFI_BLOCK_IO *blkio; EFI_STATUS status; EFI_HANDLE h; pdinfo_t *pd; CHAR16 *text; struct disk_devdesc pd_dev; char line[80]; if (STAILQ_EMPTY(pdlist)) return (0); printf("%s devices:", dev->dv_name); if ((ret = pager_output("\n")) != 0) return (ret); STAILQ_FOREACH(pd, pdlist, pd_link) { h = pd->pd_handle; if (verbose) { /* Output the device path. */ text = efi_devpath_name(efi_lookup_devpath(h)); if (text != NULL) { printf(" %S", text); efi_free_devpath_name(text); if ((ret = pager_output("\n")) != 0) break; } } snprintf(line, sizeof(line), " %s%d", dev->dv_name, pd->pd_unit); printf("%s:", line); status = OpenProtocolByHandle(h, &blkio_guid, (void **)&blkio); if (!EFI_ERROR(status)) { printf(" %llu", blkio->Media->LastBlock == 0? 0: (unsigned long long) (blkio->Media->LastBlock + 1)); if (blkio->Media->LastBlock != 0) { printf(" X %u", blkio->Media->BlockSize); } printf(" blocks"); if (blkio->Media->MediaPresent) { if (blkio->Media->RemovableMedia) printf(" (removable)"); } else { printf(" (no media)"); } if ((ret = pager_output("\n")) != 0) break; if (!blkio->Media->MediaPresent) continue; pd->pd_blkio = blkio; pd_dev.dd.d_dev = dev; pd_dev.dd.d_unit = pd->pd_unit; pd_dev.d_slice = D_SLICENONE; pd_dev.d_partition = D_PARTNONE; ret = disk_open(&pd_dev, blkio->Media->BlockSize * (blkio->Media->LastBlock + 1), blkio->Media->BlockSize); if (ret == 0) { ret = disk_print(&pd_dev, line, verbose); disk_close(&pd_dev); if (ret != 0) return (ret); } else { /* Do not fail from disk_open() */ ret = 0; } } else { if ((ret = pager_output("\n")) != 0) break; } } return (ret); } static int efipart_printfd(int verbose) { return (efipart_print_common(&efipart_fddev, &fdinfo, verbose)); } static int efipart_printcd(int verbose) { return (efipart_print_common(&efipart_cddev, &cdinfo, verbose)); } static int efipart_printhd(int verbose) { return (efipart_print_common(&efipart_hddev, &hdinfo, verbose)); } static int efipart_open(struct open_file *f, ...) { va_list args; struct disk_devdesc *dev; pdinfo_t *pd; EFI_BLOCK_IO *blkio; EFI_STATUS status; va_start(args, f); dev = va_arg(args, struct disk_devdesc *); va_end(args); if (dev == NULL) return (EINVAL); pd = efiblk_get_pdinfo((struct devdesc *)dev); if (pd == NULL) return (EIO); if (pd->pd_blkio == NULL) { status = OpenProtocolByHandle(pd->pd_handle, &blkio_guid, (void **)&pd->pd_blkio); if (EFI_ERROR(status)) return (efi_status_to_errno(status)); } blkio = pd->pd_blkio; if (!blkio->Media->MediaPresent) return (EAGAIN); pd->pd_open++; if (pd->pd_bcache == NULL) pd->pd_bcache = bcache_allocate(); if (dev->dd.d_dev->dv_type == DEVT_DISK) { int rc; rc = disk_open(dev, blkio->Media->BlockSize * (blkio->Media->LastBlock + 1), blkio->Media->BlockSize); if (rc != 0) { pd->pd_open--; if (pd->pd_open == 0) { pd->pd_blkio = NULL; bcache_free(pd->pd_bcache); pd->pd_bcache = NULL; } } return (rc); } return (0); } static int efipart_close(struct open_file *f) { struct disk_devdesc *dev; pdinfo_t *pd; dev = (struct disk_devdesc *)(f->f_devdata); if (dev == NULL) return (EINVAL); pd = efiblk_get_pdinfo((struct devdesc *)dev); if (pd == NULL) return (EINVAL); pd->pd_open--; if (pd->pd_open == 0) { pd->pd_blkio = NULL; if (dev->dd.d_dev->dv_type != DEVT_DISK) { bcache_free(pd->pd_bcache); pd->pd_bcache = NULL; } } if (dev->dd.d_dev->dv_type == DEVT_DISK) return (disk_close(dev)); return (0); } static int efipart_ioctl(struct open_file *f, u_long cmd, void *data) { struct disk_devdesc *dev; pdinfo_t *pd; int rc; dev = (struct disk_devdesc *)(f->f_devdata); if (dev == NULL) return (EINVAL); pd = efiblk_get_pdinfo((struct devdesc *)dev); if (pd == NULL) return (EINVAL); if (dev->dd.d_dev->dv_type == DEVT_DISK) { rc = disk_ioctl(dev, cmd, data); if (rc != ENOTTY) return (rc); } switch (cmd) { case DIOCGSECTORSIZE: *(u_int *)data = pd->pd_blkio->Media->BlockSize; break; case DIOCGMEDIASIZE: *(uint64_t *)data = pd->pd_blkio->Media->BlockSize * (pd->pd_blkio->Media->LastBlock + 1); break; default: return (ENOTTY); } return (0); } /* * efipart_readwrite() * Internal equivalent of efipart_strategy(), which operates on the * media-native block size. This function expects all I/O requests * to be within the media size and returns an error if such is not * the case. */ static int efipart_readwrite(EFI_BLOCK_IO *blkio, int rw, daddr_t blk, daddr_t nblks, char *buf) { EFI_STATUS status; TSENTER(); if (blkio == NULL) return (ENXIO); if (blk < 0 || blk > blkio->Media->LastBlock) return (EIO); if ((blk + nblks - 1) > blkio->Media->LastBlock) return (EIO); switch (rw & F_MASK) { case F_READ: status = blkio->ReadBlocks(blkio, blkio->Media->MediaId, blk, nblks * blkio->Media->BlockSize, buf); break; case F_WRITE: if (blkio->Media->ReadOnly) return (EROFS); status = blkio->WriteBlocks(blkio, blkio->Media->MediaId, blk, nblks * blkio->Media->BlockSize, buf); break; default: return (ENOSYS); } if (EFI_ERROR(status)) { printf("%s: rw=%d, blk=%ju size=%ju status=%lu\n", __func__, rw, blk, nblks, EFI_ERROR_CODE(status)); } TSEXIT(); return (efi_status_to_errno(status)); } static int efipart_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf, size_t *rsize) { struct bcache_devdata bcd; struct disk_devdesc *dev; pdinfo_t *pd; dev = (struct disk_devdesc *)devdata; if (dev == NULL) return (EINVAL); pd = efiblk_get_pdinfo((struct devdesc *)dev); if (pd == NULL) return (EINVAL); if (pd->pd_blkio->Media->RemovableMedia && !pd->pd_blkio->Media->MediaPresent) return (ENXIO); bcd.dv_strategy = efipart_realstrategy; bcd.dv_devdata = devdata; bcd.dv_cache = pd->pd_bcache; if (dev->dd.d_dev->dv_type == DEVT_DISK) { daddr_t offset; offset = dev->d_offset * pd->pd_blkio->Media->BlockSize; offset /= 512; return (bcache_strategy(&bcd, rw, blk + offset, size, buf, rsize)); } return (bcache_strategy(&bcd, rw, blk, size, buf, rsize)); } static int efipart_realstrategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf, size_t *rsize) { struct disk_devdesc *dev = (struct disk_devdesc *)devdata; pdinfo_t *pd; EFI_BLOCK_IO *blkio; uint64_t off, disk_blocks, d_offset = 0; char *blkbuf; size_t blkoff, blksz, bio_size; unsigned ioalign; bool need_buf; int rc; uint64_t diskend, readstart; if (dev == NULL || blk < 0) return (EINVAL); pd = efiblk_get_pdinfo((struct devdesc *)dev); if (pd == NULL) return (EINVAL); blkio = pd->pd_blkio; if (blkio == NULL) return (ENXIO); if (size == 0 || (size % 512) != 0) return (EIO); off = blk * 512; /* * Get disk blocks, this value is either for whole disk or for * partition. */ disk_blocks = 0; if (dev->dd.d_dev->dv_type == DEVT_DISK) { if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) { /* DIOCGMEDIASIZE does return bytes. */ disk_blocks /= blkio->Media->BlockSize; } d_offset = dev->d_offset; } if (disk_blocks == 0) disk_blocks = blkio->Media->LastBlock + 1 - d_offset; /* make sure we don't read past disk end */ if ((off + size) / blkio->Media->BlockSize > d_offset + disk_blocks) { diskend = d_offset + disk_blocks; readstart = off / blkio->Media->BlockSize; if (diskend <= readstart) { if (rsize != NULL) *rsize = 0; return (EIO); } size = diskend - readstart; size = size * blkio->Media->BlockSize; } need_buf = true; /* Do we need bounce buffer? */ if ((size % blkio->Media->BlockSize == 0) && (off % blkio->Media->BlockSize == 0)) need_buf = false; /* Do we have IO alignment requirement? */ ioalign = blkio->Media->IoAlign; if (ioalign == 0) ioalign++; if (ioalign > 1 && (uintptr_t)buf != roundup2((uintptr_t)buf, ioalign)) need_buf = true; if (need_buf) { for (bio_size = BIO_BUFFER_SIZE; bio_size > 0; bio_size -= blkio->Media->BlockSize) { blkbuf = memalign(ioalign, bio_size); if (blkbuf != NULL) break; } } else { blkbuf = buf; bio_size = size; } if (blkbuf == NULL) return (ENOMEM); if (rsize != NULL) *rsize = size; rc = 0; blk = off / blkio->Media->BlockSize; blkoff = off % blkio->Media->BlockSize; while (size > 0) { size_t x = min(size, bio_size); if (x < blkio->Media->BlockSize) x = 1; else x /= blkio->Media->BlockSize; switch (rw & F_MASK) { case F_READ: blksz = blkio->Media->BlockSize * x - blkoff; if (size < blksz) blksz = size; rc = efipart_readwrite(blkio, rw, blk, x, blkbuf); if (rc != 0) goto error; if (need_buf) bcopy(blkbuf + blkoff, buf, blksz); break; case F_WRITE: rc = 0; if (blkoff != 0) { /* * We got offset to sector, read 1 sector to * blkbuf. */ x = 1; blksz = blkio->Media->BlockSize - blkoff; blksz = min(blksz, size); rc = efipart_readwrite(blkio, F_READ, blk, x, blkbuf); } else if (size < blkio->Media->BlockSize) { /* * The remaining block is not full * sector. Read 1 sector to blkbuf. */ x = 1; blksz = size; rc = efipart_readwrite(blkio, F_READ, blk, x, blkbuf); } else { /* We can write full sector(s). */ blksz = blkio->Media->BlockSize * x; } if (rc != 0) goto error; /* * Put your Data In, Put your Data out, * Put your Data In, and shake it all about */ if (need_buf) bcopy(buf, blkbuf + blkoff, blksz); rc = efipart_readwrite(blkio, F_WRITE, blk, x, blkbuf); if (rc != 0) goto error; break; default: /* DO NOTHING */ rc = EROFS; goto error; } blkoff = 0; buf += blksz; size -= blksz; blk += x; } error: if (rsize != NULL) *rsize -= size; if (need_buf) free(blkbuf); return (rc); } diff --git a/stand/i386/libi386/biosdisk.c b/stand/i386/libi386/biosdisk.c index bc819d8e312e..87c8c47844b0 100644 --- a/stand/i386/libi386/biosdisk.c +++ b/stand/i386/libi386/biosdisk.c @@ -1,1371 +1,1372 @@ /*- * Copyright (c) 1998 Michael Smith * Copyright (c) 2012 Andrey V. Elsukov * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); /* * BIOS disk device handling. * * Ideas and algorithms from: * * - NetBSD libi386/biosdisk.c * - FreeBSD biosboot/disk.c * */ #include #include #include #include #include #include #include #include #include #include #include "disk.h" #include "libi386.h" #define BIOS_NUMDRIVES 0x475 #define BIOSDISK_SECSIZE 512 #define BUFSIZE (1 * BIOSDISK_SECSIZE) #define DT_ATAPI 0x10 /* disk type for ATAPI floppies */ #define WDMAJOR 0 /* major numbers for devices we frontend for */ #define WFDMAJOR 1 #define FDMAJOR 2 #define DAMAJOR 4 #define ACDMAJOR 117 #define CDMAJOR 15 /* * INT13 commands */ #define CMD_RESET 0x0000 #define CMD_READ_CHS 0x0200 #define CMD_WRITE_CHS 0x0300 #define CMD_READ_PARAM 0x0800 #define CMD_DRIVE_TYPE 0x1500 #define CMD_CHECK_EDD 0x4100 #define CMD_READ_LBA 0x4200 #define CMD_WRITE_LBA 0x4300 #define CMD_EXT_PARAM 0x4800 #define CMD_CD_GET_STATUS 0x4b01 #define DISK_BIOS 0x13 #ifdef DISK_DEBUG #define DPRINTF(fmt, args...) printf("%s: " fmt "\n", __func__, ## args) #else #define DPRINTF(fmt, args...) ((void)0) #endif struct specification_packet { uint8_t sp_size; uint8_t sp_bootmedia; uint8_t sp_drive; uint8_t sp_controller; uint32_t sp_lba; uint16_t sp_devicespec; uint16_t sp_buffersegment; uint16_t sp_loadsegment; uint16_t sp_sectorcount; uint16_t sp_cylsec; uint8_t sp_head; uint8_t sp_dummy[16]; /* Avoid memory corruption */ }; /* * List of BIOS devices, translation from disk unit number to * BIOS unit number. */ typedef struct bdinfo { STAILQ_ENTRY(bdinfo) bd_link; /* link in device list */ int bd_unit; /* BIOS unit number */ int bd_cyl; /* BIOS geometry */ int bd_hds; int bd_sec; int bd_flags; #define BD_MODEINT13 0x0000 #define BD_MODEEDD1 0x0001 #define BD_MODEEDD3 0x0002 #define BD_MODEEDD (BD_MODEEDD1 | BD_MODEEDD3) #define BD_MODEMASK 0x0003 #define BD_FLOPPY 0x0004 #define BD_CDROM 0x0008 #define BD_NO_MEDIA 0x0010 int bd_type; /* BIOS 'drive type' (floppy only) */ uint16_t bd_sectorsize; /* Sector size */ uint64_t bd_sectors; /* Disk size */ int bd_open; /* reference counter */ void *bd_bcache; /* buffer cache data */ } bdinfo_t; #define BD_RD 0 #define BD_WR 1 typedef STAILQ_HEAD(bdinfo_list, bdinfo) bdinfo_list_t; static bdinfo_list_t fdinfo = STAILQ_HEAD_INITIALIZER(fdinfo); static bdinfo_list_t cdinfo = STAILQ_HEAD_INITIALIZER(cdinfo); static bdinfo_list_t hdinfo = STAILQ_HEAD_INITIALIZER(hdinfo); static void bd_io_workaround(bdinfo_t *); static int bd_io(struct disk_devdesc *, bdinfo_t *, daddr_t, int, caddr_t, int); static bool bd_int13probe(bdinfo_t *); static int bd_init(void); static int cd_init(void); static int fd_init(void); static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size, char *buf, size_t *rsize); static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size, char *buf, size_t *rsize); static int bd_open(struct open_file *f, ...); static int bd_close(struct open_file *f); static int bd_ioctl(struct open_file *f, u_long cmd, void *data); static int bd_print(int verbose); static int cd_print(int verbose); static int fd_print(int verbose); static void bd_reset_disk(int); static int bd_get_diskinfo_std(struct bdinfo *); struct devsw biosfd = { .dv_name = "fd", .dv_type = DEVT_FD, .dv_init = fd_init, .dv_strategy = bd_strategy, .dv_open = bd_open, .dv_close = bd_close, .dv_ioctl = bd_ioctl, .dv_print = fd_print, .dv_cleanup = nullsys, }; struct devsw bioscd = { .dv_name = "cd", .dv_type = DEVT_CD, .dv_init = cd_init, .dv_strategy = bd_strategy, .dv_open = bd_open, .dv_close = bd_close, .dv_ioctl = bd_ioctl, .dv_print = cd_print, .dv_cleanup = nullsys, }; struct devsw bioshd = { .dv_name = "disk", .dv_type = DEVT_DISK, .dv_init = bd_init, .dv_strategy = bd_strategy, .dv_open = bd_open, .dv_close = bd_close, .dv_ioctl = bd_ioctl, .dv_print = bd_print, .dv_cleanup = nullsys, .dv_fmtdev = disk_fmtdev, + .dv_parsedev = disk_parsedev, }; static bdinfo_list_t * bd_get_bdinfo_list(struct devsw *dev) { if (dev->dv_type == DEVT_DISK) return (&hdinfo); if (dev->dv_type == DEVT_CD) return (&cdinfo); if (dev->dv_type == DEVT_FD) return (&fdinfo); return (NULL); } /* XXX this gets called way way too often, investigate */ static bdinfo_t * bd_get_bdinfo(struct devdesc *dev) { bdinfo_list_t *bdi; bdinfo_t *bd = NULL; int unit; bdi = bd_get_bdinfo_list(dev->d_dev); if (bdi == NULL) return (bd); unit = 0; STAILQ_FOREACH(bd, bdi, bd_link) { if (unit == dev->d_unit) return (bd); unit++; } return (bd); } /* * Translate between BIOS device numbers and our private unit numbers. */ int bd_bios2unit(int biosdev) { bdinfo_list_t *bdi[] = { &fdinfo, &cdinfo, &hdinfo, NULL }; bdinfo_t *bd; int i, unit; DPRINTF("looking for bios device 0x%x", biosdev); for (i = 0; bdi[i] != NULL; i++) { unit = 0; STAILQ_FOREACH(bd, bdi[i], bd_link) { if (bd->bd_unit == biosdev) { DPRINTF("bd unit %d is BIOS device 0x%x", unit, bd->bd_unit); return (unit); } unit++; } } return (-1); } int bd_unit2bios(struct i386_devdesc *dev) { bdinfo_list_t *bdi; bdinfo_t *bd; int unit; bdi = bd_get_bdinfo_list(dev->dd.d_dev); if (bdi == NULL) return (-1); unit = 0; STAILQ_FOREACH(bd, bdi, bd_link) { if (unit == dev->dd.d_unit) return (bd->bd_unit); unit++; } return (-1); } /* * Use INT13 AH=15 - Read Drive Type. */ static int fd_count(void) { int drive; for (drive = 0; drive < MAXBDDEV; drive++) { bd_reset_disk(drive); v86.ctl = V86_FLAGS; v86.addr = DISK_BIOS; v86.eax = CMD_DRIVE_TYPE; v86.edx = drive; v86int(); if (V86_CY(v86.efl)) break; if ((v86.eax & 0x300) == 0) break; } return (drive); } /* * Quiz the BIOS for disk devices, save a little info about them. */ static int fd_init(void) { int unit, numfd; bdinfo_t *bd; numfd = fd_count(); for (unit = 0; unit < numfd; unit++) { if ((bd = calloc(1, sizeof(*bd))) == NULL) break; bd->bd_sectorsize = BIOSDISK_SECSIZE; bd->bd_flags = BD_FLOPPY; bd->bd_unit = unit; /* Use std diskinfo for floppy drive */ if (bd_get_diskinfo_std(bd) != 0) { free(bd); break; } if (bd->bd_sectors == 0) bd->bd_flags |= BD_NO_MEDIA; printf("BIOS drive %c: is %s%d\n", ('A' + unit), biosfd.dv_name, unit); STAILQ_INSERT_TAIL(&fdinfo, bd, bd_link); } bcache_add_dev(unit); return (0); } static int bd_init(void) { int base, unit; bdinfo_t *bd; TSENTER(); base = 0x80; for (unit = 0; unit < *(unsigned char *)PTOV(BIOS_NUMDRIVES); unit++) { /* * Check the BIOS equipment list for number of fixed disks. */ if ((bd = calloc(1, sizeof(*bd))) == NULL) break; bd->bd_unit = base + unit; if (!bd_int13probe(bd)) { free(bd); break; } printf("BIOS drive %c: is %s%d\n", ('C' + unit), bioshd.dv_name, unit); STAILQ_INSERT_TAIL(&hdinfo, bd, bd_link); } bcache_add_dev(unit); TSEXIT(); return (0); } /* * We can't quiz, we have to be told what device to use, so this function * doesn't do anything. Instead, the loader calls bc_add() with the BIOS * device number to add. */ static int cd_init(void) { return (0); } /* * Information from bootable CD-ROM. */ static int bd_get_diskinfo_cd(struct bdinfo *bd) { struct specification_packet bc_sp; int ret = -1; (void) memset(&bc_sp, 0, sizeof (bc_sp)); /* Set sp_size as per specification. */ bc_sp.sp_size = sizeof (bc_sp) - sizeof (bc_sp.sp_dummy); v86.ctl = V86_FLAGS; v86.addr = DISK_BIOS; v86.eax = CMD_CD_GET_STATUS; v86.edx = bd->bd_unit; v86.ds = VTOPSEG(&bc_sp); v86.esi = VTOPOFF(&bc_sp); v86int(); if ((v86.eax & 0xff00) == 0 && bc_sp.sp_drive == bd->bd_unit) { bd->bd_cyl = ((bc_sp.sp_cylsec & 0xc0) << 2) + ((bc_sp.sp_cylsec & 0xff00) >> 8) + 1; bd->bd_sec = bc_sp.sp_cylsec & 0x3f; bd->bd_hds = bc_sp.sp_head + 1; bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec; if (bc_sp.sp_bootmedia & 0x0F) { /* Floppy or hard-disk emulation */ bd->bd_sectorsize = BIOSDISK_SECSIZE; return (-1); } else { bd->bd_sectorsize = 2048; bd->bd_flags = BD_MODEEDD | BD_CDROM; ret = 0; } } /* * If this is the boot_drive, default to non-emulation bootable CD-ROM. */ if (ret != 0 && bd->bd_unit >= 0x88) { bd->bd_cyl = 0; bd->bd_hds = 1; bd->bd_sec = 15; bd->bd_sectorsize = 2048; bd->bd_flags = BD_MODEEDD | BD_CDROM; bd->bd_sectors = 0; ret = 0; } /* * Note we can not use bd_get_diskinfo_ext() nor bd_get_diskinfo_std() * here - some systems do get hung with those. */ /* * Still no size? use 7.961GB. The size does not really matter * as long as it is reasonably large to make our reads to pass * the sector count check. */ if (bd->bd_sectors == 0) bd->bd_sectors = 4173824; return (ret); } int bc_add(int biosdev) { bdinfo_t *bd; int nbcinfo = 0; if (!STAILQ_EMPTY(&cdinfo)) return (-1); if ((bd = calloc(1, sizeof(*bd))) == NULL) return (-1); bd->bd_unit = biosdev; if (bd_get_diskinfo_cd(bd) < 0) { free(bd); return (-1); } STAILQ_INSERT_TAIL(&cdinfo, bd, bd_link); printf("BIOS CD is cd%d\n", nbcinfo); nbcinfo++; bcache_add_dev(nbcinfo); /* register cd device in bcache */ return(0); } /* * Return EDD version or 0 if EDD is not supported on this drive. */ static int bd_check_extensions(int unit) { /* do not use ext calls for floppy devices */ if (unit < 0x80) return (0); /* Determine if we can use EDD with this device. */ v86.ctl = V86_FLAGS; v86.addr = DISK_BIOS; v86.eax = CMD_CHECK_EDD; v86.edx = unit; v86.ebx = EDD_QUERY_MAGIC; v86int(); if (V86_CY(v86.efl) || /* carry set */ (v86.ebx & 0xffff) != EDD_INSTALLED) /* signature */ return (0); /* extended disk access functions (AH=42h-44h,47h,48h) supported */ if ((v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0) return (0); return ((v86.eax >> 8) & 0xff); } static void bd_reset_disk(int unit) { /* reset disk */ v86.ctl = V86_FLAGS; v86.addr = DISK_BIOS; v86.eax = CMD_RESET; v86.edx = unit; v86int(); } /* * Read CHS info. Return 0 on success, error otherwise. */ static int bd_get_diskinfo_std(struct bdinfo *bd) { bzero(&v86, sizeof(v86)); v86.ctl = V86_FLAGS; v86.addr = DISK_BIOS; v86.eax = CMD_READ_PARAM; v86.edx = bd->bd_unit; v86int(); if (V86_CY(v86.efl) && ((v86.eax & 0xff00) != 0)) return ((v86.eax & 0xff00) >> 8); /* return custom error on absurd sector number */ if ((v86.ecx & 0x3f) == 0) return (0x60); bd->bd_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1; /* Convert max head # -> # of heads */ bd->bd_hds = ((v86.edx & 0xff00) >> 8) + 1; bd->bd_sec = v86.ecx & 0x3f; bd->bd_type = v86.ebx; bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec; return (0); } /* * Read EDD info. Return 0 on success, error otherwise. * * Avoid stack corruption on some systems by adding extra bytes to * params block. */ static int bd_get_diskinfo_ext(struct bdinfo *bd) { struct disk_params { struct edd_params head; struct edd_device_path_v3 device_path; uint8_t dummy[16]; } __packed dparams; struct edd_params *params; uint64_t total; params = &dparams.head; /* Get disk params */ bzero(&dparams, sizeof(dparams)); params->len = sizeof(struct edd_params_v3); v86.ctl = V86_FLAGS; v86.addr = DISK_BIOS; v86.eax = CMD_EXT_PARAM; v86.edx = bd->bd_unit; v86.ds = VTOPSEG(&dparams); v86.esi = VTOPOFF(&dparams); v86int(); if (V86_CY(v86.efl) && ((v86.eax & 0xff00) != 0)) return ((v86.eax & 0xff00) >> 8); /* * Sector size must be a multiple of 512 bytes. * An alternate test would be to check power of 2, * powerof2(params.sector_size). * 16K is largest read buffer we can use at this time. */ if (params->sector_size >= 512 && params->sector_size <= 16384 && (params->sector_size % BIOSDISK_SECSIZE) == 0) bd->bd_sectorsize = params->sector_size; bd->bd_cyl = params->cylinders; bd->bd_hds = params->heads; bd->bd_sec = params->sectors_per_track; if (params->sectors != 0) { total = params->sectors; } else { total = (uint64_t)params->cylinders * params->heads * params->sectors_per_track; } bd->bd_sectors = total; return (0); } /* * Try to detect a device supported by the legacy int13 BIOS */ static bool bd_int13probe(bdinfo_t *bd) { int edd, ret; bd->bd_flags &= ~BD_NO_MEDIA; if ((bd->bd_flags & BD_CDROM) != 0) { return (bd_get_diskinfo_cd(bd) == 0); } edd = bd_check_extensions(bd->bd_unit); if (edd == 0) bd->bd_flags |= BD_MODEINT13; else if (edd < 0x30) bd->bd_flags |= BD_MODEEDD1; else bd->bd_flags |= BD_MODEEDD3; /* Default sector size */ if (bd->bd_sectorsize == 0) bd->bd_sectorsize = BIOSDISK_SECSIZE; /* * Test if the floppy device is present, so we can avoid receiving * bogus information from bd_get_diskinfo_std(). */ if (bd->bd_unit < 0x80) { /* reset disk */ bd_reset_disk(bd->bd_unit); /* Get disk type */ v86.ctl = V86_FLAGS; v86.addr = DISK_BIOS; v86.eax = CMD_DRIVE_TYPE; v86.edx = bd->bd_unit; v86int(); if (V86_CY(v86.efl) || (v86.eax & 0x300) == 0) return (false); } ret = 1; if (edd != 0) ret = bd_get_diskinfo_ext(bd); if (ret != 0 || bd->bd_sectors == 0) ret = bd_get_diskinfo_std(bd); if (ret != 0 && bd->bd_unit < 0x80) { /* Set defaults for 1.44 floppy */ bd->bd_cyl = 80; bd->bd_hds = 2; bd->bd_sec = 18; bd->bd_sectors = 2880; /* Since we are there, there most likely is no media */ bd->bd_flags |= BD_NO_MEDIA; ret = 0; } if (ret != 0) { if (bd->bd_sectors != 0 && edd != 0) { bd->bd_sec = 63; bd->bd_hds = 255; bd->bd_cyl = (bd->bd_sectors + bd->bd_sec * bd->bd_hds - 1) / bd->bd_sec * bd->bd_hds; } else { const char *dv_name; if ((bd->bd_flags & BD_FLOPPY) != 0) dv_name = biosfd.dv_name; else dv_name = bioshd.dv_name; printf("Can not get information about %s unit %#x\n", dv_name, bd->bd_unit); return (false); } } if (bd->bd_sec == 0) bd->bd_sec = 63; if (bd->bd_hds == 0) bd->bd_hds = 255; if (bd->bd_sectors == 0) bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec; DPRINTF("unit 0x%x geometry %d/%d/%d\n", bd->bd_unit, bd->bd_cyl, bd->bd_hds, bd->bd_sec); return (true); } static int bd_count(bdinfo_list_t *bdi) { bdinfo_t *bd; int i; i = 0; STAILQ_FOREACH(bd, bdi, bd_link) i++; return (i); } /* * Print information about disks */ static int bd_print_common(struct devsw *dev, bdinfo_list_t *bdi, int verbose) { char line[80]; struct disk_devdesc devd; bdinfo_t *bd; int i, ret = 0; char drive; if (STAILQ_EMPTY(bdi)) return (0); printf("%s devices:", dev->dv_name); if ((ret = pager_output("\n")) != 0) return (ret); i = -1; STAILQ_FOREACH(bd, bdi, bd_link) { i++; switch (dev->dv_type) { case DEVT_FD: drive = 'A'; break; case DEVT_CD: drive = 'C' + bd_count(&hdinfo); break; default: drive = 'C'; break; } snprintf(line, sizeof(line), " %s%d: BIOS drive %c (%s%ju X %u):\n", dev->dv_name, i, drive + i, (bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA ? "no media, " : "", (uintmax_t)bd->bd_sectors, bd->bd_sectorsize); if ((ret = pager_output(line)) != 0) break; if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA) continue; if (dev->dv_type != DEVT_DISK) continue; devd.dd.d_dev = dev; devd.dd.d_unit = i; devd.d_slice = D_SLICENONE; devd.d_partition = D_PARTNONE; if (disk_open(&devd, bd->bd_sectorsize * bd->bd_sectors, bd->bd_sectorsize) == 0) { snprintf(line, sizeof(line), " %s%d", dev->dv_name, i); ret = disk_print(&devd, line, verbose); disk_close(&devd); if (ret != 0) break; } } return (ret); } static int fd_print(int verbose) { return (bd_print_common(&biosfd, &fdinfo, verbose)); } static int bd_print(int verbose) { return (bd_print_common(&bioshd, &hdinfo, verbose)); } static int cd_print(int verbose) { return (bd_print_common(&bioscd, &cdinfo, verbose)); } /* * Read disk size from partition. * This is needed to work around buggy BIOS systems returning * wrong (truncated) disk media size. * During bd_probe() we tested if the multiplication of bd_sectors * would overflow so it should be safe to perform here. */ static uint64_t bd_disk_get_sectors(struct disk_devdesc *dev) { bdinfo_t *bd; struct disk_devdesc disk; uint64_t size; bd = bd_get_bdinfo(&dev->dd); if (bd == NULL) return (0); disk.dd.d_dev = dev->dd.d_dev; disk.dd.d_unit = dev->dd.d_unit; disk.d_slice = D_SLICENONE; disk.d_partition = D_PARTNONE; disk.d_offset = 0; size = bd->bd_sectors * bd->bd_sectorsize; if (disk_open(&disk, size, bd->bd_sectorsize) == 0) { (void) disk_ioctl(&disk, DIOCGMEDIASIZE, &size); disk_close(&disk); } return (size / bd->bd_sectorsize); } /* * Attempt to open the disk described by (dev) for use by (f). * * Note that the philosophy here is "give them exactly what * they ask for". This is necessary because being too "smart" * about what the user might want leads to complications. * (eg. given no slice or partition value, with a disk that is * sliced - are they after the first BSD slice, or the DOS * slice before it?) */ static int bd_open(struct open_file *f, ...) { bdinfo_t *bd; struct disk_devdesc *dev; va_list ap; int rc; TSENTER(); va_start(ap, f); dev = va_arg(ap, struct disk_devdesc *); va_end(ap); bd = bd_get_bdinfo(&dev->dd); if (bd == NULL) return (EIO); if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA) { if (!bd_int13probe(bd)) return (EIO); if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA) return (EIO); } if (bd->bd_bcache == NULL) bd->bd_bcache = bcache_allocate(); if (bd->bd_open == 0) bd->bd_sectors = bd_disk_get_sectors(dev); bd->bd_open++; rc = 0; if (dev->dd.d_dev->dv_type == DEVT_DISK) { rc = disk_open(dev, bd->bd_sectors * bd->bd_sectorsize, bd->bd_sectorsize); if (rc != 0) { bd->bd_open--; if (bd->bd_open == 0) { bcache_free(bd->bd_bcache); bd->bd_bcache = NULL; } } } TSEXIT(); return (rc); } static int bd_close(struct open_file *f) { struct disk_devdesc *dev; bdinfo_t *bd; int rc = 0; dev = (struct disk_devdesc *)f->f_devdata; bd = bd_get_bdinfo(&dev->dd); if (bd == NULL) return (EIO); bd->bd_open--; if (bd->bd_open == 0) { bcache_free(bd->bd_bcache); bd->bd_bcache = NULL; } if (dev->dd.d_dev->dv_type == DEVT_DISK) rc = disk_close(dev); return (rc); } static int bd_ioctl(struct open_file *f, u_long cmd, void *data) { bdinfo_t *bd; struct disk_devdesc *dev; int rc; dev = (struct disk_devdesc *)f->f_devdata; bd = bd_get_bdinfo(&dev->dd); if (bd == NULL) return (EIO); if (dev->dd.d_dev->dv_type == DEVT_DISK) { rc = disk_ioctl(dev, cmd, data); if (rc != ENOTTY) return (rc); } switch (cmd) { case DIOCGSECTORSIZE: *(uint32_t *)data = bd->bd_sectorsize; break; case DIOCGMEDIASIZE: *(uint64_t *)data = bd->bd_sectors * bd->bd_sectorsize; break; default: return (ENOTTY); } return (0); } static int bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize) { bdinfo_t *bd; struct bcache_devdata bcd; struct disk_devdesc *dev; daddr_t offset; dev = (struct disk_devdesc *)devdata; bd = bd_get_bdinfo(&dev->dd); if (bd == NULL) return (EINVAL); bcd.dv_strategy = bd_realstrategy; bcd.dv_devdata = devdata; bcd.dv_cache = bd->bd_bcache; offset = 0; if (dev->dd.d_dev->dv_type == DEVT_DISK) { offset = dev->d_offset * bd->bd_sectorsize; offset /= BIOSDISK_SECSIZE; } return (bcache_strategy(&bcd, rw, dblk + offset, size, buf, rsize)); } static int bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize) { struct disk_devdesc *dev = (struct disk_devdesc *)devdata; bdinfo_t *bd; uint64_t disk_blocks, offset, d_offset; size_t blks, blkoff, bsize, bio_size, rest; caddr_t bbuf = NULL; int rc; bd = bd_get_bdinfo(&dev->dd); if (bd == NULL || (bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA) return (EIO); /* * First make sure the IO size is a multiple of 512 bytes. While we do * process partial reads below, the strategy mechanism is built * assuming IO is a multiple of 512B blocks. If the request is not * a multiple of 512B blocks, it has to be some sort of bug. */ if (size == 0 || (size % BIOSDISK_SECSIZE) != 0) { printf("bd_strategy: %d bytes I/O not multiple of %d\n", size, BIOSDISK_SECSIZE); return (EIO); } DPRINTF("open_disk %p", dev); offset = dblk * BIOSDISK_SECSIZE; dblk = offset / bd->bd_sectorsize; blkoff = offset % bd->bd_sectorsize; /* * Check the value of the size argument. We do have quite small * heap (64MB), but we do not know good upper limit, so we check against * INT_MAX here. This will also protect us against possible overflows * while translating block count to bytes. */ if (size > INT_MAX) { DPRINTF("too large I/O: %zu bytes", size); return (EIO); } blks = size / bd->bd_sectorsize; if (blks == 0 || (size % bd->bd_sectorsize) != 0) blks++; if (dblk > dblk + blks) return (EIO); if (rsize) *rsize = 0; /* * Get disk blocks, this value is either for whole disk or for * partition. */ d_offset = 0; disk_blocks = 0; if (dev->dd.d_dev->dv_type == DEVT_DISK) { if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) { /* DIOCGMEDIASIZE does return bytes. */ disk_blocks /= bd->bd_sectorsize; } d_offset = dev->d_offset; } if (disk_blocks == 0) disk_blocks = bd->bd_sectors * (bd->bd_sectorsize / BIOSDISK_SECSIZE) - d_offset; /* Validate source block address. */ if (dblk < d_offset || dblk >= d_offset + disk_blocks) return (EIO); /* * Truncate if we are crossing disk or partition end. */ if (dblk + blks >= d_offset + disk_blocks) { blks = d_offset + disk_blocks - dblk; size = blks * bd->bd_sectorsize; DPRINTF("short I/O %d", blks); } bio_size = min(BIO_BUFFER_SIZE, size); while (bio_size > bd->bd_sectorsize) { bbuf = bio_alloc(bio_size); if (bbuf != NULL) break; bio_size -= bd->bd_sectorsize; } if (bbuf == NULL) { bio_size = V86_IO_BUFFER_SIZE; if (bio_size / bd->bd_sectorsize == 0) panic("BUG: Real mode buffer is too small"); /* Use alternate 4k buffer */ bbuf = PTOV(V86_IO_BUFFER); } rest = size; rc = 0; while (blks > 0) { int x = min(blks, bio_size / bd->bd_sectorsize); switch (rw & F_MASK) { case F_READ: DPRINTF("read %d from %lld to %p", x, dblk, buf); bsize = bd->bd_sectorsize * x - blkoff; if (rest < bsize) bsize = rest; if ((rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD)) != 0) { rc = EIO; goto error; } bcopy(bbuf + blkoff, buf, bsize); break; case F_WRITE : DPRINTF("write %d from %lld to %p", x, dblk, buf); if (blkoff != 0) { /* * We got offset to sector, read 1 sector to * bbuf. */ x = 1; bsize = bd->bd_sectorsize - blkoff; bsize = min(bsize, rest); rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD); } else if (rest < bd->bd_sectorsize) { /* * The remaining block is not full * sector. Read 1 sector to bbuf. */ x = 1; bsize = rest; rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD); } else { /* We can write full sector(s). */ bsize = bd->bd_sectorsize * x; } /* * Put your Data In, Put your Data out, * Put your Data In, and shake it all about */ bcopy(buf, bbuf + blkoff, bsize); if ((rc = bd_io(dev, bd, dblk, x, bbuf, BD_WR)) != 0) { rc = EIO; goto error; } break; default: /* DO NOTHING */ rc = EROFS; goto error; } blkoff = 0; buf += bsize; rest -= bsize; blks -= x; dblk += x; } if (rsize != NULL) *rsize = size; error: if (bbuf != PTOV(V86_IO_BUFFER)) bio_free(bbuf, bio_size); return (rc); } static int bd_edd_io(bdinfo_t *bd, daddr_t dblk, int blks, caddr_t dest, int dowrite) { static struct edd_packet packet; TSENTER(); packet.len = sizeof(struct edd_packet); packet.count = blks; packet.off = VTOPOFF(dest); packet.seg = VTOPSEG(dest); packet.lba = dblk; v86.ctl = V86_FLAGS; v86.addr = DISK_BIOS; if (dowrite == BD_WR) v86.eax = CMD_WRITE_LBA; /* maybe Write with verify 0x4302? */ else v86.eax = CMD_READ_LBA; v86.edx = bd->bd_unit; v86.ds = VTOPSEG(&packet); v86.esi = VTOPOFF(&packet); v86int(); if (V86_CY(v86.efl)) return (v86.eax >> 8); TSEXIT(); return (0); } static int bd_chs_io(bdinfo_t *bd, daddr_t dblk, int blks, caddr_t dest, int dowrite) { uint32_t x, bpc, cyl, hd, sec; TSENTER(); bpc = bd->bd_sec * bd->bd_hds; /* blocks per cylinder */ x = dblk; cyl = x / bpc; /* block # / blocks per cylinder */ x %= bpc; /* block offset into cylinder */ hd = x / bd->bd_sec; /* offset / blocks per track */ sec = x % bd->bd_sec; /* offset into track */ /* correct sector number for 1-based BIOS numbering */ sec++; if (cyl > 1023) { /* CHS doesn't support cylinders > 1023. */ return (1); } v86.ctl = V86_FLAGS; v86.addr = DISK_BIOS; if (dowrite == BD_WR) v86.eax = CMD_WRITE_CHS | blks; else v86.eax = CMD_READ_CHS | blks; v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec; v86.edx = (hd << 8) | bd->bd_unit; v86.es = VTOPSEG(dest); v86.ebx = VTOPOFF(dest); v86int(); if (V86_CY(v86.efl)) return (v86.eax >> 8); TSEXIT(); return (0); } static void bd_io_workaround(bdinfo_t *bd) { uint8_t buf[8 * 1024]; bd_edd_io(bd, 0xffffffff, 1, (caddr_t)buf, BD_RD); } static int bd_io(struct disk_devdesc *dev, bdinfo_t *bd, daddr_t dblk, int blks, caddr_t dest, int dowrite) { int result, retry; TSENTER(); /* Just in case some idiot actually tries to read/write -1 blocks... */ if (blks < 0) return (-1); /* * Workaround for a problem with some HP ProLiant BIOS failing to work * out the boot disk after installation. hrs and kuriyama discovered * this problem with an HP ProLiant DL320e Gen 8 with a 3TB HDD, and * discovered that an int13h call seems to cause a buffer overrun in * the bios. The problem is alleviated by doing an extra read before * the buggy read. It is not immediately known whether other models * are similarly affected. * Loop retrying the operation a couple of times. The BIOS * may also retry. */ if (dowrite == BD_RD && dblk >= 0x100000000) bd_io_workaround(bd); for (retry = 0; retry < 3; retry++) { if (bd->bd_flags & BD_MODEEDD) result = bd_edd_io(bd, dblk, blks, dest, dowrite); else result = bd_chs_io(bd, dblk, blks, dest, dowrite); if (result == 0) { if (bd->bd_flags & BD_NO_MEDIA) bd->bd_flags &= ~BD_NO_MEDIA; break; } bd_reset_disk(bd->bd_unit); /* * Error codes: * 20h controller failure * 31h no media in drive (IBM/MS INT 13 extensions) * 80h no media in drive, VMWare (Fusion) * There is no reason to repeat the IO with errors above. */ if (result == 0x20 || result == 0x31 || result == 0x80) { bd->bd_flags |= BD_NO_MEDIA; break; } } if (result != 0 && (bd->bd_flags & BD_NO_MEDIA) == 0) { if (dowrite == BD_WR) { printf("%s%d: Write %d sector(s) from %p (0x%x) " "to %lld: 0x%x\n", dev->dd.d_dev->dv_name, dev->dd.d_unit, blks, dest, VTOP(dest), dblk, result); } else { printf("%s%d: Read %d sector(s) from %lld to %p " "(0x%x): 0x%x\n", dev->dd.d_dev->dv_name, dev->dd.d_unit, blks, dblk, dest, VTOP(dest), result); } } TSEXIT(); return (result); } /* * Return a suitable dev_t value for (dev). * * In the case where it looks like (dev) is a SCSI disk, we allow the number of * IDE disks to be specified in $num_ide_disks. There should be a Better Way. */ int bd_getdev(struct i386_devdesc *d) { struct disk_devdesc *dev; bdinfo_t *bd; int biosdev; int major; int rootdev; char *nip, *cp; int i, unit, slice, partition; /* XXX: Assume partition 'a'. */ slice = 0; partition = 0; dev = (struct disk_devdesc *)d; bd = bd_get_bdinfo(&dev->dd); if (bd == NULL) return (-1); biosdev = bd_unit2bios(d); DPRINTF("unit %d BIOS device %d", dev->dd.d_unit, biosdev); if (biosdev == -1) /* not a BIOS device */ return (-1); if (dev->dd.d_dev->dv_type == DEVT_DISK) { if (disk_open(dev, bd->bd_sectors * bd->bd_sectorsize, bd->bd_sectorsize) != 0) /* oops, not a viable device */ return (-1); else disk_close(dev); slice = dev->d_slice + 1; partition = dev->d_partition; } if (biosdev < 0x80) { /* floppy (or emulated floppy) or ATAPI device */ if (bd->bd_type == DT_ATAPI) { /* is an ATAPI disk */ major = WFDMAJOR; } else { /* is a floppy disk */ major = FDMAJOR; } } else { /* assume an IDE disk */ major = WDMAJOR; } /* default root disk unit number */ unit = biosdev & 0x7f; if (dev->dd.d_dev->dv_type == DEVT_CD) { /* * XXX: Need to examine device spec here to figure out if * SCSI or ATAPI. No idea on how to figure out device number. * All we can really pass to the kernel is what bus and device * on which bus we were booted from, which dev_t isn't well * suited to since those number don't match to unit numbers * very well. We may just need to engage in a hack where * we pass -C to the boot args if we are the boot device. */ major = ACDMAJOR; unit = 0; /* XXX */ } /* XXX a better kludge to set the root disk unit number */ if ((nip = getenv("root_disk_unit")) != NULL) { i = strtol(nip, &cp, 0); /* check for parse error */ if ((cp != nip) && (*cp == 0)) unit = i; } rootdev = MAKEBOOTDEV(major, slice, unit, partition); DPRINTF("dev is 0x%x\n", rootdev); return (rootdev); } diff --git a/stand/libofw/ofw_disk.c b/stand/libofw/ofw_disk.c index 5585b463c737..524bc7b77d5c 100644 --- a/stand/libofw/ofw_disk.c +++ b/stand/libofw/ofw_disk.c @@ -1,186 +1,187 @@ /*- * Copyright (C) 2000 Benno Rice. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); /* * Disk I/O routines using Open Firmware */ #include #include #include #include #include #include "disk.h" #include "libofw.h" static int ofwd_init(void); static int ofwd_strategy(void *devdata, int flag, daddr_t dblk, size_t size, char *buf, size_t *rsize); static int ofwd_open(struct open_file *f, ...); static int ofwd_close(struct open_file *f); static int ofwd_ioctl(struct open_file *f, u_long cmd, void *data); static int ofwd_print(int verbose); struct devsw ofwdisk = { .dv_name = "block", .dv_type = DEVT_DISK, .dv_init = ofwd_init, .dv_strategy = ofwd_strategy, .dv_open = ofwd_open, .dv_close = ofwd_close, .dv_ioctl = ofwd_ioctl, .dv_print = ofwd_print, .dv_cleanup = nullsys, .dv_fmtdev = disk_fmtdev, + .dv_parsedev = disk_parsedev, }; /* * We're not guaranteed to be able to open a device more than once and there * is no OFW standard method to determine whether a device is already opened. * Opening a device multiple times simultaneously happens to work with most * OFW block device drivers but triggers a trap with at least the driver for * the on-board controllers of Sun Fire V100 and Ultra 1. Upper layers and MI * code expect to be able to open a device more than once however. Given that * different partitions of the same device might be opened at the same time as * done by ZFS, we can't generally just keep track of the opened devices and * reuse the instance handle when asked to open an already opened device. So * the best we can do is to cache the lastly used device path and close and * open devices in ofwd_strategy() as needed. */ static struct ofw_devdesc *kdp; static int ofwd_init(void) { return (0); } static int ofwd_strategy(void *devdata, int flag __unused, daddr_t dblk, size_t size, char *buf, size_t *rsize) { struct ofw_devdesc *dp = (struct ofw_devdesc *)devdata; daddr_t pos; int n; if (dp != kdp) { if (kdp != NULL) { #if !defined(__powerpc__) OF_close(kdp->d_handle); #endif kdp = NULL; } if ((dp->d_handle = OF_open(dp->d_path)) == -1) return (ENOENT); kdp = dp; } pos = dblk * 512; do { if (OF_seek(dp->d_handle, pos) < 0) return (EIO); n = OF_read(dp->d_handle, buf, size); if (n < 0 && n != -2) return (EIO); } while (n == -2); *rsize = size; return (0); } static int ofwd_open(struct open_file *f, ...) { struct ofw_devdesc *dp; va_list vl; va_start(vl, f); dp = va_arg(vl, struct ofw_devdesc *); va_end(vl); if (dp != kdp) { if (kdp != NULL) { OF_close(kdp->d_handle); kdp = NULL; } if ((dp->d_handle = OF_open(dp->d_path)) == -1) { printf("%s: Could not open %s\n", __func__, dp->d_path); return (ENOENT); } kdp = dp; } return (0); } static int ofwd_close(struct open_file *f) { struct ofw_devdesc *dev = f->f_devdata; if (dev == kdp) { #if !defined(__powerpc__) OF_close(dev->d_handle); #endif kdp = NULL; } return (0); } static int ofwd_ioctl(struct open_file *f, u_long cmd, void *data) { struct ofw_devdesc *dev = f->f_devdata; int block_size; unsigned int n; switch (cmd) { case DIOCGSECTORSIZE: block_size = OF_block_size(dev->d_handle); *(u_int *)data = block_size; break; case DIOCGMEDIASIZE: block_size = OF_block_size(dev->d_handle); n = OF_blocks(dev->d_handle); *(uint64_t *)data = (uint64_t)(n * block_size); break; default: return (ENOTTY); } return (0); } static int ofwd_print(int verbose __unused) { return (0); } diff --git a/stand/libsa/geli/gelidev.c b/stand/libsa/geli/gelidev.c index 23ded496c23d..3e256c290739 100644 --- a/stand/libsa/geli/gelidev.c +++ b/stand/libsa/geli/gelidev.c @@ -1,353 +1,354 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2018 Ian Lepore * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include #include #include #include #include "disk.h" #include "geliboot.h" #include "geliboot_internal.h" static int geli_dev_init(void); static int geli_dev_strategy(void *, int, daddr_t, size_t, char *, size_t *); static int geli_dev_open(struct open_file *f, ...); static int geli_dev_close(struct open_file *f); static int geli_dev_ioctl(struct open_file *, u_long, void *); static int geli_dev_print(int); static void geli_dev_cleanup(void); /* * geli_devsw is static because it never appears in any arch's global devsw * array. Instead, when devopen() opens a DEVT_DISK device, it then calls * geli_probe_and_attach(), and if we find that the disk_devdesc describes a * geli-encrypted partition, we create a geli_devdesc which references this * devsw and has a pointer to the original disk_devdesc of the underlying host * disk. Then we manipulate the open_file struct to reference the new * geli_devdesc, effectively routing all IO operations through our code. */ static struct devsw geli_devsw = { .dv_name = "disk", .dv_type = DEVT_DISK, .dv_init = geli_dev_init, .dv_strategy = geli_dev_strategy, .dv_open = geli_dev_open, .dv_close = geli_dev_close, .dv_ioctl = geli_dev_ioctl, .dv_print = geli_dev_print, .dv_cleanup = geli_dev_cleanup, .dv_fmtdev = disk_fmtdev, + .dv_parsedev = disk_parsedev, }; /* * geli_devdesc instances replace the disk_devdesc in an open_file struct when * the partition is encrypted. We keep a reference to the original host * disk_devdesc so that we can read the raw encrypted data using it. */ struct geli_devdesc { struct disk_devdesc ddd; /* Must be first. */ struct disk_devdesc *hdesc; /* disk/slice/part hosting geli vol */ struct geli_dev *gdev; /* geli_dev entry */ }; /* * A geli_readfunc that reads via a disk_devdesc passed in readpriv. This is * used to read the underlying host disk data when probing/tasting to see if the * host provider is geli-encrypted. */ static int diskdev_read(void *vdev, void *readpriv, off_t offbytes, void *buf, size_t sizebytes) { struct disk_devdesc *ddev; ddev = (struct disk_devdesc *)readpriv; return (ddev->dd.d_dev->dv_strategy(ddev, F_READ, offbytes / DEV_BSIZE, sizebytes, buf, NULL)); } static int geli_dev_init(void) { /* * Since geli_devsw never gets referenced in any arch's global devsw * table, this function should never get called. */ panic("%s: should never be called", __func__); return (ENXIO); } static int geli_dev_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf, size_t *rsize) { struct geli_devdesc *gdesc; off_t alnend, alnstart, reqend, reqstart; size_t alnsize; char *iobuf; int rc; gdesc = (struct geli_devdesc *)devdata; /* * We can only decrypt full geli blocks. The blk arg is expressed in * units of DEV_BSIZE blocks, while size is in bytes. Convert * everything to bytes, and calculate the geli-blocksize-aligned start * and end points. * * Note: md_sectorsize must be cast to a signed type for the round2 * macros to work correctly (otherwise they get zero-extended to 64 bits * and mask off the high order 32 bits of the requested start/end). */ reqstart = blk * DEV_BSIZE; reqend = reqstart + size; alnstart = rounddown2(reqstart, (int)gdesc->gdev->md.md_sectorsize); alnend = roundup2(reqend, (int)gdesc->gdev->md.md_sectorsize); alnsize = alnend - alnstart; /* * If alignment requires us to read/write more than the size of the * provided buffer, allocate a temporary buffer. * The writes will always get temporary buffer because of encryption. */ if (alnsize <= size && (rw & F_MASK) == F_READ) iobuf = buf; else if ((iobuf = malloc(alnsize)) == NULL) return (ENOMEM); switch (rw & F_MASK) { case F_READ: /* * Read the encrypted data using the host provider, * then decrypt it. */ rc = gdesc->hdesc->dd.d_dev->dv_strategy(gdesc->hdesc, rw, alnstart / DEV_BSIZE, alnsize, iobuf, NULL); if (rc != 0) goto out; rc = geli_io(gdesc->gdev, GELI_DECRYPT, alnstart, iobuf, alnsize); if (rc != 0) goto out; /* * If we had to use a temporary buffer, copy the requested * part of the data to the caller's buffer. */ if (iobuf != buf) memcpy(buf, iobuf + (reqstart - alnstart), size); if (rsize != NULL) *rsize = size; break; case F_WRITE: if (iobuf != buf) { /* Read, decrypt, then modify. */ rc = gdesc->hdesc->dd.d_dev->dv_strategy(gdesc->hdesc, F_READ, alnstart / DEV_BSIZE, alnsize, iobuf, NULL); if (rc != 0) goto out; rc = geli_io(gdesc->gdev, GELI_DECRYPT, alnstart, iobuf, alnsize); if (rc != 0) goto out; /* Copy data to iobuf */ memcpy(iobuf + (reqstart - alnstart), buf, size); } /* Encrypt and write it. */ rc = geli_io(gdesc->gdev, GELI_ENCRYPT, alnstart, iobuf, alnsize); if (rc != 0) goto out; rc = gdesc->hdesc->dd.d_dev->dv_strategy(gdesc->hdesc, rw, alnstart / DEV_BSIZE, alnsize, iobuf, NULL); } out: if (iobuf != buf) free(iobuf); return (rc); } static int geli_dev_open(struct open_file *f, ...) { /* * Since geli_devsw never gets referenced in any arch's global devsw * table, this function should never get called. */ panic("%s: should never be called", __func__); return (ENXIO); } static int geli_dev_close(struct open_file *f) { struct geli_devdesc *gdesc; /* * Detach the geli_devdesc from the open_file and reattach the * underlying host provider's disk_devdesc; this undoes the work done at * the end of geli_probe_and_attach(). Call the host provider's * dv_close() (because that's what our caller thought it was doing). */ gdesc = (struct geli_devdesc *)f->f_devdata; f->f_devdata = gdesc->hdesc; f->f_dev = gdesc->hdesc->dd.d_dev; free(gdesc); f->f_dev->dv_close(f); return (0); } static int geli_dev_ioctl(struct open_file *f, u_long cmd, void *data) { struct geli_devdesc *gdesc; struct g_eli_metadata *md; gdesc = (struct geli_devdesc *)f->f_devdata; md = &gdesc->gdev->md; switch (cmd) { case DIOCGSECTORSIZE: *(u_int *)data = md->md_sectorsize; break; case DIOCGMEDIASIZE: *(uint64_t *)data = md->md_provsize; break; default: return (ENOTTY); } return (0); } static int geli_dev_print(int verbose) { /* * Since geli_devsw never gets referenced in any arch's global devsw * table, this function should never get called. */ panic("%s: should never be called", __func__); return (ENXIO); } static void geli_dev_cleanup(void) { /* * Since geli_devsw never gets referenced in any arch's global devsw * table, this function should never get called. */ panic("%s: should never be called", __func__); } /* * geli_probe_and_attach() is called from devopen() after it successfully calls * the dv_open() method of a DEVT_DISK device. We taste the partition described * by the disk_devdesc, and if it's geli-encrypted and we can decrypt it, we * create a geli_devdesc and store it into the open_file struct in place of the * underlying provider's disk_devdesc, effectively attaching our code to all IO * processing for the partition. Not quite the elegant stacking provided by * geom in the kernel, but it gets the job done. */ void geli_probe_and_attach(struct open_file *f) { static char gelipw[GELI_PW_MAXLEN]; const char *envpw; struct geli_dev *gdev; struct geli_devdesc *gdesc; struct disk_devdesc *hdesc; uint64_t hmediasize; daddr_t hlastblk; int rc; hdesc = (struct disk_devdesc *)(f->f_devdata); /* We only work on DEVT_DISKs */ if (hdesc->dd.d_dev->dv_type != DEVT_DISK) return; /* Get the last block number for the host provider. */ if (hdesc->dd.d_dev->dv_ioctl(f, DIOCGMEDIASIZE, &hmediasize) != 0) return; hlastblk = (hmediasize / DEV_BSIZE) - 1; /* Taste the host provider. If it's not geli-encrypted just return. */ gdev = geli_taste(diskdev_read, hdesc, hlastblk, devformat(&hdesc->dd)); if (gdev == NULL) return; /* * It's geli, try to decrypt it with existing keys, or prompt for a * passphrase if we don't yet have a cached key for it. */ if ((rc = geli_havekey(gdev)) != 0) { envpw = getenv("kern.geom.eli.passphrase"); if (envpw != NULL) { /* Use the cached passphrase */ bcopy(envpw, &gelipw, GELI_PW_MAXLEN); } if ((rc = geli_passphrase(gdev, gelipw)) == 0) { /* Passphrase is good, cache it. */ setenv("kern.geom.eli.passphrase", gelipw, 1); } explicit_bzero(gelipw, sizeof(gelipw)); if (rc != 0) return; } /* * It's geli-encrypted and we can decrypt it. Create a geli_devdesc, * store a reference to the underlying provider's disk_devdesc in it, * then attach it to the openfile struct in place of the host provider. */ if ((gdesc = malloc(sizeof(*gdesc))) == NULL) return; gdesc->ddd.dd.d_dev = &geli_devsw; gdesc->ddd.dd.d_opendata = NULL; gdesc->ddd.dd.d_unit = hdesc->dd.d_unit; gdesc->ddd.d_offset = hdesc->d_offset; gdesc->ddd.d_partition = hdesc->d_partition; gdesc->ddd.d_slice = hdesc->d_slice; gdesc->hdesc = hdesc; gdesc->gdev = gdev; f->f_dev = gdesc->ddd.dd.d_dev; f->f_devdata = gdesc; } diff --git a/stand/uboot/uboot_disk.c b/stand/uboot/uboot_disk.c index 2a41d1181992..63a7a68e8c1b 100644 --- a/stand/uboot/uboot_disk.c +++ b/stand/uboot/uboot_disk.c @@ -1,321 +1,322 @@ /*- * Copyright (c) 2008 Semihalf, Rafal Jaworowski * Copyright (c) 2009 Semihalf, Piotr Ziecik * Copyright (c) 2012 Andrey V. Elsukov * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ /* * Block storage I/O routines for U-Boot */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include "api_public.h" #include "bootstrap.h" #include "disk.h" #include "glue.h" #include "libuboot.h" #define stor_printf(fmt, args...) do { \ printf("%s%d: ", dev->dd.d_dev->dv_name, dev->dd.d_unit); \ printf(fmt, ##args); \ } while (0) #ifdef DEBUG #define debugf(fmt, args...) do { printf("%s(): ", __func__); \ printf(fmt,##args); } while (0) #else #define debugf(fmt, args...) #endif static struct { int opened; /* device is opened */ int handle; /* storage device handle */ int type; /* storage type */ off_t blocks; /* block count */ u_int bsize; /* block size */ } stor_info[UB_MAX_DEV]; #define SI(dev) (stor_info[(dev)->dd.d_unit]) static int stor_info_no = 0; static int stor_opendev(struct disk_devdesc *); static int stor_readdev(struct disk_devdesc *, daddr_t, size_t, char *); /* devsw I/F */ static int stor_init(void); static int stor_strategy(void *, int, daddr_t, size_t, char *, size_t *); static int stor_open(struct open_file *, ...); static int stor_close(struct open_file *); static int stor_ioctl(struct open_file *f, u_long cmd, void *data); static int stor_print(int); static void stor_cleanup(void); struct devsw uboot_storage = { .dv_name = "disk", .dv_type = DEVT_DISK, .dv_init = stor_init, .dv_strategy = stor_strategy, .dv_open = stor_open, .dv_close = stor_close, .dv_ioctl = stor_ioctl, .dv_print = stor_print, .dv_cleanup = stor_cleanup, .dv_fmtdev = disk_fmtdev, + .dv_parsedev = disk_parsedev, }; static int stor_init(void) { struct device_info *di; int i; if (devs_no == 0) { printf("No U-Boot devices! Really enumerated?\n"); return (-1); } for (i = 0; i < devs_no; i++) { di = ub_dev_get(i); if ((di != NULL) && (di->type & DEV_TYP_STOR)) { if (stor_info_no >= UB_MAX_DEV) { printf("Too many storage devices: %d\n", stor_info_no); return (-1); } stor_info[stor_info_no].handle = i; stor_info[stor_info_no].opened = 0; stor_info[stor_info_no].type = di->type; stor_info[stor_info_no].blocks = di->di_stor.block_count; stor_info[stor_info_no].bsize = di->di_stor.block_size; stor_info_no++; } } if (!stor_info_no) { debugf("No storage devices\n"); return (-1); } debugf("storage devices found: %d\n", stor_info_no); return (0); } static void stor_cleanup(void) { int i; for (i = 0; i < stor_info_no; i++) if (stor_info[i].opened > 0) ub_dev_close(stor_info[i].handle); } static int stor_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf, size_t *rsize) { struct disk_devdesc *dev = (struct disk_devdesc *)devdata; daddr_t bcount; int err; rw &= F_MASK; if (rw != F_READ) { stor_printf("write attempt, operation not supported!\n"); return (EROFS); } if (size % SI(dev).bsize) { stor_printf("size=%zu not multiple of device " "block size=%d\n", size, SI(dev).bsize); return (EIO); } bcount = size / SI(dev).bsize; if (rsize) *rsize = 0; err = stor_readdev(dev, blk + dev->d_offset, bcount, buf); if (!err && rsize) *rsize = size; return (err); } static int stor_open(struct open_file *f, ...) { va_list ap; struct disk_devdesc *dev; va_start(ap, f); dev = va_arg(ap, struct disk_devdesc *); va_end(ap); return (stor_opendev(dev)); } static int stor_opendev(struct disk_devdesc *dev) { int err; if (dev->dd.d_unit < 0 || dev->dd.d_unit >= stor_info_no) return (EIO); if (SI(dev).opened == 0) { err = ub_dev_open(SI(dev).handle); if (err != 0) { stor_printf("device open failed with error=%d, " "handle=%d\n", err, SI(dev).handle); return (ENXIO); } SI(dev).opened++; } return (disk_open(dev, SI(dev).blocks * SI(dev).bsize, SI(dev).bsize)); } static int stor_close(struct open_file *f) { struct disk_devdesc *dev; dev = (struct disk_devdesc *)(f->f_devdata); return (disk_close(dev)); } static int stor_readdev(struct disk_devdesc *dev, daddr_t blk, size_t size, char *buf) { lbasize_t real_size; int err; debugf("reading blk=%d size=%d @ 0x%08x\n", (int)blk, size, (uint32_t)buf); err = ub_dev_read(SI(dev).handle, buf, size, blk, &real_size); if (err != 0) { stor_printf("read failed, error=%d\n", err); return (EIO); } if (real_size != size) { stor_printf("real size != size\n"); err = EIO; } return (err); } static int stor_print(int verbose) { struct disk_devdesc dev; static char line[80]; int i, ret = 0; if (stor_info_no == 0) return (ret); printf("%s devices:", uboot_storage.dv_name); if ((ret = pager_output("\n")) != 0) return (ret); for (i = 0; i < stor_info_no; i++) { dev.dd.d_dev = &uboot_storage; dev.dd.d_unit = i; dev.d_slice = D_SLICENONE; dev.d_partition = D_PARTNONE; snprintf(line, sizeof(line), "\tdisk%d (%s)\n", i, ub_stor_type(SI(&dev).type)); if ((ret = pager_output(line)) != 0) break; if (stor_opendev(&dev) == 0) { sprintf(line, "\tdisk%d", i); ret = disk_print(&dev, line, verbose); disk_close(&dev); if (ret != 0) break; } } return (ret); } static int stor_ioctl(struct open_file *f, u_long cmd, void *data) { struct disk_devdesc *dev; int rc; dev = (struct disk_devdesc *)f->f_devdata; rc = disk_ioctl(dev, cmd, data); if (rc != ENOTTY) return (rc); switch (cmd) { case DIOCGSECTORSIZE: *(u_int *)data = SI(dev).bsize; break; case DIOCGMEDIASIZE: *(uint64_t *)data = SI(dev).bsize * SI(dev).blocks; break; default: return (ENOTTY); } return (0); } /* * Return the device unit number for the given type and type-relative unit * number. */ int uboot_diskgetunit(int type, int type_unit) { int local_type_unit; int i; local_type_unit = 0; for (i = 0; i < stor_info_no; i++) { if ((stor_info[i].type & type) == type) { if (local_type_unit == type_unit) { return (i); } local_type_unit++; } } return (-1); } diff --git a/stand/usb/storage/umass_loader.c b/stand/usb/storage/umass_loader.c index cc1f6b68e275..647d8a58b534 100644 --- a/stand/usb/storage/umass_loader.c +++ b/stand/usb/storage/umass_loader.c @@ -1,240 +1,241 @@ /* $FreeBSD$ */ /*- * Copyright (c) 2014 Hans Petter Selasky * All rights reserved. * * This software was developed by SRI International and the University of * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) * ("CTSRD"), as part of the DARPA CRASH research programme. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include #include #include #define HAVE_STANDARD_DEFS #include USB_GLOBAL_INCLUDE_FILE #include "umass_common.h" static int umass_disk_init(void); static int umass_disk_open(struct open_file *,...); static int umass_disk_close(struct open_file *); static void umass_disk_cleanup(void); static int umass_disk_ioctl(struct open_file *, u_long, void *); static int umass_disk_strategy(void *, int, daddr_t, size_t, char *, size_t *); static int umass_disk_print(int); struct devsw umass_disk = { .dv_name = "umass", .dv_type = DEVT_DISK, .dv_init = umass_disk_init, .dv_strategy = umass_disk_strategy, .dv_open = umass_disk_open, .dv_close = umass_disk_close, .dv_ioctl = umass_disk_ioctl, .dv_print = umass_disk_print, .dv_cleanup = umass_disk_cleanup, .dv_fmtdev = disk_fmtdev, + .dv_parsedev = disk_parsedev, }; static int umass_disk_init(void) { uint32_t time; usb_init(); usb_needs_explore_all(); /* wait 8 seconds for a USB mass storage device to appear */ for (time = 0; time < (8 * hz); time++) { usb_idle(); delay(1000000 / hz); time++; callout_process(1); if (umass_uaa.device != NULL) return (0); } return (0); } static int umass_disk_strategy(void *devdata, int flag, daddr_t dblk, size_t size, char *buf, size_t *rsizep) { if (umass_uaa.device == NULL) return (ENXIO); if (rsizep != NULL) *rsizep = 0; flag &= F_MASK; if (flag == F_WRITE) { if (usb_msc_write_10(umass_uaa.device, 0, dblk, size >> 9, buf) != 0) return (EINVAL); } else if (flag == F_READ) { if (usb_msc_read_10(umass_uaa.device, 0, dblk, size >> 9, buf) != 0) return (EINVAL); } else { return (EROFS); } if (rsizep != NULL) *rsizep = size; return (0); } static int umass_disk_open_sub(struct disk_devdesc *dev) { uint32_t nblock; uint32_t blocksize; if (usb_msc_read_capacity(umass_uaa.device, 0, &nblock, &blocksize) != 0) return (EINVAL); return (disk_open(dev, ((uint64_t)nblock + 1) * (uint64_t)blocksize, blocksize)); } static int umass_disk_open(struct open_file *f,...) { va_list ap; struct disk_devdesc *dev; va_start(ap, f); dev = va_arg(ap, struct disk_devdesc *); va_end(ap); if (umass_uaa.device == NULL) return (ENXIO); if (dev->d_unit != 0) return (EIO); return (umass_disk_open_sub(dev)); } static int umass_disk_ioctl(struct open_file *f, u_long cmd, void *buf) { struct disk_devdesc *dev; uint32_t nblock; uint32_t blocksize; int rc; dev = (struct disk_devdesc *)(f->f_devdata); if (dev == NULL) return (EINVAL); rc = disk_ioctl(dev, cmd, buf); if (rc != ENOTTY) return (rc); switch (cmd) { case DIOCGSECTORSIZE: case DIOCGMEDIASIZE: if (usb_msc_read_capacity(umass_uaa.device, 0, &nblock, &blocksize) != 0) return (EINVAL); if (cmd == DIOCGMEDIASIZE) *(uint64_t*)buf = nblock; else *(uint32_t*)buf = blocksize; return (0); default: return (ENXIO); } } static int umass_disk_close(struct open_file *f) { struct disk_devdesc *dev; dev = (struct disk_devdesc *)f->f_devdata; return (disk_close(dev)); } static int umass_disk_print(int verbose) { struct disk_devdesc dev; printf("%s devices:", umass_disk.dv_name); if (pager_output("\n") != 0) return (1); memset(&dev, 0, sizeof(dev)); ret = pager_output(" umass0 UMASS device\n"); if (ret != 0) return (ret); dev.d_dev = &umass_disk; dev.d_unit = 0; dev.d_slice = D_SLICENONE; dev.d_partition = D_PARTNONE; if (umass_disk_open_sub(&dev) == 0) { ret = disk_print(&dev, " umass0", verbose); disk_close(&dev); } return (ret); } static void umass_disk_cleanup(void) { usb_uninit(); } /* USB specific functions */ extern void callout_process(int); extern void usb_idle(void); extern void usb_init(void); extern void usb_uninit(void); void DELAY(unsigned int usdelay) { delay(usdelay); } int pause(const char *what, int timeout) { if (timeout == 0) timeout = 1; delay((1000000 / hz) * timeout); return (0); } diff --git a/stand/userboot/userboot/userboot_disk.c b/stand/userboot/userboot/userboot_disk.c index d5b0f36e5e50..7af31a158ed2 100644 --- a/stand/userboot/userboot/userboot_disk.c +++ b/stand/userboot/userboot/userboot_disk.c @@ -1,249 +1,250 @@ /*- * Copyright (c) 2011 Google, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); /* * Userboot disk image handling. */ #include #include #include #include #include "disk.h" #include "libuserboot.h" struct userdisk_info { uint64_t mediasize; uint16_t sectorsize; int ud_open; /* reference counter */ void *ud_bcache; /* buffer cache data */ }; int userboot_disk_maxunit = 0; static int userdisk_maxunit = 0; static struct userdisk_info *ud_info; static int userdisk_init(void); static void userdisk_cleanup(void); static int userdisk_strategy(void *devdata, int flag, daddr_t dblk, size_t size, char *buf, size_t *rsize); static int userdisk_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size, char *buf, size_t *rsize); static int userdisk_open(struct open_file *f, ...); static int userdisk_close(struct open_file *f); static int userdisk_ioctl(struct open_file *f, u_long cmd, void *data); static int userdisk_print(int verbose); struct devsw userboot_disk = { .dv_name = "disk", .dv_type = DEVT_DISK, .dv_init = userdisk_init, .dv_strategy = userdisk_strategy, .dv_open = userdisk_open, .dv_close = userdisk_close, .dv_ioctl = userdisk_ioctl, .dv_print = userdisk_print, .dv_cleanup = userdisk_cleanup, .dv_fmtdev = disk_fmtdev, + .dv_parsedev = disk_parsedev, }; /* * Initialize userdisk_info structure for each disk. */ static int userdisk_init(void) { off_t mediasize; u_int sectorsize; int i; userdisk_maxunit = userboot_disk_maxunit; if (userdisk_maxunit > 0) { ud_info = malloc(sizeof(*ud_info) * userdisk_maxunit); if (ud_info == NULL) return (ENOMEM); for (i = 0; i < userdisk_maxunit; i++) { if (CALLBACK(diskioctl, i, DIOCGSECTORSIZE, §orsize) != 0 || CALLBACK(diskioctl, i, DIOCGMEDIASIZE, &mediasize) != 0) return (ENXIO); ud_info[i].mediasize = mediasize; ud_info[i].sectorsize = sectorsize; ud_info[i].ud_open = 0; ud_info[i].ud_bcache = NULL; } } bcache_add_dev(userdisk_maxunit); return(0); } static void userdisk_cleanup(void) { if (userdisk_maxunit > 0) free(ud_info); } /* * Print information about disks */ static int userdisk_print(int verbose) { struct disk_devdesc dev; char line[80]; int i, ret = 0; if (userdisk_maxunit == 0) return (0); printf("%s devices:", userboot_disk.dv_name); if ((ret = pager_output("\n")) != 0) return (ret); for (i = 0; i < userdisk_maxunit; i++) { snprintf(line, sizeof(line), " disk%d: Guest drive image\n", i); ret = pager_output(line); if (ret != 0) break; dev.dd.d_dev = &userboot_disk; dev.dd.d_unit = i; dev.d_slice = D_SLICENONE; dev.d_partition = D_PARTNONE; if (disk_open(&dev, ud_info[i].mediasize, ud_info[i].sectorsize) == 0) { snprintf(line, sizeof(line), " disk%d", i); ret = disk_print(&dev, line, verbose); disk_close(&dev); if (ret != 0) break; } } return (ret); } /* * Attempt to open the disk described by (dev) for use by (f). */ static int userdisk_open(struct open_file *f, ...) { va_list ap; struct disk_devdesc *dev; va_start(ap, f); dev = va_arg(ap, struct disk_devdesc *); va_end(ap); if (dev->dd.d_unit < 0 || dev->dd.d_unit >= userdisk_maxunit) return (EIO); ud_info[dev->dd.d_unit].ud_open++; if (ud_info[dev->dd.d_unit].ud_bcache == NULL) ud_info[dev->dd.d_unit].ud_bcache = bcache_allocate(); return (disk_open(dev, ud_info[dev->dd.d_unit].mediasize, ud_info[dev->dd.d_unit].sectorsize)); } static int userdisk_close(struct open_file *f) { struct disk_devdesc *dev; dev = (struct disk_devdesc *)f->f_devdata; ud_info[dev->dd.d_unit].ud_open--; if (ud_info[dev->dd.d_unit].ud_open == 0) { bcache_free(ud_info[dev->dd.d_unit].ud_bcache); ud_info[dev->dd.d_unit].ud_bcache = NULL; } return (disk_close(dev)); } static int userdisk_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize) { struct bcache_devdata bcd; struct disk_devdesc *dev; dev = (struct disk_devdesc *)devdata; bcd.dv_strategy = userdisk_realstrategy; bcd.dv_devdata = devdata; bcd.dv_cache = ud_info[dev->dd.d_unit].ud_bcache; return (bcache_strategy(&bcd, rw, dblk + dev->d_offset, size, buf, rsize)); } static int userdisk_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize) { struct disk_devdesc *dev = devdata; uint64_t off; size_t resid; int rc; if (rsize) *rsize = 0; off = dblk * ud_info[dev->dd.d_unit].sectorsize; switch (rw & F_MASK) { case F_READ: rc = CALLBACK(diskread, dev->dd.d_unit, off, buf, size, &resid); break; case F_WRITE: rc = CALLBACK(diskwrite, dev->dd.d_unit, off, buf, size, &resid); break; default: rc = EINVAL; break; } if (rc) return (rc); if (rsize) *rsize = size - resid; return (0); } static int userdisk_ioctl(struct open_file *f, u_long cmd, void *data) { struct disk_devdesc *dev; int rc; dev = (struct disk_devdesc *)f->f_devdata; rc = disk_ioctl(dev, cmd, data); if (rc != ENOTTY) return (rc); return (CALLBACK(diskioctl, dev->dd.d_unit, cmd, data)); }