Building an Embedded Linux Environment on Loongson Development Board: A Complete Guide from Scratch

Building an Embedded Linux Environment on Loongson Development Board: A Complete Guide from Scratch

The embedded Linux system, with its open-source, stable, and highly customizable features, has become an important operating system choice for the Loongson platform. For engineers looking to develop applications on domestic chips, setting up an efficient embedded Linux development environment is fundamental. This article is aimed at developers with a certain foundation in Linux and provides a detailed introduction to the complete setup process of the embedded Linux environment on the Loongson platform.

Hardware and Software Requirements

Hardware requirements:

  • Loongson 2K1000 development board (based on LoongArch architecture, 1GHz clock speed)
  • USB to serial cable
  • Power adapter (12V/2A)
  • MicroSD card (≥16GB)

Software requirements:

  • Host operating system: Ubuntu 20.04 LTS
  • Loongson cross-compilation toolchain (loongarch64-linux-gnu-gcc 8.3.0)
  • Git (version 2.25.1 or higher)
  • Linux kernel source code (5.10.x branch)
  • Buildroot (2023.02.x)

Building an Embedded Linux Environment on Loongson Development Board: A Complete Guide from Scratch

Overview of the Development Environment

The embedded Linux development environment for the Loongson platform consists of host-side development tools and the target board runtime environment. On the host side, we compile the kernel and applications suitable for the LoongArch architecture using the cross-compilation toolchain; on the target board, a customized Linux system runs. The entire development process includes steps such as toolchain configuration, kernel compilation, root filesystem construction, and system deployment, with each step interdependent and closely related.

Preparing the Host Development Environment

First, install the necessary development dependencies on the Ubuntu host:

# Install development dependencies
sudo apt-get update
sudo apt-get install -y build-essential git ncurses-dev xz-utils libssl-dev bc flex bison libelf-dev

Create a working directory and download the Loongson cross-compilation toolchain:

mkdir -p ~/loongson-dev && cd ~/loongson-dev
wget http://ftp.loongnix.cn/toolchain/gcc8.3/loongarch64-linux-gnu-toolchain-8.3.0.tar.gz
tar xf loongarch64-linux-gnu-toolchain-8.3.0.tar.gz -C /opt/

Configuring the Cross-Compilation Toolchain

Configure the toolchain environment variables and add them to the ~/.bashrc file:

# Set cross-compilation toolchain environment variables
export PATH=/opt/loongarch64-linux-gnu-toolchain-8.3.0/bin:$PATH
export CROSS_COMPILE=loongarch64-linux-gnu-
export ARCH=loongarch

Update the current session’s environment variables and verify:

source ~/.bashrc
loongarch64-linux-gnu-gcc --version

Obtaining and Configuring the Kernel Source Code

Obtain the Linux kernel source code suitable for the Loongson platform:

cd ~/loongson-dev
git clone https://github.com/loongson/linux-5.10.git
cd linux-5.10

Configure and compile the kernel:

# Kernel compilation example
make loongson2k_defconfig
make menuconfig  # Adjust configuration as needed
make -j$(nproc) Image dtbs modules

Building the Root Filesystem

Use Buildroot to build a customized root filesystem:

cd ~/loongson-dev
git clone https://git.buildroot.net/buildroot
cd buildroot
git checkout 2023.02.x

Configure Buildroot:

make menuconfig
# Configure the following key options:
# - Target options -> Target Architecture: LoongArch 64
# - Toolchain -> Toolchain type: External toolchain
# - Toolchain -> Toolchain path: /opt/loongarch64-linux-gnu-toolchain-8.3.0

Compile the root filesystem:

make -j$(nproc)

Building an Embedded Linux Environment on Loongson Development Board: A Complete Guide from Scratch

System Boot Configuration

Obtain and configure the U-Boot bootloader:

cd ~/loongson-dev
git clone https://github.com/loongson/u-boot.git
cd u-boot
make loongson2k_defconfig
make -j$(nproc)

Create the boot configuration file bootcmd.txt:

setenv bootargs 'console=ttyS0,115200 root=/dev/mmcblk0p2 rootwait rw'
setenv loadaddr 0x90000000
setenv fdtaddr 0x95000000
load mmc 0:1 ${loadaddr} Image
load mmc 0:1 ${fdtaddr} loongson2k.dtb
booti ${loadaddr} - ${fdtaddr}

Deploying the System on the Development Board

Prepare the SD card partition:

sudo fdisk /dev/sdX  # Replace X with the actual SD card letter
# Create two partitions:
# - Partition 1: 100MB (FAT32, bootable)
# - Partition 2: Remaining space (ext4)

Deploy the system files:

# Mount partitions
sudo mount /dev/sdX1 /mnt/boot
sudo mount /dev/sdX2 /mnt/rootfs

# Copy boot files
sudo cp ~/loongson-dev/u-boot/u-boot.bin /mnt/boot/
sudo cp ~/loongson-dev/linux-5.10/arch/loongarch/boot/Image /mnt/boot/
sudo cp ~/loongson-dev/linux-5.10/arch/loongarch/boot/dts/loongson/loongson2k.dtb /mnt/boot/
sudo cp bootcmd.txt /mnt/boot/

# Copy root filesystem
sudo tar -xf ~/loongson-dev/buildroot/output/images/rootfs.tar -C /mnt/rootfs/

Building an Embedded Linux Environment on Loongson Development Board: A Complete Guide from Scratch

Environment Testing and Verification

Connect the serial cable and use a serial tool to connect (baud rate 115200):

sudo minicom -D /dev/ttyUSB0 -b 115200

Power on the development board and observe the boot log. Once the system successfully boots, simple tests can be performed:

uname -a  # Should display Linux kernel version and Loongson architecture information
cat /proc/cpuinfo  # View processor information

Common Issues and Solutions

  1. Compilation error “unknown architecture”

  • Reason: The environment variable ARCH is set incorrectly
  • Solution: Ensure it is set correctly<span>export ARCH=loongarch</span>
  • System fails to boot

    • Reason: Device tree configuration mismatch or boot parameters error
    • Solution: Check if the dtb file corresponds to the current development board model and confirm the bootargs parameters are correct
  • Cross-compilation of applications fails

    • Reason: Missing necessary library dependencies
    • Solution: Use<span>loongarch64-linux-gnu-gcc -v</span> to check for missing libraries and add the corresponding packages in Buildroot

    Optimizing the Development Environment

    To improve development efficiency, it is recommended to configure an NFS shared directory for efficient file sharing between the host and the development board. Configuration method: Install the NFS server on the host, share the development directory, and mount this directory on the target system, allowing for code modifications without needing to reflash for testing.

    Conclusion

    By following the methods introduced in this article, you can successfully set up the embedded Linux development environment on the Loongson platform. With the continuous development of domestic chips, mastering development skills on the Loongson platform holds significant value. Future explorations can include instruction set optimizations unique to the Loongson platform, system performance tuning, and development practices for various application scenarios.

    Leave a Comment