From 3c688df1144b5bdaff8d7a1bb0a2b070aeb58570 Mon Sep 17 00:00:00 2001 From: phk Date: Wed, 23 Oct 2002 19:52:32 +0000 Subject: Untangle #ifdefs in the write-end of things by giving each arch its own file and own copy of WriteDisk() to do things in. This should have happened years ago, instead of adding #ifdefs all over the place. --- lib/libdisk/Makefile | 12 +- lib/libdisk/blocks.c | 8 +- lib/libdisk/chunk.c | 4 +- lib/libdisk/disk.c | 2 +- lib/libdisk/libdisk.h | 5 +- lib/libdisk/write_alpha_disk.c | 123 ++++++++++++++++ lib/libdisk/write_amd64_disk.c | 212 +++++++++++++++++++++++++++ lib/libdisk/write_disk.c | 315 +---------------------------------------- lib/libdisk/write_i386_disk.c | 212 +++++++++++++++++++++++++++ lib/libdisk/write_ia64_disk.c | 212 +++++++++++++++++++++++++++ lib/libdisk/write_pc98_disk.c | 185 ++++++++++++++++++++++++ 11 files changed, 968 insertions(+), 322 deletions(-) create mode 100644 lib/libdisk/write_alpha_disk.c create mode 100644 lib/libdisk/write_amd64_disk.c create mode 100644 lib/libdisk/write_i386_disk.c create mode 100644 lib/libdisk/write_ia64_disk.c create mode 100644 lib/libdisk/write_pc98_disk.c (limited to 'lib') diff --git a/lib/libdisk/Makefile b/lib/libdisk/Makefile index a1d0746..ec637d1 100644 --- a/lib/libdisk/Makefile +++ b/lib/libdisk/Makefile @@ -4,11 +4,21 @@ LIB= disk SRCS= blocks.c chunk.c disk.c change.c \ create_chunk.c rules.c write_disk.c INCS= libdisk.h +WARNS= 2 -CFLAGS+= -Wall .if ${MACHINE} == "pc98" CFLAGS+= -DPC98 +SRCS += write_pc98_disk.c .endif + +.if ${MACHINE} == "i386" +SRCS += write_i386_disk.c +.endif + +.if ${MACHINE} == "alpha" +SRCS += write_alpha_disk.c +.endif + CLEANFILES+= tmp.c tst01 tst01.o NOPROFILE= yes NOPIC= yes diff --git a/lib/libdisk/blocks.c b/lib/libdisk/blocks.c index a630675..682a54b 100644 --- a/lib/libdisk/blocks.c +++ b/lib/libdisk/blocks.c @@ -19,6 +19,7 @@ void * read_block(int fd, daddr_t block, u_long sector_size) { void *foo; + int i; foo = malloc(sector_size); if (!foo) @@ -27,7 +28,8 @@ read_block(int fd, daddr_t block, u_long sector_size) free (foo); return NULL; } - if (sector_size != read(fd, foo, sector_size)) { + i = read(fd, foo, sector_size); + if ((int)sector_size != i) { free (foo); return NULL; } @@ -37,10 +39,12 @@ read_block(int fd, daddr_t block, u_long sector_size) int write_block(int fd, daddr_t block, const void *foo, u_long sector_size) { + int i; if (-1 == lseek(fd, (off_t)block * sector_size, SEEK_SET)) return -1; - if (sector_size != write(fd, foo, sector_size)) + i = write(fd, foo, sector_size); + if ((int)sector_size != i) return -1; return 0; } diff --git a/lib/libdisk/chunk.c b/lib/libdisk/chunk.c index e72ad63..2b4854d 100644 --- a/lib/libdisk/chunk.c +++ b/lib/libdisk/chunk.c @@ -124,7 +124,7 @@ Clone_Chunk(const struct chunk *c1) static int Insert_Chunk(struct chunk *c2, u_long offset, u_long size, const char *name, - chunk_e type, int subtype, u_long flags, const char *sname) + chunk_e type, int subtype, u_long flags, const char *sname __unused) { struct chunk *ct,*cs; @@ -322,7 +322,7 @@ ShowChunkFlags(struct chunk *c) return ret; } -void +static void Print_Chunk(struct chunk *c1,int offset) { int i; diff --git a/lib/libdisk/disk.c b/lib/libdisk/disk.c index 329e933..ebd3def 100644 --- a/lib/libdisk/disk.c +++ b/lib/libdisk/disk.c @@ -402,7 +402,7 @@ assignToSlice(void *arg, XMLToken t, u_int *slice, u_int64_t v) * Callback to collect disk-related data. */ static int -assignToDisk(void *arg, XMLToken t, const u_int *slice, u_int64_t v) +assignToDisk(void *arg, XMLToken t, u_int *slice, u_int64_t v) { struct disklabel *dl = (struct disklabel *) arg; diff --git a/lib/libdisk/libdisk.h b/lib/libdisk/libdisk.h index bf0d263..52fa135 100644 --- a/lib/libdisk/libdisk.h +++ b/lib/libdisk/libdisk.h @@ -174,7 +174,7 @@ CheckRules(const struct disk *); */ char ** -Disk_Names(); +Disk_Names(void); /* Return char** with all disk's names (wd0, wd1 ...). You must free * each pointer, as well as the array by hand */ @@ -246,6 +246,8 @@ ShowChunkFlags(struct chunk *c); * Implementation details >>> DO NOT USE <<< */ +struct disklabel; +void Fill_Disklabel(struct disklabel *dl, const struct disk *new, const struct disk *old, const struct chunk *c1); void Debug_Chunk(struct chunk *); void Free_Chunk(struct chunk *); struct chunk * Clone_Chunk(const struct chunk *); @@ -253,7 +255,6 @@ int Add_Chunk(struct disk *, long, u_long, const char *, chunk_e, int, u_long, c void * read_block(int, daddr_t, u_long); int write_block(int, daddr_t, const void *, u_long); struct disklabel * read_disklabel(int, daddr_t, u_long); -struct chunk * Find_Mother_Chunk(struct chunk *, u_long, u_long, chunk_e); struct disk * Int_Open_Disk(const char *name, u_long size); int Fixup_Names(struct disk *); int MakeDevChunk(const struct chunk *c1, const char *path); diff --git a/lib/libdisk/write_alpha_disk.c b/lib/libdisk/write_alpha_disk.c new file mode 100644 index 0000000..313aae4 --- /dev/null +++ b/lib/libdisk/write_alpha_disk.c @@ -0,0 +1,123 @@ +/* + * ---------------------------------------------------------------------------- + * "THE BEER-WARE LICENSE" (Revision 42): + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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 */ +static int +Write_FreeBSD(int fd, const struct disk *new, const struct disk *old, const struct chunk *c1) +{ + struct disklabel *dl; + int i; + void *p; + u_char buf[BBSIZE]; + u_long *lp, sum; + + for(i = 0; i < BBSIZE/512; i++) { + p = read_block(fd, i + c1->offset, 512); + memcpy(buf + 512 * i, p, 512); + free(p); + } + if(new->boot1) + memcpy(buf + 512, new->boot1, BBSIZE-512); + + dl = (struct disklabel *)(buf + 512 * LABELSECTOR + LABELOFFSET); + Fill_Disklabel(dl, new, old, c1); + + /* + * Tell SRM where the bootstrap is. + */ + lp = (u_long *)buf; + lp[60] = 15; + lp[61] = 1; + lp[62] = 0; + + /* + * Generate the bootblock checksum for the SRM console. + */ + for (lp = (u_long *)buf, i = 0, sum = 0; i < 63; i++) + sum += lp[i]; + lp[63] = sum; + + for(i=0;ioffset, buf + 512 * i, 512); + } + + return 0; +} + + + +int +Write_Disk(const struct disk *d1) +{ + int fd,i; + struct disk *old = 0; + struct chunk *c1; + int ret = 0; + char device[64]; + u_char *mbr; + struct dos_partition *dp,work[NDOSPART]; + int s[4]; + int one = 1; + int zero = 0; + + strcpy(device,_PATH_DEV); + strcat(device,d1->name); + + + fd = open(device,O_RDWR); + if (fd < 0) { +#ifdef DEBUG + warn("open(%s) failed", device); +#endif + return 1; + } + ioctl(fd, DIOCWLABEL, &one); + + memset(s,0,sizeof s); + mbr = read_block(fd, 0, d1->sector_size); + dp = (struct dos_partition*)(mbr + DOSPARTOFF); + memcpy(work, dp, sizeof work); + dp = work; + free(mbr); + for (c1 = d1->chunks->part; c1; c1 = c1->next) { + if (c1->type == unused) continue; + if (!strcmp(c1->name, "X")) continue; + if (c1->type == freebsd) + ret += Write_FreeBSD(fd, d1, old, c1); + + } + + i = 1; + i = ioctl(fd, DIOCSYNCSLICEINFO, &i); +#ifdef DEBUG + if (i != 0) + warn("ioctl(DIOCSYNCSLICEINFO)"); +#endif + ioctl(fd, DIOCWLABEL, &zero); + close(fd); + return 0; +} diff --git a/lib/libdisk/write_amd64_disk.c b/lib/libdisk/write_amd64_disk.c new file mode 100644 index 0000000..686758e --- /dev/null +++ b/lib/libdisk/write_amd64_disk.c @@ -0,0 +1,212 @@ +/* + * ---------------------------------------------------------------------------- + * "THE BEER-WARE LICENSE" (Revision 42): + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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 */ +static int +Write_FreeBSD(int fd, const struct disk *new, const struct disk *old, const struct chunk *c1) +{ + struct disklabel *dl; + int i; + void *p; + u_char buf[BBSIZE]; + + for(i = 0; i < BBSIZE/512; i++) { + p = read_block(fd, i + c1->offset, 512); + memcpy(buf + 512 * i, p, 512); + free(p); + } + if(new->boot1) + memcpy(buf, new->boot1, 512); + + if(new->boot2) + memcpy(buf + 512, new->boot2, BBSIZE-512); + + dl = (struct disklabel *)(buf + 512 * LABELSECTOR + LABELOFFSET); + Fill_Disklabel(dl, new, old, c1); + + + for(i=0;ioffset, buf + 512 * i, 512); + } + + 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; +} + +/* + * 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 */ + } +} + +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]; + int s[4]; + int need_edd = 0; /* Need EDD (packet interface) */ + int one = 1; + int zero = 0; + + strcpy(device,_PATH_DEV); + strcat(device,d1->name); + + + fd = open(device,O_RDWR); + if (fd < 0) { +#ifdef DEBUG + warn("open(%s) failed", device); +#endif + return 1; + } + ioctl(fd, DIOCWLABEL, &one); + + memset(s,0,sizeof s); + mbr = read_block(fd, 0, d1->sector_size); + dp = (struct dos_partition*)(mbr + DOSPARTOFF); + memcpy(work, dp, sizeof work); + dp = work; + free(mbr); + 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'; + 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); + + 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) { + dp[j].dp_ssect = 0xff; + dp[j].dp_shd = 0xff; + dp[j].dp_scyl = 0xff; + need_edd++; + } else { + 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; + } + +#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); +#endif + + dp[j].dp_typ = c1->subtype; + if (c1->flags & CHUNK_ACTIVE) + dp[j].dp_flag = 0x80; + 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++; + } + 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 (d1->bootmgr) { + memcpy(mbr, d1->bootmgr, DOSPARTOFF); + Cfg_Boot_Mgr(mbr, need_edd); + } + memcpy(mbr + DOSPARTOFF, dp, sizeof *dp * NDOSPART); + mbr[512-2] = 0x55; + mbr[512-1] = 0xaa; + write_block(fd, 0, mbr, d1->sector_size); + 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); + + i = 1; + i = ioctl(fd, DIOCSYNCSLICEINFO, &i); +#ifdef DEBUG + if (i != 0) + warn("ioctl(DIOCSYNCSLICEINFO)"); +#endif + ioctl(fd, DIOCWLABEL, &zero); + close(fd); + return 0; +} diff --git a/lib/libdisk/write_disk.c b/lib/libdisk/write_disk.c index f889c11..0a27ac9 100644 --- a/lib/libdisk/write_disk.c +++ b/lib/libdisk/write_disk.c @@ -20,18 +20,10 @@ __FBSDID("$FreeBSD$"); #include #include #include -#ifdef PC98 -#include -#else -#include -#endif #include #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 */ - -static void +void Fill_Disklabel(struct disklabel *dl, const struct disk *new, const struct disk *old, const struct chunk *c1) { struct chunk *c2; @@ -80,308 +72,3 @@ Fill_Disklabel(struct disklabel *dl, const struct disk *new, const struct disk * return; } - - -/* 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) -{ - struct disklabel *dl; - int i; - void *p; - u_char buf[BBSIZE]; -#ifdef __alpha__ - u_long *lp, sum; -#endif - - for(i = 0; i < BBSIZE/512; i++) { - p = read_block(fd, i + c1->offset, 512); - memcpy(buf + 512 * i, p, 512); - free(p); - } -#if defined(__i386__) - if(new->boot1) - memcpy(buf, new->boot1, 512); - - if(new->boot2) - memcpy(buf + 512, new->boot2, BBSIZE-512); -#elif defined(__alpha__) - if(new->boot1) - memcpy(buf + 512, new->boot1, BBSIZE-512); -#endif - - dl = (struct disklabel *)(buf + 512 * LABELSECTOR + LABELOFFSET); - Fill_Disklabel(dl, new, old, c1); - -#ifdef __alpha__ - /* - * Tell SRM where the bootstrap is. - */ - lp = (u_long *)buf; - lp[60] = 15; - lp[61] = 1; - lp[62] = 0; - - /* - * Generate the bootblock checksum for the SRM console. - */ - for (lp = (u_long *)buf, i = 0, sum = 0; i < 63; i++) - sum += lp[i]; - lp[63] = sum; -#endif /*__alpha__*/ - - for(i=0;ioffset, buf + 512 * i, 512); - } - - return 0; -} - -#if defined(__i386__) && !defined(PC98) -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; -} -#endif - -#if defined(__i386__) && !defined(PC98) -/* - * 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; -#ifdef __i386__ - int j; -#endif - struct disk *old = 0; - struct chunk *c1; - int ret = 0; - char device[64]; - u_char *mbr; - struct dos_partition *dp,work[NDOSPART]; -#ifdef PC98 - int s[7]; - int PC98_EntireDisk = 0; -#else - int s[4]; -#ifdef __i386__ - int need_edd = 0; /* Need EDD (packet interface) */ -#endif -#endif - int one = 1; - int zero = 0; - - strcpy(device,_PATH_DEV); - strcat(device,d1->name); - -#ifdef PC98 - /* XXX - for entire FreeBSD(98) */ - for (c1 = d1->chunks->part; c1; c1 = c1->next) { - if ((c1->type == freebsd) || (c1->offset == 0)) - device[9] = 0; - } -#endif - - fd = open(device,O_RDWR); - if (fd < 0) { -#ifdef DEBUG - warn("open(%s) failed", device); -#endif - return 1; - } - ioctl(fd, DIOCWLABEL, &one); - - memset(s,0,sizeof s); -#ifdef PC98 - mbr = read_block(fd, 1, d1->sector_size); -#else - mbr = read_block(fd, 0, d1->sector_size); -#endif - dp = (struct dos_partition*)(mbr + DOSPARTOFF); - memcpy(work, dp, sizeof work); - dp = work; - free(mbr); - for (c1 = d1->chunks->part; c1; c1 = c1->next) { - if (c1->type == unused) continue; - if (!strcmp(c1->name, "X")) continue; -#ifdef __i386__ - j = c1->name[4] - '1'; - j = c1->name[strlen(d1->name) + 1] - '1'; -#ifdef PC98 - if (j < 0 || j > 7) -#else - if (j < 0 || j > 3) -#endif - continue; - s[j]++; -#endif - if (c1->type == freebsd) - ret += Write_FreeBSD(fd, d1, old, c1); - -#ifdef __i386__ -#ifndef PC98 - Write_Int32(&dp[j].dp_start, c1->offset); - Write_Int32(&dp[j].dp_size, c1->size); -#endif - - i = c1->offset; -#ifdef PC98 - 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; -#else - if (i >= 1024*d1->bios_sect*d1->bios_hd) { - dp[j].dp_ssect = 0xff; - dp[j].dp_shd = 0xff; - dp[j].dp_scyl = 0xff; - need_edd++; - } else { - 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; - } -#endif /* PC98 */ - -#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; -#ifdef PC98 -#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; - dp[j].dp_ehd = i % d1->bios_hd; - i -= dp[j].dp_ehd; - i /= d1->bios_hd; - dp[j].dp_ecyl = i; -#endif -#else - 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; -#endif - -#ifdef DEBUG - printf(" E:%lu = (%x/%x/%x)\n", - c1->end, dp[j].dp_ecyl, dp[j].dp_ehd, dp[j].dp_esect); -#endif - -#ifdef PC98 - 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); -#else - dp[j].dp_typ = c1->subtype; - if (c1->flags & CHUNK_ACTIVE) - dp[j].dp_flag = 0x80; - else - dp[j].dp_flag = 0; -#endif -#endif - } -#ifdef __i386__ - j = 0; - for(i = 0; i < NDOSPART; i++) { - if (!s[i]) - memset(dp + i, 0, sizeof *dp); -#ifndef PC98 - if (dp[i].dp_flag) - j++; -#endif - } -#ifndef PC98 - if (!j) - for(i = 0; i < NDOSPART; i++) - if (dp[i].dp_typ == 0xa5) - dp[i].dp_flag = 0x80; -#endif - -#ifdef PC98 - 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++) - write_block(fd, 2 + i, &d1->bootmenu[i * d1->sector_size], d1->sector_size); -#else - mbr = read_block(fd, 0, d1->sector_size); - if (d1->bootmgr) { - memcpy(mbr, d1->bootmgr, DOSPARTOFF); - Cfg_Boot_Mgr(mbr, need_edd); - } - memcpy(mbr + DOSPARTOFF, dp, sizeof *dp * NDOSPART); - mbr[512-2] = 0x55; - mbr[512-1] = 0xaa; - write_block(fd, 0, mbr, d1->sector_size); - 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 -#endif - - i = 1; - i = ioctl(fd, DIOCSYNCSLICEINFO, &i); -#ifdef DEBUG - if (i != 0) - warn("ioctl(DIOCSYNCSLICEINFO)"); -#endif - ioctl(fd, DIOCWLABEL, &zero); - close(fd); - return 0; -} diff --git a/lib/libdisk/write_i386_disk.c b/lib/libdisk/write_i386_disk.c new file mode 100644 index 0000000..686758e --- /dev/null +++ b/lib/libdisk/write_i386_disk.c @@ -0,0 +1,212 @@ +/* + * ---------------------------------------------------------------------------- + * "THE BEER-WARE LICENSE" (Revision 42): + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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 */ +static int +Write_FreeBSD(int fd, const struct disk *new, const struct disk *old, const struct chunk *c1) +{ + struct disklabel *dl; + int i; + void *p; + u_char buf[BBSIZE]; + + for(i = 0; i < BBSIZE/512; i++) { + p = read_block(fd, i + c1->offset, 512); + memcpy(buf + 512 * i, p, 512); + free(p); + } + if(new->boot1) + memcpy(buf, new->boot1, 512); + + if(new->boot2) + memcpy(buf + 512, new->boot2, BBSIZE-512); + + dl = (struct disklabel *)(buf + 512 * LABELSECTOR + LABELOFFSET); + Fill_Disklabel(dl, new, old, c1); + + + for(i=0;ioffset, buf + 512 * i, 512); + } + + 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; +} + +/* + * 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 */ + } +} + +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]; + int s[4]; + int need_edd = 0; /* Need EDD (packet interface) */ + int one = 1; + int zero = 0; + + strcpy(device,_PATH_DEV); + strcat(device,d1->name); + + + fd = open(device,O_RDWR); + if (fd < 0) { +#ifdef DEBUG + warn("open(%s) failed", device); +#endif + return 1; + } + ioctl(fd, DIOCWLABEL, &one); + + memset(s,0,sizeof s); + mbr = read_block(fd, 0, d1->sector_size); + dp = (struct dos_partition*)(mbr + DOSPARTOFF); + memcpy(work, dp, sizeof work); + dp = work; + free(mbr); + 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'; + 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); + + 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) { + dp[j].dp_ssect = 0xff; + dp[j].dp_shd = 0xff; + dp[j].dp_scyl = 0xff; + need_edd++; + } else { + 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; + } + +#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); +#endif + + dp[j].dp_typ = c1->subtype; + if (c1->flags & CHUNK_ACTIVE) + dp[j].dp_flag = 0x80; + 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++; + } + 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 (d1->bootmgr) { + memcpy(mbr, d1->bootmgr, DOSPARTOFF); + Cfg_Boot_Mgr(mbr, need_edd); + } + memcpy(mbr + DOSPARTOFF, dp, sizeof *dp * NDOSPART); + mbr[512-2] = 0x55; + mbr[512-1] = 0xaa; + write_block(fd, 0, mbr, d1->sector_size); + 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); + + i = 1; + i = ioctl(fd, DIOCSYNCSLICEINFO, &i); +#ifdef DEBUG + if (i != 0) + warn("ioctl(DIOCSYNCSLICEINFO)"); +#endif + ioctl(fd, DIOCWLABEL, &zero); + close(fd); + return 0; +} diff --git a/lib/libdisk/write_ia64_disk.c b/lib/libdisk/write_ia64_disk.c new file mode 100644 index 0000000..686758e --- /dev/null +++ b/lib/libdisk/write_ia64_disk.c @@ -0,0 +1,212 @@ +/* + * ---------------------------------------------------------------------------- + * "THE BEER-WARE LICENSE" (Revision 42): + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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 */ +static int +Write_FreeBSD(int fd, const struct disk *new, const struct disk *old, const struct chunk *c1) +{ + struct disklabel *dl; + int i; + void *p; + u_char buf[BBSIZE]; + + for(i = 0; i < BBSIZE/512; i++) { + p = read_block(fd, i + c1->offset, 512); + memcpy(buf + 512 * i, p, 512); + free(p); + } + if(new->boot1) + memcpy(buf, new->boot1, 512); + + if(new->boot2) + memcpy(buf + 512, new->boot2, BBSIZE-512); + + dl = (struct disklabel *)(buf + 512 * LABELSECTOR + LABELOFFSET); + Fill_Disklabel(dl, new, old, c1); + + + for(i=0;ioffset, buf + 512 * i, 512); + } + + 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; +} + +/* + * 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 */ + } +} + +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]; + int s[4]; + int need_edd = 0; /* Need EDD (packet interface) */ + int one = 1; + int zero = 0; + + strcpy(device,_PATH_DEV); + strcat(device,d1->name); + + + fd = open(device,O_RDWR); + if (fd < 0) { +#ifdef DEBUG + warn("open(%s) failed", device); +#endif + return 1; + } + ioctl(fd, DIOCWLABEL, &one); + + memset(s,0,sizeof s); + mbr = read_block(fd, 0, d1->sector_size); + dp = (struct dos_partition*)(mbr + DOSPARTOFF); + memcpy(work, dp, sizeof work); + dp = work; + free(mbr); + 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'; + 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); + + 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) { + dp[j].dp_ssect = 0xff; + dp[j].dp_shd = 0xff; + dp[j].dp_scyl = 0xff; + need_edd++; + } else { + 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; + } + +#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); +#endif + + dp[j].dp_typ = c1->subtype; + if (c1->flags & CHUNK_ACTIVE) + dp[j].dp_flag = 0x80; + 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++; + } + 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 (d1->bootmgr) { + memcpy(mbr, d1->bootmgr, DOSPARTOFF); + Cfg_Boot_Mgr(mbr, need_edd); + } + memcpy(mbr + DOSPARTOFF, dp, sizeof *dp * NDOSPART); + mbr[512-2] = 0x55; + mbr[512-1] = 0xaa; + write_block(fd, 0, mbr, d1->sector_size); + 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); + + i = 1; + i = ioctl(fd, DIOCSYNCSLICEINFO, &i); +#ifdef DEBUG + if (i != 0) + warn("ioctl(DIOCSYNCSLICEINFO)"); +#endif + ioctl(fd, DIOCWLABEL, &zero); + close(fd); + return 0; +} diff --git a/lib/libdisk/write_pc98_disk.c b/lib/libdisk/write_pc98_disk.c new file mode 100644 index 0000000..7d4f33e --- /dev/null +++ b/lib/libdisk/write_pc98_disk.c @@ -0,0 +1,185 @@ +/* + * ---------------------------------------------------------------------------- + * "THE BEER-WARE LICENSE" (Revision 42): + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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 */ +static int +Write_FreeBSD(int fd, const struct disk *new, const struct disk *old, const struct chunk *c1) +{ + struct disklabel *dl; + int i; + void *p; + u_char buf[BBSIZE]; + + for(i = 0; i < BBSIZE/512; i++) { + p = read_block(fd, i + c1->offset, 512); + memcpy(buf + 512 * i, p, 512); + free(p); + } + if(new->boot1) + memcpy(buf, new->boot1, 512); + + if(new->boot2) + memcpy(buf + 512, new->boot2, BBSIZE-512); + + dl = (struct disklabel *)(buf + 512 * LABELSECTOR + LABELOFFSET); + Fill_Disklabel(dl, new, old, c1); + + + for(i=0;ioffset, buf + 512 * i, 512); + } + + return 0; +} + + + +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]; + int s[7]; + int PC98_EntireDisk = 0; + int one = 1; + int zero = 0; + + strcpy(device,_PATH_DEV); + strcat(device,d1->name); + + /* XXX - for entire FreeBSD(98) */ + for (c1 = d1->chunks->part; c1; c1 = c1->next) { + if ((c1->type == freebsd) || (c1->offset == 0)) + device[9] = 0; + } + + fd = open(device,O_RDWR); + if (fd < 0) { +#ifdef DEBUG + warn("open(%s) failed", device); +#endif + return 1; + } + ioctl(fd, DIOCWLABEL, &one); + + memset(s,0,sizeof s); + mbr = read_block(fd, 1, d1->sector_size); + dp = (struct dos_partition*)(mbr + DOSPARTOFF); + memcpy(work, dp, sizeof work); + dp = work; + free(mbr); + 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'; + 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 + printf("S:%lu = (%x/%x/%x)", + c1->offset, dp[j].dp_scyl, dp[j].dp_shd, dp[j].dp_ssect); +#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; + dp[j].dp_ehd = i % d1->bios_hd; + i -= dp[j].dp_ehd; + i /= d1->bios_hd; + dp[j].dp_ecyl = i; +#endif + +#ifdef DEBUG + printf(" E:%lu = (%x/%x/%x)\n", + c1->end, dp[j].dp_ecyl, dp[j].dp_ehd, dp[j].dp_esect); +#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; + for(i = 0; i < NDOSPART; i++) { + 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++) + write_block(fd, 2 + i, &d1->bootmenu[i * d1->sector_size], d1->sector_size); + + i = 1; + i = ioctl(fd, DIOCSYNCSLICEINFO, &i); +#ifdef DEBUG + if (i != 0) + warn("ioctl(DIOCSYNCSLICEINFO)"); +#endif + ioctl(fd, DIOCWLABEL, &zero); + close(fd); + return 0; +} -- cgit v1.1