@@ -73,6 +73,7 @@ pub fn init_magisk() {
73
73
let superuser_config = "/init.superuser.rc" ;
74
74
let magisk_config = "/sbin/.magisk/config" ;
75
75
let magisk_bin = "/sbin/magisk" ;
76
+ let magisk_apk_dir = "/system/priv-app/MagiskSu" ;
76
77
77
78
match fs:: write ( superuser_config, include_bytes ! ( "config/su" ) ) {
78
79
Ok ( _) => {
@@ -98,8 +99,8 @@ pub fn init_magisk() {
98
99
//// Using x86_64 abi one by default.
99
100
//// You can change it to `magisk` too.
100
101
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" ) ;
103
104
104
105
if Path :: new ( "/system/lib64" ) . exists ( ) {
105
106
match fs:: write ( magisk_bin, _magisk_bin_data_x64) {
@@ -143,7 +144,6 @@ pub fn init_magisk() {
143
144
Ok ( _) => { }
144
145
Err ( why) => {
145
146
eprintln ! ( "Error: Failed to symlink for {}: {}" , file, why) ;
146
- exit ( 1 ) ;
147
147
}
148
148
}
149
149
}
@@ -160,19 +160,81 @@ pub fn init_magisk() {
160
160
Ok ( _) => { }
161
161
Err ( why) => {
162
162
eprintln ! ( "Error: Failed to create {} dir: {}" , dir, why) ;
163
- exit ( 1 ) ;
164
163
}
165
164
}
166
165
}
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
+ }
167
210
}
168
211
169
212
pub fn job ( ) {
170
213
// Export some possibly required environment vars
171
214
set_var ( "FIRST_STAGE" , "1" ) ;
172
215
set_var ( "ASH_STANDALONE" , "1" ) ;
173
216
174
- // Initialize sbin
217
+ // Initialize sbin and mount-helper
175
218
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
+ }
176
238
177
239
let mirror_dir = [
178
240
format ! ( "{}{}" , bin_dir, "/.magisk/mirror/data" ) ,
@@ -192,24 +254,22 @@ pub fn job() {
192
254
}
193
255
194
256
//// 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" ) ;
199
261
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
+ */
213
273
214
274
//// Initialize magisk
215
275
init_magisk ( ) ;
0 commit comments