@echo off
@rem  This script is called by x:\Windows\System32\Startnet.cmd - WinPE
@echo.
@echo.
@echo.
@echo.
@echo.
@echo Running Intel Optimization powercfg for improved imaging times
@echo.
@echo call powercfg /s 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
call powercfg /s 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
@echo.
@rem  *****************************************************************
@rem  List all the disks, then search the volumes of each one to see 
@rem  which disks may have "Winpe" in the label. We will ignore that 
@rem  disk and use the first disk without the Winpe label that we find. 
@rem  *****************************************************************
@echo call diskpart /s %WinPESource%DiskpartFiles\listdisk.txt
call diskpart /s %WinPESource%DiskpartFiles\listdisk.txt>x:\Disks.txt
@echo.
@rem Set Disk equal to blank to start
set disk=
@echo.
@rem 
@rem  This line will write a diskpart script to select the disk and detail it's contents.
@rem  We use this output to find a disk that does NOT have Winpe as the volume label.
@rem  That disk will have the OS installed to it. This way you can exclude USB from diskpart.
@rem  
@rem  This works around the issue where the USB key is sometimes enumerated as disk 0 and the 
@rem  SSD is disk 1. 
@rem  
for /f "skip=8 tokens=2" %%A in (x:\Disks.txt) do (
@echo sel disk %%A>x:\Par.txt
@echo det disk>>x:\Par.txt
@echo exit>>x:\Par.txt
if NOT "%%A"=="DiskPart..." call :ListPar %%A
)
if %Disk%=="" echo No disk found without a volume label of "Winpe"&& set disk=0
@echo Using Disk "%Disk%"
@echo.
@echo.
@echo.
@echo *****************************************************************
@echo  Write the diskpart script now that we know what disk to use
@echo *****************************************************************
@echo.>x:\wimpart.txt
@echo select disk "%Disk%">>x:\wimpart.txt
@echo clean>>x:\wimpart.txt
@echo convert gpt>>x:\wimpart.txt
@echo create partition efi size=100>>x:\wimpart.txt
@echo format quick fs=fat32 label="System">>x:\wimpart.txt
@echo assign letter="S">>x:\wimpart.txt
@echo create partition msr size=128>>x:\wimpart.txt
@echo create partition primary>>x:\wimpart.txt
@echo shrink minimum=5500>>x:\wimpart.txt
@echo format quick fs=ntfs label="Windows">>x:\wimpart.txt
@echo assign letter="W">>x:\wimpart.txt
@echo create partition primary>>x:\wimpart.txt
@echo format quick fs=ntfs label="Images">>x:\wimpart.txt
@echo assign letter="M">>x:\wimpart.txt
@echo set id="de94bba4-06d1-4d40-a16a-bfd50179d6ac">>x:\wimpart.txt
@echo gpt attributes=0x8000000000000001>>x:\wimpart.txt
@echo exit>>x:\wimpart.txt
@echo.
@echo Wimpart.txt now contains.....
@echo.
type x:\wimpart.txt
@echo.
@echo.
@echo *****************************************************************
@echo  Setting up the partition table
@echo *****************************************************************
@echo call diskpart /s x:\wimpart.txt
diskpart /s x:\wimpart.txt
@echo.
@echo.
@echo.
@echo *****************************************************************
@rem  Map drive letter for Install.wim and Winre.wim
@echo *****************************************************************
@rem Create an output file containing all the drive letters
@echo call diskpart /s %WinPESource%DiskpartFiles\listVol.txt>%WinPESource%Output.txt
diskpart /s %WinPESource%DiskpartFiles\listVol.txt>%WinPESource%Output.txt
@echo.
@rem Go through each drive letter, looking for the images\Install.txt file
for /f "skip=8 tokens=3" %%A in (%WinPESource%Output.txt) do (
if exist %%A:\images\install.txt set InstallPath=%%A:\images&& echo "Found device with images folder and install.txt file"
)
@echo.
@echo.
@rem Check InstallPath was found and there are images in it.
rem if NOT EXIST %InstallPath%\Install.wim echo "Install.wim NOT found." && goto :Error
rem @echo Install.wim FOUND at %InstallPath%\images\install.wim






@echo.
@rem Check that Winre.wim is there as well.
if NOT EXIST %InstallPath%\Winre.wim echo "Winre.wim NOT found." && goto :Error
@echo Winre.wim FOUND at %InstallPath%\images\winre.wim
@echo.
@echo.
@echo.
@echo *****************************************************************
@rem  Switches to the USB disk
@rem  Create folders on local (eMMC) disk and copy files from USB disk
@echo *****************************************************************
@echo.
@echo Making directories...
md w:\recycler\scratch
if %errorlevel% NEQ 0 echo "Failed to make w:\recycler\scratch directory" && goto :error
md m:\"windows images"
if %errorlevel% NEQ 0 echo "Failed to make m:\windows images directory" && goto :error
md m:\recovery\windowsre
if %errorlevel% NEQ 0 echo "Failed to make m:\recovery\windowsre directory" && goto :error
@echo.
set var=0
:copy_install
set /a var+=1
@echo var is %var%
if EXIST %InstallPath%\Install.wim (
@echo Install.wim FOUND at %InstallPath%\images\install.wim
@echo copy %InstallPath%\install.wim m:\"windows images"
copy %InstallPath%\install.wim m:\"windows images"
if %errorlevel% NEQ 0 echo "Failed to copy install.wim to m:\Windows images" && goto :error
) else (
echo "Install.wim  NOT found."
echo "Find Install.swm"
if NOT EXIST %InstallPath%\Install.swm echo "Install.swm  NOT found." && goto :Error
DISM /Export-image /SourceImageFile:%InstallPath%\install.swm /SwmFile:%InstallPath%\install*.swm /SourceIndex:1 /DestinationImageFile:"m:\windows images\install.wim" /WIMBoot /CheckIntegrity
)
rem %WinpeSource%\Scripts\fciv.exe -v -md5 -r "m:\windows images\install.wim" -xml %WinpeSource%\Scripts\install.xml
rem if %errorlevel% NEQ 0 echo "md5 check install.wim  error" &&if  %var% lss 3 (goto :copy_install) else (goto :error)

@echo.
set var=0
:copy_winre
set /a var+=1
@echo var is %var%
@echo copy %InstallPath%\winre.wim m:\recovery\windowsre
copy %InstallPath%\winre.wim m:\recovery\windowsre
if %errorlevel% NEQ 0 echo "Failed to copy winre.wim to m:\recovery\windowsre" && goto :error
rem %WinpeSource%\Scripts\fciv.exe -v -md5 -r m:\recovery\windowsre\winre.wim -xml %WinpeSource%\Scripts\winre.xml
rem if %errorlevel% NEQ 0 echo "md5 check winre.wim  error" &&if  %var% lss 3 (goto :copy_winre) else (goto :error)
@echo.
@echo.
@echo.
@echo *****************************************************************
@echo  Applying the WIMboot image from the Images Partition in the eMMC
@echo *****************************************************************
@echo call DISM /Apply-Image /ImageFile:"m:\windows images\install.wim" /ApplyDir:w: /Index:1 /WIMBoot /ScratchDir:w:\recycler\SCRATCH
DISM /Apply-Image /ImageFile:"m:\windows images\install.wim" /ApplyDir:w: /Index:1 /WIMBoot /ScratchDir:w:\recycler\SCRATCH /CheckIntegrity
if %errorlevel% NEQ 0 echo "Failed to apply image to w:" && goto :error
@echo.
@echo.
@echo.
@echo *****************************************************************
@echo  Copy over a custom unattend.xml if it exists to Windows\Panther
@echo *****************************************************************
@echo.
@echo.
set var=0
:copy_unattend
set /a var+=1
@echo var is %var%
@echo if EXIST %WinpeSource%images\audit\unattend.xml xcopy %WinpeSource%images\audit\unattend.xml w:\Windows\Panther /y
if EXIST %WinpeSource%images\audit\unattend.xml xcopy %WinpeSource%images\audit\unattend.xml w:\Windows\Panther /y
rem %WinpeSource%\Scripts\fciv.exe -v -md5 -r w:\Windows\Panther\unattend.xml -xml %WinpeSource%\Scripts\una.xml
rem if %errorlevel% NEQ 0 echo "md5 check unattend.xml  error" &&if  %var% lss 3 (goto :copy_unattend) else (goto :error)



@echo.
@echo *****************************************************************
@echo  Unzip TEST_TOOL
@echo *****************************************************************
set var=0
:unzip_testtool
if EXIST w:\TEST_TOOL rmdir /s /q w:\TEST_TOOL
set /a var+=1
@echo var is %var%
@echo %WinpeSource%Scripts\7za.exe x %WinpeSource%TEST_TOOL.zip -oW:\
%WinpeSource%Scripts\7za.exe x  %WinpeSource%TEST_TOOL.zip -oW:\
if %errorlevel% NEQ 0 echo "Error when run %WinpeSource%Scripts\7za.exe x  WinpeSource%TEST_TOOL.zip" && goto :error
rem %WinpeSource%\Scripts\fciv.exe -v -md5 -r w:\TEST_TOOL -xml %WinpeSource%\Scripts\testtool.xml
rem if %errorlevel% NEQ 0 echo "md5 check W:\TEST_TOOL" &&if  %var% lss 3 (goto :unzip_testtool) else (goto :error)

@echo.
@echo *****************************************************************
@echo  Unzip OA3Tool
@echo *****************************************************************
set var=0
:unzip_oatool
if EXIST w:\OA3Tool rmdir /s /q w:\OA3Tool
set /a var+=1
@echo var is %var%
@echo %WinpeSource%Scripts\7za.exe x %WinpeSource%OA3Tool.zip -oW:\
%WinpeSource%Scripts\7za.exe x  %WinpeSource%OA3Tool.zip -oW:\
if %errorlevel% NEQ 0 echo "Error when run %WinpeSource%Scripts\7za.exe x  WinpeSource%OA3Tool.zip" && goto :error
rem %WinpeSource%\Scripts\fciv.exe -v -md5 -r w:\OA3Tool -xml %WinpeSource%\Scripts\oatool.xml
rem if %errorlevel% NEQ 0 echo "md5 check w:\OA3Tool" &&if  %var% lss 3 (goto :unzip_oatool) else (goto :error)

del w:\setup.txt
@echo.
@echo.
@echo.
@echo *****************************************************************
@echo  Setting the boot environment
@echo *****************************************************************
@echo call w:\WINDOWS\SYSTEM32\BCDBOOT w:\WINDOWS /s s: /f all
w:\WINDOWS\SYSTEM32\BCDBOOT w:\WINDOWS /s s: /f all
if %errorlevel% NEQ 0 echo "Failed to set boot information" && goto :error
@echo.
@echo.
@echo.
@echo *****************************************************************
@echo  Setting the recovery environment
@echo *****************************************************************
@echo w:\windows\system32\reagentc /SetREImage /Path M:\RECOVERY\WINDOWSRE /target w:\windows
w:\windows\system32\reagentc /SetREImage /Path M:\RECOVERY\WINDOWSRE /target w:\windows
if %errorlevel% NEQ 0 echo "Failed to set recovery information" && goto :error
@echo.

@echo.
@echo.
@echo.
@echo *****************************************************************
@echo  Setting the recovery winre boot environment
@echo *****************************************************************
mkdir S:\EFI\Recovery
xcopy /e /h S:\EFI\Microsoft\* S:\EFI\Recovery\
del /a S:\EFI\Recovery\Boot\BCD
del /a S:\EFI\Recovery\Boot\BCD.LOG
bcdedit /createstore S:\BCD

bcdedit /store S:\BCD /create {bootmgr} /d "Windows Boot Manager"
bcdedit /store S:\BCD /set {bootmgr} device partition=S:
bcdedit /store S:\BCD /set {bootmgr} locale en-US
bcdedit /store S:\BCD /set {bootmgr} integrityservices Enable

bcdedit /store S:\BCD /create {11111111-1111-1111-1111-111111111111} /d "Windows Recovery" /device
bcdedit /store S:\BCD /set {11111111-1111-1111-1111-111111111111} ramdisksdidevice partition=M:
bcdedit /store S:\BCD /set {11111111-1111-1111-1111-111111111111} ramdisksdipath \Recovery\WindowsRE\boot.sdi

bcdedit /store S:\BCD /create {22222222-2222-2222-2222-222222222222} /d "Windows Recovery Environment" /application osloader
bcdedit /store S:\BCD /set {bootmgr} default {22222222-2222-2222-2222-222222222222}
bcdedit /store S:\BCD /set {bootmgr} displayorder {22222222-2222-2222-2222-222222222222}

bcdedit /store S:\BCD /set {default} device ramdisk=[M:]\Recovery\WindowsRE\winre.wim,{11111111-1111-1111-1111-111111111111}
bcdedit /store S:\BCD /set {default} path \Windows\System32\winload.efi
bcdedit /store S:\BCD /set {default} locale en-US
bcdedit /store S:\BCD /set {default} displaymessage "Recovery"
bcdedit /store S:\BCD /set {default} osdevice ramdisk=[M:]\Recovery\WindowsRE\winre.wim,{11111111-1111-1111-1111-111111111111}
bcdedit /store S:\BCD /set {default} systemroot \Windows
bcdedit /store S:\BCD /set {default} nx OptIn
bcdedit /store S:\BCD /set {default} bootmenupolicy Standard
bcdedit /store S:\BCD /set {default} winpe Yes

xcopy /h S:\BCD* S:\EFI\Recovery\Boot\.
del /a S:\BCD*
@echo.

rmdir /s /q w:\recycler
set /a x=%random%
if not exist %WinpeSource%\result mkdir %WinpeSource%\result
@echo  ***********w:\TEST_TOOL\START_TEST.cmd************************* >> %WinpeSource%\result\%x%.txt
type  w:\TEST_TOOL\START_TEST.cmd >> %WinpeSource%\result\%x%.txt
@echo  ***********w:\TEST_TOOL\START_FUNCTIONAL_TEST.cmd************************* >> %WinpeSource%\result\%x%.txt
type  w:\TEST_TOOL\START_FUNCTIONAL_TEST.cmd >> %WinpeSource%\result\%x%.txt
@echo  ***********w:\TEST_TOOL\CleanupTestTool.cmd************************* >> %WinpeSource%\result\%x%.txt
type  w:\TEST_TOOL\CleanupTestTool.cmd>> %WinpeSource%\result\%x%.txt
@echo.
@echo.
@echo.
@echo.
@echo *****************************************************************
@echo  WIMboot image deployment COMPLETE. Type EXIT from Command
@echo  Prompt to restart or turn off device.
color 2f
@echo *****************************************************************
goto :END

:ERROR
@echo.
@echo An error has been detected.
color 4f
@echo. 
goto :END

:ListPar
@REM This section is to bring a disk number and use it to List Partitions
@echo.
set TempDisk=%1
diskpart /s x:\Par.txt>x:\ParOutput.txt
for /f "skip=24 tokens=3" %%A in (x:\ParOutput.txt) do (
if /i "%%A"=="no" set Disk=%TempDisk%
)
for /f "skip=26 tokens=4" %%A in (x:\ParOutput.txt) do (
if /i NOT "%%A"=="Winpe" set Disk=%TempDisk%
)
Goto :EOF

:END
@rem 
@rem  Get finish time so we can evaluate total WinPE phase time.
@rem
call |time>x:\StopTime.txt
@rem
@rem  Now parse both files to display the Start and Finish Time
@rem
@echo.
@echo.
for /f "tokens=5" %%A in (x:\starttime.txt) do echo Script Start Time is %%A
for /f "tokens=5" %%A in (x:\stoptime.txt) do echo  Script Stop Time is %%A
@echo.