Advertisement
Guest User

flash.lua

a guest
Jan 4th, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.09 KB | None | 0 0
  1. local component = require("component")
  2. local shell = require("shell")
  3. local fs = require("filesystem")
  4.  
  5. local args, options = shell.parse(...)
  6.  
  7. if #args < 1 and not options.l then
  8.   io.write("Usage: flash [-qlr] [<bios.lua>] [label]\n")
  9.   io.write(" q: quiet mode, don't ask questions.\n")
  10.   io.write(" l: print current contents of installed EEPROM.\n")
  11.   io.write(" r: save the current contents of installed EEPROM to file.")
  12.   return
  13. end
  14.  
  15. local eeprom = component.eeprom
  16.  
  17. if options.l then
  18.   io.write(eeprom.get())
  19.   return
  20. end
  21.  
  22. if options.r then
  23.   fileName = shell.resolve(args[1])
  24.   if not options.q then
  25.     if fs.exists(fileName) then
  26.       io.write("Are you want to overwrite " .. fileName .. "?\n")
  27.       io.write("Type `y` to confirm.\n")
  28.       repeat
  29.         local response = io.read()
  30.       until response and response:lower():sub(1, 1) == "y"
  31.     end
  32.     io.write("Reading EEPROM " .. eeprom.address .. ".\n" )
  33.   end
  34.   file = io.open(fileName, 'wb')
  35.   local bios = eeprom.get()
  36.   file:write(bios)
  37.   file:close()
  38.   if not options.q then io.write("All done!\nThe label is '" .. eeprom.getLabel() .. "'.\n") end
  39.   return
  40. end
  41.  
  42. local file = assert(io.open(args[1], "rb"))
  43.  
  44. if not options.q then
  45.   io.write("Insert the EEPROM you would like to flash.\n")
  46.   io.write("When ready to write, type `y` to confirm.\n")
  47.   repeat
  48.     local response = io.read()
  49.   until response and response:lower():sub(1, 1) == "y"
  50.   io.write("Beginning to flash EEPROM.\n")
  51. end
  52.  
  53. if not options.q then
  54.   io.write("Flashing EEPROM " .. eeprom.address .. ".\n")
  55.   io.write("Please do NOT power down or restart your computer during this operation!\n")
  56. end
  57.  
  58. local bios = file:read("*a")
  59. file:close()
  60.  
  61. eeprom.set(bios)
  62.  
  63. local label = args[2]
  64. if not options.q and not label then
  65.   io.write("Enter new label for this EEPROM. Leave input blank to leave the label unchanged.\n")
  66.   label = io.read()
  67. end
  68. if label and #label > 0 then
  69.   eeprom.setLabel(label)
  70.   if not options.q then
  71.     io.write("Set label to '" .. eeprom.getLabel() .. "'.\n")
  72.   end
  73. end
  74.  
  75. if not options.q then
  76.   io.write("All done! You can remove the EEPROM and re-insert the previous one now.\n")
  77. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement