|
From: <ls...@us...> - 2007-10-11 09:16:09
|
Revision: 3543
http://jnode.svn.sourceforge.net/jnode/?rev=3543&view=rev
Author: lsantha
Date: 2007-10-11 02:16:06 -0700 (Thu, 11 Oct 2007)
Log Message:
-----------
Initial FAT formatter implementation by tango.
Modified Paths:
--------------
trunk/fs/src/fs/org/jnode/fs/jfat/BootSector.java
trunk/fs/src/fs/org/jnode/fs/jfat/FatFileSystemType.java
trunk/fs/src/fs/org/jnode/fs/jfat/FatUtils.java
Added Paths:
-----------
trunk/fs/src/fs/org/jnode/fs/jfat/FatFormatter.java
trunk/fs/src/fs/org/jnode/fs/jfat/FatFsInfo.java
trunk/fs/src/fs/org/jnode/fs/jfat/command/FatFormatCommand.java
Modified: trunk/fs/src/fs/org/jnode/fs/jfat/BootSector.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/jfat/BootSector.java 2007-10-09 15:02:20 UTC (rev 3542)
+++ trunk/fs/src/fs/org/jnode/fs/jfat/BootSector.java 2007-10-11 09:16:06 UTC (rev 3543)
@@ -1,5 +1,23 @@
-/*
+/**
*
+ * $Id$
+ *
+ * JNode.org
+ * Copyright (C) 2003-2006 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.jnode.fs.jfat;
@@ -8,7 +26,9 @@
import java.nio.ByteBuffer;
import java.util.Arrays;
+import org.apache.log4j.Logger;
import org.jnode.fs.FileSystemException;
+import org.jnode.fs.jfat.StrWriter;
import org.jnode.driver.block.BlockDeviceAPI;
import org.jnode.util.LittleEndian;
import org.jnode.util.NumberUtils;
@@ -16,8 +36,12 @@
/**
* @author gvt
+ * @author Tango
*/
public class BootSector {
+ private static final Logger log =
+ Logger.getLogger ( BootSector.class );
+
private static final int IFAT12 = 12;
private static final int IFAT16 = 16;
private static final int IFAT32 = 32;
@@ -120,10 +144,12 @@
}
- public synchronized void write ( BlockDeviceAPI device )
- throws IOException {
- encode();
- device.write ( 0, ByteBuffer.wrap ( sector ) );
+ public synchronized void write ( BlockDeviceAPI device,long offset )
+ throws IOException
+ {
+ //encode();//TODO: Notice here once (Changed Now)
+ device.write (offset, ByteBuffer.wrap ( sector ) );
+
dirty = false;
}
@@ -358,12 +384,18 @@
return false;
}
-
+ /**
+ *
+ * @return
+ */
public int fatSize() {
return type;
}
-
+ /**
+ *
+ * @return BPB_Media
+ */
public int getMediumDescriptor() {
return BPB_Media;
}
@@ -406,7 +438,147 @@
return FirstDataSector;
}
+ /**
+ * The Setting methods are writing here.
+ *
+ */
+ public void setBS_JmpBoot(byte[] BS_jmpBoot){
+ setBytes (0,3, BS_jmpBoot);
+ }
+
+ public void setBS_OemName(String BS_OEMName){
+ setString( 3, 8, BS_OEMName );
+ }
+
+
+ public void setBPB_BytesPerSector(int BPB_BytsPerSec){
+ set16( 11, BPB_BytsPerSec );
+ }
+
+
+ public void setBPB_SecPerCluster(int BPB_SecPerClus){
+ set8(13,BPB_SecPerClus );
+ }
+
+
+ public void setBPB_RsvSecCount(int BPB_RsvdSecCnt){
+ set16 (14,BPB_RsvdSecCnt );
+ }
+
+
+ public void setBPB_NoFATs(int BPB_NumFATs){
+ set8 (16,BPB_NumFATs);
+ }
+
+
+ public void setBPB_RootEntCnt(int BPB_RootEntCnt){
+ set16( 17,BPB_RootEntCnt );
+ }
+
+
+ public void setBPB_TotSec16(int BPB_TotSec16){
+ set16( 19,BPB_TotSec16);
+ }
+
+
+ public void setBPB_MediumDescriptor(int BPB_Media){
+ set8( 21, BPB_Media );
+ }
+
+
+ public void setBPB_FATSz16(int BPB_FATSz16){
+ set16( 22,BPB_FATSz16 );
+ }
+
+
+ public void setBPB_SecPerTrk(int BPB_SecPerTrk){
+ set16( 24,BPB_SecPerTrk );
+ }
+
+
+ public void setBPB_NumHeads(int BPB_NumHeads){
+ set16( 26, BPB_NumHeads );
+ }
+
+
+ public void setBPB_HiddSec(long BPB_HiddSec){
+ set32( 28,BPB_HiddSec );
+ }
+
+
+ public void setBPB_TotSec32(long BPB_TotSec32){
+ set32( 32,BPB_TotSec32 );
+ }
+
+ public void setBPB_FATSz32(long BPB_FATSz32){
+ set32( 36,BPB_FATSz32 );
+ }
+
+
+ public void setBPB_ExtFlags(int BPB_ExtFlags){
+ set16( 40,BPB_ExtFlags );
+ }
+
+
+ public void setBPB_FSVer(int BPB_FSVer){
+ set16( 42,BPB_FSVer );
+ }
+
+
+ public void setBPB_RootClus(long BPB_RootClus){
+ set32( 44,BPB_RootClus );
+ }
+
+
+ public void setBPB_FSInfo(int BPB_FSInfo){
+ set16( 48,BPB_FSInfo );
+ }
+
+
+ public void setBPB_BkBootSec(int BPB_BkBootSec){
+ set16( 50,BPB_BkBootSec );
+ }
+
+
+ public void setBPB_Reserved(byte[] BPB_Reserved ){
+ setBytes ( 52, 12, BPB_Reserved );
+ }
+
+
+ public void setBS_DrvNum(int BS_DrvNum){
+ set8( 64,BS_DrvNum );
+ }
+
+
+ public void setBS_Reserved1(int BS_Reserved1){
+ set8 ( 65,BS_Reserved1 );
+ }
+
+
+ public void setBS_BootSig(int BS_BootSig){
+ set8( 66,BS_BootSig );
+ }
+
+
+ public void setBS_VolID(long BS_VolID){
+ set32( 67,BS_VolID );
+ }
+
+
+ public void setBS_VolLab(String BS_VolLab){
+ setString ( 71, 11, BS_VolLab );
+ }
+
+
+ public void setBS_FilSysType(String BS_FilSysType){
+ setString ( 82, 8, BS_FilSysType );
+ }
+
+ public void setBS_Identifier(byte[] ident){
+ setBytes(510,2,ident);
+ }
+
public String toString() {
StrWriter out = new StrWriter();
Modified: trunk/fs/src/fs/org/jnode/fs/jfat/FatFileSystemType.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/jfat/FatFileSystemType.java 2007-10-09 15:02:20 UTC (rev 3542)
+++ trunk/fs/src/fs/org/jnode/fs/jfat/FatFileSystemType.java 2007-10-11 09:16:06 UTC (rev 3543)
@@ -1,17 +1,38 @@
/*
*
+ * $Id$
+ *
+ * JNode.org
+ * Copyright (C) 2003-2006 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.jnode.fs.jfat;
import java.io.IOException;
import org.apache.log4j.Logger;
+import org.jnode.driver.ApiNotFoundException;
import org.jnode.driver.Device;
import org.jnode.driver.block.BlockDeviceAPI;
import org.jnode.driver.block.FSBlockDeviceAPI;
import org.jnode.fs.FileSystem;
import org.jnode.fs.FileSystemException;
import org.jnode.fs.FileSystemType;
+import org.jnode.fs.jfat.FatFormatter;
+import org.jnode.fs.jfat.FatFileSystem;
import org.jnode.partitions.PartitionTableEntry;
import org.jnode.partitions.ibm.IBMPartitionTableEntry;
import org.jnode.partitions.ibm.IBMPartitionTypes;
@@ -19,6 +40,7 @@
/**
* @author gvt
+ * @author Tango
*/
public class FatFileSystemType implements FileSystemType {
private static final Logger log =
@@ -71,6 +93,71 @@
public FileSystem format ( Device device, Object specificOptions )
throws FileSystemException {
- throw new FileSystemException ( "not implemented ... yet" );
+
+ try {
+ long numberOfSectors;
+ long offset;
+ int ClusterSize = (((Integer)specificOptions).intValue())*1024;//Converted into KB
+
+ FSBlockDeviceAPI api = (FSBlockDeviceAPI)device.getAPI(BlockDeviceAPI.class);
+ int sectorSize = api.getSectorSize();
+
+ if(sectorSize!=512){
+ log.error("This mkjfat1.0 support only the Hard Disk.Sector Size must 512 bytes.\n");
}
+
+
+ PartitionTableEntry entry = api.getPartitionTableEntry();
+
+ // if we can deduce partitiontable/fat dependencies do it otherwise
+ // guess it.
+ if (entry != null && entry instanceof IBMPartitionTableEntry) {
+ numberOfSectors = ((IBMPartitionTableEntry)entry).getNrSectors();
+ offset = ((IBMPartitionTableEntry)entry).getStartLba();
+ } else {
+ numberOfSectors = api.getLength() / sectorSize;
+ offset = 0;
}
+ /**
+ * Check the Disks Availability. low end limit - 65536 sectors
+ * I suspect that most FAT32 implementations would mount this volume just fine, but the
+ * spec says that we shouldn't do this, so we won't *
+ */
+
+ if(numberOfSectors< 65536){
+
+ log.error("This drive is too small for FAT32 - there must be at least 64K clusters\n");
+
+ }
+ /**
+ * This is a more fundamental limitation on FAT32 - the total sector count in the root dir
+ * 32bit. With a bit of creativity, FAT32 could be extended to handle at least 2^28 clusters
+ * There would need to be an extra field in the FSInfo sector, and the old sector count could
+ * be set to 0xffffffff. This is non standard though, the Windows FAT driver FASTFAT.SYS won't
+ * understand this. Perhaps a future version of FAT32 and FASTFAT will handle this.
+ *
+ */
+ if(numberOfSectors>=(Math.pow(2,32)-1) ){
+ log.error("This drive is too big for FAT32 - max 2TB supported :" +numberOfSectors);
+ }
+
+ //The FAT32 Formatter class constructor
+ FatFormatter.HDDFormatter(
+ sectorSize,
+ (int)numberOfSectors,
+ ClusterSize,
+ (int)offset,//the Hidden Sectors
+ api
+ );
+
+
+
+ return new FatFileSystem(device, false); // not readOnly !
+ } catch (IOException ioe) {
+ throw new FileSystemException("Formating problem", ioe);
+ } catch (ApiNotFoundException e) {
+ throw new FileSystemException("Formating problem", e);
+ }
+}
+
+}
Added: trunk/fs/src/fs/org/jnode/fs/jfat/FatFormatter.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/jfat/FatFormatter.java (rev 0)
+++ trunk/fs/src/fs/org/jnode/fs/jfat/FatFormatter.java 2007-10-11 09:16:06 UTC (rev 3543)
@@ -0,0 +1,368 @@
+package org.jnode.fs.jfat;
+/*
+ * $Id: FatFormatter.java 2007-07-26 +0100 (s,26 JULY 2007) Tanmoy Deb $
+ *
+ * JNode.org
+ * Copyright (C) 2003-2006 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Date;
+
+import org.apache.log4j.Logger;
+import org.jnode.driver.block.BlockDeviceAPI;
+import org.jnode.fs.jfat.BootSector;
+import org.jnode.util.LittleEndian;
+import static java.lang.Integer.toHexString;
+
+/**
+ *
+ * @author tango
+ * <p>
+ * <ul>According to the FAT32 Documents.</ul>
+ * <p>
+ * <li>FAT32 disk structure:</li>
+ * <li>Sector 0 Boot Sector</li>
+ * <li>Sector 1 FSInfo</li>
+ * <li>Sector 2 More boot code</li>
+ * <li>Sector 3 unused</li>
+ * <li>Sector 4 unused</li>
+ * <li>Sector 5 unused</li>
+ * <li>Sector 6 Backup boot sector</li>
+ * <li>Sector 7 Backup FSInfo sector</li>
+ * <li>Sector 8 Backup 'more boot code</li>
+ * <li>Reserved sectors upto the FAT at sector 32</li>
+ *
+ */
+public class FatFormatter {
+ private static final Logger log =
+ Logger.getLogger ( FatFormatter.class );
+
+ /** The Device Identifier for Floppy Device*/
+ public static final int FLOPPY_DESC = 0xf0;
+ /** The Device Identifier for Hard Disk Device*/
+ public static final int HD_DESC = 0xf8;
+ /** The Device Identifier for RAM Disk Device */
+ public static final int RAMDISK_DESC = 0xfa;
+ /** The Size of Fat.*/
+ private int FatSize;
+ /** The Number of the Fat in system.*/
+ public final int NumOfFATs=2;
+ /** The boot sector backing up.*/
+ public final int BackupBootSector=6;
+ /** The number of reserce sectors.*/
+ public final int ReservedSectorCount=32;
+ /** The Tracks Per cylinder.*/
+ private static final int NB_HEADS = 255;
+ /** The sector per track.*/
+ private static final int SECTOR_PER_TRACK = 63;
+ /** The volume label.*/
+ private static final String VOL_LABEL="NO NAME ";
+ /** The FAT version label.*/
+ private static final String FAT_LABEL="FAT32 ";
+ /** The Identifier of the Boot Sector */
+ public final byte[] BS_Identifier={(byte)0x55,(byte)0xAA};
+ /** The First 3 Bytes of the BootSector */
+ public final byte[] BS_jmpBoot={(byte)0xEB,(byte)0x5A,(byte)0x90};
+ /** This lead signature is used to validate that this is in fact an FSInfo sector.*/
+ private final int FSI_LeadSig =0x41615252;
+ /** The signature that is more localized in the sector to the location of the fields that are used.*/
+ private final int FSI_StrucSig =0x61417272;
+ /** Contains the last known free cluster count on the volume. If the value is 0xFFFFFFFF, then the free count is unknown and must be computed.*/
+ private int FSI_FreeCount=0xffffffff;
+ /** This is a hint for the FAT driver. It indicates the cluster number at which the driver should start looking for free clusters.*/
+ private final int FSI_Nxt_Free =3;//A confusion here but OK now for setting the Info at 3rd sec
+ /** This trail signature is used to validate that this is in fact an FSInfo sector*/
+ private final int FSI_TrailSig =0xaa550000;
+ /** The media ID at the reserved cluster one.*/
+ private final int ReservedSector_0=0x0ffffff8;
+ /** The End Of Chain ID at the reserved sector second.*/
+ private final int ReservedSector_1=0xffffffff;
+ /** The End of cluster chain of the root directory.*/
+ private final int ReservedSector_2=0x0fffffff;
+ /** The array for the reserved sector.*/
+ private byte[] reservedSector;
+
+ /**
+ * The Hard Disk's formatting logic implementation by JFAT.
+ * This Version only support to the Hard Disks.
+ *
+ * @throws IOException
+ */
+ public static FatFormatter HDDFormatter(int bps,
+ int nbTotalSectors,
+ int fatSize,
+ int hiddenSectors,
+ BlockDeviceAPI api
+ ) throws IOException{
+
+ return new FatFormatter(
+ HD_DESC,
+ bps,
+ nbTotalSectors,
+ SECTOR_PER_TRACK,
+ NB_HEADS,
+ fatSize,
+ hiddenSectors,
+ api
+ );
+ }
+
+ /**
+ * The Constructor for the HDD devices in the JNode system.
+ * @throws IOException
+ *
+ */
+ public FatFormatter(
+ int mediumDescriptor,
+ int bps,
+ int nbTotalSectors,
+ int sectorsPerTrack,
+ int nbHeads,
+ int ClusterSize,
+ int hiddenSectors,
+ BlockDeviceAPI api
+ ) throws IOException{
+
+ FatFsInfo fsInfo=new FatFsInfo(bps);
+ BootSector bs=new BootSector(bps);
+ api.flush();
+ int DiskSize=getDiskSize(nbTotalSectors, bps);
+ int SectorPerCluster=get_spc(ClusterSize,bps);
+ int UserAreaSize=getUserAreaSize(nbTotalSectors,ReservedSectorCount,NumOfFATs,ClusterSize );
+ this.FatSize=getFATSizeSectors(nbTotalSectors,ReservedSectorCount,SectorPerCluster,NumOfFATs,bps);
+
+ //fill out the boot sector and fs info
+ bs.setBS_JmpBoot(this.BS_jmpBoot);
+ bs.setBS_OemName("MSWIN4.1");
+ bs.setBPB_BytesPerSector(bps);
+ bs.setBPB_SecPerCluster(SectorPerCluster);//look FATformat source
+ bs.setBPB_RsvSecCount(this.ReservedSectorCount);
+ bs.setBPB_NoFATs(NumOfFATs);
+ bs.setBPB_RootEntCnt(0);
+ bs.setBPB_TotSec16(0);
+ bs.setBPB_MediumDescriptor(mediumDescriptor);
+ bs.setBPB_FATSz16(0);
+ bs.setBPB_SecPerTrk(SECTOR_PER_TRACK);
+ bs.setBPB_NumHeads(NB_HEADS);
+ bs.setBPB_HiddSec(hiddenSectors);
+ bs.setBPB_TotSec32(nbTotalSectors);
+
+ //For FAT16/32 only
+ bs.setBPB_FATSz32(FatSize);
+ bs.setBPB_ExtFlags(0);
+ bs.setBPB_FSVer(0);
+ bs.setBPB_RootClus(2);
+ bs.setBPB_FSInfo(1);
+ bs.setBPB_BkBootSec(BackupBootSector);
+
+ bs.setBS_DrvNum(0x80);
+ bs.setBS_Reserved1(0);
+ bs.setBS_BootSig(0x29);//0x29 if the next three bits are OK.
+
+ int VolumeID=getDriveSerialNumber();
+ bs.setBS_VolID(VolumeID);
+ bs.setBS_VolLab(VOL_LABEL);
+ bs.setBS_FilSysType(FAT_LABEL);
+ bs.setBS_Identifier(BS_Identifier);
+
+ //Keeping the FSInfos
+ fsInfo.setFsInfo_LeadSig(FSI_LeadSig);
+ fsInfo.setFsInfo_Reserved1();
+ fsInfo.setFsInfo_StrucSig(FSI_StrucSig);
+ FSI_FreeCount=(UserAreaSize/SectorPerCluster)-1;
+ fsInfo.setFsInfo_FreeCount(FSI_FreeCount);
+ fsInfo.setFsInfo_NextFree(FSI_Nxt_Free);
+ fsInfo.setReserve2();
+ fsInfo.setFsInfo_TrailSig(FSI_TrailSig);
+
+ /**
+ * TODO:This portion need modofication for the Disk Size Handlings
+ * Not So mandatory now .WIll look into it.
+ * Work out the Cluster Count
+ */
+ long FatNeeded=UserAreaSize/SectorPerCluster;
+ /**
+ * check for a cluster count of >2^28, since the upper 4 bits of the cluster values in
+ * the FAT are reserved
+ */
+ if ( FatNeeded > Math.pow(2,28) ){
+ log.error( "This drive has more than 2^28 clusters, try to specify a larger cluster size\n" );
+ }
+
+ /**
+ * Once zero_sectors has run, any data on the drive is basically lost....
+ * First zero out ReservedSect + FatSize * NumFats + SectorsPerCluster
+ *
+ */
+ int SystemAreaSize = (ReservedSectorCount+(NumOfFATs*FatSize) + SectorPerCluster);
+ log.info("Clearing out "+SystemAreaSize+" sectors for Reserved sectors, fats and root cluster...\n" );
+
+ /** Disk Freeing */
+ try {
+ setQuickSectorFree(SystemAreaSize,api);
+ }catch (IOException e1) {
+ log.info("Error ocured during Disk Free.");
+ }
+ /**calling the Format method*/
+ try {
+ log.info("Initialising reserved sectors and FATs....");
+ format(api,bs,fsInfo,SectorPerCluster,nbTotalSectors);
+ } catch (IOException e) {
+ log.error("Problems in Disk Formatting.");
+ }
+
+ /**
+ *The mkjfat informations group
+ */
+ int DiskSizeGB=DiskSize/(1000*1000*1000);
+ log.info("Size(Bytes): "+DiskSize+"\tSize(GB): "+DiskSizeGB +"\tSectors :"+nbTotalSectors);
+ log.info("Volume ID is :"+toHexString(VolumeID>>16)+":"+toHexString(VolumeID&0xffff));
+ log.info(bps+" Bytes Per Sector , Cluster Size "+ClusterSize +" bytes.");
+ log.info(ReservedSectorCount+ " Reserved Sectors ,"+(FatSize)+" Sectors Per FAT ,"+NumOfFATs+" FATs.");
+ log.info("Total Clusters :"+(UserAreaSize/SectorPerCluster));
+ log.info("Free Clusters: "+FSI_FreeCount);
+
+ //Flushing the DeviceAPI
+ api.flush();
+ }
+ /**
+ * Format the given device, according to my settings
+ * @param api
+ * @throws IOException
+ */
+ public void format(BlockDeviceAPI api,BootSector bs,FatFsInfo fsInfo,int sectorPerCluster,int nbTotalSectors)throws IOException {
+ log.info("The Formatting...\n");
+ /** Now we should write the boot sector and fsinfo twice, once at 0 and once at the backup boot sect position */
+ for(int i=0;i<2;i++){
+ int SectorStart=(i==0)? 0 : (BackupBootSector*512);
+ bs.write(api,(long)SectorStart);//Write the BootSector
+ fsInfo.write(api,(long)SectorStart+512);
+ }
+ /** Write the first fat sector in the right places */
+ for(int i=0;i<NumOfFATs;i++){
+ int SectorStart=(ReservedSectorCount+(i*FatSize))*512;
+ this.reservedSector=new byte[12];
+ LittleEndian.setInt32(this.reservedSector, 0,ReservedSector_0);
+ LittleEndian.setInt32(this.reservedSector, 4,ReservedSector_1);
+ LittleEndian.setInt32(this.reservedSector, 8,ReservedSector_2);
+ api.write(SectorStart, ByteBuffer.wrap(this.reservedSector));
+
+ }
+
+ }
+ /**
+ * The Method for Disk Free Operations.
+ * @param systemAreaSize
+ * @param api
+ * @throws IOException
+ */
+ private void setQuickSectorFree(int systemAreaSize,BlockDeviceAPI api) throws IOException {
+ byte[] reserveArray=new byte[512];
+ for(int i=0;i<systemAreaSize;i++){
+ api.write(i*512,ByteBuffer.wrap(reserveArray));
+ }
+ }
+ /**
+ * This is for Quick Disk Free Operation
+ * TODO:Need to test with it.
+ * @param TotalSectors
+ * @param api
+ * @throws IOException
+ */
+ private void setQuickDiskFree(int TotalSectors,BlockDeviceAPI api) throws IOException{
+ //TODO:For Total Disk Formatting
+ }
+
+ /**
+ * @param ClusterSizeKB
+ * @param BytesPerSector
+ * @return
+ */
+ private static int get_spc(int ClusterSizeKB, int BytesPerSector) {
+ System.out.println("The Clusters Per KB: "+(( ClusterSizeKB ) / BytesPerSector));
+ return (( ClusterSizeKB ) / BytesPerSector);
+ }
+
+ /**
+ * The method for getting the Serial Number of the Drive.
+ */
+ public int getDriveSerialNumber(){
+ Date date=new Date();
+ int year=FatUtils.getYear(date.getTime());
+ int month=FatUtils.getMonth((int)date.getTime());
+ int day=FatUtils.getDay((int)date.getTime());
+ int hour=FatUtils.getHours((int)date.getTime());
+ int minute=FatUtils.getMinutes((int)date.getTime());
+ int second=FatUtils.getSeconds((int)date.getTime());
+ int milliSecond=(int)FatUtils.getMilliSeconds(date.getTime());
+
+ int low=day+(month<<8);
+ int tmp=(milliSecond/10)+(second<<8);
+ low+=tmp;
+
+ int high=minute+(hour<<8);
+ high+=year;
+
+ int serialNo=low+(high<<16);
+ return serialNo;
+ }
+ /**
+ * Get the FAT size sectors.
+ */
+ public int getFATSizeSectors(int TotalSectorNumber,
+ int ReservedSecCnt,
+ int SecPerClus,
+ int NumFATs,
+ int BytesPerSect){
+ long Numerator,Denominator;
+ long FATElementSize=4;
+ long FATsz;
+
+ Numerator = FATElementSize * ( TotalSectorNumber - ReservedSecCnt );
+ Denominator = ( SecPerClus * BytesPerSect ) + ( FATElementSize * NumFATs );
+
+ FATsz = Numerator / Denominator;
+ // round up
+ FATsz += 1;
+
+ return((int)FATsz);
+ }
+ /**
+ * @param TotalSectors
+ * @param ReservedSectorCount
+ * @param NumFATs
+ * @param FATSize
+ * @return
+ */
+ private int getUserAreaSize(int TotalSectors,int ReservedSectorCount,int NumFATs,int FATSize){
+ int UserAreaSize=TotalSectors - ReservedSectorCount - (NumFATs*FATSize);
+ return (UserAreaSize);
+ }
+ /**
+ * The Total Disk Size
+ * @param TotalSectors
+ * @param sectorSize
+ * @return
+ */
+ private int getDiskSize(int TotalSectors,int sectorSize){
+ return (TotalSectors*sectorSize);
+ }
+
+
+}//end
Added: trunk/fs/src/fs/org/jnode/fs/jfat/FatFsInfo.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/jfat/FatFsInfo.java (rev 0)
+++ trunk/fs/src/fs/org/jnode/fs/jfat/FatFsInfo.java 2007-10-11 09:16:06 UTC (rev 3543)
@@ -0,0 +1,160 @@
+package org.jnode.fs.jfat;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import org.jnode.driver.block.BlockDeviceAPI;
+import org.jnode.util.LittleEndian;
+/**
+ *
+ * @author tango
+ * <p>
+ * <ul>According to the FAT32 Docs :</ul>
+ * <p>
+ * <li>FAT32 disk structure</li>
+ * <li>Sector 0 Boot Sector</li>
+ * <li>Sector 1 FSInfo</li>
+ * <li>Sector 2 More boot code</li>
+ * <li>Sector 3 unused</li>
+ * <li>Sector 4 unused</li>
+ * <li>Sector 5 unused</li>
+ * <li>Sector 6 Backup boot sector</li>
+ * <li>Sector 7 Backup FSInfo sector</li>
+ * <li>Sector 8 Backup 'more boot code'</li>
+ * <li>Reserved sectors upto the FAT at sector 32</li>
+ *
+ */
+
+public class FatFsInfo {
+
+ private byte[] sector;
+
+
+ public FatFsInfo(int size){
+ sector = new byte[size];
+ }
+
+ public void write(BlockDeviceAPI device,long offset)throws IOException{
+ device.write(offset, ByteBuffer.wrap(sector));
+ }
+
+
+
+ protected int get8 ( int offset ) {
+ return LittleEndian.getUInt8 ( sector, offset );
+ }
+
+
+ protected void set8 ( int offset, int value ) {
+ LittleEndian.setInt8 ( sector, offset, value );
+ }
+
+
+ protected int get16 ( int offset ) {
+ return LittleEndian.getUInt16 ( sector, offset );
+ }
+
+
+ protected void set16 ( int offset, int value ) {
+ LittleEndian.setInt16 ( sector, offset, value );
+
+ }
+
+
+ protected long get32 ( int offset ) {
+ return LittleEndian.getUInt32 ( sector, offset );
+ }
+
+
+ protected void set32 ( int offset, long value ) {
+ LittleEndian.setInt32 ( sector, offset, (int)value );
+
+ }
+
+
+ protected String getString ( int offset, int len ) {
+ StringBuilder b = new StringBuilder ( len );
+ for ( int i = 0; i < len; i++ ) {
+ int v = sector[offset+i];
+ b.append ( (char)v );
+ }
+ return b.toString();
+ }
+
+
+ protected void setString ( int offset, int len, String value ) {
+ for ( int i = 0; i < len; i++ ) {
+ char ch;
+ if ( i < value.length() )
+ ch = value.charAt ( i );
+ else
+ ch = (char)0;
+ LittleEndian.setInt8 ( sector, offset + i, ch );
+ }
+
+ }
+
+
+ protected byte[] getBytes ( int offset, int len ) {
+ byte[] v = new byte[len];
+
+ System.arraycopy ( sector, offset, v, 0, len );
+
+ return v;
+ }
+
+
+ protected void setBytes ( int offset, int len, byte[] value ) {
+ System.arraycopy ( value, 0, sector, offset, len );
+
+ }
+
+
+
+ /**
+ * The Lead signature that used to validate the signature that it s an FS_Info Sector.
+ * @param FSI_LeadSig
+ */
+ public void setFsInfo_LeadSig(long FSI_LeadSig){
+ set32(0,FSI_LeadSig);
+ }
+ /**
+ * This field is currently kept for the expansion.FAT32 Formatter code initialize it all to zero.Bytes in this
+ * field is not currently being used.
+ */
+ public void setFsInfo_Reserved1(){
+ byte[] reserve1=new byte[480];
+ setBytes(4,480,reserve1);
+ }
+ /**
+ * Another signature that is more localized in the sector of the fields that are used.
+ * @param FSI_StrucSig
+ */
+ public void setFsInfo_StrucSig(int FSI_StrucSig){
+ set32(484,FSI_StrucSig);
+ }
+ /**
+ * Contains the last known Free cluster count on the volume. If the value is 0xFFFFFFF,
+ * then the Free count is unknown and must be Computed.Any other value can be used, but
+ * is not necessarily correct.It Should be range checked atleast make sure it is >=Volume Cluseter Count.
+ *
+ * @param FSI_FreeCount
+ */
+ public void setFsInfo_FreeCount(int FSI_FreeCount){
+ set32(488,FSI_FreeCount);
+ }
+
+ public void setFsInfo_NextFree(int FSI_Nxt_Free){
+ set32(492,FSI_Nxt_Free);
+ }
+
+ public void setReserve2(){
+ byte[] reserve2=new byte[12];
+ setBytes(496,12,reserve2);
+ }
+
+ public void setFsInfo_TrailSig(int FSI_TrailSig){
+ set32(508,FSI_TrailSig);
+ }
+
+}
Modified: trunk/fs/src/fs/org/jnode/fs/jfat/FatUtils.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/jfat/FatUtils.java 2007-10-09 15:02:20 UTC (rev 3542)
+++ trunk/fs/src/fs/org/jnode/fs/jfat/FatUtils.java 2007-10-11 09:16:06 UTC (rev 3543)
@@ -10,10 +10,13 @@
import java.util.Calendar;
import java.text.SimpleDateFormat;
+import org.jnode.driver.block.BlockDeviceAPI;
+import org.jnode.util.LittleEndian;
import org.jnode.util.NumberUtils;
/**
* @author gvt
+ * @author Tango
*/
public class FatUtils {
private static final SimpleDateFormat time =
@@ -189,6 +192,54 @@
lname = lname.replaceAll ( "\\.*$", "" );
return lname;
}
+
+ public static int getMonth(long time){
+ Calendar cal = Calendar.getInstance();
+ cal.setTimeInMillis(time);
+ return cal.get(Calendar.MONTH);
}
+ public static int getHours(long time)
+ {
+ Calendar cal = Calendar.getInstance();
+ cal.setTimeInMillis(time);
+ return cal.get(Calendar.HOUR_OF_DAY);
+ }
+ public static int getDay(long time)
+ {
+ Calendar cal = Calendar.getInstance();
+ cal.setTimeInMillis(time);
+ // For Calendar, Sunday is 1. For Date, Sunday is 0.
+ return cal.get(Calendar.DAY_OF_WEEK) - 1;
+ }
+ public static int getYear(long time)
+ {
+ Calendar cal = Calendar.getInstance();
+ cal.setTimeInMillis(time);
+ return cal.get(Calendar.YEAR) - 1980;
+ }
+ public static int getMinutes(long time)
+ {
+ Calendar cal = Calendar.getInstance();
+ cal.setTimeInMillis(time);
+ return cal.get(Calendar.MINUTE);
+ }
+ public static int getSeconds(long time)
+ {
+ Calendar cal = Calendar.getInstance();
+ cal.setTimeInMillis(time);
+ return cal.get(Calendar.SECOND);
+ }
+
+ public static long getMilliSeconds(long time){
+
+ Calendar cal = Calendar.getInstance();
+ return cal.getTimeInMillis();
+ }
+
+
+
+}
+
+
Added: trunk/fs/src/fs/org/jnode/fs/jfat/command/FatFormatCommand.java
===================================================================
--- trunk/fs/src/fs/org/jnode/fs/jfat/command/FatFormatCommand.java (rev 0)
+++ trunk/fs/src/fs/org/jnode/fs/jfat/command/FatFormatCommand.java 2007-10-11 09:16:06 UTC (rev 3543)
@@ -0,0 +1,148 @@
+package org.jnode.fs.jfat.command;
+
+/*
+ * $Id: FatFormatCommand.java 2007-07-27 +0100 (s,27 JULY 2007) Tanmoy Deb $
+ *
+ * JNode.org
+ * Copyright (C) 2003-2006 JNode.org
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; If not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+import java.io.InputStream;
+import java.io.PrintStream;
+import javax.naming.NameNotFoundException;
+
+import org.apache.log4j.Logger;
+import org.jnode.driver.Device;
+import org.jnode.driver.DeviceManager;
+import org.jnode.driver.DeviceNotFoundException;
+import org.jnode.driver.DriverException;
+import org.jnode.driver.block.FSBlockDeviceAPI;
+import org.jnode.fs.FileSystemException;
+import org.jnode.fs.FileSystemType;
+import org.jnode.fs.jfat.FatFileSystemType;
+import org.jnode.fs.service.FileSystemService;
+import org.jnode.naming.InitialNaming;
+import org.jnode.shell.Command;
+import org.jnode.shell.CommandLine;
+import org.jnode.shell.help.argument.DeviceArgument;
+import org.jnode.shell.help.Help;
+import org.jnode.shell.help.argument.OptionArgument;
+import org.jnode.shell.help.Parameter;
+import org.jnode.shell.help.ParsedArguments;
+import org.jnode.shell.help.Syntax;
+
+/**
+ * @author Tango
+ * <p>
+ * The FAT32 formating command.
+ *
+ */
+public class FatFormatCommand implements Command{
+ private static final Logger log =
+ Logger.getLogger ( FatFormatCommand.class );
+
+ static final DeviceArgument ARG_DEVICE = new DeviceArgument("device-id","the device to print informations about");
+
+ static final Parameter PARAM_DEVICE = new Parameter(ARG_DEVICE,Parameter.MANDATORY);
+
+ static final OptionArgument TYPE = new OptionArgument("action","Type parameter",
+ new OptionArgument.Option[] {
+ new OptionArgument.Option("-c","Specify Sector Per Cluster Value") });
+ static final Parameter PARAM_TYPE = new Parameter(TYPE, Parameter.OPTIONAL);
+
+ static final OptionArgument BS_VAL = new OptionArgument("SectorPerClusterValue","Setting The Cluster Size",
+ new OptionArgument.Option[] {
+ new OptionArgument.Option("1", "1Kb"),
+ new OptionArgument.Option("2", "2Kb"),
+ new OptionArgument.Option("4", "4Kb"),
+ new OptionArgument.Option("8", "8Kb"),
+ new OptionArgument.Option("16", "16Kb"),
+ new OptionArgument.Option("32", "32Kb"),
+ new OptionArgument.Option("64", "64Kb")});
+
+ static final Parameter PARAM_BS_VAL = new Parameter(BS_VAL,Parameter.OPTIONAL);
+
+ public static Help.Info HELP_INFO = new Help.Info("mkjfat",
+ new Syntax[] {
+ new Syntax("Format a block device with a specified type.Enter the Cluster Size as 1 for 1KB. ",
+ new Parameter[] { PARAM_TYPE, PARAM_BS_VAL, PARAM_DEVICE}) });
+
+
+
+
+ public static void main(String[] args) throws Exception{
+ new FatFormatCommand().execute(new CommandLine(args), System.in, System.out, System.err);
+ }
+
+ public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err) throws Exception {
+ try {
+ System.out.println("mkjfat:JFAT Formatter. Version :1.0");
+ ParsedArguments cmdLine = HELP_INFO.parse(commandLine.toStringArray());
+
+ String device = ARG_DEVICE.getValue(cmdLine);
+ Integer bsize;
+ try {
+ bsize = Integer.valueOf(BS_VAL.getValue(cmdLine));
+ } catch (NumberFormatException nfe) {
+ bsize=4;//set to the default cluster size
+ log.error("The Cluster of "+bsize+" size not available.");
+ }
+
+ String fsTypeName;
+ Object params;
+
+ fsTypeName = FatFileSystemType.NAME;
+ params = new Integer(bsize);
+
+ DeviceManager dm;
+ dm = InitialNaming.lookup(DeviceManager.NAME);
+
+ Device dev = dm.getDevice(device);
+ if(!(dev.getDriver() instanceof FSBlockDeviceAPI)){
+ throw new FileSystemException(
+ "Unsupported device by format command");
+ }
+ FileSystemService fileSystemService = InitialNaming
+ .lookup(FileSystemService.NAME);
+ FileSystemType type = fileSystemService
+ .getFileSystemTypeForNameSystemTypes(fsTypeName);
+
+ type.format(dev, params);
+
+ // restart the device
+ dm.stop(dev);
+ dm.start(dev);
+ log.info("Done.");//I will keep it
+ } catch (NameNotFoundException e) {
+ log.error("The JFAT name not found.");
+ } catch (DeviceNotFoundException e) {
+ log.error("The Device not found.");
+ } catch (DriverException e) {
+ log.error("The Driver Exception.");
+ } catch (FileSystemException e) {
+ log.error("The File System Exception");
+
+ }
+ }
+
+
+
+
+
+}
+
+
+
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|