1 /*- 2 * Copyright (c) 2002 Marcel Moolenaar 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #if HAVE_NBTOOL_CONFIG_H 28 #include "nbtool_config.h" 29 #endif 30 31 #include <sys/cdefs.h> 32 #ifdef __FBSDID 33 __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $"); 34 #endif 35 #ifdef __RCSID 36 __RCSID("$NetBSD: resizedisk.c,v 1.23 2025/12/17 15:56:06 nia Exp $"); 37 #endif 38 39 #include <sys/bootblock.h> 40 #include <sys/types.h> 41 #if defined(HAVE_SYS_ENDIAN_H) || ! defined(HAVE_NBTOOL_CONFIG_H) 42 #include <sys/endian.h> 43 #endif 44 45 #include <err.h> 46 #include <stdbool.h> 47 #include <stddef.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <unistd.h> 52 53 #include "map.h" 54 #include "gpt.h" 55 #include "gpt_private.h" 56 57 58 static int cmd_resizedisk(gpt_t, int, char *[]); 59 60 static const char *resizediskhelp[] = { 61 "[-s size] [-q]", 62 }; 63 64 const struct gpt_cmd c_resizedisk = { 65 "resizedisk", 66 cmd_resizedisk, 67 resizediskhelp, __arraycount(resizediskhelp), 68 GPT_OPTGPT, 69 }; 70 71 #define usage() gpt_usage(NULL, &c_resizedisk) 72 73 /* 74 * relocate the secondary GPT based on the following criteria: 75 * - size not specified 76 * - disk has not changed size, do nothing 77 * - disk has grown, relocate secondary 78 * - disk has shrunk, create new secondary 79 * - size specified 80 * - size is larger then disk or same as current location, do nothing 81 * - relocate or create new secondary 82 * - when shrinking, verify that table fits 83 */ 84 static int 85 resizedisk(gpt_t gpt, off_t sector, off_t size __unused, bool quiet) 86 { 87 map_t mbrmap; 88 struct gpt_hdr *hdr; 89 struct gpt_ent *ent; 90 struct mbr *mbr; 91 off_t last, oldloc, newloc, lastdata, gpt_size; 92 int i; 93 94 last = gpt->mediasz / gpt->secsz - 1; 95 lastdata = 0; 96 newloc = 0; 97 98 if (sector > last) { 99 gpt_warnx(gpt, "specified number of sectors %jd" 100 " is larger then the disk %jd", (uintmax_t)sector, 101 (uintmax_t)last); 102 return -1; 103 } 104 105 mbrmap = map_find(gpt, MAP_TYPE_PMBR); 106 if (mbrmap == NULL || mbrmap->map_start != 0) { 107 gpt_warnx(gpt, "No valid PMBR found"); 108 return -1; 109 } 110 mbr = mbrmap->map_data; 111 112 gpt->gpt = map_find(gpt, MAP_TYPE_PRI_GPT_HDR); 113 if (gpt->gpt == NULL) { 114 gpt_warnx(gpt, "No primary GPT header; run create or recover"); 115 return -1; 116 } 117 118 gpt->tbl = map_find(gpt, MAP_TYPE_PRI_GPT_TBL); 119 if (gpt->tbl == NULL) { 120 gpt_warnx(gpt, "No primary GPT table; Run recover"); 121 return -1; 122 } 123 124 hdr = gpt->gpt->map_data; 125 oldloc = (off_t)le64toh((uint64_t)hdr->hdr_lba_alt); 126 127 gpt->tpg = map_find(gpt, MAP_TYPE_SEC_GPT_HDR); 128 gpt->lbt = map_find(gpt, MAP_TYPE_SEC_GPT_TBL); 129 130 if (gpt->tpg == NULL || gpt->lbt == NULL) 131 gpt_warnx(gpt, "No secondary GPT table"); 132 133 gpt_size = gpt->tbl->map_size; 134 if (sector == oldloc) { 135 if (!quiet) 136 gpt_warnx(gpt, "Device is already the specified size"); 137 return 0; 138 } 139 140 if (sector == 0 && last == oldloc) { 141 if (!quiet) 142 gpt_warnx(gpt, "Device hasn't changed size"); 143 if (gpt->tpg != NULL && gpt->lbt != NULL) 144 return 0; 145 } 146 147 for (ent = gpt->tbl->map_data; ent < 148 (struct gpt_ent *)((char *)gpt->tbl->map_data + 149 le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)); ent++) { 150 if (!gpt_uuid_is_nil(ent->ent_type) && 151 ((off_t)le64toh(ent->ent_lba_end) > lastdata)) { 152 lastdata = (off_t)le64toh((uint64_t)ent->ent_lba_end); 153 } 154 } 155 156 if (sector - gpt_size <= lastdata) { 157 gpt_warnx(gpt, "Not enough space at %" PRIu64 158 " for secondary GPT table", sector); 159 return -1; 160 } 161 162 if (last - gpt_size <= lastdata) { 163 gpt_warnx(gpt, "Not enough space for new secondary GPT table"); 164 return -1; 165 } 166 167 if (sector > oldloc) 168 newloc = sector; 169 if (sector > 0 && sector < oldloc && last >= oldloc) 170 newloc = sector; 171 if (sector == 0 && last > oldloc) 172 newloc = last; 173 174 if (newloc > 0 && gpt->tpg != NULL && gpt->lbt != NULL) { 175 if (!quiet) 176 gpt_msg(gpt, "Moving secondary GPT header"); 177 gpt->tpg->map_start = newloc; 178 gpt->lbt->map_start = newloc - gpt_size; 179 } else { 180 if (!quiet) 181 gpt_msg(gpt, "Creating new secondary GPT header"); 182 if (sector > 0) 183 newloc = sector; 184 else 185 newloc = last; 186 187 if (gpt_add_hdr(gpt, MAP_TYPE_SEC_GPT_HDR, newloc) == -1) 188 return -1; 189 190 gpt->lbt = map_add(gpt, newloc - gpt_size, gpt_size, 191 MAP_TYPE_SEC_GPT_TBL, gpt->tbl->map_data, 0); 192 if (gpt->lbt == NULL) { 193 gpt_warn(gpt, "Error adding secondary GPT table"); 194 return -1; 195 } 196 memcpy(gpt->tpg->map_data, gpt->gpt->map_data, gpt->secsz); 197 } 198 199 hdr = gpt->gpt->map_data; 200 hdr->hdr_lba_alt = htole64((uint64_t)gpt->tpg->map_start); 201 hdr->hdr_crc_self = 0; 202 hdr->hdr_lba_end = htole64((uint64_t)(gpt->lbt->map_start - 1)); 203 hdr->hdr_crc_self = 204 htole32(crc32(gpt->gpt->map_data, GPT_HDR_SIZE)); 205 gpt_write(gpt, gpt->gpt); 206 207 hdr = gpt->tpg->map_data; 208 hdr->hdr_lba_self = htole64((uint64_t)gpt->tpg->map_start); 209 hdr->hdr_lba_alt = htole64((uint64_t)gpt->gpt->map_start); 210 hdr->hdr_lba_end = htole64((uint64_t)(gpt->lbt->map_start - 1)); 211 hdr->hdr_lba_table = htole64((uint64_t)gpt->lbt->map_start); 212 213 if (gpt_write_backup(gpt) == -1) 214 return -1; 215 216 for (i = 0; i < 4; i++) 217 if (mbr->mbr_part[0].part_typ == MBR_PTYPE_PMBR) 218 break; 219 if (i == 4) { 220 gpt_warnx(gpt, "No valid PMBR partition found"); 221 return -1; 222 } 223 if (last > 0xffffffff) { 224 mbr->mbr_part[0].part_size_lo = htole16(0xffff); 225 mbr->mbr_part[0].part_size_hi = htole16(0xffff); 226 } else { 227 mbr->mbr_part[0].part_size_lo = htole16((uint16_t)last); 228 mbr->mbr_part[0].part_size_hi = htole16((uint16_t)((uint64_t)last >> 16)); 229 } 230 if (gpt_write(gpt, mbrmap) == -1) { 231 gpt_warnx(gpt, "Error writing PMBR"); 232 return -1; 233 } 234 235 return 0; 236 } 237 238 static int 239 cmd_resizedisk(gpt_t gpt, int argc, char *argv[]) 240 { 241 int ch; 242 off_t sector, size = gpt->mediasz; 243 bool quiet = false; 244 245 while ((ch = getopt(argc, argv, "s:q")) != -1) { 246 switch(ch) { 247 case 's': 248 if (gpt_add_ais(gpt, NULL, NULL, &size, ch) == -1) 249 return -1; 250 break; 251 case 'q': 252 quiet = true; 253 break; 254 default: 255 return usage(); 256 } 257 } 258 259 if (argc != optind) 260 return usage(); 261 262 if ((sector = gpt_check_ais(gpt, 0, (u_int)~0, size)) == -1) 263 return -1; 264 265 if (--sector == 0) { 266 gpt_warnx(gpt, "New size %ju too small", (uintmax_t)size); 267 return -1; 268 } 269 270 return resizedisk(gpt, sector, size, quiet); 271 } 272