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