@echo off
REM Version 4.2 2015-03-12 - run from .\_ISO\docs\Make_E2B_USB_Drive folder (Uses LockDisMount), check for grubinst_new and new version of grubinst with silent-boot option, remove touchdrv.exe command, check 4K drives
REM Version 4.3 2015-04-07 - change method of detection of admin privileges
REM Version 4.4 2015-05-03 - Use RMPartUSB and grubinst in same folder if present.
REM version 4.5 2015-06-03 - copy bootmgr to NTBOOT.MOD folder if possible
REM version 4.6 2015-09-28 - default NTFS, add UEFI help text
REM version 4.7 2016-01-18 - bugfix for removable drive detection
REM version 4.8 2016-02-04 - ignore errors on xcopy (in case \autorun.inf is WP), tidy up .tmp file
REM version 4.9 2016-02-12 - if XP don't use wmic

cls
TITLE %~n0 v4.9
:: set cmd shell window size
mode con: cols=130 lines=60
color 1f
echo Make an Easy2Boot USB drive (using RMPartUSB)
echo =============================================
echo.
set str=%~dp0
set str=%str:~1,6%
if /i "%str%"==":\_ISO" echo ERROR: This drive is an E2B drive! Please run from a folder on your hard disk. & color 4f & pause & goto :EOF
set str=

call :check_Permissions
if "%ADMIN%"=="FAIL" goto :EOF

pushd "%~dp0"


echo.%~pn0 | find /I "Desktop\_ISO\docs\Make_e2b">nul && (set ERR=0) || (set ERR=1)
if "%ERR%"=="0" echo ERROR: Please extract the Easy2Boot files to an EMPTY FOLDER - not the Desktop! & color 4f & pause & goto :EOF
set ERR=

set str=%~dp0
set str=%STR:~-2,2%
if "%str%"==":\" echo ERROR: Source is a drive! Please run %~nx0 from the download folder & color 4f & pause & goto :EOF
set str=

if not exist ..\..\..\menu.lst echo ERROR: Cannot find Easy2Boot files in %~dp0 & color 4f & pause & exit
if not exist ..\..\e2b\grub\menu.lst echo ERROR: Cannot find Easy2Boot files in %~dp0 & color 4f & pause & exit
if not exist ..\..\e2b\grub\E2B_GRUB.txt echo ERROR: Cannot find Easy2Boot files in %~dp0 & color 4f & pause & exit

REM change to our working folder
set WF=%~dp0
if exist "%WF%\rmpartusb.exe" goto :gotwf
set WF=%ProgramFiles(x86)%\RMPrepUSB
if exist "%WF%\rmpartusb.exe" goto :gotwf
set WF=%ProgramFiles%\RMPrepUSB
if exist "%WF%\rmpartusb.exe" goto :gotwf


:NF
echo.
echo Sorry - I cannot find RMPartUSB!
echo Please install RMPrepUSB to your default Program Files folder
echo OR
echo Specify the folder where RMPartUSB.exe is located.
echo.
set /p WF=RMPartUSB folder is at (e.g. D:\temp\RMPrepUSB) ? : 
if exist "%WF%\rmpartusb.exe" goto :gotwf
set /p ask=Sorry - couldn't find it - press R to Retry or any other key to exit (Retry\Abort) : 
if /i "%ask%"=="R" goto :NF
exit



:gotwf
set ALL=
set A=
pushd "%WF%"
RMPARTUSB FIND > %temp%\mdtemp.cmd
call %temp%\mdtemp.cmd > nul
if exist %temp%\mdtemp.cmd del %temp%\mdtemp.cmd > nul
if not "%DRIVESIZE%"=="0" goto :skipa
echo.
echo.
set /P A=WARNING: NO USB DRIVES DETECTED - DO YOU WANT TO LIST ALL THE DRIVES (Y/[N]) : 
if /I not "%A%"=="Y" goto :EOF
set ALL=ALLDRIVES
RMPARTUSB FIND %ALL% > %temp%\mdtemp.cmd
call %temp%\mdtemp.cmd > nul
if exist %temp%\mdtemp.cmd del %temp%\mdtemp.cmd > nul

:skipa
if "%DRIVESIZE%"=="0" color 4f & echo. & echo NO USB DRIVES FOUND! & pause & exit

REM list all USB drives so user can see them and their drive number
RMPARTUSB LIST  %ALL%
REM Ask user for a drive number
echo ERASE AND FORMAT USB DRIVE
echo.
set /P DD=USB DRIVE NUMBER (e.g. %DRIVE%) : 
if "%DD%"=="0" color 4f & echo. & echo DRIVE 0 is your System Disk! & pause & exit
cls
echo.
REM execute RMPARTUSB (add SURE if you don't want the user prompt)
RMPARTUSB DRIVE=%DD% GETDRV %ALL% > %temp%\mdtemp.cmd
call %temp%\mdtemp.cmd > nul
if exist %temp%\mdtemp.cmd del %temp%\mdtemp.cmd > nul
if "%USBDRIVESIZE%"=="0" echo ERROR: NOT A USB DRIVE? & color 4f & pause & exit

ver | find " XP " > nul && goto :skp4k
:: check if 4K sector drive
wmic DISKDRIVE get caption, description, Deviceid, bytespersector | find "DRIVE%DD%" > %temp%\DD.tmp
find "4096" %temp%\DD.tmp > nul
if not errorlevel 1 color 4f & echo. & echo. & type %temp%\DD.tmp & echo. & echo SORRY: THIS DRIVE HAS 4K SECTORS AND IS NOT BOOTABLE! & echo. & pause & del %temp%\DD.tmp & goto :EOF
if exist %temp%\DD.tmp del %temp%\DD.tmp
:skp4k

cls
RMPARTUSB LIST %ALL%
if "%DD%"=="0" echo WARNING: DRIVE 0 IS YOUR SYSTEM DRIVE!!! & pause & exit

if exist r.tmp del r.tmp
ver | find " XP " > nul && goto :skprem

wmic /LOCALE:ms_409 diskdrive get MediaType,deviceid | find "DRIVE%DD%" > r.tmp 2>nul
set DTYPE=UNKNOWN DISK TYPE (may be Fixed or Removable)!
find /i "Removable" r.tmp > nul && set DTYPE=REMOVABLE DISK
find /i "hard" r.tmp > nul && set  DTYPE=FIXED DISK (WARNING: must use .imgPTN files or a Helper Flash drive for Windows 7/8/10 INSTALLER ISOs)
find /i "Fixed" r.tmp > nul && set DTYPE=FIXED DISK (WARNING: must use .imgPTN files or a Helper Flash drive for Windows 7/8/10 INSTALLER ISOs)
::type r.tmp
echo INFORMATION: Drive %DD% is a %DTYPE%
echo.
echo.
if exist r.tmp del r.tmp
:skprem





echo WARNING: ALL PARTITIONS ON DRIVE %dd% WILL BE DESTROYED...
echo.
set /p ask=Are you sure it is OK to format DRIVE %DD% (Y/N) : 
if /i not "%ask%"=="Y" exit
if "%DD%"=="0" exit	

:lpf
cls
echo.
echo CHOOSE A FILESYSTEM 
echo ===================
echo.
echo N = NTFS  - If you want to boot from large files over 4GB (supports UEFI) *RECOMMENDED FOR GENERAL USE
echo F = FAT32 - If all payload files are below 4GB in size    (supports UEFI)  FASTER AND MORE COMPATIBLE
echo.
echo Note: FAT32 and NTFS both support UEFI-booting.
echo.
set ask=
set /p ask=Format as FAT32 or NTFS? (F=FAT32/[N=NTFS]) : 
set FMT=NTFS
if /i "%ask%"=="F" set FMT=FAT32& goto :cc1
if "%ask%"=="" goto :cc1
if /i not "%ask%"=="N" goto :lpf
:cc1


echo.
echo ERASE AND FORMAT DRIVE %DD% as %FMT%...
RMPARTUSB DRIVE=%dd% CLEAN %ALL% > nul
if errorlevel 1 (color 4f & echo. & echo RMPARTUSB CLEAN COMMAND FAILED OR ABORTED BY USER - IF ERROR TRY AGAIN! & pause & exit)
REM delay to allow WIndows to see empty drive
RMPARTUSB DRIVE=%DD% GETDRV %ALL% > nul
RMPARTUSB DRIVE=%dd% WINPE %FMT% 2PTN %ALL% SURE VOLUME=E2B> nul
if errorlevel 1 (color 4f & echo RMPARTUSB COMMAND FAILED! & pause & exit)
echo Format complete - OK
set USBDRIVELETTER=
REM get the drive letter of the newly formatted drive
FOR /F "tokens=*" %%A IN ('RMPARTUSB DRIVE^=%dd% GETDRV %ALL%') DO %%A
If "%USBDRIVELETTER%"=="" goto :EOF



popd

if not exist ..\..\..\menu.lst exit
echo.
echo INFORMATION: Some Anti-Virus programs (e.g. Trend Micro AV) may cause this copy phase to fail!
echo.
echo Copying over Easy2Boot files to %USBDRIVELETTER% - Please wait...
REM Only copy files in root, no subdirs; System Volume Information & $RECYCLE.BIN case problems with /herky
xcopy /hrky ..\..\..\*.* %USBDRIVELETTER%\*.* > nul
If errorlevel 1 color 4f & echo ERROR IN COPYING FILES & pause & exit
REM Copy _ISO subdir
xcopy /herky ..\..\..\_ISO\*.* %USBDRIVELETTER%\_ISO\*.* > nul
echo.
If errorlevel 1 color 4f & echo ERROR IN COPYING FILES & pause & exit

:: copy bootmgr for NTBOOT if present (doesn't seem to work if do it later!)
:: Windows 10 \Windows\Boot\PCAT\bootmgr gives BCD error so don't use it if possible!
if not exist %USBDRIVELETTER%\_ISO\e2b\grub\DPMS\NTBOOT.MOD\bootmgr (
if exist C:\bootmgr xcopy /h c:\bootmgr %USBDRIVELETTER%\_ISO\e2b\grub\DPMS\NTBOOT.MOD  > nul
)
if not exist %USBDRIVELETTER%\_ISO\e2b\grub\DPMS\NTBOOT.MOD\bootmgr (
if exist C:\Windows\Panther\Rollback\bootmgr xcopy /h C:\Windows\Panther\Rollback\bootmgr %USBDRIVELETTER%\_ISO\e2b\grub\DPMS\NTBOOT.MOD  > nul
)
if not exist %USBDRIVELETTER%\_ISO\e2b\grub\DPMS\NTBOOT.MOD\bootmgr (
if exist "%SystemRoot%\Boot\PCAT\bootmgr" xcopy /h "%SystemRoot%\Boot\PCAT\bootmgr" %USBDRIVELETTER%\_ISO\e2b\grub\DPMS\NTBOOT.MOD > nul
)


pushd "%WF%"

if "%dd%"=="0" exit

REM install grub4dos - use new grubinst to install to PBR
echo Installing grub4dos to PBR of %USBDRIVELETTER%...
set GR=grubinst.exe
REM check if silent-boot supported
if exist gr.txt del gr.txt
if exist gr1.txt del gr1.txt
%GR% -h 2> gr.txt
set SILENT=
find "--silent-boot" gr.txt > nul
if not errorlevel 1 set SILENT=--silent-boot
if "%SILENT%"=="" set GR=grubinst_new.exe

if not exist %GR% color 4f & echo ERROR: %GR% not found or is an old version! & pause & exit

REM Dismount the drive, run grubinst, remount the drive
"%~dp0LockDismount.exe" -force %dd% %GR% -t=0 %SILENT% --install-partition=0 (hd%dd%) >> gr1.txt
If errorlevel 1 color 4f & pause & exit


REM also install to MBR
echo.
echo Installing grub4dos to MBR of %USBDRIVELETTER%...
::set GR=grubinst.exe
"%~dp0LockDismount.exe" -force %dd% %GR% -t=0 %SILENT% --skip-mbr-test (hd%dd%) >> gr1.txt
If errorlevel 1 color 4f & pause & exit

if exist gr.txt del gr.txt
if exist gr1.txt del gr1.txt

popd

REM if it is not FAT32 the no point in having EFI folder for UEFI booting
if not "%FMT%"=="FAT32" if exist %USBDRIVELETTER%\EFI\nul rd /s /q %USBDRIVELETTER%\EFI > nul

REM delete this batch file from the USB drive
::echo Deleting %USBDRIVELETTER%\_ISO\docs\%~n0%~x0
::if exist %USBDRIVELETTER%\_ISO\docs\%~n0%~x0 del %USBDRIVELETTER%\_ISO\docs\%~n0%~x0

if exist %USBDRIVELETTER%\UPDATE_E2B_DRIVE.CMD del %USBDRIVELETTER%\UPDATE_E2B_DRIVE.CMD
if exist "%USBDRIVELETTER%\MAKE_E2B_USB_DRIVE (run as admin).cmd" del "%USBDRIVELETTER%\MAKE_E2B_USB_DRIVE (run as admin).cmd"
if exist %USBDRIVELETTER%\E2B_ReadMe.txt del %USBDRIVELETTER%\E2B_ReadMe.txt

REM Make new user MyE2B.cfg file
call "%~dp0Make_MyE2B.cfg.cmd" %USBDRIVELETTER%

echo.
color 2f
echo FINISHED - ALL OK.
echo.
pause
goto :EOF




:check_Permissions
   :: net session >nul 2>&1
   :: sfc 2>&1 | find /i "/SCANNOW"
    fsutil dirty query %systemdrive% >nul
    if not errorLevel 1 (
        echo Administrative permissions confirmed.
    ) else (
        echo Sorry - you need to run as Administrator [use right-click - Run as administrator].
		echo.
		echo If you ran as Administrator, please check your PATH environment variable includes the Windows\system32 folder.
		echo.
		echo PATH VARIABLE = "%PATH%"
	echo.
	color cf
	pause
	Set ADMIN=FAIL
    )
goto :eof

