home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Collection - Online Library - January 1996 / CKITOS2196.ISO / diskette / gg244043.dsk / unc.dsk / LS30UTIL / SWITCHPS.C < prev    next >
Text File  |  1993-02-18  |  6KB  |  213 lines

  1. /*
  2.  * IBM INTERNAL USE ONLY
  3.  *
  4.  * Program to switch a partition between primary and secondary.
  5.  *  switch [p|s] sector(in hex)
  6.  */
  7.  /**********************************************************************/
  8.  #define   INCL_DOSPROCESS
  9.  #define   INCL_DOSDEVICES
  10.  #define   INCL_DOSFILEMGR
  11.  #include  <os2.h>
  12.  #include  <stdio.h>
  13.  #include  <stdlib.h>
  14.  #include  <memory.h>
  15.  #include  <string.h>
  16.  /**********************************************************************/
  17.  /* DECLARATIONS */
  18.  #define  STDOUT               1       // Screen's handle
  19.  #define  SECTOR_SIZE        512
  20.  #define  MAXSECTORS          64
  21.  #define  MIRROR            0x87
  22.  #define  HPFS              0x07
  23.  #define  SIG1              0x55
  24.  #define  SIG2              0xAA
  25.  
  26.  /* DosPhysicalDisk() stuff */
  27.  #define  GET_DISK_HANDLE        2
  28.  #define  REL_DISK_HANDLE        3
  29.  
  30.  /* Category 9 IOCTLs */
  31.  #define  CATEGORY_9             0x09    // Category #
  32.  #define  LOCK_PHYS_DISK         0x00
  33.  #define  UNLOCK_PHYS_DISK       0x01
  34.  #define  GET_DEV_PARMS          0x63    // Function #
  35.  #define  READ_SECTOR            0x64    // Function #
  36.  #define  WRITE_SECTOR           0x44    // Function #
  37.  
  38.  /* sector layout table */
  39.  struct ttlayout {
  40.      USHORT sector_nbr;
  41.      USHORT sector_size;
  42.  };
  43.  typedef struct _rs {
  44.      BYTE   Cmd_Info;
  45.      USHORT Head;
  46.      USHORT Cylinder;
  47.      USHORT First_Sector;
  48.      USHORT Number_Of_Sectors;
  49.      struct ttlayout table[MAXSECTORS];
  50.  } READPARMS;
  51.  
  52.  /**********************************************************************/
  53.  /* GLOBAL VARIABLES */
  54.  
  55.  
  56. //*******************************************************************
  57. // 1. Validate input parameters
  58. // 2. Initialize I/O structure
  59. // 3. Get disk handle (open drive)
  60. // 4. Read master boot record
  61. // 5. Change partition type
  62. // 6. Write master boot record
  63. // 7. Close drive
  64. //
  65.  
  66. main(argc, argv, envp)
  67.    int argc;
  68.    char *argv[];
  69.    char *envp[];
  70. {
  71.     USHORT rc;
  72.     ULONG  ndx;
  73.     BYTE   Dname[4];
  74.     USHORT save;
  75.     READPARMS rp;                       // cmd packet for IOCtl
  76.     BOOL     primary;
  77.     BOOL     secondary;
  78.     UCHAR    Buffer[512];
  79.     USHORT   Disk;                // Disk number
  80.     ULONG    Cylinder;            // Drive starting cylinder
  81.     USHORT   Handle;              // open disk's handle
  82.  
  83.     printf("SWITCH Primary/Secondary Version 1.0 \n");
  84.     printf("Program copyright International Business Machines Corp. 1992\n");
  85.     printf("IBM Internal Use Only\n\n");
  86.  
  87.  
  88.     /* validate input arguments */
  89.     if ( argc < 3)
  90.       {
  91.       puts("Switch primary/secondary partition indicator.\n\
  92.               Syntax: switchps [p|s] disk cylinder \n\
  93.               p . . . . Primary to secondary \n\
  94.               s . . . . Secondary to primary \n\
  95.               disk. . . Disk # (1 ... 24 )\n\
  96.               cylinder. Cylinder # (Decimal, zero-based)\n\
  97.             Example:  switchps p 1 0\n");
  98.       DosExit(EXIT_PROCESS, 0);
  99.       }
  100.  
  101.     /* Validate command-line arguments */
  102.     primary = FALSE;
  103.     if ((argv[1][0] == 'P') || argv[1][0] == 'p')
  104.       primary = TRUE;
  105.  
  106.     secondary = FALSE;
  107.     if ((argv[1][0] == 'S') || argv[1][0] == 's')
  108.       secondary = TRUE;
  109.  
  110.     if (primary == FALSE && secondary == FALSE)
  111.       {
  112.       printf("P/S parameter not specified\n");
  113.       DosExit(EXIT_PROCESS, 0);
  114.       }
  115.  
  116.     Disk = atoi(&argv[2][0]);
  117.     Cylinder = atoi(&argv[3][0]);
  118.  
  119.  
  120.     /* Init the track layout table w/in the cmd packet */
  121.     for (ndx = 0; ndx < MAXSECTORS; ndx++)
  122.       {
  123.       rp.table[ndx].sector_nbr  = ndx+1;
  124.       rp.table[ndx].sector_size = SECTOR_SIZE;
  125.       }
  126.     rp.Cmd_Info          = 1;
  127.     rp.Cylinder          = Cylinder;
  128.     rp.Head              = 0;
  129.     rp.First_Sector      = 0;
  130.     rp.Number_Of_Sectors = 1;
  131.  
  132.  
  133.     /* Open the disk */
  134.     Dname[0] = Disk + '0';      // Disk is 1 based - make it a character
  135.     Dname[1] = ':';
  136.     Dname[2] = '\0';
  137.     if (rc = DosPhysicalDisk( (USHORT) GET_DISK_HANDLE, (PBYTE)  &Handle,
  138.                 (USHORT) sizeof(Handle), (PBYTE)  Dname, (USHORT) 3 ))
  139.       {
  140.       printf("Could not open physical disk #%hu\n",Disk);
  141.       goto eext;
  142.       }
  143.  
  144.     /* read the MBR */
  145.     if (rc=DosDevIOCtl( (PVOID ) Buffer, (PCHAR ) &rp,
  146.                       (USHORT) READ_SECTOR, (USHORT) CATEGORY_9,
  147.                       (HFILE ) Handle ))
  148.       {
  149.       printf("ReadSector: Failure on disk# %hu, OS/2 error=%hu\n", Disk, rc);
  150.       goto shutd;
  151.       }
  152.  
  153.     /* try to change the data */
  154.     if (Buffer[510] != SIG1)
  155.       {
  156.       printf("Disk location does not contain correct data\n");
  157.       goto shutd;
  158.       }
  159.     if (Buffer[511] != SIG2)
  160.       {
  161.       printf("Disk location does not contain correct data\n");
  162.       goto shutd;
  163.       }
  164.  
  165.     if (primary)
  166.       {
  167.       if (Buffer[450] == HPFS)
  168.         Buffer[450] = MIRROR;
  169.       else if (Buffer[466] == HPFS)
  170.         Buffer[466] = MIRROR;
  171.       else
  172.         {
  173.         printf("Disk location does not contain a primary partition\n");
  174.         goto shutd;
  175.         }
  176.       }
  177.     else
  178.       {
  179.       if (Buffer[450] == MIRROR)
  180.         Buffer[450] = HPFS;
  181.       else if (Buffer[466] == MIRROR)
  182.         Buffer[466] = HPFS;
  183.       else
  184.         {
  185.         printf("Disk location does not contain a secondary partition\n");
  186.         goto shutd;
  187.         }
  188.       }
  189.  
  190.  
  191.     /* write the MBR */
  192.     if (rc=DosDevIOCtl( (PVOID ) Buffer, (PCHAR ) &rp,
  193.                       (USHORT) WRITE_SECTOR, (USHORT) CATEGORY_9,
  194.                       (HFILE ) Handle ))
  195.       printf("WriteSector: Failure on disk# %hu, OS/2 error=%hu\n", Disk, rc);
  196.     else
  197.       {
  198.       if (primary)
  199.         printf("Primary partition changed to secondary.\n");
  200.       else
  201.         printf("Secondary partition changed to primary.\n");
  202.       }
  203.  
  204. shutd:
  205.     if (DosPhysicalDisk( (USHORT) REL_DISK_HANDLE, (PBYTE)  0,
  206.                           (USHORT) 0, (PBYTE) &Handle,
  207.                           (USHORT) sizeof(USHORT) ))
  208.       printf("ClosePhysDisk: Failure on disk# %hu, OS/2 error=%hu\n", Disk, rc);
  209.  
  210. eext:
  211.     DosExit(EXIT_PROCESS, 0);
  212. }
  213.