xref: /freebsd/sys/geom/part/g_part_apm.c (revision 35a04710d7286aa9538917fd7f8e417dbee95b82)
1 /*-
2  * Copyright (c) 2006, 2007 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 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/apm.h>
32 #include <sys/bio.h>
33 #include <sys/diskmbr.h>
34 #include <sys/endian.h>
35 #include <sys/kernel.h>
36 #include <sys/kobj.h>
37 #include <sys/limits.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/queue.h>
42 #include <sys/sbuf.h>
43 #include <sys/systm.h>
44 #include <geom/geom.h>
45 #include <geom/part/g_part.h>
46 
47 #include "g_part_if.h"
48 
49 struct g_part_apm_table {
50 	struct g_part_table	base;
51 	struct apm_ddr		ddr;
52 	struct apm_ent		self;
53 };
54 
55 struct g_part_apm_entry {
56 	struct g_part_entry	base;
57 	struct apm_ent		ent;
58 };
59 
60 static int g_part_apm_add(struct g_part_table *, struct g_part_entry *,
61     struct g_part_parms *);
62 static int g_part_apm_create(struct g_part_table *, struct g_part_parms *);
63 static int g_part_apm_destroy(struct g_part_table *, struct g_part_parms *);
64 static int g_part_apm_dumpto(struct g_part_table *, struct g_part_entry *);
65 static int g_part_apm_modify(struct g_part_table *, struct g_part_entry *,
66     struct g_part_parms *);
67 static char *g_part_apm_name(struct g_part_table *, struct g_part_entry *,
68     char *, size_t);
69 static int g_part_apm_probe(struct g_part_table *, struct g_consumer *);
70 static int g_part_apm_read(struct g_part_table *, struct g_consumer *);
71 static const char *g_part_apm_type(struct g_part_table *, struct g_part_entry *,
72     char *, size_t);
73 static int g_part_apm_write(struct g_part_table *, struct g_consumer *);
74 
75 static kobj_method_t g_part_apm_methods[] = {
76 	KOBJMETHOD(g_part_add,		g_part_apm_add),
77 	KOBJMETHOD(g_part_create,	g_part_apm_create),
78 	KOBJMETHOD(g_part_destroy,	g_part_apm_destroy),
79 	KOBJMETHOD(g_part_dumpto,	g_part_apm_dumpto),
80 	KOBJMETHOD(g_part_modify,	g_part_apm_modify),
81 	KOBJMETHOD(g_part_name,		g_part_apm_name),
82 	KOBJMETHOD(g_part_probe,	g_part_apm_probe),
83 	KOBJMETHOD(g_part_read,		g_part_apm_read),
84 	KOBJMETHOD(g_part_type,		g_part_apm_type),
85 	KOBJMETHOD(g_part_write,	g_part_apm_write),
86 	{ 0, 0 }
87 };
88 
89 static struct g_part_scheme g_part_apm_scheme = {
90 	"APM",
91 	g_part_apm_methods,
92 	sizeof(struct g_part_apm_table),
93 	.gps_entrysz = sizeof(struct g_part_apm_entry),
94 	.gps_minent = 16,
95 	.gps_maxent = INT_MAX,
96 };
97 G_PART_SCHEME_DECLARE(g_part_apm_scheme);
98 
99 static int
100 apm_parse_type(const char *type, char *buf, size_t bufsz)
101 {
102 	const char *alias;
103 
104 	if (type[0] == '!') {
105 		type++;
106 		if (strlen(type) > bufsz)
107 			return (EINVAL);
108 		if (!strcmp(type, APM_ENT_TYPE_SELF) ||
109 		    !strcmp(type, APM_ENT_TYPE_UNUSED))
110 			return (EINVAL);
111 		strncpy(buf, type, bufsz);
112 		return (0);
113 	}
114 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD);
115 	if (!strcasecmp(type, alias)) {
116 		strcpy(buf, APM_ENT_TYPE_FREEBSD);
117 		return (0);
118 	}
119 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
120 	if (!strcasecmp(type, alias)) {
121 		strcpy(buf, APM_ENT_TYPE_FREEBSD_SWAP);
122 		return (0);
123 	}
124 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
125 	if (!strcasecmp(type, alias)) {
126 		strcpy(buf, APM_ENT_TYPE_FREEBSD_UFS);
127 		return (0);
128 	}
129 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
130 	if (!strcasecmp(type, alias)) {
131 		strcpy(buf, APM_ENT_TYPE_FREEBSD_VINUM);
132 		return (0);
133 	}
134 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
135 	if (!strcasecmp(type, alias)) {
136 		strcpy(buf, APM_ENT_TYPE_FREEBSD_ZFS);
137 		return (0);
138 	}
139 	return (EINVAL);
140 }
141 
142 static int
143 apm_read_ent(struct g_consumer *cp, uint32_t blk, struct apm_ent *ent)
144 {
145 	struct g_provider *pp;
146 	char *buf;
147 	int error;
148 
149 	pp = cp->provider;
150 	buf = g_read_data(cp, pp->sectorsize * blk, pp->sectorsize, &error);
151 	if (buf == NULL)
152 		return (error);
153 	ent->ent_sig = be16dec(buf);
154 	ent->ent_pmblkcnt = be32dec(buf + 4);
155 	ent->ent_start = be32dec(buf + 8);
156 	ent->ent_size = be32dec(buf + 12);
157 	bcopy(buf + 16, ent->ent_name, sizeof(ent->ent_name));
158 	bcopy(buf + 48, ent->ent_type, sizeof(ent->ent_type));
159 	g_free(buf);
160 	return (0);
161 }
162 
163 static int
164 g_part_apm_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
165     struct g_part_parms *gpp)
166 {
167 	struct g_part_apm_entry *entry;
168 	struct g_part_apm_table *table;
169 	int error;
170 
171 	entry = (struct g_part_apm_entry *)baseentry;
172 	table = (struct g_part_apm_table *)basetable;
173 	entry->ent.ent_sig = APM_ENT_SIG;
174 	entry->ent.ent_pmblkcnt = table->self.ent_pmblkcnt;
175 	entry->ent.ent_start = gpp->gpp_start;
176 	entry->ent.ent_size = gpp->gpp_size;
177 	if (baseentry->gpe_deleted) {
178 		bzero(entry->ent.ent_type, sizeof(entry->ent.ent_type));
179 		bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name));
180 	}
181 	error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type,
182 	    sizeof(entry->ent.ent_type));
183 	if (error)
184 		return (error);
185 	if (gpp->gpp_parms & G_PART_PARM_LABEL) {
186 		if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name))
187 			return (EINVAL);
188 		strncpy(entry->ent.ent_name, gpp->gpp_label,
189 		    sizeof(entry->ent.ent_name));
190 	}
191 	return (0);
192 }
193 
194 static int
195 g_part_apm_create(struct g_part_table *basetable, struct g_part_parms *gpp)
196 {
197 	struct g_provider *pp;
198 	struct g_part_apm_table *table;
199 
200 	table = (struct g_part_apm_table *)basetable;
201 	pp = gpp->gpp_provider;
202 	if (pp->sectorsize != 512 ||
203 	    pp->mediasize < (2 + 2 * basetable->gpt_entries) * pp->sectorsize)
204 		return (ENOSPC);
205 
206 	basetable->gpt_first = 2 + basetable->gpt_entries;
207 	basetable->gpt_last = (pp->mediasize / pp->sectorsize) - 1;
208 
209 	table->ddr.ddr_sig = APM_DDR_SIG;
210 	table->ddr.ddr_blksize = pp->sectorsize;
211 	table->ddr.ddr_blkcount = basetable->gpt_last + 1;
212 
213 	table->self.ent_sig = APM_ENT_SIG;
214 	table->self.ent_pmblkcnt = basetable->gpt_entries + 1;
215 	table->self.ent_start = 1;
216 	table->self.ent_size = table->self.ent_pmblkcnt;
217 	strcpy(table->self.ent_name, "Apple");
218 	strcpy(table->self.ent_type, APM_ENT_TYPE_SELF);
219 	return (0);
220 }
221 
222 static int
223 g_part_apm_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
224 {
225 
226 	/* Wipe the first 2 sectors to clear the partitioning. */
227 	basetable->gpt_smhead |= 3;
228 	return (0);
229 }
230 
231 static int
232 g_part_apm_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
233 {
234 	struct g_part_apm_entry *entry;
235 
236 	entry = (struct g_part_apm_entry *)baseentry;
237 	return ((!strcmp(entry->ent.ent_type, APM_ENT_TYPE_FREEBSD_SWAP))
238 	    ? 1 : 0);
239 }
240 
241 static int
242 g_part_apm_modify(struct g_part_table *basetable,
243     struct g_part_entry *baseentry, struct g_part_parms *gpp)
244 {
245 	struct g_part_apm_entry *entry;
246 	int error;
247 
248 	entry = (struct g_part_apm_entry *)baseentry;
249 	if (gpp->gpp_parms & G_PART_PARM_LABEL) {
250 		if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name))
251 			return (EINVAL);
252 	}
253 	if (gpp->gpp_parms & G_PART_PARM_TYPE) {
254 		error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type,
255 		    sizeof(entry->ent.ent_type));
256 		if (error)
257 			return (error);
258 	}
259 	if (gpp->gpp_parms & G_PART_PARM_LABEL) {
260 		strncpy(entry->ent.ent_name, gpp->gpp_label,
261 		    sizeof(entry->ent.ent_name));
262 	}
263 	return (0);
264 }
265 
266 static char *
267 g_part_apm_name(struct g_part_table *table, struct g_part_entry *baseentry,
268     char *buf, size_t bufsz)
269 {
270 
271 	snprintf(buf, bufsz, "s%d", baseentry->gpe_index + 1);
272 	return (buf);
273 }
274 
275 static int
276 g_part_apm_probe(struct g_part_table *basetable, struct g_consumer *cp)
277 {
278 	struct g_provider *pp;
279 	struct g_part_apm_table *table;
280 	char *buf;
281 	int error;
282 
283 	/* We don't nest, which means that our depth should be 0. */
284 	if (basetable->gpt_depth != 0)
285 		return (ENXIO);
286 
287 	table = (struct g_part_apm_table *)basetable;
288 	pp = cp->provider;
289 
290 	/* Sanity-check the provider. */
291 	if (pp->mediasize < 4 * pp->sectorsize)
292 		return (ENOSPC);
293 
294 	/* Check that there's a Driver Descriptor Record (DDR). */
295 	buf = g_read_data(cp, 0L, pp->sectorsize, &error);
296 	if (buf == NULL)
297 		return (error);
298 	table->ddr.ddr_sig = be16dec(buf);
299 	table->ddr.ddr_blksize = be16dec(buf + 2);
300 	table->ddr.ddr_blkcount = be32dec(buf + 4);
301 	g_free(buf);
302 	if (table->ddr.ddr_sig != APM_DDR_SIG)
303 		return (ENXIO);
304 	if (table->ddr.ddr_blksize != pp->sectorsize)
305 		return (ENXIO);
306 
307 	/* Check that there's a Partition Map. */
308 	error = apm_read_ent(cp, 1, &table->self);
309 	if (error)
310 		return (error);
311 	if (table->self.ent_sig != APM_ENT_SIG)
312 		return (ENXIO);
313 	if (strcmp(table->self.ent_type, APM_ENT_TYPE_SELF))
314 		return (ENXIO);
315 	if (table->self.ent_pmblkcnt >= table->ddr.ddr_blkcount)
316 		return (ENXIO);
317 	return (G_PART_PROBE_PRI_NORM);
318 }
319 
320 static int
321 g_part_apm_read(struct g_part_table *basetable, struct g_consumer *cp)
322 {
323 	struct apm_ent ent;
324 	struct g_part_apm_entry *entry;
325 	struct g_part_apm_table *table;
326 	int error, index;
327 
328 	table = (struct g_part_apm_table *)basetable;
329 
330 	basetable->gpt_first = table->self.ent_pmblkcnt + 1;
331 	basetable->gpt_last = table->ddr.ddr_blkcount - 1;
332 	basetable->gpt_entries = table->self.ent_pmblkcnt - 1;
333 
334 	for (index = table->self.ent_pmblkcnt - 1; index > 0; index--) {
335 		error = apm_read_ent(cp, index + 1, &ent);
336 		if (error)
337 			continue;
338 		if (!strcmp(ent.ent_type, APM_ENT_TYPE_UNUSED))
339 			continue;
340 		entry = (struct g_part_apm_entry *)g_part_new_entry(basetable,
341 		    index, ent.ent_start, ent.ent_start + ent.ent_size - 1);
342 		entry->ent = ent;
343 	}
344 
345 	return (0);
346 }
347 
348 static const char *
349 g_part_apm_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
350     char *buf, size_t bufsz)
351 {
352 	struct g_part_apm_entry *entry;
353 	const char *type;
354 	size_t len;
355 
356 	entry = (struct g_part_apm_entry *)baseentry;
357 	type = entry->ent.ent_type;
358 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD))
359 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD));
360 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD_SWAP))
361 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
362 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD_UFS))
363 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
364 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD_VINUM))
365 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
366 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD_ZFS))
367 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
368 	buf[0] = '!';
369 	len = MIN(sizeof(entry->ent.ent_type), bufsz - 2);
370 	bcopy(type, buf + 1, len);
371 	buf[len + 1] = '\0';
372 	return (buf);
373 }
374 
375 static int
376 g_part_apm_write(struct g_part_table *basetable, struct g_consumer *cp)
377 {
378 	char buf[512];
379 	struct g_part_entry *baseentry;
380 	struct g_part_apm_entry *entry;
381 	struct g_part_apm_table *table;
382 	int error, index;
383 
384 	table = (struct g_part_apm_table *)basetable;
385 	bzero(buf, sizeof(buf));
386 
387 	/* Write the DDR and 'self' entry only when we're newly created. */
388 	if (basetable->gpt_created) {
389 		be16enc(buf, table->ddr.ddr_sig);
390 		be16enc(buf + 2, table->ddr.ddr_blksize);
391 		be32enc(buf + 4, table->ddr.ddr_blkcount);
392 		error = g_write_data(cp, 0, buf, sizeof(buf));
393 		if (error)
394 			return (error);
395 	}
396 
397 	be16enc(buf, table->self.ent_sig);
398 	be16enc(buf + 2, 0);
399 	be32enc(buf + 4, table->self.ent_pmblkcnt);
400 
401 	if (basetable->gpt_created) {
402 		be32enc(buf + 8, table->self.ent_start);
403 		be32enc(buf + 12, table->self.ent_size);
404 		bcopy(table->self.ent_name, buf + 16,
405 		    sizeof(table->self.ent_name));
406 		bcopy(table->self.ent_type, buf + 48,
407 		    sizeof(table->self.ent_type));
408 		error = g_write_data(cp, 512, buf, sizeof(buf));
409 		if (error)
410 			return (error);
411 	}
412 
413 	baseentry = LIST_FIRST(&basetable->gpt_entry);
414 	for (index = 1; index <= basetable->gpt_entries; index++) {
415 		entry = (baseentry != NULL && index == baseentry->gpe_index)
416 		    ? (struct g_part_apm_entry *)baseentry : NULL;
417 		if (entry != NULL && !baseentry->gpe_deleted) {
418 			be32enc(buf + 8, entry->ent.ent_start);
419 			be32enc(buf + 12, entry->ent.ent_size);
420 			bcopy(entry->ent.ent_name, buf + 16,
421 			    sizeof(entry->ent.ent_name));
422 			bcopy(entry->ent.ent_type, buf + 48,
423 			    sizeof(entry->ent.ent_type));
424 		} else {
425 			bzero(buf + 8, 4 + 4 + 32 + 32);
426 			strcpy(buf + 48, APM_ENT_TYPE_UNUSED);
427 		}
428 		error = g_write_data(cp, (index + 1) * 512, buf, sizeof(buf));
429 		if (error)
430 			return (error);
431 		if (entry != NULL)
432 			baseentry = LIST_NEXT(baseentry, gpe_entry);
433 	}
434 
435 	return (0);
436 }
437