From 04b77f56f54557929e7e61cbbc67c31a08727c34 Mon Sep 17 00:00:00 2001 From: Anton Maklakov Date: Mon, 18 Dec 2023 20:01:27 +0700 Subject: [PATCH] fix(idf_tools): Fix platform detection for arm64 machine but arm32 environment --- tools/idf_tools.py | 55 +++++++++++++----- .../platform_detection/arm32_header.elf | Bin 0 -> 52 bytes .../platform_detection/arm64_header.elf | Bin 0 -> 64 bytes .../platform_detection/armhf_header.elf | Bin 0 -> 52 bytes tools/test_idf_tools/test_idf_tools.py | 27 ++++++++- 5 files changed, 65 insertions(+), 17 deletions(-) create mode 100644 tools/test_idf_tools/platform_detection/arm32_header.elf create mode 100644 tools/test_idf_tools/platform_detection/arm64_header.elf create mode 100644 tools/test_idf_tools/platform_detection/armhf_header.elf diff --git a/tools/idf_tools.py b/tools/idf_tools.py index 47be5539a5..5fb6e130d4 100755 --- a/tools/idf_tools.py +++ b/tools/idf_tools.py @@ -165,6 +165,42 @@ class Platforms: 'Linux-arm': PLATFORM_LINUX_ARM32, } + @staticmethod + def detect_linux_arm_platform(supposed_platform): # type: (Optional[str]) -> Optional[str] + """ + We probe the python binary to check exactly what environment the script is running in. + + ARM platform may run on armhf hardware but having armel installed packages. + To avoid possible armel/armhf libraries mixing need to define user's + packages architecture to use the same + See note section in https://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html#index-mfloat-abi + + ARM platform may run on aarch64 hardware but having armhf installed packages + (it happens if a docker container is running on arm64 hardware, but using an armhf image). + + """ + if supposed_platform not in (PLATFORM_LINUX_ARM32, PLATFORM_LINUX_ARMHF, PLATFORM_LINUX_ARM64): + return supposed_platform + + # suppose that installed python was built with the right ABI + with open(sys.executable, 'rb') as f: + # see ELF header description in https://man7.org/linux/man-pages/man5/elf.5.html, offsets depend on ElfN size + if int.from_bytes(f.read(4), sys.byteorder) != int.from_bytes(b'\x7fELF', sys.byteorder): + return supposed_platform # ELF magic not found. Use the default platform name from PLATFORM_FROM_NAME + f.seek(18) # seek to e_machine + e_machine = int.from_bytes(f.read(2), sys.byteorder) + if e_machine == 183: # EM_AARCH64, https://github.com/ARM-software/abi-aa/blob/main/aaelf64/aaelf64.rst + supposed_platform = PLATFORM_LINUX_ARM64 + elif e_machine == 40: # EM_ARM, https://github.com/ARM-software/abi-aa/blob/main/aaelf32/aaelf32.rst + f.seek(36) # seek to e_flags + e_flags = int.from_bytes(f.read(4), sys.byteorder) + if e_flags & 0x400: + supposed_platform = PLATFORM_LINUX_ARMHF + else: + supposed_platform = PLATFORM_LINUX_ARM32 + + return supposed_platform + @staticmethod def get(platform_alias): # type: (Optional[str]) -> Optional[str] if platform_alias is None: @@ -172,21 +208,8 @@ class Platforms: if platform_alias == 'any' and CURRENT_PLATFORM: platform_alias = CURRENT_PLATFORM - platform_name = Platforms.PLATFORM_FROM_NAME.get(platform_alias, None) - - # ARM platform may run on armhf hardware but having armel installed packages. - # To avoid possible armel/armhf libraries mixing need to define user's - # packages architecture to use the same - # See note section in https://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html#index-mfloat-abi - if platform_name in (PLATFORM_LINUX_ARM32, PLATFORM_LINUX_ARMHF) and 'arm' in platform.machine(): - # suppose that installed python was built with a right ABI - with open(sys.executable, 'rb') as f: - if int.from_bytes(f.read(4), sys.byteorder) != int.from_bytes(b'\x7fELF', sys.byteorder): - return platform_name # ELF magic not found. Use default platform name from PLATFORM_FROM_NAME - f.seek(36) # seek to e_flags (https://man7.org/linux/man-pages/man5/elf.5.html) - e_flags = int.from_bytes(f.read(4), sys.byteorder) - platform_name = PLATFORM_LINUX_ARMHF if e_flags & 0x400 else PLATFORM_LINUX_ARM32 + platform_name = Platforms.detect_linux_arm_platform(platform_name) return platform_name @staticmethod @@ -1389,7 +1412,7 @@ def get_idf_version() -> str: """ idf_version: Optional[str] = None - version_file_path = os.path.join(global_idf_path, 'version.txt') + version_file_path = os.path.join(global_idf_path or '', 'version.txt') if os.path.exists(version_file_path): with open(version_file_path, 'r') as version_file: idf_version_str = version_file.read() @@ -1400,7 +1423,7 @@ def get_idf_version() -> str: if idf_version is None: try: - with open(os.path.join(global_idf_path, 'components', 'esp_common', 'include', 'esp_idf_version.h')) as f: + with open(os.path.join(global_idf_path or '', 'components', 'esp_common', 'include', 'esp_idf_version.h')) as f: m = re.search(r'^#define\s+ESP_IDF_VERSION_MAJOR\s+(\d+).+?^#define\s+ESP_IDF_VERSION_MINOR\s+(\d+)', f.read(), re.DOTALL | re.MULTILINE) if m: diff --git a/tools/test_idf_tools/platform_detection/arm32_header.elf b/tools/test_idf_tools/platform_detection/arm32_header.elf new file mode 100644 index 0000000000000000000000000000000000000000..d1ac728eac59a262414d8ddc09fc5238dc7a66a3 GIT binary patch literal 52 zcmb<-^>JflWMpQ50%isc21W)3h8vx13?>W=3>;TaF)%POu$nL^FmN$wFvv5=F#rIY CCIyuM literal 0 HcmV?d00001 diff --git a/tools/test_idf_tools/platform_detection/arm64_header.elf b/tools/test_idf_tools/platform_detection/arm64_header.elf new file mode 100644 index 0000000000000000000000000000000000000000..44e5808390e17a1dc782b60b6c9b182a46f064b4 GIT binary patch literal 64 zcmb<-^>JfjWMpQ50%nHo42%p63|E%(f<+v_Bm={NeYc@31_lQP3kEI*2L^ctIR*gv CVh1?@ literal 0 HcmV?d00001 diff --git a/tools/test_idf_tools/platform_detection/armhf_header.elf b/tools/test_idf_tools/platform_detection/armhf_header.elf new file mode 100644 index 0000000000000000000000000000000000000000..7531b0538db4c0f2ce0ad04a265ba700fe3935ad GIT binary patch literal 52 zcmb<-^>JflWMpQ50%isc21W)3hMy&D3?>W=3}3!