Example #1
0
static int
recover_gpt_hdr(gpt_t gpt, int type, off_t last)
{
	const char *name, *origname;
	map_t *dgpt, dtbl, sgpt, stbl __unused;
	struct gpt_hdr *hdr;

	if (gpt_add_hdr(gpt, type, last) == -1)
		return -1;

	switch (type) {
	case MAP_TYPE_PRI_GPT_HDR:
		dgpt = &gpt->gpt;
		dtbl = gpt->tbl;
		sgpt = gpt->tpg;
		stbl = gpt->lbt;
		origname = "secondary";
		name = "primary";
		break;
	case MAP_TYPE_SEC_GPT_HDR:
		dgpt = &gpt->tpg;
		dtbl = gpt->lbt;
		sgpt = gpt->gpt;
		stbl = gpt->tbl;
		origname = "primary";
		name = "secondary";
		break;
	default:
		gpt_warn(gpt, "Bad table type %d", type);
		return -1;
	}

	memcpy((*dgpt)->map_data, sgpt->map_data, gpt->secsz);
	hdr = (*dgpt)->map_data;
	hdr->hdr_lba_self = htole64((uint64_t)(*dgpt)->map_start);
	hdr->hdr_lba_alt = htole64((uint64_t)sgpt->map_start);
	hdr->hdr_lba_table = htole64((uint64_t)dtbl->map_start);
	hdr->hdr_crc_self = 0;
	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
	if (gpt_write(gpt, *dgpt) == -1) {
		gpt_warnx(gpt, "Writing %s GPT header failed", name);
		return -1;
	}
	gpt_msg(gpt, "Recovered %s GPT header from %s", name, origname);
	return 0;
}
Example #2
0
static int
recover_gpt_tbl(gpt_t gpt, int type, off_t start)
{
	const char *name, *origname;
	map_t *dtbl, stbl;

	switch (type) {
	case MAP_TYPE_PRI_GPT_TBL:
		dtbl = &gpt->tbl;
		stbl = gpt->lbt;
		origname = "secondary";
		name = "primary";
		break;
	case MAP_TYPE_SEC_GPT_TBL:
		dtbl = &gpt->lbt;
		stbl = gpt->tbl;
		origname = "primary";
		name = "secondary";
		break;
	default:
		gpt_warn(gpt, "Bad table type %d", type);
		return -1;
	}

	*dtbl = map_add(gpt, start, stbl->map_size, type, stbl->map_data, 0);
	if (*dtbl == NULL) {
		gpt_warnx(gpt, "Adding %s GPT table failed", name);
		return -1;
	}
	if (gpt_write(gpt, *dtbl) == -1) {
		gpt_warnx(gpt, "Writing %s GPT table failed", name);
		return -1;
	}
	gpt_msg(gpt, "Recovered %s GPT table from %s", name, origname);
	return 0;
}
Example #3
0
static int
resize(gpt_t gpt, u_int entry, off_t alignment, off_t sectors, off_t size)
{
	map_t map;
	struct gpt_hdr *hdr;
	struct gpt_ent *ent;
	unsigned int i;
	off_t alignsecs, newsize;
	uint64_t end;
	

	if ((hdr = gpt_hdr(gpt)) == NULL)
		return -1;

	i = entry - 1;
	ent = gpt_ent_primary(gpt, i);
	if (gpt_uuid_is_nil(ent->ent_type)) {
		gpt_warnx(gpt, "Entry at index %u is unused", entry);
		return -1;
	}

	alignsecs = alignment / gpt->secsz;

	for (map = map_first(gpt); map != NULL; map = map->map_next) {
		if (entry == map->map_index)
			break;
	}
	if (map == NULL) {
		gpt_warnx(gpt, "Could not find map entry corresponding "
		    "to index");
		return -1;
	}

	if (sectors > 0 && sectors == map->map_size)
		if (alignment == 0 ||
		    (alignment > 0 && sectors % alignsecs == 0)) {
			/* nothing to do */
			gpt_warnx(gpt, "partition does not need resizing");
			return 0;
		}

	newsize = map_resize(gpt, map, sectors, alignsecs);
	if (newsize == -1)
		return -1;

	end = htole64((uint64_t)(map->map_start + newsize - 1LL));
	ent->ent_lba_end = end;

	if (gpt_write_primary(gpt) == -1)
		return -1;

	ent = gpt_ent(gpt->gpt, gpt->lbt, i);
	ent->ent_lba_end = end;

	if (gpt_write_backup(gpt) == -1)
		return -1;

	gpt_msg(gpt, "Partition %d resized: %" PRIu64 " %" PRIu64, entry,
	    map->map_start, newsize);

	return 0;
}
Example #4
0
static int
biosboot(gpt_t gpt, daddr_t start, uint64_t size, u_int entry, uint8_t *label,
    const char *bootpath)
{
	map_t mbrmap, m;
	struct mbr *mbr, *bootcode;
	unsigned int i;
	struct gpt_ent *ent;
	uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1];

	/*
	 * Parse and validate partition maps
	 */
	if (gpt_hdr(gpt) == NULL)
		return -1;

	mbrmap = map_find(gpt, MAP_TYPE_PMBR);
	if (mbrmap == NULL || mbrmap->map_start != 0) {
		gpt_warnx(gpt, "No valid Protective MBR found");
		return -1;
	}

	mbr = mbrmap->map_data;

	/*
	 * Update the boot code
	 */
	if ((bootcode = read_boot(gpt, bootpath)) == NULL) {
		gpt_warnx(gpt, "Error reading bootcode");
		return -1;
	}
	(void)memcpy(&mbr->mbr_code, &bootcode->mbr_code,
		sizeof(mbr->mbr_code));
	free(bootcode);

	/*
	 * Walk through the GPT and see where we can boot from
	 */
	for (m = map_first(gpt); m != NULL; m = m->map_next) {
		if (m->map_type != MAP_TYPE_GPT_PART || m->map_index < 1)
			continue;

		ent = m->map_data;

		/* first, prefer user selection */
		if (entry > 0 && m->map_index == entry)
			break;

		if (label != NULL) {
			utf16_to_utf8(ent->ent_name, utfbuf, sizeof(utfbuf));
			if (strcmp((char *)label, (char *)utfbuf) == 0)
				break;
		}

		/* next, partition as could be specified by wedge */
		if (entry < 1 && label == NULL && size > 0 &&
		    m->map_start == start && m->map_size == (off_t)size)
			break;
	}

	if (m == NULL) {
		gpt_warnx(gpt, "No bootable partition");
		return -1;
	}

	i = m->map_index - 1;


	if (set_bootable(gpt, gpt->gpt, gpt->tbl, i) == -1)
		return -1;

	if (set_bootable(gpt, gpt->tpg, gpt->lbt, i) == -1)
		return -1;

	if (gpt_write(gpt, mbrmap) == -1) {
		gpt_warnx(gpt, "Cannot update Protective MBR");
		return -1;
	}

	gpt_msg(gpt, "Partition %d marked as bootable", i + 1);
	return 0;
}
Example #5
0
static int
migrate(gpt_t gpt, u_int parts, int force, int slice, int active)
{
	off_t last = gpt_last(gpt);
	map_t map;
	struct gpt_ent *ent;
	struct mbr *mbr;
	uint32_t start, size;
	unsigned int i;
	gpt_type_t type = GPT_TYPE_INVALID;

	map = map_find(gpt, MAP_TYPE_MBR);
	if (map == NULL || map->map_start != 0) {
		gpt_warnx(gpt, "No MBR in disk to convert");
		return -1;
	}

	mbr = map->map_data;

	if (gpt_create(gpt, last, parts, 0) == -1)
		return -1;

	ent = gpt->tbl->map_data;

	/* Mirror partitions. */
	for (i = 0; i < 4; i++) {
		start = le16toh(mbr->mbr_part[i].part_start_hi);
		start = (start << 16) + le16toh(mbr->mbr_part[i].part_start_lo);
		size = le16toh(mbr->mbr_part[i].part_size_hi);
		size = (size << 16) + le16toh(mbr->mbr_part[i].part_size_lo);

		if (gpt->verbose > 1)
			gpt_msg(gpt, "MBR partition %u type %s", i,
			    mbrptypename(mbr->mbr_part[i].part_typ));
		switch (mbr->mbr_part[i].part_typ) {
		case MBR_PTYPE_UNUSED:
			continue;

		case MBR_PTYPE_386BSD: /* FreeBSD */
			if (slice) {
				type = GPT_TYPE_FREEBSD;
				break;
			} else {
				ent = migrate_disklabel(gpt, start, ent,
				    freebsd_fstype_to_gpt_type);
				continue;
			}

		case MBR_PTYPE_NETBSD:	/* NetBSD */
			ent = migrate_disklabel(gpt, start, ent,
			    netbsd_fstype_to_gpt_type);
			continue;

		case MBR_PTYPE_EFI:
			type = GPT_TYPE_EFI;
			break;

		default:
			if (!force) {
				gpt_warnx(gpt, "unknown partition type (%d)",
				    mbr->mbr_part[i].part_typ);
				return -1;
			}
			continue;
		}
		gpt_uuid_create(type, ent->ent_type, ent->ent_name,
		    sizeof(ent->ent_name));
		ent->ent_lba_start = htole64((uint64_t)start);
		ent->ent_lba_end = htole64((uint64_t)(start + size - 1LL));
		ent++;
	}

	if (gpt_write_primary(gpt) == -1)
		return -1;

	if (gpt_write_backup(gpt) == -1)
		return -1;

	/*
	 * Turn the MBR into a Protective MBR.
	 */
	memset(mbr->mbr_part, 0, sizeof(mbr->mbr_part));
	gpt_create_pmbr_part(mbr->mbr_part, last, active);
	if (gpt_write(gpt, map) == -1) {
		gpt_warn(gpt, "Cant write PMBR");
		return -1;
	}
	return 0;
}
Example #6
0
static struct gpt_ent *
migrate_disklabel(gpt_t gpt, off_t start, struct gpt_ent *ent,
    gpt_type_t (*convert)(gpt_t, u_int, u_int))
{
	char *buf;
	struct disklabel *dl;
	off_t ofs, rawofs;
	unsigned int i;
	gpt_type_t type;

	buf = gpt_read(gpt, start + LABELSECTOR, 1);
	if (buf == NULL) {
		gpt_warn(gpt, "Error reading label");
		return NULL;
	}
	dl = (void*)(buf + LABELOFFSET);

	if (le32toh(dl->d_magic) != DISKMAGIC ||
	    le32toh(dl->d_magic2) != DISKMAGIC) {
		gpt_warnx(gpt, "MBR partition without disklabel");
		free(buf);
		return ent;
	}

	rawofs = le32toh(dl->d_partitions[RAW_PART].p_offset) *
	    le32toh(dl->d_secsize);
	for (i = 0; i < le16toh(dl->d_npartitions); i++) {
		if (dl->d_partitions[i].p_fstype == FS_UNUSED)
			continue;
		ofs = le32toh(dl->d_partitions[i].p_offset) *
		    le32toh(dl->d_secsize);
		if (ofs < rawofs)
			rawofs = 0;
	}

	if (gpt->verbose > 1)
		gpt_msg(gpt, "rawofs=%ju", (uintmax_t)rawofs);
	rawofs /= gpt->secsz;

	for (i = 0; i < le16toh(dl->d_npartitions); i++) {
		if (gpt->verbose > 1)
			gpt_msg(gpt, "Disklabel partition %u type %s", i,
			    fstypename(dl->d_partitions[i].p_fstype));

		type = (*convert)(gpt, i, dl->d_partitions[i].p_fstype);
		if (type == GPT_TYPE_INVALID)
			continue;

		gpt_uuid_create(type, ent->ent_type,
		    ent->ent_name, sizeof(ent->ent_name));

		ofs = (le32toh(dl->d_partitions[i].p_offset) *
		    le32toh(dl->d_secsize)) / gpt->secsz;
		ofs = (ofs > 0) ? ofs - rawofs : 0;
		ent->ent_lba_start = htole64((uint64_t)ofs);
		ent->ent_lba_end = htole64((uint64_t)(ofs +
		    (off_t)le32toh((uint64_t)dl->d_partitions[i].p_size)
		    - 1LL));
		ent++;
	}

	free(buf);
	return ent;
}
Example #7
0
static int
recover(gpt_t gpt, int recoverable)
{
	off_t last = gpt_last(gpt);
	map_t map;
	struct mbr *mbr;

	if (map_find(gpt, MAP_TYPE_MBR) != NULL) {
		gpt_warnx(gpt, "Device contains an MBR");
		return -1;
	}

	gpt->gpt = map_find(gpt, MAP_TYPE_PRI_GPT_HDR);
	gpt->tpg = map_find(gpt, MAP_TYPE_SEC_GPT_HDR);
	gpt->tbl = map_find(gpt, MAP_TYPE_PRI_GPT_TBL);
	gpt->lbt = map_find(gpt, MAP_TYPE_SEC_GPT_TBL);

	if (gpt->gpt == NULL && gpt->tpg == NULL) {
		gpt_warnx(gpt, "No primary or secondary GPT headers, "
		    "can't recover");
		return -1;
	}
	if (gpt->tbl == NULL && gpt->lbt == NULL) {
		gpt_warnx(gpt, "No primary or secondary GPT tables, "
		    "can't recover");
		return -1;
	}

	if (gpt->gpt != NULL &&
	    ((struct gpt_hdr *)(gpt->gpt->map_data))->hdr_lba_alt !=
	    (uint64_t)last) {
		gpt_warnx(gpt, "Media size has changed, please use "
		   "'%s resizedisk'", getprogname());
		return -1;
	}

	if (gpt->tbl != NULL && gpt->lbt == NULL) {
		if (recover_gpt_tbl(gpt, MAP_TYPE_SEC_GPT_TBL,
		    last - gpt->tbl->map_size) == -1)
			return -1;
	} else if (gpt->tbl == NULL && gpt->lbt != NULL) {
		if (recover_gpt_tbl(gpt, MAP_TYPE_PRI_GPT_TBL, 2LL) == -1)
			return -1;
	}

	if (gpt->gpt != NULL && gpt->tpg == NULL) {
		if (recover_gpt_hdr(gpt, MAP_TYPE_SEC_GPT_HDR, last) == -1)
			return -1;
	} else if (gpt->gpt == NULL && gpt->tpg != NULL) {
		if (recover_gpt_hdr(gpt, MAP_TYPE_PRI_GPT_HDR, 1LL) == -1)
			return -1;
	}

	/*
	 * Create PMBR if it doesn't already exist.
	 */
	if (map_find(gpt, MAP_TYPE_PMBR) == NULL) {
		if (map_free(gpt, 0LL, 1LL) == 0) {
			gpt_warnx(gpt, "No room for the PMBR");
			return -1;
		}
		mbr = gpt_read(gpt, 0LL, 1);
		if (mbr == NULL) {
			gpt_warnx(gpt, "Error reading MBR");
			return -1;
		}
		memset(mbr, 0, sizeof(*mbr));
		mbr->mbr_sig = htole16(MBR_SIG);
		gpt_create_pmbr_part(mbr->mbr_part, last, 0);

		map = map_add(gpt, 0LL, 1LL, MAP_TYPE_PMBR, mbr, 1);
		if (gpt_write(gpt, map) == -1) {
			gpt_warn(gpt, "Can't write PMBR");
			return -1;
		}
		gpt_msg(gpt,
		    "Recreated PMBR (you may need to rerun 'gpt biosboot'");
	}
	return 0;
}