blob: 5025881d21ce82391019414d7a29d3cbc4c4727e [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) {
356 ErrorAbort(state, "dst_name argument to %s() can't be empty",
357 name);
358 goto done;
359 }
Michael Runged29f6412014-07-21 17:40:02 -0700360 if (make_parents(dst_name) != 0) {
361 ErrorAbort(state, "Creating parent of %s() failed, error %s()",
362 dst_name, strerror(errno));
363 } else if (rename(src_name, dst_name) != 0) {
Michael Rungece7ca712013-11-06 17:42:20 -0800364 ErrorAbort(state, "Rename of %s() to %s() failed, error %s()",
365 src_name, dst_name, strerror(errno));
366 } else {
367 result = dst_name;
368 }
369
370done:
371 free(src_name);
372 if (result != dst_name) free(dst_name);
373 return StringValue(result);
374}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700375
Doug Zongker512536a2010-02-17 16:11:44 -0800376Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700377 char** paths = malloc(argc * sizeof(char*));
378 int i;
379 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700380 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700381 if (paths[i] == NULL) {
382 int j;
383 for (j = 0; j < i; ++i) {
384 free(paths[j]);
385 }
386 free(paths);
387 return NULL;
388 }
389 }
390
391 bool recursive = (strcmp(name, "delete_recursive") == 0);
392
393 int success = 0;
394 for (i = 0; i < argc; ++i) {
395 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
396 ++success;
397 free(paths[i]);
398 }
399 free(paths);
400
401 char buffer[10];
402 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800403 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700404}
405
Doug Zongker8edb00c2009-06-11 17:21:44 -0700406
Doug Zongker512536a2010-02-17 16:11:44 -0800407Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700408 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700409 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700410 }
411 char* frac_str;
412 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700413 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700414 return NULL;
415 }
416
417 double frac = strtod(frac_str, NULL);
418 int sec = strtol(sec_str, NULL, 10);
419
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700420 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700421 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
422
Doug Zongker9931f7f2009-06-10 14:11:53 -0700423 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800424 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700425}
426
Doug Zongker512536a2010-02-17 16:11:44 -0800427Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700428 if (argc != 1) {
429 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
430 }
431 char* frac_str;
432 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
433 return NULL;
434 }
435
436 double frac = strtod(frac_str, NULL);
437
438 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
439 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
440
Doug Zongker512536a2010-02-17 16:11:44 -0800441 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700442}
443
Doug Zongker8edb00c2009-06-11 17:21:44 -0700444// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800445Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700446 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700447 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700448 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700449 }
450 char* zip_path;
451 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700452 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700453
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700454 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700455
456 // To create a consistent system image, never use the clock for timestamps.
457 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
458
459 bool success = mzExtractRecursive(za, zip_path, dest_path,
460 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500461 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700462 free(zip_path);
463 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800464 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700465}
466
Doug Zongker8edb00c2009-06-11 17:21:44 -0700467
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800468DontCareMap* ReadDontCareMapFromZip(ZipArchive* za, const char* path) {
469 const char* name = "ReadDontCareMapFromZip";
470
471 const ZipEntry* entry = mzFindZipEntry(za, path);
472 if (entry == NULL) {
473 printf("%s: no %s in package\n", name, path);
474 return NULL;
475 }
476
477 size_t map_size = mzGetZipEntryUncompLen(entry);
478 char* map_data = malloc(map_size);
479 if (map_data == NULL) {
480 printf("%s: failed to allocate %zu bytes for %s\n",
481 name, map_size, path);
482 return NULL;
483 }
484
485 if (!mzExtractZipEntryToBuffer(za, entry, (unsigned char*) map_data)) {
486 printf("%s: failed to read %s\n", name, path);
487 return NULL;
488 }
489
490 char* p = map_data;
491 DontCareMap* map = (DontCareMap*) malloc(sizeof(DontCareMap));
492
493 map->block_size = strtoul(p, &p, 0);
494 if (map->block_size != 4096) {
495 printf("%s: unexpected block size %zu\n", name, map->block_size);
496 return NULL;
497 }
498
499 map->region_count = strtoul(p, &p, 0);
500 map->regions = (int*) malloc(map->region_count * sizeof(int));
Doug Zongker43772d22014-06-09 14:13:19 -0700501 map->total_blocks = 0;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800502
503 int i;
504 for (i = 0; i < map->region_count; ++i) {
505 map->regions[i] = strtoul(p, &p, 0);
Doug Zongker43772d22014-06-09 14:13:19 -0700506 map->total_blocks += map->regions[i];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800507 }
508
509 return map;
510}
511
Doug Zongker43772d22014-06-09 14:13:19 -0700512static FILE* mapwrite_cmd_pipe;
513
514static void progress_cb(long done, long total) {
515 if (total > 0) {
516 double frac = (double)done / total;
517 fprintf(mapwrite_cmd_pipe, "set_progress %f\n", frac);
518 fflush(mapwrite_cmd_pipe);
519 }
520}
521
522
523
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800524bool MapWriter(const unsigned char* data, int dataLen, void* cookie) {
Doug Zongker43772d22014-06-09 14:13:19 -0700525 return write_with_map(data, dataLen, (MapState*) cookie, progress_cb) == dataLen;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800526}
527
528// package_extract_file(package_path, destination_path, map_path)
529// or
Doug Zongker8edb00c2009-06-11 17:21:44 -0700530// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800531// or
532// package_extract_file(package_path)
533// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800534// function (the char* returned is actually a FileContents*).
535Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700536 int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800537 if (argc < 1 || argc > 3) {
538 return ErrorAbort(state, "%s() expects 1 or 2 or 3 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800539 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700540 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700541 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700542
543 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
544 mapwrite_cmd_pipe = ui->cmd_pipe;
545
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800546 if (argc >= 2) {
547 // The two-argument version extracts to a file; the three-arg
548 // version extracts to a file, skipping over regions in a
549 // don't care map.
550
551 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700552
Doug Zongker6aece332010-02-01 14:40:12 -0800553 char* zip_path;
554 char* dest_path;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800555 char* map_path = NULL;
556 DontCareMap* map = NULL;
557 if (argc == 2) {
558 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
559 } else {
560 if (ReadArgs(state, argv, 3, &zip_path, &dest_path, &map_path) < 0) return NULL;
561 map = ReadDontCareMapFromZip(za, map_path);
562 if (map == NULL) goto done2;
563 }
Doug Zongker6aece332010-02-01 14:40:12 -0800564
Doug Zongker6aece332010-02-01 14:40:12 -0800565 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
566 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700567 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800568 goto done2;
569 }
570
571 FILE* f = fopen(dest_path, "wb");
572 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700573 printf("%s: can't open %s for write: %s\n",
Doug Zongker6aece332010-02-01 14:40:12 -0800574 name, dest_path, strerror(errno));
575 goto done2;
576 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800577 if (map) {
578 MapState state;
579 state.map = map;
580 state.cr = 0;
581 state.so_far = 0;
582 state.f = f;
583 success = mzProcessZipEntryContents(za, entry, MapWriter, &state);
584 } else {
585 success = mzExtractZipEntryToFile(za, entry, fileno(f));
586 }
Doug Zongker6aece332010-02-01 14:40:12 -0800587 fclose(f);
588
589 done2:
590 free(zip_path);
591 free(dest_path);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800592 free(map_path);
593 if (map) {
594 free(map->regions);
595 free(map);
596 }
Doug Zongker512536a2010-02-17 16:11:44 -0800597 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800598 } else {
599 // The one-argument version returns the contents of the file
600 // as the result.
601
602 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800603 Value* v = malloc(sizeof(Value));
604 v->type = VAL_BLOB;
605 v->size = -1;
606 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800607
608 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
609
610 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
611 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
612 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700613 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800614 goto done1;
615 }
616
Doug Zongker512536a2010-02-17 16:11:44 -0800617 v->size = mzGetZipEntryUncompLen(entry);
618 v->data = malloc(v->size);
619 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700620 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800621 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800622 goto done1;
623 }
624
Doug Zongker512536a2010-02-17 16:11:44 -0800625 success = mzExtractZipEntryToBuffer(za, entry,
626 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800627
628 done1:
629 free(zip_path);
630 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800631 free(v->data);
632 v->data = NULL;
633 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800634 }
Doug Zongker512536a2010-02-17 16:11:44 -0800635 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700636 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700637}
638
Doug Zongkera23075f2012-08-06 16:19:09 -0700639// Create all parent directories of name, if necessary.
640static int make_parents(char* name) {
641 char* p;
642 for (p = name + (strlen(name)-1); p > name; --p) {
643 if (*p != '/') continue;
644 *p = '\0';
645 if (make_parents(name) < 0) return -1;
646 int result = mkdir(name, 0700);
Michael Runged29f6412014-07-21 17:40:02 -0700647 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700648 *p = '/';
649 if (result == 0 || errno == EEXIST) {
650 // successfully created or already existed; we're done
651 return 0;
652 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700653 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700654 return -1;
655 }
656 }
657 return 0;
658}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700659
Doug Zongker9931f7f2009-06-10 14:11:53 -0700660// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700661// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800662Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700663 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700664 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700665 }
666 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700667 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700668 if (target == NULL) return NULL;
669
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700670 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700671 if (srcs == NULL) {
672 free(target);
673 return NULL;
674 }
675
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700676 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700677 int i;
678 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700679 if (unlink(srcs[i]) < 0) {
680 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700681 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700682 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700683 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700684 }
685 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700686 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700687 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700688 name, srcs[i], target);
689 ++bad;
690 }
Doug Zongker60babf82009-09-18 15:11:24 -0700691 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700692 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700693 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700694 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700695 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700696 free(srcs[i]);
697 }
698 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700699 if (bad) {
700 return ErrorAbort(state, "%s: some symlinks failed", name);
701 }
Doug Zongker512536a2010-02-17 16:11:44 -0800702 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700703}
704
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700705struct perm_parsed_args {
706 bool has_uid;
707 uid_t uid;
708 bool has_gid;
709 gid_t gid;
710 bool has_mode;
711 mode_t mode;
712 bool has_fmode;
713 mode_t fmode;
714 bool has_dmode;
715 mode_t dmode;
716 bool has_selabel;
717 char* selabel;
718 bool has_capabilities;
719 uint64_t capabilities;
720};
721
722static struct perm_parsed_args ParsePermArgs(int argc, char** args) {
723 int i;
724 struct perm_parsed_args parsed;
725 int bad = 0;
726 static int max_warnings = 20;
727
728 memset(&parsed, 0, sizeof(parsed));
729
730 for (i = 1; i < argc; i += 2) {
731 if (strcmp("uid", args[i]) == 0) {
732 int64_t uid;
733 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
734 parsed.uid = uid;
735 parsed.has_uid = true;
736 } else {
737 printf("ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
738 bad++;
739 }
740 continue;
741 }
742 if (strcmp("gid", args[i]) == 0) {
743 int64_t gid;
744 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
745 parsed.gid = gid;
746 parsed.has_gid = true;
747 } else {
748 printf("ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
749 bad++;
750 }
751 continue;
752 }
753 if (strcmp("mode", args[i]) == 0) {
754 int32_t mode;
755 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
756 parsed.mode = mode;
757 parsed.has_mode = true;
758 } else {
759 printf("ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
760 bad++;
761 }
762 continue;
763 }
764 if (strcmp("dmode", args[i]) == 0) {
765 int32_t mode;
766 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
767 parsed.dmode = mode;
768 parsed.has_dmode = true;
769 } else {
770 printf("ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
771 bad++;
772 }
773 continue;
774 }
775 if (strcmp("fmode", args[i]) == 0) {
776 int32_t mode;
777 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
778 parsed.fmode = mode;
779 parsed.has_fmode = true;
780 } else {
781 printf("ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
782 bad++;
783 }
784 continue;
785 }
786 if (strcmp("capabilities", args[i]) == 0) {
787 int64_t capabilities;
788 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
789 parsed.capabilities = capabilities;
790 parsed.has_capabilities = true;
791 } else {
792 printf("ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
793 bad++;
794 }
795 continue;
796 }
797 if (strcmp("selabel", args[i]) == 0) {
798 if (args[i+1][0] != '\0') {
799 parsed.selabel = args[i+1];
800 parsed.has_selabel = true;
801 } else {
802 printf("ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
803 bad++;
804 }
805 continue;
806 }
807 if (max_warnings != 0) {
808 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
809 max_warnings--;
810 if (max_warnings == 0) {
811 printf("ParsedPermArgs: suppressing further warnings\n");
812 }
813 }
814 }
815 return parsed;
816}
817
818static int ApplyParsedPerms(
819 const char* filename,
820 const struct stat *statptr,
821 struct perm_parsed_args parsed)
822{
823 int bad = 0;
824
Nick Kraleviche4612512013-09-10 15:34:19 -0700825 /* ignore symlinks */
826 if (S_ISLNK(statptr->st_mode)) {
827 return 0;
828 }
829
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700830 if (parsed.has_uid) {
831 if (chown(filename, parsed.uid, -1) < 0) {
832 printf("ApplyParsedPerms: chown of %s to %d failed: %s\n",
833 filename, parsed.uid, strerror(errno));
834 bad++;
835 }
836 }
837
838 if (parsed.has_gid) {
839 if (chown(filename, -1, parsed.gid) < 0) {
840 printf("ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
841 filename, parsed.gid, strerror(errno));
842 bad++;
843 }
844 }
845
846 if (parsed.has_mode) {
847 if (chmod(filename, parsed.mode) < 0) {
848 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
849 filename, parsed.mode, strerror(errno));
850 bad++;
851 }
852 }
853
854 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
855 if (chmod(filename, parsed.dmode) < 0) {
856 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
857 filename, parsed.dmode, strerror(errno));
858 bad++;
859 }
860 }
861
862 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
863 if (chmod(filename, parsed.fmode) < 0) {
864 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
865 filename, parsed.fmode, strerror(errno));
866 bad++;
867 }
868 }
869
870 if (parsed.has_selabel) {
871 // TODO: Don't silently ignore ENOTSUP
872 if (lsetfilecon(filename, parsed.selabel) && (errno != ENOTSUP)) {
873 printf("ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
874 filename, parsed.selabel, strerror(errno));
875 bad++;
876 }
877 }
878
879 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
880 if (parsed.capabilities == 0) {
881 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
882 // Report failure unless it's ENODATA (attribute not set)
883 printf("ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
884 filename, parsed.capabilities, strerror(errno));
885 bad++;
886 }
887 } else {
888 struct vfs_cap_data cap_data;
889 memset(&cap_data, 0, sizeof(cap_data));
890 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
891 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
892 cap_data.data[0].inheritable = 0;
893 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
894 cap_data.data[1].inheritable = 0;
895 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
896 printf("ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
897 filename, parsed.capabilities, strerror(errno));
898 bad++;
899 }
900 }
901 }
902
903 return bad;
904}
905
906// nftw doesn't allow us to pass along context, so we need to use
907// global variables. *sigh*
908static struct perm_parsed_args recursive_parsed_args;
909
910static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
911 int fileflags, struct FTW *pfwt) {
912 return ApplyParsedPerms(filename, statptr, recursive_parsed_args);
913}
914
915static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
916 int i;
917 int bad = 0;
918 static int nwarnings = 0;
919 struct stat sb;
920 Value* result = NULL;
921
922 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
923
924 if ((argc % 2) != 1) {
925 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d",
926 name, argc);
927 }
928
929 char** args = ReadVarArgs(state, argc, argv);
930 if (args == NULL) return NULL;
931
932 if (lstat(args[0], &sb) == -1) {
933 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
934 goto done;
935 }
936
937 struct perm_parsed_args parsed = ParsePermArgs(argc, args);
938
939 if (recursive) {
940 recursive_parsed_args = parsed;
941 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
942 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
943 } else {
944 bad += ApplyParsedPerms(args[0], &sb, parsed);
945 }
946
947done:
948 for (i = 0; i < argc; ++i) {
949 free(args[i]);
950 }
951 free(args);
952
953 if (result != NULL) {
954 return result;
955 }
956
957 if (bad > 0) {
958 return ErrorAbort(state, "%s: some changes failed", name);
959 }
960
961 return StringValue(strdup(""));
962}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700963
Doug Zongker512536a2010-02-17 16:11:44 -0800964Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700965 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700966 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700967 }
968 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700969 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700970 if (key == NULL) return NULL;
971
972 char value[PROPERTY_VALUE_MAX];
973 property_get(key, value, "");
974 free(key);
975
Doug Zongker512536a2010-02-17 16:11:44 -0800976 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700977}
978
979
Doug Zongker47cace92009-06-18 10:11:50 -0700980// file_getprop(file, key)
981//
982// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700983// per line. # comment lines,blank lines, lines without '=' ignored),
984// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800985Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700986 char* result = NULL;
987 char* buffer = NULL;
988 char* filename;
989 char* key;
990 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
991 return NULL;
992 }
993
994 struct stat st;
995 if (stat(filename, &st) < 0) {
996 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
997 name, filename, strerror(errno));
998 goto done;
999 }
1000
1001#define MAX_FILE_GETPROP_SIZE 65536
1002
1003 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
1004 ErrorAbort(state, "%s too large for %s (max %d)",
1005 filename, name, MAX_FILE_GETPROP_SIZE);
1006 goto done;
1007 }
1008
1009 buffer = malloc(st.st_size+1);
1010 if (buffer == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -07001011 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -07001012 goto done;
1013 }
1014
1015 FILE* f = fopen(filename, "rb");
1016 if (f == NULL) {
1017 ErrorAbort(state, "%s: failed to open %s: %s",
1018 name, filename, strerror(errno));
1019 goto done;
1020 }
1021
1022 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -07001023 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -07001024 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -07001025 fclose(f);
1026 goto done;
1027 }
1028 buffer[st.st_size] = '\0';
1029
1030 fclose(f);
1031
1032 char* line = strtok(buffer, "\n");
1033 do {
1034 // skip whitespace at start of line
1035 while (*line && isspace(*line)) ++line;
1036
1037 // comment or blank line: skip to next line
1038 if (*line == '\0' || *line == '#') continue;
1039
1040 char* equal = strchr(line, '=');
1041 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001042 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001043 }
1044
1045 // trim whitespace between key and '='
1046 char* key_end = equal-1;
1047 while (key_end > line && isspace(*key_end)) --key_end;
1048 key_end[1] = '\0';
1049
1050 // not the key we're looking for
1051 if (strcmp(key, line) != 0) continue;
1052
1053 // skip whitespace after the '=' to the start of the value
1054 char* val_start = equal+1;
1055 while(*val_start && isspace(*val_start)) ++val_start;
1056
1057 // trim trailing whitespace
1058 char* val_end = val_start + strlen(val_start)-1;
1059 while (val_end > val_start && isspace(*val_end)) --val_end;
1060 val_end[1] = '\0';
1061
1062 result = strdup(val_start);
1063 break;
1064
1065 } while ((line = strtok(NULL, "\n")));
1066
1067 if (result == NULL) result = strdup("");
1068
1069 done:
1070 free(filename);
1071 free(key);
1072 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001073 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001074}
1075
1076
Doug Zongker8edb00c2009-06-11 17:21:44 -07001077static bool write_raw_image_cb(const unsigned char* data,
1078 int data_len, void* ctx) {
1079 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
1080 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001081 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001082 return false;
1083}
1084
Doug Zongker179b2d92011-04-12 15:49:04 -07001085// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001086Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001087 char* result = NULL;
1088
Doug Zongker179b2d92011-04-12 15:49:04 -07001089 Value* partition_value;
1090 Value* contents;
1091 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001092 return NULL;
1093 }
1094
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001095 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001096 if (partition_value->type != VAL_STRING) {
1097 ErrorAbort(state, "partition argument to %s must be string", name);
1098 goto done;
1099 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001100 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001101 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001102 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001103 goto done;
1104 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001105 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001106 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001107 goto done;
1108 }
1109
1110 mtd_scan_partitions();
1111 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
1112 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001113 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001114 result = strdup("");
1115 goto done;
1116 }
1117
1118 MtdWriteContext* ctx = mtd_write_partition(mtd);
1119 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001120 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001121 name, partition);
1122 result = strdup("");
1123 goto done;
1124 }
1125
1126 bool success;
1127
Doug Zongker179b2d92011-04-12 15:49:04 -07001128 if (contents->type == VAL_STRING) {
1129 // we're given a filename as the contents
1130 char* filename = contents->data;
1131 FILE* f = fopen(filename, "rb");
1132 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001133 printf("%s: can't open %s: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001134 name, filename, strerror(errno));
1135 result = strdup("");
1136 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001137 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001138
1139 success = true;
1140 char* buffer = malloc(BUFSIZ);
1141 int read;
1142 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1143 int wrote = mtd_write_data(ctx, buffer, read);
1144 success = success && (wrote == read);
1145 }
1146 free(buffer);
1147 fclose(f);
1148 } else {
1149 // we're given a blob as the contents
1150 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1151 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001152 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001153 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001154 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001155 partition, strerror(errno));
1156 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001157
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001158 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001159 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001160 }
1161 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001162 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001163 }
1164
Doug Zongker179b2d92011-04-12 15:49:04 -07001165 printf("%s %s partition\n",
1166 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001167
1168 result = success ? partition : strdup("");
1169
1170done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001171 if (result != partition) FreeValue(partition_value);
1172 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001173 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001174}
1175
Doug Zongker8edb00c2009-06-11 17:21:44 -07001176// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001177Value* ApplyPatchSpaceFn(const char* name, State* state,
1178 int argc, Expr* argv[]) {
1179 char* bytes_str;
1180 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1181 return NULL;
1182 }
1183
1184 char* endptr;
1185 size_t bytes = strtol(bytes_str, &endptr, 10);
1186 if (bytes == 0 && endptr == bytes_str) {
1187 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
1188 name, bytes_str);
1189 free(bytes_str);
1190 return NULL;
1191 }
1192
1193 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1194}
1195
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001196bool CheckMappedFileSha1(FILE* f, DontCareMap* map, uint8_t* intended_digest) {
1197 MapState state;
1198
1199 state.f = f;
1200 state.so_far = 0;
1201 state.cr = 0;
1202 state.map = map;
1203
1204 SHA_CTX ctx;
1205 SHA_init(&ctx);
1206
1207 unsigned char buffer[32173];
1208 size_t bytes_read;
1209
1210 while ((bytes_read = read_with_map(buffer, sizeof(buffer), &state)) > 0) {
1211 SHA_update(&ctx, buffer, bytes_read);
1212 }
1213 const uint8_t* digest = SHA_final(&ctx);
1214
1215 return memcmp(digest, intended_digest, SHA_DIGEST_SIZE) == 0;
1216}
1217
1218
1219// syspatch(file, tgt_mapfile, tgt_sha1, init_mapfile, init_sha1, patch)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001220
Doug Zongker52b40362014-02-10 15:30:30 -08001221Value* SysPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001222 if (argc != 6) {
1223 return ErrorAbort(state, "%s(): expected 6 args, got %d", name, argc);
Doug Zongker52b40362014-02-10 15:30:30 -08001224 }
1225
1226 char* filename;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001227 char* target_mapfilename;
Doug Zongker52b40362014-02-10 15:30:30 -08001228 char* target_sha1;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001229 char* init_mapfilename;
Doug Zongker52b40362014-02-10 15:30:30 -08001230 char* init_sha1;
1231 char* patch_filename;
1232 uint8_t target_digest[SHA_DIGEST_SIZE];
1233 uint8_t init_digest[SHA_DIGEST_SIZE];
1234
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001235 if (ReadArgs(state, argv, 6, &filename,
1236 &target_mapfilename, &target_sha1,
1237 &init_mapfilename, &init_sha1, &patch_filename) < 0) {
Doug Zongker52b40362014-02-10 15:30:30 -08001238 return NULL;
1239 }
1240
Doug Zongker43772d22014-06-09 14:13:19 -07001241 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1242 mapwrite_cmd_pipe = ui->cmd_pipe;
1243
Doug Zongker52b40362014-02-10 15:30:30 -08001244 if (ParseSha1(target_sha1, target_digest) != 0) {
1245 printf("%s(): failed to parse '%s' as target SHA-1", name, target_sha1);
1246 memset(target_digest, 0, SHA_DIGEST_SIZE);
1247 }
1248 if (ParseSha1(init_sha1, init_digest) != 0) {
1249 printf("%s(): failed to parse '%s' as init SHA-1", name, init_sha1);
1250 memset(init_digest, 0, SHA_DIGEST_SIZE);
1251 }
1252
Doug Zongker52b40362014-02-10 15:30:30 -08001253 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001254 FILE* src = fopen(filename, "r");
Doug Zongker52b40362014-02-10 15:30:30 -08001255
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001256 DontCareMap* init_map = ReadDontCareMapFromZip(za, init_mapfilename);
1257 if (init_map == NULL) return ErrorAbort(state, "%s(): failed to read init map\n", name);
1258 DontCareMap* target_map = ReadDontCareMapFromZip(za, target_mapfilename);
1259 if (target_map == NULL) return ErrorAbort(state, "%s(): failed to read target map\n", name);
1260
1261 if (CheckMappedFileSha1(src, init_map, init_digest)) {
1262 // If the partition contents match the init_digest, then we need to apply the patch.
1263
1264 rewind(src);
1265
1266 const ZipEntry* entry = mzFindZipEntry(za, patch_filename);
1267 if (entry == NULL) {
1268 return ErrorAbort(state, "%s(): no %s in package\n", name, patch_filename);
1269 }
1270
1271 unsigned char* patch_data;
1272 size_t patch_len;
1273 if (!mzGetStoredEntry(za, entry, &patch_data, &patch_len)) {
1274 return ErrorAbort(state, "%s(): failed to get %s entry\n", name, patch_filename);
1275 }
1276
1277 FILE* tgt = fopen(filename, "r+");
1278
Doug Zongker43772d22014-06-09 14:13:19 -07001279 int ret = syspatch(src, init_map, patch_data, patch_len, tgt, target_map, progress_cb);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001280
1281 fclose(src);
1282 fclose(tgt);
1283
1284 if (ret != 0) {
1285 return ErrorAbort(state, "%s(): patching failed\n", name);
1286 }
1287 } else {
1288 rewind(src);
1289 if (CheckMappedFileSha1(src, target_map, target_digest)) {
1290 // If the partition contents match the target already, we
1291 // don't need to do anything.
1292 printf("%s: output is already target\n", name);
1293 } else {
1294 return ErrorAbort(state, "%s(): %s in unknown state\n", name, filename);
1295 }
Doug Zongker52b40362014-02-10 15:30:30 -08001296 }
1297
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001298 done:
Doug Zongker52b40362014-02-10 15:30:30 -08001299 free(target_sha1);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001300 free(target_mapfilename);
Doug Zongker52b40362014-02-10 15:30:30 -08001301 free(init_sha1);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001302 free(init_mapfilename);
Doug Zongker52b40362014-02-10 15:30:30 -08001303 free(patch_filename);
1304 return StringValue(filename);
1305
1306}
1307
1308// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1309
Doug Zongker512536a2010-02-17 16:11:44 -08001310Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001311 if (argc < 6 || (argc % 2) == 1) {
1312 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1313 "even number, got %d",
1314 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001315 }
1316
Doug Zongkerc4351c72010-02-22 14:46:32 -08001317 char* source_filename;
1318 char* target_filename;
1319 char* target_sha1;
1320 char* target_size_str;
1321 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1322 &target_sha1, &target_size_str) < 0) {
1323 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001324 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001325
Doug Zongkerc4351c72010-02-22 14:46:32 -08001326 char* endptr;
1327 size_t target_size = strtol(target_size_str, &endptr, 10);
1328 if (target_size == 0 && endptr == target_size_str) {
1329 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1330 name, target_size_str);
1331 free(source_filename);
1332 free(target_filename);
1333 free(target_sha1);
1334 free(target_size_str);
1335 return NULL;
1336 }
1337
1338 int patchcount = (argc-4) / 2;
1339 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001340
1341 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001342 for (i = 0; i < patchcount; ++i) {
1343 if (patches[i*2]->type != VAL_STRING) {
1344 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1345 break;
1346 }
1347 if (patches[i*2+1]->type != VAL_BLOB) {
1348 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1349 break;
1350 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001351 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001352 if (i != patchcount) {
1353 for (i = 0; i < patchcount*2; ++i) {
1354 FreeValue(patches[i]);
1355 }
1356 free(patches);
1357 return NULL;
1358 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001359
Doug Zongkerc4351c72010-02-22 14:46:32 -08001360 char** patch_sha_str = malloc(patchcount * sizeof(char*));
1361 for (i = 0; i < patchcount; ++i) {
1362 patch_sha_str[i] = patches[i*2]->data;
1363 patches[i*2]->data = NULL;
1364 FreeValue(patches[i*2]);
1365 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001366 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001367
1368 int result = applypatch(source_filename, target_filename,
1369 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001370 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001371
1372 for (i = 0; i < patchcount; ++i) {
1373 FreeValue(patches[i]);
1374 }
1375 free(patch_sha_str);
1376 free(patches);
1377
1378 return StringValue(strdup(result == 0 ? "t" : ""));
1379}
1380
1381// apply_patch_check(file, [sha1_1, ...])
1382Value* ApplyPatchCheckFn(const char* name, State* state,
1383 int argc, Expr* argv[]) {
1384 if (argc < 1) {
1385 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1386 name, argc);
1387 }
1388
1389 char* filename;
1390 if (ReadArgs(state, argv, 1, &filename) < 0) {
1391 return NULL;
1392 }
1393
1394 int patchcount = argc-1;
1395 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1396
1397 int result = applypatch_check(filename, patchcount, sha1s);
1398
1399 int i;
1400 for (i = 0; i < patchcount; ++i) {
1401 free(sha1s[i]);
1402 }
1403 free(sha1s);
1404
1405 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001406}
1407
Doug Zongker512536a2010-02-17 16:11:44 -08001408Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001409 char** args = ReadVarArgs(state, argc, argv);
1410 if (args == NULL) {
1411 return NULL;
1412 }
1413
1414 int size = 0;
1415 int i;
1416 for (i = 0; i < argc; ++i) {
1417 size += strlen(args[i]);
1418 }
1419 char* buffer = malloc(size+1);
1420 size = 0;
1421 for (i = 0; i < argc; ++i) {
1422 strcpy(buffer+size, args[i]);
1423 size += strlen(args[i]);
1424 free(args[i]);
1425 }
1426 free(args);
1427 buffer[size] = '\0';
1428
1429 char* line = strtok(buffer, "\n");
1430 while (line) {
1431 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
1432 "ui_print %s\n", line);
1433 line = strtok(NULL, "\n");
1434 }
1435 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
1436
Doug Zongker512536a2010-02-17 16:11:44 -08001437 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001438}
1439
Doug Zongkerd0181b82011-10-19 10:51:12 -07001440Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1441 if (argc != 0) {
1442 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1443 }
1444 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1445 return StringValue(strdup("t"));
1446}
1447
Doug Zongker512536a2010-02-17 16:11:44 -08001448Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001449 if (argc < 1) {
1450 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1451 }
1452 char** args = ReadVarArgs(state, argc, argv);
1453 if (args == NULL) {
1454 return NULL;
1455 }
1456
1457 char** args2 = malloc(sizeof(char*) * (argc+1));
1458 memcpy(args2, args, sizeof(char*) * argc);
1459 args2[argc] = NULL;
1460
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001461 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001462
1463 pid_t child = fork();
1464 if (child == 0) {
1465 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001466 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001467 _exit(1);
1468 }
1469 int status;
1470 waitpid(child, &status, 0);
1471 if (WIFEXITED(status)) {
1472 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001473 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001474 WEXITSTATUS(status));
1475 }
1476 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001477 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001478 WTERMSIG(status));
1479 }
1480
1481 int i;
1482 for (i = 0; i < argc; ++i) {
1483 free(args[i]);
1484 }
1485 free(args);
1486 free(args2);
1487
1488 char buffer[20];
1489 sprintf(buffer, "%d", status);
1490
Doug Zongker512536a2010-02-17 16:11:44 -08001491 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001492}
1493
Doug Zongker512536a2010-02-17 16:11:44 -08001494// sha1_check(data)
1495// to return the sha1 of the data (given in the format returned by
1496// read_file).
1497//
1498// sha1_check(data, sha1_hex, [sha1_hex, ...])
1499// returns the sha1 of the file if it matches any of the hex
1500// strings passed, or "" if it does not equal any of them.
1501//
1502Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1503 if (argc < 1) {
1504 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1505 }
1506
1507 Value** args = ReadValueVarArgs(state, argc, argv);
1508 if (args == NULL) {
1509 return NULL;
1510 }
1511
1512 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001513 return StringValue(strdup(""));
1514 }
1515 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001516 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001517 FreeValue(args[0]);
1518
1519 if (argc == 1) {
1520 return StringValue(PrintSha1(digest));
1521 }
1522
1523 int i;
1524 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1525 for (i = 1; i < argc; ++i) {
1526 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001527 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001528 name, i);
1529 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1530 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001531 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1532 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001533 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1534 break;
1535 }
1536 FreeValue(args[i]);
1537 }
1538 if (i >= argc) {
1539 // Didn't match any of the hex strings; return false.
1540 return StringValue(strdup(""));
1541 }
1542 // Found a match; free all the remaining arguments and return the
1543 // matched one.
1544 int j;
1545 for (j = i+1; j < argc; ++j) {
1546 FreeValue(args[j]);
1547 }
1548 return args[i];
1549}
1550
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001551// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001552// is actually a FileContents*).
1553Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1554 if (argc != 1) {
1555 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1556 }
1557 char* filename;
1558 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1559
1560 Value* v = malloc(sizeof(Value));
1561 v->type = VAL_BLOB;
1562
1563 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001564 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001565 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001566 v->size = -1;
1567 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001568 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001569 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001570 }
1571
1572 v->size = fc.size;
1573 v->data = (char*)fc.data;
1574
1575 free(filename);
1576 return v;
1577}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001578
Doug Zongkerc87bab12013-11-25 13:53:25 -08001579// Immediately reboot the device. Recovery is not finished normally,
1580// so if you reboot into recovery it will re-start applying the
1581// current package (because nothing has cleared the copy of the
1582// arguments stored in the BCB).
1583//
1584// The argument is the partition name passed to the android reboot
1585// property. It can be "recovery" to boot from the recovery
1586// partition, or "" (empty string) to boot from the regular boot
1587// partition.
1588Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1589 if (argc != 2) {
1590 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1591 }
1592
1593 char* filename;
1594 char* property;
1595 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1596
1597 char buffer[80];
1598
1599 // zero out the 'command' field of the bootloader message.
1600 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1601 FILE* f = fopen(filename, "r+b");
1602 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1603 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1604 fclose(f);
1605 free(filename);
1606
1607 strcpy(buffer, "reboot,");
1608 if (property != NULL) {
1609 strncat(buffer, property, sizeof(buffer)-10);
1610 }
1611
1612 property_set(ANDROID_RB_PROPERTY, buffer);
1613
1614 sleep(5);
1615 free(property);
1616 ErrorAbort(state, "%s() failed to reboot", name);
1617 return NULL;
1618}
1619
1620// Store a string value somewhere that future invocations of recovery
1621// can access it. This value is called the "stage" and can be used to
1622// drive packages that need to do reboots in the middle of
1623// installation and keep track of where they are in the multi-stage
1624// install.
1625//
1626// The first argument is the block device for the misc partition
1627// ("/misc" in the fstab), which is where this value is stored. The
1628// second argument is the string to store; it should not exceed 31
1629// bytes.
1630Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1631 if (argc != 2) {
1632 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1633 }
1634
1635 char* filename;
1636 char* stagestr;
1637 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1638
1639 // Store this value in the misc partition, immediately after the
1640 // bootloader message that the main recovery uses to save its
1641 // arguments in case of the device restarting midway through
1642 // package installation.
1643 FILE* f = fopen(filename, "r+b");
1644 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1645 int to_write = strlen(stagestr)+1;
1646 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1647 if (to_write > max_size) {
1648 to_write = max_size;
1649 stagestr[max_size-1] = 0;
1650 }
1651 fwrite(stagestr, to_write, 1, f);
1652 fclose(f);
1653
1654 free(stagestr);
1655 return StringValue(filename);
1656}
1657
1658// Return the value most recently saved with SetStageFn. The argument
1659// is the block device for the misc partition.
1660Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001661 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001662 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1663 }
1664
1665 char* filename;
1666 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1667
1668 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1669 FILE* f = fopen(filename, "rb");
1670 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1671 fread(buffer, sizeof(buffer), 1, f);
1672 fclose(f);
1673 buffer[sizeof(buffer)-1] = '\0';
1674
1675 return StringValue(strdup(buffer));
1676}
1677
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001678Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1679 if (argc != 2) {
1680 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1681 }
1682
1683 char* filename;
1684 char* len_str;
1685 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1686
1687 size_t len = strtoull(len_str, NULL, 0);
1688 int fd = open(filename, O_WRONLY, 0644);
1689 int success = wipe_block_device(fd, len);
1690
1691 free(filename);
1692 free(len_str);
1693
1694 close(fd);
1695
1696 return StringValue(strdup(success ? "t" : ""));
1697}
1698
Doug Zongkerc704e062014-05-23 08:40:35 -07001699Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1700 if (argc != 0) {
1701 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1702 }
1703 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1704 fprintf(ui->cmd_pipe, "enable_reboot\n");
1705 return StringValue(strdup("t"));
1706}
1707
Doug Zongker9931f7f2009-06-10 14:11:53 -07001708void RegisterInstallFunctions() {
1709 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001710 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001711 RegisterFunction("unmount", UnmountFn);
1712 RegisterFunction("format", FormatFn);
1713 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001714 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001715 RegisterFunction("delete", DeleteFn);
1716 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001717 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1718 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001719 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001720
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001721 // Usage:
1722 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1723 // Example:
1724 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1725 RegisterFunction("set_metadata", SetMetadataFn);
1726
1727 // Usage:
1728 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1729 // Example:
1730 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1731 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1732
Doug Zongker8edb00c2009-06-11 17:21:44 -07001733 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001734 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001735 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001736
1737 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001738 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1739 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001740
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001741 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001742 RegisterFunction("syspatch", SysPatchFn);
1743
Doug Zongker512536a2010-02-17 16:11:44 -08001744 RegisterFunction("read_file", ReadFileFn);
1745 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001746 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001747
Doug Zongkerd0181b82011-10-19 10:51:12 -07001748 RegisterFunction("wipe_cache", WipeCacheFn);
1749
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001750 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001751
1752 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001753
1754 RegisterFunction("reboot_now", RebootNowFn);
1755 RegisterFunction("get_stage", GetStageFn);
1756 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001757
1758 RegisterFunction("enable_reboot", EnableRebootFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001759}