⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blockdev.c

📁 DOC文件系统驱动源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
        vol.password[0] = UNAL4(partitionTable->passwordInfo[1]);
        vol.password[1] = UNAL4(partitionTable->passwordInfo[2]);
        vol.flags |= VOLUME_WRITE_PROTECTED;
     }
  }
#endif   /* WRITE_PROTECTION */
  /* Disable FAT monitoring */
  vol.firstFATSectorNo = vol.secondFATSectorNo = 0; 
  vol.flags |= VOLUME_ABS_MOUNTED;  /* Enough to do abs operations */

  return flOK;
}


/*----------------------------------------------------------------------*/
/*                     m o u n t V o l u m e                            */
/*                                                                      */
/* Mounts the Flash volume.                                             */
/*                                                                      */
/* In case the inserted volume has changed, or on the first access to   */
/* the file system, it should be mounted before file operations can be  */
/* done on it.                                                          */
/* Mounting a volume has the effect of discarding all open files (the   */
/* files cannot be properly closed since the original volume is gone),  */
/* and turning off the media-change indication to allow file processing */
/* calls.                                                               */
/*                                                                      */
/* The volume automatically becomes unmounted if it is removed or       */
/* changed.                                                             */
/*                                                                      */
/* Parameters:                                                          */
/*      vol             : Pointer identifying drive                     */
/*                                                                      */
/* Returns:                                                             */
/*      FLStatus        : 0 on success, otherwise failed                */
/*      bootSectors     : Returns the number of sectors, flMountVolume  */
/*                        skipps.                                       */
/*----------------------------------------------------------------------*/

static FLStatus mountVolume(Volume vol,unsigned FAR2* bootSectors)
{
  SectorNo noOfSectors;
  PartitionTable FAR0 *partitionTable;
  Partition ptEntry;
  DOSBootSector FAR0 *bootSector;
  unsigned ptCount,extended_depth,ptSector;
  FLBoolean primaryPtFound = FALSE, extendedPtFound = TRUE;

  *bootSectors=0;
  checkStatus(absMountVolume(&vol));

  for(extended_depth = 0,ptSector = 0;
      (extended_depth<MAX_PARTITION_DEPTH) &&
      (primaryPtFound==FALSE) &&
      (extendedPtFound==TRUE);
      extended_depth++) {

    extendedPtFound=FALSE;
    /* Read in paritition table */

    partitionTable = (PartitionTable FAR0 *) findSector(&vol,ptSector);

    if(partitionTable == NULL) {
      vol.tl.dismount(vol.tl.rec);
      return flSectorNotFound;
    }
    if((void FAR0 *)partitionTable==dataErrorToken) {
      vol.tl.dismount(vol.tl.rec);
      return flDataError;
    }

    if (LE2(partitionTable->signature) != PARTITION_SIGNATURE)
      break;
    for(ptCount=0;
    (ptCount<4) && (primaryPtFound==FALSE) && (extendedPtFound==FALSE);
    ptCount++) {

      ptEntry = partitionTable->ptEntry[ptCount];

      switch (ptEntry.type) {
    case FAT12_PARTIT:
    case FAT16_PARTIT:
    case DOS4_PARTIT:
      primaryPtFound = TRUE;
      vol.bootSectorNo =
          (unsigned) UNAL4(ptEntry.startingSectorOfPartition);
      *bootSectors=vol.bootSectorNo;
      break;
    case EX_PARTIT:
      extendedPtFound = TRUE;
      ptSector = (unsigned)UNAL4(ptEntry.startingSectorOfPartition);
      break;
    default:
      break;
      }
    }
  }

  bootSector = (DOSBootSector FAR0 *) findSector(&vol,vol.bootSectorNo);
  if(bootSector == NULL)
    return flSectorNotFound;

  if((void FAR0 *)bootSector==dataErrorToken)
    return flDataError;

  /* Do the customary sanity checks */
  if (!(bootSector->bpb.jumpInstruction[0] == 0xe9 ||
    (bootSector->bpb.jumpInstruction[0] == 0xeb &&
     bootSector->bpb.jumpInstruction[2] == 0x90))) {
    DEBUG_PRINT(("Debug: did not recognize format.\r\n"));
    return flNonFATformat;
  }

  /* See if we handle this sector size */
  if (UNAL2(bootSector->bpb.bytesPerSector) != SECTOR_SIZE)
    return flFormatNotSupported;

  vol.sectorsPerCluster = bootSector->bpb.sectorsPerCluster;
  vol.numberOfFATS = bootSector->bpb.noOfFATS;
  vol.sectorsPerFAT = LE2(bootSector->bpb.sectorsPerFAT);
  vol.firstFATSectorNo = vol.bootSectorNo +
                LE2(bootSector->bpb.reservedSectors);
  vol.secondFATSectorNo = vol.firstFATSectorNo +
                 LE2(bootSector->bpb.sectorsPerFAT);
  vol.rootDirectorySectorNo = vol.firstFATSectorNo +
           bootSector->bpb.noOfFATS * LE2(bootSector->bpb.sectorsPerFAT);
  vol.sectorsInRootDirectory =
    (UNAL2(bootSector->bpb.rootDirectoryEntries) * DIRECTORY_ENTRY_SIZE - 1) /
        SECTOR_SIZE + 1;
  vol.firstDataSectorNo = vol.rootDirectorySectorNo +
                 vol.sectorsInRootDirectory;

  noOfSectors = UNAL2(bootSector->bpb.totalSectorsInVolumeDOS3);
  if (noOfSectors == 0)
    noOfSectors = (SectorNo) LE4(bootSector->bpb.totalSectorsInVolume);


  vol.maxCluster = (unsigned) ((noOfSectors + vol.bootSectorNo - vol.firstDataSectorNo) /
                vol.sectorsPerCluster) + 1;

  if (vol.maxCluster < 4085) {
#ifdef FAT_12BIT
    vol.flags |= VOLUME_12BIT_FAT;      /* 12-bit FAT */
#else
    DEBUG_PRINT(("Debug: FAT_12BIT must be defined.\r\n"));
    return flFormatNotSupported;
#endif
  }
  vol.bytesPerCluster = vol.sectorsPerCluster * SECTOR_SIZE;
  vol.allocationRover = 2;      /* Set rover at first cluster */
  vol.flags |= VOLUME_MOUNTED;  /* That's it */
  return flOK;
}

#ifndef FL_READ_ONLY
#ifdef DEFRAGMENT_VOLUME

/*----------------------------------------------------------------------*/
/*                       d e f r a g m e n t V o l u m e                */
/*                                                                      */
/* Performs a general defragmentation and recycling of non-writable     */
/* Flash areas, to achieve optimal write speed.                         */
/*                                                                      */
/* NOTE: The required number of sectors (in irLength) may be changed    */
/* (from another execution thread) while defragmentation is active. In  */
/* particular, the defragmentation may be cut short after it began by   */
/* modifying the irLength field to 0.                                   */
/*                                                                      */
/* Parameters:                                                          */
/*      vol             : Pointer identifying drive                     */
/*      ioreq->irLength : Minimum number of sectors to make available   */
/*                        for writes.                                   */
/*                                                                      */
/* Returns:                                                             */
/*      ioreq->irLength : Actual number of sectors available for writes */
/*      FLStatus        : 0 on success, otherwise failed                */
/*----------------------------------------------------------------------*/

static FLStatus defragmentVolume(Volume vol, IOreq FAR2 *ioreq)
{
  return vol.tl.defragment(vol.tl.rec,&ioreq->irLength);
}

#endif /* DEFRAGMENT_VOLUME */

#ifdef FORMAT_VOLUME

/*-----------------------------------------------------------------------*/
/*                    f l F o r m a t V o l u m e                        */
/*                                                                       */
/* Formats a volume, writing a new and empty file-system. All existing   */
/* data is destroyed. Optionally, a low-level FTL formatting is also     */
/* done.                                                                 */
/* Formatting leaves the volume in the dismounted state, so that a       */
/* flMountVolume call is necessary after it.                             */
/*                                                                       */
/* Note: This routine was left for backwards compatibility with OSAK 4.2 */
/*       and down therfore it is strongly recommended to use the         */
/*       flFormatPhysicalDrive routine instead.                          */
/*                                                                       */
/* Parameters:                                                           */
/*      vol             : Pointer identifying drive                      */
/*      irHandle        : Drive number (0, 1, ...)                       */
/*      irFlags         : FAT_ONLY_FORMAT : Do FAT formatting only       */
/*                        TL_FORMAT_ONLY  : Do TL format only            */
/*                        TL_FORMAT       : Translation layer + FAT      */
/*                        TL_FORMAT_IF_NEEDED: Do TL formatting only if  */
/*                                             current format is invalid */
/*                                             but perform FAT anyway    */
/*      irData          : Address of FormatParams structure to use       */
/*                        (defined in flformat.h)                        */
/*                                                                       */
/* Returns:                                                              */
/*      FLStatus        : 0 on success, otherwise failed                 */
/*-----------------------------------------------------------------------*/

static FLStatus bdFormatVolume(Volume vol, IOreq FAR2 *ioreq)
{
  FormatParams FAR2 *userFp = (FormatParams FAR2 *) ioreq->irData;
  BDTLPartitionFormatParams bdtlFp;
  TLFormatParams tlFp;
  FLBoolean mountOK = FALSE;
  FLStatus status;
  byte socket = FL_GET_SOCKET_FROM_HANDLE(ioreq);

  /* Convert argument to TLFormatParmas */

  tlFp.noOfBinaryPartitions = 0;
  tlFp.noOfBDTLPartitions   = 1;
  tlFp.BDTLPartitionInfo    = NULL;
  tlFp.binaryPartitionInfo  = NULL;
  tlFp.bootImageLen         = userFp->bootImageLen;
  tlFp.percentUse           = (byte)userFp->percentUse;
  tlFp.noOfSpareUnits       = (byte)userFp->noOfSpareUnits;
  tlFp.noOfCascadedDevices  = 0;
  tlFp.progressCallback     = userFp->progressCallback;
  tlFp.vmAddressingLimit    = userFp->vmAddressingLimit;
  tlFp.embeddedCISlength    = (word)userFp->embeddedCISlength;
  tlFp.embeddedCIS          = (byte FAR1 *)userFp->embeddedCIS;
  tlFp.flags                = FL_LEAVE_BINARY_AREA;
#ifdef WRITE_EXB_IMAGE
  tlFp.exbLen               = 0;
#endif /* WRITE_EXB_IMAGE */
#ifdef HW_PROTECTION
  /* protectionKey[8]; */
  tlFp.protectionType       = 0;
#endif /* HW_PROTECTION */

  /* Dismount all physical drive volumes and set handle to the first */

  checkStatus(dismountPhysicalDrive(socket));
  pVol = &vols[socket];

  /* Format according to the irFlags argument */

  if ((ioreq->irFlags & TL_FORMAT)||(ioreq->irFlags & TL_FORMAT_ONLY))
  {
     checkStatus(flFormat(socket,&tlFp,vol.flash));
  }
  else
  {
     status = flMount(socket,socket,&vol.tl,FALSE,vol.flash); /* Try to mount translation layer */
     mountOK = TRUE;
     if ((status == flUnknownMedia || status == flBadFormat) &&
     (ioreq->irFlags & TL_FORMAT_IF_NEEDED))
     {
        status = flFormat(socket,&tlFp,vol.flash);
        mountOK = FALSE;
     }
     else
     {
        /*  assume sector 0 is DOS boot block */
        vol.bootSectorNo     = 0;     
        /* Disable FAT monitoring */
        vol.firstFATSectorNo = vol.secondFATSectorNo = 0; 
        /* Enough to do abs operations */
        vol.flags |= VOLUME_ABS_MOUNTED; 
     }
     if (status != flOK)
        return status;
  }

  if (!mountOK)
    checkStatus(absMountVolume(&vol)); /* Mount the first partition */

#if (defined(VERIFY_WRITE) || defined(VERIFY_VOLUME) || defined(VERIFY_ERASED_SECTOR))
  if(vol.tl.checkVolume != NULL)
    checkStatus(vol.tl.checkVolume(vol.tl.rec));
#endif /* VERIFY_WRITE || VERIFY_VOLUME || VERIFY_ERASED_SECTOR */

  if(!(ioreq->irFlags & TL_FORMAT_ONLY))
  {
    /* build BDTL record for dos format routine *

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -