Skip to content

Instantly share code, notes, and snippets.

@DrYak
Last active January 24, 2023 12:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrYak/fc31f558af023572e80f49ae08bb1224 to your computer and use it in GitHub Desktop.
Save DrYak/fc31f558af023572e80f49ae08bb1224 to your computer and use it in GitHub Desktop.
Patcher to add Mapsv1 into squashfs system.img for AlienDalvik on Sailfish OS'
#!/bin/bash
echo -e "\e[34;1m=================================\e[37;1m"
echo "[**] 0. Check environment"
echo -e "\e[34;1m=================================\e[0m"
if [ -e /opt/alien/system.img ]; then
HAS_ALIEN_DALVIK=1
ALIEN_VERSION="$(rpm -qf '/opt/alien/system.img' --qf '%{version}')"
echo "AlienDalvik image version ${ALIEN_VERSION} found"
fi
set -e
SE_LOST=0
WARN=0
echo -e "\n\e[34;1m=================================\e[37;1m"
echo "[**] 1. Get system.img"
echo -e "\e[34;1m=================================\e[0m"
SYSIMG_ORIG="system.img.orig${ALIEN_VERSION+.${ALIEN_VERSION}}"
if [ -f "${SYSIMG_ORIG}" ]; then
echo -e "\e[33;1mWARNING: reusing existing ${SYSIMG_ORIG}\e[0m"
elif [ -e /opt/alien/system.img ]; then
rsync -avPL --inplace '/opt/alien/system.img' "${SYSIMG_ORIG}"
chown --reference=. "${SYSIMG_ORIG}"
else
echo -e "\e[31;1mERROR: can't find ${SYSIMG_ORIG}, please fetch it /opt/alien/system.img from your Sailfish smartphone\e[0m"
exit 1
fi
echo -e "\n\e[34;1m=================================\e[37;1m"
echo "[**] 1.1 Get MicroG Mapsv1"
echo -e "\e[34;1m=================================\e[0m"
if [ -f 'mapsv1.flashable.zip' ]; then
echo -e "\e[33;1mWARNING: reusing existing mapsv1.flashable.zip\e[0m"
else
curl -LO 'https://github.com/microg/android_frameworks_mapsv1/releases/download/v0.1.0/mapsv1.flashable.zip'
chown --reference=. mapsv1.flashable.zip
fi
unzip -l mapsv1.flashable.zip
echo -e "\n\e[34;1m=================================\e[37;1m"
echo "[**] 2. Black belt mount-fu"
echo -e "\e[34;1m=================================\e[0m"
TMP_DIR="$(mktemp -d sysimg.XXXXXX)"
touch "${TMP_DIR}/.noindex"
MNT_DIR="${TMP_DIR}/squashfs_root"
mkdir -p "${MNT_DIR}"
mount -o ro,loop "${SYSIMG_ORIG}" "${MNT_DIR}"
# Check the sercurity extended attributes
if [[ "$(find ${MNT_DIR}/system/bin/ -type f -print0 | xargs -0 getfattr -m - -d)" =~ security\.capability= ]]; then
echo -e "\e[36;1mCheck: Security extended attributes are readable\e[0m"
else
echo -e "\e[33;1mWARNING: Security extended attributes are missing !\e[0m"
echo "Please re-install stock AlienDalvik RPM, otherwise logd, run-as and webzygote are going to be borken !"
echo "(but the rest of Android otherwise works)"
SE_LOST=1
fi
# MntPoints indexed array lists ALL subdirectories that we need to be writable for our patching needs
declare -a MntPoints
MntPoints=( 'system/framework' 'system/etc/permissions' )
for (( k = 0; k < "${#MntPoints[@]}"; k++)); do
# check if any of the subdirectory carries extended attributes (e.g.: we want to patch /system/bin )
if [[ "$(find ${MNT_DIR}/${MntPoints[$k]}/ -print0 | xargs -0 getfattr -m - -d)" =~ security\.capability= ]]; then
echo -e "\e[33;1mWARNING: ${MntPoints[$k]} has security extended attributes !\e[0m"
HAS_SECURITY=1
else
HAS_SECURITY=0
fi
mkdir -p "${TMP_DIR}/${k}"
rsync -avPSHAXJ --numeric-ids "${MNT_DIR}/${MntPoints[$k]}/" "${TMP_DIR}/${k}/"||WARN=1
mount --bind "${TMP_DIR}/${k}" "${MNT_DIR}/${MntPoints[$k]}"
# check if subdirectories got their extended attributes carried over
if (( HAS_SECURITY )); then
if [[ "$(find ${MNT_DIR}/${MntPoints[$k]} -print0 | xargs -0 getfattr -m - -d)" =~ security\.capability= ]]; then
echo -e "\e[36;1mCheck: ${MntPoints[$k]} security extended attributes saved\e[0m"
else
echo -e "\e[33;1mWARNING: cannot save security extened attributs in ${MntPoints[$k]}, loss of functionnality is possible\[0m"
echo "Try on a Linux laptop that doesn't run SELinux"
SE_LOST=1
fi
fi
done
echo -e "\n\e[34;1m=================================\e[37;1m"
echo "[**] 3. Patch"
echo -e "\e[34;1m=================================\e[0m"
unzip -d "${MNT_DIR}" mapsv1.flashable.zip 'system/'{framework,etc}'/*'
echo -e "\n\e[34;1m=================================\e[37;1m"
echo "[**] 4. rebuild squashfs"
echo -e "\e[34;1m=================================\e[0m"
# check compression
if [[ "$(mksquashfs --help 2>&1 )" =~ lz4 ]]; then
COMP="-comp lz4 -Xhc"
else
echo -e "\e[33;1mWARNING: fast LZ4 compression not available, falling back to slower GZip compression\[0m"
echo "AlienDalvik should work, just not as fast"
fi
mksquashfs "${MNT_DIR}" system.img.mapsv1 $COMP -noappend -no-exports -no-duplicates -no-fragments
chown --reference=. system.img.mapsv1
echo -e "\n\e[34;1m=================================\e[37;1m"
echo "[**] 4.1 mount clean-up"
echo -e "\e[34;1m=================================\e[0m"
for (( k = 0; k < "${#MntPoints[@]}"; k++)); do
umount "${MNT_DIR}/${MntPoints[$k]}"
[ -d "${TMP_DIR}/${k}/" ] && rm -rf "${TMP_DIR}/${k}/"
done
umount "${MNT_DIR}/"
echo -e "\n\e[34;1m=================================\e[37;1m"
echo "[**] 4.2 check image"
echo -e "\e[34;1m=================================\e[0m"
mount -o ro,loop system.img.mapsv1 "${MNT_DIR}"
if [[ "$(find ${MNT_DIR}/system/bin/ -print0 | xargs -0 getfattr -m - -d)" =~ security\.capability= ]]; then
echo -e "\e[36;1mCheck: Security extended attributes are still readable\e[0m"
else
echo -e "\e[33;1mWARNING: Security extended attributes are missing !\e[0m"
SE_LOST=1
fi
umount "${MNT_DIR}/"
rmdir "${MNT_DIR}/"
rm "${TMP_DIR}/.noindex"
rmdir "${TMP_DIR}/"
if (( SE_LOST )); then
echo -e "\e[33;1mWARNING: Security extended attributes are missing!\e[0m"
echo "Please re-install stock AlienDalvik RPM, otherwise logd, run-as and webzygote are going to be borken !"
echo "(but the rest of Android otherwise works)"
fi
if (( WARN )); then
echo -e "\e[33;1mWARNING: Error while extracting files with rsync\e[0m"
echo "Please double-check (this might be just caused by dangling symlink point to absolute paths on Android"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment