-
Notifications
You must be signed in to change notification settings - Fork 36
feat: add support for efuse in qemu #291
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
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,9 +1,11 @@ | ||
| import asyncio | ||
| import binascii | ||
| import logging | ||
| import os | ||
| import shlex | ||
| import socket | ||
| import typing as t | ||
| from dataclasses import dataclass | ||
|
|
||
| from pytest_embedded.log import DuplicateStdoutPopen | ||
| from qemu.qmp import QMPClient | ||
|
|
@@ -14,6 +16,87 @@ | |
| from .app import QemuApp | ||
|
|
||
|
|
||
| @dataclass | ||
| class QemuTarget: | ||
| strap_mode: str | ||
| default_efuse: bytes | ||
|
|
||
|
|
||
| QEMU_TARGETS: dict[str, QemuTarget] = { | ||
| 'esp32': QemuTarget( | ||
| strap_mode='0x0F', | ||
| default_efuse=binascii.unhexlify( | ||
| '00000000000000000000000000800000000000000000100000000000000000000000000000000000' | ||
| '00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
| '00000000000000000000000000000000000000000000000000000000000000000000000000000000' | ||
| '00000000' | ||
| ), | ||
| ), | ||
| 'esp32c3': QemuTarget( | ||
| strap_mode='0x02', | ||
| default_efuse=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' | ||
| ), | ||
| ), | ||
| 'esp32s3': QemuTarget( | ||
| strap_mode='0x07', | ||
| default_efuse=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): | ||
| """ | ||
| QEMU class | ||
|
|
@@ -26,9 +109,8 @@ class Qemu(DuplicateStdoutPopen): | |
| QEMU_DEFAULT_ARGS = '-nographic -machine esp32' | ||
| QEMU_DEFAULT_FMT = '-nographic -machine {}' | ||
|
|
||
| QEMU_STRAP_MODE_FMT = '-global driver=esp32.gpio,property=strap_mode,value={}' | ||
| QEMU_STRAP_MODE_FMT = 'driver={}.gpio,property=strap_mode,value={}' | ||
| QEMU_SERIAL_TCP_FMT = '-serial tcp::{},server,nowait' | ||
|
|
||
| QEMU_DEFAULT_QMP_FMT = '-qmp tcp:127.0.0.1:{},server,wait=off' | ||
|
|
||
| def __init__( | ||
|
|
@@ -37,6 +119,7 @@ def __init__( | |
| qemu_prog_path: str | None = None, | ||
| qemu_cli_args: str | None = None, | ||
| qemu_extra_args: str | None = None, | ||
| qemu_efuse_path: str | None = None, | ||
| app: t.Optional['QemuApp'] = None, | ||
| **kwargs, | ||
| ): | ||
|
|
@@ -53,13 +136,28 @@ def __init__( | |
| if not os.path.exists(image_path): | ||
| raise ValueError(f"QEMU image path doesn't exist: {image_path}") | ||
|
|
||
| qemu_prog_path = qemu_prog_path or self.qemu_prog_name | ||
| self.qemu_prog_path = qemu_prog_path or self.qemu_prog_name | ||
| 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_TARGETS[self.app.target].default_efuse) | ||
| qemu_extra_args += [ | ||
| '-global', | ||
| self.QEMU_STRAP_MODE_FMT.format(self.app.target, QEMU_TARGETS[self.app.target].strap_mode), | ||
| '-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 | ||
|
|
||
|
|
@@ -83,10 +181,59 @@ def __init__( | |
| qemu_cli_args += shlex.split(self.QEMU_DEFAULT_QMP_FMT.format(self.qmp_port)) | ||
|
|
||
| super().__init__( | ||
| cmd=[qemu_prog_path, *qemu_cli_args, *qemu_extra_args, '-drive', f'file={image_path},if=mtd,format=raw'], | ||
| cmd=[ | ||
| self.qemu_prog_path, | ||
| *qemu_cli_args, | ||
| *qemu_extra_args, | ||
| '-drive', | ||
| f'file={image_path},if=mtd,format=raw', | ||
| ], | ||
| **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', | ||
| self.QEMU_STRAP_MODE_FMT.format(self.app.target, QEMU_TARGETS[self.app.target].strap_mode), | ||
| '-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', | ||
|
Member
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
Member
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
Contributor
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.
I think solution using run_qemu_command = [
'-daemonize',
'-display',
'none',
'-machine',
self.app.target,
'-drive',
f'file={self.image_path},if=mtd,format=raw',
'-global',
self.QEMU_STRAP_MODE_FMT.format(self.app.target, QEMU_TARGETS[self.app.target].strap_mode),
'-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',
]
try:
proc = subprocess.run(
[self.current_qemu_executable_path, *run_qemu_command],
check=True
)
res = shlex.split(command)
...But I suggest to keep the current implementation.
Contributor
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.
Yes, but the first process never writes to the eFuse file. All write operations are performed via TCP using |
||
| ] | ||
| try: | ||
| child = pexpect.spawn(self.qemu_prog_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: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.