Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-1-5]: src/sbin/mbrlabel Pull up revisions 1.8-15 (requested by lu...



details:   https://anonhg.NetBSD.org/src/rev/ff2f074018c7
branches:  netbsd-1-5
changeset: 490760:ff2f074018c7
user:      he <he%NetBSD.org@localhost>
date:      Mon Feb 26 22:30:25 2001 +0000

description:
Pull up revisions 1.8-15 (requested by lukem):
  Several fixes and enhancements:
   o only update in-core (and on-disk) disklabels if requested
   o be saner about adding MBR partitions to free disklabel slots
   o NTFS recognition
   o various cleanups

diffstat:

 sbin/mbrlabel/mbrlabel.c |  299 ++++++++++++++++++++++++++++++----------------
 1 files changed, 191 insertions(+), 108 deletions(-)

diffs (truncated from 407 to 300 lines):

diff -r 8596a44ef58e -r ff2f074018c7 sbin/mbrlabel/mbrlabel.c
--- a/sbin/mbrlabel/mbrlabel.c  Mon Feb 26 22:28:00 2001 +0000
+++ b/sbin/mbrlabel/mbrlabel.c  Mon Feb 26 22:30:25 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: mbrlabel.c,v 1.7 2000/03/15 11:56:02 fvdl Exp $        */
+/*     $NetBSD: mbrlabel.c,v 1.7.4.1 2001/02/26 22:30:25 he Exp $      */
 
 /*
  * Copyright (C) 1998 Wolfgang Solfrank.
@@ -33,47 +33,45 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: mbrlabel.c,v 1.7 2000/03/15 11:56:02 fvdl Exp $");
+__RCSID("$NetBSD: mbrlabel.c,v 1.7.4.1 2001/02/26 22:30:25 he Exp $");
 #endif /* not lint */
 
 #include <stdio.h>
 #include <fcntl.h>
+#include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <util.h>
 
 #include <sys/param.h>
+#define FSTYPENAMES
 #include <sys/disklabel.h>
 #include <sys/disklabel_mbr.h>
 #include <sys/ioctl.h>
 
 #include "dkcksum.h"
-
-#define        FIRSTPART       0
+#include "extern.h"
 
-int main __P((int, char **));
-void usage __P((void));
-void getlabel __P((int));
-void setlabel __P((int));
-int getparts __P((int, int, u_int32_t, u_int32_t));
-int nbsdtype __P((int));
-u_int32_t getlong __P((void *p));
+int    main(int, char **);
+void   usage(void);
+void   getlabel(int);
+void   setlabel(int, int);
+int    getparts(int, u_int32_t, u_int32_t, int);
+int    nbsdtype(int);
+u_int16_t      getshort(void *);
+u_int32_t      getlong(void *);
 
 struct disklabel label;
+extern char *__progname;
 
 void
-getlabel(sd)
-       int sd;
+getlabel(int sd)
 {
-       struct partition save;
 
        if (ioctl(sd, DIOCGDINFO, &label) < 0) {
                perror("get label");
                exit(1);
        }
-       save = label.d_partitions[RAW_PART];
-       memset(label.d_partitions, 0, sizeof label.d_partitions);
-       label.d_partitions[RAW_PART] = save;
        /*
         * Some ports seem to not set the number of partitions
         * correctly, albeit they seem to set the raw partiton ok!
@@ -83,12 +81,12 @@
 }
 
 void
-setlabel(sd)
-       int sd;
+setlabel(int sd, int doraw)
 {
+
        label.d_checksum = 0;
        label.d_checksum = dkcksum(&label);
-       if (ioctl(sd, DIOCSDINFO, &label) < 0) {
+       if (ioctl(sd, doraw ? DIOCWDINFO : DIOCSDINFO, &label) < 0) {
                perror("set label");
                exit(1);
        }
@@ -98,51 +96,57 @@
        int mbrtype;
        int nbsdtype;
 } typetable[] = {
-       { MBR_PTYPE_NETBSD, FS_BSDFFS },
-       { MBR_PTYPE_386BSD, FS_BSDFFS },
-       { MBR_PTYPE_FAT12, FS_MSDOS },
-       { MBR_PTYPE_FAT16S, FS_MSDOS },
-       { MBR_PTYPE_FAT16B, FS_MSDOS },
-       { MBR_PTYPE_FAT32, FS_MSDOS },
-       { MBR_PTYPE_FAT32L, FS_MSDOS },
-       { MBR_PTYPE_FAT16L, FS_MSDOS },
-       { MBR_PTYPE_LNXEXT2, FS_EX2FS },
+       { MBR_PTYPE_386BSD,     FS_BSDFFS },
+       { MBR_PTYPE_FAT12,      FS_MSDOS },
+       { MBR_PTYPE_FAT16B,     FS_MSDOS },
+       { MBR_PTYPE_FAT16L,     FS_MSDOS },
+       { MBR_PTYPE_FAT16S,     FS_MSDOS },
+       { MBR_PTYPE_FAT32,      FS_MSDOS },
+       { MBR_PTYPE_FAT32L,     FS_MSDOS },
+       { MBR_PTYPE_LNXEXT2,    FS_EX2FS },
+       { MBR_PTYPE_LNXSWAP,    FS_SWAP },
+       { MBR_PTYPE_NETBSD,     FS_BSDFFS },
+       { MBR_PTYPE_NTFS,       FS_NTFS },
        { 0, 0 }
 };
 
 int
-nbsdtype(type)
-       int type;
+nbsdtype(int type)
 {
        struct typetab *tt;
 
        for (tt = typetable; tt->mbrtype; tt++)
                if (tt->mbrtype == type)
-                       return tt->nbsdtype;
-       return FS_OTHER;
+                       return (tt->nbsdtype);
+       return (FS_OTHER);
 }
 
-u_int32_t
-getlong(p)
-       void *p;
+u_int16_t
+getshort(void *p)
 {
        unsigned char *cp = p;
 
-       return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
+       return (cp[0] | (cp[1] << 8));
+}
+
+u_int32_t
+getlong(void *p)
+{
+       unsigned char *cp = p;
+
+       return (cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24));
 }
 
 int
-getparts(sd, np, off, eoff)
-       int sd;
-       int np;
-       u_int32_t off;
-       u_int32_t eoff;
+getparts(int sd, u_int32_t off, u_int32_t extoff, int verbose)
 {
-       unsigned char buf[DEV_BSIZE];
-       struct mbr_partition parts[NMBRPART];
-       off_t loff = 0; /* XXX this nonsense shuts up GCC 2.7.2.2 */
-       int i;
+       unsigned char           buf[DEV_BSIZE];
+       struct mbr_partition    parts[NMBRPART];
+       struct partition        npe;
+       off_t                   loff;
+       int                     i, j, unused, changed;
 
+       changed = 0;
        loff = (off_t)off * DEV_BSIZE;
 
        if (lseek(sd, loff, SEEK_SET) != loff) {
@@ -153,94 +157,173 @@
                perror("read label");
                exit(1);
        }
-       if (buf[0x1fe] != 0x55 || buf[0x1ff] != 0xaa)
-               return np;
+       if (getshort(buf + MBR_MAGICOFF) != MBR_MAGIC)
+               return (changed);
        memcpy(parts, buf + MBR_PARTOFF, sizeof parts);
+
+                               /* scan partition table */
        for (i = 0; i < NMBRPART; i++) {
-               switch (parts[i].mbrp_typ) {
-               case 0:
-                       /* Nothing to do */
-                       break;
-               case MBR_PTYPE_EXT:
-               case MBR_PTYPE_EXT_LBA:
-               case MBR_PTYPE_EXT_LNX:
-                       /* Will be handled below */
+               if (parts[i].mbrp_typ == 0 ||
+                               /* extended partitions are handled below */
+                   MBR_IS_EXTENDED(parts[i].mbrp_typ))
+                       continue;
+
+               memset((void *)&npe, 0, sizeof(npe));
+               npe.p_size = getlong(&parts[i].mbrp_size);
+               npe.p_offset = getlong(&parts[i].mbrp_start) + off;
+               npe.p_fstype = nbsdtype(parts[i].mbrp_typ);
+
+                               /* find existing entry, or first free slot */
+               unused = -1;    /* flag as no free slot */
+               if (verbose)
+                       printf(
+                           "Found %s partition; size %u (%u MB), offset %u\n",
+                           fstypenames[npe.p_fstype],
+                           npe.p_size, npe.p_size / 2048, npe.p_offset);
+               for (j = 0; j < label.d_npartitions; j++) {
+                       struct partition *lpe;
+
+                       if (j == RAW_PART)
+                               continue;
+                       lpe = &label.d_partitions[j];
+                       if (lpe->p_size == npe.p_size &&
+                           lpe->p_offset == npe.p_offset
+#ifdef notyet
+                           && (lpe->p_fstype == npe.p_fstype ||
+                            lpe->p_fstype == FS_UNUSED) */
+#endif
+                            ) {
+                               if (verbose)
+                                       printf(
+                           "  skipping existing %s partition at slot %c.\n",
+                                           fstypenames[lpe->p_fstype],
+                                           j + 'a');
+                               unused = -2;    /* flag as existing */
+                               break;
+                       }
+                       if (unused == -1 && lpe->p_size == 0 &&
+                           lpe->p_fstype == FS_UNUSED)
+                               unused = j;
+               }
+               if (unused == -2)
+                       continue;       /* entry exists, skip... */
+               if (unused == -1) {
+                       if (label.d_npartitions < MAXPARTITIONS) {
+                               unused = label.d_npartitions;
+                               label.d_npartitions++;
+                       } else {
+                               printf(
+                               "  WARNING: no slots free for %s partition.\n",
+                                   fstypenames[npe.p_fstype]);
+                               continue;
+                       }
+               }
+
+               if (verbose)
+                       printf("  adding %s partition to slot %c.\n",
+                           fstypenames[npe.p_fstype], unused + 'a');
+               switch (npe.p_fstype) {
+               case FS_BSDFFS:
+                       npe.p_size = 16384;     /* XXX */
+                       npe.p_fsize = 1024;
+                       npe.p_frag = 8;
+                       npe.p_cpg = 16;
                        break;
-               default:
-                       label.d_partitions[np].p_size = getlong(&parts[i].mbrp_size);
-                       label.d_partitions[np].p_offset = getlong(&parts[i].mbrp_start) + off;
-                       label.d_partitions[np].p_fstype = nbsdtype(parts[i].mbrp_typ);
-                       switch (label.d_partitions[np].p_fstype) {
-                       case FS_BSDFFS:
-                               label.d_partitions[np].p_size = 16384;
-                               label.d_partitions[np].p_fsize = 1024;
-                               label.d_partitions[np].p_frag = 8;
-                               label.d_partitions[np].p_cpg = 16;
-                               break;
 #ifdef __does_not_happen__
-                       case FS_BSDLFS:
-                               label.d_partitions[np].p_size = 16384;
-                               label.d_partitions[np].p_fsize = 1024;
-                               label.d_partitions[np].p_frag = 8;
-                               label.d_partitions[np].p_sgs = XXX;
-                               break;
+               case FS_BSDLFS:
+                       npe.p_size = 16384;     /* XXX */
+                       npe.p_fsize = 1024;
+                       npe.p_frag = 8;
+                       npe.p_sgs = XXX;
+                       break;
 #endif
-                       }
-                       np++;
-                       break;
                }
-               if (np >MAXPARTITIONS)
-                       return np;
-               if (np == RAW_PART)
-                       np++;
+               changed++;
+               label.d_partitions[unused] = npe;
        }
+
+                               /* recursively scan extended partitions */
        for (i = 0; i < NMBRPART; i++) {
                u_int32_t poff;
 
-               switch (parts[i].mbrp_typ) {
-               case MBR_PTYPE_EXT:



Home | Main Index | Thread Index | Old Index