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