blob: c7ecdb5c7865fd246ce359a64813aaabc7b55833 [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
Tao Baod0f30882016-11-03 23:52:01 -070042#include <android-base/file.h>
Tao Bao039f2da2016-11-22 16:29:50 -080043#include <android-base/logging.h>
Tianjie Xu5fe280a2016-10-17 18:15:20 -070044#include <android-base/parsedouble.h>
Tao Baod0f30882016-11-03 23:52:01 -070045#include <android-base/parseint.h>
Elliott Hughescb220402016-09-23 15:30:55 -070046#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080047#include <android-base/stringprintf.h>
Tao Baod0f30882016-11-03 23:52:01 -070048#include <android-base/strings.h>
Tao Bao0d3f84f2016-12-28 15:09:20 -080049#include <applypatch/applypatch.h>
50#include <bootloader_message/bootloader_message.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070051#include <cutils/android_reboot.h>
Tao Baode40ba52016-10-05 23:17:01 -070052#include <ext4_utils/make_ext4fs.h>
53#include <ext4_utils/wipe.h>
Tao Bao361342c2016-02-08 11:15:50 -080054#include <openssl/sha.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070055#include <selinux/label.h>
56#include <selinux/selinux.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070057#include <ziparchive/zip_archive.h>
Tao Bao1107d962015-09-09 17:16:55 -070058
Doug Zongker9931f7f2009-06-10 14:11:53 -070059#include "edify/expr.h"
Tianjie Xu16255832016-04-30 11:49:59 -070060#include "error_code.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070061#include "mounts.h"
Jed Estep39c1b5e2015-12-15 16:04:53 -080062#include "ota_io.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070063#include "otautil/DirUtil.h"
64#include "otautil/ZipUtil.h"
Tao Bao361342c2016-02-08 11:15:50 -080065#include "print_sha1.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080066#include "tune2fs.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070067#include "updater/updater.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070068
Tao Bao1107d962015-09-09 17:16:55 -070069// Send over the buffer to recovery though the command pipe.
70static void uiPrint(State* state, const std::string& buffer) {
Tao Bao039f2da2016-11-22 16:29:50 -080071 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070072
Tao Bao039f2da2016-11-22 16:29:50 -080073 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
74 // So skip sending empty strings to UI.
75 std::vector<std::string> lines = android::base::Split(buffer, "\n");
76 for (auto& line : lines) {
77 if (!line.empty()) {
78 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
Tao Bao1107d962015-09-09 17:16:55 -070079 }
Tao Bao039f2da2016-11-22 16:29:50 -080080 }
Tao Bao1107d962015-09-09 17:16:55 -070081
Tao Bao039f2da2016-11-22 16:29:50 -080082 // On the updater side, we need to dump the contents to stderr (which has
83 // been redirected to the log file). Because the recovery will only print
84 // the contents to screen when processing pipe command ui_print.
85 LOG(INFO) << buffer;
Michael Runged4a63422014-10-22 19:48:41 -070086}
87
Elliott Hughes83ce7552016-06-30 09:28:42 -070088void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) {
Tao Bao039f2da2016-11-22 16:29:50 -080089 std::string error_msg;
Tao Bao1107d962015-09-09 17:16:55 -070090
Tao Bao039f2da2016-11-22 16:29:50 -080091 va_list ap;
92 va_start(ap, format);
93 android::base::StringAppendV(&error_msg, format, ap);
94 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070095
Tao Bao039f2da2016-11-22 16:29:50 -080096 uiPrint(state, error_msg);
Michael Runged4a63422014-10-22 19:48:41 -070097}
98
Tianjie Xud75003d2016-11-04 11:31:29 -070099static bool is_dir(const std::string& dirpath) {
100 struct stat st;
101 return stat(dirpath.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
102}
103
Tao Bao0c7839a2016-10-10 15:48:37 -0700104// Create all parent directories of name, if necessary.
Tianjie Xud75003d2016-11-04 11:31:29 -0700105static bool make_parents(const std::string& name) {
Tao Bao039f2da2016-11-22 16:29:50 -0800106 size_t prev_end = 0;
107 while (prev_end < name.size()) {
108 size_t next_end = name.find('/', prev_end + 1);
109 if (next_end == std::string::npos) {
110 break;
Tao Bao0c7839a2016-10-10 15:48:37 -0700111 }
Tao Bao039f2da2016-11-22 16:29:50 -0800112 std::string dir_path = name.substr(0, next_end);
113 if (!is_dir(dir_path)) {
114 int result = mkdir(dir_path.c_str(), 0700);
115 if (result != 0) {
116 PLOG(ERROR) << "failed to mkdir " << dir_path << " when make parents for " << name;
117 return false;
118 }
119
120 LOG(INFO) << "created [" << dir_path << "]";
121 }
122 prev_end = next_end;
123 }
124 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[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800132 if (argc != 4 && argc != 5) {
133 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %d", name, argc);
134 }
135
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
146 if (argc == 5) {
147 mount_options = args[4];
148 }
149
150 if (fs_type.empty()) {
151 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
152 }
153 if (partition_type.empty()) {
154 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
155 name);
156 }
157 if (location.empty()) {
158 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
159 }
160 if (mount_point.empty()) {
161 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
162 name);
163 }
164
165 {
166 char* secontext = nullptr;
167
168 if (sehandle) {
169 selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755);
170 setfscreatecon(secontext);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700171 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700172
Tao Bao039f2da2016-11-22 16:29:50 -0800173 mkdir(mount_point.c_str(), 0755);
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700174
Tao Bao039f2da2016-11-22 16:29:50 -0800175 if (secontext) {
176 freecon(secontext);
177 setfscreatecon(nullptr);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700178 }
Tao Bao039f2da2016-11-22 16:29:50 -0800179 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700180
Tao Bao039f2da2016-11-22 16:29:50 -0800181 if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(),
182 MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) {
183 uiPrintf(state, "%s: failed to mount %s at %s: %s\n", name, location.c_str(),
184 mount_point.c_str(), strerror(errno));
185 return StringValue("");
186 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700187
Tao Bao039f2da2016-11-22 16:29:50 -0800188 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700189}
190
Doug Zongker8edb00c2009-06-11 17:21:44 -0700191
192// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800193Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800194 if (argc != 1) {
195 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
196 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700197
Tao Bao039f2da2016-11-22 16:29:50 -0800198 std::vector<std::string> args;
199 if (!ReadArgs(state, argc, argv, &args)) {
200 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
201 }
202 const std::string& mount_point = args[0];
203 if (mount_point.empty()) {
204 return ErrorAbort(state, kArgsParsingFailure,
205 "mount_point argument to unmount() can't be empty");
206 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700207
Tao Bao039f2da2016-11-22 16:29:50 -0800208 scan_mounted_volumes();
209 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
210 if (vol == nullptr) {
211 return StringValue("");
212 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700213
Tao Bao039f2da2016-11-22 16:29:50 -0800214 return StringValue(mount_point);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700215}
216
Doug Zongker512536a2010-02-17 16:11:44 -0800217Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800218 if (argc != 1) {
219 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
220 }
221 std::vector<std::string> args;
222 if (!ReadArgs(state, argc, argv, &args)) {
223 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
224 }
225 const std::string& mount_point = args[0];
226 if (mount_point.empty()) {
227 return ErrorAbort(state, kArgsParsingFailure,
228 "mount_point argument to unmount() can't be empty");
229 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700230
Tao Bao039f2da2016-11-22 16:29:50 -0800231 scan_mounted_volumes();
232 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
233 if (vol == nullptr) {
234 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point.c_str());
235 return nullptr;
236 } else {
237 int ret = unmount_mounted_volume(vol);
238 if (ret != 0) {
239 uiPrintf(state, "unmount of %s failed (%d): %s\n", mount_point.c_str(), ret, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700240 }
Tao Bao039f2da2016-11-22 16:29:50 -0800241 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700242
Tao Bao039f2da2016-11-22 16:29:50 -0800243 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700244}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700245
JP Abgrall37aedb32014-06-16 19:07:39 -0700246static int exec_cmd(const char* path, char* const argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800247 pid_t child;
248 if ((child = vfork()) == 0) {
249 execv(path, argv);
Tao Bao3da88012017-02-03 13:09:23 -0800250 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -0800251 }
252
253 int status;
254 waitpid(child, &status, 0);
255 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
256 LOG(ERROR) << path << " failed with status " << WEXITSTATUS(status);
257 }
258 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700259}
260
Stephen Smalley779701d2012-02-09 14:13:23 -0500261// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700262//
Tao Bao039f2da2016-11-22 16:29:50 -0800263// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
264// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700265// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800266// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700267// 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 -0800268Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800269 if (argc != 5) {
270 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %d", name, argc);
271 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500272
Tao Bao039f2da2016-11-22 16:29:50 -0800273 std::vector<std::string> args;
274 if (!ReadArgs(state, argc, argv, &args)) {
275 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
276 }
277 const std::string& fs_type = args[0];
278 const std::string& partition_type = args[1];
279 const std::string& location = args[2];
280 const std::string& fs_size = args[3];
281 const std::string& mount_point = args[4];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700282
Tao Bao039f2da2016-11-22 16:29:50 -0800283 if (fs_type.empty()) {
284 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
285 }
286 if (partition_type.empty()) {
287 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
288 name);
289 }
290 if (location.empty()) {
291 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
292 }
293 if (mount_point.empty()) {
294 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
295 name);
296 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700297
Tao Bao039f2da2016-11-22 16:29:50 -0800298 int64_t size;
299 if (!android::base::ParseInt(fs_size, &size)) {
300 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s\n", name,
301 fs_size.c_str());
302 }
303
304 if (fs_type == "ext4") {
305 int status = make_ext4fs(location.c_str(), size, mount_point.c_str(), sehandle);
306 if (status != 0) {
307 LOG(ERROR) << name << ": make_ext4fs failed (" << status << ") on " << location;
308 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700309 }
Tao Bao039f2da2016-11-22 16:29:50 -0800310 return StringValue(location);
311 } else if (fs_type == "f2fs") {
312 if (size < 0) {
313 LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size;
314 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700315 }
Tao Bao039f2da2016-11-22 16:29:50 -0800316 std::string num_sectors = std::to_string(size / 512);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700317
Tao Bao039f2da2016-11-22 16:29:50 -0800318 const char* f2fs_path = "/sbin/mkfs.f2fs";
319 const char* const f2fs_argv[] = { "mkfs.f2fs", "-t", "-d1", location.c_str(),
320 num_sectors.c_str(), nullptr };
321 int status = exec_cmd(f2fs_path, const_cast<char* const*>(f2fs_argv));
322 if (status != 0) {
323 LOG(ERROR) << name << ": mkfs.f2fs failed (" << status << ") on " << location;
324 return StringValue("");
325 }
326 return StringValue(location);
327 } else {
328 LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \""
329 << partition_type << "\"";
330 }
331
332 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700333}
334
Tao Baoa659d792016-11-03 23:25:04 -0700335// rename(src_name, dst_name)
336// Renames src_name to dst_name. It automatically creates the necessary directories for dst_name.
337// Example: rename("system/app/Hangouts/Hangouts.apk", "system/priv-app/Hangouts/Hangouts.apk")
Michael Rungece7ca712013-11-06 17:42:20 -0800338Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800339 if (argc != 2) {
340 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
341 }
Michael Rungece7ca712013-11-06 17:42:20 -0800342
Tao Bao039f2da2016-11-22 16:29:50 -0800343 std::vector<std::string> args;
344 if (!ReadArgs(state, argc, argv, &args)) {
345 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
346 }
347 const std::string& src_name = args[0];
348 const std::string& dst_name = args[1];
Michael Rungece7ca712013-11-06 17:42:20 -0800349
Tao Bao039f2da2016-11-22 16:29:50 -0800350 if (src_name.empty()) {
351 return ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", name);
352 }
353 if (dst_name.empty()) {
354 return ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", name);
355 }
356 if (!make_parents(dst_name)) {
357 return ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
358 dst_name.c_str(), strerror(errno));
359 } else if (access(dst_name.c_str(), F_OK) == 0 && access(src_name.c_str(), F_OK) != 0) {
360 // File was already moved
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700361 return StringValue(dst_name);
Tao Bao039f2da2016-11-22 16:29:50 -0800362 } else if (rename(src_name.c_str(), dst_name.c_str()) != 0) {
363 return ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
364 src_name.c_str(), dst_name.c_str(), strerror(errno));
365 }
366
367 return StringValue(dst_name);
Michael Rungece7ca712013-11-06 17:42:20 -0800368}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700369
Tao Bao0831d0b2016-11-03 23:25:04 -0700370// delete([filename, ...])
371// Deletes all the filenames listed. Returns the number of files successfully deleted.
372//
373// delete_recursive([dirname, ...])
374// Recursively deletes dirnames and all their contents. Returns the number of directories
375// successfully deleted.
Doug Zongker512536a2010-02-17 16:11:44 -0800376Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800377 std::vector<std::string> paths(argc);
378 for (int i = 0; i < argc; ++i) {
379 if (!Evaluate(state, argv[i], &paths[i])) {
380 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700381 }
Tao Bao039f2da2016-11-22 16:29:50 -0800382 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700383
Tao Bao039f2da2016-11-22 16:29:50 -0800384 bool recursive = (strcmp(name, "delete_recursive") == 0);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700385
Tao Bao039f2da2016-11-22 16:29:50 -0800386 int success = 0;
387 for (int i = 0; i < argc; ++i) {
388 if ((recursive ? dirUnlinkHierarchy(paths[i].c_str()) : unlink(paths[i].c_str())) == 0) {
389 ++success;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700390 }
Tao Bao039f2da2016-11-22 16:29:50 -0800391 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700392
Tao Bao039f2da2016-11-22 16:29:50 -0800393 return StringValue(std::to_string(success));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700394}
395
Doug Zongker8edb00c2009-06-11 17:21:44 -0700396
Doug Zongker512536a2010-02-17 16:11:44 -0800397Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800398 if (argc != 2) {
399 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
400 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700401
Tao Bao039f2da2016-11-22 16:29:50 -0800402 std::vector<std::string> args;
403 if (!ReadArgs(state, argc, argv, &args)) {
404 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
405 }
406 const std::string& frac_str = args[0];
407 const std::string& sec_str = args[1];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700408
Tao Bao039f2da2016-11-22 16:29:50 -0800409 double frac;
410 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
411 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s\n", name,
412 frac_str.c_str());
413 }
414 int sec;
415 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
416 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s\n", name,
417 sec_str.c_str());
418 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700419
Tao Bao039f2da2016-11-22 16:29:50 -0800420 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
421 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700422
Tao Bao039f2da2016-11-22 16:29:50 -0800423 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700424}
425
Doug Zongker512536a2010-02-17 16:11:44 -0800426Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800427 if (argc != 1) {
428 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
429 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700430
Tao Bao039f2da2016-11-22 16:29:50 -0800431 std::vector<std::string> args;
432 if (!ReadArgs(state, 1, argv, &args)) {
433 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
434 }
435 const std::string& frac_str = args[0];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700436
Tao Bao039f2da2016-11-22 16:29:50 -0800437 double frac;
438 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
439 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s\n", name,
440 frac_str.c_str());
441 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700442
Tao Bao039f2da2016-11-22 16:29:50 -0800443 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
444 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700445
Tao Bao039f2da2016-11-22 16:29:50 -0800446 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700447}
448
Tao Bao1036d362016-11-17 22:49:56 -0800449// package_extract_dir(package_dir, dest_dir)
450// Extracts all files from the package underneath package_dir and writes them to the
451// corresponding tree beneath dest_dir. Any existing files are overwritten.
452// Example: package_extract_dir("system", "/system")
453//
454// Note: package_dir needs to be a relative path; dest_dir needs to be an absolute path.
455Value* PackageExtractDirFn(const char* name, State* state, int argc, Expr* argv[]) {
456 if (argc != 2) {
457 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
458 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700459
Tao Bao1036d362016-11-17 22:49:56 -0800460 std::vector<std::string> args;
461 if (!ReadArgs(state, 2, argv, &args)) {
462 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
463 }
464 const std::string& zip_path = args[0];
465 const std::string& dest_path = args[1];
Doug Zongker9931f7f2009-06-10 14:11:53 -0700466
Tao Bao1036d362016-11-17 22:49:56 -0800467 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700468
Tao Bao1036d362016-11-17 22:49:56 -0800469 // To create a consistent system image, never use the clock for timestamps.
470 constexpr struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
Doug Zongker9931f7f2009-06-10 14:11:53 -0700471
Tao Bao1036d362016-11-17 22:49:56 -0800472 bool success = ExtractPackageRecursive(za, zip_path, dest_path, &timestamp, sehandle);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700473
Tao Bao1036d362016-11-17 22:49:56 -0800474 return StringValue(success ? "t" : "");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700475}
476
Tao Baoef0eb3b2016-11-14 21:29:52 -0800477// package_extract_file(package_file[, dest_file])
478// Extracts a single package_file from the update package and writes it to dest_file,
479// overwriting existing files if necessary. Without the dest_file argument, returns the
480// contents of the package file as a binary blob.
481Value* PackageExtractFileFn(const char* name, State* state, int argc, Expr* argv[]) {
482 if (argc < 1 || argc > 2) {
483 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %d", name, argc);
484 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700485
Tao Baoef0eb3b2016-11-14 21:29:52 -0800486 if (argc == 2) {
487 // The two-argument version extracts to a file.
488
489 std::vector<std::string> args;
490 if (!ReadArgs(state, 2, argv, &args)) {
491 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %d args", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700492 }
Tao Baoef0eb3b2016-11-14 21:29:52 -0800493 const std::string& zip_path = args[0];
494 const std::string& dest_path = args[1];
Doug Zongker43772d22014-06-09 14:13:19 -0700495
Tao Baoef0eb3b2016-11-14 21:29:52 -0800496 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
497 ZipString zip_string_path(zip_path.c_str());
498 ZipEntry entry;
499 if (FindEntry(za, zip_string_path, &entry) != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800500 LOG(ERROR) << name << ": no " << zip_path << " in package";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800501 return StringValue("");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700502 }
Tao Baoef0eb3b2016-11-14 21:29:52 -0800503
Tao Bao358c2ec2016-11-28 11:48:43 -0800504 unique_fd fd(TEMP_FAILURE_RETRY(
505 ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tao Baoef0eb3b2016-11-14 21:29:52 -0800506 if (fd == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800507 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800508 return StringValue("");
509 }
510
511 bool success = true;
512 int32_t ret = ExtractEntryToFile(za, &entry, fd);
513 if (ret != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800514 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
515 << entry.uncompressed_length << " bytes) to \"" << dest_path
516 << "\": " << ErrorCodeString(ret);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800517 success = false;
518 }
519 if (ota_fsync(fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800520 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800521 success = false;
522 }
523 if (ota_close(fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800524 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800525 success = false;
526 }
527
528 return StringValue(success ? "t" : "");
529 } else {
530 // The one-argument version returns the contents of the file as the result.
531
532 std::vector<std::string> args;
533 if (!ReadArgs(state, 1, argv, &args)) {
534 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %d args", name, argc);
535 }
536 const std::string& zip_path = args[0];
537
538 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
539 ZipString zip_string_path(zip_path.c_str());
540 ZipEntry entry;
541 if (FindEntry(za, zip_string_path, &entry) != 0) {
542 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
543 zip_path.c_str());
544 }
545
546 std::string buffer;
547 buffer.resize(entry.uncompressed_length);
548
549 int32_t ret = ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
550 if (ret != 0) {
551 return ErrorAbort(state, kPackageExtractFileFailure,
552 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
553 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
554 }
555
556 return new Value(VAL_BLOB, buffer);
557 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700558}
559
Tao Bao89929022016-11-08 20:51:31 -0800560// symlink(target, [src1, src2, ...])
561// Creates all sources as symlinks to target. It unlinks any previously existing src1, src2, etc
562// before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800563Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800564 if (argc == 0) {
565 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %d", name, argc);
566 }
567 std::string target;
568 if (!Evaluate(state, argv[0], &target)) {
569 return nullptr;
570 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700571
Tao Bao039f2da2016-11-22 16:29:50 -0800572 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);
575 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700576
Tao Bao039f2da2016-11-22 16:29:50 -0800577 size_t bad = 0;
578 for (const auto& src : srcs) {
579 if (unlink(src.c_str()) == -1 && errno != ENOENT) {
580 PLOG(ERROR) << name << ": failed to remove " << src;
581 ++bad;
582 } else if (!make_parents(src)) {
583 LOG(ERROR) << name << ": failed to symlink " << src << " to " << target
584 << ": making parents failed";
585 ++bad;
586 } else if (symlink(target.c_str(), src.c_str()) == -1) {
587 PLOG(ERROR) << name << ": failed to symlink " << src << " to " << target;
588 ++bad;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700589 }
Tao Bao039f2da2016-11-22 16:29:50 -0800590 }
591 if (bad != 0) {
592 return ErrorAbort(state, kSymlinkFailure, "%s: Failed to create %zu symlink(s)", name, bad);
593 }
594 return StringValue("t");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700595}
596
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700597struct perm_parsed_args {
Tao Bao039f2da2016-11-22 16:29:50 -0800598 bool has_uid;
599 uid_t uid;
600 bool has_gid;
601 gid_t gid;
602 bool has_mode;
603 mode_t mode;
604 bool has_fmode;
605 mode_t fmode;
606 bool has_dmode;
607 mode_t dmode;
608 bool has_selabel;
609 const char* selabel;
610 bool has_capabilities;
611 uint64_t capabilities;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700612};
613
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700614static struct perm_parsed_args ParsePermArgs(State * state, int argc,
615 const std::vector<std::string>& args) {
Tao Bao039f2da2016-11-22 16:29:50 -0800616 struct perm_parsed_args parsed;
617 int bad = 0;
618 static int max_warnings = 20;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700619
Tao Bao039f2da2016-11-22 16:29:50 -0800620 memset(&parsed, 0, sizeof(parsed));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700621
Tao Bao039f2da2016-11-22 16:29:50 -0800622 for (int i = 1; i < argc; i += 2) {
623 if (args[i] == "uid") {
624 int64_t uid;
625 if (sscanf(args[i + 1].c_str(), "%" SCNd64, &uid) == 1) {
626 parsed.uid = uid;
627 parsed.has_uid = true;
628 } else {
629 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1].c_str());
630 bad++;
631 }
632 continue;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700633 }
Tao Bao039f2da2016-11-22 16:29:50 -0800634 if (args[i] == "gid") {
635 int64_t gid;
636 if (sscanf(args[i + 1].c_str(), "%" SCNd64, &gid) == 1) {
637 parsed.gid = gid;
638 parsed.has_gid = true;
639 } else {
640 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1].c_str());
641 bad++;
642 }
643 continue;
644 }
645 if (args[i] == "mode") {
646 int32_t mode;
647 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
648 parsed.mode = mode;
649 parsed.has_mode = true;
650 } else {
651 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1].c_str());
652 bad++;
653 }
654 continue;
655 }
656 if (args[i] == "dmode") {
657 int32_t mode;
658 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
659 parsed.dmode = mode;
660 parsed.has_dmode = true;
661 } else {
662 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1].c_str());
663 bad++;
664 }
665 continue;
666 }
667 if (args[i] == "fmode") {
668 int32_t mode;
669 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
670 parsed.fmode = mode;
671 parsed.has_fmode = true;
672 } else {
673 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1].c_str());
674 bad++;
675 }
676 continue;
677 }
678 if (args[i] == "capabilities") {
679 int64_t capabilities;
680 if (sscanf(args[i + 1].c_str(), "%" SCNi64, &capabilities) == 1) {
681 parsed.capabilities = capabilities;
682 parsed.has_capabilities = true;
683 } else {
684 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1].c_str());
685 bad++;
686 }
687 continue;
688 }
689 if (args[i] == "selabel") {
690 if (!args[i + 1].empty()) {
691 parsed.selabel = args[i + 1].c_str();
692 parsed.has_selabel = true;
693 } else {
694 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1].c_str());
695 bad++;
696 }
697 continue;
698 }
699 if (max_warnings != 0) {
700 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i].c_str());
701 max_warnings--;
702 if (max_warnings == 0) {
703 LOG(INFO) << "ParsedPermArgs: suppressing further warnings";
704 }
705 }
706 }
707 return parsed;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700708}
709
Tao Bao039f2da2016-11-22 16:29:50 -0800710static int ApplyParsedPerms(State* state, const char* filename, const struct stat* statptr,
711 struct perm_parsed_args parsed) {
712 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700713
Tao Bao039f2da2016-11-22 16:29:50 -0800714 if (parsed.has_selabel) {
715 if (lsetfilecon(filename, parsed.selabel) != 0) {
716 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n", filename,
717 parsed.selabel, strerror(errno));
718 bad++;
Nick Kralevich68802412014-10-23 20:36:42 -0700719 }
Tao Bao039f2da2016-11-22 16:29:50 -0800720 }
Nick Kralevich68802412014-10-23 20:36:42 -0700721
Tao Bao039f2da2016-11-22 16:29:50 -0800722 /* ignore symlinks */
723 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700724 return bad;
Tao Bao039f2da2016-11-22 16:29:50 -0800725 }
726
727 if (parsed.has_uid) {
728 if (chown(filename, parsed.uid, -1) < 0) {
729 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n", filename, parsed.uid,
730 strerror(errno));
731 bad++;
732 }
733 }
734
735 if (parsed.has_gid) {
736 if (chown(filename, -1, parsed.gid) < 0) {
737 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n", filename, parsed.gid,
738 strerror(errno));
739 bad++;
740 }
741 }
742
743 if (parsed.has_mode) {
744 if (chmod(filename, parsed.mode) < 0) {
745 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.mode,
746 strerror(errno));
747 bad++;
748 }
749 }
750
751 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
752 if (chmod(filename, parsed.dmode) < 0) {
753 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.dmode,
754 strerror(errno));
755 bad++;
756 }
757 }
758
759 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
760 if (chmod(filename, parsed.fmode) < 0) {
761 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.fmode,
762 strerror(errno));
763 bad++;
764 }
765 }
766
767 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
768 if (parsed.capabilities == 0) {
769 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
770 // Report failure unless it's ENODATA (attribute not set)
771 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n", filename,
772 parsed.capabilities, strerror(errno));
773 bad++;
774 }
775 } else {
776 struct vfs_cap_data cap_data;
777 memset(&cap_data, 0, sizeof(cap_data));
778 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
779 cap_data.data[0].permitted = (uint32_t)(parsed.capabilities & 0xffffffff);
780 cap_data.data[0].inheritable = 0;
781 cap_data.data[1].permitted = (uint32_t)(parsed.capabilities >> 32);
782 cap_data.data[1].inheritable = 0;
783 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
784 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n", filename,
785 parsed.capabilities, strerror(errno));
786 bad++;
787 }
788 }
789 }
790
791 return bad;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700792}
793
794// nftw doesn't allow us to pass along context, so we need to use
795// global variables. *sigh*
796static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700797static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700798
Tao Bao039f2da2016-11-22 16:29:50 -0800799static int do_SetMetadataRecursive(const char* filename, const struct stat* statptr, int fileflags,
800 struct FTW* pfwt) {
801 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700802}
803
804static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800805 if ((argc % 2) != 1) {
806 return ErrorAbort(state, kArgsParsingFailure, "%s() expects an odd number of arguments, got %d",
807 name, argc);
808 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700809
Tao Bao039f2da2016-11-22 16:29:50 -0800810 std::vector<std::string> args;
811 if (!ReadArgs(state, argc, argv, &args)) {
812 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
813 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700814
Tao Bao039f2da2016-11-22 16:29:50 -0800815 struct stat sb;
816 if (lstat(args[0].c_str(), &sb) == -1) {
817 return ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s", name,
818 args[0].c_str(), strerror(errno));
819 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700820
Tao Bao039f2da2016-11-22 16:29:50 -0800821 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
822 int bad = 0;
823 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700824
Tao Bao039f2da2016-11-22 16:29:50 -0800825 if (recursive) {
826 recursive_parsed_args = parsed;
827 recursive_state = state;
828 bad += nftw(args[0].c_str(), do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
829 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
830 recursive_state = NULL;
831 } else {
832 bad += ApplyParsedPerms(state, args[0].c_str(), &sb, parsed);
833 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700834
Tao Bao039f2da2016-11-22 16:29:50 -0800835 if (bad > 0) {
836 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
837 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700838
Tao Bao039f2da2016-11-22 16:29:50 -0800839 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700840}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700841
Doug Zongker512536a2010-02-17 16:11:44 -0800842Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800843 if (argc != 1) {
844 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
845 }
846 std::string key;
847 if (!Evaluate(state, argv[0], &key)) {
848 return nullptr;
849 }
850 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700851
Tao Bao039f2da2016-11-22 16:29:50 -0800852 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700853}
854
Doug Zongker47cace92009-06-18 10:11:50 -0700855// file_getprop(file, key)
856//
857// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -0700858// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700859// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800860Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800861 if (argc != 2) {
862 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
863 }
864
865 std::vector<std::string> args;
866 if (!ReadArgs(state, 2, argv, &args)) {
867 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
868 }
869 const std::string& filename = args[0];
870 const std::string& key = args[1];
871
872 struct stat st;
873 if (stat(filename.c_str(), &st) < 0) {
874 return ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name,
875 filename.c_str(), strerror(errno));
876 }
877
878 constexpr off_t MAX_FILE_GETPROP_SIZE = 65536;
879 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
880 return ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %lld)",
881 filename.c_str(), name, static_cast<long long>(MAX_FILE_GETPROP_SIZE));
882 }
883
884 std::string buffer(st.st_size, '\0');
885 unique_file f(ota_fopen(filename.c_str(), "rb"));
886 if (f == nullptr) {
887 return ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename.c_str(),
888 strerror(errno));
889 }
890
891 if (ota_fread(&buffer[0], 1, st.st_size, f.get()) != static_cast<size_t>(st.st_size)) {
892 ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s", name,
893 static_cast<size_t>(st.st_size), filename.c_str());
894 return nullptr;
895 }
896
897 ota_fclose(f);
898
899 std::vector<std::string> lines = android::base::Split(buffer, "\n");
900 for (size_t i = 0; i < lines.size(); i++) {
901 std::string line = android::base::Trim(lines[i]);
902
903 // comment or blank line: skip to next line
904 if (line.empty() || line[0] == '#') {
905 continue;
906 }
907 size_t equal_pos = line.find('=');
908 if (equal_pos == std::string::npos) {
909 continue;
Tao Bao51d516e2016-11-03 14:49:01 -0700910 }
911
Tao Bao358c2ec2016-11-28 11:48:43 -0800912 // trim whitespace between key and '='
913 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -0700914
Tao Bao358c2ec2016-11-28 11:48:43 -0800915 // not the key we're looking for
916 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700917
Tao Bao358c2ec2016-11-28 11:48:43 -0800918 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
919 }
Doug Zongker47cace92009-06-18 10:11:50 -0700920
Tao Bao358c2ec2016-11-28 11:48:43 -0800921 return StringValue("");
Doug Zongker47cace92009-06-18 10:11:50 -0700922}
923
Doug Zongker8edb00c2009-06-11 17:21:44 -0700924// apply_patch_space(bytes)
Tao Bao039f2da2016-11-22 16:29:50 -0800925Value* ApplyPatchSpaceFn(const char* name, State* state, int argc, Expr* argv[]) {
926 std::vector<std::string> args;
927 if (!ReadArgs(state, 1, argv, &args)) {
928 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
929 }
930 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800931
Tao Bao039f2da2016-11-22 16:29:50 -0800932 size_t bytes;
933 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
934 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n",
935 name, bytes_str.c_str());
936 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800937
Tao Bao039f2da2016-11-22 16:29:50 -0800938 return StringValue(CacheSizeCheck(bytes) ? "" : "t");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800939}
940
Tao Bao039f2da2016-11-22 16:29:50 -0800941// apply_patch(src_file, tgt_file, tgt_sha1, tgt_size, patch1_sha1, patch1_blob, [...])
942// Applies a binary patch to the src_file to produce the tgt_file. If the desired target is the
943// same as the source, pass "-" for tgt_file. tgt_sha1 and tgt_size are the expected final SHA1
944// hash and size of the target file. The remaining arguments must come in pairs: a SHA1 hash (a
945// 40-character hex string) and a blob. The blob is the patch to be applied when the source
946// file's current contents have the given SHA1.
947//
948// The patching is done in a safe manner that guarantees the target file either has the desired
949// SHA1 hash and size, or it is untouched -- it will not be left in an unrecoverable intermediate
950// state. If the process is interrupted during patching, the target file may be in an intermediate
951// state; a copy exists in the cache partition so restarting the update can successfully update
952// the file.
Doug Zongker512536a2010-02-17 16:11:44 -0800953Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800954 if (argc < 6 || (argc % 2) == 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700955 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an "
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700956 "even number, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700957 }
958
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700959 std::vector<std::string> args;
960 if (!ReadArgs(state, 4, argv, &args)) {
961 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700962 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700963 const std::string& source_filename = args[0];
964 const std::string& target_filename = args[1];
965 const std::string& target_sha1 = args[2];
966 const std::string& target_size_str = args[3];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700967
Tao Baob15fd222015-09-24 11:10:51 -0700968 size_t target_size;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700969 if (!android::base::ParseUint(target_size_str.c_str(), &target_size)) {
970 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count",
971 name, target_size_str.c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800972 }
973
974 int patchcount = (argc-4) / 2;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700975 std::vector<std::unique_ptr<Value>> arg_values;
976 if (!ReadValueArgs(state, argc-4, argv+4, &arg_values)) {
Yabin Cui64be2132016-02-03 18:16:02 -0800977 return nullptr;
978 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700979
Yabin Cui64be2132016-02-03 18:16:02 -0800980 for (int i = 0; i < patchcount; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700981 if (arg_values[i * 2]->type != VAL_STRING) {
982 return ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name,
983 i * 2);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800984 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700985 if (arg_values[i * 2 + 1]->type != VAL_BLOB) {
986 return ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name,
987 i * 2 + 1);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800988 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700989 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700990
Tianjie Xuaced5d92016-10-12 10:55:04 -0700991 std::vector<std::string> patch_sha_str;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700992 std::vector<std::unique_ptr<Value>> patches;
Yabin Cui64be2132016-02-03 18:16:02 -0800993 for (int i = 0; i < patchcount; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700994 patch_sha_str.push_back(arg_values[i * 2]->data);
995 patches.push_back(std::move(arg_values[i * 2 + 1]));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700996 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800997
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700998 int result = applypatch(source_filename.c_str(), target_filename.c_str(),
999 target_sha1.c_str(), target_size,
1000 patch_sha_str, patches, nullptr);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001001
Tianjie Xuaced5d92016-10-12 10:55:04 -07001002 return StringValue(result == 0 ? "t" : "");
Doug Zongkerc4351c72010-02-22 14:46:32 -08001003}
1004
Tao Bao039f2da2016-11-22 16:29:50 -08001005// apply_patch_check(filename, [sha1, ...])
1006// Returns true if the contents of filename or the temporary copy in the cache partition (if
1007// present) have a SHA-1 checksum equal to one of the given sha1 values. sha1 values are
1008// specified as 40 hex digits. This function differs from sha1_check(read_file(filename),
1009// sha1 [, ...]) in that it knows to check the cache partition copy, so apply_patch_check() will
1010// succeed even if the file was corrupted by an interrupted apply_patch() update.
1011Value* ApplyPatchCheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1012 if (argc < 1) {
1013 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %d", name,
1014 argc);
1015 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001016
Tao Bao039f2da2016-11-22 16:29:50 -08001017 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);
1020 }
1021 const std::string& filename = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -08001022
Tao Bao039f2da2016-11-22 16:29:50 -08001023 std::vector<std::string> sha1s;
1024 if (!ReadArgs(state, argc - 1, argv + 1, &sha1s)) {
1025 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1026 }
1027 int result = applypatch_check(filename.c_str(), sha1s);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001028
Tao Bao039f2da2016-11-22 16:29:50 -08001029 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[]) {
Tao Bao039f2da2016-11-22 16:29:50 -08001035 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);
1038 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001039
Tao Bao039f2da2016-11-22 16:29:50 -08001040 std::string buffer = android::base::Join(args, "") + "\n";
1041 uiPrint(state, buffer);
1042 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[]) {
Tao Bao039f2da2016-11-22 16:29:50 -08001046 if (argc != 0) {
1047 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
1048 }
1049 fprintf(static_cast<UpdaterInfo*>(state->cookie)->cmd_pipe, "wipe_cache\n");
1050 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[]) {
Tao Bao039f2da2016-11-22 16:29:50 -08001054 if (argc < 1) {
1055 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
1056 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001057
Tao Bao039f2da2016-11-22 16:29:50 -08001058 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);
1061 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001062
Tao Bao039f2da2016-11-22 16:29:50 -08001063 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
Tao Bao039f2da2016-11-22 16:29:50 -08001069 LOG(INFO) << "about to run program [" << args2[0] << "] with " << argc << " args";
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001070
Tao Bao039f2da2016-11-22 16:29:50 -08001071 pid_t child = fork();
1072 if (child == 0) {
1073 execv(args2[0], args2);
1074 PLOG(ERROR) << "run_program: execv failed";
Tao Bao3da88012017-02-03 13:09:23 -08001075 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -08001076 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001077
Tao Bao039f2da2016-11-22 16:29:50 -08001078 int status;
1079 waitpid(child, &status, 0);
1080 if (WIFEXITED(status)) {
1081 if (WEXITSTATUS(status) != 0) {
1082 LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status);
1083 }
1084 } else if (WIFSIGNALED(status)) {
1085 LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status);
1086 }
1087
1088 return StringValue(std::to_string(status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001089}
1090
Doug Zongker512536a2010-02-17 16:11:44 -08001091// sha1_check(data)
1092// to return the sha1 of the data (given in the format returned by
1093// read_file).
1094//
1095// sha1_check(data, sha1_hex, [sha1_hex, ...])
1096// returns the sha1 of the file if it matches any of the hex
1097// strings passed, or "" if it does not equal any of them.
1098//
1099Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -08001100 if (argc < 1) {
1101 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
1102 }
Doug Zongker512536a2010-02-17 16:11:44 -08001103
Tao Bao039f2da2016-11-22 16:29:50 -08001104 std::vector<std::unique_ptr<Value>> args;
1105 if (!ReadValueArgs(state, argc, argv, &args)) {
1106 return nullptr;
1107 }
Doug Zongker512536a2010-02-17 16:11:44 -08001108
Tao Bao039f2da2016-11-22 16:29:50 -08001109 if (args[0]->type == VAL_INVALID) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001110 return StringValue("");
Tao Bao039f2da2016-11-22 16:29:50 -08001111 }
1112 uint8_t digest[SHA_DIGEST_LENGTH];
1113 SHA1(reinterpret_cast<const uint8_t*>(args[0]->data.c_str()), args[0]->data.size(), digest);
1114
1115 if (argc == 1) {
1116 return StringValue(print_sha1(digest));
1117 }
1118
1119 for (int i = 1; i < argc; ++i) {
1120 uint8_t arg_digest[SHA_DIGEST_LENGTH];
1121 if (args[i]->type != VAL_STRING) {
1122 LOG(ERROR) << name << "(): arg " << i << " is not a string; skipping";
1123 } else if (ParseSha1(args[i]->data.c_str(), arg_digest) != 0) {
1124 // Warn about bad args and skip them.
1125 LOG(ERROR) << name << "(): error parsing \"" << args[i]->data << "\" as sha-1; skipping";
1126 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
1127 // Found a match.
1128 return args[i].release();
1129 }
1130 }
1131
1132 // Didn't match any of the hex strings; return false.
1133 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -08001134}
1135
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001136// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001137// is actually a FileContents*).
1138Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -08001139 if (argc != 1) {
1140 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
1141 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001142
Tao Bao039f2da2016-11-22 16:29:50 -08001143 std::vector<std::string> args;
1144 if (!ReadArgs(state, 1, argv, &args)) {
1145 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1146 }
1147 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -08001148
Tao Bao039f2da2016-11-22 16:29:50 -08001149 Value* v = new Value(VAL_INVALID, "");
Doug Zongker512536a2010-02-17 16:11:44 -08001150
Tao Bao039f2da2016-11-22 16:29:50 -08001151 FileContents fc;
1152 if (LoadFileContents(filename.c_str(), &fc) == 0) {
1153 v->type = VAL_BLOB;
1154 v->data = std::string(fc.data.begin(), fc.data.end());
1155 }
1156 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001157}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001158
Tao Baod0f30882016-11-03 23:52:01 -07001159// write_value(value, filename)
1160// Writes 'value' to 'filename'.
1161// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
1162Value* WriteValueFn(const char* name, State* state, int argc, Expr* argv[]) {
1163 if (argc != 2) {
1164 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
1165 }
1166
1167 std::vector<std::string> args;
1168 if (!ReadArgs(state, 2, argv, &args)) {
1169 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1170 }
1171
1172 const std::string& filename = args[1];
1173 if (filename.empty()) {
1174 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
1175 }
1176
1177 const std::string& value = args[0];
1178 if (!android::base::WriteStringToFile(value, filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001179 PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\"";
Tao Baod0f30882016-11-03 23:52:01 -07001180 return StringValue("");
1181 } else {
1182 return StringValue("t");
1183 }
1184}
1185
Doug Zongkerc87bab12013-11-25 13:53:25 -08001186// Immediately reboot the device. Recovery is not finished normally,
1187// so if you reboot into recovery it will re-start applying the
1188// current package (because nothing has cleared the copy of the
1189// arguments stored in the BCB).
1190//
1191// The argument is the partition name passed to the android reboot
1192// property. It can be "recovery" to boot from the recovery
1193// partition, or "" (empty string) to boot from the regular boot
1194// partition.
1195Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001196 if (argc != 2) {
1197 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
1198 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001199
Tao Baobedf5fc2016-11-18 12:01:26 -08001200 std::vector<std::string> args;
1201 if (!ReadArgs(state, 2, argv, &args)) {
1202 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1203 }
1204 const std::string& filename = args[0];
1205 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001206
Tao Baobedf5fc2016-11-18 12:01:26 -08001207 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
1208 bootloader_message boot;
1209 std::string err;
1210 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001211 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001212 return StringValue("");
1213 }
1214 memset(boot.command, 0, sizeof(boot.command));
1215 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001216 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001217 return StringValue("");
1218 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001219
Tao Baobedf5fc2016-11-18 12:01:26 -08001220 const std::string reboot_cmd = "reboot," + property;
1221 android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001222
Tao Baobedf5fc2016-11-18 12:01:26 -08001223 sleep(5);
1224 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001225}
1226
1227// Store a string value somewhere that future invocations of recovery
1228// can access it. This value is called the "stage" and can be used to
1229// drive packages that need to do reboots in the middle of
1230// installation and keep track of where they are in the multi-stage
1231// install.
1232//
1233// The first argument is the block device for the misc partition
1234// ("/misc" in the fstab), which is where this value is stored. The
1235// second argument is the string to store; it should not exceed 31
1236// bytes.
1237Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001238 if (argc != 2) {
1239 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
1240 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001241
Tao Baobedf5fc2016-11-18 12:01:26 -08001242 std::vector<std::string> args;
1243 if (!ReadArgs(state, 2, argv, &args)) {
1244 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1245 }
1246 const std::string& filename = args[0];
1247 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001248
Tao Baobedf5fc2016-11-18 12:01:26 -08001249 // Store this value in the misc partition, immediately after the
1250 // bootloader message that the main recovery uses to save its
1251 // arguments in case of the device restarting midway through
1252 // package installation.
1253 bootloader_message boot;
1254 std::string err;
1255 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001256 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001257 return StringValue("");
1258 }
1259 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
1260 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001261 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001262 return StringValue("");
1263 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001264
Tao Baobedf5fc2016-11-18 12:01:26 -08001265 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001266}
1267
1268// Return the value most recently saved with SetStageFn. The argument
1269// is the block device for the misc partition.
1270Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001271 if (argc != 1) {
1272 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
1273 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001274
Tao Baobedf5fc2016-11-18 12:01:26 -08001275 std::vector<std::string> args;
1276 if (!ReadArgs(state, 1, argv, &args)) {
1277 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1278 }
1279 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001280
Tao Baobedf5fc2016-11-18 12:01:26 -08001281 bootloader_message boot;
1282 std::string err;
1283 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001284 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001285 return StringValue("");
1286 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001287
Tao Baobedf5fc2016-11-18 12:01:26 -08001288 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001289}
1290
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001291Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao358c2ec2016-11-28 11:48:43 -08001292 if (argc != 2) {
1293 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
1294 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001295
Tao Bao358c2ec2016-11-28 11:48:43 -08001296 std::vector<std::string> args;
1297 if (!ReadArgs(state, 2, argv, &args)) {
1298 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1299 }
1300 const std::string& filename = args[0];
1301 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001302
Tao Bao358c2ec2016-11-28 11:48:43 -08001303 size_t len;
1304 if (!android::base::ParseUint(len_str.c_str(), &len)) {
1305 return nullptr;
1306 }
1307 unique_fd fd(ota_open(filename.c_str(), O_WRONLY, 0644));
1308 // The wipe_block_device function in ext4_utils returns 0 on success and 1
1309 // for failure.
1310 int status = wipe_block_device(fd, len);
1311 return StringValue((status == 0) ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001312}
1313
Doug Zongkerc704e062014-05-23 08:40:35 -07001314Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -08001315 if (argc != 0) {
1316 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
1317 }
1318 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
1319 fprintf(ui->cmd_pipe, "enable_reboot\n");
1320 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -07001321}
1322
Michael Rungeacf47db2014-11-21 00:12:28 -08001323Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -08001324 if (argc == 0) {
1325 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %d", name, argc);
1326 }
Michael Rungeacf47db2014-11-21 00:12:28 -08001327
Tao Bao039f2da2016-11-22 16:29:50 -08001328 std::vector<std::string> args;
1329 if (!ReadArgs(state, argc, argv, &args)) {
1330 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
1331 }
Michael Rungeacf47db2014-11-21 00:12:28 -08001332
Tao Bao039f2da2016-11-22 16:29:50 -08001333 char* args2[argc + 1];
1334 // Tune2fs expects the program name as its args[0]
1335 args2[0] = const_cast<char*>(name);
1336 if (args2[0] == nullptr) {
1337 return nullptr;
1338 }
1339 for (int i = 0; i < argc; ++i) {
1340 args2[i + 1] = &args[i][0];
1341 }
Michael Rungeacf47db2014-11-21 00:12:28 -08001342
Tao Bao039f2da2016-11-22 16:29:50 -08001343 // tune2fs changes the file system parameters on an ext2 file system; it
1344 // returns 0 on success.
1345 int result = tune2fs_main(argc + 1, args2);
1346 if (result != 0) {
1347 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
1348 }
1349 return StringValue("t");
Michael Rungeacf47db2014-11-21 00:12:28 -08001350}
1351
Doug Zongker9931f7f2009-06-10 14:11:53 -07001352void RegisterInstallFunctions() {
Tao Bao039f2da2016-11-22 16:29:50 -08001353 RegisterFunction("mount", MountFn);
1354 RegisterFunction("is_mounted", IsMountedFn);
1355 RegisterFunction("unmount", UnmountFn);
1356 RegisterFunction("format", FormatFn);
1357 RegisterFunction("show_progress", ShowProgressFn);
1358 RegisterFunction("set_progress", SetProgressFn);
1359 RegisterFunction("delete", DeleteFn);
1360 RegisterFunction("delete_recursive", DeleteFn);
1361 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1362 RegisterFunction("package_extract_file", PackageExtractFileFn);
1363 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001364
Tao Bao039f2da2016-11-22 16:29:50 -08001365 // Usage:
1366 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1367 // Example:
1368 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel",
1369 // "u:object_r:system_file:s0", "capabilities", 0x0);
1370 RegisterFunction("set_metadata", SetMetadataFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001371
Tao Bao039f2da2016-11-22 16:29:50 -08001372 // Usage:
1373 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1374 // Example:
1375 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755,
1376 // "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1377 RegisterFunction("set_metadata_recursive", SetMetadataFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001378
Tao Bao039f2da2016-11-22 16:29:50 -08001379 RegisterFunction("getprop", GetPropFn);
1380 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001381
Tao Bao039f2da2016-11-22 16:29:50 -08001382 RegisterFunction("apply_patch", ApplyPatchFn);
1383 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1384 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001385
Tao Bao039f2da2016-11-22 16:29:50 -08001386 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001387
Tao Bao039f2da2016-11-22 16:29:50 -08001388 RegisterFunction("read_file", ReadFileFn);
1389 RegisterFunction("sha1_check", Sha1CheckFn);
1390 RegisterFunction("rename", RenameFn);
1391 RegisterFunction("write_value", WriteValueFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001392
Tao Bao039f2da2016-11-22 16:29:50 -08001393 RegisterFunction("wipe_cache", WipeCacheFn);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001394
Tao Bao039f2da2016-11-22 16:29:50 -08001395 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001396
Tao Bao039f2da2016-11-22 16:29:50 -08001397 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001398
Tao Bao039f2da2016-11-22 16:29:50 -08001399 RegisterFunction("reboot_now", RebootNowFn);
1400 RegisterFunction("get_stage", GetStageFn);
1401 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001402
Tao Bao039f2da2016-11-22 16:29:50 -08001403 RegisterFunction("enable_reboot", EnableRebootFn);
1404 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001405}