xref: /freebsd/sys/geom/part/g_part_apm.c (revision e9ac41698b2f322d55ccf9da50a3596edb2c1800)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2006-2008 Marcel Moolenaar
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/apm.h>
31 #include <sys/bio.h>
32 #include <sys/endian.h>
33 #include <sys/kernel.h>
34 #include <sys/kobj.h>
35 #include <sys/limits.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/queue.h>
40 #include <sys/sbuf.h>
41 #include <sys/systm.h>
42 #include <sys/sysctl.h>
43 #include <geom/geom.h>
44 #include <geom/geom_int.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 MODULE_VERSION(geom_part_apm, 0);
108 
109 static void
110 swab(char *buf, size_t bufsz)
111 {
112 	int i;
113 	char ch;
114 
115 	for (i = 0; i < bufsz; i += 2) {
116 		ch = buf[i];
117 		buf[i] = buf[i + 1];
118 		buf[i + 1] = ch;
119 	}
120 }
121 
122 static int
123 apm_parse_type(const char *type, char *buf, size_t bufsz)
124 {
125 	const char *alias;
126 
127 	if (type[0] == '!') {
128 		type++;
129 		if (strlen(type) > bufsz)
130 			return (EINVAL);
131 		if (!strcmp(type, APM_ENT_TYPE_SELF) ||
132 		    !strcmp(type, APM_ENT_TYPE_UNUSED))
133 			return (EINVAL);
134 		strncpy(buf, type, bufsz);
135 		return (0);
136 	}
137 	alias = g_part_alias_name(G_PART_ALIAS_APPLE_BOOT);
138 	if (!strcasecmp(type, alias)) {
139 		strcpy(buf, APM_ENT_TYPE_APPLE_BOOT);
140 		return (0);
141 	}
142 	alias = g_part_alias_name(G_PART_ALIAS_APPLE_HFS);
143 	if (!strcasecmp(type, alias)) {
144 		strcpy(buf, APM_ENT_TYPE_APPLE_HFS);
145 		return (0);
146 	}
147 	alias = g_part_alias_name(G_PART_ALIAS_APPLE_UFS);
148 	if (!strcasecmp(type, alias)) {
149 		strcpy(buf, APM_ENT_TYPE_APPLE_UFS);
150 		return (0);
151 	}
152 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD);
153 	if (!strcasecmp(type, alias)) {
154 		strcpy(buf, APM_ENT_TYPE_FREEBSD);
155 		return (0);
156 	}
157 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_NANDFS);
158 	if (!strcasecmp(type, alias)) {
159 		strcpy(buf, APM_ENT_TYPE_FREEBSD_NANDFS);
160 		return (0);
161 	}
162 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
163 	if (!strcasecmp(type, alias)) {
164 		strcpy(buf, APM_ENT_TYPE_FREEBSD_SWAP);
165 		return (0);
166 	}
167 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
168 	if (!strcasecmp(type, alias)) {
169 		strcpy(buf, APM_ENT_TYPE_FREEBSD_UFS);
170 		return (0);
171 	}
172 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
173 	if (!strcasecmp(type, alias)) {
174 		strcpy(buf, APM_ENT_TYPE_FREEBSD_VINUM);
175 		return (0);
176 	}
177 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
178 	if (!strcasecmp(type, alias)) {
179 		strcpy(buf, APM_ENT_TYPE_FREEBSD_ZFS);
180 		return (0);
181 	}
182 	return (EINVAL);
183 }
184 
185 static int
186 apm_read_ent(struct g_consumer *cp, uint32_t blk, struct apm_ent *ent,
187     int tivo_series1)
188 {
189 	struct g_provider *pp;
190 	char *buf;
191 	int error;
192 
193 	pp = cp->provider;
194 	buf = g_read_data(cp, pp->sectorsize * blk, pp->sectorsize, &error);
195 	if (buf == NULL)
196 		return (error);
197 	if (tivo_series1)
198 		swab(buf, pp->sectorsize);
199 	ent->ent_sig = be16dec(buf);
200 	ent->ent_pmblkcnt = be32dec(buf + 4);
201 	ent->ent_start = be32dec(buf + 8);
202 	ent->ent_size = be32dec(buf + 12);
203 	bcopy(buf + 16, ent->ent_name, sizeof(ent->ent_name));
204 	bcopy(buf + 48, ent->ent_type, sizeof(ent->ent_type));
205 	g_free(buf);
206 	return (0);
207 }
208 
209 static int
210 g_part_apm_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
211     struct g_part_parms *gpp)
212 {
213 	struct g_part_apm_entry *entry;
214 	struct g_part_apm_table *table;
215 	int error;
216 
217 	entry = (struct g_part_apm_entry *)baseentry;
218 	table = (struct g_part_apm_table *)basetable;
219 	entry->ent.ent_sig = APM_ENT_SIG;
220 	entry->ent.ent_pmblkcnt = table->self.ent_pmblkcnt;
221 	entry->ent.ent_start = gpp->gpp_start;
222 	entry->ent.ent_size = gpp->gpp_size;
223 	if (baseentry->gpe_deleted) {
224 		bzero(entry->ent.ent_type, sizeof(entry->ent.ent_type));
225 		bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name));
226 	}
227 	error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type,
228 	    sizeof(entry->ent.ent_type));
229 	if (error)
230 		return (error);
231 	if (gpp->gpp_parms & G_PART_PARM_LABEL) {
232 		if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name))
233 			return (EINVAL);
234 		strncpy(entry->ent.ent_name, gpp->gpp_label,
235 		    sizeof(entry->ent.ent_name));
236 	}
237 	if (baseentry->gpe_index >= table->self.ent_pmblkcnt)
238 		table->self.ent_pmblkcnt = baseentry->gpe_index + 1;
239 	KASSERT(table->self.ent_size >= table->self.ent_pmblkcnt,
240 	    ("%s", __func__));
241 	KASSERT(table->self.ent_size > baseentry->gpe_index,
242 	    ("%s", __func__));
243 	return (0);
244 }
245 
246 static int
247 g_part_apm_create(struct g_part_table *basetable, struct g_part_parms *gpp)
248 {
249 	struct g_provider *pp;
250 	struct g_part_apm_table *table;
251 	uint32_t last;
252 
253 	/* We don't nest, which means that our depth should be 0. */
254 	if (basetable->gpt_depth != 0)
255 		return (ENXIO);
256 
257 	table = (struct g_part_apm_table *)basetable;
258 	pp = gpp->gpp_provider;
259 	if (pp->sectorsize != 512 ||
260 	    pp->mediasize < (2 + 2 * basetable->gpt_entries) * pp->sectorsize)
261 		return (ENOSPC);
262 
263 	/* APM uses 32-bit LBAs. */
264 	last = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX) - 1;
265 
266 	basetable->gpt_first = 2 + basetable->gpt_entries;
267 	basetable->gpt_last = last;
268 
269 	table->ddr.ddr_sig = APM_DDR_SIG;
270 	table->ddr.ddr_blksize = pp->sectorsize;
271 	table->ddr.ddr_blkcount = last + 1;
272 
273 	table->self.ent_sig = APM_ENT_SIG;
274 	table->self.ent_pmblkcnt = basetable->gpt_entries + 1;
275 	table->self.ent_start = 1;
276 	table->self.ent_size = table->self.ent_pmblkcnt;
277 	strcpy(table->self.ent_name, "Apple");
278 	strcpy(table->self.ent_type, APM_ENT_TYPE_SELF);
279 	return (0);
280 }
281 
282 static int
283 g_part_apm_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
284 {
285 
286 	/* Wipe the first 2 sectors to clear the partitioning. */
287 	basetable->gpt_smhead |= 3;
288 	return (0);
289 }
290 
291 static void
292 g_part_apm_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
293     struct sbuf *sb, const char *indent)
294 {
295 	union {
296 		char name[APM_ENT_NAMELEN + 1];
297 		char type[APM_ENT_TYPELEN + 1];
298 	} u;
299 	struct g_part_apm_entry *entry;
300 
301 	entry = (struct g_part_apm_entry *)baseentry;
302 	if (indent == NULL) {
303 		/* conftxt: libdisk compatibility */
304 		sbuf_printf(sb, " xs APPLE xt %s", entry->ent.ent_type);
305 	} else if (entry != NULL) {
306 		/* confxml: partition entry information */
307 		strncpy(u.name, entry->ent.ent_name, APM_ENT_NAMELEN);
308 		u.name[APM_ENT_NAMELEN] = '\0';
309 		sbuf_printf(sb, "%s<label>", indent);
310 		g_conf_cat_escaped(sb, u.name);
311 		sbuf_cat(sb, "</label>\n");
312 		strncpy(u.type, entry->ent.ent_type, APM_ENT_TYPELEN);
313 		u.type[APM_ENT_TYPELEN] = '\0';
314 		sbuf_printf(sb, "%s<rawtype>", indent);
315 		g_conf_cat_escaped(sb, u.type);
316 		sbuf_cat(sb, "</rawtype>\n");
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 	struct g_provider *pp;
363 
364 	if (baseentry == NULL) {
365 		pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
366 		basetable->gpt_last = MIN(pp->mediasize / pp->sectorsize,
367 		    UINT32_MAX) - 1;
368 		return (0);
369 	}
370 
371 	entry = (struct g_part_apm_entry *)baseentry;
372 	baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
373 	entry->ent.ent_size = gpp->gpp_size;
374 
375 	return (0);
376 }
377 
378 static const char *
379 g_part_apm_name(struct g_part_table *table, struct g_part_entry *baseentry,
380     char *buf, size_t bufsz)
381 {
382 
383 	snprintf(buf, bufsz, "s%d", baseentry->gpe_index + 1);
384 	return (buf);
385 }
386 
387 static int
388 g_part_apm_probe(struct g_part_table *basetable, struct g_consumer *cp)
389 {
390 	struct g_provider *pp;
391 	struct g_part_apm_table *table;
392 	char *buf;
393 	int error;
394 
395 	/* We don't nest, which means that our depth should be 0. */
396 	if (basetable->gpt_depth != 0)
397 		return (ENXIO);
398 
399 	table = (struct g_part_apm_table *)basetable;
400 	table->tivo_series1 = 0;
401 	pp = cp->provider;
402 
403 	/* Sanity-check the provider. */
404 	if (pp->mediasize < 4 * pp->sectorsize)
405 		return (ENOSPC);
406 
407 	/* Check that there's a Driver Descriptor Record (DDR). */
408 	buf = g_read_data(cp, 0L, pp->sectorsize, &error);
409 	if (buf == NULL)
410 		return (error);
411 	if (be16dec(buf) == APM_DDR_SIG) {
412 		/* Normal Apple DDR */
413 		table->ddr.ddr_sig = be16dec(buf);
414 		table->ddr.ddr_blksize = be16dec(buf + 2);
415 		table->ddr.ddr_blkcount = be32dec(buf + 4);
416 		g_free(buf);
417 		if (table->ddr.ddr_blksize != pp->sectorsize)
418 			return (ENXIO);
419 		if (table->ddr.ddr_blkcount > pp->mediasize / pp->sectorsize)
420 			return (ENXIO);
421 	} else {
422 		/*
423 		 * Check for Tivo drives, which have no DDR and a different
424 		 * signature.  Those whose first two bytes are 14 92 are
425 		 * Series 2 drives, and aren't supported.  Those that start
426 		 * with 92 14 are series 1 drives and are supported.
427 		 */
428 		if (be16dec(buf) != 0x9214) {
429 			/* If this is 0x1492 it could be a series 2 drive */
430 			g_free(buf);
431 			return (ENXIO);
432 		}
433 		table->ddr.ddr_sig = APM_DDR_SIG;		/* XXX */
434 		table->ddr.ddr_blksize = pp->sectorsize;	/* XXX */
435 		table->ddr.ddr_blkcount =
436 		    MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
437 		table->tivo_series1 = 1;
438 		g_free(buf);
439 	}
440 
441 	/* Check that there's a Partition Map. */
442 	error = apm_read_ent(cp, 1, &table->self, table->tivo_series1);
443 	if (error)
444 		return (error);
445 	if (table->self.ent_sig != APM_ENT_SIG)
446 		return (ENXIO);
447 	if (strcmp(table->self.ent_type, APM_ENT_TYPE_SELF))
448 		return (ENXIO);
449 	if (table->self.ent_pmblkcnt >= table->ddr.ddr_blkcount)
450 		return (ENXIO);
451 	return (G_PART_PROBE_PRI_NORM);
452 }
453 
454 static int
455 g_part_apm_read(struct g_part_table *basetable, struct g_consumer *cp)
456 {
457 	struct apm_ent ent;
458 	struct g_part_apm_entry *entry;
459 	struct g_part_apm_table *table;
460 	int error, index;
461 
462 	table = (struct g_part_apm_table *)basetable;
463 
464 	basetable->gpt_first = table->self.ent_size + 1;
465 	basetable->gpt_last = table->ddr.ddr_blkcount - 1;
466 	basetable->gpt_entries = table->self.ent_size - 1;
467 
468 	for (index = table->self.ent_pmblkcnt - 1; index > 0; index--) {
469 		error = apm_read_ent(cp, index + 1, &ent, table->tivo_series1);
470 		if (error)
471 			continue;
472 		if (!strcmp(ent.ent_type, APM_ENT_TYPE_UNUSED))
473 			continue;
474 		entry = (struct g_part_apm_entry *)g_part_new_entry(basetable,
475 		    index, ent.ent_start, ent.ent_start + ent.ent_size - 1);
476 		entry->ent = ent;
477 	}
478 
479 	return (0);
480 }
481 
482 static const char *
483 g_part_apm_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
484     char *buf, size_t bufsz)
485 {
486 	struct g_part_apm_entry *entry;
487 	const char *type;
488 	size_t len;
489 
490 	entry = (struct g_part_apm_entry *)baseentry;
491 	type = entry->ent.ent_type;
492 	if (!strcmp(type, APM_ENT_TYPE_APPLE_BOOT))
493 		return (g_part_alias_name(G_PART_ALIAS_APPLE_BOOT));
494 	if (!strcmp(type, APM_ENT_TYPE_APPLE_HFS))
495 		return (g_part_alias_name(G_PART_ALIAS_APPLE_HFS));
496 	if (!strcmp(type, APM_ENT_TYPE_APPLE_UFS))
497 		return (g_part_alias_name(G_PART_ALIAS_APPLE_UFS));
498 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD))
499 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD));
500 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD_NANDFS))
501 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_NANDFS));
502 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD_SWAP))
503 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
504 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD_UFS))
505 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
506 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD_VINUM))
507 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
508 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD_ZFS))
509 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
510 	buf[0] = '!';
511 	len = MIN(sizeof(entry->ent.ent_type), bufsz - 2);
512 	bcopy(type, buf + 1, len);
513 	buf[len + 1] = '\0';
514 	return (buf);
515 }
516 
517 static int
518 g_part_apm_write(struct g_part_table *basetable, struct g_consumer *cp)
519 {
520 	struct g_provider *pp;
521 	struct g_part_entry *baseentry;
522 	struct g_part_apm_entry *entry;
523 	struct g_part_apm_table *table;
524 	char *buf, *ptr;
525 	uint32_t index;
526 	int error;
527 	size_t tblsz;
528 
529 	pp = cp->provider;
530 	table = (struct g_part_apm_table *)basetable;
531 	/*
532 	 * Tivo Series 1 disk partitions are currently read-only.
533 	 */
534 	if (table->tivo_series1)
535 		return (EOPNOTSUPP);
536 
537 	/* Write the DDR only when we're newly created. */
538 	if (basetable->gpt_created) {
539 		buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
540 		be16enc(buf, table->ddr.ddr_sig);
541 		be16enc(buf + 2, table->ddr.ddr_blksize);
542 		be32enc(buf + 4, table->ddr.ddr_blkcount);
543 		error = g_write_data(cp, 0, buf, pp->sectorsize);
544 		g_free(buf);
545 		if (error)
546 			return (error);
547 	}
548 
549 	/* Allocate the buffer for all entries */
550 	tblsz = table->self.ent_pmblkcnt;
551 	buf = g_malloc(tblsz * pp->sectorsize, M_WAITOK | M_ZERO);
552 
553 	/* Fill the self entry */
554 	be16enc(buf, APM_ENT_SIG);
555 	be32enc(buf + 4, table->self.ent_pmblkcnt);
556 	be32enc(buf + 8, table->self.ent_start);
557 	be32enc(buf + 12, table->self.ent_size);
558 	bcopy(table->self.ent_name, buf + 16, sizeof(table->self.ent_name));
559 	bcopy(table->self.ent_type, buf + 48, sizeof(table->self.ent_type));
560 
561 	baseentry = LIST_FIRST(&basetable->gpt_entry);
562 	for (index = 1; index < tblsz; index++) {
563 		entry = (baseentry != NULL && index == baseentry->gpe_index)
564 		    ? (struct g_part_apm_entry *)baseentry : NULL;
565 		ptr = buf + index * pp->sectorsize;
566 		be16enc(ptr, APM_ENT_SIG);
567 		be32enc(ptr + 4, table->self.ent_pmblkcnt);
568 		if (entry != NULL && !baseentry->gpe_deleted) {
569 			be32enc(ptr + 8, entry->ent.ent_start);
570 			be32enc(ptr + 12, entry->ent.ent_size);
571 			bcopy(entry->ent.ent_name, ptr + 16,
572 			    sizeof(entry->ent.ent_name));
573 			bcopy(entry->ent.ent_type, ptr + 48,
574 			    sizeof(entry->ent.ent_type));
575 		} else {
576 			strcpy(ptr + 48, APM_ENT_TYPE_UNUSED);
577 		}
578 		if (entry != NULL)
579 			baseentry = LIST_NEXT(baseentry, gpe_entry);
580 	}
581 
582 	for (index = 0; index < tblsz; index += maxphys / pp->sectorsize) {
583 		error = g_write_data(cp, (1 + index) * pp->sectorsize,
584 		    buf + index * pp->sectorsize,
585 		    (tblsz - index > maxphys / pp->sectorsize) ? maxphys:
586 		    (tblsz - index) * pp->sectorsize);
587 		if (error) {
588 			g_free(buf);
589 			return (error);
590 		}
591 	}
592 	g_free(buf);
593 	return (0);
594 }
595