blob: 64314544796d070a2b906d6b8f0a8eafe65c0a2a [file] [log] [blame]
Doug Zongker9931f7f2009-06-10 14:11:53 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tao Bao0c7839a2016-10-10 15:48:37 -070017#include "updater/install.h"
18
Doug Zongkerfbf3c102009-06-24 09:36:20 -070019#include <ctype.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070020#include <errno.h>
Tao Bao361342c2016-02-08 11:15:50 -080021#include <fcntl.h>
22#include <ftw.h>
23#include <inttypes.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070024#include <stdarg.h>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070025#include <stdio.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070026#include <stdlib.h>
27#include <string.h>
Tao Bao361342c2016-02-08 11:15:50 -080028#include <sys/capability.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070029#include <sys/mount.h>
30#include <sys/stat.h>
31#include <sys/types.h>
Doug Zongkera3f89ea2009-09-10 14:10:48 -070032#include <sys/wait.h>
Nick Kralevich5dbdef02013-09-07 14:41:06 -070033#include <sys/xattr.h>
Tao Bao361342c2016-02-08 11:15:50 -080034#include <time.h>
35#include <unistd.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070036#include <utime.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070037
Yabin Cui64be2132016-02-03 18:16:02 -080038#include <memory>
Tao Bao361342c2016-02-08 11:15:50 -080039#include <string>
Yabin Cui64be2132016-02-03 18:16:02 -080040#include <vector>
41
Tao Baod0f30882016-11-03 23:52:01 -070042#include <android-base/file.h>
Tao Bao039f2da2016-11-22 16:29:50 -080043#include <android-base/logging.h>
Tianjie Xu5fe280a2016-10-17 18:15:20 -070044#include <android-base/parsedouble.h>
Tao Baod0f30882016-11-03 23:52:01 -070045#include <android-base/parseint.h>
Elliott Hughescb220402016-09-23 15:30:55 -070046#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080047#include <android-base/stringprintf.h>
Tao Baod0f30882016-11-03 23:52:01 -070048#include <android-base/strings.h>
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/make_ext4fs.h>
53#include <ext4_utils/wipe.h>
Tao Bao361342c2016-02-08 11:15:50 -080054#include <openssl/sha.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070055#include <selinux/label.h>
56#include <selinux/selinux.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"
Tianjie Xu16255832016-04-30 11:49:59 -070060#include "error_code.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070061#include "mounts.h"
Jed Estep39c1b5e2015-12-15 16:04:53 -080062#include "ota_io.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070063#include "otautil/DirUtil.h"
64#include "otautil/ZipUtil.h"
Tao Bao361342c2016-02-08 11:15:50 -080065#include "print_sha1.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080066#include "tune2fs.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070067#include "updater/updater.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070068
Tao Bao1107d962015-09-09 17:16:55 -070069// Send over the buffer to recovery though the command pipe.
70static void uiPrint(State* state, const std::string& buffer) {
Tao Bao039f2da2016-11-22 16:29:50 -080071 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070072
Tao Bao039f2da2016-11-22 16:29:50 -080073 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
74 // So skip sending empty strings to UI.
75 std::vector<std::string> lines = android::base::Split(buffer, "\n");
76 for (auto& line : lines) {
77 if (!line.empty()) {
78 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
79 fprintf(ui->cmd_pipe, "ui_print\n");
Tao Bao1107d962015-09-09 17:16:55 -070080 }
Tao Bao039f2da2016-11-22 16:29:50 -080081 }
Tao Bao1107d962015-09-09 17:16:55 -070082
Tao Bao039f2da2016-11-22 16:29:50 -080083 // On the updater side, we need to dump the contents to stderr (which has
84 // been redirected to the log file). Because the recovery will only print
85 // the contents to screen when processing pipe command ui_print.
86 LOG(INFO) << buffer;
Michael Runged4a63422014-10-22 19:48:41 -070087}
88
Elliott Hughes83ce7552016-06-30 09:28:42 -070089void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) {
Tao Bao039f2da2016-11-22 16:29:50 -080090 std::string error_msg;
Tao Bao1107d962015-09-09 17:16:55 -070091
Tao Bao039f2da2016-11-22 16:29:50 -080092 va_list ap;
93 va_start(ap, format);
94 android::base::StringAppendV(&error_msg, format, ap);
95 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070096
Tao Bao039f2da2016-11-22 16:29:50 -080097 uiPrint(state, error_msg);
Michael Runged4a63422014-10-22 19:48:41 -070098}
99
Tianjie Xud75003d2016-11-04 11:31:29 -0700100static 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
Tao Bao0c7839a2016-10-10 15:48:37 -0700105// Create all parent directories of name, if necessary.
Tianjie Xud75003d2016-11-04 11:31:29 -0700106static bool make_parents(const std::string& name) {
Tao Bao039f2da2016-11-22 16:29:50 -0800107 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;
Tao Bao0c7839a2016-10-10 15:48:37 -0700112 }
Tao Bao039f2da2016-11-22 16:29:50 -0800113 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;
Tao Bao0c7839a2016-10-10 15:48:37 -0700126}
127
Doug Zongker3d177d02010-07-01 09:18:44 -0700128// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -0700129// mount(fs_type, partition_type, location, mount_point, mount_options)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700130//
Doug Zongker3d177d02010-07-01 09:18:44 -0700131// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800132Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800133 if (argc != 4 && argc != 5) {
134 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %d", name, argc);
135 }
136
137 std::vector<std::string> args;
138 if (!ReadArgs(state, argc, argv, &args)) {
139 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
140 }
141 const std::string& fs_type = args[0];
142 const std::string& partition_type = args[1];
143 const std::string& location = args[2];
144 const std::string& mount_point = args[3];
145 std::string mount_options;
146
147 if (argc == 5) {
148 mount_options = args[4];
149 }
150
151 if (fs_type.empty()) {
152 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
153 }
154 if (partition_type.empty()) {
155 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
156 name);
157 }
158 if (location.empty()) {
159 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
160 }
161 if (mount_point.empty()) {
162 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
163 name);
164 }
165
166 {
167 char* secontext = nullptr;
168
169 if (sehandle) {
170 selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755);
171 setfscreatecon(secontext);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700172 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700173
Tao Bao039f2da2016-11-22 16:29:50 -0800174 mkdir(mount_point.c_str(), 0755);
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700175
Tao Bao039f2da2016-11-22 16:29:50 -0800176 if (secontext) {
177 freecon(secontext);
178 setfscreatecon(nullptr);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700179 }
Tao Bao039f2da2016-11-22 16:29:50 -0800180 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700181
Tao Bao039f2da2016-11-22 16:29:50 -0800182 if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(),
183 MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) {
184 uiPrintf(state, "%s: failed to mount %s at %s: %s\n", name, location.c_str(),
185 mount_point.c_str(), strerror(errno));
186 return StringValue("");
187 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700188
Tao Bao039f2da2016-11-22 16:29:50 -0800189 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700190}
191
Doug Zongker8edb00c2009-06-11 17:21:44 -0700192
193// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800194Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800195 if (argc != 1) {
196 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
197 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700198
Tao Bao039f2da2016-11-22 16:29:50 -0800199 std::vector<std::string> args;
200 if (!ReadArgs(state, argc, argv, &args)) {
201 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
202 }
203 const std::string& mount_point = args[0];
204 if (mount_point.empty()) {
205 return ErrorAbort(state, kArgsParsingFailure,
206 "mount_point argument to unmount() can't be empty");
207 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700208
Tao Bao039f2da2016-11-22 16:29:50 -0800209 scan_mounted_volumes();
210 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
211 if (vol == nullptr) {
212 return StringValue("");
213 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700214
Tao Bao039f2da2016-11-22 16:29:50 -0800215 return StringValue(mount_point);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700216}
217
Doug Zongker512536a2010-02-17 16:11:44 -0800218Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800219 if (argc != 1) {
220 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
221 }
222 std::vector<std::string> args;
223 if (!ReadArgs(state, argc, argv, &args)) {
224 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
225 }
226 const std::string& mount_point = args[0];
227 if (mount_point.empty()) {
228 return ErrorAbort(state, kArgsParsingFailure,
229 "mount_point argument to unmount() can't be empty");
230 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700231
Tao Bao039f2da2016-11-22 16:29:50 -0800232 scan_mounted_volumes();
233 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
234 if (vol == nullptr) {
235 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point.c_str());
236 return nullptr;
237 } else {
238 int ret = unmount_mounted_volume(vol);
239 if (ret != 0) {
240 uiPrintf(state, "unmount of %s failed (%d): %s\n", mount_point.c_str(), ret, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700241 }
Tao Bao039f2da2016-11-22 16:29:50 -0800242 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700243
Tao Bao039f2da2016-11-22 16:29:50 -0800244 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700245}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700246
JP Abgrall37aedb32014-06-16 19:07:39 -0700247static int exec_cmd(const char* path, char* const argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800248 pid_t child;
249 if ((child = vfork()) == 0) {
250 execv(path, argv);
251 _exit(-1);
252 }
253
254 int status;
255 waitpid(child, &status, 0);
256 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
257 LOG(ERROR) << path << " failed with status " << WEXITSTATUS(status);
258 }
259 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700260}
261
Stephen Smalley779701d2012-02-09 14:13:23 -0500262// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700263//
Tao Bao039f2da2016-11-22 16:29:50 -0800264// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
265// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700266// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800267// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700268// if fs_size < 0, then reserve that many bytes at the end of the partition (not for "f2fs")
Doug Zongker512536a2010-02-17 16:11:44 -0800269Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800270 if (argc != 5) {
271 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %d", name, argc);
272 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500273
Tao Bao039f2da2016-11-22 16:29:50 -0800274 std::vector<std::string> args;
275 if (!ReadArgs(state, argc, argv, &args)) {
276 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
277 }
278 const std::string& fs_type = args[0];
279 const std::string& partition_type = args[1];
280 const std::string& location = args[2];
281 const std::string& fs_size = args[3];
282 const std::string& mount_point = args[4];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700283
Tao Bao039f2da2016-11-22 16:29:50 -0800284 if (fs_type.empty()) {
285 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
286 }
287 if (partition_type.empty()) {
288 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
289 name);
290 }
291 if (location.empty()) {
292 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
293 }
294 if (mount_point.empty()) {
295 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
296 name);
297 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700298
Tao Bao039f2da2016-11-22 16:29:50 -0800299 int64_t size;
300 if (!android::base::ParseInt(fs_size, &size)) {
301 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s\n", name,
302 fs_size.c_str());
303 }
304
305 if (fs_type == "ext4") {
306 int status = make_ext4fs(location.c_str(), size, mount_point.c_str(), sehandle);
307 if (status != 0) {
308 LOG(ERROR) << name << ": make_ext4fs failed (" << status << ") on " << location;
309 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700310 }
Tao Bao039f2da2016-11-22 16:29:50 -0800311 return StringValue(location);
312 } else if (fs_type == "f2fs") {
313 if (size < 0) {
314 LOG(ERROR) << name << ": fs_size can't be negative for f2fs: " << fs_size;
315 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700316 }
Tao Bao039f2da2016-11-22 16:29:50 -0800317 std::string num_sectors = std::to_string(size / 512);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700318
Tao Bao039f2da2016-11-22 16:29:50 -0800319 const char* f2fs_path = "/sbin/mkfs.f2fs";
320 const char* const f2fs_argv[] = { "mkfs.f2fs", "-t", "-d1", location.c_str(),
321 num_sectors.c_str(), nullptr };
322 int status = exec_cmd(f2fs_path, const_cast<char* const*>(f2fs_argv));
323 if (status != 0) {
324 LOG(ERROR) << name << ": mkfs.f2fs failed (" << status << ") on " << location;
325 return StringValue("");
326 }
327 return StringValue(location);
328 } else {
329 LOG(ERROR) << name << ": unsupported fs_type \"" << fs_type << "\" partition_type \""
330 << partition_type << "\"";
331 }
332
333 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700334}
335
Tao Baoa659d792016-11-03 23:25:04 -0700336// rename(src_name, dst_name)
337// Renames src_name to dst_name. It automatically creates the necessary directories for dst_name.
338// Example: rename("system/app/Hangouts/Hangouts.apk", "system/priv-app/Hangouts/Hangouts.apk")
Michael Rungece7ca712013-11-06 17:42:20 -0800339Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800340 if (argc != 2) {
341 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
342 }
Michael Rungece7ca712013-11-06 17:42:20 -0800343
Tao Bao039f2da2016-11-22 16:29:50 -0800344 std::vector<std::string> args;
345 if (!ReadArgs(state, argc, argv, &args)) {
346 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
347 }
348 const std::string& src_name = args[0];
349 const std::string& dst_name = args[1];
Michael Rungece7ca712013-11-06 17:42:20 -0800350
Tao Bao039f2da2016-11-22 16:29:50 -0800351 if (src_name.empty()) {
352 return ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", name);
353 }
354 if (dst_name.empty()) {
355 return ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", name);
356 }
357 if (!make_parents(dst_name)) {
358 return ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
359 dst_name.c_str(), strerror(errno));
360 } else if (access(dst_name.c_str(), F_OK) == 0 && access(src_name.c_str(), F_OK) != 0) {
361 // File was already moved
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700362 return StringValue(dst_name);
Tao Bao039f2da2016-11-22 16:29:50 -0800363 } else if (rename(src_name.c_str(), dst_name.c_str()) != 0) {
364 return ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
365 src_name.c_str(), dst_name.c_str(), strerror(errno));
366 }
367
368 return StringValue(dst_name);
Michael Rungece7ca712013-11-06 17:42:20 -0800369}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700370
Tao Bao0831d0b2016-11-03 23:25:04 -0700371// delete([filename, ...])
372// Deletes all the filenames listed. Returns the number of files successfully deleted.
373//
374// delete_recursive([dirname, ...])
375// Recursively deletes dirnames and all their contents. Returns the number of directories
376// successfully deleted.
Doug Zongker512536a2010-02-17 16:11:44 -0800377Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800378 std::vector<std::string> paths(argc);
379 for (int i = 0; i < argc; ++i) {
380 if (!Evaluate(state, argv[i], &paths[i])) {
381 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700382 }
Tao Bao039f2da2016-11-22 16:29:50 -0800383 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700384
Tao Bao039f2da2016-11-22 16:29:50 -0800385 bool recursive = (strcmp(name, "delete_recursive") == 0);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700386
Tao Bao039f2da2016-11-22 16:29:50 -0800387 int success = 0;
388 for (int i = 0; i < argc; ++i) {
389 if ((recursive ? dirUnlinkHierarchy(paths[i].c_str()) : unlink(paths[i].c_str())) == 0) {
390 ++success;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700391 }
Tao Bao039f2da2016-11-22 16:29:50 -0800392 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700393
Tao Bao039f2da2016-11-22 16:29:50 -0800394 return StringValue(std::to_string(success));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700395}
396
Doug Zongker8edb00c2009-06-11 17:21:44 -0700397
Doug Zongker512536a2010-02-17 16:11:44 -0800398Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800399 if (argc != 2) {
400 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
401 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700402
Tao Bao039f2da2016-11-22 16:29:50 -0800403 std::vector<std::string> args;
404 if (!ReadArgs(state, argc, argv, &args)) {
405 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
406 }
407 const std::string& frac_str = args[0];
408 const std::string& sec_str = args[1];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700409
Tao Bao039f2da2016-11-22 16:29:50 -0800410 double frac;
411 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
412 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s\n", name,
413 frac_str.c_str());
414 }
415 int sec;
416 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
417 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse int in %s\n", name,
418 sec_str.c_str());
419 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700420
Tao Bao039f2da2016-11-22 16:29:50 -0800421 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
422 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700423
Tao Bao039f2da2016-11-22 16:29:50 -0800424 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700425}
426
Doug Zongker512536a2010-02-17 16:11:44 -0800427Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800428 if (argc != 1) {
429 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
430 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700431
Tao Bao039f2da2016-11-22 16:29:50 -0800432 std::vector<std::string> args;
433 if (!ReadArgs(state, 1, argv, &args)) {
434 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
435 }
436 const std::string& frac_str = args[0];
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700437
Tao Bao039f2da2016-11-22 16:29:50 -0800438 double frac;
439 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
440 return ErrorAbort(state, kArgsParsingFailure, "%s: failed to parse double in %s\n", name,
441 frac_str.c_str());
442 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700443
Tao Bao039f2da2016-11-22 16:29:50 -0800444 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
445 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700446
Tao Bao039f2da2016-11-22 16:29:50 -0800447 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700448}
449
Tao Bao1036d362016-11-17 22:49:56 -0800450// package_extract_dir(package_dir, dest_dir)
451// Extracts all files from the package underneath package_dir and writes them to the
452// corresponding tree beneath dest_dir. Any existing files are overwritten.
453// Example: package_extract_dir("system", "/system")
454//
455// Note: package_dir needs to be a relative path; dest_dir needs to be an absolute path.
456Value* PackageExtractDirFn(const char* name, State* state, int argc, Expr* argv[]) {
457 if (argc != 2) {
458 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
459 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700460
Tao Bao1036d362016-11-17 22:49:56 -0800461 std::vector<std::string> args;
462 if (!ReadArgs(state, 2, argv, &args)) {
463 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
464 }
465 const std::string& zip_path = args[0];
466 const std::string& dest_path = args[1];
Doug Zongker9931f7f2009-06-10 14:11:53 -0700467
Tao Bao1036d362016-11-17 22:49:56 -0800468 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700469
Tao Bao1036d362016-11-17 22:49:56 -0800470 // To create a consistent system image, never use the clock for timestamps.
471 constexpr struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
Doug Zongker9931f7f2009-06-10 14:11:53 -0700472
Tao Bao1036d362016-11-17 22:49:56 -0800473 bool success = ExtractPackageRecursive(za, zip_path, dest_path, &timestamp, sehandle);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700474
Tao Bao1036d362016-11-17 22:49:56 -0800475 return StringValue(success ? "t" : "");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700476}
477
Tao Baoef0eb3b2016-11-14 21:29:52 -0800478// package_extract_file(package_file[, dest_file])
479// Extracts a single package_file from the update package and writes it to dest_file,
480// overwriting existing files if necessary. Without the dest_file argument, returns the
481// contents of the package file as a binary blob.
482Value* PackageExtractFileFn(const char* name, State* state, int argc, Expr* argv[]) {
483 if (argc < 1 || argc > 2) {
484 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %d", name, argc);
485 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700486
Tao Baoef0eb3b2016-11-14 21:29:52 -0800487 if (argc == 2) {
488 // The two-argument version extracts to a file.
489
490 std::vector<std::string> args;
491 if (!ReadArgs(state, 2, argv, &args)) {
492 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %d args", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700493 }
Tao Baoef0eb3b2016-11-14 21:29:52 -0800494 const std::string& zip_path = args[0];
495 const std::string& dest_path = args[1];
Doug Zongker43772d22014-06-09 14:13:19 -0700496
Tao Baoef0eb3b2016-11-14 21:29:52 -0800497 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
498 ZipString zip_string_path(zip_path.c_str());
499 ZipEntry entry;
500 if (FindEntry(za, zip_string_path, &entry) != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800501 LOG(ERROR) << name << ": no " << zip_path << " in package";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800502 return StringValue("");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700503 }
Tao Baoef0eb3b2016-11-14 21:29:52 -0800504
Tao Bao358c2ec2016-11-28 11:48:43 -0800505 unique_fd fd(TEMP_FAILURE_RETRY(
506 ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tao Baoef0eb3b2016-11-14 21:29:52 -0800507 if (fd == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800508 PLOG(ERROR) << name << ": can't open " << dest_path << " for write";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800509 return StringValue("");
510 }
511
512 bool success = true;
513 int32_t ret = ExtractEntryToFile(za, &entry, fd);
514 if (ret != 0) {
Tao Bao039f2da2016-11-22 16:29:50 -0800515 LOG(ERROR) << name << ": Failed to extract entry \"" << zip_path << "\" ("
516 << entry.uncompressed_length << " bytes) to \"" << dest_path
517 << "\": " << ErrorCodeString(ret);
Tao Baoef0eb3b2016-11-14 21:29:52 -0800518 success = false;
519 }
520 if (ota_fsync(fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800521 PLOG(ERROR) << "fsync of \"" << dest_path << "\" failed";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800522 success = false;
523 }
524 if (ota_close(fd) == -1) {
Tao Bao039f2da2016-11-22 16:29:50 -0800525 PLOG(ERROR) << "close of \"" << dest_path << "\" failed";
Tao Baoef0eb3b2016-11-14 21:29:52 -0800526 success = false;
527 }
528
529 return StringValue(success ? "t" : "");
530 } else {
531 // The one-argument version returns the contents of the file as the result.
532
533 std::vector<std::string> args;
534 if (!ReadArgs(state, 1, argv, &args)) {
535 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %d args", name, argc);
536 }
537 const std::string& zip_path = args[0];
538
539 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
540 ZipString zip_string_path(zip_path.c_str());
541 ZipEntry entry;
542 if (FindEntry(za, zip_string_path, &entry) != 0) {
543 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
544 zip_path.c_str());
545 }
546
547 std::string buffer;
548 buffer.resize(entry.uncompressed_length);
549
550 int32_t ret = ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
551 if (ret != 0) {
552 return ErrorAbort(state, kPackageExtractFileFailure,
553 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
554 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
555 }
556
557 return new Value(VAL_BLOB, buffer);
558 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700559}
560
Tao Bao89929022016-11-08 20:51:31 -0800561// symlink(target, [src1, src2, ...])
562// Creates all sources as symlinks to target. It unlinks any previously existing src1, src2, etc
563// before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800564Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800565 if (argc == 0) {
566 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %d", name, argc);
567 }
568 std::string target;
569 if (!Evaluate(state, argv[0], &target)) {
570 return nullptr;
571 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700572
Tao Bao039f2da2016-11-22 16:29:50 -0800573 std::vector<std::string> srcs;
574 if (!ReadArgs(state, argc - 1, argv + 1, &srcs)) {
575 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
576 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700577
Tao Bao039f2da2016-11-22 16:29:50 -0800578 size_t bad = 0;
579 for (const auto& src : srcs) {
580 if (unlink(src.c_str()) == -1 && errno != ENOENT) {
581 PLOG(ERROR) << name << ": failed to remove " << src;
582 ++bad;
583 } else if (!make_parents(src)) {
584 LOG(ERROR) << name << ": failed to symlink " << src << " to " << target
585 << ": making parents failed";
586 ++bad;
587 } else if (symlink(target.c_str(), src.c_str()) == -1) {
588 PLOG(ERROR) << name << ": failed to symlink " << src << " to " << target;
589 ++bad;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700590 }
Tao Bao039f2da2016-11-22 16:29:50 -0800591 }
592 if (bad != 0) {
593 return ErrorAbort(state, kSymlinkFailure, "%s: Failed to create %zu symlink(s)", name, bad);
594 }
595 return StringValue("t");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700596}
597
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700598struct perm_parsed_args {
Tao Bao039f2da2016-11-22 16:29:50 -0800599 bool has_uid;
600 uid_t uid;
601 bool has_gid;
602 gid_t gid;
603 bool has_mode;
604 mode_t mode;
605 bool has_fmode;
606 mode_t fmode;
607 bool has_dmode;
608 mode_t dmode;
609 bool has_selabel;
610 const char* selabel;
611 bool has_capabilities;
612 uint64_t capabilities;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700613};
614
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700615static struct perm_parsed_args ParsePermArgs(State * state, int argc,
616 const std::vector<std::string>& args) {
Tao Bao039f2da2016-11-22 16:29:50 -0800617 struct perm_parsed_args parsed;
618 int bad = 0;
619 static int max_warnings = 20;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700620
Tao Bao039f2da2016-11-22 16:29:50 -0800621 memset(&parsed, 0, sizeof(parsed));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700622
Tao Bao039f2da2016-11-22 16:29:50 -0800623 for (int i = 1; i < argc; i += 2) {
624 if (args[i] == "uid") {
625 int64_t uid;
626 if (sscanf(args[i + 1].c_str(), "%" SCNd64, &uid) == 1) {
627 parsed.uid = uid;
628 parsed.has_uid = true;
629 } else {
630 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1].c_str());
631 bad++;
632 }
633 continue;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700634 }
Tao Bao039f2da2016-11-22 16:29:50 -0800635 if (args[i] == "gid") {
636 int64_t gid;
637 if (sscanf(args[i + 1].c_str(), "%" SCNd64, &gid) == 1) {
638 parsed.gid = gid;
639 parsed.has_gid = true;
640 } else {
641 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1].c_str());
642 bad++;
643 }
644 continue;
645 }
646 if (args[i] == "mode") {
647 int32_t mode;
648 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
649 parsed.mode = mode;
650 parsed.has_mode = true;
651 } else {
652 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1].c_str());
653 bad++;
654 }
655 continue;
656 }
657 if (args[i] == "dmode") {
658 int32_t mode;
659 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
660 parsed.dmode = mode;
661 parsed.has_dmode = true;
662 } else {
663 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1].c_str());
664 bad++;
665 }
666 continue;
667 }
668 if (args[i] == "fmode") {
669 int32_t mode;
670 if (sscanf(args[i + 1].c_str(), "%" SCNi32, &mode) == 1) {
671 parsed.fmode = mode;
672 parsed.has_fmode = true;
673 } else {
674 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1].c_str());
675 bad++;
676 }
677 continue;
678 }
679 if (args[i] == "capabilities") {
680 int64_t capabilities;
681 if (sscanf(args[i + 1].c_str(), "%" SCNi64, &capabilities) == 1) {
682 parsed.capabilities = capabilities;
683 parsed.has_capabilities = true;
684 } else {
685 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1].c_str());
686 bad++;
687 }
688 continue;
689 }
690 if (args[i] == "selabel") {
691 if (!args[i + 1].empty()) {
692 parsed.selabel = args[i + 1].c_str();
693 parsed.has_selabel = true;
694 } else {
695 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1].c_str());
696 bad++;
697 }
698 continue;
699 }
700 if (max_warnings != 0) {
701 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i].c_str());
702 max_warnings--;
703 if (max_warnings == 0) {
704 LOG(INFO) << "ParsedPermArgs: suppressing further warnings";
705 }
706 }
707 }
708 return parsed;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700709}
710
Tao Bao039f2da2016-11-22 16:29:50 -0800711static int ApplyParsedPerms(State* state, const char* filename, const struct stat* statptr,
712 struct perm_parsed_args parsed) {
713 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700714
Tao Bao039f2da2016-11-22 16:29:50 -0800715 if (parsed.has_selabel) {
716 if (lsetfilecon(filename, parsed.selabel) != 0) {
717 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n", filename,
718 parsed.selabel, strerror(errno));
719 bad++;
Nick Kralevich68802412014-10-23 20:36:42 -0700720 }
Tao Bao039f2da2016-11-22 16:29:50 -0800721 }
Nick Kralevich68802412014-10-23 20:36:42 -0700722
Tao Bao039f2da2016-11-22 16:29:50 -0800723 /* ignore symlinks */
724 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700725 return bad;
Tao Bao039f2da2016-11-22 16:29:50 -0800726 }
727
728 if (parsed.has_uid) {
729 if (chown(filename, parsed.uid, -1) < 0) {
730 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n", filename, parsed.uid,
731 strerror(errno));
732 bad++;
733 }
734 }
735
736 if (parsed.has_gid) {
737 if (chown(filename, -1, parsed.gid) < 0) {
738 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n", filename, parsed.gid,
739 strerror(errno));
740 bad++;
741 }
742 }
743
744 if (parsed.has_mode) {
745 if (chmod(filename, parsed.mode) < 0) {
746 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.mode,
747 strerror(errno));
748 bad++;
749 }
750 }
751
752 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
753 if (chmod(filename, parsed.dmode) < 0) {
754 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.dmode,
755 strerror(errno));
756 bad++;
757 }
758 }
759
760 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
761 if (chmod(filename, parsed.fmode) < 0) {
762 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n", filename, parsed.fmode,
763 strerror(errno));
764 bad++;
765 }
766 }
767
768 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
769 if (parsed.capabilities == 0) {
770 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
771 // Report failure unless it's ENODATA (attribute not set)
772 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n", filename,
773 parsed.capabilities, strerror(errno));
774 bad++;
775 }
776 } else {
777 struct vfs_cap_data cap_data;
778 memset(&cap_data, 0, sizeof(cap_data));
779 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
780 cap_data.data[0].permitted = (uint32_t)(parsed.capabilities & 0xffffffff);
781 cap_data.data[0].inheritable = 0;
782 cap_data.data[1].permitted = (uint32_t)(parsed.capabilities >> 32);
783 cap_data.data[1].inheritable = 0;
784 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
785 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n", filename,
786 parsed.capabilities, strerror(errno));
787 bad++;
788 }
789 }
790 }
791
792 return bad;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700793}
794
795// nftw doesn't allow us to pass along context, so we need to use
796// global variables. *sigh*
797static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700798static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700799
Tao Bao039f2da2016-11-22 16:29:50 -0800800static int do_SetMetadataRecursive(const char* filename, const struct stat* statptr, int fileflags,
801 struct FTW* pfwt) {
802 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700803}
804
805static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800806 if ((argc % 2) != 1) {
807 return ErrorAbort(state, kArgsParsingFailure, "%s() expects an odd number of arguments, got %d",
808 name, argc);
809 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700810
Tao Bao039f2da2016-11-22 16:29:50 -0800811 std::vector<std::string> args;
812 if (!ReadArgs(state, argc, argv, &args)) {
813 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
814 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700815
Tao Bao039f2da2016-11-22 16:29:50 -0800816 struct stat sb;
817 if (lstat(args[0].c_str(), &sb) == -1) {
818 return ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s", name,
819 args[0].c_str(), strerror(errno));
820 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700821
Tao Bao039f2da2016-11-22 16:29:50 -0800822 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
823 int bad = 0;
824 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700825
Tao Bao039f2da2016-11-22 16:29:50 -0800826 if (recursive) {
827 recursive_parsed_args = parsed;
828 recursive_state = state;
829 bad += nftw(args[0].c_str(), do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
830 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
831 recursive_state = NULL;
832 } else {
833 bad += ApplyParsedPerms(state, args[0].c_str(), &sb, parsed);
834 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700835
Tao Bao039f2da2016-11-22 16:29:50 -0800836 if (bad > 0) {
837 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
838 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700839
Tao Bao039f2da2016-11-22 16:29:50 -0800840 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700841}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700842
Doug Zongker512536a2010-02-17 16:11:44 -0800843Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -0800844 if (argc != 1) {
845 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
846 }
847 std::string key;
848 if (!Evaluate(state, argv[0], &key)) {
849 return nullptr;
850 }
851 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700852
Tao Bao039f2da2016-11-22 16:29:50 -0800853 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700854}
855
Doug Zongker47cace92009-06-18 10:11:50 -0700856// file_getprop(file, key)
857//
858// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -0700859// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700860// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800861Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800862 if (argc != 2) {
863 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
864 }
865
866 std::vector<std::string> args;
867 if (!ReadArgs(state, 2, argv, &args)) {
868 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
869 }
870 const std::string& filename = args[0];
871 const std::string& key = args[1];
872
873 struct stat st;
874 if (stat(filename.c_str(), &st) < 0) {
875 return ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name,
876 filename.c_str(), strerror(errno));
877 }
878
879 constexpr off_t MAX_FILE_GETPROP_SIZE = 65536;
880 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
881 return ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %lld)",
882 filename.c_str(), name, static_cast<long long>(MAX_FILE_GETPROP_SIZE));
883 }
884
885 std::string buffer(st.st_size, '\0');
886 unique_file f(ota_fopen(filename.c_str(), "rb"));
887 if (f == nullptr) {
888 return ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename.c_str(),
889 strerror(errno));
890 }
891
892 if (ota_fread(&buffer[0], 1, st.st_size, f.get()) != static_cast<size_t>(st.st_size)) {
893 ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s", name,
894 static_cast<size_t>(st.st_size), filename.c_str());
895 return nullptr;
896 }
897
898 ota_fclose(f);
899
900 std::vector<std::string> lines = android::base::Split(buffer, "\n");
901 for (size_t i = 0; i < lines.size(); i++) {
902 std::string line = android::base::Trim(lines[i]);
903
904 // comment or blank line: skip to next line
905 if (line.empty() || line[0] == '#') {
906 continue;
907 }
908 size_t equal_pos = line.find('=');
909 if (equal_pos == std::string::npos) {
910 continue;
Tao Bao51d516e2016-11-03 14:49:01 -0700911 }
912
Tao Bao358c2ec2016-11-28 11:48:43 -0800913 // trim whitespace between key and '='
914 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -0700915
Tao Bao358c2ec2016-11-28 11:48:43 -0800916 // not the key we're looking for
917 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700918
Tao Bao358c2ec2016-11-28 11:48:43 -0800919 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
920 }
Doug Zongker47cace92009-06-18 10:11:50 -0700921
Tao Bao358c2ec2016-11-28 11:48:43 -0800922 return StringValue("");
Doug Zongker47cace92009-06-18 10:11:50 -0700923}
924
Doug Zongker8edb00c2009-06-11 17:21:44 -0700925// apply_patch_space(bytes)
Tao Bao039f2da2016-11-22 16:29:50 -0800926Value* ApplyPatchSpaceFn(const char* name, State* state, int argc, Expr* argv[]) {
927 std::vector<std::string> args;
928 if (!ReadArgs(state, 1, argv, &args)) {
929 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
930 }
931 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800932
Tao Bao039f2da2016-11-22 16:29:50 -0800933 size_t bytes;
934 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
935 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n",
936 name, bytes_str.c_str());
937 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800938
Tao Bao039f2da2016-11-22 16:29:50 -0800939 return StringValue(CacheSizeCheck(bytes) ? "" : "t");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800940}
941
Tao Bao039f2da2016-11-22 16:29:50 -0800942// apply_patch(src_file, tgt_file, tgt_sha1, tgt_size, patch1_sha1, patch1_blob, [...])
943// Applies a binary patch to the src_file to produce the tgt_file. If the desired target is the
944// same as the source, pass "-" for tgt_file. tgt_sha1 and tgt_size are the expected final SHA1
945// hash and size of the target file. The remaining arguments must come in pairs: a SHA1 hash (a
946// 40-character hex string) and a blob. The blob is the patch to be applied when the source
947// file's current contents have the given SHA1.
948//
949// The patching is done in a safe manner that guarantees the target file either has the desired
950// SHA1 hash and size, or it is untouched -- it will not be left in an unrecoverable intermediate
951// state. If the process is interrupted during patching, the target file may be in an intermediate
952// state; a copy exists in the cache partition so restarting the update can successfully update
953// the file.
Doug Zongker512536a2010-02-17 16:11:44 -0800954Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800955 if (argc < 6 || (argc % 2) == 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700956 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an "
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700957 "even number, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700958 }
959
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700960 std::vector<std::string> args;
961 if (!ReadArgs(state, 4, argv, &args)) {
962 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700963 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700964 const std::string& source_filename = args[0];
965 const std::string& target_filename = args[1];
966 const std::string& target_sha1 = args[2];
967 const std::string& target_size_str = args[3];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700968
Tao Baob15fd222015-09-24 11:10:51 -0700969 size_t target_size;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700970 if (!android::base::ParseUint(target_size_str.c_str(), &target_size)) {
971 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count",
972 name, target_size_str.c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800973 }
974
975 int patchcount = (argc-4) / 2;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700976 std::vector<std::unique_ptr<Value>> arg_values;
977 if (!ReadValueArgs(state, argc-4, argv+4, &arg_values)) {
Yabin Cui64be2132016-02-03 18:16:02 -0800978 return nullptr;
979 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700980
Yabin Cui64be2132016-02-03 18:16:02 -0800981 for (int i = 0; i < patchcount; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700982 if (arg_values[i * 2]->type != VAL_STRING) {
983 return ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name,
984 i * 2);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800985 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700986 if (arg_values[i * 2 + 1]->type != VAL_BLOB) {
987 return ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name,
988 i * 2 + 1);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800989 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700990 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700991
Tianjie Xuaced5d92016-10-12 10:55:04 -0700992 std::vector<std::string> patch_sha_str;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700993 std::vector<std::unique_ptr<Value>> patches;
Yabin Cui64be2132016-02-03 18:16:02 -0800994 for (int i = 0; i < patchcount; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700995 patch_sha_str.push_back(arg_values[i * 2]->data);
996 patches.push_back(std::move(arg_values[i * 2 + 1]));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700997 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800998
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700999 int result = applypatch(source_filename.c_str(), target_filename.c_str(),
1000 target_sha1.c_str(), target_size,
1001 patch_sha_str, patches, nullptr);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001002
Tianjie Xuaced5d92016-10-12 10:55:04 -07001003 return StringValue(result == 0 ? "t" : "");
Doug Zongkerc4351c72010-02-22 14:46:32 -08001004}
1005
Tao Bao039f2da2016-11-22 16:29:50 -08001006// apply_patch_check(filename, [sha1, ...])
1007// Returns true if the contents of filename or the temporary copy in the cache partition (if
1008// present) have a SHA-1 checksum equal to one of the given sha1 values. sha1 values are
1009// specified as 40 hex digits. This function differs from sha1_check(read_file(filename),
1010// sha1 [, ...]) in that it knows to check the cache partition copy, so apply_patch_check() will
1011// succeed even if the file was corrupted by an interrupted apply_patch() update.
1012Value* ApplyPatchCheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1013 if (argc < 1) {
1014 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %d", name,
1015 argc);
1016 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001017
Tao Bao039f2da2016-11-22 16:29:50 -08001018 std::vector<std::string> args;
1019 if (!ReadArgs(state, 1, argv, &args)) {
1020 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1021 }
1022 const std::string& filename = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -08001023
Tao Bao039f2da2016-11-22 16:29:50 -08001024 std::vector<std::string> sha1s;
1025 if (!ReadArgs(state, argc - 1, argv + 1, &sha1s)) {
1026 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1027 }
1028 int result = applypatch_check(filename.c_str(), sha1s);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001029
Tao Bao039f2da2016-11-22 16:29:50 -08001030 return StringValue(result == 0 ? "t" : "");
Doug Zongker8edb00c2009-06-11 17:21:44 -07001031}
1032
Tao Bao1107d962015-09-09 17:16:55 -07001033// This is the updater side handler for ui_print() in edify script. Contents
1034// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001035Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -08001036 std::vector<std::string> args;
1037 if (!ReadArgs(state, argc, argv, &args)) {
1038 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1039 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001040
Tao Bao039f2da2016-11-22 16:29:50 -08001041 std::string buffer = android::base::Join(args, "") + "\n";
1042 uiPrint(state, buffer);
1043 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001044}
1045
Doug Zongkerd0181b82011-10-19 10:51:12 -07001046Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -08001047 if (argc != 0) {
1048 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
1049 }
1050 fprintf(static_cast<UpdaterInfo*>(state->cookie)->cmd_pipe, "wipe_cache\n");
1051 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -07001052}
1053
Doug Zongker512536a2010-02-17 16:11:44 -08001054Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -08001055 if (argc < 1) {
1056 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
1057 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001058
Tao Bao039f2da2016-11-22 16:29:50 -08001059 std::vector<std::string> args;
1060 if (!ReadArgs(state, argc, argv, &args)) {
1061 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1062 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001063
Tao Bao039f2da2016-11-22 16:29:50 -08001064 char* args2[argc + 1];
1065 for (int i = 0; i < argc; i++) {
1066 args2[i] = &args[i][0];
1067 }
1068 args2[argc] = nullptr;
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001069
Tao Bao039f2da2016-11-22 16:29:50 -08001070 LOG(INFO) << "about to run program [" << args2[0] << "] with " << argc << " args";
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001071
Tao Bao039f2da2016-11-22 16:29:50 -08001072 pid_t child = fork();
1073 if (child == 0) {
1074 execv(args2[0], args2);
1075 PLOG(ERROR) << "run_program: execv failed";
1076 _exit(1);
1077 }
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001078
Tao Bao039f2da2016-11-22 16:29:50 -08001079 int status;
1080 waitpid(child, &status, 0);
1081 if (WIFEXITED(status)) {
1082 if (WEXITSTATUS(status) != 0) {
1083 LOG(ERROR) << "run_program: child exited with status " << WEXITSTATUS(status);
1084 }
1085 } else if (WIFSIGNALED(status)) {
1086 LOG(ERROR) << "run_program: child terminated by signal " << WTERMSIG(status);
1087 }
1088
1089 return StringValue(std::to_string(status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001090}
1091
Doug Zongker512536a2010-02-17 16:11:44 -08001092// sha1_check(data)
1093// to return the sha1 of the data (given in the format returned by
1094// read_file).
1095//
1096// sha1_check(data, sha1_hex, [sha1_hex, ...])
1097// returns the sha1 of the file if it matches any of the hex
1098// strings passed, or "" if it does not equal any of them.
1099//
1100Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -08001101 if (argc < 1) {
1102 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
1103 }
Doug Zongker512536a2010-02-17 16:11:44 -08001104
Tao Bao039f2da2016-11-22 16:29:50 -08001105 std::vector<std::unique_ptr<Value>> args;
1106 if (!ReadValueArgs(state, argc, argv, &args)) {
1107 return nullptr;
1108 }
Doug Zongker512536a2010-02-17 16:11:44 -08001109
Tao Bao039f2da2016-11-22 16:29:50 -08001110 if (args[0]->type == VAL_INVALID) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001111 return StringValue("");
Tao Bao039f2da2016-11-22 16:29:50 -08001112 }
1113 uint8_t digest[SHA_DIGEST_LENGTH];
1114 SHA1(reinterpret_cast<const uint8_t*>(args[0]->data.c_str()), args[0]->data.size(), digest);
1115
1116 if (argc == 1) {
1117 return StringValue(print_sha1(digest));
1118 }
1119
1120 for (int i = 1; i < argc; ++i) {
1121 uint8_t arg_digest[SHA_DIGEST_LENGTH];
1122 if (args[i]->type != VAL_STRING) {
1123 LOG(ERROR) << name << "(): arg " << i << " is not a string; skipping";
1124 } else if (ParseSha1(args[i]->data.c_str(), arg_digest) != 0) {
1125 // Warn about bad args and skip them.
1126 LOG(ERROR) << name << "(): error parsing \"" << args[i]->data << "\" as sha-1; skipping";
1127 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
1128 // Found a match.
1129 return args[i].release();
1130 }
1131 }
1132
1133 // Didn't match any of the hex strings; return false.
1134 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -08001135}
1136
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001137// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001138// is actually a FileContents*).
1139Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -08001140 if (argc != 1) {
1141 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
1142 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001143
Tao Bao039f2da2016-11-22 16:29:50 -08001144 std::vector<std::string> args;
1145 if (!ReadArgs(state, 1, argv, &args)) {
1146 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1147 }
1148 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -08001149
Tao Bao039f2da2016-11-22 16:29:50 -08001150 Value* v = new Value(VAL_INVALID, "");
Doug Zongker512536a2010-02-17 16:11:44 -08001151
Tao Bao039f2da2016-11-22 16:29:50 -08001152 FileContents fc;
1153 if (LoadFileContents(filename.c_str(), &fc) == 0) {
1154 v->type = VAL_BLOB;
1155 v->data = std::string(fc.data.begin(), fc.data.end());
1156 }
1157 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001158}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001159
Tao Baod0f30882016-11-03 23:52:01 -07001160// write_value(value, filename)
1161// Writes 'value' to 'filename'.
1162// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
1163Value* WriteValueFn(const char* name, State* state, int argc, Expr* argv[]) {
1164 if (argc != 2) {
1165 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
1166 }
1167
1168 std::vector<std::string> args;
1169 if (!ReadArgs(state, 2, argv, &args)) {
1170 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1171 }
1172
1173 const std::string& filename = args[1];
1174 if (filename.empty()) {
1175 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
1176 }
1177
1178 const std::string& value = args[0];
1179 if (!android::base::WriteStringToFile(value, filename)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001180 PLOG(ERROR) << name << ": Failed to write to \"" << filename << "\"";
Tao Baod0f30882016-11-03 23:52:01 -07001181 return StringValue("");
1182 } else {
1183 return StringValue("t");
1184 }
1185}
1186
Doug Zongkerc87bab12013-11-25 13:53:25 -08001187// Immediately reboot the device. Recovery is not finished normally,
1188// so if you reboot into recovery it will re-start applying the
1189// current package (because nothing has cleared the copy of the
1190// arguments stored in the BCB).
1191//
1192// The argument is the partition name passed to the android reboot
1193// property. It can be "recovery" to boot from the recovery
1194// partition, or "" (empty string) to boot from the regular boot
1195// partition.
1196Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001197 if (argc != 2) {
1198 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
1199 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001200
Tao Baobedf5fc2016-11-18 12:01:26 -08001201 std::vector<std::string> args;
1202 if (!ReadArgs(state, 2, argv, &args)) {
1203 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1204 }
1205 const std::string& filename = args[0];
1206 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001207
Tao Baobedf5fc2016-11-18 12:01:26 -08001208 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
1209 bootloader_message boot;
1210 std::string err;
1211 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001212 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001213 return StringValue("");
1214 }
1215 memset(boot.command, 0, sizeof(boot.command));
1216 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001217 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001218 return StringValue("");
1219 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001220
Tao Baobedf5fc2016-11-18 12:01:26 -08001221 const std::string reboot_cmd = "reboot," + property;
1222 android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001223
Tao Baobedf5fc2016-11-18 12:01:26 -08001224 sleep(5);
1225 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001226}
1227
1228// Store a string value somewhere that future invocations of recovery
1229// can access it. This value is called the "stage" and can be used to
1230// drive packages that need to do reboots in the middle of
1231// installation and keep track of where they are in the multi-stage
1232// install.
1233//
1234// The first argument is the block device for the misc partition
1235// ("/misc" in the fstab), which is where this value is stored. The
1236// second argument is the string to store; it should not exceed 31
1237// bytes.
1238Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001239 if (argc != 2) {
1240 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
1241 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001242
Tao Baobedf5fc2016-11-18 12:01:26 -08001243 std::vector<std::string> args;
1244 if (!ReadArgs(state, 2, argv, &args)) {
1245 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1246 }
1247 const std::string& filename = args[0];
1248 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001249
Tao Baobedf5fc2016-11-18 12:01:26 -08001250 // Store this value in the misc partition, immediately after the
1251 // bootloader message that the main recovery uses to save its
1252 // arguments in case of the device restarting midway through
1253 // package installation.
1254 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 }
1260 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
1261 if (!write_bootloader_message_to(boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001262 LOG(ERROR) << name << "(): Failed to write to \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001263 return StringValue("");
1264 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001265
Tao Baobedf5fc2016-11-18 12:01:26 -08001266 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001267}
1268
1269// Return the value most recently saved with SetStageFn. The argument
1270// is the block device for the misc partition.
1271Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001272 if (argc != 1) {
1273 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
1274 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001275
Tao Baobedf5fc2016-11-18 12:01:26 -08001276 std::vector<std::string> args;
1277 if (!ReadArgs(state, 1, argv, &args)) {
1278 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1279 }
1280 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001281
Tao Baobedf5fc2016-11-18 12:01:26 -08001282 bootloader_message boot;
1283 std::string err;
1284 if (!read_bootloader_message_from(&boot, filename, &err)) {
Tao Bao039f2da2016-11-22 16:29:50 -08001285 LOG(ERROR) << name << "(): Failed to read from \"" << filename << "\": " << err;
Tao Baobedf5fc2016-11-18 12:01:26 -08001286 return StringValue("");
1287 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001288
Tao Baobedf5fc2016-11-18 12:01:26 -08001289 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001290}
1291
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001292Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao358c2ec2016-11-28 11:48:43 -08001293 if (argc != 2) {
1294 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
1295 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001296
Tao Bao358c2ec2016-11-28 11:48:43 -08001297 std::vector<std::string> args;
1298 if (!ReadArgs(state, 2, argv, &args)) {
1299 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1300 }
1301 const std::string& filename = args[0];
1302 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001303
Tao Bao358c2ec2016-11-28 11:48:43 -08001304 size_t len;
1305 if (!android::base::ParseUint(len_str.c_str(), &len)) {
1306 return nullptr;
1307 }
1308 unique_fd fd(ota_open(filename.c_str(), O_WRONLY, 0644));
1309 // The wipe_block_device function in ext4_utils returns 0 on success and 1
1310 // for failure.
1311 int status = wipe_block_device(fd, len);
1312 return StringValue((status == 0) ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001313}
1314
Doug Zongkerc704e062014-05-23 08:40:35 -07001315Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -08001316 if (argc != 0) {
1317 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
1318 }
1319 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
1320 fprintf(ui->cmd_pipe, "enable_reboot\n");
1321 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -07001322}
1323
Michael Rungeacf47db2014-11-21 00:12:28 -08001324Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao039f2da2016-11-22 16:29:50 -08001325 if (argc == 0) {
1326 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %d", name, argc);
1327 }
Michael Rungeacf47db2014-11-21 00:12:28 -08001328
Tao Bao039f2da2016-11-22 16:29:50 -08001329 std::vector<std::string> args;
1330 if (!ReadArgs(state, argc, argv, &args)) {
1331 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
1332 }
Michael Rungeacf47db2014-11-21 00:12:28 -08001333
Tao Bao039f2da2016-11-22 16:29:50 -08001334 char* args2[argc + 1];
1335 // Tune2fs expects the program name as its args[0]
1336 args2[0] = const_cast<char*>(name);
1337 if (args2[0] == nullptr) {
1338 return nullptr;
1339 }
1340 for (int i = 0; i < argc; ++i) {
1341 args2[i + 1] = &args[i][0];
1342 }
Michael Rungeacf47db2014-11-21 00:12:28 -08001343
Tao Bao039f2da2016-11-22 16:29:50 -08001344 // tune2fs changes the file system parameters on an ext2 file system; it
1345 // returns 0 on success.
1346 int result = tune2fs_main(argc + 1, args2);
1347 if (result != 0) {
1348 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
1349 }
1350 return StringValue("t");
Michael Rungeacf47db2014-11-21 00:12:28 -08001351}
1352
Doug Zongker9931f7f2009-06-10 14:11:53 -07001353void RegisterInstallFunctions() {
Tao Bao039f2da2016-11-22 16:29:50 -08001354 RegisterFunction("mount", MountFn);
1355 RegisterFunction("is_mounted", IsMountedFn);
1356 RegisterFunction("unmount", UnmountFn);
1357 RegisterFunction("format", FormatFn);
1358 RegisterFunction("show_progress", ShowProgressFn);
1359 RegisterFunction("set_progress", SetProgressFn);
1360 RegisterFunction("delete", DeleteFn);
1361 RegisterFunction("delete_recursive", DeleteFn);
1362 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1363 RegisterFunction("package_extract_file", PackageExtractFileFn);
1364 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001365
Tao Bao039f2da2016-11-22 16:29:50 -08001366 // Usage:
1367 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1368 // Example:
1369 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel",
1370 // "u:object_r:system_file:s0", "capabilities", 0x0);
1371 RegisterFunction("set_metadata", SetMetadataFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001372
Tao Bao039f2da2016-11-22 16:29:50 -08001373 // Usage:
1374 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1375 // Example:
1376 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755,
1377 // "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1378 RegisterFunction("set_metadata_recursive", SetMetadataFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001379
Tao Bao039f2da2016-11-22 16:29:50 -08001380 RegisterFunction("getprop", GetPropFn);
1381 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001382
Tao Bao039f2da2016-11-22 16:29:50 -08001383 RegisterFunction("apply_patch", ApplyPatchFn);
1384 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1385 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001386
Tao Bao039f2da2016-11-22 16:29:50 -08001387 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001388
Tao Bao039f2da2016-11-22 16:29:50 -08001389 RegisterFunction("read_file", ReadFileFn);
1390 RegisterFunction("sha1_check", Sha1CheckFn);
1391 RegisterFunction("rename", RenameFn);
1392 RegisterFunction("write_value", WriteValueFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001393
Tao Bao039f2da2016-11-22 16:29:50 -08001394 RegisterFunction("wipe_cache", WipeCacheFn);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001395
Tao Bao039f2da2016-11-22 16:29:50 -08001396 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001397
Tao Bao039f2da2016-11-22 16:29:50 -08001398 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001399
Tao Bao039f2da2016-11-22 16:29:50 -08001400 RegisterFunction("reboot_now", RebootNowFn);
1401 RegisterFunction("get_stage", GetStageFn);
1402 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001403
Tao Bao039f2da2016-11-22 16:29:50 -08001404 RegisterFunction("enable_reboot", EnableRebootFn);
1405 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001406}