blob: 8c33c2bf3b5b31f4ec628303d992d2c925077e02 [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
Doug Zongkerfbf3c102009-06-24 09:36:20 -070017#include <ctype.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070018#include <errno.h>
19#include <stdarg.h>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070020#include <stdio.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070021#include <stdlib.h>
22#include <string.h>
23#include <sys/mount.h>
24#include <sys/stat.h>
25#include <sys/types.h>
Doug Zongkera3f89ea2009-09-10 14:10:48 -070026#include <sys/wait.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070027#include <unistd.h>
Hristo Bojinovdb314d62010-08-02 10:29:49 -070028#include <fcntl.h>
29#include <time.h>
Nick Kralevich5dbdef02013-09-07 14:41:06 -070030#include <ftw.h>
31#include <sys/capability.h>
32#include <sys/xattr.h>
33#include <linux/xattr.h>
34#include <inttypes.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070035
Yabin Cui64be2132016-02-03 18:16:02 -080036#include <memory>
37#include <vector>
38
Elliott Hughes4b166f02015-12-04 15:30:20 -080039#include <android-base/parseint.h>
Elliott Hughescb220402016-09-23 15:30:55 -070040#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080041#include <android-base/strings.h>
42#include <android-base/stringprintf.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070043#include <selinux/label.h>
44#include <selinux/selinux.h>
Tao Bao1107d962015-09-09 17:16:55 -070045
Doug Zongkerc87bab12013-11-25 13:53:25 -080046#include "bootloader.h"
47#include "applypatch/applypatch.h"
48#include "cutils/android_reboot.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070049#include "cutils/misc.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070050#include "edify/expr.h"
Tianjie Xu16255832016-04-30 11:49:59 -070051#include "error_code.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070052#include "minzip/DirUtil.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070053#include "mounts.h"
Tianjie Xu16255832016-04-30 11:49:59 -070054#include "openssl/sha.h"
Jed Estep39c1b5e2015-12-15 16:04:53 -080055#include "ota_io.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070056#include "updater.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080057#include "install.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080058#include "tune2fs.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070059
Doug Zongker3d177d02010-07-01 09:18:44 -070060#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080061#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070062
Tao Bao1107d962015-09-09 17:16:55 -070063// Send over the buffer to recovery though the command pipe.
64static void uiPrint(State* state, const std::string& buffer) {
65 UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070066
Tao Bao1107d962015-09-09 17:16:55 -070067 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
68 // So skip sending empty strings to UI.
69 std::vector<std::string> lines = android::base::Split(buffer, "\n");
70 for (auto& line: lines) {
71 if (!line.empty()) {
72 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
73 fprintf(ui->cmd_pipe, "ui_print\n");
74 }
75 }
76
77 // On the updater side, we need to dump the contents to stderr (which has
78 // been redirected to the log file). Because the recovery will only print
79 // the contents to screen when processing pipe command ui_print.
80 fprintf(stderr, "%s", buffer.c_str());
Michael Runged4a63422014-10-22 19:48:41 -070081}
82
Elliott Hughes83ce7552016-06-30 09:28:42 -070083void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) {
Tao Bao1107d962015-09-09 17:16:55 -070084 std::string error_msg;
85
Michael Runged4a63422014-10-22 19:48:41 -070086 va_list ap;
87 va_start(ap, format);
Tao Bao1107d962015-09-09 17:16:55 -070088 android::base::StringAppendV(&error_msg, format, ap);
Michael Runged4a63422014-10-22 19:48:41 -070089 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070090
Michael Runged4a63422014-10-22 19:48:41 -070091 uiPrint(state, error_msg);
92}
93
Doug Zongker52b40362014-02-10 15:30:30 -080094// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070095char* PrintSha1(const uint8_t* digest) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +080096 char* buffer = reinterpret_cast<char*>(malloc(SHA_DIGEST_LENGTH*2 + 1));
Doug Zongker52b40362014-02-10 15:30:30 -080097 const char* alphabet = "0123456789abcdef";
Tao Baoba9a42a2015-06-23 23:23:33 -070098 size_t i;
Sen Jiangc48cb5e2016-02-04 16:23:21 +080099 for (i = 0; i < SHA_DIGEST_LENGTH; ++i) {
Doug Zongker52b40362014-02-10 15:30:30 -0800100 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
101 buffer[i*2+1] = alphabet[digest[i] & 0xf];
102 }
103 buffer[i*2] = '\0';
104 return buffer;
105}
106
Doug Zongker3d177d02010-07-01 09:18:44 -0700107// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700108//
Doug Zongker3d177d02010-07-01 09:18:44 -0700109// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800110Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700111 char* result = NULL;
Michael Runge168f7772014-10-22 17:05:08 -0700112 if (argc != 4 && argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700113 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700114 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700115 char* fs_type;
116 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700117 char* location;
118 char* mount_point;
Michael Runge168f7772014-10-22 17:05:08 -0700119 char* mount_options;
120 bool has_mount_options;
121 if (argc == 5) {
122 has_mount_options = true;
123 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
124 &location, &mount_point, &mount_options) < 0) {
125 return NULL;
126 }
127 } else {
128 has_mount_options = false;
129 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700130 &location, &mount_point) < 0) {
Michael Runge168f7772014-10-22 17:05:08 -0700131 return NULL;
132 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700133 }
134
Doug Zongker3d177d02010-07-01 09:18:44 -0700135 if (strlen(fs_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700136 ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700137 goto done;
138 }
139 if (strlen(partition_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700140 ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
Doug Zongker3d177d02010-07-01 09:18:44 -0700141 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700142 goto done;
143 }
144 if (strlen(location) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700145 ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700146 goto done;
147 }
148 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700149 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
150 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700151 goto done;
152 }
153
Tao Baoba9a42a2015-06-23 23:23:33 -0700154 {
155 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500156
Tao Baoba9a42a2015-06-23 23:23:33 -0700157 if (sehandle) {
158 selabel_lookup(sehandle, &secontext, mount_point, 0755);
159 setfscreatecon(secontext);
160 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500161
Tao Baoba9a42a2015-06-23 23:23:33 -0700162 mkdir(mount_point, 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700163
Tao Baoba9a42a2015-06-23 23:23:33 -0700164 if (secontext) {
165 freecon(secontext);
166 setfscreatecon(NULL);
167 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500168 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500169
Elliott Hughes63a31922016-06-09 17:41:22 -0700170 if (mount(location, mount_point, fs_type,
171 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
172 has_mount_options ? mount_options : "") < 0) {
173 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
174 name, location, mount_point, strerror(errno));
175 result = strdup("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700176 } else {
Elliott Hughes63a31922016-06-09 17:41:22 -0700177 result = mount_point;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700178 }
179
180done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700181 free(fs_type);
182 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700183 free(location);
184 if (result != mount_point) free(mount_point);
Michael Runge168f7772014-10-22 17:05:08 -0700185 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800186 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700187}
188
Doug Zongker8edb00c2009-06-11 17:21:44 -0700189
190// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800191Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700192 char* result = NULL;
193 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700194 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700195 }
196 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700197 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700198 return NULL;
199 }
200 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700201 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700202 goto done;
203 }
204
205 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700206 {
Elliott Hughes63a31922016-06-09 17:41:22 -0700207 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
Tao Baoba9a42a2015-06-23 23:23:33 -0700208 if (vol == NULL) {
209 result = strdup("");
210 } else {
211 result = mount_point;
212 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700213 }
214
215done:
216 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800217 return StringValue(result);
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 char* result = NULL;
223 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700224 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700225 }
226 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700227 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700228 return NULL;
229 }
230 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700231 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700232 goto done;
233 }
234
235 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700236 {
Elliott Hughes63a31922016-06-09 17:41:22 -0700237 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
Tao Baoba9a42a2015-06-23 23:23:33 -0700238 if (vol == NULL) {
239 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
240 result = strdup("");
241 } else {
242 int ret = unmount_mounted_volume(vol);
243 if (ret != 0) {
244 uiPrintf(state, "unmount of %s failed (%d): %s\n",
245 mount_point, ret, strerror(errno));
246 }
247 result = mount_point;
Michael Runge5ddf4292014-10-24 14:14:41 -0700248 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700249 }
250
251done:
252 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800253 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700254}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700255
JP Abgrall37aedb32014-06-16 19:07:39 -0700256static int exec_cmd(const char* path, char* const argv[]) {
257 int status;
258 pid_t child;
259 if ((child = vfork()) == 0) {
260 execv(path, argv);
261 _exit(-1);
262 }
263 waitpid(child, &status, 0);
264 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
265 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
266 }
267 return WEXITSTATUS(status);
268}
269
Doug Zongker8edb00c2009-06-11 17:21:44 -0700270
Stephen Smalley779701d2012-02-09 14:13:23 -0500271// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700272//
Stephen Smalley779701d2012-02-09 14:13:23 -0500273// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700274// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
275// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800276// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700277// 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 -0800278Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700279 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400280 if (argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700281 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700282 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700283 char* fs_type;
284 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700285 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800286 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400287 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500288
Stephen Smalley779701d2012-02-09 14:13:23 -0500289 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
290 return NULL;
291 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700292
Doug Zongker3d177d02010-07-01 09:18:44 -0700293 if (strlen(fs_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700294 ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700295 goto done;
296 }
297 if (strlen(partition_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700298 ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
Doug Zongker3d177d02010-07-01 09:18:44 -0700299 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700300 goto done;
301 }
302 if (strlen(location) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700303 ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700304 goto done;
305 }
306
Stephen Smalley516e4e22012-04-03 13:35:11 -0400307 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700308 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
309 name);
Stephen Smalley779701d2012-02-09 14:13:23 -0500310 goto done;
311 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500312
Elliott Hughes63a31922016-06-09 17:41:22 -0700313 if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500314 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700315 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700316 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700317 name, status, location);
318 result = strdup("");
319 goto done;
320 }
321 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700322 } else if (strcmp(fs_type, "f2fs") == 0) {
323 char *num_sectors;
324 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
325 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
326 result = strdup("");
327 goto done;
328 }
329 const char *f2fs_path = "/sbin/mkfs.f2fs";
330 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
331 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
332 free(num_sectors);
333 if (status != 0) {
334 printf("%s: mkfs.f2fs failed (%d) on %s",
335 name, status, location);
336 result = strdup("");
337 goto done;
338 }
339 result = location;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700340 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700341 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700342 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700343 }
344
345done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700346 free(fs_type);
347 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700348 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800349 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700350}
351
Michael Rungece7ca712013-11-06 17:42:20 -0800352Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
353 char* result = NULL;
354 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700355 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Michael Rungece7ca712013-11-06 17:42:20 -0800356 }
357
358 char* src_name;
359 char* dst_name;
360
361 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
362 return NULL;
363 }
364 if (strlen(src_name) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700365 ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800366 goto done;
367 }
368 if (strlen(dst_name) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700369 ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800370 goto done;
371 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700372 if (make_parents(dst_name) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700373 ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700374 dst_name, strerror(errno));
Michael Runge2f0ef732014-10-22 14:28:23 -0700375 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
376 // File was already moved
377 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700378 } else if (rename(src_name, dst_name) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700379 ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800380 src_name, dst_name, strerror(errno));
381 } else {
382 result = dst_name;
383 }
384
385done:
386 free(src_name);
387 if (result != dst_name) free(dst_name);
388 return StringValue(result);
389}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700390
Doug Zongker512536a2010-02-17 16:11:44 -0800391Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700392 char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
393 for (int i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700394 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700395 if (paths[i] == NULL) {
Yabin Cui64be2132016-02-03 18:16:02 -0800396 for (int j = 0; j < i; ++j) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700397 free(paths[j]);
398 }
399 free(paths);
400 return NULL;
401 }
402 }
403
404 bool recursive = (strcmp(name, "delete_recursive") == 0);
405
406 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700407 for (int i = 0; i < argc; ++i) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700408 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
409 ++success;
410 free(paths[i]);
411 }
412 free(paths);
413
414 char buffer[10];
415 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800416 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700417}
418
Doug Zongker8edb00c2009-06-11 17:21:44 -0700419
Doug Zongker512536a2010-02-17 16:11:44 -0800420Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700421 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700422 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700423 }
424 char* frac_str;
425 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700426 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700427 return NULL;
428 }
429
430 double frac = strtod(frac_str, NULL);
Tao Baob15fd222015-09-24 11:10:51 -0700431 int sec;
432 android::base::ParseInt(sec_str, &sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700433
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700434 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700435 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
436
Doug Zongker9931f7f2009-06-10 14:11:53 -0700437 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800438 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700439}
440
Doug Zongker512536a2010-02-17 16:11:44 -0800441Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700442 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700443 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700444 }
445 char* frac_str;
446 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
447 return NULL;
448 }
449
450 double frac = strtod(frac_str, NULL);
451
452 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
453 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
454
Doug Zongker512536a2010-02-17 16:11:44 -0800455 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700456}
457
Doug Zongker8edb00c2009-06-11 17:21:44 -0700458// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800459Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700460 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700461 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700462 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700463 }
464 char* zip_path;
465 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700466 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700467
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700468 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700469
470 // To create a consistent system image, never use the clock for timestamps.
471 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
472
473 bool success = mzExtractRecursive(za, zip_path, dest_path,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000474 &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500475 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700476 free(zip_path);
477 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800478 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700479}
480
Doug Zongker8edb00c2009-06-11 17:21:44 -0700481
482// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800483// or
484// package_extract_file(package_path)
485// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800486// function (the char* returned is actually a FileContents*).
487Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700488 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700489 if (argc < 1 || argc > 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700490 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800491 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700492 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700493 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700494
Doug Zongker5f875bf2014-08-22 14:53:43 -0700495 if (argc == 2) {
496 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800497
498 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700499
Doug Zongker6aece332010-02-01 14:40:12 -0800500 char* zip_path;
501 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700502 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800503
Doug Zongker6aece332010-02-01 14:40:12 -0800504 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
505 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700506 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800507 goto done2;
508 }
509
Tao Baoba9a42a2015-06-23 23:23:33 -0700510 {
Alistair Strachan733285f2016-05-05 16:04:32 -0700511 int fd = TEMP_FAILURE_RETRY(ota_open(dest_path, O_WRONLY | O_CREAT | O_TRUNC,
Tao Baod3cac342015-12-14 15:53:25 -0800512 S_IRUSR | S_IWUSR));
513 if (fd == -1) {
514 printf("%s: can't open %s for write: %s\n", name, dest_path, strerror(errno));
Tao Baoba9a42a2015-06-23 23:23:33 -0700515 goto done2;
516 }
Tao Baod3cac342015-12-14 15:53:25 -0800517 success = mzExtractZipEntryToFile(za, entry, fd);
Jed Estepa7b9a462015-12-15 16:04:53 -0800518 if (ota_fsync(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800519 printf("fsync of \"%s\" failed: %s\n", dest_path, strerror(errno));
520 success = false;
521 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800522 if (ota_close(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800523 printf("close of \"%s\" failed: %s\n", dest_path, strerror(errno));
524 success = false;
525 }
Doug Zongker6aece332010-02-01 14:40:12 -0800526 }
Doug Zongker6aece332010-02-01 14:40:12 -0800527
528 done2:
529 free(zip_path);
530 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800531 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800532 } else {
533 // The one-argument version returns the contents of the file
534 // as the result.
535
536 char* zip_path;
Yabin Cui64be2132016-02-03 18:16:02 -0800537 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
538
Tao Baoba9a42a2015-06-23 23:23:33 -0700539 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -0800540 v->type = VAL_BLOB;
541 v->size = -1;
542 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800543
Doug Zongker6aece332010-02-01 14:40:12 -0800544 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
545 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
546 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700547 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800548 goto done1;
549 }
550
Doug Zongker512536a2010-02-17 16:11:44 -0800551 v->size = mzGetZipEntryUncompLen(entry);
Tao Baoba9a42a2015-06-23 23:23:33 -0700552 v->data = reinterpret_cast<char*>(malloc(v->size));
Doug Zongker512536a2010-02-17 16:11:44 -0800553 if (v->data == NULL) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700554 printf("%s: failed to allocate %zd bytes for %s\n",
555 name, v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800556 goto done1;
557 }
558
Doug Zongker512536a2010-02-17 16:11:44 -0800559 success = mzExtractZipEntryToBuffer(za, entry,
560 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800561
562 done1:
563 free(zip_path);
564 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800565 free(v->data);
566 v->data = NULL;
567 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800568 }
Doug Zongker512536a2010-02-17 16:11:44 -0800569 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700570 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700571}
572
Doug Zongkera23075f2012-08-06 16:19:09 -0700573// Create all parent directories of name, if necessary.
574static int make_parents(char* name) {
575 char* p;
576 for (p = name + (strlen(name)-1); p > name; --p) {
577 if (*p != '/') continue;
578 *p = '\0';
579 if (make_parents(name) < 0) return -1;
580 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700581 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700582 *p = '/';
583 if (result == 0 || errno == EEXIST) {
584 // successfully created or already existed; we're done
585 return 0;
586 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700587 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700588 return -1;
589 }
590 }
591 return 0;
592}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700593
Doug Zongker9931f7f2009-06-10 14:11:53 -0700594// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700595// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800596Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700597 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700598 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700599 }
600 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700601 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700602 if (target == NULL) return NULL;
603
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700604 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700605 if (srcs == NULL) {
606 free(target);
607 return NULL;
608 }
609
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700610 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700611 int i;
612 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700613 if (unlink(srcs[i]) < 0) {
614 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700615 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700616 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700617 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700618 }
619 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700620 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700621 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700622 name, srcs[i], target);
623 ++bad;
624 }
Doug Zongker60babf82009-09-18 15:11:24 -0700625 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700626 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700627 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700628 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700629 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700630 free(srcs[i]);
631 }
632 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700633 if (bad) {
Tianjie Xu16255832016-04-30 11:49:59 -0700634 return ErrorAbort(state, kSymlinkFailure, "%s: some symlinks failed", name);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700635 }
Doug Zongker512536a2010-02-17 16:11:44 -0800636 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700637}
638
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700639struct perm_parsed_args {
640 bool has_uid;
641 uid_t uid;
642 bool has_gid;
643 gid_t gid;
644 bool has_mode;
645 mode_t mode;
646 bool has_fmode;
647 mode_t fmode;
648 bool has_dmode;
649 mode_t dmode;
650 bool has_selabel;
651 char* selabel;
652 bool has_capabilities;
653 uint64_t capabilities;
654};
655
Michael Runged4a63422014-10-22 19:48:41 -0700656static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700657 int i;
658 struct perm_parsed_args parsed;
659 int bad = 0;
660 static int max_warnings = 20;
661
662 memset(&parsed, 0, sizeof(parsed));
663
664 for (i = 1; i < argc; i += 2) {
665 if (strcmp("uid", args[i]) == 0) {
666 int64_t uid;
667 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
668 parsed.uid = uid;
669 parsed.has_uid = true;
670 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700671 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700672 bad++;
673 }
674 continue;
675 }
676 if (strcmp("gid", args[i]) == 0) {
677 int64_t gid;
678 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
679 parsed.gid = gid;
680 parsed.has_gid = true;
681 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700682 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700683 bad++;
684 }
685 continue;
686 }
687 if (strcmp("mode", args[i]) == 0) {
688 int32_t mode;
689 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
690 parsed.mode = mode;
691 parsed.has_mode = true;
692 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700693 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700694 bad++;
695 }
696 continue;
697 }
698 if (strcmp("dmode", args[i]) == 0) {
699 int32_t mode;
700 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
701 parsed.dmode = mode;
702 parsed.has_dmode = true;
703 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700704 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700705 bad++;
706 }
707 continue;
708 }
709 if (strcmp("fmode", args[i]) == 0) {
710 int32_t mode;
711 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
712 parsed.fmode = mode;
713 parsed.has_fmode = true;
714 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700715 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700716 bad++;
717 }
718 continue;
719 }
720 if (strcmp("capabilities", args[i]) == 0) {
721 int64_t capabilities;
722 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
723 parsed.capabilities = capabilities;
724 parsed.has_capabilities = true;
725 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700726 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700727 bad++;
728 }
729 continue;
730 }
731 if (strcmp("selabel", args[i]) == 0) {
732 if (args[i+1][0] != '\0') {
733 parsed.selabel = args[i+1];
734 parsed.has_selabel = true;
735 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700736 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700737 bad++;
738 }
739 continue;
740 }
741 if (max_warnings != 0) {
742 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
743 max_warnings--;
744 if (max_warnings == 0) {
745 printf("ParsedPermArgs: suppressing further warnings\n");
746 }
747 }
748 }
749 return parsed;
750}
751
752static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700753 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700754 const char* filename,
755 const struct stat *statptr,
756 struct perm_parsed_args parsed)
757{
758 int bad = 0;
759
Nick Kralevich68802412014-10-23 20:36:42 -0700760 if (parsed.has_selabel) {
761 if (lsetfilecon(filename, parsed.selabel) != 0) {
762 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
763 filename, parsed.selabel, strerror(errno));
764 bad++;
765 }
766 }
767
Nick Kraleviche4612512013-09-10 15:34:19 -0700768 /* ignore symlinks */
769 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700770 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700771 }
772
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700773 if (parsed.has_uid) {
774 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700775 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
776 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700777 bad++;
778 }
779 }
780
781 if (parsed.has_gid) {
782 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700783 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
784 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700785 bad++;
786 }
787 }
788
789 if (parsed.has_mode) {
790 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700791 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
792 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700793 bad++;
794 }
795 }
796
797 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
798 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700799 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
800 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700801 bad++;
802 }
803 }
804
805 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
806 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700807 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700808 filename, parsed.fmode, strerror(errno));
809 bad++;
810 }
811 }
812
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700813 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
814 if (parsed.capabilities == 0) {
815 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
816 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700817 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700818 filename, parsed.capabilities, strerror(errno));
819 bad++;
820 }
821 } else {
822 struct vfs_cap_data cap_data;
823 memset(&cap_data, 0, sizeof(cap_data));
824 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
825 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
826 cap_data.data[0].inheritable = 0;
827 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
828 cap_data.data[1].inheritable = 0;
829 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700830 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
831 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700832 bad++;
833 }
834 }
835 }
836
837 return bad;
838}
839
840// nftw doesn't allow us to pass along context, so we need to use
841// global variables. *sigh*
842static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700843static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700844
845static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
846 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700847 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700848}
849
850static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700851 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700852 struct stat sb;
853 Value* result = NULL;
854
855 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
856
857 if ((argc % 2) != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700858 return ErrorAbort(state, kArgsParsingFailure,
859 "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700860 }
861
862 char** args = ReadVarArgs(state, argc, argv);
863 if (args == NULL) return NULL;
864
865 if (lstat(args[0], &sb) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700866 result = ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s",
867 name, args[0], strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700868 goto done;
869 }
870
Tao Baoba9a42a2015-06-23 23:23:33 -0700871 {
872 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700873
Tao Baoba9a42a2015-06-23 23:23:33 -0700874 if (recursive) {
875 recursive_parsed_args = parsed;
876 recursive_state = state;
877 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
878 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
879 recursive_state = NULL;
880 } else {
881 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
882 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700883 }
884
885done:
Tao Baoba9a42a2015-06-23 23:23:33 -0700886 for (int i = 0; i < argc; ++i) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700887 free(args[i]);
888 }
889 free(args);
890
891 if (result != NULL) {
892 return result;
893 }
894
895 if (bad > 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700896 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700897 }
898
899 return StringValue(strdup(""));
900}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700901
Doug Zongker512536a2010-02-17 16:11:44 -0800902Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700903 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700904 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700905 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700906 char* key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700907 if (key == NULL) return NULL;
908
Elliott Hughescb220402016-09-23 15:30:55 -0700909 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700910 free(key);
911
Elliott Hughescb220402016-09-23 15:30:55 -0700912 return StringValue(strdup(value.c_str()));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700913}
914
915
Doug Zongker47cace92009-06-18 10:11:50 -0700916// file_getprop(file, key)
917//
918// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700919// per line. # comment lines,blank lines, lines without '=' ignored),
920// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800921Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700922 char* result = NULL;
923 char* buffer = NULL;
924 char* filename;
925 char* key;
926 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
927 return NULL;
928 }
929
930 struct stat st;
931 if (stat(filename, &st) < 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700932 ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name, filename,
933 strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700934 goto done;
935 }
936
937#define MAX_FILE_GETPROP_SIZE 65536
938
939 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tianjie Xu16255832016-04-30 11:49:59 -0700940 ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %d)", filename, name,
941 MAX_FILE_GETPROP_SIZE);
Doug Zongker47cace92009-06-18 10:11:50 -0700942 goto done;
943 }
944
Tao Baoba9a42a2015-06-23 23:23:33 -0700945 buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700946 if (buffer == NULL) {
Tianjie Xu84478e82016-05-23 11:42:40 -0700947 ErrorAbort(state, kFileGetPropFailure, "%s: failed to alloc %zu bytes", name,
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700948 static_cast<size_t>(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700949 goto done;
950 }
951
Tao Baoba9a42a2015-06-23 23:23:33 -0700952 FILE* f;
Jed Estepa7b9a462015-12-15 16:04:53 -0800953 f = ota_fopen(filename, "rb");
Doug Zongker47cace92009-06-18 10:11:50 -0700954 if (f == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -0700955 ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename,
956 strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700957 goto done;
958 }
959
Jed Estepa7b9a462015-12-15 16:04:53 -0800960 if (ota_fread(buffer, 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
Tianjie Xu84478e82016-05-23 11:42:40 -0700961 ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s",
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700962 name, static_cast<size_t>(st.st_size), filename);
Jed Estepa7b9a462015-12-15 16:04:53 -0800963 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -0700964 goto done;
965 }
966 buffer[st.st_size] = '\0';
967
Jed Estepa7b9a462015-12-15 16:04:53 -0800968 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -0700969
Tao Baoba9a42a2015-06-23 23:23:33 -0700970 char* line;
971 line = strtok(buffer, "\n");
Doug Zongker47cace92009-06-18 10:11:50 -0700972 do {
973 // skip whitespace at start of line
974 while (*line && isspace(*line)) ++line;
975
976 // comment or blank line: skip to next line
977 if (*line == '\0' || *line == '#') continue;
978
979 char* equal = strchr(line, '=');
980 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700981 continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700982 }
983
984 // trim whitespace between key and '='
985 char* key_end = equal-1;
986 while (key_end > line && isspace(*key_end)) --key_end;
987 key_end[1] = '\0';
988
989 // not the key we're looking for
990 if (strcmp(key, line) != 0) continue;
991
992 // skip whitespace after the '=' to the start of the value
993 char* val_start = equal+1;
994 while(*val_start && isspace(*val_start)) ++val_start;
995
996 // trim trailing whitespace
997 char* val_end = val_start + strlen(val_start)-1;
998 while (val_end > val_start && isspace(*val_end)) --val_end;
999 val_end[1] = '\0';
1000
1001 result = strdup(val_start);
1002 break;
1003
1004 } while ((line = strtok(NULL, "\n")));
1005
1006 if (result == NULL) result = strdup("");
1007
1008 done:
1009 free(filename);
1010 free(key);
1011 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001012 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001013}
1014
Doug Zongker8edb00c2009-06-11 17:21:44 -07001015// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001016Value* ApplyPatchSpaceFn(const char* name, State* state,
1017 int argc, Expr* argv[]) {
1018 char* bytes_str;
1019 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1020 return NULL;
1021 }
1022
Tao Baob15fd222015-09-24 11:10:51 -07001023 size_t bytes;
1024 if (!android::base::ParseUint(bytes_str, &bytes)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001025 ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n",
1026 name, bytes_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001027 free(bytes_str);
Tao Baob15fd222015-09-24 11:10:51 -07001028 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001029 }
1030
1031 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1032}
1033
Doug Zongker52b40362014-02-10 15:30:30 -08001034// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1035
Doug Zongker512536a2010-02-17 16:11:44 -08001036Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001037 if (argc < 6 || (argc % 2) == 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001038 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an "
1039 "even number, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001040 }
1041
Doug Zongkerc4351c72010-02-22 14:46:32 -08001042 char* source_filename;
1043 char* target_filename;
1044 char* target_sha1;
1045 char* target_size_str;
1046 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1047 &target_sha1, &target_size_str) < 0) {
1048 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001049 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001050
Tao Baob15fd222015-09-24 11:10:51 -07001051 size_t target_size;
1052 if (!android::base::ParseUint(target_size_str, &target_size)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001053 ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count",
1054 name, target_size_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001055 free(source_filename);
1056 free(target_filename);
1057 free(target_sha1);
1058 free(target_size_str);
Tao Baob15fd222015-09-24 11:10:51 -07001059 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001060 }
1061
1062 int patchcount = (argc-4) / 2;
Yabin Cui64be2132016-02-03 18:16:02 -08001063 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc-4, argv+4),
1064 free);
1065 if (!arg_values) {
1066 return nullptr;
1067 }
1068 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patch_shas;
1069 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patches;
1070 // Protect values by unique_ptrs first to get rid of memory leak.
1071 for (int i = 0; i < patchcount * 2; i += 2) {
1072 patch_shas.emplace_back(arg_values.get()[i], FreeValue);
1073 patches.emplace_back(arg_values.get()[i+1], FreeValue);
1074 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001075
Yabin Cui64be2132016-02-03 18:16:02 -08001076 for (int i = 0; i < patchcount; ++i) {
1077 if (patch_shas[i]->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001078 ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001079 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001080 }
Yabin Cui64be2132016-02-03 18:16:02 -08001081 if (patches[i]->type != VAL_BLOB) {
Tianjie Xu16255832016-04-30 11:49:59 -07001082 ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001083 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001084 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001085 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001086
Yabin Cui64be2132016-02-03 18:16:02 -08001087 std::vector<char*> patch_sha_str;
1088 std::vector<Value*> patch_ptrs;
1089 for (int i = 0; i < patchcount; ++i) {
1090 patch_sha_str.push_back(patch_shas[i]->data);
1091 patch_ptrs.push_back(patches[i].get());
Doug Zongker8edb00c2009-06-11 17:21:44 -07001092 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001093
1094 int result = applypatch(source_filename, target_filename,
1095 target_sha1, target_size,
Yabin Cui64be2132016-02-03 18:16:02 -08001096 patchcount, patch_sha_str.data(), patch_ptrs.data(), NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001097
1098 return StringValue(strdup(result == 0 ? "t" : ""));
1099}
1100
1101// apply_patch_check(file, [sha1_1, ...])
1102Value* ApplyPatchCheckFn(const char* name, State* state,
1103 int argc, Expr* argv[]) {
1104 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001105 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %d",
Doug Zongkerc4351c72010-02-22 14:46:32 -08001106 name, argc);
1107 }
1108
1109 char* filename;
1110 if (ReadArgs(state, argv, 1, &filename) < 0) {
1111 return NULL;
1112 }
1113
1114 int patchcount = argc-1;
1115 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1116
1117 int result = applypatch_check(filename, patchcount, sha1s);
1118
1119 int i;
1120 for (i = 0; i < patchcount; ++i) {
1121 free(sha1s[i]);
1122 }
1123 free(sha1s);
1124
1125 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001126}
1127
Tao Bao1107d962015-09-09 17:16:55 -07001128// This is the updater side handler for ui_print() in edify script. Contents
1129// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001130Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001131 char** args = ReadVarArgs(state, argc, argv);
1132 if (args == NULL) {
1133 return NULL;
1134 }
1135
Tao Bao1107d962015-09-09 17:16:55 -07001136 std::string buffer;
1137 for (int i = 0; i < argc; ++i) {
1138 buffer += args[i];
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001139 free(args[i]);
1140 }
1141 free(args);
Tao Bao1107d962015-09-09 17:16:55 -07001142
1143 buffer += "\n";
Michael Runged4a63422014-10-22 19:48:41 -07001144 uiPrint(state, buffer);
Tao Bao1107d962015-09-09 17:16:55 -07001145 return StringValue(strdup(buffer.c_str()));
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001146}
1147
Doug Zongkerd0181b82011-10-19 10:51:12 -07001148Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1149 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001150 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001151 }
1152 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1153 return StringValue(strdup("t"));
1154}
1155
Doug Zongker512536a2010-02-17 16:11:44 -08001156Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001157 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001158 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001159 }
1160 char** args = ReadVarArgs(state, argc, argv);
1161 if (args == NULL) {
1162 return NULL;
1163 }
1164
Tao Baoba9a42a2015-06-23 23:23:33 -07001165 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001166 memcpy(args2, args, sizeof(char*) * argc);
1167 args2[argc] = NULL;
1168
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001169 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001170
1171 pid_t child = fork();
1172 if (child == 0) {
1173 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001174 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001175 _exit(1);
1176 }
1177 int status;
1178 waitpid(child, &status, 0);
1179 if (WIFEXITED(status)) {
1180 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001181 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001182 WEXITSTATUS(status));
1183 }
1184 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001185 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001186 WTERMSIG(status));
1187 }
1188
1189 int i;
1190 for (i = 0; i < argc; ++i) {
1191 free(args[i]);
1192 }
1193 free(args);
1194 free(args2);
1195
1196 char buffer[20];
1197 sprintf(buffer, "%d", status);
1198
Doug Zongker512536a2010-02-17 16:11:44 -08001199 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001200}
1201
Doug Zongker512536a2010-02-17 16:11:44 -08001202// sha1_check(data)
1203// to return the sha1 of the data (given in the format returned by
1204// read_file).
1205//
1206// sha1_check(data, sha1_hex, [sha1_hex, ...])
1207// returns the sha1 of the file if it matches any of the hex
1208// strings passed, or "" if it does not equal any of them.
1209//
1210Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1211 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001212 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongker512536a2010-02-17 16:11:44 -08001213 }
1214
Yabin Cui64be2132016-02-03 18:16:02 -08001215 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc, argv), free);
1216 if (arg_values == nullptr) {
1217 return nullptr;
1218 }
1219 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> args;
1220 for (int i = 0; i < argc; ++i) {
1221 args.emplace_back(arg_values.get()[i], FreeValue);
Doug Zongker512536a2010-02-17 16:11:44 -08001222 }
1223
1224 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001225 return StringValue(strdup(""));
1226 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001227 uint8_t digest[SHA_DIGEST_LENGTH];
1228 SHA1(reinterpret_cast<uint8_t*>(args[0]->data), args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001229
1230 if (argc == 1) {
1231 return StringValue(PrintSha1(digest));
1232 }
1233
1234 int i;
Yabin Cui64be2132016-02-03 18:16:02 -08001235 uint8_t arg_digest[SHA_DIGEST_LENGTH];
Doug Zongker512536a2010-02-17 16:11:44 -08001236 for (i = 1; i < argc; ++i) {
1237 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001238 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001239 name, i);
1240 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1241 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001242 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1243 name, args[i]->data);
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001244 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001245 break;
1246 }
Doug Zongker512536a2010-02-17 16:11:44 -08001247 }
1248 if (i >= argc) {
1249 // Didn't match any of the hex strings; return false.
1250 return StringValue(strdup(""));
1251 }
Yabin Cui64be2132016-02-03 18:16:02 -08001252 // Found a match.
1253 return args[i].release();
Doug Zongker512536a2010-02-17 16:11:44 -08001254}
1255
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001256// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001257// is actually a FileContents*).
1258Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1259 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001260 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker512536a2010-02-17 16:11:44 -08001261 }
1262 char* filename;
1263 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1264
Yabin Cuid6c93af2016-02-10 16:41:10 -08001265 Value* v = static_cast<Value*>(malloc(sizeof(Value)));
1266 if (v == nullptr) {
1267 return nullptr;
1268 }
Doug Zongker512536a2010-02-17 16:11:44 -08001269 v->type = VAL_BLOB;
Yabin Cuid6c93af2016-02-10 16:41:10 -08001270 v->size = -1;
1271 v->data = nullptr;
Doug Zongker512536a2010-02-17 16:11:44 -08001272
1273 FileContents fc;
Tao Baoefacd802016-06-11 03:56:38 -07001274 if (LoadFileContents(filename, &fc) == 0) {
Yabin Cuid6c93af2016-02-10 16:41:10 -08001275 v->data = static_cast<char*>(malloc(fc.data.size()));
1276 if (v->data != nullptr) {
1277 memcpy(v->data, fc.data.data(), fc.data.size());
1278 v->size = fc.data.size();
1279 }
Doug Zongker512536a2010-02-17 16:11:44 -08001280 }
Doug Zongker512536a2010-02-17 16:11:44 -08001281 free(filename);
1282 return v;
1283}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001284
Doug Zongkerc87bab12013-11-25 13:53:25 -08001285// Immediately reboot the device. Recovery is not finished normally,
1286// so if you reboot into recovery it will re-start applying the
1287// current package (because nothing has cleared the copy of the
1288// arguments stored in the BCB).
1289//
1290// The argument is the partition name passed to the android reboot
1291// property. It can be "recovery" to boot from the recovery
1292// partition, or "" (empty string) to boot from the regular boot
1293// partition.
1294Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1295 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001296 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001297 }
1298
1299 char* filename;
1300 char* property;
1301 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1302
Doug Zongkerc87bab12013-11-25 13:53:25 -08001303 // zero out the 'command' field of the bootloader message.
Elliott Hughescb220402016-09-23 15:30:55 -07001304 char buffer[80];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001305 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
Jed Estepa7b9a462015-12-15 16:04:53 -08001306 FILE* f = ota_fopen(filename, "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001307 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001308 ota_fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1309 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001310 free(filename);
1311
Elliott Hughescb220402016-09-23 15:30:55 -07001312 std::string reboot_cmd = "reboot,";
1313 if (property != nullptr) reboot_cmd += property;
1314 android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001315
1316 sleep(5);
1317 free(property);
Tianjie Xu16255832016-04-30 11:49:59 -07001318 ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001319 return NULL;
1320}
1321
1322// Store a string value somewhere that future invocations of recovery
1323// can access it. This value is called the "stage" and can be used to
1324// drive packages that need to do reboots in the middle of
1325// installation and keep track of where they are in the multi-stage
1326// install.
1327//
1328// The first argument is the block device for the misc partition
1329// ("/misc" in the fstab), which is where this value is stored. The
1330// second argument is the string to store; it should not exceed 31
1331// bytes.
1332Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1333 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001334 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001335 }
1336
1337 char* filename;
1338 char* stagestr;
1339 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1340
1341 // Store this value in the misc partition, immediately after the
1342 // bootloader message that the main recovery uses to save its
1343 // arguments in case of the device restarting midway through
1344 // package installation.
Jed Estepa7b9a462015-12-15 16:04:53 -08001345 FILE* f = ota_fopen(filename, "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001346 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1347 int to_write = strlen(stagestr)+1;
1348 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1349 if (to_write > max_size) {
1350 to_write = max_size;
1351 stagestr[max_size-1] = 0;
1352 }
Jed Estepa7b9a462015-12-15 16:04:53 -08001353 ota_fwrite(stagestr, to_write, 1, f);
1354 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001355
1356 free(stagestr);
1357 return StringValue(filename);
1358}
1359
1360// Return the value most recently saved with SetStageFn. The argument
1361// is the block device for the misc partition.
1362Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001363 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001364 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001365 }
1366
1367 char* filename;
1368 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1369
1370 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
Jed Estepa7b9a462015-12-15 16:04:53 -08001371 FILE* f = ota_fopen(filename, "rb");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001372 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001373 ota_fread(buffer, sizeof(buffer), 1, f);
1374 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001375 buffer[sizeof(buffer)-1] = '\0';
1376
1377 return StringValue(strdup(buffer));
1378}
1379
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001380Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1381 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001382 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001383 }
1384
1385 char* filename;
1386 char* len_str;
1387 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1388
Tao Baob15fd222015-09-24 11:10:51 -07001389 size_t len;
1390 android::base::ParseUint(len_str, &len);
Jed Estepa7b9a462015-12-15 16:04:53 -08001391 int fd = ota_open(filename, O_WRONLY, 0644);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001392 int success = wipe_block_device(fd, len);
1393
1394 free(filename);
1395 free(len_str);
1396
Jed Estepa7b9a462015-12-15 16:04:53 -08001397 ota_close(fd);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001398
1399 return StringValue(strdup(success ? "t" : ""));
1400}
1401
Doug Zongkerc704e062014-05-23 08:40:35 -07001402Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1403 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001404 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerc704e062014-05-23 08:40:35 -07001405 }
1406 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1407 fprintf(ui->cmd_pipe, "enable_reboot\n");
1408 return StringValue(strdup("t"));
1409}
1410
Michael Rungeacf47db2014-11-21 00:12:28 -08001411Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1412 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001413 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %d", name, argc);
Michael Rungeacf47db2014-11-21 00:12:28 -08001414 }
1415
1416 char** args = ReadVarArgs(state, argc, argv);
1417 if (args == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -07001418 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
Michael Rungeacf47db2014-11-21 00:12:28 -08001419 }
1420
Tao Baoba9a42a2015-06-23 23:23:33 -07001421 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Michael Rungeacf47db2014-11-21 00:12:28 -08001422 // Tune2fs expects the program name as its args[0]
1423 args2[0] = strdup(name);
Tao Baoba9a42a2015-06-23 23:23:33 -07001424 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001425 args2[i + 1] = args[i];
1426 }
1427 int result = tune2fs_main(argc + 1, args2);
Tao Baoba9a42a2015-06-23 23:23:33 -07001428 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001429 free(args[i]);
1430 }
1431 free(args);
1432
1433 free(args2[0]);
1434 free(args2);
1435 if (result != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001436 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d",
1437 name, result);
Michael Rungeacf47db2014-11-21 00:12:28 -08001438 }
1439 return StringValue(strdup("t"));
1440}
1441
Doug Zongker9931f7f2009-06-10 14:11:53 -07001442void RegisterInstallFunctions() {
1443 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001444 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001445 RegisterFunction("unmount", UnmountFn);
1446 RegisterFunction("format", FormatFn);
1447 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001448 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001449 RegisterFunction("delete", DeleteFn);
1450 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001451 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1452 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001453 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001454
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001455 // Usage:
1456 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1457 // Example:
1458 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1459 RegisterFunction("set_metadata", SetMetadataFn);
1460
1461 // Usage:
1462 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1463 // Example:
1464 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1465 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1466
Doug Zongker8edb00c2009-06-11 17:21:44 -07001467 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001468 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001469
1470 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001471 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1472 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001473
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001474 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001475
Doug Zongker512536a2010-02-17 16:11:44 -08001476 RegisterFunction("read_file", ReadFileFn);
1477 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001478 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001479
Doug Zongkerd0181b82011-10-19 10:51:12 -07001480 RegisterFunction("wipe_cache", WipeCacheFn);
1481
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001482 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001483
1484 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001485
1486 RegisterFunction("reboot_now", RebootNowFn);
1487 RegisterFunction("get_stage", GetStageFn);
1488 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001489
1490 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001491 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001492}