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 <geom/geom.h>
44 #include <geom/part/g_part.h>
46 #include "g_part_if.h"
50 struct g_part_pc98_table {
51 struct g_part_table base;
53 u_char table[SECSIZE];
56 struct g_part_pc98_entry {
57 struct g_part_entry base;
58 struct pc98_partition ent;
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 int 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 char *g_part_pc98_name(struct g_part_table *, struct g_part_entry *,
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 const char *g_part_pc98_type(struct g_part_table *, struct g_part_entry *,
77 static int g_part_pc98_write(struct g_part_table *, struct g_consumer *);
79 static kobj_method_t g_part_pc98_methods[] = {
80 KOBJMETHOD(g_part_add, g_part_pc98_add),
81 KOBJMETHOD(g_part_bootcode, g_part_pc98_bootcode),
82 KOBJMETHOD(g_part_create, g_part_pc98_create),
83 KOBJMETHOD(g_part_destroy, g_part_pc98_destroy),
84 KOBJMETHOD(g_part_dumpconf, g_part_pc98_dumpconf),
85 KOBJMETHOD(g_part_dumpto, g_part_pc98_dumpto),
86 KOBJMETHOD(g_part_modify, g_part_pc98_modify),
87 KOBJMETHOD(g_part_name, g_part_pc98_name),
88 KOBJMETHOD(g_part_probe, g_part_pc98_probe),
89 KOBJMETHOD(g_part_read, g_part_pc98_read),
90 KOBJMETHOD(g_part_type, g_part_pc98_type),
91 KOBJMETHOD(g_part_write, g_part_pc98_write),
95 static struct g_part_scheme g_part_pc98_scheme = {
98 sizeof(struct g_part_pc98_table),
99 .gps_entrysz = sizeof(struct g_part_pc98_entry),
100 .gps_minent = NDOSPART,
101 .gps_maxent = NDOSPART,
102 .gps_bootcodesz = SECSIZE,
104 G_PART_SCHEME_DECLARE(g_part_pc98);
107 pc98_parse_type(const char *type, u_char *dp_mid, u_char *dp_sid)
113 if (type[0] == '!') {
114 lt = strtol(type + 1, &endp, 0);
115 if (type[1] == '\0' || *endp != '\0' || lt <= 0 ||
118 *dp_mid = (u_char)lt;
119 *dp_sid = (u_char)(lt >> 8);
122 alias = g_part_alias_name(G_PART_ALIAS_FREEBSD);
123 if (!strcasecmp(type, alias)) {
124 *dp_mid = (u_char)DOSMID_386BSD;
125 *dp_sid = (u_char)DOSSID_386BSD;
132 pc98_set_chs(struct g_part_table *table, uint32_t lba, u_short *cylp,
133 u_char *hdp, u_char *secp)
135 uint32_t cyl, hd, sec;
137 sec = lba % table->gpt_sectors + 1;
138 lba /= table->gpt_sectors;
139 hd = lba % table->gpt_heads;
140 lba /= table->gpt_heads;
143 *cylp = htole16(cyl);
149 g_part_pc98_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
150 struct g_part_parms *gpp)
152 struct g_part_pc98_entry *entry;
153 struct g_part_pc98_table *table;
154 uint32_t cyl, start, size;
156 if (gpp->gpp_parms & G_PART_PARM_LABEL)
159 cyl = basetable->gpt_heads * basetable->gpt_sectors;
161 entry = (struct g_part_pc98_entry *)baseentry;
162 table = (struct g_part_pc98_table *)basetable;
164 start = gpp->gpp_start;
165 size = gpp->gpp_size;
169 size = size - cyl + (start % cyl);
170 start = start - (start % cyl) + cyl;
173 size = size - (size % cyl);
177 if (baseentry->gpe_deleted)
178 bzero(&entry->ent, sizeof(entry->ent));
180 KASSERT(baseentry->gpe_start <= start, (__func__));
181 KASSERT(baseentry->gpe_end >= start + size - 1, (__func__));
182 baseentry->gpe_start = start;
183 baseentry->gpe_end = start + size - 1;
184 pc98_set_chs(basetable, baseentry->gpe_start, &entry->ent.dp_scyl,
185 &entry->ent.dp_shd, &entry->ent.dp_ssect);
186 pc98_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
187 &entry->ent.dp_ehd, &entry->ent.dp_esect);
188 return (pc98_parse_type(gpp->gpp_type, &entry->ent.dp_mid,
189 &entry->ent.dp_sid));
193 g_part_pc98_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
195 struct g_part_pc98_table *table;
197 table = (struct g_part_pc98_table *)basetable;
198 bcopy(gpp->gpp_codeptr, table->boot, DOSMAGICOFFSET);
203 g_part_pc98_create(struct g_part_table *basetable, struct g_part_parms *gpp)
205 struct g_consumer *cp;
206 struct g_provider *pp;
207 struct g_part_pc98_table *table;
211 pp = gpp->gpp_provider;
212 cp = LIST_FIRST(&pp->consumers);
214 if (pp->sectorsize < SECSIZE || pp->mediasize < 2 * SECSIZE)
216 if (pp->sectorsize > SECSIZE)
219 cyl = basetable->gpt_heads * basetable->gpt_sectors;
221 msize = pp->mediasize / SECSIZE;
222 basetable->gpt_first = cyl;
223 basetable->gpt_last = msize - (msize % cyl) - 1;
225 table = (struct g_part_pc98_table *)basetable;
226 le16enc(table->boot + DOSMAGICOFFSET, DOSMAGIC);
231 g_part_pc98_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
234 /* Wipe the first two sectors to clear the partitioning. */
235 basetable->gpt_smhead |= 3;
240 g_part_pc98_dumpconf(struct g_part_table *table,
241 struct g_part_entry *baseentry, struct sbuf *sb, const char *indent)
243 struct g_part_pc98_entry *entry;
244 char name[sizeof(entry->ent.dp_name) + 1];
247 entry = (struct g_part_pc98_entry *)baseentry;
249 /* confxml: scheme information */
253 type = entry->ent.dp_mid + (entry->ent.dp_sid << 8);
254 strncpy(name, entry->ent.dp_name, sizeof(name) - 1);
255 name[sizeof(name) - 1] = '\0';
256 if (indent == NULL) {
257 /* conftxt: libdisk compatibility */
258 sbuf_printf(sb, " xs PC98 xt %u sn %s", type, name);
260 /* confxml: partition entry information */
261 sbuf_printf(sb, "%s<label>%s</label>\n", indent, name);
262 sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent, type);
268 g_part_pc98_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
270 struct g_part_pc98_entry *entry;
272 /* Allow dumping to a FreeBSD partition only. */
273 entry = (struct g_part_pc98_entry *)baseentry;
274 return ((entry->ent.dp_mid == DOSMID_386BSD &&
275 entry->ent.dp_sid == DOSSID_386BSD) ? 1 : 0);
279 g_part_pc98_modify(struct g_part_table *basetable,
280 struct g_part_entry *baseentry, struct g_part_parms *gpp)
282 struct g_part_pc98_entry *entry;
284 if (gpp->gpp_parms & G_PART_PARM_LABEL)
287 entry = (struct g_part_pc98_entry *)baseentry;
288 if (gpp->gpp_parms & G_PART_PARM_TYPE)
289 return (pc98_parse_type(gpp->gpp_type, &entry->ent.dp_mid,
290 &entry->ent.dp_sid));
295 g_part_pc98_name(struct g_part_table *table, struct g_part_entry *baseentry,
296 char *buf, size_t bufsz)
299 snprintf(buf, bufsz, "s%d", baseentry->gpe_index);
304 g_part_pc98_probe(struct g_part_table *table, struct g_consumer *cp)
306 struct g_provider *pp;
308 int error, index, res, sum;
313 /* Sanity-check the provider. */
314 if (pp->sectorsize < SECSIZE || pp->mediasize < 2 * SECSIZE)
316 if (pp->sectorsize > SECSIZE)
319 /* Check that there's a PC98 partition table. */
320 buf = g_read_data(cp, 0L, 2 * SECSIZE, &error);
324 /* We goto out on mismatch. */
327 magic = le16dec(buf + DOSMAGICOFFSET);
328 if (magic != DOSMAGIC)
332 for (index = SECSIZE; index < 2 * SECSIZE; index++)
335 res = G_PART_PROBE_PRI_LOW;
339 for (index = 0; index < NDOSPART; index++) {
340 p = buf + SECSIZE + index * DOSPARTSIZE;
341 if (p[2] != 0 || p[3] != 0)
345 if (le16dec(p + 10) == 0)
349 res = G_PART_PROBE_PRI_HIGH;
357 g_part_pc98_read(struct g_part_table *basetable, struct g_consumer *cp)
359 struct pc98_partition ent;
360 struct g_provider *pp;
361 struct g_part_pc98_table *table;
362 struct g_part_pc98_entry *entry;
370 table = (struct g_part_pc98_table *)basetable;
371 msize = pp->mediasize / SECSIZE;
373 buf = g_read_data(cp, 0L, 2 * SECSIZE, &error);
377 cyl = basetable->gpt_heads * basetable->gpt_sectors;
379 bcopy(buf, table->boot, sizeof(table->boot));
380 bcopy(buf + SECSIZE, table->table, sizeof(table->table));
382 for (index = NDOSPART - 1; index >= 0; index--) {
383 p = buf + SECSIZE + index * DOSPARTSIZE;
388 ent.dp_ipl_sct = p[4];
389 ent.dp_ipl_head = p[5];
390 ent.dp_ipl_cyl = le16dec(p + 6);
393 ent.dp_scyl = le16dec(p + 10);
394 ent.dp_esect = p[12];
396 ent.dp_ecyl = le16dec(p + 14);
397 bcopy(p + 16, ent.dp_name, sizeof(ent.dp_name));
401 start = ent.dp_scyl * cyl;
402 end = (ent.dp_ecyl + 1) * cyl - 1;
403 entry = (struct g_part_pc98_entry *)g_part_new_entry(basetable,
404 index + 1, start, end);
408 basetable->gpt_entries = NDOSPART;
409 basetable->gpt_first = cyl;
410 basetable->gpt_last = msize - (msize % cyl) - 1;
416 g_part_pc98_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
417 char *buf, size_t bufsz)
419 struct g_part_pc98_entry *entry;
422 entry = (struct g_part_pc98_entry *)baseentry;
423 type = entry->ent.dp_mid + (entry->ent.dp_sid << 8);
424 if (type == DOSPTYP_386BSD)
425 return (g_part_alias_name(G_PART_ALIAS_FREEBSD));
426 snprintf(buf, bufsz, "!%d", type);
431 g_part_pc98_write(struct g_part_table *basetable, struct g_consumer *cp)
433 struct g_part_entry *baseentry;
434 struct g_part_pc98_entry *entry;
435 struct g_part_pc98_table *table;
439 table = (struct g_part_pc98_table *)basetable;
440 baseentry = LIST_FIRST(&basetable->gpt_entry);
441 for (index = 1; index <= basetable->gpt_entries; index++) {
442 p = table->table + (index - 1) * DOSPARTSIZE;
443 entry = (baseentry != NULL && index == baseentry->gpe_index)
444 ? (struct g_part_pc98_entry *)baseentry : NULL;
445 if (entry != NULL && !baseentry->gpe_deleted) {
446 p[0] = entry->ent.dp_mid;
447 p[1] = entry->ent.dp_sid;
448 p[2] = entry->ent.dp_dum1;
449 p[3] = entry->ent.dp_dum2;
450 p[4] = entry->ent.dp_ipl_sct;
451 p[5] = entry->ent.dp_ipl_head;
452 le16enc(p + 6, entry->ent.dp_ipl_cyl);
453 p[8] = entry->ent.dp_ssect;
454 p[9] = entry->ent.dp_shd;
455 le16enc(p + 10, entry->ent.dp_scyl);
456 p[12] = entry->ent.dp_esect;
457 p[13] = entry->ent.dp_ehd;
458 le16enc(p + 14, entry->ent.dp_ecyl);
459 bcopy(entry->ent.dp_name, p + 16,
460 sizeof(entry->ent.dp_name));
462 bzero(p, DOSPARTSIZE);
465 baseentry = LIST_NEXT(baseentry, gpe_entry);
468 error = g_write_data(cp, 0, table->boot, SECSIZE);
470 error = g_write_data(cp, SECSIZE, table->table, SECSIZE);