1d6aed566Sopenharmony_ci### Qemu RISC-V virt HOWTO
2d6aed566Sopenharmony_ci
3d6aed566Sopenharmony_ci#### 1. Brief introduction
4d6aed566Sopenharmony_ci`riscv32_virt/` subdirectory contains part of the OpenHarmony LiteOS demonstration support for Qemu risc-v virt Platform,
5d6aed566Sopenharmony_cihere called *riscv32_virt*.
6d6aed566Sopenharmony_ciRISC-V Virtual platform is a `qemu-system-riscv32` machine target that provides emulation
7d6aed566Sopenharmony_cifor a generic, riscv-based board.
8d6aed566Sopenharmony_ci
9d6aed566Sopenharmony_ciIntroduced functionality adds support for RISC-V (1 CPU with security extensions), 128MB memory virtual platform.
10d6aed566Sopenharmony_ci
11d6aed566Sopenharmony_ciNote: System memory size is hard-coded to 128MB.
12d6aed566Sopenharmony_ci
13d6aed566Sopenharmony_ci#### 2. Setting up environment
14d6aed566Sopenharmony_ci
15d6aed566Sopenharmony_ci[Setting up a development environment](https://gitee.com/openharmony/docs/blob/master/en/device-dev/quick-start/Readme-EN.md)
16d6aed566Sopenharmony_ci
17d6aed566Sopenharmony_ci[Compiler install:gcc_riscv32](https://gitee.com/openharmony/docs/blob/master/en/device-dev/quick-start/quickstart-pkg-3861-tool.md),
18d6aed566Sopenharmony_ciNote: [Downloadable directly](https://repo.huaweicloud.com/harmonyos/compiler/gcc_riscv32/7.3.0/linux/gcc_riscv32-linux-7.3.0.tar.gz)
19d6aed566Sopenharmony_ci
20d6aed566Sopenharmony_ci#### 3. Code acquisition
21d6aed566Sopenharmony_ci
22d6aed566Sopenharmony_ci[Code acquisition](https://gitee.com/openharmony/docs/blob/HEAD/en/device-dev/get-code/sourcecode-acquire.md)
23d6aed566Sopenharmony_ci
24d6aed566Sopenharmony_ciNote: One can use `repo` to fetch code in a straightforward manner.
25d6aed566Sopenharmony_ci
26d6aed566Sopenharmony_ci#### 4. Building from sources
27d6aed566Sopenharmony_ci
28d6aed566Sopenharmony_ciIn the root directory of the obtained source code, run the following command:
29d6aed566Sopenharmony_ci
30d6aed566Sopenharmony_ci```
31d6aed566Sopenharmony_cihb set
32d6aed566Sopenharmony_ci```
33d6aed566Sopenharmony_ci
34d6aed566Sopenharmony_ciSelect `qemu_riscv_mini_system_demo` under **ohemu**.
35d6aed566Sopenharmony_ci
36d6aed566Sopenharmony_ciRun the following build command:
37d6aed566Sopenharmony_ci
38d6aed566Sopenharmony_ci```
39d6aed566Sopenharmony_cihb build
40d6aed566Sopenharmony_ci```
41d6aed566Sopenharmony_ci
42d6aed566Sopenharmony_ciThis will build `OHOS_Image` for Qemu RISC-V virt machine.
43d6aed566Sopenharmony_ci
44d6aed566Sopenharmony_ciAfter build is finished, the resulting image can be found in:
45d6aed566Sopenharmony_ci```
46d6aed566Sopenharmony_ciout/riscv32_virt/qemu_riscv_mini_system_demo/
47d6aed566Sopenharmony_ci```
48d6aed566Sopenharmony_ci
49d6aed566Sopenharmony_ci#### 5. Running image in Qemu
50d6aed566Sopenharmony_ci
51d6aed566Sopenharmony_cia) If not installed, please install `qemu-system-riscv32`
52d6aed566Sopenharmony_ciFor details, please refer to the HOWTO: [Qemu installation](https://gitee.com/openharmony/device_qemu/blob/HEAD/README.md)
53d6aed566Sopenharmony_ci
54d6aed566Sopenharmony_cib) Run
55d6aed566Sopenharmony_ci
56d6aed566Sopenharmony_ci(1) qemu version < 5.0.0
57d6aed566Sopenharmony_ci
58d6aed566Sopenharmony_ci```
59d6aed566Sopenharmony_cicd device/qemu/riscv32_virt
60d6aed566Sopenharmony_ciqemu-system-riscv32 -machine virt -m 128M -kernel ../../../out/riscv32_virt/qemu_riscv_mini_system_demo/OHOS_Image -nographic -append "root=dev/vda or console=ttyS0"
61d6aed566Sopenharmony_ci```
62d6aed566Sopenharmony_ci
63d6aed566Sopenharmony_ci(2). qemu version >= 5.0.0
64d6aed566Sopenharmony_ci
65d6aed566Sopenharmony_ciRun the `./qemu-run --help` command. The following information is displayed:
66d6aed566Sopenharmony_ci
67d6aed566Sopenharmony_ci```
68d6aed566Sopenharmony_ciUsage: ./qemu-run [OPTION]...
69d6aed566Sopenharmony_ciRun a OHOS image in qemu according to the options.
70d6aed566Sopenharmony_ci
71d6aed566Sopenharmony_ci    Options:
72d6aed566Sopenharmony_ci
73d6aed566Sopenharmony_ci    -e, --exec file_name     kernel exec file name
74d6aed566Sopenharmony_ci    -n, --net-enable         enable net
75d6aed566Sopenharmony_ci    -l, --local-desktop      no VNC
76d6aed566Sopenharmony_ci    -g, --gdb                enable gdb for kernel
77d6aed566Sopenharmony_ci    -t, --test               test mode, exclusive with -g
78d6aed566Sopenharmony_ci    -h, --help               print help info
79d6aed566Sopenharmony_ci
80d6aed566Sopenharmony_ci    By default, the kernel exec file is: out/riscv32_virt/qemu_riscv_mini_system_demo/OHOS_Image,
81d6aed566Sopenharmony_ci    and net will not be enabled, gpu enabled and waiting for VNC connection at port 5920.
82d6aed566Sopenharmony_ci```
83d6aed566Sopenharmony_ciBy default, the network will not be automatically configured if no parameter is specified, and the default kernel exec file will be used.
84d6aed566Sopenharmony_ciIf you want to use other kernel exec file, please try `./qemu-run -e [file_name]` to change it.
85d6aed566Sopenharmony_ci
86d6aed566Sopenharmony_ci#### 6. gdb debug
87d6aed566Sopenharmony_ci
88d6aed566Sopenharmony_ci```
89d6aed566Sopenharmony_cicd device/qemu/riscv32_virt
90d6aed566Sopenharmony_civim liteos_m/config.gni
91d6aed566Sopenharmony_ci```
92d6aed566Sopenharmony_ci
93d6aed566Sopenharmony_ciIn the modified `board_opt_flags` compiler options:
94d6aed566Sopenharmony_ci
95d6aed566Sopenharmony_ci```
96d6aed566Sopenharmony_ciboard_opt_flags = []
97d6aed566Sopenharmony_ci```
98d6aed566Sopenharmony_ci
99d6aed566Sopenharmony_cito:
100d6aed566Sopenharmony_ci
101d6aed566Sopenharmony_ci```
102d6aed566Sopenharmony_ciboard_opt_flags = [ "-g" ]
103d6aed566Sopenharmony_ci```
104d6aed566Sopenharmony_ci
105d6aed566Sopenharmony_ciSave and exit, recompile under OHOS root directory:
106d6aed566Sopenharmony_ci
107d6aed566Sopenharmony_ci```
108d6aed566Sopenharmony_cihb build -f
109d6aed566Sopenharmony_ci```
110d6aed566Sopenharmony_ci
111d6aed566Sopenharmony_ciIn a window to enter the command:
112d6aed566Sopenharmony_ci
113d6aed566Sopenharmony_ci```
114d6aed566Sopenharmony_ci./qemu-run -g
115d6aed566Sopenharmony_ci```
116d6aed566Sopenharmony_ci
117d6aed566Sopenharmony_ciIn another window to enter the command:
118d6aed566Sopenharmony_ci
119d6aed566Sopenharmony_ci```
120d6aed566Sopenharmony_ciriscv32-unknown-elf-gdb out/riscv32_virt/qemu_riscv_mini_system_demo/OHOS_Image
121d6aed566Sopenharmony_ci(gdb) target remote localhost:1234
122d6aed566Sopenharmony_ci(gdb) b main
123d6aed566Sopenharmony_ci```
124d6aed566Sopenharmony_ci
125d6aed566Sopenharmony_ciMore GDB related debugging can refer to [GDB instruction manual](https://sourceware.org/gdb/current/onlinedocs/gdb).
126d6aed566Sopenharmony_ci
127d6aed566Sopenharmony_ci## 7. Example
128d6aed566Sopenharmony_ci
129d6aed566Sopenharmony_ci- [Transferring Files Using FAT Images](example.md#sectionfatfs)
130d6aed566Sopenharmony_ci
131d6aed566Sopenharmony_ci
132