blob: cba95f5b04a52844e3cb31cf8dd82f647d0549ca [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
Tianjie Xud75003d2016-11-04 11:31:29 -070098static bool is_dir(const std::string& dirpath) {
99 struct stat st;
100 return stat(dirpath.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
101}
102
Tao Bao0c7839a2016-10-10 15:48:37 -0700103// Create all parent directories of name, if necessary.
Tianjie Xud75003d2016-11-04 11:31:29 -0700104static bool make_parents(const std::string& name) {
105 size_t prev_end = 0;
106 while (prev_end < name.size()) {
107 size_t next_end = name.find('/', prev_end + 1);
108 if (next_end == std::string::npos) {
109 break;
Tao Bao0c7839a2016-10-10 15:48:37 -0700110 }
Tianjie Xud75003d2016-11-04 11:31:29 -0700111 std::string dir_path = name.substr(0, next_end);
112 if (!is_dir(dir_path)) {
113 int result = mkdir(dir_path.c_str(), 0700);
114 if (result != 0) {
115 printf("failed to mkdir %s when make parents for %s: %s\n", dir_path.c_str(),
116 name.c_str(), strerror(errno));
117 return false;
118 }
119
120 printf("created [%s]\n", dir_path.c_str());
121 }
122 prev_end = next_end;
Tao Bao0c7839a2016-10-10 15:48:37 -0700123 }
Tianjie Xud75003d2016-11-04 11:31:29 -0700124 return true;
Tao Bao0c7839a2016-10-10 15:48:37 -0700125}
126
Doug Zongker3d177d02010-07-01 09:18:44 -0700127// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -0700128// mount(fs_type, partition_type, location, mount_point, mount_options)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700129//
Doug Zongker3d177d02010-07-01 09:18:44 -0700130// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800131Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Michael Runge168f7772014-10-22 17:05:08 -0700132 if (argc != 4 && argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700133 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700134 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700135
136 std::vector<std::string> args;
137 if (!ReadArgs(state, argc, argv, &args)) {
138 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
139 }
140 const std::string& fs_type = args[0];
141 const std::string& partition_type = args[1];
142 const std::string& location = args[2];
143 const std::string& mount_point = args[3];
144 std::string mount_options;
145
Michael Runge168f7772014-10-22 17:05:08 -0700146 if (argc == 5) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700147 mount_options = args[4];
Doug Zongker9931f7f2009-06-10 14:11:53 -0700148 }
149
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700150 if (fs_type.empty()) {
151 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty",
152 name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700153 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700154 if (partition_type.empty()) {
155 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
156 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700157 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700158 if (location.empty()) {
159 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty",
160 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700161 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700162 if (mount_point.empty()) {
163 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
164 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700165 }
166
Tao Baoba9a42a2015-06-23 23:23:33 -0700167 {
168 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500169
Tao Baoba9a42a2015-06-23 23:23:33 -0700170 if (sehandle) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700171 selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755);
Tao Baoba9a42a2015-06-23 23:23:33 -0700172 setfscreatecon(secontext);
173 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500174
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700175 mkdir(mount_point.c_str(), 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700176
Tao Baoba9a42a2015-06-23 23:23:33 -0700177 if (secontext) {
178 freecon(secontext);
179 setfscreatecon(NULL);
180 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500181 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500182
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700183 if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(),
184 MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) {
Elliott Hughes63a31922016-06-09 17:41:22 -0700185 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700186 name, location.c_str(), mount_point.c_str(), strerror(errno));
187 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700188 }
189
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700190 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700191}
192
Doug Zongker8edb00c2009-06-11 17:21:44 -0700193
194// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800195Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700196 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700197 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700198 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700199
200 std::vector<std::string> args;
201 if (!ReadArgs(state, argc, argv, &args)) {
202 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700203 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700204 const std::string& mount_point = args[0];
205 if (mount_point.empty()) {
206 return ErrorAbort(state, kArgsParsingFailure,
207 "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700208 }
209
210 scan_mounted_volumes();
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700211 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
212 if (vol == nullptr) {
213 return StringValue("");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700214 }
215
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700216 return StringValue(mount_point);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700217}
218
219
Doug Zongker512536a2010-02-17 16:11:44 -0800220Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700221 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700222 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700223 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700224 std::vector<std::string> args;
225 if (!ReadArgs(state, argc, argv, &args)) {
226 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700227 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700228 const std::string& mount_point = args[0];
229 if (mount_point.empty()) {
230 return ErrorAbort(state, kArgsParsingFailure,
231 "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700232 }
233
234 scan_mounted_volumes();
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700235 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
236 if (vol == nullptr) {
237 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point.c_str());
238 return nullptr;
239 } else {
240 int ret = unmount_mounted_volume(vol);
241 if (ret != 0) {
242 uiPrintf(state, "unmount of %s failed (%d): %s\n",
243 mount_point.c_str(), ret, strerror(errno));
Michael Runge5ddf4292014-10-24 14:14:41 -0700244 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700245 }
246
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700247 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700248}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700249
JP Abgrall37aedb32014-06-16 19:07:39 -0700250static int exec_cmd(const char* path, char* const argv[]) {
251 int status;
252 pid_t child;
253 if ((child = vfork()) == 0) {
254 execv(path, argv);
255 _exit(-1);
256 }
257 waitpid(child, &status, 0);
258 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
259 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
260 }
261 return WEXITSTATUS(status);
262}
263
Stephen Smalley779701d2012-02-09 14:13:23 -0500264// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700265//
Stephen Smalley779701d2012-02-09 14:13:23 -0500266// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700267// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
268// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800269// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700270// 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 -0800271Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Stephen Smalley516e4e22012-04-03 13:35:11 -0400272 if (argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700273 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700274 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500275
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700276 std::vector<std::string> args;
277 if (!ReadArgs(state, argc, argv, &args)) {
278 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
279 }
280 const std::string& fs_type = args[0];
281 const std::string& partition_type = args[1];
282 const std::string& location = args[2];
283 const std::string& fs_size = args[3];
284 const std::string& mount_point = args[4];
285
286 if (fs_type.empty()) {
287 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty",
288 name);
289 }
290 if (partition_type.empty()) {
291 return ErrorAbort(state, kArgsParsingFailure,
292 "partition_type argument to %s() can't be empty", name);
293 }
294 if (location.empty()) {
295 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty",
296 name);
297 }
298 if (mount_point.empty()) {
299 return ErrorAbort(state, kArgsParsingFailure,
300 "mount_point argument to %s() can't be empty", name);
Stephen Smalley779701d2012-02-09 14:13:23 -0500301 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700302
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700303 int64_t size;
Tao Baoa659d792016-11-03 23:25:04 -0700304 if (!android::base::ParseInt(fs_size, &size)) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700305 return ErrorAbort(state, kArgsParsingFailure,
306 "%s: failed to parse int in %s\n", name, fs_size.c_str());
Doug Zongker9931f7f2009-06-10 14:11:53 -0700307 }
308
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700309 if (fs_type == "ext4") {
310 int status = make_ext4fs(location.c_str(), size, mount_point.c_str(), sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700311 if (status != 0) {
Tao Bao0831d0b2016-11-03 23:25:04 -0700312 printf("%s: make_ext4fs failed (%d) on %s", name, status, location.c_str());
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700313 return StringValue("");
Doug Zongker3d177d02010-07-01 09:18:44 -0700314 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700315 return StringValue(location);
316 } else if (fs_type == "f2fs") {
317 if (size < 0) {
Tao Bao0831d0b2016-11-03 23:25:04 -0700318 printf("%s: fs_size can't be negative for f2fs: %s", name, fs_size.c_str());
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700319 return StringValue("");
JP Abgrall37aedb32014-06-16 19:07:39 -0700320 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700321 std::string num_sectors = std::to_string(size / 512);
322
JP Abgrall37aedb32014-06-16 19:07:39 -0700323 const char *f2fs_path = "/sbin/mkfs.f2fs";
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700324 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location.c_str(),
325 num_sectors.c_str(), nullptr};
JP Abgrall37aedb32014-06-16 19:07:39 -0700326 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
JP Abgrall37aedb32014-06-16 19:07:39 -0700327 if (status != 0) {
Tao Bao0831d0b2016-11-03 23:25:04 -0700328 printf("%s: mkfs.f2fs failed (%d) on %s", name, status, location.c_str());
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700329 return StringValue("");
JP Abgrall37aedb32014-06-16 19:07:39 -0700330 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700331 return StringValue(location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700332 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700333 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700334 name, fs_type.c_str(), partition_type.c_str());
Doug Zongker9931f7f2009-06-10 14:11:53 -0700335 }
336
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700337 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700338}
339
Tao Baoa659d792016-11-03 23:25:04 -0700340// rename(src_name, dst_name)
341// Renames src_name to dst_name. It automatically creates the necessary directories for dst_name.
342// Example: rename("system/app/Hangouts/Hangouts.apk", "system/priv-app/Hangouts/Hangouts.apk")
Michael Rungece7ca712013-11-06 17:42:20 -0800343Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
Michael Rungece7ca712013-11-06 17:42:20 -0800344 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700345 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Michael Rungece7ca712013-11-06 17:42:20 -0800346 }
347
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700348 std::vector<std::string> args;
349 if (!ReadArgs(state, argc, argv, &args)) {
350 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
351 }
352 const std::string& src_name = args[0];
Tianjie Xud75003d2016-11-04 11:31:29 -0700353 const std::string& dst_name = args[1];
Michael Rungece7ca712013-11-06 17:42:20 -0800354
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700355 if (src_name.empty()) {
356 return ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty",
357 name);
Michael Rungece7ca712013-11-06 17:42:20 -0800358 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700359 if (dst_name.empty()) {
360 return ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty",
361 name);
Michael Rungece7ca712013-11-06 17:42:20 -0800362 }
Tianjie Xud75003d2016-11-04 11:31:29 -0700363 if (!make_parents(dst_name)) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700364 return ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
365 dst_name.c_str(), strerror(errno));
366 } 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 -0700367 // File was already moved
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700368 return StringValue(dst_name);
369 } else if (rename(src_name.c_str(), dst_name.c_str()) != 0) {
370 return ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
371 src_name.c_str(), dst_name.c_str(), strerror(errno));
Michael Rungece7ca712013-11-06 17:42:20 -0800372 }
373
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700374 return StringValue(dst_name);
Michael Rungece7ca712013-11-06 17:42:20 -0800375}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700376
Tao Bao0831d0b2016-11-03 23:25:04 -0700377// delete([filename, ...])
378// Deletes all the filenames listed. Returns the number of files successfully deleted.
379//
380// delete_recursive([dirname, ...])
381// Recursively deletes dirnames and all their contents. Returns the number of directories
382// successfully deleted.
Doug Zongker512536a2010-02-17 16:11:44 -0800383Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao0831d0b2016-11-03 23:25:04 -0700384 std::vector<std::string> paths(argc);
Tao Baoba9a42a2015-06-23 23:23:33 -0700385 for (int i = 0; i < argc; ++i) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700386 if (!Evaluate(state, argv[i], &paths[i])) {
387 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700388 }
389 }
390
391 bool recursive = (strcmp(name, "delete_recursive") == 0);
392
393 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700394 for (int i = 0; i < argc; ++i) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700395 if ((recursive ? dirUnlinkHierarchy(paths[i].c_str()) : unlink(paths[i].c_str())) == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700396 ++success;
Tianjie Xuaced5d92016-10-12 10:55:04 -0700397 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700398 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700399
Tao Bao0831d0b2016-11-03 23:25:04 -0700400 return StringValue(std::to_string(success));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700401}
402
Doug Zongker8edb00c2009-06-11 17:21:44 -0700403
Doug Zongker512536a2010-02-17 16:11:44 -0800404Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700405 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700406 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700407 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700408
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700409 std::vector<std::string> args;
410 if (!ReadArgs(state, argc, argv, &args)) {
411 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
412 }
413 const std::string& frac_str = args[0];
414 const std::string& sec_str = args[1];
415
416 double frac;
417 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
418 return ErrorAbort(state, kArgsParsingFailure,
419 "%s: failed to parse double in %s\n", name, frac_str.c_str());
420 }
Tao Baob15fd222015-09-24 11:10:51 -0700421 int sec;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700422 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
423 return ErrorAbort(state, kArgsParsingFailure,
424 "%s: failed to parse int in %s\n", name, sec_str.c_str());
425 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700426
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700427 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700428 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
429
Doug Zongker512536a2010-02-17 16:11:44 -0800430 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700431}
432
Doug Zongker512536a2010-02-17 16:11:44 -0800433Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700434 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700435 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700436 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700437
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700438 std::vector<std::string> args;
439 if (!ReadArgs(state, 1, argv, &args)) {
440 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
441 }
442 const std::string& frac_str = args[0];
443
444 double frac;
445 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
446 return ErrorAbort(state, kArgsParsingFailure,
447 "%s: failed to parse double in %s\n", name, frac_str.c_str());
448 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700449
450 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
451 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
452
Doug Zongker512536a2010-02-17 16:11:44 -0800453 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700454}
455
Doug Zongker8edb00c2009-06-11 17:21:44 -0700456// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800457Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700458 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700459 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700460 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700461 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700462
463 std::vector<std::string> args;
464 if (!ReadArgs(state, 2, argv, &args)) {
465 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
466 }
467 const std::string& zip_path = args[0];
468 const std::string& dest_path = args[1];
Doug Zongker9931f7f2009-06-10 14:11:53 -0700469
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700470 ZipArchiveHandle za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700471
472 // To create a consistent system image, never use the clock for timestamps.
473 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
474
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700475 bool success = ExtractPackageRecursive(za, zip_path, dest_path, &timestamp, sehandle);
476
Tianjie Xuaced5d92016-10-12 10:55:04 -0700477 return StringValue(success ? "t" : "");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700478}
479
Doug Zongker8edb00c2009-06-11 17:21:44 -0700480
481// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800482// or
483// package_extract_file(package_path)
484// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800485// function (the char* returned is actually a FileContents*).
486Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700487 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700488 if (argc < 1 || argc > 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700489 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800490 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700491 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700492 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700493
Doug Zongker5f875bf2014-08-22 14:53:43 -0700494 if (argc == 2) {
495 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800496
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700497 ZipArchiveHandle za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700498
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700499 std::vector<std::string> args;
500 if (!ReadArgs(state, 2, argv, &args)) {
501 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %d args", name,
502 argc);
503 }
504 const std::string& zip_path = args[0];
505 const std::string& dest_path = args[1];
Doug Zongker6aece332010-02-01 14:40:12 -0800506
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700507 ZipString zip_string_path(zip_path.c_str());
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700508 ZipEntry entry;
509 if (FindEntry(za, zip_string_path, &entry) != 0) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700510 printf("%s: no %s in package\n", name, zip_path.c_str());
511 return StringValue("");
Doug Zongker6aece332010-02-01 14:40:12 -0800512 }
513
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700514 int fd = TEMP_FAILURE_RETRY(ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC,
515 S_IRUSR | S_IWUSR));
516 if (fd == -1) {
517 printf("%s: can't open %s for write: %s\n", name, dest_path.c_str(), strerror(errno));
518 return StringValue("");
519 }
520 success = ExtractEntryToFile(za, &entry, fd);
521 if (ota_fsync(fd) == -1) {
522 printf("fsync of \"%s\" failed: %s\n", dest_path.c_str(), strerror(errno));
523 success = false;
524 }
525 if (ota_close(fd) == -1) {
526 printf("close of \"%s\" failed: %s\n", dest_path.c_str(), strerror(errno));
527 success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800528 }
Doug Zongker6aece332010-02-01 14:40:12 -0800529
Tianjie Xuaced5d92016-10-12 10:55:04 -0700530 return StringValue(success ? "t" : "");
Doug Zongker6aece332010-02-01 14:40:12 -0800531 } else {
532 // The one-argument version returns the contents of the file
533 // as the result.
534
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700535 std::vector<std::string> args;
536 if (!ReadArgs(state, 1, argv, &args)) {
537 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %d args", name,
538 argc);
539 }
540 const std::string& zip_path = args[0];
Yabin Cui64be2132016-02-03 18:16:02 -0800541
Tianjie Xuaced5d92016-10-12 10:55:04 -0700542 Value* v = new Value(VAL_INVALID, "");
Doug Zongker6aece332010-02-01 14:40:12 -0800543
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700544 ZipArchiveHandle za = ((UpdaterInfo*)(state->cookie))->package_zip;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700545 ZipString zip_string_path(zip_path.c_str());
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700546 ZipEntry entry;
547 if (FindEntry(za, zip_string_path, &entry) != 0) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700548 printf("%s: no %s in package\n", name, zip_path.c_str());
549 return v;
Doug Zongker6aece332010-02-01 14:40:12 -0800550 }
551
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700552 v->data.resize(entry.uncompressed_length);
553 if (ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&v->data[0]),
554 v->data.size()) != 0) {
555 printf("%s: faled to extract %zu bytes to memory\n", name, v->data.size());
556 } else {
557 success = true;
558 }
Doug Zongker6aece332010-02-01 14:40:12 -0800559
Doug Zongker6aece332010-02-01 14:40:12 -0800560 if (!success) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700561 v->data.clear();
562 } else {
563 v->type = VAL_BLOB;
Doug Zongker6aece332010-02-01 14:40:12 -0800564 }
Doug Zongker512536a2010-02-17 16:11:44 -0800565 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700566 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700567}
568
Doug Zongker9931f7f2009-06-10 14:11:53 -0700569// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700570// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800571Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700572 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700573 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700574 }
Tianjie Xuaced5d92016-10-12 10:55:04 -0700575 std::string target;
576 if (!Evaluate(state, argv[0], &target)) {
577 return nullptr;
578 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700579
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700580 std::vector<std::string> srcs;
581 if (!ReadArgs(state, argc-1, argv+1, &srcs)) {
582 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700583 }
584
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700585 int bad = 0;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700586 for (int i = 0; i < argc-1; ++i) {
587 if (unlink(srcs[i].c_str()) < 0) {
Doug Zongker60babf82009-09-18 15:11:24 -0700588 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700589 printf("%s: failed to remove %s: %s\n",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700590 name, srcs[i].c_str(), strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700591 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700592 }
593 }
Tianjie Xud75003d2016-11-04 11:31:29 -0700594 if (!make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700595 printf("%s: failed to symlink %s to %s: making parents failed\n",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700596 name, srcs[i].c_str(), target.c_str());
Doug Zongkera23075f2012-08-06 16:19:09 -0700597 ++bad;
598 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700599 if (symlink(target.c_str(), srcs[i].c_str()) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700600 printf("%s: failed to symlink %s to %s: %s\n",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700601 name, srcs[i].c_str(), target.c_str(), strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700602 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700603 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700604 }
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700605 if (bad) {
Tianjie Xu16255832016-04-30 11:49:59 -0700606 return ErrorAbort(state, kSymlinkFailure, "%s: some symlinks failed", name);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700607 }
Tianjie Xuaced5d92016-10-12 10:55:04 -0700608 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700609}
610
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700611struct perm_parsed_args {
612 bool has_uid;
613 uid_t uid;
614 bool has_gid;
615 gid_t gid;
616 bool has_mode;
617 mode_t mode;
618 bool has_fmode;
619 mode_t fmode;
620 bool has_dmode;
621 mode_t dmode;
622 bool has_selabel;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700623 const char* selabel;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700624 bool has_capabilities;
625 uint64_t capabilities;
626};
627
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700628static struct perm_parsed_args ParsePermArgs(State * state, int argc,
629 const std::vector<std::string>& args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700630 int i;
631 struct perm_parsed_args parsed;
632 int bad = 0;
633 static int max_warnings = 20;
634
635 memset(&parsed, 0, sizeof(parsed));
636
637 for (i = 1; i < argc; i += 2) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700638 if (args[i] == "uid") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700639 int64_t uid;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700640 if (sscanf(args[i+1].c_str(), "%" SCNd64, &uid) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700641 parsed.uid = uid;
642 parsed.has_uid = true;
643 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700644 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700645 bad++;
646 }
647 continue;
648 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700649 if (args[i] == "gid") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700650 int64_t gid;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700651 if (sscanf(args[i+1].c_str(), "%" SCNd64, &gid) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700652 parsed.gid = gid;
653 parsed.has_gid = true;
654 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700655 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700656 bad++;
657 }
658 continue;
659 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700660 if (args[i] == "mode") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700661 int32_t mode;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700662 if (sscanf(args[i+1].c_str(), "%" SCNi32, &mode) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700663 parsed.mode = mode;
664 parsed.has_mode = true;
665 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700666 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700667 bad++;
668 }
669 continue;
670 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700671 if (args[i] == "dmode") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700672 int32_t mode;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700673 if (sscanf(args[i+1].c_str(), "%" SCNi32, &mode) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700674 parsed.dmode = mode;
675 parsed.has_dmode = true;
676 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700677 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700678 bad++;
679 }
680 continue;
681 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700682 if (args[i] == "fmode") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700683 int32_t mode;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700684 if (sscanf(args[i+1].c_str(), "%" SCNi32, &mode) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700685 parsed.fmode = mode;
686 parsed.has_fmode = true;
687 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700688 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700689 bad++;
690 }
691 continue;
692 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700693 if (args[i] == "capabilities") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700694 int64_t capabilities;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700695 if (sscanf(args[i+1].c_str(), "%" SCNi64, &capabilities) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700696 parsed.capabilities = capabilities;
697 parsed.has_capabilities = true;
698 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700699 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700700 bad++;
701 }
702 continue;
703 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700704 if (args[i] == "selabel") {
705 if (!args[i+1].empty()) {
706 parsed.selabel = args[i+1].c_str();
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700707 parsed.has_selabel = true;
708 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700709 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700710 bad++;
711 }
712 continue;
713 }
714 if (max_warnings != 0) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700715 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700716 max_warnings--;
717 if (max_warnings == 0) {
718 printf("ParsedPermArgs: suppressing further warnings\n");
719 }
720 }
721 }
722 return parsed;
723}
724
725static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700726 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700727 const char* filename,
728 const struct stat *statptr,
729 struct perm_parsed_args parsed)
730{
731 int bad = 0;
732
Nick Kralevich68802412014-10-23 20:36:42 -0700733 if (parsed.has_selabel) {
734 if (lsetfilecon(filename, parsed.selabel) != 0) {
735 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
736 filename, parsed.selabel, strerror(errno));
737 bad++;
738 }
739 }
740
Nick Kraleviche4612512013-09-10 15:34:19 -0700741 /* ignore symlinks */
742 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700743 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700744 }
745
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700746 if (parsed.has_uid) {
747 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700748 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
749 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700750 bad++;
751 }
752 }
753
754 if (parsed.has_gid) {
755 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700756 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
757 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700758 bad++;
759 }
760 }
761
762 if (parsed.has_mode) {
763 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700764 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
765 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700766 bad++;
767 }
768 }
769
770 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
771 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700772 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
773 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700774 bad++;
775 }
776 }
777
778 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
779 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700780 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700781 filename, parsed.fmode, strerror(errno));
782 bad++;
783 }
784 }
785
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700786 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
787 if (parsed.capabilities == 0) {
788 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
789 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700790 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700791 filename, parsed.capabilities, strerror(errno));
792 bad++;
793 }
794 } else {
795 struct vfs_cap_data cap_data;
796 memset(&cap_data, 0, sizeof(cap_data));
797 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
798 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
799 cap_data.data[0].inheritable = 0;
800 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
801 cap_data.data[1].inheritable = 0;
802 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700803 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
804 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700805 bad++;
806 }
807 }
808 }
809
810 return bad;
811}
812
813// nftw doesn't allow us to pass along context, so we need to use
814// global variables. *sigh*
815static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700816static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700817
818static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
819 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700820 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700821}
822
823static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700824 if ((argc % 2) != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700825 return ErrorAbort(state, kArgsParsingFailure,
826 "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700827 }
828
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700829 std::vector<std::string> args;
830 if (!ReadArgs(state, argc, argv, &args)) {
831 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700832 }
833
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700834 struct stat sb;
835 if (lstat(args[0].c_str(), &sb) == -1) {
836 return ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s",
837 name, args[0].c_str(), strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700838 }
839
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700840 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
841 int bad = 0;
842 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700843
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700844 if (recursive) {
845 recursive_parsed_args = parsed;
846 recursive_state = state;
847 bad += nftw(args[0].c_str(), do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
848 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
849 recursive_state = NULL;
850 } else {
851 bad += ApplyParsedPerms(state, args[0].c_str(), &sb, parsed);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700852 }
853
854 if (bad > 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700855 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700856 }
857
Tianjie Xuaced5d92016-10-12 10:55:04 -0700858 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700859}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700860
Doug Zongker512536a2010-02-17 16:11:44 -0800861Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700862 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700863 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700864 }
Tianjie Xuaced5d92016-10-12 10:55:04 -0700865 std::string key;
866 if (!Evaluate(state, argv[0], &key)) {
867 return nullptr;
868 }
Elliott Hughescb220402016-09-23 15:30:55 -0700869 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700870
Tianjie Xuaced5d92016-10-12 10:55:04 -0700871 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700872}
873
874
Doug Zongker47cace92009-06-18 10:11:50 -0700875// file_getprop(file, key)
876//
877// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -0700878// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700879// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800880Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao51d516e2016-11-03 14:49:01 -0700881 if (argc != 2) {
882 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
883 }
884
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700885 std::vector<std::string> args;
886 if (!ReadArgs(state, 2, argv, &args)) {
887 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker47cace92009-06-18 10:11:50 -0700888 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700889 const std::string& filename = args[0];
890 const std::string& key = args[1];
Doug Zongker47cace92009-06-18 10:11:50 -0700891
892 struct stat st;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700893 if (stat(filename.c_str(), &st) < 0) {
894 return ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name,
895 filename.c_str(), strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700896 }
897
Tao Bao51d516e2016-11-03 14:49:01 -0700898 constexpr off_t MAX_FILE_GETPROP_SIZE = 65536;
Doug Zongker47cace92009-06-18 10:11:50 -0700899 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tao Bao51d516e2016-11-03 14:49:01 -0700900 return ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %lld)",
901 filename.c_str(), name, static_cast<long long>(MAX_FILE_GETPROP_SIZE));
Doug Zongker47cace92009-06-18 10:11:50 -0700902 }
903
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700904 std::string buffer(st.st_size, '\0');
905 FILE* f = ota_fopen(filename.c_str(), "rb");
906 if (f == nullptr) {
907 return ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name,
908 filename.c_str(), strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700909 }
910
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700911 if (ota_fread(&buffer[0], 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
Tianjie Xu84478e82016-05-23 11:42:40 -0700912 ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700913 name, static_cast<size_t>(st.st_size), filename.c_str());
Jed Estepa7b9a462015-12-15 16:04:53 -0800914 ota_fclose(f);
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700915 return nullptr;
Doug Zongker47cace92009-06-18 10:11:50 -0700916 }
Doug Zongker47cace92009-06-18 10:11:50 -0700917
Jed Estepa7b9a462015-12-15 16:04:53 -0800918 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -0700919
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700920 std::vector<std::string> lines = android::base::Split(buffer, "\n");
921 for (size_t i = 0; i < lines.size(); i++) {
922 std::string line = android::base::Trim(lines[i]);
Doug Zongker47cace92009-06-18 10:11:50 -0700923
924 // comment or blank line: skip to next line
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700925 if (line.empty() || line[0] == '#') {
926 continue;
927 }
928 size_t equal_pos = line.find('=');
929 if (equal_pos == std::string::npos) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700930 continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700931 }
932
933 // trim whitespace between key and '='
Tao Bao51d516e2016-11-03 14:49:01 -0700934 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -0700935
936 // not the key we're looking for
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700937 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700938
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700939 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
940 }
Doug Zongker47cace92009-06-18 10:11:50 -0700941
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700942 return StringValue("");
Doug Zongker47cace92009-06-18 10:11:50 -0700943}
944
Doug Zongker8edb00c2009-06-11 17:21:44 -0700945// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -0800946Value* ApplyPatchSpaceFn(const char* name, State* state,
947 int argc, Expr* argv[]) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700948 std::vector<std::string> args;
949 if (!ReadArgs(state, 1, argv, &args)) {
950 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800951 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700952 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800953
Tao Baob15fd222015-09-24 11:10:51 -0700954 size_t bytes;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700955 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
956 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n",
957 name, bytes_str.c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800958 }
959
Tianjie Xuaced5d92016-10-12 10:55:04 -0700960 return StringValue(CacheSizeCheck(bytes) ? "" : "t");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800961}
962
Doug Zongker52b40362014-02-10 15:30:30 -0800963// apply_patch(file, size, init_sha1, tgt_sha1, patch)
964
Doug Zongker512536a2010-02-17 16:11:44 -0800965Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800966 if (argc < 6 || (argc % 2) == 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700967 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an "
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700968 "even number, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700969 }
970
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700971 std::vector<std::string> args;
972 if (!ReadArgs(state, 4, argv, &args)) {
973 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700974 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700975 const std::string& source_filename = args[0];
976 const std::string& target_filename = args[1];
977 const std::string& target_sha1 = args[2];
978 const std::string& target_size_str = args[3];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700979
Tao Baob15fd222015-09-24 11:10:51 -0700980 size_t target_size;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700981 if (!android::base::ParseUint(target_size_str.c_str(), &target_size)) {
982 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count",
983 name, target_size_str.c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800984 }
985
986 int patchcount = (argc-4) / 2;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700987 std::vector<std::unique_ptr<Value>> arg_values;
988 if (!ReadValueArgs(state, argc-4, argv+4, &arg_values)) {
Yabin Cui64be2132016-02-03 18:16:02 -0800989 return nullptr;
990 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700991
Yabin Cui64be2132016-02-03 18:16:02 -0800992 for (int i = 0; i < patchcount; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700993 if (arg_values[i * 2]->type != VAL_STRING) {
994 return ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name,
995 i * 2);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800996 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700997 if (arg_values[i * 2 + 1]->type != VAL_BLOB) {
998 return ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name,
999 i * 2 + 1);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001000 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001001 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001002
Tianjie Xuaced5d92016-10-12 10:55:04 -07001003 std::vector<std::string> patch_sha_str;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001004 std::vector<std::unique_ptr<Value>> patches;
Yabin Cui64be2132016-02-03 18:16:02 -08001005 for (int i = 0; i < patchcount; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001006 patch_sha_str.push_back(arg_values[i * 2]->data);
1007 patches.push_back(std::move(arg_values[i * 2 + 1]));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001008 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001009
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001010 int result = applypatch(source_filename.c_str(), target_filename.c_str(),
1011 target_sha1.c_str(), target_size,
1012 patch_sha_str, patches, nullptr);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001013
Tianjie Xuaced5d92016-10-12 10:55:04 -07001014 return StringValue(result == 0 ? "t" : "");
Doug Zongkerc4351c72010-02-22 14:46:32 -08001015}
1016
1017// apply_patch_check(file, [sha1_1, ...])
1018Value* ApplyPatchCheckFn(const char* name, State* state,
1019 int argc, Expr* argv[]) {
1020 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001021 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %d",
Doug Zongkerc4351c72010-02-22 14:46:32 -08001022 name, argc);
1023 }
1024
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001025 std::vector<std::string> args;
1026 if (!ReadArgs(state, 1, argv, &args)) {
1027 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001028 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001029 const std::string& filename = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -08001030
Tianjie Xuaced5d92016-10-12 10:55:04 -07001031 std::vector<std::string> sha1s;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001032 if (!ReadArgs(state, argc - 1, argv + 1, &sha1s)) {
1033 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001034 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001035 int result = applypatch_check(filename.c_str(), sha1s);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001036
1037 return StringValue(result == 0 ? "t" : "");
Doug Zongker8edb00c2009-06-11 17:21:44 -07001038}
1039
Tao Bao1107d962015-09-09 17:16:55 -07001040// This is the updater side handler for ui_print() in edify script. Contents
1041// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001042Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001043 std::vector<std::string> args;
1044 if (!ReadArgs(state, argc, argv, &args)) {
1045 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001046 }
1047
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001048 std::string buffer = android::base::Join(args, "") + "\n";
Michael Runged4a63422014-10-22 19:48:41 -07001049 uiPrint(state, buffer);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001050 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001051}
1052
Doug Zongkerd0181b82011-10-19 10:51:12 -07001053Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1054 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001055 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001056 }
1057 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
Tianjie Xuaced5d92016-10-12 10:55:04 -07001058 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -07001059}
1060
Doug Zongker512536a2010-02-17 16:11:44 -08001061Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001062 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001063 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001064 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001065
1066 std::vector<std::string> args;
1067 if (!ReadArgs(state, argc, argv, &args)) {
1068 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001069 }
1070
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001071 char* args2[argc+1];
1072 for (int i = 0; i < argc; i++) {
1073 args2[i] = &args[i][0];
1074 }
1075 args2[argc] = nullptr;
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001076
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001077 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001078
1079 pid_t child = fork();
1080 if (child == 0) {
1081 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001082 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001083 _exit(1);
1084 }
1085 int status;
1086 waitpid(child, &status, 0);
1087 if (WIFEXITED(status)) {
1088 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001089 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001090 WEXITSTATUS(status));
1091 }
1092 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001093 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001094 WTERMSIG(status));
1095 }
1096
Tianjie Xuaced5d92016-10-12 10:55:04 -07001097 return StringValue(android::base::StringPrintf("%d", status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001098}
1099
Doug Zongker512536a2010-02-17 16:11:44 -08001100// sha1_check(data)
1101// to return the sha1 of the data (given in the format returned by
1102// read_file).
1103//
1104// sha1_check(data, sha1_hex, [sha1_hex, ...])
1105// returns the sha1 of the file if it matches any of the hex
1106// strings passed, or "" if it does not equal any of them.
1107//
1108Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1109 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001110 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongker512536a2010-02-17 16:11:44 -08001111 }
1112
Tianjie Xuaced5d92016-10-12 10:55:04 -07001113 std::vector<std::unique_ptr<Value>> args;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001114 if (!ReadValueArgs(state, argc, argv, &args)) {
1115 return nullptr;
Doug Zongker512536a2010-02-17 16:11:44 -08001116 }
1117
Tianjie Xuaced5d92016-10-12 10:55:04 -07001118 if (args[0]->type == VAL_INVALID) {
1119 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -08001120 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001121 uint8_t digest[SHA_DIGEST_LENGTH];
Tianjie Xuaced5d92016-10-12 10:55:04 -07001122 SHA1(reinterpret_cast<const uint8_t*>(args[0]->data.c_str()), args[0]->data.size(), digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001123
1124 if (argc == 1) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001125 return StringValue(print_sha1(digest));
Doug Zongker512536a2010-02-17 16:11:44 -08001126 }
1127
Tao Bao361342c2016-02-08 11:15:50 -08001128 for (int i = 1; i < argc; ++i) {
1129 uint8_t arg_digest[SHA_DIGEST_LENGTH];
Doug Zongker512536a2010-02-17 16:11:44 -08001130 if (args[i]->type != VAL_STRING) {
Tao Bao361342c2016-02-08 11:15:50 -08001131 printf("%s(): arg %d is not a string; skipping", name, i);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001132 } else if (ParseSha1(args[i]->data.c_str(), arg_digest) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001133 // Warn about bad args and skip them.
Tianjie Xuaced5d92016-10-12 10:55:04 -07001134 printf("%s(): error parsing \"%s\" as sha-1; skipping", name, args[i]->data.c_str());
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001135 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
Tao Bao361342c2016-02-08 11:15:50 -08001136 // Found a match.
1137 return args[i].release();
Doug Zongker512536a2010-02-17 16:11:44 -08001138 }
Doug Zongker512536a2010-02-17 16:11:44 -08001139 }
Tao Bao361342c2016-02-08 11:15:50 -08001140
1141 // Didn't match any of the hex strings; return false.
Tianjie Xuaced5d92016-10-12 10:55:04 -07001142 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -08001143}
1144
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001145// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001146// is actually a FileContents*).
1147Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1148 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001149 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker512536a2010-02-17 16:11:44 -08001150 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001151
1152 std::vector<std::string> args;
1153 if (!ReadArgs(state, 1, argv, &args)) {
1154 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1155 }
1156 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -08001157
Tianjie Xuaced5d92016-10-12 10:55:04 -07001158 Value* v = new Value(VAL_INVALID, "");
Doug Zongker512536a2010-02-17 16:11:44 -08001159
1160 FileContents fc;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001161 if (LoadFileContents(filename.c_str(), &fc) == 0) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001162 v->type = VAL_BLOB;
1163 v->data = std::string(fc.data.begin(), fc.data.end());
Doug Zongker512536a2010-02-17 16:11:44 -08001164 }
Doug Zongker512536a2010-02-17 16:11:44 -08001165 return v;
1166}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001167
Doug Zongkerc87bab12013-11-25 13:53:25 -08001168// Immediately reboot the device. Recovery is not finished normally,
1169// so if you reboot into recovery it will re-start applying the
1170// current package (because nothing has cleared the copy of the
1171// arguments stored in the BCB).
1172//
1173// The argument is the partition name passed to the android reboot
1174// property. It can be "recovery" to boot from the recovery
1175// partition, or "" (empty string) to boot from the regular boot
1176// partition.
1177Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1178 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001179 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001180 }
1181
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001182 std::vector<std::string> args;
1183 if (!ReadArgs(state, 2, argv, &args)) {
1184 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1185 }
1186 const std::string& filename = args[0];
1187 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001188
Doug Zongkerc87bab12013-11-25 13:53:25 -08001189 // zero out the 'command' field of the bootloader message.
Elliott Hughescb220402016-09-23 15:30:55 -07001190 char buffer[80];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001191 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001192 FILE* f = ota_fopen(filename.c_str(), "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001193 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001194 ota_fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1195 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001196
Elliott Hughescb220402016-09-23 15:30:55 -07001197 std::string reboot_cmd = "reboot,";
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001198 reboot_cmd += property;
Elliott Hughescb220402016-09-23 15:30:55 -07001199 android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001200
1201 sleep(5);
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001202 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001203}
1204
1205// Store a string value somewhere that future invocations of recovery
1206// can access it. This value is called the "stage" and can be used to
1207// drive packages that need to do reboots in the middle of
1208// installation and keep track of where they are in the multi-stage
1209// install.
1210//
1211// The first argument is the block device for the misc partition
1212// ("/misc" in the fstab), which is where this value is stored. The
1213// second argument is the string to store; it should not exceed 31
1214// bytes.
1215Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1216 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001217 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001218 }
1219
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001220 std::vector<std::string> args;
1221 if (!ReadArgs(state, 2, argv, &args)) {
1222 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1223 }
1224 const std::string& filename = args[0];
1225 std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001226
1227 // Store this value in the misc partition, immediately after the
1228 // bootloader message that the main recovery uses to save its
1229 // arguments in case of the device restarting midway through
1230 // package installation.
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001231 FILE* f = ota_fopen(filename.c_str(), "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001232 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001233 size_t to_write = stagestr.size();
Tianjie Xuaced5d92016-10-12 10:55:04 -07001234 size_t max_size = sizeof(((struct bootloader_message*)0)->stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001235 if (to_write > max_size) {
1236 to_write = max_size;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001237 stagestr = stagestr.substr(0, max_size-1);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001238 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001239 size_t status = ota_fwrite(stagestr.c_str(), to_write, 1, f);
Jed Estepa7b9a462015-12-15 16:04:53 -08001240 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001241
Tianjie Xuaced5d92016-10-12 10:55:04 -07001242 if (status != to_write) {
1243 return StringValue("");
1244 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001245 return StringValue(filename);
1246}
1247
1248// Return the value most recently saved with SetStageFn. The argument
1249// is the block device for the misc partition.
1250Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001251 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001252 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001253 }
1254
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001255 std::vector<std::string> args;
1256 if (!ReadArgs(state, 1, argv, &args)) {
1257 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1258 }
1259 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001260
1261 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001262 FILE* f = ota_fopen(filename.c_str(), "rb");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001263 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001264 size_t status = ota_fread(buffer, sizeof(buffer), 1, f);
Jed Estepa7b9a462015-12-15 16:04:53 -08001265 ota_fclose(f);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001266 if (status != sizeof(buffer)) {
1267 return StringValue("");
1268 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001269
Tianjie Xuaced5d92016-10-12 10:55:04 -07001270 buffer[sizeof(buffer)-1] = '\0';
1271 return StringValue(buffer);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001272}
1273
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001274Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1275 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001276 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001277 }
1278
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001279 std::vector<std::string> args;
1280 if (!ReadArgs(state, 2, argv, &args)) {
1281 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1282 }
1283 const std::string& filename = args[0];
1284 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001285
Tao Baob15fd222015-09-24 11:10:51 -07001286 size_t len;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001287 if (!android::base::ParseUint(len_str.c_str(), &len)) {
1288 return nullptr;
1289 }
1290 int fd = ota_open(filename.c_str(), O_WRONLY, 0644);
1291 // The wipe_block_device function in ext4_utils returns 0 on success and 1
1292 // for failure.
1293 int status = wipe_block_device(fd, len);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001294
Jed Estepa7b9a462015-12-15 16:04:53 -08001295 ota_close(fd);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001296
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001297 return StringValue((status == 0) ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001298}
1299
Doug Zongkerc704e062014-05-23 08:40:35 -07001300Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1301 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001302 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerc704e062014-05-23 08:40:35 -07001303 }
1304 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1305 fprintf(ui->cmd_pipe, "enable_reboot\n");
Tianjie Xuaced5d92016-10-12 10:55:04 -07001306 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -07001307}
1308
Michael Rungeacf47db2014-11-21 00:12:28 -08001309Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1310 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001311 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %d", name, argc);
Michael Rungeacf47db2014-11-21 00:12:28 -08001312 }
1313
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001314 std::vector<std::string> args;
1315 if (!ReadArgs(state, argc, argv, &args)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001316 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
Michael Rungeacf47db2014-11-21 00:12:28 -08001317 }
1318
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001319 char* args2[argc+1];
Michael Rungeacf47db2014-11-21 00:12:28 -08001320 // Tune2fs expects the program name as its args[0]
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001321 args2[0] = const_cast<char*>(name);
1322 if (args2[0] == nullptr) {
1323 return nullptr;
Michael Rungeacf47db2014-11-21 00:12:28 -08001324 }
Tao Baoba9a42a2015-06-23 23:23:33 -07001325 for (int i = 0; i < argc; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001326 args2[i + 1] = &args[i][0];
Michael Rungeacf47db2014-11-21 00:12:28 -08001327 }
Michael Rungeacf47db2014-11-21 00:12:28 -08001328
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001329 // tune2fs changes the file system parameters on an ext2 file system; it
1330 // returns 0 on success.
1331 int result = tune2fs_main(argc + 1, args2);
1332
Michael Rungeacf47db2014-11-21 00:12:28 -08001333 if (result != 0) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001334 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
Michael Rungeacf47db2014-11-21 00:12:28 -08001335 }
Tianjie Xuaced5d92016-10-12 10:55:04 -07001336 return StringValue("t");
Michael Rungeacf47db2014-11-21 00:12:28 -08001337}
1338
Doug Zongker9931f7f2009-06-10 14:11:53 -07001339void RegisterInstallFunctions() {
1340 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001341 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001342 RegisterFunction("unmount", UnmountFn);
1343 RegisterFunction("format", FormatFn);
1344 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001345 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001346 RegisterFunction("delete", DeleteFn);
1347 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001348 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1349 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001350 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001351
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001352 // Usage:
1353 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1354 // Example:
1355 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1356 RegisterFunction("set_metadata", SetMetadataFn);
1357
1358 // Usage:
1359 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1360 // Example:
1361 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1362 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1363
Doug Zongker8edb00c2009-06-11 17:21:44 -07001364 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001365 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001366
1367 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001368 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1369 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001370
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001371 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001372
Doug Zongker512536a2010-02-17 16:11:44 -08001373 RegisterFunction("read_file", ReadFileFn);
1374 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001375 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001376
Doug Zongkerd0181b82011-10-19 10:51:12 -07001377 RegisterFunction("wipe_cache", WipeCacheFn);
1378
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001379 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001380
1381 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001382
1383 RegisterFunction("reboot_now", RebootNowFn);
1384 RegisterFunction("get_stage", GetStageFn);
1385 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001386
1387 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001388 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001389}