blob: ed55ea517bb7693eb562a9ee7d11536a984cc282 [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
Tao Bao0c7839a2016-10-10 15:48:37 -070017#include "updater/install.h"
18
Doug Zongkerfbf3c102009-06-24 09:36:20 -070019#include <ctype.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070020#include <errno.h>
Tao Bao361342c2016-02-08 11:15:50 -080021#include <fcntl.h>
22#include <ftw.h>
23#include <inttypes.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070024#include <stdarg.h>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070025#include <stdio.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070026#include <stdlib.h>
27#include <string.h>
Tao Bao361342c2016-02-08 11:15:50 -080028#include <sys/capability.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070029#include <sys/mount.h>
30#include <sys/stat.h>
31#include <sys/types.h>
Doug Zongkera3f89ea2009-09-10 14:10:48 -070032#include <sys/wait.h>
Nick Kralevich5dbdef02013-09-07 14:41:06 -070033#include <sys/xattr.h>
Tao Bao361342c2016-02-08 11:15:50 -080034#include <time.h>
35#include <unistd.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070036#include <utime.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070037
Yabin Cui64be2132016-02-03 18:16:02 -080038#include <memory>
Tao Bao361342c2016-02-08 11:15:50 -080039#include <string>
Yabin Cui64be2132016-02-03 18:16:02 -080040#include <vector>
41
Elliott Hughes4b166f02015-12-04 15:30:20 -080042#include <android-base/parseint.h>
Tianjie Xu5fe280a2016-10-17 18:15:20 -070043#include <android-base/parsedouble.h>
Elliott Hughescb220402016-09-23 15:30:55 -070044#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080045#include <android-base/strings.h>
46#include <android-base/stringprintf.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070047#include <cutils/android_reboot.h>
Tao Baode40ba52016-10-05 23:17:01 -070048#include <ext4_utils/make_ext4fs.h>
49#include <ext4_utils/wipe.h>
Tao Bao361342c2016-02-08 11:15:50 -080050#include <openssl/sha.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070051#include <selinux/label.h>
52#include <selinux/selinux.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070053#include <ziparchive/zip_archive.h>
Tao Bao1107d962015-09-09 17:16:55 -070054
Doug Zongkerc87bab12013-11-25 13:53:25 -080055#include "applypatch/applypatch.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070056#include "bootloader.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070057#include "edify/expr.h"
Tianjie Xu16255832016-04-30 11:49:59 -070058#include "error_code.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070059#include "mounts.h"
Jed Estep39c1b5e2015-12-15 16:04:53 -080060#include "ota_io.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070061#include "otautil/DirUtil.h"
62#include "otautil/ZipUtil.h"
Tao Bao361342c2016-02-08 11:15:50 -080063#include "print_sha1.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080064#include "tune2fs.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070065#include "updater/updater.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070066
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) {
Tao Bao0831d0b2016-11-03 23:25:04 -070069 UpdaterInfo* ui = static_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
Elliott Hughes83ce7552016-06-30 09:28:42 -070087void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) {
Tao Bao1107d962015-09-09 17:16:55 -070088 std::string error_msg;
89
Michael Runged4a63422014-10-22 19:48:41 -070090 va_list ap;
91 va_start(ap, format);
Tao Bao1107d962015-09-09 17:16:55 -070092 android::base::StringAppendV(&error_msg, format, ap);
Michael Runged4a63422014-10-22 19:48:41 -070093 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070094
Michael Runged4a63422014-10-22 19:48:41 -070095 uiPrint(state, error_msg);
96}
97
Tao Bao0c7839a2016-10-10 15:48:37 -070098// Create all parent directories of name, if necessary.
99static int make_parents(char* name) {
100 char* p;
101 for (p = name + (strlen(name)-1); p > name; --p) {
102 if (*p != '/') continue;
103 *p = '\0';
104 if (make_parents(name) < 0) return -1;
105 int result = mkdir(name, 0700);
106 if (result == 0) printf("created [%s]\n", name);
107 *p = '/';
108 if (result == 0 || errno == EEXIST) {
109 // successfully created or already existed; we're done
110 return 0;
111 } else {
112 printf("failed to mkdir %s: %s\n", name, strerror(errno));
113 return -1;
114 }
115 }
116 return 0;
117}
118
Doug Zongker3d177d02010-07-01 09:18:44 -0700119// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -0700120// mount(fs_type, partition_type, location, mount_point, mount_options)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700121//
Doug Zongker3d177d02010-07-01 09:18:44 -0700122// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800123Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Michael Runge168f7772014-10-22 17:05:08 -0700124 if (argc != 4 && argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700125 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700126 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700127
128 std::vector<std::string> args;
129 if (!ReadArgs(state, argc, argv, &args)) {
130 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
131 }
132 const std::string& fs_type = args[0];
133 const std::string& partition_type = args[1];
134 const std::string& location = args[2];
135 const std::string& mount_point = args[3];
136 std::string mount_options;
137
Michael Runge168f7772014-10-22 17:05:08 -0700138 if (argc == 5) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700139 mount_options = args[4];
Doug Zongker9931f7f2009-06-10 14:11:53 -0700140 }
141
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700142 if (fs_type.empty()) {
143 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty",
144 name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700145 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700146 if (partition_type.empty()) {
147 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
148 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700149 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700150 if (location.empty()) {
151 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty",
152 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700153 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700154 if (mount_point.empty()) {
155 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
156 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700157 }
158
Tao Baoba9a42a2015-06-23 23:23:33 -0700159 {
160 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500161
Tao Baoba9a42a2015-06-23 23:23:33 -0700162 if (sehandle) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700163 selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755);
Tao Baoba9a42a2015-06-23 23:23:33 -0700164 setfscreatecon(secontext);
165 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500166
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700167 mkdir(mount_point.c_str(), 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700168
Tao Baoba9a42a2015-06-23 23:23:33 -0700169 if (secontext) {
170 freecon(secontext);
171 setfscreatecon(NULL);
172 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500173 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500174
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700175 if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(),
176 MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) {
Elliott Hughes63a31922016-06-09 17:41:22 -0700177 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700178 name, location.c_str(), mount_point.c_str(), strerror(errno));
179 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700180 }
181
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700182 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700183}
184
Doug Zongker8edb00c2009-06-11 17:21:44 -0700185
186// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800187Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700188 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700189 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700190 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700191
192 std::vector<std::string> args;
193 if (!ReadArgs(state, argc, argv, &args)) {
194 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700195 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700196 const std::string& mount_point = args[0];
197 if (mount_point.empty()) {
198 return ErrorAbort(state, kArgsParsingFailure,
199 "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700200 }
201
202 scan_mounted_volumes();
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700203 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
204 if (vol == nullptr) {
205 return StringValue("");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700206 }
207
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700208 return StringValue(mount_point);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700209}
210
211
Doug Zongker512536a2010-02-17 16:11:44 -0800212Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700213 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700214 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700215 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700216 std::vector<std::string> args;
217 if (!ReadArgs(state, argc, argv, &args)) {
218 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700219 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700220 const std::string& mount_point = args[0];
221 if (mount_point.empty()) {
222 return ErrorAbort(state, kArgsParsingFailure,
223 "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700224 }
225
226 scan_mounted_volumes();
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700227 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
228 if (vol == nullptr) {
229 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point.c_str());
230 return nullptr;
231 } else {
232 int ret = unmount_mounted_volume(vol);
233 if (ret != 0) {
234 uiPrintf(state, "unmount of %s failed (%d): %s\n",
235 mount_point.c_str(), ret, strerror(errno));
Michael Runge5ddf4292014-10-24 14:14:41 -0700236 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700237 }
238
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700239 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700240}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700241
JP Abgrall37aedb32014-06-16 19:07:39 -0700242static int exec_cmd(const char* path, char* const argv[]) {
243 int status;
244 pid_t child;
245 if ((child = vfork()) == 0) {
246 execv(path, argv);
247 _exit(-1);
248 }
249 waitpid(child, &status, 0);
250 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
251 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
252 }
253 return WEXITSTATUS(status);
254}
255
Stephen Smalley779701d2012-02-09 14:13:23 -0500256// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700257//
Stephen Smalley779701d2012-02-09 14:13:23 -0500258// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700259// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
260// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800261// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700262// 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 -0800263Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Stephen Smalley516e4e22012-04-03 13:35:11 -0400264 if (argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700265 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700266 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500267
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700268 std::vector<std::string> args;
269 if (!ReadArgs(state, argc, argv, &args)) {
270 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
271 }
272 const std::string& fs_type = args[0];
273 const std::string& partition_type = args[1];
274 const std::string& location = args[2];
275 const std::string& fs_size = args[3];
276 const std::string& mount_point = args[4];
277
278 if (fs_type.empty()) {
279 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty",
280 name);
281 }
282 if (partition_type.empty()) {
283 return ErrorAbort(state, kArgsParsingFailure,
284 "partition_type argument to %s() can't be empty", name);
285 }
286 if (location.empty()) {
287 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty",
288 name);
289 }
290 if (mount_point.empty()) {
291 return ErrorAbort(state, kArgsParsingFailure,
292 "mount_point argument to %s() can't be empty", name);
Stephen Smalley779701d2012-02-09 14:13:23 -0500293 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700294
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700295 int64_t size;
Tao Baoa659d792016-11-03 23:25:04 -0700296 if (!android::base::ParseInt(fs_size, &size)) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700297 return ErrorAbort(state, kArgsParsingFailure,
298 "%s: failed to parse int in %s\n", name, fs_size.c_str());
Doug Zongker9931f7f2009-06-10 14:11:53 -0700299 }
300
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700301 if (fs_type == "ext4") {
302 int status = make_ext4fs(location.c_str(), size, mount_point.c_str(), sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700303 if (status != 0) {
Tao Bao0831d0b2016-11-03 23:25:04 -0700304 printf("%s: make_ext4fs failed (%d) on %s", name, status, location.c_str());
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700305 return StringValue("");
Doug Zongker3d177d02010-07-01 09:18:44 -0700306 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700307 return StringValue(location);
308 } else if (fs_type == "f2fs") {
309 if (size < 0) {
Tao Bao0831d0b2016-11-03 23:25:04 -0700310 printf("%s: fs_size can't be negative for f2fs: %s", name, fs_size.c_str());
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700311 return StringValue("");
JP Abgrall37aedb32014-06-16 19:07:39 -0700312 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700313 std::string num_sectors = std::to_string(size / 512);
314
JP Abgrall37aedb32014-06-16 19:07:39 -0700315 const char *f2fs_path = "/sbin/mkfs.f2fs";
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700316 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location.c_str(),
317 num_sectors.c_str(), nullptr};
JP Abgrall37aedb32014-06-16 19:07:39 -0700318 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
JP Abgrall37aedb32014-06-16 19:07:39 -0700319 if (status != 0) {
Tao Bao0831d0b2016-11-03 23:25:04 -0700320 printf("%s: mkfs.f2fs failed (%d) on %s", name, status, location.c_str());
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700321 return StringValue("");
JP Abgrall37aedb32014-06-16 19:07:39 -0700322 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700323 return StringValue(location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700324 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700325 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700326 name, fs_type.c_str(), partition_type.c_str());
Doug Zongker9931f7f2009-06-10 14:11:53 -0700327 }
328
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700329 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700330}
331
Tao Baoa659d792016-11-03 23:25:04 -0700332// rename(src_name, dst_name)
333// Renames src_name to dst_name. It automatically creates the necessary directories for dst_name.
334// Example: rename("system/app/Hangouts/Hangouts.apk", "system/priv-app/Hangouts/Hangouts.apk")
Michael Rungece7ca712013-11-06 17:42:20 -0800335Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
Michael Rungece7ca712013-11-06 17:42:20 -0800336 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700337 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Michael Rungece7ca712013-11-06 17:42:20 -0800338 }
339
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700340 std::vector<std::string> args;
341 if (!ReadArgs(state, argc, argv, &args)) {
342 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
343 }
344 const std::string& src_name = args[0];
345 std::string& dst_name = args[1];
Michael Rungece7ca712013-11-06 17:42:20 -0800346
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700347 if (src_name.empty()) {
348 return ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty",
349 name);
Michael Rungece7ca712013-11-06 17:42:20 -0800350 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700351 if (dst_name.empty()) {
352 return ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty",
353 name);
Michael Rungece7ca712013-11-06 17:42:20 -0800354 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700355 if (make_parents(&dst_name[0]) != 0) {
356 return ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
357 dst_name.c_str(), strerror(errno));
358 } else if (access(dst_name.c_str(), F_OK) == 0 && access(src_name.c_str(), F_OK) != 0) {
Michael Runge2f0ef732014-10-22 14:28:23 -0700359 // File was already moved
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700360 return StringValue(dst_name);
361 } else if (rename(src_name.c_str(), dst_name.c_str()) != 0) {
362 return ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
363 src_name.c_str(), dst_name.c_str(), strerror(errno));
Michael Rungece7ca712013-11-06 17:42:20 -0800364 }
365
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700366 return StringValue(dst_name);
Michael Rungece7ca712013-11-06 17:42:20 -0800367}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700368
Tao Bao0831d0b2016-11-03 23:25:04 -0700369// delete([filename, ...])
370// Deletes all the filenames listed. Returns the number of files successfully deleted.
371//
372// delete_recursive([dirname, ...])
373// Recursively deletes dirnames and all their contents. Returns the number of directories
374// successfully deleted.
Doug Zongker512536a2010-02-17 16:11:44 -0800375Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao0831d0b2016-11-03 23:25:04 -0700376 std::vector<std::string> paths(argc);
Tao Baoba9a42a2015-06-23 23:23:33 -0700377 for (int i = 0; i < argc; ++i) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700378 if (!Evaluate(state, argv[i], &paths[i])) {
379 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700380 }
381 }
382
383 bool recursive = (strcmp(name, "delete_recursive") == 0);
384
385 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700386 for (int i = 0; i < argc; ++i) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700387 if ((recursive ? dirUnlinkHierarchy(paths[i].c_str()) : unlink(paths[i].c_str())) == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700388 ++success;
Tianjie Xuaced5d92016-10-12 10:55:04 -0700389 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700390 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700391
Tao Bao0831d0b2016-11-03 23:25:04 -0700392 return StringValue(std::to_string(success));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700393}
394
Doug Zongker8edb00c2009-06-11 17:21:44 -0700395
Doug Zongker512536a2010-02-17 16:11:44 -0800396Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700397 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700398 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700399 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700400
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700401 std::vector<std::string> args;
402 if (!ReadArgs(state, argc, argv, &args)) {
403 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
404 }
405 const std::string& frac_str = args[0];
406 const std::string& sec_str = args[1];
407
408 double frac;
409 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
410 return ErrorAbort(state, kArgsParsingFailure,
411 "%s: failed to parse double in %s\n", name, frac_str.c_str());
412 }
Tao Baob15fd222015-09-24 11:10:51 -0700413 int sec;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700414 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
415 return ErrorAbort(state, kArgsParsingFailure,
416 "%s: failed to parse int in %s\n", name, sec_str.c_str());
417 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700418
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700419 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700420 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
421
Doug Zongker512536a2010-02-17 16:11:44 -0800422 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700423}
424
Doug Zongker512536a2010-02-17 16:11:44 -0800425Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700426 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700427 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700428 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700429
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700430 std::vector<std::string> args;
431 if (!ReadArgs(state, 1, argv, &args)) {
432 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
433 }
434 const std::string& frac_str = args[0];
435
436 double frac;
437 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
438 return ErrorAbort(state, kArgsParsingFailure,
439 "%s: failed to parse double in %s\n", name, frac_str.c_str());
440 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700441
442 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
443 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
444
Doug Zongker512536a2010-02-17 16:11:44 -0800445 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700446}
447
Doug Zongker8edb00c2009-06-11 17:21:44 -0700448// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800449Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700450 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700451 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700452 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700453 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700454
455 std::vector<std::string> args;
456 if (!ReadArgs(state, 2, argv, &args)) {
457 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
458 }
459 const std::string& zip_path = args[0];
460 const std::string& dest_path = args[1];
Doug Zongker9931f7f2009-06-10 14:11:53 -0700461
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700462 ZipArchiveHandle za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700463
464 // To create a consistent system image, never use the clock for timestamps.
465 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
466
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700467 bool success = ExtractPackageRecursive(za, zip_path, dest_path, &timestamp, sehandle);
468
Tianjie Xuaced5d92016-10-12 10:55:04 -0700469 return StringValue(success ? "t" : "");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700470}
471
Doug Zongker8edb00c2009-06-11 17:21:44 -0700472
473// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800474// or
475// package_extract_file(package_path)
476// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800477// function (the char* returned is actually a FileContents*).
478Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700479 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700480 if (argc < 1 || argc > 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700481 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800482 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700483 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700484 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700485
Doug Zongker5f875bf2014-08-22 14:53:43 -0700486 if (argc == 2) {
487 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800488
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700489 ZipArchiveHandle za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700490
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700491 std::vector<std::string> args;
492 if (!ReadArgs(state, 2, argv, &args)) {
493 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %d args", name,
494 argc);
495 }
496 const std::string& zip_path = args[0];
497 const std::string& dest_path = args[1];
Doug Zongker6aece332010-02-01 14:40:12 -0800498
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700499 ZipString zip_string_path(zip_path.c_str());
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700500 ZipEntry entry;
501 if (FindEntry(za, zip_string_path, &entry) != 0) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700502 printf("%s: no %s in package\n", name, zip_path.c_str());
503 return StringValue("");
Doug Zongker6aece332010-02-01 14:40:12 -0800504 }
505
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700506 int fd = TEMP_FAILURE_RETRY(ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC,
507 S_IRUSR | S_IWUSR));
508 if (fd == -1) {
509 printf("%s: can't open %s for write: %s\n", name, dest_path.c_str(), strerror(errno));
510 return StringValue("");
511 }
512 success = ExtractEntryToFile(za, &entry, fd);
513 if (ota_fsync(fd) == -1) {
514 printf("fsync of \"%s\" failed: %s\n", dest_path.c_str(), strerror(errno));
515 success = false;
516 }
517 if (ota_close(fd) == -1) {
518 printf("close of \"%s\" failed: %s\n", dest_path.c_str(), strerror(errno));
519 success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800520 }
Doug Zongker6aece332010-02-01 14:40:12 -0800521
Tianjie Xuaced5d92016-10-12 10:55:04 -0700522 return StringValue(success ? "t" : "");
Doug Zongker6aece332010-02-01 14:40:12 -0800523 } else {
524 // The one-argument version returns the contents of the file
525 // as the result.
526
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700527 std::vector<std::string> args;
528 if (!ReadArgs(state, 1, argv, &args)) {
529 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %d args", name,
530 argc);
531 }
532 const std::string& zip_path = args[0];
Yabin Cui64be2132016-02-03 18:16:02 -0800533
Tianjie Xuaced5d92016-10-12 10:55:04 -0700534 Value* v = new Value(VAL_INVALID, "");
Doug Zongker6aece332010-02-01 14:40:12 -0800535
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700536 ZipArchiveHandle za = ((UpdaterInfo*)(state->cookie))->package_zip;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700537 ZipString zip_string_path(zip_path.c_str());
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700538 ZipEntry entry;
539 if (FindEntry(za, zip_string_path, &entry) != 0) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700540 printf("%s: no %s in package\n", name, zip_path.c_str());
541 return v;
Doug Zongker6aece332010-02-01 14:40:12 -0800542 }
543
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700544 v->data.resize(entry.uncompressed_length);
545 if (ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&v->data[0]),
546 v->data.size()) != 0) {
547 printf("%s: faled to extract %zu bytes to memory\n", name, v->data.size());
548 } else {
549 success = true;
550 }
Doug Zongker6aece332010-02-01 14:40:12 -0800551
Doug Zongker6aece332010-02-01 14:40:12 -0800552 if (!success) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700553 v->data.clear();
554 } else {
555 v->type = VAL_BLOB;
Doug Zongker6aece332010-02-01 14:40:12 -0800556 }
Doug Zongker512536a2010-02-17 16:11:44 -0800557 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700558 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700559}
560
Doug Zongker9931f7f2009-06-10 14:11:53 -0700561// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700562// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800563Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700564 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700565 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700566 }
Tianjie Xuaced5d92016-10-12 10:55:04 -0700567 std::string target;
568 if (!Evaluate(state, argv[0], &target)) {
569 return nullptr;
570 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700571
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700572 std::vector<std::string> srcs;
573 if (!ReadArgs(state, argc-1, argv+1, &srcs)) {
574 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700575 }
576
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700577 int bad = 0;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700578 for (int i = 0; i < argc-1; ++i) {
579 if (unlink(srcs[i].c_str()) < 0) {
Doug Zongker60babf82009-09-18 15:11:24 -0700580 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700581 printf("%s: failed to remove %s: %s\n",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700582 name, srcs[i].c_str(), strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700583 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700584 }
585 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700586 if (make_parents(&srcs[i][0])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700587 printf("%s: failed to symlink %s to %s: making parents failed\n",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700588 name, srcs[i].c_str(), target.c_str());
Doug Zongkera23075f2012-08-06 16:19:09 -0700589 ++bad;
590 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700591 if (symlink(target.c_str(), srcs[i].c_str()) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700592 printf("%s: failed to symlink %s to %s: %s\n",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700593 name, srcs[i].c_str(), target.c_str(), strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700594 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700595 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700596 }
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700597 if (bad) {
Tianjie Xu16255832016-04-30 11:49:59 -0700598 return ErrorAbort(state, kSymlinkFailure, "%s: some symlinks failed", name);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700599 }
Tianjie Xuaced5d92016-10-12 10:55:04 -0700600 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700601}
602
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700603struct perm_parsed_args {
604 bool has_uid;
605 uid_t uid;
606 bool has_gid;
607 gid_t gid;
608 bool has_mode;
609 mode_t mode;
610 bool has_fmode;
611 mode_t fmode;
612 bool has_dmode;
613 mode_t dmode;
614 bool has_selabel;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700615 const char* selabel;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700616 bool has_capabilities;
617 uint64_t capabilities;
618};
619
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700620static struct perm_parsed_args ParsePermArgs(State * state, int argc,
621 const std::vector<std::string>& args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700622 int i;
623 struct perm_parsed_args parsed;
624 int bad = 0;
625 static int max_warnings = 20;
626
627 memset(&parsed, 0, sizeof(parsed));
628
629 for (i = 1; i < argc; i += 2) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700630 if (args[i] == "uid") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700631 int64_t uid;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700632 if (sscanf(args[i+1].c_str(), "%" SCNd64, &uid) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700633 parsed.uid = uid;
634 parsed.has_uid = true;
635 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700636 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700637 bad++;
638 }
639 continue;
640 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700641 if (args[i] == "gid") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700642 int64_t gid;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700643 if (sscanf(args[i+1].c_str(), "%" SCNd64, &gid) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700644 parsed.gid = gid;
645 parsed.has_gid = true;
646 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700647 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700648 bad++;
649 }
650 continue;
651 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700652 if (args[i] == "mode") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700653 int32_t mode;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700654 if (sscanf(args[i+1].c_str(), "%" SCNi32, &mode) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700655 parsed.mode = mode;
656 parsed.has_mode = true;
657 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700658 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700659 bad++;
660 }
661 continue;
662 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700663 if (args[i] == "dmode") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700664 int32_t mode;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700665 if (sscanf(args[i+1].c_str(), "%" SCNi32, &mode) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700666 parsed.dmode = mode;
667 parsed.has_dmode = true;
668 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700669 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700670 bad++;
671 }
672 continue;
673 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700674 if (args[i] == "fmode") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700675 int32_t mode;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700676 if (sscanf(args[i+1].c_str(), "%" SCNi32, &mode) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700677 parsed.fmode = mode;
678 parsed.has_fmode = true;
679 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700680 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700681 bad++;
682 }
683 continue;
684 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700685 if (args[i] == "capabilities") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700686 int64_t capabilities;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700687 if (sscanf(args[i+1].c_str(), "%" SCNi64, &capabilities) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700688 parsed.capabilities = capabilities;
689 parsed.has_capabilities = true;
690 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700691 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700692 bad++;
693 }
694 continue;
695 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700696 if (args[i] == "selabel") {
697 if (!args[i+1].empty()) {
698 parsed.selabel = args[i+1].c_str();
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700699 parsed.has_selabel = true;
700 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700701 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700702 bad++;
703 }
704 continue;
705 }
706 if (max_warnings != 0) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700707 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700708 max_warnings--;
709 if (max_warnings == 0) {
710 printf("ParsedPermArgs: suppressing further warnings\n");
711 }
712 }
713 }
714 return parsed;
715}
716
717static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700718 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700719 const char* filename,
720 const struct stat *statptr,
721 struct perm_parsed_args parsed)
722{
723 int bad = 0;
724
Nick Kralevich68802412014-10-23 20:36:42 -0700725 if (parsed.has_selabel) {
726 if (lsetfilecon(filename, parsed.selabel) != 0) {
727 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
728 filename, parsed.selabel, strerror(errno));
729 bad++;
730 }
731 }
732
Nick Kraleviche4612512013-09-10 15:34:19 -0700733 /* ignore symlinks */
734 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700735 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700736 }
737
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700738 if (parsed.has_uid) {
739 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700740 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
741 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700742 bad++;
743 }
744 }
745
746 if (parsed.has_gid) {
747 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700748 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
749 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700750 bad++;
751 }
752 }
753
754 if (parsed.has_mode) {
755 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700756 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
757 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700758 bad++;
759 }
760 }
761
762 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
763 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700764 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
765 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700766 bad++;
767 }
768 }
769
770 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
771 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700772 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700773 filename, parsed.fmode, strerror(errno));
774 bad++;
775 }
776 }
777
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700778 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
779 if (parsed.capabilities == 0) {
780 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
781 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700782 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700783 filename, parsed.capabilities, strerror(errno));
784 bad++;
785 }
786 } else {
787 struct vfs_cap_data cap_data;
788 memset(&cap_data, 0, sizeof(cap_data));
789 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
790 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
791 cap_data.data[0].inheritable = 0;
792 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
793 cap_data.data[1].inheritable = 0;
794 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700795 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
796 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700797 bad++;
798 }
799 }
800 }
801
802 return bad;
803}
804
805// nftw doesn't allow us to pass along context, so we need to use
806// global variables. *sigh*
807static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700808static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700809
810static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
811 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700812 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700813}
814
815static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700816 if ((argc % 2) != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700817 return ErrorAbort(state, kArgsParsingFailure,
818 "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700819 }
820
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700821 std::vector<std::string> args;
822 if (!ReadArgs(state, argc, argv, &args)) {
823 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700824 }
825
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700826 struct stat sb;
827 if (lstat(args[0].c_str(), &sb) == -1) {
828 return ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s",
829 name, args[0].c_str(), strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700830 }
831
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700832 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
833 int bad = 0;
834 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700835
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700836 if (recursive) {
837 recursive_parsed_args = parsed;
838 recursive_state = state;
839 bad += nftw(args[0].c_str(), do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
840 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
841 recursive_state = NULL;
842 } else {
843 bad += ApplyParsedPerms(state, args[0].c_str(), &sb, parsed);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700844 }
845
846 if (bad > 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700847 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700848 }
849
Tianjie Xuaced5d92016-10-12 10:55:04 -0700850 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700851}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700852
Doug Zongker512536a2010-02-17 16:11:44 -0800853Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700854 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700855 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700856 }
Tianjie Xuaced5d92016-10-12 10:55:04 -0700857 std::string key;
858 if (!Evaluate(state, argv[0], &key)) {
859 return nullptr;
860 }
Elliott Hughescb220402016-09-23 15:30:55 -0700861 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700862
Tianjie Xuaced5d92016-10-12 10:55:04 -0700863 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700864}
865
866
Doug Zongker47cace92009-06-18 10:11:50 -0700867// file_getprop(file, key)
868//
869// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -0700870// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700871// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800872Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao51d516e2016-11-03 14:49:01 -0700873 if (argc != 2) {
874 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
875 }
876
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700877 std::vector<std::string> args;
878 if (!ReadArgs(state, 2, argv, &args)) {
879 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker47cace92009-06-18 10:11:50 -0700880 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700881 const std::string& filename = args[0];
882 const std::string& key = args[1];
Doug Zongker47cace92009-06-18 10:11:50 -0700883
884 struct stat st;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700885 if (stat(filename.c_str(), &st) < 0) {
886 return ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name,
887 filename.c_str(), strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700888 }
889
Tao Bao51d516e2016-11-03 14:49:01 -0700890 constexpr off_t MAX_FILE_GETPROP_SIZE = 65536;
Doug Zongker47cace92009-06-18 10:11:50 -0700891 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tao Bao51d516e2016-11-03 14:49:01 -0700892 return ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %lld)",
893 filename.c_str(), name, static_cast<long long>(MAX_FILE_GETPROP_SIZE));
Doug Zongker47cace92009-06-18 10:11:50 -0700894 }
895
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700896 std::string buffer(st.st_size, '\0');
897 FILE* f = ota_fopen(filename.c_str(), "rb");
898 if (f == nullptr) {
899 return ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name,
900 filename.c_str(), strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700901 }
902
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700903 if (ota_fread(&buffer[0], 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
Tianjie Xu84478e82016-05-23 11:42:40 -0700904 ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700905 name, static_cast<size_t>(st.st_size), filename.c_str());
Jed Estepa7b9a462015-12-15 16:04:53 -0800906 ota_fclose(f);
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700907 return nullptr;
Doug Zongker47cace92009-06-18 10:11:50 -0700908 }
Doug Zongker47cace92009-06-18 10:11:50 -0700909
Jed Estepa7b9a462015-12-15 16:04:53 -0800910 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -0700911
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700912 std::vector<std::string> lines = android::base::Split(buffer, "\n");
913 for (size_t i = 0; i < lines.size(); i++) {
914 std::string line = android::base::Trim(lines[i]);
Doug Zongker47cace92009-06-18 10:11:50 -0700915
916 // comment or blank line: skip to next line
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700917 if (line.empty() || line[0] == '#') {
918 continue;
919 }
920 size_t equal_pos = line.find('=');
921 if (equal_pos == std::string::npos) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700922 continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700923 }
924
925 // trim whitespace between key and '='
Tao Bao51d516e2016-11-03 14:49:01 -0700926 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -0700927
928 // not the key we're looking for
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700929 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700930
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700931 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
932 }
Doug Zongker47cace92009-06-18 10:11:50 -0700933
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700934 return StringValue("");
Doug Zongker47cace92009-06-18 10:11:50 -0700935}
936
Doug Zongker8edb00c2009-06-11 17:21:44 -0700937// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -0800938Value* ApplyPatchSpaceFn(const char* name, State* state,
939 int argc, Expr* argv[]) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700940 std::vector<std::string> args;
941 if (!ReadArgs(state, 1, argv, &args)) {
942 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800943 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700944 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800945
Tao Baob15fd222015-09-24 11:10:51 -0700946 size_t bytes;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700947 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
948 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n",
949 name, bytes_str.c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800950 }
951
Tianjie Xuaced5d92016-10-12 10:55:04 -0700952 return StringValue(CacheSizeCheck(bytes) ? "" : "t");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800953}
954
Doug Zongker52b40362014-02-10 15:30:30 -0800955// apply_patch(file, size, init_sha1, tgt_sha1, patch)
956
Doug Zongker512536a2010-02-17 16:11:44 -0800957Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800958 if (argc < 6 || (argc % 2) == 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700959 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an "
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700960 "even number, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700961 }
962
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700963 std::vector<std::string> args;
964 if (!ReadArgs(state, 4, argv, &args)) {
965 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700966 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700967 const std::string& source_filename = args[0];
968 const std::string& target_filename = args[1];
969 const std::string& target_sha1 = args[2];
970 const std::string& target_size_str = args[3];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700971
Tao Baob15fd222015-09-24 11:10:51 -0700972 size_t target_size;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700973 if (!android::base::ParseUint(target_size_str.c_str(), &target_size)) {
974 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count",
975 name, target_size_str.c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800976 }
977
978 int patchcount = (argc-4) / 2;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700979 std::vector<std::unique_ptr<Value>> arg_values;
980 if (!ReadValueArgs(state, argc-4, argv+4, &arg_values)) {
Yabin Cui64be2132016-02-03 18:16:02 -0800981 return nullptr;
982 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700983
Yabin Cui64be2132016-02-03 18:16:02 -0800984 for (int i = 0; i < patchcount; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700985 if (arg_values[i * 2]->type != VAL_STRING) {
986 return ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name,
987 i * 2);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800988 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700989 if (arg_values[i * 2 + 1]->type != VAL_BLOB) {
990 return ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name,
991 i * 2 + 1);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800992 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700993 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700994
Tianjie Xuaced5d92016-10-12 10:55:04 -0700995 std::vector<std::string> patch_sha_str;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700996 std::vector<std::unique_ptr<Value>> patches;
Yabin Cui64be2132016-02-03 18:16:02 -0800997 for (int i = 0; i < patchcount; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700998 patch_sha_str.push_back(arg_values[i * 2]->data);
999 patches.push_back(std::move(arg_values[i * 2 + 1]));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001000 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001001
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001002 int result = applypatch(source_filename.c_str(), target_filename.c_str(),
1003 target_sha1.c_str(), target_size,
1004 patch_sha_str, patches, nullptr);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001005
Tianjie Xuaced5d92016-10-12 10:55:04 -07001006 return StringValue(result == 0 ? "t" : "");
Doug Zongkerc4351c72010-02-22 14:46:32 -08001007}
1008
1009// apply_patch_check(file, [sha1_1, ...])
1010Value* ApplyPatchCheckFn(const char* name, State* state,
1011 int argc, Expr* argv[]) {
1012 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001013 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %d",
Doug Zongkerc4351c72010-02-22 14:46:32 -08001014 name, argc);
1015 }
1016
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001017 std::vector<std::string> args;
1018 if (!ReadArgs(state, 1, argv, &args)) {
1019 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001020 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001021 const std::string& filename = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -08001022
Tianjie Xuaced5d92016-10-12 10:55:04 -07001023 std::vector<std::string> sha1s;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001024 if (!ReadArgs(state, argc - 1, argv + 1, &sha1s)) {
1025 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001026 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001027 int result = applypatch_check(filename.c_str(), sha1s);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001028
1029 return StringValue(result == 0 ? "t" : "");
Doug Zongker8edb00c2009-06-11 17:21:44 -07001030}
1031
Tao Bao1107d962015-09-09 17:16:55 -07001032// This is the updater side handler for ui_print() in edify script. Contents
1033// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001034Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001035 std::vector<std::string> args;
1036 if (!ReadArgs(state, argc, argv, &args)) {
1037 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001038 }
1039
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001040 std::string buffer = android::base::Join(args, "") + "\n";
Michael Runged4a63422014-10-22 19:48:41 -07001041 uiPrint(state, buffer);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001042 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001043}
1044
Doug Zongkerd0181b82011-10-19 10:51:12 -07001045Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1046 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001047 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001048 }
1049 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
Tianjie Xuaced5d92016-10-12 10:55:04 -07001050 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -07001051}
1052
Doug Zongker512536a2010-02-17 16:11:44 -08001053Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001054 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001055 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001056 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001057
1058 std::vector<std::string> args;
1059 if (!ReadArgs(state, argc, argv, &args)) {
1060 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001061 }
1062
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001063 char* args2[argc+1];
1064 for (int i = 0; i < argc; i++) {
1065 args2[i] = &args[i][0];
1066 }
1067 args2[argc] = nullptr;
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001068
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001069 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001070
1071 pid_t child = fork();
1072 if (child == 0) {
1073 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001074 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001075 _exit(1);
1076 }
1077 int status;
1078 waitpid(child, &status, 0);
1079 if (WIFEXITED(status)) {
1080 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001081 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001082 WEXITSTATUS(status));
1083 }
1084 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001085 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001086 WTERMSIG(status));
1087 }
1088
Tianjie Xuaced5d92016-10-12 10:55:04 -07001089 return StringValue(android::base::StringPrintf("%d", status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001090}
1091
Doug Zongker512536a2010-02-17 16:11:44 -08001092// sha1_check(data)
1093// to return the sha1 of the data (given in the format returned by
1094// read_file).
1095//
1096// sha1_check(data, sha1_hex, [sha1_hex, ...])
1097// returns the sha1 of the file if it matches any of the hex
1098// strings passed, or "" if it does not equal any of them.
1099//
1100Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1101 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001102 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongker512536a2010-02-17 16:11:44 -08001103 }
1104
Tianjie Xuaced5d92016-10-12 10:55:04 -07001105 std::vector<std::unique_ptr<Value>> args;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001106 if (!ReadValueArgs(state, argc, argv, &args)) {
1107 return nullptr;
Doug Zongker512536a2010-02-17 16:11:44 -08001108 }
1109
Tianjie Xuaced5d92016-10-12 10:55:04 -07001110 if (args[0]->type == VAL_INVALID) {
1111 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -08001112 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001113 uint8_t digest[SHA_DIGEST_LENGTH];
Tianjie Xuaced5d92016-10-12 10:55:04 -07001114 SHA1(reinterpret_cast<const uint8_t*>(args[0]->data.c_str()), args[0]->data.size(), digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001115
1116 if (argc == 1) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001117 return StringValue(print_sha1(digest));
Doug Zongker512536a2010-02-17 16:11:44 -08001118 }
1119
Tao Bao361342c2016-02-08 11:15:50 -08001120 for (int i = 1; i < argc; ++i) {
1121 uint8_t arg_digest[SHA_DIGEST_LENGTH];
Doug Zongker512536a2010-02-17 16:11:44 -08001122 if (args[i]->type != VAL_STRING) {
Tao Bao361342c2016-02-08 11:15:50 -08001123 printf("%s(): arg %d is not a string; skipping", name, i);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001124 } else if (ParseSha1(args[i]->data.c_str(), arg_digest) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001125 // Warn about bad args and skip them.
Tianjie Xuaced5d92016-10-12 10:55:04 -07001126 printf("%s(): error parsing \"%s\" as sha-1; skipping", name, args[i]->data.c_str());
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001127 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
Tao Bao361342c2016-02-08 11:15:50 -08001128 // Found a match.
1129 return args[i].release();
Doug Zongker512536a2010-02-17 16:11:44 -08001130 }
Doug Zongker512536a2010-02-17 16:11:44 -08001131 }
Tao Bao361342c2016-02-08 11:15:50 -08001132
1133 // Didn't match any of the hex strings; return false.
Tianjie Xuaced5d92016-10-12 10:55:04 -07001134 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -08001135}
1136
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001137// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001138// is actually a FileContents*).
1139Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1140 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001141 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker512536a2010-02-17 16:11:44 -08001142 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001143
1144 std::vector<std::string> args;
1145 if (!ReadArgs(state, 1, argv, &args)) {
1146 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1147 }
1148 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -08001149
Tianjie Xuaced5d92016-10-12 10:55:04 -07001150 Value* v = new Value(VAL_INVALID, "");
Doug Zongker512536a2010-02-17 16:11:44 -08001151
1152 FileContents fc;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001153 if (LoadFileContents(filename.c_str(), &fc) == 0) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001154 v->type = VAL_BLOB;
1155 v->data = std::string(fc.data.begin(), fc.data.end());
Doug Zongker512536a2010-02-17 16:11:44 -08001156 }
Doug Zongker512536a2010-02-17 16:11:44 -08001157 return v;
1158}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001159
Doug Zongkerc87bab12013-11-25 13:53:25 -08001160// Immediately reboot the device. Recovery is not finished normally,
1161// so if you reboot into recovery it will re-start applying the
1162// current package (because nothing has cleared the copy of the
1163// arguments stored in the BCB).
1164//
1165// The argument is the partition name passed to the android reboot
1166// property. It can be "recovery" to boot from the recovery
1167// partition, or "" (empty string) to boot from the regular boot
1168// partition.
1169Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1170 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001171 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001172 }
1173
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001174 std::vector<std::string> args;
1175 if (!ReadArgs(state, 2, argv, &args)) {
1176 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1177 }
1178 const std::string& filename = args[0];
1179 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001180
Doug Zongkerc87bab12013-11-25 13:53:25 -08001181 // zero out the 'command' field of the bootloader message.
Elliott Hughescb220402016-09-23 15:30:55 -07001182 char buffer[80];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001183 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001184 FILE* f = ota_fopen(filename.c_str(), "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001185 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001186 ota_fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1187 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001188
Elliott Hughescb220402016-09-23 15:30:55 -07001189 std::string reboot_cmd = "reboot,";
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001190 reboot_cmd += property;
Elliott Hughescb220402016-09-23 15:30:55 -07001191 android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001192
1193 sleep(5);
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001194 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001195}
1196
1197// Store a string value somewhere that future invocations of recovery
1198// can access it. This value is called the "stage" and can be used to
1199// drive packages that need to do reboots in the middle of
1200// installation and keep track of where they are in the multi-stage
1201// install.
1202//
1203// The first argument is the block device for the misc partition
1204// ("/misc" in the fstab), which is where this value is stored. The
1205// second argument is the string to store; it should not exceed 31
1206// bytes.
1207Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1208 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001209 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001210 }
1211
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001212 std::vector<std::string> args;
1213 if (!ReadArgs(state, 2, argv, &args)) {
1214 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1215 }
1216 const std::string& filename = args[0];
1217 std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001218
1219 // Store this value in the misc partition, immediately after the
1220 // bootloader message that the main recovery uses to save its
1221 // arguments in case of the device restarting midway through
1222 // package installation.
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001223 FILE* f = ota_fopen(filename.c_str(), "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001224 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001225 size_t to_write = stagestr.size();
Tianjie Xuaced5d92016-10-12 10:55:04 -07001226 size_t max_size = sizeof(((struct bootloader_message*)0)->stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001227 if (to_write > max_size) {
1228 to_write = max_size;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001229 stagestr = stagestr.substr(0, max_size-1);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001230 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001231 size_t status = ota_fwrite(stagestr.c_str(), to_write, 1, f);
Jed Estepa7b9a462015-12-15 16:04:53 -08001232 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001233
Tianjie Xuaced5d92016-10-12 10:55:04 -07001234 if (status != to_write) {
1235 return StringValue("");
1236 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001237 return StringValue(filename);
1238}
1239
1240// Return the value most recently saved with SetStageFn. The argument
1241// is the block device for the misc partition.
1242Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001243 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001244 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001245 }
1246
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001247 std::vector<std::string> args;
1248 if (!ReadArgs(state, 1, argv, &args)) {
1249 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1250 }
1251 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001252
1253 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001254 FILE* f = ota_fopen(filename.c_str(), "rb");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001255 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001256 size_t status = ota_fread(buffer, sizeof(buffer), 1, f);
Jed Estepa7b9a462015-12-15 16:04:53 -08001257 ota_fclose(f);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001258 if (status != sizeof(buffer)) {
1259 return StringValue("");
1260 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001261
Tianjie Xuaced5d92016-10-12 10:55:04 -07001262 buffer[sizeof(buffer)-1] = '\0';
1263 return StringValue(buffer);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001264}
1265
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001266Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1267 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001268 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001269 }
1270
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001271 std::vector<std::string> args;
1272 if (!ReadArgs(state, 2, argv, &args)) {
1273 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1274 }
1275 const std::string& filename = args[0];
1276 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001277
Tao Baob15fd222015-09-24 11:10:51 -07001278 size_t len;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001279 if (!android::base::ParseUint(len_str.c_str(), &len)) {
1280 return nullptr;
1281 }
1282 int fd = ota_open(filename.c_str(), O_WRONLY, 0644);
1283 // The wipe_block_device function in ext4_utils returns 0 on success and 1
1284 // for failure.
1285 int status = wipe_block_device(fd, len);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001286
Jed Estepa7b9a462015-12-15 16:04:53 -08001287 ota_close(fd);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001288
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001289 return StringValue((status == 0) ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001290}
1291
Doug Zongkerc704e062014-05-23 08:40:35 -07001292Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1293 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001294 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerc704e062014-05-23 08:40:35 -07001295 }
1296 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1297 fprintf(ui->cmd_pipe, "enable_reboot\n");
Tianjie Xuaced5d92016-10-12 10:55:04 -07001298 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -07001299}
1300
Michael Rungeacf47db2014-11-21 00:12:28 -08001301Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1302 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001303 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %d", name, argc);
Michael Rungeacf47db2014-11-21 00:12:28 -08001304 }
1305
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001306 std::vector<std::string> args;
1307 if (!ReadArgs(state, argc, argv, &args)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001308 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
Michael Rungeacf47db2014-11-21 00:12:28 -08001309 }
1310
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001311 char* args2[argc+1];
Michael Rungeacf47db2014-11-21 00:12:28 -08001312 // Tune2fs expects the program name as its args[0]
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001313 args2[0] = const_cast<char*>(name);
1314 if (args2[0] == nullptr) {
1315 return nullptr;
Michael Rungeacf47db2014-11-21 00:12:28 -08001316 }
Tao Baoba9a42a2015-06-23 23:23:33 -07001317 for (int i = 0; i < argc; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001318 args2[i + 1] = &args[i][0];
Michael Rungeacf47db2014-11-21 00:12:28 -08001319 }
Michael Rungeacf47db2014-11-21 00:12:28 -08001320
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001321 // tune2fs changes the file system parameters on an ext2 file system; it
1322 // returns 0 on success.
1323 int result = tune2fs_main(argc + 1, args2);
1324
Michael Rungeacf47db2014-11-21 00:12:28 -08001325 if (result != 0) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001326 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
Michael Rungeacf47db2014-11-21 00:12:28 -08001327 }
Tianjie Xuaced5d92016-10-12 10:55:04 -07001328 return StringValue("t");
Michael Rungeacf47db2014-11-21 00:12:28 -08001329}
1330
Doug Zongker9931f7f2009-06-10 14:11:53 -07001331void RegisterInstallFunctions() {
1332 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001333 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001334 RegisterFunction("unmount", UnmountFn);
1335 RegisterFunction("format", FormatFn);
1336 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001337 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001338 RegisterFunction("delete", DeleteFn);
1339 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001340 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1341 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001342 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001343
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001344 // Usage:
1345 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1346 // Example:
1347 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1348 RegisterFunction("set_metadata", SetMetadataFn);
1349
1350 // Usage:
1351 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1352 // Example:
1353 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1354 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1355
Doug Zongker8edb00c2009-06-11 17:21:44 -07001356 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001357 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001358
1359 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001360 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1361 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001362
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001363 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001364
Doug Zongker512536a2010-02-17 16:11:44 -08001365 RegisterFunction("read_file", ReadFileFn);
1366 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001367 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001368
Doug Zongkerd0181b82011-10-19 10:51:12 -07001369 RegisterFunction("wipe_cache", WipeCacheFn);
1370
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001371 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001372
1373 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001374
1375 RegisterFunction("reboot_now", RebootNowFn);
1376 RegisterFunction("get_stage", GetStageFn);
1377 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001378
1379 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001380 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001381}