superna9999 / pyamlboot

Amlogic USB Boot Protocol Library
https://pypi.org/project/pyamlboot/
Apache License 2.0
71 stars 27 forks source link

Not working on the latest Arch Linux (Python 3.12)? #24

Closed RadxaYuntian closed 2 months ago

RadxaYuntian commented 2 months ago

Just cloned the repo and tried boot-g12.py:

❯ sudo ./boot-g12.py ../bsp/.root/usr/lib/u-boot/radxa-zero/u-boot.bin
/home/excalibur/Documents/GitHub/pyamlboot/./boot-g12.py:8: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
  import pkg_resources
Firmware Version :
ROM: 3.2 Stage: 0.0
Need Password: 0 Password OK: 1
Writing ../bsp/.root/usr/lib/u-boot/radxa-zero/u-boot.bin at 0xfffa0000...
Traceback (most recent call last):
  File "/home/excalibur/Documents/GitHub/pyamlboot/./boot-g12.py", line 55, in <module>
    dev.writeLargeMemory(0xfffa0000, data[0:0x10000], 4096)
  File "/home/excalibur/Documents/GitHub/pyamlboot/pyamlboot/pyamlboot.py", line 229, in writeLargeMemory
    self._writeLargeMemory(address+offset, data[offset:offset+writeLength], \
  File "/home/excalibur/Documents/GitHub/pyamlboot/pyamlboot/pyamlboot.py", line 210, in _writeLargeMemory
    ep.write(data[offset:offset+blockLength], 1000)
  File "/usr/lib/python3.12/site-packages/usb/core.py", line 408, in write
    return self.device.write(self, data, timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/usb/core.py", line 989, in write
    return fn(
           ^^^
  File "/usr/lib/python3.12/site-packages/usb/backend/libusb1.py", line 837, in bulk_write
    return self.__write(self.lib.libusb_bulk_transfer,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/usb/backend/libusb1.py", line 938, in __write
    _check(retval)
  File "/usr/lib/python3.12/site-packages/usb/backend/libusb1.py", line 602, in _check
    raise USBTimeoutError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBTimeoutError: [Errno 110] Operation timed out

pyamlboot on  master via 🐍 v3.12.3 took 2s

Serial showed G12A:BL:0253b8:61aa2d;FEAT:E0F83180:2000;POC:F;RCY:0;EMMC:0;READ:0;CHK:1F;READ:0;CHK:1F;READ:0;CHK:1F;SD?:20000;USB:8; so nothing was getting to the device.


First, I reduced the block size for the failed function call back to the default 64 bytes:

diff --git a/boot-g12.py b/boot-g12.py
index 7c3b192..0594ee5 100755
--- a/boot-g12.py
+++ b/boot-g12.py
@@ -52,7 +52,7 @@ if __name__ == '__main__':
         data = f.read()

         print("Writing %s at 0x%x..." % (bpath, loadAddr))
-        dev.writeLargeMemory(0xfffa0000, data[0:0x10000], 4096)
+        dev.writeLargeMemory(0xfffa0000, data[0:0x10000])
         print("[DONE]")

         print("Running at 0x%x..." % loadAddr)

It went a bit further:

❯ sudo ./boot-g12.py ../bsp/.root/usr/lib/u-boot/radxa-zero/u-boot.bin
/home/excalibur/Documents/GitHub/pyamlboot/./boot-g12.py:8: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
  import pkg_resources
Firmware Version :
ROM: 3.2 Stage: 0.0
Need Password: 0 Password OK: 1
Writing ../bsp/.root/usr/lib/u-boot/radxa-zero/u-boot.bin at 0xfffa0000...
[DONE]
Running at 0xfffa0000...
[DONE]
Traceback (most recent call last):
  File "/home/excalibur/Documents/GitHub/pyamlboot/./boot-g12.py", line 67, in <module>
    (length, offset) = dev.getBootAMLC()
                       ^^^^^^^^^^^^^^^^^
  File "/home/excalibur/Documents/GitHub/pyamlboot/pyamlboot/pyamlboot.py", line 359, in getBootAMLC
    data = epin.read(AMLC_AMLS_BLOCK_LENGTH, 100)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/usb/core.py", line 423, in read
    return self.device.read(self, size_or_buffer, timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/usb/core.py", line 1029, in read
    ret = fn(
          ^^^
  File "/usr/lib/python3.12/site-packages/usb/backend/libusb1.py", line 846, in bulk_read
    return self.__read(self.lib.libusb_bulk_transfer,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/usb/backend/libusb1.py", line 954, in __read
    _check(retval)
  File "/usr/lib/python3.12/site-packages/usb/backend/libusb1.py", line 604, in _check
    raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 5] Input/Output Error

pyamlboot on  master [!] via 🐍 v3.12.3 took 2s

There is also some output in the device serial:

G12A:BL:0253b8:61aa2d;FEAT:E0F83180:2000;POC:F;RCY:0;EMMC:0;READ:0;CHK:1F;READ:0;CHK:1F;READ:0;CHK:1F;SD?:20000;USB:8;0.0
                                                                                                                         l2_stage_init 0x01
bl2_stage_init 0x81
hw id: 0x0001 - pwm id 0x00
bl2_stage_init 0xc0
bl2_stage_init 0x02

L0:00000000
L1:00000703
L2:00008067
L3:04000000
S1:00000000
B2:00002000
B1:e0f83180

TE: 278228867

BL2 Built : 15:21:48, Aug 28 2019. g12a g1bf2b53 - luan.yuan@droid15-sz

Board ID = 2
Set cpu clk to 24M
Set clk81 to 24M
CPU clk: 1200 MHz
Set clk81 to 166.6M
DDR driver_vesion: LPDDR4_PHY_V_0_1_18 build time: Aug 28 2019 15:21:45
board id: 2
Cfg max: 2, cur: 1. Board id: 255. Force loop cfg

I tried to play with block size (AMLC_AMLS_BLOCK_LENGTH, AMLC_MAX_TRANSFERT_LENGTH, and AMLC_MAX_BLOCK_LENGTH) for the above error to no avail. There is also another error that I'm getting:

❯ sudo ./boot-g12.py ../bsp/.root/usr/lib/u-boot/radxa-zero/u-boot.bin
/home/excalibur/Documents/GitHub/pyamlboot/./boot-g12.py:8: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
  import pkg_resources
Firmware Version :
ROM: 3.2 Stage: 0.0
Need Password: 0 Password OK: 1
Writing ../bsp/.root/usr/lib/u-boot/radxa-zero/u-boot.bin at 0xfffa0000...
[DONE]
Running at 0xfffa0000...
[DONE]
AMLC dataSize=16384, offset=65536, seq=0...
Traceback (most recent call last):
  File "/home/excalibur/Documents/GitHub/pyamlboot/./boot-g12.py", line 77, in <module>
    dev.writeAMLCData(seq, offset, data[offset:offset+length])
  File "/home/excalibur/Documents/GitHub/pyamlboot/pyamlboot/pyamlboot.py", line 452, in writeAMLCData
    self._writeAMLCData(offset, data[offset:offset+writeLength])
  File "/home/excalibur/Documents/GitHub/pyamlboot/pyamlboot/pyamlboot.py", line 408, in _writeAMLCData
    epout.write(data[dataOffset:dataOffset+blockLength], 1000)
  File "/usr/lib/python3.12/site-packages/usb/core.py", line 408, in write
    return self.device.write(self, data, timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/usb/core.py", line 989, in write
    return fn(
           ^^^
  File "/usr/lib/python3.12/site-packages/usb/backend/libusb1.py", line 837, in bulk_write
    return self.__write(self.lib.libusb_bulk_transfer,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/usb/backend/libusb1.py", line 938, in __write
    _check(retval)
  File "/usr/lib/python3.12/site-packages/usb/backend/libusb1.py", line 602, in _check
    raise USBTimeoutError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBTimeoutError: [Errno 110] Operation timed out

pyamlboot on  master [!] via 🐍 v3.12.3 took 3s

So it is mostly dev.getBootAMLC and dev.writeAMLCData throwing errors on me now.

I'd like to check if this is my own system issue, or if this is an Arch issue.

RadxaYuntian commented 2 months ago

Posted this in our Discord as well.

superna9999 commented 2 months ago

Can you precise on which usb controller model you are running? Can you run lsusb -vv ?

RadxaYuntian commented 2 months ago

Upgraded the system today and the issue disappeared. The kernel was upgraded from 6.8.8 to 6.8.9 so maybe something was not right in the previous release.