\subsection{Bootloader \& kernel}

\begin{frame}[fragile]{Trust (almost) no one
  \hfill\includegraphics[width=.5\linewidth]{drawings/00_banner-bootloader.pdf}\hspace*{0.5em}}
  \begin{itemize}
  \item no point in having a secure bootloader if not authenticated by ROM
  code
  \item bootloader \textbf{has} to be sufficiently locked-down,
  otherwise there is no point authenticating it
  \item specific case of U-Boot mainline: \textbf{has} to be
  inaccessible by anyone (no console at all:
  \code{gd->flags |= GD_FLG_DISABLE_CONSOLE} in
  \code{board_early_init_f()})
%example of the bootcmd/verify that could be modified
  \item under no circumstances should you trust anything that isn't
  in the U-Boot binary that is authenticated by the ROM code
%example of the verify U-Boot environment
  \item by default, the environment can be trusted \textbf{only} if it's in
  the U-Boot binary (\code{ENV_IS_NOWHERE})
  \item \code{env import [addr] [size] [vars...]} since 2018.09 for loading
  only a handful of variables from another environment, limiting the
  attack vector
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Kernel and DTB authentication
  \hfill\includegraphics[width=.4\linewidth]{drawings/00_banner-bootloader.pdf}\hspace*{0.5em}}
  \begin{itemize}
  \item U-Boot has DeviceTree Blob (DTB) support, used the same way
  the kernel does to probe drivers: according to the DT definition
  \item DTB can also be used to store a public key
  \item DTB is appended to the U-Boot binary and is thus affected by the
  computation of the hash used by the ROM code to authenticate the
  bootloader => can be trusted
  \item fitImage to have only one file containing binaries and
  signatures instead of lots of images to load
  \item \code{mkimage} (the tool to compile fitImages) has built-in support for
  signing of binaries hash
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Key generation
  \hfill\includegraphics[width=.5\linewidth]{drawings/00_banner-bootloader.pdf}\hspace*{0.5em}}
  \begin{itemize}
  \item \code{openssl genrsa -out my_key.key 4096}
  %FIXME: certificate is only valid for 30days
  \item \code{openssl req -batch -new -x509 -key my_key.key -out my_key.crt}
  \item \code{mkimage} requires certificate and private key files to be named
  the same
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{DTB creation
  \hfill\includegraphics[width=.5\linewidth]{drawings/00_banner-bootloader.pdf}\hspace*{0.5em}}
\begin{block}{u-boot\_pubkey.dts}
\fontsize{8}{8}\selectfont
\begin{minted}{c}
/dts-v1/;
/ {
    model = "Keys";
    compatible = "vendor,board";
    signature {
        key-my_key {
          required = "image";
          algo = "sha1,rsa4096";
          key-name-hint = "my_key";
        };
    };
};
\end{minted}
\end{block}
  \begin{itemize}
  \item \code{key-name-hint} and the suffix to the \code{key-} DT node has
  to be the same name as the one given to the key
  \item \code{required} is either \code{image} or \code{conf}, refer to
  \code{doc/uImage.FIT/signature.txt}
  %give a quick explanation anyway
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{What's a fitImage?
  \hfill\includegraphics[width=.5\linewidth]{drawings/00_banner-bootloader.pdf}\hspace*{0.5em}}
\begin{columns}
\begin{column}{0.45\textwidth}
  \includegraphics[width=0.98\linewidth]{drawings/03_fitimage.pdf}
\end{column}
\begin{column}{0.45\textwidth}
  \begin{itemize}
  \item several talks given to present the fitImage, the reasons behind
  and the challenges
%FIXME: give the links to the slides/talks
  \item it's basically a container for multiple binaries with hashing and
  signature support
  \item it also supports forcing a few binaries to be loaded together,
  \item supports different architectures, OSes, image types, ... =>
  can be found in \code{common/image.c}
  \end{itemize}
\end{column}
\end{columns}
\end{frame}

\begin{frame}[fragile]{fitImage.its
  \hfill\includegraphics[width=.5\linewidth]{drawings/00_banner-bootloader.pdf}\hspace*{0.5em}}
\begin{block}{}
\begin{columns}
\begin{column}{0.45\textwidth}
\fontsize{7}{6}\selectfont
\begin{minted}{c}
/ {
    description = "fitImage for Foo revA and revB";
    #address-cells = <1>;
    images {
        kernel@1 {
            description = "Linux kernel";
            data = /incbin/("zImage");
            type = "kernel";
            arch = "arm";
            os = "linux";
            compression = "none";
            load = <0x10008000>;
            entry = <0x10008000>;
            signature@1 {
                algo = "sha1,rsa4096";
                key-name-hint = "my_key";
            };
        };
        fdt@1 {
            description = "DTB for Foo revA";
            data = /incbin/("foo-reva.dtb");
            type = "flat_dt";
            arch = "arm";
            compression = "none";
            signature@1 {
                algo = "sha1,rsa4096";
                key-name-hint = "my_key";
            };
        };
\end{minted}
\end{column}
\begin{column}{0.45\textwidth}
\fontsize{7}{6}\selectfont
\begin{minted}{c}
        fdt@2 {
            description = "DTB for Foo revB";
            data = /incbin/("foo-revb.dtb");
            type = "flat_dt";
            arch = "arm";
            compression = "none";
            signature@1 {
                algo = "sha1,rsa4096";
                key-name-hint = "my_key";
            };
        };
    };
    configurations {
        default = "conf@1";
        conf@1 {
            kernel = "kernel@1";
            fdt = "fdt@1";
        };
        conf@2 {
            kernel = "kernel@1";
            fdt = "fdt@2";
        };
    };
};
\end{minted}
\end{column}
\end{columns}
\end{block}
\end{frame}

\begin{frame}[fragile]{U-Boot \& fitImage creation
  \hfill\includegraphics[width=.5\linewidth]{drawings/00_banner-bootloader.pdf}\hspace*{0.5em}}
\begin{block}{}
\fontsize{8}{8}\selectfont
\begin{minted}{bash}
#DTB compiled out-of-tree because we need to add the public key with \code{mkimage}
dtc u-boot_pubkey.dts -O dtb -o u-boot_pubkey.dtb
make CROSS_COMPILE=arm-linux-gnueabihf- foo_defconfig
make CROSS_COMPILE=arm-linux-gnueabihf- tools
tools/mkimage -f fitImage.its -K u-boot_pubkey.dtb -k /path/to/keys -r fitImage
make CROSS_COMPILE=arm-linux-gnueabihf- EXT_DTB=u-boot_pubkey.dtb
\end{minted}
%check this works and compiles the fitImage as well
\end{block}
\end{frame}

\begin{frame}[fragile]{U-Boot required options
  \hfill\includegraphics[width=.5\linewidth]{drawings/00_banner-bootloader.pdf}\hspace*{0.5em}}
  \begin{itemize}
  \item \code{CONFIG_SECURE_BOOT=y} (specific to NXP)
  \item \code{#ifdef CONFIG_SECURE_BOOT}\newline
  \code{CSF CONFIG_CSF_SIZE}\newline\code{#endif}, at the beginning of
  the DCD file of your NXP board
  \item \code{CONFIG_OF_CONTROL=y}
  \item \code{CONFIG_DM=y}, \code{CONFIG_FIT=y}, \code{CONFIG_FIT_SIGNATURE=y}
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{fitImage booting
  \hfill\includegraphics[width=.5\linewidth]{drawings/00_banner-bootloader.pdf}\hspace*{0.5em}}
with a fitImage loaded @ 0x15000000:
\begin{block}{}
\fontsize{7}{6}\selectfont
\begin{minted}{bash}
=> bootm 0x15000000 #or bootm 0x15000000#conf@1 since conf@1 is the default
## Loading kernel from FIT Image at 15000000 ...
   Using 'conf@1' configuration
   Verifying Hash Integrity ... OK
   Trying 'kernel@1' kernel subimage
     Description:  Linux kernel
     Type:         Kernel Image
     Compression:  uncompressed
     Data Start:   0x150000e4
     Data Size:    7010496 Bytes = 6.7 MiB
     Architecture: ARM
     OS:           Linux
     Load Address: 0x10008000
     Entry Point:  0x10008000
     Hash algo:    sha1
     Hash value:   7d1fb52f2b8d1a98d555e01bc34d11550304fc26
     Sign algo:    sha1,rsa4096:my_key
     Sign value:   [redacted]
   Verifying Hash Integrity ... sha1,rsa4096:my_key+ sha1+ OK
## Loading fdt from FIT Image at 15000000 ...
   Using 'conf@1' configuration
   Trying 'fdt@1' fdt subimage
   [...]
   Verifying Hash Integrity ... sha1,rsa4096:my_key+ sha1+ OK
   Booting using the fdt blob at 0x156afd40
   Loading Kernel Image ... OK
   Loading Device Tree to 1fff2000, end 1ffff1ed ... OK

Starting kernel...
\end{minted}
\end{block}
\end{frame}

\begin{frame}[fragile]{fitImage booting
  \hfill\includegraphics[width=.5\linewidth]{drawings/00_banner-bootloader.pdf}\hspace*{0.5em}}
with a fitImage loaded @ 0x15000000:
\begin{block}{}
\fontsize{7}{6}\selectfont
\begin{minted}{bash}
=> bootm 0x15000000 #or bootm 0x15000000#conf@1 since conf@1 is the default
## Loading kernel from FIT Image at 15000000 ...
   Using 'conf@1' configuration
   Verifying Hash Integrity ... OK
   Trying 'kernel@1' kernel subimage
     Description:  Linux kernel
     Type:         Kernel Image
     Compression:  uncompressed
     Data Start:   0x150000e4
     Data Size:    7010496 Bytes = 6.7 MiB
     Architecture: ARM
     OS:           Linux
     Load Address: 0x10008000
     Entry Point:  0x10008000
     Hash algo:    sha1
     Hash value:   7d1fb52f2b8d1a98d555e01bc34d11550304fc26
     Sign algo:    sha1,rsa4096:my_key
     Sign value:   [redacted]
   Verifying Hash Integrity ... sha1,rsa4096:my_key+ sha1+ OK
## Loading fdt from FIT Image at 15000000 ...
   Using 'conf@1' configuration
   Trying 'fdt@1' fdt subimage
   Verifying Hash Integrity ... sha1,rsa4096:my_key- Failed to verify required signature 'key-my_key'
 error!
Unable to verify required signature for '' hash node in 'fdt@1' image node
Bad Data Hash
   Booting using the fdt blob at 0x156ba280
   Loading Kernel Image ... OK
ERROR: image is not a fdt - must RESET the board to recover.
FDT creation failed! hanging...### ERROR ### Please RESET the board ###
\end{minted}
\end{block}
\end{frame}

%you can also use source 0x15000000#script@1 to load a script
