blob: c087d4ebe10cd68fc786eb1f95227aac9dee0f57 [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
81 mkdir(mount_point, 0755);
82
Doug Zongker3d177d02010-07-01 09:18:44 -070083 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070084 mtd_scan_partitions();
85 const MtdPartition* mtd;
86 mtd = mtd_find_partition_by_name(location);
87 if (mtd == NULL) {
88 fprintf(stderr, "%s: no mtd partition named \"%s\"",
89 name, location);
90 result = strdup("");
91 goto done;
92 }
Doug Zongker3d177d02010-07-01 09:18:44 -070093 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070094 fprintf(stderr, "mtd mount of %s failed: %s\n",
95 location, strerror(errno));
96 result = strdup("");
97 goto done;
98 }
99 result = mount_point;
100 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700101 if (mount(location, mount_point, fs_type,
Doug Zongker9931f7f2009-06-10 14:11:53 -0700102 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
Doug Zongker60babf82009-09-18 15:11:24 -0700103 fprintf(stderr, "%s: failed to mount %s at %s: %s\n",
104 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700105 result = strdup("");
106 } else {
107 result = mount_point;
108 }
109 }
110
111done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700112 free(fs_type);
113 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700114 free(location);
115 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800116 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700117}
118
Doug Zongker8edb00c2009-06-11 17:21:44 -0700119
120// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800121Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700122 char* result = NULL;
123 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700124 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700125 }
126 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700127 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700128 return NULL;
129 }
130 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700131 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700132 goto done;
133 }
134
135 scan_mounted_volumes();
136 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
137 if (vol == NULL) {
138 result = strdup("");
139 } else {
140 result = mount_point;
141 }
142
143done:
144 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800145 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700146}
147
148
Doug Zongker512536a2010-02-17 16:11:44 -0800149Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700150 char* result = NULL;
151 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700152 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700153 }
154 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700155 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700156 return NULL;
157 }
158 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700159 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700160 goto done;
161 }
162
163 scan_mounted_volumes();
164 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
165 if (vol == NULL) {
166 fprintf(stderr, "unmount of %s failed; no such volume\n", mount_point);
167 result = strdup("");
168 } else {
169 unmount_mounted_volume(vol);
170 result = mount_point;
171 }
172
173done:
174 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800175 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700176}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700177
178
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800179// format(fs_type, partition_type, location, fs_size)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700180//
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800181// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes>
182// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes>
183// if fs_size == 0, then make_ext4fs uses the entire partition.
184// if fs_size > 0, that is the size to use
185// if fs_size < 0, then reserve that many bytes at the end of the partition
Doug Zongker512536a2010-02-17 16:11:44 -0800186Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700187 char* result = NULL;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800188 if (argc != 4) {
189 return ErrorAbort(state, "%s() expects 4 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700190 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700191 char* fs_type;
192 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700193 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800194 char* fs_size;
195 if (ReadArgs(state, argv, 4, &fs_type, &partition_type, &location, &fs_size) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700196 return NULL;
197 }
198
Doug Zongker3d177d02010-07-01 09:18:44 -0700199 if (strlen(fs_type) == 0) {
200 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
201 goto done;
202 }
203 if (strlen(partition_type) == 0) {
204 ErrorAbort(state, "partition_type argument to %s() can't be empty",
205 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700206 goto done;
207 }
208 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700209 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700210 goto done;
211 }
212
Doug Zongker3d177d02010-07-01 09:18:44 -0700213 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700214 mtd_scan_partitions();
215 const MtdPartition* mtd = mtd_find_partition_by_name(location);
216 if (mtd == NULL) {
217 fprintf(stderr, "%s: no mtd partition named \"%s\"",
218 name, location);
219 result = strdup("");
220 goto done;
221 }
222 MtdWriteContext* ctx = mtd_write_partition(mtd);
223 if (ctx == NULL) {
224 fprintf(stderr, "%s: can't write \"%s\"", name, location);
225 result = strdup("");
226 goto done;
227 }
228 if (mtd_erase_blocks(ctx, -1) == -1) {
229 mtd_write_close(ctx);
230 fprintf(stderr, "%s: failed to erase \"%s\"", name, location);
231 result = strdup("");
232 goto done;
233 }
234 if (mtd_write_close(ctx) != 0) {
235 fprintf(stderr, "%s: failed to close \"%s\"", name, location);
236 result = strdup("");
237 goto done;
238 }
239 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700240#ifdef USE_EXT4
241 } else if (strcmp(fs_type, "ext4") == 0) {
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800242 int status = make_ext4fs(location, atoll(fs_size));
Doug Zongker3d177d02010-07-01 09:18:44 -0700243 if (status != 0) {
244 fprintf(stderr, "%s: make_ext4fs failed (%d) on %s",
245 name, status, location);
246 result = strdup("");
247 goto done;
248 }
249 result = location;
250#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700251 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700252 fprintf(stderr, "%s: unsupported fs_type \"%s\" partition_type \"%s\"",
253 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700254 }
255
256done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700257 free(fs_type);
258 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700259 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800260 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700261}
262
Doug Zongker8edb00c2009-06-11 17:21:44 -0700263
Doug Zongker512536a2010-02-17 16:11:44 -0800264Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700265 char** paths = malloc(argc * sizeof(char*));
266 int i;
267 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700268 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700269 if (paths[i] == NULL) {
270 int j;
271 for (j = 0; j < i; ++i) {
272 free(paths[j]);
273 }
274 free(paths);
275 return NULL;
276 }
277 }
278
279 bool recursive = (strcmp(name, "delete_recursive") == 0);
280
281 int success = 0;
282 for (i = 0; i < argc; ++i) {
283 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
284 ++success;
285 free(paths[i]);
286 }
287 free(paths);
288
289 char buffer[10];
290 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800291 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700292}
293
Doug Zongker8edb00c2009-06-11 17:21:44 -0700294
Doug Zongker512536a2010-02-17 16:11:44 -0800295Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700296 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700297 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700298 }
299 char* frac_str;
300 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700301 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700302 return NULL;
303 }
304
305 double frac = strtod(frac_str, NULL);
306 int sec = strtol(sec_str, NULL, 10);
307
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700308 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700309 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
310
Doug Zongker9931f7f2009-06-10 14:11:53 -0700311 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800312 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700313}
314
Doug Zongker512536a2010-02-17 16:11:44 -0800315Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700316 if (argc != 1) {
317 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
318 }
319 char* frac_str;
320 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
321 return NULL;
322 }
323
324 double frac = strtod(frac_str, NULL);
325
326 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
327 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
328
Doug Zongker512536a2010-02-17 16:11:44 -0800329 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700330}
331
Doug Zongker8edb00c2009-06-11 17:21:44 -0700332// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800333Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700334 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700335 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700336 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700337 }
338 char* zip_path;
339 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700340 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700341
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700342 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700343
344 // To create a consistent system image, never use the clock for timestamps.
345 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
346
347 bool success = mzExtractRecursive(za, zip_path, dest_path,
348 MZ_EXTRACT_FILES_ONLY, &timestamp,
349 NULL, NULL);
350 free(zip_path);
351 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800352 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700353}
354
Doug Zongker8edb00c2009-06-11 17:21:44 -0700355
356// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800357// or
358// package_extract_file(package_path)
359// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800360// function (the char* returned is actually a FileContents*).
361Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700362 int argc, Expr* argv[]) {
Doug Zongker6aece332010-02-01 14:40:12 -0800363 if (argc != 1 && argc != 2) {
364 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
365 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700366 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700367 bool success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800368 if (argc == 2) {
369 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700370
Doug Zongker6aece332010-02-01 14:40:12 -0800371 char* zip_path;
372 char* dest_path;
373 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
374
375 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
376 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
377 if (entry == NULL) {
378 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
379 goto done2;
380 }
381
382 FILE* f = fopen(dest_path, "wb");
383 if (f == NULL) {
384 fprintf(stderr, "%s: can't open %s for write: %s\n",
385 name, dest_path, strerror(errno));
386 goto done2;
387 }
388 success = mzExtractZipEntryToFile(za, entry, fileno(f));
389 fclose(f);
390
391 done2:
392 free(zip_path);
393 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800394 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800395 } else {
396 // The one-argument version returns the contents of the file
397 // as the result.
398
399 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800400 Value* v = malloc(sizeof(Value));
401 v->type = VAL_BLOB;
402 v->size = -1;
403 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800404
405 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
406
407 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
408 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
409 if (entry == NULL) {
410 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
411 goto done1;
412 }
413
Doug Zongker512536a2010-02-17 16:11:44 -0800414 v->size = mzGetZipEntryUncompLen(entry);
415 v->data = malloc(v->size);
416 if (v->data == NULL) {
Doug Zongker6aece332010-02-01 14:40:12 -0800417 fprintf(stderr, "%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800418 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800419 goto done1;
420 }
421
Doug Zongker512536a2010-02-17 16:11:44 -0800422 success = mzExtractZipEntryToBuffer(za, entry,
423 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800424
425 done1:
426 free(zip_path);
427 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800428 free(v->data);
429 v->data = NULL;
430 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800431 }
Doug Zongker512536a2010-02-17 16:11:44 -0800432 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700433 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700434}
435
436
Doug Zongker9931f7f2009-06-10 14:11:53 -0700437// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700438// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800439Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700440 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700441 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700442 }
443 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700444 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700445 if (target == NULL) return NULL;
446
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700447 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700448 if (srcs == NULL) {
449 free(target);
450 return NULL;
451 }
452
453 int i;
454 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700455 if (unlink(srcs[i]) < 0) {
456 if (errno != ENOENT) {
457 fprintf(stderr, "%s: failed to remove %s: %s\n",
458 name, srcs[i], strerror(errno));
459 }
460 }
461 if (symlink(target, srcs[i]) < 0) {
462 fprintf(stderr, "%s: failed to symlink %s to %s: %s\n",
463 name, srcs[i], target, strerror(errno));
464 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700465 free(srcs[i]);
466 }
467 free(srcs);
Doug Zongker512536a2010-02-17 16:11:44 -0800468 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700469}
470
Doug Zongker8edb00c2009-06-11 17:21:44 -0700471
Doug Zongker512536a2010-02-17 16:11:44 -0800472Value* SetPermFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700473 char* result = NULL;
474 bool recursive = (strcmp(name, "set_perm_recursive") == 0);
475
476 int min_args = 4 + (recursive ? 1 : 0);
477 if (argc < min_args) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700478 return ErrorAbort(state, "%s() expects %d+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700479 }
480
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700481 char** args = ReadVarArgs(state, argc, argv);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700482 if (args == NULL) return NULL;
483
484 char* end;
485 int i;
486
487 int uid = strtoul(args[0], &end, 0);
488 if (*end != '\0' || args[0][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700489 ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700490 goto done;
491 }
492
493 int gid = strtoul(args[1], &end, 0);
494 if (*end != '\0' || args[1][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700495 ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700496 goto done;
497 }
498
499 if (recursive) {
500 int dir_mode = strtoul(args[2], &end, 0);
501 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700502 ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700503 goto done;
504 }
505
506 int file_mode = strtoul(args[3], &end, 0);
507 if (*end != '\0' || args[3][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700508 ErrorAbort(state, "%s: \"%s\" not a valid filemode",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700509 name, args[3]);
510 goto done;
511 }
512
513 for (i = 4; i < argc; ++i) {
514 dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode);
515 }
516 } else {
517 int mode = strtoul(args[2], &end, 0);
518 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700519 ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700520 goto done;
521 }
522
Doug Zongker0bbfe3d2009-06-25 13:37:31 -0700523 for (i = 3; i < argc; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700524 if (chown(args[i], uid, gid) < 0) {
525 fprintf(stderr, "%s: chown of %s to %d %d failed: %s\n",
526 name, args[i], uid, gid, strerror(errno));
527 }
528 if (chmod(args[i], mode) < 0) {
529 fprintf(stderr, "%s: chmod of %s to %o failed: %s\n",
530 name, args[i], mode, strerror(errno));
531 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700532 }
533 }
534 result = strdup("");
535
536done:
537 for (i = 0; i < argc; ++i) {
538 free(args[i]);
539 }
540 free(args);
541
Doug Zongker512536a2010-02-17 16:11:44 -0800542 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700543}
544
Doug Zongker8edb00c2009-06-11 17:21:44 -0700545
Doug Zongker512536a2010-02-17 16:11:44 -0800546Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700547 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700548 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700549 }
550 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700551 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700552 if (key == NULL) return NULL;
553
554 char value[PROPERTY_VALUE_MAX];
555 property_get(key, value, "");
556 free(key);
557
Doug Zongker512536a2010-02-17 16:11:44 -0800558 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700559}
560
561
Doug Zongker47cace92009-06-18 10:11:50 -0700562// file_getprop(file, key)
563//
564// interprets 'file' as a getprop-style file (key=value pairs, one
565// per line, # comment lines and blank lines okay), and returns the value
566// for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800567Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700568 char* result = NULL;
569 char* buffer = NULL;
570 char* filename;
571 char* key;
572 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
573 return NULL;
574 }
575
576 struct stat st;
577 if (stat(filename, &st) < 0) {
578 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
579 name, filename, strerror(errno));
580 goto done;
581 }
582
583#define MAX_FILE_GETPROP_SIZE 65536
584
585 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
586 ErrorAbort(state, "%s too large for %s (max %d)",
587 filename, name, MAX_FILE_GETPROP_SIZE);
588 goto done;
589 }
590
591 buffer = malloc(st.st_size+1);
592 if (buffer == NULL) {
593 ErrorAbort(state, "%s: failed to alloc %d bytes", name, st.st_size+1);
594 goto done;
595 }
596
597 FILE* f = fopen(filename, "rb");
598 if (f == NULL) {
599 ErrorAbort(state, "%s: failed to open %s: %s",
600 name, filename, strerror(errno));
601 goto done;
602 }
603
604 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
605 ErrorAbort(state, "%s: failed to read %d bytes from %s",
606 name, st.st_size+1, filename);
607 fclose(f);
608 goto done;
609 }
610 buffer[st.st_size] = '\0';
611
612 fclose(f);
613
614 char* line = strtok(buffer, "\n");
615 do {
616 // skip whitespace at start of line
617 while (*line && isspace(*line)) ++line;
618
619 // comment or blank line: skip to next line
620 if (*line == '\0' || *line == '#') continue;
621
622 char* equal = strchr(line, '=');
623 if (equal == NULL) {
624 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
625 name, line, filename);
626 goto done;
627 }
628
629 // trim whitespace between key and '='
630 char* key_end = equal-1;
631 while (key_end > line && isspace(*key_end)) --key_end;
632 key_end[1] = '\0';
633
634 // not the key we're looking for
635 if (strcmp(key, line) != 0) continue;
636
637 // skip whitespace after the '=' to the start of the value
638 char* val_start = equal+1;
639 while(*val_start && isspace(*val_start)) ++val_start;
640
641 // trim trailing whitespace
642 char* val_end = val_start + strlen(val_start)-1;
643 while (val_end > val_start && isspace(*val_end)) --val_end;
644 val_end[1] = '\0';
645
646 result = strdup(val_start);
647 break;
648
649 } while ((line = strtok(NULL, "\n")));
650
651 if (result == NULL) result = strdup("");
652
653 done:
654 free(filename);
655 free(key);
656 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800657 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -0700658}
659
660
Doug Zongker8edb00c2009-06-11 17:21:44 -0700661static bool write_raw_image_cb(const unsigned char* data,
662 int data_len, void* ctx) {
663 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
664 if (r == data_len) return true;
665 fprintf(stderr, "%s\n", strerror(errno));
666 return false;
667}
668
Doug Zongker179b2d92011-04-12 15:49:04 -0700669// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -0800670Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700671 char* result = NULL;
672
Doug Zongker179b2d92011-04-12 15:49:04 -0700673 Value* partition_value;
674 Value* contents;
675 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700676 return NULL;
677 }
678
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700679 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -0700680 if (partition_value->type != VAL_STRING) {
681 ErrorAbort(state, "partition argument to %s must be string", name);
682 goto done;
683 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700684 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700685 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700686 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700687 goto done;
688 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700689 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700690 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700691 goto done;
692 }
693
694 mtd_scan_partitions();
695 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
696 if (mtd == NULL) {
697 fprintf(stderr, "%s: no mtd partition named \"%s\"\n", name, partition);
698 result = strdup("");
699 goto done;
700 }
701
702 MtdWriteContext* ctx = mtd_write_partition(mtd);
703 if (ctx == NULL) {
704 fprintf(stderr, "%s: can't write mtd partition \"%s\"\n",
705 name, partition);
706 result = strdup("");
707 goto done;
708 }
709
710 bool success;
711
Doug Zongker179b2d92011-04-12 15:49:04 -0700712 if (contents->type == VAL_STRING) {
713 // we're given a filename as the contents
714 char* filename = contents->data;
715 FILE* f = fopen(filename, "rb");
716 if (f == NULL) {
717 fprintf(stderr, "%s: can't open %s: %s\n",
718 name, filename, strerror(errno));
719 result = strdup("");
720 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700721 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700722
723 success = true;
724 char* buffer = malloc(BUFSIZ);
725 int read;
726 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
727 int wrote = mtd_write_data(ctx, buffer, read);
728 success = success && (wrote == read);
729 }
730 free(buffer);
731 fclose(f);
732 } else {
733 // we're given a blob as the contents
734 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
735 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700736 }
Doug Zongker179b2d92011-04-12 15:49:04 -0700737 if (!success) {
738 fprintf(stderr, "mtd_write_data to %s failed: %s\n",
739 partition, strerror(errno));
740 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700741
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700742 if (mtd_erase_blocks(ctx, -1) == -1) {
743 fprintf(stderr, "%s: error erasing blocks of %s\n", name, partition);
744 }
745 if (mtd_write_close(ctx) != 0) {
746 fprintf(stderr, "%s: error closing write of %s\n", name, partition);
747 }
748
Doug Zongker179b2d92011-04-12 15:49:04 -0700749 printf("%s %s partition\n",
750 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700751
752 result = success ? partition : strdup("");
753
754done:
Doug Zongker179b2d92011-04-12 15:49:04 -0700755 if (result != partition) FreeValue(partition_value);
756 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -0800757 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700758}
759
Doug Zongker8edb00c2009-06-11 17:21:44 -0700760// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -0800761Value* ApplyPatchSpaceFn(const char* name, State* state,
762 int argc, Expr* argv[]) {
763 char* bytes_str;
764 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
765 return NULL;
766 }
767
768 char* endptr;
769 size_t bytes = strtol(bytes_str, &endptr, 10);
770 if (bytes == 0 && endptr == bytes_str) {
771 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
772 name, bytes_str);
773 free(bytes_str);
774 return NULL;
775 }
776
777 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
778}
779
780
781// apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1_1, patch_1, ...)
Doug Zongker512536a2010-02-17 16:11:44 -0800782Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800783 if (argc < 6 || (argc % 2) == 1) {
784 return ErrorAbort(state, "%s(): expected at least 6 args and an "
785 "even number, got %d",
786 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700787 }
788
Doug Zongkerc4351c72010-02-22 14:46:32 -0800789 char* source_filename;
790 char* target_filename;
791 char* target_sha1;
792 char* target_size_str;
793 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
794 &target_sha1, &target_size_str) < 0) {
795 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700796 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700797
Doug Zongkerc4351c72010-02-22 14:46:32 -0800798 char* endptr;
799 size_t target_size = strtol(target_size_str, &endptr, 10);
800 if (target_size == 0 && endptr == target_size_str) {
801 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
802 name, target_size_str);
803 free(source_filename);
804 free(target_filename);
805 free(target_sha1);
806 free(target_size_str);
807 return NULL;
808 }
809
810 int patchcount = (argc-4) / 2;
811 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700812
813 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800814 for (i = 0; i < patchcount; ++i) {
815 if (patches[i*2]->type != VAL_STRING) {
816 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
817 break;
818 }
819 if (patches[i*2+1]->type != VAL_BLOB) {
820 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
821 break;
822 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700823 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800824 if (i != patchcount) {
825 for (i = 0; i < patchcount*2; ++i) {
826 FreeValue(patches[i]);
827 }
828 free(patches);
829 return NULL;
830 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700831
Doug Zongkerc4351c72010-02-22 14:46:32 -0800832 char** patch_sha_str = malloc(patchcount * sizeof(char*));
833 for (i = 0; i < patchcount; ++i) {
834 patch_sha_str[i] = patches[i*2]->data;
835 patches[i*2]->data = NULL;
836 FreeValue(patches[i*2]);
837 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700838 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800839
840 int result = applypatch(source_filename, target_filename,
841 target_sha1, target_size,
842 patchcount, patch_sha_str, patches);
843
844 for (i = 0; i < patchcount; ++i) {
845 FreeValue(patches[i]);
846 }
847 free(patch_sha_str);
848 free(patches);
849
850 return StringValue(strdup(result == 0 ? "t" : ""));
851}
852
853// apply_patch_check(file, [sha1_1, ...])
854Value* ApplyPatchCheckFn(const char* name, State* state,
855 int argc, Expr* argv[]) {
856 if (argc < 1) {
857 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
858 name, argc);
859 }
860
861 char* filename;
862 if (ReadArgs(state, argv, 1, &filename) < 0) {
863 return NULL;
864 }
865
866 int patchcount = argc-1;
867 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
868
869 int result = applypatch_check(filename, patchcount, sha1s);
870
871 int i;
872 for (i = 0; i < patchcount; ++i) {
873 free(sha1s[i]);
874 }
875 free(sha1s);
876
877 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700878}
879
Doug Zongker512536a2010-02-17 16:11:44 -0800880Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700881 char** args = ReadVarArgs(state, argc, argv);
882 if (args == NULL) {
883 return NULL;
884 }
885
886 int size = 0;
887 int i;
888 for (i = 0; i < argc; ++i) {
889 size += strlen(args[i]);
890 }
891 char* buffer = malloc(size+1);
892 size = 0;
893 for (i = 0; i < argc; ++i) {
894 strcpy(buffer+size, args[i]);
895 size += strlen(args[i]);
896 free(args[i]);
897 }
898 free(args);
899 buffer[size] = '\0';
900
901 char* line = strtok(buffer, "\n");
902 while (line) {
903 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
904 "ui_print %s\n", line);
905 line = strtok(NULL, "\n");
906 }
907 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
908
Doug Zongker512536a2010-02-17 16:11:44 -0800909 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700910}
911
Doug Zongkerd0181b82011-10-19 10:51:12 -0700912Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
913 if (argc != 0) {
914 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
915 }
916 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
917 return StringValue(strdup("t"));
918}
919
Doug Zongker512536a2010-02-17 16:11:44 -0800920Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700921 if (argc < 1) {
922 return ErrorAbort(state, "%s() expects at least 1 arg", name);
923 }
924 char** args = ReadVarArgs(state, argc, argv);
925 if (args == NULL) {
926 return NULL;
927 }
928
929 char** args2 = malloc(sizeof(char*) * (argc+1));
930 memcpy(args2, args, sizeof(char*) * argc);
931 args2[argc] = NULL;
932
933 fprintf(stderr, "about to run program [%s] with %d args\n", args2[0], argc);
934
935 pid_t child = fork();
936 if (child == 0) {
937 execv(args2[0], args2);
938 fprintf(stderr, "run_program: execv failed: %s\n", strerror(errno));
939 _exit(1);
940 }
941 int status;
942 waitpid(child, &status, 0);
943 if (WIFEXITED(status)) {
944 if (WEXITSTATUS(status) != 0) {
945 fprintf(stderr, "run_program: child exited with status %d\n",
946 WEXITSTATUS(status));
947 }
948 } else if (WIFSIGNALED(status)) {
949 fprintf(stderr, "run_program: child terminated by signal %d\n",
950 WTERMSIG(status));
951 }
952
953 int i;
954 for (i = 0; i < argc; ++i) {
955 free(args[i]);
956 }
957 free(args);
958 free(args2);
959
960 char buffer[20];
961 sprintf(buffer, "%d", status);
962
Doug Zongker512536a2010-02-17 16:11:44 -0800963 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700964}
965
Doug Zongker512536a2010-02-17 16:11:44 -0800966// Take a sha-1 digest and return it as a newly-allocated hex string.
967static char* PrintSha1(uint8_t* digest) {
968 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
969 int i;
970 const char* alphabet = "0123456789abcdef";
971 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
972 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
973 buffer[i*2+1] = alphabet[digest[i] & 0xf];
974 }
975 buffer[i*2] = '\0';
976 return buffer;
977}
978
979// sha1_check(data)
980// to return the sha1 of the data (given in the format returned by
981// read_file).
982//
983// sha1_check(data, sha1_hex, [sha1_hex, ...])
984// returns the sha1 of the file if it matches any of the hex
985// strings passed, or "" if it does not equal any of them.
986//
987Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
988 if (argc < 1) {
989 return ErrorAbort(state, "%s() expects at least 1 arg", name);
990 }
991
992 Value** args = ReadValueVarArgs(state, argc, argv);
993 if (args == NULL) {
994 return NULL;
995 }
996
997 if (args[0]->size < 0) {
998 fprintf(stderr, "%s(): no file contents received", name);
999 return StringValue(strdup(""));
1000 }
1001 uint8_t digest[SHA_DIGEST_SIZE];
1002 SHA(args[0]->data, args[0]->size, digest);
1003 FreeValue(args[0]);
1004
1005 if (argc == 1) {
1006 return StringValue(PrintSha1(digest));
1007 }
1008
1009 int i;
1010 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1011 for (i = 1; i < argc; ++i) {
1012 if (args[i]->type != VAL_STRING) {
1013 fprintf(stderr, "%s(): arg %d is not a string; skipping",
1014 name, i);
1015 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1016 // Warn about bad args and skip them.
1017 fprintf(stderr, "%s(): error parsing \"%s\" as sha-1; skipping",
1018 name, args[i]->data);
1019 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1020 break;
1021 }
1022 FreeValue(args[i]);
1023 }
1024 if (i >= argc) {
1025 // Didn't match any of the hex strings; return false.
1026 return StringValue(strdup(""));
1027 }
1028 // Found a match; free all the remaining arguments and return the
1029 // matched one.
1030 int j;
1031 for (j = i+1; j < argc; ++j) {
1032 FreeValue(args[j]);
1033 }
1034 return args[i];
1035}
1036
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001037// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001038// is actually a FileContents*).
1039Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1040 if (argc != 1) {
1041 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1042 }
1043 char* filename;
1044 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1045
1046 Value* v = malloc(sizeof(Value));
1047 v->type = VAL_BLOB;
1048
1049 FileContents fc;
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001050 if (LoadFileContents(filename, &fc, RETOUCH_DONT_MASK) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001051 ErrorAbort(state, "%s() loading \"%s\" failed: %s",
1052 name, filename, strerror(errno));
1053 free(filename);
1054 free(v);
1055 free(fc.data);
1056 return NULL;
1057 }
1058
1059 v->size = fc.size;
1060 v->data = (char*)fc.data;
1061
1062 free(filename);
1063 return v;
1064}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001065
Doug Zongker9931f7f2009-06-10 14:11:53 -07001066void RegisterInstallFunctions() {
1067 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001068 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001069 RegisterFunction("unmount", UnmountFn);
1070 RegisterFunction("format", FormatFn);
1071 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001072 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001073 RegisterFunction("delete", DeleteFn);
1074 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001075 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1076 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001077 RegisterFunction("symlink", SymlinkFn);
1078 RegisterFunction("set_perm", SetPermFn);
1079 RegisterFunction("set_perm_recursive", SetPermFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001080
1081 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001082 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001083 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001084
1085 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001086 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1087 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001088
Doug Zongker512536a2010-02-17 16:11:44 -08001089 RegisterFunction("read_file", ReadFileFn);
1090 RegisterFunction("sha1_check", Sha1CheckFn);
1091
Doug Zongkerd0181b82011-10-19 10:51:12 -07001092 RegisterFunction("wipe_cache", WipeCacheFn);
1093
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001094 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001095
1096 RegisterFunction("run_program", RunProgramFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001097}