Skip to content
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

Support connecting to ptys (not real serial ports) (ESPTOOL-493) #762

Closed
manimathma opened this issue Jul 8, 2022 · 5 comments
Closed

Comments

@manimathma
Copy link

When i try to use idf.py monitor/flash with serial pty it fails with ioctl error.

Start qemu using echr and strapmode

/tmp/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 -echr 0x02 -serial pts

then try to flash image using idf.py flash
idf.py flash -p /dev/pts/7

and flash fails with following message

Failed to get PID of a device on /dev/pts/7, using standard reset sequence.

Traceback (most recent call last):
  File "/home/amanimu/Walnut_gcov/esp-dpk/external/esp-idf/components/esptool_py/esptool/esptool.py", line 5347, in <module>
    _main()
  File "/home/amanimu/Walnut_gcov/esp-dpk/external/esp-idf/components/esptool_py/esptool/esptool.py", line 5340, in _main
    main()
  File "/home/amanimu/Walnut_gcov/esp-dpk/external/esp-idf/components/esptool_py/esptool/esptool.py", line 4649, in main
    before=args.before)
  File "/home/amanimu/Walnut_gcov/esp-dpk/external/esp-idf/components/esptool_py/esptool/esptool.py", line 115, in get_default_connected_device
    _esp.connect(before, connect_attempts)
  File "/home/amanimu/Walnut_gcov/esp-dpk/external/esp-idf/components/esptool_py/esptool/esptool.py", line 664, in connect
    last_error = self._connect_attempt(mode=mode, usb_jtag_serial=usb_jtag_serial, extra_delay=extra_delay)
  File "/home/amanimu/Walnut_gcov/esp-dpk/external/esp-idf/components/esptool_py/esptool/esptool.py", line 614, in _connect_attempt
    self.bootloader_reset(usb_jtag_serial, extra_delay)
  File "/home/amanimu/Walnut_gcov/esp-dpk/external/esp-idf/components/esptool_py/esptool/esptool.py", line 592, in bootloader_reset
    self._setDTR(False)  # IO0=HIGH
  File "/home/amanimu/Walnut_gcov/esp-dpk/external/esp-idf/components/esptool_py/esptool/esptool.py", line 532, in _setDTR
    self._port.setDTR(state)
  File "/home/amanimu/.espressif/python_env/idf4.3_py3.6_env/lib/python3.6/site-packages/serial/serialutil.py", line 603, in setDTR
    self.dtr = value
  File "/home/amanimu/.espressif/python_env/idf4.3_py3.6_env/lib/python3.6/site-packages/serial/serialutil.py", line 473, in dtr
    self._update_dtr_state()
  File "/home/amanimu/.espressif/python_env/idf4.3_py3.6_env/lib/python3.6/site-packages/serial/serialposix.py", line 715, in _update_dtr_state
    fcntl.ioctl(self.fd, TIOCMBIC, TIOCM_DTR_str)
OSError: [Errno 25] Inappropriate ioctl for device
CMake Error at run_serial_tool.cmake:50 (message):
  /home/amanimu/.espressif/python_env/idf4.3_py3.6_env/bin/python
  /home/amanimu/Walnut_gcov/esp-dpk/external/esp-idf/components/esptool_py/esptool/esptool.py
  --chip esp32 failed

We get similar error for idf.py monitor as well.

We are using idf 4.3

@igrr
Copy link
Member

igrr commented Jul 20, 2022

I think the issue here is that esptool.py (or rather, pyserial library used by esptool.py) cannot work with /dev/pts instead of a serial port.
You can use a TCP port, though, which is supported both in qemu and in pyserial. Please see https://github.com/espressif/qemu/wiki#using-esptoolpy-and-espefusepy-to-interact-with-qemu for the instructions.

@manimathma
Copy link
Author

@igrr Thanks for prompt feedback. We will look into the tcp option. It would be great if esptool could support pty. I was able to use latest pyserial module control pty. Not sure why esptool alone can't do it. Not a blocker or such. But good to have.

@igrr
Copy link
Member

igrr commented Jul 20, 2022

Probably this is because esptool expects to be able to reset the remote target using DTR and RTS handshaking lines, which are normally wired to GPIO0 and EN pins of the ESP. In case of the pty, an attempt to control DTR/RTS via the TIOCMBIC ioctl leads to an error.

esptool could ignore these errors (or disable DTR/RTS handling), but then the result would be no different from using a TCP port, where the handshaking lines (and thus resetting the remote target) aren't supported. So I don't see what pty support adds on top of the existing TCP socket support.

@manimathma
Copy link
Author

Our team already has an infra on top of ttys to automatically run test. With TCP port, we would have to make significant change to make our testing infra to make it work with simulator.

Sorry if this question is dumb, is there a way we could start simulator with both tcp port and pts.

I tried that, but only first serial option works and i couldn't use the second serial port itself.

If we have a way to have both, then for flashing i can use tcp port and for running test i could use pts.

igrr referenced this issue in espressif/qemu Aug 2, 2022
Include the qtest reproducer provided by Alexander Bulekov
in https://gitlab.com/qemu-project/qemu/-/issues/542.
Without the previous commit, we get:

  $ make check-qtest-i386
  ...
  Running test tests/qtest/intel-hda-test
  AddressSanitizer:DEADLYSIGNAL
  =================================================================
  ==1580408==ERROR: AddressSanitizer: stack-overflow on address 0x7ffc3d566fe0
      #0 0x63d297cf in address_space_translate_internal softmmu/physmem.c:356
      #1 0x63d27260 in flatview_do_translate softmmu/physmem.c:499:15
      #2 0x63d27af5 in flatview_translate softmmu/physmem.c:565:15
      #3 0x63d4ce84 in flatview_write softmmu/physmem.c:2850:10
      #4 0x63d4cb18 in address_space_write softmmu/physmem.c:2950:18
      #5 0x63d4d387 in address_space_rw softmmu/physmem.c:2960:16
      #6 0x62ae12f2 in dma_memory_rw_relaxed include/sysemu/dma.h:89:12
      #7 0x62ae104a in dma_memory_rw include/sysemu/dma.h:132:12
      #8 0x62ae6157 in dma_memory_write include/sysemu/dma.h:173:12
      #9 0x62ae5ec0 in stl_le_dma include/sysemu/dma.h:275:1
      #10 0x62ae5ba2 in stl_le_pci_dma include/hw/pci/pci.h:871:1
      #11 0x62ad59a6 in intel_hda_response hw/audio/intel-hda.c:372:12
      #12 0x62ad2afb in hda_codec_response hw/audio/intel-hda.c:107:5
      #13 0x62aec4e1 in hda_audio_command hw/audio/hda-codec.c:655:5
      #14 0x62ae05d9 in intel_hda_send_command hw/audio/intel-hda.c:307:5
      #15 0x62adff54 in intel_hda_corb_run hw/audio/intel-hda.c:342:9
      #16 0x62adc13b in intel_hda_set_corb_wp hw/audio/intel-hda.c:548:5
      #17 0x62ae5942 in intel_hda_reg_write hw/audio/intel-hda.c:977:9
      #18 0x62ada10a in intel_hda_mmio_write hw/audio/intel-hda.c:1054:5
      #19 0x63d8f383 in memory_region_write_accessor softmmu/memory.c:492:5
      #20 0x63d8ecc1 in access_with_adjusted_size softmmu/memory.c:554:18
      #21 0x63d8d5d6 in memory_region_dispatch_write softmmu/memory.c:1504:16
      #22 0x63d5e85e in flatview_write_continue softmmu/physmem.c:2812:23
      #23 0x63d4d05b in flatview_write softmmu/physmem.c:2854:12
      #24 0x63d4cb18 in address_space_write softmmu/physmem.c:2950:18
      #25 0x63d4d387 in address_space_rw softmmu/physmem.c:2960:16
      #26 0x62ae12f2 in dma_memory_rw_relaxed include/sysemu/dma.h:89:12
      #27 0x62ae104a in dma_memory_rw include/sysemu/dma.h:132:12
      #28 0x62ae6157 in dma_memory_write include/sysemu/dma.h:173:12
      #29 0x62ae5ec0 in stl_le_dma include/sysemu/dma.h:275:1
      #30 0x62ae5ba2 in stl_le_pci_dma include/hw/pci/pci.h:871:1
      #31 0x62ad59a6 in intel_hda_response hw/audio/intel-hda.c:372:12
      #32 0x62ad2afb in hda_codec_response hw/audio/intel-hda.c:107:5
      #33 0x62aec4e1 in hda_audio_command hw/audio/hda-codec.c:655:5
      #34 0x62ae05d9 in intel_hda_send_command hw/audio/intel-hda.c:307:5
      #35 0x62adff54 in intel_hda_corb_run hw/audio/intel-hda.c:342:9
      #36 0x62adc13b in intel_hda_set_corb_wp hw/audio/intel-hda.c:548:5
      #37 0x62ae5942 in intel_hda_reg_write hw/audio/intel-hda.c:977:9
      #38 0x62ada10a in intel_hda_mmio_write hw/audio/intel-hda.c:1054:5
      #39 0x63d8f383 in memory_region_write_accessor softmmu/memory.c:492:5
      #40 0x63d8ecc1 in access_with_adjusted_size softmmu/memory.c:554:18
      #41 0x63d8d5d6 in memory_region_dispatch_write softmmu/memory.c:1504:16
      #42 0x63d5e85e in flatview_write_continue softmmu/physmem.c:2812:23
      #43 0x63d4d05b in flatview_write softmmu/physmem.c:2854:12
      #44 0x63d4cb18 in address_space_write softmmu/physmem.c:2950:18
      #45 0x63d4d387 in address_space_rw softmmu/physmem.c:2960:16
      #46 0x62ae12f2 in dma_memory_rw_relaxed include/sysemu/dma.h:89:12
      qemu#47 0x62ae104a in dma_memory_rw include/sysemu/dma.h:132:12
      #48 0x62ae6157 in dma_memory_write include/sysemu/dma.h:173:12
      ...
  SUMMARY: AddressSanitizer: stack-overflow softmmu/physmem.c:356 in address_space_translate_internal
  ==1580408==ABORTING
  Broken pipe
  Aborted (core dumped)

Signed-off-by: Philippe Mathieu-Daudé <[email protected]>
Acked-by: Thomas Huth <[email protected]>
Message-Id: <[email protected]>
Signed-off-by: Thomas Huth <[email protected]>
@igrr igrr transferred this issue from espressif/qemu Aug 3, 2022
@igrr igrr changed the title Getting ioctl error while using serial pty Support connecting to ptys (not real serial ports) Aug 3, 2022
@github-actions github-actions bot changed the title Support connecting to ptys (not real serial ports) Support connecting to ptys (not real serial ports) (ESPTOOL-493) Aug 3, 2022
@igrr
Copy link
Member

igrr commented Aug 3, 2022

I've moved this issue into esptool repository, since the feature would have to be implemented here, not in QEMU.

The minimal implementation could be to catch "Inappropriate ioctl for device" error inside setDTR/setRTS functions. Or, detect that the port is a pty and warn/error out if hard reset (before or after flashing) is requested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants