home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / aspio02.zip / dispdef.c < prev    next >
C/C++ Source or Header  |  1999-08-18  |  5KB  |  248 lines

  1. /*
  2.  * $Source: r:/source/aspi/RCS/dispdef.c,v $
  3.  * $Revision: 1.2 $
  4.  * $Date: 1999/08/19 00:43:46 $
  5.  * $Locker:  $
  6.  *
  7.  *    Display primary/grown defects.
  8.  *
  9.  * $Log: dispdef.c,v $
  10.  * Revision 1.2  1999/08/19 00:43:46  vitus
  11.  * - more comments
  12.  * - more display output
  13.  *
  14.  * Revision 1.1  1999/08/18 02:00:57  vitus
  15.  * Initial revision
  16.  * ----------------------------------------
  17.  * Sample code to demonstrate use of ASPI Interface.
  18.  */
  19. static char const id[]="$Id: dispdef.c,v 1.2 1999/08/19 00:43:46 vitus Exp $";
  20.  
  21. #include <stdio.h>
  22.  
  23. #define INCL_DOS
  24. #include <os2.h>
  25.  
  26. #include "../lib/defines.h"
  27. #include "scsi.h"
  28. #include "srb.h"
  29. #include "aspio.h"
  30. #pragma pack(1)
  31.  
  32.  
  33.  
  34. PRIVATE UCHAR    data[5000];
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41. PRIVATE ULONG
  42. Swap16(USHORT us)
  43. {
  44.     return MAKEUSHORT(HIUCHAR(us), LOUCHAR(us));
  45. }
  46.  
  47.  
  48. PRIVATE ULONG
  49. Swap32(ULONG ul)
  50. {
  51.     /* Toshiba: 2* ex */
  52.     return MAKEULONG(MAKEUSHORT(HIUCHAR(HIUSHORT(ul)),
  53.                 LOUCHAR(HIUSHORT(ul))),
  54.              MAKEUSHORT(HIUCHAR(LOUSHORT(ul)),
  55.                 LOUCHAR(LOUSHORT(ul))));
  56. }
  57.  
  58.  
  59.  
  60.  
  61. PRIVATE void
  62. DumpBuffer(PVOID const arg1,ULONG const len)
  63. {
  64.     ULONG i;
  65.     PUCHAR p = arg1;
  66.  
  67.     for( i = 0; i < len; ++i, ++p )
  68.     {
  69.     printf("%02X", *p);
  70.     if( ((i + 1) % 16) == 0 )
  71.         printf("\n");
  72.     else if( ((i + 1) % 8) == 0 )
  73.         printf("-");
  74.     else
  75.         printf(" ");
  76.     }
  77.     putchar('\n');
  78. }
  79.  
  80.  
  81.  
  82.  
  83. /*# ----------------------------------------------------------------------
  84.  * DisplayDefects(data,descr)
  85.  *
  86.  * PARAMETER
  87.  *    data        defect data from device
  88.  *    descr        kind of defects (ASCIIZ)
  89.  *
  90.  * RETURNS
  91.  *    (nothing)
  92.  *
  93.  * DESCRIPTION
  94.  *    Displays defects in human readable form.
  95.  *
  96.  * REMARKS
  97.  */
  98. PRIVATE void
  99. DisplayDefects(PUCHAR data,PCSZ descr)
  100. {
  101.     ULONG    i;
  102.  
  103.  
  104.     printf("Defect list header: ");
  105.     DumpBuffer(data, 4);
  106.  
  107.  
  108.     if( (data[1] & 0x07) == 0 )
  109.     {
  110.     ULONG const    cnt = Swap16(*(PUSHORT)&data[2])/4;
  111.     PULONG        p = (PVOID)&data[4];
  112.  
  113.     printf("%lu %s in 'block format'\n", cnt, descr);
  114.     for( i = 0; i < cnt; ++i, ++p )
  115.         printf("%lu. defect block:\t%lu\n", i, Swap32(p[i]));
  116.     }
  117.     else if( (data[1] & 0x07) == 4 )
  118.     {
  119.     ULONG const    cnt = Swap16(*(PUSHORT)&data[2])/8;
  120.     struct _INDEX {
  121.         UCHAR    cyl[3];
  122.         UCHAR    head;
  123.         UCHAR    bc[4];
  124.     } * index = (PVOID)&data[4];
  125.  
  126.     printf("%lu %s in 'index format'\n", cnt, descr);
  127.     for( i = 0; i < cnt; ++i, ++index )
  128.     {
  129.         ULONG    ul1 = MAKEULONG(MAKEUSHORT(index[i].cyl[2],index[i].cyl[1]),
  130.                     MAKEUSHORT(index[i].cyl[0],0));
  131.         ULONG    ul2 = Swap32(*(PULONG)index[i].bc);
  132.  
  133.         printf("%lu. defect:\tCylinder %lu, Head %u, Bytes %lu\n",
  134.            i, ul1, index[i].head, ul2);
  135.     }
  136.     }
  137.     else if( (data[1] & 0x07) == 5 )
  138.     {
  139.     ULONG const    cnt = Swap16(*(PUSHORT)&data[2])/8;
  140.     struct _PHYS {
  141.         UCHAR    cyl[3];
  142.         UCHAR    head;
  143.         UCHAR    sec[4];
  144.     } * index = (PVOID)&data[4];
  145.  
  146.     printf("%lu %s in 'physical sector format'\n", cnt, descr);
  147.     for( i = 0; i < cnt; ++i, ++index )
  148.     {
  149.         ULONG    ul1 = Swap32((*(PULONG)index[i].cyl) & 0x00FFFFFF) >> 8;
  150.         ULONG    ul2 = Swap32(*(PULONG)index[i].sec);
  151.  
  152.         printf("%lu. defect:\tCylinder %lu, Head %u, Sector %lu\n",
  153.            i, ul1, index[i].head, ul2);
  154.     }
  155.     }
  156.     else
  157.     {
  158.     printf("%s in unknown format, dumping:\n", descr);
  159.     DumpBuffer(data, Swap16(*(PUSHORT)&data[2]));
  160.     }
  161.     return;
  162. }
  163.  
  164.  
  165.  
  166.  
  167. PUBLIC int
  168. main(int argc,char *argv[])
  169. {
  170.     APIRET    rc;
  171.     unsigned    ha = -1, target = -1, lun = -1;
  172.     UCHAR    type;                /* see SCSI spec */
  173.  
  174.     if( argc == 4 )
  175.     {
  176.     sscanf(argv[1], " %u", &ha);
  177.     sscanf(argv[2], " %u", &target);
  178.     sscanf(argv[3], " %u", &lun);
  179.     }
  180.     if( ha > 7  ||  target > 7  ||  lun > 7 )
  181.     {
  182.     fprintf(stderr,
  183.         "Invalid parameters\n"
  184.         "usage: dispdef <ha> <target> <lun>\n");
  185.     return 1;
  186.     }
  187.  
  188.  
  189.     rc = AspiOpen(0);
  190.     if( rc != 0 )
  191.     {
  192.     fprintf(stderr, "AspiOpen - rc %lu\n", rc);
  193.     return rc;
  194.     }
  195.  
  196.     do
  197.     {
  198.     rc = AspiGetType(ha, target, lun, &type);
  199.     if( rc != 0 )
  200.     {
  201.         fprintf(stderr, "AspiGetType(%u,%u,%u) - rc %lu (%#lx)\n",
  202.             ha, target, lun, rc, rc);
  203.         break;
  204.     }
  205.  
  206.     printf("HA %u  Target %u  LUN %u\t\"%s\" (%s)\n",
  207.            ha, target, lun,
  208.            AHInquiryType(type), AHInquiryQual(type));
  209.  
  210.  
  211.     printf("Getting primary defects (not critical)...\n");
  212.     rc = AspiDefectData(ha, target, lun, 0, 0, data, sizeof(data));
  213.     if( rc != 0 )
  214.     {
  215.         fprintf(stderr, "AspiDefectData - rc %lu (%#lx)\n", rc, rc);
  216.         DumpBuffer(&strLastSense, sizeof(strLastSense));
  217.         AHSense(data, &strLastSense),    printf(data);
  218.         break;
  219.     }
  220.  
  221.     DisplayDefects(data, "primary defects");
  222.  
  223.  
  224.  
  225.     printf("Getting grown defects (keep an eye on this data)...\n");
  226.     rc = AspiDefectData(ha, target, lun, 1, 0, data, sizeof(data));
  227.     if( rc != 0 )
  228.     {
  229.         fprintf(stderr, "AspiDefectData - rc %lu (%#lx)\n", rc, rc);
  230.         DumpBuffer(&strLastSense, sizeof(strLastSense));
  231.         AHSense(data, &strLastSense),    printf(data);
  232.         break;
  233.     }
  234.  
  235.     DisplayDefects(data, "grown defects");
  236.     }
  237.     while( 0 );
  238.  
  239.     rc = AspiClose();
  240.     if( rc != 0 )
  241.     {
  242.     fprintf(stderr, "AspiClose - rc %lu (%#lx)\n", rc, rc);
  243.     return rc;
  244.     }
  245.  
  246.     return 0;
  247. }
  248.