blob: ff79edce0b8dc5eaa52de684ec9bde5a898600af [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"
Tao Bao361342c2016-02-08 11:15:50 -080064#include "print_sha1.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080065#include "tune2fs.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070066#include "updater/updater.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070067
Tao Bao1107d962015-09-09 17:16:55 -070068// Send over the buffer to recovery though the command pipe.
69static void uiPrint(State* state, const std::string& buffer) {
Tao Bao039f2da2016-11-22 16:29:50 -080070 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070071
Tao Bao039f2da2016-11-22 16:29:50 -080072 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
73 // So skip sending empty strings to UI.
74 std::vector<std::string> lines = android::base::Split(buffer, "\n");
75 for (auto& line : lines) {
76 if (!line.empty()) {
77 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
Tao Bao1107d962015-09-09 17:16:55 -070078 }
Tao Bao039f2da2016-11-22 16:29:50 -080079 }
Tao Bao1107d962015-09-09 17:16:55 -070080
Tao Bao039f2da2016-11-22 16:29:50 -080081 // On the updater side, we need to dump the contents to stderr (which has
82 // been redirected to the log file). Because the recovery will only print
83 // the contents to screen when processing pipe command ui_print.
84 LOG(INFO) << buffer;
Michael Runged4a63422014-10-22 19:48:41 -070085}
86
Elliott Hughes83ce7552016-06-30 09:28:42 -070087void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) {
Tao Bao039f2da2016-11-22 16:29:50 -080088 std::string error_msg;
Tao Bao1107d962015-09-09 17:16:55 -070089
Tao Bao039f2da2016-11-22 16:29:50 -080090 va_list ap;
91 va_start(ap, format);
92 android::base::StringAppendV(&error_msg, format, ap);
93 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070094
Tao Bao039f2da2016-11-22 16:29:50 -080095 uiPrint(state, error_msg);
Michael Runged4a63422014-10-22 19:48:41 -070096}
97
Tianjie Xud75003d2016-11-04 11:31:29 -070098static bool is_dir(const std::string& dirpath) {
99 struct stat st;
100 return stat(dirpath.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
101}
102
Tao Bao0c7839a2016-10-10 15:48:37 -0700103// Create all parent directories of name, if necessary.
Tianjie Xud75003d2016-11-04 11:31:29 -0700104static bool make_parents(const std::string& name) {
Tao Bao039f2da2016-11-22 16:29:50 -0800105 size_t prev_end = 0;
106 while (prev_end < name.size()) {
107 size_t next_end = name.find('/', prev_end + 1);
108 if (next_end == std::string::npos) {
109 break;
Tao Bao0c7839a2016-10-10 15:48:37 -0700110 }
Tao Bao039f2da2016-11-22 16:29:50 -0800111 std::string dir_path = name.substr(0, next_end);
112 if (!is_dir(dir_path)) {
113 int result = mkdir(dir_path.c_str(), 0700);
114 if (result != 0) {
115 PLOG(ERROR) << "failed to mkdir " << dir_path << " when make parents for " << name;
116 return false;
117 }
118
119 LOG(INFO) << "created [" << dir_path << "]";
120 }
121 prev_end = next_end;
122 }
123 return true;
Tao Bao0c7839a2016-10-10 15:48:37 -0700124}
125
Doug Zongker3d177d02010-07-01 09:18:44 -0700126// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -0700127// mount(fs_type, partition_type, location, mount_point, mount_options)
Tianjie Xuc4447322017-03-06 14:44:59 -0800128
Doug Zongker3d177d02010-07-01 09:18:44 -0700129// fs_type="ext4" partition_type="EMMC" location=device
Tianjie Xuc4447322017-03-06 14:44:59 -0800130Value* MountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
131 if (argv.size() != 4 && argv.size() != 5) {
132 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %zu", name,
133 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800134 }
135
136 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800137 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800138 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
Tianjie Xuc4447322017-03-06 14:44:59 -0800146 if (argv.size() == 5) {
Tao Bao039f2da2016-11-22 16:29:50 -0800147 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) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700183 uiPrintf(state, "%s: Failed to mount %s at %s: %s", name, location.c_str(), mount_point.c_str(),
184 strerror(errno));
Tao Bao039f2da2016-11-22 16:29:50 -0800185 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// is_mounted(mount_point)
Tianjie Xuc4447322017-03-06 14:44:59 -0800192Value* IsMountedFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
193 if (argv.size() != 1) {
194 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800195 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700196
Tao Bao039f2da2016-11-22 16:29:50 -0800197 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800198 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800199 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
200 }
201 const std::string& mount_point = args[0];
202 if (mount_point.empty()) {
203 return ErrorAbort(state, kArgsParsingFailure,
204 "mount_point argument to unmount() can't be empty");
205 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700206
Tao Bao039f2da2016-11-22 16:29:50 -0800207 scan_mounted_volumes();
208 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
209 if (vol == nullptr) {
210 return StringValue("");
211 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700212
Tao Bao039f2da2016-11-22 16:29:50 -0800213 return StringValue(mount_point);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700214}
215
Tianjie Xuc4447322017-03-06 14:44:59 -0800216Value* UnmountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
217 if (argv.size() != 1) {
218 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800219 }
220 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800221 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800222 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
223 }
224 const std::string& mount_point = args[0];
225 if (mount_point.empty()) {
226 return ErrorAbort(state, kArgsParsingFailure,
227 "mount_point argument to unmount() can't be empty");
228 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700229
Tao Bao039f2da2016-11-22 16:29:50 -0800230 scan_mounted_volumes();
231 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
232 if (vol == nullptr) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700233 uiPrintf(state, "Failed to unmount %s: No such volume", mount_point.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -0800234 return nullptr;
235 } else {
236 int ret = unmount_mounted_volume(vol);
237 if (ret != 0) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700238 uiPrintf(state, "Failed to unmount %s: %s", mount_point.c_str(), strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700239 }
Tao Bao039f2da2016-11-22 16:29:50 -0800240 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700241
Tao Bao039f2da2016-11-22 16:29:50 -0800242 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700243}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700244
JP Abgrall37aedb32014-06-16 19:07:39 -0700245static int exec_cmd(const char* path, char* const argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800246 pid_t child;
247 if ((child = vfork()) == 0) {
248 execv(path, argv);
Tao Bao3da88012017-02-03 13:09:23 -0800249 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -0800250 }
251
252 int status;
253 waitpid(child, &status, 0);
254 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
255 LOG(ERROR) << path << " failed with status " << WEXITSTATUS(status);
256 }
257 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700258}
259
Stephen Smalley779701d2012-02-09 14:13:23 -0500260// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700261//
Tao Bao039f2da2016-11-22 16:29:50 -0800262// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
263// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700264// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800265// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700266// 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 -0800267Value* FormatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
268 if (argv.size() != 5) {
269 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %zu", name,
270 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800271 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500272
Tao Bao039f2da2016-11-22 16:29:50 -0800273 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800274 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800275 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";
Tao Bao397a8132017-05-12 11:02:27 -0700319 const char* f2fs_argv[] = {
320 "mkfs.f2fs", "-t", "-d1", location.c_str(), (size < 512) ? nullptr : num_sectors.c_str(),
321 nullptr
322 };
323 int status = exec_cmd(f2fs_path, const_cast<char**>(f2fs_argv));
Tao Bao039f2da2016-11-22 16:29:50 -0800324 if (status != 0) {
325 LOG(ERROR) << name << ": mkfs.f2fs failed (" << status << ") on " << location;
326 return StringValue("");
327 }
328 return StringValue(location);
329 } else {
330 LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \""
331 << partition_type << "\"";
332 }
333
334 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700335}
336
Tianjie Xuc4447322017-03-06 14:44:59 -0800337Value* ShowProgressFn(const char* name, State* state,
338 const std::vector<std::unique_ptr<Expr>>& argv) {
339 if (argv.size() != 2) {
340 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
341 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800342 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700343
Tao Bao039f2da2016-11-22 16:29:50 -0800344 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800345 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800346 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
347 }
348 const std::string& frac_str = args[0];
349 const std::string& sec_str = args[1];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700350
Tao Bao039f2da2016-11-22 16:29:50 -0800351 double frac;
352 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
353 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s\n", name,
354 frac_str.c_str());
355 }
356 int sec;
357 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
358 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s\n", name,
359 sec_str.c_str());
360 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700361
Tao Bao039f2da2016-11-22 16:29:50 -0800362 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
363 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700364
Tao Bao039f2da2016-11-22 16:29:50 -0800365 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700366}
367
Tianjie Xuc4447322017-03-06 14:44:59 -0800368Value* SetProgressFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
369 if (argv.size() != 1) {
370 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800371 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700372
Tao Bao039f2da2016-11-22 16:29:50 -0800373 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800374 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800375 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
376 }
377 const std::string& frac_str = args[0];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700378
Tao Bao039f2da2016-11-22 16:29:50 -0800379 double frac;
380 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
381 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s\n", name,
382 frac_str.c_str());
383 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700384
Tao Bao039f2da2016-11-22 16:29:50 -0800385 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
386 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700387
Tao Bao039f2da2016-11-22 16:29:50 -0800388 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700389}
390
Tao Baoef0eb3b2016-11-14 21:29:52 -0800391// package_extract_file(package_file[, dest_file])
392// Extracts a single package_file from the update package and writes it to dest_file,
393// overwriting existing files if necessary. Without the dest_file argument, returns the
394// contents of the package file as a binary blob.
Tianjie Xuc4447322017-03-06 14:44:59 -0800395Value* PackageExtractFileFn(const char* name, State* state,
396 const std::vector<std::unique_ptr<Expr>>& argv) {
397 if (argv.size() < 1 || argv.size() > 2) {
398 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name,
399 argv.size());
Tao Baoef0eb3b2016-11-14 21:29:52 -0800400 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700401
Tianjie Xuc4447322017-03-06 14:44:59 -0800402 if (argv.size() == 2) {
Tao Baoef0eb3b2016-11-14 21:29:52 -0800403 // The two-argument version extracts to a file.
404
405 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800406 if (!ReadArgs(state, argv, &args)) {
407 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
408 argv.size());
Doug Zongker8edb00c2009-06-11 17:21:44 -0700409 }
Tao Baoef0eb3b2016-11-14 21:29:52 -0800410 const std::string& zip_path = args[0];
411 const std::string& dest_path = args[1];
Doug Zongker43772d22014-06-09 14:13:19 -0700412
Tao Baoef0eb3b2016-11-14 21:29:52 -0800413 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
414 ZipString zip_string_path(zip_path.c_str());
415 ZipEntry entry;
416 if (FindEntry(za, zip_string_path, &entry) != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800417 LOG(ERROR) << name << ": no " << zip_path << " in package";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800418 return StringValue("");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700419 }
Tao Baoef0eb3b2016-11-14 21:29:52 -0800420
Tao Bao358c2ec2016-11-28 11:48:43 -0800421 unique_fd fd(TEMP_FAILURE_RETRY(
422 ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tao Baoef0eb3b2016-11-14 21:29:52 -0800423 if (fd == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800424 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800425 return StringValue("");
426 }
427
428 bool success = true;
429 int32_t ret = ExtractEntryToFile(za, &entry, fd);
430 if (ret != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800431 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
432 << entry.uncompressed_length << " bytes) to \"" << dest_path
433 << "\": " << ErrorCodeString(ret);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800434 success = false;
435 }
436 if (ota_fsync(fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800437 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800438 success = false;
439 }
440 if (ota_close(fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800441 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800442 success = false;
443 }
444
445 return StringValue(success ? "t" : "");
446 } else {
447 // The one-argument version returns the contents of the file as the result.
448
449 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800450 if (!ReadArgs(state, argv, &args)) {
451 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
452 argv.size());
Tao Baoef0eb3b2016-11-14 21:29:52 -0800453 }
454 const std::string& zip_path = args[0];
455
456 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
457 ZipString zip_string_path(zip_path.c_str());
458 ZipEntry entry;
459 if (FindEntry(za, zip_string_path, &entry) != 0) {
460 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
461 zip_path.c_str());
462 }
463
464 std::string buffer;
465 buffer.resize(entry.uncompressed_length);
466
467 int32_t ret = ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
468 if (ret != 0) {
469 return ErrorAbort(state, kPackageExtractFileFailure,
470 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
471 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
472 }
473
474 return new Value(VAL_BLOB, buffer);
475 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700476}
477
Tianjie Xuc4447322017-03-06 14:44:59 -0800478Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
479 if (argv.size() != 1) {
480 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800481 }
482 std::string key;
483 if (!Evaluate(state, argv[0], &key)) {
484 return nullptr;
485 }
486 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700487
Tao Bao039f2da2016-11-22 16:29:50 -0800488 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700489}
490
Doug Zongker47cace92009-06-18 10:11:50 -0700491// file_getprop(file, key)
492//
493// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -0700494// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700495// and returns the value for 'key' (or "" if it isn't defined).
Tianjie Xuc4447322017-03-06 14:44:59 -0800496Value* FileGetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
497 if (argv.size() != 2) {
498 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
499 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800500 }
501
502 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800503 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800504 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
505 }
506 const std::string& filename = args[0];
507 const std::string& key = args[1];
508
509 struct stat st;
510 if (stat(filename.c_str(), &st) < 0) {
511 return ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name,
512 filename.c_str(), strerror(errno));
513 }
514
515 constexpr off_t MAX_FILE_GETPROP_SIZE = 65536;
516 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
517 return ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %lld)",
518 filename.c_str(), name, static_cast<long long>(MAX_FILE_GETPROP_SIZE));
519 }
520
521 std::string buffer(st.st_size, '\0');
522 unique_file f(ota_fopen(filename.c_str(), "rb"));
523 if (f == nullptr) {
524 return ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename.c_str(),
525 strerror(errno));
526 }
527
528 if (ota_fread(&buffer[0], 1, st.st_size, f.get()) != static_cast<size_t>(st.st_size)) {
529 ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s", name,
530 static_cast<size_t>(st.st_size), filename.c_str());
531 return nullptr;
532 }
533
534 ota_fclose(f);
535
536 std::vector<std::string> lines = android::base::Split(buffer, "\n");
537 for (size_t i = 0; i < lines.size(); i++) {
538 std::string line = android::base::Trim(lines[i]);
539
540 // comment or blank line: skip to next line
541 if (line.empty() || line[0] == '#') {
542 continue;
543 }
544 size_t equal_pos = line.find('=');
545 if (equal_pos == std::string::npos) {
546 continue;
Tao Bao51d516e2016-11-03 14:49:01 -0700547 }
548
Tao Bao358c2ec2016-11-28 11:48:43 -0800549 // trim whitespace between key and '='
550 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -0700551
Tao Bao358c2ec2016-11-28 11:48:43 -0800552 // not the key we're looking for
553 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700554
Tao Bao358c2ec2016-11-28 11:48:43 -0800555 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
556 }
Doug Zongker47cace92009-06-18 10:11:50 -0700557
Tao Bao358c2ec2016-11-28 11:48:43 -0800558 return StringValue("");
Doug Zongker47cace92009-06-18 10:11:50 -0700559}
560
Doug Zongker8edb00c2009-06-11 17:21:44 -0700561// apply_patch_space(bytes)
Tianjie Xuc4447322017-03-06 14:44:59 -0800562Value* ApplyPatchSpaceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
563 if (argv.size() != 1) {
564 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name,
565 argv.size());
566 }
Tao Bao039f2da2016-11-22 16:29:50 -0800567 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800568 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800569 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
570 }
571 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800572
Tao Bao039f2da2016-11-22 16:29:50 -0800573 size_t bytes;
574 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
575 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n",
576 name, bytes_str.c_str());
577 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800578
Tao Bao039f2da2016-11-22 16:29:50 -0800579 return StringValue(CacheSizeCheck(bytes) ? "" : "t");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800580}
581
Tao Bao039f2da2016-11-22 16:29:50 -0800582// apply_patch(src_file, tgt_file, tgt_sha1, tgt_size, patch1_sha1, patch1_blob, [...])
583// Applies a binary patch to the src_file to produce the tgt_file. If the desired target is the
584// same as the source, pass "-" for tgt_file. tgt_sha1 and tgt_size are the expected final SHA1
585// hash and size of the target file. The remaining arguments must come in pairs: a SHA1 hash (a
586// 40-character hex string) and a blob. The blob is the patch to be applied when the source
587// file's current contents have the given SHA1.
588//
589// The patching is done in a safe manner that guarantees the target file either has the desired
590// SHA1 hash and size, or it is untouched -- it will not be left in an unrecoverable intermediate
591// state. If the process is interrupted during patching, the target file may be in an intermediate
592// state; a copy exists in the cache partition so restarting the update can successfully update
593// the file.
Tianjie Xuc4447322017-03-06 14:44:59 -0800594Value* ApplyPatchFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
595 if (argv.size() < 6 || (argv.size() % 2) == 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700596 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an "
Tianjie Xuc4447322017-03-06 14:44:59 -0800597 "even number, got %zu", name, argv.size());
Doug Zongker8edb00c2009-06-11 17:21:44 -0700598 }
599
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700600 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800601 if (!ReadArgs(state, argv, &args, 0, 4)) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700602 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700603 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700604 const std::string& source_filename = args[0];
605 const std::string& target_filename = args[1];
606 const std::string& target_sha1 = args[2];
607 const std::string& target_size_str = args[3];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700608
Tao Baob15fd222015-09-24 11:10:51 -0700609 size_t target_size;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700610 if (!android::base::ParseUint(target_size_str.c_str(), &target_size)) {
611 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count",
612 name, target_size_str.c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800613 }
614
Tianjie Xuc4447322017-03-06 14:44:59 -0800615 int patchcount = (argv.size()-4) / 2;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700616 std::vector<std::unique_ptr<Value>> arg_values;
Tianjie Xuc4447322017-03-06 14:44:59 -0800617 if (!ReadValueArgs(state, argv, &arg_values, 4, argv.size() - 4)) {
Yabin Cui64be2132016-02-03 18:16:02 -0800618 return nullptr;
619 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700620
Yabin Cui64be2132016-02-03 18:16:02 -0800621 for (int i = 0; i < patchcount; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700622 if (arg_values[i * 2]->type != VAL_STRING) {
623 return ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name,
624 i * 2);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800625 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700626 if (arg_values[i * 2 + 1]->type != VAL_BLOB) {
627 return ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name,
628 i * 2 + 1);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800629 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700630 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700631
Tianjie Xuaced5d92016-10-12 10:55:04 -0700632 std::vector<std::string> patch_sha_str;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700633 std::vector<std::unique_ptr<Value>> patches;
Yabin Cui64be2132016-02-03 18:16:02 -0800634 for (int i = 0; i < patchcount; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700635 patch_sha_str.push_back(arg_values[i * 2]->data);
636 patches.push_back(std::move(arg_values[i * 2 + 1]));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700637 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800638
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700639 int result = applypatch(source_filename.c_str(), target_filename.c_str(),
640 target_sha1.c_str(), target_size,
641 patch_sha_str, patches, nullptr);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800642
Tianjie Xuaced5d92016-10-12 10:55:04 -0700643 return StringValue(result == 0 ? "t" : "");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800644}
645
Tao Bao039f2da2016-11-22 16:29:50 -0800646// apply_patch_check(filename, [sha1, ...])
647// Returns true if the contents of filename or the temporary copy in the cache partition (if
648// present) have a SHA-1 checksum equal to one of the given sha1 values. sha1 values are
649// specified as 40 hex digits. This function differs from sha1_check(read_file(filename),
650// sha1 [, ...]) in that it knows to check the cache partition copy, so apply_patch_check() will
651// succeed even if the file was corrupted by an interrupted apply_patch() update.
Tianjie Xuc4447322017-03-06 14:44:59 -0800652Value* ApplyPatchCheckFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
653 if (argv.size() < 1) {
654 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %zu", name,
655 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800656 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800657
Tao Bao039f2da2016-11-22 16:29:50 -0800658 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800659 if (!ReadArgs(state, argv, &args, 0, 1)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800660 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
661 }
662 const std::string& filename = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800663
Tao Bao039f2da2016-11-22 16:29:50 -0800664 std::vector<std::string> sha1s;
Tao Baodb56eb02017-03-23 06:34:20 -0700665 if (argv.size() > 1 && !ReadArgs(state, argv, &sha1s, 1, argv.size() - 1)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800666 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
667 }
668 int result = applypatch_check(filename.c_str(), sha1s);
Tianjie Xuaced5d92016-10-12 10:55:04 -0700669
Tao Bao039f2da2016-11-22 16:29:50 -0800670 return StringValue(result == 0 ? "t" : "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700671}
672
Tao Bao0bbc7642017-03-29 23:57:47 -0700673// This is the updater side handler for ui_print() in edify script. Contents will be sent over to
674// the recovery side for on-screen display.
Tianjie Xuc4447322017-03-06 14:44:59 -0800675Value* UIPrintFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
Tao Bao039f2da2016-11-22 16:29:50 -0800676 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800677 if (!ReadArgs(state, argv, &args)) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700678 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tao Bao039f2da2016-11-22 16:29:50 -0800679 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700680
Tao Bao0bbc7642017-03-29 23:57:47 -0700681 std::string buffer = android::base::Join(args, "");
Tao Bao039f2da2016-11-22 16:29:50 -0800682 uiPrint(state, buffer);
683 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700684}
685
Tianjie Xuc4447322017-03-06 14:44:59 -0800686Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
687 if (!argv.empty()) {
688 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
689 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800690 }
691 fprintf(static_cast<UpdaterInfo*>(state->cookie)->cmd_pipe, "wipe_cache\n");
692 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700693}
694
Tianjie Xuc4447322017-03-06 14:44:59 -0800695Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
696 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800697 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
698 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700699
Tao Bao039f2da2016-11-22 16:29:50 -0800700 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800701 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800702 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
703 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700704
Tianjie Xuc4447322017-03-06 14:44:59 -0800705 char* args2[argv.size() + 1];
706 for (size_t i = 0; i < argv.size(); i++) {
Tao Bao039f2da2016-11-22 16:29:50 -0800707 args2[i] = &args[i][0];
708 }
Tianjie Xuc4447322017-03-06 14:44:59 -0800709 args2[argv.size()] = nullptr;
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700710
Tianjie Xuc4447322017-03-06 14:44:59 -0800711 LOG(INFO) << "about to run program [" << args2[0] << "] with " << argv.size() << " args";
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700712
Tao Bao039f2da2016-11-22 16:29:50 -0800713 pid_t child = fork();
714 if (child == 0) {
715 execv(args2[0], args2);
716 PLOG(ERROR) << "run_program: execv failed";
Tao Bao3da88012017-02-03 13:09:23 -0800717 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -0800718 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700719
Tao Bao039f2da2016-11-22 16:29:50 -0800720 int status;
721 waitpid(child, &status, 0);
722 if (WIFEXITED(status)) {
723 if (WEXITSTATUS(status) != 0) {
724 LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status);
725 }
726 } else if (WIFSIGNALED(status)) {
727 LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status);
728 }
729
730 return StringValue(std::to_string(status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700731}
732
Doug Zongker512536a2010-02-17 16:11:44 -0800733// sha1_check(data)
734// to return the sha1 of the data (given in the format returned by
735// read_file).
736//
737// sha1_check(data, sha1_hex, [sha1_hex, ...])
738// returns the sha1 of the file if it matches any of the hex
739// strings passed, or "" if it does not equal any of them.
740//
Tianjie Xuc4447322017-03-06 14:44:59 -0800741Value* Sha1CheckFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
742 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800743 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
744 }
Doug Zongker512536a2010-02-17 16:11:44 -0800745
Tao Bao039f2da2016-11-22 16:29:50 -0800746 std::vector<std::unique_ptr<Value>> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800747 if (!ReadValueArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800748 return nullptr;
749 }
Doug Zongker512536a2010-02-17 16:11:44 -0800750
Tao Bao039f2da2016-11-22 16:29:50 -0800751 if (args[0]->type == VAL_INVALID) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700752 return StringValue("");
Tao Bao039f2da2016-11-22 16:29:50 -0800753 }
754 uint8_t digest[SHA_DIGEST_LENGTH];
755 SHA1(reinterpret_cast<const uint8_t*>(args[0]->data.c_str()), args[0]->data.size(), digest);
756
Tianjie Xuc4447322017-03-06 14:44:59 -0800757 if (argv.size() == 1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800758 return StringValue(print_sha1(digest));
759 }
760
Tianjie Xuc4447322017-03-06 14:44:59 -0800761 for (size_t i = 1; i < argv.size(); ++i) {
Tao Bao039f2da2016-11-22 16:29:50 -0800762 uint8_t arg_digest[SHA_DIGEST_LENGTH];
763 if (args[i]->type != VAL_STRING) {
764 LOG(ERROR) << name << "(): arg " << i << " is not a string; skipping";
765 } else if (ParseSha1(args[i]->data.c_str(), arg_digest) != 0) {
766 // Warn about bad args and skip them.
767 LOG(ERROR) << name << "(): error parsing \"" << args[i]->data << "\" as sha-1; skipping";
768 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
769 // Found a match.
770 return args[i].release();
771 }
772 }
773
774 // Didn't match any of the hex strings; return false.
775 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -0800776}
777
Hristo Bojinovdb314d62010-08-02 10:29:49 -0700778// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -0800779// is actually a FileContents*).
Tianjie Xuc4447322017-03-06 14:44:59 -0800780Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
781 if (argv.size() != 1) {
782 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800783 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700784
Tao Bao039f2da2016-11-22 16:29:50 -0800785 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800786 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800787 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
788 }
789 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -0800790
Tao Bao039f2da2016-11-22 16:29:50 -0800791 Value* v = new Value(VAL_INVALID, "");
Doug Zongker512536a2010-02-17 16:11:44 -0800792
Tao Bao039f2da2016-11-22 16:29:50 -0800793 FileContents fc;
794 if (LoadFileContents(filename.c_str(), &fc) == 0) {
795 v->type = VAL_BLOB;
796 v->data = std::string(fc.data.begin(), fc.data.end());
797 }
798 return v;
Doug Zongker512536a2010-02-17 16:11:44 -0800799}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700800
Tao Baod0f30882016-11-03 23:52:01 -0700801// write_value(value, filename)
802// Writes 'value' to 'filename'.
803// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
Tianjie Xuc4447322017-03-06 14:44:59 -0800804Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
805 if (argv.size() != 2) {
806 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
807 argv.size());
Tao Baod0f30882016-11-03 23:52:01 -0700808 }
809
810 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800811 if (!ReadArgs(state, argv, &args)) {
Tao Baod0f30882016-11-03 23:52:01 -0700812 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
813 }
814
815 const std::string& filename = args[1];
816 if (filename.empty()) {
817 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
818 }
819
820 const std::string& value = args[0];
821 if (!android::base::WriteStringToFile(value, filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800822 PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\"";
Tao Baod0f30882016-11-03 23:52:01 -0700823 return StringValue("");
824 } else {
825 return StringValue("t");
826 }
827}
828
Doug Zongkerc87bab12013-11-25 13:53:25 -0800829// Immediately reboot the device. Recovery is not finished normally,
830// so if you reboot into recovery it will re-start applying the
831// current package (because nothing has cleared the copy of the
832// arguments stored in the BCB).
833//
834// The argument is the partition name passed to the android reboot
835// property. It can be "recovery" to boot from the recovery
836// partition, or "" (empty string) to boot from the regular boot
837// partition.
Tianjie Xuc4447322017-03-06 14:44:59 -0800838Value* RebootNowFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
839 if (argv.size() != 2) {
840 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
841 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800842 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800843
Tao Baobedf5fc2016-11-18 12:01:26 -0800844 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800845 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800846 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
847 }
848 const std::string& filename = args[0];
849 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800850
Tao Baobedf5fc2016-11-18 12:01:26 -0800851 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
852 bootloader_message boot;
853 std::string err;
854 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800855 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800856 return StringValue("");
857 }
858 memset(boot.command, 0, sizeof(boot.command));
859 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800860 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800861 return StringValue("");
862 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800863
Dmitri Plotnikov8706a982017-04-18 08:28:26 -0700864 std::string reboot_cmd = "reboot," + property;
865 if (android::base::GetBoolProperty("ro.boot.quiescent", false)) {
866 reboot_cmd += ",quiescent";
867 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800868 android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800869
Tao Baobedf5fc2016-11-18 12:01:26 -0800870 sleep(5);
871 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800872}
873
874// Store a string value somewhere that future invocations of recovery
875// can access it. This value is called the "stage" and can be used to
876// drive packages that need to do reboots in the middle of
877// installation and keep track of where they are in the multi-stage
878// install.
879//
880// The first argument is the block device for the misc partition
881// ("/misc" in the fstab), which is where this value is stored. The
882// second argument is the string to store; it should not exceed 31
883// bytes.
Tianjie Xuc4447322017-03-06 14:44:59 -0800884Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
885 if (argv.size() != 2) {
886 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
887 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800888 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800889
Tao Baobedf5fc2016-11-18 12:01:26 -0800890 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800891 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800892 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
893 }
894 const std::string& filename = args[0];
895 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800896
Tao Baobedf5fc2016-11-18 12:01:26 -0800897 // Store this value in the misc partition, immediately after the
898 // bootloader message that the main recovery uses to save its
899 // arguments in case of the device restarting midway through
900 // package installation.
901 bootloader_message boot;
902 std::string err;
903 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800904 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800905 return StringValue("");
906 }
907 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
908 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800909 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800910 return StringValue("");
911 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800912
Tao Baobedf5fc2016-11-18 12:01:26 -0800913 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800914}
915
916// Return the value most recently saved with SetStageFn. The argument
917// is the block device for the misc partition.
Tianjie Xuc4447322017-03-06 14:44:59 -0800918Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
919 if (argv.size() != 1) {
920 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800921 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800922
Tao Baobedf5fc2016-11-18 12:01:26 -0800923 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800924 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800925 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
926 }
927 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800928
Tao Baobedf5fc2016-11-18 12:01:26 -0800929 bootloader_message boot;
930 std::string err;
931 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800932 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800933 return StringValue("");
934 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800935
Tao Baobedf5fc2016-11-18 12:01:26 -0800936 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800937}
938
Tianjie Xuc4447322017-03-06 14:44:59 -0800939Value* WipeBlockDeviceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
940 if (argv.size() != 2) {
941 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
942 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800943 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800944
Tao Bao358c2ec2016-11-28 11:48:43 -0800945 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800946 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800947 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
948 }
949 const std::string& filename = args[0];
950 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800951
Tao Bao358c2ec2016-11-28 11:48:43 -0800952 size_t len;
953 if (!android::base::ParseUint(len_str.c_str(), &len)) {
954 return nullptr;
955 }
956 unique_fd fd(ota_open(filename.c_str(), O_WRONLY, 0644));
957 // The wipe_block_device function in ext4_utils returns 0 on success and 1
958 // for failure.
959 int status = wipe_block_device(fd, len);
960 return StringValue((status == 0) ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800961}
962
Tianjie Xuc4447322017-03-06 14:44:59 -0800963Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
964 if (!argv.empty()) {
965 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
966 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800967 }
968 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
969 fprintf(ui->cmd_pipe, "enable_reboot\n");
970 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -0700971}
972
Tianjie Xuc4447322017-03-06 14:44:59 -0800973Value* Tune2FsFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
974 if (argv.empty()) {
975 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800976 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800977
Tao Bao039f2da2016-11-22 16:29:50 -0800978 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800979 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800980 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
981 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800982
Tianjie Xuc4447322017-03-06 14:44:59 -0800983 char* args2[argv.size() + 1];
Tao Bao039f2da2016-11-22 16:29:50 -0800984 // Tune2fs expects the program name as its args[0]
985 args2[0] = const_cast<char*>(name);
986 if (args2[0] == nullptr) {
987 return nullptr;
988 }
Tianjie Xuc4447322017-03-06 14:44:59 -0800989 for (size_t i = 0; i < argv.size(); ++i) {
Tao Bao039f2da2016-11-22 16:29:50 -0800990 args2[i + 1] = &args[i][0];
991 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800992
Tao Bao039f2da2016-11-22 16:29:50 -0800993 // tune2fs changes the file system parameters on an ext2 file system; it
994 // returns 0 on success.
Tianjie Xuc4447322017-03-06 14:44:59 -0800995 int result = tune2fs_main(argv.size() + 1, args2);
Tao Bao039f2da2016-11-22 16:29:50 -0800996 if (result != 0) {
997 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
998 }
999 return StringValue("t");
Michael Rungeacf47db2014-11-21 00:12:28 -08001000}
1001
Doug Zongker9931f7f2009-06-10 14:11:53 -07001002void RegisterInstallFunctions() {
Tao Bao039f2da2016-11-22 16:29:50 -08001003 RegisterFunction("mount", MountFn);
1004 RegisterFunction("is_mounted", IsMountedFn);
1005 RegisterFunction("unmount", UnmountFn);
1006 RegisterFunction("format", FormatFn);
1007 RegisterFunction("show_progress", ShowProgressFn);
1008 RegisterFunction("set_progress", SetProgressFn);
Tao Bao039f2da2016-11-22 16:29:50 -08001009 RegisterFunction("package_extract_file", PackageExtractFileFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001010
Tao Bao039f2da2016-11-22 16:29:50 -08001011 RegisterFunction("getprop", GetPropFn);
1012 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001013
Tao Bao039f2da2016-11-22 16:29:50 -08001014 RegisterFunction("apply_patch", ApplyPatchFn);
1015 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1016 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001017
Tao Bao039f2da2016-11-22 16:29:50 -08001018 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001019
Tao Bao039f2da2016-11-22 16:29:50 -08001020 RegisterFunction("read_file", ReadFileFn);
1021 RegisterFunction("sha1_check", Sha1CheckFn);
Tao Bao039f2da2016-11-22 16:29:50 -08001022 RegisterFunction("write_value", WriteValueFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001023
Tao Bao039f2da2016-11-22 16:29:50 -08001024 RegisterFunction("wipe_cache", WipeCacheFn);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001025
Tao Bao039f2da2016-11-22 16:29:50 -08001026 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001027
Tao Bao039f2da2016-11-22 16:29:50 -08001028 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001029
Tao Bao039f2da2016-11-22 16:29:50 -08001030 RegisterFunction("reboot_now", RebootNowFn);
1031 RegisterFunction("get_stage", GetStageFn);
1032 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001033
Tao Bao039f2da2016-11-22 16:29:50 -08001034 RegisterFunction("enable_reboot", EnableRebootFn);
1035 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001036}