# Standard System Porting Guide - [Defining a Development Board](#section132mcpsimp) - [Defining an SoC](#section135mcpsimp) - [Defining a Product](#section145mcpsimp) - [Verifying the Porting](#section163mcpsimp) - [Porting the Kernel](#section171mcpsimp) - [1. Adding a Kernel-built Subsystem to the SoC](#section174mcpsimp) - [2. Building the Kernel](#section182mcpsimp) - [3. Verifying the Porting](#section207mcpsimp) - [用户态启动引导](#section20665151016586) - [Porting the HDF Driver](#section210mcpsimp) - [1. LCD](#section212mcpsimp) - [2. Touchscreen](#section229mcpsimp) - [3. WLAN](#section274mcpsimp) - [4. Samples](#section11253153018415) This document describes the general process for porting a development board, rather than the porting process specific to a System on Chip \(SoC\). In the future, the community will provide more development board porting examples for your reference. ## Defining a Development Board This document uses the process of porting a development board named **MyProduct** as an example. This development board is provided by **MyProductVendor** and uses the SoC **MySOC** produced by **MySoCVendor**. ### Defining an SoC Create a JSON file named after the SoC name in the **//productdefine/common/device** directory and specify the CPU architecture. For example, to port **MySOC**, which uses a 32-bit ARM kernel, configure the file as follows: //productdefine/common/device/MySOC.json ``` { "target_os": "ohos", "target_cpu": "arm" } ``` Currently, **target\_cpu** can be set to **arm** only. In the future, you can set the value depending on the CPU architecture, such as **arm64**, **riscv**, or **x86**. ### Defining a Product Create a JSON file named after the product name in the **//productdefine/common/products** directory. This file is used to describe the SoC used by the product and the required subsystems. Configure the file as follows: //productdefine/common/products/MyProduct.json ``` { "product_name": "MyProduct", "product_company" : "MyProductVendor", "product_device": "MySOC", "version": "2.0", "type": "standard", "parts":{ "ace:ace_engine_standard":{}, "ace:napi":{}, ... "xts:phone_tests":{} } } ``` The main configurations are as follows: 1. **product\_device**: SoC used by the product. 2. **type**: system level. In this example, set it to **standard**. 3. **parts**: subsystem to enable. A subsystem can be treated as an independently built functional block. You can find available subsystems in **//build/subsystem\_config.json**. You can also customize subsystems. You are advised to copy the configuration file of Hi3516D V300 and delete the **hisilicon\_products** subsystem, which is used to compile the kernel for Hi3516D V300. ### Verifying the Porting Run the following command to start the build of your product: ``` ./build.sh --product-name MyProduct ``` After the build is complete, you can view the built OpenHarmony image file in **//out/ohos-arm-release/packages/phone/images**. ## Porting the Kernel Now, you need to port the Linux kernel to enable it to run successfully. ### 1. Adding a Kernel-built Subsystem to the SoC Add the following subsystem configuration to the **//build/subsystem\_config.json** file: ``` "MySOCVendor_products": { "project": "hmf/MySOCVendor_products", "path": "device/MySOCVendor/MySOC/build", "name": "MySOCVendor_products", "dir": "device/MySOCVendor" }, ``` Then, open the configuration file **//productdefine/common/products/MyProduct.json**, which is used to define the product, and add the new subsystem to the product. ### 2. Building the Kernel The OpenHarmony source code provides the Linux kernel 4.19, which is archived in **//kernel/linux-4.19**. This section uses this kernel version as an example to describe how to build the kernel. The path for building the subsystem is defined when you define the subsystem in the previous step. The path is **//device/MySOCVendor/MySOC/build**. Now, you need to create a build script in this path to instruct the build system to build the kernel. The recommended directory structure is as follows: ``` ├── build │ ├── kernel │ │ ├── linux │ │ ├──standard_patch_for_4_19.patch // Patch for the Linux kernel 4.19 │ ├── BUILD.gn │ ├── ohos.build ``` The **BUILD.gn** file is the only entry for building the subsystem. The expected build result is as follows:

File

Description

$root_build_dir/packages/phone/images/uImage

Kernel image

$root_build_dir/packages/phone/images/uboot

Bootloader image

### 3. Verifying the Porting Now start build, and check whether the kernel image is generated as expected. ## 用户态启动引导 1. 用户态进程启动引导总览。 ![](figure/overview-of-use-mode-process-startup.png) 系统上电加载内核后,按照以下流程完成系统各个服务和应用的启动: 1. 内核启动init进程,一般在bootloader启动内核时通过设置内核的cmdline来指定init的位置;如上图所示的"init=/init root/dev/xxx"。 2. 2init进程启动后,会挂载tmpfs,procfs,创建基本的dev设备节点,提供最基本的根文件系统。 3. init继续启动ueventd监听内核热插拔事件,为这些设备创建dev设备节点;包括block设备各个分区设备都是通过此事件创建。 4. init进程挂载block设备各个分区(system,vendor),开始扫描各个系统服务的init启动脚本,并拉起各个SA服务。 5. samgr是各个SA的服务注册中心,每个SA启动时,都需要向samgr注册,每个SA会分配一个ID,应用可以通过该ID访问SA。 6. foundation是一个特殊的SA服务进程,提供了用户程序管理框架及基础服务;由该进程负责应用的生命周期管理。 7. 由于应用都需要加载JS的运行环境,涉及大量准备工作,因此appspawn作为应用的孵化器,在接收到foundation里的应用启动请求时,可以直接孵化出应用进程,减少应用启动时间。 2. init。 init启动引导组件配置文件包含了所有需要由init进程启动的系统关键服务的服务名、可执行文件路径、权限和其他信息。每个系统服务各自安装其启动脚本到/system/etc/init目录下。 新芯片平台移植时,平台相关的初始化配置需要增加平台相关的初始化配置文件/vendor/etc/init/init.\{hardware\}.cfg;该文件完成平台相关的初始化设置,如安装ko驱动,设置平台相关的/proc节点信息。 init相关进程代码在//base/startup/init\_lite目录下,该进程是系统第一个进程,无其它依赖。 初始化配置文件具体的开发指导请参考 [init启动引导组件](../subsystems/subsys-boot-init.md)。 ## Porting the HDF Driver ### 1. LCD This section describes how to port a Liquid Crystal Display \(LCD\) driver. The hardware driver framework \(HDF\) designs a driver model for the LCD. To support an LCD, you must compile a driver, generate a model instance in the driver, and register the instance. The LCD drivers are stored in the **//drivers/framework/model/display/driver/panel** directory. - Create a panel driver. In the **Init** method of the driver, call **RegisterPanel** to register the model instance. ``` int32_t XXXInit(struct HdfDeviceObject *object) { struct PanelData *panel = CreateYourPanel(); // Register the model instance. if (RegisterPanel(panel) != HDF_SUCCESS) { HDF_LOGE("%s: RegisterPanel failed", __func__); return HDF_FAILURE; } return HDF_SUCCESS; } struct HdfDriverEntry g_xxxxDevEntry = { .moduleVersion = 1, .moduleName = "LCD_XXXX", .Init = XXXInit, }; HDF_INIT(g_xxxxDevEntry); ``` - Configure and load the panel driver. All device information about the product is defined in the **//vendor/MyProductVendor/MyProduct/config/device\_info/device\_info.hcs** file. Modify the file by adding configurations for the device named **device\_lcd** to the host named **display**. Note: The value of **moduleName** must be the same as that in the panel driver. ``` root { ... display :: host { device_lcd :: device { deviceN :: deviceNode { policy = 0; priority = 100; preload = 2; moduleName = "LCD_XXXX"; } } } } ``` For details about driver development, see [LCD](../driver/driver-peripherals-lcd-des.md). ### 2. Touchscreen This section describes how to port a touchscreen driver. The touchscreen driver is stored in the **//drivers/framework/model/input/driver/touchscreen** directory. To port a touchscreen driver, register a **ChipDevice** model instance. - Create a touchscreen driver. Create the **touch\_ic\_name.c** file in the directory. Replace **ic\_name** with the name of your chip. The file template is as follows: ``` #include "hdf_touch.h" static int32_t HdfXXXXChipInit(struct HdfDeviceObject *device) { ChipDevice *tpImpl = CreateXXXXTpImpl(); if(RegisterChipDevice(tpImpl) != HDF_SUCCESS) { ReleaseXXXXTpImpl(tpImpl); return HDF_FAILURE; } return HDF_SUCCESS; } struct HdfDriverEntry g_touchXXXXChipEntry = { .moduleVersion = 1, .moduleName = "HDF_TOUCH_XXXX", .Init = HdfXXXXChipInit, }; HDF_INIT(g_touchXXXXChipEntry); ``` Implement the following interfaces in **ChipDevice**:

Interface

Description

int32_t (*Init)(ChipDevice *device)

Initializes a touchscreen.

int32_t (*Detect)(ChipDevice *device)

Detects a touchscreen.

int32_t (*Suspend)(ChipDevice *device)

Suspends a touchscreen.

int32_t (*Resume)(ChipDevice *device)

Resumes a touchscreen.

int32_t (*DataHandle)(ChipDevice *device)

Reads data from a touchscreen and writes the touch point data to device > driver > frameData.

int32_t (*UpdateFirmware)(ChipDevice *device)

Upgrades the firmware.

- Configure the product and load the driver. All device information about the product is defined in the **//vendor/MyProductVendor/MyProduct/config/device\_info/device\_info.hcs** file. Modify the file by adding configurations for the device named **device\_touch\_chip** to the host named **input**. Note: The value of **moduleName** must be the same as that in the touchscreen driver. ``` deviceN :: deviceNode { policy = 0; priority = 130; preload = 0; permission = 0660; moduleName = "HDF_TOUCH_XXXX"; deviceMatchAttr = "touch_XXXX_configs"; } ``` For details about driver development, see [TOUCHSCREEN](../driver/driver-peripherals-touch-des.md). ### 3. WLAN The WLAN driver is divided into two parts. One of the parts manages WLAN devices, and the other part manages WLAN traffic. HDF WLAN provides abstraction for the two parts. Currently, only the WLAN with the SDIO interface is supported. **Figure 1** WLAN chip ![](figure/wlan-chip.png "wlan-chip") To support a chip, implement a **ChipDriver** for it. The major task is to implement the following interfaces provided by **HDF\_WLAN\_CORE** and **NetDevice**.

Interface

Header File

Description

HdfChipDriverFactory

//drivers/framework/include/wifi/hdf_wlan_chipdriver_manager.h

Factory of the ChipDriver, which is used to support multiple WLAN interfaces of a chip.

HdfChipDriver

//drivers/framework/include/wifi/wifi_module.h

Manages a specific WLAN interface. Each WLAN interface corresponds to an HdfChipDriver.

NetDeviceInterFace

//drivers/framework/include/net/net_device.h

Communicates with the protocol stack, such as sending data and setting the status of network interfaces.

To port a WLAN driver, perform the following steps: 1. Create an HDF driver. You are advised to place the code file in the **//device/MySoCVendor/peripheral/wifi/chip\_name/** directory. The file template is as follows: ``` static int32_t HdfWlanHisiChipDriverInit(struct HdfDeviceObject *device) { static struct HdfChipDriverFactory factory = CreateChipDriverFactory(); struct HdfChipDriverManager *driverMgr = HdfWlanGetChipDriverMgr(); if (driverMgr->RegChipDriver(&factory) != HDF_SUCCESS) { HDF_LOGE("%s fail: driverMgr is NULL!", __func__); return HDF_FAILURE; } return HDF_SUCCESS; } struct HdfDriverEntry g_hdfXXXChipEntry = { .moduleVersion = 1, .Init = HdfWlanXXXChipDriverInit, .Release = HdfWlanXXXChipRelease, .moduleName = "HDF_WIFI_CHIP_XXX" }; HDF_INIT(g_hdfXXXChipEntry); ``` Create an **HdfChipDriverFactory** in the **CreateChipDriverFactory**. The interfaces are as follows:

Interface

Description

const char *driverName

Indicates the driver name.

int32_t (*InitChip)(struct HdfWlanDevice *device)

Initializes a chip.

int32_t (*DeinitChip)(struct HdfWlanDevice *device)

Deinitializes a chip.

void (_ReleaseFactory)(struct HdfChipDriverFactory _factory)

Releases the HdfChipDriverFactory object.

struct HdfChipDriver _(_Build)(struct HdfWlanDevice *device, uint8_t ifIndex)

Creates an HdfChipDriver. In the input parameters, device indicates the device information, and ifIndex indicates the sequence number of this interface in the chip.

void (_Release)(struct HdfChipDriver _chipDriver)

Releases the HdfChipDriver.

uint8_t (*GetMaxIFCount)(struct HdfChipDriverFactory *factory)

Obtains the maximum number of interfaces supported by the current chip.

Implement the following interfaces in the **HdfChipDriver**.

Interface

Description

int32_t (*init)(struct HdfChipDriver *chipDriver, NetDevice *netDev)

Initializes the current network interface. The NetDeviceInterFace needs to be provided for the netDev.

int32_t (*deinit)(struct HdfChipDriver *chipDriver, NetDevice *netDev)

Deinitializes the current network interface.

struct HdfMac80211BaseOps *ops

Provides the WLAN basic capability interface set.

struct HdfMac80211STAOps *staOps

Provides the interface set required for supporting the standalone (STA) mode.

struct HdfMac80211APOps *apOps

Provides the interface set required for supporting the access point (AP) mode.

2. Compile the configuration file to describe the devices supported by the driver. Create the chip configuration file **//vendor/MyProductVendor/MyProduct/config/wifi/wlan\_chip\_chip\_name.hcs** in the product configuration directory. Replace **MyProductVendor**, **MyProduct**, and **chip\_name** in the path with the actual names. The sample code is as follows: ``` root { wlan_config { chip_name :& chipList { chip_name :: chipInst { match_attr = "hdf_wlan_chips_chip_name"; /* Configure the matching attribute, which is used to provide the configuration root of the driver.*/ driverName = "driverName"; /* The value must be the same as that of driverName in HdfChipDriverFactory.*/ sdio { vendorId = 0x0296; deviceId = [0x5347]; } } } } } ``` 3. Edit the configuration file and load the driver. All device information about the product is defined in the **//vendor/MyProductVendor/MyProduct/config/device\_info/device\_info.hcs** file. Modify the file by adding configurations for the device named **device\_wlan\_chips** to the host named **network**. Note: The value of **moduleName** must be the same as that in the touchscreen driver. ``` deviceN :: deviceNode { policy = 0; preload = 2; moduleName = "HDF_WLAN_CHIPS"; deviceMatchAttr = "hdf_wlan_chips_chip_name"; serviceName = "driverName"; } ``` 4. Build the driver. - Create a kernel configuration menu. Create a **Kconfig** file in the **//device/MySoCVendor/peripheral** directory. The file template is as follows: ``` config DRIVERS_WLAN_XXX bool "Enable XXX WLAN Host driver" default n depends on DRIVERS_HDF_WIFI help Answer Y to enable XXX Host driver. Support chip xxx ``` Add the following sample code to the end of the **//drivers/adapter/khdf/linux/model/network/wifi/Kconfig** file to add the configuration menu to the kernel: ``` source "../../../../../device/MySoCVendor/peripheral/Kconfig" ``` - Create a build script. Add the following configuration to the end of the **//drivers/adapter/khdf/linux/model/network/wifi/Makefile** file: ``` HDF_DEVICE_ROOT := $(HDF_DIR_PREFIX)/../device obj-$(CONFIG_DRIVERS_WLAN_XXX) += $(HDF_DEVICE_ROOT)/MySoCVendor/peripheral/build/standard/ ``` When **DRIVERS\_WLAN\_XXX** is enabled in the kernel, **makefile** in **//device/MySoCVendor/peripheral/build/standard/** is called. For more details, see [WLAN Development](../guide/device-wlan-led-outcontrol.md). ### 4. Samples For details about the porting sample, see the DAYU development board adaptation guide.