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>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/disklabel.h>
|
|
|
|
#include <sys/diskmbr.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-10 20:47:02 +00:00
|
|
|
Write_FreeBSD(int fd, const struct disk *new, 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-10 20:47:02 +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);
|
2002-11-10 20:47:02 +00:00
|
|
|
if (p == NULL)
|
|
|
|
return (1);
|
2002-10-23 19:52:32 +00:00
|
|
|
memcpy(buf + 512 * i, p, 512);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
dl = (struct disklabel *)(buf + 512 * LABELSECTOR + LABELOFFSET);
|
2003-02-04 17:35:23 +00:00
|
|
|
Fill_Disklabel(dl, new, c1);
|
2002-10-23 19:52:32 +00:00
|
|
|
|
2002-11-15 13:24:29 +00:00
|
|
|
for (i = 0; i < BBSIZE / 512; i++) {
|
2002-11-10 20:47:02 +00:00
|
|
|
if (write_block(fd, i + c1->offset, buf + 512 * i, 512) != 0)
|
|
|
|
return (1);
|
2002-10-23 19:52:32 +00:00
|
|
|
}
|
|
|
|
|
2002-11-10 20:47:02 +00:00
|
|
|
return (0);
|
2002-10-23 19:52:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Write_Int32(u_int32_t *p, u_int32_t v)
|
|
|
|
{
|
2002-11-10 20:47:02 +00:00
|
|
|
u_int8_t *bp = (u_int8_t *)p;
|
2002-11-15 13:24:29 +00:00
|
|
|
|
2002-11-10 20:47:02 +00:00
|
|
|
bp[0] = (v >> 0) & 0xff;
|
|
|
|
bp[1] = (v >> 8) & 0xff;
|
|
|
|
bp[2] = (v >> 16) & 0xff;
|
|
|
|
bp[3] = (v >> 24) & 0xff;
|
2002-10-23 19:52:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Write_Disk(const struct disk *d1)
|
|
|
|
{
|
|
|
|
struct chunk *c1;
|
|
|
|
char device[64];
|
|
|
|
u_char *mbr;
|
2002-11-10 20:47:02 +00:00
|
|
|
struct dos_partition *dp, work[NDOSPART];
|
2002-10-23 19:52:32 +00:00
|
|
|
int s[4];
|
2002-11-10 20:47:02 +00:00
|
|
|
int fd, i, j, ret;
|
2002-10-23 19:52:32 +00:00
|
|
|
|
2002-11-10 20:47:02 +00:00
|
|
|
strcpy(device, _PATH_DEV);
|
|
|
|
strcat(device, d1->name);
|
2002-10-23 19:52:32 +00:00
|
|
|
|
2002-11-15 13:24:29 +00:00
|
|
|
fd = open(device, O_RDWR);
|
2002-10-23 21:05:42 +00:00
|
|
|
if (fd < 0)
|
2002-11-10 20:47:02 +00:00
|
|
|
return (1);
|
2002-10-23 19:52:32 +00:00
|
|
|
|
2002-11-10 20:47:02 +00:00
|
|
|
memset(s, 0, sizeof(s));
|
|
|
|
mbr = read_block(fd, 0, 512);
|
|
|
|
if (mbr == NULL)
|
|
|
|
goto fail;
|
2002-11-15 13:24:29 +00:00
|
|
|
dp = (struct dos_partition *)(mbr + DOSPARTOFF);
|
2002-11-10 20:47:02 +00:00
|
|
|
memcpy(work, dp, sizeof(work));
|
2002-10-23 19:52:32 +00:00
|
|
|
dp = work;
|
|
|
|
free(mbr);
|
2002-11-10 20:47:02 +00:00
|
|
|
|
|
|
|
ret = 0;
|
2002-10-23 19:52:32 +00:00
|
|
|
for (c1 = d1->chunks->part; c1; c1 = c1->next) {
|
2002-11-10 20:47:02 +00:00
|
|
|
if (c1->type == unused || !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 > 3)
|
|
|
|
continue;
|
|
|
|
s[j]++;
|
|
|
|
if (c1->type == freebsd)
|
2002-11-10 20:47:02 +00:00
|
|
|
ret += Write_FreeBSD(fd, d1, c1);
|
2002-10-23 19:52:32 +00:00
|
|
|
|
|
|
|
Write_Int32(&dp[j].dp_start, c1->offset);
|
|
|
|
Write_Int32(&dp[j].dp_size, c1->size);
|
|
|
|
|
2002-11-15 13:24:29 +00:00
|
|
|
i = 1024 * d1->bios_sect * d1->bios_hd;
|
2002-11-10 20:47:02 +00:00
|
|
|
if (i == 0 || c1->offset >= i) {
|
|
|
|
/* Start */
|
2002-10-23 19:52:32 +00:00
|
|
|
dp[j].dp_ssect = 0xff;
|
|
|
|
dp[j].dp_shd = 0xff;
|
|
|
|
dp[j].dp_scyl = 0xff;
|
2002-11-10 20:47:02 +00:00
|
|
|
|
|
|
|
/* End */
|
|
|
|
dp[j].dp_esect = 0xff;
|
|
|
|
dp[j].dp_ehd = 0xff;
|
|
|
|
dp[j].dp_ecyl = 0xff;
|
2002-10-23 19:52:32 +00:00
|
|
|
} else {
|
2002-11-10 20:47:02 +00:00
|
|
|
/* Start */
|
|
|
|
i = c1->offset;
|
2002-10-23 19:52:32 +00:00
|
|
|
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;
|
|
|
|
|
2002-11-10 20:47:02 +00:00
|
|
|
/* End */
|
|
|
|
i = c1->end;
|
|
|
|
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;
|
|
|
|
i -= dp[j].dp_ecyl;
|
|
|
|
dp[j].dp_esect |= i >> 2;
|
|
|
|
}
|
2002-10-23 19:52:32 +00:00
|
|
|
#ifdef DEBUG
|
2002-11-10 20:47:02 +00:00
|
|
|
printf("S:%lu = (%x/%x/%x) E:%lu = (%x/%x/%x)\n", c1->offset,
|
2002-11-15 13:24:29 +00:00
|
|
|
dp[j].dp_scyl, dp[j].dp_shd, dp[j].dp_ssect, 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_typ = c1->subtype;
|
|
|
|
if (c1->flags & CHUNK_ACTIVE)
|
|
|
|
dp[j].dp_flag = 0x80;
|
|
|
|
else
|
|
|
|
dp[j].dp_flag = 0;
|
|
|
|
}
|
2002-11-10 20:47:02 +00:00
|
|
|
|
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])
|
2002-11-10 20:47:02 +00:00
|
|
|
memset(dp + i, 0, sizeof(*dp));
|
2002-10-23 19:52:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mbr = read_block(fd, 0, d1->sector_size);
|
2002-11-10 20:47:02 +00:00
|
|
|
if (mbr == NULL)
|
|
|
|
goto fail;
|
|
|
|
memcpy(mbr + DOSPARTOFF, dp, sizeof(*dp) * NDOSPART);
|
2002-10-23 19:52:32 +00:00
|
|
|
mbr[512-2] = 0x55;
|
|
|
|
mbr[512-1] = 0xaa;
|
2002-11-10 20:47:02 +00:00
|
|
|
ret += write_block(fd, 0, mbr, 512);
|
|
|
|
free(mbr);
|
|
|
|
if (ret != 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return (0);
|
2002-10-23 19:52:32 +00:00
|
|
|
|
2002-11-10 20:47:02 +00:00
|
|
|
fail:
|
2002-10-23 19:52:32 +00:00
|
|
|
close(fd);
|
2002-11-10 20:47:02 +00:00
|
|
|
return (1);
|
2002-10-23 19:52:32 +00:00
|
|
|
}
|