blob: 45bbf2bc14a54bde0b93f00eedfa203de9a28f52 [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
Yabin Cui64be2132016-02-03 18:16:02 -080037#include <memory>
38#include <vector>
39
Elliott Hughes4b166f02015-12-04 15:30:20 -080040#include <android-base/parseint.h>
41#include <android-base/strings.h>
42#include <android-base/stringprintf.h>
Tao Bao1107d962015-09-09 17:16:55 -070043
Doug Zongkerc87bab12013-11-25 13:53:25 -080044#include "bootloader.h"
45#include "applypatch/applypatch.h"
46#include "cutils/android_reboot.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070047#include "cutils/misc.h"
48#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070049#include "edify/expr.h"
Sen Jiangc48cb5e2016-02-04 16:23:21 +080050#include "openssl/sha.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070051#include "minzip/DirUtil.h"
52#include "mtdutils/mounts.h"
53#include "mtdutils/mtdutils.h"
54#include "updater.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080055#include "install.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080056#include "tune2fs.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070057
Doug Zongker3d177d02010-07-01 09:18:44 -070058#ifdef USE_EXT4
59#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080060#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070061#endif
62
Tao Bao1107d962015-09-09 17:16:55 -070063// Send over the buffer to recovery though the command pipe.
64static void uiPrint(State* state, const std::string& buffer) {
65 UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070066
Tao Bao1107d962015-09-09 17:16:55 -070067 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
68 // So skip sending empty strings to UI.
69 std::vector<std::string> lines = android::base::Split(buffer, "\n");
70 for (auto& line: lines) {
71 if (!line.empty()) {
72 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
73 fprintf(ui->cmd_pipe, "ui_print\n");
74 }
75 }
76
77 // On the updater side, we need to dump the contents to stderr (which has
78 // been redirected to the log file). Because the recovery will only print
79 // the contents to screen when processing pipe command ui_print.
80 fprintf(stderr, "%s", buffer.c_str());
Michael Runged4a63422014-10-22 19:48:41 -070081}
82
83__attribute__((__format__(printf, 2, 3))) __nonnull((2))
84void uiPrintf(State* state, const char* format, ...) {
Tao Bao1107d962015-09-09 17:16:55 -070085 std::string error_msg;
86
Michael Runged4a63422014-10-22 19:48:41 -070087 va_list ap;
88 va_start(ap, format);
Tao Bao1107d962015-09-09 17:16:55 -070089 android::base::StringAppendV(&error_msg, format, ap);
Michael Runged4a63422014-10-22 19:48:41 -070090 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070091
Michael Runged4a63422014-10-22 19:48:41 -070092 uiPrint(state, error_msg);
93}
94
Doug Zongker52b40362014-02-10 15:30:30 -080095// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070096char* PrintSha1(const uint8_t* digest) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +080097 char* buffer = reinterpret_cast<char*>(malloc(SHA_DIGEST_LENGTH*2 + 1));
Doug Zongker52b40362014-02-10 15:30:30 -080098 const char* alphabet = "0123456789abcdef";
Tao Baoba9a42a2015-06-23 23:23:33 -070099 size_t i;
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800100 for (i = 0; i < SHA_DIGEST_LENGTH; ++i) {
Doug Zongker52b40362014-02-10 15:30:30 -0800101 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
102 buffer[i*2+1] = alphabet[digest[i] & 0xf];
103 }
104 buffer[i*2] = '\0';
105 return buffer;
106}
107
Doug Zongker3d177d02010-07-01 09:18:44 -0700108// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700109//
Doug Zongker3d177d02010-07-01 09:18:44 -0700110// fs_type="yaffs2" partition_type="MTD" location=partition
111// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800112Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700113 char* result = NULL;
Michael Runge168f7772014-10-22 17:05:08 -0700114 if (argc != 4 && argc != 5) {
115 return ErrorAbort(state, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700116 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700117 char* fs_type;
118 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700119 char* location;
120 char* mount_point;
Michael Runge168f7772014-10-22 17:05:08 -0700121 char* mount_options;
122 bool has_mount_options;
123 if (argc == 5) {
124 has_mount_options = true;
125 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
126 &location, &mount_point, &mount_options) < 0) {
127 return NULL;
128 }
129 } else {
130 has_mount_options = false;
131 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700132 &location, &mount_point) < 0) {
Michael Runge168f7772014-10-22 17:05:08 -0700133 return NULL;
134 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700135 }
136
Doug Zongker3d177d02010-07-01 09:18:44 -0700137 if (strlen(fs_type) == 0) {
138 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
139 goto done;
140 }
141 if (strlen(partition_type) == 0) {
142 ErrorAbort(state, "partition_type argument to %s() can't be empty",
143 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700144 goto done;
145 }
146 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700147 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700148 goto done;
149 }
150 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700151 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700152 goto done;
153 }
154
Tao Baoba9a42a2015-06-23 23:23:33 -0700155 {
156 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500157
Tao Baoba9a42a2015-06-23 23:23:33 -0700158 if (sehandle) {
159 selabel_lookup(sehandle, &secontext, mount_point, 0755);
160 setfscreatecon(secontext);
161 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500162
Tao Baoba9a42a2015-06-23 23:23:33 -0700163 mkdir(mount_point, 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700164
Tao Baoba9a42a2015-06-23 23:23:33 -0700165 if (secontext) {
166 freecon(secontext);
167 setfscreatecon(NULL);
168 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500169 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500170
Doug Zongker3d177d02010-07-01 09:18:44 -0700171 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700172 mtd_scan_partitions();
173 const MtdPartition* mtd;
174 mtd = mtd_find_partition_by_name(location);
175 if (mtd == NULL) {
Tao Bao1107d962015-09-09 17:16:55 -0700176 uiPrintf(state, "%s: no mtd partition named \"%s\"\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700177 name, location);
178 result = strdup("");
179 goto done;
180 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700181 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700182 uiPrintf(state, "mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700183 location, strerror(errno));
184 result = strdup("");
185 goto done;
186 }
187 result = mount_point;
188 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700189 if (mount(location, mount_point, fs_type,
Michael Runge168f7772014-10-22 17:05:08 -0700190 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
191 has_mount_options ? mount_options : "") < 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700192 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700193 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700194 result = strdup("");
195 } else {
196 result = mount_point;
197 }
198 }
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) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700214 return ErrorAbort(state, "%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) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700221 ErrorAbort(state, "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 {
227 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
228 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) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700244 return ErrorAbort(state, "%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) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700251 ErrorAbort(state, "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 {
257 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
258 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="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
294// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700295// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
296// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800297// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700298// 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 -0800299Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700300 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400301 if (argc != 5) {
302 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700303 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700304 char* fs_type;
305 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700306 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800307 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400308 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500309
Stephen Smalley779701d2012-02-09 14:13:23 -0500310 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
311 return NULL;
312 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700313
Doug Zongker3d177d02010-07-01 09:18:44 -0700314 if (strlen(fs_type) == 0) {
315 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
316 goto done;
317 }
318 if (strlen(partition_type) == 0) {
319 ErrorAbort(state, "partition_type argument to %s() can't be empty",
320 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700321 goto done;
322 }
323 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700324 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700325 goto done;
326 }
327
Stephen Smalley516e4e22012-04-03 13:35:11 -0400328 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500329 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
330 goto done;
331 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500332
Doug Zongker3d177d02010-07-01 09:18:44 -0700333 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700334 mtd_scan_partitions();
335 const MtdPartition* mtd = mtd_find_partition_by_name(location);
336 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700337 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700338 name, location);
339 result = strdup("");
340 goto done;
341 }
342 MtdWriteContext* ctx = mtd_write_partition(mtd);
343 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700344 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700345 result = strdup("");
346 goto done;
347 }
348 if (mtd_erase_blocks(ctx, -1) == -1) {
349 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700350 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700351 result = strdup("");
352 goto done;
353 }
354 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700355 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700356 result = strdup("");
357 goto done;
358 }
359 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700360#ifdef USE_EXT4
361 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500362 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700363 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700364 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700365 name, status, location);
366 result = strdup("");
367 goto done;
368 }
369 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700370 } else if (strcmp(fs_type, "f2fs") == 0) {
371 char *num_sectors;
372 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
373 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
374 result = strdup("");
375 goto done;
376 }
377 const char *f2fs_path = "/sbin/mkfs.f2fs";
378 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
379 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
380 free(num_sectors);
381 if (status != 0) {
382 printf("%s: mkfs.f2fs failed (%d) on %s",
383 name, status, location);
384 result = strdup("");
385 goto done;
386 }
387 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700388#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700389 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700390 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700391 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700392 }
393
394done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700395 free(fs_type);
396 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700397 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800398 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700399}
400
Michael Rungece7ca712013-11-06 17:42:20 -0800401Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
402 char* result = NULL;
403 if (argc != 2) {
404 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
405 }
406
407 char* src_name;
408 char* dst_name;
409
410 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
411 return NULL;
412 }
413 if (strlen(src_name) == 0) {
414 ErrorAbort(state, "src_name argument to %s() can't be empty", name);
415 goto done;
416 }
417 if (strlen(dst_name) == 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700418 ErrorAbort(state, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800419 goto done;
420 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700421 if (make_parents(dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700422 ErrorAbort(state, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700423 dst_name, strerror(errno));
Michael Runge2f0ef732014-10-22 14:28:23 -0700424 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
425 // File was already moved
426 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700427 } else if (rename(src_name, dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700428 ErrorAbort(state, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800429 src_name, dst_name, strerror(errno));
430 } else {
431 result = dst_name;
432 }
433
434done:
435 free(src_name);
436 if (result != dst_name) free(dst_name);
437 return StringValue(result);
438}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700439
Doug Zongker512536a2010-02-17 16:11:44 -0800440Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700441 char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
442 for (int i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700443 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700444 if (paths[i] == NULL) {
Yabin Cui64be2132016-02-03 18:16:02 -0800445 for (int j = 0; j < i; ++j) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700446 free(paths[j]);
447 }
448 free(paths);
449 return NULL;
450 }
451 }
452
453 bool recursive = (strcmp(name, "delete_recursive") == 0);
454
455 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700456 for (int i = 0; i < argc; ++i) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700457 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
458 ++success;
459 free(paths[i]);
460 }
461 free(paths);
462
463 char buffer[10];
464 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800465 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700466}
467
Doug Zongker8edb00c2009-06-11 17:21:44 -0700468
Doug Zongker512536a2010-02-17 16:11:44 -0800469Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700470 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700471 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700472 }
473 char* frac_str;
474 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700475 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700476 return NULL;
477 }
478
479 double frac = strtod(frac_str, NULL);
Tao Baob15fd222015-09-24 11:10:51 -0700480 int sec;
481 android::base::ParseInt(sec_str, &sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700482
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700483 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700484 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
485
Doug Zongker9931f7f2009-06-10 14:11:53 -0700486 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800487 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700488}
489
Doug Zongker512536a2010-02-17 16:11:44 -0800490Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700491 if (argc != 1) {
492 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
493 }
494 char* frac_str;
495 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
496 return NULL;
497 }
498
499 double frac = strtod(frac_str, NULL);
500
501 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
502 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
503
Doug Zongker512536a2010-02-17 16:11:44 -0800504 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700505}
506
Doug Zongker8edb00c2009-06-11 17:21:44 -0700507// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800508Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700509 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700510 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700511 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700512 }
513 char* zip_path;
514 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700515 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700516
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700517 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700518
519 // To create a consistent system image, never use the clock for timestamps.
520 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
521
522 bool success = mzExtractRecursive(za, zip_path, dest_path,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000523 &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500524 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700525 free(zip_path);
526 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800527 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700528}
529
Doug Zongker8edb00c2009-06-11 17:21:44 -0700530
531// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800532// or
533// package_extract_file(package_path)
534// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800535// function (the char* returned is actually a FileContents*).
536Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700537 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700538 if (argc < 1 || argc > 2) {
539 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800540 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700541 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700542 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700543
Doug Zongker5f875bf2014-08-22 14:53:43 -0700544 if (argc == 2) {
545 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800546
547 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700548
Doug Zongker6aece332010-02-01 14:40:12 -0800549 char* zip_path;
550 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700551 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800552
Doug Zongker6aece332010-02-01 14:40:12 -0800553 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
554 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700555 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800556 goto done2;
557 }
558
Tao Baoba9a42a2015-06-23 23:23:33 -0700559 {
Tao Baod3cac342015-12-14 15:53:25 -0800560 int fd = TEMP_FAILURE_RETRY(open(dest_path, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
561 S_IRUSR | S_IWUSR));
562 if (fd == -1) {
563 printf("%s: can't open %s for write: %s\n", name, dest_path, strerror(errno));
Tao Baoba9a42a2015-06-23 23:23:33 -0700564 goto done2;
565 }
Tao Baod3cac342015-12-14 15:53:25 -0800566 success = mzExtractZipEntryToFile(za, entry, fd);
567 if (fsync(fd) == -1) {
568 printf("fsync of \"%s\" failed: %s\n", dest_path, strerror(errno));
569 success = false;
570 }
571 if (close(fd) == -1) {
572 printf("close of \"%s\" failed: %s\n", dest_path, strerror(errno));
573 success = false;
574 }
Doug Zongker6aece332010-02-01 14:40:12 -0800575 }
Doug Zongker6aece332010-02-01 14:40:12 -0800576
577 done2:
578 free(zip_path);
579 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800580 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800581 } else {
582 // The one-argument version returns the contents of the file
583 // as the result.
584
585 char* zip_path;
Yabin Cui64be2132016-02-03 18:16:02 -0800586 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
587
Tao Baoba9a42a2015-06-23 23:23:33 -0700588 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -0800589 v->type = VAL_BLOB;
590 v->size = -1;
591 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800592
Doug Zongker6aece332010-02-01 14:40:12 -0800593 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
594 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
595 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700596 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800597 goto done1;
598 }
599
Doug Zongker512536a2010-02-17 16:11:44 -0800600 v->size = mzGetZipEntryUncompLen(entry);
Tao Baoba9a42a2015-06-23 23:23:33 -0700601 v->data = reinterpret_cast<char*>(malloc(v->size));
Doug Zongker512536a2010-02-17 16:11:44 -0800602 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700603 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800604 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800605 goto done1;
606 }
607
Doug Zongker512536a2010-02-17 16:11:44 -0800608 success = mzExtractZipEntryToBuffer(za, entry,
609 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800610
611 done1:
612 free(zip_path);
613 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800614 free(v->data);
615 v->data = NULL;
616 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800617 }
Doug Zongker512536a2010-02-17 16:11:44 -0800618 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700619 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700620}
621
Doug Zongkera23075f2012-08-06 16:19:09 -0700622// Create all parent directories of name, if necessary.
623static int make_parents(char* name) {
624 char* p;
625 for (p = name + (strlen(name)-1); p > name; --p) {
626 if (*p != '/') continue;
627 *p = '\0';
628 if (make_parents(name) < 0) return -1;
629 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700630 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700631 *p = '/';
632 if (result == 0 || errno == EEXIST) {
633 // successfully created or already existed; we're done
634 return 0;
635 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700636 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700637 return -1;
638 }
639 }
640 return 0;
641}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700642
Doug Zongker9931f7f2009-06-10 14:11:53 -0700643// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700644// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800645Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700646 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700647 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700648 }
649 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700650 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700651 if (target == NULL) return NULL;
652
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700653 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700654 if (srcs == NULL) {
655 free(target);
656 return NULL;
657 }
658
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700659 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700660 int i;
661 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700662 if (unlink(srcs[i]) < 0) {
663 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700664 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700665 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700666 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700667 }
668 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700669 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700670 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700671 name, srcs[i], target);
672 ++bad;
673 }
Doug Zongker60babf82009-09-18 15:11:24 -0700674 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700675 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700676 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700677 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700678 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700679 free(srcs[i]);
680 }
681 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700682 if (bad) {
683 return ErrorAbort(state, "%s: some symlinks failed", name);
684 }
Doug Zongker512536a2010-02-17 16:11:44 -0800685 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700686}
687
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700688struct perm_parsed_args {
689 bool has_uid;
690 uid_t uid;
691 bool has_gid;
692 gid_t gid;
693 bool has_mode;
694 mode_t mode;
695 bool has_fmode;
696 mode_t fmode;
697 bool has_dmode;
698 mode_t dmode;
699 bool has_selabel;
700 char* selabel;
701 bool has_capabilities;
702 uint64_t capabilities;
703};
704
Michael Runged4a63422014-10-22 19:48:41 -0700705static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700706 int i;
707 struct perm_parsed_args parsed;
708 int bad = 0;
709 static int max_warnings = 20;
710
711 memset(&parsed, 0, sizeof(parsed));
712
713 for (i = 1; i < argc; i += 2) {
714 if (strcmp("uid", args[i]) == 0) {
715 int64_t uid;
716 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
717 parsed.uid = uid;
718 parsed.has_uid = true;
719 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700720 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700721 bad++;
722 }
723 continue;
724 }
725 if (strcmp("gid", args[i]) == 0) {
726 int64_t gid;
727 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
728 parsed.gid = gid;
729 parsed.has_gid = true;
730 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700731 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700732 bad++;
733 }
734 continue;
735 }
736 if (strcmp("mode", args[i]) == 0) {
737 int32_t mode;
738 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
739 parsed.mode = mode;
740 parsed.has_mode = true;
741 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700742 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700743 bad++;
744 }
745 continue;
746 }
747 if (strcmp("dmode", args[i]) == 0) {
748 int32_t mode;
749 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
750 parsed.dmode = mode;
751 parsed.has_dmode = true;
752 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700753 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700754 bad++;
755 }
756 continue;
757 }
758 if (strcmp("fmode", args[i]) == 0) {
759 int32_t mode;
760 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
761 parsed.fmode = mode;
762 parsed.has_fmode = true;
763 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700764 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700765 bad++;
766 }
767 continue;
768 }
769 if (strcmp("capabilities", args[i]) == 0) {
770 int64_t capabilities;
771 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
772 parsed.capabilities = capabilities;
773 parsed.has_capabilities = true;
774 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700775 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700776 bad++;
777 }
778 continue;
779 }
780 if (strcmp("selabel", args[i]) == 0) {
781 if (args[i+1][0] != '\0') {
782 parsed.selabel = args[i+1];
783 parsed.has_selabel = true;
784 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700785 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700786 bad++;
787 }
788 continue;
789 }
790 if (max_warnings != 0) {
791 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
792 max_warnings--;
793 if (max_warnings == 0) {
794 printf("ParsedPermArgs: suppressing further warnings\n");
795 }
796 }
797 }
798 return parsed;
799}
800
801static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700802 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700803 const char* filename,
804 const struct stat *statptr,
805 struct perm_parsed_args parsed)
806{
807 int bad = 0;
808
Nick Kralevich68802412014-10-23 20:36:42 -0700809 if (parsed.has_selabel) {
810 if (lsetfilecon(filename, parsed.selabel) != 0) {
811 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
812 filename, parsed.selabel, strerror(errno));
813 bad++;
814 }
815 }
816
Nick Kraleviche4612512013-09-10 15:34:19 -0700817 /* ignore symlinks */
818 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700819 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700820 }
821
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700822 if (parsed.has_uid) {
823 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700824 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
825 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700826 bad++;
827 }
828 }
829
830 if (parsed.has_gid) {
831 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700832 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
833 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700834 bad++;
835 }
836 }
837
838 if (parsed.has_mode) {
839 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700840 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
841 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700842 bad++;
843 }
844 }
845
846 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
847 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700848 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
849 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700850 bad++;
851 }
852 }
853
854 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
855 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700856 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700857 filename, parsed.fmode, strerror(errno));
858 bad++;
859 }
860 }
861
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700862 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
863 if (parsed.capabilities == 0) {
864 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
865 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700866 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700867 filename, parsed.capabilities, strerror(errno));
868 bad++;
869 }
870 } else {
871 struct vfs_cap_data cap_data;
872 memset(&cap_data, 0, sizeof(cap_data));
873 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
874 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
875 cap_data.data[0].inheritable = 0;
876 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
877 cap_data.data[1].inheritable = 0;
878 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700879 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
880 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700881 bad++;
882 }
883 }
884 }
885
886 return bad;
887}
888
889// nftw doesn't allow us to pass along context, so we need to use
890// global variables. *sigh*
891static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700892static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700893
894static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
895 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700896 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700897}
898
899static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700900 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700901 struct stat sb;
902 Value* result = NULL;
903
904 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
905
906 if ((argc % 2) != 1) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700907 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700908 }
909
910 char** args = ReadVarArgs(state, argc, argv);
911 if (args == NULL) return NULL;
912
913 if (lstat(args[0], &sb) == -1) {
914 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
915 goto done;
916 }
917
Tao Baoba9a42a2015-06-23 23:23:33 -0700918 {
919 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700920
Tao Baoba9a42a2015-06-23 23:23:33 -0700921 if (recursive) {
922 recursive_parsed_args = parsed;
923 recursive_state = state;
924 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
925 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
926 recursive_state = NULL;
927 } else {
928 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
929 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700930 }
931
932done:
Tao Baoba9a42a2015-06-23 23:23:33 -0700933 for (int i = 0; i < argc; ++i) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700934 free(args[i]);
935 }
936 free(args);
937
938 if (result != NULL) {
939 return result;
940 }
941
942 if (bad > 0) {
943 return ErrorAbort(state, "%s: some changes failed", name);
944 }
945
946 return StringValue(strdup(""));
947}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700948
Doug Zongker512536a2010-02-17 16:11:44 -0800949Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700950 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700951 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700952 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700953 char* key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700954 if (key == NULL) return NULL;
955
956 char value[PROPERTY_VALUE_MAX];
957 property_get(key, value, "");
958 free(key);
959
Doug Zongker512536a2010-02-17 16:11:44 -0800960 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700961}
962
963
Doug Zongker47cace92009-06-18 10:11:50 -0700964// file_getprop(file, key)
965//
966// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700967// per line. # comment lines,blank lines, lines without '=' ignored),
968// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800969Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700970 char* result = NULL;
971 char* buffer = NULL;
972 char* filename;
973 char* key;
974 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
975 return NULL;
976 }
977
978 struct stat st;
979 if (stat(filename, &st) < 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700980 ErrorAbort(state, "%s: failed to stat \"%s\": %s", name, filename, strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700981 goto done;
982 }
983
984#define MAX_FILE_GETPROP_SIZE 65536
985
986 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700987 ErrorAbort(state, "%s too large for %s (max %d)", filename, name, MAX_FILE_GETPROP_SIZE);
Doug Zongker47cace92009-06-18 10:11:50 -0700988 goto done;
989 }
990
Tao Baoba9a42a2015-06-23 23:23:33 -0700991 buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700992 if (buffer == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700993 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700994 goto done;
995 }
996
Tao Baoba9a42a2015-06-23 23:23:33 -0700997 FILE* f;
998 f = fopen(filename, "rb");
Doug Zongker47cace92009-06-18 10:11:50 -0700999 if (f == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001000 ErrorAbort(state, "%s: failed to open %s: %s", name, filename, strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -07001001 goto done;
1002 }
1003
Tao Bao5701d582015-09-24 10:56:48 -07001004 if (fread(buffer, 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
Doug Zongkera23075f2012-08-06 16:19:09 -07001005 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -07001006 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -07001007 fclose(f);
1008 goto done;
1009 }
1010 buffer[st.st_size] = '\0';
1011
1012 fclose(f);
1013
Tao Baoba9a42a2015-06-23 23:23:33 -07001014 char* line;
1015 line = strtok(buffer, "\n");
Doug Zongker47cace92009-06-18 10:11:50 -07001016 do {
1017 // skip whitespace at start of line
1018 while (*line && isspace(*line)) ++line;
1019
1020 // comment or blank line: skip to next line
1021 if (*line == '\0' || *line == '#') continue;
1022
1023 char* equal = strchr(line, '=');
1024 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001025 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001026 }
1027
1028 // trim whitespace between key and '='
1029 char* key_end = equal-1;
1030 while (key_end > line && isspace(*key_end)) --key_end;
1031 key_end[1] = '\0';
1032
1033 // not the key we're looking for
1034 if (strcmp(key, line) != 0) continue;
1035
1036 // skip whitespace after the '=' to the start of the value
1037 char* val_start = equal+1;
1038 while(*val_start && isspace(*val_start)) ++val_start;
1039
1040 // trim trailing whitespace
1041 char* val_end = val_start + strlen(val_start)-1;
1042 while (val_end > val_start && isspace(*val_end)) --val_end;
1043 val_end[1] = '\0';
1044
1045 result = strdup(val_start);
1046 break;
1047
1048 } while ((line = strtok(NULL, "\n")));
1049
1050 if (result == NULL) result = strdup("");
1051
1052 done:
1053 free(filename);
1054 free(key);
1055 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001056 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001057}
1058
Doug Zongker179b2d92011-04-12 15:49:04 -07001059// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001060Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001061 char* result = NULL;
1062
Doug Zongker179b2d92011-04-12 15:49:04 -07001063 Value* partition_value;
1064 Value* contents;
1065 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001066 return NULL;
1067 }
1068
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001069 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001070 if (partition_value->type != VAL_STRING) {
1071 ErrorAbort(state, "partition argument to %s must be string", name);
1072 goto done;
1073 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001074 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001075 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001076 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001077 goto done;
1078 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001079 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001080 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001081 goto done;
1082 }
1083
1084 mtd_scan_partitions();
Tao Baoba9a42a2015-06-23 23:23:33 -07001085 const MtdPartition* mtd;
1086 mtd = mtd_find_partition_by_name(partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001087 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001088 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001089 result = strdup("");
1090 goto done;
1091 }
1092
Tao Baoba9a42a2015-06-23 23:23:33 -07001093 MtdWriteContext* ctx;
1094 ctx = mtd_write_partition(mtd);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001095 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001096 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001097 name, partition);
1098 result = strdup("");
1099 goto done;
1100 }
1101
1102 bool success;
1103
Doug Zongker179b2d92011-04-12 15:49:04 -07001104 if (contents->type == VAL_STRING) {
1105 // we're given a filename as the contents
1106 char* filename = contents->data;
1107 FILE* f = fopen(filename, "rb");
1108 if (f == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001109 printf("%s: can't open %s: %s\n", name, filename, strerror(errno));
Doug Zongker179b2d92011-04-12 15:49:04 -07001110 result = strdup("");
1111 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001112 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001113
1114 success = true;
Tao Baoba9a42a2015-06-23 23:23:33 -07001115 char* buffer = reinterpret_cast<char*>(malloc(BUFSIZ));
Doug Zongker179b2d92011-04-12 15:49:04 -07001116 int read;
1117 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1118 int wrote = mtd_write_data(ctx, buffer, read);
1119 success = success && (wrote == read);
1120 }
1121 free(buffer);
1122 fclose(f);
1123 } else {
1124 // we're given a blob as the contents
1125 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1126 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001127 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001128 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001129 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001130 partition, strerror(errno));
1131 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001132
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001133 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001134 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001135 }
1136 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001137 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001138 }
1139
Doug Zongker179b2d92011-04-12 15:49:04 -07001140 printf("%s %s partition\n",
1141 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001142
1143 result = success ? partition : strdup("");
1144
1145done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001146 if (result != partition) FreeValue(partition_value);
1147 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001148 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001149}
1150
Doug Zongker8edb00c2009-06-11 17:21:44 -07001151// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001152Value* ApplyPatchSpaceFn(const char* name, State* state,
1153 int argc, Expr* argv[]) {
1154 char* bytes_str;
1155 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1156 return NULL;
1157 }
1158
Tao Baob15fd222015-09-24 11:10:51 -07001159 size_t bytes;
1160 if (!android::base::ParseUint(bytes_str, &bytes)) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001161 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n", name, bytes_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001162 free(bytes_str);
Tao Baob15fd222015-09-24 11:10:51 -07001163 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001164 }
1165
1166 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1167}
1168
Doug Zongker52b40362014-02-10 15:30:30 -08001169// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1170
Doug Zongker512536a2010-02-17 16:11:44 -08001171Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001172 if (argc < 6 || (argc % 2) == 1) {
1173 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1174 "even number, got %d",
1175 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001176 }
1177
Doug Zongkerc4351c72010-02-22 14:46:32 -08001178 char* source_filename;
1179 char* target_filename;
1180 char* target_sha1;
1181 char* target_size_str;
1182 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1183 &target_sha1, &target_size_str) < 0) {
1184 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001185 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001186
Tao Baob15fd222015-09-24 11:10:51 -07001187 size_t target_size;
1188 if (!android::base::ParseUint(target_size_str, &target_size)) {
1189 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count", name, target_size_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001190 free(source_filename);
1191 free(target_filename);
1192 free(target_sha1);
1193 free(target_size_str);
Tao Baob15fd222015-09-24 11:10:51 -07001194 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001195 }
1196
1197 int patchcount = (argc-4) / 2;
Yabin Cui64be2132016-02-03 18:16:02 -08001198 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc-4, argv+4),
1199 free);
1200 if (!arg_values) {
1201 return nullptr;
1202 }
1203 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patch_shas;
1204 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patches;
1205 // Protect values by unique_ptrs first to get rid of memory leak.
1206 for (int i = 0; i < patchcount * 2; i += 2) {
1207 patch_shas.emplace_back(arg_values.get()[i], FreeValue);
1208 patches.emplace_back(arg_values.get()[i+1], FreeValue);
1209 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001210
Yabin Cui64be2132016-02-03 18:16:02 -08001211 for (int i = 0; i < patchcount; ++i) {
1212 if (patch_shas[i]->type != VAL_STRING) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001213 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001214 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001215 }
Yabin Cui64be2132016-02-03 18:16:02 -08001216 if (patches[i]->type != VAL_BLOB) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001217 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001218 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001219 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001220 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001221
Yabin Cui64be2132016-02-03 18:16:02 -08001222 std::vector<char*> patch_sha_str;
1223 std::vector<Value*> patch_ptrs;
1224 for (int i = 0; i < patchcount; ++i) {
1225 patch_sha_str.push_back(patch_shas[i]->data);
1226 patch_ptrs.push_back(patches[i].get());
Doug Zongker8edb00c2009-06-11 17:21:44 -07001227 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001228
1229 int result = applypatch(source_filename, target_filename,
1230 target_sha1, target_size,
Yabin Cui64be2132016-02-03 18:16:02 -08001231 patchcount, patch_sha_str.data(), patch_ptrs.data(), NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001232
1233 return StringValue(strdup(result == 0 ? "t" : ""));
1234}
1235
1236// apply_patch_check(file, [sha1_1, ...])
1237Value* ApplyPatchCheckFn(const char* name, State* state,
1238 int argc, Expr* argv[]) {
1239 if (argc < 1) {
1240 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1241 name, argc);
1242 }
1243
1244 char* filename;
1245 if (ReadArgs(state, argv, 1, &filename) < 0) {
1246 return NULL;
1247 }
1248
1249 int patchcount = argc-1;
1250 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1251
1252 int result = applypatch_check(filename, patchcount, sha1s);
1253
1254 int i;
1255 for (i = 0; i < patchcount; ++i) {
1256 free(sha1s[i]);
1257 }
1258 free(sha1s);
1259
1260 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001261}
1262
Tao Bao1107d962015-09-09 17:16:55 -07001263// This is the updater side handler for ui_print() in edify script. Contents
1264// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001265Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001266 char** args = ReadVarArgs(state, argc, argv);
1267 if (args == NULL) {
1268 return NULL;
1269 }
1270
Tao Bao1107d962015-09-09 17:16:55 -07001271 std::string buffer;
1272 for (int i = 0; i < argc; ++i) {
1273 buffer += args[i];
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001274 free(args[i]);
1275 }
1276 free(args);
Tao Bao1107d962015-09-09 17:16:55 -07001277
1278 buffer += "\n";
Michael Runged4a63422014-10-22 19:48:41 -07001279 uiPrint(state, buffer);
Tao Bao1107d962015-09-09 17:16:55 -07001280 return StringValue(strdup(buffer.c_str()));
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001281}
1282
Doug Zongkerd0181b82011-10-19 10:51:12 -07001283Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1284 if (argc != 0) {
1285 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1286 }
1287 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1288 return StringValue(strdup("t"));
1289}
1290
Doug Zongker512536a2010-02-17 16:11:44 -08001291Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001292 if (argc < 1) {
1293 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1294 }
1295 char** args = ReadVarArgs(state, argc, argv);
1296 if (args == NULL) {
1297 return NULL;
1298 }
1299
Tao Baoba9a42a2015-06-23 23:23:33 -07001300 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001301 memcpy(args2, args, sizeof(char*) * argc);
1302 args2[argc] = NULL;
1303
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001304 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001305
1306 pid_t child = fork();
1307 if (child == 0) {
1308 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001309 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001310 _exit(1);
1311 }
1312 int status;
1313 waitpid(child, &status, 0);
1314 if (WIFEXITED(status)) {
1315 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001316 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001317 WEXITSTATUS(status));
1318 }
1319 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001320 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001321 WTERMSIG(status));
1322 }
1323
1324 int i;
1325 for (i = 0; i < argc; ++i) {
1326 free(args[i]);
1327 }
1328 free(args);
1329 free(args2);
1330
1331 char buffer[20];
1332 sprintf(buffer, "%d", status);
1333
Doug Zongker512536a2010-02-17 16:11:44 -08001334 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001335}
1336
Doug Zongker512536a2010-02-17 16:11:44 -08001337// sha1_check(data)
1338// to return the sha1 of the data (given in the format returned by
1339// read_file).
1340//
1341// sha1_check(data, sha1_hex, [sha1_hex, ...])
1342// returns the sha1 of the file if it matches any of the hex
1343// strings passed, or "" if it does not equal any of them.
1344//
1345Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1346 if (argc < 1) {
1347 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1348 }
1349
Yabin Cui64be2132016-02-03 18:16:02 -08001350 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc, argv), free);
1351 if (arg_values == nullptr) {
1352 return nullptr;
1353 }
1354 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> args;
1355 for (int i = 0; i < argc; ++i) {
1356 args.emplace_back(arg_values.get()[i], FreeValue);
Doug Zongker512536a2010-02-17 16:11:44 -08001357 }
1358
1359 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001360 return StringValue(strdup(""));
1361 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001362 uint8_t digest[SHA_DIGEST_LENGTH];
1363 SHA1(reinterpret_cast<uint8_t*>(args[0]->data), args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001364
1365 if (argc == 1) {
1366 return StringValue(PrintSha1(digest));
1367 }
1368
1369 int i;
Yabin Cui64be2132016-02-03 18:16:02 -08001370 uint8_t arg_digest[SHA_DIGEST_LENGTH];
Doug Zongker512536a2010-02-17 16:11:44 -08001371 for (i = 1; i < argc; ++i) {
1372 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001373 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001374 name, i);
1375 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1376 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001377 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1378 name, args[i]->data);
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001379 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001380 break;
1381 }
Doug Zongker512536a2010-02-17 16:11:44 -08001382 }
1383 if (i >= argc) {
1384 // Didn't match any of the hex strings; return false.
1385 return StringValue(strdup(""));
1386 }
Yabin Cui64be2132016-02-03 18:16:02 -08001387 // Found a match.
1388 return args[i].release();
Doug Zongker512536a2010-02-17 16:11:44 -08001389}
1390
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001391// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001392// is actually a FileContents*).
1393Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1394 if (argc != 1) {
1395 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1396 }
1397 char* filename;
1398 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1399
Tao Baoba9a42a2015-06-23 23:23:33 -07001400 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -08001401 v->type = VAL_BLOB;
1402
1403 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001404 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001405 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001406 v->size = -1;
1407 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001408 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001409 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001410 }
1411
1412 v->size = fc.size;
1413 v->data = (char*)fc.data;
1414
1415 free(filename);
1416 return v;
1417}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001418
Doug Zongkerc87bab12013-11-25 13:53:25 -08001419// Immediately reboot the device. Recovery is not finished normally,
1420// so if you reboot into recovery it will re-start applying the
1421// current package (because nothing has cleared the copy of the
1422// arguments stored in the BCB).
1423//
1424// The argument is the partition name passed to the android reboot
1425// property. It can be "recovery" to boot from the recovery
1426// partition, or "" (empty string) to boot from the regular boot
1427// partition.
1428Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1429 if (argc != 2) {
1430 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1431 }
1432
1433 char* filename;
1434 char* property;
1435 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1436
1437 char buffer[80];
1438
1439 // zero out the 'command' field of the bootloader message.
1440 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1441 FILE* f = fopen(filename, "r+b");
1442 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1443 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1444 fclose(f);
1445 free(filename);
1446
1447 strcpy(buffer, "reboot,");
1448 if (property != NULL) {
1449 strncat(buffer, property, sizeof(buffer)-10);
1450 }
1451
1452 property_set(ANDROID_RB_PROPERTY, buffer);
1453
1454 sleep(5);
1455 free(property);
1456 ErrorAbort(state, "%s() failed to reboot", name);
1457 return NULL;
1458}
1459
1460// Store a string value somewhere that future invocations of recovery
1461// can access it. This value is called the "stage" and can be used to
1462// drive packages that need to do reboots in the middle of
1463// installation and keep track of where they are in the multi-stage
1464// install.
1465//
1466// The first argument is the block device for the misc partition
1467// ("/misc" in the fstab), which is where this value is stored. The
1468// second argument is the string to store; it should not exceed 31
1469// bytes.
1470Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1471 if (argc != 2) {
1472 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1473 }
1474
1475 char* filename;
1476 char* stagestr;
1477 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1478
1479 // Store this value in the misc partition, immediately after the
1480 // bootloader message that the main recovery uses to save its
1481 // arguments in case of the device restarting midway through
1482 // package installation.
1483 FILE* f = fopen(filename, "r+b");
1484 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1485 int to_write = strlen(stagestr)+1;
1486 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1487 if (to_write > max_size) {
1488 to_write = max_size;
1489 stagestr[max_size-1] = 0;
1490 }
1491 fwrite(stagestr, to_write, 1, f);
1492 fclose(f);
1493
1494 free(stagestr);
1495 return StringValue(filename);
1496}
1497
1498// Return the value most recently saved with SetStageFn. The argument
1499// is the block device for the misc partition.
1500Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001501 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001502 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1503 }
1504
1505 char* filename;
1506 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1507
1508 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1509 FILE* f = fopen(filename, "rb");
1510 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1511 fread(buffer, sizeof(buffer), 1, f);
1512 fclose(f);
1513 buffer[sizeof(buffer)-1] = '\0';
1514
1515 return StringValue(strdup(buffer));
1516}
1517
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001518Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1519 if (argc != 2) {
1520 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1521 }
1522
1523 char* filename;
1524 char* len_str;
1525 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1526
Tao Baob15fd222015-09-24 11:10:51 -07001527 size_t len;
1528 android::base::ParseUint(len_str, &len);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001529 int fd = open(filename, O_WRONLY, 0644);
1530 int success = wipe_block_device(fd, len);
1531
1532 free(filename);
1533 free(len_str);
1534
1535 close(fd);
1536
1537 return StringValue(strdup(success ? "t" : ""));
1538}
1539
Doug Zongkerc704e062014-05-23 08:40:35 -07001540Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1541 if (argc != 0) {
1542 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1543 }
1544 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1545 fprintf(ui->cmd_pipe, "enable_reboot\n");
1546 return StringValue(strdup("t"));
1547}
1548
Michael Rungeacf47db2014-11-21 00:12:28 -08001549Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1550 if (argc == 0) {
1551 return ErrorAbort(state, "%s() expects args, got %d", name, argc);
1552 }
1553
1554 char** args = ReadVarArgs(state, argc, argv);
1555 if (args == NULL) {
1556 return ErrorAbort(state, "%s() could not read args", name);
1557 }
1558
Tao Baoba9a42a2015-06-23 23:23:33 -07001559 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Michael Rungeacf47db2014-11-21 00:12:28 -08001560 // Tune2fs expects the program name as its args[0]
1561 args2[0] = strdup(name);
Tao Baoba9a42a2015-06-23 23:23:33 -07001562 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001563 args2[i + 1] = args[i];
1564 }
1565 int result = tune2fs_main(argc + 1, args2);
Tao Baoba9a42a2015-06-23 23:23:33 -07001566 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001567 free(args[i]);
1568 }
1569 free(args);
1570
1571 free(args2[0]);
1572 free(args2);
1573 if (result != 0) {
1574 return ErrorAbort(state, "%s() returned error code %d", name, result);
1575 }
1576 return StringValue(strdup("t"));
1577}
1578
Doug Zongker9931f7f2009-06-10 14:11:53 -07001579void RegisterInstallFunctions() {
1580 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001581 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001582 RegisterFunction("unmount", UnmountFn);
1583 RegisterFunction("format", FormatFn);
1584 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001585 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001586 RegisterFunction("delete", DeleteFn);
1587 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001588 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1589 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001590 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001591
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001592 // Usage:
1593 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1594 // Example:
1595 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1596 RegisterFunction("set_metadata", SetMetadataFn);
1597
1598 // Usage:
1599 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1600 // Example:
1601 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1602 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1603
Doug Zongker8edb00c2009-06-11 17:21:44 -07001604 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001605 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001606 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001607
1608 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001609 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1610 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001611
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001612 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001613
Doug Zongker512536a2010-02-17 16:11:44 -08001614 RegisterFunction("read_file", ReadFileFn);
1615 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001616 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001617
Doug Zongkerd0181b82011-10-19 10:51:12 -07001618 RegisterFunction("wipe_cache", WipeCacheFn);
1619
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001620 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001621
1622 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001623
1624 RegisterFunction("reboot_now", RebootNowFn);
1625 RegisterFunction("get_stage", GetStageFn);
1626 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001627
1628 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001629 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001630}