blob: 6a79964673ae7de450d6719dff449c6efa4d23c6 [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"
Hristo Bojinovdb314d62010-08-02 10:29:49 -070036#include "minelf/Retouch.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070037#include "mtdutils/mounts.h"
38#include "mtdutils/mtdutils.h"
39#include "updater.h"
Doug Zongker512536a2010-02-17 16:11:44 -080040#include "applypatch/applypatch.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
82 mkdir(mount_point, 0755);
83
Doug Zongker3d177d02010-07-01 09:18:44 -070084 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070085 mtd_scan_partitions();
86 const MtdPartition* mtd;
87 mtd = mtd_find_partition_by_name(location);
88 if (mtd == NULL) {
89 fprintf(stderr, "%s: no mtd partition named \"%s\"",
90 name, location);
91 result = strdup("");
92 goto done;
93 }
Doug Zongker3d177d02010-07-01 09:18:44 -070094 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070095 fprintf(stderr, "mtd mount of %s failed: %s\n",
96 location, strerror(errno));
97 result = strdup("");
98 goto done;
99 }
100 result = mount_point;
101 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700102 if (mount(location, mount_point, fs_type,
Doug Zongker9931f7f2009-06-10 14:11:53 -0700103 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
Doug Zongker60babf82009-09-18 15:11:24 -0700104 fprintf(stderr, "%s: failed to mount %s at %s: %s\n",
105 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700106 result = strdup("");
107 } else {
108 result = mount_point;
109 }
110 }
111
112done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700113 free(fs_type);
114 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700115 free(location);
116 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800117 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700118}
119
Doug Zongker8edb00c2009-06-11 17:21:44 -0700120
121// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800122Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700123 char* result = NULL;
124 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700125 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700126 }
127 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700128 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700129 return NULL;
130 }
131 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700132 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700133 goto done;
134 }
135
136 scan_mounted_volumes();
137 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
138 if (vol == NULL) {
139 result = strdup("");
140 } else {
141 result = mount_point;
142 }
143
144done:
145 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800146 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700147}
148
149
Doug Zongker512536a2010-02-17 16:11:44 -0800150Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700151 char* result = NULL;
152 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700153 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700154 }
155 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700156 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700157 return NULL;
158 }
159 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700160 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700161 goto done;
162 }
163
164 scan_mounted_volumes();
165 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
166 if (vol == NULL) {
167 fprintf(stderr, "unmount of %s failed; no such volume\n", mount_point);
168 result = strdup("");
169 } else {
170 unmount_mounted_volume(vol);
171 result = mount_point;
172 }
173
174done:
175 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800176 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700177}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700178
179
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800180// format(fs_type, partition_type, location, fs_size)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700181//
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800182// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes>
183// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes>
184// if fs_size == 0, then make_ext4fs uses the entire partition.
185// if fs_size > 0, that is the size to use
186// if fs_size < 0, then reserve that many bytes at the end of the partition
Doug Zongker512536a2010-02-17 16:11:44 -0800187Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700188 char* result = NULL;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800189 if (argc != 4) {
190 return ErrorAbort(state, "%s() expects 4 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700191 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700192 char* fs_type;
193 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700194 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800195 char* fs_size;
196 if (ReadArgs(state, argv, 4, &fs_type, &partition_type, &location, &fs_size) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700197 return NULL;
198 }
199
Doug Zongker3d177d02010-07-01 09:18:44 -0700200 if (strlen(fs_type) == 0) {
201 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
202 goto done;
203 }
204 if (strlen(partition_type) == 0) {
205 ErrorAbort(state, "partition_type argument to %s() can't be empty",
206 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700207 goto done;
208 }
209 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700210 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700211 goto done;
212 }
213
Doug Zongker3d177d02010-07-01 09:18:44 -0700214 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700215 mtd_scan_partitions();
216 const MtdPartition* mtd = mtd_find_partition_by_name(location);
217 if (mtd == NULL) {
218 fprintf(stderr, "%s: no mtd partition named \"%s\"",
219 name, location);
220 result = strdup("");
221 goto done;
222 }
223 MtdWriteContext* ctx = mtd_write_partition(mtd);
224 if (ctx == NULL) {
225 fprintf(stderr, "%s: can't write \"%s\"", name, location);
226 result = strdup("");
227 goto done;
228 }
229 if (mtd_erase_blocks(ctx, -1) == -1) {
230 mtd_write_close(ctx);
231 fprintf(stderr, "%s: failed to erase \"%s\"", name, location);
232 result = strdup("");
233 goto done;
234 }
235 if (mtd_write_close(ctx) != 0) {
236 fprintf(stderr, "%s: failed to close \"%s\"", name, location);
237 result = strdup("");
238 goto done;
239 }
240 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700241#ifdef USE_EXT4
242 } else if (strcmp(fs_type, "ext4") == 0) {
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800243 int status = make_ext4fs(location, atoll(fs_size));
Doug Zongker3d177d02010-07-01 09:18:44 -0700244 if (status != 0) {
245 fprintf(stderr, "%s: make_ext4fs failed (%d) on %s",
246 name, status, location);
247 result = strdup("");
248 goto done;
249 }
250 result = location;
251#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700252 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700253 fprintf(stderr, "%s: unsupported fs_type \"%s\" partition_type \"%s\"",
254 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700255 }
256
257done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700258 free(fs_type);
259 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700260 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800261 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700262}
263
Doug Zongker8edb00c2009-06-11 17:21:44 -0700264
Doug Zongker512536a2010-02-17 16:11:44 -0800265Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700266 char** paths = malloc(argc * sizeof(char*));
267 int i;
268 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700269 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700270 if (paths[i] == NULL) {
271 int j;
272 for (j = 0; j < i; ++i) {
273 free(paths[j]);
274 }
275 free(paths);
276 return NULL;
277 }
278 }
279
280 bool recursive = (strcmp(name, "delete_recursive") == 0);
281
282 int success = 0;
283 for (i = 0; i < argc; ++i) {
284 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
285 ++success;
286 free(paths[i]);
287 }
288 free(paths);
289
290 char buffer[10];
291 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800292 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700293}
294
Doug Zongker8edb00c2009-06-11 17:21:44 -0700295
Doug Zongker512536a2010-02-17 16:11:44 -0800296Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700297 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700298 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700299 }
300 char* frac_str;
301 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700302 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700303 return NULL;
304 }
305
306 double frac = strtod(frac_str, NULL);
307 int sec = strtol(sec_str, NULL, 10);
308
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700309 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700310 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
311
Doug Zongker9931f7f2009-06-10 14:11:53 -0700312 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800313 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700314}
315
Doug Zongker512536a2010-02-17 16:11:44 -0800316Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700317 if (argc != 1) {
318 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
319 }
320 char* frac_str;
321 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
322 return NULL;
323 }
324
325 double frac = strtod(frac_str, NULL);
326
327 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
328 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
329
Doug Zongker512536a2010-02-17 16:11:44 -0800330 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700331}
332
Doug Zongker8edb00c2009-06-11 17:21:44 -0700333// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800334Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700335 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700336 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700337 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700338 }
339 char* zip_path;
340 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700341 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700342
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700343 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700344
345 // To create a consistent system image, never use the clock for timestamps.
346 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
347
348 bool success = mzExtractRecursive(za, zip_path, dest_path,
349 MZ_EXTRACT_FILES_ONLY, &timestamp,
350 NULL, NULL);
351 free(zip_path);
352 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800353 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700354}
355
Doug Zongker8edb00c2009-06-11 17:21:44 -0700356
357// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800358// or
359// package_extract_file(package_path)
360// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800361// function (the char* returned is actually a FileContents*).
362Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700363 int argc, Expr* argv[]) {
Doug Zongker6aece332010-02-01 14:40:12 -0800364 if (argc != 1 && argc != 2) {
365 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
366 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700367 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700368 bool success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800369 if (argc == 2) {
370 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700371
Doug Zongker6aece332010-02-01 14:40:12 -0800372 char* zip_path;
373 char* dest_path;
374 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
375
376 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
377 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
378 if (entry == NULL) {
379 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
380 goto done2;
381 }
382
383 FILE* f = fopen(dest_path, "wb");
384 if (f == NULL) {
385 fprintf(stderr, "%s: can't open %s for write: %s\n",
386 name, dest_path, strerror(errno));
387 goto done2;
388 }
389 success = mzExtractZipEntryToFile(za, entry, fileno(f));
390 fclose(f);
391
392 done2:
393 free(zip_path);
394 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800395 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800396 } else {
397 // The one-argument version returns the contents of the file
398 // as the result.
399
400 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800401 Value* v = malloc(sizeof(Value));
402 v->type = VAL_BLOB;
403 v->size = -1;
404 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800405
406 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
407
408 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
409 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
410 if (entry == NULL) {
411 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
412 goto done1;
413 }
414
Doug Zongker512536a2010-02-17 16:11:44 -0800415 v->size = mzGetZipEntryUncompLen(entry);
416 v->data = malloc(v->size);
417 if (v->data == NULL) {
Doug Zongker6aece332010-02-01 14:40:12 -0800418 fprintf(stderr, "%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800419 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800420 goto done1;
421 }
422
Doug Zongker512536a2010-02-17 16:11:44 -0800423 success = mzExtractZipEntryToBuffer(za, entry,
424 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800425
426 done1:
427 free(zip_path);
428 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800429 free(v->data);
430 v->data = NULL;
431 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800432 }
Doug Zongker512536a2010-02-17 16:11:44 -0800433 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700434 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700435}
436
437
Hristo Bojinovdb314d62010-08-02 10:29:49 -0700438// retouch_binaries(lib1, lib2, ...)
439Value* RetouchBinariesFn(const char* name, State* state,
440 int argc, Expr* argv[]) {
441 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
442
443 char **retouch_entries = ReadVarArgs(state, argc, argv);
444 if (retouch_entries == NULL) {
445 return StringValue(strdup("t"));
446 }
447
448 // some randomness from the clock
449 int32_t override_base;
450 bool override_set = false;
451 int32_t random_base = time(NULL) % 1024;
452 // some more randomness from /dev/random
453 FILE *f_random = fopen("/dev/random", "rb");
454 uint16_t random_bits = 0;
455 if (f_random != NULL) {
456 fread(&random_bits, 2, 1, f_random);
457 random_bits = random_bits % 1024;
458 fclose(f_random);
459 }
460 random_base = (random_base + random_bits) % 1024;
461 fprintf(ui->cmd_pipe, "ui_print Random offset: 0x%x\n", random_base);
462 fprintf(ui->cmd_pipe, "ui_print\n");
463
464 // make sure we never randomize to zero; this let's us look at a file
465 // and know for sure whether it has been processed; important in the
466 // crash recovery process
467 if (random_base == 0) random_base = 1;
468 // make sure our randomization is page-aligned
469 random_base *= -0x1000;
470 override_base = random_base;
471
472 int i = 0;
473 bool success = true;
474 while (i < (argc - 1)) {
475 success = success && retouch_one_library(retouch_entries[i],
476 retouch_entries[i+1],
477 random_base,
478 override_set ?
479 NULL :
480 &override_base);
481 if (!success)
482 ErrorAbort(state, "Failed to retouch '%s'.", retouch_entries[i]);
483
484 free(retouch_entries[i]);
485 free(retouch_entries[i+1]);
486 i += 2;
487
488 if (success && override_base != 0) {
489 random_base = override_base;
490 override_set = true;
491 }
492 }
493 if (i < argc) {
494 free(retouch_entries[i]);
495 success = false;
496 }
497 free(retouch_entries);
498
499 if (!success) {
500 Value* v = malloc(sizeof(Value));
501 v->type = VAL_STRING;
502 v->data = NULL;
503 v->size = -1;
504 return v;
505 }
506 return StringValue(strdup("t"));
507}
508
509
510// undo_retouch_binaries(lib1, lib2, ...)
511Value* UndoRetouchBinariesFn(const char* name, State* state,
512 int argc, Expr* argv[]) {
513 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
514
515 char **retouch_entries = ReadVarArgs(state, argc, argv);
516 if (retouch_entries == NULL) {
517 return StringValue(strdup("t"));
518 }
519
520 int i = 0;
521 bool success = true;
522 int32_t override_base;
523 while (i < (argc-1)) {
524 success = success && retouch_one_library(retouch_entries[i],
525 retouch_entries[i+1],
526 0 /* undo => offset==0 */,
527 NULL);
528 if (!success)
529 ErrorAbort(state, "Failed to unretouch '%s'.",
530 retouch_entries[i]);
531
532 free(retouch_entries[i]);
533 free(retouch_entries[i+1]);
534 i += 2;
535 }
536 if (i < argc) {
537 free(retouch_entries[i]);
538 success = false;
539 }
540 free(retouch_entries);
541
542 if (!success) {
543 Value* v = malloc(sizeof(Value));
544 v->type = VAL_STRING;
545 v->data = NULL;
546 v->size = -1;
547 return v;
548 }
549 return StringValue(strdup("t"));
550}
551
552
Doug Zongker9931f7f2009-06-10 14:11:53 -0700553// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700554// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800555Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700556 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700557 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700558 }
559 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700560 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700561 if (target == NULL) return NULL;
562
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700563 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700564 if (srcs == NULL) {
565 free(target);
566 return NULL;
567 }
568
569 int i;
570 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700571 if (unlink(srcs[i]) < 0) {
572 if (errno != ENOENT) {
573 fprintf(stderr, "%s: failed to remove %s: %s\n",
574 name, srcs[i], strerror(errno));
575 }
576 }
577 if (symlink(target, srcs[i]) < 0) {
578 fprintf(stderr, "%s: failed to symlink %s to %s: %s\n",
579 name, srcs[i], target, strerror(errno));
580 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700581 free(srcs[i]);
582 }
583 free(srcs);
Doug Zongker512536a2010-02-17 16:11:44 -0800584 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700585}
586
Doug Zongker8edb00c2009-06-11 17:21:44 -0700587
Doug Zongker512536a2010-02-17 16:11:44 -0800588Value* SetPermFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700589 char* result = NULL;
590 bool recursive = (strcmp(name, "set_perm_recursive") == 0);
591
592 int min_args = 4 + (recursive ? 1 : 0);
593 if (argc < min_args) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700594 return ErrorAbort(state, "%s() expects %d+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700595 }
596
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700597 char** args = ReadVarArgs(state, argc, argv);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700598 if (args == NULL) return NULL;
599
600 char* end;
601 int i;
602
603 int uid = strtoul(args[0], &end, 0);
604 if (*end != '\0' || args[0][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700605 ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700606 goto done;
607 }
608
609 int gid = strtoul(args[1], &end, 0);
610 if (*end != '\0' || args[1][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700611 ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700612 goto done;
613 }
614
615 if (recursive) {
616 int dir_mode = strtoul(args[2], &end, 0);
617 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700618 ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700619 goto done;
620 }
621
622 int file_mode = strtoul(args[3], &end, 0);
623 if (*end != '\0' || args[3][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700624 ErrorAbort(state, "%s: \"%s\" not a valid filemode",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700625 name, args[3]);
626 goto done;
627 }
628
629 for (i = 4; i < argc; ++i) {
630 dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode);
631 }
632 } else {
633 int mode = strtoul(args[2], &end, 0);
634 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700635 ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700636 goto done;
637 }
638
Doug Zongker0bbfe3d2009-06-25 13:37:31 -0700639 for (i = 3; i < argc; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700640 if (chown(args[i], uid, gid) < 0) {
641 fprintf(stderr, "%s: chown of %s to %d %d failed: %s\n",
642 name, args[i], uid, gid, strerror(errno));
643 }
644 if (chmod(args[i], mode) < 0) {
645 fprintf(stderr, "%s: chmod of %s to %o failed: %s\n",
646 name, args[i], mode, strerror(errno));
647 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700648 }
649 }
650 result = strdup("");
651
652done:
653 for (i = 0; i < argc; ++i) {
654 free(args[i]);
655 }
656 free(args);
657
Doug Zongker512536a2010-02-17 16:11:44 -0800658 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700659}
660
Doug Zongker8edb00c2009-06-11 17:21:44 -0700661
Doug Zongker512536a2010-02-17 16:11:44 -0800662Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700663 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700664 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700665 }
666 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700667 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700668 if (key == NULL) return NULL;
669
670 char value[PROPERTY_VALUE_MAX];
671 property_get(key, value, "");
672 free(key);
673
Doug Zongker512536a2010-02-17 16:11:44 -0800674 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700675}
676
677
Doug Zongker47cace92009-06-18 10:11:50 -0700678// file_getprop(file, key)
679//
680// interprets 'file' as a getprop-style file (key=value pairs, one
681// per line, # comment lines and blank lines okay), and returns the value
682// for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800683Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700684 char* result = NULL;
685 char* buffer = NULL;
686 char* filename;
687 char* key;
688 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
689 return NULL;
690 }
691
692 struct stat st;
693 if (stat(filename, &st) < 0) {
694 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
695 name, filename, strerror(errno));
696 goto done;
697 }
698
699#define MAX_FILE_GETPROP_SIZE 65536
700
701 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
702 ErrorAbort(state, "%s too large for %s (max %d)",
703 filename, name, MAX_FILE_GETPROP_SIZE);
704 goto done;
705 }
706
707 buffer = malloc(st.st_size+1);
708 if (buffer == NULL) {
709 ErrorAbort(state, "%s: failed to alloc %d bytes", name, st.st_size+1);
710 goto done;
711 }
712
713 FILE* f = fopen(filename, "rb");
714 if (f == NULL) {
715 ErrorAbort(state, "%s: failed to open %s: %s",
716 name, filename, strerror(errno));
717 goto done;
718 }
719
720 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
721 ErrorAbort(state, "%s: failed to read %d bytes from %s",
722 name, st.st_size+1, filename);
723 fclose(f);
724 goto done;
725 }
726 buffer[st.st_size] = '\0';
727
728 fclose(f);
729
730 char* line = strtok(buffer, "\n");
731 do {
732 // skip whitespace at start of line
733 while (*line && isspace(*line)) ++line;
734
735 // comment or blank line: skip to next line
736 if (*line == '\0' || *line == '#') continue;
737
738 char* equal = strchr(line, '=');
739 if (equal == NULL) {
740 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
741 name, line, filename);
742 goto done;
743 }
744
745 // trim whitespace between key and '='
746 char* key_end = equal-1;
747 while (key_end > line && isspace(*key_end)) --key_end;
748 key_end[1] = '\0';
749
750 // not the key we're looking for
751 if (strcmp(key, line) != 0) continue;
752
753 // skip whitespace after the '=' to the start of the value
754 char* val_start = equal+1;
755 while(*val_start && isspace(*val_start)) ++val_start;
756
757 // trim trailing whitespace
758 char* val_end = val_start + strlen(val_start)-1;
759 while (val_end > val_start && isspace(*val_end)) --val_end;
760 val_end[1] = '\0';
761
762 result = strdup(val_start);
763 break;
764
765 } while ((line = strtok(NULL, "\n")));
766
767 if (result == NULL) result = strdup("");
768
769 done:
770 free(filename);
771 free(key);
772 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800773 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -0700774}
775
776
Doug Zongker8edb00c2009-06-11 17:21:44 -0700777static bool write_raw_image_cb(const unsigned char* data,
778 int data_len, void* ctx) {
779 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
780 if (r == data_len) return true;
781 fprintf(stderr, "%s\n", strerror(errno));
782 return false;
783}
784
785// write_raw_image(file, partition)
Doug Zongker512536a2010-02-17 16:11:44 -0800786Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700787 char* result = NULL;
788
789 char* partition;
790 char* filename;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700791 if (ReadArgs(state, argv, 2, &filename, &partition) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700792 return NULL;
793 }
794
795 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700796 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700797 goto done;
798 }
799 if (strlen(filename) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700800 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700801 goto done;
802 }
803
804 mtd_scan_partitions();
805 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
806 if (mtd == NULL) {
807 fprintf(stderr, "%s: no mtd partition named \"%s\"\n", name, partition);
808 result = strdup("");
809 goto done;
810 }
811
812 MtdWriteContext* ctx = mtd_write_partition(mtd);
813 if (ctx == NULL) {
814 fprintf(stderr, "%s: can't write mtd partition \"%s\"\n",
815 name, partition);
816 result = strdup("");
817 goto done;
818 }
819
820 bool success;
821
822 FILE* f = fopen(filename, "rb");
823 if (f == NULL) {
824 fprintf(stderr, "%s: can't open %s: %s\n",
825 name, filename, strerror(errno));
826 result = strdup("");
827 goto done;
828 }
829
830 success = true;
831 char* buffer = malloc(BUFSIZ);
832 int read;
833 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
834 int wrote = mtd_write_data(ctx, buffer, read);
835 success = success && (wrote == read);
836 if (!success) {
837 fprintf(stderr, "mtd_write_data to %s failed: %s\n",
838 partition, strerror(errno));
839 }
840 }
841 free(buffer);
842 fclose(f);
843
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700844 if (mtd_erase_blocks(ctx, -1) == -1) {
845 fprintf(stderr, "%s: error erasing blocks of %s\n", name, partition);
846 }
847 if (mtd_write_close(ctx) != 0) {
848 fprintf(stderr, "%s: error closing write of %s\n", name, partition);
849 }
850
Doug Zongker8edb00c2009-06-11 17:21:44 -0700851 printf("%s %s partition from %s\n",
852 success ? "wrote" : "failed to write", partition, filename);
853
854 result = success ? partition : strdup("");
855
856done:
857 if (result != partition) free(partition);
858 free(filename);
Doug Zongker512536a2010-02-17 16:11:44 -0800859 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700860}
861
Doug Zongker8edb00c2009-06-11 17:21:44 -0700862// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -0800863Value* ApplyPatchSpaceFn(const char* name, State* state,
864 int argc, Expr* argv[]) {
865 char* bytes_str;
866 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
867 return NULL;
868 }
869
870 char* endptr;
871 size_t bytes = strtol(bytes_str, &endptr, 10);
872 if (bytes == 0 && endptr == bytes_str) {
873 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
874 name, bytes_str);
875 free(bytes_str);
876 return NULL;
877 }
878
879 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
880}
881
882
883// apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1_1, patch_1, ...)
Doug Zongker512536a2010-02-17 16:11:44 -0800884Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800885 if (argc < 6 || (argc % 2) == 1) {
886 return ErrorAbort(state, "%s(): expected at least 6 args and an "
887 "even number, got %d",
888 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700889 }
890
Doug Zongkerc4351c72010-02-22 14:46:32 -0800891 char* source_filename;
892 char* target_filename;
893 char* target_sha1;
894 char* target_size_str;
895 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
896 &target_sha1, &target_size_str) < 0) {
897 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700898 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700899
Doug Zongkerc4351c72010-02-22 14:46:32 -0800900 char* endptr;
901 size_t target_size = strtol(target_size_str, &endptr, 10);
902 if (target_size == 0 && endptr == target_size_str) {
903 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
904 name, target_size_str);
905 free(source_filename);
906 free(target_filename);
907 free(target_sha1);
908 free(target_size_str);
909 return NULL;
910 }
911
912 int patchcount = (argc-4) / 2;
913 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700914
915 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800916 for (i = 0; i < patchcount; ++i) {
917 if (patches[i*2]->type != VAL_STRING) {
918 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
919 break;
920 }
921 if (patches[i*2+1]->type != VAL_BLOB) {
922 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
923 break;
924 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700925 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800926 if (i != patchcount) {
927 for (i = 0; i < patchcount*2; ++i) {
928 FreeValue(patches[i]);
929 }
930 free(patches);
931 return NULL;
932 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700933
Doug Zongkerc4351c72010-02-22 14:46:32 -0800934 char** patch_sha_str = malloc(patchcount * sizeof(char*));
935 for (i = 0; i < patchcount; ++i) {
936 patch_sha_str[i] = patches[i*2]->data;
937 patches[i*2]->data = NULL;
938 FreeValue(patches[i*2]);
939 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700940 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800941
942 int result = applypatch(source_filename, target_filename,
943 target_sha1, target_size,
944 patchcount, patch_sha_str, patches);
945
946 for (i = 0; i < patchcount; ++i) {
947 FreeValue(patches[i]);
948 }
949 free(patch_sha_str);
950 free(patches);
951
952 return StringValue(strdup(result == 0 ? "t" : ""));
953}
954
955// apply_patch_check(file, [sha1_1, ...])
956Value* ApplyPatchCheckFn(const char* name, State* state,
957 int argc, Expr* argv[]) {
958 if (argc < 1) {
959 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
960 name, argc);
961 }
962
963 char* filename;
964 if (ReadArgs(state, argv, 1, &filename) < 0) {
965 return NULL;
966 }
967
968 int patchcount = argc-1;
969 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
970
971 int result = applypatch_check(filename, patchcount, sha1s);
972
973 int i;
974 for (i = 0; i < patchcount; ++i) {
975 free(sha1s[i]);
976 }
977 free(sha1s);
978
979 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700980}
981
Doug Zongker512536a2010-02-17 16:11:44 -0800982Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700983 char** args = ReadVarArgs(state, argc, argv);
984 if (args == NULL) {
985 return NULL;
986 }
987
988 int size = 0;
989 int i;
990 for (i = 0; i < argc; ++i) {
991 size += strlen(args[i]);
992 }
993 char* buffer = malloc(size+1);
994 size = 0;
995 for (i = 0; i < argc; ++i) {
996 strcpy(buffer+size, args[i]);
997 size += strlen(args[i]);
998 free(args[i]);
999 }
1000 free(args);
1001 buffer[size] = '\0';
1002
1003 char* line = strtok(buffer, "\n");
1004 while (line) {
1005 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
1006 "ui_print %s\n", line);
1007 line = strtok(NULL, "\n");
1008 }
1009 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
1010
Doug Zongker512536a2010-02-17 16:11:44 -08001011 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001012}
1013
Doug Zongker512536a2010-02-17 16:11:44 -08001014Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001015 if (argc < 1) {
1016 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1017 }
1018 char** args = ReadVarArgs(state, argc, argv);
1019 if (args == NULL) {
1020 return NULL;
1021 }
1022
1023 char** args2 = malloc(sizeof(char*) * (argc+1));
1024 memcpy(args2, args, sizeof(char*) * argc);
1025 args2[argc] = NULL;
1026
1027 fprintf(stderr, "about to run program [%s] with %d args\n", args2[0], argc);
1028
1029 pid_t child = fork();
1030 if (child == 0) {
1031 execv(args2[0], args2);
1032 fprintf(stderr, "run_program: execv failed: %s\n", strerror(errno));
1033 _exit(1);
1034 }
1035 int status;
1036 waitpid(child, &status, 0);
1037 if (WIFEXITED(status)) {
1038 if (WEXITSTATUS(status) != 0) {
1039 fprintf(stderr, "run_program: child exited with status %d\n",
1040 WEXITSTATUS(status));
1041 }
1042 } else if (WIFSIGNALED(status)) {
1043 fprintf(stderr, "run_program: child terminated by signal %d\n",
1044 WTERMSIG(status));
1045 }
1046
1047 int i;
1048 for (i = 0; i < argc; ++i) {
1049 free(args[i]);
1050 }
1051 free(args);
1052 free(args2);
1053
1054 char buffer[20];
1055 sprintf(buffer, "%d", status);
1056
Doug Zongker512536a2010-02-17 16:11:44 -08001057 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001058}
1059
Doug Zongker512536a2010-02-17 16:11:44 -08001060// Take a sha-1 digest and return it as a newly-allocated hex string.
1061static char* PrintSha1(uint8_t* digest) {
1062 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
1063 int i;
1064 const char* alphabet = "0123456789abcdef";
1065 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
1066 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
1067 buffer[i*2+1] = alphabet[digest[i] & 0xf];
1068 }
1069 buffer[i*2] = '\0';
1070 return buffer;
1071}
1072
1073// sha1_check(data)
1074// to return the sha1 of the data (given in the format returned by
1075// read_file).
1076//
1077// sha1_check(data, sha1_hex, [sha1_hex, ...])
1078// returns the sha1 of the file if it matches any of the hex
1079// strings passed, or "" if it does not equal any of them.
1080//
1081Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1082 if (argc < 1) {
1083 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1084 }
1085
1086 Value** args = ReadValueVarArgs(state, argc, argv);
1087 if (args == NULL) {
1088 return NULL;
1089 }
1090
1091 if (args[0]->size < 0) {
1092 fprintf(stderr, "%s(): no file contents received", name);
1093 return StringValue(strdup(""));
1094 }
1095 uint8_t digest[SHA_DIGEST_SIZE];
1096 SHA(args[0]->data, args[0]->size, digest);
1097 FreeValue(args[0]);
1098
1099 if (argc == 1) {
1100 return StringValue(PrintSha1(digest));
1101 }
1102
1103 int i;
1104 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1105 for (i = 1; i < argc; ++i) {
1106 if (args[i]->type != VAL_STRING) {
1107 fprintf(stderr, "%s(): arg %d is not a string; skipping",
1108 name, i);
1109 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1110 // Warn about bad args and skip them.
1111 fprintf(stderr, "%s(): error parsing \"%s\" as sha-1; skipping",
1112 name, args[i]->data);
1113 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1114 break;
1115 }
1116 FreeValue(args[i]);
1117 }
1118 if (i >= argc) {
1119 // Didn't match any of the hex strings; return false.
1120 return StringValue(strdup(""));
1121 }
1122 // Found a match; free all the remaining arguments and return the
1123 // matched one.
1124 int j;
1125 for (j = i+1; j < argc; ++j) {
1126 FreeValue(args[j]);
1127 }
1128 return args[i];
1129}
1130
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001131// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001132// is actually a FileContents*).
1133Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1134 if (argc != 1) {
1135 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1136 }
1137 char* filename;
1138 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1139
1140 Value* v = malloc(sizeof(Value));
1141 v->type = VAL_BLOB;
1142
1143 FileContents fc;
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001144 if (LoadFileContents(filename, &fc, RETOUCH_DONT_MASK) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001145 ErrorAbort(state, "%s() loading \"%s\" failed: %s",
1146 name, filename, strerror(errno));
1147 free(filename);
1148 free(v);
1149 free(fc.data);
1150 return NULL;
1151 }
1152
1153 v->size = fc.size;
1154 v->data = (char*)fc.data;
1155
1156 free(filename);
1157 return v;
1158}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001159
Doug Zongker9931f7f2009-06-10 14:11:53 -07001160void RegisterInstallFunctions() {
1161 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001162 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001163 RegisterFunction("unmount", UnmountFn);
1164 RegisterFunction("format", FormatFn);
1165 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001166 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001167 RegisterFunction("delete", DeleteFn);
1168 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001169 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1170 RegisterFunction("package_extract_file", PackageExtractFileFn);
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001171 RegisterFunction("retouch_binaries", RetouchBinariesFn);
1172 RegisterFunction("undo_retouch_binaries", UndoRetouchBinariesFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001173 RegisterFunction("symlink", SymlinkFn);
1174 RegisterFunction("set_perm", SetPermFn);
1175 RegisterFunction("set_perm_recursive", SetPermFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001176
1177 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001178 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001179 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001180
1181 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001182 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1183 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001184
Doug Zongker512536a2010-02-17 16:11:44 -08001185 RegisterFunction("read_file", ReadFileFn);
1186 RegisterFunction("sha1_check", Sha1CheckFn);
1187
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001188 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001189
1190 RegisterFunction("run_program", RunProgramFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001191}