Skip to content
/ openwrt Public
forked from openwrt/openwrt

Commit

Permalink
stm32: add new stm32 target
Browse files Browse the repository at this point in the history
New stm32 target introduces support for stm32mp1 based devices.
For now it includes an initial support of the STM32MP135F-DK device.
The specifications bellow only list supported features.

Specifications
--------------

SOC: STM32MP135FAF7
RAM: 512 MiB
Storage: SD Card
Ethernet: 2x 100 Mbps
Wireless: 2.4GHz Cypress CYW43455 (802.11b/g/n)
LEDs: Heartbeat (Blue)
Buttons: 1x Reset, 1x User (USER2)
USB: 4x 2.0 Type-A

Signed-off-by: Thomas Richard <[email protected]>
Link: openwrt#16716
Signed-off-by: Hauke Mehrtens <[email protected]>
  • Loading branch information
thom24 authored and hauke committed Nov 23, 2024
1 parent e109831 commit 851e7f7
Show file tree
Hide file tree
Showing 31 changed files with 4,674 additions and 0 deletions.
24 changes: 24 additions & 0 deletions target/linux/stm32/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-License-Identifier: GPL-2.0-only
#
# Copyright (C) 2024 Bootlin
#

include $(TOPDIR)/rules.mk

BOARD:=stm32
BOARDNAME:=STMicroelectronics STM32
FEATURES:=boot-part emmc ext4 gpio rtc usb
SUBTARGETS:=stm32mp1
CPU_TYPE:=

KERNEL_PATCHVER:=6.6

include $(INCLUDE_DIR)/target.mk

DEFAULT_PACKAGES += blockdev kmod-gpio-button-hotplug

define Target/Description
Build firmware image for STM32 devices
endef

$(eval $(call BuildTarget))
18 changes: 18 additions & 0 deletions target/linux/stm32/base-files/etc/board.d/02_network
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

. /lib/functions/uci-defaults.sh
. /lib/functions.sh
. /lib/functions/system.sh

board_config_update

board=$(board_name)

case "$board" in
st,stm32mp135f-dk)
ucidef_set_interfaces_lan_wan "eth0" "eth1"
;;
esac

board_config_flush

exit 0
15 changes: 15 additions & 0 deletions target/linux/stm32/base-files/lib/preinit/79_move_config
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

move_config() {
. /lib/upgrade/common.sh
. /lib/upgrade/platform.sh

if export_bootdevice && export_partdevice partdev 4; then
mount -t ext4 -o rw,noatime "/dev/$partdev" /mnt
if [ -f "/mnt/$BACKUP_FILE" ]; then
mv -f "/mnt/$BACKUP_FILE" /
fi
umount /mnt
fi
}

boot_hook_add preinit_mount_root move_config
181 changes: 181 additions & 0 deletions target/linux/stm32/base-files/lib/upgrade/platform.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
REQUIRE_IMAGE_METADATA=1

UBOOT_ENV_PART=3
BOOT_PART=4
ROOTFS_PART=5

RAMFS_COPY_BIN='blockdev'

export_bootdevice() {
local cmdline uuid blockdev uevent line class
local MAJOR MINOR DEVNAME DEVTYPE
local rootpart="$(cmdline_get_var root)"

case "$rootpart" in
PARTUUID=????????-????-????-????-??????????0?/PARTNROFF=1 | \
PARTUUID=????????-????-????-????-??????????05)
uuid="${rootpart#PARTUUID=}"
uuid="${uuid%/PARTNROFF=1}"
uuid="${uuid%0?}00"
for disk in $(find /dev -type b); do
set -- $(dd if=$disk bs=1 skip=568 count=16 2>/dev/null | hexdump -v -e '8/1 "%02x "" "2/1 "%02x""-"6/1 "%02x"')
if [ "$4$3$2$1-$6$5-$8$7-$9" = "$uuid" ]; then
uevent="/sys/class/block/${disk##*/}/uevent"
break
fi
done
;;
esac

if [ -e "$uevent" ]; then
while read line; do
export -n "$line"
done < "$uevent"
export BOOTDEV_MAJOR=$MAJOR
export BOOTDEV_MINOR=$MINOR
return 0
fi

return 1
}

platform_check_image() {
local diskdev partdev diff

[ "$#" -gt 1 ] && return 1

export_bootdevice && export_partdevice diskdev 0 || {
v "platform_check_image: Unable to determine upgrade device"
return 1
}

get_partitions "/dev/$diskdev" bootdisk

v "Extract the boot sector from the image"
get_image_dd "$1" of=/tmp/image.bs count=63 bs=512b

get_partitions /tmp/image.bs image

#compare tables
diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"

rm -f /tmp/image.bs /tmp/partmap.bootdisk /tmp/partmap.image

if [ -n "$diff" ]; then
echo "Partition layout has changed. Full image will be written."
ask_bool 0 "Abort" && exit 1
return 0
fi
}

platform_do_upgrade() {
local diskdev partdev diff partlabel

export_bootdevice && export_partdevice diskdev 0 || {
v "platform_do_upgrade: Unable to determine upgrade device"
return 1
}

sync

if [ "$UPGRADE_OPT_SAVE_PARTITIONS" = "1" ]; then
get_partitions "/dev/$diskdev" bootdisk

v "Extract boot sector from the image"
get_image_dd "$1" of=/tmp/image.bs count=63 bs=512b

get_partitions /tmp/image.bs image

#compare tables
diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
else
diff=1
fi

if [ -n "$diff" ]; then
rm -rf /tmp/ubootenv

if export_partdevice partdev $UBOOT_ENV_PART; then
v "Saving u-boot env (/dev/$partdev) before to write image"
get_image_dd "/dev/$partdev" of=/tmp/ubootenv
fi

v "Writing image to /dev/$diskdev..."
get_image_dd "$1" of="/dev/$diskdev" conv=fsync

blockdev --rereadpt "/dev/$diskdev"

[ -f /tmp/ubootenv ] && {
# iterate over each partition from the image to find the
# u-boot-env partition and restore u-boot env.
while read part start size; do
if export_partdevice partdev $part; then
while read line; do
eval "local l$line"
done < "/sys/class/block/$partdev/uevent"

[ "$lPARTNAME" = "u-boot-env" ] || continue

v "Writting u-boot env to /dev/$partdev"
get_image_dd /tmp/ubootenv of="/dev/$partdev" conv=fsync

return 0
fi
done < /tmp/partmap.image
}
return 0
fi

#iterate over each partition from the image and write it to the boot disk
while read part start size; do
if export_partdevice partdev $part; then
# do not erase u-boot env
[ "$part" = "$UBOOT_ENV_PART" ] && continue

v "Writing image to /dev/$partdev..."
v "Normal partition, doing DD"
get_image_dd "$1" of="/dev/$partdev" ibs=512 obs=1M skip="$start" \
count="$size" conv=fsync
else
v "Unable to find partition $part device, skipped."
fi
done < /tmp/partmap.image

if export_partdevice partdev "$BOOT_PART"; then
mount -t ext4 -o rw,noatime "/dev/$partdev" /mnt
local partuuid="$(cmdline_get_var root)"
v "Setting rootfs ${partuuid}"
sed -i "s/PARTUUID=[a-f0-9-]\+/${partuuid}/ig" \
/mnt/extlinux/extlinux.conf
umount /mnt
fi
}

platform_copy_config() {
local partdev

# Iterate over each partition from the image to find the boot partition
# and copy the config tarball.
# The partlabel is used to find the partition.
# An hardcoded partition number cannot be used, as it could be wrong if
# the partition table changed, and the full image was written.
while read part start size; do
# config is copied in the boot partition, as for squashfs image, the
# rootfs partition is not writable.
if export_partdevice partdev "$part"; then
while read line; do
eval "local l$line"
done < "/sys/class/block/$partdev/uevent"

[ "$lPARTNAME" = "boot" ] || continue

mount -t ext4 -o rw,noatime "/dev/$partdev" /mnt
cp -af "$UPGRADE_BACKUP" "/mnt/$BACKUP_FILE"
umount /mnt
return 0
else
v "ERROR: Unable to find partition to copy config data to"
fi
done < /tmp/partmap.image
}

63 changes: 63 additions & 0 deletions target/linux/stm32/image/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# SPDX-License-Identifier: GPL-2.0-only
#
# Copyright (C) 2024 Bootlin
#

include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/image.mk

define Build/boot-img-ext4
rm -fR $@.boot
mkdir -p $@.boot
$(foreach dts,$(DEVICE_DTS), $(CP) $(KDIR)/image-$(dts).dtb $@.boot/$(dts).dtb;)
$(CP) $(IMAGE_KERNEL) $@.boot/$(KERNEL_IMG)
$(INSTALL_DIR) $@.boot/extlinux
$(CP) ./extlinux.conf $@.boot/extlinux/
$(SED) 's/@KERNEL@/$(KERNEL_IMG)/' $@.boot/extlinux/extlinux.conf
$(SED) 's/@DEVICE@/$(DEVICE_NAME)/' $@.boot/extlinux/extlinux.conf
$(SED) 's/@DTS@/$(DEVICE_DTS)/' $@.boot/extlinux/extlinux.conf
$(SED) 's/@ROOT@/PARTUUID=$(shell echo $(IMG_PART_DISKGUID) | sed 's/00$$/05/')/' $@.boot/extlinux/extlinux.conf

make_ext4fs -J -L kernel -l $(CONFIG_TARGET_KERNEL_PARTSIZE)M \
$(if $(SOURCE_DATE_EPOCH),-T $(SOURCE_DATE_EPOCH)) \
$@.bootimg $@.boot
endef

define Build/sdcard-img
GUID=$(IMG_PART_DISKGUID) ./gen_stm32_sdcard_img.sh \
$@ $(STAGING_DIR_IMAGE)/tf-a-$(DEVICE_NAME).stm32 \
$(STAGING_DIR_IMAGE)/fip-$(DEVICE_NAME).bin $@.bootimg $(IMAGE_ROOTFS) \
$(ENV_SIZE) $(CONFIG_TARGET_KERNEL_PARTSIZE) $(CONFIG_TARGET_ROOTFS_PARTSIZE)
endef

define Device/Default
PROFILES := Default
DEVICE_VENDOR := STMicroelectronics
IMAGES := factory.img.gz sysupgrade.img.gz
IMAGE/factory.img.gz := boot-img-ext4 | sdcard-img | gzip
IMAGE/sysupgrade.img.gz := boot-img-ext4 | sdcard-img | gzip | append-metadata
KERNEL := kernel-bin
KERNEL_NAME := zImage
KERNEL_IMG := zImage
DEVICE_DTS_DIR := $(DTS_DIR)/st
ENV_SIZE := 0x200000
DEVICE_PACKAGES := kmod-brcmfmac \
murata-firmware-43430-sdio \
murata-nvram-43430-sdio \
wpad-basic-mbedtls \
kmod-phy-stm32-usbphyc \
kmod-usb2 \
kmod-usb-storage \
kmod-usb-ledtrig-usbport \
-mtd
endef

define Device/stm32mp135f-dk
DEVICE_MODEL := STM32MP135F-DK
DEVICE_DTS := stm32mp135f-dk
SUPPORTED_DEVICES := st,stm32mp135f-dk
endef

TARGET_DEVICES += stm32mp135f-dk

$(eval $(call BuildImage))
4 changes: 4 additions & 0 deletions target/linux/stm32/image/extlinux.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
label @DEVICE@-openwrt
kernel /@KERNEL@
devicetree /@[email protected]
append root=@ROOT@ rootwait
35 changes: 35 additions & 0 deletions target/linux/stm32/image/gen_stm32_sdcard_img.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/sh
# Copyright (C) 2024 Bootlin

set -ex
[ $# -eq 8 ] || {
echo "SYNTAX: $0 <file> <fsbl> <fip> <bootfs image> <rootfs image> <env size> <bootfs size> <rootfs size>"
exit 1
}

OUTPUT="${1}"
FSBL="${2}"
FIP="${3}"
BOOTFS="${4}"
ROOTFS="${5}"
ENVSIZE="$((${6} / 1024))"
BOOTFSSIZE="${7}"
ROOTFSSIZE="${8}"

set $(ptgen -o "${OUTPUT}" -g -a 4 -l 2048 -G ${GUID} -N fsbla -p 2M -N fip -p 3M -N u-boot-env -p "${ENVSIZE}" -N boot -p${BOOTFSSIZE}M -N rootfs -p ${ROOTFSSIZE}M)
FSBLAOFFSET="$((${1} / 512))"
FSBLASIZE="$((${2} / 512))"
FIPOFFSET="$((${3} / 512))"
FIPSIZE="$((${4} / 512))"
ENVOFFSET="$((${5} / 512))"
ENVSIZE="$((${6} / 512))"
BOOTFSOFFSET="$((${7} / 512))"
BOOTFSSIZE="$((${8} / 512))"
ROOTFSOFFSET="$((${9} / 512))"
ROOTFSSIZE="$((${10} / 512))"

dd bs=512 if="${FSBL}" of="${OUTPUT}" seek="${FSBLAOFFSET}" conv=notrunc
dd bs=512 if="${FIP}" of="${OUTPUT}" seek="${FIPOFFSET}" conv=notrunc
dd bs=512 if=/dev/zero of="${OUTPUT}" seek="${ENVOFFSET}" count="${ENVSIZE}" conv=notrunc
dd bs=512 if="${BOOTFS}" of="${OUTPUT}" seek="${BOOTFSOFFSET}" conv=notrunc
dd bs=512 if="${ROOTFS}" of="${OUTPUT}" seek="${ROOTFSOFFSET}" conv=notrunc
Loading

0 comments on commit 851e7f7

Please sign in to comment.