Add scripted build

The intel build instructions are phenomenally complicated and would have you
building an entire kernel and grub.efi.  For the purposes of the secure boot
reference platform, we don't really care what the EFI payload is, so this
scripted build simply adds the EFI Shell as the payload (meaning it won't work
with the Arduino IDE).  The build process is also coded for the Galileo Gen 1
(Kipps Bay Rev D).  If you have a Gen 2, you'll have to alter the last part
where the platform data is added.

Signed-off-by: James Bottomley <JBottomley@Parallels.com>
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..6684da2
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,7 @@
+Build
+Conf
+Report.log
+edksetup.sh
+*.d
+*.o
+*~
diff --git a/build.sh b/build.sh
new file mode 100644
index 0000000..399b5d3
--- /dev/null
+++ b/build.sh
@@ -0,0 +1,89 @@
+#!/bin/bash
+#
+# Build recipe for the git submodule based quark tree
+#
+# First initialise the submodules
+git submodule init
+git submodule update
+# add the external crypto libraries
+pushd CryptoPkg/Library/OpensslLib
+if [ ! -f openssl-0.9.8w.tar.gz ]; then
+    wget http://www.openssl.org/source/old/0.9.x/openssl-0.9.8w.tar.gz
+fi
+if [ ! -d openssl-0.9.8w ]; then
+    tar xfz openssl-0.9.8w.tar.gz
+    cd openssl-0.9.8w
+    patch -p0 <../EDKII_openssl-0.9.8w.patch
+    cd ..
+    sh ./Install.sh
+fi
+popd
+# now initialise the edk2environment using the QuarkPlatformPkg Overrides
+cp .module/edk2/edksetup.sh .
+mkdir Conf
+. ./edksetup.sh
+cp QuarkPlatformPkg/Override/BaseTools/Conf/build_rule.template Conf/build_rule.txt
+cp QuarkPlatformPkg/Override/BaseTools/Conf/tools_def.template Conf/tools_def.txt
+make -C BaseTools
+. ./edksetup.sh
+# OK now we're ready to build.  This is for the default debug package
+# alternatively, you can just comment this out and run quarkbuild.sh but
+# it does a lot of irrelevant other stuff
+type=DEBUG
+flags="-DDEBUG_PRINT_ERROR_LEVEL=0x80000042 -DDEBUG_PROPERTY_MASK=0x27"
+#type=RELEASE
+#flags="-DDEBUG_PRINT_ERROR_LEVEL=0x80000000 -DDEBUG_PROPERTY_MASK=0x23"
+##
+# You probably have gcc 4.8 or 4.9, but this doesn't seem to matter
+toolchain=GCC47
+build -a IA32 -b ${type} -y Report.log -t ${toolchain} -p QuarkPlatformPkg/QuarkPlatformPkg.dsc ${flags}
+# finally, the spi flash tools are going to need the capsule creator, so build it
+make -C QuarkPlatformPkg/Tools/CapsuleCreate
+#
+# FIXME: This should be in QuarkPlatformPkg.fdf
+# Now split the FV files apart again
+./QuarkPlatformPkg/Tools/QuarkSpiFixup/QuarkSpiFixup.py QuarkPlatform ${type} ${toolchain}
+# finally put the capsule build where the spi tools can find it
+mkdir Build/QuarkPlatform/${type}_${toolchain}/FV/Tools
+# and the shell
+cp EdkShellBinPkg/FullShell/Ia32/Shell_Full.efi Build/QuarkPlatform/${type}_${toolchain}/FV/Shell.efi
+mv QuarkPlatformPkg/Tools/CapsuleCreate/CapsuleCreate Build/QuarkPlatform/${type}_${toolchain}/FV/Tools
+##
+# Now begin the process of creating the BootRom
+##
+cd Build
+if [ ! -f spi-flash-tools_v1.0.1.tar.gz ]; then
+    wget -O spi-flash-tools_v1.0.1.tar.gz https://github.com/01org/Galileo-Runtime/blob/master/spi-flash-tools_v1.0.1.tar.gz?raw=true
+fi
+if [ ! -f sysimage_v1.0.1.tar.gz ]; then
+    wget -O sysimage_v1.0.1.tar.gz https://github.com/01org/Galileo-Runtime/blob/master/sysimage_v1.0.1.tar.gz?raw=true
+fi
+if [ ! -d spi-flash-tools_v1.0.1 ]; then
+    tar xfz spi-flash-tools_v1.0.1.tar.gz
+fi
+if [ ! -d sysimage_v1.0.1.tar.gz ]; then
+    tar xfz sysimage_v1.0.1.tar.gz
+fi
+# create our own build directory and copy in a few hand made files
+if [ ! -d mysysimage ]; then
+    mkdir mysysimage
+    pushd mysysimage
+    ln -s ../sysimage_v1.0.1/config .
+    ln -s ../sysimage_v1.0.1/inf .
+    ln -s ../QuarkPlatform/${type}_${toolchain}/FV .
+    cp ../../layout.conf .
+    popd
+    cp ../galileo-platform-data.ini spi-flash-tools_v1.0.1/platform-data/platform-data.ini
+fi
+cd mysysimage
+rm -f layout.mk
+make -f ../spi-flash-tools_v1.0.1/Makefile
+cd ../spi-flash-tools_v1.0.1/platform-data/
+./platform-data-patch.py -i ../../mysysimage/Flash-missingPDAT.bin
+mv Flash+PlatformData.bin ../../mysysimage/
+echo
+echo everything is ready, the image to flash is
+echo    Build/mysysimage/Flash+PlatformData.bin
+#
+# The flash command I use is
+#  flashrom -p buspirate_spi:dev=/dev/ttyUSB0,pullups=on -w Flash+PlatformData.bin
diff --git a/edksetup.sh b/edksetup.sh
deleted file mode 100755
index 159b20a..0000000
--- a/edksetup.sh
+++ /dev/null
@@ -1,70 +0,0 @@
-#
-# Copyright (c) 2013 Intel Corporation.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
-#
-# * Redistributions of source code must retain the above copyright
-# notice, this list of conditions and the following disclaimer.
-# * Redistributions in binary form must reproduce the above copyright
-# notice, this list of conditions and the following disclaimer in
-# the documentation and/or other materials provided with the
-# distribution.
-# * Neither the name of Intel Corporation nor the names of its
-# contributors may be used to endorse or promote products derived
-# from this software without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-# 
-# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
-# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
-#
-# In *inux environment, the build tools's source is required and need to be compiled
-# firstly, please reference https://edk2.tianocore.org/unix-getting-started.html to 
-# to get how to setup build tool.
-#
-# After build tool is downloaded and compiled, a soft symbol linker need to be created
-# at <workspace>/Conf. For example: ln -s /work/BaseTools /work/edk2/Conf/BaseToolsSource.
-#
-# Setup the environment for unix-like systems running a bash-like shell.
-# This file must be "sourced" not merely executed. For example: ". edksetup.sh"
-#
-# CYGWIN users: Your path and filename related environment variables should be
-# set up in the unix style.  This script will make the necessary conversions to
-# windows style.
-#
-# Please reference edk2 user manual for more detail descriptions at https://edk2.tianocore.org/files/documents/64/494/EDKII_UserManual.pdf
-#
-
-if [ \
-     "$1" = "-?" -o \
-     "$1" = "-h" -o \
-     "$1" = "--help" \
-   ]
-then
-  echo BaseTools Usage: \'. edksetup.sh\'
-  echo
-  echo Please note: This script must be \'sourced\' so the environment can be changed.
-  echo \(Either \'. edksetup.sh\' or \'source edksetup.sh\'\)
-  return
-fi
-
-if [ -z "$WORKSPACE" ]
-then
-  . BaseTools/BuildEnv $*
-else
-  . $WORKSPACE/BaseTools/BuildEnv $*
-fi
-
-
diff --git a/galileo-platform-data.ini b/galileo-platform-data.ini
new file mode 100755
index 0000000..ae1625e
--- /dev/null
+++ b/galileo-platform-data.ini
@@ -0,0 +1,98 @@
+# Every module contains:

+# [unique name]

+# id=decimal integer, data type identifier

+# desc=string, short description of a data; max 10 characters

+# data.value=[ABC | CAFEBEBA | xyz abc | /path/to/file ]

+# data.type=[hex.uint[8/16/32/64] | hex.string | utf8.string | file]

+# ver=decimal integer, version number; if not specified defaults to 0

+

+

+# WARNING: the platform type data.value MUST match the MRC data.value below

+[Platform Type]

+id=1

+desc=PlatformID

+data.type=hex.uint16

+# ClantonPeak 2, KipsBay 3, CrossHill 4, ClantonHill 5, KipsBay-fabD 6, GalileoGen2 8

+data.value=6

+

+

+# WARNING: the MRC data.value MUST match the platform type data.value above

+[Mrc Params]

+id=6

+ver=1

+desc=MrcParams

+data.type=file

+#data.value=MRC/clantonpeak.v1.bin

+#data.value=MRC/kipsbay.v1.bin

+#data.value=MRC/crosshill.v1.bin

+#data.value=MRC/clantonhill.v1.bin

+data.value=MRC/kipsbay-fabD.v1.bin

+#data.value=MRC/GalileoGen2.bin

+

+

+# If you are developing MRC for a new system you can alternatively

+# inline the value like this:

+

+# data.type=hex.string

+# data.value=00000000000000010101000300000100010101017C9200001027000010270000409C000006

+

+# The unique MAC address(es) owned by each device are typically found

+# on a sticker.  You must find it(them) and change the bogus values

+# below.

+[MAC address 0]

+id=3

+desc=1st MAC

+data.type=hex.string

+data.value=001320FDF6F0

+

+[MAC address 1]

+id=4

+desc=2nd MAC

+data.type=hex.string

+data.value=001320FDF6F1

+

+#

+# MRC Params data byte explanation.

+# u32, u16 vales read with least significant byte first, little endian.

+#

+# data[00-01] - platform_id       u16 - should match value of platform type/id record as sanity check.

+# data[02-05] - flags             u32 - bit[0] ECC_EN, bit[1] SCRAMBLE_EN, others=RESERVED

+# data[06]    - dram_width        u8  - 0=x8, 1=x16, others=RESERVED.

+# data[07]    - dram_speed        u8  - 0=DDRFREQ_800, 1=DDRFREQ_1066, others=RESERVED. Only 533MHz SKU support 1066 memory.

+# data[08]    - dram_type         u8  - 0=DDR3, 1=DDR3L, others=RESERVED.

+# data[09]    - rank_mask         u8  - bit[0] RANK0_EN, bit[1] RANK1_EN, others=RESERVED.

+# data[0A]    - chan_mask         u8  - bit[0] CHAN0_EN, others=RESERVED..

+# data[0B]    - chan_width        u8  - 1=x16, others=RESERVED.

+# data[0C]    - addr_mode         u8  - 0, 1, 2 (mode 2 forced if ecc enabled), others=RESERVED

+# data[0D]    - sr_int            u8  - 1=1.95us, 2=3.9us, 3=7.8us, others=RESERVED. REFRESH_RATE.

+# data[0E]    - sr_temp           u8  - 0=normal, 1=extended, others=RESERVED. SR_TEMP_RANGE.

+# data[0F]    - dram_ron_val      u8  - 0=34ohm, 1=40ohm, others=RESERVED. RON_VALUE Select MRS1.DIC driver impedance control.

+# data[10]    - dram_rtt_nom_val  u8  - 0=40ohm, 1=60ohm, 2=120ohm, others=RESERVED. RTT_NOM_VALUE.

+# data[11]    - dram_rtt_wr_val   u8  - 0=off, others=RESERVED

+# data[12]    - soc_rd_odt_val    u8  - 0=off, 1=60ohm, 2=120ohm, 3=180ohm, others=RESERVED. RD_ODT_VALUE.

+# data[13]    - soc_wr_ron_val    u8  - 0=27ohm, 1=32ohm, 2=40ohm, others=RESERVED. (WR_RON_VALUE select Vref code for DQ DRV PU evaluation using external resistor).

+# data[14]    - soc_wr_slew       u8  - 0=2.5V/ns, 1=4V/ns, others=RESERVED. (WR_SLEW_RATE).

+# data[15]    - dram_density      u8  - 0=512Mb, 1=Gb, 2=2Gb, 3=4Gb, others=RESERVED.

+# data[16-19] - tRAS              u32 - ACT to PRE command period in picoseconds.

+# data[1A-1D] - tWTR              u32 - Delay from start of internal write transaction to internal read command in picoseconds.

+# data[1E-21] - tRRD              u32 - ACT to ACT command period (JESD79 specific to page size 1K/2K) in picoseconds.

+# data[22-25] - tFAW              u32 - Four activate window (JESD79 specific to page size 1K/2K) in picoseconds.

+# data[26]    - tCL               u8  - DRAM CAS Latency in clocks.

+#

+#  Example DDR3-800, Single Rank, 1gbit devices.

+#   byte index  FFFF000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324

+#   byte values FFFF00000000000000010101000300000100020100017C9200001027000010270000409C000006

+#

+#  Example DDR3-800, Single Rank, 2gbit devices.

+#   byte index  FFFF000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324

+#   byte values FFFF00000000000000010101000300000100020100027C9200001027000010270000409C000006

+#

+#  Example DDR3-800, Single Rank, 4gbit devices.

+#   byte index  FFFF000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324

+#   byte values FFFF00000000000000010101000300000100020100037C9200001027000010270000409C000006

+#

+#  Example DDR3-800, Dual Rank, 4gbit devices

+#   byte index  FFFF000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324

+#   byte values FFFF00000000000000030101000300000100020100037C9200001027000010270000409C000006

+#

+# byte idx 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20212223242526

diff --git a/layout.conf b/layout.conf
new file mode 100644
index 0000000..cc37206
--- /dev/null
+++ b/layout.conf
@@ -0,0 +1,125 @@
+

+# WARNING: this file is indirectly included in a Makefile where it

+# defines Make targets and pre-requisites. As a consequence you MUST

+# run "make clean" BEFORE making changes to it. Failure to do so may

+# result in the make process being unable to clean files it no longer

+# has references to.

+

+[main]

+size=8388608

+type=global

+

+

+[MFH]

+version=0x1

+flags=0x0

+address=0xfff08000

+type=mfh

+

+

+[Flash Image Version]

+type=mfh.version

+meta=version

+value=0x01000105

+

+[ROM_OVERLAY]

+address=0xfffe0000

+item_file=FV/FlashModules/EDKII_BOOTROM_OVERRIDE.Fv

+type=some_type

+

+[signed-key-module]

+address=0xfffd8000

+item_file=config/SvpSignedKeyModule.bin

+svn_index=0

+type=some_type

+in_capsule=no

+

+# On a deployed system, the SVN area holds the last known secure

+# version of each signed asset.

+# TODO: generate this area by collecting the SVN from the assets

+# themselves.

+[svn-area]

+address=0xfffd0000

+item_file=config/SVNArea.bin

+type=some_type

+# A capsule upgrade must implement some smart logic to make sure the

+# highest Security Version Number always wins (rollback protection)

+in_capsule=no

+

+[fixed_recovery_image]

+address=0xfff90000

+item_file=FV/FlashModules/EDKII_RECOVERY_IMAGE1.Fv

+sign=yes

+type=mfh.host_fw_stage1_signed

+svn_index=2

+# in_capsule=no

+

+[NV_Storage]

+address=0xfff30000

+item_file=FV/FlashModules/EDKII_NVRAM.bin

+type=some_type

+

+[RMU]

+address=0xfff00000

+item_file=FV/FlashModules/RMU.bin

+type=none_registered

+

+[boot_stage1_image1]

+address=0xffec0000

+item_file=FV/FlashModules/EDKII_BOOT_STAGE1_IMAGE1.Fv

+sign=yes

+boot_index=0

+type=mfh.host_fw_stage1_signed

+svn_index=1

+

+[boot_stage1_image2]

+address=0xffe80000

+item_file=FV/FlashModules/EDKII_BOOT_STAGE1_IMAGE2.Fv

+sign=yes

+boot_index=1

+type=mfh.host_fw_stage1_signed

+svn_index=1

+

+[boot_stage_2_compact]

+address=0xffd00000

+item_file=FV/FlashModules/EDKII_BOOT_STAGE2_COMPACT.Fv

+sign=yes

+type=mfh.host_fw_stage2_signed

+svn_index=3

+

+#[Ramdisk]

+#address=0xffa60000

+#item_file=Payload/initrd.lzma

+#sign=yes

+#type=mfh.ramdisk_signed

+#svn_index=7

+

+[LAYOUT.CONF_DUMP]

+address=0xffcff000

+type=mfh.build_information

+meta=layout

+

+#[Kernel]

+#address=0xff852000

+#item_file=Payload/bzImage

+#sign=yes

+#type=mfh.kernel_signed

+#svn_index=6

+

+#[grub.conf]

+#address=0xff851000

+#item_file=grub/grub-spi.conf

+#sign=yes

+#type=mfh.bootloader_conf_signed

+#svn_index=5

+

+# instead of the arduino linux payload, just add an EFI shell

+[grub]

+address=0xff800000

+item_file=FV/Shell.efi

+sign=yes

+fvwrap=yes

+guid=B43BD3E1-64D1-4744-9394-D0E1C4DE8C87

+type=mfh.bootloader_signed

+svn_index=4

+