###### tags: `TSC` `Linux`
Create Standalone SDK from imx
=====
# 前言
## Key component
要讓 CPU 跑 linux 基本要有幾個組件
- Bootloader
- Kernel
- rootfs
### Bootloader

### Files and binaries required in the boot partition:
• flash.bin: Fit image containing U-Boot and the ATF
• zImage: Kernel image
• zImage-*.dtb: Device tree
# Pre Setup
:::danger
注意事項:
1. 編譯Yocto Project 官方文件提到 Linux Host 機器,硬碟空間至少要 ==120 GB==
2. 使用虛擬機(VM)建構硬碟/linux Host時,建議使用==固定大小==,不要使用動態分配
3. 建議使用 Ubuntu 18.04 or 相同的host linux 版本
ref:

:::
## 安裝相依套件
```bash=
sudo apt-get install gawk wget git-core diffstat unzip texinfo gcc-multilib \
build-essential chrpath socat cpio python python3 python3-pip python3-pexpect \
xz-utils debianutils iputils-ping python3-git python3-jinja2 libegl1-mesa libsdl1.2-dev \
pylint3 xterm rsync curl libncursesw5-dev libncurses5-dev
```
## 配置 Git
```bash=
git config --global user.name "<Your Name>"
git config --global user.email "<Your Email>"
git config --list
```
## 配置 repo utility
- Create a bin folder in the home directory.
```bash=
mkdir ~/bin
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
```
- Add the following line to the .bashrc file to ensure that the ~/bin folder is in your PATH variable.
```bash=
export PATH=~/bin:$PATH > ~/.bashrc
source ~/.bashrc
```
# 建立 Toolchain (使用 Yocto Project)
1. 建立 Yocto Project on imx-5.10.52-2.1.0
```bash=
mkdir imx-yocto-bsp
cd imx-yocto-bsp
repo init -u https://source.codeaurora.org/external/imx/imx-manifest -b imx-linux-hardknott -m imx-5.10.52-2.1.0.xml
repo sync
```
2. 建立yocto 設定檔
```
usage:
DISTRO=<distro name> MACHINE=<machine name> source imx-setup-release.sh -b <build dir>
```
- imx8mp-lpddr4-evk
```bash=
DISTRO=fsl-imx-xwayland MACHINE=imx8mp-lpddr4-evk source imx-setup-release.sh -b imx8mp_build_xwayland
```
- imx8mmevk
```bash=
DISTRO=fsl-imx-xwayland MACHINE=imx8mmevk source imx-setup-release.sh -b imx8mmevk_build_xwayland
```
:::info
執行會進行 EULA 簽署,輸入 y 即可

> distro name:
* fsl-imx-wayland : Wayland weston graphics.
* fsl-imx-xwayland : Wayland graphics and X11. X11 applications using EGL are not supported.
* ~~fsl-imx-fb : Frame Buffer graphics - no X11 or Wayland. Frame Buffer is not supported on i.MX 8.~~
> machine name:
i.MX 8
* imx8qmmek
* imx8qxpc0mek
* imx8mqevk
* imx8mm-lpddr4-evk
* imx8mm-ddr4-evk
* imx8mn-lpddr4-evk
* imx8mn-ddr4-evk
- [x] ==imx8mp-lpddr4-evk==
* imx8mp-ddr4-evk
* imx8dxl-lpddr4-evk
* imx8mnddr3levk
* imx8ulp-lpddr4-evk
* imx8mmddr3lval ==> 編譯失敗
> 其他config 定義至 source/meta-xxx/conf/machine 的 xxx.conf
example: imx8mmddr3lval in "/sources/meta-imx/meta-bsp/conf/machine"
Restarting a build environment
> If a new terminal window is opened or the machine is rebooted after a build directory is set up, the setup environment script should
be used to set up the environment variables and run a build again. The full imx-setup-release.sh is not needed.
```
source setup-environment <build-dir>
```
:::
3. 執行 bitbake
> ==這個步驟需要等待 4~5 小時==
- imx8mpevk
```bash=
DISTRO=fsl-imx-xwayland MACHINE=imx8mp-lpddr4-evk bitbake imx-image-core -c populate_sdk
```
- imx8mmevk
```bash=
DISTRO=fsl-imx-xwayland MACHINE=imx8mmevk bitbake imx-image-core -c populate_sdk
```
:::info
最後會在 ==tmp/deploy/sdk== 目錄下會產出
`fsl-xxxx.sh` (toolchain install Script file)
:::
4. 安裝 toolchain
執行 toolchain install script file
example:
```bash=
. /home/max/imx-yocto-bsp/imx8mp_build_xwayland/tmp/deploy/sdk/fsl-imx-xwayland-glibc-x86_64-core-image-minimal-cortexa53-crypto-imx8mpevk-toolchain-5.10-hardknott.sh
```

:::info
預設安裝置 /opt 下
:::
5. Start build environment
run environment-setup script
```bash=
. /opt/fsl-imx-xwayland/5.10-hardknott/environment-setup-cortexa53-crypto-poky-linux
```
or use source
```bash=
source /opt/fsl-imx-xwayland/5.10-hardknott/environment-setup-cortexa53-crypto-poky-linux
```
6. 確認 toolchain 是否安裝成功
```
echo &env | grep -e ARCH= -e CROSS_COMPILE= -e SYSROOT= -e CC=
```

:::warning
由於sdk 採用動態設定環境變數,若充新開啟 terminal ,請重新執行 'run environment-setup script
:::
# 建立 Bootloader
Bootloader 包含以下組件:
- 1. SECO(Security Controller)
- 2. ATF (ARM Trusted FW)
- 3. U-BOOT
## 建置 SECO FW: Security Controller
SECO: NXP 只提供 Binary file,主要啟動過程中的一些安全認證。
```bash=
wget http://www.nxp.com/lgfiles/NMG/MAD/YOCTO/firmware-imx-8.13.bin
chmod a+x firmware-imx-8.13.bin
./firmware-imx-8.13.bin --auto-accept
```
以下路徑會有所需的 bin 檔
firmware-imx-8.13/firmware/ddr/synopsys

## 建置 imx-atf
atf: ARM Trusted FW
[介紹](https://blog.csdn.net/Alex___Zhao/article/details/76794586)
### Only clone lf-5.10.52-2.1.0 branch
```bash=
git clone -b lf-5.10.52-2.1.0 --single-branch https://source.codeaurora.org/external/imx/imx-atf/
```
### Make image
`usage: make [PLAT=<platform>] [OPTIONS] [TARGET]`
>detail please run ==make help==
example build the bl31 binary image:
```bash=
make clean PLAT=imx8mp
LDFLAGS="" make -j $(nproc) PLAT=imx8mp bl31
```
:::info
PLAT: Platform define
> imx Platform
Path: imx-atf/plat/imx
bl31: EL3 Firmware
:::
建置結果

## 建置 uboot-imx
### 下載
Only clone lf-5.10.52-2.1.0 branch
```bash=
git clone -b lf-5.10.52-2.1.0 --single-branch https://source.codeaurora.org/external/imx/uboot-imx
```
### 編譯
#### clean dist data
```bash=
make distclean
```
#### make config file
- for i.MX 8M Plus DDR4 EVK board
```bash=
make imx8mp_ddr4_evk_defconfig
```
- for i.MX 8M Plus LPDDR4 EVK board
```bash=
make imx8mp_evk_defconfig
```
- for i.MX 8M mini LPDDR4 EVK board
```bash=
make imx8mm_evk_defconfig
```
- for i.MX 8M mini DDR3L val board
```bash=
make imx8mm_ddr3l_val_defconfig
```

#### make image
```bash=
make -j $(nproc)
````

### make menuconfig
:::info
linux host (ubuntu should install libncurses5-dev)
apt install libncurses5-dev
:::
should check the compiler is valid

### 所需檔案
:::info
tools/mkimage
spl/u-boot-spl.bin
u-boot-nodtb.bin
arch/arm/dts/imx8mm-ddr4-evk.dtb
:::
## 使用 imx-mkimage 建立 Bootloader image
### 下載
Only clone lf-5.10.52-2.1.0 branch
```bash=
git clone -b lf-5.10.52-2.1.0 --single-branch https://source.codeaurora.org/external/imx/imx-mkimage
```
### 首先需要複製 Bootloader image 所需的檔案
- uboot-imx
```bash=
cp uboot-imx/tools/mkimage ./imx-mkimage/iMX8M/mkimage_uboot
# for ddr4 copy
cp uboot-imx/arch/arm/dts/imx8mp-ddr4-evk.dtb ./imx-mkimage/iMX8M/
cp uboot-imx/spl/u-boot-spl-ddr.bin ./imx-mkimage/iMX8M/
cp uboot-imx/u-boot-nodtb.bin ./imx-mkimage/iMX8M/
# for lpddr4
cp uboot-imx/arch/arm/dts/imx8mp-evk.dtb ./imx-mkimage/iMX8M/
cp uboot-imx/spl/u-boot-spl.bin ./imx-mkimage/iMX8M/
cp uboot-imx/u-boot-nodtb.bin ./imx-mkimage/iMX8M/
```
- firmware-imx
:::info
for ddr4 copy
- ddr4_dmem_1d.bin
- ddr4_dmem_2d.bin
- ddr4_imem_1d.bin
- ddr4_imem_2d.bin
:::
```bash=
cp firmware-imx-8.12/firmware/ddr/synopsys/ddr4_dmem_1d.bin ./imx-mkimage/iMX8M/
cp firmware-imx-8.12/firmware/ddr/synopsys/ddr4_dmem_2d.bin ./imx-mkimage/iMX8M/
cp firmware-imx-8.12/firmware/ddr/synopsys/ddr4_imem_1d.bin ./imx-mkimage/iMX8M/
cp firmware-imx-8.12/firmware/ddr/synopsys/ddr4_imem_2d.bin ./imx-mkimage/iMX8M/
```
```
cp firmware-imx-8.12/firmware/ddr/synopsys/ddr3_*.bin ./imx-mkimage/iMX8M/
```
:::info
for lpddr4 copy
- lpddr4_pmu_train_1d_dmem_202006.bin
- lpddr4_pmu_train_1d_imem_202006.bin
- lpddr4_pmu_train_2d_dmem_202006.bin
- lpddr4_pmu_train_2d_imem_202006.bin
:::
```bash=
cp firmware-imx-8.12/firmware/ddr/synopsys/lpddr4_pmu_train_1d_dmem_202006.bin ./imx-mkimage/iMX8M
cp firmware-imx-8.12/firmware/ddr/synopsys/lpddr4_pmu_train_1d_imem_202006.bin ./imx-mkimage/iMX8M
cp firmware-imx-8.12/firmware/ddr/synopsys/lpddr4_pmu_train_2d_dmem_202006.bin ./imx-mkimage/iMX8M
cp firmware-imx-8.12/firmware/ddr/synopsys/lpddr4_pmu_train_2d_imem_202006.bin ./imx-mkimage/iMX8M
```
- imx-atf
```bash=
cp imx-atf/build/imx8mp/release/bl31.bin ./imx-mkimage/iMX8M/
```
## Make Bootloader image
```bash=
cd imx-mkimage
```
run make help
```
usage make SOC=<SOC_TARGET> [TARGET]
i.e. make SOC=iMX8QX flash
Common Targets:
Parts with SCU
flash_scfw - Only boot SCU
flash - SCU + AP
flash_flexspi - SCU + AP (FlexSPI device)
flash_nand - SCU + AP (NAND device)
flash_cm4 - SCU + M4_0 TCM image
flash_linux_m4 - SCU + AP (OPTEE) + M4_0 (and M4_1) TCM image
flash_linux_m4_xip - SCU + AP (OPTEE) + M4_0 (and M4_1) FLASH XIP image
flash_linux_m4_ddr - SCU + AP (OPTEE) + M4_0 (and M4_1) DDR image
Parts w/o SCU
flash_ddr3l_val - DisaplayPort FW + u-boot spl
flash_ddr3l_val_no_hdmi - u-boot spl
flash_hdmi_spl_uboot - HDMI FW + u-boot spl
flash_dp_spl_uboot - DisaplayPort FW + u-boot spl
flash_spl_uboot - u-boot spl
Typical flash cmd: dd if=iMX8QM/flash.bin of=/dev/<your device> bs=1k seek=33
```
- for DDR4
- iMX8M Plus
```bash=
make SOC=iMX8MP flash_ddr4_evk
```
- iMX8M Mini
```bash=
make SOC=iMX8MM flash_ddr4_evk
```
- for LPDDR4
- iMX8M Plus
```bash=
make SOC=iMX8MP flash_evk
```
- iMX8M Mini
```bash=
make SOC=iMX8MM flash_evk
```
- for LPDDR3L
- iMX8M Mini
```bash=
make SOC=iMX8MM flash_ddr3l_evk
```
建置成功將於 `imx-mkimage/iMX8M` 輸出 ==flash.bin==

## make parameter
in soc.mak file
- imx8mp
::: info
PLAT = imx8mp
HDMI = no
SPL_LOAD_ADDR = 0x920000
SPL_FSPI_LOAD_ADDR = 0x920000
TEE_LOAD_ADDR = 0x56000000
ATF_LOAD_ADDR = 0x00970000
VAL_BOARD = val
#define the F(Q)SPI header file
QSPI_HEADER = ../scripts/fspi_header
QSPI_PACKER = ../scripts/fspi_packer.sh
VERSION = v2
LPDDR_FW_VERSION = _202006
DDR_FW_VERSION = _202006
:::
- imx8mm
::: info
PLAT = imx8mm
HDMI = no
SPL_LOAD_ADDR = 0x7E1000
SPL_FSPI_LOAD_ADDR = 0x7E2000
TEE_LOAD_ADDR ?= 0xbe000000
ATF_LOAD_ADDR = 0x00920000
VAL_BOARD = val
#define the F(Q)SPI header file
QSPI_HEADER = ../scripts/fspi_header 0
QSPI_PACKER = ../scripts/fspi_packer.sh
VERSION = v1
:::
# 編譯 linux kernel
## 下載
Only clone lf-5.10.52-2.1.0 branch
```bash=
git clone -b lf-5.10.52-2.1.0 --single-branch https://source.codeaurora.org/external/imx/linux-imx
```
## 編譯
```bash=
make distclean
make imx_v8_defconfig
make -j $(nproc) Image dtbs
```
:::info
“error : openssl/bio.h :No such file or folder”
sudo apt install libssl-dev
:::
## 所需檔案:
```shell=
## device tree binary
arch/arm64/boot/dts/freescale/*imx8mp*.dtb
## Kernal image
arch/arm64/boot/Image
```
# rootfs
1. 直接下載Nxp官方編譯好的 (需要登入)
https://www.nxp.com/webapp/sps/download/license.jsp?colCode=L5.10.35_2.0.0_MX8MP&appType=file1&location=null&DOWNLOAD_ID=null
# 燒錄
```
# The disk layout used is:
# - ---------- -------------- --------------
# | | imx-boot | boot | rootfs |
# - ---------- -------------- --------------
# ^ ^ ^ ^ ^
# | | | | |
# 0 | 8MiB 72MiB 72MiB + rootfs + IMAGE_EXTRA_SPACE (default 10MiB)
# 32 32 or 33kiB, see reference manual
```
請參考 Linux Image 製作方式
https://hackmd.io/NhzRJ7rqTeuBoc4iEhy4yg
## bootloader
```
dd if=<boot_image> of=/dev/sd<x> bs=1k seek=<offset> conv=fsync
```
example:
- iMX 8M Plus
```
dd if=flash.bin of=/dev/sdb bs=1k seek=32 conv=fsync
```
- iMX 8M Mini
```
dd if=flash.bin of=/dev/sdb bs=1k seek=33 conv=fsync
```
```
Where:
• offset is:
— 1 - for i.MX 6 or i.MX 7
— 33 - for i.MX 8QuadMax A0, i.MX 8QuadXPlus A0, i.MX 8M Quad, i.MX 8M Mini
— 32 - for i.MX 8QuadXPlus B0, i.MX 8QuadMax B0, i.MX 8DXL, i.MX 8M Nano, and i.MX 8M Plus
• sd<x> is:
— Device node for the SD card
• boot_image is:
— u-boot.imx - for i.MX 6 or i.MX 7
— flash.bin - for i.MX8
```
## kernel image
```bash=
sudo dd if=<zImageName> of=/dev/sd<partition> bs=512 seek=2048 conv=fsync && sync
```
## kernel devicetree
```bash=
sudo dd if=<DevicetreeName>.dtb of=/dev/sd<partition> bs=512 seek=20480 conv=fsync
```
# 參考資料
https://github.com/SolidRun/imx8mp_build/blob/master/runme.sh
https://coldnew.github.io/9cbf4b94/
iMX8QXP BSP Standalone Building Up
https://www.wpgdadatong.com/tw/blog/detail?BID=B1395
LF5.10.35_2.0.0 IMX_LINUX_RELEASE_NOTES
https://www.nxp.com/docs/en/release-note/IMX_LINUX_RELEASE_NOTES.pdf
IMX_YOCTO_PROJECT_USERS_GUIDE
https://www.nxp.com/docs/en/user-guide/IMX_YOCTO_PROJECT_USERS_GUIDE.pdf
https://www.nxp.com/search?keyword=%2520L5.10.35&start=0&TARGET=https%253A%252F%252Fwww.nxp.com%252Fsearch%253Fkeyword%253DL5.10%2526start%253D0%2526&SAMLart=AAFSsPYAkNKN6Mb0Q6Li8D8gawrtLDPEJ99b3OIofvIALYpynjEcWdWa
imx8m mini 中如何手动将kernel和dtb拷贝到eMMC上
https://www.wpgdadatong.com/tw/blog/detail?BID=B1656
在Imx8m mini 上通过TFTP实现NFS启动
https://www.wpgdadatong.com/tw/blog/detail?BID=B1503
imx8m mini中jailhouse hypervisor的应用
https://www.wpgdadatong.com/tw/blog/detail?BID=B2215
【ATU Book-i.MX8系列】 NXP i.MX8M Mini 環境建置
https://www.wpgdadatong.com/tw/blog/detail?BID=B1134
https://www.wpgdadatong.com/tw/profile?uid=64902&type=blogPost&timeSort=2&title=blogPost
https://www.wpgdadatong.com/tw/profile?uid=60572&type=blogPost&timeSort=2&title=blogPost
https://www.wpgdadatong.com/cn/profile?uid=66412&type=blogPost&timeSort=2&title=blogPost
https://www.wpgdadatong.com/tw/profile?uid=62865&type=blogPost&timeSort=2&title=blogPost
https://www.wpgdadatong.com/tw/profile?uid=72266&type=blogPost&timeSort=2&title=blogPost
https://www.wpgdadatong.com/tw/profile?uid=64710&type=blogPost&timeSort=2&title=blogPost
https://www.wpgdadatong.com/tw/profile?uid=60836&type=blogPost&timeSort=2&title=blogPost
https://www.wpgdadatong.com/tw/profile?uid=60459&type=blogPost&timeSort=2&title=blogPost
## 构建 i.MX8 Yocto BSP 镜像
https://getiot.tech/imx8/build-imx-yocto-bsp-image.html
## Test Report vA0076
http://ess-wiki.advantech.com.tw/view/Test_Report_vA0076
iMX8QXP BSP Standalone Building Up
https://www.wpgdadatong.com/tw/blog/detail?BID=B1395