blob: aebd4f34be15a1c2481ac7a069f5456057d81ffa [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 Zongker8edb00c2009-06-11 17:21:44 -070048
Doug Zongker3d177d02010-07-01 09:18:44 -070049#ifdef USE_EXT4
50#include "make_ext4fs.h"
51#endif
52
53// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -070054//
Doug Zongker3d177d02010-07-01 09:18:44 -070055// fs_type="yaffs2" partition_type="MTD" location=partition
56// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -080057Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070058 char* result = NULL;
Doug Zongker3d177d02010-07-01 09:18:44 -070059 if (argc != 4) {
60 return ErrorAbort(state, "%s() expects 4 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -070061 }
Doug Zongker3d177d02010-07-01 09:18:44 -070062 char* fs_type;
63 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -070064 char* location;
65 char* mount_point;
Doug Zongker3d177d02010-07-01 09:18:44 -070066 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
67 &location, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070068 return NULL;
69 }
70
Doug Zongker3d177d02010-07-01 09:18:44 -070071 if (strlen(fs_type) == 0) {
72 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
73 goto done;
74 }
75 if (strlen(partition_type) == 0) {
76 ErrorAbort(state, "partition_type argument to %s() can't be empty",
77 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070078 goto done;
79 }
80 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070081 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070082 goto done;
83 }
84 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070085 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070086 goto done;
87 }
88
Stephen Smalley779701d2012-02-09 14:13:23 -050089 char *secontext = NULL;
90
91 if (sehandle) {
92 selabel_lookup(sehandle, &secontext, mount_point, 0755);
93 setfscreatecon(secontext);
94 }
Stephen Smalley779701d2012-02-09 14:13:23 -050095
Doug Zongker9931f7f2009-06-10 14:11:53 -070096 mkdir(mount_point, 0755);
97
Stephen Smalley779701d2012-02-09 14:13:23 -050098 if (secontext) {
99 freecon(secontext);
100 setfscreatecon(NULL);
101 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500102
Doug Zongker3d177d02010-07-01 09:18:44 -0700103 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700104 mtd_scan_partitions();
105 const MtdPartition* mtd;
106 mtd = mtd_find_partition_by_name(location);
107 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700108 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700109 name, location);
110 result = strdup("");
111 goto done;
112 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700113 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700114 printf("mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700115 location, strerror(errno));
116 result = strdup("");
117 goto done;
118 }
119 result = mount_point;
120 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700121 if (mount(location, mount_point, fs_type,
Doug Zongker9931f7f2009-06-10 14:11:53 -0700122 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700123 printf("%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700124 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700125 result = strdup("");
126 } else {
127 result = mount_point;
128 }
129 }
130
131done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700132 free(fs_type);
133 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700134 free(location);
135 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800136 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700137}
138
Doug Zongker8edb00c2009-06-11 17:21:44 -0700139
140// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800141Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700142 char* result = NULL;
143 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700144 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700145 }
146 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700147 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700148 return NULL;
149 }
150 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700151 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700152 goto done;
153 }
154
155 scan_mounted_volumes();
156 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
157 if (vol == NULL) {
158 result = strdup("");
159 } else {
160 result = mount_point;
161 }
162
163done:
164 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800165 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700166}
167
168
Doug Zongker512536a2010-02-17 16:11:44 -0800169Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700170 char* result = NULL;
171 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700172 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700173 }
174 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700175 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700176 return NULL;
177 }
178 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700179 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700180 goto done;
181 }
182
183 scan_mounted_volumes();
184 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
185 if (vol == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700186 printf("unmount of %s failed; no such volume\n", mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700187 result = strdup("");
188 } else {
189 unmount_mounted_volume(vol);
190 result = mount_point;
191 }
192
193done:
194 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800195 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700196}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700197
198
Stephen Smalley779701d2012-02-09 14:13:23 -0500199// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700200//
Stephen Smalley779701d2012-02-09 14:13:23 -0500201// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
202// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800203// if fs_size == 0, then make_ext4fs uses the entire partition.
204// if fs_size > 0, that is the size to use
205// if fs_size < 0, then reserve that many bytes at the end of the partition
Doug Zongker512536a2010-02-17 16:11:44 -0800206Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700207 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400208 if (argc != 5) {
209 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700210 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700211 char* fs_type;
212 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700213 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800214 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400215 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500216
Stephen Smalley779701d2012-02-09 14:13:23 -0500217 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
218 return NULL;
219 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700220
Doug Zongker3d177d02010-07-01 09:18:44 -0700221 if (strlen(fs_type) == 0) {
222 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
223 goto done;
224 }
225 if (strlen(partition_type) == 0) {
226 ErrorAbort(state, "partition_type argument to %s() can't be empty",
227 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700228 goto done;
229 }
230 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700231 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700232 goto done;
233 }
234
Stephen Smalley516e4e22012-04-03 13:35:11 -0400235 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500236 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
237 goto done;
238 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500239
Doug Zongker3d177d02010-07-01 09:18:44 -0700240 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700241 mtd_scan_partitions();
242 const MtdPartition* mtd = mtd_find_partition_by_name(location);
243 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700244 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700245 name, location);
246 result = strdup("");
247 goto done;
248 }
249 MtdWriteContext* ctx = mtd_write_partition(mtd);
250 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700251 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700252 result = strdup("");
253 goto done;
254 }
255 if (mtd_erase_blocks(ctx, -1) == -1) {
256 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700257 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700258 result = strdup("");
259 goto done;
260 }
261 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700262 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700263 result = strdup("");
264 goto done;
265 }
266 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700267#ifdef USE_EXT4
268 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500269 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700270 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700271 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700272 name, status, location);
273 result = strdup("");
274 goto done;
275 }
276 result = location;
277#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700278 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700279 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700280 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700281 }
282
283done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700284 free(fs_type);
285 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700286 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800287 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700288}
289
Michael Rungece7ca712013-11-06 17:42:20 -0800290Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
291 char* result = NULL;
292 if (argc != 2) {
293 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
294 }
295
296 char* src_name;
297 char* dst_name;
298
299 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
300 return NULL;
301 }
302 if (strlen(src_name) == 0) {
303 ErrorAbort(state, "src_name argument to %s() can't be empty", name);
304 goto done;
305 }
306 if (strlen(dst_name) == 0) {
307 ErrorAbort(state, "dst_name argument to %s() can't be empty",
308 name);
309 goto done;
310 }
311
312 if (rename(src_name, dst_name) != 0) {
313 ErrorAbort(state, "Rename of %s() to %s() failed, error %s()",
314 src_name, dst_name, strerror(errno));
315 } else {
316 result = dst_name;
317 }
318
319done:
320 free(src_name);
321 if (result != dst_name) free(dst_name);
322 return StringValue(result);
323}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700324
Doug Zongker512536a2010-02-17 16:11:44 -0800325Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700326 char** paths = malloc(argc * sizeof(char*));
327 int i;
328 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700329 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700330 if (paths[i] == NULL) {
331 int j;
332 for (j = 0; j < i; ++i) {
333 free(paths[j]);
334 }
335 free(paths);
336 return NULL;
337 }
338 }
339
340 bool recursive = (strcmp(name, "delete_recursive") == 0);
341
342 int success = 0;
343 for (i = 0; i < argc; ++i) {
344 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
345 ++success;
346 free(paths[i]);
347 }
348 free(paths);
349
350 char buffer[10];
351 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800352 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700353}
354
Doug Zongker8edb00c2009-06-11 17:21:44 -0700355
Doug Zongker512536a2010-02-17 16:11:44 -0800356Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700357 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700358 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700359 }
360 char* frac_str;
361 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700362 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700363 return NULL;
364 }
365
366 double frac = strtod(frac_str, NULL);
367 int sec = strtol(sec_str, NULL, 10);
368
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700369 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700370 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
371
Doug Zongker9931f7f2009-06-10 14:11:53 -0700372 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800373 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700374}
375
Doug Zongker512536a2010-02-17 16:11:44 -0800376Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700377 if (argc != 1) {
378 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
379 }
380 char* frac_str;
381 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
382 return NULL;
383 }
384
385 double frac = strtod(frac_str, NULL);
386
387 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
388 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
389
Doug Zongker512536a2010-02-17 16:11:44 -0800390 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700391}
392
Doug Zongker8edb00c2009-06-11 17:21:44 -0700393// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800394Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700395 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700396 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700397 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700398 }
399 char* zip_path;
400 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700401 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700402
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700403 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700404
405 // To create a consistent system image, never use the clock for timestamps.
406 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
407
408 bool success = mzExtractRecursive(za, zip_path, dest_path,
409 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500410 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700411 free(zip_path);
412 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800413 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700414}
415
Doug Zongker8edb00c2009-06-11 17:21:44 -0700416
417// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800418// or
419// package_extract_file(package_path)
420// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800421// function (the char* returned is actually a FileContents*).
422Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700423 int argc, Expr* argv[]) {
Doug Zongker6aece332010-02-01 14:40:12 -0800424 if (argc != 1 && argc != 2) {
425 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
426 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700427 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700428 bool success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800429 if (argc == 2) {
430 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700431
Doug Zongker6aece332010-02-01 14:40:12 -0800432 char* zip_path;
433 char* dest_path;
434 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
435
436 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
437 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
438 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700439 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800440 goto done2;
441 }
442
443 FILE* f = fopen(dest_path, "wb");
444 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700445 printf("%s: can't open %s for write: %s\n",
Doug Zongker6aece332010-02-01 14:40:12 -0800446 name, dest_path, strerror(errno));
447 goto done2;
448 }
449 success = mzExtractZipEntryToFile(za, entry, fileno(f));
450 fclose(f);
451
452 done2:
453 free(zip_path);
454 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800455 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800456 } else {
457 // The one-argument version returns the contents of the file
458 // as the result.
459
460 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800461 Value* v = malloc(sizeof(Value));
462 v->type = VAL_BLOB;
463 v->size = -1;
464 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800465
466 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
467
468 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
469 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
470 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700471 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800472 goto done1;
473 }
474
Doug Zongker512536a2010-02-17 16:11:44 -0800475 v->size = mzGetZipEntryUncompLen(entry);
476 v->data = malloc(v->size);
477 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700478 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800479 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800480 goto done1;
481 }
482
Doug Zongker512536a2010-02-17 16:11:44 -0800483 success = mzExtractZipEntryToBuffer(za, entry,
484 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800485
486 done1:
487 free(zip_path);
488 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800489 free(v->data);
490 v->data = NULL;
491 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800492 }
Doug Zongker512536a2010-02-17 16:11:44 -0800493 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700494 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700495}
496
Doug Zongkera23075f2012-08-06 16:19:09 -0700497// Create all parent directories of name, if necessary.
498static int make_parents(char* name) {
499 char* p;
500 for (p = name + (strlen(name)-1); p > name; --p) {
501 if (*p != '/') continue;
502 *p = '\0';
503 if (make_parents(name) < 0) return -1;
504 int result = mkdir(name, 0700);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700505 if (result == 0) printf("symlink(): created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700506 *p = '/';
507 if (result == 0 || errno == EEXIST) {
508 // successfully created or already existed; we're done
509 return 0;
510 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700511 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700512 return -1;
513 }
514 }
515 return 0;
516}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700517
Doug Zongker9931f7f2009-06-10 14:11:53 -0700518// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700519// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800520Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700521 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700522 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700523 }
524 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700525 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700526 if (target == NULL) return NULL;
527
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700528 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700529 if (srcs == NULL) {
530 free(target);
531 return NULL;
532 }
533
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700534 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700535 int i;
536 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700537 if (unlink(srcs[i]) < 0) {
538 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700539 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700540 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700541 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700542 }
543 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700544 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700545 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700546 name, srcs[i], target);
547 ++bad;
548 }
Doug Zongker60babf82009-09-18 15:11:24 -0700549 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700550 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700551 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700552 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700553 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700554 free(srcs[i]);
555 }
556 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700557 if (bad) {
558 return ErrorAbort(state, "%s: some symlinks failed", name);
559 }
Doug Zongker512536a2010-02-17 16:11:44 -0800560 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700561}
562
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700563struct perm_parsed_args {
564 bool has_uid;
565 uid_t uid;
566 bool has_gid;
567 gid_t gid;
568 bool has_mode;
569 mode_t mode;
570 bool has_fmode;
571 mode_t fmode;
572 bool has_dmode;
573 mode_t dmode;
574 bool has_selabel;
575 char* selabel;
576 bool has_capabilities;
577 uint64_t capabilities;
578};
579
580static struct perm_parsed_args ParsePermArgs(int argc, char** args) {
581 int i;
582 struct perm_parsed_args parsed;
583 int bad = 0;
584 static int max_warnings = 20;
585
586 memset(&parsed, 0, sizeof(parsed));
587
588 for (i = 1; i < argc; i += 2) {
589 if (strcmp("uid", args[i]) == 0) {
590 int64_t uid;
591 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
592 parsed.uid = uid;
593 parsed.has_uid = true;
594 } else {
595 printf("ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
596 bad++;
597 }
598 continue;
599 }
600 if (strcmp("gid", args[i]) == 0) {
601 int64_t gid;
602 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
603 parsed.gid = gid;
604 parsed.has_gid = true;
605 } else {
606 printf("ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
607 bad++;
608 }
609 continue;
610 }
611 if (strcmp("mode", args[i]) == 0) {
612 int32_t mode;
613 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
614 parsed.mode = mode;
615 parsed.has_mode = true;
616 } else {
617 printf("ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
618 bad++;
619 }
620 continue;
621 }
622 if (strcmp("dmode", args[i]) == 0) {
623 int32_t mode;
624 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
625 parsed.dmode = mode;
626 parsed.has_dmode = true;
627 } else {
628 printf("ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
629 bad++;
630 }
631 continue;
632 }
633 if (strcmp("fmode", args[i]) == 0) {
634 int32_t mode;
635 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
636 parsed.fmode = mode;
637 parsed.has_fmode = true;
638 } else {
639 printf("ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
640 bad++;
641 }
642 continue;
643 }
644 if (strcmp("capabilities", args[i]) == 0) {
645 int64_t capabilities;
646 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
647 parsed.capabilities = capabilities;
648 parsed.has_capabilities = true;
649 } else {
650 printf("ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
651 bad++;
652 }
653 continue;
654 }
655 if (strcmp("selabel", args[i]) == 0) {
656 if (args[i+1][0] != '\0') {
657 parsed.selabel = args[i+1];
658 parsed.has_selabel = true;
659 } else {
660 printf("ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
661 bad++;
662 }
663 continue;
664 }
665 if (max_warnings != 0) {
666 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
667 max_warnings--;
668 if (max_warnings == 0) {
669 printf("ParsedPermArgs: suppressing further warnings\n");
670 }
671 }
672 }
673 return parsed;
674}
675
676static int ApplyParsedPerms(
677 const char* filename,
678 const struct stat *statptr,
679 struct perm_parsed_args parsed)
680{
681 int bad = 0;
682
Nick Kraleviche4612512013-09-10 15:34:19 -0700683 /* ignore symlinks */
684 if (S_ISLNK(statptr->st_mode)) {
685 return 0;
686 }
687
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700688 if (parsed.has_uid) {
689 if (chown(filename, parsed.uid, -1) < 0) {
690 printf("ApplyParsedPerms: chown of %s to %d failed: %s\n",
691 filename, parsed.uid, strerror(errno));
692 bad++;
693 }
694 }
695
696 if (parsed.has_gid) {
697 if (chown(filename, -1, parsed.gid) < 0) {
698 printf("ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
699 filename, parsed.gid, strerror(errno));
700 bad++;
701 }
702 }
703
704 if (parsed.has_mode) {
705 if (chmod(filename, parsed.mode) < 0) {
706 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
707 filename, parsed.mode, strerror(errno));
708 bad++;
709 }
710 }
711
712 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
713 if (chmod(filename, parsed.dmode) < 0) {
714 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
715 filename, parsed.dmode, strerror(errno));
716 bad++;
717 }
718 }
719
720 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
721 if (chmod(filename, parsed.fmode) < 0) {
722 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
723 filename, parsed.fmode, strerror(errno));
724 bad++;
725 }
726 }
727
728 if (parsed.has_selabel) {
729 // TODO: Don't silently ignore ENOTSUP
730 if (lsetfilecon(filename, parsed.selabel) && (errno != ENOTSUP)) {
731 printf("ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
732 filename, parsed.selabel, strerror(errno));
733 bad++;
734 }
735 }
736
737 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
738 if (parsed.capabilities == 0) {
739 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
740 // Report failure unless it's ENODATA (attribute not set)
741 printf("ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
742 filename, parsed.capabilities, strerror(errno));
743 bad++;
744 }
745 } else {
746 struct vfs_cap_data cap_data;
747 memset(&cap_data, 0, sizeof(cap_data));
748 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
749 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
750 cap_data.data[0].inheritable = 0;
751 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
752 cap_data.data[1].inheritable = 0;
753 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
754 printf("ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
755 filename, parsed.capabilities, strerror(errno));
756 bad++;
757 }
758 }
759 }
760
761 return bad;
762}
763
764// nftw doesn't allow us to pass along context, so we need to use
765// global variables. *sigh*
766static struct perm_parsed_args recursive_parsed_args;
767
768static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
769 int fileflags, struct FTW *pfwt) {
770 return ApplyParsedPerms(filename, statptr, recursive_parsed_args);
771}
772
773static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
774 int i;
775 int bad = 0;
776 static int nwarnings = 0;
777 struct stat sb;
778 Value* result = NULL;
779
780 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
781
782 if ((argc % 2) != 1) {
783 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d",
784 name, argc);
785 }
786
787 char** args = ReadVarArgs(state, argc, argv);
788 if (args == NULL) return NULL;
789
790 if (lstat(args[0], &sb) == -1) {
791 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
792 goto done;
793 }
794
795 struct perm_parsed_args parsed = ParsePermArgs(argc, args);
796
797 if (recursive) {
798 recursive_parsed_args = parsed;
799 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
800 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
801 } else {
802 bad += ApplyParsedPerms(args[0], &sb, parsed);
803 }
804
805done:
806 for (i = 0; i < argc; ++i) {
807 free(args[i]);
808 }
809 free(args);
810
811 if (result != NULL) {
812 return result;
813 }
814
815 if (bad > 0) {
816 return ErrorAbort(state, "%s: some changes failed", name);
817 }
818
819 return StringValue(strdup(""));
820}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700821
Doug Zongker512536a2010-02-17 16:11:44 -0800822Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700823 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700824 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700825 }
826 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700827 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700828 if (key == NULL) return NULL;
829
830 char value[PROPERTY_VALUE_MAX];
831 property_get(key, value, "");
832 free(key);
833
Doug Zongker512536a2010-02-17 16:11:44 -0800834 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700835}
836
837
Doug Zongker47cace92009-06-18 10:11:50 -0700838// file_getprop(file, key)
839//
840// interprets 'file' as a getprop-style file (key=value pairs, one
841// per line, # comment lines and blank lines okay), and returns the value
842// for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800843Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700844 char* result = NULL;
845 char* buffer = NULL;
846 char* filename;
847 char* key;
848 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
849 return NULL;
850 }
851
852 struct stat st;
853 if (stat(filename, &st) < 0) {
854 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
855 name, filename, strerror(errno));
856 goto done;
857 }
858
859#define MAX_FILE_GETPROP_SIZE 65536
860
861 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
862 ErrorAbort(state, "%s too large for %s (max %d)",
863 filename, name, MAX_FILE_GETPROP_SIZE);
864 goto done;
865 }
866
867 buffer = malloc(st.st_size+1);
868 if (buffer == NULL) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700869 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700870 goto done;
871 }
872
873 FILE* f = fopen(filename, "rb");
874 if (f == NULL) {
875 ErrorAbort(state, "%s: failed to open %s: %s",
876 name, filename, strerror(errno));
877 goto done;
878 }
879
880 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700881 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Doug Zongker47cace92009-06-18 10:11:50 -0700882 name, st.st_size+1, filename);
883 fclose(f);
884 goto done;
885 }
886 buffer[st.st_size] = '\0';
887
888 fclose(f);
889
890 char* line = strtok(buffer, "\n");
891 do {
892 // skip whitespace at start of line
893 while (*line && isspace(*line)) ++line;
894
895 // comment or blank line: skip to next line
896 if (*line == '\0' || *line == '#') continue;
897
898 char* equal = strchr(line, '=');
899 if (equal == NULL) {
900 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
901 name, line, filename);
902 goto done;
903 }
904
905 // trim whitespace between key and '='
906 char* key_end = equal-1;
907 while (key_end > line && isspace(*key_end)) --key_end;
908 key_end[1] = '\0';
909
910 // not the key we're looking for
911 if (strcmp(key, line) != 0) continue;
912
913 // skip whitespace after the '=' to the start of the value
914 char* val_start = equal+1;
915 while(*val_start && isspace(*val_start)) ++val_start;
916
917 // trim trailing whitespace
918 char* val_end = val_start + strlen(val_start)-1;
919 while (val_end > val_start && isspace(*val_end)) --val_end;
920 val_end[1] = '\0';
921
922 result = strdup(val_start);
923 break;
924
925 } while ((line = strtok(NULL, "\n")));
926
927 if (result == NULL) result = strdup("");
928
929 done:
930 free(filename);
931 free(key);
932 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800933 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -0700934}
935
936
Doug Zongker8edb00c2009-06-11 17:21:44 -0700937static bool write_raw_image_cb(const unsigned char* data,
938 int data_len, void* ctx) {
939 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
940 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700941 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700942 return false;
943}
944
Doug Zongker179b2d92011-04-12 15:49:04 -0700945// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -0800946Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700947 char* result = NULL;
948
Doug Zongker179b2d92011-04-12 15:49:04 -0700949 Value* partition_value;
950 Value* contents;
951 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700952 return NULL;
953 }
954
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700955 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -0700956 if (partition_value->type != VAL_STRING) {
957 ErrorAbort(state, "partition argument to %s must be string", name);
958 goto done;
959 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700960 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700961 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700962 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700963 goto done;
964 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700965 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700966 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700967 goto done;
968 }
969
970 mtd_scan_partitions();
971 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
972 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700973 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700974 result = strdup("");
975 goto done;
976 }
977
978 MtdWriteContext* ctx = mtd_write_partition(mtd);
979 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700980 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -0700981 name, partition);
982 result = strdup("");
983 goto done;
984 }
985
986 bool success;
987
Doug Zongker179b2d92011-04-12 15:49:04 -0700988 if (contents->type == VAL_STRING) {
989 // we're given a filename as the contents
990 char* filename = contents->data;
991 FILE* f = fopen(filename, "rb");
992 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700993 printf("%s: can't open %s: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -0700994 name, filename, strerror(errno));
995 result = strdup("");
996 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700997 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700998
999 success = true;
1000 char* buffer = malloc(BUFSIZ);
1001 int read;
1002 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1003 int wrote = mtd_write_data(ctx, buffer, read);
1004 success = success && (wrote == read);
1005 }
1006 free(buffer);
1007 fclose(f);
1008 } else {
1009 // we're given a blob as the contents
1010 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1011 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001012 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001013 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001014 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001015 partition, strerror(errno));
1016 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001017
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001018 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001019 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001020 }
1021 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001022 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001023 }
1024
Doug Zongker179b2d92011-04-12 15:49:04 -07001025 printf("%s %s partition\n",
1026 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001027
1028 result = success ? partition : strdup("");
1029
1030done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001031 if (result != partition) FreeValue(partition_value);
1032 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001033 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001034}
1035
Doug Zongker8edb00c2009-06-11 17:21:44 -07001036// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001037Value* ApplyPatchSpaceFn(const char* name, State* state,
1038 int argc, Expr* argv[]) {
1039 char* bytes_str;
1040 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1041 return NULL;
1042 }
1043
1044 char* endptr;
1045 size_t bytes = strtol(bytes_str, &endptr, 10);
1046 if (bytes == 0 && endptr == bytes_str) {
1047 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
1048 name, bytes_str);
1049 free(bytes_str);
1050 return NULL;
1051 }
1052
1053 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1054}
1055
1056
1057// apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1_1, patch_1, ...)
Doug Zongker512536a2010-02-17 16:11:44 -08001058Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001059 if (argc < 6 || (argc % 2) == 1) {
1060 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1061 "even number, got %d",
1062 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001063 }
1064
Doug Zongkerc4351c72010-02-22 14:46:32 -08001065 char* source_filename;
1066 char* target_filename;
1067 char* target_sha1;
1068 char* target_size_str;
1069 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1070 &target_sha1, &target_size_str) < 0) {
1071 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001072 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001073
Doug Zongkerc4351c72010-02-22 14:46:32 -08001074 char* endptr;
1075 size_t target_size = strtol(target_size_str, &endptr, 10);
1076 if (target_size == 0 && endptr == target_size_str) {
1077 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1078 name, target_size_str);
1079 free(source_filename);
1080 free(target_filename);
1081 free(target_sha1);
1082 free(target_size_str);
1083 return NULL;
1084 }
1085
1086 int patchcount = (argc-4) / 2;
1087 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001088
1089 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001090 for (i = 0; i < patchcount; ++i) {
1091 if (patches[i*2]->type != VAL_STRING) {
1092 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1093 break;
1094 }
1095 if (patches[i*2+1]->type != VAL_BLOB) {
1096 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1097 break;
1098 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001099 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001100 if (i != patchcount) {
1101 for (i = 0; i < patchcount*2; ++i) {
1102 FreeValue(patches[i]);
1103 }
1104 free(patches);
1105 return NULL;
1106 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001107
Doug Zongkerc4351c72010-02-22 14:46:32 -08001108 char** patch_sha_str = malloc(patchcount * sizeof(char*));
1109 for (i = 0; i < patchcount; ++i) {
1110 patch_sha_str[i] = patches[i*2]->data;
1111 patches[i*2]->data = NULL;
1112 FreeValue(patches[i*2]);
1113 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001114 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001115
1116 int result = applypatch(source_filename, target_filename,
1117 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001118 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001119
1120 for (i = 0; i < patchcount; ++i) {
1121 FreeValue(patches[i]);
1122 }
1123 free(patch_sha_str);
1124 free(patches);
1125
1126 return StringValue(strdup(result == 0 ? "t" : ""));
1127}
1128
1129// apply_patch_check(file, [sha1_1, ...])
1130Value* ApplyPatchCheckFn(const char* name, State* state,
1131 int argc, Expr* argv[]) {
1132 if (argc < 1) {
1133 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1134 name, argc);
1135 }
1136
1137 char* filename;
1138 if (ReadArgs(state, argv, 1, &filename) < 0) {
1139 return NULL;
1140 }
1141
1142 int patchcount = argc-1;
1143 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1144
1145 int result = applypatch_check(filename, patchcount, sha1s);
1146
1147 int i;
1148 for (i = 0; i < patchcount; ++i) {
1149 free(sha1s[i]);
1150 }
1151 free(sha1s);
1152
1153 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001154}
1155
Doug Zongker512536a2010-02-17 16:11:44 -08001156Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001157 char** args = ReadVarArgs(state, argc, argv);
1158 if (args == NULL) {
1159 return NULL;
1160 }
1161
1162 int size = 0;
1163 int i;
1164 for (i = 0; i < argc; ++i) {
1165 size += strlen(args[i]);
1166 }
1167 char* buffer = malloc(size+1);
1168 size = 0;
1169 for (i = 0; i < argc; ++i) {
1170 strcpy(buffer+size, args[i]);
1171 size += strlen(args[i]);
1172 free(args[i]);
1173 }
1174 free(args);
1175 buffer[size] = '\0';
1176
1177 char* line = strtok(buffer, "\n");
1178 while (line) {
1179 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
1180 "ui_print %s\n", line);
1181 line = strtok(NULL, "\n");
1182 }
1183 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
1184
Doug Zongker512536a2010-02-17 16:11:44 -08001185 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001186}
1187
Doug Zongkerd0181b82011-10-19 10:51:12 -07001188Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1189 if (argc != 0) {
1190 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1191 }
1192 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1193 return StringValue(strdup("t"));
1194}
1195
Doug Zongker512536a2010-02-17 16:11:44 -08001196Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001197 if (argc < 1) {
1198 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1199 }
1200 char** args = ReadVarArgs(state, argc, argv);
1201 if (args == NULL) {
1202 return NULL;
1203 }
1204
1205 char** args2 = malloc(sizeof(char*) * (argc+1));
1206 memcpy(args2, args, sizeof(char*) * argc);
1207 args2[argc] = NULL;
1208
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001209 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001210
1211 pid_t child = fork();
1212 if (child == 0) {
1213 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001214 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001215 _exit(1);
1216 }
1217 int status;
1218 waitpid(child, &status, 0);
1219 if (WIFEXITED(status)) {
1220 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001221 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001222 WEXITSTATUS(status));
1223 }
1224 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001225 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001226 WTERMSIG(status));
1227 }
1228
1229 int i;
1230 for (i = 0; i < argc; ++i) {
1231 free(args[i]);
1232 }
1233 free(args);
1234 free(args2);
1235
1236 char buffer[20];
1237 sprintf(buffer, "%d", status);
1238
Doug Zongker512536a2010-02-17 16:11:44 -08001239 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001240}
1241
Doug Zongker512536a2010-02-17 16:11:44 -08001242// Take a sha-1 digest and return it as a newly-allocated hex string.
1243static char* PrintSha1(uint8_t* digest) {
1244 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
1245 int i;
1246 const char* alphabet = "0123456789abcdef";
1247 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
1248 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
1249 buffer[i*2+1] = alphabet[digest[i] & 0xf];
1250 }
1251 buffer[i*2] = '\0';
1252 return buffer;
1253}
1254
1255// sha1_check(data)
1256// to return the sha1 of the data (given in the format returned by
1257// read_file).
1258//
1259// sha1_check(data, sha1_hex, [sha1_hex, ...])
1260// returns the sha1 of the file if it matches any of the hex
1261// strings passed, or "" if it does not equal any of them.
1262//
1263Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1264 if (argc < 1) {
1265 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1266 }
1267
1268 Value** args = ReadValueVarArgs(state, argc, argv);
1269 if (args == NULL) {
1270 return NULL;
1271 }
1272
1273 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001274 return StringValue(strdup(""));
1275 }
1276 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001277 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001278 FreeValue(args[0]);
1279
1280 if (argc == 1) {
1281 return StringValue(PrintSha1(digest));
1282 }
1283
1284 int i;
1285 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1286 for (i = 1; i < argc; ++i) {
1287 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001288 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001289 name, i);
1290 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1291 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001292 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1293 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001294 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1295 break;
1296 }
1297 FreeValue(args[i]);
1298 }
1299 if (i >= argc) {
1300 // Didn't match any of the hex strings; return false.
1301 return StringValue(strdup(""));
1302 }
1303 // Found a match; free all the remaining arguments and return the
1304 // matched one.
1305 int j;
1306 for (j = i+1; j < argc; ++j) {
1307 FreeValue(args[j]);
1308 }
1309 return args[i];
1310}
1311
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001312// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001313// is actually a FileContents*).
1314Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1315 if (argc != 1) {
1316 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1317 }
1318 char* filename;
1319 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1320
1321 Value* v = malloc(sizeof(Value));
1322 v->type = VAL_BLOB;
1323
1324 FileContents fc;
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001325 if (LoadFileContents(filename, &fc, RETOUCH_DONT_MASK) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001326 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001327 v->size = -1;
1328 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001329 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001330 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001331 }
1332
1333 v->size = fc.size;
1334 v->data = (char*)fc.data;
1335
1336 free(filename);
1337 return v;
1338}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001339
Doug Zongkerc87bab12013-11-25 13:53:25 -08001340// Immediately reboot the device. Recovery is not finished normally,
1341// so if you reboot into recovery it will re-start applying the
1342// current package (because nothing has cleared the copy of the
1343// arguments stored in the BCB).
1344//
1345// The argument is the partition name passed to the android reboot
1346// property. It can be "recovery" to boot from the recovery
1347// partition, or "" (empty string) to boot from the regular boot
1348// partition.
1349Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1350 if (argc != 2) {
1351 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1352 }
1353
1354 char* filename;
1355 char* property;
1356 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1357
1358 char buffer[80];
1359
1360 // zero out the 'command' field of the bootloader message.
1361 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1362 FILE* f = fopen(filename, "r+b");
1363 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1364 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1365 fclose(f);
1366 free(filename);
1367
1368 strcpy(buffer, "reboot,");
1369 if (property != NULL) {
1370 strncat(buffer, property, sizeof(buffer)-10);
1371 }
1372
1373 property_set(ANDROID_RB_PROPERTY, buffer);
1374
1375 sleep(5);
1376 free(property);
1377 ErrorAbort(state, "%s() failed to reboot", name);
1378 return NULL;
1379}
1380
1381// Store a string value somewhere that future invocations of recovery
1382// can access it. This value is called the "stage" and can be used to
1383// drive packages that need to do reboots in the middle of
1384// installation and keep track of where they are in the multi-stage
1385// install.
1386//
1387// The first argument is the block device for the misc partition
1388// ("/misc" in the fstab), which is where this value is stored. The
1389// second argument is the string to store; it should not exceed 31
1390// bytes.
1391Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1392 if (argc != 2) {
1393 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1394 }
1395
1396 char* filename;
1397 char* stagestr;
1398 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1399
1400 // Store this value in the misc partition, immediately after the
1401 // bootloader message that the main recovery uses to save its
1402 // arguments in case of the device restarting midway through
1403 // package installation.
1404 FILE* f = fopen(filename, "r+b");
1405 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1406 int to_write = strlen(stagestr)+1;
1407 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1408 if (to_write > max_size) {
1409 to_write = max_size;
1410 stagestr[max_size-1] = 0;
1411 }
1412 fwrite(stagestr, to_write, 1, f);
1413 fclose(f);
1414
1415 free(stagestr);
1416 return StringValue(filename);
1417}
1418
1419// Return the value most recently saved with SetStageFn. The argument
1420// is the block device for the misc partition.
1421Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1422 if (argc != 2) {
1423 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1424 }
1425
1426 char* filename;
1427 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1428
1429 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1430 FILE* f = fopen(filename, "rb");
1431 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1432 fread(buffer, sizeof(buffer), 1, f);
1433 fclose(f);
1434 buffer[sizeof(buffer)-1] = '\0';
1435
1436 return StringValue(strdup(buffer));
1437}
1438
Doug Zongker9931f7f2009-06-10 14:11:53 -07001439void RegisterInstallFunctions() {
1440 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001441 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001442 RegisterFunction("unmount", UnmountFn);
1443 RegisterFunction("format", FormatFn);
1444 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001445 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001446 RegisterFunction("delete", DeleteFn);
1447 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001448 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1449 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001450 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001451
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001452 // Usage:
1453 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1454 // Example:
1455 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1456 RegisterFunction("set_metadata", SetMetadataFn);
1457
1458 // Usage:
1459 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1460 // Example:
1461 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1462 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1463
Doug Zongker8edb00c2009-06-11 17:21:44 -07001464 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001465 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001466 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001467
1468 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001469 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1470 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001471
Doug Zongker512536a2010-02-17 16:11:44 -08001472 RegisterFunction("read_file", ReadFileFn);
1473 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001474 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001475
Doug Zongkerd0181b82011-10-19 10:51:12 -07001476 RegisterFunction("wipe_cache", WipeCacheFn);
1477
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001478 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001479
1480 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001481
1482 RegisterFunction("reboot_now", RebootNowFn);
1483 RegisterFunction("get_stage", GetStageFn);
1484 RegisterFunction("set_stage", SetStageFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001485}