blob: c5aa77c49267fa676cf33035d2a6334285abe24f [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#ifdef HAVE_SELINUX
82 char *secontext = NULL;
83
84 if (sehandle) {
85 selabel_lookup(sehandle, &secontext, mount_point, 0755);
86 setfscreatecon(secontext);
87 }
88#endif
89
Doug Zongker9931f7f2009-06-10 14:11:53 -070090 mkdir(mount_point, 0755);
91
Stephen Smalley779701d2012-02-09 14:13:23 -050092#ifdef HAVE_SELINUX
93 if (secontext) {
94 freecon(secontext);
95 setfscreatecon(NULL);
96 }
97#endif
98
Doug Zongker3d177d02010-07-01 09:18:44 -070099 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700100 mtd_scan_partitions();
101 const MtdPartition* mtd;
102 mtd = mtd_find_partition_by_name(location);
103 if (mtd == NULL) {
104 fprintf(stderr, "%s: no mtd partition named \"%s\"",
105 name, location);
106 result = strdup("");
107 goto done;
108 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700109 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700110 fprintf(stderr, "mtd mount of %s failed: %s\n",
111 location, strerror(errno));
112 result = strdup("");
113 goto done;
114 }
115 result = mount_point;
116 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700117 if (mount(location, mount_point, fs_type,
Doug Zongker9931f7f2009-06-10 14:11:53 -0700118 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
Doug Zongker60babf82009-09-18 15:11:24 -0700119 fprintf(stderr, "%s: failed to mount %s at %s: %s\n",
120 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700121 result = strdup("");
122 } else {
123 result = mount_point;
124 }
125 }
126
127done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700128 free(fs_type);
129 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700130 free(location);
131 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800132 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700133}
134
Doug Zongker8edb00c2009-06-11 17:21:44 -0700135
136// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800137Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700138 char* result = NULL;
139 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700140 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700141 }
142 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700143 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700144 return NULL;
145 }
146 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700147 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700148 goto done;
149 }
150
151 scan_mounted_volumes();
152 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
153 if (vol == NULL) {
154 result = strdup("");
155 } else {
156 result = mount_point;
157 }
158
159done:
160 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800161 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700162}
163
164
Doug Zongker512536a2010-02-17 16:11:44 -0800165Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700166 char* result = NULL;
167 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700168 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700169 }
170 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700171 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700172 return NULL;
173 }
174 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700175 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700176 goto done;
177 }
178
179 scan_mounted_volumes();
180 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
181 if (vol == NULL) {
182 fprintf(stderr, "unmount of %s failed; no such volume\n", mount_point);
183 result = strdup("");
184 } else {
185 unmount_mounted_volume(vol);
186 result = mount_point;
187 }
188
189done:
190 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800191 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700192}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700193
194
Stephen Smalley779701d2012-02-09 14:13:23 -0500195// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700196//
Stephen Smalley779701d2012-02-09 14:13:23 -0500197// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
198// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800199// if fs_size == 0, then make_ext4fs uses the entire partition.
200// if fs_size > 0, that is the size to use
201// if fs_size < 0, then reserve that many bytes at the end of the partition
Doug Zongker512536a2010-02-17 16:11:44 -0800202Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700203 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400204 if (argc != 5) {
205 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700206 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700207 char* fs_type;
208 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700209 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800210 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400211 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500212
Stephen Smalley779701d2012-02-09 14:13:23 -0500213 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
214 return NULL;
215 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700216
Doug Zongker3d177d02010-07-01 09:18:44 -0700217 if (strlen(fs_type) == 0) {
218 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
219 goto done;
220 }
221 if (strlen(partition_type) == 0) {
222 ErrorAbort(state, "partition_type argument to %s() can't be empty",
223 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700224 goto done;
225 }
226 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700227 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700228 goto done;
229 }
230
Stephen Smalley516e4e22012-04-03 13:35:11 -0400231 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500232 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
233 goto done;
234 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500235
Doug Zongker3d177d02010-07-01 09:18:44 -0700236 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700237 mtd_scan_partitions();
238 const MtdPartition* mtd = mtd_find_partition_by_name(location);
239 if (mtd == NULL) {
240 fprintf(stderr, "%s: no mtd partition named \"%s\"",
241 name, location);
242 result = strdup("");
243 goto done;
244 }
245 MtdWriteContext* ctx = mtd_write_partition(mtd);
246 if (ctx == NULL) {
247 fprintf(stderr, "%s: can't write \"%s\"", name, location);
248 result = strdup("");
249 goto done;
250 }
251 if (mtd_erase_blocks(ctx, -1) == -1) {
252 mtd_write_close(ctx);
253 fprintf(stderr, "%s: failed to erase \"%s\"", name, location);
254 result = strdup("");
255 goto done;
256 }
257 if (mtd_write_close(ctx) != 0) {
258 fprintf(stderr, "%s: failed to close \"%s\"", name, location);
259 result = strdup("");
260 goto done;
261 }
262 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700263#ifdef USE_EXT4
264 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500265 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700266 if (status != 0) {
267 fprintf(stderr, "%s: make_ext4fs failed (%d) on %s",
268 name, status, location);
269 result = strdup("");
270 goto done;
271 }
272 result = location;
273#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700274 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700275 fprintf(stderr, "%s: unsupported fs_type \"%s\" partition_type \"%s\"",
276 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700277 }
278
279done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700280 free(fs_type);
281 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700282 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800283 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700284}
285
Doug Zongker8edb00c2009-06-11 17:21:44 -0700286
Doug Zongker512536a2010-02-17 16:11:44 -0800287Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700288 char** paths = malloc(argc * sizeof(char*));
289 int i;
290 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700291 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700292 if (paths[i] == NULL) {
293 int j;
294 for (j = 0; j < i; ++i) {
295 free(paths[j]);
296 }
297 free(paths);
298 return NULL;
299 }
300 }
301
302 bool recursive = (strcmp(name, "delete_recursive") == 0);
303
304 int success = 0;
305 for (i = 0; i < argc; ++i) {
306 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
307 ++success;
308 free(paths[i]);
309 }
310 free(paths);
311
312 char buffer[10];
313 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800314 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700315}
316
Doug Zongker8edb00c2009-06-11 17:21:44 -0700317
Doug Zongker512536a2010-02-17 16:11:44 -0800318Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700319 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700320 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700321 }
322 char* frac_str;
323 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700324 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700325 return NULL;
326 }
327
328 double frac = strtod(frac_str, NULL);
329 int sec = strtol(sec_str, NULL, 10);
330
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700331 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700332 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
333
Doug Zongker9931f7f2009-06-10 14:11:53 -0700334 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800335 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700336}
337
Doug Zongker512536a2010-02-17 16:11:44 -0800338Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700339 if (argc != 1) {
340 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
341 }
342 char* frac_str;
343 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
344 return NULL;
345 }
346
347 double frac = strtod(frac_str, NULL);
348
349 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
350 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
351
Doug Zongker512536a2010-02-17 16:11:44 -0800352 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700353}
354
Doug Zongker8edb00c2009-06-11 17:21:44 -0700355// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800356Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700357 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700358 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700359 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700360 }
361 char* zip_path;
362 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700363 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700364
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700365 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700366
367 // To create a consistent system image, never use the clock for timestamps.
368 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
369
370 bool success = mzExtractRecursive(za, zip_path, dest_path,
371 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500372 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700373 free(zip_path);
374 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800375 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700376}
377
Doug Zongker8edb00c2009-06-11 17:21:44 -0700378
379// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800380// or
381// package_extract_file(package_path)
382// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800383// function (the char* returned is actually a FileContents*).
384Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700385 int argc, Expr* argv[]) {
Doug Zongker6aece332010-02-01 14:40:12 -0800386 if (argc != 1 && argc != 2) {
387 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
388 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700389 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700390 bool success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800391 if (argc == 2) {
392 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700393
Doug Zongker6aece332010-02-01 14:40:12 -0800394 char* zip_path;
395 char* dest_path;
396 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
397
398 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
399 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
400 if (entry == NULL) {
401 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
402 goto done2;
403 }
404
405 FILE* f = fopen(dest_path, "wb");
406 if (f == NULL) {
407 fprintf(stderr, "%s: can't open %s for write: %s\n",
408 name, dest_path, strerror(errno));
409 goto done2;
410 }
411 success = mzExtractZipEntryToFile(za, entry, fileno(f));
412 fclose(f);
413
414 done2:
415 free(zip_path);
416 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800417 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800418 } else {
419 // The one-argument version returns the contents of the file
420 // as the result.
421
422 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800423 Value* v = malloc(sizeof(Value));
424 v->type = VAL_BLOB;
425 v->size = -1;
426 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800427
428 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
429
430 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
431 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
432 if (entry == NULL) {
433 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
434 goto done1;
435 }
436
Doug Zongker512536a2010-02-17 16:11:44 -0800437 v->size = mzGetZipEntryUncompLen(entry);
438 v->data = malloc(v->size);
439 if (v->data == NULL) {
Doug Zongker6aece332010-02-01 14:40:12 -0800440 fprintf(stderr, "%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800441 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800442 goto done1;
443 }
444
Doug Zongker512536a2010-02-17 16:11:44 -0800445 success = mzExtractZipEntryToBuffer(za, entry,
446 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800447
448 done1:
449 free(zip_path);
450 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800451 free(v->data);
452 v->data = NULL;
453 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800454 }
Doug Zongker512536a2010-02-17 16:11:44 -0800455 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700456 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700457}
458
459
Doug Zongker9931f7f2009-06-10 14:11:53 -0700460// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700461// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800462Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700463 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700464 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700465 }
466 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700467 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700468 if (target == NULL) return NULL;
469
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700470 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700471 if (srcs == NULL) {
472 free(target);
473 return NULL;
474 }
475
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700476 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700477 int i;
478 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700479 if (unlink(srcs[i]) < 0) {
480 if (errno != ENOENT) {
481 fprintf(stderr, "%s: failed to remove %s: %s\n",
482 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700483 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700484 }
485 }
486 if (symlink(target, srcs[i]) < 0) {
487 fprintf(stderr, "%s: failed to symlink %s to %s: %s\n",
488 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700489 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700490 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700491 free(srcs[i]);
492 }
493 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700494 if (bad) {
495 return ErrorAbort(state, "%s: some symlinks failed", name);
496 }
Doug Zongker512536a2010-02-17 16:11:44 -0800497 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700498}
499
Doug Zongker8edb00c2009-06-11 17:21:44 -0700500
Doug Zongker512536a2010-02-17 16:11:44 -0800501Value* SetPermFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700502 char* result = NULL;
503 bool recursive = (strcmp(name, "set_perm_recursive") == 0);
504
505 int min_args = 4 + (recursive ? 1 : 0);
506 if (argc < min_args) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700507 return ErrorAbort(state, "%s() expects %d+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700508 }
509
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700510 char** args = ReadVarArgs(state, argc, argv);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700511 if (args == NULL) return NULL;
512
513 char* end;
514 int i;
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700515 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700516
517 int uid = strtoul(args[0], &end, 0);
518 if (*end != '\0' || args[0][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700519 ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700520 goto done;
521 }
522
523 int gid = strtoul(args[1], &end, 0);
524 if (*end != '\0' || args[1][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700525 ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700526 goto done;
527 }
528
529 if (recursive) {
530 int dir_mode = strtoul(args[2], &end, 0);
531 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700532 ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700533 goto done;
534 }
535
536 int file_mode = strtoul(args[3], &end, 0);
537 if (*end != '\0' || args[3][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700538 ErrorAbort(state, "%s: \"%s\" not a valid filemode",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700539 name, args[3]);
540 goto done;
541 }
542
543 for (i = 4; i < argc; ++i) {
544 dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode);
545 }
546 } else {
547 int mode = strtoul(args[2], &end, 0);
548 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700549 ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700550 goto done;
551 }
552
Doug Zongker0bbfe3d2009-06-25 13:37:31 -0700553 for (i = 3; i < argc; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700554 if (chown(args[i], uid, gid) < 0) {
555 fprintf(stderr, "%s: chown of %s to %d %d failed: %s\n",
556 name, args[i], uid, gid, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700557 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700558 }
559 if (chmod(args[i], mode) < 0) {
560 fprintf(stderr, "%s: chmod of %s to %o failed: %s\n",
561 name, args[i], mode, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700562 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700563 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700564 }
565 }
566 result = strdup("");
567
568done:
569 for (i = 0; i < argc; ++i) {
570 free(args[i]);
571 }
572 free(args);
573
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700574 if (bad) {
575 free(result);
576 return ErrorAbort(state, "%s: some changes failed", name);
577 }
Doug Zongker512536a2010-02-17 16:11:44 -0800578 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700579}
580
Doug Zongker8edb00c2009-06-11 17:21:44 -0700581
Doug Zongker512536a2010-02-17 16:11:44 -0800582Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700583 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700584 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700585 }
586 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700587 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700588 if (key == NULL) return NULL;
589
590 char value[PROPERTY_VALUE_MAX];
591 property_get(key, value, "");
592 free(key);
593
Doug Zongker512536a2010-02-17 16:11:44 -0800594 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700595}
596
597
Doug Zongker47cace92009-06-18 10:11:50 -0700598// file_getprop(file, key)
599//
600// interprets 'file' as a getprop-style file (key=value pairs, one
601// per line, # comment lines and blank lines okay), and returns the value
602// for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800603Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700604 char* result = NULL;
605 char* buffer = NULL;
606 char* filename;
607 char* key;
608 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
609 return NULL;
610 }
611
612 struct stat st;
613 if (stat(filename, &st) < 0) {
614 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
615 name, filename, strerror(errno));
616 goto done;
617 }
618
619#define MAX_FILE_GETPROP_SIZE 65536
620
621 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
622 ErrorAbort(state, "%s too large for %s (max %d)",
623 filename, name, MAX_FILE_GETPROP_SIZE);
624 goto done;
625 }
626
627 buffer = malloc(st.st_size+1);
628 if (buffer == NULL) {
629 ErrorAbort(state, "%s: failed to alloc %d bytes", name, st.st_size+1);
630 goto done;
631 }
632
633 FILE* f = fopen(filename, "rb");
634 if (f == NULL) {
635 ErrorAbort(state, "%s: failed to open %s: %s",
636 name, filename, strerror(errno));
637 goto done;
638 }
639
640 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
641 ErrorAbort(state, "%s: failed to read %d bytes from %s",
642 name, st.st_size+1, filename);
643 fclose(f);
644 goto done;
645 }
646 buffer[st.st_size] = '\0';
647
648 fclose(f);
649
650 char* line = strtok(buffer, "\n");
651 do {
652 // skip whitespace at start of line
653 while (*line && isspace(*line)) ++line;
654
655 // comment or blank line: skip to next line
656 if (*line == '\0' || *line == '#') continue;
657
658 char* equal = strchr(line, '=');
659 if (equal == NULL) {
660 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
661 name, line, filename);
662 goto done;
663 }
664
665 // trim whitespace between key and '='
666 char* key_end = equal-1;
667 while (key_end > line && isspace(*key_end)) --key_end;
668 key_end[1] = '\0';
669
670 // not the key we're looking for
671 if (strcmp(key, line) != 0) continue;
672
673 // skip whitespace after the '=' to the start of the value
674 char* val_start = equal+1;
675 while(*val_start && isspace(*val_start)) ++val_start;
676
677 // trim trailing whitespace
678 char* val_end = val_start + strlen(val_start)-1;
679 while (val_end > val_start && isspace(*val_end)) --val_end;
680 val_end[1] = '\0';
681
682 result = strdup(val_start);
683 break;
684
685 } while ((line = strtok(NULL, "\n")));
686
687 if (result == NULL) result = strdup("");
688
689 done:
690 free(filename);
691 free(key);
692 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800693 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -0700694}
695
696
Doug Zongker8edb00c2009-06-11 17:21:44 -0700697static bool write_raw_image_cb(const unsigned char* data,
698 int data_len, void* ctx) {
699 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
700 if (r == data_len) return true;
701 fprintf(stderr, "%s\n", strerror(errno));
702 return false;
703}
704
Doug Zongker179b2d92011-04-12 15:49:04 -0700705// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -0800706Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700707 char* result = NULL;
708
Doug Zongker179b2d92011-04-12 15:49:04 -0700709 Value* partition_value;
710 Value* contents;
711 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700712 return NULL;
713 }
714
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700715 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -0700716 if (partition_value->type != VAL_STRING) {
717 ErrorAbort(state, "partition argument to %s must be string", name);
718 goto done;
719 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700720 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700721 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700722 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700723 goto done;
724 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700725 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700726 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700727 goto done;
728 }
729
Dees_Troy76a1d412013-04-20 13:54:35 +0000730 char* filename = contents->data;
731 if (0 == restore_raw_partition(NULL, partition, filename))
732 result = strdup(partition);
733 else {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700734 result = strdup("");
735 goto done;
736 }
737
Doug Zongker8edb00c2009-06-11 17:21:44 -0700738done:
Doug Zongker179b2d92011-04-12 15:49:04 -0700739 if (result != partition) FreeValue(partition_value);
740 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -0800741 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700742}
743
Doug Zongker8edb00c2009-06-11 17:21:44 -0700744// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -0800745Value* ApplyPatchSpaceFn(const char* name, State* state,
746 int argc, Expr* argv[]) {
747 char* bytes_str;
748 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
749 return NULL;
750 }
751
752 char* endptr;
753 size_t bytes = strtol(bytes_str, &endptr, 10);
754 if (bytes == 0 && endptr == bytes_str) {
755 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
756 name, bytes_str);
757 free(bytes_str);
758 return NULL;
759 }
760
761 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
762}
763
764
765// apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1_1, patch_1, ...)
Doug Zongker512536a2010-02-17 16:11:44 -0800766Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800767 if (argc < 6 || (argc % 2) == 1) {
768 return ErrorAbort(state, "%s(): expected at least 6 args and an "
769 "even number, got %d",
770 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700771 }
772
Doug Zongkerc4351c72010-02-22 14:46:32 -0800773 char* source_filename;
774 char* target_filename;
775 char* target_sha1;
776 char* target_size_str;
777 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
778 &target_sha1, &target_size_str) < 0) {
779 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700780 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700781
Doug Zongkerc4351c72010-02-22 14:46:32 -0800782 char* endptr;
783 size_t target_size = strtol(target_size_str, &endptr, 10);
784 if (target_size == 0 && endptr == target_size_str) {
785 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
786 name, target_size_str);
787 free(source_filename);
788 free(target_filename);
789 free(target_sha1);
790 free(target_size_str);
791 return NULL;
792 }
793
794 int patchcount = (argc-4) / 2;
795 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700796
797 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800798 for (i = 0; i < patchcount; ++i) {
799 if (patches[i*2]->type != VAL_STRING) {
800 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
801 break;
802 }
803 if (patches[i*2+1]->type != VAL_BLOB) {
804 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
805 break;
806 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700807 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800808 if (i != patchcount) {
809 for (i = 0; i < patchcount*2; ++i) {
810 FreeValue(patches[i]);
811 }
812 free(patches);
813 return NULL;
814 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700815
Doug Zongkerc4351c72010-02-22 14:46:32 -0800816 char** patch_sha_str = malloc(patchcount * sizeof(char*));
817 for (i = 0; i < patchcount; ++i) {
818 patch_sha_str[i] = patches[i*2]->data;
819 patches[i*2]->data = NULL;
820 FreeValue(patches[i*2]);
821 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700822 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800823
824 int result = applypatch(source_filename, target_filename,
825 target_sha1, target_size,
826 patchcount, patch_sha_str, patches);
827
828 for (i = 0; i < patchcount; ++i) {
829 FreeValue(patches[i]);
830 }
831 free(patch_sha_str);
832 free(patches);
833
834 return StringValue(strdup(result == 0 ? "t" : ""));
835}
836
837// apply_patch_check(file, [sha1_1, ...])
838Value* ApplyPatchCheckFn(const char* name, State* state,
839 int argc, Expr* argv[]) {
840 if (argc < 1) {
841 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
842 name, argc);
843 }
844
845 char* filename;
846 if (ReadArgs(state, argv, 1, &filename) < 0) {
847 return NULL;
848 }
849
850 int patchcount = argc-1;
851 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
852
853 int result = applypatch_check(filename, patchcount, sha1s);
854
855 int i;
856 for (i = 0; i < patchcount; ++i) {
857 free(sha1s[i]);
858 }
859 free(sha1s);
860
861 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700862}
863
Doug Zongker512536a2010-02-17 16:11:44 -0800864Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700865 char** args = ReadVarArgs(state, argc, argv);
866 if (args == NULL) {
867 return NULL;
868 }
869
870 int size = 0;
871 int i;
872 for (i = 0; i < argc; ++i) {
873 size += strlen(args[i]);
874 }
875 char* buffer = malloc(size+1);
876 size = 0;
877 for (i = 0; i < argc; ++i) {
878 strcpy(buffer+size, args[i]);
879 size += strlen(args[i]);
880 free(args[i]);
881 }
882 free(args);
883 buffer[size] = '\0';
884
885 char* line = strtok(buffer, "\n");
886 while (line) {
887 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
888 "ui_print %s\n", line);
889 line = strtok(NULL, "\n");
890 }
891 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
892
Doug Zongker512536a2010-02-17 16:11:44 -0800893 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700894}
895
Doug Zongkerd0181b82011-10-19 10:51:12 -0700896Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
897 if (argc != 0) {
898 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
899 }
900 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
901 return StringValue(strdup("t"));
902}
903
Doug Zongker512536a2010-02-17 16:11:44 -0800904Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700905 if (argc < 1) {
906 return ErrorAbort(state, "%s() expects at least 1 arg", name);
907 }
908 char** args = ReadVarArgs(state, argc, argv);
909 if (args == NULL) {
910 return NULL;
911 }
912
913 char** args2 = malloc(sizeof(char*) * (argc+1));
914 memcpy(args2, args, sizeof(char*) * argc);
915 args2[argc] = NULL;
916
917 fprintf(stderr, "about to run program [%s] with %d args\n", args2[0], argc);
918
919 pid_t child = fork();
920 if (child == 0) {
921 execv(args2[0], args2);
922 fprintf(stderr, "run_program: execv failed: %s\n", strerror(errno));
923 _exit(1);
924 }
925 int status;
926 waitpid(child, &status, 0);
927 if (WIFEXITED(status)) {
928 if (WEXITSTATUS(status) != 0) {
929 fprintf(stderr, "run_program: child exited with status %d\n",
930 WEXITSTATUS(status));
931 }
932 } else if (WIFSIGNALED(status)) {
933 fprintf(stderr, "run_program: child terminated by signal %d\n",
934 WTERMSIG(status));
935 }
936
937 int i;
938 for (i = 0; i < argc; ++i) {
939 free(args[i]);
940 }
941 free(args);
942 free(args2);
943
944 char buffer[20];
945 sprintf(buffer, "%d", status);
946
Doug Zongker512536a2010-02-17 16:11:44 -0800947 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700948}
949
Doug Zongker512536a2010-02-17 16:11:44 -0800950// Take a sha-1 digest and return it as a newly-allocated hex string.
951static char* PrintSha1(uint8_t* digest) {
952 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
953 int i;
954 const char* alphabet = "0123456789abcdef";
955 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
956 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
957 buffer[i*2+1] = alphabet[digest[i] & 0xf];
958 }
959 buffer[i*2] = '\0';
960 return buffer;
961}
962
963// sha1_check(data)
964// to return the sha1 of the data (given in the format returned by
965// read_file).
966//
967// sha1_check(data, sha1_hex, [sha1_hex, ...])
968// returns the sha1 of the file if it matches any of the hex
969// strings passed, or "" if it does not equal any of them.
970//
971Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
972 if (argc < 1) {
973 return ErrorAbort(state, "%s() expects at least 1 arg", name);
974 }
975
976 Value** args = ReadValueVarArgs(state, argc, argv);
977 if (args == NULL) {
978 return NULL;
979 }
980
981 if (args[0]->size < 0) {
982 fprintf(stderr, "%s(): no file contents received", name);
983 return StringValue(strdup(""));
984 }
985 uint8_t digest[SHA_DIGEST_SIZE];
986 SHA(args[0]->data, args[0]->size, digest);
987 FreeValue(args[0]);
988
989 if (argc == 1) {
990 return StringValue(PrintSha1(digest));
991 }
992
993 int i;
994 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
995 for (i = 1; i < argc; ++i) {
996 if (args[i]->type != VAL_STRING) {
997 fprintf(stderr, "%s(): arg %d is not a string; skipping",
998 name, i);
999 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1000 // Warn about bad args and skip them.
1001 fprintf(stderr, "%s(): error parsing \"%s\" as sha-1; skipping",
1002 name, args[i]->data);
1003 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1004 break;
1005 }
1006 FreeValue(args[i]);
1007 }
1008 if (i >= argc) {
1009 // Didn't match any of the hex strings; return false.
1010 return StringValue(strdup(""));
1011 }
1012 // Found a match; free all the remaining arguments and return the
1013 // matched one.
1014 int j;
1015 for (j = i+1; j < argc; ++j) {
1016 FreeValue(args[j]);
1017 }
1018 return args[i];
1019}
1020
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001021// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001022// is actually a FileContents*).
1023Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1024 if (argc != 1) {
1025 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1026 }
1027 char* filename;
1028 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1029
1030 Value* v = malloc(sizeof(Value));
1031 v->type = VAL_BLOB;
1032
1033 FileContents fc;
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001034 if (LoadFileContents(filename, &fc, RETOUCH_DONT_MASK) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001035 ErrorAbort(state, "%s() loading \"%s\" failed: %s",
1036 name, filename, strerror(errno));
1037 free(filename);
1038 free(v);
1039 free(fc.data);
1040 return NULL;
1041 }
1042
1043 v->size = fc.size;
1044 v->data = (char*)fc.data;
1045
1046 free(filename);
1047 return v;
1048}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001049
Doug Zongker9931f7f2009-06-10 14:11:53 -07001050void RegisterInstallFunctions() {
1051 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001052 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001053 RegisterFunction("unmount", UnmountFn);
1054 RegisterFunction("format", FormatFn);
1055 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001056 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001057 RegisterFunction("delete", DeleteFn);
1058 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001059 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1060 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001061 RegisterFunction("symlink", SymlinkFn);
1062 RegisterFunction("set_perm", SetPermFn);
1063 RegisterFunction("set_perm_recursive", SetPermFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001064
1065 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001066 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001067 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001068
1069 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001070 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1071 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001072
Doug Zongker512536a2010-02-17 16:11:44 -08001073 RegisterFunction("read_file", ReadFileFn);
1074 RegisterFunction("sha1_check", Sha1CheckFn);
1075
Doug Zongkerd0181b82011-10-19 10:51:12 -07001076 RegisterFunction("wipe_cache", WipeCacheFn);
1077
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001078 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001079
1080 RegisterFunction("run_program", RunProgramFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001081}