diff options
author | phk <phk@FreeBSD.org> | 1995-04-29 01:55:25 +0000 |
---|---|---|
committer | phk <phk@FreeBSD.org> | 1995-04-29 01:55:25 +0000 |
commit | f7bf327ed4f468193062d32f7b6bb2cc4b521e9b (patch) | |
tree | 18f4a0a2d2e1413fdf21f0dd954bae1ddab9657f /lib/libdisk | |
parent | 66c00a8f24111e84617ee4d9c63bce82a6738d38 (diff) | |
download | FreeBSD-src-f7bf327ed4f468193062d32f7b6bb2cc4b521e9b.zip FreeBSD-src-f7bf327ed4f468193062d32f7b6bb2cc4b521e9b.tar.gz |
Sanitize and explain a little bit... Implement the first rules...
Diffstat (limited to 'lib/libdisk')
-rw-r--r-- | lib/libdisk/Makefile | 2 | ||||
-rw-r--r-- | lib/libdisk/change.c | 1 | ||||
-rw-r--r-- | lib/libdisk/chunk.c | 23 | ||||
-rw-r--r-- | lib/libdisk/create_chunk.c | 1 | ||||
-rw-r--r-- | lib/libdisk/disk.c | 5 | ||||
-rw-r--r-- | lib/libdisk/libdisk.h | 105 | ||||
-rw-r--r-- | lib/libdisk/rules.c | 95 | ||||
-rw-r--r-- | lib/libdisk/tst01.c | 39 |
8 files changed, 198 insertions, 73 deletions
diff --git a/lib/libdisk/Makefile b/lib/libdisk/Makefile index e4396f5..6a5fcdb 100644 --- a/lib/libdisk/Makefile +++ b/lib/libdisk/Makefile @@ -1,6 +1,6 @@ .PATH: /usr/src/sbin/disklabel OBJS= tst01.o blocks.o disklabel.o dkcksum.o chunk.o disk.o change.o \ - create_chunk.o + create_chunk.o rules.o CFLAGS+= -Wall test: tst01 diff --git a/lib/libdisk/change.c b/lib/libdisk/change.c index 17dd782..b1db61c 100644 --- a/lib/libdisk/change.c +++ b/lib/libdisk/change.c @@ -17,7 +17,6 @@ #include <string.h> #include <err.h> #include <sys/types.h> -#include <sys/queue.h> #include "libdisk.h" struct disk * diff --git a/lib/libdisk/chunk.c b/lib/libdisk/chunk.c index 088b5ab..a286cb1 100644 --- a/lib/libdisk/chunk.c +++ b/lib/libdisk/chunk.c @@ -15,11 +15,11 @@ #include <unistd.h> #include <string.h> #include <sys/types.h> -#include <sys/queue.h> #include <err.h> #include "libdisk.h" CHAR_N; + #define new_chunk() malloc(sizeof(struct chunk)) /* Is c2 completely inside c1 ? */ @@ -206,6 +206,10 @@ Add_Chunk(struct disk *d, u_long offset, u_long size, char *name, chunk_e type, c1 = Find_Mother_Chunk(d->chunks,offset,end,whole); if(!c1 && type == part) c1 = Find_Mother_Chunk(d->chunks,offset,end,freebsd); + if(!c1 && type == reserved) + c1 = Find_Mother_Chunk(d->chunks,offset,end,extended); + if(!c1 && type == reserved) + c1 = Find_Mother_Chunk(d->chunks,offset,end,whole); if(!c1) return __LINE__; for(c2=c1->part;c2;c2=c2->next) { @@ -252,26 +256,25 @@ Bios_Limit_Chunk(struct chunk *c1, u_long limit) } int -Delete_Chunk(struct disk *d, u_long offset, u_long end, chunk_e type) +Delete_Chunk(struct disk *d, struct chunk *c) { struct chunk *c1=0,*c2,*c3; + chunk_e type = c->type; if(type == whole) return 1; if(!c1 && (type == freebsd || type == fat || type == foo)) - c1 = Find_Mother_Chunk(d->chunks,offset,end,extended); + c1 = Find_Mother_Chunk(d->chunks,c->offset,c->end,extended); if(!c1 && (type == freebsd || type == fat || type == foo)) - c1 = Find_Mother_Chunk(d->chunks,offset,end,whole); + c1 = Find_Mother_Chunk(d->chunks,c->offset,c->end,whole); if(!c1 && type == extended) - c1 = Find_Mother_Chunk(d->chunks,offset,end,whole); + c1 = Find_Mother_Chunk(d->chunks,c->offset,c->end,whole); if(!c1 && type == part) - c1 = Find_Mother_Chunk(d->chunks,offset,end,freebsd); + c1 = Find_Mother_Chunk(d->chunks,c->offset,c->end,freebsd); if(!c1) return 1; for(c2=c1->part;c2;c2=c2->next) { - if (c2->offset == offset && - c2->end == end && - c2->type == type) { + if (c2 == c) { c2->type = unused; c2->subtype = 0; c2->flags = 0; @@ -329,7 +332,7 @@ Collapse_Chunk(struct disk *d, struct chunk *c1) return 0; if(c3->type == unused && c3->size == c1->size) { - Delete_Chunk(d,c1->offset, c1->end, c1->type); + Delete_Chunk(d,c1); return 1; } if(c3->type == unused) { diff --git a/lib/libdisk/create_chunk.c b/lib/libdisk/create_chunk.c index 6664820..7abcac9 100644 --- a/lib/libdisk/create_chunk.c +++ b/lib/libdisk/create_chunk.c @@ -15,7 +15,6 @@ #include <unistd.h> #include <string.h> #include <sys/types.h> -#include <sys/queue.h> #include <err.h> #include "libdisk.h" diff --git a/lib/libdisk/disk.c b/lib/libdisk/disk.c index 5ff2a21..4f78c5a 100644 --- a/lib/libdisk/disk.c +++ b/lib/libdisk/disk.c @@ -20,7 +20,6 @@ #include <sys/ioctl.h> #include <sys/disklabel.h> #include <sys/diskslice.h> -#include <sys/queue.h> #include "libdisk.h" #define DOSPTYP_EXTENDED 5 @@ -66,6 +65,7 @@ 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); for(i=2;i<ds.dss_nslices;i++) { char sname[20]; @@ -94,6 +94,9 @@ Int_Open_Disk(char *name, u_long size) flags |= CHUNK_ALIGN; Add_Chunk(d,ds.dss_slices[i].ds_offset, ds.dss_slices[i].ds_size, sname,ce,subtype,flags); + if (ce == extended) + Add_Chunk(d,ds.dss_slices[i].ds_offset, + 1, "-",reserved, subtype, flags); if (ds.dss_slices[i].ds_type == 0xa5) { struct disklabel *dl; int j; diff --git a/lib/libdisk/libdisk.h b/lib/libdisk/libdisk.h index 04c5a2f..2946f90 100644 --- a/lib/libdisk/libdisk.h +++ b/lib/libdisk/libdisk.h @@ -1,13 +1,25 @@ +/* + * ---------------------------------------------------------------------------- + * "THE BEER-WARE LICENSE" (Revision 42): + * <phk@login.dknet.dk> 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 + * ---------------------------------------------------------------------------- + * + * $Id$ + * + */ 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"}; struct disk { char *name; u_long flags; -#define DISK_ON_TRACK 1 -#define DISK_REAL_GEOM 2 +# define DISK_ON_TRACK 1 +# define DISK_REAL_GEOM 2 u_long real_cyl; u_long real_hd; u_long real_sect; @@ -26,36 +38,79 @@ struct chunk { char *name; chunk_e type; int subtype; -#define SUBTYPE_BSD_FS 1 -#define SUBTYPE_BSD_SWAP 2 -#define SUBTYPE_BSD_UNUSED 3 +# define SUBTYPE_BSD_FS 1 +# define SUBTYPE_BSD_SWAP 2 +# define SUBTYPE_BSD_UNUSED 3 u_long flags; -#define CHUNK_PAST_1024 1 - /* this chunk cannot be booted from */ -#define CHUNK_BSD_COMPAT 2 - /* this chunk is in the BSD-compatibility, and has a short name - * too, ie wd0s4f -> wd0f - */ -#define CHUNK_BAD144 4 - /* this chunk has bad144 mapping */ -#define CHUNK_ALIGN 8 +# define CHUNK_PAST_1024 1 + /* this chunk cannot be booted from */ +# define CHUNK_BSD_COMPAT 2 + /* this chunk is in the BSD-compatibility, and has a + * short name too, ie wd0s4f -> wd0f + */ +# define CHUNK_BAD144 4 + /* this chunk has bad144 mapping */ +# define CHUNK_ALIGN 8 }; -struct disk *Open_Disk(char *devname); -void Free_Disk(struct disk *disk); -void Debug_Disk(struct disk *disk); -struct disk *Clone_Disk(struct disk *disk); +struct disk * +Open_Disk(char *devname); + /* Will open the named disk, and return populated tree. + */ + +struct disk * +Clone_Disk(struct disk *disk); + /* Clone a copy of a tree. Useful for "Undo" functionality + */ + +void +Free_Disk(struct disk *disk); + /* Free a tree made with Open_Disk() or Clone_Disk() + */ + +void +Debug_Disk(struct disk *disk); + /* Print the content of the tree to stdout + */ + +struct disk * +Set_Phys_Geom(struct disk *disk, u_long cyl, u_long heads, u_long sects); + /* Use a different physical geometry. Makes sense for ST506 disks only. + * The tree returned is read from the disk, using this geometry. + */ + +void +Set_Bios_Geom(struct disk *disk, u_long cyl, u_long heads, u_long sects); + /* Set the geometry the bios uses. + */ + +int +Delete_Chunk(struct disk *disk, struct chunk *); + /* Free a chunk of disk_space + */ -struct disk *Set_Phys_Geom(struct disk *disk, u_long cyl, u_long heads, u_long sects); -void Set_Bios_Geom(struct disk *disk, u_long cyl, u_long heads, u_long sects); +void +Collapse_Disk(struct disk *disk); + /* Experimental, do not use. + */ +int +Collapse_Chunk(struct disk *disk, struct chunk *chunk); + /* Experimental, do not use. + */ -int Delete_Chunk(struct disk *disk, u_long offset, u_long end, chunk_e type); -void Collapse_Disk(struct disk *disk); -int Collapse_Chunk(struct disk *disk, struct chunk *chunk); +int +Create_Chunk(struct disk *disk, u_long offset, u_long size, chunk_e type, int subtype, u_long flags); + /* Create a chunk with the specified paramters + */ -int Create_Chunk(struct disk *disk, u_long offset, u_long size, chunk_e type, int subtype, u_long flags); +char * +CheckRules(struct disk *); + /* Return char* to warnings about broken design rules in this disklayout + */ -/* Implementation details */ +/* + * Implementation details >>> DO NOT USE <<< + */ struct disk *Int_Open_Disk(char *devname, u_long maxsize); diff --git a/lib/libdisk/rules.c b/lib/libdisk/rules.c new file mode 100644 index 0000000..7db89ea --- /dev/null +++ b/lib/libdisk/rules.c @@ -0,0 +1,95 @@ +/* + * ---------------------------------------------------------------------------- + * "THE BEER-WARE LICENSE" (Revision 42): + * <phk@login.dknet.dk> 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 + * ---------------------------------------------------------------------------- + * + * $Id$ + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <sys/types.h> +#include <sys/diskslice.h> +#include <sys/disklabel.h> +#include <err.h> +#include "libdisk.h" + +/* + * Rule#0: + * Chunks of type 'whole' can have max NDOSPART children. + */ +void +Rule_000(struct disk *d, struct chunk *c, char *msg) +{ + int i; + struct chunk *c1; + + if (c->type != whole) + return; + for (i=0, c1=c->part; c1; c1=c1->next) + if (c1->type != unused) + i++; + if (i <= NDOSPART) + return; + sprintf(msg+strlen(msg), + "%d is too many children of the 'whole' chunk. Max is %d\n", + i, NDOSPART); +} + +/* + * Rule#1: + * All children of 'whole' must be track-aligned + */ +void +Rule_001(struct disk *d, struct chunk *c, char *msg) +{ + int i; + struct chunk *c1; + + if (c->type != whole) + return; + for (i=0, c1=c->part; c1; c1=c1->next) { + if (c1->type == reserved) + continue; + if (c1->type == unused) + continue; + if (!Aligned(d,c1->offset)) + 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)) + sprintf(msg+strlen(msg), + "chunk '%s' [%ld..%ld] does not end on a track boundary\n", + c1->name,c1->offset,c1->end); + } +} + +void +Check_Chunk(struct disk *d, struct chunk *c, char *msg) +{ + Rule_000(d,c,msg); + Rule_001(d,c,msg); + if (c->part) + Check_Chunk(d,c->part,msg); + if (c->next) + Check_Chunk(d,c->next,msg); + return; +} + +char * +CheckRules(struct disk *d) +{ + char msg[BUFSIZ]; + + *msg = '\0'; + Check_Chunk(d,d->chunks,msg); + if (*msg) + return strdup(msg); + return 0; +} diff --git a/lib/libdisk/tst01.c b/lib/libdisk/tst01.c index bcc1344..9320356 100644 --- a/lib/libdisk/tst01.c +++ b/lib/libdisk/tst01.c @@ -14,37 +14,8 @@ #include <stdlib.h> #include <unistd.h> #include <sys/types.h> -#include <sys/ioctl.h> -#include <sys/disklabel.h> -#include <sys/diskslice.h> -#include <sys/queue.h> #include "libdisk.h" -void -fprint_diskslices(FILE *fi, struct diskslices *ds) -{ - int i; - - printf("@%p: struct diskslices\n",ds); - printf("\tdss_first_bsd_slice = %d\n",ds->dss_first_bsd_slice); - printf("\tdss_nslices = %d\n",ds->dss_nslices); - for(i=0;i<ds->dss_nslices;i++) { - printf("\tdss_slices[%d] = struct diskslice",i); - if (i == 0) - printf(" /* FreeBSD compatibility slice */\n"); - else if (i == 1) - printf(" /* Whole disk slice */\n"); - else if (i < 6) - printf(" /* Primary MBR slice %d */\n",i-1); - else - printf("\n"); - printf("\t\tds_offset = %lu\n",ds->dss_slices[i].ds_offset); - printf("\t\tds_size = %lu\n",ds->dss_slices[i].ds_size); - printf("\t\tds_type = %u\n",ds->dss_slices[i].ds_type); - printf("\t\tds_openmask = %u\n",ds->dss_slices[i].ds_openmask); - } -} - int main(int argc, char **argv) { @@ -55,12 +26,12 @@ main(int argc, char **argv) d = Open_Disk(argv[i]); if (!d) continue; Debug_Disk(d); - Delete_Chunk(d,0,4108599,freebsd); - Debug_Disk(d); - printf("Create=%d\n",Create_Chunk(d,0,32768,fat,0,0)); - printf("Create=%d\n",Create_Chunk(d,192512,409600,freebsd,0,0)); - printf("Create=%d\n",Create_Chunk(d,192512,409600,part,0,0)); + 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)); } exit (0); } |