1e41f4b71Sopenharmony_ci# Device Boot 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Context Structure 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciThe following figure shows the context structure of the Startup subsystem. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci **Figure 1** Context structure of the Startup subsystem 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci  10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ciWhen the system is powered on, the kernel loads and starts services and applications as follows: 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci1. The kernel loads the init process, which is specified by cmdline of the kernel when the bootloader starts the kernel. 14e41f4b71Sopenharmony_ci2. After the init process is started, tmpfs and procfs are mounted and basic dev nodes are created to establish a basic root file system. 15e41f4b71Sopenharmony_ci3. 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. 16e41f4b71Sopenharmony_ci4. After mounting partitions (system and vendor) of the block device, the init process scans for the init startup script of each system service and starts the respective system ability (SA). 17e41f4b71Sopenharmony_ci5. 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. 18e41f4b71Sopenharmony_ci6. The foundation process implements application lifecycle management. It is a special SA service process that provides the user program management framework and basic services. This process manages the lifecycle of applications. 19e41f4b71Sopenharmony_ci7. For an application, loading of the JS running environment involves a great deal of preparations. To reduce the application startup time, the appspawn process directly spawns an application process once receiving an application startup request from the foundation process. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ciThe Startup subsystem consists of the following modules: 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci- init Module 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci This module corresponds to the init process, which is the first user-space process started after the kernel is initialized. After the init process starts, it reads and parses the init.cfg file. Based on the parsing result, the init module executes the commands listed in Table 2 in [Job Management](../subsystems/subsys-boot-init-jobs.md#available-apis) and starts the key system service processes in sequence with corresponding permissions granted. 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci- ueventd module 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci This module listens for **netlink** events about hot plug of kernel device drivers and dynamically manages the dev node of the corresponding device based on the event type. 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci- appspawn Module 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci This module spawns application processes upon receiving commands from the foundation, configures permissions for new processes, and calls the entry function of the application framework. 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci- bootstrap Module 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci This module provides entry identifiers for starting services and features. When samgr is started, the entry function identified by bootstrap is called and system services are started. 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci## Constraints 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci The source code directory of the Startup subsystem varies according to the platform. 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci **Table 1** Directories and applicable platforms of the Startup subsystem 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci| Name| Applicable Platform| 47e41f4b71Sopenharmony_ci| -------- | -------- | 48e41f4b71Sopenharmony_ci| base/startup/appspawne | Small system devices (reference memory ≥ 1 MB) and standard systems, such as Hi3516D V300 , Hi3518E V300, and RK3568| 49e41f4b71Sopenharmony_ci| base/startup/bootstrap_lite | Mini-system devices (reference memory ≥ 128 KiB), for example, Hi3861 V100| 50e41f4b71Sopenharmony_ci| base/startup/init | Small system devices (reference memory ≥ 1 MB) and standard systems, such as Hi3516D V300 , Hi3518E V300, and RK3568| 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci- init module 53e41f4b71Sopenharmony_ci - To start a system service, you first need to write a boot script file named `init.cfg`, in which you define the service name, path of executable files, permissions, etc. 54e41f4b71Sopenharmony_ci - The boot script of each system service is installed in the `/system/etc/init` directory. The init process scans this directory for the boot script to execute. 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci- 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. 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci > **NOTE** 59e41f4b71Sopenharmony_ci > 60e41f4b71Sopenharmony_ci > The `init.cfg` file must be in JSON format. 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci- bootstrap module: The zInit code must be configured in the link script. 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci## Boot Process for the OpenHarmony Standard System 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ciBy default, the OpenHarmony standard system supports the images listed in the following table. 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci| Image | Mount Point | Description | 69e41f4b71Sopenharmony_ci| ------------ | ------- | ---------------------------------------------------- | 70e41f4b71Sopenharmony_ci| boot.img | NA | Kernel and ramdisk image, which is the first image loaded by the bootloader. | 71e41f4b71Sopenharmony_ci| system.img | /system | System component image, which stores chip-irrelevant platform services. | 72e41f4b71Sopenharmony_ci| vendor.img | /vendor | Chip component image, which stores chip-related hardware abstraction services. | 73e41f4b71Sopenharmony_ci| updater.img | / | Updater image, which is used for system updating. This image is not loaded during normal startup.| 74e41f4b71Sopenharmony_ci| userdata.img | /data | Writable user data image. | 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ciOn each development board, you need to partition the memory to store the preceding images. When the SoC starts, the bootloader loads the images as follows: 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci- Initializes hardware such as the ROM and RAM, and loads the partition table information. 79e41f4b71Sopenharmony_ci- Loads the `boot.img` file based on the partition table and parses and loads the `ramdisk.img` file to the memory. 80e41f4b71Sopenharmony_ci- Prepares the partition table information and ramdisk address and enters the kernel, so that the kernel loads the the ramdisk image and starts the init process. 81e41f4b71Sopenharmony_ci- Waits until the init process prepares the initial file system and mounts `required.fstab` (including `system.img` and `vendor.img`) to the file system. 82e41f4b71Sopenharmony_ci- Scans the boot scripts in the `etc/init` directory in `system.img` and `vendor.img` and runs each boot command. 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci### U-Boot Process 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci[U-Boot](https://elinux.org/U-Boot) is used as an example to describe the key image loading process. When U-Boot starts the system, it passes the boot information to the system by using bootargs. 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci- `boot.img` loading and parsing 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci - `boot.img` format 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci boot.img building and loading varies depending on the platform. The implementation on mainstream OpenHarmony platforms is as follows: 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci - Hi3516DV300 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci On this platform, the `boot.img` file uses the flattened image tree (FIT) format. It is generated by the Mkimage tool by packing the `ramdisk.img` files, which are packed by using zImage-dtb or cpio during kernel building, based on the information in the `its` file. 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci The related files and tool are described as follows: 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci 1. `its` file 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci An image source file that describes the information about the image to be generated. You need to create the file, for example, the `ohos.its` file on the Hi3516 platform. 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci 2. Mkimage packaging tool 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci A tool that parses its files and packs the corresponding images based on the image configuration to generate an itb file, that is, `boot.img`. 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci 3. ramdisk 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci A `ramdisk.img` file packed by using cpio. 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci 4. zImage-dtb 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci An image that contains the compressed kernel image and device description file image. 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci - rk3568 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci On this platform, the boot.img file is named `boot_linux.img`. The packaged files are different from those on the Hi3516D V300 platform. 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci 1. Image 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci Image file generated after kernel building. 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci 2. toybrick.dtb 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci A file that is similar to the device description file image generated through dts building. 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci 3. ramdisk.img 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci A `ramdisk.img` file packed by using cpio. 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci - U-Boot loading 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci The ramdisk boot process is supported. In this scenario, you need to modify the product configuration file in `productdefine` and enable ramdisk generation by setting `enable_ramdisk`. The ramdisk processing method varies according to the platform. Take the Hi3516D V300 platform as an example. You need to change the original U-Boot parameter to `root=/dev/ram0 initrd=0x84000000,0x292e00`. 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci- Kernel start 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci When U-Boot starts the kernel, it can pass key information to the kernel through bootargs. The information varies according to the platform. The main fields are described in the table below. 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci | Name | Example | Description | 142e41f4b71Sopenharmony_ci | ----------- | ------------------------------------------------------------ | ------------------------------------------------------------ | 143e41f4b71Sopenharmony_ci | initrd | 0x84000000,0x292e00 | For details, see the kernel documentation:<br>[ramfs-rootfs-initramfs.rst](https://gitee.com/openharmony/kernel_linux_5.10/blob/master/Documentation/filesystems/ramfs-rootfs-initramfs.rst)<br>[initrd.rst](https://gitee.com/openharmony/kernel_linux_5.10/blob/master/Documentation/admin-guide/initrd.rst) | 144e41f4b71Sopenharmony_ci | init | /init | | 145e41f4b71Sopenharmony_ci | blkdevparts | mmcblk0:1M(boot),15M(kernel),200M(system),200M(vendor),<br>2M(misc),20M(updater),-(userdata) | Partition table information. The kernel creates physical partitions based on the information. | 146e41f4b71Sopenharmony_ci | hardware | Hi3516D V300, rk3568, etc. | (Mandatory information) Hardware platform.| 147e41f4b71Sopenharmony_ci | root | /dev/ram0 (Hi3516DV00), root=PARTUUID=614e0000-0000 rw (rk3568) | Boot device loaded by the kernel.| 148e41f4b71Sopenharmony_ci | rootfstype | ext4 | Type of the root file system.| 149e41f4b71Sopenharmony_ci | default_boot_device | soc/10100000.himci.eMMC | (Recommended information) Default boot device. In the first phase of the boot process, a soft link will be created for the required partition device based on this field.| 150e41f4b71Sopenharmony_ci | ohos.required_mount.xxx | /dev/block/platform/soc/10100000.himci.eMMC/by-name/xxx@/usr@ext4@ro,barrier=1@wait,required | The `fstab` information is first read from cmdline. If this fails, the system will try to read the information from the `fstab.required` file.| 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci- Mounting of `required` partitions 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci A `required` partition is one that is essential for system boot. It must be mounted before level-2 boot. For mandatory images like `system` and `vendor` images, the corresponding block device files must be created before mounting. This is usually done based on the **uevent** messages reported by the kernel. The init process needs to know the main device directory of the storage device. The bootloader process passes the primary device directory of the storage device to the init process through `default_boot_device`. 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci Currently, the init process obtains required partition information in two ways. The init process first reads the required partition information through `bootargs` in `/proc/cmdline`. If the attempt fails, the init process reads the `fstab.required` file in the ramdisk image. 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci - Logic of block device creation 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci - Preparations 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci 1. The init process reads the `fstab.required` file from cmdline. If the attempt fails, the init process reads the `fstab.required` file to obtain `PARTNAME` of the block devices that must be mounted, for example, `system` or `vendor`. 163e41f4b71Sopenharmony_ci 2. Create a socket for receiving **uevent** messages reported by the kernel and read `default_boot_device` from `/proc/cmdline`. 164e41f4b71Sopenharmony_ci 3. Traverse the `/sys/devices` directory with the fstab information and socket handle to get the kernel prepared for reporting **uevent** messages. 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ci - Event triggering 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci 1. Use **ueventd** to trigger the kernel to report a **uevent** message. 169e41f4b71Sopenharmony_ci 2. Check whether partitionName in the **uevent** message matches with device information in the `fstab.required` file. 170e41f4b71Sopenharmony_ci 3. If they match, format the device node path and proceed with device node creation. 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci - Node creation 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci 1. Format the path of the soft link to be created for required block device nodes. A soft link helps facilitate access to device nodes in user mode and improve their readability. 175e41f4b71Sopenharmony_ci 2. Create device nodes based on the primary and secondary device numbers passed in the **uevent** message, and the device node path and soft link path obtained in the previous steps. Meanwhile, create a soft link for them. 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ci Up to now, the creation of block device nodes is complete. 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci - Mapping with default_boot_device 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci The kernel writes bootargs information to `/proc/cmdline`. The information includes `default_boot_device`, which specifies the primary device directory required for system boot. The content prefixed with `ohos.required_mount.` is the partition mounting information required for system boot. It should be the same as that in the `fstab.required` file. In addition, the block device node in the partition mounting information should be a device node pointed by the soft link under by-name in the `default_boot_device` directory. For example, if the value of `default_boot_device` is `soc/10100000.himci.eMMC`, then the value of `ohos.required_mount.system` contains `/dev/block/platform/soc/10100000.himci.eMMC/by-name/system`, which is the soft link pointing to the system device node. 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci During creation of a block device node, the device path will be matched against the value of `default_boot_device`. If the matching is successful, a soft link pointing to the real block device node will be created in `/dev/block/by-name`. In this way, device node access is made irrelevant to the chip platform. 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci - Example 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci This example assumes the `system` partition as the required partition on the Hi3516D V300 platform to illustrate the boot process. During this process, the init process reads the `fstab.required` file, creates a block device node, and mounts it to the required partition. The following provides the key code snippets and log information as reference for debugging. 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci > **NOTE** 190e41f4b71Sopenharmony_ci > 191e41f4b71Sopenharmony_ci > The code snippets below are exhibited in the logical sequence. They are not neighboring to each other in the source code. 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci 1. Obtain the `required` partition device information. 194e41f4b71Sopenharmony_ci ``` 195e41f4b71Sopenharmony_ci Fstab* LoadRequiredFstab(void) 196e41f4b71Sopenharmony_ci { 197e41f4b71Sopenharmony_ci Fstab *fstab = NULL; 198e41f4b71Sopenharmony_ci fstab = LoadFstabFromCommandLine(); 199e41f4b71Sopenharmony_ci if (fstab == NULL) { 200e41f4b71Sopenharmony_ci INIT_LOGI("Cannot load fstab from command line, try read from fstab.required"); 201e41f4b71Sopenharmony_ci const char *fstabFile = "/etc/fstab.required"; 202e41f4b71Sopenharmony_ci if (access(fstabFile, F_OK) != 0) { 203e41f4b71Sopenharmony_ci fstabFile = "/system/etc/fstab.required"; 204e41f4b71Sopenharmony_ci } 205e41f4b71Sopenharmony_ci INIT_ERROR_CHECK(access(fstabFile, F_OK) == 0, abort(), "Failed get fstab.required"); 206e41f4b71Sopenharmony_ci fstab = ReadFstabFromFile(fstabFile, false); 207e41f4b71Sopenharmony_ci } 208e41f4b71Sopenharmony_ci return fstab; 209e41f4b71Sopenharmony_ci } 210e41f4b71Sopenharmony_ci ``` 211e41f4b71Sopenharmony_ci The preceding code provides two methods for the init process to obtain the fstab information. First, the init process calls `LoadFstabFromCommandLine()` to read the fstab information from cmdline. If the attempt fails, the init process outputs log information and continues to read the `fstab.required` file for the fstab information. 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci For the `system` partition, the key information read from devices is as follows: 214e41f4b71Sopenharmony_ci ``` 215e41f4b71Sopenharmony_ci /dev/block/platform/fe310000.sdhci/by-name/system 216e41f4b71Sopenharmony_ci ``` 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ci 2. Create a socket and trigger the kernel to report a **uevent** message. 219e41f4b71Sopenharmony_ci ``` 220e41f4b71Sopenharmony_ci static int StartUeventd(char **requiredDevices, int num) 221e41f4b71Sopenharmony_ci { 222e41f4b71Sopenharmony_ci INIT_ERROR_CHECK(requiredDevices != NULL && num > 0, return -1, "Failed parameters"); 223e41f4b71Sopenharmony_ci int ueventSockFd = UeventdSocketInit(); 224e41f4b71Sopenharmony_ci if (ueventSockFd < 0) { 225e41f4b71Sopenharmony_ci INIT_LOGE("Failed to create uevent socket"); 226e41f4b71Sopenharmony_ci return -1; 227e41f4b71Sopenharmony_ci } 228e41f4b71Sopenharmony_ci RetriggerUevent(ueventSockFd, requiredDevices, num); 229e41f4b71Sopenharmony_ci close(ueventSockFd); 230e41f4b71Sopenharmony_ci return 0; 231e41f4b71Sopenharmony_ci } 232e41f4b71Sopenharmony_ci ``` 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_ci 3. Read information from cmdline to obtain `default_boot_device`. 235e41f4b71Sopenharmony_ci ``` 236e41f4b71Sopenharmony_ci char *buffer = ReadFileData("/proc/cmdline"); 237e41f4b71Sopenharmony_ci int ret = GetProcCmdlineValue("default_boot_device", buffer, bootDevice, CMDLINE_VALUE_LEN_MAX); 238e41f4b71Sopenharmony_ci INIT_CHECK_ONLY_ELOG(ret == 0, "Failed get default_boot_device value from cmdline"); 239e41f4b71Sopenharmony_ci ``` 240e41f4b71Sopenharmony_ci In this example, the value of `default_boot_device` is `soc/10100000.himci.eMMC`. The value is stored in the global variable `bootDevice` and will be matched with the path of the `system` partition device when a soft link is created. 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ci 4. Process the **uevent** message of the `required` partition device. 243e41f4b71Sopenharmony_ci ``` 244e41f4b71Sopenharmony_ci if (uevent->partitionName == NULL) { 245e41f4b71Sopenharmony_ci INIT_LOGI("Match with %s for %s", devices[i], uevent->syspath); 246e41f4b71Sopenharmony_ci deviceName = strstr(devices[i], "/dev/block"); 247e41f4b71Sopenharmony_ci INIT_INFO_CHECK(deviceName != NULL, continue, 248e41f4b71Sopenharmony_ci "device %s not match \"/dev/block\".", devices[i]); 249e41f4b71Sopenharmony_ci deviceName += sizeof("/dev/block") - 1; 250e41f4b71Sopenharmony_ci INIT_INFO_CHECK(strstr(uevent->syspath, deviceName) != NULL, continue, 251e41f4b71Sopenharmony_ci "uevent->syspath %s not match deviceName %s", uevent->syspath, deviceName); 252e41f4b71Sopenharmony_ci HandleBlockDeviceEvent(uevent); 253e41f4b71Sopenharmony_ci break; 254e41f4b71Sopenharmony_ci } else if (strstr(devices[i], uevent->partitionName) != NULL) { 255e41f4b71Sopenharmony_ci INIT_LOGI("Handle block device partitionName %s", uevent->partitionName); 256e41f4b71Sopenharmony_ci HandleBlockDeviceEvent(uevent); 257e41f4b71Sopenharmony_ci break; 258e41f4b71Sopenharmony_ci } 259e41f4b71Sopenharmony_ci ``` 260e41f4b71Sopenharmony_ci In this step, the device information in `devices` is matched with the **uevent** message reported by the kernel. The value of `uevent -> partitionName` should be system for the **uevent** message of the system partition device. If the value matches that of the `/dev/block/platform/fe310000.sdhci/by-name/system` field in `devices`, the **uevent** message of the system partition device will be processed. 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ci 5. Create the `required` partition device node and the corresponding soft link. 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci The first thing is to format the path of the corresponding soft link. In this step, the value of `default_boot_device` in `bootargs` will be matched with the path of the required device node in the **uevent** message, so as to create a platform-irrelevant soft link that points to the device node. The key code is as follows: 265e41f4b71Sopenharmony_ci ``` 266e41f4b71Sopenharmony_ci if (STRINGEQUAL(bus, "/sys/bus/platform")) { 267e41f4b71Sopenharmony_ci INIT_LOGV("Find a platform device: %s", parent); 268e41f4b71Sopenharmony_ci parent = FindPlatformDeviceName(parent); 269e41f4b71Sopenharmony_ci if (parent != NULL) { 270e41f4b71Sopenharmony_ci BuildDeviceSymbolLinks(links, linkNum, parent, uevent->partitionName, uevent->deviceName); 271e41f4b71Sopenharmony_ci } 272e41f4b71Sopenharmony_ci linkNum++; 273e41f4b71Sopenharmony_ci if ((parent != NULL) && STRINGEQUAL(parent, bootDevice)) { 274e41f4b71Sopenharmony_ci BuildBootDeviceSymbolLink(links, linkNum, uevent->partitionName); 275e41f4b71Sopenharmony_ci linkNum++; 276e41f4b71Sopenharmony_ci } 277e41f4b71Sopenharmony_ci } 278e41f4b71Sopenharmony_ci ``` 279e41f4b71Sopenharmony_ci The key variables in the code are as follows: 280e41f4b71Sopenharmony_ci 281e41f4b71Sopenharmony_ci - `bus`: a string that saves the path of the bus connected to the current device. 282e41f4b71Sopenharmony_ci - `parent`: a string that stores the device path obtained from `uevent -> syspath` in the **uevent** message. 283e41f4b71Sopenharmony_ci - `links`: a pointer to the memory that stores the soft link path. 284e41f4b71Sopenharmony_ci - `bootDevice`: a string that stores the value of `default_boot_device` in `bootargs`. 285e41f4b71Sopenharmony_ci According to the code, the corresponding soft link is created for the device only when the type of the connected bus is platform. The path of the soft link is as follows: 286e41f4b71Sopenharmony_ci ``` 287e41f4b71Sopenharmony_ci /dev/block/platform/soc/10100000.himci.eMMC/by-name 288e41f4b71Sopenharmony_ci ``` 289e41f4b71Sopenharmony_ci A platform-irrelevant soft link is created only when the device path matches that in `bootDevice`. 290e41f4b71Sopenharmony_ci 291e41f4b71Sopenharmony_ci For the `system` partition device, the path is as follows: 292e41f4b71Sopenharmony_ci ``` 293e41f4b71Sopenharmony_ci /sys/devices/platform/soc/10100000.himci.eMMC/mmc_host/mmc0/mmc0:0001/block/mmcblk0/mmcblk0p5 294e41f4b71Sopenharmony_ci ``` 295e41f4b71Sopenharmony_ci Therefore, when processing the **uevent** message of the device, the init process compares the device path with that in `bootDevice`, that is, `soc/10100000.himci.eMMC`. If they match, a soft link will be created as follows: 296e41f4b71Sopenharmony_ci ``` 297e41f4b71Sopenharmony_ci /dev/block/by-name/system 298e41f4b71Sopenharmony_ci ``` 299e41f4b71Sopenharmony_ci 300e41f4b71Sopenharmony_ci After the soft link path is formatted, the init process creates the device node and soft link based on the information in the **uevent** message. Up to now, the creation of a device node for the `system` partition is complete. 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci 6. Mount the `required` partition. 303e41f4b71Sopenharmony_ci 304e41f4b71Sopenharmony_ci After a device node is created, mount it to the corresponding partition. The code is as follows: 305e41f4b71Sopenharmony_ci ``` 306e41f4b71Sopenharmony_ci int MountRequiredPartitions(const Fstab *fstab) 307e41f4b71Sopenharmony_ci { 308e41f4b71Sopenharmony_ci INIT_ERROR_CHECK(fstab != NULL, return -1, "Failed fstab is NULL"); 309e41f4b71Sopenharmony_ci int rc; 310e41f4b71Sopenharmony_ci INIT_LOGI("Mount required partitions"); 311e41f4b71Sopenharmony_ci rc = MountAllWithFstab(fstab, 1); 312e41f4b71Sopenharmony_ci return rc; 313e41f4b71Sopenharmony_ci } 314e41f4b71Sopenharmony_ci ``` 315e41f4b71Sopenharmony_ci Therefore, when "Mount required partitions" is displayed, the `required` partition device is ready for mounting. During the mounting process, the following key information is printed: 316e41f4b71Sopenharmony_ci ``` 317e41f4b71Sopenharmony_ci BEGET_LOGE("Unsupported file system \" %s \"", item->fsType); 318e41f4b71Sopenharmony_ci ``` 319e41f4b71Sopenharmony_ci The current file system type is not supported. 320e41f4b71Sopenharmony_ci ``` 321e41f4b71Sopenharmony_ci BEGET_LOGE("Cannot get stat of \" %s \", err = %d", target, errno); 322e41f4b71Sopenharmony_ci ``` 323e41f4b71Sopenharmony_ci The attempt to obtain the mount point directory has failed. 324e41f4b71Sopenharmony_ci ``` 325e41f4b71Sopenharmony_ci BEGET_LOGE("Failed to create dir \" %s \", err = %d", target, errno); 326e41f4b71Sopenharmony_ci ``` 327e41f4b71Sopenharmony_ci The attempt to create the mount point directory has failed. 328e41f4b71Sopenharmony_ci ``` 329e41f4b71Sopenharmony_ci BEGET_LOGI("Mount %s to %s successful", item->deviceName, item->mountPoint); 330e41f4b71Sopenharmony_ci ``` 331e41f4b71Sopenharmony_ci The device is successfully mounted. The output also contains the name of the mounted device and information about the mount point. 332e41f4b71Sopenharmony_ci 333e41f4b71Sopenharmony_ci- Mounting of `vendor` partition 334e41f4b71Sopenharmony_ci 335e41f4b71Sopenharmony_ciAfter mounting required partitions, the init process scans each script file in the `vendor` partition. The initialization scripts related to the chip or development board is named in the format of `/vendor/etc/init.{ohos.boot.hardware}.cfg`. Wherein, `/vendor/etc/fstab.{ohos.boot.hardware}` represents the extended mount partition file; `hardware` is sourced from `bootargs`, which is passed from the bootloader to the kernel. 336e41f4b71Sopenharmony_ci 337e41f4b71Sopenharmony_ci### fstable File Description 338e41f4b71Sopenharmony_ci 339e41f4b71Sopenharmony_ci- File content 340e41f4b71Sopenharmony_ci 341e41f4b71Sopenharmony_ci ``` 342e41f4b71Sopenharmony_ci # fstab file. 343e41f4b71Sopenharmony_ci #<src> <mnt_point> <type> <mnt_flags and options> <fs_mgr_flags> 344e41f4b71Sopenharmony_ci /dev/block/platform/fe310000.sdhci/by-name/system /usr ext4 ro,barrier=1 wait,required 345e41f4b71Sopenharmony_ci /dev/block/platform/fe310000.sdhci/by-name/vendor /vendor ext4 ro,barrier=1 wait,required 346e41f4b71Sopenharmony_ci /dev/block/platform/fe310000.sdhci/by-name/sys-prod /sys_prod ext4 ro,barrier=1 wait 347e41f4b71Sopenharmony_ci /dev/block/platform/fe310000.sdhci/by-name/chip-prod /chip_prod ext4 ro,barrier=1 wait 348e41f4b71Sopenharmony_ci /dev/block/platform/fe310000.sdhci/by-name/userdata /data f2fs discard,noatime,nosuid,nodev,fscrypt=2:aes-256-cts:aes-256-xts,usrquota wait,check,fileencryption=software,quota 349e41f4b71Sopenharmony_ci ``` 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_ci- Field description 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ci | Field | Description | 354e41f4b71Sopenharmony_ci | --------------------- | ------------------------------------------------------------ | 355e41f4b71Sopenharmony_ci | src | Address of the partition or path of the device mounted to the file system | 356e41f4b71Sopenharmony_ci | mnt_point | Mount point in the root file system. | 357e41f4b71Sopenharmony_ci | type | File system type. Common file systems are **ext2**, **vfat**, and **NTFS**. | 358e41f4b71Sopenharmony_ci | mnt_flags and options | Mounting parameters. | 359e41f4b71Sopenharmony_ci | fs_mgr_flags | File system manager flags.<br>Available values include **check**, **wait**, **required**, **nofail**, and **hvb**.| 360e41f4b71Sopenharmony_ci 361e41f4b71Sopenharmony_ci- Description of mnt_flags and options 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ci ``` 364e41f4b71Sopenharmony_ci auto Attempts automatic mounting at startup or after the mount -a command is entered. 365e41f4b71Sopenharmony_ci noauto Do not attempt automatic mounting upon system startup. The mount -a command is not used to load the file system. 366e41f4b71Sopenharmony_ci exec Allows execution of binary files in this file system. 367e41f4b71Sopenharmony_ci noexec Do not allow execution of binary files in this file system. 368e41f4b71Sopenharmony_ci ro Mounts a file system as read-only. 369e41f4b71Sopenharmony_ci rw Mounts a file system as read/write. 370e41f4b71Sopenharmony_ci user Allows any user to mount the file system. If no definition is displayed, the noexec nosuid nodev parameter is implicitly enabled. 371e41f4b71Sopenharmony_ci users Allows all users in the users group to mount the file system. 372e41f4b71Sopenharmony_ci nouser Allows only the root user to mount the file system. 373e41f4b71Sopenharmony_ci owner Allows only the device owner to mount the file system. 374e41f4b71Sopenharmony_ci sync Synchronizes the data in the memory and disk in real time without buffering the write operations of the device. This protects the file system against damages caused by abnormal device shutdown but reduces the system speed. 375e41f4b71Sopenharmony_ci async Do not synchronize the data in the memory and disk. The system writes the memory data to the disk at a specified interval. 376e41f4b71Sopenharmony_ci dev Parses block special files in the file system. 377e41f4b71Sopenharmony_ci nodev Do not parse block special files in the file system. 378e41f4b71Sopenharmony_ci suid/nosuid Allows a partition to have the suid attribute or not. 379e41f4b71Sopenharmony_ci quota Forcibly limits the disk quota on the file system. 380e41f4b71Sopenharmony_ci usrquota Enables the disk quota mode to limit the disk quota by users. 381e41f4b71Sopenharmony_ci grpquota Enables the disk quota mode to limit the disk quota by groups. 382e41f4b71Sopenharmony_ci nodiratime Do not update the inode access records on the file system. 383e41f4b71Sopenharmony_ci realtime Updates inode access records in real time. 384e41f4b71Sopenharmony_ci defaults Uses the default mounting parameters of the file system. The default parameters of the ext4 file system are rw, suid, dev, exec ,auto, nouser, and async. 385e41f4b71Sopenharmony_ci ``` 386e41f4b71Sopenharmony_ci### Boot Loading Without ramdisk 387e41f4b71Sopenharmony_ci 388e41f4b71Sopenharmony_ciCertain development boards do not use the ramdisk boot mode. For these boards, the boot process is implemented by directly loading the `system.img` file through the kernel. In such case, you need to modify the product configuration file in `productdefine`. Specifically, you need to turn off the `enable_ramdisk` switch to disable ramdisk generation so that the init process does not boot from `ramdisk` to `system`. 389e41f4b71Sopenharmony_ci 390e41f4b71Sopenharmony_ciThe boot loading process in this scenario is similar to that in the preceding section. The only difference is as follows: If ramdisk is used, the init process mounts `system.img` to the `/usr` directory and then switches to the `/usr` directory using chroot. If ramdisk is not used, the init process directly runs the `init.cfg` file. 391e41f4b71Sopenharmony_ci 392e41f4b71Sopenharmony_ciFor the boot loading process without ramdisk, that is, system as root, the block device where the root file system is located is passed to the kernel through `bootargs`, for example, `root=/dev/mmcblk0p5` and `rootfstype=ext4`. During initialization of the root file system, the kernel parses the `root` field in `bootargs` to complete mounting of the root file system. 393e41f4b71Sopenharmony_ci 394e41f4b71Sopenharmony_ci 395e41f4b71Sopenharmony_ci### Partition A/B Booting 396e41f4b71Sopenharmony_ci 397e41f4b71Sopenharmony_ciCurrently, OpenHarmony supports booting from partitions A and B (active and standby system partitions), both of which are configured in the same device storage. During the booting process, the system partition to load is determined based on the slot of the active partition. Partition A/B booting is supported only for the system and chipset partitions. 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ci- bootslots 400e41f4b71Sopenharmony_ci 401e41f4b71Sopenharmony_ci Number of the supported boot partitions. If `bootslots` is set to **2**, the system can boot from both system partitions A and B. If `bootslots` is set to **1**, partition A/B booting is not supported and the system can boot only from the default system partition. 402e41f4b71Sopenharmony_ci 403e41f4b71Sopenharmony_ci In the initial phase of init process startup, the system reads the `bootslots` value to determine whether partition A/B booting is supported. If yes, the system continues to determine the system partition to mount. If not, the system mounts the system partition based on the default fstab. The API for the init process to obtain the `bootslots` value is as follows: 404e41f4b71Sopenharmony_ci ``` 405e41f4b71Sopenharmony_ci int GetBootSlots(void) 406e41f4b71Sopenharmony_ci { 407e41f4b71Sopenharmony_ci int bootSlots = GetSlotInfoFromParameter("bootslots"); 408e41f4b71Sopenharmony_ci BEGET_CHECK_RETURN_VALUE(bootSlots <= 0, bootSlots); 409e41f4b71Sopenharmony_ci BEGET_LOGI("No valid slot value found from parameter, try to get it from cmdline"); 410e41f4b71Sopenharmony_ci return GetSlotInfoFromCmdLine("bootslots"); 411e41f4b71Sopenharmony_ci } 412e41f4b71Sopenharmony_ci ``` 413e41f4b71Sopenharmony_ci After normal system startup, you can obtain the `bootslots` value from the system parameter `ohos.boot.bootslots` to check whether the current system supports partition A/B booting. The command for obtaining `ohos.boot.bootslots` is as follows: 414e41f4b71Sopenharmony_ci ``` 415e41f4b71Sopenharmony_ci param get ohos.boot.bootslots 416e41f4b71Sopenharmony_ci ``` 417e41f4b71Sopenharmony_ci 418e41f4b71Sopenharmony_ci- currentslot 419e41f4b71Sopenharmony_ci 420e41f4b71Sopenharmony_ci `currentslot` indicates the current system partition, for example, partition A or partition B. The value is a number. For example, **1** indicates partition A, and **2** indicates partition B. 421e41f4b71Sopenharmony_ci 422e41f4b71Sopenharmony_ci In the initial phase of startup, the init process determines whether the system supports partition A/B booting based on `bootslots`. If the system does not support partition A/B booting, the init process directly boots from the default system partition instead of obtaining the `currentslot` value. If the system supports partition A/B booting, the init process obtains the `currentslot` value and determines whether partition A or partition B is the current system partition. The API for the init process to obtain the `currentslot` value is as follows: 423e41f4b71Sopenharmony_ci ``` 424e41f4b71Sopenharmony_ci int GetCurrentSlot(void) 425e41f4b71Sopenharmony_ci { 426e41f4b71Sopenharmony_ci // get current slot from parameter 427e41f4b71Sopenharmony_ci int currentSlot = GetSlotInfoFromParameter("currentslot"); 428e41f4b71Sopenharmony_ci BEGET_CHECK_RETURN_VALUE(currentSlot <= 0, currentSlot); 429e41f4b71Sopenharmony_ci BEGET_LOGI("No valid slot value found from parameter, try to get it from cmdline"); 430e41f4b71Sopenharmony_ci 431e41f4b71Sopenharmony_ci // get current slot from cmdline 432e41f4b71Sopenharmony_ci currentSlot = GetSlotInfoFromCmdLine("currentslot"); 433e41f4b71Sopenharmony_ci BEGET_CHECK_RETURN_VALUE(currentSlot <= 0, currentSlot); 434e41f4b71Sopenharmony_ci BEGET_LOGI("No valid slot value found from cmdline, try to get it from misc"); 435e41f4b71Sopenharmony_ci 436e41f4b71Sopenharmony_ci // get current slot from misc 437e41f4b71Sopenharmony_ci return GetSlotInfoFromMisc(MISC_PARTITION_ACTIVE_SLOT_OFFSET, MISC_PARTITION_ACTIVE_SLOT_SIZE); 438e41f4b71Sopenharmony_ci } 439e41f4b71Sopenharmony_ci ``` 440e41f4b71Sopenharmony_ci 441e41f4b71Sopenharmony_ci- Partition A/B booting process 442e41f4b71Sopenharmony_ci 443e41f4b71Sopenharmony_ci 1. Obtain the `currentslot` value to determine whether partition A or partition B is the current system partition. 444e41f4b71Sopenharmony_ci 2. Construct new partition mounting configuration based on the original fstab file, and add the suffix `_a` or `_b` to the partitions that support partition A/B booting, that is, `system` and `chipset` partitions. 445e41f4b71Sopenharmony_ci 3. Mount the partition added with the corresponding suffix and enter the second phase of startup. This phase occurs in partition A or B and concludes the partition A/B booting process. 446e41f4b71Sopenharmony_ci 447e41f4b71Sopenharmony_ci The API for constructing new partition mounting configuration is as follows: 448e41f4b71Sopenharmony_ci ``` 449e41f4b71Sopenharmony_ci static void AdjustPartitionNameByPartitionSlot(FstabItem *item) 450e41f4b71Sopenharmony_ci { 451e41f4b71Sopenharmony_ci BEGET_CHECK_ONLY_RETURN(strstr(item->deviceName, "/system") != NULL || 452e41f4b71Sopenharmony_ci strstr(item->deviceName, "/chipset") != NULL); 453e41f4b71Sopenharmony_ci char buffer[MAX_BUFFER_LEN] = {0}; 454e41f4b71Sopenharmony_ci int slot = GetCurrentSlot(); 455e41f4b71Sopenharmony_ci BEGET_ERROR_CHECK(slot > 0 && slot <= MAX_SLOT, slot = 1, "slot value %d is invalid, set default value", slot); 456e41f4b71Sopenharmony_ci BEGET_INFO_CHECK(slot > 1, return, "default partition doesn't need to add suffix"); 457e41f4b71Sopenharmony_ci BEGET_ERROR_CHECK(sprintf_s(buffer, sizeof(buffer), "%s_%c", item->deviceName, 'a' + slot - 1) > 0, 458e41f4b71Sopenharmony_ci return, "Failed to format partition name suffix, use default partition name"); 459e41f4b71Sopenharmony_ci free(item->deviceName); 460e41f4b71Sopenharmony_ci item->deviceName = strdup(buffer); 461e41f4b71Sopenharmony_ci BEGET_LOGI("partition name with slot suffix: %s", item->deviceName); 462e41f4b71Sopenharmony_ci } 463e41f4b71Sopenharmony_ci ``` 464e41f4b71Sopenharmony_ci 465e41f4b71Sopenharmony_ci- Development example 466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_ci The following uses the rk3568 platform as an example to illustrate how to change from default partition booting to partition A/B booting. 468e41f4b71Sopenharmony_ci 469e41f4b71Sopenharmony_ci 1. Burn the original image, and view the device information of each partition. 470e41f4b71Sopenharmony_ci 471e41f4b71Sopenharmony_ci  472e41f4b71Sopenharmony_ci 473e41f4b71Sopenharmony_ci Use the original image to construct images of the partitions used for partition A/B booting, and test the partition A/B booting function. 474e41f4b71Sopenharmony_ci - Copy the `system` and `vendor` images, and add the suffix `_b` to them. 475e41f4b71Sopenharmony_ci - Add the `system_b` and `vendor_b` partitions to the partition table in the `parameter.txt` file. 476e41f4b71Sopenharmony_ci 477e41f4b71Sopenharmony_ci 2. Burn the images of the partitions used for partition A/B booting. 478e41f4b71Sopenharmony_ci 479e41f4b71Sopenharmony_ci - Import the partition configuration to the rk3568 burning tool, and select the `parameter.txt` file containing the `system_b` and `vendor_b` partitions. 480e41f4b71Sopenharmony_ci - Select images (including `system_b` and `vendor_b` images) based on the new partition table configuration, and then burn the images. 481e41f4b71Sopenharmony_ci 482e41f4b71Sopenharmony_ci 3. After the configuration is complete, perform the following: 483e41f4b71Sopenharmony_ci 484e41f4b71Sopenharmony_ci 1. Run the `cat /proc/cmdline` command. If the command output contains `bootslot=2`, the system supports partition A/B booting. 485e41f4b71Sopenharmony_ci 486e41f4b71Sopenharmony_ci  487e41f4b71Sopenharmony_ci 2. Run the `param get ohos.boot.bootslot` command. If the command output contains 2, the `bootslot` information is successfully written to the `parameter.txt` file. 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_ci 3. Run the `ls -l /dev/block/by-name` command. If the command output contains `system_b` and `vendor_b`, device nodes are successfully created in partition B. 490e41f4b71Sopenharmony_ci 491e41f4b71Sopenharmony_ci  492e41f4b71Sopenharmony_ci 493e41f4b71Sopenharmony_ci 4. Run the `df -h` command to check the partitions mounted to the system. 494e41f4b71Sopenharmony_ci 495e41f4b71Sopenharmony_ci  496e41f4b71Sopenharmony_ci 497e41f4b71Sopenharmony_ci As shown in the figure, partition `mmcblk0p6` is mounted to the root file system (represented by a slash), and partition `mmcblk0p7` is mounted to `/vendor`. Based on the command output in step 3, `mmcblk0p6` is the system partition, and `mmcblk0p7` is the `/vendor` partition. That is, the mounted partitions are the default partitions, that is, `system` and `vendor` partitions without suffixes. In other words, partition A is the default partition. 498e41f4b71Sopenharmony_ci 499e41f4b71Sopenharmony_ci Next, let's try booting from partition B. 500e41f4b71Sopenharmony_ci 501e41f4b71Sopenharmony_ci 1. Run the `partitionslot setactive 2` command to set the slot of the active partition to **2**, that is, the slot of partition B. 502e41f4b71Sopenharmony_ci 503e41f4b71Sopenharmony_ci  504e41f4b71Sopenharmony_ci 505e41f4b71Sopenharmony_ci 2. Run the `partitionslot getslot` command to check the configured slot. 506e41f4b71Sopenharmony_ci 507e41f4b71Sopenharmony_ci  508e41f4b71Sopenharmony_ci 509e41f4b71Sopenharmony_ci If `current slot` is 2, the slot of the active partition is successfully set to **2**. 510e41f4b71Sopenharmony_ci 511e41f4b71Sopenharmony_ci 3. Upon restarting, run the `df -h` command to check the partitions mounted to the system. 512e41f4b71Sopenharmony_ci 513e41f4b71Sopenharmony_ci According to the command output, partition `mmcblk0p11` is mounted to the root file system, and partition `mmcblk0p12` is mounted to `/vendor`. 514e41f4b71Sopenharmony_ci 515e41f4b71Sopenharmony_ci  516e41f4b71Sopenharmony_ci 517e41f4b71Sopenharmony_ci 4. Run the `ls -l /dev/block/by-name` command again. 518e41f4b71Sopenharmony_ci 519e41f4b71Sopenharmony_ci  520e41f4b71Sopenharmony_ci 521e41f4b71Sopenharmony_ci mmcblk0p11 corresponds to system_b, and mmcblk0p12 corresponds to vendor_b. That is, the system is successfully booted from partition B. 522