1e41f4b71Sopenharmony_ci# Linux Kernel 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci## Overview 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ciLinux kernel porting involves basic kernel compilation, building, and verification after third-party chipset patches are installed based on the Linux kernel baseline. 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci### Basic Information 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ciThe current Linux kernel baseline evolves based on the Linux LTS version 4.19 and incorporates the CVE and bugfix patches. For details, see the [kernel_linux code repository](https://gitee.com/openharmony/kernel_linux). The code path for the **repo** project is **kernel/linux-4.19**. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci### Bootloader 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ciYou can use the Bootloader provided by the chipset vendor or open-source U-Boot to load the kernel image. For example, you can use [U-Boot](https://gitee.com/openharmony/third_party_u-boot) for the Hi3516D V300 development board. 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci## Adaptation, Building, Burning, and Startup 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci1. Prepare the kernel configuration files, especially the chipset-related configuration files. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci Source code directory of the configuration files: **kernel/linux/config/** 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci Create a **<*YOUR_CHIP*>_small_defconfig** file, such as **hi3516dv300_small_defconfig**, in the **linux-4.19/arch/arm/configs/** directory. The configuration file can be created by combining the general-purpose **small_common_defconfig** file and chipset-specific configurations. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci2. Prepare the chipset patches. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci Source code directory of the patch files: **kernel/linux/patches/linux-4.19** 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci Create a **<*YOUR_CHIP*>_patch** directory by referring to the existing patch directory **hi3516dv300_small_patch**, and place the related chipset patches, such as **hdf.patch** (recommended), in this directory. 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci3. Start the build. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci In the project directory **kernel/linux/patches/**, after version-level build commands are passed to the **kernel_module_build.sh** and **kernel.mk** files, adapt the **patch** and **defconfig** configuration file paths, compiler, chipset architecture, and kernel image format. 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci Adjust the patches based on build error logs. Typical error scenarios are as follows: 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci (1) A conflict occurs in installing a patch. In this case, context adaptation is required. 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci (2) The build fails due to kernel version mismatch. In this case, kernel adaptation is required, including function implementation adjustment. 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci >  **CAUTION**<br/> 44e41f4b71Sopenharmony_ci > - As in the **kernel.mk** file, patches are applied after the code environment of **kernel/linux-4.19** is copied during compilation and building of the OpenHarmony project. Retain the original code environment of **kernel/linux-4.19** before running the OpenHarmony version-level build command. 45e41f4b71Sopenharmony_ci > 46e41f4b71Sopenharmony_ci > - You can modify the patches in **out/<\*\*\*>/kernel/linux-4.19**, to which the code environment is copied. 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci4. Burn images and start the development board. 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci The burning mode varies according to the development board of the chipset. Pay attention to the size of each burnt image and the configuration of the boot parameters. Below is the U-Boot parameter settings of Hi3516D V300: 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci ``` 54e41f4b71Sopenharmony_ci setenv bootargs 'mem=128M console=ttyAMA0,115200 root=/dev/mmcblk0p3 ro rootfstype=ext4 rootwait blkdevparts=mmcblk0:1M(boot),9M(kernel),50M(rootfs),50M(userfs)' 55e41f4b71Sopenharmony_ci ``` 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci## Verification 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ciDebug the **init** process, start shell, and run a simple program in the user space to check whether the kernel porting is successful. Below is the OS image structure of the OpenHarmony small system and the Linux user-space program startup process. 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci**Figure 1** OS image structure and user-space program startup process based on the Linux kernel 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ciBased on the preceding process, the recommended verification procedure is as follows: 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci1. Create a root file system image. 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci Create a root file system image **rootfs.img** by following instructions in [Building Procedures](../subsystems/subsys-build-all.md). As shown in the preceding figure, the startup process is closely related to the product configuration. You need to complete the following configuration when creating **rootfs.img**: 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci - Component configuration 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci In the product component configuration file ***vendor*/{*company*}/{*product*}/config.json**, configure the **init** component of the startup subsystem and the **linux_4_1_9** component of the kernel subsystem. 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci - System service configuration 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci Modify the system service configuration file ***vendor*/{*company*}/{*product*}/init_configs/init_xxx.cfg** to start the shell service. 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci - File system configuration 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci In the file system configuration file ***vendor*/{*company*}/{*product*}/fs.yml**, create the **/bin/sh -> mksh** and **/lib/ld-musl-arm.so.1 -> libc.so** soft links. These two files are the shell executable program and the c library on which the executable program depends, respectively. 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci - Startup configuration 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci In the ***vendor*/{*company*}/{*product*}/init_configs/etc** directory, configure startup settings, including the **fstab**, **rsS**, and **S*xxx*** files. Configure the startup settings as needed. 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci After the build is complete, check the **rootfs** content in the product compilation output directory to determine whether the generated **rootfs.img** file meets the expectation. 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci2. Debug the init process and shell. 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci Burn **rootfs.img** and debug the init process and shell. The burning tools and processes vary according to the development board. Follow the instructions provided by the chipset solution vendor. Before burning **rootfs.img**, ensure that the bootloader and Linux kernel are started properly. When **rootfs.img** is properly mounted by the kernel, the **/bin/init** program is executed, indicating the start of the user space. 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci The init process calls the **/etc/init.d/rcS** script. The **rcS** script runs the first command **/bin/mount -a** to load the **fstab** file. After the commands in this file are executed, **rcS** calls the **S*xxx*** scripts in sequence to create and scan for device nodes and configure file permissions. 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci Then the init process reads the **init.cfg** system service configuration file and starts the shell as configured. If the preceding process is executed properly, the system enters the shell. 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci If the init startup log contains the version number, the init program is started properly: 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci **Figure 2** Log indicating that the init process is started properly 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci  103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci After entering the shell, run the **ls** command. The following figure shows the information printed over the serial port. 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci **Figure 3** Information printed after the ls command is executed in the shell 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci  109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci3. Configure the NFS. 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci After the init process and shell are started, run the following command in the root directory to enable the NFS service. In this example, the server IP address is 192.168.1.22 and the client IP address is 192.168.1.4. 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci ``` 115e41f4b71Sopenharmony_ci ifconfig eth0 192.168.1.4 netmask 255.255.255.0 116e41f4b71Sopenharmony_ci mkdir -p /storgage/nfs 117e41f4b71Sopenharmony_ci mount -t nfs -o nolock,addr=192.168.1.22 192.168.1.22:/nfs /storage/nfs 118e41f4b71Sopenharmony_ci ```