home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / e2fltsrc.zip / e2inutil.c < prev    next >
C/C++ Source or Header  |  1995-07-25  |  9KB  |  264 lines

  1. /************************************************************************/
  2. /*  Linux partition filter (C) Deon van der Westhuysen.                 */
  3. /*                                                                      */
  4. /*  Dedicated to Jesus Christ, my Lord and Saviour.                     */
  5. /*                                                                      */
  6. /*  Permission is granted to freely use and modify this code for non-   */
  7. /*  profit use on the condition that this notice is not removed. All    */
  8. /*  other rights are reserved. No warranty, etc.                        */
  9. /*                                                                      */
  10. /*  This code is still under development; expect some rough edges.      */
  11. /*                                                                      */
  12. /************************************************************************/
  13.  
  14. #include "debug.h" 
  15. #include "e2data.h"
  16. #include "e2wrap.h"
  17. #include "e2inutil.h"
  18.  
  19. UCHAR        InitIORB[MAX_IORB_SIZE]= {0};    /* IORB to use */
  20.  
  21. /* Add a unit that we might want to use to the base unit list after */
  22. /* allocating the unit. */
  23. USHORT InitAddBaseUnit (PADDEntryPoint pADDEntry, PADAPTERINFO pAdapterInfo)
  24. {
  25.  NPBaseUnitRec    pBaseUnit;                
  26.  USHORT        UnitHandle;                
  27.  USHORT        Result;
  28.  
  29.  if (NumBaseUnits==MAX_LINUX_PARTITIONS)    /* Check table full */
  30.   return 0xFFFF;
  31.  pBaseUnit= BaseUnits+NumBaseUnits;        /* Pointer to new entry */
  32.  UnitHandle= pAdapterInfo->UnitInfo->UnitHandle; 
  33.  if ((Result=InitAllocateUnit (pADDEntry,UnitHandle))!=0)
  34.                         /* Try to allocate unit */
  35.   return Result;                /* If failed, return error */
  36.  
  37.  /* Fill first free base unit record with info for current unit */
  38.  pBaseUnit->Hdr.Flags= 0;
  39.  pBaseUnit->UnitHandle= pAdapterInfo->UnitInfo->UnitHandle;
  40.  pBaseUnit->pADDEntry= pADDEntry;
  41.  if (InstallFlags&FI_ALLPART)            /* If virtualize all parts: */
  42.   pAdapterInfo->UnitInfo->UnitFlags|=UF_NODASD_SUPT;  
  43.                         /* Disable DASD support for */
  44.                         /* the original base unit. */
  45.  memcpy (&pBaseUnit->AdapterInfo,pAdapterInfo,sizeof(ADAPTERINFO));
  46.  
  47.  /* Get geometry for this unit; ignore any errors getting it. */
  48.  InitGetGeometry (pBaseUnit->pADDEntry,pBaseUnit->UnitHandle,
  49.                   &(pBaseUnit->GeoNumHeads),&(pBaseUnit->GeoTrackSec));
  50.  NumBaseUnits++;                    /* Keep this entry */
  51.  return 0;
  52. }
  53.  
  54. /* Function to remove the last unit in the BaseUnits array. Deallocates the */
  55. /* unit. Should not be called for 'commited' units. */
  56. USHORT InitRemoveBaseUnit (void)
  57. {
  58.  USHORT        UnitHandle;
  59.  PADDEntryPoint    pADDEntry;
  60.  
  61.  NumBaseUnits--;                    /* Remove unit */
  62.  UnitHandle= BaseUnits[NumBaseUnits].UnitHandle;
  63.  pADDEntry= BaseUnits[NumBaseUnits].pADDEntry;
  64.  return InitFreeUnit (pADDEntry,UnitHandle);         /* Deallocate unit */
  65. }
  66.  
  67. /* Function to install our filter on a base unit. Called after we found a */
  68. /* partition that we want to keep on this unit. */
  69. USHORT InitFilterBaseUnit (int Index)
  70. {
  71.  NPBaseUnitRec    pBaseUnit;                
  72.  USHORT        Result;
  73.  
  74.  pBaseUnit= BaseUnits+Index;            /* Pointer to last base unit */
  75.  pBaseUnit->AdapterInfo.UnitInfo->UnitHandle= (USHORT) pBaseUnit;
  76.  pBaseUnit->AdapterInfo.UnitInfo->FilterADDHandle= ADDHandle;
  77.  Result= InitChangeUnitInfo (pBaseUnit->pADDEntry,pBaseUnit->UnitHandle,
  78.                              pBaseUnit->AdapterInfo.UnitInfo);
  79.  if (Result)                    /* Error changing info... */
  80.   pBaseUnit->AdapterInfo.AdapterUnits= 0;    /* No units on adapter */
  81.  return Result;
  82. }
  83.  
  84. int InitAddVirtualUnit (ULONG StartRBA, ULONG NumSectors, UCHAR SysIndicator)
  85. {
  86.  NPVirtUnitRec    pVirtUnit;
  87.  ULONG        NumShort;
  88.  ULONG        SectorsPerCylinder;
  89.  USHORT        GeoNumHeads;
  90.  USHORT        GeoTrackSec;
  91.  
  92.  
  93.  if (NumVirtUnits==MAX_LINUX_PARTITIONS)        /* Check table full */
  94.   return 0;
  95.  pVirtUnit= VirtUnits+(NumVirtUnits++);            /* Grab next entry */
  96.  pVirtUnit->Hdr.Flags= 0;
  97.  if (InstallFlags&FI_ALLOWWRITE)            /* If writes allowed: */
  98.   pVirtUnit->Hdr.Flags|= F_ALLOW_WRITE;            /* Make unit writable */
  99.  pVirtUnit->PartSysIndicator= SysIndicator;        /* Save sys indicator */
  100.  pVirtUnit->pSourceUnitRec= BaseUnits+NumBaseUnits-1;
  101.  pVirtUnit->StartRBA= StartRBA;
  102.  pVirtUnit->NumSectors= NumSectors;
  103.  
  104.  GeoNumHeads= pVirtUnit->pSourceUnitRec->GeoNumHeads;
  105.  GeoTrackSec= pVirtUnit->pSourceUnitRec->GeoTrackSec;
  106.  SectorsPerCylinder= GeoNumHeads*GeoTrackSec;
  107.  
  108.  NumShort= pVirtUnit->NumSectors%SectorsPerCylinder;
  109.  if (NumShort) NumShort= SectorsPerCylinder-NumShort;
  110.  pVirtUnit->NumExtraSectors= NumShort+
  111.     SectorsPerCylinder*+((NumShort<GeoTrackSec)?2:1);
  112.  pVirtUnit->NumVirtualSectors= pVirtUnit->NumExtraSectors+
  113.     ((pVirtUnit->PartSysIndicator== PARTITION_LINUX)?LINUX_VIRTUAL_SECS:0);
  114.  return 1;
  115. }
  116.  
  117. /* Function to read the device table from a adapter device driver */
  118. USHORT InitReadDevTable (PADDEntryPoint pAddEP, PDEVICETABLE pDevTable,
  119.                          USHORT DevTableLen)
  120. {
  121. #define pIOCF    ((PIORB_CONFIGURATION) InitIORB)
  122.  
  123.  pIOCF->iorbh.Length= sizeof(IORB_CONFIGURATION);
  124.  pIOCF->iorbh.UnitHandle= 0;
  125.  pIOCF->iorbh.CommandCode= IOCC_CONFIGURATION;
  126.  pIOCF->iorbh.CommandModifier= IOCM_GET_DEVICE_TABLE;
  127.  
  128.  pIOCF->pDeviceTable= pDevTable;
  129.  pIOCF->DeviceTableLen= DevTableLen;
  130.  
  131.  return InitSendIORB((PIORB) pIOCF,pAddEP);
  132.  
  133. #undef pIOCF
  134. }
  135.  
  136. /* Function to allocate a unit. Needed before any function except */
  137. /* InitReadDevTable */
  138. USHORT InitAllocateUnit (PADDEntryPoint pAddEP, USHORT UnitHandle)
  139. {
  140. #define pIOUC    ((PIORB_UNIT_CONTROL) InitIORB)
  141.  
  142.  pIOUC->iorbh.Length= sizeof(IORB_UNIT_CONTROL);
  143.  pIOUC->iorbh.UnitHandle= UnitHandle;
  144.  pIOUC->iorbh.CommandCode= IOCC_UNIT_CONTROL;
  145.  pIOUC->iorbh.CommandModifier= IOCM_ALLOCATE_UNIT;
  146.  pIOUC->Flags= 0;
  147.  return InitSendIORB((PIORB) pIOUC,pAddEP);
  148.  
  149. #undef pIOUC
  150. }
  151.  
  152. /* Function to release an allocated unit. */
  153. USHORT InitFreeUnit (PADDEntryPoint pAddEP, USHORT UnitHandle)
  154. {
  155. #define pIOUC    ((PIORB_UNIT_CONTROL) InitIORB)
  156.  
  157.  pIOUC->iorbh.Length= sizeof(IORB_UNIT_CONTROL);
  158.  pIOUC->iorbh.UnitHandle= UnitHandle;
  159.  pIOUC->iorbh.CommandCode= IOCC_UNIT_CONTROL;
  160.  pIOUC->iorbh.CommandModifier= IOCM_DEALLOCATE_UNIT;
  161.  pIOUC->Flags= 0;
  162.  return InitSendIORB((PIORB) pIOUC,pAddEP);
  163.  
  164. #undef pIOUC
  165. }
  166.  
  167. /* Function to change the unit information of a unit. */
  168. USHORT InitChangeUnitInfo (PADDEntryPoint pAddEP, USHORT UnitHandle,
  169.                            PUNITINFO pUnitInfo)
  170. {
  171. #define pIOUC    ((PIORB_UNIT_CONTROL) InitIORB)
  172.  
  173.  pIOUC->iorbh.Length= sizeof(IORB_UNIT_CONTROL);
  174.  pIOUC->iorbh.UnitHandle= UnitHandle;
  175.  pIOUC->iorbh.CommandCode= IOCC_UNIT_CONTROL;
  176.  pIOUC->iorbh.CommandModifier= IOCM_CHANGE_UNITINFO;
  177.  pIOUC->Flags= 0;
  178.  pIOUC->pUnitInfo= pUnitInfo;
  179.  pIOUC->UnitInfoLen= sizeof(UNITINFO);
  180.  return InitSendIORB((PIORB) pIOUC,pAddEP);
  181.  
  182. #undef pIOUC
  183. }
  184.  
  185. /* Function to get disk geometry of an unit. */
  186. USHORT InitGetGeometry (PADDEntryPoint pAddEP, USHORT UnitHandle,
  187.                         USHORT FAR *NumHeads, USHORT FAR *TrackSectors)
  188. {
  189. #define pIOG    ((PIORB_GEOMETRY) InitIORB)
  190.  
  191.  GEOMETRY    Geometry;
  192.  USHORT        Result;
  193.  
  194.  pIOG->iorbh.Length= sizeof(IORB_GEOMETRY);
  195.  pIOG->iorbh.UnitHandle= UnitHandle;
  196.  pIOG->iorbh.CommandCode= IOCC_GEOMETRY;
  197.  pIOG->iorbh.CommandModifier= IOCM_GET_DEVICE_GEOMETRY;
  198.  pIOG->pGeometry= &Geometry;
  199.  pIOG->GeometryLen= sizeof (Geometry);
  200.  memset (&Geometry,0,sizeof(Geometry));
  201.  
  202.  Result= InitSendIORB((PIORB) pIOG,pAddEP);
  203.  *NumHeads= Geometry.NumHeads;
  204.  *TrackSectors= Geometry.SectorsPerTrack;
  205.  if ((Result)||(*(NumHeads)==0)||(*(TrackSectors)==0))
  206.  {
  207.   *NumHeads= 32;
  208.   *TrackSectors= 32;
  209.  }
  210.  return Result;
  211.  
  212. #undef pIOG
  213. }
  214.  
  215. /* Reads a sector into the buffer at physical address ppSectorBuf. */
  216. USHORT InitReadSector (PADDEntryPoint pAddEP, USHORT UnitHandle,
  217.                        ULONG SectorRBA, ULONG ppSectorBuf)
  218. {
  219. #define pIOXIO    ((PIORB_EXECUTEIO) InitIORB)
  220.  
  221.  PSCATGATENTRY        pScatGat;
  222.  
  223. /* Fill in the scatter/gather list...*/
  224.  pScatGat= (PSCATGATENTRY) pIOXIO->iorbh.DMWorkSpace;
  225.  pScatGat->XferBufLen= SECTOR_SIZE;
  226.  pScatGat->ppXferBuf= ppSectorBuf;
  227.  
  228.  pIOXIO->iorbh.Length= sizeof (IORB_EXECUTEIO);
  229.  pIOXIO->iorbh.UnitHandle= UnitHandle;
  230.  pIOXIO->iorbh.CommandCode= IOCC_EXECUTE_IO;
  231.  pIOXIO->iorbh.CommandModifier= IOCM_READ;
  232.  pIOXIO->cSGList= 1;
  233.  pIOXIO->pSGList= pScatGat;
  234.  pIOXIO->ppSGList= ppDataSeg+OFFSETOF((void FAR*)pScatGat);
  235.  pIOXIO->RBA= SectorRBA;
  236.  pIOXIO->BlockCount= 1;
  237.  pIOXIO->BlocksXferred= 0;
  238.  pIOXIO->BlockSize= SECTOR_SIZE;
  239.  pIOXIO->Flags= 0;
  240.  return InitSendIORB ((PIORB) pIOXIO,pAddEP);
  241.  
  242. #undef PIOXIO
  243. }
  244.  
  245. /* Function to submit an IORB. Waits for request to finish before returning */
  246. USHORT InitSendIORB(PIORB pIORB, PADDEntryPoint pADDEntry)
  247. {
  248.  pIORB->NotifyAddress  = (void far*) ProcRun;
  249.  pIORB->RequestControl = IORB_ASYNC_POST;
  250.  pIORB->ErrorCode      = 0;
  251.  pIORB->Status         = 0;
  252.  
  253.  (*pADDEntry)(pIORB);
  254.  
  255.  DISABLE
  256.  while (!(pIORB->Status & IORB_DONE))
  257.  {
  258.   ProcBlock(pIORB);
  259.   DISABLE
  260.  }
  261.  ENABLE
  262.  return pIORB->ErrorCode;
  263. }
  264.