@echo off


@SET WORKSPACE=%cd%
@SET STITCH_REBUILD_PATH=StitchRebuild
@SET STITCH_PATH=Stitch


@for /f "tokens=4" %%a in ('find "EC_VERSION" %WORKSPACE%\BinConfig.env') do @set EC_Version=%%a
@for /f "tokens=4" %%a in ('find "ISH_VERSION" %WORKSPACE%\BinConfig.env') do @set ISH_Version=%%a
@for /f "tokens=4" %%a in ('find "PMC_VERSION" %WORKSPACE%\BinConfig.env') do @set PMC_Version=%%a
@for /f "tokens=4" %%a in ('find "MCU_PATCH_VERSION" %WORKSPACE%\BinConfig.env') do @set Patch_Version=%%a
@for /f "tokens=4" %%a in ('find "FSP_ENABLE" %WORKSPACE%\Project.env') do @set FSP_ENABLE=%%a
@for /f "tokens=4" %%a in ('find "CSE_VERSION" %WORKSPACE%\BinConfig.env') do @set CSE_Version=%%a

@if exist %WORKSPACE%\input (
     @del /s /q %WORKSPACE%\input
)

REM REM Gen NvStore
REM   GenNvStoreFv.exe %WORKSPACE%\FV\GEMINILAKE.fd -o %WORKSPACE%\input\NvStore.Fv
REM   copy %WORKSPACE%\input\NvStore.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
REM if "%FSP_ENABLE%" == "YES" (
REM   REM Gen PAD for MEU
REM   GenPad.exe %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\xml\GELKBiosFspW.xml -o %WORKSPACE%\input\PAD.bin
REM ) else (
REM   REM Gen PAD for MEU
REM   GenPad.exe %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\xml\GELKBios.xml -o %WORKSPACE%\input\PAD.bin
REM )
REM REM Gen BPM for MEU
REM   GenBpmLite.exe -i %WORKSPACE%\FV\FVIBBL.Fv %WORKSPACE%\FV\IBBFV.Fv %WORKSPACE%\FV\RECOVERYFV.Fv -o %WORKSPACE%\input\BPM.bin
REM if "%FSP_ENABLE%" == "YES" (
REM   copy /y /b %WORKSPACE%\FV\IBBM.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
REM   copy /y /b %WORKSPACE%\FV\FSP-S.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
REM   copy /y /b %WORKSPACE%\FV\FVIBBL_FSP-T.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin\FVIBBL.Fv
REM ) else (
REM   copy %WORKSPACE%\FV\IBBFV.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
REM   copy %WORKSPACE%\FV\FVIBBL.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
REM )
REM   copy %WORKSPACE%\FV\RECOVERYFV.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
REM   copy %WORKSPACE%\FV\FVMAIN_COMPACT.Fv %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
REM   copy %WORKSPACE%\FV\NVCOMMON.fd %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU\input\module_bin
REM 
REM pushd %WORKSPACE%\CSE\%CSE_Version%\GELK\MEU
REM 
REM meu -f xml/OEMKeyManifest.xml -o oemkeymn2.bin -u1 . -key keys/bxt_dbg_priv_key.pem -w out
REM move /y oemkeymn2.bin   %WORKSPACE%\input\oemkeymn2.bin
REM if "%FSP_ENABLE%" == "YES" (
REM   meu -f %WORKSPACE%/CSE/%CSE_Version%/GELK/MEU/xml/GELKBiosFspW.xml -o bios.bin -u1 . -key keys/bxt_dbg_priv_key.pem -w out -s input
REM ) else (
REM   meu -f %WORKSPACE%/CSE/%CSE_Version%/GELK/MEU/xml/GELKBios.xml -o bios.bin -u1 . -key keys/bxt_dbg_priv_key.pem -w out -s input
REM )
REM
REM move /y bios.bin %WORKSPACE%\input\bios.bin
REM copy /y %WORKSPACE%\input\bios.bin %WORKSPACE%
REM popd

@if not exist bios.bin (
	echo. & echo   BUILD ERROR!!!
	echo --------Please create bios.bin -------------
    PAUSE
    @set exitCode=1 
    @exit /b %exitCode%@ 
)

move /y bios.bin %WORKSPACE%\input\bios.bin
copy /y %WORKSPACE%\input\bios.bin %WORKSPACE%

echo.
echo  ----------  Bios Bin File Complete  ----------


::
:: Temporary SMIP integration
:: Can be removed once FIT has integrated the layout files
:: at that time, FIT will consume the user-config.xml and create SMIP during IFWI creation
::
echo.
echo ------------------------------------------
echo.
echo   Staging SMIP data...
:: copy CSE and PMC smip to FIT\input

pushd %WORKSPACE%\SMIP
copy /y smip_iafw.bin %WORKSPACE%\input\smip_iafw.bin
popd


REM Staging PMC Patch
echo.
echo ------------------------------------------
echo.
echo   Merging PMC FW [ %PMC_Version% ] ...

if exist %WORKSPACE%\PMC\%PMC_Version%\Silicon\pmcp.bin (
    copy /y %WORKSPACE%\PMC\%PMC_Version%\Silicon\pmcp.bin %WORKSPACE%\input\
) else (
    echo %WORKSPACE%\%PMC_Version%\Silicon\pmcp.bin doesn't exist.
)

if exist %WORKSPACE%\PMC\%PMC_Version%_lpc_disable\Silicon\pmcp.bin (
    copy /y %WORKSPACE%\PMC\%PMC_Version%_lpc_disable\Silicon\pmcp.bin %WORKSPACE%\input\pmcp_lpc_disable.bin
)

@echo off

REM Merge MCU Patch
echo.
echo ------------------------------------------
echo.
echo   Merging MCU Patch [ %Patch_Version% ] ...
pushd %WORKSPACE%\Patch

GenFw.exe -m %Patch_Version%.inc -o %Patch_Version%.bin
copy /y %Patch_Version%.bin %WORKSPACE%\input\upatch1.bin

popd

xcopy input %WORKSPACE%\CSE\%CSE_Version%\GELK\FIT\input  /S /Y /D 
xcopy keys %WORKSPACE%\CSE\%CSE_Version%\GELK\FIT\keys  /S  /Y /D
echo.
echo ------------------------------------------
echo.
echo   Generating SPI Image...
pushd %WORKSPACE%\CSE\%CSE_Version%\GELK\FIT
  fit -b -f xml/glk_spi_8mb_RVP1_lpc.xml  -o glk_spi_8mb_rvp1_lpc.bin
popd

copy /b /y %WORKSPACE%\CSE\%CSE_Version%\GELK\FIT\glk_spi_8mb_rvp1_lpc.bin %WORKSPACE%\NL4050GU.bin > nul
echo.
echo  ----------  IFWI_Stitch Complete  ----------


