blob: b09086964cf467f0c64850971139016304e81bea [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 <selinux/selinux.h>
31#include <ftw.h>
32#include <sys/capability.h>
33#include <sys/xattr.h>
34#include <linux/xattr.h>
35#include <inttypes.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070036
Elliott Hughes4b166f02015-12-04 15:30:20 -080037#include <android-base/parseint.h>
38#include <android-base/strings.h>
39#include <android-base/stringprintf.h>
Tao Bao1107d962015-09-09 17:16:55 -070040
Doug Zongkerc87bab12013-11-25 13:53:25 -080041#include "bootloader.h"
42#include "applypatch/applypatch.h"
43#include "cutils/android_reboot.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070044#include "cutils/misc.h"
45#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070046#include "edify/expr.h"
Doug Zongker512536a2010-02-17 16:11:44 -080047#include "mincrypt/sha.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070048#include "minzip/DirUtil.h"
49#include "mtdutils/mounts.h"
50#include "mtdutils/mtdutils.h"
51#include "updater.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080052#include "install.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080053#include "tune2fs.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070054
Doug Zongker3d177d02010-07-01 09:18:44 -070055#ifdef USE_EXT4
56#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080057#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070058#endif
59
Tao Bao1107d962015-09-09 17:16:55 -070060// Send over the buffer to recovery though the command pipe.
61static void uiPrint(State* state, const std::string& buffer) {
62 UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070063
Tao Bao1107d962015-09-09 17:16:55 -070064 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
65 // So skip sending empty strings to UI.
66 std::vector<std::string> lines = android::base::Split(buffer, "\n");
67 for (auto& line: lines) {
68 if (!line.empty()) {
69 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
70 fprintf(ui->cmd_pipe, "ui_print\n");
71 }
72 }
73
74 // On the updater side, we need to dump the contents to stderr (which has
75 // been redirected to the log file). Because the recovery will only print
76 // the contents to screen when processing pipe command ui_print.
77 fprintf(stderr, "%s", buffer.c_str());
Michael Runged4a63422014-10-22 19:48:41 -070078}
79
80__attribute__((__format__(printf, 2, 3))) __nonnull((2))
81void uiPrintf(State* state, const char* format, ...) {
Tao Bao1107d962015-09-09 17:16:55 -070082 std::string error_msg;
83
Michael Runged4a63422014-10-22 19:48:41 -070084 va_list ap;
85 va_start(ap, format);
Tao Bao1107d962015-09-09 17:16:55 -070086 android::base::StringAppendV(&error_msg, format, ap);
Michael Runged4a63422014-10-22 19:48:41 -070087 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070088
Michael Runged4a63422014-10-22 19:48:41 -070089 uiPrint(state, error_msg);
90}
91
Doug Zongker52b40362014-02-10 15:30:30 -080092// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070093char* PrintSha1(const uint8_t* digest) {
Tao Baoba9a42a2015-06-23 23:23:33 -070094 char* buffer = reinterpret_cast<char*>(malloc(SHA_DIGEST_SIZE*2 + 1));
Doug Zongker52b40362014-02-10 15:30:30 -080095 const char* alphabet = "0123456789abcdef";
Tao Baoba9a42a2015-06-23 23:23:33 -070096 size_t i;
Doug Zongker52b40362014-02-10 15:30:30 -080097 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
98 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
99 buffer[i*2+1] = alphabet[digest[i] & 0xf];
100 }
101 buffer[i*2] = '\0';
102 return buffer;
103}
104
Doug Zongker3d177d02010-07-01 09:18:44 -0700105// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700106//
Doug Zongker3d177d02010-07-01 09:18:44 -0700107// fs_type="yaffs2" partition_type="MTD" location=partition
108// 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
Doug Zongker3d177d02010-07-01 09:18:44 -0700168 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700169 mtd_scan_partitions();
170 const MtdPartition* mtd;
171 mtd = mtd_find_partition_by_name(location);
172 if (mtd == NULL) {
Tao Bao1107d962015-09-09 17:16:55 -0700173 uiPrintf(state, "%s: no mtd partition named \"%s\"\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700174 name, location);
175 result = strdup("");
176 goto done;
177 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700178 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700179 uiPrintf(state, "mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700180 location, strerror(errno));
181 result = strdup("");
182 goto done;
183 }
184 result = mount_point;
185 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700186 if (mount(location, mount_point, fs_type,
Michael Runge168f7772014-10-22 17:05:08 -0700187 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
188 has_mount_options ? mount_options : "") < 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700189 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700190 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700191 result = strdup("");
192 } else {
193 result = mount_point;
194 }
195 }
196
197done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700198 free(fs_type);
199 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700200 free(location);
201 if (result != mount_point) free(mount_point);
Michael Runge168f7772014-10-22 17:05:08 -0700202 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800203 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700204}
205
Doug Zongker8edb00c2009-06-11 17:21:44 -0700206
207// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800208Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700209 char* result = NULL;
210 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700211 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700212 }
213 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700214 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700215 return NULL;
216 }
217 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700218 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700219 goto done;
220 }
221
222 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700223 {
224 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
225 if (vol == NULL) {
226 result = strdup("");
227 } else {
228 result = mount_point;
229 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700230 }
231
232done:
233 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800234 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700235}
236
237
Doug Zongker512536a2010-02-17 16:11:44 -0800238Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700239 char* result = NULL;
240 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700241 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700242 }
243 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700244 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700245 return NULL;
246 }
247 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700248 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700249 goto done;
250 }
251
252 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700253 {
254 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
255 if (vol == NULL) {
256 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
257 result = strdup("");
258 } else {
259 int ret = unmount_mounted_volume(vol);
260 if (ret != 0) {
261 uiPrintf(state, "unmount of %s failed (%d): %s\n",
262 mount_point, ret, strerror(errno));
263 }
264 result = mount_point;
Michael Runge5ddf4292014-10-24 14:14:41 -0700265 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700266 }
267
268done:
269 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800270 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700271}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700272
JP Abgrall37aedb32014-06-16 19:07:39 -0700273static int exec_cmd(const char* path, char* const argv[]) {
274 int status;
275 pid_t child;
276 if ((child = vfork()) == 0) {
277 execv(path, argv);
278 _exit(-1);
279 }
280 waitpid(child, &status, 0);
281 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
282 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
283 }
284 return WEXITSTATUS(status);
285}
286
Doug Zongker8edb00c2009-06-11 17:21:44 -0700287
Stephen Smalley779701d2012-02-09 14:13:23 -0500288// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700289//
Stephen Smalley779701d2012-02-09 14:13:23 -0500290// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
291// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700292// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
293// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800294// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700295// 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 -0800296Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700297 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400298 if (argc != 5) {
299 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700300 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700301 char* fs_type;
302 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700303 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800304 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400305 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500306
Stephen Smalley779701d2012-02-09 14:13:23 -0500307 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
308 return NULL;
309 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700310
Doug Zongker3d177d02010-07-01 09:18:44 -0700311 if (strlen(fs_type) == 0) {
312 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
313 goto done;
314 }
315 if (strlen(partition_type) == 0) {
316 ErrorAbort(state, "partition_type argument to %s() can't be empty",
317 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700318 goto done;
319 }
320 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700321 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700322 goto done;
323 }
324
Stephen Smalley516e4e22012-04-03 13:35:11 -0400325 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500326 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
327 goto done;
328 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500329
Doug Zongker3d177d02010-07-01 09:18:44 -0700330 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700331 mtd_scan_partitions();
332 const MtdPartition* mtd = mtd_find_partition_by_name(location);
333 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700334 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700335 name, location);
336 result = strdup("");
337 goto done;
338 }
339 MtdWriteContext* ctx = mtd_write_partition(mtd);
340 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700341 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700342 result = strdup("");
343 goto done;
344 }
345 if (mtd_erase_blocks(ctx, -1) == -1) {
346 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700347 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700348 result = strdup("");
349 goto done;
350 }
351 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700352 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700353 result = strdup("");
354 goto done;
355 }
356 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700357#ifdef USE_EXT4
358 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500359 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700360 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700361 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700362 name, status, location);
363 result = strdup("");
364 goto done;
365 }
366 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700367 } else if (strcmp(fs_type, "f2fs") == 0) {
368 char *num_sectors;
369 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
370 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
371 result = strdup("");
372 goto done;
373 }
374 const char *f2fs_path = "/sbin/mkfs.f2fs";
375 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
376 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
377 free(num_sectors);
378 if (status != 0) {
379 printf("%s: mkfs.f2fs failed (%d) on %s",
380 name, status, location);
381 result = strdup("");
382 goto done;
383 }
384 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700385#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700386 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700387 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700388 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700389 }
390
391done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700392 free(fs_type);
393 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700394 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800395 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700396}
397
Michael Rungece7ca712013-11-06 17:42:20 -0800398Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
399 char* result = NULL;
400 if (argc != 2) {
401 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
402 }
403
404 char* src_name;
405 char* dst_name;
406
407 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
408 return NULL;
409 }
410 if (strlen(src_name) == 0) {
411 ErrorAbort(state, "src_name argument to %s() can't be empty", name);
412 goto done;
413 }
414 if (strlen(dst_name) == 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700415 ErrorAbort(state, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800416 goto done;
417 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700418 if (make_parents(dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700419 ErrorAbort(state, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700420 dst_name, strerror(errno));
Michael Runge2f0ef732014-10-22 14:28:23 -0700421 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
422 // File was already moved
423 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700424 } else if (rename(src_name, dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700425 ErrorAbort(state, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800426 src_name, dst_name, strerror(errno));
427 } else {
428 result = dst_name;
429 }
430
431done:
432 free(src_name);
433 if (result != dst_name) free(dst_name);
434 return StringValue(result);
435}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700436
Doug Zongker512536a2010-02-17 16:11:44 -0800437Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700438 char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
439 for (int i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700440 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700441 if (paths[i] == NULL) {
442 int j;
443 for (j = 0; j < i; ++i) {
444 free(paths[j]);
445 }
446 free(paths);
447 return NULL;
448 }
449 }
450
451 bool recursive = (strcmp(name, "delete_recursive") == 0);
452
453 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700454 for (int i = 0; i < argc; ++i) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700455 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
456 ++success;
457 free(paths[i]);
458 }
459 free(paths);
460
461 char buffer[10];
462 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800463 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700464}
465
Doug Zongker8edb00c2009-06-11 17:21:44 -0700466
Doug Zongker512536a2010-02-17 16:11:44 -0800467Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700468 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700469 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700470 }
471 char* frac_str;
472 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700473 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700474 return NULL;
475 }
476
477 double frac = strtod(frac_str, NULL);
Tao Baob15fd222015-09-24 11:10:51 -0700478 int sec;
479 android::base::ParseInt(sec_str, &sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700480
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700481 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700482 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
483
Doug Zongker9931f7f2009-06-10 14:11:53 -0700484 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800485 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700486}
487
Doug Zongker512536a2010-02-17 16:11:44 -0800488Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700489 if (argc != 1) {
490 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
491 }
492 char* frac_str;
493 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
494 return NULL;
495 }
496
497 double frac = strtod(frac_str, NULL);
498
499 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
500 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
501
Doug Zongker512536a2010-02-17 16:11:44 -0800502 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700503}
504
Doug Zongker8edb00c2009-06-11 17:21:44 -0700505// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800506Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700507 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700508 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700509 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700510 }
511 char* zip_path;
512 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700513 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700514
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700515 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700516
517 // To create a consistent system image, never use the clock for timestamps.
518 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
519
520 bool success = mzExtractRecursive(za, zip_path, dest_path,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000521 &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500522 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700523 free(zip_path);
524 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800525 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700526}
527
Doug Zongker8edb00c2009-06-11 17:21:44 -0700528
529// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800530// or
531// package_extract_file(package_path)
532// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800533// function (the char* returned is actually a FileContents*).
534Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700535 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700536 if (argc < 1 || argc > 2) {
537 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800538 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700539 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700540 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700541
Doug Zongker5f875bf2014-08-22 14:53:43 -0700542 if (argc == 2) {
543 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800544
545 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700546
Doug Zongker6aece332010-02-01 14:40:12 -0800547 char* zip_path;
548 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700549 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800550
Doug Zongker6aece332010-02-01 14:40:12 -0800551 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
552 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700553 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800554 goto done2;
555 }
556
Tao Baoba9a42a2015-06-23 23:23:33 -0700557 {
Tao Baod3cac342015-12-14 15:53:25 -0800558 int fd = TEMP_FAILURE_RETRY(open(dest_path, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
559 S_IRUSR | S_IWUSR));
560 if (fd == -1) {
561 printf("%s: can't open %s for write: %s\n", name, dest_path, strerror(errno));
Tao Baoba9a42a2015-06-23 23:23:33 -0700562 goto done2;
563 }
Tao Baod3cac342015-12-14 15:53:25 -0800564 success = mzExtractZipEntryToFile(za, entry, fd);
565 if (fsync(fd) == -1) {
566 printf("fsync of \"%s\" failed: %s\n", dest_path, strerror(errno));
567 success = false;
568 }
569 if (close(fd) == -1) {
570 printf("close of \"%s\" failed: %s\n", dest_path, strerror(errno));
571 success = false;
572 }
Doug Zongker6aece332010-02-01 14:40:12 -0800573 }
Doug Zongker6aece332010-02-01 14:40:12 -0800574
575 done2:
576 free(zip_path);
577 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800578 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800579 } else {
580 // The one-argument version returns the contents of the file
581 // as the result.
582
583 char* zip_path;
Tao Baoba9a42a2015-06-23 23:23:33 -0700584 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -0800585 v->type = VAL_BLOB;
586 v->size = -1;
587 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800588
589 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
590
591 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
592 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
593 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700594 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800595 goto done1;
596 }
597
Doug Zongker512536a2010-02-17 16:11:44 -0800598 v->size = mzGetZipEntryUncompLen(entry);
Tao Baoba9a42a2015-06-23 23:23:33 -0700599 v->data = reinterpret_cast<char*>(malloc(v->size));
Doug Zongker512536a2010-02-17 16:11:44 -0800600 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700601 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800602 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800603 goto done1;
604 }
605
Doug Zongker512536a2010-02-17 16:11:44 -0800606 success = mzExtractZipEntryToBuffer(za, entry,
607 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800608
609 done1:
610 free(zip_path);
611 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800612 free(v->data);
613 v->data = NULL;
614 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800615 }
Doug Zongker512536a2010-02-17 16:11:44 -0800616 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700617 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700618}
619
Doug Zongkera23075f2012-08-06 16:19:09 -0700620// Create all parent directories of name, if necessary.
621static int make_parents(char* name) {
622 char* p;
623 for (p = name + (strlen(name)-1); p > name; --p) {
624 if (*p != '/') continue;
625 *p = '\0';
626 if (make_parents(name) < 0) return -1;
627 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700628 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700629 *p = '/';
630 if (result == 0 || errno == EEXIST) {
631 // successfully created or already existed; we're done
632 return 0;
633 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700634 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700635 return -1;
636 }
637 }
638 return 0;
639}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700640
Doug Zongker9931f7f2009-06-10 14:11:53 -0700641// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700642// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800643Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700644 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700645 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700646 }
647 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700648 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700649 if (target == NULL) return NULL;
650
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700651 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700652 if (srcs == NULL) {
653 free(target);
654 return NULL;
655 }
656
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700657 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700658 int i;
659 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700660 if (unlink(srcs[i]) < 0) {
661 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700662 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700663 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700664 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700665 }
666 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700667 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700668 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700669 name, srcs[i], target);
670 ++bad;
671 }
Doug Zongker60babf82009-09-18 15:11:24 -0700672 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700673 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700674 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700675 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700676 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700677 free(srcs[i]);
678 }
679 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700680 if (bad) {
681 return ErrorAbort(state, "%s: some symlinks failed", name);
682 }
Doug Zongker512536a2010-02-17 16:11:44 -0800683 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700684}
685
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700686struct perm_parsed_args {
687 bool has_uid;
688 uid_t uid;
689 bool has_gid;
690 gid_t gid;
691 bool has_mode;
692 mode_t mode;
693 bool has_fmode;
694 mode_t fmode;
695 bool has_dmode;
696 mode_t dmode;
697 bool has_selabel;
698 char* selabel;
699 bool has_capabilities;
700 uint64_t capabilities;
701};
702
Michael Runged4a63422014-10-22 19:48:41 -0700703static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700704 int i;
705 struct perm_parsed_args parsed;
706 int bad = 0;
707 static int max_warnings = 20;
708
709 memset(&parsed, 0, sizeof(parsed));
710
711 for (i = 1; i < argc; i += 2) {
712 if (strcmp("uid", args[i]) == 0) {
713 int64_t uid;
714 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
715 parsed.uid = uid;
716 parsed.has_uid = true;
717 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700718 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700719 bad++;
720 }
721 continue;
722 }
723 if (strcmp("gid", args[i]) == 0) {
724 int64_t gid;
725 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
726 parsed.gid = gid;
727 parsed.has_gid = true;
728 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700729 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700730 bad++;
731 }
732 continue;
733 }
734 if (strcmp("mode", args[i]) == 0) {
735 int32_t mode;
736 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
737 parsed.mode = mode;
738 parsed.has_mode = true;
739 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700740 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700741 bad++;
742 }
743 continue;
744 }
745 if (strcmp("dmode", args[i]) == 0) {
746 int32_t mode;
747 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
748 parsed.dmode = mode;
749 parsed.has_dmode = true;
750 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700751 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700752 bad++;
753 }
754 continue;
755 }
756 if (strcmp("fmode", args[i]) == 0) {
757 int32_t mode;
758 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
759 parsed.fmode = mode;
760 parsed.has_fmode = true;
761 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700762 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700763 bad++;
764 }
765 continue;
766 }
767 if (strcmp("capabilities", args[i]) == 0) {
768 int64_t capabilities;
769 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
770 parsed.capabilities = capabilities;
771 parsed.has_capabilities = true;
772 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700773 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700774 bad++;
775 }
776 continue;
777 }
778 if (strcmp("selabel", args[i]) == 0) {
779 if (args[i+1][0] != '\0') {
780 parsed.selabel = args[i+1];
781 parsed.has_selabel = true;
782 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700783 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700784 bad++;
785 }
786 continue;
787 }
788 if (max_warnings != 0) {
789 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
790 max_warnings--;
791 if (max_warnings == 0) {
792 printf("ParsedPermArgs: suppressing further warnings\n");
793 }
794 }
795 }
796 return parsed;
797}
798
799static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700800 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700801 const char* filename,
802 const struct stat *statptr,
803 struct perm_parsed_args parsed)
804{
805 int bad = 0;
806
Nick Kralevich68802412014-10-23 20:36:42 -0700807 if (parsed.has_selabel) {
808 if (lsetfilecon(filename, parsed.selabel) != 0) {
809 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
810 filename, parsed.selabel, strerror(errno));
811 bad++;
812 }
813 }
814
Nick Kraleviche4612512013-09-10 15:34:19 -0700815 /* ignore symlinks */
816 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700817 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700818 }
819
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700820 if (parsed.has_uid) {
821 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700822 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
823 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700824 bad++;
825 }
826 }
827
828 if (parsed.has_gid) {
829 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700830 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
831 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700832 bad++;
833 }
834 }
835
836 if (parsed.has_mode) {
837 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700838 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
839 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700840 bad++;
841 }
842 }
843
844 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
845 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700846 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
847 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700848 bad++;
849 }
850 }
851
852 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
853 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700854 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700855 filename, parsed.fmode, strerror(errno));
856 bad++;
857 }
858 }
859
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700860 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
861 if (parsed.capabilities == 0) {
862 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
863 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700864 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700865 filename, parsed.capabilities, strerror(errno));
866 bad++;
867 }
868 } else {
869 struct vfs_cap_data cap_data;
870 memset(&cap_data, 0, sizeof(cap_data));
871 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
872 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
873 cap_data.data[0].inheritable = 0;
874 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
875 cap_data.data[1].inheritable = 0;
876 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700877 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
878 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700879 bad++;
880 }
881 }
882 }
883
884 return bad;
885}
886
887// nftw doesn't allow us to pass along context, so we need to use
888// global variables. *sigh*
889static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700890static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700891
892static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
893 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700894 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700895}
896
897static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700898 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700899 struct stat sb;
900 Value* result = NULL;
901
902 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
903
904 if ((argc % 2) != 1) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700905 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700906 }
907
908 char** args = ReadVarArgs(state, argc, argv);
909 if (args == NULL) return NULL;
910
911 if (lstat(args[0], &sb) == -1) {
912 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
913 goto done;
914 }
915
Tao Baoba9a42a2015-06-23 23:23:33 -0700916 {
917 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700918
Tao Baoba9a42a2015-06-23 23:23:33 -0700919 if (recursive) {
920 recursive_parsed_args = parsed;
921 recursive_state = state;
922 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
923 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
924 recursive_state = NULL;
925 } else {
926 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
927 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700928 }
929
930done:
Tao Baoba9a42a2015-06-23 23:23:33 -0700931 for (int i = 0; i < argc; ++i) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700932 free(args[i]);
933 }
934 free(args);
935
936 if (result != NULL) {
937 return result;
938 }
939
940 if (bad > 0) {
941 return ErrorAbort(state, "%s: some changes failed", name);
942 }
943
944 return StringValue(strdup(""));
945}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700946
Doug Zongker512536a2010-02-17 16:11:44 -0800947Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700948 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700949 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700950 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700951 char* key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700952 if (key == NULL) return NULL;
953
954 char value[PROPERTY_VALUE_MAX];
955 property_get(key, value, "");
956 free(key);
957
Doug Zongker512536a2010-02-17 16:11:44 -0800958 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700959}
960
961
Doug Zongker47cace92009-06-18 10:11:50 -0700962// file_getprop(file, key)
963//
964// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700965// per line. # comment lines,blank lines, lines without '=' ignored),
966// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800967Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700968 char* result = NULL;
969 char* buffer = NULL;
970 char* filename;
971 char* key;
972 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
973 return NULL;
974 }
975
976 struct stat st;
977 if (stat(filename, &st) < 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700978 ErrorAbort(state, "%s: failed to stat \"%s\": %s", name, filename, strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700979 goto done;
980 }
981
982#define MAX_FILE_GETPROP_SIZE 65536
983
984 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700985 ErrorAbort(state, "%s too large for %s (max %d)", filename, name, MAX_FILE_GETPROP_SIZE);
Doug Zongker47cace92009-06-18 10:11:50 -0700986 goto done;
987 }
988
Tao Baoba9a42a2015-06-23 23:23:33 -0700989 buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700990 if (buffer == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700991 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700992 goto done;
993 }
994
Tao Baoba9a42a2015-06-23 23:23:33 -0700995 FILE* f;
996 f = fopen(filename, "rb");
Doug Zongker47cace92009-06-18 10:11:50 -0700997 if (f == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700998 ErrorAbort(state, "%s: failed to open %s: %s", name, filename, strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700999 goto done;
1000 }
1001
Tao Bao5701d582015-09-24 10:56:48 -07001002 if (fread(buffer, 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
Doug Zongkera23075f2012-08-06 16:19:09 -07001003 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -07001004 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -07001005 fclose(f);
1006 goto done;
1007 }
1008 buffer[st.st_size] = '\0';
1009
1010 fclose(f);
1011
Tao Baoba9a42a2015-06-23 23:23:33 -07001012 char* line;
1013 line = strtok(buffer, "\n");
Doug Zongker47cace92009-06-18 10:11:50 -07001014 do {
1015 // skip whitespace at start of line
1016 while (*line && isspace(*line)) ++line;
1017
1018 // comment or blank line: skip to next line
1019 if (*line == '\0' || *line == '#') continue;
1020
1021 char* equal = strchr(line, '=');
1022 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001023 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001024 }
1025
1026 // trim whitespace between key and '='
1027 char* key_end = equal-1;
1028 while (key_end > line && isspace(*key_end)) --key_end;
1029 key_end[1] = '\0';
1030
1031 // not the key we're looking for
1032 if (strcmp(key, line) != 0) continue;
1033
1034 // skip whitespace after the '=' to the start of the value
1035 char* val_start = equal+1;
1036 while(*val_start && isspace(*val_start)) ++val_start;
1037
1038 // trim trailing whitespace
1039 char* val_end = val_start + strlen(val_start)-1;
1040 while (val_end > val_start && isspace(*val_end)) --val_end;
1041 val_end[1] = '\0';
1042
1043 result = strdup(val_start);
1044 break;
1045
1046 } while ((line = strtok(NULL, "\n")));
1047
1048 if (result == NULL) result = strdup("");
1049
1050 done:
1051 free(filename);
1052 free(key);
1053 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001054 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001055}
1056
Doug Zongker179b2d92011-04-12 15:49:04 -07001057// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001058Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001059 char* result = NULL;
1060
Doug Zongker179b2d92011-04-12 15:49:04 -07001061 Value* partition_value;
1062 Value* contents;
1063 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001064 return NULL;
1065 }
1066
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001067 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001068 if (partition_value->type != VAL_STRING) {
1069 ErrorAbort(state, "partition argument to %s must be string", name);
1070 goto done;
1071 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001072 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001073 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001074 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001075 goto done;
1076 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001077 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001078 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001079 goto done;
1080 }
1081
1082 mtd_scan_partitions();
Tao Baoba9a42a2015-06-23 23:23:33 -07001083 const MtdPartition* mtd;
1084 mtd = mtd_find_partition_by_name(partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001085 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001086 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001087 result = strdup("");
1088 goto done;
1089 }
1090
Tao Baoba9a42a2015-06-23 23:23:33 -07001091 MtdWriteContext* ctx;
1092 ctx = mtd_write_partition(mtd);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001093 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001094 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001095 name, partition);
1096 result = strdup("");
1097 goto done;
1098 }
1099
1100 bool success;
1101
Doug Zongker179b2d92011-04-12 15:49:04 -07001102 if (contents->type == VAL_STRING) {
1103 // we're given a filename as the contents
1104 char* filename = contents->data;
1105 FILE* f = fopen(filename, "rb");
1106 if (f == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001107 printf("%s: can't open %s: %s\n", name, filename, strerror(errno));
Doug Zongker179b2d92011-04-12 15:49:04 -07001108 result = strdup("");
1109 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001110 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001111
1112 success = true;
Tao Baoba9a42a2015-06-23 23:23:33 -07001113 char* buffer = reinterpret_cast<char*>(malloc(BUFSIZ));
Doug Zongker179b2d92011-04-12 15:49:04 -07001114 int read;
1115 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1116 int wrote = mtd_write_data(ctx, buffer, read);
1117 success = success && (wrote == read);
1118 }
1119 free(buffer);
1120 fclose(f);
1121 } else {
1122 // we're given a blob as the contents
1123 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1124 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001125 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001126 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001127 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001128 partition, strerror(errno));
1129 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001130
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001131 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001132 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001133 }
1134 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001135 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001136 }
1137
Doug Zongker179b2d92011-04-12 15:49:04 -07001138 printf("%s %s partition\n",
1139 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001140
1141 result = success ? partition : strdup("");
1142
1143done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001144 if (result != partition) FreeValue(partition_value);
1145 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001146 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001147}
1148
Doug Zongker8edb00c2009-06-11 17:21:44 -07001149// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001150Value* ApplyPatchSpaceFn(const char* name, State* state,
1151 int argc, Expr* argv[]) {
1152 char* bytes_str;
1153 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1154 return NULL;
1155 }
1156
Tao Baob15fd222015-09-24 11:10:51 -07001157 size_t bytes;
1158 if (!android::base::ParseUint(bytes_str, &bytes)) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001159 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n", name, bytes_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001160 free(bytes_str);
Tao Baob15fd222015-09-24 11:10:51 -07001161 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001162 }
1163
1164 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1165}
1166
Doug Zongker52b40362014-02-10 15:30:30 -08001167// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1168
Doug Zongker512536a2010-02-17 16:11:44 -08001169Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001170 if (argc < 6 || (argc % 2) == 1) {
1171 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1172 "even number, got %d",
1173 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001174 }
1175
Doug Zongkerc4351c72010-02-22 14:46:32 -08001176 char* source_filename;
1177 char* target_filename;
1178 char* target_sha1;
1179 char* target_size_str;
1180 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1181 &target_sha1, &target_size_str) < 0) {
1182 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001183 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001184
Tao Baob15fd222015-09-24 11:10:51 -07001185 size_t target_size;
1186 if (!android::base::ParseUint(target_size_str, &target_size)) {
1187 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count", name, target_size_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001188 free(source_filename);
1189 free(target_filename);
1190 free(target_sha1);
1191 free(target_size_str);
Tao Baob15fd222015-09-24 11:10:51 -07001192 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001193 }
1194
1195 int patchcount = (argc-4) / 2;
1196 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001197
1198 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001199 for (i = 0; i < patchcount; ++i) {
1200 if (patches[i*2]->type != VAL_STRING) {
1201 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1202 break;
1203 }
1204 if (patches[i*2+1]->type != VAL_BLOB) {
1205 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1206 break;
1207 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001208 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001209 if (i != patchcount) {
1210 for (i = 0; i < patchcount*2; ++i) {
1211 FreeValue(patches[i]);
1212 }
1213 free(patches);
1214 return NULL;
1215 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001216
Tao Baoba9a42a2015-06-23 23:23:33 -07001217 char** patch_sha_str = reinterpret_cast<char**>(malloc(patchcount * sizeof(char*)));
Doug Zongkerc4351c72010-02-22 14:46:32 -08001218 for (i = 0; i < patchcount; ++i) {
1219 patch_sha_str[i] = patches[i*2]->data;
1220 patches[i*2]->data = NULL;
1221 FreeValue(patches[i*2]);
1222 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001223 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001224
1225 int result = applypatch(source_filename, target_filename,
1226 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001227 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001228
1229 for (i = 0; i < patchcount; ++i) {
1230 FreeValue(patches[i]);
1231 }
1232 free(patch_sha_str);
1233 free(patches);
1234
1235 return StringValue(strdup(result == 0 ? "t" : ""));
1236}
1237
1238// apply_patch_check(file, [sha1_1, ...])
1239Value* ApplyPatchCheckFn(const char* name, State* state,
1240 int argc, Expr* argv[]) {
1241 if (argc < 1) {
1242 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1243 name, argc);
1244 }
1245
1246 char* filename;
1247 if (ReadArgs(state, argv, 1, &filename) < 0) {
1248 return NULL;
1249 }
1250
1251 int patchcount = argc-1;
1252 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1253
1254 int result = applypatch_check(filename, patchcount, sha1s);
1255
1256 int i;
1257 for (i = 0; i < patchcount; ++i) {
1258 free(sha1s[i]);
1259 }
1260 free(sha1s);
1261
1262 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001263}
1264
Tao Bao1107d962015-09-09 17:16:55 -07001265// This is the updater side handler for ui_print() in edify script. Contents
1266// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001267Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001268 char** args = ReadVarArgs(state, argc, argv);
1269 if (args == NULL) {
1270 return NULL;
1271 }
1272
Tao Bao1107d962015-09-09 17:16:55 -07001273 std::string buffer;
1274 for (int i = 0; i < argc; ++i) {
1275 buffer += args[i];
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001276 free(args[i]);
1277 }
1278 free(args);
Tao Bao1107d962015-09-09 17:16:55 -07001279
1280 buffer += "\n";
Michael Runged4a63422014-10-22 19:48:41 -07001281 uiPrint(state, buffer);
Tao Bao1107d962015-09-09 17:16:55 -07001282 return StringValue(strdup(buffer.c_str()));
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001283}
1284
Doug Zongkerd0181b82011-10-19 10:51:12 -07001285Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1286 if (argc != 0) {
1287 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1288 }
1289 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1290 return StringValue(strdup("t"));
1291}
1292
Doug Zongker512536a2010-02-17 16:11:44 -08001293Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001294 if (argc < 1) {
1295 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1296 }
1297 char** args = ReadVarArgs(state, argc, argv);
1298 if (args == NULL) {
1299 return NULL;
1300 }
1301
Tao Baoba9a42a2015-06-23 23:23:33 -07001302 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001303 memcpy(args2, args, sizeof(char*) * argc);
1304 args2[argc] = NULL;
1305
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001306 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001307
1308 pid_t child = fork();
1309 if (child == 0) {
1310 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001311 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001312 _exit(1);
1313 }
1314 int status;
1315 waitpid(child, &status, 0);
1316 if (WIFEXITED(status)) {
1317 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001318 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001319 WEXITSTATUS(status));
1320 }
1321 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001322 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001323 WTERMSIG(status));
1324 }
1325
1326 int i;
1327 for (i = 0; i < argc; ++i) {
1328 free(args[i]);
1329 }
1330 free(args);
1331 free(args2);
1332
1333 char buffer[20];
1334 sprintf(buffer, "%d", status);
1335
Doug Zongker512536a2010-02-17 16:11:44 -08001336 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001337}
1338
Doug Zongker512536a2010-02-17 16:11:44 -08001339// sha1_check(data)
1340// to return the sha1 of the data (given in the format returned by
1341// read_file).
1342//
1343// sha1_check(data, sha1_hex, [sha1_hex, ...])
1344// returns the sha1 of the file if it matches any of the hex
1345// strings passed, or "" if it does not equal any of them.
1346//
1347Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1348 if (argc < 1) {
1349 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1350 }
1351
1352 Value** args = ReadValueVarArgs(state, argc, argv);
1353 if (args == NULL) {
1354 return NULL;
1355 }
1356
1357 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001358 return StringValue(strdup(""));
1359 }
1360 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001361 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001362 FreeValue(args[0]);
1363
1364 if (argc == 1) {
1365 return StringValue(PrintSha1(digest));
1366 }
1367
1368 int i;
Tao Baoba9a42a2015-06-23 23:23:33 -07001369 uint8_t* arg_digest = reinterpret_cast<uint8_t*>(malloc(SHA_DIGEST_SIZE));
Doug Zongker512536a2010-02-17 16:11:44 -08001370 for (i = 1; i < argc; ++i) {
1371 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001372 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001373 name, i);
1374 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1375 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001376 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1377 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001378 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1379 break;
1380 }
1381 FreeValue(args[i]);
1382 }
1383 if (i >= argc) {
1384 // Didn't match any of the hex strings; return false.
1385 return StringValue(strdup(""));
1386 }
1387 // Found a match; free all the remaining arguments and return the
1388 // matched one.
1389 int j;
1390 for (j = i+1; j < argc; ++j) {
1391 FreeValue(args[j]);
1392 }
1393 return args[i];
1394}
1395
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001396// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001397// is actually a FileContents*).
1398Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1399 if (argc != 1) {
1400 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1401 }
1402 char* filename;
1403 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1404
Tao Baoba9a42a2015-06-23 23:23:33 -07001405 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -08001406 v->type = VAL_BLOB;
1407
1408 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001409 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001410 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001411 v->size = -1;
1412 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001413 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001414 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001415 }
1416
1417 v->size = fc.size;
1418 v->data = (char*)fc.data;
1419
1420 free(filename);
1421 return v;
1422}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001423
Doug Zongkerc87bab12013-11-25 13:53:25 -08001424// Immediately reboot the device. Recovery is not finished normally,
1425// so if you reboot into recovery it will re-start applying the
1426// current package (because nothing has cleared the copy of the
1427// arguments stored in the BCB).
1428//
1429// The argument is the partition name passed to the android reboot
1430// property. It can be "recovery" to boot from the recovery
1431// partition, or "" (empty string) to boot from the regular boot
1432// partition.
1433Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1434 if (argc != 2) {
1435 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1436 }
1437
1438 char* filename;
1439 char* property;
1440 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1441
1442 char buffer[80];
1443
1444 // zero out the 'command' field of the bootloader message.
1445 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1446 FILE* f = fopen(filename, "r+b");
1447 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1448 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1449 fclose(f);
1450 free(filename);
1451
1452 strcpy(buffer, "reboot,");
1453 if (property != NULL) {
1454 strncat(buffer, property, sizeof(buffer)-10);
1455 }
1456
1457 property_set(ANDROID_RB_PROPERTY, buffer);
1458
1459 sleep(5);
1460 free(property);
1461 ErrorAbort(state, "%s() failed to reboot", name);
1462 return NULL;
1463}
1464
1465// Store a string value somewhere that future invocations of recovery
1466// can access it. This value is called the "stage" and can be used to
1467// drive packages that need to do reboots in the middle of
1468// installation and keep track of where they are in the multi-stage
1469// install.
1470//
1471// The first argument is the block device for the misc partition
1472// ("/misc" in the fstab), which is where this value is stored. The
1473// second argument is the string to store; it should not exceed 31
1474// bytes.
1475Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1476 if (argc != 2) {
1477 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1478 }
1479
1480 char* filename;
1481 char* stagestr;
1482 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1483
1484 // Store this value in the misc partition, immediately after the
1485 // bootloader message that the main recovery uses to save its
1486 // arguments in case of the device restarting midway through
1487 // package installation.
1488 FILE* f = fopen(filename, "r+b");
1489 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1490 int to_write = strlen(stagestr)+1;
1491 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1492 if (to_write > max_size) {
1493 to_write = max_size;
1494 stagestr[max_size-1] = 0;
1495 }
1496 fwrite(stagestr, to_write, 1, f);
1497 fclose(f);
1498
1499 free(stagestr);
1500 return StringValue(filename);
1501}
1502
1503// Return the value most recently saved with SetStageFn. The argument
1504// is the block device for the misc partition.
1505Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001506 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001507 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1508 }
1509
1510 char* filename;
1511 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1512
1513 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1514 FILE* f = fopen(filename, "rb");
1515 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1516 fread(buffer, sizeof(buffer), 1, f);
1517 fclose(f);
1518 buffer[sizeof(buffer)-1] = '\0';
1519
1520 return StringValue(strdup(buffer));
1521}
1522
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001523Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1524 if (argc != 2) {
1525 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1526 }
1527
1528 char* filename;
1529 char* len_str;
1530 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1531
Tao Baob15fd222015-09-24 11:10:51 -07001532 size_t len;
1533 android::base::ParseUint(len_str, &len);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001534 int fd = open(filename, O_WRONLY, 0644);
1535 int success = wipe_block_device(fd, len);
1536
1537 free(filename);
1538 free(len_str);
1539
1540 close(fd);
1541
1542 return StringValue(strdup(success ? "t" : ""));
1543}
1544
Doug Zongkerc704e062014-05-23 08:40:35 -07001545Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1546 if (argc != 0) {
1547 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1548 }
1549 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1550 fprintf(ui->cmd_pipe, "enable_reboot\n");
1551 return StringValue(strdup("t"));
1552}
1553
Michael Rungeacf47db2014-11-21 00:12:28 -08001554Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1555 if (argc == 0) {
1556 return ErrorAbort(state, "%s() expects args, got %d", name, argc);
1557 }
1558
1559 char** args = ReadVarArgs(state, argc, argv);
1560 if (args == NULL) {
1561 return ErrorAbort(state, "%s() could not read args", name);
1562 }
1563
Tao Baoba9a42a2015-06-23 23:23:33 -07001564 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Michael Rungeacf47db2014-11-21 00:12:28 -08001565 // Tune2fs expects the program name as its args[0]
1566 args2[0] = strdup(name);
Tao Baoba9a42a2015-06-23 23:23:33 -07001567 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001568 args2[i + 1] = args[i];
1569 }
1570 int result = tune2fs_main(argc + 1, args2);
Tao Baoba9a42a2015-06-23 23:23:33 -07001571 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001572 free(args[i]);
1573 }
1574 free(args);
1575
1576 free(args2[0]);
1577 free(args2);
1578 if (result != 0) {
1579 return ErrorAbort(state, "%s() returned error code %d", name, result);
1580 }
1581 return StringValue(strdup("t"));
1582}
1583
Doug Zongker9931f7f2009-06-10 14:11:53 -07001584void RegisterInstallFunctions() {
1585 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001586 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001587 RegisterFunction("unmount", UnmountFn);
1588 RegisterFunction("format", FormatFn);
1589 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001590 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001591 RegisterFunction("delete", DeleteFn);
1592 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001593 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1594 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001595 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001596
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001597 // Usage:
1598 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1599 // Example:
1600 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1601 RegisterFunction("set_metadata", SetMetadataFn);
1602
1603 // Usage:
1604 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1605 // Example:
1606 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1607 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1608
Doug Zongker8edb00c2009-06-11 17:21:44 -07001609 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001610 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001611 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001612
1613 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001614 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1615 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001616
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001617 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001618
Doug Zongker512536a2010-02-17 16:11:44 -08001619 RegisterFunction("read_file", ReadFileFn);
1620 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001621 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001622
Doug Zongkerd0181b82011-10-19 10:51:12 -07001623 RegisterFunction("wipe_cache", WipeCacheFn);
1624
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001625 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001626
1627 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001628
1629 RegisterFunction("reboot_now", RebootNowFn);
1630 RegisterFunction("get_stage", GetStageFn);
1631 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001632
1633 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001634 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001635}