blob: 40c4461ebb7096e2068fd5f82918b2b3d398aeef [file] [log] [blame]
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -08001/*
2 * Copyright (C) 2007 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
17#undef NDEBUG
18
19#include <assert.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <limits.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <sys/wait.h>
29#include <unistd.h>
30#include <unistd.h>
31
32#include "amend/commands.h"
33#include "commands.h"
34#include "common.h"
35#include "cutils/misc.h"
36#include "cutils/properties.h"
37#include "firmware.h"
38#include "minzip/DirUtil.h"
39#include "minzip/Zip.h"
40#include "roots.h"
41
42#include "extendedcommands.h"
43
44static int gDidShowProgress = 0;
45
46#define UNUSED(p) ((void)(p))
47
48#define CHECK_BOOL() \
49 do { \
50 assert(argv == NULL); \
51 if (argv != NULL) return -1; \
52 assert(argc == true || argc == false); \
53 if (argc != true && argc != false) return -1; \
54 } while (false)
55
56#define CHECK_WORDS() \
57 do { \
58 assert(argc >= 0); \
59 if (argc < 0) return -1; \
60 assert(argc == 0 || argv != NULL); \
61 if (argc != 0 && argv == NULL) return -1; \
62 if (permissions != NULL) { \
63 int CW_I_; \
64 for (CW_I_ = 0; CW_I_ < argc; CW_I_++) { \
65 assert(argv[CW_I_] != NULL); \
66 if (argv[CW_I_] == NULL) return -1; \
67 } \
68 } \
69 } while (false)
70
71#define CHECK_FN() \
72 do { \
73 CHECK_WORDS(); \
74 if (permissions != NULL) { \
75 assert(result == NULL); \
76 if (result != NULL) return -1; \
77 } else { \
78 assert(result != NULL); \
79 if (result == NULL) return -1; \
80 } \
81 } while (false)
82
83#define NO_PERMS(perms) \
84 do { \
85 PermissionRequestList *NP_PRL_ = (perms); \
86 if (NP_PRL_ != NULL) { \
87 int NP_RET_ = addPermissionRequestToList(NP_PRL_, \
88 "", false, PERM_NONE); \
89 if (NP_RET_ < 0) { \
90 /* Returns from the calling function. \
91 */ \
92 return NP_RET_; \
93 } \
94 } \
95 } while (false)
96
97/*
98 * Command definitions
99 */
100
101/* assert <boolexpr>
102 */
103static int
104cmd_assert(const char *name, void *cookie, int argc, const char *argv[],
105 PermissionRequestList *permissions)
106{
107 UNUSED(name);
108 UNUSED(cookie);
109 CHECK_BOOL();
110 NO_PERMS(permissions);
111
112 if (!script_assert_enabled) {
113 return 0;
114 }
115
116 /* If our argument is false, return non-zero (failure)
117 * If our argument is true, return zero (success)
118 */
119 if (argc) {
120 return 0;
121 } else {
122 return 1;
123 }
124}
125
126/* format <root>
127 */
128static int
129cmd_format(const char *name, void *cookie, int argc, const char *argv[],
130 PermissionRequestList *permissions)
131{
132 UNUSED(name);
133 UNUSED(cookie);
134 CHECK_WORDS();
135
136 if (argc != 1) {
137 LOGE("Command %s requires exactly one argument\n", name);
138 return 1;
139 }
140 const char *root = argv[0];
141 ui_print("Formatting %s...\n", root);
142
143 int ret = format_root_device(root);
144 if (ret != 0) {
145 LOGE("Can't format %s\n", root);
146 return 1;
147 }
Koushik Dutta63e04762010-06-15 12:56:17 -0700148#ifdef HAS_DATADATA
149 if (0 == strcmp(root, "DATA:")) {
150 ret = format_root_device("DATADATA:");
151 if (ret != 0) {
152 LOGE("Can't format %s\n", root);
153 return 1;
154 }
155 }
156#endif
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800157 return 0;
158}
159
160/* delete <file1> [<fileN> ...]
161 * delete_recursive <file-or-dir1> [<file-or-dirN> ...]
162 *
163 * Like "rm -f", will try to delete every named file/dir, even if
164 * earlier ones fail. Recursive deletes that fail halfway through
165 * give up early.
166 */
167static int
168cmd_delete(const char *name, void *cookie, int argc, const char *argv[],
169 PermissionRequestList *permissions)
170{
171 UNUSED(cookie);
172 CHECK_WORDS();
173 int nerr = 0;
174 bool recurse;
175
176 if (argc < 1) {
177 LOGE("Command %s requires at least one argument\n", name);
178 return 1;
179 }
180
181 recurse = (strcmp(name, "delete_recursive") == 0);
182 ui_print("Deleting files...\n");
183//xxx permissions
184
185 int i;
186 for (i = 0; i < argc; i++) {
187 const char *root_path = argv[i];
188 char pathbuf[PATH_MAX];
189 const char *path;
190
191 /* This guarantees that all paths use "SYSTEM:"-style roots;
192 * plain paths won't make it through translate_root_path().
193 */
194 path = translate_root_path(root_path, pathbuf, sizeof(pathbuf));
195 if (path != NULL) {
196 int ret = ensure_root_path_mounted(root_path);
197 if (ret < 0) {
198 LOGW("Can't mount volume to delete \"%s\"\n", root_path);
199 nerr++;
200 continue;
201 }
202 if (recurse) {
203 ret = dirUnlinkHierarchy(path);
204 } else {
205 ret = unlink(path);
206 }
207 if (ret != 0 && errno != ENOENT) {
208 LOGW("Can't delete %s\n(%s)\n", path, strerror(errno));
209 nerr++;
210 }
211 } else {
212 nerr++;
213 }
214 }
215//TODO: add a way to fail if a delete didn't work
216
217 return 0;
218}
219
220typedef struct {
221 int num_done;
222 int num_total;
223} ExtractContext;
224
225static void extract_count_cb(const char *fn, void *cookie)
226{
227 ++((ExtractContext*) cookie)->num_total;
228}
229
230static void extract_cb(const char *fn, void *cookie)
231{
232 // minzip writes the filename to the log, so we don't need to
233 ExtractContext *ctx = (ExtractContext*) cookie;
234 ui_set_progress((float) ++ctx->num_done / ctx->num_total);
235}
236
237/* copy_dir <src-dir> <dst-dir> [<timestamp>]
238 *
239 * The contents of <src-dir> will become the contents of <dst-dir>.
240 * The original contents of <dst-dir> are preserved unless something
241 * in <src-dir> overwrote them.
242 *
243 * e.g., for "copy_dir PKG:system SYSTEM:", the file "PKG:system/a"
244 * would be copied to "SYSTEM:a".
245 *
246 * The specified timestamp (in decimal seconds since 1970) will be used,
247 * or a fixed default timestamp will be supplied otherwise.
248 */
249static int
250cmd_copy_dir(const char *name, void *cookie, int argc, const char *argv[],
251 PermissionRequestList *permissions)
252{
253 UNUSED(name);
254 UNUSED(cookie);
255 CHECK_WORDS();
256//xxx permissions
257
258 // To create a consistent system image, never use the clock for timestamps.
259 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
260 if (argc == 3) {
261 char *end;
262 time_t value = strtoul(argv[2], &end, 0);
263 if (value == 0 || end[0] != '\0') {
264 LOGE("Command %s: invalid timestamp \"%s\"\n", name, argv[2]);
265 return 1;
266 } else if (value < timestamp.modtime) {
267 LOGE("Command %s: timestamp \"%s\" too early\n", name, argv[2]);
268 return 1;
269 }
270 timestamp.modtime = timestamp.actime = value;
271 } else if (argc != 2) {
272 LOGE("Command %s requires exactly two arguments\n", name);
273 return 1;
274 }
275
276 // Use 40% of the progress bar (80% post-verification) by default
277 ui_print("Copying files...\n");
278 if (!gDidShowProgress) ui_show_progress(DEFAULT_FILES_PROGRESS_FRACTION, 0);
279
280 /* Mount the destination volume if it isn't already.
281 */
282 const char *dst_root_path = argv[1];
283 int ret = ensure_root_path_mounted(dst_root_path);
284 if (ret < 0) {
285 LOGE("Can't mount %s\n", dst_root_path);
286 return 1;
287 }
288
289 /* Get the real target path.
290 */
291 char dstpathbuf[PATH_MAX];
292 const char *dst_path;
293 dst_path = translate_root_path(dst_root_path,
294 dstpathbuf, sizeof(dstpathbuf));
295 if (dst_path == NULL) {
296 LOGE("Command %s: bad destination path \"%s\"\n", name, dst_root_path);
297 return 1;
298 }
299
300 /* Try to copy the directory. The source may be inside a package.
301 */
302 const char *src_root_path = argv[0];
303 char srcpathbuf[PATH_MAX];
304 const char *src_path;
305 if (is_package_root_path(src_root_path)) {
306 const ZipArchive *package;
307 src_path = translate_package_root_path(src_root_path,
308 srcpathbuf, sizeof(srcpathbuf), &package);
309 if (src_path == NULL) {
310 LOGE("Command %s: bad source path \"%s\"\n", name, src_root_path);
311 return 1;
312 }
313
314 /* Extract the files. Set MZ_EXTRACT_FILES_ONLY, because only files
315 * are validated by the signature. Do a dry run first to count how
316 * many there are (and find some errors early).
317 */
318 ExtractContext ctx;
319 ctx.num_done = 0;
320 ctx.num_total = 0;
321
322 if (!mzExtractRecursive(package, src_path, dst_path,
323 MZ_EXTRACT_FILES_ONLY | MZ_EXTRACT_DRY_RUN,
324 &timestamp, extract_count_cb, (void *) &ctx) ||
325 !mzExtractRecursive(package, src_path, dst_path,
326 MZ_EXTRACT_FILES_ONLY,
327 &timestamp, extract_cb, (void *) &ctx)) {
328 LOGW("Command %s: couldn't extract \"%s\" to \"%s\"\n",
329 name, src_root_path, dst_root_path);
330 return 1;
331 }
332 } else {
333 LOGE("Command %s: non-package source path \"%s\" not yet supported\n",
334 name, src_root_path);
335//xxx mount the src volume
336//xxx
337 return 255;
338 }
339
340 return 0;
341}
342
343/* run_program <program-file> [<args> ...]
344 *
345 * Run an external program included in the update package.
346 */
347static int
348cmd_run_program(const char *name, void *cookie, int argc, const char *argv[],
349 PermissionRequestList *permissions)
350{
351 UNUSED(cookie);
352 CHECK_WORDS();
353
354 if (argc < 1) {
355 LOGE("Command %s requires at least one argument\n", name);
356 return 1;
357 }
358
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800359 static const char *binary = "/tmp/run_program_binary";
Koushik K. Duttaa958ac42010-03-29 23:10:32 -0700360 char path[PATH_MAX];
361 if (!is_package_root_path(argv[0])) {
362 // Copy the program file to temporary storage.
363 binary = argv[0];
364 strcpy(path, binary);
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800365 }
Koushik K. Duttaa958ac42010-03-29 23:10:32 -0700366 else
367 {
368 const ZipArchive *package;
369 if (!translate_package_root_path(argv[0], path, sizeof(path), &package)) {
370 LOGE("Command %s: bad source path \"%s\"\n", name, argv[0]);
371 return 1;
372 }
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800373
Koushik K. Duttaa958ac42010-03-29 23:10:32 -0700374 const ZipEntry *entry = mzFindZipEntry(package, path);
375 if (entry == NULL) {
376 LOGE("Can't find %s\n", path);
377 return 1;
378 }
379
380 unlink(binary); // just to be sure
381 int fd = creat(binary, 0755);
382 if (fd < 0) {
383 LOGE("Can't make %s\n", binary);
384 return 1;
385 }
386 bool ok = mzExtractZipEntryToFile(package, entry, fd);
387 close(fd);
388
389 if (!ok) {
390 LOGE("Can't copy %s\n", path);
391 return 1;
392 }
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800393 }
394
395 // Create a copy of argv to NULL-terminate it, as execv requires
396 char **args = (char **) malloc(sizeof(char*) * (argc + 1));
397 memcpy(args, argv, sizeof(char*) * argc);
398 args[argc] = NULL;
399
400 pid_t pid = fork();
401 if (pid == 0) {
402 execv(binary, args);
403 fprintf(stderr, "E:Can't run %s\n(%s)\n", binary, strerror(errno));
404 _exit(-1);
405 }
406
407 int status;
408 waitpid(pid, &status, 0);
Koushik K. Dutta69b75412010-02-23 18:07:31 -0800409 if (WIFEXITED(status) && WEXITSTATUS(status) == 0 || !script_assert_enabled) {
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800410 return 0;
411 } else {
412 LOGE("Error in %s\n(Status %d)\n", path, status);
413 return 1;
414 }
415}
416
417/* set_perm <uid> <gid> <mode> <path> [... <pathN>]
418 * set_perm_recursive <uid> <gid> <dir-mode> <file-mode> <path> [... <pathN>]
419 *
420 * Like "chmod", "chown" and "chgrp" all in one, set ownership and permissions
421 * of single files or entire directory trees. Any error causes failure.
422 * User, group, and modes must all be integer values (hex or octal OK).
423 */
424static int
425cmd_set_perm(const char *name, void *cookie, int argc, const char *argv[],
426 PermissionRequestList *permissions)
427{
428 UNUSED(cookie);
429 CHECK_WORDS();
430 bool recurse = !strcmp(name, "set_perm_recursive");
431
432 int min_args = 4 + (recurse ? 1 : 0);
433 if (argc < min_args) {
434 LOGE("Command %s requires at least %d args\n", name, min_args);
435 return 1;
436 }
437
438 // All the arguments except the path(s) are numeric.
439 int i, n[min_args - 1];
440 for (i = 0; i < min_args - 1; ++i) {
441 char *end;
442 n[i] = strtoul(argv[i], &end, 0);
443 if (end[0] != '\0' || argv[i][0] == '\0') {
444 LOGE("Command %s: invalid argument \"%s\"\n", name, argv[i]);
445 return 1;
446 }
447 }
448
449 for (i = min_args - 1; i < min_args; ++i) {
450 char path[PATH_MAX];
451 if (translate_root_path(argv[i], path, sizeof(path)) == NULL) {
452 LOGE("Command %s: bad path \"%s\"\n", name, argv[i]);
453 return 1;
454 }
455
456 if (ensure_root_path_mounted(argv[i])) {
457 LOGE("Can't mount %s\n", argv[i]);
458 return 1;
459 }
460
461 if (recurse
462 ? dirSetHierarchyPermissions(path, n[0], n[1], n[2], n[3])
463 : (chown(path, n[0], n[1]) || chmod(path, n[2]))) {
464 LOGE("Can't chown/mod %s\n(%s)\n", path, strerror(errno));
465 return 1;
466 }
467 }
468
469 return 0;
470}
471
472/* show_progress <fraction> <duration>
473 *
474 * Use <fraction> of the on-screen progress meter for the next operation,
475 * automatically advancing the meter over <duration> seconds (or more rapidly
476 * if the actual rate of progress can be determined).
477 */
478static int
479cmd_show_progress(const char *name, void *cookie, int argc, const char *argv[],
480 PermissionRequestList *permissions)
481{
482 UNUSED(cookie);
483 CHECK_WORDS();
484
485 if (argc != 2) {
486 LOGE("Command %s requires exactly two arguments\n", name);
487 return 1;
488 }
489
490 char *end;
491 double fraction = strtod(argv[0], &end);
492 if (end[0] != '\0' || argv[0][0] == '\0' || fraction < 0 || fraction > 1) {
493 LOGE("Command %s: invalid fraction \"%s\"\n", name, argv[0]);
494 return 1;
495 }
496
497 int duration = strtoul(argv[1], &end, 0);
498 if (end[0] != '\0' || argv[0][0] == '\0') {
499 LOGE("Command %s: invalid duration \"%s\"\n", name, argv[1]);
500 return 1;
501 }
502
503 // Half of the progress bar is taken by verification,
504 // so everything that happens during installation is scaled.
505 ui_show_progress(fraction * (1 - VERIFICATION_PROGRESS_FRACTION), duration);
506 gDidShowProgress = 1;
507 return 0;
508}
509
510/* symlink <link-target> <link-path>
511 *
512 * Create a symlink, like "ln -s". The link path must not exist already.
513 * Note that <link-path> is in root:path format, but <link-target> is
514 * for the target filesystem (and may be relative).
515 */
516static int
517cmd_symlink(const char *name, void *cookie, int argc, const char *argv[],
518 PermissionRequestList *permissions)
519{
520 UNUSED(cookie);
521 CHECK_WORDS();
522
523 if (argc != 2) {
524 LOGE("Command %s requires exactly two arguments\n", name);
525 return 1;
526 }
527
528 char path[PATH_MAX];
529 if (translate_root_path(argv[1], path, sizeof(path)) == NULL) {
530 LOGE("Command %s: bad path \"%s\"\n", name, argv[1]);
531 return 1;
532 }
533
534 if (ensure_root_path_mounted(argv[1])) {
535 LOGE("Can't mount %s\n", argv[1]);
536 return 1;
537 }
538
539 if (symlink(argv[0], path)) {
540 LOGE("Can't symlink %s\n", path);
541 return 1;
542 }
543
544 return 0;
545}
546
547struct FirmwareContext {
548 size_t total_bytes, done_bytes;
549 char *data;
550};
551
552static bool firmware_fn(const unsigned char *data, int data_len, void *cookie)
553{
554 struct FirmwareContext *context = (struct FirmwareContext*) cookie;
555 if (context->done_bytes + data_len > context->total_bytes) {
556 LOGE("Data overrun in firmware\n");
557 return false; // Should not happen, but let's be safe.
558 }
559
560 memcpy(context->data + context->done_bytes, data, data_len);
561 context->done_bytes += data_len;
562 ui_set_progress(context->done_bytes * 1.0 / context->total_bytes);
563 return true;
564}
565
566/* write_radio_image <src-image>
567 * write_hboot_image <src-image>
568 * Doesn't actually take effect until the rest of installation finishes.
569 */
570static int
571cmd_write_firmware_image(const char *name, void *cookie,
572 int argc, const char *argv[], PermissionRequestList *permissions)
573{
574 UNUSED(cookie);
575 CHECK_WORDS();
576
577 if (argc != 1) {
578 LOGE("Command %s requires exactly one argument\n", name);
579 return 1;
580 }
581
582 const char *type;
583 if (!strcmp(name, "write_radio_image")) {
584 type = "radio";
585 } else if (!strcmp(name, "write_hboot_image")) {
586 type = "hboot";
587 } else {
588 LOGE("Unknown firmware update command %s\n", name);
589 return 1;
590 }
591
592 if (!is_package_root_path(argv[0])) {
593 LOGE("Command %s: non-package image file \"%s\" not supported\n",
594 name, argv[0]);
595 return 1;
596 }
597
598 ui_print("Extracting %s image...\n", type);
599 char path[PATH_MAX];
600 const ZipArchive *package;
601 if (!translate_package_root_path(argv[0], path, sizeof(path), &package)) {
602 LOGE("Command %s: bad source path \"%s\"\n", name, argv[0]);
603 return 1;
604 }
605
606 const ZipEntry *entry = mzFindZipEntry(package, path);
607 if (entry == NULL) {
608 LOGE("Can't find %s\n", path);
609 return 1;
610 }
611
612 // Load the update image into RAM.
613 struct FirmwareContext context;
614 context.total_bytes = mzGetZipEntryUncompLen(entry);
615 context.done_bytes = 0;
616 context.data = malloc(context.total_bytes);
617 if (context.data == NULL) {
618 LOGE("Can't allocate %d bytes for %s\n", context.total_bytes, argv[0]);
619 return 1;
620 }
621
622 if (!mzProcessZipEntryContents(package, entry, firmware_fn, &context) ||
623 context.done_bytes != context.total_bytes) {
624 LOGE("Can't read %s\n", argv[0]);
625 free(context.data);
626 return 1;
627 }
628
629 if (remember_firmware_update(type, context.data, context.total_bytes)) {
630 LOGE("Can't store %s image\n", type);
631 free(context.data);
632 return 1;
633 }
634
635 return 0;
636}
637
638static bool write_raw_image_process_fn(
639 const unsigned char *data,
640 int data_len, void *ctx)
641{
642 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
643 if (r == data_len) return true;
644 LOGE("%s\n", strerror(errno));
645 return false;
646}
647
648/* write_raw_image <src-image> <dest-root>
649 */
650static int
651cmd_write_raw_image(const char *name, void *cookie,
652 int argc, const char *argv[], PermissionRequestList *permissions)
653{
654 UNUSED(cookie);
655 CHECK_WORDS();
656//xxx permissions
657
658 if (argc != 2) {
659 LOGE("Command %s requires exactly two arguments\n", name);
660 return 1;
661 }
662
663 // Use 10% of the progress bar (20% post-verification) by default
664 const char *src_root_path = argv[0];
665 const char *dst_root_path = argv[1];
666 ui_print("Writing %s...\n", dst_root_path);
667 if (!gDidShowProgress) ui_show_progress(DEFAULT_IMAGE_PROGRESS_FRACTION, 0);
668
669 /* Find the source image, which is probably in a package.
670 */
671 if (!is_package_root_path(src_root_path)) {
672 LOGE("Command %s: non-package source path \"%s\" not yet supported\n",
673 name, src_root_path);
674 return 255;
675 }
676
677 /* Get the package.
678 */
679 char srcpathbuf[PATH_MAX];
680 const char *src_path;
681 const ZipArchive *package;
682 src_path = translate_package_root_path(src_root_path,
683 srcpathbuf, sizeof(srcpathbuf), &package);
684 if (src_path == NULL) {
685 LOGE("Command %s: bad source path \"%s\"\n", name, src_root_path);
686 return 1;
687 }
688
689 /* Get the entry.
690 */
691 const ZipEntry *entry = mzFindZipEntry(package, src_path);
692 if (entry == NULL) {
693 LOGE("Missing file %s\n", src_path);
694 return 1;
695 }
696
697 /* Unmount the destination root if it isn't already.
698 */
699 int ret = ensure_root_path_unmounted(dst_root_path);
700 if (ret < 0) {
701 LOGE("Can't unmount %s\n", dst_root_path);
702 return 1;
703 }
704
705 /* Open the partition for writing.
706 */
707 const MtdPartition *partition = get_root_mtd_partition(dst_root_path);
708 if (partition == NULL) {
709 LOGE("Can't find %s\n", dst_root_path);
710 return 1;
711 }
712 MtdWriteContext *context = mtd_write_partition(partition);
713 if (context == NULL) {
714 LOGE("Can't open %s\n", dst_root_path);
715 return 1;
716 }
717
718 /* Extract and write the image.
719 */
720 bool ok = mzProcessZipEntryContents(package, entry,
721 write_raw_image_process_fn, context);
722 if (!ok) {
723 LOGE("Error writing %s\n", dst_root_path);
724 mtd_write_close(context);
725 return 1;
726 }
727
728 if (mtd_erase_blocks(context, -1) == (off_t) -1) {
729 LOGE("Error finishing %s\n", dst_root_path);
730 mtd_write_close(context);
731 return -1;
732 }
733
734 if (mtd_write_close(context)) {
735 LOGE("Error closing %s\n", dst_root_path);
736 return -1;
737 }
738 return 0;
739}
740
741/* mark <resource> dirty|clean
742 */
743static int
744cmd_mark(const char *name, void *cookie, int argc, const char *argv[],
745 PermissionRequestList *permissions)
746{
747 UNUSED(name);
748 UNUSED(cookie);
749 CHECK_WORDS();
750//xxx when marking, save the top-level hash at the mark point
751// so we can retry on failure. Otherwise the hashes won't match,
752// or someone could intentionally dirty the FS to force a downgrade
753//xxx
754 return -1;
755}
756
757/* done
758 */
759static int
760cmd_done(const char *name, void *cookie, int argc, const char *argv[],
761 PermissionRequestList *permissions)
762{
763 UNUSED(name);
764 UNUSED(cookie);
765 CHECK_WORDS();
766//xxx
767 return -1;
768}
769
770
Koushik K. Duttaa9483082010-03-07 21:47:41 -0800771static int
772cmd_backup_rom(const char *name, void *cookie, int argc, const char *argv[],
773 PermissionRequestList *permissions)
774{
775 UNUSED(cookie);
776 CHECK_WORDS();
777
778 char* backup_name = NULL;
Koushik K. Dutta7e0a72c2010-03-15 00:54:34 -0700779 char backup_path[PATH_MAX];
Koushik K. Duttaa9483082010-03-07 21:47:41 -0800780 switch(argc)
781 {
782 case 0:
Koushik K. Dutta7e0a72c2010-03-15 00:54:34 -0700783 {
Koushik K. Dutta6a26e7c2010-03-27 15:26:11 -0700784 char backup_path[PATH_MAX];
Koushik Dutta08370912010-06-26 12:25:02 -0700785 nandroid_generate_timestamp_path(backup_path);
Koushik K. Dutta7e0a72c2010-03-15 00:54:34 -0700786 backup_name = backup_path;
787 }
Koushik K. Duttaa9483082010-03-07 21:47:41 -0800788 break;
789 case 1:
790 backup_name = argv[0];
791 break;
792 default:
793 LOGE("Command %s requires zero or one argument\n", name);
794 return 1;
795 }
796
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800797 return nandroid_backup(backup_name);
Koushik K. Duttaa9483082010-03-07 21:47:41 -0800798}
799
800static int
801cmd_restore_rom(const char *name, void *cookie, int argc, const char *argv[],
802 PermissionRequestList *permissions)
803{
804 UNUSED(cookie);
805 CHECK_WORDS();
806
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700807 int restoreboot = 1;
808 int restoresystem = 1;
809 int restoredata = 1;
810 int restorecache = 1;
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700811 int restoresdext = 1;
Koushik K. Dutta6923cc32010-03-29 14:46:00 -0700812 int i;
813 for (i = 0; i < argc; i++)
814 {
815 if (strcmp(argv[i], "noboot") == 0)
816 restoreboot = 0;
817 else if (strcmp(argv[i], "nosystem") == 0)
818 restoresystem = 0;
819 else if (strcmp(argv[i], "nodata") == 0)
820 restoredata = 0;
821 else if (strcmp(argv[i], "nocache") == 0)
822 restorecache = 0;
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700823 else if (strcmp(argv[i], "nosd-ext") == 0)
824 restorecache = 0;
Koushik K. Duttaa9483082010-03-07 21:47:41 -0800825 }
826
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700827 return nandroid_restore(argv[0], restoreboot, restoresystem, restoredata, restorecache, restoresdext);
Koushik K. Duttaa9483082010-03-07 21:47:41 -0800828}
829
Koushik K. Dutta60d7ee02010-03-07 22:16:55 -0800830static int
831cmd_sleep(const char *name, void *cookie, int argc, const char *argv[],
832 PermissionRequestList *permissions)
833{
834 UNUSED(cookie);
835 CHECK_WORDS();
836
837 if (argc != 1) {
838 LOGE("Command %s requires exactly one argument\n", name);
839 return 1;
840 }
841
842 int seconds = atoi(argv[0]);
843 sleep(seconds);
844
845 return 0;
846}
847
848static int
849cmd_print(const char *name, void *cookie, int argc, const char *argv[],
850 PermissionRequestList *permissions)
851{
852 UNUSED(cookie);
853 CHECK_WORDS();
854
855 char message[1024];
856 message[0] = NULL;
857 int i;
858 for (i = 0; i < argc; i++)
859 {
860 strcat(message, argv[i]);
861 strcat(message, " ");
862 }
863 strcat(message, "\n");
864
865 ui_print(message);
866 return 0;
867}
Koushik K. Duttaa9483082010-03-07 21:47:41 -0800868
Koushik K. Duttaea46fe22010-03-08 02:58:04 -0800869static int
870cmd_install_zip(const char *name, void *cookie, int argc, const char *argv[],
871 PermissionRequestList *permissions)
872{
873 UNUSED(cookie);
874 CHECK_WORDS();
875
876 if (argc != 1) {
877 LOGE("Command %s requires exactly one argument\n", name);
878 return 1;
879 }
880
881 return install_zip(argv[0]);
882}
883
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -0800884/*
885 * Function definitions
886 */
887
888/* compatible_with(<version>)
889 *
890 * Returns "true" if this version of the script parser and command
891 * set supports the named version.
892 */
893static int
894fn_compatible_with(const char *name, void *cookie, int argc, const char *argv[],
895 char **result, size_t *resultLen,
896 PermissionRequestList *permissions)
897{
898 UNUSED(name);
899 UNUSED(cookie);
900 CHECK_FN();
901 NO_PERMS(permissions);
902
903 if (argc != 1) {
904 fprintf(stderr, "%s: wrong number of arguments (%d)\n",
905 name, argc);
906 return 1;
907 }
908
909 if (!strcmp(argv[0], "0.1") || !strcmp(argv[0], "0.2")) {
910 *result = strdup("true");
911 } else {
912 *result = strdup("");
913 }
914 if (resultLen != NULL) {
915 *resultLen = strlen(*result);
916 }
917 return 0;
918}
919
920/* update_forced()
921 *
922 * Returns "true" if some system setting has determined that
923 * the update should happen no matter what.
924 */
925static int
926fn_update_forced(const char *name, void *cookie, int argc, const char *argv[],
927 char **result, size_t *resultLen,
928 PermissionRequestList *permissions)
929{
930 UNUSED(name);
931 UNUSED(cookie);
932 CHECK_FN();
933 NO_PERMS(permissions);
934
935 if (argc != 0) {
936 fprintf(stderr, "%s: wrong number of arguments (%d)\n",
937 name, argc);
938 return 1;
939 }
940
941 //xxx check some global or property
942 bool force = true;
943 if (force) {
944 *result = strdup("true");
945 } else {
946 *result = strdup("");
947 }
948 if (resultLen != NULL) {
949 *resultLen = strlen(*result);
950 }
951
952 return 0;
953}
954
955/* get_mark(<resource>)
956 *
957 * Returns the current mark associated with the provided resource.
958 */
959static int
960fn_get_mark(const char *name, void *cookie, int argc, const char *argv[],
961 char **result, size_t *resultLen,
962 PermissionRequestList *permissions)
963{
964 UNUSED(name);
965 UNUSED(cookie);
966 CHECK_FN();
967 NO_PERMS(permissions);
968
969 if (argc != 1) {
970 fprintf(stderr, "%s: wrong number of arguments (%d)\n",
971 name, argc);
972 return 1;
973 }
974
975 //xxx look up the value
976 *result = strdup("");
977 if (resultLen != NULL) {
978 *resultLen = strlen(*result);
979 }
980
981 return 0;
982}
983
984/* hash_dir(<path-to-directory>)
985 */
986static int
987fn_hash_dir(const char *name, void *cookie, int argc, const char *argv[],
988 char **result, size_t *resultLen,
989 PermissionRequestList *permissions)
990{
991 int ret = -1;
992
993 UNUSED(name);
994 UNUSED(cookie);
995 CHECK_FN();
996
997 const char *dir;
998 if (argc != 1) {
999 fprintf(stderr, "%s: wrong number of arguments (%d)\n",
1000 name, argc);
1001 return 1;
1002 } else {
1003 dir = argv[0];
1004 }
1005
1006 if (permissions != NULL) {
1007 if (dir == NULL) {
1008 /* The argument is the result of another function.
1009 * Assume the worst case, where the function returns
1010 * the root.
1011 */
1012 dir = "/";
1013 }
1014 ret = addPermissionRequestToList(permissions, dir, true, PERM_READ);
1015 } else {
1016//xxx build and return the string
1017 *result = strdup("hashvalue");
1018 if (resultLen != NULL) {
1019 *resultLen = strlen(*result);
1020 }
1021 ret = 0;
1022 }
1023
1024 return ret;
1025}
1026
1027/* matches(<str>, <str1> [, <strN>...])
1028 * If <str> matches (strcmp) any of <str1>...<strN>, returns <str>,
1029 * otherwise returns "".
1030 *
1031 * E.g., assert matches(hash_dir("/path"), "hash1", "hash2")
1032 */
1033static int
1034fn_matches(const char *name, void *cookie, int argc, const char *argv[],
1035 char **result, size_t *resultLen,
1036 PermissionRequestList *permissions)
1037{
1038 UNUSED(name);
1039 UNUSED(cookie);
1040 CHECK_FN();
1041 NO_PERMS(permissions);
1042
1043 if (argc < 2) {
1044 fprintf(stderr, "%s: not enough arguments (%d < 2)\n",
1045 name, argc);
1046 return 1;
1047 }
1048
1049 int i;
1050 for (i = 1; i < argc; i++) {
1051 if (strcmp(argv[0], argv[i]) == 0) {
1052 *result = strdup(argv[0]);
1053 if (resultLen != NULL) {
1054 *resultLen = strlen(*result);
1055 }
1056 return 0;
1057 }
1058 }
1059
1060 *result = strdup("");
1061 if (resultLen != NULL) {
1062 *resultLen = 1;
1063 }
1064 return 0;
1065}
1066
1067/* concat(<str>, <str1> [, <strN>...])
1068 * Returns the concatenation of all strings.
1069 */
1070static int
1071fn_concat(const char *name, void *cookie, int argc, const char *argv[],
1072 char **result, size_t *resultLen,
1073 PermissionRequestList *permissions)
1074{
1075 UNUSED(name);
1076 UNUSED(cookie);
1077 CHECK_FN();
1078 NO_PERMS(permissions);
1079
1080 size_t totalLen = 0;
1081 int i;
1082 for (i = 0; i < argc; i++) {
1083 totalLen += strlen(argv[i]);
1084 }
1085
1086 char *s = (char *)malloc(totalLen + 1);
1087 if (s == NULL) {
1088 return -1;
1089 }
1090 s[totalLen] = '\0';
1091 for (i = 0; i < argc; i++) {
1092 //TODO: keep track of the end to avoid walking the string each time
1093 strcat(s, argv[i]);
1094 }
1095 *result = s;
1096 if (resultLen != NULL) {
1097 *resultLen = strlen(s);
1098 }
1099
1100 return 0;
1101}
1102
1103/* getprop(<property>)
1104 * Returns the named Android system property value, or "" if not set.
1105 */
1106static int
1107fn_getprop(const char *name, void *cookie, int argc, const char *argv[],
1108 char **result, size_t *resultLen,
1109 PermissionRequestList *permissions)
1110{
1111 UNUSED(cookie);
1112 CHECK_FN();
1113 NO_PERMS(permissions);
1114
1115 if (argc != 1) {
1116 LOGE("Command %s requires exactly one argument\n", name);
1117 return 1;
1118 }
1119
1120 char value[PROPERTY_VALUE_MAX];
1121 property_get(argv[0], value, "");
1122
1123 *result = strdup(value);
1124 if (resultLen != NULL) {
1125 *resultLen = strlen(*result);
1126 }
1127
1128 return 0;
1129}
1130
1131/* file_contains(<filename>, <substring>)
1132 * Returns "true" if the file exists and contains the specified substring.
1133 */
1134static int
1135fn_file_contains(const char *name, void *cookie, int argc, const char *argv[],
1136 char **result, size_t *resultLen,
1137 PermissionRequestList *permissions)
1138{
1139 UNUSED(cookie);
1140 CHECK_FN();
1141 NO_PERMS(permissions);
1142
1143 if (argc != 2) {
1144 LOGE("Command %s requires exactly two arguments\n", name);
1145 return 1;
1146 }
1147
1148 char pathbuf[PATH_MAX];
1149 const char *root_path = argv[0];
1150 const char *path = translate_root_path(root_path, pathbuf, sizeof(pathbuf));
1151 if (path == NULL) {
1152 LOGE("Command %s: bad path \"%s\"\n", name, root_path);
1153 return 1;
1154 }
1155
1156 if (ensure_root_path_mounted(root_path)) {
1157 LOGE("Can't mount %s\n", root_path);
1158 return 1;
1159 }
1160
1161 const char *needle = argv[1];
1162 char *haystack = (char*) load_file(path, NULL);
1163 if (haystack == NULL) {
1164 LOGI("%s: Can't read \"%s\" (%s)\n", name, path, strerror(errno));
1165 *result = ""; /* File not found is not an error. */
1166 } else if (strstr(haystack, needle) == NULL) {
1167 LOGI("%s: Can't find \"%s\" in \"%s\"\n", name, needle, path);
1168 *result = strdup("");
1169 free(haystack);
1170 } else {
1171 *result = strdup("true");
1172 free(haystack);
1173 }
1174
1175 if (resultLen != NULL) {
1176 *resultLen = strlen(*result);
1177 }
1178 return 0;
1179}
1180
1181int
1182register_update_commands(RecoveryCommandContext *ctx)
1183{
1184 int ret;
1185
1186 ret = commandInit();
1187 if (ret < 0) return ret;
1188
1189 /*
1190 * Commands
1191 */
1192
1193 ret = registerCommand("assert", CMD_ARGS_BOOLEAN, cmd_assert, (void *)ctx);
1194 if (ret < 0) return ret;
1195
1196 ret = registerCommand("delete", CMD_ARGS_WORDS, cmd_delete, (void *)ctx);
1197 if (ret < 0) return ret;
1198
1199 ret = registerCommand("delete_recursive", CMD_ARGS_WORDS, cmd_delete,
1200 (void *)ctx);
1201 if (ret < 0) return ret;
1202
1203 ret = registerCommand("copy_dir", CMD_ARGS_WORDS,
1204 cmd_copy_dir, (void *)ctx);
1205 if (ret < 0) return ret;
1206
1207 ret = registerCommand("run_program", CMD_ARGS_WORDS,
1208 cmd_run_program, (void *)ctx);
1209 if (ret < 0) return ret;
1210
1211 ret = registerCommand("set_perm", CMD_ARGS_WORDS,
1212 cmd_set_perm, (void *)ctx);
1213 if (ret < 0) return ret;
1214
1215 ret = registerCommand("set_perm_recursive", CMD_ARGS_WORDS,
1216 cmd_set_perm, (void *)ctx);
1217 if (ret < 0) return ret;
1218
1219 ret = registerCommand("show_progress", CMD_ARGS_WORDS,
1220 cmd_show_progress, (void *)ctx);
1221 if (ret < 0) return ret;
1222
1223 ret = registerCommand("symlink", CMD_ARGS_WORDS, cmd_symlink, (void *)ctx);
1224 if (ret < 0) return ret;
1225
1226 ret = registerCommand("format", CMD_ARGS_WORDS, cmd_format, (void *)ctx);
1227 if (ret < 0) return ret;
1228
1229 ret = registerCommand("write_radio_image", CMD_ARGS_WORDS,
1230 cmd_write_firmware_image, (void *)ctx);
1231 if (ret < 0) return ret;
1232
1233 ret = registerCommand("write_hboot_image", CMD_ARGS_WORDS,
1234 cmd_write_firmware_image, (void *)ctx);
1235 if (ret < 0) return ret;
1236
1237 ret = registerCommand("write_raw_image", CMD_ARGS_WORDS,
1238 cmd_write_raw_image, (void *)ctx);
1239 if (ret < 0) return ret;
1240
1241 ret = registerCommand("mark", CMD_ARGS_WORDS, cmd_mark, (void *)ctx);
1242 if (ret < 0) return ret;
1243
1244 ret = registerCommand("done", CMD_ARGS_WORDS, cmd_done, (void *)ctx);
1245 if (ret < 0) return ret;
1246
Koushik K. Duttaa9483082010-03-07 21:47:41 -08001247 ret = registerCommand("backup_rom", CMD_ARGS_WORDS, cmd_backup_rom, (void *)ctx);
1248 if (ret < 0) return ret;
1249
1250 ret = registerCommand("restore_rom", CMD_ARGS_WORDS, cmd_restore_rom, (void *)ctx);
1251 if (ret < 0) return ret;
1252
Koushik K. Dutta60d7ee02010-03-07 22:16:55 -08001253 ret = registerCommand("sleep", CMD_ARGS_WORDS, cmd_sleep, (void *)ctx);
1254 if (ret < 0) return ret;
1255
1256 ret = registerCommand("print", CMD_ARGS_WORDS, cmd_print, (void *)ctx);
1257 if (ret < 0) return ret;
1258
Koushik K. Duttaea46fe22010-03-08 02:58:04 -08001259 ret = registerCommand("install_zip", CMD_ARGS_WORDS, cmd_install_zip, (void *)ctx);
1260 if (ret < 0) return ret;
1261
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -08001262 /*
1263 * Functions
1264 */
1265
1266 ret = registerFunction("compatible_with", fn_compatible_with, (void *)ctx);
1267 if (ret < 0) return ret;
1268
1269 ret = registerFunction("update_forced", fn_update_forced, (void *)ctx);
1270 if (ret < 0) return ret;
1271
1272 ret = registerFunction("get_mark", fn_get_mark, (void *)ctx);
1273 if (ret < 0) return ret;
1274
1275 ret = registerFunction("hash_dir", fn_hash_dir, (void *)ctx);
1276 if (ret < 0) return ret;
1277
1278 ret = registerFunction("matches", fn_matches, (void *)ctx);
1279 if (ret < 0) return ret;
1280
1281 ret = registerFunction("concat", fn_concat, (void *)ctx);
1282 if (ret < 0) return ret;
1283
1284 ret = registerFunction("getprop", fn_getprop, (void *)ctx);
1285 if (ret < 0) return ret;
1286
1287 ret = registerFunction("file_contains", fn_file_contains, (void *)ctx);
1288 if (ret < 0) return ret;
1289
1290 return 0;
1291}