home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 10 / mycd10.iso / share / os2 / utilidad / ext2fl / e2inutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-31  |  9.6 KB  |  275 lines

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