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