JioFiber Firmware Upgrade LUA Function

 avatar
unknown
lua
3 years ago
10 kB
98
Indexable
function firmware.upgrade (filename, reboot, rebootTime, background, flags,upgradeFlag)
    if (filename == nil) then
        local systemConfig =  db.getTable("firmUpdateSystemConfig", false)
        filename = systemConfig[1].downloadPath
    end

    -- Outputs mentioned as comments but only for JCO4032. Your router may have different outputs. Check /tmp/system.db

    local upgrade = db.getAttribute("environment", "name", "UPGRADE_PROGRAM", "value") -- /pfrm2.0/bin/firmd
    local device = db.getAttribute("environment", "name", "DEVICE_NAME", "value") -- Reliance
    local partitionImage1 = db.getAttribute("environment", "name", "FLASH_FIRM_PARTITION", "value") -- /dev/mtd2
    local partitionImage2 = db.getAttribute("environment", "name", "FLASH_FIRM_PARTITION_IMAGE2", "value") -- /dev/mtd4
    local partition2Image1 = db.getAttribute("environment", "name", "FLASH_FIRM_FS_PARTITION", "value") -- /dev/mtd3
    local partition2Image2 = db.getAttribute("environment", "name", "FLASH_FIRM_FS_PARTITION_IMAGE2", "value") -- /dev/mtd7
	local firmName = ""



	if (util.fileExists ("/pfrm2.0/FIRMWARE_SIGNING_ENABLED1") == true or util.fileExists ("/pfrm2.0/FIRMWARE_SIGNING_ENABLED2") == true) then


	    -- Check the status returned from firmware integrityCheck scrcipt
	
	    upgradeCmd  = "/pfrm2.0/bin/firmwareVerify.sh " .. filename .. " " .. upgradeFlag .. " >/dev/null"                                               
	    os.execute (upgradeCmd)
	
	    local firmSignCheck = ""
	    f = io.open ("/tmp/firmCheckRes.txt", "r")
	    if(f ~= nil) then
	        firmSignCheck = f:read("*line")
	        f:close()
	    else
	        firmware.debugPrint("Firmware integrity check failed. Aborting upgrade ")
	        return "1"
	    end

        
        if (firmSignCheck ~= nil) then
            if ((string.find (string.lower(firmSignCheck), "error") ~= nil) or (string.find (string.lower(firmSignCheck), "sign or verify one file") ~= nil) or (string.find (string.lower(firmSignCheck), "no signature") ~= nil) or (string.find (string.lower(firmSignCheck), "mac and signing key cannot both") ~= nil) or (string.find (string.lower(firmSignCheck), "unsupported") ~= nil) or (string.find (string.lower(firmSignCheck), "failure") ~= nil)) then

                firmware.debugPrint("Firmware integrity check failed. Aborting upgrade ")
                return "1"
            else
                firmware.debugPrint("Firmware integrity check passed")
            end
        else
            firmware.debugPrint("Firmware integrity check passed")
        end

	    f = io.open ("/tmp/firmName", "r")
	    if(f ~= nil) then
	        firmName = f:read("*line")
	        f:close()
	    else
	        firmware.debugPrint("Firmware integrity check failed. Aborting upgrade ")
	        return "1"
	    end
	end
    if (util.fileExists ("/pfrm2.0/BRCMJCO300") or util.fileExists ("/pfrm2.0/ECONET")) then
        local file = ""
        if (util.fileExists ("/pfrm2.0/HW_FOXCONN_JCO500")) then
            file = "HW_FOXCONN_JCO500"  
        elseif (util.fileExists ("/pfrm2.0/HW_JCO110")) then
            file = "HW_JCO110"  
        elseif( util.fileExists("/pfrm2.0/HW_HG260ES")) then
            file = "HW_HG260ES"  
        elseif( util.fileExists("/pfrm2.0/HW_HG261GU2")) then
            file = "HW_HG261GU2"  
        elseif (util.fileExists("/pfrm2.0/HW_HG260X")) then
            file = "HW_HG260X"  
        elseif(util.fileExists("/pfrm2.0/HW_FIBERHOME_JCO300"))then
            file = "HW_FIBERHOME_JCO300"
        elseif(util.fileExists("/pfrm2.0/HW_JCOW206"))then
            file = "HW_JCOW206"
        elseif(util.fileExists("/pfrm2.0/HW_JCOW401"))then
            file = "HW_JCOW401"
        elseif(util.fileExists("/pfrm2.0/HW_JCOW403"))then
            file = "HW_JCOW403"	
        elseif(util.fileExists("/pfrm2.0/HW_JCOW404"))then
            file = "HW_JCOW404"
        elseif(util.fileExists("/pfrm2.0/HW_JCOW411"))then
            file = "HW_JCOW411"
        elseif(util.fileExists("/pfrm2.0/HW_JCO2031"))then
            file = "JCO2031"	
        elseif(util.fileExists("/pfrm2.0/HW_JCO4031"))then
            file = "JCO4031"
	elseif(util.fileExists("/pfrm2.0/HW_JCOW414"))then
            file = "JCOW414"
        elseif(util.fileExists("/pfrm2.0/HW_JCO4032"))then
            file = "JCO4032"
        elseif(util.fileExists("/pfrm2.0/HW_JCOW407"))then
            file = "JCOW407"
        elseif(util.fileExists("/pfrm2.0/HW_JMA24"))then
            file = "JMA24"
        elseif(util.fileExists("/pfrm2.0/HW_JCOW402"))then
            file = "JCOW402"	
        end

        local firm_check_cmd = ""
        if (file ~= nil) then
	        if (util.fileExists ("/pfrm2.0/FIRMWARE_SIGNING_ENABLED1") == true or util.fileExists ("/pfrm2.0/FIRMWARE_SIGNING_ENABLED2") == true) then
                firm_check_cmd = "strings " .. firmName.. " | grep " ..file.. " > /tmp/firm_check.txt"
            else
                firm_check_cmd = "strings "..filename.." | grep " ..file.. " > /tmp/firm_check.txt"
            end
        end
      
        os.execute(firm_check_cmd)

        local firmCheck = ""
	    fp= io.open ("/tmp/firm_check.txt", "r")
	    if(fp ~= nil) then
	        firmCheck = fp:read("*line")
        end

        if (firmCheck ~= nil) then
            if (string.match(firmCheck,file) == nil) then
                return "2"
            end
        else
            return "2"
        end
    end

    if (util.fileExists("/pfrm2.0/HW_HG260X")) then
          local filename = "/tmp/upgrade/download.dwn"
          local s1 = "strings "..filename.." | grep HG260X_D2.96  > /tmp/dmz6.txt "
          local s2 = "strings "..filename.." | grep HG260X_R2.96  >> /tmp/dmz6.txt "
          local s3 = "strings "..filename.." | grep HG260X_D2.97 >> /tmp/dmz6.txt "
          local s4 = "strings "..filename.." | grep HG260X_R2.97 >> /tmp/dmz6.txt "
          os.execute(s1)
          os.execute(s2)
          os.execute(s3)
          os.execute(s4)
         local contents = ""
         f = io.open ("/tmp/dmz6.txt", "r")
         if(f ~= nil) then
            contents  = f:read("*all")
            f:close()
         else
            f:close()
            return "ERROR"
         end
	
	 if(string.match(contents,"HG260X_D2.96") or string.match(contents,"HG260X_R2.96") or string.match(contents,"HG260X_R2.97") or string.match(contents,"HG260X_D2.97")) then
		local query = "DROP TABLE dmz6"
                local valid = db.execute(query)
                if(valid)then
                  db.save2()
                else
                  db.rollback()
                end

	 end

     end

	local runningFirmware = ""
    f = io.open("/tmp/runningFirmware","r")
    if(f ~= nil) then
        runningFirmware = f:read("*line")
        f:close()
	else
		runningFirmware = "1"
    end

	if (runningFirmware == "1") then
		partition = partitionImage2
		partition2 = partition2Image2
	else
		partition = partitionImage1
		partition2 = partition2Image1		
	end

    -- TODO: local upgradeprep = db.getAttribute("environment", "name", "UPGRADEPREP_PROGRAM", "value")

	if (util.fileExists ("/pfrm2.0/FIRMWARE_SIGNING_ENABLED1") == true or util.fileExists ("/pfrm2.0/FIRMWARE_SIGNING_ENABLED2") == true) then
    cmd = upgrade .. " -f " .. firmName .. " -d " .. device .. "  -p " .. partition
    else
    cmd = upgrade .. " -f " .. filename .. " -d " .. device .. "  -p " .. partition
    end

    if (partition2) then
        cmd = cmd .. " -s " .. partition2 .. " -R " .. partition2
    end

    -- creating files in flash for using them in sending 0BOOTSTRAP event in tr69
    os.execute("touch " ..firmware.flashKeepAliveEvents)
    --os.execute("touch " ..firmware.flashUpdradesManaged)
    if (util.fileExists(firmware.serialNUMFile)) then
	    local serial = util.fileToString(firmware.serialNUMFile)
        if(serial ~= nil) then
            os.execute("/bin/echo " ..serial.. " > " ..firmware.flashserialNUMFile.. " ")
        end
    end
    if (util.fileExists(firmware.prodclassFile)) then
	    local productclass = util.fileToString(firmware.prodclassFile)
        if(productclass ~= nil) then
            os.execute("/bin/echo " ..productclass.. " > " ..firmware.flashprodclassFile.. " " )
        end
    end

    local filep = io.open(firmware.versionFile, "r")
    local swVersion = ""

    if (filep ~= nil) then
        swVersion  = filep:read("*l")
        filep:close()
    end        

    local fp = io.open(firmware.flashfirmwareVerFile,"w")
    if (fp ~= nil) then
        fp:write(swVersion)
        io.close(fp)
    end

    if (tonumber(reboot) ~= 0) then
        cmd = cmd .. " -r " .. rebootTime
        if(util.fileExists("flash2/pfrm2.0/bin/ont_dc")) then
	    os.execute ("/flash2/pfrm2.0/bin/ont_dc -e REBOOT NA 1 NA NA NA")
        else
	    os.execute ("/pfrm2.0/bin/ont_dc -e REBOOT NA 1 NA NA NA")
        end
    end
	if (flags) then
        cmd = cmd .. " " .. flags .. " "
	end

    if (tonumber(background) == 0) then
        cmd = cmd .. " -F "
    end

	cmd = cmd .. "2> /dev/null" 

    firmware.debugPrint("Exec = " .. cmd)

    status = os.execute (cmd)

    if(util.fileExists("/pfrm2.0/DEVICE_REPEATER"))then
        os.execute("/bin/ledctl1 ALL off;/bin/ledctl1 GREEN slowBlink;")
    end

    firmware.debugPrint("STATUS : " .. status)
        if (status ~= 0) then
        --create a file on Firmware upgrade Failure for DC 
        os.execute ("touch /flash/DC_Firm_Upgrade_Failure") 
        os.execute("rm -rf /flash/DC_Firm_Upgrade_Success")
        else
        --create a file on Firmware upgrade success for DC 
        os.execute ("touch /flash/DC_Firm_Upgrade_Success") 
        os.execute("rm -rf /flash/DC_Firm_Upgrade_Failure")
        os.execute("touch /flash/MESH_CONFIG_UPGRADE")
    	os.execute ("rm -rf /flash/smartcable/iptables.sh")
    	os.execute ("rm -rf /flash/smartcable/ota.zip")
    	os.execute ("rm -rf /flash/smartcable/firmware_file_version.txt")
	os.execute ("/bin/rm -rf /flash/ont_dc.conf")
            if( util.fileExists("/pfrm2.0/HW_HG260ES") or util.fileExists("/pfrm2.0/HW_HG260X")) then
                os.execute("/bin/ledctl WLAN off")
                os.execute("/bin/ledctl WPS off")
            end
	firmware.voipCleanUp()
        end

    return status;
end
Editor is loading...