blob: bc4cca91363ea35a4f0ee05e52452bd77ebc91b3 [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"
Jed Estepff6df892015-12-15 16:04:53 -080054#include "ota_io.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070055#include "updater.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080056#include "install.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080057#include "tune2fs.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070058
Doug Zongker3d177d02010-07-01 09:18:44 -070059#ifdef USE_EXT4
60#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080061#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070062#endif
63
Tao Bao1107d962015-09-09 17:16:55 -070064// Send over the buffer to recovery though the command pipe.
65static void uiPrint(State* state, const std::string& buffer) {
66 UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070067
Tao Bao1107d962015-09-09 17:16:55 -070068 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
69 // So skip sending empty strings to UI.
70 std::vector<std::string> lines = android::base::Split(buffer, "\n");
71 for (auto& line: lines) {
72 if (!line.empty()) {
73 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
74 fprintf(ui->cmd_pipe, "ui_print\n");
75 }
76 }
77
78 // On the updater side, we need to dump the contents to stderr (which has
79 // been redirected to the log file). Because the recovery will only print
80 // the contents to screen when processing pipe command ui_print.
81 fprintf(stderr, "%s", buffer.c_str());
Michael Runged4a63422014-10-22 19:48:41 -070082}
83
84__attribute__((__format__(printf, 2, 3))) __nonnull((2))
85void uiPrintf(State* state, const char* format, ...) {
Tao Bao1107d962015-09-09 17:16:55 -070086 std::string error_msg;
87
Michael Runged4a63422014-10-22 19:48:41 -070088 va_list ap;
89 va_start(ap, format);
Tao Bao1107d962015-09-09 17:16:55 -070090 android::base::StringAppendV(&error_msg, format, ap);
Michael Runged4a63422014-10-22 19:48:41 -070091 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070092
Michael Runged4a63422014-10-22 19:48:41 -070093 uiPrint(state, error_msg);
94}
95
Doug Zongker52b40362014-02-10 15:30:30 -080096// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070097char* PrintSha1(const uint8_t* digest) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +080098 char* buffer = reinterpret_cast<char*>(malloc(SHA_DIGEST_LENGTH*2 + 1));
Doug Zongker52b40362014-02-10 15:30:30 -080099 const char* alphabet = "0123456789abcdef";
Tao Baoba9a42a2015-06-23 23:23:33 -0700100 size_t i;
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800101 for (i = 0; i < SHA_DIGEST_LENGTH; ++i) {
Doug Zongker52b40362014-02-10 15:30:30 -0800102 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
103 buffer[i*2+1] = alphabet[digest[i] & 0xf];
104 }
105 buffer[i*2] = '\0';
106 return buffer;
107}
108
Doug Zongker3d177d02010-07-01 09:18:44 -0700109// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700110//
Doug Zongker3d177d02010-07-01 09:18:44 -0700111// fs_type="yaffs2" partition_type="MTD" location=partition
112// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800113Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700114 char* result = NULL;
Michael Runge168f7772014-10-22 17:05:08 -0700115 if (argc != 4 && argc != 5) {
116 return ErrorAbort(state, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700117 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700118 char* fs_type;
119 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700120 char* location;
121 char* mount_point;
Michael Runge168f7772014-10-22 17:05:08 -0700122 char* mount_options;
123 bool has_mount_options;
124 if (argc == 5) {
125 has_mount_options = true;
126 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
127 &location, &mount_point, &mount_options) < 0) {
128 return NULL;
129 }
130 } else {
131 has_mount_options = false;
132 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700133 &location, &mount_point) < 0) {
Michael Runge168f7772014-10-22 17:05:08 -0700134 return NULL;
135 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700136 }
137
Doug Zongker3d177d02010-07-01 09:18:44 -0700138 if (strlen(fs_type) == 0) {
139 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
140 goto done;
141 }
142 if (strlen(partition_type) == 0) {
143 ErrorAbort(state, "partition_type argument to %s() can't be empty",
144 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700145 goto done;
146 }
147 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700148 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700149 goto done;
150 }
151 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700152 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700153 goto done;
154 }
155
Tao Baoba9a42a2015-06-23 23:23:33 -0700156 {
157 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500158
Tao Baoba9a42a2015-06-23 23:23:33 -0700159 if (sehandle) {
160 selabel_lookup(sehandle, &secontext, mount_point, 0755);
161 setfscreatecon(secontext);
162 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500163
Tao Baoba9a42a2015-06-23 23:23:33 -0700164 mkdir(mount_point, 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700165
Tao Baoba9a42a2015-06-23 23:23:33 -0700166 if (secontext) {
167 freecon(secontext);
168 setfscreatecon(NULL);
169 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500170 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500171
Doug Zongker3d177d02010-07-01 09:18:44 -0700172 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700173 mtd_scan_partitions();
174 const MtdPartition* mtd;
175 mtd = mtd_find_partition_by_name(location);
176 if (mtd == NULL) {
Tao Bao1107d962015-09-09 17:16:55 -0700177 uiPrintf(state, "%s: no mtd partition named \"%s\"\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700178 name, location);
179 result = strdup("");
180 goto done;
181 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700182 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700183 uiPrintf(state, "mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700184 location, strerror(errno));
185 result = strdup("");
186 goto done;
187 }
188 result = mount_point;
189 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700190 if (mount(location, mount_point, fs_type,
Michael Runge168f7772014-10-22 17:05:08 -0700191 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
192 has_mount_options ? mount_options : "") < 0) {
Michael Runge5ddf4292014-10-24 14:14:41 -0700193 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700194 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700195 result = strdup("");
196 } else {
197 result = mount_point;
198 }
199 }
200
201done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700202 free(fs_type);
203 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700204 free(location);
205 if (result != mount_point) free(mount_point);
Michael Runge168f7772014-10-22 17:05:08 -0700206 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800207 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700208}
209
Doug Zongker8edb00c2009-06-11 17:21:44 -0700210
211// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800212Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700213 char* result = NULL;
214 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700215 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700216 }
217 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700218 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700219 return NULL;
220 }
221 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700222 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700223 goto done;
224 }
225
226 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700227 {
228 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
229 if (vol == NULL) {
230 result = strdup("");
231 } else {
232 result = mount_point;
233 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700234 }
235
236done:
237 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800238 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700239}
240
241
Doug Zongker512536a2010-02-17 16:11:44 -0800242Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700243 char* result = NULL;
244 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700245 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700246 }
247 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700248 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700249 return NULL;
250 }
251 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700252 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700253 goto done;
254 }
255
256 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700257 {
258 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
259 if (vol == NULL) {
260 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
261 result = strdup("");
262 } else {
263 int ret = unmount_mounted_volume(vol);
264 if (ret != 0) {
265 uiPrintf(state, "unmount of %s failed (%d): %s\n",
266 mount_point, ret, strerror(errno));
267 }
268 result = mount_point;
Michael Runge5ddf4292014-10-24 14:14:41 -0700269 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700270 }
271
272done:
273 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800274 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700275}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700276
JP Abgrall37aedb32014-06-16 19:07:39 -0700277static int exec_cmd(const char* path, char* const argv[]) {
278 int status;
279 pid_t child;
280 if ((child = vfork()) == 0) {
281 execv(path, argv);
282 _exit(-1);
283 }
284 waitpid(child, &status, 0);
285 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
286 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
287 }
288 return WEXITSTATUS(status);
289}
290
Doug Zongker8edb00c2009-06-11 17:21:44 -0700291
Stephen Smalley779701d2012-02-09 14:13:23 -0500292// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700293//
Stephen Smalley779701d2012-02-09 14:13:23 -0500294// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
295// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700296// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
297// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800298// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700299// 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 -0800300Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700301 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400302 if (argc != 5) {
303 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700304 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700305 char* fs_type;
306 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700307 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800308 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400309 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500310
Stephen Smalley779701d2012-02-09 14:13:23 -0500311 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
312 return NULL;
313 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700314
Doug Zongker3d177d02010-07-01 09:18:44 -0700315 if (strlen(fs_type) == 0) {
316 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
317 goto done;
318 }
319 if (strlen(partition_type) == 0) {
320 ErrorAbort(state, "partition_type argument to %s() can't be empty",
321 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700322 goto done;
323 }
324 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700325 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700326 goto done;
327 }
328
Stephen Smalley516e4e22012-04-03 13:35:11 -0400329 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500330 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
331 goto done;
332 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500333
Doug Zongker3d177d02010-07-01 09:18:44 -0700334 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700335 mtd_scan_partitions();
336 const MtdPartition* mtd = mtd_find_partition_by_name(location);
337 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700338 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700339 name, location);
340 result = strdup("");
341 goto done;
342 }
343 MtdWriteContext* ctx = mtd_write_partition(mtd);
344 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700345 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700346 result = strdup("");
347 goto done;
348 }
349 if (mtd_erase_blocks(ctx, -1) == -1) {
350 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700351 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700352 result = strdup("");
353 goto done;
354 }
355 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700356 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700357 result = strdup("");
358 goto done;
359 }
360 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700361#ifdef USE_EXT4
362 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500363 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700364 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700365 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700366 name, status, location);
367 result = strdup("");
368 goto done;
369 }
370 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700371 } else if (strcmp(fs_type, "f2fs") == 0) {
372 char *num_sectors;
373 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
374 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
375 result = strdup("");
376 goto done;
377 }
378 const char *f2fs_path = "/sbin/mkfs.f2fs";
379 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
380 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
381 free(num_sectors);
382 if (status != 0) {
383 printf("%s: mkfs.f2fs failed (%d) on %s",
384 name, status, location);
385 result = strdup("");
386 goto done;
387 }
388 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700389#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700390 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700391 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700392 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700393 }
394
395done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700396 free(fs_type);
397 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700398 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800399 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700400}
401
Michael Rungece7ca712013-11-06 17:42:20 -0800402Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
403 char* result = NULL;
404 if (argc != 2) {
405 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
406 }
407
408 char* src_name;
409 char* dst_name;
410
411 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
412 return NULL;
413 }
414 if (strlen(src_name) == 0) {
415 ErrorAbort(state, "src_name argument to %s() can't be empty", name);
416 goto done;
417 }
418 if (strlen(dst_name) == 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700419 ErrorAbort(state, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800420 goto done;
421 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700422 if (make_parents(dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700423 ErrorAbort(state, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700424 dst_name, strerror(errno));
Michael Runge2f0ef732014-10-22 14:28:23 -0700425 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
426 // File was already moved
427 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700428 } else if (rename(src_name, dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700429 ErrorAbort(state, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800430 src_name, dst_name, strerror(errno));
431 } else {
432 result = dst_name;
433 }
434
435done:
436 free(src_name);
437 if (result != dst_name) free(dst_name);
438 return StringValue(result);
439}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700440
Doug Zongker512536a2010-02-17 16:11:44 -0800441Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700442 char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
443 for (int i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700444 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700445 if (paths[i] == NULL) {
Yabin Cui64be2132016-02-03 18:16:02 -0800446 for (int j = 0; j < i; ++j) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700447 free(paths[j]);
448 }
449 free(paths);
450 return NULL;
451 }
452 }
453
454 bool recursive = (strcmp(name, "delete_recursive") == 0);
455
456 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700457 for (int i = 0; i < argc; ++i) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700458 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
459 ++success;
460 free(paths[i]);
461 }
462 free(paths);
463
464 char buffer[10];
465 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800466 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700467}
468
Doug Zongker8edb00c2009-06-11 17:21:44 -0700469
Doug Zongker512536a2010-02-17 16:11:44 -0800470Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700471 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700472 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700473 }
474 char* frac_str;
475 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700476 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700477 return NULL;
478 }
479
480 double frac = strtod(frac_str, NULL);
Tao Baob15fd222015-09-24 11:10:51 -0700481 int sec;
482 android::base::ParseInt(sec_str, &sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700483
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700484 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700485 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
486
Doug Zongker9931f7f2009-06-10 14:11:53 -0700487 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800488 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700489}
490
Doug Zongker512536a2010-02-17 16:11:44 -0800491Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700492 if (argc != 1) {
493 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
494 }
495 char* frac_str;
496 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
497 return NULL;
498 }
499
500 double frac = strtod(frac_str, NULL);
501
502 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
503 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
504
Doug Zongker512536a2010-02-17 16:11:44 -0800505 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700506}
507
Doug Zongker8edb00c2009-06-11 17:21:44 -0700508// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800509Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700510 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700511 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700512 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700513 }
514 char* zip_path;
515 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700516 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700517
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700518 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700519
520 // To create a consistent system image, never use the clock for timestamps.
521 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
522
523 bool success = mzExtractRecursive(za, zip_path, dest_path,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000524 &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500525 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700526 free(zip_path);
527 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800528 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700529}
530
Doug Zongker8edb00c2009-06-11 17:21:44 -0700531
532// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800533// or
534// package_extract_file(package_path)
535// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800536// function (the char* returned is actually a FileContents*).
537Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700538 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700539 if (argc < 1 || argc > 2) {
540 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800541 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700542 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700543 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700544
Doug Zongker5f875bf2014-08-22 14:53:43 -0700545 if (argc == 2) {
546 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800547
548 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700549
Doug Zongker6aece332010-02-01 14:40:12 -0800550 char* zip_path;
551 char* dest_path;
Doug Zongker5f875bf2014-08-22 14:53:43 -0700552 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800553
Doug Zongker6aece332010-02-01 14:40:12 -0800554 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
555 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700556 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800557 goto done2;
558 }
559
Tao Baoba9a42a2015-06-23 23:23:33 -0700560 {
Jed Estepa7b9a462015-12-15 16:04:53 -0800561 int fd = TEMP_FAILURE_RETRY(ota_open(dest_path, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
Tao Baod3cac342015-12-14 15:53:25 -0800562 S_IRUSR | S_IWUSR));
563 if (fd == -1) {
564 printf("%s: can't open %s for write: %s\n", name, dest_path, strerror(errno));
Tao Baoba9a42a2015-06-23 23:23:33 -0700565 goto done2;
566 }
Tao Baod3cac342015-12-14 15:53:25 -0800567 success = mzExtractZipEntryToFile(za, entry, fd);
Jed Estepa7b9a462015-12-15 16:04:53 -0800568 if (ota_fsync(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800569 printf("fsync of \"%s\" failed: %s\n", dest_path, strerror(errno));
570 success = false;
571 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800572 if (ota_close(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800573 printf("close of \"%s\" failed: %s\n", dest_path, strerror(errno));
574 success = false;
575 }
Doug Zongker6aece332010-02-01 14:40:12 -0800576 }
Doug Zongker6aece332010-02-01 14:40:12 -0800577
578 done2:
579 free(zip_path);
580 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800581 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800582 } else {
583 // The one-argument version returns the contents of the file
584 // as the result.
585
586 char* zip_path;
Yabin Cui64be2132016-02-03 18:16:02 -0800587 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
588
Tao Baoba9a42a2015-06-23 23:23:33 -0700589 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -0800590 v->type = VAL_BLOB;
591 v->size = -1;
592 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800593
Doug Zongker6aece332010-02-01 14:40:12 -0800594 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
595 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
596 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700597 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800598 goto done1;
599 }
600
Doug Zongker512536a2010-02-17 16:11:44 -0800601 v->size = mzGetZipEntryUncompLen(entry);
Tao Baoba9a42a2015-06-23 23:23:33 -0700602 v->data = reinterpret_cast<char*>(malloc(v->size));
Doug Zongker512536a2010-02-17 16:11:44 -0800603 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700604 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800605 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800606 goto done1;
607 }
608
Doug Zongker512536a2010-02-17 16:11:44 -0800609 success = mzExtractZipEntryToBuffer(za, entry,
610 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800611
612 done1:
613 free(zip_path);
614 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800615 free(v->data);
616 v->data = NULL;
617 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800618 }
Doug Zongker512536a2010-02-17 16:11:44 -0800619 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700620 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700621}
622
Doug Zongkera23075f2012-08-06 16:19:09 -0700623// Create all parent directories of name, if necessary.
624static int make_parents(char* name) {
625 char* p;
626 for (p = name + (strlen(name)-1); p > name; --p) {
627 if (*p != '/') continue;
628 *p = '\0';
629 if (make_parents(name) < 0) return -1;
630 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700631 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700632 *p = '/';
633 if (result == 0 || errno == EEXIST) {
634 // successfully created or already existed; we're done
635 return 0;
636 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700637 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700638 return -1;
639 }
640 }
641 return 0;
642}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700643
Doug Zongker9931f7f2009-06-10 14:11:53 -0700644// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700645// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800646Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700647 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700648 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700649 }
650 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700651 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700652 if (target == NULL) return NULL;
653
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700654 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700655 if (srcs == NULL) {
656 free(target);
657 return NULL;
658 }
659
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700660 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700661 int i;
662 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700663 if (unlink(srcs[i]) < 0) {
664 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700665 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700666 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700667 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700668 }
669 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700670 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700671 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700672 name, srcs[i], target);
673 ++bad;
674 }
Doug Zongker60babf82009-09-18 15:11:24 -0700675 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700676 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700677 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700678 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700679 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700680 free(srcs[i]);
681 }
682 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700683 if (bad) {
684 return ErrorAbort(state, "%s: some symlinks failed", name);
685 }
Doug Zongker512536a2010-02-17 16:11:44 -0800686 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700687}
688
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700689struct perm_parsed_args {
690 bool has_uid;
691 uid_t uid;
692 bool has_gid;
693 gid_t gid;
694 bool has_mode;
695 mode_t mode;
696 bool has_fmode;
697 mode_t fmode;
698 bool has_dmode;
699 mode_t dmode;
700 bool has_selabel;
701 char* selabel;
702 bool has_capabilities;
703 uint64_t capabilities;
704};
705
Michael Runged4a63422014-10-22 19:48:41 -0700706static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700707 int i;
708 struct perm_parsed_args parsed;
709 int bad = 0;
710 static int max_warnings = 20;
711
712 memset(&parsed, 0, sizeof(parsed));
713
714 for (i = 1; i < argc; i += 2) {
715 if (strcmp("uid", args[i]) == 0) {
716 int64_t uid;
717 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
718 parsed.uid = uid;
719 parsed.has_uid = true;
720 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700721 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700722 bad++;
723 }
724 continue;
725 }
726 if (strcmp("gid", args[i]) == 0) {
727 int64_t gid;
728 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
729 parsed.gid = gid;
730 parsed.has_gid = true;
731 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700732 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700733 bad++;
734 }
735 continue;
736 }
737 if (strcmp("mode", args[i]) == 0) {
738 int32_t mode;
739 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
740 parsed.mode = mode;
741 parsed.has_mode = true;
742 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700743 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700744 bad++;
745 }
746 continue;
747 }
748 if (strcmp("dmode", args[i]) == 0) {
749 int32_t mode;
750 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
751 parsed.dmode = mode;
752 parsed.has_dmode = true;
753 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700754 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700755 bad++;
756 }
757 continue;
758 }
759 if (strcmp("fmode", args[i]) == 0) {
760 int32_t mode;
761 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
762 parsed.fmode = mode;
763 parsed.has_fmode = true;
764 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700765 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700766 bad++;
767 }
768 continue;
769 }
770 if (strcmp("capabilities", args[i]) == 0) {
771 int64_t capabilities;
772 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
773 parsed.capabilities = capabilities;
774 parsed.has_capabilities = true;
775 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700776 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700777 bad++;
778 }
779 continue;
780 }
781 if (strcmp("selabel", args[i]) == 0) {
782 if (args[i+1][0] != '\0') {
783 parsed.selabel = args[i+1];
784 parsed.has_selabel = true;
785 } else {
Michael Runged4a63422014-10-22 19:48:41 -0700786 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700787 bad++;
788 }
789 continue;
790 }
791 if (max_warnings != 0) {
792 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
793 max_warnings--;
794 if (max_warnings == 0) {
795 printf("ParsedPermArgs: suppressing further warnings\n");
796 }
797 }
798 }
799 return parsed;
800}
801
802static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700803 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700804 const char* filename,
805 const struct stat *statptr,
806 struct perm_parsed_args parsed)
807{
808 int bad = 0;
809
Nick Kralevich68802412014-10-23 20:36:42 -0700810 if (parsed.has_selabel) {
811 if (lsetfilecon(filename, parsed.selabel) != 0) {
812 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
813 filename, parsed.selabel, strerror(errno));
814 bad++;
815 }
816 }
817
Nick Kraleviche4612512013-09-10 15:34:19 -0700818 /* ignore symlinks */
819 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700820 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700821 }
822
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700823 if (parsed.has_uid) {
824 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700825 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
826 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700827 bad++;
828 }
829 }
830
831 if (parsed.has_gid) {
832 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700833 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
834 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700835 bad++;
836 }
837 }
838
839 if (parsed.has_mode) {
840 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700841 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
842 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700843 bad++;
844 }
845 }
846
847 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
848 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700849 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
850 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700851 bad++;
852 }
853 }
854
855 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
856 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700857 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700858 filename, parsed.fmode, strerror(errno));
859 bad++;
860 }
861 }
862
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700863 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
864 if (parsed.capabilities == 0) {
865 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
866 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700867 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700868 filename, parsed.capabilities, strerror(errno));
869 bad++;
870 }
871 } else {
872 struct vfs_cap_data cap_data;
873 memset(&cap_data, 0, sizeof(cap_data));
874 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
875 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
876 cap_data.data[0].inheritable = 0;
877 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
878 cap_data.data[1].inheritable = 0;
879 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700880 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
881 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700882 bad++;
883 }
884 }
885 }
886
887 return bad;
888}
889
890// nftw doesn't allow us to pass along context, so we need to use
891// global variables. *sigh*
892static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700893static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700894
895static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
896 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700897 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700898}
899
900static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700901 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700902 struct stat sb;
903 Value* result = NULL;
904
905 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
906
907 if ((argc % 2) != 1) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700908 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700909 }
910
911 char** args = ReadVarArgs(state, argc, argv);
912 if (args == NULL) return NULL;
913
914 if (lstat(args[0], &sb) == -1) {
915 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
916 goto done;
917 }
918
Tao Baoba9a42a2015-06-23 23:23:33 -0700919 {
920 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700921
Tao Baoba9a42a2015-06-23 23:23:33 -0700922 if (recursive) {
923 recursive_parsed_args = parsed;
924 recursive_state = state;
925 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
926 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
927 recursive_state = NULL;
928 } else {
929 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
930 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700931 }
932
933done:
Tao Baoba9a42a2015-06-23 23:23:33 -0700934 for (int i = 0; i < argc; ++i) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700935 free(args[i]);
936 }
937 free(args);
938
939 if (result != NULL) {
940 return result;
941 }
942
943 if (bad > 0) {
944 return ErrorAbort(state, "%s: some changes failed", name);
945 }
946
947 return StringValue(strdup(""));
948}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700949
Doug Zongker512536a2010-02-17 16:11:44 -0800950Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700951 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700952 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700953 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700954 char* key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700955 if (key == NULL) return NULL;
956
957 char value[PROPERTY_VALUE_MAX];
958 property_get(key, value, "");
959 free(key);
960
Doug Zongker512536a2010-02-17 16:11:44 -0800961 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700962}
963
964
Doug Zongker47cace92009-06-18 10:11:50 -0700965// file_getprop(file, key)
966//
967// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700968// per line. # comment lines,blank lines, lines without '=' ignored),
969// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800970Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700971 char* result = NULL;
972 char* buffer = NULL;
973 char* filename;
974 char* key;
975 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
976 return NULL;
977 }
978
979 struct stat st;
980 if (stat(filename, &st) < 0) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700981 ErrorAbort(state, "%s: failed to stat \"%s\": %s", name, filename, strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700982 goto done;
983 }
984
985#define MAX_FILE_GETPROP_SIZE 65536
986
987 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700988 ErrorAbort(state, "%s too large for %s (max %d)", filename, name, MAX_FILE_GETPROP_SIZE);
Doug Zongker47cace92009-06-18 10:11:50 -0700989 goto done;
990 }
991
Tao Baoba9a42a2015-06-23 23:23:33 -0700992 buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -0700993 if (buffer == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700994 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700995 goto done;
996 }
997
Tao Baoba9a42a2015-06-23 23:23:33 -0700998 FILE* f;
Jed Estepa7b9a462015-12-15 16:04:53 -0800999 f = ota_fopen(filename, "rb");
Doug Zongker47cace92009-06-18 10:11:50 -07001000 if (f == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001001 ErrorAbort(state, "%s: failed to open %s: %s", name, filename, strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -07001002 goto done;
1003 }
1004
Jed Estepa7b9a462015-12-15 16:04:53 -08001005 if (ota_fread(buffer, 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
Doug Zongkera23075f2012-08-06 16:19:09 -07001006 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -07001007 name, (long long)st.st_size+1, filename);
Jed Estepa7b9a462015-12-15 16:04:53 -08001008 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -07001009 goto done;
1010 }
1011 buffer[st.st_size] = '\0';
1012
Jed Estepa7b9a462015-12-15 16:04:53 -08001013 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -07001014
Tao Baoba9a42a2015-06-23 23:23:33 -07001015 char* line;
1016 line = strtok(buffer, "\n");
Doug Zongker47cace92009-06-18 10:11:50 -07001017 do {
1018 // skip whitespace at start of line
1019 while (*line && isspace(*line)) ++line;
1020
1021 // comment or blank line: skip to next line
1022 if (*line == '\0' || *line == '#') continue;
1023
1024 char* equal = strchr(line, '=');
1025 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001026 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001027 }
1028
1029 // trim whitespace between key and '='
1030 char* key_end = equal-1;
1031 while (key_end > line && isspace(*key_end)) --key_end;
1032 key_end[1] = '\0';
1033
1034 // not the key we're looking for
1035 if (strcmp(key, line) != 0) continue;
1036
1037 // skip whitespace after the '=' to the start of the value
1038 char* val_start = equal+1;
1039 while(*val_start && isspace(*val_start)) ++val_start;
1040
1041 // trim trailing whitespace
1042 char* val_end = val_start + strlen(val_start)-1;
1043 while (val_end > val_start && isspace(*val_end)) --val_end;
1044 val_end[1] = '\0';
1045
1046 result = strdup(val_start);
1047 break;
1048
1049 } while ((line = strtok(NULL, "\n")));
1050
1051 if (result == NULL) result = strdup("");
1052
1053 done:
1054 free(filename);
1055 free(key);
1056 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001057 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001058}
1059
Doug Zongker179b2d92011-04-12 15:49:04 -07001060// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001061Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001062 char* result = NULL;
1063
Doug Zongker179b2d92011-04-12 15:49:04 -07001064 Value* partition_value;
1065 Value* contents;
1066 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001067 return NULL;
1068 }
1069
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001070 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001071 if (partition_value->type != VAL_STRING) {
1072 ErrorAbort(state, "partition argument to %s must be string", name);
1073 goto done;
1074 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001075 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001076 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001077 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001078 goto done;
1079 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001080 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001081 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001082 goto done;
1083 }
1084
1085 mtd_scan_partitions();
Tao Baoba9a42a2015-06-23 23:23:33 -07001086 const MtdPartition* mtd;
1087 mtd = mtd_find_partition_by_name(partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001088 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001089 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001090 result = strdup("");
1091 goto done;
1092 }
1093
Tao Baoba9a42a2015-06-23 23:23:33 -07001094 MtdWriteContext* ctx;
1095 ctx = mtd_write_partition(mtd);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001096 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001097 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001098 name, partition);
1099 result = strdup("");
1100 goto done;
1101 }
1102
1103 bool success;
1104
Doug Zongker179b2d92011-04-12 15:49:04 -07001105 if (contents->type == VAL_STRING) {
1106 // we're given a filename as the contents
1107 char* filename = contents->data;
Jed Estepa7b9a462015-12-15 16:04:53 -08001108 FILE* f = ota_fopen(filename, "rb");
Doug Zongker179b2d92011-04-12 15:49:04 -07001109 if (f == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001110 printf("%s: can't open %s: %s\n", name, filename, strerror(errno));
Doug Zongker179b2d92011-04-12 15:49:04 -07001111 result = strdup("");
1112 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001113 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001114
1115 success = true;
Tao Baoba9a42a2015-06-23 23:23:33 -07001116 char* buffer = reinterpret_cast<char*>(malloc(BUFSIZ));
Doug Zongker179b2d92011-04-12 15:49:04 -07001117 int read;
Jed Estepa7b9a462015-12-15 16:04:53 -08001118 while (success && (read = ota_fread(buffer, 1, BUFSIZ, f)) > 0) {
Doug Zongker179b2d92011-04-12 15:49:04 -07001119 int wrote = mtd_write_data(ctx, buffer, read);
1120 success = success && (wrote == read);
1121 }
1122 free(buffer);
Jed Estepa7b9a462015-12-15 16:04:53 -08001123 ota_fclose(f);
Doug Zongker179b2d92011-04-12 15:49:04 -07001124 } else {
1125 // we're given a blob as the contents
1126 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1127 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001128 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001129 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001130 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001131 partition, strerror(errno));
1132 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001133
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001134 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001135 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001136 }
1137 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001138 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001139 }
1140
Doug Zongker179b2d92011-04-12 15:49:04 -07001141 printf("%s %s partition\n",
1142 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001143
1144 result = success ? partition : strdup("");
1145
1146done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001147 if (result != partition) FreeValue(partition_value);
1148 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001149 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001150}
1151
Doug Zongker8edb00c2009-06-11 17:21:44 -07001152// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001153Value* ApplyPatchSpaceFn(const char* name, State* state,
1154 int argc, Expr* argv[]) {
1155 char* bytes_str;
1156 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1157 return NULL;
1158 }
1159
Tao Baob15fd222015-09-24 11:10:51 -07001160 size_t bytes;
1161 if (!android::base::ParseUint(bytes_str, &bytes)) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001162 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n", name, bytes_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001163 free(bytes_str);
Tao Baob15fd222015-09-24 11:10:51 -07001164 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001165 }
1166
1167 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1168}
1169
Doug Zongker52b40362014-02-10 15:30:30 -08001170// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1171
Doug Zongker512536a2010-02-17 16:11:44 -08001172Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001173 if (argc < 6 || (argc % 2) == 1) {
1174 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1175 "even number, got %d",
1176 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001177 }
1178
Doug Zongkerc4351c72010-02-22 14:46:32 -08001179 char* source_filename;
1180 char* target_filename;
1181 char* target_sha1;
1182 char* target_size_str;
1183 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1184 &target_sha1, &target_size_str) < 0) {
1185 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001186 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001187
Tao Baob15fd222015-09-24 11:10:51 -07001188 size_t target_size;
1189 if (!android::base::ParseUint(target_size_str, &target_size)) {
1190 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count", name, target_size_str);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001191 free(source_filename);
1192 free(target_filename);
1193 free(target_sha1);
1194 free(target_size_str);
Tao Baob15fd222015-09-24 11:10:51 -07001195 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001196 }
1197
1198 int patchcount = (argc-4) / 2;
Yabin Cui64be2132016-02-03 18:16:02 -08001199 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc-4, argv+4),
1200 free);
1201 if (!arg_values) {
1202 return nullptr;
1203 }
1204 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patch_shas;
1205 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patches;
1206 // Protect values by unique_ptrs first to get rid of memory leak.
1207 for (int i = 0; i < patchcount * 2; i += 2) {
1208 patch_shas.emplace_back(arg_values.get()[i], FreeValue);
1209 patches.emplace_back(arg_values.get()[i+1], FreeValue);
1210 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001211
Yabin Cui64be2132016-02-03 18:16:02 -08001212 for (int i = 0; i < patchcount; ++i) {
1213 if (patch_shas[i]->type != VAL_STRING) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001214 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001215 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001216 }
Yabin Cui64be2132016-02-03 18:16:02 -08001217 if (patches[i]->type != VAL_BLOB) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001218 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001219 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001220 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001221 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001222
Yabin Cui64be2132016-02-03 18:16:02 -08001223 std::vector<char*> patch_sha_str;
1224 std::vector<Value*> patch_ptrs;
1225 for (int i = 0; i < patchcount; ++i) {
1226 patch_sha_str.push_back(patch_shas[i]->data);
1227 patch_ptrs.push_back(patches[i].get());
Doug Zongker8edb00c2009-06-11 17:21:44 -07001228 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001229
1230 int result = applypatch(source_filename, target_filename,
1231 target_sha1, target_size,
Yabin Cui64be2132016-02-03 18:16:02 -08001232 patchcount, patch_sha_str.data(), patch_ptrs.data(), NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001233
1234 return StringValue(strdup(result == 0 ? "t" : ""));
1235}
1236
1237// apply_patch_check(file, [sha1_1, ...])
1238Value* ApplyPatchCheckFn(const char* name, State* state,
1239 int argc, Expr* argv[]) {
1240 if (argc < 1) {
1241 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1242 name, argc);
1243 }
1244
1245 char* filename;
1246 if (ReadArgs(state, argv, 1, &filename) < 0) {
1247 return NULL;
1248 }
1249
1250 int patchcount = argc-1;
1251 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1252
1253 int result = applypatch_check(filename, patchcount, sha1s);
1254
1255 int i;
1256 for (i = 0; i < patchcount; ++i) {
1257 free(sha1s[i]);
1258 }
1259 free(sha1s);
1260
1261 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001262}
1263
Tao Bao1107d962015-09-09 17:16:55 -07001264// This is the updater side handler for ui_print() in edify script. Contents
1265// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001266Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001267 char** args = ReadVarArgs(state, argc, argv);
1268 if (args == NULL) {
1269 return NULL;
1270 }
1271
Tao Bao1107d962015-09-09 17:16:55 -07001272 std::string buffer;
1273 for (int i = 0; i < argc; ++i) {
1274 buffer += args[i];
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001275 free(args[i]);
1276 }
1277 free(args);
Tao Bao1107d962015-09-09 17:16:55 -07001278
1279 buffer += "\n";
Michael Runged4a63422014-10-22 19:48:41 -07001280 uiPrint(state, buffer);
Tao Bao1107d962015-09-09 17:16:55 -07001281 return StringValue(strdup(buffer.c_str()));
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001282}
1283
Doug Zongkerd0181b82011-10-19 10:51:12 -07001284Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1285 if (argc != 0) {
1286 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1287 }
1288 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1289 return StringValue(strdup("t"));
1290}
1291
Doug Zongker512536a2010-02-17 16:11:44 -08001292Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001293 if (argc < 1) {
1294 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1295 }
1296 char** args = ReadVarArgs(state, argc, argv);
1297 if (args == NULL) {
1298 return NULL;
1299 }
1300
Tao Baoba9a42a2015-06-23 23:23:33 -07001301 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001302 memcpy(args2, args, sizeof(char*) * argc);
1303 args2[argc] = NULL;
1304
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001305 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001306
1307 pid_t child = fork();
1308 if (child == 0) {
1309 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001310 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001311 _exit(1);
1312 }
1313 int status;
1314 waitpid(child, &status, 0);
1315 if (WIFEXITED(status)) {
1316 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001317 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001318 WEXITSTATUS(status));
1319 }
1320 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001321 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001322 WTERMSIG(status));
1323 }
1324
1325 int i;
1326 for (i = 0; i < argc; ++i) {
1327 free(args[i]);
1328 }
1329 free(args);
1330 free(args2);
1331
1332 char buffer[20];
1333 sprintf(buffer, "%d", status);
1334
Doug Zongker512536a2010-02-17 16:11:44 -08001335 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001336}
1337
Doug Zongker512536a2010-02-17 16:11:44 -08001338// sha1_check(data)
1339// to return the sha1 of the data (given in the format returned by
1340// read_file).
1341//
1342// sha1_check(data, sha1_hex, [sha1_hex, ...])
1343// returns the sha1 of the file if it matches any of the hex
1344// strings passed, or "" if it does not equal any of them.
1345//
1346Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1347 if (argc < 1) {
1348 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1349 }
1350
Yabin Cui64be2132016-02-03 18:16:02 -08001351 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc, argv), free);
1352 if (arg_values == nullptr) {
1353 return nullptr;
1354 }
1355 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> args;
1356 for (int i = 0; i < argc; ++i) {
1357 args.emplace_back(arg_values.get()[i], FreeValue);
Doug Zongker512536a2010-02-17 16:11:44 -08001358 }
1359
1360 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001361 return StringValue(strdup(""));
1362 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001363 uint8_t digest[SHA_DIGEST_LENGTH];
1364 SHA1(reinterpret_cast<uint8_t*>(args[0]->data), args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001365
1366 if (argc == 1) {
1367 return StringValue(PrintSha1(digest));
1368 }
1369
1370 int i;
Yabin Cui64be2132016-02-03 18:16:02 -08001371 uint8_t arg_digest[SHA_DIGEST_LENGTH];
Doug Zongker512536a2010-02-17 16:11:44 -08001372 for (i = 1; i < argc; ++i) {
1373 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001374 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001375 name, i);
1376 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1377 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001378 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1379 name, args[i]->data);
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001380 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001381 break;
1382 }
Doug Zongker512536a2010-02-17 16:11:44 -08001383 }
1384 if (i >= argc) {
1385 // Didn't match any of the hex strings; return false.
1386 return StringValue(strdup(""));
1387 }
Yabin Cui64be2132016-02-03 18:16:02 -08001388 // Found a match.
1389 return args[i].release();
Doug Zongker512536a2010-02-17 16:11:44 -08001390}
1391
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001392// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001393// is actually a FileContents*).
1394Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1395 if (argc != 1) {
1396 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1397 }
1398 char* filename;
1399 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1400
Yabin Cuid6c93af2016-02-10 16:41:10 -08001401 Value* v = static_cast<Value*>(malloc(sizeof(Value)));
1402 if (v == nullptr) {
1403 return nullptr;
1404 }
Doug Zongker512536a2010-02-17 16:11:44 -08001405 v->type = VAL_BLOB;
Yabin Cuid6c93af2016-02-10 16:41:10 -08001406 v->size = -1;
1407 v->data = nullptr;
Doug Zongker512536a2010-02-17 16:11:44 -08001408
1409 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001410 if (LoadFileContents(filename, &fc) != 0) {
Yabin Cuid6c93af2016-02-10 16:41:10 -08001411 v->data = static_cast<char*>(malloc(fc.data.size()));
1412 if (v->data != nullptr) {
1413 memcpy(v->data, fc.data.data(), fc.data.size());
1414 v->size = fc.data.size();
1415 }
Doug Zongker512536a2010-02-17 16:11:44 -08001416 }
Doug Zongker512536a2010-02-17 16:11:44 -08001417 free(filename);
1418 return v;
1419}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001420
Doug Zongkerc87bab12013-11-25 13:53:25 -08001421// Immediately reboot the device. Recovery is not finished normally,
1422// so if you reboot into recovery it will re-start applying the
1423// current package (because nothing has cleared the copy of the
1424// arguments stored in the BCB).
1425//
1426// The argument is the partition name passed to the android reboot
1427// property. It can be "recovery" to boot from the recovery
1428// partition, or "" (empty string) to boot from the regular boot
1429// partition.
1430Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1431 if (argc != 2) {
1432 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1433 }
1434
1435 char* filename;
1436 char* property;
1437 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1438
1439 char buffer[80];
1440
1441 // zero out the 'command' field of the bootloader message.
1442 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
Jed Estepa7b9a462015-12-15 16:04:53 -08001443 FILE* f = ota_fopen(filename, "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001444 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001445 ota_fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1446 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001447 free(filename);
1448
1449 strcpy(buffer, "reboot,");
1450 if (property != NULL) {
1451 strncat(buffer, property, sizeof(buffer)-10);
1452 }
1453
1454 property_set(ANDROID_RB_PROPERTY, buffer);
1455
1456 sleep(5);
1457 free(property);
1458 ErrorAbort(state, "%s() failed to reboot", name);
1459 return NULL;
1460}
1461
1462// Store a string value somewhere that future invocations of recovery
1463// can access it. This value is called the "stage" and can be used to
1464// drive packages that need to do reboots in the middle of
1465// installation and keep track of where they are in the multi-stage
1466// install.
1467//
1468// The first argument is the block device for the misc partition
1469// ("/misc" in the fstab), which is where this value is stored. The
1470// second argument is the string to store; it should not exceed 31
1471// bytes.
1472Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1473 if (argc != 2) {
1474 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1475 }
1476
1477 char* filename;
1478 char* stagestr;
1479 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1480
1481 // Store this value in the misc partition, immediately after the
1482 // bootloader message that the main recovery uses to save its
1483 // arguments in case of the device restarting midway through
1484 // package installation.
Jed Estepa7b9a462015-12-15 16:04:53 -08001485 FILE* f = ota_fopen(filename, "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001486 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1487 int to_write = strlen(stagestr)+1;
1488 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1489 if (to_write > max_size) {
1490 to_write = max_size;
1491 stagestr[max_size-1] = 0;
1492 }
Jed Estepa7b9a462015-12-15 16:04:53 -08001493 ota_fwrite(stagestr, to_write, 1, f);
1494 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001495
1496 free(stagestr);
1497 return StringValue(filename);
1498}
1499
1500// Return the value most recently saved with SetStageFn. The argument
1501// is the block device for the misc partition.
1502Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001503 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001504 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1505 }
1506
1507 char* filename;
1508 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1509
1510 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
Jed Estepa7b9a462015-12-15 16:04:53 -08001511 FILE* f = ota_fopen(filename, "rb");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001512 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001513 ota_fread(buffer, sizeof(buffer), 1, f);
1514 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001515 buffer[sizeof(buffer)-1] = '\0';
1516
1517 return StringValue(strdup(buffer));
1518}
1519
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001520Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1521 if (argc != 2) {
1522 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1523 }
1524
1525 char* filename;
1526 char* len_str;
1527 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1528
Tao Baob15fd222015-09-24 11:10:51 -07001529 size_t len;
1530 android::base::ParseUint(len_str, &len);
Jed Estepa7b9a462015-12-15 16:04:53 -08001531 int fd = ota_open(filename, O_WRONLY, 0644);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001532 int success = wipe_block_device(fd, len);
1533
1534 free(filename);
1535 free(len_str);
1536
Jed Estepa7b9a462015-12-15 16:04:53 -08001537 ota_close(fd);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001538
1539 return StringValue(strdup(success ? "t" : ""));
1540}
1541
Doug Zongkerc704e062014-05-23 08:40:35 -07001542Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1543 if (argc != 0) {
1544 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1545 }
1546 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1547 fprintf(ui->cmd_pipe, "enable_reboot\n");
1548 return StringValue(strdup("t"));
1549}
1550
Michael Rungeacf47db2014-11-21 00:12:28 -08001551Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1552 if (argc == 0) {
1553 return ErrorAbort(state, "%s() expects args, got %d", name, argc);
1554 }
1555
1556 char** args = ReadVarArgs(state, argc, argv);
1557 if (args == NULL) {
1558 return ErrorAbort(state, "%s() could not read args", name);
1559 }
1560
Tao Baoba9a42a2015-06-23 23:23:33 -07001561 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Michael Rungeacf47db2014-11-21 00:12:28 -08001562 // Tune2fs expects the program name as its args[0]
1563 args2[0] = strdup(name);
Tao Baoba9a42a2015-06-23 23:23:33 -07001564 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001565 args2[i + 1] = args[i];
1566 }
1567 int result = tune2fs_main(argc + 1, args2);
Tao Baoba9a42a2015-06-23 23:23:33 -07001568 for (int i = 0; i < argc; ++i) {
Michael Rungeacf47db2014-11-21 00:12:28 -08001569 free(args[i]);
1570 }
1571 free(args);
1572
1573 free(args2[0]);
1574 free(args2);
1575 if (result != 0) {
1576 return ErrorAbort(state, "%s() returned error code %d", name, result);
1577 }
1578 return StringValue(strdup("t"));
1579}
1580
Doug Zongker9931f7f2009-06-10 14:11:53 -07001581void RegisterInstallFunctions() {
1582 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001583 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001584 RegisterFunction("unmount", UnmountFn);
1585 RegisterFunction("format", FormatFn);
1586 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001587 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001588 RegisterFunction("delete", DeleteFn);
1589 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001590 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1591 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001592 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001593
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001594 // Usage:
1595 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1596 // Example:
1597 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1598 RegisterFunction("set_metadata", SetMetadataFn);
1599
1600 // Usage:
1601 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1602 // Example:
1603 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1604 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1605
Doug Zongker8edb00c2009-06-11 17:21:44 -07001606 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001607 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001608 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001609
1610 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001611 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1612 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001613
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001614 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001615
Doug Zongker512536a2010-02-17 16:11:44 -08001616 RegisterFunction("read_file", ReadFileFn);
1617 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001618 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001619
Doug Zongkerd0181b82011-10-19 10:51:12 -07001620 RegisterFunction("wipe_cache", WipeCacheFn);
1621
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001622 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001623
1624 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001625
1626 RegisterFunction("reboot_now", RebootNowFn);
1627 RegisterFunction("get_stage", GetStageFn);
1628 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001629
1630 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001631 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001632}