blob: 8264a77927f26e3793f6b55ca255a42d0154b161 [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");
Adrian DCf7a54e52021-01-05 22:34:21 +010046 printf(" format data\n");
Ethan Yonker03a42f62014-08-08 11:03:51 -050047 printf(" sideload\n");
nailyk-fr79605ae2017-04-17 14:24:40 +020048 printf(" set <variable> [value]\n");
Noah Jacobson81d638d2019-04-28 00:10:07 -040049 printf(" decrypt <password> [USER ID]\n");
bigbiffce8f83c2015-12-12 18:30:21 -050050 printf(" remountrw\n");
Nikolay29969ec2019-05-08 15:38:23 +030051 printf(" fixperms\n");
52 printf(" mount <path>\n");
53 printf(" unmount <path>\n");
bigbiffee7b7ff2020-03-23 15:08:27 -040054 printf(" listmounts\n");
Nikolay29969ec2019-05-08 15:38:23 +030055 printf(" print <value>\n");
56 printf(" mkdir <directory>\n");
57 printf(" reboot [recovery|poweroff|bootloader|download|edl]\n");
nailyk-fr79605ae2017-04-17 14:24:40 +020058 printf("\nSee more documentation at https://twrp.me/faq/openrecoveryscript.html\n");
Ethan Yonker03a42f62014-08-08 11:03:51 -050059}
60
that6b9ad622017-01-18 23:34:28 +010061int do_setcap(const char* filename, const char* capabilities)
62{
63 uint64_t caps;
64 if (sscanf(capabilities, "%" SCNi64, &caps) != 1)
65 {
66 printf("setcap: invalid capabilities \"%s\"\n", filename);
67 return 1;
68 }
69 struct vfs_cap_data cap_data;
70 memset(&cap_data, 0, sizeof(cap_data));
71 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
72 cap_data.data[0].permitted = (uint32_t) (caps & 0xffffffff);
73 cap_data.data[0].inheritable = 0;
74 cap_data.data[1].permitted = (uint32_t) (caps >> 32);
75 cap_data.data[1].inheritable = 0;
76 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
77 printf("setcap of %s to %" PRIx64 " failed: %s\n",
78 filename, caps, strerror(errno));
79 return 1;
80 }
81 return 0;
82}
83
84int do_getcap(const char* filename)
85{
86 struct vfs_cap_data cap_data;
87 memset(&cap_data, 0, sizeof(cap_data));
88 int rc = getxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(vfs_cap_data));
89 if (rc > 0)
90 {
91 uint64_t caps = (uint64_t) cap_data.data[1].permitted << 32 | cap_data.data[0].permitted;
92 printf("0x%" PRIx64 "\n", caps);
93 }
94 else
95 printf("getcap of %s failed: %s\n", filename, strerror(errno));
96
97 return rc > 0;
98}
99
Ethan Yonker03a42f62014-08-08 11:03:51 -0500100int main(int argc, char **argv) {
101 int read_fd, write_fd, index;
102 char command[1024], result[512];
103
104 if (argc < 2 || strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "?") == 0 || strcmp(argv[1], "-h") == 0) {
105 print_usage();
106 return 0;
107 }
108 if (strcmp(argv[1], "version") == 0 || strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) {
109 print_version();
110 return 0;
111 }
112
that6b9ad622017-01-18 23:34:28 +0100113 if (strcmp(argv[1], "setcap") == 0) {
114 if (argc != 4)
115 {
116 printf("Usage: setcap filename capabilities\n\n"
117 "capabilities must be specified as a number. Prefix with 0x for hexadecimal.\n");
118 return 1;
119 }
120 return do_setcap(argv[2], argv[3]);
121 }
122
123 if (strcmp(argv[1], "getcap") == 0) {
124 if (argc != 3)
125 {
126 printf("Usage: getcap filename\n");
127 return 1;
128 }
129 return do_getcap(argv[2]);
130 }
131
Ethan Yonker03a42f62014-08-08 11:03:51 -0500132 sprintf(command, "%s", argv[1]);
133 for (index = 2; index < argc; index++) {
134 sprintf(command, "%s %s", command, argv[index]);
135 }
136
137 write_fd = open(ORS_INPUT_FILE, O_WRONLY);
138 if (write_fd < 0) {
139 printf("TWRP does not appear to be running. Waiting for TWRP to start . . .\n");
140 printf("Press CTRL + C to quit.\n");
141 while (write_fd < 0)
142 write_fd = open(ORS_INPUT_FILE, O_WRONLY);
143 }
144 if (write(write_fd, command, sizeof(command)) != sizeof(command)) {
145 printf("Error sending command.\n");
146 close(write_fd);
147 return -1;
148 }
149 read_fd = open(ORS_OUTPUT_FILE, O_RDONLY);
150 if (read_fd < 0) {
151 printf("Unable to open %s for read.\n", ORS_OUTPUT_FILE);
152 return -1;
153 }
154 memset(&result, 0, sizeof(result));
155 while (read(read_fd, &result, sizeof(result)) > 0) {
156 result[510] = '\n';
157 result[511] = '\0';
158 printf("%s", result);
159 memset(&result, 0, sizeof(result));
160 }
161 close(write_fd);
162 close(read_fd);
163 return 0;
164}