blob: aa80d75768dbc659c44c334395c2b8d56f7d0fa7 [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>
28
Doug Zongker8edb00c2009-06-11 17:21:44 -070029#include "cutils/misc.h"
30#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070031#include "edify/expr.h"
32#include "minzip/DirUtil.h"
33#include "mtdutils/mounts.h"
34#include "mtdutils/mtdutils.h"
35#include "updater.h"
36
Doug Zongker8edb00c2009-06-11 17:21:44 -070037
Doug Zongker9931f7f2009-06-10 14:11:53 -070038// mount(type, location, mount_point)
39//
40// what: type="MTD" location="<partition>" to mount a yaffs2 filesystem
41// type="vfat" location="/dev/block/<whatever>" to mount a device
Doug Zongkerd9c9d102009-06-12 12:24:39 -070042char* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070043 char* result = NULL;
44 if (argc != 3) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070045 return ErrorAbort(state, "%s() expects 3 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -070046 }
47 char* type;
48 char* location;
49 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -070050 if (ReadArgs(state, argv, 3, &type, &location, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070051 return NULL;
52 }
53
54 if (strlen(type) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070055 ErrorAbort(state, "type argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070056 goto done;
57 }
58 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070059 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070060 goto done;
61 }
62 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070063 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -070064 goto done;
65 }
66
67 mkdir(mount_point, 0755);
68
69 if (strcmp(type, "MTD") == 0) {
70 mtd_scan_partitions();
71 const MtdPartition* mtd;
72 mtd = mtd_find_partition_by_name(location);
73 if (mtd == NULL) {
74 fprintf(stderr, "%s: no mtd partition named \"%s\"",
75 name, location);
76 result = strdup("");
77 goto done;
78 }
79 if (mtd_mount_partition(mtd, mount_point, "yaffs2", 0 /* rw */) != 0) {
80 fprintf(stderr, "mtd mount of %s failed: %s\n",
81 location, strerror(errno));
82 result = strdup("");
83 goto done;
84 }
85 result = mount_point;
86 } else {
87 if (mount(location, mount_point, type,
88 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "") < 0) {
Doug Zongker60babf82009-09-18 15:11:24 -070089 fprintf(stderr, "%s: failed to mount %s at %s: %s\n",
90 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -070091 result = strdup("");
92 } else {
93 result = mount_point;
94 }
95 }
96
97done:
98 free(type);
99 free(location);
100 if (result != mount_point) free(mount_point);
101 return result;
102}
103
Doug Zongker8edb00c2009-06-11 17:21:44 -0700104
105// is_mounted(mount_point)
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700106char* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700107 char* result = NULL;
108 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700109 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700110 }
111 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700112 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700113 return NULL;
114 }
115 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700116 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700117 goto done;
118 }
119
120 scan_mounted_volumes();
121 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
122 if (vol == NULL) {
123 result = strdup("");
124 } else {
125 result = mount_point;
126 }
127
128done:
129 if (result != mount_point) free(mount_point);
130 return result;
131}
132
133
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700134char* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700135 char* result = NULL;
136 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700137 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700138 }
139 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700140 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700141 return NULL;
142 }
143 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700144 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700145 goto done;
146 }
147
148 scan_mounted_volumes();
149 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
150 if (vol == NULL) {
151 fprintf(stderr, "unmount of %s failed; no such volume\n", mount_point);
152 result = strdup("");
153 } else {
154 unmount_mounted_volume(vol);
155 result = mount_point;
156 }
157
158done:
159 if (result != mount_point) free(mount_point);
160 return result;
161}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700162
163
Doug Zongker9931f7f2009-06-10 14:11:53 -0700164// format(type, location)
165//
166// type="MTD" location=partition
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700167char* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700168 char* result = NULL;
169 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700170 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700171 }
172 char* type;
173 char* location;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700174 if (ReadArgs(state, argv, 2, &type, &location) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700175 return NULL;
176 }
177
178 if (strlen(type) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700179 ErrorAbort(state, "type argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700180 goto done;
181 }
182 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700183 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700184 goto done;
185 }
186
187 if (strcmp(type, "MTD") == 0) {
188 mtd_scan_partitions();
189 const MtdPartition* mtd = mtd_find_partition_by_name(location);
190 if (mtd == NULL) {
191 fprintf(stderr, "%s: no mtd partition named \"%s\"",
192 name, location);
193 result = strdup("");
194 goto done;
195 }
196 MtdWriteContext* ctx = mtd_write_partition(mtd);
197 if (ctx == NULL) {
198 fprintf(stderr, "%s: can't write \"%s\"", name, location);
199 result = strdup("");
200 goto done;
201 }
202 if (mtd_erase_blocks(ctx, -1) == -1) {
203 mtd_write_close(ctx);
204 fprintf(stderr, "%s: failed to erase \"%s\"", name, location);
205 result = strdup("");
206 goto done;
207 }
208 if (mtd_write_close(ctx) != 0) {
209 fprintf(stderr, "%s: failed to close \"%s\"", name, location);
210 result = strdup("");
211 goto done;
212 }
213 result = location;
214 } else {
215 fprintf(stderr, "%s: unsupported type \"%s\"", name, type);
216 }
217
218done:
219 free(type);
220 if (result != location) free(location);
221 return result;
222}
223
Doug Zongker8edb00c2009-06-11 17:21:44 -0700224
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700225char* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700226 char** paths = malloc(argc * sizeof(char*));
227 int i;
228 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700229 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700230 if (paths[i] == NULL) {
231 int j;
232 for (j = 0; j < i; ++i) {
233 free(paths[j]);
234 }
235 free(paths);
236 return NULL;
237 }
238 }
239
240 bool recursive = (strcmp(name, "delete_recursive") == 0);
241
242 int success = 0;
243 for (i = 0; i < argc; ++i) {
244 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
245 ++success;
246 free(paths[i]);
247 }
248 free(paths);
249
250 char buffer[10];
251 sprintf(buffer, "%d", success);
252 return strdup(buffer);
253}
254
Doug Zongker8edb00c2009-06-11 17:21:44 -0700255
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700256char* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700257 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700258 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700259 }
260 char* frac_str;
261 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700262 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700263 return NULL;
264 }
265
266 double frac = strtod(frac_str, NULL);
267 int sec = strtol(sec_str, NULL, 10);
268
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700269 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700270 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
271
Doug Zongker9931f7f2009-06-10 14:11:53 -0700272 free(sec_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700273 return frac_str;
274}
275
276char* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
277 if (argc != 1) {
278 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
279 }
280 char* frac_str;
281 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
282 return NULL;
283 }
284
285 double frac = strtod(frac_str, NULL);
286
287 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
288 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
289
290 return frac_str;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700291}
292
Doug Zongker8edb00c2009-06-11 17:21:44 -0700293// package_extract_dir(package_path, destination_path)
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700294char* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700295 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* zip_path;
300 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700301 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700302
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700303 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700304
305 // To create a consistent system image, never use the clock for timestamps.
306 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
307
308 bool success = mzExtractRecursive(za, zip_path, dest_path,
309 MZ_EXTRACT_FILES_ONLY, &timestamp,
310 NULL, NULL);
311 free(zip_path);
312 free(dest_path);
313 return strdup(success ? "t" : "");
314}
315
Doug Zongker8edb00c2009-06-11 17:21:44 -0700316
317// package_extract_file(package_path, destination_path)
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700318char* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700319 int argc, Expr* argv[]) {
320 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700321 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700322 }
323 char* zip_path;
324 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700325 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700326
327 bool success = false;
328
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700329 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700330 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
331 if (entry == NULL) {
332 fprintf(stderr, "%s: no %s in package\n", name, zip_path);
333 goto done;
334 }
335
336 FILE* f = fopen(dest_path, "wb");
337 if (f == NULL) {
338 fprintf(stderr, "%s: can't open %s for write: %s\n",
339 name, dest_path, strerror(errno));
340 goto done;
341 }
342 success = mzExtractZipEntryToFile(za, entry, fileno(f));
343 fclose(f);
344
345 done:
346 free(zip_path);
347 free(dest_path);
348 return strdup(success ? "t" : "");
349}
350
351
Doug Zongker9931f7f2009-06-10 14:11:53 -0700352// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700353// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700354char* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700355 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700356 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700357 }
358 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700359 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700360 if (target == NULL) return NULL;
361
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700362 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700363 if (srcs == NULL) {
364 free(target);
365 return NULL;
366 }
367
368 int i;
369 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700370 if (unlink(srcs[i]) < 0) {
371 if (errno != ENOENT) {
372 fprintf(stderr, "%s: failed to remove %s: %s\n",
373 name, srcs[i], strerror(errno));
374 }
375 }
376 if (symlink(target, srcs[i]) < 0) {
377 fprintf(stderr, "%s: failed to symlink %s to %s: %s\n",
378 name, srcs[i], target, strerror(errno));
379 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700380 free(srcs[i]);
381 }
382 free(srcs);
383 return strdup("");
384}
385
Doug Zongker8edb00c2009-06-11 17:21:44 -0700386
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700387char* SetPermFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700388 char* result = NULL;
389 bool recursive = (strcmp(name, "set_perm_recursive") == 0);
390
391 int min_args = 4 + (recursive ? 1 : 0);
392 if (argc < min_args) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700393 return ErrorAbort(state, "%s() expects %d+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700394 }
395
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700396 char** args = ReadVarArgs(state, argc, argv);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700397 if (args == NULL) return NULL;
398
399 char* end;
400 int i;
401
402 int uid = strtoul(args[0], &end, 0);
403 if (*end != '\0' || args[0][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700404 ErrorAbort(state, "%s: \"%s\" not a valid uid", name, args[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700405 goto done;
406 }
407
408 int gid = strtoul(args[1], &end, 0);
409 if (*end != '\0' || args[1][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700410 ErrorAbort(state, "%s: \"%s\" not a valid gid", name, args[1]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700411 goto done;
412 }
413
414 if (recursive) {
415 int dir_mode = strtoul(args[2], &end, 0);
416 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700417 ErrorAbort(state, "%s: \"%s\" not a valid dirmode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700418 goto done;
419 }
420
421 int file_mode = strtoul(args[3], &end, 0);
422 if (*end != '\0' || args[3][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700423 ErrorAbort(state, "%s: \"%s\" not a valid filemode",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700424 name, args[3]);
425 goto done;
426 }
427
428 for (i = 4; i < argc; ++i) {
429 dirSetHierarchyPermissions(args[i], uid, gid, dir_mode, file_mode);
430 }
431 } else {
432 int mode = strtoul(args[2], &end, 0);
433 if (*end != '\0' || args[2][0] == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700434 ErrorAbort(state, "%s: \"%s\" not a valid mode", name, args[2]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700435 goto done;
436 }
437
Doug Zongker0bbfe3d2009-06-25 13:37:31 -0700438 for (i = 3; i < argc; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700439 if (chown(args[i], uid, gid) < 0) {
440 fprintf(stderr, "%s: chown of %s to %d %d failed: %s\n",
441 name, args[i], uid, gid, strerror(errno));
442 }
443 if (chmod(args[i], mode) < 0) {
444 fprintf(stderr, "%s: chmod of %s to %o failed: %s\n",
445 name, args[i], mode, strerror(errno));
446 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700447 }
448 }
449 result = strdup("");
450
451done:
452 for (i = 0; i < argc; ++i) {
453 free(args[i]);
454 }
455 free(args);
456
457 return result;
458}
459
Doug Zongker8edb00c2009-06-11 17:21:44 -0700460
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700461char* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700462 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700463 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700464 }
465 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700466 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700467 if (key == NULL) return NULL;
468
469 char value[PROPERTY_VALUE_MAX];
470 property_get(key, value, "");
471 free(key);
472
473 return strdup(value);
474}
475
476
Doug Zongker47cace92009-06-18 10:11:50 -0700477// file_getprop(file, key)
478//
479// interprets 'file' as a getprop-style file (key=value pairs, one
480// per line, # comment lines and blank lines okay), and returns the value
481// for 'key' (or "" if it isn't defined).
482char* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
483 char* result = NULL;
484 char* buffer = NULL;
485 char* filename;
486 char* key;
487 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
488 return NULL;
489 }
490
491 struct stat st;
492 if (stat(filename, &st) < 0) {
493 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
494 name, filename, strerror(errno));
495 goto done;
496 }
497
498#define MAX_FILE_GETPROP_SIZE 65536
499
500 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
501 ErrorAbort(state, "%s too large for %s (max %d)",
502 filename, name, MAX_FILE_GETPROP_SIZE);
503 goto done;
504 }
505
506 buffer = malloc(st.st_size+1);
507 if (buffer == NULL) {
508 ErrorAbort(state, "%s: failed to alloc %d bytes", name, st.st_size+1);
509 goto done;
510 }
511
512 FILE* f = fopen(filename, "rb");
513 if (f == NULL) {
514 ErrorAbort(state, "%s: failed to open %s: %s",
515 name, filename, strerror(errno));
516 goto done;
517 }
518
519 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
520 ErrorAbort(state, "%s: failed to read %d bytes from %s",
521 name, st.st_size+1, filename);
522 fclose(f);
523 goto done;
524 }
525 buffer[st.st_size] = '\0';
526
527 fclose(f);
528
529 char* line = strtok(buffer, "\n");
530 do {
531 // skip whitespace at start of line
532 while (*line && isspace(*line)) ++line;
533
534 // comment or blank line: skip to next line
535 if (*line == '\0' || *line == '#') continue;
536
537 char* equal = strchr(line, '=');
538 if (equal == NULL) {
539 ErrorAbort(state, "%s: malformed line \"%s\": %s not a prop file?",
540 name, line, filename);
541 goto done;
542 }
543
544 // trim whitespace between key and '='
545 char* key_end = equal-1;
546 while (key_end > line && isspace(*key_end)) --key_end;
547 key_end[1] = '\0';
548
549 // not the key we're looking for
550 if (strcmp(key, line) != 0) continue;
551
552 // skip whitespace after the '=' to the start of the value
553 char* val_start = equal+1;
554 while(*val_start && isspace(*val_start)) ++val_start;
555
556 // trim trailing whitespace
557 char* val_end = val_start + strlen(val_start)-1;
558 while (val_end > val_start && isspace(*val_end)) --val_end;
559 val_end[1] = '\0';
560
561 result = strdup(val_start);
562 break;
563
564 } while ((line = strtok(NULL, "\n")));
565
566 if (result == NULL) result = strdup("");
567
568 done:
569 free(filename);
570 free(key);
571 free(buffer);
572 return result;
573}
574
575
Doug Zongker8edb00c2009-06-11 17:21:44 -0700576static bool write_raw_image_cb(const unsigned char* data,
577 int data_len, void* ctx) {
578 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
579 if (r == data_len) return true;
580 fprintf(stderr, "%s\n", strerror(errno));
581 return false;
582}
583
584// write_raw_image(file, partition)
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700585char* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700586 char* result = NULL;
587
588 char* partition;
589 char* filename;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700590 if (ReadArgs(state, argv, 2, &filename, &partition) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700591 return NULL;
592 }
593
594 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700595 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700596 goto done;
597 }
598 if (strlen(filename) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700599 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700600 goto done;
601 }
602
603 mtd_scan_partitions();
604 const MtdPartition* mtd = mtd_find_partition_by_name(partition);
605 if (mtd == NULL) {
606 fprintf(stderr, "%s: no mtd partition named \"%s\"\n", name, partition);
607 result = strdup("");
608 goto done;
609 }
610
611 MtdWriteContext* ctx = mtd_write_partition(mtd);
612 if (ctx == NULL) {
613 fprintf(stderr, "%s: can't write mtd partition \"%s\"\n",
614 name, partition);
615 result = strdup("");
616 goto done;
617 }
618
619 bool success;
620
621 FILE* f = fopen(filename, "rb");
622 if (f == NULL) {
623 fprintf(stderr, "%s: can't open %s: %s\n",
624 name, filename, strerror(errno));
625 result = strdup("");
626 goto done;
627 }
628
629 success = true;
630 char* buffer = malloc(BUFSIZ);
631 int read;
632 while (success && (read = fread(buffer, 1, BUFSIZ, f)) > 0) {
633 int wrote = mtd_write_data(ctx, buffer, read);
634 success = success && (wrote == read);
635 if (!success) {
636 fprintf(stderr, "mtd_write_data to %s failed: %s\n",
637 partition, strerror(errno));
638 }
639 }
640 free(buffer);
641 fclose(f);
642
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700643 if (mtd_erase_blocks(ctx, -1) == -1) {
644 fprintf(stderr, "%s: error erasing blocks of %s\n", name, partition);
645 }
646 if (mtd_write_close(ctx) != 0) {
647 fprintf(stderr, "%s: error closing write of %s\n", name, partition);
648 }
649
Doug Zongker8edb00c2009-06-11 17:21:44 -0700650 printf("%s %s partition from %s\n",
651 success ? "wrote" : "failed to write", partition, filename);
652
653 result = success ? partition : strdup("");
654
655done:
656 if (result != partition) free(partition);
657 free(filename);
658 return result;
659}
660
661// write_firmware_image(file, partition)
662//
663// partition is "radio" or "hboot"
664// file is not used until after updater exits
665//
666// TODO: this should live in some HTC-specific library
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700667char* WriteFirmwareImageFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700668 int argc, Expr* argv[]) {
669 char* result = NULL;
670
671 char* partition;
672 char* filename;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700673 if (ReadArgs(state, argv, 2, &filename, &partition) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700674 return NULL;
675 }
676
677 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700678 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700679 goto done;
680 }
681 if (strlen(filename) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700682 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700683 goto done;
684 }
685
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700686 FILE* cmd = ((UpdaterInfo*)(state->cookie))->cmd_pipe;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700687 fprintf(cmd, "firmware %s %s\n", partition, filename);
688
689 printf("will write %s firmware from %s\n", partition, filename);
690 result = partition;
691
692done:
693 if (result != partition) free(partition);
694 free(filename);
695 return result;
696}
697
698
699extern int applypatch(int argc, char** argv);
700
701// apply_patch(srcfile, tgtfile, tgtsha1, tgtsize, sha1:patch, ...)
702// apply_patch_check(file, sha1, ...)
703// apply_patch_space(bytes)
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700704char* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700705 printf("in applypatchfn (%s)\n", name);
706
707 char* prepend = NULL;
708 if (strstr(name, "check") != NULL) {
709 prepend = "-c";
710 } else if (strstr(name, "space") != NULL) {
711 prepend = "-s";
712 }
713
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700714 char** args = ReadVarArgs(state, argc, argv);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700715 if (args == NULL) return NULL;
716
717 // insert the "program name" argv[0] and a copy of the "prepend"
718 // string (if any) at the start of the args.
719
720 int extra = 1 + (prepend != NULL ? 1 : 0);
721 char** temp = malloc((argc+extra) * sizeof(char*));
722 memcpy(temp+extra, args, argc * sizeof(char*));
723 temp[0] = strdup("updater");
724 if (prepend) {
725 temp[1] = strdup(prepend);
726 }
727 free(args);
728 args = temp;
729 argc += extra;
730
731 printf("calling applypatch\n");
732 fflush(stdout);
733 int result = applypatch(argc, args);
734 printf("applypatch returned %d\n", result);
735
736 int i;
737 for (i = 0; i < argc; ++i) {
738 free(args[i]);
739 }
740 free(args);
741
742 switch (result) {
743 case 0: return strdup("t");
744 case 1: return strdup("");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700745 default: return ErrorAbort(state, "applypatch couldn't parse args");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700746 }
747}
748
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700749char* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
750 char** args = ReadVarArgs(state, argc, argv);
751 if (args == NULL) {
752 return NULL;
753 }
754
755 int size = 0;
756 int i;
757 for (i = 0; i < argc; ++i) {
758 size += strlen(args[i]);
759 }
760 char* buffer = malloc(size+1);
761 size = 0;
762 for (i = 0; i < argc; ++i) {
763 strcpy(buffer+size, args[i]);
764 size += strlen(args[i]);
765 free(args[i]);
766 }
767 free(args);
768 buffer[size] = '\0';
769
770 char* line = strtok(buffer, "\n");
771 while (line) {
772 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe,
773 "ui_print %s\n", line);
774 line = strtok(NULL, "\n");
775 }
776 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "ui_print\n");
777
778 return buffer;
779}
780
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700781char* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
782 if (argc < 1) {
783 return ErrorAbort(state, "%s() expects at least 1 arg", name);
784 }
785 char** args = ReadVarArgs(state, argc, argv);
786 if (args == NULL) {
787 return NULL;
788 }
789
790 char** args2 = malloc(sizeof(char*) * (argc+1));
791 memcpy(args2, args, sizeof(char*) * argc);
792 args2[argc] = NULL;
793
794 fprintf(stderr, "about to run program [%s] with %d args\n", args2[0], argc);
795
796 pid_t child = fork();
797 if (child == 0) {
798 execv(args2[0], args2);
799 fprintf(stderr, "run_program: execv failed: %s\n", strerror(errno));
800 _exit(1);
801 }
802 int status;
803 waitpid(child, &status, 0);
804 if (WIFEXITED(status)) {
805 if (WEXITSTATUS(status) != 0) {
806 fprintf(stderr, "run_program: child exited with status %d\n",
807 WEXITSTATUS(status));
808 }
809 } else if (WIFSIGNALED(status)) {
810 fprintf(stderr, "run_program: child terminated by signal %d\n",
811 WTERMSIG(status));
812 }
813
814 int i;
815 for (i = 0; i < argc; ++i) {
816 free(args[i]);
817 }
818 free(args);
819 free(args2);
820
821 char buffer[20];
822 sprintf(buffer, "%d", status);
823
824 return strdup(buffer);
825}
826
Doug Zongker8edb00c2009-06-11 17:21:44 -0700827
Doug Zongker9931f7f2009-06-10 14:11:53 -0700828void RegisterInstallFunctions() {
829 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700830 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700831 RegisterFunction("unmount", UnmountFn);
832 RegisterFunction("format", FormatFn);
833 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700834 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700835 RegisterFunction("delete", DeleteFn);
836 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700837 RegisterFunction("package_extract_dir", PackageExtractDirFn);
838 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700839 RegisterFunction("symlink", SymlinkFn);
840 RegisterFunction("set_perm", SetPermFn);
841 RegisterFunction("set_perm_recursive", SetPermFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700842
843 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -0700844 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700845 RegisterFunction("write_raw_image", WriteRawImageFn);
846 RegisterFunction("write_firmware_image", WriteFirmwareImageFn);
847
848 RegisterFunction("apply_patch", ApplyPatchFn);
849 RegisterFunction("apply_patch_check", ApplyPatchFn);
850 RegisterFunction("apply_patch_space", ApplyPatchFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700851
852 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -0700853
854 RegisterFunction("run_program", RunProgramFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700855}