--[[
Copyright (c) 2026 VCHs
Based on OpenOS "flash" utility. OpenOS is part of the source code of OpenComputers.
]]--

local component = require("component")
local term = require("term")
local c = component.computer

local bios_version = "1.7.12"

term.clear()

io.write([[
⡏⠉⠭⠍⠩⠭⠉⢹ Giez BIOS v. ]]..bios_version.."\n"..[[
⡇ ⠿⠇⠸⠿ ⢸ Flashing Utility
⣇⣀⣘⣒⣒⣃⣀⣸ © 2018 Luca_S
⣇⣇⣓⣒⣃⣓⣚⣸ © 2026 VCHs
         License: MIT
═════════════════════════
]])

local function writeRom()
	local bios = [===[--Giez BIOS 1.7.12 © 2026 VCHs © 2018 Luca_S; MIT License
local i,b,o,d,e,f,g,h,l='local a,b,c,d=computer, onent,{k="key_down",t="touch",r="v_removed",clipboard"},{d="drive",f="filesystem",g="gpu",s="screeaou}ni="in0net",e="eeprom"} e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A=nil,{},b.invoke,a.pullSignallist(d.e)()ws*g*a*i!{0,0x80808ffff000ff},1p.1,5p250B20,{	pit.lua","boot/k	l/poipes OS\nfunction B() C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R;if not m orna.isRobthen b\nP(3p1)a.shut()end;f  S=0,4 do j(n,"setPaletteColor",S,?q[S+1]#2po!So=b.\npxy(o)o.$Wave(1,4\'2(3(VoluUm(as*z2+\\3+UADSR(p0\r s,u-x2.?*0.Go=1@pen;o.yp1p\npcess1f=U(T,U,V\'Frequency(pT32,uU033,V6S#Ao.t3\'de_lay(r8$d8k(t8$r8(u8$=s8clos(`>U2>3#1 #AC3WX=j(W,"readSect%q1)#1,#X$pX:sub(S,S)=="\\0"!bX=D1,S-1)bBk|@@returE~@a.getB@OAddr28fB0l,"HData")GfH$HArGJ2$J,W#2DAY,Zab.typeA==d.d!SP=load+(CA)tY!SKalseGItruHTTPf!SCvADBBex0A[S])a@BBisDirByY4SUsG+TdTS@$Rbiv	m)$URes*v",$Rmax`))\rPB0$pHb)\r=math.floor(v/2)-4,dweq2;E3_,a0,a1M$s",g#AclearLine3a2`SlC0,a2\r@1," "z#2Fj3,a4`VForegra3F$UBackJn47G8`o"\rA)F(q[2]kK1\rBlE)Pw-2,"Giez BIOS v. 1.7.12ulp 2026 V?CHs   y0xr18 Luca_=SlG()Hjl 0_=e`-#a1/2+1;Egz#AIj5,a6Sa5==c.r!SJA6Qg s!_"{UӀsa&Usi!Spe!Serre0"EEPRO!MS@e@J8t&!3a"p!p]2Ќ241511J?t91o?d39A*91s91r91p#"@9K3,a7~8 =k(s)I~zt!4]Sa7/"!j06Ϻ#AHy+2E(\r"⡏⠭⠍{2⢹u\r+lp⡇ ⠅⠨⠪ ⢸Uv`⣇⣀ߣ⣉Ƀ"63ԡ⣠Bm5,3 45W bT_2E##p!SW`@wh@Uc$ Fk !F"GLA,a\r9E3]cߞ⠇⠸%УӢ?_yW~tried 1ing f\n aIl9 Ya9~="EsN&bP!b aP?B319W{ȍYRATF@TK("No W`	 : "..Z#2 b~repeatucB7aehuge)Pab..(ac"")unIt"cȑp  l(RrabRwZ "P@rU<֔R]BaS qui	iM8zia=#,ހi@airs())$ g[#\rg& =S@%h[#h囔1xygpD(ad[i[#iހ@u(hHm# /{(iad==We=S;Ft=1;f@M()}NJ#i==1WFz("Any P" summRmenus."®z>0$VdU$P|E Qz=z-1c/()L("pr!;N()O3(< [Tab] Exit >",3<nP,">"Uf,ag,ahp`,"",a!irr5EjtzA4h=ah5#ah5`ae=ae-k3+5+f" Y\'28l7ah001ahnw>31S6<1a0\'..s ng.char(ei\\#+i6ccay6)j=a6\'v-ap6+1aj#ajޤe>=v^e=0;af=af  ppqҀ3<绅Cae<0VAf0XsPv#Zvjv9▭[F;P;k#p~/jjpBj⠰⠆pjjjpCjj"BDHe50ak_6Wel,amQpques+ 0`WelvConn@ nHܼc=al. A(m=amW_@W_pppWV(bQ)6)^m_UcriptQ+ctpEnter URL{R2?ek=O5ڟ?e$P2usto9meenoi?mediumd6n,aoan~adan:leb`ad1"&ʿignWame9	9~OL(ao^0Re1&R] RefR hCG] C^p45\rAԓI] NetE:r8QTYab9PYnzS==TF(0,;BSp,kaqZjf,"s_Label\'Fay0R()N(a\rqUIDP1k SsA@" (Defa_ult)"R, w @@??*z?Xz20Te=!>#ij0<1e|L(i[es~De4T\r(M)^23,BQÍ<ti[ai-6]seȰ XTl5 x"5 tPcU80B)a.beep(15 `.5)n=<d.g)()msDn1m_aj(n~Foregr#",0)Bac=kq[4]v , Gieztaa $!chnirvanna"JXK,9 ',1,'',''while b<=#i do e=o.byte(i,b)b=b+1;for j=0,7 do h=o.sub;l=h(i,b,b)if e>>j&1<1 and b<#i then g=o.unpack('>I2',i,b)f=1+(g>>4);l=h(d,f,f+(g&15)+2)b=b+1 end;b=b+1;o=o..l;d=h(d..l,-4^6)end end load(o,'=bios')()]===]

	io.write("Insert the EEPROM you would like to flash.\nType \"y\" to write => ")
	local response
	repeat
	response = io.read()
	until response

	if response:lower():sub(1, 1) ~= "y" then
		return nil, "flashing manually aborted"
	end
	
	local eeprom = component.eeprom

	io.write("Flashing EEPROM " .. eeprom.address .. ".\n")
	io.write("Please do NOT power down or restart your computer during this operation!\n")

	local result, reason = pcall(eeprom.set, bios)
	if reason then
		return nil, reason
	end

	local result, reason = eeprom.setLabel("Giez BIOS "..bios_version)
	if reason then
		return nil, reason
	end
	
	local checksum_because_the_user_could_be_Pushnoy = eeprom.getChecksum()

	c.beep(330, .1)
	c.beep(500, .1)
	io.write("Giez BIOS "..bios_version.." has been successfully flashed.\nType \"l\" if you want to lock the EEPROM => ")

	repeat
	response = io.read()
	until response

	if response:lower():sub(1, 1) == "l" then
		c.beep(1500, .1)
		io.write("\nAre you sure? This operation is NOT reversable and you won't be able to reflash this EEPROM ever again! Only do this if you know what you're getting into and accept the consequences.\nType \"y\" to lock the EEPROM => ")
		
		repeat
		response = io.read()
		until response
		
		if response:lower():sub(1, 1) == "y" then
			if checksum_because_the_user_could_be_Pushnoy ~= eeprom.getChecksum() then
				io.write("\nWell played, interloper. Very well played.")
				return nil
			end
			eeprom.makeReadonly(checksum_because_the_user_could_be_Pushnoy)
			c.beep(450, .2)
			io.write("\nGiez BIOS "..bios_version.." has been successfully flashed PERMANENTLY.")
		end
	end

	return nil
end

local result, reason
result, reason = writeRom()
if reason then
	c.beep(300, .1)
	io.stderr:write("Failed to flash Giez BIOS "..bios_version..":\n"..reason..'\n')
	return 1
end
io.write("\nExiting.")