blob: c82351ec4b9c514fd0c3393553d5311f09096077 [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>
Tianjie Xu22f11202018-08-27 10:50:31 -070049#include <android-base/unique_fd.h>
Tao Bao0d3f84f2016-12-28 15:09:20 -080050#include <applypatch/applypatch.h>
51#include <bootloader_message/bootloader_message.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>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070056#include <ziparchive/zip_archive.h>
Tao Bao1107d962015-09-09 17:16:55 -070057
Doug Zongker9931f7f2009-06-10 14:11:53 -070058#include "edify/expr.h"
Tianjie Xu1536db82019-05-14 10:54:43 -070059#include "edify/updater_interface.h"
60#include "edify/updater_runtime_interface.h"
Tao Bao17054c02018-05-03 22:41:23 -070061#include "otautil/dirutil.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070062#include "otautil/error_code.h"
Tao Bao9a319f02018-01-04 13:19:11 -080063#include "otautil/mounts.h"
Tao Bao09e468f2017-09-29 14:39:33 -070064#include "otautil/print_sha1.h"
Tao Bao2c526392018-05-03 23:01:13 -070065#include "otautil/sysutil.h"
Tianjie Xu1536db82019-05-14 10:54:43 -070066
Tianjie Xu27556d02019-05-22 14:48:35 -070067#ifndef __ANDROID__
68#include <cutils/memory.h> // for strlcpy
69#endif
70
Tianjie Xu1536db82019-05-14 10:54:43 -070071static bool UpdateBlockDeviceNameForPartition(UpdaterInterface* updater, Partition* partition) {
72 CHECK(updater);
73 std::string name = updater->FindBlockDeviceName(partition->name);
74 if (name.empty()) {
75 LOG(ERROR) << "Failed to find the block device " << partition->name;
76 return false;
77 }
78
79 partition->name = std::move(name);
80 return true;
81}
Doug Zongker8edb00c2009-06-11 17:21:44 -070082
Tianjie Xu5419ad32018-02-02 16:49:15 -080083// This is the updater side handler for ui_print() in edify script. Contents will be sent over to
84// the recovery side for on-screen display.
85Value* UIPrintFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
86 std::vector<std::string> args;
87 if (!ReadArgs(state, argv, &args)) {
88 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
89 }
90
91 std::string buffer = android::base::Join(args, "");
Tianjie Xu1536db82019-05-14 10:54:43 -070092 state->updater->UiPrint(buffer);
Tianjie Xu5419ad32018-02-02 16:49:15 -080093 return StringValue(buffer);
94}
95
96// package_extract_file(package_file[, dest_file])
97// Extracts a single package_file from the update package and writes it to dest_file,
98// overwriting existing files if necessary. Without the dest_file argument, returns the
99// contents of the package file as a binary blob.
100Value* PackageExtractFileFn(const char* name, State* state,
101 const std::vector<std::unique_ptr<Expr>>& argv) {
102 if (argv.size() < 1 || argv.size() > 2) {
103 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name,
104 argv.size());
105 }
106
107 if (argv.size() == 2) {
108 // The two-argument version extracts to a file.
109
110 std::vector<std::string> args;
111 if (!ReadArgs(state, argv, &args)) {
112 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
113 argv.size());
114 }
115 const std::string& zip_path = args[0];
116 const std::string& dest_path = args[1];
117
Tianjie Xu1536db82019-05-14 10:54:43 -0700118 ZipArchiveHandle za = state->updater->GetPackageHandle();
Tianjie Xu5419ad32018-02-02 16:49:15 -0800119 ZipEntry entry;
Elliott Hughesa86dddb2019-05-03 22:52:37 -0700120 if (FindEntry(za, zip_path, &entry) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800121 LOG(ERROR) << name << ": no " << zip_path << " in package";
122 return StringValue("");
123 }
124
Tianjie Xu22f11202018-08-27 10:50:31 -0700125 android::base::unique_fd fd(TEMP_FAILURE_RETRY(
126 open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tianjie Xu5419ad32018-02-02 16:49:15 -0800127 if (fd == -1) {
128 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
129 return StringValue("");
130 }
131
132 bool success = true;
133 int32_t ret = ExtractEntryToFile(za, &entry, fd);
134 if (ret != 0) {
135 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
136 << entry.uncompressed_length << " bytes) to \"" << dest_path
137 << "\": " << ErrorCodeString(ret);
138 success = false;
139 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700140 if (fsync(fd) == -1) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800141 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
142 success = false;
143 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700144
145 if (close(fd.release()) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800146 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
147 success = false;
148 }
149
150 return StringValue(success ? "t" : "");
151 } else {
152 // The one-argument version returns the contents of the file as the result.
153
154 std::vector<std::string> args;
155 if (!ReadArgs(state, argv, &args)) {
156 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
157 argv.size());
158 }
159 const std::string& zip_path = args[0];
160
Tianjie Xu1536db82019-05-14 10:54:43 -0700161 ZipArchiveHandle za = state->updater->GetPackageHandle();
Tianjie Xu5419ad32018-02-02 16:49:15 -0800162 ZipEntry entry;
Elliott Hughesa86dddb2019-05-03 22:52:37 -0700163 if (FindEntry(za, zip_path, &entry) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800164 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
165 zip_path.c_str());
166 }
167
168 std::string buffer;
169 buffer.resize(entry.uncompressed_length);
170
171 int32_t ret =
172 ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
173 if (ret != 0) {
174 return ErrorAbort(state, kPackageExtractFileFailure,
175 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
176 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
177 }
178
Tao Bao511d7592018-06-19 15:56:49 -0700179 return new Value(Value::Type::BLOB, buffer);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800180 }
181}
182
Tao Bao5609bc82018-06-20 00:30:48 -0700183// patch_partition_check(target_partition, source_partition)
184// Checks if the target and source partitions have the desired checksums to be patched. It returns
185// directly, if the target partition already has the expected checksum. Otherwise it in turn
186// checks the integrity of the source partition and the backup file on /cache.
Tianjie Xu5419ad32018-02-02 16:49:15 -0800187//
Tao Bao5609bc82018-06-20 00:30:48 -0700188// For example, patch_partition_check(
189// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
190// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4")
191Value* PatchPartitionCheckFn(const char* name, State* state,
192 const std::vector<std::unique_ptr<Expr>>& argv) {
193 if (argv.size() != 2) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800194 return ErrorAbort(state, kArgsParsingFailure,
Tao Bao5609bc82018-06-20 00:30:48 -0700195 "%s(): Invalid number of args (expected 2, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800196 }
197
198 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700199 if (!ReadArgs(state, argv, &args, 0, 2)) {
200 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800201 }
202
Tao Bao5609bc82018-06-20 00:30:48 -0700203 std::string err;
204 auto target = Partition::Parse(args[0], &err);
205 if (!target) {
206 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
207 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800208 }
209
Tao Bao5609bc82018-06-20 00:30:48 -0700210 auto source = Partition::Parse(args[1], &err);
211 if (!source) {
212 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
213 args[1].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800214 }
215
Tianjie Xu1536db82019-05-14 10:54:43 -0700216 if (!UpdateBlockDeviceNameForPartition(state->updater, &source) ||
217 !UpdateBlockDeviceNameForPartition(state->updater, &target)) {
218 return StringValue("");
219 }
220
Tao Bao5609bc82018-06-20 00:30:48 -0700221 bool result = PatchPartitionCheck(target, source);
222 return StringValue(result ? "t" : "");
Tianjie Xu5419ad32018-02-02 16:49:15 -0800223}
224
Tao Bao5609bc82018-06-20 00:30:48 -0700225// patch_partition(target, source, patch)
226// Applies the given patch to the source partition, and writes the result to the target partition.
227//
228// For example, patch_partition(
229// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
230// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4",
231// package_extract_file("boot.img.p"))
232Value* PatchPartitionFn(const char* name, State* state,
233 const std::vector<std::unique_ptr<Expr>>& argv) {
234 if (argv.size() != 3) {
235 return ErrorAbort(state, kArgsParsingFailure,
236 "%s(): Invalid number of args (expected 3, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800237 }
238
239 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700240 if (!ReadArgs(state, argv, &args, 0, 2)) {
241 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800242 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800243
Tao Bao5609bc82018-06-20 00:30:48 -0700244 std::string err;
245 auto target = Partition::Parse(args[0], &err);
246 if (!target) {
247 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
248 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800249 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800250
Tao Bao5609bc82018-06-20 00:30:48 -0700251 auto source = Partition::Parse(args[1], &err);
252 if (!source) {
253 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
254 args[1].c_str(), err.c_str());
255 }
256
257 std::vector<std::unique_ptr<Value>> values;
258 if (!ReadValueArgs(state, argv, &values, 2, 1) || values[0]->type != Value::Type::BLOB) {
259 return ErrorAbort(state, kArgsParsingFailure, "%s(): Invalid patch arg", name);
260 }
261
Tianjie Xu1536db82019-05-14 10:54:43 -0700262 if (!UpdateBlockDeviceNameForPartition(state->updater, &source) ||
263 !UpdateBlockDeviceNameForPartition(state->updater, &target)) {
264 return StringValue("");
265 }
266
Tao Bao5609bc82018-06-20 00:30:48 -0700267 bool result = PatchPartition(target, source, *values[0], nullptr);
268 return StringValue(result ? "t" : "");
Tianjie Xu5419ad32018-02-02 16:49:15 -0800269}
270
Doug Zongker3d177d02010-07-01 09:18:44 -0700271// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -0700272// mount(fs_type, partition_type, location, mount_point, mount_options)
Tianjie Xuc4447322017-03-06 14:44:59 -0800273
Doug Zongker3d177d02010-07-01 09:18:44 -0700274// fs_type="ext4" partition_type="EMMC" location=device
Tianjie Xuc4447322017-03-06 14:44:59 -0800275Value* MountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
276 if (argv.size() != 4 && argv.size() != 5) {
277 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %zu", name,
278 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800279 }
280
281 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800282 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800283 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
284 }
285 const std::string& fs_type = args[0];
286 const std::string& partition_type = args[1];
287 const std::string& location = args[2];
288 const std::string& mount_point = args[3];
289 std::string mount_options;
290
Tianjie Xuc4447322017-03-06 14:44:59 -0800291 if (argv.size() == 5) {
Tao Bao039f2da2016-11-22 16:29:50 -0800292 mount_options = args[4];
293 }
294
295 if (fs_type.empty()) {
296 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
297 }
298 if (partition_type.empty()) {
299 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
300 name);
301 }
302 if (location.empty()) {
303 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
304 }
305 if (mount_point.empty()) {
306 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
307 name);
308 }
309
Tianjie Xu1536db82019-05-14 10:54:43 -0700310 auto updater = state->updater;
311 if (updater->GetRuntime()->Mount(location, mount_point, fs_type, mount_options) != 0) {
Tianjie Xu58d59122019-05-03 01:05:04 -0700312 updater->UiPrint(android::base::StringPrintf("%s: Failed to mount %s at %s: %s", name,
313 location.c_str(), mount_point.c_str(),
314 strerror(errno)));
Tao Bao039f2da2016-11-22 16:29:50 -0800315 return StringValue("");
316 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700317
Tao Bao039f2da2016-11-22 16:29:50 -0800318 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700319}
320
Doug Zongker8edb00c2009-06-11 17:21:44 -0700321// is_mounted(mount_point)
Tianjie Xuc4447322017-03-06 14:44:59 -0800322Value* IsMountedFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
323 if (argv.size() != 1) {
324 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800325 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700326
Tao Bao039f2da2016-11-22 16:29:50 -0800327 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800328 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800329 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
330 }
331 const std::string& mount_point = args[0];
332 if (mount_point.empty()) {
333 return ErrorAbort(state, kArgsParsingFailure,
334 "mount_point argument to unmount() can't be empty");
335 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700336
Tianjie Xu1536db82019-05-14 10:54:43 -0700337 auto updater_runtime = state->updater->GetRuntime();
338 if (!updater_runtime->IsMounted(mount_point)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800339 return StringValue("");
340 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700341
Tao Bao039f2da2016-11-22 16:29:50 -0800342 return StringValue(mount_point);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700343}
344
Tianjie Xuc4447322017-03-06 14:44:59 -0800345Value* UnmountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
346 if (argv.size() != 1) {
347 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800348 }
349 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800350 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800351 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
352 }
353 const std::string& mount_point = args[0];
354 if (mount_point.empty()) {
355 return ErrorAbort(state, kArgsParsingFailure,
356 "mount_point argument to unmount() can't be empty");
357 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700358
Tianjie Xu1536db82019-05-14 10:54:43 -0700359 auto updater = state->updater;
360 auto [mounted, result] = updater->GetRuntime()->Unmount(mount_point);
361 if (!mounted) {
Tianjie Xu58d59122019-05-03 01:05:04 -0700362 updater->UiPrint(
363 android::base::StringPrintf("Failed to unmount %s: No such volume", mount_point.c_str()));
Tao Bao039f2da2016-11-22 16:29:50 -0800364 return nullptr;
Tianjie Xu1536db82019-05-14 10:54:43 -0700365 } else if (result != 0) {
366 updater->UiPrint(android::base::StringPrintf("Failed to unmount %s: %s", mount_point.c_str(),
367 strerror(errno)));
Tao Bao039f2da2016-11-22 16:29:50 -0800368 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700369
Tao Bao039f2da2016-11-22 16:29:50 -0800370 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700371}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700372
Stephen Smalley779701d2012-02-09 14:13:23 -0500373// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700374//
Tao Bao039f2da2016-11-22 16:29:50 -0800375// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
376// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700377// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800378// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700379// 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 -0800380Value* FormatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
381 if (argv.size() != 5) {
382 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %zu", name,
383 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800384 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500385
Tao Bao039f2da2016-11-22 16:29:50 -0800386 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800387 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800388 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
389 }
390 const std::string& fs_type = args[0];
391 const std::string& partition_type = args[1];
392 const std::string& location = args[2];
393 const std::string& fs_size = args[3];
394 const std::string& mount_point = args[4];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700395
Tao Bao039f2da2016-11-22 16:29:50 -0800396 if (fs_type.empty()) {
397 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
398 }
399 if (partition_type.empty()) {
400 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
401 name);
402 }
403 if (location.empty()) {
404 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
405 }
406 if (mount_point.empty()) {
407 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
408 name);
409 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700410
Tao Bao039f2da2016-11-22 16:29:50 -0800411 int64_t size;
412 if (!android::base::ParseInt(fs_size, &size)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800413 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800414 fs_size.c_str());
415 }
416
Tianjie Xu1536db82019-05-14 10:54:43 -0700417 auto updater_runtime = state->updater->GetRuntime();
Tao Bao039f2da2016-11-22 16:29:50 -0800418 if (fs_type == "ext4") {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800419 std::vector<std::string> mke2fs_args = {
420 "/system/bin/mke2fs", "-t", "ext4", "-b", "4096", location
421 };
Jin Qianded2dac2017-04-21 14:36:12 -0700422 if (size != 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800423 mke2fs_args.push_back(std::to_string(size / 4096LL));
Jin Qianded2dac2017-04-21 14:36:12 -0700424 }
425
Tianjie Xu1536db82019-05-14 10:54:43 -0700426 if (auto status = updater_runtime->RunProgram(mke2fs_args, true); status != 0) {
Jin Qian502fd1c2017-11-02 11:58:12 -0700427 LOG(ERROR) << name << ": mke2fs failed (" << status << ") on " << location;
428 return StringValue("");
Jin Qianded2dac2017-04-21 14:36:12 -0700429 }
430
Tianjie Xu1536db82019-05-14 10:54:43 -0700431 if (auto status = updater_runtime->RunProgram(
432 { "/system/bin/e2fsdroid", "-e", "-a", mount_point, location }, true);
Tao Bao3d69f0d2018-12-20 09:44:06 -0800433 status != 0) {
Jin Qianded2dac2017-04-21 14:36:12 -0700434 LOG(ERROR) << name << ": e2fsdroid failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800435 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700436 }
Tao Bao039f2da2016-11-22 16:29:50 -0800437 return StringValue(location);
Tao Bao3d69f0d2018-12-20 09:44:06 -0800438 }
439
440 if (fs_type == "f2fs") {
Tao Bao039f2da2016-11-22 16:29:50 -0800441 if (size < 0) {
442 LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size;
443 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700444 }
Tao Bao3d69f0d2018-12-20 09:44:06 -0800445 std::vector<std::string> f2fs_args = {
Tao Baoc674dfb2018-12-20 14:25:15 -0800446 "/system/bin/make_f2fs", "-g", "android", "-w", "512", location
Tao Bao3d69f0d2018-12-20 09:44:06 -0800447 };
448 if (size >= 512) {
449 f2fs_args.push_back(std::to_string(size / 512));
450 }
Tianjie Xu1536db82019-05-14 10:54:43 -0700451 if (auto status = updater_runtime->RunProgram(f2fs_args, true); status != 0) {
Tao Baoc674dfb2018-12-20 14:25:15 -0800452 LOG(ERROR) << name << ": make_f2fs failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800453 return StringValue("");
454 }
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800455
Tianjie Xu1536db82019-05-14 10:54:43 -0700456 if (auto status = updater_runtime->RunProgram(
457 { "/system/bin/sload_f2fs", "-t", mount_point, location }, true);
Tao Baoc674dfb2018-12-20 14:25:15 -0800458 status != 0) {
459 LOG(ERROR) << name << ": sload_f2fs failed (" << status << ") on " << location;
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800460 return StringValue("");
461 }
462
Tao Bao039f2da2016-11-22 16:29:50 -0800463 return StringValue(location);
Tao Bao039f2da2016-11-22 16:29:50 -0800464 }
465
Tao Bao3d69f0d2018-12-20 09:44:06 -0800466 LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \""
467 << partition_type << "\"";
Tao Bao039f2da2016-11-22 16:29:50 -0800468 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700469}
470
Tianjie Xuc4447322017-03-06 14:44:59 -0800471Value* ShowProgressFn(const char* name, State* state,
472 const std::vector<std::unique_ptr<Expr>>& argv) {
473 if (argv.size() != 2) {
474 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
475 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800476 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700477
Tao Bao039f2da2016-11-22 16:29:50 -0800478 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800479 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800480 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
481 }
482 const std::string& frac_str = args[0];
483 const std::string& sec_str = args[1];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700484
Tao Bao039f2da2016-11-22 16:29:50 -0800485 double frac;
486 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800487 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800488 frac_str.c_str());
489 }
490 int sec;
491 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800492 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800493 sec_str.c_str());
494 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700495
Tianjie Xu1536db82019-05-14 10:54:43 -0700496 state->updater->WriteToCommandPipe(android::base::StringPrintf("progress %f %d", frac, sec));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700497
Tao Bao039f2da2016-11-22 16:29:50 -0800498 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700499}
500
Tianjie Xu5419ad32018-02-02 16:49:15 -0800501Value* SetProgressFn(const char* name, State* state,
502 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800503 if (argv.size() != 1) {
504 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800505 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700506
Tao Bao039f2da2016-11-22 16:29:50 -0800507 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800508 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800509 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
510 }
511 const std::string& frac_str = args[0];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700512
Tao Bao039f2da2016-11-22 16:29:50 -0800513 double frac;
514 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800515 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800516 frac_str.c_str());
517 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700518
Tianjie Xu1536db82019-05-14 10:54:43 -0700519 state->updater->WriteToCommandPipe(android::base::StringPrintf("set_progress %f", frac));
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700520
Tao Bao039f2da2016-11-22 16:29:50 -0800521 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700522}
523
Tianjie Xuc4447322017-03-06 14:44:59 -0800524Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
525 if (argv.size() != 1) {
526 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800527 }
528 std::string key;
529 if (!Evaluate(state, argv[0], &key)) {
530 return nullptr;
531 }
Tianjie Xu1536db82019-05-14 10:54:43 -0700532
533 auto updater_runtime = state->updater->GetRuntime();
534 std::string value = updater_runtime->GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700535
Tao Bao039f2da2016-11-22 16:29:50 -0800536 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700537}
538
Doug Zongker47cace92009-06-18 10:11:50 -0700539// file_getprop(file, key)
540//
541// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -0700542// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700543// and returns the value for 'key' (or "" if it isn't defined).
Tianjie Xu5419ad32018-02-02 16:49:15 -0800544Value* FileGetPropFn(const char* name, State* state,
545 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800546 if (argv.size() != 2) {
547 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
548 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800549 }
550
551 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800552 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800553 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
554 }
555 const std::string& filename = args[0];
556 const std::string& key = args[1];
557
Tianjie Xu22f11202018-08-27 10:50:31 -0700558 std::string buffer;
Tianjie Xu1536db82019-05-14 10:54:43 -0700559 auto updater_runtime = state->updater->GetRuntime();
560 if (!updater_runtime->ReadFileToString(filename, &buffer)) {
Tianjie Xu22f11202018-08-27 10:50:31 -0700561 ErrorAbort(state, kFreadFailure, "%s: failed to read %s", name, filename.c_str());
Tao Bao358c2ec2016-11-28 11:48:43 -0800562 return nullptr;
563 }
564
Tao Bao358c2ec2016-11-28 11:48:43 -0800565 std::vector<std::string> lines = android::base::Split(buffer, "\n");
566 for (size_t i = 0; i < lines.size(); i++) {
567 std::string line = android::base::Trim(lines[i]);
568
569 // comment or blank line: skip to next line
570 if (line.empty() || line[0] == '#') {
571 continue;
572 }
573 size_t equal_pos = line.find('=');
574 if (equal_pos == std::string::npos) {
575 continue;
Tao Bao51d516e2016-11-03 14:49:01 -0700576 }
577
Tao Bao358c2ec2016-11-28 11:48:43 -0800578 // trim whitespace between key and '='
579 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -0700580
Tao Bao358c2ec2016-11-28 11:48:43 -0800581 // not the key we're looking for
582 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700583
Tao Bao358c2ec2016-11-28 11:48:43 -0800584 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
585 }
Doug Zongker47cace92009-06-18 10:11:50 -0700586
Tao Bao358c2ec2016-11-28 11:48:43 -0800587 return StringValue("");
Doug Zongker47cace92009-06-18 10:11:50 -0700588}
589
Doug Zongker8edb00c2009-06-11 17:21:44 -0700590// apply_patch_space(bytes)
Tianjie Xu5419ad32018-02-02 16:49:15 -0800591Value* ApplyPatchSpaceFn(const char* name, State* state,
592 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800593 if (argv.size() != 1) {
594 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name,
595 argv.size());
596 }
Tao Bao039f2da2016-11-22 16:29:50 -0800597 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800598 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800599 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
600 }
601 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800602
Tao Bao039f2da2016-11-22 16:29:50 -0800603 size_t bytes;
604 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800605 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name,
606 bytes_str.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -0800607 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800608
Tianjie Xu99b73be2017-11-28 17:23:06 -0800609 // Skip the cache size check if the update is a retry.
Tao Bao5ee25662018-07-11 15:55:32 -0700610 if (state->is_retry || CheckAndFreeSpaceOnCache(bytes)) {
Tianjie Xu99b73be2017-11-28 17:23:06 -0800611 return StringValue("t");
612 }
613 return StringValue("");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800614}
615
Tianjie Xuc4447322017-03-06 14:44:59 -0800616Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
617 if (!argv.empty()) {
618 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
619 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800620 }
Tianjie Xu58d59122019-05-03 01:05:04 -0700621
Tianjie Xu1536db82019-05-14 10:54:43 -0700622 state->updater->WriteToCommandPipe("wipe_cache");
Tao Bao039f2da2016-11-22 16:29:50 -0800623 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700624}
625
Tianjie Xuc4447322017-03-06 14:44:59 -0800626Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
627 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800628 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
629 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700630
Tao Bao039f2da2016-11-22 16:29:50 -0800631 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800632 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800633 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
634 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700635
Tianjie Xu1536db82019-05-14 10:54:43 -0700636 auto updater_runtime = state->updater->GetRuntime();
637 auto status = updater_runtime->RunProgram(args, false);
Tao Bao039f2da2016-11-22 16:29:50 -0800638 return StringValue(std::to_string(status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700639}
640
Tao Bao511d7592018-06-19 15:56:49 -0700641// read_file(filename)
Tao Baobafd6c72018-07-09 15:08:50 -0700642// Reads a local file 'filename' and returns its contents as a string Value.
Tianjie Xuc4447322017-03-06 14:44:59 -0800643Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
644 if (argv.size() != 1) {
645 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800646 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700647
Tao Bao039f2da2016-11-22 16:29:50 -0800648 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800649 if (!ReadArgs(state, argv, &args)) {
Tao Bao511d7592018-06-19 15:56:49 -0700650 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tao Bao039f2da2016-11-22 16:29:50 -0800651 }
652 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -0800653
Tao Baobafd6c72018-07-09 15:08:50 -0700654 std::string contents;
Tianjie Xu1536db82019-05-14 10:54:43 -0700655 auto updater_runtime = state->updater->GetRuntime();
656 if (updater_runtime->ReadFileToString(filename, &contents)) {
Tao Baobafd6c72018-07-09 15:08:50 -0700657 return new Value(Value::Type::STRING, std::move(contents));
Tao Bao039f2da2016-11-22 16:29:50 -0800658 }
Tao Bao511d7592018-06-19 15:56:49 -0700659
660 // Leave it to caller to handle the failure.
Tao Baobafd6c72018-07-09 15:08:50 -0700661 PLOG(ERROR) << name << ": Failed to read " << filename;
Tao Bao511d7592018-06-19 15:56:49 -0700662 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -0800663}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700664
Tao Baod0f30882016-11-03 23:52:01 -0700665// write_value(value, filename)
666// Writes 'value' to 'filename'.
667// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
Tianjie Xuc4447322017-03-06 14:44:59 -0800668Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
669 if (argv.size() != 2) {
670 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
671 argv.size());
Tao Baod0f30882016-11-03 23:52:01 -0700672 }
673
674 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800675 if (!ReadArgs(state, argv, &args)) {
Tao Baod0f30882016-11-03 23:52:01 -0700676 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
677 }
678
679 const std::string& filename = args[1];
680 if (filename.empty()) {
681 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
682 }
683
684 const std::string& value = args[0];
Tianjie Xu1536db82019-05-14 10:54:43 -0700685 auto updater_runtime = state->updater->GetRuntime();
686 if (!updater_runtime->WriteStringToFile(value, filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800687 PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\"";
Tao Baod0f30882016-11-03 23:52:01 -0700688 return StringValue("");
Tao Baod0f30882016-11-03 23:52:01 -0700689 }
Tianjie Xu1536db82019-05-14 10:54:43 -0700690 return StringValue("t");
Tao Baod0f30882016-11-03 23:52:01 -0700691}
692
Doug Zongkerc87bab12013-11-25 13:53:25 -0800693// Immediately reboot the device. Recovery is not finished normally,
694// so if you reboot into recovery it will re-start applying the
695// current package (because nothing has cleared the copy of the
696// arguments stored in the BCB).
697//
698// The argument is the partition name passed to the android reboot
699// property. It can be "recovery" to boot from the recovery
700// partition, or "" (empty string) to boot from the regular boot
701// partition.
Tianjie Xuc4447322017-03-06 14:44:59 -0800702Value* RebootNowFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
703 if (argv.size() != 2) {
704 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
705 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800706 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800707
Tao Baobedf5fc2016-11-18 12:01:26 -0800708 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800709 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800710 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
711 }
712 const std::string& filename = args[0];
713 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800714
Tao Baobedf5fc2016-11-18 12:01:26 -0800715 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
716 bootloader_message boot;
717 std::string err;
718 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800719 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800720 return StringValue("");
721 }
722 memset(boot.command, 0, sizeof(boot.command));
723 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800724 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800725 return StringValue("");
726 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800727
Tao Bao782dcc12019-04-29 11:23:16 -0700728 Reboot(property);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800729
Tao Baobedf5fc2016-11-18 12:01:26 -0800730 sleep(5);
731 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800732}
733
734// Store a string value somewhere that future invocations of recovery
735// can access it. This value is called the "stage" and can be used to
736// drive packages that need to do reboots in the middle of
737// installation and keep track of where they are in the multi-stage
738// install.
739//
740// The first argument is the block device for the misc partition
741// ("/misc" in the fstab), which is where this value is stored. The
742// second argument is the string to store; it should not exceed 31
743// bytes.
Tianjie Xuc4447322017-03-06 14:44:59 -0800744Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
745 if (argv.size() != 2) {
746 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
747 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800748 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800749
Tao Baobedf5fc2016-11-18 12:01:26 -0800750 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800751 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800752 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
753 }
754 const std::string& filename = args[0];
755 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800756
Tao Baobedf5fc2016-11-18 12:01:26 -0800757 // Store this value in the misc partition, immediately after the
758 // bootloader message that the main recovery uses to save its
759 // arguments in case of the device restarting midway through
760 // package installation.
761 bootloader_message boot;
762 std::string err;
763 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800764 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800765 return StringValue("");
766 }
767 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
768 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800769 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800770 return StringValue("");
771 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800772
Tao Baobedf5fc2016-11-18 12:01:26 -0800773 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800774}
775
776// Return the value most recently saved with SetStageFn. The argument
777// is the block device for the misc partition.
Tianjie Xuc4447322017-03-06 14:44:59 -0800778Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
779 if (argv.size() != 1) {
780 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800781 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800782
Tao Baobedf5fc2016-11-18 12:01:26 -0800783 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800784 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800785 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
786 }
787 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800788
Tao Baobedf5fc2016-11-18 12:01:26 -0800789 bootloader_message boot;
790 std::string err;
791 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800792 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800793 return StringValue("");
794 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800795
Tao Baobedf5fc2016-11-18 12:01:26 -0800796 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800797}
798
Tianjie Xuc4447322017-03-06 14:44:59 -0800799Value* WipeBlockDeviceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
800 if (argv.size() != 2) {
801 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
802 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800803 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800804
Tao Bao358c2ec2016-11-28 11:48:43 -0800805 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800806 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800807 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
808 }
809 const std::string& filename = args[0];
810 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800811
Tao Bao358c2ec2016-11-28 11:48:43 -0800812 size_t len;
813 if (!android::base::ParseUint(len_str.c_str(), &len)) {
814 return nullptr;
815 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700816
Tianjie Xu1536db82019-05-14 10:54:43 -0700817 auto updater_runtime = state->updater->GetRuntime();
818 int status = updater_runtime->WipeBlockDevice(filename, len);
819 return StringValue(status == 0 ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800820}
821
Tianjie Xuc4447322017-03-06 14:44:59 -0800822Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
823 if (!argv.empty()) {
824 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
825 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800826 }
Tianjie Xu1536db82019-05-14 10:54:43 -0700827 state->updater->WriteToCommandPipe("enable_reboot");
Tao Bao039f2da2016-11-22 16:29:50 -0800828 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -0700829}
830
Tianjie Xuc4447322017-03-06 14:44:59 -0800831Value* Tune2FsFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
832 if (argv.empty()) {
833 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800834 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800835
Tao Bao039f2da2016-11-22 16:29:50 -0800836 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800837 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800838 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
839 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800840
Tao Bao3d69f0d2018-12-20 09:44:06 -0800841 // tune2fs expects the program name as its first arg.
842 args.insert(args.begin(), "tune2fs");
Tianjie Xu1536db82019-05-14 10:54:43 -0700843 auto updater_runtime = state->updater->GetRuntime();
844 if (auto result = updater_runtime->Tune2Fs(args); result != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800845 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
846 }
847 return StringValue("t");
Michael Rungeacf47db2014-11-21 00:12:28 -0800848}
849
Doug Zongker9931f7f2009-06-10 14:11:53 -0700850void RegisterInstallFunctions() {
Tao Bao039f2da2016-11-22 16:29:50 -0800851 RegisterFunction("mount", MountFn);
852 RegisterFunction("is_mounted", IsMountedFn);
853 RegisterFunction("unmount", UnmountFn);
854 RegisterFunction("format", FormatFn);
855 RegisterFunction("show_progress", ShowProgressFn);
856 RegisterFunction("set_progress", SetProgressFn);
Tao Bao039f2da2016-11-22 16:29:50 -0800857 RegisterFunction("package_extract_file", PackageExtractFileFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700858
Tao Bao039f2da2016-11-22 16:29:50 -0800859 RegisterFunction("getprop", GetPropFn);
860 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700861
Tao Bao039f2da2016-11-22 16:29:50 -0800862 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Tao Bao5609bc82018-06-20 00:30:48 -0700863 RegisterFunction("patch_partition", PatchPartitionFn);
864 RegisterFunction("patch_partition_check", PatchPartitionCheckFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700865
Tao Bao039f2da2016-11-22 16:29:50 -0800866 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -0800867
Tao Bao039f2da2016-11-22 16:29:50 -0800868 RegisterFunction("read_file", ReadFileFn);
Tao Bao039f2da2016-11-22 16:29:50 -0800869 RegisterFunction("write_value", WriteValueFn);
Doug Zongker512536a2010-02-17 16:11:44 -0800870
Tao Bao039f2da2016-11-22 16:29:50 -0800871 RegisterFunction("wipe_cache", WipeCacheFn);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700872
Tao Bao039f2da2016-11-22 16:29:50 -0800873 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700874
Tao Bao039f2da2016-11-22 16:29:50 -0800875 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800876
Tao Bao039f2da2016-11-22 16:29:50 -0800877 RegisterFunction("reboot_now", RebootNowFn);
878 RegisterFunction("get_stage", GetStageFn);
879 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -0700880
Tao Bao039f2da2016-11-22 16:29:50 -0800881 RegisterFunction("enable_reboot", EnableRebootFn);
882 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700883}