recover.c revision 1.9 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.5 christos #if HAVE_NBTOOL_CONFIG_H
28 1.5 christos #include "nbtool_config.h"
29 1.5 christos #endif
30 1.5 christos
31 1.1 christos #include <sys/cdefs.h>
32 1.2 christos #ifdef __FBSDID
33 1.1 christos __FBSDID("$FreeBSD: src/sbin/gpt/recover.c,v 1.8 2005/08/31 01:47:19 marcel Exp $");
34 1.2 christos #endif
35 1.2 christos #ifdef __RCSID
36 1.9 christos __RCSID("$NetBSD: recover.c,v 1.9 2015/12/01 16:32:19 christos Exp $");
37 1.2 christos #endif
38 1.1 christos
39 1.1 christos #include <sys/types.h>
40 1.1 christos
41 1.1 christos #include <err.h>
42 1.1 christos #include <stddef.h>
43 1.1 christos #include <stdio.h>
44 1.1 christos #include <stdlib.h>
45 1.1 christos #include <string.h>
46 1.1 christos #include <unistd.h>
47 1.1 christos
48 1.1 christos #include "map.h"
49 1.1 christos #include "gpt.h"
50 1.8 christos #include "gpt_private.h"
51 1.1 christos
52 1.1 christos static int recoverable;
53 1.1 christos
54 1.9 christos static int cmd_recover(gpt_t, int, char *[]);
55 1.3 riz
56 1.9 christos static const char *recoverhelp[] = {
57 1.9 christos "",
58 1.9 christos };
59 1.9 christos
60 1.9 christos struct gpt_cmd c_recover = {
61 1.9 christos "recover",
62 1.9 christos cmd_recover,
63 1.9 christos recoverhelp, __arraycount(recoverhelp),
64 1.9 christos 0,
65 1.9 christos };
66 1.1 christos
67 1.9 christos #define usage() gpt_usage(NULL, &c_recover)
68 1.1 christos
69 1.8 christos static int
70 1.8 christos recover(gpt_t gpt)
71 1.1 christos {
72 1.6 jnemeth uint64_t last;
73 1.1 christos struct gpt_hdr *hdr;
74 1.1 christos
75 1.8 christos if (map_find(gpt, MAP_TYPE_MBR) != NULL) {
76 1.8 christos gpt_warnx(gpt, "Device contains an MBR");
77 1.8 christos return -1;
78 1.8 christos }
79 1.8 christos
80 1.8 christos gpt->gpt = map_find(gpt, MAP_TYPE_PRI_GPT_HDR);
81 1.8 christos gpt->tpg = map_find(gpt, MAP_TYPE_SEC_GPT_HDR);
82 1.8 christos gpt->tbl = map_find(gpt, MAP_TYPE_PRI_GPT_TBL);
83 1.8 christos gpt->lbt = map_find(gpt, MAP_TYPE_SEC_GPT_TBL);
84 1.8 christos
85 1.8 christos if (gpt->gpt == NULL && gpt->tpg == NULL) {
86 1.8 christos gpt_warnx(gpt, "No primary or secondary GPT headers, "
87 1.8 christos "can't recover");
88 1.8 christos return -1;
89 1.8 christos }
90 1.8 christos if (gpt->tbl == NULL && gpt->lbt == NULL) {
91 1.8 christos gpt_warnx(gpt, "No primary or secondary GPT tables, "
92 1.8 christos "can't recover");
93 1.8 christos return -1;
94 1.8 christos }
95 1.8 christos
96 1.8 christos last = gpt->mediasz / gpt->secsz - 1LL;
97 1.8 christos
98 1.8 christos if (gpt->gpt != NULL &&
99 1.8 christos ((struct gpt_hdr *)(gpt->gpt->map_data))->hdr_lba_alt != last) {
100 1.8 christos gpt_warnx(gpt, "Media size has changed, please use "
101 1.8 christos "'gpt resizedisk'");
102 1.8 christos return -1;
103 1.8 christos }
104 1.8 christos
105 1.8 christos if (gpt->tbl != NULL && gpt->lbt == NULL) {
106 1.8 christos gpt->lbt = map_add(gpt, last - gpt->tbl->map_size,
107 1.8 christos gpt->tbl->map_size, MAP_TYPE_SEC_GPT_TBL,
108 1.8 christos gpt->tbl->map_data);
109 1.8 christos if (gpt->lbt == NULL) {
110 1.8 christos gpt_warnx(gpt, "Adding secondary GPT table failed");
111 1.8 christos return -1;
112 1.8 christos }
113 1.8 christos if (gpt_write(gpt, gpt->lbt) == -1) {
114 1.8 christos gpt_warnx(gpt, "Writing secondary GPT table failed");
115 1.8 christos return -1;
116 1.8 christos }
117 1.8 christos gpt_msg(gpt, "Recovered secondary GPT table from primary");
118 1.8 christos } else if (gpt->tbl == NULL && gpt->lbt != NULL) {
119 1.8 christos gpt->tbl = map_add(gpt, 2LL, gpt->lbt->map_size,
120 1.8 christos MAP_TYPE_PRI_GPT_TBL, gpt->lbt->map_data);
121 1.8 christos if (gpt->tbl == NULL) {
122 1.8 christos gpt_warnx(gpt, "Adding primary GPT table failed");
123 1.8 christos return -1;
124 1.8 christos }
125 1.8 christos if (gpt_write(gpt, gpt->tbl) == -1) {
126 1.8 christos gpt_warnx(gpt, "Writing primary GPT table failed");
127 1.8 christos return -1;
128 1.8 christos }
129 1.8 christos gpt_msg(gpt, "Recovered primary GPT table from secondary");
130 1.8 christos }
131 1.8 christos
132 1.8 christos if (gpt->gpt != NULL && gpt->tpg == NULL) {
133 1.8 christos gpt->tpg = map_add(gpt, last, 1LL, MAP_TYPE_SEC_GPT_HDR,
134 1.8 christos calloc(1, gpt->secsz));
135 1.8 christos if (gpt->tpg == NULL) {
136 1.8 christos gpt_warnx(gpt, "Adding secondary GPT header failed");
137 1.8 christos return -1;
138 1.8 christos }
139 1.8 christos memcpy(gpt->tpg->map_data, gpt->gpt->map_data, gpt->secsz);
140 1.8 christos hdr = gpt->tpg->map_data;
141 1.8 christos hdr->hdr_lba_self = htole64(gpt->tpg->map_start);
142 1.8 christos hdr->hdr_lba_alt = htole64(gpt->gpt->map_start);
143 1.8 christos hdr->hdr_lba_table = htole64(gpt->lbt->map_start);
144 1.1 christos hdr->hdr_crc_self = 0;
145 1.1 christos hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
146 1.8 christos if (gpt_write(gpt, gpt->tpg) == -1) {
147 1.8 christos gpt_warnx(gpt, "Writing secondary GPT header failed");
148 1.8 christos return -1;
149 1.8 christos }
150 1.8 christos gpt_msg(gpt, "Recovered secondary GPT header from primary");
151 1.8 christos } else if (gpt->gpt == NULL && gpt->tpg != NULL) {
152 1.8 christos gpt->gpt = map_add(gpt, 1LL, 1LL, MAP_TYPE_PRI_GPT_HDR,
153 1.8 christos calloc(1, gpt->secsz));
154 1.8 christos if (gpt->gpt == NULL) {
155 1.8 christos gpt_warnx(gpt, "Adding primary GPT header failed");
156 1.8 christos return -1;
157 1.8 christos }
158 1.8 christos memcpy(gpt->gpt->map_data, gpt->tpg->map_data, gpt->secsz);
159 1.8 christos hdr = gpt->gpt->map_data;
160 1.8 christos hdr->hdr_lba_self = htole64(gpt->gpt->map_start);
161 1.8 christos hdr->hdr_lba_alt = htole64(gpt->tpg->map_start);
162 1.8 christos hdr->hdr_lba_table = htole64(gpt->tbl->map_start);
163 1.1 christos hdr->hdr_crc_self = 0;
164 1.1 christos hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
165 1.8 christos if (gpt_write(gpt, gpt->gpt) == -1) {
166 1.8 christos gpt_warnx(gpt, "Writing primary GPT header failed");
167 1.8 christos return -1;
168 1.8 christos }
169 1.8 christos gpt_msg(gpt, "Recovered primary GPT header from secondary");
170 1.1 christos }
171 1.8 christos return 0;
172 1.1 christos }
173 1.1 christos
174 1.9 christos static int
175 1.8 christos cmd_recover(gpt_t gpt, int argc, char *argv[])
176 1.1 christos {
177 1.8 christos int ch;
178 1.1 christos
179 1.1 christos while ((ch = getopt(argc, argv, "r")) != -1) {
180 1.1 christos switch(ch) {
181 1.1 christos case 'r':
182 1.1 christos recoverable = 1;
183 1.1 christos break;
184 1.1 christos default:
185 1.9 christos return usage();
186 1.1 christos }
187 1.1 christos }
188 1.1 christos
189 1.8 christos if (argc != optind)
190 1.9 christos return usage();
191 1.1 christos
192 1.8 christos return recover(gpt);
193 1.1 christos }
194