1 Preparing the environment and software packages
This tutorial is a simulation on an Ubuntu system, which can be installed on a physical machine or virtual machine. The following table lists the software versions selected for this tutorial:
Before simulating, you need to download and upload the package to the Ubuntu directory (any directory is optional, for this tutorial, /home/xt), as shown below:
2 Replace the Ubunut OS with a domestic source
In order to improve the download speed, it is recommended to change the Ubuntu source to a domestic source. The replacement procedure is as follows:
2.1 Log in to the Ubuntu Background and switch to user root
2.2 Perform the following operations in sequence to replace the device
# mv/etc/apt/sources. List/etc/apt/sourses. List. Backup# backup sources. The list file # vim/etc/apt/sources list # new sources. The list file and add the following content deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverseCopy the code
Add a screenshot as shown below
Press ESC to enter command mode and then :wq to save the configuration and exit.
2.3 Run the following command to update information
#apt update #
#apt Upgrade # Update installed packages
3 the qemu installation
Qemu is an open source hosted virtual machine that uses pure software to implement virtualization emulators that can simulate almost any hardware device. This tutorial uses QEMU to simulate the ARM platform with aARCH64 architecture.
If QEMu has been installed on ubuntu, uninstall QEMui by referring to section 8.1.
3.1 Going to the Ubuntu /home/xt Directory
#cd /home/xt
3.2 Decompress the QEMu-6.0.0.tar. xz software package
# tar XVF qemu – 6.0.0. Tar. Xz
3.3 Going to the QEMU directory
# CD qemu – 6.0.0
3.4./configure: Ninja cannot be found
3.4.1 track installation ninja
#git clone git://github.com/ninja-build/ninja.git && CD Ninja
Py –bootstrap # Execute the configuration file. If this step fails, execute the following command
#apt-get install python
#apt-get install re2c
Re-execute #./configure.py –bootstrapa
#cp -r ninja /usr/bin/
Verify ninja installation by executing the following command
#ninja –version
If an error occurs, run the following command
#apt-get install ninja-build
3.4.2 Perform 3.4 again
ERROR: glib-2.48 gthread-2.0 is required to compile QEMU
#apt-get install build-essential zlib1g-dev pkg-config libglib2.0-dev binutils-dev libboost-all-dev autoconf libtool libssl-dev libpixman-1-dev libpython-dev python-pip python-capstone virtualenv
3.5 Compiling QEMU (This step takes about 30 minutes)
#make
3.6 install qemu
#make install
3.7 Verifying the QEMU installation
After the installation, enter qemu in the command line and press TAB to automatically complete the installation, indicating that the installation is successful (query version as shown below)
4 Install the ARM cross-compiler
Since we compile on x86 platform and run on ARM system, the two platforms have different instruction sets, so we need to cross-compile to get programs that can be executed on ARM system. The cross-compilation tool used in this tutorial is aARCH64-linux-GNU -. Follow the instructions below to install the cross-compilation tool.
4.1 Querying the Current Installable version
#apt-cache search aarch64
4.2 Installing the GCC cross-compiler
#sudo apt-get install gcc-8-aarch64-linux-gnu
4.3 installing the g++ cross-compiler
#sudo apt-get install g++-8-aarch64-linux-gnu
4.4 Installing the GCC cross-compiler without version number
#sudo apt-get install gcc-aarch64-linux-gnu
4.5 installing g++ cross-compiler without version number
#sudo apt-get install g++-aarch64-linux-gnu
5 Kernel configuration and compilation
The function of kernel is also self-evident, which is equivalent to our Windows operating system. Without this operating system, hardware is a pile of scrap metal. When the system boots, the kernel is loaded into memory and executed from the kernel entry address.
Perform the following steps to configure and compile the kernel.
5.1 Going to the Ubuntu /home/xt Directory
#cd /home/xt
5.2 Decompressing the Linux-5.12.9.tar. xz Software Package
# tar XVF Linux – 5.12.9. Tar. Xz
5.3 Accessing the Linux Directory
# CD Linux – 5.12.9
5.4 Run the following command to configure the kernel
#export ARCH=arm64
#export CROSS_CONMPILE=aarch64-linux-gnu-
#make defconfig
#make menuconfig # If this step fails, execute
#apt-get install libncurses* flex bison
Run make Menuconfig again, and the following interface is displayed
Perform the following two configurations in sequence.
Select General Setup, and select Initial RAM Filesystem and RAM Disk (initramfs/initrd) Support.
Select Device Drivers – Block Devices, select RAM Block Device Support, and set the disk size (kbytes) of the Default RAM to an appropriate size (not smaller than the size of the file system mirror).
5.5 Generating a Kernel Image (This Step Takes About 30 Minutes)
#make Image
After the Image is compiled successfully, as shown in the figure, the kernel file ARCH /arm64/boot/Image is compiled. Qemu needs to specify the use of this Image file when starting.
6 Create a root file system
After the kernel starts, at the last step, it needs to mount the root file system and then execute the execution program specified in the file system.
6.1 Going to the Ubuntu /home/xt Directory
#cd /home/xt
6.2 Decompressing the BusyBox-1.25.0.tar. bz2 software package
# tar XJVF busybox – 1.25.0. Tar..bz2
6.3 Accessing the BusyBox Directory
# CD busybox – 1.25.0
6.4 Run the following commands to configure the compilation environment
#make defconfig
#make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
#make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- install
6.5 Creating the Rootfs Root Directory in /home/xt/
#cd /home/xt
# create rootfs folder
# CD rootfs # Enter the rootfs directory
#mkdir lib dev proc etc #mkdir lib dev proc etc
#cp busybox-1.25.0/_install/* -r rootfs/ # copy command files generated under _install to rootfs
#cp -p /usr/aarch64-linux-gnu/lib/* rootfs/lib/ #cp -p /usr/aarch64-linux-gnu/lib/
# cp -r busybox – 1.25.0 / examples/bootfloppy/etc / * rootfs/etc /
D /rcS(mount -t proc proc/proc)
#vim rootfs/etc/init.d/rcS
Go to the rootfs directory
#cd /home/xt/rootfs
Create four TTY terminal devices (C stands for character devices, 4 is the primary device number, and 1 to 4 are secondary devices)
#mknod dev/tty1 c 4 1
#mknod dev/tty2 c 4 2
#mknod dev/tty3 c 4 3
#mknod dev/tty4 c 4 4
Create terminals and recycle bins
#mknod console c 5 1
#mknod null c 1 3
6.6 Run the following command to generate an SD Card Image
#dd if=/dev/zero of=rootfs.ext3 bs=1M count=32
Ext3 rootfs. Ext3 # Format the generated ext3 filesystem
Create a TMPFS folder
#mount -t ext3 rootfs.ext3 TMPFS / -o loop
-r rootfs/* TMPFS /
#umount TMPFS
7 QEMU startup and test examples
According to the different test objectives, it is divided into the following two test methods. 7.1 Suitable for applications that only need to be tested in the upper layer of the OS without paying attention to the bottom layer of the application; 7.2 A program suitable for testing on a bare machine (CPU).
7.1 Testing Applications in the Linux kernel
7.1.1 Run the qEMU command to start an ARM VM
# qemu – system – aarch64 – machine virt – CPU architecture a72 – nographic – m 2-1024 – SMP kernel Linux – 5.12.9 / arch/arm64 / boot/Image -initrd rootfs.ext3 -append “root=/dev/ram0 rdinit=/linuxrc console=ttyAMA0”
The screenshot of successful operation is as follows:
7.1.2 Simply test the Hello-world application
A. In any Ubuntu directory (/home/xt for this tutorial), write the HelloWorld executable hello.c:
#include <stdio.h>
int main()
{
printf(“HelloWorld! \n”);
return 0;
}
B. Cross-compile hello.c to get executable program Hello:
#aarch64-linux-gnu-gcc hello.c -o hello
C. Copy the Hello executable program to the disk image rootfs.ext3
#mount -o loop rootfs.ext3 TMPFS /
#cp myhello TMPFS / # copy
#umount TMPFS
D. Run the Hello program
Perform step 7.1 to start the VM again. The myHello file is displayed in the root directory. Run the command to view the output.
7.1.3 Sample test program
Upload xt_DHry_OS to any ubuntu directory
Unzip xt_dhry_os
# CD xt_dhry_os # go to xt_dhry_os
#make clean # Clear the previous compilation result
# make # compiler
#./run # Start the ARM virtual machine
The running screenshot is shown below (the screenshot only shows part of it)
After logging in to the ARM VM, run
#./dhry
The execution result is as follows (screenshot only shows part of it)
Exit qEMU emulation: press Ctrl+A, then X.
7.2 Bare machine test procedure
Upload the following xt_Hello folder to any ubuntu directory
Unzip xt_hello
# CD xt_hello # Go to xt_hello directory
#make clean # Clear the previous compilation result
# make # compiler
Start the test program
The following screenshot shows the operation
Exit qEMU emulation: press Ctrl+A, then X.
8 Address Mapping
Please refer to xx(directory)/ qEMU-6.0.0 /hw/ ARM /virt.c, part of which is as follows.
If you need to test the source code, contact me at [email protected]