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