Import BSD-licensed crtbegin/crtend support.
[dragonfly.git] / lib / libdisk / write_disk.c
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@login.dknet.dk> 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  * $FreeBSD: src/lib/libdisk/write_disk.c,v 1.28.2.10 2001/05/13 21:01:38 jkh Exp $
10  * $DragonFly: src/lib/libdisk/Attic/write_disk.c,v 1.3 2003/11/10 06:14:40 dillon Exp $
11  *
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <string.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/ioctl.h>
22 #include <sys/disklabel.h>
23 #include <sys/diskslice.h>
24 #include <sys/diskmbr.h>
25 #include <paths.h>
26 #include "libdisk.h"
27
28 #define DOSPTYP_EXTENDED        5
29 #define BBSIZE                  8192
30 #define SBSIZE                  8192
31 #define DEF_RPM                 3600
32 #define DEF_INTERLEAVE  1
33
34 #ifdef PC98
35 #define WHERE(offset,disk) (offset)
36 #else
37 #define WHERE(offset,disk) (disk->flags & DISK_ON_TRACK ? offset + 63 : offset)
38 #endif
39
40 /* XXX: A lot of hardcoded 512s probably should be foo->sector_size;
41         I'm not sure which, so I leave it like it worked before. --schweikh */
42 int
43 Write_FreeBSD(int fd, struct disk *new, struct disk *old, struct chunk *c1)
44 {
45         struct disklabel *dl;
46         struct chunk *c2;
47         int i,j;
48         void *p;
49         u_char buf[BBSIZE];
50 #ifdef __alpha__
51         u_long *lp, sum;
52 #endif
53
54         for(i = 0; i < BBSIZE/512; i++) {
55                 p = read_block(fd, WHERE(i + c1->offset, new), 512);
56                 memcpy(buf + 512 * i, p, 512);
57                 free(p);
58         }
59 #if defined(__i386__)
60         if(new->boot1)
61                 memcpy(buf, new->boot1, 512);
62
63         if(new->boot2)
64                 memcpy(buf + 512, new->boot2, BBSIZE-512);
65 #elif defined(__alpha__)
66         if(new->boot1)
67                 memcpy(buf + 512, new->boot1, BBSIZE-512);
68 #endif
69
70         dl = (struct disklabel *)(buf + 512 * LABELSECTOR + LABELOFFSET);
71         memset(dl, 0, sizeof *dl);
72
73         for(c2 = c1->part; c2; c2 = c2->next) {
74                 if (c2->type == unused) continue;
75                 if (!strcmp(c2->name, "X")) continue;
76 #ifdef __alpha__
77                 j = c2->name[strlen(c2->name) - 1] - 'a';
78 #else
79                 j = c2->name[strlen(new->name) + 2] - 'a';
80 #endif
81                 if (j < 0 || j >= MAXPARTITIONS || j == RAW_PART) {
82 #ifdef DEBUG
83                         warn("weird partition letter %c", c2->name[strlen(new->name) + 2]);
84 #endif
85                         continue;
86                 }
87                 dl->d_partitions[j].p_size = c2->size;
88                 dl->d_partitions[j].p_offset = c2->offset;
89                 dl->d_partitions[j].p_fstype = c2->subtype;
90         }
91
92         dl->d_bbsize = BBSIZE;
93         /*
94          * Add in defaults for superblock size, interleave, and rpms
95          */
96         dl->d_sbsize = SBSIZE;
97         dl->d_interleave = DEF_INTERLEAVE;
98         dl->d_rpm = DEF_RPM;
99
100         strcpy(dl->d_typename, c1->name);
101
102         dl->d_secsize = 512;
103         dl->d_secperunit = new->chunks->size;
104         dl->d_ncylinders =  new->bios_cyl;
105         dl->d_ntracks =  new->bios_hd;
106         dl->d_nsectors =  new->bios_sect;
107         dl->d_secpercyl = dl->d_ntracks * dl->d_nsectors;
108
109         dl->d_npartitions = MAXPARTITIONS;
110
111         dl->d_type = new->name[0] == 's' || new->name[0] == 'd' ||
112             new->name[0] == 'o' ? DTYPE_SCSI : DTYPE_ESDI;
113         dl->d_partitions[RAW_PART].p_size = c1->size;
114         dl->d_partitions[RAW_PART].p_offset = c1->offset;
115 #ifdef PC98
116         dl->d_rpm = 3600;
117         dl->d_interleave = 1;
118 #endif
119
120 #ifndef PC98
121         if(new->flags & DISK_ON_TRACK)
122                 for(i=0;i<MAXPARTITIONS;i++)
123                         if (dl->d_partitions[i].p_size)
124                                 dl->d_partitions[i].p_offset += 63;
125 #endif
126         dl->d_magic = DISKMAGIC;
127         dl->d_magic2 = DISKMAGIC;
128         dl->d_checksum = dkcksum(dl);
129
130 #ifdef __alpha__
131         /*
132          * Tell SRM where the bootstrap is.
133          */
134         lp = (u_long *)buf;
135         lp[60] = 15;
136         lp[61] = 1;
137         lp[62] = 0;
138
139         /*
140          * Generate the bootblock checksum for the SRM console.
141          */
142         for (lp = (u_long *)buf, i = 0, sum = 0; i < 63; i++)
143             sum += lp[i];
144         lp[63] = sum;
145 #endif /*__alpha__*/
146
147         for(i=0;i<BBSIZE/512;i++) {
148                 write_block(fd,WHERE(i + c1->offset, new), buf + 512 * i, 512);
149         }
150
151         return 0;
152 }
153
154 int
155 Write_Extended(int fd, struct disk *new, struct disk *old, struct chunk *c1)
156 {
157         return 0;
158 }
159
160 #if defined(__i386__) && !defined(PC98)
161 static void
162 Write_Int32(u_int32_t *p, u_int32_t v)
163 {
164     u_int8_t *bp = (u_int8_t *)p;
165     bp[0] = (v >> 0) & 0xff;
166     bp[1] = (v >> 8) & 0xff;
167     bp[2] = (v >> 16) & 0xff;
168     bp[3] = (v >> 24) & 0xff;
169 }
170 #endif
171
172 #if defined(__i386__) && !defined(PC98)
173 /*
174  * Special install-time configuration for the i386 boot0 boot manager.
175  */
176 static void
177 Cfg_Boot_Mgr(u_char *mbr, int edd)
178 {
179     if (mbr[0x1b0] == 0x66 && mbr[0x1b1] == 0xbb) {
180         if (edd)
181             mbr[0x1bb] |= 0x80; /* Packet mode on */
182         else
183             mbr[0x1bb] &= 0x7f; /* Packet mode off */
184     }
185 }
186 #endif
187
188 int
189 Write_Disk(struct disk *d1)
190 {
191         int fd,i;
192 #ifdef __i386__
193         int j;
194 #endif
195         struct disk *old = 0;
196         struct chunk *c1;
197         int ret = 0;
198         char device[64];
199         u_char *mbr;
200         struct dos_partition *dp,work[NDOSPART];
201 #ifdef PC98
202         int s[7];
203         int PC98_EntireDisk = 0;
204 #else
205         int s[4];
206 #ifdef __i386__
207         int need_edd = 0;       /* Need EDD (packet interface) */
208 #endif
209 #endif
210         int one = 1;
211         int zero = 0;
212
213         strcpy(device,_PATH_DEV);
214         strcat(device,d1->name);
215
216 #ifdef PC98
217         /* XXX - for entire FreeBSD(98) */
218         for (c1 = d1->chunks->part; c1; c1 = c1->next) {
219             if ((c1->type == freebsd) || (c1->offset == 0))
220                 device[9] = 0;
221         }
222 #endif
223
224         fd = open(device,O_RDWR);
225         if (fd < 0) {
226 #ifdef DEBUG
227                 warn("open(%s) failed", device);
228 #endif
229                 return 1;
230         }
231         ioctl(fd, DIOCWLABEL, &one);
232
233         memset(s,0,sizeof s);
234 #ifdef PC98
235         mbr = read_block(fd, WHERE(1, d1), d1->sector_size);
236 #else
237         mbr = read_block(fd, WHERE(0, d1), d1->sector_size);
238 #endif
239         dp = (struct dos_partition*)(mbr + DOSPARTOFF);
240         memcpy(work, dp, sizeof work);
241         dp = work;
242         free(mbr);
243         for (c1 = d1->chunks->part; c1; c1 = c1->next) {
244                 if (c1->type == unused) continue;
245                 if (!strcmp(c1->name, "X")) continue;
246 #ifdef __i386__
247                 j = c1->name[4] - '1';
248                 j = c1->name[strlen(d1->name) + 1] - '1';
249 #ifdef PC98
250                 if (j < 0 || j > 7)
251 #else
252                 if (j < 0 || j > 3)
253 #endif
254                         continue;
255                 s[j]++;
256 #endif
257 #ifndef PC98
258                 if (c1->type == extended)
259                         ret += Write_Extended(fd, d1, old, c1);
260 #endif
261                 if (c1->type == freebsd)
262                         ret += Write_FreeBSD(fd, d1, old, c1);
263
264 #ifdef __i386__
265 #ifndef PC98
266                 Write_Int32(&dp[j].dp_start, c1->offset);
267                 Write_Int32(&dp[j].dp_size, c1->size);
268 #endif
269
270                 i = c1->offset;
271 #ifdef PC98
272                 dp[j].dp_ssect = dp[j].dp_ipl_sct = i % d1->bios_sect;
273                 i -= dp[j].dp_ssect;
274                 i /= d1->bios_sect;
275                 dp[j].dp_shd = dp[j].dp_ipl_head = i % d1->bios_hd;
276                 i -= dp[j].dp_shd;
277                 i /= d1->bios_hd;
278                 dp[j].dp_scyl = dp[j].dp_ipl_cyl = i;
279 #else
280                 if (i >= 1024*d1->bios_sect*d1->bios_hd) {
281                         dp[j].dp_ssect = 0xff;
282                         dp[j].dp_shd = 0xff;
283                         dp[j].dp_scyl = 0xff;
284                         need_edd++;
285                 } else {
286                         dp[j].dp_ssect = i % d1->bios_sect;
287                         i -= dp[j].dp_ssect++;
288                         i /= d1->bios_sect;
289                         dp[j].dp_shd =  i % d1->bios_hd;
290                         i -= dp[j].dp_shd;
291                         i /= d1->bios_hd;
292                         dp[j].dp_scyl = i;
293                         i -= dp[j].dp_scyl;
294                         dp[j].dp_ssect |= i >> 2;
295                 }
296 #endif /* PC98 */
297
298 #ifdef DEBUG
299                 printf("S:%lu = (%x/%x/%x)",
300                         c1->offset, dp[j].dp_scyl, dp[j].dp_shd, dp[j].dp_ssect);
301 #endif
302
303                 i = c1->end;
304 #ifdef PC98
305 #if 1
306                 dp[j].dp_esect = dp[j].dp_ehd = 0;
307                 dp[j].dp_ecyl = i / (d1->bios_sect * d1->bios_hd);
308 #else
309                 dp[j].dp_esect = i % d1->bios_sect;
310                 i -= dp[j].dp_esect;
311                 i /= d1->bios_sect;
312                 dp[j].dp_ehd =  i % d1->bios_hd;
313                 i -= dp[j].dp_ehd;
314                 i /= d1->bios_hd;
315                 dp[j].dp_ecyl = i;
316 #endif
317 #else
318                 dp[j].dp_esect = i % d1->bios_sect;
319                 i -= dp[j].dp_esect++;
320                 i /= d1->bios_sect;
321                 dp[j].dp_ehd =  i % d1->bios_hd;
322                 i -= dp[j].dp_ehd;
323                 i /= d1->bios_hd;
324                 if (i>1023) i = 1023;
325                 dp[j].dp_ecyl = i;
326                 i -= dp[j].dp_ecyl;
327                 dp[j].dp_esect |= i >> 2;
328 #endif
329
330 #ifdef DEBUG
331                 printf("  E:%lu = (%x/%x/%x)\n",
332                         c1->end, dp[j].dp_ecyl, dp[j].dp_ehd, dp[j].dp_esect);
333 #endif
334
335 #ifdef PC98
336                 dp[j].dp_mid = c1->subtype & 0xff;
337                 dp[j].dp_sid = c1->subtype >> 8;
338                 if (c1->flags & CHUNK_ACTIVE)
339                         dp[j].dp_mid |= 0x80;
340
341                 strncpy(dp[j].dp_name, c1->sname, 16);
342 #else
343                 dp[j].dp_typ = c1->subtype;
344                 if (c1->flags & CHUNK_ACTIVE)
345                         dp[j].dp_flag = 0x80;
346                 else
347                         dp[j].dp_flag = 0;
348 #endif
349 #endif
350         }
351 #ifdef __i386__
352         j = 0;
353         for(i = 0; i < NDOSPART; i++) {
354                 if (!s[i])
355                         memset(dp + i, 0, sizeof *dp);
356 #ifndef PC98
357                 if (dp[i].dp_flag)
358                         j++;
359 #endif
360         }
361 #ifndef PC98
362         if (!j)
363                 for(i = 0; i < NDOSPART; i++)
364                         if (dp[i].dp_typ == 0xa5)
365                                 dp[i].dp_flag = 0x80;
366 #endif
367
368 #ifdef PC98
369         if (d1->bootipl)
370                 write_block(fd, WHERE(0, d1), d1->bootipl, d1->sector_size);
371
372         mbr = read_block(fd, WHERE(1, d1), d1->sector_size);
373         memcpy(mbr + DOSPARTOFF, dp, sizeof *dp * NDOSPART);
374         /* XXX - for entire FreeBSD(98) */
375         for (c1 = d1->chunks->part; c1; c1 = c1->next)
376                 if (((c1->type == freebsd) || (c1->type == fat))
377                          && (c1->offset == 0))
378                         PC98_EntireDisk = 1;
379         if (PC98_EntireDisk == 0)
380                 write_block(fd, WHERE(1, d1), mbr, d1->sector_size);
381
382         if (d1->bootmenu)
383                 for (i = 0; i * d1->sector_size < d1->bootmenu_size; i++)
384                         write_block(fd, WHERE(2 + i, d1), &d1->bootmenu[i * d1->sector_size], d1->sector_size);
385 #else
386         mbr = read_block(fd, WHERE(0, d1), d1->sector_size);
387         if (d1->bootmgr) {
388                 memcpy(mbr, d1->bootmgr, DOSPARTOFF);
389                 Cfg_Boot_Mgr(mbr, need_edd);
390         }
391         memcpy(mbr + DOSPARTOFF, dp, sizeof *dp * NDOSPART);
392         mbr[512-2] = 0x55;
393         mbr[512-1] = 0xaa;
394         write_block(fd, WHERE(0, d1), mbr, d1->sector_size);
395         if (d1->bootmgr && d1->bootmgr_size > d1->sector_size)
396           for(i = 1; i * d1->sector_size <= d1->bootmgr_size; i++)
397             write_block(fd, WHERE(i, d1), &d1->bootmgr[i * d1->sector_size], d1->sector_size);
398 #endif
399 #endif
400
401         i = 1;
402         i = ioctl(fd, DIOCSYNCSLICEINFO, &i);
403 #ifdef DEBUG
404         if (i != 0)
405                 warn("ioctl(DIOCSYNCSLICEINFO)");
406 #endif
407         ioctl(fd, DIOCWLABEL, &zero);
408         close(fd);
409         return 0;
410 }