blob: d8c6dc249fbab91befdb605b20d918d1865adb57 [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"
Dees_Troy512376c2013-09-03 19:39:41 +000040#include "flashutils/flashutils.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070041
Doug Zongker3d177d02010-07-01 09:18:44 -070042#ifdef USE_EXT4
43#include "make_ext4fs.h"
44#endif
45
46// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -070047//
Doug Zongker3d177d02010-07-01 09:18:44 -070048// fs_type="yaffs2" partition_type="MTD" location=partition
49// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -080050Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070051 char* result = NULL;
Doug Zongker3d177d02010-07-01 09:18:44 -070052 if (argc != 4) {
53 return ErrorAbort(state, "%s() expects 4 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -070054 }
Doug Zongker3d177d02010-07-01 09:18:44 -070055 char* fs_type;
56 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -070057 char* location;
58 char* mount_point;
Doug Zongker3d177d02010-07-01 09:18:44 -070059 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
60 &location, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070061 return NULL;
62 }
63
Doug Zongker3d177d02010-07-01 09:18:44 -070064 if (strlen(fs_type) == 0) {
65 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
66 goto done;
67 }
68 if (strlen(partition_type) == 0) {
69 ErrorAbort(state, "partition_type argument to %s() can't be empty",
70 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070071 goto done;
72 }
73 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070074 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070075 goto done;
76 }
77 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070078 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070079 goto done;
80 }
81
Stephen Smalley779701d2012-02-09 14:13:23 -050082 char *secontext = NULL;
83
84 if (sehandle) {
85 selabel_lookup(sehandle, &secontext, mount_point, 0755);
86 setfscreatecon(secontext);
87 }
Stephen Smalley779701d2012-02-09 14:13:23 -050088
Doug Zongker9931f7f2009-06-10 14:11:53 -070089 mkdir(mount_point, 0755);
90
Stephen Smalley779701d2012-02-09 14:13:23 -050091 if (secontext) {
92 freecon(secontext);
93 setfscreatecon(NULL);
94 }
Stephen Smalley779701d2012-02-09 14:13:23 -050095
Doug Zongker3d177d02010-07-01 09:18:44 -070096 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070097 mtd_scan_partitions();
98 const MtdPartition* mtd;
99 mtd = mtd_find_partition_by_name(location);
100 if (mtd == NULL) {
101 fprintf(stderr, "%s: no mtd partition named \"%s\"",
102 name, location);
103 result = strdup("");
104 goto done;
105 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700106 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700107 fprintf(stderr, "mtd mount of %s failed: %s\n",
108 location, strerror(errno));
109 result = strdup("");
110 goto done;
111 }
112 result = mount_point;
113 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700114 if (mount(location, mount_point, fs_type,
Doug Zongker9931f7f2009-06-10 14:11:53 -0700115 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
Doug Zongker60babf82009-09-18 15:11:24 -0700116 fprintf(stderr, "%s: failed to mount %s at %s: %s\n",
117 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700118 result = strdup("");
119 } else {
120 result = mount_point;
121 }
122 }
123
124done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700125 free(fs_type);
126 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700127 free(location);
128 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800129 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700130}
131
Doug Zongker8edb00c2009-06-11 17:21:44 -0700132
133// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800134Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700135 char* result = NULL;
136 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700137 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700138 }
139 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700140 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700141 return NULL;
142 }
143 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700144 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700145 goto done;
146 }
147
148 scan_mounted_volumes();
149 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
150 if (vol == NULL) {
151 result = strdup("");
152 } else {
153 result = mount_point;
154 }
155
156done:
157 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800158 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700159}
160
161
Doug Zongker512536a2010-02-17 16:11:44 -0800162Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700163 char* result = NULL;
164 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700165 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700166 }
167 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700168 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700169 return NULL;
170 }
171 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700172 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700173 goto done;
174 }
175
176 scan_mounted_volumes();
177 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
178 if (vol == NULL) {
179 fprintf(stderr, "unmount of %s failed; no such volume\n", mount_point);
180 result = strdup("");
181 } else {
182 unmount_mounted_volume(vol);
183 result = mount_point;
184 }
185
186done:
187 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800188 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700189}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700190
191
Stephen Smalley779701d2012-02-09 14:13:23 -0500192// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700193//
Stephen Smalley779701d2012-02-09 14:13:23 -0500194// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
195// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800196// if fs_size == 0, then make_ext4fs uses the entire partition.
197// if fs_size > 0, that is the size to use
198// if fs_size < 0, then reserve that many bytes at the end of the partition
Doug Zongker512536a2010-02-17 16:11:44 -0800199Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700200 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400201 if (argc != 5) {
202 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700203 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700204 char* fs_type;
205 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700206 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800207 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400208 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500209
Stephen Smalley779701d2012-02-09 14:13:23 -0500210 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
211 return NULL;
212 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700213
Doug Zongker3d177d02010-07-01 09:18:44 -0700214 if (strlen(fs_type) == 0) {
215 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
216 goto done;
217 }
218 if (strlen(partition_type) == 0) {
219 ErrorAbort(state, "partition_type argument to %s() can't be empty",
220 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700221 goto done;
222 }
223 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700224 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700225 goto done;
226 }
227
Stephen Smalley516e4e22012-04-03 13:35:11 -0400228 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500229 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
230 goto done;
231 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500232
Doug Zongker3d177d02010-07-01 09:18:44 -0700233 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700234 mtd_scan_partitions();
235 const MtdPartition* mtd = mtd_find_partition_by_name(location);
236 if (mtd == NULL) {
237 fprintf(stderr, "%s: no mtd partition named \"%s\"",
238 name, location);
239 result = strdup("");
240 goto done;
241 }
242 MtdWriteContext* ctx = mtd_write_partition(mtd);
243 if (ctx == NULL) {
244 fprintf(stderr, "%s: can't write \"%s\"", name, location);
245 result = strdup("");
246 goto done;
247 }
248 if (mtd_erase_blocks(ctx, -1) == -1) {
249 mtd_write_close(ctx);
250 fprintf(stderr, "%s: failed to erase \"%s\"", name, location);
251 result = strdup("");
252 goto done;
253 }
254 if (mtd_write_close(ctx) != 0) {
255 fprintf(stderr, "%s: failed to close \"%s\"", name, location);
256 result = strdup("");
257 goto done;
258 }
259 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700260#ifdef USE_EXT4
261 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500262 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700263 if (status != 0) {
264 fprintf(stderr, "%s: make_ext4fs failed (%d) on %s",
265 name, status, location);
266 result = strdup("");
267 goto done;
268 }
269 result = location;
270#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700271 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700272 fprintf(stderr, "%s: unsupported fs_type \"%s\" partition_type \"%s\"",
273 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700274 }
275
276done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700277 free(fs_type);
278 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700279 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800280 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700281}
282
Doug Zongker8edb00c2009-06-11 17:21:44 -0700283
Doug Zongker512536a2010-02-17 16:11:44 -0800284Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700285 char** paths = malloc(argc * sizeof(char*));
286 int i;
287 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700288 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700289 if (paths[i] == NULL) {
290 int j;
291 for (j = 0; j < i; ++i) {
292 free(paths[j]);
293 }
294 free(paths);
295 return NULL;
296 }
297 }
298
299 bool recursive = (strcmp(name, "delete_recursive") == 0);
300
301 int success = 0;
302 for (i = 0; i < argc; ++i) {
303 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
304 ++success;
305 free(paths[i]);
306 }
307 free(paths);
308
309 char buffer[10];
310 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800311 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700312}
313
Doug Zongker8edb00c2009-06-11 17:21:44 -0700314
Doug Zongker512536a2010-02-17 16:11:44 -0800315Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700316 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700317 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700318 }
319 char* frac_str;
320 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700321 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700322 return NULL;
323 }
324
325 double frac = strtod(frac_str, NULL);
326 int sec = strtol(sec_str, NULL, 10);
327
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700328 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700329 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
330
Doug Zongker9931f7f2009-06-10 14:11:53 -0700331 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800332 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700333}
334
Doug Zongker512536a2010-02-17 16:11:44 -0800335Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700336 if (argc != 1) {
337 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
338 }
339 char* frac_str;
340 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
341 return NULL;
342 }
343
344 double frac = strtod(frac_str, NULL);
345
346 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
347 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
348
Doug Zongker512536a2010-02-17 16:11:44 -0800349 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700350}
351
Doug Zongker8edb00c2009-06-11 17:21:44 -0700352// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800353Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700354 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700355 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700356 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700357 }
358 char* zip_path;
359 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700360 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700361
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700362 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700363
364 // To create a consistent system image, never use the clock for timestamps.
365 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
366
367 bool success = mzExtractRecursive(za, zip_path, dest_path,
368 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500369 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700370 free(zip_path);
371 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800372 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700373}
374
Doug Zongker8edb00c2009-06-11 17:21:44 -0700375
376// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800377// or
378// package_extract_file(package_path)
379// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800380// function (the char* returned is actually a FileContents*).
381Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700382 int argc, Expr* argv[]) {
Doug Zongker6aece332010-02-01 14:40:12 -0800383 if (argc != 1 && argc != 2) {
384 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
385 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700386 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700387 bool success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800388 if (argc == 2) {
389 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700390
Doug Zongker6aece332010-02-01 14:40:12 -0800391 char* zip_path;
392 char* dest_path;
393 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
394
395 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
396 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
397 if (entry == NULL) {
398 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
399 goto done2;
400 }
401
402 FILE* f = fopen(dest_path, "wb");
403 if (f == NULL) {
404 fprintf(stderr, "%s: can't open %s for write: %s\n",
405 name, dest_path, strerror(errno));
406 goto done2;
407 }
408 success = mzExtractZipEntryToFile(za, entry, fileno(f));
409 fclose(f);
410
411 done2:
412 free(zip_path);
413 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800414 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800415 } else {
416 // The one-argument version returns the contents of the file
417 // as the result.
418
419 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800420 Value* v = malloc(sizeof(Value));
421 v->type = VAL_BLOB;
422 v->size = -1;
423 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800424
425 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
426
427 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
428 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
429 if (entry == NULL) {
430 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
431 goto done1;
432 }
433
Doug Zongker512536a2010-02-17 16:11:44 -0800434 v->size = mzGetZipEntryUncompLen(entry);
435 v->data = malloc(v->size);
436 if (v->data == NULL) {
Doug Zongker6aece332010-02-01 14:40:12 -0800437 fprintf(stderr, "%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800438 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800439 goto done1;
440 }
441
Doug Zongker512536a2010-02-17 16:11:44 -0800442 success = mzExtractZipEntryToBuffer(za, entry,
443 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800444
445 done1:
446 free(zip_path);
447 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800448 free(v->data);
449 v->data = NULL;
450 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800451 }
Doug Zongker512536a2010-02-17 16:11:44 -0800452 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700453 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700454}
455
Doug Zongkera23075f2012-08-06 16:19:09 -0700456// Create all parent directories of name, if necessary.
457static int make_parents(char* name) {
458 char* p;
459 for (p = name + (strlen(name)-1); p > name; --p) {
460 if (*p != '/') continue;
461 *p = '\0';
462 if (make_parents(name) < 0) return -1;
463 int result = mkdir(name, 0700);
464 if (result == 0) fprintf(stderr, "symlink(): created [%s]\n", name);
465 *p = '/';
466 if (result == 0 || errno == EEXIST) {
467 // successfully created or already existed; we're done
468 return 0;
469 } else {
470 fprintf(stderr, "failed to mkdir %s: %s\n", name, strerror(errno));
471 return -1;
472 }
473 }
474 return 0;
475}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700476
Doug Zongker9931f7f2009-06-10 14:11:53 -0700477// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700478// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800479Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700480 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700481 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700482 }
483 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700484 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700485 if (target == NULL) return NULL;
486
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700487 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700488 if (srcs == NULL) {
489 free(target);
490 return NULL;
491 }
492
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700493 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700494 int i;
495 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700496 if (unlink(srcs[i]) < 0) {
497 if (errno != ENOENT) {
498 fprintf(stderr, "%s: failed to remove %s: %s\n",
499 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700500 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700501 }
502 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700503 if (make_parents(srcs[i])) {
504 fprintf(stderr, "%s: failed to symlink %s to %s: making parents failed\n",
505 name, srcs[i], target);
506 ++bad;
507 }
Doug Zongker60babf82009-09-18 15:11:24 -0700508 if (symlink(target, srcs[i]) < 0) {
509 fprintf(stderr, "%s: failed to symlink %s to %s: %s\n",
510 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700511 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700512 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700513 free(srcs[i]);
514 }
515 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700516 if (bad) {
517 return ErrorAbort(state, "%s: some symlinks failed", name);
518 }
Doug Zongker512536a2010-02-17 16:11:44 -0800519 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700520}
521
Doug Zongker8edb00c2009-06-11 17:21:44 -0700522
Doug Zongker512536a2010-02-17 16:11:44 -0800523Value* SetPermFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700524 char* result = NULL;
525 bool recursive = (strcmp(name, "set_perm_recursive") == 0);
526
527 int min_args = 4 + (recursive ? 1 : 0);
528 if (argc < min_args) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700529 return ErrorAbort(state, "%s() expects %d+ args, got %d",
530 name, min_args, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700531 }
532
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700533 char** args = ReadVarArgs(state, argc, argv);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700534 if (args == NULL) return NULL;
535
536 char* end;
537 int i;
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700538 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700539
540 int uid = strtoul(args[0], &end, 0);
541 if (*end != '\0' || args[0][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700542 ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700543 goto done;
544 }
545
546 int gid = strtoul(args[1], &end, 0);
547 if (*end != '\0' || args[1][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700548 ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700549 goto done;
550 }
551
552 if (recursive) {
553 int dir_mode = strtoul(args[2], &end, 0);
554 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700555 ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700556 goto done;
557 }
558
559 int file_mode = strtoul(args[3], &end, 0);
560 if (*end != '\0' || args[3][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700561 ErrorAbort(state, "%s: \"%s\" not a valid filemode",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700562 name, args[3]);
563 goto done;
564 }
565
566 for (i = 4; i < argc; ++i) {
567 dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode);
568 }
569 } else {
570 int mode = strtoul(args[2], &end, 0);
571 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700572 ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700573 goto done;
574 }
575
Doug Zongker0bbfe3d2009-06-25 13:37:31 -0700576 for (i = 3; i < argc; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700577 if (chown(args[i], uid, gid) < 0) {
578 fprintf(stderr, "%s: chown of %s to %d %d failed: %s\n",
579 name, args[i], uid, gid, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700580 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700581 }
582 if (chmod(args[i], mode) < 0) {
583 fprintf(stderr, "%s: chmod of %s to %o failed: %s\n",
584 name, args[i], mode, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700585 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700586 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700587 }
588 }
589 result = strdup("");
590
591done:
592 for (i = 0; i < argc; ++i) {
593 free(args[i]);
594 }
595 free(args);
596
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700597 if (bad) {
598 free(result);
599 return ErrorAbort(state, "%s: some changes failed", name);
600 }
Doug Zongker512536a2010-02-17 16:11:44 -0800601 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700602}
603
Doug Zongker8edb00c2009-06-11 17:21:44 -0700604
Doug Zongker512536a2010-02-17 16:11:44 -0800605Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700606 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700607 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700608 }
609 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700610 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700611 if (key == NULL) return NULL;
612
613 char value[PROPERTY_VALUE_MAX];
614 property_get(key, value, "");
615 free(key);
616
Doug Zongker512536a2010-02-17 16:11:44 -0800617 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700618}
619
620
Doug Zongker47cace92009-06-18 10:11:50 -0700621// file_getprop(file, key)
622//
623// interprets 'file' as a getprop-style file (key=value pairs, one
624// per line, # comment lines and blank lines okay), and returns the value
625// for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800626Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700627 char* result = NULL;
628 char* buffer = NULL;
629 char* filename;
630 char* key;
631 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
632 return NULL;
633 }
634
635 struct stat st;
636 if (stat(filename, &st) < 0) {
637 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
638 name, filename, strerror(errno));
639 goto done;
640 }
641
642#define MAX_FILE_GETPROP_SIZE 65536
643
644 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
645 ErrorAbort(state, "%s too large for %s (max %d)",
646 filename, name, MAX_FILE_GETPROP_SIZE);
647 goto done;
648 }
649
650 buffer = malloc(st.st_size+1);
651 if (buffer == NULL) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700652 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700653 goto done;
654 }
655
656 FILE* f = fopen(filename, "rb");
657 if (f == NULL) {
658 ErrorAbort(state, "%s: failed to open %s: %s",
659 name, filename, strerror(errno));
660 goto done;
661 }
662
663 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700664 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Doug Zongker47cace92009-06-18 10:11:50 -0700665 name, st.st_size+1, filename);
666 fclose(f);
667 goto done;
668 }
669 buffer[st.st_size] = '\0';
670
671 fclose(f);
672
673 char* line = strtok(buffer, "\n");
674 do {
675 // skip whitespace at start of line
676 while (*line && isspace(*line)) ++line;
677
678 // comment or blank line: skip to next line
679 if (*line == '\0' || *line == '#') continue;
680
681 char* equal = strchr(line, '=');
682 if (equal == NULL) {
683 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
684 name, line, filename);
685 goto done;
686 }
687
688 // trim whitespace between key and '='
689 char* key_end = equal-1;
690 while (key_end > line && isspace(*key_end)) --key_end;
691 key_end[1] = '\0';
692
693 // not the key we're looking for
694 if (strcmp(key, line) != 0) continue;
695
696 // skip whitespace after the '=' to the start of the value
697 char* val_start = equal+1;
698 while(*val_start && isspace(*val_start)) ++val_start;
699
700 // trim trailing whitespace
701 char* val_end = val_start + strlen(val_start)-1;
702 while (val_end > val_start && isspace(*val_end)) --val_end;
703 val_end[1] = '\0';
704
705 result = strdup(val_start);
706 break;
707
708 } while ((line = strtok(NULL, "\n")));
709
710 if (result == NULL) result = strdup("");
711
712 done:
713 free(filename);
714 free(key);
715 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800716 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -0700717}
718
719
Doug Zongker8edb00c2009-06-11 17:21:44 -0700720static bool write_raw_image_cb(const unsigned char* data,
721 int data_len, void* ctx) {
722 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
723 if (r == data_len) return true;
724 fprintf(stderr, "%s\n", strerror(errno));
725 return false;
726}
727
Doug Zongker179b2d92011-04-12 15:49:04 -0700728// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -0800729Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700730 char* result = NULL;
731
Doug Zongker179b2d92011-04-12 15:49:04 -0700732 Value* partition_value;
733 Value* contents;
734 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700735 return NULL;
736 }
737
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700738 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -0700739 if (partition_value->type != VAL_STRING) {
740 ErrorAbort(state, "partition argument to %s must be string", name);
741 goto done;
742 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700743 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700744 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700745 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700746 goto done;
747 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700748 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700749 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700750 goto done;
751 }
752
Dees_Troy76a1d412013-04-20 13:54:35 +0000753 char* filename = contents->data;
754 if (0 == restore_raw_partition(NULL, partition, filename))
755 result = strdup(partition);
756 else {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700757 result = strdup("");
758 goto done;
759 }
760
Doug Zongker8edb00c2009-06-11 17:21:44 -0700761done:
Doug Zongker179b2d92011-04-12 15:49:04 -0700762 if (result != partition) FreeValue(partition_value);
763 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -0800764 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700765}
766
Doug Zongker8edb00c2009-06-11 17:21:44 -0700767// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -0800768Value* ApplyPatchSpaceFn(const char* name, State* state,
769 int argc, Expr* argv[]) {
770 char* bytes_str;
771 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
772 return NULL;
773 }
774
775 char* endptr;
776 size_t bytes = strtol(bytes_str, &endptr, 10);
777 if (bytes == 0 && endptr == bytes_str) {
778 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
779 name, bytes_str);
780 free(bytes_str);
781 return NULL;
782 }
783
784 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
785}
786
787
788// apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1_1, patch_1, ...)
Doug Zongker512536a2010-02-17 16:11:44 -0800789Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800790 if (argc < 6 || (argc % 2) == 1) {
791 return ErrorAbort(state, "%s(): expected at least 6 args and an "
792 "even number, got %d",
793 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700794 }
795
Doug Zongkerc4351c72010-02-22 14:46:32 -0800796 char* source_filename;
797 char* target_filename;
798 char* target_sha1;
799 char* target_size_str;
800 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
801 &target_sha1, &target_size_str) < 0) {
802 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700803 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700804
Doug Zongkerc4351c72010-02-22 14:46:32 -0800805 char* endptr;
806 size_t target_size = strtol(target_size_str, &endptr, 10);
807 if (target_size == 0 && endptr == target_size_str) {
808 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
809 name, target_size_str);
810 free(source_filename);
811 free(target_filename);
812 free(target_sha1);
813 free(target_size_str);
814 return NULL;
815 }
816
817 int patchcount = (argc-4) / 2;
818 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700819
820 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800821 for (i = 0; i < patchcount; ++i) {
822 if (patches[i*2]->type != VAL_STRING) {
823 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
824 break;
825 }
826 if (patches[i*2+1]->type != VAL_BLOB) {
827 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
828 break;
829 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700830 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800831 if (i != patchcount) {
832 for (i = 0; i < patchcount*2; ++i) {
833 FreeValue(patches[i]);
834 }
835 free(patches);
836 return NULL;
837 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700838
Doug Zongkerc4351c72010-02-22 14:46:32 -0800839 char** patch_sha_str = malloc(patchcount * sizeof(char*));
840 for (i = 0; i < patchcount; ++i) {
841 patch_sha_str[i] = patches[i*2]->data;
842 patches[i*2]->data = NULL;
843 FreeValue(patches[i*2]);
844 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700845 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800846
847 int result = applypatch(source_filename, target_filename,
848 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -0700849 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800850
851 for (i = 0; i < patchcount; ++i) {
852 FreeValue(patches[i]);
853 }
854 free(patch_sha_str);
855 free(patches);
856
857 return StringValue(strdup(result == 0 ? "t" : ""));
858}
859
860// apply_patch_check(file, [sha1_1, ...])
861Value* ApplyPatchCheckFn(const char* name, State* state,
862 int argc, Expr* argv[]) {
863 if (argc < 1) {
864 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
865 name, argc);
866 }
867
868 char* filename;
869 if (ReadArgs(state, argv, 1, &filename) < 0) {
870 return NULL;
871 }
872
873 int patchcount = argc-1;
874 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
875
876 int result = applypatch_check(filename, patchcount, sha1s);
877
878 int i;
879 for (i = 0; i < patchcount; ++i) {
880 free(sha1s[i]);
881 }
882 free(sha1s);
883
884 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700885}
886
Doug Zongker512536a2010-02-17 16:11:44 -0800887Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700888 char** args = ReadVarArgs(state, argc, argv);
889 if (args == NULL) {
890 return NULL;
891 }
892
893 int size = 0;
894 int i;
895 for (i = 0; i < argc; ++i) {
896 size += strlen(args[i]);
897 }
898 char* buffer = malloc(size+1);
899 size = 0;
900 for (i = 0; i < argc; ++i) {
901 strcpy(buffer+size, args[i]);
902 size += strlen(args[i]);
903 free(args[i]);
904 }
905 free(args);
906 buffer[size] = '\0';
907
908 char* line = strtok(buffer, "\n");
909 while (line) {
910 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
911 "ui_print %s\n", line);
912 line = strtok(NULL, "\n");
913 }
914 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
915
Doug Zongker512536a2010-02-17 16:11:44 -0800916 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700917}
918
Doug Zongkerd0181b82011-10-19 10:51:12 -0700919Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
920 if (argc != 0) {
921 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
922 }
923 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
924 return StringValue(strdup("t"));
925}
926
Doug Zongker512536a2010-02-17 16:11:44 -0800927Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700928 if (argc < 1) {
929 return ErrorAbort(state, "%s() expects at least 1 arg", name);
930 }
931 char** args = ReadVarArgs(state, argc, argv);
932 if (args == NULL) {
933 return NULL;
934 }
935
936 char** args2 = malloc(sizeof(char*) * (argc+1));
937 memcpy(args2, args, sizeof(char*) * argc);
938 args2[argc] = NULL;
939
940 fprintf(stderr, "about to run program [%s] with %d args\n", args2[0], argc);
941
942 pid_t child = fork();
943 if (child == 0) {
944 execv(args2[0], args2);
945 fprintf(stderr, "run_program: execv failed: %s\n", strerror(errno));
946 _exit(1);
947 }
948 int status;
949 waitpid(child, &status, 0);
950 if (WIFEXITED(status)) {
951 if (WEXITSTATUS(status) != 0) {
952 fprintf(stderr, "run_program: child exited with status %d\n",
953 WEXITSTATUS(status));
954 }
955 } else if (WIFSIGNALED(status)) {
956 fprintf(stderr, "run_program: child terminated by signal %d\n",
957 WTERMSIG(status));
958 }
959
960 int i;
961 for (i = 0; i < argc; ++i) {
962 free(args[i]);
963 }
964 free(args);
965 free(args2);
966
967 char buffer[20];
968 sprintf(buffer, "%d", status);
969
Doug Zongker512536a2010-02-17 16:11:44 -0800970 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700971}
972
Doug Zongker512536a2010-02-17 16:11:44 -0800973// Take a sha-1 digest and return it as a newly-allocated hex string.
974static char* PrintSha1(uint8_t* digest) {
975 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
976 int i;
977 const char* alphabet = "0123456789abcdef";
978 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
979 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
980 buffer[i*2+1] = alphabet[digest[i] & 0xf];
981 }
982 buffer[i*2] = '\0';
983 return buffer;
984}
985
986// sha1_check(data)
987// to return the sha1 of the data (given in the format returned by
988// read_file).
989//
990// sha1_check(data, sha1_hex, [sha1_hex, ...])
991// returns the sha1 of the file if it matches any of the hex
992// strings passed, or "" if it does not equal any of them.
993//
994Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
995 if (argc < 1) {
996 return ErrorAbort(state, "%s() expects at least 1 arg", name);
997 }
998
999 Value** args = ReadValueVarArgs(state, argc, argv);
1000 if (args == NULL) {
1001 return NULL;
1002 }
1003
1004 if (args[0]->size < 0) {
1005 fprintf(stderr, "%s(): no file contents received", name);
1006 return StringValue(strdup(""));
1007 }
1008 uint8_t digest[SHA_DIGEST_SIZE];
1009 SHA(args[0]->data, args[0]->size, digest);
1010 FreeValue(args[0]);
1011
1012 if (argc == 1) {
1013 return StringValue(PrintSha1(digest));
1014 }
1015
1016 int i;
1017 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1018 for (i = 1; i < argc; ++i) {
1019 if (args[i]->type != VAL_STRING) {
1020 fprintf(stderr, "%s(): arg %d is not a string; skipping",
1021 name, i);
1022 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1023 // Warn about bad args and skip them.
1024 fprintf(stderr, "%s(): error parsing \"%s\" as sha-1; skipping",
1025 name, args[i]->data);
1026 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1027 break;
1028 }
1029 FreeValue(args[i]);
1030 }
1031 if (i >= argc) {
1032 // Didn't match any of the hex strings; return false.
1033 return StringValue(strdup(""));
1034 }
1035 // Found a match; free all the remaining arguments and return the
1036 // matched one.
1037 int j;
1038 for (j = i+1; j < argc; ++j) {
1039 FreeValue(args[j]);
1040 }
1041 return args[i];
1042}
1043
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001044// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001045// is actually a FileContents*).
1046Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1047 if (argc != 1) {
1048 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1049 }
1050 char* filename;
1051 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1052
1053 Value* v = malloc(sizeof(Value));
1054 v->type = VAL_BLOB;
1055
1056 FileContents fc;
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001057 if (LoadFileContents(filename, &fc, RETOUCH_DONT_MASK) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001058 ErrorAbort(state, "%s() loading \"%s\" failed: %s",
1059 name, filename, strerror(errno));
1060 free(filename);
1061 free(v);
1062 free(fc.data);
1063 return NULL;
1064 }
1065
1066 v->size = fc.size;
1067 v->data = (char*)fc.data;
1068
1069 free(filename);
1070 return v;
1071}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001072
Doug Zongker9931f7f2009-06-10 14:11:53 -07001073void RegisterInstallFunctions() {
1074 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001075 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001076 RegisterFunction("unmount", UnmountFn);
1077 RegisterFunction("format", FormatFn);
1078 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001079 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001080 RegisterFunction("delete", DeleteFn);
1081 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001082 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1083 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001084 RegisterFunction("symlink", SymlinkFn);
1085 RegisterFunction("set_perm", SetPermFn);
1086 RegisterFunction("set_perm_recursive", SetPermFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001087
1088 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001089 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001090 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001091
1092 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001093 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1094 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001095
Doug Zongker512536a2010-02-17 16:11:44 -08001096 RegisterFunction("read_file", ReadFileFn);
1097 RegisterFunction("sha1_check", Sha1CheckFn);
1098
Doug Zongkerd0181b82011-10-19 10:51:12 -07001099 RegisterFunction("wipe_cache", WipeCacheFn);
1100
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001101 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001102
1103 RegisterFunction("run_program", RunProgramFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001104}