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"); |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 46 | printf(" sideload\n"); |
nailyk-fr | 79605ae | 2017-04-17 14:24:40 +0200 | [diff] [blame] | 47 | printf(" set <variable> [value]\n"); |
bigbiff | adc599e | 2020-05-28 19:36:30 +0000 | [diff] [blame] | 48 | printf(" decrypt <password>\n"); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 49 | printf(" remountrw\n"); |
Nikolay | 29969ec | 2019-05-08 15:38:23 +0300 | [diff] [blame] | 50 | printf(" fixperms\n"); |
| 51 | printf(" mount <path>\n"); |
| 52 | printf(" unmount <path>\n"); |
| 53 | printf(" print <value>\n"); |
| 54 | printf(" mkdir <directory>\n"); |
| 55 | printf(" reboot [recovery|poweroff|bootloader|download|edl]\n"); |
nailyk-fr | 79605ae | 2017-04-17 14:24:40 +0200 | [diff] [blame] | 56 | printf("\nSee more documentation at https://twrp.me/faq/openrecoveryscript.html\n"); |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 57 | } |
| 58 | |
that | 6b9ad62 | 2017-01-18 23:34:28 +0100 | [diff] [blame] | 59 | int do_setcap(const char* filename, const char* capabilities) |
| 60 | { |
| 61 | uint64_t caps; |
| 62 | if (sscanf(capabilities, "%" SCNi64, &caps) != 1) |
| 63 | { |
| 64 | printf("setcap: invalid capabilities \"%s\"\n", filename); |
| 65 | return 1; |
| 66 | } |
| 67 | struct vfs_cap_data cap_data; |
| 68 | memset(&cap_data, 0, sizeof(cap_data)); |
| 69 | cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE; |
| 70 | cap_data.data[0].permitted = (uint32_t) (caps & 0xffffffff); |
| 71 | cap_data.data[0].inheritable = 0; |
| 72 | cap_data.data[1].permitted = (uint32_t) (caps >> 32); |
| 73 | cap_data.data[1].inheritable = 0; |
| 74 | if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) { |
| 75 | printf("setcap of %s to %" PRIx64 " failed: %s\n", |
| 76 | filename, caps, strerror(errno)); |
| 77 | return 1; |
| 78 | } |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | int do_getcap(const char* filename) |
| 83 | { |
| 84 | struct vfs_cap_data cap_data; |
| 85 | memset(&cap_data, 0, sizeof(cap_data)); |
| 86 | int rc = getxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(vfs_cap_data)); |
| 87 | if (rc > 0) |
| 88 | { |
| 89 | uint64_t caps = (uint64_t) cap_data.data[1].permitted << 32 | cap_data.data[0].permitted; |
| 90 | printf("0x%" PRIx64 "\n", caps); |
| 91 | } |
| 92 | else |
| 93 | printf("getcap of %s failed: %s\n", filename, strerror(errno)); |
| 94 | |
| 95 | return rc > 0; |
| 96 | } |
| 97 | |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 98 | int main(int argc, char **argv) { |
| 99 | int read_fd, write_fd, index; |
| 100 | char command[1024], result[512]; |
| 101 | |
| 102 | if (argc < 2 || strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "?") == 0 || strcmp(argv[1], "-h") == 0) { |
| 103 | print_usage(); |
| 104 | return 0; |
| 105 | } |
| 106 | if (strcmp(argv[1], "version") == 0 || strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) { |
| 107 | print_version(); |
| 108 | return 0; |
| 109 | } |
| 110 | |
that | 6b9ad62 | 2017-01-18 23:34:28 +0100 | [diff] [blame] | 111 | if (strcmp(argv[1], "setcap") == 0) { |
| 112 | if (argc != 4) |
| 113 | { |
| 114 | printf("Usage: setcap filename capabilities\n\n" |
| 115 | "capabilities must be specified as a number. Prefix with 0x for hexadecimal.\n"); |
| 116 | return 1; |
| 117 | } |
| 118 | return do_setcap(argv[2], argv[3]); |
| 119 | } |
| 120 | |
| 121 | if (strcmp(argv[1], "getcap") == 0) { |
| 122 | if (argc != 3) |
| 123 | { |
| 124 | printf("Usage: getcap filename\n"); |
| 125 | return 1; |
| 126 | } |
| 127 | return do_getcap(argv[2]); |
| 128 | } |
| 129 | |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 130 | sprintf(command, "%s", argv[1]); |
| 131 | for (index = 2; index < argc; index++) { |
| 132 | sprintf(command, "%s %s", command, argv[index]); |
| 133 | } |
| 134 | |
| 135 | write_fd = open(ORS_INPUT_FILE, O_WRONLY); |
| 136 | if (write_fd < 0) { |
| 137 | printf("TWRP does not appear to be running. Waiting for TWRP to start . . .\n"); |
| 138 | printf("Press CTRL + C to quit.\n"); |
| 139 | while (write_fd < 0) |
| 140 | write_fd = open(ORS_INPUT_FILE, O_WRONLY); |
| 141 | } |
| 142 | if (write(write_fd, command, sizeof(command)) != sizeof(command)) { |
| 143 | printf("Error sending command.\n"); |
| 144 | close(write_fd); |
| 145 | return -1; |
| 146 | } |
| 147 | read_fd = open(ORS_OUTPUT_FILE, O_RDONLY); |
| 148 | if (read_fd < 0) { |
| 149 | printf("Unable to open %s for read.\n", ORS_OUTPUT_FILE); |
| 150 | return -1; |
| 151 | } |
| 152 | memset(&result, 0, sizeof(result)); |
| 153 | while (read(read_fd, &result, sizeof(result)) > 0) { |
| 154 | result[510] = '\n'; |
| 155 | result[511] = '\0'; |
| 156 | printf("%s", result); |
| 157 | memset(&result, 0, sizeof(result)); |
| 158 | } |
| 159 | close(write_fd); |
| 160 | close(read_fd); |
| 161 | return 0; |
| 162 | } |