# Use for installing Win8 or SVR2012 from a menu (no user choices)
# see also $$AddWin2Main.mnu  for a more compact menu

title Install Win8 ISO \n Install Windows with a specific Key
# run win ISO
debug 1
# run win ISO - ISO MUST BE IN \_ISO\WINDOWS\xxx folder

set MYISO=Win8.iso
set WINDIR=WIN8

#Specify KEY and generic XML file
set XML=auwin8.xml 
set KEY=32JNW-9KQ84-P47T8-D8GGY-CWCK7
# if KXML=1 then will use auwin8.xml and replace key with the KEY above
set KXML=1

# if you want to use a specific XML file from the \_ISO\WINDOWS\WIN8 folder, then use
###set XML=abcdef.xml
###set KXML=0
###set KEY=

#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

# check KEY 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%"!
echo
set KL=

echo -e ISO          = %MYISO%
echo -e Unattend.XML = %XML%
if not "%KEY%"=="" echo KEY  = %KEY%


# 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
errorcheck on
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)/_ISO/Windows/%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


if "%WH%"=="1" if not exist /AutoUnattend.xml echo -e $$STRbV05 && 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

if %SG%>=128 if not "%WH%"=="1" echo -e $$STRbV03
#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.

if %SG%>=128 if not "%WH%"=="1" set /p ask=$$STRbV04
#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

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)/_ISO/Windows/%WINDIR%/%XML% of=()/AutoUnattend.xml > nul

set WH=
root (bd) > nul
if not exist CD dd if=(rd)+1 of=()/_ISO/e2b/firadisk/ISONAME.CMD > nul
if not exist CD write ()/_ISO/e2b/firadisk/ISONAME.CMD SET \"MYISO=\\_iso\\Windows\\%WINDIR%\\%MYISO%\"\r\n > nul
map /_ISO/Windows/%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
chainloader (0xff)/BOOTMGR || chainloader (0xff)

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

boot
exit



