1994-10-18 07:56:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1994, Paul Richards.
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software may be used, modified, copied, distributed, and
|
|
|
|
* sold, in both source and binary form provided that the above
|
|
|
|
* copyright and these terms are retained, verbatim, as the first
|
|
|
|
* lines of this file. Under no circumstances is the author
|
|
|
|
* responsible for the proper functioning of this software, nor does
|
|
|
|
* the author assume any responsibility for damages incurred with
|
|
|
|
* its use.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/disklabel.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
1994-10-26 04:09:19 +00:00
|
|
|
#include <string.h>
|
1994-10-21 18:08:33 +00:00
|
|
|
#include <dialog.h>
|
1994-10-18 07:56:34 +00:00
|
|
|
|
1994-10-19 23:58:03 +00:00
|
|
|
#include "mbr.h"
|
1994-10-18 07:56:34 +00:00
|
|
|
#include "sysinstall.h"
|
|
|
|
|
|
|
|
extern char *bootblocks;
|
1994-10-19 23:58:03 +00:00
|
|
|
extern struct mbr *mbr;
|
1994-10-21 18:08:33 +00:00
|
|
|
extern char boot1[];
|
|
|
|
extern char boot2[];
|
1994-10-18 07:56:34 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
enable_label(int fd)
|
1994-10-19 23:58:03 +00:00
|
|
|
{
|
1994-10-18 07:56:34 +00:00
|
|
|
int flag = 1;
|
|
|
|
if (ioctl(fd, DIOCWLABEL, &flag) < 0) {
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
disable_label(int fd)
|
1994-10-19 23:58:03 +00:00
|
|
|
{
|
1994-10-18 07:56:34 +00:00
|
|
|
int flag = 0;
|
|
|
|
if (ioctl(fd, DIOCWLABEL, &flag) < 0) {
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
write_bootblocks(int fd, off_t offset, int bbsize)
|
|
|
|
{
|
1994-10-29 10:01:40 +00:00
|
|
|
if (ioctl(fd, DIOCWDINFO, &avail_disklabels[inst_disk]) < 0) {
|
|
|
|
Fatal("Failed to write disklabel: %s\n", strerror(errno));
|
1994-10-18 07:56:34 +00:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
1994-10-29 12:03:59 +00:00
|
|
|
Debug("Seeking to block %ld ", offset);
|
|
|
|
Debug("Seeking to byte %ld ", (offset * avail_disklabels[inst_disk].d_secsize));
|
1994-10-18 07:56:34 +00:00
|
|
|
if (lseek(fd, (offset * avail_disklabels[inst_disk].d_secsize), SEEK_SET) < 0) {
|
|
|
|
sprintf(errmsg, "Couldn't seek to start of partition\n");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enable_label(fd) == -1)
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
if (write(fd, bootblocks, bbsize) != bbsize) {
|
1994-10-29 12:03:59 +00:00
|
|
|
sprintf(errmsg, "Failed to write bootblocks (%p,%d) %d %s\n",
|
|
|
|
bootblocks, bbsize,
|
1994-10-29 10:01:40 +00:00
|
|
|
errno, strerror(errno)
|
|
|
|
);
|
1994-10-18 07:56:34 +00:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disable_label(fd) == -1)
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
build_bootblocks(struct disklabel *label)
|
|
|
|
{
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
|
1994-10-21 18:08:33 +00:00
|
|
|
sprintf(scratch, "\nLoading boot code from %s\n", boot1);
|
|
|
|
dialog_msgbox(TITLE, scratch, 5, 60, 0);
|
1994-10-18 07:56:34 +00:00
|
|
|
fd = open(boot1, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
sprintf(errmsg, "Couldn't open boot file %s\n", boot1);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
1994-10-21 18:08:33 +00:00
|
|
|
if (read(fd, bootblocks, MBRSIZE) < 0) {
|
1994-10-18 07:56:34 +00:00
|
|
|
sprintf(errmsg, "Couldn't read from boot file %s\n", boot1);
|
1994-10-21 18:08:33 +00:00
|
|
|
return(-1);
|
1994-10-18 07:56:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (close(fd) == -1) {
|
|
|
|
sprintf(errmsg, "Couldn't close boot file %s\n", boot1);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
1994-10-21 18:08:33 +00:00
|
|
|
dialog_clear();
|
1994-10-21 18:26:25 +00:00
|
|
|
sprintf(scratch, "\nLoading boot code from %s\n", boot2);
|
1994-10-21 18:08:33 +00:00
|
|
|
dialog_msgbox(TITLE, scratch, 5, 60, 0);
|
|
|
|
|
1994-10-18 07:56:34 +00:00
|
|
|
fd = open(boot2, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
sprintf(errmsg, "Couldn't open boot file %s\n", boot2);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
1994-10-21 18:08:33 +00:00
|
|
|
if (read(fd, &bootblocks[MBRSIZE],
|
|
|
|
(int)(label->d_bbsize - MBRSIZE)) < 0) {
|
1994-10-18 07:56:34 +00:00
|
|
|
sprintf(errmsg, "Couldn't read from boot file %s\n", boot2);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (close(fd) == -1) {
|
|
|
|
sprintf(errmsg, "Couldn't close boot file %s\n", boot2);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
1994-10-21 18:26:25 +00:00
|
|
|
dialog_clear();
|
|
|
|
|
1994-10-24 03:30:56 +00:00
|
|
|
/* Copy MBR partition area into bootblocks */
|
1994-10-19 23:58:03 +00:00
|
|
|
|
|
|
|
bcopy(mbr->dospart, &bootblocks[DOSPARTOFF],
|
1994-10-24 03:30:56 +00:00
|
|
|
sizeof(struct dos_partition) * NDOSPART);
|
1994-10-19 23:58:03 +00:00
|
|
|
|
1994-10-18 07:56:34 +00:00
|
|
|
/* Write the disklabel into the bootblocks */
|
|
|
|
|
|
|
|
label->d_checksum = dkcksum(label);
|
1994-10-19 23:58:03 +00:00
|
|
|
bcopy(label, &bootblocks[(LABELSECTOR * label->d_secsize) + LABELOFFSET],
|
|
|
|
sizeof *label);
|
1994-10-18 07:56:34 +00:00
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|