blob: b83d30ff38d592c1c47093fab37fc74f20ca8bee [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/wipe.h>
Tao Bao361342c2016-02-08 11:15:50 -080053#include <openssl/sha.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070054#include <selinux/label.h>
55#include <selinux/selinux.h>
Tao Bao09e468f2017-09-29 14:39:33 -070056#include <tune2fs.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"
Elliott Hughes63a31922016-06-09 17:41:22 -070060#include "mounts.h"
Tao Baod33b2f82017-09-28 21:29:11 -070061#include "otafault/ota_io.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070062#include "otautil/DirUtil.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070063#include "otautil/error_code.h"
Tao Bao09e468f2017-09-29 14:39:33 -070064#include "otautil/print_sha1.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070065#include "updater/updater.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070066
Tao Bao1107d962015-09-09 17:16:55 -070067// Send over the buffer to recovery though the command pipe.
68static void uiPrint(State* state, const std::string& buffer) {
Tao Bao039f2da2016-11-22 16:29:50 -080069 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070070
Tao Bao039f2da2016-11-22 16:29:50 -080071 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
72 // So skip sending empty strings to UI.
73 std::vector<std::string> lines = android::base::Split(buffer, "\n");
74 for (auto& line : lines) {
75 if (!line.empty()) {
76 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
Tao Bao1107d962015-09-09 17:16:55 -070077 }
Tao Bao039f2da2016-11-22 16:29:50 -080078 }
Tao Bao1107d962015-09-09 17:16:55 -070079
Tao Bao039f2da2016-11-22 16:29:50 -080080 // On the updater side, we need to dump the contents to stderr (which has
81 // been redirected to the log file). Because the recovery will only print
82 // the contents to screen when processing pipe command ui_print.
83 LOG(INFO) << buffer;
Michael Runged4a63422014-10-22 19:48:41 -070084}
85
Elliott Hughes83ce7552016-06-30 09:28:42 -070086void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) {
Tao Bao039f2da2016-11-22 16:29:50 -080087 std::string error_msg;
Tao Bao1107d962015-09-09 17:16:55 -070088
Tao Bao039f2da2016-11-22 16:29:50 -080089 va_list ap;
90 va_start(ap, format);
91 android::base::StringAppendV(&error_msg, format, ap);
92 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070093
Tao Bao039f2da2016-11-22 16:29:50 -080094 uiPrint(state, error_msg);
Michael Runged4a63422014-10-22 19:48:41 -070095}
96
Doug Zongker3d177d02010-07-01 09:18:44 -070097// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -070098// mount(fs_type, partition_type, location, mount_point, mount_options)
Tianjie Xuc4447322017-03-06 14:44:59 -080099
Doug Zongker3d177d02010-07-01 09:18:44 -0700100// fs_type="ext4" partition_type="EMMC" location=device
Tianjie Xuc4447322017-03-06 14:44:59 -0800101Value* MountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
102 if (argv.size() != 4 && argv.size() != 5) {
103 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %zu", name,
104 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800105 }
106
107 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800108 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800109 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
110 }
111 const std::string& fs_type = args[0];
112 const std::string& partition_type = args[1];
113 const std::string& location = args[2];
114 const std::string& mount_point = args[3];
115 std::string mount_options;
116
Tianjie Xuc4447322017-03-06 14:44:59 -0800117 if (argv.size() == 5) {
Tao Bao039f2da2016-11-22 16:29:50 -0800118 mount_options = args[4];
119 }
120
121 if (fs_type.empty()) {
122 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
123 }
124 if (partition_type.empty()) {
125 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
126 name);
127 }
128 if (location.empty()) {
129 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
130 }
131 if (mount_point.empty()) {
132 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
133 name);
134 }
135
136 {
137 char* secontext = nullptr;
138
139 if (sehandle) {
140 selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755);
141 setfscreatecon(secontext);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700142 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700143
Tao Bao039f2da2016-11-22 16:29:50 -0800144 mkdir(mount_point.c_str(), 0755);
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700145
Tao Bao039f2da2016-11-22 16:29:50 -0800146 if (secontext) {
147 freecon(secontext);
148 setfscreatecon(nullptr);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700149 }
Tao Bao039f2da2016-11-22 16:29:50 -0800150 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700151
Tao Bao039f2da2016-11-22 16:29:50 -0800152 if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(),
153 MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700154 uiPrintf(state, "%s: Failed to mount %s at %s: %s", name, location.c_str(), mount_point.c_str(),
155 strerror(errno));
Tao Bao039f2da2016-11-22 16:29:50 -0800156 return StringValue("");
157 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700158
Tao Bao039f2da2016-11-22 16:29:50 -0800159 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700160}
161
Doug Zongker8edb00c2009-06-11 17:21:44 -0700162// is_mounted(mount_point)
Tianjie Xuc4447322017-03-06 14:44:59 -0800163Value* IsMountedFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
164 if (argv.size() != 1) {
165 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800166 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700167
Tao Bao039f2da2016-11-22 16:29:50 -0800168 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800169 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800170 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
171 }
172 const std::string& mount_point = args[0];
173 if (mount_point.empty()) {
174 return ErrorAbort(state, kArgsParsingFailure,
175 "mount_point argument to unmount() can't be empty");
176 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700177
Tao Bao039f2da2016-11-22 16:29:50 -0800178 scan_mounted_volumes();
179 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
180 if (vol == nullptr) {
181 return StringValue("");
182 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700183
Tao Bao039f2da2016-11-22 16:29:50 -0800184 return StringValue(mount_point);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700185}
186
Tianjie Xuc4447322017-03-06 14:44:59 -0800187Value* UnmountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
188 if (argv.size() != 1) {
189 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800190 }
191 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800192 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800193 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
194 }
195 const std::string& mount_point = args[0];
196 if (mount_point.empty()) {
197 return ErrorAbort(state, kArgsParsingFailure,
198 "mount_point argument to unmount() can't be empty");
199 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700200
Tao Bao039f2da2016-11-22 16:29:50 -0800201 scan_mounted_volumes();
202 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
203 if (vol == nullptr) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700204 uiPrintf(state, "Failed to unmount %s: No such volume", mount_point.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -0800205 return nullptr;
206 } else {
207 int ret = unmount_mounted_volume(vol);
208 if (ret != 0) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700209 uiPrintf(state, "Failed to unmount %s: %s", mount_point.c_str(), strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700210 }
Tao Bao039f2da2016-11-22 16:29:50 -0800211 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700212
Tao Bao039f2da2016-11-22 16:29:50 -0800213 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700214}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700215
JP Abgrall37aedb32014-06-16 19:07:39 -0700216static int exec_cmd(const char* path, char* const argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800217 pid_t child;
218 if ((child = vfork()) == 0) {
219 execv(path, argv);
Tao Bao3da88012017-02-03 13:09:23 -0800220 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -0800221 }
222
223 int status;
224 waitpid(child, &status, 0);
225 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
226 LOG(ERROR) << path << " failed with status " << WEXITSTATUS(status);
227 }
228 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700229}
230
Stephen Smalley779701d2012-02-09 14:13:23 -0500231// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700232//
Tao Bao039f2da2016-11-22 16:29:50 -0800233// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
234// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700235// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800236// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700237// 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 -0800238Value* FormatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
239 if (argv.size() != 5) {
240 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %zu", name,
241 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800242 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500243
Tao Bao039f2da2016-11-22 16:29:50 -0800244 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800245 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800246 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
247 }
248 const std::string& fs_type = args[0];
249 const std::string& partition_type = args[1];
250 const std::string& location = args[2];
251 const std::string& fs_size = args[3];
252 const std::string& mount_point = args[4];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700253
Tao Bao039f2da2016-11-22 16:29:50 -0800254 if (fs_type.empty()) {
255 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
256 }
257 if (partition_type.empty()) {
258 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
259 name);
260 }
261 if (location.empty()) {
262 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
263 }
264 if (mount_point.empty()) {
265 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
266 name);
267 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700268
Tao Bao039f2da2016-11-22 16:29:50 -0800269 int64_t size;
270 if (!android::base::ParseInt(fs_size, &size)) {
271 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s\n", name,
272 fs_size.c_str());
273 }
274
275 if (fs_type == "ext4") {
Jin Qianded2dac2017-04-21 14:36:12 -0700276 const char* mke2fs_argv[] = { "/sbin/mke2fs_static", "-t", "ext4", "-b", "4096",
277 location.c_str(), nullptr, nullptr };
278 std::string size_str;
279 if (size != 0) {
280 size_str = std::to_string(size / 4096LL);
281 mke2fs_argv[6] = size_str.c_str();
282 }
283
284 int status = exec_cmd(mke2fs_argv[0], const_cast<char**>(mke2fs_argv));
Tao Bao039f2da2016-11-22 16:29:50 -0800285 if (status != 0) {
Jin Qian502fd1c2017-11-02 11:58:12 -0700286 LOG(ERROR) << name << ": mke2fs failed (" << status << ") on " << location;
287 return StringValue("");
Jin Qianded2dac2017-04-21 14:36:12 -0700288 }
289
Tao Bao338be532017-07-11 16:45:04 -0700290 const char* e2fsdroid_argv[] = { "/sbin/e2fsdroid_static", "-e", "-a", mount_point.c_str(),
Jin Qianded2dac2017-04-21 14:36:12 -0700291 location.c_str(), nullptr };
292 status = exec_cmd(e2fsdroid_argv[0], const_cast<char**>(e2fsdroid_argv));
293 if (status != 0) {
294 LOG(ERROR) << name << ": e2fsdroid failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800295 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700296 }
Tao Bao039f2da2016-11-22 16:29:50 -0800297 return StringValue(location);
298 } else if (fs_type == "f2fs") {
299 if (size < 0) {
300 LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size;
301 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700302 }
Tao Bao039f2da2016-11-22 16:29:50 -0800303 std::string num_sectors = std::to_string(size / 512);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700304
Tao Bao039f2da2016-11-22 16:29:50 -0800305 const char* f2fs_path = "/sbin/mkfs.f2fs";
Jaegeuk Kim1a8bb0f2017-11-01 13:24:43 -0700306 const char* f2fs_argv[] = { "mkfs.f2fs",
307 "-d1",
308 "-f",
309 "-O",
310 "encrypt",
311 "-O",
312 "quota",
313 location.c_str(),
314 (size < 512) ? nullptr : num_sectors.c_str(),
315 nullptr };
Tao Bao397a8132017-05-12 11:02:27 -0700316 int status = exec_cmd(f2fs_path, const_cast<char**>(f2fs_argv));
Tao Bao039f2da2016-11-22 16:29:50 -0800317 if (status != 0) {
318 LOG(ERROR) << name << ": mkfs.f2fs failed (" << status << ") on " << location;
319 return StringValue("");
320 }
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800321
322 const char* sload_argv[] = { "/sbin/sload.f2fs", "-t", mount_point.c_str(), location.c_str(),
323 nullptr };
324 status = exec_cmd(sload_argv[0], const_cast<char**>(sload_argv));
325 if (status != 0) {
326 LOG(ERROR) << name << ": sload.f2fs failed (" << status << ") on " << location;
327 return StringValue("");
328 }
329
Tao Bao039f2da2016-11-22 16:29:50 -0800330 return StringValue(location);
331 } else {
332 LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \""
333 << partition_type << "\"";
334 }
335
336 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700337}
338
Tianjie Xuc4447322017-03-06 14:44:59 -0800339Value* ShowProgressFn(const char* name, State* state,
340 const std::vector<std::unique_ptr<Expr>>& argv) {
341 if (argv.size() != 2) {
342 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
343 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800344 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700345
Tao Bao039f2da2016-11-22 16:29:50 -0800346 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800347 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800348 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
349 }
350 const std::string& frac_str = args[0];
351 const std::string& sec_str = args[1];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700352
Tao Bao039f2da2016-11-22 16:29:50 -0800353 double frac;
354 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
355 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s\n", name,
356 frac_str.c_str());
357 }
358 int sec;
359 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
360 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s\n", name,
361 sec_str.c_str());
362 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700363
Tao Bao039f2da2016-11-22 16:29:50 -0800364 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
365 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700366
Tao Bao039f2da2016-11-22 16:29:50 -0800367 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700368}
369
Tianjie Xuc4447322017-03-06 14:44:59 -0800370Value* SetProgressFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
371 if (argv.size() != 1) {
372 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800373 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700374
Tao Bao039f2da2016-11-22 16:29:50 -0800375 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800376 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800377 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
378 }
379 const std::string& frac_str = args[0];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700380
Tao Bao039f2da2016-11-22 16:29:50 -0800381 double frac;
382 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
383 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s\n", name,
384 frac_str.c_str());
385 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700386
Tao Bao039f2da2016-11-22 16:29:50 -0800387 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
388 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700389
Tao Bao039f2da2016-11-22 16:29:50 -0800390 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700391}
392
Tao Baoef0eb3b2016-11-14 21:29:52 -0800393// package_extract_file(package_file[, dest_file])
394// Extracts a single package_file from the update package and writes it to dest_file,
395// overwriting existing files if necessary. Without the dest_file argument, returns the
396// contents of the package file as a binary blob.
Tianjie Xuc4447322017-03-06 14:44:59 -0800397Value* PackageExtractFileFn(const char* name, State* state,
398 const std::vector<std::unique_ptr<Expr>>& argv) {
399 if (argv.size() < 1 || argv.size() > 2) {
400 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name,
401 argv.size());
Tao Baoef0eb3b2016-11-14 21:29:52 -0800402 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700403
Tianjie Xuc4447322017-03-06 14:44:59 -0800404 if (argv.size() == 2) {
Tao Baoef0eb3b2016-11-14 21:29:52 -0800405 // The two-argument version extracts to a file.
406
407 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800408 if (!ReadArgs(state, argv, &args)) {
409 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
410 argv.size());
Doug Zongker8edb00c2009-06-11 17:21:44 -0700411 }
Tao Baoef0eb3b2016-11-14 21:29:52 -0800412 const std::string& zip_path = args[0];
413 const std::string& dest_path = args[1];
Doug Zongker43772d22014-06-09 14:13:19 -0700414
Tao Baoef0eb3b2016-11-14 21:29:52 -0800415 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
416 ZipString zip_string_path(zip_path.c_str());
417 ZipEntry entry;
418 if (FindEntry(za, zip_string_path, &entry) != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800419 LOG(ERROR) << name << ": no " << zip_path << " in package";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800420 return StringValue("");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700421 }
Tao Baoef0eb3b2016-11-14 21:29:52 -0800422
Tao Bao358c2ec2016-11-28 11:48:43 -0800423 unique_fd fd(TEMP_FAILURE_RETRY(
424 ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tao Baoef0eb3b2016-11-14 21:29:52 -0800425 if (fd == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800426 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800427 return StringValue("");
428 }
429
430 bool success = true;
431 int32_t ret = ExtractEntryToFile(za, &entry, fd);
432 if (ret != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800433 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
434 << entry.uncompressed_length << " bytes) to \"" << dest_path
435 << "\": " << ErrorCodeString(ret);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800436 success = false;
437 }
438 if (ota_fsync(fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800439 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800440 success = false;
441 }
442 if (ota_close(fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800443 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800444 success = false;
445 }
446
447 return StringValue(success ? "t" : "");
448 } else {
449 // The one-argument version returns the contents of the file as the result.
450
451 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800452 if (!ReadArgs(state, argv, &args)) {
453 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
454 argv.size());
Tao Baoef0eb3b2016-11-14 21:29:52 -0800455 }
456 const std::string& zip_path = args[0];
457
458 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
459 ZipString zip_string_path(zip_path.c_str());
460 ZipEntry entry;
461 if (FindEntry(za, zip_string_path, &entry) != 0) {
462 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
463 zip_path.c_str());
464 }
465
466 std::string buffer;
467 buffer.resize(entry.uncompressed_length);
468
469 int32_t ret = ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
470 if (ret != 0) {
471 return ErrorAbort(state, kPackageExtractFileFailure,
472 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
473 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
474 }
475
476 return new Value(VAL_BLOB, buffer);
477 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700478}
479
Tianjie Xuc4447322017-03-06 14:44:59 -0800480Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
481 if (argv.size() != 1) {
482 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800483 }
484 std::string key;
485 if (!Evaluate(state, argv[0], &key)) {
486 return nullptr;
487 }
488 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700489
Tao Bao039f2da2016-11-22 16:29:50 -0800490 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700491}
492
Doug Zongker47cace92009-06-18 10:11:50 -0700493// file_getprop(file, key)
494//
495// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -0700496// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700497// and returns the value for 'key' (or "" if it isn't defined).
Tianjie Xuc4447322017-03-06 14:44:59 -0800498Value* FileGetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
499 if (argv.size() != 2) {
500 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
501 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800502 }
503
504 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800505 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800506 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
507 }
508 const std::string& filename = args[0];
509 const std::string& key = args[1];
510
511 struct stat st;
512 if (stat(filename.c_str(), &st) < 0) {
513 return ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name,
514 filename.c_str(), strerror(errno));
515 }
516
517 constexpr off_t MAX_FILE_GETPROP_SIZE = 65536;
518 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
519 return ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %lld)",
520 filename.c_str(), name, static_cast<long long>(MAX_FILE_GETPROP_SIZE));
521 }
522
523 std::string buffer(st.st_size, '\0');
524 unique_file f(ota_fopen(filename.c_str(), "rb"));
525 if (f == nullptr) {
526 return ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename.c_str(),
527 strerror(errno));
528 }
529
530 if (ota_fread(&buffer[0], 1, st.st_size, f.get()) != static_cast<size_t>(st.st_size)) {
531 ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s", name,
532 static_cast<size_t>(st.st_size), filename.c_str());
533 return nullptr;
534 }
535
536 ota_fclose(f);
537
538 std::vector<std::string> lines = android::base::Split(buffer, "\n");
539 for (size_t i = 0; i < lines.size(); i++) {
540 std::string line = android::base::Trim(lines[i]);
541
542 // comment or blank line: skip to next line
543 if (line.empty() || line[0] == '#') {
544 continue;
545 }
546 size_t equal_pos = line.find('=');
547 if (equal_pos == std::string::npos) {
548 continue;
Tao Bao51d516e2016-11-03 14:49:01 -0700549 }
550
Tao Bao358c2ec2016-11-28 11:48:43 -0800551 // trim whitespace between key and '='
552 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -0700553
Tao Bao358c2ec2016-11-28 11:48:43 -0800554 // not the key we're looking for
555 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700556
Tao Bao358c2ec2016-11-28 11:48:43 -0800557 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
558 }
Doug Zongker47cace92009-06-18 10:11:50 -0700559
Tao Bao358c2ec2016-11-28 11:48:43 -0800560 return StringValue("");
Doug Zongker47cace92009-06-18 10:11:50 -0700561}
562
Doug Zongker8edb00c2009-06-11 17:21:44 -0700563// apply_patch_space(bytes)
Tianjie Xuc4447322017-03-06 14:44:59 -0800564Value* ApplyPatchSpaceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
565 if (argv.size() != 1) {
566 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name,
567 argv.size());
568 }
Tao Bao039f2da2016-11-22 16:29:50 -0800569 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800570 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800571 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
572 }
573 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800574
Tao Bao039f2da2016-11-22 16:29:50 -0800575 size_t bytes;
576 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
577 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n",
578 name, bytes_str.c_str());
579 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800580
Tianjie Xu99b73be2017-11-28 17:23:06 -0800581 // Skip the cache size check if the update is a retry.
582 if (state->is_retry || CacheSizeCheck(bytes) == 0) {
583 return StringValue("t");
584 }
585 return StringValue("");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800586}
587
Tao Bao039f2da2016-11-22 16:29:50 -0800588// apply_patch(src_file, tgt_file, tgt_sha1, tgt_size, patch1_sha1, patch1_blob, [...])
589// Applies a binary patch to the src_file to produce the tgt_file. If the desired target is the
590// same as the source, pass "-" for tgt_file. tgt_sha1 and tgt_size are the expected final SHA1
591// hash and size of the target file. The remaining arguments must come in pairs: a SHA1 hash (a
592// 40-character hex string) and a blob. The blob is the patch to be applied when the source
593// file's current contents have the given SHA1.
594//
595// The patching is done in a safe manner that guarantees the target file either has the desired
596// SHA1 hash and size, or it is untouched -- it will not be left in an unrecoverable intermediate
597// state. If the process is interrupted during patching, the target file may be in an intermediate
598// state; a copy exists in the cache partition so restarting the update can successfully update
599// the file.
Tianjie Xuc4447322017-03-06 14:44:59 -0800600Value* ApplyPatchFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
601 if (argv.size() < 6 || (argv.size() % 2) == 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700602 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an "
Tianjie Xuc4447322017-03-06 14:44:59 -0800603 "even number, got %zu", name, argv.size());
Doug Zongker8edb00c2009-06-11 17:21:44 -0700604 }
605
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700606 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800607 if (!ReadArgs(state, argv, &args, 0, 4)) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700608 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700609 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700610 const std::string& source_filename = args[0];
611 const std::string& target_filename = args[1];
612 const std::string& target_sha1 = args[2];
613 const std::string& target_size_str = args[3];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700614
Tao Baob15fd222015-09-24 11:10:51 -0700615 size_t target_size;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700616 if (!android::base::ParseUint(target_size_str.c_str(), &target_size)) {
617 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count",
618 name, target_size_str.c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800619 }
620
Tianjie Xuc4447322017-03-06 14:44:59 -0800621 int patchcount = (argv.size()-4) / 2;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700622 std::vector<std::unique_ptr<Value>> arg_values;
Tianjie Xuc4447322017-03-06 14:44:59 -0800623 if (!ReadValueArgs(state, argv, &arg_values, 4, argv.size() - 4)) {
Yabin Cui64be2132016-02-03 18:16:02 -0800624 return nullptr;
625 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700626
Yabin Cui64be2132016-02-03 18:16:02 -0800627 for (int i = 0; i < patchcount; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700628 if (arg_values[i * 2]->type != VAL_STRING) {
629 return ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name,
630 i * 2);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800631 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700632 if (arg_values[i * 2 + 1]->type != VAL_BLOB) {
633 return ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name,
634 i * 2 + 1);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800635 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700636 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700637
Tianjie Xuaced5d92016-10-12 10:55:04 -0700638 std::vector<std::string> patch_sha_str;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700639 std::vector<std::unique_ptr<Value>> patches;
Yabin Cui64be2132016-02-03 18:16:02 -0800640 for (int i = 0; i < patchcount; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700641 patch_sha_str.push_back(arg_values[i * 2]->data);
642 patches.push_back(std::move(arg_values[i * 2 + 1]));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700643 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800644
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700645 int result = applypatch(source_filename.c_str(), target_filename.c_str(),
646 target_sha1.c_str(), target_size,
647 patch_sha_str, patches, nullptr);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800648
Tianjie Xuaced5d92016-10-12 10:55:04 -0700649 return StringValue(result == 0 ? "t" : "");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800650}
651
Tao Bao039f2da2016-11-22 16:29:50 -0800652// apply_patch_check(filename, [sha1, ...])
653// Returns true if the contents of filename or the temporary copy in the cache partition (if
654// present) have a SHA-1 checksum equal to one of the given sha1 values. sha1 values are
655// specified as 40 hex digits. This function differs from sha1_check(read_file(filename),
656// sha1 [, ...]) in that it knows to check the cache partition copy, so apply_patch_check() will
657// succeed even if the file was corrupted by an interrupted apply_patch() update.
Tianjie Xuc4447322017-03-06 14:44:59 -0800658Value* ApplyPatchCheckFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
659 if (argv.size() < 1) {
660 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %zu", name,
661 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800662 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800663
Tao Bao039f2da2016-11-22 16:29:50 -0800664 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800665 if (!ReadArgs(state, argv, &args, 0, 1)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800666 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
667 }
668 const std::string& filename = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800669
Tao Bao039f2da2016-11-22 16:29:50 -0800670 std::vector<std::string> sha1s;
Tao Baodb56eb02017-03-23 06:34:20 -0700671 if (argv.size() > 1 && !ReadArgs(state, argv, &sha1s, 1, argv.size() - 1)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800672 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
673 }
674 int result = applypatch_check(filename.c_str(), sha1s);
Tianjie Xuaced5d92016-10-12 10:55:04 -0700675
Tao Bao039f2da2016-11-22 16:29:50 -0800676 return StringValue(result == 0 ? "t" : "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700677}
678
Tao Bao0bbc7642017-03-29 23:57:47 -0700679// This is the updater side handler for ui_print() in edify script. Contents will be sent over to
680// the recovery side for on-screen display.
Tianjie Xuc4447322017-03-06 14:44:59 -0800681Value* UIPrintFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
Tao Bao039f2da2016-11-22 16:29:50 -0800682 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800683 if (!ReadArgs(state, argv, &args)) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700684 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tao Bao039f2da2016-11-22 16:29:50 -0800685 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700686
Tao Bao0bbc7642017-03-29 23:57:47 -0700687 std::string buffer = android::base::Join(args, "");
Tao Bao039f2da2016-11-22 16:29:50 -0800688 uiPrint(state, buffer);
689 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700690}
691
Tianjie Xuc4447322017-03-06 14:44:59 -0800692Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
693 if (!argv.empty()) {
694 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
695 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800696 }
697 fprintf(static_cast<UpdaterInfo*>(state->cookie)->cmd_pipe, "wipe_cache\n");
698 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700699}
700
Tianjie Xuc4447322017-03-06 14:44:59 -0800701Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
702 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800703 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
704 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700705
Tao Bao039f2da2016-11-22 16:29:50 -0800706 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800707 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800708 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
709 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700710
Tianjie Xuc4447322017-03-06 14:44:59 -0800711 char* args2[argv.size() + 1];
712 for (size_t i = 0; i < argv.size(); i++) {
Tao Bao039f2da2016-11-22 16:29:50 -0800713 args2[i] = &args[i][0];
714 }
Tianjie Xuc4447322017-03-06 14:44:59 -0800715 args2[argv.size()] = nullptr;
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700716
Tianjie Xuc4447322017-03-06 14:44:59 -0800717 LOG(INFO) << "about to run program [" << args2[0] << "] with " << argv.size() << " args";
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700718
Tao Bao039f2da2016-11-22 16:29:50 -0800719 pid_t child = fork();
720 if (child == 0) {
721 execv(args2[0], args2);
722 PLOG(ERROR) << "run_program: execv failed";
Tao Bao3da88012017-02-03 13:09:23 -0800723 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -0800724 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700725
Tao Bao039f2da2016-11-22 16:29:50 -0800726 int status;
727 waitpid(child, &status, 0);
728 if (WIFEXITED(status)) {
729 if (WEXITSTATUS(status) != 0) {
730 LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status);
731 }
732 } else if (WIFSIGNALED(status)) {
733 LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status);
734 }
735
736 return StringValue(std::to_string(status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700737}
738
Doug Zongker512536a2010-02-17 16:11:44 -0800739// sha1_check(data)
740// to return the sha1 of the data (given in the format returned by
741// read_file).
742//
743// sha1_check(data, sha1_hex, [sha1_hex, ...])
744// returns the sha1 of the file if it matches any of the hex
745// strings passed, or "" if it does not equal any of them.
746//
Tianjie Xuc4447322017-03-06 14:44:59 -0800747Value* Sha1CheckFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
748 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800749 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
750 }
Doug Zongker512536a2010-02-17 16:11:44 -0800751
Tao Bao039f2da2016-11-22 16:29:50 -0800752 std::vector<std::unique_ptr<Value>> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800753 if (!ReadValueArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800754 return nullptr;
755 }
Doug Zongker512536a2010-02-17 16:11:44 -0800756
Tao Bao039f2da2016-11-22 16:29:50 -0800757 if (args[0]->type == VAL_INVALID) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700758 return StringValue("");
Tao Bao039f2da2016-11-22 16:29:50 -0800759 }
760 uint8_t digest[SHA_DIGEST_LENGTH];
761 SHA1(reinterpret_cast<const uint8_t*>(args[0]->data.c_str()), args[0]->data.size(), digest);
762
Tianjie Xuc4447322017-03-06 14:44:59 -0800763 if (argv.size() == 1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800764 return StringValue(print_sha1(digest));
765 }
766
Tianjie Xuc4447322017-03-06 14:44:59 -0800767 for (size_t i = 1; i < argv.size(); ++i) {
Tao Bao039f2da2016-11-22 16:29:50 -0800768 uint8_t arg_digest[SHA_DIGEST_LENGTH];
769 if (args[i]->type != VAL_STRING) {
770 LOG(ERROR) << name << "(): arg " << i << " is not a string; skipping";
771 } else if (ParseSha1(args[i]->data.c_str(), arg_digest) != 0) {
772 // Warn about bad args and skip them.
773 LOG(ERROR) << name << "(): error parsing \"" << args[i]->data << "\" as sha-1; skipping";
774 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
775 // Found a match.
776 return args[i].release();
777 }
778 }
779
780 // Didn't match any of the hex strings; return false.
781 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -0800782}
783
Hristo Bojinovdb314d62010-08-02 10:29:49 -0700784// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -0800785// is actually a FileContents*).
Tianjie Xuc4447322017-03-06 14:44:59 -0800786Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
787 if (argv.size() != 1) {
788 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800789 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700790
Tao Bao039f2da2016-11-22 16:29:50 -0800791 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800792 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800793 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
794 }
795 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -0800796
Tao Bao039f2da2016-11-22 16:29:50 -0800797 Value* v = new Value(VAL_INVALID, "");
Doug Zongker512536a2010-02-17 16:11:44 -0800798
Tao Bao039f2da2016-11-22 16:29:50 -0800799 FileContents fc;
800 if (LoadFileContents(filename.c_str(), &fc) == 0) {
801 v->type = VAL_BLOB;
802 v->data = std::string(fc.data.begin(), fc.data.end());
803 }
804 return v;
Doug Zongker512536a2010-02-17 16:11:44 -0800805}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700806
Tao Baod0f30882016-11-03 23:52:01 -0700807// write_value(value, filename)
808// Writes 'value' to 'filename'.
809// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
Tianjie Xuc4447322017-03-06 14:44:59 -0800810Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
811 if (argv.size() != 2) {
812 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
813 argv.size());
Tao Baod0f30882016-11-03 23:52:01 -0700814 }
815
816 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800817 if (!ReadArgs(state, argv, &args)) {
Tao Baod0f30882016-11-03 23:52:01 -0700818 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
819 }
820
821 const std::string& filename = args[1];
822 if (filename.empty()) {
823 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
824 }
825
826 const std::string& value = args[0];
827 if (!android::base::WriteStringToFile(value, filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800828 PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\"";
Tao Baod0f30882016-11-03 23:52:01 -0700829 return StringValue("");
830 } else {
831 return StringValue("t");
832 }
833}
834
Doug Zongkerc87bab12013-11-25 13:53:25 -0800835// Immediately reboot the device. Recovery is not finished normally,
836// so if you reboot into recovery it will re-start applying the
837// current package (because nothing has cleared the copy of the
838// arguments stored in the BCB).
839//
840// The argument is the partition name passed to the android reboot
841// property. It can be "recovery" to boot from the recovery
842// partition, or "" (empty string) to boot from the regular boot
843// partition.
Tianjie Xuc4447322017-03-06 14:44:59 -0800844Value* RebootNowFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
845 if (argv.size() != 2) {
846 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
847 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800848 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800849
Tao Baobedf5fc2016-11-18 12:01:26 -0800850 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800851 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800852 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
853 }
854 const std::string& filename = args[0];
855 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800856
Tao Baobedf5fc2016-11-18 12:01:26 -0800857 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
858 bootloader_message boot;
859 std::string err;
860 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800861 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800862 return StringValue("");
863 }
864 memset(boot.command, 0, sizeof(boot.command));
865 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800866 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800867 return StringValue("");
868 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800869
Dmitri Plotnikov8706a982017-04-18 08:28:26 -0700870 std::string reboot_cmd = "reboot," + property;
871 if (android::base::GetBoolProperty("ro.boot.quiescent", false)) {
872 reboot_cmd += ",quiescent";
873 }
Tao Baobedf5fc2016-11-18 12:01:26 -0800874 android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800875
Tao Baobedf5fc2016-11-18 12:01:26 -0800876 sleep(5);
877 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800878}
879
880// Store a string value somewhere that future invocations of recovery
881// can access it. This value is called the "stage" and can be used to
882// drive packages that need to do reboots in the middle of
883// installation and keep track of where they are in the multi-stage
884// install.
885//
886// The first argument is the block device for the misc partition
887// ("/misc" in the fstab), which is where this value is stored. The
888// second argument is the string to store; it should not exceed 31
889// bytes.
Tianjie Xuc4447322017-03-06 14:44:59 -0800890Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
891 if (argv.size() != 2) {
892 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
893 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800894 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800895
Tao Baobedf5fc2016-11-18 12:01:26 -0800896 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800897 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800898 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
899 }
900 const std::string& filename = args[0];
901 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800902
Tao Baobedf5fc2016-11-18 12:01:26 -0800903 // Store this value in the misc partition, immediately after the
904 // bootloader message that the main recovery uses to save its
905 // arguments in case of the device restarting midway through
906 // package installation.
907 bootloader_message boot;
908 std::string err;
909 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800910 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800911 return StringValue("");
912 }
913 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
914 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800915 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800916 return StringValue("");
917 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800918
Tao Baobedf5fc2016-11-18 12:01:26 -0800919 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800920}
921
922// Return the value most recently saved with SetStageFn. The argument
923// is the block device for the misc partition.
Tianjie Xuc4447322017-03-06 14:44:59 -0800924Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
925 if (argv.size() != 1) {
926 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800927 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800928
Tao Baobedf5fc2016-11-18 12:01:26 -0800929 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800930 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800931 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
932 }
933 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800934
Tao Baobedf5fc2016-11-18 12:01:26 -0800935 bootloader_message boot;
936 std::string err;
937 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800938 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800939 return StringValue("");
940 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800941
Tao Baobedf5fc2016-11-18 12:01:26 -0800942 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800943}
944
Tianjie Xuc4447322017-03-06 14:44:59 -0800945Value* WipeBlockDeviceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
946 if (argv.size() != 2) {
947 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
948 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800949 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800950
Tao Bao358c2ec2016-11-28 11:48:43 -0800951 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800952 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800953 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
954 }
955 const std::string& filename = args[0];
956 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800957
Tao Bao358c2ec2016-11-28 11:48:43 -0800958 size_t len;
959 if (!android::base::ParseUint(len_str.c_str(), &len)) {
960 return nullptr;
961 }
962 unique_fd fd(ota_open(filename.c_str(), O_WRONLY, 0644));
963 // The wipe_block_device function in ext4_utils returns 0 on success and 1
964 // for failure.
965 int status = wipe_block_device(fd, len);
966 return StringValue((status == 0) ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800967}
968
Tianjie Xuc4447322017-03-06 14:44:59 -0800969Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
970 if (!argv.empty()) {
971 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
972 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800973 }
974 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
975 fprintf(ui->cmd_pipe, "enable_reboot\n");
976 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -0700977}
978
Tianjie Xuc4447322017-03-06 14:44:59 -0800979Value* Tune2FsFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
980 if (argv.empty()) {
981 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800982 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800983
Tao Bao039f2da2016-11-22 16:29:50 -0800984 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800985 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800986 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
987 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800988
Tianjie Xuc4447322017-03-06 14:44:59 -0800989 char* args2[argv.size() + 1];
Tao Bao039f2da2016-11-22 16:29:50 -0800990 // Tune2fs expects the program name as its args[0]
991 args2[0] = const_cast<char*>(name);
992 if (args2[0] == nullptr) {
993 return nullptr;
994 }
Tianjie Xuc4447322017-03-06 14:44:59 -0800995 for (size_t i = 0; i < argv.size(); ++i) {
Tao Bao039f2da2016-11-22 16:29:50 -0800996 args2[i + 1] = &args[i][0];
997 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800998
Tao Bao039f2da2016-11-22 16:29:50 -0800999 // tune2fs changes the file system parameters on an ext2 file system; it
1000 // returns 0 on success.
Tianjie Xuc4447322017-03-06 14:44:59 -08001001 int result = tune2fs_main(argv.size() + 1, args2);
Tao Bao039f2da2016-11-22 16:29:50 -08001002 if (result != 0) {
1003 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
1004 }
1005 return StringValue("t");
Michael Rungeacf47db2014-11-21 00:12:28 -08001006}
1007
Doug Zongker9931f7f2009-06-10 14:11:53 -07001008void RegisterInstallFunctions() {
Tao Bao039f2da2016-11-22 16:29:50 -08001009 RegisterFunction("mount", MountFn);
1010 RegisterFunction("is_mounted", IsMountedFn);
1011 RegisterFunction("unmount", UnmountFn);
1012 RegisterFunction("format", FormatFn);
1013 RegisterFunction("show_progress", ShowProgressFn);
1014 RegisterFunction("set_progress", SetProgressFn);
Tao Bao039f2da2016-11-22 16:29:50 -08001015 RegisterFunction("package_extract_file", PackageExtractFileFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001016
Tao Bao039f2da2016-11-22 16:29:50 -08001017 RegisterFunction("getprop", GetPropFn);
1018 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001019
Tao Bao039f2da2016-11-22 16:29:50 -08001020 RegisterFunction("apply_patch", ApplyPatchFn);
1021 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1022 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001023
Tao Bao039f2da2016-11-22 16:29:50 -08001024 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001025
Tao Bao039f2da2016-11-22 16:29:50 -08001026 RegisterFunction("read_file", ReadFileFn);
1027 RegisterFunction("sha1_check", Sha1CheckFn);
Tao Bao039f2da2016-11-22 16:29:50 -08001028 RegisterFunction("write_value", WriteValueFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001029
Tao Bao039f2da2016-11-22 16:29:50 -08001030 RegisterFunction("wipe_cache", WipeCacheFn);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001031
Tao Bao039f2da2016-11-22 16:29:50 -08001032 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001033
Tao Bao039f2da2016-11-22 16:29:50 -08001034 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001035
Tao Bao039f2da2016-11-22 16:29:50 -08001036 RegisterFunction("reboot_now", RebootNowFn);
1037 RegisterFunction("get_stage", GetStageFn);
1038 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001039
Tao Bao039f2da2016-11-22 16:29:50 -08001040 RegisterFunction("enable_reboot", EnableRebootFn);
1041 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001042}