blob: 0bd56f2872d1352c8a8d2fee3ac5339f8b4ea933 [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>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070056#include <ziparchive/zip_archive.h>
Tao Bao1107d962015-09-09 17:16:55 -070057
Doug Zongker9931f7f2009-06-10 14:11:53 -070058#include "edify/expr.h"
Tianjie Xu1536db82019-05-14 10:54:43 -070059#include "edify/updater_interface.h"
60#include "edify/updater_runtime_interface.h"
Tao Bao17054c02018-05-03 22:41:23 -070061#include "otautil/dirutil.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070062#include "otautil/error_code.h"
Tao Bao09e468f2017-09-29 14:39:33 -070063#include "otautil/print_sha1.h"
Tao Bao2c526392018-05-03 23:01:13 -070064#include "otautil/sysutil.h"
Michael Runge75480252014-10-22 19:48:41 -070065
Tianjie Xu27556d02019-05-22 14:48:35 -070066#ifndef __ANDROID__
67#include <cutils/memory.h> // for strlcpy
68#endif
Simon Shields2e9747f2018-08-09 01:17:21 +100069
Tianjie Xu1536db82019-05-14 10:54:43 -070070static bool UpdateBlockDeviceNameForPartition(UpdaterInterface* updater, Partition* partition) {
71 CHECK(updater);
72 std::string name = updater->FindBlockDeviceName(partition->name);
73 if (name.empty()) {
74 LOG(ERROR) << "Failed to find the block device " << partition->name;
75 return false;
Simon Shields2e9747f2018-08-09 01:17:21 +100076 }
Tianjie Xu1536db82019-05-14 10:54:43 -070077
78 partition->name = std::move(name);
Simon Shields2e9747f2018-08-09 01:17:21 +100079 return true;
80}
81
Tianjie Xu5419ad32018-02-02 16:49:15 -080082// This is the updater side handler for ui_print() in edify script. Contents will be sent over to
83// the recovery side for on-screen display.
84Value* UIPrintFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
85 std::vector<std::string> args;
86 if (!ReadArgs(state, argv, &args)) {
87 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
88 }
89
90 std::string buffer = android::base::Join(args, "");
Tianjie Xu1536db82019-05-14 10:54:43 -070091 state->updater->UiPrint(buffer);
Tianjie Xu5419ad32018-02-02 16:49:15 -080092 return StringValue(buffer);
Doug Zongker8edb00c2009-06-11 17:21:44 -070093}
94
Tianjie Xu5419ad32018-02-02 16:49:15 -080095// package_extract_file(package_file[, dest_file])
96// Extracts a single package_file from the update package and writes it to dest_file,
97// overwriting existing files if necessary. Without the dest_file argument, returns the
98// contents of the package file as a binary blob.
99Value* PackageExtractFileFn(const char* name, State* state,
100 const std::vector<std::unique_ptr<Expr>>& argv) {
101 if (argv.size() < 1 || argv.size() > 2) {
102 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name,
103 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800104 }
Tianjie Xu5419ad32018-02-02 16:49:15 -0800105
106 if (argv.size() == 2) {
107 // The two-argument version extracts to a file.
108
109 std::vector<std::string> args;
110 if (!ReadArgs(state, argv, &args)) {
111 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
112 argv.size());
113 }
114 const std::string& zip_path = args[0];
Tianjie Xu60b242c2019-07-30 16:48:52 -0700115 std::string dest_path = args[1];
Tianjie Xu5419ad32018-02-02 16:49:15 -0800116
Tianjie Xu1536db82019-05-14 10:54:43 -0700117 ZipArchiveHandle za = state->updater->GetPackageHandle();
Tianjie Xu5419ad32018-02-02 16:49:15 -0800118 ZipEntry entry;
Elliott Hughesa86dddb2019-05-03 22:52:37 -0700119 if (FindEntry(za, zip_path, &entry) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800120 LOG(ERROR) << name << ": no " << zip_path << " in package";
121 return StringValue("");
122 }
123
Tianjie Xu60b242c2019-07-30 16:48:52 -0700124 // Update the destination of package_extract_file if it's a block device. During simulation the
125 // destination will map to a fake file.
126 if (std::string block_device_name = state->updater->FindBlockDeviceName(dest_path);
127 !block_device_name.empty()) {
128 dest_path = block_device_name;
129 }
130
Tianjie Xu22f11202018-08-27 10:50:31 -0700131 android::base::unique_fd fd(TEMP_FAILURE_RETRY(
132 open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tianjie Xu5419ad32018-02-02 16:49:15 -0800133 if (fd == -1) {
134 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
135 return StringValue("");
136 }
137
138 bool success = true;
139 int32_t ret = ExtractEntryToFile(za, &entry, fd);
140 if (ret != 0) {
141 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
142 << entry.uncompressed_length << " bytes) to \"" << dest_path
143 << "\": " << ErrorCodeString(ret);
144 success = false;
145 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700146 if (fsync(fd) == -1) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800147 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
148 success = false;
149 }
Tianjie Xu22f11202018-08-27 10:50:31 -0700150
151 if (close(fd.release()) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800152 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
153 success = false;
154 }
155
156 return StringValue(success ? "t" : "");
157 } else {
158 // The one-argument version returns the contents of the file as the result.
159
160 std::vector<std::string> args;
161 if (!ReadArgs(state, argv, &args)) {
162 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
163 argv.size());
164 }
165 const std::string& zip_path = args[0];
166
Tianjie Xu1536db82019-05-14 10:54:43 -0700167 ZipArchiveHandle za = state->updater->GetPackageHandle();
Tianjie Xu5419ad32018-02-02 16:49:15 -0800168 ZipEntry entry;
Elliott Hughesa86dddb2019-05-03 22:52:37 -0700169 if (FindEntry(za, zip_path, &entry) != 0) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800170 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
171 zip_path.c_str());
172 }
173
174 std::string buffer;
175 buffer.resize(entry.uncompressed_length);
176
177 int32_t ret =
178 ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
179 if (ret != 0) {
180 return ErrorAbort(state, kPackageExtractFileFailure,
181 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
182 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
183 }
184
Tao Bao511d7592018-06-19 15:56:49 -0700185 return new Value(Value::Type::BLOB, buffer);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800186 }
187}
188
Tao Bao5609bc82018-06-20 00:30:48 -0700189// patch_partition_check(target_partition, source_partition)
190// Checks if the target and source partitions have the desired checksums to be patched. It returns
191// directly, if the target partition already has the expected checksum. Otherwise it in turn
192// checks the integrity of the source partition and the backup file on /cache.
Tianjie Xu5419ad32018-02-02 16:49:15 -0800193//
Tao Bao5609bc82018-06-20 00:30:48 -0700194// For example, patch_partition_check(
195// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
196// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4")
197Value* PatchPartitionCheckFn(const char* name, State* state,
198 const std::vector<std::unique_ptr<Expr>>& argv) {
199 if (argv.size() != 2) {
Tianjie Xu5419ad32018-02-02 16:49:15 -0800200 return ErrorAbort(state, kArgsParsingFailure,
Tao Bao5609bc82018-06-20 00:30:48 -0700201 "%s(): Invalid number of args (expected 2, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800202 }
203
204 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700205 if (!ReadArgs(state, argv, &args, 0, 2)) {
206 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800207 }
208
Tao Bao5609bc82018-06-20 00:30:48 -0700209 std::string err;
210 auto target = Partition::Parse(args[0], &err);
211 if (!target) {
212 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
213 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800214 }
215
Tao Bao5609bc82018-06-20 00:30:48 -0700216 auto source = Partition::Parse(args[1], &err);
217 if (!source) {
218 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
219 args[1].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800220 }
221
Tianjie Xu1536db82019-05-14 10:54:43 -0700222 if (!UpdateBlockDeviceNameForPartition(state->updater, &source) ||
223 !UpdateBlockDeviceNameForPartition(state->updater, &target)) {
224 return StringValue("");
225 }
226
Tao Bao5609bc82018-06-20 00:30:48 -0700227 bool result = PatchPartitionCheck(target, source);
228 return StringValue(result ? "t" : "");
Tianjie Xu5419ad32018-02-02 16:49:15 -0800229}
230
Tao Bao5609bc82018-06-20 00:30:48 -0700231// patch_partition(target, source, patch)
232// Applies the given patch to the source partition, and writes the result to the target partition.
233//
234// For example, patch_partition(
235// "EMMC:/dev/block/boot:12342568:8aaacf187a6929d0e9c3e9e46ea7ff495b43424d",
236// "EMMC:/dev/block/boot:12363048:06b0b16299dcefc94900efed01e0763ff644ffa4",
237// package_extract_file("boot.img.p"))
238Value* PatchPartitionFn(const char* name, State* state,
239 const std::vector<std::unique_ptr<Expr>>& argv) {
240 if (argv.size() != 3) {
241 return ErrorAbort(state, kArgsParsingFailure,
242 "%s(): Invalid number of args (expected 3, got %zu)", name, argv.size());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800243 }
244
245 std::vector<std::string> args;
Tao Bao5609bc82018-06-20 00:30:48 -0700246 if (!ReadArgs(state, argv, &args, 0, 2)) {
247 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800248 }
249
Tao Bao5609bc82018-06-20 00:30:48 -0700250 std::string err;
251 auto target = Partition::Parse(args[0], &err);
252 if (!target) {
253 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse target \"%s\": %s", name,
254 args[0].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800255 }
256
Tao Bao5609bc82018-06-20 00:30:48 -0700257 auto source = Partition::Parse(args[1], &err);
258 if (!source) {
259 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse source \"%s\": %s", name,
260 args[1].c_str(), err.c_str());
Tianjie Xu5419ad32018-02-02 16:49:15 -0800261 }
262
Tao Bao5609bc82018-06-20 00:30:48 -0700263 std::vector<std::unique_ptr<Value>> values;
264 if (!ReadValueArgs(state, argv, &values, 2, 1) || values[0]->type != Value::Type::BLOB) {
265 return ErrorAbort(state, kArgsParsingFailure, "%s(): Invalid patch arg", name);
Tianjie Xu5419ad32018-02-02 16:49:15 -0800266 }
267
Tianjie Xu1536db82019-05-14 10:54:43 -0700268 if (!UpdateBlockDeviceNameForPartition(state->updater, &source) ||
269 !UpdateBlockDeviceNameForPartition(state->updater, &target)) {
270 return StringValue("");
271 }
272
Tao Bao5234ad42019-09-23 10:28:54 -0700273 bool result = PatchPartition(target, source, *values[0], nullptr, true);
Tao Bao5609bc82018-06-20 00:30:48 -0700274 return StringValue(result ? "t" : "");
Doug Zongkera23075f2012-08-06 16:19:09 -0700275}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700276
Doug Zongker9931f7f2009-06-10 14:11:53 -0700277// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -0700278// mount(fs_type, partition_type, location, mount_point, mount_options)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700279
Doug Zongker9931f7f2009-06-10 14:11:53 -0700280// fs_type="ext4" partition_type="EMMC" location=device
Tianjie Xuc4447322017-03-06 14:44:59 -0800281Value* MountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
282 if (argv.size() != 4 && argv.size() != 5) {
283 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %zu", name,
284 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800285 }
286
287 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800288 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800289 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
290 }
291 const std::string& fs_type = args[0];
292 const std::string& partition_type = args[1];
293 const std::string& location = args[2];
294 const std::string& mount_point = args[3];
295 std::string mount_options;
296
Tianjie Xuc4447322017-03-06 14:44:59 -0800297 if (argv.size() == 5) {
Tao Bao039f2da2016-11-22 16:29:50 -0800298 mount_options = args[4];
299 }
300
301 if (fs_type.empty()) {
302 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
303 }
304 if (partition_type.empty()) {
305 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
306 name);
307 }
308 if (location.empty()) {
309 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
310 }
311 if (mount_point.empty()) {
312 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
313 name);
314 }
315
Tianjie Xu1536db82019-05-14 10:54:43 -0700316 auto updater = state->updater;
317 if (updater->GetRuntime()->Mount(location, mount_point, fs_type, mount_options) != 0) {
Tianjie Xu58d59122019-05-03 01:05:04 -0700318 updater->UiPrint(android::base::StringPrintf("%s: Failed to mount %s at %s: %s", name,
319 location.c_str(), mount_point.c_str(),
320 strerror(errno)));
Tao Bao039f2da2016-11-22 16:29:50 -0800321 return StringValue("");
322 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700323
Tao Bao039f2da2016-11-22 16:29:50 -0800324 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700325}
326
Doug Zongker9931f7f2009-06-10 14:11:53 -0700327// is_mounted(mount_point)
Tianjie Xuc4447322017-03-06 14:44:59 -0800328Value* IsMountedFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
329 if (argv.size() != 1) {
330 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800331 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700332
Tao Bao039f2da2016-11-22 16:29:50 -0800333 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800334 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800335 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
336 }
337 const std::string& mount_point = args[0];
338 if (mount_point.empty()) {
339 return ErrorAbort(state, kArgsParsingFailure,
340 "mount_point argument to unmount() can't be empty");
341 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700342
Tianjie Xu1536db82019-05-14 10:54:43 -0700343 auto updater_runtime = state->updater->GetRuntime();
344 if (!updater_runtime->IsMounted(mount_point)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800345 return StringValue("");
346 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700347
Tao Bao039f2da2016-11-22 16:29:50 -0800348 return StringValue(mount_point);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700349}
350
Tianjie Xuc4447322017-03-06 14:44:59 -0800351Value* UnmountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
352 if (argv.size() != 1) {
353 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800354 }
355 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800356 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800357 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
358 }
359 const std::string& mount_point = args[0];
360 if (mount_point.empty()) {
361 return ErrorAbort(state, kArgsParsingFailure,
362 "mount_point argument to unmount() can't be empty");
363 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700364
Tianjie Xu1536db82019-05-14 10:54:43 -0700365 auto updater = state->updater;
366 auto [mounted, result] = updater->GetRuntime()->Unmount(mount_point);
367 if (!mounted) {
Tianjie Xu58d59122019-05-03 01:05:04 -0700368 updater->UiPrint(
369 android::base::StringPrintf("Failed to unmount %s: No such volume", mount_point.c_str()));
Tao Bao039f2da2016-11-22 16:29:50 -0800370 return nullptr;
Tianjie Xu1536db82019-05-14 10:54:43 -0700371 } else if (result != 0) {
372 updater->UiPrint(android::base::StringPrintf("Failed to unmount %s: %s", mount_point.c_str(),
373 strerror(errno)));
Tao Bao039f2da2016-11-22 16:29:50 -0800374 }
Nick Kralevich6a821fe2014-10-23 20:36:42 -0700375
Tao Bao039f2da2016-11-22 16:29:50 -0800376 return StringValue(mount_point);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700377}
378
Doug Zongker9931f7f2009-06-10 14:11:53 -0700379// format(fs_type, partition_type, location, fs_size, mount_point)
380//
Tao Bao039f2da2016-11-22 16:29:50 -0800381// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
382// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700383// if fs_size == 0, then make fs uses the entire partition.
Doug Zongker9931f7f2009-06-10 14:11:53 -0700384// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700385// if fs_size < 0, then reserve that many bytes at the end of the partition (not for "f2fs")
Tianjie Xuc4447322017-03-06 14:44:59 -0800386Value* FormatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
387 if (argv.size() != 5) {
388 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %zu", name,
389 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800390 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700391
Tao Bao039f2da2016-11-22 16:29:50 -0800392 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800393 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800394 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
395 }
396 const std::string& fs_type = args[0];
397 const std::string& partition_type = args[1];
398 const std::string& location = args[2];
399 const std::string& fs_size = args[3];
400 const std::string& mount_point = args[4];
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700401
Tao Bao039f2da2016-11-22 16:29:50 -0800402 if (fs_type.empty()) {
403 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
404 }
405 if (partition_type.empty()) {
406 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
407 name);
408 }
409 if (location.empty()) {
410 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
411 }
412 if (mount_point.empty()) {
413 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
414 name);
415 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700416
Tao Bao039f2da2016-11-22 16:29:50 -0800417 int64_t size;
418 if (!android::base::ParseInt(fs_size, &size)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800419 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800420 fs_size.c_str());
421 }
422
Tianjie Xu1536db82019-05-14 10:54:43 -0700423 auto updater_runtime = state->updater->GetRuntime();
Tao Bao039f2da2016-11-22 16:29:50 -0800424 if (fs_type == "ext4") {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800425 std::vector<std::string> mke2fs_args = {
426 "/system/bin/mke2fs", "-t", "ext4", "-b", "4096", location
427 };
Jin Qianac318082017-04-21 14:36:12 -0700428 if (size != 0) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800429 mke2fs_args.push_back(std::to_string(size / 4096LL));
Jin Qianac318082017-04-21 14:36:12 -0700430 }
431
Tianjie Xu1536db82019-05-14 10:54:43 -0700432 if (auto status = updater_runtime->RunProgram(mke2fs_args, true); status != 0) {
Jin Qian502fd1c2017-11-02 11:58:12 -0700433 LOG(ERROR) << name << ": mke2fs failed (" << status << ") on " << location;
434 return StringValue("");
Jin Qianac318082017-04-21 14:36:12 -0700435 }
436
Tianjie Xu1536db82019-05-14 10:54:43 -0700437 if (auto status = updater_runtime->RunProgram(
438 { "/system/bin/e2fsdroid", "-e", "-a", mount_point, location }, true);
Tao Bao3d69f0d2018-12-20 09:44:06 -0800439 status != 0) {
Jin Qianac318082017-04-21 14:36:12 -0700440 LOG(ERROR) << name << ": e2fsdroid failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800441 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700442 }
Tao Bao039f2da2016-11-22 16:29:50 -0800443 return StringValue(location);
Tao Bao3d69f0d2018-12-20 09:44:06 -0800444 }
445
446 if (fs_type == "f2fs") {
Tao Bao039f2da2016-11-22 16:29:50 -0800447 if (size < 0) {
448 LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size;
449 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700450 }
Tao Bao3d69f0d2018-12-20 09:44:06 -0800451 std::vector<std::string> f2fs_args = {
Tao Baoc674dfb2018-12-20 14:25:15 -0800452 "/system/bin/make_f2fs", "-g", "android", "-w", "512", location
Tao Bao3d69f0d2018-12-20 09:44:06 -0800453 };
454 if (size >= 512) {
455 f2fs_args.push_back(std::to_string(size / 512));
456 }
Tianjie Xu1536db82019-05-14 10:54:43 -0700457 if (auto status = updater_runtime->RunProgram(f2fs_args, true); status != 0) {
Tao Baoc674dfb2018-12-20 14:25:15 -0800458 LOG(ERROR) << name << ": make_f2fs failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800459 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700460 }
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800461
Tianjie Xu1536db82019-05-14 10:54:43 -0700462 if (auto status = updater_runtime->RunProgram(
463 { "/system/bin/sload_f2fs", "-t", mount_point, location }, true);
Tao Baoc674dfb2018-12-20 14:25:15 -0800464 status != 0) {
465 LOG(ERROR) << name << ": sload_f2fs failed (" << status << ") on " << location;
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800466 return StringValue("");
467 }
468
Tao Bao039f2da2016-11-22 16:29:50 -0800469 return StringValue(location);
Tao Bao039f2da2016-11-22 16:29:50 -0800470 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700471
Tao Bao3d69f0d2018-12-20 09:44:06 -0800472 LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \""
473 << partition_type << "\"";
Tao Bao039f2da2016-11-22 16:29:50 -0800474 return nullptr;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700475}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700476
Tom Marshallbf4f24f2017-08-23 18:14:00 +0000477// rename(src_name, dst_name)
478// Renames src_name to dst_name. It automatically creates the necessary directories for dst_name.
479// Example: rename("system/app/Hangouts/Hangouts.apk", "system/priv-app/Hangouts/Hangouts.apk")
480Value* RenameFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
481 if (argv.size() != 2) {
482 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
483 argv.size());
484 }
485
486 std::vector<std::string> args;
487 if (!ReadArgs(state, argv, &args)) {
488 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
489 }
490 const std::string& src_name = args[0];
491 const std::string& dst_name = args[1];
492
493 if (src_name.empty()) {
494 return ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", name);
495 }
496 if (dst_name.empty()) {
497 return ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", name);
498 }
499 if (!make_parents(dst_name)) {
500 return ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
501 dst_name.c_str(), strerror(errno));
502 } else if (access(dst_name.c_str(), F_OK) == 0 && access(src_name.c_str(), F_OK) != 0) {
503 // File was already moved
504 return StringValue(dst_name);
505 } else if (rename(src_name.c_str(), dst_name.c_str()) != 0) {
506 return ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
507 src_name.c_str(), dst_name.c_str(), strerror(errno));
508 }
509
510 return StringValue(dst_name);
511}
512
513// delete([filename, ...])
514// Deletes all the filenames listed. Returns the number of files successfully deleted.
515//
516// delete_recursive([dirname, ...])
517// Recursively deletes dirnames and all their contents. Returns the number of directories
518// successfully deleted.
519Value* DeleteFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
520 std::vector<std::string> paths;
521 if (!ReadArgs(state, argv, &paths)) {
522 return nullptr;
523 }
524
525 bool recursive = (strcmp(name, "delete_recursive") == 0);
526
527 int success = 0;
528 for (const auto& path : paths) {
529 if ((recursive ? dirUnlinkHierarchy(path.c_str()) : unlink(path.c_str())) == 0) {
530 ++success;
531 }
532 }
533
534 return StringValue(std::to_string(success));
535}
536
537
Tianjie Xuc4447322017-03-06 14:44:59 -0800538Value* ShowProgressFn(const char* name, State* state,
539 const std::vector<std::unique_ptr<Expr>>& argv) {
540 if (argv.size() != 2) {
541 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
542 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800543 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700544
Tao Bao039f2da2016-11-22 16:29:50 -0800545 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800546 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800547 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
548 }
549 const std::string& frac_str = args[0];
550 const std::string& sec_str = args[1];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700551
Tao Bao039f2da2016-11-22 16:29:50 -0800552 double frac;
553 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800554 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800555 frac_str.c_str());
556 }
557 int sec;
558 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800559 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800560 sec_str.c_str());
561 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700562
Tianjie Xu1536db82019-05-14 10:54:43 -0700563 state->updater->WriteToCommandPipe(android::base::StringPrintf("progress %f %d", frac, sec));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700564
Tao Bao039f2da2016-11-22 16:29:50 -0800565 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700566}
567
Tianjie Xu5419ad32018-02-02 16:49:15 -0800568Value* SetProgressFn(const char* name, State* state,
569 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800570 if (argv.size() != 1) {
571 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800572 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700573
Tao Bao039f2da2016-11-22 16:29:50 -0800574 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800575 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800576 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
577 }
578 const std::string& frac_str = args[0];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700579
Tao Bao039f2da2016-11-22 16:29:50 -0800580 double frac;
581 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800582 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800583 frac_str.c_str());
584 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700585
Tianjie Xu1536db82019-05-14 10:54:43 -0700586 state->updater->WriteToCommandPipe(android::base::StringPrintf("set_progress %f", frac));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700587
Tao Bao039f2da2016-11-22 16:29:50 -0800588 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700589}
590
Tom Marshall981118e2017-10-25 20:27:08 +0200591// package_extract_dir(package_dir, dest_dir)
592// Extracts all files from the package underneath package_dir and writes them to the
593// corresponding tree beneath dest_dir. Any existing files are overwritten.
594// Example: package_extract_dir("system", "/system")
595//
596// Note: package_dir needs to be a relative path; dest_dir needs to be an absolute path.
597Value* PackageExtractDirFn(const char* name, State* state,
598 const std::vector<std::unique_ptr<Expr>>&argv) {
599 if (argv.size() != 2) {
600 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
601 argv.size());
602 }
603
604 std::vector<std::string> args;
605 if (!ReadArgs(state, argv, &args)) {
606 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
607 }
608 const std::string& zip_path = args[0];
609 const std::string& dest_path = args[1];
610
611 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
612
613 // To create a consistent system image, never use the clock for timestamps.
614 constexpr struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
615
616 bool success = ExtractPackageRecursive(za, zip_path, dest_path, &timestamp, sehandle);
617
618 return StringValue(success ? "t" : "");
619}
620
Tao Baoef0eb3b2016-11-14 21:29:52 -0800621// package_extract_file(package_file[, dest_file])
622// Extracts a single package_file from the update package and writes it to dest_file,
623// overwriting existing files if necessary. Without the dest_file argument, returns the
624// contents of the package file as a binary blob.
Tianjie Xuc4447322017-03-06 14:44:59 -0800625Value* PackageExtractFileFn(const char* name, State* state,
626 const std::vector<std::unique_ptr<Expr>>& argv) {
627 if (argv.size() < 1 || argv.size() > 2) {
628 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name,
629 argv.size());
Tao Baoef0eb3b2016-11-14 21:29:52 -0800630 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700631
Tianjie Xuc4447322017-03-06 14:44:59 -0800632 if (argv.size() == 2) {
Tao Baoef0eb3b2016-11-14 21:29:52 -0800633 // The two-argument version extracts to a file.
634
635 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800636 if (!ReadArgs(state, argv, &args)) {
637 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
638 argv.size());
Doug Zongker8edb00c2009-06-11 17:21:44 -0700639 }
Tao Baoef0eb3b2016-11-14 21:29:52 -0800640 const std::string& zip_path = args[0];
641 const std::string& dest_path = args[1];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700642
Tao Baoef0eb3b2016-11-14 21:29:52 -0800643 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
644 ZipString zip_string_path(zip_path.c_str());
645 ZipEntry entry;
646 if (FindEntry(za, zip_string_path, &entry) != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800647 LOG(ERROR) << name << ": no " << zip_path << " in package";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800648 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700649 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700650
Tao Bao358c2ec2016-11-28 11:48:43 -0800651 unique_fd fd(TEMP_FAILURE_RETRY(
652 ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tao Baoef0eb3b2016-11-14 21:29:52 -0800653 if (fd == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800654 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800655 return StringValue("");
656 }
657
658 bool success = true;
659 int32_t ret = ExtractEntryToFile(za, &entry, fd);
660 if (ret != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800661 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
662 << entry.uncompressed_length << " bytes) to \"" << dest_path
663 << "\": " << ErrorCodeString(ret);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800664 success = false;
665 }
666 if (ota_fsync(fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800667 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800668 success = false;
669 }
670 if (ota_close(fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800671 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800672 success = false;
673 }
674
675 return StringValue(success ? "t" : "");
676 } else {
677 // The one-argument version returns the contents of the file as the result.
678
679 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800680 if (!ReadArgs(state, argv, &args)) {
681 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
682 argv.size());
Tao Baoef0eb3b2016-11-14 21:29:52 -0800683 }
684 const std::string& zip_path = args[0];
685
686 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
687 ZipString zip_string_path(zip_path.c_str());
688 ZipEntry entry;
689 if (FindEntry(za, zip_string_path, &entry) != 0) {
690 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
691 zip_path.c_str());
692 }
693
694 std::string buffer;
695 buffer.resize(entry.uncompressed_length);
696
697 int32_t ret = ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
698 if (ret != 0) {
699 return ErrorAbort(state, kPackageExtractFileFailure,
700 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
701 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
702 }
703
704 return new Value(VAL_BLOB, buffer);
705 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700706}
707
Tom Marshallbf4f24f2017-08-23 18:14:00 +0000708// symlink(target, [src1, src2, ...])
709// Creates all sources as symlinks to target. It unlinks any previously existing src1, src2, etc
710// before creating symlinks.
711Value* SymlinkFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
712 if (argv.size() == 0) {
713 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %zu", name, argv.size());
714 }
715 std::string target;
716 if (!Evaluate(state, argv[0], &target)) {
717 return nullptr;
718 }
719
720 std::vector<std::string> srcs;
721 if (!ReadArgs(state, argv, &srcs, 1, argv.size())) {
722 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
723 }
724
725 size_t bad = 0;
726 for (const auto& src : srcs) {
727 if (unlink(src.c_str()) == -1 && errno != ENOENT) {
728 PLOG(ERROR) << name << ": failed to remove " << src;
729 ++bad;
730 } else if (!make_parents(src)) {
731 LOG(ERROR) << name << ": failed to symlink " << src << " to " << target
732 << ": making parents failed";
733 ++bad;
734 } else if (symlink(target.c_str(), src.c_str()) == -1) {
735 PLOG(ERROR) << name << ": failed to symlink " << src << " to " << target;
736 ++bad;
737 }
738 }
739 if (bad != 0) {
740 return ErrorAbort(state, kSymlinkFailure, "%s: Failed to create %zu symlink(s)", name, bad);
741 }
742 return StringValue("t");
743}
744
745struct perm_parsed_args {
746 bool has_uid;
747 uid_t uid;
748 bool has_gid;
749 gid_t gid;
750 bool has_mode;
751 mode_t mode;
752 bool has_fmode;
753 mode_t fmode;
754 bool has_dmode;
755 mode_t dmode;
756 bool has_selabel;
757 const char* selabel;
758 bool has_capabilities;
759 uint64_t capabilities;
760};
761
762static struct perm_parsed_args ParsePermArgs(State * state,
763 const std::vector<std::string>& args) {
764 struct perm_parsed_args parsed;
765 int bad = 0;
766 static int max_warnings = 20;
767
768 memset(&parsed, 0, sizeof(parsed));
769
770 for (size_t i = 1; i < args.size(); i += 2) {
771 if (args[i] == "uid") {
772 int64_t uid;
773 if (sscanf(args[i + 1].c_str(), "%" SCNd64, &uid) == 1) {
774 parsed.uid = uid;
775 parsed.has_uid = true;
776 } else {
777 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1].c_str());
778 bad++;
779 }
780 continue;
781 }
782 if (args[i] == "gid") {
783 int64_t gid;
784 if (sscanf(args[i + 1].c_str(), "%" SCNd64, &gid) == 1) {
785 parsed.gid = gid;
786 parsed.has_gid = true;
787 } else {
788 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1].c_str());
789 bad++;
790 }
791 continue;
792 }
793 if (args[i] == "mode") {
794 int32_t mode;
795 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
796 parsed.mode = mode;
797 parsed.has_mode = true;
798 } else {
799 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1].c_str());
800 bad++;
801 }
802 continue;
803 }
804 if (args[i] == "dmode") {
805 int32_t mode;
806 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
807 parsed.dmode = mode;
808 parsed.has_dmode = true;
809 } else {
810 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1].c_str());
811 bad++;
812 }
813 continue;
814 }
815 if (args[i] == "fmode") {
816 int32_t mode;
817 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
818 parsed.fmode = mode;
819 parsed.has_fmode = true;
820 } else {
821 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1].c_str());
822 bad++;
823 }
824 continue;
825 }
826 if (args[i] == "capabilities") {
827 int64_t capabilities;
828 if (sscanf(args[i + 1].c_str(), "%" SCNi64, &capabilities) == 1) {
829 parsed.capabilities = capabilities;
830 parsed.has_capabilities = true;
831 } else {
832 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1].c_str());
833 bad++;
834 }
835 continue;
836 }
837 if (args[i] == "selabel") {
838 if (!args[i + 1].empty()) {
839 parsed.selabel = args[i + 1].c_str();
840 parsed.has_selabel = true;
841 } else {
842 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1].c_str());
843 bad++;
844 }
845 continue;
846 }
847 if (max_warnings != 0) {
848 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i].c_str());
849 max_warnings--;
850 if (max_warnings == 0) {
851 LOG(INFO) << "ParsedPermArgs: suppressing further warnings";
852 }
853 }
854 }
855 return parsed;
856}
857
858static int ApplyParsedPerms(State* state, const char* filename, const struct stat* statptr,
859 struct perm_parsed_args parsed) {
860 int bad = 0;
861
862 if (parsed.has_selabel) {
863 if (lsetfilecon(filename, parsed.selabel) != 0) {
864 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n", filename,
865 parsed.selabel, strerror(errno));
866 bad++;
867 }
868 }
869
870 /* ignore symlinks */
871 if (S_ISLNK(statptr->st_mode)) {
872 return bad;
873 }
874
875 if (parsed.has_uid) {
876 if (chown(filename, parsed.uid, -1) < 0) {
877 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n", filename, parsed.uid,
878 strerror(errno));
879 bad++;
880 }
881 }
882
883 if (parsed.has_gid) {
884 if (chown(filename, -1, parsed.gid) < 0) {
885 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n", filename, parsed.gid,
886 strerror(errno));
887 bad++;
888 }
889 }
890
891 if (parsed.has_mode) {
892 if (chmod(filename, parsed.mode) < 0) {
893 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.mode,
894 strerror(errno));
895 bad++;
896 }
897 }
898
899 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
900 if (chmod(filename, parsed.dmode) < 0) {
901 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.dmode,
902 strerror(errno));
903 bad++;
904 }
905 }
906
907 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
908 if (chmod(filename, parsed.fmode) < 0) {
909 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.fmode,
910 strerror(errno));
911 bad++;
912 }
913 }
914
915 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
916 if (parsed.capabilities == 0) {
917 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
918 // Report failure unless it's ENODATA (attribute not set)
919 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n", filename,
920 parsed.capabilities, strerror(errno));
921 bad++;
922 }
923 } else {
924 struct vfs_cap_data cap_data;
925 memset(&cap_data, 0, sizeof(cap_data));
926 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
927 cap_data.data[0].permitted = (uint32_t)(parsed.capabilities & 0xffffffff);
928 cap_data.data[0].inheritable = 0;
929 cap_data.data[1].permitted = (uint32_t)(parsed.capabilities >> 32);
930 cap_data.data[1].inheritable = 0;
931 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
932 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n", filename,
933 parsed.capabilities, strerror(errno));
934 bad++;
935 }
936 }
937 }
938
939 return bad;
940}
941
942// nftw doesn't allow us to pass along context, so we need to use
943// global variables. *sigh*
944static struct perm_parsed_args recursive_parsed_args;
945static State* recursive_state;
946
947static int do_SetMetadataRecursive(const char* filename, const struct stat* statptr, int fileflags,
948 struct FTW* pfwt) {
949 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
950}
951
952static Value* SetMetadataFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
953 if ((argv.size() % 2) != 1) {
954 return ErrorAbort(state, kArgsParsingFailure, "%s() expects an odd number of arguments, got %zu",
955 name, argv.size());
956 }
957
958 std::vector<std::string> args;
959 if (!ReadArgs(state, argv, &args)) {
960 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
961 }
962
963 struct stat sb;
964 if (lstat(args[0].c_str(), &sb) == -1) {
965 return ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s", name,
966 args[0].c_str(), strerror(errno));
967 }
968
969 struct perm_parsed_args parsed = ParsePermArgs(state, args);
970 int bad = 0;
971 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
972
973 if (recursive) {
974 recursive_parsed_args = parsed;
975 recursive_state = state;
976 bad += nftw(args[0].c_str(), do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
977 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
978 recursive_state = NULL;
979 } else {
980 bad += ApplyParsedPerms(state, args[0].c_str(), &sb, parsed);
981 }
982
983 if (bad > 0) {
984 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
985 }
986
987 return StringValue("");
988}
989
Tianjie Xuc4447322017-03-06 14:44:59 -0800990Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
991 if (argv.size() != 1) {
992 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800993 }
994 std::string key;
995 if (!Evaluate(state, argv[0], &key)) {
996 return nullptr;
997 }
Tianjie Xu1536db82019-05-14 10:54:43 -0700998
999 auto updater_runtime = state->updater->GetRuntime();
1000 std::string value = updater_runtime->GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -07001001
Tao Bao039f2da2016-11-22 16:29:50 -08001002 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001003}
1004
Doug Zongker47cace92009-06-18 10:11:50 -07001005// file_getprop(file, key)
1006//
1007// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -07001008// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001009// and returns the value for 'key' (or "" if it isn't defined).
Tianjie Xu5419ad32018-02-02 16:49:15 -08001010Value* FileGetPropFn(const char* name, State* state,
1011 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -08001012 if (argv.size() != 2) {
1013 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1014 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -08001015 }
1016
1017 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001018 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -08001019 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1020 }
1021 const std::string& filename = args[0];
1022 const std::string& key = args[1];
1023
Tianjie Xu22f11202018-08-27 10:50:31 -07001024 std::string buffer;
Tianjie Xu1536db82019-05-14 10:54:43 -07001025 auto updater_runtime = state->updater->GetRuntime();
1026 if (!updater_runtime->ReadFileToString(filename, &buffer)) {
Tianjie Xu22f11202018-08-27 10:50:31 -07001027 ErrorAbort(state, kFreadFailure, "%s: failed to read %s", name, filename.c_str());
Tao Bao358c2ec2016-11-28 11:48:43 -08001028 return nullptr;
1029 }
1030
Tao Bao358c2ec2016-11-28 11:48:43 -08001031 std::vector<std::string> lines = android::base::Split(buffer, "\n");
1032 for (size_t i = 0; i < lines.size(); i++) {
1033 std::string line = android::base::Trim(lines[i]);
1034
1035 // comment or blank line: skip to next line
1036 if (line.empty() || line[0] == '#') {
1037 continue;
1038 }
1039 size_t equal_pos = line.find('=');
1040 if (equal_pos == std::string::npos) {
1041 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001042 }
1043
Tao Bao358c2ec2016-11-28 11:48:43 -08001044 // trim whitespace between key and '='
1045 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -07001046
Tao Bao358c2ec2016-11-28 11:48:43 -08001047 // not the key we're looking for
1048 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001049
Tao Bao358c2ec2016-11-28 11:48:43 -08001050 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
1051 }
Doug Zongker47cace92009-06-18 10:11:50 -07001052
Tao Bao358c2ec2016-11-28 11:48:43 -08001053 return StringValue("");
Doug Zongker8edb00c2009-06-11 17:21:44 -07001054}
1055
Doug Zongker8edb00c2009-06-11 17:21:44 -07001056// apply_patch_space(bytes)
Tianjie Xu5419ad32018-02-02 16:49:15 -08001057Value* ApplyPatchSpaceFn(const char* name, State* state,
1058 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -08001059 if (argv.size() != 1) {
1060 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name,
1061 argv.size());
1062 }
Tao Bao039f2da2016-11-22 16:29:50 -08001063 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001064 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001065 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1066 }
1067 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -08001068
Tao Bao039f2da2016-11-22 16:29:50 -08001069 size_t bytes;
1070 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -08001071 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name,
1072 bytes_str.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -08001073 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001074
Tianjie Xu99b73be2017-11-28 17:23:06 -08001075 // Skip the cache size check if the update is a retry.
Tao Bao5ee25662018-07-11 15:55:32 -07001076 if (state->is_retry || CheckAndFreeSpaceOnCache(bytes)) {
Tianjie Xu99b73be2017-11-28 17:23:06 -08001077 return StringValue("t");
Tao Bao039f2da2016-11-22 16:29:50 -08001078 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001079 return StringValue("");
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001080}
1081
Tianjie Xuc4447322017-03-06 14:44:59 -08001082Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1083 if (!argv.empty()) {
1084 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
1085 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001086 }
Tianjie Xu58d59122019-05-03 01:05:04 -07001087
Tianjie Xu1536db82019-05-14 10:54:43 -07001088 state->updater->WriteToCommandPipe("wipe_cache");
Tao Bao039f2da2016-11-22 16:29:50 -08001089 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -07001090}
1091
Tianjie Xuc4447322017-03-06 14:44:59 -08001092Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1093 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -08001094 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
1095 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001096
Tao Bao039f2da2016-11-22 16:29:50 -08001097 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001098 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001099 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1100 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001101
Tianjie Xu1536db82019-05-14 10:54:43 -07001102 auto updater_runtime = state->updater->GetRuntime();
1103 auto status = updater_runtime->RunProgram(args, false);
Tao Bao039f2da2016-11-22 16:29:50 -08001104 return StringValue(std::to_string(status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001105}
1106
Tao Bao511d7592018-06-19 15:56:49 -07001107// read_file(filename)
Tao Baobafd6c72018-07-09 15:08:50 -07001108// Reads a local file 'filename' and returns its contents as a string Value.
Tianjie Xuc4447322017-03-06 14:44:59 -08001109Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1110 if (argv.size() != 1) {
1111 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001112 }
Doug Zongker512536a2010-02-17 16:11:44 -08001113
Tao Bao039f2da2016-11-22 16:29:50 -08001114 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001115 if (!ReadArgs(state, argv, &args)) {
Tao Bao511d7592018-06-19 15:56:49 -07001116 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
Tao Bao039f2da2016-11-22 16:29:50 -08001117 }
1118 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -08001119
Tao Baobafd6c72018-07-09 15:08:50 -07001120 std::string contents;
Tianjie Xu1536db82019-05-14 10:54:43 -07001121 auto updater_runtime = state->updater->GetRuntime();
1122 if (updater_runtime->ReadFileToString(filename, &contents)) {
Tao Baobafd6c72018-07-09 15:08:50 -07001123 return new Value(Value::Type::STRING, std::move(contents));
Tao Bao039f2da2016-11-22 16:29:50 -08001124 }
Tao Bao511d7592018-06-19 15:56:49 -07001125
1126 // Leave it to caller to handle the failure.
Tao Baobafd6c72018-07-09 15:08:50 -07001127 PLOG(ERROR) << name << ": Failed to read " << filename;
Tao Bao511d7592018-06-19 15:56:49 -07001128 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -08001129}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001130
Tao Bao1bf17722016-11-03 23:52:01 -07001131// write_value(value, filename)
1132// Writes 'value' to 'filename'.
1133// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
Tianjie Xuc4447322017-03-06 14:44:59 -08001134Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1135 if (argv.size() != 2) {
1136 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1137 argv.size());
Tao Baod0f30882016-11-03 23:52:01 -07001138 }
Tao Bao1bf17722016-11-03 23:52:01 -07001139
Tao Baod0f30882016-11-03 23:52:01 -07001140 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001141 if (!ReadArgs(state, argv, &args)) {
Tao Baod0f30882016-11-03 23:52:01 -07001142 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1143 }
Tao Bao1bf17722016-11-03 23:52:01 -07001144
Tao Baod0f30882016-11-03 23:52:01 -07001145 const std::string& filename = args[1];
1146 if (filename.empty()) {
1147 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
1148 }
Tao Bao1bf17722016-11-03 23:52:01 -07001149
Tao Baod0f30882016-11-03 23:52:01 -07001150 const std::string& value = args[0];
Tianjie Xu1536db82019-05-14 10:54:43 -07001151 auto updater_runtime = state->updater->GetRuntime();
1152 if (!updater_runtime->WriteStringToFile(value, filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001153 PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\"";
Tao Baod0f30882016-11-03 23:52:01 -07001154 return StringValue("");
Tao Baod0f30882016-11-03 23:52:01 -07001155 }
Tianjie Xu1536db82019-05-14 10:54:43 -07001156 return StringValue("t");
Tao Bao1bf17722016-11-03 23:52:01 -07001157}
1158
Doug Zongkerc87bab12013-11-25 13:53:25 -08001159// Immediately reboot the device. Recovery is not finished normally,
1160// so if you reboot into recovery it will re-start applying the
1161// current package (because nothing has cleared the copy of the
1162// arguments stored in the BCB).
1163//
1164// The argument is the partition name passed to the android reboot
1165// property. It can be "recovery" to boot from the recovery
1166// partition, or "" (empty string) to boot from the regular boot
1167// partition.
Tianjie Xuc4447322017-03-06 14:44:59 -08001168Value* RebootNowFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1169 if (argv.size() != 2) {
1170 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1171 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001172 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001173
Tao Baobedf5fc2016-11-18 12:01:26 -08001174 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001175 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001176 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1177 }
1178 const std::string& filename = args[0];
1179 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001180
Tao Baobedf5fc2016-11-18 12:01:26 -08001181 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
1182 bootloader_message boot;
1183 std::string err;
1184 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001185 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001186 return StringValue("");
1187 }
1188 memset(boot.command, 0, sizeof(boot.command));
1189 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001190 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001191 return StringValue("");
1192 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001193
Tao Bao782dcc12019-04-29 11:23:16 -07001194 Reboot(property);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001195
Tao Baobedf5fc2016-11-18 12:01:26 -08001196 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001197}
1198
1199// Store a string value somewhere that future invocations of recovery
1200// can access it. This value is called the "stage" and can be used to
1201// drive packages that need to do reboots in the middle of
1202// installation and keep track of where they are in the multi-stage
1203// install.
1204//
1205// The first argument is the block device for the misc partition
1206// ("/misc" in the fstab), which is where this value is stored. The
1207// second argument is the string to store; it should not exceed 31
1208// bytes.
Tianjie Xuc4447322017-03-06 14:44:59 -08001209Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1210 if (argv.size() != 2) {
1211 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1212 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001213 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001214
Tao Baobedf5fc2016-11-18 12:01:26 -08001215 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001216 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001217 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1218 }
1219 const std::string& filename = args[0];
1220 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001221
Tao Baobedf5fc2016-11-18 12:01:26 -08001222 // Store this value in the misc partition, immediately after the
1223 // bootloader message that the main recovery uses to save its
1224 // arguments in case of the device restarting midway through
1225 // package installation.
1226 bootloader_message boot;
1227 std::string err;
1228 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001229 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001230 return StringValue("");
1231 }
1232 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
1233 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001234 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001235 return StringValue("");
1236 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001237
Tao Baobedf5fc2016-11-18 12:01:26 -08001238 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001239}
1240
1241// Return the value most recently saved with SetStageFn. The argument
1242// is the block device for the misc partition.
Tianjie Xuc4447322017-03-06 14:44:59 -08001243Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1244 if (argv.size() != 1) {
1245 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001246 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001247
Tao Baobedf5fc2016-11-18 12:01:26 -08001248 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001249 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001250 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1251 }
1252 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001253
Tao Baobedf5fc2016-11-18 12:01:26 -08001254 bootloader_message boot;
1255 std::string err;
1256 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001257 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001258 return StringValue("");
1259 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001260
Tao Baobedf5fc2016-11-18 12:01:26 -08001261 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001262}
1263
Tianjie Xuc4447322017-03-06 14:44:59 -08001264Value* WipeBlockDeviceFn(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 Bao358c2ec2016-11-28 11:48:43 -08001268 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001269
Tao Bao358c2ec2016-11-28 11:48:43 -08001270 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001271 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -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& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001276
Tao Bao358c2ec2016-11-28 11:48:43 -08001277 size_t len;
1278 if (!android::base::ParseUint(len_str.c_str(), &len)) {
1279 return nullptr;
1280 }
Tianjie Xu22f11202018-08-27 10:50:31 -07001281
Tianjie Xu1536db82019-05-14 10:54:43 -07001282 auto updater_runtime = state->updater->GetRuntime();
1283 int status = updater_runtime->WipeBlockDevice(filename, len);
1284 return StringValue(status == 0 ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001285}
1286
Tianjie Xuc4447322017-03-06 14:44:59 -08001287Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1288 if (!argv.empty()) {
1289 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
1290 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001291 }
Tianjie Xu1536db82019-05-14 10:54:43 -07001292 state->updater->WriteToCommandPipe("enable_reboot");
Tao Bao039f2da2016-11-22 16:29:50 -08001293 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) {
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001297#ifdef HAVE_LIBTUNE2FS
Tianjie Xuc4447322017-03-06 14:44:59 -08001298 if (argv.empty()) {
1299 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001300 }
Michael Rungeb278c252014-11-21 00:12:28 -08001301
Tao Bao039f2da2016-11-22 16:29:50 -08001302 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001303 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001304 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
1305 }
Michael Rungeb278c252014-11-21 00:12:28 -08001306
Tao Bao3d69f0d2018-12-20 09:44:06 -08001307 // tune2fs expects the program name as its first arg.
1308 args.insert(args.begin(), "tune2fs");
Tianjie Xu1536db82019-05-14 10:54:43 -07001309 auto updater_runtime = state->updater->GetRuntime();
1310 if (auto result = updater_runtime->Tune2Fs(args); result != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -08001311 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
1312 }
1313 return StringValue("t");
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001314#else
Ethan Yonker8373cfe2017-09-08 06:50:54 -05001315 return ErrorAbort(state, kTune2FsFailure, "%s() support not present, no libtune2fs", name);
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001316#endif // HAVE_LIBTUNE2FS
Michael Rungeb278c252014-11-21 00:12:28 -08001317}
1318
Yifan Hongdff80042020-04-28 13:31:11 -07001319Value* AddSlotSuffixFn(const char* name, State* state,
1320 const std::vector<std::unique_ptr<Expr>>& argv) {
1321 if (argv.size() != 1) {
1322 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
1323 }
1324 std::vector<std::string> args;
1325 if (!ReadArgs(state, argv, &args)) {
1326 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1327 }
1328 const std::string& arg = args[0];
1329 auto updater_runtime = state->updater->GetRuntime();
1330 return StringValue(updater_runtime->AddSlotSuffix(arg));
1331}
1332
Doug Zongker9931f7f2009-06-10 14:11:53 -07001333void RegisterInstallFunctions() {
Tao Bao039f2da2016-11-22 16:29:50 -08001334 RegisterFunction("mount", MountFn);
1335 RegisterFunction("is_mounted", IsMountedFn);
1336 RegisterFunction("unmount", UnmountFn);
1337 RegisterFunction("format", FormatFn);
1338 RegisterFunction("show_progress", ShowProgressFn);
1339 RegisterFunction("set_progress", SetProgressFn);
Tom Marshallbf4f24f2017-08-23 18:14:00 +00001340 RegisterFunction("delete", DeleteFn);
1341 RegisterFunction("delete_recursive", DeleteFn);
Tom Marshall981118e2017-10-25 20:27:08 +02001342 RegisterFunction("package_extract_dir", PackageExtractDirFn);
Tao Bao039f2da2016-11-22 16:29:50 -08001343 RegisterFunction("package_extract_file", PackageExtractFileFn);
Tom Marshallbf4f24f2017-08-23 18:14:00 +00001344 RegisterFunction("symlink", SymlinkFn);
1345
1346 // Usage:
1347 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1348 // Example:
1349 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel",
1350 // "u:object_r:system_file:s0", "capabilities", 0x0);
1351 RegisterFunction("set_metadata", SetMetadataFn);
1352
1353 // Usage:
1354 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1355 // Example:
1356 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755,
1357 // "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1358 RegisterFunction("set_metadata_recursive", SetMetadataFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001359
Tao Bao039f2da2016-11-22 16:29:50 -08001360 RegisterFunction("getprop", GetPropFn);
1361 RegisterFunction("file_getprop", FileGetPropFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001362
Tao Bao039f2da2016-11-22 16:29:50 -08001363 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Tao Bao5609bc82018-06-20 00:30:48 -07001364 RegisterFunction("patch_partition", PatchPartitionFn);
1365 RegisterFunction("patch_partition_check", PatchPartitionCheckFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001366
Tao Bao039f2da2016-11-22 16:29:50 -08001367 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001368
Tao Bao039f2da2016-11-22 16:29:50 -08001369 RegisterFunction("read_file", ReadFileFn);
Tao Bao039f2da2016-11-22 16:29:50 -08001370 RegisterFunction("write_value", WriteValueFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001371
Tao Bao039f2da2016-11-22 16:29:50 -08001372 RegisterFunction("wipe_cache", WipeCacheFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001373
Tao Bao039f2da2016-11-22 16:29:50 -08001374 RegisterFunction("ui_print", UIPrintFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001375
Tao Bao039f2da2016-11-22 16:29:50 -08001376 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001377
Tao Bao039f2da2016-11-22 16:29:50 -08001378 RegisterFunction("reboot_now", RebootNowFn);
1379 RegisterFunction("get_stage", GetStageFn);
1380 RegisterFunction("set_stage", SetStageFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001381
Tao Bao039f2da2016-11-22 16:29:50 -08001382 RegisterFunction("enable_reboot", EnableRebootFn);
1383 RegisterFunction("tune2fs", Tune2FsFn);
Yifan Hongdff80042020-04-28 13:31:11 -07001384
1385 RegisterFunction("add_slot_suffix", AddSlotSuffixFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001386}