home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / dmode_25.lhz / dmode.c next >
C/C++ Source or Header  |  2005-10-13  |  12KB  |  552 lines

  1. /* DMODE.C    -    Change RBF descriptor values.
  2.  
  3.                 Version history:
  4.                 
  5.                 1.0        Initial Release            Jun 1988
  6.                         Ron D Voights            
  7.                         2024 Baldwin Court
  8.                         Glendale Heights
  9.                         IL 60139
  10.                         USA
  11.                         
  12.                 1.1        Added help display        Aug 1988
  13.                         RDV
  14.                 
  15.                 2.0        Converted to OS9/68K    Feb 1991
  16.                         Martin Gregorie            
  17.                         10 Sadlers Mead
  18.                         Harlow, Essex
  19.                         UK.
  20.                 2.1        Copyright notice added    Aug 1992
  21.                 2.2        Upgraded for OSK 2.4    Nov 1994
  22.                 2.3        hex can be $ff or 0xff    Oct 1995
  23.                 2.4        added detach/attach        Oct 1995
  24.                         the deiniz;dmode;iniz
  25.                         seq. is not needed
  26.                 2.5        Removed dependency on   Oct 2005
  27.                         environment library.
  28. */
  29.  
  30. #define    VERSION        "2.5"
  31. #define COPYRIGHT     "(c) Ron D.Voights, M.C.Gregorie, 2005"
  32.  
  33. #include <modes.h>
  34. #include <stdio.h>
  35. #include <strings.h>
  36. #include "rbfparams.h"
  37.  
  38. extern int errno;
  39.  
  40. #define CMNDCNT 21
  41. #define TRUE    1
  42. #define FALSE    0
  43.  
  44. static char *p[] = 
  45.     {
  46.         "drv", "stp", "typ", 
  47.         "dns", "cyl", "sid", 
  48.         "vfy", "sct", "tos", 
  49.         "sas", "ilv", "tof",
  50.         "sof", "siz", "ctl",
  51.         "try", "prk", "lsn",
  52.         "tot", "rts", "xfr"
  53.     };
  54.  
  55. static char *desc[] =
  56.     {    
  57.         "Drive number",       "Step rate",           "Device type",
  58.         "Media density",      "Number of cylinders", "Number of sides",
  59.         "Verify disk writes", "Sectors per track",   "Sectors on track 0",
  60.         "Segment allocation", "Interleave factor",   "Track base offset",
  61.         "Sector base offset", "Sector size (bytes)", "Control word",
  62.         "Retry count",        "Hard disk park cyl",  "Logical sector offset",
  63.         "Total cylinders",      "Rpm, xfr rate",       "Maximum transfer"
  64.     };
  65.  
  66. static int hex[] =
  67.     {
  68.         FALSE, FALSE, TRUE,
  69.         TRUE,  FALSE, FALSE,
  70.         TRUE , FALSE, FALSE,
  71.         FALSE, FALSE, FALSE,
  72.         FALSE, FALSE, TRUE,
  73.         FALSE, FALSE, FALSE,
  74.         FALSE, TRUE,  FALSE
  75.     };
  76.  
  77. rbf_dev     *modlink();
  78. rbf_dev    *munlink();
  79.  
  80. void        help();
  81. void        display_descriptor();
  82. void        update_descriptor();
  83. void        error();
  84.  
  85. main(argc,argv)
  86. int     argc;
  87. char     **argv;
  88.  
  89. {
  90.  
  91.     int     i;        
  92.  
  93.     /*
  94.         Start by checking for help requests
  95.     */
  96.     if (argc < 2)
  97.         help();
  98.         
  99.     for (i = 1; i < argc; i++)
  100.         if (strcmp(argv[i],"-?") == 0)
  101.             help();
  102.     
  103.     /*
  104.         If just the device descriptor name is given,
  105.         show current device parameters and exit.
  106.         
  107.         If more parameters are given attempt to modify the 
  108.         descriptor.
  109.     */
  110.     if (argc == 2)
  111.         display_descriptor(argv[0],argv[1]);
  112.     else
  113.         update_descriptor(argc,argv);
  114.     
  115.     return(0);
  116. }
  117.  
  118. /*
  119.     Display explanation of program usage
  120. */
  121. void help()
  122. {
  123.     int    i;
  124.     int    nlr;
  125.     char hexreqd[6];
  126.     
  127.     printf("\ndmode v%s %s\n\n",VERSION,COPYRIGHT);
  128.     printf("Syntax    : dmode <RBF descriptor> [<RBF parameter list>]\n");
  129.     printf("Function  : Change RBF device parameters in a descriptor\n");
  130.     printf("            The device is detached, changed, and re-attached\n");
  131.     printf("            If it isn't in use this should re-initialise it.\n");
  132.     printf("Parameters:\n");
  133.     
  134.     for(i = 0; i < CMNDCNT; i++)
  135.     {
  136.         if (hex[i])
  137.             strcpy(hexreqd,"(hex)");
  138.         else
  139.             strcpy(hexreqd,"");
  140.         printf("     %-3s%-5s = %-22s",p[i],hexreqd,desc[i]);
  141.         if (i%2 == 0)
  142.             nlr = TRUE;
  143.         else
  144.         {
  145.             printf("\n");
  146.             nlr = FALSE;
  147.         }
  148.     }
  149.     if (nlr)
  150.         printf("\n");
  151.     exit(0);
  152. }
  153.  
  154. /*
  155.     Show the current state of an RBF descriptor
  156. */
  157. void display_descriptor(pn,dev)
  158. char *pn;
  159. char *dev;
  160. {
  161.     mod_dev          *mod;
  162.     struct modhcom    *hddr;
  163.     rbf_dev            *it;
  164.     char            *devname;
  165.     int                i;
  166.     int                x;
  167.     int                floppy_disk;
  168.     
  169.     if (dev[0] != '/')
  170.         error(pn,dev,"is not a valid device name");
  171.  
  172.     mod = (mod_dev*)modlink(dev+1,mktypelang(15,0));
  173.     if (mod == (mod_dev*)-1)
  174.         exit(errno);
  175.  
  176.     if ((int)mod->_mdtype != 1)
  177.         error(pn,dev,"is not an RBF device");
  178.         
  179.     hddr = (struct modhcom*)mod;
  180.     devname = (char*) hddr + hddr->_mname;
  181.     it = (rbf_dev*) ( (char*)mod + sizeof(mod_dev) -2 );
  182.     
  183.     printf("Parameters for                      /%s\n",devname);
  184.  
  185.     for (i = 0; i < CMNDCNT; i++)
  186.     {
  187.         printf("%-22s (%s) =",desc[i],p[i]);
  188.         switch (i)
  189.         {
  190.             case 0: printf("   %4d \n",(int)(it->pd_drv)); break;
  191.             case 1: printf("   %4d \n",(int)(it->pd_stp)); break;
  192.             case 2: printf("   0x%02x ",(int)(it->pd_typ)); 
  193.                     floppy_disk = FALSE;
  194.                     if (it->pd_typ == 0 && it->pd_dns == 0)
  195.                     {
  196.                         printf("\n");
  197.                         break;
  198.                     }
  199.                     if (it->pd_typ & TYP_HARD)
  200.                     {
  201.                         printf("Hard disk\n");
  202.                         break;
  203.                     }
  204.                     if (it->pd_typ & TYP_HREMOV)
  205.                     {
  206.                         printf("Removable hard disk\n");
  207.                         break;
  208.                     }
  209.                     floppy_disk = TRUE;
  210.                     x = (it->pd_typ >> 1) & 0xf;
  211.                     if (x == SIZE_OLD)
  212.                     {
  213.                         if (it->pd_typ & TYP_EIGHT)
  214.                             printf("8\" ");
  215.                         else
  216.                             printf("5 1/4\" ");
  217.                     }
  218.                     else
  219.                     {
  220.                         if (x == TYP_SIZE8)
  221.                             printf("8\" ");
  222.                         if (x == TYP_SIZE5)
  223.                             printf("5 1/4\" ");
  224.                         if (x == TYP_SIZE3)
  225.                             printf("3 1/2\" ");
  226.                     }
  227.                     printf("floppy ");
  228.                     if (it->pd_typ & TYP_DDTRK0)
  229.                         printf("DD ");
  230.                     else
  231.                         printf("SD ");
  232.                     printf("track 0");
  233.                     printf("\n");
  234.                     break;
  235.             case 3: printf("   0x%02x ",(int)(it->pd_dns));
  236.                     if (!floppy_disk)
  237.                     {
  238.                         printf("\n");
  239.                         break;
  240.                     }
  241.                     if (it->pd_dns & DNS_DD)
  242.                         printf("DD ");
  243.                     else
  244.                         printf("SD ");
  245.                     if (it->pd_dns & DNS_DT)
  246.                     {
  247.                         x = (it->pd_typ >> 1) & 0xf;
  248.                         if (x == TYP_SIZE3)
  249.                             printf("135tpi ");
  250.                         else
  251.                             printf("96tpi ");
  252.                     }
  253.                     else
  254.                     if (it->pd_dns & DNS_QUAD)
  255.                         printf("192tpi ");
  256.                     else
  257.                     if (it->pd_dns & DNS_OCTAL)
  258.                         printf("384tpi ");
  259.                     else
  260.                         printf("48tpi ");
  261.                     printf("\n");
  262.                     break;
  263.             case 4: printf("   %4d \n",(int)(it->pd_cyl)); break;
  264.             case 5: printf("   %4d \n",(int)(it->pd_sid)); break;
  265.             case 6: printf("   0x%02x Verify ",(int)(it->pd_vfy)); 
  266.                     if (it->pd_vfy)
  267.                         printf("off");
  268.                     else
  269.                         printf("on");
  270.                     printf("\n");
  271.                     break;
  272.             case 7: printf("   %4d \n",(int)(it->pd_sct)); break;
  273.             case 8: printf("   %4d \n",(int)(it->pd_t0s)); break;
  274.             case 9: printf("   %4d \n",(int)(it->pd_sas)); break;
  275.             case 10: printf("   %4d \n",(int)(it->pd_ilv)); break;
  276.             case 11: printf("   %4d \n",(int)(it->pd_toffs)); break;
  277.             case 12: printf("   %4d \n",(int)(it->pd_soffs)); break;
  278.             case 13: printf("   %4d \n",(int)(it->pd_ssize)); break;
  279.             case 14: printf(" 0x%04x ",(int)(it->pd_cntl));
  280.                     if (it->pd_cntl & CNTL_NOFMT)
  281.                         printf ("Nofmt ");
  282.                     if (it->pd_cntl & CNTL_MULTSECT)
  283.                         printf ("M/secI/O ");
  284.                     if (it->pd_cntl & CNTL_STABID)
  285.                         printf ("Stabid ");
  286.                     if (it->pd_cntl & CNTL_AUTOSIZE)
  287.                         printf ("Autosize ");
  288.                     if (it->pd_cntl & CNTL_FMTTRK)
  289.                         printf ("Trkfmt ");
  290.                     printf("\n");
  291.                     break;
  292.             case 15: printf("   %4d ",(int)(it->pd_trys)); 
  293.                     if (it->pd_trys == 0)
  294.                         printf("(driver default)");
  295.                     printf("\n");
  296.                     break;
  297.             case 16: printf("   %4d \n",(int)(it->pd_park)); break;
  298.             case 17: printf("   %4d \n",(int)(it->pd_lsnoffs)); break;
  299.             case 18: printf("  %5d \n",(int)(it->pd_totcyls)); break;
  300.             case 19: printf("   0x%02x ",(int)(it->pd_rate)); 
  301.                     if (!floppy_disk)
  302.                     {
  303.                         printf("\n");
  304.                         break;
  305.                     }
  306.                     x = it->pd_rate & 0x7;
  307.                     if (x == RPM_300)
  308.                         printf("300");
  309.                     if (x == RPM_360)
  310.                         printf("360");
  311.                     if (x == RPM_600)
  312.                         printf("600");
  313.                     printf(" rpm, ");
  314.                     x = (it->pd_rate >> 4) & 0xf;
  315.                     if (x == XFR_125K)
  316.                         printf("125 k/bytes sec.");
  317.                     if (x == XFR_250K)
  318.                         printf("250 k/bytes sec.");
  319.                     if (x == XFR_300K)
  320.                         printf("300 k/bytes sec.");
  321.                     if (x == XFR_500K)
  322.                         printf("500 k/bytes sec.");
  323.                     if (x == XFR_1M)
  324.                         printf("1 m/bytes sec.");
  325.                     if (x == XFR_2M)
  326.                         printf("2 m/bytes sec.");
  327.                     if (x == XFR_5M)
  328.                         printf("5 m/bytes sec.");
  329.                     printf("\n");
  330.                     break;
  331.             case 20: printf("  %5d ",(int)(it->pd_maxcnt)); 
  332.                     if ((it->pd_maxcnt) == 0xffffffff)
  333.                         printf("(unlimited)\n");
  334.                     else
  335.                         printf("bytes\n");
  336.                     break;
  337.             default: printf("**** Unknown data subscript\n");
  338.         }
  339.     }
  340.     
  341.     munlink((mod_exec*)mod);
  342.  
  343. }
  344.  
  345. /*
  346.     Change parameter values in an RBF descriptor
  347. */
  348. void update_descriptor(n,args)
  349. int        n;
  350. char    **args;
  351. {
  352.     mod_dev          *mod;
  353.     rbf_dev            *it;
  354.     char            *devptr;
  355.     int                i,
  356.                     cmndref,
  357.                     newval;
  358.  
  359.     char* attach();
  360.  
  361.     if (args[1][0] != '/')
  362.         error(args[0],args[1],"is not a valid device name");
  363.  
  364.     /*
  365.         Get the device table pointer and detach the device
  366.     */
  367.     devptr = attach(args[1]+1,0);
  368.     if (devptr == (char*)-1)
  369.         error(args[0],"Can't find",args[1]);
  370.  
  371.     /*
  372.         Cancel the attachment
  373.     */
  374.     i = detach(devptr);
  375.  
  376.     /*
  377.         Detach the device before modifying it
  378.     */
  379.     if (!errno)
  380.         i = detach(devptr);
  381.     if (errno)
  382.         error(args[0],"Detach error while detaching %s\n", args[1]); 
  383.  
  384.     mod = (mod_dev*)modlink(args[1]+1,mktypelang(MT_DEVDESC,ML_ANY));
  385.     if (mod == (mod_dev*)-1)
  386.         exit(errno);
  387.  
  388.     if ((int)mod->_mdtype != 1)
  389.         error(args[0],args[1],"is not an RBF device");
  390.         
  391.     it = (rbf_dev*) ( (char*)mod + sizeof(mod_dev) -2 );
  392.  
  393.     for (i = 2; i < n; i++)
  394.     {
  395.         cmndref = isin(args[i]);
  396.         if (cmndref == -1)
  397.             error(args[0],"invalid keyword:",args[i]);
  398.         newval = value(args[i],hex[cmndref],args[0]);
  399.         
  400.         switch (cmndref)
  401.         {
  402.             case 0: it->pd_drv = (u_char)newval; break;
  403.             case 1: it->pd_stp = (u_char)newval; break;
  404.             case 2: it->pd_typ = (u_char)newval; break;
  405.             case 3: it->pd_dns = (u_char)newval; break;
  406.             case 4: it->pd_cyl = (u_short)newval; break;
  407.             case 5: it->pd_sid = (u_char)newval; break;
  408.             case 6: it->pd_vfy = (u_char)newval; break;
  409.             case 7: it->pd_sct = (u_short)newval; break;
  410.             case 8: it->pd_t0s = (u_short)newval; break;
  411.             case 9: it->pd_sas = (u_short)newval; break;
  412.             case 10: it->pd_ilv = (u_char)newval; break;
  413.             case 11: it->pd_toffs = (u_char)newval; break;
  414.             case 12: it->pd_soffs = (u_char)newval; break;
  415.             case 13: it->pd_ssize = (u_short)newval; break;
  416.             case 14: it->pd_cntl = (u_short)newval; break;
  417.             case 15: it->pd_trys = (u_char)newval; break;
  418.             case 16: it->pd_park = (u_short)newval; break;
  419.             case 17: it->pd_lsnoffs = (u_int)newval; break;
  420.             case 18: it->pd_totcyls = (u_short)newval; break;
  421.             case 19: it->pd_rate = (u_short)newval; break;
  422.             case 20: it->pd_maxcnt = (int)newval; break;
  423.             default: printf("**** Unknown data subscript\n");
  424.         }
  425.     }
  426.     
  427.     if (_setcrc((mod_exec*)mod) == -1)
  428.         exit(errno);    
  429.  
  430.     munlink((mod_exec*)mod); 
  431.  
  432.     /*
  433.         Re-attach the device now its modified
  434.     */
  435.     devptr = attach(args[1]+1,0);
  436.     if (devptr == (char*)-1)
  437.         error(args[0],"Can't re-attach",args[1]);
  438. }
  439.  
  440. /*
  441.     Utility functions follow
  442. */
  443.  
  444. /* 
  445.     isin returns the table location of the argument 
  446. */
  447. int isin(s)
  448. char    *s;
  449. {
  450.     int i;
  451.     
  452.     lowercase(s);
  453.     for ( i = 0 ; i < CMNDCNT ; i++ )
  454.         if (findstr(1,s,p[i]))
  455.             return(i);
  456.     return(-1);
  457. }
  458.  
  459. /*
  460.     Returns value integer from argument
  461. */
  462. int value(s,hex,prog)
  463. char    *s;
  464. int        hex;
  465. char    *prog;
  466. {
  467.     char    *param;
  468.     int        foundhex;
  469.     
  470.     param = s;
  471.     while (*s != '=')
  472.     {
  473.         if (*s == NULL)
  474.             error(prog,param,"must have value ( xxx=value )");
  475.         s++;
  476.     }
  477.     s++;
  478.     if (strncmp(s,"$",1) == 0)
  479.     {
  480.         s++;
  481.         foundhex = TRUE;
  482.     }
  483.     else
  484.     if (strncmp(s,"0x",2) == 0 || strncmp(s,"0X",2) == 0)
  485.     {
  486.         s = s+2;
  487.         foundhex = TRUE;
  488.     }
  489.     else
  490.         foundhex = FALSE;
  491.         
  492.     if (hex && !foundhex)
  493.         error(prog,
  494.               param,
  495.               "must have a hex value ( xxx=0xff or xxx=$ff )");
  496.     if (!hex && foundhex)
  497.         error(prog,param,"must have a decimal value ( xxx=val )");
  498.     if (hex)
  499.         return(atohex(s));
  500.     else
  501.         return(atoi(s));
  502. }
  503.  
  504. /*
  505.     Converts a hex string to binary
  506. */
  507. int atohex(s)
  508. char *s;
  509. {
  510.     int         hex,i;
  511.     static char hc[] = "0123456789abcdef";
  512.     
  513.     hex = 0;            
  514.     while (*s)
  515.     {
  516.         i = 0;
  517.         while (hc[i] && *s != hc[i])
  518.             i++;
  519.         if (i > 15)
  520.             break;            /* non-hex found */
  521.         hex = hex * 16 + i;
  522.         s++;
  523.     }
  524.     return(hex);
  525. }
  526.  
  527. /*
  528.     Convert string to lower case
  529. */
  530. lowercase(s)
  531. char *s;
  532. {
  533.     while(*s)
  534.     {
  535.         *s = tolower(*s);
  536.         s++;
  537.     }
  538. }
  539.  
  540. /*
  541.     Output an error message and quit
  542. */
  543. void error(progname,s1,s2)
  544. char *progname;
  545. char *s1;
  546. char *s2;
  547. {
  548.     fprintf(stderr, "%s: %s %s\n", progname, s1, s2);
  549.     exit(1);
  550. }
  551.  
  552.