blob: 1a647dfa5013444c43b5854532e76e3a814b6cc6 [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 Zongkerc9d6e4f2014-02-24 16:02:50 -080057#include "install.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080058#include "tune2fs.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070059
Doug Zongker3d177d02010-07-01 09:18:44 -070060#ifdef USE_EXT4
61#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080062#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070063#endif
64
Tao Bao1107d962015-09-09 17:16:55 -070065// Send over the buffer to recovery though the command pipe.
66static void uiPrint(State* state, const std::string& buffer) {
67 UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070068
Tao Bao1107d962015-09-09 17:16:55 -070069 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
70 // So skip sending empty strings to UI.
71 std::vector<std::string> lines = android::base::Split(buffer, "\n");
72 for (auto& line: lines) {
73 if (!line.empty()) {
74 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
75 fprintf(ui->cmd_pipe, "ui_print\n");
76 }
77 }
78
79 // On the updater side, we need to dump the contents to stderr (which has
80 // been redirected to the log file). Because the recovery will only print
81 // the contents to screen when processing pipe command ui_print.
82 fprintf(stderr, "%s", buffer.c_str());
Michael Runged4a63422014-10-22 19:48:41 -070083}
84
85__attribute__((__format__(printf, 2, 3))) __nonnull((2))
86void uiPrintf(State* state, const char* format, ...) {
Tao Bao1107d962015-09-09 17:16:55 -070087 std::string error_msg;
88
Michael Runged4a63422014-10-22 19:48:41 -070089 va_list ap;
90 va_start(ap, format);
Tao Bao1107d962015-09-09 17:16:55 -070091 android::base::StringAppendV(&error_msg, format, ap);
Michael Runged4a63422014-10-22 19:48:41 -070092 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070093
Michael Runged4a63422014-10-22 19:48:41 -070094 uiPrint(state, error_msg);
95}
96
Doug Zongker52b40362014-02-10 15:30:30 -080097// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070098char* PrintSha1(const uint8_t* digest) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +080099 char* buffer = reinterpret_cast<char*>(malloc(SHA_DIGEST_LENGTH*2 + 1));
Doug Zongker52b40362014-02-10 15:30:30 -0800100 const char* alphabet = "0123456789abcdef";
Tao Baoba9a42a2015-06-23 23:23:33 -0700101 size_t i;
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800102 for (i = 0; i < SHA_DIGEST_LENGTH; ++i) {
Doug Zongker52b40362014-02-10 15:30:30 -0800103 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
104 buffer[i*2+1] = alphabet[digest[i] & 0xf];
105 }
106 buffer[i*2] = '\0';
107 return buffer;
108}
109
Doug Zongker3d177d02010-07-01 09:18:44 -0700110// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700111//
Doug Zongker3d177d02010-07-01 09:18:44 -0700112// fs_type="yaffs2" partition_type="MTD" location=partition
113// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800114Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700115 char* result = NULL;
Michael Runge168f7772014-10-22 17:05:08 -0700116 if (argc != 4 && argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700117 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700118 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700119 char* fs_type;
120 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700121 char* location;
122 char* mount_point;
Michael Runge168f7772014-10-22 17:05:08 -0700123 char* mount_options;
124 bool has_mount_options;
125 if (argc == 5) {
126 has_mount_options = true;
127 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
128 &location, &mount_point, &mount_options) < 0) {
129 return NULL;
130 }
131 } else {
132 has_mount_options = false;
133 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700134 &location, &mount_point) < 0) {
Michael Runge168f7772014-10-22 17:05:08 -0700135 return NULL;
136 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700137 }
138
Doug Zongker3d177d02010-07-01 09:18:44 -0700139 if (strlen(fs_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700140 ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700141 goto done;
142 }
143 if (strlen(partition_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700144 ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
Doug Zongker3d177d02010-07-01 09:18:44 -0700145 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700146 goto done;
147 }
148 if (strlen(location) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700149 ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700150 goto done;
151 }
152 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700153 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
154 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700155 goto done;
156 }
157
Tao Baoba9a42a2015-06-23 23:23:33 -0700158 {
159 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500160
Tao Baoba9a42a2015-06-23 23:23:33 -0700161 if (sehandle) {
162 selabel_lookup(sehandle, &secontext, mount_point, 0755);
163 setfscreatecon(secontext);
164 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500165
Tao Baoba9a42a2015-06-23 23:23:33 -0700166 mkdir(mount_point, 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700167
Tao Baoba9a42a2015-06-23 23:23:33 -0700168 if (secontext) {
169 freecon(secontext);
170 setfscreatecon(NULL);
171 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500172 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500173
Doug Zongker3d177d02010-07-01 09:18:44 -0700174 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700175 mtd_scan_partitions();
176 const MtdPartition* mtd;
177 mtd = mtd_find_partition_by_name(location);
178 if (mtd == NULL) {
Tao Bao1107d962015-09-09 17:16:55 -0700179 uiPrintf(state, "%s: no mtd partition named \"%s\"\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700180 name, location);
181 result = strdup("");
182 goto done;
183 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700184 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700185 uiPrintf(state, "mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700186 location, strerror(errno));
187 result = strdup("");
188 goto done;
189 }
190 result = mount_point;
191 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700192 if (mount(location, mount_point, fs_type,
Michael Runge168f7772014-10-22 17:05:08 -0700193 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
194 has_mount_options ? mount_options : "") < 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700195 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700196 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700197 result = strdup("");
198 } else {
199 result = mount_point;
200 }
201 }
202
203done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700204 free(fs_type);
205 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700206 free(location);
207 if (result != mount_point) free(mount_point);
Michael Runge168f7772014-10-22 17:05:08 -0700208 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800209 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700210}
211
Doug Zongker8edb00c2009-06-11 17:21:44 -0700212
213// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800214Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700215 char* result = NULL;
216 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700217 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700218 }
219 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700220 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700221 return NULL;
222 }
223 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700224 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700225 goto done;
226 }
227
228 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700229 {
230 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
231 if (vol == NULL) {
232 result = strdup("");
233 } else {
234 result = mount_point;
235 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700236 }
237
238done:
239 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800240 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700241}
242
243
Doug Zongker512536a2010-02-17 16:11:44 -0800244Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700245 char* result = NULL;
246 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700247 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700248 }
249 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700250 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700251 return NULL;
252 }
253 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700254 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700255 goto done;
256 }
257
258 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700259 {
260 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
261 if (vol == NULL) {
262 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
263 result = strdup("");
264 } else {
265 int ret = unmount_mounted_volume(vol);
266 if (ret != 0) {
267 uiPrintf(state, "unmount of %s failed (%d): %s\n",
268 mount_point, ret, strerror(errno));
269 }
270 result = mount_point;
Michael Runge5ddf4292014-10-24 14:14:41 -0700271 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700272 }
273
274done:
275 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800276 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700277}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700278
JP Abgrall37aedb32014-06-16 19:07:39 -0700279static int exec_cmd(const char* path, char* const argv[]) {
280 int status;
281 pid_t child;
282 if ((child = vfork()) == 0) {
283 execv(path, argv);
284 _exit(-1);
285 }
286 waitpid(child, &status, 0);
287 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
288 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
289 }
290 return WEXITSTATUS(status);
291}
292
Doug Zongker8edb00c2009-06-11 17:21:44 -0700293
Stephen Smalley779701d2012-02-09 14:13:23 -0500294// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700295//
Stephen Smalley779701d2012-02-09 14:13:23 -0500296// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
297// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700298// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
299// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800300// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700301// 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 -0800302Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700303 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400304 if (argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700305 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700306 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700307 char* fs_type;
308 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700309 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800310 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400311 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500312
Stephen Smalley779701d2012-02-09 14:13:23 -0500313 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
314 return NULL;
315 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700316
Doug Zongker3d177d02010-07-01 09:18:44 -0700317 if (strlen(fs_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700318 ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700319 goto done;
320 }
321 if (strlen(partition_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700322 ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
Doug Zongker3d177d02010-07-01 09:18:44 -0700323 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700324 goto done;
325 }
326 if (strlen(location) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700327 ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700328 goto done;
329 }
330
Stephen Smalley516e4e22012-04-03 13:35:11 -0400331 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700332 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
333 name);
Stephen Smalley779701d2012-02-09 14:13:23 -0500334 goto done;
335 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500336
Doug Zongker3d177d02010-07-01 09:18:44 -0700337 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700338 mtd_scan_partitions();
339 const MtdPartition* mtd = mtd_find_partition_by_name(location);
340 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700341 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700342 name, location);
343 result = strdup("");
344 goto done;
345 }
346 MtdWriteContext* ctx = mtd_write_partition(mtd);
347 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700348 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700349 result = strdup("");
350 goto done;
351 }
352 if (mtd_erase_blocks(ctx, -1) == -1) {
353 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700354 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700355 result = strdup("");
356 goto done;
357 }
358 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700359 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700360 result = strdup("");
361 goto done;
362 }
363 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700364#ifdef USE_EXT4
365 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500366 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700367 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700368 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700369 name, status, location);
370 result = strdup("");
371 goto done;
372 }
373 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700374 } else if (strcmp(fs_type, "f2fs") == 0) {
375 char *num_sectors;
376 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
377 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
378 result = strdup("");
379 goto done;
380 }
381 const char *f2fs_path = "/sbin/mkfs.f2fs";
382 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
383 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
384 free(num_sectors);
385 if (status != 0) {
386 printf("%s: mkfs.f2fs failed (%d) on %s",
387 name, status, location);
388 result = strdup("");
389 goto done;
390 }
391 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700392#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700393 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700394 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700395 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700396 }
397
398done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700399 free(fs_type);
400 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700401 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800402 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700403}
404
Michael Rungece7ca712013-11-06 17:42:20 -0800405Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
406 char* result = NULL;
407 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700408 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Michael Rungece7ca712013-11-06 17:42:20 -0800409 }
410
411 char* src_name;
412 char* dst_name;
413
414 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
415 return NULL;
416 }
417 if (strlen(src_name) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700418 ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800419 goto done;
420 }
421 if (strlen(dst_name) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700422 ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800423 goto done;
424 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700425 if (make_parents(dst_name) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700426 ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700427 dst_name, strerror(errno));
Michael Runge2f0ef732014-10-22 14:28:23 -0700428 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
429 // File was already moved
430 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700431 } else if (rename(src_name, dst_name) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700432 ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800433 src_name, dst_name, strerror(errno));
434 } else {
435 result = dst_name;
436 }
437
438done:
439 free(src_name);
440 if (result != dst_name) free(dst_name);
441 return StringValue(result);
442}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700443
Doug Zongker512536a2010-02-17 16:11:44 -0800444Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700445 char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
446 for (int i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700447 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700448 if (paths[i] == NULL) {
Yabin Cui64be2132016-02-03 18:16:02 -0800449 for (int j = 0; j < i; ++j) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700450 free(paths[j]);
451 }
452 free(paths);
453 return NULL;
454 }
455 }
456
457 bool recursive = (strcmp(name, "delete_recursive") == 0);
458
459 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700460 for (int i = 0; i < argc; ++i) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700461 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
462 ++success;
463 free(paths[i]);
464 }
465 free(paths);
466
467 char buffer[10];
468 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800469 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700470}
471
Doug Zongker8edb00c2009-06-11 17:21:44 -0700472
Doug Zongker512536a2010-02-17 16:11:44 -0800473Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700474 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700475 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700476 }
477 char* frac_str;
478 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700479 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700480 return NULL;
481 }
482
483 double frac = strtod(frac_str, NULL);
Tao Baob15fd222015-09-24 11:10:51 -0700484 int sec;
485 android::base::ParseInt(sec_str, &sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700486
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700487 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700488 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
489
Doug Zongker9931f7f2009-06-10 14:11:53 -0700490 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800491 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700492}
493
Doug Zongker512536a2010-02-17 16:11:44 -0800494Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700495 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700496 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700497 }
498 char* frac_str;
499 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
500 return NULL;
501 }
502
503 double frac = strtod(frac_str, NULL);
504
505 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
506 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
507
Doug Zongker512536a2010-02-17 16:11:44 -0800508 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700509}
510
Doug Zongker8edb00c2009-06-11 17:21:44 -0700511// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800512Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700513 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700514 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700515 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700516 }
517 char* zip_path;
518 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700519 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700520
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700521 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700522
523 // To create a consistent system image, never use the clock for timestamps.
524 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
525
526 bool success = mzExtractRecursive(za, zip_path, dest_path,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000527 &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500528 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700529 free(zip_path);
530 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800531 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700532}
533
Doug Zongker8edb00c2009-06-11 17:21:44 -0700534
535// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800536// or
537// package_extract_file(package_path)
538// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800539// function (the char* returned is actually a FileContents*).
540Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700541 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700542 if (argc < 1 || argc > 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700543 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800544 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700545 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700546 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700547
Doug Zongker5f875bf2014-08-22 14:53:43 -0700548 if (argc == 2) {
549 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800550
551 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700552
Doug Zongker6aece332010-02-01 14:40:12 -0800553 char* zip_path;
554 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700555 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800556
Doug Zongker6aece332010-02-01 14:40:12 -0800557 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
558 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700559 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800560 goto done2;
561 }
562
Tao Baoba9a42a2015-06-23 23:23:33 -0700563 {
Jed Estepf1fc48c2015-12-15 16:04:53 -0800564 int fd = TEMP_FAILURE_RETRY(ota_open(dest_path, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
Tao Baod3cac342015-12-14 15:53:25 -0800565 S_IRUSR | S_IWUSR));
566 if (fd == -1) {
567 printf("%s: can't open %s for write: %s\n", name, dest_path, strerror(errno));
Tao Baoba9a42a2015-06-23 23:23:33 -0700568 goto done2;
569 }
Tao Baod3cac342015-12-14 15:53:25 -0800570 success = mzExtractZipEntryToFile(za, entry, fd);
Jed Estepf1fc48c2015-12-15 16:04:53 -0800571 if (ota_fsync(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800572 printf("fsync of \"%s\" failed: %s\n", dest_path, strerror(errno));
573 success = false;
574 }
Jed Estepf1fc48c2015-12-15 16:04:53 -0800575 if (ota_close(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800576 printf("close of \"%s\" failed: %s\n", dest_path, strerror(errno));
577 success = false;
578 }
Doug Zongker6aece332010-02-01 14:40:12 -0800579 }
Doug Zongker6aece332010-02-01 14:40:12 -0800580
581 done2:
582 free(zip_path);
583 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800584 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800585 } else {
586 // The one-argument version returns the contents of the file
587 // as the result.
588
589 char* zip_path;
Yabin Cui64be2132016-02-03 18:16:02 -0800590 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
591
Tao Baoba9a42a2015-06-23 23:23:33 -0700592 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -0800593 v->type = VAL_BLOB;
594 v->size = -1;
595 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800596
Doug Zongker6aece332010-02-01 14:40:12 -0800597 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
598 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
599 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700600 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800601 goto done1;
602 }
603
Doug Zongker512536a2010-02-17 16:11:44 -0800604 v->size = mzGetZipEntryUncompLen(entry);
Tao Baoba9a42a2015-06-23 23:23:33 -0700605 v->data = reinterpret_cast<char*>(malloc(v->size));
Doug Zongker512536a2010-02-17 16:11:44 -0800606 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700607 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800608 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800609 goto done1;
610 }
611
Doug Zongker512536a2010-02-17 16:11:44 -0800612 success = mzExtractZipEntryToBuffer(za, entry,
613 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800614
615 done1:
616 free(zip_path);
617 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800618 free(v->data);
619 v->data = NULL;
620 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800621 }
Doug Zongker512536a2010-02-17 16:11:44 -0800622 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700623 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700624}
625
Doug Zongkera23075f2012-08-06 16:19:09 -0700626// Create all parent directories of name, if necessary.
627static int make_parents(char* name) {
628 char* p;
629 for (p = name + (strlen(name)-1); p > name; --p) {
630 if (*p != '/') continue;
631 *p = '\0';
632 if (make_parents(name) < 0) return -1;
633 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700634 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700635 *p = '/';
636 if (result == 0 || errno == EEXIST) {
637 // successfully created or already existed; we're done
638 return 0;
639 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700640 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700641 return -1;
642 }
643 }
644 return 0;
645}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700646
Doug Zongker9931f7f2009-06-10 14:11:53 -0700647// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700648// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800649Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700650 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700651 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700652 }
653 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700654 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700655 if (target == NULL) return NULL;
656
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700657 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700658 if (srcs == NULL) {
659 free(target);
660 return NULL;
661 }
662
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700663 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700664 int i;
665 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700666 if (unlink(srcs[i]) < 0) {
667 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700668 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700669 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700670 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700671 }
672 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700673 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700674 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700675 name, srcs[i], target);
676 ++bad;
677 }
Doug Zongker60babf82009-09-18 15:11:24 -0700678 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700679 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700680 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700681 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700682 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700683 free(srcs[i]);
684 }
685 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700686 if (bad) {
Tianjie Xu16255832016-04-30 11:49:59 -0700687 return ErrorAbort(state, kSymlinkFailure, "%s: some symlinks failed", name);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700688 }
Doug Zongker512536a2010-02-17 16:11:44 -0800689 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700690}
691
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700692struct perm_parsed_args {
693 bool has_uid;
694 uid_t uid;
695 bool has_gid;
696 gid_t gid;
697 bool has_mode;
698 mode_t mode;
699 bool has_fmode;
700 mode_t fmode;
701 bool has_dmode;
702 mode_t dmode;
703 bool has_selabel;
704 char* selabel;
705 bool has_capabilities;
706 uint64_t capabilities;
707};
708
Michael Runged4a63422014-10-22 19:48:41 -0700709static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700710 int i;
711 struct perm_parsed_args parsed;
712 int bad = 0;
713 static int max_warnings = 20;
714
715 memset(&parsed, 0, sizeof(parsed));
716
717 for (i = 1; i < argc; i += 2) {
718 if (strcmp("uid", args[i]) == 0) {
719 int64_t uid;
720 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
721 parsed.uid = uid;
722 parsed.has_uid = true;
723 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700724 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700725 bad++;
726 }
727 continue;
728 }
729 if (strcmp("gid", args[i]) == 0) {
730 int64_t gid;
731 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
732 parsed.gid = gid;
733 parsed.has_gid = true;
734 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700735 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700736 bad++;
737 }
738 continue;
739 }
740 if (strcmp("mode", args[i]) == 0) {
741 int32_t mode;
742 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
743 parsed.mode = mode;
744 parsed.has_mode = true;
745 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700746 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700747 bad++;
748 }
749 continue;
750 }
751 if (strcmp("dmode", args[i]) == 0) {
752 int32_t mode;
753 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
754 parsed.dmode = mode;
755 parsed.has_dmode = true;
756 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700757 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700758 bad++;
759 }
760 continue;
761 }
762 if (strcmp("fmode", args[i]) == 0) {
763 int32_t mode;
764 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
765 parsed.fmode = mode;
766 parsed.has_fmode = true;
767 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700768 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700769 bad++;
770 }
771 continue;
772 }
773 if (strcmp("capabilities", args[i]) == 0) {
774 int64_t capabilities;
775 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
776 parsed.capabilities = capabilities;
777 parsed.has_capabilities = true;
778 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700779 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700780 bad++;
781 }
782 continue;
783 }
784 if (strcmp("selabel", args[i]) == 0) {
785 if (args[i+1][0] != '\0') {
786 parsed.selabel = args[i+1];
787 parsed.has_selabel = true;
788 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700789 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700790 bad++;
791 }
792 continue;
793 }
794 if (max_warnings != 0) {
795 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
796 max_warnings--;
797 if (max_warnings == 0) {
798 printf("ParsedPermArgs: suppressing further warnings\n");
799 }
800 }
801 }
802 return parsed;
803}
804
805static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700806 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700807 const char* filename,
808 const struct stat *statptr,
809 struct perm_parsed_args parsed)
810{
811 int bad = 0;
812
Nick Kralevich68802412014-10-23 20:36:42 -0700813 if (parsed.has_selabel) {
814 if (lsetfilecon(filename, parsed.selabel) != 0) {
815 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
816 filename, parsed.selabel, strerror(errno));
817 bad++;
818 }
819 }
820
Nick Kraleviche4612512013-09-10 15:34:19 -0700821 /* ignore symlinks */
822 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700823 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700824 }
825
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700826 if (parsed.has_uid) {
827 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700828 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
829 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700830 bad++;
831 }
832 }
833
834 if (parsed.has_gid) {
835 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700836 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
837 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700838 bad++;
839 }
840 }
841
842 if (parsed.has_mode) {
843 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700844 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
845 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700846 bad++;
847 }
848 }
849
850 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
851 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700852 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
853 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700854 bad++;
855 }
856 }
857
858 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
859 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700860 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700861 filename, parsed.fmode, strerror(errno));
862 bad++;
863 }
864 }
865
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700866 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
867 if (parsed.capabilities == 0) {
868 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
869 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700870 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700871 filename, parsed.capabilities, strerror(errno));
872 bad++;
873 }
874 } else {
875 struct vfs_cap_data cap_data;
876 memset(&cap_data, 0, sizeof(cap_data));
877 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
878 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
879 cap_data.data[0].inheritable = 0;
880 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
881 cap_data.data[1].inheritable = 0;
882 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700883 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
884 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700885 bad++;
886 }
887 }
888 }
889
890 return bad;
891}
892
893// nftw doesn't allow us to pass along context, so we need to use
894// global variables. *sigh*
895static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700896static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700897
898static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
899 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700900 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700901}
902
903static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700904 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700905 struct stat sb;
906 Value* result = NULL;
907
908 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
909
910 if ((argc % 2) != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700911 return ErrorAbort(state, kArgsParsingFailure,
912 "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700913 }
914
915 char** args = ReadVarArgs(state, argc, argv);
916 if (args == NULL) return NULL;
917
918 if (lstat(args[0], &sb) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700919 result = ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s",
920 name, args[0], strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700921 goto done;
922 }
923
Tao Baoba9a42a2015-06-23 23:23:33 -0700924 {
925 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700926
Tao Baoba9a42a2015-06-23 23:23:33 -0700927 if (recursive) {
928 recursive_parsed_args = parsed;
929 recursive_state = state;
930 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
931 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
932 recursive_state = NULL;
933 } else {
934 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
935 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700936 }
937
938done:
Tao Baoba9a42a2015-06-23 23:23:33 -0700939 for (int i = 0; i < argc; ++i) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700940 free(args[i]);
941 }
942 free(args);
943
944 if (result != NULL) {
945 return result;
946 }
947
948 if (bad > 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700949 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700950 }
951
952 return StringValue(strdup(""));
953}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700954
Doug Zongker512536a2010-02-17 16:11:44 -0800955Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700956 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700957 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700958 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700959 char* key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700960 if (key == NULL) return NULL;
961
962 char value[PROPERTY_VALUE_MAX];
963 property_get(key, value, "");
964 free(key);
965
Doug Zongker512536a2010-02-17 16:11:44 -0800966 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700967}
968
969
Doug Zongker47cace92009-06-18 10:11:50 -0700970// file_getprop(file, key)
971//
972// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700973// per line. # comment lines,blank lines, lines without '=' ignored),
974// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800975Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700976 char* result = NULL;
977 char* buffer = NULL;
978 char* filename;
979 char* key;
980 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
981 return NULL;
982 }
983
984 struct stat st;
985 if (stat(filename, &st) < 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700986 ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name, filename,
987 strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700988 goto done;
989 }
990
991#define MAX_FILE_GETPROP_SIZE 65536
992
993 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tianjie Xu16255832016-04-30 11:49:59 -0700994 ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %d)", filename, name,
995 MAX_FILE_GETPROP_SIZE);
Doug Zongker47cace92009-06-18 10:11:50 -0700996 goto done;
997 }
998
Tao Baoba9a42a2015-06-23 23:23:33 -0700999 buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -07001000 if (buffer == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -07001001 ErrorAbort(state, kFileGetPropFailure, "%s: failed to alloc %lld bytes", name,
1002 (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -07001003 goto done;
1004 }
1005
Tao Baoba9a42a2015-06-23 23:23:33 -07001006 FILE* f;
1007 f = fopen(filename, "rb");
Doug Zongker47cace92009-06-18 10:11:50 -07001008 if (f == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -07001009 ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename,
1010 strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -07001011 goto done;
1012 }
1013
Jed Estepf1fc48c2015-12-15 16:04:53 -08001014 if (ota_fread(buffer, 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001015 ErrorAbort(state, kFreadFailure, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -07001016 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -07001017 fclose(f);
1018 goto done;
1019 }
1020 buffer[st.st_size] = '\0';
1021
1022 fclose(f);
1023
Tao Baoba9a42a2015-06-23 23:23:33 -07001024 char* line;
1025 line = strtok(buffer, "\n");
Doug Zongker47cace92009-06-18 10:11:50 -07001026 do {
1027 // skip whitespace at start of line
1028 while (*line && isspace(*line)) ++line;
1029
1030 // comment or blank line: skip to next line
1031 if (*line == '\0' || *line == '#') continue;
1032
1033 char* equal = strchr(line, '=');
1034 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001035 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001036 }
1037
1038 // trim whitespace between key and '='
1039 char* key_end = equal-1;
1040 while (key_end > line && isspace(*key_end)) --key_end;
1041 key_end[1] = '\0';
1042
1043 // not the key we're looking for
1044 if (strcmp(key, line) != 0) continue;
1045
1046 // skip whitespace after the '=' to the start of the value
1047 char* val_start = equal+1;
1048 while(*val_start && isspace(*val_start)) ++val_start;
1049
1050 // trim trailing whitespace
1051 char* val_end = val_start + strlen(val_start)-1;
1052 while (val_end > val_start && isspace(*val_end)) --val_end;
1053 val_end[1] = '\0';
1054
1055 result = strdup(val_start);
1056 break;
1057
1058 } while ((line = strtok(NULL, "\n")));
1059
1060 if (result == NULL) result = strdup("");
1061
1062 done:
1063 free(filename);
1064 free(key);
1065 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001066 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001067}
1068
Doug Zongker179b2d92011-04-12 15:49:04 -07001069// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001070Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001071 char* result = NULL;
1072
Doug Zongker179b2d92011-04-12 15:49:04 -07001073 Value* partition_value;
1074 Value* contents;
1075 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001076 return NULL;
1077 }
1078
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001079 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001080 if (partition_value->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001081 ErrorAbort(state, kArgsParsingFailure, "partition argument to %s must be string", name);
Doug Zongker179b2d92011-04-12 15:49:04 -07001082 goto done;
1083 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001084 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001085 if (strlen(partition) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001086 ErrorAbort(state, kArgsParsingFailure, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001087 goto done;
1088 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001089 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001090 ErrorAbort(state, kArgsParsingFailure, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001091 goto done;
1092 }
1093
1094 mtd_scan_partitions();
Tao Baoba9a42a2015-06-23 23:23:33 -07001095 const MtdPartition* mtd;
1096 mtd = mtd_find_partition_by_name(partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001097 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001098 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001099 result = strdup("");
1100 goto done;
1101 }
1102
Tao Baoba9a42a2015-06-23 23:23:33 -07001103 MtdWriteContext* ctx;
1104 ctx = mtd_write_partition(mtd);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001105 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001106 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001107 name, partition);
1108 result = strdup("");
1109 goto done;
1110 }
1111
1112 bool success;
1113
Doug Zongker179b2d92011-04-12 15:49:04 -07001114 if (contents->type == VAL_STRING) {
1115 // we're given a filename as the contents
1116 char* filename = contents->data;
Jed Estepf1fc48c2015-12-15 16:04:53 -08001117 FILE* f = ota_fopen(filename, "rb");
Doug Zongker179b2d92011-04-12 15:49:04 -07001118 if (f == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001119 printf("%s: can't open %s: %s\n", name, filename, strerror(errno));
Doug Zongker179b2d92011-04-12 15:49:04 -07001120 result = strdup("");
1121 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001122 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001123
1124 success = true;
Tao Baoba9a42a2015-06-23 23:23:33 -07001125 char* buffer = reinterpret_cast<char*>(malloc(BUFSIZ));
Doug Zongker179b2d92011-04-12 15:49:04 -07001126 int read;
Jed Estepf1fc48c2015-12-15 16:04:53 -08001127 while (success && (read = ota_fread(buffer, 1, BUFSIZ, f)) > 0) {
Doug Zongker179b2d92011-04-12 15:49:04 -07001128 int wrote = mtd_write_data(ctx, buffer, read);
1129 success = success && (wrote == read);
1130 }
1131 free(buffer);
Jed Estepf1fc48c2015-12-15 16:04:53 -08001132 ota_fclose(f);
Doug Zongker179b2d92011-04-12 15:49:04 -07001133 } else {
1134 // we're given a blob as the contents
1135 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1136 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001137 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001138 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001139 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001140 partition, strerror(errno));
1141 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001142
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001143 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001144 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001145 }
1146 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001147 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001148 }
1149
Doug Zongker179b2d92011-04-12 15:49:04 -07001150 printf("%s %s partition\n",
1151 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001152
1153 result = success ? partition : strdup("");
1154
1155done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001156 if (result != partition) FreeValue(partition_value);
1157 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001158 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001159}
1160
Doug Zongker8edb00c2009-06-11 17:21:44 -07001161// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001162Value* ApplyPatchSpaceFn(const char* name, State* state,
1163 int argc, Expr* argv[]) {
1164 char* bytes_str;
1165 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1166 return NULL;
1167 }
1168
Tao Baob15fd222015-09-24 11:10:51 -07001169 size_t bytes;
1170 if (!android::base::ParseUint(bytes_str, &bytes)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001171 ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n",
1172 name, bytes_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001173 free(bytes_str);
Tao Baob15fd222015-09-24 11:10:51 -07001174 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001175 }
1176
1177 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1178}
1179
Doug Zongker52b40362014-02-10 15:30:30 -08001180// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1181
Doug Zongker512536a2010-02-17 16:11:44 -08001182Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001183 if (argc < 6 || (argc % 2) == 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001184 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an "
1185 "even number, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001186 }
1187
Doug Zongkerc4351c72010-02-22 14:46:32 -08001188 char* source_filename;
1189 char* target_filename;
1190 char* target_sha1;
1191 char* target_size_str;
1192 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1193 &target_sha1, &target_size_str) < 0) {
1194 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001195 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001196
Tao Baob15fd222015-09-24 11:10:51 -07001197 size_t target_size;
1198 if (!android::base::ParseUint(target_size_str, &target_size)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001199 ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count",
1200 name, target_size_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001201 free(source_filename);
1202 free(target_filename);
1203 free(target_sha1);
1204 free(target_size_str);
Tao Baob15fd222015-09-24 11:10:51 -07001205 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001206 }
1207
1208 int patchcount = (argc-4) / 2;
Yabin Cui64be2132016-02-03 18:16:02 -08001209 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc-4, argv+4),
1210 free);
1211 if (!arg_values) {
1212 return nullptr;
1213 }
1214 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patch_shas;
1215 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patches;
1216 // Protect values by unique_ptrs first to get rid of memory leak.
1217 for (int i = 0; i < patchcount * 2; i += 2) {
1218 patch_shas.emplace_back(arg_values.get()[i], FreeValue);
1219 patches.emplace_back(arg_values.get()[i+1], FreeValue);
1220 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001221
Yabin Cui64be2132016-02-03 18:16:02 -08001222 for (int i = 0; i < patchcount; ++i) {
1223 if (patch_shas[i]->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001224 ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001225 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001226 }
Yabin Cui64be2132016-02-03 18:16:02 -08001227 if (patches[i]->type != VAL_BLOB) {
Tianjie Xu16255832016-04-30 11:49:59 -07001228 ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001229 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001230 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001231 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001232
Yabin Cui64be2132016-02-03 18:16:02 -08001233 std::vector<char*> patch_sha_str;
1234 std::vector<Value*> patch_ptrs;
1235 for (int i = 0; i < patchcount; ++i) {
1236 patch_sha_str.push_back(patch_shas[i]->data);
1237 patch_ptrs.push_back(patches[i].get());
Doug Zongker8edb00c2009-06-11 17:21:44 -07001238 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001239
1240 int result = applypatch(source_filename, target_filename,
1241 target_sha1, target_size,
Yabin Cui64be2132016-02-03 18:16:02 -08001242 patchcount, patch_sha_str.data(), patch_ptrs.data(), NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001243
1244 return StringValue(strdup(result == 0 ? "t" : ""));
1245}
1246
1247// apply_patch_check(file, [sha1_1, ...])
1248Value* ApplyPatchCheckFn(const char* name, State* state,
1249 int argc, Expr* argv[]) {
1250 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001251 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %d",
Doug Zongkerc4351c72010-02-22 14:46:32 -08001252 name, argc);
1253 }
1254
1255 char* filename;
1256 if (ReadArgs(state, argv, 1, &filename) < 0) {
1257 return NULL;
1258 }
1259
1260 int patchcount = argc-1;
1261 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1262
1263 int result = applypatch_check(filename, patchcount, sha1s);
1264
1265 int i;
1266 for (i = 0; i < patchcount; ++i) {
1267 free(sha1s[i]);
1268 }
1269 free(sha1s);
1270
1271 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001272}
1273
Tao Bao1107d962015-09-09 17:16:55 -07001274// This is the updater side handler for ui_print() in edify script. Contents
1275// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001276Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001277 char** args = ReadVarArgs(state, argc, argv);
1278 if (args == NULL) {
1279 return NULL;
1280 }
1281
Tao Bao1107d962015-09-09 17:16:55 -07001282 std::string buffer;
1283 for (int i = 0; i < argc; ++i) {
1284 buffer += args[i];
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001285 free(args[i]);
1286 }
1287 free(args);
Tao Bao1107d962015-09-09 17:16:55 -07001288
1289 buffer += "\n";
Michael Runged4a63422014-10-22 19:48:41 -07001290 uiPrint(state, buffer);
Tao Bao1107d962015-09-09 17:16:55 -07001291 return StringValue(strdup(buffer.c_str()));
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001292}
1293
Doug Zongkerd0181b82011-10-19 10:51:12 -07001294Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1295 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001296 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001297 }
1298 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1299 return StringValue(strdup("t"));
1300}
1301
Doug Zongker512536a2010-02-17 16:11:44 -08001302Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001303 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001304 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001305 }
1306 char** args = ReadVarArgs(state, argc, argv);
1307 if (args == NULL) {
1308 return NULL;
1309 }
1310
Tao Baoba9a42a2015-06-23 23:23:33 -07001311 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001312 memcpy(args2, args, sizeof(char*) * argc);
1313 args2[argc] = NULL;
1314
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001315 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001316
1317 pid_t child = fork();
1318 if (child == 0) {
1319 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001320 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001321 _exit(1);
1322 }
1323 int status;
1324 waitpid(child, &status, 0);
1325 if (WIFEXITED(status)) {
1326 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001327 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001328 WEXITSTATUS(status));
1329 }
1330 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001331 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001332 WTERMSIG(status));
1333 }
1334
1335 int i;
1336 for (i = 0; i < argc; ++i) {
1337 free(args[i]);
1338 }
1339 free(args);
1340 free(args2);
1341
1342 char buffer[20];
1343 sprintf(buffer, "%d", status);
1344
Doug Zongker512536a2010-02-17 16:11:44 -08001345 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001346}
1347
Doug Zongker512536a2010-02-17 16:11:44 -08001348// sha1_check(data)
1349// to return the sha1 of the data (given in the format returned by
1350// read_file).
1351//
1352// sha1_check(data, sha1_hex, [sha1_hex, ...])
1353// returns the sha1 of the file if it matches any of the hex
1354// strings passed, or "" if it does not equal any of them.
1355//
1356Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1357 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001358 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongker512536a2010-02-17 16:11:44 -08001359 }
1360
Yabin Cui64be2132016-02-03 18:16:02 -08001361 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc, argv), free);
1362 if (arg_values == nullptr) {
1363 return nullptr;
1364 }
1365 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> args;
1366 for (int i = 0; i < argc; ++i) {
1367 args.emplace_back(arg_values.get()[i], FreeValue);
Doug Zongker512536a2010-02-17 16:11:44 -08001368 }
1369
1370 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001371 return StringValue(strdup(""));
1372 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001373 uint8_t digest[SHA_DIGEST_LENGTH];
1374 SHA1(reinterpret_cast<uint8_t*>(args[0]->data), args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001375
1376 if (argc == 1) {
1377 return StringValue(PrintSha1(digest));
1378 }
1379
1380 int i;
Yabin Cui64be2132016-02-03 18:16:02 -08001381 uint8_t arg_digest[SHA_DIGEST_LENGTH];
Doug Zongker512536a2010-02-17 16:11:44 -08001382 for (i = 1; i < argc; ++i) {
1383 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001384 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001385 name, i);
1386 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1387 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001388 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1389 name, args[i]->data);
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001390 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001391 break;
1392 }
Doug Zongker512536a2010-02-17 16:11:44 -08001393 }
1394 if (i >= argc) {
1395 // Didn't match any of the hex strings; return false.
1396 return StringValue(strdup(""));
1397 }
Yabin Cui64be2132016-02-03 18:16:02 -08001398 // Found a match.
1399 return args[i].release();
Doug Zongker512536a2010-02-17 16:11:44 -08001400}
1401
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001402// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001403// is actually a FileContents*).
1404Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1405 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001406 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker512536a2010-02-17 16:11:44 -08001407 }
1408 char* filename;
1409 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1410
Yabin Cui1c522df2016-02-10 16:41:10 -08001411 Value* v = static_cast<Value*>(malloc(sizeof(Value)));
1412 if (v == nullptr) {
1413 return nullptr;
1414 }
Doug Zongker512536a2010-02-17 16:11:44 -08001415 v->type = VAL_BLOB;
Yabin Cui1c522df2016-02-10 16:41:10 -08001416 v->size = -1;
1417 v->data = nullptr;
Doug Zongker512536a2010-02-17 16:11:44 -08001418
1419 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001420 if (LoadFileContents(filename, &fc) != 0) {
Yabin Cui1c522df2016-02-10 16:41:10 -08001421 v->data = static_cast<char*>(malloc(fc.data.size()));
1422 if (v->data != nullptr) {
1423 memcpy(v->data, fc.data.data(), fc.data.size());
1424 v->size = fc.data.size();
1425 }
Doug Zongker512536a2010-02-17 16:11:44 -08001426 }
Doug Zongker512536a2010-02-17 16:11:44 -08001427 free(filename);
1428 return v;
1429}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001430
Doug Zongkerc87bab12013-11-25 13:53:25 -08001431// Immediately reboot the device. Recovery is not finished normally,
1432// so if you reboot into recovery it will re-start applying the
1433// current package (because nothing has cleared the copy of the
1434// arguments stored in the BCB).
1435//
1436// The argument is the partition name passed to the android reboot
1437// property. It can be "recovery" to boot from the recovery
1438// partition, or "" (empty string) to boot from the regular boot
1439// partition.
1440Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1441 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001442 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001443 }
1444
1445 char* filename;
1446 char* property;
1447 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1448
1449 char buffer[80];
1450
1451 // zero out the 'command' field of the bootloader message.
1452 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1453 FILE* f = fopen(filename, "r+b");
1454 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
Jed Estepf1fc48c2015-12-15 16:04:53 -08001455 ota_fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001456 fclose(f);
1457 free(filename);
1458
1459 strcpy(buffer, "reboot,");
1460 if (property != NULL) {
1461 strncat(buffer, property, sizeof(buffer)-10);
1462 }
1463
1464 property_set(ANDROID_RB_PROPERTY, buffer);
1465
1466 sleep(5);
1467 free(property);
Tianjie Xu16255832016-04-30 11:49:59 -07001468 ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001469 return NULL;
1470}
1471
1472// Store a string value somewhere that future invocations of recovery
1473// can access it. This value is called the "stage" and can be used to
1474// drive packages that need to do reboots in the middle of
1475// installation and keep track of where they are in the multi-stage
1476// install.
1477//
1478// The first argument is the block device for the misc partition
1479// ("/misc" in the fstab), which is where this value is stored. The
1480// second argument is the string to store; it should not exceed 31
1481// bytes.
1482Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1483 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001484 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001485 }
1486
1487 char* filename;
1488 char* stagestr;
1489 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1490
1491 // Store this value in the misc partition, immediately after the
1492 // bootloader message that the main recovery uses to save its
1493 // arguments in case of the device restarting midway through
1494 // package installation.
1495 FILE* f = fopen(filename, "r+b");
1496 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1497 int to_write = strlen(stagestr)+1;
1498 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1499 if (to_write > max_size) {
1500 to_write = max_size;
1501 stagestr[max_size-1] = 0;
1502 }
Jed Estepf1fc48c2015-12-15 16:04:53 -08001503 ota_fwrite(stagestr, to_write, 1, f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001504 fclose(f);
1505
1506 free(stagestr);
1507 return StringValue(filename);
1508}
1509
1510// Return the value most recently saved with SetStageFn. The argument
1511// is the block device for the misc partition.
1512Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001513 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001514 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001515 }
1516
1517 char* filename;
1518 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1519
1520 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1521 FILE* f = fopen(filename, "rb");
1522 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Jed Estepf1fc48c2015-12-15 16:04:53 -08001523 ota_fread(buffer, sizeof(buffer), 1, f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001524 fclose(f);
1525 buffer[sizeof(buffer)-1] = '\0';
1526
1527 return StringValue(strdup(buffer));
1528}
1529
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001530Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1531 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001532 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001533 }
1534
1535 char* filename;
1536 char* len_str;
1537 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1538
Tao Baob15fd222015-09-24 11:10:51 -07001539 size_t len;
1540 android::base::ParseUint(len_str, &len);
Jed Estepf1fc48c2015-12-15 16:04:53 -08001541 int fd = ota_open(filename, O_WRONLY, 0644);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001542 int success = wipe_block_device(fd, len);
1543
1544 free(filename);
1545 free(len_str);
1546
Jed Estepf1fc48c2015-12-15 16:04:53 -08001547 ota_close(fd);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001548
1549 return StringValue(strdup(success ? "t" : ""));
1550}
1551
Doug Zongkerc704e062014-05-23 08:40:35 -07001552Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1553 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001554 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerc704e062014-05-23 08:40:35 -07001555 }
1556 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1557 fprintf(ui->cmd_pipe, "enable_reboot\n");
1558 return StringValue(strdup("t"));
1559}
1560
Michael Rungeacf47db2014-11-21 00:12:28 -08001561Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1562 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001563 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %d", name, argc);
Michael Rungeacf47db2014-11-21 00:12:28 -08001564 }
1565
1566 char** args = ReadVarArgs(state, argc, argv);
1567 if (args == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -07001568 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
Michael Rungeacf47db2014-11-21 00:12:28 -08001569 }
1570
Tao Baoba9a42a2015-06-23 23:23:33 -07001571 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Michael Rungeacf47db2014-11-21 00:12:28 -08001572 // Tune2fs expects the program name as its args[0]
1573 args2[0] = strdup(name);
Tao Baoba9a42a2015-06-23 23:23:33 -07001574 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001575 args2[i + 1] = args[i];
1576 }
1577 int result = tune2fs_main(argc + 1, args2);
Tao Baoba9a42a2015-06-23 23:23:33 -07001578 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001579 free(args[i]);
1580 }
1581 free(args);
1582
1583 free(args2[0]);
1584 free(args2);
1585 if (result != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001586 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d",
1587 name, result);
Michael Rungeacf47db2014-11-21 00:12:28 -08001588 }
1589 return StringValue(strdup("t"));
1590}
1591
Doug Zongker9931f7f2009-06-10 14:11:53 -07001592void RegisterInstallFunctions() {
1593 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001594 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001595 RegisterFunction("unmount", UnmountFn);
1596 RegisterFunction("format", FormatFn);
1597 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001598 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001599 RegisterFunction("delete", DeleteFn);
1600 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001601 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1602 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001603 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001604
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001605 // Usage:
1606 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1607 // Example:
1608 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1609 RegisterFunction("set_metadata", SetMetadataFn);
1610
1611 // Usage:
1612 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1613 // Example:
1614 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1615 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1616
Doug Zongker8edb00c2009-06-11 17:21:44 -07001617 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001618 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001619 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001620
1621 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001622 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1623 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001624
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001625 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001626
Doug Zongker512536a2010-02-17 16:11:44 -08001627 RegisterFunction("read_file", ReadFileFn);
1628 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001629 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001630
Doug Zongkerd0181b82011-10-19 10:51:12 -07001631 RegisterFunction("wipe_cache", WipeCacheFn);
1632
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001633 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001634
1635 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001636
1637 RegisterFunction("reboot_now", RebootNowFn);
1638 RegisterFunction("get_stage", GetStageFn);
1639 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001640
1641 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001642 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001643}