General cleanup:

o  Remove all code guarded by !defined(__ia64__). This file is
   specifically written for ia64,
o  Handle the case when read_block() or write_block() fails. We
   don't want sysinstall(8) to signal a thumbs-up on error,
o  Set the starting (cyl,hd,sect) triple to 0xFFFFFF when either
   bios_hd or bios_sect is zero or the LBA us not representable
   with the triple. In that case automaticly initialize the
   ending triple with 0xFFFFFF as well,
o  Reindent Write_Int32() as it was different than the rest of
   the file,
o  Remove some unused variables that appeared to be used but
   were effectively useless.
o  Plug a memory leak: The second timne we read the MBR, we write
   out a modified block, but didn't free the memory after writing.
o  Replace d1->sector_size with 512 when we read/write the MBR.
   We ignore the sector size in cases we shouldn't but adhered to
   it in cases it would be wrong if the sector_size wasn't 512.

This file should eventually be rewritten to write out a GPT. For
now, a MBR will do...
This commit is contained in:
Marcel Moolenaar 2002-11-10 20:47:02 +00:00
parent 78edc0fda2
commit 255bef40ce

View File

@ -26,112 +26,96 @@ __FBSDID("$FreeBSD$");
/* 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)
Write_FreeBSD(int fd, const struct disk *new, const struct chunk *c1)
{
struct disklabel *dl;
int i;
void *p;
u_char buf[BBSIZE];
for(i = 0; i < BBSIZE/512; i++) {
for (i = 0; i < BBSIZE/512; i++) {
p = read_block(fd, i + c1->offset, 512);
if (p == NULL)
return (1);
memcpy(buf + 512 * i, p, 512);
free(p);
}
#if !defined(__ia64__)
if(new->boot1)
memcpy(buf, new->boot1, 512);
if(new->boot2)
memcpy(buf + 512, new->boot2, BBSIZE-512);
#endif
dl = (struct disklabel *)(buf + 512 * LABELSECTOR + LABELOFFSET);
Fill_Disklabel(dl, new, old, c1);
Fill_Disklabel(dl, new, NULL, c1);
for(i=0;i<BBSIZE/512;i++) {
write_block(fd, i + c1->offset, buf + 512 * i, 512);
for (i = 0; i < BBSIZE/512; i++) {
if (write_block(fd, i + c1->offset, buf + 512 * i, 512) != 0)
return (1);
}
return 0;
return (0);
}
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;
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;
}
#if !defined(__ia64__)
/*
* 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;
int j;
struct disk *old = 0;
struct chunk *c1;
int ret = 0;
char device[64];
u_char *mbr;
struct dos_partition *dp,work[NDOSPART];
struct dos_partition *dp, work[NDOSPART];
int s[4];
int need_edd = 0; /* Need EDD (packet interface) */
strcpy(device,_PATH_DEV);
strcat(device,d1->name);
int fd, i, j, ret;
strcpy(device, _PATH_DEV);
strcat(device, d1->name);
fd = open(device,O_RDWR);
if (fd < 0)
return 1;
return (1);
memset(s,0,sizeof s);
mbr = read_block(fd, 0, d1->sector_size);
memset(s, 0, sizeof(s));
mbr = read_block(fd, 0, 512);
if (mbr == NULL)
goto fail;
dp = (struct dos_partition*)(mbr + DOSPARTOFF);
memcpy(work, dp, sizeof work);
memcpy(work, dp, sizeof(work));
dp = work;
free(mbr);
ret = 0;
for (c1 = d1->chunks->part; c1; c1 = c1->next) {
if (c1->type == unused) continue;
if (!strcmp(c1->name, "X")) continue;
j = c1->name[4] - '1';
if (c1->type == unused || !strcmp(c1->name, "X"))
continue;
j = c1->name[strlen(d1->name) + 1] - '1';
if (j < 0 || j > 3)
continue;
s[j]++;
if (c1->type == freebsd)
ret += Write_FreeBSD(fd, d1, old, c1);
ret += Write_FreeBSD(fd, d1, c1);
Write_Int32(&dp[j].dp_start, c1->offset);
Write_Int32(&dp[j].dp_size, c1->size);
i = c1->offset;
if (i >= 1024*d1->bios_sect*d1->bios_hd) {
i = 1024*d1->bios_sect*d1->bios_hd;
if (i == 0 || c1->offset >= i) {
/* Start */
dp[j].dp_ssect = 0xff;
dp[j].dp_shd = 0xff;
dp[j].dp_scyl = 0xff;
need_edd++;
/* End */
dp[j].dp_esect = 0xff;
dp[j].dp_ehd = 0xff;
dp[j].dp_ecyl = 0xff;
} else {
/* Start */
i = c1->offset;
dp[j].dp_ssect = i % d1->bios_sect;
i -= dp[j].dp_ssect++;
i /= d1->bios_sect;
@ -141,28 +125,24 @@ Write_Disk(const struct disk *d1)
dp[j].dp_scyl = i;
i -= dp[j].dp_scyl;
dp[j].dp_ssect |= i >> 2;
/* 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;
}
#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;
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;
#ifdef DEBUG
printf(" E:%lu = (%x/%x/%x)\n",
c1->end, dp[j].dp_ecyl, dp[j].dp_ehd, dp[j].dp_esect);
printf("S:%lu = (%x/%x/%x) E:%lu = (%x/%x/%x)\n", c1->offset,
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);
#endif
dp[j].dp_typ = c1->subtype;
@ -171,35 +151,27 @@ Write_Disk(const struct disk *d1)
else
dp[j].dp_flag = 0;
}
j = 0;
for(i = 0; i < NDOSPART; i++) {
if (!s[i])
memset(dp + i, 0, sizeof *dp);
if (dp[i].dp_flag)
j++;
memset(dp + i, 0, sizeof(*dp));
}
if (!j)
for(i = 0; i < NDOSPART; i++)
if (dp[i].dp_typ == 0xa5)
dp[i].dp_flag = 0x80;
mbr = read_block(fd, 0, d1->sector_size);
#if !defined(__ia64__)
if (d1->bootmgr) {
memcpy(mbr, d1->bootmgr, DOSPARTOFF);
Cfg_Boot_Mgr(mbr, need_edd);
}
#endif
memcpy(mbr + DOSPARTOFF, dp, sizeof *dp * NDOSPART);
if (mbr == NULL)
goto fail;
memcpy(mbr + DOSPARTOFF, dp, sizeof(*dp) * NDOSPART);
mbr[512-2] = 0x55;
mbr[512-1] = 0xaa;
write_block(fd, 0, mbr, d1->sector_size);
#if !defined(__ia64__)
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
ret += write_block(fd, 0, mbr, 512);
free(mbr);
if (ret != 0)
goto fail;
close(fd);
return 0;
return (0);
fail:
close(fd);
return (1);
}