blob: 74feb56be1d55553dd481b7ffa2ca6adf03fc3b5 [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>
Tao Bao1bf17722016-11-03 23:52:01 -070038#include <string>
Yabin Cui64be2132016-02-03 18:16:02 -080039#include <vector>
40
Tao Bao1bf17722016-11-03 23:52:01 -070041#include <android-base/file.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080042#include <android-base/parseint.h>
43#include <android-base/strings.h>
44#include <android-base/stringprintf.h>
Tao Bao1107d962015-09-09 17:16:55 -070045
Doug Zongkerc87bab12013-11-25 13:53:25 -080046#include "bootloader.h"
47#include "applypatch/applypatch.h"
48#include "cutils/android_reboot.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070049#include "cutils/misc.h"
50#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070051#include "edify/expr.h"
Tianjie Xu16255832016-04-30 11:49:59 -070052#include "error_code.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070053#include "minzip/DirUtil.h"
54#include "mtdutils/mounts.h"
55#include "mtdutils/mtdutils.h"
Tianjie Xu16255832016-04-30 11:49:59 -070056#include "openssl/sha.h"
Jed Estepff6df892015-12-15 16:04:53 -080057#include "ota_io.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070058#include "updater.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080059#include "install.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080060#include "tune2fs.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070061
Doug Zongker3d177d02010-07-01 09:18:44 -070062#ifdef USE_EXT4
63#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080064#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070065#endif
66
Tao Bao1107d962015-09-09 17:16:55 -070067// Send over the buffer to recovery though the command pipe.
68static void uiPrint(State* state, const std::string& buffer) {
69 UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070070
Tao Bao1107d962015-09-09 17:16:55 -070071 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
72 // So skip sending empty strings to UI.
73 std::vector<std::string> lines = android::base::Split(buffer, "\n");
74 for (auto& line: lines) {
75 if (!line.empty()) {
76 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
77 fprintf(ui->cmd_pipe, "ui_print\n");
78 }
79 }
80
81 // On the updater side, we need to dump the contents to stderr (which has
82 // been redirected to the log file). Because the recovery will only print
83 // the contents to screen when processing pipe command ui_print.
84 fprintf(stderr, "%s", buffer.c_str());
Michael Runged4a63422014-10-22 19:48:41 -070085}
86
87__attribute__((__format__(printf, 2, 3))) __nonnull((2))
88void uiPrintf(State* state, const char* format, ...) {
Tao Bao1107d962015-09-09 17:16:55 -070089 std::string error_msg;
90
Michael Runged4a63422014-10-22 19:48:41 -070091 va_list ap;
92 va_start(ap, format);
Tao Bao1107d962015-09-09 17:16:55 -070093 android::base::StringAppendV(&error_msg, format, ap);
Michael Runged4a63422014-10-22 19:48:41 -070094 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070095
Michael Runged4a63422014-10-22 19:48:41 -070096 uiPrint(state, error_msg);
97}
98
Doug Zongker52b40362014-02-10 15:30:30 -080099// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700100char* PrintSha1(const uint8_t* digest) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800101 char* buffer = reinterpret_cast<char*>(malloc(SHA_DIGEST_LENGTH*2 + 1));
Doug Zongker52b40362014-02-10 15:30:30 -0800102 const char* alphabet = "0123456789abcdef";
Tao Baoba9a42a2015-06-23 23:23:33 -0700103 size_t i;
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800104 for (i = 0; i < SHA_DIGEST_LENGTH; ++i) {
Doug Zongker52b40362014-02-10 15:30:30 -0800105 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
106 buffer[i*2+1] = alphabet[digest[i] & 0xf];
107 }
108 buffer[i*2] = '\0';
109 return buffer;
110}
111
Doug Zongker3d177d02010-07-01 09:18:44 -0700112// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700113//
Doug Zongker3d177d02010-07-01 09:18:44 -0700114// fs_type="yaffs2" partition_type="MTD" location=partition
115// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800116Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700117 char* result = NULL;
Michael Runge168f7772014-10-22 17:05:08 -0700118 if (argc != 4 && argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700119 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700120 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700121 char* fs_type;
122 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700123 char* location;
124 char* mount_point;
Michael Runge168f7772014-10-22 17:05:08 -0700125 char* mount_options;
126 bool has_mount_options;
127 if (argc == 5) {
128 has_mount_options = true;
129 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
130 &location, &mount_point, &mount_options) < 0) {
131 return NULL;
132 }
133 } else {
134 has_mount_options = false;
135 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700136 &location, &mount_point) < 0) {
Michael Runge168f7772014-10-22 17:05:08 -0700137 return NULL;
138 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700139 }
140
Doug Zongker3d177d02010-07-01 09:18:44 -0700141 if (strlen(fs_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700142 ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700143 goto done;
144 }
145 if (strlen(partition_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700146 ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
Doug Zongker3d177d02010-07-01 09:18:44 -0700147 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700148 goto done;
149 }
150 if (strlen(location) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700151 ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700152 goto done;
153 }
154 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700155 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
156 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700157 goto done;
158 }
159
Tao Baoba9a42a2015-06-23 23:23:33 -0700160 {
161 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500162
Tao Baoba9a42a2015-06-23 23:23:33 -0700163 if (sehandle) {
164 selabel_lookup(sehandle, &secontext, mount_point, 0755);
165 setfscreatecon(secontext);
166 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500167
Tao Baoba9a42a2015-06-23 23:23:33 -0700168 mkdir(mount_point, 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700169
Tao Baoba9a42a2015-06-23 23:23:33 -0700170 if (secontext) {
171 freecon(secontext);
172 setfscreatecon(NULL);
173 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500174 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500175
Doug Zongker3d177d02010-07-01 09:18:44 -0700176 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700177 mtd_scan_partitions();
178 const MtdPartition* mtd;
179 mtd = mtd_find_partition_by_name(location);
180 if (mtd == NULL) {
Tao Bao1107d962015-09-09 17:16:55 -0700181 uiPrintf(state, "%s: no mtd partition named \"%s\"\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700182 name, location);
183 result = strdup("");
184 goto done;
185 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700186 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700187 uiPrintf(state, "mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700188 location, strerror(errno));
189 result = strdup("");
190 goto done;
191 }
192 result = mount_point;
193 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700194 if (mount(location, mount_point, fs_type,
Michael Runge168f7772014-10-22 17:05:08 -0700195 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
196 has_mount_options ? mount_options : "") < 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700197 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700198 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700199 result = strdup("");
200 } else {
201 result = mount_point;
202 }
203 }
204
205done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700206 free(fs_type);
207 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700208 free(location);
209 if (result != mount_point) free(mount_point);
Michael Runge168f7772014-10-22 17:05:08 -0700210 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800211 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700212}
213
Doug Zongker8edb00c2009-06-11 17:21:44 -0700214
215// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800216Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700217 char* result = NULL;
218 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700219 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700220 }
221 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700222 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700223 return NULL;
224 }
225 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700226 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700227 goto done;
228 }
229
230 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700231 {
232 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
233 if (vol == NULL) {
234 result = strdup("");
235 } else {
236 result = mount_point;
237 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700238 }
239
240done:
241 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800242 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700243}
244
245
Doug Zongker512536a2010-02-17 16:11:44 -0800246Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700247 char* result = NULL;
248 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700249 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700250 }
251 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700252 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700253 return NULL;
254 }
255 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700256 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700257 goto done;
258 }
259
260 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700261 {
262 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
263 if (vol == NULL) {
264 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
265 result = strdup("");
266 } else {
267 int ret = unmount_mounted_volume(vol);
268 if (ret != 0) {
269 uiPrintf(state, "unmount of %s failed (%d): %s\n",
270 mount_point, ret, strerror(errno));
271 }
272 result = mount_point;
Michael Runge5ddf4292014-10-24 14:14:41 -0700273 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700274 }
275
276done:
277 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800278 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700279}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700280
JP Abgrall37aedb32014-06-16 19:07:39 -0700281static int exec_cmd(const char* path, char* const argv[]) {
282 int status;
283 pid_t child;
284 if ((child = vfork()) == 0) {
285 execv(path, argv);
286 _exit(-1);
287 }
288 waitpid(child, &status, 0);
289 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
290 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
291 }
292 return WEXITSTATUS(status);
293}
294
Doug Zongker8edb00c2009-06-11 17:21:44 -0700295
Stephen Smalley779701d2012-02-09 14:13:23 -0500296// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700297//
Stephen Smalley779701d2012-02-09 14:13:23 -0500298// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
299// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700300// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
301// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800302// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700303// 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 -0800304Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700305 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400306 if (argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700307 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700308 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700309 char* fs_type;
310 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700311 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800312 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400313 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500314
Stephen Smalley779701d2012-02-09 14:13:23 -0500315 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
316 return NULL;
317 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700318
Doug Zongker3d177d02010-07-01 09:18:44 -0700319 if (strlen(fs_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700320 ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700321 goto done;
322 }
323 if (strlen(partition_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700324 ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
Doug Zongker3d177d02010-07-01 09:18:44 -0700325 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700326 goto done;
327 }
328 if (strlen(location) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700329 ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700330 goto done;
331 }
332
Stephen Smalley516e4e22012-04-03 13:35:11 -0400333 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700334 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
335 name);
Stephen Smalley779701d2012-02-09 14:13:23 -0500336 goto done;
337 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500338
Doug Zongker3d177d02010-07-01 09:18:44 -0700339 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700340 mtd_scan_partitions();
341 const MtdPartition* mtd = mtd_find_partition_by_name(location);
342 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700343 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700344 name, location);
345 result = strdup("");
346 goto done;
347 }
348 MtdWriteContext* ctx = mtd_write_partition(mtd);
349 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700350 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700351 result = strdup("");
352 goto done;
353 }
354 if (mtd_erase_blocks(ctx, -1) == -1) {
355 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700356 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700357 result = strdup("");
358 goto done;
359 }
360 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700361 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700362 result = strdup("");
363 goto done;
364 }
365 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700366#ifdef USE_EXT4
367 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500368 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700369 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700370 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700371 name, status, location);
372 result = strdup("");
373 goto done;
374 }
375 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700376 } else if (strcmp(fs_type, "f2fs") == 0) {
377 char *num_sectors;
378 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
379 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
380 result = strdup("");
381 goto done;
382 }
383 const char *f2fs_path = "/sbin/mkfs.f2fs";
384 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
385 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
386 free(num_sectors);
387 if (status != 0) {
388 printf("%s: mkfs.f2fs failed (%d) on %s",
389 name, status, location);
390 result = strdup("");
391 goto done;
392 }
393 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700394#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700395 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700396 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700397 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700398 }
399
400done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700401 free(fs_type);
402 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700403 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800404 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700405}
406
Michael Rungece7ca712013-11-06 17:42:20 -0800407Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
408 char* result = NULL;
409 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700410 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Michael Rungece7ca712013-11-06 17:42:20 -0800411 }
412
413 char* src_name;
414 char* dst_name;
415
416 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
417 return NULL;
418 }
419 if (strlen(src_name) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700420 ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800421 goto done;
422 }
423 if (strlen(dst_name) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700424 ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800425 goto done;
426 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700427 if (make_parents(dst_name) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700428 ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700429 dst_name, strerror(errno));
Michael Runge2f0ef732014-10-22 14:28:23 -0700430 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
431 // File was already moved
432 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700433 } else if (rename(src_name, dst_name) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700434 ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800435 src_name, dst_name, strerror(errno));
436 } else {
437 result = dst_name;
438 }
439
440done:
441 free(src_name);
442 if (result != dst_name) free(dst_name);
443 return StringValue(result);
444}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700445
Doug Zongker512536a2010-02-17 16:11:44 -0800446Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700447 char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
448 for (int i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700449 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700450 if (paths[i] == NULL) {
Yabin Cui64be2132016-02-03 18:16:02 -0800451 for (int j = 0; j < i; ++j) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700452 free(paths[j]);
453 }
454 free(paths);
455 return NULL;
456 }
457 }
458
459 bool recursive = (strcmp(name, "delete_recursive") == 0);
460
461 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700462 for (int i = 0; i < argc; ++i) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700463 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
464 ++success;
465 free(paths[i]);
466 }
467 free(paths);
468
469 char buffer[10];
470 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800471 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700472}
473
Doug Zongker8edb00c2009-06-11 17:21:44 -0700474
Doug Zongker512536a2010-02-17 16:11:44 -0800475Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700476 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700477 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700478 }
479 char* frac_str;
480 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700481 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700482 return NULL;
483 }
484
485 double frac = strtod(frac_str, NULL);
Tao Baob15fd222015-09-24 11:10:51 -0700486 int sec;
487 android::base::ParseInt(sec_str, &sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700488
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700489 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700490 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
491
Doug Zongker9931f7f2009-06-10 14:11:53 -0700492 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800493 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700494}
495
Doug Zongker512536a2010-02-17 16:11:44 -0800496Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700497 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700498 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700499 }
500 char* frac_str;
501 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
502 return NULL;
503 }
504
505 double frac = strtod(frac_str, NULL);
506
507 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
508 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
509
Doug Zongker512536a2010-02-17 16:11:44 -0800510 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700511}
512
Doug Zongker8edb00c2009-06-11 17:21:44 -0700513// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800514Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700515 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700516 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700517 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700518 }
519 char* zip_path;
520 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700521 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700522
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700523 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700524
525 // To create a consistent system image, never use the clock for timestamps.
526 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
527
528 bool success = mzExtractRecursive(za, zip_path, dest_path,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000529 &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500530 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700531 free(zip_path);
532 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800533 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700534}
535
Doug Zongker8edb00c2009-06-11 17:21:44 -0700536
537// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800538// or
539// package_extract_file(package_path)
540// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800541// function (the char* returned is actually a FileContents*).
542Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700543 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700544 if (argc < 1 || argc > 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700545 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800546 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700547 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700548 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700549
Doug Zongker5f875bf2014-08-22 14:53:43 -0700550 if (argc == 2) {
551 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800552
553 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700554
Doug Zongker6aece332010-02-01 14:40:12 -0800555 char* zip_path;
556 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700557 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800558
Doug Zongker6aece332010-02-01 14:40:12 -0800559 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
560 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700561 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800562 goto done2;
563 }
564
Tao Baoba9a42a2015-06-23 23:23:33 -0700565 {
Jed Estepf1fc48c2015-12-15 16:04:53 -0800566 int fd = TEMP_FAILURE_RETRY(ota_open(dest_path, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
Tao Baod3cac342015-12-14 15:53:25 -0800567 S_IRUSR | S_IWUSR));
568 if (fd == -1) {
569 printf("%s: can't open %s for write: %s\n", name, dest_path, strerror(errno));
Tao Baoba9a42a2015-06-23 23:23:33 -0700570 goto done2;
571 }
Tao Baod3cac342015-12-14 15:53:25 -0800572 success = mzExtractZipEntryToFile(za, entry, fd);
Jed Estepf1fc48c2015-12-15 16:04:53 -0800573 if (ota_fsync(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800574 printf("fsync of \"%s\" failed: %s\n", dest_path, strerror(errno));
575 success = false;
576 }
Jed Estepf1fc48c2015-12-15 16:04:53 -0800577 if (ota_close(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800578 printf("close of \"%s\" failed: %s\n", dest_path, strerror(errno));
579 success = false;
580 }
Doug Zongker6aece332010-02-01 14:40:12 -0800581 }
Doug Zongker6aece332010-02-01 14:40:12 -0800582
583 done2:
584 free(zip_path);
585 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800586 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800587 } else {
588 // The one-argument version returns the contents of the file
589 // as the result.
590
591 char* zip_path;
Yabin Cui64be2132016-02-03 18:16:02 -0800592 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
593
Tao Baoba9a42a2015-06-23 23:23:33 -0700594 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -0800595 v->type = VAL_BLOB;
596 v->size = -1;
597 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800598
Doug Zongker6aece332010-02-01 14:40:12 -0800599 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
600 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
601 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700602 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800603 goto done1;
604 }
605
Doug Zongker512536a2010-02-17 16:11:44 -0800606 v->size = mzGetZipEntryUncompLen(entry);
Tao Baoba9a42a2015-06-23 23:23:33 -0700607 v->data = reinterpret_cast<char*>(malloc(v->size));
Doug Zongker512536a2010-02-17 16:11:44 -0800608 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700609 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800610 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800611 goto done1;
612 }
613
Doug Zongker512536a2010-02-17 16:11:44 -0800614 success = mzExtractZipEntryToBuffer(za, entry,
615 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800616
617 done1:
618 free(zip_path);
619 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800620 free(v->data);
621 v->data = NULL;
622 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800623 }
Doug Zongker512536a2010-02-17 16:11:44 -0800624 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700625 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700626}
627
Doug Zongkera23075f2012-08-06 16:19:09 -0700628// Create all parent directories of name, if necessary.
629static int make_parents(char* name) {
630 char* p;
631 for (p = name + (strlen(name)-1); p > name; --p) {
632 if (*p != '/') continue;
633 *p = '\0';
634 if (make_parents(name) < 0) return -1;
635 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700636 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700637 *p = '/';
638 if (result == 0 || errno == EEXIST) {
639 // successfully created or already existed; we're done
640 return 0;
641 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700642 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700643 return -1;
644 }
645 }
646 return 0;
647}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700648
Doug Zongker9931f7f2009-06-10 14:11:53 -0700649// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700650// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800651Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700652 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700653 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700654 }
655 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700656 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700657 if (target == NULL) return NULL;
658
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700659 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700660 if (srcs == NULL) {
661 free(target);
662 return NULL;
663 }
664
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700665 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700666 int i;
667 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700668 if (unlink(srcs[i]) < 0) {
669 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700670 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700671 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700672 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700673 }
674 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700675 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700676 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700677 name, srcs[i], target);
678 ++bad;
679 }
Doug Zongker60babf82009-09-18 15:11:24 -0700680 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700681 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700682 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700683 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700684 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700685 free(srcs[i]);
686 }
687 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700688 if (bad) {
Tianjie Xu16255832016-04-30 11:49:59 -0700689 return ErrorAbort(state, kSymlinkFailure, "%s: some symlinks failed", name);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700690 }
Doug Zongker512536a2010-02-17 16:11:44 -0800691 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700692}
693
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700694struct perm_parsed_args {
695 bool has_uid;
696 uid_t uid;
697 bool has_gid;
698 gid_t gid;
699 bool has_mode;
700 mode_t mode;
701 bool has_fmode;
702 mode_t fmode;
703 bool has_dmode;
704 mode_t dmode;
705 bool has_selabel;
706 char* selabel;
707 bool has_capabilities;
708 uint64_t capabilities;
709};
710
Michael Runged4a63422014-10-22 19:48:41 -0700711static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700712 int i;
713 struct perm_parsed_args parsed;
714 int bad = 0;
715 static int max_warnings = 20;
716
717 memset(&parsed, 0, sizeof(parsed));
718
719 for (i = 1; i < argc; i += 2) {
720 if (strcmp("uid", args[i]) == 0) {
721 int64_t uid;
722 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
723 parsed.uid = uid;
724 parsed.has_uid = true;
725 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700726 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700727 bad++;
728 }
729 continue;
730 }
731 if (strcmp("gid", args[i]) == 0) {
732 int64_t gid;
733 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
734 parsed.gid = gid;
735 parsed.has_gid = true;
736 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700737 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700738 bad++;
739 }
740 continue;
741 }
742 if (strcmp("mode", args[i]) == 0) {
743 int32_t mode;
744 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
745 parsed.mode = mode;
746 parsed.has_mode = true;
747 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700748 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700749 bad++;
750 }
751 continue;
752 }
753 if (strcmp("dmode", args[i]) == 0) {
754 int32_t mode;
755 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
756 parsed.dmode = mode;
757 parsed.has_dmode = true;
758 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700759 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700760 bad++;
761 }
762 continue;
763 }
764 if (strcmp("fmode", args[i]) == 0) {
765 int32_t mode;
766 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
767 parsed.fmode = mode;
768 parsed.has_fmode = true;
769 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700770 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700771 bad++;
772 }
773 continue;
774 }
775 if (strcmp("capabilities", args[i]) == 0) {
776 int64_t capabilities;
777 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
778 parsed.capabilities = capabilities;
779 parsed.has_capabilities = true;
780 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700781 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700782 bad++;
783 }
784 continue;
785 }
786 if (strcmp("selabel", args[i]) == 0) {
787 if (args[i+1][0] != '\0') {
788 parsed.selabel = args[i+1];
789 parsed.has_selabel = true;
790 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700791 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700792 bad++;
793 }
794 continue;
795 }
796 if (max_warnings != 0) {
797 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
798 max_warnings--;
799 if (max_warnings == 0) {
800 printf("ParsedPermArgs: suppressing further warnings\n");
801 }
802 }
803 }
804 return parsed;
805}
806
807static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700808 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700809 const char* filename,
810 const struct stat *statptr,
811 struct perm_parsed_args parsed)
812{
813 int bad = 0;
814
Nick Kralevich68802412014-10-23 20:36:42 -0700815 if (parsed.has_selabel) {
816 if (lsetfilecon(filename, parsed.selabel) != 0) {
817 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
818 filename, parsed.selabel, strerror(errno));
819 bad++;
820 }
821 }
822
Nick Kraleviche4612512013-09-10 15:34:19 -0700823 /* ignore symlinks */
824 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700825 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700826 }
827
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700828 if (parsed.has_uid) {
829 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700830 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
831 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700832 bad++;
833 }
834 }
835
836 if (parsed.has_gid) {
837 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700838 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
839 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700840 bad++;
841 }
842 }
843
844 if (parsed.has_mode) {
845 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700846 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
847 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700848 bad++;
849 }
850 }
851
852 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
853 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700854 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
855 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700856 bad++;
857 }
858 }
859
860 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
861 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700862 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700863 filename, parsed.fmode, strerror(errno));
864 bad++;
865 }
866 }
867
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700868 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
869 if (parsed.capabilities == 0) {
870 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
871 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700872 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700873 filename, parsed.capabilities, strerror(errno));
874 bad++;
875 }
876 } else {
877 struct vfs_cap_data cap_data;
878 memset(&cap_data, 0, sizeof(cap_data));
879 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
880 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
881 cap_data.data[0].inheritable = 0;
882 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
883 cap_data.data[1].inheritable = 0;
884 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700885 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
886 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700887 bad++;
888 }
889 }
890 }
891
892 return bad;
893}
894
895// nftw doesn't allow us to pass along context, so we need to use
896// global variables. *sigh*
897static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700898static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700899
900static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
901 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700902 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700903}
904
905static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700906 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700907 struct stat sb;
908 Value* result = NULL;
909
910 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
911
912 if ((argc % 2) != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700913 return ErrorAbort(state, kArgsParsingFailure,
914 "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700915 }
916
917 char** args = ReadVarArgs(state, argc, argv);
918 if (args == NULL) return NULL;
919
920 if (lstat(args[0], &sb) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700921 result = ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s",
922 name, args[0], strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700923 goto done;
924 }
925
Tao Baoba9a42a2015-06-23 23:23:33 -0700926 {
927 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700928
Tao Baoba9a42a2015-06-23 23:23:33 -0700929 if (recursive) {
930 recursive_parsed_args = parsed;
931 recursive_state = state;
932 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
933 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
934 recursive_state = NULL;
935 } else {
936 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
937 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700938 }
939
940done:
Tao Baoba9a42a2015-06-23 23:23:33 -0700941 for (int i = 0; i < argc; ++i) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700942 free(args[i]);
943 }
944 free(args);
945
946 if (result != NULL) {
947 return result;
948 }
949
950 if (bad > 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700951 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700952 }
953
954 return StringValue(strdup(""));
955}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700956
Doug Zongker512536a2010-02-17 16:11:44 -0800957Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700958 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700959 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700960 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700961 char* key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700962 if (key == NULL) return NULL;
963
964 char value[PROPERTY_VALUE_MAX];
965 property_get(key, value, "");
966 free(key);
967
Doug Zongker512536a2010-02-17 16:11:44 -0800968 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700969}
970
Doug Zongker47cace92009-06-18 10:11:50 -0700971// file_getprop(file, key)
972//
973// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700974// per line. # comment lines,blank lines, lines without '=' ignored),
975// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800976Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700977 char* result = NULL;
978 char* buffer = NULL;
979 char* filename;
980 char* key;
981 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
982 return NULL;
983 }
984
985 struct stat st;
986 if (stat(filename, &st) < 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700987 ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name, filename,
988 strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700989 goto done;
990 }
991
992#define MAX_FILE_GETPROP_SIZE 65536
993
994 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tianjie Xu16255832016-04-30 11:49:59 -0700995 ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %d)", filename, name,
996 MAX_FILE_GETPROP_SIZE);
Doug Zongker47cace92009-06-18 10:11:50 -0700997 goto done;
998 }
999
Tao Baoba9a42a2015-06-23 23:23:33 -07001000 buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -07001001 if (buffer == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -07001002 ErrorAbort(state, kFileGetPropFailure, "%s: failed to alloc %lld bytes", name,
1003 (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -07001004 goto done;
1005 }
1006
Tao Baoba9a42a2015-06-23 23:23:33 -07001007 FILE* f;
1008 f = fopen(filename, "rb");
Doug Zongker47cace92009-06-18 10:11:50 -07001009 if (f == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -07001010 ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename,
1011 strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -07001012 goto done;
1013 }
1014
Jed Estepf1fc48c2015-12-15 16:04:53 -08001015 if (ota_fread(buffer, 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001016 ErrorAbort(state, kFreadFailure, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -07001017 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -07001018 fclose(f);
1019 goto done;
1020 }
1021 buffer[st.st_size] = '\0';
1022
1023 fclose(f);
1024
Tao Baoba9a42a2015-06-23 23:23:33 -07001025 char* line;
1026 line = strtok(buffer, "\n");
Doug Zongker47cace92009-06-18 10:11:50 -07001027 do {
1028 // skip whitespace at start of line
1029 while (*line && isspace(*line)) ++line;
1030
1031 // comment or blank line: skip to next line
1032 if (*line == '\0' || *line == '#') continue;
1033
1034 char* equal = strchr(line, '=');
1035 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001036 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001037 }
1038
1039 // trim whitespace between key and '='
1040 char* key_end = equal-1;
1041 while (key_end > line && isspace(*key_end)) --key_end;
1042 key_end[1] = '\0';
1043
1044 // not the key we're looking for
1045 if (strcmp(key, line) != 0) continue;
1046
1047 // skip whitespace after the '=' to the start of the value
1048 char* val_start = equal+1;
1049 while(*val_start && isspace(*val_start)) ++val_start;
1050
1051 // trim trailing whitespace
1052 char* val_end = val_start + strlen(val_start)-1;
1053 while (val_end > val_start && isspace(*val_end)) --val_end;
1054 val_end[1] = '\0';
1055
1056 result = strdup(val_start);
1057 break;
1058
1059 } while ((line = strtok(NULL, "\n")));
1060
1061 if (result == NULL) result = strdup("");
1062
1063 done:
1064 free(filename);
1065 free(key);
1066 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001067 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001068}
1069
Doug Zongker179b2d92011-04-12 15:49:04 -07001070// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001071Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001072 char* result = NULL;
1073
Doug Zongker179b2d92011-04-12 15:49:04 -07001074 Value* partition_value;
1075 Value* contents;
1076 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001077 return NULL;
1078 }
1079
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001080 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001081 if (partition_value->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001082 ErrorAbort(state, kArgsParsingFailure, "partition argument to %s must be string", name);
Doug Zongker179b2d92011-04-12 15:49:04 -07001083 goto done;
1084 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001085 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001086 if (strlen(partition) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001087 ErrorAbort(state, kArgsParsingFailure, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001088 goto done;
1089 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001090 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001091 ErrorAbort(state, kArgsParsingFailure, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001092 goto done;
1093 }
1094
1095 mtd_scan_partitions();
Tao Baoba9a42a2015-06-23 23:23:33 -07001096 const MtdPartition* mtd;
1097 mtd = mtd_find_partition_by_name(partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001098 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001099 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001100 result = strdup("");
1101 goto done;
1102 }
1103
Tao Baoba9a42a2015-06-23 23:23:33 -07001104 MtdWriteContext* ctx;
1105 ctx = mtd_write_partition(mtd);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001106 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001107 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001108 name, partition);
1109 result = strdup("");
1110 goto done;
1111 }
1112
1113 bool success;
1114
Doug Zongker179b2d92011-04-12 15:49:04 -07001115 if (contents->type == VAL_STRING) {
1116 // we're given a filename as the contents
1117 char* filename = contents->data;
Jed Estepf1fc48c2015-12-15 16:04:53 -08001118 FILE* f = ota_fopen(filename, "rb");
Doug Zongker179b2d92011-04-12 15:49:04 -07001119 if (f == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001120 printf("%s: can't open %s: %s\n", name, filename, strerror(errno));
Doug Zongker179b2d92011-04-12 15:49:04 -07001121 result = strdup("");
1122 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001123 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001124
1125 success = true;
Tao Baoba9a42a2015-06-23 23:23:33 -07001126 char* buffer = reinterpret_cast<char*>(malloc(BUFSIZ));
Doug Zongker179b2d92011-04-12 15:49:04 -07001127 int read;
Jed Estepf1fc48c2015-12-15 16:04:53 -08001128 while (success && (read = ota_fread(buffer, 1, BUFSIZ, f)) > 0) {
Doug Zongker179b2d92011-04-12 15:49:04 -07001129 int wrote = mtd_write_data(ctx, buffer, read);
1130 success = success && (wrote == read);
1131 }
1132 free(buffer);
Jed Estepf1fc48c2015-12-15 16:04:53 -08001133 ota_fclose(f);
Doug Zongker179b2d92011-04-12 15:49:04 -07001134 } else {
1135 // we're given a blob as the contents
1136 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1137 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001138 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001139 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001140 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001141 partition, strerror(errno));
1142 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001143
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001144 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001145 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001146 }
1147 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001148 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001149 }
1150
Doug Zongker179b2d92011-04-12 15:49:04 -07001151 printf("%s %s partition\n",
1152 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001153
1154 result = success ? partition : strdup("");
1155
1156done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001157 if (result != partition) FreeValue(partition_value);
1158 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001159 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001160}
1161
Doug Zongker8edb00c2009-06-11 17:21:44 -07001162// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001163Value* ApplyPatchSpaceFn(const char* name, State* state,
1164 int argc, Expr* argv[]) {
1165 char* bytes_str;
1166 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1167 return NULL;
1168 }
1169
Tao Baob15fd222015-09-24 11:10:51 -07001170 size_t bytes;
1171 if (!android::base::ParseUint(bytes_str, &bytes)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001172 ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n",
1173 name, bytes_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001174 free(bytes_str);
Tao Baob15fd222015-09-24 11:10:51 -07001175 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001176 }
1177
1178 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1179}
1180
Doug Zongker52b40362014-02-10 15:30:30 -08001181// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1182
Doug Zongker512536a2010-02-17 16:11:44 -08001183Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001184 if (argc < 6 || (argc % 2) == 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001185 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an "
1186 "even number, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001187 }
1188
Doug Zongkerc4351c72010-02-22 14:46:32 -08001189 char* source_filename;
1190 char* target_filename;
1191 char* target_sha1;
1192 char* target_size_str;
1193 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1194 &target_sha1, &target_size_str) < 0) {
1195 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001196 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001197
Tao Baob15fd222015-09-24 11:10:51 -07001198 size_t target_size;
1199 if (!android::base::ParseUint(target_size_str, &target_size)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001200 ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count",
1201 name, target_size_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001202 free(source_filename);
1203 free(target_filename);
1204 free(target_sha1);
1205 free(target_size_str);
Tao Baob15fd222015-09-24 11:10:51 -07001206 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001207 }
1208
1209 int patchcount = (argc-4) / 2;
Yabin Cui64be2132016-02-03 18:16:02 -08001210 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc-4, argv+4),
1211 free);
1212 if (!arg_values) {
1213 return nullptr;
1214 }
1215 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patch_shas;
1216 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patches;
1217 // Protect values by unique_ptrs first to get rid of memory leak.
1218 for (int i = 0; i < patchcount * 2; i += 2) {
1219 patch_shas.emplace_back(arg_values.get()[i], FreeValue);
1220 patches.emplace_back(arg_values.get()[i+1], FreeValue);
1221 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001222
Yabin Cui64be2132016-02-03 18:16:02 -08001223 for (int i = 0; i < patchcount; ++i) {
1224 if (patch_shas[i]->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001225 ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001226 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001227 }
Yabin Cui64be2132016-02-03 18:16:02 -08001228 if (patches[i]->type != VAL_BLOB) {
Tianjie Xu16255832016-04-30 11:49:59 -07001229 ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001230 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001231 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001232 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001233
Yabin Cui64be2132016-02-03 18:16:02 -08001234 std::vector<char*> patch_sha_str;
1235 std::vector<Value*> patch_ptrs;
1236 for (int i = 0; i < patchcount; ++i) {
1237 patch_sha_str.push_back(patch_shas[i]->data);
1238 patch_ptrs.push_back(patches[i].get());
Doug Zongker8edb00c2009-06-11 17:21:44 -07001239 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001240
1241 int result = applypatch(source_filename, target_filename,
1242 target_sha1, target_size,
Yabin Cui64be2132016-02-03 18:16:02 -08001243 patchcount, patch_sha_str.data(), patch_ptrs.data(), NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001244
1245 return StringValue(strdup(result == 0 ? "t" : ""));
1246}
1247
1248// apply_patch_check(file, [sha1_1, ...])
1249Value* ApplyPatchCheckFn(const char* name, State* state,
1250 int argc, Expr* argv[]) {
1251 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001252 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %d",
Doug Zongkerc4351c72010-02-22 14:46:32 -08001253 name, argc);
1254 }
1255
1256 char* filename;
1257 if (ReadArgs(state, argv, 1, &filename) < 0) {
1258 return NULL;
1259 }
1260
1261 int patchcount = argc-1;
1262 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1263
1264 int result = applypatch_check(filename, patchcount, sha1s);
1265
1266 int i;
1267 for (i = 0; i < patchcount; ++i) {
1268 free(sha1s[i]);
1269 }
1270 free(sha1s);
1271
1272 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001273}
1274
Tao Bao1107d962015-09-09 17:16:55 -07001275// This is the updater side handler for ui_print() in edify script. Contents
1276// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001277Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001278 char** args = ReadVarArgs(state, argc, argv);
1279 if (args == NULL) {
1280 return NULL;
1281 }
1282
Tao Bao1107d962015-09-09 17:16:55 -07001283 std::string buffer;
1284 for (int i = 0; i < argc; ++i) {
1285 buffer += args[i];
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001286 free(args[i]);
1287 }
1288 free(args);
Tao Bao1107d962015-09-09 17:16:55 -07001289
1290 buffer += "\n";
Michael Runged4a63422014-10-22 19:48:41 -07001291 uiPrint(state, buffer);
Tao Bao1107d962015-09-09 17:16:55 -07001292 return StringValue(strdup(buffer.c_str()));
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001293}
1294
Doug Zongkerd0181b82011-10-19 10:51:12 -07001295Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1296 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001297 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001298 }
1299 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1300 return StringValue(strdup("t"));
1301}
1302
Doug Zongker512536a2010-02-17 16:11:44 -08001303Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001304 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001305 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001306 }
1307 char** args = ReadVarArgs(state, argc, argv);
1308 if (args == NULL) {
1309 return NULL;
1310 }
1311
Tao Baoba9a42a2015-06-23 23:23:33 -07001312 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001313 memcpy(args2, args, sizeof(char*) * argc);
1314 args2[argc] = NULL;
1315
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001316 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001317
1318 pid_t child = fork();
1319 if (child == 0) {
1320 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001321 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001322 _exit(1);
1323 }
1324 int status;
1325 waitpid(child, &status, 0);
1326 if (WIFEXITED(status)) {
1327 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001328 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001329 WEXITSTATUS(status));
1330 }
1331 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001332 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001333 WTERMSIG(status));
1334 }
1335
1336 int i;
1337 for (i = 0; i < argc; ++i) {
1338 free(args[i]);
1339 }
1340 free(args);
1341 free(args2);
1342
1343 char buffer[20];
1344 sprintf(buffer, "%d", status);
1345
Doug Zongker512536a2010-02-17 16:11:44 -08001346 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001347}
1348
Doug Zongker512536a2010-02-17 16:11:44 -08001349// sha1_check(data)
1350// to return the sha1 of the data (given in the format returned by
1351// read_file).
1352//
1353// sha1_check(data, sha1_hex, [sha1_hex, ...])
1354// returns the sha1 of the file if it matches any of the hex
1355// strings passed, or "" if it does not equal any of them.
1356//
1357Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1358 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001359 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongker512536a2010-02-17 16:11:44 -08001360 }
1361
Yabin Cui64be2132016-02-03 18:16:02 -08001362 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc, argv), free);
1363 if (arg_values == nullptr) {
1364 return nullptr;
1365 }
1366 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> args;
1367 for (int i = 0; i < argc; ++i) {
1368 args.emplace_back(arg_values.get()[i], FreeValue);
Doug Zongker512536a2010-02-17 16:11:44 -08001369 }
1370
1371 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001372 return StringValue(strdup(""));
1373 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001374 uint8_t digest[SHA_DIGEST_LENGTH];
1375 SHA1(reinterpret_cast<uint8_t*>(args[0]->data), args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001376
1377 if (argc == 1) {
1378 return StringValue(PrintSha1(digest));
1379 }
1380
1381 int i;
Yabin Cui64be2132016-02-03 18:16:02 -08001382 uint8_t arg_digest[SHA_DIGEST_LENGTH];
Doug Zongker512536a2010-02-17 16:11:44 -08001383 for (i = 1; i < argc; ++i) {
1384 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001385 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001386 name, i);
1387 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1388 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001389 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1390 name, args[i]->data);
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001391 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001392 break;
1393 }
Doug Zongker512536a2010-02-17 16:11:44 -08001394 }
1395 if (i >= argc) {
1396 // Didn't match any of the hex strings; return false.
1397 return StringValue(strdup(""));
1398 }
Yabin Cui64be2132016-02-03 18:16:02 -08001399 // Found a match.
1400 return args[i].release();
Doug Zongker512536a2010-02-17 16:11:44 -08001401}
1402
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001403// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001404// is actually a FileContents*).
1405Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1406 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001407 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker512536a2010-02-17 16:11:44 -08001408 }
1409 char* filename;
1410 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1411
Yabin Cui1c522df2016-02-10 16:41:10 -08001412 Value* v = static_cast<Value*>(malloc(sizeof(Value)));
1413 if (v == nullptr) {
1414 return nullptr;
1415 }
Doug Zongker512536a2010-02-17 16:11:44 -08001416 v->type = VAL_BLOB;
Yabin Cui1c522df2016-02-10 16:41:10 -08001417 v->size = -1;
1418 v->data = nullptr;
Doug Zongker512536a2010-02-17 16:11:44 -08001419
1420 FileContents fc;
Tao Baoccb0ba92016-06-11 03:56:38 -07001421 if (LoadFileContents(filename, &fc) == 0) {
Yabin Cui1c522df2016-02-10 16:41:10 -08001422 v->data = static_cast<char*>(malloc(fc.data.size()));
1423 if (v->data != nullptr) {
1424 memcpy(v->data, fc.data.data(), fc.data.size());
1425 v->size = fc.data.size();
1426 }
Doug Zongker512536a2010-02-17 16:11:44 -08001427 }
Doug Zongker512536a2010-02-17 16:11:44 -08001428 free(filename);
1429 return v;
1430}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001431
Tao Bao1bf17722016-11-03 23:52:01 -07001432// write_value(value, filename)
1433// Writes 'value' to 'filename'.
1434// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
1435Value* WriteValueFn(const char* name, State* state, int argc, Expr* argv[]) {
1436 if (argc != 2) {
1437 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
1438 }
1439
1440 char* value;
1441 char* filename;
1442 if (ReadArgs(state, argv, 2, &value, &filename) < 0) {
1443 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)",
1444 name);
1445 }
1446
1447 bool ret = android::base::WriteStringToFile(value, filename);
1448 if (!ret) {
1449 printf("%s: Failed to write to \"%s\": %s\n", name, filename, strerror(errno));
1450 }
1451
1452 free(value);
1453 free(filename);
1454 return StringValue(strdup(ret ? "t" : ""));
1455}
1456
Doug Zongkerc87bab12013-11-25 13:53:25 -08001457// Immediately reboot the device. Recovery is not finished normally,
1458// so if you reboot into recovery it will re-start applying the
1459// current package (because nothing has cleared the copy of the
1460// arguments stored in the BCB).
1461//
1462// The argument is the partition name passed to the android reboot
1463// property. It can be "recovery" to boot from the recovery
1464// partition, or "" (empty string) to boot from the regular boot
1465// partition.
1466Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1467 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001468 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001469 }
1470
1471 char* filename;
1472 char* property;
1473 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1474
1475 char buffer[80];
1476
1477 // zero out the 'command' field of the bootloader message.
1478 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1479 FILE* f = fopen(filename, "r+b");
1480 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
Jed Estepf1fc48c2015-12-15 16:04:53 -08001481 ota_fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001482 fclose(f);
1483 free(filename);
1484
1485 strcpy(buffer, "reboot,");
1486 if (property != NULL) {
1487 strncat(buffer, property, sizeof(buffer)-10);
1488 }
1489
1490 property_set(ANDROID_RB_PROPERTY, buffer);
1491
1492 sleep(5);
1493 free(property);
Tianjie Xu16255832016-04-30 11:49:59 -07001494 ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001495 return NULL;
1496}
1497
1498// Store a string value somewhere that future invocations of recovery
1499// can access it. This value is called the "stage" and can be used to
1500// drive packages that need to do reboots in the middle of
1501// installation and keep track of where they are in the multi-stage
1502// install.
1503//
1504// The first argument is the block device for the misc partition
1505// ("/misc" in the fstab), which is where this value is stored. The
1506// second argument is the string to store; it should not exceed 31
1507// bytes.
1508Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1509 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001510 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001511 }
1512
1513 char* filename;
1514 char* stagestr;
1515 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1516
1517 // Store this value in the misc partition, immediately after the
1518 // bootloader message that the main recovery uses to save its
1519 // arguments in case of the device restarting midway through
1520 // package installation.
1521 FILE* f = fopen(filename, "r+b");
1522 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1523 int to_write = strlen(stagestr)+1;
1524 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1525 if (to_write > max_size) {
1526 to_write = max_size;
1527 stagestr[max_size-1] = 0;
1528 }
Jed Estepf1fc48c2015-12-15 16:04:53 -08001529 ota_fwrite(stagestr, to_write, 1, f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001530 fclose(f);
1531
1532 free(stagestr);
1533 return StringValue(filename);
1534}
1535
1536// Return the value most recently saved with SetStageFn. The argument
1537// is the block device for the misc partition.
1538Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001539 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001540 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001541 }
1542
1543 char* filename;
1544 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1545
1546 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1547 FILE* f = fopen(filename, "rb");
1548 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Jed Estepf1fc48c2015-12-15 16:04:53 -08001549 ota_fread(buffer, sizeof(buffer), 1, f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001550 fclose(f);
1551 buffer[sizeof(buffer)-1] = '\0';
1552
1553 return StringValue(strdup(buffer));
1554}
1555
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001556Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1557 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001558 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001559 }
1560
1561 char* filename;
1562 char* len_str;
1563 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1564
Tao Baob15fd222015-09-24 11:10:51 -07001565 size_t len;
1566 android::base::ParseUint(len_str, &len);
Jed Estepf1fc48c2015-12-15 16:04:53 -08001567 int fd = ota_open(filename, O_WRONLY, 0644);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001568 int success = wipe_block_device(fd, len);
1569
1570 free(filename);
1571 free(len_str);
1572
Jed Estepf1fc48c2015-12-15 16:04:53 -08001573 ota_close(fd);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001574
1575 return StringValue(strdup(success ? "t" : ""));
1576}
1577
Doug Zongkerc704e062014-05-23 08:40:35 -07001578Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1579 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001580 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerc704e062014-05-23 08:40:35 -07001581 }
1582 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1583 fprintf(ui->cmd_pipe, "enable_reboot\n");
1584 return StringValue(strdup("t"));
1585}
1586
Michael Rungeacf47db2014-11-21 00:12:28 -08001587Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1588 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001589 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %d", name, argc);
Michael Rungeacf47db2014-11-21 00:12:28 -08001590 }
1591
1592 char** args = ReadVarArgs(state, argc, argv);
1593 if (args == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -07001594 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
Michael Rungeacf47db2014-11-21 00:12:28 -08001595 }
1596
Tao Baoba9a42a2015-06-23 23:23:33 -07001597 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Michael Rungeacf47db2014-11-21 00:12:28 -08001598 // Tune2fs expects the program name as its args[0]
1599 args2[0] = strdup(name);
Tao Baoba9a42a2015-06-23 23:23:33 -07001600 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001601 args2[i + 1] = args[i];
1602 }
1603 int result = tune2fs_main(argc + 1, args2);
Tao Baoba9a42a2015-06-23 23:23:33 -07001604 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001605 free(args[i]);
1606 }
1607 free(args);
1608
1609 free(args2[0]);
1610 free(args2);
1611 if (result != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001612 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d",
1613 name, result);
Michael Rungeacf47db2014-11-21 00:12:28 -08001614 }
1615 return StringValue(strdup("t"));
1616}
1617
Doug Zongker9931f7f2009-06-10 14:11:53 -07001618void RegisterInstallFunctions() {
1619 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001620 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001621 RegisterFunction("unmount", UnmountFn);
1622 RegisterFunction("format", FormatFn);
1623 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001624 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001625 RegisterFunction("delete", DeleteFn);
1626 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001627 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1628 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001629 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001630
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001631 // Usage:
1632 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1633 // Example:
1634 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1635 RegisterFunction("set_metadata", SetMetadataFn);
1636
1637 // Usage:
1638 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1639 // Example:
1640 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1641 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1642
Doug Zongker8edb00c2009-06-11 17:21:44 -07001643 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001644 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001645 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001646
1647 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001648 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1649 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001650
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001651 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001652
Doug Zongker512536a2010-02-17 16:11:44 -08001653 RegisterFunction("read_file", ReadFileFn);
1654 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001655 RegisterFunction("rename", RenameFn);
Tao Bao1bf17722016-11-03 23:52:01 -07001656 RegisterFunction("write_value", WriteValueFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001657
Doug Zongkerd0181b82011-10-19 10:51:12 -07001658 RegisterFunction("wipe_cache", WipeCacheFn);
1659
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001660 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001661
1662 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001663
1664 RegisterFunction("reboot_now", RebootNowFn);
1665 RegisterFunction("get_stage", GetStageFn);
1666 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001667
1668 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001669 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001670}