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