!BAT
# %1 has name of Windows folder,  %2 has name of XML file (user choose if blank)

# run win ISO
# load getstr batch file into memory
#load module if not already loaded
debug 0
delmod -l getstr.g4b || insmod /%grub%/getstr.g4b
debug 1

set MYISO=%ISO%
set WINDIR=%1

#get size of boot device in GB in SG (allow for 2TB drives - max grub4dos +ve number is 0x7FFFFFFFFFFFFFFF 1TB)
geometry (bd) > (md)0x333+1
cat --locate=/512 --replace=\x00 (md)0x333+1 > nul
cat --locate=nt/Size= (md)0x333+1 > nul
set /A a=%?%+8 > nul
cat --skip=%a% (md)0x333+1 | set a=
set /a a=%a%>>2 > nul
set /a a=%a%/2048 > nul
set /a SG=%a%/1024 > nul
set /a SG=%SG%<<2 > nul
if "%SG%"=="0" set SG=1

if not "%2"=="" set XML=%2 && set KXML=0 && goto :skipkey


clear
getstr.g4b $$STRb801
echo -e %STR%
#echo    Select an Unattend.XML file
#echo -e ===========================
#echo    Note: Your own .XML files should contain the correct Key
#echo    and they must run LOADISO (see example file).
#echo
#echo    Option 0 allows you to choose a Product Key.

echo
getstr.g4b $$STRb802
##debug -1
set ZZ=%STR%
#set ZZ=CHOOSE A PRODUCT KEY (uses Easy2Boot blank .xml file)
set ask=

/%grub%/PickaFile.g4b %WINDIR% .XML

#autorun using a specific key (comment out the line above)
#set XML=auwin8.xml && set KXML=1 && set KEY=32JNW-9KQ84-P47T8-D8GGY-CWCK7 && goto :skipkey

#if no user xmls then use default
if "%FCOUNT%"=="0" set CHOSEN=0
# check if user aborted
set XML=%CHOSEN%
if "%XML%"=="0" set XML=auwin8.xml
set KXML=0
if "%CHOSEN%"=="0" set KXML=1
echo
if "%XML%"==""  echo Aborting... && /%grub%/configX.g4b (bd)/%grub%/MenuWinInstall.lst

echo Using %XML%...
if "%KXML%"=="0" goto :skipkey

:PKey
clear
debug 0
getstr.g4b $$STRb803
echo -e %STR%
#echo Select a Product Key file
#echo -e =========================
echo
set ask=
set KEY=
getstr.g4b $$STRb805
set ZZ=%STR%
#set ZZ=Enter your own key

/%grub%/PickaFile.g4b %WINDIR% .KEY

#if no user xmls then use default
if "%FCOUNT%"=="0" set ask=0
# check if user aborted
if "%CHOSEN%"=="" call /%grub%/configX.g4b /%grub%/MenuWinInstall.lst
if "%CHOSEN%"=="0" goto :getkey
echo Using %WINDIR%/%CHOSEN%...
%WINDIR%/%CHOSEN%
echo
if "%KEY%"=="" echo No KEY! && pause --wait=2 && /%grub%/configX.g4b /%grub%/MenuWinInstall.lst

:getkey
echo
getstr.g4b $$STRb806
if "%CHOSEN%"=="0" echo -e %STR%
#if "%CHOSEN%"=="0" echo Product Keys must be in      AAAAA-BBBBB-CCCCC-DDDDD-EEEEE   format (29 characters)

getstr.g4b $$STRb807
if "%CHOSEN%"=="0" set /p /u KEY=%STR%
#if "%CHOSEN%"=="0" set /p /u KEY=Enter your 5x5 Product Key : 

if "%KEY%"=="" echo No KEY! && pause --wait=2 && /%grub%/configX.g4b /%grub%/MenuWinInstall.lst

# check is 29 characters!
set KL=0
if not "%KEY%"=="" if not "%KEY:~29,1%"=="" set KL=1 && echo -e \nTOO LONG!
if not "%KEY%"=="" if "%KEY%"=="%KEY:~0,28%" set KL=2 && echo -e \nTOO SHORT!
if not "%KEY%"=="" if not "%KL%"=="0" pause --wait=3 INVALID PRODUCT KEY "%KEY%"! && goto :PKey
echo
if not "%KEY%"=="" echo USING KEY %KEY%

:skipkey
clear
echo -e ISO          = %MYISO%
echo -e Unattend.XML = %XML%
if not "%KEY%"=="" echo KEY  = %KEY%
#echo Booting...
# fill (rd) with 0's , 0x82d0 is rd-base mem address, Fn24 is memset - fill memory  <addr> <string> <size>
map (md)0x300+200 (rd) > nul ;; read 0x82d0 > nul ;; call Fn.24  %@retval% 0x20  102400 > nul  

debug 1
# don't abort if error accessing other drive
errorcheck off
if not exist CD dd if=(rd)+1 of=()/AutoUnattend.xml > nul
if not exist CD if "%KXML%"=="1" dd if=(bd)/_ISO/e2b/FiraDisk/%XML% of=()/AutoUnattend.xml > nul
if not exist CD if "%KXML%"=="1" cat --locate=TK8TP --replace=%KEY% /AutoUnattend.xml > nul
if not exist CD if "%KXML%"=="0" dd if=(bd)%WINDIR%/%XML% of=()/AutoUnattend.xml > nul
# if extra USB Flash drive use Autounattend.xml on it - must have WINHELPER.USB file on it!
###findx /WINHELPER.USB && findx --set-root /WINHELPER.USB > nul &&  ls /AutoUnattend.xml > nul || echo USB DRIVE HAS WINHELPER.USB file but no AutoUnattend.xml! && pause
set WH=
if exist (hd1,0)/WINHELPER.USB root (hd1,0) > nul && set WH=1
# test hd0 in case we booted from the Helper flash drive - bugfix 2013-01-20
if "%WH%"=="" if exist (hd0,0)/WINHELPER.USB root (hd0,0) > nul && set WH=1
if "%WH%"=="" if exist (hd2,0)/WINHELPER.USB root (hd2,0) > nul && set WH=1
if "%WH%"=="" if exist (hd3,0)/WINHELPER.USB root (hd3,0) > nul && set WH=1
if "%WH%"=="" if exist (hd4,0)/WINHELPER.USB root (hd4,0) > nul && set WH=1
if "%WH%"=="" if exist (hd5,0)/WINHELPER.USB root (hd5,0) > nul && set WH=1
if "%WH%"=="" if exist (hd6,0)/WINHELPER.USB root (hd6,0) > nul && set WH=1
if "%WH%"=="" if exist (hd7,0)/WINHELPER.USB root (hd7,0) > nul && set WH=1
if "%WH%"=="" if exist (hd8,0)/WINHELPER.USB root (hd8,0) > nul && set WH=1
if "%WH%"=="" if exist (hd9,0)/WINHELPER.USB root (hd9,0) > nul && set WH=1
errorcheck on

getstr.g4b $$STRbV05
if "%WH%"=="1" if not exist /AutoUnattend.xml echo -e %STR% && pause
#USB DRIVE HAS WINHELPER.USB file but no AutoUnattend.xml!

#check if running from HDD with no HELPER USB drive
if %SG%>=128 if not "%WH%"=="1" echo
getstr.g4b $$STRbV03
if %SG%>=128 if not "%WH%"=="1" echo -e %STR%
#if %SG%>=128 if not "%WH%"=="1" echo $[0104]WARNING: To install Windows from a USB Hard disk you also need an E2B HELPER USB Flash drive. 
#if %SG%>=128 if not "%WH%"=="1" echo $[0104]If you do not have one you will get a 'DVD drive device driver is missing' message.
#if %SG%>=128 if not "%WH%"=="1" echo $[0104]Please read the Easy2Boot Tutorial for more details.
getstr.g4b $$STRbV04
if %SG%>=128 if not "%WH%"=="1" set /p ask=%STR%
#if %SG%>=128 if not "%WH%"=="1" set /p ask=Press C to continue or press [Enter] to return : 

if %SG%>=128 if not "%WH%"=="1" if not /i "%ask%"=="C" echo -e \nAborting... && /%grub%/configX.g4b (bd)/%grub%/MenuWinInstall.lst
if %SG%>=128 if not "%WH%"=="1" echo

##echo XXXX WH=%WH% KXML=%KXML% XML=%XML% KEY=%KEY%  WINDIR=%WINDIR% 

if "%WH%"=="1" dd if=(rd)+1 of=()/AutoUnattend.xml > nul
if "%WH%"=="1" if "%KXML%"=="1" dd if=(bd)/_ISO/e2b/FiraDisk/%XML% of=()/AutoUnattend.xml > nul
if "%WH%"=="1" if "%KXML%"=="1" cat --locate=TK8TP --replace=%KEY% /AutoUnattend.xml > nul
if "%WH%"=="1" if "%KXML%"=="0" dd if=(bd)%WINDIR%/%XML% of=()/AutoUnattend.xml > nul

##cat --length=200 ()/AutoUnattend.xml
##pause

set WH=
root (bd) > nul
if not exist CD dd if=(rd)+1 of=()/_ISO/e2b/firadisk/ISONAME.CMD > nul

### CHANGED 12-09-2014
if not exist CD write (bd)/_ISO/e2b/firadisk/ISONAME.CMD SET \"MYISO=%WINDIR%\\%ISO%\"\r\n > nul
if not exist CD cat --locate=/ --replace=\\ (bd)/_ISO/e2b/firadisk/ISONAME.CMD

#if not exist CD write ()/_ISO/e2b/firadisk/ISONAME.CMD SET \"MYISO=\\_iso\\Windows\\%WINDIR%\\%MYISO%\"\r\n > nul



##map --mem (md)0x800+4 (99) > nul
map %WINDIR%/%MYISO% (0xff) > nul
errorcheck on
if not exist CD map (hd0) (hd1) > nul
if not exist CD map (hd1) (hd0) > nul
map --hook > nul
##write (99) [FiraDisk]\nStartOptions=cdrom,vmem=find:%WINDIR%/%MYISO%;\n\0 > nul
chainloader (0xff)/BOOTMGR || chainloader (0xff)

#get rid of 'press any key' prompt
cat --locate=BOOTFIX.BIN --number=1 --replace=XXX (0xff)+0x1000

exit



