blob: 1986180016a5286161d0f6d3661141c94481ea0a [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
Doug Zongkerc87bab12013-11-25 13:53:25 -080037#include "bootloader.h"
38#include "applypatch/applypatch.h"
39#include "cutils/android_reboot.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070040#include "cutils/misc.h"
41#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070042#include "edify/expr.h"
Doug Zongker512536a2010-02-17 16:11:44 -080043#include "mincrypt/sha.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070044#include "minzip/DirUtil.h"
45#include "mtdutils/mounts.h"
46#include "mtdutils/mtdutils.h"
47#include "updater.h"
Doug Zongker52b40362014-02-10 15:30:30 -080048#include "syspatch.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080049#include "install.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070050
Doug Zongker3d177d02010-07-01 09:18:44 -070051#ifdef USE_EXT4
52#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080053#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070054#endif
55
Doug Zongker52b40362014-02-10 15:30:30 -080056// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongker0d32f252014-02-13 15:07:56 -080057static char* PrintSha1(const uint8_t* digest) {
Doug Zongker52b40362014-02-10 15:30:30 -080058 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
59 int i;
60 const char* alphabet = "0123456789abcdef";
61 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
62 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
63 buffer[i*2+1] = alphabet[digest[i] & 0xf];
64 }
65 buffer[i*2] = '\0';
66 return buffer;
67}
68
Doug Zongker3d177d02010-07-01 09:18:44 -070069// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -070070//
Doug Zongker3d177d02010-07-01 09:18:44 -070071// fs_type="yaffs2" partition_type="MTD" location=partition
72// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -080073Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070074 char* result = NULL;
Doug Zongker3d177d02010-07-01 09:18:44 -070075 if (argc != 4) {
76 return ErrorAbort(state, "%s() expects 4 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -070077 }
Doug Zongker3d177d02010-07-01 09:18:44 -070078 char* fs_type;
79 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -070080 char* location;
81 char* mount_point;
Doug Zongker3d177d02010-07-01 09:18:44 -070082 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
83 &location, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070084 return NULL;
85 }
86
Doug Zongker3d177d02010-07-01 09:18:44 -070087 if (strlen(fs_type) == 0) {
88 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
89 goto done;
90 }
91 if (strlen(partition_type) == 0) {
92 ErrorAbort(state, "partition_type argument to %s() can't be empty",
93 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070094 goto done;
95 }
96 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070097 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070098 goto done;
99 }
100 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700101 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700102 goto done;
103 }
104
Stephen Smalley779701d2012-02-09 14:13:23 -0500105 char *secontext = NULL;
106
107 if (sehandle) {
108 selabel_lookup(sehandle, &secontext, mount_point, 0755);
109 setfscreatecon(secontext);
110 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500111
Doug Zongker9931f7f2009-06-10 14:11:53 -0700112 mkdir(mount_point, 0755);
113
Stephen Smalley779701d2012-02-09 14:13:23 -0500114 if (secontext) {
115 freecon(secontext);
116 setfscreatecon(NULL);
117 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500118
Doug Zongker3d177d02010-07-01 09:18:44 -0700119 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700120 mtd_scan_partitions();
121 const MtdPartition* mtd;
122 mtd = mtd_find_partition_by_name(location);
123 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700124 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700125 name, location);
126 result = strdup("");
127 goto done;
128 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700129 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700130 printf("mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700131 location, strerror(errno));
132 result = strdup("");
133 goto done;
134 }
135 result = mount_point;
136 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700137 if (mount(location, mount_point, fs_type,
Doug Zongker9931f7f2009-06-10 14:11:53 -0700138 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700139 printf("%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700140 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700141 result = strdup("");
142 } else {
143 result = mount_point;
144 }
145 }
146
147done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700148 free(fs_type);
149 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700150 free(location);
151 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800152 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700153}
154
Doug Zongker8edb00c2009-06-11 17:21:44 -0700155
156// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800157Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700158 char* result = NULL;
159 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700160 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700161 }
162 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700163 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700164 return NULL;
165 }
166 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700167 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700168 goto done;
169 }
170
171 scan_mounted_volumes();
172 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
173 if (vol == NULL) {
174 result = strdup("");
175 } else {
176 result = mount_point;
177 }
178
179done:
180 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800181 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700182}
183
184
Doug Zongker512536a2010-02-17 16:11:44 -0800185Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700186 char* result = NULL;
187 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700188 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700189 }
190 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700191 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700192 return NULL;
193 }
194 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700195 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700196 goto done;
197 }
198
199 scan_mounted_volumes();
200 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
201 if (vol == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700202 printf("unmount of %s failed; no such volume\n", mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700203 result = strdup("");
204 } else {
205 unmount_mounted_volume(vol);
206 result = mount_point;
207 }
208
209done:
210 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800211 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700212}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700213
JP Abgrall37aedb32014-06-16 19:07:39 -0700214static int exec_cmd(const char* path, char* const argv[]) {
215 int status;
216 pid_t child;
217 if ((child = vfork()) == 0) {
218 execv(path, argv);
219 _exit(-1);
220 }
221 waitpid(child, &status, 0);
222 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
223 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
224 }
225 return WEXITSTATUS(status);
226}
227
Doug Zongker8edb00c2009-06-11 17:21:44 -0700228
Stephen Smalley779701d2012-02-09 14:13:23 -0500229// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700230//
Stephen Smalley779701d2012-02-09 14:13:23 -0500231// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
232// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700233// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
234// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800235// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700236// 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 -0800237Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700238 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400239 if (argc != 5) {
240 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700241 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700242 char* fs_type;
243 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700244 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800245 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400246 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500247
Stephen Smalley779701d2012-02-09 14:13:23 -0500248 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
249 return NULL;
250 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700251
Doug Zongker3d177d02010-07-01 09:18:44 -0700252 if (strlen(fs_type) == 0) {
253 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
254 goto done;
255 }
256 if (strlen(partition_type) == 0) {
257 ErrorAbort(state, "partition_type argument to %s() can't be empty",
258 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700259 goto done;
260 }
261 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700262 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700263 goto done;
264 }
265
Stephen Smalley516e4e22012-04-03 13:35:11 -0400266 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500267 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
268 goto done;
269 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500270
Doug Zongker3d177d02010-07-01 09:18:44 -0700271 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700272 mtd_scan_partitions();
273 const MtdPartition* mtd = mtd_find_partition_by_name(location);
274 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700275 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700276 name, location);
277 result = strdup("");
278 goto done;
279 }
280 MtdWriteContext* ctx = mtd_write_partition(mtd);
281 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700282 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700283 result = strdup("");
284 goto done;
285 }
286 if (mtd_erase_blocks(ctx, -1) == -1) {
287 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700288 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700289 result = strdup("");
290 goto done;
291 }
292 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700293 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700294 result = strdup("");
295 goto done;
296 }
297 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700298#ifdef USE_EXT4
299 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500300 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700301 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700302 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700303 name, status, location);
304 result = strdup("");
305 goto done;
306 }
307 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700308 } else if (strcmp(fs_type, "f2fs") == 0) {
309 char *num_sectors;
310 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
311 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
312 result = strdup("");
313 goto done;
314 }
315 const char *f2fs_path = "/sbin/mkfs.f2fs";
316 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
317 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
318 free(num_sectors);
319 if (status != 0) {
320 printf("%s: mkfs.f2fs failed (%d) on %s",
321 name, status, location);
322 result = strdup("");
323 goto done;
324 }
325 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700326#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700327 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700328 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700329 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700330 }
331
332done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700333 free(fs_type);
334 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700335 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800336 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700337}
338
Michael Rungece7ca712013-11-06 17:42:20 -0800339Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
340 char* result = NULL;
341 if (argc != 2) {
342 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
343 }
344
345 char* src_name;
346 char* dst_name;
347
348 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
349 return NULL;
350 }
351 if (strlen(src_name) == 0) {
352 ErrorAbort(state, "src_name argument to %s() can't be empty", name);
353 goto done;
354 }
355 if (strlen(dst_name) == 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700356 ErrorAbort(state, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800357 goto done;
358 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700359 if (make_parents(dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700360 ErrorAbort(state, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700361 dst_name, strerror(errno));
362 } else if (rename(src_name, dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700363 ErrorAbort(state, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800364 src_name, dst_name, strerror(errno));
365 } else {
366 result = dst_name;
367 }
368
369done:
370 free(src_name);
371 if (result != dst_name) free(dst_name);
372 return StringValue(result);
373}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700374
Doug Zongker512536a2010-02-17 16:11:44 -0800375Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700376 char** paths = malloc(argc * sizeof(char*));
377 int i;
378 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700379 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700380 if (paths[i] == NULL) {
381 int j;
382 for (j = 0; j < i; ++i) {
383 free(paths[j]);
384 }
385 free(paths);
386 return NULL;
387 }
388 }
389
390 bool recursive = (strcmp(name, "delete_recursive") == 0);
391
392 int success = 0;
393 for (i = 0; i < argc; ++i) {
394 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
395 ++success;
396 free(paths[i]);
397 }
398 free(paths);
399
400 char buffer[10];
401 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800402 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700403}
404
Doug Zongker8edb00c2009-06-11 17:21:44 -0700405
Doug Zongker512536a2010-02-17 16:11:44 -0800406Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700407 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700408 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700409 }
410 char* frac_str;
411 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700412 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700413 return NULL;
414 }
415
416 double frac = strtod(frac_str, NULL);
417 int sec = strtol(sec_str, NULL, 10);
418
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700419 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700420 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
421
Doug Zongker9931f7f2009-06-10 14:11:53 -0700422 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800423 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700424}
425
Doug Zongker512536a2010-02-17 16:11:44 -0800426Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700427 if (argc != 1) {
428 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
429 }
430 char* frac_str;
431 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
432 return NULL;
433 }
434
435 double frac = strtod(frac_str, NULL);
436
437 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
438 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
439
Doug Zongker512536a2010-02-17 16:11:44 -0800440 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700441}
442
Doug Zongker8edb00c2009-06-11 17:21:44 -0700443// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800444Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700445 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700446 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700447 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700448 }
449 char* zip_path;
450 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700451 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700452
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700453 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700454
455 // To create a consistent system image, never use the clock for timestamps.
456 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
457
458 bool success = mzExtractRecursive(za, zip_path, dest_path,
459 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500460 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700461 free(zip_path);
462 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800463 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700464}
465
Doug Zongker8edb00c2009-06-11 17:21:44 -0700466
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800467DontCareMap* ReadDontCareMapFromZip(ZipArchive* za, const char* path) {
468 const char* name = "ReadDontCareMapFromZip";
469
470 const ZipEntry* entry = mzFindZipEntry(za, path);
471 if (entry == NULL) {
472 printf("%s: no %s in package\n", name, path);
473 return NULL;
474 }
475
476 size_t map_size = mzGetZipEntryUncompLen(entry);
477 char* map_data = malloc(map_size);
478 if (map_data == NULL) {
479 printf("%s: failed to allocate %zu bytes for %s\n",
480 name, map_size, path);
481 return NULL;
482 }
483
484 if (!mzExtractZipEntryToBuffer(za, entry, (unsigned char*) map_data)) {
485 printf("%s: failed to read %s\n", name, path);
486 return NULL;
487 }
488
489 char* p = map_data;
490 DontCareMap* map = (DontCareMap*) malloc(sizeof(DontCareMap));
491
492 map->block_size = strtoul(p, &p, 0);
493 if (map->block_size != 4096) {
494 printf("%s: unexpected block size %zu\n", name, map->block_size);
495 return NULL;
496 }
497
498 map->region_count = strtoul(p, &p, 0);
499 map->regions = (int*) malloc(map->region_count * sizeof(int));
Doug Zongker43772d22014-06-09 14:13:19 -0700500 map->total_blocks = 0;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800501
502 int i;
503 for (i = 0; i < map->region_count; ++i) {
504 map->regions[i] = strtoul(p, &p, 0);
Doug Zongker43772d22014-06-09 14:13:19 -0700505 map->total_blocks += map->regions[i];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800506 }
507
508 return map;
509}
510
Doug Zongker43772d22014-06-09 14:13:19 -0700511static FILE* mapwrite_cmd_pipe;
512
513static void progress_cb(long done, long total) {
514 if (total > 0) {
515 double frac = (double)done / total;
516 fprintf(mapwrite_cmd_pipe, "set_progress %f\n", frac);
517 fflush(mapwrite_cmd_pipe);
518 }
519}
520
521
522
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800523bool MapWriter(const unsigned char* data, int dataLen, void* cookie) {
Doug Zongker43772d22014-06-09 14:13:19 -0700524 return write_with_map(data, dataLen, (MapState*) cookie, progress_cb) == dataLen;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800525}
526
527// package_extract_file(package_path, destination_path, map_path)
528// or
Doug Zongker8edb00c2009-06-11 17:21:44 -0700529// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800530// or
531// package_extract_file(package_path)
532// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800533// function (the char* returned is actually a FileContents*).
534Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700535 int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800536 if (argc < 1 || argc > 3) {
537 return ErrorAbort(state, "%s() expects 1 or 2 or 3 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800538 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700539 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700540 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700541
542 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
543 mapwrite_cmd_pipe = ui->cmd_pipe;
544
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800545 if (argc >= 2) {
546 // The two-argument version extracts to a file; the three-arg
547 // version extracts to a file, skipping over regions in a
548 // don't care map.
549
550 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700551
Doug Zongker6aece332010-02-01 14:40:12 -0800552 char* zip_path;
553 char* dest_path;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800554 char* map_path = NULL;
555 DontCareMap* map = NULL;
556 if (argc == 2) {
557 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
558 } else {
559 if (ReadArgs(state, argv, 3, &zip_path, &dest_path, &map_path) < 0) return NULL;
560 map = ReadDontCareMapFromZip(za, map_path);
561 if (map == NULL) goto done2;
562 }
Doug Zongker6aece332010-02-01 14:40:12 -0800563
Doug Zongker6aece332010-02-01 14:40:12 -0800564 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
565 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700566 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800567 goto done2;
568 }
569
570 FILE* f = fopen(dest_path, "wb");
571 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700572 printf("%s: can't open %s for write: %s\n",
Doug Zongker6aece332010-02-01 14:40:12 -0800573 name, dest_path, strerror(errno));
574 goto done2;
575 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800576 if (map) {
577 MapState state;
578 state.map = map;
579 state.cr = 0;
580 state.so_far = 0;
581 state.f = f;
582 success = mzProcessZipEntryContents(za, entry, MapWriter, &state);
583 } else {
584 success = mzExtractZipEntryToFile(za, entry, fileno(f));
585 }
Doug Zongker6aece332010-02-01 14:40:12 -0800586 fclose(f);
587
588 done2:
589 free(zip_path);
590 free(dest_path);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800591 free(map_path);
592 if (map) {
593 free(map->regions);
594 free(map);
595 }
Doug Zongker512536a2010-02-17 16:11:44 -0800596 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800597 } else {
598 // The one-argument version returns the contents of the file
599 // as the result.
600
601 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800602 Value* v = malloc(sizeof(Value));
603 v->type = VAL_BLOB;
604 v->size = -1;
605 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800606
607 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
608
609 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
610 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
611 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700612 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800613 goto done1;
614 }
615
Doug Zongker512536a2010-02-17 16:11:44 -0800616 v->size = mzGetZipEntryUncompLen(entry);
617 v->data = malloc(v->size);
618 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700619 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800620 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800621 goto done1;
622 }
623
Doug Zongker512536a2010-02-17 16:11:44 -0800624 success = mzExtractZipEntryToBuffer(za, entry,
625 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800626
627 done1:
628 free(zip_path);
629 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800630 free(v->data);
631 v->data = NULL;
632 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800633 }
Doug Zongker512536a2010-02-17 16:11:44 -0800634 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700635 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700636}
637
Doug Zongkera23075f2012-08-06 16:19:09 -0700638// Create all parent directories of name, if necessary.
639static int make_parents(char* name) {
640 char* p;
641 for (p = name + (strlen(name)-1); p > name; --p) {
642 if (*p != '/') continue;
643 *p = '\0';
644 if (make_parents(name) < 0) return -1;
645 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700646 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700647 *p = '/';
648 if (result == 0 || errno == EEXIST) {
649 // successfully created or already existed; we're done
650 return 0;
651 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700652 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700653 return -1;
654 }
655 }
656 return 0;
657}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700658
Doug Zongker9931f7f2009-06-10 14:11:53 -0700659// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700660// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800661Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700662 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700663 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700664 }
665 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700666 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700667 if (target == NULL) return NULL;
668
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700669 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700670 if (srcs == NULL) {
671 free(target);
672 return NULL;
673 }
674
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700675 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700676 int i;
677 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700678 if (unlink(srcs[i]) < 0) {
679 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700680 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700681 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700682 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700683 }
684 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700685 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700686 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700687 name, srcs[i], target);
688 ++bad;
689 }
Doug Zongker60babf82009-09-18 15:11:24 -0700690 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700691 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700692 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700693 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700694 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700695 free(srcs[i]);
696 }
697 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700698 if (bad) {
699 return ErrorAbort(state, "%s: some symlinks failed", name);
700 }
Doug Zongker512536a2010-02-17 16:11:44 -0800701 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700702}
703
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700704struct perm_parsed_args {
705 bool has_uid;
706 uid_t uid;
707 bool has_gid;
708 gid_t gid;
709 bool has_mode;
710 mode_t mode;
711 bool has_fmode;
712 mode_t fmode;
713 bool has_dmode;
714 mode_t dmode;
715 bool has_selabel;
716 char* selabel;
717 bool has_capabilities;
718 uint64_t capabilities;
719};
720
721static struct perm_parsed_args ParsePermArgs(int argc, char** args) {
722 int i;
723 struct perm_parsed_args parsed;
724 int bad = 0;
725 static int max_warnings = 20;
726
727 memset(&parsed, 0, sizeof(parsed));
728
729 for (i = 1; i < argc; i += 2) {
730 if (strcmp("uid", args[i]) == 0) {
731 int64_t uid;
732 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
733 parsed.uid = uid;
734 parsed.has_uid = true;
735 } else {
736 printf("ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
737 bad++;
738 }
739 continue;
740 }
741 if (strcmp("gid", args[i]) == 0) {
742 int64_t gid;
743 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
744 parsed.gid = gid;
745 parsed.has_gid = true;
746 } else {
747 printf("ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
748 bad++;
749 }
750 continue;
751 }
752 if (strcmp("mode", args[i]) == 0) {
753 int32_t mode;
754 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
755 parsed.mode = mode;
756 parsed.has_mode = true;
757 } else {
758 printf("ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
759 bad++;
760 }
761 continue;
762 }
763 if (strcmp("dmode", args[i]) == 0) {
764 int32_t mode;
765 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
766 parsed.dmode = mode;
767 parsed.has_dmode = true;
768 } else {
769 printf("ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
770 bad++;
771 }
772 continue;
773 }
774 if (strcmp("fmode", args[i]) == 0) {
775 int32_t mode;
776 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
777 parsed.fmode = mode;
778 parsed.has_fmode = true;
779 } else {
780 printf("ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
781 bad++;
782 }
783 continue;
784 }
785 if (strcmp("capabilities", args[i]) == 0) {
786 int64_t capabilities;
787 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
788 parsed.capabilities = capabilities;
789 parsed.has_capabilities = true;
790 } else {
791 printf("ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
792 bad++;
793 }
794 continue;
795 }
796 if (strcmp("selabel", args[i]) == 0) {
797 if (args[i+1][0] != '\0') {
798 parsed.selabel = args[i+1];
799 parsed.has_selabel = true;
800 } else {
801 printf("ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
802 bad++;
803 }
804 continue;
805 }
806 if (max_warnings != 0) {
807 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
808 max_warnings--;
809 if (max_warnings == 0) {
810 printf("ParsedPermArgs: suppressing further warnings\n");
811 }
812 }
813 }
814 return parsed;
815}
816
817static int ApplyParsedPerms(
818 const char* filename,
819 const struct stat *statptr,
820 struct perm_parsed_args parsed)
821{
822 int bad = 0;
823
Nick Kraleviche4612512013-09-10 15:34:19 -0700824 /* ignore symlinks */
825 if (S_ISLNK(statptr->st_mode)) {
826 return 0;
827 }
828
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700829 if (parsed.has_uid) {
830 if (chown(filename, parsed.uid, -1) < 0) {
831 printf("ApplyParsedPerms: chown of %s to %d failed: %s\n",
832 filename, parsed.uid, strerror(errno));
833 bad++;
834 }
835 }
836
837 if (parsed.has_gid) {
838 if (chown(filename, -1, parsed.gid) < 0) {
839 printf("ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
840 filename, parsed.gid, strerror(errno));
841 bad++;
842 }
843 }
844
845 if (parsed.has_mode) {
846 if (chmod(filename, parsed.mode) < 0) {
847 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
848 filename, parsed.mode, strerror(errno));
849 bad++;
850 }
851 }
852
853 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
854 if (chmod(filename, parsed.dmode) < 0) {
855 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
856 filename, parsed.dmode, strerror(errno));
857 bad++;
858 }
859 }
860
861 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
862 if (chmod(filename, parsed.fmode) < 0) {
863 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
864 filename, parsed.fmode, strerror(errno));
865 bad++;
866 }
867 }
868
869 if (parsed.has_selabel) {
870 // TODO: Don't silently ignore ENOTSUP
871 if (lsetfilecon(filename, parsed.selabel) && (errno != ENOTSUP)) {
872 printf("ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
873 filename, parsed.selabel, strerror(errno));
874 bad++;
875 }
876 }
877
878 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
879 if (parsed.capabilities == 0) {
880 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
881 // Report failure unless it's ENODATA (attribute not set)
882 printf("ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
883 filename, parsed.capabilities, strerror(errno));
884 bad++;
885 }
886 } else {
887 struct vfs_cap_data cap_data;
888 memset(&cap_data, 0, sizeof(cap_data));
889 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
890 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
891 cap_data.data[0].inheritable = 0;
892 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
893 cap_data.data[1].inheritable = 0;
894 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
895 printf("ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
896 filename, parsed.capabilities, strerror(errno));
897 bad++;
898 }
899 }
900 }
901
902 return bad;
903}
904
905// nftw doesn't allow us to pass along context, so we need to use
906// global variables. *sigh*
907static struct perm_parsed_args recursive_parsed_args;
908
909static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
910 int fileflags, struct FTW *pfwt) {
911 return ApplyParsedPerms(filename, statptr, recursive_parsed_args);
912}
913
914static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
915 int i;
916 int bad = 0;
917 static int nwarnings = 0;
918 struct stat sb;
919 Value* result = NULL;
920
921 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
922
923 if ((argc % 2) != 1) {
924 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d",
925 name, argc);
926 }
927
928 char** args = ReadVarArgs(state, argc, argv);
929 if (args == NULL) return NULL;
930
931 if (lstat(args[0], &sb) == -1) {
932 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
933 goto done;
934 }
935
936 struct perm_parsed_args parsed = ParsePermArgs(argc, args);
937
938 if (recursive) {
939 recursive_parsed_args = parsed;
940 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
941 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
942 } else {
943 bad += ApplyParsedPerms(args[0], &sb, parsed);
944 }
945
946done:
947 for (i = 0; i < argc; ++i) {
948 free(args[i]);
949 }
950 free(args);
951
952 if (result != NULL) {
953 return result;
954 }
955
956 if (bad > 0) {
957 return ErrorAbort(state, "%s: some changes failed", name);
958 }
959
960 return StringValue(strdup(""));
961}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700962
Doug Zongker512536a2010-02-17 16:11:44 -0800963Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700964 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700965 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700966 }
967 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700968 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700969 if (key == NULL) return NULL;
970
971 char value[PROPERTY_VALUE_MAX];
972 property_get(key, value, "");
973 free(key);
974
Doug Zongker512536a2010-02-17 16:11:44 -0800975 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700976}
977
978
Doug Zongker47cace92009-06-18 10:11:50 -0700979// file_getprop(file, key)
980//
981// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700982// per line. # comment lines,blank lines, lines without '=' ignored),
983// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800984Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700985 char* result = NULL;
986 char* buffer = NULL;
987 char* filename;
988 char* key;
989 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
990 return NULL;
991 }
992
993 struct stat st;
994 if (stat(filename, &st) < 0) {
995 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
996 name, filename, strerror(errno));
997 goto done;
998 }
999
1000#define MAX_FILE_GETPROP_SIZE 65536
1001
1002 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
1003 ErrorAbort(state, "%s too large for %s (max %d)",
1004 filename, name, MAX_FILE_GETPROP_SIZE);
1005 goto done;
1006 }
1007
1008 buffer = malloc(st.st_size+1);
1009 if (buffer == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -07001010 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -07001011 goto done;
1012 }
1013
1014 FILE* f = fopen(filename, "rb");
1015 if (f == NULL) {
1016 ErrorAbort(state, "%s: failed to open %s: %s",
1017 name, filename, strerror(errno));
1018 goto done;
1019 }
1020
1021 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -07001022 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -07001023 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -07001024 fclose(f);
1025 goto done;
1026 }
1027 buffer[st.st_size] = '\0';
1028
1029 fclose(f);
1030
1031 char* line = strtok(buffer, "\n");
1032 do {
1033 // skip whitespace at start of line
1034 while (*line && isspace(*line)) ++line;
1035
1036 // comment or blank line: skip to next line
1037 if (*line == '\0' || *line == '#') continue;
1038
1039 char* equal = strchr(line, '=');
1040 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001041 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001042 }
1043
1044 // trim whitespace between key and '='
1045 char* key_end = equal-1;
1046 while (key_end > line && isspace(*key_end)) --key_end;
1047 key_end[1] = '\0';
1048
1049 // not the key we're looking for
1050 if (strcmp(key, line) != 0) continue;
1051
1052 // skip whitespace after the '=' to the start of the value
1053 char* val_start = equal+1;
1054 while(*val_start && isspace(*val_start)) ++val_start;
1055
1056 // trim trailing whitespace
1057 char* val_end = val_start + strlen(val_start)-1;
1058 while (val_end > val_start && isspace(*val_end)) --val_end;
1059 val_end[1] = '\0';
1060
1061 result = strdup(val_start);
1062 break;
1063
1064 } while ((line = strtok(NULL, "\n")));
1065
1066 if (result == NULL) result = strdup("");
1067
1068 done:
1069 free(filename);
1070 free(key);
1071 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001072 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001073}
1074
1075
Doug Zongker8edb00c2009-06-11 17:21:44 -07001076static bool write_raw_image_cb(const unsigned char* data,
1077 int data_len, void* ctx) {
1078 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
1079 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001080 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001081 return false;
1082}
1083
Doug Zongker179b2d92011-04-12 15:49:04 -07001084// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001085Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001086 char* result = NULL;
1087
Doug Zongker179b2d92011-04-12 15:49:04 -07001088 Value* partition_value;
1089 Value* contents;
1090 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001091 return NULL;
1092 }
1093
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001094 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001095 if (partition_value->type != VAL_STRING) {
1096 ErrorAbort(state, "partition argument to %s must be string", name);
1097 goto done;
1098 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001099 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001100 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001101 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001102 goto done;
1103 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001104 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001105 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001106 goto done;
1107 }
1108
1109 mtd_scan_partitions();
1110 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
1111 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001112 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001113 result = strdup("");
1114 goto done;
1115 }
1116
1117 MtdWriteContext* ctx = mtd_write_partition(mtd);
1118 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001119 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001120 name, partition);
1121 result = strdup("");
1122 goto done;
1123 }
1124
1125 bool success;
1126
Doug Zongker179b2d92011-04-12 15:49:04 -07001127 if (contents->type == VAL_STRING) {
1128 // we're given a filename as the contents
1129 char* filename = contents->data;
1130 FILE* f = fopen(filename, "rb");
1131 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001132 printf("%s: can't open %s: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001133 name, filename, strerror(errno));
1134 result = strdup("");
1135 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001136 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001137
1138 success = true;
1139 char* buffer = malloc(BUFSIZ);
1140 int read;
1141 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1142 int wrote = mtd_write_data(ctx, buffer, read);
1143 success = success && (wrote == read);
1144 }
1145 free(buffer);
1146 fclose(f);
1147 } else {
1148 // we're given a blob as the contents
1149 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1150 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001151 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001152 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001153 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001154 partition, strerror(errno));
1155 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001156
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001157 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001158 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001159 }
1160 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001161 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001162 }
1163
Doug Zongker179b2d92011-04-12 15:49:04 -07001164 printf("%s %s partition\n",
1165 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001166
1167 result = success ? partition : strdup("");
1168
1169done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001170 if (result != partition) FreeValue(partition_value);
1171 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001172 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001173}
1174
Doug Zongker8edb00c2009-06-11 17:21:44 -07001175// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001176Value* ApplyPatchSpaceFn(const char* name, State* state,
1177 int argc, Expr* argv[]) {
1178 char* bytes_str;
1179 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1180 return NULL;
1181 }
1182
1183 char* endptr;
1184 size_t bytes = strtol(bytes_str, &endptr, 10);
1185 if (bytes == 0 && endptr == bytes_str) {
1186 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
1187 name, bytes_str);
1188 free(bytes_str);
1189 return NULL;
1190 }
1191
1192 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1193}
1194
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001195bool CheckMappedFileSha1(FILE* f, DontCareMap* map, uint8_t* intended_digest) {
1196 MapState state;
1197
1198 state.f = f;
1199 state.so_far = 0;
1200 state.cr = 0;
1201 state.map = map;
1202
1203 SHA_CTX ctx;
1204 SHA_init(&ctx);
1205
1206 unsigned char buffer[32173];
1207 size_t bytes_read;
1208
1209 while ((bytes_read = read_with_map(buffer, sizeof(buffer), &state)) > 0) {
1210 SHA_update(&ctx, buffer, bytes_read);
1211 }
1212 const uint8_t* digest = SHA_final(&ctx);
1213
1214 return memcmp(digest, intended_digest, SHA_DIGEST_SIZE) == 0;
1215}
1216
1217
1218// syspatch(file, tgt_mapfile, tgt_sha1, init_mapfile, init_sha1, patch)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001219
Doug Zongker52b40362014-02-10 15:30:30 -08001220Value* SysPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001221 if (argc != 6) {
1222 return ErrorAbort(state, "%s(): expected 6 args, got %d", name, argc);
Doug Zongker52b40362014-02-10 15:30:30 -08001223 }
1224
1225 char* filename;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001226 char* target_mapfilename;
Doug Zongker52b40362014-02-10 15:30:30 -08001227 char* target_sha1;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001228 char* init_mapfilename;
Doug Zongker52b40362014-02-10 15:30:30 -08001229 char* init_sha1;
1230 char* patch_filename;
1231 uint8_t target_digest[SHA_DIGEST_SIZE];
1232 uint8_t init_digest[SHA_DIGEST_SIZE];
1233
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001234 if (ReadArgs(state, argv, 6, &filename,
1235 &target_mapfilename, &target_sha1,
1236 &init_mapfilename, &init_sha1, &patch_filename) < 0) {
Doug Zongker52b40362014-02-10 15:30:30 -08001237 return NULL;
1238 }
1239
Doug Zongker43772d22014-06-09 14:13:19 -07001240 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1241 mapwrite_cmd_pipe = ui->cmd_pipe;
1242
Doug Zongker52b40362014-02-10 15:30:30 -08001243 if (ParseSha1(target_sha1, target_digest) != 0) {
1244 printf("%s(): failed to parse '%s' as target SHA-1", name, target_sha1);
1245 memset(target_digest, 0, SHA_DIGEST_SIZE);
1246 }
1247 if (ParseSha1(init_sha1, init_digest) != 0) {
1248 printf("%s(): failed to parse '%s' as init SHA-1", name, init_sha1);
1249 memset(init_digest, 0, SHA_DIGEST_SIZE);
1250 }
1251
Doug Zongker52b40362014-02-10 15:30:30 -08001252 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001253 FILE* src = fopen(filename, "r");
Doug Zongker52b40362014-02-10 15:30:30 -08001254
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001255 DontCareMap* init_map = ReadDontCareMapFromZip(za, init_mapfilename);
1256 if (init_map == NULL) return ErrorAbort(state, "%s(): failed to read init map\n", name);
1257 DontCareMap* target_map = ReadDontCareMapFromZip(za, target_mapfilename);
1258 if (target_map == NULL) return ErrorAbort(state, "%s(): failed to read target map\n", name);
1259
1260 if (CheckMappedFileSha1(src, init_map, init_digest)) {
1261 // If the partition contents match the init_digest, then we need to apply the patch.
1262
1263 rewind(src);
1264
1265 const ZipEntry* entry = mzFindZipEntry(za, patch_filename);
1266 if (entry == NULL) {
1267 return ErrorAbort(state, "%s(): no %s in package\n", name, patch_filename);
1268 }
1269
1270 unsigned char* patch_data;
1271 size_t patch_len;
1272 if (!mzGetStoredEntry(za, entry, &patch_data, &patch_len)) {
1273 return ErrorAbort(state, "%s(): failed to get %s entry\n", name, patch_filename);
1274 }
1275
1276 FILE* tgt = fopen(filename, "r+");
1277
Doug Zongker43772d22014-06-09 14:13:19 -07001278 int ret = syspatch(src, init_map, patch_data, patch_len, tgt, target_map, progress_cb);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001279
1280 fclose(src);
1281 fclose(tgt);
1282
1283 if (ret != 0) {
1284 return ErrorAbort(state, "%s(): patching failed\n", name);
1285 }
1286 } else {
1287 rewind(src);
1288 if (CheckMappedFileSha1(src, target_map, target_digest)) {
1289 // If the partition contents match the target already, we
1290 // don't need to do anything.
1291 printf("%s: output is already target\n", name);
1292 } else {
1293 return ErrorAbort(state, "%s(): %s in unknown state\n", name, filename);
1294 }
Doug Zongker52b40362014-02-10 15:30:30 -08001295 }
1296
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001297 done:
Doug Zongker52b40362014-02-10 15:30:30 -08001298 free(target_sha1);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001299 free(target_mapfilename);
Doug Zongker52b40362014-02-10 15:30:30 -08001300 free(init_sha1);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001301 free(init_mapfilename);
Doug Zongker52b40362014-02-10 15:30:30 -08001302 free(patch_filename);
1303 return StringValue(filename);
1304
1305}
1306
1307// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1308
Doug Zongker512536a2010-02-17 16:11:44 -08001309Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001310 if (argc < 6 || (argc % 2) == 1) {
1311 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1312 "even number, got %d",
1313 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001314 }
1315
Doug Zongkerc4351c72010-02-22 14:46:32 -08001316 char* source_filename;
1317 char* target_filename;
1318 char* target_sha1;
1319 char* target_size_str;
1320 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1321 &target_sha1, &target_size_str) < 0) {
1322 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001323 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001324
Doug Zongkerc4351c72010-02-22 14:46:32 -08001325 char* endptr;
1326 size_t target_size = strtol(target_size_str, &endptr, 10);
1327 if (target_size == 0 && endptr == target_size_str) {
1328 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1329 name, target_size_str);
1330 free(source_filename);
1331 free(target_filename);
1332 free(target_sha1);
1333 free(target_size_str);
1334 return NULL;
1335 }
1336
1337 int patchcount = (argc-4) / 2;
1338 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001339
1340 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001341 for (i = 0; i < patchcount; ++i) {
1342 if (patches[i*2]->type != VAL_STRING) {
1343 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1344 break;
1345 }
1346 if (patches[i*2+1]->type != VAL_BLOB) {
1347 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1348 break;
1349 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001350 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001351 if (i != patchcount) {
1352 for (i = 0; i < patchcount*2; ++i) {
1353 FreeValue(patches[i]);
1354 }
1355 free(patches);
1356 return NULL;
1357 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001358
Doug Zongkerc4351c72010-02-22 14:46:32 -08001359 char** patch_sha_str = malloc(patchcount * sizeof(char*));
1360 for (i = 0; i < patchcount; ++i) {
1361 patch_sha_str[i] = patches[i*2]->data;
1362 patches[i*2]->data = NULL;
1363 FreeValue(patches[i*2]);
1364 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001365 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001366
1367 int result = applypatch(source_filename, target_filename,
1368 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001369 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001370
1371 for (i = 0; i < patchcount; ++i) {
1372 FreeValue(patches[i]);
1373 }
1374 free(patch_sha_str);
1375 free(patches);
1376
1377 return StringValue(strdup(result == 0 ? "t" : ""));
1378}
1379
1380// apply_patch_check(file, [sha1_1, ...])
1381Value* ApplyPatchCheckFn(const char* name, State* state,
1382 int argc, Expr* argv[]) {
1383 if (argc < 1) {
1384 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1385 name, argc);
1386 }
1387
1388 char* filename;
1389 if (ReadArgs(state, argv, 1, &filename) < 0) {
1390 return NULL;
1391 }
1392
1393 int patchcount = argc-1;
1394 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1395
1396 int result = applypatch_check(filename, patchcount, sha1s);
1397
1398 int i;
1399 for (i = 0; i < patchcount; ++i) {
1400 free(sha1s[i]);
1401 }
1402 free(sha1s);
1403
1404 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001405}
1406
Doug Zongker512536a2010-02-17 16:11:44 -08001407Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001408 char** args = ReadVarArgs(state, argc, argv);
1409 if (args == NULL) {
1410 return NULL;
1411 }
1412
1413 int size = 0;
1414 int i;
1415 for (i = 0; i < argc; ++i) {
1416 size += strlen(args[i]);
1417 }
1418 char* buffer = malloc(size+1);
1419 size = 0;
1420 for (i = 0; i < argc; ++i) {
1421 strcpy(buffer+size, args[i]);
1422 size += strlen(args[i]);
1423 free(args[i]);
1424 }
1425 free(args);
1426 buffer[size] = '\0';
1427
1428 char* line = strtok(buffer, "\n");
1429 while (line) {
1430 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
1431 "ui_print %s\n", line);
1432 line = strtok(NULL, "\n");
1433 }
1434 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
1435
Doug Zongker512536a2010-02-17 16:11:44 -08001436 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001437}
1438
Doug Zongkerd0181b82011-10-19 10:51:12 -07001439Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1440 if (argc != 0) {
1441 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1442 }
1443 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1444 return StringValue(strdup("t"));
1445}
1446
Doug Zongker512536a2010-02-17 16:11:44 -08001447Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001448 if (argc < 1) {
1449 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1450 }
1451 char** args = ReadVarArgs(state, argc, argv);
1452 if (args == NULL) {
1453 return NULL;
1454 }
1455
1456 char** args2 = malloc(sizeof(char*) * (argc+1));
1457 memcpy(args2, args, sizeof(char*) * argc);
1458 args2[argc] = NULL;
1459
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001460 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001461
1462 pid_t child = fork();
1463 if (child == 0) {
1464 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001465 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001466 _exit(1);
1467 }
1468 int status;
1469 waitpid(child, &status, 0);
1470 if (WIFEXITED(status)) {
1471 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001472 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001473 WEXITSTATUS(status));
1474 }
1475 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001476 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001477 WTERMSIG(status));
1478 }
1479
1480 int i;
1481 for (i = 0; i < argc; ++i) {
1482 free(args[i]);
1483 }
1484 free(args);
1485 free(args2);
1486
1487 char buffer[20];
1488 sprintf(buffer, "%d", status);
1489
Doug Zongker512536a2010-02-17 16:11:44 -08001490 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001491}
1492
Doug Zongker512536a2010-02-17 16:11:44 -08001493// sha1_check(data)
1494// to return the sha1 of the data (given in the format returned by
1495// read_file).
1496//
1497// sha1_check(data, sha1_hex, [sha1_hex, ...])
1498// returns the sha1 of the file if it matches any of the hex
1499// strings passed, or "" if it does not equal any of them.
1500//
1501Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1502 if (argc < 1) {
1503 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1504 }
1505
1506 Value** args = ReadValueVarArgs(state, argc, argv);
1507 if (args == NULL) {
1508 return NULL;
1509 }
1510
1511 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001512 return StringValue(strdup(""));
1513 }
1514 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001515 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001516 FreeValue(args[0]);
1517
1518 if (argc == 1) {
1519 return StringValue(PrintSha1(digest));
1520 }
1521
1522 int i;
1523 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1524 for (i = 1; i < argc; ++i) {
1525 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001526 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001527 name, i);
1528 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1529 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001530 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1531 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001532 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1533 break;
1534 }
1535 FreeValue(args[i]);
1536 }
1537 if (i >= argc) {
1538 // Didn't match any of the hex strings; return false.
1539 return StringValue(strdup(""));
1540 }
1541 // Found a match; free all the remaining arguments and return the
1542 // matched one.
1543 int j;
1544 for (j = i+1; j < argc; ++j) {
1545 FreeValue(args[j]);
1546 }
1547 return args[i];
1548}
1549
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001550// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001551// is actually a FileContents*).
1552Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1553 if (argc != 1) {
1554 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1555 }
1556 char* filename;
1557 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1558
1559 Value* v = malloc(sizeof(Value));
1560 v->type = VAL_BLOB;
1561
1562 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001563 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001564 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001565 v->size = -1;
1566 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001567 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001568 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001569 }
1570
1571 v->size = fc.size;
1572 v->data = (char*)fc.data;
1573
1574 free(filename);
1575 return v;
1576}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001577
Doug Zongkerc87bab12013-11-25 13:53:25 -08001578// Immediately reboot the device. Recovery is not finished normally,
1579// so if you reboot into recovery it will re-start applying the
1580// current package (because nothing has cleared the copy of the
1581// arguments stored in the BCB).
1582//
1583// The argument is the partition name passed to the android reboot
1584// property. It can be "recovery" to boot from the recovery
1585// partition, or "" (empty string) to boot from the regular boot
1586// partition.
1587Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1588 if (argc != 2) {
1589 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1590 }
1591
1592 char* filename;
1593 char* property;
1594 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1595
1596 char buffer[80];
1597
1598 // zero out the 'command' field of the bootloader message.
1599 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1600 FILE* f = fopen(filename, "r+b");
1601 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1602 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1603 fclose(f);
1604 free(filename);
1605
1606 strcpy(buffer, "reboot,");
1607 if (property != NULL) {
1608 strncat(buffer, property, sizeof(buffer)-10);
1609 }
1610
1611 property_set(ANDROID_RB_PROPERTY, buffer);
1612
1613 sleep(5);
1614 free(property);
1615 ErrorAbort(state, "%s() failed to reboot", name);
1616 return NULL;
1617}
1618
1619// Store a string value somewhere that future invocations of recovery
1620// can access it. This value is called the "stage" and can be used to
1621// drive packages that need to do reboots in the middle of
1622// installation and keep track of where they are in the multi-stage
1623// install.
1624//
1625// The first argument is the block device for the misc partition
1626// ("/misc" in the fstab), which is where this value is stored. The
1627// second argument is the string to store; it should not exceed 31
1628// bytes.
1629Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1630 if (argc != 2) {
1631 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1632 }
1633
1634 char* filename;
1635 char* stagestr;
1636 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1637
1638 // Store this value in the misc partition, immediately after the
1639 // bootloader message that the main recovery uses to save its
1640 // arguments in case of the device restarting midway through
1641 // package installation.
1642 FILE* f = fopen(filename, "r+b");
1643 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1644 int to_write = strlen(stagestr)+1;
1645 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1646 if (to_write > max_size) {
1647 to_write = max_size;
1648 stagestr[max_size-1] = 0;
1649 }
1650 fwrite(stagestr, to_write, 1, f);
1651 fclose(f);
1652
1653 free(stagestr);
1654 return StringValue(filename);
1655}
1656
1657// Return the value most recently saved with SetStageFn. The argument
1658// is the block device for the misc partition.
1659Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001660 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001661 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1662 }
1663
1664 char* filename;
1665 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1666
1667 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1668 FILE* f = fopen(filename, "rb");
1669 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1670 fread(buffer, sizeof(buffer), 1, f);
1671 fclose(f);
1672 buffer[sizeof(buffer)-1] = '\0';
1673
1674 return StringValue(strdup(buffer));
1675}
1676
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001677Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1678 if (argc != 2) {
1679 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1680 }
1681
1682 char* filename;
1683 char* len_str;
1684 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1685
1686 size_t len = strtoull(len_str, NULL, 0);
1687 int fd = open(filename, O_WRONLY, 0644);
1688 int success = wipe_block_device(fd, len);
1689
1690 free(filename);
1691 free(len_str);
1692
1693 close(fd);
1694
1695 return StringValue(strdup(success ? "t" : ""));
1696}
1697
Doug Zongkerc704e062014-05-23 08:40:35 -07001698Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1699 if (argc != 0) {
1700 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1701 }
1702 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1703 fprintf(ui->cmd_pipe, "enable_reboot\n");
1704 return StringValue(strdup("t"));
1705}
1706
Doug Zongker9931f7f2009-06-10 14:11:53 -07001707void RegisterInstallFunctions() {
1708 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001709 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001710 RegisterFunction("unmount", UnmountFn);
1711 RegisterFunction("format", FormatFn);
1712 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001713 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001714 RegisterFunction("delete", DeleteFn);
1715 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001716 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1717 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001718 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001719
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001720 // Usage:
1721 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1722 // Example:
1723 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1724 RegisterFunction("set_metadata", SetMetadataFn);
1725
1726 // Usage:
1727 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1728 // Example:
1729 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1730 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1731
Doug Zongker8edb00c2009-06-11 17:21:44 -07001732 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001733 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001734 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001735
1736 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001737 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1738 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001739
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001740 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001741 RegisterFunction("syspatch", SysPatchFn);
1742
Doug Zongker512536a2010-02-17 16:11:44 -08001743 RegisterFunction("read_file", ReadFileFn);
1744 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001745 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001746
Doug Zongkerd0181b82011-10-19 10:51:12 -07001747 RegisterFunction("wipe_cache", WipeCacheFn);
1748
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001749 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001750
1751 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001752
1753 RegisterFunction("reboot_now", RebootNowFn);
1754 RegisterFunction("get_stage", GetStageFn);
1755 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001756
1757 RegisterFunction("enable_reboot", EnableRebootFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001758}