📄 fileblockdevice.java
字号:
/* JPC: A x86 PC Hardware Emulator for a pure Java Virtual Machine Release Version 2.0 A project from the Physics Dept, The University of Oxford Copyright (C) 2007 Isis Innovation Limited This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Details (including contact information) can be found at: www.physics.ox.ac.uk/jpc*/package org.jpc.support;import java.io.*;//import java.nio.*; // This import is only needed for the ByteBuffer & ByteOrder stuffimport java.util.regex.*; //this is used to extract filenames for directory entriespublic class FileBlockDevice implements BlockDevice{ private File source; private RandomAccessFile raf; private boolean locked; private byte[] MBR; private byte[] header; private byte[] header2; private byte[] start; // first 33 sectors of drive private byte[] FAT; private byte[] rootdir; private byte[] empty; // An empty sector private int cylinders; private int heads; private long fileSizeSectors; //Size of file in sectors private int rootSize; // Size in sectors of root directory private int rootSizeClusters; private int startingHead; //0-255 private int startingSector; //1 - 63 private int startingCylinder; //0-1023 private int endingHead; //0-255 private int endingSector; //1 - 63 private int endingCylinder; //0-1023 private long relativeSectors; // counts from 0 private int totalSectors; private int bytesPerSector; //normally 512 private int sectorsPerCluster; private int reservedSectors; private int sectorsPerTrack; private int hiddenSectors; private int sectorsPerFAT; private long rootStartCluster; private int fsinfoSector; private int backupBootSector; private long freeClusters; private long lastAllocatedCluster; private static Pattern pattern; //this and the next are the regular expression matching bits private static Matcher matcher; private int fileSizeClusters; public FileBlockDevice() { } public void configure(String specs) throws IOException { File source = new File(specs); this.source = source; raf = new RandomAccessFile(source, "rw"); locked = false; //set Drive Geometry if (raf.length()/512 < 2147483646*512) fileSizeSectors=raf.length()/512+1; sectorsPerTrack = 63; heads = 255; if (fileSizeSectors <67*sectorsPerTrack*heads) cylinders=67; // Size of drive in sectors else cylinders = 1000; //(int) fileSizeSectors/sectorsPerTrack/heads+1; //set specifications for disk which go in partition table startingHead = 1; startingSector = 1; startingCylinder = 0; endingHead = heads; endingSector = sectorsPerTrack; endingCylinder = cylinders; relativeSectors = 63; totalSectors = cylinders*sectorsPerTrack*heads-1;// -1 for the MBR which isn't counted //set specifications for disk which go in partition boot sector bytesPerSector = 512; sectorsPerCluster = 8; reservedSectors = 32; hiddenSectors=63; //what's not in the partition itself sectorsPerFAT = totalSectors/sectorsPerCluster*4/bytesPerSector+1; //add 1 to ensure rounding up rootStartCluster = 2; fsinfoSector = 1; backupBootSector = 6; System.out.println("sectorsPerFAT = " + sectorsPerFAT); //set specifications for disk which go in FSINFO Sector //freeClusters=100; not set yet but doesn't matter lastAllocatedCluster = 3; FAT = new byte[sectorsPerFAT*bytesPerSector]; rootSize=1; //set up the master boot record with partition table MBR =new byte[512]; int pos = 0; //put in executable code to first 446 bytes byte[] MBRcode = { (byte)0x00 ,(byte)0x33 ,(byte)0xC0 ,(byte)0x8E ,(byte)0xD0 ,(byte)0xBC ,(byte)0x00 ,(byte)0x7C, (byte)0x8B ,(byte)0xF4 ,(byte)0x50 ,(byte)0x07 ,(byte)0x50 ,(byte)0x1F ,(byte)0xFB ,(byte)0xFC, (byte)0xBF ,(byte)0x00 ,(byte)0x06 ,(byte)0xB9 ,(byte)0x00 ,(byte)0x01 ,(byte)0xF2 ,(byte)0xA5, (byte)0xEA ,(byte)0x1D ,(byte)0x06 ,(byte)0x00 ,(byte)0x00 ,(byte)0xBE ,(byte)0xBE ,(byte)0x07, (byte)0xB3 ,(byte)0x04 ,(byte)0x80 ,(byte)0x3C ,(byte)0x80 ,(byte)0x74 ,(byte)0x0E ,(byte)0x80, (byte)0x3C ,(byte)0x00 ,(byte)0x75 ,(byte)0x1C ,(byte)0x83 ,(byte)0xC6 ,(byte)0x10 ,(byte)0xFE, (byte)0xCB ,(byte)0x75 ,(byte)0xEF ,(byte)0xCD ,(byte)0x18 ,(byte)0x8B ,(byte)0x14 ,(byte)0x8B, (byte)0x4C ,(byte)0x02 ,(byte)0x8B ,(byte)0xEE ,(byte)0x83 ,(byte)0xC6 ,(byte)0x10 ,(byte)0xFE, (byte)0xCB ,(byte)0x74 ,(byte)0x1A ,(byte)0x80 ,(byte)0x3C ,(byte)0x00 ,(byte)0x74 ,(byte)0xF4, (byte)0xBE ,(byte)0x8B ,(byte)0x06 ,(byte)0xAC ,(byte)0x3C ,(byte)0x00 ,(byte)0x74 ,(byte)0x0B, (byte)0x56 ,(byte)0xBB ,(byte)0x07 ,(byte)0x00 ,(byte)0xB4 ,(byte)0x0E ,(byte)0xCD ,(byte)0x10, (byte)0x5E ,(byte)0xEB ,(byte)0xF0 ,(byte)0xEB ,(byte)0xFE ,(byte)0xBF ,(byte)0x05 ,(byte)0x00, (byte)0xBB ,(byte)0x00 ,(byte)0x7C ,(byte)0xB8 ,(byte)0x01 ,(byte)0x02 ,(byte)0x57 ,(byte)0xCD, (byte)0x13 ,(byte)0x5F ,(byte)0x73 ,(byte)0x0C ,(byte)0x33 ,(byte)0xC0 ,(byte)0xCD ,(byte)0x13, (byte)0x4F ,(byte)0x75 ,(byte)0xED ,(byte)0xBE ,(byte)0xA3 ,(byte)0x06 ,(byte)0xEB ,(byte)0xD3, (byte)0xBE ,(byte)0xC2 ,(byte)0x06 ,(byte)0xBF ,(byte)0xFE ,(byte)0x7D ,(byte)0x81 ,(byte)0x3D, (byte)0x55 ,(byte)0xAA ,(byte)0x75 ,(byte)0xC7 ,(byte)0x8B ,(byte)0xF5 ,(byte)0xEA ,(byte)0x00, (byte)0x7C ,(byte)0x00 ,(byte)0x00 ,(byte)0x49 ,(byte)0x6E ,(byte)0x76 ,(byte)0x61 ,(byte)0x6C, (byte)0x69 ,(byte)0x64 ,(byte)0x20 ,(byte)0x70 ,(byte)0x61 ,(byte)0x72 ,(byte)0x74 ,(byte)0x69, (byte)0x74 ,(byte)0x69 ,(byte)0x6F ,(byte)0x6E ,(byte)0x20 ,(byte)0x74 ,(byte)0x61 ,(byte)0x62, (byte)0x6C ,(byte)0x65 ,(byte)0x00 ,(byte)0x45 ,(byte)0x72 ,(byte)0x72 ,(byte)0x6F ,(byte)0x72, (byte)0x20 ,(byte)0x6C ,(byte)0x6F ,(byte)0x61 ,(byte)0x64 ,(byte)0x69 ,(byte)0x6E ,(byte)0x67, (byte)0x20 ,(byte)0x6F ,(byte)0x70 ,(byte)0x65 ,(byte)0x72 ,(byte)0x61 ,(byte)0x74 ,(byte)0x69, (byte)0x6E ,(byte)0x67 ,(byte)0x20 ,(byte)0x73 ,(byte)0x79 ,(byte)0x73 ,(byte)0x74 ,(byte)0x65, (byte)0x6D ,(byte)0x00 ,(byte)0x4D ,(byte)0x69 ,(byte)0x73 ,(byte)0x73 ,(byte)0x69 ,(byte)0x6E, (byte)0x67 ,(byte)0x20 ,(byte)0xF6 ,(byte)0x70 ,(byte)0x65 ,(byte)0x72 ,(byte)0x61 ,(byte)0x74, (byte)0x69 ,(byte)0x6E ,(byte)0x67 ,(byte)0x20 ,(byte)0x73 ,(byte)0x79 ,(byte)0x73 ,(byte)0x74, (byte)0x65 ,(byte)0x6D ,(byte)0x00 ,(byte)0x00 ,(byte)0x80 ,(byte)0x45 ,(byte)0x14 ,(byte)0x15, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00 ,(byte)0x00, (byte)0xFD ,(byte)0x4E ,(byte)0xF2 ,(byte)0x14 ,(byte)0x00 ,(byte)0x00 }; System.arraycopy(MBRcode,0,MBR,0,446); pos=446; //partition table MBR[pos++]=(byte)0x80;// 80 means system partition, 00 means do not use for booting System.arraycopy(toByteArray(startingHead,1),0,MBR,pos++,1); // starting Sector (bits 0-5) and high starting cylinder bits System.arraycopy(toByteArray(startingSector | ((startingCylinder >>> 2) & (64+128)),1),0,MBR,pos++,1); //low 8 bits of starting Cylinder System.arraycopy(toByteArray(startingCylinder & 0xFF,1),0,MBR,pos++,1); MBR[pos++]=(byte)0x0C;//System ID System.arraycopy(toByteArray(endingHead,1),0,MBR,pos++,1); // ending Sector (bits 0-5) and high ending cylinder bits System.arraycopy(toByteArray(endingSector | ((endingCylinder >>> 2) & (64+128)),1),0,MBR,pos++,1); //low 8 bits of ending Cylinder System.arraycopy(toByteArray(endingCylinder & 0xFF,1),0,MBR,pos++,1); //relative sectors System.arraycopy(toByteArray(relativeSectors,4),0,MBR,pos++,4); pos=pos+3; // N.B. remaining sectors on first track after MBR are empty //total sectors System.arraycopy(toByteArray(totalSectors,4),0,MBR,pos++,4); pos=pos+3; //fill 3 empty partition entries for(int i=0;i<16*3;i++) MBR[pos++]=0x00; MBR[0x1FE]=(byte)0x55;// end of sector marker MBR[0x1FF]=(byte)0xAA; //****************************************************************** //set up the partition boot sector header = new byte[512]; pos = 0; // write header data into this array - don't see www.ntfs.com - it is wrong! header[pos++] = (byte) 0xEB; header[pos++] = (byte) 0x3C; header[pos++] = (byte) 0x90; String myOEM = "JPCFS1.0"; System.arraycopy(myOEM.getBytes(), 0, header, 3, 8); pos += 8; //BIOS Parameter block System.arraycopy(toByteArray(bytesPerSector,2),0,header,pos++,2); pos++; System.arraycopy(toByteArray(sectorsPerCluster,1),0,header,pos++,1); System.arraycopy(toByteArray(reservedSectors,2),0,header,pos++,2); pos++; header[pos++] = (byte) 0x02;//number of copies of FAT byte offset 0x10
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -