]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libdisk/write_i386_disk.c
Untangle #ifdefs in the write-end of things by giving each arch its
[FreeBSD/FreeBSD.git] / lib / libdisk / write_i386_disk.c
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 <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/ioctl.h>
21 #include <sys/disklabel.h>
22 #include <sys/diskslice.h>
23 #include <sys/diskmbr.h>
24 #include <paths.h>
25 #include "libdisk.h"
26
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 */
29 static int
30 Write_FreeBSD(int fd, const struct disk *new, const struct disk *old, const struct chunk *c1)
31 {
32         struct disklabel *dl;
33         int i;
34         void *p;
35         u_char buf[BBSIZE];
36
37         for(i = 0; i < BBSIZE/512; i++) {
38                 p = read_block(fd, i + c1->offset, 512);
39                 memcpy(buf + 512 * i, p, 512);
40                 free(p);
41         }
42         if(new->boot1)
43                 memcpy(buf, new->boot1, 512);
44
45         if(new->boot2)
46                 memcpy(buf + 512, new->boot2, BBSIZE-512);
47
48         dl = (struct disklabel *)(buf + 512 * LABELSECTOR + LABELOFFSET);
49         Fill_Disklabel(dl, new, old, c1);
50
51
52         for(i=0;i<BBSIZE/512;i++) {
53                 write_block(fd, i + c1->offset, buf + 512 * i, 512);
54         }
55
56         return 0;
57 }
58
59 static void
60 Write_Int32(u_int32_t *p, u_int32_t v)
61 {
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;
67 }
68
69 /*
70  * Special install-time configuration for the i386 boot0 boot manager.
71  */
72 static void
73 Cfg_Boot_Mgr(u_char *mbr, int edd)
74 {
75     if (mbr[0x1b0] == 0x66 && mbr[0x1b1] == 0xbb) {
76         if (edd)
77             mbr[0x1bb] |= 0x80; /* Packet mode on */
78         else
79             mbr[0x1bb] &= 0x7f; /* Packet mode off */
80     }
81 }
82
83 int
84 Write_Disk(const struct disk *d1)
85 {
86         int fd,i;
87         int j;
88         struct disk *old = 0;
89         struct chunk *c1;
90         int ret = 0;
91         char device[64];
92         u_char *mbr;
93         struct dos_partition *dp,work[NDOSPART];
94         int s[4];
95         int need_edd = 0;       /* Need EDD (packet interface) */
96         int one = 1;
97         int zero = 0;
98
99         strcpy(device,_PATH_DEV);
100         strcat(device,d1->name);
101
102
103         fd = open(device,O_RDWR);
104         if (fd < 0) {
105 #ifdef DEBUG
106                 warn("open(%s) failed", device);
107 #endif
108                 return 1;
109         }
110         ioctl(fd, DIOCWLABEL, &one);
111
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);
116         dp = work;
117         free(mbr);
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';
123                 if (j < 0 || j > 3)
124                         continue;
125                 s[j]++;
126                 if (c1->type == freebsd)
127                         ret += Write_FreeBSD(fd, d1, old, c1);
128
129                 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 }