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
cf8f042a
Commit
cf8f042a
authored
Aug 13, 2021
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleanup magiskboot cpio code
parent
844bc2d8
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
127 additions
and
84 deletions
+127
-84
Android.mk
native/jni/Android.mk
+1
-1
Android.mk
native/jni/external/Android.mk
+1
-2
cpio.cpp
native/jni/magiskboot/cpio.cpp
+37
-47
cpio.hpp
native/jni/magiskboot/cpio.hpp
+55
-0
ramdisk.cpp
native/jni/magiskboot/ramdisk.cpp
+33
-34
No files found.
native/jni/Android.mk
View file @
cf8f042a
...
...
@@ -97,7 +97,7 @@ LOCAL_SRC_FILES := \
magiskboot/dtb.cpp \
magiskboot/ramdisk.cpp \
magiskboot/pattern.cpp \
utils
/cpio.cpp
magiskboot
/cpio.cpp
LOCAL_LDFLAGS := -static
include $(BUILD_EXECUTABLE)
...
...
native/jni/external/Android.mk
View file @
cf8f042a
...
...
@@ -3,8 +3,7 @@ LOCAL_PATH := $(call my-dir)
# Header only library
include $(CLEAR_VARS)
LOCAL_MODULE:= libphmap
LOCAL_C_INCLUDES := $(LOCAL_PATH)/parallel-hashmap
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/parallel-hashmap
include $(BUILD_STATIC_LIBRARY)
# libxz.a
...
...
native/jni/
utils
/cpio.cpp
→
native/jni/
magiskboot
/cpio.cpp
View file @
cf8f042a
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <utils.hpp>
#include <cpio.hpp>
#include "cpio.hpp"
using
namespace
std
;
...
...
@@ -43,8 +42,11 @@ static uint32_t x8u(const char *hex) {
return
val
;
}
cpio_entry_base
::
cpio_entry_base
(
const
cpio_newc_header
*
h
)
:
mode
(
x8u
(
h
->
mode
)),
uid
(
x8u
(
h
->
uid
)),
gid
(
x8u
(
h
->
gid
)),
filesize
(
x8u
(
h
->
filesize
))
{};
cpio_entry
::
cpio_entry
(
uint32_t
mode
)
:
mode
(
mode
),
uid
(
0
),
gid
(
0
),
filesize
(
0
),
data
(
nullptr
)
{}
cpio_entry
::
cpio_entry
(
const
cpio_newc_header
*
h
)
:
mode
(
x8u
(
h
->
mode
)),
uid
(
x8u
(
h
->
uid
)),
gid
(
x8u
(
h
->
gid
)),
filesize
(
x8u
(
h
->
filesize
)),
data
(
nullptr
)
{}
void
cpio
::
dump
(
const
char
*
file
)
{
fprintf
(
stderr
,
"Dump cpio: [%s]
\n
"
,
file
);
...
...
@@ -71,12 +73,12 @@ void cpio::rm(const char *name, bool r) {
}
}
static
void
extract_entry
(
const
entry_map
::
value_type
&
e
,
const
char
*
file
)
{
void
cpio
::
extract_entry
(
const
entry_map
::
value_type
&
e
,
const
char
*
file
)
{
fprintf
(
stderr
,
"Extract [%s] to [%s]
\n
"
,
e
.
first
.
data
(),
file
);
unlink
(
file
);
rmdir
(
file
);
if
(
S_ISDIR
(
e
.
second
->
mode
))
{
mkdir
(
file
,
e
.
second
->
mode
&
0777
);
::
mkdir
(
file
,
e
.
second
->
mode
&
0777
);
}
else
if
(
S_ISREG
(
e
.
second
->
mode
))
{
int
fd
=
creat
(
file
,
e
.
second
->
mode
&
0777
);
xwrite
(
fd
,
e
.
second
->
data
,
e
.
second
->
filesize
);
...
...
@@ -148,11 +150,7 @@ void cpio::dump(FILE *out) {
fclose
(
out
);
}
cpio_rw
::
cpio_rw
(
const
char
*
file
)
{
load_cpio
(
file
);
}
void
cpio_rw
::
load_cpio
(
const
char
*
file
)
{
void
cpio
::
load_cpio
(
const
char
*
file
)
{
char
*
buf
;
size_t
sz
;
mmap_ro
(
file
,
buf
,
sz
);
...
...
@@ -161,54 +159,49 @@ void cpio_rw::load_cpio(const char *file) {
munmap
(
buf
,
sz
);
}
void
cpio
_rw
::
insert
(
cpio_entry
*
e
)
{
auto
ex
=
entries
.
extract
(
e
->
file
name
);
if
(
!
ex
)
{
entries
[
e
->
filename
]
.
reset
(
e
);
void
cpio
::
insert
(
string_view
name
,
cpio_entry
*
e
)
{
auto
it
=
entries
.
find
(
name
);
if
(
it
!=
entries
.
end
()
)
{
it
->
second
.
reset
(
e
);
}
else
{
ex
.
key
()
=
e
->
filename
;
ex
.
mapped
().
reset
(
e
);
entries
.
insert
(
std
::
move
(
ex
));
entries
.
emplace
(
name
,
e
);
}
}
void
cpio
_rw
::
add
(
mode_t
mode
,
const
char
*
name
,
const
char
*
file
)
{
void
cpio
::
add
(
mode_t
mode
,
const
char
*
name
,
const
char
*
file
)
{
void
*
buf
;
size_t
sz
;
mmap_ro
(
file
,
buf
,
sz
);
auto
e
=
new
cpio_entry
(
name
,
S_IFREG
|
mode
);
auto
e
=
new
cpio_entry
(
S_IFREG
|
mode
);
e
->
filesize
=
sz
;
e
->
data
=
xmalloc
(
sz
);
memcpy
(
e
->
data
,
buf
,
sz
);
munmap
(
buf
,
sz
);
insert
(
e
);
insert
(
name
,
e
);
fprintf
(
stderr
,
"Add entry [%s] (%04o)
\n
"
,
name
,
mode
);
}
void
cpio
_rw
::
mkdir
(
mode_t
mode
,
const
char
*
name
)
{
insert
(
n
ew
cpio_entry
(
name
,
S_IFDIR
|
mode
));
void
cpio
::
mkdir
(
mode_t
mode
,
const
char
*
name
)
{
insert
(
n
ame
,
new
cpio_entry
(
S_IFDIR
|
mode
));
fprintf
(
stderr
,
"Create directory [%s] (%04o)
\n
"
,
name
,
mode
);
}
void
cpio
_rw
::
ln
(
const
char
*
target
,
const
char
*
name
)
{
auto
e
=
new
cpio_entry
(
name
,
S_IFLNK
);
void
cpio
::
ln
(
const
char
*
target
,
const
char
*
name
)
{
auto
e
=
new
cpio_entry
(
S_IFLNK
);
e
->
filesize
=
strlen
(
target
);
e
->
data
=
strdup
(
target
);
insert
(
e
);
insert
(
name
,
e
);
fprintf
(
stderr
,
"Create symlink [%s] -> [%s]
\n
"
,
name
,
target
);
}
void
cpio_rw
::
mv
(
entry_map
::
iterator
&
it
,
const
char
*
to
)
{
fprintf
(
stderr
,
"Move [%s] -> [%s]
\n
"
,
it
->
first
.
data
(),
to
);
auto
ex
=
entries
.
extract
(
it
);
auto
&
name
=
static_cast
<
cpio_entry
*>
(
ex
.
mapped
().
get
())
->
filename
;
name
=
to
;
ex
.
key
()
=
name
;
entries
.
erase
(
name
);
entries
.
insert
(
std
::
move
(
ex
));
void
cpio
::
mv
(
entry_map
::
iterator
&
it
,
const
char
*
name
)
{
fprintf
(
stderr
,
"Move [%s] -> [%s]
\n
"
,
it
->
first
.
data
(),
name
);
auto
e
=
it
->
second
.
release
();
entries
.
erase
(
it
);
insert
(
name
,
e
);
}
bool
cpio
_rw
::
mv
(
const
char
*
from
,
const
char
*
to
)
{
bool
cpio
::
mv
(
const
char
*
from
,
const
char
*
to
)
{
auto
it
=
entries
.
find
(
from
);
if
(
it
!=
entries
.
end
())
{
mv
(
it
,
to
);
...
...
@@ -220,26 +213,23 @@ bool cpio_rw::mv(const char *from, const char *to) {
#define pos_align(p) p = do_align(p, 4)
void
cpio
_rw
::
load_cpio
(
const
char
*
buf
,
size_t
sz
)
{
void
cpio
::
load_cpio
(
const
char
*
buf
,
size_t
sz
)
{
size_t
pos
=
0
;
const
cpio_newc_header
*
header
;
unique_ptr
<
cpio_entry
>
entry
;
while
(
pos
<
sz
)
{
header
=
reinterpret_cast
<
const
cpio_newc_header
*>
(
buf
+
pos
);
entry
=
make_unique
<
cpio_entry
>
(
header
);
pos
+=
sizeof
(
*
header
);
string_view
name_view
(
buf
+
pos
);
auto
header
=
reinterpret_cast
<
const
cpio_newc_header
*>
(
buf
+
pos
);
pos
+=
sizeof
(
cpio_newc_header
);
string_view
name
(
buf
+
pos
);
pos
+=
x8u
(
header
->
namesize
);
pos_align
(
pos
);
if
(
name
_view
==
"."
||
name_view
==
".."
)
if
(
name
==
"."
||
name
==
".."
)
continue
;
if
(
name
_view
==
"TRAILER!!!"
)
if
(
name
==
"TRAILER!!!"
)
break
;
entry
->
filename
=
name_view
;
auto
entry
=
new
cpio_entry
(
header
)
;
entry
->
data
=
xmalloc
(
entry
->
filesize
);
memcpy
(
entry
->
data
,
buf
+
pos
,
entry
->
filesize
);
pos
+=
entry
->
filesize
;
entries
[
entry
->
filename
]
=
std
::
move
(
entry
);
insert
(
name
,
entry
);
pos_align
(
pos
);
}
}
native/jni/
utils/include
/cpio.hpp
→
native/jni/
magiskboot
/cpio.hpp
View file @
cf8f042a
...
...
@@ -6,63 +6,50 @@
#include <map>
#include <string_view>
#include <stream.hpp>
struct
cpio_newc_header
;
struct
cpio_entry_base
{
uint32_t
mode
=
0
;
uint32_t
uid
=
0
;
uint32_t
gid
=
0
;
uint32_t
filesize
=
0
;
void
*
data
=
nullptr
;
cpio_entry_base
()
:
mode
(
0
),
uid
(
0
),
gid
(
0
),
filesize
(
0
)
{};
explicit
cpio_entry_base
(
const
cpio_newc_header
*
h
);
virtual
~
cpio_entry_base
()
=
default
;
};
struct
cpio_entry
:
public
cpio_entry_base
{
std
::
string
filename
;
cpio_entry
()
=
default
;
explicit
cpio_entry
(
const
char
*
name
,
uint32_t
mode
)
:
filename
(
name
)
{
this
->
mode
=
mode
;
}
explicit
cpio_entry
(
const
cpio_newc_header
*
h
)
:
cpio_entry_base
(
h
)
{}
struct
cpio_entry
{
uint32_t
mode
;
uint32_t
uid
;
uint32_t
gid
;
uint32_t
filesize
;
void
*
data
;
~
cpio_entry
()
override
{
free
(
data
);
};
explicit
cpio_entry
(
uint32_t
mode
=
0
);
explicit
cpio_entry
(
const
cpio_newc_header
*
h
);
~
cpio_entry
()
{
free
(
data
);
}
};
typedef
std
::
map
<
std
::
string_view
,
std
::
unique_ptr
<
cpio_entry_base
>>
entry_map
;
class
cpio
{
public
:
struct
StringCmp
{
using
is_transparent
=
void
;
bool
operator
()(
std
::
string_view
a
,
std
::
string_view
b
)
const
{
return
a
<
b
;
}
};
using
entry_map
=
std
::
map
<
std
::
string
,
std
::
unique_ptr
<
cpio_entry
>
,
StringCmp
>
;
void
load_cpio
(
const
char
*
file
);
void
dump
(
const
char
*
file
);
void
rm
(
const
char
*
name
,
bool
r
=
false
);
void
extract
();
bool
extract
(
const
char
*
name
,
const
char
*
file
);
bool
exists
(
const
char
*
name
);
protected
:
entry_map
entries
;
void
rm
(
entry_map
::
iterator
&
it
);
void
dump
(
FILE
*
out
);
};
class
cpio_rw
:
public
cpio
{
public
:
cpio_rw
()
=
default
;
explicit
cpio_rw
(
const
char
*
file
);
void
load_cpio
(
const
char
*
file
);
void
add
(
mode_t
mode
,
const
char
*
name
,
const
char
*
file
);
void
mkdir
(
mode_t
mode
,
const
char
*
name
);
void
ln
(
const
char
*
target
,
const
char
*
name
);
bool
mv
(
const
char
*
from
,
const
char
*
to
);
protected
:
void
insert
(
cpio_entry
*
e
);
void
mv
(
entry_map
::
iterator
&
it
,
const
char
*
to
);
entry_map
entries
;
static
void
extract_entry
(
const
entry_map
::
value_type
&
e
,
const
char
*
file
);
void
rm
(
entry_map
::
iterator
&
it
);
void
mv
(
entry_map
::
iterator
&
it
,
const
char
*
name
);
private
:
void
dump
(
FILE
*
out
);
void
insert
(
std
::
string_view
name
,
cpio_entry
*
e
);
void
load_cpio
(
const
char
*
buf
,
size_t
sz
);
};
native/jni/magiskboot/ramdisk.cpp
View file @
cf8f042a
#include <utils.hpp>
#include <cpio.hpp>
#include "cpio.hpp"
#include "magiskboot.hpp"
#include "compress.hpp"
...
...
@@ -14,9 +14,8 @@ static const char *MAGISK_LIST[] =
{
".backup/.magisk"
,
"init.magisk.rc"
,
"overlay/init.magisk.rc"
};
class
magisk_cpio
:
public
cpio
_rw
{
class
magisk_cpio
:
public
cpio
{
public
:
using
cpio_rw
::
cpio_rw
;
void
patch
();
int
test
();
char
*
sha1
();
...
...
@@ -95,7 +94,7 @@ char *magisk_cpio::sha1() {
}
}
else
if
(
e
.
first
==
".backup/.magisk"
)
{
for_each_line
(
line
,
e
.
second
->
data
,
e
.
second
->
filesize
)
{
if
(
str
ncmp
(
line
,
"SHA1="
,
5
)
==
0
)
{
if
(
str
_starts
(
line
,
"SHA1="
)
)
{
strncpy
(
sha1
,
line
+
5
,
40
);
sha1
[
40
]
=
'\0'
;
return
strdup
(
sha1
);
...
...
@@ -114,15 +113,16 @@ for (str = (char *) buf; str < (char *) buf + size; str = str += strlen(str) + 1
void
magisk_cpio
::
restore
()
{
if
(
auto
it
=
entries
.
find
(
".backup/.rmlist"
);
it
!=
entries
.
end
())
{
char
*
file
;
for_each_str
(
file
,
it
->
second
->
data
,
it
->
second
->
filesize
)
rm
(
file
,
false
);
for_each_str
(
file
,
it
->
second
->
data
,
it
->
second
->
filesize
)
{
rm
(
file
);
}
rm
(
it
);
}
for
(
auto
it
=
entries
.
begin
();
it
!=
entries
.
end
();)
{
auto
cur
=
it
++
;
if
(
str_starts
(
cur
->
first
,
".backup"
))
{
if
(
cur
->
first
.
length
()
==
7
||
cur
->
first
.
substr
(
8
)
==
".magisk"
)
{
if
(
cur
->
first
.
length
()
==
7
||
&
cur
->
first
[
8
]
==
".magisk"
sv
)
{
rm
(
cur
);
}
else
{
mv
(
cur
,
&
cur
->
first
[
8
]);
...
...
@@ -140,15 +140,15 @@ void magisk_cpio::restore() {
void
magisk_cpio
::
backup
(
const
char
*
orig
)
{
if
(
access
(
orig
,
R_OK
))
return
;
entry_map
bkup_entries
;
string
remv
;
auto
b
=
new
cpio_entry
(
".backup"
,
S_IFDIR
);
bkup_entries
[
b
->
filename
].
reset
(
b
);
entry_map
backups
;
string
rm_list
;
backups
.
emplace
(
".backup"
,
new
cpio_entry
(
S_IFDIR
));
magisk_cpio
o
(
orig
);
magisk_cpio
o
;
o
.
load_cpio
(
orig
);
// Remove
possible
backups in original ramdisk
// Remove
existing
backups in original ramdisk
o
.
rm
(
".backup"
,
true
);
rm
(
".backup"
,
true
);
...
...
@@ -157,7 +157,7 @@ void magisk_cpio::backup(const char *orig) {
while
(
lhs
!=
o
.
entries
.
end
()
||
rhs
!=
entries
.
end
())
{
int
res
;
bool
backup
=
false
;
bool
do_
backup
=
false
;
if
(
lhs
!=
o
.
entries
.
end
()
&&
rhs
!=
entries
.
end
())
{
res
=
lhs
->
first
.
compare
(
rhs
->
first
);
}
else
if
(
lhs
==
o
.
entries
.
end
())
{
...
...
@@ -167,29 +167,28 @@ void magisk_cpio::backup(const char *orig) {
}
if
(
res
<
0
)
{
// Something is missing in new ramdisk, backup!
backup
=
true
;
// Something is missing in new ramdisk,
do_
backup!
do_
backup
=
true
;
fprintf
(
stderr
,
"Backup missing entry: "
);
}
else
if
(
res
==
0
)
{
if
(
lhs
->
second
->
filesize
!=
rhs
->
second
->
filesize
||
memcmp
(
lhs
->
second
->
data
,
rhs
->
second
->
data
,
lhs
->
second
->
filesize
)
!=
0
)
{
// Not the same!
backup
=
true
;
do_
backup
=
true
;
fprintf
(
stderr
,
"Backup mismatch entry: "
);
}
}
else
{
// Something new in ramdisk
r
emv
+=
rhs
->
first
;
r
emv
+=
(
char
)
'\0'
;
r
m_list
+=
rhs
->
first
;
r
m_list
+=
(
char
)
'\0'
;
fprintf
(
stderr
,
"Record new entry: [%s] -> [.backup/.rmlist]
\n
"
,
rhs
->
first
.
data
());
}
if
(
backup
)
{
string
back_name
(
".backup/"
);
back_name
+=
lhs
->
first
;
fprintf
(
stderr
,
"[%s] -> [%s]
\n
"
,
lhs
->
first
.
data
(),
back_name
.
data
());
auto
ex
=
static_cast
<
cpio_entry
*>
(
lhs
->
second
.
release
());
ex
->
filename
=
back_name
;
bkup_entries
[
ex
->
filename
].
reset
(
ex
);
if
(
do_backup
)
{
string
name
=
".backup/"
+
lhs
->
first
;
fprintf
(
stderr
,
"[%s] -> [%s]
\n
"
,
lhs
->
first
.
data
(),
name
.
data
());
auto
e
=
lhs
->
second
.
release
();
backups
.
emplace
(
name
,
e
);
}
// Increment positions
...
...
@@ -202,16 +201,16 @@ void magisk_cpio::backup(const char *orig) {
}
}
if
(
!
r
emv
.
empty
())
{
auto
rm
list
=
new
cpio_entry
(
".backup/.rmlist"
,
S_IFREG
);
rm
list
->
filesize
=
remv
.
length
();
rm
list
->
data
=
xmalloc
(
remv
.
length
());
memcpy
(
rm
list
->
data
,
remv
.
data
(),
remv
.
length
());
b
kup_entries
[
rmlist
->
filename
].
reset
(
rmlist
);
if
(
!
r
m_list
.
empty
())
{
auto
rm
_list_file
=
new
cpio_entry
(
S_IFREG
);
rm
_list_file
->
filesize
=
rm_list
.
length
();
rm
_list_file
->
data
=
xmalloc
(
rm_list
.
length
());
memcpy
(
rm
_list_file
->
data
,
rm_list
.
data
(),
rm_list
.
length
());
b
ackups
.
emplace
(
".backup/.rmlist"
,
rm_list_file
);
}
if
(
b
kup_entrie
s
.
size
()
>
1
)
entries
.
merge
(
b
kup_entrie
s
);
if
(
b
ackup
s
.
size
()
>
1
)
entries
.
merge
(
b
ackup
s
);
}
int
cpio_commands
(
int
argc
,
char
*
argv
[])
{
...
...
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