xref: /freebsd/sys/geom/part/g_part_apm.c (revision 09c999b1557a8031d2b60435d71a0a5ed4f0f016)
11d3aed33SMarcel Moolenaar /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
33728855aSPedro F. Giffuni  *
4ab1e8f04SMarcel Moolenaar  * Copyright (c) 2006-2008 Marcel Moolenaar
51d3aed33SMarcel Moolenaar  * All rights reserved.
61d3aed33SMarcel Moolenaar  *
71d3aed33SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
81d3aed33SMarcel Moolenaar  * modification, are permitted provided that the following conditions
91d3aed33SMarcel Moolenaar  * are met:
101d3aed33SMarcel Moolenaar  *
111d3aed33SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
121d3aed33SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
131d3aed33SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
141d3aed33SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
151d3aed33SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
161d3aed33SMarcel Moolenaar  *
171d3aed33SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
181d3aed33SMarcel Moolenaar  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
191d3aed33SMarcel Moolenaar  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
201d3aed33SMarcel Moolenaar  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
211d3aed33SMarcel Moolenaar  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
221d3aed33SMarcel Moolenaar  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231d3aed33SMarcel Moolenaar  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241d3aed33SMarcel Moolenaar  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251d3aed33SMarcel Moolenaar  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
261d3aed33SMarcel Moolenaar  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
271d3aed33SMarcel Moolenaar  */
281d3aed33SMarcel Moolenaar 
291d3aed33SMarcel Moolenaar #include <sys/param.h>
301d3aed33SMarcel Moolenaar #include <sys/apm.h>
311d3aed33SMarcel Moolenaar #include <sys/bio.h>
321d3aed33SMarcel Moolenaar #include <sys/endian.h>
331d3aed33SMarcel Moolenaar #include <sys/kernel.h>
341d3aed33SMarcel Moolenaar #include <sys/kobj.h>
351d3aed33SMarcel Moolenaar #include <sys/limits.h>
361d3aed33SMarcel Moolenaar #include <sys/lock.h>
371d3aed33SMarcel Moolenaar #include <sys/malloc.h>
381d3aed33SMarcel Moolenaar #include <sys/mutex.h>
391d3aed33SMarcel Moolenaar #include <sys/queue.h>
401d3aed33SMarcel Moolenaar #include <sys/sbuf.h>
411d3aed33SMarcel Moolenaar #include <sys/systm.h>
42cb08c2ccSAlexander Leidinger #include <sys/sysctl.h>
431d3aed33SMarcel Moolenaar #include <geom/geom.h>
4439dcac84SAndrey V. Elsukov #include <geom/geom_int.h>
451d3aed33SMarcel Moolenaar #include <geom/part/g_part.h>
461d3aed33SMarcel Moolenaar 
471d3aed33SMarcel Moolenaar #include "g_part_if.h"
481d3aed33SMarcel Moolenaar 
49cb08c2ccSAlexander Leidinger FEATURE(geom_part_apm, "GEOM partitioning class for Apple-style partitions");
50cb08c2ccSAlexander Leidinger 
511d3aed33SMarcel Moolenaar struct g_part_apm_table {
521d3aed33SMarcel Moolenaar 	struct g_part_table	base;
531d3aed33SMarcel Moolenaar 	struct apm_ddr		ddr;
541d3aed33SMarcel Moolenaar 	struct apm_ent		self;
550952268eSWarner Losh 	int			tivo_series1;
561d3aed33SMarcel Moolenaar };
571d3aed33SMarcel Moolenaar 
581d3aed33SMarcel Moolenaar struct g_part_apm_entry {
591d3aed33SMarcel Moolenaar 	struct g_part_entry	base;
601d3aed33SMarcel Moolenaar 	struct apm_ent		ent;
611d3aed33SMarcel Moolenaar };
621d3aed33SMarcel Moolenaar 
631d3aed33SMarcel Moolenaar static int g_part_apm_add(struct g_part_table *, struct g_part_entry *,
641d3aed33SMarcel Moolenaar     struct g_part_parms *);
651d3aed33SMarcel Moolenaar static int g_part_apm_create(struct g_part_table *, struct g_part_parms *);
661d3aed33SMarcel Moolenaar static int g_part_apm_destroy(struct g_part_table *, struct g_part_parms *);
6751f53a08SWarner Losh static void g_part_apm_dumpconf(struct g_part_table *, struct g_part_entry *,
685db67052SMarcel Moolenaar     struct sbuf *, const char *);
691d3aed33SMarcel Moolenaar static int g_part_apm_dumpto(struct g_part_table *, struct g_part_entry *);
701d3aed33SMarcel Moolenaar static int g_part_apm_modify(struct g_part_table *, struct g_part_entry *,
711d3aed33SMarcel Moolenaar     struct g_part_parms *);
7251f53a08SWarner Losh static const char *g_part_apm_name(struct g_part_table *, struct g_part_entry *,
731d3aed33SMarcel Moolenaar     char *, size_t);
741d3aed33SMarcel Moolenaar static int g_part_apm_probe(struct g_part_table *, struct g_consumer *);
751d3aed33SMarcel Moolenaar static int g_part_apm_read(struct g_part_table *, struct g_consumer *);
761d3aed33SMarcel Moolenaar static const char *g_part_apm_type(struct g_part_table *, struct g_part_entry *,
771d3aed33SMarcel Moolenaar     char *, size_t);
781d3aed33SMarcel Moolenaar static int g_part_apm_write(struct g_part_table *, struct g_consumer *);
793f71c319SMarcel Moolenaar static int g_part_apm_resize(struct g_part_table *, struct g_part_entry *,
803f71c319SMarcel Moolenaar     struct g_part_parms *);
811d3aed33SMarcel Moolenaar 
821d3aed33SMarcel Moolenaar static kobj_method_t g_part_apm_methods[] = {
831d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_add,		g_part_apm_add),
841d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_create,	g_part_apm_create),
851d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_destroy,	g_part_apm_destroy),
865db67052SMarcel Moolenaar 	KOBJMETHOD(g_part_dumpconf,	g_part_apm_dumpconf),
871d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_dumpto,	g_part_apm_dumpto),
881d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_modify,	g_part_apm_modify),
893f71c319SMarcel Moolenaar 	KOBJMETHOD(g_part_resize,	g_part_apm_resize),
901d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_name,		g_part_apm_name),
911d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_probe,	g_part_apm_probe),
921d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_read,		g_part_apm_read),
931d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_type,		g_part_apm_type),
941d3aed33SMarcel Moolenaar 	KOBJMETHOD(g_part_write,	g_part_apm_write),
951d3aed33SMarcel Moolenaar 	{ 0, 0 }
961d3aed33SMarcel Moolenaar };
971d3aed33SMarcel Moolenaar 
981d3aed33SMarcel Moolenaar static struct g_part_scheme g_part_apm_scheme = {
991d3aed33SMarcel Moolenaar 	"APM",
1001d3aed33SMarcel Moolenaar 	g_part_apm_methods,
1011d3aed33SMarcel Moolenaar 	sizeof(struct g_part_apm_table),
1021d3aed33SMarcel Moolenaar 	.gps_entrysz = sizeof(struct g_part_apm_entry),
1031d3aed33SMarcel Moolenaar 	.gps_minent = 16,
104*09c999b1SWarner Losh 	.gps_defent = 16,
105f44d97bdSAndrey V. Elsukov 	.gps_maxent = 4096,
1061d3aed33SMarcel Moolenaar };
1074ffca444SMarcel Moolenaar G_PART_SCHEME_DECLARE(g_part_apm);
10874d6c131SKyle Evans MODULE_VERSION(geom_part_apm, 0);
1091d3aed33SMarcel Moolenaar 
1100952268eSWarner Losh static void
swab(char * buf,size_t bufsz)1110952268eSWarner Losh swab(char *buf, size_t bufsz)
1120952268eSWarner Losh {
1130952268eSWarner Losh 	int i;
1140952268eSWarner Losh 	char ch;
1150952268eSWarner Losh 
1160952268eSWarner Losh 	for (i = 0; i < bufsz; i += 2) {
1170952268eSWarner Losh 		ch = buf[i];
1180952268eSWarner Losh 		buf[i] = buf[i + 1];
1190952268eSWarner Losh 		buf[i + 1] = ch;
1200952268eSWarner Losh 	}
1210952268eSWarner Losh }
1220952268eSWarner Losh 
1231d3aed33SMarcel Moolenaar static int
apm_parse_type(const char * type,char * buf,size_t bufsz)1241d3aed33SMarcel Moolenaar apm_parse_type(const char *type, char *buf, size_t bufsz)
1251d3aed33SMarcel Moolenaar {
126d287f590SMarcel Moolenaar 	const char *alias;
1271d3aed33SMarcel Moolenaar 
128d287f590SMarcel Moolenaar 	if (type[0] == '!') {
129d287f590SMarcel Moolenaar 		type++;
1301d3aed33SMarcel Moolenaar 		if (strlen(type) > bufsz)
1311d3aed33SMarcel Moolenaar 			return (EINVAL);
1321d3aed33SMarcel Moolenaar 		if (!strcmp(type, APM_ENT_TYPE_SELF) ||
1331d3aed33SMarcel Moolenaar 		    !strcmp(type, APM_ENT_TYPE_UNUSED))
1341d3aed33SMarcel Moolenaar 			return (EINVAL);
1351d3aed33SMarcel Moolenaar 		strncpy(buf, type, bufsz);
1361d3aed33SMarcel Moolenaar 		return (0);
1371d3aed33SMarcel Moolenaar 	}
1380532c3a5SNathan Whitehorn 	alias = g_part_alias_name(G_PART_ALIAS_APPLE_BOOT);
1390532c3a5SNathan Whitehorn 	if (!strcasecmp(type, alias)) {
1400532c3a5SNathan Whitehorn 		strcpy(buf, APM_ENT_TYPE_APPLE_BOOT);
1410532c3a5SNathan Whitehorn 		return (0);
1420532c3a5SNathan Whitehorn 	}
1430532c3a5SNathan Whitehorn 	alias = g_part_alias_name(G_PART_ALIAS_APPLE_HFS);
1440532c3a5SNathan Whitehorn 	if (!strcasecmp(type, alias)) {
1450532c3a5SNathan Whitehorn 		strcpy(buf, APM_ENT_TYPE_APPLE_HFS);
1460532c3a5SNathan Whitehorn 		return (0);
1470532c3a5SNathan Whitehorn 	}
1480532c3a5SNathan Whitehorn 	alias = g_part_alias_name(G_PART_ALIAS_APPLE_UFS);
1490532c3a5SNathan Whitehorn 	if (!strcasecmp(type, alias)) {
1500532c3a5SNathan Whitehorn 		strcpy(buf, APM_ENT_TYPE_APPLE_UFS);
1510532c3a5SNathan Whitehorn 		return (0);
1520532c3a5SNathan Whitehorn 	}
153d287f590SMarcel Moolenaar 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD);
154d287f590SMarcel Moolenaar 	if (!strcasecmp(type, alias)) {
1551d3aed33SMarcel Moolenaar 		strcpy(buf, APM_ENT_TYPE_FREEBSD);
1561d3aed33SMarcel Moolenaar 		return (0);
1571d3aed33SMarcel Moolenaar 	}
158f24a8224SMarcel Moolenaar 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_NANDFS);
159f24a8224SMarcel Moolenaar 	if (!strcasecmp(type, alias)) {
160f24a8224SMarcel Moolenaar 		strcpy(buf, APM_ENT_TYPE_FREEBSD_NANDFS);
161f24a8224SMarcel Moolenaar 		return (0);
162f24a8224SMarcel Moolenaar 	}
163d287f590SMarcel Moolenaar 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP);
164d287f590SMarcel Moolenaar 	if (!strcasecmp(type, alias)) {
165d287f590SMarcel Moolenaar 		strcpy(buf, APM_ENT_TYPE_FREEBSD_SWAP);
166d287f590SMarcel Moolenaar 		return (0);
167d287f590SMarcel Moolenaar 	}
168d287f590SMarcel Moolenaar 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS);
169d287f590SMarcel Moolenaar 	if (!strcasecmp(type, alias)) {
170d287f590SMarcel Moolenaar 		strcpy(buf, APM_ENT_TYPE_FREEBSD_UFS);
171d287f590SMarcel Moolenaar 		return (0);
172d287f590SMarcel Moolenaar 	}
173d287f590SMarcel Moolenaar 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM);
174d287f590SMarcel Moolenaar 	if (!strcasecmp(type, alias)) {
175d287f590SMarcel Moolenaar 		strcpy(buf, APM_ENT_TYPE_FREEBSD_VINUM);
176d287f590SMarcel Moolenaar 		return (0);
177d287f590SMarcel Moolenaar 	}
178a1fedf91SMarcel Moolenaar 	alias = g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS);
179a1fedf91SMarcel Moolenaar 	if (!strcasecmp(type, alias)) {
180a1fedf91SMarcel Moolenaar 		strcpy(buf, APM_ENT_TYPE_FREEBSD_ZFS);
181a1fedf91SMarcel Moolenaar 		return (0);
182a1fedf91SMarcel Moolenaar 	}
183d287f590SMarcel Moolenaar 	return (EINVAL);
184d287f590SMarcel Moolenaar }
1851d3aed33SMarcel Moolenaar 
1861d3aed33SMarcel Moolenaar static int
apm_read_ent(struct g_consumer * cp,uint32_t blk,struct apm_ent * ent,int tivo_series1)1870952268eSWarner Losh apm_read_ent(struct g_consumer *cp, uint32_t blk, struct apm_ent *ent,
1880952268eSWarner Losh     int tivo_series1)
1891d3aed33SMarcel Moolenaar {
1901d3aed33SMarcel Moolenaar 	struct g_provider *pp;
1911d3aed33SMarcel Moolenaar 	char *buf;
1921d3aed33SMarcel Moolenaar 	int error;
1931d3aed33SMarcel Moolenaar 
1941d3aed33SMarcel Moolenaar 	pp = cp->provider;
1951d3aed33SMarcel Moolenaar 	buf = g_read_data(cp, pp->sectorsize * blk, pp->sectorsize, &error);
1961d3aed33SMarcel Moolenaar 	if (buf == NULL)
1971d3aed33SMarcel Moolenaar 		return (error);
1980952268eSWarner Losh 	if (tivo_series1)
1990952268eSWarner Losh 		swab(buf, pp->sectorsize);
2001d3aed33SMarcel Moolenaar 	ent->ent_sig = be16dec(buf);
2011d3aed33SMarcel Moolenaar 	ent->ent_pmblkcnt = be32dec(buf + 4);
2021d3aed33SMarcel Moolenaar 	ent->ent_start = be32dec(buf + 8);
2031d3aed33SMarcel Moolenaar 	ent->ent_size = be32dec(buf + 12);
2041d3aed33SMarcel Moolenaar 	bcopy(buf + 16, ent->ent_name, sizeof(ent->ent_name));
2051d3aed33SMarcel Moolenaar 	bcopy(buf + 48, ent->ent_type, sizeof(ent->ent_type));
2061d3aed33SMarcel Moolenaar 	g_free(buf);
2071d3aed33SMarcel Moolenaar 	return (0);
2081d3aed33SMarcel Moolenaar }
2091d3aed33SMarcel Moolenaar 
2101d3aed33SMarcel Moolenaar static int
g_part_apm_add(struct g_part_table * basetable,struct g_part_entry * baseentry,struct g_part_parms * gpp)2111d3aed33SMarcel Moolenaar g_part_apm_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
2121d3aed33SMarcel Moolenaar     struct g_part_parms *gpp)
2131d3aed33SMarcel Moolenaar {
2141d3aed33SMarcel Moolenaar 	struct g_part_apm_entry *entry;
2151d3aed33SMarcel Moolenaar 	struct g_part_apm_table *table;
2161d3aed33SMarcel Moolenaar 	int error;
2171d3aed33SMarcel Moolenaar 
2181d3aed33SMarcel Moolenaar 	entry = (struct g_part_apm_entry *)baseentry;
2191d3aed33SMarcel Moolenaar 	table = (struct g_part_apm_table *)basetable;
2201d3aed33SMarcel Moolenaar 	entry->ent.ent_sig = APM_ENT_SIG;
2211d3aed33SMarcel Moolenaar 	entry->ent.ent_pmblkcnt = table->self.ent_pmblkcnt;
2221d3aed33SMarcel Moolenaar 	entry->ent.ent_start = gpp->gpp_start;
2231d3aed33SMarcel Moolenaar 	entry->ent.ent_size = gpp->gpp_size;
2241d3aed33SMarcel Moolenaar 	if (baseentry->gpe_deleted) {
2251d3aed33SMarcel Moolenaar 		bzero(entry->ent.ent_type, sizeof(entry->ent.ent_type));
2261d3aed33SMarcel Moolenaar 		bzero(entry->ent.ent_name, sizeof(entry->ent.ent_name));
2271d3aed33SMarcel Moolenaar 	}
2281d3aed33SMarcel Moolenaar 	error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type,
2291d3aed33SMarcel Moolenaar 	    sizeof(entry->ent.ent_type));
2301d3aed33SMarcel Moolenaar 	if (error)
2311d3aed33SMarcel Moolenaar 		return (error);
2321d3aed33SMarcel Moolenaar 	if (gpp->gpp_parms & G_PART_PARM_LABEL) {
2331d3aed33SMarcel Moolenaar 		if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name))
2341d3aed33SMarcel Moolenaar 			return (EINVAL);
2351d3aed33SMarcel Moolenaar 		strncpy(entry->ent.ent_name, gpp->gpp_label,
2361d3aed33SMarcel Moolenaar 		    sizeof(entry->ent.ent_name));
2371d3aed33SMarcel Moolenaar 	}
238a85a0d46SAndrey V. Elsukov 	if (baseentry->gpe_index >= table->self.ent_pmblkcnt)
239a85a0d46SAndrey V. Elsukov 		table->self.ent_pmblkcnt = baseentry->gpe_index + 1;
240a85a0d46SAndrey V. Elsukov 	KASSERT(table->self.ent_size >= table->self.ent_pmblkcnt,
241a85a0d46SAndrey V. Elsukov 	    ("%s", __func__));
242a85a0d46SAndrey V. Elsukov 	KASSERT(table->self.ent_size > baseentry->gpe_index,
243a85a0d46SAndrey V. Elsukov 	    ("%s", __func__));
2441d3aed33SMarcel Moolenaar 	return (0);
2451d3aed33SMarcel Moolenaar }
2461d3aed33SMarcel Moolenaar 
2471d3aed33SMarcel Moolenaar static int
g_part_apm_create(struct g_part_table * basetable,struct g_part_parms * gpp)2481d3aed33SMarcel Moolenaar g_part_apm_create(struct g_part_table *basetable, struct g_part_parms *gpp)
2491d3aed33SMarcel Moolenaar {
2501d3aed33SMarcel Moolenaar 	struct g_provider *pp;
2511d3aed33SMarcel Moolenaar 	struct g_part_apm_table *table;
252d01a198bSMarcel Moolenaar 	uint32_t last;
253d01a198bSMarcel Moolenaar 
254d01a198bSMarcel Moolenaar 	/* We don't nest, which means that our depth should be 0. */
255d01a198bSMarcel Moolenaar 	if (basetable->gpt_depth != 0)
256d01a198bSMarcel Moolenaar 		return (ENXIO);
2571d3aed33SMarcel Moolenaar 
2581d3aed33SMarcel Moolenaar 	table = (struct g_part_apm_table *)basetable;
2591d3aed33SMarcel Moolenaar 	pp = gpp->gpp_provider;
2601d3aed33SMarcel Moolenaar 	if (pp->sectorsize != 512 ||
2611d3aed33SMarcel Moolenaar 	    pp->mediasize < (2 + 2 * basetable->gpt_entries) * pp->sectorsize)
2621d3aed33SMarcel Moolenaar 		return (ENOSPC);
2631d3aed33SMarcel Moolenaar 
264d01a198bSMarcel Moolenaar 	/* APM uses 32-bit LBAs. */
2656017ae3fSAndrey V. Elsukov 	last = MIN(pp->mediasize / pp->sectorsize, UINT32_MAX) - 1;
266d01a198bSMarcel Moolenaar 
2671d3aed33SMarcel Moolenaar 	basetable->gpt_first = 2 + basetable->gpt_entries;
268d01a198bSMarcel Moolenaar 	basetable->gpt_last = last;
2691d3aed33SMarcel Moolenaar 
2701d3aed33SMarcel Moolenaar 	table->ddr.ddr_sig = APM_DDR_SIG;
2711d3aed33SMarcel Moolenaar 	table->ddr.ddr_blksize = pp->sectorsize;
272d01a198bSMarcel Moolenaar 	table->ddr.ddr_blkcount = last + 1;
2731d3aed33SMarcel Moolenaar 
2741d3aed33SMarcel Moolenaar 	table->self.ent_sig = APM_ENT_SIG;
2751d3aed33SMarcel Moolenaar 	table->self.ent_pmblkcnt = basetable->gpt_entries + 1;
2761d3aed33SMarcel Moolenaar 	table->self.ent_start = 1;
2771d3aed33SMarcel Moolenaar 	table->self.ent_size = table->self.ent_pmblkcnt;
2781d3aed33SMarcel Moolenaar 	strcpy(table->self.ent_name, "Apple");
2791d3aed33SMarcel Moolenaar 	strcpy(table->self.ent_type, APM_ENT_TYPE_SELF);
2801d3aed33SMarcel Moolenaar 	return (0);
2811d3aed33SMarcel Moolenaar }
2821d3aed33SMarcel Moolenaar 
2831d3aed33SMarcel Moolenaar static int
g_part_apm_destroy(struct g_part_table * basetable,struct g_part_parms * gpp)2841d3aed33SMarcel Moolenaar g_part_apm_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
2851d3aed33SMarcel Moolenaar {
2861d3aed33SMarcel Moolenaar 
2871d3aed33SMarcel Moolenaar 	/* Wipe the first 2 sectors to clear the partitioning. */
2881d3aed33SMarcel Moolenaar 	basetable->gpt_smhead |= 3;
2891d3aed33SMarcel Moolenaar 	return (0);
2901d3aed33SMarcel Moolenaar }
2911d3aed33SMarcel Moolenaar 
29251f53a08SWarner Losh static void
g_part_apm_dumpconf(struct g_part_table * table,struct g_part_entry * baseentry,struct sbuf * sb,const char * indent)2935db67052SMarcel Moolenaar g_part_apm_dumpconf(struct g_part_table *table, struct g_part_entry *baseentry,
2945db67052SMarcel Moolenaar     struct sbuf *sb, const char *indent)
2955db67052SMarcel Moolenaar {
296ab1e8f04SMarcel Moolenaar 	union {
297ab1e8f04SMarcel Moolenaar 		char name[APM_ENT_NAMELEN + 1];
298ab1e8f04SMarcel Moolenaar 		char type[APM_ENT_TYPELEN + 1];
299ab1e8f04SMarcel Moolenaar 	} u;
3005db67052SMarcel Moolenaar 	struct g_part_apm_entry *entry;
3015db67052SMarcel Moolenaar 
3025db67052SMarcel Moolenaar 	entry = (struct g_part_apm_entry *)baseentry;
303ab1e8f04SMarcel Moolenaar 	if (indent == NULL) {
304ab1e8f04SMarcel Moolenaar 		/* conftxt: libdisk compatibility */
3055db67052SMarcel Moolenaar 		sbuf_printf(sb, " xs APPLE xt %s", entry->ent.ent_type);
306ab1e8f04SMarcel Moolenaar 	} else if (entry != NULL) {
307ab1e8f04SMarcel Moolenaar 		/* confxml: partition entry information */
308ab1e8f04SMarcel Moolenaar 		strncpy(u.name, entry->ent.ent_name, APM_ENT_NAMELEN);
309ab1e8f04SMarcel Moolenaar 		u.name[APM_ENT_NAMELEN] = '\0';
31039dcac84SAndrey V. Elsukov 		sbuf_printf(sb, "%s<label>", indent);
31149ee0fceSAlexander Motin 		g_conf_cat_escaped(sb, u.name);
31249ee0fceSAlexander Motin 		sbuf_cat(sb, "</label>\n");
313ab1e8f04SMarcel Moolenaar 		strncpy(u.type, entry->ent.ent_type, APM_ENT_TYPELEN);
314ab1e8f04SMarcel Moolenaar 		u.type[APM_ENT_TYPELEN] = '\0';
31539dcac84SAndrey V. Elsukov 		sbuf_printf(sb, "%s<rawtype>", indent);
31649ee0fceSAlexander Motin 		g_conf_cat_escaped(sb, u.type);
31749ee0fceSAlexander Motin 		sbuf_cat(sb, "</rawtype>\n");
318ab1e8f04SMarcel Moolenaar 	} else {
319ab1e8f04SMarcel Moolenaar 		/* confxml: scheme information */
320ab1e8f04SMarcel Moolenaar 	}
3215db67052SMarcel Moolenaar }
3225db67052SMarcel Moolenaar 
3235db67052SMarcel Moolenaar static int
g_part_apm_dumpto(struct g_part_table * table,struct g_part_entry * baseentry)3241d3aed33SMarcel Moolenaar g_part_apm_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
3251d3aed33SMarcel Moolenaar {
3261d3aed33SMarcel Moolenaar 	struct g_part_apm_entry *entry;
3271d3aed33SMarcel Moolenaar 
3281d3aed33SMarcel Moolenaar 	entry = (struct g_part_apm_entry *)baseentry;
3291d3aed33SMarcel Moolenaar 	return ((!strcmp(entry->ent.ent_type, APM_ENT_TYPE_FREEBSD_SWAP))
3301d3aed33SMarcel Moolenaar 	    ? 1 : 0);
3311d3aed33SMarcel Moolenaar }
3321d3aed33SMarcel Moolenaar 
3331d3aed33SMarcel Moolenaar static int
g_part_apm_modify(struct g_part_table * basetable,struct g_part_entry * baseentry,struct g_part_parms * gpp)3341d3aed33SMarcel Moolenaar g_part_apm_modify(struct g_part_table *basetable,
3351d3aed33SMarcel Moolenaar     struct g_part_entry *baseentry, struct g_part_parms *gpp)
3361d3aed33SMarcel Moolenaar {
3371d3aed33SMarcel Moolenaar 	struct g_part_apm_entry *entry;
3381d3aed33SMarcel Moolenaar 	int error;
3391d3aed33SMarcel Moolenaar 
3401d3aed33SMarcel Moolenaar 	entry = (struct g_part_apm_entry *)baseentry;
3411d3aed33SMarcel Moolenaar 	if (gpp->gpp_parms & G_PART_PARM_LABEL) {
3421d3aed33SMarcel Moolenaar 		if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name))
3431d3aed33SMarcel Moolenaar 			return (EINVAL);
3441d3aed33SMarcel Moolenaar 	}
3451d3aed33SMarcel Moolenaar 	if (gpp->gpp_parms & G_PART_PARM_TYPE) {
3461d3aed33SMarcel Moolenaar 		error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type,
3471d3aed33SMarcel Moolenaar 		    sizeof(entry->ent.ent_type));
3481d3aed33SMarcel Moolenaar 		if (error)
3491d3aed33SMarcel Moolenaar 			return (error);
3501d3aed33SMarcel Moolenaar 	}
3511d3aed33SMarcel Moolenaar 	if (gpp->gpp_parms & G_PART_PARM_LABEL) {
3521d3aed33SMarcel Moolenaar 		strncpy(entry->ent.ent_name, gpp->gpp_label,
3531d3aed33SMarcel Moolenaar 		    sizeof(entry->ent.ent_name));
3541d3aed33SMarcel Moolenaar 	}
3551d3aed33SMarcel Moolenaar 	return (0);
3561d3aed33SMarcel Moolenaar }
3571d3aed33SMarcel Moolenaar 
3583f71c319SMarcel Moolenaar static int
g_part_apm_resize(struct g_part_table * basetable,struct g_part_entry * baseentry,struct g_part_parms * gpp)3593f71c319SMarcel Moolenaar g_part_apm_resize(struct g_part_table *basetable,
3603f71c319SMarcel Moolenaar     struct g_part_entry *baseentry, struct g_part_parms *gpp)
3613f71c319SMarcel Moolenaar {
3623f71c319SMarcel Moolenaar 	struct g_part_apm_entry *entry;
363884c8e4fSAndrey V. Elsukov 	struct g_provider *pp;
364884c8e4fSAndrey V. Elsukov 
365884c8e4fSAndrey V. Elsukov 	if (baseentry == NULL) {
366884c8e4fSAndrey V. Elsukov 		pp = LIST_FIRST(&basetable->gpt_gp->consumer)->provider;
367884c8e4fSAndrey V. Elsukov 		basetable->gpt_last = MIN(pp->mediasize / pp->sectorsize,
368884c8e4fSAndrey V. Elsukov 		    UINT32_MAX) - 1;
369884c8e4fSAndrey V. Elsukov 		return (0);
370884c8e4fSAndrey V. Elsukov 	}
3713f71c319SMarcel Moolenaar 
3723f71c319SMarcel Moolenaar 	entry = (struct g_part_apm_entry *)baseentry;
3733f71c319SMarcel Moolenaar 	baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
3743f71c319SMarcel Moolenaar 	entry->ent.ent_size = gpp->gpp_size;
3753f71c319SMarcel Moolenaar 
3763f71c319SMarcel Moolenaar 	return (0);
3773f71c319SMarcel Moolenaar }
3783f71c319SMarcel Moolenaar 
37951f53a08SWarner Losh static const char *
g_part_apm_name(struct g_part_table * table,struct g_part_entry * baseentry,char * buf,size_t bufsz)3801d3aed33SMarcel Moolenaar g_part_apm_name(struct g_part_table *table, struct g_part_entry *baseentry,
3811d3aed33SMarcel Moolenaar     char *buf, size_t bufsz)
3821d3aed33SMarcel Moolenaar {
3831d3aed33SMarcel Moolenaar 
3841d3aed33SMarcel Moolenaar 	snprintf(buf, bufsz, "s%d", baseentry->gpe_index + 1);
3851d3aed33SMarcel Moolenaar 	return (buf);
3861d3aed33SMarcel Moolenaar }
3871d3aed33SMarcel Moolenaar 
3881d3aed33SMarcel Moolenaar static int
g_part_apm_probe(struct g_part_table * basetable,struct g_consumer * cp)3891d3aed33SMarcel Moolenaar g_part_apm_probe(struct g_part_table *basetable, struct g_consumer *cp)
3901d3aed33SMarcel Moolenaar {
3911d3aed33SMarcel Moolenaar 	struct g_provider *pp;
3921d3aed33SMarcel Moolenaar 	struct g_part_apm_table *table;
3931d3aed33SMarcel Moolenaar 	char *buf;
3941d3aed33SMarcel Moolenaar 	int error;
3951d3aed33SMarcel Moolenaar 
3961d3aed33SMarcel Moolenaar 	/* We don't nest, which means that our depth should be 0. */
3971d3aed33SMarcel Moolenaar 	if (basetable->gpt_depth != 0)
3981d3aed33SMarcel Moolenaar 		return (ENXIO);
3991d3aed33SMarcel Moolenaar 
4001d3aed33SMarcel Moolenaar 	table = (struct g_part_apm_table *)basetable;
4010952268eSWarner Losh 	table->tivo_series1 = 0;
4021d3aed33SMarcel Moolenaar 	pp = cp->provider;
4031d3aed33SMarcel Moolenaar 
4041d3aed33SMarcel Moolenaar 	/* Sanity-check the provider. */
4051d3aed33SMarcel Moolenaar 	if (pp->mediasize < 4 * pp->sectorsize)
4061d3aed33SMarcel Moolenaar 		return (ENOSPC);
4071d3aed33SMarcel Moolenaar 
4081d3aed33SMarcel Moolenaar 	/* Check that there's a Driver Descriptor Record (DDR). */
4091d3aed33SMarcel Moolenaar 	buf = g_read_data(cp, 0L, pp->sectorsize, &error);
4101d3aed33SMarcel Moolenaar 	if (buf == NULL)
4111d3aed33SMarcel Moolenaar 		return (error);
41247bae5fdSGavin Atkinson 	if (be16dec(buf) == APM_DDR_SIG) {
4130952268eSWarner Losh 		/* Normal Apple DDR */
4141d3aed33SMarcel Moolenaar 		table->ddr.ddr_sig = be16dec(buf);
4151d3aed33SMarcel Moolenaar 		table->ddr.ddr_blksize = be16dec(buf + 2);
4161d3aed33SMarcel Moolenaar 		table->ddr.ddr_blkcount = be32dec(buf + 4);
4171d3aed33SMarcel Moolenaar 		g_free(buf);
4181d3aed33SMarcel Moolenaar 		if (table->ddr.ddr_blksize != pp->sectorsize)
4191d3aed33SMarcel Moolenaar 			return (ENXIO);
4206017ae3fSAndrey V. Elsukov 		if (table->ddr.ddr_blkcount > pp->mediasize / pp->sectorsize)
4216017ae3fSAndrey V. Elsukov 			return (ENXIO);
4220952268eSWarner Losh 	} else {
4230952268eSWarner Losh 		/*
4240952268eSWarner Losh 		 * Check for Tivo drives, which have no DDR and a different
4250952268eSWarner Losh 		 * signature.  Those whose first two bytes are 14 92 are
4260952268eSWarner Losh 		 * Series 2 drives, and aren't supported.  Those that start
4270952268eSWarner Losh 		 * with 92 14 are series 1 drives and are supported.
4280952268eSWarner Losh 		 */
4290952268eSWarner Losh 		if (be16dec(buf) != 0x9214) {
4300952268eSWarner Losh 			/* If this is 0x1492 it could be a series 2 drive */
4310952268eSWarner Losh 			g_free(buf);
4320952268eSWarner Losh 			return (ENXIO);
4330952268eSWarner Losh 		}
4340952268eSWarner Losh 		table->ddr.ddr_sig = APM_DDR_SIG;		/* XXX */
4350952268eSWarner Losh 		table->ddr.ddr_blksize = pp->sectorsize;	/* XXX */
4366017ae3fSAndrey V. Elsukov 		table->ddr.ddr_blkcount =
4376017ae3fSAndrey V. Elsukov 		    MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
4380952268eSWarner Losh 		table->tivo_series1 = 1;
4390952268eSWarner Losh 		g_free(buf);
4400952268eSWarner Losh 	}
4411d3aed33SMarcel Moolenaar 
4421d3aed33SMarcel Moolenaar 	/* Check that there's a Partition Map. */
4430952268eSWarner Losh 	error = apm_read_ent(cp, 1, &table->self, table->tivo_series1);
4441d3aed33SMarcel Moolenaar 	if (error)
4451d3aed33SMarcel Moolenaar 		return (error);
4461d3aed33SMarcel Moolenaar 	if (table->self.ent_sig != APM_ENT_SIG)
4471d3aed33SMarcel Moolenaar 		return (ENXIO);
4481d3aed33SMarcel Moolenaar 	if (strcmp(table->self.ent_type, APM_ENT_TYPE_SELF))
4491d3aed33SMarcel Moolenaar 		return (ENXIO);
4501d3aed33SMarcel Moolenaar 	if (table->self.ent_pmblkcnt >= table->ddr.ddr_blkcount)
4511d3aed33SMarcel Moolenaar 		return (ENXIO);
4521d3aed33SMarcel Moolenaar 	return (G_PART_PROBE_PRI_NORM);
4531d3aed33SMarcel Moolenaar }
4541d3aed33SMarcel Moolenaar 
4551d3aed33SMarcel Moolenaar static int
g_part_apm_read(struct g_part_table * basetable,struct g_consumer * cp)4561d3aed33SMarcel Moolenaar g_part_apm_read(struct g_part_table *basetable, struct g_consumer *cp)
4571d3aed33SMarcel Moolenaar {
4581d3aed33SMarcel Moolenaar 	struct apm_ent ent;
4591d3aed33SMarcel Moolenaar 	struct g_part_apm_entry *entry;
4601d3aed33SMarcel Moolenaar 	struct g_part_apm_table *table;
4611d3aed33SMarcel Moolenaar 	int error, index;
4621d3aed33SMarcel Moolenaar 
4631d3aed33SMarcel Moolenaar 	table = (struct g_part_apm_table *)basetable;
4641d3aed33SMarcel Moolenaar 
46564c4a837SAndrey V. Elsukov 	basetable->gpt_first = table->self.ent_size + 1;
4661d3aed33SMarcel Moolenaar 	basetable->gpt_last = table->ddr.ddr_blkcount - 1;
467a85a0d46SAndrey V. Elsukov 	basetable->gpt_entries = table->self.ent_size - 1;
4681d3aed33SMarcel Moolenaar 
4691d3aed33SMarcel Moolenaar 	for (index = table->self.ent_pmblkcnt - 1; index > 0; index--) {
4700952268eSWarner Losh 		error = apm_read_ent(cp, index + 1, &ent, table->tivo_series1);
4711d3aed33SMarcel Moolenaar 		if (error)
4721d3aed33SMarcel Moolenaar 			continue;
4731d3aed33SMarcel Moolenaar 		if (!strcmp(ent.ent_type, APM_ENT_TYPE_UNUSED))
4741d3aed33SMarcel Moolenaar 			continue;
4751d3aed33SMarcel Moolenaar 		entry = (struct g_part_apm_entry *)g_part_new_entry(basetable,
4761d3aed33SMarcel Moolenaar 		    index, ent.ent_start, ent.ent_start + ent.ent_size - 1);
4771d3aed33SMarcel Moolenaar 		entry->ent = ent;
4781d3aed33SMarcel Moolenaar 	}
4791d3aed33SMarcel Moolenaar 
4801d3aed33SMarcel Moolenaar 	return (0);
4811d3aed33SMarcel Moolenaar }
4821d3aed33SMarcel Moolenaar 
4831d3aed33SMarcel Moolenaar static const char *
g_part_apm_type(struct g_part_table * basetable,struct g_part_entry * baseentry,char * buf,size_t bufsz)4841d3aed33SMarcel Moolenaar g_part_apm_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
4851d3aed33SMarcel Moolenaar     char *buf, size_t bufsz)
4861d3aed33SMarcel Moolenaar {
4871d3aed33SMarcel Moolenaar 	struct g_part_apm_entry *entry;
4881d3aed33SMarcel Moolenaar 	const char *type;
4891d3aed33SMarcel Moolenaar 	size_t len;
4901d3aed33SMarcel Moolenaar 
4911d3aed33SMarcel Moolenaar 	entry = (struct g_part_apm_entry *)baseentry;
4921d3aed33SMarcel Moolenaar 	type = entry->ent.ent_type;
4930532c3a5SNathan Whitehorn 	if (!strcmp(type, APM_ENT_TYPE_APPLE_BOOT))
4940532c3a5SNathan Whitehorn 		return (g_part_alias_name(G_PART_ALIAS_APPLE_BOOT));
4950532c3a5SNathan Whitehorn 	if (!strcmp(type, APM_ENT_TYPE_APPLE_HFS))
4960532c3a5SNathan Whitehorn 		return (g_part_alias_name(G_PART_ALIAS_APPLE_HFS));
4970532c3a5SNathan Whitehorn 	if (!strcmp(type, APM_ENT_TYPE_APPLE_UFS))
4980532c3a5SNathan Whitehorn 		return (g_part_alias_name(G_PART_ALIAS_APPLE_UFS));
4991d3aed33SMarcel Moolenaar 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD))
5001d3aed33SMarcel Moolenaar 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD));
501f24a8224SMarcel Moolenaar 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD_NANDFS))
502f24a8224SMarcel Moolenaar 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_NANDFS));
5031d3aed33SMarcel Moolenaar 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD_SWAP))
5041d3aed33SMarcel Moolenaar 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
5051d3aed33SMarcel Moolenaar 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD_UFS))
5061d3aed33SMarcel Moolenaar 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
5071d3aed33SMarcel Moolenaar 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD_VINUM))
5081d3aed33SMarcel Moolenaar 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
509a1fedf91SMarcel Moolenaar 	if (!strcmp(type, APM_ENT_TYPE_FREEBSD_ZFS))
510a1fedf91SMarcel Moolenaar 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
511cf231470SMarcel Moolenaar 	buf[0] = '!';
512cf231470SMarcel Moolenaar 	len = MIN(sizeof(entry->ent.ent_type), bufsz - 2);
513cf231470SMarcel Moolenaar 	bcopy(type, buf + 1, len);
514cf231470SMarcel Moolenaar 	buf[len + 1] = '\0';
5151d3aed33SMarcel Moolenaar 	return (buf);
5161d3aed33SMarcel Moolenaar }
5171d3aed33SMarcel Moolenaar 
5181d3aed33SMarcel Moolenaar static int
g_part_apm_write(struct g_part_table * basetable,struct g_consumer * cp)5191d3aed33SMarcel Moolenaar g_part_apm_write(struct g_part_table *basetable, struct g_consumer *cp)
5201d3aed33SMarcel Moolenaar {
521a85a0d46SAndrey V. Elsukov 	struct g_provider *pp;
5221d3aed33SMarcel Moolenaar 	struct g_part_entry *baseentry;
5231d3aed33SMarcel Moolenaar 	struct g_part_apm_entry *entry;
5241d3aed33SMarcel Moolenaar 	struct g_part_apm_table *table;
525a85a0d46SAndrey V. Elsukov 	char *buf, *ptr;
526a85a0d46SAndrey V. Elsukov 	uint32_t index;
527a85a0d46SAndrey V. Elsukov 	int error;
528a85a0d46SAndrey V. Elsukov 	size_t tblsz;
5291d3aed33SMarcel Moolenaar 
530a85a0d46SAndrey V. Elsukov 	pp = cp->provider;
5311d3aed33SMarcel Moolenaar 	table = (struct g_part_apm_table *)basetable;
5320952268eSWarner Losh 	/*
5330952268eSWarner Losh 	 * Tivo Series 1 disk partitions are currently read-only.
5340952268eSWarner Losh 	 */
5350952268eSWarner Losh 	if (table->tivo_series1)
5360952268eSWarner Losh 		return (EOPNOTSUPP);
5371d3aed33SMarcel Moolenaar 
538a85a0d46SAndrey V. Elsukov 	/* Write the DDR only when we're newly created. */
5391d3aed33SMarcel Moolenaar 	if (basetable->gpt_created) {
540a85a0d46SAndrey V. Elsukov 		buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
5411d3aed33SMarcel Moolenaar 		be16enc(buf, table->ddr.ddr_sig);
5421d3aed33SMarcel Moolenaar 		be16enc(buf + 2, table->ddr.ddr_blksize);
5431d3aed33SMarcel Moolenaar 		be32enc(buf + 4, table->ddr.ddr_blkcount);
544a85a0d46SAndrey V. Elsukov 		error = g_write_data(cp, 0, buf, pp->sectorsize);
545a85a0d46SAndrey V. Elsukov 		g_free(buf);
5461d3aed33SMarcel Moolenaar 		if (error)
5471d3aed33SMarcel Moolenaar 			return (error);
5481d3aed33SMarcel Moolenaar 	}
5491d3aed33SMarcel Moolenaar 
550a85a0d46SAndrey V. Elsukov 	/* Allocate the buffer for all entries */
551a85a0d46SAndrey V. Elsukov 	tblsz = table->self.ent_pmblkcnt;
552a85a0d46SAndrey V. Elsukov 	buf = g_malloc(tblsz * pp->sectorsize, M_WAITOK | M_ZERO);
5531d3aed33SMarcel Moolenaar 
554a85a0d46SAndrey V. Elsukov 	/* Fill the self entry */
555a85a0d46SAndrey V. Elsukov 	be16enc(buf, APM_ENT_SIG);
556a85a0d46SAndrey V. Elsukov 	be32enc(buf + 4, table->self.ent_pmblkcnt);
5571d3aed33SMarcel Moolenaar 	be32enc(buf + 8, table->self.ent_start);
5581d3aed33SMarcel Moolenaar 	be32enc(buf + 12, table->self.ent_size);
559a85a0d46SAndrey V. Elsukov 	bcopy(table->self.ent_name, buf + 16, sizeof(table->self.ent_name));
560a85a0d46SAndrey V. Elsukov 	bcopy(table->self.ent_type, buf + 48, sizeof(table->self.ent_type));
5611d3aed33SMarcel Moolenaar 
5621d3aed33SMarcel Moolenaar 	baseentry = LIST_FIRST(&basetable->gpt_entry);
563a85a0d46SAndrey V. Elsukov 	for (index = 1; index < tblsz; index++) {
564d287f590SMarcel Moolenaar 		entry = (baseentry != NULL && index == baseentry->gpe_index)
565d287f590SMarcel Moolenaar 		    ? (struct g_part_apm_entry *)baseentry : NULL;
566a85a0d46SAndrey V. Elsukov 		ptr = buf + index * pp->sectorsize;
567a85a0d46SAndrey V. Elsukov 		be16enc(ptr, APM_ENT_SIG);
568a85a0d46SAndrey V. Elsukov 		be32enc(ptr + 4, table->self.ent_pmblkcnt);
569d287f590SMarcel Moolenaar 		if (entry != NULL && !baseentry->gpe_deleted) {
570a85a0d46SAndrey V. Elsukov 			be32enc(ptr + 8, entry->ent.ent_start);
571a85a0d46SAndrey V. Elsukov 			be32enc(ptr + 12, entry->ent.ent_size);
572a85a0d46SAndrey V. Elsukov 			bcopy(entry->ent.ent_name, ptr + 16,
5731d3aed33SMarcel Moolenaar 			    sizeof(entry->ent.ent_name));
574a85a0d46SAndrey V. Elsukov 			bcopy(entry->ent.ent_type, ptr + 48,
5751d3aed33SMarcel Moolenaar 			    sizeof(entry->ent.ent_type));
5761d3aed33SMarcel Moolenaar 		} else {
577a85a0d46SAndrey V. Elsukov 			strcpy(ptr + 48, APM_ENT_TYPE_UNUSED);
5781d3aed33SMarcel Moolenaar 		}
579d287f590SMarcel Moolenaar 		if (entry != NULL)
580d287f590SMarcel Moolenaar 			baseentry = LIST_NEXT(baseentry, gpe_entry);
5811d3aed33SMarcel Moolenaar 	}
5821d3aed33SMarcel Moolenaar 
583cd853791SKonstantin Belousov 	for (index = 0; index < tblsz; index += maxphys / pp->sectorsize) {
584a85a0d46SAndrey V. Elsukov 		error = g_write_data(cp, (1 + index) * pp->sectorsize,
585a85a0d46SAndrey V. Elsukov 		    buf + index * pp->sectorsize,
586cd853791SKonstantin Belousov 		    (tblsz - index > maxphys / pp->sectorsize) ? maxphys:
587a85a0d46SAndrey V. Elsukov 		    (tblsz - index) * pp->sectorsize);
588a85a0d46SAndrey V. Elsukov 		if (error) {
589a85a0d46SAndrey V. Elsukov 			g_free(buf);
590a85a0d46SAndrey V. Elsukov 			return (error);
591a85a0d46SAndrey V. Elsukov 		}
592a85a0d46SAndrey V. Elsukov 	}
593a85a0d46SAndrey V. Elsukov 	g_free(buf);
5941d3aed33SMarcel Moolenaar 	return (0);
5951d3aed33SMarcel Moolenaar }
596