+ add u_long sector_size to struct disk (documented in libdisk.3)

+ make Open_Disk sense the sector size by trying 512, 1024 and 2048
  in this order. This makes the kernel note that
  dscheck(cd1): bio_bcount 512 is not on a sector boundary (ssize 2048)
  dscheck(cd1): bio_bcount 1024 is not on a sector boundary (ssize 2048)
  if 2048 is the sector size. If this worries anyone: the message is from
  /usr/src/sys/kern/subr_diskslice.c and shutups are to be placed there.
+ Have read_block and write_block use an additional parameter, the
  sector size.
+ replace all barfout calls with return NULL, 0, __LINE__, etc.
  Note that this does NOT emit diagnostics. More often than not,
  you don't want library functions to scribble on stderr -- it may
  not even be available. The right thing is to propagate the error
  condition to upper management. The app should take care of errors.
+ use d1->sector_size instead of 512 in various places. I've left many
  places untouched, especially those writing MBRs. I simply added
  another arg hardcoded as 512. This is because I would not know what
  I'm doing... I felt this approach would be reasonably backward
  compatible and not introduce any new bugs in critical software.
  Famous last words. Messing with MBRs might soon put me in the same
  screwup meister category as, uh, never mind.  :-)
+ bump the max no of disks from 20 to 32 (due to PR 24503).

PR:		8434 / 8436 / 24503
Submitted by:	Jens Schweikhardt <schweikh@schweikhardt.net>
This commit is contained in:
jkh 2001-05-13 20:08:54 +00:00
parent df0ea50582
commit f235bbf938
8 changed files with 101 additions and 80 deletions

View File

@ -16,25 +16,30 @@
#include "libdisk.h"
void *
read_block(int fd, daddr_t block)
read_block(int fd, daddr_t block, u_long sector_size)
{
void *foo;
foo = malloc(512);
foo = malloc(sector_size);
if (!foo)
barfout(1, "malloc");
if (-1 == lseek(fd, (off_t)block * 512, SEEK_SET))
barfout(1, "lseek");
if (512 != read(fd, foo, 512))
barfout(1, "read");
return NULL;
if (-1 == lseek(fd, (off_t)block * sector_size, SEEK_SET)) {
free (foo);
return NULL;
}
if (sector_size != read(fd, foo, sector_size)) {
free (foo);
return NULL;
}
return foo;
}
void
write_block(int fd, daddr_t block, void *foo)
int
write_block(int fd, daddr_t block, void *foo, u_long sector_size)
{
if (-1 == lseek(fd, (off_t)block * 512, SEEK_SET))
barfout(1, "lseek");
if (512 != write(fd,foo, 512))
barfout(1, "write");
if (-1 == lseek(fd, (off_t)block * sector_size, SEEK_SET))
return -1;
if (sector_size != write(fd, foo, sector_size))
return -1;
return 0;
}

View File

@ -99,7 +99,7 @@ Clone_Chunk(struct chunk *c1)
if(!c1)
return 0;
c2 = new_chunk();
if (!c2) barfout(1, "malloc failed");
if (!c2) return NULL;
*c2 = *c1;
if (c1->private_data && c1->private_clone)
c2->private_data = c2->private_clone(c2->private_data);
@ -128,7 +128,7 @@ Insert_Chunk(struct chunk *c2, u_long offset, u_long size, const char *name,
return __LINE__;
ct = new_chunk();
if (!ct) barfout(1, "malloc failed");
if (!ct) return __LINE__;
memset(ct, 0, sizeof *ct);
ct->disk = c2->disk;
ct->offset = offset;
@ -149,7 +149,7 @@ Insert_Chunk(struct chunk *c2, u_long offset, u_long size, const char *name,
if(type==freebsd || type==extended) {
cs = new_chunk();
if (!cs) barfout(1, "malloc failed");
if (!cs) return __LINE__;
memset(cs, 0, sizeof *cs);
cs->disk = c2->disk;
cs->offset = offset;
@ -166,7 +166,7 @@ Insert_Chunk(struct chunk *c2, u_long offset, u_long size, const char *name,
/* Make a new chunk for any trailing unused space */
if (c2->end > ct->end) {
cs = new_chunk();
if (!cs) barfout(1, "malloc failed");
if (!cs) return __LINE__;
*cs = *c2;
cs->disk = c2->disk;
cs->offset = ct->end + 1;
@ -224,10 +224,10 @@ Add_Chunk(struct disk *d, long offset, u_long size, const char *name,
if (type == whole) {
d->chunks = c1 = new_chunk();
if (!c1) barfout(1, "malloc failed");
if (!c1) return __LINE__;
memset(c1, 0, sizeof *c1);
c2 = c1->part = new_chunk();
if (!c2) barfout(1, "malloc failed");
if (!c2) return __LINE__;
memset(c2,0,sizeof *c2);
c2->disk = c1->disk = d;
c2->offset = c1->offset = offset;

View File

@ -56,19 +56,19 @@ msgDebug(char *fmt, ...)
write(DebugFD, dbg, strlen(dbg));
}
void
int
Fixup_FreeBSD_Names(struct disk *d, struct chunk *c)
{
struct chunk *c1, *c3;
int j;
if (!strcmp(c->name, "X")) return;
if (!strcmp(c->name, "X")) return 0;
/* reset all names to "X" */
for (c1 = c->part; c1; c1 = c1->next) {
c1->oname = c1->name;
c1->name = malloc(12);
if(!c1->name) barfout(1, "Malloc failed");
if(!c1->name) return -1;
strcpy(c1->name,"X");
}
@ -96,7 +96,7 @@ Fixup_FreeBSD_Names(struct disk *d, struct chunk *c)
goto newname;
}
strcpy(c1->name, c1->oname);
newname:
newname: ;
}
@ -121,9 +121,10 @@ Fixup_FreeBSD_Names(struct disk *d, struct chunk *c)
free(c1->oname);
c1->oname = 0;
}
return 0;
}
void
int
Fixup_Extended_Names(struct disk *d, struct chunk *c)
{
struct chunk *c1;
@ -133,14 +134,16 @@ Fixup_Extended_Names(struct disk *d, struct chunk *c)
if (c1->type == unused) continue;
free(c1->name);
c1->name = malloc(12);
if(!c1->name) barfout(1, "malloc failed");
if(!c1->name) return -1;
sprintf(c1->name, "%ss%d", d->chunks->name, j++);
if (c1->type == freebsd)
Fixup_FreeBSD_Names(d, c1);
if (Fixup_FreeBSD_Names(d, c1) != 0)
return -1;
}
return 0;
}
void
int
Fixup_Names(struct disk *d)
{
struct chunk *c1, *c2;
@ -159,7 +162,7 @@ Fixup_Names(struct disk *d)
continue;
#ifdef __i386__
c2->oname = malloc(12);
if(!c2->oname) barfout(1, "malloc failed");
if(!c2->oname) return -1;
for(j = 1; j <= NDOSPART; j++) {
sprintf(c2->oname, "%ss%d", c1->name, j);
for(c3 = c1->part; c3; c3 = c3->next)
@ -193,6 +196,7 @@ Fixup_Names(struct disk *d)
Fixup_Extended_Names(d, c2);
#endif
}
return 0;
}
int
@ -263,7 +267,7 @@ Create_Chunk_DWIM(struct disk *d, struct chunk *parent , u_long size, chunk_e ty
for (c1=parent->part; c1; c1 = c1->next)
if (c1->offset == offset)
return c1;
barfout(1, "Serious internal trouble");
/* barfout(1, "Serious internal trouble"); */
return 0;
}

View File

@ -60,8 +60,9 @@ Int_Open_Disk(const char *name, u_long size)
int i,fd;
struct diskslices ds;
struct disklabel dl;
char device[64];
char device[64], *buf;
struct disk *d;
u_long sector_size;
#ifdef PC98
unsigned char *p;
#else
@ -74,7 +75,7 @@ Int_Open_Disk(const char *name, u_long size)
strcat(device, name);
d = (struct disk *)malloc(sizeof *d);
if(!d) barfout(1, "malloc failed");
if(!d) return NULL;
memset(d, 0, sizeof *d);
fd = open(device, O_RDONLY);
@ -104,7 +105,7 @@ Int_Open_Disk(const char *name, u_long size)
printf("\n");
#endif
/* XXX --- ds.dss_slice[WHOLE_DISK_SLCIE].ds.size of MO disk is wrong!!! */
/* XXX --- ds.dss_slice[WHOLE_DISK_SLICE].ds.size of MO disk is wrong!!! */
#ifdef PC98
if (!size)
size = dl.d_ncylinders * dl.d_ntracks * dl.d_nsectors;
@ -113,10 +114,23 @@ Int_Open_Disk(const char *name, u_long size)
size = ds.dss_slices[WHOLE_DISK_SLICE].ds_size;
#endif
/* determine media sector size */
if ((buf = malloc(MAX_SEC_SIZE)) == NULL)
return NULL;
for (sector_size = MIN_SEC_SIZE; sector_size <= MAX_SEC_SIZE; sector_size *= 2) {
if (read(fd, buf, sector_size) == sector_size) {
d->sector_size = sector_size;
break;
}
}
free (buf);
if (sector_size > MAX_SEC_SIZE)
return NULL; /* could not determine sector size */
#ifdef PC98
p = (unsigned char*)read_block(fd, 1);
p = (unsigned char*)read_block(fd, 1, sector_size);
#else
p = read_block(fd, 0);
p = read_block(fd, 0, sector_size);
dp = (struct dos_partition*)(p + DOSPARTOFF);
for (i = 0; i < NDOSPART; i++) {
if (Read_Int32(&dp->dp_start) >= size)
@ -416,7 +430,7 @@ Clone_Disk(struct disk *d)
struct disk *d2;
d2 = (struct disk*) malloc(sizeof *d2);
if(!d2) barfout(1, "malloc failed");
if(!d2) return NULL;
*d2 = *d;
d2->name = strdup(d2->name);
d2->chunks = Clone_Chunk(d2->chunks);
@ -490,7 +504,7 @@ Disk_Names()
disklist = (char *)malloc(listsize);
error = sysctlbyname("kern.disks", disklist, &listsize, NULL, 0);
if (error)
barfout(1, "sysctlbyname(\"kern.disks\") failed");
return NULL;
k = 0;
for (dp = disks; ((*dp = strsep(&disklist, " ")) != NULL) && k < MAX_NO_DISKS; k++, dp++);
return disks;
@ -531,8 +545,7 @@ Set_Boot_Mgr(struct disk *d, const u_char *b, const size_t s)
#endif
{
#ifdef PC98
/* XXX - assumes sector size of 512 */
if (bootipl_size % 512 != 0)
if (bootipl_size % d->sector_size != 0)
return;
if (d->bootipl)
free(d->bootipl);
@ -541,12 +554,11 @@ Set_Boot_Mgr(struct disk *d, const u_char *b, const size_t s)
} else {
d->bootipl_size = bootipl_size;
d->bootipl = malloc(bootipl_size);
if(!d->bootipl) barfout(1, "malloc failed");
if(!d->bootipl) return;
memcpy(d->bootipl, bootipl, bootipl_size);
}
/* XXX - assumes sector size of 512 */
if (bootmenu_size % 512 != 0)
if (bootmenu_size % d->sector_size != 0)
return;
if (d->bootmenu)
free(d->bootmenu);
@ -555,12 +567,11 @@ Set_Boot_Mgr(struct disk *d, const u_char *b, const size_t s)
} else {
d->bootmenu_size = bootmenu_size;
d->bootmenu = malloc(bootmenu_size);
if(!d->bootmenu) barfout(1, "malloc failed");
if(!d->bootmenu) return;
memcpy(d->bootmenu, bootmenu, bootmenu_size);
}
#else
/* XXX - assumes sector size of 512 */
if (s % 512 != 0)
if (s % d->sector_size != 0)
return;
if (d->bootmgr)
free(d->bootmgr);
@ -569,30 +580,31 @@ Set_Boot_Mgr(struct disk *d, const u_char *b, const size_t s)
} else {
d->bootmgr_size = s;
d->bootmgr = malloc(s);
if(!d->bootmgr) barfout(1, "malloc failed");
if(!d->bootmgr) return;
memcpy(d->bootmgr, b, s);
}
#endif
}
void
int
Set_Boot_Blocks(struct disk *d, const u_char *b1, const u_char *b2)
{
#if defined(__i386__)
if (d->boot1) free(d->boot1);
d->boot1 = malloc(512);
if(!d->boot1) barfout(1, "malloc failed");
if(!d->boot1) return -1;
memcpy(d->boot1, b1, 512);
if (d->boot2) free(d->boot2);
d->boot2 = malloc(15 * 512);
if(!d->boot2) barfout(1, "malloc failed");
if(!d->boot2) return -1;
memcpy(d->boot2, b2, 15 * 512);
#elif defined(__alpha__)
if (d->boot1) free(d->boot1);
d->boot1 = malloc(15 * 512);
if(!d->boot1) barfout(1, "malloc failed");
if(!d->boot1) return -1;
memcpy(d->boot1, b1, 15 * 512);
#endif
return 0;
}
const char *

View File

@ -17,11 +17,11 @@
#include "libdisk.h"
struct disklabel *
read_disklabel(int fd, daddr_t block)
read_disklabel(int fd, daddr_t block, u_long sector_size)
{
struct disklabel *dp;
dp = (struct disklabel *) read_block(fd, block);
dp = (struct disklabel *) read_block(fd, block, sector_size);
if (dp->d_magic != DISKMAGIC)
return 0;
if (dp->d_magic2 != DISKMAGIC)

View File

@ -142,6 +142,7 @@ struct disk {
u_char *boot1;
u_char *boot2;
struct chunk *chunks;
u_long sector_size;
};
.Ed
The only flag value by now is

View File

@ -10,17 +10,12 @@
*
*/
#ifdef __i386__
#include <err.h>
#define barfout(n, errstr) err(n, errstr)
#else
#include <stdio.h>
#define barfout(n, errstr) fprintf(stderr, "\n\n\t***[ %s ]***\t\n\n", errstr)
#endif
#define MAX_NO_DISKS 20
#define MAX_NO_DISKS 32
/* Max # of disks Disk_Names() will return */
#define MAX_SEC_SIZE 2048 /* maximum sector size that is supported */
#define MIN_SEC_SIZE 512 /* the sector size to end sensing at */
typedef enum {
whole,
unknown,
@ -53,6 +48,7 @@ struct disk {
u_char *boot2;
#endif
struct chunk *chunks;
u_long sector_size; /* media sector size, a power of 2 */
};
struct chunk {
@ -190,10 +186,10 @@ Set_Boot_Mgr(struct disk *d, const u_char *bootmgr, const size_t bootmgr_size);
* is called
*/
void
int
Set_Boot_Blocks(struct disk *d, const u_char *_boot1, const u_char *_boot2);
/* Use these boot-blocks on this disk. Gets written when Write_Disk()
* is called
* is called. Returns nonzero upon failure.
*/
int
@ -270,12 +266,12 @@ int Add_Chunk(struct disk *, long, u_long, const char *, chunk_e, int, u_long, c
#else
int Add_Chunk(struct disk *, long, u_long, const char *, chunk_e, int, u_long);
#endif
void * read_block(int, daddr_t);
void write_block(int fd, daddr_t block, void *foo);
struct disklabel * read_disklabel(int, daddr_t);
void * read_block(int, daddr_t, u_long);
int write_block(int, daddr_t, 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);
void Fixup_Names(struct disk *);
int Fixup_Names(struct disk *);
__END_DECLS
#define dprintf printf

View File

@ -34,6 +34,9 @@
#else
#define WHERE(offset,disk) (disk->flags & DISK_ON_TRACK ? offset + 63 : offset)
#endif
/* 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 */
int
Write_FreeBSD(int fd, struct disk *new, struct disk *old, struct chunk *c1)
{
@ -47,7 +50,7 @@ Write_FreeBSD(int fd, struct disk *new, struct disk *old, struct chunk *c1)
#endif
for(i = 0; i < BBSIZE/512; i++) {
p = read_block(fd, WHERE(i + c1->offset, new));
p = read_block(fd, WHERE(i + c1->offset, new), 512);
memcpy(buf + 512 * i, p, 512);
free(p);
}
@ -140,7 +143,7 @@ Write_FreeBSD(int fd, struct disk *new, struct disk *old, struct chunk *c1)
#endif /*__alpha__*/
for(i=0;i<BBSIZE/512;i++) {
write_block(fd,WHERE(i + c1->offset, new), buf + 512 * i);
write_block(fd,WHERE(i + c1->offset, new), buf + 512 * i, 512);
}
return 0;
@ -227,9 +230,9 @@ Write_Disk(struct disk *d1)
memset(s,0,sizeof s);
#ifdef PC98
mbr = read_block(fd, WHERE(1, d1));
mbr = read_block(fd, WHERE(1, d1), d1->sector_size);
#else
mbr = read_block(fd, WHERE(0, d1));
mbr = read_block(fd, WHERE(0, d1), d1->sector_size);
#endif
dp = (struct dos_partition*)(mbr + DOSPARTOFF);
memcpy(work, dp, sizeof work);
@ -362,9 +365,9 @@ Write_Disk(struct disk *d1)
#ifdef PC98
if (d1->bootipl)
write_block(fd, WHERE(0, d1), d1->bootipl);
write_block(fd, WHERE(0, d1), d1->bootipl, d1->sector_size);
mbr = read_block(fd, WHERE(1, d1));
mbr = read_block(fd, WHERE(1, d1), d1->sector_size);
memcpy(mbr + DOSPARTOFF, dp, sizeof *dp * NDOSPART);
/* XXX - for entire FreeBSD(98) */
for (c1 = d1->chunks->part; c1; c1 = c1->next)
@ -372,13 +375,13 @@ Write_Disk(struct disk *d1)
&& (c1->offset == 0))
PC98_EntireDisk = 1;
if (PC98_EntireDisk == 0)
write_block(fd, WHERE(1, d1), mbr);
write_block(fd, WHERE(1, d1), mbr, d1->sector_size);
if (d1->bootmenu)
for (i = 0; i * 512 < d1->bootmenu_size; i++)
write_block(fd, WHERE(2 + i, d1), &d1->bootmenu[i * 512]);
for (i = 0; i * d1->sector_size < d1->bootmenu_size; i++)
write_block(fd, WHERE(2 + i, d1), &d1->bootmenu[i * d1->sector_size], d1->sector_size);
#else
mbr = read_block(fd, WHERE(0, d1));
mbr = read_block(fd, WHERE(0, d1), d1->sector_size);
if (d1->bootmgr) {
memcpy(mbr, d1->bootmgr, DOSPARTOFF);
Cfg_Boot_Mgr(mbr, need_edd);
@ -386,10 +389,10 @@ Write_Disk(struct disk *d1)
memcpy(mbr + DOSPARTOFF, dp, sizeof *dp * NDOSPART);
mbr[512-2] = 0x55;
mbr[512-1] = 0xaa;
write_block(fd, WHERE(0, d1), mbr);
if (d1->bootmgr && d1->bootmgr_size > 512)
for(i = 1; i * 512 <= d1->bootmgr_size; i++)
write_block(fd, WHERE(i, d1), &d1->bootmgr[i * 512]);
write_block(fd, WHERE(0, d1), 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, WHERE(i, d1), &d1->bootmgr[i * d1->sector_size], d1->sector_size);
#endif
#endif