summaryrefslogtreecommitdiffstats
path: root/lib/libdisk/disk.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libdisk/disk.c')
-rw-r--r--lib/libdisk/disk.c304
1 files changed, 13 insertions, 291 deletions
diff --git a/lib/libdisk/disk.c b/lib/libdisk/disk.c
index 6690fba..42eea97 100644
--- a/lib/libdisk/disk.c
+++ b/lib/libdisk/disk.c
@@ -33,14 +33,6 @@ __FBSDID("$FreeBSD$");
#include <assert.h>
#include <uuid.h>
-#ifdef DEBUG
-#define DPRINT(x) warn x
-#define DPRINTX(x) warnx x
-#else
-#define DPRINT(x)
-#define DPRINTX(x)
-#endif
-
const enum platform platform =
#if defined (P_DEBUG)
P_DEBUG
@@ -81,303 +73,32 @@ chunk_name(chunk_e type)
}
};
-static chunk_e
-uuid_type(uuid_t *uuid)
-{
- static uuid_t _efi = GPT_ENT_TYPE_EFI;
- static uuid_t _mbr = GPT_ENT_TYPE_MBR;
- static uuid_t _fbsd = GPT_ENT_TYPE_FREEBSD;
- static uuid_t _swap = GPT_ENT_TYPE_FREEBSD_SWAP;
- static uuid_t _ufs = GPT_ENT_TYPE_FREEBSD_UFS;
- static uuid_t _vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
-
- if (uuid_is_nil(uuid, NULL))
- return (unused);
- if (uuid_equal(uuid, &_efi, NULL))
- return (efi);
- if (uuid_equal(uuid, &_mbr, NULL))
- return (mbr);
- if (uuid_equal(uuid, &_fbsd, NULL))
- return (freebsd);
- if (uuid_equal(uuid, &_swap, NULL))
- return (part);
- if (uuid_equal(uuid, &_ufs, NULL))
- return (part);
- if (uuid_equal(uuid, &_vinum, NULL))
- return (part);
- return (spare);
-}
-
struct disk *
Open_Disk(const char *name)
{
-
- return Int_Open_Disk(name);
-}
-
-struct disk *
-Int_Open_Disk(const char *name)
-{
- uuid_t uuid;
- char *conftxt = NULL;
- struct disk *d;
+ char *conftxt;
size_t txtsize;
- int error, i;
- char *p, *q, *r, *a, *b, *n, *t, *sn;
- off_t o, len, off;
- u_int l, s, ty, sc, hd, alt;
- off_t lo[10];
+ int error;
error = sysctlbyname("kern.geom.conftxt", NULL, &txtsize, NULL, 0);
if (error) {
warn("kern.geom.conftxt sysctl not available, giving up!");
return (NULL);
}
- conftxt = (char *) malloc(txtsize+1);
+ conftxt = malloc(txtsize+1);
if (conftxt == NULL) {
- DPRINT(("cannot malloc memory for conftxt"));
+ warn("cannot malloc memory for conftxt");
return (NULL);
}
error = sysctlbyname("kern.geom.conftxt", conftxt, &txtsize, NULL, 0);
if (error) {
- DPRINT(("error reading kern.geom.conftxt from the system"));
+ warn("error reading kern.geom.conftxt from the system");
free(conftxt);
return (NULL);
}
conftxt[txtsize] = '\0'; /* in case kernel bug is still there */
- for (p = conftxt; p != NULL && *p; p = strchr(p, '\n')) {
- if (*p == '\n')
- p++;
- a = strsep(&p, " ");
- if (strcmp(a, "0"))
- continue;
-
- a = strsep(&p, " ");
- if (strcmp(a, "DISK"))
- continue;
-
- a = strsep(&p, " ");
- if (strcmp(a, name))
- continue;
- break;
- }
-
- q = strchr(p, '\n');
- if (q != NULL)
- *q++ = '\0';
-
- d = (struct disk *)calloc(sizeof *d, 1);
- if(d == NULL)
- return NULL;
-
- d->name = strdup(name);
-
- a = strsep(&p, " "); /* length in bytes */
- len = strtoimax(a, &r, 0);
- if (*r) {
- printf("BARF %d <%d>\n", __LINE__, *r);
- exit (0);
- }
-
- a = strsep(&p, " "); /* sectorsize */
- s = strtoul(a, &r, 0);
- if (*r) {
- printf("BARF %d <%d>\n", __LINE__, *r);
- exit (0);
- }
-
- if (s == 0)
- return (NULL);
- d->sector_size = s;
- len /= s; /* media size in number of sectors. */
-
- if (Add_Chunk(d, 0, len, name, whole, 0, 0, "-"))
- DPRINT(("Failed to add 'whole' chunk"));
-
- for (;;) {
- a = strsep(&p, " ");
- if (a == NULL)
- break;
- b = strsep(&p, " ");
- o = strtoul(b, &r, 0);
- if (*r) {
- printf("BARF %d <%d>\n", __LINE__, *r);
- exit (0);
- }
- if (!strcmp(a, "hd"))
- d->bios_hd = o;
- else if (!strcmp(a, "sc"))
- d->bios_sect = o;
- else
- printf("HUH ? <%s> <%s>\n", a, b);
- }
-
- /*
- * Calculate the number of cylinders this disk must have. If we have
- * an obvious insanity, we set the number of cyclinders to zero.
- */
- o = d->bios_hd * d->bios_sect;
- d->bios_cyl = (o != 0) ? len / o : 0;
-
- p = q;
- lo[0] = 0;
-
- for (; p != NULL && *p; p = q) {
- q = strchr(p, '\n');
- if (q != NULL)
- *q++ = '\0';
- a = strsep(&p, " "); /* Index */
- if (!strcmp(a, "0"))
- break;
- l = strtoimax(a, &r, 0);
- if (*r) {
- printf("BARF %d <%d>\n", __LINE__, *r);
- exit (0);
- }
- t = strsep(&p, " "); /* Type {SUN, BSD, MBR, PC98, GPT} */
- n = strsep(&p, " "); /* name */
- a = strsep(&p, " "); /* len */
- len = strtoimax(a, &r, 0);
- if (*r) {
- printf("BARF %d <%d>\n", __LINE__, *r);
- exit (0);
- }
- a = strsep(&p, " "); /* secsize */
- s = strtoimax(a, &r, 0);
- if (*r) {
- printf("BARF %d <%d>\n", __LINE__, *r);
- exit (0);
- }
- for (;;) {
- a = strsep(&p, " ");
- if (a == NULL)
- break;
- /* XXX: Slice name may include a space. */
- if (!strcmp(a, "sn")) {
- sn = p;
- break;
- }
- b = strsep(&p, " ");
- o = strtoimax(b, &r, 0);
- if (*r) {
- uint32_t status;
-
- uuid_from_string(b, &uuid, &status);
- if (status != uuid_s_ok) {
- printf("BARF %d <%d>\n", __LINE__, *r);
- exit (0);
- }
- o = uuid_type(&uuid);
- }
- if (!strcmp(a, "o"))
- off = o;
- else if (!strcmp(a, "i"))
- i = o;
- else if (!strcmp(a, "ty"))
- ty = o;
- else if (!strcmp(a, "sc"))
- sc = o;
- else if (!strcmp(a, "hd"))
- hd = o;
- else if (!strcmp(a, "alt"))
- alt = o;
- }
-
- /* PLATFORM POLICY BEGIN ----------------------------------- */
- if (platform == p_sparc64 && !strcmp(t, "SUN") && i == 2)
- continue;
- if (platform == p_sparc64 && !strcmp(t, "SUN") &&
- d->chunks->part->part == NULL) {
- d->bios_hd = hd;
- d->bios_sect = sc;
- o = d->chunks->size / (hd * sc);
- o *= (hd * sc);
- o -= alt * hd * sc;
- if (Add_Chunk(d, 0, o, name, freebsd, 0, 0, "-"))
- DPRINT(("Failed to add 'freebsd' chunk"));
- }
- if (platform == p_alpha && !strcmp(t, "BSD") &&
- d->chunks->part->part == NULL) {
- if (Add_Chunk(d, 0, d->chunks->size, name, freebsd,
- 0, 0, "-"))
- DPRINT(("Failed to add 'freebsd' chunk"));
- }
- if (!strcmp(t, "BSD") && i == RAW_PART)
- continue;
- /* PLATFORM POLICY END ------------------------------------- */
-
- off /= s;
- len /= s;
- off += lo[l - 1];
- lo[l] = off;
- if (!strcmp(t, "SUN"))
- i = Add_Chunk(d, off, len, n, part, 0, 0, 0);
- else if (!strncmp(t, "MBR", 3)) {
- switch (ty) {
- case 0xa5:
- i = Add_Chunk(d, off, len, n, freebsd, ty, 0, 0);
- break;
- case 0x01:
- case 0x04:
- case 0x06:
- case 0x0b:
- case 0x0c:
- case 0x0e:
- i = Add_Chunk(d, off, len, n, fat, ty, 0, 0);
- break;
- case 0xef: /* EFI */
- i = Add_Chunk(d, off, len, n, efi, ty, 0, 0);
- break;
- default:
- i = Add_Chunk(d, off, len, n, mbr, ty, 0, 0);
- break;
- }
- } else if (!strcmp(t, "BSD"))
- i = Add_Chunk(d, off, len, n, part, ty, 0, 0);
- else if (!strcmp(t, "PC98")) {
- switch (ty & 0x7f) {
- case 0x14:
- i = Add_Chunk(d, off, len, n, freebsd, ty, 0,
- sn);
- break;
- case 0x20:
- case 0x21:
- case 0x22:
- case 0x23:
- case 0x24:
- i = Add_Chunk(d, off, len, n, fat, ty, 0, sn);
- break;
- default:
- i = Add_Chunk(d, off, len, n, pc98, ty, 0, sn);
- break;
- }
- } else if (!strcmp(t, "GPT"))
- i = Add_Chunk(d, off, len, n, ty, 0, 0, 0);
- else if (!strcmp(t, "BDE"))
- ; /* nothing */
- else if (!strcmp(t, "CCD"))
- ; /* nothing */
- else {
- printf("BARF %d\n", __LINE__);
- exit(0);
- }
- }
- /* PLATFORM POLICY BEGIN ------------------------------------- */
- /* We have a chance to do things on a blank disk here */
- if (platform == p_sparc64 && d->chunks->part->part == NULL) {
- hd = d->bios_hd;
- sc = d->bios_sect;
- o = d->chunks->size / (hd * sc);
- o *= (hd * sc);
- o -= 2 * hd * sc;
- if (Add_Chunk(d, 0, o, name, freebsd, 0, 0, "-"))
- DPRINT(("Failed to add 'freebsd' chunk"));
- }
- /* PLATFORM POLICY END --------------------------------------- */
-
- return (d);
- i = 0;
+ return Int_Open_Disk(name, conftxt);
}
void
@@ -385,10 +106,8 @@ Debug_Disk(struct disk *d)
{
printf("Debug_Disk(%s)", d->name);
-#if 0
- printf(" real_geom=%lu/%lu/%lu",
- d->real_cyl, d->real_hd, d->real_sect);
-#endif
+
+#ifndef __ia64__
printf(" bios_geom=%lu/%lu/%lu = %lu\n",
d->bios_cyl, d->bios_hd, d->bios_sect,
d->bios_cyl * d->bios_hd * d->bios_sect);
@@ -401,11 +120,14 @@ Debug_Disk(struct disk *d)
#elif defined(__alpha__)
printf(" boot1=%p, bootmgr=%p\n",
d->boot1, d->bootmgr);
-#elif defined(__ia64__)
- printf("\n");
#else
/* Should be: error "Debug_Disk: unknown arch"; */
#endif
+#else /* __ia64__ */
+ printf(" media size=%lu, sector size=%lu\n", d->media_size,
+ d->sector_size);
+#endif
+
Debug_Chunk(d->chunks);
}
OpenPOWER on IntegriCloud