blob: bcc859a1e20711fdb2ae4bc6f1f26687997d6f64 [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>
Tao Bao0d3f84f2016-12-28 15:09:20 -080049#include <applypatch/applypatch.h>
50#include <bootloader_message/bootloader_message.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070051#include <cutils/android_reboot.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"
Elliott Hughes63a31922016-06-09 17:41:22 -070060#include "mounts.h"
Ethan Yonker8373cfe2017-09-08 06:50:54 -050061
Doug Zongker512536a2010-02-17 16:11:44 -080062#include "applypatch/applypatch.h"
Dees_Troy512376c2013-09-03 19:39:41 +000063#include "flashutils/flashutils.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080064#include "install.h"
Ethan Yonker7e1b9862015-03-19 14:10:01 -050065#ifdef HAVE_LIBTUNE2FS
Michael Rungeb278c252014-11-21 00:12:28 -080066#include "tune2fs.h"
Ethan Yonker7e1b9862015-03-19 14:10:01 -050067#endif
Doug Zongker8edb00c2009-06-11 17:21:44 -070068
Doug Zongker3d177d02010-07-01 09:18:44 -070069#ifdef USE_EXT4
70#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080071#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070072#endif
73
Tom Marshall981118e2017-10-25 20:27:08 +020074#include "otautil/ZipUtil.h"
Tao Baod33b2f82017-09-28 21:29:11 -070075#include "otafault/ota_io.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070076#include "otautil/DirUtil.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070077#include "otautil/error_code.h"
Tao Bao09e468f2017-09-29 14:39:33 -070078#include "otautil/print_sha1.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070079#include "updater/updater.h"
Doug Zongker52b40362014-02-10 15:30:30 -080080
Tao Bao1107d962015-09-09 17:16:55 -070081// Send over the buffer to recovery though the command pipe.
82static void uiPrint(State* state, const std::string& buffer) {
Tao Bao039f2da2016-11-22 16:29:50 -080083 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070084
Tao Bao039f2da2016-11-22 16:29:50 -080085 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
86 // So skip sending empty strings to UI.
87 std::vector<std::string> lines = android::base::Split(buffer, "\n");
88 for (auto& line : lines) {
89 if (!line.empty()) {
90 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
Michael Runge75480252014-10-22 19:48:41 -070091 }
Tao Bao039f2da2016-11-22 16:29:50 -080092 }
Tao Bao1107d962015-09-09 17:16:55 -070093
Tao Bao039f2da2016-11-22 16:29:50 -080094 // On the updater side, we need to dump the contents to stderr (which has
95 // been redirected to the log file). Because the recovery will only print
96 // the contents to screen when processing pipe command ui_print.
97 LOG(INFO) << buffer;
Michael Runge75480252014-10-22 19:48:41 -070098}
99
Simon Shields2e9747f2018-08-09 01:17:21 +1000100static bool is_dir(const std::string& dirpath) {
101 struct stat st;
102 return stat(dirpath.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
103}
104
105// Create all parent directories of name, if necessary.
106static bool make_parents(const std::string& name) {
107 size_t prev_end = 0;
108 while (prev_end < name.size()) {
109 size_t next_end = name.find('/', prev_end + 1);
110 if (next_end == std::string::npos) {
111 break;
112 }
113 std::string dir_path = name.substr(0, next_end);
114 if (!is_dir(dir_path)) {
115 int result = mkdir(dir_path.c_str(), 0700);
116 if (result != 0) {
117 PLOG(ERROR) << "failed to mkdir " << dir_path << " when make parents for " << name;
118 return false;
119 }
120
121 LOG(INFO) << "created [" << dir_path << "]";
122 }
123 prev_end = next_end;
124 }
125 return true;
126}
127
Elliott Hughes83ce7552016-06-30 09:28:42 -0700128void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) {
Tao Bao039f2da2016-11-22 16:29:50 -0800129 std::string error_msg;
Tao Bao1107d962015-09-09 17:16:55 -0700130
Tao Bao039f2da2016-11-22 16:29:50 -0800131 va_list ap;
132 va_start(ap, format);
133 android::base::StringAppendV(&error_msg, format, ap);
134 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -0700135
Tao Bao039f2da2016-11-22 16:29:50 -0800136 uiPrint(state, error_msg);
Michael Runge75480252014-10-22 19:48:41 -0700137}
138
Tianjie Xu5419ad32018-02-02 16:49:15 -0800139// This is the updater side handler for ui_print() in edify script. Contents will be sent over to
140// the recovery side for on-screen display.
141Value* UIPrintFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
142 std::vector<std::string> args;
143 if (!ReadArgs(state, argv, &args)) {
144 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
145 }
146
147 std::string buffer = android::base::Join(args, "");
148 uiPrint(state, buffer);
149 return StringValue(buffer);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700150}
151
Tianjie Xu5419ad32018-02-02 16:49:15 -0800152// apply_patch(src_file, tgt_file, tgt_sha1, tgt_size, patch1_sha1, patch1_blob, [...])
153// Applies a binary patch to the src_file to produce the tgt_file. If the desired target is the
154// same as the source, pass "-" for tgt_file. tgt_sha1 and tgt_size are the expected final SHA1
155// hash and size of the target file. The remaining arguments must come in pairs: a SHA1 hash (a
156// 40-character hex string) and a blob. The blob is the patch to be applied when the source
157// file's current contents have the given SHA1.
158//
159// The patching is done in a safe manner that guarantees the target file either has the desired
160// SHA1 hash and size, or it is untouched -- it will not be left in an unrecoverable intermediate
161// state. If the process is interrupted during patching, the target file may be in an intermediate
162// state; a copy exists in the cache partition so restarting the update can successfully update
163// the file.
164Value* ApplyPatchFn(const char* name, State* state,
165 const std::vector<std::unique_ptr<Expr>>& argv) {
166 if (argv.size() < 6 || (argv.size() % 2) == 1) {
167 return ErrorAbort(state, kArgsParsingFailure,
168 "%s(): expected at least 6 args and an "
169 "even number, got %zu",
170 name, argv.size());
171 }
172
173 std::vector<std::string> args;
174 if (!ReadArgs(state, argv, &args, 0, 4)) {
175 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
176 }
177 const std::string& source_filename = args[0];
178 const std::string& target_filename = args[1];
179 const std::string& target_sha1 = args[2];
180 const std::string& target_size_str = args[3];
181
182 size_t target_size;
183 if (!android::base::ParseUint(target_size_str.c_str(), &target_size)) {
184 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name,
185 target_size_str.c_str());
186 }
187
188 int patchcount = (argv.size() - 4) / 2;
189 std::vector<std::unique_ptr<Value>> arg_values;
190 if (!ReadValueArgs(state, argv, &arg_values, 4, argv.size() - 4)) {
191 return nullptr;
192 }
193
194 for (int i = 0; i < patchcount; ++i) {
195 if (arg_values[i * 2]->type != VAL_STRING) {
196 return ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name, i * 2);
197 }
198 if (arg_values[i * 2 + 1]->type != VAL_BLOB) {
199 return ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name, i * 2 + 1);
200 }
201 }
202
203 std::vector<std::string> patch_sha_str;
204 std::vector<std::unique_ptr<Value>> patches;
205 for (int i = 0; i < patchcount; ++i) {
206 patch_sha_str.push_back(arg_values[i * 2]->data);
207 patches.push_back(std::move(arg_values[i * 2 + 1]));
208 }
209
210 int result = applypatch(source_filename.c_str(), target_filename.c_str(), target_sha1.c_str(),
211 target_size, patch_sha_str, patches, nullptr);
212
213 return StringValue(result == 0 ? "t" : "");
214}
215
216// apply_patch_check(filename, [sha1, ...])
217// Returns true if the contents of filename or the temporary copy in the cache partition (if
218// present) have a SHA-1 checksum equal to one of the given sha1 values. sha1 values are
219// specified as 40 hex digits. This function differs from sha1_check(read_file(filename),
220// sha1 [, ...]) in that it knows to check the cache partition copy, so apply_patch_check() will
221// succeed even if the file was corrupted by an interrupted apply_patch() update.
222Value* ApplyPatchCheckFn(const char* name, State* state,
223 const std::vector<std::unique_ptr<Expr>>& argv) {
224 if (argv.size() < 1) {
225 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %zu", name,
226 argv.size());
227 }
228
229 std::vector<std::string> args;
230 if (!ReadArgs(state, argv, &args, 0, 1)) {
231 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
232 }
233 const std::string& filename = args[0];
234
235 std::vector<std::string> sha1s;
236 if (argv.size() > 1 && !ReadArgs(state, argv, &sha1s, 1, argv.size() - 1)) {
237 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
238 }
239 int result = applypatch_check(filename.c_str(), sha1s);
240
241 return StringValue(result == 0 ? "t" : "");
242}
243
244// sha1_check(data)
245// to return the sha1 of the data (given in the format returned by
246// read_file).
247//
248// sha1_check(data, sha1_hex, [sha1_hex, ...])
249// returns the sha1 of the file if it matches any of the hex
250// strings passed, or "" if it does not equal any of them.
251//
252Value* Sha1CheckFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
253 if (argv.size() < 1) {
254 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
255 }
256
257 std::vector<std::unique_ptr<Value>> args;
258 if (!ReadValueArgs(state, argv, &args)) {
259 return nullptr;
260 }
261
262 if (args[0]->type == VAL_INVALID) {
263 return StringValue("");
264 }
265 uint8_t digest[SHA_DIGEST_LENGTH];
266 SHA1(reinterpret_cast<const uint8_t*>(args[0]->data.c_str()), args[0]->data.size(), digest);
267
268 if (argv.size() == 1) {
269 return StringValue(print_sha1(digest));
270 }
271
272 for (size_t i = 1; i < argv.size(); ++i) {
273 uint8_t arg_digest[SHA_DIGEST_LENGTH];
274 if (args[i]->type != VAL_STRING) {
275 LOG(ERROR) << name << "(): arg " << i << " is not a string; skipping";
276 } else if (ParseSha1(args[i]->data.c_str(), arg_digest) != 0) {
277 // Warn about bad args and skip them.
278 LOG(ERROR) << name << "(): error parsing \"" << args[i]->data << "\" as sha-1; skipping";
279 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
280 // Found a match.
281 return args[i].release();
282 }
283 }
284
285 // Didn't match any of the hex strings; return false.
286 return StringValue("");
Doug Zongkera23075f2012-08-06 16:19:09 -0700287}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700288
Doug Zongker9931f7f2009-06-10 14:11:53 -0700289// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -0700290// mount(fs_type, partition_type, location, mount_point, mount_options)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700291
Doug Zongker9931f7f2009-06-10 14:11:53 -0700292// fs_type="ext4" partition_type="EMMC" location=device
Tianjie Xuc4447322017-03-06 14:44:59 -0800293Value* MountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
294 if (argv.size() != 4 && argv.size() != 5) {
295 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %zu", name,
296 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800297 }
298
299 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800300 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800301 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
302 }
303 const std::string& fs_type = args[0];
304 const std::string& partition_type = args[1];
305 const std::string& location = args[2];
306 const std::string& mount_point = args[3];
307 std::string mount_options;
308
Tianjie Xuc4447322017-03-06 14:44:59 -0800309 if (argv.size() == 5) {
Tao Bao039f2da2016-11-22 16:29:50 -0800310 mount_options = args[4];
311 }
312
313 if (fs_type.empty()) {
314 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
315 }
316 if (partition_type.empty()) {
317 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
318 name);
319 }
320 if (location.empty()) {
321 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
322 }
323 if (mount_point.empty()) {
324 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
325 name);
326 }
327
328 {
329 char* secontext = nullptr;
330
331 if (sehandle) {
332 selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755);
333 setfscreatecon(secontext);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700334 }
335
Tao Bao039f2da2016-11-22 16:29:50 -0800336 mkdir(mount_point.c_str(), 0755);
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700337
Tao Bao039f2da2016-11-22 16:29:50 -0800338 if (secontext) {
339 freecon(secontext);
340 setfscreatecon(nullptr);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700341 }
Tao Bao039f2da2016-11-22 16:29:50 -0800342 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700343
Tao Bao039f2da2016-11-22 16:29:50 -0800344 if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(),
345 MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700346 uiPrintf(state, "%s: Failed to mount %s at %s: %s", name, location.c_str(), mount_point.c_str(),
347 strerror(errno));
Tao Bao039f2da2016-11-22 16:29:50 -0800348 return StringValue("");
349 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700350
Tao Bao039f2da2016-11-22 16:29:50 -0800351 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700352}
353
Doug Zongker9931f7f2009-06-10 14:11:53 -0700354// is_mounted(mount_point)
Tianjie Xuc4447322017-03-06 14:44:59 -0800355Value* IsMountedFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
356 if (argv.size() != 1) {
357 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800358 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700359
Tao Bao039f2da2016-11-22 16:29:50 -0800360 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800361 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800362 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
363 }
364 const std::string& mount_point = args[0];
365 if (mount_point.empty()) {
366 return ErrorAbort(state, kArgsParsingFailure,
367 "mount_point argument to unmount() can't be empty");
368 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700369
Tao Bao039f2da2016-11-22 16:29:50 -0800370 scan_mounted_volumes();
371 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
372 if (vol == nullptr) {
373 return StringValue("");
374 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700375
Tao Bao039f2da2016-11-22 16:29:50 -0800376 return StringValue(mount_point);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700377}
378
Tianjie Xuc4447322017-03-06 14:44:59 -0800379Value* UnmountFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
380 if (argv.size() != 1) {
381 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800382 }
383 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800384 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800385 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
386 }
387 const std::string& mount_point = args[0];
388 if (mount_point.empty()) {
389 return ErrorAbort(state, kArgsParsingFailure,
390 "mount_point argument to unmount() can't be empty");
391 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700392
Tao Bao039f2da2016-11-22 16:29:50 -0800393 scan_mounted_volumes();
394 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
395 if (vol == nullptr) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700396 uiPrintf(state, "Failed to unmount %s: No such volume", mount_point.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -0800397 return nullptr;
398 } else {
399 int ret = unmount_mounted_volume(vol);
400 if (ret != 0) {
Tao Bao0bbc7642017-03-29 23:57:47 -0700401 uiPrintf(state, "Failed to unmount %s: %s", mount_point.c_str(), strerror(errno));
Nick Kralevich6a821fe2014-10-23 20:36:42 -0700402 }
Tao Bao039f2da2016-11-22 16:29:50 -0800403 }
Nick Kralevich6a821fe2014-10-23 20:36:42 -0700404
Tao Bao039f2da2016-11-22 16:29:50 -0800405 return StringValue(mount_point);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700406}
407
JP Abgrall37aedb32014-06-16 19:07:39 -0700408static int exec_cmd(const char* path, char* const argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800409 pid_t child;
410 if ((child = vfork()) == 0) {
411 execv(path, argv);
Tao Bao3da88012017-02-03 13:09:23 -0800412 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -0800413 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700414
Tao Bao039f2da2016-11-22 16:29:50 -0800415 int status;
416 waitpid(child, &status, 0);
417 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
418 LOG(ERROR) << path << " failed with status " << WEXITSTATUS(status);
419 }
420 return WEXITSTATUS(status);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700421}
422
Doug Zongker9931f7f2009-06-10 14:11:53 -0700423// format(fs_type, partition_type, location, fs_size, mount_point)
424//
Tao Bao039f2da2016-11-22 16:29:50 -0800425// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
426// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700427// if fs_size == 0, then make fs uses the entire partition.
Doug Zongker9931f7f2009-06-10 14:11:53 -0700428// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700429// 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 -0800430Value* FormatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
431 if (argv.size() != 5) {
432 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %zu", name,
433 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800434 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700435
Tao Bao039f2da2016-11-22 16:29:50 -0800436 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800437 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800438 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
439 }
440 const std::string& fs_type = args[0];
441 const std::string& partition_type = args[1];
442 const std::string& location = args[2];
443 const std::string& fs_size = args[3];
444 const std::string& mount_point = args[4];
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700445
Tao Bao039f2da2016-11-22 16:29:50 -0800446 if (fs_type.empty()) {
447 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
448 }
449 if (partition_type.empty()) {
450 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
451 name);
452 }
453 if (location.empty()) {
454 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
455 }
456 if (mount_point.empty()) {
457 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
458 name);
459 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700460
Tao Bao039f2da2016-11-22 16:29:50 -0800461 int64_t size;
462 if (!android::base::ParseInt(fs_size, &size)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800463 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800464 fs_size.c_str());
465 }
466
467 if (fs_type == "ext4") {
Jin Qianac318082017-04-21 14:36:12 -0700468 const char* mke2fs_argv[] = { "/sbin/mke2fs_static", "-t", "ext4", "-b", "4096",
469 location.c_str(), nullptr, nullptr };
470 std::string size_str;
471 if (size != 0) {
472 size_str = std::to_string(size / 4096LL);
473 mke2fs_argv[6] = size_str.c_str();
474 }
475
476 int status = exec_cmd(mke2fs_argv[0], const_cast<char**>(mke2fs_argv));
Tao Bao039f2da2016-11-22 16:29:50 -0800477 if (status != 0) {
Jin Qian502fd1c2017-11-02 11:58:12 -0700478 LOG(ERROR) << name << ": mke2fs failed (" << status << ") on " << location;
479 return StringValue("");
Jin Qianac318082017-04-21 14:36:12 -0700480 }
481
Tao Bao7af933b2017-07-11 16:45:04 -0700482 const char* e2fsdroid_argv[] = { "/sbin/e2fsdroid_static", "-e", "-a", mount_point.c_str(),
Jin Qianac318082017-04-21 14:36:12 -0700483 location.c_str(), nullptr };
484 status = exec_cmd(e2fsdroid_argv[0], const_cast<char**>(e2fsdroid_argv));
485 if (status != 0) {
486 LOG(ERROR) << name << ": e2fsdroid failed (" << status << ") on " << location;
Tao Bao039f2da2016-11-22 16:29:50 -0800487 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700488 }
Tao Bao039f2da2016-11-22 16:29:50 -0800489 return StringValue(location);
490 } else if (fs_type == "f2fs") {
491 if (size < 0) {
492 LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size;
493 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700494 }
Tao Bao039f2da2016-11-22 16:29:50 -0800495 std::string num_sectors = std::to_string(size / 512);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700496
Tao Bao039f2da2016-11-22 16:29:50 -0800497 const char* f2fs_path = "/sbin/mkfs.f2fs";
Jaegeuk Kim1a8bb0f2017-11-01 13:24:43 -0700498 const char* f2fs_argv[] = { "mkfs.f2fs",
499 "-d1",
500 "-f",
Jaegeuk Kim2e5dc842018-04-05 22:42:13 -0700501 "-O", "encrypt",
502 "-O", "quota",
503 "-O", "verity",
504 "-w", "512",
Jaegeuk Kim1a8bb0f2017-11-01 13:24:43 -0700505 location.c_str(),
506 (size < 512) ? nullptr : num_sectors.c_str(),
507 nullptr };
Tao Bao397a8132017-05-12 11:02:27 -0700508 int status = exec_cmd(f2fs_path, const_cast<char**>(f2fs_argv));
Tao Bao039f2da2016-11-22 16:29:50 -0800509 if (status != 0) {
510 LOG(ERROR) << name << ": mkfs.f2fs failed (" << status << ") on " << location;
511 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700512 }
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800513
514 const char* sload_argv[] = { "/sbin/sload.f2fs", "-t", mount_point.c_str(), location.c_str(),
515 nullptr };
516 status = exec_cmd(sload_argv[0], const_cast<char**>(sload_argv));
517 if (status != 0) {
518 LOG(ERROR) << name << ": sload.f2fs failed (" << status << ") on " << location;
519 return StringValue("");
520 }
521
Tao Bao039f2da2016-11-22 16:29:50 -0800522 return StringValue(location);
523 } else {
524 LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \""
525 << partition_type << "\"";
526 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700527
Tao Bao039f2da2016-11-22 16:29:50 -0800528 return nullptr;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700529}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700530
Tom Marshallbf4f24f2017-08-23 18:14:00 +0000531// rename(src_name, dst_name)
532// Renames src_name to dst_name. It automatically creates the necessary directories for dst_name.
533// Example: rename("system/app/Hangouts/Hangouts.apk", "system/priv-app/Hangouts/Hangouts.apk")
534Value* RenameFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
535 if (argv.size() != 2) {
536 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
537 argv.size());
538 }
539
540 std::vector<std::string> args;
541 if (!ReadArgs(state, argv, &args)) {
542 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
543 }
544 const std::string& src_name = args[0];
545 const std::string& dst_name = args[1];
546
547 if (src_name.empty()) {
548 return ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", name);
549 }
550 if (dst_name.empty()) {
551 return ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", name);
552 }
553 if (!make_parents(dst_name)) {
554 return ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
555 dst_name.c_str(), strerror(errno));
556 } else if (access(dst_name.c_str(), F_OK) == 0 && access(src_name.c_str(), F_OK) != 0) {
557 // File was already moved
558 return StringValue(dst_name);
559 } else if (rename(src_name.c_str(), dst_name.c_str()) != 0) {
560 return ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
561 src_name.c_str(), dst_name.c_str(), strerror(errno));
562 }
563
564 return StringValue(dst_name);
565}
566
567// delete([filename, ...])
568// Deletes all the filenames listed. Returns the number of files successfully deleted.
569//
570// delete_recursive([dirname, ...])
571// Recursively deletes dirnames and all their contents. Returns the number of directories
572// successfully deleted.
573Value* DeleteFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
574 std::vector<std::string> paths;
575 if (!ReadArgs(state, argv, &paths)) {
576 return nullptr;
577 }
578
579 bool recursive = (strcmp(name, "delete_recursive") == 0);
580
581 int success = 0;
582 for (const auto& path : paths) {
583 if ((recursive ? dirUnlinkHierarchy(path.c_str()) : unlink(path.c_str())) == 0) {
584 ++success;
585 }
586 }
587
588 return StringValue(std::to_string(success));
589}
590
591
Tianjie Xuc4447322017-03-06 14:44:59 -0800592Value* ShowProgressFn(const char* name, State* state,
593 const std::vector<std::unique_ptr<Expr>>& argv) {
594 if (argv.size() != 2) {
595 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
596 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800597 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700598
Tao Bao039f2da2016-11-22 16:29:50 -0800599 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800600 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800601 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
602 }
603 const std::string& frac_str = args[0];
604 const std::string& sec_str = args[1];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700605
Tao Bao039f2da2016-11-22 16:29:50 -0800606 double frac;
607 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800608 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800609 frac_str.c_str());
610 }
611 int sec;
612 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800613 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800614 sec_str.c_str());
615 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700616
Tao Bao039f2da2016-11-22 16:29:50 -0800617 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
618 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700619
Tao Bao039f2da2016-11-22 16:29:50 -0800620 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700621}
622
Tianjie Xu5419ad32018-02-02 16:49:15 -0800623Value* SetProgressFn(const char* name, State* state,
624 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -0800625 if (argv.size() != 1) {
626 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -0800627 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700628
Tao Bao039f2da2016-11-22 16:29:50 -0800629 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800630 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -0800631 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
632 }
633 const std::string& frac_str = args[0];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700634
Tao Bao039f2da2016-11-22 16:29:50 -0800635 double frac;
636 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -0800637 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s", name,
Tao Bao039f2da2016-11-22 16:29:50 -0800638 frac_str.c_str());
639 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700640
Tao Bao039f2da2016-11-22 16:29:50 -0800641 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
642 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700643
Tao Bao039f2da2016-11-22 16:29:50 -0800644 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700645}
646
Tom Marshall981118e2017-10-25 20:27:08 +0200647// package_extract_dir(package_dir, dest_dir)
648// Extracts all files from the package underneath package_dir and writes them to the
649// corresponding tree beneath dest_dir. Any existing files are overwritten.
650// Example: package_extract_dir("system", "/system")
651//
652// Note: package_dir needs to be a relative path; dest_dir needs to be an absolute path.
653Value* PackageExtractDirFn(const char* name, State* state,
654 const std::vector<std::unique_ptr<Expr>>&argv) {
655 if (argv.size() != 2) {
656 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
657 argv.size());
658 }
659
660 std::vector<std::string> args;
661 if (!ReadArgs(state, argv, &args)) {
662 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
663 }
664 const std::string& zip_path = args[0];
665 const std::string& dest_path = args[1];
666
667 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
668
669 // To create a consistent system image, never use the clock for timestamps.
670 constexpr struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
671
672 bool success = ExtractPackageRecursive(za, zip_path, dest_path, &timestamp, sehandle);
673
674 return StringValue(success ? "t" : "");
675}
676
Tao Baoef0eb3b2016-11-14 21:29:52 -0800677// package_extract_file(package_file[, dest_file])
678// Extracts a single package_file from the update package and writes it to dest_file,
679// overwriting existing files if necessary. Without the dest_file argument, returns the
680// contents of the package file as a binary blob.
Tianjie Xuc4447322017-03-06 14:44:59 -0800681Value* PackageExtractFileFn(const char* name, State* state,
682 const std::vector<std::unique_ptr<Expr>>& argv) {
683 if (argv.size() < 1 || argv.size() > 2) {
684 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %zu", name,
685 argv.size());
Tao Baoef0eb3b2016-11-14 21:29:52 -0800686 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700687
Tianjie Xuc4447322017-03-06 14:44:59 -0800688 if (argv.size() == 2) {
Tao Baoef0eb3b2016-11-14 21:29:52 -0800689 // The two-argument version extracts to a file.
690
691 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800692 if (!ReadArgs(state, argv, &args)) {
693 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
694 argv.size());
Doug Zongker8edb00c2009-06-11 17:21:44 -0700695 }
Tao Baoef0eb3b2016-11-14 21:29:52 -0800696 const std::string& zip_path = args[0];
697 const std::string& dest_path = args[1];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700698
Tao Baoef0eb3b2016-11-14 21:29:52 -0800699 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
700 ZipString zip_string_path(zip_path.c_str());
701 ZipEntry entry;
702 if (FindEntry(za, zip_string_path, &entry) != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800703 LOG(ERROR) << name << ": no " << zip_path << " in package";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800704 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700705 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700706
Tao Bao358c2ec2016-11-28 11:48:43 -0800707 unique_fd fd(TEMP_FAILURE_RETRY(
708 ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tao Baoef0eb3b2016-11-14 21:29:52 -0800709 if (fd == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800710 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800711 return StringValue("");
712 }
713
714 bool success = true;
715 int32_t ret = ExtractEntryToFile(za, &entry, fd);
716 if (ret != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800717 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
718 << entry.uncompressed_length << " bytes) to \"" << dest_path
719 << "\": " << ErrorCodeString(ret);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800720 success = false;
721 }
722 if (ota_fsync(fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800723 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800724 success = false;
725 }
726 if (ota_close(fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800727 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800728 success = false;
729 }
730
731 return StringValue(success ? "t" : "");
732 } else {
733 // The one-argument version returns the contents of the file as the result.
734
735 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -0800736 if (!ReadArgs(state, argv, &args)) {
737 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %zu args", name,
738 argv.size());
Tao Baoef0eb3b2016-11-14 21:29:52 -0800739 }
740 const std::string& zip_path = args[0];
741
742 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
743 ZipString zip_string_path(zip_path.c_str());
744 ZipEntry entry;
745 if (FindEntry(za, zip_string_path, &entry) != 0) {
746 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
747 zip_path.c_str());
748 }
749
750 std::string buffer;
751 buffer.resize(entry.uncompressed_length);
752
753 int32_t ret = ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
754 if (ret != 0) {
755 return ErrorAbort(state, kPackageExtractFileFailure,
756 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
757 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
758 }
759
760 return new Value(VAL_BLOB, buffer);
761 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700762}
763
Tom Marshallbf4f24f2017-08-23 18:14:00 +0000764// symlink(target, [src1, src2, ...])
765// Creates all sources as symlinks to target. It unlinks any previously existing src1, src2, etc
766// before creating symlinks.
767Value* SymlinkFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
768 if (argv.size() == 0) {
769 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %zu", name, argv.size());
770 }
771 std::string target;
772 if (!Evaluate(state, argv[0], &target)) {
773 return nullptr;
774 }
775
776 std::vector<std::string> srcs;
777 if (!ReadArgs(state, argv, &srcs, 1, argv.size())) {
778 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
779 }
780
781 size_t bad = 0;
782 for (const auto& src : srcs) {
783 if (unlink(src.c_str()) == -1 && errno != ENOENT) {
784 PLOG(ERROR) << name << ": failed to remove " << src;
785 ++bad;
786 } else if (!make_parents(src)) {
787 LOG(ERROR) << name << ": failed to symlink " << src << " to " << target
788 << ": making parents failed";
789 ++bad;
790 } else if (symlink(target.c_str(), src.c_str()) == -1) {
791 PLOG(ERROR) << name << ": failed to symlink " << src << " to " << target;
792 ++bad;
793 }
794 }
795 if (bad != 0) {
796 return ErrorAbort(state, kSymlinkFailure, "%s: Failed to create %zu symlink(s)", name, bad);
797 }
798 return StringValue("t");
799}
800
801struct perm_parsed_args {
802 bool has_uid;
803 uid_t uid;
804 bool has_gid;
805 gid_t gid;
806 bool has_mode;
807 mode_t mode;
808 bool has_fmode;
809 mode_t fmode;
810 bool has_dmode;
811 mode_t dmode;
812 bool has_selabel;
813 const char* selabel;
814 bool has_capabilities;
815 uint64_t capabilities;
816};
817
818static struct perm_parsed_args ParsePermArgs(State * state,
819 const std::vector<std::string>& args) {
820 struct perm_parsed_args parsed;
821 int bad = 0;
822 static int max_warnings = 20;
823
824 memset(&parsed, 0, sizeof(parsed));
825
826 for (size_t i = 1; i < args.size(); i += 2) {
827 if (args[i] == "uid") {
828 int64_t uid;
829 if (sscanf(args[i + 1].c_str(), "%" SCNd64, &uid) == 1) {
830 parsed.uid = uid;
831 parsed.has_uid = true;
832 } else {
833 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1].c_str());
834 bad++;
835 }
836 continue;
837 }
838 if (args[i] == "gid") {
839 int64_t gid;
840 if (sscanf(args[i + 1].c_str(), "%" SCNd64, &gid) == 1) {
841 parsed.gid = gid;
842 parsed.has_gid = true;
843 } else {
844 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1].c_str());
845 bad++;
846 }
847 continue;
848 }
849 if (args[i] == "mode") {
850 int32_t mode;
851 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
852 parsed.mode = mode;
853 parsed.has_mode = true;
854 } else {
855 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1].c_str());
856 bad++;
857 }
858 continue;
859 }
860 if (args[i] == "dmode") {
861 int32_t mode;
862 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
863 parsed.dmode = mode;
864 parsed.has_dmode = true;
865 } else {
866 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1].c_str());
867 bad++;
868 }
869 continue;
870 }
871 if (args[i] == "fmode") {
872 int32_t mode;
873 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
874 parsed.fmode = mode;
875 parsed.has_fmode = true;
876 } else {
877 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1].c_str());
878 bad++;
879 }
880 continue;
881 }
882 if (args[i] == "capabilities") {
883 int64_t capabilities;
884 if (sscanf(args[i + 1].c_str(), "%" SCNi64, &capabilities) == 1) {
885 parsed.capabilities = capabilities;
886 parsed.has_capabilities = true;
887 } else {
888 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1].c_str());
889 bad++;
890 }
891 continue;
892 }
893 if (args[i] == "selabel") {
894 if (!args[i + 1].empty()) {
895 parsed.selabel = args[i + 1].c_str();
896 parsed.has_selabel = true;
897 } else {
898 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1].c_str());
899 bad++;
900 }
901 continue;
902 }
903 if (max_warnings != 0) {
904 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i].c_str());
905 max_warnings--;
906 if (max_warnings == 0) {
907 LOG(INFO) << "ParsedPermArgs: suppressing further warnings";
908 }
909 }
910 }
911 return parsed;
912}
913
914static int ApplyParsedPerms(State* state, const char* filename, const struct stat* statptr,
915 struct perm_parsed_args parsed) {
916 int bad = 0;
917
918 if (parsed.has_selabel) {
919 if (lsetfilecon(filename, parsed.selabel) != 0) {
920 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n", filename,
921 parsed.selabel, strerror(errno));
922 bad++;
923 }
924 }
925
926 /* ignore symlinks */
927 if (S_ISLNK(statptr->st_mode)) {
928 return bad;
929 }
930
931 if (parsed.has_uid) {
932 if (chown(filename, parsed.uid, -1) < 0) {
933 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n", filename, parsed.uid,
934 strerror(errno));
935 bad++;
936 }
937 }
938
939 if (parsed.has_gid) {
940 if (chown(filename, -1, parsed.gid) < 0) {
941 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n", filename, parsed.gid,
942 strerror(errno));
943 bad++;
944 }
945 }
946
947 if (parsed.has_mode) {
948 if (chmod(filename, parsed.mode) < 0) {
949 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.mode,
950 strerror(errno));
951 bad++;
952 }
953 }
954
955 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
956 if (chmod(filename, parsed.dmode) < 0) {
957 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.dmode,
958 strerror(errno));
959 bad++;
960 }
961 }
962
963 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
964 if (chmod(filename, parsed.fmode) < 0) {
965 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.fmode,
966 strerror(errno));
967 bad++;
968 }
969 }
970
971 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
972 if (parsed.capabilities == 0) {
973 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
974 // Report failure unless it's ENODATA (attribute not set)
975 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n", filename,
976 parsed.capabilities, strerror(errno));
977 bad++;
978 }
979 } else {
980 struct vfs_cap_data cap_data;
981 memset(&cap_data, 0, sizeof(cap_data));
982 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
983 cap_data.data[0].permitted = (uint32_t)(parsed.capabilities & 0xffffffff);
984 cap_data.data[0].inheritable = 0;
985 cap_data.data[1].permitted = (uint32_t)(parsed.capabilities >> 32);
986 cap_data.data[1].inheritable = 0;
987 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
988 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n", filename,
989 parsed.capabilities, strerror(errno));
990 bad++;
991 }
992 }
993 }
994
995 return bad;
996}
997
998// nftw doesn't allow us to pass along context, so we need to use
999// global variables. *sigh*
1000static struct perm_parsed_args recursive_parsed_args;
1001static State* recursive_state;
1002
1003static int do_SetMetadataRecursive(const char* filename, const struct stat* statptr, int fileflags,
1004 struct FTW* pfwt) {
1005 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
1006}
1007
1008static Value* SetMetadataFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1009 if ((argv.size() % 2) != 1) {
1010 return ErrorAbort(state, kArgsParsingFailure, "%s() expects an odd number of arguments, got %zu",
1011 name, argv.size());
1012 }
1013
1014 std::vector<std::string> args;
1015 if (!ReadArgs(state, argv, &args)) {
1016 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1017 }
1018
1019 struct stat sb;
1020 if (lstat(args[0].c_str(), &sb) == -1) {
1021 return ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s", name,
1022 args[0].c_str(), strerror(errno));
1023 }
1024
1025 struct perm_parsed_args parsed = ParsePermArgs(state, args);
1026 int bad = 0;
1027 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
1028
1029 if (recursive) {
1030 recursive_parsed_args = parsed;
1031 recursive_state = state;
1032 bad += nftw(args[0].c_str(), do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
1033 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
1034 recursive_state = NULL;
1035 } else {
1036 bad += ApplyParsedPerms(state, args[0].c_str(), &sb, parsed);
1037 }
1038
1039 if (bad > 0) {
1040 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
1041 }
1042
1043 return StringValue("");
1044}
1045
Tianjie Xuc4447322017-03-06 14:44:59 -08001046Value* GetPropFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1047 if (argv.size() != 1) {
1048 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001049 }
1050 std::string key;
1051 if (!Evaluate(state, argv[0], &key)) {
1052 return nullptr;
1053 }
1054 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -07001055
Tao Bao039f2da2016-11-22 16:29:50 -08001056 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001057}
1058
Doug Zongker47cace92009-06-18 10:11:50 -07001059// file_getprop(file, key)
1060//
1061// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -07001062// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001063// and returns the value for 'key' (or "" if it isn't defined).
Tianjie Xu5419ad32018-02-02 16:49:15 -08001064Value* FileGetPropFn(const char* name, State* state,
1065 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -08001066 if (argv.size() != 2) {
1067 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1068 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -08001069 }
1070
1071 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001072 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -08001073 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1074 }
1075 const std::string& filename = args[0];
1076 const std::string& key = args[1];
1077
1078 struct stat st;
1079 if (stat(filename.c_str(), &st) < 0) {
1080 return ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name,
1081 filename.c_str(), strerror(errno));
1082 }
1083
1084 constexpr off_t MAX_FILE_GETPROP_SIZE = 65536;
1085 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
1086 return ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %lld)",
1087 filename.c_str(), name, static_cast<long long>(MAX_FILE_GETPROP_SIZE));
1088 }
1089
1090 std::string buffer(st.st_size, '\0');
1091 unique_file f(ota_fopen(filename.c_str(), "rb"));
1092 if (f == nullptr) {
1093 return ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename.c_str(),
1094 strerror(errno));
1095 }
1096
1097 if (ota_fread(&buffer[0], 1, st.st_size, f.get()) != static_cast<size_t>(st.st_size)) {
1098 ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s", name,
1099 static_cast<size_t>(st.st_size), filename.c_str());
1100 return nullptr;
1101 }
1102
1103 ota_fclose(f);
1104
1105 std::vector<std::string> lines = android::base::Split(buffer, "\n");
1106 for (size_t i = 0; i < lines.size(); i++) {
1107 std::string line = android::base::Trim(lines[i]);
1108
1109 // comment or blank line: skip to next line
1110 if (line.empty() || line[0] == '#') {
1111 continue;
1112 }
1113 size_t equal_pos = line.find('=');
1114 if (equal_pos == std::string::npos) {
1115 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001116 }
1117
Tao Bao358c2ec2016-11-28 11:48:43 -08001118 // trim whitespace between key and '='
1119 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -07001120
Tao Bao358c2ec2016-11-28 11:48:43 -08001121 // not the key we're looking for
1122 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001123
Tao Bao358c2ec2016-11-28 11:48:43 -08001124 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
1125 }
Doug Zongker47cace92009-06-18 10:11:50 -07001126
Tao Bao358c2ec2016-11-28 11:48:43 -08001127 return StringValue("");
Doug Zongker8edb00c2009-06-11 17:21:44 -07001128}
1129
Doug Zongker8edb00c2009-06-11 17:21:44 -07001130// apply_patch_space(bytes)
Tianjie Xu5419ad32018-02-02 16:49:15 -08001131Value* ApplyPatchSpaceFn(const char* name, State* state,
1132 const std::vector<std::unique_ptr<Expr>>& argv) {
Tianjie Xuc4447322017-03-06 14:44:59 -08001133 if (argv.size() != 1) {
1134 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 args, got %zu", name,
1135 argv.size());
1136 }
Tao Bao039f2da2016-11-22 16:29:50 -08001137 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001138 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001139 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1140 }
1141 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -08001142
Tao Bao039f2da2016-11-22 16:29:50 -08001143 size_t bytes;
1144 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
Tianjie Xu5ad80282018-01-28 15:37:48 -08001145 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count", name,
1146 bytes_str.c_str());
Tao Bao039f2da2016-11-22 16:29:50 -08001147 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001148
Tianjie Xu99b73be2017-11-28 17:23:06 -08001149 // Skip the cache size check if the update is a retry.
1150 if (state->is_retry || CacheSizeCheck(bytes) == 0) {
1151 return StringValue("t");
Tao Bao039f2da2016-11-22 16:29:50 -08001152 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001153 return StringValue("");
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001154}
1155
Tianjie Xuc4447322017-03-06 14:44:59 -08001156Value* WipeCacheFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1157 if (!argv.empty()) {
1158 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
1159 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001160 }
1161 fprintf(static_cast<UpdaterInfo*>(state->cookie)->cmd_pipe, "wipe_cache\n");
1162 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -07001163}
1164
Tianjie Xuc4447322017-03-06 14:44:59 -08001165Value* RunProgramFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1166 if (argv.size() < 1) {
Tao Bao039f2da2016-11-22 16:29:50 -08001167 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
1168 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001169
Tao Bao039f2da2016-11-22 16:29:50 -08001170 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001171 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001172 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1173 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001174
Tianjie Xuc4447322017-03-06 14:44:59 -08001175 char* args2[argv.size() + 1];
1176 for (size_t i = 0; i < argv.size(); i++) {
Tao Bao039f2da2016-11-22 16:29:50 -08001177 args2[i] = &args[i][0];
1178 }
Tianjie Xuc4447322017-03-06 14:44:59 -08001179 args2[argv.size()] = nullptr;
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001180
Tianjie Xuc4447322017-03-06 14:44:59 -08001181 LOG(INFO) << "about to run program [" << args2[0] << "] with " << argv.size() << " args";
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001182
Tao Bao039f2da2016-11-22 16:29:50 -08001183 pid_t child = fork();
1184 if (child == 0) {
1185 execv(args2[0], args2);
1186 PLOG(ERROR) << "run_program: execv failed";
Tao Bao3da88012017-02-03 13:09:23 -08001187 _exit(EXIT_FAILURE);
Tao Bao039f2da2016-11-22 16:29:50 -08001188 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001189
Tao Bao039f2da2016-11-22 16:29:50 -08001190 int status;
1191 waitpid(child, &status, 0);
1192 if (WIFEXITED(status)) {
1193 if (WEXITSTATUS(status) != 0) {
1194 LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001195 }
Tao Bao039f2da2016-11-22 16:29:50 -08001196 } else if (WIFSIGNALED(status)) {
1197 LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status);
1198 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001199
Tao Bao039f2da2016-11-22 16:29:50 -08001200 return StringValue(std::to_string(status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001201}
1202
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001203// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001204// is actually a FileContents*).
Tianjie Xuc4447322017-03-06 14:44:59 -08001205Value* ReadFileFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1206 if (argv.size() != 1) {
1207 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001208 }
Doug Zongker512536a2010-02-17 16:11:44 -08001209
Tao Bao039f2da2016-11-22 16:29:50 -08001210 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001211 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001212 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1213 }
1214 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -08001215
Tao Bao039f2da2016-11-22 16:29:50 -08001216 Value* v = new Value(VAL_INVALID, "");
Doug Zongker512536a2010-02-17 16:11:44 -08001217
Tao Bao039f2da2016-11-22 16:29:50 -08001218 FileContents fc;
1219 if (LoadFileContents(filename.c_str(), &fc) == 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001220 v->type = VAL_BLOB;
Tao Bao039f2da2016-11-22 16:29:50 -08001221 v->data = std::string(fc.data.begin(), fc.data.end());
1222 }
1223 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001224}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001225
Tao Bao1bf17722016-11-03 23:52:01 -07001226// write_value(value, filename)
1227// Writes 'value' to 'filename'.
1228// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
Tianjie Xuc4447322017-03-06 14:44:59 -08001229Value* WriteValueFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1230 if (argv.size() != 2) {
1231 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1232 argv.size());
Tao Baod0f30882016-11-03 23:52:01 -07001233 }
Tao Bao1bf17722016-11-03 23:52:01 -07001234
Tao Baod0f30882016-11-03 23:52:01 -07001235 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001236 if (!ReadArgs(state, argv, &args)) {
Tao Baod0f30882016-11-03 23:52:01 -07001237 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1238 }
Tao Bao1bf17722016-11-03 23:52:01 -07001239
Tao Baod0f30882016-11-03 23:52:01 -07001240 const std::string& filename = args[1];
1241 if (filename.empty()) {
1242 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
1243 }
Tao Bao1bf17722016-11-03 23:52:01 -07001244
Tao Baod0f30882016-11-03 23:52:01 -07001245 const std::string& value = args[0];
1246 if (!android::base::WriteStringToFile(value, filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001247 PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\"";
Tao Baod0f30882016-11-03 23:52:01 -07001248 return StringValue("");
1249 } else {
1250 return StringValue("t");
1251 }
Tao Bao1bf17722016-11-03 23:52:01 -07001252}
1253
Doug Zongkerc87bab12013-11-25 13:53:25 -08001254// Immediately reboot the device. Recovery is not finished normally,
1255// so if you reboot into recovery it will re-start applying the
1256// current package (because nothing has cleared the copy of the
1257// arguments stored in the BCB).
1258//
1259// The argument is the partition name passed to the android reboot
1260// property. It can be "recovery" to boot from the recovery
1261// partition, or "" (empty string) to boot from the regular boot
1262// partition.
Tianjie Xuc4447322017-03-06 14:44:59 -08001263Value* RebootNowFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1264 if (argv.size() != 2) {
1265 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1266 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001267 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001268
Tao Baobedf5fc2016-11-18 12:01:26 -08001269 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001270 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001271 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1272 }
1273 const std::string& filename = args[0];
1274 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001275
Tao Baobedf5fc2016-11-18 12:01:26 -08001276 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
1277 bootloader_message boot;
1278 std::string err;
1279 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001280 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001281 return StringValue("");
1282 }
1283 memset(boot.command, 0, sizeof(boot.command));
1284 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001285 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001286 return StringValue("");
1287 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001288
Dmitri Plotnikoved9db0f2017-04-18 08:28:26 -07001289 std::string reboot_cmd = "reboot," + property;
1290 if (android::base::GetBoolProperty("ro.boot.quiescent", false)) {
1291 reboot_cmd += ",quiescent";
1292 }
Tao Baobedf5fc2016-11-18 12:01:26 -08001293 android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001294
Tao Baobedf5fc2016-11-18 12:01:26 -08001295 sleep(5);
1296 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001297}
1298
1299// Store a string value somewhere that future invocations of recovery
1300// can access it. This value is called the "stage" and can be used to
1301// drive packages that need to do reboots in the middle of
1302// installation and keep track of where they are in the multi-stage
1303// install.
1304//
1305// The first argument is the block device for the misc partition
1306// ("/misc" in the fstab), which is where this value is stored. The
1307// second argument is the string to store; it should not exceed 31
1308// bytes.
Tianjie Xuc4447322017-03-06 14:44:59 -08001309Value* SetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1310 if (argv.size() != 2) {
1311 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1312 argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001313 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001314
Tao Baobedf5fc2016-11-18 12:01:26 -08001315 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001316 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001317 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1318 }
1319 const std::string& filename = args[0];
1320 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001321
Tao Baobedf5fc2016-11-18 12:01:26 -08001322 // Store this value in the misc partition, immediately after the
1323 // bootloader message that the main recovery uses to save its
1324 // arguments in case of the device restarting midway through
1325 // package installation.
1326 bootloader_message boot;
1327 std::string err;
1328 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001329 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001330 return StringValue("");
1331 }
1332 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
1333 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001334 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001335 return StringValue("");
1336 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001337
Tao Baobedf5fc2016-11-18 12:01:26 -08001338 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001339}
1340
1341// Return the value most recently saved with SetStageFn. The argument
1342// is the block device for the misc partition.
Tianjie Xuc4447322017-03-06 14:44:59 -08001343Value* GetStageFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1344 if (argv.size() != 1) {
1345 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %zu", name, argv.size());
Tao Baobedf5fc2016-11-18 12:01:26 -08001346 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001347
Tao Baobedf5fc2016-11-18 12:01:26 -08001348 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001349 if (!ReadArgs(state, argv, &args)) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001350 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1351 }
1352 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001353
Tao Baobedf5fc2016-11-18 12:01:26 -08001354 bootloader_message boot;
1355 std::string err;
1356 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001357 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001358 return StringValue("");
1359 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001360
Tao Baobedf5fc2016-11-18 12:01:26 -08001361 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001362}
1363
Tianjie Xuc4447322017-03-06 14:44:59 -08001364Value* WipeBlockDeviceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1365 if (argv.size() != 2) {
1366 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %zu", name,
1367 argv.size());
Tao Bao358c2ec2016-11-28 11:48:43 -08001368 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001369
Tao Bao358c2ec2016-11-28 11:48:43 -08001370 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001371 if (!ReadArgs(state, argv, &args)) {
Tao Bao358c2ec2016-11-28 11:48:43 -08001372 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1373 }
1374 const std::string& filename = args[0];
1375 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001376
Tao Bao358c2ec2016-11-28 11:48:43 -08001377 size_t len;
1378 if (!android::base::ParseUint(len_str.c_str(), &len)) {
1379 return nullptr;
1380 }
1381 unique_fd fd(ota_open(filename.c_str(), O_WRONLY, 0644));
1382 // The wipe_block_device function in ext4_utils returns 0 on success and 1
1383 // for failure.
1384 int status = wipe_block_device(fd, len);
1385 return StringValue((status == 0) ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001386}
1387
Tianjie Xuc4447322017-03-06 14:44:59 -08001388Value* EnableRebootFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
1389 if (!argv.empty()) {
1390 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %zu", name,
1391 argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001392 }
1393 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
1394 fprintf(ui->cmd_pipe, "enable_reboot\n");
1395 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -07001396}
1397
Tianjie Xuc4447322017-03-06 14:44:59 -08001398Value* Tune2FsFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001399#ifdef HAVE_LIBTUNE2FS
Tianjie Xuc4447322017-03-06 14:44:59 -08001400 if (argv.empty()) {
1401 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %zu", name, argv.size());
Tao Bao039f2da2016-11-22 16:29:50 -08001402 }
Michael Rungeb278c252014-11-21 00:12:28 -08001403
Tao Bao039f2da2016-11-22 16:29:50 -08001404 std::vector<std::string> args;
Tianjie Xuc4447322017-03-06 14:44:59 -08001405 if (!ReadArgs(state, argv, &args)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001406 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
1407 }
Michael Rungeb278c252014-11-21 00:12:28 -08001408
Tianjie Xuc4447322017-03-06 14:44:59 -08001409 char* args2[argv.size() + 1];
Tao Bao039f2da2016-11-22 16:29:50 -08001410 // Tune2fs expects the program name as its args[0]
1411 args2[0] = const_cast<char*>(name);
1412 if (args2[0] == nullptr) {
1413 return nullptr;
1414 }
Tianjie Xuc4447322017-03-06 14:44:59 -08001415 for (size_t i = 0; i < argv.size(); ++i) {
Tao Bao039f2da2016-11-22 16:29:50 -08001416 args2[i + 1] = &args[i][0];
1417 }
Michael Rungeb278c252014-11-21 00:12:28 -08001418
Tao Bao039f2da2016-11-22 16:29:50 -08001419 // tune2fs changes the file system parameters on an ext2 file system; it
1420 // returns 0 on success.
Tianjie Xuc4447322017-03-06 14:44:59 -08001421 int result = tune2fs_main(argv.size() + 1, args2);
Tao Bao039f2da2016-11-22 16:29:50 -08001422 if (result != 0) {
1423 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
1424 }
1425 return StringValue("t");
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001426#else
Ethan Yonker8373cfe2017-09-08 06:50:54 -05001427 return ErrorAbort(state, kTune2FsFailure, "%s() support not present, no libtune2fs", name);
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001428#endif // HAVE_LIBTUNE2FS
Michael Rungeb278c252014-11-21 00:12:28 -08001429}
1430
Doug Zongker9931f7f2009-06-10 14:11:53 -07001431void RegisterInstallFunctions() {
Tao Bao039f2da2016-11-22 16:29:50 -08001432 RegisterFunction("mount", MountFn);
1433 RegisterFunction("is_mounted", IsMountedFn);
1434 RegisterFunction("unmount", UnmountFn);
1435 RegisterFunction("format", FormatFn);
1436 RegisterFunction("show_progress", ShowProgressFn);
1437 RegisterFunction("set_progress", SetProgressFn);
Tom Marshallbf4f24f2017-08-23 18:14:00 +00001438 RegisterFunction("delete", DeleteFn);
1439 RegisterFunction("delete_recursive", DeleteFn);
Tom Marshall981118e2017-10-25 20:27:08 +02001440 RegisterFunction("package_extract_dir", PackageExtractDirFn);
Tao Bao039f2da2016-11-22 16:29:50 -08001441 RegisterFunction("package_extract_file", PackageExtractFileFn);
Tom Marshallbf4f24f2017-08-23 18:14:00 +00001442 RegisterFunction("symlink", SymlinkFn);
1443
1444 // Usage:
1445 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1446 // Example:
1447 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel",
1448 // "u:object_r:system_file:s0", "capabilities", 0x0);
1449 RegisterFunction("set_metadata", SetMetadataFn);
1450
1451 // Usage:
1452 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1453 // Example:
1454 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755,
1455 // "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1456 RegisterFunction("set_metadata_recursive", SetMetadataFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001457
Tao Bao039f2da2016-11-22 16:29:50 -08001458 RegisterFunction("getprop", GetPropFn);
1459 RegisterFunction("file_getprop", FileGetPropFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001460
Tao Bao039f2da2016-11-22 16:29:50 -08001461 RegisterFunction("apply_patch", ApplyPatchFn);
1462 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1463 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001464
Tao Bao039f2da2016-11-22 16:29:50 -08001465 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001466
Tao Bao039f2da2016-11-22 16:29:50 -08001467 RegisterFunction("read_file", ReadFileFn);
1468 RegisterFunction("sha1_check", Sha1CheckFn);
Tom Marshallbf4f24f2017-08-23 18:14:00 +00001469 RegisterFunction("rename", RenameFn);
Tao Bao039f2da2016-11-22 16:29:50 -08001470 RegisterFunction("write_value", WriteValueFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001471
Tao Bao039f2da2016-11-22 16:29:50 -08001472 RegisterFunction("wipe_cache", WipeCacheFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001473
Tao Bao039f2da2016-11-22 16:29:50 -08001474 RegisterFunction("ui_print", UIPrintFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001475
Tao Bao039f2da2016-11-22 16:29:50 -08001476 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001477
Tao Bao039f2da2016-11-22 16:29:50 -08001478 RegisterFunction("reboot_now", RebootNowFn);
1479 RegisterFunction("get_stage", GetStageFn);
1480 RegisterFunction("set_stage", SetStageFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001481
Tao Bao039f2da2016-11-22 16:29:50 -08001482 RegisterFunction("enable_reboot", EnableRebootFn);
1483 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001484}