recover.c revision 1.8 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.8 christos __RCSID("$NetBSD: recover.c,v 1.8 2015/12/01 09:05:33 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.8 christos const char recovermsg[] = "recover";
55 1.3 riz
56 1.8 christos static int
57 1.1 christos usage_recover(void)
58 1.1 christos {
59 1.1 christos
60 1.1 christos fprintf(stderr,
61 1.3 riz "usage: %s %s\n", getprogname(), recovermsg);
62 1.8 christos return -1;
63 1.1 christos }
64 1.1 christos
65 1.8 christos static int
66 1.8 christos recover(gpt_t gpt)
67 1.1 christos {
68 1.6 jnemeth uint64_t last;
69 1.1 christos struct gpt_hdr *hdr;
70 1.1 christos
71 1.8 christos if (map_find(gpt, MAP_TYPE_MBR) != NULL) {
72 1.8 christos gpt_warnx(gpt, "Device contains an MBR");
73 1.8 christos return -1;
74 1.8 christos }
75 1.8 christos
76 1.8 christos gpt->gpt = map_find(gpt, MAP_TYPE_PRI_GPT_HDR);
77 1.8 christos gpt->tpg = map_find(gpt, MAP_TYPE_SEC_GPT_HDR);
78 1.8 christos gpt->tbl = map_find(gpt, MAP_TYPE_PRI_GPT_TBL);
79 1.8 christos gpt->lbt = map_find(gpt, MAP_TYPE_SEC_GPT_TBL);
80 1.8 christos
81 1.8 christos if (gpt->gpt == NULL && gpt->tpg == NULL) {
82 1.8 christos gpt_warnx(gpt, "No primary or secondary GPT headers, "
83 1.8 christos "can't recover");
84 1.8 christos return -1;
85 1.8 christos }
86 1.8 christos if (gpt->tbl == NULL && gpt->lbt == NULL) {
87 1.8 christos gpt_warnx(gpt, "No primary or secondary GPT tables, "
88 1.8 christos "can't recover");
89 1.8 christos return -1;
90 1.8 christos }
91 1.8 christos
92 1.8 christos last = gpt->mediasz / gpt->secsz - 1LL;
93 1.8 christos
94 1.8 christos if (gpt->gpt != NULL &&
95 1.8 christos ((struct gpt_hdr *)(gpt->gpt->map_data))->hdr_lba_alt != last) {
96 1.8 christos gpt_warnx(gpt, "Media size has changed, please use "
97 1.8 christos "'gpt resizedisk'");
98 1.8 christos return -1;
99 1.8 christos }
100 1.8 christos
101 1.8 christos if (gpt->tbl != NULL && gpt->lbt == NULL) {
102 1.8 christos gpt->lbt = map_add(gpt, last - gpt->tbl->map_size,
103 1.8 christos gpt->tbl->map_size, MAP_TYPE_SEC_GPT_TBL,
104 1.8 christos gpt->tbl->map_data);
105 1.8 christos if (gpt->lbt == NULL) {
106 1.8 christos gpt_warnx(gpt, "Adding secondary GPT table failed");
107 1.8 christos return -1;
108 1.8 christos }
109 1.8 christos if (gpt_write(gpt, gpt->lbt) == -1) {
110 1.8 christos gpt_warnx(gpt, "Writing secondary GPT table failed");
111 1.8 christos return -1;
112 1.8 christos }
113 1.8 christos gpt_msg(gpt, "Recovered secondary GPT table from primary");
114 1.8 christos } else if (gpt->tbl == NULL && gpt->lbt != NULL) {
115 1.8 christos gpt->tbl = map_add(gpt, 2LL, gpt->lbt->map_size,
116 1.8 christos MAP_TYPE_PRI_GPT_TBL, gpt->lbt->map_data);
117 1.8 christos if (gpt->tbl == NULL) {
118 1.8 christos gpt_warnx(gpt, "Adding primary GPT table failed");
119 1.8 christos return -1;
120 1.8 christos }
121 1.8 christos if (gpt_write(gpt, gpt->tbl) == -1) {
122 1.8 christos gpt_warnx(gpt, "Writing primary GPT table failed");
123 1.8 christos return -1;
124 1.8 christos }
125 1.8 christos gpt_msg(gpt, "Recovered primary GPT table from secondary");
126 1.8 christos }
127 1.8 christos
128 1.8 christos if (gpt->gpt != NULL && gpt->tpg == NULL) {
129 1.8 christos gpt->tpg = map_add(gpt, last, 1LL, MAP_TYPE_SEC_GPT_HDR,
130 1.8 christos calloc(1, gpt->secsz));
131 1.8 christos if (gpt->tpg == NULL) {
132 1.8 christos gpt_warnx(gpt, "Adding secondary GPT header failed");
133 1.8 christos return -1;
134 1.8 christos }
135 1.8 christos memcpy(gpt->tpg->map_data, gpt->gpt->map_data, gpt->secsz);
136 1.8 christos hdr = gpt->tpg->map_data;
137 1.8 christos hdr->hdr_lba_self = htole64(gpt->tpg->map_start);
138 1.8 christos hdr->hdr_lba_alt = htole64(gpt->gpt->map_start);
139 1.8 christos hdr->hdr_lba_table = htole64(gpt->lbt->map_start);
140 1.1 christos hdr->hdr_crc_self = 0;
141 1.1 christos hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
142 1.8 christos if (gpt_write(gpt, gpt->tpg) == -1) {
143 1.8 christos gpt_warnx(gpt, "Writing secondary GPT header failed");
144 1.8 christos return -1;
145 1.8 christos }
146 1.8 christos gpt_msg(gpt, "Recovered secondary GPT header from primary");
147 1.8 christos } else if (gpt->gpt == NULL && gpt->tpg != NULL) {
148 1.8 christos gpt->gpt = map_add(gpt, 1LL, 1LL, MAP_TYPE_PRI_GPT_HDR,
149 1.8 christos calloc(1, gpt->secsz));
150 1.8 christos if (gpt->gpt == NULL) {
151 1.8 christos gpt_warnx(gpt, "Adding primary GPT header failed");
152 1.8 christos return -1;
153 1.8 christos }
154 1.8 christos memcpy(gpt->gpt->map_data, gpt->tpg->map_data, gpt->secsz);
155 1.8 christos hdr = gpt->gpt->map_data;
156 1.8 christos hdr->hdr_lba_self = htole64(gpt->gpt->map_start);
157 1.8 christos hdr->hdr_lba_alt = htole64(gpt->tpg->map_start);
158 1.8 christos hdr->hdr_lba_table = htole64(gpt->tbl->map_start);
159 1.1 christos hdr->hdr_crc_self = 0;
160 1.1 christos hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
161 1.8 christos if (gpt_write(gpt, gpt->gpt) == -1) {
162 1.8 christos gpt_warnx(gpt, "Writing primary GPT header failed");
163 1.8 christos return -1;
164 1.8 christos }
165 1.8 christos gpt_msg(gpt, "Recovered primary GPT header from secondary");
166 1.1 christos }
167 1.8 christos return 0;
168 1.1 christos }
169 1.1 christos
170 1.1 christos int
171 1.8 christos cmd_recover(gpt_t gpt, int argc, char *argv[])
172 1.1 christos {
173 1.8 christos int ch;
174 1.1 christos
175 1.1 christos while ((ch = getopt(argc, argv, "r")) != -1) {
176 1.1 christos switch(ch) {
177 1.1 christos case 'r':
178 1.1 christos recoverable = 1;
179 1.1 christos break;
180 1.1 christos default:
181 1.8 christos return usage_recover();
182 1.1 christos }
183 1.1 christos }
184 1.1 christos
185 1.8 christos if (argc != optind)
186 1.1 christos usage_recover();
187 1.1 christos
188 1.8 christos return recover(gpt);
189 1.1 christos }
190