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