1e41f4b71Sopenharmony_ci# A Method for Rapidly Porting the OpenHarmony Linux Kernel 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Overview 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciThis document describes how to quickly port OpenHarmony to a third-party chip platform by using the existing capabilities of the Linux kernel of the third-party chip platform. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## Overall Porting Approach 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci### Kernel Mode Layer and User Mode Layer 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ciFor easy description, we divide the OpenHarmony architecture into two parts: 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ciOpenHarmony = Kernel mode layer + User mode layer 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciThe kernel mode layer is the OpenHarmony kernel layer \(purple part in the figure\). It consists of the kernel, such as Linux Kernel and LiteOS, and features, such as Hardware Driver Foundation \(HDF\), running in the kernel mode. 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciThe user mode layer includes other parts except the OpenHarmony kernel layer. It consists of the system service layer, framework layer, and application layer from the bottom to the top. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ciThe user mode layer of OpenHarmony is loosely coupled with the third-party chip platform and is easy to port. The kernel and HDF at the kernel mode layer are closely coupled with the third-party chip platform and are difficult to port. This document focus on the porting of the kernel mode layer. In addition, it describes only the quick porting of the Linux kernel. It does not include the porting of LiteOS. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci### Composition of the Kernel Mode Layer 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ciThe OpenHarmony kernel mode layer can be further divided as follows: 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ciOpenHarmony kernel mode layer = OpenHarmony Linux kernel + OpenHarmony kernel-mode features \(mandatory features, such as HDF, and optional features, such as HMDFS\) 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ciOpenHarmony Linux kernel = Standard LTS Linux kernel + Third-party SoC platform code + OpenHarmony basic kernel-mode code \(most basic code required for running of the OpenHarmony user mode layer\) 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ciTherefore, the OpenHarmony kernel mode layer includes the following: 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci- Standard LTS Linux kernel 34e41f4b71Sopenharmony_ci- Third-party SoC platform code 35e41f4b71Sopenharmony_ci- OpenHarmony basic kernel-mode code 36e41f4b71Sopenharmony_ci- OpenHarmony kernel-mode features, such as HDF 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ciThe standard LTS Linux kernel and third-party SoC chip platform code constitute the basis of a third-party Linux kernel. The OpenHarmony kernel mode layer can be composed of either of the following: 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci- OpenHarmony kernel mode layer = Third-party Linux kernel + OpenHarmony basic kernel-mode code + OpenHarmony kernel-mode features \(such as HDF and HMDFS\) 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci In this case, the OpenHarmony kernel mode layer consists of the third-party Linux kernel and OpenHarmony basic code and features for the kernel mode. 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci- OpenHarmony kernel mode layer = OpenHarmony Linux kernel + OpenHarmony kernel-mode features \(such as HDF and HMDFS\) 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci In this case, the OpenHarmony kernel mode layer consists of OpenHarmony kernel and features. However, the OpenHarmony kernel supports few third-party chip platforms. 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ciIn the following, we elaborate how to port OpenHarmony that uses with the third-party Linux kernel. 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci### Overall Porting Process 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ciThe porting process is as follows: 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci1. Prepare the build environment, including copying the existing kernel code of the third-party chip platform to the OpenHarmony build environment. 59e41f4b71Sopenharmony_ci2. Port the OpenHarmony basic kernel-mode code. 60e41f4b71Sopenharmony_ci3. Port OpenHarmony mandatory kernel-mode features \(such as HDF\). 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci## Procedure 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ciThe following uses Raspberry Pi 3b \(BCM2837\) as an example to describe how to port OpenHarmony to Raspberry Pi. 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci### Setting Up the Environment 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci1. Copy the third-party kernel code to the OpenHarmony build environment. 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci Build the standard Hi3516D V300 kernel, clone the Raspberry Pi kernel source code, and copy it to the manifest output directory. 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci ``` 73e41f4b71Sopenharmony_ci export PROJ_ROOT=[OpenHarmony manifest] 74e41f4b71Sopenharmony_ci git clone https://gitee.com/xfan1024/oh-rpi3b-kernel.git 75e41f4b71Sopenharmony_ci cp -r oh-rpi3b-kernel $PROJ_ROOT/out/KERNEL_OBJ/kernel/src_tmp/linux-rpi3b 76e41f4b71Sopenharmony_ci ``` 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci2. Configure the Raspberry Pi kernel build environment. 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci ``` 81e41f4b71Sopenharmony_ci # Go to the Raspberry Pi kernel directory. 82e41f4b71Sopenharmony_ci cd out/KERNEL_OBJ/kernel/src_tmp/linux-rpi3b 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci # Configure the build environment, and use clang provided by the project to build the Raspberry Pi kernel source code. 85e41f4b71Sopenharmony_ci export PATH=$PROJ_ROOT/prebuilts/clang/ohos/linux-x86_64/llvm/bin:$PROJ_ROOT/prebuilts/gcc/linux-x86/arm/gcc-linaro-7.5.0-arm-linux-gnueabi/bin/:$PATH 86e41f4b71Sopenharmony_ci export MAKE_OPTIONS="ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- CC=clang HOSTCC=clang" 87e41f4b71Sopenharmony_ci export PRODUCT_PATH=vendor/hisilicon/hispark_taurus_linux 88e41f4b71Sopenharmony_ci ``` 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci3. Comment out the flags that cannot be recognized by **clang**. 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci In the **PROJ\_ROOT/out/KERNEL\_OBJ/kernel/src\_tmp/linux-rpi3b/arch/arm/Makefile** file, comment out the following line: 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci ``` 95e41f4b71Sopenharmony_ci KBUILD_CFLAGS +=-fno-omit-frame-pointer -mapcs -mno-sched-prolog 96e41f4b71Sopenharmony_ci ``` 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci### Porting Basic Kernel-Mode Code 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ciCurrently, the basic kernel-mode code of OpenHarmony is related to the log service. The lightweight kernel log service code includes the following: 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci``` 104e41f4b71Sopenharmony_cidrivers/staging/hilog 105e41f4b71Sopenharmony_cidrivers/staging/hievent 106e41f4b71Sopenharmony_ci``` 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ciCopy the preceding code from the **kernel/linux/linux-4.19/drivers/staging** directory of OpenHarmony to **out/KERNEL\_OBJ/kernel/src\_tmp/linux-rpi3b/drivers/staging**. 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ciAdd the following code to the third-party kernel file **Kconfig** in the **drivers/staging** directory: 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci``` 113e41f4b71Sopenharmony_cisource "drivers/staging/hilog/Kconfig" 114e41f4b71Sopenharmony_cisource "drivers/staging/hievent/Kconfig" 115e41f4b71Sopenharmony_ci``` 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ciAdd the following code to the third-party kernel file **Makefile** in the **drivers/staging** directory: 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci``` 120e41f4b71Sopenharmony_ciobj-$(CONFIG_HILOG) += hilog/ 121e41f4b71Sopenharmony_ciobj-$(CONFIG_HIEVENT) += hievent/ 122e41f4b71Sopenharmony_ci``` 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ciEnable the CONFIG macros **CONFIG\_HILOG** and **CONFIG\_HIEVENT** in the kernel **config** section. 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ciFor details about how to use the logs, see [Hilog\_lite](https://gitee.com/openharmony/hiviewdfx_hilog_lite/blob/master/README.md). 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci### Porting the HDF Feature 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci1. Install HDF patches. 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci Run the shell script to apply HDF patches. 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci 1. Set the four variables in the **patch\_hdf.sh** script. 135e41f4b71Sopenharmony_ci 2. Obtain the **patch\_hdf.sh** script. 136e41f4b71Sopenharmony_ci 3. Run the **patch\_hdf.sh** script to pass the four variables in sequence. 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci Run the following command: 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci ``` 141e41f4b71Sopenharmony_ci ./patch_hdf.sh [Project root directory path] [Kernel directory path] [Kernel patch path] [Device name] 142e41f4b71Sopenharmony_ci ``` 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci The following uses Raspberry Pi 3b as an example: 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci ``` 147e41f4b71Sopenharmony_ci # Go to the Raspberry Pi kernel directory. 148e41f4b71Sopenharmony_ci PROJ_ROOT/drivers/hdf_core/adapter/khdf/linux/patch_hdf.sh \ 149e41f4b71Sopenharmony_ci PROJ_ROOT # Specify the path of the project root directory.\ 150e41f4b71Sopenharmony_ci PROJ_ROOT/out/KERNEL_OBJ/kernel/src_tmp/linux-rpi3b # Specify the kernel directory for applying the patch.\ 151e41f4b71Sopenharmony_ci PROJ_ROOT/kernel/linux/patches/linux-4.19 # Kernel patch patch.\ 152e41f4b71Sopenharmony_ci hi3516dv300 # Device name. 153e41f4b71Sopenharmony_ci ``` 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci2. Configure the **config** file. 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci Configure basic HDF settings. If other functions are required, enable the corresponding driver switch by using **menuconfig**. 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci After HDF patches are installed, the HDF function is disabled by default. To enable the HDF function, perform the following settings: 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci ``` 162e41f4b71Sopenharmony_ci CONFIG_DRIVERS_HDF=y 163e41f4b71Sopenharmony_ci CONFIG_HDF_SUPPORT_LEVEL=2 164e41f4b71Sopenharmony_ci CONFIG_DRIVERS_HDF_PLATFORM=y 165e41f4b71Sopenharmony_ci CONFIG_DRIVERS_HDF_PLATFORM_MIPI_DSI=y 166e41f4b71Sopenharmony_ci CONFIG_DRIVERS_HDF_PLATFORM_GPIO=y 167e41f4b71Sopenharmony_ci CONFIG_DRIVERS_HDF_PLATFORM_I2C=y 168e41f4b71Sopenharmony_ci CONFIG_DRIVERS_HDF_PLATFORM_UART=y 169e41f4b71Sopenharmony_ci CONFIG_DRIVERS_HDF_TEST=y 170e41f4b71Sopenharmony_ci ``` 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci Alternatively, run the following command to enable the HDF configuration on the **menuconfig** page: 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci ``` 175e41f4b71Sopenharmony_ci # Generate the .config file. 176e41f4b71Sopenharmony_ci make ${MAKE_OPTIONS} rpi3b_oh_defconfig 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci # Modify the HDF kernel configuration. 179e41f4b71Sopenharmony_ci make ${MAKE_OPTIONS} menuconfig 180e41f4b71Sopenharmony_ci # [*] Device Drivers 181e41f4b71Sopenharmony_ci # [*] HDF driver framework support ---> 182e41f4b71Sopenharmony_ci ``` 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci The configuration \(in **Device Drivers** \> **HDF driver framework support**\) is as follows: 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci  187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci### Building the Image 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci``` 192e41f4b71Sopenharmony_ci# Run the following command: 193e41f4b71Sopenharmony_cimake ${MAKE_OPTIONS} -j33 zImage 194e41f4b71Sopenharmony_ci``` 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci### \(Optional\) Building and Running HDF Test Cases 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci**Overview** 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ciThe HDF test cases are used to verify basic functions of the HDF framework and peripherals. This document describes the HDF kernel-mode test cases. 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci**Prerequisites** 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci- In **menuconfig**, **CONFIG\_DRIVERS\_HDF\_TEST** is set to **y**. 205e41f4b71Sopenharmony_ci- The standard Hi3516D V300 kernel is built completely. 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci**Test Case Build and Test Method** 208e41f4b71Sopenharmony_ci 209e41f4b71Sopenharmony_ciUse the [hdc_std](../subsystems/subsys-toolchain-hdc-guide.md) tool to push the test case execution file to the device and execute the test cases. The procedure is as follows: 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci1. Build the HDF test cases. 212e41f4b71Sopenharmony_ci2. Use the **hdc_std** tool to push the test case execution file to the device. 213e41f4b71Sopenharmony_ci3. Go to the **data/test** directory of the device and execute the test file. 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ciThe procedure is as follows: 216e41f4b71Sopenharmony_ci 217e41f4b71Sopenharmony_ci1. Build the HDF test cases. 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ci Run the following command: 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_ci ``` 222e41f4b71Sopenharmony_ci ./build.sh --product-name hispark_taurus_standard --build-target hdf_test 223e41f4b71Sopenharmony_ci ``` 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci Wait until the build is complete. 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ci2. Copy the test files to the target device \(Raspberry Pi in this example\). 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_ci Method 1: Use the [hdc_std](../subsystems/subsys-toolchain-hdc-guide.md) tool. 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ci 1. Create the **data/test** directory in Raspberry Pi. 232e41f4b71Sopenharmony_ci 233e41f4b71Sopenharmony_ci ``` 234e41f4b71Sopenharmony_ci mkdir -p data/test 235e41f4b71Sopenharmony_ci ``` 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_ci 2. Push the dependencies and test cases to Raspberry Pi. 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ci ``` 240e41f4b71Sopenharmony_ci hdc file send XXX\out\{device_name}\hdf\hdf\libhdf_test_common.z.so /system/lib 241e41f4b71Sopenharmony_ci hdc file send XXX\out\{device_name}\tests\unittest\hdf\config\hdf_adapter_uhdf_test_config /data/test 242e41f4b71Sopenharmony_ci hdc file send XXX\out\{device_name}\tests\unittest\hdf\devmgr\DevMgrTest /data/test 243e41f4b71Sopenharmony_ci hdc file send XXX\out\{device_name}\tests\unittest\hdf\osal\OsalTest /data/test 244e41f4b71Sopenharmony_ci hdc file send XXX\out\{device_name}\tests\unittest\hdf\sbuf\SbufTest /data/test 245e41f4b71Sopenharmony_ci ``` 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ci Method 2: Copy the test files to Raspberry Pi using its memory card. 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ci 1. Remove the serial port and USB cable of Raspberry Pi from the PC, and remove its memory card. The **zImage** file in the memory card will be replaced. Back it up in advance. 250e41f4b71Sopenharmony_ci 2. Insert the memory card into the port of the PC used for porting, download the **zImage** file and the **test/** folder to the PC, and then copy them to the root directory of the memory card. 251e41f4b71Sopenharmony_ci 3. Insert the memory card into Raspberry Pi. 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci ``` 254e41f4b71Sopenharmony_ci # Enable the Raspberry Pi file system to read the root directory of the memory card. 255e41f4b71Sopenharmony_ci mount -t vfat /dev/block/mmcblk0p1 /boot 256e41f4b71Sopenharmony_ci cd /boot/[Test file directory] 257e41f4b71Sopenharmony_ci # Enable system files to be modified. 258e41f4b71Sopenharmony_ci mount -o remount,rw / 259e41f4b71Sopenharmony_ci # Install the test library. 260e41f4b71Sopenharmony_ci mv libhdf_test_common.z.so /system/lib 261e41f4b71Sopenharmony_ci mkdir /data/test 262e41f4b71Sopenharmony_ci mv * /data/test 263e41f4b71Sopenharmony_ci ``` 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_ci3. Perform the test. 266e41f4b71Sopenharmony_ci 1. Go to the **data/test** directory. 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci ``` 269e41f4b71Sopenharmony_ci cd /data/test 270e41f4b71Sopenharmony_ci ``` 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ci 2. Assign the execute permission on the test files. 273e41f4b71Sopenharmony_ci 274e41f4b71Sopenharmony_ci ``` 275e41f4b71Sopenharmony_ci chmod 777 hdf_adapter_uhdf_test_config DevMgrTest OsalTest SbufTest 276e41f4b71Sopenharmony_ci ``` 277e41f4b71Sopenharmony_ci 278e41f4b71Sopenharmony_ci 3. Start the test. 279e41f4b71Sopenharmony_ci 280e41f4b71Sopenharmony_ci ``` 281e41f4b71Sopenharmony_ci ./hdf_adapter_uhdf_test_config 282e41f4b71Sopenharmony_ci ./DevMgrTest 283e41f4b71Sopenharmony_ci ./OsalTest 284e41f4b71Sopenharmony_ci ./SbufTest 285e41f4b71Sopenharmony_ci ``` 286e41f4b71Sopenharmony_ci 287e41f4b71Sopenharmony_ci 4. If **PASSED** is displayed for all test items, HDF is functioning. 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ci Example: DevMgrTest case 290e41f4b71Sopenharmony_ci 291e41f4b71Sopenharmony_ci ``` 292e41f4b71Sopenharmony_ci ./DevMgrTest 293e41f4b71Sopenharmony_ci Running main() from gmock_main.cc 294e41f4b71Sopenharmony_ci [==========] Running 1 test from 1 test case. 295e41f4b71Sopenharmony_ci [----------] Global test environment set-up. 296e41f4b71Sopenharmony_ci [----------] 1 test from DevMgrTest 297e41f4b71Sopenharmony_ci [ RUN ] DevMgrTest.DriverLoaderTest_001 298e41f4b71Sopenharmony_ci [ OK ] DevMgrTest.DriverLoaderTest_001 (0 ms) 299e41f4b71Sopenharmony_ci [----------] 1 test from DevMgrTest (0 ms total) 300e41f4b71Sopenharmony_ci [----------] Global test environment tear-down 301e41f4b71Sopenharmony_ci Gtest xml output finished 302e41f4b71Sopenharmony_ci [==========] 1 test from 1 test case ran. (0 ms total) 303e41f4b71Sopenharmony_ci [ PASSED ] 1 test. 304e41f4b71Sopenharmony_ci ``` 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_ci 307e41f4b71Sopenharmony_ci 308