# SPDX-License-Identifier: GPL-2.0-or-later

sanitize_name_arm64() {
	sed -e '
		y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/;
		s/[^a-z0-9_-]\+/-/g;
		s/^-//;
		s/-$//;
	' "$@"
}

do_sysinfo_arm64() {
	local vendor product file

	for file in sys_vendor board_vendor; do
		vendor="$(cat /sys/devices/virtual/dmi/id/$file 2>/dev/null)"
		case "$vendor" in
		empty | \
		System\ manufacturer | \
		To\ [bB]e\ [fF]illed\ [bB]y\ O\.E\.M\.)
			continue
			;;
		esac
		[ -n "$vendor" ] && break
	done

	for file in product_name board_name; do
		product="$(cat /sys/devices/virtual/dmi/id/$file 2>/dev/null)"
		case "$vendor:$product" in
		?*:empty | \
		?*:System\ Product\ Name | \
		?*:To\ [bB]e\ [fF]illed\ [bB]y\ O\.E\.M\.)
			continue
			;;
		?*:?*)
			break
			;;
		esac
	done

	[ -d "/sys/firmware/devicetree/base" ] && return

	[ -n "$vendor" -a -n "$product" ] || return

	mkdir -p /tmp/sysinfo

	echo "$vendor $product" > /tmp/sysinfo/model

	sanitize_name_arm64 /tmp/sysinfo/model > /tmp/sysinfo/board_name
}

boot_hook_add preinit_main do_sysinfo_arm64
