2011-06-30 16:08:56 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
|
2012-08-05 12:15:15 +00:00
|
|
|
* Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
|
2011-06-30 16:08:56 +00:00
|
|
|
* 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 <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2012-08-05 12:15:15 +00:00
|
|
|
#include <sys/disk.h>
|
2012-09-29 16:47:56 +00:00
|
|
|
#include <sys/queue.h>
|
2011-06-30 16:08:56 +00:00
|
|
|
#include <stand.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <bootstrap.h>
|
2012-08-05 12:15:15 +00:00
|
|
|
#include <part.h>
|
2022-08-11 15:04:50 +00:00
|
|
|
#include <assert.h>
|
2011-06-30 16:08:56 +00:00
|
|
|
|
|
|
|
#include "disk.h"
|
|
|
|
|
|
|
|
#ifdef DISK_DEBUG
|
2019-03-12 16:21:39 +00:00
|
|
|
# define DPRINTF(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
|
2011-06-30 16:08:56 +00:00
|
|
|
#else
|
2019-05-09 13:12:43 +00:00
|
|
|
# define DPRINTF(fmt, args...) ((void)0)
|
2011-06-30 16:08:56 +00:00
|
|
|
#endif
|
|
|
|
|
2012-08-05 12:15:15 +00:00
|
|
|
struct open_disk {
|
|
|
|
struct ptable *table;
|
2017-02-01 20:10:56 +00:00
|
|
|
uint64_t mediasize;
|
2017-02-06 08:26:45 +00:00
|
|
|
uint64_t entrysize;
|
2012-08-05 12:15:15 +00:00
|
|
|
u_int sectorsize;
|
2011-06-30 16:08:56 +00:00
|
|
|
};
|
|
|
|
|
2012-08-05 12:15:15 +00:00
|
|
|
struct print_args {
|
|
|
|
struct disk_devdesc *dev;
|
|
|
|
const char *prefix;
|
|
|
|
int verbose;
|
|
|
|
};
|
2011-07-01 18:31:59 +00:00
|
|
|
|
2012-08-05 12:15:15 +00:00
|
|
|
/* Convert size to a human-readable number. */
|
2011-06-30 16:08:56 +00:00
|
|
|
static char *
|
2012-08-05 12:15:15 +00:00
|
|
|
display_size(uint64_t size, u_int sectorsize)
|
2011-06-30 16:08:56 +00:00
|
|
|
{
|
|
|
|
static char buf[80];
|
|
|
|
char unit;
|
|
|
|
|
2012-08-05 12:15:15 +00:00
|
|
|
size = size * sectorsize / 1024;
|
2011-06-30 16:08:56 +00:00
|
|
|
unit = 'K';
|
|
|
|
if (size >= 10485760000LL) {
|
|
|
|
size /= 1073741824;
|
|
|
|
unit = 'T';
|
|
|
|
} else if (size >= 10240000) {
|
|
|
|
size /= 1048576;
|
|
|
|
unit = 'G';
|
|
|
|
} else if (size >= 10000) {
|
|
|
|
size /= 1024;
|
|
|
|
unit = 'M';
|
|
|
|
}
|
2019-05-09 11:04:10 +00:00
|
|
|
snprintf(buf, sizeof(buf), "%4ld%cB", (long)size, unit);
|
2011-06-30 16:08:56 +00:00
|
|
|
return (buf);
|
|
|
|
}
|
|
|
|
|
2016-03-16 23:12:19 +00:00
|
|
|
int
|
2017-02-01 20:10:56 +00:00
|
|
|
ptblread(void *d, void *buf, size_t blocks, uint64_t offset)
|
2011-06-30 16:08:56 +00:00
|
|
|
{
|
2012-08-05 12:15:15 +00:00
|
|
|
struct disk_devdesc *dev;
|
|
|
|
struct open_disk *od;
|
2011-06-30 16:08:56 +00:00
|
|
|
|
2012-08-05 12:15:15 +00:00
|
|
|
dev = (struct disk_devdesc *)d;
|
2018-03-12 21:39:49 +00:00
|
|
|
od = (struct open_disk *)dev->dd.d_opendata;
|
2017-04-06 15:57:53 +00:00
|
|
|
|
2017-10-31 20:29:31 +00:00
|
|
|
/*
|
|
|
|
* The strategy function assumes the offset is in units of 512 byte
|
|
|
|
* sectors. For larger sector sizes, we need to adjust the offset to
|
|
|
|
* match the actual sector size.
|
|
|
|
*/
|
|
|
|
offset *= (od->sectorsize / 512);
|
2017-04-06 15:57:53 +00:00
|
|
|
/*
|
|
|
|
* As the GPT backup partition is located at the end of the disk,
|
|
|
|
* to avoid reading past disk end, flag bcache not to use RA.
|
|
|
|
*/
|
2018-03-12 21:39:49 +00:00
|
|
|
return (dev->dd.d_dev->dv_strategy(dev, F_READ | F_NORA, offset,
|
2012-08-05 12:15:15 +00:00
|
|
|
blocks * od->sectorsize, (char *)buf, NULL));
|
2011-06-30 16:08:56 +00:00
|
|
|
}
|
|
|
|
|
2016-05-18 05:59:05 +00:00
|
|
|
static int
|
2012-08-05 12:15:15 +00:00
|
|
|
ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
|
2011-06-30 16:08:56 +00:00
|
|
|
{
|
2017-03-16 12:04:43 +00:00
|
|
|
struct disk_devdesc dev;
|
2012-08-05 12:15:15 +00:00
|
|
|
struct print_args *pa, bsd;
|
|
|
|
struct open_disk *od;
|
|
|
|
struct ptable *table;
|
2011-06-30 16:08:56 +00:00
|
|
|
char line[80];
|
2016-05-18 05:59:05 +00:00
|
|
|
int res;
|
2019-02-17 23:38:17 +00:00
|
|
|
u_int sectsize;
|
|
|
|
uint64_t partsize;
|
2011-06-30 16:08:56 +00:00
|
|
|
|
2012-08-05 12:15:15 +00:00
|
|
|
pa = (struct print_args *)arg;
|
2018-03-12 21:39:49 +00:00
|
|
|
od = (struct open_disk *)pa->dev->dd.d_opendata;
|
2019-02-17 23:38:17 +00:00
|
|
|
sectsize = od->sectorsize;
|
|
|
|
partsize = part->end - part->start + 1;
|
2019-05-09 11:04:10 +00:00
|
|
|
snprintf(line, sizeof(line), " %s%s: %s", pa->prefix, pname,
|
|
|
|
parttype2str(part->type));
|
2016-05-18 05:59:05 +00:00
|
|
|
if (pager_output(line))
|
2019-05-09 11:04:10 +00:00
|
|
|
return (1);
|
|
|
|
|
|
|
|
if (pa->verbose) {
|
|
|
|
/* Emit extra tab when the line is shorter than 3 tab stops */
|
|
|
|
if (strlen(line) < 24)
|
|
|
|
(void) pager_output("\t");
|
|
|
|
|
|
|
|
snprintf(line, sizeof(line), "\t%s",
|
|
|
|
display_size(partsize, sectsize));
|
|
|
|
if (pager_output(line))
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if (pager_output("\n"))
|
|
|
|
return (1);
|
|
|
|
|
2016-05-18 05:59:05 +00:00
|
|
|
res = 0;
|
2012-08-05 12:15:15 +00:00
|
|
|
if (part->type == PART_FREEBSD) {
|
|
|
|
/* Open slice with BSD label */
|
2018-03-12 21:39:49 +00:00
|
|
|
dev.dd.d_dev = pa->dev->dd.d_dev;
|
|
|
|
dev.dd.d_unit = pa->dev->dd.d_unit;
|
2017-03-16 12:04:43 +00:00
|
|
|
dev.d_slice = part->index;
|
2019-03-24 18:51:52 +00:00
|
|
|
dev.d_partition = D_PARTNONE;
|
2019-02-17 23:38:17 +00:00
|
|
|
if (disk_open(&dev, partsize, sectsize) == 0) {
|
|
|
|
table = ptable_open(&dev, partsize, sectsize, ptblread);
|
2017-03-16 12:04:43 +00:00
|
|
|
if (table != NULL) {
|
2019-05-09 11:04:10 +00:00
|
|
|
snprintf(line, sizeof(line), " %s%s",
|
|
|
|
pa->prefix, pname);
|
2017-03-16 12:04:43 +00:00
|
|
|
bsd.dev = pa->dev;
|
|
|
|
bsd.prefix = line;
|
|
|
|
bsd.verbose = pa->verbose;
|
|
|
|
res = ptable_iterate(table, &bsd, ptable_print);
|
|
|
|
ptable_close(table);
|
|
|
|
}
|
|
|
|
disk_close(&dev);
|
|
|
|
}
|
2011-06-30 16:08:56 +00:00
|
|
|
}
|
2016-05-18 05:59:05 +00:00
|
|
|
|
|
|
|
return (res);
|
2011-06-30 16:08:56 +00:00
|
|
|
}
|
|
|
|
|
2016-05-18 05:59:05 +00:00
|
|
|
int
|
2012-08-05 12:15:15 +00:00
|
|
|
disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
|
2011-06-30 16:08:56 +00:00
|
|
|
{
|
2012-08-05 12:15:15 +00:00
|
|
|
struct open_disk *od;
|
|
|
|
struct print_args pa;
|
|
|
|
|
|
|
|
/* Disk should be opened */
|
2018-03-12 21:39:49 +00:00
|
|
|
od = (struct open_disk *)dev->dd.d_opendata;
|
2012-08-05 12:15:15 +00:00
|
|
|
pa.dev = dev;
|
|
|
|
pa.prefix = prefix;
|
|
|
|
pa.verbose = verbose;
|
2016-05-18 05:59:05 +00:00
|
|
|
return (ptable_iterate(od->table, &pa, ptable_print));
|
2011-06-30 16:08:56 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 18:17:53 +00:00
|
|
|
int
|
2017-02-01 20:10:56 +00:00
|
|
|
disk_read(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
|
2015-11-27 18:17:53 +00:00
|
|
|
{
|
|
|
|
struct open_disk *od;
|
|
|
|
int ret;
|
|
|
|
|
2018-03-12 21:39:49 +00:00
|
|
|
od = (struct open_disk *)dev->dd.d_opendata;
|
|
|
|
ret = dev->dd.d_dev->dv_strategy(dev, F_READ, dev->d_offset + offset,
|
2015-11-27 18:17:53 +00:00
|
|
|
blocks * od->sectorsize, buf, NULL);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-02-01 20:10:56 +00:00
|
|
|
disk_write(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
|
2015-11-27 18:17:53 +00:00
|
|
|
{
|
|
|
|
struct open_disk *od;
|
|
|
|
int ret;
|
|
|
|
|
2018-03-12 21:39:49 +00:00
|
|
|
od = (struct open_disk *)dev->dd.d_opendata;
|
|
|
|
ret = dev->dd.d_dev->dv_strategy(dev, F_WRITE, dev->d_offset + offset,
|
2015-11-27 18:17:53 +00:00
|
|
|
blocks * od->sectorsize, buf, NULL);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-02-06 08:26:45 +00:00
|
|
|
disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *data)
|
2015-11-27 18:17:53 +00:00
|
|
|
{
|
2018-03-12 21:39:49 +00:00
|
|
|
struct open_disk *od = dev->dd.d_opendata;
|
2017-02-06 08:26:45 +00:00
|
|
|
|
|
|
|
if (od == NULL)
|
|
|
|
return (ENOTTY);
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case DIOCGSECTORSIZE:
|
|
|
|
*(u_int *)data = od->sectorsize;
|
|
|
|
break;
|
|
|
|
case DIOCGMEDIASIZE:
|
|
|
|
if (dev->d_offset == 0)
|
|
|
|
*(uint64_t *)data = od->mediasize;
|
|
|
|
else
|
|
|
|
*(uint64_t *)data = od->entrysize * od->sectorsize;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (ENOTTY);
|
|
|
|
}
|
2015-11-27 18:17:53 +00:00
|
|
|
|
2017-02-06 08:26:45 +00:00
|
|
|
return (0);
|
2015-11-27 18:17:53 +00:00
|
|
|
}
|
|
|
|
|
2012-08-05 12:15:15 +00:00
|
|
|
int
|
2017-03-16 12:04:43 +00:00
|
|
|
disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize)
|
2011-06-30 16:08:56 +00:00
|
|
|
{
|
2018-07-02 18:19:08 +00:00
|
|
|
struct disk_devdesc partdev;
|
2012-08-05 12:15:15 +00:00
|
|
|
struct open_disk *od;
|
|
|
|
struct ptable *table;
|
|
|
|
struct ptable_entry part;
|
2012-09-29 16:47:56 +00:00
|
|
|
int rc, slice, partition;
|
2011-06-30 16:08:56 +00:00
|
|
|
|
2019-05-05 06:38:47 +00:00
|
|
|
if (sectorsize == 0) {
|
|
|
|
DPRINTF("unknown sector size");
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
2012-10-21 12:14:58 +00:00
|
|
|
rc = 0;
|
2017-03-16 12:04:43 +00:00
|
|
|
od = (struct open_disk *)malloc(sizeof(struct open_disk));
|
|
|
|
if (od == NULL) {
|
2019-03-12 16:21:39 +00:00
|
|
|
DPRINTF("no memory");
|
2017-03-16 12:04:43 +00:00
|
|
|
return (ENOMEM);
|
2012-09-29 16:47:56 +00:00
|
|
|
}
|
2018-03-12 21:39:49 +00:00
|
|
|
dev->dd.d_opendata = od;
|
2017-03-16 12:04:43 +00:00
|
|
|
od->entrysize = 0;
|
2012-08-05 12:15:15 +00:00
|
|
|
od->mediasize = mediasize;
|
|
|
|
od->sectorsize = sectorsize;
|
2018-07-02 18:19:08 +00:00
|
|
|
/*
|
|
|
|
* While we are reading disk metadata, make sure we do it relative
|
|
|
|
* to the start of the disk
|
|
|
|
*/
|
|
|
|
memcpy(&partdev, dev, sizeof(partdev));
|
|
|
|
partdev.d_offset = 0;
|
2019-03-24 18:51:52 +00:00
|
|
|
partdev.d_slice = D_SLICENONE;
|
|
|
|
partdev.d_partition = D_PARTNONE;
|
2018-07-02 18:19:08 +00:00
|
|
|
|
2018-07-03 05:53:27 +00:00
|
|
|
dev->d_offset = 0;
|
2018-07-02 18:19:08 +00:00
|
|
|
table = NULL;
|
|
|
|
slice = dev->d_slice;
|
|
|
|
partition = dev->d_partition;
|
|
|
|
|
2019-06-06 16:27:05 +00:00
|
|
|
DPRINTF("%s unit %d, slice %d, partition %d => %p", disk_fmtdev(dev),
|
|
|
|
dev->dd.d_unit, dev->d_slice, dev->d_partition, od);
|
2012-08-05 12:15:15 +00:00
|
|
|
|
|
|
|
/* Determine disk layout. */
|
2018-07-02 18:19:08 +00:00
|
|
|
od->table = ptable_open(&partdev, mediasize / sectorsize, sectorsize,
|
2012-08-05 12:15:15 +00:00
|
|
|
ptblread);
|
|
|
|
if (od->table == NULL) {
|
2019-03-12 16:21:39 +00:00
|
|
|
DPRINTF("Can't read partition table");
|
2012-08-05 12:15:15 +00:00
|
|
|
rc = ENXIO;
|
2011-06-30 16:08:56 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2017-02-06 08:26:45 +00:00
|
|
|
|
|
|
|
if (ptable_getsize(od->table, &mediasize) != 0) {
|
|
|
|
rc = ENXIO;
|
|
|
|
goto out;
|
|
|
|
}
|
2018-11-07 11:14:22 +00:00
|
|
|
od->mediasize = mediasize;
|
2017-03-16 12:04:43 +00:00
|
|
|
|
2012-08-13 13:08:30 +00:00
|
|
|
if (ptable_gettype(od->table) == PTABLE_BSD &&
|
2012-09-29 16:47:56 +00:00
|
|
|
partition >= 0) {
|
2012-08-13 13:08:30 +00:00
|
|
|
/* It doesn't matter what value has d_slice */
|
2012-09-29 16:47:56 +00:00
|
|
|
rc = ptable_getpart(od->table, &part, partition);
|
2017-02-06 08:26:45 +00:00
|
|
|
if (rc == 0) {
|
2012-08-13 13:08:30 +00:00
|
|
|
dev->d_offset = part.start;
|
2017-02-06 08:26:45 +00:00
|
|
|
od->entrysize = part.end - part.start + 1;
|
|
|
|
}
|
2018-04-05 19:45:30 +00:00
|
|
|
} else if (ptable_gettype(od->table) == PTABLE_ISO9660) {
|
|
|
|
dev->d_offset = 0;
|
|
|
|
od->entrysize = mediasize;
|
2012-09-29 16:47:56 +00:00
|
|
|
} else if (slice >= 0) {
|
2012-08-05 12:15:15 +00:00
|
|
|
/* Try to get information about partition */
|
2012-09-29 16:47:56 +00:00
|
|
|
if (slice == 0)
|
2012-09-28 10:49:41 +00:00
|
|
|
rc = ptable_getbestpart(od->table, &part);
|
|
|
|
else
|
2012-09-29 16:47:56 +00:00
|
|
|
rc = ptable_getpart(od->table, &part, slice);
|
2012-08-05 12:15:15 +00:00
|
|
|
if (rc != 0) /* Partition doesn't exist */
|
2011-06-30 16:08:56 +00:00
|
|
|
goto out;
|
2012-08-05 12:15:15 +00:00
|
|
|
dev->d_offset = part.start;
|
2017-02-06 08:26:45 +00:00
|
|
|
od->entrysize = part.end - part.start + 1;
|
2012-09-29 16:47:56 +00:00
|
|
|
slice = part.index;
|
|
|
|
if (ptable_gettype(od->table) == PTABLE_GPT) {
|
2019-05-01 05:42:13 +00:00
|
|
|
partition = D_PARTISGPT;
|
2012-08-05 12:15:15 +00:00
|
|
|
goto out; /* Nothing more to do */
|
2019-05-01 05:42:13 +00:00
|
|
|
} else if (partition == D_PARTISGPT) {
|
2012-10-22 11:01:43 +00:00
|
|
|
/*
|
|
|
|
* When we try to open GPT partition, but partition
|
2019-06-06 16:27:05 +00:00
|
|
|
* table isn't GPT, reset partition value to
|
|
|
|
* D_PARTWILD and try to autodetect appropriate value.
|
2012-10-22 11:01:43 +00:00
|
|
|
*/
|
2019-06-06 16:27:05 +00:00
|
|
|
partition = D_PARTWILD;
|
2012-09-29 16:47:56 +00:00
|
|
|
}
|
2019-06-06 16:27:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If partition is D_PARTNONE, then disk_open() was called
|
|
|
|
* to open raw MBR slice.
|
|
|
|
*/
|
|
|
|
if (partition == D_PARTNONE)
|
|
|
|
goto out;
|
|
|
|
|
2012-08-15 10:11:29 +00:00
|
|
|
/*
|
2019-06-06 16:27:05 +00:00
|
|
|
* If partition is D_PARTWILD and we are looking at a BSD slice,
|
2012-08-15 10:11:29 +00:00
|
|
|
* then try to read BSD label, otherwise return the
|
|
|
|
* whole MBR slice.
|
|
|
|
*/
|
2019-06-06 16:27:05 +00:00
|
|
|
if (partition == D_PARTWILD &&
|
2012-08-15 10:11:29 +00:00
|
|
|
part.type != PART_FREEBSD)
|
|
|
|
goto out;
|
2012-08-05 12:15:15 +00:00
|
|
|
/* Try to read BSD label */
|
|
|
|
table = ptable_open(dev, part.end - part.start + 1,
|
|
|
|
od->sectorsize, ptblread);
|
|
|
|
if (table == NULL) {
|
2019-03-12 16:21:39 +00:00
|
|
|
DPRINTF("Can't read BSD label");
|
2012-08-05 12:15:15 +00:00
|
|
|
rc = ENXIO;
|
2011-06-30 16:08:56 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2012-08-15 10:11:29 +00:00
|
|
|
/*
|
2019-06-06 16:27:05 +00:00
|
|
|
* If slice contains BSD label and partition < 0, then
|
2012-08-15 10:11:29 +00:00
|
|
|
* assume the 'a' partition. Otherwise just return the
|
|
|
|
* whole MBR slice, because it can contain ZFS.
|
|
|
|
*/
|
2012-09-29 16:47:56 +00:00
|
|
|
if (partition < 0) {
|
2012-08-15 10:11:29 +00:00
|
|
|
if (ptable_gettype(table) != PTABLE_BSD)
|
|
|
|
goto out;
|
2012-09-29 16:47:56 +00:00
|
|
|
partition = 0;
|
2012-08-15 10:11:29 +00:00
|
|
|
}
|
2012-09-29 16:47:56 +00:00
|
|
|
rc = ptable_getpart(table, &part, partition);
|
2012-08-05 12:15:15 +00:00
|
|
|
if (rc != 0)
|
|
|
|
goto out;
|
|
|
|
dev->d_offset += part.start;
|
2017-02-06 08:26:45 +00:00
|
|
|
od->entrysize = part.end - part.start + 1;
|
2011-06-30 16:08:56 +00:00
|
|
|
}
|
|
|
|
out:
|
2012-08-05 12:15:15 +00:00
|
|
|
if (table != NULL)
|
|
|
|
ptable_close(table);
|
2012-08-12 14:16:21 +00:00
|
|
|
|
2012-08-05 12:15:15 +00:00
|
|
|
if (rc != 0) {
|
2017-03-16 12:04:43 +00:00
|
|
|
if (od->table != NULL)
|
|
|
|
ptable_close(od->table);
|
|
|
|
free(od);
|
2019-03-12 16:21:39 +00:00
|
|
|
DPRINTF("%s could not open", disk_fmtdev(dev));
|
2012-08-12 14:16:21 +00:00
|
|
|
} else {
|
2012-09-29 16:47:56 +00:00
|
|
|
/* Save the slice and partition number to the dev */
|
|
|
|
dev->d_slice = slice;
|
|
|
|
dev->d_partition = partition;
|
2019-03-12 16:21:39 +00:00
|
|
|
DPRINTF("%s offset %lld => %p", disk_fmtdev(dev),
|
2014-10-05 06:04:47 +00:00
|
|
|
(long long)dev->d_offset, od);
|
2012-08-05 12:15:15 +00:00
|
|
|
}
|
2011-06-30 16:08:56 +00:00
|
|
|
return (rc);
|
|
|
|
}
|
|
|
|
|
2012-08-05 12:15:15 +00:00
|
|
|
int
|
|
|
|
disk_close(struct disk_devdesc *dev)
|
2011-06-30 16:08:56 +00:00
|
|
|
{
|
2012-08-05 12:15:15 +00:00
|
|
|
struct open_disk *od;
|
2011-06-30 16:08:56 +00:00
|
|
|
|
2018-03-12 21:39:49 +00:00
|
|
|
od = (struct open_disk *)dev->dd.d_opendata;
|
2019-03-12 16:21:39 +00:00
|
|
|
DPRINTF("%s closed => %p", disk_fmtdev(dev), od);
|
2017-03-16 12:04:43 +00:00
|
|
|
ptable_close(od->table);
|
|
|
|
free(od);
|
2011-06-30 16:08:56 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2022-07-25 13:32:15 +00:00
|
|
|
char *
|
2022-08-11 15:04:50 +00:00
|
|
|
disk_fmtdev(struct devdesc *vdev)
|
2011-06-30 16:08:56 +00:00
|
|
|
{
|
2022-08-11 15:04:50 +00:00
|
|
|
struct disk_devdesc *dev = (struct disk_devdesc *)vdev;
|
2012-08-05 12:15:15 +00:00
|
|
|
static char buf[128];
|
|
|
|
char *cp;
|
2011-06-30 16:08:56 +00:00
|
|
|
|
2022-08-11 15:04:50 +00:00
|
|
|
assert(vdev->d_dev->dv_type == DEVT_DISK);
|
2018-03-12 21:39:49 +00:00
|
|
|
cp = buf + sprintf(buf, "%s%d", dev->dd.d_dev->dv_name, dev->dd.d_unit);
|
2019-03-24 18:51:52 +00:00
|
|
|
if (dev->d_slice > D_SLICENONE) {
|
2011-06-30 16:08:56 +00:00
|
|
|
#ifdef LOADER_GPT_SUPPORT
|
2019-03-24 18:51:52 +00:00
|
|
|
if (dev->d_partition == D_PARTISGPT) {
|
2012-08-05 12:15:15 +00:00
|
|
|
sprintf(cp, "p%d:", dev->d_slice);
|
|
|
|
return (buf);
|
|
|
|
} else
|
2011-06-30 16:08:56 +00:00
|
|
|
#endif
|
2011-07-01 18:31:59 +00:00
|
|
|
#ifdef LOADER_MBR_SUPPORT
|
2012-08-05 12:15:15 +00:00
|
|
|
cp += sprintf(cp, "s%d", dev->d_slice);
|
2011-07-01 18:31:59 +00:00
|
|
|
#endif
|
2012-08-05 12:15:15 +00:00
|
|
|
}
|
2019-03-24 18:51:52 +00:00
|
|
|
if (dev->d_partition > D_PARTNONE)
|
2012-08-13 13:08:30 +00:00
|
|
|
cp += sprintf(cp, "%c", dev->d_partition + 'a');
|
2012-08-05 12:15:15 +00:00
|
|
|
strcat(cp, ":");
|
|
|
|
return (buf);
|
2011-06-30 16:08:56 +00:00
|
|
|
}
|
|
|
|
|
2012-08-05 12:15:15 +00:00
|
|
|
int
|
2022-11-30 22:08:15 +00:00
|
|
|
disk_parsedev(struct devdesc **idev, const char *devspec, const char **path)
|
2011-06-30 16:08:56 +00:00
|
|
|
{
|
2012-08-05 12:15:15 +00:00
|
|
|
int unit, slice, partition;
|
|
|
|
const char *np;
|
|
|
|
char *cp;
|
2022-11-30 22:08:15 +00:00
|
|
|
struct disk_devdesc *dev;
|
2012-08-05 12:15:15 +00:00
|
|
|
|
2022-11-30 22:09:36 +00:00
|
|
|
np = devspec + 4; /* Skip the leading 'disk' */
|
2019-03-24 18:51:52 +00:00
|
|
|
unit = -1;
|
2019-04-25 15:09:21 +00:00
|
|
|
/*
|
|
|
|
* If there is path/file info after the device info, then any missing
|
|
|
|
* slice or partition info should be considered a request to search for
|
|
|
|
* an appropriate partition. Otherwise we want to open the raw device
|
|
|
|
* itself and not try to fill in missing info by searching.
|
|
|
|
*/
|
|
|
|
if ((cp = strchr(np, ':')) != NULL && cp[1] != '\0') {
|
|
|
|
slice = D_SLICEWILD;
|
|
|
|
partition = D_PARTWILD;
|
|
|
|
} else {
|
|
|
|
slice = D_SLICENONE;
|
|
|
|
partition = D_PARTNONE;
|
|
|
|
}
|
|
|
|
|
2012-08-05 12:15:15 +00:00
|
|
|
if (*np != '\0' && *np != ':') {
|
|
|
|
unit = strtol(np, &cp, 10);
|
|
|
|
if (cp == np)
|
|
|
|
return (EUNIT);
|
2011-06-30 16:08:56 +00:00
|
|
|
#ifdef LOADER_GPT_SUPPORT
|
2012-08-05 12:15:15 +00:00
|
|
|
if (*cp == 'p') {
|
|
|
|
np = cp + 1;
|
|
|
|
slice = strtol(np, &cp, 10);
|
|
|
|
if (np == cp)
|
|
|
|
return (ESLICE);
|
|
|
|
/* we don't support nested partitions on GPT */
|
|
|
|
if (*cp != '\0' && *cp != ':')
|
|
|
|
return (EINVAL);
|
2019-05-01 05:42:13 +00:00
|
|
|
partition = D_PARTISGPT;
|
2012-08-05 12:15:15 +00:00
|
|
|
} else
|
2011-06-30 16:08:56 +00:00
|
|
|
#endif
|
2011-07-01 18:31:59 +00:00
|
|
|
#ifdef LOADER_MBR_SUPPORT
|
2012-08-05 12:15:15 +00:00
|
|
|
if (*cp == 's') {
|
|
|
|
np = cp + 1;
|
|
|
|
slice = strtol(np, &cp, 10);
|
|
|
|
if (np == cp)
|
|
|
|
return (ESLICE);
|
|
|
|
}
|
2011-07-01 18:31:59 +00:00
|
|
|
#endif
|
2012-08-05 12:15:15 +00:00
|
|
|
if (*cp != '\0' && *cp != ':') {
|
|
|
|
partition = *cp - 'a';
|
|
|
|
if (partition < 0)
|
|
|
|
return (EPART);
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
if (*cp != '\0' && *cp != ':')
|
|
|
|
return (EINVAL);
|
2022-11-30 22:08:15 +00:00
|
|
|
dev = malloc(sizeof(*dev));
|
|
|
|
if (dev == NULL)
|
|
|
|
return (ENOMEM);
|
2018-03-12 21:39:49 +00:00
|
|
|
dev->dd.d_unit = unit;
|
2012-08-05 12:15:15 +00:00
|
|
|
dev->d_slice = slice;
|
|
|
|
dev->d_partition = partition;
|
2022-11-30 22:08:15 +00:00
|
|
|
*idev = &dev->dd;
|
2012-08-05 12:15:15 +00:00
|
|
|
if (path != NULL)
|
|
|
|
*path = (*cp == '\0') ? cp: cp + 1;
|
|
|
|
return (0);
|
2011-06-30 16:08:56 +00:00
|
|
|
}
|