blob: b17c34fb23c2a49ae2f9d2c46fb973cc36e68aab [file] [log] [blame]
Doug Zongker9931f7f2009-06-10 14:11:53 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Doug Zongkerfbf3c102009-06-24 09:36:20 -070017#include <ctype.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070018#include <errno.h>
19#include <stdarg.h>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070020#include <stdio.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070021#include <stdlib.h>
22#include <string.h>
23#include <sys/mount.h>
24#include <sys/stat.h>
25#include <sys/types.h>
Doug Zongkera3f89ea2009-09-10 14:10:48 -070026#include <sys/wait.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070027#include <unistd.h>
Hristo Bojinovdb314d62010-08-02 10:29:49 -070028#include <fcntl.h>
29#include <time.h>
Nick Kralevich5dbdef02013-09-07 14:41:06 -070030#include <selinux/selinux.h>
31#include <ftw.h>
32#include <sys/capability.h>
33#include <sys/xattr.h>
34#include <linux/xattr.h>
35#include <inttypes.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070036
Yabin Cui64be2132016-02-03 18:16:02 -080037#include <memory>
38#include <vector>
39
Elliott Hughes4b166f02015-12-04 15:30:20 -080040#include <android-base/parseint.h>
41#include <android-base/strings.h>
42#include <android-base/stringprintf.h>
Tao Bao1107d962015-09-09 17:16:55 -070043
Doug Zongkerc87bab12013-11-25 13:53:25 -080044#include "bootloader.h"
45#include "applypatch/applypatch.h"
46#include "cutils/android_reboot.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070047#include "cutils/misc.h"
48#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070049#include "edify/expr.h"
Tianjie Xu16255832016-04-30 11:49:59 -070050#include "error_code.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070051#include "minzip/DirUtil.h"
52#include "mtdutils/mounts.h"
53#include "mtdutils/mtdutils.h"
Tianjie Xu16255832016-04-30 11:49:59 -070054#include "openssl/sha.h"
Jed Estepff6df892015-12-15 16:04:53 -080055#include "ota_io.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070056#include "updater.h"
Doug Zongker512536a2010-02-17 16:11:44 -080057#include "applypatch/applypatch.h"
Dees_Troy512376c2013-09-03 19:39:41 +000058#include "flashutils/flashutils.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080059#include "install.h"
Ethan Yonker7e1b9862015-03-19 14:10:01 -050060#ifdef HAVE_LIBTUNE2FS
Michael Rungeb278c252014-11-21 00:12:28 -080061#include "tune2fs.h"
Ethan Yonker7e1b9862015-03-19 14:10:01 -050062#endif
Doug Zongker8edb00c2009-06-11 17:21:44 -070063
Doug Zongker3d177d02010-07-01 09:18:44 -070064#ifdef USE_EXT4
65#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080066#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070067#endif
68
Tao Bao1107d962015-09-09 17:16:55 -070069// Send over the buffer to recovery though the command pipe.
70static void uiPrint(State* state, const std::string& buffer) {
71 UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070072
Tao Bao1107d962015-09-09 17:16:55 -070073 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
74 // So skip sending empty strings to UI.
75 std::vector<std::string> lines = android::base::Split(buffer, "\n");
76 for (auto& line: lines) {
77 if (!line.empty()) {
78 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
79 fprintf(ui->cmd_pipe, "ui_print\n");
80 }
Michael Runge75480252014-10-22 19:48:41 -070081 }
Tao Bao1107d962015-09-09 17:16:55 -070082
83 // On the updater side, we need to dump the contents to stderr (which has
84 // been redirected to the log file). Because the recovery will only print
85 // the contents to screen when processing pipe command ui_print.
86 fprintf(stderr, "%s", buffer.c_str());
Michael Runge75480252014-10-22 19:48:41 -070087}
88
89__attribute__((__format__(printf, 2, 3))) __nonnull((2))
90void uiPrintf(State* state, const char* format, ...) {
Tao Bao1107d962015-09-09 17:16:55 -070091 std::string error_msg;
92
Michael Runge75480252014-10-22 19:48:41 -070093 va_list ap;
94 va_start(ap, format);
Tao Bao1107d962015-09-09 17:16:55 -070095 android::base::StringAppendV(&error_msg, format, ap);
Michael Runge75480252014-10-22 19:48:41 -070096 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070097
Michael Runge75480252014-10-22 19:48:41 -070098 uiPrint(state, error_msg);
99}
100
Doug Zongker52b40362014-02-10 15:30:30 -0800101// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700102char* PrintSha1(const uint8_t* digest) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800103 char* buffer = reinterpret_cast<char*>(malloc(SHA_DIGEST_LENGTH*2 + 1));
Doug Zongker52b40362014-02-10 15:30:30 -0800104 const char* alphabet = "0123456789abcdef";
Tao Baoba9a42a2015-06-23 23:23:33 -0700105 size_t i;
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800106 for (i = 0; i < SHA_DIGEST_LENGTH; ++i) {
Doug Zongker52b40362014-02-10 15:30:30 -0800107 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
108 buffer[i*2+1] = alphabet[digest[i] & 0xf];
109 }
110 buffer[i*2] = '\0';
111 return buffer;
112}
113
Doug Zongker3d177d02010-07-01 09:18:44 -0700114// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700115//
Doug Zongker3d177d02010-07-01 09:18:44 -0700116// fs_type="yaffs2" partition_type="MTD" location=partition
117// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800118Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700119 char* result = NULL;
Michael Rungebd6138c2014-10-22 17:05:08 -0700120 if (argc != 4 && argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700121 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700122 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700123 char* fs_type;
124 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700125 char* location;
126 char* mount_point;
Michael Rungebd6138c2014-10-22 17:05:08 -0700127 char* mount_options;
128 bool has_mount_options;
129 if (argc == 5) {
130 has_mount_options = true;
131 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
132 &location, &mount_point, &mount_options) < 0) {
133 return NULL;
134 }
135 } else {
136 has_mount_options = false;
137 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700138 &location, &mount_point) < 0) {
Michael Rungebd6138c2014-10-22 17:05:08 -0700139 return NULL;
140 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700141 }
142
Doug Zongker3d177d02010-07-01 09:18:44 -0700143 if (strlen(fs_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700144 ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700145 goto done;
146 }
147 if (strlen(partition_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700148 ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
Doug Zongker3d177d02010-07-01 09:18:44 -0700149 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700150 goto done;
151 }
152 if (strlen(location) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700153 ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700154 goto done;
155 }
156 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700157 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
158 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700159 goto done;
160 }
161
Tao Baoba9a42a2015-06-23 23:23:33 -0700162 {
163 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500164
Tao Baoba9a42a2015-06-23 23:23:33 -0700165 if (sehandle) {
166 selabel_lookup(sehandle, &secontext, mount_point, 0755);
167 setfscreatecon(secontext);
168 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500169
Tao Baoba9a42a2015-06-23 23:23:33 -0700170 mkdir(mount_point, 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700171
Tao Baoba9a42a2015-06-23 23:23:33 -0700172 if (secontext) {
173 freecon(secontext);
174 setfscreatecon(NULL);
175 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500176 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500177
Doug Zongker3d177d02010-07-01 09:18:44 -0700178 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700179 mtd_scan_partitions();
180 const MtdPartition* mtd;
181 mtd = mtd_find_partition_by_name(location);
182 if (mtd == NULL) {
Tao Bao1107d962015-09-09 17:16:55 -0700183 uiPrintf(state, "%s: no mtd partition named \"%s\"\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700184 name, location);
185 result = strdup("");
186 goto done;
187 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700188 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Michael Rungef15e31e2014-10-24 14:14:41 -0700189 uiPrintf(state, "mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700190 location, strerror(errno));
191 result = strdup("");
192 goto done;
193 }
194 result = mount_point;
195 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700196 if (mount(location, mount_point, fs_type,
Michael Rungebd6138c2014-10-22 17:05:08 -0700197 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
198 has_mount_options ? mount_options : "") < 0) {
Michael Rungef15e31e2014-10-24 14:14:41 -0700199 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700200 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700201 result = strdup("");
202 } else {
203 result = mount_point;
204 }
205 }
206
207done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700208 free(fs_type);
209 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700210 free(location);
211 if (result != mount_point) free(mount_point);
Michael Rungebd6138c2014-10-22 17:05:08 -0700212 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800213 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700214}
215
Doug Zongker8edb00c2009-06-11 17:21:44 -0700216
217// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800218Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700219 char* result = NULL;
220 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700221 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700222 }
223 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700224 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700225 return NULL;
226 }
227 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700228 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700229 goto done;
230 }
231
232 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700233 {
234 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
235 if (vol == NULL) {
236 result = strdup("");
237 } else {
238 result = mount_point;
239 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700240 }
241
242done:
243 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800244 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700245}
246
247
Doug Zongker512536a2010-02-17 16:11:44 -0800248Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700249 char* result = NULL;
250 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700251 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700252 }
253 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700254 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700255 return NULL;
256 }
257 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700258 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700259 goto done;
260 }
261
262 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700263 {
264 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
265 if (vol == NULL) {
266 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
267 result = strdup("");
268 } else {
269 int ret = unmount_mounted_volume(vol);
270 if (ret != 0) {
271 uiPrintf(state, "unmount of %s failed (%d): %s\n",
272 mount_point, ret, strerror(errno));
273 }
274 result = mount_point;
Michael Rungef15e31e2014-10-24 14:14:41 -0700275 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700276 }
277
278done:
279 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800280 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700281}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700282
JP Abgrall37aedb32014-06-16 19:07:39 -0700283static int exec_cmd(const char* path, char* const argv[]) {
284 int status;
285 pid_t child;
286 if ((child = vfork()) == 0) {
287 execv(path, argv);
288 _exit(-1);
289 }
290 waitpid(child, &status, 0);
291 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
292 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
293 }
294 return WEXITSTATUS(status);
295}
296
Doug Zongker8edb00c2009-06-11 17:21:44 -0700297
Stephen Smalley779701d2012-02-09 14:13:23 -0500298// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700299//
Stephen Smalley779701d2012-02-09 14:13:23 -0500300// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
301// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700302// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
303// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800304// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700305// if fs_size < 0, then reserve that many bytes at the end of the partition (not for "f2fs")
Doug Zongker512536a2010-02-17 16:11:44 -0800306Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700307 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400308 if (argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700309 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700310 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700311 char* fs_type;
312 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700313 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800314 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400315 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500316
Stephen Smalley779701d2012-02-09 14:13:23 -0500317 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
318 return NULL;
319 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700320
Doug Zongker3d177d02010-07-01 09:18:44 -0700321 if (strlen(fs_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700322 ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700323 goto done;
324 }
325 if (strlen(partition_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700326 ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
Doug Zongker3d177d02010-07-01 09:18:44 -0700327 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700328 goto done;
329 }
330 if (strlen(location) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700331 ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700332 goto done;
333 }
334
Stephen Smalley516e4e22012-04-03 13:35:11 -0400335 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700336 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
337 name);
Stephen Smalley779701d2012-02-09 14:13:23 -0500338 goto done;
339 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500340
Doug Zongker3d177d02010-07-01 09:18:44 -0700341 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700342 mtd_scan_partitions();
343 const MtdPartition* mtd = mtd_find_partition_by_name(location);
344 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700345 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700346 name, location);
347 result = strdup("");
348 goto done;
349 }
350 MtdWriteContext* ctx = mtd_write_partition(mtd);
351 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700352 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700353 result = strdup("");
354 goto done;
355 }
356 if (mtd_erase_blocks(ctx, -1) == -1) {
357 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700358 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700359 result = strdup("");
360 goto done;
361 }
362 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700363 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700364 result = strdup("");
365 goto done;
366 }
367 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700368#ifdef USE_EXT4
369 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500370 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700371 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700372 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700373 name, status, location);
374 result = strdup("");
375 goto done;
376 }
377 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700378 } else if (strcmp(fs_type, "f2fs") == 0) {
379 char *num_sectors;
380 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
381 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
382 result = strdup("");
383 goto done;
384 }
385 const char *f2fs_path = "/sbin/mkfs.f2fs";
386 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
387 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
388 free(num_sectors);
389 if (status != 0) {
390 printf("%s: mkfs.f2fs failed (%d) on %s",
391 name, status, location);
392 result = strdup("");
393 goto done;
394 }
395 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700396#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700397 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700398 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700399 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700400 }
401
402done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700403 free(fs_type);
404 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700405 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800406 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700407}
408
Michael Rungece7ca712013-11-06 17:42:20 -0800409Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
410 char* result = NULL;
411 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700412 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Michael Rungece7ca712013-11-06 17:42:20 -0800413 }
414
415 char* src_name;
416 char* dst_name;
417
418 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
419 return NULL;
420 }
421 if (strlen(src_name) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700422 ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800423 goto done;
424 }
425 if (strlen(dst_name) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700426 ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800427 goto done;
428 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700429 if (make_parents(dst_name) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700430 ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700431 dst_name, strerror(errno));
Michael Runged5b17272014-10-22 14:28:23 -0700432 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
433 // File was already moved
434 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700435 } else if (rename(src_name, dst_name) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700436 ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800437 src_name, dst_name, strerror(errno));
438 } else {
439 result = dst_name;
440 }
441
442done:
443 free(src_name);
444 if (result != dst_name) free(dst_name);
445 return StringValue(result);
446}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700447
Doug Zongker512536a2010-02-17 16:11:44 -0800448Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700449 char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
450 for (int i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700451 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700452 if (paths[i] == NULL) {
Yabin Cui64be2132016-02-03 18:16:02 -0800453 for (int j = 0; j < i; ++j) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700454 free(paths[j]);
455 }
456 free(paths);
457 return NULL;
458 }
459 }
460
461 bool recursive = (strcmp(name, "delete_recursive") == 0);
462
463 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700464 for (int i = 0; i < argc; ++i) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700465 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
466 ++success;
467 free(paths[i]);
468 }
469 free(paths);
470
471 char buffer[10];
472 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800473 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700474}
475
Doug Zongker8edb00c2009-06-11 17:21:44 -0700476
Doug Zongker512536a2010-02-17 16:11:44 -0800477Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700478 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700479 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700480 }
481 char* frac_str;
482 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700483 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700484 return NULL;
485 }
486
487 double frac = strtod(frac_str, NULL);
Tao Baob15fd222015-09-24 11:10:51 -0700488 int sec;
489 android::base::ParseInt(sec_str, &sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700490
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700491 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700492 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
493
Doug Zongker9931f7f2009-06-10 14:11:53 -0700494 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800495 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700496}
497
Doug Zongker512536a2010-02-17 16:11:44 -0800498Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700499 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700500 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700501 }
502 char* frac_str;
503 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
504 return NULL;
505 }
506
507 double frac = strtod(frac_str, NULL);
508
509 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
510 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
511
Doug Zongker512536a2010-02-17 16:11:44 -0800512 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700513}
514
Doug Zongker8edb00c2009-06-11 17:21:44 -0700515// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800516Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700517 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700518 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700519 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700520 }
521 char* zip_path;
522 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700523 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700524
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700525 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700526
527 // To create a consistent system image, never use the clock for timestamps.
528 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
529
530 bool success = mzExtractRecursive(za, zip_path, dest_path,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000531 &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500532 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700533 free(zip_path);
534 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800535 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700536}
537
Doug Zongker8edb00c2009-06-11 17:21:44 -0700538
539// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800540// or
541// package_extract_file(package_path)
542// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800543// function (the char* returned is actually a FileContents*).
544Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700545 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700546 if (argc < 1 || argc > 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700547 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800548 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700549 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700550 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700551
Doug Zongker6aece332010-02-01 14:40:12 -0800552 if (argc == 2) {
553 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700554
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800555 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700556
Doug Zongker6aece332010-02-01 14:40:12 -0800557 char* zip_path;
558 char* dest_path;
559 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
560
Doug Zongker6aece332010-02-01 14:40:12 -0800561 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
562 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700563 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800564 goto done2;
565 }
566
Tao Baoba9a42a2015-06-23 23:23:33 -0700567 {
Jed Estepf1fc48c2015-12-15 16:04:53 -0800568 int fd = TEMP_FAILURE_RETRY(ota_open(dest_path, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
Tao Baod3cac342015-12-14 15:53:25 -0800569 S_IRUSR | S_IWUSR));
570 if (fd == -1) {
571 printf("%s: can't open %s for write: %s\n", name, dest_path, strerror(errno));
Tao Baoba9a42a2015-06-23 23:23:33 -0700572 goto done2;
573 }
Tao Baod3cac342015-12-14 15:53:25 -0800574 success = mzExtractZipEntryToFile(za, entry, fd);
Jed Estepf1fc48c2015-12-15 16:04:53 -0800575 if (ota_fsync(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800576 printf("fsync of \"%s\" failed: %s\n", dest_path, strerror(errno));
577 success = false;
578 }
Jed Estepf1fc48c2015-12-15 16:04:53 -0800579 if (ota_close(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800580 printf("close of \"%s\" failed: %s\n", dest_path, strerror(errno));
581 success = false;
582 }
Doug Zongker6aece332010-02-01 14:40:12 -0800583 }
Doug Zongker6aece332010-02-01 14:40:12 -0800584
585 done2:
586 free(zip_path);
587 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800588 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800589 } else {
590 // The one-argument version returns the contents of the file
591 // as the result.
592
593 char* zip_path;
Yabin Cui64be2132016-02-03 18:16:02 -0800594 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
595
Tao Baoba9a42a2015-06-23 23:23:33 -0700596 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -0800597 v->type = VAL_BLOB;
598 v->size = -1;
599 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800600
Doug Zongker6aece332010-02-01 14:40:12 -0800601 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
602 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
603 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700604 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800605 goto done1;
606 }
607
Doug Zongker512536a2010-02-17 16:11:44 -0800608 v->size = mzGetZipEntryUncompLen(entry);
Tao Baoba9a42a2015-06-23 23:23:33 -0700609 v->data = reinterpret_cast<char*>(malloc(v->size));
Doug Zongker512536a2010-02-17 16:11:44 -0800610 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700611 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800612 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800613 goto done1;
614 }
615
Doug Zongker512536a2010-02-17 16:11:44 -0800616 success = mzExtractZipEntryToBuffer(za, entry,
617 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800618
619 done1:
620 free(zip_path);
621 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800622 free(v->data);
623 v->data = NULL;
624 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800625 }
Doug Zongker512536a2010-02-17 16:11:44 -0800626 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700627 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700628}
629
Doug Zongkera23075f2012-08-06 16:19:09 -0700630// Create all parent directories of name, if necessary.
631static int make_parents(char* name) {
632 char* p;
633 for (p = name + (strlen(name)-1); p > name; --p) {
634 if (*p != '/') continue;
635 *p = '\0';
636 if (make_parents(name) < 0) return -1;
637 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700638 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700639 *p = '/';
640 if (result == 0 || errno == EEXIST) {
641 // successfully created or already existed; we're done
642 return 0;
643 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700644 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700645 return -1;
646 }
647 }
648 return 0;
649}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700650
Doug Zongker9931f7f2009-06-10 14:11:53 -0700651// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700652// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800653Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700654 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700655 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700656 }
657 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700658 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700659 if (target == NULL) return NULL;
660
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700661 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700662 if (srcs == NULL) {
663 free(target);
664 return NULL;
665 }
666
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700667 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700668 int i;
669 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700670 if (unlink(srcs[i]) < 0) {
671 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700672 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700673 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700674 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700675 }
676 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700677 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700678 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700679 name, srcs[i], target);
680 ++bad;
681 }
Doug Zongker60babf82009-09-18 15:11:24 -0700682 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700683 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700684 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700685 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700686 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700687 free(srcs[i]);
688 }
689 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700690 if (bad) {
Tianjie Xu16255832016-04-30 11:49:59 -0700691 return ErrorAbort(state, kSymlinkFailure, "%s: some symlinks failed", name);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700692 }
Doug Zongker512536a2010-02-17 16:11:44 -0800693 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700694}
695
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700696struct perm_parsed_args {
697 bool has_uid;
698 uid_t uid;
699 bool has_gid;
700 gid_t gid;
701 bool has_mode;
702 mode_t mode;
703 bool has_fmode;
704 mode_t fmode;
705 bool has_dmode;
706 mode_t dmode;
707 bool has_selabel;
708 char* selabel;
709 bool has_capabilities;
710 uint64_t capabilities;
711};
712
Michael Runge75480252014-10-22 19:48:41 -0700713static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700714 int i;
715 struct perm_parsed_args parsed;
716 int bad = 0;
717 static int max_warnings = 20;
718
719 memset(&parsed, 0, sizeof(parsed));
720
721 for (i = 1; i < argc; i += 2) {
722 if (strcmp("uid", args[i]) == 0) {
723 int64_t uid;
724 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
725 parsed.uid = uid;
726 parsed.has_uid = true;
727 } else {
Michael Runge75480252014-10-22 19:48:41 -0700728 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700729 bad++;
730 }
731 continue;
732 }
733 if (strcmp("gid", args[i]) == 0) {
734 int64_t gid;
735 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
736 parsed.gid = gid;
737 parsed.has_gid = true;
738 } else {
Michael Runge75480252014-10-22 19:48:41 -0700739 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700740 bad++;
741 }
742 continue;
743 }
744 if (strcmp("mode", args[i]) == 0) {
745 int32_t mode;
746 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
747 parsed.mode = mode;
748 parsed.has_mode = true;
749 } else {
Michael Runge75480252014-10-22 19:48:41 -0700750 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700751 bad++;
752 }
753 continue;
754 }
755 if (strcmp("dmode", args[i]) == 0) {
756 int32_t mode;
757 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
758 parsed.dmode = mode;
759 parsed.has_dmode = true;
760 } else {
Michael Runge75480252014-10-22 19:48:41 -0700761 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700762 bad++;
763 }
764 continue;
765 }
766 if (strcmp("fmode", args[i]) == 0) {
767 int32_t mode;
768 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
769 parsed.fmode = mode;
770 parsed.has_fmode = true;
771 } else {
Michael Runge75480252014-10-22 19:48:41 -0700772 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700773 bad++;
774 }
775 continue;
776 }
777 if (strcmp("capabilities", args[i]) == 0) {
778 int64_t capabilities;
779 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
780 parsed.capabilities = capabilities;
781 parsed.has_capabilities = true;
782 } else {
Michael Runge75480252014-10-22 19:48:41 -0700783 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700784 bad++;
785 }
786 continue;
787 }
788 if (strcmp("selabel", args[i]) == 0) {
789 if (args[i+1][0] != '\0') {
790 parsed.selabel = args[i+1];
791 parsed.has_selabel = true;
792 } else {
Michael Runge75480252014-10-22 19:48:41 -0700793 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700794 bad++;
795 }
796 continue;
797 }
798 if (max_warnings != 0) {
799 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
800 max_warnings--;
801 if (max_warnings == 0) {
802 printf("ParsedPermArgs: suppressing further warnings\n");
803 }
804 }
805 }
806 return parsed;
807}
808
809static int ApplyParsedPerms(
Michael Runge75480252014-10-22 19:48:41 -0700810 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700811 const char* filename,
812 const struct stat *statptr,
813 struct perm_parsed_args parsed)
814{
815 int bad = 0;
816
Nick Kralevich6a821fe2014-10-23 20:36:42 -0700817 if (parsed.has_selabel) {
818 if (lsetfilecon(filename, parsed.selabel) != 0) {
819 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
820 filename, parsed.selabel, strerror(errno));
821 bad++;
822 }
823 }
824
Nick Kraleviche4612512013-09-10 15:34:19 -0700825 /* ignore symlinks */
826 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich6a821fe2014-10-23 20:36:42 -0700827 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700828 }
829
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700830 if (parsed.has_uid) {
831 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700832 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
833 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700834 bad++;
835 }
836 }
837
838 if (parsed.has_gid) {
839 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700840 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
841 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700842 bad++;
843 }
844 }
845
846 if (parsed.has_mode) {
847 if (chmod(filename, parsed.mode) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700848 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
849 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700850 bad++;
851 }
852 }
853
854 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
855 if (chmod(filename, parsed.dmode) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700856 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
857 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700858 bad++;
859 }
860 }
861
862 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
863 if (chmod(filename, parsed.fmode) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700864 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700865 filename, parsed.fmode, strerror(errno));
866 bad++;
867 }
868 }
869
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700870 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
871 if (parsed.capabilities == 0) {
872 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
873 // Report failure unless it's ENODATA (attribute not set)
Michael Runge75480252014-10-22 19:48:41 -0700874 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700875 filename, parsed.capabilities, strerror(errno));
876 bad++;
877 }
878 } else {
879 struct vfs_cap_data cap_data;
880 memset(&cap_data, 0, sizeof(cap_data));
881 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
882 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
883 cap_data.data[0].inheritable = 0;
884 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
885 cap_data.data[1].inheritable = 0;
886 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700887 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
888 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700889 bad++;
890 }
891 }
892 }
893
894 return bad;
895}
896
897// nftw doesn't allow us to pass along context, so we need to use
898// global variables. *sigh*
899static struct perm_parsed_args recursive_parsed_args;
Michael Runge75480252014-10-22 19:48:41 -0700900static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700901
902static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
903 int fileflags, struct FTW *pfwt) {
Michael Runge75480252014-10-22 19:48:41 -0700904 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700905}
906
907static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700908 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700909 struct stat sb;
910 Value* result = NULL;
911
912 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
913
914 if ((argc % 2) != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700915 return ErrorAbort(state, kArgsParsingFailure,
916 "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700917 }
918
919 char** args = ReadVarArgs(state, argc, argv);
920 if (args == NULL) return NULL;
921
922 if (lstat(args[0], &sb) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700923 result = ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s",
924 name, args[0], strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700925 goto done;
926 }
927
Tao Baoba9a42a2015-06-23 23:23:33 -0700928 {
929 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700930
Tao Baoba9a42a2015-06-23 23:23:33 -0700931 if (recursive) {
932 recursive_parsed_args = parsed;
933 recursive_state = state;
934 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
935 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
936 recursive_state = NULL;
937 } else {
938 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
939 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700940 }
941
942done:
Tao Baoba9a42a2015-06-23 23:23:33 -0700943 for (int i = 0; i < argc; ++i) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700944 free(args[i]);
945 }
946 free(args);
947
948 if (result != NULL) {
949 return result;
950 }
951
952 if (bad > 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700953 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700954 }
955
956 return StringValue(strdup(""));
957}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700958
Doug Zongker512536a2010-02-17 16:11:44 -0800959Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700960 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700961 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700962 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700963 char* key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700964 if (key == NULL) return NULL;
965
966 char value[PROPERTY_VALUE_MAX];
967 property_get(key, value, "");
968 free(key);
969
Doug Zongker512536a2010-02-17 16:11:44 -0800970 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700971}
972
973
Doug Zongker47cace92009-06-18 10:11:50 -0700974// file_getprop(file, key)
975//
976// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700977// per line. # comment lines,blank lines, lines without '=' ignored),
978// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800979Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700980 char* result = NULL;
981 char* buffer = NULL;
982 char* filename;
983 char* key;
984 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
985 return NULL;
986 }
987
988 struct stat st;
989 if (stat(filename, &st) < 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700990 ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name, filename,
991 strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700992 goto done;
993 }
994
995#define MAX_FILE_GETPROP_SIZE 65536
996
997 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tianjie Xu16255832016-04-30 11:49:59 -0700998 ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %d)", filename, name,
999 MAX_FILE_GETPROP_SIZE);
Doug Zongker47cace92009-06-18 10:11:50 -07001000 goto done;
1001 }
1002
Tao Baoba9a42a2015-06-23 23:23:33 -07001003 buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -07001004 if (buffer == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -07001005 ErrorAbort(state, kFileGetPropFailure, "%s: failed to alloc %lld bytes", name,
1006 (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -07001007 goto done;
1008 }
1009
Tao Baoba9a42a2015-06-23 23:23:33 -07001010 FILE* f;
1011 f = fopen(filename, "rb");
Doug Zongker47cace92009-06-18 10:11:50 -07001012 if (f == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -07001013 ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename,
1014 strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -07001015 goto done;
1016 }
1017
Jed Estepf1fc48c2015-12-15 16:04:53 -08001018 if (ota_fread(buffer, 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001019 ErrorAbort(state, kFreadFailure, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -07001020 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -07001021 fclose(f);
1022 goto done;
1023 }
1024 buffer[st.st_size] = '\0';
1025
1026 fclose(f);
1027
Tao Baoba9a42a2015-06-23 23:23:33 -07001028 char* line;
1029 line = strtok(buffer, "\n");
Doug Zongker47cace92009-06-18 10:11:50 -07001030 do {
1031 // skip whitespace at start of line
1032 while (*line && isspace(*line)) ++line;
1033
1034 // comment or blank line: skip to next line
1035 if (*line == '\0' || *line == '#') continue;
1036
1037 char* equal = strchr(line, '=');
1038 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001039 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001040 }
1041
1042 // trim whitespace between key and '='
1043 char* key_end = equal-1;
1044 while (key_end > line && isspace(*key_end)) --key_end;
1045 key_end[1] = '\0';
1046
1047 // not the key we're looking for
1048 if (strcmp(key, line) != 0) continue;
1049
1050 // skip whitespace after the '=' to the start of the value
1051 char* val_start = equal+1;
1052 while(*val_start && isspace(*val_start)) ++val_start;
1053
1054 // trim trailing whitespace
1055 char* val_end = val_start + strlen(val_start)-1;
1056 while (val_end > val_start && isspace(*val_end)) --val_end;
1057 val_end[1] = '\0';
1058
1059 result = strdup(val_start);
1060 break;
1061
1062 } while ((line = strtok(NULL, "\n")));
1063
1064 if (result == NULL) result = strdup("");
1065
1066 done:
1067 free(filename);
1068 free(key);
1069 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001070 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001071}
1072
Doug Zongker179b2d92011-04-12 15:49:04 -07001073// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001074Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001075 char* result = NULL;
1076
Doug Zongker179b2d92011-04-12 15:49:04 -07001077 Value* partition_value;
1078 Value* contents;
1079 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001080 return NULL;
1081 }
1082
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001083 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001084 if (partition_value->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001085 ErrorAbort(state, kArgsParsingFailure, "partition argument to %s must be string", name);
Doug Zongker179b2d92011-04-12 15:49:04 -07001086 goto done;
1087 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001088 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001089 if (strlen(partition) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001090 ErrorAbort(state, kArgsParsingFailure, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001091 goto done;
1092 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001093 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001094 ErrorAbort(state, kArgsParsingFailure, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001095 goto done;
1096 }
1097
1098 mtd_scan_partitions();
Tao Baoba9a42a2015-06-23 23:23:33 -07001099 const MtdPartition* mtd;
1100 mtd = mtd_find_partition_by_name(partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001101 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001102 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001103 result = strdup("");
1104 goto done;
1105 }
1106
Tao Baoba9a42a2015-06-23 23:23:33 -07001107 MtdWriteContext* ctx;
1108 ctx = mtd_write_partition(mtd);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001109 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001110 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001111 name, partition);
1112 result = strdup("");
1113 goto done;
1114 }
1115
1116 bool success;
1117
Doug Zongker179b2d92011-04-12 15:49:04 -07001118 if (contents->type == VAL_STRING) {
1119 // we're given a filename as the contents
1120 char* filename = contents->data;
Jed Estepf1fc48c2015-12-15 16:04:53 -08001121 FILE* f = ota_fopen(filename, "rb");
Doug Zongker179b2d92011-04-12 15:49:04 -07001122 if (f == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001123 printf("%s: can't open %s: %s\n", name, filename, strerror(errno));
Doug Zongker179b2d92011-04-12 15:49:04 -07001124 result = strdup("");
1125 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001126 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001127
1128 success = true;
Tao Baoba9a42a2015-06-23 23:23:33 -07001129 char* buffer = reinterpret_cast<char*>(malloc(BUFSIZ));
Doug Zongker179b2d92011-04-12 15:49:04 -07001130 int read;
Jed Estepf1fc48c2015-12-15 16:04:53 -08001131 while (success && (read = ota_fread(buffer, 1, BUFSIZ, f)) > 0) {
Doug Zongker179b2d92011-04-12 15:49:04 -07001132 int wrote = mtd_write_data(ctx, buffer, read);
1133 success = success && (wrote == read);
1134 }
1135 free(buffer);
Jed Estepf1fc48c2015-12-15 16:04:53 -08001136 ota_fclose(f);
Doug Zongker179b2d92011-04-12 15:49:04 -07001137 } else {
1138 // we're given a blob as the contents
1139 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1140 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001141 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001142 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001143 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001144 partition, strerror(errno));
1145 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001146
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001147 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001148 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001149 }
1150 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001151 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001152 }
1153
Doug Zongker179b2d92011-04-12 15:49:04 -07001154 printf("%s %s partition\n",
1155 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001156
1157 result = success ? partition : strdup("");
1158
1159done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001160 if (result != partition) FreeValue(partition_value);
1161 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001162 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001163}
1164
Doug Zongker8edb00c2009-06-11 17:21:44 -07001165// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001166Value* ApplyPatchSpaceFn(const char* name, State* state,
1167 int argc, Expr* argv[]) {
1168 char* bytes_str;
1169 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1170 return NULL;
1171 }
1172
Tao Baob15fd222015-09-24 11:10:51 -07001173 size_t bytes;
1174 if (!android::base::ParseUint(bytes_str, &bytes)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001175 ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n",
Doug Zongkerc4351c72010-02-22 14:46:32 -08001176 name, bytes_str);
1177 free(bytes_str);
Tao Baob15fd222015-09-24 11:10:51 -07001178 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001179 }
1180
1181 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1182}
1183
Doug Zongker52b40362014-02-10 15:30:30 -08001184// apply_patch(file, size, init_sha1, tgt_sha1, patch)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001185
Doug Zongker512536a2010-02-17 16:11:44 -08001186Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001187 if (argc < 6 || (argc % 2) == 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001188 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an "
1189 "even number, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001190 }
1191
Doug Zongkerc4351c72010-02-22 14:46:32 -08001192 char* source_filename;
1193 char* target_filename;
1194 char* target_sha1;
1195 char* target_size_str;
1196 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1197 &target_sha1, &target_size_str) < 0) {
1198 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001199 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001200
Tao Baob15fd222015-09-24 11:10:51 -07001201 size_t target_size;
1202 if (!android::base::ParseUint(target_size_str, &target_size)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001203 ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count",
Doug Zongkerc4351c72010-02-22 14:46:32 -08001204 name, target_size_str);
1205 free(source_filename);
1206 free(target_filename);
1207 free(target_sha1);
1208 free(target_size_str);
Tao Baob15fd222015-09-24 11:10:51 -07001209 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001210 }
1211
1212 int patchcount = (argc-4) / 2;
Yabin Cui64be2132016-02-03 18:16:02 -08001213 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc-4, argv+4),
1214 free);
1215 if (!arg_values) {
1216 return nullptr;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001217 }
Yabin Cui64be2132016-02-03 18:16:02 -08001218 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patch_shas;
1219 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patches;
1220 // Protect values by unique_ptrs first to get rid of memory leak.
1221 for (int i = 0; i < patchcount * 2; i += 2) {
1222 patch_shas.emplace_back(arg_values.get()[i], FreeValue);
1223 patches.emplace_back(arg_values.get()[i+1], FreeValue);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001224 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001225
Yabin Cui64be2132016-02-03 18:16:02 -08001226 for (int i = 0; i < patchcount; ++i) {
1227 if (patch_shas[i]->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001228 ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001229 return nullptr;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001230 }
Yabin Cui64be2132016-02-03 18:16:02 -08001231 if (patches[i]->type != VAL_BLOB) {
Tianjie Xu16255832016-04-30 11:49:59 -07001232 ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001233 return nullptr;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001234 }
1235 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001236
Yabin Cui64be2132016-02-03 18:16:02 -08001237 std::vector<char*> patch_sha_str;
1238 std::vector<Value*> patch_ptrs;
1239 for (int i = 0; i < patchcount; ++i) {
1240 patch_sha_str.push_back(patch_shas[i]->data);
1241 patch_ptrs.push_back(patches[i].get());
Doug Zongker8edb00c2009-06-11 17:21:44 -07001242 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001243
1244 int result = applypatch(source_filename, target_filename,
1245 target_sha1, target_size,
Yabin Cui64be2132016-02-03 18:16:02 -08001246 patchcount, patch_sha_str.data(), patch_ptrs.data(), NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001247
1248 return StringValue(strdup(result == 0 ? "t" : ""));
1249}
1250
1251// apply_patch_check(file, [sha1_1, ...])
1252Value* ApplyPatchCheckFn(const char* name, State* state,
1253 int argc, Expr* argv[]) {
1254 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001255 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %d",
Doug Zongkerc4351c72010-02-22 14:46:32 -08001256 name, argc);
1257 }
1258
1259 char* filename;
1260 if (ReadArgs(state, argv, 1, &filename) < 0) {
1261 return NULL;
1262 }
1263
1264 int patchcount = argc-1;
1265 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1266
1267 int result = applypatch_check(filename, patchcount, sha1s);
1268
1269 int i;
1270 for (i = 0; i < patchcount; ++i) {
1271 free(sha1s[i]);
1272 }
1273 free(sha1s);
1274
1275 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001276}
1277
Tao Bao1107d962015-09-09 17:16:55 -07001278// This is the updater side handler for ui_print() in edify script. Contents
1279// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001280Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001281 char** args = ReadVarArgs(state, argc, argv);
1282 if (args == NULL) {
1283 return NULL;
1284 }
1285
Tao Bao1107d962015-09-09 17:16:55 -07001286 std::string buffer;
1287 for (int i = 0; i < argc; ++i) {
1288 buffer += args[i];
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001289 free(args[i]);
1290 }
1291 free(args);
Tao Bao1107d962015-09-09 17:16:55 -07001292
1293 buffer += "\n";
Michael Runge75480252014-10-22 19:48:41 -07001294 uiPrint(state, buffer);
Tao Bao1107d962015-09-09 17:16:55 -07001295 return StringValue(strdup(buffer.c_str()));
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001296}
1297
Doug Zongkerd0181b82011-10-19 10:51:12 -07001298Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1299 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001300 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001301 }
1302 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1303 return StringValue(strdup("t"));
1304}
1305
Doug Zongker512536a2010-02-17 16:11:44 -08001306Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001307 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001308 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001309 }
1310 char** args = ReadVarArgs(state, argc, argv);
1311 if (args == NULL) {
1312 return NULL;
1313 }
1314
Tao Baoba9a42a2015-06-23 23:23:33 -07001315 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001316 memcpy(args2, args, sizeof(char*) * argc);
1317 args2[argc] = NULL;
1318
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001319 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001320
1321 pid_t child = fork();
1322 if (child == 0) {
1323 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001324 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001325 _exit(1);
1326 }
1327 int status;
1328 waitpid(child, &status, 0);
1329 if (WIFEXITED(status)) {
1330 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001331 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001332 WEXITSTATUS(status));
1333 }
1334 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001335 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001336 WTERMSIG(status));
1337 }
1338
1339 int i;
1340 for (i = 0; i < argc; ++i) {
1341 free(args[i]);
1342 }
1343 free(args);
1344 free(args2);
1345
1346 char buffer[20];
1347 sprintf(buffer, "%d", status);
1348
Doug Zongker512536a2010-02-17 16:11:44 -08001349 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001350}
1351
Doug Zongker512536a2010-02-17 16:11:44 -08001352// sha1_check(data)
1353// to return the sha1 of the data (given in the format returned by
1354// read_file).
1355//
1356// sha1_check(data, sha1_hex, [sha1_hex, ...])
1357// returns the sha1 of the file if it matches any of the hex
1358// strings passed, or "" if it does not equal any of them.
1359//
1360Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1361 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001362 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongker512536a2010-02-17 16:11:44 -08001363 }
1364
Yabin Cui64be2132016-02-03 18:16:02 -08001365 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc, argv), free);
1366 if (arg_values == nullptr) {
1367 return nullptr;
1368 }
1369 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> args;
1370 for (int i = 0; i < argc; ++i) {
1371 args.emplace_back(arg_values.get()[i], FreeValue);
Doug Zongker512536a2010-02-17 16:11:44 -08001372 }
1373
1374 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001375 return StringValue(strdup(""));
1376 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001377 uint8_t digest[SHA_DIGEST_LENGTH];
1378 SHA1(reinterpret_cast<uint8_t*>(args[0]->data), args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001379
1380 if (argc == 1) {
1381 return StringValue(PrintSha1(digest));
1382 }
1383
1384 int i;
Yabin Cui64be2132016-02-03 18:16:02 -08001385 uint8_t arg_digest[SHA_DIGEST_LENGTH];
Doug Zongker512536a2010-02-17 16:11:44 -08001386 for (i = 1; i < argc; ++i) {
1387 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001388 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001389 name, i);
1390 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1391 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001392 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1393 name, args[i]->data);
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001394 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001395 break;
1396 }
Doug Zongker512536a2010-02-17 16:11:44 -08001397 }
1398 if (i >= argc) {
1399 // Didn't match any of the hex strings; return false.
1400 return StringValue(strdup(""));
1401 }
Yabin Cui64be2132016-02-03 18:16:02 -08001402 // Found a match.
1403 return args[i].release();
Doug Zongker512536a2010-02-17 16:11:44 -08001404}
1405
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001406// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001407// is actually a FileContents*).
1408Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1409 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001410 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker512536a2010-02-17 16:11:44 -08001411 }
1412 char* filename;
1413 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1414
Yabin Cui1c522df2016-02-10 16:41:10 -08001415 Value* v = static_cast<Value*>(malloc(sizeof(Value)));
1416 if (v == nullptr) {
1417 return nullptr;
1418 }
Doug Zongker512536a2010-02-17 16:11:44 -08001419 v->type = VAL_BLOB;
Yabin Cui1c522df2016-02-10 16:41:10 -08001420 v->size = -1;
1421 v->data = nullptr;
Doug Zongker512536a2010-02-17 16:11:44 -08001422
1423 FileContents fc;
Tao Baoccb0ba92016-06-11 03:56:38 -07001424 if (LoadFileContents(filename, &fc) == 0) {
Yabin Cui1c522df2016-02-10 16:41:10 -08001425 v->data = static_cast<char*>(malloc(fc.data.size()));
1426 if (v->data != nullptr) {
1427 memcpy(v->data, fc.data.data(), fc.data.size());
1428 v->size = fc.data.size();
1429 }
Doug Zongker512536a2010-02-17 16:11:44 -08001430 }
Doug Zongker512536a2010-02-17 16:11:44 -08001431 free(filename);
1432 return v;
1433}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001434
Doug Zongkerc87bab12013-11-25 13:53:25 -08001435// Immediately reboot the device. Recovery is not finished normally,
1436// so if you reboot into recovery it will re-start applying the
1437// current package (because nothing has cleared the copy of the
1438// arguments stored in the BCB).
1439//
1440// The argument is the partition name passed to the android reboot
1441// property. It can be "recovery" to boot from the recovery
1442// partition, or "" (empty string) to boot from the regular boot
1443// partition.
1444Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1445 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001446 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001447 }
1448
1449 char* filename;
1450 char* property;
1451 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1452
1453 char buffer[80];
1454
1455 // zero out the 'command' field of the bootloader message.
1456 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1457 FILE* f = fopen(filename, "r+b");
1458 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
Jed Estepf1fc48c2015-12-15 16:04:53 -08001459 ota_fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001460 fclose(f);
1461 free(filename);
1462
1463 strcpy(buffer, "reboot,");
1464 if (property != NULL) {
1465 strncat(buffer, property, sizeof(buffer)-10);
1466 }
1467
1468 property_set(ANDROID_RB_PROPERTY, buffer);
1469
1470 sleep(5);
1471 free(property);
Tianjie Xu16255832016-04-30 11:49:59 -07001472 ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001473 return NULL;
1474}
1475
1476// Store a string value somewhere that future invocations of recovery
1477// can access it. This value is called the "stage" and can be used to
1478// drive packages that need to do reboots in the middle of
1479// installation and keep track of where they are in the multi-stage
1480// install.
1481//
1482// The first argument is the block device for the misc partition
1483// ("/misc" in the fstab), which is where this value is stored. The
1484// second argument is the string to store; it should not exceed 31
1485// bytes.
1486Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1487 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001488 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001489 }
1490
1491 char* filename;
1492 char* stagestr;
1493 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1494
1495 // Store this value in the misc partition, immediately after the
1496 // bootloader message that the main recovery uses to save its
1497 // arguments in case of the device restarting midway through
1498 // package installation.
1499 FILE* f = fopen(filename, "r+b");
1500 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1501 int to_write = strlen(stagestr)+1;
1502 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1503 if (to_write > max_size) {
1504 to_write = max_size;
1505 stagestr[max_size-1] = 0;
1506 }
Jed Estepf1fc48c2015-12-15 16:04:53 -08001507 ota_fwrite(stagestr, to_write, 1, f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001508 fclose(f);
1509
1510 free(stagestr);
1511 return StringValue(filename);
1512}
1513
1514// Return the value most recently saved with SetStageFn. The argument
1515// is the block device for the misc partition.
1516Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001517 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001518 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001519 }
1520
1521 char* filename;
1522 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1523
1524 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1525 FILE* f = fopen(filename, "rb");
1526 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Jed Estepf1fc48c2015-12-15 16:04:53 -08001527 ota_fread(buffer, sizeof(buffer), 1, f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001528 fclose(f);
1529 buffer[sizeof(buffer)-1] = '\0';
1530
1531 return StringValue(strdup(buffer));
1532}
1533
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001534Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1535 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001536 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001537 }
1538
1539 char* filename;
1540 char* len_str;
1541 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1542
Tao Baob15fd222015-09-24 11:10:51 -07001543 size_t len;
1544 android::base::ParseUint(len_str, &len);
Jed Estepf1fc48c2015-12-15 16:04:53 -08001545 int fd = ota_open(filename, O_WRONLY, 0644);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001546 int success = wipe_block_device(fd, len);
1547
1548 free(filename);
1549 free(len_str);
1550
Jed Estepf1fc48c2015-12-15 16:04:53 -08001551 ota_close(fd);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001552
1553 return StringValue(strdup(success ? "t" : ""));
1554}
1555
Doug Zongkerc704e062014-05-23 08:40:35 -07001556Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1557 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001558 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerc704e062014-05-23 08:40:35 -07001559 }
1560 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1561 fprintf(ui->cmd_pipe, "enable_reboot\n");
1562 return StringValue(strdup("t"));
1563}
1564
Michael Rungeb278c252014-11-21 00:12:28 -08001565Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001566#ifdef HAVE_LIBTUNE2FS
Michael Rungeb278c252014-11-21 00:12:28 -08001567 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001568 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %d", name, argc);
Michael Rungeb278c252014-11-21 00:12:28 -08001569 }
1570
1571 char** args = ReadVarArgs(state, argc, argv);
1572 if (args == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -07001573 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
Michael Rungeb278c252014-11-21 00:12:28 -08001574 }
1575
Tao Baoba9a42a2015-06-23 23:23:33 -07001576 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Michael Rungeb278c252014-11-21 00:12:28 -08001577 // Tune2fs expects the program name as its args[0]
1578 args2[0] = strdup(name);
Tao Baoba9a42a2015-06-23 23:23:33 -07001579 for (int i = 0; i < argc; ++i) {
Michael Rungeb278c252014-11-21 00:12:28 -08001580 args2[i + 1] = args[i];
1581 }
1582 int result = tune2fs_main(argc + 1, args2);
Tao Baoba9a42a2015-06-23 23:23:33 -07001583 for (int i = 0; i < argc; ++i) {
Michael Rungeb278c252014-11-21 00:12:28 -08001584 free(args[i]);
1585 }
1586 free(args);
1587
1588 free(args2[0]);
1589 free(args2);
1590 if (result != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001591 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d",
1592 name, result);
Michael Rungeb278c252014-11-21 00:12:28 -08001593 }
1594 return StringValue(strdup("t"));
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001595#else
1596 return ErrorAbort(state, "%s() support not present, no libtune2fs", name);
1597#endif // HAVE_LIBTUNE2FS
Michael Rungeb278c252014-11-21 00:12:28 -08001598}
1599
Doug Zongker9931f7f2009-06-10 14:11:53 -07001600void RegisterInstallFunctions() {
1601 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001602 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001603 RegisterFunction("unmount", UnmountFn);
1604 RegisterFunction("format", FormatFn);
1605 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001606 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001607 RegisterFunction("delete", DeleteFn);
1608 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001609 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1610 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001611 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001612
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001613 // Usage:
1614 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1615 // Example:
1616 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1617 RegisterFunction("set_metadata", SetMetadataFn);
1618
1619 // Usage:
1620 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1621 // Example:
1622 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1623 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1624
Doug Zongker8edb00c2009-06-11 17:21:44 -07001625 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001626 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001627 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001628
1629 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001630 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1631 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001632
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001633 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001634
Doug Zongker512536a2010-02-17 16:11:44 -08001635 RegisterFunction("read_file", ReadFileFn);
1636 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001637 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001638
Doug Zongkerd0181b82011-10-19 10:51:12 -07001639 RegisterFunction("wipe_cache", WipeCacheFn);
1640
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001641 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001642
1643 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001644
1645 RegisterFunction("reboot_now", RebootNowFn);
1646 RegisterFunction("get_stage", GetStageFn);
1647 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001648
1649 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeb278c252014-11-21 00:12:28 -08001650 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001651}