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/create.c,v 1.11 2005/08/31 01:47:19 marcel Exp $"); 34#endif 35#ifdef __RCSID 36__RCSID("$NetBSD: create.c,v 1.23 2016/09/23 19:36:50 christos Exp $"); 37#endif 38 39#include <sys/types.h> 40#include <sys/param.h> 41#include <sys/stat.h> 42#include <sys/bootblock.h> 43 44#include <err.h> 45#include <stddef.h> 46#include <stdio.h> 47#include <stdlib.h> 48#include <string.h> 49#include <unistd.h> 50 51#include "map.h" 52#include "gpt.h" 53#include "gpt_private.h" 54 55static int cmd_create(gpt_t, int, char *[]); 56 57static const char *createhelp[] = { 58 "[-AfP] [-p partitions]", 59}; 60 61struct gpt_cmd c_create = { 62 "create", 63 cmd_create, 64 createhelp, __arraycount(createhelp), 65 0, 66}; 67 68#define usage() gpt_usage(NULL, &c_create) 69 70 71static int 72create(gpt_t gpt, u_int parts, int force, int primary_only, int active) 73{ 74 off_t last = gpt_last(gpt); 75 map_t map; 76 struct mbr *mbr; 77 78 map = map_find(gpt, MAP_TYPE_MBR); 79 if (map != NULL) { 80 if (!force) { 81 gpt_warnx(gpt, "Device contains a MBR"); 82 return -1; 83 } 84 /* Nuke the MBR in our internal map. */ 85 map->map_type = MAP_TYPE_UNUSED; 86 } 87 88 /* 89 * Create PMBR. 90 */ 91 if (map_find(gpt, MAP_TYPE_PMBR) == NULL) { 92 if (map_free(gpt, 0LL, 1LL) == 0) { 93 gpt_warnx(gpt, "No room for the PMBR"); 94 return -1; 95 } 96 mbr = gpt_read(gpt, 0LL, 1); 97 if (mbr == NULL) { 98 gpt_warnx(gpt, "Error reading MBR"); 99 return -1; 100 } 101 memset(mbr, 0, sizeof(*mbr)); 102 mbr->mbr_sig = htole16(MBR_SIG); 103 gpt_create_pmbr_part(mbr->mbr_part, last, active); 104 105 map = map_add(gpt, 0LL, 1LL, MAP_TYPE_PMBR, mbr, 1); 106 if (gpt_write(gpt, map) == -1) { 107 gpt_warn(gpt, "Can't write PMBR"); 108 return -1; 109 } 110 } 111 112 if (gpt_create(gpt, last, parts, primary_only) == -1) 113 return -1; 114 115 if (gpt_write_primary(gpt) == -1) 116 return -1; 117 118 if (!primary_only && gpt_write_backup(gpt) == -1) 119 return -1; 120 121 return 0; 122} 123 124static int 125cmd_create(gpt_t gpt, int argc, char *argv[]) 126{ 127 int ch; 128 int active = 0; 129 int force = 0; 130 int primary_only = 0; 131 u_int parts = 0; 132 133 while ((ch = getopt(argc, argv, "AfPp:")) != -1) { 134 switch(ch) { 135 case 'A': 136 active = 1; 137 break; 138 case 'f': 139 force = 1; 140 break; 141 case 'P': 142 primary_only = 1; 143 break; 144 case 'p': 145 if (gpt_uint_get(gpt, &parts) == -1) 146 return -1; 147 break; 148 default: 149 return usage(); 150 } 151 } 152 if (parts == 0) 153 parts = 128; 154 155 if (argc != optind) 156 return usage(); 157 158 return create(gpt, parts, force, primary_only, active); 159} 160