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/destroy.c,v 1.6 2005/08/31 01:47:19 marcel Exp $"); 34#endif 35#ifdef __RCSID 36__RCSID("$NetBSD: destroy.c,v 1.14 2022/11/20 11:57:02 mlelstv Exp $"); 37#endif 38 39#include <sys/types.h> 40 41#include <err.h> 42#include <stddef.h> 43#include <stdio.h> 44#include <stdlib.h> 45#include <string.h> 46#include <unistd.h> 47 48#include "map.h" 49#include "gpt.h" 50#include "gpt_private.h" 51 52static int cmd_destroy(gpt_t, int, char *[]); 53 54static const char *destroyhelp[] = { 55 "[-r]", 56}; 57 58struct gpt_cmd c_destroy = { 59 "destroy", 60 cmd_destroy, 61 destroyhelp, __arraycount(destroyhelp), 62 GPT_OPTGPT | GPT_SYNC, 63}; 64 65#define usage() gpt_usage(NULL, &c_destroy) 66 67 68static int 69destroy(gpt_t gpt, int force, int recoverable) 70{ 71 map_t pri_hdr, sec_hdr, pmbr; 72 73 pri_hdr = map_find(gpt, MAP_TYPE_PRI_GPT_HDR); 74 sec_hdr = map_find(gpt, MAP_TYPE_SEC_GPT_HDR); 75 pmbr = map_find(gpt, MAP_TYPE_PMBR); 76 77 if (pri_hdr == NULL && sec_hdr == NULL) { 78 gpt_warnx(gpt, "Device doesn't contain a GPT"); 79 return -1; 80 } 81 82 if (recoverable && sec_hdr == NULL) { 83 gpt_warnx(gpt, "Recoverability not possible"); 84 return -1; 85 } 86 87 if (pri_hdr != NULL) { 88 memset(pri_hdr->map_data, 0, gpt->secsz); 89 if (gpt_write(gpt, pri_hdr) == -1) { 90 gpt_warnx(gpt, "Error writing primary header"); 91 return -1; 92 } 93 } 94 95 if (!recoverable && sec_hdr != NULL) { 96 memset(sec_hdr->map_data, 0, gpt->secsz); 97 if (gpt_write(gpt, sec_hdr) == -1) { 98 gpt_warnx(gpt, "Error writing backup header"); 99 return -1; 100 } 101 } 102 103 if (!recoverable && pmbr != NULL) { 104 memset(pmbr->map_data, 0, gpt->secsz); 105 if (gpt_write(gpt, pmbr) == -1) { 106 gpt_warnx(gpt, "Error deleting PMBR"); 107 return -1; 108 } 109 } 110 111 return 0; 112} 113 114static int 115cmd_destroy(gpt_t gpt, int argc, char *argv[]) 116{ 117 int ch; 118 int recoverable = 0; 119 120 while ((ch = getopt(argc, argv, "fr")) != -1) { 121 switch(ch) { 122 case 'f': 123 break; 124 case 'r': 125 recoverable = 1; 126 break; 127 default: 128 return usage(); 129 } 130 } 131 132 if (argc != optind) 133 return usage(); 134 135 return destroy(gpt, 0, recoverable); 136} 137