blob: edc386dc680e88183e305f68b4d034077be2cdc1 [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 }
360
361 if (rename(src_name, dst_name) != 0) {
362 ErrorAbort(state, "Rename of %s() to %s() failed, error %s()",
363 src_name, dst_name, strerror(errno));
364 } else {
365 result = dst_name;
366 }
367
368done:
369 free(src_name);
370 if (result != dst_name) free(dst_name);
371 return StringValue(result);
372}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700373
Doug Zongker512536a2010-02-17 16:11:44 -0800374Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700375 char** paths = malloc(argc * sizeof(char*));
376 int i;
377 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700378 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700379 if (paths[i] == NULL) {
380 int j;
381 for (j = 0; j < i; ++i) {
382 free(paths[j]);
383 }
384 free(paths);
385 return NULL;
386 }
387 }
388
389 bool recursive = (strcmp(name, "delete_recursive") == 0);
390
391 int success = 0;
392 for (i = 0; i < argc; ++i) {
393 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
394 ++success;
395 free(paths[i]);
396 }
397 free(paths);
398
399 char buffer[10];
400 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800401 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700402}
403
Doug Zongker8edb00c2009-06-11 17:21:44 -0700404
Doug Zongker512536a2010-02-17 16:11:44 -0800405Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700406 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700407 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700408 }
409 char* frac_str;
410 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700411 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700412 return NULL;
413 }
414
415 double frac = strtod(frac_str, NULL);
416 int sec = strtol(sec_str, NULL, 10);
417
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700418 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700419 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
420
Doug Zongker9931f7f2009-06-10 14:11:53 -0700421 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800422 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700423}
424
Doug Zongker512536a2010-02-17 16:11:44 -0800425Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700426 if (argc != 1) {
427 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
428 }
429 char* frac_str;
430 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
431 return NULL;
432 }
433
434 double frac = strtod(frac_str, NULL);
435
436 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
437 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
438
Doug Zongker512536a2010-02-17 16:11:44 -0800439 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700440}
441
Doug Zongker8edb00c2009-06-11 17:21:44 -0700442// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800443Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700444 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700445 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700446 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700447 }
448 char* zip_path;
449 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700450 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700451
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700452 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700453
454 // To create a consistent system image, never use the clock for timestamps.
455 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
456
457 bool success = mzExtractRecursive(za, zip_path, dest_path,
458 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500459 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700460 free(zip_path);
461 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800462 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700463}
464
Doug Zongker8edb00c2009-06-11 17:21:44 -0700465
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800466DontCareMap* ReadDontCareMapFromZip(ZipArchive* za, const char* path) {
467 const char* name = "ReadDontCareMapFromZip";
468
469 const ZipEntry* entry = mzFindZipEntry(za, path);
470 if (entry == NULL) {
471 printf("%s: no %s in package\n", name, path);
472 return NULL;
473 }
474
475 size_t map_size = mzGetZipEntryUncompLen(entry);
476 char* map_data = malloc(map_size);
477 if (map_data == NULL) {
478 printf("%s: failed to allocate %zu bytes for %s\n",
479 name, map_size, path);
480 return NULL;
481 }
482
483 if (!mzExtractZipEntryToBuffer(za, entry, (unsigned char*) map_data)) {
484 printf("%s: failed to read %s\n", name, path);
485 return NULL;
486 }
487
488 char* p = map_data;
489 DontCareMap* map = (DontCareMap*) malloc(sizeof(DontCareMap));
490
491 map->block_size = strtoul(p, &p, 0);
492 if (map->block_size != 4096) {
493 printf("%s: unexpected block size %zu\n", name, map->block_size);
494 return NULL;
495 }
496
497 map->region_count = strtoul(p, &p, 0);
498 map->regions = (int*) malloc(map->region_count * sizeof(int));
Doug Zongker43772d22014-06-09 14:13:19 -0700499 map->total_blocks = 0;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800500
501 int i;
502 for (i = 0; i < map->region_count; ++i) {
503 map->regions[i] = strtoul(p, &p, 0);
Doug Zongker43772d22014-06-09 14:13:19 -0700504 map->total_blocks += map->regions[i];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800505 }
506
507 return map;
508}
509
Doug Zongker43772d22014-06-09 14:13:19 -0700510static FILE* mapwrite_cmd_pipe;
511
512static void progress_cb(long done, long total) {
513 if (total > 0) {
514 double frac = (double)done / total;
515 fprintf(mapwrite_cmd_pipe, "set_progress %f\n", frac);
516 fflush(mapwrite_cmd_pipe);
517 }
518}
519
520
521
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800522bool MapWriter(const unsigned char* data, int dataLen, void* cookie) {
Doug Zongker43772d22014-06-09 14:13:19 -0700523 return write_with_map(data, dataLen, (MapState*) cookie, progress_cb) == dataLen;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800524}
525
526// package_extract_file(package_path, destination_path, map_path)
527// or
Doug Zongker8edb00c2009-06-11 17:21:44 -0700528// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800529// or
530// package_extract_file(package_path)
531// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800532// function (the char* returned is actually a FileContents*).
533Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700534 int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800535 if (argc < 1 || argc > 3) {
536 return ErrorAbort(state, "%s() expects 1 or 2 or 3 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800537 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700538 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700539 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700540
541 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
542 mapwrite_cmd_pipe = ui->cmd_pipe;
543
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800544 if (argc >= 2) {
545 // The two-argument version extracts to a file; the three-arg
546 // version extracts to a file, skipping over regions in a
547 // don't care map.
548
549 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700550
Doug Zongker6aece332010-02-01 14:40:12 -0800551 char* zip_path;
552 char* dest_path;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800553 char* map_path = NULL;
554 DontCareMap* map = NULL;
555 if (argc == 2) {
556 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
557 } else {
558 if (ReadArgs(state, argv, 3, &zip_path, &dest_path, &map_path) < 0) return NULL;
559 map = ReadDontCareMapFromZip(za, map_path);
560 if (map == NULL) goto done2;
561 }
Doug Zongker6aece332010-02-01 14:40:12 -0800562
Doug Zongker6aece332010-02-01 14:40:12 -0800563 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
564 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700565 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800566 goto done2;
567 }
568
569 FILE* f = fopen(dest_path, "wb");
570 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700571 printf("%s: can't open %s for write: %s\n",
Doug Zongker6aece332010-02-01 14:40:12 -0800572 name, dest_path, strerror(errno));
573 goto done2;
574 }
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800575 if (map) {
576 MapState state;
577 state.map = map;
578 state.cr = 0;
579 state.so_far = 0;
580 state.f = f;
581 success = mzProcessZipEntryContents(za, entry, MapWriter, &state);
582 } else {
583 success = mzExtractZipEntryToFile(za, entry, fileno(f));
584 }
Doug Zongker6aece332010-02-01 14:40:12 -0800585 fclose(f);
586
587 done2:
588 free(zip_path);
589 free(dest_path);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800590 free(map_path);
591 if (map) {
592 free(map->regions);
593 free(map);
594 }
Doug Zongker512536a2010-02-17 16:11:44 -0800595 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800596 } else {
597 // The one-argument version returns the contents of the file
598 // as the result.
599
600 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800601 Value* v = malloc(sizeof(Value));
602 v->type = VAL_BLOB;
603 v->size = -1;
604 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800605
606 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
607
608 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
609 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
610 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700611 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800612 goto done1;
613 }
614
Doug Zongker512536a2010-02-17 16:11:44 -0800615 v->size = mzGetZipEntryUncompLen(entry);
616 v->data = malloc(v->size);
617 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700618 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800619 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800620 goto done1;
621 }
622
Doug Zongker512536a2010-02-17 16:11:44 -0800623 success = mzExtractZipEntryToBuffer(za, entry,
624 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800625
626 done1:
627 free(zip_path);
628 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800629 free(v->data);
630 v->data = NULL;
631 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800632 }
Doug Zongker512536a2010-02-17 16:11:44 -0800633 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700634 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700635}
636
Doug Zongkera23075f2012-08-06 16:19:09 -0700637// Create all parent directories of name, if necessary.
638static int make_parents(char* name) {
639 char* p;
640 for (p = name + (strlen(name)-1); p > name; --p) {
641 if (*p != '/') continue;
642 *p = '\0';
643 if (make_parents(name) < 0) return -1;
644 int result = mkdir(name, 0700);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700645 if (result == 0) printf("symlink(): created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700646 *p = '/';
647 if (result == 0 || errno == EEXIST) {
648 // successfully created or already existed; we're done
649 return 0;
650 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700651 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700652 return -1;
653 }
654 }
655 return 0;
656}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700657
Doug Zongker9931f7f2009-06-10 14:11:53 -0700658// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700659// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800660Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700661 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700662 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700663 }
664 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700665 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700666 if (target == NULL) return NULL;
667
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700668 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700669 if (srcs == NULL) {
670 free(target);
671 return NULL;
672 }
673
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700674 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700675 int i;
676 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700677 if (unlink(srcs[i]) < 0) {
678 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700679 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700680 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700681 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700682 }
683 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700684 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700685 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700686 name, srcs[i], target);
687 ++bad;
688 }
Doug Zongker60babf82009-09-18 15:11:24 -0700689 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700690 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700691 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700692 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700693 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700694 free(srcs[i]);
695 }
696 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700697 if (bad) {
698 return ErrorAbort(state, "%s: some symlinks failed", name);
699 }
Doug Zongker512536a2010-02-17 16:11:44 -0800700 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700701}
702
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700703struct perm_parsed_args {
704 bool has_uid;
705 uid_t uid;
706 bool has_gid;
707 gid_t gid;
708 bool has_mode;
709 mode_t mode;
710 bool has_fmode;
711 mode_t fmode;
712 bool has_dmode;
713 mode_t dmode;
714 bool has_selabel;
715 char* selabel;
716 bool has_capabilities;
717 uint64_t capabilities;
718};
719
720static struct perm_parsed_args ParsePermArgs(int argc, char** args) {
721 int i;
722 struct perm_parsed_args parsed;
723 int bad = 0;
724 static int max_warnings = 20;
725
726 memset(&parsed, 0, sizeof(parsed));
727
728 for (i = 1; i < argc; i += 2) {
729 if (strcmp("uid", args[i]) == 0) {
730 int64_t uid;
731 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
732 parsed.uid = uid;
733 parsed.has_uid = true;
734 } else {
735 printf("ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
736 bad++;
737 }
738 continue;
739 }
740 if (strcmp("gid", args[i]) == 0) {
741 int64_t gid;
742 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
743 parsed.gid = gid;
744 parsed.has_gid = true;
745 } else {
746 printf("ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
747 bad++;
748 }
749 continue;
750 }
751 if (strcmp("mode", args[i]) == 0) {
752 int32_t mode;
753 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
754 parsed.mode = mode;
755 parsed.has_mode = true;
756 } else {
757 printf("ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
758 bad++;
759 }
760 continue;
761 }
762 if (strcmp("dmode", args[i]) == 0) {
763 int32_t mode;
764 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
765 parsed.dmode = mode;
766 parsed.has_dmode = true;
767 } else {
768 printf("ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
769 bad++;
770 }
771 continue;
772 }
773 if (strcmp("fmode", args[i]) == 0) {
774 int32_t mode;
775 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
776 parsed.fmode = mode;
777 parsed.has_fmode = true;
778 } else {
779 printf("ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
780 bad++;
781 }
782 continue;
783 }
784 if (strcmp("capabilities", args[i]) == 0) {
785 int64_t capabilities;
786 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
787 parsed.capabilities = capabilities;
788 parsed.has_capabilities = true;
789 } else {
790 printf("ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
791 bad++;
792 }
793 continue;
794 }
795 if (strcmp("selabel", args[i]) == 0) {
796 if (args[i+1][0] != '\0') {
797 parsed.selabel = args[i+1];
798 parsed.has_selabel = true;
799 } else {
800 printf("ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
801 bad++;
802 }
803 continue;
804 }
805 if (max_warnings != 0) {
806 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
807 max_warnings--;
808 if (max_warnings == 0) {
809 printf("ParsedPermArgs: suppressing further warnings\n");
810 }
811 }
812 }
813 return parsed;
814}
815
816static int ApplyParsedPerms(
817 const char* filename,
818 const struct stat *statptr,
819 struct perm_parsed_args parsed)
820{
821 int bad = 0;
822
Nick Kraleviche4612512013-09-10 15:34:19 -0700823 /* ignore symlinks */
824 if (S_ISLNK(statptr->st_mode)) {
825 return 0;
826 }
827
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700828 if (parsed.has_uid) {
829 if (chown(filename, parsed.uid, -1) < 0) {
830 printf("ApplyParsedPerms: chown of %s to %d failed: %s\n",
831 filename, parsed.uid, strerror(errno));
832 bad++;
833 }
834 }
835
836 if (parsed.has_gid) {
837 if (chown(filename, -1, parsed.gid) < 0) {
838 printf("ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
839 filename, parsed.gid, strerror(errno));
840 bad++;
841 }
842 }
843
844 if (parsed.has_mode) {
845 if (chmod(filename, parsed.mode) < 0) {
846 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
847 filename, parsed.mode, strerror(errno));
848 bad++;
849 }
850 }
851
852 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
853 if (chmod(filename, parsed.dmode) < 0) {
854 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
855 filename, parsed.dmode, strerror(errno));
856 bad++;
857 }
858 }
859
860 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
861 if (chmod(filename, parsed.fmode) < 0) {
862 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
863 filename, parsed.fmode, strerror(errno));
864 bad++;
865 }
866 }
867
868 if (parsed.has_selabel) {
869 // TODO: Don't silently ignore ENOTSUP
870 if (lsetfilecon(filename, parsed.selabel) && (errno != ENOTSUP)) {
871 printf("ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
872 filename, parsed.selabel, strerror(errno));
873 bad++;
874 }
875 }
876
877 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
878 if (parsed.capabilities == 0) {
879 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
880 // Report failure unless it's ENODATA (attribute not set)
881 printf("ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
882 filename, parsed.capabilities, strerror(errno));
883 bad++;
884 }
885 } else {
886 struct vfs_cap_data cap_data;
887 memset(&cap_data, 0, sizeof(cap_data));
888 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
889 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
890 cap_data.data[0].inheritable = 0;
891 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
892 cap_data.data[1].inheritable = 0;
893 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
894 printf("ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
895 filename, parsed.capabilities, strerror(errno));
896 bad++;
897 }
898 }
899 }
900
901 return bad;
902}
903
904// nftw doesn't allow us to pass along context, so we need to use
905// global variables. *sigh*
906static struct perm_parsed_args recursive_parsed_args;
907
908static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
909 int fileflags, struct FTW *pfwt) {
910 return ApplyParsedPerms(filename, statptr, recursive_parsed_args);
911}
912
913static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
914 int i;
915 int bad = 0;
916 static int nwarnings = 0;
917 struct stat sb;
918 Value* result = NULL;
919
920 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
921
922 if ((argc % 2) != 1) {
923 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d",
924 name, argc);
925 }
926
927 char** args = ReadVarArgs(state, argc, argv);
928 if (args == NULL) return NULL;
929
930 if (lstat(args[0], &sb) == -1) {
931 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
932 goto done;
933 }
934
935 struct perm_parsed_args parsed = ParsePermArgs(argc, args);
936
937 if (recursive) {
938 recursive_parsed_args = parsed;
939 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
940 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
941 } else {
942 bad += ApplyParsedPerms(args[0], &sb, parsed);
943 }
944
945done:
946 for (i = 0; i < argc; ++i) {
947 free(args[i]);
948 }
949 free(args);
950
951 if (result != NULL) {
952 return result;
953 }
954
955 if (bad > 0) {
956 return ErrorAbort(state, "%s: some changes failed", name);
957 }
958
959 return StringValue(strdup(""));
960}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700961
Doug Zongker512536a2010-02-17 16:11:44 -0800962Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700963 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700964 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700965 }
966 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700967 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700968 if (key == NULL) return NULL;
969
970 char value[PROPERTY_VALUE_MAX];
971 property_get(key, value, "");
972 free(key);
973
Doug Zongker512536a2010-02-17 16:11:44 -0800974 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700975}
976
977
Doug Zongker47cace92009-06-18 10:11:50 -0700978// file_getprop(file, key)
979//
980// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700981// per line. # comment lines,blank lines, lines without '=' ignored),
982// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800983Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700984 char* result = NULL;
985 char* buffer = NULL;
986 char* filename;
987 char* key;
988 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
989 return NULL;
990 }
991
992 struct stat st;
993 if (stat(filename, &st) < 0) {
994 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
995 name, filename, strerror(errno));
996 goto done;
997 }
998
999#define MAX_FILE_GETPROP_SIZE 65536
1000
1001 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
1002 ErrorAbort(state, "%s too large for %s (max %d)",
1003 filename, name, MAX_FILE_GETPROP_SIZE);
1004 goto done;
1005 }
1006
1007 buffer = malloc(st.st_size+1);
1008 if (buffer == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -07001009 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -07001010 goto done;
1011 }
1012
1013 FILE* f = fopen(filename, "rb");
1014 if (f == NULL) {
1015 ErrorAbort(state, "%s: failed to open %s: %s",
1016 name, filename, strerror(errno));
1017 goto done;
1018 }
1019
1020 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -07001021 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -07001022 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -07001023 fclose(f);
1024 goto done;
1025 }
1026 buffer[st.st_size] = '\0';
1027
1028 fclose(f);
1029
1030 char* line = strtok(buffer, "\n");
1031 do {
1032 // skip whitespace at start of line
1033 while (*line && isspace(*line)) ++line;
1034
1035 // comment or blank line: skip to next line
1036 if (*line == '\0' || *line == '#') continue;
1037
1038 char* equal = strchr(line, '=');
1039 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001040 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001041 }
1042
1043 // trim whitespace between key and '='
1044 char* key_end = equal-1;
1045 while (key_end > line && isspace(*key_end)) --key_end;
1046 key_end[1] = '\0';
1047
1048 // not the key we're looking for
1049 if (strcmp(key, line) != 0) continue;
1050
1051 // skip whitespace after the '=' to the start of the value
1052 char* val_start = equal+1;
1053 while(*val_start && isspace(*val_start)) ++val_start;
1054
1055 // trim trailing whitespace
1056 char* val_end = val_start + strlen(val_start)-1;
1057 while (val_end > val_start && isspace(*val_end)) --val_end;
1058 val_end[1] = '\0';
1059
1060 result = strdup(val_start);
1061 break;
1062
1063 } while ((line = strtok(NULL, "\n")));
1064
1065 if (result == NULL) result = strdup("");
1066
1067 done:
1068 free(filename);
1069 free(key);
1070 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001071 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001072}
1073
1074
Doug Zongker8edb00c2009-06-11 17:21:44 -07001075static bool write_raw_image_cb(const unsigned char* data,
1076 int data_len, void* ctx) {
1077 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
1078 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001079 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001080 return false;
1081}
1082
Doug Zongker179b2d92011-04-12 15:49:04 -07001083// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001084Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001085 char* result = NULL;
1086
Doug Zongker179b2d92011-04-12 15:49:04 -07001087 Value* partition_value;
1088 Value* contents;
1089 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001090 return NULL;
1091 }
1092
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001093 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001094 if (partition_value->type != VAL_STRING) {
1095 ErrorAbort(state, "partition argument to %s must be string", name);
1096 goto done;
1097 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001098 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001099 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001100 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001101 goto done;
1102 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001103 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001104 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001105 goto done;
1106 }
1107
1108 mtd_scan_partitions();
1109 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
1110 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001111 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001112 result = strdup("");
1113 goto done;
1114 }
1115
1116 MtdWriteContext* ctx = mtd_write_partition(mtd);
1117 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001118 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001119 name, partition);
1120 result = strdup("");
1121 goto done;
1122 }
1123
1124 bool success;
1125
Doug Zongker179b2d92011-04-12 15:49:04 -07001126 if (contents->type == VAL_STRING) {
1127 // we're given a filename as the contents
1128 char* filename = contents->data;
1129 FILE* f = fopen(filename, "rb");
1130 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001131 printf("%s: can't open %s: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001132 name, filename, strerror(errno));
1133 result = strdup("");
1134 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001135 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001136
1137 success = true;
1138 char* buffer = malloc(BUFSIZ);
1139 int read;
1140 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1141 int wrote = mtd_write_data(ctx, buffer, read);
1142 success = success && (wrote == read);
1143 }
1144 free(buffer);
1145 fclose(f);
1146 } else {
1147 // we're given a blob as the contents
1148 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1149 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001150 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001151 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001152 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001153 partition, strerror(errno));
1154 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001155
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001156 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001157 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001158 }
1159 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001160 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001161 }
1162
Doug Zongker179b2d92011-04-12 15:49:04 -07001163 printf("%s %s partition\n",
1164 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001165
1166 result = success ? partition : strdup("");
1167
1168done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001169 if (result != partition) FreeValue(partition_value);
1170 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001171 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001172}
1173
Doug Zongker8edb00c2009-06-11 17:21:44 -07001174// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001175Value* ApplyPatchSpaceFn(const char* name, State* state,
1176 int argc, Expr* argv[]) {
1177 char* bytes_str;
1178 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1179 return NULL;
1180 }
1181
1182 char* endptr;
1183 size_t bytes = strtol(bytes_str, &endptr, 10);
1184 if (bytes == 0 && endptr == bytes_str) {
1185 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
1186 name, bytes_str);
1187 free(bytes_str);
1188 return NULL;
1189 }
1190
1191 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1192}
1193
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001194bool CheckMappedFileSha1(FILE* f, DontCareMap* map, uint8_t* intended_digest) {
1195 MapState state;
1196
1197 state.f = f;
1198 state.so_far = 0;
1199 state.cr = 0;
1200 state.map = map;
1201
1202 SHA_CTX ctx;
1203 SHA_init(&ctx);
1204
1205 unsigned char buffer[32173];
1206 size_t bytes_read;
1207
1208 while ((bytes_read = read_with_map(buffer, sizeof(buffer), &state)) > 0) {
1209 SHA_update(&ctx, buffer, bytes_read);
1210 }
1211 const uint8_t* digest = SHA_final(&ctx);
1212
1213 return memcmp(digest, intended_digest, SHA_DIGEST_SIZE) == 0;
1214}
1215
1216
1217// syspatch(file, tgt_mapfile, tgt_sha1, init_mapfile, init_sha1, patch)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001218
Doug Zongker52b40362014-02-10 15:30:30 -08001219Value* SysPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001220 if (argc != 6) {
1221 return ErrorAbort(state, "%s(): expected 6 args, got %d", name, argc);
Doug Zongker52b40362014-02-10 15:30:30 -08001222 }
1223
1224 char* filename;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001225 char* target_mapfilename;
Doug Zongker52b40362014-02-10 15:30:30 -08001226 char* target_sha1;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001227 char* init_mapfilename;
Doug Zongker52b40362014-02-10 15:30:30 -08001228 char* init_sha1;
1229 char* patch_filename;
1230 uint8_t target_digest[SHA_DIGEST_SIZE];
1231 uint8_t init_digest[SHA_DIGEST_SIZE];
1232
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001233 if (ReadArgs(state, argv, 6, &filename,
1234 &target_mapfilename, &target_sha1,
1235 &init_mapfilename, &init_sha1, &patch_filename) < 0) {
Doug Zongker52b40362014-02-10 15:30:30 -08001236 return NULL;
1237 }
1238
Doug Zongker43772d22014-06-09 14:13:19 -07001239 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1240 mapwrite_cmd_pipe = ui->cmd_pipe;
1241
Doug Zongker52b40362014-02-10 15:30:30 -08001242 if (ParseSha1(target_sha1, target_digest) != 0) {
1243 printf("%s(): failed to parse '%s' as target SHA-1", name, target_sha1);
1244 memset(target_digest, 0, SHA_DIGEST_SIZE);
1245 }
1246 if (ParseSha1(init_sha1, init_digest) != 0) {
1247 printf("%s(): failed to parse '%s' as init SHA-1", name, init_sha1);
1248 memset(init_digest, 0, SHA_DIGEST_SIZE);
1249 }
1250
Doug Zongker52b40362014-02-10 15:30:30 -08001251 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001252 FILE* src = fopen(filename, "r");
Doug Zongker52b40362014-02-10 15:30:30 -08001253
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001254 DontCareMap* init_map = ReadDontCareMapFromZip(za, init_mapfilename);
1255 if (init_map == NULL) return ErrorAbort(state, "%s(): failed to read init map\n", name);
1256 DontCareMap* target_map = ReadDontCareMapFromZip(za, target_mapfilename);
1257 if (target_map == NULL) return ErrorAbort(state, "%s(): failed to read target map\n", name);
1258
1259 if (CheckMappedFileSha1(src, init_map, init_digest)) {
1260 // If the partition contents match the init_digest, then we need to apply the patch.
1261
1262 rewind(src);
1263
1264 const ZipEntry* entry = mzFindZipEntry(za, patch_filename);
1265 if (entry == NULL) {
1266 return ErrorAbort(state, "%s(): no %s in package\n", name, patch_filename);
1267 }
1268
1269 unsigned char* patch_data;
1270 size_t patch_len;
1271 if (!mzGetStoredEntry(za, entry, &patch_data, &patch_len)) {
1272 return ErrorAbort(state, "%s(): failed to get %s entry\n", name, patch_filename);
1273 }
1274
1275 FILE* tgt = fopen(filename, "r+");
1276
Doug Zongker43772d22014-06-09 14:13:19 -07001277 int ret = syspatch(src, init_map, patch_data, patch_len, tgt, target_map, progress_cb);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001278
1279 fclose(src);
1280 fclose(tgt);
1281
1282 if (ret != 0) {
1283 return ErrorAbort(state, "%s(): patching failed\n", name);
1284 }
1285 } else {
1286 rewind(src);
1287 if (CheckMappedFileSha1(src, target_map, target_digest)) {
1288 // If the partition contents match the target already, we
1289 // don't need to do anything.
1290 printf("%s: output is already target\n", name);
1291 } else {
1292 return ErrorAbort(state, "%s(): %s in unknown state\n", name, filename);
1293 }
Doug Zongker52b40362014-02-10 15:30:30 -08001294 }
1295
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001296 done:
Doug Zongker52b40362014-02-10 15:30:30 -08001297 free(target_sha1);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001298 free(target_mapfilename);
Doug Zongker52b40362014-02-10 15:30:30 -08001299 free(init_sha1);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001300 free(init_mapfilename);
Doug Zongker52b40362014-02-10 15:30:30 -08001301 free(patch_filename);
1302 return StringValue(filename);
1303
1304}
1305
1306// apply_patch(file, size, init_sha1, tgt_sha1, patch)
1307
Doug Zongker512536a2010-02-17 16:11:44 -08001308Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001309 if (argc < 6 || (argc % 2) == 1) {
1310 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1311 "even number, got %d",
1312 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001313 }
1314
Doug Zongkerc4351c72010-02-22 14:46:32 -08001315 char* source_filename;
1316 char* target_filename;
1317 char* target_sha1;
1318 char* target_size_str;
1319 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1320 &target_sha1, &target_size_str) < 0) {
1321 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001322 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001323
Doug Zongkerc4351c72010-02-22 14:46:32 -08001324 char* endptr;
1325 size_t target_size = strtol(target_size_str, &endptr, 10);
1326 if (target_size == 0 && endptr == target_size_str) {
1327 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1328 name, target_size_str);
1329 free(source_filename);
1330 free(target_filename);
1331 free(target_sha1);
1332 free(target_size_str);
1333 return NULL;
1334 }
1335
1336 int patchcount = (argc-4) / 2;
1337 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001338
1339 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001340 for (i = 0; i < patchcount; ++i) {
1341 if (patches[i*2]->type != VAL_STRING) {
1342 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1343 break;
1344 }
1345 if (patches[i*2+1]->type != VAL_BLOB) {
1346 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1347 break;
1348 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001349 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001350 if (i != patchcount) {
1351 for (i = 0; i < patchcount*2; ++i) {
1352 FreeValue(patches[i]);
1353 }
1354 free(patches);
1355 return NULL;
1356 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001357
Doug Zongkerc4351c72010-02-22 14:46:32 -08001358 char** patch_sha_str = malloc(patchcount * sizeof(char*));
1359 for (i = 0; i < patchcount; ++i) {
1360 patch_sha_str[i] = patches[i*2]->data;
1361 patches[i*2]->data = NULL;
1362 FreeValue(patches[i*2]);
1363 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001364 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001365
1366 int result = applypatch(source_filename, target_filename,
1367 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001368 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001369
1370 for (i = 0; i < patchcount; ++i) {
1371 FreeValue(patches[i]);
1372 }
1373 free(patch_sha_str);
1374 free(patches);
1375
1376 return StringValue(strdup(result == 0 ? "t" : ""));
1377}
1378
1379// apply_patch_check(file, [sha1_1, ...])
1380Value* ApplyPatchCheckFn(const char* name, State* state,
1381 int argc, Expr* argv[]) {
1382 if (argc < 1) {
1383 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1384 name, argc);
1385 }
1386
1387 char* filename;
1388 if (ReadArgs(state, argv, 1, &filename) < 0) {
1389 return NULL;
1390 }
1391
1392 int patchcount = argc-1;
1393 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1394
1395 int result = applypatch_check(filename, patchcount, sha1s);
1396
1397 int i;
1398 for (i = 0; i < patchcount; ++i) {
1399 free(sha1s[i]);
1400 }
1401 free(sha1s);
1402
1403 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001404}
1405
Doug Zongker512536a2010-02-17 16:11:44 -08001406Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001407 char** args = ReadVarArgs(state, argc, argv);
1408 if (args == NULL) {
1409 return NULL;
1410 }
1411
1412 int size = 0;
1413 int i;
1414 for (i = 0; i < argc; ++i) {
1415 size += strlen(args[i]);
1416 }
1417 char* buffer = malloc(size+1);
1418 size = 0;
1419 for (i = 0; i < argc; ++i) {
1420 strcpy(buffer+size, args[i]);
1421 size += strlen(args[i]);
1422 free(args[i]);
1423 }
1424 free(args);
1425 buffer[size] = '\0';
1426
1427 char* line = strtok(buffer, "\n");
1428 while (line) {
1429 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
1430 "ui_print %s\n", line);
1431 line = strtok(NULL, "\n");
1432 }
1433 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
1434
Doug Zongker512536a2010-02-17 16:11:44 -08001435 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001436}
1437
Doug Zongkerd0181b82011-10-19 10:51:12 -07001438Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1439 if (argc != 0) {
1440 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1441 }
1442 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1443 return StringValue(strdup("t"));
1444}
1445
Doug Zongker512536a2010-02-17 16:11:44 -08001446Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001447 if (argc < 1) {
1448 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1449 }
1450 char** args = ReadVarArgs(state, argc, argv);
1451 if (args == NULL) {
1452 return NULL;
1453 }
1454
1455 char** args2 = malloc(sizeof(char*) * (argc+1));
1456 memcpy(args2, args, sizeof(char*) * argc);
1457 args2[argc] = NULL;
1458
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001459 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001460
1461 pid_t child = fork();
1462 if (child == 0) {
1463 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001464 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001465 _exit(1);
1466 }
1467 int status;
1468 waitpid(child, &status, 0);
1469 if (WIFEXITED(status)) {
1470 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001471 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001472 WEXITSTATUS(status));
1473 }
1474 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001475 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001476 WTERMSIG(status));
1477 }
1478
1479 int i;
1480 for (i = 0; i < argc; ++i) {
1481 free(args[i]);
1482 }
1483 free(args);
1484 free(args2);
1485
1486 char buffer[20];
1487 sprintf(buffer, "%d", status);
1488
Doug Zongker512536a2010-02-17 16:11:44 -08001489 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001490}
1491
Doug Zongker512536a2010-02-17 16:11:44 -08001492// sha1_check(data)
1493// to return the sha1 of the data (given in the format returned by
1494// read_file).
1495//
1496// sha1_check(data, sha1_hex, [sha1_hex, ...])
1497// returns the sha1 of the file if it matches any of the hex
1498// strings passed, or "" if it does not equal any of them.
1499//
1500Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1501 if (argc < 1) {
1502 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1503 }
1504
1505 Value** args = ReadValueVarArgs(state, argc, argv);
1506 if (args == NULL) {
1507 return NULL;
1508 }
1509
1510 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001511 return StringValue(strdup(""));
1512 }
1513 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001514 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001515 FreeValue(args[0]);
1516
1517 if (argc == 1) {
1518 return StringValue(PrintSha1(digest));
1519 }
1520
1521 int i;
1522 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1523 for (i = 1; i < argc; ++i) {
1524 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001525 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001526 name, i);
1527 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1528 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001529 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1530 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001531 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1532 break;
1533 }
1534 FreeValue(args[i]);
1535 }
1536 if (i >= argc) {
1537 // Didn't match any of the hex strings; return false.
1538 return StringValue(strdup(""));
1539 }
1540 // Found a match; free all the remaining arguments and return the
1541 // matched one.
1542 int j;
1543 for (j = i+1; j < argc; ++j) {
1544 FreeValue(args[j]);
1545 }
1546 return args[i];
1547}
1548
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001549// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001550// is actually a FileContents*).
1551Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1552 if (argc != 1) {
1553 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1554 }
1555 char* filename;
1556 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1557
1558 Value* v = malloc(sizeof(Value));
1559 v->type = VAL_BLOB;
1560
1561 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001562 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001563 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001564 v->size = -1;
1565 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001566 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001567 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001568 }
1569
1570 v->size = fc.size;
1571 v->data = (char*)fc.data;
1572
1573 free(filename);
1574 return v;
1575}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001576
Doug Zongkerc87bab12013-11-25 13:53:25 -08001577// Immediately reboot the device. Recovery is not finished normally,
1578// so if you reboot into recovery it will re-start applying the
1579// current package (because nothing has cleared the copy of the
1580// arguments stored in the BCB).
1581//
1582// The argument is the partition name passed to the android reboot
1583// property. It can be "recovery" to boot from the recovery
1584// partition, or "" (empty string) to boot from the regular boot
1585// partition.
1586Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1587 if (argc != 2) {
1588 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1589 }
1590
1591 char* filename;
1592 char* property;
1593 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1594
1595 char buffer[80];
1596
1597 // zero out the 'command' field of the bootloader message.
1598 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1599 FILE* f = fopen(filename, "r+b");
1600 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1601 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1602 fclose(f);
1603 free(filename);
1604
1605 strcpy(buffer, "reboot,");
1606 if (property != NULL) {
1607 strncat(buffer, property, sizeof(buffer)-10);
1608 }
1609
1610 property_set(ANDROID_RB_PROPERTY, buffer);
1611
1612 sleep(5);
1613 free(property);
1614 ErrorAbort(state, "%s() failed to reboot", name);
1615 return NULL;
1616}
1617
1618// Store a string value somewhere that future invocations of recovery
1619// can access it. This value is called the "stage" and can be used to
1620// drive packages that need to do reboots in the middle of
1621// installation and keep track of where they are in the multi-stage
1622// install.
1623//
1624// The first argument is the block device for the misc partition
1625// ("/misc" in the fstab), which is where this value is stored. The
1626// second argument is the string to store; it should not exceed 31
1627// bytes.
1628Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1629 if (argc != 2) {
1630 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1631 }
1632
1633 char* filename;
1634 char* stagestr;
1635 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1636
1637 // Store this value in the misc partition, immediately after the
1638 // bootloader message that the main recovery uses to save its
1639 // arguments in case of the device restarting midway through
1640 // package installation.
1641 FILE* f = fopen(filename, "r+b");
1642 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1643 int to_write = strlen(stagestr)+1;
1644 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1645 if (to_write > max_size) {
1646 to_write = max_size;
1647 stagestr[max_size-1] = 0;
1648 }
1649 fwrite(stagestr, to_write, 1, f);
1650 fclose(f);
1651
1652 free(stagestr);
1653 return StringValue(filename);
1654}
1655
1656// Return the value most recently saved with SetStageFn. The argument
1657// is the block device for the misc partition.
1658Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001659 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001660 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1661 }
1662
1663 char* filename;
1664 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1665
1666 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1667 FILE* f = fopen(filename, "rb");
1668 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1669 fread(buffer, sizeof(buffer), 1, f);
1670 fclose(f);
1671 buffer[sizeof(buffer)-1] = '\0';
1672
1673 return StringValue(strdup(buffer));
1674}
1675
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001676Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1677 if (argc != 2) {
1678 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1679 }
1680
1681 char* filename;
1682 char* len_str;
1683 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1684
1685 size_t len = strtoull(len_str, NULL, 0);
1686 int fd = open(filename, O_WRONLY, 0644);
1687 int success = wipe_block_device(fd, len);
1688
1689 free(filename);
1690 free(len_str);
1691
1692 close(fd);
1693
1694 return StringValue(strdup(success ? "t" : ""));
1695}
1696
Doug Zongkerc704e062014-05-23 08:40:35 -07001697Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1698 if (argc != 0) {
1699 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1700 }
1701 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1702 fprintf(ui->cmd_pipe, "enable_reboot\n");
1703 return StringValue(strdup("t"));
1704}
1705
Doug Zongker9931f7f2009-06-10 14:11:53 -07001706void RegisterInstallFunctions() {
1707 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001708 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001709 RegisterFunction("unmount", UnmountFn);
1710 RegisterFunction("format", FormatFn);
1711 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001712 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001713 RegisterFunction("delete", DeleteFn);
1714 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001715 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1716 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001717 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001718
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001719 // Usage:
1720 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1721 // Example:
1722 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1723 RegisterFunction("set_metadata", SetMetadataFn);
1724
1725 // Usage:
1726 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1727 // Example:
1728 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1729 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1730
Doug Zongker8edb00c2009-06-11 17:21:44 -07001731 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001732 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001733 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001734
1735 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001736 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1737 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001738
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001739 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001740 RegisterFunction("syspatch", SysPatchFn);
1741
Doug Zongker512536a2010-02-17 16:11:44 -08001742 RegisterFunction("read_file", ReadFileFn);
1743 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001744 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001745
Doug Zongkerd0181b82011-10-19 10:51:12 -07001746 RegisterFunction("wipe_cache", WipeCacheFn);
1747
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001748 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001749
1750 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001751
1752 RegisterFunction("reboot_now", RebootNowFn);
1753 RegisterFunction("get_stage", GetStageFn);
1754 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001755
1756 RegisterFunction("enable_reboot", EnableRebootFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001757}