Skip to content

Instantly share code, notes, and snippets.

@beriberikix
Last active November 4, 2024 01:29
Show Gist options
  • Save beriberikix/05aeff1abab233a4d789fde40e63cb50 to your computer and use it in GitHub Desktop.
Save beriberikix/05aeff1abab233a4d789fde40e63cb50 to your computer and use it in GitHub Desktop.
Yocto + RPI4

Quick Start Guide: Building Yocto for Raspberry Pi 4

This guide sets up Yocto for a Raspberry Pi 4, using the scarthgap branch and enabling SSH access via Dropbear. It also includes steps for setting up Wi-Fi credentials, using the ghcr.io/crops/poky:ubuntu-22.04 Docker image, and configuring Shared State Cache for faster builds.


Prerequisites

  1. Docker: Ensure Docker is installed and running:

    sudo apt update
    sudo apt install docker.io
    sudo systemctl start docker
    sudo systemctl enable docker
  2. Docker Permissions: Add your user to the Docker group:

    sudo usermod -aG docker $USER
    newgrp docker
  3. Balena Etcher: Download and install Balena Etcher to flash images onto the SD card.


Step 1: Set Up CROPS Environment

  1. Pull the CROPS Docker Image:

    docker pull ghcr.io/crops/poky:ubuntu-22.04
  2. Create a Working Directory:

    mkdir ~/yocto-work
  3. Start the CROPS Container: Run the following command to start the container with your working directory mounted:

    docker run --rm -it -v ~/yocto-work:/workdir ghcr.io/crops/poky:ubuntu-22.04 --workdir=/workdir

    This opens a shell in the CROPS environment with access to your ~/yocto-work directory as /workdir inside the container.


Step 2: Set Up Yocto Environment Inside CROPS

  1. Clone Yocto and Required Layers: Inside the CROPS container, run:

    cd /workdir
    git clone -b scarthgap git://git.yoctoproject.org/poky.git
    cd poky
    git clone -b scarthgap git://git.yoctoproject.org/meta-raspberrypi
    git clone -b scarthgap git://git.openembedded.org/meta-openembedded
  2. Source the Yocto Build Environment:

    source oe-init-build-env
  3. Configure Layer Paths: Open conf/bblayers.conf and add paths to the meta-raspberrypi and meta-openembedded layers:

    BBLAYERS ?= " \
        /workdir/poky/meta \
        /workdir/poky/meta-poky \
        /workdir/poky/meta-yocto-bsp \
        /workdir/poky/meta-raspberrypi \
        /workdir/poky/meta-openembedded/meta-oe \
        /workdir/poky/meta-openembedded/meta-networking \
        /workdir/poky/meta-openembedded/meta-python \
    "
    
  4. Configure Build Settings: In conf/local.conf, set up the target machine, enable SSH, add Dropbear, configure SSTATE, and add Wi-Fi support:

    MACHINE = "raspberrypi4"
    DISTRO_FEATURES:append = " wifi"
    IMAGE_INSTALL:append = " wpa-supplicant dropbear"
    
    # Configure Shared State Cache (SSTATE) Mirrors
    SSTATE_MIRRORS ?= "file://.* https://sstate.yoctoproject.org/dev/PATH;downloadfilename=PATH"
    
    # Enable Hash Equivalence for faster rebuilds
    ENABLE_CACHE = "1"
    BB_HASHSERVE = "auto"
    BB_HASHSERVE_UPSTREAM = "hashserv.yoctoproject.org:8687"
    

Step 3: Configure Wi-Fi Credentials

  1. Create wpa_supplicant.conf: In the poky directory, create a wpa_supplicant.conf file with your Wi-Fi network credentials:

    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    update_config=1
    
    network={
        ssid="YourNetworkSSID"
        psk="YourNetworkPassword"
        key_mgmt=WPA-PSK
    }
    
  2. Copy wpa_supplicant.conf to the Yocto Root Filesystem: To ensure the Raspberry Pi uses your Wi-Fi credentials on boot, modify local.conf to include the wpa_supplicant.conf file in the root filesystem:

    FILES_${PN}-base += "/etc/wpa_supplicant.conf"
    
  3. Add wpa_supplicant.conf to the Image Recipe: To include this file during the build, edit your image recipe or create a new .bbappend file (e.g., core-image-base.bbappend) in your layer to copy wpa_supplicant.conf:

    install -m 644 ${WORKDIR}/wpa_supplicant.conf ${D}/etc/wpa_supplicant.conf

Step 4: Build the Yocto Image

  1. Run Bitbake to Build the Image: Inside the CROPS container, use bitbake to build the desired image (e.g., core-image-base):

    bitbake core-image-base

    This process may take some time. The output image file will be located in the tmp/deploy/images/raspberrypi4 directory within your Yocto build directory.


Step 5: Flash the Image to the SD Card

  1. Locate the Output Image: The built image should be in:

    /workdir/poky/build/tmp/deploy/images/raspberrypi4/core-image-base-raspberrypi4.wic
    
  2. Use Balena Etcher to Write the Image to the SD Card:

    • Open Balena Etcher.
    • Select the .wic image file located in the directory above.
    • Select your SD card as the target.
    • Click "Flash!" and wait for the process to complete.
  3. Insert the SD Card into the Raspberry Pi 4: Once flashing is complete, insert the SD card into your Raspberry Pi 4, connect power, and it should boot with the configured SSH access and Wi-Fi credentials.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment