blob: 770dbd09eeebeee5e48f0b409f14e61733f0f61b [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 Zongker8edb00c2009-06-11 17:21:44 -070037#include "cutils/misc.h"
38#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070039#include "edify/expr.h"
Doug Zongker512536a2010-02-17 16:11:44 -080040#include "mincrypt/sha.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070041#include "minzip/DirUtil.h"
42#include "mtdutils/mounts.h"
43#include "mtdutils/mtdutils.h"
44#include "updater.h"
Doug Zongker512536a2010-02-17 16:11:44 -080045#include "applypatch/applypatch.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070046
Doug Zongker3d177d02010-07-01 09:18:44 -070047#ifdef USE_EXT4
48#include "make_ext4fs.h"
49#endif
50
51// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -070052//
Doug Zongker3d177d02010-07-01 09:18:44 -070053// fs_type="yaffs2" partition_type="MTD" location=partition
54// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -080055Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070056 char* result = NULL;
Doug Zongker3d177d02010-07-01 09:18:44 -070057 if (argc != 4) {
58 return ErrorAbort(state, "%s() expects 4 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -070059 }
Doug Zongker3d177d02010-07-01 09:18:44 -070060 char* fs_type;
61 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -070062 char* location;
63 char* mount_point;
Doug Zongker3d177d02010-07-01 09:18:44 -070064 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
65 &location, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070066 return NULL;
67 }
68
Doug Zongker3d177d02010-07-01 09:18:44 -070069 if (strlen(fs_type) == 0) {
70 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
71 goto done;
72 }
73 if (strlen(partition_type) == 0) {
74 ErrorAbort(state, "partition_type argument to %s() can't be empty",
75 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070076 goto done;
77 }
78 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070079 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070080 goto done;
81 }
82 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070083 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070084 goto done;
85 }
86
Stephen Smalley779701d2012-02-09 14:13:23 -050087 char *secontext = NULL;
88
89 if (sehandle) {
90 selabel_lookup(sehandle, &secontext, mount_point, 0755);
91 setfscreatecon(secontext);
92 }
Stephen Smalley779701d2012-02-09 14:13:23 -050093
Doug Zongker9931f7f2009-06-10 14:11:53 -070094 mkdir(mount_point, 0755);
95
Stephen Smalley779701d2012-02-09 14:13:23 -050096 if (secontext) {
97 freecon(secontext);
98 setfscreatecon(NULL);
99 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500100
Doug Zongker3d177d02010-07-01 09:18:44 -0700101 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700102 mtd_scan_partitions();
103 const MtdPartition* mtd;
104 mtd = mtd_find_partition_by_name(location);
105 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700106 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700107 name, location);
108 result = strdup("");
109 goto done;
110 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700111 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700112 printf("mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700113 location, strerror(errno));
114 result = strdup("");
115 goto done;
116 }
117 result = mount_point;
118 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700119 if (mount(location, mount_point, fs_type,
Doug Zongker9931f7f2009-06-10 14:11:53 -0700120 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700121 printf("%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700122 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700123 result = strdup("");
124 } else {
125 result = mount_point;
126 }
127 }
128
129done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700130 free(fs_type);
131 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700132 free(location);
133 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800134 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700135}
136
Doug Zongker8edb00c2009-06-11 17:21:44 -0700137
138// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800139Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700140 char* result = NULL;
141 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700142 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700143 }
144 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700145 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700146 return NULL;
147 }
148 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700149 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700150 goto done;
151 }
152
153 scan_mounted_volumes();
154 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
155 if (vol == NULL) {
156 result = strdup("");
157 } else {
158 result = mount_point;
159 }
160
161done:
162 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800163 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700164}
165
166
Doug Zongker512536a2010-02-17 16:11:44 -0800167Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700168 char* result = NULL;
169 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700170 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700171 }
172 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700173 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700174 return NULL;
175 }
176 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700177 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700178 goto done;
179 }
180
181 scan_mounted_volumes();
182 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
183 if (vol == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700184 printf("unmount of %s failed; no such volume\n", mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700185 result = strdup("");
186 } else {
187 unmount_mounted_volume(vol);
188 result = mount_point;
189 }
190
191done:
192 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800193 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700194}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700195
196
Stephen Smalley779701d2012-02-09 14:13:23 -0500197// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700198//
Stephen Smalley779701d2012-02-09 14:13:23 -0500199// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
200// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800201// if fs_size == 0, then make_ext4fs uses the entire partition.
202// if fs_size > 0, that is the size to use
203// if fs_size < 0, then reserve that many bytes at the end of the partition
Doug Zongker512536a2010-02-17 16:11:44 -0800204Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700205 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400206 if (argc != 5) {
207 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700208 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700209 char* fs_type;
210 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700211 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800212 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400213 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500214
Stephen Smalley779701d2012-02-09 14:13:23 -0500215 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
216 return NULL;
217 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700218
Doug Zongker3d177d02010-07-01 09:18:44 -0700219 if (strlen(fs_type) == 0) {
220 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
221 goto done;
222 }
223 if (strlen(partition_type) == 0) {
224 ErrorAbort(state, "partition_type argument to %s() can't be empty",
225 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700226 goto done;
227 }
228 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700229 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700230 goto done;
231 }
232
Stephen Smalley516e4e22012-04-03 13:35:11 -0400233 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500234 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
235 goto done;
236 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500237
Doug Zongker3d177d02010-07-01 09:18:44 -0700238 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700239 mtd_scan_partitions();
240 const MtdPartition* mtd = mtd_find_partition_by_name(location);
241 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700242 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700243 name, location);
244 result = strdup("");
245 goto done;
246 }
247 MtdWriteContext* ctx = mtd_write_partition(mtd);
248 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700249 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700250 result = strdup("");
251 goto done;
252 }
253 if (mtd_erase_blocks(ctx, -1) == -1) {
254 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700255 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700256 result = strdup("");
257 goto done;
258 }
259 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700260 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700261 result = strdup("");
262 goto done;
263 }
264 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700265#ifdef USE_EXT4
266 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500267 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700268 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700269 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700270 name, status, location);
271 result = strdup("");
272 goto done;
273 }
274 result = location;
275#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700276 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700277 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700278 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700279 }
280
281done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700282 free(fs_type);
283 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700284 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800285 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700286}
287
Doug Zongker8edb00c2009-06-11 17:21:44 -0700288
Doug Zongker512536a2010-02-17 16:11:44 -0800289Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700290 char** paths = malloc(argc * sizeof(char*));
291 int i;
292 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700293 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700294 if (paths[i] == NULL) {
295 int j;
296 for (j = 0; j < i; ++i) {
297 free(paths[j]);
298 }
299 free(paths);
300 return NULL;
301 }
302 }
303
304 bool recursive = (strcmp(name, "delete_recursive") == 0);
305
306 int success = 0;
307 for (i = 0; i < argc; ++i) {
308 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
309 ++success;
310 free(paths[i]);
311 }
312 free(paths);
313
314 char buffer[10];
315 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800316 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700317}
318
Doug Zongker8edb00c2009-06-11 17:21:44 -0700319
Doug Zongker512536a2010-02-17 16:11:44 -0800320Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700321 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700322 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700323 }
324 char* frac_str;
325 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700326 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700327 return NULL;
328 }
329
330 double frac = strtod(frac_str, NULL);
331 int sec = strtol(sec_str, NULL, 10);
332
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700333 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700334 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
335
Doug Zongker9931f7f2009-06-10 14:11:53 -0700336 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800337 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700338}
339
Doug Zongker512536a2010-02-17 16:11:44 -0800340Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700341 if (argc != 1) {
342 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
343 }
344 char* frac_str;
345 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
346 return NULL;
347 }
348
349 double frac = strtod(frac_str, NULL);
350
351 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
352 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
353
Doug Zongker512536a2010-02-17 16:11:44 -0800354 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700355}
356
Doug Zongker8edb00c2009-06-11 17:21:44 -0700357// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800358Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700359 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700360 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700361 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700362 }
363 char* zip_path;
364 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700365 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700366
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700367 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700368
369 // To create a consistent system image, never use the clock for timestamps.
370 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
371
372 bool success = mzExtractRecursive(za, zip_path, dest_path,
373 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500374 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700375 free(zip_path);
376 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800377 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700378}
379
Doug Zongker8edb00c2009-06-11 17:21:44 -0700380
381// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800382// or
383// package_extract_file(package_path)
384// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800385// function (the char* returned is actually a FileContents*).
386Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700387 int argc, Expr* argv[]) {
Doug Zongker6aece332010-02-01 14:40:12 -0800388 if (argc != 1 && argc != 2) {
389 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
390 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700391 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700392 bool success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800393 if (argc == 2) {
394 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700395
Doug Zongker6aece332010-02-01 14:40:12 -0800396 char* zip_path;
397 char* dest_path;
398 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
399
400 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
401 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
402 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700403 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800404 goto done2;
405 }
406
407 FILE* f = fopen(dest_path, "wb");
408 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700409 printf("%s: can't open %s for write: %s\n",
Doug Zongker6aece332010-02-01 14:40:12 -0800410 name, dest_path, strerror(errno));
411 goto done2;
412 }
413 success = mzExtractZipEntryToFile(za, entry, fileno(f));
414 fclose(f);
415
416 done2:
417 free(zip_path);
418 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800419 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800420 } else {
421 // The one-argument version returns the contents of the file
422 // as the result.
423
424 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800425 Value* v = malloc(sizeof(Value));
426 v->type = VAL_BLOB;
427 v->size = -1;
428 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800429
430 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
431
432 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
433 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
434 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700435 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800436 goto done1;
437 }
438
Doug Zongker512536a2010-02-17 16:11:44 -0800439 v->size = mzGetZipEntryUncompLen(entry);
440 v->data = malloc(v->size);
441 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700442 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800443 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800444 goto done1;
445 }
446
Doug Zongker512536a2010-02-17 16:11:44 -0800447 success = mzExtractZipEntryToBuffer(za, entry,
448 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800449
450 done1:
451 free(zip_path);
452 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800453 free(v->data);
454 v->data = NULL;
455 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800456 }
Doug Zongker512536a2010-02-17 16:11:44 -0800457 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700458 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700459}
460
Doug Zongkera23075f2012-08-06 16:19:09 -0700461// Create all parent directories of name, if necessary.
462static int make_parents(char* name) {
463 char* p;
464 for (p = name + (strlen(name)-1); p > name; --p) {
465 if (*p != '/') continue;
466 *p = '\0';
467 if (make_parents(name) < 0) return -1;
468 int result = mkdir(name, 0700);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700469 if (result == 0) printf("symlink(): created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700470 *p = '/';
471 if (result == 0 || errno == EEXIST) {
472 // successfully created or already existed; we're done
473 return 0;
474 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700475 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700476 return -1;
477 }
478 }
479 return 0;
480}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700481
Doug Zongker9931f7f2009-06-10 14:11:53 -0700482// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700483// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800484Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700485 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700486 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700487 }
488 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700489 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700490 if (target == NULL) return NULL;
491
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700492 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700493 if (srcs == NULL) {
494 free(target);
495 return NULL;
496 }
497
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700498 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700499 int i;
500 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700501 if (unlink(srcs[i]) < 0) {
502 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700503 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700504 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700505 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700506 }
507 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700508 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700509 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700510 name, srcs[i], target);
511 ++bad;
512 }
Doug Zongker60babf82009-09-18 15:11:24 -0700513 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700514 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700515 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700516 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700517 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700518 free(srcs[i]);
519 }
520 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700521 if (bad) {
522 return ErrorAbort(state, "%s: some symlinks failed", name);
523 }
Doug Zongker512536a2010-02-17 16:11:44 -0800524 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700525}
526
Doug Zongker8edb00c2009-06-11 17:21:44 -0700527
Doug Zongker512536a2010-02-17 16:11:44 -0800528Value* SetPermFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700529 char* result = NULL;
Nick Kralevich3328e3b2013-09-09 10:47:14 -0700530 bool recursive = (strcmp(name, "set_perm_recursive") == 0);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700531
Nick Kralevich3328e3b2013-09-09 10:47:14 -0700532 int min_args = 4 + (recursive ? 1 : 0);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700533 if (argc < min_args) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700534 return ErrorAbort(state, "%s() expects %d+ args, got %d",
535 name, min_args, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700536 }
537
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700538 char** args = ReadVarArgs(state, argc, argv);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700539 if (args == NULL) return NULL;
540
541 char* end;
542 int i;
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700543 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700544
545 int uid = strtoul(args[0], &end, 0);
546 if (*end != '\0' || args[0][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700547 ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700548 goto done;
549 }
550
551 int gid = strtoul(args[1], &end, 0);
552 if (*end != '\0' || args[1][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700553 ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700554 goto done;
555 }
556
557 if (recursive) {
558 int dir_mode = strtoul(args[2], &end, 0);
559 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700560 ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700561 goto done;
562 }
563
564 int file_mode = strtoul(args[3], &end, 0);
565 if (*end != '\0' || args[3][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700566 ErrorAbort(state, "%s: \"%s\" not a valid filemode",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700567 name, args[3]);
568 goto done;
569 }
570
Nick Kralevich3328e3b2013-09-09 10:47:14 -0700571 for (i = 4; i < argc; ++i) {
572 dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700573 }
574 } else {
575 int mode = strtoul(args[2], &end, 0);
576 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700577 ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700578 goto done;
579 }
580
Nick Kralevich3328e3b2013-09-09 10:47:14 -0700581 for (i = 3; i < argc; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700582 if (chown(args[i], uid, gid) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700583 printf("%s: chown of %s to %d %d failed: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700584 name, args[i], uid, gid, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700585 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700586 }
587 if (chmod(args[i], mode) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700588 printf("%s: chmod of %s to %o failed: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700589 name, args[i], mode, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700590 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700591 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700592 }
593 }
594 result = strdup("");
595
596done:
597 for (i = 0; i < argc; ++i) {
598 free(args[i]);
599 }
600 free(args);
601
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700602 if (bad) {
603 free(result);
604 return ErrorAbort(state, "%s: some changes failed", name);
605 }
Doug Zongker512536a2010-02-17 16:11:44 -0800606 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700607}
608
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700609struct perm_parsed_args {
610 bool has_uid;
611 uid_t uid;
612 bool has_gid;
613 gid_t gid;
614 bool has_mode;
615 mode_t mode;
616 bool has_fmode;
617 mode_t fmode;
618 bool has_dmode;
619 mode_t dmode;
620 bool has_selabel;
621 char* selabel;
622 bool has_capabilities;
623 uint64_t capabilities;
624};
625
626static struct perm_parsed_args ParsePermArgs(int argc, char** args) {
627 int i;
628 struct perm_parsed_args parsed;
629 int bad = 0;
630 static int max_warnings = 20;
631
632 memset(&parsed, 0, sizeof(parsed));
633
634 for (i = 1; i < argc; i += 2) {
635 if (strcmp("uid", args[i]) == 0) {
636 int64_t uid;
637 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
638 parsed.uid = uid;
639 parsed.has_uid = true;
640 } else {
641 printf("ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
642 bad++;
643 }
644 continue;
645 }
646 if (strcmp("gid", args[i]) == 0) {
647 int64_t gid;
648 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
649 parsed.gid = gid;
650 parsed.has_gid = true;
651 } else {
652 printf("ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
653 bad++;
654 }
655 continue;
656 }
657 if (strcmp("mode", args[i]) == 0) {
658 int32_t mode;
659 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
660 parsed.mode = mode;
661 parsed.has_mode = true;
662 } else {
663 printf("ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
664 bad++;
665 }
666 continue;
667 }
668 if (strcmp("dmode", args[i]) == 0) {
669 int32_t mode;
670 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
671 parsed.dmode = mode;
672 parsed.has_dmode = true;
673 } else {
674 printf("ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
675 bad++;
676 }
677 continue;
678 }
679 if (strcmp("fmode", args[i]) == 0) {
680 int32_t mode;
681 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
682 parsed.fmode = mode;
683 parsed.has_fmode = true;
684 } else {
685 printf("ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
686 bad++;
687 }
688 continue;
689 }
690 if (strcmp("capabilities", args[i]) == 0) {
691 int64_t capabilities;
692 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
693 parsed.capabilities = capabilities;
694 parsed.has_capabilities = true;
695 } else {
696 printf("ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
697 bad++;
698 }
699 continue;
700 }
701 if (strcmp("selabel", args[i]) == 0) {
702 if (args[i+1][0] != '\0') {
703 parsed.selabel = args[i+1];
704 parsed.has_selabel = true;
705 } else {
706 printf("ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
707 bad++;
708 }
709 continue;
710 }
711 if (max_warnings != 0) {
712 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
713 max_warnings--;
714 if (max_warnings == 0) {
715 printf("ParsedPermArgs: suppressing further warnings\n");
716 }
717 }
718 }
719 return parsed;
720}
721
722static int ApplyParsedPerms(
723 const char* filename,
724 const struct stat *statptr,
725 struct perm_parsed_args parsed)
726{
727 int bad = 0;
728
729 if (parsed.has_uid) {
730 if (chown(filename, parsed.uid, -1) < 0) {
731 printf("ApplyParsedPerms: chown of %s to %d failed: %s\n",
732 filename, parsed.uid, strerror(errno));
733 bad++;
734 }
735 }
736
737 if (parsed.has_gid) {
738 if (chown(filename, -1, parsed.gid) < 0) {
739 printf("ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
740 filename, parsed.gid, strerror(errno));
741 bad++;
742 }
743 }
744
745 if (parsed.has_mode) {
746 if (chmod(filename, parsed.mode) < 0) {
747 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
748 filename, parsed.mode, strerror(errno));
749 bad++;
750 }
751 }
752
753 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
754 if (chmod(filename, parsed.dmode) < 0) {
755 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
756 filename, parsed.dmode, strerror(errno));
757 bad++;
758 }
759 }
760
761 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
762 if (chmod(filename, parsed.fmode) < 0) {
763 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
764 filename, parsed.fmode, strerror(errno));
765 bad++;
766 }
767 }
768
769 if (parsed.has_selabel) {
770 // TODO: Don't silently ignore ENOTSUP
771 if (lsetfilecon(filename, parsed.selabel) && (errno != ENOTSUP)) {
772 printf("ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
773 filename, parsed.selabel, strerror(errno));
774 bad++;
775 }
776 }
777
778 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
779 if (parsed.capabilities == 0) {
780 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
781 // Report failure unless it's ENODATA (attribute not set)
782 printf("ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
783 filename, parsed.capabilities, strerror(errno));
784 bad++;
785 }
786 } else {
787 struct vfs_cap_data cap_data;
788 memset(&cap_data, 0, sizeof(cap_data));
789 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
790 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
791 cap_data.data[0].inheritable = 0;
792 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
793 cap_data.data[1].inheritable = 0;
794 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
795 printf("ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
796 filename, parsed.capabilities, strerror(errno));
797 bad++;
798 }
799 }
800 }
801
802 return bad;
803}
804
805// nftw doesn't allow us to pass along context, so we need to use
806// global variables. *sigh*
807static struct perm_parsed_args recursive_parsed_args;
808
809static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
810 int fileflags, struct FTW *pfwt) {
811 return ApplyParsedPerms(filename, statptr, recursive_parsed_args);
812}
813
814static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
815 int i;
816 int bad = 0;
817 static int nwarnings = 0;
818 struct stat sb;
819 Value* result = NULL;
820
821 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
822
823 if ((argc % 2) != 1) {
824 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d",
825 name, argc);
826 }
827
828 char** args = ReadVarArgs(state, argc, argv);
829 if (args == NULL) return NULL;
830
831 if (lstat(args[0], &sb) == -1) {
832 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
833 goto done;
834 }
835
836 struct perm_parsed_args parsed = ParsePermArgs(argc, args);
837
838 if (recursive) {
839 recursive_parsed_args = parsed;
840 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
841 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
842 } else {
843 bad += ApplyParsedPerms(args[0], &sb, parsed);
844 }
845
846done:
847 for (i = 0; i < argc; ++i) {
848 free(args[i]);
849 }
850 free(args);
851
852 if (result != NULL) {
853 return result;
854 }
855
856 if (bad > 0) {
857 return ErrorAbort(state, "%s: some changes failed", name);
858 }
859
860 return StringValue(strdup(""));
861}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700862
Doug Zongker512536a2010-02-17 16:11:44 -0800863Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700864 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700865 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700866 }
867 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700868 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700869 if (key == NULL) return NULL;
870
871 char value[PROPERTY_VALUE_MAX];
872 property_get(key, value, "");
873 free(key);
874
Doug Zongker512536a2010-02-17 16:11:44 -0800875 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700876}
877
878
Doug Zongker47cace92009-06-18 10:11:50 -0700879// file_getprop(file, key)
880//
881// interprets 'file' as a getprop-style file (key=value pairs, one
882// per line, # comment lines and blank lines okay), and returns the value
883// for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800884Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700885 char* result = NULL;
886 char* buffer = NULL;
887 char* filename;
888 char* key;
889 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
890 return NULL;
891 }
892
893 struct stat st;
894 if (stat(filename, &st) < 0) {
895 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
896 name, filename, strerror(errno));
897 goto done;
898 }
899
900#define MAX_FILE_GETPROP_SIZE 65536
901
902 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
903 ErrorAbort(state, "%s too large for %s (max %d)",
904 filename, name, MAX_FILE_GETPROP_SIZE);
905 goto done;
906 }
907
908 buffer = malloc(st.st_size+1);
909 if (buffer == NULL) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700910 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700911 goto done;
912 }
913
914 FILE* f = fopen(filename, "rb");
915 if (f == NULL) {
916 ErrorAbort(state, "%s: failed to open %s: %s",
917 name, filename, strerror(errno));
918 goto done;
919 }
920
921 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700922 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Doug Zongker47cace92009-06-18 10:11:50 -0700923 name, st.st_size+1, filename);
924 fclose(f);
925 goto done;
926 }
927 buffer[st.st_size] = '\0';
928
929 fclose(f);
930
931 char* line = strtok(buffer, "\n");
932 do {
933 // skip whitespace at start of line
934 while (*line && isspace(*line)) ++line;
935
936 // comment or blank line: skip to next line
937 if (*line == '\0' || *line == '#') continue;
938
939 char* equal = strchr(line, '=');
940 if (equal == NULL) {
941 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
942 name, line, filename);
943 goto done;
944 }
945
946 // trim whitespace between key and '='
947 char* key_end = equal-1;
948 while (key_end > line && isspace(*key_end)) --key_end;
949 key_end[1] = '\0';
950
951 // not the key we're looking for
952 if (strcmp(key, line) != 0) continue;
953
954 // skip whitespace after the '=' to the start of the value
955 char* val_start = equal+1;
956 while(*val_start && isspace(*val_start)) ++val_start;
957
958 // trim trailing whitespace
959 char* val_end = val_start + strlen(val_start)-1;
960 while (val_end > val_start && isspace(*val_end)) --val_end;
961 val_end[1] = '\0';
962
963 result = strdup(val_start);
964 break;
965
966 } while ((line = strtok(NULL, "\n")));
967
968 if (result == NULL) result = strdup("");
969
970 done:
971 free(filename);
972 free(key);
973 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800974 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -0700975}
976
977
Doug Zongker8edb00c2009-06-11 17:21:44 -0700978static bool write_raw_image_cb(const unsigned char* data,
979 int data_len, void* ctx) {
980 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
981 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700982 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700983 return false;
984}
985
Doug Zongker179b2d92011-04-12 15:49:04 -0700986// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -0800987Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700988 char* result = NULL;
989
Doug Zongker179b2d92011-04-12 15:49:04 -0700990 Value* partition_value;
991 Value* contents;
992 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700993 return NULL;
994 }
995
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700996 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -0700997 if (partition_value->type != VAL_STRING) {
998 ErrorAbort(state, "partition argument to %s must be string", name);
999 goto done;
1000 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001001 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001002 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001003 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001004 goto done;
1005 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001006 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001007 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001008 goto done;
1009 }
1010
1011 mtd_scan_partitions();
1012 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
1013 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001014 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001015 result = strdup("");
1016 goto done;
1017 }
1018
1019 MtdWriteContext* ctx = mtd_write_partition(mtd);
1020 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001021 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001022 name, partition);
1023 result = strdup("");
1024 goto done;
1025 }
1026
1027 bool success;
1028
Doug Zongker179b2d92011-04-12 15:49:04 -07001029 if (contents->type == VAL_STRING) {
1030 // we're given a filename as the contents
1031 char* filename = contents->data;
1032 FILE* f = fopen(filename, "rb");
1033 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001034 printf("%s: can't open %s: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001035 name, filename, strerror(errno));
1036 result = strdup("");
1037 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001038 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001039
1040 success = true;
1041 char* buffer = malloc(BUFSIZ);
1042 int read;
1043 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1044 int wrote = mtd_write_data(ctx, buffer, read);
1045 success = success && (wrote == read);
1046 }
1047 free(buffer);
1048 fclose(f);
1049 } else {
1050 // we're given a blob as the contents
1051 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1052 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001053 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001054 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001055 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001056 partition, strerror(errno));
1057 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001058
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001059 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001060 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001061 }
1062 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001063 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001064 }
1065
Doug Zongker179b2d92011-04-12 15:49:04 -07001066 printf("%s %s partition\n",
1067 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001068
1069 result = success ? partition : strdup("");
1070
1071done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001072 if (result != partition) FreeValue(partition_value);
1073 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001074 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001075}
1076
Doug Zongker8edb00c2009-06-11 17:21:44 -07001077// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001078Value* ApplyPatchSpaceFn(const char* name, State* state,
1079 int argc, Expr* argv[]) {
1080 char* bytes_str;
1081 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1082 return NULL;
1083 }
1084
1085 char* endptr;
1086 size_t bytes = strtol(bytes_str, &endptr, 10);
1087 if (bytes == 0 && endptr == bytes_str) {
1088 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
1089 name, bytes_str);
1090 free(bytes_str);
1091 return NULL;
1092 }
1093
1094 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1095}
1096
1097
1098// apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1_1, patch_1, ...)
Doug Zongker512536a2010-02-17 16:11:44 -08001099Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001100 if (argc < 6 || (argc % 2) == 1) {
1101 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1102 "even number, got %d",
1103 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001104 }
1105
Doug Zongkerc4351c72010-02-22 14:46:32 -08001106 char* source_filename;
1107 char* target_filename;
1108 char* target_sha1;
1109 char* target_size_str;
1110 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1111 &target_sha1, &target_size_str) < 0) {
1112 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001113 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001114
Doug Zongkerc4351c72010-02-22 14:46:32 -08001115 char* endptr;
1116 size_t target_size = strtol(target_size_str, &endptr, 10);
1117 if (target_size == 0 && endptr == target_size_str) {
1118 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1119 name, target_size_str);
1120 free(source_filename);
1121 free(target_filename);
1122 free(target_sha1);
1123 free(target_size_str);
1124 return NULL;
1125 }
1126
1127 int patchcount = (argc-4) / 2;
1128 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001129
1130 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001131 for (i = 0; i < patchcount; ++i) {
1132 if (patches[i*2]->type != VAL_STRING) {
1133 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1134 break;
1135 }
1136 if (patches[i*2+1]->type != VAL_BLOB) {
1137 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1138 break;
1139 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001140 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001141 if (i != patchcount) {
1142 for (i = 0; i < patchcount*2; ++i) {
1143 FreeValue(patches[i]);
1144 }
1145 free(patches);
1146 return NULL;
1147 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001148
Doug Zongkerc4351c72010-02-22 14:46:32 -08001149 char** patch_sha_str = malloc(patchcount * sizeof(char*));
1150 for (i = 0; i < patchcount; ++i) {
1151 patch_sha_str[i] = patches[i*2]->data;
1152 patches[i*2]->data = NULL;
1153 FreeValue(patches[i*2]);
1154 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001155 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001156
1157 int result = applypatch(source_filename, target_filename,
1158 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001159 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001160
1161 for (i = 0; i < patchcount; ++i) {
1162 FreeValue(patches[i]);
1163 }
1164 free(patch_sha_str);
1165 free(patches);
1166
1167 return StringValue(strdup(result == 0 ? "t" : ""));
1168}
1169
1170// apply_patch_check(file, [sha1_1, ...])
1171Value* ApplyPatchCheckFn(const char* name, State* state,
1172 int argc, Expr* argv[]) {
1173 if (argc < 1) {
1174 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1175 name, argc);
1176 }
1177
1178 char* filename;
1179 if (ReadArgs(state, argv, 1, &filename) < 0) {
1180 return NULL;
1181 }
1182
1183 int patchcount = argc-1;
1184 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1185
1186 int result = applypatch_check(filename, patchcount, sha1s);
1187
1188 int i;
1189 for (i = 0; i < patchcount; ++i) {
1190 free(sha1s[i]);
1191 }
1192 free(sha1s);
1193
1194 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001195}
1196
Doug Zongker512536a2010-02-17 16:11:44 -08001197Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001198 char** args = ReadVarArgs(state, argc, argv);
1199 if (args == NULL) {
1200 return NULL;
1201 }
1202
1203 int size = 0;
1204 int i;
1205 for (i = 0; i < argc; ++i) {
1206 size += strlen(args[i]);
1207 }
1208 char* buffer = malloc(size+1);
1209 size = 0;
1210 for (i = 0; i < argc; ++i) {
1211 strcpy(buffer+size, args[i]);
1212 size += strlen(args[i]);
1213 free(args[i]);
1214 }
1215 free(args);
1216 buffer[size] = '\0';
1217
1218 char* line = strtok(buffer, "\n");
1219 while (line) {
1220 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
1221 "ui_print %s\n", line);
1222 line = strtok(NULL, "\n");
1223 }
1224 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
1225
Doug Zongker512536a2010-02-17 16:11:44 -08001226 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001227}
1228
Doug Zongkerd0181b82011-10-19 10:51:12 -07001229Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1230 if (argc != 0) {
1231 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1232 }
1233 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1234 return StringValue(strdup("t"));
1235}
1236
Doug Zongker512536a2010-02-17 16:11:44 -08001237Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001238 if (argc < 1) {
1239 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1240 }
1241 char** args = ReadVarArgs(state, argc, argv);
1242 if (args == NULL) {
1243 return NULL;
1244 }
1245
1246 char** args2 = malloc(sizeof(char*) * (argc+1));
1247 memcpy(args2, args, sizeof(char*) * argc);
1248 args2[argc] = NULL;
1249
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001250 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001251
1252 pid_t child = fork();
1253 if (child == 0) {
1254 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001255 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001256 _exit(1);
1257 }
1258 int status;
1259 waitpid(child, &status, 0);
1260 if (WIFEXITED(status)) {
1261 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001262 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001263 WEXITSTATUS(status));
1264 }
1265 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001266 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001267 WTERMSIG(status));
1268 }
1269
1270 int i;
1271 for (i = 0; i < argc; ++i) {
1272 free(args[i]);
1273 }
1274 free(args);
1275 free(args2);
1276
1277 char buffer[20];
1278 sprintf(buffer, "%d", status);
1279
Doug Zongker512536a2010-02-17 16:11:44 -08001280 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001281}
1282
Doug Zongker512536a2010-02-17 16:11:44 -08001283// Take a sha-1 digest and return it as a newly-allocated hex string.
1284static char* PrintSha1(uint8_t* digest) {
1285 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
1286 int i;
1287 const char* alphabet = "0123456789abcdef";
1288 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
1289 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
1290 buffer[i*2+1] = alphabet[digest[i] & 0xf];
1291 }
1292 buffer[i*2] = '\0';
1293 return buffer;
1294}
1295
1296// sha1_check(data)
1297// to return the sha1 of the data (given in the format returned by
1298// read_file).
1299//
1300// sha1_check(data, sha1_hex, [sha1_hex, ...])
1301// returns the sha1 of the file if it matches any of the hex
1302// strings passed, or "" if it does not equal any of them.
1303//
1304Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1305 if (argc < 1) {
1306 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1307 }
1308
1309 Value** args = ReadValueVarArgs(state, argc, argv);
1310 if (args == NULL) {
1311 return NULL;
1312 }
1313
1314 if (args[0]->size < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001315 printf("%s(): no file contents received", name);
Doug Zongker512536a2010-02-17 16:11:44 -08001316 return StringValue(strdup(""));
1317 }
1318 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001319 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001320 FreeValue(args[0]);
1321
1322 if (argc == 1) {
1323 return StringValue(PrintSha1(digest));
1324 }
1325
1326 int i;
1327 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1328 for (i = 1; i < argc; ++i) {
1329 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001330 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001331 name, i);
1332 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1333 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001334 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1335 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001336 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1337 break;
1338 }
1339 FreeValue(args[i]);
1340 }
1341 if (i >= argc) {
1342 // Didn't match any of the hex strings; return false.
1343 return StringValue(strdup(""));
1344 }
1345 // Found a match; free all the remaining arguments and return the
1346 // matched one.
1347 int j;
1348 for (j = i+1; j < argc; ++j) {
1349 FreeValue(args[j]);
1350 }
1351 return args[i];
1352}
1353
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001354// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001355// is actually a FileContents*).
1356Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1357 if (argc != 1) {
1358 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1359 }
1360 char* filename;
1361 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1362
1363 Value* v = malloc(sizeof(Value));
1364 v->type = VAL_BLOB;
1365
1366 FileContents fc;
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001367 if (LoadFileContents(filename, &fc, RETOUCH_DONT_MASK) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001368 ErrorAbort(state, "%s() loading \"%s\" failed: %s",
1369 name, filename, strerror(errno));
1370 free(filename);
1371 free(v);
1372 free(fc.data);
1373 return NULL;
1374 }
1375
1376 v->size = fc.size;
1377 v->data = (char*)fc.data;
1378
1379 free(filename);
1380 return v;
1381}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001382
Doug Zongker9931f7f2009-06-10 14:11:53 -07001383void RegisterInstallFunctions() {
1384 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001385 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001386 RegisterFunction("unmount", UnmountFn);
1387 RegisterFunction("format", FormatFn);
1388 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001389 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001390 RegisterFunction("delete", DeleteFn);
1391 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001392 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1393 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001394 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001395
1396 // Maybe, at some future point, we can delete these functions? They have been
1397 // replaced by perm_set and perm_set_recursive.
Doug Zongker9931f7f2009-06-10 14:11:53 -07001398 RegisterFunction("set_perm", SetPermFn);
1399 RegisterFunction("set_perm_recursive", SetPermFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001400
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001401 // Usage:
1402 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1403 // Example:
1404 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1405 RegisterFunction("set_metadata", SetMetadataFn);
1406
1407 // Usage:
1408 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1409 // Example:
1410 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1411 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1412
Doug Zongker8edb00c2009-06-11 17:21:44 -07001413 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001414 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001415 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001416
1417 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001418 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1419 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001420
Doug Zongker512536a2010-02-17 16:11:44 -08001421 RegisterFunction("read_file", ReadFileFn);
1422 RegisterFunction("sha1_check", Sha1CheckFn);
1423
Doug Zongkerd0181b82011-10-19 10:51:12 -07001424 RegisterFunction("wipe_cache", WipeCacheFn);
1425
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001426 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001427
1428 RegisterFunction("run_program", RunProgramFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001429}