blob: ab8d08a43ce0246a28b7f955fa4d2a15e64ccca5 [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)
Tianjie Xuc4447322017-03-06 14:44:59 -0800129
Doug Zongker3d177d02010-07-01 09:18:44 -0700130// fs_type="ext4" partition_type="EMMC" location=device
Tianjie Xuc4447322017-03-06 14:44:59 -0800131Value* MountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
132 if (argv.size() != 4 && argv.size() != 5) {
133 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %zu", name,
134 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800135 }
136
137 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800138 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800139 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
140 }
141 const std::string& fs_type = args[0];
142 const std::string& partition_type = args[1];
143 const std::string& location = args[2];
144 const std::string& mount_point = args[3];
145 std::string mount_options;
146
Tianjie Xuc4447322017-03-06 14:44:59 -0800147 if (argv.size() == 5) {
Tao Bao039f2da2016-11-22 16:29:50 -0800148 mount_options = args[4];
149 }
150
151 if (fs_type.empty()) {
152 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
153 }
154 if (partition_type.empty()) {
155 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
156 name);
157 }
158 if (location.empty()) {
159 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
160 }
161 if (mount_point.empty()) {
162 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
163 name);
164 }
165
166 {
167 char* secontext = nullptr;
168
169 if (sehandle) {
170 selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755);
171 setfscreatecon(secontext);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700172 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700173
Tao Bao039f2da2016-11-22 16:29:50 -0800174 mkdir(mount_point.c_str(), 0755);
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700175
Tao Bao039f2da2016-11-22 16:29:50 -0800176 if (secontext) {
177 freecon(secontext);
178 setfscreatecon(nullptr);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700179 }
Tao Bao039f2da2016-11-22 16:29:50 -0800180 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700181
Tao Bao039f2da2016-11-22 16:29:50 -0800182 if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(),
183 MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) {
184 uiPrintf(state, "%s: failed to mount %s at %s: %s\n", name, location.c_str(),
185 mount_point.c_str(), strerror(errno));
186 return StringValue("");
187 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700188
Tao Bao039f2da2016-11-22 16:29:50 -0800189 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700190}
191
Doug Zongker8edb00c2009-06-11 17:21:44 -0700192// is_mounted(mount_point)
Tianjie Xuc4447322017-03-06 14:44:59 -0800193Value* IsMountedFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
194 if (argv.size() != 1) {
195 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800196 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700197
Tao Bao039f2da2016-11-22 16:29:50 -0800198 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800199 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800200 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
Tianjie Xuc4447322017-03-06 14:44:59 -0800217Value* UnmountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
218 if (argv.size() != 1) {
219 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800220 }
221 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800222 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800223 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")
Tianjie Xuc4447322017-03-06 14:44:59 -0800268Value* FormatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
269 if (argv.size() != 5) {
270 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %zu", name,
271 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800272 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500273
Tao Bao039f2da2016-11-22 16:29:50 -0800274 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800275 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800276 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
277 }
278 const std::string& fs_type = args[0];
279 const std::string& partition_type = args[1];
280 const std::string& location = args[2];
281 const std::string& fs_size = args[3];
282 const std::string& mount_point = args[4];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700283
Tao Bao039f2da2016-11-22 16:29:50 -0800284 if (fs_type.empty()) {
285 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
286 }
287 if (partition_type.empty()) {
288 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
289 name);
290 }
291 if (location.empty()) {
292 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
293 }
294 if (mount_point.empty()) {
295 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
296 name);
297 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700298
Tao Bao039f2da2016-11-22 16:29:50 -0800299 int64_t size;
300 if (!android::base::ParseInt(fs_size, &size)) {
301 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s\n", name,
302 fs_size.c_str());
303 }
304
305 if (fs_type == "ext4") {
306 int status = make_ext4fs(location.c_str(), size, mount_point.c_str(), sehandle);
307 if (status != 0) {
308 LOG(ERROR) << name << ": make_ext4fs failed (" << status << ") on " << location;
309 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700310 }
Tao Bao039f2da2016-11-22 16:29:50 -0800311 return StringValue(location);
312 } else if (fs_type == "f2fs") {
313 if (size < 0) {
314 LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size;
315 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700316 }
Tao Bao039f2da2016-11-22 16:29:50 -0800317 std::string num_sectors = std::to_string(size / 512);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700318
Tao Bao039f2da2016-11-22 16:29:50 -0800319 const char* f2fs_path = "/sbin/mkfs.f2fs";
320 const char* const f2fs_argv[] = { "mkfs.f2fs", "-t", "-d1", location.c_str(),
321 num_sectors.c_str(), nullptr };
322 int status = exec_cmd(f2fs_path, const_cast<char* const*>(f2fs_argv));
323 if (status != 0) {
324 LOG(ERROR) << name << ": mkfs.f2fs failed (" << status << ") on " << location;
325 return StringValue("");
326 }
327 return StringValue(location);
328 } else {
329 LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \""
330 << partition_type << "\"";
331 }
332
333 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700334}
335
Tom Marshall8e2579f2017-08-23 18:14:00 +0000336// rename(src_name, dst_name)
337// Renames src_name to dst_name. It automatically creates the necessary directories for dst_name.
338// Example: rename("system/app/Hangouts/Hangouts.apk", "system/priv-app/Hangouts/Hangouts.apk")
339Value* RenameFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
340 if (argv.size() != 2) {
341 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
342 argv.size());
343 }
344
345 std::vector<std::string> args;
346 if (!ReadArgs(state, argv, &args)) {
347 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
348 }
349 const std::string& src_name = args[0];
350 const std::string& dst_name = args[1];
351
352 if (src_name.empty()) {
353 return ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", name);
354 }
355 if (dst_name.empty()) {
356 return ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", name);
357 }
358 if (!make_parents(dst_name)) {
359 return ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
360 dst_name.c_str(), strerror(errno));
361 } else if (access(dst_name.c_str(), F_OK) == 0 && access(src_name.c_str(), F_OK) != 0) {
362 // File was already moved
363 return StringValue(dst_name);
364 } else if (rename(src_name.c_str(), dst_name.c_str()) != 0) {
365 return ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
366 src_name.c_str(), dst_name.c_str(), strerror(errno));
367 }
368
369 return StringValue(dst_name);
370}
371
372// delete([filename, ...])
373// Deletes all the filenames listed. Returns the number of files successfully deleted.
374//
375// delete_recursive([dirname, ...])
376// Recursively deletes dirnames and all their contents. Returns the number of directories
377// successfully deleted.
378Value* DeleteFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
379 std::vector<std::string> paths;
380 if (!ReadArgs(state, argv, &paths)) {
381 return nullptr;
382 }
383
384 bool recursive = (strcmp(name, "delete_recursive") == 0);
385
386 int success = 0;
387 for (const auto& path : paths) {
388 if ((recursive ? dirUnlinkHierarchy(path.c_str()) : unlink(path.c_str())) == 0) {
389 ++success;
390 }
391 }
392
393 return StringValue(std::to_string(success));
394}
395
396
Tianjie Xuc4447322017-03-06 14:44:59 -0800397Value* ShowProgressFn(const char* name, State* state,
398 const std::vector<std::unique_ptr<Expr>>& argv) {
399 if (argv.size() != 2) {
400 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
401 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800402 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700403
Tao Bao039f2da2016-11-22 16:29:50 -0800404 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800405 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800406 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
407 }
408 const std::string& frac_str = args[0];
409 const std::string& sec_str = args[1];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700410
Tao Bao039f2da2016-11-22 16:29:50 -0800411 double frac;
412 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
413 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s\n", name,
414 frac_str.c_str());
415 }
416 int sec;
417 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
418 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s\n", name,
419 sec_str.c_str());
420 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700421
Tao Bao039f2da2016-11-22 16:29:50 -0800422 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
423 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700424
Tao Bao039f2da2016-11-22 16:29:50 -0800425 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700426}
427
Tianjie Xuc4447322017-03-06 14:44:59 -0800428Value* SetProgressFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
429 if (argv.size() != 1) {
430 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800431 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700432
Tao Bao039f2da2016-11-22 16:29:50 -0800433 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800434 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800435 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
436 }
437 const std::string& frac_str = args[0];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700438
Tao Bao039f2da2016-11-22 16:29:50 -0800439 double frac;
440 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
441 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s\n", name,
442 frac_str.c_str());
443 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700444
Tao Bao039f2da2016-11-22 16:29:50 -0800445 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
446 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700447
Tao Bao039f2da2016-11-22 16:29:50 -0800448 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700449}
450
Tao Bao1036d362016-11-17 22:49:56 -0800451// package_extract_dir(package_dir, dest_dir)
452// Extracts all files from the package underneath package_dir and writes them to the
453// corresponding tree beneath dest_dir. Any existing files are overwritten.
454// Example: package_extract_dir("system", "/system")
455//
456// Note: package_dir needs to be a relative path; dest_dir needs to be an absolute path.
Tianjie Xuc4447322017-03-06 14:44:59 -0800457Value* PackageExtractDirFn(const char* name, State* state,
458 const std::vector<std::unique_ptr<Expr>>&argv) {
459 if (argv.size() != 2) {
460 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
461 argv.size());
Tao Bao1036d362016-11-17 22:49:56 -0800462 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700463
Tao Bao1036d362016-11-17 22:49:56 -0800464 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800465 if (!ReadArgs(state, argv, &args)) {
Tao Bao1036d362016-11-17 22:49:56 -0800466 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
467 }
468 const std::string& zip_path = args[0];
469 const std::string& dest_path = args[1];
Doug Zongker9931f7f2009-06-10 14:11:53 -0700470
Tao Bao1036d362016-11-17 22:49:56 -0800471 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700472
Tao Bao1036d362016-11-17 22:49:56 -0800473 // To create a consistent system image, never use the clock for timestamps.
474 constexpr struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
Doug Zongker9931f7f2009-06-10 14:11:53 -0700475
Tao Bao1036d362016-11-17 22:49:56 -0800476 bool success = ExtractPackageRecursive(za, zip_path, dest_path, &timestamp, sehandle);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700477
Tao Bao1036d362016-11-17 22:49:56 -0800478 return StringValue(success ? "t" : "");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700479}
480
Tao Baoef0eb3b2016-11-14 21:29:52 -0800481// package_extract_file(package_file[, dest_file])
482// Extracts a single package_file from the update package and writes it to dest_file,
483// overwriting existing files if necessary. Without the dest_file argument, returns the
484// contents of the package file as a binary blob.
Tianjie Xuc4447322017-03-06 14:44:59 -0800485Value* PackageExtractFileFn(const char* name, State* state,
486 const std::vector<std::unique_ptr<Expr>>& argv) {
487 if (argv.size() < 1 || argv.size() > 2) {
488 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name,
489 argv.size());
Tao Baoef0eb3b2016-11-14 21:29:52 -0800490 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700491
Tianjie Xuc4447322017-03-06 14:44:59 -0800492 if (argv.size() == 2) {
Tao Baoef0eb3b2016-11-14 21:29:52 -0800493 // The two-argument version extracts to a file.
494
495 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800496 if (!ReadArgs(state, argv, &args)) {
497 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
498 argv.size());
Doug Zongker8edb00c2009-06-11 17:21:44 -0700499 }
Tao Baoef0eb3b2016-11-14 21:29:52 -0800500 const std::string& zip_path = args[0];
501 const std::string& dest_path = args[1];
Doug Zongker43772d22014-06-09 14:13:19 -0700502
Tao Baoef0eb3b2016-11-14 21:29:52 -0800503 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
504 ZipString zip_string_path(zip_path.c_str());
505 ZipEntry entry;
506 if (FindEntry(za, zip_string_path, &entry) != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800507 LOG(ERROR) << name << ": no " << zip_path << " in package";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800508 return StringValue("");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700509 }
Tao Baoef0eb3b2016-11-14 21:29:52 -0800510
Tao Bao358c2ec2016-11-28 11:48:43 -0800511 unique_fd fd(TEMP_FAILURE_RETRY(
512 ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tao Baoef0eb3b2016-11-14 21:29:52 -0800513 if (fd == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800514 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800515 return StringValue("");
516 }
517
518 bool success = true;
519 int32_t ret = ExtractEntryToFile(za, &entry, fd);
520 if (ret != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800521 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
522 << entry.uncompressed_length << " bytes) to \"" << dest_path
523 << "\": " << ErrorCodeString(ret);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800524 success = false;
525 }
526 if (ota_fsync(fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800527 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800528 success = false;
529 }
530 if (ota_close(fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800531 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800532 success = false;
533 }
534
535 return StringValue(success ? "t" : "");
536 } else {
537 // The one-argument version returns the contents of the file as the result.
538
539 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800540 if (!ReadArgs(state, argv, &args)) {
541 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
542 argv.size());
Tao Baoef0eb3b2016-11-14 21:29:52 -0800543 }
544 const std::string& zip_path = args[0];
545
546 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
547 ZipString zip_string_path(zip_path.c_str());
548 ZipEntry entry;
549 if (FindEntry(za, zip_string_path, &entry) != 0) {
550 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
551 zip_path.c_str());
552 }
553
554 std::string buffer;
555 buffer.resize(entry.uncompressed_length);
556
557 int32_t ret = ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
558 if (ret != 0) {
559 return ErrorAbort(state, kPackageExtractFileFailure,
560 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
561 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
562 }
563
564 return new Value(VAL_BLOB, buffer);
565 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700566}
567
Tom Marshall8e2579f2017-08-23 18:14:00 +0000568// symlink(target, [src1, src2, ...])
569// Creates all sources as symlinks to target. It unlinks any previously existing src1, src2, etc
570// before creating symlinks.
571Value* SymlinkFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
572 if (argv.size() == 0) {
573 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %zu", name, argv.size());
574 }
575 std::string target;
576 if (!Evaluate(state, argv[0], &target)) {
577 return nullptr;
578 }
579
580 std::vector<std::string> srcs;
581 if (!ReadArgs(state, argv, &srcs, 1, argv.size())) {
582 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
583 }
584
585 size_t bad = 0;
586 for (const auto& src : srcs) {
587 if (unlink(src.c_str()) == -1 && errno != ENOENT) {
588 PLOG(ERROR) << name << ": failed to remove " << src;
589 ++bad;
590 } else if (!make_parents(src)) {
591 LOG(ERROR) << name << ": failed to symlink " << src << " to " << target
592 << ": making parents failed";
593 ++bad;
594 } else if (symlink(target.c_str(), src.c_str()) == -1) {
595 PLOG(ERROR) << name << ": failed to symlink " << src << " to " << target;
596 ++bad;
597 }
598 }
599 if (bad != 0) {
600 return ErrorAbort(state, kSymlinkFailure, "%s: Failed to create %zu symlink(s)", name, bad);
601 }
602 return StringValue("t");
603}
604
605struct perm_parsed_args {
606 bool has_uid;
607 uid_t uid;
608 bool has_gid;
609 gid_t gid;
610 bool has_mode;
611 mode_t mode;
612 bool has_fmode;
613 mode_t fmode;
614 bool has_dmode;
615 mode_t dmode;
616 bool has_selabel;
617 const char* selabel;
618 bool has_capabilities;
619 uint64_t capabilities;
620};
621
622static struct perm_parsed_args ParsePermArgs(State * state,
623 const std::vector<std::string>& args) {
624 struct perm_parsed_args parsed;
625 int bad = 0;
626 static int max_warnings = 20;
627
628 memset(&parsed, 0, sizeof(parsed));
629
630 for (size_t i = 1; i < args.size(); i += 2) {
631 if (args[i] == "uid") {
632 int64_t uid;
633 if (sscanf(args[i + 1].c_str(), "%" SCNd64, &uid) == 1) {
634 parsed.uid = uid;
635 parsed.has_uid = true;
636 } else {
637 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1].c_str());
638 bad++;
639 }
640 continue;
641 }
642 if (args[i] == "gid") {
643 int64_t gid;
644 if (sscanf(args[i + 1].c_str(), "%" SCNd64, &gid) == 1) {
645 parsed.gid = gid;
646 parsed.has_gid = true;
647 } else {
648 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1].c_str());
649 bad++;
650 }
651 continue;
652 }
653 if (args[i] == "mode") {
654 int32_t mode;
655 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
656 parsed.mode = mode;
657 parsed.has_mode = true;
658 } else {
659 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1].c_str());
660 bad++;
661 }
662 continue;
663 }
664 if (args[i] == "dmode") {
665 int32_t mode;
666 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
667 parsed.dmode = mode;
668 parsed.has_dmode = true;
669 } else {
670 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1].c_str());
671 bad++;
672 }
673 continue;
674 }
675 if (args[i] == "fmode") {
676 int32_t mode;
677 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
678 parsed.fmode = mode;
679 parsed.has_fmode = true;
680 } else {
681 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1].c_str());
682 bad++;
683 }
684 continue;
685 }
686 if (args[i] == "capabilities") {
687 int64_t capabilities;
688 if (sscanf(args[i + 1].c_str(), "%" SCNi64, &capabilities) == 1) {
689 parsed.capabilities = capabilities;
690 parsed.has_capabilities = true;
691 } else {
692 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1].c_str());
693 bad++;
694 }
695 continue;
696 }
697 if (args[i] == "selabel") {
698 if (!args[i + 1].empty()) {
699 parsed.selabel = args[i + 1].c_str();
700 parsed.has_selabel = true;
701 } else {
702 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1].c_str());
703 bad++;
704 }
705 continue;
706 }
707 if (max_warnings != 0) {
708 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i].c_str());
709 max_warnings--;
710 if (max_warnings == 0) {
711 LOG(INFO) << "ParsedPermArgs: suppressing further warnings";
712 }
713 }
714 }
715 return parsed;
716}
717
718static int ApplyParsedPerms(State* state, const char* filename, const struct stat* statptr,
719 struct perm_parsed_args parsed) {
720 int bad = 0;
721
722 if (parsed.has_selabel) {
723 if (lsetfilecon(filename, parsed.selabel) != 0) {
724 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n", filename,
725 parsed.selabel, strerror(errno));
726 bad++;
727 }
728 }
729
730 /* ignore symlinks */
731 if (S_ISLNK(statptr->st_mode)) {
732 return bad;
733 }
734
735 if (parsed.has_uid) {
736 if (chown(filename, parsed.uid, -1) < 0) {
737 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n", filename, parsed.uid,
738 strerror(errno));
739 bad++;
740 }
741 }
742
743 if (parsed.has_gid) {
744 if (chown(filename, -1, parsed.gid) < 0) {
745 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n", filename, parsed.gid,
746 strerror(errno));
747 bad++;
748 }
749 }
750
751 if (parsed.has_mode) {
752 if (chmod(filename, parsed.mode) < 0) {
753 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.mode,
754 strerror(errno));
755 bad++;
756 }
757 }
758
759 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
760 if (chmod(filename, parsed.dmode) < 0) {
761 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.dmode,
762 strerror(errno));
763 bad++;
764 }
765 }
766
767 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
768 if (chmod(filename, parsed.fmode) < 0) {
769 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.fmode,
770 strerror(errno));
771 bad++;
772 }
773 }
774
775 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
776 if (parsed.capabilities == 0) {
777 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
778 // Report failure unless it's ENODATA (attribute not set)
779 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n", filename,
780 parsed.capabilities, strerror(errno));
781 bad++;
782 }
783 } else {
784 struct vfs_cap_data cap_data;
785 memset(&cap_data, 0, sizeof(cap_data));
786 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
787 cap_data.data[0].permitted = (uint32_t)(parsed.capabilities & 0xffffffff);
788 cap_data.data[0].inheritable = 0;
789 cap_data.data[1].permitted = (uint32_t)(parsed.capabilities >> 32);
790 cap_data.data[1].inheritable = 0;
791 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
792 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n", filename,
793 parsed.capabilities, strerror(errno));
794 bad++;
795 }
796 }
797 }
798
799 return bad;
800}
801
802// nftw doesn't allow us to pass along context, so we need to use
803// global variables. *sigh*
804static struct perm_parsed_args recursive_parsed_args;
805static State* recursive_state;
806
807static int do_SetMetadataRecursive(const char* filename, const struct stat* statptr, int fileflags,
808 struct FTW* pfwt) {
809 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
810}
811
812static Value* SetMetadataFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
813 if ((argv.size() % 2) != 1) {
814 return ErrorAbort(state, kArgsParsingFailure, "%s() expects an odd number of arguments, got %zu",
815 name, argv.size());
816 }
817
818 std::vector<std::string> args;
819 if (!ReadArgs(state, argv, &args)) {
820 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
821 }
822
823 struct stat sb;
824 if (lstat(args[0].c_str(), &sb) == -1) {
825 return ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s", name,
826 args[0].c_str(), strerror(errno));
827 }
828
829 struct perm_parsed_args parsed = ParsePermArgs(state, args);
830 int bad = 0;
831 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
832
833 if (recursive) {
834 recursive_parsed_args = parsed;
835 recursive_state = state;
836 bad += nftw(args[0].c_str(), do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
837 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
838 recursive_state = NULL;
839 } else {
840 bad += ApplyParsedPerms(state, args[0].c_str(), &sb, parsed);
841 }
842
843 if (bad > 0) {
844 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
845 }
846
847 return StringValue("");
848}
849
Tianjie Xuc4447322017-03-06 14:44:59 -0800850Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
851 if (argv.size() != 1) {
852 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800853 }
854 std::string key;
855 if (!Evaluate(state, argv[0], &key)) {
856 return nullptr;
857 }
858 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700859
Tao Bao039f2da2016-11-22 16:29:50 -0800860 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700861}
862
Doug Zongker47cace92009-06-18 10:11:50 -0700863// file_getprop(file, key)
864//
865// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -0700866// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700867// and returns the value for 'key' (or "" if it isn't defined).
Tianjie Xuc4447322017-03-06 14:44:59 -0800868Value* FileGetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
869 if (argv.size() != 2) {
870 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
871 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800872 }
873
874 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800875 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800876 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
877 }
878 const std::string& filename = args[0];
879 const std::string& key = args[1];
880
881 struct stat st;
882 if (stat(filename.c_str(), &st) < 0) {
883 return ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name,
884 filename.c_str(), strerror(errno));
885 }
886
887 constexpr off_t MAX_FILE_GETPROP_SIZE = 65536;
888 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
889 return ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %lld)",
890 filename.c_str(), name, static_cast<long long>(MAX_FILE_GETPROP_SIZE));
891 }
892
893 std::string buffer(st.st_size, '\0');
894 unique_file f(ota_fopen(filename.c_str(), "rb"));
895 if (f == nullptr) {
896 return ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename.c_str(),
897 strerror(errno));
898 }
899
900 if (ota_fread(&buffer[0], 1, st.st_size, f.get()) != static_cast<size_t>(st.st_size)) {
901 ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s", name,
902 static_cast<size_t>(st.st_size), filename.c_str());
903 return nullptr;
904 }
905
906 ota_fclose(f);
907
908 std::vector<std::string> lines = android::base::Split(buffer, "\n");
909 for (size_t i = 0; i < lines.size(); i++) {
910 std::string line = android::base::Trim(lines[i]);
911
912 // comment or blank line: skip to next line
913 if (line.empty() || line[0] == '#') {
914 continue;
915 }
916 size_t equal_pos = line.find('=');
917 if (equal_pos == std::string::npos) {
918 continue;
Tao Bao51d516e2016-11-03 14:49:01 -0700919 }
920
Tao Bao358c2ec2016-11-28 11:48:43 -0800921 // trim whitespace between key and '='
922 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -0700923
Tao Bao358c2ec2016-11-28 11:48:43 -0800924 // not the key we're looking for
925 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700926
Tao Bao358c2ec2016-11-28 11:48:43 -0800927 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
928 }
Doug Zongker47cace92009-06-18 10:11:50 -0700929
Tao Bao358c2ec2016-11-28 11:48:43 -0800930 return StringValue("");
Doug Zongker47cace92009-06-18 10:11:50 -0700931}
932
Doug Zongker8edb00c2009-06-11 17:21:44 -0700933// apply_patch_space(bytes)
Tianjie Xuc4447322017-03-06 14:44:59 -0800934Value* ApplyPatchSpaceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
935 if (argv.size() != 1) {
936 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name,
937 argv.size());
938 }
Tao Bao039f2da2016-11-22 16:29:50 -0800939 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800940 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800941 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
942 }
943 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800944
Tao Bao039f2da2016-11-22 16:29:50 -0800945 size_t bytes;
946 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
947 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n",
948 name, bytes_str.c_str());
949 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800950
Tao Bao039f2da2016-11-22 16:29:50 -0800951 return StringValue(CacheSizeCheck(bytes) ? "" : "t");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800952}
953
Tao Bao039f2da2016-11-22 16:29:50 -0800954// apply_patch(src_file, tgt_file, tgt_sha1, tgt_size, patch1_sha1, patch1_blob, [...])
955// Applies a binary patch to the src_file to produce the tgt_file. If the desired target is the
956// same as the source, pass "-" for tgt_file. tgt_sha1 and tgt_size are the expected final SHA1
957// hash and size of the target file. The remaining arguments must come in pairs: a SHA1 hash (a
958// 40-character hex string) and a blob. The blob is the patch to be applied when the source
959// file's current contents have the given SHA1.
960//
961// The patching is done in a safe manner that guarantees the target file either has the desired
962// SHA1 hash and size, or it is untouched -- it will not be left in an unrecoverable intermediate
963// state. If the process is interrupted during patching, the target file may be in an intermediate
964// state; a copy exists in the cache partition so restarting the update can successfully update
965// the file.
Tianjie Xuc4447322017-03-06 14:44:59 -0800966Value* ApplyPatchFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
967 if (argv.size() < 6 || (argv.size() % 2) == 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700968 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an "
Tianjie Xuc4447322017-03-06 14:44:59 -0800969 "even number, got %zu", name, argv.size());
Doug Zongker8edb00c2009-06-11 17:21:44 -0700970 }
971
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700972 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800973 if (!ReadArgs(state, argv, &args, 0, 4)) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700974 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700975 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700976 const std::string& source_filename = args[0];
977 const std::string& target_filename = args[1];
978 const std::string& target_sha1 = args[2];
979 const std::string& target_size_str = args[3];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700980
Tao Baob15fd222015-09-24 11:10:51 -0700981 size_t target_size;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700982 if (!android::base::ParseUint(target_size_str.c_str(), &target_size)) {
983 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count",
984 name, target_size_str.c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800985 }
986
Tianjie Xuc4447322017-03-06 14:44:59 -0800987 int patchcount = (argv.size()-4) / 2;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700988 std::vector<std::unique_ptr<Value>> arg_values;
Tianjie Xuc4447322017-03-06 14:44:59 -0800989 if (!ReadValueArgs(state, argv, &arg_values, 4, argv.size() - 4)) {
Yabin Cui64be2132016-02-03 18:16:02 -0800990 return nullptr;
991 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700992
Yabin Cui64be2132016-02-03 18:16:02 -0800993 for (int i = 0; i < patchcount; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700994 if (arg_values[i * 2]->type != VAL_STRING) {
995 return ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name,
996 i * 2);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800997 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700998 if (arg_values[i * 2 + 1]->type != VAL_BLOB) {
999 return ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name,
1000 i * 2 + 1);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001001 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001002 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001003
Tianjie Xuaced5d92016-10-12 10:55:04 -07001004 std::vector<std::string> patch_sha_str;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001005 std::vector<std::unique_ptr<Value>> patches;
Yabin Cui64be2132016-02-03 18:16:02 -08001006 for (int i = 0; i < patchcount; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001007 patch_sha_str.push_back(arg_values[i * 2]->data);
1008 patches.push_back(std::move(arg_values[i * 2 + 1]));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001009 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001010
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001011 int result = applypatch(source_filename.c_str(), target_filename.c_str(),
1012 target_sha1.c_str(), target_size,
1013 patch_sha_str, patches, nullptr);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001014
Tianjie Xuaced5d92016-10-12 10:55:04 -07001015 return StringValue(result == 0 ? "t" : "");
Doug Zongkerc4351c72010-02-22 14:46:32 -08001016}
1017
Tao Bao039f2da2016-11-22 16:29:50 -08001018// apply_patch_check(filename, [sha1, ...])
1019// Returns true if the contents of filename or the temporary copy in the cache partition (if
1020// present) have a SHA-1 checksum equal to one of the given sha1 values. sha1 values are
1021// specified as 40 hex digits. This function differs from sha1_check(read_file(filename),
1022// sha1 [, ...]) in that it knows to check the cache partition copy, so apply_patch_check() will
1023// succeed even if the file was corrupted by an interrupted apply_patch() update.
Tianjie Xuc4447322017-03-06 14:44:59 -08001024Value* ApplyPatchCheckFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1025 if (argv.size() < 1) {
1026 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %zu", name,
1027 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001028 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001029
Tao Bao039f2da2016-11-22 16:29:50 -08001030 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001031 if (!ReadArgs(state, argv, &args, 0, 1)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001032 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1033 }
1034 const std::string& filename = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -08001035
Tao Bao039f2da2016-11-22 16:29:50 -08001036 std::vector<std::string> sha1s;
Tao Baodb56eb02017-03-23 06:34:20 -07001037 if (argv.size() > 1 && !ReadArgs(state, argv, &sha1s, 1, argv.size() - 1)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001038 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1039 }
1040 int result = applypatch_check(filename.c_str(), sha1s);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001041
Tao Bao039f2da2016-11-22 16:29:50 -08001042 return StringValue(result == 0 ? "t" : "");
Doug Zongker8edb00c2009-06-11 17:21:44 -07001043}
1044
Tao Bao1107d962015-09-09 17:16:55 -07001045// This is the updater side handler for ui_print() in edify script. Contents
1046// will be sent over to the recovery side for on-screen display.
Tianjie Xuc4447322017-03-06 14:44:59 -08001047Value* UIPrintFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
Tao Bao039f2da2016-11-22 16:29:50 -08001048 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001049 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001050 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1051 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001052
Tao Bao039f2da2016-11-22 16:29:50 -08001053 std::string buffer = android::base::Join(args, "") + "\n";
1054 uiPrint(state, buffer);
1055 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001056}
1057
Tianjie Xuc4447322017-03-06 14:44:59 -08001058Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1059 if (!argv.empty()) {
1060 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
1061 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001062 }
1063 fprintf(static_cast<UpdaterInfo*>(state->cookie)->cmd_pipe, "wipe_cache\n");
1064 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -07001065}
1066
Tianjie Xuc4447322017-03-06 14:44:59 -08001067Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1068 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -08001069 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
1070 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001071
Tao Bao039f2da2016-11-22 16:29:50 -08001072 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001073 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001074 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1075 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001076
Tianjie Xuc4447322017-03-06 14:44:59 -08001077 char* args2[argv.size() + 1];
1078 for (size_t i = 0; i < argv.size(); i++) {
Tao Bao039f2da2016-11-22 16:29:50 -08001079 args2[i] = &args[i][0];
1080 }
Tianjie Xuc4447322017-03-06 14:44:59 -08001081 args2[argv.size()] = nullptr;
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001082
Tianjie Xuc4447322017-03-06 14:44:59 -08001083 LOG(INFO) << "about to run program [" << args2[0] << "] with " << argv.size() << " args";
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001084
Tao Bao039f2da2016-11-22 16:29:50 -08001085 pid_t child = fork();
1086 if (child == 0) {
1087 execv(args2[0], args2);
1088 PLOG(ERROR) << "run_program: execv failed";
Tao Bao3da88012017-02-03 13:09:23 -08001089 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -08001090 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001091
Tao Bao039f2da2016-11-22 16:29:50 -08001092 int status;
1093 waitpid(child, &status, 0);
1094 if (WIFEXITED(status)) {
1095 if (WEXITSTATUS(status) != 0) {
1096 LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status);
1097 }
1098 } else if (WIFSIGNALED(status)) {
1099 LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status);
1100 }
1101
1102 return StringValue(std::to_string(status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001103}
1104
Doug Zongker512536a2010-02-17 16:11:44 -08001105// sha1_check(data)
1106// to return the sha1 of the data (given in the format returned by
1107// read_file).
1108//
1109// sha1_check(data, sha1_hex, [sha1_hex, ...])
1110// returns the sha1 of the file if it matches any of the hex
1111// strings passed, or "" if it does not equal any of them.
1112//
Tianjie Xuc4447322017-03-06 14:44:59 -08001113Value* Sha1CheckFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1114 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -08001115 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
1116 }
Doug Zongker512536a2010-02-17 16:11:44 -08001117
Tao Bao039f2da2016-11-22 16:29:50 -08001118 std::vector<std::unique_ptr<Value>> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001119 if (!ReadValueArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001120 return nullptr;
1121 }
Doug Zongker512536a2010-02-17 16:11:44 -08001122
Tao Bao039f2da2016-11-22 16:29:50 -08001123 if (args[0]->type == VAL_INVALID) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001124 return StringValue("");
Tao Bao039f2da2016-11-22 16:29:50 -08001125 }
1126 uint8_t digest[SHA_DIGEST_LENGTH];
1127 SHA1(reinterpret_cast<const uint8_t*>(args[0]->data.c_str()), args[0]->data.size(), digest);
1128
Tianjie Xuc4447322017-03-06 14:44:59 -08001129 if (argv.size() == 1) {
Tao Bao039f2da2016-11-22 16:29:50 -08001130 return StringValue(print_sha1(digest));
1131 }
1132
Tianjie Xuc4447322017-03-06 14:44:59 -08001133 for (size_t i = 1; i < argv.size(); ++i) {
Tao Bao039f2da2016-11-22 16:29:50 -08001134 uint8_t arg_digest[SHA_DIGEST_LENGTH];
1135 if (args[i]->type != VAL_STRING) {
1136 LOG(ERROR) << name << "(): arg " << i << " is not a string; skipping";
1137 } else if (ParseSha1(args[i]->data.c_str(), arg_digest) != 0) {
1138 // Warn about bad args and skip them.
1139 LOG(ERROR) << name << "(): error parsing \"" << args[i]->data << "\" as sha-1; skipping";
1140 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
1141 // Found a match.
1142 return args[i].release();
1143 }
1144 }
1145
1146 // Didn't match any of the hex strings; return false.
1147 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -08001148}
1149
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001150// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001151// is actually a FileContents*).
Tianjie Xuc4447322017-03-06 14:44:59 -08001152Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1153 if (argv.size() != 1) {
1154 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001155 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001156
Tao Bao039f2da2016-11-22 16:29:50 -08001157 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001158 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001159 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1160 }
1161 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -08001162
Tao Bao039f2da2016-11-22 16:29:50 -08001163 Value* v = new Value(VAL_INVALID, "");
Doug Zongker512536a2010-02-17 16:11:44 -08001164
Tao Bao039f2da2016-11-22 16:29:50 -08001165 FileContents fc;
1166 if (LoadFileContents(filename.c_str(), &fc) == 0) {
1167 v->type = VAL_BLOB;
1168 v->data = std::string(fc.data.begin(), fc.data.end());
1169 }
1170 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001171}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001172
Tao Baod0f30882016-11-03 23:52:01 -07001173// write_value(value, filename)
1174// Writes 'value' to 'filename'.
1175// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
Tianjie Xuc4447322017-03-06 14:44:59 -08001176Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1177 if (argv.size() != 2) {
1178 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1179 argv.size());
Tao Baod0f30882016-11-03 23:52:01 -07001180 }
1181
1182 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001183 if (!ReadArgs(state, argv, &args)) {
Tao Baod0f30882016-11-03 23:52:01 -07001184 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1185 }
1186
1187 const std::string& filename = args[1];
1188 if (filename.empty()) {
1189 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
1190 }
1191
1192 const std::string& value = args[0];
1193 if (!android::base::WriteStringToFile(value, filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001194 PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\"";
Tao Baod0f30882016-11-03 23:52:01 -07001195 return StringValue("");
1196 } else {
1197 return StringValue("t");
1198 }
1199}
1200
Doug Zongkerc87bab12013-11-25 13:53:25 -08001201// Immediately reboot the device. Recovery is not finished normally,
1202// so if you reboot into recovery it will re-start applying the
1203// current package (because nothing has cleared the copy of the
1204// arguments stored in the BCB).
1205//
1206// The argument is the partition name passed to the android reboot
1207// property. It can be "recovery" to boot from the recovery
1208// partition, or "" (empty string) to boot from the regular boot
1209// partition.
Tianjie Xuc4447322017-03-06 14:44:59 -08001210Value* RebootNowFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1211 if (argv.size() != 2) {
1212 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1213 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001214 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001215
Tao Baobedf5fc2016-11-18 12:01:26 -08001216 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001217 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001218 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1219 }
1220 const std::string& filename = args[0];
1221 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001222
Tao Baobedf5fc2016-11-18 12:01:26 -08001223 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
1224 bootloader_message boot;
1225 std::string err;
1226 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001227 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001228 return StringValue("");
1229 }
1230 memset(boot.command, 0, sizeof(boot.command));
1231 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001232 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001233 return StringValue("");
1234 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001235
Dmitri Plotnikoved9db0f2017-04-18 08:28:26 -07001236 std::string reboot_cmd = "reboot," + property;
1237 if (android::base::GetBoolProperty("ro.boot.quiescent", false)) {
1238 reboot_cmd += ",quiescent";
1239 }
Tao Baobedf5fc2016-11-18 12:01:26 -08001240 android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001241
Tao Baobedf5fc2016-11-18 12:01:26 -08001242 sleep(5);
1243 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001244}
1245
1246// Store a string value somewhere that future invocations of recovery
1247// can access it. This value is called the "stage" and can be used to
1248// drive packages that need to do reboots in the middle of
1249// installation and keep track of where they are in the multi-stage
1250// install.
1251//
1252// The first argument is the block device for the misc partition
1253// ("/misc" in the fstab), which is where this value is stored. The
1254// second argument is the string to store; it should not exceed 31
1255// bytes.
Tianjie Xuc4447322017-03-06 14:44:59 -08001256Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1257 if (argv.size() != 2) {
1258 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1259 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001260 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001261
Tao Baobedf5fc2016-11-18 12:01:26 -08001262 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001263 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001264 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1265 }
1266 const std::string& filename = args[0];
1267 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001268
Tao Baobedf5fc2016-11-18 12:01:26 -08001269 // Store this value in the misc partition, immediately after the
1270 // bootloader message that the main recovery uses to save its
1271 // arguments in case of the device restarting midway through
1272 // package installation.
1273 bootloader_message boot;
1274 std::string err;
1275 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001276 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001277 return StringValue("");
1278 }
1279 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
1280 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001281 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001282 return StringValue("");
1283 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001284
Tao Baobedf5fc2016-11-18 12:01:26 -08001285 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001286}
1287
1288// Return the value most recently saved with SetStageFn. The argument
1289// is the block device for the misc partition.
Tianjie Xuc4447322017-03-06 14:44:59 -08001290Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1291 if (argv.size() != 1) {
1292 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001293 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001294
Tao Baobedf5fc2016-11-18 12:01:26 -08001295 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001296 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001297 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1298 }
1299 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001300
Tao Baobedf5fc2016-11-18 12:01:26 -08001301 bootloader_message boot;
1302 std::string err;
1303 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001304 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001305 return StringValue("");
1306 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001307
Tao Baobedf5fc2016-11-18 12:01:26 -08001308 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001309}
1310
Tianjie Xuc4447322017-03-06 14:44:59 -08001311Value* WipeBlockDeviceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1312 if (argv.size() != 2) {
1313 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1314 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -08001315 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001316
Tao Bao358c2ec2016-11-28 11:48:43 -08001317 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001318 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -08001319 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1320 }
1321 const std::string& filename = args[0];
1322 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001323
Tao Bao358c2ec2016-11-28 11:48:43 -08001324 size_t len;
1325 if (!android::base::ParseUint(len_str.c_str(), &len)) {
1326 return nullptr;
1327 }
1328 unique_fd fd(ota_open(filename.c_str(), O_WRONLY, 0644));
1329 // The wipe_block_device function in ext4_utils returns 0 on success and 1
1330 // for failure.
1331 int status = wipe_block_device(fd, len);
1332 return StringValue((status == 0) ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001333}
1334
Tianjie Xuc4447322017-03-06 14:44:59 -08001335Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1336 if (!argv.empty()) {
1337 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
1338 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001339 }
1340 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
1341 fprintf(ui->cmd_pipe, "enable_reboot\n");
1342 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -07001343}
1344
Tianjie Xuc4447322017-03-06 14:44:59 -08001345Value* Tune2FsFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1346 if (argv.empty()) {
1347 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001348 }
Michael Rungeacf47db2014-11-21 00:12:28 -08001349
Tao Bao039f2da2016-11-22 16:29:50 -08001350 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001351 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001352 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
1353 }
Michael Rungeacf47db2014-11-21 00:12:28 -08001354
Tianjie Xuc4447322017-03-06 14:44:59 -08001355 char* args2[argv.size() + 1];
Tao Bao039f2da2016-11-22 16:29:50 -08001356 // Tune2fs expects the program name as its args[0]
1357 args2[0] = const_cast<char*>(name);
1358 if (args2[0] == nullptr) {
1359 return nullptr;
1360 }
Tianjie Xuc4447322017-03-06 14:44:59 -08001361 for (size_t i = 0; i < argv.size(); ++i) {
Tao Bao039f2da2016-11-22 16:29:50 -08001362 args2[i + 1] = &args[i][0];
1363 }
Michael Rungeacf47db2014-11-21 00:12:28 -08001364
Tao Bao039f2da2016-11-22 16:29:50 -08001365 // tune2fs changes the file system parameters on an ext2 file system; it
1366 // returns 0 on success.
Tianjie Xuc4447322017-03-06 14:44:59 -08001367 int result = tune2fs_main(argv.size() + 1, args2);
Tao Bao039f2da2016-11-22 16:29:50 -08001368 if (result != 0) {
1369 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
1370 }
1371 return StringValue("t");
Michael Rungeacf47db2014-11-21 00:12:28 -08001372}
1373
Doug Zongker9931f7f2009-06-10 14:11:53 -07001374void RegisterInstallFunctions() {
Tao Bao039f2da2016-11-22 16:29:50 -08001375 RegisterFunction("mount", MountFn);
1376 RegisterFunction("is_mounted", IsMountedFn);
1377 RegisterFunction("unmount", UnmountFn);
1378 RegisterFunction("format", FormatFn);
1379 RegisterFunction("show_progress", ShowProgressFn);
1380 RegisterFunction("set_progress", SetProgressFn);
Tom Marshall8e2579f2017-08-23 18:14:00 +00001381 RegisterFunction("delete", DeleteFn);
1382 RegisterFunction("delete_recursive", DeleteFn);
Tao Bao039f2da2016-11-22 16:29:50 -08001383 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1384 RegisterFunction("package_extract_file", PackageExtractFileFn);
Tom Marshall8e2579f2017-08-23 18:14:00 +00001385 RegisterFunction("symlink", SymlinkFn);
1386
1387 // Usage:
1388 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1389 // Example:
1390 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel",
1391 // "u:object_r:system_file:s0", "capabilities", 0x0);
1392 RegisterFunction("set_metadata", SetMetadataFn);
1393
1394 // Usage:
1395 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1396 // Example:
1397 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755,
1398 // "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1399 RegisterFunction("set_metadata_recursive", SetMetadataFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001400
Tao Bao039f2da2016-11-22 16:29:50 -08001401 RegisterFunction("getprop", GetPropFn);
1402 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001403
Tao Bao039f2da2016-11-22 16:29:50 -08001404 RegisterFunction("apply_patch", ApplyPatchFn);
1405 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1406 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001407
Tao Bao039f2da2016-11-22 16:29:50 -08001408 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001409
Tao Bao039f2da2016-11-22 16:29:50 -08001410 RegisterFunction("read_file", ReadFileFn);
1411 RegisterFunction("sha1_check", Sha1CheckFn);
Tom Marshall8e2579f2017-08-23 18:14:00 +00001412 RegisterFunction("rename", RenameFn);
Tao Bao039f2da2016-11-22 16:29:50 -08001413 RegisterFunction("write_value", WriteValueFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001414
Tao Bao039f2da2016-11-22 16:29:50 -08001415 RegisterFunction("wipe_cache", WipeCacheFn);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001416
Tao Bao039f2da2016-11-22 16:29:50 -08001417 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001418
Tao Bao039f2da2016-11-22 16:29:50 -08001419 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001420
Tao Bao039f2da2016-11-22 16:29:50 -08001421 RegisterFunction("reboot_now", RebootNowFn);
1422 RegisterFunction("get_stage", GetStageFn);
1423 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001424
Tao Bao039f2da2016-11-22 16:29:50 -08001425 RegisterFunction("enable_reboot", EnableRebootFn);
1426 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001427}