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
a3c49de6
Commit
a3c49de6
authored
Dec 20, 2017
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor magiskboot
parent
e8dd1b29
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
351 additions
and
347 deletions
+351
-347
build.py
build.py
+2
-4
magiskinit.c
core/jni/core/magiskinit.c
+21
-25
cpio.h
core/jni/include/cpio.h
+2
-0
vector.h
core/jni/include/vector.h
+4
-4
bootimg.c
core/jni/magiskboot/bootimg.c
+2
-4
compress.c
core/jni/magiskboot/compress.c
+2
-2
hexpatch.c
core/jni/magiskboot/hexpatch.c
+1
-1
magiskboot.h
core/jni/magiskboot/magiskboot.h
+1
-1
main.c
core/jni/magiskboot/main.c
+32
-30
ramdisk.c
core/jni/magiskboot/ramdisk.c
+139
-139
resetprop.cpp
core/jni/resetprop/resetprop.cpp
+1
-1
cpio.c
core/jni/utils/cpio.c
+113
-110
vector.c
core/jni/utils/vector.c
+17
-1
boot_patch.sh
scripts/boot_patch.sh
+7
-19
magisk_uninstaller.sh
scripts/magisk_uninstaller.sh
+5
-4
util_functions.sh
scripts/util_functions.sh
+2
-2
No files found.
build.py
View file @
a3c49de6
...
...
@@ -266,7 +266,7 @@ def zip_main(args):
with
open
(
source
,
'r'
)
as
script
:
# Add version info util_functions.sh
util_func
=
script
.
read
()
.
replace
(
'MAGISK_VERSION_STUB'
,
'MAGISK_VER="{}"
\n
MAGISK_VER_CODE={}'
.
format
(
args
.
versionString
,
args
.
versionCode
))
'
#
MAGISK_VERSION_STUB'
,
'MAGISK_VER="{}"
\n
MAGISK_VER_CODE={}'
.
format
(
args
.
versionString
,
args
.
versionCode
))
target
=
os
.
path
.
join
(
'common'
,
'util_functions.sh'
)
print
(
'zip: '
+
source
+
' -> '
+
target
)
zipf
.
writestr
(
target
,
util_func
)
...
...
@@ -316,11 +316,9 @@ def zip_uninstaller(args):
source
=
os
.
path
.
join
(
'scripts'
,
'util_functions.sh'
)
with
open
(
source
,
'r'
)
as
script
:
# Remove the stub
util_func
=
script
.
read
()
.
replace
(
'MAGISK_VERSION_STUB'
,
''
)
target
=
os
.
path
.
join
(
'util_functions.sh'
)
print
(
'zip: '
+
source
+
' -> '
+
target
)
zipf
.
writestr
(
target
,
util_func
)
zipf
.
writestr
(
target
,
script
.
read
()
)
# Prebuilts
for
chromeos
in
[
'futility'
,
'kernel_data_key.vbprivk'
,
'kernel.keyblock'
]:
...
...
core/jni/core/magiskinit.c
View file @
a3c49de6
...
...
@@ -182,22 +182,24 @@ static void patch_ramdisk(int root) {
close
(
fd
);
free
(
addr
);
char
*
key
,
*
value
;
full_read
(
"/.backup/.magisk"
,
&
addr
,
&
size
);
for
(
char
*
tok
=
strtok
(
addr
,
"
\n
"
);
tok
;
tok
=
strtok
(
NULL
,
"
\n
"
))
{
key
=
tok
;
value
=
strchr
(
tok
,
'='
)
+
1
;
value
[
-
1
]
=
'\0'
;
if
(
strcmp
(
key
,
"KEEPVERITY"
)
==
0
)
keepverity
=
strcmp
(
value
,
"true"
)
==
0
;
else
if
(
strcmp
(
key
,
"KEEPFORCEENCRYPT"
)
==
0
)
keepencrypt
=
strcmp
(
value
,
"true"
)
==
0
;
}
excl_list
=
(
char
*
[])
{
"system_root"
,
"system"
,
"vendor"
,
NULL
};
in_order_walk
(
root
,
fstab_patch_cb
);
if
(
!
keepverity
)
unlink
(
"/verity_key"
);
/* Disabled for now */
// char *key, *value;
// full_read("/.backup/.magisk", &addr, &size);
// for (char *tok = strtok(addr, "\n"); tok; tok = strtok(NULL, "\n")) {
// key = tok;
// value = strchr(tok, '=') + 1;
// value[-1] = '\0';
// if (strcmp(key, "KEEPVERITY") == 0)
// keepverity = strcmp(value, "true") == 0;
// else if (strcmp(key, "KEEPFORCEENCRYPT") == 0)
// keepencrypt = strcmp(value, "true") == 0;
// }
// excl_list = (char *[]) { "system_root", "system", "vendor", NULL };
// in_order_walk(root, fstab_patch_cb);
// if (!keepverity)
// unlink("/verity_key");
}
static
int
strend
(
const
char
*
s1
,
const
char
*
s2
)
{
...
...
@@ -472,17 +474,11 @@ int main(int argc, char *argv[]) {
close
(
system_root
);
}
else
{
char
*
ramdisk_xz
=
NULL
;
if
(
access
(
"/ramdisk-recovery.xz"
,
R_OK
)
==
0
)
ramdisk_xz
=
"/ramdisk-recovery.xz"
;
else
if
(
access
(
"/ramdisk.cpio.xz"
,
R_OK
)
==
0
)
ramdisk_xz
=
"/ramdisk.cpio.xz"
;
if
(
ramdisk_xz
)
{
// High compress mode
if
(
access
(
"/ramdisk.cpio.xz"
,
R_OK
)
==
0
)
{
// High compression mode
void
*
addr
;
size_t
size
;
mmap_ro
(
ramdisk_xz
,
&
addr
,
&
size
);
mmap_ro
(
"/ramdisk.cpio.xz"
,
&
addr
,
&
size
);
int
fd
=
creat
(
"/ramdisk.cpio"
,
0
);
unxz
(
addr
,
size
,
fd
);
munmap
(
addr
,
size
);
...
...
core/jni/include/cpio.h
View file @
a3c49de6
...
...
@@ -42,6 +42,8 @@ typedef struct cpio_newc_header {
}
cpio_newc_header
;
// Basic cpio functions
void
cpio_free
(
cpio_entry
*
e
);
int
cpio_find
(
struct
vector
*
v
,
const
char
*
entry
);
int
cpio_cmp
(
const
void
*
a
,
const
void
*
b
);
void
parse_cpio
(
struct
vector
*
v
,
const
char
*
filename
);
void
dump_cpio
(
struct
vector
*
v
,
const
char
*
filename
);
...
...
core/jni/include/vector.h
View file @
a3c49de6
...
...
@@ -7,8 +7,8 @@
#include <sys/types.h>
struct
vector
{
size_t
size
;
size_t
cap
;
unsigned
size
;
unsigned
cap
;
void
**
data
;
};
...
...
@@ -26,11 +26,11 @@ struct vector *vec_dup(struct vector *v);
/* Usage: vec_for_each(vector *v, void *e) */
#define vec_for_each(v, e) \
e = v ? (v)->data[0] : NULL; \
for (
size_
t _ = 0; v && _ < (v)->size; ++_, e = (v)->data[_])
for (
in
t _ = 0; v && _ < (v)->size; ++_, e = (v)->data[_])
#define vec_for_each_r(v, e) \
e = v ? (v)->data[(v)->size - 1] : NULL; \
for (
size_t _ = (v)->size; v && _ > 0; --_, e = (v)->data[_ - 1
])
for (
int _ = ((int) (v)->size) - 1; v && _ >= 0; --_, e = (v)->data[_
])
#define vec_cur(v) vec_entry(v)[_]
...
...
core/jni/magiskboot/bootimg.c
View file @
a3c49de6
...
...
@@ -55,7 +55,6 @@ static void print_hdr(const boot_img_hdr *hdr) {
}
fprintf
(
stderr
,
"NAME [%s]
\n
"
,
hdr
->
name
);
fprintf
(
stderr
,
"CMDLINE [%s]
\n
"
,
hdr
->
cmdline
);
fprintf
(
stderr
,
"
\n
"
);
}
int
parse_img
(
const
char
*
image
,
boot_img
*
boot
)
{
...
...
@@ -63,7 +62,7 @@ int parse_img(const char *image, boot_img *boot) {
int
is_blk
=
mmap_ro
(
image
,
&
boot
->
map_addr
,
&
boot
->
map_size
);
// Parse image
fprintf
(
stderr
,
"Parsing boot image: [%s]
\n
\n
"
,
image
);
fprintf
(
stderr
,
"Parsing boot image: [%s]
\n
"
,
image
);
for
(
size_t
pos
=
0
;
pos
<
boot
->
map_size
;
pos
+=
256
)
{
switch
(
check_type
(
boot
->
map_addr
+
pos
))
{
case
CHROMEOS
:
...
...
@@ -143,7 +142,6 @@ int parse_img(const char *image, boot_img *boot) {
fprintf
(
stderr
,
"KERNEL_FMT [%s]
\n
"
,
fmt
);
get_type_name
(
boot
->
ramdisk_type
,
fmt
);
fprintf
(
stderr
,
"RAMDISK_FMT [%s]
\n
"
,
fmt
);
fprintf
(
stderr
,
"
\n
"
);
return
boot
->
flags
&
CHROMEOS_FLAG
?
CHROMEOS_RET
:
((
is_blk
&&
boot
->
tail_size
<
500
*
1024
)
?
INSUF_BLOCK_RET
:
0
);
...
...
@@ -206,7 +204,7 @@ void repack(const char* orig_image, const char* out_image) {
// Parse original image
parse_img
(
orig_image
,
&
boot
);
fprintf
(
stderr
,
"Repack to boot image: [%s]
\n
\n
"
,
out_image
);
fprintf
(
stderr
,
"Repack to boot image: [%s]
\n
"
,
out_image
);
// Create new image
int
fd
=
creat
(
out_image
,
0644
);
...
...
core/jni/magiskboot/compress.c
View file @
a3c49de6
...
...
@@ -437,7 +437,7 @@ void decomp_file(char *from, const char *to) {
fd
=
STDOUT_FILENO
;
}
else
{
fd
=
creat
(
to
,
0644
);
fprintf
(
stderr
,
"Decompressing to [%s]
\n
\n
"
,
to
);
fprintf
(
stderr
,
"Decompressing to [%s]
\n
"
,
to
);
}
decomp
(
type
,
fd
,
file
,
size
);
...
...
@@ -498,7 +498,7 @@ void comp_file(const char *method, const char *from, const char *to) {
fd
=
STDOUT_FILENO
;
}
else
{
fd
=
creat
(
dest
,
0644
);
fprintf
(
stderr
,
"Compressing to [%s]
\n
\n
"
,
dest
);
fprintf
(
stderr
,
"Compressing to [%s]
\n
"
,
dest
);
}
comp
(
type
,
fd
,
file
,
size
);
close
(
fd
);
...
...
core/jni/magiskboot/hexpatch.c
View file @
a3c49de6
...
...
@@ -26,7 +26,7 @@ void hexpatch(const char *image, const char *from, const char *to) {
hex2byte
(
to
,
patch
);
for
(
size_t
i
=
0
;
filesize
>
0
&&
i
<
filesize
-
patternsize
;
++
i
)
{
if
(
memcmp
(
file
+
i
,
pattern
,
patternsize
)
==
0
)
{
fprintf
(
stderr
,
"Pat
tern %s found!
\n
Patching to %s
\n
"
,
from
,
to
);
fprintf
(
stderr
,
"Pat
ch @ %08X [%s]->[%s]
\n
"
,
(
unsigned
)
i
,
from
,
to
);
memset
(
file
+
i
,
0
,
patternsize
);
memcpy
(
file
+
i
,
patch
,
patchsize
);
i
+=
patternsize
-
1
;
...
...
core/jni/magiskboot/magiskboot.h
View file @
a3c49de6
...
...
@@ -18,7 +18,7 @@ void unpack(const char *image);
void
repack
(
const
char
*
orig_image
,
const
char
*
out_image
);
void
hexpatch
(
const
char
*
image
,
const
char
*
from
,
const
char
*
to
);
int
parse_img
(
const
char
*
image
,
boot_img
*
boot
);
int
cpio_commands
(
const
char
*
cmd
,
int
argc
,
char
*
argv
[]);
int
cpio_commands
(
int
argc
,
char
*
argv
[]);
void
comp_file
(
const
char
*
method
,
const
char
*
from
,
const
char
*
to
);
void
decomp_file
(
char
*
from
,
const
char
*
to
);
int
dtb_commands
(
const
char
*
cmd
,
int
argc
,
char
*
argv
[]);
...
...
core/jni/magiskboot/main.c
View file @
a3c49de6
...
...
@@ -36,34 +36,39 @@ static void usage(char *arg0) {
" --hexpatch <file> <hexpattern1> <hexpattern2>
\n
"
" Search <hexpattern1> in <file>, and replace with <hexpattern2>
\n
"
"
\n
"
" --cpio-<cmd> <incpio> [flags...] [args...]
\n
"
" Do cpio related cmds to <incpio> (modifications are done directly)
\n
"
" --cpio <incpio> [commands...]
\n
"
" Do cpio commands to <incpio> (modifications are done directly)
\n
"
" Each command is a single argument, use quotes if necessary
\n
"
" Supported commands:
\n
"
" -rm [-r] <entry>
\n
"
" Remove entry from <incpio>, flag [-r] to remove recursively
\n
"
" -mkdir <mode> <entry>
\n
"
" Create directory as an <entry>
\n
"
" -ln <target> <entry>
\n
"
" Create symlink <entry> to point to <target>
\n
"
" -mv <from-entry> <to-entry>
\n
"
" Move <from-entry> to <to-entry>
\n
"
" -add <mode> <entry> <infile>
\n
"
" Add <infile> as an <entry>; replaces <entry> if already exists
\n
"
" -extract [<entry> <outfile>]
\n
"
" Extract <entry> to <outfile>, or extract all to current directory
\n
"
" -test
\n
"
" rm [-r] ENTRY
\n
"
" Remove ENTRY, specify [-r] to remove recursively
\n
"
" mkdir MODE ENTRY
\n
"
" Create directory ENTRY in permissions MODE
\n
"
" ln TARGET ENTRY
\n
"
" Create a symlink to TARGET with the name ENTRY
\n
"
" mv SOURCE DEST
\n
"
" Move SOURCE to DEST
\n
"
" add MODE ENTRY INFILE
\n
"
" Add INFILE as ENTRY in permissions MODE; replaces ENTRY if exists
\n
"
" extract [ENTRY OUT]
\n
"
" Extract ENTRY to OUT, or extract all entries to current directory
\n
"
" test
\n
"
" Test the current cpio's patch status
\n
"
" Return value: 0/stock 1/Magisk 2/other (phh, SuperSU, Xposed)
\n
"
"
-patch <KEEPVERITY> <KEEPFORCEENCRYPT>
\n
"
"
patch KEEPVERITY KEEPFORCEENCRYPT
\n
"
" Ramdisk patches. KEEP**** are boolean values
\n
"
" This command is no longer used in Magisk installations
\n
"
" -backup <origcpio> <HIGHCOMP> <KEEPVERITY> <KEEPFORCEENCRYPT> [SHA1]
\n
"
" Create ramdisk backups into <incpio> from <origcpio>
\n
"
" backup ORIG [SHA1]
\n
"
" Create ramdisk backups from ORIG
\n
"
" SHA1 of stock boot image is optional
\n
"
" restore
\n
"
" Restore ramdisk from ramdisk backup stored within incpio
\n
"
" magisk ORIG HIGHCOMP KEEPVERITY KEEPFORCEENCRYPT [SHA1]
\n
"
" Do Magisk patches and backups all in one step
\n
"
" Create ramdisk backups from ORIG
\n
"
" HIGHCOMP, KEEP**** are boolean values
\n
"
" SHA1 of stock boot image is optional
\n
"
" -restore
\n
"
" Restore ramdisk from ramdisk backup within <incpio>
\n
"
" -stocksha1
\n
"
" Get stock boot SHA1 recorded within <incpio>
\n
"
" sha1
\n
"
" Print stock boot SHA1 if previously stored
\n
"
"
\n
"
" --dtb-<cmd> <dtb>
\n
"
" Do dtb related cmds to <dtb> (modifications are done directly)
\n
"
...
...
@@ -104,11 +109,11 @@ static void usage(char *arg0) {
}
int
main
(
int
argc
,
char
*
argv
[])
{
fprintf
(
stderr
,
"MagiskBoot v"
xstr
(
MAGISK_VERSION
)
"("
xstr
(
MAGISK_VER_CODE
)
") (by topjohnwu) - Boot Image Modification Tool
\n
\n
"
);
fprintf
(
stderr
,
"MagiskBoot v"
xstr
(
MAGISK_VERSION
)
"("
xstr
(
MAGISK_VER_CODE
)
") (by topjohnwu) - Boot Image Modification Tool
\n
"
);
umask
(
0
);
if
(
argc
>
1
&&
strcmp
(
argv
[
1
],
"--cleanup"
)
==
0
)
{
fprintf
(
stderr
,
"Cleaning up...
\n
\n
"
);
fprintf
(
stderr
,
"Cleaning up...
\n
"
);
char
name
[
PATH_MAX
];
unlink
(
KERNEL_FILE
);
unlink
(
RAMDISK_FILE
);
...
...
@@ -146,11 +151,8 @@ int main(int argc, char *argv[]) {
comp_file
(
method
,
argv
[
2
],
argc
>
3
?
argv
[
3
]
:
NULL
);
}
else
if
(
argc
>
4
&&
strcmp
(
argv
[
1
],
"--hexpatch"
)
==
0
)
{
hexpatch
(
argv
[
2
],
argv
[
3
],
argv
[
4
]);
}
else
if
(
argc
>
2
&&
strncmp
(
argv
[
1
],
"--cpio"
,
6
)
==
0
)
{
char
*
cmd
=
argv
[
1
]
+
6
;
if
(
*
cmd
==
'\0'
)
usage
(
argv
[
0
]);
else
++
cmd
;
if
(
cpio_commands
(
cmd
,
argc
-
2
,
argv
+
2
))
usage
(
argv
[
0
]);
}
else
if
(
argc
>
2
&&
strcmp
(
argv
[
1
],
"--cpio"
)
==
0
)
{
if
(
cpio_commands
(
argc
-
2
,
argv
+
2
))
usage
(
argv
[
0
]);
}
else
if
(
argc
>
2
&&
strncmp
(
argv
[
1
],
"--dtb"
,
5
)
==
0
)
{
char
*
cmd
=
argv
[
1
]
+
5
;
if
(
*
cmd
==
'\0'
)
usage
(
argv
[
0
]);
...
...
core/jni/magiskboot/ramdisk.c
View file @
a3c49de6
This diff is collapsed.
Click to expand it.
core/jni/resetprop/resetprop.cpp
View file @
a3c49de6
...
...
@@ -209,7 +209,7 @@ public:
vector
prop_list
;
static
int
prop_cmp
(
const
void
*
p1
,
const
void
*
p2
)
{
return
strcmp
((
*
((
property
**
)
p1
))
->
name
,
(
*
((
property
**
)
p2
)
)
->
name
);
return
strcmp
((
(
property
*
)
p1
)
->
name
,
((
property
*
)
p2
)
->
name
);
}
static
void
print_all_props_cb
(
const
char
*
name
,
const
char
*
value
)
{
...
...
core/jni/utils/cpio.c
View file @
a3c49de6
...
...
@@ -22,34 +22,42 @@ static uint32_t x8u(char *hex) {
return
val
;
}
static
void
cpio_free
(
cpio_entry
*
f
)
{
if
(
f
)
{
free
(
f
->
filename
);
free
(
f
->
data
);
free
(
f
);
void
cpio_free
(
cpio_entry
*
e
)
{
if
(
e
)
{
free
(
e
->
filename
);
free
(
e
->
data
);
free
(
e
);
}
}
int
cpio_find
(
struct
vector
*
v
,
const
char
*
entry
)
{
cpio_entry
*
e
;
vec_for_each
(
v
,
e
)
{
if
(
!
e
)
continue
;
if
(
strcmp
(
e
->
filename
,
entry
)
==
0
)
return
_
;
}
return
-
1
;
}
int
cpio_cmp
(
const
void
*
a
,
const
void
*
b
)
{
return
strcmp
((
*
(
cpio_entry
**
)
a
)
->
filename
,
(
*
(
cpio_entry
*
*
)
b
)
->
filename
);
return
strcmp
((
(
cpio_entry
*
)
a
)
->
filename
,
((
cpio_entry
*
)
b
)
->
filename
);
}
void
cpio_vec_insert
(
struct
vector
*
v
,
cpio_entry
*
n
)
{
cpio_entry
*
f
;
vec_for_each
(
v
,
f
)
{
if
(
strcmp
(
f
->
filename
,
n
->
filename
)
==
0
)
{
// Replace, then all is done
cpio_free
(
f
);
vec_cur
(
v
)
=
n
;
return
;
}
int
i
=
cpio_find
(
v
,
n
->
filename
);
if
(
i
>
0
)
{
// Replace, then all is done
cpio_free
(
vec_entry
(
v
)[
i
]);
vec_entry
(
v
)[
i
]
=
n
;
return
;
}
vec_push_back
(
v
,
n
);
}
// Parse cpio file to a vector of cpio_entry
void
parse_cpio
(
struct
vector
*
v
,
const
char
*
filename
)
{
fprintf
(
stderr
,
"Loading cpio: [%s]
\n
\n
"
,
filename
);
fprintf
(
stderr
,
"Loading cpio: [%s]
\n
"
,
filename
);
int
fd
=
open
(
filename
,
O_RDONLY
);
if
(
fd
<
0
)
return
;
cpio_newc_header
header
;
...
...
@@ -91,35 +99,34 @@ void parse_cpio(struct vector *v, const char *filename) {
}
void
dump_cpio
(
struct
vector
*
v
,
const
char
*
filename
)
{
fprintf
(
stderr
,
"
\n
Dump cpio: [%s]
\n\n
"
,
filename
);
int
fd
=
creat
(
filename
,
0644
);
fprintf
(
stderr
,
"Dump cpio: [%s]
\n
"
,
filename
);
unsigned
inode
=
300000
;
char
header
[
111
];
// Sort by name
vec_sort
(
v
,
cpio_cmp
);
cpio_entry
*
f
;
vec_for_each
(
v
,
f
)
{
if
(
f
->
remove
)
continue
;
cpio_entry
*
e
;
int
fd
=
creat
(
filename
,
0644
);
vec_for_each
(
v
,
e
)
{
sprintf
(
header
,
"070701%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x"
,
inode
++
,
// f
->ino
f
->
mode
,
f
->
uid
,
f
->
gid
,
1
,
// f
->nlink
0
,
// f
->mtime
f
->
filesize
,
0
,
// f
->devmajor
0
,
// f
->devminor
0
,
// f
->rdevmajor
0
,
// f
->rdevminor
(
uint32_t
)
strlen
(
f
->
filename
)
+
1
,
0
// f
->check
inode
++
,
// e
->ino
e
->
mode
,
e
->
uid
,
e
->
gid
,
1
,
// e
->nlink
0
,
// e
->mtime
e
->
filesize
,
0
,
// e
->devmajor
0
,
// e
->devminor
0
,
// e
->rdevmajor
0
,
// e
->rdevminor
(
uint32_t
)
strlen
(
e
->
filename
)
+
1
,
0
// e
->check
);
xwrite
(
fd
,
header
,
110
);
xwrite
(
fd
,
f
->
filename
,
strlen
(
f
->
filename
)
+
1
);
xwrite
(
fd
,
e
->
filename
,
strlen
(
e
->
filename
)
+
1
);
file_align
(
fd
,
4
,
1
);
if
(
f
->
filesize
)
{
xwrite
(
fd
,
f
->
data
,
f
->
filesize
);
if
(
e
->
filesize
)
{
xwrite
(
fd
,
e
->
data
,
e
->
filesize
);
file_align
(
fd
,
4
,
1
);
}
}
...
...
@@ -133,23 +140,22 @@ void dump_cpio(struct vector *v, const char *filename) {
void
cpio_vec_destroy
(
struct
vector
*
v
)
{
// Free each cpio_entry
cpio_entry
*
f
;
vec_for_each
(
v
,
f
)
{
cpio_free
(
f
);
}
cpio_entry
*
e
;
vec_for_each
(
v
,
e
)
cpio_free
(
e
);
vec_destroy
(
v
);
}
void
cpio_rm
(
struct
vector
*
v
,
int
recursive
,
const
char
*
entry
)
{
cpio_entry
*
f
;
vec_for_each
(
v
,
f
)
{
if
(
strncmp
(
f
->
filename
,
entry
,
strlen
(
entry
))
==
0
)
{
char
next
=
f
->
filename
[
strlen
(
entry
)]
;
if
((
recursive
&&
next
==
'/'
)
||
next
==
'\0'
)
{
if
(
!
f
->
remove
)
{
fprintf
(
stderr
,
"Remove [%s]
\n
"
,
f
->
filename
);
f
->
remove
=
1
;
}
cpio_entry
*
e
;
size_t
len
=
strlen
(
entry
);
vec_for_each
(
v
,
e
)
{
if
(
!
e
)
continue
;
if
(
strncmp
(
e
->
filename
,
entry
,
len
)
==
0
)
{
if
((
recursive
&&
e
->
filename
[
len
]
==
'/'
)
||
e
->
filename
[
len
]
==
'\0'
)
{
fprintf
(
stderr
,
"Remove [%s]
\n
"
,
e
->
filename
);
cpio_free
(
e
)
;
vec_cur
(
v
)
=
NULL
;
if
(
!
recursive
)
return
;
}
}
...
...
@@ -157,97 +163,94 @@ void cpio_rm(struct vector *v, int recursive, const char *entry) {
}
void
cpio_mkdir
(
struct
vector
*
v
,
mode_t
mode
,
const
char
*
entry
)
{
cpio_entry
*
f
=
xcalloc
(
sizeof
(
*
f
),
1
);
f
->
mode
=
S_IFDIR
|
mode
;
f
->
filename
=
strdup
(
entry
);
cpio_vec_insert
(
v
,
f
);
cpio_entry
*
e
=
xcalloc
(
sizeof
(
*
e
),
1
);
e
->
mode
=
S_IFDIR
|
mode
;
e
->
filename
=
strdup
(
entry
);
cpio_vec_insert
(
v
,
e
);
fprintf
(
stderr
,
"Create directory [%s] (%04o)
\n
"
,
entry
,
mode
);
}
void
cpio_ln
(
struct
vector
*
v
,
const
char
*
target
,
const
char
*
entry
)
{
cpio_entry
*
f
=
xcalloc
(
sizeof
(
*
f
),
1
);
f
->
mode
=
S_IFLNK
;
f
->
filename
=
strdup
(
entry
);
f
->
filesize
=
strlen
(
target
);
f
->
data
=
strdup
(
target
);
cpio_vec_insert
(
v
,
f
);
cpio_entry
*
e
=
xcalloc
(
sizeof
(
*
e
),
1
);
e
->
mode
=
S_IFLNK
;
e
->
filename
=
strdup
(
entry
);
e
->
filesize
=
strlen
(
target
);
e
->
data
=
strdup
(
target
);
cpio_vec_insert
(
v
,
e
);
fprintf
(
stderr
,
"Create symlink [%s] -> [%s]
\n
"
,
entry
,
target
);
}
void
cpio_add
(
struct
vector
*
v
,
mode_t
mode
,
const
char
*
entry
,
const
char
*
filename
)
{
int
fd
=
xopen
(
filename
,
O_RDONLY
);
cpio_entry
*
f
=
xcalloc
(
sizeof
(
*
f
),
1
);
f
->
mode
=
S_IFREG
|
mode
;
f
->
filename
=
strdup
(
entry
);
f
->
filesize
=
lseek
(
fd
,
0
,
SEEK_END
);
cpio_entry
*
e
=
xcalloc
(
sizeof
(
*
e
),
1
);
e
->
mode
=
S_IFREG
|
mode
;
e
->
filename
=
strdup
(
entry
);
e
->
filesize
=
lseek
(
fd
,
0
,
SEEK_END
);
lseek
(
fd
,
0
,
SEEK_SET
);
f
->
data
=
xmalloc
(
f
->
filesize
);
xxread
(
fd
,
f
->
data
,
f
->
filesize
);
e
->
data
=
xmalloc
(
e
->
filesize
);
xxread
(
fd
,
e
->
data
,
e
->
filesize
);
close
(
fd
);
cpio_vec_insert
(
v
,
f
);
cpio_vec_insert
(
v
,
e
);
fprintf
(
stderr
,
"Add entry [%s] (%04o)
\n
"
,
entry
,
mode
);
}
int
cpio_mv
(
struct
vector
*
v
,
const
char
*
from
,
const
char
*
to
)
{
struct
cpio_entry
*
f
,
*
t
;
vec_for_each
(
v
,
f
)
{
if
(
strcmp
(
f
->
filename
,
from
)
==
0
)
{
fprintf
(
stderr
,
"Move [%s] -> [%s]
\n
"
,
from
,
to
);
vec_for_each
(
v
,
t
)
{
if
(
strcmp
(
t
->
filename
,
to
)
==
0
)
{
t
->
remove
=
1
;
break
;
}
}
free
(
f
->
filename
);
f
->
filename
=
strdup
(
to
);
return
0
;
struct
cpio_entry
*
e
;
int
f
=
cpio_find
(
v
,
from
),
t
=
cpio_find
(
v
,
to
);
if
(
f
>
0
)
{
if
(
t
>
0
)
{
cpio_free
(
vec_entry
(
v
)[
t
]);
vec_entry
(
v
)[
t
]
=
NULL
;
}
e
=
vec_entry
(
v
)[
f
];
free
(
e
->
filename
);
e
->
filename
=
strdup
(
to
);
return
0
;
}
fprintf
(
stderr
,
"Cannot find entry %s
\n
"
,
from
);
return
1
;
}
int
cpio_extract
(
struct
vector
*
v
,
const
char
*
entry
,
const
char
*
filename
)
{
cpio_entry
*
f
;
vec_for_each
(
v
,
f
)
{
if
(
strcmp
(
f
->
filename
,
entry
)
==
0
)
{
fprintf
(
stderr
,
"Extracting [%s] to [%s]
\n\n
"
,
entry
,
filename
);
if
(
S_ISREG
(
f
->
mode
))
{
int
fd
=
creat
(
filename
,
f
->
mode
&
0777
);
xwrite
(
fd
,
f
->
data
,
f
->
filesize
);
fchown
(
fd
,
f
->
uid
,
f
->
gid
);
close
(
fd
);
}
else
if
(
S_ISLNK
(
f
->
mode
))
{
char
*
target
=
xcalloc
(
f
->
filesize
+
1
,
1
);
memcpy
(
target
,
f
->
data
,
f
->
filesize
);
unlink
(
filename
);
symlink
(
target
,
filename
);
}
return
0
;
int
i
=
cpio_find
(
v
,
entry
);
if
(
i
>
0
)
{
cpio_entry
*
e
=
vec_entry
(
v
)[
i
];
fprintf
(
stderr
,
"Extracting [%s] to [%s]
\n
"
,
entry
,
filename
);
if
(
S_ISREG
(
e
->
mode
))
{
int
fd
=
creat
(
filename
,
e
->
mode
&
0777
);
xwrite
(
fd
,
e
->
data
,
e
->
filesize
);
fchown
(
fd
,
e
->
uid
,
e
->
gid
);
close
(
fd
);
}
else
if
(
S_ISLNK
(
e
->
mode
))
{
char
*
target
=
xcalloc
(
e
->
filesize
+
1
,
1
);
memcpy
(
target
,
e
->
data
,
e
->
filesize
);
unlink
(
filename
);
symlink
(
target
,
filename
);
}
return
0
;
}
fprintf
(
stderr
,
"Cannot find the file entry [%s]
\n
"
,
entry
);
return
1
;
}
void
cpio_extract_all
(
struct
vector
*
v
)
{
cpio_entry
*
f
;
vec_for_each
(
v
,
f
)
{
fprintf
(
stderr
,
"Extracting [%s]
\n
"
,
f
->
filename
);
unlink
(
f
->
filename
);
rmdir
(
f
->
filename
);
if
(
S_ISDIR
(
f
->
mode
))
{
mkdir
(
f
->
filename
,
f
->
mode
&
0777
);
}
else
if
(
S_ISREG
(
f
->
mode
))
{
int
fd
=
creat
(
f
->
filename
,
f
->
mode
&
0777
);
xwrite
(
fd
,
f
->
data
,
f
->
filesize
);
fchown
(
fd
,
f
->
uid
,
f
->
gid
);
cpio_entry
*
e
;
vec_for_each
(
v
,
e
)
{
if
(
!
e
)
continue
;
fprintf
(
stderr
,
"Extracting [%s]
\n
"
,
e
->
filename
);
unlink
(
e
->
filename
);
rmdir
(
e
->
filename
);
if
(
S_ISDIR
(
e
->
mode
))
{
mkdir
(
e
->
filename
,
e
->
mode
&
0777
);
}
else
if
(
S_ISREG
(
e
->
mode
))
{
int
fd
=
creat
(
e
->
filename
,
e
->
mode
&
0777
);
xwrite
(
fd
,
e
->
data
,
e
->
filesize
);
fchown
(
fd
,
e
->
uid
,
e
->
gid
);
close
(
fd
);
}
else
if
(
S_ISLNK
(
f
->
mode
))
{
char
*
target
=
xcalloc
(
f
->
filesize
+
1
,
1
);
memcpy
(
target
,
f
->
data
,
f
->
filesize
);
symlink
(
target
,
f
->
filename
);
}
else
if
(
S_ISLNK
(
e
->
mode
))
{
char
*
target
=
xcalloc
(
e
->
filesize
+
1
,
1
);
memcpy
(
target
,
e
->
data
,
e
->
filesize
);
symlink
(
target
,
e
->
filename
);
}
}
}
core/jni/utils/vector.c
View file @
a3c49de6
...
...
@@ -29,9 +29,25 @@ void *vec_pop_back(struct vector *v) {
return
ret
;
}
static
int
(
*
cmp
)(
const
void
*
,
const
void
*
);
static
int
vec_comp
(
const
void
*
a
,
const
void
*
b
)
{
void
*
aa
=
*
((
void
**
)
a
),
*
bb
=
*
((
void
**
)
b
);
if
(
aa
==
NULL
&&
bb
==
NULL
)
return
0
;
else
if
(
aa
==
NULL
)
return
1
;
else
if
(
bb
==
NULL
)
return
-
1
;
else
return
cmp
?
cmp
(
aa
,
bb
)
:
0
;
}
void
vec_sort
(
struct
vector
*
v
,
int
(
*
compar
)(
const
void
*
,
const
void
*
))
{
if
(
v
==
NULL
)
return
;
qsort
(
vec_entry
(
v
),
vec_size
(
v
),
sizeof
(
void
*
),
compar
);
cmp
=
compar
;
qsort
(
vec_entry
(
v
),
vec_size
(
v
),
sizeof
(
void
*
),
vec_comp
);
void
*
e
;
vec_for_each_r
(
v
,
e
)
{
if
(
e
)
break
;
--
vec_size
(
v
);
}
}
/* Will cleanup only the vector itself
...
...
scripts/boot_patch.sh
View file @
a3c49de6
...
...
@@ -89,6 +89,7 @@ case $? in
2
)
ui_print
"! Insufficient boot partition size detected"
HIGHCOMP
=
true
ui_print
"- Enable high compression mode"
;;
3
)
ui_print
"- ChromeOS boot image detected"
...
...
@@ -103,20 +104,13 @@ case $? in
abort
"! Stock kernel cannot be patched, please use a custom kernel"
esac
if
[
-f
/sdcard/ramdisk-recovery.img
]
;
then
HIGHCOMP
=
true
ui_print
"- Detected ramdisk-recovery.img"
fi
$HIGHCOMP
&&
ui_print
"- Enable high compression mode"
##########################################################################################
# Ramdisk restores
##########################################################################################
# Test patch status and do restore, after this section, ramdisk.cpio.orig is guaranteed to exist
ui_print
"- Checking ramdisk status"
./magiskboot
--cpio
-test
ramdisk.cpio
./magiskboot
--cpio
ramdisk.cpio
test
case
$?
in
0
)
# Stock boot
ui_print
"- Stock boot image detected!"
...
...
@@ -129,8 +123,8 @@ case $? in
1
)
# Magisk patched
ui_print
"- Magisk patched image detected!"
# Find SHA1 of stock boot image
[
-z
$SHA1
]
&&
SHA1
=
`
./magiskboot
--cpio
-stocksha1
ramdisk.cpio
2>/dev/null
`
./magiskboot
--cpio
-restore
ramdisk.cpio
[
-z
$SHA1
]
&&
SHA1
=
`
./magiskboot
--cpio
ramdisk.cpio sha1
2>/dev/null
`
./magiskboot
--cpio
ramdisk.cpio restore
cp
-af
ramdisk.cpio ramdisk.cpio.orig
;;
2
)
# Other patched
...
...
@@ -145,18 +139,12 @@ esac
ui_print
"- Patching ramdisk"
./magiskboot
--cpio-add
ramdisk.cpio 750 init magiskinit
./magiskboot
--cpio-backup
ramdisk.cpio ramdisk.cpio.orig
$HIGHCOMP
$KEEPVERITY
$KEEPFORCEENCRYPT
$SHA1
./magiskboot
--cpio
ramdisk.cpio
\
'add 750 init magiskinit'
\
"magisk ramdisk.cpio.orig
$HIGHCOMP
$KEEPVERITY
$KEEPFORCEENCRYPT
$SHA1
"
rm
-f
ramdisk.cpio.orig
if
[
-f
/sdcard/ramdisk-recovery.img
]
;
then
ui_print
"- Adding ramdisk-recovery.img"
./magiskboot
--decompress
- < /sdcard/ramdisk-recovery.img | ./magiskboot
--compress
=
xz - ramdisk-recovery.xz
./magiskboot
--cpio-add
ramdisk.cpio 0 ramdisk-recovery.xz ramdisk-recovery.xz
rm
ramdisk-recovery.xz
fi
##########################################################################################
# Binary patches
##########################################################################################
...
...
scripts/magisk_uninstaller.sh
View file @
a3c49de6
...
...
@@ -71,7 +71,7 @@ esac
# Detect boot image state
ui_print
"- Checking ramdisk status"
./magiskboot
--cpio
-test
ramdisk.cpio
./magiskboot
--cpio
ramdisk.cpio
test
case
$?
in
0
)
# Stock boot
ui_print
"- Stock boot image detected!"
...
...
@@ -80,13 +80,13 @@ case $? in
1
)
# Magisk patched
ui_print
"- Magisk patched image detected!"
# Find SHA1 of stock boot image
[
-z
$SHA1
]
&&
SHA1
=
`
./magiskboot
--cpio
-stocksha1
ramdisk.cpio
2>/dev/null
`
[
-z
$SHA1
]
&&
SHA1
=
`
./magiskboot
--cpio
ramdisk.cpio sha1
2>/dev/null
`
OK
=
false
[
!
-z
$SHA1
]
&&
restore_imgs
$SHA1
&&
OK
=
true
if
!
$OK
;
then
ui_print
"! Boot image backup unavailable"
ui_print
"- Restoring ramdisk with internal backup"
./magiskboot
--cpio
-restore
ramdisk.cpio
./magiskboot
--cpio
ramdisk.cpio restore
./magiskboot
--repack
$BOOTIMAGE
# Sign chromeos boot
$CHROMEOS
&&
sign_chromeos
...
...
@@ -104,6 +104,7 @@ cd /
ui_print
"- Removing Magisk files"
rm
-rf
/cache/
*
magisk
*
/cache/unblock /data/
*
magisk
*
/data/cache/
*
magisk
*
/data/property/
*
magisk
*
\
/data/Magisk.apk /data/busybox /data/custom_ramdisk_patch.sh /data/app/com.topjohnwu.magisk
*
\
/data/user
*
/
*
/magisk.db /data/user
*
/
*
/com.topjohnwu.magisk /data/adb/
*
magisk
*
2>/dev/null
/data/user
*
/
*
/magisk.db /data/user
*
/
*
/com.topjohnwu.magisk /data/user
*
/
*
/.tmp.magisk.config
\
/data/adb/
*
magisk
*
2>/dev/null
$BOOTMODE
&&
reboot
scripts/util_functions.sh
View file @
a3c49de6
...
...
@@ -7,11 +7,11 @@
#
##########################################################################################
MAGISK_VERSION_STUB
#
MAGISK_VERSION_STUB
SCRIPT_VERSION
=
$MAGISK_VER_CODE
# Default location, will override if needed
MAGISKBIN
=
/data/adb
/magisk
[
-d
/data/adb/magisk
]
&&
MAGISKBIN
=
/data/adb/magisk
||
MAGISKBIN
=
/data
/magisk
BOOTSIGNER
=
"/system/bin/dalvikvm -Xnodex2oat -Xnoimage-dex2oat -cp
\$
APK com.topjohnwu.magisk.utils.BootSigner"
BOOTSIGNED
=
false
...
...
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