standard-system-porting-guide.md 17.3 KB
Newer Older
1
# Standard System Porting Guide
D
duangavin123 已提交
2 3


4 5 6 7
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
D
duangavin123 已提交
8

E
ester.zhou 已提交
9
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**.
D
duangavin123 已提交
10 11


12 13 14
### Defining a Product

Create a file named **config.json** in the **//vendor/MyProductVendor/*{product_name}*** directory. This file is used to describe the SoC used by the product and the required subsystems. For example, if **product_name** is **MyProduct**, configure the **//vendor/MyProductVendor/MyProduct/config.json** file as follows:
E
ester.zhou 已提交
15

16
//vendor/MyProductVendor/MyProduct/config.json
D
duangavin123 已提交
17

D
duangavin123 已提交
18 19

```
D
duangavin123 已提交
20
{
21 22 23 24 25 26
    "product_name": "MyProduct",
    "version": "3.0",
    "type": "standard",
    "target_cpu": "arm",
    "ohos_version": "OpenHarmony 1.0",
    "device_company": "MyProductVendor",
27
    "board": "MySOC",
28 29 30 31 32 33 34
    "enable_ramdisk": true,
    "subsystems": [
      {
        "subsystem": "ace",
        "components": [
          { "component": "ace_engine_lite", "features":[""] }
        ]
35 36
      },

37
    ]
D
duangavin123 已提交
38
}
39 40


D
duangavin123 已提交
41
```
42
The main configurations are as follows:
D
duangavin123 已提交
43

E
ester.zhou 已提交
44
**product_name**: product name. This parameter is required.
D
duangavin123 已提交
45

E
ester.zhou 已提交
46
**version**: version. This parameter is required.
D
duangavin123 已提交
47

48
**type**: system level, such as **small** or **standard**. This parameter is required.
D
duangavin123 已提交
49

50
**target_cpu**: CPU type of the device, such as **arm64**, **riscv**, or **x86**. This parameter is required.
D
duangavin123 已提交
51

E
ester.zhou 已提交
52
**ohos_version**: operating system version. This parameter is optional.
D
duangavin123 已提交
53

E
ester.zhou 已提交
54
**device_company**: device manufacturer name. This parameter is required.
55

E
ester.zhou 已提交
56
**board**: board name. This parameter is required.
57

E
ester.zhou 已提交
58
**enable_ramdisk**: whether to enable the RAM disk. This parameter is required.
59

60 61 62
**kernel_type**: kernel type. This parameter is optional.

**kernel_version**: kernel version. This parameter is optional. Both **kernel_type** and **kernel_version** are fixed.
63

E
ester.zhou 已提交
64
**subsystems**: subsystem to enable. A subsystem can be treated as an independently built functional block. This parameter is required.
D
duangavin123 已提交
65

E
ester.zhou 已提交
66
**product_company**: device manufacturer name. It is not set in the configuration, but in the directory name, next to the vendor name. It can be accessed from **build.gn script**.
D
duangavin123 已提交
67 68


69 70 71
You can find predefined 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.
D
duangavin123 已提交
72 73


74 75 76
### Verifying the Porting

  Run the following command to start the build of your product:
D
duangavin123 已提交
77

D
duangavin123 已提交
78 79 80
```
./build.sh --product-name MyProduct 
```
D
duangavin123 已提交
81

82 83
After the build is complete, you can view the built image file in **//out/{*device_name*}/packages/phone/images**.

D
duangavin123 已提交
84

85
## Porting the Kernel
D
duangavin123 已提交
86 87 88 89

Now, you need to port the Linux kernel to enable it to run successfully.


E
ester.zhou 已提交
90
### Adding a Kernel-built Subsystem to the SoC
91 92 93

Add the following subsystem configuration to the **//build/subsystem_config.json** file:

D
duangavin123 已提交
94

D
duangavin123 已提交
95
```
D
duangavin123 已提交
96 97 98 99 100 101 102 103
  "MySOCVendor_products": {
    "project": "hmf/MySOCVendor_products",
    "path": "device/MySOCVendor/MySOC/build",
    "name": "MySOCVendor_products",
    "dir": "device/MySOCVendor"
  },
```

104 105
Then, open the configuration file **//vendor/MyProductVendor/MyProduct/config.json** and add the new subsystem to the product.

D
duangavin123 已提交
106

E
ester.zhou 已提交
107
### Building the Kernel
D
duangavin123 已提交
108

E
ester.zhou 已提交
109
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.
D
duangavin123 已提交
110

111
The path for building the subsystem is defined when you define the subsystem in the previous step. In this example, 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.
D
duangavin123 已提交
112 113

The recommended directory structure is as follows:
D
duangavin123 已提交
114

115

D
duangavin123 已提交
116 117
```
├── build
D
duangavin123 已提交
118 119 120 121 122
│ ├── kernel
│ │     ├── linux
│ │           ├──standard_patch_for_4_19.patch // Patch for the Linux kernel 4.19
│ ├── BUILD.gn
│ ├── ohos.build
D
duangavin123 已提交
123 124
```

E
ester.zhou 已提交
125
The **BUILD.gn** file is the only entry for building the subsystem.
D
duangavin123 已提交
126

127 128 129 130 131 132 133 134
The expected build result described in the table below.

| File| Description|
| -------- | -------- |
| $root_build_dir/packages/phone/images/uImage | Kernel image.|
| $root_build_dir/packages/phone/images/uboot | Bootloader image.|


E
ester.zhou 已提交
135
### Verifying the Porting
D
duangavin123 已提交
136 137 138

Now start build, and check whether the kernel image is generated as expected.

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
## User-mode Boot

1. Overview of user-mode boot process

![user-mode boot](figures/user-mode boot.png)

   When the system is powered on, the kernel loads and starts services and applications as follows:

1. The kernel loads the init process, which is specified by **cmdline** of the kernel when the bootloader starts the kernel, for example, **init=/init root/dev/xxx**.
2. After the init process is started, **tmpfs** and **procfs** are mounted and basic **dev** nodes are created to establish a basic root file system.
3. The init process starts the ueventd process to listen for device hot-swap events in the kernel and creates **dev** nodes for related devices as well as partitions for the block device.
4. After mounting partitions (**system** and **vendor**) of the block device, the init process scans for the init startup script of each system service and launches the respective service ability (SA).
5. Each SA registers with the samgr process, which serves as the service registration center. The samgr process assigns each SA with an ID, which will be used by an application to access the desired SA.
6. The foundation process implements application lifecycle management. It is a special SA service process that provides the user program management framework and basic services.
7. The appspawn process directly spawns the application process, eliminating the need for the application to load the JS runtime environment. This reduces the application startup time. 

2. init module

   The configuration file of the init module contains service names, executable file paths, permissions, and other information of all key system services that need to be started by the init process. The boot script of each system service is installed in the **/system/etc/init** directory.

   When porting a new chip platform, you need to add the **/vendor/etc/init/init.{hardware}.cfg** file that contains the platform-level initialization configuration. This file is used to implement platform-level initialization, for example, installing the ko driver and configuring information on the related **/proc** nodes.

   The code of the init process is stored in the **//base/startup/init_lite** directory. This process is the first process in the system and does not depend on other processes.

   For details about how to develop the initialization configuration file, see [Startup](../subsystems/subsys-boot-overview.md).
D
duangavin123 已提交
164 165


166 167 168
## Porting the HDF Driver


E
ester.zhou 已提交
169
### LCD
170 171

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.
D
duangavin123 已提交
172

E
ester.zhou 已提交
173
The LCD drivers are stored in the **//drivers/framework/model/display/driver/panel** directory.
D
duangavin123 已提交
174

175 176 177
- Create a panel driver.

  In the **Init** method of the driver, call **RegisterPanel** to register the model instance.
D
duangavin123 已提交
178 179


D
duangavin123 已提交
180
```
D
duangavin123 已提交
181 182 183 184
int32_t XXXInit(struct HdfDeviceObject *object)
{
    struct PanelData *panel = CreateYourPanel();

185
    // Registration
D
duangavin123 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    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);
```

202
- 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.
D
duangavin123 已提交
203

204
  
D
duangavin123 已提交
205
```
D
duangavin123 已提交
206 207 208 209
root {
    ...
    display :: host {
        device_lcd :: device {
210 211 212 213 214 215
            deviceN :: deviceNode {
                policy = 0;
                priority = 100;
                preload = 2;
                moduleName = "LCD_XXXX";
            }
D
duangavin123 已提交
216 217 218 219 220
        }
    }
}
```

E
ester.zhou 已提交
221
For details about driver development, see [LCD](../driver/driver-peripherals-lcd-des.md).
D
duangavin123 已提交
222

223

E
ester.zhou 已提交
224
### Touchscreen
D
duangavin123 已提交
225

E
ester.zhou 已提交
226
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.
D
duangavin123 已提交
227

228 229 230
- 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:
D
duangavin123 已提交
231

D
duangavin123 已提交
232 233

```
D
duangavin123 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
#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);
```

255
Implement the following APIs in **ChipDevice**:
D
duangavin123 已提交
256

257 258 259 260 261 262 263 264 265 266 267 268
| API| 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.
D
duangavin123 已提交
269

D
duangavin123 已提交
270
```
D
duangavin123 已提交
271 272 273 274 275 276 277 278 279 280
                deviceN :: deviceNode {
                    policy = 0;
                    priority = 130;
                    preload = 0;
                    permission = 0660;
                    moduleName = "HDF_TOUCH_XXXX";
                    deviceMatchAttr = "touch_XXXX_configs";
                }
```

281 282
For details about driver development, see [Touchscreen](../driver/driver-peripherals-touch-des.md).

D
duangavin123 已提交
283

E
ester.zhou 已提交
284
### WLAN
D
duangavin123 已提交
285 286 287

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.

288 289 290 291 292 293 294 295 296 297 298
**Figure 1** WLAN chip

![hdf_wifi](figures/hdf_wifi.png)

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**.

| API| Header File| Description|
| -------- | -------- | -------- |
| HdfChipDriverFactory | //drivers/framework/include/wifi/hdf_wlan_chipdriver_manager.h | Supports 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.|
D
duangavin123 已提交
299 300 301

To port a WLAN driver, perform the following steps:

302 303
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:

D
duangavin123 已提交
304

D
duangavin123 已提交
305
```
D
duangavin123 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
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);
```

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
Create an **HdfChipDriverFactory** in the **CreateChipDriverFactory**. The APIs are as follows:

| API| 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 APIs supported by the current chip.|

Implement the following APIs in the **HdfChipDriver**.

| API| 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.|
D
duangavin123 已提交
347

D
duangavin123 已提交
348
2. Compile the configuration file to describe the devices supported by the driver.
D
duangavin123 已提交
349

350
   Create the chip configuration file **//vendor/MyProductVendor/MyProduct/config/wifi/wlan_chip_chip_name.hcs** in the product configuration directory.
D
duangavin123 已提交
351

352 353 354
   Replace **MyProductVendor**, **MyProduct**, and **chip_name** in the path with the actual names.

   The sample code is as follows:
D
duangavin123 已提交
355

D
duangavin123 已提交
356 357

```
D
duangavin123 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
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.

376 377
   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.

D
duangavin123 已提交
378

D
duangavin123 已提交
379
```
D
duangavin123 已提交
380 381 382 383 384 385 386 387 388 389 390
                deviceN :: deviceNode {
                    policy = 0;
                    preload = 2;
                    moduleName = "HDF_WLAN_CHIPS";
                    deviceMatchAttr = "hdf_wlan_chips_chip_name";
                    serviceName = "driverName";
                }
```

4. Build the driver.

391
- Create a kernel configuration menu. Create a **Kconfig** file in the **//device/MySoCVendor/peripheral** directory. The file template is as follows:
D
duangavin123 已提交
392

D
duangavin123 已提交
393 394 395 396 397 398 399 400 401
```
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
```

E
ester.zhou 已提交
402
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:
D
duangavin123 已提交
403

404

D
duangavin123 已提交
405 406 407 408
```
source "../../../../../device/MySoCVendor/peripheral/Kconfig"
```

409 410 411
- Create a build script.
  
Add the following configuration to the end of the **//drivers/adapter/khdf/linux/model/network/wifi/Makefile** file:
D
duangavin123 已提交
412 413 414 415 416
```
HDF_DEVICE_ROOT := $(HDF_DIR_PREFIX)/../device
obj-$(CONFIG_DRIVERS_WLAN_XXX) += $(HDF_DEVICE_ROOT)/MySoCVendor/peripheral/build/standard/
```

417
When **DRIVERS_WLAN_XXX** is enabled in the kernel, **makefile** in **//device/MySoCVendor/peripheral/build/standard/** is called. For details about the development, see [LED Peripheral Control](../guide/device-wlan-led-control.md).