# Aquila Boot Linux
## Aquila HW
The Aquila SoC hardware project is at `aquila/hw/`, using the `build.tcl` to create the project.
```shell!
cd aquila/hw
vivado -mode batch -source build.tcl
```
## Software Enivronment Setup
### Build GNU RISC-V Toolchain
clone from [github](https://github.com/riscv-collab/riscv-gnu-toolchain)
```shell
git clone https://github.com/riscv-collab/riscv-gnu-toolchain.git
```
installation for newlib
```shell
./configure --prefix=/opt/riscv --with-arch=rv32ima --with-abi=ilp32
sudo make -j16
```
installation for linux (glibc)
```shell
./configure --prefix=/opt/riscv-linux --with-arch=rv32ima --with-abi=ilp32
sudo make linux -j16
```
In shell rc file, add:
```bash
# RISCV Newlib toolchain
export RISCV=/opt/riscv
export PATH=$PATH:$RISCV/bin
# RISCV Linux toolchain
export RISCV=/opt/riscv-linux
export PATH=$PATH:$RISCV/bin
```
When using newlib to build, comment out the Linux toolchain and vice versa.
To Build OpenSBI, U-Boot and Linux, using RISC-V Linux toolchain (glibc)
## Boot Software
We will using OpenSBI, U-Boot, Linux kernel and Busybox, the following will guide you build the software from source code.
The configs file for Aquila of each software is at `aquila/sw/linux_boot`
```shell!
linux_boot
├── linux-aquila
├── opensbi-aquila
└── u-boot-aquila
```
### Build U-Boot
clone from [U-Boot GitHub](https://github.com/u-boot/u-boot)
```shell!
git clone https://github.com/u-boot/u-boot.git
git checkout v2023.04-rc3
```
copy Aquila devices tree and config file to u-boot
```shell!
cp -r aquila/sw/linux_boot/u-boot-aquila/* u-boot/
```
build with the following command
```shell!
cd u-boot
make CROSS_COMPILE=riscv32-unknown-linux-gnu- eisl-nctu_aquila32_kc705_defconfig
make CROSS_COMPILE=riscv32-unknown-linux-gnu- -j $(nproc)
```
### Build OpenSBI
clone from [OpenSBI GitHub](https://github.com/riscv-software-src/opensbi)
```shell!
git clone https://github.com/riscv-software-src/opensbi.git
git checkout v1.2
```
copy Aquila config files and platform files to OpenSBI
```shell!
cp -r aquila/sw/linux_boot/opensbi-aquila/* opensbi/
```
Build OpenSBI with our custom platform
```shell!
cd opensbi
make all \
CROSS_COMPILE=riscv32-unknown-linux-gnu- \
PLATFORM=fpga/aquila \
PLATFORM_RISCV_XLEN=32 \
PLATFORM_RISCV_ABI=ilp32 \
PLATFORM_RISCV_ISA=rv32ima_zicsr_zifencei \
FW_PAYLOAD_PATH=<path to u-boot/u-boot.bin> \
-j $(nproc)
```
### Preparing Initramfs
First, we need to prepare rootfs for initramfs.
Create a folder named *initramfs*, and create the directory that initrmafs needs.
```shell!
mkdir initramfs
cd initramfs
mkdir dev
mkdir proc
mkdir sys
```
in dev/, create device node `console` and `null`
```shell!
sudo mknod -m 600 dev/console c 5 1
sudo mknod -m 600 dev/null c 1 3
```
using busybox in rootfs, first we need to build busybox.
clone from [Busybox GitHub](https://github.com/mirror/busybox)
```shell!
git clone https://github.com/mirror/busybox.git
```
build busybox
```shell!
cd busybox
make defconfig
make menuconfig (settings -> build options -> choose build static binary (no shared libs))
make CROSS_COMPILE=riscv32-unknown-linux-gnu- -j $(nproc) install
```
the busybox binary executable will install at busybox/_install, copy the binary executable to our initramfs folder.
```shell!
cp -r _install/bin <path_to_initramfs_folder>
```
create an init script as the first initial program, create a file named `init` and makes it executable.
```shell!
touch init
sudo chmod +x init
```
the content of the `init` is like a shell script :
```bash!
#!/bin/busybox sh
/bin/mount -t devtmpfs none /dev
/bin/mount -t proc none /proc
/bin/mount -t sysfs none /sys
echo "Hello Aquila!"
/bin/sh
```
now, our inintramfs folder has:
```shell!
initramfs
├── bin
│ ├── arch -> busybox
│ ├── ash -> busybox
│ ├── base32 -> busybox
│ ├── base64 -> busybox
│ ├── busybox
│ ├── cat -> busybox
│ ├── ...
│ ├── vi -> busybox
│ ├── watch -> busybox
│ └── zcat -> busybox
├── dev
│ ├── console
│ └── null
├── init
├── proc
└── sys
```
### Build Linux Kernel
clone from [Linux GitHub](https://github.com/torvalds/linux)
```shell!
git clone https://github.com/torvalds/linux.git
```
copy Aquila config files to linux
```shell!
cp -r aquila/sw/linux_boot/linux-aquila/* linux/
```
set kernel configs
```shell!
cd linux
git checkout v5.19
make ARCH=riscv CROSS_COMPILE=riscv32-unknown-linux-gnu- aquila32_defconfig
make ARCH=riscv CROSS_COMPILE=riscv32-unknown-linux-gnu- menuconfig (General setup -> Initramfs source files(s))
```
in menuconfig, set the path to our initramfs folder

build kernel
```shell!
make ARCH=riscv CROSS_COMPILE=riscv32-unknown-linux-gnu- -j $(nproc)
```
### Preparing Boot SD-Card
preparing a SD-card, formatting to FAT-32
copy the opensbi payload elf to SD-card and rename to `system.elf`
```shell!
cp opensbi/build/platform/fpga/aquila/firmware/fw_payload.elf <path_to_sdcard>/system.elf
```
copy the Linux kernel image to SD-card
```shell!
cp linux/arch/riscv/boot/Image <path_to_sdcard>
```