owned this note
owned this note
Published
Linked with GitHub
# Build Altera SoC Image with Ubuntu
* There are five steps to build Altera SoC image with Ubuntu.
1. <a href="#1-establish-cross-compile-environment">Establish Cross-compile environment</a>
2. <a href="#2-build-fpga-project">Build FPGA Project</a>
3. <a href="#3-build-linux-filesystem">Build Linux Filesystem</a>
4. <a href="#4-build-linux-kernel">Build Linux Kernel</a>
5. <a href="#5-create-linux-image-file">Create Linux Image File</a>
## 1. Establish Cross-compile environment
This chapter describe how to establish a cross-compile environment on Linux. Please follow the instruction from the sections below to install the required software and finally create an empty project folder for building the project later.
<div style="margin-left: 20px;">
1.1 <a href="#11-install-ubuntu-16041-64bits-on-pc">Install Ubuntu 16.04.1 64-bit</a>
1.2 <a href="#12-install-quartus-prime-standard-edition-1602">Install Quartus Prime Standard Edition 16.0.2 build 222</a>
1.3 <a href="#13-install-soc-eds-tool">Install SoC Embedded Design Suite 16.0.0</a>
1.4 <a href="#14-install-arm-linux-gnueabihf-toolchain">Install Linaro Cross Compiler: arm-linux-gnueabihf 5.2</a>
1.5 <a href="#15-install-library-amp-tool">Install Library and Tool</a>
1.6 <a href="#16-prepare-an-empty-project-folder-tree">Create an Empty Project Folder Tree</a>
</div>
### 1.1 Install Ubuntu 16.04.1 64-bit on the Host PC
A 64-bit Ubuntu is required to establish a compile environment. Please download the Ubuntu image file and install it on your 64-bit Intel/AMD PC with the following instructions.
* Download [Ubuntu 16.04.1 64-bit](http://releases.ubuntu.com/16.04.1/ubuntu-16.04.1-desktop-amd64.iso)
* [Install Ubuntu 16.04.1 64-bit on the Host PC](https://www.ubuntu.com/download/desktop/install-ubuntu-desktop)
### 1.2 Install Quartus Prime Standard Edition 16.0.2
Altera Quartus is required to compile FPGA project. Please follow the instruction below to download and install Quartus Prime, Cyclone V device and the patch on Ubuntu.
* Download [Quartus Prime Standard Edition](http://dl.altera.com/16.0/?edition=standard&platform=linux#tabs-2) and [Cyclone V device support](http://dl.altera.com/16.0/?edition=standard&platform=linux#tabs-2)
![Download Quartus Prime Standard Edition & Cyclone V device support](https://i.imgur.com/CergaYS.png)
* Download [Quartus Prime Software v16.0 Update 2](http://dl.altera.com/16.0/?edition=standard&platform=linux#tabs-5)
![Download Quartus Prime Software v16.0 Update 2](https://i.imgur.com/wRZZYNZ.png)
* Install Quartus Prime Standard Edition
* Click Next
![](https://i.imgur.com/G1uZpTv.png)
* Select "I accept the agreement" and click Next
![](https://i.imgur.com/GGkRrGo.png)
* Pease use the default directory and click Next
![](https://i.imgur.com/GycEQpN.png)
* Select "Quartus Prime Standard Edition", "Cyclone V", "Quartus Prime Update 16.0.2.222" and click Next
![](https://i.imgur.com/U0UipiL.png)
* Click Next
![](https://i.imgur.com/PmZsH9m.png)
* Unselect "Create shortcutes on Desktop", "Launch Quartus Prime Standard Edition" and click Finish
![](https://i.imgur.com/GqgOr2W.png)
* Setup the License
* fixed license
`export LM_LICENSE_FILE="<path-to-license>/license.dat"`
* floating license
`export LM_LICENSE_FILE=port@192.168.X.X`
### 1.3 Install SoC EDS tool
Altera SoC EDS tool is required to compile ARM project. Please follow the instructions below to install the EDS tool.
:::info
Note: Altera DS-5 is not required for the installation.
:::
* Download the [SoC EDS tool](http://dl.altera.com/soceds/16.0/?platform=linux)
* Install the SoC EDS tool
* Click Next
![](https://i.imgur.com/ATVY2pS.png)
* Select "I accept the agreement" and click Next
![](https://i.imgur.com/FRgmsHR.png)
* Please use the default directory and click Next
![](https://i.imgur.com/rzsXXY1.png)
* Unselect "Quartus Prime Programmer and Tools" and click Next
![](https://i.imgur.com/aQLFgYP.png)
* Click Next
![](https://i.imgur.com/uvuxp7B.png)
* Unselect "Launch DS-5 installation" and click Finish
![](https://i.imgur.com/2dHgAx0.png)
### 1.4 Install arm-linux-gnueabihf Toolchain
ARM toolchain is required to cross-compile ARM project. Please type the following commands in Ubuntu Terminal to install the required ARM toolcahin.
```shell
cd /opt
sudo wget -c https://releases.linaro.org/components/toolchain/binaries/5.2-2015.11-2/arm-linux-gnueabihf/gcc-linaro-5.2-2015.11-2-x86_64_arm-linux-gnueabihf.tar.xz
sudo tar xvf gcc-linaro-5.2-2015.11-2-x86_64_arm-linux-gnueabihf.tar.xz
sudo ln -s gcc-linaro-5.2-2015.11-2-x86_64_arm-linux-gnueabihf arm-linux-guneabihf
export PATH=$PATH:/opt/arm-linux-guneabihf/bin
```
### 1.5 Install Library and Tool
Some library and tool are required to perform cross-compiler for ARM project. Please type the following commands in Ubuntu Terminal to install the required library and tool.
```shell
sudo apt install lib32ncurses5 -y
sudo apt install qemu-user-static -y
sudo apt install git pv -y
sudo apt install libncurses5-dev -y
```
### 1.6 Prepare an Empty Project Folder Tree
An empty project folder tree is established to store the source code for compilation. The folder tree looks like the example below:
```
de1_soc/
├── image
│ ├── p1
│ ├── p2
│ └── p3
├── ghrd
├── linux-socfpga
└── rootfs
```
* Create folder tree
Type the following commands in Ubuntu Terminal to create an empty folder tree.
```shell
cd ~
mkdir de1_soc
cd de1_soc
mkdir image
mkdir image/p1
mkdir image/p1/output_files
mkdir image/p3
mkdir ghrd
mkdir rootfs
ln -s ../rootfs image/p2
```
## 2. Build FPGA Quartus Project
This chapter describes how to build the FPGA configure file (soc_system.rbf), preloader(preloader-mkpimage.bin), u-boot(u-boot.img), and device tree blob(soc_system.dtb).
Please type the following commands in Ubuntu Terminal for building the FPGA project in Quartus Prime Standard edition.
### 2.1 Prepare Project
* Download the FPGA Quartus project named ghrd.tar
```shell
cd ~/de1_soc/ghrd
wget http://mail.terasic.com.tw/~jim/de1_soc/ghrd.tar
```
* Decompress ghrd.tar
```shell
tar xvf ghrd.tar
```
### 2.2 Quartus Setup
* Setting environment variable
```shell
~/altera/16.0/embedded/embedded_command_shell.sh
```
* Confirm the Quartus license is valid
Type 'quartus' to launch Quartus and check the license validity.
```shell
quartus
```
* Tool -> License Setup
![Quartus License Setup](https://i.imgur.com/m9qMgtD.png)
![](https://i.imgur.com/gowcmOV.png)
* If Quartus fails to read license information, please make sure the variable value `LM_LICENSE_FILE` is valid
* Close Quartus
### 2.3 Build Project
* Generate Components
Type the following commands to generate all components including preloader-mkpimage.bin, u-boot.img, soc_system.dtb, soc_system.rbf, and u-boot.scr.
```shell
make all
```
* Copy Components
Type the following commands to copy the generated components to a dedicated folder for building an image.
```shell
cp output_files/soc_system.rbf ~/de1_soc/image/p1/output_files/
cp soc_system.dtb ~/de1_soc/image/p1/
cp u-boot.scr ~/de1_soc/image/p1/
cp software/preloader/preloader-mkpimage.bin ~/de1_soc/image/p3/
cp software/preloader/uboot-socfpga/u-boot.img ~/de1_soc/image/p3/
```
## 3. Build Linux Filesystem
This chapter describes how to build a Linux tool filesystem. The final filesystem is stored in the folder "~/de1_soc/rootfs".
Type the following commands in Ubuntu Terminal for building the Linux Filesystem.
* Switch to root privilege
```shell
sudo -s
```
* Download the Ubuntu root filesystem
```shell
cd ~/de1_soc/rootfs
wget -c http://cdimage.ubuntu.com/ubuntu-base/releases/16.04/release/ubuntu-base-16.04.1-base-armhf.tar.gz
tar xvf ubuntu-base-16.04.1-base-armhf.tar.gz
rm ubuntu-base-16.04.1-base-armhf.tar.gz
```
* Copy qemu-user-static
```shell
cp /usr/bin/qemu-arm-static usr/bin/
```
* Modify `etc/apt/sources.list` to un-comment all the repositories except the ones starting with `deb-src`.
```shell
sed -i 's%^# deb %deb %' etc/apt/sources.list
```
* copy your system’s (host machine’s) `/etc/resolv.conf` to `~/de1_soc/rootfs/etc/resolv.conf`. Set proxies if necessary.
```shell
cp /etc/resolv.conf ~/de1_soc/rootfs/etc/resolv.conf
```
* Download a [simple bash script](https://raw.githubusercontent.com/psachin/bash_scripts/master/ch-mount.sh) for devices mounting and un-mounting later.
```shell
cd ~/de1_soc
wget https://raw.githubusercontent.com/psachin/bash_scripts/master/ch-mount.sh
```
* Mount `proc, sys, dev, dev/pts` to the new filesystem.
```shell
chmod a+x ch-mount.sh
./ch-mount.sh -m rootfs/
```
* Update the repositories
```shell
apt update
```
* Install minimal packages required for core utils
```shell
apt install language-pack-en-base sudo ssh net-tools ethtool iputils-ping rsyslog alsa-utils bash-completion htop python-gobject-2 network-manager --no-install-recommends --yes
```
* Install packages required for LXDE Desktop
```shell
apt install lxde xfce4-power-manager xinit xorg lightdm-gtk-greeter xserver-xorg-video-fbdev gnome-mplayer lightdm lxtask htop --yes
```
* Add a user account and include it in suitable groups
```shell
useradd terasic -m -s /bin/bash
echo terasic:123 | chpasswd
addgroup terasic adm && addgroup terasic sudo && addgroup terasic audio && addgroup terasic video
```
* Add hostname to /etc/hostname
```shell
echo 'de1-soc' > /etc/hostname
```
* Add host entry to /etc/hosts
```shell
echo "127.0.0.1 localhost" >> /etc/hosts
echo "127.0.1.1 de1-soc" >> /etc/hosts
```
* Exit chroot and unmount `proc, sys, dev, dev/pts`
```shell
exit
./ch-mount.sh -u rootfs/
```
* Return to user privilege
```shell
exit
```
Reference: [Building Ubuntu rootfs for ARM](https://gnu-linux.org/building-ubuntu-rootfs-for-arm.html)
## 4. Build Linux Kernel
This chapter describes how to build Linux kernel.The final kernal image is stored in the directory `~/de1_soc/image/p1`. The kernel module is stored in the directory `~/de1_soc/rootfs`.
Type the following commands in Ubuntu Terminal for building the Linux kernel.
* Download the source code of the kernel
```shell
cd ~/de1_soc
git clone https://github.com/altera-opensource/linux-socfpga.git
cd linux-socfpga
git checkout -t -b socfpga-4.5 origin/socfpga-4.5
```
* Setup enviroment variables for cross-compilation
```shell
export ARCH=arm
export CROSS_COMPILE=arm-linux-gnueabihf-
```
* Generate the default config. file `.config`
```shell
make socfpga_defconfig
```
* Kernel configuration
```shell
make menuconfig
```
* Enable Frame Reader framebuffer support
```
Device Drivers --->
Graphics support --->
Frame buffer Devices --->
<*> Support for frame buffer devices --->
<*> Altera VIP Frame Reader framebuffer support
```
![Altera VIP Frame Reader framebuffer](https://i.imgur.com/mQLUi3m.png)
* Enable UVC Camera support
```
Device driver-->
<*>Multimedia support-->
[*]Cameras/video grabbers support
[*]Media USB Adapters-->
<*>USB Video Class(UVC)
[*]UVC input event device support
```
![](https://i.imgur.com/75BnS11.png)
> Move cursor to 「Media USB Adapters-->」,then press 'Enter' to enter the submenu.
![](https://i.imgur.com/EENMBfq.png)
![](https://i.imgur.com/uuqTnYz.png)
* Save config and exit
![](https://i.imgur.com/eEFMclq.png)
![](https://i.imgur.com/pXmRbmi.png)
![](https://i.imgur.com/ynLw1AH.png)
* Compile kernel
```shell
make zImage -j4
```
* Copy kernel
```shell
cp arch/arm/boot/zImage ~/de1_soc/image/p1/
```
* Compile modules
```shell
make modules -j4
```
* Install modules
```shell
sudo -E make modules_install INSTALL_MOD_PATH=~/de1_soc/rootfs/
```
## 5. Create Linux Image File
This chapter describes how to merge the components generated in above chapters into an image file. It also shows how to write this image file into a microSD card.
The Python script file 'make_sdimage.py' provided from rocketboards.org can be used to merge these components. Please type the following commands to download the script file and execute the script file to generate final image file.
* Download the script file 'make_sdimage.py'
```shell
cd ~/de1_soc/image
wget https://releases.rocketboards.org/release/2016.05/gsrd/tools/make_sdimage.py
chmod a+x make_sdimage.py
```
* Execute the script file
Type the following commands in terminal to execute the script file and merge all components into a single image file 'de1-soc-sd-card.img'
```shell
sudo ./make_sdimage.py -f \
-P p1/*,num=1,format=vfat,size=100M \
-P p2/*,num=2,format=ext3,size=3500M \
-P p3/preloader-mkpimage.bin,p3/u-boot.img,num=3,format=raw,size=10M,type=A2 \
-s 3700M \
-n de1-soc-sd-card.img
```
* Clone the image to the microSD card
Insert a microSD card (at least 4GB is required and 8GB is recommended) into your host PC. Execute the following commands to clone the image file.
Note the "sd<span style="color: red">x</span>" in the command below needs to be replaced with the device name of the microSD card on your host PC. The `lsblk` command can be used to check the device name associated with the inserted microSD card.
```shell
umount /dev/sdx* 2> /dev/null
pv -tpreb de1-soc-sd-card.img | sudo dd of=/dev/sdx bs=1M
```
# Appendix
## Contents of the microSD Card Image in Linux
### Partition Format
There are three partitions in microSD Card
* FAT32
* EXT3
* RAW A2
### Partition Content
#### FAT32
* Device tree blob
* FPGA configuration
* U-boot script for FPGA configuration
* Linux kernel image
#### EXT3
* Filesystem
#### RAW A2
* Preloader image
* U-boot image
:::info
The order of the partitions cannot be changed.
:::
The following table shows how components are generated.
| Item | Description | Reference |
| --------- | ----------- | -------------- |
| preloader-mkpimage.bin | Preloader image | <a href="#2-build-fpga-project">Build FPGA Project</a> <br>`make preloader` |
| u-boot.img | U-boot image | <a href="#2-build-fpga-project">Build FPGA Project</a> <br>`make uboot` |
| soc_system.dtb | Device Tree Blob | <a href="#2-build-fpga-project">Build FPGA Project</a> <br>`make dtb` |
| soc_system.rbf | FPGA configuration file | <a href="#2-build-fpga-project">Build FPGA Project</a> <br>`make rbf` |
| u-boot.scr | U-boot script for configuring FPGA | <a href="#2-build-fpga-project">Build FPGA Project</a> <br>`make u-boot.scr` |
| zImage | Linux kernel image file | <a href="#4-build-linux-kernel">Build Linux Kernel</a> |
| filesystem | Linux root filesystem | <a href="#3-build-linux-filesystem">Build Linux Filesystem</a> |
## Update Individual Elements on the microSD Card
It is time consuming to write the entire image to the microSD card whenever a modification is made. Hence it is preferred to update the elements individually after the first image is created and written to the microSD card.
The following table shows how each item can be updated individually.
<table>
<tr>
<th>Item</th>
<th>Update Procedure</th>
</tr>
<tr>
<td>zImage</td>
<td rowspan="4">Mount partition 1 from the microSD card and replace the file with the new one.<br><code>mkdir sdcard</code> <br><code>mount /dev/sdx1 sdcard/</code><br><code>cp <file_name> sdcard/</code><br><code>umount sdcard</code></td>
</tr>
<tr>
<td>soc_system.rbf</td>
</tr>
<tr>
<td>soc_system.dtb</td>
</tr>
<tr>
<td>u-boot.scr</td>
</tr>
<tr>
<td>preloader-mkpimage.bin</td>
<td>dd if=preloader-mkpimage.bin of=/dev/sdx3 bs=64k seek=0</td>
</tr>
<tr>
<td>u-boot.img</td>
<td>dd if=u-boot.img of=/dev/sdx3 bs=64k seek=4</td>
</tr>
<tr>
<td>filesystem</td>
<td>dd if=de1-soc-sd-card.img bs=512 skip=206849 count=7168001 | pv -tpreb -s 3500m | sudo dd of=/dev/sdx2 bs=512</td>
</tr>
</table>
Remember to replace "sdx" in the command above with the device name of the microSD card on your host PC. You can find out the device name by executing `cat /proc/partitions` after plugging in the card reader into the host.
Reference: [GSRD v15.1 - microSD Card - Arrow SoCKit Edition](https://rocketboards.org/foswiki/view/Documentation/GSRD151SDCardArrowSoCKitEdition)