15819a8c0Schristos /*-
25819a8c0Schristos * Copyright (c) 2002 Marcel Moolenaar
35819a8c0Schristos * All rights reserved.
45819a8c0Schristos *
55819a8c0Schristos * Redistribution and use in source and binary forms, with or without
65819a8c0Schristos * modification, are permitted provided that the following conditions
75819a8c0Schristos * are met:
85819a8c0Schristos *
95819a8c0Schristos * 1. Redistributions of source code must retain the above copyright
105819a8c0Schristos * notice, this list of conditions and the following disclaimer.
115819a8c0Schristos * 2. Redistributions in binary form must reproduce the above copyright
125819a8c0Schristos * notice, this list of conditions and the following disclaimer in the
135819a8c0Schristos * documentation and/or other materials provided with the distribution.
145819a8c0Schristos *
155819a8c0Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
165819a8c0Schristos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
175819a8c0Schristos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
185819a8c0Schristos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
195819a8c0Schristos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
205819a8c0Schristos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
215819a8c0Schristos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
225819a8c0Schristos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
235819a8c0Schristos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
245819a8c0Schristos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
255819a8c0Schristos */
265819a8c0Schristos
27a50708a1Schristos #if HAVE_NBTOOL_CONFIG_H
28a50708a1Schristos #include "nbtool_config.h"
29a50708a1Schristos #endif
30a50708a1Schristos
315819a8c0Schristos #include <sys/cdefs.h>
329b522365Schristos #ifdef __FBSDID
335819a8c0Schristos __FBSDID("$FreeBSD: src/sbin/gpt/create.c,v 1.11 2005/08/31 01:47:19 marcel Exp $");
349b522365Schristos #endif
359b522365Schristos #ifdef __RCSID
36*e1a93a1bSchristos __RCSID("$NetBSD: create.c,v 1.23 2016/09/23 19:36:50 christos Exp $");
379b522365Schristos #endif
385819a8c0Schristos
395819a8c0Schristos #include <sys/types.h>
400b43d398Schristos #include <sys/param.h>
410b43d398Schristos #include <sys/stat.h>
4216550bc8Sjakllsch #include <sys/bootblock.h>
435819a8c0Schristos
445819a8c0Schristos #include <err.h>
455819a8c0Schristos #include <stddef.h>
465819a8c0Schristos #include <stdio.h>
475819a8c0Schristos #include <stdlib.h>
485819a8c0Schristos #include <string.h>
495819a8c0Schristos #include <unistd.h>
505819a8c0Schristos
515819a8c0Schristos #include "map.h"
525819a8c0Schristos #include "gpt.h"
530b43d398Schristos #include "gpt_private.h"
545819a8c0Schristos
558ca93e46Schristos static int cmd_create(gpt_t, int, char *[]);
565819a8c0Schristos
578ca93e46Schristos static const char *createhelp[] = {
58b7b34325Schristos "[-AfP] [-p partitions]",
598ca93e46Schristos };
600fac2edbSriz
618ca93e46Schristos struct gpt_cmd c_create = {
628ca93e46Schristos "create",
638ca93e46Schristos cmd_create,
648ca93e46Schristos createhelp, __arraycount(createhelp),
658ca93e46Schristos 0,
668ca93e46Schristos };
675819a8c0Schristos
688ca93e46Schristos #define usage() gpt_usage(NULL, &c_create)
698ca93e46Schristos
705819a8c0Schristos
710b43d398Schristos static int
create(gpt_t gpt,u_int parts,int force,int primary_only,int active)723017a7a3Schristos create(gpt_t gpt, u_int parts, int force, int primary_only, int active)
735819a8c0Schristos {
74bbb4a8abSchristos off_t last = gpt_last(gpt);
750b43d398Schristos map_t map;
765819a8c0Schristos struct mbr *mbr;
775819a8c0Schristos
780b43d398Schristos map = map_find(gpt, MAP_TYPE_MBR);
795819a8c0Schristos if (map != NULL) {
805819a8c0Schristos if (!force) {
810b43d398Schristos gpt_warnx(gpt, "Device contains a MBR");
820b43d398Schristos return -1;
835819a8c0Schristos }
845819a8c0Schristos /* Nuke the MBR in our internal map. */
855819a8c0Schristos map->map_type = MAP_TYPE_UNUSED;
865819a8c0Schristos }
875819a8c0Schristos
885819a8c0Schristos /*
895819a8c0Schristos * Create PMBR.
905819a8c0Schristos */
910b43d398Schristos if (map_find(gpt, MAP_TYPE_PMBR) == NULL) {
920b43d398Schristos if (map_free(gpt, 0LL, 1LL) == 0) {
930b43d398Schristos gpt_warnx(gpt, "No room for the PMBR");
940b43d398Schristos return -1;
955819a8c0Schristos }
960b43d398Schristos mbr = gpt_read(gpt, 0LL, 1);
970b43d398Schristos if (mbr == NULL) {
980b43d398Schristos gpt_warnx(gpt, "Error reading MBR");
990b43d398Schristos return -1;
1000b43d398Schristos }
101a50708a1Schristos memset(mbr, 0, sizeof(*mbr));
1025819a8c0Schristos mbr->mbr_sig = htole16(MBR_SIG);
1033017a7a3Schristos gpt_create_pmbr_part(mbr->mbr_part, last, active);
1040b43d398Schristos
1050f110115Schristos map = map_add(gpt, 0LL, 1LL, MAP_TYPE_PMBR, mbr, 1);
106bbb4a8abSchristos if (gpt_write(gpt, map) == -1) {
107bbb4a8abSchristos gpt_warn(gpt, "Can't write PMBR");
1080b43d398Schristos return -1;
1095819a8c0Schristos }
1105819a8c0Schristos }
1115819a8c0Schristos
112bbb4a8abSchristos if (gpt_create(gpt, last, parts, primary_only) == -1)
1130b43d398Schristos return -1;
1145819a8c0Schristos
1150b43d398Schristos if (gpt_write_primary(gpt) == -1)
1160b43d398Schristos return -1;
1175819a8c0Schristos
118bbb4a8abSchristos if (!primary_only && gpt_write_backup(gpt) == -1)
1190b43d398Schristos return -1;
120bbb4a8abSchristos
1210b43d398Schristos return 0;
1225819a8c0Schristos }
1235819a8c0Schristos
1248ca93e46Schristos static int
cmd_create(gpt_t gpt,int argc,char * argv[])1250b43d398Schristos cmd_create(gpt_t gpt, int argc, char *argv[])
1265819a8c0Schristos {
1270b43d398Schristos int ch;
1283017a7a3Schristos int active = 0;
129da0a3b4cSchristos int force = 0;
130da0a3b4cSchristos int primary_only = 0;
131*e1a93a1bSchristos u_int parts = 0;
1320b43d398Schristos
133b7b34325Schristos while ((ch = getopt(argc, argv, "AfPp:")) != -1) {
1345819a8c0Schristos switch(ch) {
135b7b34325Schristos case 'A':
1363017a7a3Schristos active = 1;
1373017a7a3Schristos break;
1385819a8c0Schristos case 'f':
1395819a8c0Schristos force = 1;
1405819a8c0Schristos break;
1410b43d398Schristos case 'P':
1425819a8c0Schristos primary_only = 1;
1435819a8c0Schristos break;
1440b43d398Schristos case 'p':
145f9db7548Schristos if (gpt_uint_get(gpt, &parts) == -1)
1465c1ccc6eSchristos return -1;
1470b43d398Schristos break;
1485819a8c0Schristos default:
1498ca93e46Schristos return usage();
1505819a8c0Schristos }
1515819a8c0Schristos }
152*e1a93a1bSchristos if (parts == 0)
153*e1a93a1bSchristos parts = 128;
1545819a8c0Schristos
1550b43d398Schristos if (argc != optind)
1568ca93e46Schristos return usage();
1575819a8c0Schristos
1583017a7a3Schristos return create(gpt, parts, force, primary_only, active);
1595819a8c0Schristos }
160