PYNQ SD Card image

This page will explain how SD card images can be built for PYNQ embedded platforms (Zynq, Zynq Ultrascale+, Zynq RFSoC). SD card images are not used with Alveo and other PCI-express connected platforms. For those datacenter platforms, only PYNQ software is installed on the host OS.

Note: the PYNQ images for supported boards are provided as precompiled downloadable SD card images and do not need rebuilt. The SD card build flow is only required to modify SD cards’ contents or target a new board.

Specifically, The SD card build flow will create the BOOT.bin, the u-boot bootloader, the Linux Device tree blob, the Linux kernel and the PYNQ-Linux root filesystem.

The source files for the PYNQ image flow build can be found here:

<PYNQ repository>/sdbuild

More details on configuring the root filesystem can be found in the README file in the folder above.

Prepare the Building Environment

It is recommended to use a Ubuntu OS to build the image. The currently supported Ubuntu OS are listed below:

Supported OS Code name
Ubuntu 18.04 Bionic
Ubuntu 20.04 Focal

Use Vagrant to prepare Ubuntu OS

If you do not have an Ubuntu enabled machine, you cam also prepare a Ubuntu virtual machine (VM) on your host OS . We provide in our repository a vagrant file that can help you install the Ubuntu VM on your host OS using the following steps:

  1. Download the vagrant software and Virtual Box. Install them on your host OS.

  2. In your host OS, open a terminal program. Locate your PYNQ repository, where the vagrant file is stored.

    cd <PYNQ repository>
    
  3. (optional) Depending on your Virtual Box configurations, you may need to run the following command first; it may help you get better screen resolution for your Ubuntu VM.

    vagrant plugin install vagrant-vbguest
    
  4. You can then prepare the VM using the following command. This step will prepare a Ubuntu VM called pynq_ubuntu_<version> on your Virtual Box. The Ubuntu packages on the VM will be updated during this process; the Ubuntu desktop will also be installed so you can install Xilinx software later.

    vagrant up
    

    The above command will take about 20 minutes to finish. By default our vagrant file will prepare a Ubuntu 20.04 OS. If you would like to use another OS, do:

    vagrant up <ubuntu_code_name>
    

    For example, you can do the following to prepare a Ubuntu 20.04 OS:

    vagrant up focal
    

    The supported OS and their corresponding code names are listed in the beginning of this section.

  5. In the end, you will see a Virtual Box window pop up with only shell terminal, asking for your Ubuntu login information. Ignore this window and close it. Run the following command on your host:

    vagrant reload <ubuntu_code_name>
    

    After running the above command, you will be asked to log onto your Ubuntu desktop. The username and password are both defaulted to vagrant. The current working directory on your host machine will be shared with /pynq on your VM. Always use vagrant reload command to reboot the VM; otherwise vagrant has no clue whether the VM has been rebooted, and users will not be able to see shared folder.

  6. (optional) You can enable bidirectional clipboard between your host and your VM in your Virtual Box settings:

    _images/bidirectional-clipboard.png
  7. Now you are ready to install Xilinx tools. You will need PetaLinux, Vivado, and Vitis for building PYNQ image. Do not install Xilinx tools into /pynq since it is only a small shared folder. Instead, a 160GB disk space will be allocated at /workspace folder in VM. Install Xilinx tools there.

    Starting from image v2.5, SDx is no longer needed. The version of Xilinx tools for each PYNQ release is shown below:

    Release version Xilinx Tool Version
    v1.4 2015.4
    v2.0 2016.1
    v2.1 2017.4
    v2.2 2017.4
    v2.3 2018.2
    v2.4 2018.3
    v2.5 2019.1
    v2.6 2020.1
    v2.7 2020.2
    v3.0 2022.1

Use an existing Ubuntu OS

If you already have a Ubuntu OS, and it is listed in the beginning of this section, you can simply do the following:

  1. Install dependencies using the following script. This is necessary if you are not using our vagrant file to prepare the environment.

    <PYNQ repository>/sdbuild/scripts/setup_host.sh
    
  2. Install correct version of the Xilinx tools, including PetaLinux, Vivado, and Vitis. See the above table for the correct version of each release.

Building the Image From Source

Once you have the build environment ready, you can build an SD card image following the steps below. You don’t have to rerun the setup_host.sh.

  1. Source the appropriate settings for PetaLinux and Vitis. Suppose you are using Xilinx 2022.1 tools:

    source <path-to-vitis>/Vitis/2022.1/settings64.sh
    source <path-to-petalinux>/petalinux-2022.1-final/settings.sh
    
  2. Depending on the overlays being rebuilt, make sure you have the appropriate Vivado licenses to build for your target board, especially the HDMI IP for the ZCU104 or the CMAC IP for the RFSoC4x2.

  3. Collect a prebuilt board-agnostic root filesystem tarball and a prebuilt PYNQ source distribution. Starting in PYNQ v3.0, by default the SD card build flow expects a prebuilt root filesystem and a PYNQ source distribution to speedup and simplify user rebuilds of SD card images. These binaries can be found at the PYNQ boards page and copied into the sdbuild prebuilt folder

    # For rebuilding all SD cards, both arm and aarch64 root filesystems
    # may be required depending on boards being targetted.
    cp pynq_rootfs.<arm|aarch64>.tar.gz <PYNQ repository>/sdbuild/prebuilt/pynq_rootfs.<arm|aarch64>.tar.gz
    cp pynq-<version>.tar.gz            <PYNQ repository>/sdbuild/prebuilt/pynq_sdist.tar.gz
    
  4. Navigate to the following directory and run make

    cd <PYNQ repository>/sdbuild/
    make
    

The build flow can take several hours and will rebuild SD cards for the Pynq-Z1, Pynq-Z2 and ZCU104 platforms.

Rebuilding the prebuilt board-agnostic image

In order to simplify and speed-up the image building process, users should re-use the prebuilt board-agnostic image appropriate to the architecture - arm for Zynq-7000 and aarch64 for Zynq UltraScale+, downloadable at the boards page of our website. This will allow you to completely skip the board-agnostic stage.

You can force a root filesystem build by setting the REBUILD_PYNQ_ROOTFS variable when invoking make:

cd <PYNQ repository>/sdbuild/
make REBUILD_PYNQ_ROOTFS=True BOARDS=<board>

Rebuilding the PYNQ source distribution tarball

To avoid rebuilding the PYNQ source distribution package, and consequently bypass the need to build bitstreams for the PYNQ-Z1, PYNQ-Z2 and the ZCU104, a prebuilt PYNQ sdist tarball should be reused as described in steps listed above.

You can force a PYNQ source distribution rebuild by setting the REBUILD_PYNQ_SDIST variable when invoking make

cd <PYNQ repository>/sdbuild/
make REBUILD_PYNQ_SDIST=True

Please also refer to the sdbuild readme on our GitHub repository for more info regarding the image-build flow.

Unmount images before building again

Sometimes the SD image building process can error out, leaving mounted images in your host OS. You need to unmount these images before trying the make process again. Starting from image v2.6, users can do the following to unmount the images.

cd <PYNQ repository>/sdbuild/
make delete

The above command not only unmounts all the images, but also deletes the failed images. This makes sure the users do not use the failed images when continuing the SD build process.

To unmount images but not delete them, use the following command instead.

cd <PYNQ repository>/sdbuild/
make unmount

If you want to ignore all the previous staged or cached SD build artifacts and start from scratch again, you can use the following command. This will unmount and delete the failed images, and remove all the previously built images at different stages.

cd <PYNQ repository>/sdbuild/
make clean

Retargeting to a Different Board

Additional boards are supported through external board repositories. A board repository consists of a directory for each board consisting of a spec file and any other files. The board repository is treated the same way as the <PYNQ repository>/boards directory.

Elements of the specification file

The specification file should be name <BOARD>.spec where BOARD is the name of the board directory. A minimal spec file contains the following information

ARCH_${BOARD} := arm
BSP_${BOARD} := mybsp.bsp
BITSTREAM_${BOARD} := mybitstream.bsp
FPGA_MANAGER_${BOARD} := 1

where ${BOARD} is also the name of the board. The ARCH should be arm for Zynq-7000 or aarch64 for Zynq UltraScale+. If no bitstream is provided then the one included in the BSP will be used by default. All paths in this file should be relative to the board directory.

To customise the BSP a petalinux_bsp folder can be included in the board directory the contents of which will be added to the provided BSP before the project is created. See the ZCU104 for an example of this in action. This is designed to allow for additional drivers, kernel or boot-file patches and device tree configuration that are helpful to support elements of PYNQ to be added to a pre-existing BSP.

If a suitable PetaLinux BSP is unavailable for the board then BSP_${BOARD} can be left blank; in this case, users have two options:

  1. Place a <design_name>.xsa file in the petalinux_bsp/hardware_project folder. As part of the build flow, a new BSP will be created from this XSA file.
  2. Place a makefile along with tcl files which can generate the hardware design in the petalinux_bsp/hardware_project folder. As part of the build flow, the hardware design along with the XSA file will be generated, then a new BSP will be created from this XSA file.

Starting from image v2.6, we allow users to disable FPGA manager by setting FPGA_MANAGER_${BOARD} to 0. This may have many use cases. For example, users may want the bitstream to be downloaded at boot to enable some board components as early as possible. Another use case is that users want to enable interrupt for XRT. The side effect of this, is that users may not be able to reload a bitstream after boot.

If FPGA_MANAGER_${BOARD} is set to 1 or FPGA_MANAGER_${BOARD} is not defined at all, FPGA manager will be enabled. In this case, the bitstream will be downloaded later in user applications; and users can only use XRT in polling mode. This is the default behavior of PYNQ since we want users to be able to download any bitstream after boot.

Board-specific packages

A packages directory can be included in board directory with the same layout as the <PYNQ repository>/sdbuild/packages directory. Each subdirectory is a package that can optionally be installed as part of image creation. See <PYNQ repository>/sdbuild/packages/README.md for a description of the format of a PYNQ sdbuild package.

To add a package to the image you must also define a STAGE4_PACKAGE_${BOARD} variable in your spec file. These can either packages in the standard sdbuild library or ones contained within the board package. It is often useful to add the pynq package to this list which will ensure that a customised PYNQ installation is included in your final image.

Leveraging boot.py to modify SD card boot behavior

Starting from the v2.6.0 release, PYNQ SD card images include a boot.py file in the boot partition that runs automatically after the board has been booted. Whatever is inside this file runs during boot and can be modified any time for a custom next-boot behavior (e.g. changing the host name, connecting the board to WiFi, etc.).

This file can be accessed using a SD Card reader on your host machine or from a running PYNQ board - if you are live on the board inside Linux, the file is located in the /boot folder. Note that /boot is the boot partition of the board and no other files should be modified.

If you see some existing code running inside the boot.py file, it probably came from a PYNQ sdbuild package that modified that file. To see an example of an sdbuild package writing the boot.py file see the ZCU104’s boot_leds package which simply flashes the boards LEDs to signify Linux has booted on the board.

Using the PYNQ package

The pynq package will treat your board directory the same as any of the officially supported boards. This means, in particular, that:

  1. A notebooks folder, if it exists, will be copied into the jupyter_notebooks folder in the image. Notebooks here will overwrite any of the default ones.
  2. Any directory containing a bitstream will be treated as an overlay and copied into the overlays folder of the PYNQ installation. Any notebooks will likewise by installed in an overlay-specific subdirectory.

Building from a board repository

To build from a third-party board repository, pass the BOARDDIR variable to the sdbuild makefile.

cd <PYNQ repository>/sdbuild/
make BOARDDIR=${BOARD_REPO}

The board repo should be provided as an absolute path. The BOARDDIR variable can be combined with the BOARDS variable if the repository contains multiple boards and only a subset should be built.