User Tools

Site Tools


operating_systems:microsoft:patchbootini

Patch boot.ini

@ECHO OFF
REM ** This script is made to check the registry and to modify correctly the boot.ini file
REM ** It works at least on WINXP SP2
REM ** Author: Sylvain Bolay
REM ** CREATION Date: June 16, 2006
REM ** E-MAIL: sylvain.bolay@irovision.ch http://www.bolay.net
 
cls
Echo ** CD to c:\ **
cd
c:
cd
 
ECHO *******************************************
ECHO ** Query of boot.ini **
 
BOOTCFG /Query | FIND "OS Load Options" > tempfile
TYPE tempfile

REM ** Record the boot options
FOR /F "tokens=3*" %%i IN (tempfile) DO SET bootOptions=%%j
ECHO Boot Options: %bootOptions%
 
ECHO ** End of Query of boot.ini **
ECHO ******************************
ECHO ** Check if /noexecute is present **
REM ** This script uses a tricks for setting a variable with the output
REM ** of the FIND command. While executing FIND with the options /C, it will
REM ** return the count of lines containing the stringin the TEMPFILE file.
REM ** The format of the output is like: ---------- TEMPFILE: 1
REM ** We see that FIND write first the string "----------" that can be used for the trick.
REM ** By naming a batch file (.bat) with the name ----------.bat and then
REM ** calling (CALL) the file (.bat) that contains the output of the FIND command
REM ** it will first search for the ---------- command and execute it.
REM ** If this file (----------.bat) contains a command like: SET found0_=%%2
REM ** you will finaly have the variable found0_ set to the second parameters
REM ** after the command ---------- that is in this exampe: 1
 
FIND /C /I "/noexecute=" tempfile > tmp$#$#$.bat
 
ECHO tmp$#$#$.bat:
TYPE tmp$#$#$.bat

REM ** Create a .bat file to set the found_ variable
ECHO SET found0_=%%2 > ----------.bat
 
ECHO ----------.bat:
TYPE ----------.bat

REM ** Make the set variable
SET found0_=
CALL tmp$#$#$.bat ----------.bat
ECHO "found /noexecute?: %found0_% (0=No, 1=Yes)"

REM ** Delete temporary files
IF EXIST ----------.bat DEL ----------.bat
IF EXIST tmp$#$#$.bat DEL tmp$#$#$.bat

REM ** If /noexecute not found, add it else continues...
IF %found0_%==0 (
        ECHO "/noexecute= NOT Found!"
        ECHO "** End of Check if /noexecute is present **"
        ECHO "*******************************************"
        ECHO "**          Patching boot.ini            **"
 
        SET found0_=
        SET bootOptions=
        IF EXIST tempfile DEL tempfile
        REM ** Backup the actual bootcfg
        BOOTCFG /Query > boot.ini.bck
        BOOTCFG /RAW "%bootOptions% /noexecute=alwaysoff" /ID 1
        IF "%ERRORLEVEL%"=="0" (
                GOTO _end_OK_without_restart
        ) ELSE (
                GOTO _end_KO
        )
        REM ** Paranoïa!
        GOTO _end_KO
) ELSE (
        ECHO "/noexecute= Found!"
        ECHO "**   End of Check if /noexecute is present  **"
        ECHO "**********************************************"
        ECHO "** Check if /noexecute=alwaysoff is present **"
        SET found0_=
        GOTO _find_alwaysoff
)
REM ** End of if /noexecute= present.

REM ** Paranoïa!
GOTO _end_KO
 
 
:_find_alwaysoff
 
FIND /C /I "/noexecute=alwaysoff" tempfile > tmp$#$#$.bat
 
ECHO "A) tmp$#$#$.bat:"
TYPE tmp$#$#$.bat
REM ** Create a .bat file to set the found_ variable
ECHO SET found1_=%%2 > ----------.bat
ECHO ECHO 1: %%1 >> ----------.bat
ECHO ECHO 2: %%2 >> ----------.bat
ECHO ECHO found1_: %%found1_%% >> ----------.bat
 
ECHO "B) ----------.bat:"
TYPE ----------.bat

REM ** Make the set variable
CALL tmp$#$#$.bat ----------.bat
 
ECHO ***FOUND1_***
ECHO found1_: %found1_%
ECHO *************
 
ECHO "C) found /noexecute=alwaysoff?: %found1_% (0=No, 1=Yes)"

REM ** Delete temporary files
IF EXIST ----------.bat DEL ----------.bat
IF EXIST tmp$#$#$.bat DEL tmp$#$#$.bat

REM ** If /noexecute=alwaysoff founded --> done, else continues...
IF %found1_%==1 (
        SET found1_=
        GOTO _end_OK_without_restart
) ELSE (
        SET found1_=
        REM ** Backup the actual bootcfg
        BOOTCFG /Query > boot.ini.bck
        ECHO "D) tempfile:"
        TYPE tempfile
        ECHO "E) Boot Options: %bootOptions%"

        REM ** Will parse only up to 9 tokens (this part may be improved)
        FOR /F "tokens=1-9*" %%1 IN ("%bootOptions%") DO (
                IF %%1 NEQ 0 ECHO mySpecialCall %%1> tmp$#$#$.1_.bat
                IF %%2 NEQ 0 ECHO mySpecialCall %%2> tmp$#$#$.2_.bat
                IF %%3 NEQ 0 ECHO mySpecialCall %%3> tmp$#$#$.3_.bat
                IF %%4 NEQ 0 ECHO mySpecialCall %%4> tmp$#$#$.4_.bat
                IF %%5 NEQ 0 ECHO mySpecialCall %%5> tmp$#$#$.5_.bat
                IF %%6 NEQ 0 ECHO mySpecialCall %%6> tmp$#$#$.6_.bat
                IF %%7 NEQ 0 ECHO mySpecialCall %%7> tmp$#$#$.7_.bat
                IF %%8 NEQ 0 ECHO mySpecialCall %%8> tmp$#$#$.8_.bat
                IF %%9 NEQ 0 ECHO mySpecialCall %%9> tmp$#$#$.9_.bat
                SET bootOptions=
                GOTO _processFiles
        )
)
REM ** End of if /noexecute=alwaysoff present.

REM ** Paranoïa!
GOTO _end_KO
 
:_processFiles
REM ** This part may be improved
CALL :_doesFileExist tmp$#$#$.1_.bat
CALL :_doesFileExist tmp$#$#$.2_.bat
CALL :_doesFileExist tmp$#$#$.3_.bat
CALL :_doesFileExist tmp$#$#$.4_.bat
CALL :_doesFileExist tmp$#$#$.5_.bat
CALL :_doesFileExist tmp$#$#$.6_.bat
CALL :_doesFileExist tmp$#$#$.7_.bat
CALL :_doesFileExist tmp$#$#$.8_.bat
CALL :_doesFileExist tmp$#$#$.9_.bat
GOTO _update_bootini
 
:_doesFileExist
ECHO *** Does the file "%1" exist? ***
IF EXIST %1 (
        ECHO "FILE %1 EXIST!"
) ELSE (
        GOTO :eof
)
ECHO %1 contains:
TYPE %1
TYPE %1 | FIND "/noexecute" > $1.$$$
CALL :_checkSize $1.$$$ noexecuteFound
IF %noexecuteFound%==0 (
        CALL :_addOptions %1
) ELSE (
        ECHO /noexecute found!!
)
IF EXIST %1 DEL %1
SET noexecuteFound=
GOTO :eof
REM ** END of _doesFileExist ***
 
 
:_addOptions
ECHO *** Add options from file %1 ***
ECHO %1:
TYPE %1
REM ** Create a .bat file to set the new_bootOptions_ variable
ECHO SET new_bootOptions_=%%new_bootOptions_%%%%1 > mySpecialCall.bat
ECHO ECHO 1) %%1 >> mySpecialCall.bat
ECHO ECHO 1) %%1 >> mySpecialCall.bat
ECHO ECHO 2) %%2 >> mySpecialCall.bat
ECHO ECHO 3) %%3 >> mySpecialCall.bat
ECHO ECHO 4) %%4 >> mySpecialCall.bat
ECHO mySpecialCall.bat:
TYPE mySpecialCall.bat
REM ** Make the set variable
CALL %1 mySpecialCall.bat
ECHO new_bootOptions: %new_bootOptions_%

REM ** Delete temporary files
IF EXIST mySpecialCall.bat DEL mySpecialCall.bat
GOTO :eof
REM ** End of _addOptions ***
 
 
:_update_bootini
ECHO Finalize the new boot options:
SET new_bootOptions_=%new_bootOptions_%/noexecute=alwaysoff
ECHO New Boot Options: "%new_bootOptions_%"
BOOTCFG /RAW "%new_bootOptions_%" /ID 1
SET new_bootOptions_=
GOTO _end_OK_without_restart
 
:_end_KO
        ECHO NOT OK
        GOTO _tmp_end
 
:_end_OK_with_restart
        ECHO END OK WITH A RESTART
        CALL :_updateRegistry
        BOOTCFG /Query | find "OS Load Options"
        SHUTDOWN -r -t 5 -c "BOOT.INI file successfully patched. Windows needs to be restarted. Please wait..."
        GOTO _tmp_end
 
:_end_OK_without_restart
        ECHO END OK WITHOUT A RESTART
        CALL :_updateRegistry
        BOOTCFG /Query | find "OS Load Options"
        GOTO _tmp_end
 
:_checkSize
SET %2=0
IF %1'==' ECHO usage is: %0 FILENAME.EXT && GOTO :eof
IF NOT EXIST %1 ECHO %1 was not found && GOTO :eof
ECHO Check size of %1 file:
IF %~z1 GTR 0 ECHO The size of the %1 file is: %~z1 && SET %2=1
IF EXIST %1 DEL %1
SET %2
GOTO :eof
 
:_updateRegistry
regedit /S \\siro.iro\netlogon\Registry\HKLM_bootini.reg
GOTO :eof
 
:_tmp_end
ECHO ** FINISHING...*
SET counter1=
REM ** BOOTCFG /RAW "/noexecute=alwaysoff /fastdetect" /ID 1
REM ** BOOTCFG /RAW "/noexecute=alwayson /fastdetect" /ID 1
REM ** BOOTCFG /RAW "/toto1 /toto2 /toto3 /noexecute=alwayson /toto5 /toto6 /toto7 /toto8 /toto9 /toto10" /ID 1
SET
ECHO FINISHED!
operating_systems/microsoft/patchbootini.txt · Last modified: 2011/08/15 07:15 by sbolay