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tly3X1jeHzNti71HKv
y20jm17jj551ev4h93hqxcVRRuvZqd8QaDDLlK5h2IwNnSLOaQQ1gUm2sIYA
JW3vroc1v7jklSmD/cjzuuJwxfeLEd5Ir57WAHOMGXhYv+6dAAHsDIAzAADX
D1esscRn+wIAAAAABFla
===="

dtb_uuencoded="$(mktemp switchpwm.XXXXXX)"
dtb_xz="$(mktemp switchpwm.XXXXXX)"


mkdir /tmp/boot
mount -t vfat /dev/mmcblk0p1 /tmp/boot

if [ ! -e /tmp/boot/suniv-f1c500s-miyoo.dtb.orig ]; then
    cp /tmp/boot/suniv-f1c500s-miyoo.dtb /tmp/boot/suniv-f1c500s-miyoo.dtb.orig
fi

printf "$devicetree" >"$dtb_uuencoded"
uudecode -o "$dtb_xz" "$dtb_uuencoded"
xz -dc "$dtb_xz" >/tmp/boot/suniv-f1c500s-miyoo.dtb

rm "$dtb_uuencoded" "$dtb_xz"

umount /tmp/boot

printf 'Device tree copied successfully, reboot for changes to take effect\n'
sleep 5
