1 1.1 christos /*- 2 1.1 christos * Copyright (c) 2002 Marcel Moolenaar 3 1.1 christos * All rights reserved. 4 1.1 christos * 5 1.1 christos * Redistribution and use in source and binary forms, with or without 6 1.1 christos * modification, are permitted provided that the following conditions 7 1.1 christos * are met: 8 1.1 christos * 9 1.1 christos * 1. Redistributions of source code must retain the above copyright 10 1.1 christos * notice, this list of conditions and the following disclaimer. 11 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 12 1.1 christos * notice, this list of conditions and the following disclaimer in the 13 1.1 christos * documentation and/or other materials provided with the distribution. 14 1.1 christos * 15 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 1.1 christos */ 26 1.1 christos 27 1.8 christos #if HAVE_NBTOOL_CONFIG_H 28 1.8 christos #include "nbtool_config.h" 29 1.8 christos #endif 30 1.8 christos 31 1.1 christos #include <sys/cdefs.h> 32 1.2 christos #ifdef __FBSDID 33 1.1 christos __FBSDID("$FreeBSD: src/sbin/gpt/create.c,v 1.11 2005/08/31 01:47:19 marcel Exp $"); 34 1.2 christos #endif 35 1.2 christos #ifdef __RCSID 36 1.24 christos __RCSID("$NetBSD: create.c,v 1.24 2025/02/23 20:47:19 christos Exp $"); 37 1.2 christos #endif 38 1.1 christos 39 1.1 christos #include <sys/types.h> 40 1.14 christos #include <sys/param.h> 41 1.14 christos #include <sys/stat.h> 42 1.7 jakllsch #include <sys/bootblock.h> 43 1.1 christos 44 1.1 christos #include <err.h> 45 1.1 christos #include <stddef.h> 46 1.1 christos #include <stdio.h> 47 1.1 christos #include <stdlib.h> 48 1.1 christos #include <string.h> 49 1.1 christos #include <unistd.h> 50 1.1 christos 51 1.1 christos #include "map.h" 52 1.1 christos #include "gpt.h" 53 1.14 christos #include "gpt_private.h" 54 1.1 christos 55 1.15 christos static int cmd_create(gpt_t, int, char *[]); 56 1.1 christos 57 1.15 christos static const char *createhelp[] = { 58 1.22 christos "[-AfP] [-p partitions]", 59 1.15 christos }; 60 1.15 christos 61 1.24 christos const struct gpt_cmd c_create = { 62 1.15 christos "create", 63 1.15 christos cmd_create, 64 1.15 christos createhelp, __arraycount(createhelp), 65 1.15 christos 0, 66 1.15 christos }; 67 1.3 riz 68 1.15 christos #define usage() gpt_usage(NULL, &c_create) 69 1.1 christos 70 1.1 christos 71 1.14 christos static int 72 1.21 christos create(gpt_t gpt, u_int parts, int force, int primary_only, int active) 73 1.1 christos { 74 1.16 christos off_t last = gpt_last(gpt); 75 1.14 christos map_t map; 76 1.1 christos struct mbr *mbr; 77 1.16 christos 78 1.14 christos map = map_find(gpt, MAP_TYPE_MBR); 79 1.1 christos if (map != NULL) { 80 1.1 christos if (!force) { 81 1.14 christos gpt_warnx(gpt, "Device contains a MBR"); 82 1.14 christos return -1; 83 1.1 christos } 84 1.1 christos /* Nuke the MBR in our internal map. */ 85 1.1 christos map->map_type = MAP_TYPE_UNUSED; 86 1.1 christos } 87 1.1 christos 88 1.1 christos /* 89 1.1 christos * Create PMBR. 90 1.1 christos */ 91 1.14 christos if (map_find(gpt, MAP_TYPE_PMBR) == NULL) { 92 1.14 christos if (map_free(gpt, 0LL, 1LL) == 0) { 93 1.14 christos gpt_warnx(gpt, "No room for the PMBR"); 94 1.14 christos return -1; 95 1.14 christos } 96 1.14 christos mbr = gpt_read(gpt, 0LL, 1); 97 1.14 christos if (mbr == NULL) { 98 1.14 christos gpt_warnx(gpt, "Error reading MBR"); 99 1.14 christos return -1; 100 1.1 christos } 101 1.8 christos memset(mbr, 0, sizeof(*mbr)); 102 1.1 christos mbr->mbr_sig = htole16(MBR_SIG); 103 1.21 christos gpt_create_pmbr_part(mbr->mbr_part, last, active); 104 1.14 christos 105 1.19 christos map = map_add(gpt, 0LL, 1LL, MAP_TYPE_PMBR, mbr, 1); 106 1.16 christos if (gpt_write(gpt, map) == -1) { 107 1.16 christos gpt_warn(gpt, "Can't write PMBR"); 108 1.16 christos return -1; 109 1.16 christos } 110 1.1 christos } 111 1.1 christos 112 1.16 christos if (gpt_create(gpt, last, parts, primary_only) == -1) 113 1.14 christos return -1; 114 1.1 christos 115 1.16 christos if (gpt_write_primary(gpt) == -1) 116 1.14 christos return -1; 117 1.1 christos 118 1.16 christos if (!primary_only && gpt_write_backup(gpt) == -1) 119 1.14 christos return -1; 120 1.14 christos 121 1.14 christos return 0; 122 1.1 christos } 123 1.1 christos 124 1.15 christos static int 125 1.14 christos cmd_create(gpt_t gpt, int argc, char *argv[]) 126 1.1 christos { 127 1.14 christos int ch; 128 1.21 christos int active = 0; 129 1.17 christos int force = 0; 130 1.17 christos int primary_only = 0; 131 1.23 christos u_int parts = 0; 132 1.14 christos 133 1.22 christos while ((ch = getopt(argc, argv, "AfPp:")) != -1) { 134 1.1 christos switch(ch) { 135 1.22 christos case 'A': 136 1.21 christos active = 1; 137 1.21 christos break; 138 1.1 christos case 'f': 139 1.1 christos force = 1; 140 1.1 christos break; 141 1.14 christos case 'P': 142 1.14 christos primary_only = 1; 143 1.14 christos break; 144 1.1 christos case 'p': 145 1.20 christos if (gpt_uint_get(gpt, &parts) == -1) 146 1.18 christos return -1; 147 1.1 christos break; 148 1.1 christos default: 149 1.15 christos return usage(); 150 1.1 christos } 151 1.1 christos } 152 1.23 christos if (parts == 0) 153 1.23 christos parts = 128; 154 1.1 christos 155 1.14 christos if (argc != optind) 156 1.15 christos return usage(); 157 1.1 christos 158 1.21 christos return create(gpt, parts, force, primary_only, active); 159 1.1 christos } 160