#!/bin/sh
# TP-Link/Mercusys boards keep their factory data (including the 6-byte
# "default-mac") in the tp_data UBIFS volume. Attach and mount it read-only
# during preinit so board.d/02_network and the ath11k caldata hotplug can
# read the MAC without each attaching the volume themselves.

. /lib/functions.sh

preinit_mount_tp_data() {
	case "$(board_name)" in
	mercusys,mr80x-v2|tplink,archer-ax55-v1) ;;
	*) return ;;
	esac

	# ubiattach lives in /usr/sbin, which the preinit PATH omits
	local PATH="/usr/sbin:/usr/bin:$PATH"
	local mtd_num ubi_num

	mtd_num=$(find_mtd_index tp_data)
	[ -n "$mtd_num" ] || return

	ubi_num=$(ubiattach -m "$mtd_num" 2>/dev/null | \
		sed -n 's/.*device number \([0-9]*\).*/\1/p')
	[ -n "$ubi_num" ] || return

	mkdir -p /tmp/tp_data
	mount -t ubifs -o ro "ubi${ubi_num}:tp_data" /tmp/tp_data 2>/dev/null
}

boot_hook_add preinit_main preinit_mount_tp_data
