blob: d45879454f4291ed655c6f749d55796d45623c12 [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
Kelvin Zhangd1ba38f2020-09-17 11:32:29 -040038#include <limits>
Yabin Cui64be2132016-02-03 18:16:02 -080039#include <memory>
Tao Bao1bf17722016-11-03 23:52:01 -070040#include <string>
Yabin Cui64be2132016-02-03 18:16:02 -080041#include <vector>
42
Tao Bao1bf17722016-11-03 23:52:01 -070043#include <android-base/file.h>
Tao Bao039f2da2016-11-22 16:29:50 -080044#include <android-base/logging.h>
Tianjie Xu5fe280a2016-10-17 18:15:20 -070045#include <android-base/parsedouble.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080046#include <android-base/parseint.h>
Elliott Hughescb220402016-09-23 15:30:55 -070047#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080048#include <android-base/stringprintf.h>
Tao Baod0f30882016-11-03 23:52:01 -070049#include <android-base/strings.h>
Tianjie Xu22f11202018-08-27 10:50:31 -070050#include <android-base/unique_fd.h>
Tao Bao0d3f84f2016-12-28 15:09:20 -080051#include <applypatch/applypatch.h>
52#include <bootloader_message/bootloader_message.h>
Tao Baode40ba52016-10-05 23:17:01 -070053#include <ext4_utils/wipe.h>
Tao Bao361342c2016-02-08 11:15:50 -080054#include <openssl/sha.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070055#include <selinux/label.h>
56#include <selinux/selinux.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070057#include <ziparchive/zip_archive.h>
Tao Bao1107d962015-09-09 17:16:55 -070058
Doug Zongker9931f7f2009-06-10 14:11:53 -070059#include "edify/expr.h"
Tianjie Xu1536db82019-05-14 10:54:43 -070060#include "edify/updater_interface.h"
61#include "edify/updater_runtime_interface.h"
Tao Bao17054c02018-05-03 22:41:23 -070062#include "otautil/dirutil.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070063#include "otautil/error_code.h"
Tao Bao09e468f2017-09-29 14:39:33 -070064#include "otautil/print_sha1.h"
Tao Bao2c526392018-05-03 23:01:13 -070065#include "otautil/sysutil.h"
Michael Runge75480252014-10-22 19:48:41 -070066
Tianjie Xu27556d02019-05-22 14:48:35 -070067#ifndef __ANDROID__
68#include <cutils/memory.h> // for strlcpy
69#endif
Simon Shields2e9747f2018-08-09 01:17:21 +100070
Tianjie Xu1536db82019-05-14 10:54:43 -070071static bool UpdateBlockDeviceNameForPartition(UpdaterInterface* updater, Partition* partition) {
72 CHECK(updater);
73 std::string name = updater->FindBlockDeviceName(partition->name);
74 if (name.empty()) {
75 LOG(ERROR) << "Failed to find the block device " << partition->name;
76 return false;
Simon Shields2e9747f2018-08-09 01:17:21 +100077 }
Tianjie Xu1536db82019-05-14 10:54:43 -070078
79 partition->name = std::move(name);
Simon Shields2e9747f2018-08-09 01:17:21 +100080 return true;
81}
82
Tianjie Xu5419ad32018-02-02 16:49:15 -080083// This is the updater side handler for ui_print() in edify script. Contents will be sent over to
84// the recovery side for on-screen display.
85Value* UIPrintFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
86 std::vector<std::string> args;
87 if (!ReadArgs(state, argv, &args)) {
88 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
89 }
90
91 std::string buffer = android::base::Join(args, "");
Tianjie Xu1536db82019-05-14 10:54:43 -070092 state->updater->UiPrint(buffer);
Tianjie Xu5419ad32018-02-02 16:49:15 -080093 return StringValue(buffer);
Doug Zongker8edb00c2009-06-11 17:21:44 -070094}
95
Tianjie Xu5419ad32018-02-02 16:49:15 -080096// package_extract_file(package_file[, dest_file])
97// Extracts a single package_file from the update package and writes it to dest_file,
98// overwriting existing files if necessary. Without the dest_file argument, returns the
99// contents of the package file as a binary blob.
100Value* PackageExtractFileFn(const char* name, State* state,
101 const std::vector<std::unique_ptr<Expr>>& argv) {
102 if (argv.size() < 1 || argv.size() > 2) {
103 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name,
104 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800105 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800106
107 if (argv.size() == 2) {
108 // The two-argument version extracts to a file.
109
110 std::vector<std::string> args;
111 if (!ReadArgs(state, argv, &args)) {
112 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
113 argv.size());
114 }
115 const std::string& zip_path = args[0];
Tianjie Xu60b242c2019-07-30 16:48:52 -0700116 std::string dest_path = args[1];
Tianjie Xu5419ad32018-02-02 16:49:15 -0800117
Tianjie Xu1536db82019-05-14 10:54:43 -0700118 ZipArchiveHandle za = state->updater->GetPackageHandle();
Kelvin Zhang4f811302020-09-16 14:06:12 -0400119 ZipEntry64 entry;
Elliott Hughesa86dddb2019-05-03 22:52:37 -0700120 if (FindEntry(za, zip_path, &entry) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800121 LOG(ERROR) << name << ": no " << zip_path << " in package";
122 return StringValue("");
123 }
124
Tianjie Xu60b242c2019-07-30 16:48:52 -0700125 // Update the destination of package_extract_file if it's a block device. During simulation the
126 // destination will map to a fake file.
127 if (std::string block_device_name = state->updater->FindBlockDeviceName(dest_path);
128 !block_device_name.empty()) {
129 dest_path = block_device_name;
130 }
131
Tianjie Xu22f11202018-08-27 10:50:31 -0700132 android::base::unique_fd fd(TEMP_FAILURE_RETRY(
133 open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tianjie Xu5419ad32018-02-02 16:49:15 -0800134 if (fd == -1) {
135 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
136 return StringValue("");
137 }
138
139 bool success = true;
140 int32_t ret = ExtractEntryToFile(za, &entry, fd);
141 if (ret != 0) {
142 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
143 << entry.uncompressed_length << " bytes) to \"" << dest_path
144 << "\": " << ErrorCodeString(ret);
145 success = false;
146 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700147 if (fsync(fd) == -1) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800148 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
149 success = false;
150 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700151
152 if (close(fd.release()) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800153 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
154 success = false;
155 }
156
157 return StringValue(success ? "t" : "");
158 } else {
159 // The one-argument version returns the contents of the file as the result.
160
161 std::vector<std::string> args;
162 if (!ReadArgs(state, argv, &args)) {
163 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
164 argv.size());
165 }
166 const std::string& zip_path = args[0];
167
Tianjie Xu1536db82019-05-14 10:54:43 -0700168 ZipArchiveHandle za = state->updater->GetPackageHandle();
Kelvin Zhang4f811302020-09-16 14:06:12 -0400169 ZipEntry64 entry;
Elliott Hughesa86dddb2019-05-03 22:52:37 -0700170 if (FindEntry(za, zip_path, &entry) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800171 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
172 zip_path.c_str());
173 }
174
175 std::string buffer;
Kelvin Zhangd1ba38f2020-09-17 11:32:29 -0400176 if (entry.uncompressed_length > std::numeric_limits<size_t>::max()) {
177 return ErrorAbort(state, kPackageExtractFileFailure,
178 "%s(): Entry `%s` Uncompressed size exceeds size of address space.", name,
179 zip_path.c_str());
180 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800181 buffer.resize(entry.uncompressed_length);
182
183 int32_t ret =
184 ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
185 if (ret != 0) {
186 return ErrorAbort(state, kPackageExtractFileFailure,
187 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
188 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
189 }
190
Tao Bao511d7592018-06-19 15:56:49 -0700191 return new Value(Value::Type::BLOB, buffer);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800192 }
193}
194
Tao Bao5609bc82018-06-20 00:30:48 -0700195// patch_partition_check(target_partition, source_partition)
196// Checks if the target and source partitions have the desired checksums to be patched. It returns
197// directly, if the target partition already has the expected checksum. Otherwise it in turn
198// checks the integrity of the source partition and the backup file on /cache.
Tianjie Xu5419ad32018-02-02 16:49:15 -0800199//
Tao Bao5609bc82018-06-20 00:30:48 -0700200// For example, patch_partition_check(
201// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
202// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4")
203Value* PatchPartitionCheckFn(const char* name, State* state,
204 const std::vector<std::unique_ptr<Expr>>& argv) {
205 if (argv.size() != 2) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800206 return ErrorAbort(state, kArgsParsingFailure,
Tao Bao5609bc82018-06-20 00:30:48 -0700207 "%s(): Invalid number of args (expected 2, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800208 }
209
210 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700211 if (!ReadArgs(state, argv, &args, 0, 2)) {
212 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800213 }
214
Tao Bao5609bc82018-06-20 00:30:48 -0700215 std::string err;
216 auto target = Partition::Parse(args[0], &err);
217 if (!target) {
218 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
219 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800220 }
221
Tao Bao5609bc82018-06-20 00:30:48 -0700222 auto source = Partition::Parse(args[1], &err);
223 if (!source) {
224 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
225 args[1].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800226 }
227
Tianjie Xu1536db82019-05-14 10:54:43 -0700228 if (!UpdateBlockDeviceNameForPartition(state->updater, &source) ||
229 !UpdateBlockDeviceNameForPartition(state->updater, &target)) {
230 return StringValue("");
231 }
232
Tao Bao5609bc82018-06-20 00:30:48 -0700233 bool result = PatchPartitionCheck(target, source);
234 return StringValue(result ? "t" : "");
Tianjie Xu5419ad32018-02-02 16:49:15 -0800235}
236
Tao Bao5609bc82018-06-20 00:30:48 -0700237// patch_partition(target, source, patch)
238// Applies the given patch to the source partition, and writes the result to the target partition.
239//
240// For example, patch_partition(
241// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
242// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4",
243// package_extract_file("boot.img.p"))
244Value* PatchPartitionFn(const char* name, State* state,
245 const std::vector<std::unique_ptr<Expr>>& argv) {
246 if (argv.size() != 3) {
247 return ErrorAbort(state, kArgsParsingFailure,
248 "%s(): Invalid number of args (expected 3, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800249 }
250
251 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700252 if (!ReadArgs(state, argv, &args, 0, 2)) {
253 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800254 }
255
Tao Bao5609bc82018-06-20 00:30:48 -0700256 std::string err;
257 auto target = Partition::Parse(args[0], &err);
258 if (!target) {
259 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
260 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800261 }
262
Tao Bao5609bc82018-06-20 00:30:48 -0700263 auto source = Partition::Parse(args[1], &err);
264 if (!source) {
265 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
266 args[1].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800267 }
268
Tao Bao5609bc82018-06-20 00:30:48 -0700269 std::vector<std::unique_ptr<Value>> values;
270 if (!ReadValueArgs(state, argv, &values, 2, 1) || values[0]->type != Value::Type::BLOB) {
271 return ErrorAbort(state, kArgsParsingFailure, "%s(): Invalid patch arg", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800272 }
273
Tianjie Xu1536db82019-05-14 10:54:43 -0700274 if (!UpdateBlockDeviceNameForPartition(state->updater, &source) ||
275 !UpdateBlockDeviceNameForPartition(state->updater, &target)) {
276 return StringValue("");
277 }
278
Tao Bao5234ad42019-09-23 10:28:54 -0700279 bool result = PatchPartition(target, source, *values[0], nullptr, true);
Tao Bao5609bc82018-06-20 00:30:48 -0700280 return StringValue(result ? "t" : "");
Doug Zongkera23075f2012-08-06 16:19:09 -0700281}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700282
Doug Zongker9931f7f2009-06-10 14:11:53 -0700283// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -0700284// mount(fs_type, partition_type, location, mount_point, mount_options)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700285
Doug Zongker9931f7f2009-06-10 14:11:53 -0700286// fs_type="ext4" partition_type="EMMC" location=device
Tianjie Xuc4447322017-03-06 14:44:59 -0800287Value* MountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
288 if (argv.size() != 4 && argv.size() != 5) {
289 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %zu", name,
290 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800291 }
292
293 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800294 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800295 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
296 }
297 const std::string& fs_type = args[0];
298 const std::string& partition_type = args[1];
299 const std::string& location = args[2];
300 const std::string& mount_point = args[3];
301 std::string mount_options;
302
Tianjie Xuc4447322017-03-06 14:44:59 -0800303 if (argv.size() == 5) {
Tao Bao039f2da2016-11-22 16:29:50 -0800304 mount_options = args[4];
305 }
306
307 if (fs_type.empty()) {
308 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
309 }
310 if (partition_type.empty()) {
311 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
312 name);
313 }
314 if (location.empty()) {
315 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
316 }
317 if (mount_point.empty()) {
318 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
319 name);
320 }
321
Tianjie Xu1536db82019-05-14 10:54:43 -0700322 auto updater = state->updater;
323 if (updater->GetRuntime()->Mount(location, mount_point, fs_type, mount_options) != 0) {
Tianjie Xu58d59122019-05-03 01:05:04 -0700324 updater->UiPrint(android::base::StringPrintf("%s: Failed to mount %s at %s: %s", name,
325 location.c_str(), mount_point.c_str(),
326 strerror(errno)));
Tao Bao039f2da2016-11-22 16:29:50 -0800327 return StringValue("");
328 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700329
Tao Bao039f2da2016-11-22 16:29:50 -0800330 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700331}
332
Doug Zongker9931f7f2009-06-10 14:11:53 -0700333// is_mounted(mount_point)
Tianjie Xuc4447322017-03-06 14:44:59 -0800334Value* IsMountedFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
335 if (argv.size() != 1) {
336 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800337 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700338
Tao Bao039f2da2016-11-22 16:29:50 -0800339 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800340 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800341 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
342 }
343 const std::string& mount_point = args[0];
344 if (mount_point.empty()) {
345 return ErrorAbort(state, kArgsParsingFailure,
346 "mount_point argument to unmount() can't be empty");
347 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700348
Tianjie Xu1536db82019-05-14 10:54:43 -0700349 auto updater_runtime = state->updater->GetRuntime();
350 if (!updater_runtime->IsMounted(mount_point)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800351 return StringValue("");
352 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700353
Tao Bao039f2da2016-11-22 16:29:50 -0800354 return StringValue(mount_point);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700355}
356
Tianjie Xuc4447322017-03-06 14:44:59 -0800357Value* UnmountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
358 if (argv.size() != 1) {
359 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800360 }
361 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800362 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800363 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
364 }
365 const std::string& mount_point = args[0];
366 if (mount_point.empty()) {
367 return ErrorAbort(state, kArgsParsingFailure,
368 "mount_point argument to unmount() can't be empty");
369 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700370
Tianjie Xu1536db82019-05-14 10:54:43 -0700371 auto updater = state->updater;
372 auto [mounted, result] = updater->GetRuntime()->Unmount(mount_point);
373 if (!mounted) {
Tianjie Xu58d59122019-05-03 01:05:04 -0700374 updater->UiPrint(
375 android::base::StringPrintf("Failed to unmount %s: No such volume", mount_point.c_str()));
Tao Bao039f2da2016-11-22 16:29:50 -0800376 return nullptr;
Tianjie Xu1536db82019-05-14 10:54:43 -0700377 } else if (result != 0) {
378 updater->UiPrint(android::base::StringPrintf("Failed to unmount %s: %s", mount_point.c_str(),
379 strerror(errno)));
Tao Bao039f2da2016-11-22 16:29:50 -0800380 }
Nick Kralevich6a821fe2014-10-23 20:36:42 -0700381
Tao Bao039f2da2016-11-22 16:29:50 -0800382 return StringValue(mount_point);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700383}
384
Doug Zongker9931f7f2009-06-10 14:11:53 -0700385// format(fs_type, partition_type, location, fs_size, mount_point)
386//
Tao Bao039f2da2016-11-22 16:29:50 -0800387// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
388// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700389// if fs_size == 0, then make fs uses the entire partition.
Doug Zongker9931f7f2009-06-10 14:11:53 -0700390// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700391// 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 -0800392Value* FormatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
393 if (argv.size() != 5) {
394 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %zu", name,
395 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800396 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700397
Tao Bao039f2da2016-11-22 16:29:50 -0800398 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800399 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800400 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
401 }
402 const std::string& fs_type = args[0];
403 const std::string& partition_type = args[1];
404 const std::string& location = args[2];
405 const std::string& fs_size = args[3];
406 const std::string& mount_point = args[4];
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700407
Tao Bao039f2da2016-11-22 16:29:50 -0800408 if (fs_type.empty()) {
409 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
410 }
411 if (partition_type.empty()) {
412 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
413 name);
414 }
415 if (location.empty()) {
416 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
417 }
418 if (mount_point.empty()) {
419 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
420 name);
421 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700422
Tao Bao039f2da2016-11-22 16:29:50 -0800423 int64_t size;
424 if (!android::base::ParseInt(fs_size, &size)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800425 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800426 fs_size.c_str());
427 }
428
Tianjie Xu1536db82019-05-14 10:54:43 -0700429 auto updater_runtime = state->updater->GetRuntime();
Tao Bao039f2da2016-11-22 16:29:50 -0800430 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 Qianac318082017-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 Qianac318082017-04-21 14:36:12 -0700436 }
437
Tianjie Xu1536db82019-05-14 10:54:43 -0700438 if (auto status = updater_runtime->RunProgram(mke2fs_args, true); status != 0) {
Jin Qian502fd1c2017-11-02 11:58:12 -0700439 LOG(ERROR) << name << ": mke2fs failed (" << status << ") on " << location;
440 return StringValue("");
Jin Qianac318082017-04-21 14:36:12 -0700441 }
442
Tianjie Xu1536db82019-05-14 10:54:43 -0700443 if (auto status = updater_runtime->RunProgram(
444 { "/system/bin/e2fsdroid", "-e", "-a", mount_point, location }, true);
Tao Bao3d69f0d2018-12-20 09:44:06 -0800445 status != 0) {
Jin Qianac318082017-04-21 14:36:12 -0700446 LOG(ERROR) << name << ": e2fsdroid failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800447 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700448 }
Tao Bao039f2da2016-11-22 16:29:50 -0800449 return StringValue(location);
Tao Bao3d69f0d2018-12-20 09:44:06 -0800450 }
451
452 if (fs_type == "f2fs") {
Tao Bao039f2da2016-11-22 16:29:50 -0800453 if (size < 0) {
454 LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size;
455 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700456 }
Tao Bao3d69f0d2018-12-20 09:44:06 -0800457 std::vector<std::string> f2fs_args = {
Tao Baoc674dfb2018-12-20 14:25:15 -0800458 "/system/bin/make_f2fs", "-g", "android", "-w", "512", location
Tao Bao3d69f0d2018-12-20 09:44:06 -0800459 };
460 if (size >= 512) {
461 f2fs_args.push_back(std::to_string(size / 512));
462 }
Tianjie Xu1536db82019-05-14 10:54:43 -0700463 if (auto status = updater_runtime->RunProgram(f2fs_args, true); status != 0) {
Tao Baoc674dfb2018-12-20 14:25:15 -0800464 LOG(ERROR) << name << ": make_f2fs failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800465 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700466 }
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800467
Tianjie Xu1536db82019-05-14 10:54:43 -0700468 if (auto status = updater_runtime->RunProgram(
469 { "/system/bin/sload_f2fs", "-t", mount_point, location }, true);
Tao Baoc674dfb2018-12-20 14:25:15 -0800470 status != 0) {
471 LOG(ERROR) << name << ": sload_f2fs failed (" << status << ") on " << location;
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800472 return StringValue("");
473 }
474
Tao Bao039f2da2016-11-22 16:29:50 -0800475 return StringValue(location);
Tao Bao039f2da2016-11-22 16:29:50 -0800476 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700477
Tao Bao3d69f0d2018-12-20 09:44:06 -0800478 LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \""
479 << partition_type << "\"";
Tao Bao039f2da2016-11-22 16:29:50 -0800480 return nullptr;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700481}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700482
Tom Marshallbf4f24f2017-08-23 18:14:00 +0000483// rename(src_name, dst_name)
484// Renames src_name to dst_name. It automatically creates the necessary directories for dst_name.
485// Example: rename("system/app/Hangouts/Hangouts.apk", "system/priv-app/Hangouts/Hangouts.apk")
486Value* RenameFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
487 if (argv.size() != 2) {
488 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
489 argv.size());
490 }
491
492 std::vector<std::string> args;
493 if (!ReadArgs(state, argv, &args)) {
494 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
495 }
496 const std::string& src_name = args[0];
497 const std::string& dst_name = args[1];
498
499 if (src_name.empty()) {
500 return ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", name);
501 }
502 if (dst_name.empty()) {
503 return ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", name);
504 }
505 if (!make_parents(dst_name)) {
506 return ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
507 dst_name.c_str(), strerror(errno));
508 } else if (access(dst_name.c_str(), F_OK) == 0 && access(src_name.c_str(), F_OK) != 0) {
509 // File was already moved
510 return StringValue(dst_name);
511 } else if (rename(src_name.c_str(), dst_name.c_str()) != 0) {
512 return ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
513 src_name.c_str(), dst_name.c_str(), strerror(errno));
514 }
515
516 return StringValue(dst_name);
517}
518
519// delete([filename, ...])
520// Deletes all the filenames listed. Returns the number of files successfully deleted.
521//
522// delete_recursive([dirname, ...])
523// Recursively deletes dirnames and all their contents. Returns the number of directories
524// successfully deleted.
525Value* DeleteFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
526 std::vector<std::string> paths;
527 if (!ReadArgs(state, argv, &paths)) {
528 return nullptr;
529 }
530
531 bool recursive = (strcmp(name, "delete_recursive") == 0);
532
533 int success = 0;
534 for (const auto& path : paths) {
535 if ((recursive ? dirUnlinkHierarchy(path.c_str()) : unlink(path.c_str())) == 0) {
536 ++success;
537 }
538 }
539
540 return StringValue(std::to_string(success));
541}
542
543
Tianjie Xuc4447322017-03-06 14:44:59 -0800544Value* ShowProgressFn(const char* name, State* state,
545 const std::vector<std::unique_ptr<Expr>>& argv) {
546 if (argv.size() != 2) {
547 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
548 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800549 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700550
Tao Bao039f2da2016-11-22 16:29:50 -0800551 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800552 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800553 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
554 }
555 const std::string& frac_str = args[0];
556 const std::string& sec_str = args[1];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700557
Tao Bao039f2da2016-11-22 16:29:50 -0800558 double frac;
559 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800560 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800561 frac_str.c_str());
562 }
563 int sec;
564 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800565 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800566 sec_str.c_str());
567 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700568
Tianjie Xu1536db82019-05-14 10:54:43 -0700569 state->updater->WriteToCommandPipe(android::base::StringPrintf("progress %f %d", frac, sec));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700570
Tao Bao039f2da2016-11-22 16:29:50 -0800571 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700572}
573
Tianjie Xu5419ad32018-02-02 16:49:15 -0800574Value* SetProgressFn(const char* name, State* state,
575 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800576 if (argv.size() != 1) {
577 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800578 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700579
Tao Bao039f2da2016-11-22 16:29:50 -0800580 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800581 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800582 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
583 }
584 const std::string& frac_str = args[0];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700585
Tao Bao039f2da2016-11-22 16:29:50 -0800586 double frac;
587 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800588 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800589 frac_str.c_str());
590 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700591
Tianjie Xu1536db82019-05-14 10:54:43 -0700592 state->updater->WriteToCommandPipe(android::base::StringPrintf("set_progress %f", frac));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700593
Tao Bao039f2da2016-11-22 16:29:50 -0800594 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700595}
596
Tom Marshall981118e2017-10-25 20:27:08 +0200597// package_extract_dir(package_dir, dest_dir)
598// Extracts all files from the package underneath package_dir and writes them to the
599// corresponding tree beneath dest_dir. Any existing files are overwritten.
600// Example: package_extract_dir("system", "/system")
601//
602// Note: package_dir needs to be a relative path; dest_dir needs to be an absolute path.
603Value* PackageExtractDirFn(const char* name, State* state,
604 const std::vector<std::unique_ptr<Expr>>&argv) {
605 if (argv.size() != 2) {
606 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
607 argv.size());
608 }
609
610 std::vector<std::string> args;
611 if (!ReadArgs(state, argv, &args)) {
612 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
613 }
614 const std::string& zip_path = args[0];
615 const std::string& dest_path = args[1];
616
617 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
618
619 // To create a consistent system image, never use the clock for timestamps.
620 constexpr struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
621
622 bool success = ExtractPackageRecursive(za, zip_path, dest_path, &timestamp, sehandle);
623
624 return StringValue(success ? "t" : "");
625}
626
Tao Baoef0eb3b2016-11-14 21:29:52 -0800627// package_extract_file(package_file[, dest_file])
628// Extracts a single package_file from the update package and writes it to dest_file,
629// overwriting existing files if necessary. Without the dest_file argument, returns the
630// contents of the package file as a binary blob.
Tianjie Xuc4447322017-03-06 14:44:59 -0800631Value* PackageExtractFileFn(const char* name, State* state,
632 const std::vector<std::unique_ptr<Expr>>& argv) {
633 if (argv.size() < 1 || argv.size() > 2) {
634 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name,
635 argv.size());
Tao Baoef0eb3b2016-11-14 21:29:52 -0800636 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700637
Tianjie Xuc4447322017-03-06 14:44:59 -0800638 if (argv.size() == 2) {
Tao Baoef0eb3b2016-11-14 21:29:52 -0800639 // The two-argument version extracts to a file.
640
641 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800642 if (!ReadArgs(state, argv, &args)) {
643 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
644 argv.size());
Doug Zongker8edb00c2009-06-11 17:21:44 -0700645 }
Tao Baoef0eb3b2016-11-14 21:29:52 -0800646 const std::string& zip_path = args[0];
647 const std::string& dest_path = args[1];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700648
Tao Baoef0eb3b2016-11-14 21:29:52 -0800649 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
650 ZipString zip_string_path(zip_path.c_str());
651 ZipEntry entry;
652 if (FindEntry(za, zip_string_path, &entry) != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800653 LOG(ERROR) << name << ": no " << zip_path << " in package";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800654 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700655 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700656
Tao Bao358c2ec2016-11-28 11:48:43 -0800657 unique_fd fd(TEMP_FAILURE_RETRY(
658 ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tao Baoef0eb3b2016-11-14 21:29:52 -0800659 if (fd == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800660 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800661 return StringValue("");
662 }
663
664 bool success = true;
665 int32_t ret = ExtractEntryToFile(za, &entry, fd);
666 if (ret != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800667 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
668 << entry.uncompressed_length << " bytes) to \"" << dest_path
669 << "\": " << ErrorCodeString(ret);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800670 success = false;
671 }
672 if (ota_fsync(fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800673 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800674 success = false;
675 }
676 if (ota_close(fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800677 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800678 success = false;
679 }
680
681 return StringValue(success ? "t" : "");
682 } else {
683 // The one-argument version returns the contents of the file as the result.
684
685 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800686 if (!ReadArgs(state, argv, &args)) {
687 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
688 argv.size());
Tao Baoef0eb3b2016-11-14 21:29:52 -0800689 }
690 const std::string& zip_path = args[0];
691
692 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
693 ZipString zip_string_path(zip_path.c_str());
694 ZipEntry entry;
695 if (FindEntry(za, zip_string_path, &entry) != 0) {
696 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
697 zip_path.c_str());
698 }
699
700 std::string buffer;
701 buffer.resize(entry.uncompressed_length);
702
703 int32_t ret = ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
704 if (ret != 0) {
705 return ErrorAbort(state, kPackageExtractFileFailure,
706 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
707 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
708 }
709
710 return new Value(VAL_BLOB, buffer);
711 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700712}
713
Tom Marshallbf4f24f2017-08-23 18:14:00 +0000714// symlink(target, [src1, src2, ...])
715// Creates all sources as symlinks to target. It unlinks any previously existing src1, src2, etc
716// before creating symlinks.
717Value* SymlinkFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
718 if (argv.size() == 0) {
719 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %zu", name, argv.size());
720 }
721 std::string target;
722 if (!Evaluate(state, argv[0], &target)) {
723 return nullptr;
724 }
725
726 std::vector<std::string> srcs;
727 if (!ReadArgs(state, argv, &srcs, 1, argv.size())) {
728 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
729 }
730
731 size_t bad = 0;
732 for (const auto& src : srcs) {
733 if (unlink(src.c_str()) == -1 && errno != ENOENT) {
734 PLOG(ERROR) << name << ": failed to remove " << src;
735 ++bad;
736 } else if (!make_parents(src)) {
737 LOG(ERROR) << name << ": failed to symlink " << src << " to " << target
738 << ": making parents failed";
739 ++bad;
740 } else if (symlink(target.c_str(), src.c_str()) == -1) {
741 PLOG(ERROR) << name << ": failed to symlink " << src << " to " << target;
742 ++bad;
743 }
744 }
745 if (bad != 0) {
746 return ErrorAbort(state, kSymlinkFailure, "%s: Failed to create %zu symlink(s)", name, bad);
747 }
748 return StringValue("t");
749}
750
751struct perm_parsed_args {
752 bool has_uid;
753 uid_t uid;
754 bool has_gid;
755 gid_t gid;
756 bool has_mode;
757 mode_t mode;
758 bool has_fmode;
759 mode_t fmode;
760 bool has_dmode;
761 mode_t dmode;
762 bool has_selabel;
763 const char* selabel;
764 bool has_capabilities;
765 uint64_t capabilities;
766};
767
768static struct perm_parsed_args ParsePermArgs(State * state,
769 const std::vector<std::string>& args) {
770 struct perm_parsed_args parsed;
771 int bad = 0;
772 static int max_warnings = 20;
773
774 memset(&parsed, 0, sizeof(parsed));
775
776 for (size_t i = 1; i < args.size(); i += 2) {
777 if (args[i] == "uid") {
778 int64_t uid;
779 if (sscanf(args[i + 1].c_str(), "%" SCNd64, &uid) == 1) {
780 parsed.uid = uid;
781 parsed.has_uid = true;
782 } else {
783 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1].c_str());
784 bad++;
785 }
786 continue;
787 }
788 if (args[i] == "gid") {
789 int64_t gid;
790 if (sscanf(args[i + 1].c_str(), "%" SCNd64, &gid) == 1) {
791 parsed.gid = gid;
792 parsed.has_gid = true;
793 } else {
794 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1].c_str());
795 bad++;
796 }
797 continue;
798 }
799 if (args[i] == "mode") {
800 int32_t mode;
801 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
802 parsed.mode = mode;
803 parsed.has_mode = true;
804 } else {
805 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1].c_str());
806 bad++;
807 }
808 continue;
809 }
810 if (args[i] == "dmode") {
811 int32_t mode;
812 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
813 parsed.dmode = mode;
814 parsed.has_dmode = true;
815 } else {
816 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1].c_str());
817 bad++;
818 }
819 continue;
820 }
821 if (args[i] == "fmode") {
822 int32_t mode;
823 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
824 parsed.fmode = mode;
825 parsed.has_fmode = true;
826 } else {
827 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1].c_str());
828 bad++;
829 }
830 continue;
831 }
832 if (args[i] == "capabilities") {
833 int64_t capabilities;
834 if (sscanf(args[i + 1].c_str(), "%" SCNi64, &capabilities) == 1) {
835 parsed.capabilities = capabilities;
836 parsed.has_capabilities = true;
837 } else {
838 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1].c_str());
839 bad++;
840 }
841 continue;
842 }
843 if (args[i] == "selabel") {
844 if (!args[i + 1].empty()) {
845 parsed.selabel = args[i + 1].c_str();
846 parsed.has_selabel = true;
847 } else {
848 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1].c_str());
849 bad++;
850 }
851 continue;
852 }
853 if (max_warnings != 0) {
854 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i].c_str());
855 max_warnings--;
856 if (max_warnings == 0) {
857 LOG(INFO) << "ParsedPermArgs: suppressing further warnings";
858 }
859 }
860 }
861 return parsed;
862}
863
864static int ApplyParsedPerms(State* state, const char* filename, const struct stat* statptr,
865 struct perm_parsed_args parsed) {
866 int bad = 0;
867
868 if (parsed.has_selabel) {
869 if (lsetfilecon(filename, parsed.selabel) != 0) {
870 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n", filename,
871 parsed.selabel, strerror(errno));
872 bad++;
873 }
874 }
875
876 /* ignore symlinks */
877 if (S_ISLNK(statptr->st_mode)) {
878 return bad;
879 }
880
881 if (parsed.has_uid) {
882 if (chown(filename, parsed.uid, -1) < 0) {
883 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n", filename, parsed.uid,
884 strerror(errno));
885 bad++;
886 }
887 }
888
889 if (parsed.has_gid) {
890 if (chown(filename, -1, parsed.gid) < 0) {
891 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n", filename, parsed.gid,
892 strerror(errno));
893 bad++;
894 }
895 }
896
897 if (parsed.has_mode) {
898 if (chmod(filename, parsed.mode) < 0) {
899 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.mode,
900 strerror(errno));
901 bad++;
902 }
903 }
904
905 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
906 if (chmod(filename, parsed.dmode) < 0) {
907 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.dmode,
908 strerror(errno));
909 bad++;
910 }
911 }
912
913 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
914 if (chmod(filename, parsed.fmode) < 0) {
915 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.fmode,
916 strerror(errno));
917 bad++;
918 }
919 }
920
921 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
922 if (parsed.capabilities == 0) {
923 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
924 // Report failure unless it's ENODATA (attribute not set)
925 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n", filename,
926 parsed.capabilities, strerror(errno));
927 bad++;
928 }
929 } else {
930 struct vfs_cap_data cap_data;
931 memset(&cap_data, 0, sizeof(cap_data));
932 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
933 cap_data.data[0].permitted = (uint32_t)(parsed.capabilities & 0xffffffff);
934 cap_data.data[0].inheritable = 0;
935 cap_data.data[1].permitted = (uint32_t)(parsed.capabilities >> 32);
936 cap_data.data[1].inheritable = 0;
937 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
938 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n", filename,
939 parsed.capabilities, strerror(errno));
940 bad++;
941 }
942 }
943 }
944
945 return bad;
946}
947
948// nftw doesn't allow us to pass along context, so we need to use
949// global variables. *sigh*
950static struct perm_parsed_args recursive_parsed_args;
951static State* recursive_state;
952
953static int do_SetMetadataRecursive(const char* filename, const struct stat* statptr, int fileflags,
954 struct FTW* pfwt) {
955 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
956}
957
958static Value* SetMetadataFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
959 if ((argv.size() % 2) != 1) {
960 return ErrorAbort(state, kArgsParsingFailure, "%s() expects an odd number of arguments, got %zu",
961 name, argv.size());
962 }
963
964 std::vector<std::string> args;
965 if (!ReadArgs(state, argv, &args)) {
966 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
967 }
968
969 struct stat sb;
970 if (lstat(args[0].c_str(), &sb) == -1) {
971 return ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s", name,
972 args[0].c_str(), strerror(errno));
973 }
974
975 struct perm_parsed_args parsed = ParsePermArgs(state, args);
976 int bad = 0;
977 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
978
979 if (recursive) {
980 recursive_parsed_args = parsed;
981 recursive_state = state;
982 bad += nftw(args[0].c_str(), do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
983 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
984 recursive_state = NULL;
985 } else {
986 bad += ApplyParsedPerms(state, args[0].c_str(), &sb, parsed);
987 }
988
989 if (bad > 0) {
990 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
991 }
992
993 return StringValue("");
994}
995
Tianjie Xuc4447322017-03-06 14:44:59 -0800996Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
997 if (argv.size() != 1) {
998 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800999 }
1000 std::string key;
1001 if (!Evaluate(state, argv[0], &key)) {
1002 return nullptr;
1003 }
Tianjie Xu1536db82019-05-14 10:54:43 -07001004
1005 auto updater_runtime = state->updater->GetRuntime();
1006 std::string value = updater_runtime->GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -07001007
Tao Bao039f2da2016-11-22 16:29:50 -08001008 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001009}
1010
Doug Zongker47cace92009-06-18 10:11:50 -07001011// file_getprop(file, key)
1012//
1013// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -07001014// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001015// and returns the value for 'key' (or "" if it isn't defined).
Tianjie Xu5419ad32018-02-02 16:49:15 -08001016Value* FileGetPropFn(const char* name, State* state,
1017 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -08001018 if (argv.size() != 2) {
1019 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1020 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -08001021 }
1022
1023 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001024 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -08001025 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1026 }
1027 const std::string& filename = args[0];
1028 const std::string& key = args[1];
1029
Tianjie Xu22f11202018-08-27 10:50:31 -07001030 std::string buffer;
Tianjie Xu1536db82019-05-14 10:54:43 -07001031 auto updater_runtime = state->updater->GetRuntime();
1032 if (!updater_runtime->ReadFileToString(filename, &buffer)) {
Tianjie Xu22f11202018-08-27 10:50:31 -07001033 ErrorAbort(state, kFreadFailure, "%s: failed to read %s", name, filename.c_str());
Tao Bao358c2ec2016-11-28 11:48:43 -08001034 return nullptr;
1035 }
1036
Tao Bao358c2ec2016-11-28 11:48:43 -08001037 std::vector<std::string> lines = android::base::Split(buffer, "\n");
1038 for (size_t i = 0; i < lines.size(); i++) {
1039 std::string line = android::base::Trim(lines[i]);
1040
1041 // comment or blank line: skip to next line
1042 if (line.empty() || line[0] == '#') {
1043 continue;
1044 }
1045 size_t equal_pos = line.find('=');
1046 if (equal_pos == std::string::npos) {
1047 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001048 }
1049
Tao Bao358c2ec2016-11-28 11:48:43 -08001050 // trim whitespace between key and '='
1051 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -07001052
Tao Bao358c2ec2016-11-28 11:48:43 -08001053 // not the key we're looking for
1054 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001055
Tao Bao358c2ec2016-11-28 11:48:43 -08001056 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
1057 }
Doug Zongker47cace92009-06-18 10:11:50 -07001058
Tao Bao358c2ec2016-11-28 11:48:43 -08001059 return StringValue("");
Doug Zongker8edb00c2009-06-11 17:21:44 -07001060}
1061
Doug Zongker8edb00c2009-06-11 17:21:44 -07001062// apply_patch_space(bytes)
Tianjie Xu5419ad32018-02-02 16:49:15 -08001063Value* ApplyPatchSpaceFn(const char* name, State* state,
1064 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -08001065 if (argv.size() != 1) {
1066 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name,
1067 argv.size());
1068 }
Tao Bao039f2da2016-11-22 16:29:50 -08001069 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001070 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001071 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1072 }
1073 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -08001074
Tao Bao039f2da2016-11-22 16:29:50 -08001075 size_t bytes;
1076 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -08001077 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name,
1078 bytes_str.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -08001079 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001080
Tianjie Xu99b73be2017-11-28 17:23:06 -08001081 // Skip the cache size check if the update is a retry.
Tao Bao5ee25662018-07-11 15:55:32 -07001082 if (state->is_retry || CheckAndFreeSpaceOnCache(bytes)) {
Tianjie Xu99b73be2017-11-28 17:23:06 -08001083 return StringValue("t");
Tao Bao039f2da2016-11-22 16:29:50 -08001084 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001085 return StringValue("");
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001086}
1087
Tianjie Xuc4447322017-03-06 14:44:59 -08001088Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1089 if (!argv.empty()) {
1090 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
1091 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001092 }
Tianjie Xu58d59122019-05-03 01:05:04 -07001093
Tianjie Xu1536db82019-05-14 10:54:43 -07001094 state->updater->WriteToCommandPipe("wipe_cache");
Tao Bao039f2da2016-11-22 16:29:50 -08001095 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -07001096}
1097
Tianjie Xuc4447322017-03-06 14:44:59 -08001098Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1099 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -08001100 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
1101 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001102
Tao Bao039f2da2016-11-22 16:29:50 -08001103 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001104 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001105 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1106 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001107
Tianjie Xu1536db82019-05-14 10:54:43 -07001108 auto updater_runtime = state->updater->GetRuntime();
1109 auto status = updater_runtime->RunProgram(args, false);
Tao Bao039f2da2016-11-22 16:29:50 -08001110 return StringValue(std::to_string(status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001111}
1112
Tao Bao511d7592018-06-19 15:56:49 -07001113// read_file(filename)
Tao Baobafd6c72018-07-09 15:08:50 -07001114// Reads a local file 'filename' and returns its contents as a string Value.
Tianjie Xuc4447322017-03-06 14:44:59 -08001115Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1116 if (argv.size() != 1) {
1117 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001118 }
Doug Zongker512536a2010-02-17 16:11:44 -08001119
Tao Bao039f2da2016-11-22 16:29:50 -08001120 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001121 if (!ReadArgs(state, argv, &args)) {
Tao Bao511d7592018-06-19 15:56:49 -07001122 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tao Bao039f2da2016-11-22 16:29:50 -08001123 }
1124 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -08001125
Tao Baobafd6c72018-07-09 15:08:50 -07001126 std::string contents;
Tianjie Xu1536db82019-05-14 10:54:43 -07001127 auto updater_runtime = state->updater->GetRuntime();
1128 if (updater_runtime->ReadFileToString(filename, &contents)) {
Tao Baobafd6c72018-07-09 15:08:50 -07001129 return new Value(Value::Type::STRING, std::move(contents));
Tao Bao039f2da2016-11-22 16:29:50 -08001130 }
Tao Bao511d7592018-06-19 15:56:49 -07001131
1132 // Leave it to caller to handle the failure.
Tao Baobafd6c72018-07-09 15:08:50 -07001133 PLOG(ERROR) << name << ": Failed to read " << filename;
Tao Bao511d7592018-06-19 15:56:49 -07001134 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -08001135}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001136
Tao Bao1bf17722016-11-03 23:52:01 -07001137// write_value(value, filename)
1138// Writes 'value' to 'filename'.
1139// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
Tianjie Xuc4447322017-03-06 14:44:59 -08001140Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1141 if (argv.size() != 2) {
1142 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1143 argv.size());
Tao Baod0f30882016-11-03 23:52:01 -07001144 }
Tao Bao1bf17722016-11-03 23:52:01 -07001145
Tao Baod0f30882016-11-03 23:52:01 -07001146 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001147 if (!ReadArgs(state, argv, &args)) {
Tao Baod0f30882016-11-03 23:52:01 -07001148 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1149 }
Tao Bao1bf17722016-11-03 23:52:01 -07001150
Tao Baod0f30882016-11-03 23:52:01 -07001151 const std::string& filename = args[1];
1152 if (filename.empty()) {
1153 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
1154 }
Tao Bao1bf17722016-11-03 23:52:01 -07001155
Tao Baod0f30882016-11-03 23:52:01 -07001156 const std::string& value = args[0];
Tianjie Xu1536db82019-05-14 10:54:43 -07001157 auto updater_runtime = state->updater->GetRuntime();
1158 if (!updater_runtime->WriteStringToFile(value, filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001159 PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\"";
Tao Baod0f30882016-11-03 23:52:01 -07001160 return StringValue("");
Tao Baod0f30882016-11-03 23:52:01 -07001161 }
Tianjie Xu1536db82019-05-14 10:54:43 -07001162 return StringValue("t");
Tao Bao1bf17722016-11-03 23:52:01 -07001163}
1164
Doug Zongkerc87bab12013-11-25 13:53:25 -08001165// Immediately reboot the device. Recovery is not finished normally,
1166// so if you reboot into recovery it will re-start applying the
1167// current package (because nothing has cleared the copy of the
1168// arguments stored in the BCB).
1169//
1170// The argument is the partition name passed to the android reboot
1171// property. It can be "recovery" to boot from the recovery
1172// partition, or "" (empty string) to boot from the regular boot
1173// partition.
Tianjie Xuc4447322017-03-06 14:44:59 -08001174Value* RebootNowFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1175 if (argv.size() != 2) {
1176 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1177 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001178 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001179
Tao Baobedf5fc2016-11-18 12:01:26 -08001180 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001181 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001182 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1183 }
1184 const std::string& filename = args[0];
1185 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001186
Tao Baobedf5fc2016-11-18 12:01:26 -08001187 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
1188 bootloader_message boot;
1189 std::string err;
1190 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001191 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001192 return StringValue("");
1193 }
1194 memset(boot.command, 0, sizeof(boot.command));
1195 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001196 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001197 return StringValue("");
1198 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001199
Tao Bao782dcc12019-04-29 11:23:16 -07001200 Reboot(property);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001201
Tao Baobedf5fc2016-11-18 12:01:26 -08001202 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001203}
1204
1205// Store a string value somewhere that future invocations of recovery
1206// can access it. This value is called the "stage" and can be used to
1207// drive packages that need to do reboots in the middle of
1208// installation and keep track of where they are in the multi-stage
1209// install.
1210//
1211// The first argument is the block device for the misc partition
1212// ("/misc" in the fstab), which is where this value is stored. The
1213// second argument is the string to store; it should not exceed 31
1214// bytes.
Tianjie Xuc4447322017-03-06 14:44:59 -08001215Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1216 if (argv.size() != 2) {
1217 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1218 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001219 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001220
Tao Baobedf5fc2016-11-18 12:01:26 -08001221 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001222 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001223 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1224 }
1225 const std::string& filename = args[0];
1226 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001227
Tao Baobedf5fc2016-11-18 12:01:26 -08001228 // Store this value in the misc partition, immediately after the
1229 // bootloader message that the main recovery uses to save its
1230 // arguments in case of the device restarting midway through
1231 // package installation.
1232 bootloader_message boot;
1233 std::string err;
1234 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001235 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001236 return StringValue("");
1237 }
1238 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
1239 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001240 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001241 return StringValue("");
1242 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001243
Tao Baobedf5fc2016-11-18 12:01:26 -08001244 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001245}
1246
1247// Return the value most recently saved with SetStageFn. The argument
1248// is the block device for the misc partition.
Tianjie Xuc4447322017-03-06 14:44:59 -08001249Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1250 if (argv.size() != 1) {
1251 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001252 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001253
Tao Baobedf5fc2016-11-18 12:01:26 -08001254 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001255 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001256 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1257 }
1258 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001259
Tao Baobedf5fc2016-11-18 12:01:26 -08001260 bootloader_message boot;
1261 std::string err;
1262 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001263 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001264 return StringValue("");
1265 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001266
Tao Baobedf5fc2016-11-18 12:01:26 -08001267 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001268}
1269
Tianjie Xuc4447322017-03-06 14:44:59 -08001270Value* WipeBlockDeviceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1271 if (argv.size() != 2) {
1272 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1273 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -08001274 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001275
Tao Bao358c2ec2016-11-28 11:48:43 -08001276 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001277 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -08001278 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1279 }
1280 const std::string& filename = args[0];
1281 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001282
Tao Bao358c2ec2016-11-28 11:48:43 -08001283 size_t len;
1284 if (!android::base::ParseUint(len_str.c_str(), &len)) {
1285 return nullptr;
1286 }
Tianjie Xu22f11202018-08-27 10:50:31 -07001287
Tianjie Xu1536db82019-05-14 10:54:43 -07001288 auto updater_runtime = state->updater->GetRuntime();
1289 int status = updater_runtime->WipeBlockDevice(filename, len);
1290 return StringValue(status == 0 ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001291}
1292
Tianjie Xuc4447322017-03-06 14:44:59 -08001293Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1294 if (!argv.empty()) {
1295 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
1296 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001297 }
Tianjie Xu1536db82019-05-14 10:54:43 -07001298 state->updater->WriteToCommandPipe("enable_reboot");
Tao Bao039f2da2016-11-22 16:29:50 -08001299 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -07001300}
1301
Tianjie Xuc4447322017-03-06 14:44:59 -08001302Value* Tune2FsFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001303#ifdef HAVE_LIBTUNE2FS
Tianjie Xuc4447322017-03-06 14:44:59 -08001304 if (argv.empty()) {
1305 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001306 }
Michael Rungeb278c252014-11-21 00:12:28 -08001307
Tao Bao039f2da2016-11-22 16:29:50 -08001308 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001309 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001310 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
1311 }
Michael Rungeb278c252014-11-21 00:12:28 -08001312
Tao Bao3d69f0d2018-12-20 09:44:06 -08001313 // tune2fs expects the program name as its first arg.
1314 args.insert(args.begin(), "tune2fs");
Tianjie Xu1536db82019-05-14 10:54:43 -07001315 auto updater_runtime = state->updater->GetRuntime();
1316 if (auto result = updater_runtime->Tune2Fs(args); result != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -08001317 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
1318 }
1319 return StringValue("t");
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001320#else
Ethan Yonker8373cfe2017-09-08 06:50:54 -05001321 return ErrorAbort(state, kTune2FsFailure, "%s() support not present, no libtune2fs", name);
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001322#endif // HAVE_LIBTUNE2FS
Michael Rungeb278c252014-11-21 00:12:28 -08001323}
1324
Yifan Hongdff80042020-04-28 13:31:11 -07001325Value* AddSlotSuffixFn(const char* name, State* state,
1326 const std::vector<std::unique_ptr<Expr>>& argv) {
1327 if (argv.size() != 1) {
1328 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
1329 }
1330 std::vector<std::string> args;
1331 if (!ReadArgs(state, argv, &args)) {
1332 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1333 }
1334 const std::string& arg = args[0];
1335 auto updater_runtime = state->updater->GetRuntime();
1336 return StringValue(updater_runtime->AddSlotSuffix(arg));
1337}
1338
Doug Zongker9931f7f2009-06-10 14:11:53 -07001339void RegisterInstallFunctions() {
Tao Bao039f2da2016-11-22 16:29:50 -08001340 RegisterFunction("mount", MountFn);
1341 RegisterFunction("is_mounted", IsMountedFn);
1342 RegisterFunction("unmount", UnmountFn);
1343 RegisterFunction("format", FormatFn);
1344 RegisterFunction("show_progress", ShowProgressFn);
1345 RegisterFunction("set_progress", SetProgressFn);
Tom Marshallbf4f24f2017-08-23 18:14:00 +00001346 RegisterFunction("delete", DeleteFn);
1347 RegisterFunction("delete_recursive", DeleteFn);
Tom Marshall981118e2017-10-25 20:27:08 +02001348 RegisterFunction("package_extract_dir", PackageExtractDirFn);
Tao Bao039f2da2016-11-22 16:29:50 -08001349 RegisterFunction("package_extract_file", PackageExtractFileFn);
Tom Marshallbf4f24f2017-08-23 18:14:00 +00001350 RegisterFunction("symlink", SymlinkFn);
1351
1352 // Usage:
1353 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1354 // Example:
1355 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel",
1356 // "u:object_r:system_file:s0", "capabilities", 0x0);
1357 RegisterFunction("set_metadata", SetMetadataFn);
1358
1359 // Usage:
1360 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1361 // Example:
1362 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755,
1363 // "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1364 RegisterFunction("set_metadata_recursive", SetMetadataFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001365
Tao Bao039f2da2016-11-22 16:29:50 -08001366 RegisterFunction("getprop", GetPropFn);
1367 RegisterFunction("file_getprop", FileGetPropFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001368
Tao Bao039f2da2016-11-22 16:29:50 -08001369 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Tao Bao5609bc82018-06-20 00:30:48 -07001370 RegisterFunction("patch_partition", PatchPartitionFn);
1371 RegisterFunction("patch_partition_check", PatchPartitionCheckFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001372
Tao Bao039f2da2016-11-22 16:29:50 -08001373 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001374
Tao Bao039f2da2016-11-22 16:29:50 -08001375 RegisterFunction("read_file", ReadFileFn);
Tao Bao039f2da2016-11-22 16:29:50 -08001376 RegisterFunction("write_value", WriteValueFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001377
Tao Bao039f2da2016-11-22 16:29:50 -08001378 RegisterFunction("wipe_cache", WipeCacheFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001379
Tao Bao039f2da2016-11-22 16:29:50 -08001380 RegisterFunction("ui_print", UIPrintFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001381
Tao Bao039f2da2016-11-22 16:29:50 -08001382 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001383
Tao Bao039f2da2016-11-22 16:29:50 -08001384 RegisterFunction("reboot_now", RebootNowFn);
1385 RegisterFunction("get_stage", GetStageFn);
1386 RegisterFunction("set_stage", SetStageFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001387
Tao Bao039f2da2016-11-22 16:29:50 -08001388 RegisterFunction("enable_reboot", EnableRebootFn);
1389 RegisterFunction("tune2fs", Tune2FsFn);
Yifan Hongdff80042020-04-28 13:31:11 -07001390
1391 RegisterFunction("add_slot_suffix", AddSlotSuffixFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001392}