freebsd-skq/lib/libdisk/open_ia64_disk.c
Marcel Moolenaar f5ea9b34c6 o Move Int_Open_Disk() from disk.c to open_disk.c for use by all
platforms except ia64 and use Int_Open_Disk() in open_ia64_disk.c
   on ia64. We need to know more than GEOM can provide us so we're
   forced to read from the disk. Move uuid_type() to open_ia64_disk.c
   and remove all references on non-ia64.
o  Pass the GEOM conftxt to Int_Open_Disk() so that only Open_Disk()
   needs to know about GEOM and libdisk can more easily be used with
   media not handled by GEOM.
o  Create an ia64 specific definiton of struct disk on ia64, because
   we don't need/have most of the fields other platforms need and
   other fields not applicable on platforms other than ia64.
o  Do not compile change.c on ia64. It's too PC specific.
o  In Fixup_Names() in create_chunk.c, try all partition numbers
   that are valid for the GPT disk. We have the total number of
   partitions that can be allocated in the disk structure on ia64.
   Also, use the GPT partition naming if we're creating one under
   a chunk of type "whole". It's a GPT partition in that case.
o  In Create_Chunk(), compile-out the PC specific code on ia64 that
   checks BIOS geometry restrictions.
o  In Debug_Disk() in disk.c, dump the ia64 specific fields.
o  Save the partition index in the chunk on ia64 so that we can
   preserve it when we write the data back to disk. This avoids that
   partitions get moved around or swapped after installing FreeBSD,
   which may render a disk unusable.
2003-11-02 08:39:08 +00:00

274 lines
6.7 KiB
C

/*
* Copyright (c) 2003 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 ``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 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$");
#include <sys/types.h>
#include <sys/disklabel.h>
#include <sys/diskmbr.h>
#include <sys/gpt.h>
#include <sys/uuid.h>
#include <fcntl.h>
#include <inttypes.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <uuid.h>
#include "libdisk.h"
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 struct disk *
parse_disk(char *conftxt, const char *name)
{
char devname[64];
struct disk *disk;
struct dos_partition *part;
struct gpt_hdr *gpt;
char *buffer, *p, *q;
int fd, i;
disk = (struct disk *)calloc(sizeof *disk, 1);
if (disk == NULL)
return (NULL);
disk->name = strdup(name);
p = strsep(&conftxt, " "); /* media size */
disk->media_size = strtoimax(p, &q, 0);
if (*q)
goto fail;
p = strsep(&conftxt, " "); /* sector size */
disk->sector_size = strtoul(p, &q, 0);
if (*q)
goto fail;
if (disk->sector_size == 0)
disk->sector_size = 512;
if (disk->media_size % disk->sector_size)
goto fail;
/*
* We need to read the disk to get GPT specific information.
*/
snprintf(devname, sizeof(devname), "%s%s", _PATH_DEV, name);
fd = open(devname, O_RDONLY);
if (fd == -1)
goto fail;
buffer = malloc(2 * disk->sector_size);
if (buffer == NULL) {
close (fd);
goto fail;
}
if (read(fd, buffer, 2 * disk->sector_size) == -1) {
free(buffer);
close(fd);
goto fail;
}
close(fd);
gpt = (struct gpt_hdr *)(buffer + disk->sector_size);
if (memcmp(gpt->hdr_sig, GPT_HDR_SIG, sizeof(gpt->hdr_sig))) {
/*
* No GPT present. Check if the MBR is empty (if present)
* or is a PMBR before declaring this disk as empty. If
* the MBR isn't empty, bail out. Let's not risk nuking a
* disk.
*/
if (*(u_short *)(buffer + DOSMAGICOFFSET) == DOSMAGIC) {
for (i = 0; i < 4; i++) {
part = (struct dos_partition *)
(buffer + DOSPARTOFF + i * DOSPARTSIZE);
if (part->dp_typ != 0 &&
part->dp_typ != DOSPTYP_PMBR)
break;
}
if (i < 4) {
free(buffer);
goto fail;
}
}
disk->gpt_size = 128;
disk->lba_start = (disk->gpt_size * sizeof(struct gpt_ent)) /
disk->sector_size + 2;
disk->lba_end = (disk->media_size * disk->sector_size) -
disk->lba_start + 1;
} else {
disk->lba_start = gpt->hdr_lba_start;
disk->lba_end = gpt->hdr_lba_end;
disk->gpt_size = gpt->hdr_entries;
}
free(buffer);
Add_Chunk(disk, disk->lba_start, disk->lba_end - disk->lba_start + 1,
name, whole, 0, 0, "-");
return (disk);
fail:
free(disk->name);
free(disk);
return (NULL);
}
struct disk *
Int_Open_Disk(const char *name, char *conftxt)
{
struct chunk chunk;
uuid_t uuid;
struct disk *disk;
char *p, *q, *r, *s, *sd, *type;
u_long i;
uint32_t status;
p = conftxt;
while (p != NULL && *p != 0) {
q = strsep(&p, " ");
if (strcmp(q, "0") == 0) {
q = strsep(&p, " ");
if (strcmp(q, "DISK") == 0) {
q = strsep(&p, " ");
if (strcmp(q, name) == 0)
break;
}
}
p = strchr(p, '\n');
if (p != NULL && *p == '\n')
p++;
conftxt = p;
}
if (p == NULL || *p == 0)
return (NULL);
conftxt = strchr(p, '\n');
if (conftxt != NULL)
*conftxt++ = '\0';
disk = parse_disk(p, name);
if (disk == NULL)
return (NULL);
while (conftxt != NULL && *conftxt != 0) {
p = conftxt;
conftxt = strchr(p, '\n');
if (conftxt != NULL)
*conftxt++ = '\0';
sd = strsep(&p, " "); /* depth */
if (strcmp(sd, "0") == 0)
break;
type = strsep(&p, " "); /* type */
chunk.name = strsep(&p, " "); /* name */
q = strsep(&p, " "); /* length */
i = strtoimax(q, &r, 0);
if (*r)
abort();
chunk.end = i / disk->sector_size;
q = strsep(&p, " "); /* sector size */
for (;;) {
q = strsep(&p, " ");
if (q == NULL)
break;
r = strsep(&p, " ");
i = strtoimax(r, &s, 0);
if (*s) {
uuid_from_string(r, &uuid, &status);
if (status != uuid_s_ok)
abort();
} else
status = uuid_s_invalid_string_uuid;
if (!strcmp(q, "o"))
chunk.offset = i / disk->sector_size;
else if (!strcmp(q, "i"))
chunk.flags = CHUNK_ITOF(i) | CHUNK_HAS_INDEX;
else if (!strcmp(q, "ty"))
chunk.subtype = i;
}
if (strncmp(type, "MBR", 3) == 0) {
switch (chunk.subtype) {
case 0xa5:
chunk.type = freebsd;
break;
case 0x01:
case 0x04:
case 0x06:
case 0x0b:
case 0x0c:
case 0x0e:
chunk.type = fat;
break;
case 0xef: /* EFI */
chunk.type = efi;
break;
default:
chunk.type = mbr;
break;
}
} else if (strcmp(type, "BSD") == 0) {
chunk.type = part;
} else if (strcmp(type, "GPT") == 0) {
chunk.subtype = 0;
if (status != uuid_s_ok)
abort();
if (uuid_is_nil(&uuid, NULL))
chunk.type = unused;
else if (uuid_equal(&uuid, &_efi, NULL))
chunk.type = efi;
else if (uuid_equal(&uuid, &_mbr, NULL))
chunk.type = unknown;
else if (uuid_equal(&uuid, &_fbsd, NULL)) {
chunk.type = freebsd;
chunk.subtype = 0xa5;
} else if (uuid_equal(&uuid, &_swap, NULL)) {
chunk.type = part;
chunk.subtype = FS_SWAP;
} else if (uuid_equal(&uuid, &_ufs, NULL)) {
chunk.type = part;
chunk.subtype = FS_BSDFFS;
} else
chunk.type = unknown;
} else
abort();
Add_Chunk(disk, chunk.offset, chunk.end, chunk.name,
chunk.type, chunk.subtype, chunk.flags, 0);
}
return (disk);
}