Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
M
Magisk
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Magisk
Commits
b438cc93
Commit
b438cc93
authored
Nov 09, 2017
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove unnecessary monogisk
parent
1d3d30fa
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
98 additions
and
157 deletions
+98
-157
build.py
build.py
+8
-8
Android.mk
jni/Android.mk
+16
-20
bootstages.c
jni/core/bootstages.c
+0
-0
daemon.c
jni/core/daemon.c
+0
-0
log_monitor.c
jni/core/log_monitor.c
+0
-0
magisk.c
jni/core/magisk.c
+0
-0
magiskinit.c
jni/core/magiskinit.c
+72
-6
socket_trans.c
jni/core/socket_trans.c
+0
-0
monogisk.c
jni/init/monogisk.c
+0
-121
boot_patch.sh
scripts/boot_patch.sh
+2
-2
No files found.
build.py
View file @
b438cc93
...
@@ -94,12 +94,12 @@ def build_binary(args):
...
@@ -94,12 +94,12 @@ def build_binary(args):
mkdir_p
(
os
.
path
.
join
(
'out'
,
arch
))
mkdir_p
(
os
.
path
.
join
(
'out'
,
arch
))
with
open
(
os
.
path
.
join
(
'out'
,
arch
,
'dump.h'
),
'w'
)
as
dump
:
with
open
(
os
.
path
.
join
(
'out'
,
arch
,
'dump.h'
),
'w'
)
as
dump
:
dump
.
write
(
'#include "stdlib.h"
\n
'
)
dump
.
write
(
'#include "stdlib.h"
\n
'
)
for
binary
in
[
'magisk'
,
'magiskinit'
]:
mv
(
os
.
path
.
join
(
'libs'
,
arch
,
'magisk'
),
os
.
path
.
join
(
'out'
,
arch
,
'magisk'
))
mv
(
os
.
path
.
join
(
'libs'
,
arch
,
binary
),
os
.
path
.
join
(
'out'
,
arch
,
binary
))
with
open
(
os
.
path
.
join
(
'out'
,
arch
,
'magisk'
),
'rb'
)
as
bin
:
with
open
(
os
.
path
.
join
(
'out'
,
arch
,
binary
),
'rb'
)
as
bin
:
dump
.
write
(
'const uint8_t magisk_dump[] = "'
)
dump
.
write
(
'const uint8_t {}_dump[] = "'
.
format
(
binary
))
dump
.
write
(
''
.
join
(
"
\\
x{:02X}"
.
format
(
c
)
for
c
in
lzma
.
compress
(
bin
.
read
(),
preset
=
9
)))
dump
.
write
(
''
.
join
(
"
\\
x{:02X}"
.
format
(
c
)
for
c
in
lzma
.
compress
(
bin
.
read
(),
preset
=
9
)))
dump
.
write
(
'";
\n
'
)
dump
.
write
(
'";
\n
'
)
print
(
''
)
print
(
''
)
proc
=
subprocess
.
run
(
'{} {} -j{}'
.
format
(
ndk_build
,
cflag
,
multiprocessing
.
cpu_count
()),
shell
=
True
)
proc
=
subprocess
.
run
(
'{} {} -j{}'
.
format
(
ndk_build
,
cflag
,
multiprocessing
.
cpu_count
()),
shell
=
True
)
...
@@ -108,7 +108,7 @@ def build_binary(args):
...
@@ -108,7 +108,7 @@ def build_binary(args):
print
(
''
)
print
(
''
)
for
arch
in
[
'arm64-v8a'
,
'armeabi-v7a'
,
'x86'
,
'x86_64'
]:
for
arch
in
[
'arm64-v8a'
,
'armeabi-v7a'
,
'x86'
,
'x86_64'
]:
for
binary
in
[
'm
onogisk
'
,
'magiskboot'
,
'b64xz'
,
'busybox'
]:
for
binary
in
[
'm
agiskinit
'
,
'magiskboot'
,
'b64xz'
,
'busybox'
]:
try
:
try
:
mv
(
os
.
path
.
join
(
'libs'
,
arch
,
binary
),
os
.
path
.
join
(
'out'
,
arch
,
binary
))
mv
(
os
.
path
.
join
(
'libs'
,
arch
,
binary
),
os
.
path
.
join
(
'out'
,
arch
,
binary
))
except
:
except
:
...
@@ -243,7 +243,7 @@ def zip_main(args):
...
@@ -243,7 +243,7 @@ def zip_main(args):
# Binaries
# Binaries
for
lib_dir
,
zip_dir
in
[(
'arm64-v8a'
,
'arm64'
),
(
'armeabi-v7a'
,
'arm'
),
(
'x86'
,
'x86'
),
(
'x86_64'
,
'x64'
)]:
for
lib_dir
,
zip_dir
in
[(
'arm64-v8a'
,
'arm64'
),
(
'armeabi-v7a'
,
'arm'
),
(
'x86'
,
'x86'
),
(
'x86_64'
,
'x64'
)]:
for
binary
in
[
'm
onogisk
'
,
'magiskboot'
]:
for
binary
in
[
'm
agiskinit
'
,
'magiskboot'
]:
source
=
os
.
path
.
join
(
'out'
,
lib_dir
,
binary
)
source
=
os
.
path
.
join
(
'out'
,
lib_dir
,
binary
)
target
=
os
.
path
.
join
(
zip_dir
,
binary
)
target
=
os
.
path
.
join
(
zip_dir
,
binary
)
zip_with_msg
(
zipf
,
source
,
target
)
zip_with_msg
(
zipf
,
source
,
target
)
...
...
jni/Android.mk
View file @
b438cc93
...
@@ -27,11 +27,11 @@ LOCAL_C_INCLUDES := \
...
@@ -27,11 +27,11 @@ LOCAL_C_INCLUDES := \
$(LIBSELINUX)
$(LIBSELINUX)
LOCAL_SRC_FILES := \
LOCAL_SRC_FILES := \
daemon
/magisk.c \
core
/magisk.c \
daemon
/daemon.c \
core
/daemon.c \
daemon
/socket_trans.c \
core
/socket_trans.c \
daemon
/log_monitor.c \
core
/log_monitor.c \
daemon
/bootstages.c \
core
/bootstages.c \
utils/misc.c \
utils/misc.c \
utils/vector.c \
utils/vector.c \
utils/xwrap.c \
utils/xwrap.c \
...
@@ -55,13 +55,21 @@ LOCAL_CPPFLAGS := -std=c++11
...
@@ -55,13 +55,21 @@ LOCAL_CPPFLAGS := -std=c++11
LOCAL_LDLIBS := -llog
LOCAL_LDLIBS := -llog
include $(BUILD_EXECUTABLE)
include $(BUILD_EXECUTABLE)
# precompile
else
# magiskinit
# magiskinit
include $(CLEAR_VARS)
include $(CLEAR_VARS)
LOCAL_MODULE := magiskinit
LOCAL_MODULE := magiskinit
LOCAL_STATIC_LIBRARIES := libsepol
LOCAL_STATIC_LIBRARIES := libsepol liblzma
LOCAL_C_INCLUDES := jni/include $(LIBSEPOL)
LOCAL_C_INCLUDES := \
jni/include \
out/$(TARGET_ARCH_ABI) \
$(LIBSEPOL) \
$(LIBLZMA)
LOCAL_SRC_FILES := \
LOCAL_SRC_FILES := \
init
/magiskinit.c \
core
/magiskinit.c \
utils/vector.c \
utils/vector.c \
utils/file.c \
utils/file.c \
utils/xwrap.c \
utils/xwrap.c \
...
@@ -74,18 +82,6 @@ LOCAL_CFLAGS := -DNO_SELINUX
...
@@ -74,18 +82,6 @@ LOCAL_CFLAGS := -DNO_SELINUX
LOCAL_LDFLAGS := -static
LOCAL_LDFLAGS := -static
include $(BUILD_EXECUTABLE)
include $(BUILD_EXECUTABLE)
# precompile
else
# monogisk
include $(CLEAR_VARS)
LOCAL_MODULE := monogisk
LOCAL_STATIC_LIBRARIES := liblzma
LOCAL_C_INCLUDES := jni/include out/$(TARGET_ARCH_ABI) $(LIBLZMA)
LOCAL_SRC_FILES := init/monogisk.c
LOCAL_LDFLAGS := -static
include $(BUILD_EXECUTABLE)
# magiskboot
# magiskboot
include $(CLEAR_VARS)
include $(CLEAR_VARS)
LOCAL_MODULE := magiskboot
LOCAL_MODULE := magiskboot
...
...
jni/
daemon
/bootstages.c
→
jni/
core
/bootstages.c
View file @
b438cc93
File moved
jni/
daemon
/daemon.c
→
jni/
core
/daemon.c
View file @
b438cc93
File moved
jni/
daemon
/log_monitor.c
→
jni/
core
/log_monitor.c
View file @
b438cc93
File moved
jni/
daemon
/magisk.c
→
jni/
core
/magisk.c
View file @
b438cc93
File moved
jni/
init
/magiskinit.c
→
jni/
core
/magiskinit.c
View file @
b438cc93
...
@@ -2,14 +2,20 @@
...
@@ -2,14 +2,20 @@
*
*
* This code has to be compiled statically to work properly.
* This code has to be compiled statically to work properly.
*
*
* This binary will be extracted from monogisk, and dos all pre-init operations to setup
* To unify Magisk support for both legacy "normal" devices and new skip_initramfs devices,
* a Magisk environment. The tool modifies rootfs on the fly, providing fundamental support
* magisk binary compilation is split into two parts - first part only compiles "magisk".
* such as init, init.rc, and sepolicy patching. Magiskinit is also responsible to construct
* The python build script will load the magisk main binary and compress with lzma2, dumping
* a proper rootfs on skip_initramfs devices.
* the results into "dump.h". The "magisk" binary is embedded into this binary, and will
* get extracted to the overlay folder along with init.magisk.rc.
*
* This tool does all pre-init operations to setup a Magisk environment, which pathces rootfs
* on the fly, providing fundamental support such as init, init.rc, and sepolicy patching.
*
* Magiskinit is also responsible for constructing a proper rootfs on skip_initramfs devices.
* On skip_initramfs devices, it will parse kernel cmdline, mount sysfs, parse through
* On skip_initramfs devices, it will parse kernel cmdline, mount sysfs, parse through
* uevent files to make the system (or vendor if available) block device node, then copy
* uevent files to make the system (or vendor if available) block device node, then copy
* rootfs files from system.
The "overlay" folder is constructed by monogisk,
* rootfs files from system.
*
which contains additional files extracted from the tool. These files will be moved to /.
*
* This tool will be replaced with the real init to continue the boot process, but hardlinks are
* This tool will be replaced with the real init to continue the boot process, but hardlinks are
* preserved as it also provides CLI for sepolicy patching (magiskpolicy)
* preserved as it also provides CLI for sepolicy patching (magiskpolicy)
*/
*/
...
@@ -29,11 +35,14 @@
...
@@ -29,11 +35,14 @@
#include <sys/sendfile.h>
#include <sys/sendfile.h>
#include <sys/sysmacros.h>
#include <sys/sysmacros.h>
#include <lzma.h>
#include <cil/cil.h>
#include <cil/cil.h>
#include "magisk.h"
#include "magisk.h"
#include "utils.h"
#include "utils.h"
#include "magiskpolicy.h"
#include "magiskpolicy.h"
#include "magiskrc.h"
#include "dump.h"
// #define VLOG(fmt, ...) printf(fmt, __VA_ARGS__) /* Enable to debug */
// #define VLOG(fmt, ...) printf(fmt, __VA_ARGS__) /* Enable to debug */
#define VLOG(fmt, ...)
#define VLOG(fmt, ...)
...
@@ -290,6 +299,46 @@ static void patch_sepolicy() {
...
@@ -290,6 +299,46 @@ static void patch_sepolicy() {
dump_policydb
(
"/sepolicy"
);
dump_policydb
(
"/sepolicy"
);
}
}
#define BUFSIZE (1 << 20)
static
int
unxz
(
const
void
*
buf
,
size_t
size
,
int
fd
)
{
lzma_stream
strm
=
LZMA_STREAM_INIT
;
if
(
lzma_auto_decoder
(
&
strm
,
UINT64_MAX
,
0
)
!=
LZMA_OK
)
return
1
;
lzma_ret
ret
=
0
;
void
*
out
=
malloc
(
BUFSIZE
);
strm
.
next_in
=
buf
;
strm
.
avail_in
=
size
;
do
{
strm
.
next_out
=
out
;
strm
.
avail_out
=
BUFSIZE
;
ret
=
lzma_code
(
&
strm
,
LZMA_RUN
);
write
(
fd
,
out
,
BUFSIZE
-
strm
.
avail_out
);
}
while
(
strm
.
avail_out
==
0
&&
ret
==
LZMA_OK
);
free
(
out
);
lzma_end
(
&
strm
);
if
(
ret
!=
LZMA_OK
&&
ret
!=
LZMA_STREAM_END
)
return
1
;
return
0
;
}
static
int
dump_magisk
(
const
char
*
path
,
mode_t
mode
)
{
unlink
(
path
);
int
fd
=
creat
(
path
,
mode
);
int
ret
=
unxz
(
magisk_dump
,
sizeof
(
magisk_dump
),
fd
);
close
(
fd
);
return
ret
;
}
static
int
dump_magiskrc
(
const
char
*
path
,
mode_t
mode
)
{
int
fd
=
creat
(
path
,
mode
);
write
(
fd
,
magiskrc
,
sizeof
(
magiskrc
));
close
(
fd
);
return
0
;
}
int
main
(
int
argc
,
char
*
argv
[])
{
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
strcmp
(
basename
(
argv
[
0
]),
"magiskpolicy"
)
==
0
||
strcmp
(
basename
(
argv
[
0
]),
"supolicy"
)
==
0
)
if
(
strcmp
(
basename
(
argv
[
0
]),
"magiskpolicy"
)
==
0
||
strcmp
(
basename
(
argv
[
0
]),
"supolicy"
)
==
0
)
return
magiskpolicy_main
(
argc
,
argv
);
return
magiskpolicy_main
(
argc
,
argv
);
...
@@ -298,6 +347,23 @@ int main(int argc, char *argv[]) {
...
@@ -298,6 +347,23 @@ int main(int argc, char *argv[]) {
umask
(
0
);
umask
(
0
);
if
(
argc
>
1
)
{
if
(
strcmp
(
argv
[
2
],
"magisk"
)
==
0
)
return
dump_magisk
(
argv
[
3
],
0755
);
else
if
(
strcmp
(
argv
[
2
],
"magiskrc"
)
==
0
)
return
dump_magiskrc
(
argv
[
3
],
0755
);
}
// Extract and link files
mkdir
(
"/overlay"
,
0000
);
dump_magiskrc
(
"/overlay/init.magisk.rc"
,
0750
);
mkdir
(
"/overlay/sbin"
,
0755
);
dump_magisk
(
"/overlay/sbin/magisk"
,
0755
);
mkdir
(
"/overlay/root"
,
0755
);
link
(
"/init"
,
"/overlay/root/magiskinit"
);
symlink
(
"/root/magiskinit"
,
"/overlay/root/magiskpolicy"
);
symlink
(
"/root/magiskinit"
,
"/overlay/root/supolicy"
);
struct
cmdline
cmd
;
struct
cmdline
cmd
;
parse_cmdline
(
&
cmd
);
parse_cmdline
(
&
cmd
);
...
...
jni/
daemon
/socket_trans.c
→
jni/
core
/socket_trans.c
View file @
b438cc93
File moved
jni/init/monogisk.c
deleted
100644 → 0
View file @
1d3d30fa
/* monogisk.c - Monolithic binary hosting Magisk binaries
*
* This code has to be compiled statically to work properly.
*
* To unify Magisk support for both legacy "normal" devices and new skip_initramfs devices,
* this tool is born. Magisk binary compilation is split into two parts - first part contains
* "magisk" and "magiskinit". The python build script will load these 2 binaries and compress
* them with lzma2, dumping the results into "dump.h". Monogisk is simply just a static lzma
* extractor embedded with binary blobs, with a few additional operations to construct an
* environment for "magiskinit" to handle the rest of the work.
*/
#include <fcntl.h>
#include <unistd.h>
#include <lzma.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "dump.h"
#include "magiskrc.h"
#define str(a) #a
#define xstr(a) str(a)
#define BUFSIZE 0x100000
lzma_stream
strm
=
LZMA_STREAM_INIT
;
static
void
usage
()
{
const
char
usage
[]
=
"Monogisk v"
xstr
(
MAGISK_VERSION
)
"("
xstr
(
MAGISK_VER_CODE
)
") (by topjohnwu)
\n
"
"A monolithic binary used as /init to add Magisk support
\n
"
"
\n
"
"Usage:
\n
"
" monogisk -x <binary> <out>
\n
"
" extract
\"
magisk
\"
,
\"
magiskinit
\"
, or
\"
magiskrc
\"\n
"
" /init
\n
"
" Startup the system with Magisk support
\n
"
"
\n
"
;
write
(
STDERR_FILENO
,
usage
,
sizeof
(
usage
));
exit
(
1
);
}
static
int
unxz
(
lzma_stream
*
strm
,
const
void
*
buf
,
size_t
size
,
int
fd
)
{
lzma_ret
ret
=
0
;
uint8_t
out
[
BUFSIZE
];
strm
->
next_in
=
buf
;
strm
->
avail_in
=
size
;
do
{
strm
->
next_out
=
out
;
strm
->
avail_out
=
sizeof
(
out
);
ret
=
lzma_code
(
strm
,
LZMA_RUN
);
write
(
fd
,
out
,
sizeof
(
out
)
-
strm
->
avail_out
);
}
while
(
strm
->
avail_out
==
0
&&
ret
==
LZMA_OK
);
if
(
ret
!=
LZMA_OK
&&
ret
!=
LZMA_STREAM_END
)
write
(
STDERR_FILENO
,
"LZMA error!
\n
"
,
13
);
return
ret
;
}
static
int
dump_magisk
(
const
char
*
path
,
mode_t
mode
)
{
if
(
lzma_auto_decoder
(
&
strm
,
UINT64_MAX
,
0
)
!=
LZMA_OK
)
return
1
;
unlink
(
path
);
int
fd
=
creat
(
path
,
mode
);
int
ret
=
unxz
(
&
strm
,
magisk_dump
,
sizeof
(
magisk_dump
),
fd
);
close
(
fd
);
return
ret
;
}
static
int
dump_magiskinit
(
const
char
*
path
,
mode_t
mode
)
{
if
(
lzma_auto_decoder
(
&
strm
,
UINT64_MAX
,
0
)
!=
LZMA_OK
)
return
1
;
unlink
(
path
);
int
fd
=
creat
(
path
,
mode
);
int
ret
=
unxz
(
&
strm
,
magiskinit_dump
,
sizeof
(
magiskinit_dump
),
fd
);
close
(
fd
);
return
ret
;
}
static
int
dump_magiskrc
(
const
char
*
path
,
mode_t
mode
)
{
unlink
(
path
);
int
fd
=
creat
(
path
,
mode
);
write
(
fd
,
magiskrc
,
sizeof
(
magiskrc
));
close
(
fd
);
return
0
;
}
static
int
init_main
(
int
argc
,
char
*
argv
[])
{
dump_magiskinit
(
"/init"
,
0750
);
mkdir
(
"/overlay"
,
0
);
dump_magiskrc
(
"/overlay/init.magisk.rc"
,
0750
);
mkdir
(
"/overlay/sbin"
,
0755
);
dump_magisk
(
"/overlay/sbin/magisk"
,
0755
);
mkdir
(
"/overlay/root"
,
0755
);
link
(
"/init"
,
"/overlay/root/magiskpolicy"
);
link
(
"/init"
,
"/overlay/root/supolicy"
);
execv
(
"/init"
,
argv
);
return
1
;
/* Should not happen */
}
int
main
(
int
argc
,
char
*
argv
[])
{
umask
(
0
);
if
(
argc
==
1
)
return
init_main
(
argc
,
argv
);
if
(
argc
<
4
)
usage
();
if
(
strcmp
(
argv
[
2
],
"magisk"
)
==
0
)
dump_magisk
(
argv
[
3
],
0755
);
else
if
(
strcmp
(
argv
[
2
],
"magiskinit"
)
==
0
)
dump_magiskinit
(
argv
[
3
],
0755
);
else
usage
();
lzma_end
(
&
strm
);
return
0
;
}
scripts/boot_patch.sh
View file @
b438cc93
...
@@ -68,7 +68,7 @@ BOOTIMAGE="$1"
...
@@ -68,7 +68,7 @@ BOOTIMAGE="$1"
chmod
-R
755
.
chmod
-R
755
.
# Extract magisk if doesn't exist
# Extract magisk if doesn't exist
[
-e
magisk
]
||
./m
onogisk
-x
magisk magisk
[
-e
magisk
]
||
./m
agiskinit
-x
magisk magisk
##########################################################################################
##########################################################################################
# Unpack
# Unpack
...
@@ -157,7 +157,7 @@ esac
...
@@ -157,7 +157,7 @@ esac
ui_print
"- Patching ramdisk"
ui_print
"- Patching ramdisk"
./magiskboot
--cpio-add
ramdisk.cpio 750 init m
onogisk
./magiskboot
--cpio-add
ramdisk.cpio 750 init m
agiskinit
./magiskboot
--cpio-patch
ramdisk.cpio
$KEEPVERITY
$KEEPFORCEENCRYPT
./magiskboot
--cpio-patch
ramdisk.cpio
$KEEPVERITY
$KEEPFORCEENCRYPT
# Create ramdisk backups
# Create ramdisk backups
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment