]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/geom/part/g_part_apm.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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/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>%s</label>\n", indent, u.name);
315                 strncpy(u.type, entry->ent.ent_type, APM_ENT_TYPELEN);
316                 u.type[APM_ENT_TYPELEN] = '\0';
317                 sbuf_printf(sb, "%s<rawtype>%s</rawtype>\n", indent, u.type);
318         } else {
319                 /* confxml: scheme information */
320         }
321 }
322
323 static int
324 g_part_apm_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
325 {
326         struct g_part_apm_entry *entry;
327
328         entry = (struct g_part_apm_entry *)baseentry;
329         return ((!strcmp(entry->ent.ent_type, APM_ENT_TYPE_FREEBSD_SWAP))
330             ? 1 : 0);
331 }
332
333 static int
334 g_part_apm_modify(struct g_part_table *basetable,
335     struct g_part_entry *baseentry, struct g_part_parms *gpp)
336 {
337         struct g_part_apm_entry *entry;
338         int error;
339
340         entry = (struct g_part_apm_entry *)baseentry;
341         if (gpp->gpp_parms & G_PART_PARM_LABEL) {
342                 if (strlen(gpp->gpp_label) > sizeof(entry->ent.ent_name))
343                         return (EINVAL);
344         }
345         if (gpp->gpp_parms & G_PART_PARM_TYPE) {
346                 error = apm_parse_type(gpp->gpp_type, entry->ent.ent_type,
347                     sizeof(entry->ent.ent_type));
348                 if (error)
349                         return (error);
350         }
351         if (gpp->gpp_parms & G_PART_PARM_LABEL) {
352                 strncpy(entry->ent.ent_name, gpp->gpp_label,
353                     sizeof(entry->ent.ent_name));
354         }
355         return (0);
356 }
357
358 static int
359 g_part_apm_resize(struct g_part_table *basetable,
360     struct g_part_entry *baseentry, struct g_part_parms *gpp)
361 {
362         struct g_part_apm_entry *entry;
363
364         entry = (struct g_part_apm_entry *)baseentry;
365         baseentry->gpe_end = baseentry->gpe_start + gpp->gpp_size - 1;
366         entry->ent.ent_size = gpp->gpp_size;
367
368         return (0);
369 }
370
371 static const char *
372 g_part_apm_name(struct g_part_table *table, struct g_part_entry *baseentry,
373     char *buf, size_t bufsz)
374 {
375
376         snprintf(buf, bufsz, "s%d", baseentry->gpe_index + 1);
377         return (buf);
378 }
379
380 static int
381 g_part_apm_probe(struct g_part_table *basetable, struct g_consumer *cp)
382 {
383         struct g_provider *pp;
384         struct g_part_apm_table *table;
385         char *buf;
386         int error;
387
388         /* We don't nest, which means that our depth should be 0. */
389         if (basetable->gpt_depth != 0)
390                 return (ENXIO);
391
392         table = (struct g_part_apm_table *)basetable;
393         table->tivo_series1 = 0;
394         pp = cp->provider;
395
396         /* Sanity-check the provider. */
397         if (pp->mediasize < 4 * pp->sectorsize)
398                 return (ENOSPC);
399
400         /* Check that there's a Driver Descriptor Record (DDR). */
401         buf = g_read_data(cp, 0L, pp->sectorsize, &error);
402         if (buf == NULL)
403                 return (error);
404         if (be16dec(buf) == APM_DDR_SIG) {
405                 /* Normal Apple DDR */
406                 table->ddr.ddr_sig = be16dec(buf);
407                 table->ddr.ddr_blksize = be16dec(buf + 2);
408                 table->ddr.ddr_blkcount = be32dec(buf + 4);
409                 g_free(buf);
410                 if (table->ddr.ddr_blksize != pp->sectorsize)
411                         return (ENXIO);
412                 if (table->ddr.ddr_blkcount > pp->mediasize / pp->sectorsize)
413                         return (ENXIO);
414         } else {
415                 /*
416                  * Check for Tivo drives, which have no DDR and a different
417                  * signature.  Those whose first two bytes are 14 92 are
418                  * Series 2 drives, and aren't supported.  Those that start
419                  * with 92 14 are series 1 drives and are supported.
420                  */
421                 if (be16dec(buf) != 0x9214) {
422                         /* If this is 0x1492 it could be a series 2 drive */
423                         g_free(buf);
424                         return (ENXIO);
425                 }
426                 table->ddr.ddr_sig = APM_DDR_SIG;               /* XXX */
427                 table->ddr.ddr_blksize = pp->sectorsize;        /* XXX */
428                 table->ddr.ddr_blkcount =
429                     MIN(pp->mediasize / pp->sectorsize, UINT32_MAX);
430                 table->tivo_series1 = 1;
431                 g_free(buf);
432         }
433
434         /* Check that there's a Partition Map. */
435         error = apm_read_ent(cp, 1, &table->self, table->tivo_series1);
436         if (error)
437                 return (error);
438         if (table->self.ent_sig != APM_ENT_SIG)
439                 return (ENXIO);
440         if (strcmp(table->self.ent_type, APM_ENT_TYPE_SELF))
441                 return (ENXIO);
442         if (table->self.ent_pmblkcnt >= table->ddr.ddr_blkcount)
443                 return (ENXIO);
444         return (G_PART_PROBE_PRI_NORM);
445 }
446
447 static int
448 g_part_apm_read(struct g_part_table *basetable, struct g_consumer *cp)
449 {
450         struct apm_ent ent;
451         struct g_part_apm_entry *entry;
452         struct g_part_apm_table *table;
453         int error, index;
454
455         table = (struct g_part_apm_table *)basetable;
456
457         basetable->gpt_first = table->self.ent_size + 1;
458         basetable->gpt_last = table->ddr.ddr_blkcount - 1;
459         basetable->gpt_entries = table->self.ent_size - 1;
460
461         for (index = table->self.ent_pmblkcnt - 1; index > 0; index--) {
462                 error = apm_read_ent(cp, index + 1, &ent, table->tivo_series1);
463                 if (error)
464                         continue;
465                 if (!strcmp(ent.ent_type, APM_ENT_TYPE_UNUSED))
466                         continue;
467                 entry = (struct g_part_apm_entry *)g_part_new_entry(basetable,
468                     index, ent.ent_start, ent.ent_start + ent.ent_size - 1);
469                 entry->ent = ent;
470         }
471
472         return (0);
473 }
474
475 static const char *
476 g_part_apm_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
477     char *buf, size_t bufsz)
478 {
479         struct g_part_apm_entry *entry;
480         const char *type;
481         size_t len;
482
483         entry = (struct g_part_apm_entry *)baseentry;
484         type = entry->ent.ent_type;
485         if (!strcmp(type, APM_ENT_TYPE_APPLE_BOOT))
486                 return (g_part_alias_name(G_PART_ALIAS_APPLE_BOOT));
487         if (!strcmp(type, APM_ENT_TYPE_APPLE_HFS))
488                 return (g_part_alias_name(G_PART_ALIAS_APPLE_HFS));
489         if (!strcmp(type, APM_ENT_TYPE_APPLE_UFS))
490                 return (g_part_alias_name(G_PART_ALIAS_APPLE_UFS));
491         if (!strcmp(type, APM_ENT_TYPE_FREEBSD))
492                 return (g_part_alias_name(G_PART_ALIAS_FREEBSD));
493         if (!strcmp(type, APM_ENT_TYPE_FREEBSD_NANDFS))
494                 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_NANDFS));
495         if (!strcmp(type, APM_ENT_TYPE_FREEBSD_SWAP))
496                 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_SWAP));
497         if (!strcmp(type, APM_ENT_TYPE_FREEBSD_UFS))
498                 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_UFS));
499         if (!strcmp(type, APM_ENT_TYPE_FREEBSD_VINUM))
500                 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_VINUM));
501         if (!strcmp(type, APM_ENT_TYPE_FREEBSD_ZFS))
502                 return (g_part_alias_name(G_PART_ALIAS_FREEBSD_ZFS));
503         buf[0] = '!';
504         len = MIN(sizeof(entry->ent.ent_type), bufsz - 2);
505         bcopy(type, buf + 1, len);
506         buf[len + 1] = '\0';
507         return (buf);
508 }
509
510 static int
511 g_part_apm_write(struct g_part_table *basetable, struct g_consumer *cp)
512 {
513         struct g_provider *pp;
514         struct g_part_entry *baseentry;
515         struct g_part_apm_entry *entry;
516         struct g_part_apm_table *table;
517         char *buf, *ptr;
518         uint32_t index;
519         int error;
520         size_t tblsz;
521
522         pp = cp->provider;
523         table = (struct g_part_apm_table *)basetable;
524         /*
525          * Tivo Series 1 disk partitions are currently read-only.
526          */
527         if (table->tivo_series1)
528                 return (EOPNOTSUPP);
529
530         /* Write the DDR only when we're newly created. */
531         if (basetable->gpt_created) {
532                 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
533                 be16enc(buf, table->ddr.ddr_sig);
534                 be16enc(buf + 2, table->ddr.ddr_blksize);
535                 be32enc(buf + 4, table->ddr.ddr_blkcount);
536                 error = g_write_data(cp, 0, buf, pp->sectorsize);
537                 g_free(buf);
538                 if (error)
539                         return (error);
540         }
541
542         /* Allocate the buffer for all entries */
543         tblsz = table->self.ent_pmblkcnt;
544         buf = g_malloc(tblsz * pp->sectorsize, M_WAITOK | M_ZERO);
545
546         /* Fill the self entry */
547         be16enc(buf, APM_ENT_SIG);
548         be32enc(buf + 4, table->self.ent_pmblkcnt);
549         be32enc(buf + 8, table->self.ent_start);
550         be32enc(buf + 12, table->self.ent_size);
551         bcopy(table->self.ent_name, buf + 16, sizeof(table->self.ent_name));
552         bcopy(table->self.ent_type, buf + 48, sizeof(table->self.ent_type));
553
554         baseentry = LIST_FIRST(&basetable->gpt_entry);
555         for (index = 1; index < tblsz; index++) {
556                 entry = (baseentry != NULL && index == baseentry->gpe_index)
557                     ? (struct g_part_apm_entry *)baseentry : NULL;
558                 ptr = buf + index * pp->sectorsize;
559                 be16enc(ptr, APM_ENT_SIG);
560                 be32enc(ptr + 4, table->self.ent_pmblkcnt);
561                 if (entry != NULL && !baseentry->gpe_deleted) {
562                         be32enc(ptr + 8, entry->ent.ent_start);
563                         be32enc(ptr + 12, entry->ent.ent_size);
564                         bcopy(entry->ent.ent_name, ptr + 16,
565                             sizeof(entry->ent.ent_name));
566                         bcopy(entry->ent.ent_type, ptr + 48,
567                             sizeof(entry->ent.ent_type));
568                 } else {
569                         strcpy(ptr + 48, APM_ENT_TYPE_UNUSED);
570                 }
571                 if (entry != NULL)
572                         baseentry = LIST_NEXT(baseentry, gpe_entry);
573         }
574
575         for (index = 0; index < tblsz; index += MAXPHYS / pp->sectorsize) {
576                 error = g_write_data(cp, (1 + index) * pp->sectorsize,
577                     buf + index * pp->sectorsize,
578                     (tblsz - index > MAXPHYS / pp->sectorsize) ? MAXPHYS:
579                     (tblsz - index) * pp->sectorsize);
580                 if (error) {
581                         g_free(buf);
582                         return (error);
583                 }
584         }
585         g_free(buf);
586         return (0);
587 }