]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/geom/part/g_part_pc98.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / geom / part / g_part_pc98.c
1 /*-
2  * Copyright (c) 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/bio.h>
32 #include <sys/diskpc98.h>
33 #include <sys/endian.h>
34 #include <sys/kernel.h>
35 #include <sys/kobj.h>
36 #include <sys/limits.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/queue.h>
41 #include <sys/sbuf.h>
42 #include <sys/systm.h>
43 #include <geom/geom.h>
44 #include <geom/part/g_part.h>
45
46 #include "g_part_if.h"
47
48 #define SECSIZE         512
49
50 struct g_part_pc98_table {
51         struct g_part_table     base;
52         u_char          boot[SECSIZE];
53         u_char          table[SECSIZE];
54 };
55
56 struct g_part_pc98_entry {
57         struct g_part_entry     base;
58         struct pc98_partition ent;
59 };
60
61 static int g_part_pc98_add(struct g_part_table *, struct g_part_entry *,
62     struct g_part_parms *);
63 static int g_part_pc98_bootcode(struct g_part_table *, struct g_part_parms *);
64 static int g_part_pc98_create(struct g_part_table *, struct g_part_parms *);
65 static int g_part_pc98_destroy(struct g_part_table *, struct g_part_parms *);
66 static void g_part_pc98_dumpconf(struct g_part_table *, struct g_part_entry *,
67     struct sbuf *, const char *);
68 static int g_part_pc98_dumpto(struct g_part_table *, struct g_part_entry *);
69 static int g_part_pc98_modify(struct g_part_table *, struct g_part_entry *,  
70     struct g_part_parms *);
71 static const char *g_part_pc98_name(struct g_part_table *, struct g_part_entry *,
72     char *, size_t);
73 static int g_part_pc98_probe(struct g_part_table *, struct g_consumer *);
74 static int g_part_pc98_read(struct g_part_table *, struct g_consumer *);
75 static int g_part_pc98_setunset(struct g_part_table *, struct g_part_entry *,
76     const char *, unsigned int);
77 static const char *g_part_pc98_type(struct g_part_table *,
78     struct g_part_entry *, char *, size_t);
79 static int g_part_pc98_write(struct g_part_table *, struct g_consumer *);
80
81 static kobj_method_t g_part_pc98_methods[] = {
82         KOBJMETHOD(g_part_add,          g_part_pc98_add),
83         KOBJMETHOD(g_part_bootcode,     g_part_pc98_bootcode),
84         KOBJMETHOD(g_part_create,       g_part_pc98_create),
85         KOBJMETHOD(g_part_destroy,      g_part_pc98_destroy),
86         KOBJMETHOD(g_part_dumpconf,     g_part_pc98_dumpconf),
87         KOBJMETHOD(g_part_dumpto,       g_part_pc98_dumpto),
88         KOBJMETHOD(g_part_modify,       g_part_pc98_modify),
89         KOBJMETHOD(g_part_name,         g_part_pc98_name),
90         KOBJMETHOD(g_part_probe,        g_part_pc98_probe),
91         KOBJMETHOD(g_part_read,         g_part_pc98_read),
92         KOBJMETHOD(g_part_setunset,     g_part_pc98_setunset),
93         KOBJMETHOD(g_part_type,         g_part_pc98_type),
94         KOBJMETHOD(g_part_write,        g_part_pc98_write),
95         { 0, 0 }
96 };
97
98 static struct g_part_scheme g_part_pc98_scheme = {
99         "PC98",
100         g_part_pc98_methods,
101         sizeof(struct g_part_pc98_table),
102         .gps_entrysz = sizeof(struct g_part_pc98_entry),
103         .gps_minent = NDOSPART,
104         .gps_maxent = NDOSPART,
105         .gps_bootcodesz = SECSIZE,
106 };
107 G_PART_SCHEME_DECLARE(g_part_pc98);
108
109 static int
110 pc98_parse_type(const char *type, u_char *dp_mid, u_char *dp_sid)
111 {
112         const char *alias;
113         char *endp;
114         long lt;
115
116         if (type[0] == '!') {
117                 lt = strtol(type + 1, &endp, 0);
118                 if (type[1] == '\0' || *endp != '\0' || lt <= 0 ||
119                     lt >= 65536)
120                         return (EINVAL);
121                 /* Make sure the active and bootable flags aren't set. */
122                 if (lt & ((PC98_SID_ACTIVE << 8) | PC98_MID_BOOTABLE))
123                         return (ENOATTR);
124                 *dp_mid = (*dp_mid & PC98_MID_BOOTABLE) | (u_char)lt;
125                 *dp_sid = (*dp_sid & PC98_SID_ACTIVE) | (u_char)(lt >> 8);
126                 return (0);
127         }
128         alias = g_part_alias_name(G_PART_ALIAS_FREEBSD);
129         if (!strcasecmp(type, alias)) {
130                 *dp_mid = (*dp_mid & PC98_MID_BOOTABLE) | PC98_MID_386BSD;
131                 *dp_sid = (*dp_sid & PC98_SID_ACTIVE) | PC98_SID_386BSD;
132                 return (0);
133         }
134         return (EINVAL);
135 }
136
137 static void
138 pc98_set_chs(struct g_part_table *table, uint32_t lba, u_short *cylp,
139     u_char *hdp, u_char *secp)
140 {
141         uint32_t cyl, hd, sec;
142
143         sec = lba % table->gpt_sectors + 1;
144         lba /= table->gpt_sectors;
145         hd = lba % table->gpt_heads;
146         lba /= table->gpt_heads;
147         cyl = lba;
148
149         *cylp = htole16(cyl);
150         *hdp = hd;
151         *secp = sec;
152 }
153
154 static int
155 g_part_pc98_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
156     struct g_part_parms *gpp)
157 {
158         struct g_part_pc98_entry *entry;
159         struct g_part_pc98_table *table;
160         uint32_t cyl, start, size;
161
162         if (gpp->gpp_parms & G_PART_PARM_LABEL)
163                 return (EINVAL);
164
165         cyl = basetable->gpt_heads * basetable->gpt_sectors;
166
167         entry = (struct g_part_pc98_entry *)baseentry;
168         table = (struct g_part_pc98_table *)basetable;
169
170         start = gpp->gpp_start;
171         size = gpp->gpp_size;
172         if (size < cyl)
173                 return (EINVAL);
174         if (start % cyl) {
175                 size = size - cyl + (start % cyl);
176                 start = start - (start % cyl) + cyl;
177         }
178         if (size % cyl)
179                 size = size - (size % cyl);
180         if (size < cyl)
181                 return (EINVAL);
182
183         if (baseentry->gpe_deleted)
184                 bzero(&entry->ent, sizeof(entry->ent));
185         else
186                 entry->ent.dp_mid = entry->ent.dp_sid = 0;
187
188         KASSERT(baseentry->gpe_start <= start, (__func__));
189         KASSERT(baseentry->gpe_end >= start + size - 1, (__func__));
190         baseentry->gpe_start = start;
191         baseentry->gpe_end = start + size - 1;
192         pc98_set_chs(basetable, baseentry->gpe_start, &entry->ent.dp_scyl,
193             &entry->ent.dp_shd, &entry->ent.dp_ssect);
194         pc98_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
195             &entry->ent.dp_ehd, &entry->ent.dp_esect);
196         return (pc98_parse_type(gpp->gpp_type, &entry->ent.dp_mid,
197             &entry->ent.dp_sid));
198 }
199
200 static int
201 g_part_pc98_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
202 {
203         struct g_part_pc98_table *table;
204         size_t codesz;
205
206         codesz = DOSMAGICOFFSET;
207         table = (struct g_part_pc98_table *)basetable;
208         bzero(table->boot, codesz);
209         codesz = MIN(codesz, gpp->gpp_codesize);
210         if (codesz > 0)
211                 bcopy(gpp->gpp_codeptr, table->boot, codesz);
212         return (0);
213 }
214
215 static int
216 g_part_pc98_create(struct g_part_table *basetable, struct g_part_parms *gpp)
217 {
218         struct g_consumer *cp;
219         struct g_provider *pp;
220         struct g_part_pc98_table *table;
221         uint64_t msize;
222         uint32_t cyl;
223
224         pp = gpp->gpp_provider;
225         cp = LIST_FIRST(&pp->consumers);
226
227         if (pp->sectorsize < SECSIZE || pp->mediasize < 2 * SECSIZE)
228                 return (ENOSPC);
229         if (pp->sectorsize > SECSIZE)
230                 return (ENXIO);
231
232         cyl = basetable->gpt_heads * basetable->gpt_sectors;
233
234         msize = pp->mediasize / SECSIZE;
235         basetable->gpt_first = cyl;
236         basetable->gpt_last = msize - (msize % cyl) - 1;
237
238         table = (struct g_part_pc98_table *)basetable;
239         le16enc(table->boot + DOSMAGICOFFSET, DOSMAGIC);
240         return (0);
241 }
242
243 static int
244 g_part_pc98_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
245 {
246
247         /* Wipe the first two sectors to clear the partitioning. */
248         basetable->gpt_smhead |= 3;
249         return (0);
250 }
251
252 static void
253 g_part_pc98_dumpconf(struct g_part_table *table,
254     struct g_part_entry *baseentry, struct sbuf *sb, const char *indent)
255 {
256         struct g_part_pc98_entry *entry;
257         char name[sizeof(entry->ent.dp_name) + 1];
258         u_int type;
259
260         entry = (struct g_part_pc98_entry *)baseentry;
261         if (entry == NULL) {
262                 /* confxml: scheme information */
263                 return;
264         }
265
266         type = entry->ent.dp_mid + (entry->ent.dp_sid << 8);
267         strncpy(name, entry->ent.dp_name, sizeof(name) - 1);
268         name[sizeof(name) - 1] = '\0';
269         if (indent == NULL) {
270                 /* conftxt: libdisk compatibility */
271                 sbuf_printf(sb, " xs PC98 xt %u sn %s", type, name);
272         } else {
273                 /* confxml: partition entry information */
274                 sbuf_printf(sb, "%s<label>%s</label>\n", indent, name);
275                 if (entry->ent.dp_mid & PC98_MID_BOOTABLE)
276                         sbuf_printf(sb, "%s<attrib>bootable</attrib>\n",
277                             indent);
278                 if (entry->ent.dp_sid & PC98_SID_ACTIVE)
279                         sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent);
280                 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
281                     type & 0x7f7f);
282         }
283 }
284
285 static int
286 g_part_pc98_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)  
287 {
288         struct g_part_pc98_entry *entry;
289
290         /* Allow dumping to a FreeBSD partition only. */
291         entry = (struct g_part_pc98_entry *)baseentry;
292         return (((entry->ent.dp_mid & PC98_MID_MASK) == PC98_MID_386BSD &&
293             (entry->ent.dp_sid & PC98_SID_MASK) == PC98_SID_386BSD) ? 1 : 0);
294 }
295
296 static int
297 g_part_pc98_modify(struct g_part_table *basetable,
298     struct g_part_entry *baseentry, struct g_part_parms *gpp)
299 {
300         struct g_part_pc98_entry *entry;
301
302         if (gpp->gpp_parms & G_PART_PARM_LABEL)
303                 return (EINVAL);
304
305         entry = (struct g_part_pc98_entry *)baseentry;
306         if (gpp->gpp_parms & G_PART_PARM_TYPE)
307                 return (pc98_parse_type(gpp->gpp_type, &entry->ent.dp_mid,
308                     &entry->ent.dp_sid));
309         return (0);
310 }
311
312 static const char *
313 g_part_pc98_name(struct g_part_table *table, struct g_part_entry *baseentry,
314     char *buf, size_t bufsz)
315 {
316
317         snprintf(buf, bufsz, "s%d", baseentry->gpe_index);
318         return (buf);
319 }
320
321 static int
322 g_part_pc98_probe(struct g_part_table *table, struct g_consumer *cp)
323 {
324         struct g_provider *pp;
325         u_char *buf, *p;
326         int error, index, res, sum;
327         uint16_t magic, ecyl, scyl;
328
329         pp = cp->provider;
330
331         /* Sanity-check the provider. */
332         if (pp->sectorsize < SECSIZE || pp->mediasize < 2 * SECSIZE)
333                 return (ENOSPC);
334         if (pp->sectorsize > SECSIZE)
335                 return (ENXIO);
336
337         /* Check that there's a PC98 partition table. */
338         buf = g_read_data(cp, 0L, 2 * SECSIZE, &error);
339         if (buf == NULL)
340                 return (error);
341
342         /* We goto out on mismatch. */
343         res = ENXIO;
344
345         magic = le16dec(buf + DOSMAGICOFFSET);
346         if (magic != DOSMAGIC)
347                 goto out;
348
349         sum = 0;
350         for (index = SECSIZE; index < 2 * SECSIZE; index++)
351                 sum += buf[index];
352         if (sum == 0) {
353                 res = G_PART_PROBE_PRI_LOW;
354                 goto out;
355         }
356
357         for (index = 0; index < NDOSPART; index++) {
358                 p = buf + SECSIZE + index * DOSPARTSIZE;
359                 if (p[0] == 0 || p[1] == 0)     /* !dp_mid || !dp_sid */
360                         continue;
361                 scyl = le16dec(p + 10);
362                 ecyl = le16dec(p + 14);
363                 if (scyl == 0 || ecyl == 0)
364                         goto out;
365                 if (p[8] == p[12] &&            /* dp_ssect == dp_esect */
366                     p[9] == p[13] &&            /* dp_shd == dp_ehd */
367                     scyl == ecyl)
368                         goto out;
369         }
370
371         res = G_PART_PROBE_PRI_HIGH;
372
373  out:
374         g_free(buf);
375         return (res);
376 }
377
378 static int
379 g_part_pc98_read(struct g_part_table *basetable, struct g_consumer *cp)
380 {
381         struct pc98_partition ent;
382         struct g_provider *pp;
383         struct g_part_pc98_table *table;
384         struct g_part_pc98_entry *entry;
385         u_char *buf, *p;
386         off_t msize;
387         off_t start, end;
388         u_int cyl;
389         int error, index;
390
391         pp = cp->provider;
392         table = (struct g_part_pc98_table *)basetable;
393         msize = pp->mediasize / SECSIZE;
394
395         buf = g_read_data(cp, 0L, 2 * SECSIZE, &error);
396         if (buf == NULL)
397                 return (error);
398
399         cyl = basetable->gpt_heads * basetable->gpt_sectors;
400
401         bcopy(buf, table->boot, sizeof(table->boot));
402         bcopy(buf + SECSIZE, table->table, sizeof(table->table));
403
404         for (index = NDOSPART - 1; index >= 0; index--) {
405                 p = buf + SECSIZE + index * DOSPARTSIZE;
406                 ent.dp_mid = p[0];
407                 ent.dp_sid = p[1];
408                 ent.dp_dum1 = p[2];
409                 ent.dp_dum2 = p[3];
410                 ent.dp_ipl_sct = p[4];
411                 ent.dp_ipl_head = p[5];
412                 ent.dp_ipl_cyl = le16dec(p + 6);
413                 ent.dp_ssect = p[8];
414                 ent.dp_shd = p[9];
415                 ent.dp_scyl = le16dec(p + 10);
416                 ent.dp_esect = p[12];
417                 ent.dp_ehd = p[13];
418                 ent.dp_ecyl = le16dec(p + 14);
419                 bcopy(p + 16, ent.dp_name, sizeof(ent.dp_name));
420                 if (ent.dp_sid == 0)
421                         continue;
422
423                 start = ent.dp_scyl * cyl;
424                 end = (ent.dp_ecyl + 1) * cyl - 1;
425                 entry = (struct g_part_pc98_entry *)g_part_new_entry(basetable,
426                     index + 1, start, end);
427                 entry->ent = ent;
428         }
429
430         basetable->gpt_entries = NDOSPART;
431         basetable->gpt_first = cyl;
432         basetable->gpt_last = msize - (msize % cyl) - 1;
433
434         return (0);
435 }
436
437 static int
438 g_part_pc98_setunset(struct g_part_table *table, struct g_part_entry *baseentry,
439     const char *attrib, unsigned int set)
440 {
441         struct g_part_entry *iter;
442         struct g_part_pc98_entry *entry;
443         int changed, mid, sid;
444
445         mid = sid = 0;
446         if (strcasecmp(attrib, "active") == 0)
447                 sid = 1;
448         else if (strcasecmp(attrib, "bootable") == 0)
449                 mid = 1;
450         if (mid == 0 && sid == 0)
451                 return (EINVAL);
452
453         LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
454                 if (iter->gpe_deleted)
455                         continue;
456                 if (iter != baseentry)
457                         continue;
458                 changed = 0;
459                 entry = (struct g_part_pc98_entry *)iter;
460                 if (set) {
461                         if (mid && !(entry->ent.dp_mid & PC98_MID_BOOTABLE)) {
462                                 entry->ent.dp_mid |= PC98_MID_BOOTABLE;
463                                 changed = 1;
464                         }
465                         if (sid && !(entry->ent.dp_sid & PC98_SID_ACTIVE)) {
466                                 entry->ent.dp_sid |= PC98_SID_ACTIVE;
467                                 changed = 1;
468                         }
469                 } else {
470                         if (mid && (entry->ent.dp_mid & PC98_MID_BOOTABLE)) {
471                                 entry->ent.dp_mid &= ~PC98_MID_BOOTABLE;
472                                 changed = 1;
473                         }
474                         if (sid && (entry->ent.dp_sid & PC98_SID_ACTIVE)) {
475                                 entry->ent.dp_sid &= ~PC98_SID_ACTIVE;
476                                 changed = 1;
477                         }
478                 }
479                 if (changed && !iter->gpe_created)
480                         iter->gpe_modified = 1;
481         }
482         return (0);
483 }
484
485 static const char *
486 g_part_pc98_type(struct g_part_table *basetable, struct g_part_entry *baseentry, 
487     char *buf, size_t bufsz)
488 {
489         struct g_part_pc98_entry *entry;
490         u_int type;
491
492         entry = (struct g_part_pc98_entry *)baseentry;
493         type = (entry->ent.dp_mid & PC98_MID_MASK) |
494             ((entry->ent.dp_sid & PC98_SID_MASK) << 8);
495         if (type == (PC98_MID_386BSD | (PC98_SID_386BSD << 8)))
496                 return (g_part_alias_name(G_PART_ALIAS_FREEBSD));
497         snprintf(buf, bufsz, "!%d", type);
498         return (buf);
499 }
500
501 static int
502 g_part_pc98_write(struct g_part_table *basetable, struct g_consumer *cp)
503 {
504         struct g_part_entry *baseentry;
505         struct g_part_pc98_entry *entry;
506         struct g_part_pc98_table *table;
507         u_char *p;
508         int error, index;
509
510         table = (struct g_part_pc98_table *)basetable;
511         baseentry = LIST_FIRST(&basetable->gpt_entry);
512         for (index = 1; index <= basetable->gpt_entries; index++) {
513                 p = table->table + (index - 1) * DOSPARTSIZE;
514                 entry = (baseentry != NULL && index == baseentry->gpe_index)
515                     ? (struct g_part_pc98_entry *)baseentry : NULL;
516                 if (entry != NULL && !baseentry->gpe_deleted) {
517                         p[0] = entry->ent.dp_mid;
518                         p[1] = entry->ent.dp_sid;
519                         p[2] = entry->ent.dp_dum1;
520                         p[3] = entry->ent.dp_dum2;
521                         p[4] = entry->ent.dp_ipl_sct;
522                         p[5] = entry->ent.dp_ipl_head;
523                         le16enc(p + 6, entry->ent.dp_ipl_cyl);
524                         p[8] = entry->ent.dp_ssect;
525                         p[9] = entry->ent.dp_shd;
526                         le16enc(p + 10, entry->ent.dp_scyl);
527                         p[12] = entry->ent.dp_esect;
528                         p[13] = entry->ent.dp_ehd;
529                         le16enc(p + 14, entry->ent.dp_ecyl);
530                         bcopy(entry->ent.dp_name, p + 16,
531                             sizeof(entry->ent.dp_name));
532                 } else
533                         bzero(p, DOSPARTSIZE);
534
535                 if (entry != NULL)
536                         baseentry = LIST_NEXT(baseentry, gpe_entry);
537         }
538
539         error = g_write_data(cp, 0, table->boot, SECSIZE);
540         if (!error)
541                 error = g_write_data(cp, SECSIZE, table->table, SECSIZE);
542         return (error);
543 }