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 * ----------------------------------------------------------------------------
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.2 2003/06/17 04:26:49 dillon Exp $
19 #include <sys/types.h>
21 #include <sys/ioctl.h>
22 #include <sys/disklabel.h>
23 #include <sys/diskslice.h>
27 #define DOSPTYP_EXTENDED 5
31 #define DEF_INTERLEAVE 1
34 #define WHERE(offset,disk) (offset)
36 #define WHERE(offset,disk) (disk->flags & DISK_ON_TRACK ? offset + 63 : offset)
39 /* XXX: A lot of hardcoded 512s probably should be foo->sector_size;
40 I'm not sure which, so I leave it like it worked before. --schweikh */
42 Write_FreeBSD(int fd, struct disk *new, struct disk *old, struct chunk *c1)
53 for(i = 0; i < BBSIZE/512; i++) {
54 p = read_block(fd, WHERE(i + c1->offset, new), 512);
55 memcpy(buf + 512 * i, p, 512);
60 memcpy(buf, new->boot1, 512);
63 memcpy(buf + 512, new->boot2, BBSIZE-512);
64 #elif defined(__alpha__)
66 memcpy(buf + 512, new->boot1, BBSIZE-512);
69 dl = (struct disklabel *)(buf + 512 * LABELSECTOR + LABELOFFSET);
70 memset(dl, 0, sizeof *dl);
72 for(c2 = c1->part; c2; c2 = c2->next) {
73 if (c2->type == unused) continue;
74 if (!strcmp(c2->name, "X")) continue;
76 j = c2->name[strlen(c2->name) - 1] - 'a';
78 j = c2->name[strlen(new->name) + 2] - 'a';
80 if (j < 0 || j >= MAXPARTITIONS || j == RAW_PART) {
82 warn("weird partition letter %c", c2->name[strlen(new->name) + 2]);
86 dl->d_partitions[j].p_size = c2->size;
87 dl->d_partitions[j].p_offset = c2->offset;
88 dl->d_partitions[j].p_fstype = c2->subtype;
91 dl->d_bbsize = BBSIZE;
93 * Add in defaults for superblock size, interleave, and rpms
95 dl->d_sbsize = SBSIZE;
96 dl->d_interleave = DEF_INTERLEAVE;
99 strcpy(dl->d_typename, c1->name);
102 dl->d_secperunit = new->chunks->size;
103 dl->d_ncylinders = new->bios_cyl;
104 dl->d_ntracks = new->bios_hd;
105 dl->d_nsectors = new->bios_sect;
106 dl->d_secpercyl = dl->d_ntracks * dl->d_nsectors;
108 dl->d_npartitions = MAXPARTITIONS;
110 dl->d_type = new->name[0] == 's' || new->name[0] == 'd' ||
111 new->name[0] == 'o' ? DTYPE_SCSI : DTYPE_ESDI;
112 dl->d_partitions[RAW_PART].p_size = c1->size;
113 dl->d_partitions[RAW_PART].p_offset = c1->offset;
116 dl->d_interleave = 1;
120 if(new->flags & DISK_ON_TRACK)
121 for(i=0;i<MAXPARTITIONS;i++)
122 if (dl->d_partitions[i].p_size)
123 dl->d_partitions[i].p_offset += 63;
125 dl->d_magic = DISKMAGIC;
126 dl->d_magic2 = DISKMAGIC;
127 dl->d_checksum = dkcksum(dl);
131 * Tell SRM where the bootstrap is.
139 * Generate the bootblock checksum for the SRM console.
141 for (lp = (u_long *)buf, i = 0, sum = 0; i < 63; i++)
146 for(i=0;i<BBSIZE/512;i++) {
147 write_block(fd,WHERE(i + c1->offset, new), buf + 512 * i, 512);
154 Write_Extended(int fd, struct disk *new, struct disk *old, struct chunk *c1)
159 #if defined(__i386__) && !defined(PC98)
161 Write_Int32(u_int32_t *p, u_int32_t v)
163 u_int8_t *bp = (u_int8_t *)p;
164 bp[0] = (v >> 0) & 0xff;
165 bp[1] = (v >> 8) & 0xff;
166 bp[2] = (v >> 16) & 0xff;
167 bp[3] = (v >> 24) & 0xff;
171 #if defined(__i386__) && !defined(PC98)
173 * Special install-time configuration for the i386 boot0 boot manager.
176 Cfg_Boot_Mgr(u_char *mbr, int edd)
178 if (mbr[0x1b0] == 0x66 && mbr[0x1b1] == 0xbb) {
180 mbr[0x1bb] |= 0x80; /* Packet mode on */
182 mbr[0x1bb] &= 0x7f; /* Packet mode off */
188 Write_Disk(struct disk *d1)
194 struct disk *old = 0;
199 struct dos_partition *dp,work[NDOSPART];
202 int PC98_EntireDisk = 0;
206 int need_edd = 0; /* Need EDD (packet interface) */
212 strcpy(device,_PATH_DEV);
213 strcat(device,d1->name);
216 /* XXX - for entire FreeBSD(98) */
217 for (c1 = d1->chunks->part; c1; c1 = c1->next) {
218 if ((c1->type == freebsd) || (c1->offset == 0))
223 fd = open(device,O_RDWR);
226 warn("open(%s) failed", device);
230 ioctl(fd, DIOCWLABEL, &one);
232 memset(s,0,sizeof s);
234 mbr = read_block(fd, WHERE(1, d1), d1->sector_size);
236 mbr = read_block(fd, WHERE(0, d1), d1->sector_size);
238 dp = (struct dos_partition*)(mbr + DOSPARTOFF);
239 memcpy(work, dp, sizeof work);
242 for (c1 = d1->chunks->part; c1; c1 = c1->next) {
243 if (c1->type == unused) continue;
244 if (!strcmp(c1->name, "X")) continue;
246 j = c1->name[4] - '1';
247 j = c1->name[strlen(d1->name) + 1] - '1';
257 if (c1->type == extended)
258 ret += Write_Extended(fd, d1, old, c1);
260 if (c1->type == freebsd)
261 ret += Write_FreeBSD(fd, d1, old, c1);
265 Write_Int32(&dp[j].dp_start, c1->offset);
266 Write_Int32(&dp[j].dp_size, c1->size);
271 dp[j].dp_ssect = dp[j].dp_ipl_sct = i % d1->bios_sect;
274 dp[j].dp_shd = dp[j].dp_ipl_head = i % d1->bios_hd;
277 dp[j].dp_scyl = dp[j].dp_ipl_cyl = i;
279 if (i >= 1024*d1->bios_sect*d1->bios_hd) {
280 dp[j].dp_ssect = 0xff;
282 dp[j].dp_scyl = 0xff;
285 dp[j].dp_ssect = i % d1->bios_sect;
286 i -= dp[j].dp_ssect++;
288 dp[j].dp_shd = i % d1->bios_hd;
293 dp[j].dp_ssect |= i >> 2;
298 printf("S:%lu = (%x/%x/%x)",
299 c1->offset, dp[j].dp_scyl, dp[j].dp_shd, dp[j].dp_ssect);
305 dp[j].dp_esect = dp[j].dp_ehd = 0;
306 dp[j].dp_ecyl = i / (d1->bios_sect * d1->bios_hd);
308 dp[j].dp_esect = i % d1->bios_sect;
311 dp[j].dp_ehd = i % d1->bios_hd;
317 dp[j].dp_esect = i % d1->bios_sect;
318 i -= dp[j].dp_esect++;
320 dp[j].dp_ehd = i % d1->bios_hd;
323 if (i>1023) i = 1023;
326 dp[j].dp_esect |= i >> 2;
330 printf(" E:%lu = (%x/%x/%x)\n",
331 c1->end, dp[j].dp_ecyl, dp[j].dp_ehd, dp[j].dp_esect);
335 dp[j].dp_mid = c1->subtype & 0xff;
336 dp[j].dp_sid = c1->subtype >> 8;
337 if (c1->flags & CHUNK_ACTIVE)
338 dp[j].dp_mid |= 0x80;
340 strncpy(dp[j].dp_name, c1->sname, 16);
342 dp[j].dp_typ = c1->subtype;
343 if (c1->flags & CHUNK_ACTIVE)
344 dp[j].dp_flag = 0x80;
352 for(i = 0; i < NDOSPART; i++) {
354 memset(dp + i, 0, sizeof *dp);
362 for(i = 0; i < NDOSPART; i++)
363 if (dp[i].dp_typ == 0xa5)
364 dp[i].dp_flag = 0x80;
369 write_block(fd, WHERE(0, d1), d1->bootipl, d1->sector_size);
371 mbr = read_block(fd, WHERE(1, d1), d1->sector_size);
372 memcpy(mbr + DOSPARTOFF, dp, sizeof *dp * NDOSPART);
373 /* XXX - for entire FreeBSD(98) */
374 for (c1 = d1->chunks->part; c1; c1 = c1->next)
375 if (((c1->type == freebsd) || (c1->type == fat))
376 && (c1->offset == 0))
378 if (PC98_EntireDisk == 0)
379 write_block(fd, WHERE(1, d1), mbr, d1->sector_size);
382 for (i = 0; i * d1->sector_size < d1->bootmenu_size; i++)
383 write_block(fd, WHERE(2 + i, d1), &d1->bootmenu[i * d1->sector_size], d1->sector_size);
385 mbr = read_block(fd, WHERE(0, d1), d1->sector_size);
387 memcpy(mbr, d1->bootmgr, DOSPARTOFF);
388 Cfg_Boot_Mgr(mbr, need_edd);
390 memcpy(mbr + DOSPARTOFF, dp, sizeof *dp * NDOSPART);
393 write_block(fd, WHERE(0, d1), mbr, d1->sector_size);
394 if (d1->bootmgr && d1->bootmgr_size > d1->sector_size)
395 for(i = 1; i * d1->sector_size <= d1->bootmgr_size; i++)
396 write_block(fd, WHERE(i, d1), &d1->bootmgr[i * d1->sector_size], d1->sector_size);
401 i = ioctl(fd, DIOCSYNCSLICEINFO, &i);
404 warn("ioctl(DIOCSYNCSLICEINFO)");
406 ioctl(fd, DIOCWLABEL, &zero);