blob: 62ff87e762937181e1afac816506400383a2c458 [file] [log] [blame]
Doug Zongker9931f7f2009-06-10 14:11:53 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tao Bao0c7839a2016-10-10 15:48:37 -070017#include "updater/install.h"
18
Doug Zongkerfbf3c102009-06-24 09:36:20 -070019#include <ctype.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070020#include <errno.h>
Tao Bao361342c2016-02-08 11:15:50 -080021#include <fcntl.h>
22#include <ftw.h>
23#include <inttypes.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070024#include <stdarg.h>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070025#include <stdio.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070026#include <stdlib.h>
27#include <string.h>
Tao Bao361342c2016-02-08 11:15:50 -080028#include <sys/capability.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070029#include <sys/mount.h>
30#include <sys/stat.h>
31#include <sys/types.h>
Doug Zongkera3f89ea2009-09-10 14:10:48 -070032#include <sys/wait.h>
Nick Kralevich5dbdef02013-09-07 14:41:06 -070033#include <sys/xattr.h>
Tao Bao361342c2016-02-08 11:15:50 -080034#include <time.h>
35#include <unistd.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070036#include <utime.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070037
Yabin Cui64be2132016-02-03 18:16:02 -080038#include <memory>
Tao Bao361342c2016-02-08 11:15:50 -080039#include <string>
Yabin Cui64be2132016-02-03 18:16:02 -080040#include <vector>
41
Tao Baod0f30882016-11-03 23:52:01 -070042#include <android-base/file.h>
Tao Bao039f2da2016-11-22 16:29:50 -080043#include <android-base/logging.h>
Tianjie Xu5fe280a2016-10-17 18:15:20 -070044#include <android-base/parsedouble.h>
Tao Baod0f30882016-11-03 23:52:01 -070045#include <android-base/parseint.h>
Elliott Hughescb220402016-09-23 15:30:55 -070046#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080047#include <android-base/stringprintf.h>
Tao Baod0f30882016-11-03 23:52:01 -070048#include <android-base/strings.h>
Tianjie Xu22f11202018-08-27 10:50:31 -070049#include <android-base/unique_fd.h>
Tao Bao0d3f84f2016-12-28 15:09:20 -080050#include <applypatch/applypatch.h>
51#include <bootloader_message/bootloader_message.h>
Tao Baode40ba52016-10-05 23:17:01 -070052#include <ext4_utils/wipe.h>
Tao Bao361342c2016-02-08 11:15:50 -080053#include <openssl/sha.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070054#include <selinux/label.h>
55#include <selinux/selinux.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070056#include <ziparchive/zip_archive.h>
Tao Bao1107d962015-09-09 17:16:55 -070057
Doug Zongker9931f7f2009-06-10 14:11:53 -070058#include "edify/expr.h"
Tianjie Xu1536db82019-05-14 10:54:43 -070059#include "edify/updater_interface.h"
60#include "edify/updater_runtime_interface.h"
Tao Bao17054c02018-05-03 22:41:23 -070061#include "otautil/dirutil.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070062#include "otautil/error_code.h"
Tao Bao09e468f2017-09-29 14:39:33 -070063#include "otautil/print_sha1.h"
Tao Bao2c526392018-05-03 23:01:13 -070064#include "otautil/sysutil.h"
Tianjie Xu1536db82019-05-14 10:54:43 -070065
Tianjie Xu27556d02019-05-22 14:48:35 -070066#ifndef __ANDROID__
67#include <cutils/memory.h> // for strlcpy
68#endif
69
Tianjie Xu1536db82019-05-14 10:54:43 -070070static bool UpdateBlockDeviceNameForPartition(UpdaterInterface* updater, Partition* partition) {
71 CHECK(updater);
72 std::string name = updater->FindBlockDeviceName(partition->name);
73 if (name.empty()) {
74 LOG(ERROR) << "Failed to find the block device " << partition->name;
75 return false;
76 }
77
78 partition->name = std::move(name);
79 return true;
80}
Doug Zongker8edb00c2009-06-11 17:21:44 -070081
Tianjie Xu5419ad32018-02-02 16:49:15 -080082// This is the updater side handler for ui_print() in edify script. Contents will be sent over to
83// the recovery side for on-screen display.
84Value* UIPrintFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
85 std::vector<std::string> args;
86 if (!ReadArgs(state, argv, &args)) {
87 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
88 }
89
90 std::string buffer = android::base::Join(args, "");
Tianjie Xu1536db82019-05-14 10:54:43 -070091 state->updater->UiPrint(buffer);
Tianjie Xu5419ad32018-02-02 16:49:15 -080092 return StringValue(buffer);
93}
94
95// package_extract_file(package_file[, dest_file])
96// Extracts a single package_file from the update package and writes it to dest_file,
97// overwriting existing files if necessary. Without the dest_file argument, returns the
98// contents of the package file as a binary blob.
99Value* PackageExtractFileFn(const char* name, State* state,
100 const std::vector<std::unique_ptr<Expr>>& argv) {
101 if (argv.size() < 1 || argv.size() > 2) {
102 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name,
103 argv.size());
104 }
105
106 if (argv.size() == 2) {
107 // The two-argument version extracts to a file.
108
109 std::vector<std::string> args;
110 if (!ReadArgs(state, argv, &args)) {
111 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
112 argv.size());
113 }
114 const std::string& zip_path = args[0];
Tianjie Xu60b242c2019-07-30 16:48:52 -0700115 std::string dest_path = args[1];
Tianjie Xu5419ad32018-02-02 16:49:15 -0800116
Tianjie Xu1536db82019-05-14 10:54:43 -0700117 ZipArchiveHandle za = state->updater->GetPackageHandle();
Tianjie Xu5419ad32018-02-02 16:49:15 -0800118 ZipEntry entry;
Elliott Hughesa86dddb2019-05-03 22:52:37 -0700119 if (FindEntry(za, zip_path, &entry) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800120 LOG(ERROR) << name << ": no " << zip_path << " in package";
121 return StringValue("");
122 }
123
Tianjie Xu60b242c2019-07-30 16:48:52 -0700124 // Update the destination of package_extract_file if it's a block device. During simulation the
125 // destination will map to a fake file.
126 if (std::string block_device_name = state->updater->FindBlockDeviceName(dest_path);
127 !block_device_name.empty()) {
128 dest_path = block_device_name;
129 }
130
Tianjie Xu22f11202018-08-27 10:50:31 -0700131 android::base::unique_fd fd(TEMP_FAILURE_RETRY(
132 open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tianjie Xu5419ad32018-02-02 16:49:15 -0800133 if (fd == -1) {
134 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
135 return StringValue("");
136 }
137
138 bool success = true;
139 int32_t ret = ExtractEntryToFile(za, &entry, fd);
140 if (ret != 0) {
141 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
142 << entry.uncompressed_length << " bytes) to \"" << dest_path
143 << "\": " << ErrorCodeString(ret);
144 success = false;
145 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700146 if (fsync(fd) == -1) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800147 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
148 success = false;
149 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700150
151 if (close(fd.release()) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800152 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
153 success = false;
154 }
155
156 return StringValue(success ? "t" : "");
157 } else {
158 // The one-argument version returns the contents of the file as the result.
159
160 std::vector<std::string> args;
161 if (!ReadArgs(state, argv, &args)) {
162 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
163 argv.size());
164 }
165 const std::string& zip_path = args[0];
166
Tianjie Xu1536db82019-05-14 10:54:43 -0700167 ZipArchiveHandle za = state->updater->GetPackageHandle();
Tianjie Xu5419ad32018-02-02 16:49:15 -0800168 ZipEntry entry;
Elliott Hughesa86dddb2019-05-03 22:52:37 -0700169 if (FindEntry(za, zip_path, &entry) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800170 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
171 zip_path.c_str());
172 }
173
174 std::string buffer;
175 buffer.resize(entry.uncompressed_length);
176
177 int32_t ret =
178 ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
179 if (ret != 0) {
180 return ErrorAbort(state, kPackageExtractFileFailure,
181 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
182 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
183 }
184
Tao Bao511d7592018-06-19 15:56:49 -0700185 return new Value(Value::Type::BLOB, buffer);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800186 }
187}
188
Tao Bao5609bc82018-06-20 00:30:48 -0700189// patch_partition_check(target_partition, source_partition)
190// Checks if the target and source partitions have the desired checksums to be patched. It returns
191// directly, if the target partition already has the expected checksum. Otherwise it in turn
192// checks the integrity of the source partition and the backup file on /cache.
Tianjie Xu5419ad32018-02-02 16:49:15 -0800193//
Tao Bao5609bc82018-06-20 00:30:48 -0700194// For example, patch_partition_check(
195// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
196// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4")
197Value* PatchPartitionCheckFn(const char* name, State* state,
198 const std::vector<std::unique_ptr<Expr>>& argv) {
199 if (argv.size() != 2) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800200 return ErrorAbort(state, kArgsParsingFailure,
Tao Bao5609bc82018-06-20 00:30:48 -0700201 "%s(): Invalid number of args (expected 2, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800202 }
203
204 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700205 if (!ReadArgs(state, argv, &args, 0, 2)) {
206 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800207 }
208
Tao Bao5609bc82018-06-20 00:30:48 -0700209 std::string err;
210 auto target = Partition::Parse(args[0], &err);
211 if (!target) {
212 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
213 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800214 }
215
Tao Bao5609bc82018-06-20 00:30:48 -0700216 auto source = Partition::Parse(args[1], &err);
217 if (!source) {
218 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
219 args[1].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800220 }
221
Tianjie Xu1536db82019-05-14 10:54:43 -0700222 if (!UpdateBlockDeviceNameForPartition(state->updater, &source) ||
223 !UpdateBlockDeviceNameForPartition(state->updater, &target)) {
224 return StringValue("");
225 }
226
Tao Bao5609bc82018-06-20 00:30:48 -0700227 bool result = PatchPartitionCheck(target, source);
228 return StringValue(result ? "t" : "");
Tianjie Xu5419ad32018-02-02 16:49:15 -0800229}
230
Tao Bao5609bc82018-06-20 00:30:48 -0700231// patch_partition(target, source, patch)
232// Applies the given patch to the source partition, and writes the result to the target partition.
233//
234// For example, patch_partition(
235// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
236// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4",
237// package_extract_file("boot.img.p"))
238Value* PatchPartitionFn(const char* name, State* state,
239 const std::vector<std::unique_ptr<Expr>>& argv) {
240 if (argv.size() != 3) {
241 return ErrorAbort(state, kArgsParsingFailure,
242 "%s(): Invalid number of args (expected 3, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800243 }
244
245 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700246 if (!ReadArgs(state, argv, &args, 0, 2)) {
247 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800248 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800249
Tao Bao5609bc82018-06-20 00:30:48 -0700250 std::string err;
251 auto target = Partition::Parse(args[0], &err);
252 if (!target) {
253 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
254 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800255 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800256
Tao Bao5609bc82018-06-20 00:30:48 -0700257 auto source = Partition::Parse(args[1], &err);
258 if (!source) {
259 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
260 args[1].c_str(), err.c_str());
261 }
262
263 std::vector<std::unique_ptr<Value>> values;
264 if (!ReadValueArgs(state, argv, &values, 2, 1) || values[0]->type != Value::Type::BLOB) {
265 return ErrorAbort(state, kArgsParsingFailure, "%s(): Invalid patch arg", name);
266 }
267
Tianjie Xu1536db82019-05-14 10:54:43 -0700268 if (!UpdateBlockDeviceNameForPartition(state->updater, &source) ||
269 !UpdateBlockDeviceNameForPartition(state->updater, &target)) {
270 return StringValue("");
271 }
272
Tao Bao5234ad42019-09-23 10:28:54 -0700273 bool result = PatchPartition(target, source, *values[0], nullptr, true);
Tao Bao5609bc82018-06-20 00:30:48 -0700274 return StringValue(result ? "t" : "");
Tianjie Xu5419ad32018-02-02 16:49:15 -0800275}
276
Doug Zongker3d177d02010-07-01 09:18:44 -0700277// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -0700278// mount(fs_type, partition_type, location, mount_point, mount_options)
Tianjie Xuc4447322017-03-06 14:44:59 -0800279
Doug Zongker3d177d02010-07-01 09:18:44 -0700280// fs_type="ext4" partition_type="EMMC" location=device
Tianjie Xuc4447322017-03-06 14:44:59 -0800281Value* MountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
282 if (argv.size() != 4 && argv.size() != 5) {
283 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %zu", name,
284 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800285 }
286
287 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800288 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800289 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
290 }
291 const std::string& fs_type = args[0];
292 const std::string& partition_type = args[1];
293 const std::string& location = args[2];
294 const std::string& mount_point = args[3];
295 std::string mount_options;
296
Tianjie Xuc4447322017-03-06 14:44:59 -0800297 if (argv.size() == 5) {
Tao Bao039f2da2016-11-22 16:29:50 -0800298 mount_options = args[4];
299 }
300
301 if (fs_type.empty()) {
302 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
303 }
304 if (partition_type.empty()) {
305 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
306 name);
307 }
308 if (location.empty()) {
309 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
310 }
311 if (mount_point.empty()) {
312 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
313 name);
314 }
315
Tianjie Xu1536db82019-05-14 10:54:43 -0700316 auto updater = state->updater;
317 if (updater->GetRuntime()->Mount(location, mount_point, fs_type, mount_options) != 0) {
Tianjie Xu58d59122019-05-03 01:05:04 -0700318 updater->UiPrint(android::base::StringPrintf("%s: Failed to mount %s at %s: %s", name,
319 location.c_str(), mount_point.c_str(),
320 strerror(errno)));
Tao Bao039f2da2016-11-22 16:29:50 -0800321 return StringValue("");
322 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700323
Tao Bao039f2da2016-11-22 16:29:50 -0800324 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700325}
326
Doug Zongker8edb00c2009-06-11 17:21:44 -0700327// is_mounted(mount_point)
Tianjie Xuc4447322017-03-06 14:44:59 -0800328Value* IsMountedFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
329 if (argv.size() != 1) {
330 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800331 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700332
Tao Bao039f2da2016-11-22 16:29:50 -0800333 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800334 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800335 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
336 }
337 const std::string& mount_point = args[0];
338 if (mount_point.empty()) {
339 return ErrorAbort(state, kArgsParsingFailure,
340 "mount_point argument to unmount() can't be empty");
341 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700342
Tianjie Xu1536db82019-05-14 10:54:43 -0700343 auto updater_runtime = state->updater->GetRuntime();
344 if (!updater_runtime->IsMounted(mount_point)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800345 return StringValue("");
346 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700347
Tao Bao039f2da2016-11-22 16:29:50 -0800348 return StringValue(mount_point);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700349}
350
Tianjie Xuc4447322017-03-06 14:44:59 -0800351Value* UnmountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
352 if (argv.size() != 1) {
353 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800354 }
355 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800356 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800357 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
358 }
359 const std::string& mount_point = args[0];
360 if (mount_point.empty()) {
361 return ErrorAbort(state, kArgsParsingFailure,
362 "mount_point argument to unmount() can't be empty");
363 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700364
Tianjie Xu1536db82019-05-14 10:54:43 -0700365 auto updater = state->updater;
366 auto [mounted, result] = updater->GetRuntime()->Unmount(mount_point);
367 if (!mounted) {
Tianjie Xu58d59122019-05-03 01:05:04 -0700368 updater->UiPrint(
369 android::base::StringPrintf("Failed to unmount %s: No such volume", mount_point.c_str()));
Tao Bao039f2da2016-11-22 16:29:50 -0800370 return nullptr;
Tianjie Xu1536db82019-05-14 10:54:43 -0700371 } else if (result != 0) {
372 updater->UiPrint(android::base::StringPrintf("Failed to unmount %s: %s", mount_point.c_str(),
373 strerror(errno)));
Tao Bao039f2da2016-11-22 16:29:50 -0800374 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700375
Tao Bao039f2da2016-11-22 16:29:50 -0800376 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700377}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700378
Stephen Smalley779701d2012-02-09 14:13:23 -0500379// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700380//
Tao Bao039f2da2016-11-22 16:29:50 -0800381// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
382// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700383// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800384// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700385// 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 -0800386Value* FormatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
387 if (argv.size() != 5) {
388 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %zu", name,
389 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800390 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500391
Tao Bao039f2da2016-11-22 16:29:50 -0800392 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800393 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800394 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
395 }
396 const std::string& fs_type = args[0];
397 const std::string& partition_type = args[1];
398 const std::string& location = args[2];
399 const std::string& fs_size = args[3];
400 const std::string& mount_point = args[4];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700401
Tao Bao039f2da2016-11-22 16:29:50 -0800402 if (fs_type.empty()) {
403 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
404 }
405 if (partition_type.empty()) {
406 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
407 name);
408 }
409 if (location.empty()) {
410 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
411 }
412 if (mount_point.empty()) {
413 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
414 name);
415 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700416
Tao Bao039f2da2016-11-22 16:29:50 -0800417 int64_t size;
418 if (!android::base::ParseInt(fs_size, &size)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800419 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800420 fs_size.c_str());
421 }
422
Tianjie Xu1536db82019-05-14 10:54:43 -0700423 auto updater_runtime = state->updater->GetRuntime();
Tao Bao039f2da2016-11-22 16:29:50 -0800424 if (fs_type == "ext4") {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800425 std::vector<std::string> mke2fs_args = {
426 "/system/bin/mke2fs", "-t", "ext4", "-b", "4096", location
427 };
Jin Qianded2dac2017-04-21 14:36:12 -0700428 if (size != 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800429 mke2fs_args.push_back(std::to_string(size / 4096LL));
Jin Qianded2dac2017-04-21 14:36:12 -0700430 }
431
Tianjie Xu1536db82019-05-14 10:54:43 -0700432 if (auto status = updater_runtime->RunProgram(mke2fs_args, true); status != 0) {
Jin Qian502fd1c2017-11-02 11:58:12 -0700433 LOG(ERROR) << name << ": mke2fs failed (" << status << ") on " << location;
434 return StringValue("");
Jin Qianded2dac2017-04-21 14:36:12 -0700435 }
436
Tianjie Xu1536db82019-05-14 10:54:43 -0700437 if (auto status = updater_runtime->RunProgram(
438 { "/system/bin/e2fsdroid", "-e", "-a", mount_point, location }, true);
Tao Bao3d69f0d2018-12-20 09:44:06 -0800439 status != 0) {
Jin Qianded2dac2017-04-21 14:36:12 -0700440 LOG(ERROR) << name << ": e2fsdroid failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800441 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700442 }
Tao Bao039f2da2016-11-22 16:29:50 -0800443 return StringValue(location);
Tao Bao3d69f0d2018-12-20 09:44:06 -0800444 }
445
446 if (fs_type == "f2fs") {
Tao Bao039f2da2016-11-22 16:29:50 -0800447 if (size < 0) {
448 LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size;
449 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700450 }
Tao Bao3d69f0d2018-12-20 09:44:06 -0800451 std::vector<std::string> f2fs_args = {
Tao Baoc674dfb2018-12-20 14:25:15 -0800452 "/system/bin/make_f2fs", "-g", "android", "-w", "512", location
Tao Bao3d69f0d2018-12-20 09:44:06 -0800453 };
454 if (size >= 512) {
455 f2fs_args.push_back(std::to_string(size / 512));
456 }
Tianjie Xu1536db82019-05-14 10:54:43 -0700457 if (auto status = updater_runtime->RunProgram(f2fs_args, true); status != 0) {
Tao Baoc674dfb2018-12-20 14:25:15 -0800458 LOG(ERROR) << name << ": make_f2fs failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800459 return StringValue("");
460 }
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800461
Tianjie Xu1536db82019-05-14 10:54:43 -0700462 if (auto status = updater_runtime->RunProgram(
463 { "/system/bin/sload_f2fs", "-t", mount_point, location }, true);
Tao Baoc674dfb2018-12-20 14:25:15 -0800464 status != 0) {
465 LOG(ERROR) << name << ": sload_f2fs failed (" << status << ") on " << location;
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800466 return StringValue("");
467 }
468
Tao Bao039f2da2016-11-22 16:29:50 -0800469 return StringValue(location);
Tao Bao039f2da2016-11-22 16:29:50 -0800470 }
471
Tao Bao3d69f0d2018-12-20 09:44:06 -0800472 LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \""
473 << partition_type << "\"";
Tao Bao039f2da2016-11-22 16:29:50 -0800474 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700475}
476
Tianjie Xuc4447322017-03-06 14:44:59 -0800477Value* ShowProgressFn(const char* name, State* state,
478 const std::vector<std::unique_ptr<Expr>>& argv) {
479 if (argv.size() != 2) {
480 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
481 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800482 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700483
Tao Bao039f2da2016-11-22 16:29:50 -0800484 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800485 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800486 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
487 }
488 const std::string& frac_str = args[0];
489 const std::string& sec_str = args[1];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700490
Tao Bao039f2da2016-11-22 16:29:50 -0800491 double frac;
492 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800493 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800494 frac_str.c_str());
495 }
496 int sec;
497 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800498 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800499 sec_str.c_str());
500 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700501
Tianjie Xu1536db82019-05-14 10:54:43 -0700502 state->updater->WriteToCommandPipe(android::base::StringPrintf("progress %f %d", frac, sec));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700503
Tao Bao039f2da2016-11-22 16:29:50 -0800504 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700505}
506
Tianjie Xu5419ad32018-02-02 16:49:15 -0800507Value* SetProgressFn(const char* name, State* state,
508 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800509 if (argv.size() != 1) {
510 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800511 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700512
Tao Bao039f2da2016-11-22 16:29:50 -0800513 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800514 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800515 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
516 }
517 const std::string& frac_str = args[0];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700518
Tao Bao039f2da2016-11-22 16:29:50 -0800519 double frac;
520 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800521 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800522 frac_str.c_str());
523 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700524
Tianjie Xu1536db82019-05-14 10:54:43 -0700525 state->updater->WriteToCommandPipe(android::base::StringPrintf("set_progress %f", frac));
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700526
Tao Bao039f2da2016-11-22 16:29:50 -0800527 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700528}
529
Tianjie Xuc4447322017-03-06 14:44:59 -0800530Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
531 if (argv.size() != 1) {
532 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800533 }
534 std::string key;
535 if (!Evaluate(state, argv[0], &key)) {
536 return nullptr;
537 }
Tianjie Xu1536db82019-05-14 10:54:43 -0700538
539 auto updater_runtime = state->updater->GetRuntime();
540 std::string value = updater_runtime->GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700541
Tao Bao039f2da2016-11-22 16:29:50 -0800542 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700543}
544
Doug Zongker47cace92009-06-18 10:11:50 -0700545// file_getprop(file, key)
546//
547// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -0700548// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700549// and returns the value for 'key' (or "" if it isn't defined).
Tianjie Xu5419ad32018-02-02 16:49:15 -0800550Value* FileGetPropFn(const char* name, State* state,
551 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800552 if (argv.size() != 2) {
553 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
554 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800555 }
556
557 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800558 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800559 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
560 }
561 const std::string& filename = args[0];
562 const std::string& key = args[1];
563
Tianjie Xu22f11202018-08-27 10:50:31 -0700564 std::string buffer;
Tianjie Xu1536db82019-05-14 10:54:43 -0700565 auto updater_runtime = state->updater->GetRuntime();
566 if (!updater_runtime->ReadFileToString(filename, &buffer)) {
Tianjie Xu22f11202018-08-27 10:50:31 -0700567 ErrorAbort(state, kFreadFailure, "%s: failed to read %s", name, filename.c_str());
Tao Bao358c2ec2016-11-28 11:48:43 -0800568 return nullptr;
569 }
570
Tao Bao358c2ec2016-11-28 11:48:43 -0800571 std::vector<std::string> lines = android::base::Split(buffer, "\n");
572 for (size_t i = 0; i < lines.size(); i++) {
573 std::string line = android::base::Trim(lines[i]);
574
575 // comment or blank line: skip to next line
576 if (line.empty() || line[0] == '#') {
577 continue;
578 }
579 size_t equal_pos = line.find('=');
580 if (equal_pos == std::string::npos) {
581 continue;
Tao Bao51d516e2016-11-03 14:49:01 -0700582 }
583
Tao Bao358c2ec2016-11-28 11:48:43 -0800584 // trim whitespace between key and '='
585 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -0700586
Tao Bao358c2ec2016-11-28 11:48:43 -0800587 // not the key we're looking for
588 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700589
Tao Bao358c2ec2016-11-28 11:48:43 -0800590 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
591 }
Doug Zongker47cace92009-06-18 10:11:50 -0700592
Tao Bao358c2ec2016-11-28 11:48:43 -0800593 return StringValue("");
Doug Zongker47cace92009-06-18 10:11:50 -0700594}
595
Doug Zongker8edb00c2009-06-11 17:21:44 -0700596// apply_patch_space(bytes)
Tianjie Xu5419ad32018-02-02 16:49:15 -0800597Value* ApplyPatchSpaceFn(const char* name, State* state,
598 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800599 if (argv.size() != 1) {
600 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name,
601 argv.size());
602 }
Tao Bao039f2da2016-11-22 16:29:50 -0800603 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800604 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800605 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
606 }
607 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800608
Tao Bao039f2da2016-11-22 16:29:50 -0800609 size_t bytes;
610 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800611 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name,
612 bytes_str.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -0800613 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800614
Tianjie Xu99b73be2017-11-28 17:23:06 -0800615 // Skip the cache size check if the update is a retry.
Tao Bao5ee25662018-07-11 15:55:32 -0700616 if (state->is_retry || CheckAndFreeSpaceOnCache(bytes)) {
Tianjie Xu99b73be2017-11-28 17:23:06 -0800617 return StringValue("t");
618 }
619 return StringValue("");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800620}
621
Tianjie Xuc4447322017-03-06 14:44:59 -0800622Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
623 if (!argv.empty()) {
624 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
625 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800626 }
Tianjie Xu58d59122019-05-03 01:05:04 -0700627
Tianjie Xu1536db82019-05-14 10:54:43 -0700628 state->updater->WriteToCommandPipe("wipe_cache");
Tao Bao039f2da2016-11-22 16:29:50 -0800629 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700630}
631
Tianjie Xuc4447322017-03-06 14:44:59 -0800632Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
633 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800634 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
635 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700636
Tao Bao039f2da2016-11-22 16:29:50 -0800637 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800638 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800639 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
640 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700641
Tianjie Xu1536db82019-05-14 10:54:43 -0700642 auto updater_runtime = state->updater->GetRuntime();
643 auto status = updater_runtime->RunProgram(args, false);
Tao Bao039f2da2016-11-22 16:29:50 -0800644 return StringValue(std::to_string(status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700645}
646
Tao Bao511d7592018-06-19 15:56:49 -0700647// read_file(filename)
Tao Baobafd6c72018-07-09 15:08:50 -0700648// Reads a local file 'filename' and returns its contents as a string Value.
Tianjie Xuc4447322017-03-06 14:44:59 -0800649Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
650 if (argv.size() != 1) {
651 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800652 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700653
Tao Bao039f2da2016-11-22 16:29:50 -0800654 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800655 if (!ReadArgs(state, argv, &args)) {
Tao Bao511d7592018-06-19 15:56:49 -0700656 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tao Bao039f2da2016-11-22 16:29:50 -0800657 }
658 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -0800659
Tao Baobafd6c72018-07-09 15:08:50 -0700660 std::string contents;
Tianjie Xu1536db82019-05-14 10:54:43 -0700661 auto updater_runtime = state->updater->GetRuntime();
662 if (updater_runtime->ReadFileToString(filename, &contents)) {
Tao Baobafd6c72018-07-09 15:08:50 -0700663 return new Value(Value::Type::STRING, std::move(contents));
Tao Bao039f2da2016-11-22 16:29:50 -0800664 }
Tao Bao511d7592018-06-19 15:56:49 -0700665
666 // Leave it to caller to handle the failure.
Tao Baobafd6c72018-07-09 15:08:50 -0700667 PLOG(ERROR) << name << ": Failed to read " << filename;
Tao Bao511d7592018-06-19 15:56:49 -0700668 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -0800669}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700670
Tao Baod0f30882016-11-03 23:52:01 -0700671// write_value(value, filename)
672// Writes 'value' to 'filename'.
673// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
Tianjie Xuc4447322017-03-06 14:44:59 -0800674Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
675 if (argv.size() != 2) {
676 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
677 argv.size());
Tao Baod0f30882016-11-03 23:52:01 -0700678 }
679
680 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800681 if (!ReadArgs(state, argv, &args)) {
Tao Baod0f30882016-11-03 23:52:01 -0700682 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
683 }
684
685 const std::string& filename = args[1];
686 if (filename.empty()) {
687 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
688 }
689
690 const std::string& value = args[0];
Tianjie Xu1536db82019-05-14 10:54:43 -0700691 auto updater_runtime = state->updater->GetRuntime();
692 if (!updater_runtime->WriteStringToFile(value, filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800693 PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\"";
Tao Baod0f30882016-11-03 23:52:01 -0700694 return StringValue("");
Tao Baod0f30882016-11-03 23:52:01 -0700695 }
Tianjie Xu1536db82019-05-14 10:54:43 -0700696 return StringValue("t");
Tao Baod0f30882016-11-03 23:52:01 -0700697}
698
Doug Zongkerc87bab12013-11-25 13:53:25 -0800699// Immediately reboot the device. Recovery is not finished normally,
700// so if you reboot into recovery it will re-start applying the
701// current package (because nothing has cleared the copy of the
702// arguments stored in the BCB).
703//
704// The argument is the partition name passed to the android reboot
705// property. It can be "recovery" to boot from the recovery
706// partition, or "" (empty string) to boot from the regular boot
707// partition.
Tianjie Xuc4447322017-03-06 14:44:59 -0800708Value* RebootNowFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
709 if (argv.size() != 2) {
710 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
711 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800712 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800713
Tao Baobedf5fc2016-11-18 12:01:26 -0800714 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800715 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800716 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
717 }
718 const std::string& filename = args[0];
719 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800720
Tao Baobedf5fc2016-11-18 12:01:26 -0800721 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
722 bootloader_message boot;
723 std::string err;
724 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800725 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800726 return StringValue("");
727 }
728 memset(boot.command, 0, sizeof(boot.command));
729 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800730 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800731 return StringValue("");
732 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800733
Tao Bao782dcc12019-04-29 11:23:16 -0700734 Reboot(property);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800735
Tao Baobedf5fc2016-11-18 12:01:26 -0800736 sleep(5);
737 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800738}
739
740// Store a string value somewhere that future invocations of recovery
741// can access it. This value is called the "stage" and can be used to
742// drive packages that need to do reboots in the middle of
743// installation and keep track of where they are in the multi-stage
744// install.
745//
746// The first argument is the block device for the misc partition
747// ("/misc" in the fstab), which is where this value is stored. The
748// second argument is the string to store; it should not exceed 31
749// bytes.
Tianjie Xuc4447322017-03-06 14:44:59 -0800750Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
751 if (argv.size() != 2) {
752 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
753 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800754 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800755
Tao Baobedf5fc2016-11-18 12:01:26 -0800756 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800757 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800758 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
759 }
760 const std::string& filename = args[0];
761 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800762
Tao Baobedf5fc2016-11-18 12:01:26 -0800763 // Store this value in the misc partition, immediately after the
764 // bootloader message that the main recovery uses to save its
765 // arguments in case of the device restarting midway through
766 // package installation.
767 bootloader_message boot;
768 std::string err;
769 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800770 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800771 return StringValue("");
772 }
773 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
774 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800775 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800776 return StringValue("");
777 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800778
Tao Baobedf5fc2016-11-18 12:01:26 -0800779 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800780}
781
782// Return the value most recently saved with SetStageFn. The argument
783// is the block device for the misc partition.
Tianjie Xuc4447322017-03-06 14:44:59 -0800784Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
785 if (argv.size() != 1) {
786 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -0800787 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800788
Tao Baobedf5fc2016-11-18 12:01:26 -0800789 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800790 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -0800791 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
792 }
793 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -0800794
Tao Baobedf5fc2016-11-18 12:01:26 -0800795 bootloader_message boot;
796 std::string err;
797 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800798 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -0800799 return StringValue("");
800 }
Doug Zongkerc87bab12013-11-25 13:53:25 -0800801
Tao Baobedf5fc2016-11-18 12:01:26 -0800802 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800803}
804
Tianjie Xuc4447322017-03-06 14:44:59 -0800805Value* WipeBlockDeviceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
806 if (argv.size() != 2) {
807 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
808 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800809 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800810
Tao Bao358c2ec2016-11-28 11:48:43 -0800811 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800812 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800813 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
814 }
815 const std::string& filename = args[0];
816 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800817
Tao Bao358c2ec2016-11-28 11:48:43 -0800818 size_t len;
819 if (!android::base::ParseUint(len_str.c_str(), &len)) {
820 return nullptr;
821 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700822
Tianjie Xu1536db82019-05-14 10:54:43 -0700823 auto updater_runtime = state->updater->GetRuntime();
824 int status = updater_runtime->WipeBlockDevice(filename, len);
825 return StringValue(status == 0 ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800826}
827
Tianjie Xuc4447322017-03-06 14:44:59 -0800828Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
829 if (!argv.empty()) {
830 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
831 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800832 }
Tianjie Xu1536db82019-05-14 10:54:43 -0700833 state->updater->WriteToCommandPipe("enable_reboot");
Tao Bao039f2da2016-11-22 16:29:50 -0800834 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -0700835}
836
Tianjie Xuc4447322017-03-06 14:44:59 -0800837Value* Tune2FsFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
838 if (argv.empty()) {
839 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800840 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800841
Tao Bao039f2da2016-11-22 16:29:50 -0800842 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800843 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800844 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
845 }
Michael Rungeacf47db2014-11-21 00:12:28 -0800846
Tao Bao3d69f0d2018-12-20 09:44:06 -0800847 // tune2fs expects the program name as its first arg.
848 args.insert(args.begin(), "tune2fs");
Tianjie Xu1536db82019-05-14 10:54:43 -0700849 auto updater_runtime = state->updater->GetRuntime();
850 if (auto result = updater_runtime->Tune2Fs(args); result != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800851 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
852 }
853 return StringValue("t");
Michael Rungeacf47db2014-11-21 00:12:28 -0800854}
855
Doug Zongker9931f7f2009-06-10 14:11:53 -0700856void RegisterInstallFunctions() {
Tao Bao039f2da2016-11-22 16:29:50 -0800857 RegisterFunction("mount", MountFn);
858 RegisterFunction("is_mounted", IsMountedFn);
859 RegisterFunction("unmount", UnmountFn);
860 RegisterFunction("format", FormatFn);
861 RegisterFunction("show_progress", ShowProgressFn);
862 RegisterFunction("set_progress", SetProgressFn);
Tao Bao039f2da2016-11-22 16:29:50 -0800863 RegisterFunction("package_extract_file", PackageExtractFileFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700864
Tao Bao039f2da2016-11-22 16:29:50 -0800865 RegisterFunction("getprop", GetPropFn);
866 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700867
Tao Bao039f2da2016-11-22 16:29:50 -0800868 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Tao Bao5609bc82018-06-20 00:30:48 -0700869 RegisterFunction("patch_partition", PatchPartitionFn);
870 RegisterFunction("patch_partition_check", PatchPartitionCheckFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700871
Tao Bao039f2da2016-11-22 16:29:50 -0800872 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -0800873
Tao Bao039f2da2016-11-22 16:29:50 -0800874 RegisterFunction("read_file", ReadFileFn);
Tao Bao039f2da2016-11-22 16:29:50 -0800875 RegisterFunction("write_value", WriteValueFn);
Doug Zongker512536a2010-02-17 16:11:44 -0800876
Tao Bao039f2da2016-11-22 16:29:50 -0800877 RegisterFunction("wipe_cache", WipeCacheFn);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700878
Tao Bao039f2da2016-11-22 16:29:50 -0800879 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700880
Tao Bao039f2da2016-11-22 16:29:50 -0800881 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -0800882
Tao Bao039f2da2016-11-22 16:29:50 -0800883 RegisterFunction("reboot_now", RebootNowFn);
884 RegisterFunction("get_stage", GetStageFn);
885 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -0700886
Tao Bao039f2da2016-11-22 16:29:50 -0800887 RegisterFunction("enable_reboot", EnableRebootFn);
888 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700889}