Mac has no mkfs.vfat
2014-08-17
I have to package my kernel into a fat partition and load it up using grub, but mac doesn’t has a mkfs.vfat and “mount loopback” command. So I am creating a small programing using https://github.com/waldheinz/fat32-lib/ to manipulate the fat image by myself. So far these two small program can read & write a fat image:
Read Fat32:
package peter; import java.io.File; import java.nio.ByteBuffer; import java.util.Iterator; import de.waldheinz.fs.FsDirectory; import de.waldheinz.fs.FsDirectoryEntry; import de.waldheinz.fs.FsFile; import de.waldheinz.fs.fat.BootSector; import de.waldheinz.fs.fat.FatFileSystem; import de.waldheinz.fs.util.FileDisk; public class Fat32Lib_ReadTest { public static void main(String[] args) { try { File file = new File("peter.img"); if (!file.exists()) { System.out.println(file.getName() + " not exist"); } FileDisk rd = new FileDisk(file, false); FatFileSystem fatFs = FatFileSystem.read(rd, true); BootSector bs = fatFs.getBootSector(); System.out.println(bs.getOemName()); FsDirectory fatRootDir = fatFs.getRoot(); Iterator<FsDirectoryEntry> i = fatRootDir.iterator(); while (i.hasNext()) { final FsDirectoryEntry e = i.next(); System.out.println(" - " + e); FsDirectory dir = e.getDirectory(); Iterator<FsDirectoryEntry> i2 = dir.iterator(); while (i2.hasNext()) { final FsDirectoryEntry e2 = i2.next(); System.out.println(" - " + e2); if (e2.isFile()) { FsFile f = e2.getFile(); System.out.println(" File = " + e2.getName() + ", " + f.getLength()); ByteBuffer buff = ByteBuffer.allocate((int) f.getLength()); f.read(0, buff); System.out.println(new String(buff.array(), "utf8")); } } } } catch (Exception ex) { ex.printStackTrace(); } System.out.println("done"); } }
Write to Fat32
package peter; import java.io.File; import java.nio.ByteBuffer; import de.waldheinz.fs.FsDirectoryEntry; import de.waldheinz.fs.FsFile; import de.waldheinz.fs.fat.FatFileSystem; import de.waldheinz.fs.fat.FatLfnDirectory; import de.waldheinz.fs.fat.FatLfnDirectoryEntry; import de.waldheinz.fs.fat.FatType; import de.waldheinz.fs.fat.SuperFloppyFormatter; import de.waldheinz.fs.util.FileDisk; public class Fat32Lib_WriteTest { public static void main(String[] args) { try { File file = new File("peter.img"); if (!file.delete()) { System.out.println("unable to delete " + file.getName()); } FileDisk dev = FileDisk.create(file, 40 * 1024 * 1024); SuperFloppyFormatter sf = SuperFloppyFormatter.get(dev); sf.setOemName("Peter32"); sf.setVolumeLabel("Peter Fat32"); sf.setFatType(FatType.FAT32); sf.format(); FatFileSystem fs = new FatFileSystem(dev, false); final FatLfnDirectory rootDir = (FatLfnDirectory) fs.getRoot(); FatLfnDirectoryEntry entry = rootDir.addDirectory("Directory"); for (int i = 0; i < 10; i++) { final FsDirectoryEntry e = entry.getDirectory().addFile("file_" + i); final FsFile fsFile = e.getFile(); byte[] nullBytes = "Peter 你好 UTF8\n".getBytes(); ByteBuffer buff = ByteBuffer.wrap(nullBytes); buff.rewind(); fsFile.write(0, buff); } fs.flush(); fs.close(); } catch (Exception ex) { ex.printStackTrace(); } System.out.println("done"); } }