Magisk/docs/tools.md

325 lines
14 KiB
Markdown
Raw Normal View History

2018-10-15 22:04:51 +00:00
# Magisk Tools
2022-01-25 10:32:52 +00:00
Magisk comes with a huge collections of tools for installation, daemons, and utilities for developers. This documentation covers the 4 binaries and all included applets. The binaries and applets are shown below:
2018-01-08 14:33:55 +00:00
```
magiskboot /* binary */
magiskinit /* binary */
magiskpolicy /* binary */
supolicy -> magiskpolicy
2018-01-08 14:33:55 +00:00
magisk /* binary */
resetprop -> magisk
su -> magisk
```
### magiskboot
2022-01-25 10:32:52 +00:00
2018-10-15 04:46:37 +00:00
A tool to unpack / repack boot images, parse / patch / extract cpio, patch dtb, hex patch binaries, and compress / decompress files with multiple algorithms.
2018-01-08 14:33:55 +00:00
`magiskboot` natively supports (which means it does not rely on external tools) common compression formats including `gzip`, `lz4`, `lz4_legacy` , `lz4_lg` ([the LG edition](https://events.static.linuxfound.org/sites/events/files/lcjpcojp13_klee.pdf) of `lz4_legacy`, only used on LG), `lzma`, `xz`, and `bzip2`.
2018-01-08 14:33:55 +00:00
2019-03-28 08:54:06 +00:00
The concept of `magiskboot` is to make boot image modification simpler. For unpacking, it parses the header and extracts all sections in the image, decompressing on-the-fly if compression is detected in any sections. For repacking, the original boot image is required so the original headers can be used, changing only the necessary entries such as section sizes and checksum. All sections will be compressed back to the original format if required. The tool also supports many CPIO and DTB operations.
2018-01-08 14:33:55 +00:00
```
2022-01-25 10:32:52 +00:00
Usage: ./magiskboot <action> [args...]
2018-01-08 14:33:55 +00:00
Supported actions:
2020-01-02 05:46:59 +00:00
unpack [-n] [-h] <bootimg>
2022-06-19 09:35:05 +00:00
Unpack <bootimg> to its individual components, each component to
a file with its corresponding file name in the current directory.
Supported components: kernel, kernel_dtb, ramdisk.cpio, second,
dtb, extra, and recovery_dtbo.
By default, each component will be automatically decompressed
on-the-fly before writing to the output file.
If '-n' is provided, all decompression operations will be skipped;
each component will remain untouched, dumped in its original format.
If '-h' is provided, the boot image header information will be
dumped to the file 'header', which can be used to modify header
configurations during repacking.
2019-03-28 08:54:06 +00:00
Return values:
0:valid 1:error 2:chromeos
2019-09-19 09:00:29 +00:00
repack [-n] <origbootimg> [outbootimg]
2022-06-19 09:35:05 +00:00
Repack boot image components using files from the current directory
to [outbootimg], or 'new-boot.img' if not specified.
<origbootimg> is the original boot image used to unpack the components.
By default, each component will be automatically compressed using its
corresponding format detected in <origbootimg>. If a component file
in the current directory is already compressed, then no addition
compression will be performed for that specific component.
If '-n' is provided, all compression operations will be skipped.
If env variable PATCHVBMETAFLAG is set to true, all disable flags in
the boot image's vbmeta header will be set.
2018-10-15 04:46:37 +00:00
verify <bootimg> [x509.pem]
Check whether the boot image is signed with AVB 1.0 signature.
Optionally provide a certificate to verify whether the image is
signed by the public key certificate.
Return value:
0:valid 1:error
sign <bootimg> [name] [x509.pem pk8]
Sign <bootimg> with AVB 1.0 signature.
Optionally provide the name of the image (default: '/boot').
Optionally provide the certificate/private key pair for signing.
If the certificate/private key pair is not provided, the AOSP
verity key bundled in the executable will be used.
extract <payload.bin> [partition] [outfile]
Extract [partition] from <payload.bin> to [outfile].
If [outfile] is not specified, then output to '[partition].img'.
If [partition] is not specified, then attempt to extract either
'init_boot' or 'boot'. Which partition was chosen can be determined
by whichever 'init_boot.img' or 'boot.img' exists.
<payload.bin> can be '-' to be STDIN.
2019-03-28 08:54:06 +00:00
hexpatch <file> <hexpattern1> <hexpattern2>
2022-06-19 09:35:05 +00:00
Search <hexpattern1> in <file>, and replace it with <hexpattern2>
2018-10-15 04:46:37 +00:00
2019-03-28 08:54:06 +00:00
cpio <incpio> [commands...]
2020-10-03 04:47:47 +00:00
Do cpio commands to <incpio> (modifications are done in-place)
2022-06-19 09:35:05 +00:00
Each command is a single argument, add quotes for each command.
2018-10-15 04:46:37 +00:00
Supported commands:
2019-03-28 08:54:06 +00:00
exists ENTRY
Return 0 if ENTRY exists, else return 1
2018-10-15 04:46:37 +00:00
rm [-r] ENTRY
Remove ENTRY, specify [-r] to remove recursively
mkdir MODE ENTRY
Create directory ENTRY in permissions MODE
ln TARGET ENTRY
Create a symlink to TARGET with the name ENTRY
mv SOURCE DEST
Move SOURCE to DEST
add MODE ENTRY INFILE
Add INFILE as ENTRY in permissions MODE; replaces ENTRY if exists
extract [ENTRY OUT]
Extract ENTRY to OUT, or extract all entries to current directory
test
2022-06-19 09:35:05 +00:00
Test the cpio's status
2022-01-25 10:32:52 +00:00
Return value is 0 or bitwise or-ed of following values:
0x1:Magisk 0x2:unsupported 0x4:Sony
2020-01-02 05:46:59 +00:00
patch
2020-10-03 04:47:47 +00:00
Apply ramdisk patches
Configure with env variables: KEEPVERITY KEEPFORCEENCRYPT
2019-03-28 08:54:06 +00:00
backup ORIG
2018-10-15 04:46:37 +00:00
Create ramdisk backups from ORIG
restore
Restore ramdisk from ramdisk backup stored within incpio
2022-06-19 09:35:05 +00:00
dtb <file> <action> [args...]
Do dtb related actions to <file>
2020-01-02 05:46:59 +00:00
Supported actions:
print [-f]
Print all contents of dtb for debugging
Specify [-f] to only print fstab nodes
2020-10-03 04:47:47 +00:00
patch
2018-10-15 04:46:37 +00:00
Search for fstab and remove verity/avb
2020-10-03 04:47:47 +00:00
Modifications are done directly to the file in-place
Configure with env variables: KEEPVERITY
2022-06-19 09:35:05 +00:00
test
Test the fstab's status
Return values:
0:valid 1:error
2020-01-02 05:46:59 +00:00
2022-06-19 09:35:05 +00:00
split <file>
2020-01-02 05:46:59 +00:00
Split image.*-dtb into kernel + kernel_dtb
sha1 <file>
Print the SHA1 checksum for <file>
cleanup
Cleanup the current working directory
2018-10-15 04:46:37 +00:00
2022-01-25 10:32:52 +00:00
compress[=format] <infile> [outfile]
2022-06-19 09:35:05 +00:00
Compress <infile> with [format] to [outfile].
<infile>/[outfile] can be '-' to be STDIN/STDOUT.
If [format] is not specified, then gzip will be used.
If [outfile] is not specified, then <infile> will be replaced
with another file suffixed with a matching file extension.
Supported formats: gzip zopfli xz lzma bzip2 lz4 lz4_legacy lz4_lg
2018-10-15 04:46:37 +00:00
2019-03-28 08:54:06 +00:00
decompress <infile> [outfile]
2022-06-19 09:35:05 +00:00
Detect format and decompress <infile> to [outfile].
<infile>/[outfile] can be '-' to be STDIN/STDOUT.
If [outfile] is not specified, then <infile> will be replaced
with another file removing its archive format file extension.
Supported formats: gzip zopfli xz lzma bzip2 lz4 lz4_legacy lz4_lg
2018-01-08 14:33:55 +00:00
```
### magiskinit
2022-01-25 10:32:52 +00:00
2019-03-28 08:54:06 +00:00
This binary will replace `init` in the ramdisk of a Magisk patched boot image. It is originally created for supporting devices using system-as-root, but the tool is extended to support all devices and became a crucial part of Magisk. More details can be found in the **Pre-Init** section in [Magisk Booting Process](details.md#magisk-booting-process).
2018-01-08 14:33:55 +00:00
### magiskpolicy
2022-01-25 10:32:52 +00:00
2018-01-08 14:33:55 +00:00
(This tool is aliased to `supolicy` for compatibility with SuperSU's sepolicy tool)
This tool could be used for advanced developers to modify SELinux policies. In common scenarios like Linux server admins, they would directly modify the SELinux policy sources (`*.te`) and recompile the `sepolicy` binary, but here on Android we directly patch the binary file (or runtime policies).
2018-01-08 14:33:55 +00:00
2018-10-15 04:46:37 +00:00
All processes spawned from the Magisk daemon, including root shells and all its forks, are running in the context `u:r:magisk:s0`. The rule used on all Magisk installed systems can be viewed as stock `sepolicy` with these patches: `magiskpolicy --magisk 'allow magisk * * *'`.
2018-01-08 14:33:55 +00:00
```
2022-01-25 10:32:52 +00:00
Usage: ./magiskpolicy [--options...] [policy statements...]
2018-01-08 14:33:55 +00:00
Options:
2019-03-28 08:54:06 +00:00
--help show help message for policy statements
2022-01-25 10:32:52 +00:00
--load FILE load monolithic sepolicy from FILE
2020-01-02 05:45:08 +00:00
--load-split load from precompiled sepolicy or compile
2022-01-25 10:32:52 +00:00
split cil policies
2019-03-28 08:54:06 +00:00
--compile-split compile split cil policies
2022-01-25 10:32:52 +00:00
--save FILE dump monolithic sepolicy to FILE
--live immediately load sepolicy into the kernel
--magisk apply built-in Magisk sepolicy rules
2020-01-02 05:45:08 +00:00
--apply FILE apply rules from FILE, read and parsed
line by line as policy statements
2022-01-25 10:32:52 +00:00
(multiple --apply are allowed)
2018-01-08 14:33:55 +00:00
2022-01-25 10:32:52 +00:00
If neither --load, --load-split, nor --compile-split is specified,
it will load from current live policies (/sys/fs/selinux/policy)
2018-01-08 14:33:55 +00:00
One policy statement should be treated as one parameter;
2020-10-03 04:47:47 +00:00
this means each policy statement should be enclosed in quotes.
2020-01-02 05:45:08 +00:00
Multiple policy statements can be provided in a single command.
2018-01-08 14:33:55 +00:00
2020-10-03 04:47:47 +00:00
Statements has a format of "<rule_name> [args...]".
Arguments labeled with (^) can accept one or more entries. Multiple
entries consist of a space separated list enclosed in braces ({}).
Arguments labeled with (*) are the same as (^), but additionally
support the match-all operator (*).
Example: "allow { s1 s2 } { t1 t2 } class *"
Will be expanded to:
allow s1 t1 class { all-permissions-of-class }
allow s1 t2 class { all-permissions-of-class }
allow s2 t1 class { all-permissions-of-class }
allow s2 t2 class { all-permissions-of-class }
2018-01-08 14:33:55 +00:00
Supported policy statements:
2020-10-03 04:47:47 +00:00
"allow *source_type *target_type *class *perm_set"
"deny *source_type *target_type *class *perm_set"
"auditallow *source_type *target_type *class *perm_set"
"dontaudit *source_type *target_type *class *perm_set"
2018-01-08 14:33:55 +00:00
2020-10-03 04:47:47 +00:00
"allowxperm *source_type *target_type *class operation xperm_set"
"auditallowxperm *source_type *target_type *class operation xperm_set"
"dontauditxperm *source_type *target_type *class operation xperm_set"
- The only supported operation is 'ioctl'
- xperm_set format is either 'low-high', 'value', or '*'.
'*' will be treated as '0x0000-0xFFFF'.
All values should be written in hexadecimal.
2018-01-08 14:33:55 +00:00
2020-10-03 04:47:47 +00:00
"permissive ^type"
"enforce ^type"
2018-01-08 14:33:55 +00:00
2020-10-03 04:47:47 +00:00
"typeattribute ^type ^attribute"
2018-01-08 14:33:55 +00:00
2020-10-03 04:47:47 +00:00
"type type_name ^(attribute)"
- Argument 'attribute' is optional, default to 'domain'
2019-03-28 08:54:06 +00:00
2020-10-03 04:47:47 +00:00
"attribute attribute_name"
2018-01-08 14:33:55 +00:00
2020-10-03 04:47:47 +00:00
"type_transition source_type target_type class default_type (object_name)"
- Argument 'object_name' is optional
2018-01-08 14:33:55 +00:00
2020-10-03 04:47:47 +00:00
"type_change source_type target_type class default_type"
"type_member source_type target_type class default_type"
2018-01-08 14:33:55 +00:00
2020-10-03 04:47:47 +00:00
"genfscon fs_name partial_path fs_context"
2018-01-08 14:33:55 +00:00
```
### magisk
2022-01-25 10:32:52 +00:00
2020-10-03 04:47:47 +00:00
When the magisk binary is called with the name `magisk`, it works as a utility tool with many helper functions and the entry points for several Magisk services.
2018-01-08 14:33:55 +00:00
```
Usage: magisk [applet [arguments]...]
or: magisk [options]...
Options:
-c print current binary version
-v print running daemon version
-V print running daemon version code
--list list all available applets
--remove-modules [-n] remove all modules, reboot if -n is not provided
2020-10-03 04:47:47 +00:00
--install-module ZIP install a module zip file
2019-03-28 08:54:06 +00:00
Advanced Options (Internal APIs):
2020-10-03 04:47:47 +00:00
--daemon manually start magisk daemon
2022-01-25 10:32:52 +00:00
--stop remove all magisk changes and stop daemon
2022-06-19 09:35:05 +00:00
--[init trigger] callback on init triggers. Valid triggers:
post-fs-data, service, boot-complete, zygote-restart
2018-01-08 14:33:55 +00:00
--unlock-blocks set BLKROSET flag to OFF for all block devices
2019-03-28 08:54:06 +00:00
--restorecon restore selinux context on Magisk files
2018-01-08 14:33:55 +00:00
--clone-attr SRC DEST clone permission, owner, and selinux context
2019-03-28 08:54:06 +00:00
--clone SRC DEST clone SRC to DEST
2019-09-19 09:00:29 +00:00
--sqlite SQL exec SQL commands to Magisk database
2020-10-03 04:47:47 +00:00
--path print Magisk tmpfs mount path
2022-01-25 10:32:52 +00:00
--denylist ARGS denylist config CLI
--preinit-device resolve a device to store preinit files
2018-01-08 14:33:55 +00:00
2020-10-03 04:47:47 +00:00
Available applets:
2022-01-25 10:32:52 +00:00
su, resetprop
Usage: magisk --denylist [action [arguments...] ]
Actions:
status Return the enforcement status
enable Enable denylist enforcement
disable Disable denylist enforcement
add PKG [PROC] Add a new target to the denylist
rm PKG [PROC] Remove target(s) from the denylist
ls Print the current denylist
exec CMDs... Execute commands in isolated mount
namespace and do all unmounts
2018-01-08 14:33:55 +00:00
```
### su
2022-01-25 10:32:52 +00:00
2018-10-15 04:46:37 +00:00
An applet of `magisk`, the MagiskSU entry point. Good old `su` command.
2018-01-08 14:33:55 +00:00
```
Usage: su [options] [-] [user [argument...]]
Options:
-c, --command COMMAND Pass COMMAND to the invoked shell
-g, --group GROUP Specify the primary group
-G, --supp-group GROUP Specify a supplementary group.
The first specified supplementary group is also used
as a primary group if the option -g is not specified.
-Z, --context CONTEXT Change SELinux context
-t, --target PID PID to take mount namespace from
-h, --help Display this help message and exit
-, -l, --login Pretend the shell to be a login shell
2018-01-08 14:33:55 +00:00
-m, -p,
--preserve-environment Preserve the entire environment
-s, --shell SHELL Use SHELL instead of the default /system/bin/sh
-v, --version Display version number and exit
-V Display version code and exit
2018-01-08 14:33:55 +00:00
-mm, -M,
--mount-master Force run in the global mount namespace
2018-01-08 14:33:55 +00:00
```
### resetprop
2022-01-25 10:32:52 +00:00
2018-10-15 04:46:37 +00:00
An applet of `magisk`. An advanced system property manipulation utility. Check the [Resetprop Details](details.md#resetprop) for more background information.
2018-01-08 14:33:55 +00:00
```
Usage: resetprop [flags] [options...]
Options:
-h, --help show this message
(no arguments) print all properties
NAME get property
NAME VALUE set property entry NAME with VALUE
--file FILE load props from FILE
--delete NAME delete property
Flags:
-v print verbose output to stderr
2020-11-13 12:32:20 +00:00
-n set props without going through property_service
(this flag only affects setprop)
-p read/write props from/to persistent storage
(this flag only affects getprop and delprop)
2018-01-08 14:33:55 +00:00
```