| # Copyright 2023 The Pigweed Authors |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| # use this file except in compliance with the License. You may obtain a copy of |
| # the License at |
| # |
| # https://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| # License for the specific language governing permissions and limitations under |
| # the License. |
| """Checks for firmware files bundled into the gonk_firmware module.""" |
| |
| import importlib.resources |
| |
| FIRMWARE_PY_PACKAGE = 'gonk_firmware' |
| |
| BUNDLED_FPGA_BINFILE_NAME = 'fpga.bin' |
| BUNDLED_FPGA_BINFILE = '' |
| BUNDLED_ELF_NAME = 'gonk.elf' |
| BUNDLED_ELF = '' |
| BUNDLED_BIN_NAME = 'gonk.bin' |
| BUNDLED_BIN = '' |
| |
| # Check for a bundled Gonk firmware and FPGA bitstream file. |
| try: |
| with importlib.resources.as_file( |
| importlib.resources.files(FIRMWARE_PY_PACKAGE) |
| / BUNDLED_FPGA_BINFILE_NAME |
| ) as bin_path: |
| if bin_path.is_file(): |
| BUNDLED_FPGA_BINFILE = BUNDLED_FPGA_BINFILE_NAME |
| |
| with importlib.resources.as_file( |
| importlib.resources.files(FIRMWARE_PY_PACKAGE) / BUNDLED_ELF_NAME |
| ) as elf_path: |
| if elf_path.is_file(): |
| BUNDLED_ELF = BUNDLED_ELF_NAME |
| |
| with importlib.resources.as_file( |
| importlib.resources.files(FIRMWARE_PY_PACKAGE) / BUNDLED_BIN_NAME |
| ) as bin_path: |
| if bin_path.is_file(): |
| BUNDLED_BIN = BUNDLED_BIN_NAME |
| except ModuleNotFoundError: |
| pass |
| |
| |
| def bundled_bin_path(): |
| try: |
| return importlib.resources.files(FIRMWARE_PY_PACKAGE).joinpath( |
| BUNDLED_BIN |
| ) |
| except ModuleNotFoundError: |
| return None |
| |
| |
| def bundled_elf_path(): |
| try: |
| return importlib.resources.files(FIRMWARE_PY_PACKAGE).joinpath( |
| BUNDLED_ELF |
| ) |
| except ModuleNotFoundError: |
| return None |
| |
| |
| def bundled_fpga_binfile_path(): |
| try: |
| return importlib.resources.files(FIRMWARE_PY_PACKAGE).joinpath( |
| BUNDLED_FPGA_BINFILE |
| ) |
| except ModuleNotFoundError: |
| return None |