Changeset View
Changeset View
Standalone View
Standalone View
stand/common/disk.c
Show First 20 Lines • Show All 80 Lines • ▼ Show 20 Lines | |||||
int | int | ||||
ptblread(void *d, void *buf, size_t blocks, uint64_t offset) | ptblread(void *d, void *buf, size_t blocks, uint64_t offset) | ||||
{ | { | ||||
struct disk_devdesc *dev; | struct disk_devdesc *dev; | ||||
struct open_disk *od; | struct open_disk *od; | ||||
dev = (struct disk_devdesc *)d; | dev = (struct disk_devdesc *)d; | ||||
od = (struct open_disk *)dev->d_opendata; | od = (struct open_disk *)dev->dd.d_opendata; | ||||
/* | /* | ||||
* The strategy function assumes the offset is in units of 512 byte | * The strategy function assumes the offset is in units of 512 byte | ||||
* sectors. For larger sector sizes, we need to adjust the offset to | * sectors. For larger sector sizes, we need to adjust the offset to | ||||
* match the actual sector size. | * match the actual sector size. | ||||
*/ | */ | ||||
offset *= (od->sectorsize / 512); | offset *= (od->sectorsize / 512); | ||||
/* | /* | ||||
* As the GPT backup partition is located at the end of the disk, | * 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. | * to avoid reading past disk end, flag bcache not to use RA. | ||||
*/ | */ | ||||
return (dev->d_dev->dv_strategy(dev, F_READ | F_NORA, offset, | return (dev->dd.d_dev->dv_strategy(dev, F_READ | F_NORA, offset, | ||||
blocks * od->sectorsize, (char *)buf, NULL)); | blocks * od->sectorsize, (char *)buf, NULL)); | ||||
} | } | ||||
#define PWIDTH 35 | #define PWIDTH 35 | ||||
static int | static int | ||||
ptable_print(void *arg, const char *pname, const struct ptable_entry *part) | ptable_print(void *arg, const char *pname, const struct ptable_entry *part) | ||||
{ | { | ||||
struct disk_devdesc dev; | struct disk_devdesc dev; | ||||
struct print_args *pa, bsd; | struct print_args *pa, bsd; | ||||
struct open_disk *od; | struct open_disk *od; | ||||
struct ptable *table; | struct ptable *table; | ||||
char line[80]; | char line[80]; | ||||
int res; | int res; | ||||
pa = (struct print_args *)arg; | pa = (struct print_args *)arg; | ||||
od = (struct open_disk *)pa->dev->d_opendata; | od = (struct open_disk *)pa->dev->dd.d_opendata; | ||||
sprintf(line, " %s%s: %s", pa->prefix, pname, | sprintf(line, " %s%s: %s", pa->prefix, pname, | ||||
parttype2str(part->type)); | parttype2str(part->type)); | ||||
if (pa->verbose) | if (pa->verbose) | ||||
sprintf(line, "%-*s%s", PWIDTH, line, | sprintf(line, "%-*s%s", PWIDTH, line, | ||||
display_size(part->end - part->start + 1, | display_size(part->end - part->start + 1, | ||||
od->sectorsize)); | od->sectorsize)); | ||||
strcat(line, "\n"); | strcat(line, "\n"); | ||||
if (pager_output(line)) | if (pager_output(line)) | ||||
return 1; | return 1; | ||||
res = 0; | res = 0; | ||||
if (part->type == PART_FREEBSD) { | if (part->type == PART_FREEBSD) { | ||||
/* Open slice with BSD label */ | /* Open slice with BSD label */ | ||||
dev.d_dev = pa->dev->d_dev; | dev.dd.d_dev = pa->dev->dd.d_dev; | ||||
dev.d_unit = pa->dev->d_unit; | dev.dd.d_unit = pa->dev->dd.d_unit; | ||||
dev.d_slice = part->index; | dev.d_slice = part->index; | ||||
dev.d_partition = -1; | dev.d_partition = -1; | ||||
if (disk_open(&dev, part->end - part->start + 1, | if (disk_open(&dev, part->end - part->start + 1, | ||||
od->sectorsize) == 0) { | od->sectorsize) == 0) { | ||||
table = ptable_open(&dev, part->end - part->start + 1, | table = ptable_open(&dev, part->end - part->start + 1, | ||||
od->sectorsize, ptblread); | od->sectorsize, ptblread); | ||||
if (table != NULL) { | if (table != NULL) { | ||||
sprintf(line, " %s%s", pa->prefix, pname); | sprintf(line, " %s%s", pa->prefix, pname); | ||||
Show All 13 Lines | |||||
int | int | ||||
disk_print(struct disk_devdesc *dev, char *prefix, int verbose) | disk_print(struct disk_devdesc *dev, char *prefix, int verbose) | ||||
{ | { | ||||
struct open_disk *od; | struct open_disk *od; | ||||
struct print_args pa; | struct print_args pa; | ||||
/* Disk should be opened */ | /* Disk should be opened */ | ||||
od = (struct open_disk *)dev->d_opendata; | od = (struct open_disk *)dev->dd.d_opendata; | ||||
pa.dev = dev; | pa.dev = dev; | ||||
pa.prefix = prefix; | pa.prefix = prefix; | ||||
pa.verbose = verbose; | pa.verbose = verbose; | ||||
return (ptable_iterate(od->table, &pa, ptable_print)); | return (ptable_iterate(od->table, &pa, ptable_print)); | ||||
} | } | ||||
int | int | ||||
disk_read(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks) | disk_read(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks) | ||||
{ | { | ||||
struct open_disk *od; | struct open_disk *od; | ||||
int ret; | int ret; | ||||
od = (struct open_disk *)dev->d_opendata; | od = (struct open_disk *)dev->dd.d_opendata; | ||||
ret = dev->d_dev->dv_strategy(dev, F_READ, dev->d_offset + offset, | ret = dev->dd.d_dev->dv_strategy(dev, F_READ, dev->d_offset + offset, | ||||
blocks * od->sectorsize, buf, NULL); | blocks * od->sectorsize, buf, NULL); | ||||
return (ret); | return (ret); | ||||
} | } | ||||
int | int | ||||
disk_write(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks) | disk_write(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks) | ||||
{ | { | ||||
struct open_disk *od; | struct open_disk *od; | ||||
int ret; | int ret; | ||||
od = (struct open_disk *)dev->d_opendata; | od = (struct open_disk *)dev->dd.d_opendata; | ||||
ret = dev->d_dev->dv_strategy(dev, F_WRITE, dev->d_offset + offset, | ret = dev->dd.d_dev->dv_strategy(dev, F_WRITE, dev->d_offset + offset, | ||||
blocks * od->sectorsize, buf, NULL); | blocks * od->sectorsize, buf, NULL); | ||||
return (ret); | return (ret); | ||||
} | } | ||||
int | int | ||||
disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *data) | disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *data) | ||||
{ | { | ||||
struct open_disk *od = dev->d_opendata; | struct open_disk *od = dev->dd.d_opendata; | ||||
if (od == NULL) | if (od == NULL) | ||||
return (ENOTTY); | return (ENOTTY); | ||||
switch (cmd) { | switch (cmd) { | ||||
case DIOCGSECTORSIZE: | case DIOCGSECTORSIZE: | ||||
*(u_int *)data = od->sectorsize; | *(u_int *)data = od->sectorsize; | ||||
break; | break; | ||||
Show All 27 Lines | disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize) | ||||
table = NULL; | table = NULL; | ||||
slice = dev->d_slice; | slice = dev->d_slice; | ||||
partition = dev->d_partition; | partition = dev->d_partition; | ||||
od = (struct open_disk *)malloc(sizeof(struct open_disk)); | od = (struct open_disk *)malloc(sizeof(struct open_disk)); | ||||
if (od == NULL) { | if (od == NULL) { | ||||
DEBUG("no memory"); | DEBUG("no memory"); | ||||
return (ENOMEM); | return (ENOMEM); | ||||
} | } | ||||
dev->d_opendata = od; | dev->dd.d_opendata = od; | ||||
od->entrysize = 0; | od->entrysize = 0; | ||||
od->mediasize = mediasize; | od->mediasize = mediasize; | ||||
od->sectorsize = sectorsize; | od->sectorsize = sectorsize; | ||||
DEBUG("%s unit %d, slice %d, partition %d => %p", | DEBUG("%s unit %d, slice %d, partition %d => %p", | ||||
disk_fmtdev(dev), dev->d_unit, dev->d_slice, dev->d_partition, od); | disk_fmtdev(dev), dev->d_unit, dev->d_slice, dev->d_partition, od); | ||||
/* Determine disk layout. */ | /* Determine disk layout. */ | ||||
od->table = ptable_open(dev, mediasize / sectorsize, sectorsize, | od->table = ptable_open(dev, mediasize / sectorsize, sectorsize, | ||||
▲ Show 20 Lines • Show All 93 Lines • ▼ Show 20 Lines | out: | ||||
return (rc); | return (rc); | ||||
} | } | ||||
int | int | ||||
disk_close(struct disk_devdesc *dev) | disk_close(struct disk_devdesc *dev) | ||||
{ | { | ||||
struct open_disk *od; | struct open_disk *od; | ||||
od = (struct open_disk *)dev->d_opendata; | od = (struct open_disk *)dev->dd.d_opendata; | ||||
DEBUG("%s closed => %p", disk_fmtdev(dev), od); | DEBUG("%s closed => %p", disk_fmtdev(dev), od); | ||||
ptable_close(od->table); | ptable_close(od->table); | ||||
free(od); | free(od); | ||||
return (0); | return (0); | ||||
} | } | ||||
char* | char* | ||||
disk_fmtdev(struct disk_devdesc *dev) | disk_fmtdev(struct disk_devdesc *dev) | ||||
{ | { | ||||
static char buf[128]; | static char buf[128]; | ||||
char *cp; | char *cp; | ||||
cp = buf + sprintf(buf, "%s%d", dev->d_dev->dv_name, dev->d_unit); | cp = buf + sprintf(buf, "%s%d", dev->dd.d_dev->dv_name, dev->dd.d_unit); | ||||
if (dev->d_slice >= 0) { | if (dev->d_slice >= 0) { | ||||
#ifdef LOADER_GPT_SUPPORT | #ifdef LOADER_GPT_SUPPORT | ||||
if (dev->d_partition == 255) { | if (dev->d_partition == 255) { | ||||
sprintf(cp, "p%d:", dev->d_slice); | sprintf(cp, "p%d:", dev->d_slice); | ||||
return (buf); | return (buf); | ||||
} else | } else | ||||
#endif | #endif | ||||
#ifdef LOADER_MBR_SUPPORT | #ifdef LOADER_MBR_SUPPORT | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | if (*cp != '\0' && *cp != ':') { | ||||
return (EPART); | return (EPART); | ||||
cp++; | cp++; | ||||
} | } | ||||
} else | } else | ||||
return (EINVAL); | return (EINVAL); | ||||
if (*cp != '\0' && *cp != ':') | if (*cp != '\0' && *cp != ':') | ||||
return (EINVAL); | return (EINVAL); | ||||
dev->d_unit = unit; | dev->dd.d_unit = unit; | ||||
dev->d_slice = slice; | dev->d_slice = slice; | ||||
dev->d_partition = partition; | dev->d_partition = partition; | ||||
if (path != NULL) | if (path != NULL) | ||||
*path = (*cp == '\0') ? cp: cp + 1; | *path = (*cp == '\0') ? cp: cp + 1; | ||||
return (0); | return (0); | ||||
} | } |