Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 855aadf

Browse files
committedOct 18, 2025
Add script
1 parent 6e44214 commit 855aadf

File tree

1 file changed

+346
-52
lines changed

1 file changed

+346
-52
lines changed
 

‎scripts/app_functions.sh‎

Lines changed: 346 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ env_check() {
1717
if [ "$2" -ge 25000 ]; then
1818
[ -f "$MAGISKBIN/magiskpolicy" ] || return 1
1919
fi
20-
if [ "$2" -ge 25210 ]; then
21-
[ -b "$MAGISKTMP/.magisk/device/preinit" ] || [ -b "$MAGISKTMP/.magisk/block/preinit" ] || return 2
22-
fi
20+
#if [ "$2" -ge 25210 ]; then
21+
#[ -b "$MAGISKTMP/.magisk/device/preinit" ] || [ -b "$MAGISKTMP/.magisk/block/preinit" ] || return 2
22+
#fi
2323
grep -xqF "MAGISK_VER='$1'" "$MAGISKBIN/util_functions.sh" || return 3
2424
grep -xqF "MAGISK_VER_CODE=$2" "$MAGISKBIN/util_functions.sh" || return 3
2525
return 0
@@ -59,63 +59,351 @@ fix_env() {
5959
# $1 = install dir
6060
# $2 = boot partition
6161
direct_install() {
62-
echo "- Flashing new boot image"
63-
flash_image $1/new-boot.img $2
64-
case $? in
65-
1)
66-
echo "! Insufficient partition size"
67-
return 1
68-
;;
69-
2)
70-
echo "! $2 is read only"
71-
return 2
72-
;;
73-
esac
74-
75-
rm -f $1/new-boot.img
76-
fix_env $1
77-
run_migrations
62+
# echo "- Flashing new boot image"
63+
# flash_image $1/new-boot.img $2
64+
# case $? in
65+
# 1)
66+
# echo "! Insufficient partition size"
67+
# return 1
68+
# ;;
69+
# 2)
70+
# echo "! $2 is read only"
71+
# return 2
72+
# ;;
73+
# esac
74+
#
75+
# rm -f $1/new-boot.img
76+
# fix_env $1
77+
# run_migrations
78+
#
79+
return 0
80+
}
81+
82+
# $1 = mount mode
83+
mount_system() {
84+
local slot="$(getprop ro.boot.slot_suffix)"
7885

86+
mount -o "${1},remount" / 2>/dev/null
87+
mount -o "${1},remount" /system 2>/dev/null
88+
89+
for block in "/dev/block/by-name/system" "/dev/block/mapper/system${slot}"; do
90+
if [ -b "$block" ] || [ -L "$block" ]; then
91+
blockdev --set${1} "$block" 2>/dev/null
92+
mount -o "remount,${1}" "$block" /system 2>/dev/null
93+
break
94+
fi
95+
done
96+
return 0
97+
}
98+
99+
run_installer() {
100+
# Default permissions
101+
umask 022
102+
103+
[ "$(id -u)" = "0" ] || exit 1
104+
105+
if mount | grep -q 'rootfs' || [ -e /sbin ]; then
106+
export MAGISKTMP=/sbin
107+
else
108+
export MAGISKTMP=/debug_ramdisk
109+
fi
110+
111+
local files_dir_sys=/system/etc/magisk
112+
113+
ui_print "- Extracting Magisk files"
114+
mount_system rw
115+
116+
rm -rf "$files_dir_sys"
117+
mkdir -p "${files_dir_sys}/chromeos/"
118+
119+
ls magisk* *.sh init-ld busybox stub.apk | while read file; do
120+
cp -f "./${file}" "${files_dir_sys}/${file}"
121+
122+
if [ ! -f "${files_dir_sys}/${file}" ]; then
123+
restore_system
124+
abort "! Unable to write system partition"
125+
fi
126+
done
127+
128+
cp -r ./chromeos/* "${files_dir_sys}/chromeos/"
129+
130+
set_perm_recursive "$files_dir_sys" 0 0 0750 0750
131+
132+
rm -rf "$(pwd)"
133+
134+
cat << 'END' > /system/etc/setup-magisk.sh
135+
#!/system/bin/sh
136+
# Magisk On System
137+
# Core: Setup Magisk tmpfs script
138+
139+
# Default permissions
140+
umask 022
141+
142+
exec > /dev/null 2>&1
143+
144+
files_dir_sys=/system/etc/magisk
145+
mods=/data/adb/modules
146+
147+
# Load utility functions
148+
BOOTMODE=true
149+
. "${files_dir_sys}/util_functions.sh"
150+
151+
mount_tmpfs() {
152+
mv magisk magisk.tmp
153+
mount -t tmpfs -o 'mode=0755' magisk "$@"
154+
mv magisk.tmp magisk
155+
}
156+
157+
mount_sbin() {
158+
mount_tmpfs /sbin
159+
chcon u:object_r:rootfs:s0 /sbin
160+
}
161+
162+
[ -n "$(magisk -v)" ] && exit 1
163+
164+
cd /
165+
166+
rm -rf "$MAGISKBIN"
167+
mkdir -p "$MAGISKBIN"
168+
169+
cp -r "$files_dir_sys"/* "$MAGISKBIN"
170+
171+
chmod 755 -R "$MAGISKBIN"
172+
chown 0:0 -R "$MAGISKBIN"
173+
174+
# Setup bin overlay
175+
# from live_setup.sh
176+
if mount | grep -q rootfs; then
177+
# Legacy rootfs
178+
MAGISKTMP=/sbin
179+
180+
mount -o rw,remount /
181+
rm -rf /root
182+
mkdir /root /sbin 2>/dev/null
183+
chmod 750 /root
184+
ln /sbin/* /root
185+
186+
mount -o ro,remount /
187+
mount_sbin
188+
ln -s /root/* /sbin
189+
elif [ -e /sbin ]; then
190+
# Legacy SAR
191+
MAGISKTMP=/sbin
192+
mount_sbin
193+
194+
block="$(mount | grep ' / ' | awk '{ print $1 }')"
195+
[ "$block" = "/dev/root" ] && block=/dev/block/vda1
196+
mkdir -p /dev/sysroot
197+
mount -o ro "$block" /dev/sysroot
198+
199+
for file in /dev/sysroot/sbin/*; do
200+
[ ! -e "$file" ] && break
201+
202+
if [ -L "$file" ]; then
203+
cp -af "$file" /sbin
204+
else
205+
file_sbin="/sbin/$(basename "$file")"
206+
207+
touch "$file_sbin"
208+
mount -o bind "$file" "$file_sbin"
209+
fi
210+
done
211+
212+
umount -l /dev/sysroot
213+
rm -rf /dev/sysroot
214+
else
215+
# Android Q+ without sbin
216+
MAGISKTMP=/debug_ramdisk
217+
mount_tmpfs /debug_ramdisk
218+
fi
219+
220+
if [ ! -L /cache ] && ! mount | grep -q ' /cache '; then
221+
mount -t tmpfs -o 'mode=0755' tmpfs /cache
222+
fi
223+
224+
# Magisk stuffs
225+
for dir in "device" "worker"; do
226+
mkdir -p "${MAGISKTMP}/.magisk/${dir}/"
227+
done
228+
229+
mount_tmpfs "${MAGISKTMP}/.magisk/worker/"
230+
mount --make-private "${MAGISKTMP}/.magisk/worker/"
231+
232+
cd "$files_dir_sys"
233+
234+
touch "${MAGISKTMP}/.magisk/.config"
235+
236+
for file in "magisk" "magisk32" "magiskpolicy" "stub.apk"; do
237+
cp "./${file}" "${MAGISKTMP}/${file}"
238+
set_perm "${MAGISKTMP}/${file}" 0 0 0755
239+
done
240+
241+
cd "$MAGISKTMP"
242+
ln -s magisk resetprop
243+
ln -s magiskpolicy supolicy
244+
ln -s magisk su
245+
246+
cd /
247+
248+
# SELinux stuffs
249+
if [ -d /sys/fs/selinux/ ]; then
250+
apply_cmd="--live --magisk"
251+
252+
if [ ! -e /sys/fs/selinux/policy ]; then
253+
for sepolicy_file in /sepolicy /sepolicy_debug /sepolicy.unlocked /system/etc/selinux/precompiled_sepolicy /vendor/etc/selinux/precompiled_sepolicy /odm/etc/selinux/precompiled_sepolicy; do
254+
[ -f "$sepolicy_file" ] || continue
255+
apply_cmd="--load ${sepolicy_file} ${apply_cmd}"
256+
break
257+
done
258+
fi
259+
260+
ls "$mods" | while read modid; do
261+
modpath="${mods}_update/${modid}"
262+
[ -d "$modpath" ] || modpath="${mods}/${modid}"
263+
264+
[ -f "${modpath}/disable" ] && continue
265+
[ -f "${modpath}/remove" ] && continue
266+
[ -f "${modpath}/sepolicy.rule" ] || continue
267+
268+
apply_cmd="${apply_cmd} --apply ${modpath}/sepolicy.rule"
269+
done
270+
271+
"${MAGISKTMP}/magiskpolicy" ${apply_cmd}
272+
fi
273+
274+
exit 0
275+
END
276+
277+
set_perm /system/etc/setup-magisk.sh 0 0 0750
278+
279+
local exec_sectx="$(id -Z)"
280+
[ -z "$exec_sectx" ] && local exec_sectx="u:r:init:s0"
281+
282+
cat << END > /system/etc/init/magisk.rc
283+
on post-fs-data
284+
exec ${exec_sectx} 0 0 -- ${files_dir_sys}/busybox sh -o standalone /system/etc/setup-magisk.sh
285+
exec u:r:magisk:s0 0 0 -- ${MAGISKTMP}/magisk --post-fs-data
286+
287+
on property:vold.decrypt=trigger_restart_framework
288+
exec u:r:magisk:s0 0 0 -- ${MAGISKTMP}/magisk --service
289+
290+
on nonencrypted
291+
exec u:r:magisk:s0 0 0 -- ${MAGISKTMP}/magisk --service
292+
293+
on property:sys.boot_completed=1
294+
exec u:r:magisk:s0 0 0 -- ${MAGISKTMP}/magisk --boot-complete
295+
296+
on property:init.svc.zygote=stopped
297+
exec u:r:magisk:s0 0 0 -- ${MAGISKTMP}/magisk --zygote-restart
298+
END
299+
300+
set_perm /system/etc/init/magisk.rc 0 0 0644
301+
302+
mount_system ro
303+
304+
if [ -n "$(magisk -v >&2)" ]; then
305+
ui_print "! Magisk daemon is running"
306+
ui_print "- Welcome to Magisk On System"
307+
return 0
308+
fi
309+
310+
ui_print "- Launch Magisk daemon"
311+
cd /
312+
sh /system/etc/setup-magisk.sh
313+
314+
for trigger in "post-fs-data" "service" "boot-complete"; do
315+
sleep 0.5s
316+
"${MAGISKTMP}/magisk" --${trigger} >&2
317+
done
318+
319+
sleep 0.5s
320+
echo $(magisk -v)
321+
ui_print "- Welcome to Magisk On System"
79322
return 0
80323
}
81324

82-
# $1 = uninstaller zip
83325
run_uninstaller() {
84-
rm -rf /dev/tmp
85-
mkdir -p /dev/tmp/install
86-
unzip -o "$1" "assets/*" "lib/*" -d /dev/tmp/install
87-
INSTALLER=/dev/tmp/install sh /dev/tmp/install/assets/uninstaller.sh dummy 1 "$1"
326+
# Default permissions
327+
umask 022
328+
329+
if echo ${MAGISK_VER} | grep -q '\.'; then
330+
local PRETTY_VER=${MAGISK_VER}
331+
else
332+
local PRETTY_VER="${MAGISK_VER}(${MAGISK_VER_CODE})"
333+
fi
334+
print_title "Magisk ${PRETTY_VER} Uninstaller"
335+
336+
ui_print "- Removing modules"
337+
magisk --remove-modules -n
338+
339+
ui_print "- Removing Magisk files"
340+
rm -rf \
341+
/cache/*magisk* /cache/unblock /data/*magisk* /data/cache/*magisk* /data/property/*magisk* \
342+
/data/Magisk.apk /data/busybox /data/custom_ramdisk_patch.sh /data/adb/*magisk* \
343+
/data/adb/post-fs-data.d /data/adb/service.d /data/adb/modules* \
344+
/data/unencrypted/magisk /metadata/magisk /metadata/watchdog/magisk /persist/magisk /mnt/vendor/persist/magisk
345+
346+
ui_print "- Restoring system partition"
347+
restore_system
348+
349+
if [ -d /system/etc/magisk/ ]; then
350+
abort "! Unable to restore system partition"
351+
fi
352+
353+
return 0
354+
}
355+
356+
restore_system() {
357+
mount_system rw
358+
359+
rm -rf \
360+
/system/etc/magisk/ \
361+
/system/etc/init/magisk.rc \
362+
/system/etc/setup-magisk.sh
363+
364+
mount_system ro
365+
return 0
88366
}
89367

368+
# $1 = uninstaller zip
369+
#run_uninstaller() {
370+
# rm -rf /dev/tmp
371+
# mkdir -p /dev/tmp/install
372+
# unzip -o "$1" "assets/*" "lib/*" -d /dev/tmp/install
373+
# INSTALLER=/dev/tmp/install sh /dev/tmp/install/assets/uninstaller.sh dummy 1 "$1"
374+
#}
375+
90376
# $1 = boot partition
91377
restore_imgs() {
92-
local SHA1=$(grep_prop SHA1 $MAGISKTMP/.magisk/config)
93-
local BACKUPDIR=/data/magisk_backup_$SHA1
94-
[ -d $BACKUPDIR ] || return 1
95-
[ -f $BACKUPDIR/boot.img.gz ] || return 1
96-
flash_image $BACKUPDIR/boot.img.gz $1
378+
# local SHA1=$(grep_prop SHA1 $MAGISKTMP/.magisk/config)
379+
# local BACKUPDIR=/data/magisk_backup_$SHA1
380+
# [ -d $BACKUPDIR ] || return 1
381+
# [ -f $BACKUPDIR/boot.img.gz ] || return 1
382+
# flash_image $BACKUPDIR/boot.img.gz $1
383+
restore_system
97384
}
98385

99386
# $1 = path to bootctl executable
100387
post_ota() {
101-
cd /data/adb
102-
cp -f $1 bootctl
103-
rm -f $1
104-
chmod 755 bootctl
105-
if ! ./bootctl hal-info; then
106-
rm -f bootctl
107-
return
108-
fi
109-
SLOT_NUM=0
110-
[ $(./bootctl get-current-slot) -eq 0 ] && SLOT_NUM=1
111-
./bootctl set-active-boot-slot $SLOT_NUM
112-
cat << EOF > post-fs-data.d/post_ota.sh
113-
/data/adb/bootctl mark-boot-successful
114-
rm -f /data/adb/bootctl
115-
rm -f /data/adb/post-fs-data.d/post_ota.sh
116-
EOF
117-
chmod 755 post-fs-data.d/post_ota.sh
118-
cd /
388+
# cd /data/adb
389+
# cp -f $1 bootctl
390+
# rm -f $1
391+
# chmod 755 bootctl
392+
# if ! ./bootctl hal-info; then
393+
# rm -f bootctl
394+
# return
395+
# fi
396+
# SLOT_NUM=0
397+
# [ $(./bootctl get-current-slot) -eq 0 ] && SLOT_NUM=1
398+
# ./bootctl set-active-boot-slot $SLOT_NUM
399+
# cat << EOF > post-fs-data.d/post_ota.sh
400+
#/data/adb/bootctl mark-boot-successful
401+
#rm -f /data/adb/bootctl
402+
#rm -f /data/adb/post-fs-data.d/post_ota.sh
403+
#EOF
404+
# chmod 755 post-fs-data.d/post_ota.sh
405+
# cd /
406+
return 0
119407
}
120408

121409
# $1 = APK
@@ -135,16 +423,20 @@ adb_pm_install() {
135423

136424
check_boot_ramdisk() {
137425
# Create boolean ISAB
138-
ISAB=true
139-
[ -z $SLOT ] && ISAB=false
426+
#ISAB=true
427+
#[ -z $SLOT ] && ISAB=false
428+
ISAB=false
140429

141430
# If we are A/B, then we must have ramdisk
142431
$ISAB && return 0
143432

144433
# If we are using legacy SAR, but not A/B, assume we do not have ramdisk
145434
if $LEGACYSAR; then
146435
# Override recovery mode to true
147-
RECOVERYMODE=true
436+
#RECOVERYMODE=true
437+
438+
# Override system mode to true
439+
SYSTEMMODE=true
148440
return 1
149441
fi
150442

@@ -213,7 +505,8 @@ get_flags() {
213505
else
214506
PATCHVBMETAFLAG=true
215507
fi
216-
[ -z $RECOVERYMODE ] && RECOVERYMODE=false
508+
#[ -z $RECOVERYMODE ] && RECOVERYMODE=false
509+
[ -z $SYSTEMMODE ] && SYSTEMMODE=false
217510
[ -z $VENDORBOOT ] && VENDORBOOT=false
218511
}
219512

@@ -241,7 +534,8 @@ app_init() {
241534
printvar CRYPTOTYPE
242535
printvar PATCHVBMETAFLAG
243536
printvar LEGACYSAR
244-
printvar RECOVERYMODE
537+
#printvar RECOVERYMODE
538+
printvar SYSTEMMODE
245539
printvar KEEPVERITY
246540
printvar KEEPFORCEENCRYPT
247541
printvar VENDORBOOT

0 commit comments

Comments
 (0)
Please sign in to comment.