blob: b4d88403c6da5d0617819097a089db8761ded2ee [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"
Tao Bao17054c02018-05-03 22:41:23 -070060#include "otautil/dirutil.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070061#include "otautil/error_code.h"
Tao Bao9a319f02018-01-04 13:19:11 -080062#include "otautil/mounts.h"
Tao Bao09e468f2017-09-29 14:39:33 -070063#include "otautil/print_sha1.h"
Tao Bao2c526392018-05-03 23:01:13 -070064#include "otautil/sysutil.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070065#include "updater/updater.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070066
Tianjie Xu5419ad32018-02-02 16:49:15 -080067// This is the updater side handler for ui_print() in edify script. Contents will be sent over to
68// the recovery side for on-screen display.
69Value* UIPrintFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
70 std::vector<std::string> args;
71 if (!ReadArgs(state, argv, &args)) {
72 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
73 }
74
75 std::string buffer = android::base::Join(args, "");
Tianjie Xu58d59122019-05-03 01:05:04 -070076 static_cast<Updater*>(state->cookie)->UiPrint(buffer);
Tianjie Xu5419ad32018-02-02 16:49:15 -080077 return StringValue(buffer);
78}
79
80// package_extract_file(package_file[, dest_file])
81// Extracts a single package_file from the update package and writes it to dest_file,
82// overwriting existing files if necessary. Without the dest_file argument, returns the
83// contents of the package file as a binary blob.
84Value* PackageExtractFileFn(const char* name, State* state,
85 const std::vector<std::unique_ptr<Expr>>& argv) {
86 if (argv.size() < 1 || argv.size() > 2) {
87 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name,
88 argv.size());
89 }
90
91 if (argv.size() == 2) {
92 // The two-argument version extracts to a file.
93
94 std::vector<std::string> args;
95 if (!ReadArgs(state, argv, &args)) {
96 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
97 argv.size());
98 }
99 const std::string& zip_path = args[0];
100 const std::string& dest_path = args[1];
101
Tianjie Xu58d59122019-05-03 01:05:04 -0700102 ZipArchiveHandle za = static_cast<Updater*>(state->cookie)->package_handle();
Tianjie Xu5419ad32018-02-02 16:49:15 -0800103 ZipEntry entry;
Elliott Hughesa86dddb2019-05-03 22:52:37 -0700104 if (FindEntry(za, zip_path, &entry) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800105 LOG(ERROR) << name << ": no " << zip_path << " in package";
106 return StringValue("");
107 }
108
Tianjie Xu22f11202018-08-27 10:50:31 -0700109 android::base::unique_fd fd(TEMP_FAILURE_RETRY(
110 open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tianjie Xu5419ad32018-02-02 16:49:15 -0800111 if (fd == -1) {
112 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
113 return StringValue("");
114 }
115
116 bool success = true;
117 int32_t ret = ExtractEntryToFile(za, &entry, fd);
118 if (ret != 0) {
119 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
120 << entry.uncompressed_length << " bytes) to \"" << dest_path
121 << "\": " << ErrorCodeString(ret);
122 success = false;
123 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700124 if (fsync(fd) == -1) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800125 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
126 success = false;
127 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700128
129 if (close(fd.release()) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800130 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
131 success = false;
132 }
133
134 return StringValue(success ? "t" : "");
135 } else {
136 // The one-argument version returns the contents of the file as the result.
137
138 std::vector<std::string> args;
139 if (!ReadArgs(state, argv, &args)) {
140 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
141 argv.size());
142 }
143 const std::string& zip_path = args[0];
144
Tianjie Xu58d59122019-05-03 01:05:04 -0700145 ZipArchiveHandle za = static_cast<Updater*>(state->cookie)->package_handle();
Tianjie Xu5419ad32018-02-02 16:49:15 -0800146 ZipEntry entry;
Elliott Hughesa86dddb2019-05-03 22:52:37 -0700147 if (FindEntry(za, zip_path, &entry) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800148 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
149 zip_path.c_str());
150 }
151
152 std::string buffer;
153 buffer.resize(entry.uncompressed_length);
154
155 int32_t ret =
156 ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
157 if (ret != 0) {
158 return ErrorAbort(state, kPackageExtractFileFailure,
159 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
160 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
161 }
162
Tao Bao511d7592018-06-19 15:56:49 -0700163 return new Value(Value::Type::BLOB, buffer);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800164 }
165}
166
Tao Bao5609bc82018-06-20 00:30:48 -0700167// patch_partition_check(target_partition, source_partition)
168// Checks if the target and source partitions have the desired checksums to be patched. It returns
169// directly, if the target partition already has the expected checksum. Otherwise it in turn
170// checks the integrity of the source partition and the backup file on /cache.
Tianjie Xu5419ad32018-02-02 16:49:15 -0800171//
Tao Bao5609bc82018-06-20 00:30:48 -0700172// For example, patch_partition_check(
173// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
174// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4")
175Value* PatchPartitionCheckFn(const char* name, State* state,
176 const std::vector<std::unique_ptr<Expr>>& argv) {
177 if (argv.size() != 2) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800178 return ErrorAbort(state, kArgsParsingFailure,
Tao Bao5609bc82018-06-20 00:30:48 -0700179 "%s(): Invalid number of args (expected 2, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800180 }
181
182 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700183 if (!ReadArgs(state, argv, &args, 0, 2)) {
184 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800185 }
186
Tao Bao5609bc82018-06-20 00:30:48 -0700187 std::string err;
188 auto target = Partition::Parse(args[0], &err);
189 if (!target) {
190 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
191 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800192 }
193
Tao Bao5609bc82018-06-20 00:30:48 -0700194 auto source = Partition::Parse(args[1], &err);
195 if (!source) {
196 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
197 args[1].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800198 }
199
Tao Bao5609bc82018-06-20 00:30:48 -0700200 bool result = PatchPartitionCheck(target, source);
201 return StringValue(result ? "t" : "");
Tianjie Xu5419ad32018-02-02 16:49:15 -0800202}
203
Tao Bao5609bc82018-06-20 00:30:48 -0700204// patch_partition(target, source, patch)
205// Applies the given patch to the source partition, and writes the result to the target partition.
206//
207// For example, patch_partition(
208// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
209// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4",
210// package_extract_file("boot.img.p"))
211Value* PatchPartitionFn(const char* name, State* state,
212 const std::vector<std::unique_ptr<Expr>>& argv) {
213 if (argv.size() != 3) {
214 return ErrorAbort(state, kArgsParsingFailure,
215 "%s(): Invalid number of args (expected 3, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800216 }
217
218 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700219 if (!ReadArgs(state, argv, &args, 0, 2)) {
220 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800221 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800222
Tao Bao5609bc82018-06-20 00:30:48 -0700223 std::string err;
224 auto target = Partition::Parse(args[0], &err);
225 if (!target) {
226 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
227 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800228 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800229
Tao Bao5609bc82018-06-20 00:30:48 -0700230 auto source = Partition::Parse(args[1], &err);
231 if (!source) {
232 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
233 args[1].c_str(), err.c_str());
234 }
235
236 std::vector<std::unique_ptr<Value>> values;
237 if (!ReadValueArgs(state, argv, &values, 2, 1) || values[0]->type != Value::Type::BLOB) {
238 return ErrorAbort(state, kArgsParsingFailure, "%s(): Invalid patch arg", name);
239 }
240
241 bool result = PatchPartition(target, source, *values[0], nullptr);
242 return StringValue(result ? "t" : "");
Tianjie Xu5419ad32018-02-02 16:49:15 -0800243}
244
Doug Zongker3d177d02010-07-01 09:18:44 -0700245// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -0700246// mount(fs_type, partition_type, location, mount_point, mount_options)
Tianjie Xuc4447322017-03-06 14:44:59 -0800247
Doug Zongker3d177d02010-07-01 09:18:44 -0700248// fs_type="ext4" partition_type="EMMC" location=device
Tianjie Xuc4447322017-03-06 14:44:59 -0800249Value* MountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
250 if (argv.size() != 4 && argv.size() != 5) {
251 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %zu", name,
252 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800253 }
254
255 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800256 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800257 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
258 }
259 const std::string& fs_type = args[0];
260 const std::string& partition_type = args[1];
261 const std::string& location = args[2];
262 const std::string& mount_point = args[3];
263 std::string mount_options;
264
Tianjie Xuc4447322017-03-06 14:44:59 -0800265 if (argv.size() == 5) {
Tao Bao039f2da2016-11-22 16:29:50 -0800266 mount_options = args[4];
267 }
268
269 if (fs_type.empty()) {
270 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
271 }
272 if (partition_type.empty()) {
273 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
274 name);
275 }
276 if (location.empty()) {
277 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
278 }
279 if (mount_point.empty()) {
280 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
281 name);
282 }
283
Tianjie Xu58d59122019-05-03 01:05:04 -0700284 auto updater = static_cast<Updater*>(state->cookie);
Tao Bao039f2da2016-11-22 16:29:50 -0800285 {
286 char* secontext = nullptr;
Tianjie Xu58d59122019-05-03 01:05:04 -0700287 if (updater->sehandle()) {
288 selabel_lookup(updater->sehandle(), &secontext, mount_point.c_str(), 0755);
Tao Bao039f2da2016-11-22 16:29:50 -0800289 setfscreatecon(secontext);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700290 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700291
Tao Bao039f2da2016-11-22 16:29:50 -0800292 mkdir(mount_point.c_str(), 0755);
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700293
Tao Bao039f2da2016-11-22 16:29:50 -0800294 if (secontext) {
295 freecon(secontext);
296 setfscreatecon(nullptr);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700297 }
Tao Bao039f2da2016-11-22 16:29:50 -0800298 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700299
Tao Bao039f2da2016-11-22 16:29:50 -0800300 if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(),
301 MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) {
Tianjie Xu58d59122019-05-03 01:05:04 -0700302 updater->UiPrint(android::base::StringPrintf("%s: Failed to mount %s at %s: %s", name,
303 location.c_str(), mount_point.c_str(),
304 strerror(errno)));
Tao Bao039f2da2016-11-22 16:29:50 -0800305 return StringValue("");
306 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700307
Tao Bao039f2da2016-11-22 16:29:50 -0800308 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700309}
310
Doug Zongker8edb00c2009-06-11 17:21:44 -0700311// is_mounted(mount_point)
Tianjie Xuc4447322017-03-06 14:44:59 -0800312Value* IsMountedFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
313 if (argv.size() != 1) {
314 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800315 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700316
Tao Bao039f2da2016-11-22 16:29:50 -0800317 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800318 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800319 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
320 }
321 const std::string& mount_point = args[0];
322 if (mount_point.empty()) {
323 return ErrorAbort(state, kArgsParsingFailure,
324 "mount_point argument to unmount() can't be empty");
325 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700326
Tao Bao039f2da2016-11-22 16:29:50 -0800327 scan_mounted_volumes();
328 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
329 if (vol == nullptr) {
330 return StringValue("");
331 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700332
Tao Bao039f2da2016-11-22 16:29:50 -0800333 return StringValue(mount_point);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700334}
335
Tianjie Xuc4447322017-03-06 14:44:59 -0800336Value* UnmountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
337 if (argv.size() != 1) {
338 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800339 }
340 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800341 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800342 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
343 }
344 const std::string& mount_point = args[0];
345 if (mount_point.empty()) {
346 return ErrorAbort(state, kArgsParsingFailure,
347 "mount_point argument to unmount() can't be empty");
348 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700349
Tianjie Xu58d59122019-05-03 01:05:04 -0700350 auto updater = static_cast<Updater*>(state->cookie);
Tao Bao039f2da2016-11-22 16:29:50 -0800351 scan_mounted_volumes();
352 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
353 if (vol == nullptr) {
Tianjie Xu58d59122019-05-03 01:05:04 -0700354 updater->UiPrint(
355 android::base::StringPrintf("Failed to unmount %s: No such volume", mount_point.c_str()));
Tao Bao039f2da2016-11-22 16:29:50 -0800356 return nullptr;
357 } else {
358 int ret = unmount_mounted_volume(vol);
359 if (ret != 0) {
Tianjie Xu58d59122019-05-03 01:05:04 -0700360 updater->UiPrint(android::base::StringPrintf("Failed to unmount %s: %s", mount_point.c_str(),
361 strerror(errno)));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700362 }
Tao Bao039f2da2016-11-22 16:29:50 -0800363 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700364
Tao Bao039f2da2016-11-22 16:29:50 -0800365 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700366}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700367
Tao Bao3d69f0d2018-12-20 09:44:06 -0800368static int exec_cmd(const std::vector<std::string>& args) {
369 CHECK(!args.empty());
370 auto argv = StringVectorToNullTerminatedArray(args);
371
Tao Bao039f2da2016-11-22 16:29:50 -0800372 pid_t child;
373 if ((child = vfork()) == 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800374 execv(argv[0], argv.data());
Tao Bao3da88012017-02-03 13:09:23 -0800375 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -0800376 }
377
378 int status;
379 waitpid(child, &status, 0);
380 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800381 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
Tao Bao039f2da2016-11-22 16:29:50 -0800382 }
383 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700384}
385
Stephen Smalley779701d2012-02-09 14:13:23 -0500386// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700387//
Tao Bao039f2da2016-11-22 16:29:50 -0800388// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
389// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700390// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800391// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700392// 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 -0800393Value* FormatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
394 if (argv.size() != 5) {
395 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %zu", name,
396 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800397 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500398
Tao Bao039f2da2016-11-22 16:29:50 -0800399 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800400 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800401 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
402 }
403 const std::string& fs_type = args[0];
404 const std::string& partition_type = args[1];
405 const std::string& location = args[2];
406 const std::string& fs_size = args[3];
407 const std::string& mount_point = args[4];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700408
Tao Bao039f2da2016-11-22 16:29:50 -0800409 if (fs_type.empty()) {
410 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
411 }
412 if (partition_type.empty()) {
413 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
414 name);
415 }
416 if (location.empty()) {
417 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
418 }
419 if (mount_point.empty()) {
420 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
421 name);
422 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700423
Tao Bao039f2da2016-11-22 16:29:50 -0800424 int64_t size;
425 if (!android::base::ParseInt(fs_size, &size)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800426 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800427 fs_size.c_str());
428 }
429
430 if (fs_type == "ext4") {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800431 std::vector<std::string> mke2fs_args = {
432 "/system/bin/mke2fs", "-t", "ext4", "-b", "4096", location
433 };
Jin Qianded2dac2017-04-21 14:36:12 -0700434 if (size != 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800435 mke2fs_args.push_back(std::to_string(size / 4096LL));
Jin Qianded2dac2017-04-21 14:36:12 -0700436 }
437
Tao Bao3d69f0d2018-12-20 09:44:06 -0800438 if (auto status = exec_cmd(mke2fs_args); status != 0) {
Jin Qian502fd1c2017-11-02 11:58:12 -0700439 LOG(ERROR) << name << ": mke2fs failed (" << status << ") on " << location;
440 return StringValue("");
Jin Qianded2dac2017-04-21 14:36:12 -0700441 }
442
Tao Bao3d69f0d2018-12-20 09:44:06 -0800443 if (auto status = exec_cmd({ "/system/bin/e2fsdroid", "-e", "-a", mount_point, location });
444 status != 0) {
Jin Qianded2dac2017-04-21 14:36:12 -0700445 LOG(ERROR) << name << ": e2fsdroid failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800446 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700447 }
Tao Bao039f2da2016-11-22 16:29:50 -0800448 return StringValue(location);
Tao Bao3d69f0d2018-12-20 09:44:06 -0800449 }
450
451 if (fs_type == "f2fs") {
Tao Bao039f2da2016-11-22 16:29:50 -0800452 if (size < 0) {
453 LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size;
454 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700455 }
Tao Bao3d69f0d2018-12-20 09:44:06 -0800456 std::vector<std::string> f2fs_args = {
Tao Baoc674dfb2018-12-20 14:25:15 -0800457 "/system/bin/make_f2fs", "-g", "android", "-w", "512", location
Tao Bao3d69f0d2018-12-20 09:44:06 -0800458 };
459 if (size >= 512) {
460 f2fs_args.push_back(std::to_string(size / 512));
461 }
462 if (auto status = exec_cmd(f2fs_args); status != 0) {
Tao Baoc674dfb2018-12-20 14:25:15 -0800463 LOG(ERROR) << name << ": make_f2fs failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800464 return StringValue("");
465 }
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800466
Tao Baoc674dfb2018-12-20 14:25:15 -0800467 if (auto status = exec_cmd({ "/system/bin/sload_f2fs", "-t", mount_point, location });
468 status != 0) {
469 LOG(ERROR) << name << ": sload_f2fs failed (" << status << ") on " << location;
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800470 return StringValue("");
471 }
472
Tao Bao039f2da2016-11-22 16:29:50 -0800473 return StringValue(location);
Tao Bao039f2da2016-11-22 16:29:50 -0800474 }
475
Tao Bao3d69f0d2018-12-20 09:44:06 -0800476 LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \""
477 << partition_type << "\"";
Tao Bao039f2da2016-11-22 16:29:50 -0800478 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700479}
480
Tianjie Xuc4447322017-03-06 14:44:59 -0800481Value* ShowProgressFn(const char* name, State* state,
482 const std::vector<std::unique_ptr<Expr>>& argv) {
483 if (argv.size() != 2) {
484 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
485 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800486 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700487
Tao Bao039f2da2016-11-22 16:29:50 -0800488 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800489 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800490 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
491 }
492 const std::string& frac_str = args[0];
493 const std::string& sec_str = args[1];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700494
Tao Bao039f2da2016-11-22 16:29:50 -0800495 double frac;
496 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800497 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800498 frac_str.c_str());
499 }
500 int sec;
501 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800502 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800503 sec_str.c_str());
504 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700505
Tianjie Xu58d59122019-05-03 01:05:04 -0700506 auto updater = static_cast<Updater*>(state->cookie);
507 updater->WriteToCommandPipe(android::base::StringPrintf("progress %f %d", frac, sec));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700508
Tao Bao039f2da2016-11-22 16:29:50 -0800509 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700510}
511
Tianjie Xu5419ad32018-02-02 16:49:15 -0800512Value* SetProgressFn(const char* name, State* state,
513 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800514 if (argv.size() != 1) {
515 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800516 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700517
Tao Bao039f2da2016-11-22 16:29:50 -0800518 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800519 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800520 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
521 }
522 const std::string& frac_str = args[0];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700523
Tao Bao039f2da2016-11-22 16:29:50 -0800524 double frac;
525 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800526 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800527 frac_str.c_str());
528 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700529
Tianjie Xu58d59122019-05-03 01:05:04 -0700530 auto updater = static_cast<Updater*>(state->cookie);
531 updater->WriteToCommandPipe(android::base::StringPrintf("set_progress %f", frac));
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700532
Tao Bao039f2da2016-11-22 16:29:50 -0800533 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700534}
535
Tianjie Xuc4447322017-03-06 14:44:59 -0800536Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
537 if (argv.size() != 1) {
538 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800539 }
540 std::string key;
541 if (!Evaluate(state, argv[0], &key)) {
542 return nullptr;
543 }
544 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700545
Tao Bao039f2da2016-11-22 16:29:50 -0800546 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700547}
548
Doug Zongker47cace92009-06-18 10:11:50 -0700549// file_getprop(file, key)
550//
551// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -0700552// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700553// and returns the value for 'key' (or "" if it isn't defined).
Tianjie Xu5419ad32018-02-02 16:49:15 -0800554Value* FileGetPropFn(const char* name, State* state,
555 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800556 if (argv.size() != 2) {
557 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
558 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800559 }
560
561 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800562 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800563 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
564 }
565 const std::string& filename = args[0];
566 const std::string& key = args[1];
567
Tianjie Xu22f11202018-08-27 10:50:31 -0700568 std::string buffer;
569 if (!android::base::ReadFileToString(filename, &buffer)) {
570 ErrorAbort(state, kFreadFailure, "%s: failed to read %s", name, filename.c_str());
Tao Bao358c2ec2016-11-28 11:48:43 -0800571 return nullptr;
572 }
573
Tao Bao358c2ec2016-11-28 11:48:43 -0800574 std::vector<std::string> lines = android::base::Split(buffer, "\n");
575 for (size_t i = 0; i < lines.size(); i++) {
576 std::string line = android::base::Trim(lines[i]);
577
578 // comment or blank line: skip to next line
579 if (line.empty() || line[0] == '#') {
580 continue;
581 }
582 size_t equal_pos = line.find('=');
583 if (equal_pos == std::string::npos) {
584 continue;
Tao Bao51d516e2016-11-03 14:49:01 -0700585 }
586
Tao Bao358c2ec2016-11-28 11:48:43 -0800587 // trim whitespace between key and '='
588 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -0700589
Tao Bao358c2ec2016-11-28 11:48:43 -0800590 // not the key we're looking for
591 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700592
Tao Bao358c2ec2016-11-28 11:48:43 -0800593 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
594 }
Doug Zongker47cace92009-06-18 10:11:50 -0700595
Tao Bao358c2ec2016-11-28 11:48:43 -0800596 return StringValue("");
Doug Zongker47cace92009-06-18 10:11:50 -0700597}
598
Doug Zongker8edb00c2009-06-11 17:21:44 -0700599// apply_patch_space(bytes)
Tianjie Xu5419ad32018-02-02 16:49:15 -0800600Value* ApplyPatchSpaceFn(const char* name, State* state,
601 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800602 if (argv.size() != 1) {
603 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name,
604 argv.size());
605 }
Tao Bao039f2da2016-11-22 16:29:50 -0800606 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800607 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800608 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
609 }
610 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800611
Tao Bao039f2da2016-11-22 16:29:50 -0800612 size_t bytes;
613 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800614 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name,
615 bytes_str.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -0800616 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800617
Tianjie Xu99b73be2017-11-28 17:23:06 -0800618 // Skip the cache size check if the update is a retry.
Tao Bao5ee25662018-07-11 15:55:32 -0700619 if (state->is_retry || CheckAndFreeSpaceOnCache(bytes)) {
Tianjie Xu99b73be2017-11-28 17:23:06 -0800620 return StringValue("t");
621 }
622 return StringValue("");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800623}
624
Tianjie Xuc4447322017-03-06 14:44:59 -0800625Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
626 if (!argv.empty()) {
627 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
628 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800629 }
Tianjie Xu58d59122019-05-03 01:05:04 -0700630
631 static_cast<Updater*>(state->cookie)->WriteToCommandPipe("wipe_cache");
Tao Bao039f2da2016-11-22 16:29:50 -0800632 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700633}
634
Tianjie Xuc4447322017-03-06 14:44:59 -0800635Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
636 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800637 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
638 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700639
Tao Bao039f2da2016-11-22 16:29:50 -0800640 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800641 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800642 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
643 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700644
Tao Bao3d69f0d2018-12-20 09:44:06 -0800645 auto exec_args = StringVectorToNullTerminatedArray(args);
646 LOG(INFO) << "about to run program [" << exec_args[0] << "] with " << argv.size() << " args";
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700647
Tao Bao039f2da2016-11-22 16:29:50 -0800648 pid_t child = fork();
649 if (child == 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800650 execv(exec_args[0], exec_args.data());
Tao Bao039f2da2016-11-22 16:29:50 -0800651 PLOG(ERROR) << "run_program: execv failed";
Tao Bao3da88012017-02-03 13:09:23 -0800652 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -0800653 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700654
Tao Bao039f2da2016-11-22 16:29:50 -0800655 int status;
656 waitpid(child, &status, 0);
657 if (WIFEXITED(status)) {
658 if (WEXITSTATUS(status) != 0) {
659 LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status);
660 }
661 } else if (WIFSIGNALED(status)) {
662 LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status);
663 }
664
665 return StringValue(std::to_string(status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700666}
667
Tao Bao511d7592018-06-19 15:56:49 -0700668// read_file(filename)
Tao Baobafd6c72018-07-09 15:08:50 -0700669// Reads a local file 'filename' and returns its contents as a string Value.
Tianjie Xuc4447322017-03-06 14:44:59 -0800670Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
671 if (argv.size() != 1) {
672 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800673 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700674
Tao Bao039f2da2016-11-22 16:29:50 -0800675 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800676 if (!ReadArgs(state, argv, &args)) {
Tao Bao511d7592018-06-19 15:56:49 -0700677 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tao Bao039f2da2016-11-22 16:29:50 -0800678 }
679 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -0800680
Tao Baobafd6c72018-07-09 15:08:50 -0700681 std::string contents;
682 if (android::base::ReadFileToString(filename, &contents)) {
683 return new Value(Value::Type::STRING, std::move(contents));
Tao Bao039f2da2016-11-22 16:29:50 -0800684 }
Tao Bao511d7592018-06-19 15:56:49 -0700685
686 // Leave it to caller to handle the failure.
Tao Baobafd6c72018-07-09 15:08:50 -0700687 PLOG(ERROR) << name << ": Failed to read " << filename;
Tao Bao511d7592018-06-19 15:56:49 -0700688 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -0800689}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700690
Tao Baod0f30882016-11-03 23:52:01 -0700691// write_value(value, filename)
692// Writes 'value' to 'filename'.
693// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
Tianjie Xuc4447322017-03-06 14:44:59 -0800694Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
695 if (argv.size() != 2) {
696 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
697 argv.size());
Tao Baod0f30882016-11-03 23:52:01 -0700698 }
699
700 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800701 if (!ReadArgs(state, argv, &args)) {
Tao Baod0f30882016-11-03 23:52:01 -0700702 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
703 }
704
705 const std::string& filename = args[1];
706 if (filename.empty()) {
707 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
708 }
709
710 const std::string& value = args[0];
711 if (!android::base::WriteStringToFile(value, filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800712 PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\"";
Tao Baod0f30882016-11-03 23:52:01 -0700713 return StringValue("");
714 } else {
715 return StringValue("t");
716 }
717}
718
Doug Zongkerc87bab12013-11-25 13:53:25 -0800719// Immediately reboot the device. Recovery is not finished normally,
720// so if you reboot into recovery it will re-start applying the
721// current package (because nothing has cleared the copy of the
722// arguments stored in the BCB).
723//
724// The argument is the partition name passed to the android reboot
725// property. It can be "recovery" to boot from the recovery
726// partition, or "" (empty string) to boot from the regular boot
727// partition.
Tianjie Xuc4447322017-03-06 14:44:59 -0800728Value* RebootNowFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
729 if (argv.size() != 2) {
730 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
731 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800732 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800733
Tao Baobedf5fc2016-11-18 12:01:26 -0800734 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800735 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800736 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
737 }
738 const std::string& filename = args[0];
739 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800740
Tao Baobedf5fc2016-11-18 12:01:26 -0800741 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
742 bootloader_message boot;
743 std::string err;
744 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800745 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800746 return StringValue("");
747 }
748 memset(boot.command, 0, sizeof(boot.command));
749 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800750 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800751 return StringValue("");
752 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800753
Tao Bao782dcc12019-04-29 11:23:16 -0700754 Reboot(property);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800755
Tao Baobedf5fc2016-11-18 12:01:26 -0800756 sleep(5);
757 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800758}
759
760// Store a string value somewhere that future invocations of recovery
761// can access it. This value is called the "stage" and can be used to
762// drive packages that need to do reboots in the middle of
763// installation and keep track of where they are in the multi-stage
764// install.
765//
766// The first argument is the block device for the misc partition
767// ("/misc" in the fstab), which is where this value is stored. The
768// second argument is the string to store; it should not exceed 31
769// bytes.
Tianjie Xuc4447322017-03-06 14:44:59 -0800770Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
771 if (argv.size() != 2) {
772 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
773 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800774 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800775
Tao Baobedf5fc2016-11-18 12:01:26 -0800776 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800777 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800778 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
779 }
780 const std::string& filename = args[0];
781 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800782
Tao Baobedf5fc2016-11-18 12:01:26 -0800783 // Store this value in the misc partition, immediately after the
784 // bootloader message that the main recovery uses to save its
785 // arguments in case of the device restarting midway through
786 // package installation.
787 bootloader_message boot;
788 std::string err;
789 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800790 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800791 return StringValue("");
792 }
793 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
794 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800795 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800796 return StringValue("");
797 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800798
Tao Baobedf5fc2016-11-18 12:01:26 -0800799 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800800}
801
802// Return the value most recently saved with SetStageFn. The argument
803// is the block device for the misc partition.
Tianjie Xuc4447322017-03-06 14:44:59 -0800804Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
805 if (argv.size() != 1) {
806 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800807 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800808
Tao Baobedf5fc2016-11-18 12:01:26 -0800809 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800810 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800811 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
812 }
813 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800814
Tao Baobedf5fc2016-11-18 12:01:26 -0800815 bootloader_message boot;
816 std::string err;
817 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800818 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800819 return StringValue("");
820 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800821
Tao Baobedf5fc2016-11-18 12:01:26 -0800822 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800823}
824
Tianjie Xuc4447322017-03-06 14:44:59 -0800825Value* WipeBlockDeviceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
826 if (argv.size() != 2) {
827 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
828 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800829 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800830
Tao Bao358c2ec2016-11-28 11:48:43 -0800831 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800832 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800833 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
834 }
835 const std::string& filename = args[0];
836 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800837
Tao Bao358c2ec2016-11-28 11:48:43 -0800838 size_t len;
839 if (!android::base::ParseUint(len_str.c_str(), &len)) {
840 return nullptr;
841 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700842 android::base::unique_fd fd(open(filename.c_str(), O_WRONLY));
843 if (fd == -1) {
844 PLOG(ERROR) << "Failed to open " << filename;
845 return StringValue("");
846 }
847
Tao Bao358c2ec2016-11-28 11:48:43 -0800848 // The wipe_block_device function in ext4_utils returns 0 on success and 1
849 // for failure.
850 int status = wipe_block_device(fd, len);
851 return StringValue((status == 0) ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800852}
853
Tianjie Xuc4447322017-03-06 14:44:59 -0800854Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
855 if (!argv.empty()) {
856 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
857 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800858 }
Tianjie Xu58d59122019-05-03 01:05:04 -0700859 static_cast<Updater*>(state->cookie)->WriteToCommandPipe("enable_reboot");
Tao Bao039f2da2016-11-22 16:29:50 -0800860 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -0700861}
862
Tianjie Xuc4447322017-03-06 14:44:59 -0800863Value* Tune2FsFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
864 if (argv.empty()) {
865 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800866 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800867
Tao Bao039f2da2016-11-22 16:29:50 -0800868 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800869 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800870 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
871 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800872
Tao Bao3d69f0d2018-12-20 09:44:06 -0800873 // tune2fs expects the program name as its first arg.
874 args.insert(args.begin(), "tune2fs");
875 auto tune2fs_args = StringVectorToNullTerminatedArray(args);
Michael Rungeacf47db2014-11-21 00:12:28 -0800876
Tao Bao3d69f0d2018-12-20 09:44:06 -0800877 // tune2fs changes the filesystem parameters on an ext2 filesystem; it returns 0 on success.
878 if (auto result = tune2fs_main(tune2fs_args.size() - 1, tune2fs_args.data()); result != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800879 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
880 }
881 return StringValue("t");
Michael Rungeacf47db2014-11-21 00:12:28 -0800882}
883
Doug Zongker9931f7f2009-06-10 14:11:53 -0700884void RegisterInstallFunctions() {
Tao Bao039f2da2016-11-22 16:29:50 -0800885 RegisterFunction("mount", MountFn);
886 RegisterFunction("is_mounted", IsMountedFn);
887 RegisterFunction("unmount", UnmountFn);
888 RegisterFunction("format", FormatFn);
889 RegisterFunction("show_progress", ShowProgressFn);
890 RegisterFunction("set_progress", SetProgressFn);
Tao Bao039f2da2016-11-22 16:29:50 -0800891 RegisterFunction("package_extract_file", PackageExtractFileFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700892
Tao Bao039f2da2016-11-22 16:29:50 -0800893 RegisterFunction("getprop", GetPropFn);
894 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700895
Tao Bao039f2da2016-11-22 16:29:50 -0800896 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Tao Bao5609bc82018-06-20 00:30:48 -0700897 RegisterFunction("patch_partition", PatchPartitionFn);
898 RegisterFunction("patch_partition_check", PatchPartitionCheckFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700899
Tao Bao039f2da2016-11-22 16:29:50 -0800900 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -0800901
Tao Bao039f2da2016-11-22 16:29:50 -0800902 RegisterFunction("read_file", ReadFileFn);
Tao Bao039f2da2016-11-22 16:29:50 -0800903 RegisterFunction("write_value", WriteValueFn);
Doug Zongker512536a2010-02-17 16:11:44 -0800904
Tao Bao039f2da2016-11-22 16:29:50 -0800905 RegisterFunction("wipe_cache", WipeCacheFn);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700906
Tao Bao039f2da2016-11-22 16:29:50 -0800907 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700908
Tao Bao039f2da2016-11-22 16:29:50 -0800909 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800910
Tao Bao039f2da2016-11-22 16:29:50 -0800911 RegisterFunction("reboot_now", RebootNowFn);
912 RegisterFunction("get_stage", GetStageFn);
913 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -0700914
Tao Bao039f2da2016-11-22 16:29:50 -0800915 RegisterFunction("enable_reboot", EnableRebootFn);
916 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700917}