write_ia64_disk.c revision 105816
195065Strhodes/*
295065Strhodes * ----------------------------------------------------------------------------
395065Strhodes * "THE BEER-WARE LICENSE" (Revision 42):
495065Strhodes * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
595065Strhodes * can do whatever you want with this stuff. If we meet some day, and you think
695065Strhodes * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
795065Strhodes * ----------------------------------------------------------------------------
895065Strhodes */
995065Strhodes
1095065Strhodes#include <sys/cdefs.h>
1195065Strhodes__FBSDID("$FreeBSD: head/lib/libdisk/write_ia64_disk.c 105816 2002-10-23 19:52:32Z phk $");
1295065Strhodes
1395065Strhodes#include <stdio.h>
1495065Strhodes#include <stdlib.h>
1595065Strhodes#include <unistd.h>
1695065Strhodes#include <fcntl.h>
1795065Strhodes#include <string.h>
1895065Strhodes#include <sys/types.h>
1995065Strhodes#include <sys/stat.h>
2095065Strhodes#include <sys/ioctl.h>
2195065Strhodes#include <sys/disklabel.h>
2295065Strhodes#include <sys/diskslice.h>
2395065Strhodes#include <sys/diskmbr.h>
2495065Strhodes#include <paths.h>
2595065Strhodes#include "libdisk.h"
2695065Strhodes
2795065Strhodes/* XXX: A lot of hardcoded 512s probably should be foo->sector_size;
2895065Strhodes        I'm not sure which, so I leave it like it worked before. --schweikh */
2995065Strhodesstatic int
3097593SruWrite_FreeBSD(int fd, const struct disk *new, const struct disk *old, const struct chunk *c1)
3197593Sru{
32318493Strasz	struct disklabel *dl;
3395065Strhodes	int i;
3495065Strhodes	void *p;
3595065Strhodes	u_char buf[BBSIZE];
3695065Strhodes
3795065Strhodes	for(i = 0; i < BBSIZE/512; i++) {
3895065Strhodes		p = read_block(fd, i + c1->offset, 512);
39164524Sbrueffer		memcpy(buf + 512 * i, p, 512);
40164524Sbrueffer		free(p);
41164524Sbrueffer	}
42164524Sbrueffer	if(new->boot1)
43227750Smiwi		memcpy(buf, new->boot1, 512);
44227750Smiwi
45131034Simp	if(new->boot2)
46164524Sbrueffer		memcpy(buf + 512, new->boot2, BBSIZE-512);
47164524Sbrueffer
48164524Sbrueffer	dl = (struct disklabel *)(buf + 512 * LABELSECTOR + LABELOFFSET);
49164524Sbrueffer	Fill_Disklabel(dl, new, old, c1);
50164524Sbrueffer
51164524Sbrueffer
52164524Sbrueffer	for(i=0;i<BBSIZE/512;i++) {
53164524Sbrueffer		write_block(fd, i + c1->offset, buf + 512 * i, 512);
5495065Strhodes	}
5595065Strhodes
5695065Strhodes	return 0;
5795065Strhodes}
5895065Strhodes
5995065Strhodesstatic void
6097593SruWrite_Int32(u_int32_t *p, u_int32_t v)
6197593Sru{
6295065Strhodes    u_int8_t *bp = (u_int8_t *)p;
6397593Sru    bp[0] = (v >> 0) & 0xff;
6497593Sru    bp[1] = (v >> 8) & 0xff;
65134738Sbrueffer    bp[2] = (v >> 16) & 0xff;
6695065Strhodes    bp[3] = (v >> 24) & 0xff;
6795065Strhodes}
6895065Strhodes
6995065Strhodes/*
7095065Strhodes * Special install-time configuration for the i386 boot0 boot manager.
71134046Ssimon */
72134046Ssimonstatic void
73134046SsimonCfg_Boot_Mgr(u_char *mbr, int edd)
74134046Ssimon{
75134046Ssimon    if (mbr[0x1b0] == 0x66 && mbr[0x1b1] == 0xbb) {
76134046Ssimon	if (edd)
77134046Ssimon	    mbr[0x1bb] |= 0x80;	/* Packet mode on */
78134046Ssimon	else
79134046Ssimon	    mbr[0x1bb] &= 0x7f;	/* Packet mode off */
80159745Snetchild    }
81159745Snetchild}
82171910Ssanpei
83171910Ssanpeiint
84171910SsanpeiWrite_Disk(const struct disk *d1)
85171910Ssanpei{
86134046Ssimon	int fd,i;
87134046Ssimon	int j;
88159745Snetchild	struct disk *old = 0;
89159745Snetchild	struct chunk *c1;
90134046Ssimon	int ret = 0;
91149230Skeramida	char device[64];
92149236Skeramida	u_char *mbr;
93184182Sn_hibma	struct dos_partition *dp,work[NDOSPART];
94184182Sn_hibma	int s[4];
95239090Sglebius	int need_edd = 0;	/* Need EDD (packet interface) */
96239090Sglebius	int one = 1;
97134046Ssimon	int zero = 0;
98318493Strasz
99318493Strasz	strcpy(device,_PATH_DEV);
100318493Strasz        strcat(device,d1->name);
101318493Strasz
102318493Strasz
103318493Strasz        fd = open(device,O_RDWR);
104318493Strasz        if (fd < 0) {
105318493Strasz#ifdef DEBUG
106318493Strasz                warn("open(%s) failed", device);
107318493Strasz#endif
108318493Strasz                return 1;
109318493Strasz        }
110318493Strasz	ioctl(fd, DIOCWLABEL, &one);
111318493Strasz
11295065Strhodes	memset(s,0,sizeof s);
11395065Strhodes	mbr = read_block(fd, 0, d1->sector_size);
11495065Strhodes	dp = (struct dos_partition*)(mbr + DOSPARTOFF);
11595065Strhodes	memcpy(work, dp, sizeof work);
11695065Strhodes	dp = work;
11795065Strhodes	free(mbr);
11895065Strhodes	for (c1 = d1->chunks->part; c1; c1 = c1->next) {
11995065Strhodes		if (c1->type == unused) continue;
12095065Strhodes		if (!strcmp(c1->name, "X")) continue;
12195065Strhodes		j = c1->name[4] - '1';
12295065Strhodes		j = c1->name[strlen(d1->name) + 1] - '1';
12395065Strhodes		if (j < 0 || j > 3)
12497593Sru			continue;
125267938Sbapt		s[j]++;
12695065Strhodes		if (c1->type == freebsd)
12795065Strhodes			ret += Write_FreeBSD(fd, d1, old, c1);
12895065Strhodes
12995065Strhodes		Write_Int32(&dp[j].dp_start, c1->offset);
130		Write_Int32(&dp[j].dp_size, c1->size);
131
132		i = c1->offset;
133		if (i >= 1024*d1->bios_sect*d1->bios_hd) {
134			dp[j].dp_ssect = 0xff;
135			dp[j].dp_shd = 0xff;
136			dp[j].dp_scyl = 0xff;
137			need_edd++;
138		} else {
139			dp[j].dp_ssect = i % d1->bios_sect;
140			i -= dp[j].dp_ssect++;
141			i /= d1->bios_sect;
142			dp[j].dp_shd =  i % d1->bios_hd;
143			i -= dp[j].dp_shd;
144			i /= d1->bios_hd;
145			dp[j].dp_scyl = i;
146			i -= dp[j].dp_scyl;
147			dp[j].dp_ssect |= i >> 2;
148		}
149
150#ifdef DEBUG
151		printf("S:%lu = (%x/%x/%x)",
152			c1->offset, dp[j].dp_scyl, dp[j].dp_shd, dp[j].dp_ssect);
153#endif
154
155		i = c1->end;
156		dp[j].dp_esect = i % d1->bios_sect;
157		i -= dp[j].dp_esect++;
158		i /= d1->bios_sect;
159		dp[j].dp_ehd =  i % d1->bios_hd;
160		i -= dp[j].dp_ehd;
161		i /= d1->bios_hd;
162		if (i>1023) i = 1023;
163		dp[j].dp_ecyl = i;
164		i -= dp[j].dp_ecyl;
165		dp[j].dp_esect |= i >> 2;
166
167#ifdef DEBUG
168		printf("  E:%lu = (%x/%x/%x)\n",
169			c1->end, dp[j].dp_ecyl, dp[j].dp_ehd, dp[j].dp_esect);
170#endif
171
172		dp[j].dp_typ = c1->subtype;
173		if (c1->flags & CHUNK_ACTIVE)
174			dp[j].dp_flag = 0x80;
175		else
176			dp[j].dp_flag = 0;
177	}
178	j = 0;
179	for(i = 0; i < NDOSPART; i++) {
180		if (!s[i])
181			memset(dp + i, 0, sizeof *dp);
182		if (dp[i].dp_flag)
183			j++;
184	}
185	if (!j)
186		for(i = 0; i < NDOSPART; i++)
187			if (dp[i].dp_typ == 0xa5)
188				dp[i].dp_flag = 0x80;
189
190	mbr = read_block(fd, 0, d1->sector_size);
191	if (d1->bootmgr) {
192		memcpy(mbr, d1->bootmgr, DOSPARTOFF);
193		Cfg_Boot_Mgr(mbr, need_edd);
194        }
195	memcpy(mbr + DOSPARTOFF, dp, sizeof *dp * NDOSPART);
196	mbr[512-2] = 0x55;
197	mbr[512-1] = 0xaa;
198	write_block(fd, 0, mbr, d1->sector_size);
199	if (d1->bootmgr && d1->bootmgr_size > d1->sector_size)
200	  for(i = 1; i * d1->sector_size <= d1->bootmgr_size; i++)
201	    write_block(fd, i, &d1->bootmgr[i * d1->sector_size], d1->sector_size);
202
203	i = 1;
204	i = ioctl(fd, DIOCSYNCSLICEINFO, &i);
205#ifdef DEBUG
206	if (i != 0)
207		warn("ioctl(DIOCSYNCSLICEINFO)");
208#endif
209	ioctl(fd, DIOCWLABEL, &zero);
210	close(fd);
211	return 0;
212}
213