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: stable/9/sys/geom/part/g_part_pc98.c 267157 2014-06-06 12:45:11Z ae $");
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 <sys/sysctl.h>
44 #include <geom/geom.h>
45 #include <geom/geom_int.h>
46 #include <geom/part/g_part.h>
47 
48 #include "g_part_if.h"
49 
50 FEATURE(geom_part_pc98, "GEOM partitioning class for PC-9800 disk partitions");
51 
52 #define	SECSIZE		512
53 #define	MENUSIZE	7168
54 #define	BOOTSIZE	8192
55 
56 struct g_part_pc98_table {
57 	struct g_part_table	base;
58 	u_char		boot[SECSIZE];
59 	u_char		table[SECSIZE];
60 	u_char		menu[MENUSIZE];
61 };
62 
63 struct g_part_pc98_entry {
64 	struct g_part_entry	base;
65 	struct pc98_partition ent;
66 };
67 
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 *,
79     char *, size_t);
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 *);
89 
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),
105 	{ 0, 0 }
106 };
107 
108 static struct g_part_scheme g_part_pc98_scheme = {
109 	"PC98",
110 	g_part_pc98_methods,
111 	sizeof(struct g_part_pc98_table),
112 	.gps_entrysz = sizeof(struct g_part_pc98_entry),
113 	.gps_minent = NDOSPART,
114 	.gps_maxent = NDOSPART,
115 	.gps_bootcodesz = BOOTSIZE,
116 };
117 G_PART_SCHEME_DECLARE(g_part_pc98);
118 
119 static int
pc98_parse_type(const char * type,u_char * dp_mid,u_char * dp_sid)120 pc98_parse_type(const char *type, u_char *dp_mid, u_char *dp_sid)
121 {
122 	const char *alias;
123 	char *endp;
124 	long lt;
125 
126 	if (type[0] == '!') {
127 		lt = strtol(type + 1, &endp, 0);
128 		if (type[1] == '\0' || *endp != '\0' || lt <= 0 ||
129 		    lt >= 65536)
130 			return (EINVAL);
131 		/* Make sure the active and bootable flags aren't set. */
132 		if (lt & ((PC98_SID_ACTIVE << 8) | PC98_MID_BOOTABLE))
133 			return (ENOATTR);
134 		*dp_mid = (*dp_mid & PC98_MID_BOOTABLE) | (u_char)lt;
135 		*dp_sid = (*dp_sid & PC98_SID_ACTIVE) | (u_char)(lt >> 8);
136 		return (0);
137 	}
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;
142 		return (0);
143 	}
144 	return (EINVAL);
145 }
146 
147 static int
pc98_set_slicename(const char * label,u_char * dp_name)148 pc98_set_slicename(const char *label, u_char *dp_name)
149 {
150 	int len;
151 
152 	len = strlen(label);
153 	if (len > sizeof(((struct pc98_partition *)NULL)->dp_name))
154 		return (EINVAL);
155 	bzero(dp_name, sizeof(((struct pc98_partition *)NULL)->dp_name));
156 	strncpy(dp_name, label, len);
157 
158 	return (0);
159 }
160 
161 static void
pc98_set_chs(struct g_part_table * table,uint32_t lba,u_short * cylp,u_char * hdp,u_char * secp)162 pc98_set_chs(struct g_part_table *table, uint32_t lba, u_short *cylp,
163     u_char *hdp, u_char *secp)
164 {
165 	uint32_t cyl, hd, sec;
166 
167 	sec = lba % table->gpt_sectors + 1;
168 	lba /= table->gpt_sectors;
169 	hd = lba % table->gpt_heads;
170 	lba /= table->gpt_heads;
171 	cyl = lba;
172 
173 	*cylp = htole16(cyl);
174 	*hdp = hd;
175 	*secp = sec;
176 }
177 
178 static int
pc98_align(struct g_part_table * basetable,uint32_t * start,uint32_t * size)179 pc98_align(struct g_part_table *basetable, uint32_t *start, uint32_t *size)
180 {
181 	uint32_t cyl;
182 
183 	cyl = basetable->gpt_heads * basetable->gpt_sectors;
184 	if (*size < cyl)
185 		return (EINVAL);
186 	if (start != NULL && (*start % cyl)) {
187 		*size += (*start % cyl) - cyl;
188 		*start -= (*start % cyl) - cyl;
189 	}
190 	if (*size % cyl)
191 		*size -= (*size % cyl);
192 	if (*size < cyl)
193 		return (EINVAL);
194 	return (0);
195 }
196 
197 static int
g_part_pc98_add(struct g_part_table * basetable,struct g_part_entry * baseentry,struct g_part_parms * gpp)198 g_part_pc98_add(struct g_part_table *basetable, struct g_part_entry *baseentry,
199     struct g_part_parms *gpp)
200 {
201 	struct g_part_pc98_entry *entry;
202 	uint32_t start, size;
203 	int error;
204 
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)
209 		return (EINVAL);
210 	if (baseentry->gpe_deleted)
211 		bzero(&entry->ent, sizeof(entry->ent));
212 	else
213 		entry->ent.dp_mid = entry->ent.dp_sid = 0;
214 
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);
223 
224 	error = pc98_parse_type(gpp->gpp_type, &entry->ent.dp_mid,
225 	    &entry->ent.dp_sid);
226 	if (error)
227 		return (error);
228 
229 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
230 		return (pc98_set_slicename(gpp->gpp_label, entry->ent.dp_name));
231 
232 	return (0);
233 }
234 
235 static int
g_part_pc98_bootcode(struct g_part_table * basetable,struct g_part_parms * gpp)236 g_part_pc98_bootcode(struct g_part_table *basetable, struct g_part_parms *gpp)
237 {
238 	struct g_part_pc98_table *table;
239 	const u_char *codeptr;
240 
241 	if (gpp->gpp_codesize != BOOTSIZE)
242 		return (EINVAL);
243 
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);
248 
249 	return (0);
250 }
251 
252 static int
g_part_pc98_create(struct g_part_table * basetable,struct g_part_parms * gpp)253 g_part_pc98_create(struct g_part_table *basetable, struct g_part_parms *gpp)
254 {
255 	struct g_provider *pp;
256 	struct g_part_pc98_table *table;
257 
258 	pp = gpp->gpp_provider;
259 	if (pp->sectorsize < SECSIZE || pp->mediasize < BOOTSIZE)
260 		return (ENOSPC);
261 	if (pp->sectorsize > SECSIZE)
262 		return (ENXIO);
263 
264 	basetable->gpt_first = basetable->gpt_heads * basetable->gpt_sectors;
265 	basetable->gpt_last = MIN(pp->mediasize / SECSIZE, UINT32_MAX) - 1;
266 
267 	table = (struct g_part_pc98_table *)basetable;
268 	le16enc(table->boot + DOSMAGICOFFSET, DOSMAGIC);
269 	return (0);
270 }
271 
272 static int
g_part_pc98_destroy(struct g_part_table * basetable,struct g_part_parms * gpp)273 g_part_pc98_destroy(struct g_part_table *basetable, struct g_part_parms *gpp)
274 {
275 
276 	/* Wipe the first two sectors to clear the partitioning. */
277 	basetable->gpt_smhead |= 3;
278 	return (0);
279 }
280 
281 static void
g_part_pc98_dumpconf(struct g_part_table * table,struct g_part_entry * baseentry,struct sbuf * sb,const char * indent)282 g_part_pc98_dumpconf(struct g_part_table *table,
283     struct g_part_entry *baseentry, struct sbuf *sb, const char *indent)
284 {
285 	struct g_part_pc98_entry *entry;
286 	char name[sizeof(entry->ent.dp_name) + 1];
287 	u_int type;
288 
289 	entry = (struct g_part_pc98_entry *)baseentry;
290 	if (entry == NULL) {
291 		/* confxml: scheme information */
292 		return;
293 	}
294 
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);
301 	} else {
302 		/* confxml: partition entry information */
303 		sbuf_printf(sb, "%s<label>", indent);
304 		g_conf_printf_escaped(sb, "%s", name);
305 		sbuf_printf(sb, "</label>\n");
306 		if (entry->ent.dp_mid & PC98_MID_BOOTABLE)
307 			sbuf_printf(sb, "%s<attrib>bootable</attrib>\n",
308 			    indent);
309 		if (entry->ent.dp_sid & PC98_SID_ACTIVE)
310 			sbuf_printf(sb, "%s<attrib>active</attrib>\n", indent);
311 		sbuf_printf(sb, "%s<rawtype>%u</rawtype>\n", indent,
312 		    type & 0x7f7f);
313 	}
314 }
315 
316 static int
g_part_pc98_dumpto(struct g_part_table * table,struct g_part_entry * baseentry)317 g_part_pc98_dumpto(struct g_part_table *table, struct g_part_entry *baseentry)
318 {
319 	struct g_part_pc98_entry *entry;
320 
321 	/* Allow dumping to a FreeBSD partition only. */
322 	entry = (struct g_part_pc98_entry *)baseentry;
323 	return (((entry->ent.dp_mid & PC98_MID_MASK) == PC98_MID_386BSD &&
324 	    (entry->ent.dp_sid & PC98_SID_MASK) == PC98_SID_386BSD) ? 1 : 0);
325 }
326 
327 static int
g_part_pc98_modify(struct g_part_table * basetable,struct g_part_entry * baseentry,struct g_part_parms * gpp)328 g_part_pc98_modify(struct g_part_table *basetable,
329     struct g_part_entry *baseentry, struct g_part_parms *gpp)
330 {
331 	struct g_part_pc98_entry *entry;
332 	int error;
333 
334 	entry = (struct g_part_pc98_entry *)baseentry;
335 
336 	if (gpp->gpp_parms & G_PART_PARM_TYPE) {
337 		error = pc98_parse_type(gpp->gpp_type, &entry->ent.dp_mid,
338 		    &entry->ent.dp_sid);
339 		if (error)
340 			return (error);
341 	}
342 
343 	if (gpp->gpp_parms & G_PART_PARM_LABEL)
344 		return (pc98_set_slicename(gpp->gpp_label, entry->ent.dp_name));
345 
346 	return (0);
347 }
348 
349 static int
g_part_pc98_resize(struct g_part_table * basetable,struct g_part_entry * baseentry,struct g_part_parms * gpp)350 g_part_pc98_resize(struct g_part_table *basetable,
351     struct g_part_entry *baseentry, struct g_part_parms *gpp)
352 {
353 	struct g_part_pc98_entry *entry;
354 	struct g_provider *pp;
355 	uint32_t size;
356 
357 	size = gpp->gpp_size;
358 	if (pc98_align(basetable, NULL, &size) != 0)
359 		return (EINVAL);
360 	/* XXX: prevent unexpected shrinking. */
361 	pp = baseentry->gpe_pp;
362 	if ((g_debugflags & 0x10) == 0 && size < gpp->gpp_size &&
363 	    pp->mediasize / pp->sectorsize > size)
364 		return (EBUSY);
365 	entry = (struct g_part_pc98_entry *)baseentry;
366 	baseentry->gpe_end = baseentry->gpe_start + size - 1;
367 	pc98_set_chs(basetable, baseentry->gpe_end, &entry->ent.dp_ecyl,
368 	    &entry->ent.dp_ehd, &entry->ent.dp_esect);
369 
370 	return (0);
371 }
372 
373 static const char *
g_part_pc98_name(struct g_part_table * table,struct g_part_entry * baseentry,char * buf,size_t bufsz)374 g_part_pc98_name(struct g_part_table *table, struct g_part_entry *baseentry,
375     char *buf, size_t bufsz)
376 {
377 
378 	snprintf(buf, bufsz, "s%d", baseentry->gpe_index);
379 	return (buf);
380 }
381 
382 static int
g_part_pc98_probe(struct g_part_table * table,struct g_consumer * cp)383 g_part_pc98_probe(struct g_part_table *table, struct g_consumer *cp)
384 {
385 	struct g_provider *pp;
386 	u_char *buf, *p;
387 	int error, index, res, sum;
388 	uint16_t magic, ecyl, scyl;
389 
390 	pp = cp->provider;
391 
392 	/* Sanity-check the provider. */
393 	if (pp->sectorsize < SECSIZE || pp->mediasize < BOOTSIZE)
394 		return (ENOSPC);
395 	if (pp->sectorsize > SECSIZE)
396 		return (ENXIO);
397 
398 	/* Check that there's a PC98 partition table. */
399 	buf = g_read_data(cp, 0L, 2 * SECSIZE, &error);
400 	if (buf == NULL)
401 		return (error);
402 
403 	/* We goto out on mismatch. */
404 	res = ENXIO;
405 
406 	magic = le16dec(buf + DOSMAGICOFFSET);
407 	if (magic != DOSMAGIC)
408 		goto out;
409 
410 	sum = 0;
411 	for (index = SECSIZE; index < 2 * SECSIZE; index++)
412 		sum += buf[index];
413 	if (sum == 0) {
414 		res = G_PART_PROBE_PRI_LOW;
415 		goto out;
416 	}
417 
418 	for (index = 0; index < NDOSPART; index++) {
419 		p = buf + SECSIZE + index * DOSPARTSIZE;
420 		if (p[0] == 0 || p[1] == 0)	/* !dp_mid || !dp_sid */
421 			continue;
422 		scyl = le16dec(p + 10);
423 		ecyl = le16dec(p + 14);
424 		if (scyl == 0 || ecyl == 0)
425 			goto out;
426 		if (p[8] == p[12] &&		/* dp_ssect == dp_esect */
427 		    p[9] == p[13] &&		/* dp_shd == dp_ehd */
428 		    scyl == ecyl)
429 			goto out;
430 	}
431 
432 	res = G_PART_PROBE_PRI_HIGH;
433 
434  out:
435 	g_free(buf);
436 	return (res);
437 }
438 
439 static int
g_part_pc98_read(struct g_part_table * basetable,struct g_consumer * cp)440 g_part_pc98_read(struct g_part_table *basetable, struct g_consumer *cp)
441 {
442 	struct pc98_partition ent;
443 	struct g_provider *pp;
444 	struct g_part_pc98_table *table;
445 	struct g_part_pc98_entry *entry;
446 	u_char *buf, *p;
447 	off_t msize;
448 	off_t start, end;
449 	u_int cyl;
450 	int error, index;
451 
452 	pp = cp->provider;
453 	table = (struct g_part_pc98_table *)basetable;
454 	msize = MIN(pp->mediasize / SECSIZE, UINT32_MAX);
455 
456 	buf = g_read_data(cp, 0L, BOOTSIZE, &error);
457 	if (buf == NULL)
458 		return (error);
459 
460 	cyl = basetable->gpt_heads * basetable->gpt_sectors;
461 
462 	bcopy(buf, table->boot, sizeof(table->boot));
463 	bcopy(buf + SECSIZE, table->table, sizeof(table->table));
464 	bcopy(buf + SECSIZE*2, table->menu, sizeof(table->menu));
465 
466 	for (index = NDOSPART - 1; index >= 0; index--) {
467 		p = buf + SECSIZE + index * DOSPARTSIZE;
468 		ent.dp_mid = p[0];
469 		ent.dp_sid = p[1];
470 		ent.dp_dum1 = p[2];
471 		ent.dp_dum2 = p[3];
472 		ent.dp_ipl_sct = p[4];
473 		ent.dp_ipl_head = p[5];
474 		ent.dp_ipl_cyl = le16dec(p + 6);
475 		ent.dp_ssect = p[8];
476 		ent.dp_shd = p[9];
477 		ent.dp_scyl = le16dec(p + 10);
478 		ent.dp_esect = p[12];
479 		ent.dp_ehd = p[13];
480 		ent.dp_ecyl = le16dec(p + 14);
481 		bcopy(p + 16, ent.dp_name, sizeof(ent.dp_name));
482 		if (ent.dp_sid == 0)
483 			continue;
484 
485 		start = ent.dp_scyl * cyl;
486 		end = (ent.dp_ecyl + 1) * cyl - 1;
487 		entry = (struct g_part_pc98_entry *)g_part_new_entry(basetable,
488 		    index + 1, start, end);
489 		entry->ent = ent;
490 	}
491 
492 	basetable->gpt_entries = NDOSPART;
493 	basetable->gpt_first = cyl;
494 	basetable->gpt_last = msize - 1;
495 
496 	g_free(buf);
497 	return (0);
498 }
499 
500 static int
g_part_pc98_setunset(struct g_part_table * table,struct g_part_entry * baseentry,const char * attrib,unsigned int set)501 g_part_pc98_setunset(struct g_part_table *table, struct g_part_entry *baseentry,
502     const char *attrib, unsigned int set)
503 {
504 	struct g_part_entry *iter;
505 	struct g_part_pc98_entry *entry;
506 	int changed, mid, sid;
507 
508 	if (baseentry == NULL)
509 		return (ENODEV);
510 
511 	mid = sid = 0;
512 	if (strcasecmp(attrib, "active") == 0)
513 		sid = 1;
514 	else if (strcasecmp(attrib, "bootable") == 0)
515 		mid = 1;
516 	if (mid == 0 && sid == 0)
517 		return (EINVAL);
518 
519 	LIST_FOREACH(iter, &table->gpt_entry, gpe_entry) {
520 		if (iter->gpe_deleted)
521 			continue;
522 		if (iter != baseentry)
523 			continue;
524 		changed = 0;
525 		entry = (struct g_part_pc98_entry *)iter;
526 		if (set) {
527 			if (mid && !(entry->ent.dp_mid & PC98_MID_BOOTABLE)) {
528 				entry->ent.dp_mid |= PC98_MID_BOOTABLE;
529 				changed = 1;
530 			}
531 			if (sid && !(entry->ent.dp_sid & PC98_SID_ACTIVE)) {
532 				entry->ent.dp_sid |= PC98_SID_ACTIVE;
533 				changed = 1;
534 			}
535 		} else {
536 			if (mid && (entry->ent.dp_mid & PC98_MID_BOOTABLE)) {
537 				entry->ent.dp_mid &= ~PC98_MID_BOOTABLE;
538 				changed = 1;
539 			}
540 			if (sid && (entry->ent.dp_sid & PC98_SID_ACTIVE)) {
541 				entry->ent.dp_sid &= ~PC98_SID_ACTIVE;
542 				changed = 1;
543 			}
544 		}
545 		if (changed && !iter->gpe_created)
546 			iter->gpe_modified = 1;
547 	}
548 	return (0);
549 }
550 
551 static const char *
g_part_pc98_type(struct g_part_table * basetable,struct g_part_entry * baseentry,char * buf,size_t bufsz)552 g_part_pc98_type(struct g_part_table *basetable, struct g_part_entry *baseentry,
553     char *buf, size_t bufsz)
554 {
555 	struct g_part_pc98_entry *entry;
556 	u_int type;
557 
558 	entry = (struct g_part_pc98_entry *)baseentry;
559 	type = (entry->ent.dp_mid & PC98_MID_MASK) |
560 	    ((entry->ent.dp_sid & PC98_SID_MASK) << 8);
561 	if (type == (PC98_MID_386BSD | (PC98_SID_386BSD << 8)))
562 		return (g_part_alias_name(G_PART_ALIAS_FREEBSD));
563 	snprintf(buf, bufsz, "!%d", type);
564 	return (buf);
565 }
566 
567 static int
g_part_pc98_write(struct g_part_table * basetable,struct g_consumer * cp)568 g_part_pc98_write(struct g_part_table *basetable, struct g_consumer *cp)
569 {
570 	struct g_part_entry *baseentry;
571 	struct g_part_pc98_entry *entry;
572 	struct g_part_pc98_table *table;
573 	u_char *p;
574 	int error, index;
575 
576 	table = (struct g_part_pc98_table *)basetable;
577 	baseentry = LIST_FIRST(&basetable->gpt_entry);
578 	for (index = 1; index <= basetable->gpt_entries; index++) {
579 		p = table->table + (index - 1) * DOSPARTSIZE;
580 		entry = (baseentry != NULL && index == baseentry->gpe_index)
581 		    ? (struct g_part_pc98_entry *)baseentry : NULL;
582 		if (entry != NULL && !baseentry->gpe_deleted) {
583 			p[0] = entry->ent.dp_mid;
584 			p[1] = entry->ent.dp_sid;
585 			p[2] = entry->ent.dp_dum1;
586 			p[3] = entry->ent.dp_dum2;
587 			p[4] = entry->ent.dp_ipl_sct;
588 			p[5] = entry->ent.dp_ipl_head;
589 			le16enc(p + 6, entry->ent.dp_ipl_cyl);
590 			p[8] = entry->ent.dp_ssect;
591 			p[9] = entry->ent.dp_shd;
592 			le16enc(p + 10, entry->ent.dp_scyl);
593 			p[12] = entry->ent.dp_esect;
594 			p[13] = entry->ent.dp_ehd;
595 			le16enc(p + 14, entry->ent.dp_ecyl);
596 			bcopy(entry->ent.dp_name, p + 16,
597 			    sizeof(entry->ent.dp_name));
598 		} else
599 			bzero(p, DOSPARTSIZE);
600 
601 		if (entry != NULL)
602 			baseentry = LIST_NEXT(baseentry, gpe_entry);
603 	}
604 
605 	error = g_write_data(cp, 0, table->boot, SECSIZE);
606 	if (!error)
607 		error = g_write_data(cp, SECSIZE, table->table, SECSIZE);
608 	if (!error)
609 		error = g_write_data(cp, SECSIZE*2, table->menu, MENUSIZE);
610 	return (error);
611 }
612