【物联网设备端开发】ESP开发工具:QEMU的使用方法
概要
本文提供了一些运行QEMU的ESP特定说明。有关QEMU的一般使用问题,请参阅官方文档:https://www.qemu.org/documentation/.
编译 QEMU
准备工作
在此之前,请查看有关构建先决条件的QEMU文档。如果你在Linux主机上构建QEMU,你可以参考 这篇文章.
请确保您的系统上安装了libgcrypt ( Ubuntu 中的libgcrypt-devel
, Arch 中的 libgcrypt
, macOS Homebrew 中的libgcrypt
).
配置
要生成 ninja
构建文件,我们需要先配置项目,可以使用以下命令:
./configure --target-list=xtensa-softmmu \--enable-gcrypt \--enable-slirp \--enable-debug --enable-sanitizers \--enable-sdl \--disable-strip --disable-user \--disable-capstone --disable-vnc \--disable-gtk
为了减少第一次编译时间,可以添加的 --disable
选项。可启用或禁用的选项的完整列表可以通过 ./configure --help
命令获取。
If you need a graphical interface for the virtual machine, make sure to provide --enable-sdl
or --enable-gtk
or both.
如果您需要虚拟机的图形界面,请添加 --enable-sdl
或 --enable-gtk
配置,或两者都添加。
编译
成功配置项目后,可以使用 ninja
来构建它:
ninja -C build
编译可能需要几分钟,具体取决于之前启用或禁用的组件。编译完成后,会创建build/qemu-system-xtensa
可执行文件。
编译ESP-IDF程序进行仿真
QEMU for ESP32 target is ready, it already includes the first stage bootloader, located on the ROM on the real chip, which is mainly responsible for initializing the peripherals, like the UART and, more importantly, the SPI Flash. The latter must contain the second stage bootloader and the program to run.
Thus, in this section, we are going to create a flash image that combines the (second stage) bootloader, the partition table and the application to run. This can be done using esptool.py merge_bin
command, supported in esptool.py
3.1 or later. Let’s suppose that the ESP-IDf project has just been compiled successfully, the following commands will create that flash image:
cd build
esptool.py --chip esp32 merge_bin --fill-flash-size 4MB -o flash_image.bin @flash_args
Here, flash_args
is a file generated by ESP-IDF build system in the build directory, it contains the list of names of binary files and corresponding flash addresses. merge_bin
command takes this list and creates the binary image of the whole flash. --fill-flash-size 4MB
argument specifies the total flash size.
ESP32 target in QEMU supports flash of size 2, 4, 8 and 16MB, creating an image with any other size will result in an error.
Notes
-
For “Secure Boot” feature in ESP-IDF, we recommend separate command to flash bootloader and hence
flash_args
file do not have corresponding entry. However, you may modifyflash_args
file to add entry forbootloader.bin
as per below:0x1000 bootloader/bootloader.bin
-
It is also possible to use
esptool.py
to “flash” the application into QEMU, but QEMU needs to be started with the right strapping mode. See Bootstrapping Mode section below for more info.
Run QEMU
Without attaching GDB
If you don’t need to debug the guest application, you can execute QEMU without attaching GDB to it:
build/qemu-system-xtensa -nographic \-machine esp32 \-drive file=flash_image.bin,if=mtd,format=raw
Where flash_image.bin
is the SPI flash image generated previously.
With GDB server, waiting for a connection
If you need to debug the guest application, you can execute QEMU with -s -S
options. This tells QEMU not to start the CPU after initializing the virtual machine. It will wait for a connection from a GDB client:
build/qemu-system-xtensa -nographic -s -S \-machine esp32 \-drive file=flash_image.bin,if=mtd,format=raw
Where flash_image.bin
is the SPI flash image generated previously.
Then, to connect the GDB client, use the following command:
xtensa-esp32-elf-gdb build/app-name.elf \-ex "target remote :1234" \-ex "monitor system_reset" \-ex "tb app_main" -ex "c"
The last line sets a breakpoint in app_main
function of the guest application and starts the virtual CPU with c
. If you need to put a breakpoint in any other functions or if you don’t need to start the CPU directly, please adapt this last line.
GDB and toolchain compatibility
If you are using esp2021r1 or an earlier toolchain release, GDB may report the following error when connecting to QEMU:
Remote 'g' packet reply is too long (expected 420 bytes, got 628 bytes):
In that case, you need to set the following environment variable beforehand:
export QEMU_XTENSA_CORE_REGS_ONLY=1
When this environment variable is set, QEMU will only send the values of non-privileged registers to GDB.
If you are using later releases of Xtensa toolchain, i.e. esp-2021r2 and later, GDB will work out-of-the-box, without the need to set this environment variable.
Hardware crypto support
Starting from IDF 4.1, the following hardware crypto features are enabled by default: AES, SHA, RSA.
All of them are implemented in QEMU for ESP32 target. However, please note that the SHA emulation currently doesn’t support concurrent operations with different SHA types.
Ethernet support
Support for Opencores Ethernet MAC in ESP-IDF is added in https://github.com/espressif/esp-idf/commit/31dac92e5f0daac98190fd603df213a0a25a3807.
- When running
protocols
examples, enableCONFIG_EXAMPLE_CONNECT_ETHERNET
andCONFIG_EXAMPLE_USE_OPENETH
. - When running a custom app, enable
CONFIG_ETH_USE_OPENETH
and initialize the Ethernet driver as it is done in examples/common_components/protocol_examples_common/connect.c (look foresp_eth_mac_new_openeth
).
When starting QEMU, use open_eth
network device.
User mode networking
For example, to start networking in user mode (TCP/UDP only, emulated device is behind NAT), add the following option to the QEMU command line:
-nic user,model=open_eth
Some ESP projects (specifically running TCP listeners) might need port forwarding to be setup,
-nic user,model=open_eth,id=lo0,hostfwd=tcp:127.0.0.1:PORT_HOST-:PORT_GUEST
(e.g. asio-echo-server sets up a server on 2222 by default, so hostfwd=tcp:127.0.0.1:2222-:2222
enables to nc localhost 2222
from host machine)
Bridge/tap networking
- Tap networking
Specifying bootstrapping mode
To specify the desired strapping mode, it is necessary to add the following argument when running QEMU:
-global driver=esp32.gpio,property=strap_mode,value=0x0f
This sets the value of GPIO_STRAP
register.
- Use
0x12
for flash boot mode (default) - Use
0x0f
for UART-only download mode (since the SDIO part is not implemented)
Specifying eFuse storage
Add extra arguments to the command line:
-drive file=qemu_efuse.bin,if=none,format=raw,id=efuse
-global driver=nvram.esp32.efuse,property=drive,value=efuse
The first argument creates a block device backed by qemu_efuse.bin
file, with identifier efuse
. The second line configures nvram.esp32.efuse
device to use this block device for storage.
The file must be created before starting QEMU:
dd if=/dev/zero bs=1 count=124 of=/tmp/qemu_efuse.bin
124 bytes is the total size of ESP32 eFuse blocks.
Note
Specifying eFuse storage is mandatory to test out any platform security features like “Secure Boot” or “Flash Encryption”.
Emulating ESP32 ECO3
For the application to detect the emulated chip as ESP32 ECO3, the following virtual efuses must be set:
- CHIP_VER_REV1
- CHIP_VER_REV2
Here is the corresponding efuse file (in hexadecimal, produced using xxd -p
):
000000000000000000000000008000000000000000001000000000000000
000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000
00000000
To convert this (efuse.hex
) back to binary, run xxd -r -p efuse.hex qemu_efuse.bin
.
Alternatively, these bits can be set using espefuse:
espefuse.py --port=socket://localhost:5555 burn_efuse CHIP_VER_REV1
espefuse.py --port=socket://localhost:5555 burn_efuse CHIP_VER_REV2
Disabling the watchdogs
By default, Timer Group watchdog timers are emulated, and TG0 WDT is enabled at reset. It is sometimes useful to disable these watchdog timers. This can be done by adding the following to the command line:
-global driver=timer.esp32.timg,property=wdt_disable,value=true
This disables the emulation of TG watchdog timers. Even if the application configures them, they will not fire.
The RTC watchdog timer is not emulated yet, so it doesn’t need to be disabled.
Using esptool.py and espefuse.py to interact with QEMU
-
Start QEMU:
build/qemu-system-xtensa -nographic \-machine esp32 \-drive file=flash_image.bin,if=mtd,format=raw \-global driver=esp32.gpio,property=strap_mode,value=0x0f \-drive file=qemu_efuse.bin,if=none,format=raw,id=efuse \-global driver=nvram.esp32.efuse,property=drive,value=efuse \-serial tcp::5555,server,nowait
The final line redirects the emulated UART to TCP port 5555 (QEMU acts as a server).
Type q and press Enter at any time to quit.
-
Run esptool.py:
esptool.py -p socket://localhost:5555 flash_id
Flashing with
idf.py
also works:export ESPPORT=socket://localhost:5555 idf.py flash
-
Or, run espefuse.py:
espefuse.py --port socket://localhost:5555 --do-not-confirm burn_custom_mac 00:11:22:33:44:55
Note: esptool
can not reset the emulated chip using the RTS signal, because the state of RTS is not transmitted over TCP to QEMU. To reset the emulated chip, run system_reset
command in QEMU console (started at step 1).
Specifying ROM ELF file
If -kernel
and -bios
arguments are not given, ESP32 (rev. 3) ROM code will be loaded. This ROM code binary is included in the repository. To specify the ROM code ELF file to load, pass the filename with a -bios <filename>
argument.
Using flash encryption
Self-encryption workflow
- In the IDF application, enable
CONFIG_SECURE_FLASH_ENC_ENABLED
throughmenuconfig
, and build it - Build the flash image as per the instructions from the Compiling the ESP-IDF program to emulate section.
- Create
qemu_efuse.bin
as highlighted in the Specifying eFuse storage section. - Execute
qemu-system-xtensa
using the following command:build/qemu-system-xtensa -nographic -machine esp32 \-drive file=/path/to/qemu_efuse.bin,if=none,format=raw,id=efuse \-global driver=nvram.esp32.efuse,property=drive,value=efuse \-drive file=/path/to/flash_image.bin,if=mtd,format=raw \-global driver=timer.esp32.timg,property=wdt_disable,value=true
Adding PSRAM
QEMU “memory size” option can be used to enable PSRAM emulation. By default, no PSRAM is added to the machine. You can add 2MB or 4MB PSRAM using -m 2M
or -m 4M
command line options, respectively.
Note that PSRAM MMU is not emulated yet, so things like bank switching (himem
in IDF) do not work.
Using SD cards
QEMU emulates SD/MMC host controller used in ESP32. To add an SD card to the system, create an image and pass it to QEMU.
-
Create a raw image file, for example, 64 MB:
$ dd if=/dev/zero bs=$((1024*1024)) count=64 of=sd_image.bin
-
Add the following argument when running QEMU:
-drive file=sd_image.bin,if=sd,format=raw
If you need to create a large SD card image, it is recommended to use sparse cqow2
images instead of raw ones. Consult QEMU manual about qemu-img
tool for details.
Only one SD card is supported at a time. You can use either slot 0 or slot 1 of the SD/MMC controller in the application code.
Enabling graphical user interface (GUI)
The ESP32 QEMU implementation implements a virtual RGB panel, absent on the real hardware, that can be used to show graphical interface. It is associated to a virtual frame buffer that can be used to populate the pixels to show. It is also possible to use the target internal RAM as a frame buffer.
To enable the graphical interface, while keeping the serial output in the console, use the following command line:
build/qemu-system-xtensa \-machine esp32 \-drive file=flash_image.bin,if=mtd,format=raw-display sdl \-serial stdio
If gtk
backend was enabled when compiling QEMU, it is possible to replace -display sdl
with -display gtk
相关文章:
【物联网设备端开发】ESP开发工具:QEMU的使用方法
概要 本文提供了一些运行QEMU的ESP特定说明。有关QEMU的一般使用问题,请参阅官方文档:https://www.qemu.org/documentation/. 编译 QEMU 准备工作 在此之前,请查看有关构建先决条件的QEMU文档。如果你在Linux主机上构建QEMU,你…...
c++中std::endl 和“\n“ 这两个换行符有什么区别
std::endl 和 "\n" 都用于在C中生成换行符,但它们之间有一些重要的区别 std::endl: 功能:输出一个换行符,并刷新输出流(即缓冲区)。作用:确保所有数据立即输出到目的地,例…...

http中get和post怎么选
5.4.2.怎么选择1.如果你是想从服务器上获取资源,建议使用GET请求,如果你这个请求是为了向服务器提交数据,建议使用POST请求。2.大部分的form表单提交,都是post方式,因为form表单中要填写大量的数据,这些数据…...

数据分析及应用:快手直播间人员在线分析
目录 0 需求描述 1、进入直播间的高峰期为?(以进入用户数衡量) 2、晚上 11 点,哪个直播间的进入人数最多? 3、20:00-23:00,娱乐类、搞笑类,进入人数最多直播间分别是? 4、娱乐类、搞笑类,人均在线时长(退出时间-进入时间)最长的直播间分别是? 5、同时在线人数…...

【Python】nn.nn.CircularPad1、2、3d函数和nn.ConstantPad1、2、3d函数详解和示例
前言 在深度学习中,尤其是在处理图像、音频或其他多维数据时,数据填充(Padding)是一个常见的操作。填充不仅可以保持数据的空间维度,还能在卷积操作中避免信息丢失。PyTorch提供了多种填充方式,其中nn.Cir…...

LearnOpenGL——混合、面剔除
LearnOpenGL——混合、面剔除 混合 Blending一、丢弃片段 Alpha Test二、混合 Alpha Blending渲染顺序 面剔除一、环绕顺序二、面剔除 混合 Blending OpenGL中,混合(Blending)通常是实现物体透明度(Transparency)的一种技术。透明的物体可以是完全透明的࿰…...
视频网站为何热衷于SCDN
视频网站为何热衷于SCDN?随着互联网技术的飞速发展,视频网站已成为人们日常生活中不可或缺的一部分。无论是观看高清电影、热门剧集,还是直播体育赛事、游戏竞技,视频网站都以其丰富的内容和便捷的访问方式吸引了无数用户。然而&a…...

Redis与DataBase保持数据一致性
文章目录 1. 读取数据2. 写数据2.1 先操作缓存2.2 先操作数据库 在我们系统中缓存最常用的策略是:服务端需要同时维系DB和Cache,并且是以DB的结果为准, Cache-Aside Pattern(缓存分离模式、旁路缓存)。 1. 读取数据 当…...

解决 MacOS 连接公司 VPN 成功但是不能网络的问题
目录 解决办法2024 Mac mini 爆料 解决办法 操作比较简单,修改配置文件即可(如果没有则需要手动创建)。 sudo vim /etc/ppp/options在此文件下,加入 plugin L2TP.ppp: plugin L2TP.ppp如果文件里有l2tpnoipsec&…...

【Kubernetes】k8s集群之Pod容器资源限制和三种探针
目录 一、Pod容器的资源限制 1.资源限制 2.Pod 和容器的资源请求与限制 3.CPU 资源单位 4.内存资源单位 二、Pod容器的三种探针 1.探针的三种规则 2.Probe支持三种检查方法: 一、Pod容器的资源限制 1.资源限制 当定义 Pod 时可以选择性地为每个容器设定所…...

从古代驿站体系看软件安全管控@安全历史04
在古代,车、马都很慢,信息传递很不顺畅,中央的政令又是如何传达至地方的呢?实际上,很多朝代都有专门的驿站制度,可以保障全国各地的信息传递,对于维护统治和稳定有着关键作用。 若将国家比作一个…...

8.8 哈希表简单 1 Two Sum 141 Linked List Cycle
1 Two Sum class Solution { public:vector<int> twoSum(vector<int>& nums, int target) {//给的target是目标sum 要返回vector<int> res(2,0);是在num中找加数//首先假设每个输入都是由唯一的结果,而且不适用相同的元素两次一共有n*(n-1)种…...

动态规划之——背包DP(完结篇)
文章目录 概要说明分组背包模板例题1思路code模板例题2思路code 有依赖的背包问题模板例题思路code 背包问题求方案数模板例题思路code 背包问题求具体方案模板例题思路code 概要说明 本文讲分组背包、有依赖的背包、 背包问题求方案数以及背包问题求具体方案 入门篇(01背包和…...

Advanced IP Scanner - 网络扫描工具介绍
Advanced IP Scanner 是一款免费、快速且用户友好的网络扫描工具。它能够帮助用户扫描局域网(LAN)中的所有设备,提供详细的设备信息,包括IP地址、MAC地址、设备名称和厂商信息。该工具对IT管理员和普通用户都非常有用,…...

数据库事务的四大特性ACID
数据库事务的四大特性ACID 数据库事务(Transaction)是数据库管理系统(DBMS)执行过程中的一个逻辑单位,由一个或多个SQL语句组成,这些语句作为一个整体一起向系统提交,要么全部执行,…...

ELK架构介绍
一、ELK简介 ELK 是由三个开源软件组成的,分别是:Elasticsearch、Logstash和Kibana,这三个软件各自在日志管理和数据分析领域发挥着重要作用。Elasticsearch提供分布式存储和搜索能力;Logstash负责数据收集和处理,而K…...

Vscode下ESP32工程函数定义无法跳转
1.删除.vscode 2.按下 ctrlshiftp,搜索 ESP-IDF:Add vscode Configuration Folder...

liquibase.exception.LockException: Could not acquire change log lock.
项目场景: 启动应用花了好长时间,最后报出异常. 问题描述 启动应用花了好长时间,最后报出异常. 异常: Caused by: liquibase.exception.LockException: Could not acquire change log lock. Currently locked by LAPTOP-OQ9VB…...

【多线程-从零开始-捌】阻塞队列,消费者生产者模型
什么是阻塞队列 阻塞队里是在普通的队列(先进先出队列)基础上,做出了扩充 线程安全 标准库中原有的队列 Queue 和其子类,默认都是线程不安全的 具有阻塞特性 如果队列为空,进行出队列操作,此时就会出现阻…...

数据结构——栈(Stack)
目录 前言 一、栈的概念 1、栈的基本定义 2、栈的特性 二、栈的基本操作 1.相关操作概念 2.实现方式 (1)顺序栈 (2)链式栈 三、栈的应用 总结 前言 栈(Stack)是一种常见且重要的数据结构,它遵循…...
Ubuntu系统下交叉编译openssl
一、参考资料 OpenSSL&&libcurl库的交叉编译 - hesetone - 博客园 二、准备工作 1. 编译环境 宿主机:Ubuntu 20.04.6 LTSHost:ARM32位交叉编译器:arm-linux-gnueabihf-gcc-11.1.0 2. 设置交叉编译工具链 在交叉编译之前&#x…...

【kafka】Golang实现分布式Masscan任务调度系统
要求: 输出两个程序,一个命令行程序(命令行参数用flag)和一个服务端程序。 命令行程序支持通过命令行参数配置下发IP或IP段、端口、扫描带宽,然后将消息推送到kafka里面。 服务端程序: 从kafka消费者接收…...
在HarmonyOS ArkTS ArkUI-X 5.0及以上版本中,手势开发全攻略:
在 HarmonyOS 应用开发中,手势交互是连接用户与设备的核心纽带。ArkTS 框架提供了丰富的手势处理能力,既支持点击、长按、拖拽等基础单一手势的精细控制,也能通过多种绑定策略解决父子组件的手势竞争问题。本文将结合官方开发文档,…...

Mybatis逆向工程,动态创建实体类、条件扩展类、Mapper接口、Mapper.xml映射文件
今天呢,博主的学习进度也是步入了Java Mybatis 框架,目前正在逐步杨帆旗航。 那么接下来就给大家出一期有关 Mybatis 逆向工程的教学,希望能对大家有所帮助,也特别欢迎大家指点不足之处,小生很乐意接受正确的建议&…...
【磁盘】每天掌握一个Linux命令 - iostat
目录 【磁盘】每天掌握一个Linux命令 - iostat工具概述安装方式核心功能基础用法进阶操作实战案例面试题场景生产场景 注意事项 【磁盘】每天掌握一个Linux命令 - iostat 工具概述 iostat(I/O Statistics)是Linux系统下用于监视系统输入输出设备和CPU使…...
Java - Mysql数据类型对应
Mysql数据类型java数据类型备注整型INT/INTEGERint / java.lang.Integer–BIGINTlong/java.lang.Long–––浮点型FLOATfloat/java.lang.FloatDOUBLEdouble/java.lang.Double–DECIMAL/NUMERICjava.math.BigDecimal字符串型CHARjava.lang.String固定长度字符串VARCHARjava.lang…...
浅谈不同二分算法的查找情况
二分算法原理比较简单,但是实际的算法模板却有很多,这一切都源于二分查找问题中的复杂情况和二分算法的边界处理,以下是博主对一些二分算法查找的情况分析。 需要说明的是,以下二分算法都是基于有序序列为升序有序的情况…...

零基础在实践中学习网络安全-皮卡丘靶场(第九期-Unsafe Fileupload模块)(yakit方式)
本期内容并不是很难,相信大家会学的很愉快,当然对于有后端基础的朋友来说,本期内容更加容易了解,当然没有基础的也别担心,本期内容会详细解释有关内容 本期用到的软件:yakit(因为经过之前好多期…...
在QWebEngineView上实现鼠标、触摸等事件捕获的解决方案
这个问题我看其他博主也写了,要么要会员、要么写的乱七八糟。这里我整理一下,把问题说清楚并且给出代码,拿去用就行,照着葫芦画瓢。 问题 在继承QWebEngineView后,重写mousePressEvent或event函数无法捕获鼠标按下事…...
JS设计模式(4):观察者模式
JS设计模式(4):观察者模式 一、引入 在开发中,我们经常会遇到这样的场景:一个对象的状态变化需要自动通知其他对象,比如: 电商平台中,商品库存变化时需要通知所有订阅该商品的用户;新闻网站中࿰…...