blob: 86cdd5d3236cfcc3ce75f43a95c4db8e95f5202f [file] [log] [blame]
Doug Zongker9931f7f2009-06-10 14:11:53 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tao Bao0c7839a2016-10-10 15:48:37 -070017#include "updater/install.h"
18
Doug Zongkerfbf3c102009-06-24 09:36:20 -070019#include <ctype.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070020#include <errno.h>
21#include <stdarg.h>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070022#include <stdio.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070023#include <stdlib.h>
24#include <string.h>
25#include <sys/mount.h>
26#include <sys/stat.h>
27#include <sys/types.h>
Doug Zongkera3f89ea2009-09-10 14:10:48 -070028#include <sys/wait.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070029#include <unistd.h>
Hristo Bojinovdb314d62010-08-02 10:29:49 -070030#include <fcntl.h>
31#include <time.h>
Nick Kralevich5dbdef02013-09-07 14:41:06 -070032#include <ftw.h>
33#include <sys/capability.h>
34#include <sys/xattr.h>
35#include <linux/xattr.h>
36#include <inttypes.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070037
Yabin Cui64be2132016-02-03 18:16:02 -080038#include <memory>
39#include <vector>
40
Elliott Hughes4b166f02015-12-04 15:30:20 -080041#include <android-base/parseint.h>
Elliott Hughescb220402016-09-23 15:30:55 -070042#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080043#include <android-base/strings.h>
44#include <android-base/stringprintf.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070045#include <cutils/android_reboot.h>
Tao Baode40ba52016-10-05 23:17:01 -070046#include <ext4_utils/make_ext4fs.h>
47#include <ext4_utils/wipe.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070048#include <selinux/label.h>
49#include <selinux/selinux.h>
Tao Bao1107d962015-09-09 17:16:55 -070050
Doug Zongkerc87bab12013-11-25 13:53:25 -080051#include "applypatch/applypatch.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070052#include "bootloader.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070053#include "edify/expr.h"
Tianjie Xu16255832016-04-30 11:49:59 -070054#include "error_code.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070055#include "minzip/DirUtil.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070056#include "mounts.h"
Tianjie Xu16255832016-04-30 11:49:59 -070057#include "openssl/sha.h"
Jed Estep39c1b5e2015-12-15 16:04:53 -080058#include "ota_io.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080059#include "tune2fs.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070060#include "updater/updater.h"
Doug Zongker8edb00c2009-06-11 17:21: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
Tao Bao0c7839a2016-10-10 15:48:37 -070093// Create all parent directories of name, if necessary.
94static int make_parents(char* name) {
95 char* p;
96 for (p = name + (strlen(name)-1); p > name; --p) {
97 if (*p != '/') continue;
98 *p = '\0';
99 if (make_parents(name) < 0) return -1;
100 int result = mkdir(name, 0700);
101 if (result == 0) printf("created [%s]\n", name);
102 *p = '/';
103 if (result == 0 || errno == EEXIST) {
104 // successfully created or already existed; we're done
105 return 0;
106 } else {
107 printf("failed to mkdir %s: %s\n", name, strerror(errno));
108 return -1;
109 }
110 }
111 return 0;
112}
113
Doug Zongker52b40362014-02-10 15:30:30 -0800114// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700115char* PrintSha1(const uint8_t* digest) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800116 char* buffer = reinterpret_cast<char*>(malloc(SHA_DIGEST_LENGTH*2 + 1));
Doug Zongker52b40362014-02-10 15:30:30 -0800117 const char* alphabet = "0123456789abcdef";
Tao Baoba9a42a2015-06-23 23:23:33 -0700118 size_t i;
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800119 for (i = 0; i < SHA_DIGEST_LENGTH; ++i) {
Doug Zongker52b40362014-02-10 15:30:30 -0800120 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
121 buffer[i*2+1] = alphabet[digest[i] & 0xf];
122 }
123 buffer[i*2] = '\0';
124 return buffer;
125}
126
Doug Zongker3d177d02010-07-01 09:18:44 -0700127// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700128//
Doug Zongker3d177d02010-07-01 09:18:44 -0700129// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800130Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700131 char* result = NULL;
Michael Runge168f7772014-10-22 17:05:08 -0700132 if (argc != 4 && argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700133 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700134 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700135 char* fs_type;
136 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700137 char* location;
138 char* mount_point;
Michael Runge168f7772014-10-22 17:05:08 -0700139 char* mount_options;
140 bool has_mount_options;
141 if (argc == 5) {
142 has_mount_options = true;
143 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
144 &location, &mount_point, &mount_options) < 0) {
145 return NULL;
146 }
147 } else {
148 has_mount_options = false;
149 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700150 &location, &mount_point) < 0) {
Michael Runge168f7772014-10-22 17:05:08 -0700151 return NULL;
152 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700153 }
154
Doug Zongker3d177d02010-07-01 09:18:44 -0700155 if (strlen(fs_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700156 ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700157 goto done;
158 }
159 if (strlen(partition_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700160 ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
Doug Zongker3d177d02010-07-01 09:18:44 -0700161 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700162 goto done;
163 }
164 if (strlen(location) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700165 ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700166 goto done;
167 }
168 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700169 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
170 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700171 goto done;
172 }
173
Tao Baoba9a42a2015-06-23 23:23:33 -0700174 {
175 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500176
Tao Baoba9a42a2015-06-23 23:23:33 -0700177 if (sehandle) {
178 selabel_lookup(sehandle, &secontext, mount_point, 0755);
179 setfscreatecon(secontext);
180 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500181
Tao Baoba9a42a2015-06-23 23:23:33 -0700182 mkdir(mount_point, 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700183
Tao Baoba9a42a2015-06-23 23:23:33 -0700184 if (secontext) {
185 freecon(secontext);
186 setfscreatecon(NULL);
187 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500188 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500189
Elliott Hughes63a31922016-06-09 17:41:22 -0700190 if (mount(location, mount_point, fs_type,
191 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
192 has_mount_options ? mount_options : "") < 0) {
193 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
194 name, location, mount_point, strerror(errno));
195 result = strdup("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700196 } else {
Elliott Hughes63a31922016-06-09 17:41:22 -0700197 result = mount_point;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700198 }
199
200done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700201 free(fs_type);
202 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700203 free(location);
204 if (result != mount_point) free(mount_point);
Michael Runge168f7772014-10-22 17:05:08 -0700205 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800206 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700207}
208
Doug Zongker8edb00c2009-06-11 17:21:44 -0700209
210// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800211Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700212 char* result = NULL;
213 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700214 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700215 }
216 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700217 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700218 return NULL;
219 }
220 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700221 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700222 goto done;
223 }
224
225 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700226 {
Elliott Hughes63a31922016-06-09 17:41:22 -0700227 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
Tao Baoba9a42a2015-06-23 23:23:33 -0700228 if (vol == NULL) {
229 result = strdup("");
230 } else {
231 result = mount_point;
232 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700233 }
234
235done:
236 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800237 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700238}
239
240
Doug Zongker512536a2010-02-17 16:11:44 -0800241Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700242 char* result = NULL;
243 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700244 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700245 }
246 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700247 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700248 return NULL;
249 }
250 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700251 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700252 goto done;
253 }
254
255 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700256 {
Elliott Hughes63a31922016-06-09 17:41:22 -0700257 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
Tao Baoba9a42a2015-06-23 23:23:33 -0700258 if (vol == NULL) {
259 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
260 result = strdup("");
261 } else {
262 int ret = unmount_mounted_volume(vol);
263 if (ret != 0) {
264 uiPrintf(state, "unmount of %s failed (%d): %s\n",
265 mount_point, ret, strerror(errno));
266 }
267 result = mount_point;
Michael Runge5ddf4292014-10-24 14:14:41 -0700268 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700269 }
270
271done:
272 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800273 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700274}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700275
JP Abgrall37aedb32014-06-16 19:07:39 -0700276static int exec_cmd(const char* path, char* const argv[]) {
277 int status;
278 pid_t child;
279 if ((child = vfork()) == 0) {
280 execv(path, argv);
281 _exit(-1);
282 }
283 waitpid(child, &status, 0);
284 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
285 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
286 }
287 return WEXITSTATUS(status);
288}
289
Doug Zongker8edb00c2009-06-11 17:21:44 -0700290
Stephen Smalley779701d2012-02-09 14:13:23 -0500291// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700292//
Stephen Smalley779701d2012-02-09 14:13:23 -0500293// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700294// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
295// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800296// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700297// 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 -0800298Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700299 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400300 if (argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700301 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700302 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700303 char* fs_type;
304 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700305 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800306 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400307 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500308
Stephen Smalley779701d2012-02-09 14:13:23 -0500309 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
310 return NULL;
311 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700312
Doug Zongker3d177d02010-07-01 09:18:44 -0700313 if (strlen(fs_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700314 ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700315 goto done;
316 }
317 if (strlen(partition_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700318 ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
Doug Zongker3d177d02010-07-01 09:18:44 -0700319 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700320 goto done;
321 }
322 if (strlen(location) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700323 ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700324 goto done;
325 }
326
Stephen Smalley516e4e22012-04-03 13:35:11 -0400327 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700328 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
329 name);
Stephen Smalley779701d2012-02-09 14:13:23 -0500330 goto done;
331 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500332
Elliott Hughes63a31922016-06-09 17:41:22 -0700333 if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500334 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700335 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700336 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700337 name, status, location);
338 result = strdup("");
339 goto done;
340 }
341 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700342 } else if (strcmp(fs_type, "f2fs") == 0) {
343 char *num_sectors;
344 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
345 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
346 result = strdup("");
347 goto done;
348 }
349 const char *f2fs_path = "/sbin/mkfs.f2fs";
350 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
351 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
352 free(num_sectors);
353 if (status != 0) {
354 printf("%s: mkfs.f2fs failed (%d) on %s",
355 name, status, location);
356 result = strdup("");
357 goto done;
358 }
359 result = location;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700360 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700361 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700362 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700363 }
364
365done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700366 free(fs_type);
367 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700368 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800369 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700370}
371
Michael Rungece7ca712013-11-06 17:42:20 -0800372Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
373 char* result = NULL;
374 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700375 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Michael Rungece7ca712013-11-06 17:42:20 -0800376 }
377
378 char* src_name;
379 char* dst_name;
380
381 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
382 return NULL;
383 }
384 if (strlen(src_name) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700385 ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800386 goto done;
387 }
388 if (strlen(dst_name) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700389 ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800390 goto done;
391 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700392 if (make_parents(dst_name) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700393 ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700394 dst_name, strerror(errno));
Michael Runge2f0ef732014-10-22 14:28:23 -0700395 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
396 // File was already moved
397 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700398 } else if (rename(src_name, dst_name) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700399 ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800400 src_name, dst_name, strerror(errno));
401 } else {
402 result = dst_name;
403 }
404
405done:
406 free(src_name);
407 if (result != dst_name) free(dst_name);
408 return StringValue(result);
409}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700410
Doug Zongker512536a2010-02-17 16:11:44 -0800411Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700412 char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
413 for (int i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700414 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700415 if (paths[i] == NULL) {
Yabin Cui64be2132016-02-03 18:16:02 -0800416 for (int j = 0; j < i; ++j) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700417 free(paths[j]);
418 }
419 free(paths);
420 return NULL;
421 }
422 }
423
424 bool recursive = (strcmp(name, "delete_recursive") == 0);
425
426 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700427 for (int i = 0; i < argc; ++i) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700428 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
429 ++success;
430 free(paths[i]);
431 }
432 free(paths);
433
434 char buffer[10];
435 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800436 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700437}
438
Doug Zongker8edb00c2009-06-11 17:21:44 -0700439
Doug Zongker512536a2010-02-17 16:11:44 -0800440Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700441 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700442 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700443 }
444 char* frac_str;
445 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700446 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700447 return NULL;
448 }
449
450 double frac = strtod(frac_str, NULL);
Tao Baob15fd222015-09-24 11:10:51 -0700451 int sec;
452 android::base::ParseInt(sec_str, &sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700453
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700454 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700455 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
456
Doug Zongker9931f7f2009-06-10 14:11:53 -0700457 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800458 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700459}
460
Doug Zongker512536a2010-02-17 16:11:44 -0800461Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700462 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700463 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700464 }
465 char* frac_str;
466 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
467 return NULL;
468 }
469
470 double frac = strtod(frac_str, NULL);
471
472 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
473 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
474
Doug Zongker512536a2010-02-17 16:11:44 -0800475 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700476}
477
Doug Zongker8edb00c2009-06-11 17:21:44 -0700478// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800479Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700480 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700481 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700482 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700483 }
484 char* zip_path;
485 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700486 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700487
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700488 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700489
490 // To create a consistent system image, never use the clock for timestamps.
491 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
492
493 bool success = mzExtractRecursive(za, zip_path, dest_path,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000494 &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500495 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700496 free(zip_path);
497 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800498 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700499}
500
Doug Zongker8edb00c2009-06-11 17:21:44 -0700501
502// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800503// or
504// package_extract_file(package_path)
505// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800506// function (the char* returned is actually a FileContents*).
507Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700508 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700509 if (argc < 1 || argc > 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700510 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800511 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700512 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700513 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700514
Doug Zongker5f875bf2014-08-22 14:53:43 -0700515 if (argc == 2) {
516 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800517
518 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700519
Doug Zongker6aece332010-02-01 14:40:12 -0800520 char* zip_path;
521 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700522 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800523
Doug Zongker6aece332010-02-01 14:40:12 -0800524 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
525 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700526 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800527 goto done2;
528 }
529
Tao Baoba9a42a2015-06-23 23:23:33 -0700530 {
Alistair Strachan733285f2016-05-05 16:04:32 -0700531 int fd = TEMP_FAILURE_RETRY(ota_open(dest_path, O_WRONLY | O_CREAT | O_TRUNC,
Tao Baod3cac342015-12-14 15:53:25 -0800532 S_IRUSR | S_IWUSR));
533 if (fd == -1) {
534 printf("%s: can't open %s for write: %s\n", name, dest_path, strerror(errno));
Tao Baoba9a42a2015-06-23 23:23:33 -0700535 goto done2;
536 }
Tao Baod3cac342015-12-14 15:53:25 -0800537 success = mzExtractZipEntryToFile(za, entry, fd);
Jed Estepa7b9a462015-12-15 16:04:53 -0800538 if (ota_fsync(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800539 printf("fsync of \"%s\" failed: %s\n", dest_path, strerror(errno));
540 success = false;
541 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800542 if (ota_close(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800543 printf("close of \"%s\" failed: %s\n", dest_path, strerror(errno));
544 success = false;
545 }
Doug Zongker6aece332010-02-01 14:40:12 -0800546 }
Doug Zongker6aece332010-02-01 14:40:12 -0800547
548 done2:
549 free(zip_path);
550 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800551 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800552 } else {
553 // The one-argument version returns the contents of the file
554 // as the result.
555
556 char* zip_path;
Yabin Cui64be2132016-02-03 18:16:02 -0800557 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
558
Tao Baoba9a42a2015-06-23 23:23:33 -0700559 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -0800560 v->type = VAL_BLOB;
561 v->size = -1;
562 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800563
Doug Zongker6aece332010-02-01 14:40:12 -0800564 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
565 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
566 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700567 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800568 goto done1;
569 }
570
Doug Zongker512536a2010-02-17 16:11:44 -0800571 v->size = mzGetZipEntryUncompLen(entry);
Tao Baoba9a42a2015-06-23 23:23:33 -0700572 v->data = reinterpret_cast<char*>(malloc(v->size));
Doug Zongker512536a2010-02-17 16:11:44 -0800573 if (v->data == NULL) {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700574 printf("%s: failed to allocate %zd bytes for %s\n",
575 name, v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800576 goto done1;
577 }
578
Doug Zongker512536a2010-02-17 16:11:44 -0800579 success = mzExtractZipEntryToBuffer(za, entry,
580 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800581
582 done1:
583 free(zip_path);
584 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800585 free(v->data);
586 v->data = NULL;
587 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800588 }
Doug Zongker512536a2010-02-17 16:11:44 -0800589 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700590 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700591}
592
Doug Zongker9931f7f2009-06-10 14:11:53 -0700593// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700594// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800595Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700596 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700597 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700598 }
599 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700600 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700601 if (target == NULL) return NULL;
602
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700603 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700604 if (srcs == NULL) {
605 free(target);
606 return NULL;
607 }
608
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700609 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700610 int i;
611 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700612 if (unlink(srcs[i]) < 0) {
613 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700614 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700615 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700616 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700617 }
618 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700619 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700620 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700621 name, srcs[i], target);
622 ++bad;
623 }
Doug Zongker60babf82009-09-18 15:11:24 -0700624 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700625 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700626 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700627 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700628 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700629 free(srcs[i]);
630 }
631 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700632 if (bad) {
Tianjie Xu16255832016-04-30 11:49:59 -0700633 return ErrorAbort(state, kSymlinkFailure, "%s: some symlinks failed", name);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700634 }
Doug Zongker512536a2010-02-17 16:11:44 -0800635 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700636}
637
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700638struct perm_parsed_args {
639 bool has_uid;
640 uid_t uid;
641 bool has_gid;
642 gid_t gid;
643 bool has_mode;
644 mode_t mode;
645 bool has_fmode;
646 mode_t fmode;
647 bool has_dmode;
648 mode_t dmode;
649 bool has_selabel;
650 char* selabel;
651 bool has_capabilities;
652 uint64_t capabilities;
653};
654
Michael Runged4a63422014-10-22 19:48:41 -0700655static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700656 int i;
657 struct perm_parsed_args parsed;
658 int bad = 0;
659 static int max_warnings = 20;
660
661 memset(&parsed, 0, sizeof(parsed));
662
663 for (i = 1; i < argc; i += 2) {
664 if (strcmp("uid", args[i]) == 0) {
665 int64_t uid;
666 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
667 parsed.uid = uid;
668 parsed.has_uid = true;
669 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700670 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700671 bad++;
672 }
673 continue;
674 }
675 if (strcmp("gid", args[i]) == 0) {
676 int64_t gid;
677 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
678 parsed.gid = gid;
679 parsed.has_gid = true;
680 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700681 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700682 bad++;
683 }
684 continue;
685 }
686 if (strcmp("mode", args[i]) == 0) {
687 int32_t mode;
688 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
689 parsed.mode = mode;
690 parsed.has_mode = true;
691 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700692 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700693 bad++;
694 }
695 continue;
696 }
697 if (strcmp("dmode", args[i]) == 0) {
698 int32_t mode;
699 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
700 parsed.dmode = mode;
701 parsed.has_dmode = true;
702 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700703 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700704 bad++;
705 }
706 continue;
707 }
708 if (strcmp("fmode", args[i]) == 0) {
709 int32_t mode;
710 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
711 parsed.fmode = mode;
712 parsed.has_fmode = true;
713 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700714 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700715 bad++;
716 }
717 continue;
718 }
719 if (strcmp("capabilities", args[i]) == 0) {
720 int64_t capabilities;
721 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
722 parsed.capabilities = capabilities;
723 parsed.has_capabilities = true;
724 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700725 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700726 bad++;
727 }
728 continue;
729 }
730 if (strcmp("selabel", args[i]) == 0) {
731 if (args[i+1][0] != '\0') {
732 parsed.selabel = args[i+1];
733 parsed.has_selabel = true;
734 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700735 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700736 bad++;
737 }
738 continue;
739 }
740 if (max_warnings != 0) {
741 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
742 max_warnings--;
743 if (max_warnings == 0) {
744 printf("ParsedPermArgs: suppressing further warnings\n");
745 }
746 }
747 }
748 return parsed;
749}
750
751static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700752 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700753 const char* filename,
754 const struct stat *statptr,
755 struct perm_parsed_args parsed)
756{
757 int bad = 0;
758
Nick Kralevich68802412014-10-23 20:36:42 -0700759 if (parsed.has_selabel) {
760 if (lsetfilecon(filename, parsed.selabel) != 0) {
761 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
762 filename, parsed.selabel, strerror(errno));
763 bad++;
764 }
765 }
766
Nick Kraleviche4612512013-09-10 15:34:19 -0700767 /* ignore symlinks */
768 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700769 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700770 }
771
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700772 if (parsed.has_uid) {
773 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700774 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
775 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700776 bad++;
777 }
778 }
779
780 if (parsed.has_gid) {
781 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700782 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
783 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700784 bad++;
785 }
786 }
787
788 if (parsed.has_mode) {
789 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700790 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
791 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700792 bad++;
793 }
794 }
795
796 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
797 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700798 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
799 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700800 bad++;
801 }
802 }
803
804 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
805 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700806 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700807 filename, parsed.fmode, strerror(errno));
808 bad++;
809 }
810 }
811
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700812 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
813 if (parsed.capabilities == 0) {
814 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
815 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700816 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700817 filename, parsed.capabilities, strerror(errno));
818 bad++;
819 }
820 } else {
821 struct vfs_cap_data cap_data;
822 memset(&cap_data, 0, sizeof(cap_data));
823 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
824 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
825 cap_data.data[0].inheritable = 0;
826 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
827 cap_data.data[1].inheritable = 0;
828 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700829 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
830 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700831 bad++;
832 }
833 }
834 }
835
836 return bad;
837}
838
839// nftw doesn't allow us to pass along context, so we need to use
840// global variables. *sigh*
841static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700842static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700843
844static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
845 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700846 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700847}
848
849static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700850 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700851 struct stat sb;
852 Value* result = NULL;
853
854 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
855
856 if ((argc % 2) != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700857 return ErrorAbort(state, kArgsParsingFailure,
858 "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700859 }
860
861 char** args = ReadVarArgs(state, argc, argv);
862 if (args == NULL) return NULL;
863
864 if (lstat(args[0], &sb) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700865 result = ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s",
866 name, args[0], strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700867 goto done;
868 }
869
Tao Baoba9a42a2015-06-23 23:23:33 -0700870 {
871 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700872
Tao Baoba9a42a2015-06-23 23:23:33 -0700873 if (recursive) {
874 recursive_parsed_args = parsed;
875 recursive_state = state;
876 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
877 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
878 recursive_state = NULL;
879 } else {
880 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
881 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700882 }
883
884done:
Tao Baoba9a42a2015-06-23 23:23:33 -0700885 for (int i = 0; i < argc; ++i) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700886 free(args[i]);
887 }
888 free(args);
889
890 if (result != NULL) {
891 return result;
892 }
893
894 if (bad > 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700895 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700896 }
897
898 return StringValue(strdup(""));
899}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700900
Doug Zongker512536a2010-02-17 16:11:44 -0800901Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700902 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700903 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700904 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700905 char* key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700906 if (key == NULL) return NULL;
907
Elliott Hughescb220402016-09-23 15:30:55 -0700908 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700909 free(key);
910
Elliott Hughescb220402016-09-23 15:30:55 -0700911 return StringValue(strdup(value.c_str()));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700912}
913
914
Doug Zongker47cace92009-06-18 10:11:50 -0700915// file_getprop(file, key)
916//
917// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700918// per line. # comment lines,blank lines, lines without '=' ignored),
919// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800920Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700921 char* result = NULL;
922 char* buffer = NULL;
923 char* filename;
924 char* key;
925 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
926 return NULL;
927 }
928
929 struct stat st;
930 if (stat(filename, &st) < 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700931 ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name, filename,
932 strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700933 goto done;
934 }
935
936#define MAX_FILE_GETPROP_SIZE 65536
937
938 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tianjie Xu16255832016-04-30 11:49:59 -0700939 ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %d)", filename, name,
940 MAX_FILE_GETPROP_SIZE);
Doug Zongker47cace92009-06-18 10:11:50 -0700941 goto done;
942 }
943
Tao Baoba9a42a2015-06-23 23:23:33 -0700944 buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700945 if (buffer == NULL) {
Tianjie Xu84478e82016-05-23 11:42:40 -0700946 ErrorAbort(state, kFileGetPropFailure, "%s: failed to alloc %zu bytes", name,
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700947 static_cast<size_t>(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700948 goto done;
949 }
950
Tao Baoba9a42a2015-06-23 23:23:33 -0700951 FILE* f;
Jed Estepa7b9a462015-12-15 16:04:53 -0800952 f = ota_fopen(filename, "rb");
Doug Zongker47cace92009-06-18 10:11:50 -0700953 if (f == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -0700954 ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename,
955 strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700956 goto done;
957 }
958
Jed Estepa7b9a462015-12-15 16:04:53 -0800959 if (ota_fread(buffer, 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
Tianjie Xu84478e82016-05-23 11:42:40 -0700960 ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s",
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700961 name, static_cast<size_t>(st.st_size), filename);
Jed Estepa7b9a462015-12-15 16:04:53 -0800962 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -0700963 goto done;
964 }
965 buffer[st.st_size] = '\0';
966
Jed Estepa7b9a462015-12-15 16:04:53 -0800967 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -0700968
Tao Baoba9a42a2015-06-23 23:23:33 -0700969 char* line;
970 line = strtok(buffer, "\n");
Doug Zongker47cace92009-06-18 10:11:50 -0700971 do {
972 // skip whitespace at start of line
973 while (*line && isspace(*line)) ++line;
974
975 // comment or blank line: skip to next line
976 if (*line == '\0' || *line == '#') continue;
977
978 char* equal = strchr(line, '=');
979 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700980 continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700981 }
982
983 // trim whitespace between key and '='
984 char* key_end = equal-1;
985 while (key_end > line && isspace(*key_end)) --key_end;
986 key_end[1] = '\0';
987
988 // not the key we're looking for
989 if (strcmp(key, line) != 0) continue;
990
991 // skip whitespace after the '=' to the start of the value
992 char* val_start = equal+1;
993 while(*val_start && isspace(*val_start)) ++val_start;
994
995 // trim trailing whitespace
996 char* val_end = val_start + strlen(val_start)-1;
997 while (val_end > val_start && isspace(*val_end)) --val_end;
998 val_end[1] = '\0';
999
1000 result = strdup(val_start);
1001 break;
1002
1003 } while ((line = strtok(NULL, "\n")));
1004
1005 if (result == NULL) result = strdup("");
1006
1007 done:
1008 free(filename);
1009 free(key);
1010 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001011 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001012}
1013
Doug Zongker8edb00c2009-06-11 17:21:44 -07001014// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001015Value* ApplyPatchSpaceFn(const char* name, State* state,
1016 int argc, Expr* argv[]) {
1017 char* bytes_str;
1018 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1019 return NULL;
1020 }
1021
Tao Baob15fd222015-09-24 11:10:51 -07001022 size_t bytes;
1023 if (!android::base::ParseUint(bytes_str, &bytes)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001024 ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n",
1025 name, bytes_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001026 free(bytes_str);
Tao Baob15fd222015-09-24 11:10:51 -07001027 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001028 }
1029
1030 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1031}
1032
Doug Zongker52b40362014-02-10 15:30:30 -08001033// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1034
Doug Zongker512536a2010-02-17 16:11:44 -08001035Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001036 if (argc < 6 || (argc % 2) == 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001037 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an "
1038 "even number, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001039 }
1040
Doug Zongkerc4351c72010-02-22 14:46:32 -08001041 char* source_filename;
1042 char* target_filename;
1043 char* target_sha1;
1044 char* target_size_str;
1045 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1046 &target_sha1, &target_size_str) < 0) {
1047 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001048 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001049
Tao Baob15fd222015-09-24 11:10:51 -07001050 size_t target_size;
1051 if (!android::base::ParseUint(target_size_str, &target_size)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001052 ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count",
1053 name, target_size_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001054 free(source_filename);
1055 free(target_filename);
1056 free(target_sha1);
1057 free(target_size_str);
Tao Baob15fd222015-09-24 11:10:51 -07001058 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001059 }
1060
1061 int patchcount = (argc-4) / 2;
Yabin Cui64be2132016-02-03 18:16:02 -08001062 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc-4, argv+4),
1063 free);
1064 if (!arg_values) {
1065 return nullptr;
1066 }
1067 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patch_shas;
1068 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patches;
1069 // Protect values by unique_ptrs first to get rid of memory leak.
1070 for (int i = 0; i < patchcount * 2; i += 2) {
1071 patch_shas.emplace_back(arg_values.get()[i], FreeValue);
1072 patches.emplace_back(arg_values.get()[i+1], FreeValue);
1073 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001074
Yabin Cui64be2132016-02-03 18:16:02 -08001075 for (int i = 0; i < patchcount; ++i) {
1076 if (patch_shas[i]->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001077 ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001078 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001079 }
Yabin Cui64be2132016-02-03 18:16:02 -08001080 if (patches[i]->type != VAL_BLOB) {
Tianjie Xu16255832016-04-30 11:49:59 -07001081 ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001082 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001083 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001084 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001085
Yabin Cui64be2132016-02-03 18:16:02 -08001086 std::vector<char*> patch_sha_str;
1087 std::vector<Value*> patch_ptrs;
1088 for (int i = 0; i < patchcount; ++i) {
1089 patch_sha_str.push_back(patch_shas[i]->data);
1090 patch_ptrs.push_back(patches[i].get());
Doug Zongker8edb00c2009-06-11 17:21:44 -07001091 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001092
1093 int result = applypatch(source_filename, target_filename,
1094 target_sha1, target_size,
Yabin Cui64be2132016-02-03 18:16:02 -08001095 patchcount, patch_sha_str.data(), patch_ptrs.data(), NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001096
1097 return StringValue(strdup(result == 0 ? "t" : ""));
1098}
1099
1100// apply_patch_check(file, [sha1_1, ...])
1101Value* ApplyPatchCheckFn(const char* name, State* state,
1102 int argc, Expr* argv[]) {
1103 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001104 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %d",
Doug Zongkerc4351c72010-02-22 14:46:32 -08001105 name, argc);
1106 }
1107
1108 char* filename;
1109 if (ReadArgs(state, argv, 1, &filename) < 0) {
1110 return NULL;
1111 }
1112
1113 int patchcount = argc-1;
1114 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1115
1116 int result = applypatch_check(filename, patchcount, sha1s);
1117
1118 int i;
1119 for (i = 0; i < patchcount; ++i) {
1120 free(sha1s[i]);
1121 }
1122 free(sha1s);
1123
1124 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001125}
1126
Tao Bao1107d962015-09-09 17:16:55 -07001127// This is the updater side handler for ui_print() in edify script. Contents
1128// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001129Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001130 char** args = ReadVarArgs(state, argc, argv);
1131 if (args == NULL) {
1132 return NULL;
1133 }
1134
Tao Bao1107d962015-09-09 17:16:55 -07001135 std::string buffer;
1136 for (int i = 0; i < argc; ++i) {
1137 buffer += args[i];
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001138 free(args[i]);
1139 }
1140 free(args);
Tao Bao1107d962015-09-09 17:16:55 -07001141
1142 buffer += "\n";
Michael Runged4a63422014-10-22 19:48:41 -07001143 uiPrint(state, buffer);
Tao Bao1107d962015-09-09 17:16:55 -07001144 return StringValue(strdup(buffer.c_str()));
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001145}
1146
Doug Zongkerd0181b82011-10-19 10:51:12 -07001147Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1148 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001149 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001150 }
1151 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1152 return StringValue(strdup("t"));
1153}
1154
Doug Zongker512536a2010-02-17 16:11:44 -08001155Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001156 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001157 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001158 }
1159 char** args = ReadVarArgs(state, argc, argv);
1160 if (args == NULL) {
1161 return NULL;
1162 }
1163
Tao Baoba9a42a2015-06-23 23:23:33 -07001164 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001165 memcpy(args2, args, sizeof(char*) * argc);
1166 args2[argc] = NULL;
1167
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001168 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001169
1170 pid_t child = fork();
1171 if (child == 0) {
1172 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001173 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001174 _exit(1);
1175 }
1176 int status;
1177 waitpid(child, &status, 0);
1178 if (WIFEXITED(status)) {
1179 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001180 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001181 WEXITSTATUS(status));
1182 }
1183 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001184 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001185 WTERMSIG(status));
1186 }
1187
1188 int i;
1189 for (i = 0; i < argc; ++i) {
1190 free(args[i]);
1191 }
1192 free(args);
1193 free(args2);
1194
1195 char buffer[20];
1196 sprintf(buffer, "%d", status);
1197
Doug Zongker512536a2010-02-17 16:11:44 -08001198 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001199}
1200
Doug Zongker512536a2010-02-17 16:11:44 -08001201// sha1_check(data)
1202// to return the sha1 of the data (given in the format returned by
1203// read_file).
1204//
1205// sha1_check(data, sha1_hex, [sha1_hex, ...])
1206// returns the sha1 of the file if it matches any of the hex
1207// strings passed, or "" if it does not equal any of them.
1208//
1209Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1210 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001211 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongker512536a2010-02-17 16:11:44 -08001212 }
1213
Yabin Cui64be2132016-02-03 18:16:02 -08001214 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc, argv), free);
1215 if (arg_values == nullptr) {
1216 return nullptr;
1217 }
1218 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> args;
1219 for (int i = 0; i < argc; ++i) {
1220 args.emplace_back(arg_values.get()[i], FreeValue);
Doug Zongker512536a2010-02-17 16:11:44 -08001221 }
1222
1223 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001224 return StringValue(strdup(""));
1225 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001226 uint8_t digest[SHA_DIGEST_LENGTH];
1227 SHA1(reinterpret_cast<uint8_t*>(args[0]->data), args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001228
1229 if (argc == 1) {
1230 return StringValue(PrintSha1(digest));
1231 }
1232
1233 int i;
Yabin Cui64be2132016-02-03 18:16:02 -08001234 uint8_t arg_digest[SHA_DIGEST_LENGTH];
Doug Zongker512536a2010-02-17 16:11:44 -08001235 for (i = 1; i < argc; ++i) {
1236 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001237 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001238 name, i);
1239 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1240 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001241 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1242 name, args[i]->data);
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001243 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001244 break;
1245 }
Doug Zongker512536a2010-02-17 16:11:44 -08001246 }
1247 if (i >= argc) {
1248 // Didn't match any of the hex strings; return false.
1249 return StringValue(strdup(""));
1250 }
Yabin Cui64be2132016-02-03 18:16:02 -08001251 // Found a match.
1252 return args[i].release();
Doug Zongker512536a2010-02-17 16:11:44 -08001253}
1254
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001255// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001256// is actually a FileContents*).
1257Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1258 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001259 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker512536a2010-02-17 16:11:44 -08001260 }
1261 char* filename;
1262 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1263
Yabin Cuid6c93af2016-02-10 16:41:10 -08001264 Value* v = static_cast<Value*>(malloc(sizeof(Value)));
1265 if (v == nullptr) {
1266 return nullptr;
1267 }
Doug Zongker512536a2010-02-17 16:11:44 -08001268 v->type = VAL_BLOB;
Yabin Cuid6c93af2016-02-10 16:41:10 -08001269 v->size = -1;
1270 v->data = nullptr;
Doug Zongker512536a2010-02-17 16:11:44 -08001271
1272 FileContents fc;
Tao Baoefacd802016-06-11 03:56:38 -07001273 if (LoadFileContents(filename, &fc) == 0) {
Yabin Cuid6c93af2016-02-10 16:41:10 -08001274 v->data = static_cast<char*>(malloc(fc.data.size()));
1275 if (v->data != nullptr) {
1276 memcpy(v->data, fc.data.data(), fc.data.size());
1277 v->size = fc.data.size();
1278 }
Doug Zongker512536a2010-02-17 16:11:44 -08001279 }
Doug Zongker512536a2010-02-17 16:11:44 -08001280 free(filename);
1281 return v;
1282}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001283
Doug Zongkerc87bab12013-11-25 13:53:25 -08001284// Immediately reboot the device. Recovery is not finished normally,
1285// so if you reboot into recovery it will re-start applying the
1286// current package (because nothing has cleared the copy of the
1287// arguments stored in the BCB).
1288//
1289// The argument is the partition name passed to the android reboot
1290// property. It can be "recovery" to boot from the recovery
1291// partition, or "" (empty string) to boot from the regular boot
1292// partition.
1293Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1294 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001295 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001296 }
1297
1298 char* filename;
1299 char* property;
1300 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1301
Doug Zongkerc87bab12013-11-25 13:53:25 -08001302 // zero out the 'command' field of the bootloader message.
Elliott Hughescb220402016-09-23 15:30:55 -07001303 char buffer[80];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001304 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
Jed Estepa7b9a462015-12-15 16:04:53 -08001305 FILE* f = ota_fopen(filename, "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001306 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001307 ota_fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1308 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001309 free(filename);
1310
Elliott Hughescb220402016-09-23 15:30:55 -07001311 std::string reboot_cmd = "reboot,";
1312 if (property != nullptr) reboot_cmd += property;
1313 android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001314
1315 sleep(5);
1316 free(property);
Tianjie Xu16255832016-04-30 11:49:59 -07001317 ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001318 return NULL;
1319}
1320
1321// Store a string value somewhere that future invocations of recovery
1322// can access it. This value is called the "stage" and can be used to
1323// drive packages that need to do reboots in the middle of
1324// installation and keep track of where they are in the multi-stage
1325// install.
1326//
1327// The first argument is the block device for the misc partition
1328// ("/misc" in the fstab), which is where this value is stored. The
1329// second argument is the string to store; it should not exceed 31
1330// bytes.
1331Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1332 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001333 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001334 }
1335
1336 char* filename;
1337 char* stagestr;
1338 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1339
1340 // Store this value in the misc partition, immediately after the
1341 // bootloader message that the main recovery uses to save its
1342 // arguments in case of the device restarting midway through
1343 // package installation.
Jed Estepa7b9a462015-12-15 16:04:53 -08001344 FILE* f = ota_fopen(filename, "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001345 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1346 int to_write = strlen(stagestr)+1;
1347 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1348 if (to_write > max_size) {
1349 to_write = max_size;
1350 stagestr[max_size-1] = 0;
1351 }
Jed Estepa7b9a462015-12-15 16:04:53 -08001352 ota_fwrite(stagestr, to_write, 1, f);
1353 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001354
1355 free(stagestr);
1356 return StringValue(filename);
1357}
1358
1359// Return the value most recently saved with SetStageFn. The argument
1360// is the block device for the misc partition.
1361Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001362 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001363 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001364 }
1365
1366 char* filename;
1367 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1368
1369 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
Jed Estepa7b9a462015-12-15 16:04:53 -08001370 FILE* f = ota_fopen(filename, "rb");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001371 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001372 ota_fread(buffer, sizeof(buffer), 1, f);
1373 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001374 buffer[sizeof(buffer)-1] = '\0';
1375
1376 return StringValue(strdup(buffer));
1377}
1378
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001379Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1380 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001381 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001382 }
1383
1384 char* filename;
1385 char* len_str;
1386 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1387
Tao Baob15fd222015-09-24 11:10:51 -07001388 size_t len;
1389 android::base::ParseUint(len_str, &len);
Jed Estepa7b9a462015-12-15 16:04:53 -08001390 int fd = ota_open(filename, O_WRONLY, 0644);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001391 int success = wipe_block_device(fd, len);
1392
1393 free(filename);
1394 free(len_str);
1395
Jed Estepa7b9a462015-12-15 16:04:53 -08001396 ota_close(fd);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001397
1398 return StringValue(strdup(success ? "t" : ""));
1399}
1400
Doug Zongkerc704e062014-05-23 08:40:35 -07001401Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1402 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001403 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerc704e062014-05-23 08:40:35 -07001404 }
1405 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1406 fprintf(ui->cmd_pipe, "enable_reboot\n");
1407 return StringValue(strdup("t"));
1408}
1409
Michael Rungeacf47db2014-11-21 00:12:28 -08001410Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1411 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001412 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %d", name, argc);
Michael Rungeacf47db2014-11-21 00:12:28 -08001413 }
1414
1415 char** args = ReadVarArgs(state, argc, argv);
1416 if (args == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -07001417 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
Michael Rungeacf47db2014-11-21 00:12:28 -08001418 }
1419
Tao Baoba9a42a2015-06-23 23:23:33 -07001420 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Michael Rungeacf47db2014-11-21 00:12:28 -08001421 // Tune2fs expects the program name as its args[0]
1422 args2[0] = strdup(name);
Tao Baoba9a42a2015-06-23 23:23:33 -07001423 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001424 args2[i + 1] = args[i];
1425 }
1426 int result = tune2fs_main(argc + 1, args2);
Tao Baoba9a42a2015-06-23 23:23:33 -07001427 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001428 free(args[i]);
1429 }
1430 free(args);
1431
1432 free(args2[0]);
1433 free(args2);
1434 if (result != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001435 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d",
1436 name, result);
Michael Rungeacf47db2014-11-21 00:12:28 -08001437 }
1438 return StringValue(strdup("t"));
1439}
1440
Doug Zongker9931f7f2009-06-10 14:11:53 -07001441void RegisterInstallFunctions() {
1442 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001443 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001444 RegisterFunction("unmount", UnmountFn);
1445 RegisterFunction("format", FormatFn);
1446 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001447 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001448 RegisterFunction("delete", DeleteFn);
1449 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001450 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1451 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001452 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001453
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001454 // Usage:
1455 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1456 // Example:
1457 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1458 RegisterFunction("set_metadata", SetMetadataFn);
1459
1460 // Usage:
1461 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1462 // Example:
1463 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1464 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1465
Doug Zongker8edb00c2009-06-11 17:21:44 -07001466 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001467 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001468
1469 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001470 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1471 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001472
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001473 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001474
Doug Zongker512536a2010-02-17 16:11:44 -08001475 RegisterFunction("read_file", ReadFileFn);
1476 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001477 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001478
Doug Zongkerd0181b82011-10-19 10:51:12 -07001479 RegisterFunction("wipe_cache", WipeCacheFn);
1480
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001481 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001482
1483 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001484
1485 RegisterFunction("reboot_now", RebootNowFn);
1486 RegisterFunction("get_stage", GetStageFn);
1487 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001488
1489 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001490 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001491}