]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - fdisk/fdisksunlabel.c
Imported from util-linux-2.9i tarball.
[thirdparty/util-linux.git] / fdisk / fdisksunlabel.c
similarity index 73%
rename from disk-utils/fdisksunlabel.c
rename to fdisk/fdisksunlabel.c
index a5a87b8d45e33533b4a7c8fda8f7048b71b07084..70837737919b006ae594a3fa45e5ea2b2f831523 100644 (file)
@@ -6,7 +6,6 @@
  *
  * Merged with fdisk for other architectures, aeb, June 1998.
  */
-#if defined (sparc)
 
 #include <stdio.h>             /* stderr */
 #include <stdlib.h>            /* uint */
 #include <sys/stat.h>          /* stat */
 
 #include <endian.h>
-#include <scsi/scsi.h>
+#if 1
+#include <scsi/scsi.h>         /* SCSI_IOCTL_GET_IDLUN */
+#endif
 #include <linux/major.h>       /* FLOPPY_MAJOR */
 #include <linux/hdreg.h>       /* HDIO_GETGEO */
 
 #include "fdisk.h"
 #include "fdisksunlabel.h"
 
-int     other_endian = 0;
-int     scsi_disk = 0;
-int     floppy = 0;
+static int     other_endian = 0;
+static int     scsi_disk = 0;
+static int     floppy = 0;
 
 #define LINUX_SWAP      0x82
 #define LINUX_NATIVE    0x83
@@ -45,10 +46,10 @@ struct systypes sun_sys_types[] = {
        { 0, NULL }
 };
 
-inline unsigned short __swap16(unsigned short x) {
+static inline unsigned short __swap16(unsigned short x) {
         return (((__u16)(x) & 0xFF) << 8) | (((__u16)(x) & 0xFF00) >> 8);
 }
-inline __u32 __swap32(__u32 x) {
+static inline __u32 __swap32(__u32 x) {
         return (((__u32)(x) & 0xFF) << 24) | (((__u32)(x) & 0xFF00) << 8) | (((__u32)(x) & 0xFF0000) >> 8) | (((__u32)(x) & 0xFF000000) >> 24);
 }
 
@@ -88,12 +89,20 @@ void set_sun_partition(int i, uint start, uint stop, int sysid)
        changed[i] = 1;
 }
 
+void sun_nolabel(void)
+{
+       sun_label = 0;
+       sunlabel->magic = 0;
+       partitions = 4;
+}
+
 int check_sun_label(void)
 {
        unsigned short *ush;
        int csum;
        
-       if (sunlabel->magic != SUN_LABEL_MAGIC && sunlabel->magic != SUN_LABEL_MAGIC_SWAPPED) {
+       if (sunlabel->magic != SUN_LABEL_MAGIC &&
+           sunlabel->magic != SUN_LABEL_MAGIC_SWAPPED) {
                sun_label = 0;
                other_endian = 0;
                return 0;
@@ -145,6 +154,74 @@ struct sun_predefined_drives {
 {"IOMEGA","Jaz",0,1019,2,1021,64,32,5394},
 };
 
+struct sun_predefined_drives *
+sun_autoconfigure_scsi() {
+    struct sun_predefined_drives *p = NULL;
+
+#ifdef SCSI_IOCTL_GET_IDLUN
+    unsigned int id[2];
+    char buffer[2048];
+    char buffer2[2048];
+    FILE *pfd;
+    char *vendor;
+    char *model;
+    char *q;
+    int i;
+
+    if (!ioctl(fd, SCSI_IOCTL_GET_IDLUN, &id)) {
+        sprintf(buffer,
+            "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n",
+#if 0                    
+            ((id[0]>>24)&0xff)-/*PROC_SCSI_SCSI+PROC_SCSI_FILE*/33,
+#else
+            /* This is very wrong (works only if you have one HBA),
+               but I haven't found a way how to get hostno
+               from the current kernel */
+            0,
+#endif                        
+            (id[0]>>16)&0xff,
+            id[0]&0xff,
+            (id[0]>>8)&0xff);
+        pfd = fopen("/proc/scsi/scsi","r");
+        if (pfd) {
+            while (fgets(buffer2,2048,pfd)) {
+                            if (!strcmp(buffer, buffer2)) {
+                    if (fgets(buffer2,2048,pfd)) {
+                        q = strstr(buffer2,"Vendor: ");
+                        if (q) {
+                            q += 8;
+                            vendor = q;
+                            q = strstr(q," Model: ");
+                            if (q) {
+                                *q = 0;
+                                q += 8;
+                                model = q;
+                                q = strstr(q," Rev: ");
+                                if (q) {
+                                    *q = 0;
+                                    for (i = 0; i < SIZE(sun_drives); i++) {
+                                        if (*sun_drives[i].vendor && strcasecmp(sun_drives[i].vendor, vendor))
+                                            continue;
+                                        if (!strstr(model, sun_drives[i].model))
+                                            continue;
+                                        printf("Autoconfigure found a %s%s%s\n",sun_drives[i].vendor,(*sun_drives[i].vendor) ? " " : "",sun_drives[i].model);
+                                        p = sun_drives + i;
+                                        break;
+                                    }
+                                }
+                            }
+                        }
+                    }
+                    break;
+                }
+            }
+            fclose(pfd);
+        }
+    }
+#endif
+    return p;
+}
+
 void create_sunlabel(void)
 {
        struct hd_geometry geometry;
@@ -181,64 +258,10 @@ void create_sunlabel(void)
                } else if (c >= 'A' && c < 'A' + SIZE(sun_drives)) {
                    p = sun_drives + c - 'A';
                    break;
-               } else if (c == '0')
+               } else if (c == '0') {
                    break;
-               else if (c == '?' && scsi_disk) {
-                   unsigned int id[2];
-                   char buffer[2048];
-                   char buffer2[2048];
-                   FILE *pfd;
-                   char *vendor;
-                   char *model;
-                   char *q;
-                   
-                   if (!ioctl(fd, SCSI_IOCTL_GET_IDLUN, &id)) {
-                       sprintf(buffer, "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n",
-#if 0                  
-                           ((id[0]>>24)&0xff)-/*PROC_SCSI_SCSI+PROC_SCSI_FILE*/33,
-#else
-                       /* This is very wrong (works only if you have one HBA), but I haven't find a way how to get hostno from the current kernel */
-                           0,
-#endif                     
-                           (id[0]>>16)&0xff,
-                           id[0]&0xff,
-                           (id[0]>>8)&0xff);
-                       pfd = fopen("/proc/scsi/scsi","r");
-                       if (pfd) {
-                           while (fgets(buffer2,2048,pfd)) {
-                               if (!strcmp(buffer, buffer2)) {
-                                   if (fgets(buffer2,2048,pfd)) {
-                                       q = strstr(buffer2,"Vendor: ");
-                                       if (q) {
-                                           q += 8;
-                                           vendor = q;
-                                           q = strstr(q," Model: ");
-                                           if (q) {
-                                               *q = 0;
-                                               q += 8;
-                                               model = q;
-                                               q = strstr(q," Rev: ");
-                                               if (q) {
-                                                   *q = 0;
-                                                   for (i = 0; i < SIZE(sun_drives); i++) {
-                                                       if (*sun_drives[i].vendor && strcasecmp(sun_drives[i].vendor, vendor))
-                                                           continue;
-                                                       if (!strstr(model, sun_drives[i].model))
-                                                           continue;
-                                                       printf("Autoconfigure found a %s%s%s\n",sun_drives[i].vendor,(*sun_drives[i].vendor) ? " " : "",sun_drives[i].model);
-                                                       p = sun_drives + i;
-                                                       break;
-                                                   }
-                                               }
-                                           }
-                                       }
-                                   }
-                                   break;
-                               }
-                           }
-                           fclose(pfd);
-                       }
-                   }
+               } else if (c == '?' && scsi_disk) {
+                   p = sun_autoconfigure_scsi();
                    if (!p)
                        printf("Autoconfigure failed.\n");
                    else
@@ -267,23 +290,26 @@ void create_sunlabel(void)
                sunlabel->ilfact = SSWAP16(1);
                sunlabel->sparecyl = 0;
            } else {
-               if (heads)
-                   heads = read_int(1,heads,1024,deflt,"Heads");
-               else
-                   heads = read_int(1,1,1024,ignore,"Heads");
-               if (sectors)
-                   sectors = read_int(1,sectors,1024,deflt,"Sectors/track");
-               else
-                   sectors = read_int(1,1,1024,ignore,"Sectors/track");
+               heads = read_int(1,heads,1024,0,"Heads");
+               sectors = read_int(1,sectors,1024,0,"Sectors/track");
                if (cylinders)
-                   cylinders = read_int(1,cylinders-2,65535,deflt,"Cylinders");
+                   cylinders = read_int(1,cylinders-2,65535,0,"Cylinders");
                else
-                   cylinders = read_int(1,1,65535,ignore,"Cylinders");
-               sunlabel->nacyl = SSWAP16(read_int(0,2,65535,deflt,"Alternate cylinders"));
-               sunlabel->pcylcount = SSWAP16(read_int(0,cylinders + SSWAP16(sunlabel->nacyl),65535,deflt,"Physical cylinders"));
-               sunlabel->rspeed = SSWAP16(read_int(1,5400,100000,deflt,"Rotation speed (rpm)"));
-               sunlabel->ilfact = SSWAP16(read_int(1,1,32,deflt,"Interleave factor"));
-               sunlabel->sparecyl = SSWAP16(read_int(0,0,sectors,deflt,"Extra sectors per cylinder"));
+                   cylinders = read_int(1,0,65535,0,"Cylinders");
+               sunlabel->nacyl =
+                       SSWAP16(read_int(0,2,65535,0,
+                                        "Alternate cylinders"));
+               sunlabel->pcylcount =
+                       SSWAP16(read_int(0,cylinders+SSWAP16(sunlabel->nacyl),
+                                        65535,0,"Physical cylinders"));
+               sunlabel->rspeed =
+                       SSWAP16(read_int(1,5400,100000,0,
+                                        "Rotation speed (rpm)"));
+               sunlabel->ilfact =
+                       SSWAP16(read_int(1,1,32,0,"Interleave factor"));
+               sunlabel->sparecyl =
+                       SSWAP16(read_int(0,0,sectors,0,
+                                        "Extra sectors per cylinder"));
            }
        } else {
            sunlabel->sparecyl = SSWAP16(p->sparecyl);
@@ -459,18 +485,22 @@ void add_sun_partition(int n, int sys)
        sprintf(mesg, "First %s", str_units());
        for (;;) {
                if (whole_disk)
-                       first = read_int(0, 0, 0, deflt, mesg);
+                       first = read_int(0, 0, 0, 0, mesg);
                else
-                       first = read_int(scround(start), scround(start), scround(stop),
-                                        ignore, mesg);
+                       first = read_int(scround(start), scround(stop)+1,
+                                        scround(stop), 0, mesg);
                if (unit_flag)
                        first *= display_factor;
                else
-                       first = (first + heads * sectors - 1) / (heads * sectors); /* Starting sector has to be properly aligned */
-               if (n == 2 && first != 0) printf ("It is highly recommended that third partition covers the whole disk\n"
-                                                 "and is of type `Whole disk'\n");
+                       /* Starting sector has to be properly aligned */
+                       first = (first + heads * sectors - 1) / (heads * sectors);
+               if (n == 2 && first != 0)
+                       printf ("\
+It is highly recommended that the third partition covers the whole disk\n\
+and is of type `Whole disk'\n");
                for (i = 0; i < partitions; i++)
-                       if (lens[i] && starts[i] <= first && starts[i] + lens[i] > first)
+                       if (lens[i] && starts[i] <= first
+                                   && starts[i] + lens[i] > first)
                                break;
                if (i < partitions && !whole_disk) {
                        if (n == 2 && !first) {
@@ -489,14 +519,14 @@ void add_sun_partition(int n, int sys)
        }
        sprintf(mesg, "Last %s or +size or +sizeM or +sizeK", str_units());
        if (whole_disk)
-               last = read_int(scround(stop2), scround(stop2), scround(stop2), 
-                               deflt, mesg);
+               last = read_int(scround(stop2), scround(stop2), scround(stop2),
+                               0, mesg);
        else if (n == 2 && !first)
                last = read_int(scround(first), scround(stop2), scround(stop2),
-                               upper, mesg);
+                               scround(first), mesg);
        else
                last = read_int(scround(first), scround(stop), scround(stop),
-                               lower, mesg);
+                               scround(first), mesg);
        if (unit_flag)
                last *= display_factor;
        if (n == 2 && !first) {
@@ -520,10 +550,13 @@ void
 sun_delete_partition(int i) {
        if (i == 2 && sunlabel->infos[i].id == WHOLE_DISK && 
            !sunlabel->partitions[i].start_cylinder && 
-           SSWAP32(sunlabel->partitions[i].num_sectors) == heads * sectors * cylinders)
-               printf("If you want to maintain SunOS/Solaris compatibility, consider leaving this\n"
-                      "partition as Whole disk (5), starting at 0, with %d sectors\n",
-                      SSWAP32(sunlabel->partitions[i].num_sectors));
+           SSWAP32(sunlabel->partitions[i].num_sectors)
+             == heads * sectors * cylinders)
+               printf("If you want to maintain SunOS/Solaris compatibility, "
+                      "consider leaving this\n"
+                      "partition as Whole disk (5), starting at 0, with %u "
+                      "sectors\n",
+                      (uint) SSWAP32(sunlabel->partitions[i].num_sectors));
        sunlabel->infos[i].id = 0;
        sunlabel->partitions[i].num_sectors = 0;
 }
@@ -580,23 +613,20 @@ sun_list_table(int xtra) {
                       disk_device, heads, sectors, cylinders,
                       str_units(), display_factor);
 
-       printf("%*s Flag %s  Start      End   Blocks   Id  System\n",
-              w + 1, "Device", show_begin ? "  Begin  " : " ");
+       printf("%*s Flag     Start       End    Blocks   Id  System\n",
+              w + 1, "Device");
        for (i = 0 ; i < partitions; i++) {
                if (sunlabel->partitions[i].num_sectors) {
                        __u32 start = SSWAP32(sunlabel->partitions[i].start_cylinder) * heads * sectors;
                        __u32 len = SSWAP32(sunlabel->partitions[i].num_sectors);
                        printf(
-                         show_begin
-                           ? "%*s%-2d %c%c%9d%9d%9d%9d%c  %2x  %s\n"
-                           : "%*s%-2d %c%c%c%9d%9d%9d%c  %2x  %s\n",
+                           "%*s%-2d %c%c %9ld %9ld %9ld%c  %2x  %s\n",
 /* device */             w, disk_device, i + 1,
 /* flags */              (sunlabel->infos[i].flags & 0x01) ? 'u' : ' ',
                          (sunlabel->infos[i].flags & 0x10) ? 'r' : ' ',
-/* begin */              show_begin ? scround(start) : ' ',
-/* start */              scround(start),
-/* end */                scround(start+len),
-/* odd flag on end */    len / 2, len & 1 ? '+' : ' ',
+/* start */              (long) scround(start),
+/* end */                (long) scround(start+len),
+/* odd flag on end */    (long) len / 2, len & 1 ? '+' : ' ',
 /* type id */            sunlabel->infos[i].id,
 /* type name */                  (type = partition_type(sunlabel->infos[i].id))
                                ? type : "Unknown");
@@ -606,9 +636,9 @@ sun_list_table(int xtra) {
 
 void
 sun_set_alt_cyl(void) {
-       sunlabel->nacyl = SSWAP16(read_int(0,SSWAP16(sunlabel->nacyl),
-                                          65535, deflt,
-                                          "Number of alternate cylinders"));
+       sunlabel->nacyl =
+               SSWAP16(read_int(0,SSWAP16(sunlabel->nacyl), 65535, 0,
+                                "Number of alternate cylinders"));
 }
 
 void
@@ -618,30 +648,30 @@ sun_set_ncyl(int cyl) {
 
 void
 sun_set_xcyl(void) {
-       sunlabel->sparecyl = SSWAP16(read_int(0, SSWAP16(sunlabel->sparecyl),
-                                             sectors, deflt,
-                                             "Extra sectors per cylinder"));
+       sunlabel->sparecyl =
+               SSWAP16(read_int(0, SSWAP16(sunlabel->sparecyl), sectors, 0,
+                                "Extra sectors per cylinder"));
 }
 
 void
 sun_set_ilfact(void) {
-       sunlabel->ilfact = SSWAP16(read_int(1, SSWAP16(sunlabel->ilfact),
-                                           32, deflt,
-                                           "Interleave factor"));
+       sunlabel->ilfact =
+               SSWAP16(read_int(1, SSWAP16(sunlabel->ilfact), 32, 0,
+                                "Interleave factor"));
 }
 
 void
 sun_set_rspeed(void) {
-       sunlabel->rspeed = SSWAP16(read_int(1, SSWAP16(sunlabel->rspeed),
-                                           100000, deflt,
-                                           "Rotation speed (rpm)"));
+       sunlabel->rspeed =
+               SSWAP16(read_int(1, SSWAP16(sunlabel->rspeed), 100000, 0,
+                                "Rotation speed (rpm)"));
 }
 
 void
 sun_set_pcylcount(void) {
-       sunlabel->pcylcount=SSWAP16(read_int(0, SSWAP16(sunlabel->pcylcount),
-                                            65535, deflt,
-                                            "Number of physical cylinders"));
+       sunlabel->pcylcount =
+               SSWAP16(read_int(0, SSWAP16(sunlabel->pcylcount), 65535, 0,
+                                "Number of physical cylinders"));
 }
 
 void
@@ -658,4 +688,3 @@ sun_write_table(void) {
                fatal(unable_to_write);
 }
 
-#endif /* sparc */