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