static gpt_type_t netbsd_fstype_to_gpt_type(gpt_t gpt, u_int i, u_int fstype) { switch (fstype) { case FS_UNUSED: return GPT_TYPE_INVALID; case FS_HFS: return GPT_TYPE_APPLE_HFS; case FS_EX2FS: return GPT_TYPE_LINUX_DATA; case FS_SWAP: return GPT_TYPE_NETBSD_SWAP; case FS_BSDFFS: return GPT_TYPE_NETBSD_FFS; case FS_BSDLFS: return GPT_TYPE_NETBSD_LFS; case FS_RAID: return GPT_TYPE_NETBSD_RAIDFRAME; case FS_CCD: return GPT_TYPE_NETBSD_CCD; case FS_CGD: return GPT_TYPE_NETBSD_CGD; default: gpt_warnx(gpt, "Partition %u unknown type %s, " "using \"Microsoft Basic Data\"", i, fstypename(fstype)); return GPT_TYPE_MS_BASIC_DATA; } }
static struct mbr* read_boot(gpt_t gpt, const char *bootpath) { int bfd, ret = -1; struct mbr *buf; struct stat st; char *bp; buf = NULL; bfd = -1; if (bootpath == NULL) bp = strdup(DEFAULT_BOOTDIR "/" DEFAULT_BOOTCODE); else if (*bootpath == '/') bp = strdup(bootpath); else { if (asprintf(&bp, "%s/%s", DEFAULT_BOOTDIR, bootpath) < 0) bp = NULL; } if (bp == NULL) { gpt_warn(gpt, "Can't allocate memory for bootpath"); goto fail; } if ((buf = malloc((size_t)gpt->secsz)) == NULL) { gpt_warn(gpt, "Can't allocate memory for sector"); goto fail; } if ((bfd = open(bp, O_RDONLY)) < 0 || fstat(bfd, &st) == -1) { gpt_warn(gpt, "Can't open `%s'", bp); goto fail; } if (st.st_size != MBR_DSN_OFFSET) { gpt_warnx(gpt, "The bootcode in `%s' does not match the" " expected size %u", bp, MBR_DSN_OFFSET); goto fail; } if (read(bfd, buf, (size_t)st.st_size) != (ssize_t)st.st_size) { gpt_warn(gpt, "Error reading from `%s'", bp); goto fail; } ret = 0; fail: if (bfd != -1) close(bfd); if (ret == -1) { free(buf); buf = NULL; } free(bp); return buf; }
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; }
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; }
static gpt_type_t freebsd_fstype_to_gpt_type(gpt_t gpt, u_int i, u_int fstype) { switch (fstype) { case FS_UNUSED: return GPT_TYPE_INVALID; case FS_SWAP: return GPT_TYPE_FREEBSD_SWAP; case FS_BSDFFS: return GPT_TYPE_FREEBSD_UFS; case FREEBSD_FS_VINUM: return GPT_TYPE_FREEBSD_VINUM; case FREEBSD_FS_ZFS: return GPT_TYPE_FREEBSD_ZFS; default: gpt_warnx(gpt, "Unknown FreeBSD partition (%d)", fstype); return GPT_TYPE_INVALID; } }
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; }
map_t map_add(gpt_t gpt, off_t start, off_t size, int type, void *data, int alloc) { map_t m, n, p; #ifdef DEBUG printf("add: %s %#jx %#jx\n", map_type(type), (uintmax_t)start, (uintmax_t)size); for (n = gpt->mediamap; n; n = n->map_next) printf("have: %s %#jx %#jx\n", map_type(n->map_type), (uintmax_t)n->map_start, (uintmax_t)n->map_size); #endif n = gpt->mediamap; while (n != NULL && n->map_start + n->map_size <= start) n = n->map_next; if (n == NULL) { if (!(gpt->flags & GPT_QUIET)) gpt_warnx(gpt, "Can't find map"); return NULL; } if (n->map_start + n->map_size < start + size) { if (!(gpt->flags & GPT_QUIET)) gpt_warnx(gpt, "map entry doesn't fit media"); return NULL; } if (n->map_start == start && n->map_size == size) { if (n->map_type != MAP_TYPE_UNUSED) { if (n->map_type != MAP_TYPE_MBR_PART || type != MAP_TYPE_GPT_PART) { if (!(gpt->flags & GPT_QUIET)) gpt_warnx(gpt, "partition(%ju,%ju) mirrored", (uintmax_t)start, (uintmax_t)size); } } n->map_type = type; n->map_data = data; n->map_alloc = alloc; return n; } if (n->map_type != MAP_TYPE_UNUSED) { if (n->map_type != MAP_TYPE_MBR_PART || type != MAP_TYPE_GPT_PART) { gpt_warnx(gpt, "bogus map current=%s new=%s", map_type(n->map_type), map_type(type)); return NULL; } n->map_type = MAP_TYPE_UNUSED; } m = map_create(start, size, type); if (m == NULL) goto oomem; m->map_data = data; m->map_alloc = alloc; if (start == n->map_start) { m->map_prev = n->map_prev; m->map_next = n; if (m->map_prev != NULL) m->map_prev->map_next = m; else gpt->mediamap = m; n->map_prev = m; n->map_start += size; n->map_size -= size; } else if (start + size == n->map_start + n->map_size) { p = n; m->map_next = p->map_next; m->map_prev = p; if (m->map_next != NULL) m->map_next->map_prev = m; p->map_next = m; p->map_size -= size; } else { p = map_create(n->map_start, start - n->map_start, n->map_type); if (p == NULL) goto oomem; n->map_start += p->map_size + m->map_size; n->map_size -= (p->map_size + m->map_size); p->map_prev = n->map_prev; m->map_prev = p; n->map_prev = m; m->map_next = n; p->map_next = m; if (p->map_prev != NULL) p->map_prev->map_next = p; else gpt->mediamap = p; } return m; oomem: map_destroy(m); gpt_warn(gpt, "Can't create map"); return NULL; }
off_t map_resize(gpt_t gpt, map_t m, off_t size, off_t alignment) { map_t n, o; off_t alignsize, prevsize; n = m->map_next; if (size < 0 || alignment < 0) { gpt_warnx(gpt, "negative size or alignment"); return -1; } /* Size == 0 means delete, if the next map is unused */ if (size == 0) { if (n == NULL) { // XXX: we could just turn the map to UNUSED! gpt_warnx(gpt, "Can't delete, next map is not found"); return -1; } if (n->map_type != MAP_TYPE_UNUSED) { gpt_warnx(gpt, "Can't delete, next map is in use"); return -1; } if (alignment == 0) { size = m->map_size + n->map_size; m->map_size = size; m->map_next = n->map_next; if (n->map_next != NULL) n->map_next->map_prev = m; map_destroy(n); return size; } else { /* alignment > 0 */ prevsize = m->map_size; size = ((m->map_size + n->map_size) / alignment) * alignment; if (size <= prevsize) { gpt_warnx(gpt, "Can't coalesce %ju <= %ju", (uintmax_t)prevsize, (uintmax_t)size); return -1; } m->map_size = size; n->map_start += size - prevsize; n->map_size -= size - prevsize; if (n->map_size == 0) { m->map_next = n->map_next; if (n->map_next != NULL) n->map_next->map_prev = m; map_destroy(n); } return size; } } alignsize = size; if (alignment % size != 0) alignsize = (size + alignment) / alignment * alignment; if (alignsize < m->map_size) { /* shrinking */ prevsize = m->map_size; m->map_size = alignsize; if (n == NULL || n->map_type != MAP_TYPE_UNUSED) { o = map_create(m->map_start + alignsize, prevsize - alignsize, MAP_TYPE_UNUSED); if (o == NULL) { gpt_warn(gpt, "Can't create map"); return -1; } m->map_next = o; o->map_prev = m; o->map_next = n; if (n != NULL) n->map_prev = o; return alignsize; } else { n->map_start -= alignsize; n->map_size += alignsize; return alignsize; } } else if (alignsize > m->map_size) { /* expanding */ if (n == NULL) { gpt_warnx(gpt, "Can't expand map, no space after it"); return -1; } if (n->map_type != MAP_TYPE_UNUSED) { gpt_warnx(gpt, "Can't expand map, next map after it in use"); return -1; } if (n->map_size < alignsize - m->map_size) { gpt_warnx(gpt, "Can't expand map, not enough space in the" " next map after it"); return -1; } n->map_size -= alignsize - m->map_size; n->map_start += alignsize - m->map_size; if (n->map_size == 0) { m->map_next = n->map_next; if (n->map_next != NULL) n->map_next->map_prev = m; map_destroy(n); } m->map_size = alignsize; return alignsize; } else /* correct size */ return alignsize; }
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; }
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; }
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; }
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; }