Use QEMU to build BIOS and kernel development environment

Description

This article mainly creates a complete system, which includes the BIOS described above, and the GRUB, kernel, and file system described here.

 

Kernel download and compilation

Since the environment used is Ubuntu 18.04, downloading and compiling the kernel is relatively simple. The first is to download the kernel, you need to use the following command:

sudo apt install linux-source

The download process is as follows:

The downloaded code can be found in the /usr/src directory:

We download it and put it in the specified directory:

tar -xjvf linux-source-4.15.0.tar.bz2 -C /home/jw/code/

The -C is used to specify the destination folder for decompression, so that the Linux source code can be found in the specified directory and used. Then enter the /home/jw/code/linux-source-4.15.0 directory and start compiling. Before compiling, you need to create a .config file. For convenience, use the default configuration directly (subsequent to be modified according to the actual situation):

make defconfig

After that, you can use make to compile, but an error may be reported during the compilation process, because some dependent libraries do not exist, just install it, you need to install it here (different Ubuntu versions may require different libraries to be installed, please install according to the actual situation) :

sudo apt install libelf-dev
sudo apt install libssl-dev

After successful compilation, you can find the kernel file we need under ~/code/linux-source-4.15.0/arch/x86/boot:

At the end of the compilation, be careful not to install the kernel using make install, because we are not installing this kernel on the compiler, but need to use the virtual machine (QEMU) to run the kernel, that is, bzImage will be used for the QEMU startup system.

 

GRUB

You can download GRUB through git. GRUB 2.02 is used here. The command is as follows:

git clone https://gitee.com/jiangwei0512/grub-2.02

After the download is complete, enter the directory and first run ./configure:

./configure --target=x86_64 --with-platform=efi

Several parameters are used here, among which the --target parameter is used to specify the corresponding platform, the x86 64-bit platform is specified here, and the other parameter --with-platform is used to specify the UEFI. But there will be errors when configuring, because there are some libraries that need to be installed:

sudo apt install flex bison

After configure runs successfully, you can execute the make command. If there is no alcocal-1.15 error when compiling, you can install automake and run autoreconf -i -f to solve it, but there is an easier way to touch all the files. After make is successful, use the following command to generate GRUB binary:

./grub-mkimage -p . -d ./grub-core/ -O x86_64-efi -o bootx64.efi XXX

Briefly explain the following parameters: -p specifies the directory where grub.cfg, independent modules and other files are located; -d specifies the location of the image and module we just compiled, because they are what we need, not already existing in the system ; -O specifies the binary format, we need the UEFI format under the x86 platform; -o is used to specify the output file name; the last XXX is the GRUB module, which is the *.module file in the grub-core directory:

The modules used here are specified as follows:

./grub-mkimage -p . -d ./grub-core/ -O x86_64-efi -o bootx64.efi boot linux part_msdos part_gpt fat normal serial efi_gop minicmd

The finally compiled binary is bootx64.efi (the name is used because it is the universal OS loader name in UEFI), which will be used later. In addition, GRUB has a corresponding configuration file called grub.cfg, and its specific content will be introduced later. GRUB and its configuration files will be used for QEMU boot system.

 

busybox

You can download the source code of busybox at https://busybox.net/downloads/ , here is version 1.29.1, unzip it to the specified directory:

tar -xjvf busybox-1.29.2.tar.bz2 -C /home/jw/code

Then enter the directory for configuration (execute make menuconfig in the source code directory):

What we need to configure is to select static compilation, and the location is under Settings. Note that in order to use menuconfig, additional libraries (libncurses5-dev) may need to be installed. After configuration, you can use make to compile. After the compilation is complete, install through the make install command:

make install

After executing the above command, a new directory called _install will be generated in the current directory. We need these files to generate initramfs for the kernel to use. The corresponding files are as follows:

The method of creating initramfs, the simplest one is used here, and the corresponding command is as follows:

mkdir initramfs
cd initramfs
mkdir dev proc sys
cp ../_install/* ./ -ra
sudo cp -a /dev/{null,console,tty1,tty2,tty3,tty4} dev/
touch init
chmod a+x init

The contents of init are as follows:

mount -t proc none /proc
mount -t sysfs none /sys
mdev -s
exec /sbin/init

Finally, package it through the following command:

find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../initramfs.cpio.gz

initramfs.cpio.gz is the file we finally get, it will be used for QEMU boot system.

 

QEMU boot system

The first is to create a hard disk used by QEMU (actually just a file) and format it into FAT format (because UEFI only recognizes this type by default):

dd if=/dev/zero of=disk.img bs=1M count=32
mkfs.fat disk.img

The size of the disk.img created here is 32M. This is not a hard requirement, as long as it is larger than the sum of the kernel, initramfs, GRUB, etc. The next operation is to mount disk.img and put the files mentioned above in disk.img:

sudo mount disk.img tmp
sudo mkdir tmp/EFI tmp/EFI/BOOT
sudo cp bootx64.efi grub.cfg tmp/EFI/BOOT
sudo cp bzImage initramfs.cpio.gz tmp

We first need to say that disk.img is mounted to a directory (here is tmp), and then files are placed in it. Special attention should be paid to GRUB and its configuration files, which need to be placed in the /EFI/BOOT directory, the final file as follows:

At this point, we have put the required files in disk.img, which can be used as a QEMU hard disk. The corresponding instructions are as follows:

qemu-system-x86_64 -bios OVMF.fd -hda disk.img -m 512M

The QEMU parameters here are as follows: -bios specifies the BIOS name, here is OVMF.fd; -hda specifies the hard disk, here is disk.img; -m specifies the memory size used by the virtual machine. The final state after startup is as follows:

At this point, a basic kernel has been started, and we can rely on the system for debugging later.

 

Guess you like

Origin blog.csdn.net/jiangwei0512/article/details/108176837