blob: 93c890411e5960069de518592e9e93889a4cea21 [file] [log] [blame]
Doug Zongker9931f7f2009-06-10 14:11:53 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tao Bao0c7839a2016-10-10 15:48:37 -070017#include "updater/install.h"
18
Doug Zongkerfbf3c102009-06-24 09:36:20 -070019#include <ctype.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070020#include <errno.h>
Tao Bao361342c2016-02-08 11:15:50 -080021#include <fcntl.h>
22#include <ftw.h>
23#include <inttypes.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070024#include <stdarg.h>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070025#include <stdio.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070026#include <stdlib.h>
27#include <string.h>
Tao Bao361342c2016-02-08 11:15:50 -080028#include <sys/capability.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070029#include <sys/mount.h>
30#include <sys/stat.h>
31#include <sys/types.h>
Doug Zongkera3f89ea2009-09-10 14:10:48 -070032#include <sys/wait.h>
Nick Kralevich5dbdef02013-09-07 14:41:06 -070033#include <sys/xattr.h>
Tao Bao361342c2016-02-08 11:15:50 -080034#include <time.h>
35#include <unistd.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070036#include <utime.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070037
Yabin Cui64be2132016-02-03 18:16:02 -080038#include <memory>
Tao Bao361342c2016-02-08 11:15:50 -080039#include <string>
Yabin Cui64be2132016-02-03 18:16:02 -080040#include <vector>
41
Tao Baod0f30882016-11-03 23:52:01 -070042#include <android-base/file.h>
Tao Bao039f2da2016-11-22 16:29:50 -080043#include <android-base/logging.h>
Tianjie Xu5fe280a2016-10-17 18:15:20 -070044#include <android-base/parsedouble.h>
Tao Baod0f30882016-11-03 23:52:01 -070045#include <android-base/parseint.h>
Elliott Hughescb220402016-09-23 15:30:55 -070046#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080047#include <android-base/stringprintf.h>
Tao Baod0f30882016-11-03 23:52:01 -070048#include <android-base/strings.h>
Tianjie Xu22f11202018-08-27 10:50:31 -070049#include <android-base/unique_fd.h>
Tao Bao0d3f84f2016-12-28 15:09:20 -080050#include <applypatch/applypatch.h>
51#include <bootloader_message/bootloader_message.h>
Tao Baode40ba52016-10-05 23:17:01 -070052#include <ext4_utils/wipe.h>
Tao Bao361342c2016-02-08 11:15:50 -080053#include <openssl/sha.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070054#include <selinux/label.h>
55#include <selinux/selinux.h>
Tao Bao09e468f2017-09-29 14:39:33 -070056#include <tune2fs.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070057#include <ziparchive/zip_archive.h>
Tao Bao1107d962015-09-09 17:16:55 -070058
Doug Zongker9931f7f2009-06-10 14:11:53 -070059#include "edify/expr.h"
Tao Bao17054c02018-05-03 22:41:23 -070060#include "otautil/dirutil.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070061#include "otautil/error_code.h"
Tao Bao9a319f02018-01-04 13:19:11 -080062#include "otautil/mounts.h"
Tao Bao09e468f2017-09-29 14:39:33 -070063#include "otautil/print_sha1.h"
Tao Bao2c526392018-05-03 23:01:13 -070064#include "otautil/sysutil.h"
Michael Bestas847c0652019-09-19 21:42:12 +030065#include "otautil/ziputil.h"
66#include "tune2fs.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070067#include "updater/updater.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070068
Tao Bao1107d962015-09-09 17:16:55 -070069// Send over the buffer to recovery though the command pipe.
70static void uiPrint(State* state, const std::string& buffer) {
Tao Bao039f2da2016-11-22 16:29:50 -080071 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070072
Tao Bao039f2da2016-11-22 16:29:50 -080073 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
74 // So skip sending empty strings to UI.
75 std::vector<std::string> lines = android::base::Split(buffer, "\n");
76 for (auto& line : lines) {
77 if (!line.empty()) {
78 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
Tao Bao1107d962015-09-09 17:16:55 -070079 }
Tao Bao039f2da2016-11-22 16:29:50 -080080 }
Tao Bao1107d962015-09-09 17:16:55 -070081
Tao Bao039f2da2016-11-22 16:29:50 -080082 // On the updater side, we need to dump the contents to stderr (which has
83 // been redirected to the log file). Because the recovery will only print
84 // the contents to screen when processing pipe command ui_print.
85 LOG(INFO) << buffer;
Michael Runged4a63422014-10-22 19:48:41 -070086}
87
Elliott Hughes83ce7552016-06-30 09:28:42 -070088void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) {
Tao Bao039f2da2016-11-22 16:29:50 -080089 std::string error_msg;
Tao Bao1107d962015-09-09 17:16:55 -070090
Tao Bao039f2da2016-11-22 16:29:50 -080091 va_list ap;
92 va_start(ap, format);
93 android::base::StringAppendV(&error_msg, format, ap);
94 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070095
Tao Bao039f2da2016-11-22 16:29:50 -080096 uiPrint(state, error_msg);
Michael Runged4a63422014-10-22 19:48:41 -070097}
98
Michael Bestas847c0652019-09-19 21:42:12 +030099static bool is_dir(const std::string& dirpath) {
100 struct stat st;
101 return stat(dirpath.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
102}
103
104// Create all parent directories of name, if necessary.
105static bool make_parents(const std::string& name) {
106 size_t prev_end = 0;
107 while (prev_end < name.size()) {
108 size_t next_end = name.find('/', prev_end + 1);
109 if (next_end == std::string::npos) {
110 break;
111 }
112 std::string dir_path = name.substr(0, next_end);
113 if (!is_dir(dir_path)) {
114 int result = mkdir(dir_path.c_str(), 0700);
115 if (result != 0) {
116 PLOG(ERROR) << "failed to mkdir " << dir_path << " when make parents for " << name;
117 return false;
118 }
119
120 LOG(INFO) << "created [" << dir_path << "]";
121 }
122 prev_end = next_end;
123 }
124 return true;
125}
126
Tianjie Xu5419ad32018-02-02 16:49:15 -0800127// This is the updater side handler for ui_print() in edify script. Contents will be sent over to
128// the recovery side for on-screen display.
129Value* UIPrintFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
130 std::vector<std::string> args;
131 if (!ReadArgs(state, argv, &args)) {
132 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
133 }
134
135 std::string buffer = android::base::Join(args, "");
136 uiPrint(state, buffer);
137 return StringValue(buffer);
138}
139
Michael Bestas847c0652019-09-19 21:42:12 +0300140// package_extract_dir(package_dir, dest_dir)
141// Extracts all files from the package underneath package_dir and writes them to the
142// corresponding tree beneath dest_dir. Any existing files are overwritten.
143// Example: package_extract_dir("system", "/system")
144//
145// Note: package_dir needs to be a relative path; dest_dir needs to be an absolute path.
146Value* PackageExtractDirFn(const char* name, State* state,
147 const std::vector<std::unique_ptr<Expr>>& argv) {
148 if (argv.size() != 2) {
149 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
150 argv.size());
151 }
152
153 std::vector<std::string> args;
154 if (!ReadArgs(state, argv, &args)) {
155 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
156 }
157 const std::string& zip_path = args[0];
158 const std::string& dest_path = args[1];
159
160 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
161
162 // To create a consistent system image, never use the clock for timestamps.
163 constexpr struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
164
165 bool success = ExtractPackageRecursive(za, zip_path, dest_path, &timestamp, sehandle);
166
167 return StringValue(success ? "t" : "");
168}
169
Tianjie Xu5419ad32018-02-02 16:49:15 -0800170// package_extract_file(package_file[, dest_file])
171// Extracts a single package_file from the update package and writes it to dest_file,
172// overwriting existing files if necessary. Without the dest_file argument, returns the
173// contents of the package file as a binary blob.
174Value* PackageExtractFileFn(const char* name, State* state,
175 const std::vector<std::unique_ptr<Expr>>& argv) {
176 if (argv.size() < 1 || argv.size() > 2) {
177 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name,
178 argv.size());
179 }
180
181 if (argv.size() == 2) {
182 // The two-argument version extracts to a file.
183
184 std::vector<std::string> args;
185 if (!ReadArgs(state, argv, &args)) {
186 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
187 argv.size());
188 }
189 const std::string& zip_path = args[0];
190 const std::string& dest_path = args[1];
191
192 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
193 ZipString zip_string_path(zip_path.c_str());
194 ZipEntry entry;
195 if (FindEntry(za, zip_string_path, &entry) != 0) {
196 LOG(ERROR) << name << ": no " << zip_path << " in package";
197 return StringValue("");
198 }
199
Tianjie Xu22f11202018-08-27 10:50:31 -0700200 android::base::unique_fd fd(TEMP_FAILURE_RETRY(
201 open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tianjie Xu5419ad32018-02-02 16:49:15 -0800202 if (fd == -1) {
203 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
204 return StringValue("");
205 }
206
207 bool success = true;
208 int32_t ret = ExtractEntryToFile(za, &entry, fd);
209 if (ret != 0) {
210 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
211 << entry.uncompressed_length << " bytes) to \"" << dest_path
212 << "\": " << ErrorCodeString(ret);
213 success = false;
214 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700215 if (fsync(fd) == -1) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800216 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
217 success = false;
218 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700219
220 if (close(fd.release()) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800221 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
222 success = false;
223 }
224
225 return StringValue(success ? "t" : "");
226 } else {
227 // The one-argument version returns the contents of the file as the result.
228
229 std::vector<std::string> args;
230 if (!ReadArgs(state, argv, &args)) {
231 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
232 argv.size());
233 }
234 const std::string& zip_path = args[0];
235
236 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
237 ZipString zip_string_path(zip_path.c_str());
238 ZipEntry entry;
239 if (FindEntry(za, zip_string_path, &entry) != 0) {
240 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
241 zip_path.c_str());
242 }
243
244 std::string buffer;
245 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
Tao Bao5609bc82018-06-20 00:30:48 -0700292 bool result = PatchPartitionCheck(target, source);
293 return StringValue(result ? "t" : "");
Tianjie Xu5419ad32018-02-02 16:49:15 -0800294}
295
Tao Bao5609bc82018-06-20 00:30:48 -0700296// patch_partition(target, source, patch)
297// Applies the given patch to the source partition, and writes the result to the target partition.
298//
299// For example, patch_partition(
300// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
301// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4",
302// package_extract_file("boot.img.p"))
303Value* PatchPartitionFn(const char* name, State* state,
304 const std::vector<std::unique_ptr<Expr>>& argv) {
305 if (argv.size() != 3) {
306 return ErrorAbort(state, kArgsParsingFailure,
307 "%s(): Invalid number of args (expected 3, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800308 }
309
310 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700311 if (!ReadArgs(state, argv, &args, 0, 2)) {
312 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800313 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800314
Tao Bao5609bc82018-06-20 00:30:48 -0700315 std::string err;
316 auto target = Partition::Parse(args[0], &err);
317 if (!target) {
318 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
319 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800320 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800321
Tao Bao5609bc82018-06-20 00:30:48 -0700322 auto source = Partition::Parse(args[1], &err);
323 if (!source) {
324 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
325 args[1].c_str(), err.c_str());
326 }
327
328 std::vector<std::unique_ptr<Value>> values;
329 if (!ReadValueArgs(state, argv, &values, 2, 1) || values[0]->type != Value::Type::BLOB) {
330 return ErrorAbort(state, kArgsParsingFailure, "%s(): Invalid patch arg", name);
331 }
332
333 bool result = PatchPartition(target, source, *values[0], nullptr);
334 return StringValue(result ? "t" : "");
Tianjie Xu5419ad32018-02-02 16:49:15 -0800335}
336
Doug Zongker3d177d02010-07-01 09:18:44 -0700337// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -0700338// mount(fs_type, partition_type, location, mount_point, mount_options)
Tianjie Xuc4447322017-03-06 14:44:59 -0800339
Doug Zongker3d177d02010-07-01 09:18:44 -0700340// fs_type="ext4" partition_type="EMMC" location=device
Tianjie Xuc4447322017-03-06 14:44:59 -0800341Value* MountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
342 if (argv.size() != 4 && argv.size() != 5) {
343 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %zu", name,
344 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800345 }
346
347 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800348 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800349 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
350 }
351 const std::string& fs_type = args[0];
352 const std::string& partition_type = args[1];
353 const std::string& location = args[2];
354 const std::string& mount_point = args[3];
355 std::string mount_options;
356
Tianjie Xuc4447322017-03-06 14:44:59 -0800357 if (argv.size() == 5) {
Tao Bao039f2da2016-11-22 16:29:50 -0800358 mount_options = args[4];
359 }
360
361 if (fs_type.empty()) {
362 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
363 }
364 if (partition_type.empty()) {
365 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
366 name);
367 }
368 if (location.empty()) {
369 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
370 }
371 if (mount_point.empty()) {
372 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
373 name);
374 }
375
376 {
377 char* secontext = nullptr;
378
379 if (sehandle) {
380 selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755);
381 setfscreatecon(secontext);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700382 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700383
Tao Bao039f2da2016-11-22 16:29:50 -0800384 mkdir(mount_point.c_str(), 0755);
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700385
Tao Bao039f2da2016-11-22 16:29:50 -0800386 if (secontext) {
387 freecon(secontext);
388 setfscreatecon(nullptr);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700389 }
Tao Bao039f2da2016-11-22 16:29:50 -0800390 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700391
Tao Bao039f2da2016-11-22 16:29:50 -0800392 if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(),
393 MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700394 uiPrintf(state, "%s: Failed to mount %s at %s: %s", name, location.c_str(), mount_point.c_str(),
395 strerror(errno));
Tao Bao039f2da2016-11-22 16:29:50 -0800396 return StringValue("");
397 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700398
Tao Bao039f2da2016-11-22 16:29:50 -0800399 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700400}
401
Doug Zongker8edb00c2009-06-11 17:21:44 -0700402// is_mounted(mount_point)
Tianjie Xuc4447322017-03-06 14:44:59 -0800403Value* IsMountedFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
404 if (argv.size() != 1) {
405 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800406 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700407
Tao Bao039f2da2016-11-22 16:29:50 -0800408 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800409 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800410 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
411 }
412 const std::string& mount_point = args[0];
413 if (mount_point.empty()) {
414 return ErrorAbort(state, kArgsParsingFailure,
415 "mount_point argument to unmount() can't be empty");
416 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700417
Tao Bao039f2da2016-11-22 16:29:50 -0800418 scan_mounted_volumes();
419 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
420 if (vol == nullptr) {
421 return StringValue("");
422 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700423
Tao Bao039f2da2016-11-22 16:29:50 -0800424 return StringValue(mount_point);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700425}
426
Tianjie Xuc4447322017-03-06 14:44:59 -0800427Value* UnmountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
428 if (argv.size() != 1) {
429 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800430 }
431 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800432 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800433 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
434 }
435 const std::string& mount_point = args[0];
436 if (mount_point.empty()) {
437 return ErrorAbort(state, kArgsParsingFailure,
438 "mount_point argument to unmount() can't be empty");
439 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700440
Tao Bao039f2da2016-11-22 16:29:50 -0800441 scan_mounted_volumes();
442 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
443 if (vol == nullptr) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700444 uiPrintf(state, "Failed to unmount %s: No such volume", mount_point.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -0800445 return nullptr;
446 } else {
447 int ret = unmount_mounted_volume(vol);
448 if (ret != 0) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700449 uiPrintf(state, "Failed to unmount %s: %s", mount_point.c_str(), strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700450 }
Tao Bao039f2da2016-11-22 16:29:50 -0800451 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700452
Tao Bao039f2da2016-11-22 16:29:50 -0800453 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700454}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700455
Tao Bao3d69f0d2018-12-20 09:44:06 -0800456static int exec_cmd(const std::vector<std::string>& args) {
457 CHECK(!args.empty());
458 auto argv = StringVectorToNullTerminatedArray(args);
459
Tao Bao039f2da2016-11-22 16:29:50 -0800460 pid_t child;
461 if ((child = vfork()) == 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800462 execv(argv[0], argv.data());
Tao Bao3da88012017-02-03 13:09:23 -0800463 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -0800464 }
465
466 int status;
467 waitpid(child, &status, 0);
468 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800469 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
Tao Bao039f2da2016-11-22 16:29:50 -0800470 }
471 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700472}
473
Stephen Smalley779701d2012-02-09 14:13:23 -0500474// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700475//
Tao Bao039f2da2016-11-22 16:29:50 -0800476// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
477// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700478// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800479// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700480// 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 -0800481Value* FormatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
482 if (argv.size() != 5) {
483 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %zu", name,
484 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800485 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500486
Tao Bao039f2da2016-11-22 16:29:50 -0800487 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800488 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800489 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
490 }
491 const std::string& fs_type = args[0];
492 const std::string& partition_type = args[1];
493 const std::string& location = args[2];
494 const std::string& fs_size = args[3];
495 const std::string& mount_point = args[4];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700496
Tao Bao039f2da2016-11-22 16:29:50 -0800497 if (fs_type.empty()) {
498 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
499 }
500 if (partition_type.empty()) {
501 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
502 name);
503 }
504 if (location.empty()) {
505 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
506 }
507 if (mount_point.empty()) {
508 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
509 name);
510 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700511
Tao Bao039f2da2016-11-22 16:29:50 -0800512 int64_t size;
513 if (!android::base::ParseInt(fs_size, &size)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800514 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800515 fs_size.c_str());
516 }
517
518 if (fs_type == "ext4") {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800519 std::vector<std::string> mke2fs_args = {
520 "/system/bin/mke2fs", "-t", "ext4", "-b", "4096", location
521 };
Jin Qianded2dac2017-04-21 14:36:12 -0700522 if (size != 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800523 mke2fs_args.push_back(std::to_string(size / 4096LL));
Jin Qianded2dac2017-04-21 14:36:12 -0700524 }
525
Tao Bao3d69f0d2018-12-20 09:44:06 -0800526 if (auto status = exec_cmd(mke2fs_args); status != 0) {
Jin Qian502fd1c2017-11-02 11:58:12 -0700527 LOG(ERROR) << name << ": mke2fs failed (" << status << ") on " << location;
528 return StringValue("");
Jin Qianded2dac2017-04-21 14:36:12 -0700529 }
530
Tao Bao3d69f0d2018-12-20 09:44:06 -0800531 if (auto status = exec_cmd({ "/system/bin/e2fsdroid", "-e", "-a", mount_point, location });
532 status != 0) {
Jin Qianded2dac2017-04-21 14:36:12 -0700533 LOG(ERROR) << name << ": e2fsdroid failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800534 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700535 }
Tao Bao039f2da2016-11-22 16:29:50 -0800536 return StringValue(location);
Tao Bao3d69f0d2018-12-20 09:44:06 -0800537 }
538
539 if (fs_type == "f2fs") {
Tao Bao039f2da2016-11-22 16:29:50 -0800540 if (size < 0) {
541 LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size;
542 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700543 }
Tao Bao3d69f0d2018-12-20 09:44:06 -0800544 std::vector<std::string> f2fs_args = {
Tao Baoc674dfb2018-12-20 14:25:15 -0800545 "/system/bin/make_f2fs", "-g", "android", "-w", "512", location
Tao Bao3d69f0d2018-12-20 09:44:06 -0800546 };
547 if (size >= 512) {
548 f2fs_args.push_back(std::to_string(size / 512));
549 }
550 if (auto status = exec_cmd(f2fs_args); status != 0) {
Tao Baoc674dfb2018-12-20 14:25:15 -0800551 LOG(ERROR) << name << ": make_f2fs failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800552 return StringValue("");
553 }
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800554
Tao Baoc674dfb2018-12-20 14:25:15 -0800555 if (auto status = exec_cmd({ "/system/bin/sload_f2fs", "-t", mount_point, location });
556 status != 0) {
557 LOG(ERROR) << name << ": sload_f2fs failed (" << status << ") on " << location;
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800558 return StringValue("");
559 }
560
Tao Bao039f2da2016-11-22 16:29:50 -0800561 return StringValue(location);
Tao Bao039f2da2016-11-22 16:29:50 -0800562 }
563
Tao Bao3d69f0d2018-12-20 09:44:06 -0800564 LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \""
565 << partition_type << "\"";
Tao Bao039f2da2016-11-22 16:29:50 -0800566 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700567}
568
Michael Bestas847c0652019-09-19 21:42:12 +0300569// rename(src_name, dst_name)
570// Renames src_name to dst_name. It automatically creates the necessary directories for dst_name.
571// Example: rename("system/app/Hangouts/Hangouts.apk", "system/priv-app/Hangouts/Hangouts.apk")
572Value* RenameFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
573 if (argv.size() != 2) {
574 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
575 argv.size());
576 }
577
578 std::vector<std::string> args;
579 if (!ReadArgs(state, argv, &args)) {
580 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
581 }
582 const std::string& src_name = args[0];
583 const std::string& dst_name = args[1];
584
585 if (src_name.empty()) {
586 return ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", name);
587 }
588 if (dst_name.empty()) {
589 return ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", name);
590 }
591 if (!make_parents(dst_name)) {
592 return ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
593 dst_name.c_str(), strerror(errno));
594 } else if (access(dst_name.c_str(), F_OK) == 0 && access(src_name.c_str(), F_OK) != 0) {
595 // File was already moved
596 return StringValue(dst_name);
597 } else if (rename(src_name.c_str(), dst_name.c_str()) != 0) {
598 return ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
599 src_name.c_str(), dst_name.c_str(), strerror(errno));
600 }
601
602 return StringValue(dst_name);
603}
604
605// delete([filename, ...])
606// Deletes all the filenames listed. Returns the number of files successfully deleted.
607//
608// delete_recursive([dirname, ...])
609// Recursively deletes dirnames and all their contents. Returns the number of directories
610// successfully deleted.
611Value* DeleteFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
612 std::vector<std::string> paths;
613 if (!ReadArgs(state, argv, &paths)) {
614 return nullptr;
615 }
616
617 bool recursive = (strcmp(name, "delete_recursive") == 0);
618
619 int success = 0;
620 for (const auto& path : paths) {
621 if ((recursive ? dirUnlinkHierarchy(path.c_str()) : unlink(path.c_str())) == 0) {
622 ++success;
623 }
624 }
625
626 return StringValue(std::to_string(success));
627}
628
Tianjie Xuc4447322017-03-06 14:44:59 -0800629Value* ShowProgressFn(const char* name, State* state,
630 const std::vector<std::unique_ptr<Expr>>& argv) {
631 if (argv.size() != 2) {
632 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
633 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800634 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700635
Tao Bao039f2da2016-11-22 16:29:50 -0800636 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800637 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800638 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
639 }
640 const std::string& frac_str = args[0];
641 const std::string& sec_str = args[1];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700642
Tao Bao039f2da2016-11-22 16:29:50 -0800643 double frac;
644 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800645 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800646 frac_str.c_str());
647 }
648 int sec;
649 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800650 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800651 sec_str.c_str());
652 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700653
Tao Bao039f2da2016-11-22 16:29:50 -0800654 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
655 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700656
Tao Bao039f2da2016-11-22 16:29:50 -0800657 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700658}
659
Tianjie Xu5419ad32018-02-02 16:49:15 -0800660Value* SetProgressFn(const char* name, State* state,
661 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800662 if (argv.size() != 1) {
663 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800664 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700665
Tao Bao039f2da2016-11-22 16:29:50 -0800666 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800667 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800668 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
669 }
670 const std::string& frac_str = args[0];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700671
Tao Bao039f2da2016-11-22 16:29:50 -0800672 double frac;
673 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800674 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800675 frac_str.c_str());
676 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700677
Tao Bao039f2da2016-11-22 16:29:50 -0800678 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
679 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700680
Tao Bao039f2da2016-11-22 16:29:50 -0800681 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700682}
683
Michael Bestas847c0652019-09-19 21:42:12 +0300684// symlink(target, [src1, src2, ...])
685// Creates all sources as symlinks to target. It unlinks any previously existing src1, src2, etc
686// before creating symlinks.
687Value* SymlinkFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
688 if (argv.size() == 0) {
689 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %zu", name,
690 argv.size());
691 }
692
693 std::vector<std::string> args;
694 if (!ReadArgs(state, argv, &args)) {
695 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
696 }
697
698 const auto& target = args[0];
699 if (target.empty()) {
700 return ErrorAbort(state, kArgsParsingFailure, "%s() target argument can't be empty", name);
701 }
702
703 size_t bad = 0;
704 for (size_t i = 1; i < args.size(); ++i) {
705 const auto& src = args[i];
706 if (unlink(src.c_str()) == -1 && errno != ENOENT) {
707 PLOG(ERROR) << name << ": failed to remove " << src;
708 ++bad;
709 } else if (!make_parents(src)) {
710 LOG(ERROR) << name << ": failed to symlink " << src << " to " << target
711 << ": making parents failed";
712 ++bad;
713 } else if (symlink(target.c_str(), src.c_str()) == -1) {
714 PLOG(ERROR) << name << ": failed to symlink " << src << " to " << target;
715 ++bad;
716 }
717 }
718 if (bad != 0) {
719 return ErrorAbort(state, kSymlinkFailure, "%s: Failed to create %zu symlink(s)", name, bad);
720 }
721 return StringValue("t");
722}
723
724struct perm_parsed_args {
725 bool has_uid;
726 uid_t uid;
727 bool has_gid;
728 gid_t gid;
729 bool has_mode;
730 mode_t mode;
731 bool has_fmode;
732 mode_t fmode;
733 bool has_dmode;
734 mode_t dmode;
735 bool has_selabel;
736 const char* selabel;
737 bool has_capabilities;
738 uint64_t capabilities;
739};
740
741static struct perm_parsed_args ParsePermArgs(State* state, const std::vector<std::string>& args) {
742 struct perm_parsed_args parsed;
743 int bad = 0;
744 static int max_warnings = 20;
745
746 memset(&parsed, 0, sizeof(parsed));
747
748 for (size_t i = 1; i < args.size(); i += 2) {
749 if (args[i] == "uid") {
750 int64_t uid;
751 if (sscanf(args[i + 1].c_str(), "%" SCNd64, &uid) == 1) {
752 parsed.uid = uid;
753 parsed.has_uid = true;
754 } else {
755 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1].c_str());
756 bad++;
757 }
758 continue;
759 }
760 if (args[i] == "gid") {
761 int64_t gid;
762 if (sscanf(args[i + 1].c_str(), "%" SCNd64, &gid) == 1) {
763 parsed.gid = gid;
764 parsed.has_gid = true;
765 } else {
766 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1].c_str());
767 bad++;
768 }
769 continue;
770 }
771 if (args[i] == "mode") {
772 int32_t mode;
773 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
774 parsed.mode = mode;
775 parsed.has_mode = true;
776 } else {
777 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1].c_str());
778 bad++;
779 }
780 continue;
781 }
782 if (args[i] == "dmode") {
783 int32_t mode;
784 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
785 parsed.dmode = mode;
786 parsed.has_dmode = true;
787 } else {
788 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1].c_str());
789 bad++;
790 }
791 continue;
792 }
793 if (args[i] == "fmode") {
794 int32_t mode;
795 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
796 parsed.fmode = mode;
797 parsed.has_fmode = true;
798 } else {
799 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1].c_str());
800 bad++;
801 }
802 continue;
803 }
804 if (args[i] == "capabilities") {
805 int64_t capabilities;
806 if (sscanf(args[i + 1].c_str(), "%" SCNi64, &capabilities) == 1) {
807 parsed.capabilities = capabilities;
808 parsed.has_capabilities = true;
809 } else {
810 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1].c_str());
811 bad++;
812 }
813 continue;
814 }
815 if (args[i] == "selabel") {
816 if (!args[i + 1].empty()) {
817 parsed.selabel = args[i + 1].c_str();
818 parsed.has_selabel = true;
819 } else {
820 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1].c_str());
821 bad++;
822 }
823 continue;
824 }
825 if (max_warnings != 0) {
826 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i].c_str());
827 max_warnings--;
828 if (max_warnings == 0) {
829 LOG(INFO) << "ParsedPermArgs: suppressing further warnings";
830 }
831 }
832 }
833 return parsed;
834}
835
836static int ApplyParsedPerms(State* state, const char* filename, const struct stat* statptr,
837 struct perm_parsed_args parsed) {
838 int bad = 0;
839
840 if (parsed.has_selabel) {
841 if (lsetfilecon(filename, parsed.selabel) != 0) {
842 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n", filename,
843 parsed.selabel, strerror(errno));
844 bad++;
845 }
846 }
847
848 /* ignore symlinks */
849 if (S_ISLNK(statptr->st_mode)) {
850 return bad;
851 }
852
853 if (parsed.has_uid) {
854 if (chown(filename, parsed.uid, -1) < 0) {
855 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n", filename, parsed.uid,
856 strerror(errno));
857 bad++;
858 }
859 }
860
861 if (parsed.has_gid) {
862 if (chown(filename, -1, parsed.gid) < 0) {
863 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n", filename, parsed.gid,
864 strerror(errno));
865 bad++;
866 }
867 }
868
869 if (parsed.has_mode) {
870 if (chmod(filename, parsed.mode) < 0) {
871 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.mode,
872 strerror(errno));
873 bad++;
874 }
875 }
876
877 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
878 if (chmod(filename, parsed.dmode) < 0) {
879 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.dmode,
880 strerror(errno));
881 bad++;
882 }
883 }
884
885 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
886 if (chmod(filename, parsed.fmode) < 0) {
887 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.fmode,
888 strerror(errno));
889 bad++;
890 }
891 }
892
893 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
894 if (parsed.capabilities == 0) {
895 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
896 // Report failure unless it's ENODATA (attribute not set)
897 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n", filename,
898 parsed.capabilities, strerror(errno));
899 bad++;
900 }
901 } else {
902 struct vfs_cap_data cap_data;
903 memset(&cap_data, 0, sizeof(cap_data));
XiNGRZ9de4bbb2019-12-03 14:31:56 +0800904 cap_data.magic_etc = VFS_CAP_REVISION_2 | VFS_CAP_FLAGS_EFFECTIVE;
Michael Bestas847c0652019-09-19 21:42:12 +0300905 cap_data.data[0].permitted = (uint32_t)(parsed.capabilities & 0xffffffff);
906 cap_data.data[0].inheritable = 0;
907 cap_data.data[1].permitted = (uint32_t)(parsed.capabilities >> 32);
908 cap_data.data[1].inheritable = 0;
909 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
910 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n", filename,
911 parsed.capabilities, strerror(errno));
912 bad++;
913 }
914 }
915 }
916
917 return bad;
918}
919
920// nftw doesn't allow us to pass along context, so we need to use
921// global variables. *sigh*
922static struct perm_parsed_args recursive_parsed_args;
923static State* recursive_state;
924
925static int do_SetMetadataRecursive(const char* filename, const struct stat* statptr,
926 int /*fileflags*/, struct FTW* /*pfwt*/) {
927 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
928}
929
930static Value* SetMetadataFn(const char* name, State* state,
931 const std::vector<std::unique_ptr<Expr>>& argv) {
932 if ((argv.size() % 2) != 1) {
933 return ErrorAbort(state, kArgsParsingFailure,
934 "%s() expects an odd number of arguments, got %zu", name, argv.size());
935 }
936
937 std::vector<std::string> args;
938 if (!ReadArgs(state, argv, &args)) {
939 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
940 }
941
942 struct stat sb;
943 if (lstat(args[0].c_str(), &sb) == -1) {
944 return ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s", name,
945 args[0].c_str(), strerror(errno));
946 }
947
948 struct perm_parsed_args parsed = ParsePermArgs(state, args);
949 int bad = 0;
950 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
951
952 if (recursive) {
953 recursive_parsed_args = parsed;
954 recursive_state = state;
955 bad += nftw(args[0].c_str(), do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
956 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
957 recursive_state = NULL;
958 } else {
959 bad += ApplyParsedPerms(state, args[0].c_str(), &sb, parsed);
960 }
961
962 if (bad > 0) {
963 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
964 }
965
966 return StringValue("");
967}
968
Tianjie Xuc4447322017-03-06 14:44:59 -0800969Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
970 if (argv.size() != 1) {
971 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800972 }
973 std::string key;
974 if (!Evaluate(state, argv[0], &key)) {
975 return nullptr;
976 }
977 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700978
Tao Bao039f2da2016-11-22 16:29:50 -0800979 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700980}
981
Doug Zongker47cace92009-06-18 10:11:50 -0700982// file_getprop(file, key)
983//
984// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -0700985// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700986// and returns the value for 'key' (or "" if it isn't defined).
Tianjie Xu5419ad32018-02-02 16:49:15 -0800987Value* FileGetPropFn(const char* name, State* state,
988 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800989 if (argv.size() != 2) {
990 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
991 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -0800992 }
993
994 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800995 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800996 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
997 }
998 const std::string& filename = args[0];
999 const std::string& key = args[1];
1000
Tianjie Xu22f11202018-08-27 10:50:31 -07001001 std::string buffer;
1002 if (!android::base::ReadFileToString(filename, &buffer)) {
1003 ErrorAbort(state, kFreadFailure, "%s: failed to read %s", name, filename.c_str());
Tao Bao358c2ec2016-11-28 11:48:43 -08001004 return nullptr;
1005 }
1006
Tao Bao358c2ec2016-11-28 11:48:43 -08001007 std::vector<std::string> lines = android::base::Split(buffer, "\n");
1008 for (size_t i = 0; i < lines.size(); i++) {
1009 std::string line = android::base::Trim(lines[i]);
1010
1011 // comment or blank line: skip to next line
1012 if (line.empty() || line[0] == '#') {
1013 continue;
1014 }
1015 size_t equal_pos = line.find('=');
1016 if (equal_pos == std::string::npos) {
1017 continue;
Tao Bao51d516e2016-11-03 14:49:01 -07001018 }
1019
Tao Bao358c2ec2016-11-28 11:48:43 -08001020 // trim whitespace between key and '='
1021 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -07001022
Tao Bao358c2ec2016-11-28 11:48:43 -08001023 // not the key we're looking for
1024 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001025
Tao Bao358c2ec2016-11-28 11:48:43 -08001026 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
1027 }
Doug Zongker47cace92009-06-18 10:11:50 -07001028
Tao Bao358c2ec2016-11-28 11:48:43 -08001029 return StringValue("");
Doug Zongker47cace92009-06-18 10:11:50 -07001030}
1031
Doug Zongker8edb00c2009-06-11 17:21:44 -07001032// apply_patch_space(bytes)
Tianjie Xu5419ad32018-02-02 16:49:15 -08001033Value* ApplyPatchSpaceFn(const char* name, State* state,
1034 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -08001035 if (argv.size() != 1) {
1036 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name,
1037 argv.size());
1038 }
Tao Bao039f2da2016-11-22 16:29:50 -08001039 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001040 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001041 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1042 }
1043 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -08001044
Tao Bao039f2da2016-11-22 16:29:50 -08001045 size_t bytes;
1046 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -08001047 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name,
1048 bytes_str.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -08001049 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001050
Tianjie Xu99b73be2017-11-28 17:23:06 -08001051 // Skip the cache size check if the update is a retry.
Tao Bao5ee25662018-07-11 15:55:32 -07001052 if (state->is_retry || CheckAndFreeSpaceOnCache(bytes)) {
Tianjie Xu99b73be2017-11-28 17:23:06 -08001053 return StringValue("t");
1054 }
1055 return StringValue("");
Doug Zongkerc4351c72010-02-22 14:46:32 -08001056}
1057
Tianjie Xuc4447322017-03-06 14:44:59 -08001058Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1059 if (!argv.empty()) {
1060 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
1061 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001062 }
1063 fprintf(static_cast<UpdaterInfo*>(state->cookie)->cmd_pipe, "wipe_cache\n");
1064 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -07001065}
1066
Tianjie Xuc4447322017-03-06 14:44:59 -08001067Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1068 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -08001069 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
1070 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001071
Tao Bao039f2da2016-11-22 16:29:50 -08001072 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001073 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001074 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1075 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001076
Tao Bao3d69f0d2018-12-20 09:44:06 -08001077 auto exec_args = StringVectorToNullTerminatedArray(args);
1078 LOG(INFO) << "about to run program [" << exec_args[0] << "] with " << argv.size() << " args";
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001079
Tao Bao039f2da2016-11-22 16:29:50 -08001080 pid_t child = fork();
1081 if (child == 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -08001082 execv(exec_args[0], exec_args.data());
Tao Bao039f2da2016-11-22 16:29:50 -08001083 PLOG(ERROR) << "run_program: execv failed";
Tao Bao3da88012017-02-03 13:09:23 -08001084 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -08001085 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001086
Tao Bao039f2da2016-11-22 16:29:50 -08001087 int status;
1088 waitpid(child, &status, 0);
1089 if (WIFEXITED(status)) {
1090 if (WEXITSTATUS(status) != 0) {
1091 LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status);
1092 }
1093 } else if (WIFSIGNALED(status)) {
1094 LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status);
1095 }
1096
1097 return StringValue(std::to_string(status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001098}
1099
Tao Bao511d7592018-06-19 15:56:49 -07001100// read_file(filename)
Tao Baobafd6c72018-07-09 15:08:50 -07001101// Reads a local file 'filename' and returns its contents as a string Value.
Tianjie Xuc4447322017-03-06 14:44:59 -08001102Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1103 if (argv.size() != 1) {
1104 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001105 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001106
Tao Bao039f2da2016-11-22 16:29:50 -08001107 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001108 if (!ReadArgs(state, argv, &args)) {
Tao Bao511d7592018-06-19 15:56:49 -07001109 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tao Bao039f2da2016-11-22 16:29:50 -08001110 }
1111 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -08001112
Tao Baobafd6c72018-07-09 15:08:50 -07001113 std::string contents;
1114 if (android::base::ReadFileToString(filename, &contents)) {
1115 return new Value(Value::Type::STRING, std::move(contents));
Tao Bao039f2da2016-11-22 16:29:50 -08001116 }
Tao Bao511d7592018-06-19 15:56:49 -07001117
1118 // Leave it to caller to handle the failure.
Tao Baobafd6c72018-07-09 15:08:50 -07001119 PLOG(ERROR) << name << ": Failed to read " << filename;
Tao Bao511d7592018-06-19 15:56:49 -07001120 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -08001121}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001122
Tao Baod0f30882016-11-03 23:52:01 -07001123// write_value(value, filename)
1124// Writes 'value' to 'filename'.
1125// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
Tianjie Xuc4447322017-03-06 14:44:59 -08001126Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1127 if (argv.size() != 2) {
1128 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1129 argv.size());
Tao Baod0f30882016-11-03 23:52:01 -07001130 }
1131
1132 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001133 if (!ReadArgs(state, argv, &args)) {
Tao Baod0f30882016-11-03 23:52:01 -07001134 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1135 }
1136
1137 const std::string& filename = args[1];
1138 if (filename.empty()) {
1139 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
1140 }
1141
1142 const std::string& value = args[0];
1143 if (!android::base::WriteStringToFile(value, filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001144 PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\"";
Tao Baod0f30882016-11-03 23:52:01 -07001145 return StringValue("");
1146 } else {
1147 return StringValue("t");
1148 }
1149}
1150
Doug Zongkerc87bab12013-11-25 13:53:25 -08001151// Immediately reboot the device. Recovery is not finished normally,
1152// so if you reboot into recovery it will re-start applying the
1153// current package (because nothing has cleared the copy of the
1154// arguments stored in the BCB).
1155//
1156// The argument is the partition name passed to the android reboot
1157// property. It can be "recovery" to boot from the recovery
1158// partition, or "" (empty string) to boot from the regular boot
1159// partition.
Tianjie Xuc4447322017-03-06 14:44:59 -08001160Value* RebootNowFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1161 if (argv.size() != 2) {
1162 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1163 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001164 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001165
Tao Baobedf5fc2016-11-18 12:01:26 -08001166 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001167 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001168 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1169 }
1170 const std::string& filename = args[0];
1171 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001172
Tao Baobedf5fc2016-11-18 12:01:26 -08001173 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
1174 bootloader_message boot;
1175 std::string err;
1176 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001177 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001178 return StringValue("");
1179 }
1180 memset(boot.command, 0, sizeof(boot.command));
1181 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001182 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001183 return StringValue("");
1184 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001185
Tao Bao2c526392018-05-03 23:01:13 -07001186 reboot("reboot," + property);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001187
Tao Baobedf5fc2016-11-18 12:01:26 -08001188 sleep(5);
1189 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001190}
1191
1192// Store a string value somewhere that future invocations of recovery
1193// can access it. This value is called the "stage" and can be used to
1194// drive packages that need to do reboots in the middle of
1195// installation and keep track of where they are in the multi-stage
1196// install.
1197//
1198// The first argument is the block device for the misc partition
1199// ("/misc" in the fstab), which is where this value is stored. The
1200// second argument is the string to store; it should not exceed 31
1201// bytes.
Tianjie Xuc4447322017-03-06 14:44:59 -08001202Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1203 if (argv.size() != 2) {
1204 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1205 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001206 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001207
Tao Baobedf5fc2016-11-18 12:01:26 -08001208 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001209 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001210 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1211 }
1212 const std::string& filename = args[0];
1213 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001214
Tao Baobedf5fc2016-11-18 12:01:26 -08001215 // Store this value in the misc partition, immediately after the
1216 // bootloader message that the main recovery uses to save its
1217 // arguments in case of the device restarting midway through
1218 // package installation.
1219 bootloader_message boot;
1220 std::string err;
1221 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001222 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001223 return StringValue("");
1224 }
1225 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
1226 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001227 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001228 return StringValue("");
1229 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001230
Tao Baobedf5fc2016-11-18 12:01:26 -08001231 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001232}
1233
1234// Return the value most recently saved with SetStageFn. The argument
1235// is the block device for the misc partition.
Tianjie Xuc4447322017-03-06 14:44:59 -08001236Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1237 if (argv.size() != 1) {
1238 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001239 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001240
Tao Baobedf5fc2016-11-18 12:01:26 -08001241 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001242 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001243 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1244 }
1245 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001246
Tao Baobedf5fc2016-11-18 12:01:26 -08001247 bootloader_message boot;
1248 std::string err;
1249 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001250 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001251 return StringValue("");
1252 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001253
Tao Baobedf5fc2016-11-18 12:01:26 -08001254 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001255}
1256
Tianjie Xuc4447322017-03-06 14:44:59 -08001257Value* WipeBlockDeviceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1258 if (argv.size() != 2) {
1259 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1260 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -08001261 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001262
Tao Bao358c2ec2016-11-28 11:48:43 -08001263 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001264 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -08001265 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1266 }
1267 const std::string& filename = args[0];
1268 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001269
Tao Bao358c2ec2016-11-28 11:48:43 -08001270 size_t len;
1271 if (!android::base::ParseUint(len_str.c_str(), &len)) {
1272 return nullptr;
1273 }
Tianjie Xu22f11202018-08-27 10:50:31 -07001274 android::base::unique_fd fd(open(filename.c_str(), O_WRONLY));
1275 if (fd == -1) {
1276 PLOG(ERROR) << "Failed to open " << filename;
1277 return StringValue("");
1278 }
1279
Tao Bao358c2ec2016-11-28 11:48:43 -08001280 // The wipe_block_device function in ext4_utils returns 0 on success and 1
1281 // for failure.
1282 int status = wipe_block_device(fd, len);
1283 return StringValue((status == 0) ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001284}
1285
Tianjie Xuc4447322017-03-06 14:44:59 -08001286Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1287 if (!argv.empty()) {
1288 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
1289 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001290 }
1291 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
1292 fprintf(ui->cmd_pipe, "enable_reboot\n");
1293 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -07001294}
1295
Tianjie Xuc4447322017-03-06 14:44:59 -08001296Value* Tune2FsFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1297 if (argv.empty()) {
1298 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001299 }
Michael Rungeacf47db2014-11-21 00:12:28 -08001300
Tao Bao039f2da2016-11-22 16:29:50 -08001301 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001302 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001303 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
1304 }
Michael Rungeacf47db2014-11-21 00:12:28 -08001305
Tao Bao3d69f0d2018-12-20 09:44:06 -08001306 // tune2fs expects the program name as its first arg.
1307 args.insert(args.begin(), "tune2fs");
1308 auto tune2fs_args = StringVectorToNullTerminatedArray(args);
Michael Rungeacf47db2014-11-21 00:12:28 -08001309
Tao Bao3d69f0d2018-12-20 09:44:06 -08001310 // tune2fs changes the filesystem parameters on an ext2 filesystem; it returns 0 on success.
1311 if (auto result = tune2fs_main(tune2fs_args.size() - 1, tune2fs_args.data()); result != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -08001312 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
1313 }
1314 return StringValue("t");
Michael Rungeacf47db2014-11-21 00:12:28 -08001315}
1316
Doug Zongker9931f7f2009-06-10 14:11:53 -07001317void RegisterInstallFunctions() {
Tao Bao039f2da2016-11-22 16:29:50 -08001318 RegisterFunction("mount", MountFn);
1319 RegisterFunction("is_mounted", IsMountedFn);
1320 RegisterFunction("unmount", UnmountFn);
1321 RegisterFunction("format", FormatFn);
1322 RegisterFunction("show_progress", ShowProgressFn);
1323 RegisterFunction("set_progress", SetProgressFn);
Michael Bestas847c0652019-09-19 21:42:12 +03001324 RegisterFunction("delete", DeleteFn);
1325 RegisterFunction("delete_recursive", DeleteFn);
1326 RegisterFunction("package_extract_dir", PackageExtractDirFn);
Tao Bao039f2da2016-11-22 16:29:50 -08001327 RegisterFunction("package_extract_file", PackageExtractFileFn);
Michael Bestas847c0652019-09-19 21:42:12 +03001328 RegisterFunction("symlink", SymlinkFn);
1329
1330 // Usage:
1331 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1332 // Example:
1333 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel",
1334 // "u:object_r:system_file:s0", "capabilities", 0x0);
1335 RegisterFunction("set_metadata", SetMetadataFn);
1336
1337 // Usage:
1338 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1339 // Example:
1340 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755,
1341 // "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1342 RegisterFunction("set_metadata_recursive", SetMetadataFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001343
Tao Bao039f2da2016-11-22 16:29:50 -08001344 RegisterFunction("getprop", GetPropFn);
1345 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001346
Tao Bao039f2da2016-11-22 16:29:50 -08001347 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Tao Bao5609bc82018-06-20 00:30:48 -07001348 RegisterFunction("patch_partition", PatchPartitionFn);
1349 RegisterFunction("patch_partition_check", PatchPartitionCheckFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001350
Tao Bao039f2da2016-11-22 16:29:50 -08001351 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001352
Tao Bao039f2da2016-11-22 16:29:50 -08001353 RegisterFunction("read_file", ReadFileFn);
Michael Bestas847c0652019-09-19 21:42:12 +03001354 RegisterFunction("rename", RenameFn);
Tao Bao039f2da2016-11-22 16:29:50 -08001355 RegisterFunction("write_value", WriteValueFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001356
Tao Bao039f2da2016-11-22 16:29:50 -08001357 RegisterFunction("wipe_cache", WipeCacheFn);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001358
Tao Bao039f2da2016-11-22 16:29:50 -08001359 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001360
Tao Bao039f2da2016-11-22 16:29:50 -08001361 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001362
Tao Bao039f2da2016-11-22 16:29:50 -08001363 RegisterFunction("reboot_now", RebootNowFn);
1364 RegisterFunction("get_stage", GetStageFn);
1365 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001366
Tao Bao039f2da2016-11-22 16:29:50 -08001367 RegisterFunction("enable_reboot", EnableRebootFn);
1368 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001369}