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 * ----------------------------------------------------------------------------
10 #include <sys/cdefs.h>
11 __FBSDID("$FreeBSD$");
18 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 #include <sys/disklabel.h>
22 #include <sys/diskslice.h>
23 #include <sys/diskmbr.h>
27 /* XXX: A lot of hardcoded 512s probably should be foo->sector_size;
28 I'm not sure which, so I leave it like it worked before. --schweikh */
30 Write_FreeBSD(int fd, const struct disk *new, const struct disk *old, const struct chunk *c1)
37 for(i = 0; i < BBSIZE/512; i++) {
38 p = read_block(fd, i + c1->offset, 512);
39 memcpy(buf + 512 * i, p, 512);
43 memcpy(buf, new->boot1, 512);
46 memcpy(buf + 512, new->boot2, BBSIZE-512);
48 dl = (struct disklabel *)(buf + 512 * LABELSECTOR + LABELOFFSET);
49 Fill_Disklabel(dl, new, old, c1);
52 for(i=0;i<BBSIZE/512;i++) {
53 write_block(fd, i + c1->offset, buf + 512 * i, 512);
60 Write_Int32(u_int32_t *p, u_int32_t v)
62 u_int8_t *bp = (u_int8_t *)p;
63 bp[0] = (v >> 0) & 0xff;
64 bp[1] = (v >> 8) & 0xff;
65 bp[2] = (v >> 16) & 0xff;
66 bp[3] = (v >> 24) & 0xff;
70 * Special install-time configuration for the i386 boot0 boot manager.
73 Cfg_Boot_Mgr(u_char *mbr, int edd)
75 if (mbr[0x1b0] == 0x66 && mbr[0x1b1] == 0xbb) {
77 mbr[0x1bb] |= 0x80; /* Packet mode on */
79 mbr[0x1bb] &= 0x7f; /* Packet mode off */
84 Write_Disk(const struct disk *d1)
93 struct dos_partition *dp,work[NDOSPART];
95 int need_edd = 0; /* Need EDD (packet interface) */
99 strcpy(device,_PATH_DEV);
100 strcat(device,d1->name);
103 fd = open(device,O_RDWR);
106 warn("open(%s) failed", device);
110 ioctl(fd, DIOCWLABEL, &one);
112 memset(s,0,sizeof s);
113 mbr = read_block(fd, 0, d1->sector_size);
114 dp = (struct dos_partition*)(mbr + DOSPARTOFF);
115 memcpy(work, dp, sizeof work);
118 for (c1 = d1->chunks->part; c1; c1 = c1->next) {
119 if (c1->type == unused) continue;
120 if (!strcmp(c1->name, "X")) continue;
121 j = c1->name[4] - '1';
122 j = c1->name[strlen(d1->name) + 1] - '1';
126 if (c1->type == freebsd)
127 ret += Write_FreeBSD(fd, d1, old, c1);
129 Write_Int32(&dp[j].dp_start, c1->offset);
130 Write_Int32(&dp[j].dp_size, c1->size);
133 if (i >= 1024*d1->bios_sect*d1->bios_hd) {
134 dp[j].dp_ssect = 0xff;
136 dp[j].dp_scyl = 0xff;
139 dp[j].dp_ssect = i % d1->bios_sect;
140 i -= dp[j].dp_ssect++;
142 dp[j].dp_shd = i % d1->bios_hd;
147 dp[j].dp_ssect |= i >> 2;
151 printf("S:%lu = (%x/%x/%x)",
152 c1->offset, dp[j].dp_scyl, dp[j].dp_shd, dp[j].dp_ssect);
156 dp[j].dp_esect = i % d1->bios_sect;
157 i -= dp[j].dp_esect++;
159 dp[j].dp_ehd = i % d1->bios_hd;
162 if (i>1023) i = 1023;
165 dp[j].dp_esect |= i >> 2;
168 printf(" E:%lu = (%x/%x/%x)\n",
169 c1->end, dp[j].dp_ecyl, dp[j].dp_ehd, dp[j].dp_esect);
172 dp[j].dp_typ = c1->subtype;
173 if (c1->flags & CHUNK_ACTIVE)
174 dp[j].dp_flag = 0x80;
179 for(i = 0; i < NDOSPART; i++) {
181 memset(dp + i, 0, sizeof *dp);
186 for(i = 0; i < NDOSPART; i++)
187 if (dp[i].dp_typ == 0xa5)
188 dp[i].dp_flag = 0x80;
190 mbr = read_block(fd, 0, d1->sector_size);
192 memcpy(mbr, d1->bootmgr, DOSPARTOFF);
193 Cfg_Boot_Mgr(mbr, need_edd);
195 memcpy(mbr + DOSPARTOFF, dp, sizeof *dp * NDOSPART);
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);
204 i = ioctl(fd, DIOCSYNCSLICEINFO, &i);
207 warn("ioctl(DIOCSYNCSLICEINFO)");
209 ioctl(fd, DIOCWLABEL, &zero);