blob: 8db5c1fe038def45ffd481c9ccc18e44df9fac0c [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>
Tianjie Xu5fe280a2016-10-17 18:15:20 -070043#include <android-base/parsedouble.h>
Tao Baod0f30882016-11-03 23:52:01 -070044#include <android-base/parseint.h>
Elliott Hughescb220402016-09-23 15:30:55 -070045#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080046#include <android-base/stringprintf.h>
Tao Baod0f30882016-11-03 23:52:01 -070047#include <android-base/strings.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070048#include <cutils/android_reboot.h>
Tao Baode40ba52016-10-05 23:17:01 -070049#include <ext4_utils/make_ext4fs.h>
50#include <ext4_utils/wipe.h>
Tao Bao361342c2016-02-08 11:15:50 -080051#include <openssl/sha.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070052#include <selinux/label.h>
53#include <selinux/selinux.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070054#include <ziparchive/zip_archive.h>
Tao Bao1107d962015-09-09 17:16:55 -070055
Doug Zongkerc87bab12013-11-25 13:53:25 -080056#include "applypatch/applypatch.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070057#include "bootloader.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070058#include "edify/expr.h"
Tianjie Xu16255832016-04-30 11:49:59 -070059#include "error_code.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070060#include "mounts.h"
Jed Estep39c1b5e2015-12-15 16:04:53 -080061#include "ota_io.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070062#include "otautil/DirUtil.h"
63#include "otautil/ZipUtil.h"
Tao Bao361342c2016-02-08 11:15:50 -080064#include "print_sha1.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080065#include "tune2fs.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070066#include "updater/updater.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070067
Tao Bao1107d962015-09-09 17:16:55 -070068// Send over the buffer to recovery though the command pipe.
69static void uiPrint(State* state, const std::string& buffer) {
Tao Bao0831d0b2016-11-03 23:25:04 -070070 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070071
Tao Bao1107d962015-09-09 17:16:55 -070072 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
73 // So skip sending empty strings to UI.
74 std::vector<std::string> lines = android::base::Split(buffer, "\n");
75 for (auto& line: lines) {
76 if (!line.empty()) {
77 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
78 fprintf(ui->cmd_pipe, "ui_print\n");
79 }
80 }
81
82 // On the updater side, we need to dump the contents to stderr (which has
83 // been redirected to the log file). Because the recovery will only print
84 // the contents to screen when processing pipe command ui_print.
85 fprintf(stderr, "%s", buffer.c_str());
Michael Runged4a63422014-10-22 19:48:41 -070086}
87
Elliott Hughes83ce7552016-06-30 09:28:42 -070088void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) {
Tao Bao1107d962015-09-09 17:16:55 -070089 std::string error_msg;
90
Michael Runged4a63422014-10-22 19:48:41 -070091 va_list ap;
92 va_start(ap, format);
Tao Bao1107d962015-09-09 17:16:55 -070093 android::base::StringAppendV(&error_msg, format, ap);
Michael Runged4a63422014-10-22 19:48:41 -070094 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070095
Michael Runged4a63422014-10-22 19:48:41 -070096 uiPrint(state, error_msg);
97}
98
Tianjie Xud75003d2016-11-04 11:31:29 -070099static bool is_dir(const std::string& dirpath) {
100 struct stat st;
101 return stat(dirpath.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
102}
103
Tao Bao0c7839a2016-10-10 15:48:37 -0700104// Create all parent directories of name, if necessary.
Tianjie Xud75003d2016-11-04 11:31:29 -0700105static bool make_parents(const std::string& name) {
106 size_t prev_end = 0;
107 while (prev_end < name.size()) {
108 size_t next_end = name.find('/', prev_end + 1);
109 if (next_end == std::string::npos) {
110 break;
Tao Bao0c7839a2016-10-10 15:48:37 -0700111 }
Tianjie Xud75003d2016-11-04 11:31:29 -0700112 std::string dir_path = name.substr(0, next_end);
113 if (!is_dir(dir_path)) {
114 int result = mkdir(dir_path.c_str(), 0700);
115 if (result != 0) {
116 printf("failed to mkdir %s when make parents for %s: %s\n", dir_path.c_str(),
117 name.c_str(), strerror(errno));
118 return false;
119 }
120
121 printf("created [%s]\n", dir_path.c_str());
122 }
123 prev_end = next_end;
Tao Bao0c7839a2016-10-10 15:48:37 -0700124 }
Tianjie Xud75003d2016-11-04 11:31:29 -0700125 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[]) {
Michael Runge168f7772014-10-22 17:05:08 -0700133 if (argc != 4 && argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700134 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700135 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700136
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
Michael Runge168f7772014-10-22 17:05:08 -0700147 if (argc == 5) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700148 mount_options = args[4];
Doug Zongker9931f7f2009-06-10 14:11:53 -0700149 }
150
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700151 if (fs_type.empty()) {
152 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty",
153 name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700154 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700155 if (partition_type.empty()) {
156 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
157 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700158 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700159 if (location.empty()) {
160 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty",
161 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700162 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700163 if (mount_point.empty()) {
164 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
165 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700166 }
167
Tao Baoba9a42a2015-06-23 23:23:33 -0700168 {
169 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500170
Tao Baoba9a42a2015-06-23 23:23:33 -0700171 if (sehandle) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700172 selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755);
Tao Baoba9a42a2015-06-23 23:23:33 -0700173 setfscreatecon(secontext);
174 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500175
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700176 mkdir(mount_point.c_str(), 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700177
Tao Baoba9a42a2015-06-23 23:23:33 -0700178 if (secontext) {
179 freecon(secontext);
180 setfscreatecon(NULL);
181 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500182 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500183
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700184 if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(),
185 MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) {
Elliott Hughes63a31922016-06-09 17:41:22 -0700186 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700187 name, location.c_str(), mount_point.c_str(), strerror(errno));
188 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700189 }
190
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700191 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700192}
193
Doug Zongker8edb00c2009-06-11 17:21:44 -0700194
195// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800196Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700197 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700198 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700199 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700200
201 std::vector<std::string> args;
202 if (!ReadArgs(state, argc, argv, &args)) {
203 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700204 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700205 const std::string& mount_point = args[0];
206 if (mount_point.empty()) {
207 return ErrorAbort(state, kArgsParsingFailure,
208 "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700209 }
210
211 scan_mounted_volumes();
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700212 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
213 if (vol == nullptr) {
214 return StringValue("");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700215 }
216
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700217 return StringValue(mount_point);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700218}
219
220
Doug Zongker512536a2010-02-17 16:11:44 -0800221Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700222 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700223 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700224 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700225 std::vector<std::string> args;
226 if (!ReadArgs(state, argc, argv, &args)) {
227 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700228 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700229 const std::string& mount_point = args[0];
230 if (mount_point.empty()) {
231 return ErrorAbort(state, kArgsParsingFailure,
232 "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700233 }
234
235 scan_mounted_volumes();
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700236 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
237 if (vol == nullptr) {
238 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point.c_str());
239 return nullptr;
240 } else {
241 int ret = unmount_mounted_volume(vol);
242 if (ret != 0) {
243 uiPrintf(state, "unmount of %s failed (%d): %s\n",
244 mount_point.c_str(), ret, strerror(errno));
Michael Runge5ddf4292014-10-24 14:14:41 -0700245 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700246 }
247
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700248 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700249}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700250
JP Abgrall37aedb32014-06-16 19:07:39 -0700251static int exec_cmd(const char* path, char* const argv[]) {
252 int status;
253 pid_t child;
254 if ((child = vfork()) == 0) {
255 execv(path, argv);
256 _exit(-1);
257 }
258 waitpid(child, &status, 0);
259 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
260 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
261 }
262 return WEXITSTATUS(status);
263}
264
Stephen Smalley779701d2012-02-09 14:13:23 -0500265// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700266//
Stephen Smalley779701d2012-02-09 14:13:23 -0500267// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700268// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
269// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800270// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700271// 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 -0800272Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Stephen Smalley516e4e22012-04-03 13:35:11 -0400273 if (argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700274 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700275 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500276
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700277 std::vector<std::string> args;
278 if (!ReadArgs(state, argc, argv, &args)) {
279 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
280 }
281 const std::string& fs_type = args[0];
282 const std::string& partition_type = args[1];
283 const std::string& location = args[2];
284 const std::string& fs_size = args[3];
285 const std::string& mount_point = args[4];
286
287 if (fs_type.empty()) {
288 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty",
289 name);
290 }
291 if (partition_type.empty()) {
292 return ErrorAbort(state, kArgsParsingFailure,
293 "partition_type argument to %s() can't be empty", name);
294 }
295 if (location.empty()) {
296 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty",
297 name);
298 }
299 if (mount_point.empty()) {
300 return ErrorAbort(state, kArgsParsingFailure,
301 "mount_point argument to %s() can't be empty", name);
Stephen Smalley779701d2012-02-09 14:13:23 -0500302 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700303
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700304 int64_t size;
Tao Baoa659d792016-11-03 23:25:04 -0700305 if (!android::base::ParseInt(fs_size, &size)) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700306 return ErrorAbort(state, kArgsParsingFailure,
307 "%s: failed to parse int in %s\n", name, fs_size.c_str());
Doug Zongker9931f7f2009-06-10 14:11:53 -0700308 }
309
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700310 if (fs_type == "ext4") {
311 int status = make_ext4fs(location.c_str(), size, mount_point.c_str(), sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700312 if (status != 0) {
Tao Bao0831d0b2016-11-03 23:25:04 -0700313 printf("%s: make_ext4fs failed (%d) on %s", name, status, location.c_str());
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700314 return StringValue("");
Doug Zongker3d177d02010-07-01 09:18:44 -0700315 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700316 return StringValue(location);
317 } else if (fs_type == "f2fs") {
318 if (size < 0) {
Tao Bao0831d0b2016-11-03 23:25:04 -0700319 printf("%s: fs_size can't be negative for f2fs: %s", name, fs_size.c_str());
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700320 return StringValue("");
JP Abgrall37aedb32014-06-16 19:07:39 -0700321 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700322 std::string num_sectors = std::to_string(size / 512);
323
JP Abgrall37aedb32014-06-16 19:07:39 -0700324 const char *f2fs_path = "/sbin/mkfs.f2fs";
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700325 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location.c_str(),
326 num_sectors.c_str(), nullptr};
JP Abgrall37aedb32014-06-16 19:07:39 -0700327 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
JP Abgrall37aedb32014-06-16 19:07:39 -0700328 if (status != 0) {
Tao Bao0831d0b2016-11-03 23:25:04 -0700329 printf("%s: mkfs.f2fs failed (%d) on %s", name, status, location.c_str());
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700330 return StringValue("");
JP Abgrall37aedb32014-06-16 19:07:39 -0700331 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700332 return StringValue(location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700333 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700334 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700335 name, fs_type.c_str(), partition_type.c_str());
Doug Zongker9931f7f2009-06-10 14:11:53 -0700336 }
337
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700338 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700339}
340
Tao Baoa659d792016-11-03 23:25:04 -0700341// rename(src_name, dst_name)
342// Renames src_name to dst_name. It automatically creates the necessary directories for dst_name.
343// Example: rename("system/app/Hangouts/Hangouts.apk", "system/priv-app/Hangouts/Hangouts.apk")
Michael Rungece7ca712013-11-06 17:42:20 -0800344Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
Michael Rungece7ca712013-11-06 17:42:20 -0800345 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700346 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Michael Rungece7ca712013-11-06 17:42:20 -0800347 }
348
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700349 std::vector<std::string> args;
350 if (!ReadArgs(state, argc, argv, &args)) {
351 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
352 }
353 const std::string& src_name = args[0];
Tianjie Xud75003d2016-11-04 11:31:29 -0700354 const std::string& dst_name = args[1];
Michael Rungece7ca712013-11-06 17:42:20 -0800355
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700356 if (src_name.empty()) {
357 return ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty",
358 name);
Michael Rungece7ca712013-11-06 17:42:20 -0800359 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700360 if (dst_name.empty()) {
361 return ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty",
362 name);
Michael Rungece7ca712013-11-06 17:42:20 -0800363 }
Tianjie Xud75003d2016-11-04 11:31:29 -0700364 if (!make_parents(dst_name)) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700365 return ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
366 dst_name.c_str(), strerror(errno));
367 } else if (access(dst_name.c_str(), F_OK) == 0 && access(src_name.c_str(), F_OK) != 0) {
Michael Runge2f0ef732014-10-22 14:28:23 -0700368 // File was already moved
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700369 return StringValue(dst_name);
370 } else if (rename(src_name.c_str(), dst_name.c_str()) != 0) {
371 return ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
372 src_name.c_str(), dst_name.c_str(), strerror(errno));
Michael Rungece7ca712013-11-06 17:42:20 -0800373 }
374
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700375 return StringValue(dst_name);
Michael Rungece7ca712013-11-06 17:42:20 -0800376}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700377
Tao Bao0831d0b2016-11-03 23:25:04 -0700378// delete([filename, ...])
379// Deletes all the filenames listed. Returns the number of files successfully deleted.
380//
381// delete_recursive([dirname, ...])
382// Recursively deletes dirnames and all their contents. Returns the number of directories
383// successfully deleted.
Doug Zongker512536a2010-02-17 16:11:44 -0800384Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao0831d0b2016-11-03 23:25:04 -0700385 std::vector<std::string> paths(argc);
Tao Baoba9a42a2015-06-23 23:23:33 -0700386 for (int i = 0; i < argc; ++i) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700387 if (!Evaluate(state, argv[i], &paths[i])) {
388 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700389 }
390 }
391
392 bool recursive = (strcmp(name, "delete_recursive") == 0);
393
394 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700395 for (int i = 0; i < argc; ++i) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700396 if ((recursive ? dirUnlinkHierarchy(paths[i].c_str()) : unlink(paths[i].c_str())) == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700397 ++success;
Tianjie Xuaced5d92016-10-12 10:55:04 -0700398 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700399 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700400
Tao Bao0831d0b2016-11-03 23:25:04 -0700401 return StringValue(std::to_string(success));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700402}
403
Doug Zongker8edb00c2009-06-11 17:21:44 -0700404
Doug Zongker512536a2010-02-17 16:11:44 -0800405Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700406 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700407 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700408 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700409
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700410 std::vector<std::string> args;
411 if (!ReadArgs(state, argc, argv, &args)) {
412 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
413 }
414 const std::string& frac_str = args[0];
415 const std::string& sec_str = args[1];
416
417 double frac;
418 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
419 return ErrorAbort(state, kArgsParsingFailure,
420 "%s: failed to parse double in %s\n", name, frac_str.c_str());
421 }
Tao Baob15fd222015-09-24 11:10:51 -0700422 int sec;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700423 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
424 return ErrorAbort(state, kArgsParsingFailure,
425 "%s: failed to parse int in %s\n", name, sec_str.c_str());
426 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700427
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700428 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700429 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
430
Doug Zongker512536a2010-02-17 16:11:44 -0800431 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700432}
433
Doug Zongker512536a2010-02-17 16:11:44 -0800434Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700435 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700436 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700437 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700438
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700439 std::vector<std::string> args;
440 if (!ReadArgs(state, 1, argv, &args)) {
441 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
442 }
443 const std::string& frac_str = args[0];
444
445 double frac;
446 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
447 return ErrorAbort(state, kArgsParsingFailure,
448 "%s: failed to parse double in %s\n", name, frac_str.c_str());
449 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700450
451 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
452 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
453
Doug Zongker512536a2010-02-17 16:11:44 -0800454 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700455}
456
Tao Bao1036d362016-11-17 22:49:56 -0800457// package_extract_dir(package_dir, dest_dir)
458// Extracts all files from the package underneath package_dir and writes them to the
459// corresponding tree beneath dest_dir. Any existing files are overwritten.
460// Example: package_extract_dir("system", "/system")
461//
462// Note: package_dir needs to be a relative path; dest_dir needs to be an absolute path.
463Value* PackageExtractDirFn(const char* name, State* state, int argc, Expr* argv[]) {
464 if (argc != 2) {
465 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
466 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700467
Tao Bao1036d362016-11-17 22:49:56 -0800468 std::vector<std::string> args;
469 if (!ReadArgs(state, 2, argv, &args)) {
470 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
471 }
472 const std::string& zip_path = args[0];
473 const std::string& dest_path = args[1];
Doug Zongker9931f7f2009-06-10 14:11:53 -0700474
Tao Bao1036d362016-11-17 22:49:56 -0800475 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700476
Tao Bao1036d362016-11-17 22:49:56 -0800477 // To create a consistent system image, never use the clock for timestamps.
478 constexpr struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
Doug Zongker9931f7f2009-06-10 14:11:53 -0700479
Tao Bao1036d362016-11-17 22:49:56 -0800480 bool success = ExtractPackageRecursive(za, zip_path, dest_path, &timestamp, sehandle);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700481
Tao Bao1036d362016-11-17 22:49:56 -0800482 return StringValue(success ? "t" : "");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700483}
484
Tao Baoef0eb3b2016-11-14 21:29:52 -0800485// package_extract_file(package_file[, dest_file])
486// Extracts a single package_file from the update package and writes it to dest_file,
487// overwriting existing files if necessary. Without the dest_file argument, returns the
488// contents of the package file as a binary blob.
489Value* PackageExtractFileFn(const char* name, State* state, int argc, Expr* argv[]) {
490 if (argc < 1 || argc > 2) {
491 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %d", name, argc);
492 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700493
Tao Baoef0eb3b2016-11-14 21:29:52 -0800494 if (argc == 2) {
495 // The two-argument version extracts to a file.
496
497 std::vector<std::string> args;
498 if (!ReadArgs(state, 2, argv, &args)) {
499 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %d args", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700500 }
Tao Baoef0eb3b2016-11-14 21:29:52 -0800501 const std::string& zip_path = args[0];
502 const std::string& dest_path = args[1];
Doug Zongker43772d22014-06-09 14:13:19 -0700503
Tao Baoef0eb3b2016-11-14 21:29:52 -0800504 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
505 ZipString zip_string_path(zip_path.c_str());
506 ZipEntry entry;
507 if (FindEntry(za, zip_string_path, &entry) != 0) {
508 printf("%s: no %s in package\n", name, zip_path.c_str());
509 return StringValue("");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700510 }
Tao Baoef0eb3b2016-11-14 21:29:52 -0800511
Tao Bao358c2ec2016-11-28 11:48:43 -0800512 unique_fd fd(TEMP_FAILURE_RETRY(
513 ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)));
Tao Baoef0eb3b2016-11-14 21:29:52 -0800514 if (fd == -1) {
515 printf("%s: can't open %s for write: %s\n", name, dest_path.c_str(), strerror(errno));
516 return StringValue("");
517 }
518
519 bool success = true;
520 int32_t ret = ExtractEntryToFile(za, &entry, fd);
521 if (ret != 0) {
522 printf("%s: Failed to extract entry \"%s\" (%u bytes) to \"%s\": %s\n", name,
523 zip_path.c_str(), entry.uncompressed_length, dest_path.c_str(), ErrorCodeString(ret));
524 success = false;
525 }
526 if (ota_fsync(fd) == -1) {
527 printf("fsync of \"%s\" failed: %s\n", dest_path.c_str(), strerror(errno));
528 success = false;
529 }
530 if (ota_close(fd) == -1) {
531 printf("close of \"%s\" failed: %s\n", dest_path.c_str(), strerror(errno));
532 success = false;
533 }
534
535 return StringValue(success ? "t" : "");
536 } else {
537 // The one-argument version returns the contents of the file as the result.
538
539 std::vector<std::string> args;
540 if (!ReadArgs(state, 1, argv, &args)) {
541 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %d args", name, argc);
542 }
543 const std::string& zip_path = args[0];
544
545 ZipArchiveHandle za = static_cast<UpdaterInfo*>(state->cookie)->package_zip;
546 ZipString zip_string_path(zip_path.c_str());
547 ZipEntry entry;
548 if (FindEntry(za, zip_string_path, &entry) != 0) {
549 return ErrorAbort(state, kPackageExtractFileFailure, "%s(): no %s in package", name,
550 zip_path.c_str());
551 }
552
553 std::string buffer;
554 buffer.resize(entry.uncompressed_length);
555
556 int32_t ret = ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&buffer[0]), buffer.size());
557 if (ret != 0) {
558 return ErrorAbort(state, kPackageExtractFileFailure,
559 "%s: Failed to extract entry \"%s\" (%zu bytes) to memory: %s", name,
560 zip_path.c_str(), buffer.size(), ErrorCodeString(ret));
561 }
562
563 return new Value(VAL_BLOB, buffer);
564 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700565}
566
Tao Bao89929022016-11-08 20:51:31 -0800567// symlink(target, [src1, src2, ...])
568// Creates all sources as symlinks to target. It unlinks any previously existing src1, src2, etc
569// before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800570Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700571 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700572 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700573 }
Tianjie Xuaced5d92016-10-12 10:55:04 -0700574 std::string target;
575 if (!Evaluate(state, argv[0], &target)) {
576 return nullptr;
577 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700578
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700579 std::vector<std::string> srcs;
580 if (!ReadArgs(state, argc-1, argv+1, &srcs)) {
Tao Bao89929022016-11-08 20:51:31 -0800581 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)",
582 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700583 }
584
Tao Bao89929022016-11-08 20:51:31 -0800585 size_t bad = 0;
586 for (const auto& src : srcs) {
587 if (unlink(src.c_str()) == -1 && errno != ENOENT) {
588 printf("%s: failed to remove %s: %s\n", name, src.c_str(), strerror(errno));
589 ++bad;
590 } else if (!make_parents(src)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700591 printf("%s: failed to symlink %s to %s: making parents failed\n",
Tao Bao89929022016-11-08 20:51:31 -0800592 name, src.c_str(), target.c_str());
Doug Zongkera23075f2012-08-06 16:19:09 -0700593 ++bad;
Tao Bao89929022016-11-08 20:51:31 -0800594 } else if (symlink(target.c_str(), src.c_str()) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700595 printf("%s: failed to symlink %s to %s: %s\n",
Tao Bao89929022016-11-08 20:51:31 -0800596 name, src.c_str(), target.c_str(), strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700597 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700598 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700599 }
Tao Bao89929022016-11-08 20:51:31 -0800600 if (bad != 0) {
601 return ErrorAbort(state, kSymlinkFailure, "%s: Failed to create %zu symlink(s)", name, bad);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700602 }
Tao Bao89929022016-11-08 20:51:31 -0800603 return StringValue("t");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700604}
605
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700606struct perm_parsed_args {
607 bool has_uid;
608 uid_t uid;
609 bool has_gid;
610 gid_t gid;
611 bool has_mode;
612 mode_t mode;
613 bool has_fmode;
614 mode_t fmode;
615 bool has_dmode;
616 mode_t dmode;
617 bool has_selabel;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700618 const char* selabel;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700619 bool has_capabilities;
620 uint64_t capabilities;
621};
622
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700623static struct perm_parsed_args ParsePermArgs(State * state, int argc,
624 const std::vector<std::string>& args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700625 int i;
626 struct perm_parsed_args parsed;
627 int bad = 0;
628 static int max_warnings = 20;
629
630 memset(&parsed, 0, sizeof(parsed));
631
632 for (i = 1; i < argc; i += 2) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700633 if (args[i] == "uid") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700634 int64_t uid;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700635 if (sscanf(args[i+1].c_str(), "%" SCNd64, &uid) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700636 parsed.uid = uid;
637 parsed.has_uid = true;
638 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700639 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700640 bad++;
641 }
642 continue;
643 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700644 if (args[i] == "gid") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700645 int64_t gid;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700646 if (sscanf(args[i+1].c_str(), "%" SCNd64, &gid) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700647 parsed.gid = gid;
648 parsed.has_gid = true;
649 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700650 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700651 bad++;
652 }
653 continue;
654 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700655 if (args[i] == "mode") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700656 int32_t mode;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700657 if (sscanf(args[i+1].c_str(), "%" SCNi32, &mode) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700658 parsed.mode = mode;
659 parsed.has_mode = true;
660 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700661 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700662 bad++;
663 }
664 continue;
665 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700666 if (args[i] == "dmode") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700667 int32_t mode;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700668 if (sscanf(args[i+1].c_str(), "%" SCNi32, &mode) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700669 parsed.dmode = mode;
670 parsed.has_dmode = true;
671 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700672 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700673 bad++;
674 }
675 continue;
676 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700677 if (args[i] == "fmode") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700678 int32_t mode;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700679 if (sscanf(args[i+1].c_str(), "%" SCNi32, &mode) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700680 parsed.fmode = mode;
681 parsed.has_fmode = true;
682 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700683 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700684 bad++;
685 }
686 continue;
687 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700688 if (args[i] == "capabilities") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700689 int64_t capabilities;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700690 if (sscanf(args[i+1].c_str(), "%" SCNi64, &capabilities) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700691 parsed.capabilities = capabilities;
692 parsed.has_capabilities = true;
693 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700694 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700695 bad++;
696 }
697 continue;
698 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700699 if (args[i] == "selabel") {
700 if (!args[i+1].empty()) {
701 parsed.selabel = args[i+1].c_str();
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700702 parsed.has_selabel = true;
703 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700704 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700705 bad++;
706 }
707 continue;
708 }
709 if (max_warnings != 0) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700710 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700711 max_warnings--;
712 if (max_warnings == 0) {
713 printf("ParsedPermArgs: suppressing further warnings\n");
714 }
715 }
716 }
717 return parsed;
718}
719
720static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700721 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700722 const char* filename,
723 const struct stat *statptr,
724 struct perm_parsed_args parsed)
725{
726 int bad = 0;
727
Nick Kralevich68802412014-10-23 20:36:42 -0700728 if (parsed.has_selabel) {
729 if (lsetfilecon(filename, parsed.selabel) != 0) {
730 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
731 filename, parsed.selabel, strerror(errno));
732 bad++;
733 }
734 }
735
Nick Kraleviche4612512013-09-10 15:34:19 -0700736 /* ignore symlinks */
737 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700738 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700739 }
740
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700741 if (parsed.has_uid) {
742 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700743 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
744 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700745 bad++;
746 }
747 }
748
749 if (parsed.has_gid) {
750 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700751 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
752 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700753 bad++;
754 }
755 }
756
757 if (parsed.has_mode) {
758 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700759 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
760 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700761 bad++;
762 }
763 }
764
765 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
766 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700767 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
768 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700769 bad++;
770 }
771 }
772
773 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
774 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700775 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700776 filename, parsed.fmode, strerror(errno));
777 bad++;
778 }
779 }
780
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700781 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
782 if (parsed.capabilities == 0) {
783 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
784 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700785 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700786 filename, parsed.capabilities, strerror(errno));
787 bad++;
788 }
789 } else {
790 struct vfs_cap_data cap_data;
791 memset(&cap_data, 0, sizeof(cap_data));
792 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
793 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
794 cap_data.data[0].inheritable = 0;
795 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
796 cap_data.data[1].inheritable = 0;
797 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700798 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
799 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700800 bad++;
801 }
802 }
803 }
804
805 return bad;
806}
807
808// nftw doesn't allow us to pass along context, so we need to use
809// global variables. *sigh*
810static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700811static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700812
813static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
814 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700815 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700816}
817
818static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700819 if ((argc % 2) != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700820 return ErrorAbort(state, kArgsParsingFailure,
821 "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700822 }
823
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700824 std::vector<std::string> args;
825 if (!ReadArgs(state, argc, argv, &args)) {
826 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700827 }
828
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700829 struct stat sb;
830 if (lstat(args[0].c_str(), &sb) == -1) {
831 return ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s",
832 name, args[0].c_str(), strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700833 }
834
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700835 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
836 int bad = 0;
837 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700838
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700839 if (recursive) {
840 recursive_parsed_args = parsed;
841 recursive_state = state;
842 bad += nftw(args[0].c_str(), do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
843 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
844 recursive_state = NULL;
845 } else {
846 bad += ApplyParsedPerms(state, args[0].c_str(), &sb, parsed);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700847 }
848
849 if (bad > 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700850 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700851 }
852
Tianjie Xuaced5d92016-10-12 10:55:04 -0700853 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700854}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700855
Doug Zongker512536a2010-02-17 16:11:44 -0800856Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700857 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700858 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700859 }
Tianjie Xuaced5d92016-10-12 10:55:04 -0700860 std::string key;
861 if (!Evaluate(state, argv[0], &key)) {
862 return nullptr;
863 }
Elliott Hughescb220402016-09-23 15:30:55 -0700864 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700865
Tianjie Xuaced5d92016-10-12 10:55:04 -0700866 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700867}
868
Doug Zongker47cace92009-06-18 10:11:50 -0700869// file_getprop(file, key)
870//
871// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -0700872// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700873// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800874Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao358c2ec2016-11-28 11:48:43 -0800875 if (argc != 2) {
876 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
877 }
878
879 std::vector<std::string> args;
880 if (!ReadArgs(state, 2, argv, &args)) {
881 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
882 }
883 const std::string& filename = args[0];
884 const std::string& key = args[1];
885
886 struct stat st;
887 if (stat(filename.c_str(), &st) < 0) {
888 return ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name,
889 filename.c_str(), strerror(errno));
890 }
891
892 constexpr off_t MAX_FILE_GETPROP_SIZE = 65536;
893 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
894 return ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %lld)",
895 filename.c_str(), name, static_cast<long long>(MAX_FILE_GETPROP_SIZE));
896 }
897
898 std::string buffer(st.st_size, '\0');
899 unique_file f(ota_fopen(filename.c_str(), "rb"));
900 if (f == nullptr) {
901 return ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename.c_str(),
902 strerror(errno));
903 }
904
905 if (ota_fread(&buffer[0], 1, st.st_size, f.get()) != static_cast<size_t>(st.st_size)) {
906 ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s", name,
907 static_cast<size_t>(st.st_size), filename.c_str());
908 return nullptr;
909 }
910
911 ota_fclose(f);
912
913 std::vector<std::string> lines = android::base::Split(buffer, "\n");
914 for (size_t i = 0; i < lines.size(); i++) {
915 std::string line = android::base::Trim(lines[i]);
916
917 // comment or blank line: skip to next line
918 if (line.empty() || line[0] == '#') {
919 continue;
920 }
921 size_t equal_pos = line.find('=');
922 if (equal_pos == std::string::npos) {
923 continue;
Tao Bao51d516e2016-11-03 14:49:01 -0700924 }
925
Tao Bao358c2ec2016-11-28 11:48:43 -0800926 // trim whitespace between key and '='
927 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -0700928
Tao Bao358c2ec2016-11-28 11:48:43 -0800929 // not the key we're looking for
930 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700931
Tao Bao358c2ec2016-11-28 11:48:43 -0800932 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
933 }
Doug Zongker47cace92009-06-18 10:11:50 -0700934
Tao Bao358c2ec2016-11-28 11:48:43 -0800935 return StringValue("");
Doug Zongker47cace92009-06-18 10:11:50 -0700936}
937
Doug Zongker8edb00c2009-06-11 17:21:44 -0700938// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -0800939Value* ApplyPatchSpaceFn(const char* name, State* state,
940 int argc, Expr* argv[]) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700941 std::vector<std::string> args;
942 if (!ReadArgs(state, 1, argv, &args)) {
943 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800944 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700945 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800946
Tao Baob15fd222015-09-24 11:10:51 -0700947 size_t bytes;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700948 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
949 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n",
950 name, bytes_str.c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800951 }
952
Tianjie Xuaced5d92016-10-12 10:55:04 -0700953 return StringValue(CacheSizeCheck(bytes) ? "" : "t");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800954}
955
Doug Zongker52b40362014-02-10 15:30:30 -0800956// apply_patch(file, size, init_sha1, tgt_sha1, patch)
957
Doug Zongker512536a2010-02-17 16:11:44 -0800958Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800959 if (argc < 6 || (argc % 2) == 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700960 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an "
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700961 "even number, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700962 }
963
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700964 std::vector<std::string> args;
965 if (!ReadArgs(state, 4, argv, &args)) {
966 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700967 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700968 const std::string& source_filename = args[0];
969 const std::string& target_filename = args[1];
970 const std::string& target_sha1 = args[2];
971 const std::string& target_size_str = args[3];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700972
Tao Baob15fd222015-09-24 11:10:51 -0700973 size_t target_size;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700974 if (!android::base::ParseUint(target_size_str.c_str(), &target_size)) {
975 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count",
976 name, target_size_str.c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800977 }
978
979 int patchcount = (argc-4) / 2;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700980 std::vector<std::unique_ptr<Value>> arg_values;
981 if (!ReadValueArgs(state, argc-4, argv+4, &arg_values)) {
Yabin Cui64be2132016-02-03 18:16:02 -0800982 return nullptr;
983 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700984
Yabin Cui64be2132016-02-03 18:16:02 -0800985 for (int i = 0; i < patchcount; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700986 if (arg_values[i * 2]->type != VAL_STRING) {
987 return ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name,
988 i * 2);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800989 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700990 if (arg_values[i * 2 + 1]->type != VAL_BLOB) {
991 return ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name,
992 i * 2 + 1);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800993 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700994 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700995
Tianjie Xuaced5d92016-10-12 10:55:04 -0700996 std::vector<std::string> patch_sha_str;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700997 std::vector<std::unique_ptr<Value>> patches;
Yabin Cui64be2132016-02-03 18:16:02 -0800998 for (int i = 0; i < patchcount; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700999 patch_sha_str.push_back(arg_values[i * 2]->data);
1000 patches.push_back(std::move(arg_values[i * 2 + 1]));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001001 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001002
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001003 int result = applypatch(source_filename.c_str(), target_filename.c_str(),
1004 target_sha1.c_str(), target_size,
1005 patch_sha_str, patches, nullptr);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001006
Tianjie Xuaced5d92016-10-12 10:55:04 -07001007 return StringValue(result == 0 ? "t" : "");
Doug Zongkerc4351c72010-02-22 14:46:32 -08001008}
1009
1010// apply_patch_check(file, [sha1_1, ...])
1011Value* ApplyPatchCheckFn(const char* name, State* state,
1012 int argc, Expr* argv[]) {
1013 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001014 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %d",
Doug Zongkerc4351c72010-02-22 14:46:32 -08001015 name, argc);
1016 }
1017
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001018 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);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001021 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001022 const std::string& filename = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -08001023
Tianjie Xuaced5d92016-10-12 10:55:04 -07001024 std::vector<std::string> sha1s;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001025 if (!ReadArgs(state, argc - 1, argv + 1, &sha1s)) {
1026 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001027 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001028 int result = applypatch_check(filename.c_str(), sha1s);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001029
1030 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[]) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001036 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);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001039 }
1040
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001041 std::string buffer = android::base::Join(args, "") + "\n";
Michael Runged4a63422014-10-22 19:48:41 -07001042 uiPrint(state, buffer);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001043 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[]) {
1047 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001048 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001049 }
1050 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
Tianjie Xuaced5d92016-10-12 10:55:04 -07001051 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[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001055 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001056 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001057 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001058
1059 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);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001062 }
1063
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001064 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
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001070 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001071
1072 pid_t child = fork();
1073 if (child == 0) {
1074 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001075 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001076 _exit(1);
1077 }
1078 int status;
1079 waitpid(child, &status, 0);
1080 if (WIFEXITED(status)) {
1081 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001082 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001083 WEXITSTATUS(status));
1084 }
1085 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001086 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001087 WTERMSIG(status));
1088 }
1089
Tianjie Xuaced5d92016-10-12 10:55:04 -07001090 return StringValue(android::base::StringPrintf("%d", status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001091}
1092
Doug Zongker512536a2010-02-17 16:11:44 -08001093// sha1_check(data)
1094// to return the sha1 of the data (given in the format returned by
1095// read_file).
1096//
1097// sha1_check(data, sha1_hex, [sha1_hex, ...])
1098// returns the sha1 of the file if it matches any of the hex
1099// strings passed, or "" if it does not equal any of them.
1100//
1101Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1102 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001103 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongker512536a2010-02-17 16:11:44 -08001104 }
1105
Tianjie Xuaced5d92016-10-12 10:55:04 -07001106 std::vector<std::unique_ptr<Value>> args;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001107 if (!ReadValueArgs(state, argc, argv, &args)) {
1108 return nullptr;
Doug Zongker512536a2010-02-17 16:11:44 -08001109 }
1110
Tianjie Xuaced5d92016-10-12 10:55:04 -07001111 if (args[0]->type == VAL_INVALID) {
1112 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -08001113 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001114 uint8_t digest[SHA_DIGEST_LENGTH];
Tianjie Xuaced5d92016-10-12 10:55:04 -07001115 SHA1(reinterpret_cast<const uint8_t*>(args[0]->data.c_str()), args[0]->data.size(), digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001116
1117 if (argc == 1) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001118 return StringValue(print_sha1(digest));
Doug Zongker512536a2010-02-17 16:11:44 -08001119 }
1120
Tao Bao361342c2016-02-08 11:15:50 -08001121 for (int i = 1; i < argc; ++i) {
1122 uint8_t arg_digest[SHA_DIGEST_LENGTH];
Doug Zongker512536a2010-02-17 16:11:44 -08001123 if (args[i]->type != VAL_STRING) {
Tao Bao361342c2016-02-08 11:15:50 -08001124 printf("%s(): arg %d is not a string; skipping", name, i);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001125 } else if (ParseSha1(args[i]->data.c_str(), arg_digest) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001126 // Warn about bad args and skip them.
Tianjie Xuaced5d92016-10-12 10:55:04 -07001127 printf("%s(): error parsing \"%s\" as sha-1; skipping", name, args[i]->data.c_str());
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001128 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
Tao Bao361342c2016-02-08 11:15:50 -08001129 // Found a match.
1130 return args[i].release();
Doug Zongker512536a2010-02-17 16:11:44 -08001131 }
Doug Zongker512536a2010-02-17 16:11:44 -08001132 }
Tao Bao361342c2016-02-08 11:15:50 -08001133
1134 // Didn't match any of the hex strings; return false.
Tianjie Xuaced5d92016-10-12 10:55:04 -07001135 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -08001136}
1137
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001138// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001139// is actually a FileContents*).
1140Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1141 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001142 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker512536a2010-02-17 16:11:44 -08001143 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001144
1145 std::vector<std::string> args;
1146 if (!ReadArgs(state, 1, argv, &args)) {
1147 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1148 }
1149 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -08001150
Tianjie Xuaced5d92016-10-12 10:55:04 -07001151 Value* v = new Value(VAL_INVALID, "");
Doug Zongker512536a2010-02-17 16:11:44 -08001152
1153 FileContents fc;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001154 if (LoadFileContents(filename.c_str(), &fc) == 0) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001155 v->type = VAL_BLOB;
1156 v->data = std::string(fc.data.begin(), fc.data.end());
Doug Zongker512536a2010-02-17 16:11:44 -08001157 }
Doug Zongker512536a2010-02-17 16:11:44 -08001158 return v;
1159}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001160
Tao Baod0f30882016-11-03 23:52:01 -07001161// write_value(value, filename)
1162// Writes 'value' to 'filename'.
1163// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
1164Value* WriteValueFn(const char* name, State* state, int argc, Expr* argv[]) {
1165 if (argc != 2) {
1166 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
1167 }
1168
1169 std::vector<std::string> args;
1170 if (!ReadArgs(state, 2, argv, &args)) {
1171 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1172 }
1173
1174 const std::string& filename = args[1];
1175 if (filename.empty()) {
1176 return ErrorAbort(state, kArgsParsingFailure, "%s(): Filename cannot be empty", name);
1177 }
1178
1179 const std::string& value = args[0];
1180 if (!android::base::WriteStringToFile(value, filename)) {
1181 printf("%s: Failed to write to \"%s\": %s\n", name, filename.c_str(), strerror(errno));
1182 return StringValue("");
1183 } else {
1184 return StringValue("t");
1185 }
1186}
1187
Doug Zongkerc87bab12013-11-25 13:53:25 -08001188// Immediately reboot the device. Recovery is not finished normally,
1189// so if you reboot into recovery it will re-start applying the
1190// current package (because nothing has cleared the copy of the
1191// arguments stored in the BCB).
1192//
1193// The argument is the partition name passed to the android reboot
1194// property. It can be "recovery" to boot from the recovery
1195// partition, or "" (empty string) to boot from the regular boot
1196// partition.
1197Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001198 if (argc != 2) {
1199 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
1200 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001201
Tao Baobedf5fc2016-11-18 12:01:26 -08001202 std::vector<std::string> args;
1203 if (!ReadArgs(state, 2, argv, &args)) {
1204 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)", name);
1205 }
1206 const std::string& filename = args[0];
1207 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001208
Tao Baobedf5fc2016-11-18 12:01:26 -08001209 // Zero out the 'command' field of the bootloader message. Leave the rest intact.
1210 bootloader_message boot;
1211 std::string err;
1212 if (!read_bootloader_message_from(&boot, filename, &err)) {
1213 printf("%s(): Failed to read from \"%s\": %s", name, filename.c_str(), err.c_str());
1214 return StringValue("");
1215 }
1216 memset(boot.command, 0, sizeof(boot.command));
1217 if (!write_bootloader_message_to(boot, filename, &err)) {
1218 printf("%s(): Failed to write to \"%s\": %s", name, filename.c_str(), err.c_str());
1219 return StringValue("");
1220 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001221
Tao Baobedf5fc2016-11-18 12:01:26 -08001222 const std::string reboot_cmd = "reboot," + property;
1223 android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001224
Tao Baobedf5fc2016-11-18 12:01:26 -08001225 sleep(5);
1226 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001227}
1228
1229// Store a string value somewhere that future invocations of recovery
1230// can access it. This value is called the "stage" and can be used to
1231// drive packages that need to do reboots in the middle of
1232// installation and keep track of where they are in the multi-stage
1233// install.
1234//
1235// The first argument is the block device for the misc partition
1236// ("/misc" in the fstab), which is where this value is stored. The
1237// second argument is the string to store; it should not exceed 31
1238// bytes.
1239Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001240 if (argc != 2) {
1241 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
1242 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001243
Tao Baobedf5fc2016-11-18 12:01:26 -08001244 std::vector<std::string> args;
1245 if (!ReadArgs(state, 2, argv, &args)) {
1246 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1247 }
1248 const std::string& filename = args[0];
1249 const std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001250
Tao Baobedf5fc2016-11-18 12:01:26 -08001251 // Store this value in the misc partition, immediately after the
1252 // bootloader message that the main recovery uses to save its
1253 // arguments in case of the device restarting midway through
1254 // package installation.
1255 bootloader_message boot;
1256 std::string err;
1257 if (!read_bootloader_message_from(&boot, filename, &err)) {
1258 printf("%s(): Failed to read from \"%s\": %s", name, filename.c_str(), err.c_str());
1259 return StringValue("");
1260 }
1261 strlcpy(boot.stage, stagestr.c_str(), sizeof(boot.stage));
1262 if (!write_bootloader_message_to(boot, filename, &err)) {
1263 printf("%s(): Failed to write to \"%s\": %s", name, filename.c_str(), err.c_str());
1264 return StringValue("");
1265 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001266
Tao Baobedf5fc2016-11-18 12:01:26 -08001267 return StringValue(filename);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001268}
1269
1270// Return the value most recently saved with SetStageFn. The argument
1271// is the block device for the misc partition.
1272Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baobedf5fc2016-11-18 12:01:26 -08001273 if (argc != 1) {
1274 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
1275 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001276
Tao Baobedf5fc2016-11-18 12:01:26 -08001277 std::vector<std::string> args;
1278 if (!ReadArgs(state, 1, argv, &args)) {
1279 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1280 }
1281 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001282
Tao Baobedf5fc2016-11-18 12:01:26 -08001283 bootloader_message boot;
1284 std::string err;
1285 if (!read_bootloader_message_from(&boot, filename, &err)) {
1286 printf("%s(): Failed to read from \"%s\": %s", name, filename.c_str(), err.c_str());
1287 return StringValue("");
1288 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001289
Tao Baobedf5fc2016-11-18 12:01:26 -08001290 return StringValue(boot.stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001291}
1292
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001293Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao358c2ec2016-11-28 11:48:43 -08001294 if (argc != 2) {
1295 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
1296 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001297
Tao Bao358c2ec2016-11-28 11:48:43 -08001298 std::vector<std::string> args;
1299 if (!ReadArgs(state, 2, argv, &args)) {
1300 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1301 }
1302 const std::string& filename = args[0];
1303 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001304
Tao Bao358c2ec2016-11-28 11:48:43 -08001305 size_t len;
1306 if (!android::base::ParseUint(len_str.c_str(), &len)) {
1307 return nullptr;
1308 }
1309 unique_fd fd(ota_open(filename.c_str(), O_WRONLY, 0644));
1310 // The wipe_block_device function in ext4_utils returns 0 on success and 1
1311 // for failure.
1312 int status = wipe_block_device(fd, len);
1313 return StringValue((status == 0) ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001314}
1315
Doug Zongkerc704e062014-05-23 08:40:35 -07001316Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1317 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001318 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerc704e062014-05-23 08:40:35 -07001319 }
1320 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1321 fprintf(ui->cmd_pipe, "enable_reboot\n");
Tianjie Xuaced5d92016-10-12 10:55:04 -07001322 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -07001323}
1324
Michael Rungeacf47db2014-11-21 00:12:28 -08001325Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1326 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001327 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %d", name, argc);
Michael Rungeacf47db2014-11-21 00:12:28 -08001328 }
1329
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001330 std::vector<std::string> args;
1331 if (!ReadArgs(state, argc, argv, &args)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001332 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
Michael Rungeacf47db2014-11-21 00:12:28 -08001333 }
1334
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001335 char* args2[argc+1];
Michael Rungeacf47db2014-11-21 00:12:28 -08001336 // Tune2fs expects the program name as its args[0]
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001337 args2[0] = const_cast<char*>(name);
1338 if (args2[0] == nullptr) {
1339 return nullptr;
Michael Rungeacf47db2014-11-21 00:12:28 -08001340 }
Tao Baoba9a42a2015-06-23 23:23:33 -07001341 for (int i = 0; i < argc; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001342 args2[i + 1] = &args[i][0];
Michael Rungeacf47db2014-11-21 00:12:28 -08001343 }
Michael Rungeacf47db2014-11-21 00:12:28 -08001344
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001345 // tune2fs changes the file system parameters on an ext2 file system; it
1346 // returns 0 on success.
1347 int result = tune2fs_main(argc + 1, args2);
1348
Michael Rungeacf47db2014-11-21 00:12:28 -08001349 if (result != 0) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001350 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
Michael Rungeacf47db2014-11-21 00:12:28 -08001351 }
Tianjie Xuaced5d92016-10-12 10:55:04 -07001352 return StringValue("t");
Michael Rungeacf47db2014-11-21 00:12:28 -08001353}
1354
Doug Zongker9931f7f2009-06-10 14:11:53 -07001355void RegisterInstallFunctions() {
1356 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001357 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001358 RegisterFunction("unmount", UnmountFn);
1359 RegisterFunction("format", FormatFn);
1360 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001361 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001362 RegisterFunction("delete", DeleteFn);
1363 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001364 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1365 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001366 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001367
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001368 // Usage:
1369 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1370 // Example:
1371 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1372 RegisterFunction("set_metadata", SetMetadataFn);
1373
1374 // Usage:
1375 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1376 // Example:
1377 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1378 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1379
Doug Zongker8edb00c2009-06-11 17:21:44 -07001380 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001381 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001382
1383 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001384 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1385 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001386
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001387 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001388
Doug Zongker512536a2010-02-17 16:11:44 -08001389 RegisterFunction("read_file", ReadFileFn);
1390 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001391 RegisterFunction("rename", RenameFn);
Tao Baod0f30882016-11-03 23:52:01 -07001392 RegisterFunction("write_value", WriteValueFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001393
Doug Zongkerd0181b82011-10-19 10:51:12 -07001394 RegisterFunction("wipe_cache", WipeCacheFn);
1395
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001396 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001397
1398 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001399
1400 RegisterFunction("reboot_now", RebootNowFn);
1401 RegisterFunction("get_stage", GetStageFn);
1402 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001403
1404 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001405 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001406}