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