Fix basically everything

Add permission handling with ueventd
Add selinux context to system_ext/etc/selinux/system_ext_file_contexts
Remove init changes and boot image patching
Only initialize the keyboard once on boot with a non-standard name
  Standard names are f[1-9] we use fhid
This commit is contained in:
Timmy Welch 2025-02-14 21:07:41 -08:00
parent 92f41b38c4
commit ff422dbc23
9 changed files with 71 additions and 365 deletions

View File

@ -1,174 +0,0 @@
#!/system/bin/sh
#######################################################################################
# Magisk Boot Image Patcher
#######################################################################################
#
# Usage: boot_patch.sh <bootimage>
#
# The following flags can be set in environment variables:
# KEEPVERITY, KEEPFORCEENCRYPT, RECOVERYMODE
#
# This script should be placed in a directory with the following files:
#
# File name Type Description
#
# boot_patch.sh script A script to patch boot image for Magisk.
# (this file) The script will use files in its same
# directory to complete the patching process
# util_functions.sh script A script which hosts all functions required
# for this script to work properly
# magiskinit binary The binary to replace /init
# magisk(32/64) binary The magisk binaries
# magiskboot binary A tool to manipulate boot images
# chromeos folder This folder includes the utility and keys to sign
# (optional) chromeos boot images. Only used for Pixel C.
#
#######################################################################################
############
# Functions
############
# Pure bash dirname implementation
getdir() {
case "$1" in
*/*)
dir=${1%/*}
if [ -z $dir ]; then
echo "/"
else
echo $dir
fi
;;
*) echo "." ;;
esac
}
#################
# Initialization
#################
[ -e "$BOOTIMAGE" ] || abort "$BOOTIMAGE does not exist!"
# Dump image for MTD/NAND character device boot partitions
if [ -c "$BOOTIMAGE" ]; then
nanddump -f boot.img "$BOOTIMAGE"
BOOTNAND="$BOOTIMAGE"
BOOTIMAGE=boot.img
fi
# Flags
[ -z $KEEPVERITY ] && KEEPVERITY=false
[ -z $KEEPFORCEENCRYPT ] && KEEPFORCEENCRYPT=false
[ -z $RECOVERYMODE ] && RECOVERYMODE=false
export KEEPVERITY
export KEEPFORCEENCRYPT
chmod -R 755 .
#########
# Unpack
#########
CHROMEOS=false
ui_print "- Unpacking boot image"
$MAGISKBIN/magiskboot unpack "$BOOTIMAGE"
case $? in
0 ) ;;
1 )
abort "! Unsupported/Unknown image format"
;;
2 )
ui_print "- ChromeOS boot image detected"
CHROMEOS=true
;;
* )
abort "! Unable to unpack boot image"
;;
esac
[ -f recovery_dtbo ] && RECOVERYMODE=true
###################
# Ramdisk Restores
###################
# Test patch status and do restore
ui_print "- Checking ramdisk status"
if [ -e ramdisk.cpio ]; then
$MAGISKBIN/magiskboot cpio ramdisk.cpio test
STATUS=$?
else
# Stock A only system-as-root
STATUS=0
fi
case $((STATUS & 3)) in
0 ) # Stock boot
ui_print "- Stock boot image detected"
abort "! magisk must be installed"
;;
1 ) # Magisk patched
ui_print "- Magisk patched boot image detected"
# Find SHA1 of stock boot image
[ -z $SHA1 ] && SHA1=$($MAGISKBIN/magiskboot cpio ramdisk.cpio sha1 2>/dev/null)
cp -af ramdisk.cpio ramdisk.cpio.stock.magisk
;;
2 ) # Unsupported
ui_print "! Boot image patched by unsupported programs"
abort "! Please restore back to stock boot image"
;;
esac
##################
# Ramdisk Patches
##################
ui_print "- Patching ramdisk"
echo "KEEPVERITY=$KEEPVERITY" > config
echo "KEEPFORCEENCRYPT=$KEEPFORCEENCRYPT" >> config
echo "RECOVERYMODE=$RECOVERYMODE" >> config
[ ! -z $SHA1 ] && echo "SHA1=$SHA1" >> config
$MAGISKBIN/magiskboot cpio ramdisk.cpio "extract overlay.d/ghid.rc ghid.rc" 2>/dev/null
if ! diff $MODPATH/overlay.d/ghid.rc ghid.rc 2>/dev/null; then
ui_print "- Adding overlay.d/ghid.rc"
$MAGISKBIN/magiskboot cpio ramdisk.cpio \
"mkdir 755 overlay.d" \
"add 755 overlay.d/ghid.rc $MODPATH/overlay.d/ghid.rc"
rm -f ramdisk.cpio.orig config magisk*.xz
#################
# Binary Patches
#################
if [ $((STATUS & 4)) -ne 0 ]; then
ui_print "- Compressing ramdisk"
$MAGISKBIN/magiskboot cpio ramdisk.cpio compress
fi
rm -f ramdisk.cpio.stock.magisk config ghid.rc
#################
# Repack & Flash
#################
ui_print "- Repacking boot image"
$MAGISKBIN/magiskboot repack "$BOOTIMAGE" || abort "! Unable to repack boot image!"
# Sign chromeos boot
$CHROMEOS && sign_chromeos
# Sign chromeos boot
$CHROMEOS && sign_chromeos
else
ui_print "- ghid.rc already installed. Skipping boot patching."
rm -f ramdisk.cpio.stock.magisk config ghid.rc
fi
# Restore the original boot partition path
[ -e "$BOOTNAND" ] && BOOTIMAGE="$BOOTNAND"
# Reset any error code
true

5
build.sh Executable file
View File

@ -0,0 +1,5 @@
#!/bin/bash
rm *.zip 2>/dev/null
eval $(grep -v ' ' module.prop)
printf '%s\n' * | grep -Ev '(zip|build.sh)$' | xargs zip -r "magisk-$name-$version-$versionCode.zip"
adb push "magisk-$name-$version-$versionCode.zip" /sdcard/

View File

@ -1,15 +1,22 @@
#!/bin/sh #!/bin/sh
$BOOTMODE || abort "! must be installed in the running system" $BOOTMODE || abort "! must be installed in the running system"
if [[ -e /proc/config.gz ]]; then if [[ -e /proc/config.gz ]]; then
zcat /proc/config.gz | grep -Eq 'CONFIG_USB_(CONFIGFS_)?F_HID=[my]' || abort "! kernel does not have the required modules" zcat /proc/config.gz | grep -Eq 'CONFIG_USB_(CONFIGFS_)?F_HID=[my]' || abort "! kernel does not have the required modules"
else else
[[ -d /config/usb_gadget ]] || abort "! kernel does not have the required modules" test -d /config/usb_gadget || abort "! kernel does not have the required modules"
mkdir /config/usb_gadget/g1 || abort "! kernel does not have the required modules" mkdir /config/usb_gadget/g1 || abort "! kernel does not have the required modules"
mkdir /config/usb_gadget/g1/functions/hid.usb0 || abort "! kernel does not have the required modules" mkdir /config/usb_gadget/g1/functions/hid.usb0 || abort "! kernel does not have the required modules"
fi fi
chmod 755 "$MODPATH/system/bin/ghid.sh" chmod 755 "$MODPATH/system/bin/ghid.sh"
sed "s@\[MODDIR\]@$MODPATH@g" "$MODPATH/system/bin/ghid.sh" | sed 's/modules_update/modules/g' >"$TMPDIR/ghid.sh" sed -i -e "s@\[MODDIR\]@${MODPATH%_update}@g" "$MODPATH/system/bin/ghid.sh"
cp "$TMPDIR/ghid.sh" "$MODPATH/system/bin/ghid.sh"
. "$MODPATH/utils.sh" mkdir -p "$MODPATH/system/system_ext/etc/selinux/" "$MODPATH/system/etc"
. "$MODPATH/flash_script.sh" {
grep -v /dev/hidg /system_ext/etc/selinux/system_ext_file_contexts
echo "/dev/hidg[0-9]+ u:object_r:null_device:s0"
} >"$MODPATH/system/system_ext/etc/selinux/system_ext_file_contexts"
{
grep -v /dev/hidg /system/etc/ueventd.rc
echo "/dev/hidg* 0666 root root"
} >"$MODPATH/system/etc/ueventd.rc"

View File

@ -1,60 +0,0 @@
#MAGISK
############################################
# Magisk Flash Script (updater-script)
############################################
##############
# Preparation
##############
find_magisk_apk
# Default permissions
umask 022
OUTFD=$2
APK="$3"
COMMONDIR=$INSTALLER/assets
CHROMEDIR=$INSTALLER/assets/chromeos
setup_flashable
############
# Detection
############
if echo $MAGISK_VER | grep -q '\.'; then
PRETTY_VER=$MAGISK_VER
else
PRETTY_VER="$MAGISK_VER($MAGISK_VER_CODE)"
fi
print_title "Magisk $PRETTY_VER Installer"
is_mounted /data || mount /data || is_mounted /cache || mount /cache
mount_partitions
check_data
get_flags
find_boot_image
[ -z $BOOTIMAGE ] && abort "! Unable to detect target image"
ui_print "- Target image: $BOOTIMAGE"
# Detect version and architecture
api_level_arch_detect
[ $API -lt 21 ] && abort "! Magisk only support Android 5.0 and above"
ui_print "- Device platform: $ARCH"
##################
# Image Patching
##################
install_magisk
# Cleanups
$BOOTMODE || recovery_cleanup
rm -rf $TMPDIR
ui_print "- Done"
exit 0

View File

@ -1,6 +1,6 @@
id=ghid id=ghid
name=ghid name=ghid
version=0.11 version=0.12.69
versionCode=38 versionCode=69
author=lordwelch author=lordwelch
description=creates ghid device files in /dev description=creates ghid device files in /dev

View File

@ -1,8 +0,0 @@
on property:sys.usb.ffs.ready=1 && property:sys.usb.config=* && property:sys.usb.configfs=1
exec u:r:magisk:s0 root root -- /system/bin/ghid.sh setup ${sys.usb.config}
start ghid
service ghid /system/bin/ghid.sh setup ${sys.usb.config}
user root
disabled
oneshot

8
service.sh Normal file
View File

@ -0,0 +1,8 @@
#!/bin/sh
ueventd=$(getprop init.svc.ueventd)
if [ "$ueventd" = "running" ]; then
# ueventd ensures that the correct permissions get applied, it may also apply the correct selabel, not sure
stop ueventd
start ueventd
fi
/system/bin/ghid.sh init

View File

@ -2,7 +2,7 @@
# $1 setup # $1 setup
controller=$(getprop sys.usb.controller) controller=$(getprop sys.usb.controller)
moddir=[MODDIR] moddir=[MODDIR]
selected=keyboard-standard
exec >>$moddir/ghid.log exec >>$moddir/ghid.log
exec 2>>$moddir/ghid.log exec 2>>$moddir/ghid.log
date date
@ -11,68 +11,59 @@ set -o nohup
set +o errexit set +o errexit
set +o sh set +o sh
set +o posix set +o posix
exec >>$moddir/ghid.log exec >>$moddir/oghid.log
exec 2>>$moddir/ghid.log exec 2>>$moddir/oghid.log
remove_ghid() { remove_ghid() {
echo "disabling ghid" echo "disabling ghid"
rm /config/usb_gadget/g1/configs/b.1/fhid rm /config/usb_gadget/g1/configs/b.1/fhid
rmdir /config/usb_gadget/g1/functions/hid.usb0 rmdir /config/usb_gadget/g1/functions/hid.usb0
} }
enable_ghid() { enable_ghid() {
for f in $selected; do . "$moddir/definitions/keyboard-standard"
echo "enabling ghid" if [ ! -d /config/usb_gadget/g1/functions/hid.usb0 ]; then
running=true mkdir /config/usb_gadget/g1/functions/hid.usb0
. "$moddir/definitions/$f" fi
if [ ! -d /config/usb_gadget/g1/functions/hid.usb0 ]; then current_protocol="$(cat /config/usb_gadget/g1/functions/hid.usb0/protocol)"
mkdir /config/usb_gadget/g1/functions/hid.usb0 current_subclass="$(cat /config/usb_gadget/g1/functions/hid.usb0/subclass)"
fi current_report_length="$(cat /config/usb_gadget/g1/functions/hid.usb0/report_length)"
current_protocol="$(cat /config/usb_gadget/g1/functions/hid.usb0/protocol)"
current_subclass="$(cat /config/usb_gadget/g1/functions/hid.usb0/subclass)"
current_report_length="$(cat /config/usb_gadget/g1/functions/hid.usb0/report_length)"
echo "current ghid"
echo current_protocol $current_protocol
echo current_subclass $current_subclass
echo current_report_length $current_report_length
if [ -e /config/usb_gadget/g1/functions/hid.usb0 -a "$current_protocol" == "$protocol" -a "$current_subclass" == "$subclass" -a "$current_report_length" == "$report_length" ] && diff "$moddir/reports/$report_name" /config/usb_gadget/g1/functions/hid.usb0/report_desc ; then if [ -e /config/usb_gadget/g1/functions/hid.usb0 -a "$current_protocol" == "$protocol" -a "$current_subclass" == "$subclass" -a "$current_report_length" == "$report_length" ] && diff "$moddir/reports/$report_name" /config/usb_gadget/g1/functions/hid.usb0/report_desc; then
echo nothing to update echo nothing to update
return 0 cd /config/usb_gadget/g1/configs/b.1/
fi /data/adb/magisk/busybox ln -nsf ../../../../usb_gadget/g1/functions/hid.usb0 ./fhid
return 0
fi
echo new ghid echo new ghid
echo $protocol echo protocol: $protocol
echo $subclass echo subclass: $subclass
echo $report_length echo report_length: $report_length
rm /config/usb_gadget/g1/configs/b.1/fhid echo $protocol >/config/usb_gadget/g1/functions/hid.usb0/protocol
echo $protocol >/config/usb_gadget/g1/functions/hid.usb0/protocol echo $subclass >/config/usb_gadget/g1/functions/hid.usb0/subclass
echo $subclass >/config/usb_gadget/g1/functions/hid.usb0/subclass echo $report_length >/config/usb_gadget/g1/functions/hid.usb0/report_length
echo $report_length >/config/usb_gadget/g1/functions/hid.usb0/report_length cat "$moddir/reports/$report_name" >/config/usb_gadget/g1/functions/hid.usb0/report_desc
cp "$moddir/reports/$report_name" /config/usb_gadget/g1/functions/hid.usb0/report_desc
cd /config/usb_gadget/g1/configs/b.1/
/data/adb/magisk/busybox ln -nsf ../../../../usb_gadget/g1/functions/hid.usb0 ./fhid
done
# echo none >/config/usb_gadget/g1/UDC
# echo "$controller" >/config/usb_gadget/g1/UDC
cd /config/usb_gadget/g1/configs/b.1/
/data/adb/magisk/busybox ln -nsf ../../../../usb_gadget/g1/functions/hid.usb0 ./fhid
} }
echo success ghid was run with: "$@" echo success ghid was run with: "$@"
if ! [[ "$1" == "setup" ]]; then
exit 1
fi
shift
case "$1" in case "$1" in
""|"none" ) "init")
remove_ghid enable_ghid
;; ;;
* ) "remove")
enable_ghid remove_ghid
;; ;;
*)
shift
exit 0
;;
esac esac
sh <<<'chcon u:object_r:null_device:s0 /dev/hidg*;chmod 666 /dev/hidg*;sleep 5;chcon u:object_r:null_device:s0 /dev/hidg*;chmod 666 /dev/hidg*;ls -alZ /dev/hidg*;' &
restorecon /dev/hidg*

View File

@ -1,63 +0,0 @@
# get_flags() {
# # override variables
# getvar KEEPVERITY
# getvar KEEPFORCEENCRYPT
# getvar RECOVERYMODE
# if [ -z $KEEPVERITY ]; then
# if $SYSTEM_ROOT; then
# KEEPVERITY=true
# ui_print "- System-as-root, keep dm/avb-verity"
# else
# KEEPVERITY=false
# fi
# fi
# ISENCRYPTED=false
# grep ' /data ' /proc/mounts | grep -q 'dm-' && ISENCRYPTED=true
# [ "$(getprop ro.crypto.state)" = "encrypted" ] && ISENCRYPTED=true
# if [ -z $KEEPFORCEENCRYPT ]; then
# # No data access means unable to decrypt in recovery
# if $ISENCRYPTED || ! $DATA; then
# KEEPFORCEENCRYPT=true
# ui_print "- Encrypted data, keep forceencrypt"
# else
# KEEPFORCEENCRYPT=false
# fi
# fi
# [ -z $RECOVERYMODE ] && RECOVERYMODE=false
# }
install_magisk() {
cd $TMPDIR
mkdir boot_patching
cd boot_patching/
# Dump image for MTD/NAND character device boot partitions
if [ -c $BOOTIMAGE ]; then
nanddump -f boot.img $BOOTIMAGE
local BOOTNAND=$BOOTIMAGE
BOOTIMAGE=boot.img
fi
if [ $API -ge 21 ]; then
eval $BOOTSIGNER -verify < $BOOTIMAGE && BOOTSIGNED=true
$BOOTSIGNED && ui_print "- Boot image is signed with AVB 1.0"
fi
# Source the boot patcher
. "$MODPATH/boot_patch.sh"
if [ -f new-boot.img ]; then
ui_print "- Flashing new boot image"
# Restore the original boot partition path
[ "$BOOTNAND" ] && BOOTIMAGE=$BOOTNAND
flash_image new-boot.img "$BOOTIMAGE" || abort "! Insufficient partition size"
rm -f new-boot.img
run_migrations
fi
$MAGISKBIN/magiskboot cleanup
}