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/ioctl.h>
|
|
|
|
#include <sys/disklabel.h>
|
|
|
|
#include <sys/diskslice.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include "libdisk.h"
|
|
|
|
|
|
|
|
/* 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-11-08 13:58:00 +00:00
|
|
|
int
|
|
|
|
Write_Disk(const struct disk *d1)
|
2002-10-23 19:52:32 +00:00
|
|
|
{
|
2002-11-08 13:58:00 +00:00
|
|
|
u_char buf[BBSIZE];
|
|
|
|
char device[64];
|
|
|
|
struct chunk *c1;
|
2002-10-23 19:52:32 +00:00
|
|
|
struct disklabel *dl;
|
|
|
|
void *p;
|
2002-11-08 13:58:00 +00:00
|
|
|
uint64_t *lp, sum;
|
|
|
|
int fd, i;
|
|
|
|
|
|
|
|
strcpy(device, _PATH_DEV);
|
|
|
|
strcat(device, d1->name);
|
2002-10-23 19:52:32 +00:00
|
|
|
|
2002-11-08 13:58:00 +00:00
|
|
|
fd = open(device, O_RDWR);
|
|
|
|
if (fd < 0)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
c1 = d1->chunks->part;
|
|
|
|
if (!strcmp(c1->name, "X") || c1->type != freebsd) {
|
|
|
|
close (fd);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08 13:58:00 +00:00
|
|
|
if(d1->boot1)
|
|
|
|
memcpy(buf + 512, d1->boot1, BBSIZE - 512);
|
2002-10-23 19:52:32 +00:00
|
|
|
|
|
|
|
dl = (struct disklabel *)(buf + 512 * LABELSECTOR + LABELOFFSET);
|
2002-11-08 13:58:00 +00:00
|
|
|
Fill_Disklabel(dl, d1, NULL, c1);
|
2002-10-23 19:52:32 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Tell SRM where the bootstrap is.
|
|
|
|
*/
|
|
|
|
lp = (u_long *)buf;
|
2002-11-08 13:58:00 +00:00
|
|
|
lp[60] = (BBSIZE - 512) / 512; /* Length */
|
|
|
|
lp[61] = 1; /* Start */
|
|
|
|
lp[62] = 0; /* Flags */
|
2002-10-23 19:52:32 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate the bootblock checksum for the SRM console.
|
|
|
|
*/
|
2002-11-08 13:58:00 +00:00
|
|
|
sum = 0;
|
|
|
|
for (i = 0; i < 63; i++)
|
2002-10-23 19:52:32 +00:00
|
|
|
sum += lp[i];
|
|
|
|
lp[63] = sum;
|
|
|
|
|
2002-11-08 13:58:00 +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);
|
|
|
|
close(fd);
|
2002-11-08 13:58:00 +00:00
|
|
|
|
|
|
|
return (0);
|
2002-10-23 19:52:32 +00:00
|
|
|
}
|