2007-06-13 04:27:36 +00:00
|
|
|
/*-
|
2017-11-27 15:17:37 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2008-06-18 01:13:34 +00:00
|
|
|
* Copyright (c) 2007, 2008 Marcel Moolenaar
|
2007-06-13 04:27:36 +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 ``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/param.h>
|
|
|
|
#include <sys/bio.h>
|
|
|
|
#include <sys/diskmbr.h>
|
|
|
|
#include <sys/endian.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/kobj.h>
|
|
|
|
#include <sys/limits.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/sbuf.h>
|
|
|
|
#include <sys/systm.h>
|
2011-02-25 10:24:35 +00:00
|
|
|
#include <sys/sysctl.h>
|
2007-06-13 04:27:36 +00:00
|
|
|
#include <geom/geom.h>
|
2014-05-04 16:43:57 +00:00
|
|
|
#include <geom/geom_int.h>
|
2007-06-13 04:27:36 +00:00
|
|
|
#include <geom/part/g_part.h>
|
|
|
|
|
|
|
|
#include "g_part_if.h"
|
|
|
|
|
2011-02-25 10:24:35 +00:00
|
|
|
FEATURE(geom_part_mbr, "GEOM partitioning class for MBR support");
|
|
|
|
|
2014-08-12 09:10:13 +00:00
|
|
|
SYSCTL_DECL(_kern_geom_part);
|
|
|
|
static SYSCTL_NODE(_kern_geom_part, OID_AUTO, mbr, CTLFLAG_RW, 0,
|
|
|
|
"GEOM_PART_MBR Master Boot Record");
|
|
|
|
|
2014-08-12 10:31:31 +00:00
|
|
|
static u_int enforce_chs = 0;
|
2014-08-12 09:10:13 +00:00
|
|
|
SYSCTL_UINT(_kern_geom_part_mbr, OID_AUTO, enforce_chs,
|
2014-08-12 10:31:31 +00:00
|
|
|
CTLFLAG_RWTUN, &enforce_chs, 0, "Enforce alignment to CHS addressing");
|
2014-08-12 09:10:13 +00:00
|
|
|
|
2007-06-13 04:27:36 +00:00
|
|
|
#define MBRSIZE 512
|
|
|
|
|
|
|
|
struct g_part_mbr_table {
|
|
|
|
struct g_part_table base;
|
|
|
|
u_char mbr[MBRSIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct g_part_mbr_entry {
|
|
|
|
struct g_part_entry base;
|
|
|
|
struct dos_partition ent;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int g_part_mbr_add(struct g_part_table *, struct g_part_entry *,
|
|
|
|
struct g_part_parms *);
|
2008-04-13 19:54:54 +00:00
|
|
|
static int g_part_mbr_bootcode(struct g_part_table *, struct g_part_parms *);
|
2007-06-13 04:27:36 +00:00
|
|
|
static int g_part_mbr_create(struct g_part_table *, struct g_part_parms *);
|
|
|
|
static int g_part_mbr_destroy(struct g_part_table *, struct g_part_parms *);
|
2009-02-08 07:05:23 +00:00
|
|
|
static void g_part_mbr_dumpconf(struct g_part_table *, struct g_part_entry *,
|
2008-04-23 20:13:05 +00:00
|
|
|
struct sbuf *, const char *);
|
2007-06-13 04:27:36 +00:00
|
|
|
static int g_part_mbr_dumpto(struct g_part_table *, struct g_part_entry *);
|
2017-11-21 05:42:13 +00:00
|
|
|
static int g_part_mbr_modify(struct g_part_table *, struct g_part_entry *,
|
2007-06-13 04:27:36 +00:00
|
|
|
struct g_part_parms *);
|
2009-02-08 07:05:23 +00:00
|
|
|
static const char *g_part_mbr_name(struct g_part_table *, struct g_part_entry *,
|
2007-06-13 04:27:36 +00:00
|
|
|
char *, size_t);
|
|
|
|
static int g_part_mbr_probe(struct g_part_table *, struct g_consumer *);
|
|
|
|
static int g_part_mbr_read(struct g_part_table *, struct g_consumer *);
|
2008-06-18 01:13:34 +00:00
|
|
|
static int g_part_mbr_setunset(struct g_part_table *, struct g_part_entry *,
|
|
|
|
const char *, unsigned int);
|
2007-06-13 04:27:36 +00:00
|
|
|
static const char *g_part_mbr_type(struct g_part_table *, struct g_part_entry *,
|
|
|
|
char *, size_t);
|
|
|
|
static int g_part_mbr_write(struct g_part_table *, struct g_consumer *);
|
2010-04-23 03:11:39 +00:00
|
|
|
static int g_part_mbr_resize(struct g_part_table *, struct g_part_entry *,
|
|
|
|
struct g_part_parms *);
|
2007-06-13 04:27:36 +00:00
|
|
|
|
|
|
|
static kobj_method_t g_part_mbr_methods[] = {
|
|
|
|
KOBJMETHOD(g_part_add, g_part_mbr_add),
|
2008-04-13 19:54:54 +00:00
|
|
|
KOBJMETHOD(g_part_bootcode, g_part_mbr_bootcode),
|
2007-06-13 04:27:36 +00:00
|
|
|
KOBJMETHOD(g_part_create, g_part_mbr_create),
|
|
|
|
KOBJMETHOD(g_part_destroy, g_part_mbr_destroy),
|
2008-04-23 20:13:05 +00:00
|
|
|
KOBJMETHOD(g_part_dumpconf, g_part_mbr_dumpconf),
|
2007-06-13 04:27:36 +00:00
|
|
|
KOBJMETHOD(g_part_dumpto, g_part_mbr_dumpto),
|
|
|
|
KOBJMETHOD(g_part_modify, g_part_mbr_modify),
|
2010-04-23 03:11:39 +00:00
|
|
|
KOBJMETHOD(g_part_resize, g_part_mbr_resize),
|
2007-06-13 04:27:36 +00:00
|
|
|
KOBJMETHOD(g_part_name, g_part_mbr_name),
|
|
|
|
KOBJMETHOD(g_part_probe, g_part_mbr_probe),
|
|
|
|
KOBJMETHOD(g_part_read, g_part_mbr_read),
|
2008-06-18 01:13:34 +00:00
|
|
|
KOBJMETHOD(g_part_setunset, g_part_mbr_setunset),
|
2007-06-13 04:27:36 +00:00
|
|
|
KOBJMETHOD(g_part_type, g_part_mbr_type),
|
|
|
|
KOBJMETHOD(g_part_write, g_part_mbr_write),
|
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct g_part_scheme g_part_mbr_scheme = {
|
|
|
|
"MBR",
|
|
|
|
g_part_mbr_methods,
|
|
|
|
sizeof(struct g_part_mbr_table),
|
|
|
|
.gps_entrysz = sizeof(struct g_part_mbr_entry),
|
|
|
|
.gps_minent = NDOSPART,
|
|
|
|
.gps_maxent = NDOSPART,
|
2008-04-13 19:54:54 +00:00
|
|
|
.gps_bootcodesz = MBRSIZE,
|
2007-06-13 04:27:36 +00:00
|
|
|
};
|
2008-03-23 01:31:59 +00:00
|
|
|
G_PART_SCHEME_DECLARE(g_part_mbr);
|
2018-04-10 19:18:16 +00:00
|
|
|
MODULE_VERSION(geom_part_mbr, 0);
|
2007-06-13 04:27:36 +00:00
|
|
|
|
2011-01-28 11:13:01 +00:00
|
|
|
static struct g_part_mbr_alias {
|
|
|
|
u_char typ;
|
|
|
|
int alias;
|
|
|
|
} mbr_alias_match[] = {
|
|
|
|
{ DOSPTYP_386BSD, G_PART_ALIAS_FREEBSD },
|
2018-06-17 20:06:27 +00:00
|
|
|
{ DOSPTYP_APPLE_BOOT, G_PART_ALIAS_APPLE_BOOT },
|
|
|
|
{ DOSPTYP_APPLE_UFS, G_PART_ALIAS_APPLE_UFS },
|
2018-06-17 20:10:48 +00:00
|
|
|
{ DOSPTYP_EFI, G_PART_ALIAS_EFI },
|
2011-01-28 11:13:01 +00:00
|
|
|
{ DOSPTYP_EXT, G_PART_ALIAS_EBR },
|
2018-06-17 20:06:27 +00:00
|
|
|
{ DOSPTYP_EXTLBA, G_PART_ALIAS_EBR },
|
2013-03-07 22:32:41 +00:00
|
|
|
{ DOSPTYP_FAT16, G_PART_ALIAS_MS_FAT16 },
|
2011-01-28 11:13:01 +00:00
|
|
|
{ DOSPTYP_FAT32, G_PART_ALIAS_MS_FAT32 },
|
2018-05-04 00:34:27 +00:00
|
|
|
{ DOSPTYP_FAT32LBA, G_PART_ALIAS_MS_FAT32LBA },
|
2018-06-17 20:06:27 +00:00
|
|
|
{ DOSPTYP_HFS, G_PART_ALIAS_APPLE_HFS },
|
2012-02-10 09:55:18 +00:00
|
|
|
{ DOSPTYP_LDM, G_PART_ALIAS_MS_LDM_DATA },
|
2011-01-28 11:13:01 +00:00
|
|
|
{ DOSPTYP_LINLVM, G_PART_ALIAS_LINUX_LVM },
|
|
|
|
{ DOSPTYP_LINRAID, G_PART_ALIAS_LINUX_RAID },
|
2018-06-17 20:06:27 +00:00
|
|
|
{ DOSPTYP_LINSWP, G_PART_ALIAS_LINUX_SWAP },
|
|
|
|
{ DOSPTYP_LINUX, G_PART_ALIAS_LINUX_DATA },
|
|
|
|
{ DOSPTYP_NTFS, G_PART_ALIAS_MS_NTFS },
|
2014-07-04 15:55:32 +00:00
|
|
|
{ DOSPTYP_PPCBOOT, G_PART_ALIAS_PREP_BOOT },
|
2012-04-18 11:59:03 +00:00
|
|
|
{ DOSPTYP_VMFS, G_PART_ALIAS_VMFS },
|
|
|
|
{ DOSPTYP_VMKDIAG, G_PART_ALIAS_VMKDIAG },
|
2011-01-28 11:13:01 +00:00
|
|
|
};
|
|
|
|
|
2007-06-13 04:27:36 +00:00
|
|
|
static int
|
|
|
|
mbr_parse_type(const char *type, u_char *dp_typ)
|
|
|
|
{
|
|
|
|
const char *alias;
|
|
|
|
char *endp;
|
|
|
|
long lt;
|
2011-01-28 11:13:01 +00:00
|
|
|
int i;
|
2007-06-13 04:27:36 +00:00
|
|
|
|
|
|
|
if (type[0] == '!') {
|
|
|
|
lt = strtol(type + 1, &endp, 0);
|
|
|
|
if (type[1] == '\0' || *endp != '\0' || lt <= 0 || lt >= 256)
|
|
|
|
return (EINVAL);
|
|
|
|
*dp_typ = (u_char)lt;
|
|
|
|
return (0);
|
|
|
|
}
|
2016-04-20 16:19:44 +00:00
|
|
|
for (i = 0; i < nitems(mbr_alias_match); i++) {
|
2011-01-28 11:13:01 +00:00
|
|
|
alias = g_part_alias_name(mbr_alias_match[i].alias);
|
|
|
|
if (strcasecmp(type, alias) == 0) {
|
|
|
|
*dp_typ = mbr_alias_match[i].typ;
|
|
|
|
return (0);
|
|
|
|
}
|
2010-06-26 13:20:40 +00:00
|
|
|
}
|
2007-06-13 04:27:36 +00:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
2008-02-29 22:41:36 +00:00
|
|
|
static int
|
|
|
|
mbr_probe_bpb(u_char *bpb)
|
|
|
|
{
|
|
|
|
uint16_t secsz;
|
|
|
|
uint8_t clstsz;
|
|
|
|
|
|
|
|
#define PO2(x) ((x & (x - 1)) == 0)
|
|
|
|
secsz = le16dec(bpb);
|
|
|
|
if (secsz < 512 || secsz > 4096 || !PO2(secsz))
|
|
|
|
return (0);
|
|
|
|
clstsz = bpb[2];
|
|
|
|
if (clstsz < 1 || clstsz > 128 || !PO2(clstsz))
|
|
|
|
return (0);
|
|
|
|
#undef PO2
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2007-06-17 22:19:19 +00:00
|
|
|
static void
|
|
|
|
mbr_set_chs(struct g_part_table *table, uint32_t lba, u_char *cylp, u_char *hdp,
|
|
|
|
u_char *secp)
|
|
|
|
{
|
|
|
|
uint32_t cyl, hd, sec;
|
|
|
|
|
|
|
|
sec = lba % table->gpt_sectors + 1;
|
|
|
|
lba /= table->gpt_sectors;
|
|
|
|
hd = lba % table->gpt_heads;
|
|
|
|
lba /= table->gpt_heads;
|
|
|
|
cyl = lba;
|
|
|
|
if (cyl > 1023)
|
|
|
|
sec = hd = cyl = ~0;
|
|
|
|
|
|
|
|
*cylp = cyl & 0xff;
|
|
|
|
*hdp = hd & 0xff;
|
|
|
|
*secp = (sec & 0x3f) | ((cyl >> 2) & 0xc0);
|
|
|
|
}
|
|
|
|
|
2014-05-04 10:14:25 +00:00
|
|
|
static int
|
|
|
|
mbr_align(struct g_part_table *basetable, uint32_t *start, uint32_t *size)
|
|
|
|
{
|
|
|
|
uint32_t sectors;
|
|
|
|
|
2014-08-12 09:10:13 +00:00
|
|
|
if (enforce_chs == 0)
|
|
|
|
return (0);
|
2014-05-04 10:14:25 +00:00
|
|
|
sectors = basetable->gpt_sectors;
|
|
|
|
if (*size < sectors)
|
|
|
|
return (EINVAL);
|
|
|
|
if (start != NULL && (*start % sectors)) {
|
|
|
|
*size += (*start % sectors) - sectors;
|
|
|
|
*start -= (*start % sectors) - sectors;
|
|
|
|
}
|
|
|
|
if (*size % sectors)
|
|
|
|
*size -= (*size % sectors);
|
|
|
|
if (*size < sectors)
|
|
|
|
return (EINVAL);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2007-06-13 04:27:36 +00:00
|
|
|
static int
|
|
|
|
g_part_mbr_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
|
|
|
|
struct g_part_parms *gpp)
|
|
|
|
{
|
|
|
|
struct g_part_mbr_entry *entry;
|
2014-05-04 10:14:25 +00:00
|
|
|
uint32_t start, size;
|
2007-06-13 04:27:36 +00:00
|
|
|
|
|
|
|
if (gpp->gpp_parms & G_PART_PARM_LABEL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
entry = (struct g_part_mbr_entry *)baseentry;
|
|
|
|
start = gpp->gpp_start;
|
|
|
|
size = gpp->gpp_size;
|
2014-05-04 10:14:25 +00:00
|
|
|
if (mbr_align(basetable, &start, &size) != 0)
|
2007-06-13 04:27:36 +00:00
|
|
|
return (EINVAL);
|
|
|
|
if (baseentry->gpe_deleted)
|
|
|
|
bzero(&entry->ent, sizeof(entry->ent));
|
|
|
|
|
2010-10-13 11:35:59 +00:00
|
|
|
KASSERT(baseentry->gpe_start <= start, ("%s", __func__));
|
|
|
|
KASSERT(baseentry->gpe_end >= start + size - 1, ("%s", __func__));
|
2007-06-13 04:27:36 +00:00
|
|
|
baseentry->gpe_start = start;
|
|
|
|
baseentry->gpe_end = start + size - 1;
|
|
|
|
entry->ent.dp_start = start;
|
|
|
|
entry->ent.dp_size = size;
|
2007-06-17 22:19:19 +00:00
|
|
|
mbr_set_chs(basetable, baseentry->gpe_start, &entry->ent.dp_scyl,
|
|
|
|
&entry->ent.dp_shd, &entry->ent.dp_ssect);
|
|
|
|
mbr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
|
|
|
|
&entry->ent.dp_ehd, &entry->ent.dp_esect);
|
2007-06-13 04:27:36 +00:00
|
|
|
return (mbr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
|
|
|
|
}
|
|
|
|
|
2008-04-13 19:54:54 +00:00
|
|
|
static int
|
|
|
|
g_part_mbr_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
|
|
|
|
{
|
|
|
|
struct g_part_mbr_table *table;
|
2011-06-27 10:42:06 +00:00
|
|
|
uint32_t dsn;
|
|
|
|
|
|
|
|
if (gpp->gpp_codesize != MBRSIZE)
|
|
|
|
return (ENODEV);
|
2008-04-13 19:54:54 +00:00
|
|
|
|
|
|
|
table = (struct g_part_mbr_table *)basetable;
|
2011-06-27 10:42:06 +00:00
|
|
|
dsn = *(uint32_t *)(table->mbr + DOSDSNOFF);
|
|
|
|
bcopy(gpp->gpp_codeptr, table->mbr, DOSPARTOFF);
|
2018-11-27 14:58:19 +00:00
|
|
|
if (dsn != 0 && !gpp->gpp_skip_dsn)
|
2011-06-27 10:42:06 +00:00
|
|
|
*(uint32_t *)(table->mbr + DOSDSNOFF) = dsn;
|
2008-04-13 19:54:54 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2007-06-13 04:27:36 +00:00
|
|
|
static int
|
|
|
|
g_part_mbr_create(struct g_part_table *basetable, struct g_part_parms *gpp)
|
|
|
|
{
|
|
|
|
struct g_provider *pp;
|
|
|
|
struct g_part_mbr_table *table;
|
|
|
|
|
|
|
|
pp = gpp->gpp_provider;
|
2007-06-17 22:19:19 +00:00
|
|
|
if (pp->sectorsize < MBRSIZE)
|
2007-06-13 04:27:36 +00:00
|
|
|
return (ENOSPC);
|
|
|
|
|
2007-06-17 22:19:19 +00:00
|
|
|
basetable->gpt_first = basetable->gpt_sectors;
|
2011-05-25 09:32:19 +00:00
|
|
|
basetable->gpt_last = MIN(pp->mediasize / pp->sectorsize,
|
|
|
|
UINT32_MAX) - 1;
|
2007-06-13 04:27:36 +00:00
|
|
|
|
|
|
|
table = (struct g_part_mbr_table *)basetable;
|
|
|
|
le16enc(table->mbr + DOSMAGICOFFSET, DOSMAGIC);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
g_part_mbr_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Wipe the first sector to clear the partitioning. */
|
|
|
|
basetable->gpt_smhead |= 1;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2009-02-08 07:05:23 +00:00
|
|
|
static void
|
2017-11-21 05:42:13 +00:00
|
|
|
g_part_mbr_dumpconf(struct g_part_table *basetable, struct g_part_entry *baseentry,
|
2008-04-23 20:13:05 +00:00
|
|
|
struct sbuf *sb, const char *indent)
|
|
|
|
{
|
|
|
|
struct g_part_mbr_entry *entry;
|
2017-11-21 05:35:21 +00:00
|
|
|
struct g_part_mbr_table *table;
|
|
|
|
uint32_t dsn;
|
2017-11-21 05:42:13 +00:00
|
|
|
|
2017-11-21 05:35:21 +00:00
|
|
|
table = (struct g_part_mbr_table *)basetable;
|
2008-04-23 20:13:05 +00:00
|
|
|
entry = (struct g_part_mbr_entry *)baseentry;
|
2008-06-12 05:56:03 +00:00
|
|
|
if (indent == NULL) {
|
|
|
|
/* conftxt: libdisk compatibility */
|
|
|
|
sbuf_printf(sb, " xs MBR xt %u", entry->ent.dp_typ);
|
|
|
|
} else if (entry != NULL) {
|
|
|
|
/* confxml: partition entry information */
|
|
|
|
sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
|
|
|
|
entry->ent.dp_typ);
|
2008-06-18 01:13:34 +00:00
|
|
|
if (entry->ent.dp_flag & 0x80)
|
|
|
|
sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent);
|
2017-11-21 05:35:21 +00:00
|
|
|
dsn = le32dec(table->mbr + DOSDSNOFF);
|
2017-11-21 06:12:21 +00:00
|
|
|
sbuf_printf(sb, "%s<efimedia>HD(%d,MBR,%#08x,%#jx,%#jx)", indent,
|
2017-11-21 05:35:21 +00:00
|
|
|
entry->base.gpe_index, dsn, (intmax_t)entry->base.gpe_start,
|
|
|
|
(intmax_t)(entry->base.gpe_end - entry->base.gpe_start + 1));
|
|
|
|
sbuf_printf(sb, "</efimedia>\n");
|
2008-06-12 05:56:03 +00:00
|
|
|
} else {
|
|
|
|
/* confxml: scheme information */
|
|
|
|
}
|
2008-04-23 20:13:05 +00:00
|
|
|
}
|
|
|
|
|
2007-06-13 04:27:36 +00:00
|
|
|
static int
|
2017-11-21 05:42:13 +00:00
|
|
|
g_part_mbr_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
|
2007-06-13 04:27:36 +00:00
|
|
|
{
|
|
|
|
struct g_part_mbr_entry *entry;
|
|
|
|
|
2012-01-13 18:32:56 +00:00
|
|
|
/* Allow dumping to a FreeBSD partition or Linux swap partition only. */
|
2007-06-13 04:27:36 +00:00
|
|
|
entry = (struct g_part_mbr_entry *)baseentry;
|
2012-01-13 18:32:56 +00:00
|
|
|
return ((entry->ent.dp_typ == DOSPTYP_386BSD ||
|
|
|
|
entry->ent.dp_typ == DOSPTYP_LINSWP) ? 1 : 0);
|
2007-06-13 04:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
g_part_mbr_modify(struct g_part_table *basetable,
|
|
|
|
struct g_part_entry *baseentry, struct g_part_parms *gpp)
|
|
|
|
{
|
|
|
|
struct g_part_mbr_entry *entry;
|
|
|
|
|
|
|
|
if (gpp->gpp_parms & G_PART_PARM_LABEL)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
entry = (struct g_part_mbr_entry *)baseentry;
|
|
|
|
if (gpp->gpp_parms & G_PART_PARM_TYPE)
|
|
|
|
return (mbr_parse_type(gpp->gpp_type, &entry->ent.dp_typ));
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2010-04-23 03:11:39 +00:00
|
|
|
static int
|
|
|
|
g_part_mbr_resize(struct g_part_table *basetable,
|
|
|
|
struct g_part_entry *baseentry, struct g_part_parms *gpp)
|
|
|
|
{
|
|
|
|
struct g_part_mbr_entry *entry;
|
2013-10-17 16:18:43 +00:00
|
|
|
struct g_provider *pp;
|
2014-05-04 10:14:25 +00:00
|
|
|
uint32_t size;
|
2010-04-23 03:11:39 +00:00
|
|
|
|
2013-10-17 16:18:43 +00:00
|
|
|
if (baseentry == NULL) {
|
|
|
|
pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
|
|
|
|
basetable->gpt_last = MIN(pp->mediasize / pp->sectorsize,
|
|
|
|
UINT32_MAX) - 1;
|
|
|
|
return (0);
|
|
|
|
}
|
2010-04-23 03:11:39 +00:00
|
|
|
size = gpp->gpp_size;
|
2014-05-04 10:14:25 +00:00
|
|
|
if (mbr_align(basetable, NULL, &size) != 0)
|
2010-04-23 03:11:39 +00:00
|
|
|
return (EINVAL);
|
2014-05-04 16:43:57 +00:00
|
|
|
/* XXX: prevent unexpected shrinking. */
|
|
|
|
pp = baseentry->gpe_pp;
|
2014-05-07 11:18:27 +00:00
|
|
|
if ((g_debugflags & 0x10) == 0 && size < gpp->gpp_size &&
|
|
|
|
pp->mediasize / pp->sectorsize > size)
|
2014-05-04 16:43:57 +00:00
|
|
|
return (EBUSY);
|
2010-04-23 03:11:39 +00:00
|
|
|
entry = (struct g_part_mbr_entry *)baseentry;
|
|
|
|
baseentry->gpe_end = baseentry->gpe_start + size - 1;
|
|
|
|
entry->ent.dp_size = size;
|
|
|
|
mbr_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
|
|
|
|
&entry->ent.dp_ehd, &entry->ent.dp_esect);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2009-02-08 07:05:23 +00:00
|
|
|
static const char *
|
2007-06-13 04:27:36 +00:00
|
|
|
g_part_mbr_name(struct g_part_table *table, struct g_part_entry *baseentry,
|
|
|
|
char *buf, size_t bufsz)
|
|
|
|
{
|
|
|
|
|
|
|
|
snprintf(buf, bufsz, "s%d", baseentry->gpe_index);
|
|
|
|
return (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
g_part_mbr_probe(struct g_part_table *table, struct g_consumer *cp)
|
|
|
|
{
|
2009-02-08 23:51:44 +00:00
|
|
|
char psn[8];
|
2007-06-13 04:27:36 +00:00
|
|
|
struct g_provider *pp;
|
2008-02-28 22:30:41 +00:00
|
|
|
u_char *buf, *p;
|
2008-02-29 22:41:36 +00:00
|
|
|
int error, index, res, sum;
|
2008-02-28 22:30:41 +00:00
|
|
|
uint16_t magic;
|
2007-06-13 04:27:36 +00:00
|
|
|
|
|
|
|
pp = cp->provider;
|
|
|
|
|
|
|
|
/* Sanity-check the provider. */
|
|
|
|
if (pp->sectorsize < MBRSIZE || pp->mediasize < pp->sectorsize)
|
|
|
|
return (ENOSPC);
|
2008-02-28 22:30:41 +00:00
|
|
|
if (pp->sectorsize > 4096)
|
|
|
|
return (ENXIO);
|
2007-06-13 04:27:36 +00:00
|
|
|
|
2009-02-08 23:51:44 +00:00
|
|
|
/* We don't nest under an MBR (see EBR instead). */
|
|
|
|
error = g_getattr("PART::scheme", cp, &psn);
|
|
|
|
if (error == 0 && strcmp(psn, g_part_mbr_scheme.name) == 0)
|
|
|
|
return (ELOOP);
|
|
|
|
|
2007-06-13 04:27:36 +00:00
|
|
|
/* Check that there's a MBR. */
|
|
|
|
buf = g_read_data(cp, 0L, pp->sectorsize, &error);
|
|
|
|
if (buf == NULL)
|
|
|
|
return (error);
|
2008-02-28 22:30:41 +00:00
|
|
|
|
|
|
|
/* We goto out on mismatch. */
|
|
|
|
res = ENXIO;
|
|
|
|
|
|
|
|
magic = le16dec(buf + DOSMAGICOFFSET);
|
|
|
|
if (magic != DOSMAGIC)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
for (index = 0; index < NDOSPART; index++) {
|
|
|
|
p = buf + DOSPARTOFF + index * DOSPARTSIZE;
|
|
|
|
if (p[0] != 0 && p[0] != 0x80)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2008-02-29 22:41:36 +00:00
|
|
|
/*
|
|
|
|
* If the partition table does not consist of all zeroes,
|
|
|
|
* assume we have a MBR. If it's all zeroes, we could have
|
|
|
|
* a boot sector. For example, a boot sector that doesn't
|
|
|
|
* have boot code -- common on non-i386 hardware. In that
|
|
|
|
* case we check if we have a possible BPB. If so, then we
|
|
|
|
* assume we have a boot sector instead.
|
|
|
|
*/
|
|
|
|
sum = 0;
|
|
|
|
for (index = 0; index < NDOSPART * DOSPARTSIZE; index++)
|
|
|
|
sum += buf[DOSPARTOFF + index];
|
|
|
|
if (sum != 0 || !mbr_probe_bpb(buf + 0x0b))
|
|
|
|
res = G_PART_PROBE_PRI_NORM;
|
2008-02-28 22:30:41 +00:00
|
|
|
|
|
|
|
out:
|
2007-06-13 04:27:36 +00:00
|
|
|
g_free(buf);
|
2008-02-28 22:30:41 +00:00
|
|
|
return (res);
|
2007-06-13 04:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
g_part_mbr_read(struct g_part_table *basetable, struct g_consumer *cp)
|
|
|
|
{
|
|
|
|
struct dos_partition ent;
|
|
|
|
struct g_provider *pp;
|
|
|
|
struct g_part_mbr_table *table;
|
|
|
|
struct g_part_mbr_entry *entry;
|
|
|
|
u_char *buf, *p;
|
2011-05-27 06:37:42 +00:00
|
|
|
off_t chs, msize, first;
|
2007-06-17 22:19:19 +00:00
|
|
|
u_int sectors, heads;
|
|
|
|
int error, index;
|
2007-06-13 04:27:36 +00:00
|
|
|
|
|
|
|
pp = cp->provider;
|
|
|
|
table = (struct g_part_mbr_table *)basetable;
|
2011-05-27 06:37:42 +00:00
|
|
|
first = basetable->gpt_sectors;
|
2011-05-08 11:42:51 +00:00
|
|
|
msize = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
|
2007-06-13 04:27:36 +00:00
|
|
|
|
|
|
|
buf = g_read_data(cp, 0L, pp->sectorsize, &error);
|
|
|
|
if (buf == NULL)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
bcopy(buf, table->mbr, sizeof(table->mbr));
|
|
|
|
for (index = NDOSPART - 1; index >= 0; index--) {
|
|
|
|
p = buf + DOSPARTOFF + index * DOSPARTSIZE;
|
|
|
|
ent.dp_flag = p[0];
|
|
|
|
ent.dp_shd = p[1];
|
|
|
|
ent.dp_ssect = p[2];
|
|
|
|
ent.dp_scyl = p[3];
|
|
|
|
ent.dp_typ = p[4];
|
|
|
|
ent.dp_ehd = p[5];
|
|
|
|
ent.dp_esect = p[6];
|
|
|
|
ent.dp_ecyl = p[7];
|
|
|
|
ent.dp_start = le32dec(p + 8);
|
|
|
|
ent.dp_size = le32dec(p + 12);
|
|
|
|
if (ent.dp_typ == 0 || ent.dp_typ == DOSPTYP_PMBR)
|
|
|
|
continue;
|
|
|
|
if (ent.dp_start == 0 || ent.dp_size == 0)
|
|
|
|
continue;
|
2007-06-17 22:19:19 +00:00
|
|
|
sectors = ent.dp_esect & 0x3f;
|
|
|
|
if (sectors > basetable->gpt_sectors &&
|
|
|
|
!basetable->gpt_fixgeom) {
|
|
|
|
g_part_geometry_heads(msize, sectors, &chs, &heads);
|
|
|
|
if (chs != 0) {
|
|
|
|
basetable->gpt_sectors = sectors;
|
|
|
|
basetable->gpt_heads = heads;
|
|
|
|
}
|
|
|
|
}
|
2011-05-27 06:37:42 +00:00
|
|
|
if (ent.dp_start < first)
|
|
|
|
first = ent.dp_start;
|
2007-06-13 04:27:36 +00:00
|
|
|
entry = (struct g_part_mbr_entry *)g_part_new_entry(basetable,
|
|
|
|
index + 1, ent.dp_start, ent.dp_start + ent.dp_size - 1);
|
|
|
|
entry->ent = ent;
|
|
|
|
}
|
2007-06-17 22:19:19 +00:00
|
|
|
|
|
|
|
basetable->gpt_entries = NDOSPART;
|
|
|
|
basetable->gpt_first = basetable->gpt_sectors;
|
2011-05-25 09:32:19 +00:00
|
|
|
basetable->gpt_last = msize - 1;
|
2007-06-17 22:19:19 +00:00
|
|
|
|
2011-05-27 06:37:42 +00:00
|
|
|
if (first < basetable->gpt_first)
|
|
|
|
basetable->gpt_first = 1;
|
|
|
|
|
2010-09-25 18:27:29 +00:00
|
|
|
g_free(buf);
|
2007-06-13 04:27:36 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2008-06-18 01:13:34 +00:00
|
|
|
static int
|
|
|
|
g_part_mbr_setunset(struct g_part_table *table, struct g_part_entry *baseentry,
|
|
|
|
const char *attrib, unsigned int set)
|
|
|
|
{
|
|
|
|
struct g_part_entry *iter;
|
|
|
|
struct g_part_mbr_entry *entry;
|
|
|
|
int changed;
|
|
|
|
|
2013-06-09 23:34:26 +00:00
|
|
|
if (baseentry == NULL)
|
|
|
|
return (ENODEV);
|
2008-06-18 01:13:34 +00:00
|
|
|
if (strcasecmp(attrib, "active") != 0)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
/* Only one entry can have the active attribute. */
|
|
|
|
LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
|
|
|
|
if (iter->gpe_deleted)
|
|
|
|
continue;
|
|
|
|
changed = 0;
|
|
|
|
entry = (struct g_part_mbr_entry *)iter;
|
|
|
|
if (iter == baseentry) {
|
|
|
|
if (set && (entry->ent.dp_flag & 0x80) == 0) {
|
|
|
|
entry->ent.dp_flag |= 0x80;
|
|
|
|
changed = 1;
|
|
|
|
} else if (!set && (entry->ent.dp_flag & 0x80)) {
|
|
|
|
entry->ent.dp_flag &= ~0x80;
|
|
|
|
changed = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (set && (entry->ent.dp_flag & 0x80)) {
|
|
|
|
entry->ent.dp_flag &= ~0x80;
|
|
|
|
changed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (changed && !iter->gpe_created)
|
|
|
|
iter->gpe_modified = 1;
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2007-06-13 04:27:36 +00:00
|
|
|
static const char *
|
2017-11-21 05:42:13 +00:00
|
|
|
g_part_mbr_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
|
2007-06-13 04:27:36 +00:00
|
|
|
char *buf, size_t bufsz)
|
|
|
|
{
|
|
|
|
struct g_part_mbr_entry *entry;
|
2011-01-28 11:13:01 +00:00
|
|
|
int i;
|
2007-06-13 04:27:36 +00:00
|
|
|
|
|
|
|
entry = (struct g_part_mbr_entry *)baseentry;
|
2016-04-20 16:19:44 +00:00
|
|
|
for (i = 0; i < nitems(mbr_alias_match); i++) {
|
2011-01-28 11:13:01 +00:00
|
|
|
if (mbr_alias_match[i].typ == entry->ent.dp_typ)
|
|
|
|
return (g_part_alias_name(mbr_alias_match[i].alias));
|
2010-06-26 13:20:40 +00:00
|
|
|
}
|
2011-01-28 11:13:01 +00:00
|
|
|
snprintf(buf, bufsz, "!%d", entry->ent.dp_typ);
|
2007-06-13 04:27:36 +00:00
|
|
|
return (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
g_part_mbr_write(struct g_part_table *basetable, struct g_consumer *cp)
|
|
|
|
{
|
|
|
|
struct g_part_entry *baseentry;
|
|
|
|
struct g_part_mbr_entry *entry;
|
|
|
|
struct g_part_mbr_table *table;
|
|
|
|
u_char *p;
|
|
|
|
int error, index;
|
|
|
|
|
|
|
|
table = (struct g_part_mbr_table *)basetable;
|
|
|
|
baseentry = LIST_FIRST(&basetable->gpt_entry);
|
|
|
|
for (index = 1; index <= basetable->gpt_entries; index++) {
|
|
|
|
p = table->mbr + DOSPARTOFF + (index - 1) * DOSPARTSIZE;
|
|
|
|
entry = (baseentry != NULL && index == baseentry->gpe_index)
|
|
|
|
? (struct g_part_mbr_entry *)baseentry : NULL;
|
|
|
|
if (entry != NULL && !baseentry->gpe_deleted) {
|
|
|
|
p[0] = entry->ent.dp_flag;
|
|
|
|
p[1] = entry->ent.dp_shd;
|
|
|
|
p[2] = entry->ent.dp_ssect;
|
|
|
|
p[3] = entry->ent.dp_scyl;
|
|
|
|
p[4] = entry->ent.dp_typ;
|
|
|
|
p[5] = entry->ent.dp_ehd;
|
|
|
|
p[6] = entry->ent.dp_esect;
|
|
|
|
p[7] = entry->ent.dp_ecyl;
|
|
|
|
le32enc(p + 8, entry->ent.dp_start);
|
|
|
|
le32enc(p + 12, entry->ent.dp_size);
|
|
|
|
} else
|
|
|
|
bzero(p, DOSPARTSIZE);
|
|
|
|
|
|
|
|
if (entry != NULL)
|
|
|
|
baseentry = LIST_NEXT(baseentry, gpe_entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
error = g_write_data(cp, 0, table->mbr, cp->provider->sectorsize);
|
|
|
|
return (error);
|
|
|
|
}
|