blob: 11d5215c3f86797969af6196bbcfe3f40d375259 [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>
40#include <android-base/strings.h>
41#include <android-base/stringprintf.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070042#include <selinux/label.h>
43#include <selinux/selinux.h>
Tao Bao1107d962015-09-09 17:16:55 -070044
Doug Zongkerc87bab12013-11-25 13:53:25 -080045#include "bootloader.h"
46#include "applypatch/applypatch.h"
47#include "cutils/android_reboot.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070048#include "cutils/misc.h"
49#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070050#include "edify/expr.h"
Sen Jiangc48cb5e2016-02-04 16:23:21 +080051#include "openssl/sha.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070052#include "minzip/DirUtil.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070053#include "mounts.h"
Jed Estep39c1b5e2015-12-15 16:04:53 -080054#include "ota_io.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070055#include "updater.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080056#include "install.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080057#include "tune2fs.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070058
Doug Zongker3d177d02010-07-01 09:18:44 -070059#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080060#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070061
Tao Bao1107d962015-09-09 17:16:55 -070062// Send over the buffer to recovery though the command pipe.
63static void uiPrint(State* state, const std::string& buffer) {
64 UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070065
Tao Bao1107d962015-09-09 17:16:55 -070066 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
67 // So skip sending empty strings to UI.
68 std::vector<std::string> lines = android::base::Split(buffer, "\n");
69 for (auto& line: lines) {
70 if (!line.empty()) {
71 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
72 fprintf(ui->cmd_pipe, "ui_print\n");
73 }
74 }
75
76 // On the updater side, we need to dump the contents to stderr (which has
77 // been redirected to the log file). Because the recovery will only print
78 // the contents to screen when processing pipe command ui_print.
79 fprintf(stderr, "%s", buffer.c_str());
Michael Runged4a63422014-10-22 19:48:41 -070080}
81
82__attribute__((__format__(printf, 2, 3))) __nonnull((2))
83void uiPrintf(State* state, const char* 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) {
113 return ErrorAbort(state, "%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) {
136 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
137 goto done;
138 }
139 if (strlen(partition_type) == 0) {
140 ErrorAbort(state, "partition_type argument to %s() can't be empty",
141 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700142 goto done;
143 }
144 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700145 ErrorAbort(state, "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) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700149 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700150 goto done;
151 }
152
Tao Baoba9a42a2015-06-23 23:23:33 -0700153 {
154 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500155
Tao Baoba9a42a2015-06-23 23:23:33 -0700156 if (sehandle) {
157 selabel_lookup(sehandle, &secontext, mount_point, 0755);
158 setfscreatecon(secontext);
159 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500160
Tao Baoba9a42a2015-06-23 23:23:33 -0700161 mkdir(mount_point, 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700162
Tao Baoba9a42a2015-06-23 23:23:33 -0700163 if (secontext) {
164 freecon(secontext);
165 setfscreatecon(NULL);
166 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500167 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500168
Elliott Hughes63a31922016-06-09 17:41:22 -0700169 if (mount(location, mount_point, fs_type,
170 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
171 has_mount_options ? mount_options : "") < 0) {
172 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
173 name, location, mount_point, strerror(errno));
174 result = strdup("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700175 } else {
Elliott Hughes63a31922016-06-09 17:41:22 -0700176 result = mount_point;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700177 }
178
179done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700180 free(fs_type);
181 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700182 free(location);
183 if (result != mount_point) free(mount_point);
Michael Runge168f7772014-10-22 17:05:08 -0700184 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800185 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700186}
187
Doug Zongker8edb00c2009-06-11 17:21:44 -0700188
189// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800190Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700191 char* result = NULL;
192 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700193 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700194 }
195 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700196 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700197 return NULL;
198 }
199 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700200 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700201 goto done;
202 }
203
204 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700205 {
Elliott Hughes63a31922016-06-09 17:41:22 -0700206 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
Tao Baoba9a42a2015-06-23 23:23:33 -0700207 if (vol == NULL) {
208 result = strdup("");
209 } else {
210 result = mount_point;
211 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700212 }
213
214done:
215 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800216 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700217}
218
219
Doug Zongker512536a2010-02-17 16:11:44 -0800220Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700221 char* result = NULL;
222 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700223 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700224 }
225 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700226 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700227 return NULL;
228 }
229 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700230 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700231 goto done;
232 }
233
234 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700235 {
Elliott Hughes63a31922016-06-09 17:41:22 -0700236 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
Tao Baoba9a42a2015-06-23 23:23:33 -0700237 if (vol == NULL) {
238 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
239 result = strdup("");
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, ret, strerror(errno));
245 }
246 result = mount_point;
Michael Runge5ddf4292014-10-24 14:14:41 -0700247 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700248 }
249
250done:
251 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800252 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700253}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700254
JP Abgrall37aedb32014-06-16 19:07:39 -0700255static int exec_cmd(const char* path, char* const argv[]) {
256 int status;
257 pid_t child;
258 if ((child = vfork()) == 0) {
259 execv(path, argv);
260 _exit(-1);
261 }
262 waitpid(child, &status, 0);
263 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
264 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
265 }
266 return WEXITSTATUS(status);
267}
268
Doug Zongker8edb00c2009-06-11 17:21:44 -0700269
Stephen Smalley779701d2012-02-09 14:13:23 -0500270// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700271//
Stephen Smalley779701d2012-02-09 14:13:23 -0500272// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700273// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
274// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800275// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700276// 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 -0800277Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700278 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400279 if (argc != 5) {
280 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700281 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700282 char* fs_type;
283 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700284 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800285 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400286 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500287
Stephen Smalley779701d2012-02-09 14:13:23 -0500288 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
289 return NULL;
290 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700291
Doug Zongker3d177d02010-07-01 09:18:44 -0700292 if (strlen(fs_type) == 0) {
293 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
294 goto done;
295 }
296 if (strlen(partition_type) == 0) {
297 ErrorAbort(state, "partition_type argument to %s() can't be empty",
298 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700299 goto done;
300 }
301 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700302 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700303 goto done;
304 }
305
Stephen Smalley516e4e22012-04-03 13:35:11 -0400306 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500307 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
308 goto done;
309 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500310
Elliott Hughes63a31922016-06-09 17:41:22 -0700311 if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500312 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700313 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700314 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700315 name, status, location);
316 result = strdup("");
317 goto done;
318 }
319 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700320 } else if (strcmp(fs_type, "f2fs") == 0) {
321 char *num_sectors;
322 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
323 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
324 result = strdup("");
325 goto done;
326 }
327 const char *f2fs_path = "/sbin/mkfs.f2fs";
328 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
329 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
330 free(num_sectors);
331 if (status != 0) {
332 printf("%s: mkfs.f2fs failed (%d) on %s",
333 name, status, location);
334 result = strdup("");
335 goto done;
336 }
337 result = location;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700338 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700339 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700340 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700341 }
342
343done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700344 free(fs_type);
345 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700346 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800347 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700348}
349
Michael Rungece7ca712013-11-06 17:42:20 -0800350Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
351 char* result = NULL;
352 if (argc != 2) {
353 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
354 }
355
356 char* src_name;
357 char* dst_name;
358
359 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
360 return NULL;
361 }
362 if (strlen(src_name) == 0) {
363 ErrorAbort(state, "src_name argument to %s() can't be empty", name);
364 goto done;
365 }
366 if (strlen(dst_name) == 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700367 ErrorAbort(state, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800368 goto done;
369 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700370 if (make_parents(dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700371 ErrorAbort(state, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700372 dst_name, strerror(errno));
Michael Runge2f0ef732014-10-22 14:28:23 -0700373 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
374 // File was already moved
375 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700376 } else if (rename(src_name, dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700377 ErrorAbort(state, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800378 src_name, dst_name, strerror(errno));
379 } else {
380 result = dst_name;
381 }
382
383done:
384 free(src_name);
385 if (result != dst_name) free(dst_name);
386 return StringValue(result);
387}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700388
Doug Zongker512536a2010-02-17 16:11:44 -0800389Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700390 char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
391 for (int i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700392 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700393 if (paths[i] == NULL) {
Yabin Cui64be2132016-02-03 18:16:02 -0800394 for (int j = 0; j < i; ++j) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700395 free(paths[j]);
396 }
397 free(paths);
398 return NULL;
399 }
400 }
401
402 bool recursive = (strcmp(name, "delete_recursive") == 0);
403
404 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700405 for (int i = 0; i < argc; ++i) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700406 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
407 ++success;
408 free(paths[i]);
409 }
410 free(paths);
411
412 char buffer[10];
413 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800414 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700415}
416
Doug Zongker8edb00c2009-06-11 17:21:44 -0700417
Doug Zongker512536a2010-02-17 16:11:44 -0800418Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700419 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700420 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700421 }
422 char* frac_str;
423 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700424 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700425 return NULL;
426 }
427
428 double frac = strtod(frac_str, NULL);
Tao Baob15fd222015-09-24 11:10:51 -0700429 int sec;
430 android::base::ParseInt(sec_str, &sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700431
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700432 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700433 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
434
Doug Zongker9931f7f2009-06-10 14:11:53 -0700435 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800436 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700437}
438
Doug Zongker512536a2010-02-17 16:11:44 -0800439Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700440 if (argc != 1) {
441 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
442 }
443 char* frac_str;
444 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
445 return NULL;
446 }
447
448 double frac = strtod(frac_str, NULL);
449
450 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
451 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
452
Doug Zongker512536a2010-02-17 16:11:44 -0800453 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700454}
455
Doug Zongker8edb00c2009-06-11 17:21:44 -0700456// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800457Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700458 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700459 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700460 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700461 }
462 char* zip_path;
463 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700464 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700465
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700466 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700467
468 // To create a consistent system image, never use the clock for timestamps.
469 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
470
471 bool success = mzExtractRecursive(za, zip_path, dest_path,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000472 &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500473 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700474 free(zip_path);
475 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800476 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700477}
478
Doug Zongker8edb00c2009-06-11 17:21:44 -0700479
480// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800481// or
482// package_extract_file(package_path)
483// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800484// function (the char* returned is actually a FileContents*).
485Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700486 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700487 if (argc < 1 || argc > 2) {
488 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800489 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700490 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700491 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700492
Doug Zongker5f875bf2014-08-22 14:53:43 -0700493 if (argc == 2) {
494 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800495
496 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700497
Doug Zongker6aece332010-02-01 14:40:12 -0800498 char* zip_path;
499 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700500 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800501
Doug Zongker6aece332010-02-01 14:40:12 -0800502 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
503 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700504 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800505 goto done2;
506 }
507
Tao Baoba9a42a2015-06-23 23:23:33 -0700508 {
Alistair Strachan733285f2016-05-05 16:04:32 -0700509 int fd = TEMP_FAILURE_RETRY(ota_open(dest_path, O_WRONLY | O_CREAT | O_TRUNC,
Tao Baod3cac342015-12-14 15:53:25 -0800510 S_IRUSR | S_IWUSR));
511 if (fd == -1) {
512 printf("%s: can't open %s for write: %s\n", name, dest_path, strerror(errno));
Tao Baoba9a42a2015-06-23 23:23:33 -0700513 goto done2;
514 }
Tao Baod3cac342015-12-14 15:53:25 -0800515 success = mzExtractZipEntryToFile(za, entry, fd);
Jed Estepa7b9a462015-12-15 16:04:53 -0800516 if (ota_fsync(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800517 printf("fsync of \"%s\" failed: %s\n", dest_path, strerror(errno));
518 success = false;
519 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800520 if (ota_close(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800521 printf("close of \"%s\" failed: %s\n", dest_path, strerror(errno));
522 success = false;
523 }
Doug Zongker6aece332010-02-01 14:40:12 -0800524 }
Doug Zongker6aece332010-02-01 14:40:12 -0800525
526 done2:
527 free(zip_path);
528 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800529 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800530 } else {
531 // The one-argument version returns the contents of the file
532 // as the result.
533
534 char* zip_path;
Yabin Cui64be2132016-02-03 18:16:02 -0800535 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
536
Tao Baoba9a42a2015-06-23 23:23:33 -0700537 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -0800538 v->type = VAL_BLOB;
539 v->size = -1;
540 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800541
Doug Zongker6aece332010-02-01 14:40:12 -0800542 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
543 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
544 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700545 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800546 goto done1;
547 }
548
Doug Zongker512536a2010-02-17 16:11:44 -0800549 v->size = mzGetZipEntryUncompLen(entry);
Tao Baoba9a42a2015-06-23 23:23:33 -0700550 v->data = reinterpret_cast<char*>(malloc(v->size));
Doug Zongker512536a2010-02-17 16:11:44 -0800551 if (v->data == NULL) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700552 printf("%s: failed to allocate %zd bytes for %s\n",
553 name, v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800554 goto done1;
555 }
556
Doug Zongker512536a2010-02-17 16:11:44 -0800557 success = mzExtractZipEntryToBuffer(za, entry,
558 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800559
560 done1:
561 free(zip_path);
562 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800563 free(v->data);
564 v->data = NULL;
565 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800566 }
Doug Zongker512536a2010-02-17 16:11:44 -0800567 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700568 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700569}
570
Doug Zongkera23075f2012-08-06 16:19:09 -0700571// Create all parent directories of name, if necessary.
572static int make_parents(char* name) {
573 char* p;
574 for (p = name + (strlen(name)-1); p > name; --p) {
575 if (*p != '/') continue;
576 *p = '\0';
577 if (make_parents(name) < 0) return -1;
578 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700579 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700580 *p = '/';
581 if (result == 0 || errno == EEXIST) {
582 // successfully created or already existed; we're done
583 return 0;
584 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700585 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700586 return -1;
587 }
588 }
589 return 0;
590}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700591
Doug Zongker9931f7f2009-06-10 14:11:53 -0700592// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700593// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800594Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700595 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700596 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700597 }
598 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700599 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700600 if (target == NULL) return NULL;
601
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700602 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700603 if (srcs == NULL) {
604 free(target);
605 return NULL;
606 }
607
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700608 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700609 int i;
610 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700611 if (unlink(srcs[i]) < 0) {
612 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700613 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700614 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700615 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700616 }
617 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700618 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700619 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700620 name, srcs[i], target);
621 ++bad;
622 }
Doug Zongker60babf82009-09-18 15:11:24 -0700623 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700624 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700625 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700626 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700627 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700628 free(srcs[i]);
629 }
630 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700631 if (bad) {
632 return ErrorAbort(state, "%s: some symlinks failed", name);
633 }
Doug Zongker512536a2010-02-17 16:11:44 -0800634 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700635}
636
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700637struct perm_parsed_args {
638 bool has_uid;
639 uid_t uid;
640 bool has_gid;
641 gid_t gid;
642 bool has_mode;
643 mode_t mode;
644 bool has_fmode;
645 mode_t fmode;
646 bool has_dmode;
647 mode_t dmode;
648 bool has_selabel;
649 char* selabel;
650 bool has_capabilities;
651 uint64_t capabilities;
652};
653
Michael Runged4a63422014-10-22 19:48:41 -0700654static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700655 int i;
656 struct perm_parsed_args parsed;
657 int bad = 0;
658 static int max_warnings = 20;
659
660 memset(&parsed, 0, sizeof(parsed));
661
662 for (i = 1; i < argc; i += 2) {
663 if (strcmp("uid", args[i]) == 0) {
664 int64_t uid;
665 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
666 parsed.uid = uid;
667 parsed.has_uid = true;
668 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700669 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700670 bad++;
671 }
672 continue;
673 }
674 if (strcmp("gid", args[i]) == 0) {
675 int64_t gid;
676 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
677 parsed.gid = gid;
678 parsed.has_gid = true;
679 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700680 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700681 bad++;
682 }
683 continue;
684 }
685 if (strcmp("mode", args[i]) == 0) {
686 int32_t mode;
687 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
688 parsed.mode = mode;
689 parsed.has_mode = true;
690 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700691 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700692 bad++;
693 }
694 continue;
695 }
696 if (strcmp("dmode", args[i]) == 0) {
697 int32_t mode;
698 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
699 parsed.dmode = mode;
700 parsed.has_dmode = true;
701 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700702 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700703 bad++;
704 }
705 continue;
706 }
707 if (strcmp("fmode", args[i]) == 0) {
708 int32_t mode;
709 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
710 parsed.fmode = mode;
711 parsed.has_fmode = true;
712 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700713 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700714 bad++;
715 }
716 continue;
717 }
718 if (strcmp("capabilities", args[i]) == 0) {
719 int64_t capabilities;
720 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
721 parsed.capabilities = capabilities;
722 parsed.has_capabilities = true;
723 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700724 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700725 bad++;
726 }
727 continue;
728 }
729 if (strcmp("selabel", args[i]) == 0) {
730 if (args[i+1][0] != '\0') {
731 parsed.selabel = args[i+1];
732 parsed.has_selabel = true;
733 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700734 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700735 bad++;
736 }
737 continue;
738 }
739 if (max_warnings != 0) {
740 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
741 max_warnings--;
742 if (max_warnings == 0) {
743 printf("ParsedPermArgs: suppressing further warnings\n");
744 }
745 }
746 }
747 return parsed;
748}
749
750static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700751 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700752 const char* filename,
753 const struct stat *statptr,
754 struct perm_parsed_args parsed)
755{
756 int bad = 0;
757
Nick Kralevich68802412014-10-23 20:36:42 -0700758 if (parsed.has_selabel) {
759 if (lsetfilecon(filename, parsed.selabel) != 0) {
760 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
761 filename, parsed.selabel, strerror(errno));
762 bad++;
763 }
764 }
765
Nick Kraleviche4612512013-09-10 15:34:19 -0700766 /* ignore symlinks */
767 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700768 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700769 }
770
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700771 if (parsed.has_uid) {
772 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700773 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
774 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700775 bad++;
776 }
777 }
778
779 if (parsed.has_gid) {
780 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700781 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
782 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700783 bad++;
784 }
785 }
786
787 if (parsed.has_mode) {
788 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700789 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
790 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700791 bad++;
792 }
793 }
794
795 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
796 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700797 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
798 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700799 bad++;
800 }
801 }
802
803 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
804 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700805 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700806 filename, parsed.fmode, strerror(errno));
807 bad++;
808 }
809 }
810
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700811 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
812 if (parsed.capabilities == 0) {
813 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
814 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700815 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700816 filename, parsed.capabilities, strerror(errno));
817 bad++;
818 }
819 } else {
820 struct vfs_cap_data cap_data;
821 memset(&cap_data, 0, sizeof(cap_data));
822 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
823 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
824 cap_data.data[0].inheritable = 0;
825 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
826 cap_data.data[1].inheritable = 0;
827 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700828 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
829 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700830 bad++;
831 }
832 }
833 }
834
835 return bad;
836}
837
838// nftw doesn't allow us to pass along context, so we need to use
839// global variables. *sigh*
840static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700841static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700842
843static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
844 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700845 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700846}
847
848static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700849 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700850 struct stat sb;
851 Value* result = NULL;
852
853 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
854
855 if ((argc % 2) != 1) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700856 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700857 }
858
859 char** args = ReadVarArgs(state, argc, argv);
860 if (args == NULL) return NULL;
861
862 if (lstat(args[0], &sb) == -1) {
863 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
864 goto done;
865 }
866
Tao Baoba9a42a2015-06-23 23:23:33 -0700867 {
868 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700869
Tao Baoba9a42a2015-06-23 23:23:33 -0700870 if (recursive) {
871 recursive_parsed_args = parsed;
872 recursive_state = state;
873 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
874 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
875 recursive_state = NULL;
876 } else {
877 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
878 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700879 }
880
881done:
Tao Baoba9a42a2015-06-23 23:23:33 -0700882 for (int i = 0; i < argc; ++i) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700883 free(args[i]);
884 }
885 free(args);
886
887 if (result != NULL) {
888 return result;
889 }
890
891 if (bad > 0) {
892 return ErrorAbort(state, "%s: some changes failed", name);
893 }
894
895 return StringValue(strdup(""));
896}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700897
Doug Zongker512536a2010-02-17 16:11:44 -0800898Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700899 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700900 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700901 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700902 char* key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700903 if (key == NULL) return NULL;
904
905 char value[PROPERTY_VALUE_MAX];
906 property_get(key, value, "");
907 free(key);
908
Doug Zongker512536a2010-02-17 16:11:44 -0800909 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700910}
911
912
Doug Zongker47cace92009-06-18 10:11:50 -0700913// file_getprop(file, key)
914//
915// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700916// per line. # comment lines,blank lines, lines without '=' ignored),
917// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800918Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700919 char* result = NULL;
920 char* buffer = NULL;
921 char* filename;
922 char* key;
923 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
924 return NULL;
925 }
926
927 struct stat st;
928 if (stat(filename, &st) < 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700929 ErrorAbort(state, "%s: failed to stat \"%s\": %s", name, filename, strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700930 goto done;
931 }
932
933#define MAX_FILE_GETPROP_SIZE 65536
934
935 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700936 ErrorAbort(state, "%s too large for %s (max %d)", filename, name, MAX_FILE_GETPROP_SIZE);
Doug Zongker47cace92009-06-18 10:11:50 -0700937 goto done;
938 }
939
Tao Baoba9a42a2015-06-23 23:23:33 -0700940 buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700941 if (buffer == NULL) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700942 ErrorAbort(state, "%s: failed to alloc %zu bytes", name,
943 static_cast<size_t>(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700944 goto done;
945 }
946
Tao Baoba9a42a2015-06-23 23:23:33 -0700947 FILE* f;
Jed Estepa7b9a462015-12-15 16:04:53 -0800948 f = ota_fopen(filename, "rb");
Doug Zongker47cace92009-06-18 10:11:50 -0700949 if (f == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700950 ErrorAbort(state, "%s: failed to open %s: %s", name, filename, strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700951 goto done;
952 }
953
Jed Estepa7b9a462015-12-15 16:04:53 -0800954 if (ota_fread(buffer, 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700955 ErrorAbort(state, "%s: failed to read %zu bytes from %s",
956 name, static_cast<size_t>(st.st_size), filename);
Jed Estepa7b9a462015-12-15 16:04:53 -0800957 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -0700958 goto done;
959 }
960 buffer[st.st_size] = '\0';
961
Jed Estepa7b9a462015-12-15 16:04:53 -0800962 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -0700963
Tao Baoba9a42a2015-06-23 23:23:33 -0700964 char* line;
965 line = strtok(buffer, "\n");
Doug Zongker47cace92009-06-18 10:11:50 -0700966 do {
967 // skip whitespace at start of line
968 while (*line && isspace(*line)) ++line;
969
970 // comment or blank line: skip to next line
971 if (*line == '\0' || *line == '#') continue;
972
973 char* equal = strchr(line, '=');
974 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700975 continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700976 }
977
978 // trim whitespace between key and '='
979 char* key_end = equal-1;
980 while (key_end > line && isspace(*key_end)) --key_end;
981 key_end[1] = '\0';
982
983 // not the key we're looking for
984 if (strcmp(key, line) != 0) continue;
985
986 // skip whitespace after the '=' to the start of the value
987 char* val_start = equal+1;
988 while(*val_start && isspace(*val_start)) ++val_start;
989
990 // trim trailing whitespace
991 char* val_end = val_start + strlen(val_start)-1;
992 while (val_end > val_start && isspace(*val_end)) --val_end;
993 val_end[1] = '\0';
994
995 result = strdup(val_start);
996 break;
997
998 } while ((line = strtok(NULL, "\n")));
999
1000 if (result == NULL) result = strdup("");
1001
1002 done:
1003 free(filename);
1004 free(key);
1005 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001006 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001007}
1008
Doug Zongker8edb00c2009-06-11 17:21:44 -07001009// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001010Value* ApplyPatchSpaceFn(const char* name, State* state,
1011 int argc, Expr* argv[]) {
1012 char* bytes_str;
1013 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1014 return NULL;
1015 }
1016
Tao Baob15fd222015-09-24 11:10:51 -07001017 size_t bytes;
1018 if (!android::base::ParseUint(bytes_str, &bytes)) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001019 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n", name, bytes_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001020 free(bytes_str);
Tao Baob15fd222015-09-24 11:10:51 -07001021 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001022 }
1023
1024 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1025}
1026
Doug Zongker52b40362014-02-10 15:30:30 -08001027// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1028
Doug Zongker512536a2010-02-17 16:11:44 -08001029Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001030 if (argc < 6 || (argc % 2) == 1) {
1031 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1032 "even number, got %d",
1033 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001034 }
1035
Doug Zongkerc4351c72010-02-22 14:46:32 -08001036 char* source_filename;
1037 char* target_filename;
1038 char* target_sha1;
1039 char* target_size_str;
1040 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1041 &target_sha1, &target_size_str) < 0) {
1042 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001043 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001044
Tao Baob15fd222015-09-24 11:10:51 -07001045 size_t target_size;
1046 if (!android::base::ParseUint(target_size_str, &target_size)) {
1047 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count", name, target_size_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001048 free(source_filename);
1049 free(target_filename);
1050 free(target_sha1);
1051 free(target_size_str);
Tao Baob15fd222015-09-24 11:10:51 -07001052 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001053 }
1054
1055 int patchcount = (argc-4) / 2;
Yabin Cui64be2132016-02-03 18:16:02 -08001056 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc-4, argv+4),
1057 free);
1058 if (!arg_values) {
1059 return nullptr;
1060 }
1061 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patch_shas;
1062 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patches;
1063 // Protect values by unique_ptrs first to get rid of memory leak.
1064 for (int i = 0; i < patchcount * 2; i += 2) {
1065 patch_shas.emplace_back(arg_values.get()[i], FreeValue);
1066 patches.emplace_back(arg_values.get()[i+1], FreeValue);
1067 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001068
Yabin Cui64be2132016-02-03 18:16:02 -08001069 for (int i = 0; i < patchcount; ++i) {
1070 if (patch_shas[i]->type != VAL_STRING) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001071 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001072 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001073 }
Yabin Cui64be2132016-02-03 18:16:02 -08001074 if (patches[i]->type != VAL_BLOB) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001075 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001076 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001077 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001078 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001079
Yabin Cui64be2132016-02-03 18:16:02 -08001080 std::vector<char*> patch_sha_str;
1081 std::vector<Value*> patch_ptrs;
1082 for (int i = 0; i < patchcount; ++i) {
1083 patch_sha_str.push_back(patch_shas[i]->data);
1084 patch_ptrs.push_back(patches[i].get());
Doug Zongker8edb00c2009-06-11 17:21:44 -07001085 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001086
1087 int result = applypatch(source_filename, target_filename,
1088 target_sha1, target_size,
Yabin Cui64be2132016-02-03 18:16:02 -08001089 patchcount, patch_sha_str.data(), patch_ptrs.data(), NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001090
1091 return StringValue(strdup(result == 0 ? "t" : ""));
1092}
1093
1094// apply_patch_check(file, [sha1_1, ...])
1095Value* ApplyPatchCheckFn(const char* name, State* state,
1096 int argc, Expr* argv[]) {
1097 if (argc < 1) {
1098 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1099 name, argc);
1100 }
1101
1102 char* filename;
1103 if (ReadArgs(state, argv, 1, &filename) < 0) {
1104 return NULL;
1105 }
1106
1107 int patchcount = argc-1;
1108 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1109
1110 int result = applypatch_check(filename, patchcount, sha1s);
1111
1112 int i;
1113 for (i = 0; i < patchcount; ++i) {
1114 free(sha1s[i]);
1115 }
1116 free(sha1s);
1117
1118 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001119}
1120
Tao Bao1107d962015-09-09 17:16:55 -07001121// This is the updater side handler for ui_print() in edify script. Contents
1122// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001123Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001124 char** args = ReadVarArgs(state, argc, argv);
1125 if (args == NULL) {
1126 return NULL;
1127 }
1128
Tao Bao1107d962015-09-09 17:16:55 -07001129 std::string buffer;
1130 for (int i = 0; i < argc; ++i) {
1131 buffer += args[i];
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001132 free(args[i]);
1133 }
1134 free(args);
Tao Bao1107d962015-09-09 17:16:55 -07001135
1136 buffer += "\n";
Michael Runged4a63422014-10-22 19:48:41 -07001137 uiPrint(state, buffer);
Tao Bao1107d962015-09-09 17:16:55 -07001138 return StringValue(strdup(buffer.c_str()));
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001139}
1140
Doug Zongkerd0181b82011-10-19 10:51:12 -07001141Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1142 if (argc != 0) {
1143 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1144 }
1145 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1146 return StringValue(strdup("t"));
1147}
1148
Doug Zongker512536a2010-02-17 16:11:44 -08001149Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001150 if (argc < 1) {
1151 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1152 }
1153 char** args = ReadVarArgs(state, argc, argv);
1154 if (args == NULL) {
1155 return NULL;
1156 }
1157
Tao Baoba9a42a2015-06-23 23:23:33 -07001158 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001159 memcpy(args2, args, sizeof(char*) * argc);
1160 args2[argc] = NULL;
1161
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001162 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001163
1164 pid_t child = fork();
1165 if (child == 0) {
1166 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001167 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001168 _exit(1);
1169 }
1170 int status;
1171 waitpid(child, &status, 0);
1172 if (WIFEXITED(status)) {
1173 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001174 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001175 WEXITSTATUS(status));
1176 }
1177 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001178 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001179 WTERMSIG(status));
1180 }
1181
1182 int i;
1183 for (i = 0; i < argc; ++i) {
1184 free(args[i]);
1185 }
1186 free(args);
1187 free(args2);
1188
1189 char buffer[20];
1190 sprintf(buffer, "%d", status);
1191
Doug Zongker512536a2010-02-17 16:11:44 -08001192 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001193}
1194
Doug Zongker512536a2010-02-17 16:11:44 -08001195// sha1_check(data)
1196// to return the sha1 of the data (given in the format returned by
1197// read_file).
1198//
1199// sha1_check(data, sha1_hex, [sha1_hex, ...])
1200// returns the sha1 of the file if it matches any of the hex
1201// strings passed, or "" if it does not equal any of them.
1202//
1203Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1204 if (argc < 1) {
1205 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1206 }
1207
Yabin Cui64be2132016-02-03 18:16:02 -08001208 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc, argv), free);
1209 if (arg_values == nullptr) {
1210 return nullptr;
1211 }
1212 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> args;
1213 for (int i = 0; i < argc; ++i) {
1214 args.emplace_back(arg_values.get()[i], FreeValue);
Doug Zongker512536a2010-02-17 16:11:44 -08001215 }
1216
1217 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001218 return StringValue(strdup(""));
1219 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001220 uint8_t digest[SHA_DIGEST_LENGTH];
1221 SHA1(reinterpret_cast<uint8_t*>(args[0]->data), args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001222
1223 if (argc == 1) {
1224 return StringValue(PrintSha1(digest));
1225 }
1226
1227 int i;
Yabin Cui64be2132016-02-03 18:16:02 -08001228 uint8_t arg_digest[SHA_DIGEST_LENGTH];
Doug Zongker512536a2010-02-17 16:11:44 -08001229 for (i = 1; i < argc; ++i) {
1230 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001231 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001232 name, i);
1233 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1234 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001235 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1236 name, args[i]->data);
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001237 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001238 break;
1239 }
Doug Zongker512536a2010-02-17 16:11:44 -08001240 }
1241 if (i >= argc) {
1242 // Didn't match any of the hex strings; return false.
1243 return StringValue(strdup(""));
1244 }
Yabin Cui64be2132016-02-03 18:16:02 -08001245 // Found a match.
1246 return args[i].release();
Doug Zongker512536a2010-02-17 16:11:44 -08001247}
1248
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001249// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001250// is actually a FileContents*).
1251Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1252 if (argc != 1) {
1253 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1254 }
1255 char* filename;
1256 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1257
Yabin Cuid6c93af2016-02-10 16:41:10 -08001258 Value* v = static_cast<Value*>(malloc(sizeof(Value)));
1259 if (v == nullptr) {
1260 return nullptr;
1261 }
Doug Zongker512536a2010-02-17 16:11:44 -08001262 v->type = VAL_BLOB;
Yabin Cuid6c93af2016-02-10 16:41:10 -08001263 v->size = -1;
1264 v->data = nullptr;
Doug Zongker512536a2010-02-17 16:11:44 -08001265
1266 FileContents fc;
Tao Baoefacd802016-06-11 03:56:38 -07001267 if (LoadFileContents(filename, &fc) == 0) {
Yabin Cuid6c93af2016-02-10 16:41:10 -08001268 v->data = static_cast<char*>(malloc(fc.data.size()));
1269 if (v->data != nullptr) {
1270 memcpy(v->data, fc.data.data(), fc.data.size());
1271 v->size = fc.data.size();
1272 }
Doug Zongker512536a2010-02-17 16:11:44 -08001273 }
Doug Zongker512536a2010-02-17 16:11:44 -08001274 free(filename);
1275 return v;
1276}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001277
Doug Zongkerc87bab12013-11-25 13:53:25 -08001278// Immediately reboot the device. Recovery is not finished normally,
1279// so if you reboot into recovery it will re-start applying the
1280// current package (because nothing has cleared the copy of the
1281// arguments stored in the BCB).
1282//
1283// The argument is the partition name passed to the android reboot
1284// property. It can be "recovery" to boot from the recovery
1285// partition, or "" (empty string) to boot from the regular boot
1286// partition.
1287Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1288 if (argc != 2) {
1289 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1290 }
1291
1292 char* filename;
1293 char* property;
1294 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1295
1296 char buffer[80];
1297
1298 // zero out the 'command' field of the bootloader message.
1299 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
Jed Estepa7b9a462015-12-15 16:04:53 -08001300 FILE* f = ota_fopen(filename, "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001301 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001302 ota_fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1303 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001304 free(filename);
1305
1306 strcpy(buffer, "reboot,");
1307 if (property != NULL) {
1308 strncat(buffer, property, sizeof(buffer)-10);
1309 }
1310
1311 property_set(ANDROID_RB_PROPERTY, buffer);
1312
1313 sleep(5);
1314 free(property);
1315 ErrorAbort(state, "%s() failed to reboot", name);
1316 return NULL;
1317}
1318
1319// Store a string value somewhere that future invocations of recovery
1320// can access it. This value is called the "stage" and can be used to
1321// drive packages that need to do reboots in the middle of
1322// installation and keep track of where they are in the multi-stage
1323// install.
1324//
1325// The first argument is the block device for the misc partition
1326// ("/misc" in the fstab), which is where this value is stored. The
1327// second argument is the string to store; it should not exceed 31
1328// bytes.
1329Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1330 if (argc != 2) {
1331 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1332 }
1333
1334 char* filename;
1335 char* stagestr;
1336 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1337
1338 // Store this value in the misc partition, immediately after the
1339 // bootloader message that the main recovery uses to save its
1340 // arguments in case of the device restarting midway through
1341 // package installation.
Jed Estepa7b9a462015-12-15 16:04:53 -08001342 FILE* f = ota_fopen(filename, "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001343 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1344 int to_write = strlen(stagestr)+1;
1345 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1346 if (to_write > max_size) {
1347 to_write = max_size;
1348 stagestr[max_size-1] = 0;
1349 }
Jed Estepa7b9a462015-12-15 16:04:53 -08001350 ota_fwrite(stagestr, to_write, 1, f);
1351 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001352
1353 free(stagestr);
1354 return StringValue(filename);
1355}
1356
1357// Return the value most recently saved with SetStageFn. The argument
1358// is the block device for the misc partition.
1359Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001360 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001361 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1362 }
1363
1364 char* filename;
1365 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1366
1367 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
Jed Estepa7b9a462015-12-15 16:04:53 -08001368 FILE* f = ota_fopen(filename, "rb");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001369 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001370 ota_fread(buffer, sizeof(buffer), 1, f);
1371 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001372 buffer[sizeof(buffer)-1] = '\0';
1373
1374 return StringValue(strdup(buffer));
1375}
1376
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001377Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1378 if (argc != 2) {
1379 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1380 }
1381
1382 char* filename;
1383 char* len_str;
1384 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1385
Tao Baob15fd222015-09-24 11:10:51 -07001386 size_t len;
1387 android::base::ParseUint(len_str, &len);
Jed Estepa7b9a462015-12-15 16:04:53 -08001388 int fd = ota_open(filename, O_WRONLY, 0644);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001389 int success = wipe_block_device(fd, len);
1390
1391 free(filename);
1392 free(len_str);
1393
Jed Estepa7b9a462015-12-15 16:04:53 -08001394 ota_close(fd);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001395
1396 return StringValue(strdup(success ? "t" : ""));
1397}
1398
Doug Zongkerc704e062014-05-23 08:40:35 -07001399Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1400 if (argc != 0) {
1401 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1402 }
1403 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1404 fprintf(ui->cmd_pipe, "enable_reboot\n");
1405 return StringValue(strdup("t"));
1406}
1407
Michael Rungeacf47db2014-11-21 00:12:28 -08001408Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1409 if (argc == 0) {
1410 return ErrorAbort(state, "%s() expects args, got %d", name, argc);
1411 }
1412
1413 char** args = ReadVarArgs(state, argc, argv);
1414 if (args == NULL) {
1415 return ErrorAbort(state, "%s() could not read args", name);
1416 }
1417
Tao Baoba9a42a2015-06-23 23:23:33 -07001418 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Michael Rungeacf47db2014-11-21 00:12:28 -08001419 // Tune2fs expects the program name as its args[0]
1420 args2[0] = strdup(name);
Tao Baoba9a42a2015-06-23 23:23:33 -07001421 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001422 args2[i + 1] = args[i];
1423 }
1424 int result = tune2fs_main(argc + 1, args2);
Tao Baoba9a42a2015-06-23 23:23:33 -07001425 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001426 free(args[i]);
1427 }
1428 free(args);
1429
1430 free(args2[0]);
1431 free(args2);
1432 if (result != 0) {
1433 return ErrorAbort(state, "%s() returned error code %d", name, result);
1434 }
1435 return StringValue(strdup("t"));
1436}
1437
Doug Zongker9931f7f2009-06-10 14:11:53 -07001438void RegisterInstallFunctions() {
1439 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001440 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001441 RegisterFunction("unmount", UnmountFn);
1442 RegisterFunction("format", FormatFn);
1443 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001444 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001445 RegisterFunction("delete", DeleteFn);
1446 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001447 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1448 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001449 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001450
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001451 // Usage:
1452 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1453 // Example:
1454 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1455 RegisterFunction("set_metadata", SetMetadataFn);
1456
1457 // Usage:
1458 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1459 // Example:
1460 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1461 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1462
Doug Zongker8edb00c2009-06-11 17:21:44 -07001463 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001464 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001465
1466 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001467 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1468 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001469
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001470 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001471
Doug Zongker512536a2010-02-17 16:11:44 -08001472 RegisterFunction("read_file", ReadFileFn);
1473 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001474 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001475
Doug Zongkerd0181b82011-10-19 10:51:12 -07001476 RegisterFunction("wipe_cache", WipeCacheFn);
1477
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001478 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001479
1480 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001481
1482 RegisterFunction("reboot_now", RebootNowFn);
1483 RegisterFunction("get_stage", GetStageFn);
1484 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001485
1486 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001487 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001488}