blob: 70092c5b5e1231f8817f05b49902c23fdd6ba8cb [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 Bao1bf17722016-11-03 23:52:01 -070039#include <string>
Yabin Cui64be2132016-02-03 18:16:02 -080040#include <vector>
41
Tao Bao1bf17722016-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>
Elliott Hughes4b166f02015-12-04 15:30:20 -080045#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 Bao039f2da2016-11-22 16:29:50 -080060 // been redirected to the log file). Because the recovery will only print
61 // the contents to screen when processing pipe command ui_print.
62 LOG(INFO) << buffer;
Michael Runge75480252014-10-22 19:48:41 -070063}
64
Simon Shields2e9747f2018-08-09 01:17:21 +100065static bool is_dir(const std::string& dirpath) {
66 struct stat st;
67 return stat(dirpath.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
68}
69
70// Create all parent directories of name, if necessary.
71static bool make_parents(const std::string& name) {
72 size_t prev_end = 0;
73 while (prev_end < name.size()) {
74 size_t next_end = name.find('/', prev_end + 1);
75 if (next_end == std::string::npos) {
76 break;
77 }
78 std::string dir_path = name.substr(0, next_end);
79 if (!is_dir(dir_path)) {
80 int result = mkdir(dir_path.c_str(), 0700);
81 if (result != 0) {
82 PLOG(ERROR) << "failed to mkdir " << dir_path << " when make parents for " << name;
83 return false;
84 }
85
86 LOG(INFO) << "created [" << dir_path << "]";
87 }
88 prev_end = next_end;
89 }
90 return true;
91}
92
Elliott Hughes83ce7552016-06-30 09:28:42 -070093void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) {
Tao Bao039f2da2016-11-22 16:29:50 -080094 std::string error_msg;
Tao Bao1107d962015-09-09 17:16:55 -070095
Tao Bao039f2da2016-11-22 16:29:50 -080096 va_list ap;
97 va_start(ap, format);
98 android::base::StringAppendV(&error_msg, format, ap);
99 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -0700100
Tao Bao039f2da2016-11-22 16:29:50 -0800101 uiPrint(state, error_msg);
Michael Runge75480252014-10-22 19:48:41 -0700102}
103
Tianjie Xu5419ad32018-02-02 16:49:15 -0800104// This is the updater side handler for ui_print() in edify script. Contents will be sent over to
105// the recovery side for on-screen display.
106Value* UIPrintFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
107 std::vector<std::string> args;
108 if (!ReadArgs(state, argv, &args)) {
109 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
110 }
111
112 std::string buffer = android::base::Join(args, "");
113 uiPrint(state, buffer);
114 return StringValue(buffer);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700115}
116
Tianjie Xu5419ad32018-02-02 16:49:15 -0800117// package_extract_file(package_file[, dest_file])
118// Extracts a single package_file from the update package and writes it to dest_file,
119// overwriting existing files if necessary. Without the dest_file argument, returns the
120// contents of the package file as a binary blob.
121Value* PackageExtractFileFn(const char* name, State* state,
122 const std::vector<std::unique_ptr<Expr>>& argv) {
123 if (argv.size() < 1 || argv.size() > 2) {
124 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name,
125 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800126 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800127
128 if (argv.size() == 2) {
129 // The two-argument version extracts to a file.
130
131 std::vector<std::string> args;
132 if (!ReadArgs(state, argv, &args)) {
133 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
134 argv.size());
135 }
136 const std::string& zip_path = args[0];
137 const std::string& dest_path = args[1];
138
139 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
140 ZipString zip_string_path(zip_path.c_str());
141 ZipEntry entry;
142 if (FindEntry(za, zip_string_path, &entry) != 0) {
143 LOG(ERROR) << name << ": no " << zip_path << " in package";
144 return StringValue("");
145 }
146
Tianjie Xu22f11202018-08-27 10:50:31 -0700147 android::base::unique_fd fd(TEMP_FAILURE_RETRY(
148 open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tianjie Xu5419ad32018-02-02 16:49:15 -0800149 if (fd == -1) {
150 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
151 return StringValue("");
152 }
153
154 bool success = true;
155 int32_t ret = ExtractEntryToFile(za, &entry, fd);
156 if (ret != 0) {
157 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
158 << entry.uncompressed_length << " bytes) to \"" << dest_path
159 << "\": " << ErrorCodeString(ret);
160 success = false;
161 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700162 if (fsync(fd) == -1) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800163 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
164 success = false;
165 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700166
167 if (close(fd.release()) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800168 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
169 success = false;
170 }
171
172 return StringValue(success ? "t" : "");
173 } else {
174 // The one-argument version returns the contents of the file as the result.
175
176 std::vector<std::string> args;
177 if (!ReadArgs(state, argv, &args)) {
178 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
179 argv.size());
180 }
181 const std::string& zip_path = args[0];
182
183 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
184 ZipString zip_string_path(zip_path.c_str());
185 ZipEntry entry;
186 if (FindEntry(za, zip_string_path, &entry) != 0) {
187 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
188 zip_path.c_str());
189 }
190
191 std::string buffer;
192 buffer.resize(entry.uncompressed_length);
193
194 int32_t ret =
195 ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
196 if (ret != 0) {
197 return ErrorAbort(state, kPackageExtractFileFailure,
198 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
199 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
200 }
201
Tao Bao511d7592018-06-19 15:56:49 -0700202 return new Value(Value::Type::BLOB, buffer);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800203 }
204}
205
Tao Bao5609bc82018-06-20 00:30:48 -0700206// patch_partition_check(target_partition, source_partition)
207// Checks if the target and source partitions have the desired checksums to be patched. It returns
208// directly, if the target partition already has the expected checksum. Otherwise it in turn
209// checks the integrity of the source partition and the backup file on /cache.
Tianjie Xu5419ad32018-02-02 16:49:15 -0800210//
Tao Bao5609bc82018-06-20 00:30:48 -0700211// For example, patch_partition_check(
212// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
213// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4")
214Value* PatchPartitionCheckFn(const char* name, State* state,
215 const std::vector<std::unique_ptr<Expr>>& argv) {
216 if (argv.size() != 2) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800217 return ErrorAbort(state, kArgsParsingFailure,
Tao Bao5609bc82018-06-20 00:30:48 -0700218 "%s(): Invalid number of args (expected 2, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800219 }
220
221 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700222 if (!ReadArgs(state, argv, &args, 0, 2)) {
223 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800224 }
225
Tao Bao5609bc82018-06-20 00:30:48 -0700226 std::string err;
227 auto target = Partition::Parse(args[0], &err);
228 if (!target) {
229 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
230 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800231 }
232
Tao Bao5609bc82018-06-20 00:30:48 -0700233 auto source = Partition::Parse(args[1], &err);
234 if (!source) {
235 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
236 args[1].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800237 }
238
Tao Bao5609bc82018-06-20 00:30:48 -0700239 bool result = PatchPartitionCheck(target, source);
240 return StringValue(result ? "t" : "");
Tianjie Xu5419ad32018-02-02 16:49:15 -0800241}
242
Tao Bao5609bc82018-06-20 00:30:48 -0700243// patch_partition(target, source, patch)
244// Applies the given patch to the source partition, and writes the result to the target partition.
245//
246// For example, patch_partition(
247// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
248// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4",
249// package_extract_file("boot.img.p"))
250Value* PatchPartitionFn(const char* name, State* state,
251 const std::vector<std::unique_ptr<Expr>>& argv) {
252 if (argv.size() != 3) {
253 return ErrorAbort(state, kArgsParsingFailure,
254 "%s(): Invalid number of args (expected 3, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800255 }
256
257 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700258 if (!ReadArgs(state, argv, &args, 0, 2)) {
259 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800260 }
261
Tao Bao5609bc82018-06-20 00:30:48 -0700262 std::string err;
263 auto target = Partition::Parse(args[0], &err);
264 if (!target) {
265 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
266 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800267 }
268
Tao Bao5609bc82018-06-20 00:30:48 -0700269 auto source = Partition::Parse(args[1], &err);
270 if (!source) {
271 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
272 args[1].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800273 }
274
Tao Bao5609bc82018-06-20 00:30:48 -0700275 std::vector<std::unique_ptr<Value>> values;
276 if (!ReadValueArgs(state, argv, &values, 2, 1) || values[0]->type != Value::Type::BLOB) {
277 return ErrorAbort(state, kArgsParsingFailure, "%s(): Invalid patch arg", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800278 }
279
Tao Bao5609bc82018-06-20 00:30:48 -0700280 bool result = PatchPartition(target, source, *values[0], nullptr);
281 return StringValue(result ? "t" : "");
Doug Zongkera23075f2012-08-06 16:19:09 -0700282}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700283
Doug Zongker9931f7f2009-06-10 14:11:53 -0700284// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -0700285// mount(fs_type, partition_type, location, mount_point, mount_options)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700286
Doug Zongker9931f7f2009-06-10 14:11:53 -0700287// fs_type="ext4" partition_type="EMMC" location=device
Tianjie Xuc4447322017-03-06 14:44:59 -0800288Value* MountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
289 if (argv.size() != 4 && argv.size() != 5) {
290 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %zu", name,
291 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800292 }
293
294 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800295 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800296 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
297 }
298 const std::string& fs_type = args[0];
299 const std::string& partition_type = args[1];
300 const std::string& location = args[2];
301 const std::string& mount_point = args[3];
302 std::string mount_options;
303
Tianjie Xuc4447322017-03-06 14:44:59 -0800304 if (argv.size() == 5) {
Tao Bao039f2da2016-11-22 16:29:50 -0800305 mount_options = args[4];
306 }
307
308 if (fs_type.empty()) {
309 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
310 }
311 if (partition_type.empty()) {
312 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
313 name);
314 }
315 if (location.empty()) {
316 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
317 }
318 if (mount_point.empty()) {
319 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
320 name);
321 }
322
323 {
324 char* secontext = nullptr;
325
326 if (sehandle) {
327 selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755);
328 setfscreatecon(secontext);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700329 }
330
Tao Bao039f2da2016-11-22 16:29:50 -0800331 mkdir(mount_point.c_str(), 0755);
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700332
Tao Bao039f2da2016-11-22 16:29:50 -0800333 if (secontext) {
334 freecon(secontext);
335 setfscreatecon(nullptr);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700336 }
Tao Bao039f2da2016-11-22 16:29:50 -0800337 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700338
Tao Bao039f2da2016-11-22 16:29:50 -0800339 if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(),
340 MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700341 uiPrintf(state, "%s: Failed to mount %s at %s: %s", name, location.c_str(), mount_point.c_str(),
342 strerror(errno));
Tao Bao039f2da2016-11-22 16:29:50 -0800343 return StringValue("");
344 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700345
Tao Bao039f2da2016-11-22 16:29:50 -0800346 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700347}
348
Doug Zongker9931f7f2009-06-10 14:11:53 -0700349// is_mounted(mount_point)
Tianjie Xuc4447322017-03-06 14:44:59 -0800350Value* IsMountedFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
351 if (argv.size() != 1) {
352 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800353 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700354
Tao Bao039f2da2016-11-22 16:29:50 -0800355 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800356 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800357 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
358 }
359 const std::string& mount_point = args[0];
360 if (mount_point.empty()) {
361 return ErrorAbort(state, kArgsParsingFailure,
362 "mount_point argument to unmount() can't be empty");
363 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700364
Tao Bao039f2da2016-11-22 16:29:50 -0800365 scan_mounted_volumes();
366 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
367 if (vol == nullptr) {
368 return StringValue("");
369 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700370
Tao Bao039f2da2016-11-22 16:29:50 -0800371 return StringValue(mount_point);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700372}
373
Tianjie Xuc4447322017-03-06 14:44:59 -0800374Value* UnmountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
375 if (argv.size() != 1) {
376 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800377 }
378 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800379 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800380 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
381 }
382 const std::string& mount_point = args[0];
383 if (mount_point.empty()) {
384 return ErrorAbort(state, kArgsParsingFailure,
385 "mount_point argument to unmount() can't be empty");
386 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700387
Tao Bao039f2da2016-11-22 16:29:50 -0800388 scan_mounted_volumes();
389 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
390 if (vol == nullptr) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700391 uiPrintf(state, "Failed to unmount %s: No such volume", mount_point.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -0800392 return nullptr;
393 } else {
394 int ret = unmount_mounted_volume(vol);
395 if (ret != 0) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700396 uiPrintf(state, "Failed to unmount %s: %s", mount_point.c_str(), strerror(errno));
Nick Kralevich6a821fe2014-10-23 20:36:42 -0700397 }
Tao Bao039f2da2016-11-22 16:29:50 -0800398 }
Nick Kralevich6a821fe2014-10-23 20:36:42 -0700399
Tao Bao039f2da2016-11-22 16:29:50 -0800400 return StringValue(mount_point);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700401}
402
Tao Bao3d69f0d2018-12-20 09:44:06 -0800403static int exec_cmd(const std::vector<std::string>& args) {
404 CHECK(!args.empty());
405 auto argv = StringVectorToNullTerminatedArray(args);
406
Tao Bao039f2da2016-11-22 16:29:50 -0800407 pid_t child;
408 if ((child = vfork()) == 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800409 execv(argv[0], argv.data());
Tao Bao3da88012017-02-03 13:09:23 -0800410 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -0800411 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700412
Tao Bao039f2da2016-11-22 16:29:50 -0800413 int status;
414 waitpid(child, &status, 0);
415 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800416 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
Tao Bao039f2da2016-11-22 16:29:50 -0800417 }
418 return WEXITSTATUS(status);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700419}
420
Doug Zongker9931f7f2009-06-10 14:11:53 -0700421// format(fs_type, partition_type, location, fs_size, mount_point)
422//
Tao Bao039f2da2016-11-22 16:29:50 -0800423// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
424// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700425// if fs_size == 0, then make fs uses the entire partition.
Doug Zongker9931f7f2009-06-10 14:11:53 -0700426// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700427// 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 -0800428Value* FormatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
429 if (argv.size() != 5) {
430 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %zu", name,
431 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800432 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700433
Tao Bao039f2da2016-11-22 16:29:50 -0800434 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800435 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800436 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
437 }
438 const std::string& fs_type = args[0];
439 const std::string& partition_type = args[1];
440 const std::string& location = args[2];
441 const std::string& fs_size = args[3];
442 const std::string& mount_point = args[4];
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700443
Tao Bao039f2da2016-11-22 16:29:50 -0800444 if (fs_type.empty()) {
445 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
446 }
447 if (partition_type.empty()) {
448 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
449 name);
450 }
451 if (location.empty()) {
452 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
453 }
454 if (mount_point.empty()) {
455 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
456 name);
457 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700458
Tao Bao039f2da2016-11-22 16:29:50 -0800459 int64_t size;
460 if (!android::base::ParseInt(fs_size, &size)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800461 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800462 fs_size.c_str());
463 }
464
465 if (fs_type == "ext4") {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800466 std::vector<std::string> mke2fs_args = {
467 "/system/bin/mke2fs", "-t", "ext4", "-b", "4096", location
468 };
Jin Qianac318082017-04-21 14:36:12 -0700469 if (size != 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800470 mke2fs_args.push_back(std::to_string(size / 4096LL));
Jin Qianac318082017-04-21 14:36:12 -0700471 }
472
Tao Bao3d69f0d2018-12-20 09:44:06 -0800473 if (auto status = exec_cmd(mke2fs_args); status != 0) {
Jin Qian502fd1c2017-11-02 11:58:12 -0700474 LOG(ERROR) << name << ": mke2fs failed (" << status << ") on " << location;
475 return StringValue("");
Jin Qianac318082017-04-21 14:36:12 -0700476 }
477
Tao Bao3d69f0d2018-12-20 09:44:06 -0800478 if (auto status = exec_cmd({ "/system/bin/e2fsdroid", "-e", "-a", mount_point, location });
479 status != 0) {
Jin Qianac318082017-04-21 14:36:12 -0700480 LOG(ERROR) << name << ": e2fsdroid failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800481 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700482 }
Tao Bao039f2da2016-11-22 16:29:50 -0800483 return StringValue(location);
Tao Bao3d69f0d2018-12-20 09:44:06 -0800484 }
485
486 if (fs_type == "f2fs") {
Tao Bao039f2da2016-11-22 16:29:50 -0800487 if (size < 0) {
488 LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size;
489 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700490 }
Tao Bao3d69f0d2018-12-20 09:44:06 -0800491 std::vector<std::string> f2fs_args = {
Tao Baoc674dfb2018-12-20 14:25:15 -0800492 "/system/bin/make_f2fs", "-g", "android", "-w", "512", location
Tao Bao3d69f0d2018-12-20 09:44:06 -0800493 };
494 if (size >= 512) {
495 f2fs_args.push_back(std::to_string(size / 512));
496 }
497 if (auto status = exec_cmd(f2fs_args); status != 0) {
Tao Baoc674dfb2018-12-20 14:25:15 -0800498 LOG(ERROR) << name << ": make_f2fs failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800499 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700500 }
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800501
Tao Baoc674dfb2018-12-20 14:25:15 -0800502 if (auto status = exec_cmd({ "/system/bin/sload_f2fs", "-t", mount_point, location });
503 status != 0) {
504 LOG(ERROR) << name << ": sload_f2fs failed (" << status << ") on " << location;
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800505 return StringValue("");
506 }
507
Tao Bao039f2da2016-11-22 16:29:50 -0800508 return StringValue(location);
Tao Bao039f2da2016-11-22 16:29:50 -0800509 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700510
Tao Bao3d69f0d2018-12-20 09:44:06 -0800511 LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \""
512 << partition_type << "\"";
Tao Bao039f2da2016-11-22 16:29:50 -0800513 return nullptr;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700514}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700515
Tom Marshallbf4f24f2017-08-23 18:14:00 +0000516// rename(src_name, dst_name)
517// Renames src_name to dst_name. It automatically creates the necessary directories for dst_name.
518// Example: rename("system/app/Hangouts/Hangouts.apk", "system/priv-app/Hangouts/Hangouts.apk")
519Value* RenameFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
520 if (argv.size() != 2) {
521 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
522 argv.size());
523 }
524
525 std::vector<std::string> args;
526 if (!ReadArgs(state, argv, &args)) {
527 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
528 }
529 const std::string& src_name = args[0];
530 const std::string& dst_name = args[1];
531
532 if (src_name.empty()) {
533 return ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", name);
534 }
535 if (dst_name.empty()) {
536 return ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", name);
537 }
538 if (!make_parents(dst_name)) {
539 return ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
540 dst_name.c_str(), strerror(errno));
541 } else if (access(dst_name.c_str(), F_OK) == 0 && access(src_name.c_str(), F_OK) != 0) {
542 // File was already moved
543 return StringValue(dst_name);
544 } else if (rename(src_name.c_str(), dst_name.c_str()) != 0) {
545 return ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
546 src_name.c_str(), dst_name.c_str(), strerror(errno));
547 }
548
549 return StringValue(dst_name);
550}
551
552// delete([filename, ...])
553// Deletes all the filenames listed. Returns the number of files successfully deleted.
554//
555// delete_recursive([dirname, ...])
556// Recursively deletes dirnames and all their contents. Returns the number of directories
557// successfully deleted.
558Value* DeleteFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
559 std::vector<std::string> paths;
560 if (!ReadArgs(state, argv, &paths)) {
561 return nullptr;
562 }
563
564 bool recursive = (strcmp(name, "delete_recursive") == 0);
565
566 int success = 0;
567 for (const auto& path : paths) {
568 if ((recursive ? dirUnlinkHierarchy(path.c_str()) : unlink(path.c_str())) == 0) {
569 ++success;
570 }
571 }
572
573 return StringValue(std::to_string(success));
574}
575
576
Tianjie Xuc4447322017-03-06 14:44:59 -0800577Value* ShowProgressFn(const char* name, State* state,
578 const std::vector<std::unique_ptr<Expr>>& argv) {
579 if (argv.size() != 2) {
580 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
581 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800582 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700583
Tao Bao039f2da2016-11-22 16:29:50 -0800584 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800585 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800586 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
587 }
588 const std::string& frac_str = args[0];
589 const std::string& sec_str = args[1];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700590
Tao Bao039f2da2016-11-22 16:29:50 -0800591 double frac;
592 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800593 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800594 frac_str.c_str());
595 }
596 int sec;
597 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800598 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800599 sec_str.c_str());
600 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700601
Tao Bao039f2da2016-11-22 16:29:50 -0800602 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
603 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700604
Tao Bao039f2da2016-11-22 16:29:50 -0800605 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700606}
607
Tianjie Xu5419ad32018-02-02 16:49:15 -0800608Value* SetProgressFn(const char* name, State* state,
609 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800610 if (argv.size() != 1) {
611 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, 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];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700619
Tao Bao039f2da2016-11-22 16:29:50 -0800620 double frac;
621 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800622 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800623 frac_str.c_str());
624 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700625
Tao Bao039f2da2016-11-22 16:29:50 -0800626 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
627 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700628
Tao Bao039f2da2016-11-22 16:29:50 -0800629 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700630}
631
Tom Marshall981118e2017-10-25 20:27:08 +0200632// package_extract_dir(package_dir, dest_dir)
633// Extracts all files from the package underneath package_dir and writes them to the
634// corresponding tree beneath dest_dir. Any existing files are overwritten.
635// Example: package_extract_dir("system", "/system")
636//
637// Note: package_dir needs to be a relative path; dest_dir needs to be an absolute path.
638Value* PackageExtractDirFn(const char* name, State* state,
639 const std::vector<std::unique_ptr<Expr>>&argv) {
640 if (argv.size() != 2) {
641 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
642 argv.size());
643 }
644
645 std::vector<std::string> args;
646 if (!ReadArgs(state, argv, &args)) {
647 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
648 }
649 const std::string& zip_path = args[0];
650 const std::string& dest_path = args[1];
651
652 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
653
654 // To create a consistent system image, never use the clock for timestamps.
655 constexpr struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
656
657 bool success = ExtractPackageRecursive(za, zip_path, dest_path, &timestamp, sehandle);
658
659 return StringValue(success ? "t" : "");
660}
661
Tao Baoef0eb3b2016-11-14 21:29:52 -0800662// package_extract_file(package_file[, dest_file])
663// Extracts a single package_file from the update package and writes it to dest_file,
664// overwriting existing files if necessary. Without the dest_file argument, returns the
665// contents of the package file as a binary blob.
Tianjie Xuc4447322017-03-06 14:44:59 -0800666Value* PackageExtractFileFn(const char* name, State* state,
667 const std::vector<std::unique_ptr<Expr>>& argv) {
668 if (argv.size() < 1 || argv.size() > 2) {
669 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name,
670 argv.size());
Tao Baoef0eb3b2016-11-14 21:29:52 -0800671 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700672
Tianjie Xuc4447322017-03-06 14:44:59 -0800673 if (argv.size() == 2) {
Tao Baoef0eb3b2016-11-14 21:29:52 -0800674 // The two-argument version extracts to a file.
675
676 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800677 if (!ReadArgs(state, argv, &args)) {
678 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
679 argv.size());
Doug Zongker8edb00c2009-06-11 17:21:44 -0700680 }
Tao Baoef0eb3b2016-11-14 21:29:52 -0800681 const std::string& zip_path = args[0];
682 const std::string& dest_path = args[1];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700683
Tao Baoef0eb3b2016-11-14 21:29:52 -0800684 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
685 ZipString zip_string_path(zip_path.c_str());
686 ZipEntry entry;
687 if (FindEntry(za, zip_string_path, &entry) != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800688 LOG(ERROR) << name << ": no " << zip_path << " in package";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800689 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700690 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700691
Tao Bao358c2ec2016-11-28 11:48:43 -0800692 unique_fd fd(TEMP_FAILURE_RETRY(
693 ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tao Baoef0eb3b2016-11-14 21:29:52 -0800694 if (fd == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800695 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800696 return StringValue("");
697 }
698
699 bool success = true;
700 int32_t ret = ExtractEntryToFile(za, &entry, fd);
701 if (ret != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800702 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
703 << entry.uncompressed_length << " bytes) to \"" << dest_path
704 << "\": " << ErrorCodeString(ret);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800705 success = false;
706 }
707 if (ota_fsync(fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800708 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800709 success = false;
710 }
711 if (ota_close(fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800712 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800713 success = false;
714 }
715
716 return StringValue(success ? "t" : "");
717 } else {
718 // The one-argument version returns the contents of the file as the result.
719
720 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800721 if (!ReadArgs(state, argv, &args)) {
722 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
723 argv.size());
Tao Baoef0eb3b2016-11-14 21:29:52 -0800724 }
725 const std::string& zip_path = args[0];
726
727 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
728 ZipString zip_string_path(zip_path.c_str());
729 ZipEntry entry;
730 if (FindEntry(za, zip_string_path, &entry) != 0) {
731 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
732 zip_path.c_str());
733 }
734
735 std::string buffer;
736 buffer.resize(entry.uncompressed_length);
737
738 int32_t ret = ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
739 if (ret != 0) {
740 return ErrorAbort(state, kPackageExtractFileFailure,
741 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
742 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
743 }
744
745 return new Value(VAL_BLOB, buffer);
746 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700747}
748
Tom Marshallbf4f24f2017-08-23 18:14:00 +0000749// symlink(target, [src1, src2, ...])
750// Creates all sources as symlinks to target. It unlinks any previously existing src1, src2, etc
751// before creating symlinks.
752Value* SymlinkFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
753 if (argv.size() == 0) {
754 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %zu", name, argv.size());
755 }
756 std::string target;
757 if (!Evaluate(state, argv[0], &target)) {
758 return nullptr;
759 }
760
761 std::vector<std::string> srcs;
762 if (!ReadArgs(state, argv, &srcs, 1, argv.size())) {
763 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
764 }
765
766 size_t bad = 0;
767 for (const auto& src : srcs) {
768 if (unlink(src.c_str()) == -1 && errno != ENOENT) {
769 PLOG(ERROR) << name << ": failed to remove " << src;
770 ++bad;
771 } else if (!make_parents(src)) {
772 LOG(ERROR) << name << ": failed to symlink " << src << " to " << target
773 << ": making parents failed";
774 ++bad;
775 } else if (symlink(target.c_str(), src.c_str()) == -1) {
776 PLOG(ERROR) << name << ": failed to symlink " << src << " to " << target;
777 ++bad;
778 }
779 }
780 if (bad != 0) {
781 return ErrorAbort(state, kSymlinkFailure, "%s: Failed to create %zu symlink(s)", name, bad);
782 }
783 return StringValue("t");
784}
785
786struct perm_parsed_args {
787 bool has_uid;
788 uid_t uid;
789 bool has_gid;
790 gid_t gid;
791 bool has_mode;
792 mode_t mode;
793 bool has_fmode;
794 mode_t fmode;
795 bool has_dmode;
796 mode_t dmode;
797 bool has_selabel;
798 const char* selabel;
799 bool has_capabilities;
800 uint64_t capabilities;
801};
802
803static struct perm_parsed_args ParsePermArgs(State * state,
804 const std::vector<std::string>& args) {
805 struct perm_parsed_args parsed;
806 int bad = 0;
807 static int max_warnings = 20;
808
809 memset(&parsed, 0, sizeof(parsed));
810
811 for (size_t i = 1; i < args.size(); i += 2) {
812 if (args[i] == "uid") {
813 int64_t uid;
814 if (sscanf(args[i + 1].c_str(), "%" SCNd64, &uid) == 1) {
815 parsed.uid = uid;
816 parsed.has_uid = true;
817 } else {
818 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1].c_str());
819 bad++;
820 }
821 continue;
822 }
823 if (args[i] == "gid") {
824 int64_t gid;
825 if (sscanf(args[i + 1].c_str(), "%" SCNd64, &gid) == 1) {
826 parsed.gid = gid;
827 parsed.has_gid = true;
828 } else {
829 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1].c_str());
830 bad++;
831 }
832 continue;
833 }
834 if (args[i] == "mode") {
835 int32_t mode;
836 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
837 parsed.mode = mode;
838 parsed.has_mode = true;
839 } else {
840 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1].c_str());
841 bad++;
842 }
843 continue;
844 }
845 if (args[i] == "dmode") {
846 int32_t mode;
847 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
848 parsed.dmode = mode;
849 parsed.has_dmode = true;
850 } else {
851 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1].c_str());
852 bad++;
853 }
854 continue;
855 }
856 if (args[i] == "fmode") {
857 int32_t mode;
858 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
859 parsed.fmode = mode;
860 parsed.has_fmode = true;
861 } else {
862 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1].c_str());
863 bad++;
864 }
865 continue;
866 }
867 if (args[i] == "capabilities") {
868 int64_t capabilities;
869 if (sscanf(args[i + 1].c_str(), "%" SCNi64, &capabilities) == 1) {
870 parsed.capabilities = capabilities;
871 parsed.has_capabilities = true;
872 } else {
873 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1].c_str());
874 bad++;
875 }
876 continue;
877 }
878 if (args[i] == "selabel") {
879 if (!args[i + 1].empty()) {
880 parsed.selabel = args[i + 1].c_str();
881 parsed.has_selabel = true;
882 } else {
883 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1].c_str());
884 bad++;
885 }
886 continue;
887 }
888 if (max_warnings != 0) {
889 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i].c_str());
890 max_warnings--;
891 if (max_warnings == 0) {
892 LOG(INFO) << "ParsedPermArgs: suppressing further warnings";
893 }
894 }
895 }
896 return parsed;
897}
898
899static int ApplyParsedPerms(State* state, const char* filename, const struct stat* statptr,
900 struct perm_parsed_args parsed) {
901 int bad = 0;
902
903 if (parsed.has_selabel) {
904 if (lsetfilecon(filename, parsed.selabel) != 0) {
905 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n", filename,
906 parsed.selabel, strerror(errno));
907 bad++;
908 }
909 }
910
911 /* ignore symlinks */
912 if (S_ISLNK(statptr->st_mode)) {
913 return bad;
914 }
915
916 if (parsed.has_uid) {
917 if (chown(filename, parsed.uid, -1) < 0) {
918 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n", filename, parsed.uid,
919 strerror(errno));
920 bad++;
921 }
922 }
923
924 if (parsed.has_gid) {
925 if (chown(filename, -1, parsed.gid) < 0) {
926 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n", filename, parsed.gid,
927 strerror(errno));
928 bad++;
929 }
930 }
931
932 if (parsed.has_mode) {
933 if (chmod(filename, parsed.mode) < 0) {
934 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.mode,
935 strerror(errno));
936 bad++;
937 }
938 }
939
940 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
941 if (chmod(filename, parsed.dmode) < 0) {
942 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.dmode,
943 strerror(errno));
944 bad++;
945 }
946 }
947
948 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
949 if (chmod(filename, parsed.fmode) < 0) {
950 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.fmode,
951 strerror(errno));
952 bad++;
953 }
954 }
955
956 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
957 if (parsed.capabilities == 0) {
958 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
959 // Report failure unless it's ENODATA (attribute not set)
960 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n", filename,
961 parsed.capabilities, strerror(errno));
962 bad++;
963 }
964 } else {
965 struct vfs_cap_data cap_data;
966 memset(&cap_data, 0, sizeof(cap_data));
967 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
968 cap_data.data[0].permitted = (uint32_t)(parsed.capabilities & 0xffffffff);
969 cap_data.data[0].inheritable = 0;
970 cap_data.data[1].permitted = (uint32_t)(parsed.capabilities >> 32);
971 cap_data.data[1].inheritable = 0;
972 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
973 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n", filename,
974 parsed.capabilities, strerror(errno));
975 bad++;
976 }
977 }
978 }
979
980 return bad;
981}
982
983// nftw doesn't allow us to pass along context, so we need to use
984// global variables. *sigh*
985static struct perm_parsed_args recursive_parsed_args;
986static State* recursive_state;
987
988static int do_SetMetadataRecursive(const char* filename, const struct stat* statptr, int fileflags,
989 struct FTW* pfwt) {
990 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
991}
992
993static Value* SetMetadataFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
994 if ((argv.size() % 2) != 1) {
995 return ErrorAbort(state, kArgsParsingFailure, "%s() expects an odd number of arguments, got %zu",
996 name, argv.size());
997 }
998
999 std::vector<std::string> args;
1000 if (!ReadArgs(state, argv, &args)) {
1001 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1002 }
1003
1004 struct stat sb;
1005 if (lstat(args[0].c_str(), &sb) == -1) {
1006 return ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s", name,
1007 args[0].c_str(), strerror(errno));
1008 }
1009
1010 struct perm_parsed_args parsed = ParsePermArgs(state, args);
1011 int bad = 0;
1012 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
1013
1014 if (recursive) {
1015 recursive_parsed_args = parsed;
1016 recursive_state = state;
1017 bad += nftw(args[0].c_str(), do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
1018 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
1019 recursive_state = NULL;
1020 } else {
1021 bad += ApplyParsedPerms(state, args[0].c_str(), &sb, parsed);
1022 }
1023
1024 if (bad > 0) {
1025 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
1026 }
1027
1028 return StringValue("");
1029}
1030
Tianjie Xuc4447322017-03-06 14:44:59 -08001031Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1032 if (argv.size() != 1) {
1033 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001034 }
1035 std::string key;
1036 if (!Evaluate(state, argv[0], &key)) {
1037 return nullptr;
1038 }
1039 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -07001040
Tao Bao039f2da2016-11-22 16:29:50 -08001041 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001042}
1043
Doug Zongker47cace92009-06-18 10:11:50 -07001044// file_getprop(file, key)
1045//
1046// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -07001047// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001048// and returns the value for 'key' (or "" if it isn't defined).
Tianjie Xu5419ad32018-02-02 16:49:15 -08001049Value* FileGetPropFn(const char* name, State* state,
1050 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -08001051 if (argv.size() != 2) {
1052 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1053 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -08001054 }
1055
1056 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001057 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -08001058 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1059 }
1060 const std::string& filename = args[0];
1061 const std::string& key = args[1];
1062
Tianjie Xu22f11202018-08-27 10:50:31 -07001063 std::string buffer;
1064 if (!android::base::ReadFileToString(filename, &buffer)) {
1065 ErrorAbort(state, kFreadFailure, "%s: failed to read %s", name, filename.c_str());
Tao Bao358c2ec2016-11-28 11:48:43 -08001066 return nullptr;
1067 }
1068
Tao Bao358c2ec2016-11-28 11:48:43 -08001069 std::vector<std::string> lines = android::base::Split(buffer, "\n");
1070 for (size_t i = 0; i < lines.size(); i++) {
1071 std::string line = android::base::Trim(lines[i]);
1072
1073 // comment or blank line: skip to next line
1074 if (line.empty() || line[0] == '#') {
1075 continue;
1076 }
1077 size_t equal_pos = line.find('=');
1078 if (equal_pos == std::string::npos) {
1079 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001080 }
1081
Tao Bao358c2ec2016-11-28 11:48:43 -08001082 // trim whitespace between key and '='
1083 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -07001084
Tao Bao358c2ec2016-11-28 11:48:43 -08001085 // not the key we're looking for
1086 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001087
Tao Bao358c2ec2016-11-28 11:48:43 -08001088 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
1089 }
Doug Zongker47cace92009-06-18 10:11:50 -07001090
Tao Bao358c2ec2016-11-28 11:48:43 -08001091 return StringValue("");
Doug Zongker8edb00c2009-06-11 17:21:44 -07001092}
1093
Doug Zongker8edb00c2009-06-11 17:21:44 -07001094// apply_patch_space(bytes)
Tianjie Xu5419ad32018-02-02 16:49:15 -08001095Value* ApplyPatchSpaceFn(const char* name, State* state,
1096 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -08001097 if (argv.size() != 1) {
1098 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name,
1099 argv.size());
1100 }
Tao Bao039f2da2016-11-22 16:29:50 -08001101 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001102 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001103 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1104 }
1105 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -08001106
Tao Bao039f2da2016-11-22 16:29:50 -08001107 size_t bytes;
1108 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -08001109 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name,
1110 bytes_str.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -08001111 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001112
Tianjie Xu99b73be2017-11-28 17:23:06 -08001113 // Skip the cache size check if the update is a retry.
Tao Bao5ee25662018-07-11 15:55:32 -07001114 if (state->is_retry || CheckAndFreeSpaceOnCache(bytes)) {
Tianjie Xu99b73be2017-11-28 17:23:06 -08001115 return StringValue("t");
Tao Bao039f2da2016-11-22 16:29:50 -08001116 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001117 return StringValue("");
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001118}
1119
Tianjie Xuc4447322017-03-06 14:44:59 -08001120Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1121 if (!argv.empty()) {
1122 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
1123 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001124 }
1125 fprintf(static_cast<UpdaterInfo*>(state->cookie)->cmd_pipe, "wipe_cache\n");
1126 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -07001127}
1128
Tianjie Xuc4447322017-03-06 14:44:59 -08001129Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1130 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -08001131 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
1132 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001133
Tao Bao039f2da2016-11-22 16:29:50 -08001134 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001135 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001136 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1137 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001138
Tao Bao3d69f0d2018-12-20 09:44:06 -08001139 auto exec_args = StringVectorToNullTerminatedArray(args);
1140 LOG(INFO) << "about to run program [" << exec_args[0] << "] with " << argv.size() << " args";
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001141
Tao Bao039f2da2016-11-22 16:29:50 -08001142 pid_t child = fork();
1143 if (child == 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -08001144 execv(exec_args[0], exec_args.data());
Tao Bao039f2da2016-11-22 16:29:50 -08001145 PLOG(ERROR) << "run_program: execv failed";
Tao Bao3da88012017-02-03 13:09:23 -08001146 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -08001147 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001148
Tao Bao039f2da2016-11-22 16:29:50 -08001149 int status;
1150 waitpid(child, &status, 0);
1151 if (WIFEXITED(status)) {
1152 if (WEXITSTATUS(status) != 0) {
1153 LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001154 }
Tao Bao039f2da2016-11-22 16:29:50 -08001155 } else if (WIFSIGNALED(status)) {
1156 LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status);
1157 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001158
Tao Bao039f2da2016-11-22 16:29:50 -08001159 return StringValue(std::to_string(status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001160}
1161
Tao Bao511d7592018-06-19 15:56:49 -07001162// read_file(filename)
Tao Baobafd6c72018-07-09 15:08:50 -07001163// Reads a local file 'filename' and returns its contents as a string Value.
Tianjie Xuc4447322017-03-06 14:44:59 -08001164Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1165 if (argv.size() != 1) {
1166 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001167 }
Doug Zongker512536a2010-02-17 16:11:44 -08001168
Tao Bao039f2da2016-11-22 16:29:50 -08001169 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001170 if (!ReadArgs(state, argv, &args)) {
Tao Bao511d7592018-06-19 15:56:49 -07001171 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tao Bao039f2da2016-11-22 16:29:50 -08001172 }
1173 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -08001174
Tao Baobafd6c72018-07-09 15:08:50 -07001175 std::string contents;
1176 if (android::base::ReadFileToString(filename, &contents)) {
1177 return new Value(Value::Type::STRING, std::move(contents));
Tao Bao039f2da2016-11-22 16:29:50 -08001178 }
Tao Bao511d7592018-06-19 15:56:49 -07001179
1180 // Leave it to caller to handle the failure.
Tao Baobafd6c72018-07-09 15:08:50 -07001181 PLOG(ERROR) << name << ": Failed to read " << filename;
Tao Bao511d7592018-06-19 15:56:49 -07001182 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -08001183}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001184
Tao Bao1bf17722016-11-03 23:52:01 -07001185// write_value(value, filename)
1186// Writes 'value' to 'filename'.
1187// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
Tianjie Xuc4447322017-03-06 14:44:59 -08001188Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1189 if (argv.size() != 2) {
1190 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1191 argv.size());
Tao Baod0f30882016-11-03 23:52:01 -07001192 }
Tao Bao1bf17722016-11-03 23:52:01 -07001193
Tao Baod0f30882016-11-03 23:52:01 -07001194 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001195 if (!ReadArgs(state, argv, &args)) {
Tao Baod0f30882016-11-03 23:52:01 -07001196 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1197 }
Tao Bao1bf17722016-11-03 23:52:01 -07001198
Tao Baod0f30882016-11-03 23:52:01 -07001199 const std::string& filename = args[1];
1200 if (filename.empty()) {
1201 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
1202 }
Tao Bao1bf17722016-11-03 23:52:01 -07001203
Tao Baod0f30882016-11-03 23:52:01 -07001204 const std::string& value = args[0];
1205 if (!android::base::WriteStringToFile(value, filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001206 PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\"";
Tao Baod0f30882016-11-03 23:52:01 -07001207 return StringValue("");
1208 } else {
1209 return StringValue("t");
1210 }
Tao Bao1bf17722016-11-03 23:52:01 -07001211}
1212
Doug Zongkerc87bab12013-11-25 13:53:25 -08001213// Immediately reboot the device. Recovery is not finished normally,
1214// so if you reboot into recovery it will re-start applying the
1215// current package (because nothing has cleared the copy of the
1216// arguments stored in the BCB).
1217//
1218// The argument is the partition name passed to the android reboot
1219// property. It can be "recovery" to boot from the recovery
1220// partition, or "" (empty string) to boot from the regular boot
1221// partition.
Tianjie Xuc4447322017-03-06 14:44:59 -08001222Value* RebootNowFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1223 if (argv.size() != 2) {
1224 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1225 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001226 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001227
Tao Baobedf5fc2016-11-18 12:01:26 -08001228 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001229 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001230 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1231 }
1232 const std::string& filename = args[0];
1233 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001234
Tao Baobedf5fc2016-11-18 12:01:26 -08001235 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
1236 bootloader_message boot;
1237 std::string err;
1238 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001239 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001240 return StringValue("");
1241 }
1242 memset(boot.command, 0, sizeof(boot.command));
1243 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001244 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001245 return StringValue("");
1246 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001247
Tao Bao2c526392018-05-03 23:01:13 -07001248 reboot("reboot," + property);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001249
Tao Baobedf5fc2016-11-18 12:01:26 -08001250 sleep(5);
1251 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001252}
1253
1254// Store a string value somewhere that future invocations of recovery
1255// can access it. This value is called the "stage" and can be used to
1256// drive packages that need to do reboots in the middle of
1257// installation and keep track of where they are in the multi-stage
1258// install.
1259//
1260// The first argument is the block device for the misc partition
1261// ("/misc" in the fstab), which is where this value is stored. The
1262// second argument is the string to store; it should not exceed 31
1263// bytes.
Tianjie Xuc4447322017-03-06 14:44:59 -08001264Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1265 if (argv.size() != 2) {
1266 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1267 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001268 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001269
Tao Baobedf5fc2016-11-18 12:01:26 -08001270 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001271 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001272 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1273 }
1274 const std::string& filename = args[0];
1275 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001276
Tao Baobedf5fc2016-11-18 12:01:26 -08001277 // Store this value in the misc partition, immediately after the
1278 // bootloader message that the main recovery uses to save its
1279 // arguments in case of the device restarting midway through
1280 // package installation.
1281 bootloader_message boot;
1282 std::string err;
1283 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001284 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001285 return StringValue("");
1286 }
1287 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
1288 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001289 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001290 return StringValue("");
1291 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001292
Tao Baobedf5fc2016-11-18 12:01:26 -08001293 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001294}
1295
1296// Return the value most recently saved with SetStageFn. The argument
1297// is the block device for the misc partition.
Tianjie Xuc4447322017-03-06 14:44:59 -08001298Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1299 if (argv.size() != 1) {
1300 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001301 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001302
Tao Baobedf5fc2016-11-18 12:01:26 -08001303 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001304 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001305 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1306 }
1307 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001308
Tao Baobedf5fc2016-11-18 12:01:26 -08001309 bootloader_message boot;
1310 std::string err;
1311 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001312 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001313 return StringValue("");
1314 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001315
Tao Baobedf5fc2016-11-18 12:01:26 -08001316 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001317}
1318
Tianjie Xuc4447322017-03-06 14:44:59 -08001319Value* WipeBlockDeviceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1320 if (argv.size() != 2) {
1321 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1322 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -08001323 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001324
Tao Bao358c2ec2016-11-28 11:48:43 -08001325 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001326 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -08001327 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1328 }
1329 const std::string& filename = args[0];
1330 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001331
Tao Bao358c2ec2016-11-28 11:48:43 -08001332 size_t len;
1333 if (!android::base::ParseUint(len_str.c_str(), &len)) {
1334 return nullptr;
1335 }
Tianjie Xu22f11202018-08-27 10:50:31 -07001336 android::base::unique_fd fd(open(filename.c_str(), O_WRONLY));
1337 if (fd == -1) {
1338 PLOG(ERROR) << "Failed to open " << filename;
1339 return StringValue("");
1340 }
1341
Tao Bao358c2ec2016-11-28 11:48:43 -08001342 // The wipe_block_device function in ext4_utils returns 0 on success and 1
1343 // for failure.
1344 int status = wipe_block_device(fd, len);
1345 return StringValue((status == 0) ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001346}
1347
Tianjie Xuc4447322017-03-06 14:44:59 -08001348Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1349 if (!argv.empty()) {
1350 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
1351 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001352 }
1353 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
1354 fprintf(ui->cmd_pipe, "enable_reboot\n");
1355 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -07001356}
1357
Tianjie Xuc4447322017-03-06 14:44:59 -08001358Value* Tune2FsFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001359#ifdef HAVE_LIBTUNE2FS
Tianjie Xuc4447322017-03-06 14:44:59 -08001360 if (argv.empty()) {
1361 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001362 }
Michael Rungeb278c252014-11-21 00:12:28 -08001363
Tao Bao039f2da2016-11-22 16:29:50 -08001364 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001365 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001366 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
1367 }
Michael Rungeb278c252014-11-21 00:12:28 -08001368
Tao Bao3d69f0d2018-12-20 09:44:06 -08001369 // tune2fs expects the program name as its first arg.
1370 args.insert(args.begin(), "tune2fs");
1371 auto tune2fs_args = StringVectorToNullTerminatedArray(args);
Michael Rungeb278c252014-11-21 00:12:28 -08001372
Tao Bao3d69f0d2018-12-20 09:44:06 -08001373 // tune2fs changes the filesystem parameters on an ext2 filesystem; it returns 0 on success.
1374 if (auto result = tune2fs_main(tune2fs_args.size() - 1, tune2fs_args.data()); result != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -08001375 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
1376 }
1377 return StringValue("t");
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001378#else
Ethan Yonker8373cfe2017-09-08 06:50:54 -05001379 return ErrorAbort(state, kTune2FsFailure, "%s() support not present, no libtune2fs", name);
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001380#endif // HAVE_LIBTUNE2FS
Michael Rungeb278c252014-11-21 00:12:28 -08001381}
1382
Doug Zongker9931f7f2009-06-10 14:11:53 -07001383void RegisterInstallFunctions() {
Tao Bao039f2da2016-11-22 16:29:50 -08001384 RegisterFunction("mount", MountFn);
1385 RegisterFunction("is_mounted", IsMountedFn);
1386 RegisterFunction("unmount", UnmountFn);
1387 RegisterFunction("format", FormatFn);
1388 RegisterFunction("show_progress", ShowProgressFn);
1389 RegisterFunction("set_progress", SetProgressFn);
Tom Marshallbf4f24f2017-08-23 18:14:00 +00001390 RegisterFunction("delete", DeleteFn);
1391 RegisterFunction("delete_recursive", DeleteFn);
Tom Marshall981118e2017-10-25 20:27:08 +02001392 RegisterFunction("package_extract_dir", PackageExtractDirFn);
Tao Bao039f2da2016-11-22 16:29:50 -08001393 RegisterFunction("package_extract_file", PackageExtractFileFn);
Tom Marshallbf4f24f2017-08-23 18:14:00 +00001394 RegisterFunction("symlink", SymlinkFn);
1395
1396 // Usage:
1397 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1398 // Example:
1399 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel",
1400 // "u:object_r:system_file:s0", "capabilities", 0x0);
1401 RegisterFunction("set_metadata", SetMetadataFn);
1402
1403 // Usage:
1404 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1405 // Example:
1406 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755,
1407 // "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1408 RegisterFunction("set_metadata_recursive", SetMetadataFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001409
Tao Bao039f2da2016-11-22 16:29:50 -08001410 RegisterFunction("getprop", GetPropFn);
1411 RegisterFunction("file_getprop", FileGetPropFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001412
Tao Bao039f2da2016-11-22 16:29:50 -08001413 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Tao Bao5609bc82018-06-20 00:30:48 -07001414 RegisterFunction("patch_partition", PatchPartitionFn);
1415 RegisterFunction("patch_partition_check", PatchPartitionCheckFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001416
Tao Bao039f2da2016-11-22 16:29:50 -08001417 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001418
Tao Bao039f2da2016-11-22 16:29:50 -08001419 RegisterFunction("read_file", ReadFileFn);
Tao Bao039f2da2016-11-22 16:29:50 -08001420 RegisterFunction("write_value", WriteValueFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001421
Tao Bao039f2da2016-11-22 16:29:50 -08001422 RegisterFunction("wipe_cache", WipeCacheFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001423
Tao Bao039f2da2016-11-22 16:29:50 -08001424 RegisterFunction("ui_print", UIPrintFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001425
Tao Bao039f2da2016-11-22 16:29:50 -08001426 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001427
Tao Bao039f2da2016-11-22 16:29:50 -08001428 RegisterFunction("reboot_now", RebootNowFn);
1429 RegisterFunction("get_stage", GetStageFn);
1430 RegisterFunction("set_stage", SetStageFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001431
Tao Bao039f2da2016-11-22 16:29:50 -08001432 RegisterFunction("enable_reboot", EnableRebootFn);
1433 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001434}