blob: 061aa7b5b7397ddbffcae0c1b72af3f1029d56b3 [file] [log] [blame]
Doug Zongker9931f7f2009-06-10 14:11:53 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Doug Zongkerfbf3c102009-06-24 09:36:20 -070017#include <ctype.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070018#include <errno.h>
19#include <stdarg.h>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070020#include <stdio.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070021#include <stdlib.h>
22#include <string.h>
23#include <sys/mount.h>
24#include <sys/stat.h>
25#include <sys/types.h>
Doug Zongkera3f89ea2009-09-10 14:10:48 -070026#include <sys/wait.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070027#include <unistd.h>
Hristo Bojinovdb314d62010-08-02 10:29:49 -070028#include <fcntl.h>
29#include <time.h>
Nick Kralevich5dbdef02013-09-07 14:41:06 -070030#include <selinux/selinux.h>
31#include <ftw.h>
32#include <sys/capability.h>
33#include <sys/xattr.h>
34#include <linux/xattr.h>
35#include <inttypes.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070036
Doug Zongker8edb00c2009-06-11 17:21:44 -070037#include "cutils/misc.h"
38#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070039#include "edify/expr.h"
Doug Zongker512536a2010-02-17 16:11:44 -080040#include "mincrypt/sha.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070041#include "minzip/DirUtil.h"
42#include "mtdutils/mounts.h"
43#include "mtdutils/mtdutils.h"
44#include "updater.h"
Doug Zongker512536a2010-02-17 16:11:44 -080045#include "applypatch/applypatch.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070046
Doug Zongker3d177d02010-07-01 09:18:44 -070047#ifdef USE_EXT4
48#include "make_ext4fs.h"
49#endif
50
51// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -070052//
Doug Zongker3d177d02010-07-01 09:18:44 -070053// fs_type="yaffs2" partition_type="MTD" location=partition
54// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -080055Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070056 char* result = NULL;
Doug Zongker3d177d02010-07-01 09:18:44 -070057 if (argc != 4) {
58 return ErrorAbort(state, "%s() expects 4 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -070059 }
Doug Zongker3d177d02010-07-01 09:18:44 -070060 char* fs_type;
61 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -070062 char* location;
63 char* mount_point;
Doug Zongker3d177d02010-07-01 09:18:44 -070064 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
65 &location, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070066 return NULL;
67 }
68
Doug Zongker3d177d02010-07-01 09:18:44 -070069 if (strlen(fs_type) == 0) {
70 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
71 goto done;
72 }
73 if (strlen(partition_type) == 0) {
74 ErrorAbort(state, "partition_type argument to %s() can't be empty",
75 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070076 goto done;
77 }
78 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070079 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070080 goto done;
81 }
82 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070083 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070084 goto done;
85 }
86
Stephen Smalley779701d2012-02-09 14:13:23 -050087 char *secontext = NULL;
88
89 if (sehandle) {
90 selabel_lookup(sehandle, &secontext, mount_point, 0755);
91 setfscreatecon(secontext);
92 }
Stephen Smalley779701d2012-02-09 14:13:23 -050093
Doug Zongker9931f7f2009-06-10 14:11:53 -070094 mkdir(mount_point, 0755);
95
Stephen Smalley779701d2012-02-09 14:13:23 -050096 if (secontext) {
97 freecon(secontext);
98 setfscreatecon(NULL);
99 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500100
Doug Zongker3d177d02010-07-01 09:18:44 -0700101 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700102 mtd_scan_partitions();
103 const MtdPartition* mtd;
104 mtd = mtd_find_partition_by_name(location);
105 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700106 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700107 name, location);
108 result = strdup("");
109 goto done;
110 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700111 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700112 printf("mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700113 location, strerror(errno));
114 result = strdup("");
115 goto done;
116 }
117 result = mount_point;
118 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700119 if (mount(location, mount_point, fs_type,
Doug Zongker9931f7f2009-06-10 14:11:53 -0700120 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700121 printf("%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700122 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700123 result = strdup("");
124 } else {
125 result = mount_point;
126 }
127 }
128
129done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700130 free(fs_type);
131 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700132 free(location);
133 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800134 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700135}
136
Doug Zongker8edb00c2009-06-11 17:21:44 -0700137
138// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800139Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700140 char* result = NULL;
141 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700142 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700143 }
144 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700145 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700146 return NULL;
147 }
148 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700149 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700150 goto done;
151 }
152
153 scan_mounted_volumes();
154 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
155 if (vol == NULL) {
156 result = strdup("");
157 } else {
158 result = mount_point;
159 }
160
161done:
162 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800163 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700164}
165
166
Doug Zongker512536a2010-02-17 16:11:44 -0800167Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700168 char* result = NULL;
169 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700170 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700171 }
172 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700173 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700174 return NULL;
175 }
176 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700177 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700178 goto done;
179 }
180
181 scan_mounted_volumes();
182 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
183 if (vol == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700184 printf("unmount of %s failed; no such volume\n", mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700185 result = strdup("");
186 } else {
187 unmount_mounted_volume(vol);
188 result = mount_point;
189 }
190
191done:
192 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800193 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700194}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700195
196
Stephen Smalley779701d2012-02-09 14:13:23 -0500197// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700198//
Stephen Smalley779701d2012-02-09 14:13:23 -0500199// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
200// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800201// if fs_size == 0, then make_ext4fs uses the entire partition.
202// if fs_size > 0, that is the size to use
203// if fs_size < 0, then reserve that many bytes at the end of the partition
Doug Zongker512536a2010-02-17 16:11:44 -0800204Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700205 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400206 if (argc != 5) {
207 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700208 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700209 char* fs_type;
210 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700211 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800212 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400213 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500214
Stephen Smalley779701d2012-02-09 14:13:23 -0500215 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
216 return NULL;
217 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700218
Doug Zongker3d177d02010-07-01 09:18:44 -0700219 if (strlen(fs_type) == 0) {
220 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
221 goto done;
222 }
223 if (strlen(partition_type) == 0) {
224 ErrorAbort(state, "partition_type argument to %s() can't be empty",
225 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700226 goto done;
227 }
228 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700229 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700230 goto done;
231 }
232
Stephen Smalley516e4e22012-04-03 13:35:11 -0400233 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500234 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
235 goto done;
236 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500237
Doug Zongker3d177d02010-07-01 09:18:44 -0700238 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700239 mtd_scan_partitions();
240 const MtdPartition* mtd = mtd_find_partition_by_name(location);
241 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700242 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700243 name, location);
244 result = strdup("");
245 goto done;
246 }
247 MtdWriteContext* ctx = mtd_write_partition(mtd);
248 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700249 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700250 result = strdup("");
251 goto done;
252 }
253 if (mtd_erase_blocks(ctx, -1) == -1) {
254 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700255 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700256 result = strdup("");
257 goto done;
258 }
259 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700260 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700261 result = strdup("");
262 goto done;
263 }
264 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700265#ifdef USE_EXT4
266 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500267 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700268 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700269 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700270 name, status, location);
271 result = strdup("");
272 goto done;
273 }
274 result = location;
275#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700276 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700277 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700278 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700279 }
280
281done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700282 free(fs_type);
283 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700284 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800285 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700286}
287
Doug Zongker8edb00c2009-06-11 17:21:44 -0700288
Doug Zongker512536a2010-02-17 16:11:44 -0800289Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700290 char** paths = malloc(argc * sizeof(char*));
291 int i;
292 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700293 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700294 if (paths[i] == NULL) {
295 int j;
296 for (j = 0; j < i; ++i) {
297 free(paths[j]);
298 }
299 free(paths);
300 return NULL;
301 }
302 }
303
304 bool recursive = (strcmp(name, "delete_recursive") == 0);
305
306 int success = 0;
307 for (i = 0; i < argc; ++i) {
308 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
309 ++success;
310 free(paths[i]);
311 }
312 free(paths);
313
314 char buffer[10];
315 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800316 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700317}
318
Doug Zongker8edb00c2009-06-11 17:21:44 -0700319
Doug Zongker512536a2010-02-17 16:11:44 -0800320Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700321 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700322 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700323 }
324 char* frac_str;
325 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700326 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700327 return NULL;
328 }
329
330 double frac = strtod(frac_str, NULL);
331 int sec = strtol(sec_str, NULL, 10);
332
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700333 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700334 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
335
Doug Zongker9931f7f2009-06-10 14:11:53 -0700336 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800337 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700338}
339
Doug Zongker512536a2010-02-17 16:11:44 -0800340Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700341 if (argc != 1) {
342 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
343 }
344 char* frac_str;
345 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
346 return NULL;
347 }
348
349 double frac = strtod(frac_str, NULL);
350
351 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
352 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
353
Doug Zongker512536a2010-02-17 16:11:44 -0800354 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700355}
356
Doug Zongker8edb00c2009-06-11 17:21:44 -0700357// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800358Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700359 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700360 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700361 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700362 }
363 char* zip_path;
364 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700365 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700366
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700367 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700368
369 // To create a consistent system image, never use the clock for timestamps.
370 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
371
372 bool success = mzExtractRecursive(za, zip_path, dest_path,
373 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500374 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700375 free(zip_path);
376 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800377 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700378}
379
Doug Zongker8edb00c2009-06-11 17:21:44 -0700380
381// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800382// or
383// package_extract_file(package_path)
384// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800385// function (the char* returned is actually a FileContents*).
386Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700387 int argc, Expr* argv[]) {
Doug Zongker6aece332010-02-01 14:40:12 -0800388 if (argc != 1 && argc != 2) {
389 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
390 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700391 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700392 bool success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800393 if (argc == 2) {
394 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700395
Doug Zongker6aece332010-02-01 14:40:12 -0800396 char* zip_path;
397 char* dest_path;
398 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
399
400 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
401 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
402 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700403 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800404 goto done2;
405 }
406
407 FILE* f = fopen(dest_path, "wb");
408 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700409 printf("%s: can't open %s for write: %s\n",
Doug Zongker6aece332010-02-01 14:40:12 -0800410 name, dest_path, strerror(errno));
411 goto done2;
412 }
413 success = mzExtractZipEntryToFile(za, entry, fileno(f));
414 fclose(f);
415
416 done2:
417 free(zip_path);
418 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800419 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800420 } else {
421 // The one-argument version returns the contents of the file
422 // as the result.
423
424 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800425 Value* v = malloc(sizeof(Value));
426 v->type = VAL_BLOB;
427 v->size = -1;
428 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800429
430 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
431
432 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
433 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
434 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700435 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800436 goto done1;
437 }
438
Doug Zongker512536a2010-02-17 16:11:44 -0800439 v->size = mzGetZipEntryUncompLen(entry);
440 v->data = malloc(v->size);
441 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700442 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800443 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800444 goto done1;
445 }
446
Doug Zongker512536a2010-02-17 16:11:44 -0800447 success = mzExtractZipEntryToBuffer(za, entry,
448 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800449
450 done1:
451 free(zip_path);
452 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800453 free(v->data);
454 v->data = NULL;
455 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800456 }
Doug Zongker512536a2010-02-17 16:11:44 -0800457 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700458 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700459}
460
Doug Zongkera23075f2012-08-06 16:19:09 -0700461// Create all parent directories of name, if necessary.
462static int make_parents(char* name) {
463 char* p;
464 for (p = name + (strlen(name)-1); p > name; --p) {
465 if (*p != '/') continue;
466 *p = '\0';
467 if (make_parents(name) < 0) return -1;
468 int result = mkdir(name, 0700);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700469 if (result == 0) printf("symlink(): created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700470 *p = '/';
471 if (result == 0 || errno == EEXIST) {
472 // successfully created or already existed; we're done
473 return 0;
474 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700475 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700476 return -1;
477 }
478 }
479 return 0;
480}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700481
Doug Zongker9931f7f2009-06-10 14:11:53 -0700482// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700483// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800484Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700485 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700486 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700487 }
488 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700489 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700490 if (target == NULL) return NULL;
491
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700492 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700493 if (srcs == NULL) {
494 free(target);
495 return NULL;
496 }
497
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700498 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700499 int i;
500 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700501 if (unlink(srcs[i]) < 0) {
502 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700503 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700504 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700505 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700506 }
507 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700508 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700509 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700510 name, srcs[i], target);
511 ++bad;
512 }
Doug Zongker60babf82009-09-18 15:11:24 -0700513 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700514 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700515 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700516 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700517 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700518 free(srcs[i]);
519 }
520 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700521 if (bad) {
522 return ErrorAbort(state, "%s: some symlinks failed", name);
523 }
Doug Zongker512536a2010-02-17 16:11:44 -0800524 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700525}
526
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700527struct perm_parsed_args {
528 bool has_uid;
529 uid_t uid;
530 bool has_gid;
531 gid_t gid;
532 bool has_mode;
533 mode_t mode;
534 bool has_fmode;
535 mode_t fmode;
536 bool has_dmode;
537 mode_t dmode;
538 bool has_selabel;
539 char* selabel;
540 bool has_capabilities;
541 uint64_t capabilities;
542};
543
544static struct perm_parsed_args ParsePermArgs(int argc, char** args) {
545 int i;
546 struct perm_parsed_args parsed;
547 int bad = 0;
548 static int max_warnings = 20;
549
550 memset(&parsed, 0, sizeof(parsed));
551
552 for (i = 1; i < argc; i += 2) {
553 if (strcmp("uid", args[i]) == 0) {
554 int64_t uid;
555 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
556 parsed.uid = uid;
557 parsed.has_uid = true;
558 } else {
559 printf("ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
560 bad++;
561 }
562 continue;
563 }
564 if (strcmp("gid", args[i]) == 0) {
565 int64_t gid;
566 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
567 parsed.gid = gid;
568 parsed.has_gid = true;
569 } else {
570 printf("ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
571 bad++;
572 }
573 continue;
574 }
575 if (strcmp("mode", args[i]) == 0) {
576 int32_t mode;
577 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
578 parsed.mode = mode;
579 parsed.has_mode = true;
580 } else {
581 printf("ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
582 bad++;
583 }
584 continue;
585 }
586 if (strcmp("dmode", args[i]) == 0) {
587 int32_t mode;
588 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
589 parsed.dmode = mode;
590 parsed.has_dmode = true;
591 } else {
592 printf("ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
593 bad++;
594 }
595 continue;
596 }
597 if (strcmp("fmode", args[i]) == 0) {
598 int32_t mode;
599 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
600 parsed.fmode = mode;
601 parsed.has_fmode = true;
602 } else {
603 printf("ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
604 bad++;
605 }
606 continue;
607 }
608 if (strcmp("capabilities", args[i]) == 0) {
609 int64_t capabilities;
610 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
611 parsed.capabilities = capabilities;
612 parsed.has_capabilities = true;
613 } else {
614 printf("ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
615 bad++;
616 }
617 continue;
618 }
619 if (strcmp("selabel", args[i]) == 0) {
620 if (args[i+1][0] != '\0') {
621 parsed.selabel = args[i+1];
622 parsed.has_selabel = true;
623 } else {
624 printf("ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
625 bad++;
626 }
627 continue;
628 }
629 if (max_warnings != 0) {
630 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
631 max_warnings--;
632 if (max_warnings == 0) {
633 printf("ParsedPermArgs: suppressing further warnings\n");
634 }
635 }
636 }
637 return parsed;
638}
639
640static int ApplyParsedPerms(
641 const char* filename,
642 const struct stat *statptr,
643 struct perm_parsed_args parsed)
644{
645 int bad = 0;
646
Nick Kraleviche4612512013-09-10 15:34:19 -0700647 /* ignore symlinks */
648 if (S_ISLNK(statptr->st_mode)) {
649 return 0;
650 }
651
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700652 if (parsed.has_uid) {
653 if (chown(filename, parsed.uid, -1) < 0) {
654 printf("ApplyParsedPerms: chown of %s to %d failed: %s\n",
655 filename, parsed.uid, strerror(errno));
656 bad++;
657 }
658 }
659
660 if (parsed.has_gid) {
661 if (chown(filename, -1, parsed.gid) < 0) {
662 printf("ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
663 filename, parsed.gid, strerror(errno));
664 bad++;
665 }
666 }
667
668 if (parsed.has_mode) {
669 if (chmod(filename, parsed.mode) < 0) {
670 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
671 filename, parsed.mode, strerror(errno));
672 bad++;
673 }
674 }
675
676 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
677 if (chmod(filename, parsed.dmode) < 0) {
678 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
679 filename, parsed.dmode, strerror(errno));
680 bad++;
681 }
682 }
683
684 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
685 if (chmod(filename, parsed.fmode) < 0) {
686 printf("ApplyParsedPerms: chmod of %s to %d failed: %s\n",
687 filename, parsed.fmode, strerror(errno));
688 bad++;
689 }
690 }
691
692 if (parsed.has_selabel) {
693 // TODO: Don't silently ignore ENOTSUP
694 if (lsetfilecon(filename, parsed.selabel) && (errno != ENOTSUP)) {
695 printf("ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
696 filename, parsed.selabel, strerror(errno));
697 bad++;
698 }
699 }
700
701 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
702 if (parsed.capabilities == 0) {
703 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
704 // Report failure unless it's ENODATA (attribute not set)
705 printf("ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
706 filename, parsed.capabilities, strerror(errno));
707 bad++;
708 }
709 } else {
710 struct vfs_cap_data cap_data;
711 memset(&cap_data, 0, sizeof(cap_data));
712 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
713 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
714 cap_data.data[0].inheritable = 0;
715 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
716 cap_data.data[1].inheritable = 0;
717 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
718 printf("ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
719 filename, parsed.capabilities, strerror(errno));
720 bad++;
721 }
722 }
723 }
724
725 return bad;
726}
727
728// nftw doesn't allow us to pass along context, so we need to use
729// global variables. *sigh*
730static struct perm_parsed_args recursive_parsed_args;
731
732static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
733 int fileflags, struct FTW *pfwt) {
734 return ApplyParsedPerms(filename, statptr, recursive_parsed_args);
735}
736
737static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
738 int i;
739 int bad = 0;
740 static int nwarnings = 0;
741 struct stat sb;
742 Value* result = NULL;
743
744 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
745
746 if ((argc % 2) != 1) {
747 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d",
748 name, argc);
749 }
750
751 char** args = ReadVarArgs(state, argc, argv);
752 if (args == NULL) return NULL;
753
754 if (lstat(args[0], &sb) == -1) {
755 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
756 goto done;
757 }
758
759 struct perm_parsed_args parsed = ParsePermArgs(argc, args);
760
761 if (recursive) {
762 recursive_parsed_args = parsed;
763 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
764 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
765 } else {
766 bad += ApplyParsedPerms(args[0], &sb, parsed);
767 }
768
769done:
770 for (i = 0; i < argc; ++i) {
771 free(args[i]);
772 }
773 free(args);
774
775 if (result != NULL) {
776 return result;
777 }
778
779 if (bad > 0) {
780 return ErrorAbort(state, "%s: some changes failed", name);
781 }
782
783 return StringValue(strdup(""));
784}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700785
Doug Zongker512536a2010-02-17 16:11:44 -0800786Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700787 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700788 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700789 }
790 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700791 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700792 if (key == NULL) return NULL;
793
794 char value[PROPERTY_VALUE_MAX];
795 property_get(key, value, "");
796 free(key);
797
Doug Zongker512536a2010-02-17 16:11:44 -0800798 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700799}
800
801
Doug Zongker47cace92009-06-18 10:11:50 -0700802// file_getprop(file, key)
803//
804// interprets 'file' as a getprop-style file (key=value pairs, one
805// per line, # comment lines and blank lines okay), and returns the value
806// for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800807Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700808 char* result = NULL;
809 char* buffer = NULL;
810 char* filename;
811 char* key;
812 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
813 return NULL;
814 }
815
816 struct stat st;
817 if (stat(filename, &st) < 0) {
818 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
819 name, filename, strerror(errno));
820 goto done;
821 }
822
823#define MAX_FILE_GETPROP_SIZE 65536
824
825 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
826 ErrorAbort(state, "%s too large for %s (max %d)",
827 filename, name, MAX_FILE_GETPROP_SIZE);
828 goto done;
829 }
830
831 buffer = malloc(st.st_size+1);
832 if (buffer == NULL) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700833 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700834 goto done;
835 }
836
837 FILE* f = fopen(filename, "rb");
838 if (f == NULL) {
839 ErrorAbort(state, "%s: failed to open %s: %s",
840 name, filename, strerror(errno));
841 goto done;
842 }
843
844 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700845 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Doug Zongker47cace92009-06-18 10:11:50 -0700846 name, st.st_size+1, filename);
847 fclose(f);
848 goto done;
849 }
850 buffer[st.st_size] = '\0';
851
852 fclose(f);
853
854 char* line = strtok(buffer, "\n");
855 do {
856 // skip whitespace at start of line
857 while (*line && isspace(*line)) ++line;
858
859 // comment or blank line: skip to next line
860 if (*line == '\0' || *line == '#') continue;
861
862 char* equal = strchr(line, '=');
863 if (equal == NULL) {
864 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
865 name, line, filename);
866 goto done;
867 }
868
869 // trim whitespace between key and '='
870 char* key_end = equal-1;
871 while (key_end > line && isspace(*key_end)) --key_end;
872 key_end[1] = '\0';
873
874 // not the key we're looking for
875 if (strcmp(key, line) != 0) continue;
876
877 // skip whitespace after the '=' to the start of the value
878 char* val_start = equal+1;
879 while(*val_start && isspace(*val_start)) ++val_start;
880
881 // trim trailing whitespace
882 char* val_end = val_start + strlen(val_start)-1;
883 while (val_end > val_start && isspace(*val_end)) --val_end;
884 val_end[1] = '\0';
885
886 result = strdup(val_start);
887 break;
888
889 } while ((line = strtok(NULL, "\n")));
890
891 if (result == NULL) result = strdup("");
892
893 done:
894 free(filename);
895 free(key);
896 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800897 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -0700898}
899
900
Doug Zongker8edb00c2009-06-11 17:21:44 -0700901static bool write_raw_image_cb(const unsigned char* data,
902 int data_len, void* ctx) {
903 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
904 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700905 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700906 return false;
907}
908
Doug Zongker179b2d92011-04-12 15:49:04 -0700909// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -0800910Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700911 char* result = NULL;
912
Doug Zongker179b2d92011-04-12 15:49:04 -0700913 Value* partition_value;
914 Value* contents;
915 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700916 return NULL;
917 }
918
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700919 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -0700920 if (partition_value->type != VAL_STRING) {
921 ErrorAbort(state, "partition argument to %s must be string", name);
922 goto done;
923 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700924 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700925 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700926 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700927 goto done;
928 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700929 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700930 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700931 goto done;
932 }
933
934 mtd_scan_partitions();
935 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
936 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700937 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700938 result = strdup("");
939 goto done;
940 }
941
942 MtdWriteContext* ctx = mtd_write_partition(mtd);
943 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700944 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -0700945 name, partition);
946 result = strdup("");
947 goto done;
948 }
949
950 bool success;
951
Doug Zongker179b2d92011-04-12 15:49:04 -0700952 if (contents->type == VAL_STRING) {
953 // we're given a filename as the contents
954 char* filename = contents->data;
955 FILE* f = fopen(filename, "rb");
956 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700957 printf("%s: can't open %s: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -0700958 name, filename, strerror(errno));
959 result = strdup("");
960 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700961 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700962
963 success = true;
964 char* buffer = malloc(BUFSIZ);
965 int read;
966 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
967 int wrote = mtd_write_data(ctx, buffer, read);
968 success = success && (wrote == read);
969 }
970 free(buffer);
971 fclose(f);
972 } else {
973 // we're given a blob as the contents
974 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
975 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700976 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700977 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700978 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -0700979 partition, strerror(errno));
980 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700981
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700982 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700983 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700984 }
985 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700986 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700987 }
988
Doug Zongker179b2d92011-04-12 15:49:04 -0700989 printf("%s %s partition\n",
990 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700991
992 result = success ? partition : strdup("");
993
994done:
Doug Zongker179b2d92011-04-12 15:49:04 -0700995 if (result != partition) FreeValue(partition_value);
996 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -0800997 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700998}
999
Doug Zongker8edb00c2009-06-11 17:21:44 -07001000// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001001Value* ApplyPatchSpaceFn(const char* name, State* state,
1002 int argc, Expr* argv[]) {
1003 char* bytes_str;
1004 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1005 return NULL;
1006 }
1007
1008 char* endptr;
1009 size_t bytes = strtol(bytes_str, &endptr, 10);
1010 if (bytes == 0 && endptr == bytes_str) {
1011 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
1012 name, bytes_str);
1013 free(bytes_str);
1014 return NULL;
1015 }
1016
1017 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1018}
1019
1020
1021// apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1_1, patch_1, ...)
Doug Zongker512536a2010-02-17 16:11:44 -08001022Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001023 if (argc < 6 || (argc % 2) == 1) {
1024 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1025 "even number, got %d",
1026 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001027 }
1028
Doug Zongkerc4351c72010-02-22 14:46:32 -08001029 char* source_filename;
1030 char* target_filename;
1031 char* target_sha1;
1032 char* target_size_str;
1033 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1034 &target_sha1, &target_size_str) < 0) {
1035 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001036 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001037
Doug Zongkerc4351c72010-02-22 14:46:32 -08001038 char* endptr;
1039 size_t target_size = strtol(target_size_str, &endptr, 10);
1040 if (target_size == 0 && endptr == target_size_str) {
1041 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1042 name, target_size_str);
1043 free(source_filename);
1044 free(target_filename);
1045 free(target_sha1);
1046 free(target_size_str);
1047 return NULL;
1048 }
1049
1050 int patchcount = (argc-4) / 2;
1051 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001052
1053 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001054 for (i = 0; i < patchcount; ++i) {
1055 if (patches[i*2]->type != VAL_STRING) {
1056 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1057 break;
1058 }
1059 if (patches[i*2+1]->type != VAL_BLOB) {
1060 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1061 break;
1062 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001063 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001064 if (i != patchcount) {
1065 for (i = 0; i < patchcount*2; ++i) {
1066 FreeValue(patches[i]);
1067 }
1068 free(patches);
1069 return NULL;
1070 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001071
Doug Zongkerc4351c72010-02-22 14:46:32 -08001072 char** patch_sha_str = malloc(patchcount * sizeof(char*));
1073 for (i = 0; i < patchcount; ++i) {
1074 patch_sha_str[i] = patches[i*2]->data;
1075 patches[i*2]->data = NULL;
1076 FreeValue(patches[i*2]);
1077 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001078 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001079
1080 int result = applypatch(source_filename, target_filename,
1081 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001082 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001083
1084 for (i = 0; i < patchcount; ++i) {
1085 FreeValue(patches[i]);
1086 }
1087 free(patch_sha_str);
1088 free(patches);
1089
1090 return StringValue(strdup(result == 0 ? "t" : ""));
1091}
1092
1093// apply_patch_check(file, [sha1_1, ...])
1094Value* ApplyPatchCheckFn(const char* name, State* state,
1095 int argc, Expr* argv[]) {
1096 if (argc < 1) {
1097 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1098 name, argc);
1099 }
1100
1101 char* filename;
1102 if (ReadArgs(state, argv, 1, &filename) < 0) {
1103 return NULL;
1104 }
1105
1106 int patchcount = argc-1;
1107 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1108
1109 int result = applypatch_check(filename, patchcount, sha1s);
1110
1111 int i;
1112 for (i = 0; i < patchcount; ++i) {
1113 free(sha1s[i]);
1114 }
1115 free(sha1s);
1116
1117 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001118}
1119
Doug Zongker512536a2010-02-17 16:11:44 -08001120Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001121 char** args = ReadVarArgs(state, argc, argv);
1122 if (args == NULL) {
1123 return NULL;
1124 }
1125
1126 int size = 0;
1127 int i;
1128 for (i = 0; i < argc; ++i) {
1129 size += strlen(args[i]);
1130 }
1131 char* buffer = malloc(size+1);
1132 size = 0;
1133 for (i = 0; i < argc; ++i) {
1134 strcpy(buffer+size, args[i]);
1135 size += strlen(args[i]);
1136 free(args[i]);
1137 }
1138 free(args);
1139 buffer[size] = '\0';
1140
1141 char* line = strtok(buffer, "\n");
1142 while (line) {
1143 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
1144 "ui_print %s\n", line);
1145 line = strtok(NULL, "\n");
1146 }
1147 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
1148
Doug Zongker512536a2010-02-17 16:11:44 -08001149 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001150}
1151
Doug Zongkerd0181b82011-10-19 10:51:12 -07001152Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1153 if (argc != 0) {
1154 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1155 }
1156 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1157 return StringValue(strdup("t"));
1158}
1159
Doug Zongker512536a2010-02-17 16:11:44 -08001160Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001161 if (argc < 1) {
1162 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1163 }
1164 char** args = ReadVarArgs(state, argc, argv);
1165 if (args == NULL) {
1166 return NULL;
1167 }
1168
1169 char** args2 = malloc(sizeof(char*) * (argc+1));
1170 memcpy(args2, args, sizeof(char*) * argc);
1171 args2[argc] = NULL;
1172
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001173 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001174
1175 pid_t child = fork();
1176 if (child == 0) {
1177 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001178 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001179 _exit(1);
1180 }
1181 int status;
1182 waitpid(child, &status, 0);
1183 if (WIFEXITED(status)) {
1184 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001185 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001186 WEXITSTATUS(status));
1187 }
1188 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001189 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001190 WTERMSIG(status));
1191 }
1192
1193 int i;
1194 for (i = 0; i < argc; ++i) {
1195 free(args[i]);
1196 }
1197 free(args);
1198 free(args2);
1199
1200 char buffer[20];
1201 sprintf(buffer, "%d", status);
1202
Doug Zongker512536a2010-02-17 16:11:44 -08001203 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001204}
1205
Doug Zongker512536a2010-02-17 16:11:44 -08001206// Take a sha-1 digest and return it as a newly-allocated hex string.
1207static char* PrintSha1(uint8_t* digest) {
1208 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
1209 int i;
1210 const char* alphabet = "0123456789abcdef";
1211 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
1212 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
1213 buffer[i*2+1] = alphabet[digest[i] & 0xf];
1214 }
1215 buffer[i*2] = '\0';
1216 return buffer;
1217}
1218
1219// sha1_check(data)
1220// to return the sha1 of the data (given in the format returned by
1221// read_file).
1222//
1223// sha1_check(data, sha1_hex, [sha1_hex, ...])
1224// returns the sha1 of the file if it matches any of the hex
1225// strings passed, or "" if it does not equal any of them.
1226//
1227Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1228 if (argc < 1) {
1229 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1230 }
1231
1232 Value** args = ReadValueVarArgs(state, argc, argv);
1233 if (args == NULL) {
1234 return NULL;
1235 }
1236
1237 if (args[0]->size < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001238 printf("%s(): no file contents received", name);
Doug Zongker512536a2010-02-17 16:11:44 -08001239 return StringValue(strdup(""));
1240 }
1241 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001242 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001243 FreeValue(args[0]);
1244
1245 if (argc == 1) {
1246 return StringValue(PrintSha1(digest));
1247 }
1248
1249 int i;
1250 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1251 for (i = 1; i < argc; ++i) {
1252 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001253 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001254 name, i);
1255 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1256 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001257 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1258 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001259 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1260 break;
1261 }
1262 FreeValue(args[i]);
1263 }
1264 if (i >= argc) {
1265 // Didn't match any of the hex strings; return false.
1266 return StringValue(strdup(""));
1267 }
1268 // Found a match; free all the remaining arguments and return the
1269 // matched one.
1270 int j;
1271 for (j = i+1; j < argc; ++j) {
1272 FreeValue(args[j]);
1273 }
1274 return args[i];
1275}
1276
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001277// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001278// is actually a FileContents*).
1279Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1280 if (argc != 1) {
1281 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1282 }
1283 char* filename;
1284 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1285
1286 Value* v = malloc(sizeof(Value));
1287 v->type = VAL_BLOB;
1288
1289 FileContents fc;
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001290 if (LoadFileContents(filename, &fc, RETOUCH_DONT_MASK) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001291 ErrorAbort(state, "%s() loading \"%s\" failed: %s",
1292 name, filename, strerror(errno));
1293 free(filename);
1294 free(v);
1295 free(fc.data);
1296 return NULL;
1297 }
1298
1299 v->size = fc.size;
1300 v->data = (char*)fc.data;
1301
1302 free(filename);
1303 return v;
1304}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001305
Doug Zongker9931f7f2009-06-10 14:11:53 -07001306void RegisterInstallFunctions() {
1307 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001308 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001309 RegisterFunction("unmount", UnmountFn);
1310 RegisterFunction("format", FormatFn);
1311 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001312 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001313 RegisterFunction("delete", DeleteFn);
1314 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001315 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1316 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001317 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001318
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001319 // Usage:
1320 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1321 // Example:
1322 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1323 RegisterFunction("set_metadata", SetMetadataFn);
1324
1325 // Usage:
1326 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1327 // Example:
1328 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1329 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1330
Doug Zongker8edb00c2009-06-11 17:21:44 -07001331 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001332 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001333 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001334
1335 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001336 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1337 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001338
Doug Zongker512536a2010-02-17 16:11:44 -08001339 RegisterFunction("read_file", ReadFileFn);
1340 RegisterFunction("sha1_check", Sha1CheckFn);
1341
Doug Zongkerd0181b82011-10-19 10:51:12 -07001342 RegisterFunction("wipe_cache", WipeCacheFn);
1343
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001344 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001345
1346 RegisterFunction("run_program", RunProgramFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001347}