blob: 3e7e9285a68ba72543700baefc1793a42c881bcd [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
Elliott Hughes83ce7552016-06-30 09:28:42 -070082void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) {
Tao Bao1107d962015-09-09 17:16:55 -070083 std::string error_msg;
84
Michael Runged4a63422014-10-22 19:48:41 -070085 va_list ap;
86 va_start(ap, format);
Tao Bao1107d962015-09-09 17:16:55 -070087 android::base::StringAppendV(&error_msg, format, ap);
Michael Runged4a63422014-10-22 19:48:41 -070088 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070089
Michael Runged4a63422014-10-22 19:48:41 -070090 uiPrint(state, error_msg);
91}
92
Doug Zongker52b40362014-02-10 15:30:30 -080093// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070094char* PrintSha1(const uint8_t* digest) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +080095 char* buffer = reinterpret_cast<char*>(malloc(SHA_DIGEST_LENGTH*2 + 1));
Doug Zongker52b40362014-02-10 15:30:30 -080096 const char* alphabet = "0123456789abcdef";
Tao Baoba9a42a2015-06-23 23:23:33 -070097 size_t i;
Sen Jiangc48cb5e2016-02-04 16:23:21 +080098 for (i = 0; i < SHA_DIGEST_LENGTH; ++i) {
Doug Zongker52b40362014-02-10 15:30:30 -080099 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
100 buffer[i*2+1] = alphabet[digest[i] & 0xf];
101 }
102 buffer[i*2] = '\0';
103 return buffer;
104}
105
Doug Zongker3d177d02010-07-01 09:18:44 -0700106// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700107//
Doug Zongker3d177d02010-07-01 09:18:44 -0700108// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800109Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700110 char* result = NULL;
Michael Runge168f7772014-10-22 17:05:08 -0700111 if (argc != 4 && argc != 5) {
112 return ErrorAbort(state, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700113 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700114 char* fs_type;
115 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700116 char* location;
117 char* mount_point;
Michael Runge168f7772014-10-22 17:05:08 -0700118 char* mount_options;
119 bool has_mount_options;
120 if (argc == 5) {
121 has_mount_options = true;
122 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
123 &location, &mount_point, &mount_options) < 0) {
124 return NULL;
125 }
126 } else {
127 has_mount_options = false;
128 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700129 &location, &mount_point) < 0) {
Michael Runge168f7772014-10-22 17:05:08 -0700130 return NULL;
131 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700132 }
133
Doug Zongker3d177d02010-07-01 09:18:44 -0700134 if (strlen(fs_type) == 0) {
135 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
136 goto done;
137 }
138 if (strlen(partition_type) == 0) {
139 ErrorAbort(state, "partition_type argument to %s() can't be empty",
140 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700141 goto done;
142 }
143 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700144 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700145 goto done;
146 }
147 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700148 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700149 goto done;
150 }
151
Tao Baoba9a42a2015-06-23 23:23:33 -0700152 {
153 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500154
Tao Baoba9a42a2015-06-23 23:23:33 -0700155 if (sehandle) {
156 selabel_lookup(sehandle, &secontext, mount_point, 0755);
157 setfscreatecon(secontext);
158 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500159
Tao Baoba9a42a2015-06-23 23:23:33 -0700160 mkdir(mount_point, 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700161
Tao Baoba9a42a2015-06-23 23:23:33 -0700162 if (secontext) {
163 freecon(secontext);
164 setfscreatecon(NULL);
165 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500166 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500167
Elliott Hughes63a31922016-06-09 17:41:22 -0700168 if (mount(location, mount_point, fs_type,
169 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
170 has_mount_options ? mount_options : "") < 0) {
171 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
172 name, location, mount_point, strerror(errno));
173 result = strdup("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700174 } else {
Elliott Hughes63a31922016-06-09 17:41:22 -0700175 result = mount_point;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700176 }
177
178done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700179 free(fs_type);
180 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700181 free(location);
182 if (result != mount_point) free(mount_point);
Michael Runge168f7772014-10-22 17:05:08 -0700183 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800184 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700185}
186
Doug Zongker8edb00c2009-06-11 17:21:44 -0700187
188// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800189Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700190 char* result = NULL;
191 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700192 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700193 }
194 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700195 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700196 return NULL;
197 }
198 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700199 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700200 goto done;
201 }
202
203 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700204 {
Elliott Hughes63a31922016-06-09 17:41:22 -0700205 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
Tao Baoba9a42a2015-06-23 23:23:33 -0700206 if (vol == NULL) {
207 result = strdup("");
208 } else {
209 result = mount_point;
210 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700211 }
212
213done:
214 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800215 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700216}
217
218
Doug Zongker512536a2010-02-17 16:11:44 -0800219Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700220 char* result = NULL;
221 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700222 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700223 }
224 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700225 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700226 return NULL;
227 }
228 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700229 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700230 goto done;
231 }
232
233 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700234 {
Elliott Hughes63a31922016-06-09 17:41:22 -0700235 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
Tao Baoba9a42a2015-06-23 23:23:33 -0700236 if (vol == NULL) {
237 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
238 result = strdup("");
239 } else {
240 int ret = unmount_mounted_volume(vol);
241 if (ret != 0) {
242 uiPrintf(state, "unmount of %s failed (%d): %s\n",
243 mount_point, ret, strerror(errno));
244 }
245 result = mount_point;
Michael Runge5ddf4292014-10-24 14:14:41 -0700246 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700247 }
248
249done:
250 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800251 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700252}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700253
JP Abgrall37aedb32014-06-16 19:07:39 -0700254static int exec_cmd(const char* path, char* const argv[]) {
255 int status;
256 pid_t child;
257 if ((child = vfork()) == 0) {
258 execv(path, argv);
259 _exit(-1);
260 }
261 waitpid(child, &status, 0);
262 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
263 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
264 }
265 return WEXITSTATUS(status);
266}
267
Doug Zongker8edb00c2009-06-11 17:21:44 -0700268
Stephen Smalley779701d2012-02-09 14:13:23 -0500269// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700270//
Stephen Smalley779701d2012-02-09 14:13:23 -0500271// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700272// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
273// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800274// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700275// 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 -0800276Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700277 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400278 if (argc != 5) {
279 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700280 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700281 char* fs_type;
282 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700283 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800284 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400285 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500286
Stephen Smalley779701d2012-02-09 14:13:23 -0500287 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
288 return NULL;
289 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700290
Doug Zongker3d177d02010-07-01 09:18:44 -0700291 if (strlen(fs_type) == 0) {
292 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
293 goto done;
294 }
295 if (strlen(partition_type) == 0) {
296 ErrorAbort(state, "partition_type argument to %s() can't be empty",
297 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700298 goto done;
299 }
300 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700301 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700302 goto done;
303 }
304
Stephen Smalley516e4e22012-04-03 13:35:11 -0400305 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500306 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
307 goto done;
308 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500309
Elliott Hughes63a31922016-06-09 17:41:22 -0700310 if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500311 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700312 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700313 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700314 name, status, location);
315 result = strdup("");
316 goto done;
317 }
318 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700319 } else if (strcmp(fs_type, "f2fs") == 0) {
320 char *num_sectors;
321 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
322 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
323 result = strdup("");
324 goto done;
325 }
326 const char *f2fs_path = "/sbin/mkfs.f2fs";
327 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
328 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
329 free(num_sectors);
330 if (status != 0) {
331 printf("%s: mkfs.f2fs failed (%d) on %s",
332 name, status, location);
333 result = strdup("");
334 goto done;
335 }
336 result = location;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700337 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700338 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700339 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700340 }
341
342done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700343 free(fs_type);
344 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700345 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800346 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700347}
348
Michael Rungece7ca712013-11-06 17:42:20 -0800349Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
350 char* result = NULL;
351 if (argc != 2) {
352 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
353 }
354
355 char* src_name;
356 char* dst_name;
357
358 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
359 return NULL;
360 }
361 if (strlen(src_name) == 0) {
362 ErrorAbort(state, "src_name argument to %s() can't be empty", name);
363 goto done;
364 }
365 if (strlen(dst_name) == 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700366 ErrorAbort(state, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800367 goto done;
368 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700369 if (make_parents(dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700370 ErrorAbort(state, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700371 dst_name, strerror(errno));
Michael Runge2f0ef732014-10-22 14:28:23 -0700372 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
373 // File was already moved
374 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700375 } else if (rename(src_name, dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700376 ErrorAbort(state, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800377 src_name, dst_name, strerror(errno));
378 } else {
379 result = dst_name;
380 }
381
382done:
383 free(src_name);
384 if (result != dst_name) free(dst_name);
385 return StringValue(result);
386}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700387
Doug Zongker512536a2010-02-17 16:11:44 -0800388Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700389 char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
390 for (int i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700391 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700392 if (paths[i] == NULL) {
Yabin Cui64be2132016-02-03 18:16:02 -0800393 for (int j = 0; j < i; ++j) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700394 free(paths[j]);
395 }
396 free(paths);
397 return NULL;
398 }
399 }
400
401 bool recursive = (strcmp(name, "delete_recursive") == 0);
402
403 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700404 for (int i = 0; i < argc; ++i) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700405 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
406 ++success;
407 free(paths[i]);
408 }
409 free(paths);
410
411 char buffer[10];
412 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800413 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700414}
415
Doug Zongker8edb00c2009-06-11 17:21:44 -0700416
Doug Zongker512536a2010-02-17 16:11:44 -0800417Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700418 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700419 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700420 }
421 char* frac_str;
422 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700423 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700424 return NULL;
425 }
426
427 double frac = strtod(frac_str, NULL);
Tao Baob15fd222015-09-24 11:10:51 -0700428 int sec;
429 android::base::ParseInt(sec_str, &sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700430
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700431 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700432 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
433
Doug Zongker9931f7f2009-06-10 14:11:53 -0700434 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800435 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700436}
437
Doug Zongker512536a2010-02-17 16:11:44 -0800438Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700439 if (argc != 1) {
440 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
441 }
442 char* frac_str;
443 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
444 return NULL;
445 }
446
447 double frac = strtod(frac_str, NULL);
448
449 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
450 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
451
Doug Zongker512536a2010-02-17 16:11:44 -0800452 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700453}
454
Doug Zongker8edb00c2009-06-11 17:21:44 -0700455// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800456Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700457 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700458 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700459 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700460 }
461 char* zip_path;
462 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700463 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700464
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700465 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700466
467 // To create a consistent system image, never use the clock for timestamps.
468 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
469
470 bool success = mzExtractRecursive(za, zip_path, dest_path,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000471 &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500472 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700473 free(zip_path);
474 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800475 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700476}
477
Doug Zongker8edb00c2009-06-11 17:21:44 -0700478
479// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800480// or
481// package_extract_file(package_path)
482// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800483// function (the char* returned is actually a FileContents*).
484Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700485 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700486 if (argc < 1 || argc > 2) {
487 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800488 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700489 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700490 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700491
Doug Zongker5f875bf2014-08-22 14:53:43 -0700492 if (argc == 2) {
493 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800494
495 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700496
Doug Zongker6aece332010-02-01 14:40:12 -0800497 char* zip_path;
498 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700499 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800500
Doug Zongker6aece332010-02-01 14:40:12 -0800501 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
502 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700503 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800504 goto done2;
505 }
506
Tao Baoba9a42a2015-06-23 23:23:33 -0700507 {
Alistair Strachan733285f2016-05-05 16:04:32 -0700508 int fd = TEMP_FAILURE_RETRY(ota_open(dest_path, O_WRONLY | O_CREAT | O_TRUNC,
Tao Baod3cac342015-12-14 15:53:25 -0800509 S_IRUSR | S_IWUSR));
510 if (fd == -1) {
511 printf("%s: can't open %s for write: %s\n", name, dest_path, strerror(errno));
Tao Baoba9a42a2015-06-23 23:23:33 -0700512 goto done2;
513 }
Tao Baod3cac342015-12-14 15:53:25 -0800514 success = mzExtractZipEntryToFile(za, entry, fd);
Jed Estepa7b9a462015-12-15 16:04:53 -0800515 if (ota_fsync(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800516 printf("fsync of \"%s\" failed: %s\n", dest_path, strerror(errno));
517 success = false;
518 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800519 if (ota_close(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800520 printf("close of \"%s\" failed: %s\n", dest_path, strerror(errno));
521 success = false;
522 }
Doug Zongker6aece332010-02-01 14:40:12 -0800523 }
Doug Zongker6aece332010-02-01 14:40:12 -0800524
525 done2:
526 free(zip_path);
527 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800528 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800529 } else {
530 // The one-argument version returns the contents of the file
531 // as the result.
532
533 char* zip_path;
Yabin Cui64be2132016-02-03 18:16:02 -0800534 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
535
Tao Baoba9a42a2015-06-23 23:23:33 -0700536 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -0800537 v->type = VAL_BLOB;
538 v->size = -1;
539 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800540
Doug Zongker6aece332010-02-01 14:40:12 -0800541 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
542 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
543 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700544 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800545 goto done1;
546 }
547
Doug Zongker512536a2010-02-17 16:11:44 -0800548 v->size = mzGetZipEntryUncompLen(entry);
Tao Baoba9a42a2015-06-23 23:23:33 -0700549 v->data = reinterpret_cast<char*>(malloc(v->size));
Doug Zongker512536a2010-02-17 16:11:44 -0800550 if (v->data == NULL) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700551 printf("%s: failed to allocate %zd bytes for %s\n",
552 name, v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800553 goto done1;
554 }
555
Doug Zongker512536a2010-02-17 16:11:44 -0800556 success = mzExtractZipEntryToBuffer(za, entry,
557 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800558
559 done1:
560 free(zip_path);
561 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800562 free(v->data);
563 v->data = NULL;
564 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800565 }
Doug Zongker512536a2010-02-17 16:11:44 -0800566 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700567 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700568}
569
Doug Zongkera23075f2012-08-06 16:19:09 -0700570// Create all parent directories of name, if necessary.
571static int make_parents(char* name) {
572 char* p;
573 for (p = name + (strlen(name)-1); p > name; --p) {
574 if (*p != '/') continue;
575 *p = '\0';
576 if (make_parents(name) < 0) return -1;
577 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700578 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700579 *p = '/';
580 if (result == 0 || errno == EEXIST) {
581 // successfully created or already existed; we're done
582 return 0;
583 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700584 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700585 return -1;
586 }
587 }
588 return 0;
589}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700590
Doug Zongker9931f7f2009-06-10 14:11:53 -0700591// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700592// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800593Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700594 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700595 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700596 }
597 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700598 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700599 if (target == NULL) return NULL;
600
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700601 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700602 if (srcs == NULL) {
603 free(target);
604 return NULL;
605 }
606
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700607 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700608 int i;
609 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700610 if (unlink(srcs[i]) < 0) {
611 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700612 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700613 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700614 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700615 }
616 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700617 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700618 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700619 name, srcs[i], target);
620 ++bad;
621 }
Doug Zongker60babf82009-09-18 15:11:24 -0700622 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700623 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700624 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700625 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700626 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700627 free(srcs[i]);
628 }
629 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700630 if (bad) {
631 return ErrorAbort(state, "%s: some symlinks failed", name);
632 }
Doug Zongker512536a2010-02-17 16:11:44 -0800633 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700634}
635
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700636struct perm_parsed_args {
637 bool has_uid;
638 uid_t uid;
639 bool has_gid;
640 gid_t gid;
641 bool has_mode;
642 mode_t mode;
643 bool has_fmode;
644 mode_t fmode;
645 bool has_dmode;
646 mode_t dmode;
647 bool has_selabel;
648 char* selabel;
649 bool has_capabilities;
650 uint64_t capabilities;
651};
652
Michael Runged4a63422014-10-22 19:48:41 -0700653static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700654 int i;
655 struct perm_parsed_args parsed;
656 int bad = 0;
657 static int max_warnings = 20;
658
659 memset(&parsed, 0, sizeof(parsed));
660
661 for (i = 1; i < argc; i += 2) {
662 if (strcmp("uid", args[i]) == 0) {
663 int64_t uid;
664 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
665 parsed.uid = uid;
666 parsed.has_uid = true;
667 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700668 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700669 bad++;
670 }
671 continue;
672 }
673 if (strcmp("gid", args[i]) == 0) {
674 int64_t gid;
675 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
676 parsed.gid = gid;
677 parsed.has_gid = true;
678 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700679 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700680 bad++;
681 }
682 continue;
683 }
684 if (strcmp("mode", args[i]) == 0) {
685 int32_t mode;
686 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
687 parsed.mode = mode;
688 parsed.has_mode = true;
689 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700690 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700691 bad++;
692 }
693 continue;
694 }
695 if (strcmp("dmode", args[i]) == 0) {
696 int32_t mode;
697 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
698 parsed.dmode = mode;
699 parsed.has_dmode = true;
700 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700701 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700702 bad++;
703 }
704 continue;
705 }
706 if (strcmp("fmode", args[i]) == 0) {
707 int32_t mode;
708 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
709 parsed.fmode = mode;
710 parsed.has_fmode = true;
711 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700712 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700713 bad++;
714 }
715 continue;
716 }
717 if (strcmp("capabilities", args[i]) == 0) {
718 int64_t capabilities;
719 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
720 parsed.capabilities = capabilities;
721 parsed.has_capabilities = true;
722 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700723 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700724 bad++;
725 }
726 continue;
727 }
728 if (strcmp("selabel", args[i]) == 0) {
729 if (args[i+1][0] != '\0') {
730 parsed.selabel = args[i+1];
731 parsed.has_selabel = true;
732 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700733 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700734 bad++;
735 }
736 continue;
737 }
738 if (max_warnings != 0) {
739 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
740 max_warnings--;
741 if (max_warnings == 0) {
742 printf("ParsedPermArgs: suppressing further warnings\n");
743 }
744 }
745 }
746 return parsed;
747}
748
749static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700750 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700751 const char* filename,
752 const struct stat *statptr,
753 struct perm_parsed_args parsed)
754{
755 int bad = 0;
756
Nick Kralevich68802412014-10-23 20:36:42 -0700757 if (parsed.has_selabel) {
758 if (lsetfilecon(filename, parsed.selabel) != 0) {
759 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
760 filename, parsed.selabel, strerror(errno));
761 bad++;
762 }
763 }
764
Nick Kraleviche4612512013-09-10 15:34:19 -0700765 /* ignore symlinks */
766 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700767 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700768 }
769
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700770 if (parsed.has_uid) {
771 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700772 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
773 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700774 bad++;
775 }
776 }
777
778 if (parsed.has_gid) {
779 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700780 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
781 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700782 bad++;
783 }
784 }
785
786 if (parsed.has_mode) {
787 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700788 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
789 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700790 bad++;
791 }
792 }
793
794 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
795 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700796 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
797 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700798 bad++;
799 }
800 }
801
802 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
803 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700804 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700805 filename, parsed.fmode, strerror(errno));
806 bad++;
807 }
808 }
809
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700810 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
811 if (parsed.capabilities == 0) {
812 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
813 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700814 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700815 filename, parsed.capabilities, strerror(errno));
816 bad++;
817 }
818 } else {
819 struct vfs_cap_data cap_data;
820 memset(&cap_data, 0, sizeof(cap_data));
821 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
822 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
823 cap_data.data[0].inheritable = 0;
824 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
825 cap_data.data[1].inheritable = 0;
826 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700827 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
828 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700829 bad++;
830 }
831 }
832 }
833
834 return bad;
835}
836
837// nftw doesn't allow us to pass along context, so we need to use
838// global variables. *sigh*
839static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700840static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700841
842static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
843 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700844 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700845}
846
847static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700848 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700849 struct stat sb;
850 Value* result = NULL;
851
852 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
853
854 if ((argc % 2) != 1) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700855 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700856 }
857
858 char** args = ReadVarArgs(state, argc, argv);
859 if (args == NULL) return NULL;
860
861 if (lstat(args[0], &sb) == -1) {
862 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
863 goto done;
864 }
865
Tao Baoba9a42a2015-06-23 23:23:33 -0700866 {
867 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700868
Tao Baoba9a42a2015-06-23 23:23:33 -0700869 if (recursive) {
870 recursive_parsed_args = parsed;
871 recursive_state = state;
872 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
873 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
874 recursive_state = NULL;
875 } else {
876 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
877 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700878 }
879
880done:
Tao Baoba9a42a2015-06-23 23:23:33 -0700881 for (int i = 0; i < argc; ++i) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700882 free(args[i]);
883 }
884 free(args);
885
886 if (result != NULL) {
887 return result;
888 }
889
890 if (bad > 0) {
891 return ErrorAbort(state, "%s: some changes failed", name);
892 }
893
894 return StringValue(strdup(""));
895}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700896
Doug Zongker512536a2010-02-17 16:11:44 -0800897Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700898 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700899 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700900 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700901 char* key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700902 if (key == NULL) return NULL;
903
904 char value[PROPERTY_VALUE_MAX];
905 property_get(key, value, "");
906 free(key);
907
Doug Zongker512536a2010-02-17 16:11:44 -0800908 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700909}
910
911
Doug Zongker47cace92009-06-18 10:11:50 -0700912// file_getprop(file, key)
913//
914// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700915// per line. # comment lines,blank lines, lines without '=' ignored),
916// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800917Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700918 char* result = NULL;
919 char* buffer = NULL;
920 char* filename;
921 char* key;
922 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
923 return NULL;
924 }
925
926 struct stat st;
927 if (stat(filename, &st) < 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700928 ErrorAbort(state, "%s: failed to stat \"%s\": %s", name, filename, strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700929 goto done;
930 }
931
932#define MAX_FILE_GETPROP_SIZE 65536
933
934 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700935 ErrorAbort(state, "%s too large for %s (max %d)", filename, name, MAX_FILE_GETPROP_SIZE);
Doug Zongker47cace92009-06-18 10:11:50 -0700936 goto done;
937 }
938
Tao Baoba9a42a2015-06-23 23:23:33 -0700939 buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700940 if (buffer == NULL) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700941 ErrorAbort(state, "%s: failed to alloc %zu bytes", name,
942 static_cast<size_t>(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700943 goto done;
944 }
945
Tao Baoba9a42a2015-06-23 23:23:33 -0700946 FILE* f;
Jed Estepa7b9a462015-12-15 16:04:53 -0800947 f = ota_fopen(filename, "rb");
Doug Zongker47cace92009-06-18 10:11:50 -0700948 if (f == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700949 ErrorAbort(state, "%s: failed to open %s: %s", name, filename, strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700950 goto done;
951 }
952
Jed Estepa7b9a462015-12-15 16:04:53 -0800953 if (ota_fread(buffer, 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700954 ErrorAbort(state, "%s: failed to read %zu bytes from %s",
955 name, static_cast<size_t>(st.st_size), filename);
Jed Estepa7b9a462015-12-15 16:04:53 -0800956 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -0700957 goto done;
958 }
959 buffer[st.st_size] = '\0';
960
Jed Estepa7b9a462015-12-15 16:04:53 -0800961 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -0700962
Tao Baoba9a42a2015-06-23 23:23:33 -0700963 char* line;
964 line = strtok(buffer, "\n");
Doug Zongker47cace92009-06-18 10:11:50 -0700965 do {
966 // skip whitespace at start of line
967 while (*line && isspace(*line)) ++line;
968
969 // comment or blank line: skip to next line
970 if (*line == '\0' || *line == '#') continue;
971
972 char* equal = strchr(line, '=');
973 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700974 continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700975 }
976
977 // trim whitespace between key and '='
978 char* key_end = equal-1;
979 while (key_end > line && isspace(*key_end)) --key_end;
980 key_end[1] = '\0';
981
982 // not the key we're looking for
983 if (strcmp(key, line) != 0) continue;
984
985 // skip whitespace after the '=' to the start of the value
986 char* val_start = equal+1;
987 while(*val_start && isspace(*val_start)) ++val_start;
988
989 // trim trailing whitespace
990 char* val_end = val_start + strlen(val_start)-1;
991 while (val_end > val_start && isspace(*val_end)) --val_end;
992 val_end[1] = '\0';
993
994 result = strdup(val_start);
995 break;
996
997 } while ((line = strtok(NULL, "\n")));
998
999 if (result == NULL) result = strdup("");
1000
1001 done:
1002 free(filename);
1003 free(key);
1004 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001005 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001006}
1007
Doug Zongker8edb00c2009-06-11 17:21:44 -07001008// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001009Value* ApplyPatchSpaceFn(const char* name, State* state,
1010 int argc, Expr* argv[]) {
1011 char* bytes_str;
1012 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1013 return NULL;
1014 }
1015
Tao Baob15fd222015-09-24 11:10:51 -07001016 size_t bytes;
1017 if (!android::base::ParseUint(bytes_str, &bytes)) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001018 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n", name, bytes_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001019 free(bytes_str);
Tao Baob15fd222015-09-24 11:10:51 -07001020 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001021 }
1022
1023 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1024}
1025
Doug Zongker52b40362014-02-10 15:30:30 -08001026// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1027
Doug Zongker512536a2010-02-17 16:11:44 -08001028Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001029 if (argc < 6 || (argc % 2) == 1) {
1030 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1031 "even number, got %d",
1032 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001033 }
1034
Doug Zongkerc4351c72010-02-22 14:46:32 -08001035 char* source_filename;
1036 char* target_filename;
1037 char* target_sha1;
1038 char* target_size_str;
1039 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1040 &target_sha1, &target_size_str) < 0) {
1041 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001042 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001043
Tao Baob15fd222015-09-24 11:10:51 -07001044 size_t target_size;
1045 if (!android::base::ParseUint(target_size_str, &target_size)) {
1046 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count", name, target_size_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001047 free(source_filename);
1048 free(target_filename);
1049 free(target_sha1);
1050 free(target_size_str);
Tao Baob15fd222015-09-24 11:10:51 -07001051 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001052 }
1053
1054 int patchcount = (argc-4) / 2;
Yabin Cui64be2132016-02-03 18:16:02 -08001055 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc-4, argv+4),
1056 free);
1057 if (!arg_values) {
1058 return nullptr;
1059 }
1060 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patch_shas;
1061 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patches;
1062 // Protect values by unique_ptrs first to get rid of memory leak.
1063 for (int i = 0; i < patchcount * 2; i += 2) {
1064 patch_shas.emplace_back(arg_values.get()[i], FreeValue);
1065 patches.emplace_back(arg_values.get()[i+1], FreeValue);
1066 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001067
Yabin Cui64be2132016-02-03 18:16:02 -08001068 for (int i = 0; i < patchcount; ++i) {
1069 if (patch_shas[i]->type != VAL_STRING) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001070 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001071 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001072 }
Yabin Cui64be2132016-02-03 18:16:02 -08001073 if (patches[i]->type != VAL_BLOB) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001074 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001075 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001076 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001077 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001078
Yabin Cui64be2132016-02-03 18:16:02 -08001079 std::vector<char*> patch_sha_str;
1080 std::vector<Value*> patch_ptrs;
1081 for (int i = 0; i < patchcount; ++i) {
1082 patch_sha_str.push_back(patch_shas[i]->data);
1083 patch_ptrs.push_back(patches[i].get());
Doug Zongker8edb00c2009-06-11 17:21:44 -07001084 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001085
1086 int result = applypatch(source_filename, target_filename,
1087 target_sha1, target_size,
Yabin Cui64be2132016-02-03 18:16:02 -08001088 patchcount, patch_sha_str.data(), patch_ptrs.data(), NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001089
1090 return StringValue(strdup(result == 0 ? "t" : ""));
1091}
1092
1093// apply_patch_check(file, [sha1_1, ...])
1094Value* ApplyPatchCheckFn(const char* name, State* state,
1095 int argc, Expr* argv[]) {
1096 if (argc < 1) {
1097 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1098 name, argc);
1099 }
1100
1101 char* filename;
1102 if (ReadArgs(state, argv, 1, &filename) < 0) {
1103 return NULL;
1104 }
1105
1106 int patchcount = argc-1;
1107 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1108
1109 int result = applypatch_check(filename, patchcount, sha1s);
1110
1111 int i;
1112 for (i = 0; i < patchcount; ++i) {
1113 free(sha1s[i]);
1114 }
1115 free(sha1s);
1116
1117 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001118}
1119
Tao Bao1107d962015-09-09 17:16:55 -07001120// This is the updater side handler for ui_print() in edify script. Contents
1121// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001122Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001123 char** args = ReadVarArgs(state, argc, argv);
1124 if (args == NULL) {
1125 return NULL;
1126 }
1127
Tao Bao1107d962015-09-09 17:16:55 -07001128 std::string buffer;
1129 for (int i = 0; i < argc; ++i) {
1130 buffer += args[i];
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001131 free(args[i]);
1132 }
1133 free(args);
Tao Bao1107d962015-09-09 17:16:55 -07001134
1135 buffer += "\n";
Michael Runged4a63422014-10-22 19:48:41 -07001136 uiPrint(state, buffer);
Tao Bao1107d962015-09-09 17:16:55 -07001137 return StringValue(strdup(buffer.c_str()));
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001138}
1139
Doug Zongkerd0181b82011-10-19 10:51:12 -07001140Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1141 if (argc != 0) {
1142 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1143 }
1144 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1145 return StringValue(strdup("t"));
1146}
1147
Doug Zongker512536a2010-02-17 16:11:44 -08001148Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001149 if (argc < 1) {
1150 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1151 }
1152 char** args = ReadVarArgs(state, argc, argv);
1153 if (args == NULL) {
1154 return NULL;
1155 }
1156
Tao Baoba9a42a2015-06-23 23:23:33 -07001157 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001158 memcpy(args2, args, sizeof(char*) * argc);
1159 args2[argc] = NULL;
1160
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001161 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001162
1163 pid_t child = fork();
1164 if (child == 0) {
1165 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001166 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001167 _exit(1);
1168 }
1169 int status;
1170 waitpid(child, &status, 0);
1171 if (WIFEXITED(status)) {
1172 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001173 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001174 WEXITSTATUS(status));
1175 }
1176 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001177 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001178 WTERMSIG(status));
1179 }
1180
1181 int i;
1182 for (i = 0; i < argc; ++i) {
1183 free(args[i]);
1184 }
1185 free(args);
1186 free(args2);
1187
1188 char buffer[20];
1189 sprintf(buffer, "%d", status);
1190
Doug Zongker512536a2010-02-17 16:11:44 -08001191 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001192}
1193
Doug Zongker512536a2010-02-17 16:11:44 -08001194// sha1_check(data)
1195// to return the sha1 of the data (given in the format returned by
1196// read_file).
1197//
1198// sha1_check(data, sha1_hex, [sha1_hex, ...])
1199// returns the sha1 of the file if it matches any of the hex
1200// strings passed, or "" if it does not equal any of them.
1201//
1202Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1203 if (argc < 1) {
1204 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1205 }
1206
Yabin Cui64be2132016-02-03 18:16:02 -08001207 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc, argv), free);
1208 if (arg_values == nullptr) {
1209 return nullptr;
1210 }
1211 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> args;
1212 for (int i = 0; i < argc; ++i) {
1213 args.emplace_back(arg_values.get()[i], FreeValue);
Doug Zongker512536a2010-02-17 16:11:44 -08001214 }
1215
1216 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001217 return StringValue(strdup(""));
1218 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001219 uint8_t digest[SHA_DIGEST_LENGTH];
1220 SHA1(reinterpret_cast<uint8_t*>(args[0]->data), args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001221
1222 if (argc == 1) {
1223 return StringValue(PrintSha1(digest));
1224 }
1225
1226 int i;
Yabin Cui64be2132016-02-03 18:16:02 -08001227 uint8_t arg_digest[SHA_DIGEST_LENGTH];
Doug Zongker512536a2010-02-17 16:11:44 -08001228 for (i = 1; i < argc; ++i) {
1229 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001230 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001231 name, i);
1232 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1233 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001234 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1235 name, args[i]->data);
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001236 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001237 break;
1238 }
Doug Zongker512536a2010-02-17 16:11:44 -08001239 }
1240 if (i >= argc) {
1241 // Didn't match any of the hex strings; return false.
1242 return StringValue(strdup(""));
1243 }
Yabin Cui64be2132016-02-03 18:16:02 -08001244 // Found a match.
1245 return args[i].release();
Doug Zongker512536a2010-02-17 16:11:44 -08001246}
1247
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001248// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001249// is actually a FileContents*).
1250Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1251 if (argc != 1) {
1252 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1253 }
1254 char* filename;
1255 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1256
Yabin Cuid6c93af2016-02-10 16:41:10 -08001257 Value* v = static_cast<Value*>(malloc(sizeof(Value)));
1258 if (v == nullptr) {
1259 return nullptr;
1260 }
Doug Zongker512536a2010-02-17 16:11:44 -08001261 v->type = VAL_BLOB;
Yabin Cuid6c93af2016-02-10 16:41:10 -08001262 v->size = -1;
1263 v->data = nullptr;
Doug Zongker512536a2010-02-17 16:11:44 -08001264
1265 FileContents fc;
Tao Baoefacd802016-06-11 03:56:38 -07001266 if (LoadFileContents(filename, &fc) == 0) {
Yabin Cuid6c93af2016-02-10 16:41:10 -08001267 v->data = static_cast<char*>(malloc(fc.data.size()));
1268 if (v->data != nullptr) {
1269 memcpy(v->data, fc.data.data(), fc.data.size());
1270 v->size = fc.data.size();
1271 }
Doug Zongker512536a2010-02-17 16:11:44 -08001272 }
Doug Zongker512536a2010-02-17 16:11:44 -08001273 free(filename);
1274 return v;
1275}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001276
Doug Zongkerc87bab12013-11-25 13:53:25 -08001277// Immediately reboot the device. Recovery is not finished normally,
1278// so if you reboot into recovery it will re-start applying the
1279// current package (because nothing has cleared the copy of the
1280// arguments stored in the BCB).
1281//
1282// The argument is the partition name passed to the android reboot
1283// property. It can be "recovery" to boot from the recovery
1284// partition, or "" (empty string) to boot from the regular boot
1285// partition.
1286Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1287 if (argc != 2) {
1288 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1289 }
1290
1291 char* filename;
1292 char* property;
1293 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1294
1295 char buffer[80];
1296
1297 // zero out the 'command' field of the bootloader message.
1298 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
Jed Estepa7b9a462015-12-15 16:04:53 -08001299 FILE* f = ota_fopen(filename, "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001300 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001301 ota_fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1302 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001303 free(filename);
1304
1305 strcpy(buffer, "reboot,");
1306 if (property != NULL) {
1307 strncat(buffer, property, sizeof(buffer)-10);
1308 }
1309
1310 property_set(ANDROID_RB_PROPERTY, buffer);
1311
1312 sleep(5);
1313 free(property);
1314 ErrorAbort(state, "%s() failed to reboot", name);
1315 return NULL;
1316}
1317
1318// Store a string value somewhere that future invocations of recovery
1319// can access it. This value is called the "stage" and can be used to
1320// drive packages that need to do reboots in the middle of
1321// installation and keep track of where they are in the multi-stage
1322// install.
1323//
1324// The first argument is the block device for the misc partition
1325// ("/misc" in the fstab), which is where this value is stored. The
1326// second argument is the string to store; it should not exceed 31
1327// bytes.
1328Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1329 if (argc != 2) {
1330 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1331 }
1332
1333 char* filename;
1334 char* stagestr;
1335 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1336
1337 // Store this value in the misc partition, immediately after the
1338 // bootloader message that the main recovery uses to save its
1339 // arguments in case of the device restarting midway through
1340 // package installation.
Jed Estepa7b9a462015-12-15 16:04:53 -08001341 FILE* f = ota_fopen(filename, "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001342 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1343 int to_write = strlen(stagestr)+1;
1344 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1345 if (to_write > max_size) {
1346 to_write = max_size;
1347 stagestr[max_size-1] = 0;
1348 }
Jed Estepa7b9a462015-12-15 16:04:53 -08001349 ota_fwrite(stagestr, to_write, 1, f);
1350 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001351
1352 free(stagestr);
1353 return StringValue(filename);
1354}
1355
1356// Return the value most recently saved with SetStageFn. The argument
1357// is the block device for the misc partition.
1358Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001359 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001360 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1361 }
1362
1363 char* filename;
1364 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1365
1366 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
Jed Estepa7b9a462015-12-15 16:04:53 -08001367 FILE* f = ota_fopen(filename, "rb");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001368 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001369 ota_fread(buffer, sizeof(buffer), 1, f);
1370 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001371 buffer[sizeof(buffer)-1] = '\0';
1372
1373 return StringValue(strdup(buffer));
1374}
1375
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001376Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1377 if (argc != 2) {
1378 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1379 }
1380
1381 char* filename;
1382 char* len_str;
1383 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1384
Tao Baob15fd222015-09-24 11:10:51 -07001385 size_t len;
1386 android::base::ParseUint(len_str, &len);
Jed Estepa7b9a462015-12-15 16:04:53 -08001387 int fd = ota_open(filename, O_WRONLY, 0644);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001388 int success = wipe_block_device(fd, len);
1389
1390 free(filename);
1391 free(len_str);
1392
Jed Estepa7b9a462015-12-15 16:04:53 -08001393 ota_close(fd);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001394
1395 return StringValue(strdup(success ? "t" : ""));
1396}
1397
Doug Zongkerc704e062014-05-23 08:40:35 -07001398Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1399 if (argc != 0) {
1400 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1401 }
1402 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1403 fprintf(ui->cmd_pipe, "enable_reboot\n");
1404 return StringValue(strdup("t"));
1405}
1406
Michael Rungeacf47db2014-11-21 00:12:28 -08001407Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1408 if (argc == 0) {
1409 return ErrorAbort(state, "%s() expects args, got %d", name, argc);
1410 }
1411
1412 char** args = ReadVarArgs(state, argc, argv);
1413 if (args == NULL) {
1414 return ErrorAbort(state, "%s() could not read args", name);
1415 }
1416
Tao Baoba9a42a2015-06-23 23:23:33 -07001417 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Michael Rungeacf47db2014-11-21 00:12:28 -08001418 // Tune2fs expects the program name as its args[0]
1419 args2[0] = strdup(name);
Tao Baoba9a42a2015-06-23 23:23:33 -07001420 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001421 args2[i + 1] = args[i];
1422 }
1423 int result = tune2fs_main(argc + 1, args2);
Tao Baoba9a42a2015-06-23 23:23:33 -07001424 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001425 free(args[i]);
1426 }
1427 free(args);
1428
1429 free(args2[0]);
1430 free(args2);
1431 if (result != 0) {
1432 return ErrorAbort(state, "%s() returned error code %d", name, result);
1433 }
1434 return StringValue(strdup("t"));
1435}
1436
Doug Zongker9931f7f2009-06-10 14:11:53 -07001437void RegisterInstallFunctions() {
1438 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001439 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001440 RegisterFunction("unmount", UnmountFn);
1441 RegisterFunction("format", FormatFn);
1442 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001443 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001444 RegisterFunction("delete", DeleteFn);
1445 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001446 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1447 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001448 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001449
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001450 // Usage:
1451 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1452 // Example:
1453 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1454 RegisterFunction("set_metadata", SetMetadataFn);
1455
1456 // Usage:
1457 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1458 // Example:
1459 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1460 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1461
Doug Zongker8edb00c2009-06-11 17:21:44 -07001462 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001463 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001464
1465 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001466 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1467 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001468
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001469 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001470
Doug Zongker512536a2010-02-17 16:11:44 -08001471 RegisterFunction("read_file", ReadFileFn);
1472 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001473 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001474
Doug Zongkerd0181b82011-10-19 10:51:12 -07001475 RegisterFunction("wipe_cache", WipeCacheFn);
1476
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001477 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001478
1479 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001480
1481 RegisterFunction("reboot_now", RebootNowFn);
1482 RegisterFunction("get_stage", GetStageFn);
1483 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001484
1485 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001486 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001487}