blob: 6b15eaa3a874b85562ba2b6444db7c38e3ff7545 [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>
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"
Tianjie Xu1536db82019-05-14 10:54:43 -070060#include "edify/updater_interface.h"
61#include "edify/updater_runtime_interface.h"
Tao Bao17054c02018-05-03 22:41:23 -070062#include "otautil/dirutil.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070063#include "otautil/error_code.h"
Tao Bao9a319f02018-01-04 13:19:11 -080064#include "otautil/mounts.h"
Tao Bao09e468f2017-09-29 14:39:33 -070065#include "otautil/print_sha1.h"
Tao Bao2c526392018-05-03 23:01:13 -070066#include "otautil/sysutil.h"
Tianjie Xu1536db82019-05-14 10:54:43 -070067
68static bool UpdateBlockDeviceNameForPartition(UpdaterInterface* updater, Partition* partition) {
69 CHECK(updater);
70 std::string name = updater->FindBlockDeviceName(partition->name);
71 if (name.empty()) {
72 LOG(ERROR) << "Failed to find the block device " << partition->name;
73 return false;
74 }
75
76 partition->name = std::move(name);
77 return true;
78}
Doug Zongker8edb00c2009-06-11 17:21:44 -070079
Tianjie Xu5419ad32018-02-02 16:49:15 -080080// This is the updater side handler for ui_print() in edify script. Contents will be sent over to
81// the recovery side for on-screen display.
82Value* UIPrintFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
83 std::vector<std::string> args;
84 if (!ReadArgs(state, argv, &args)) {
85 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
86 }
87
88 std::string buffer = android::base::Join(args, "");
Tianjie Xu1536db82019-05-14 10:54:43 -070089 state->updater->UiPrint(buffer);
Tianjie Xu5419ad32018-02-02 16:49:15 -080090 return StringValue(buffer);
91}
92
93// package_extract_file(package_file[, dest_file])
94// Extracts a single package_file from the update package and writes it to dest_file,
95// overwriting existing files if necessary. Without the dest_file argument, returns the
96// contents of the package file as a binary blob.
97Value* PackageExtractFileFn(const char* name, State* state,
98 const std::vector<std::unique_ptr<Expr>>& argv) {
99 if (argv.size() < 1 || argv.size() > 2) {
100 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name,
101 argv.size());
102 }
103
104 if (argv.size() == 2) {
105 // The two-argument version extracts to a file.
106
107 std::vector<std::string> args;
108 if (!ReadArgs(state, argv, &args)) {
109 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
110 argv.size());
111 }
112 const std::string& zip_path = args[0];
113 const std::string& dest_path = args[1];
114
Tianjie Xu1536db82019-05-14 10:54:43 -0700115 ZipArchiveHandle za = state->updater->GetPackageHandle();
Tianjie Xu5419ad32018-02-02 16:49:15 -0800116 ZipEntry entry;
Elliott Hughesa86dddb2019-05-03 22:52:37 -0700117 if (FindEntry(za, zip_path, &entry) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800118 LOG(ERROR) << name << ": no " << zip_path << " in package";
119 return StringValue("");
120 }
121
Tianjie Xu22f11202018-08-27 10:50:31 -0700122 android::base::unique_fd fd(TEMP_FAILURE_RETRY(
123 open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tianjie Xu5419ad32018-02-02 16:49:15 -0800124 if (fd == -1) {
125 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
126 return StringValue("");
127 }
128
129 bool success = true;
130 int32_t ret = ExtractEntryToFile(za, &entry, fd);
131 if (ret != 0) {
132 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
133 << entry.uncompressed_length << " bytes) to \"" << dest_path
134 << "\": " << ErrorCodeString(ret);
135 success = false;
136 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700137 if (fsync(fd) == -1) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800138 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
139 success = false;
140 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700141
142 if (close(fd.release()) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800143 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
144 success = false;
145 }
146
147 return StringValue(success ? "t" : "");
148 } else {
149 // The one-argument version returns the contents of the file as the result.
150
151 std::vector<std::string> args;
152 if (!ReadArgs(state, argv, &args)) {
153 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
154 argv.size());
155 }
156 const std::string& zip_path = args[0];
157
Tianjie Xu1536db82019-05-14 10:54:43 -0700158 ZipArchiveHandle za = state->updater->GetPackageHandle();
Tianjie Xu5419ad32018-02-02 16:49:15 -0800159 ZipEntry entry;
Elliott Hughesa86dddb2019-05-03 22:52:37 -0700160 if (FindEntry(za, zip_path, &entry) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800161 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
162 zip_path.c_str());
163 }
164
165 std::string buffer;
166 buffer.resize(entry.uncompressed_length);
167
168 int32_t ret =
169 ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
170 if (ret != 0) {
171 return ErrorAbort(state, kPackageExtractFileFailure,
172 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
173 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
174 }
175
Tao Bao511d7592018-06-19 15:56:49 -0700176 return new Value(Value::Type::BLOB, buffer);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800177 }
178}
179
Tao Bao5609bc82018-06-20 00:30:48 -0700180// patch_partition_check(target_partition, source_partition)
181// Checks if the target and source partitions have the desired checksums to be patched. It returns
182// directly, if the target partition already has the expected checksum. Otherwise it in turn
183// checks the integrity of the source partition and the backup file on /cache.
Tianjie Xu5419ad32018-02-02 16:49:15 -0800184//
Tao Bao5609bc82018-06-20 00:30:48 -0700185// For example, patch_partition_check(
186// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
187// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4")
188Value* PatchPartitionCheckFn(const char* name, State* state,
189 const std::vector<std::unique_ptr<Expr>>& argv) {
190 if (argv.size() != 2) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800191 return ErrorAbort(state, kArgsParsingFailure,
Tao Bao5609bc82018-06-20 00:30:48 -0700192 "%s(): Invalid number of args (expected 2, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800193 }
194
195 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700196 if (!ReadArgs(state, argv, &args, 0, 2)) {
197 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800198 }
199
Tao Bao5609bc82018-06-20 00:30:48 -0700200 std::string err;
201 auto target = Partition::Parse(args[0], &err);
202 if (!target) {
203 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
204 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800205 }
206
Tao Bao5609bc82018-06-20 00:30:48 -0700207 auto source = Partition::Parse(args[1], &err);
208 if (!source) {
209 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
210 args[1].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800211 }
212
Tianjie Xu1536db82019-05-14 10:54:43 -0700213 if (!UpdateBlockDeviceNameForPartition(state->updater, &source) ||
214 !UpdateBlockDeviceNameForPartition(state->updater, &target)) {
215 return StringValue("");
216 }
217
Tao Bao5609bc82018-06-20 00:30:48 -0700218 bool result = PatchPartitionCheck(target, source);
219 return StringValue(result ? "t" : "");
Tianjie Xu5419ad32018-02-02 16:49:15 -0800220}
221
Tao Bao5609bc82018-06-20 00:30:48 -0700222// patch_partition(target, source, patch)
223// Applies the given patch to the source partition, and writes the result to the target partition.
224//
225// For example, patch_partition(
226// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
227// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4",
228// package_extract_file("boot.img.p"))
229Value* PatchPartitionFn(const char* name, State* state,
230 const std::vector<std::unique_ptr<Expr>>& argv) {
231 if (argv.size() != 3) {
232 return ErrorAbort(state, kArgsParsingFailure,
233 "%s(): Invalid number of args (expected 3, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800234 }
235
236 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700237 if (!ReadArgs(state, argv, &args, 0, 2)) {
238 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800239 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800240
Tao Bao5609bc82018-06-20 00:30:48 -0700241 std::string err;
242 auto target = Partition::Parse(args[0], &err);
243 if (!target) {
244 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
245 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800246 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800247
Tao Bao5609bc82018-06-20 00:30:48 -0700248 auto source = Partition::Parse(args[1], &err);
249 if (!source) {
250 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
251 args[1].c_str(), err.c_str());
252 }
253
254 std::vector<std::unique_ptr<Value>> values;
255 if (!ReadValueArgs(state, argv, &values, 2, 1) || values[0]->type != Value::Type::BLOB) {
256 return ErrorAbort(state, kArgsParsingFailure, "%s(): Invalid patch arg", name);
257 }
258
Tianjie Xu1536db82019-05-14 10:54:43 -0700259 if (!UpdateBlockDeviceNameForPartition(state->updater, &source) ||
260 !UpdateBlockDeviceNameForPartition(state->updater, &target)) {
261 return StringValue("");
262 }
263
Tao Bao5609bc82018-06-20 00:30:48 -0700264 bool result = PatchPartition(target, source, *values[0], nullptr);
265 return StringValue(result ? "t" : "");
Tianjie Xu5419ad32018-02-02 16:49:15 -0800266}
267
Doug Zongker3d177d02010-07-01 09:18:44 -0700268// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -0700269// mount(fs_type, partition_type, location, mount_point, mount_options)
Tianjie Xuc4447322017-03-06 14:44:59 -0800270
Doug Zongker3d177d02010-07-01 09:18:44 -0700271// fs_type="ext4" partition_type="EMMC" location=device
Tianjie Xuc4447322017-03-06 14:44:59 -0800272Value* MountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
273 if (argv.size() != 4 && argv.size() != 5) {
274 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %zu", name,
275 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800276 }
277
278 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800279 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800280 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
281 }
282 const std::string& fs_type = args[0];
283 const std::string& partition_type = args[1];
284 const std::string& location = args[2];
285 const std::string& mount_point = args[3];
286 std::string mount_options;
287
Tianjie Xuc4447322017-03-06 14:44:59 -0800288 if (argv.size() == 5) {
Tao Bao039f2da2016-11-22 16:29:50 -0800289 mount_options = args[4];
290 }
291
292 if (fs_type.empty()) {
293 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
294 }
295 if (partition_type.empty()) {
296 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
297 name);
298 }
299 if (location.empty()) {
300 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
301 }
302 if (mount_point.empty()) {
303 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
304 name);
305 }
306
Tianjie Xu1536db82019-05-14 10:54:43 -0700307 auto updater = state->updater;
308 if (updater->GetRuntime()->Mount(location, mount_point, fs_type, mount_options) != 0) {
Tianjie Xu58d59122019-05-03 01:05:04 -0700309 updater->UiPrint(android::base::StringPrintf("%s: Failed to mount %s at %s: %s", name,
310 location.c_str(), mount_point.c_str(),
311 strerror(errno)));
Tao Bao039f2da2016-11-22 16:29:50 -0800312 return StringValue("");
313 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700314
Tao Bao039f2da2016-11-22 16:29:50 -0800315 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700316}
317
Doug Zongker8edb00c2009-06-11 17:21:44 -0700318// is_mounted(mount_point)
Tianjie Xuc4447322017-03-06 14:44:59 -0800319Value* IsMountedFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
320 if (argv.size() != 1) {
321 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800322 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700323
Tao Bao039f2da2016-11-22 16:29:50 -0800324 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800325 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800326 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
327 }
328 const std::string& mount_point = args[0];
329 if (mount_point.empty()) {
330 return ErrorAbort(state, kArgsParsingFailure,
331 "mount_point argument to unmount() can't be empty");
332 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700333
Tianjie Xu1536db82019-05-14 10:54:43 -0700334 auto updater_runtime = state->updater->GetRuntime();
335 if (!updater_runtime->IsMounted(mount_point)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800336 return StringValue("");
337 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700338
Tao Bao039f2da2016-11-22 16:29:50 -0800339 return StringValue(mount_point);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700340}
341
Tianjie Xuc4447322017-03-06 14:44:59 -0800342Value* UnmountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
343 if (argv.size() != 1) {
344 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800345 }
346 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& mount_point = args[0];
351 if (mount_point.empty()) {
352 return ErrorAbort(state, kArgsParsingFailure,
353 "mount_point argument to unmount() can't be empty");
354 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700355
Tianjie Xu1536db82019-05-14 10:54:43 -0700356 auto updater = state->updater;
357 auto [mounted, result] = updater->GetRuntime()->Unmount(mount_point);
358 if (!mounted) {
Tianjie Xu58d59122019-05-03 01:05:04 -0700359 updater->UiPrint(
360 android::base::StringPrintf("Failed to unmount %s: No such volume", mount_point.c_str()));
Tao Bao039f2da2016-11-22 16:29:50 -0800361 return nullptr;
Tianjie Xu1536db82019-05-14 10:54:43 -0700362 } else if (result != 0) {
363 updater->UiPrint(android::base::StringPrintf("Failed to unmount %s: %s", mount_point.c_str(),
364 strerror(errno)));
Tao Bao039f2da2016-11-22 16:29:50 -0800365 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700366
Tao Bao039f2da2016-11-22 16:29:50 -0800367 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700368}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700369
Stephen Smalley779701d2012-02-09 14:13:23 -0500370// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700371//
Tao Bao039f2da2016-11-22 16:29:50 -0800372// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
373// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700374// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800375// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700376// 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 -0800377Value* FormatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
378 if (argv.size() != 5) {
379 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %zu", name,
380 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800381 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500382
Tao Bao039f2da2016-11-22 16:29:50 -0800383 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800384 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800385 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
386 }
387 const std::string& fs_type = args[0];
388 const std::string& partition_type = args[1];
389 const std::string& location = args[2];
390 const std::string& fs_size = args[3];
391 const std::string& mount_point = args[4];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700392
Tao Bao039f2da2016-11-22 16:29:50 -0800393 if (fs_type.empty()) {
394 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
395 }
396 if (partition_type.empty()) {
397 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
398 name);
399 }
400 if (location.empty()) {
401 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
402 }
403 if (mount_point.empty()) {
404 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
405 name);
406 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700407
Tao Bao039f2da2016-11-22 16:29:50 -0800408 int64_t size;
409 if (!android::base::ParseInt(fs_size, &size)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800410 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800411 fs_size.c_str());
412 }
413
Tianjie Xu1536db82019-05-14 10:54:43 -0700414 auto updater_runtime = state->updater->GetRuntime();
Tao Bao039f2da2016-11-22 16:29:50 -0800415 if (fs_type == "ext4") {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800416 std::vector<std::string> mke2fs_args = {
417 "/system/bin/mke2fs", "-t", "ext4", "-b", "4096", location
418 };
Jin Qianded2dac2017-04-21 14:36:12 -0700419 if (size != 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800420 mke2fs_args.push_back(std::to_string(size / 4096LL));
Jin Qianded2dac2017-04-21 14:36:12 -0700421 }
422
Tianjie Xu1536db82019-05-14 10:54:43 -0700423 if (auto status = updater_runtime->RunProgram(mke2fs_args, true); status != 0) {
Jin Qian502fd1c2017-11-02 11:58:12 -0700424 LOG(ERROR) << name << ": mke2fs failed (" << status << ") on " << location;
425 return StringValue("");
Jin Qianded2dac2017-04-21 14:36:12 -0700426 }
427
Tianjie Xu1536db82019-05-14 10:54:43 -0700428 if (auto status = updater_runtime->RunProgram(
429 { "/system/bin/e2fsdroid", "-e", "-a", mount_point, location }, true);
Tao Bao3d69f0d2018-12-20 09:44:06 -0800430 status != 0) {
Jin Qianded2dac2017-04-21 14:36:12 -0700431 LOG(ERROR) << name << ": e2fsdroid failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800432 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700433 }
Tao Bao039f2da2016-11-22 16:29:50 -0800434 return StringValue(location);
Tao Bao3d69f0d2018-12-20 09:44:06 -0800435 }
436
437 if (fs_type == "f2fs") {
Tao Bao039f2da2016-11-22 16:29:50 -0800438 if (size < 0) {
439 LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size;
440 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700441 }
Tao Bao3d69f0d2018-12-20 09:44:06 -0800442 std::vector<std::string> f2fs_args = {
Tao Baoc674dfb2018-12-20 14:25:15 -0800443 "/system/bin/make_f2fs", "-g", "android", "-w", "512", location
Tao Bao3d69f0d2018-12-20 09:44:06 -0800444 };
445 if (size >= 512) {
446 f2fs_args.push_back(std::to_string(size / 512));
447 }
Tianjie Xu1536db82019-05-14 10:54:43 -0700448 if (auto status = updater_runtime->RunProgram(f2fs_args, true); status != 0) {
Tao Baoc674dfb2018-12-20 14:25:15 -0800449 LOG(ERROR) << name << ": make_f2fs failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800450 return StringValue("");
451 }
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800452
Tianjie Xu1536db82019-05-14 10:54:43 -0700453 if (auto status = updater_runtime->RunProgram(
454 { "/system/bin/sload_f2fs", "-t", mount_point, location }, true);
Tao Baoc674dfb2018-12-20 14:25:15 -0800455 status != 0) {
456 LOG(ERROR) << name << ": sload_f2fs failed (" << status << ") on " << location;
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800457 return StringValue("");
458 }
459
Tao Bao039f2da2016-11-22 16:29:50 -0800460 return StringValue(location);
Tao Bao039f2da2016-11-22 16:29:50 -0800461 }
462
Tao Bao3d69f0d2018-12-20 09:44:06 -0800463 LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \""
464 << partition_type << "\"";
Tao Bao039f2da2016-11-22 16:29:50 -0800465 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700466}
467
Tianjie Xuc4447322017-03-06 14:44:59 -0800468Value* ShowProgressFn(const char* name, State* state,
469 const std::vector<std::unique_ptr<Expr>>& argv) {
470 if (argv.size() != 2) {
471 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
472 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800473 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700474
Tao Bao039f2da2016-11-22 16:29:50 -0800475 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800476 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800477 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
478 }
479 const std::string& frac_str = args[0];
480 const std::string& sec_str = args[1];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700481
Tao Bao039f2da2016-11-22 16:29:50 -0800482 double frac;
483 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800484 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800485 frac_str.c_str());
486 }
487 int sec;
488 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800489 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800490 sec_str.c_str());
491 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700492
Tianjie Xu1536db82019-05-14 10:54:43 -0700493 state->updater->WriteToCommandPipe(android::base::StringPrintf("progress %f %d", frac, sec));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700494
Tao Bao039f2da2016-11-22 16:29:50 -0800495 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700496}
497
Tianjie Xu5419ad32018-02-02 16:49:15 -0800498Value* SetProgressFn(const char* name, State* state,
499 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800500 if (argv.size() != 1) {
501 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800502 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700503
Tao Bao039f2da2016-11-22 16:29:50 -0800504 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800505 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800506 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
507 }
508 const std::string& frac_str = args[0];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700509
Tao Bao039f2da2016-11-22 16:29:50 -0800510 double frac;
511 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800512 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800513 frac_str.c_str());
514 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700515
Tianjie Xu1536db82019-05-14 10:54:43 -0700516 state->updater->WriteToCommandPipe(android::base::StringPrintf("set_progress %f", frac));
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700517
Tao Bao039f2da2016-11-22 16:29:50 -0800518 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700519}
520
Tianjie Xuc4447322017-03-06 14:44:59 -0800521Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
522 if (argv.size() != 1) {
523 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800524 }
525 std::string key;
526 if (!Evaluate(state, argv[0], &key)) {
527 return nullptr;
528 }
Tianjie Xu1536db82019-05-14 10:54:43 -0700529
530 auto updater_runtime = state->updater->GetRuntime();
531 std::string value = updater_runtime->GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700532
Tao Bao039f2da2016-11-22 16:29:50 -0800533 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700534}
535
Doug Zongker47cace92009-06-18 10:11:50 -0700536// file_getprop(file, key)
537//
538// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -0700539// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700540// and returns the value for 'key' (or "" if it isn't defined).
Tianjie Xu5419ad32018-02-02 16:49:15 -0800541Value* FileGetPropFn(const char* name, State* state,
542 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800543 if (argv.size() != 2) {
544 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
545 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800546 }
547
548 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800549 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800550 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
551 }
552 const std::string& filename = args[0];
553 const std::string& key = args[1];
554
Tianjie Xu22f11202018-08-27 10:50:31 -0700555 std::string buffer;
Tianjie Xu1536db82019-05-14 10:54:43 -0700556 auto updater_runtime = state->updater->GetRuntime();
557 if (!updater_runtime->ReadFileToString(filename, &buffer)) {
Tianjie Xu22f11202018-08-27 10:50:31 -0700558 ErrorAbort(state, kFreadFailure, "%s: failed to read %s", name, filename.c_str());
Tao Bao358c2ec2016-11-28 11:48:43 -0800559 return nullptr;
560 }
561
Tao Bao358c2ec2016-11-28 11:48:43 -0800562 std::vector<std::string> lines = android::base::Split(buffer, "\n");
563 for (size_t i = 0; i < lines.size(); i++) {
564 std::string line = android::base::Trim(lines[i]);
565
566 // comment or blank line: skip to next line
567 if (line.empty() || line[0] == '#') {
568 continue;
569 }
570 size_t equal_pos = line.find('=');
571 if (equal_pos == std::string::npos) {
572 continue;
Tao Bao51d516e2016-11-03 14:49:01 -0700573 }
574
Tao Bao358c2ec2016-11-28 11:48:43 -0800575 // trim whitespace between key and '='
576 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -0700577
Tao Bao358c2ec2016-11-28 11:48:43 -0800578 // not the key we're looking for
579 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700580
Tao Bao358c2ec2016-11-28 11:48:43 -0800581 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
582 }
Doug Zongker47cace92009-06-18 10:11:50 -0700583
Tao Bao358c2ec2016-11-28 11:48:43 -0800584 return StringValue("");
Doug Zongker47cace92009-06-18 10:11:50 -0700585}
586
Doug Zongker8edb00c2009-06-11 17:21:44 -0700587// apply_patch_space(bytes)
Tianjie Xu5419ad32018-02-02 16:49:15 -0800588Value* ApplyPatchSpaceFn(const char* name, State* state,
589 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800590 if (argv.size() != 1) {
591 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name,
592 argv.size());
593 }
Tao Bao039f2da2016-11-22 16:29:50 -0800594 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800595 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800596 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
597 }
598 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800599
Tao Bao039f2da2016-11-22 16:29:50 -0800600 size_t bytes;
601 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800602 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name,
603 bytes_str.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -0800604 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800605
Tianjie Xu99b73be2017-11-28 17:23:06 -0800606 // Skip the cache size check if the update is a retry.
Tao Bao5ee25662018-07-11 15:55:32 -0700607 if (state->is_retry || CheckAndFreeSpaceOnCache(bytes)) {
Tianjie Xu99b73be2017-11-28 17:23:06 -0800608 return StringValue("t");
609 }
610 return StringValue("");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800611}
612
Tianjie Xuc4447322017-03-06 14:44:59 -0800613Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
614 if (!argv.empty()) {
615 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
616 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800617 }
Tianjie Xu58d59122019-05-03 01:05:04 -0700618
Tianjie Xu1536db82019-05-14 10:54:43 -0700619 state->updater->WriteToCommandPipe("wipe_cache");
Tao Bao039f2da2016-11-22 16:29:50 -0800620 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700621}
622
Tianjie Xuc4447322017-03-06 14:44:59 -0800623Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
624 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800625 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
626 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700627
Tao Bao039f2da2016-11-22 16:29:50 -0800628 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800629 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800630 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
631 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700632
Tianjie Xu1536db82019-05-14 10:54:43 -0700633 auto updater_runtime = state->updater->GetRuntime();
634 auto status = updater_runtime->RunProgram(args, false);
Tao Bao039f2da2016-11-22 16:29:50 -0800635 return StringValue(std::to_string(status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700636}
637
Tao Bao511d7592018-06-19 15:56:49 -0700638// read_file(filename)
Tao Baobafd6c72018-07-09 15:08:50 -0700639// Reads a local file 'filename' and returns its contents as a string Value.
Tianjie Xuc4447322017-03-06 14:44:59 -0800640Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
641 if (argv.size() != 1) {
642 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800643 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700644
Tao Bao039f2da2016-11-22 16:29:50 -0800645 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800646 if (!ReadArgs(state, argv, &args)) {
Tao Bao511d7592018-06-19 15:56:49 -0700647 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tao Bao039f2da2016-11-22 16:29:50 -0800648 }
649 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -0800650
Tao Baobafd6c72018-07-09 15:08:50 -0700651 std::string contents;
Tianjie Xu1536db82019-05-14 10:54:43 -0700652 auto updater_runtime = state->updater->GetRuntime();
653 if (updater_runtime->ReadFileToString(filename, &contents)) {
Tao Baobafd6c72018-07-09 15:08:50 -0700654 return new Value(Value::Type::STRING, std::move(contents));
Tao Bao039f2da2016-11-22 16:29:50 -0800655 }
Tao Bao511d7592018-06-19 15:56:49 -0700656
657 // Leave it to caller to handle the failure.
Tao Baobafd6c72018-07-09 15:08:50 -0700658 PLOG(ERROR) << name << ": Failed to read " << filename;
Tao Bao511d7592018-06-19 15:56:49 -0700659 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -0800660}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700661
Tao Baod0f30882016-11-03 23:52:01 -0700662// write_value(value, filename)
663// Writes 'value' to 'filename'.
664// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
Tianjie Xuc4447322017-03-06 14:44:59 -0800665Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
666 if (argv.size() != 2) {
667 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
668 argv.size());
Tao Baod0f30882016-11-03 23:52:01 -0700669 }
670
671 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800672 if (!ReadArgs(state, argv, &args)) {
Tao Baod0f30882016-11-03 23:52:01 -0700673 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
674 }
675
676 const std::string& filename = args[1];
677 if (filename.empty()) {
678 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
679 }
680
681 const std::string& value = args[0];
Tianjie Xu1536db82019-05-14 10:54:43 -0700682 auto updater_runtime = state->updater->GetRuntime();
683 if (!updater_runtime->WriteStringToFile(value, filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800684 PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\"";
Tao Baod0f30882016-11-03 23:52:01 -0700685 return StringValue("");
Tao Baod0f30882016-11-03 23:52:01 -0700686 }
Tianjie Xu1536db82019-05-14 10:54:43 -0700687 return StringValue("t");
Tao Baod0f30882016-11-03 23:52:01 -0700688}
689
Doug Zongkerc87bab12013-11-25 13:53:25 -0800690// Immediately reboot the device. Recovery is not finished normally,
691// so if you reboot into recovery it will re-start applying the
692// current package (because nothing has cleared the copy of the
693// arguments stored in the BCB).
694//
695// The argument is the partition name passed to the android reboot
696// property. It can be "recovery" to boot from the recovery
697// partition, or "" (empty string) to boot from the regular boot
698// partition.
Tianjie Xuc4447322017-03-06 14:44:59 -0800699Value* RebootNowFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
700 if (argv.size() != 2) {
701 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
702 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800703 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800704
Tao Baobedf5fc2016-11-18 12:01:26 -0800705 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800706 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800707 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
708 }
709 const std::string& filename = args[0];
710 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800711
Tao Baobedf5fc2016-11-18 12:01:26 -0800712 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
713 bootloader_message boot;
714 std::string err;
715 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800716 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800717 return StringValue("");
718 }
719 memset(boot.command, 0, sizeof(boot.command));
720 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800721 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800722 return StringValue("");
723 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800724
Tao Bao782dcc12019-04-29 11:23:16 -0700725 Reboot(property);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800726
Tao Baobedf5fc2016-11-18 12:01:26 -0800727 sleep(5);
728 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800729}
730
731// Store a string value somewhere that future invocations of recovery
732// can access it. This value is called the "stage" and can be used to
733// drive packages that need to do reboots in the middle of
734// installation and keep track of where they are in the multi-stage
735// install.
736//
737// The first argument is the block device for the misc partition
738// ("/misc" in the fstab), which is where this value is stored. The
739// second argument is the string to store; it should not exceed 31
740// bytes.
Tianjie Xuc4447322017-03-06 14:44:59 -0800741Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
742 if (argv.size() != 2) {
743 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
744 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800745 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800746
Tao Baobedf5fc2016-11-18 12:01:26 -0800747 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800748 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800749 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
750 }
751 const std::string& filename = args[0];
752 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800753
Tao Baobedf5fc2016-11-18 12:01:26 -0800754 // Store this value in the misc partition, immediately after the
755 // bootloader message that the main recovery uses to save its
756 // arguments in case of the device restarting midway through
757 // package installation.
758 bootloader_message boot;
759 std::string err;
760 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800761 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800762 return StringValue("");
763 }
764 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
765 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800766 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800767 return StringValue("");
768 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800769
Tao Baobedf5fc2016-11-18 12:01:26 -0800770 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800771}
772
773// Return the value most recently saved with SetStageFn. The argument
774// is the block device for the misc partition.
Tianjie Xuc4447322017-03-06 14:44:59 -0800775Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
776 if (argv.size() != 1) {
777 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800778 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800779
Tao Baobedf5fc2016-11-18 12:01:26 -0800780 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800781 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800782 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
783 }
784 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800785
Tao Baobedf5fc2016-11-18 12:01:26 -0800786 bootloader_message boot;
787 std::string err;
788 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800789 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800790 return StringValue("");
791 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800792
Tao Baobedf5fc2016-11-18 12:01:26 -0800793 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800794}
795
Tianjie Xuc4447322017-03-06 14:44:59 -0800796Value* WipeBlockDeviceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
797 if (argv.size() != 2) {
798 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
799 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800800 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800801
Tao Bao358c2ec2016-11-28 11:48:43 -0800802 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800803 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800804 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
805 }
806 const std::string& filename = args[0];
807 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800808
Tao Bao358c2ec2016-11-28 11:48:43 -0800809 size_t len;
810 if (!android::base::ParseUint(len_str.c_str(), &len)) {
811 return nullptr;
812 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700813
Tianjie Xu1536db82019-05-14 10:54:43 -0700814 auto updater_runtime = state->updater->GetRuntime();
815 int status = updater_runtime->WipeBlockDevice(filename, len);
816 return StringValue(status == 0 ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800817}
818
Tianjie Xuc4447322017-03-06 14:44:59 -0800819Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
820 if (!argv.empty()) {
821 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
822 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800823 }
Tianjie Xu1536db82019-05-14 10:54:43 -0700824 state->updater->WriteToCommandPipe("enable_reboot");
Tao Bao039f2da2016-11-22 16:29:50 -0800825 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -0700826}
827
Tianjie Xuc4447322017-03-06 14:44:59 -0800828Value* Tune2FsFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
829 if (argv.empty()) {
830 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800831 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800832
Tao Bao039f2da2016-11-22 16:29:50 -0800833 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800834 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800835 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
836 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800837
Tao Bao3d69f0d2018-12-20 09:44:06 -0800838 // tune2fs expects the program name as its first arg.
839 args.insert(args.begin(), "tune2fs");
Tianjie Xu1536db82019-05-14 10:54:43 -0700840 auto updater_runtime = state->updater->GetRuntime();
841 if (auto result = updater_runtime->Tune2Fs(args); result != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800842 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
843 }
844 return StringValue("t");
Michael Rungeacf47db2014-11-21 00:12:28 -0800845}
846
Doug Zongker9931f7f2009-06-10 14:11:53 -0700847void RegisterInstallFunctions() {
Tao Bao039f2da2016-11-22 16:29:50 -0800848 RegisterFunction("mount", MountFn);
849 RegisterFunction("is_mounted", IsMountedFn);
850 RegisterFunction("unmount", UnmountFn);
851 RegisterFunction("format", FormatFn);
852 RegisterFunction("show_progress", ShowProgressFn);
853 RegisterFunction("set_progress", SetProgressFn);
Tao Bao039f2da2016-11-22 16:29:50 -0800854 RegisterFunction("package_extract_file", PackageExtractFileFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700855
Tao Bao039f2da2016-11-22 16:29:50 -0800856 RegisterFunction("getprop", GetPropFn);
857 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700858
Tao Bao039f2da2016-11-22 16:29:50 -0800859 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Tao Bao5609bc82018-06-20 00:30:48 -0700860 RegisterFunction("patch_partition", PatchPartitionFn);
861 RegisterFunction("patch_partition_check", PatchPartitionCheckFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700862
Tao Bao039f2da2016-11-22 16:29:50 -0800863 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -0800864
Tao Bao039f2da2016-11-22 16:29:50 -0800865 RegisterFunction("read_file", ReadFileFn);
Tao Bao039f2da2016-11-22 16:29:50 -0800866 RegisterFunction("write_value", WriteValueFn);
Doug Zongker512536a2010-02-17 16:11:44 -0800867
Tao Bao039f2da2016-11-22 16:29:50 -0800868 RegisterFunction("wipe_cache", WipeCacheFn);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700869
Tao Bao039f2da2016-11-22 16:29:50 -0800870 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700871
Tao Bao039f2da2016-11-22 16:29:50 -0800872 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800873
Tao Bao039f2da2016-11-22 16:29:50 -0800874 RegisterFunction("reboot_now", RebootNowFn);
875 RegisterFunction("get_stage", GetStageFn);
876 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -0700877
Tao Bao039f2da2016-11-22 16:29:50 -0800878 RegisterFunction("enable_reboot", EnableRebootFn);
879 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700880}