1/*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
8 */
9
10#include <sys/cdefs.h>
11__FBSDID("$FreeBSD$");
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <fcntl.h>
17#include <string.h>
18#include <err.h>
19#include <sys/types.h>
20#include <sys/stat.h>
21#include <sys/ioctl.h>
22#include <sys/disklabel.h>
23#include <sys/diskpc98.h>
24#include <paths.h>
25#include "libdisk.h"
26
27/*
28 * XXX: A lot of hardcoded 512s probably should be foo->sector_size;
29 *	I'm not sure which, so I leave it like it worked before. --schweikh
30 */
31static int
32Write_FreeBSD(int fd, const struct disk *new, const struct chunk *c1)
33{
34	struct disklabel *dl;
35	int i;
36	void *p;
37	u_char buf[BBSIZE];
38
39	for (i = 0; i < BBSIZE / 512; i++) {
40		if (!(p = read_block(fd, i + c1->offset, 512)))
41			return (1);
42		memcpy(buf + 512 * i, p, 512);
43		free(p);
44	}
45	if (new->boot1)
46		memcpy(buf, new->boot1, 512);
47
48	if (new->boot2)
49		memcpy(buf + 512, new->boot2, BBSIZE - 512);
50
51	dl = (struct disklabel *)(buf + 512 * LABELSECTOR + LABELOFFSET);
52	Fill_Disklabel(dl, new, c1);
53
54	for (i = 0; i < BBSIZE / 512; i++)
55		write_block(fd, i + c1->offset, buf + 512 * i, 512);
56
57	return 0;
58}
59
60
61int
62Write_Disk(const struct disk *d1)
63{
64	int fd, i, j;
65	struct chunk *c1;
66	int ret = 0;
67	char device[64];
68	u_char *mbrblk;
69	struct pc98_partition *dp, work[NDOSPART];
70	int s[7];
71	int PC98_EntireDisk = 0;
72
73	strcpy(device, _PATH_DEV);
74        strcat(device, d1->name);
75
76	/* XXX - for entire FreeBSD(98) */
77	for (c1 = d1->chunks->part; c1; c1 = c1->next) {
78	    if ((c1->type == freebsd) || (c1->offset == 0))
79		device[9] = 0;
80	}
81
82	fd = open(device, O_RDWR);
83	if (fd < 0) {
84#ifdef DEBUG
85		warn("open(%s) failed", device);
86#endif
87		return 1;
88	}
89
90	memset(s, 0, sizeof s);
91	if (!(mbrblk = read_block(fd, 1, d1->sector_size))) {
92		close (fd);
93		return (1);
94	}
95	dp = (struct pc98_partition *)(mbrblk + DOSPARTOFF);
96	memcpy(work, dp, sizeof work);
97	dp = work;
98	free(mbrblk);
99	for (c1 = d1->chunks->part; c1; c1 = c1->next) {
100		if (c1->type == unused)
101			continue;
102		if (!strcmp(c1->name, "X"))
103			continue;
104		j = c1->name[strlen(d1->name) + 1] - '1';
105		if (j < 0 || j > 7)
106			continue;
107		s[j]++;
108		if (c1->type == freebsd)
109			ret += Write_FreeBSD(fd, d1, c1);
110
111		i = c1->offset;
112		dp[j].dp_ssect = dp[j].dp_ipl_sct = i % d1->bios_sect;
113		i -= dp[j].dp_ssect;
114		i /= d1->bios_sect;
115		dp[j].dp_shd = dp[j].dp_ipl_head = i % d1->bios_hd;
116		i -= dp[j].dp_shd;
117		i /= d1->bios_hd;
118		dp[j].dp_scyl = dp[j].dp_ipl_cyl = i;
119#ifdef DEBUG
120		printf("S:%lu = (%x/%x/%x)", c1->offset,
121		       dp[j].dp_scyl, dp[j].dp_shd, dp[j].dp_ssect);
122#endif
123
124		i = c1->end;
125#if 1
126		dp[j].dp_esect = dp[j].dp_ehd = 0;
127		dp[j].dp_ecyl = i / (d1->bios_sect * d1->bios_hd);
128#else
129		dp[j].dp_esect = i % d1->bios_sect;
130		i -= dp[j].dp_esect;
131		i /= d1->bios_sect;
132		dp[j].dp_ehd = i % d1->bios_hd;
133		i -= dp[j].dp_ehd;
134		i /= d1->bios_hd;
135		dp[j].dp_ecyl = i;
136#endif
137#ifdef DEBUG
138		printf("  E:%lu = (%x/%x/%x)\n", c1->end,
139		       dp[j].dp_ecyl, dp[j].dp_ehd, dp[j].dp_esect);
140#endif
141
142		dp[j].dp_mid = c1->subtype & 0xff;
143		dp[j].dp_sid = c1->subtype >> 8;
144		if (c1->flags & CHUNK_ACTIVE)
145			dp[j].dp_mid |= 0x80;
146
147		strncpy(dp[j].dp_name, c1->sname, 16);
148	}
149	j = 0;
150	for (i = 0; i < NDOSPART; i++) {
151		if (!s[i])
152			memset(dp + i, 0, sizeof *dp);
153	}
154
155	if (d1->bootipl)
156		write_block(fd, 0, d1->bootipl, d1->sector_size);
157
158	if (!(mbrblk = read_block(fd, 1, d1->sector_size))) {
159		close (fd);
160		return (1);
161	}
162	memcpy(mbrblk + DOSPARTOFF, dp, sizeof *dp * NDOSPART);
163	/* XXX - for entire FreeBSD(98) */
164	for (c1 = d1->chunks->part; c1; c1 = c1->next)
165		if (((c1->type == freebsd) || (c1->type == fat))
166			 && (c1->offset == 0))
167			PC98_EntireDisk = 1;
168	if (PC98_EntireDisk == 0)
169		write_block(fd, 1, mbrblk, d1->sector_size);
170
171	if (d1->bootmenu)
172		for (i = 0; i * d1->sector_size < d1->bootmenu_size; i++)
173			write_block(fd, 2 + i,
174				    &d1->bootmenu[i * d1->sector_size],
175				    d1->sector_size);
176
177	close(fd);
178	return 0;
179}
180