KasperskyOS Community Edition

Preparing Raspberry Pi 4 B to run examples

May 21, 2024

ID preparing_sd_card_rpi

Connecting a computer and Raspberry Pi 4 B

To see the output from Raspberry Pi 4 B on a computer, do the following:

  1. Connect the pins of the FT232 USB-UART converter to the corresponding GPIO pins of the Raspberry Pi 4 B (see the figure below).

    RPI_USB

    Diagram for connecting the USB-UART converter and Raspberry Pi 4 B

  2. Connect the computer's USB port to the USB-UART converter.
  3. Install PuTTY or another equivalent program. Configure the settings as follows: bps = 115200, data bits = 8, stop bits = 1, parity = none, flow control = none. Define the USB port connected to the USB-UART converter used for receiving output from Raspberry Pi 4 B.

To allow a computer and Raspberry Pi 4 B to interact through Ethernet:

  1. Connect the network cards of the computer and Raspberry Pi 4 B to a switch or to each other.
  2. Configure the computer's network card so that its IP address is in the same subnet as the IP address of the Raspberry Pi 4 B network card (the settings of the Raspberry Pi 4 B network card are defined in the dhcpcd.conf file, which is found at the path <example name>/resources/...).

Preparing a bootable SD card for Raspberry Pi 4 B

If the rpi4kos.img image was created when building the example, all you have to do is write the resulting image to the SD card. To do this, connect the SD card to the computer and run the following command:

# In the following command, path_to_img is the path to the image file

# [X] is the final character in the name of the SD card block device.

$ sudo pv -L 32M path_to_img | sudo dd bs=64k of=/dev/sd[X] conv=fsync

If kos-image was created when building the example, the SD card requires additional preparations before you can write the image to it. A bootable SD card for Raspberry Pi 4 B can be prepared automatically or manually.

To automatically prepare the bootable SD card, connect the SD card to the computer and run the following commands:

# To create a bootable drive image file (*.img),

# run the script:

$ sudo /opt/KasperskyOS-Community-Edition-<version>/common/rpi4_prepare_sdcard_image.sh

# In the following command, path_to_img is the path to the image file

# of the bootable drive (this path is displayed upon completion

# of the previous command), [X] is the final character

# in the name of the SD card block device.

$ sudo pv -L 32M path_to_img | sudo dd bs=64k of=/dev/sd[X] conv=fsync

To manually prepare the bootable SD card:

  1. Build the U-Boot bootloader for ARMv8, which will automatically run the example. To do this, run the following commands:

    $ sudo apt install git build-essential libssl-dev bison flex unzip parted gcc-aarch64-linux-gnu pv -y

    $ git clone --depth 1 --branch v2022.01 https://github.com/u-boot/u-boot.git u-boot-armv8

    $ cd u-boot-armv8

    $ make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- rpi_4_defconfig

    $ echo 'CONFIG_SERIAL_PROBE_ALL=y' > ./.custom_config

    $ echo 'CONFIG_BOOTCOMMAND="fatload mmc 0 ${loadaddr} kos-image; bootelf ${loadaddr} ${fdt_addr}"' >> ./.custom_config

    $ echo 'CONFIG_PREBOOT="pci enum;"' >> ./.custom_config

    $ ./scripts/kconfig/merge_config.sh '.config' '.custom_config'

    $ make ARCH=arm CROSS_COMPILE=aarch64-linux-gnu- u-boot.bin

  2. Prepare the image containing the file system for the SD card.

    # Image will contain a boot partition of 1 GB in fat32 and three partitions of 350 MB each in ext2, ext3 and ext4, respectively.

    $ fs_image_name=sdcard.img

    $ dd if=/dev/zero of=${fs_image_name} bs=1024k count=2048

    $ sudo parted ${fs_image_name} mklabel msdos

    $ loop_device=$(sudo losetup --find --show --partscan ${fs_image_name})

    $ sudo parted ${loop_device} mkpart primary fat32 8192s 50%

    $ sudo parted ${loop_device} mkpart extended 50% 100%

    $ sudo parted ${loop_device} mkpart logical ext2 50% 67%

    $ sudo parted ${loop_device} mkpart logical ext3 67% 84%

    $ sudo parted ${loop_device} mkpart logical ext4 84% 100%

    $ sudo parted ${loop_device} set 1 boot on

    $ sudo mkfs.vfat ${loop_device}p1

    $ sudo mkfs.ext2 ${loop_device}p5

    $ sudo mkfs.ext3 ${loop_device}p6

    $ sudo mkfs.ext4 -O ^64bit,^extent ${loop_device}p7

  3. Copy the U-Boot bootloader and embedded software (firmware) for Raspberry Pi 4 B to the received file system image by running the following commands:

    # In the following commands, the path ~/mnt/fat32 is just an example.

    # You can use a different path.

    $ mount_temp_dir=~/mnt/fat32

    $ mkdir -p ${mount_temp_dir}

    $ sudo mount ${loop_device}p1 ${mount_temp_dir}

    $ git clone --depth 1 --branch 1.20220331 https://github.com/raspberrypi/firmware.git firmware

    $ sudo cp u-boot.bin ${mount_temp_dir}/u-boot.bin

    $ sudo cp -r firmware/boot/. ${mount_temp_dir}

  4. Fill in the configuration file for the U-Boot bootloader in the image by using the following commands:

    $ sudo sh -c "echo '[all]' > ${mount_temp_dir}/config.txt"

    $ sudo sh -c "echo 'arm_64bit=1' >> ${mount_temp_dir}/config.txt"

    $ sudo sh -c "echo 'enable_uart=1' >> ${mount_temp_dir}/config.txt"

    $ sudo sh -c "echo 'kernel=u-boot.bin' >> ${mount_temp_dir}/config.txt"

    $ sudo sh -c "echo 'dtparam=i2c_arm=on' >> ${mount_temp_dir}/config.txt"

    $ sudo sh -c "echo 'dtparam=i2c=on' >> ${mount_temp_dir}/config.txt"

    $ sudo sh -c "echo 'dtparam=spi=on' >> ${mount_temp_dir}/config.txt"

    $ sudo sh -c "echo 'device_tree_address=0x2eff5b00' >> ${mount_temp_dir}/config.txt"

    $ sudo sh -c "echo 'device_tree_end=0x2f0f5b00' >> ${mount_temp_dir}/config.txt"

    $ sudo sh -c "echo 'dtoverlay=uart5' >> ${mount_temp_dir}/config.txt"

    $ sudo umount ${mount_temp_dir}

    $ sudo losetup -d ${loop_device}

  5. Write the resulting image to an SD card. To do this, connect the SD card to the computer and run the following command:

    # In the following command, [X] is the last symbol in the name of the block device for the SD card.

    $ sudo pv -L 32M ${fs_image_name} | sudo dd bs=64k of=/dev/sd[X] conv=fsync

Did you find this article helpful?
What can we do better?
Thank you for your feedback! You're helping us improve.
Thank you for your feedback! You're helping us improve.