1# Standard System Porting Guide
2
3
4This 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.
5
6
7## Defining a Development Board
8
9This 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**.
10
11
12### Defining a Product
13
14Create 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:
15
16//vendor/MyProductVendor/MyProduct/config.json
17
18
19```
20{
21    "product_name": "MyProduct",
22    "version": "3.0",
23    "type": "standard",
24    "target_cpu": "arm",
25    "ohos_version": "OpenHarmony 1.0",
26    "device_company": "MyProductVendor",
27    "board": "MySOC",
28    "enable_ramdisk": true,
29    "subsystems": [
30      {
31        "subsystem": "ace",
32        "components": [
33          { "component": "ace_engine_lite", "features":[] }
34        ]
35      },
36	...
37    ]
38}
39
40
41```
42The main configurations are as follows:
43
44| Item| Description|
45|-------|----------|
46|product_name |(Mandatory) Product name.|
47|version|(Mandatory) Version.|
48|type|(Mandatory) System level, such as **small** and **standard**.|
49|target_cpu |(Mandatory) CPU type of the device, such as **arm64**, **riscv**, or **x86**.|
50|ohos_version|(Optional) Operating system version.|
51|device_company|(Mandatory) Device manufacturer name.|
52|board|(Mandatory) Development board name.|
53|enable_ramdisk|(Mandatory) Whether to start the RAM disk.|
54|kernel_type|(Optional) Kernel type.|
55|kernel_version|(Optional) Kernel version. The values of both **kernel_type** and **kernel_version** are fixed in the standard system.|
56|subsystems|(Mandatory) Subsystem to enable. A subsystem can be treated as an independently built functional block.|
57|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**.|
58
59
60You can find predefined subsystems in **//build/subsystem_config.json**. You can also customize subsystems.
61
62You 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.
63
64
65### Verifying the Porting
66
67  Run the following command to start the build of your product:
68
69```
70./build.sh --product-name MyProduct
71```
72
73After the build is complete, you can view the built image file in **//out/{*device_name*}/packages/phone/images**.
74
75
76## Porting the Kernel
77
78Now, you need to port the Linux kernel to enable it to run successfully.
79
80
81### Adding a Kernel-built Subsystem to the SoC
82
83Modify the **//build/subsystem_config.json** file to add a subsystem. For example, if **product_name** is **MyProduct**, configure the **//vendor/MyProductVendor/MyProduct/config.json** file as follows:
84
85
86```
87  "MySOCVendor_products": {
88    "project": "hmf/MySOCVendor_products",
89    "path": "device/MySOCVendor/MySOC/build",
90    "name": "MySOCVendor_products",
91    "dir": "device/MySOCVendor"
92  },
93```
94
95Then, open the configuration file **//vendor/MyProductVendor/MyProduct/config.json** and add the new subsystem to the product.
96
97
98### Building the Kernel
99
100The 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.
101
102The 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.
103
104The recommended directory structure is as follows:
105
106
107```
108├── build
109│ ├── kernel
110│ │     ├── linux
111│ │           ├──standard_patch_for_4_19.patch // Patch for the Linux kernel 4.19
112│ ├── BUILD.gn
113│ ├── ohos.build
114```
115
116The **BUILD.gn** file is the only entry for building the subsystem.
117
118The expected build result described in the table below.
119
120| File| Description|
121| -------- | -------- |
122| $root_build_dir/packages/phone/images/uImage | Kernel image.|
123| $root_build_dir/packages/phone/images/uboot | Bootloader image.|
124
125
126### Verifying the Porting
127
128Now start build, and check whether the kernel image is generated as expected.
129
130## User-mode Boot
131
1321. Overview of user-mode boot process
133
134   ![user-mode boot](figures/user-mode boot.png)
135
136
137   When the system is powered on, the kernel loads and starts services and applications as follows:
138
139   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**.
140   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.
141   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.
142   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).
143   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.
144   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.
145   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.
146
1472. init module
148
149   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.
150
151   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.
152
153   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.
154
155   For details about how to develop the initialization configuration file, see [Startup](../subsystems/subsys-boot-overview.md).
156
157
158## Porting the HDF Driver
159
160
161### LCD
162
163This 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.
164
165The LCD drivers are stored in the **//drivers/hdf_core/framework/model/display/driver/panel** directory.
166
1671. Create a panel driver.
168
169   In the **Init** method of the driver, call **RegisterPanel** to register the model instance. For example:
170
171
172   ```
173   int32_t XXXInit(struct HdfDeviceObject *object)
174   {
175       struct PanelData *panel = CreateYourPanel();
176
177       // Registration
178       if (RegisterPanel(panel) != HDF_SUCCESS) {
179           HDF_LOGE("%s: RegisterPanel failed", __func__);
180           return HDF_FAILURE;
181       }
182       return HDF_SUCCESS;
183   }
184
185   struct HdfDriverEntry g_xxxxDevEntry = {
186       .moduleVersion = 1,
187       .moduleName = "LCD_XXXX",
188       .Init = XXXInit,
189   };
190
191   HDF_INIT(g_xxxxDevEntry);
192   ```
193
1942. 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**.
195
196   Note: The value of **moduleName** must be the same as that in the panel driver.
197
198   ```
199   root {
200       ...
201       display :: host {
202           device_lcd :: device {
203               deviceN :: deviceNode {
204                   policy = 0;
205                   priority = 100;
206                   preload = 2;
207                   moduleName = "LCD_XXXX";
208               }
209           }
210       }
211   }
212   ```
213
214   For details about driver development, see [LCD](../driver/driver-peripherals-lcd-des.md).
215
216
217### Touchscreen
218
219This section describes how to port a touchscreen driver. The touchscreen driver is stored in the **//drivers/hdf_core/framework/model/input/driver/touchscreen** directory. To port a touchscreen driver, register a **ChipDevice** model instance.
220
2211. Create a touchscreen driver.
222
223   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:
224
225
226   ```
227   #include "hdf_touch.h"
228
229   static int32_t HdfXXXXChipInit(struct HdfDeviceObject *device)
230   {
231       ChipDevice *tpImpl = CreateXXXXTpImpl();
232       if(RegisterChipDevice(tpImpl) != HDF_SUCCESS) {
233           ReleaseXXXXTpImpl(tpImpl);
234           return HDF_FAILURE;
235       }
236       return HDF_SUCCESS;
237   }
238
239   struct HdfDriverEntry g_touchXXXXChipEntry = {
240       .moduleVersion = 1,
241       .moduleName = "HDF_TOUCH_XXXX",
242       .Init = HdfXXXXChipInit,
243   };
244
245   HDF_INIT(g_touchXXXXChipEntry);
246   ```
247
248   Implement the following interfaces in **ChipDevice**:
249
250   | API| Description|
251   | -------- | -------- |
252   | int32_t (\*Init)(ChipDevice \*device) | Initializes a touchscreen.|
253   | int32_t (\*Detect)(ChipDevice \*device) | Detects a touchscreen.|
254   | int32_t (\*Suspend)(ChipDevice \*device) | Suspends a touchscreen.|
255   | int32_t (\*Resume)(ChipDevice \*device) | Resumes a touchscreen.|
256   | int32_t (\*DataHandle)(ChipDevice \*device) | Reads data from a touchscreen and writes the touch point data to **device** > **driver** > **frameData**.|
257   | int32_t (\*UpdateFirmware)(ChipDevice \*device) | Upgrades the firmware.|
258
2592. Configure the product and load the driver.
260
261   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.
262
263
264   ```
265   deviceN :: deviceNode {
266       policy = 0;
267       priority = 130;
268       preload = 0;
269       permission = 0660;
270       moduleName = "HDF_TOUCH_XXXX";
271       deviceMatchAttr = "touch_XXXX_configs";
272   }
273   ```
274
275   For details about driver development, see [Touchscreen](../driver/driver-peripherals-touch-des.md).
276
277
278### WLAN
279
280The 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.
281
282  **Figure 1** WLAN chip
283
284  ![hdf_wifi](figures/hdf_wifi.png)
285
286To support a chip, implement a **ChipDriver** for it. The major task is to implement the following interfaces provided by **HDF_WLAN_CORE** and **NetDevice**.
287
288| API| Header File| Description|
289| -------- | -------- | -------- |
290| HdfChipDriverFactory | //drivers/hdf_core/framework/include/wifi/hdf_wlan_chipdriver_manager.h | Supports multiple WLAN interfaces of a chip.|
291| HdfChipDriver | //drivers/hdf_core/framework/include/wifi/wifi_module.h | Manages a specific WLAN interface. Each WLAN interface corresponds to an **HdfChipDriver**.|
292| NetDeviceInterFace | //drivers/hdf_core/framework/include/net/net_device.h | Communicates with the protocol stack, such as sending data and setting the status of network interfaces.|
293
294To port a WLAN driver, perform the following steps:
295
2961. 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:
297
298
299   ```
300   static int32_t HdfWlanXXXChipDriverInit(struct HdfDeviceObject *device) {
301       static struct HdfChipDriverFactory factory = CreateChipDriverFactory();
302       struct HdfChipDriverManager *driverMgr = HdfWlanGetChipDriverMgr();
303       if (driverMgr->RegChipDriver(&factory) != HDF_SUCCESS) {
304           HDF_LOGE("%s fail: driverMgr is NULL!", __func__);
305           return HDF_FAILURE;
306       }
307       return HDF_SUCCESS;
308   }
309
310   struct HdfDriverEntry g_hdfXXXChipEntry = {
311       .moduleVersion = 1,
312       .Init = HdfWlanXXXChipDriverInit,
313       .Release = HdfWlanXXXChipRelease,
314       .moduleName = "HDF_WIFI_CHIP_XXX"
315   };
316
317   HDF_INIT(g_hdfXXXChipEntry);
318   ```
319
320   Create an **HdfChipDriverFactory** in the **CreateChipDriverFactory**. The APIs are as follows:
321
322
323
324   | API| Description|
325   | -------- | -------- |
326   | const char \*driverName | Indicates the driver name.|
327   | int32_t (\*InitChip)(struct HdfWlanDevice \*device) | Initializes a chip.|
328   | int32_t (\*DeinitChip)(struct HdfWlanDevice \*device) | Deinitializes a chip.|
329   | void (_ReleaseFactory)(struct HdfChipDriverFactory _factory) | Releases the **HdfChipDriverFactory** object.|
330   | 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.|
331   | void (_Release)(struct HdfChipDriver _chipDriver) | Releases the **HdfChipDriver**.|
332   | uint8_t (\*GetMaxIFCount)(struct HdfChipDriverFactory \*factory) | Obtains the maximum number of APIs supported by the current chip.|
333
334   Implement the following APIs in the **HdfChipDriver**.
335
336   | API| Description|
337   | -------- | -------- |
338   | int32_t (\*init)(struct HdfChipDriver \*chipDriver, NetDevice \*netDev) | Initializes the current network interface. The **NetDeviceInterFace** needs to be provided for the **netDev**.|
339   | int32_t (\*deinit)(struct HdfChipDriver \*chipDriver, NetDevice \*netDev) | Deinitializes the current network interface.|
340   | struct HdfMac80211BaseOps \*ops | Provides the WLAN basic capability interface set.|
341   | struct HdfMac80211STAOps \*staOps | Provides the interface set required for supporting the standalone (STA) mode.|
342   | struct HdfMac80211APOps \*apOps | Provides the interface set required for supporting the access point (AP) mode.|
343
3442. Compile the configuration file to describe the devices supported by the driver.
345
346   Create the chip configuration file **//vendor/MyProductVendor/MyProduct/config/wifi/wlan_chip_chip_name.hcs** in the product configuration directory.
347
348   Replace **MyProductVendor**, **MyProduct**, and **chip_name** in the path with the actual names.
349
350   The sample code is as follows:
351
352   ```
353   root {
354       wlan_config {
355           chip_name :& chipList {
356               chip_name :: chipInst {
357                   match_attr = "hdf_wlan_chips_chip_name"; /* Configure the matching attribute, which is used to provide the configuration root of the driver.*/
358                   driverName = "driverName"; /* The value must be the same as that of driverName in HdfChipDriverFactory.*/
359                   sdio {
360                       vendorId = 0x0296;
361                       deviceId = [0x5347];
362                   }
363               }
364           }
365       }
366   }
367   ```
368
3693. Edit the configuration file and load the driver.
370
371   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**.
372
373   Note: The value of **moduleName** must be the same as that in the touchscreen driver.
374
375   ```
376   deviceN :: deviceNode {
377       policy = 0;
378       preload = 2;
379       moduleName = "HDF_WLAN_CHIPS";
380       deviceMatchAttr = "hdf_wlan_chips_chip_name";
381       serviceName = "driverName";
382   }
383   ```
384
3854. Build the driver.
386
387   - Create a kernel configuration menu. Create a **Kconfig** file in the **//device/MySoCVendor/peripheral** directory. The file template is as follows:
388
389     ```
390     config DRIVERS_WLAN_XXX
391         bool "Enable XXX WLAN Host driver"
392         default n
393         depends on DRIVERS_HDF_WIFI
394         help
395           Answer Y to enable XXX Host driver. Support chip xxx
396     ```
397
398     Add the following configuration to the end of the **//drivers/hdf_core/adapter/khdf/linux/model/network/wifi/Kconfig** file:
399
400
401     ```
402     source "../../../../../device/MySoCVendor/peripheral/Kconfig"
403     ```
404
405   - Create a build script.
406
407     Add the following configuration to the end of the **//drivers/hdf_core/adapter/khdf/linux/model/network/wifi/Makefile** file:
408
409
410     ```
411     HDF_DEVICE_ROOT := $(HDF_DIR_PREFIX)/../device
412     obj-$(CONFIG_DRIVERS_WLAN_XXX) += $(HDF_DEVICE_ROOT)/MySoCVendor/peripheral/build/standard/
413     ```
414
415     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).
416
417