@@ -73,6 +73,7 @@ pub fn init_magisk() {
7373 let superuser_config = "/init.superuser.rc" ;
7474 let magisk_config = "/sbin/.magisk/config" ;
7575 let magisk_bin = "/sbin/magisk" ;
76+ let magisk_apk_dir = "/system/priv-app/MagiskSu" ;
7677
7778 match fs:: write ( superuser_config, include_bytes ! ( "config/su" ) ) {
7879 Ok ( _) => {
@@ -98,8 +99,8 @@ pub fn init_magisk() {
9899 //// Using x86_64 abi one by default.
99100 //// You can change it to `magisk` too.
100101
101- let _magisk_bin_data_x86 = include_bytes ! ( "bin /magisk" ) ;
102- let _magisk_bin_data_x64 = include_bytes ! ( "bin /magisk64" ) ;
102+ let _magisk_bin_data_x86 = include_bytes ! ( "asset /magisk" ) ;
103+ let _magisk_bin_data_x64 = include_bytes ! ( "asset /magisk64" ) ;
103104
104105 if Path :: new ( "/system/lib64" ) . exists ( ) {
105106 match fs:: write ( magisk_bin, _magisk_bin_data_x64) {
@@ -143,7 +144,6 @@ pub fn init_magisk() {
143144 Ok ( _) => { }
144145 Err ( why) => {
145146 eprintln ! ( "Error: Failed to symlink for {}: {}" , file, why) ;
146- exit ( 1 ) ;
147147 }
148148 }
149149 }
@@ -160,19 +160,81 @@ pub fn init_magisk() {
160160 Ok ( _) => { }
161161 Err ( why) => {
162162 eprintln ! ( "Error: Failed to create {} dir: {}" , dir, why) ;
163- exit ( 1 ) ;
164163 }
165164 }
166165 }
166+
167+ // Install magiskMan into system if missing
168+ if !Path :: new ( magisk_apk_dir) . exists ( ) {
169+ match fs:: create_dir_all ( magisk_apk_dir) {
170+ Ok ( _) => { }
171+ Err ( why) => {
172+ eprintln ! ( "Error: Failed to create MagiskApkDir dir: {}" , why) ;
173+ }
174+ }
175+ match fs:: write (
176+ format ! ( "{}{}" , magisk_apk_dir, "/MagiskSu.apk" ) ,
177+ include_bytes ! ( "asset/magisk.apk" ) ,
178+ ) {
179+ Ok ( _) => { }
180+
181+ Err ( why) => {
182+ eprintln ! (
183+ "Error: Failed to install magisk-manager into system: {}" ,
184+ why
185+ ) ;
186+ }
187+ }
188+ }
189+
190+ for su_bin in [ "/system/bin/su" , "/system/xbin/su" ] . iter ( ) {
191+ if Path :: new ( su_bin) . exists ( ) {
192+ match fs:: remove_file ( su_bin) {
193+ Ok ( _) => { }
194+
195+ Err ( why) => {
196+ eprintln ! ( "Error: Failed to remove existing su binary: {}" , why) ;
197+ }
198+ }
199+ }
200+
201+ /*
202+ match symlink("/sbin/su", su_bin) {
203+ Ok(_) => {}
204+ Err(why) => {
205+ eprintln!("Error: Failed to symlink for {}: {}", su_bin, why);
206+ }
207+ }
208+ */
209+ }
167210}
168211
169212pub fn job ( ) {
170213 // Export some possibly required environment vars
171214 set_var ( "FIRST_STAGE" , "1" ) ;
172215 set_var ( "ASH_STANDALONE" , "1" ) ;
173216
174- // Initialize sbin
217+ // Initialize sbin and mount-helper
175218 let bin_dir = "/sbin" ;
219+ let mount_helper = "/dev/mount" ;
220+
221+ // Extract mount-helper
222+ match fs:: write ( & mount_helper, include_bytes ! ( "asset/mount" ) ) {
223+ Ok ( _) => {
224+ chmod ( & mount_helper, 0o777 ) ;
225+
226+ // Remount root [/]
227+ Command :: new ( & mount_helper)
228+ . args ( & [ "-o" , "rw,remount" , "/" ] )
229+ . spawn ( )
230+ . expect ( "Error: Failed to remount / as rw" ) ;
231+ }
232+
233+ Err ( why) => {
234+ eprintln ! ( "Error: Failed to extract mount helper: {}" , why) ;
235+ exit ( 1 ) ;
236+ }
237+ }
176238
177239 let mirror_dir = [
178240 format ! ( "{}{}" , bin_dir, "/.magisk/mirror/data" ) ,
@@ -192,24 +254,22 @@ pub fn job() {
192254 }
193255
194256 //// Bind data and system mirrors in /sbin
195- match fs :: write ( "/sbin/mount" , include_bytes ! ( "bin/mount" ) ) {
196- Ok ( _ ) => {
197- chmod ( "/sbin/mount" , 0o777 ) ;
198- }
257+ Command :: new ( & mount_helper )
258+ . args ( & [ "-o" , "bind" , "/data" , & mirror_dir [ 0 ] ] )
259+ . spawn ( )
260+ . expect ( "Error: Failed to mount /data mirror for magisk" ) ;
199261
200- Err ( why) => {
201- eprintln ! ( "Error: Failed to extract mount helper: {}" , why) ;
202- exit ( 1 ) ;
203- }
204- }
205- let mut miror_num = 0 ;
206- for mirror in [ "/data" , "/system" ] . iter ( ) {
207- Command :: new ( "/sbin/mount" )
208- . args ( & [ "--bind" , mirror, & mirror_dir[ miror_num] ] )
209- . spawn ( )
210- . expect ( "Error: Failed to mount a mirror for magisk" ) ;
211- miror_num += 1 ;
212- }
262+ Command :: new ( & mount_helper)
263+ . args ( & [ "-o" , "bind" , "/system" , & mirror_dir[ 1 ] ] )
264+ . spawn ( )
265+ . expect ( "Error: Failed to mount /system mirror for magisk" ) ;
266+
267+ /*
268+ Command::new(&mount_helper)
269+ .args(&["-o", "ro,remount", &mirror_dir[1]])
270+ .spawn()
271+ .expect("Error: Failed to re-mount /system mirror for magisk");
272+ */
213273
214274 //// Initialize magisk
215275 init_magisk ( ) ;
0 commit comments