blob: 6a8a6b3fcb3f99cc79cf00c1e4da45486a46ecd8 [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
Michael Rungece7ca712013-11-06 17:42:20 -0800288Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
289 char* result = NULL;
290 if (argc != 2) {
291 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
292 }
293
294 char* src_name;
295 char* dst_name;
296
297 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
298 return NULL;
299 }
300 if (strlen(src_name) == 0) {
301 ErrorAbort(state, "src_name argument to %s() can't be empty", name);
302 goto done;
303 }
304 if (strlen(dst_name) == 0) {
305 ErrorAbort(state, "dst_name argument to %s() can't be empty",
306 name);
307 goto done;
308 }
309
310 if (rename(src_name, dst_name) != 0) {
311 ErrorAbort(state, "Rename of %s() to %s() failed, error %s()",
312 src_name, dst_name, strerror(errno));
313 } else {
314 result = dst_name;
315 }
316
317done:
318 free(src_name);
319 if (result != dst_name) free(dst_name);
320 return StringValue(result);
321}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700322
Doug Zongker512536a2010-02-17 16:11:44 -0800323Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700324 char** paths = malloc(argc * sizeof(char*));
325 int i;
326 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700327 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700328 if (paths[i] == NULL) {
329 int j;
330 for (j = 0; j < i; ++i) {
331 free(paths[j]);
332 }
333 free(paths);
334 return NULL;
335 }
336 }
337
338 bool recursive = (strcmp(name, "delete_recursive") == 0);
339
340 int success = 0;
341 for (i = 0; i < argc; ++i) {
342 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
343 ++success;
344 free(paths[i]);
345 }
346 free(paths);
347
348 char buffer[10];
349 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800350 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700351}
352
Doug Zongker8edb00c2009-06-11 17:21:44 -0700353
Doug Zongker512536a2010-02-17 16:11:44 -0800354Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700355 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700356 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700357 }
358 char* frac_str;
359 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700360 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700361 return NULL;
362 }
363
364 double frac = strtod(frac_str, NULL);
365 int sec = strtol(sec_str, NULL, 10);
366
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700367 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700368 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
369
Doug Zongker9931f7f2009-06-10 14:11:53 -0700370 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800371 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700372}
373
Doug Zongker512536a2010-02-17 16:11:44 -0800374Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700375 if (argc != 1) {
376 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
377 }
378 char* frac_str;
379 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
380 return NULL;
381 }
382
383 double frac = strtod(frac_str, NULL);
384
385 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
386 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
387
Doug Zongker512536a2010-02-17 16:11:44 -0800388 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700389}
390
Doug Zongker8edb00c2009-06-11 17:21:44 -0700391// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800392Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700393 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700394 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700395 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700396 }
397 char* zip_path;
398 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700399 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700400
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700401 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700402
403 // To create a consistent system image, never use the clock for timestamps.
404 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
405
406 bool success = mzExtractRecursive(za, zip_path, dest_path,
407 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500408 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700409 free(zip_path);
410 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800411 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700412}
413
Doug Zongker8edb00c2009-06-11 17:21:44 -0700414
415// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800416// or
417// package_extract_file(package_path)
418// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800419// function (the char* returned is actually a FileContents*).
420Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700421 int argc, Expr* argv[]) {
Doug Zongker6aece332010-02-01 14:40:12 -0800422 if (argc != 1 && argc != 2) {
423 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
424 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700425 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700426 bool success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800427 if (argc == 2) {
428 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700429
Doug Zongker6aece332010-02-01 14:40:12 -0800430 char* zip_path;
431 char* dest_path;
432 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
433
434 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
435 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
436 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700437 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800438 goto done2;
439 }
440
441 FILE* f = fopen(dest_path, "wb");
442 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700443 printf("%s: can't open %s for write: %s\n",
Doug Zongker6aece332010-02-01 14:40:12 -0800444 name, dest_path, strerror(errno));
445 goto done2;
446 }
447 success = mzExtractZipEntryToFile(za, entry, fileno(f));
448 fclose(f);
449
450 done2:
451 free(zip_path);
452 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800453 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800454 } else {
455 // The one-argument version returns the contents of the file
456 // as the result.
457
458 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800459 Value* v = malloc(sizeof(Value));
460 v->type = VAL_BLOB;
461 v->size = -1;
462 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800463
464 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
465
466 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
467 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
468 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700469 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800470 goto done1;
471 }
472
Doug Zongker512536a2010-02-17 16:11:44 -0800473 v->size = mzGetZipEntryUncompLen(entry);
474 v->data = malloc(v->size);
475 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700476 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800477 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800478 goto done1;
479 }
480
Doug Zongker512536a2010-02-17 16:11:44 -0800481 success = mzExtractZipEntryToBuffer(za, entry,
482 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800483
484 done1:
485 free(zip_path);
486 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800487 free(v->data);
488 v->data = NULL;
489 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800490 }
Doug Zongker512536a2010-02-17 16:11:44 -0800491 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700492 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700493}
494
Doug Zongkera23075f2012-08-06 16:19:09 -0700495// Create all parent directories of name, if necessary.
496static int make_parents(char* name) {
497 char* p;
498 for (p = name + (strlen(name)-1); p > name; --p) {
499 if (*p != '/') continue;
500 *p = '\0';
501 if (make_parents(name) < 0) return -1;
502 int result = mkdir(name, 0700);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700503 if (result == 0) printf("symlink(): created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700504 *p = '/';
505 if (result == 0 || errno == EEXIST) {
506 // successfully created or already existed; we're done
507 return 0;
508 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700509 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700510 return -1;
511 }
512 }
513 return 0;
514}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700515
Doug Zongker9931f7f2009-06-10 14:11:53 -0700516// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700517// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800518Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700519 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700520 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700521 }
522 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700523 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700524 if (target == NULL) return NULL;
525
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700526 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700527 if (srcs == NULL) {
528 free(target);
529 return NULL;
530 }
531
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700532 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700533 int i;
534 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700535 if (unlink(srcs[i]) < 0) {
536 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700537 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700538 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700539 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700540 }
541 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700542 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700543 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700544 name, srcs[i], target);
545 ++bad;
546 }
Doug Zongker60babf82009-09-18 15:11:24 -0700547 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700548 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700549 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700550 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700551 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700552 free(srcs[i]);
553 }
554 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700555 if (bad) {
556 return ErrorAbort(state, "%s: some symlinks failed", name);
557 }
Doug Zongker512536a2010-02-17 16:11:44 -0800558 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700559}
560
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700561struct perm_parsed_args {
562 bool has_uid;
563 uid_t uid;
564 bool has_gid;
565 gid_t gid;
566 bool has_mode;
567 mode_t mode;
568 bool has_fmode;
569 mode_t fmode;
570 bool has_dmode;
571 mode_t dmode;
572 bool has_selabel;
573 char* selabel;
574 bool has_capabilities;
575 uint64_t capabilities;
576};
577
578static struct perm_parsed_args ParsePermArgs(int argc, char** args) {
579 int i;
580 struct perm_parsed_args parsed;
581 int bad = 0;
582 static int max_warnings = 20;
583
584 memset(&parsed, 0, sizeof(parsed));
585
586 for (i = 1; i < argc; i += 2) {
587 if (strcmp("uid", args[i]) == 0) {
588 int64_t uid;
589 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
590 parsed.uid = uid;
591 parsed.has_uid = true;
592 } else {
593 printf("ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
594 bad++;
595 }
596 continue;
597 }
598 if (strcmp("gid", args[i]) == 0) {
599 int64_t gid;
600 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
601 parsed.gid = gid;
602 parsed.has_gid = true;
603 } else {
604 printf("ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
605 bad++;
606 }
607 continue;
608 }
609 if (strcmp("mode", args[i]) == 0) {
610 int32_t mode;
611 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
612 parsed.mode = mode;
613 parsed.has_mode = true;
614 } else {
615 printf("ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
616 bad++;
617 }
618 continue;
619 }
620 if (strcmp("dmode", args[i]) == 0) {
621 int32_t mode;
622 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
623 parsed.dmode = mode;
624 parsed.has_dmode = true;
625 } else {
626 printf("ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
627 bad++;
628 }
629 continue;
630 }
631 if (strcmp("fmode", args[i]) == 0) {
632 int32_t mode;
633 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
634 parsed.fmode = mode;
635 parsed.has_fmode = true;
636 } else {
637 printf("ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
638 bad++;
639 }
640 continue;
641 }
642 if (strcmp("capabilities", args[i]) == 0) {
643 int64_t capabilities;
644 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
645 parsed.capabilities = capabilities;
646 parsed.has_capabilities = true;
647 } else {
648 printf("ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
649 bad++;
650 }
651 continue;
652 }
653 if (strcmp("selabel", args[i]) == 0) {
654 if (args[i+1][0] != '\0') {
655 parsed.selabel = args[i+1];
656 parsed.has_selabel = true;
657 } else {
658 printf("ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
659 bad++;
660 }
661 continue;
662 }
663 if (max_warnings != 0) {
664 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
665 max_warnings--;
666 if (max_warnings == 0) {
667 printf("ParsedPermArgs: suppressing further warnings\n");
668 }
669 }
670 }
671 return parsed;
672}
673
674static int ApplyParsedPerms(
675 const char* filename,
676 const struct stat *statptr,
677 struct perm_parsed_args parsed)
678{
679 int bad = 0;
680
Nick Kraleviche4612512013-09-10 15:34:19 -0700681 /* ignore symlinks */
682 if (S_ISLNK(statptr->st_mode)) {
683 return 0;
684 }
685
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700686 if (parsed.has_uid) {
687 if (chown(filename, parsed.uid, -1) < 0) {
688 printf("ApplyParsedPerms: chown of %s to %d failed: %s\n",
689 filename, parsed.uid, strerror(errno));
690 bad++;
691 }
692 }
693
694 if (parsed.has_gid) {
695 if (chown(filename, -1, parsed.gid) < 0) {
696 printf("ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
697 filename, parsed.gid, strerror(errno));
698 bad++;
699 }
700 }
701
702 if (parsed.has_mode) {
703 if (chmod(filename, parsed.mode) < 0) {
704 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
705 filename, parsed.mode, strerror(errno));
706 bad++;
707 }
708 }
709
710 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
711 if (chmod(filename, parsed.dmode) < 0) {
712 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
713 filename, parsed.dmode, strerror(errno));
714 bad++;
715 }
716 }
717
718 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
719 if (chmod(filename, parsed.fmode) < 0) {
720 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
721 filename, parsed.fmode, strerror(errno));
722 bad++;
723 }
724 }
725
726 if (parsed.has_selabel) {
727 // TODO: Don't silently ignore ENOTSUP
728 if (lsetfilecon(filename, parsed.selabel) && (errno != ENOTSUP)) {
729 printf("ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
730 filename, parsed.selabel, strerror(errno));
731 bad++;
732 }
733 }
734
735 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
736 if (parsed.capabilities == 0) {
737 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
738 // Report failure unless it's ENODATA (attribute not set)
739 printf("ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
740 filename, parsed.capabilities, strerror(errno));
741 bad++;
742 }
743 } else {
744 struct vfs_cap_data cap_data;
745 memset(&cap_data, 0, sizeof(cap_data));
746 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
747 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
748 cap_data.data[0].inheritable = 0;
749 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
750 cap_data.data[1].inheritable = 0;
751 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
752 printf("ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
753 filename, parsed.capabilities, strerror(errno));
754 bad++;
755 }
756 }
757 }
758
759 return bad;
760}
761
762// nftw doesn't allow us to pass along context, so we need to use
763// global variables. *sigh*
764static struct perm_parsed_args recursive_parsed_args;
765
766static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
767 int fileflags, struct FTW *pfwt) {
768 return ApplyParsedPerms(filename, statptr, recursive_parsed_args);
769}
770
771static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
772 int i;
773 int bad = 0;
774 static int nwarnings = 0;
775 struct stat sb;
776 Value* result = NULL;
777
778 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
779
780 if ((argc % 2) != 1) {
781 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d",
782 name, argc);
783 }
784
785 char** args = ReadVarArgs(state, argc, argv);
786 if (args == NULL) return NULL;
787
788 if (lstat(args[0], &sb) == -1) {
789 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
790 goto done;
791 }
792
793 struct perm_parsed_args parsed = ParsePermArgs(argc, args);
794
795 if (recursive) {
796 recursive_parsed_args = parsed;
797 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
798 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
799 } else {
800 bad += ApplyParsedPerms(args[0], &sb, parsed);
801 }
802
803done:
804 for (i = 0; i < argc; ++i) {
805 free(args[i]);
806 }
807 free(args);
808
809 if (result != NULL) {
810 return result;
811 }
812
813 if (bad > 0) {
814 return ErrorAbort(state, "%s: some changes failed", name);
815 }
816
817 return StringValue(strdup(""));
818}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700819
Doug Zongker512536a2010-02-17 16:11:44 -0800820Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700821 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700822 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700823 }
824 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700825 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700826 if (key == NULL) return NULL;
827
828 char value[PROPERTY_VALUE_MAX];
829 property_get(key, value, "");
830 free(key);
831
Doug Zongker512536a2010-02-17 16:11:44 -0800832 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700833}
834
835
Doug Zongker47cace92009-06-18 10:11:50 -0700836// file_getprop(file, key)
837//
838// interprets 'file' as a getprop-style file (key=value pairs, one
839// per line, # comment lines and blank lines okay), and returns the value
840// for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800841Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700842 char* result = NULL;
843 char* buffer = NULL;
844 char* filename;
845 char* key;
846 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
847 return NULL;
848 }
849
850 struct stat st;
851 if (stat(filename, &st) < 0) {
852 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
853 name, filename, strerror(errno));
854 goto done;
855 }
856
857#define MAX_FILE_GETPROP_SIZE 65536
858
859 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
860 ErrorAbort(state, "%s too large for %s (max %d)",
861 filename, name, MAX_FILE_GETPROP_SIZE);
862 goto done;
863 }
864
865 buffer = malloc(st.st_size+1);
866 if (buffer == NULL) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700867 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700868 goto done;
869 }
870
871 FILE* f = fopen(filename, "rb");
872 if (f == NULL) {
873 ErrorAbort(state, "%s: failed to open %s: %s",
874 name, filename, strerror(errno));
875 goto done;
876 }
877
878 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700879 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Doug Zongker47cace92009-06-18 10:11:50 -0700880 name, st.st_size+1, filename);
881 fclose(f);
882 goto done;
883 }
884 buffer[st.st_size] = '\0';
885
886 fclose(f);
887
888 char* line = strtok(buffer, "\n");
889 do {
890 // skip whitespace at start of line
891 while (*line && isspace(*line)) ++line;
892
893 // comment or blank line: skip to next line
894 if (*line == '\0' || *line == '#') continue;
895
896 char* equal = strchr(line, '=');
897 if (equal == NULL) {
898 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
899 name, line, filename);
900 goto done;
901 }
902
903 // trim whitespace between key and '='
904 char* key_end = equal-1;
905 while (key_end > line && isspace(*key_end)) --key_end;
906 key_end[1] = '\0';
907
908 // not the key we're looking for
909 if (strcmp(key, line) != 0) continue;
910
911 // skip whitespace after the '=' to the start of the value
912 char* val_start = equal+1;
913 while(*val_start && isspace(*val_start)) ++val_start;
914
915 // trim trailing whitespace
916 char* val_end = val_start + strlen(val_start)-1;
917 while (val_end > val_start && isspace(*val_end)) --val_end;
918 val_end[1] = '\0';
919
920 result = strdup(val_start);
921 break;
922
923 } while ((line = strtok(NULL, "\n")));
924
925 if (result == NULL) result = strdup("");
926
927 done:
928 free(filename);
929 free(key);
930 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800931 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -0700932}
933
934
Doug Zongker8edb00c2009-06-11 17:21:44 -0700935static bool write_raw_image_cb(const unsigned char* data,
936 int data_len, void* ctx) {
937 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
938 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700939 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700940 return false;
941}
942
Doug Zongker179b2d92011-04-12 15:49:04 -0700943// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -0800944Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700945 char* result = NULL;
946
Doug Zongker179b2d92011-04-12 15:49:04 -0700947 Value* partition_value;
948 Value* contents;
949 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700950 return NULL;
951 }
952
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700953 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -0700954 if (partition_value->type != VAL_STRING) {
955 ErrorAbort(state, "partition argument to %s must be string", name);
956 goto done;
957 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700958 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700959 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700960 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700961 goto done;
962 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700963 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700964 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700965 goto done;
966 }
967
968 mtd_scan_partitions();
969 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
970 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700971 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700972 result = strdup("");
973 goto done;
974 }
975
976 MtdWriteContext* ctx = mtd_write_partition(mtd);
977 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700978 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -0700979 name, partition);
980 result = strdup("");
981 goto done;
982 }
983
984 bool success;
985
Doug Zongker179b2d92011-04-12 15:49:04 -0700986 if (contents->type == VAL_STRING) {
987 // we're given a filename as the contents
988 char* filename = contents->data;
989 FILE* f = fopen(filename, "rb");
990 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700991 printf("%s: can't open %s: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -0700992 name, filename, strerror(errno));
993 result = strdup("");
994 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700995 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700996
997 success = true;
998 char* buffer = malloc(BUFSIZ);
999 int read;
1000 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
1001 int wrote = mtd_write_data(ctx, buffer, read);
1002 success = success && (wrote == read);
1003 }
1004 free(buffer);
1005 fclose(f);
1006 } else {
1007 // we're given a blob as the contents
1008 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1009 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001010 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001011 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001012 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001013 partition, strerror(errno));
1014 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001015
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001016 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001017 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001018 }
1019 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001020 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001021 }
1022
Doug Zongker179b2d92011-04-12 15:49:04 -07001023 printf("%s %s partition\n",
1024 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001025
1026 result = success ? partition : strdup("");
1027
1028done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001029 if (result != partition) FreeValue(partition_value);
1030 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001031 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001032}
1033
Doug Zongker8edb00c2009-06-11 17:21:44 -07001034// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001035Value* ApplyPatchSpaceFn(const char* name, State* state,
1036 int argc, Expr* argv[]) {
1037 char* bytes_str;
1038 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1039 return NULL;
1040 }
1041
1042 char* endptr;
1043 size_t bytes = strtol(bytes_str, &endptr, 10);
1044 if (bytes == 0 && endptr == bytes_str) {
1045 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
1046 name, bytes_str);
1047 free(bytes_str);
1048 return NULL;
1049 }
1050
1051 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1052}
1053
1054
1055// apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1_1, patch_1, ...)
Doug Zongker512536a2010-02-17 16:11:44 -08001056Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001057 if (argc < 6 || (argc % 2) == 1) {
1058 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1059 "even number, got %d",
1060 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001061 }
1062
Doug Zongkerc4351c72010-02-22 14:46:32 -08001063 char* source_filename;
1064 char* target_filename;
1065 char* target_sha1;
1066 char* target_size_str;
1067 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1068 &target_sha1, &target_size_str) < 0) {
1069 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001070 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001071
Doug Zongkerc4351c72010-02-22 14:46:32 -08001072 char* endptr;
1073 size_t target_size = strtol(target_size_str, &endptr, 10);
1074 if (target_size == 0 && endptr == target_size_str) {
1075 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1076 name, target_size_str);
1077 free(source_filename);
1078 free(target_filename);
1079 free(target_sha1);
1080 free(target_size_str);
1081 return NULL;
1082 }
1083
1084 int patchcount = (argc-4) / 2;
1085 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001086
1087 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001088 for (i = 0; i < patchcount; ++i) {
1089 if (patches[i*2]->type != VAL_STRING) {
1090 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1091 break;
1092 }
1093 if (patches[i*2+1]->type != VAL_BLOB) {
1094 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1095 break;
1096 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001097 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001098 if (i != patchcount) {
1099 for (i = 0; i < patchcount*2; ++i) {
1100 FreeValue(patches[i]);
1101 }
1102 free(patches);
1103 return NULL;
1104 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001105
Doug Zongkerc4351c72010-02-22 14:46:32 -08001106 char** patch_sha_str = malloc(patchcount * sizeof(char*));
1107 for (i = 0; i < patchcount; ++i) {
1108 patch_sha_str[i] = patches[i*2]->data;
1109 patches[i*2]->data = NULL;
1110 FreeValue(patches[i*2]);
1111 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001112 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001113
1114 int result = applypatch(source_filename, target_filename,
1115 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001116 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001117
1118 for (i = 0; i < patchcount; ++i) {
1119 FreeValue(patches[i]);
1120 }
1121 free(patch_sha_str);
1122 free(patches);
1123
1124 return StringValue(strdup(result == 0 ? "t" : ""));
1125}
1126
1127// apply_patch_check(file, [sha1_1, ...])
1128Value* ApplyPatchCheckFn(const char* name, State* state,
1129 int argc, Expr* argv[]) {
1130 if (argc < 1) {
1131 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1132 name, argc);
1133 }
1134
1135 char* filename;
1136 if (ReadArgs(state, argv, 1, &filename) < 0) {
1137 return NULL;
1138 }
1139
1140 int patchcount = argc-1;
1141 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1142
1143 int result = applypatch_check(filename, patchcount, sha1s);
1144
1145 int i;
1146 for (i = 0; i < patchcount; ++i) {
1147 free(sha1s[i]);
1148 }
1149 free(sha1s);
1150
1151 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001152}
1153
Doug Zongker512536a2010-02-17 16:11:44 -08001154Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001155 char** args = ReadVarArgs(state, argc, argv);
1156 if (args == NULL) {
1157 return NULL;
1158 }
1159
1160 int size = 0;
1161 int i;
1162 for (i = 0; i < argc; ++i) {
1163 size += strlen(args[i]);
1164 }
1165 char* buffer = malloc(size+1);
1166 size = 0;
1167 for (i = 0; i < argc; ++i) {
1168 strcpy(buffer+size, args[i]);
1169 size += strlen(args[i]);
1170 free(args[i]);
1171 }
1172 free(args);
1173 buffer[size] = '\0';
1174
1175 char* line = strtok(buffer, "\n");
1176 while (line) {
1177 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
1178 "ui_print %s\n", line);
1179 line = strtok(NULL, "\n");
1180 }
1181 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
1182
Doug Zongker512536a2010-02-17 16:11:44 -08001183 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001184}
1185
Doug Zongkerd0181b82011-10-19 10:51:12 -07001186Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1187 if (argc != 0) {
1188 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1189 }
1190 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1191 return StringValue(strdup("t"));
1192}
1193
Doug Zongker512536a2010-02-17 16:11:44 -08001194Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001195 if (argc < 1) {
1196 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1197 }
1198 char** args = ReadVarArgs(state, argc, argv);
1199 if (args == NULL) {
1200 return NULL;
1201 }
1202
1203 char** args2 = malloc(sizeof(char*) * (argc+1));
1204 memcpy(args2, args, sizeof(char*) * argc);
1205 args2[argc] = NULL;
1206
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001207 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001208
1209 pid_t child = fork();
1210 if (child == 0) {
1211 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001212 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001213 _exit(1);
1214 }
1215 int status;
1216 waitpid(child, &status, 0);
1217 if (WIFEXITED(status)) {
1218 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001219 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001220 WEXITSTATUS(status));
1221 }
1222 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001223 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001224 WTERMSIG(status));
1225 }
1226
1227 int i;
1228 for (i = 0; i < argc; ++i) {
1229 free(args[i]);
1230 }
1231 free(args);
1232 free(args2);
1233
1234 char buffer[20];
1235 sprintf(buffer, "%d", status);
1236
Doug Zongker512536a2010-02-17 16:11:44 -08001237 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001238}
1239
Doug Zongker512536a2010-02-17 16:11:44 -08001240// Take a sha-1 digest and return it as a newly-allocated hex string.
1241static char* PrintSha1(uint8_t* digest) {
1242 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
1243 int i;
1244 const char* alphabet = "0123456789abcdef";
1245 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
1246 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
1247 buffer[i*2+1] = alphabet[digest[i] & 0xf];
1248 }
1249 buffer[i*2] = '\0';
1250 return buffer;
1251}
1252
1253// sha1_check(data)
1254// to return the sha1 of the data (given in the format returned by
1255// read_file).
1256//
1257// sha1_check(data, sha1_hex, [sha1_hex, ...])
1258// returns the sha1 of the file if it matches any of the hex
1259// strings passed, or "" if it does not equal any of them.
1260//
1261Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1262 if (argc < 1) {
1263 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1264 }
1265
1266 Value** args = ReadValueVarArgs(state, argc, argv);
1267 if (args == NULL) {
1268 return NULL;
1269 }
1270
1271 if (args[0]->size < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001272 printf("%s(): no file contents received", name);
Doug Zongker512536a2010-02-17 16:11:44 -08001273 return StringValue(strdup(""));
1274 }
1275 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001276 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001277 FreeValue(args[0]);
1278
1279 if (argc == 1) {
1280 return StringValue(PrintSha1(digest));
1281 }
1282
1283 int i;
1284 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1285 for (i = 1; i < argc; ++i) {
1286 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001287 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001288 name, i);
1289 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1290 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001291 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1292 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001293 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1294 break;
1295 }
1296 FreeValue(args[i]);
1297 }
1298 if (i >= argc) {
1299 // Didn't match any of the hex strings; return false.
1300 return StringValue(strdup(""));
1301 }
1302 // Found a match; free all the remaining arguments and return the
1303 // matched one.
1304 int j;
1305 for (j = i+1; j < argc; ++j) {
1306 FreeValue(args[j]);
1307 }
1308 return args[i];
1309}
1310
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001311// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001312// is actually a FileContents*).
1313Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1314 if (argc != 1) {
1315 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1316 }
1317 char* filename;
1318 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1319
1320 Value* v = malloc(sizeof(Value));
1321 v->type = VAL_BLOB;
1322
1323 FileContents fc;
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001324 if (LoadFileContents(filename, &fc, RETOUCH_DONT_MASK) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001325 ErrorAbort(state, "%s() loading \"%s\" failed: %s",
1326 name, filename, strerror(errno));
1327 free(filename);
1328 free(v);
1329 free(fc.data);
1330 return NULL;
1331 }
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 Zongker9931f7f2009-06-10 14:11:53 -07001340void RegisterInstallFunctions() {
1341 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001342 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001343 RegisterFunction("unmount", UnmountFn);
1344 RegisterFunction("format", FormatFn);
1345 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001346 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001347 RegisterFunction("delete", DeleteFn);
1348 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001349 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1350 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001351 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001352
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001353 // Usage:
1354 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1355 // Example:
1356 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1357 RegisterFunction("set_metadata", SetMetadataFn);
1358
1359 // Usage:
1360 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1361 // Example:
1362 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1363 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1364
Doug Zongker8edb00c2009-06-11 17:21:44 -07001365 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001366 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001367 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001368
1369 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001370 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1371 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001372
Doug Zongker512536a2010-02-17 16:11:44 -08001373 RegisterFunction("read_file", ReadFileFn);
1374 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001375 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001376
Doug Zongkerd0181b82011-10-19 10:51:12 -07001377 RegisterFunction("wipe_cache", WipeCacheFn);
1378
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001379 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001380
1381 RegisterFunction("run_program", RunProgramFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001382}