Advertisement
Guest User

Untitled

a guest
Aug 26th, 2015
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. local fs = require "filesystem"
  2. local args = {...}
  3.  
  4. local dev = args[1] or error("Must specify a file")
  5.  
  6. local device = fs.open(dev, "wb")
  7.  
  8. local sectors = math.floor(device:seek("end", 0)/512)
  9. print(sectors .. " sectors")
  10. device:seek("set", 0)
  11.  
  12. local fat12 = sectors/4 < 4085
  13. local fatSize = math.ceil(sectors*(fat12 and 1.5 or 2)/2048)
  14.  
  15. local startseq = "\xEB\x3C\x90ocdosfs\0"
  16. local bytesPerSector = "\0\2"
  17. local sectorsPerCluster = "\4"
  18. local reservedSectors = "\1\0"
  19. local fatnum = "\2"
  20. local dirent = "\0\2"
  21. local sectors = string.char(sectors%256) .. string.char(math.floor(sectors/256))
  22. local descriptor = "\xF8"
  23. local sectorsPerFat = string.char(fatSize%256) .. string.char(math.floor(fatSize/256))
  24. local sectorsPerTrack = "\x20\0"
  25. local heads = "\x40\0"
  26. local hiddenSect = "\0\0\0\0"
  27. local largeSectorAmount = "\0\0\0\0"
  28.  
  29. local drivenum = "\x80"
  30. local ntflags = "\0"
  31. local signature = "\x29"
  32. local id = "\0\0\0\0"
  33. local label = "NO NAME "
  34. local sysid = fat12 and "FAT12 " or "FAT16 "
  35. local bootcode = ("\0"):rep(448)
  36. local bootsig = "\x55\xAA"
  37.  
  38. local bootRecord = startseq .. bytesPerSector .. sectorsPerCluster .. reservedSectors .. fatnum .. dirent .. sectors .. descriptor
  39. .. sectorsPerFat .. sectorsPerTrack .. heads .. hiddenSect .. largeSectorAmount
  40. .. drivenum .. ntflags .. signature .. id .. label .. sysid .. bootcode .. bootsig
  41.  
  42. print("Boot record size: " .. #bootRecord)
  43.  
  44. device:write(bootRecord)
  45.  
  46. print("Cleaning FAT tables")
  47. os.sleep(0)
  48. for i = 1, 2 do
  49. device:write("\xF8" .. ("\xFF"):rep(fat12 and 2 or 3) .. ("\0"):rep(fat12 and 509 or 508))
  50. for i = 1, fatSize-1 do
  51. device:write(("\0"):rep(512))
  52. end
  53. end
  54.  
  55. print("Cleaning Root Directory")
  56. os.sleep(0)
  57.  
  58. for i = 1, 512 do
  59. device:write(("\0"):rep(32))
  60. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement