2 * Copyright (c) 2008 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/diskpc98.h>
33 #include <sys/endian.h>
34 #include <sys/kernel.h>
36 #include <sys/limits.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/queue.h>
42 #include <sys/systm.h>
43 #include <sys/sysctl.h>
44 #include <geom/geom.h>
45 #include <geom/geom_int.h>
46 #include <geom/part/g_part.h>
48 #include "g_part_if.h"
50 FEATURE(geom_part_pc98, "GEOM partitioning class for PC-9800 disk partitions");
56 struct g_part_pc98_table {
57 struct g_part_table base;
59 u_char table[SECSIZE];
60 u_char menu[MENUSIZE];
63 struct g_part_pc98_entry {
64 struct g_part_entry base;
65 struct pc98_partition ent;
68 static int g_part_pc98_add(struct g_part_table *, struct g_part_entry *,
69 struct g_part_parms *);
70 static int g_part_pc98_bootcode(struct g_part_table *, struct g_part_parms *);
71 static int g_part_pc98_create(struct g_part_table *, struct g_part_parms *);
72 static int g_part_pc98_destroy(struct g_part_table *, struct g_part_parms *);
73 static void g_part_pc98_dumpconf(struct g_part_table *, struct g_part_entry *,
74 struct sbuf *, const char *);
75 static int g_part_pc98_dumpto(struct g_part_table *, struct g_part_entry *);
76 static int g_part_pc98_modify(struct g_part_table *, struct g_part_entry *,
77 struct g_part_parms *);
78 static const char *g_part_pc98_name(struct g_part_table *, struct g_part_entry *,
80 static int g_part_pc98_probe(struct g_part_table *, struct g_consumer *);
81 static int g_part_pc98_read(struct g_part_table *, struct g_consumer *);
82 static int g_part_pc98_setunset(struct g_part_table *, struct g_part_entry *,
83 const char *, unsigned int);
84 static const char *g_part_pc98_type(struct g_part_table *,
85 struct g_part_entry *, char *, size_t);
86 static int g_part_pc98_write(struct g_part_table *, struct g_consumer *);
87 static int g_part_pc98_resize(struct g_part_table *, struct g_part_entry *,
88 struct g_part_parms *);
90 static kobj_method_t g_part_pc98_methods[] = {
91 KOBJMETHOD(g_part_add, g_part_pc98_add),
92 KOBJMETHOD(g_part_bootcode, g_part_pc98_bootcode),
93 KOBJMETHOD(g_part_create, g_part_pc98_create),
94 KOBJMETHOD(g_part_destroy, g_part_pc98_destroy),
95 KOBJMETHOD(g_part_dumpconf, g_part_pc98_dumpconf),
96 KOBJMETHOD(g_part_dumpto, g_part_pc98_dumpto),
97 KOBJMETHOD(g_part_modify, g_part_pc98_modify),
98 KOBJMETHOD(g_part_resize, g_part_pc98_resize),
99 KOBJMETHOD(g_part_name, g_part_pc98_name),
100 KOBJMETHOD(g_part_probe, g_part_pc98_probe),
101 KOBJMETHOD(g_part_read, g_part_pc98_read),
102 KOBJMETHOD(g_part_setunset, g_part_pc98_setunset),
103 KOBJMETHOD(g_part_type, g_part_pc98_type),
104 KOBJMETHOD(g_part_write, g_part_pc98_write),
108 static struct g_part_scheme g_part_pc98_scheme = {
111 sizeof(struct g_part_pc98_table),
112 .gps_entrysz = sizeof(struct g_part_pc98_entry),
113 .gps_minent = PC98_NPARTS,
114 .gps_maxent = PC98_NPARTS,
115 .gps_bootcodesz = BOOTSIZE,
117 G_PART_SCHEME_DECLARE(g_part_pc98);
120 pc98_parse_type(const char *type, u_char *dp_mid, u_char *dp_sid)
126 if (type[0] == '!') {
127 lt = strtol(type + 1, &endp, 0);
128 if (type[1] == '\0' || *endp != '\0' || lt <= 0 ||
131 /* Make sure the active and bootable flags aren't set. */
132 if (lt & ((PC98_SID_ACTIVE << 8) | PC98_MID_BOOTABLE))
134 *dp_mid = (*dp_mid & PC98_MID_BOOTABLE) | (u_char)lt;
135 *dp_sid = (*dp_sid & PC98_SID_ACTIVE) | (u_char)(lt >> 8);
138 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD);
139 if (!strcasecmp(type, alias)) {
140 *dp_mid = (*dp_mid & PC98_MID_BOOTABLE) | PC98_MID_386BSD;
141 *dp_sid = (*dp_sid & PC98_SID_ACTIVE) | PC98_SID_386BSD;
148 pc98_set_slicename(const char *label, u_char *dp_name)
153 if (len > sizeof(((struct pc98_partition *)NULL)->dp_name))
155 bzero(dp_name, sizeof(((struct pc98_partition *)NULL)->dp_name));
156 strncpy(dp_name, label, len);
162 pc98_set_chs(struct g_part_table *table, uint32_t lba, u_short *cylp,
163 u_char *hdp, u_char *secp)
165 uint32_t cyl, hd, sec;
167 sec = lba % table->gpt_sectors + 1;
168 lba /= table->gpt_sectors;
169 hd = lba % table->gpt_heads;
170 lba /= table->gpt_heads;
173 *cylp = htole16(cyl);
179 pc98_align(struct g_part_table *basetable, uint32_t *start, uint32_t *size)
183 cyl = basetable->gpt_heads * basetable->gpt_sectors;
186 if (start != NULL && (*start % cyl)) {
187 *size += (*start % cyl) - cyl;
188 *start -= (*start % cyl) - cyl;
191 *size -= (*size % cyl);
198 g_part_pc98_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
199 struct g_part_parms *gpp)
201 struct g_part_pc98_entry *entry;
202 uint32_t start, size;
205 entry = (struct g_part_pc98_entry *)baseentry;
206 start = gpp->gpp_start;
207 size = gpp->gpp_size;
208 if (pc98_align(basetable, &start, &size) != 0)
210 if (baseentry->gpe_deleted)
211 bzero(&entry->ent, sizeof(entry->ent));
213 entry->ent.dp_mid = entry->ent.dp_sid = 0;
215 KASSERT(baseentry->gpe_start <= start, (__func__));
216 KASSERT(baseentry->gpe_end >= start + size - 1, (__func__));
217 baseentry->gpe_start = start;
218 baseentry->gpe_end = start + size - 1;
219 pc98_set_chs(basetable, baseentry->gpe_start, &entry->ent.dp_scyl,
220 &entry->ent.dp_shd, &entry->ent.dp_ssect);
221 pc98_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
222 &entry->ent.dp_ehd, &entry->ent.dp_esect);
224 error = pc98_parse_type(gpp->gpp_type, &entry->ent.dp_mid,
229 if (gpp->gpp_parms & G_PART_PARM_LABEL)
230 return (pc98_set_slicename(gpp->gpp_label, entry->ent.dp_name));
236 g_part_pc98_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
238 struct g_part_pc98_table *table;
239 const u_char *codeptr;
241 if (gpp->gpp_codesize != BOOTSIZE)
244 table = (struct g_part_pc98_table *)basetable;
245 codeptr = gpp->gpp_codeptr;
246 bcopy(codeptr, table->boot, SECSIZE);
247 bcopy(codeptr + SECSIZE*2, table->menu, MENUSIZE);
253 g_part_pc98_create(struct g_part_table *basetable, struct g_part_parms *gpp)
255 struct g_provider *pp;
256 struct g_part_pc98_table *table;
258 pp = gpp->gpp_provider;
259 if (pp->sectorsize < SECSIZE || pp->mediasize < BOOTSIZE)
261 if (pp->sectorsize > SECSIZE)
264 basetable->gpt_first = basetable->gpt_heads * basetable->gpt_sectors;
265 basetable->gpt_last = MIN(pp->mediasize / SECSIZE, UINT32_MAX) - 1;
267 table = (struct g_part_pc98_table *)basetable;
268 le16enc(table->boot + PC98_MAGICOFS, PC98_MAGIC);
273 g_part_pc98_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
276 /* Wipe the first two sectors to clear the partitioning. */
277 basetable->gpt_smhead |= 3;
282 g_part_pc98_dumpconf(struct g_part_table *table,
283 struct g_part_entry *baseentry, struct sbuf *sb, const char *indent)
285 struct g_part_pc98_entry *entry;
286 char name[sizeof(entry->ent.dp_name) + 1];
289 entry = (struct g_part_pc98_entry *)baseentry;
291 /* confxml: scheme information */
295 type = entry->ent.dp_mid + (entry->ent.dp_sid << 8);
296 strncpy(name, entry->ent.dp_name, sizeof(name) - 1);
297 name[sizeof(name) - 1] = '\0';
298 if (indent == NULL) {
299 /* conftxt: libdisk compatibility */
300 sbuf_printf(sb, " xs PC98 xt %u sn %s", type, name);
302 /* confxml: partition entry information */
303 sbuf_printf(sb, "%s<label>%s</label>\n", indent, name);
304 if (entry->ent.dp_mid & PC98_MID_BOOTABLE)
305 sbuf_printf(sb, "%s<attrib>bootable</attrib>\n",
307 if (entry->ent.dp_sid & PC98_SID_ACTIVE)
308 sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent);
309 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
315 g_part_pc98_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
317 struct g_part_pc98_entry *entry;
319 /* Allow dumping to a FreeBSD partition only. */
320 entry = (struct g_part_pc98_entry *)baseentry;
321 return (((entry->ent.dp_mid & PC98_MID_MASK) == PC98_MID_386BSD &&
322 (entry->ent.dp_sid & PC98_SID_MASK) == PC98_SID_386BSD) ? 1 : 0);
326 g_part_pc98_modify(struct g_part_table *basetable,
327 struct g_part_entry *baseentry, struct g_part_parms *gpp)
329 struct g_part_pc98_entry *entry;
332 entry = (struct g_part_pc98_entry *)baseentry;
334 if (gpp->gpp_parms & G_PART_PARM_TYPE) {
335 error = pc98_parse_type(gpp->gpp_type, &entry->ent.dp_mid,
341 if (gpp->gpp_parms & G_PART_PARM_LABEL)
342 return (pc98_set_slicename(gpp->gpp_label, entry->ent.dp_name));
348 g_part_pc98_resize(struct g_part_table *basetable,
349 struct g_part_entry *baseentry, struct g_part_parms *gpp)
351 struct g_part_pc98_entry *entry;
352 struct g_provider *pp;
355 size = gpp->gpp_size;
356 if (pc98_align(basetable, NULL, &size) != 0)
358 /* XXX: prevent unexpected shrinking. */
359 pp = baseentry->gpe_pp;
360 if ((g_debugflags & 0x10) == 0 && size < gpp->gpp_size &&
361 pp->mediasize / pp->sectorsize > size)
363 entry = (struct g_part_pc98_entry *)baseentry;
364 baseentry->gpe_end = baseentry->gpe_start + size - 1;
365 pc98_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
366 &entry->ent.dp_ehd, &entry->ent.dp_esect);
372 g_part_pc98_name(struct g_part_table *table, struct g_part_entry *baseentry,
373 char *buf, size_t bufsz)
376 snprintf(buf, bufsz, "s%d", baseentry->gpe_index);
381 g_part_pc98_probe(struct g_part_table *table, struct g_consumer *cp)
383 struct g_provider *pp;
385 int error, index, res, sum;
386 uint16_t magic, ecyl, scyl;
390 /* Sanity-check the provider. */
391 if (pp->sectorsize < SECSIZE || pp->mediasize < BOOTSIZE)
393 if (pp->sectorsize > SECSIZE)
396 /* Check that there's a PC98 partition table. */
397 buf = g_read_data(cp, 0L, 2 * SECSIZE, &error);
401 /* We goto out on mismatch. */
404 magic = le16dec(buf + PC98_MAGICOFS);
405 if (magic != PC98_MAGIC)
409 for (index = SECSIZE; index < 2 * SECSIZE; index++)
412 res = G_PART_PROBE_PRI_LOW;
416 for (index = 0; index < PC98_NPARTS; index++) {
417 p = buf + SECSIZE + index * PC98_PARTSIZE;
418 if (p[0] == 0 || p[1] == 0) /* !dp_mid || !dp_sid */
420 scyl = le16dec(p + 10);
421 ecyl = le16dec(p + 14);
422 if (scyl == 0 || ecyl == 0)
424 if (p[8] == p[12] && /* dp_ssect == dp_esect */
425 p[9] == p[13] && /* dp_shd == dp_ehd */
430 res = G_PART_PROBE_PRI_HIGH;
438 g_part_pc98_read(struct g_part_table *basetable, struct g_consumer *cp)
440 struct pc98_partition ent;
441 struct g_provider *pp;
442 struct g_part_pc98_table *table;
443 struct g_part_pc98_entry *entry;
451 table = (struct g_part_pc98_table *)basetable;
452 msize = MIN(pp->mediasize / SECSIZE, UINT32_MAX);
454 buf = g_read_data(cp, 0L, BOOTSIZE, &error);
458 cyl = basetable->gpt_heads * basetable->gpt_sectors;
460 bcopy(buf, table->boot, sizeof(table->boot));
461 bcopy(buf + SECSIZE, table->table, sizeof(table->table));
462 bcopy(buf + SECSIZE*2, table->menu, sizeof(table->menu));
464 for (index = PC98_NPARTS - 1; index >= 0; index--) {
465 p = buf + SECSIZE + index * PC98_PARTSIZE;
470 ent.dp_ipl_sct = p[4];
471 ent.dp_ipl_head = p[5];
472 ent.dp_ipl_cyl = le16dec(p + 6);
475 ent.dp_scyl = le16dec(p + 10);
476 ent.dp_esect = p[12];
478 ent.dp_ecyl = le16dec(p + 14);
479 bcopy(p + 16, ent.dp_name, sizeof(ent.dp_name));
483 start = ent.dp_scyl * cyl;
484 end = (ent.dp_ecyl + 1) * cyl - 1;
485 entry = (struct g_part_pc98_entry *)g_part_new_entry(basetable,
486 index + 1, start, end);
490 basetable->gpt_entries = PC98_NPARTS;
491 basetable->gpt_first = cyl;
492 basetable->gpt_last = msize - 1;
499 g_part_pc98_setunset(struct g_part_table *table, struct g_part_entry *baseentry,
500 const char *attrib, unsigned int set)
502 struct g_part_entry *iter;
503 struct g_part_pc98_entry *entry;
504 int changed, mid, sid;
506 if (baseentry == NULL)
510 if (strcasecmp(attrib, "active") == 0)
512 else if (strcasecmp(attrib, "bootable") == 0)
514 if (mid == 0 && sid == 0)
517 LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
518 if (iter->gpe_deleted)
520 if (iter != baseentry)
523 entry = (struct g_part_pc98_entry *)iter;
525 if (mid && !(entry->ent.dp_mid & PC98_MID_BOOTABLE)) {
526 entry->ent.dp_mid |= PC98_MID_BOOTABLE;
529 if (sid && !(entry->ent.dp_sid & PC98_SID_ACTIVE)) {
530 entry->ent.dp_sid |= PC98_SID_ACTIVE;
534 if (mid && (entry->ent.dp_mid & PC98_MID_BOOTABLE)) {
535 entry->ent.dp_mid &= ~PC98_MID_BOOTABLE;
538 if (sid && (entry->ent.dp_sid & PC98_SID_ACTIVE)) {
539 entry->ent.dp_sid &= ~PC98_SID_ACTIVE;
543 if (changed && !iter->gpe_created)
544 iter->gpe_modified = 1;
550 g_part_pc98_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
551 char *buf, size_t bufsz)
553 struct g_part_pc98_entry *entry;
556 entry = (struct g_part_pc98_entry *)baseentry;
557 type = (entry->ent.dp_mid & PC98_MID_MASK) |
558 ((entry->ent.dp_sid & PC98_SID_MASK) << 8);
559 if (type == (PC98_MID_386BSD | (PC98_SID_386BSD << 8)))
560 return (g_part_alias_name(G_PART_ALIAS_FREEBSD));
561 snprintf(buf, bufsz, "!%d", type);
566 g_part_pc98_write(struct g_part_table *basetable, struct g_consumer *cp)
568 struct g_part_entry *baseentry;
569 struct g_part_pc98_entry *entry;
570 struct g_part_pc98_table *table;
574 table = (struct g_part_pc98_table *)basetable;
575 baseentry = LIST_FIRST(&basetable->gpt_entry);
576 for (index = 1; index <= basetable->gpt_entries; index++) {
577 p = table->table + (index - 1) * PC98_PARTSIZE;
578 entry = (baseentry != NULL && index == baseentry->gpe_index)
579 ? (struct g_part_pc98_entry *)baseentry : NULL;
580 if (entry != NULL && !baseentry->gpe_deleted) {
581 p[0] = entry->ent.dp_mid;
582 p[1] = entry->ent.dp_sid;
583 p[2] = entry->ent.dp_dum1;
584 p[3] = entry->ent.dp_dum2;
585 p[4] = entry->ent.dp_ipl_sct;
586 p[5] = entry->ent.dp_ipl_head;
587 le16enc(p + 6, entry->ent.dp_ipl_cyl);
588 p[8] = entry->ent.dp_ssect;
589 p[9] = entry->ent.dp_shd;
590 le16enc(p + 10, entry->ent.dp_scyl);
591 p[12] = entry->ent.dp_esect;
592 p[13] = entry->ent.dp_ehd;
593 le16enc(p + 14, entry->ent.dp_ecyl);
594 bcopy(entry->ent.dp_name, p + 16,
595 sizeof(entry->ent.dp_name));
597 bzero(p, PC98_PARTSIZE);
600 baseentry = LIST_NEXT(baseentry, gpe_entry);
603 error = g_write_data(cp, 0, table->boot, SECSIZE);
605 error = g_write_data(cp, SECSIZE, table->table, SECSIZE);
607 error = g_write_data(cp, SECSIZE*2, table->menu, MENUSIZE);