1# QEMU(Tutorials of ESP32) 2 3## 1.Introduction 4 5QEMU can simulate the kernel to run on different boards ,eliminate dependence on physical development boards. The esp32 subdirectory contains part of the OpenHarmony kernel\_liteos\_m code verified by the Qemu esp32 virtualization platform, it can be used to simulate a single-core esp32 single board. 6 7## 2.Setup Environment 8 9 1. Install esp-idf 10 11 Please refer to the installation instructions: (https://docs.espressif.com/projects/esp-idf/en/release-v4.1/get-started/index.html) 12 13 Annotation: The installation of esp-idf can be skipped. The bootloader.bin and partition-table.bin generated have been placed in the vendor\ohemu\qemu_xtensa_mini_system_demo\image folder. 14 15 Annotation: If you skip the installation of esp-idf, you can install the toolchain as follows: 16 17 Note: [Downloadable directly](https://repo.huaweicloud.com/openharmony/compiler/gcc_esp/2019r2-8.2.0/linux/esp-2019r2-8.2.0.zip) 18 19 Optional compiler installation process: 20 21 a) Download the esp official release the SDK package: https://www.espressif.com/zh-hans/support/download/sdks-demos?keys=&field_type_tid%5B%5D=13 22 23 b) Put the downloaded SDK package into the Linux system, go to the directory, and run the following commands: 24 25 ```shell 26 unzip esp-idf-v4.3.1.zip 27 cd esp-idf-v4.3.1/ 28 ./install.sh 29 . ./export.sh 30 ``` 31 32 Annotation: The version of the toolchain used in the test is GCC Version 8.2.0 (Crosstool-ng ESP-2019R2) or GCC Version 8.4.0 (Crosstool-ng ESP-2021R1). 33 34 2. Install esptool 35 36 a) In step 1, the export.sh script will set the esptool in the environment path, you need to ensure that the esptool version is 3.1 and above. 37 ```shell 38 esptool.py version 39 ``` 40 b) If the version of the esptool that comes with esp-idf is too low, delete the environment variable corresponding to the current esptool path and run the following commands. (The recommended python version is 3.8 and above) 41 ```shell 42 python -m pip install esptool 43 ``` 44 45 3. Compile qemu-system-xtensa 46 47 a) Install and compile 48 49 ```shell 50 git clone https://github.com/espressif/qemu.git 51 cd qemu 52 ./configure --target-list=xtensa-softmmu \ 53 --enable-gcrypt \ 54 --enable-debug --enable-sanitizers \ 55 --disable-strip --disable-user \ 56 --disable-capstone --disable-vnc \ 57 --disable-sdl --disable-gtk 58 ``` 59 60 b) Waiting for the completion of the compilation and executing the installation command (If the compilation fail, please refer to https://github.com/espressif/qemu/issues/21): 61 62 ```shell 63 ninja -C build 64 ``` 65 66 c) Add qemu to the environment variable (modify user_qemu_xxx_path to your own installation path): 67 68 ```shell 69 vim ~/.bashrc 70 export QEMU=user_qemu_xxx_path/qemu/build 71 source ~/.bashrc 72 ``` 73 74 d) Installation dependencies 75 76 ```shell 77 ldd $QEMU/qemu-system-xtensa 78 ``` 79 80 According to the execution result of ldd, install the missing dependent libraries 81 82 (Annotation: For more installation instructions, please refer to the following link: [Home · espressif/qemu Wiki · GitHub](https://github.com/espressif/qemu/wiki#configure)) 83 84## 3.Get source code 85 86[code acquisition ](https://gitee.com/openharmony/docs/blob/master/en/device-dev/get-code/sourcecode-acquire.md) 87 88Hint : You can use the `repo` command to get the source code. 89 90## 4.Source building 91 92 1. Execute the hb set command and select the project `qemu_xtensa_mini_system_demo`. 93 94 2. Execute the hb clean && hb build command to build the executable file that produces `OHOS_Image`. 95 96 ```shell 97 hb set 98 hb clean && hb build 99 ``` 100 101 3. After the building is complete, the corresponding executable file is in the home directory: 102 103 ``` 104 out/esp32/qemu_xtensa_mini_system_demo/ 105 ``` 106 107## 5.Run the image in Qemu 108 109 1. Run qemu(Don't cooperate with GDB ) 110 111 ```shell 112 ./qemu-run 113 ``` 114 115 2. Run qemu(Cooperate with GDB) 116 117 a) Start the GDB server and wait for the connection 118 119 ```shell 120 ./qemu-run -g 121 ``` 122 123 b) Create a new terminal and use GDB to connect to qemu 124 125 ```shell 126 xtensa-esp32-elf-gdb out/esp32/qemu_xtensa_mini_system_demo/OHOS_Image -ex "target remote :1234" 127 ``` 128 129 Annotation:Since the qemu-system-xtensa tool of qemu has the same name as the qemu-system-xtensa tool of esp32, the absolute path is used to execute the qemu-system-xtensa tool of esp32. 130 Annotation:The way to exit qemu : press ctrl and a, then release and press x. 131 132(Annotation:For more operating instructions, please refer to:[Home · espressif/qemu Wiki · GitHub](https://github.com/espressif/qemu/wiki#configure)) 133