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