Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 1 | /* |
| 2 | TWRP is free software: you can redistribute it and/or modify |
| 3 | it under the terms of the GNU General Public License as published by |
| 4 | the Free Software Foundation, either version 3 of the License, or |
| 5 | (at your option) any later version. |
| 6 | |
| 7 | TWRP is distributed in the hope that it will be useful, |
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | GNU General Public License for more details. |
| 11 | |
| 12 | You should have received a copy of the GNU General Public License |
| 13 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 14 | */ |
| 15 | |
that | 6b9ad62 | 2017-01-18 23:34:28 +0100 | [diff] [blame] | 16 | #define __STDC_FORMAT_MACROS 1 |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <unistd.h> |
| 21 | #include <string.h> |
| 22 | #include <fcntl.h> |
that | 6b9ad62 | 2017-01-18 23:34:28 +0100 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <inttypes.h> |
| 25 | |
| 26 | // for setcap and getcap |
| 27 | #include <sys/capability.h> |
| 28 | #include <sys/xattr.h> |
| 29 | #include <linux/xattr.h> |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 30 | |
| 31 | #include "orscmd.h" |
| 32 | #include "../variables.h" |
| 33 | |
| 34 | void print_version(void) { |
| 35 | printf("TWRP openrecoveryscript command line tool, TWRP version %s\n\n", TW_VERSION_STR); |
| 36 | } |
| 37 | |
| 38 | void print_usage(void) { |
| 39 | print_version(); |
| 40 | printf("Allows command line usage of TWRP via openrecoveryscript commands.\n"); |
| 41 | printf("Some common commands include:\n"); |
| 42 | printf(" install /path/to/update.zip\n"); |
nailyk-fr | 79605ae | 2017-04-17 14:24:40 +0200 | [diff] [blame] | 43 | printf(" backup <SDCRBAEM> [backupname]\n"); |
| 44 | printf(" restore <SDCRBAEM> [backupname]\n"); |
| 45 | printf(" wipe <partition name>\n"); |
Adrian DC | 3b3bd12 | 2021-01-05 22:34:21 +0100 | [diff] [blame] | 46 | printf(" format data\n"); |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 47 | printf(" sideload\n"); |
nailyk-fr | 79605ae | 2017-04-17 14:24:40 +0200 | [diff] [blame] | 48 | printf(" set <variable> [value]\n"); |
Noah Jacobson | 5a79f67 | 2019-04-28 00:10:07 -0400 | [diff] [blame] | 49 | printf(" decrypt <password> [USER ID]\n"); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 50 | printf(" remountrw\n"); |
Nikolay | 29969ec | 2019-05-08 15:38:23 +0300 | [diff] [blame] | 51 | printf(" fixperms\n"); |
| 52 | printf(" mount <path>\n"); |
| 53 | printf(" unmount <path>\n"); |
| 54 | printf(" print <value>\n"); |
| 55 | printf(" mkdir <directory>\n"); |
| 56 | printf(" reboot [recovery|poweroff|bootloader|download|edl]\n"); |
nailyk-fr | 79605ae | 2017-04-17 14:24:40 +0200 | [diff] [blame] | 57 | printf("\nSee more documentation at https://twrp.me/faq/openrecoveryscript.html\n"); |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 58 | } |
| 59 | |
that | 6b9ad62 | 2017-01-18 23:34:28 +0100 | [diff] [blame] | 60 | int do_setcap(const char* filename, const char* capabilities) |
| 61 | { |
| 62 | uint64_t caps; |
| 63 | if (sscanf(capabilities, "%" SCNi64, &caps) != 1) |
| 64 | { |
| 65 | printf("setcap: invalid capabilities \"%s\"\n", filename); |
| 66 | return 1; |
| 67 | } |
| 68 | struct vfs_cap_data cap_data; |
| 69 | memset(&cap_data, 0, sizeof(cap_data)); |
| 70 | cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE; |
| 71 | cap_data.data[0].permitted = (uint32_t) (caps & 0xffffffff); |
| 72 | cap_data.data[0].inheritable = 0; |
| 73 | cap_data.data[1].permitted = (uint32_t) (caps >> 32); |
| 74 | cap_data.data[1].inheritable = 0; |
| 75 | if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) { |
| 76 | printf("setcap of %s to %" PRIx64 " failed: %s\n", |
| 77 | filename, caps, strerror(errno)); |
| 78 | return 1; |
| 79 | } |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | int do_getcap(const char* filename) |
| 84 | { |
| 85 | struct vfs_cap_data cap_data; |
| 86 | memset(&cap_data, 0, sizeof(cap_data)); |
| 87 | int rc = getxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(vfs_cap_data)); |
| 88 | if (rc > 0) |
| 89 | { |
| 90 | uint64_t caps = (uint64_t) cap_data.data[1].permitted << 32 | cap_data.data[0].permitted; |
| 91 | printf("0x%" PRIx64 "\n", caps); |
| 92 | } |
| 93 | else |
| 94 | printf("getcap of %s failed: %s\n", filename, strerror(errno)); |
| 95 | |
| 96 | return rc > 0; |
| 97 | } |
| 98 | |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 99 | int main(int argc, char **argv) { |
| 100 | int read_fd, write_fd, index; |
| 101 | char command[1024], result[512]; |
| 102 | |
| 103 | if (argc < 2 || strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "?") == 0 || strcmp(argv[1], "-h") == 0) { |
| 104 | print_usage(); |
| 105 | return 0; |
| 106 | } |
| 107 | if (strcmp(argv[1], "version") == 0 || strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) { |
| 108 | print_version(); |
| 109 | return 0; |
| 110 | } |
| 111 | |
that | 6b9ad62 | 2017-01-18 23:34:28 +0100 | [diff] [blame] | 112 | if (strcmp(argv[1], "setcap") == 0) { |
| 113 | if (argc != 4) |
| 114 | { |
| 115 | printf("Usage: setcap filename capabilities\n\n" |
| 116 | "capabilities must be specified as a number. Prefix with 0x for hexadecimal.\n"); |
| 117 | return 1; |
| 118 | } |
| 119 | return do_setcap(argv[2], argv[3]); |
| 120 | } |
| 121 | |
| 122 | if (strcmp(argv[1], "getcap") == 0) { |
| 123 | if (argc != 3) |
| 124 | { |
| 125 | printf("Usage: getcap filename\n"); |
| 126 | return 1; |
| 127 | } |
| 128 | return do_getcap(argv[2]); |
| 129 | } |
| 130 | |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 131 | sprintf(command, "%s", argv[1]); |
| 132 | for (index = 2; index < argc; index++) { |
| 133 | sprintf(command, "%s %s", command, argv[index]); |
| 134 | } |
| 135 | |
| 136 | write_fd = open(ORS_INPUT_FILE, O_WRONLY); |
| 137 | if (write_fd < 0) { |
| 138 | printf("TWRP does not appear to be running. Waiting for TWRP to start . . .\n"); |
| 139 | printf("Press CTRL + C to quit.\n"); |
| 140 | while (write_fd < 0) |
| 141 | write_fd = open(ORS_INPUT_FILE, O_WRONLY); |
| 142 | } |
| 143 | if (write(write_fd, command, sizeof(command)) != sizeof(command)) { |
| 144 | printf("Error sending command.\n"); |
| 145 | close(write_fd); |
| 146 | return -1; |
| 147 | } |
| 148 | read_fd = open(ORS_OUTPUT_FILE, O_RDONLY); |
| 149 | if (read_fd < 0) { |
| 150 | printf("Unable to open %s for read.\n", ORS_OUTPUT_FILE); |
| 151 | return -1; |
| 152 | } |
| 153 | memset(&result, 0, sizeof(result)); |
| 154 | while (read(read_fd, &result, sizeof(result)) > 0) { |
| 155 | result[510] = '\n'; |
| 156 | result[511] = '\0'; |
| 157 | printf("%s", result); |
| 158 | memset(&result, 0, sizeof(result)); |
| 159 | } |
| 160 | close(write_fd); |
| 161 | close(read_fd); |
| 162 | return 0; |
| 163 | } |