1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-29 12:03:03 +00:00
freebsd/lib/libdisk/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

406 lines
8.4 KiB
C

/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
* ----------------------------------------------------------------------------
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <inttypes.h>
#include <err.h>
#include <sys/sysctl.h>
#include <sys/stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/disklabel.h>
#include <sys/uuid.h>
#include <sys/gpt.h>
#include <paths.h>
#include "libdisk.h"
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <uuid.h>
const enum platform platform =
#if defined (P_DEBUG)
P_DEBUG
#elif defined (PC98)
p_pc98
#elif defined(__i386__)
p_i386
#elif defined(__alpha__)
p_alpha
#elif defined(__sparc64__)
p_sparc64
#elif defined(__ia64__)
p_ia64
#elif defined(__ppc__)
p_ppc
#elif defined(__amd64__)
p_amd64
#else
IHAVENOIDEA
#endif
;
const char *
chunk_name(chunk_e type)
{
switch(type) {
case unused: return ("unused");
case mbr: return ("mbr");
case part: return ("part");
case gpt: return ("gpt");
case pc98: return ("pc98");
case sun: return ("sun");
case freebsd: return ("freebsd");
case fat: return ("fat");
case spare: return ("spare");
case efi: return ("efi");
default: return ("??");
}
};
struct disk *
Open_Disk(const char *name)
{
char *conftxt;
size_t txtsize;
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 = malloc(txtsize+1);
if (conftxt == NULL) {
warn("cannot malloc memory for conftxt");
return (NULL);
}
error = sysctlbyname("kern.geom.conftxt", conftxt, &txtsize, NULL, 0);
if (error) {
warn("error reading kern.geom.conftxt from the system");
free(conftxt);
return (NULL);
}
conftxt[txtsize] = '\0'; /* in case kernel bug is still there */
return Int_Open_Disk(name, conftxt);
}
void
Debug_Disk(struct disk *d)
{
printf("Debug_Disk(%s)", d->name);
#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);
#if defined(PC98)
printf(" boot1=%p, boot2=%p, bootipl=%p, bootmenu=%p\n",
d->boot1, d->boot2, d->bootipl, d->bootmenu);
#elif defined(__i386__) || defined(__amd64__)
printf(" boot1=%p, boot2=%p, bootmgr=%p\n",
d->boot1, d->boot2, d->bootmgr);
#elif defined(__alpha__)
printf(" boot1=%p, bootmgr=%p\n",
d->boot1, d->bootmgr);
#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);
}
void
Free_Disk(struct disk *d)
{
if (d->chunks)
Free_Chunk(d->chunks);
if (d->name)
free(d->name);
#ifdef PC98
if (d->bootipl)
free(d->bootipl);
if (d->bootmenu)
free(d->bootmenu);
#else
#if !defined(__ia64__)
if (d->bootmgr)
free(d->bootmgr);
#endif
#endif
#if !defined(__ia64__)
if (d->boot1)
free(d->boot1);
#endif
#if defined(__i386__) || defined(__amd64__)
if (d->boot2)
free(d->boot2);
#endif
free(d);
}
#if 0
void
Collapse_Disk(struct disk *d)
{
while (Collapse_Chunk(d, d->chunks))
;
}
#endif
static int
qstrcmp(const void* a, const void* b)
{
char *str1 = *(char**)a;
char *str2 = *(char**)b;
return strcmp(str1, str2);
}
char **
Disk_Names()
{
int disk_cnt;
static char **disks;
int error;
size_t listsize;
char *disklist;
error = sysctlbyname("kern.disks", NULL, &listsize, NULL, 0);
if (error) {
warn("kern.disks sysctl not available");
return NULL;
}
if (listsize == 0)
return (NULL);
disks = malloc(sizeof *disks * (1 + MAX_NO_DISKS));
if (disks == NULL)
return NULL;
disklist = (char *)malloc(listsize + 1);
if (disklist == NULL) {
free(disks);
return NULL;
}
memset(disks,0,sizeof *disks * (1 + MAX_NO_DISKS));
memset(disklist, 0, listsize + 1);
error = sysctlbyname("kern.disks", disklist, &listsize, NULL, 0);
if (error || disklist[0] == 0) {
free(disklist);
free(disks);
return NULL;
}
for (disk_cnt = 0; disk_cnt < MAX_NO_DISKS; disk_cnt++) {
disks[disk_cnt] = strsep(&disklist, " ");
if (disks[disk_cnt] == NULL)
break;
}
qsort(disks, disk_cnt, sizeof(char*), qstrcmp);
return disks;
}
#ifdef PC98
void
Set_Boot_Mgr(struct disk *d, const u_char *bootipl, const size_t bootipl_size,
const u_char *bootmenu, const size_t bootmenu_size)
#else
void
Set_Boot_Mgr(struct disk *d, const u_char *b, const size_t s)
#endif
{
#if !defined(__ia64__)
#ifdef PC98
if (d->sector_size == 0)
return;
if (bootipl_size % d->sector_size != 0)
return;
if (d->bootipl)
free(d->bootipl);
if (!bootipl) {
d->bootipl = NULL;
} else {
d->bootipl_size = bootipl_size;
d->bootipl = malloc(bootipl_size);
if (!d->bootipl)
return;
memcpy(d->bootipl, bootipl, bootipl_size);
}
if (bootmenu_size % d->sector_size != 0)
return;
if (d->bootmenu)
free(d->bootmenu);
if (!bootmenu) {
d->bootmenu = NULL;
} else {
d->bootmenu_size = bootmenu_size;
d->bootmenu = malloc(bootmenu_size);
if (!d->bootmenu)
return;
memcpy(d->bootmenu, bootmenu, bootmenu_size);
}
#else
if (d->sector_size == 0)
return;
if (s % d->sector_size != 0)
return;
if (d->bootmgr)
free(d->bootmgr);
if (!b) {
d->bootmgr = NULL;
} else {
d->bootmgr_size = s;
d->bootmgr = malloc(s);
if (!d->bootmgr)
return;
memcpy(d->bootmgr, b, s);
}
#endif
#endif
}
int
Set_Boot_Blocks(struct disk *d, const u_char *b1, const u_char *b2)
{
#if defined(__i386__) || defined(__amd64__)
if (d->boot1)
free(d->boot1);
d->boot1 = malloc(512);
if (!d->boot1)
return -1;
memcpy(d->boot1, b1, 512);
if (d->boot2)
free(d->boot2);
d->boot2 = malloc(15 * 512);
if (!d->boot2)
return -1;
memcpy(d->boot2, b2, 15 * 512);
#elif defined(__alpha__)
if (d->boot1)
free(d->boot1);
d->boot1 = malloc(15 * 512);
if (!d->boot1)
return -1;
memcpy(d->boot1, b1, 15 * 512);
#elif defined(__sparc64__)
if (d->boot1 != NULL)
free(d->boot1);
d->boot1 = malloc(16 * 512);
if (d->boot1 == NULL)
return (-1);
memcpy(d->boot1, b1, 16 * 512);
#elif defined(__ia64__)
/* nothing */
#else
/* Should be: #error "Set_Boot_Blocks: unknown arch"; */
#endif
return 0;
}
#ifdef PC98
const char *
slice_type_name( int type, int subtype )
{
switch (type) {
case whole:
return "whole";
case fat:
return "fat";
case freebsd:
switch (subtype) {
case 0xc494: return "freebsd";
default: return "unknown";
}
case unused:
return "unused";
default:
return "unknown";
}
}
#else /* PC98 */
const char *
slice_type_name( int type, int subtype )
{
switch (type) {
case whole:
return "whole";
case mbr:
switch (subtype) {
case 1: return "fat (12-bit)";
case 2: return "XENIX /";
case 3: return "XENIX /usr";
case 4: return "fat (16-bit,<=32Mb)";
case 5: return "extended DOS";
case 6: return "fat (16-bit,>32Mb)";
case 7: return "NTFS/HPFS/QNX";
case 8: return "AIX bootable";
case 9: return "AIX data";
case 10: return "OS/2 bootmgr";
case 11: return "fat (32-bit)";
case 12: return "fat (32-bit,LBA)";
case 14: return "fat (16-bit,>32Mb,LBA)";
case 15: return "extended DOS, LBA";
case 18: return "Compaq Diagnostic";
case 84: return "OnTrack diskmgr";
case 100: return "Netware 2.x";
case 101: return "Netware 3.x";
case 115: return "SCO UnixWare";
case 128: return "Minix 1.1";
case 129: return "Minix 1.5";
case 130: return "linux_swap";
case 131: return "ext2fs";
case 166: return "OpenBSD FFS"; /* 0xA6 */
case 169: return "NetBSD FFS"; /* 0xA9 */
case 182: return "OpenBSD"; /* dedicated */
case 183: return "bsd/os";
case 184: return "bsd/os swap";
case 238: return "EFI GPT";
case 239: return "EFI Sys. Part.";
default: return "unknown";
}
case fat:
return "fat";
case freebsd:
switch (subtype) {
case 165: return "freebsd";
default: return "unknown";
}
case extended:
return "extended";
case part:
return "part";
case efi:
return "efi";
case unused:
return "unused";
default:
return "unknown";
}
}
#endif /* PC98 */