From f9c10dfd1a49c87ca42b7ad41173c959bfd5aa64 Mon Sep 17 00:00:00 2001 From: Poul-Henning Kamp Date: Sat, 29 Apr 1995 04:00:57 +0000 Subject: [PATCH] Next Iteration, getting better. Made an All_FreeBSD() function. Added a cmd-line interface (lowest rank) to the tst01 program. The tst01 program is harmless (worst it can do is coredump), but it is instructive to run, you can see what the slice-code things of your disk... --- lib/libdisk/Makefile | 7 ++- lib/libdisk/change.c | 17 ++++- lib/libdisk/chunk.c | 6 +- lib/libdisk/disk.c | 27 ++------ lib/libdisk/libdisk.h | 9 ++- lib/libdisk/rules.c | 33 +++++++++- lib/libdisk/tst01.c | 128 ++++++++++++++++++++++++++++++++++---- release/libdisk/Makefile | 7 ++- release/libdisk/change.c | 17 ++++- release/libdisk/chunk.c | 6 +- release/libdisk/disk.c | 27 ++------ release/libdisk/libdisk.h | 9 ++- release/libdisk/rules.c | 33 +++++++++- release/libdisk/tst01.c | 128 ++++++++++++++++++++++++++++++++++---- 14 files changed, 362 insertions(+), 92 deletions(-) diff --git a/lib/libdisk/Makefile b/lib/libdisk/Makefile index 6a5fcdbd7f4..8f55911b235 100644 --- a/lib/libdisk/Makefile +++ b/lib/libdisk/Makefile @@ -1,11 +1,14 @@ .PATH: /usr/src/sbin/disklabel OBJS= tst01.o blocks.o disklabel.o dkcksum.o chunk.o disk.o change.o \ create_chunk.o rules.o -CFLAGS+= -Wall +CFLAGS+= -Wall -g test: tst01 cp tst01 /0 ./tst01 sd0 tst01: ${OBJS} - cc ${CFLAGS} -static -o tst01 ${OBJS} + cc ${CFLAGS} -static -o tst01 ${OBJS} -lreadline -ltermcap + +clean: + rm -f *.o *.core tst01 diff --git a/lib/libdisk/change.c b/lib/libdisk/change.c index b1db61c34a2..76d28cf1baf 100644 --- a/lib/libdisk/change.c +++ b/lib/libdisk/change.c @@ -6,7 +6,7 @@ * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp * ---------------------------------------------------------------------------- * - * $Id$ + * $Id: change.c,v 1.2 1995/04/29 01:55:18 phk Exp $ * */ @@ -42,3 +42,18 @@ Set_Bios_Geom(struct disk *disk, u_long cyl, u_long hd, u_long sect) disk->bios_sect = sect; Bios_Limit_Chunk(disk->chunks,1024*hd*sect); } + +void +All_FreeBSD(struct disk *d) +{ + struct chunk *c; + + again: + for (c=d->chunks->part;c;c=c->next) + if (c->type != unused) { + Delete_Chunk(d,c); + goto again; + } + c=d->chunks; + Create_Chunk(d,c->offset,c->size,freebsd,0,0); +} diff --git a/lib/libdisk/chunk.c b/lib/libdisk/chunk.c index a286cb11d7f..e81c646ae3b 100644 --- a/lib/libdisk/chunk.c +++ b/lib/libdisk/chunk.c @@ -6,7 +6,7 @@ * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp * ---------------------------------------------------------------------------- * - * $Id$ + * $Id: chunk.c,v 1.2 1995/04/29 01:55:19 phk Exp $ * */ @@ -228,8 +228,8 @@ Print_Chunk(struct chunk *c1,int offset) if(!c1) return; for(i=0;i'); for(;i<10;i++) putchar(' '); - printf("%10lu %10lu %10lu %-8s %d %-8s %d %lx\n", - c1->offset, c1->size, c1->end, c1->name, + printf("%p %10lu %10lu %10lu %-8s %d %-8s %d %lx\n", + c1, c1->offset, c1->size, c1->end, c1->name, c1->type, chunk_n[c1->type],c1->subtype,c1->flags); Print_Chunk(c1->part,offset + 2); Print_Chunk(c1->next,offset); diff --git a/lib/libdisk/disk.c b/lib/libdisk/disk.c index 4f78c5ae46e..154e68725a8 100644 --- a/lib/libdisk/disk.c +++ b/lib/libdisk/disk.c @@ -6,7 +6,7 @@ * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp * ---------------------------------------------------------------------------- * - * $Id$ + * $Id: disk.c,v 1.2 1995/04/29 01:55:21 phk Exp $ * */ @@ -65,9 +65,10 @@ Int_Open_Disk(char *name, u_long size) size = ds.dss_slices[WHOLE_DISK_SLICE].ds_size; Add_Chunk(d, 0, size, name,whole,0,0); - Add_Chunk(d, 0, 1, "-",reserved,0,0); + if (ds.dss_slices[COMPATIBILITY_SLICE].ds_offset) + Add_Chunk(d, 0, 1, "-",reserved,0,0); - for(i=2;ichunks)) ; } - -int -Aligned(struct disk *d, u_long offset) -{ - if (offset % d->bios_sect) - return 0; - return 1; -} - -u_long -Prev_Aligned(struct disk *d, u_long offset) -{ - return (offset / d->bios_sect) * d->bios_sect; -} - -u_long -Next_Aligned(struct disk *d, u_long offset) -{ - return Prev_Aligned(d,offset + d->bios_sect); -} diff --git a/lib/libdisk/libdisk.h b/lib/libdisk/libdisk.h index 2946f90fb0b..e4412cf1ebd 100644 --- a/lib/libdisk/libdisk.h +++ b/lib/libdisk/libdisk.h @@ -6,14 +6,14 @@ * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp * ---------------------------------------------------------------------------- * - * $Id$ + * $Id: libdisk.h,v 1.2 1995/04/29 01:55:23 phk Exp $ * */ typedef enum {whole, foo, fat, freebsd, extended, part, unused, reserved} chunk_e; #define CHAR_N static char *chunk_n[] = { \ - "whole","foo","fat","freebsd","extended","part","unused","reserved"}; + "whole","foo","fat","freebsd","extended","part","unused","reserved",0}; struct disk { char *name; @@ -103,6 +103,11 @@ Create_Chunk(struct disk *disk, u_long offset, u_long size, chunk_e type, int su /* Create a chunk with the specified paramters */ +void +All_FreeBSD(struct disk *d); + /* Make one FreeBSD chunk covering the entire disk + */ + char * CheckRules(struct disk *); /* Return char* to warnings about broken design rules in this disklayout diff --git a/lib/libdisk/rules.c b/lib/libdisk/rules.c index 7db89ea9e52..447c6de4f58 100644 --- a/lib/libdisk/rules.c +++ b/lib/libdisk/rules.c @@ -6,7 +6,7 @@ * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp * ---------------------------------------------------------------------------- * - * $Id$ + * $Id: rules.c,v 1.1 1995/04/29 01:55:24 phk Exp $ * */ @@ -20,6 +20,32 @@ #include #include "libdisk.h" +int +Aligned(struct disk *d, u_long offset) +{ + if (!d->bios_sect) + return 1; + if (offset % d->bios_sect) + return 0; + return 1; +} + +u_long +Prev_Aligned(struct disk *d, u_long offset) +{ + if (!d->bios_sect) + return offset; + return (offset / d->bios_sect) * d->bios_sect; +} + +u_long +Next_Aligned(struct disk *d, u_long offset) +{ + if (!d->bios_sect) + return offset; + return Prev_Aligned(d,offset + d->bios_sect); +} + /* * Rule#0: * Chunks of type 'whole' can have max NDOSPART children. @@ -44,7 +70,8 @@ Rule_000(struct disk *d, struct chunk *c, char *msg) /* * Rule#1: - * All children of 'whole' must be track-aligned + * All children of 'whole' must be track-aligned. + * Exception: the end can be unaligned if it matches the end of 'whole' */ void Rule_001(struct disk *d, struct chunk *c, char *msg) @@ -63,7 +90,7 @@ Rule_001(struct disk *d, struct chunk *c, char *msg) sprintf(msg+strlen(msg), "chunk '%s' [%ld..%ld] does not start on a track boundary\n", c1->name,c1->offset,c1->end); - if (!Aligned(d,c1->end+1)) + if (c->end != c1->end && !Aligned(d,c1->end+1)) sprintf(msg+strlen(msg), "chunk '%s' [%ld..%ld] does not end on a track boundary\n", c1->name,c1->offset,c1->end); diff --git a/lib/libdisk/tst01.c b/lib/libdisk/tst01.c index 9320356584c..8ed705a614a 100644 --- a/lib/libdisk/tst01.c +++ b/lib/libdisk/tst01.c @@ -6,32 +6,136 @@ * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp * ---------------------------------------------------------------------------- * - * $Id$ + * $Id: tst01.c,v 1.2 1995/04/29 01:55:25 phk Exp $ * */ #include #include #include +#include +#include +#include #include #include "libdisk.h" +CHAR_N; + int main(int argc, char **argv) { - int i; - struct disk *d; + struct disk *d,*db; + char myprompt[BUFSIZ]; + char *p,*q=0; + char **cp,*cmds[200]; + int ncmd,i; - for(i=1;i ",argv[0],argv[1]); + while(1) { + printf("\n\n\n\n"); Debug_Disk(d); - if (d->chunks->size == 1411200) - Set_Bios_Geom(d,1024,15,63); - else - Set_Bios_Geom(d,2003,64,32); - Debug_Disk(d); - printf("<%s>\n",CheckRules(d)); + p = CheckRules(d); + if (p) { + printf("%s",p); + free(p); + } + if (q) + free(q); + q = p = readline(myprompt); + if(!p) + break; + for(cp = cmds; (*cp = strsep(&p, " \t")) != NULL;) + if (**cp != '\0') + cp++; + ncmd = cp - cmds; + if(!ncmd) + continue; + if (!strcasecmp(*cmds,"quit")) { break; } + if (!strcasecmp(*cmds,"exit")) { break; } + if (!strcasecmp(*cmds,"q")) { break; } + if (!strcasecmp(*cmds,"x")) { break; } + if (!strcasecmp(*cmds,"delete") && ncmd == 2) { + printf("delete = %d\n", + Delete_Chunk(d, + (struct chunk *)strtol(cmds[1],0,0))); + continue; + } + if (!strcasecmp(*cmds,"allfreebsd")) { + All_FreeBSD(d); + continue; + } + if (!strcasecmp(*cmds,"bios") && ncmd == 4) { + Set_Bios_Geom(d, + strtol(cmds[1],0,0), + strtol(cmds[2],0,0), + strtol(cmds[3],0,0)); + continue; + } + if (!strcasecmp(*cmds,"phys") && ncmd == 4) { + d = Set_Phys_Geom(d, + strtol(cmds[1],0,0), + strtol(cmds[2],0,0), + strtol(cmds[3],0,0)); + continue; + } + if (!strcasecmp(*cmds,"collapse")) { + if (cmds[1]) + while (Collapse_Chunk(d, + (struct chunk *)strtol(cmds[1],0,0))) + ; + else + Collapse_Disk(d); + continue; + } + if (!strcasecmp(*cmds,"read")) { + db=d; + if (cmds[1]) + d = Open_Disk(cmds[1]); + else + d = Open_Disk(db->name); + if (!d) { + fprintf(stderr,"Failed to open %s\n",argv[1]); + d = db; + } else { + Free_Disk(db); + } + continue; + } + if (!strcasecmp(*cmds,"create") && ncmd == 6) { + + printf("Create=%d\n", + Create_Chunk(d, + strtol(cmds[1],0,0), + strtol(cmds[2],0,0), + strtol(cmds[3],0,0), + strtol(cmds[4],0,0), + strtol(cmds[5],0,0))); + continue; + } + if (strcasecmp(*cmds,"help")) + printf("\007ERROR\n"); + printf("CMDS:\n"); + printf("\tallfreebsd\n"); + printf("\tbios cyl hd sect\n"); + printf("\tcollapse [pointer]\n"); + printf("\tcreate offset size enum subtype flags\n"); + printf("\tdelete pointer\n"); + printf("\tphys cyl hd sect\n"); + printf("\tquit\n"); + printf("\tread [disk]\n"); + printf("\nENUM:\n\t"); + for(i=0;chunk_n[i];i++) + printf("%d = %s%s",i,chunk_n[i],i == 4 ? "\n\t" : " "); + printf("\n"); + } exit (0); } diff --git a/release/libdisk/Makefile b/release/libdisk/Makefile index 6a5fcdbd7f4..8f55911b235 100644 --- a/release/libdisk/Makefile +++ b/release/libdisk/Makefile @@ -1,11 +1,14 @@ .PATH: /usr/src/sbin/disklabel OBJS= tst01.o blocks.o disklabel.o dkcksum.o chunk.o disk.o change.o \ create_chunk.o rules.o -CFLAGS+= -Wall +CFLAGS+= -Wall -g test: tst01 cp tst01 /0 ./tst01 sd0 tst01: ${OBJS} - cc ${CFLAGS} -static -o tst01 ${OBJS} + cc ${CFLAGS} -static -o tst01 ${OBJS} -lreadline -ltermcap + +clean: + rm -f *.o *.core tst01 diff --git a/release/libdisk/change.c b/release/libdisk/change.c index b1db61c34a2..76d28cf1baf 100644 --- a/release/libdisk/change.c +++ b/release/libdisk/change.c @@ -6,7 +6,7 @@ * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp * ---------------------------------------------------------------------------- * - * $Id$ + * $Id: change.c,v 1.2 1995/04/29 01:55:18 phk Exp $ * */ @@ -42,3 +42,18 @@ Set_Bios_Geom(struct disk *disk, u_long cyl, u_long hd, u_long sect) disk->bios_sect = sect; Bios_Limit_Chunk(disk->chunks,1024*hd*sect); } + +void +All_FreeBSD(struct disk *d) +{ + struct chunk *c; + + again: + for (c=d->chunks->part;c;c=c->next) + if (c->type != unused) { + Delete_Chunk(d,c); + goto again; + } + c=d->chunks; + Create_Chunk(d,c->offset,c->size,freebsd,0,0); +} diff --git a/release/libdisk/chunk.c b/release/libdisk/chunk.c index a286cb11d7f..e81c646ae3b 100644 --- a/release/libdisk/chunk.c +++ b/release/libdisk/chunk.c @@ -6,7 +6,7 @@ * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp * ---------------------------------------------------------------------------- * - * $Id$ + * $Id: chunk.c,v 1.2 1995/04/29 01:55:19 phk Exp $ * */ @@ -228,8 +228,8 @@ Print_Chunk(struct chunk *c1,int offset) if(!c1) return; for(i=0;i'); for(;i<10;i++) putchar(' '); - printf("%10lu %10lu %10lu %-8s %d %-8s %d %lx\n", - c1->offset, c1->size, c1->end, c1->name, + printf("%p %10lu %10lu %10lu %-8s %d %-8s %d %lx\n", + c1, c1->offset, c1->size, c1->end, c1->name, c1->type, chunk_n[c1->type],c1->subtype,c1->flags); Print_Chunk(c1->part,offset + 2); Print_Chunk(c1->next,offset); diff --git a/release/libdisk/disk.c b/release/libdisk/disk.c index 4f78c5ae46e..154e68725a8 100644 --- a/release/libdisk/disk.c +++ b/release/libdisk/disk.c @@ -6,7 +6,7 @@ * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp * ---------------------------------------------------------------------------- * - * $Id$ + * $Id: disk.c,v 1.2 1995/04/29 01:55:21 phk Exp $ * */ @@ -65,9 +65,10 @@ Int_Open_Disk(char *name, u_long size) size = ds.dss_slices[WHOLE_DISK_SLICE].ds_size; Add_Chunk(d, 0, size, name,whole,0,0); - Add_Chunk(d, 0, 1, "-",reserved,0,0); + if (ds.dss_slices[COMPATIBILITY_SLICE].ds_offset) + Add_Chunk(d, 0, 1, "-",reserved,0,0); - for(i=2;ichunks)) ; } - -int -Aligned(struct disk *d, u_long offset) -{ - if (offset % d->bios_sect) - return 0; - return 1; -} - -u_long -Prev_Aligned(struct disk *d, u_long offset) -{ - return (offset / d->bios_sect) * d->bios_sect; -} - -u_long -Next_Aligned(struct disk *d, u_long offset) -{ - return Prev_Aligned(d,offset + d->bios_sect); -} diff --git a/release/libdisk/libdisk.h b/release/libdisk/libdisk.h index 2946f90fb0b..e4412cf1ebd 100644 --- a/release/libdisk/libdisk.h +++ b/release/libdisk/libdisk.h @@ -6,14 +6,14 @@ * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp * ---------------------------------------------------------------------------- * - * $Id$ + * $Id: libdisk.h,v 1.2 1995/04/29 01:55:23 phk Exp $ * */ typedef enum {whole, foo, fat, freebsd, extended, part, unused, reserved} chunk_e; #define CHAR_N static char *chunk_n[] = { \ - "whole","foo","fat","freebsd","extended","part","unused","reserved"}; + "whole","foo","fat","freebsd","extended","part","unused","reserved",0}; struct disk { char *name; @@ -103,6 +103,11 @@ Create_Chunk(struct disk *disk, u_long offset, u_long size, chunk_e type, int su /* Create a chunk with the specified paramters */ +void +All_FreeBSD(struct disk *d); + /* Make one FreeBSD chunk covering the entire disk + */ + char * CheckRules(struct disk *); /* Return char* to warnings about broken design rules in this disklayout diff --git a/release/libdisk/rules.c b/release/libdisk/rules.c index 7db89ea9e52..447c6de4f58 100644 --- a/release/libdisk/rules.c +++ b/release/libdisk/rules.c @@ -6,7 +6,7 @@ * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp * ---------------------------------------------------------------------------- * - * $Id$ + * $Id: rules.c,v 1.1 1995/04/29 01:55:24 phk Exp $ * */ @@ -20,6 +20,32 @@ #include #include "libdisk.h" +int +Aligned(struct disk *d, u_long offset) +{ + if (!d->bios_sect) + return 1; + if (offset % d->bios_sect) + return 0; + return 1; +} + +u_long +Prev_Aligned(struct disk *d, u_long offset) +{ + if (!d->bios_sect) + return offset; + return (offset / d->bios_sect) * d->bios_sect; +} + +u_long +Next_Aligned(struct disk *d, u_long offset) +{ + if (!d->bios_sect) + return offset; + return Prev_Aligned(d,offset + d->bios_sect); +} + /* * Rule#0: * Chunks of type 'whole' can have max NDOSPART children. @@ -44,7 +70,8 @@ Rule_000(struct disk *d, struct chunk *c, char *msg) /* * Rule#1: - * All children of 'whole' must be track-aligned + * All children of 'whole' must be track-aligned. + * Exception: the end can be unaligned if it matches the end of 'whole' */ void Rule_001(struct disk *d, struct chunk *c, char *msg) @@ -63,7 +90,7 @@ Rule_001(struct disk *d, struct chunk *c, char *msg) sprintf(msg+strlen(msg), "chunk '%s' [%ld..%ld] does not start on a track boundary\n", c1->name,c1->offset,c1->end); - if (!Aligned(d,c1->end+1)) + if (c->end != c1->end && !Aligned(d,c1->end+1)) sprintf(msg+strlen(msg), "chunk '%s' [%ld..%ld] does not end on a track boundary\n", c1->name,c1->offset,c1->end); diff --git a/release/libdisk/tst01.c b/release/libdisk/tst01.c index 9320356584c..8ed705a614a 100644 --- a/release/libdisk/tst01.c +++ b/release/libdisk/tst01.c @@ -6,32 +6,136 @@ * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp * ---------------------------------------------------------------------------- * - * $Id$ + * $Id: tst01.c,v 1.2 1995/04/29 01:55:25 phk Exp $ * */ #include #include #include +#include +#include +#include #include #include "libdisk.h" +CHAR_N; + int main(int argc, char **argv) { - int i; - struct disk *d; + struct disk *d,*db; + char myprompt[BUFSIZ]; + char *p,*q=0; + char **cp,*cmds[200]; + int ncmd,i; - for(i=1;i ",argv[0],argv[1]); + while(1) { + printf("\n\n\n\n"); Debug_Disk(d); - if (d->chunks->size == 1411200) - Set_Bios_Geom(d,1024,15,63); - else - Set_Bios_Geom(d,2003,64,32); - Debug_Disk(d); - printf("<%s>\n",CheckRules(d)); + p = CheckRules(d); + if (p) { + printf("%s",p); + free(p); + } + if (q) + free(q); + q = p = readline(myprompt); + if(!p) + break; + for(cp = cmds; (*cp = strsep(&p, " \t")) != NULL;) + if (**cp != '\0') + cp++; + ncmd = cp - cmds; + if(!ncmd) + continue; + if (!strcasecmp(*cmds,"quit")) { break; } + if (!strcasecmp(*cmds,"exit")) { break; } + if (!strcasecmp(*cmds,"q")) { break; } + if (!strcasecmp(*cmds,"x")) { break; } + if (!strcasecmp(*cmds,"delete") && ncmd == 2) { + printf("delete = %d\n", + Delete_Chunk(d, + (struct chunk *)strtol(cmds[1],0,0))); + continue; + } + if (!strcasecmp(*cmds,"allfreebsd")) { + All_FreeBSD(d); + continue; + } + if (!strcasecmp(*cmds,"bios") && ncmd == 4) { + Set_Bios_Geom(d, + strtol(cmds[1],0,0), + strtol(cmds[2],0,0), + strtol(cmds[3],0,0)); + continue; + } + if (!strcasecmp(*cmds,"phys") && ncmd == 4) { + d = Set_Phys_Geom(d, + strtol(cmds[1],0,0), + strtol(cmds[2],0,0), + strtol(cmds[3],0,0)); + continue; + } + if (!strcasecmp(*cmds,"collapse")) { + if (cmds[1]) + while (Collapse_Chunk(d, + (struct chunk *)strtol(cmds[1],0,0))) + ; + else + Collapse_Disk(d); + continue; + } + if (!strcasecmp(*cmds,"read")) { + db=d; + if (cmds[1]) + d = Open_Disk(cmds[1]); + else + d = Open_Disk(db->name); + if (!d) { + fprintf(stderr,"Failed to open %s\n",argv[1]); + d = db; + } else { + Free_Disk(db); + } + continue; + } + if (!strcasecmp(*cmds,"create") && ncmd == 6) { + + printf("Create=%d\n", + Create_Chunk(d, + strtol(cmds[1],0,0), + strtol(cmds[2],0,0), + strtol(cmds[3],0,0), + strtol(cmds[4],0,0), + strtol(cmds[5],0,0))); + continue; + } + if (strcasecmp(*cmds,"help")) + printf("\007ERROR\n"); + printf("CMDS:\n"); + printf("\tallfreebsd\n"); + printf("\tbios cyl hd sect\n"); + printf("\tcollapse [pointer]\n"); + printf("\tcreate offset size enum subtype flags\n"); + printf("\tdelete pointer\n"); + printf("\tphys cyl hd sect\n"); + printf("\tquit\n"); + printf("\tread [disk]\n"); + printf("\nENUM:\n\t"); + for(i=0;chunk_n[i];i++) + printf("%d = %s%s",i,chunk_n[i],i == 4 ? "\n\t" : " "); + printf("\n"); + } exit (0); }