blob: 9ace1254146a831179d1301db07defdf352422e1 [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
455
Doug Zongker9931f7f2009-06-10 14:11:53 -0700456// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700457// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800458Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700459 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700460 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700461 }
462 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700463 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700464 if (target == NULL) return NULL;
465
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700466 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700467 if (srcs == NULL) {
468 free(target);
469 return NULL;
470 }
471
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700472 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700473 int i;
474 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700475 if (unlink(srcs[i]) < 0) {
476 if (errno != ENOENT) {
477 fprintf(stderr, "%s: failed to remove %s: %s\n",
478 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700479 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700480 }
481 }
482 if (symlink(target, srcs[i]) < 0) {
483 fprintf(stderr, "%s: failed to symlink %s to %s: %s\n",
484 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700485 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700486 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700487 free(srcs[i]);
488 }
489 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700490 if (bad) {
491 return ErrorAbort(state, "%s: some symlinks failed", name);
492 }
Doug Zongker512536a2010-02-17 16:11:44 -0800493 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700494}
495
Doug Zongker8edb00c2009-06-11 17:21:44 -0700496
Doug Zongker512536a2010-02-17 16:11:44 -0800497Value* SetPermFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700498 char* result = NULL;
499 bool recursive = (strcmp(name, "set_perm_recursive") == 0);
500
501 int min_args = 4 + (recursive ? 1 : 0);
502 if (argc < min_args) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700503 return ErrorAbort(state, "%s() expects %d+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700504 }
505
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700506 char** args = ReadVarArgs(state, argc, argv);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700507 if (args == NULL) return NULL;
508
509 char* end;
510 int i;
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700511 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700512
513 int uid = strtoul(args[0], &end, 0);
514 if (*end != '\0' || args[0][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700515 ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700516 goto done;
517 }
518
519 int gid = strtoul(args[1], &end, 0);
520 if (*end != '\0' || args[1][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700521 ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700522 goto done;
523 }
524
525 if (recursive) {
526 int dir_mode = strtoul(args[2], &end, 0);
527 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700528 ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700529 goto done;
530 }
531
532 int file_mode = strtoul(args[3], &end, 0);
533 if (*end != '\0' || args[3][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700534 ErrorAbort(state, "%s: \"%s\" not a valid filemode",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700535 name, args[3]);
536 goto done;
537 }
538
539 for (i = 4; i < argc; ++i) {
540 dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode);
541 }
542 } else {
543 int mode = strtoul(args[2], &end, 0);
544 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700545 ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700546 goto done;
547 }
548
Doug Zongker0bbfe3d2009-06-25 13:37:31 -0700549 for (i = 3; i < argc; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700550 if (chown(args[i], uid, gid) < 0) {
551 fprintf(stderr, "%s: chown of %s to %d %d failed: %s\n",
552 name, args[i], uid, gid, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700553 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700554 }
555 if (chmod(args[i], mode) < 0) {
556 fprintf(stderr, "%s: chmod of %s to %o failed: %s\n",
557 name, args[i], mode, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700558 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700559 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700560 }
561 }
562 result = strdup("");
563
564done:
565 for (i = 0; i < argc; ++i) {
566 free(args[i]);
567 }
568 free(args);
569
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700570 if (bad) {
571 free(result);
572 return ErrorAbort(state, "%s: some changes failed", name);
573 }
Doug Zongker512536a2010-02-17 16:11:44 -0800574 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700575}
576
Doug Zongker8edb00c2009-06-11 17:21:44 -0700577
Doug Zongker512536a2010-02-17 16:11:44 -0800578Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700579 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700580 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700581 }
582 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700583 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700584 if (key == NULL) return NULL;
585
586 char value[PROPERTY_VALUE_MAX];
587 property_get(key, value, "");
588 free(key);
589
Doug Zongker512536a2010-02-17 16:11:44 -0800590 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700591}
592
593
Doug Zongker47cace92009-06-18 10:11:50 -0700594// file_getprop(file, key)
595//
596// interprets 'file' as a getprop-style file (key=value pairs, one
597// per line, # comment lines and blank lines okay), and returns the value
598// for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800599Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700600 char* result = NULL;
601 char* buffer = NULL;
602 char* filename;
603 char* key;
604 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
605 return NULL;
606 }
607
608 struct stat st;
609 if (stat(filename, &st) < 0) {
610 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
611 name, filename, strerror(errno));
612 goto done;
613 }
614
615#define MAX_FILE_GETPROP_SIZE 65536
616
617 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
618 ErrorAbort(state, "%s too large for %s (max %d)",
619 filename, name, MAX_FILE_GETPROP_SIZE);
620 goto done;
621 }
622
623 buffer = malloc(st.st_size+1);
624 if (buffer == NULL) {
625 ErrorAbort(state, "%s: failed to alloc %d bytes", name, st.st_size+1);
626 goto done;
627 }
628
629 FILE* f = fopen(filename, "rb");
630 if (f == NULL) {
631 ErrorAbort(state, "%s: failed to open %s: %s",
632 name, filename, strerror(errno));
633 goto done;
634 }
635
636 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
637 ErrorAbort(state, "%s: failed to read %d bytes from %s",
638 name, st.st_size+1, filename);
639 fclose(f);
640 goto done;
641 }
642 buffer[st.st_size] = '\0';
643
644 fclose(f);
645
646 char* line = strtok(buffer, "\n");
647 do {
648 // skip whitespace at start of line
649 while (*line && isspace(*line)) ++line;
650
651 // comment or blank line: skip to next line
652 if (*line == '\0' || *line == '#') continue;
653
654 char* equal = strchr(line, '=');
655 if (equal == NULL) {
656 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
657 name, line, filename);
658 goto done;
659 }
660
661 // trim whitespace between key and '='
662 char* key_end = equal-1;
663 while (key_end > line && isspace(*key_end)) --key_end;
664 key_end[1] = '\0';
665
666 // not the key we're looking for
667 if (strcmp(key, line) != 0) continue;
668
669 // skip whitespace after the '=' to the start of the value
670 char* val_start = equal+1;
671 while(*val_start && isspace(*val_start)) ++val_start;
672
673 // trim trailing whitespace
674 char* val_end = val_start + strlen(val_start)-1;
675 while (val_end > val_start && isspace(*val_end)) --val_end;
676 val_end[1] = '\0';
677
678 result = strdup(val_start);
679 break;
680
681 } while ((line = strtok(NULL, "\n")));
682
683 if (result == NULL) result = strdup("");
684
685 done:
686 free(filename);
687 free(key);
688 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800689 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -0700690}
691
692
Doug Zongker8edb00c2009-06-11 17:21:44 -0700693static bool write_raw_image_cb(const unsigned char* data,
694 int data_len, void* ctx) {
695 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
696 if (r == data_len) return true;
697 fprintf(stderr, "%s\n", strerror(errno));
698 return false;
699}
700
Doug Zongker179b2d92011-04-12 15:49:04 -0700701// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -0800702Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700703 char* result = NULL;
704
Doug Zongker179b2d92011-04-12 15:49:04 -0700705 Value* partition_value;
706 Value* contents;
707 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700708 return NULL;
709 }
710
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700711 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -0700712 if (partition_value->type != VAL_STRING) {
713 ErrorAbort(state, "partition argument to %s must be string", name);
714 goto done;
715 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700716 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700717 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700718 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700719 goto done;
720 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700721 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700722 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700723 goto done;
724 }
725
726 mtd_scan_partitions();
727 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
728 if (mtd == NULL) {
729 fprintf(stderr, "%s: no mtd partition named \"%s\"\n", name, partition);
730 result = strdup("");
731 goto done;
732 }
733
734 MtdWriteContext* ctx = mtd_write_partition(mtd);
735 if (ctx == NULL) {
736 fprintf(stderr, "%s: can't write mtd partition \"%s\"\n",
737 name, partition);
738 result = strdup("");
739 goto done;
740 }
741
742 bool success;
743
Doug Zongker179b2d92011-04-12 15:49:04 -0700744 if (contents->type == VAL_STRING) {
745 // we're given a filename as the contents
746 char* filename = contents->data;
747 FILE* f = fopen(filename, "rb");
748 if (f == NULL) {
749 fprintf(stderr, "%s: can't open %s: %s\n",
750 name, filename, strerror(errno));
751 result = strdup("");
752 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700753 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700754
755 success = true;
756 char* buffer = malloc(BUFSIZ);
757 int read;
758 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
759 int wrote = mtd_write_data(ctx, buffer, read);
760 success = success && (wrote == read);
761 }
762 free(buffer);
763 fclose(f);
764 } else {
765 // we're given a blob as the contents
766 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
767 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700768 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700769 if (!success) {
770 fprintf(stderr, "mtd_write_data to %s failed: %s\n",
771 partition, strerror(errno));
772 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700773
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700774 if (mtd_erase_blocks(ctx, -1) == -1) {
775 fprintf(stderr, "%s: error erasing blocks of %s\n", name, partition);
776 }
777 if (mtd_write_close(ctx) != 0) {
778 fprintf(stderr, "%s: error closing write of %s\n", name, partition);
779 }
780
Doug Zongker179b2d92011-04-12 15:49:04 -0700781 printf("%s %s partition\n",
782 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700783
784 result = success ? partition : strdup("");
785
786done:
Doug Zongker179b2d92011-04-12 15:49:04 -0700787 if (result != partition) FreeValue(partition_value);
788 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -0800789 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700790}
791
Doug Zongker8edb00c2009-06-11 17:21:44 -0700792// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -0800793Value* ApplyPatchSpaceFn(const char* name, State* state,
794 int argc, Expr* argv[]) {
795 char* bytes_str;
796 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
797 return NULL;
798 }
799
800 char* endptr;
801 size_t bytes = strtol(bytes_str, &endptr, 10);
802 if (bytes == 0 && endptr == bytes_str) {
803 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
804 name, bytes_str);
805 free(bytes_str);
806 return NULL;
807 }
808
809 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
810}
811
812
813// apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1_1, patch_1, ...)
Doug Zongker512536a2010-02-17 16:11:44 -0800814Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800815 if (argc < 6 || (argc % 2) == 1) {
816 return ErrorAbort(state, "%s(): expected at least 6 args and an "
817 "even number, got %d",
818 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700819 }
820
Doug Zongkerc4351c72010-02-22 14:46:32 -0800821 char* source_filename;
822 char* target_filename;
823 char* target_sha1;
824 char* target_size_str;
825 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
826 &target_sha1, &target_size_str) < 0) {
827 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700828 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700829
Doug Zongkerc4351c72010-02-22 14:46:32 -0800830 char* endptr;
831 size_t target_size = strtol(target_size_str, &endptr, 10);
832 if (target_size == 0 && endptr == target_size_str) {
833 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
834 name, target_size_str);
835 free(source_filename);
836 free(target_filename);
837 free(target_sha1);
838 free(target_size_str);
839 return NULL;
840 }
841
842 int patchcount = (argc-4) / 2;
843 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700844
845 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800846 for (i = 0; i < patchcount; ++i) {
847 if (patches[i*2]->type != VAL_STRING) {
848 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
849 break;
850 }
851 if (patches[i*2+1]->type != VAL_BLOB) {
852 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
853 break;
854 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700855 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800856 if (i != patchcount) {
857 for (i = 0; i < patchcount*2; ++i) {
858 FreeValue(patches[i]);
859 }
860 free(patches);
861 return NULL;
862 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700863
Doug Zongkerc4351c72010-02-22 14:46:32 -0800864 char** patch_sha_str = malloc(patchcount * sizeof(char*));
865 for (i = 0; i < patchcount; ++i) {
866 patch_sha_str[i] = patches[i*2]->data;
867 patches[i*2]->data = NULL;
868 FreeValue(patches[i*2]);
869 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700870 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800871
872 int result = applypatch(source_filename, target_filename,
873 target_sha1, target_size,
874 patchcount, patch_sha_str, patches);
875
876 for (i = 0; i < patchcount; ++i) {
877 FreeValue(patches[i]);
878 }
879 free(patch_sha_str);
880 free(patches);
881
882 return StringValue(strdup(result == 0 ? "t" : ""));
883}
884
885// apply_patch_check(file, [sha1_1, ...])
886Value* ApplyPatchCheckFn(const char* name, State* state,
887 int argc, Expr* argv[]) {
888 if (argc < 1) {
889 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
890 name, argc);
891 }
892
893 char* filename;
894 if (ReadArgs(state, argv, 1, &filename) < 0) {
895 return NULL;
896 }
897
898 int patchcount = argc-1;
899 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
900
901 int result = applypatch_check(filename, patchcount, sha1s);
902
903 int i;
904 for (i = 0; i < patchcount; ++i) {
905 free(sha1s[i]);
906 }
907 free(sha1s);
908
909 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700910}
911
Doug Zongker512536a2010-02-17 16:11:44 -0800912Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700913 char** args = ReadVarArgs(state, argc, argv);
914 if (args == NULL) {
915 return NULL;
916 }
917
918 int size = 0;
919 int i;
920 for (i = 0; i < argc; ++i) {
921 size += strlen(args[i]);
922 }
923 char* buffer = malloc(size+1);
924 size = 0;
925 for (i = 0; i < argc; ++i) {
926 strcpy(buffer+size, args[i]);
927 size += strlen(args[i]);
928 free(args[i]);
929 }
930 free(args);
931 buffer[size] = '\0';
932
933 char* line = strtok(buffer, "\n");
934 while (line) {
935 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
936 "ui_print %s\n", line);
937 line = strtok(NULL, "\n");
938 }
939 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
940
Doug Zongker512536a2010-02-17 16:11:44 -0800941 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700942}
943
Doug Zongkerd0181b82011-10-19 10:51:12 -0700944Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
945 if (argc != 0) {
946 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
947 }
948 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
949 return StringValue(strdup("t"));
950}
951
Doug Zongker512536a2010-02-17 16:11:44 -0800952Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700953 if (argc < 1) {
954 return ErrorAbort(state, "%s() expects at least 1 arg", name);
955 }
956 char** args = ReadVarArgs(state, argc, argv);
957 if (args == NULL) {
958 return NULL;
959 }
960
961 char** args2 = malloc(sizeof(char*) * (argc+1));
962 memcpy(args2, args, sizeof(char*) * argc);
963 args2[argc] = NULL;
964
965 fprintf(stderr, "about to run program [%s] with %d args\n", args2[0], argc);
966
967 pid_t child = fork();
968 if (child == 0) {
969 execv(args2[0], args2);
970 fprintf(stderr, "run_program: execv failed: %s\n", strerror(errno));
971 _exit(1);
972 }
973 int status;
974 waitpid(child, &status, 0);
975 if (WIFEXITED(status)) {
976 if (WEXITSTATUS(status) != 0) {
977 fprintf(stderr, "run_program: child exited with status %d\n",
978 WEXITSTATUS(status));
979 }
980 } else if (WIFSIGNALED(status)) {
981 fprintf(stderr, "run_program: child terminated by signal %d\n",
982 WTERMSIG(status));
983 }
984
985 int i;
986 for (i = 0; i < argc; ++i) {
987 free(args[i]);
988 }
989 free(args);
990 free(args2);
991
992 char buffer[20];
993 sprintf(buffer, "%d", status);
994
Doug Zongker512536a2010-02-17 16:11:44 -0800995 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700996}
997
Doug Zongker512536a2010-02-17 16:11:44 -0800998// Take a sha-1 digest and return it as a newly-allocated hex string.
999static char* PrintSha1(uint8_t* digest) {
1000 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
1001 int i;
1002 const char* alphabet = "0123456789abcdef";
1003 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
1004 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
1005 buffer[i*2+1] = alphabet[digest[i] & 0xf];
1006 }
1007 buffer[i*2] = '\0';
1008 return buffer;
1009}
1010
1011// sha1_check(data)
1012// to return the sha1 of the data (given in the format returned by
1013// read_file).
1014//
1015// sha1_check(data, sha1_hex, [sha1_hex, ...])
1016// returns the sha1 of the file if it matches any of the hex
1017// strings passed, or "" if it does not equal any of them.
1018//
1019Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1020 if (argc < 1) {
1021 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1022 }
1023
1024 Value** args = ReadValueVarArgs(state, argc, argv);
1025 if (args == NULL) {
1026 return NULL;
1027 }
1028
1029 if (args[0]->size < 0) {
1030 fprintf(stderr, "%s(): no file contents received", name);
1031 return StringValue(strdup(""));
1032 }
1033 uint8_t digest[SHA_DIGEST_SIZE];
1034 SHA(args[0]->data, args[0]->size, digest);
1035 FreeValue(args[0]);
1036
1037 if (argc == 1) {
1038 return StringValue(PrintSha1(digest));
1039 }
1040
1041 int i;
1042 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1043 for (i = 1; i < argc; ++i) {
1044 if (args[i]->type != VAL_STRING) {
1045 fprintf(stderr, "%s(): arg %d is not a string; skipping",
1046 name, i);
1047 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1048 // Warn about bad args and skip them.
1049 fprintf(stderr, "%s(): error parsing \"%s\" as sha-1; skipping",
1050 name, args[i]->data);
1051 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1052 break;
1053 }
1054 FreeValue(args[i]);
1055 }
1056 if (i >= argc) {
1057 // Didn't match any of the hex strings; return false.
1058 return StringValue(strdup(""));
1059 }
1060 // Found a match; free all the remaining arguments and return the
1061 // matched one.
1062 int j;
1063 for (j = i+1; j < argc; ++j) {
1064 FreeValue(args[j]);
1065 }
1066 return args[i];
1067}
1068
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001069// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001070// is actually a FileContents*).
1071Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1072 if (argc != 1) {
1073 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1074 }
1075 char* filename;
1076 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1077
1078 Value* v = malloc(sizeof(Value));
1079 v->type = VAL_BLOB;
1080
1081 FileContents fc;
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001082 if (LoadFileContents(filename, &fc, RETOUCH_DONT_MASK) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001083 ErrorAbort(state, "%s() loading \"%s\" failed: %s",
1084 name, filename, strerror(errno));
1085 free(filename);
1086 free(v);
1087 free(fc.data);
1088 return NULL;
1089 }
1090
1091 v->size = fc.size;
1092 v->data = (char*)fc.data;
1093
1094 free(filename);
1095 return v;
1096}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001097
Doug Zongker9931f7f2009-06-10 14:11:53 -07001098void RegisterInstallFunctions() {
1099 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001100 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001101 RegisterFunction("unmount", UnmountFn);
1102 RegisterFunction("format", FormatFn);
1103 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001104 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001105 RegisterFunction("delete", DeleteFn);
1106 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001107 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1108 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001109 RegisterFunction("symlink", SymlinkFn);
1110 RegisterFunction("set_perm", SetPermFn);
1111 RegisterFunction("set_perm_recursive", SetPermFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001112
1113 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001114 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001115 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001116
1117 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001118 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1119 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001120
Doug Zongker512536a2010-02-17 16:11:44 -08001121 RegisterFunction("read_file", ReadFileFn);
1122 RegisterFunction("sha1_check", Sha1CheckFn);
1123
Doug Zongkerd0181b82011-10-19 10:51:12 -07001124 RegisterFunction("wipe_cache", WipeCacheFn);
1125
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001126 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001127
1128 RegisterFunction("run_program", RunProgramFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001129}