blob: 70a1235babbbf7f1d307a02ee9f2cbde6ad2d391 [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
Michael Bestas15a104f2019-09-19 21:42:12 +030038#include <linux/xattr.h>
39
Kelvin Zhangd1ba38f2020-09-17 11:32:29 -040040#include <limits>
Yabin Cui64be2132016-02-03 18:16:02 -080041#include <memory>
Tao Bao361342c2016-02-08 11:15:50 -080042#include <string>
Yabin Cui64be2132016-02-03 18:16:02 -080043#include <vector>
44
Tao Baod0f30882016-11-03 23:52:01 -070045#include <android-base/file.h>
Tao Bao039f2da2016-11-22 16:29:50 -080046#include <android-base/logging.h>
Tianjie Xu5fe280a2016-10-17 18:15:20 -070047#include <android-base/parsedouble.h>
Tao Baod0f30882016-11-03 23:52:01 -070048#include <android-base/parseint.h>
Elliott Hughescb220402016-09-23 15:30:55 -070049#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080050#include <android-base/stringprintf.h>
Tao Baod0f30882016-11-03 23:52:01 -070051#include <android-base/strings.h>
Tianjie Xu22f11202018-08-27 10:50:31 -070052#include <android-base/unique_fd.h>
Tao Bao0d3f84f2016-12-28 15:09:20 -080053#include <applypatch/applypatch.h>
54#include <bootloader_message/bootloader_message.h>
Tao Baode40ba52016-10-05 23:17:01 -070055#include <ext4_utils/wipe.h>
Tao Bao361342c2016-02-08 11:15:50 -080056#include <openssl/sha.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070057#include <selinux/label.h>
58#include <selinux/selinux.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070059#include <ziparchive/zip_archive.h>
Tao Bao1107d962015-09-09 17:16:55 -070060
Doug Zongker9931f7f2009-06-10 14:11:53 -070061#include "edify/expr.h"
Tianjie Xu1536db82019-05-14 10:54:43 -070062#include "edify/updater_interface.h"
63#include "edify/updater_runtime_interface.h"
Tao Bao17054c02018-05-03 22:41:23 -070064#include "otautil/dirutil.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070065#include "otautil/error_code.h"
Tao Bao09e468f2017-09-29 14:39:33 -070066#include "otautil/print_sha1.h"
Tao Bao2c526392018-05-03 23:01:13 -070067#include "otautil/sysutil.h"
Michael Bestas15a104f2019-09-19 21:42:12 +030068#include "otautil/ziputil.h"
Tianjie Xu1536db82019-05-14 10:54:43 -070069
Tianjie Xu27556d02019-05-22 14:48:35 -070070#ifndef __ANDROID__
71#include <cutils/memory.h> // for strlcpy
72#endif
73
Tianjie Xu1536db82019-05-14 10:54:43 -070074static bool UpdateBlockDeviceNameForPartition(UpdaterInterface* updater, Partition* partition) {
75 CHECK(updater);
76 std::string name = updater->FindBlockDeviceName(partition->name);
77 if (name.empty()) {
78 LOG(ERROR) << "Failed to find the block device " << partition->name;
79 return false;
80 }
81
82 partition->name = std::move(name);
83 return true;
84}
Doug Zongker8edb00c2009-06-11 17:21:44 -070085
Michael Bestas15a104f2019-09-19 21:42:12 +030086static bool is_dir(const std::string& dirpath) {
87 struct stat st;
88 return stat(dirpath.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
89}
90
91// Create all parent directories of name, if necessary.
92static bool make_parents(const std::string& name) {
93 size_t prev_end = 0;
94 while (prev_end < name.size()) {
95 size_t next_end = name.find('/', prev_end + 1);
96 if (next_end == std::string::npos) {
97 break;
98 }
99 std::string dir_path = name.substr(0, next_end);
100 if (!is_dir(dir_path)) {
101 int result = mkdir(dir_path.c_str(), 0700);
102 if (result != 0) {
103 PLOG(ERROR) << "failed to mkdir " << dir_path << " when make parents for " << name;
104 return false;
105 }
106
107 LOG(INFO) << "created [" << dir_path << "]";
108 }
109 prev_end = next_end;
110 }
111 return true;
112}
113
Tianjie Xu5419ad32018-02-02 16:49:15 -0800114// This is the updater side handler for ui_print() in edify script. Contents will be sent over to
115// the recovery side for on-screen display.
116Value* UIPrintFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
117 std::vector<std::string> args;
118 if (!ReadArgs(state, argv, &args)) {
119 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
120 }
121
122 std::string buffer = android::base::Join(args, "");
Tianjie Xu1536db82019-05-14 10:54:43 -0700123 state->updater->UiPrint(buffer);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800124 return StringValue(buffer);
125}
126
Michael Bestas15a104f2019-09-19 21:42:12 +0300127// package_extract_dir(package_dir, dest_dir)
128// Extracts all files from the package underneath package_dir and writes them to the
129// corresponding tree beneath dest_dir. Any existing files are overwritten.
130// Example: package_extract_dir("system", "/system")
131//
132// Note: package_dir needs to be a relative path; dest_dir needs to be an absolute path.
133Value* PackageExtractDirFn(const char* name, State* state,
134 const std::vector<std::unique_ptr<Expr>>& argv) {
135 if (argv.size() != 2) {
136 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
137 argv.size());
138 }
139
140 std::vector<std::string> args;
141 if (!ReadArgs(state, argv, &args)) {
142 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
143 }
144 const std::string& zip_path = args[0];
145 const std::string& dest_path = args[1];
146
147 auto updater = state->updater;
148
149 ZipArchiveHandle za = updater->GetPackageHandle();
150
151 // To create a consistent system image, never use the clock for timestamps.
152 constexpr struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
153
154 bool success = ExtractPackageRecursive(za, zip_path, dest_path, &timestamp,
155 updater->GetRuntime()->sehandle());
156
157 return StringValue(success ? "t" : "");
158}
159
Tianjie Xu5419ad32018-02-02 16:49:15 -0800160// package_extract_file(package_file[, dest_file])
161// Extracts a single package_file from the update package and writes it to dest_file,
162// overwriting existing files if necessary. Without the dest_file argument, returns the
163// contents of the package file as a binary blob.
164Value* PackageExtractFileFn(const char* name, State* state,
165 const std::vector<std::unique_ptr<Expr>>& argv) {
166 if (argv.size() < 1 || argv.size() > 2) {
167 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name,
168 argv.size());
169 }
170
171 if (argv.size() == 2) {
172 // The two-argument version extracts to a file.
173
174 std::vector<std::string> args;
175 if (!ReadArgs(state, argv, &args)) {
176 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
177 argv.size());
178 }
179 const std::string& zip_path = args[0];
Tianjie Xu60b242c2019-07-30 16:48:52 -0700180 std::string dest_path = args[1];
Tianjie Xu5419ad32018-02-02 16:49:15 -0800181
Tianjie Xu1536db82019-05-14 10:54:43 -0700182 ZipArchiveHandle za = state->updater->GetPackageHandle();
Kelvin Zhang4f811302020-09-16 14:06:12 -0400183 ZipEntry64 entry;
Elliott Hughesa86dddb2019-05-03 22:52:37 -0700184 if (FindEntry(za, zip_path, &entry) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800185 LOG(ERROR) << name << ": no " << zip_path << " in package";
186 return StringValue("");
187 }
188
Tianjie Xu60b242c2019-07-30 16:48:52 -0700189 // Update the destination of package_extract_file if it's a block device. During simulation the
190 // destination will map to a fake file.
191 if (std::string block_device_name = state->updater->FindBlockDeviceName(dest_path);
192 !block_device_name.empty()) {
193 dest_path = block_device_name;
194 }
195
Tianjie Xu22f11202018-08-27 10:50:31 -0700196 android::base::unique_fd fd(TEMP_FAILURE_RETRY(
197 open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tianjie Xu5419ad32018-02-02 16:49:15 -0800198 if (fd == -1) {
199 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
200 return StringValue("");
201 }
202
203 bool success = true;
204 int32_t ret = ExtractEntryToFile(za, &entry, fd);
205 if (ret != 0) {
206 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
207 << entry.uncompressed_length << " bytes) to \"" << dest_path
208 << "\": " << ErrorCodeString(ret);
209 success = false;
210 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700211 if (fsync(fd) == -1) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800212 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
213 success = false;
214 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700215
216 if (close(fd.release()) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800217 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
218 success = false;
219 }
220
221 return StringValue(success ? "t" : "");
222 } else {
223 // The one-argument version returns the contents of the file as the result.
224
225 std::vector<std::string> args;
226 if (!ReadArgs(state, argv, &args)) {
227 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
228 argv.size());
229 }
230 const std::string& zip_path = args[0];
231
Tianjie Xu1536db82019-05-14 10:54:43 -0700232 ZipArchiveHandle za = state->updater->GetPackageHandle();
Kelvin Zhang4f811302020-09-16 14:06:12 -0400233 ZipEntry64 entry;
Elliott Hughesa86dddb2019-05-03 22:52:37 -0700234 if (FindEntry(za, zip_path, &entry) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800235 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
236 zip_path.c_str());
237 }
238
239 std::string buffer;
Kelvin Zhangd1ba38f2020-09-17 11:32:29 -0400240 if (entry.uncompressed_length > std::numeric_limits<size_t>::max()) {
241 return ErrorAbort(state, kPackageExtractFileFailure,
242 "%s(): Entry `%s` Uncompressed size exceeds size of address space.", name,
243 zip_path.c_str());
244 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800245 buffer.resize(entry.uncompressed_length);
246
247 int32_t ret =
248 ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
249 if (ret != 0) {
250 return ErrorAbort(state, kPackageExtractFileFailure,
251 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
252 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
253 }
254
Tao Bao511d7592018-06-19 15:56:49 -0700255 return new Value(Value::Type::BLOB, buffer);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800256 }
257}
258
Tao Bao5609bc82018-06-20 00:30:48 -0700259// patch_partition_check(target_partition, source_partition)
260// Checks if the target and source partitions have the desired checksums to be patched. It returns
261// directly, if the target partition already has the expected checksum. Otherwise it in turn
262// checks the integrity of the source partition and the backup file on /cache.
Tianjie Xu5419ad32018-02-02 16:49:15 -0800263//
Tao Bao5609bc82018-06-20 00:30:48 -0700264// For example, patch_partition_check(
265// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
266// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4")
267Value* PatchPartitionCheckFn(const char* name, State* state,
268 const std::vector<std::unique_ptr<Expr>>& argv) {
269 if (argv.size() != 2) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800270 return ErrorAbort(state, kArgsParsingFailure,
Tao Bao5609bc82018-06-20 00:30:48 -0700271 "%s(): Invalid number of args (expected 2, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800272 }
273
274 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700275 if (!ReadArgs(state, argv, &args, 0, 2)) {
276 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800277 }
278
Tao Bao5609bc82018-06-20 00:30:48 -0700279 std::string err;
280 auto target = Partition::Parse(args[0], &err);
281 if (!target) {
282 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
283 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800284 }
285
Tao Bao5609bc82018-06-20 00:30:48 -0700286 auto source = Partition::Parse(args[1], &err);
287 if (!source) {
288 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
289 args[1].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800290 }
291
Tianjie Xu1536db82019-05-14 10:54:43 -0700292 if (!UpdateBlockDeviceNameForPartition(state->updater, &source) ||
293 !UpdateBlockDeviceNameForPartition(state->updater, &target)) {
294 return StringValue("");
295 }
296
Tao Bao5609bc82018-06-20 00:30:48 -0700297 bool result = PatchPartitionCheck(target, source);
298 return StringValue(result ? "t" : "");
Tianjie Xu5419ad32018-02-02 16:49:15 -0800299}
300
Tao Bao5609bc82018-06-20 00:30:48 -0700301// patch_partition(target, source, patch)
302// Applies the given patch to the source partition, and writes the result to the target partition.
303//
304// For example, patch_partition(
305// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
306// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4",
307// package_extract_file("boot.img.p"))
308Value* PatchPartitionFn(const char* name, State* state,
309 const std::vector<std::unique_ptr<Expr>>& argv) {
310 if (argv.size() != 3) {
311 return ErrorAbort(state, kArgsParsingFailure,
312 "%s(): Invalid number of args (expected 3, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800313 }
314
315 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700316 if (!ReadArgs(state, argv, &args, 0, 2)) {
317 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800318 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800319
Tao Bao5609bc82018-06-20 00:30:48 -0700320 std::string err;
321 auto target = Partition::Parse(args[0], &err);
322 if (!target) {
323 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
324 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800325 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800326
Tao Bao5609bc82018-06-20 00:30:48 -0700327 auto source = Partition::Parse(args[1], &err);
328 if (!source) {
329 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
330 args[1].c_str(), err.c_str());
331 }
332
333 std::vector<std::unique_ptr<Value>> values;
334 if (!ReadValueArgs(state, argv, &values, 2, 1) || values[0]->type != Value::Type::BLOB) {
335 return ErrorAbort(state, kArgsParsingFailure, "%s(): Invalid patch arg", name);
336 }
337
Tianjie Xu1536db82019-05-14 10:54:43 -0700338 if (!UpdateBlockDeviceNameForPartition(state->updater, &source) ||
339 !UpdateBlockDeviceNameForPartition(state->updater, &target)) {
340 return StringValue("");
341 }
342
Tao Bao5234ad42019-09-23 10:28:54 -0700343 bool result = PatchPartition(target, source, *values[0], nullptr, true);
Tao Bao5609bc82018-06-20 00:30:48 -0700344 return StringValue(result ? "t" : "");
Tianjie Xu5419ad32018-02-02 16:49:15 -0800345}
346
Doug Zongker3d177d02010-07-01 09:18:44 -0700347// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -0700348// mount(fs_type, partition_type, location, mount_point, mount_options)
Tianjie Xuc4447322017-03-06 14:44:59 -0800349
Doug Zongker3d177d02010-07-01 09:18:44 -0700350// fs_type="ext4" partition_type="EMMC" location=device
Tianjie Xuc4447322017-03-06 14:44:59 -0800351Value* MountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
352 if (argv.size() != 4 && argv.size() != 5) {
353 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %zu", name,
354 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800355 }
356
357 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800358 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800359 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
360 }
361 const std::string& fs_type = args[0];
362 const std::string& partition_type = args[1];
363 const std::string& location = args[2];
364 const std::string& mount_point = args[3];
365 std::string mount_options;
366
Tianjie Xuc4447322017-03-06 14:44:59 -0800367 if (argv.size() == 5) {
Tao Bao039f2da2016-11-22 16:29:50 -0800368 mount_options = args[4];
369 }
370
371 if (fs_type.empty()) {
372 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
373 }
374 if (partition_type.empty()) {
375 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
376 name);
377 }
378 if (location.empty()) {
379 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
380 }
381 if (mount_point.empty()) {
382 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
383 name);
384 }
385
Tianjie Xu1536db82019-05-14 10:54:43 -0700386 auto updater = state->updater;
387 if (updater->GetRuntime()->Mount(location, mount_point, fs_type, mount_options) != 0) {
Tianjie Xu58d59122019-05-03 01:05:04 -0700388 updater->UiPrint(android::base::StringPrintf("%s: Failed to mount %s at %s: %s", name,
389 location.c_str(), mount_point.c_str(),
390 strerror(errno)));
Tao Bao039f2da2016-11-22 16:29:50 -0800391 return StringValue("");
392 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700393
Tao Bao039f2da2016-11-22 16:29:50 -0800394 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700395}
396
Doug Zongker8edb00c2009-06-11 17:21:44 -0700397// is_mounted(mount_point)
Tianjie Xuc4447322017-03-06 14:44:59 -0800398Value* IsMountedFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
399 if (argv.size() != 1) {
400 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800401 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700402
Tao Bao039f2da2016-11-22 16:29:50 -0800403 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800404 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800405 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
406 }
407 const std::string& mount_point = args[0];
408 if (mount_point.empty()) {
409 return ErrorAbort(state, kArgsParsingFailure,
410 "mount_point argument to unmount() can't be empty");
411 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700412
Tianjie Xu1536db82019-05-14 10:54:43 -0700413 auto updater_runtime = state->updater->GetRuntime();
414 if (!updater_runtime->IsMounted(mount_point)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800415 return StringValue("");
416 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700417
Tao Bao039f2da2016-11-22 16:29:50 -0800418 return StringValue(mount_point);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700419}
420
Tianjie Xuc4447322017-03-06 14:44:59 -0800421Value* UnmountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
422 if (argv.size() != 1) {
423 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800424 }
425 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800426 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800427 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
428 }
429 const std::string& mount_point = args[0];
430 if (mount_point.empty()) {
431 return ErrorAbort(state, kArgsParsingFailure,
432 "mount_point argument to unmount() can't be empty");
433 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700434
Tianjie Xu1536db82019-05-14 10:54:43 -0700435 auto updater = state->updater;
436 auto [mounted, result] = updater->GetRuntime()->Unmount(mount_point);
437 if (!mounted) {
Tianjie Xu58d59122019-05-03 01:05:04 -0700438 updater->UiPrint(
439 android::base::StringPrintf("Failed to unmount %s: No such volume", mount_point.c_str()));
Tao Bao039f2da2016-11-22 16:29:50 -0800440 return nullptr;
Tianjie Xu1536db82019-05-14 10:54:43 -0700441 } else if (result != 0) {
442 updater->UiPrint(android::base::StringPrintf("Failed to unmount %s: %s", mount_point.c_str(),
443 strerror(errno)));
Tao Bao039f2da2016-11-22 16:29:50 -0800444 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700445
Tao Bao039f2da2016-11-22 16:29:50 -0800446 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700447}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700448
Stephen Smalley779701d2012-02-09 14:13:23 -0500449// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700450//
Tao Bao039f2da2016-11-22 16:29:50 -0800451// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
452// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700453// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800454// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700455// 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 -0800456Value* FormatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
457 if (argv.size() != 5) {
458 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %zu", name,
459 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800460 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500461
Tao Bao039f2da2016-11-22 16:29:50 -0800462 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800463 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800464 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
465 }
466 const std::string& fs_type = args[0];
467 const std::string& partition_type = args[1];
468 const std::string& location = args[2];
469 const std::string& fs_size = args[3];
470 const std::string& mount_point = args[4];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700471
Tao Bao039f2da2016-11-22 16:29:50 -0800472 if (fs_type.empty()) {
473 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
474 }
475 if (partition_type.empty()) {
476 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
477 name);
478 }
479 if (location.empty()) {
480 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
481 }
482 if (mount_point.empty()) {
483 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
484 name);
485 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700486
Tao Bao039f2da2016-11-22 16:29:50 -0800487 int64_t size;
488 if (!android::base::ParseInt(fs_size, &size)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800489 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800490 fs_size.c_str());
491 }
492
Tianjie Xu1536db82019-05-14 10:54:43 -0700493 auto updater_runtime = state->updater->GetRuntime();
Tao Bao039f2da2016-11-22 16:29:50 -0800494 if (fs_type == "ext4") {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800495 std::vector<std::string> mke2fs_args = {
496 "/system/bin/mke2fs", "-t", "ext4", "-b", "4096", location
497 };
Jin Qianded2dac2017-04-21 14:36:12 -0700498 if (size != 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800499 mke2fs_args.push_back(std::to_string(size / 4096LL));
Jin Qianded2dac2017-04-21 14:36:12 -0700500 }
501
Tianjie Xu1536db82019-05-14 10:54:43 -0700502 if (auto status = updater_runtime->RunProgram(mke2fs_args, true); status != 0) {
Jin Qian502fd1c2017-11-02 11:58:12 -0700503 LOG(ERROR) << name << ": mke2fs failed (" << status << ") on " << location;
504 return StringValue("");
Jin Qianded2dac2017-04-21 14:36:12 -0700505 }
506
Tianjie Xu1536db82019-05-14 10:54:43 -0700507 if (auto status = updater_runtime->RunProgram(
508 { "/system/bin/e2fsdroid", "-e", "-a", mount_point, location }, true);
Tao Bao3d69f0d2018-12-20 09:44:06 -0800509 status != 0) {
Jin Qianded2dac2017-04-21 14:36:12 -0700510 LOG(ERROR) << name << ": e2fsdroid failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800511 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700512 }
Tao Bao039f2da2016-11-22 16:29:50 -0800513 return StringValue(location);
Tao Bao3d69f0d2018-12-20 09:44:06 -0800514 }
515
516 if (fs_type == "f2fs") {
Tao Bao039f2da2016-11-22 16:29:50 -0800517 if (size < 0) {
518 LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size;
519 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700520 }
Tao Bao3d69f0d2018-12-20 09:44:06 -0800521 std::vector<std::string> f2fs_args = {
Tao Baoc674dfb2018-12-20 14:25:15 -0800522 "/system/bin/make_f2fs", "-g", "android", "-w", "512", location
Tao Bao3d69f0d2018-12-20 09:44:06 -0800523 };
524 if (size >= 512) {
525 f2fs_args.push_back(std::to_string(size / 512));
526 }
Tianjie Xu1536db82019-05-14 10:54:43 -0700527 if (auto status = updater_runtime->RunProgram(f2fs_args, true); status != 0) {
Tao Baoc674dfb2018-12-20 14:25:15 -0800528 LOG(ERROR) << name << ": make_f2fs failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800529 return StringValue("");
530 }
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800531
Tianjie Xu1536db82019-05-14 10:54:43 -0700532 if (auto status = updater_runtime->RunProgram(
533 { "/system/bin/sload_f2fs", "-t", mount_point, location }, true);
Tao Baoc674dfb2018-12-20 14:25:15 -0800534 status != 0) {
535 LOG(ERROR) << name << ": sload_f2fs failed (" << status << ") on " << location;
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800536 return StringValue("");
537 }
538
Tao Bao039f2da2016-11-22 16:29:50 -0800539 return StringValue(location);
Tao Bao039f2da2016-11-22 16:29:50 -0800540 }
541
Tao Bao3d69f0d2018-12-20 09:44:06 -0800542 LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \""
543 << partition_type << "\"";
Tao Bao039f2da2016-11-22 16:29:50 -0800544 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700545}
546
Michael Bestas15a104f2019-09-19 21:42:12 +0300547// rename(src_name, dst_name)
548// Renames src_name to dst_name. It automatically creates the necessary directories for dst_name.
549// Example: rename("system/app/Hangouts/Hangouts.apk", "system/priv-app/Hangouts/Hangouts.apk")
550Value* RenameFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
551 if (argv.size() != 2) {
552 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
553 argv.size());
554 }
555
556 std::vector<std::string> args;
557 if (!ReadArgs(state, argv, &args)) {
558 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
559 }
560 const std::string& src_name = args[0];
561 const std::string& dst_name = args[1];
562
563 if (src_name.empty()) {
564 return ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", name);
565 }
566 if (dst_name.empty()) {
567 return ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", name);
568 }
569 if (!make_parents(dst_name)) {
570 return ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
571 dst_name.c_str(), strerror(errno));
572 } else if (access(dst_name.c_str(), F_OK) == 0 && access(src_name.c_str(), F_OK) != 0) {
573 // File was already moved
574 return StringValue(dst_name);
575 } else if (rename(src_name.c_str(), dst_name.c_str()) != 0) {
576 return ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
577 src_name.c_str(), dst_name.c_str(), strerror(errno));
578 }
579
580 return StringValue(dst_name);
581}
582
583// delete([filename, ...])
584// Deletes all the filenames listed. Returns the number of files successfully deleted.
585//
586// delete_recursive([dirname, ...])
587// Recursively deletes dirnames and all their contents. Returns the number of directories
588// successfully deleted.
589Value* DeleteFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
590 std::vector<std::string> paths;
591 if (!ReadArgs(state, argv, &paths)) {
592 return nullptr;
593 }
594
595 bool recursive = (strcmp(name, "delete_recursive") == 0);
596
597 int success = 0;
598 for (const auto& path : paths) {
599 if ((recursive ? dirUnlinkHierarchy(path.c_str()) : unlink(path.c_str())) == 0) {
600 ++success;
601 }
602 }
603
604 return StringValue(std::to_string(success));
605}
606
Tianjie Xuc4447322017-03-06 14:44:59 -0800607Value* ShowProgressFn(const char* name, State* state,
608 const std::vector<std::unique_ptr<Expr>>& argv) {
609 if (argv.size() != 2) {
610 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
611 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800612 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700613
Tao Bao039f2da2016-11-22 16:29:50 -0800614 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800615 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800616 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
617 }
618 const std::string& frac_str = args[0];
619 const std::string& sec_str = args[1];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700620
Tao Bao039f2da2016-11-22 16:29:50 -0800621 double frac;
622 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800623 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800624 frac_str.c_str());
625 }
626 int sec;
627 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800628 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800629 sec_str.c_str());
630 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700631
Tianjie Xu1536db82019-05-14 10:54:43 -0700632 state->updater->WriteToCommandPipe(android::base::StringPrintf("progress %f %d", frac, sec));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700633
Tao Bao039f2da2016-11-22 16:29:50 -0800634 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700635}
636
Tianjie Xu5419ad32018-02-02 16:49:15 -0800637Value* SetProgressFn(const char* name, State* state,
638 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800639 if (argv.size() != 1) {
640 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800641 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700642
Tao Bao039f2da2016-11-22 16:29:50 -0800643 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800644 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800645 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
646 }
647 const std::string& frac_str = args[0];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700648
Tao Bao039f2da2016-11-22 16:29:50 -0800649 double frac;
650 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800651 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800652 frac_str.c_str());
653 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700654
Tianjie Xu1536db82019-05-14 10:54:43 -0700655 state->updater->WriteToCommandPipe(android::base::StringPrintf("set_progress %f", frac));
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700656
Tao Bao039f2da2016-11-22 16:29:50 -0800657 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700658}
659
Michael Bestas15a104f2019-09-19 21:42:12 +0300660// symlink(target, [src1, src2, ...])
661// Creates all sources as symlinks to target. It unlinks any previously existing src1, src2, etc
662// before creating symlinks.
663Value* SymlinkFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
664 if (argv.size() == 0) {
665 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %zu", name,
666 argv.size());
667 }
668
669 std::vector<std::string> args;
670 if (!ReadArgs(state, argv, &args)) {
671 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
672 }
673
674 const auto& target = args[0];
675 if (target.empty()) {
676 return ErrorAbort(state, kArgsParsingFailure, "%s() target argument can't be empty", name);
677 }
678
679 size_t bad = 0;
680 for (size_t i = 1; i < args.size(); ++i) {
681 const auto& src = args[i];
682 if (unlink(src.c_str()) == -1 && errno != ENOENT) {
683 PLOG(ERROR) << name << ": failed to remove " << src;
684 ++bad;
685 } else if (!make_parents(src)) {
686 LOG(ERROR) << name << ": failed to symlink " << src << " to " << target
687 << ": making parents failed";
688 ++bad;
689 } else if (symlink(target.c_str(), src.c_str()) == -1) {
690 PLOG(ERROR) << name << ": failed to symlink " << src << " to " << target;
691 ++bad;
692 }
693 }
694 if (bad != 0) {
695 return ErrorAbort(state, kSymlinkFailure, "%s: Failed to create %zu symlink(s)", name, bad);
696 }
697 return StringValue("t");
698}
699
700struct perm_parsed_args {
701 bool has_uid;
702 uid_t uid;
703 bool has_gid;
704 gid_t gid;
705 bool has_mode;
706 mode_t mode;
707 bool has_fmode;
708 mode_t fmode;
709 bool has_dmode;
710 mode_t dmode;
711 bool has_selabel;
712 const char* selabel;
713 bool has_capabilities;
714 uint64_t capabilities;
715};
716
717static struct perm_parsed_args ParsePermArgs(State* state, const std::vector<std::string>& args) {
718 struct perm_parsed_args parsed;
719 auto updater = state->updater;
720 int bad = 0;
721 static int max_warnings = 20;
722
723 memset(&parsed, 0, sizeof(parsed));
724
725 for (size_t i = 1; i < args.size(); i += 2) {
726 if (args[i] == "uid") {
727 int64_t uid;
728 if (sscanf(args[i + 1].c_str(), "%" SCNd64, &uid) == 1) {
729 parsed.uid = uid;
730 parsed.has_uid = true;
731 } else {
732 updater->UiPrint(android::base::StringPrintf("ParsePermArgs: invalid UID \"%s\"\n",
733 args[i + 1].c_str()));
734 bad++;
735 }
736 continue;
737 }
738 if (args[i] == "gid") {
739 int64_t gid;
740 if (sscanf(args[i + 1].c_str(), "%" SCNd64, &gid) == 1) {
741 parsed.gid = gid;
742 parsed.has_gid = true;
743 } else {
744 updater->UiPrint(android::base::StringPrintf("ParsePermArgs: invalid GID \"%s\"\n",
745 args[i + 1].c_str()));
746 bad++;
747 }
748 continue;
749 }
750 if (args[i] == "mode") {
751 int32_t mode;
752 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
753 parsed.mode = mode;
754 parsed.has_mode = true;
755 } else {
756 updater->UiPrint(android::base::StringPrintf("ParsePermArgs: invalid mode \"%s\"\n",
757 args[i + 1].c_str()));
758 bad++;
759 }
760 continue;
761 }
762 if (args[i] == "dmode") {
763 int32_t mode;
764 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
765 parsed.dmode = mode;
766 parsed.has_dmode = true;
767 } else {
768 updater->UiPrint(android::base::StringPrintf("ParsePermArgs: invalid dmode \"%s\"\n",
769 args[i + 1].c_str()));
770 bad++;
771 }
772 continue;
773 }
774 if (args[i] == "fmode") {
775 int32_t mode;
776 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
777 parsed.fmode = mode;
778 parsed.has_fmode = true;
779 } else {
780 updater->UiPrint(android::base::StringPrintf("ParsePermArgs: invalid fmode \"%s\"\n",
781 args[i + 1].c_str()));
782 bad++;
783 }
784 continue;
785 }
786 if (args[i] == "capabilities") {
787 int64_t capabilities;
788 if (sscanf(args[i + 1].c_str(), "%" SCNi64, &capabilities) == 1) {
789 parsed.capabilities = capabilities;
790 parsed.has_capabilities = true;
791 } else {
792 updater->UiPrint(android::base::StringPrintf("ParsePermArgs: invalid capabilities \"%s\"\n",
793 args[i + 1].c_str()));
794 bad++;
795 }
796 continue;
797 }
798 if (args[i] == "selabel") {
799 if (!args[i + 1].empty()) {
800 parsed.selabel = args[i + 1].c_str();
801 parsed.has_selabel = true;
802 } else {
803 updater->UiPrint(android::base::StringPrintf("ParsePermArgs: invalid selabel \"%s\"\n",
804 args[i + 1].c_str()));
805 bad++;
806 }
807 continue;
808 }
809 if (max_warnings != 0) {
810 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i].c_str());
811 max_warnings--;
812 if (max_warnings == 0) {
813 LOG(INFO) << "ParsedPermArgs: suppressing further warnings";
814 }
815 }
816 }
817 return parsed;
818}
819
820static int ApplyParsedPerms(State* state, const char* filename, const struct stat* statptr,
821 struct perm_parsed_args parsed) {
822 auto updater = state->updater;
823 int bad = 0;
824
825 if (parsed.has_selabel) {
826 if (lsetfilecon(filename, parsed.selabel) != 0) {
827 updater->UiPrint(android::base::StringPrintf(
828 "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
829 filename, parsed.selabel, strerror(errno)));
830 bad++;
831 }
832 }
833
834 /* ignore symlinks */
835 if (S_ISLNK(statptr->st_mode)) {
836 return bad;
837 }
838
839 if (parsed.has_uid) {
840 if (chown(filename, parsed.uid, -1) < 0) {
841 updater->UiPrint(android::base::StringPrintf(
842 "ApplyParsedPerms: chown of %s to %d failed: %s\n",
843 filename, parsed.uid, strerror(errno)));
844 bad++;
845 }
846 }
847
848 if (parsed.has_gid) {
849 if (chown(filename, -1, parsed.gid) < 0) {
850 updater->UiPrint(android::base::StringPrintf(
851 "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
852 filename, parsed.gid, strerror(errno)));
853 bad++;
854 }
855 }
856
857 if (parsed.has_mode) {
858 if (chmod(filename, parsed.mode) < 0) {
859 updater->UiPrint(android::base::StringPrintf(
860 "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
861 filename, parsed.mode, strerror(errno)));
862 bad++;
863 }
864 }
865
866 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
867 if (chmod(filename, parsed.dmode) < 0) {
868 updater->UiPrint(android::base::StringPrintf(
869 "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
870 filename, parsed.dmode, strerror(errno)));
871 bad++;
872 }
873 }
874
875 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
876 if (chmod(filename, parsed.fmode) < 0) {
877 updater->UiPrint(android::base::StringPrintf(
878 "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
879 filename, parsed.fmode, strerror(errno)));
880 bad++;
881 }
882 }
883
884 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
885 if (parsed.capabilities == 0) {
886 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
887 // Report failure unless it's ENODATA (attribute not set)
888 updater->UiPrint(android::base::StringPrintf(
889 "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
890 filename, parsed.capabilities, strerror(errno)));
891 bad++;
892 }
893 } else {
894 struct vfs_cap_data cap_data;
895 memset(&cap_data, 0, sizeof(cap_data));
896 cap_data.magic_etc = VFS_CAP_REVISION_2 | VFS_CAP_FLAGS_EFFECTIVE;
897 cap_data.data[0].permitted = (uint32_t)(parsed.capabilities & 0xffffffff);
898 cap_data.data[0].inheritable = 0;
899 cap_data.data[1].permitted = (uint32_t)(parsed.capabilities >> 32);
900 cap_data.data[1].inheritable = 0;
901 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
902 updater->UiPrint(android::base::StringPrintf(
903 "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
904 filename, parsed.capabilities, strerror(errno)));
905 bad++;
906 }
907 }
908 }
909
910 return bad;
911}
912
913// nftw doesn't allow us to pass along context, so we need to use
914// global variables. *sigh*
915static struct perm_parsed_args recursive_parsed_args;
916static State* recursive_state;
917
918static int do_SetMetadataRecursive(const char* filename, const struct stat* statptr,
919 int /*fileflags*/, struct FTW* /*pfwt*/) {
920 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
921}
922
923static Value* SetMetadataFn(const char* name, State* state,
924 const std::vector<std::unique_ptr<Expr>>& argv) {
925 if ((argv.size() % 2) != 1) {
926 return ErrorAbort(state, kArgsParsingFailure,
927 "%s() expects an odd number of arguments, got %zu", name, argv.size());
928 }
929
930 std::vector<std::string> args;
931 if (!ReadArgs(state, argv, &args)) {
932 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
933 }
934
935 struct stat sb;
936 if (lstat(args[0].c_str(), &sb) == -1) {
937 return ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s", name,
938 args[0].c_str(), strerror(errno));
939 }
940
941 struct perm_parsed_args parsed = ParsePermArgs(state, args);
942 int bad = 0;
943 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
944
945 if (recursive) {
946 recursive_parsed_args = parsed;
947 recursive_state = state;
948 bad += nftw(args[0].c_str(), do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
949 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
950 recursive_state = NULL;
951 } else {
952 bad += ApplyParsedPerms(state, args[0].c_str(), &sb, parsed);
953 }
954
955 if (bad > 0) {
956 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
957 }
958
959 return StringValue("");
960}
961
Tianjie Xuc4447322017-03-06 14:44:59 -0800962Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
963 if (argv.size() != 1) {
964 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800965 }
966 std::string key;
967 if (!Evaluate(state, argv[0], &key)) {
968 return nullptr;
969 }
Tianjie Xu1536db82019-05-14 10:54:43 -0700970
971 auto updater_runtime = state->updater->GetRuntime();
972 std::string value = updater_runtime->GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700973
Tao Bao039f2da2016-11-22 16:29:50 -0800974 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700975}
976
Doug Zongker47cace92009-06-18 10:11:50 -0700977// file_getprop(file, key)
978//
979// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -0700980// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700981// and returns the value for 'key' (or "" if it isn't defined).
Tianjie Xu5419ad32018-02-02 16:49:15 -0800982Value* FileGetPropFn(const char* name, State* state,
983 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800984 if (argv.size() != 2) {
985 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
986 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800987 }
988
989 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800990 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800991 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
992 }
993 const std::string& filename = args[0];
994 const std::string& key = args[1];
995
Tianjie Xu22f11202018-08-27 10:50:31 -0700996 std::string buffer;
Tianjie Xu1536db82019-05-14 10:54:43 -0700997 auto updater_runtime = state->updater->GetRuntime();
998 if (!updater_runtime->ReadFileToString(filename, &buffer)) {
Tianjie Xu22f11202018-08-27 10:50:31 -0700999 ErrorAbort(state, kFreadFailure, "%s: failed to read %s", name, filename.c_str());
Tao Bao358c2ec2016-11-28 11:48:43 -08001000 return nullptr;
1001 }
1002
Tao Bao358c2ec2016-11-28 11:48:43 -08001003 std::vector<std::string> lines = android::base::Split(buffer, "\n");
1004 for (size_t i = 0; i < lines.size(); i++) {
1005 std::string line = android::base::Trim(lines[i]);
1006
1007 // comment or blank line: skip to next line
1008 if (line.empty() || line[0] == '#') {
1009 continue;
1010 }
1011 size_t equal_pos = line.find('=');
1012 if (equal_pos == std::string::npos) {
1013 continue;
Tao Bao51d516e2016-11-03 14:49:01 -07001014 }
1015
Tao Bao358c2ec2016-11-28 11:48:43 -08001016 // trim whitespace between key and '='
1017 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -07001018
Tao Bao358c2ec2016-11-28 11:48:43 -08001019 // not the key we're looking for
1020 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001021
Tao Bao358c2ec2016-11-28 11:48:43 -08001022 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
1023 }
Doug Zongker47cace92009-06-18 10:11:50 -07001024
Tao Bao358c2ec2016-11-28 11:48:43 -08001025 return StringValue("");
Doug Zongker47cace92009-06-18 10:11:50 -07001026}
1027
Doug Zongker8edb00c2009-06-11 17:21:44 -07001028// apply_patch_space(bytes)
Tianjie Xu5419ad32018-02-02 16:49:15 -08001029Value* ApplyPatchSpaceFn(const char* name, State* state,
1030 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -08001031 if (argv.size() != 1) {
1032 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name,
1033 argv.size());
1034 }
Tao Bao039f2da2016-11-22 16:29:50 -08001035 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001036 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001037 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1038 }
1039 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -08001040
Tao Bao039f2da2016-11-22 16:29:50 -08001041 size_t bytes;
1042 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -08001043 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name,
1044 bytes_str.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -08001045 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001046
Tianjie Xu99b73be2017-11-28 17:23:06 -08001047 // Skip the cache size check if the update is a retry.
Tao Bao5ee25662018-07-11 15:55:32 -07001048 if (state->is_retry || CheckAndFreeSpaceOnCache(bytes)) {
Tianjie Xu99b73be2017-11-28 17:23:06 -08001049 return StringValue("t");
1050 }
1051 return StringValue("");
Doug Zongkerc4351c72010-02-22 14:46:32 -08001052}
1053
Tianjie Xuc4447322017-03-06 14:44:59 -08001054Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1055 if (!argv.empty()) {
1056 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
1057 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001058 }
Tianjie Xu58d59122019-05-03 01:05:04 -07001059
Tianjie Xu1536db82019-05-14 10:54:43 -07001060 state->updater->WriteToCommandPipe("wipe_cache");
Tao Bao039f2da2016-11-22 16:29:50 -08001061 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -07001062}
1063
Tianjie Xuc4447322017-03-06 14:44:59 -08001064Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1065 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -08001066 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
1067 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001068
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 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001073
Tianjie Xu1536db82019-05-14 10:54:43 -07001074 auto updater_runtime = state->updater->GetRuntime();
1075 auto status = updater_runtime->RunProgram(args, false);
Tao Bao039f2da2016-11-22 16:29:50 -08001076 return StringValue(std::to_string(status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001077}
1078
Tao Bao511d7592018-06-19 15:56:49 -07001079// read_file(filename)
Tao Baobafd6c72018-07-09 15:08:50 -07001080// Reads a local file 'filename' and returns its contents as a string Value.
Tianjie Xuc4447322017-03-06 14:44:59 -08001081Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1082 if (argv.size() != 1) {
1083 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001084 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001085
Tao Bao039f2da2016-11-22 16:29:50 -08001086 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001087 if (!ReadArgs(state, argv, &args)) {
Tao Bao511d7592018-06-19 15:56:49 -07001088 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tao Bao039f2da2016-11-22 16:29:50 -08001089 }
1090 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -08001091
Tao Baobafd6c72018-07-09 15:08:50 -07001092 std::string contents;
Tianjie Xu1536db82019-05-14 10:54:43 -07001093 auto updater_runtime = state->updater->GetRuntime();
1094 if (updater_runtime->ReadFileToString(filename, &contents)) {
Tao Baobafd6c72018-07-09 15:08:50 -07001095 return new Value(Value::Type::STRING, std::move(contents));
Tao Bao039f2da2016-11-22 16:29:50 -08001096 }
Tao Bao511d7592018-06-19 15:56:49 -07001097
1098 // Leave it to caller to handle the failure.
Tao Baobafd6c72018-07-09 15:08:50 -07001099 PLOG(ERROR) << name << ": Failed to read " << filename;
Tao Bao511d7592018-06-19 15:56:49 -07001100 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -08001101}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001102
Tao Baod0f30882016-11-03 23:52:01 -07001103// write_value(value, filename)
1104// Writes 'value' to 'filename'.
1105// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
Tianjie Xuc4447322017-03-06 14:44:59 -08001106Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1107 if (argv.size() != 2) {
1108 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1109 argv.size());
Tao Baod0f30882016-11-03 23:52:01 -07001110 }
1111
1112 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001113 if (!ReadArgs(state, argv, &args)) {
Tao Baod0f30882016-11-03 23:52:01 -07001114 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1115 }
1116
1117 const std::string& filename = args[1];
1118 if (filename.empty()) {
1119 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
1120 }
1121
1122 const std::string& value = args[0];
Tianjie Xu1536db82019-05-14 10:54:43 -07001123 auto updater_runtime = state->updater->GetRuntime();
1124 if (!updater_runtime->WriteStringToFile(value, filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001125 PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\"";
Tao Baod0f30882016-11-03 23:52:01 -07001126 return StringValue("");
Tao Baod0f30882016-11-03 23:52:01 -07001127 }
Tianjie Xu1536db82019-05-14 10:54:43 -07001128 return StringValue("t");
Tao Baod0f30882016-11-03 23:52:01 -07001129}
1130
Doug Zongkerc87bab12013-11-25 13:53:25 -08001131// Immediately reboot the device. Recovery is not finished normally,
1132// so if you reboot into recovery it will re-start applying the
1133// current package (because nothing has cleared the copy of the
1134// arguments stored in the BCB).
1135//
1136// The argument is the partition name passed to the android reboot
1137// property. It can be "recovery" to boot from the recovery
1138// partition, or "" (empty string) to boot from the regular boot
1139// partition.
Tianjie Xuc4447322017-03-06 14:44:59 -08001140Value* RebootNowFn(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 Baobedf5fc2016-11-18 12:01:26 -08001144 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001145
Tao Baobedf5fc2016-11-18 12:01:26 -08001146 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001147 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001148 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1149 }
1150 const std::string& filename = args[0];
1151 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001152
Tao Baobedf5fc2016-11-18 12:01:26 -08001153 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
1154 bootloader_message boot;
1155 std::string err;
1156 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001157 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001158 return StringValue("");
1159 }
1160 memset(boot.command, 0, sizeof(boot.command));
1161 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001162 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001163 return StringValue("");
1164 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001165
Tao Bao782dcc12019-04-29 11:23:16 -07001166 Reboot(property);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001167
Tao Baobedf5fc2016-11-18 12:01:26 -08001168 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001169}
1170
1171// Store a string value somewhere that future invocations of recovery
1172// can access it. This value is called the "stage" and can be used to
1173// drive packages that need to do reboots in the middle of
1174// installation and keep track of where they are in the multi-stage
1175// install.
1176//
1177// The first argument is the block device for the misc partition
1178// ("/misc" in the fstab), which is where this value is stored. The
1179// second argument is the string to store; it should not exceed 31
1180// bytes.
Tianjie Xuc4447322017-03-06 14:44:59 -08001181Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1182 if (argv.size() != 2) {
1183 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1184 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001185 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001186
Tao Baobedf5fc2016-11-18 12:01:26 -08001187 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001188 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001189 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1190 }
1191 const std::string& filename = args[0];
1192 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001193
Tao Baobedf5fc2016-11-18 12:01:26 -08001194 // Store this value in the misc partition, immediately after the
1195 // bootloader message that the main recovery uses to save its
1196 // arguments in case of the device restarting midway through
1197 // package installation.
1198 bootloader_message boot;
1199 std::string err;
1200 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001201 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001202 return StringValue("");
1203 }
1204 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
1205 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001206 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001207 return StringValue("");
1208 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001209
Tao Baobedf5fc2016-11-18 12:01:26 -08001210 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001211}
1212
1213// Return the value most recently saved with SetStageFn. The argument
1214// is the block device for the misc partition.
Tianjie Xuc4447322017-03-06 14:44:59 -08001215Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1216 if (argv.size() != 1) {
1217 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001218 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001219
Tao Baobedf5fc2016-11-18 12:01:26 -08001220 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001221 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001222 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1223 }
1224 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001225
Tao Baobedf5fc2016-11-18 12:01:26 -08001226 bootloader_message boot;
1227 std::string err;
1228 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001229 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001230 return StringValue("");
1231 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001232
Tao Baobedf5fc2016-11-18 12:01:26 -08001233 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001234}
1235
Tianjie Xuc4447322017-03-06 14:44:59 -08001236Value* WipeBlockDeviceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1237 if (argv.size() != 2) {
1238 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1239 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -08001240 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001241
Tao Bao358c2ec2016-11-28 11:48:43 -08001242 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001243 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -08001244 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1245 }
1246 const std::string& filename = args[0];
1247 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001248
Tao Bao358c2ec2016-11-28 11:48:43 -08001249 size_t len;
1250 if (!android::base::ParseUint(len_str.c_str(), &len)) {
1251 return nullptr;
1252 }
Tianjie Xu22f11202018-08-27 10:50:31 -07001253
Tianjie Xu1536db82019-05-14 10:54:43 -07001254 auto updater_runtime = state->updater->GetRuntime();
1255 int status = updater_runtime->WipeBlockDevice(filename, len);
1256 return StringValue(status == 0 ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001257}
1258
Tianjie Xuc4447322017-03-06 14:44:59 -08001259Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1260 if (!argv.empty()) {
1261 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
1262 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001263 }
Tianjie Xu1536db82019-05-14 10:54:43 -07001264 state->updater->WriteToCommandPipe("enable_reboot");
Tao Bao039f2da2016-11-22 16:29:50 -08001265 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -07001266}
1267
Tianjie Xuc4447322017-03-06 14:44:59 -08001268Value* Tune2FsFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1269 if (argv.empty()) {
1270 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001271 }
Michael Rungeacf47db2014-11-21 00:12:28 -08001272
Tao Bao039f2da2016-11-22 16:29:50 -08001273 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001274 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001275 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
1276 }
Michael Rungeacf47db2014-11-21 00:12:28 -08001277
Tao Bao3d69f0d2018-12-20 09:44:06 -08001278 // tune2fs expects the program name as its first arg.
1279 args.insert(args.begin(), "tune2fs");
Tianjie Xu1536db82019-05-14 10:54:43 -07001280 auto updater_runtime = state->updater->GetRuntime();
1281 if (auto result = updater_runtime->Tune2Fs(args); result != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -08001282 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
1283 }
1284 return StringValue("t");
Michael Rungeacf47db2014-11-21 00:12:28 -08001285}
1286
Yifan Hong0c328d02020-04-28 13:31:11 -07001287Value* AddSlotSuffixFn(const char* name, State* state,
1288 const std::vector<std::unique_ptr<Expr>>& argv) {
1289 if (argv.size() != 1) {
1290 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
1291 }
1292 std::vector<std::string> args;
1293 if (!ReadArgs(state, argv, &args)) {
1294 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1295 }
1296 const std::string& arg = args[0];
1297 auto updater_runtime = state->updater->GetRuntime();
1298 return StringValue(updater_runtime->AddSlotSuffix(arg));
1299}
1300
Doug Zongker9931f7f2009-06-10 14:11:53 -07001301void RegisterInstallFunctions() {
Tao Bao039f2da2016-11-22 16:29:50 -08001302 RegisterFunction("mount", MountFn);
1303 RegisterFunction("is_mounted", IsMountedFn);
1304 RegisterFunction("unmount", UnmountFn);
1305 RegisterFunction("format", FormatFn);
1306 RegisterFunction("show_progress", ShowProgressFn);
1307 RegisterFunction("set_progress", SetProgressFn);
Michael Bestas15a104f2019-09-19 21:42:12 +03001308 RegisterFunction("delete", DeleteFn);
1309 RegisterFunction("delete_recursive", DeleteFn);
1310 RegisterFunction("package_extract_dir", PackageExtractDirFn);
Tao Bao039f2da2016-11-22 16:29:50 -08001311 RegisterFunction("package_extract_file", PackageExtractFileFn);
Michael Bestas15a104f2019-09-19 21:42:12 +03001312 RegisterFunction("symlink", SymlinkFn);
1313
1314 // Usage:
1315 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1316 // Example:
1317 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel",
1318 // "u:object_r:system_file:s0", "capabilities", 0x0);
1319 RegisterFunction("set_metadata", SetMetadataFn);
1320
1321 // Usage:
1322 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1323 // Example:
1324 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755,
1325 // "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1326 RegisterFunction("set_metadata_recursive", SetMetadataFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001327
Tao Bao039f2da2016-11-22 16:29:50 -08001328 RegisterFunction("getprop", GetPropFn);
1329 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001330
Tao Bao039f2da2016-11-22 16:29:50 -08001331 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Tao Bao5609bc82018-06-20 00:30:48 -07001332 RegisterFunction("patch_partition", PatchPartitionFn);
1333 RegisterFunction("patch_partition_check", PatchPartitionCheckFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001334
Tao Bao039f2da2016-11-22 16:29:50 -08001335 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001336
Tao Bao039f2da2016-11-22 16:29:50 -08001337 RegisterFunction("read_file", ReadFileFn);
Michael Bestas15a104f2019-09-19 21:42:12 +03001338 RegisterFunction("rename", RenameFn);
Tao Bao039f2da2016-11-22 16:29:50 -08001339 RegisterFunction("write_value", WriteValueFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001340
Tao Bao039f2da2016-11-22 16:29:50 -08001341 RegisterFunction("wipe_cache", WipeCacheFn);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001342
Tao Bao039f2da2016-11-22 16:29:50 -08001343 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001344
Tao Bao039f2da2016-11-22 16:29:50 -08001345 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001346
Tao Bao039f2da2016-11-22 16:29:50 -08001347 RegisterFunction("reboot_now", RebootNowFn);
1348 RegisterFunction("get_stage", GetStageFn);
1349 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001350
Tao Bao039f2da2016-11-22 16:29:50 -08001351 RegisterFunction("enable_reboot", EnableRebootFn);
1352 RegisterFunction("tune2fs", Tune2FsFn);
Yifan Hong0c328d02020-04-28 13:31:11 -07001353
1354 RegisterFunction("add_slot_suffix", AddSlotSuffixFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001355}