xref: /freebsd/sys/geom/part/g_part_apm.c (revision c0020399)
1 /*-
2  * Copyright (c) 2006-2008 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 	int			tivo_series1;
54 };
55 
56 struct g_part_apm_entry {
57 	struct g_part_entry	base;
58 	struct apm_ent		ent;
59 };
60 
61 static int g_part_apm_add(struct g_part_table *, struct g_part_entry *,
62     struct g_part_parms *);
63 static int g_part_apm_create(struct g_part_table *, struct g_part_parms *);
64 static int g_part_apm_destroy(struct g_part_table *, struct g_part_parms *);
65 static void g_part_apm_dumpconf(struct g_part_table *, struct g_part_entry *,
66     struct sbuf *, const char *);
67 static int g_part_apm_dumpto(struct g_part_table *, struct g_part_entry *);
68 static int g_part_apm_modify(struct g_part_table *, struct g_part_entry *,
69     struct g_part_parms *);
70 static const char *g_part_apm_name(struct g_part_table *, struct g_part_entry *,
71     char *, size_t);
72 static int g_part_apm_probe(struct g_part_table *, struct g_consumer *);
73 static int g_part_apm_read(struct g_part_table *, struct g_consumer *);
74 static const char *g_part_apm_type(struct g_part_table *, struct g_part_entry *,
75     char *, size_t);
76 static int g_part_apm_write(struct g_part_table *, struct g_consumer *);
77 
78 static kobj_method_t g_part_apm_methods[] = {
79 	KOBJMETHOD(g_part_add,		g_part_apm_add),
80 	KOBJMETHOD(g_part_create,	g_part_apm_create),
81 	KOBJMETHOD(g_part_destroy,	g_part_apm_destroy),
82 	KOBJMETHOD(g_part_dumpconf,	g_part_apm_dumpconf),
83 	KOBJMETHOD(g_part_dumpto,	g_part_apm_dumpto),
84 	KOBJMETHOD(g_part_modify,	g_part_apm_modify),
85 	KOBJMETHOD(g_part_name,		g_part_apm_name),
86 	KOBJMETHOD(g_part_probe,	g_part_apm_probe),
87 	KOBJMETHOD(g_part_read,		g_part_apm_read),
88 	KOBJMETHOD(g_part_type,		g_part_apm_type),
89 	KOBJMETHOD(g_part_write,	g_part_apm_write),
90 	{ 0, 0 }
91 };
92 
93 static struct g_part_scheme g_part_apm_scheme = {
94 	"APM",
95 	g_part_apm_methods,
96 	sizeof(struct g_part_apm_table),
97 	.gps_entrysz = sizeof(struct g_part_apm_entry),
98 	.gps_minent = 16,
99 	.gps_maxent = INT_MAX,
100 };
101 G_PART_SCHEME_DECLARE(g_part_apm);
102 
103 static void
104 swab(char *buf, size_t bufsz)
105 {
106 	int i;
107 	char ch;
108 
109 	for (i = 0; i < bufsz; i += 2) {
110 		ch = buf[i];
111 		buf[i] = buf[i + 1];
112 		buf[i + 1] = ch;
113 	}
114 }
115 
116 static int
117 apm_parse_type(const char *type, char *buf, size_t bufsz)
118 {
119 	const char *alias;
120 
121 	if (type[0] == '!') {
122 		type++;
123 		if (strlen(type) > bufsz)
124 			return (EINVAL);
125 		if (!strcmp(type, APM_ENT_TYPE_SELF) ||
126 		    !strcmp(type, APM_ENT_TYPE_UNUSED))
127 			return (EINVAL);
128 		strncpy(buf, type, bufsz);
129 		return (0);
130 	}
131 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD);
132 	if (!strcasecmp(type, alias)) {
133 		strcpy(buf, APM_ENT_TYPE_FREEBSD);
134 		return (0);
135 	}
136 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
137 	if (!strcasecmp(type, alias)) {
138 		strcpy(buf, APM_ENT_TYPE_FREEBSD_SWAP);
139 		return (0);
140 	}
141 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
142 	if (!strcasecmp(type, alias)) {
143 		strcpy(buf, APM_ENT_TYPE_FREEBSD_UFS);
144 		return (0);
145 	}
146 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
147 	if (!strcasecmp(type, alias)) {
148 		strcpy(buf, APM_ENT_TYPE_FREEBSD_VINUM);
149 		return (0);
150 	}
151 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
152 	if (!strcasecmp(type, alias)) {
153 		strcpy(buf, APM_ENT_TYPE_FREEBSD_ZFS);
154 		return (0);
155 	}
156 	return (EINVAL);
157 }
158 
159 static int
160 apm_read_ent(struct g_consumer *cp, uint32_t blk, struct apm_ent *ent,
161     int tivo_series1)
162 {
163 	struct g_provider *pp;
164 	char *buf;
165 	int error;
166 
167 	pp = cp->provider;
168 	buf = g_read_data(cp, pp->sectorsize * blk, pp->sectorsize, &error);
169 	if (buf == NULL)
170 		return (error);
171 	if (tivo_series1)
172 		swab(buf, pp->sectorsize);
173 	ent->ent_sig = be16dec(buf);
174 	ent->ent_pmblkcnt = be32dec(buf + 4);
175 	ent->ent_start = be32dec(buf + 8);
176 	ent->ent_size = be32dec(buf + 12);
177 	bcopy(buf + 16, ent->ent_name, sizeof(ent->ent_name));
178 	bcopy(buf + 48, ent->ent_type, sizeof(ent->ent_type));
179 	g_free(buf);
180 	return (0);
181 }
182 
183 static int
184 g_part_apm_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
185     struct g_part_parms *gpp)
186 {
187 	struct g_part_apm_entry *entry;
188 	struct g_part_apm_table *table;
189 	int error;
190 
191 	entry = (struct g_part_apm_entry *)baseentry;
192 	table = (struct g_part_apm_table *)basetable;
193 	entry->ent.ent_sig = APM_ENT_SIG;
194 	entry->ent.ent_pmblkcnt = table->self.ent_pmblkcnt;
195 	entry->ent.ent_start = gpp->gpp_start;
196 	entry->ent.ent_size = gpp->gpp_size;
197 	if (baseentry->gpe_deleted) {
198 		bzero(entry->ent.ent_type, sizeof(entry->ent.ent_type));
199 		bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name));
200 	}
201 	error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type,
202 	    sizeof(entry->ent.ent_type));
203 	if (error)
204 		return (error);
205 	if (gpp->gpp_parms & G_PART_PARM_LABEL) {
206 		if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name))
207 			return (EINVAL);
208 		strncpy(entry->ent.ent_name, gpp->gpp_label,
209 		    sizeof(entry->ent.ent_name));
210 	}
211 	return (0);
212 }
213 
214 static int
215 g_part_apm_create(struct g_part_table *basetable, struct g_part_parms *gpp)
216 {
217 	struct g_provider *pp;
218 	struct g_part_apm_table *table;
219 	uint32_t last;
220 
221 	/* We don't nest, which means that our depth should be 0. */
222 	if (basetable->gpt_depth != 0)
223 		return (ENXIO);
224 
225 	table = (struct g_part_apm_table *)basetable;
226 	pp = gpp->gpp_provider;
227 	if (pp->sectorsize != 512 ||
228 	    pp->mediasize < (2 + 2 * basetable->gpt_entries) * pp->sectorsize)
229 		return (ENOSPC);
230 
231 	/* APM uses 32-bit LBAs. */
232 	last = MIN(pp->mediasize / pp->sectorsize, 0xffffffff) - 1;
233 
234 	basetable->gpt_first = 2 + basetable->gpt_entries;
235 	basetable->gpt_last = last;
236 
237 	table->ddr.ddr_sig = APM_DDR_SIG;
238 	table->ddr.ddr_blksize = pp->sectorsize;
239 	table->ddr.ddr_blkcount = last + 1;
240 
241 	table->self.ent_sig = APM_ENT_SIG;
242 	table->self.ent_pmblkcnt = basetable->gpt_entries + 1;
243 	table->self.ent_start = 1;
244 	table->self.ent_size = table->self.ent_pmblkcnt;
245 	strcpy(table->self.ent_name, "Apple");
246 	strcpy(table->self.ent_type, APM_ENT_TYPE_SELF);
247 	return (0);
248 }
249 
250 static int
251 g_part_apm_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
252 {
253 
254 	/* Wipe the first 2 sectors to clear the partitioning. */
255 	basetable->gpt_smhead |= 3;
256 	return (0);
257 }
258 
259 static void
260 g_part_apm_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
261     struct sbuf *sb, const char *indent)
262 {
263 	union {
264 		char name[APM_ENT_NAMELEN + 1];
265 		char type[APM_ENT_TYPELEN + 1];
266 	} u;
267 	struct g_part_apm_entry *entry;
268 
269 	entry = (struct g_part_apm_entry *)baseentry;
270 	if (indent == NULL) {
271 		/* conftxt: libdisk compatibility */
272 		sbuf_printf(sb, " xs APPLE xt %s", entry->ent.ent_type);
273 	} else if (entry != NULL) {
274 		/* confxml: partition entry information */
275 		strncpy(u.name, entry->ent.ent_name, APM_ENT_NAMELEN);
276 		u.name[APM_ENT_NAMELEN] = '\0';
277 		sbuf_printf(sb, "%s<label>%s</label>\n", indent, u.name);
278 		strncpy(u.type, entry->ent.ent_type, APM_ENT_TYPELEN);
279 		u.type[APM_ENT_TYPELEN] = '\0';
280 		sbuf_printf(sb, "%s<rawtype>%s</rawtype>\n", indent, u.type);
281 	} else {
282 		/* confxml: scheme information */
283 	}
284 }
285 
286 static int
287 g_part_apm_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
288 {
289 	struct g_part_apm_entry *entry;
290 
291 	entry = (struct g_part_apm_entry *)baseentry;
292 	return ((!strcmp(entry->ent.ent_type, APM_ENT_TYPE_FREEBSD_SWAP))
293 	    ? 1 : 0);
294 }
295 
296 static int
297 g_part_apm_modify(struct g_part_table *basetable,
298     struct g_part_entry *baseentry, struct g_part_parms *gpp)
299 {
300 	struct g_part_apm_entry *entry;
301 	int error;
302 
303 	entry = (struct g_part_apm_entry *)baseentry;
304 	if (gpp->gpp_parms & G_PART_PARM_LABEL) {
305 		if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name))
306 			return (EINVAL);
307 	}
308 	if (gpp->gpp_parms & G_PART_PARM_TYPE) {
309 		error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type,
310 		    sizeof(entry->ent.ent_type));
311 		if (error)
312 			return (error);
313 	}
314 	if (gpp->gpp_parms & G_PART_PARM_LABEL) {
315 		strncpy(entry->ent.ent_name, gpp->gpp_label,
316 		    sizeof(entry->ent.ent_name));
317 	}
318 	return (0);
319 }
320 
321 static const char *
322 g_part_apm_name(struct g_part_table *table, struct g_part_entry *baseentry,
323     char *buf, size_t bufsz)
324 {
325 
326 	snprintf(buf, bufsz, "s%d", baseentry->gpe_index + 1);
327 	return (buf);
328 }
329 
330 static int
331 g_part_apm_probe(struct g_part_table *basetable, struct g_consumer *cp)
332 {
333 	struct g_provider *pp;
334 	struct g_part_apm_table *table;
335 	char *buf;
336 	int error;
337 
338 	/* We don't nest, which means that our depth should be 0. */
339 	if (basetable->gpt_depth != 0)
340 		return (ENXIO);
341 
342 	table = (struct g_part_apm_table *)basetable;
343 	table->tivo_series1 = 0;
344 	pp = cp->provider;
345 
346 	/* Sanity-check the provider. */
347 	if (pp->mediasize < 4 * pp->sectorsize)
348 		return (ENOSPC);
349 
350 	/* Check that there's a Driver Descriptor Record (DDR). */
351 	buf = g_read_data(cp, 0L, pp->sectorsize, &error);
352 	if (buf == NULL)
353 		return (error);
354 	if (be16dec(buf) == be16toh(APM_DDR_SIG)) {
355 		/* Normal Apple DDR */
356 		table->ddr.ddr_sig = be16dec(buf);
357 		table->ddr.ddr_blksize = be16dec(buf + 2);
358 		table->ddr.ddr_blkcount = be32dec(buf + 4);
359 		g_free(buf);
360 		if (table->ddr.ddr_blksize != pp->sectorsize)
361 			return (ENXIO);
362 	} else {
363 		/*
364 		 * Check for Tivo drives, which have no DDR and a different
365 		 * signature.  Those whose first two bytes are 14 92 are
366 		 * Series 2 drives, and aren't supported.  Those that start
367 		 * with 92 14 are series 1 drives and are supported.
368 		 */
369 		if (be16dec(buf) != 0x9214) {
370 			/* If this is 0x1492 it could be a series 2 drive */
371 			g_free(buf);
372 			return (ENXIO);
373 		}
374 		table->ddr.ddr_sig = APM_DDR_SIG;		/* XXX */
375 		table->ddr.ddr_blksize = pp->sectorsize;	/* XXX */
376 		table->ddr.ddr_blkcount = pp->mediasize / pp->sectorsize;/* XXX */
377 		table->tivo_series1 = 1;
378 		g_free(buf);
379 	}
380 
381 	/* Check that there's a Partition Map. */
382 	error = apm_read_ent(cp, 1, &table->self, table->tivo_series1);
383 	if (error)
384 		return (error);
385 	if (table->self.ent_sig != APM_ENT_SIG)
386 		return (ENXIO);
387 	if (strcmp(table->self.ent_type, APM_ENT_TYPE_SELF))
388 		return (ENXIO);
389 	if (table->self.ent_pmblkcnt >= table->ddr.ddr_blkcount)
390 		return (ENXIO);
391 	return (G_PART_PROBE_PRI_NORM);
392 }
393 
394 static int
395 g_part_apm_read(struct g_part_table *basetable, struct g_consumer *cp)
396 {
397 	struct apm_ent ent;
398 	struct g_part_apm_entry *entry;
399 	struct g_part_apm_table *table;
400 	int error, index;
401 
402 	table = (struct g_part_apm_table *)basetable;
403 
404 	basetable->gpt_first = table->self.ent_pmblkcnt + 1;
405 	basetable->gpt_last = table->ddr.ddr_blkcount - 1;
406 	basetable->gpt_entries = table->self.ent_pmblkcnt - 1;
407 
408 	for (index = table->self.ent_pmblkcnt - 1; index > 0; index--) {
409 		error = apm_read_ent(cp, index + 1, &ent, table->tivo_series1);
410 		if (error)
411 			continue;
412 		if (!strcmp(ent.ent_type, APM_ENT_TYPE_UNUSED))
413 			continue;
414 		entry = (struct g_part_apm_entry *)g_part_new_entry(basetable,
415 		    index, ent.ent_start, ent.ent_start + ent.ent_size - 1);
416 		entry->ent = ent;
417 	}
418 
419 	return (0);
420 }
421 
422 static const char *
423 g_part_apm_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
424     char *buf, size_t bufsz)
425 {
426 	struct g_part_apm_entry *entry;
427 	const char *type;
428 	size_t len;
429 
430 	entry = (struct g_part_apm_entry *)baseentry;
431 	type = entry->ent.ent_type;
432 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD))
433 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD));
434 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD_SWAP))
435 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
436 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD_UFS))
437 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
438 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD_VINUM))
439 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
440 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD_ZFS))
441 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
442 	buf[0] = '!';
443 	len = MIN(sizeof(entry->ent.ent_type), bufsz - 2);
444 	bcopy(type, buf + 1, len);
445 	buf[len + 1] = '\0';
446 	return (buf);
447 }
448 
449 static int
450 g_part_apm_write(struct g_part_table *basetable, struct g_consumer *cp)
451 {
452 	char buf[512];
453 	struct g_part_entry *baseentry;
454 	struct g_part_apm_entry *entry;
455 	struct g_part_apm_table *table;
456 	int error, index;
457 
458 	table = (struct g_part_apm_table *)basetable;
459 	/*
460 	 * Tivo Series 1 disk partitions are currently read-only.
461 	 */
462 	if (table->tivo_series1)
463 		return (EOPNOTSUPP);
464 	bzero(buf, sizeof(buf));
465 
466 	/* Write the DDR and 'self' entry only when we're newly created. */
467 	if (basetable->gpt_created) {
468 		be16enc(buf, table->ddr.ddr_sig);
469 		be16enc(buf + 2, table->ddr.ddr_blksize);
470 		be32enc(buf + 4, table->ddr.ddr_blkcount);
471 		error = g_write_data(cp, 0, buf, sizeof(buf));
472 		if (error)
473 			return (error);
474 	}
475 
476 	be16enc(buf, table->self.ent_sig);
477 	be16enc(buf + 2, 0);
478 	be32enc(buf + 4, table->self.ent_pmblkcnt);
479 
480 	if (basetable->gpt_created) {
481 		be32enc(buf + 8, table->self.ent_start);
482 		be32enc(buf + 12, table->self.ent_size);
483 		bcopy(table->self.ent_name, buf + 16,
484 		    sizeof(table->self.ent_name));
485 		bcopy(table->self.ent_type, buf + 48,
486 		    sizeof(table->self.ent_type));
487 		error = g_write_data(cp, 512, buf, sizeof(buf));
488 		if (error)
489 			return (error);
490 	}
491 
492 	baseentry = LIST_FIRST(&basetable->gpt_entry);
493 	for (index = 1; index <= basetable->gpt_entries; index++) {
494 		entry = (baseentry != NULL && index == baseentry->gpe_index)
495 		    ? (struct g_part_apm_entry *)baseentry : NULL;
496 		if (entry != NULL && !baseentry->gpe_deleted) {
497 			be32enc(buf + 8, entry->ent.ent_start);
498 			be32enc(buf + 12, entry->ent.ent_size);
499 			bcopy(entry->ent.ent_name, buf + 16,
500 			    sizeof(entry->ent.ent_name));
501 			bcopy(entry->ent.ent_type, buf + 48,
502 			    sizeof(entry->ent.ent_type));
503 		} else {
504 			bzero(buf + 8, 4 + 4 + 32 + 32);
505 			strcpy(buf + 48, APM_ENT_TYPE_UNUSED);
506 		}
507 		error = g_write_data(cp, (index + 1) * 512, buf, sizeof(buf));
508 		if (error)
509 			return (error);
510 		if (entry != NULL)
511 			baseentry = LIST_NEXT(baseentry, gpe_entry);
512 	}
513 
514 	return (0);
515 }
516