#!/bin/sh
# SPDX-License-Identifier: MIT

#
# This script is responsible for loading firmware blobs for the HexagonFS
# on qcom devices. It assumes to be run after msm-firmware-loader.
#

# Base directory that msm-firmware-loader uses.
BASEDIR="/lib/firmware/msm-firmware-loader"

# HexagonFS base directory.
HEXAGONFSDIR="/usr/share/qcom"

# socinfo file base directory.
SOCINFODIR="/usr/share/hexagonfs-firmware-loader"

# Preparations:

mount -o mode=755,nodev,noexec,nosuid,uid=fastrpc,gid=fastrpc -t tmpfs none "$HEXAGONFSDIR"

mkdir "$HEXAGONFSDIR/sensors"

# Get the device codename (in the form of vendor-device) from the compatible
# in the devicetree.
DEVICE=$(tr '\0,' '\n-' < /sys/firmware/devicetree/base/compatible | head -n1)

# Symlink the files and directories from msm-firmware-loader mounts:

ln -s "$BASEDIR"/mnt/vendor/etc/acdbdata "$HEXAGONFSDIR"/acdb

# Some devices have a dsp partition, others have /vendor/dsp.
# The ones with dsp partition have a mount point for it at /vendor/dsp, but
# we don't mount it there, so we need to fall back to /vendor/dsp.
if [ -d "$BASEDIR"/mnt/dsp ]
then
	ln -s "$BASEDIR"/mnt/dsp "$HEXAGONFSDIR"/dsp
else
	ln -s "$BASEDIR"/mnt/vendor/dsp "$HEXAGONFSDIR"/dsp
fi

ln -s "$BASEDIR"/mnt/vendor/etc/sensors/config "$HEXAGONFSDIR"/sensors/config

ln -s "$BASEDIR"/mnt/persist/sensors/registry/registry "$HEXAGONFSDIR"/sensors/registry

ln -s "$BASEDIR"/mnt/vendor/etc/sensors/sns_reg_config "$HEXAGONFSDIR"/sensors/sns_reg.conf

# Some devices (hey, Xiaomi!) require a bunch of files from /sys/devices/soc0.
# Since that is device-specific, let's check if we have a directory for this device.
if [ -d "$SOCINFODIR"/"$DEVICE" ]
then
	set -ex
	ln -s "$SOCINFODIR"/"$DEVICE" "$HEXAGONFSDIR"/socinfo
fi
