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