-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add support for efuse in qemu #291
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import asyncio | ||
import binascii | ||
import logging | ||
import os | ||
import shlex | ||
import socket | ||
|
@@ -12,6 +14,43 @@ | |
if t.TYPE_CHECKING: | ||
from .app import QemuApp | ||
|
||
QEMU_DEFAULT_EFUSE = { | ||
'esp32': binascii.unhexlify( | ||
'00000000000000000000000000800000000000000000100000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000' | ||
), | ||
'esp32c3': binascii.unhexlify( | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000c00' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
'000000000000000000000000000000000000000000000000' | ||
), | ||
} | ||
|
||
|
||
class Qemu(DuplicateStdoutPopen): | ||
""" | ||
|
@@ -37,6 +76,7 @@ def __init__( | |
qemu_prog_path: t.Optional[str] = None, | ||
qemu_cli_args: t.Optional[str] = None, | ||
qemu_extra_args: t.Optional[str] = None, | ||
qemu_efuse_path: t.Optional[str] = None, | ||
app: t.Optional['QemuApp'] = None, | ||
**kwargs, | ||
): | ||
|
@@ -55,11 +95,28 @@ def __init__( | |
|
||
qemu_prog_path = qemu_prog_path or self.qemu_prog_name | ||
|
||
self.current_qemu_executable_path = qemu_prog_path | ||
self.image_path = image_path | ||
self.efuse_path = qemu_efuse_path | ||
|
||
if qemu_cli_args: | ||
qemu_cli_args = qemu_cli_args.strip('"').strip("'") | ||
qemu_cli_args = shlex.split(qemu_cli_args or self.qemu_default_args) | ||
qemu_extra_args = shlex.split(qemu_extra_args or '') | ||
|
||
if self.efuse_path: | ||
logging.debug('The eFuse file will be saved to: %s', self.efuse_path) | ||
with open(self.efuse_path, 'wb') as f: | ||
f.write(QEMU_DEFAULT_EFUSE[self.app.target]) | ||
qemu_extra_args += [ | ||
'-global', | ||
f'driver={self.app.target}.gpio,property=strap_mode,value=0x08', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
'-drive', | ||
f'file={self.efuse_path},if=none,format=raw,id=efuse', | ||
'-global', | ||
f'driver=nvram.{self.app.target}.efuse,property=drive,value=efuse', | ||
] | ||
|
||
self.qmp_addr = None | ||
self.qmp_port = None | ||
|
||
|
@@ -87,6 +144,47 @@ def __init__( | |
**kwargs, | ||
) | ||
|
||
def execute_efuse_command(self, command: str): | ||
import espefuse | ||
import pexpect | ||
|
||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | ||
s.bind(('127.0.0.1', 0)) | ||
_, available_port = s.getsockname() | ||
|
||
run_qemu_command = [ | ||
'-nographic', | ||
'-machine', | ||
self.app.target, | ||
'-drive', | ||
f'file={self.image_path},if=mtd,format=raw', | ||
'-global', | ||
f'driver={self.app.target}.gpio,property=strap_mode,value=0x0f', | ||
'-drive', | ||
f'file={self.efuse_path},if=none,format=raw,id=efuse', | ||
'-global', | ||
f'driver=nvram.{self.app.target}.efuse,property=drive,value=efuse', | ||
'-serial', | ||
f'tcp::{available_port},server,nowait', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When launching QEMU and expecting that it should communicate with another application, it is better to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, now that I'm thinking of this... don't you end up with two QEMU processes running at the same time, and accessing the same eFuse file?.. One QEMU process is already launched when |
||
] | ||
try: | ||
child = pexpect.spawn(self.current_qemu_executable_path, run_qemu_command) | ||
res = shlex.split(command) | ||
child.expect('qemu') | ||
|
||
res = [r for r in res if r != '--do-not-confirm'] | ||
espefuse.main([ | ||
'--port', | ||
f'socket://localhost:{available_port}', | ||
'--before', | ||
'no_reset', | ||
'--do-not-confirm', | ||
*res, | ||
]) | ||
self._hard_reset() | ||
finally: | ||
child.terminate() | ||
|
||
@property | ||
def qemu_prog_name(self): | ||
if self.app: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also support ESP32-S3 now, please add it as well.