2002-10-23 19:52:32 +00:00
|
|
|
/*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "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>
|
2002-10-31 05:38:48 +00:00
|
|
|
#include <err.h>
|
2002-10-23 19:52:32 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/disklabel.h>
|
|
|
|
#include <sys/diskslice.h>
|
|
|
|
#include <sys/diskpc98.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include "libdisk.h"
|
|
|
|
|
2002-11-15 13:24:29 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2002-10-23 19:52:32 +00:00
|
|
|
static int
|
2002-11-15 13:24:29 +00:00
|
|
|
Write_FreeBSD(int fd, const struct disk *new, const struct disk *old,
|
|
|
|
const struct chunk *c1)
|
2002-10-23 19:52:32 +00:00
|
|
|
{
|
|
|
|
struct disklabel *dl;
|
|
|
|
int i;
|
|
|
|
void *p;
|
|
|
|
u_char buf[BBSIZE];
|
|
|
|
|
2002-11-15 13:24:29 +00:00
|
|
|
for (i = 0; i < BBSIZE / 512; i++) {
|
2002-10-23 19:52:32 +00:00
|
|
|
p = read_block(fd, i + c1->offset, 512);
|
|
|
|
memcpy(buf + 512 * i, p, 512);
|
|
|
|
free(p);
|
|
|
|
}
|
2002-11-15 13:24:29 +00:00
|
|
|
if (new->boot1)
|
2002-10-23 19:52:32 +00:00
|
|
|
memcpy(buf, new->boot1, 512);
|
|
|
|
|
2002-11-15 13:24:29 +00:00
|
|
|
if (new->boot2)
|
|
|
|
memcpy(buf + 512, new->boot2, BBSIZE - 512);
|
2002-10-23 19:52:32 +00:00
|
|
|
|
|
|
|
dl = (struct disklabel *)(buf + 512 * LABELSECTOR + LABELOFFSET);
|
|
|
|
Fill_Disklabel(dl, new, old, c1);
|
|
|
|
|
2002-11-15 13:24:29 +00:00
|
|
|
for (i = 0; i < BBSIZE / 512; i++)
|
2002-10-23 19:52:32 +00:00
|
|
|
write_block(fd, i + c1->offset, buf + 512 * i, 512);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
Write_Disk(const struct disk *d1)
|
|
|
|
{
|
2002-11-15 13:24:29 +00:00
|
|
|
int fd, i, j;
|
|
|
|
struct disk *old = NULL;
|
2002-10-23 19:52:32 +00:00
|
|
|
struct chunk *c1;
|
|
|
|
int ret = 0;
|
|
|
|
char device[64];
|
|
|
|
u_char *mbr;
|
2003-01-04 08:50:48 +00:00
|
|
|
struct pc98_partition *dp, work[NDOSPART];
|
2002-10-23 19:52:32 +00:00
|
|
|
int s[7];
|
|
|
|
int PC98_EntireDisk = 0;
|
|
|
|
|
2002-11-15 13:24:29 +00:00
|
|
|
strcpy(device, _PATH_DEV);
|
|
|
|
strcat(device, d1->name);
|
2002-10-23 19:52:32 +00:00
|
|
|
|
|
|
|
/* XXX - for entire FreeBSD(98) */
|
|
|
|
for (c1 = d1->chunks->part; c1; c1 = c1->next) {
|
|
|
|
if ((c1->type == freebsd) || (c1->offset == 0))
|
|
|
|
device[9] = 0;
|
|
|
|
}
|
|
|
|
|
2002-11-15 13:24:29 +00:00
|
|
|
fd = open(device, O_RDWR);
|
|
|
|
if (fd < 0) {
|
2002-10-23 19:52:32 +00:00
|
|
|
#ifdef DEBUG
|
2002-11-15 13:24:29 +00:00
|
|
|
warn("open(%s) failed", device);
|
2002-10-23 19:52:32 +00:00
|
|
|
#endif
|
2002-11-15 13:24:29 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2002-10-23 19:52:32 +00:00
|
|
|
|
2002-11-15 13:24:29 +00:00
|
|
|
memset(s, 0, sizeof s);
|
2002-10-23 19:52:32 +00:00
|
|
|
mbr = read_block(fd, 1, d1->sector_size);
|
2003-01-04 08:50:48 +00:00
|
|
|
dp = (struct pc98_partition *)(mbr + DOSPARTOFF);
|
2002-10-23 19:52:32 +00:00
|
|
|
memcpy(work, dp, sizeof work);
|
|
|
|
dp = work;
|
|
|
|
free(mbr);
|
|
|
|
for (c1 = d1->chunks->part; c1; c1 = c1->next) {
|
2002-11-15 13:24:29 +00:00
|
|
|
if (c1->type == unused)
|
|
|
|
continue;
|
|
|
|
if (!strcmp(c1->name, "X"))
|
|
|
|
continue;
|
2002-10-23 19:52:32 +00:00
|
|
|
j = c1->name[strlen(d1->name) + 1] - '1';
|
|
|
|
if (j < 0 || j > 7)
|
|
|
|
continue;
|
|
|
|
s[j]++;
|
|
|
|
if (c1->type == freebsd)
|
|
|
|
ret += Write_FreeBSD(fd, d1, old, c1);
|
|
|
|
|
|
|
|
i = c1->offset;
|
|
|
|
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;
|
|
|
|
#ifdef DEBUG
|
2002-11-15 13:24:29 +00:00
|
|
|
printf("S:%lu = (%x/%x/%x)", c1->offset,
|
|
|
|
dp[j].dp_scyl, dp[j].dp_shd, dp[j].dp_ssect);
|
2002-10-23 19:52:32 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
i = c1->end;
|
|
|
|
#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;
|
2002-11-15 13:24:29 +00:00
|
|
|
dp[j].dp_ehd = i % d1->bios_hd;
|
2002-10-23 19:52:32 +00:00
|
|
|
i -= dp[j].dp_ehd;
|
|
|
|
i /= d1->bios_hd;
|
|
|
|
dp[j].dp_ecyl = i;
|
|
|
|
#endif
|
|
|
|
#ifdef DEBUG
|
2002-11-15 13:24:29 +00:00
|
|
|
printf(" E:%lu = (%x/%x/%x)\n", c1->end,
|
|
|
|
dp[j].dp_ecyl, dp[j].dp_ehd, dp[j].dp_esect);
|
2002-10-23 19:52:32 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
j = 0;
|
2002-11-15 13:24:29 +00:00
|
|
|
for (i = 0; i < NDOSPART; i++) {
|
2002-10-23 19:52:32 +00:00
|
|
|
if (!s[i])
|
|
|
|
memset(dp + i, 0, sizeof *dp);
|
|
|
|
}
|
|
|
|
|
|
|
|
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++)
|
2002-11-15 13:24:29 +00:00
|
|
|
write_block(fd, 2 + i,
|
|
|
|
&d1->bootmenu[i * d1->sector_size],
|
|
|
|
d1->sector_size);
|
2002-10-23 19:52:32 +00:00
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|