EttusResearch / uhd

The USRP™ Hardware Driver Repository
http://uhd.ettus.com
Other
942 stars 644 forks source link

Building X300 ZPU from source unable to generate COE file #760

Open nmolinski opened 3 weeks ago

nmolinski commented 3 weeks ago

Issue Description

When building from source the X300 reference firmware with the ZPU (via zpugcc) the final make step runs bin_to_coe.py

While running make the python file fails to read the binary file properly since the 'rb' flag isn't specified.

#7 27.18 [ 95%] Generating x300_main.rom
#7 27.30 [100%] Generating x300_main.coe
#7 27.44 Traceback (most recent call last):
#7 27.44   File "/tmp/uhd/firmware/usrp3/utils/bin_to_coe.py", line 11, in <module>
#7 27.45     h = binascii.hexlify(open(bin_file).read()) + '0'*7
#7 27.45   File "/usr/lib/python3.10/codecs.py", line 322, in decode
#7 27.45     (result, consumed) = self._buffer_decode(data, self.errors, final)
#7 27.46 UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 2: invalid start byte
#7 27.50 make[2]: *** [x300/CMakeFiles/x300.dir/build.make:82: x300/x300_main.coe] Error 1
#7 27.50 make[1]: *** [CMakeFiles/Makefile2:170: x300/CMakeFiles/x300.dir/all] Error 2
#7 27.50 make: *** [Makefile:136: all] Error 2

Setup Details

Here is a dockerfile to run this test

docker build --no-cache --platform=linux/amd64 .

FROM ubuntu:22.04

ENV DEBIAN_FRONTEND=noninteractive

RUN apt update && apt install -y  \
  cmake                           \
  xz-utils                        \
  bsdmainutils                    \
  python3                         \
  git

WORKDIR /tmp

RUN git clone --depth 1 https://github.com/zylin/zpugcc.git                                              \
  && tar -xvJf  /tmp/zpugcc/releases/20150428/zpu-gcc-g++-multilib-linux64-20150428.tar.xz -C /usr/local \
  && export PATH="/usr/local/zpu-gcc-g++-multilib-linux64-20150428/bin:$PATH"                            \
  && git clone --depth 1 https://github.com/EttusResearch/uhd.git                                        \
  && cd /tmp/uhd/firmware/usrp3                                                                          \
  && mkdir build                                                                                         \
  && cd build                                                                                            \
  && cmake ..                                                                                            \
  && make

Note: I am running this on Apple Silicon, but the bug is irrespective of that.

Expected Behavior

the COE file generation to complete properly so my pipeline can pass. The binary file needs to be read and formatted into the COE format.

Actual Behaviour

'utf-8' codec can't decode byte The file is being read as ascii instead of binary ('rb'), which causes failure due to non-utf-8 characters in binary file

Steps to reproduce the problem

Run the provided docker file above docker build --no-cache --platform=linux/amd64 .

Additional Information

Suggestions for fixes of firmware/usrp3/utils/bin_to_coe.py

  1. specify read binary flags to read the .bin file.
h = binascii.hexlify(open(bin_file, 'rb').read()) + b'0'*7 
  1. cast result of len(h)/8 to int (causes range error due to float specified in range)
out.write(',\n'.join([h[i*8:(i+1)*8].decode('ascii') for i in range(int(len(h)/8))]) + ';')
  1. Delete the line for variable d assignment, its not used

d = [h[i*8:(i+1)*8] for i in range(len(h)/8)]

mbr0wn commented 2 weeks ago

Thanks for reporting!