blob: 7b9aa80b31a06ae236e6583d7b7e0d0511ab4c5e [file] [log] [blame]
Doug Zongker9931f7f2009-06-10 14:11:53 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
Shashank Mittal815ca5d2010-07-27 11:09:19 -07003 * Copyright (c) 2010, Code Aurora Forum. All rights reserved.
Doug Zongker9931f7f2009-06-10 14:11:53 -07004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Doug Zongkerfbf3c102009-06-24 09:36:20 -070018#include <ctype.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070019#include <errno.h>
20#include <stdarg.h>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070021#include <stdio.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070022#include <stdlib.h>
23#include <string.h>
24#include <sys/mount.h>
25#include <sys/stat.h>
26#include <sys/types.h>
Doug Zongkera3f89ea2009-09-10 14:10:48 -070027#include <sys/wait.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070028#include <unistd.h>
29
Doug Zongker8edb00c2009-06-11 17:21:44 -070030#include "cutils/misc.h"
31#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070032#include "edify/expr.h"
Doug Zongker512536a2010-02-17 16:11:44 -080033#include "mincrypt/sha.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070034#include "minzip/DirUtil.h"
Koushik Dutta1b867542010-11-10 23:52:09 -080035#include "mounts.h"
Steve Kondik4123b582010-11-14 03:18:40 -050036#include "flashutils/flashutils.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070037#include "mtdutils/mtdutils.h"
Shashank Mittal815ca5d2010-07-27 11:09:19 -070038#include "mmcutils/mmcutils.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070039#include "updater.h"
Doug Zongker512536a2010-02-17 16:11:44 -080040#include "applypatch/applypatch.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070041
Doug Zongker9931f7f2009-06-10 14:11:53 -070042// mount(type, location, mount_point)
43//
44// what: type="MTD" location="<partition>" to mount a yaffs2 filesystem
45// type="vfat" location="/dev/block/<whatever>" to mount a device
Doug Zongker512536a2010-02-17 16:11:44 -080046Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070047 char* result = NULL;
48 if (argc != 3) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070049 return ErrorAbort(state, "%s() expects 3 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -070050 }
51 char* type;
52 char* location;
53 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -070054 if (ReadArgs(state, argv, 3, &type, &location, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070055 return NULL;
56 }
57
58 if (strlen(type) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070059 ErrorAbort(state, "type argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070060 goto done;
61 }
62 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070063 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070064 goto done;
65 }
66 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070067 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070068 goto done;
69 }
70
71 mkdir(mount_point, 0755);
72
Koushik Dutta1b867542010-11-10 23:52:09 -080073 if (strcmp(type, "MTD") == 0 || strcmp(type, "MMC") == 0) {
Steve Kondik4123b582010-11-14 03:18:40 -050074 if (0 == mount_partition(location, mount_point, get_default_filesystem(), 0))
Koushik Dutta1b867542010-11-10 23:52:09 -080075 result = mount_point;
76 else
Doug Zongker9931f7f2009-06-10 14:11:53 -070077 result = strdup("");
Doug Zongker9931f7f2009-06-10 14:11:53 -070078 } else {
79 if (mount(location, mount_point, type,
80 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
Doug Zongker60babf82009-09-18 15:11:24 -070081 fprintf(stderr, "%s: failed to mount %s at %s: %s\n",
82 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -070083 result = strdup("");
84 } else {
85 result = mount_point;
86 }
87 }
88
89done:
90 free(type);
91 free(location);
92 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -080093 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -070094}
95
Doug Zongker8edb00c2009-06-11 17:21:44 -070096
97// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -080098Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -070099 char* result = NULL;
100 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700101 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700102 }
103 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700104 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700105 return NULL;
106 }
107 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700108 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700109 goto done;
110 }
111
112 scan_mounted_volumes();
113 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
114 if (vol == NULL) {
115 result = strdup("");
116 } else {
117 result = mount_point;
118 }
119
120done:
121 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800122 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700123}
124
125
Doug Zongker512536a2010-02-17 16:11:44 -0800126Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700127 char* result = NULL;
128 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700129 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700130 }
131 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700132 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700133 return NULL;
134 }
135 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700136 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700137 goto done;
138 }
139
140 scan_mounted_volumes();
141 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
142 if (vol == NULL) {
143 fprintf(stderr, "unmount of %s failed; no such volume\n", mount_point);
144 result = strdup("");
145 } else {
146 unmount_mounted_volume(vol);
147 result = mount_point;
148 }
149
150done:
151 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800152 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700153}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700154
155
Doug Zongker9931f7f2009-06-10 14:11:53 -0700156// format(type, location)
157//
158// type="MTD" location=partition
Doug Zongker512536a2010-02-17 16:11:44 -0800159Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700160 char* result = NULL;
161 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700162 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700163 }
164 char* type;
165 char* location;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700166 if (ReadArgs(state, argv, 2, &type, &location) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700167 return NULL;
168 }
169
170 if (strlen(type) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700171 ErrorAbort(state, "type argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700172 goto done;
173 }
174 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700175 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700176 goto done;
177 }
178
Koushik Dutta1b867542010-11-10 23:52:09 -0800179 if (strcmp(type, "MTD") == 0 || strcmp(type, "MMC") == 0) {
180 if (0 != erase_partition(location, NULL)) {
Shashank Mittal815ca5d2010-07-27 11:09:19 -0700181 result = strdup("");
182 goto done;
183 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700184 } else {
185 fprintf(stderr, "%s: unsupported type \"%s\"", name, type);
186 }
Shashank Mittal815ca5d2010-07-27 11:09:19 -0700187 result = location;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700188done:
189 free(type);
190 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800191 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700192}
193
Doug Zongker8edb00c2009-06-11 17:21:44 -0700194
Doug Zongker512536a2010-02-17 16:11:44 -0800195Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700196 char** paths = malloc(argc * sizeof(char*));
197 int i;
198 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700199 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700200 if (paths[i] == NULL) {
201 int j;
202 for (j = 0; j < i; ++i) {
203 free(paths[j]);
204 }
205 free(paths);
206 return NULL;
207 }
208 }
209
210 bool recursive = (strcmp(name, "delete_recursive") == 0);
211
212 int success = 0;
213 for (i = 0; i < argc; ++i) {
214 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
215 ++success;
216 free(paths[i]);
217 }
218 free(paths);
219
220 char buffer[10];
221 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800222 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700223}
224
Doug Zongker8edb00c2009-06-11 17:21:44 -0700225
Doug Zongker512536a2010-02-17 16:11:44 -0800226Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700227 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700228 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700229 }
230 char* frac_str;
231 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700232 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700233 return NULL;
234 }
235
236 double frac = strtod(frac_str, NULL);
237 int sec = strtol(sec_str, NULL, 10);
238
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700239 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700240 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
241
Doug Zongker9931f7f2009-06-10 14:11:53 -0700242 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800243 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700244}
245
Doug Zongker512536a2010-02-17 16:11:44 -0800246Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700247 if (argc != 1) {
248 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
249 }
250 char* frac_str;
251 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
252 return NULL;
253 }
254
255 double frac = strtod(frac_str, NULL);
256
257 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
258 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
259
Doug Zongker512536a2010-02-17 16:11:44 -0800260 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700261}
262
Doug Zongker8edb00c2009-06-11 17:21:44 -0700263// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800264Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700265 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700266 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700267 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700268 }
269 char* zip_path;
270 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700271 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700272
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700273 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700274
275 // To create a consistent system image, never use the clock for timestamps.
276 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
277
278 bool success = mzExtractRecursive(za, zip_path, dest_path,
279 MZ_EXTRACT_FILES_ONLY, &timestamp,
280 NULL, NULL);
281 free(zip_path);
282 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800283 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700284}
285
Doug Zongker8edb00c2009-06-11 17:21:44 -0700286
287// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800288// or
289// package_extract_file(package_path)
290// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800291// function (the char* returned is actually a FileContents*).
292Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700293 int argc, Expr* argv[]) {
Doug Zongker6aece332010-02-01 14:40:12 -0800294 if (argc != 1 && argc != 2) {
295 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
296 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700297 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700298 bool success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800299 if (argc == 2) {
300 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700301
Doug Zongker6aece332010-02-01 14:40:12 -0800302 char* zip_path;
303 char* dest_path;
304 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
305
306 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
307 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
308 if (entry == NULL) {
309 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
310 goto done2;
311 }
312
313 FILE* f = fopen(dest_path, "wb");
314 if (f == NULL) {
315 fprintf(stderr, "%s: can't open %s for write: %s\n",
316 name, dest_path, strerror(errno));
317 goto done2;
318 }
319 success = mzExtractZipEntryToFile(za, entry, fileno(f));
320 fclose(f);
321
322 done2:
323 free(zip_path);
324 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800325 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800326 } else {
327 // The one-argument version returns the contents of the file
328 // as the result.
329
330 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800331 Value* v = malloc(sizeof(Value));
332 v->type = VAL_BLOB;
333 v->size = -1;
334 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800335
336 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
337
338 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
339 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
340 if (entry == NULL) {
341 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
342 goto done1;
343 }
344
Doug Zongker512536a2010-02-17 16:11:44 -0800345 v->size = mzGetZipEntryUncompLen(entry);
346 v->data = malloc(v->size);
347 if (v->data == NULL) {
Doug Zongker6aece332010-02-01 14:40:12 -0800348 fprintf(stderr, "%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800349 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800350 goto done1;
351 }
352
Doug Zongker512536a2010-02-17 16:11:44 -0800353 success = mzExtractZipEntryToBuffer(za, entry,
354 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800355
356 done1:
357 free(zip_path);
358 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800359 free(v->data);
360 v->data = NULL;
361 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800362 }
Doug Zongker512536a2010-02-17 16:11:44 -0800363 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700364 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700365}
366
367
Doug Zongker9931f7f2009-06-10 14:11:53 -0700368// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700369// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800370Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700371 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700372 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700373 }
374 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700375 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700376 if (target == NULL) return NULL;
377
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700378 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700379 if (srcs == NULL) {
380 free(target);
381 return NULL;
382 }
383
384 int i;
385 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700386 if (unlink(srcs[i]) < 0) {
387 if (errno != ENOENT) {
388 fprintf(stderr, "%s: failed to remove %s: %s\n",
389 name, srcs[i], strerror(errno));
390 }
391 }
392 if (symlink(target, srcs[i]) < 0) {
393 fprintf(stderr, "%s: failed to symlink %s to %s: %s\n",
394 name, srcs[i], target, strerror(errno));
395 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700396 free(srcs[i]);
397 }
398 free(srcs);
Doug Zongker512536a2010-02-17 16:11:44 -0800399 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700400}
401
Doug Zongker8edb00c2009-06-11 17:21:44 -0700402
Doug Zongker512536a2010-02-17 16:11:44 -0800403Value* SetPermFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700404 char* result = NULL;
405 bool recursive = (strcmp(name, "set_perm_recursive") == 0);
406
407 int min_args = 4 + (recursive ? 1 : 0);
408 if (argc < min_args) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700409 return ErrorAbort(state, "%s() expects %d+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700410 }
411
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700412 char** args = ReadVarArgs(state, argc, argv);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700413 if (args == NULL) return NULL;
414
415 char* end;
416 int i;
417
418 int uid = strtoul(args[0], &end, 0);
419 if (*end != '\0' || args[0][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700420 ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700421 goto done;
422 }
423
424 int gid = strtoul(args[1], &end, 0);
425 if (*end != '\0' || args[1][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700426 ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700427 goto done;
428 }
429
430 if (recursive) {
431 int dir_mode = strtoul(args[2], &end, 0);
432 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700433 ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700434 goto done;
435 }
436
437 int file_mode = strtoul(args[3], &end, 0);
438 if (*end != '\0' || args[3][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700439 ErrorAbort(state, "%s: \"%s\" not a valid filemode",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700440 name, args[3]);
441 goto done;
442 }
443
444 for (i = 4; i < argc; ++i) {
445 dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode);
446 }
447 } else {
448 int mode = strtoul(args[2], &end, 0);
449 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700450 ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700451 goto done;
452 }
453
Doug Zongker0bbfe3d2009-06-25 13:37:31 -0700454 for (i = 3; i < argc; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700455 if (chown(args[i], uid, gid) < 0) {
456 fprintf(stderr, "%s: chown of %s to %d %d failed: %s\n",
457 name, args[i], uid, gid, strerror(errno));
458 }
459 if (chmod(args[i], mode) < 0) {
460 fprintf(stderr, "%s: chmod of %s to %o failed: %s\n",
461 name, args[i], mode, strerror(errno));
462 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700463 }
464 }
465 result = strdup("");
466
467done:
468 for (i = 0; i < argc; ++i) {
469 free(args[i]);
470 }
471 free(args);
472
Doug Zongker512536a2010-02-17 16:11:44 -0800473 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700474}
475
Doug Zongker8edb00c2009-06-11 17:21:44 -0700476
Doug Zongker512536a2010-02-17 16:11:44 -0800477Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700478 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700479 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700480 }
481 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700482 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700483 if (key == NULL) return NULL;
484
485 char value[PROPERTY_VALUE_MAX];
486 property_get(key, value, "");
487 free(key);
488
Doug Zongker512536a2010-02-17 16:11:44 -0800489 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700490}
491
492
Doug Zongker47cace92009-06-18 10:11:50 -0700493// file_getprop(file, key)
494//
495// interprets 'file' as a getprop-style file (key=value pairs, one
496// per line, # comment lines and blank lines okay), and returns the value
497// for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800498Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700499 char* result = NULL;
500 char* buffer = NULL;
501 char* filename;
502 char* key;
503 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
504 return NULL;
505 }
506
507 struct stat st;
508 if (stat(filename, &st) < 0) {
509 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
510 name, filename, strerror(errno));
511 goto done;
512 }
513
514#define MAX_FILE_GETPROP_SIZE 65536
515
516 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
517 ErrorAbort(state, "%s too large for %s (max %d)",
518 filename, name, MAX_FILE_GETPROP_SIZE);
519 goto done;
520 }
521
522 buffer = malloc(st.st_size+1);
523 if (buffer == NULL) {
524 ErrorAbort(state, "%s: failed to alloc %d bytes", name, st.st_size+1);
525 goto done;
526 }
527
528 FILE* f = fopen(filename, "rb");
529 if (f == NULL) {
530 ErrorAbort(state, "%s: failed to open %s: %s",
531 name, filename, strerror(errno));
532 goto done;
533 }
534
535 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
536 ErrorAbort(state, "%s: failed to read %d bytes from %s",
537 name, st.st_size+1, filename);
538 fclose(f);
539 goto done;
540 }
541 buffer[st.st_size] = '\0';
542
543 fclose(f);
544
545 char* line = strtok(buffer, "\n");
546 do {
547 // skip whitespace at start of line
548 while (*line && isspace(*line)) ++line;
549
550 // comment or blank line: skip to next line
551 if (*line == '\0' || *line == '#') continue;
552
553 char* equal = strchr(line, '=');
554 if (equal == NULL) {
555 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
556 name, line, filename);
557 goto done;
558 }
559
560 // trim whitespace between key and '='
561 char* key_end = equal-1;
562 while (key_end > line && isspace(*key_end)) --key_end;
563 key_end[1] = '\0';
564
565 // not the key we're looking for
566 if (strcmp(key, line) != 0) continue;
567
568 // skip whitespace after the '=' to the start of the value
569 char* val_start = equal+1;
570 while(*val_start && isspace(*val_start)) ++val_start;
571
572 // trim trailing whitespace
573 char* val_end = val_start + strlen(val_start)-1;
574 while (val_end > val_start && isspace(*val_end)) --val_end;
575 val_end[1] = '\0';
576
577 result = strdup(val_start);
578 break;
579
580 } while ((line = strtok(NULL, "\n")));
581
582 if (result == NULL) result = strdup("");
583
584 done:
585 free(filename);
586 free(key);
587 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -0800588 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -0700589}
590
591
Doug Zongker8edb00c2009-06-11 17:21:44 -0700592static bool write_raw_image_cb(const unsigned char* data,
593 int data_len, void* ctx) {
594 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
595 if (r == data_len) return true;
596 fprintf(stderr, "%s\n", strerror(errno));
597 return false;
598}
599
600// write_raw_image(file, partition)
Doug Zongker512536a2010-02-17 16:11:44 -0800601Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700602 char* result = NULL;
603
604 char* partition;
605 char* filename;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700606 if (ReadArgs(state, argv, 2, &filename, &partition) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700607 return NULL;
608 }
609
610 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700611 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700612 goto done;
613 }
614 if (strlen(filename) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700615 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700616 goto done;
617 }
618
Koushik Dutta1b867542010-11-10 23:52:09 -0800619 if (0 == restore_raw_partition(partition, filename))
620 result = strdup(partition);
621 else
Doug Zongker8edb00c2009-06-11 17:21:44 -0700622 result = strdup("");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700623
624done:
625 if (result != partition) free(partition);
626 free(filename);
Doug Zongker512536a2010-02-17 16:11:44 -0800627 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700628}
629
Doug Zongker8edb00c2009-06-11 17:21:44 -0700630// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -0800631Value* ApplyPatchSpaceFn(const char* name, State* state,
632 int argc, Expr* argv[]) {
633 char* bytes_str;
634 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
635 return NULL;
636 }
637
638 char* endptr;
639 size_t bytes = strtol(bytes_str, &endptr, 10);
640 if (bytes == 0 && endptr == bytes_str) {
641 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
642 name, bytes_str);
643 free(bytes_str);
644 return NULL;
645 }
646
647 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
648}
649
650
651// apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1_1, patch_1, ...)
Doug Zongker512536a2010-02-17 16:11:44 -0800652Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800653 if (argc < 6 || (argc % 2) == 1) {
654 return ErrorAbort(state, "%s(): expected at least 6 args and an "
655 "even number, got %d",
656 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700657 }
658
Doug Zongkerc4351c72010-02-22 14:46:32 -0800659 char* source_filename;
660 char* target_filename;
661 char* target_sha1;
662 char* target_size_str;
663 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
664 &target_sha1, &target_size_str) < 0) {
665 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700666 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700667
Doug Zongkerc4351c72010-02-22 14:46:32 -0800668 char* endptr;
669 size_t target_size = strtol(target_size_str, &endptr, 10);
670 if (target_size == 0 && endptr == target_size_str) {
671 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
672 name, target_size_str);
673 free(source_filename);
674 free(target_filename);
675 free(target_sha1);
676 free(target_size_str);
677 return NULL;
678 }
679
680 int patchcount = (argc-4) / 2;
681 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700682
683 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -0800684 for (i = 0; i < patchcount; ++i) {
685 if (patches[i*2]->type != VAL_STRING) {
686 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
687 break;
688 }
689 if (patches[i*2+1]->type != VAL_BLOB) {
690 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
691 break;
692 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700693 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800694 if (i != patchcount) {
695 for (i = 0; i < patchcount*2; ++i) {
696 FreeValue(patches[i]);
697 }
698 free(patches);
699 return NULL;
700 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700701
Doug Zongkerc4351c72010-02-22 14:46:32 -0800702 char** patch_sha_str = malloc(patchcount * sizeof(char*));
703 for (i = 0; i < patchcount; ++i) {
704 patch_sha_str[i] = patches[i*2]->data;
705 patches[i*2]->data = NULL;
706 FreeValue(patches[i*2]);
707 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700708 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800709
710 int result = applypatch(source_filename, target_filename,
711 target_sha1, target_size,
712 patchcount, patch_sha_str, patches);
713
714 for (i = 0; i < patchcount; ++i) {
715 FreeValue(patches[i]);
716 }
717 free(patch_sha_str);
718 free(patches);
719
720 return StringValue(strdup(result == 0 ? "t" : ""));
721}
722
723// apply_patch_check(file, [sha1_1, ...])
724Value* ApplyPatchCheckFn(const char* name, State* state,
725 int argc, Expr* argv[]) {
726 if (argc < 1) {
727 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
728 name, argc);
729 }
730
731 char* filename;
732 if (ReadArgs(state, argv, 1, &filename) < 0) {
733 return NULL;
734 }
735
736 int patchcount = argc-1;
737 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
738
739 int result = applypatch_check(filename, patchcount, sha1s);
740
741 int i;
742 for (i = 0; i < patchcount; ++i) {
743 free(sha1s[i]);
744 }
745 free(sha1s);
746
747 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700748}
749
Doug Zongker512536a2010-02-17 16:11:44 -0800750Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700751 char** args = ReadVarArgs(state, argc, argv);
752 if (args == NULL) {
753 return NULL;
754 }
755
756 int size = 0;
757 int i;
758 for (i = 0; i < argc; ++i) {
759 size += strlen(args[i]);
760 }
761 char* buffer = malloc(size+1);
762 size = 0;
763 for (i = 0; i < argc; ++i) {
764 strcpy(buffer+size, args[i]);
765 size += strlen(args[i]);
766 free(args[i]);
767 }
768 free(args);
769 buffer[size] = '\0';
770
771 char* line = strtok(buffer, "\n");
772 while (line) {
773 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
774 "ui_print %s\n", line);
775 line = strtok(NULL, "\n");
776 }
777 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
778
Doug Zongker512536a2010-02-17 16:11:44 -0800779 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700780}
781
Doug Zongker512536a2010-02-17 16:11:44 -0800782Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700783 if (argc < 1) {
784 return ErrorAbort(state, "%s() expects at least 1 arg", name);
785 }
786 char** args = ReadVarArgs(state, argc, argv);
787 if (args == NULL) {
788 return NULL;
789 }
790
791 char** args2 = malloc(sizeof(char*) * (argc+1));
792 memcpy(args2, args, sizeof(char*) * argc);
793 args2[argc] = NULL;
794
795 fprintf(stderr, "about to run program [%s] with %d args\n", args2[0], argc);
796
797 pid_t child = fork();
798 if (child == 0) {
799 execv(args2[0], args2);
800 fprintf(stderr, "run_program: execv failed: %s\n", strerror(errno));
801 _exit(1);
802 }
803 int status;
804 waitpid(child, &status, 0);
805 if (WIFEXITED(status)) {
806 if (WEXITSTATUS(status) != 0) {
807 fprintf(stderr, "run_program: child exited with status %d\n",
808 WEXITSTATUS(status));
809 }
810 } else if (WIFSIGNALED(status)) {
811 fprintf(stderr, "run_program: child terminated by signal %d\n",
812 WTERMSIG(status));
813 }
814
815 int i;
816 for (i = 0; i < argc; ++i) {
817 free(args[i]);
818 }
819 free(args);
820 free(args2);
821
822 char buffer[20];
823 sprintf(buffer, "%d", status);
824
Doug Zongker512536a2010-02-17 16:11:44 -0800825 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700826}
827
Doug Zongker512536a2010-02-17 16:11:44 -0800828// Take a sha-1 digest and return it as a newly-allocated hex string.
829static char* PrintSha1(uint8_t* digest) {
830 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
831 int i;
832 const char* alphabet = "0123456789abcdef";
833 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
834 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
835 buffer[i*2+1] = alphabet[digest[i] & 0xf];
836 }
837 buffer[i*2] = '\0';
838 return buffer;
839}
840
841// sha1_check(data)
842// to return the sha1 of the data (given in the format returned by
843// read_file).
844//
845// sha1_check(data, sha1_hex, [sha1_hex, ...])
846// returns the sha1 of the file if it matches any of the hex
847// strings passed, or "" if it does not equal any of them.
848//
849Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
850 if (argc < 1) {
851 return ErrorAbort(state, "%s() expects at least 1 arg", name);
852 }
853
854 Value** args = ReadValueVarArgs(state, argc, argv);
855 if (args == NULL) {
856 return NULL;
857 }
858
859 if (args[0]->size < 0) {
860 fprintf(stderr, "%s(): no file contents received", name);
861 return StringValue(strdup(""));
862 }
863 uint8_t digest[SHA_DIGEST_SIZE];
864 SHA(args[0]->data, args[0]->size, digest);
865 FreeValue(args[0]);
866
867 if (argc == 1) {
868 return StringValue(PrintSha1(digest));
869 }
870
871 int i;
872 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
873 for (i = 1; i < argc; ++i) {
874 if (args[i]->type != VAL_STRING) {
875 fprintf(stderr, "%s(): arg %d is not a string; skipping",
876 name, i);
877 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
878 // Warn about bad args and skip them.
879 fprintf(stderr, "%s(): error parsing \"%s\" as sha-1; skipping",
880 name, args[i]->data);
881 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
882 break;
883 }
884 FreeValue(args[i]);
885 }
886 if (i >= argc) {
887 // Didn't match any of the hex strings; return false.
888 return StringValue(strdup(""));
889 }
890 // Found a match; free all the remaining arguments and return the
891 // matched one.
892 int j;
893 for (j = i+1; j < argc; ++j) {
894 FreeValue(args[j]);
895 }
896 return args[i];
897}
898
899// Read a local file and return its contents (the char* returned
900// is actually a FileContents*).
901Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
902 if (argc != 1) {
903 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
904 }
905 char* filename;
906 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
907
908 Value* v = malloc(sizeof(Value));
909 v->type = VAL_BLOB;
910
911 FileContents fc;
912 if (LoadFileContents(filename, &fc) != 0) {
913 ErrorAbort(state, "%s() loading \"%s\" failed: %s",
914 name, filename, strerror(errno));
915 free(filename);
916 free(v);
917 free(fc.data);
918 return NULL;
919 }
920
921 v->size = fc.size;
922 v->data = (char*)fc.data;
923
924 free(filename);
925 return v;
926}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700927
Doug Zongker9931f7f2009-06-10 14:11:53 -0700928void RegisterInstallFunctions() {
929 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700930 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700931 RegisterFunction("unmount", UnmountFn);
932 RegisterFunction("format", FormatFn);
933 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700934 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700935 RegisterFunction("delete", DeleteFn);
936 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700937 RegisterFunction("package_extract_dir", PackageExtractDirFn);
938 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700939 RegisterFunction("symlink", SymlinkFn);
940 RegisterFunction("set_perm", SetPermFn);
941 RegisterFunction("set_perm_recursive", SetPermFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700942
943 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -0700944 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700945 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700946
947 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800948 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
949 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700950
Doug Zongker512536a2010-02-17 16:11:44 -0800951 RegisterFunction("read_file", ReadFileFn);
952 RegisterFunction("sha1_check", Sha1CheckFn);
953
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700954 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700955
956 RegisterFunction("run_program", RunProgramFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700957}