blob: 19054236c241716e09ee6f699ca464df9c712c29 [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>
Doug Zongker9931f7f2009-06-10 14:11:53 -070030
Doug Zongker8edb00c2009-06-11 17:21:44 -070031#include "cutils/misc.h"
32#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070033#include "edify/expr.h"
Doug Zongker512536a2010-02-17 16:11:44 -080034#include "mincrypt/sha.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070035#include "minzip/DirUtil.h"
36#include "mtdutils/mounts.h"
37#include "mtdutils/mtdutils.h"
38#include "updater.h"
Doug Zongker512536a2010-02-17 16:11:44 -080039#include "applypatch/applypatch.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070040
Doug Zongker3d177d02010-07-01 09:18:44 -070041#ifdef USE_EXT4
42#include "make_ext4fs.h"
43#endif
44
45// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -070046//
Doug Zongker3d177d02010-07-01 09:18:44 -070047// fs_type="yaffs2" partition_type="MTD" location=partition
48// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -080049Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070050 char* result = NULL;
Doug Zongker3d177d02010-07-01 09:18:44 -070051 if (argc != 4) {
52 return ErrorAbort(state, "%s() expects 4 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -070053 }
Doug Zongker3d177d02010-07-01 09:18:44 -070054 char* fs_type;
55 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -070056 char* location;
57 char* mount_point;
Doug Zongker3d177d02010-07-01 09:18:44 -070058 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
59 &location, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070060 return NULL;
61 }
62
Doug Zongker3d177d02010-07-01 09:18:44 -070063 if (strlen(fs_type) == 0) {
64 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
65 goto done;
66 }
67 if (strlen(partition_type) == 0) {
68 ErrorAbort(state, "partition_type argument to %s() can't be empty",
69 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070070 goto done;
71 }
72 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070073 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070074 goto done;
75 }
76 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070077 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070078 goto done;
79 }
80
Stephen Smalley779701d2012-02-09 14:13:23 -050081 char *secontext = NULL;
82
83 if (sehandle) {
84 selabel_lookup(sehandle, &secontext, mount_point, 0755);
85 setfscreatecon(secontext);
86 }
Stephen Smalley779701d2012-02-09 14:13:23 -050087
Doug Zongker9931f7f2009-06-10 14:11:53 -070088 mkdir(mount_point, 0755);
89
Stephen Smalley779701d2012-02-09 14:13:23 -050090 if (secontext) {
91 freecon(secontext);
92 setfscreatecon(NULL);
93 }
Stephen Smalley779701d2012-02-09 14:13:23 -050094
Doug Zongker3d177d02010-07-01 09:18:44 -070095 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070096 mtd_scan_partitions();
97 const MtdPartition* mtd;
98 mtd = mtd_find_partition_by_name(location);
99 if (mtd == NULL) {
100 fprintf(stderr, "%s: no mtd partition named \"%s\"",
101 name, location);
102 result = strdup("");
103 goto done;
104 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700105 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700106 fprintf(stderr, "mtd mount of %s failed: %s\n",
107 location, strerror(errno));
108 result = strdup("");
109 goto done;
110 }
111 result = mount_point;
112 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700113 if (mount(location, mount_point, fs_type,
Doug Zongker9931f7f2009-06-10 14:11:53 -0700114 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
Doug Zongker60babf82009-09-18 15:11:24 -0700115 fprintf(stderr, "%s: failed to mount %s at %s: %s\n",
116 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700117 result = strdup("");
118 } else {
119 result = mount_point;
120 }
121 }
122
123done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700124 free(fs_type);
125 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700126 free(location);
127 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800128 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700129}
130
Doug Zongker8edb00c2009-06-11 17:21:44 -0700131
132// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800133Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700134 char* result = NULL;
135 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700136 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700137 }
138 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700139 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700140 return NULL;
141 }
142 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700143 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700144 goto done;
145 }
146
147 scan_mounted_volumes();
148 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
149 if (vol == NULL) {
150 result = strdup("");
151 } else {
152 result = mount_point;
153 }
154
155done:
156 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800157 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700158}
159
160
Doug Zongker512536a2010-02-17 16:11:44 -0800161Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700162 char* result = NULL;
163 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700164 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700165 }
166 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700167 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700168 return NULL;
169 }
170 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700171 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700172 goto done;
173 }
174
175 scan_mounted_volumes();
176 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
177 if (vol == NULL) {
178 fprintf(stderr, "unmount of %s failed; no such volume\n", mount_point);
179 result = strdup("");
180 } else {
181 unmount_mounted_volume(vol);
182 result = mount_point;
183 }
184
185done:
186 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800187 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700188}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700189
190
Stephen Smalley779701d2012-02-09 14:13:23 -0500191// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700192//
Stephen Smalley779701d2012-02-09 14:13:23 -0500193// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
194// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800195// if fs_size == 0, then make_ext4fs uses the entire partition.
196// if fs_size > 0, that is the size to use
197// if fs_size < 0, then reserve that many bytes at the end of the partition
Doug Zongker512536a2010-02-17 16:11:44 -0800198Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700199 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400200 if (argc != 5) {
201 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700202 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700203 char* fs_type;
204 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700205 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800206 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400207 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500208
Stephen Smalley779701d2012-02-09 14:13:23 -0500209 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
210 return NULL;
211 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700212
Doug Zongker3d177d02010-07-01 09:18:44 -0700213 if (strlen(fs_type) == 0) {
214 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
215 goto done;
216 }
217 if (strlen(partition_type) == 0) {
218 ErrorAbort(state, "partition_type argument to %s() can't be empty",
219 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700220 goto done;
221 }
222 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700223 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700224 goto done;
225 }
226
Stephen Smalley516e4e22012-04-03 13:35:11 -0400227 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500228 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
229 goto done;
230 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500231
Doug Zongker3d177d02010-07-01 09:18:44 -0700232 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700233 mtd_scan_partitions();
234 const MtdPartition* mtd = mtd_find_partition_by_name(location);
235 if (mtd == NULL) {
236 fprintf(stderr, "%s: no mtd partition named \"%s\"",
237 name, location);
238 result = strdup("");
239 goto done;
240 }
241 MtdWriteContext* ctx = mtd_write_partition(mtd);
242 if (ctx == NULL) {
243 fprintf(stderr, "%s: can't write \"%s\"", name, location);
244 result = strdup("");
245 goto done;
246 }
247 if (mtd_erase_blocks(ctx, -1) == -1) {
248 mtd_write_close(ctx);
249 fprintf(stderr, "%s: failed to erase \"%s\"", name, location);
250 result = strdup("");
251 goto done;
252 }
253 if (mtd_write_close(ctx) != 0) {
254 fprintf(stderr, "%s: failed to close \"%s\"", name, location);
255 result = strdup("");
256 goto done;
257 }
258 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700259#ifdef USE_EXT4
260 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500261 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700262 if (status != 0) {
263 fprintf(stderr, "%s: make_ext4fs failed (%d) on %s",
264 name, status, location);
265 result = strdup("");
266 goto done;
267 }
268 result = location;
269#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700270 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700271 fprintf(stderr, "%s: unsupported fs_type \"%s\" partition_type \"%s\"",
272 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700273 }
274
275done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700276 free(fs_type);
277 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700278 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800279 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700280}
281
Doug Zongker8edb00c2009-06-11 17:21:44 -0700282
Doug Zongker512536a2010-02-17 16:11:44 -0800283Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700284 char** paths = malloc(argc * sizeof(char*));
285 int i;
286 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700287 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700288 if (paths[i] == NULL) {
289 int j;
290 for (j = 0; j < i; ++i) {
291 free(paths[j]);
292 }
293 free(paths);
294 return NULL;
295 }
296 }
297
298 bool recursive = (strcmp(name, "delete_recursive") == 0);
299
300 int success = 0;
301 for (i = 0; i < argc; ++i) {
302 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
303 ++success;
304 free(paths[i]);
305 }
306 free(paths);
307
308 char buffer[10];
309 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800310 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700311}
312
Doug Zongker8edb00c2009-06-11 17:21:44 -0700313
Doug Zongker512536a2010-02-17 16:11:44 -0800314Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700315 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700316 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700317 }
318 char* frac_str;
319 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700320 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700321 return NULL;
322 }
323
324 double frac = strtod(frac_str, NULL);
325 int sec = strtol(sec_str, NULL, 10);
326
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700327 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700328 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
329
Doug Zongker9931f7f2009-06-10 14:11:53 -0700330 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800331 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700332}
333
Doug Zongker512536a2010-02-17 16:11:44 -0800334Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700335 if (argc != 1) {
336 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
337 }
338 char* frac_str;
339 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
340 return NULL;
341 }
342
343 double frac = strtod(frac_str, NULL);
344
345 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
346 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
347
Doug Zongker512536a2010-02-17 16:11:44 -0800348 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700349}
350
Doug Zongker8edb00c2009-06-11 17:21:44 -0700351// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800352Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700353 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700354 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700355 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700356 }
357 char* zip_path;
358 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700359 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700360
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700361 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700362
363 // To create a consistent system image, never use the clock for timestamps.
364 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
365
366 bool success = mzExtractRecursive(za, zip_path, dest_path,
367 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500368 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700369 free(zip_path);
370 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800371 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700372}
373
Doug Zongker8edb00c2009-06-11 17:21:44 -0700374
375// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800376// or
377// package_extract_file(package_path)
378// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800379// function (the char* returned is actually a FileContents*).
380Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700381 int argc, Expr* argv[]) {
Doug Zongker6aece332010-02-01 14:40:12 -0800382 if (argc != 1 && argc != 2) {
383 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
384 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700385 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700386 bool success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800387 if (argc == 2) {
388 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700389
Doug Zongker6aece332010-02-01 14:40:12 -0800390 char* zip_path;
391 char* dest_path;
392 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
393
394 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
395 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
396 if (entry == NULL) {
397 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
398 goto done2;
399 }
400
401 FILE* f = fopen(dest_path, "wb");
402 if (f == NULL) {
403 fprintf(stderr, "%s: can't open %s for write: %s\n",
404 name, dest_path, strerror(errno));
405 goto done2;
406 }
407 success = mzExtractZipEntryToFile(za, entry, fileno(f));
408 fclose(f);
409
410 done2:
411 free(zip_path);
412 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800413 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800414 } else {
415 // The one-argument version returns the contents of the file
416 // as the result.
417
418 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800419 Value* v = malloc(sizeof(Value));
420 v->type = VAL_BLOB;
421 v->size = -1;
422 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800423
424 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
425
426 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
427 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
428 if (entry == NULL) {
429 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
430 goto done1;
431 }
432
Doug Zongker512536a2010-02-17 16:11:44 -0800433 v->size = mzGetZipEntryUncompLen(entry);
434 v->data = malloc(v->size);
435 if (v->data == NULL) {
Doug Zongker6aece332010-02-01 14:40:12 -0800436 fprintf(stderr, "%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800437 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800438 goto done1;
439 }
440
Doug Zongker512536a2010-02-17 16:11:44 -0800441 success = mzExtractZipEntryToBuffer(za, entry,
442 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800443
444 done1:
445 free(zip_path);
446 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800447 free(v->data);
448 v->data = NULL;
449 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800450 }
Doug Zongker512536a2010-02-17 16:11:44 -0800451 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700452 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700453}
454
Doug Zongkera23075f2012-08-06 16:19:09 -0700455// Create all parent directories of name, if necessary.
456static int make_parents(char* name) {
457 char* p;
458 for (p = name + (strlen(name)-1); p > name; --p) {
459 if (*p != '/') continue;
460 *p = '\0';
461 if (make_parents(name) < 0) return -1;
462 int result = mkdir(name, 0700);
463 if (result == 0) fprintf(stderr, "symlink(): created [%s]\n", name);
464 *p = '/';
465 if (result == 0 || errno == EEXIST) {
466 // successfully created or already existed; we're done
467 return 0;
468 } else {
469 fprintf(stderr, "failed to mkdir %s: %s\n", name, strerror(errno));
470 return -1;
471 }
472 }
473 return 0;
474}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700475
Doug Zongker9931f7f2009-06-10 14:11:53 -0700476// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700477// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800478Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700479 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700480 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700481 }
482 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700483 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700484 if (target == NULL) return NULL;
485
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700486 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700487 if (srcs == NULL) {
488 free(target);
489 return NULL;
490 }
491
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700492 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700493 int i;
494 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700495 if (unlink(srcs[i]) < 0) {
496 if (errno != ENOENT) {
497 fprintf(stderr, "%s: failed to remove %s: %s\n",
498 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700499 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700500 }
501 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700502 if (make_parents(srcs[i])) {
503 fprintf(stderr, "%s: failed to symlink %s to %s: making parents failed\n",
504 name, srcs[i], target);
505 ++bad;
506 }
Doug Zongker60babf82009-09-18 15:11:24 -0700507 if (symlink(target, srcs[i]) < 0) {
508 fprintf(stderr, "%s: failed to symlink %s to %s: %s\n",
509 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700510 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700511 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700512 free(srcs[i]);
513 }
514 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700515 if (bad) {
516 return ErrorAbort(state, "%s: some symlinks failed", name);
517 }
Doug Zongker512536a2010-02-17 16:11:44 -0800518 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700519}
520
Doug Zongker8edb00c2009-06-11 17:21:44 -0700521
Doug Zongker512536a2010-02-17 16:11:44 -0800522Value* SetPermFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700523 char* result = NULL;
524 bool recursive = (strcmp(name, "set_perm_recursive") == 0);
525
526 int min_args = 4 + (recursive ? 1 : 0);
527 if (argc < min_args) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700528 return ErrorAbort(state, "%s() expects %d+ args, got %d",
529 name, min_args, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700530 }
531
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700532 char** args = ReadVarArgs(state, argc, argv);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700533 if (args == NULL) return NULL;
534
535 char* end;
536 int i;
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700537 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700538
539 int uid = strtoul(args[0], &end, 0);
540 if (*end != '\0' || args[0][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700541 ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700542 goto done;
543 }
544
545 int gid = strtoul(args[1], &end, 0);
546 if (*end != '\0' || args[1][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700547 ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700548 goto done;
549 }
550
551 if (recursive) {
552 int dir_mode = strtoul(args[2], &end, 0);
553 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700554 ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700555 goto done;
556 }
557
558 int file_mode = strtoul(args[3], &end, 0);
559 if (*end != '\0' || args[3][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700560 ErrorAbort(state, "%s: \"%s\" not a valid filemode",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700561 name, args[3]);
562 goto done;
563 }
564
565 for (i = 4; i < argc; ++i) {
566 dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode);
567 }
568 } else {
569 int mode = strtoul(args[2], &end, 0);
570 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700571 ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700572 goto done;
573 }
574
Doug Zongker0bbfe3d2009-06-25 13:37:31 -0700575 for (i = 3; i < argc; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700576 if (chown(args[i], uid, gid) < 0) {
577 fprintf(stderr, "%s: chown of %s to %d %d failed: %s\n",
578 name, args[i], uid, gid, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700579 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700580 }
581 if (chmod(args[i], mode) < 0) {
582 fprintf(stderr, "%s: chmod of %s to %o failed: %s\n",
583 name, args[i], mode, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700584 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700585 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700586 }
587 }
588 result = strdup("");
589
590done:
591 for (i = 0; i < argc; ++i) {
592 free(args[i]);
593 }
594 free(args);
595
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700596 if (bad) {
597 free(result);
598 return ErrorAbort(state, "%s: some changes failed", name);
599 }
Doug Zongker512536a2010-02-17 16:11:44 -0800600 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700601}
602
Doug Zongker8edb00c2009-06-11 17:21:44 -0700603
Doug Zongker512536a2010-02-17 16:11:44 -0800604Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700605 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700606 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700607 }
608 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700609 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700610 if (key == NULL) return NULL;
611
612 char value[PROPERTY_VALUE_MAX];
613 property_get(key, value, "");
614 free(key);
615
Doug Zongker512536a2010-02-17 16:11:44 -0800616 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700617}
618
619
Doug Zongker47cace92009-06-18 10:11:50 -0700620// file_getprop(file, key)
621//
622// interprets 'file' as a getprop-style file (key=value pairs, one
623// per line, # comment lines and blank lines okay), and returns the value
624// for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800625Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700626 char* result = NULL;
627 char* buffer = NULL;
628 char* filename;
629 char* key;
630 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
631 return NULL;
632 }
633
634 struct stat st;
635 if (stat(filename, &st) < 0) {
636 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
637 name, filename, strerror(errno));
638 goto done;
639 }
640
641#define MAX_FILE_GETPROP_SIZE 65536
642
643 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
644 ErrorAbort(state, "%s too large for %s (max %d)",
645 filename, name, MAX_FILE_GETPROP_SIZE);
646 goto done;
647 }
648
649 buffer = malloc(st.st_size+1);
650 if (buffer == NULL) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700651 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700652 goto done;
653 }
654
655 FILE* f = fopen(filename, "rb");
656 if (f == NULL) {
657 ErrorAbort(state, "%s: failed to open %s: %s",
658 name, filename, strerror(errno));
659 goto done;
660 }
661
662 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700663 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Doug Zongker47cace92009-06-18 10:11:50 -0700664 name, st.st_size+1, filename);
665 fclose(f);
666 goto done;
667 }
668 buffer[st.st_size] = '\0';
669
670 fclose(f);
671
672 char* line = strtok(buffer, "\n");
673 do {
674 // skip whitespace at start of line
675 while (*line && isspace(*line)) ++line;
676
677 // comment or blank line: skip to next line
678 if (*line == '\0' || *line == '#') continue;
679
680 char* equal = strchr(line, '=');
681 if (equal == NULL) {
682 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
683 name, line, filename);
684 goto done;
685 }
686
687 // trim whitespace between key and '='
688 char* key_end = equal-1;
689 while (key_end > line && isspace(*key_end)) --key_end;
690 key_end[1] = '\0';
691
692 // not the key we're looking for
693 if (strcmp(key, line) != 0) continue;
694
695 // skip whitespace after the '=' to the start of the value
696 char* val_start = equal+1;
697 while(*val_start && isspace(*val_start)) ++val_start;
698
699 // trim trailing whitespace
700 char* val_end = val_start + strlen(val_start)-1;
701 while (val_end > val_start && isspace(*val_end)) --val_end;
702 val_end[1] = '\0';
703
704 result = strdup(val_start);
705 break;
706
707 } while ((line = strtok(NULL, "\n")));
708
709 if (result == NULL) result = strdup("");
710
711 done:
712 free(filename);
713 free(key);
714 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800715 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -0700716}
717
718
Doug Zongker8edb00c2009-06-11 17:21:44 -0700719static bool write_raw_image_cb(const unsigned char* data,
720 int data_len, void* ctx) {
721 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
722 if (r == data_len) return true;
723 fprintf(stderr, "%s\n", strerror(errno));
724 return false;
725}
726
Doug Zongker179b2d92011-04-12 15:49:04 -0700727// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -0800728Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700729 char* result = NULL;
730
Doug Zongker179b2d92011-04-12 15:49:04 -0700731 Value* partition_value;
732 Value* contents;
733 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700734 return NULL;
735 }
736
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700737 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -0700738 if (partition_value->type != VAL_STRING) {
739 ErrorAbort(state, "partition argument to %s must be string", name);
740 goto done;
741 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700742 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700743 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700744 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700745 goto done;
746 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700747 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700748 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700749 goto done;
750 }
751
752 mtd_scan_partitions();
753 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
754 if (mtd == NULL) {
755 fprintf(stderr, "%s: no mtd partition named \"%s\"\n", name, partition);
756 result = strdup("");
757 goto done;
758 }
759
760 MtdWriteContext* ctx = mtd_write_partition(mtd);
761 if (ctx == NULL) {
762 fprintf(stderr, "%s: can't write mtd partition \"%s\"\n",
763 name, partition);
764 result = strdup("");
765 goto done;
766 }
767
768 bool success;
769
Doug Zongker179b2d92011-04-12 15:49:04 -0700770 if (contents->type == VAL_STRING) {
771 // we're given a filename as the contents
772 char* filename = contents->data;
773 FILE* f = fopen(filename, "rb");
774 if (f == NULL) {
775 fprintf(stderr, "%s: can't open %s: %s\n",
776 name, filename, strerror(errno));
777 result = strdup("");
778 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700779 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700780
781 success = true;
782 char* buffer = malloc(BUFSIZ);
783 int read;
784 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
785 int wrote = mtd_write_data(ctx, buffer, read);
786 success = success && (wrote == read);
787 }
788 free(buffer);
789 fclose(f);
790 } else {
791 // we're given a blob as the contents
792 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
793 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700794 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700795 if (!success) {
796 fprintf(stderr, "mtd_write_data to %s failed: %s\n",
797 partition, strerror(errno));
798 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700799
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700800 if (mtd_erase_blocks(ctx, -1) == -1) {
801 fprintf(stderr, "%s: error erasing blocks of %s\n", name, partition);
802 }
803 if (mtd_write_close(ctx) != 0) {
804 fprintf(stderr, "%s: error closing write of %s\n", name, partition);
805 }
806
Doug Zongker179b2d92011-04-12 15:49:04 -0700807 printf("%s %s partition\n",
808 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700809
810 result = success ? partition : strdup("");
811
812done:
Doug Zongker179b2d92011-04-12 15:49:04 -0700813 if (result != partition) FreeValue(partition_value);
814 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -0800815 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700816}
817
Doug Zongker8edb00c2009-06-11 17:21:44 -0700818// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -0800819Value* ApplyPatchSpaceFn(const char* name, State* state,
820 int argc, Expr* argv[]) {
821 char* bytes_str;
822 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
823 return NULL;
824 }
825
826 char* endptr;
827 size_t bytes = strtol(bytes_str, &endptr, 10);
828 if (bytes == 0 && endptr == bytes_str) {
829 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
830 name, bytes_str);
831 free(bytes_str);
832 return NULL;
833 }
834
835 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
836}
837
838
839// apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1_1, patch_1, ...)
Doug Zongker512536a2010-02-17 16:11:44 -0800840Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800841 if (argc < 6 || (argc % 2) == 1) {
842 return ErrorAbort(state, "%s(): expected at least 6 args and an "
843 "even number, got %d",
844 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700845 }
846
Doug Zongkerc4351c72010-02-22 14:46:32 -0800847 char* source_filename;
848 char* target_filename;
849 char* target_sha1;
850 char* target_size_str;
851 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
852 &target_sha1, &target_size_str) < 0) {
853 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700854 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700855
Doug Zongkerc4351c72010-02-22 14:46:32 -0800856 char* endptr;
857 size_t target_size = strtol(target_size_str, &endptr, 10);
858 if (target_size == 0 && endptr == target_size_str) {
859 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
860 name, target_size_str);
861 free(source_filename);
862 free(target_filename);
863 free(target_sha1);
864 free(target_size_str);
865 return NULL;
866 }
867
868 int patchcount = (argc-4) / 2;
869 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700870
871 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800872 for (i = 0; i < patchcount; ++i) {
873 if (patches[i*2]->type != VAL_STRING) {
874 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
875 break;
876 }
877 if (patches[i*2+1]->type != VAL_BLOB) {
878 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
879 break;
880 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700881 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800882 if (i != patchcount) {
883 for (i = 0; i < patchcount*2; ++i) {
884 FreeValue(patches[i]);
885 }
886 free(patches);
887 return NULL;
888 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700889
Doug Zongkerc4351c72010-02-22 14:46:32 -0800890 char** patch_sha_str = malloc(patchcount * sizeof(char*));
891 for (i = 0; i < patchcount; ++i) {
892 patch_sha_str[i] = patches[i*2]->data;
893 patches[i*2]->data = NULL;
894 FreeValue(patches[i*2]);
895 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700896 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800897
898 int result = applypatch(source_filename, target_filename,
899 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -0700900 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800901
902 for (i = 0; i < patchcount; ++i) {
903 FreeValue(patches[i]);
904 }
905 free(patch_sha_str);
906 free(patches);
907
908 return StringValue(strdup(result == 0 ? "t" : ""));
909}
910
911// apply_patch_check(file, [sha1_1, ...])
912Value* ApplyPatchCheckFn(const char* name, State* state,
913 int argc, Expr* argv[]) {
914 if (argc < 1) {
915 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
916 name, argc);
917 }
918
919 char* filename;
920 if (ReadArgs(state, argv, 1, &filename) < 0) {
921 return NULL;
922 }
923
924 int patchcount = argc-1;
925 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
926
927 int result = applypatch_check(filename, patchcount, sha1s);
928
929 int i;
930 for (i = 0; i < patchcount; ++i) {
931 free(sha1s[i]);
932 }
933 free(sha1s);
934
935 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700936}
937
Doug Zongker512536a2010-02-17 16:11:44 -0800938Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700939 char** args = ReadVarArgs(state, argc, argv);
940 if (args == NULL) {
941 return NULL;
942 }
943
944 int size = 0;
945 int i;
946 for (i = 0; i < argc; ++i) {
947 size += strlen(args[i]);
948 }
949 char* buffer = malloc(size+1);
950 size = 0;
951 for (i = 0; i < argc; ++i) {
952 strcpy(buffer+size, args[i]);
953 size += strlen(args[i]);
954 free(args[i]);
955 }
956 free(args);
957 buffer[size] = '\0';
958
959 char* line = strtok(buffer, "\n");
960 while (line) {
961 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
962 "ui_print %s\n", line);
963 line = strtok(NULL, "\n");
964 }
965 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
966
Doug Zongker512536a2010-02-17 16:11:44 -0800967 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700968}
969
Doug Zongkerd0181b82011-10-19 10:51:12 -0700970Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
971 if (argc != 0) {
972 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
973 }
974 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
975 return StringValue(strdup("t"));
976}
977
Doug Zongker512536a2010-02-17 16:11:44 -0800978Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700979 if (argc < 1) {
980 return ErrorAbort(state, "%s() expects at least 1 arg", name);
981 }
982 char** args = ReadVarArgs(state, argc, argv);
983 if (args == NULL) {
984 return NULL;
985 }
986
987 char** args2 = malloc(sizeof(char*) * (argc+1));
988 memcpy(args2, args, sizeof(char*) * argc);
989 args2[argc] = NULL;
990
991 fprintf(stderr, "about to run program [%s] with %d args\n", args2[0], argc);
992
993 pid_t child = fork();
994 if (child == 0) {
995 execv(args2[0], args2);
996 fprintf(stderr, "run_program: execv failed: %s\n", strerror(errno));
997 _exit(1);
998 }
999 int status;
1000 waitpid(child, &status, 0);
1001 if (WIFEXITED(status)) {
1002 if (WEXITSTATUS(status) != 0) {
1003 fprintf(stderr, "run_program: child exited with status %d\n",
1004 WEXITSTATUS(status));
1005 }
1006 } else if (WIFSIGNALED(status)) {
1007 fprintf(stderr, "run_program: child terminated by signal %d\n",
1008 WTERMSIG(status));
1009 }
1010
1011 int i;
1012 for (i = 0; i < argc; ++i) {
1013 free(args[i]);
1014 }
1015 free(args);
1016 free(args2);
1017
1018 char buffer[20];
1019 sprintf(buffer, "%d", status);
1020
Doug Zongker512536a2010-02-17 16:11:44 -08001021 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001022}
1023
Doug Zongker512536a2010-02-17 16:11:44 -08001024// Take a sha-1 digest and return it as a newly-allocated hex string.
1025static char* PrintSha1(uint8_t* digest) {
1026 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
1027 int i;
1028 const char* alphabet = "0123456789abcdef";
1029 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
1030 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
1031 buffer[i*2+1] = alphabet[digest[i] & 0xf];
1032 }
1033 buffer[i*2] = '\0';
1034 return buffer;
1035}
1036
1037// sha1_check(data)
1038// to return the sha1 of the data (given in the format returned by
1039// read_file).
1040//
1041// sha1_check(data, sha1_hex, [sha1_hex, ...])
1042// returns the sha1 of the file if it matches any of the hex
1043// strings passed, or "" if it does not equal any of them.
1044//
1045Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1046 if (argc < 1) {
1047 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1048 }
1049
1050 Value** args = ReadValueVarArgs(state, argc, argv);
1051 if (args == NULL) {
1052 return NULL;
1053 }
1054
1055 if (args[0]->size < 0) {
1056 fprintf(stderr, "%s(): no file contents received", name);
1057 return StringValue(strdup(""));
1058 }
1059 uint8_t digest[SHA_DIGEST_SIZE];
1060 SHA(args[0]->data, args[0]->size, digest);
1061 FreeValue(args[0]);
1062
1063 if (argc == 1) {
1064 return StringValue(PrintSha1(digest));
1065 }
1066
1067 int i;
1068 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1069 for (i = 1; i < argc; ++i) {
1070 if (args[i]->type != VAL_STRING) {
1071 fprintf(stderr, "%s(): arg %d is not a string; skipping",
1072 name, i);
1073 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1074 // Warn about bad args and skip them.
1075 fprintf(stderr, "%s(): error parsing \"%s\" as sha-1; skipping",
1076 name, args[i]->data);
1077 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1078 break;
1079 }
1080 FreeValue(args[i]);
1081 }
1082 if (i >= argc) {
1083 // Didn't match any of the hex strings; return false.
1084 return StringValue(strdup(""));
1085 }
1086 // Found a match; free all the remaining arguments and return the
1087 // matched one.
1088 int j;
1089 for (j = i+1; j < argc; ++j) {
1090 FreeValue(args[j]);
1091 }
1092 return args[i];
1093}
1094
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001095// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001096// is actually a FileContents*).
1097Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1098 if (argc != 1) {
1099 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1100 }
1101 char* filename;
1102 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1103
1104 Value* v = malloc(sizeof(Value));
1105 v->type = VAL_BLOB;
1106
1107 FileContents fc;
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001108 if (LoadFileContents(filename, &fc, RETOUCH_DONT_MASK) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001109 ErrorAbort(state, "%s() loading \"%s\" failed: %s",
1110 name, filename, strerror(errno));
1111 free(filename);
1112 free(v);
1113 free(fc.data);
1114 return NULL;
1115 }
1116
1117 v->size = fc.size;
1118 v->data = (char*)fc.data;
1119
1120 free(filename);
1121 return v;
1122}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001123
Doug Zongker9931f7f2009-06-10 14:11:53 -07001124void RegisterInstallFunctions() {
1125 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001126 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001127 RegisterFunction("unmount", UnmountFn);
1128 RegisterFunction("format", FormatFn);
1129 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001130 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001131 RegisterFunction("delete", DeleteFn);
1132 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001133 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1134 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001135 RegisterFunction("symlink", SymlinkFn);
1136 RegisterFunction("set_perm", SetPermFn);
1137 RegisterFunction("set_perm_recursive", SetPermFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001138
1139 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001140 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001141 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001142
1143 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001144 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1145 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001146
Doug Zongker512536a2010-02-17 16:11:44 -08001147 RegisterFunction("read_file", ReadFileFn);
1148 RegisterFunction("sha1_check", Sha1CheckFn);
1149
Doug Zongkerd0181b82011-10-19 10:51:12 -07001150 RegisterFunction("wipe_cache", WipeCacheFn);
1151
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001152 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001153
1154 RegisterFunction("run_program", RunProgramFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001155}