blob: fd8172dff8790431fabbff3611a2e28a9b968f7a [file] [log] [blame]
Ethan Yonker03a42f62014-08-08 11:03:51 -05001/*
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
that6b9ad622017-01-18 23:34:28 +010016#define __STDC_FORMAT_MACROS 1
Ethan Yonker03a42f62014-08-08 11:03:51 -050017#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
21#include <string.h>
22#include <fcntl.h>
that6b9ad622017-01-18 23:34:28 +010023#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 Yonker03a42f62014-08-08 11:03:51 -050030
31#include "orscmd.h"
32#include "../variables.h"
33
34void print_version(void) {
35 printf("TWRP openrecoveryscript command line tool, TWRP version %s\n\n", TW_VERSION_STR);
36}
37
38void 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-fr79605ae2017-04-17 14:24:40 +020043 printf(" backup <SDCRBAEM> [backupname]\n");
44 printf(" restore <SDCRBAEM> [backupname]\n");
45 printf(" wipe <partition name>\n");
Ethan Yonker03a42f62014-08-08 11:03:51 -050046 printf(" sideload\n");
nailyk-fr79605ae2017-04-17 14:24:40 +020047 printf(" set <variable> [value]\n");
48 printf(" decrypt <password>\n");
bigbiffce8f83c2015-12-12 18:30:21 -050049 printf(" remountrw\n");
nailyk-fr79605ae2017-04-17 14:24:40 +020050 printf("\nSee more documentation at https://twrp.me/faq/openrecoveryscript.html\n");
Ethan Yonker03a42f62014-08-08 11:03:51 -050051}
52
that6b9ad622017-01-18 23:34:28 +010053int do_setcap(const char* filename, const char* capabilities)
54{
55 uint64_t caps;
56 if (sscanf(capabilities, "%" SCNi64, &caps) != 1)
57 {
58 printf("setcap: invalid capabilities \"%s\"\n", filename);
59 return 1;
60 }
61 struct vfs_cap_data cap_data;
62 memset(&cap_data, 0, sizeof(cap_data));
63 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
64 cap_data.data[0].permitted = (uint32_t) (caps & 0xffffffff);
65 cap_data.data[0].inheritable = 0;
66 cap_data.data[1].permitted = (uint32_t) (caps >> 32);
67 cap_data.data[1].inheritable = 0;
68 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
69 printf("setcap of %s to %" PRIx64 " failed: %s\n",
70 filename, caps, strerror(errno));
71 return 1;
72 }
73 return 0;
74}
75
76int do_getcap(const char* filename)
77{
78 struct vfs_cap_data cap_data;
79 memset(&cap_data, 0, sizeof(cap_data));
80 int rc = getxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(vfs_cap_data));
81 if (rc > 0)
82 {
83 uint64_t caps = (uint64_t) cap_data.data[1].permitted << 32 | cap_data.data[0].permitted;
84 printf("0x%" PRIx64 "\n", caps);
85 }
86 else
87 printf("getcap of %s failed: %s\n", filename, strerror(errno));
88
89 return rc > 0;
90}
91
Ethan Yonker03a42f62014-08-08 11:03:51 -050092int main(int argc, char **argv) {
93 int read_fd, write_fd, index;
94 char command[1024], result[512];
95
96 if (argc < 2 || strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "?") == 0 || strcmp(argv[1], "-h") == 0) {
97 print_usage();
98 return 0;
99 }
100 if (strcmp(argv[1], "version") == 0 || strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) {
101 print_version();
102 return 0;
103 }
104
that6b9ad622017-01-18 23:34:28 +0100105 if (strcmp(argv[1], "setcap") == 0) {
106 if (argc != 4)
107 {
108 printf("Usage: setcap filename capabilities\n\n"
109 "capabilities must be specified as a number. Prefix with 0x for hexadecimal.\n");
110 return 1;
111 }
112 return do_setcap(argv[2], argv[3]);
113 }
114
115 if (strcmp(argv[1], "getcap") == 0) {
116 if (argc != 3)
117 {
118 printf("Usage: getcap filename\n");
119 return 1;
120 }
121 return do_getcap(argv[2]);
122 }
123
Ethan Yonker03a42f62014-08-08 11:03:51 -0500124 sprintf(command, "%s", argv[1]);
125 for (index = 2; index < argc; index++) {
126 sprintf(command, "%s %s", command, argv[index]);
127 }
128
129 write_fd = open(ORS_INPUT_FILE, O_WRONLY);
130 if (write_fd < 0) {
131 printf("TWRP does not appear to be running. Waiting for TWRP to start . . .\n");
132 printf("Press CTRL + C to quit.\n");
133 while (write_fd < 0)
134 write_fd = open(ORS_INPUT_FILE, O_WRONLY);
135 }
136 if (write(write_fd, command, sizeof(command)) != sizeof(command)) {
137 printf("Error sending command.\n");
138 close(write_fd);
139 return -1;
140 }
141 read_fd = open(ORS_OUTPUT_FILE, O_RDONLY);
142 if (read_fd < 0) {
143 printf("Unable to open %s for read.\n", ORS_OUTPUT_FILE);
144 return -1;
145 }
146 memset(&result, 0, sizeof(result));
147 while (read(read_fd, &result, sizeof(result)) > 0) {
148 result[510] = '\n';
149 result[511] = '\0';
150 printf("%s", result);
151 memset(&result, 0, sizeof(result));
152 }
153 close(write_fd);
154 close(read_fd);
155 return 0;
156}