restore.c revision 1.1 1 1.1 jnemeth /*-
2 1.1 jnemeth * Copyright (c) 2002 Marcel Moolenaar
3 1.1 jnemeth * All rights reserved.
4 1.1 jnemeth *
5 1.1 jnemeth * Redistribution and use in source and binary forms, with or without
6 1.1 jnemeth * modification, are permitted provided that the following conditions
7 1.1 jnemeth * are met:
8 1.1 jnemeth *
9 1.1 jnemeth * 1. Redistributions of source code must retain the above copyright
10 1.1 jnemeth * notice, this list of conditions and the following disclaimer.
11 1.1 jnemeth * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 jnemeth * notice, this list of conditions and the following disclaimer in the
13 1.1 jnemeth * documentation and/or other materials provided with the distribution.
14 1.1 jnemeth *
15 1.1 jnemeth * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 jnemeth * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 jnemeth * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 jnemeth * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 jnemeth * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1 jnemeth * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1 jnemeth * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1 jnemeth * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1 jnemeth * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1 jnemeth * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 jnemeth */
26 1.1 jnemeth
27 1.1 jnemeth #include <sys/cdefs.h>
28 1.1 jnemeth #ifdef __FBSDID
29 1.1 jnemeth __FBSDID("$FreeBSD: src/sbin/gpt/create.c,v 1.11 2005/08/31 01:47:19 marcel Exp $");
30 1.1 jnemeth #endif
31 1.1 jnemeth #ifdef __RCSID
32 1.1 jnemeth __RCSID("$NetBSD: restore.c,v 1.1 2014/08/10 18:27:15 jnemeth Exp $");
33 1.1 jnemeth #endif
34 1.1 jnemeth
35 1.1 jnemeth #include <sys/types.h>
36 1.1 jnemeth #include <sys/bootblock.h>
37 1.1 jnemeth
38 1.1 jnemeth #include <err.h>
39 1.1 jnemeth #include <stddef.h>
40 1.1 jnemeth #include <stdio.h>
41 1.1 jnemeth #include <stdlib.h>
42 1.1 jnemeth #include <string.h>
43 1.1 jnemeth #include <unistd.h>
44 1.1 jnemeth
45 1.1 jnemeth #include "map.h"
46 1.1 jnemeth #include "gpt.h"
47 1.1 jnemeth
48 1.1 jnemeth static int force;
49 1.1 jnemeth static int primary_only;
50 1.1 jnemeth
51 1.1 jnemeth const char restoremsg[] = "restore [-F] device ...";
52 1.1 jnemeth
53 1.1 jnemeth __dead static void
54 1.1 jnemeth usage_restore(void)
55 1.1 jnemeth {
56 1.1 jnemeth
57 1.1 jnemeth fprintf(stderr,
58 1.1 jnemeth "usage: %s %s\n", getprogname(), restoremsg);
59 1.1 jnemeth exit(1);
60 1.1 jnemeth }
61 1.1 jnemeth
62 1.1 jnemeth static void
63 1.1 jnemeth restore(int fd)
64 1.1 jnemeth {
65 1.1 jnemeth uuid_t uuid;
66 1.1 jnemeth off_t blocks, last;
67 1.1 jnemeth map_t *gpt, *tpg;
68 1.1 jnemeth map_t *tbl, *lbt;
69 1.1 jnemeth map_t *map;
70 1.1 jnemeth struct mbr *mbr;
71 1.1 jnemeth struct gpt_hdr *hdr;
72 1.1 jnemeth struct gpt_ent *ent;
73 1.1 jnemeth unsigned int i;
74 1.1 jnemeth
75 1.1 jnemeth last = mediasz / secsz - 1LL;
76 1.1 jnemeth
77 1.1 jnemeth if (map_find(MAP_TYPE_PRI_GPT_HDR) != NULL ||
78 1.1 jnemeth map_find(MAP_TYPE_SEC_GPT_HDR) != NULL) {
79 1.1 jnemeth warnx("%s: error: device already contains a GPT", device_name);
80 1.1 jnemeth return;
81 1.1 jnemeth }
82 1.1 jnemeth map = map_find(MAP_TYPE_MBR);
83 1.1 jnemeth if (map != NULL) {
84 1.1 jnemeth if (!force) {
85 1.1 jnemeth warnx("%s: error: device contains a MBR", device_name);
86 1.1 jnemeth return;
87 1.1 jnemeth }
88 1.1 jnemeth
89 1.1 jnemeth /* Nuke the MBR in our internal map. */
90 1.1 jnemeth map->map_type = MAP_TYPE_UNUSED;
91 1.1 jnemeth }
92 1.1 jnemeth
93 1.1 jnemeth /*
94 1.1 jnemeth * Create PMBR.
95 1.1 jnemeth */
96 1.1 jnemeth if (map_find(MAP_TYPE_PMBR) == NULL) {
97 1.1 jnemeth if (map_free(0LL, 1LL) == 0) {
98 1.1 jnemeth warnx("%s: error: no room for the PMBR", device_name);
99 1.1 jnemeth return;
100 1.1 jnemeth }
101 1.1 jnemeth mbr = gpt_read(fd, 0LL, 1);
102 1.1 jnemeth bzero(mbr, sizeof(*mbr));
103 1.1 jnemeth mbr->mbr_sig = htole16(MBR_SIG);
104 1.1 jnemeth mbr->mbr_part[0].part_shd = 0x00;
105 1.1 jnemeth mbr->mbr_part[0].part_ssect = 0x02;
106 1.1 jnemeth mbr->mbr_part[0].part_scyl = 0x00;
107 1.1 jnemeth mbr->mbr_part[0].part_typ = MBR_PTYPE_PMBR;
108 1.1 jnemeth mbr->mbr_part[0].part_ehd = 0xfe;
109 1.1 jnemeth mbr->mbr_part[0].part_esect = 0xff;
110 1.1 jnemeth mbr->mbr_part[0].part_ecyl = 0xff;
111 1.1 jnemeth mbr->mbr_part[0].part_start_lo = htole16(1);
112 1.1 jnemeth if (last > 0xffffffff) {
113 1.1 jnemeth mbr->mbr_part[0].part_size_lo = htole16(0xffff);
114 1.1 jnemeth mbr->mbr_part[0].part_size_hi = htole16(0xffff);
115 1.1 jnemeth } else {
116 1.1 jnemeth mbr->mbr_part[0].part_size_lo = htole16(last);
117 1.1 jnemeth mbr->mbr_part[0].part_size_hi = htole16(last >> 16);
118 1.1 jnemeth }
119 1.1 jnemeth map = map_add(0LL, 1LL, MAP_TYPE_PMBR, mbr);
120 1.1 jnemeth gpt_write(fd, map);
121 1.1 jnemeth }
122 1.1 jnemeth
123 1.1 jnemeth /* Get the amount of free space after the MBR */
124 1.1 jnemeth blocks = map_free(1LL, 0LL);
125 1.1 jnemeth if (blocks == 0LL) {
126 1.1 jnemeth warnx("%s: error: no room for the GPT header", device_name);
127 1.1 jnemeth return;
128 1.1 jnemeth }
129 1.1 jnemeth
130 1.1 jnemeth /* Don't create more than parts entries. */
131 1.1 jnemeth if ((uint64_t)(blocks - 1) * secsz > parts * sizeof(struct gpt_ent)) {
132 1.1 jnemeth blocks = (parts * sizeof(struct gpt_ent)) / secsz;
133 1.1 jnemeth if ((parts * sizeof(struct gpt_ent)) % secsz)
134 1.1 jnemeth blocks++;
135 1.1 jnemeth blocks++; /* Don't forget the header itself */
136 1.1 jnemeth }
137 1.1 jnemeth
138 1.1 jnemeth /* Never cross the median of the device. */
139 1.1 jnemeth if ((blocks + 1LL) > ((last + 1LL) >> 1))
140 1.1 jnemeth blocks = ((last + 1LL) >> 1) - 1LL;
141 1.1 jnemeth
142 1.1 jnemeth /*
143 1.1 jnemeth * Get the amount of free space at the end of the device and
144 1.1 jnemeth * calculate the size for the GPT structures.
145 1.1 jnemeth */
146 1.1 jnemeth map = map_last();
147 1.1 jnemeth if (map->map_type != MAP_TYPE_UNUSED) {
148 1.1 jnemeth warnx("%s: error: no room for the backup header", device_name);
149 1.1 jnemeth return;
150 1.1 jnemeth }
151 1.1 jnemeth
152 1.1 jnemeth if (map->map_size < blocks)
153 1.1 jnemeth blocks = map->map_size;
154 1.1 jnemeth if (blocks == 1LL) {
155 1.1 jnemeth warnx("%s: error: no room for the GPT table", device_name);
156 1.1 jnemeth return;
157 1.1 jnemeth }
158 1.1 jnemeth
159 1.1 jnemeth blocks--; /* Number of blocks in the GPT table. */
160 1.1 jnemeth gpt = map_add(1LL, 1LL, MAP_TYPE_PRI_GPT_HDR, calloc(1, secsz));
161 1.1 jnemeth tbl = map_add(2LL, blocks, MAP_TYPE_PRI_GPT_TBL,
162 1.1 jnemeth calloc(blocks, secsz));
163 1.1 jnemeth if (gpt == NULL || tbl == NULL)
164 1.1 jnemeth return;
165 1.1 jnemeth
166 1.1 jnemeth hdr = gpt->map_data;
167 1.1 jnemeth memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig));
168 1.1 jnemeth hdr->hdr_revision = htole32(GPT_HDR_REVISION);
169 1.1 jnemeth hdr->hdr_size = htole32(GPT_SIZE);
170 1.1 jnemeth hdr->hdr_lba_self = htole64(gpt->map_start);
171 1.1 jnemeth hdr->hdr_lba_alt = htole64(last);
172 1.1 jnemeth hdr->hdr_lba_start = htole64(tbl->map_start + blocks);
173 1.1 jnemeth hdr->hdr_lba_end = htole64(last - blocks - 1LL);
174 1.1 jnemeth uuid_create(&uuid, NULL);
175 1.1 jnemeth le_uuid_enc(hdr->hdr_uuid, &uuid);
176 1.1 jnemeth hdr->hdr_lba_table = htole64(tbl->map_start);
177 1.1 jnemeth hdr->hdr_entries = htole32((blocks * secsz) / sizeof(struct gpt_ent));
178 1.1 jnemeth if (le32toh(hdr->hdr_entries) > parts)
179 1.1 jnemeth hdr->hdr_entries = htole32(parts);
180 1.1 jnemeth hdr->hdr_entsz = htole32(sizeof(struct gpt_ent));
181 1.1 jnemeth
182 1.1 jnemeth ent = tbl->map_data;
183 1.1 jnemeth for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
184 1.1 jnemeth uuid_create(&uuid, NULL);
185 1.1 jnemeth le_uuid_enc(ent[i].ent_uuid, &uuid);
186 1.1 jnemeth }
187 1.1 jnemeth
188 1.1 jnemeth hdr->hdr_crc_table = htole32(crc32(ent, le32toh(hdr->hdr_entries) *
189 1.1 jnemeth le32toh(hdr->hdr_entsz)));
190 1.1 jnemeth hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
191 1.1 jnemeth
192 1.1 jnemeth gpt_write(fd, gpt);
193 1.1 jnemeth gpt_write(fd, tbl);
194 1.1 jnemeth
195 1.1 jnemeth /*
196 1.1 jnemeth * Create backup GPT if the user didn't suppress it.
197 1.1 jnemeth */
198 1.1 jnemeth if (!primary_only) {
199 1.1 jnemeth tpg = map_add(last, 1LL, MAP_TYPE_SEC_GPT_HDR,
200 1.1 jnemeth calloc(1, secsz));
201 1.1 jnemeth lbt = map_add(last - blocks, blocks, MAP_TYPE_SEC_GPT_TBL,
202 1.1 jnemeth tbl->map_data);
203 1.1 jnemeth memcpy(tpg->map_data, gpt->map_data, secsz);
204 1.1 jnemeth hdr = tpg->map_data;
205 1.1 jnemeth hdr->hdr_lba_self = htole64(tpg->map_start);
206 1.1 jnemeth hdr->hdr_lba_alt = htole64(gpt->map_start);
207 1.1 jnemeth hdr->hdr_lba_table = htole64(lbt->map_start);
208 1.1 jnemeth hdr->hdr_crc_self = 0; /* Don't ever forget this! */
209 1.1 jnemeth hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
210 1.1 jnemeth gpt_write(fd, lbt);
211 1.1 jnemeth gpt_write(fd, tpg);
212 1.1 jnemeth }
213 1.1 jnemeth }
214 1.1 jnemeth
215 1.1 jnemeth int
216 1.1 jnemeth cmd_restore(int argc, char *argv[])
217 1.1 jnemeth {
218 1.1 jnemeth int ch, fd;
219 1.1 jnemeth
220 1.1 jnemeth while ((ch = getopt(argc, argv, "F")) != -1) {
221 1.1 jnemeth switch(ch) {
222 1.1 jnemeth case 'F':
223 1.1 jnemeth force = 1;
224 1.1 jnemeth break;
225 1.1 jnemeth default:
226 1.1 jnemeth usage_restore();
227 1.1 jnemeth }
228 1.1 jnemeth }
229 1.1 jnemeth
230 1.1 jnemeth if (argc == optind)
231 1.1 jnemeth usage_restore();
232 1.1 jnemeth
233 1.1 jnemeth while (optind < argc) {
234 1.1 jnemeth fd = gpt_open(argv[optind++]);
235 1.1 jnemeth if (fd == -1) {
236 1.1 jnemeth warn("unable to open device '%s'", device_name);
237 1.1 jnemeth continue;
238 1.1 jnemeth }
239 1.1 jnemeth
240 1.1 jnemeth restore(fd);
241 1.1 jnemeth
242 1.1 jnemeth gpt_close(fd);
243 1.1 jnemeth }
244 1.1 jnemeth
245 1.1 jnemeth return (0);
246 1.1 jnemeth }
247