blob: 25f6a910603a9a4ba369142048f248962b3d5113 [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
Tao Bao0c7839a2016-10-10 15:48:37 -070017#include "updater/install.h"
18
Doug Zongkerfbf3c102009-06-24 09:36:20 -070019#include <ctype.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070020#include <errno.h>
Tao Bao361342c2016-02-08 11:15:50 -080021#include <fcntl.h>
22#include <ftw.h>
23#include <inttypes.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070024#include <stdarg.h>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070025#include <stdio.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070026#include <stdlib.h>
27#include <string.h>
Tao Bao361342c2016-02-08 11:15:50 -080028#include <sys/capability.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070029#include <sys/mount.h>
30#include <sys/stat.h>
31#include <sys/types.h>
Doug Zongkera3f89ea2009-09-10 14:10:48 -070032#include <sys/wait.h>
Nick Kralevich5dbdef02013-09-07 14:41:06 -070033#include <sys/xattr.h>
Tao Bao361342c2016-02-08 11:15:50 -080034#include <time.h>
35#include <unistd.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070036#include <utime.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070037
Yabin Cui64be2132016-02-03 18:16:02 -080038#include <memory>
Tao Bao361342c2016-02-08 11:15:50 -080039#include <string>
Yabin Cui64be2132016-02-03 18:16:02 -080040#include <vector>
41
Elliott Hughes4b166f02015-12-04 15:30:20 -080042#include <android-base/parseint.h>
Tianjie Xu5fe280a2016-10-17 18:15:20 -070043#include <android-base/parsedouble.h>
Elliott Hughescb220402016-09-23 15:30:55 -070044#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080045#include <android-base/strings.h>
46#include <android-base/stringprintf.h>
Tao Bao0c7839a2016-10-10 15:48:37 -070047#include <cutils/android_reboot.h>
Tao Baode40ba52016-10-05 23:17:01 -070048#include <ext4_utils/make_ext4fs.h>
49#include <ext4_utils/wipe.h>
Tao Bao361342c2016-02-08 11:15:50 -080050#include <openssl/sha.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070051#include <selinux/label.h>
52#include <selinux/selinux.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070053#include <ziparchive/zip_archive.h>
Tao Bao1107d962015-09-09 17:16:55 -070054
Doug Zongkerc87bab12013-11-25 13:53:25 -080055#include "applypatch/applypatch.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070056#include "bootloader.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070057#include "edify/expr.h"
Tianjie Xu16255832016-04-30 11:49:59 -070058#include "error_code.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070059#include "mounts.h"
Jed Estep39c1b5e2015-12-15 16:04:53 -080060#include "ota_io.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070061#include "otautil/DirUtil.h"
62#include "otautil/ZipUtil.h"
Tao Bao361342c2016-02-08 11:15:50 -080063#include "print_sha1.h"
Michael Rungeacf47db2014-11-21 00:12:28 -080064#include "tune2fs.h"
Tao Bao0c7839a2016-10-10 15:48:37 -070065#include "updater/updater.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070066
Tao Bao1107d962015-09-09 17:16:55 -070067// Send over the buffer to recovery though the command pipe.
68static void uiPrint(State* state, const std::string& buffer) {
Tao Bao0831d0b2016-11-03 23:25:04 -070069 UpdaterInfo* ui = static_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070070
Tao Bao1107d962015-09-09 17:16:55 -070071 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
72 // So skip sending empty strings to UI.
73 std::vector<std::string> lines = android::base::Split(buffer, "\n");
74 for (auto& line: lines) {
75 if (!line.empty()) {
76 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
77 fprintf(ui->cmd_pipe, "ui_print\n");
78 }
79 }
80
81 // On the updater side, we need to dump the contents to stderr (which has
82 // been redirected to the log file). Because the recovery will only print
83 // the contents to screen when processing pipe command ui_print.
84 fprintf(stderr, "%s", buffer.c_str());
Michael Runged4a63422014-10-22 19:48:41 -070085}
86
Elliott Hughes83ce7552016-06-30 09:28:42 -070087void uiPrintf(State* _Nonnull state, const char* _Nonnull format, ...) {
Tao Bao1107d962015-09-09 17:16:55 -070088 std::string error_msg;
89
Michael Runged4a63422014-10-22 19:48:41 -070090 va_list ap;
91 va_start(ap, format);
Tao Bao1107d962015-09-09 17:16:55 -070092 android::base::StringAppendV(&error_msg, format, ap);
Michael Runged4a63422014-10-22 19:48:41 -070093 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070094
Michael Runged4a63422014-10-22 19:48:41 -070095 uiPrint(state, error_msg);
96}
97
Tao Bao0c7839a2016-10-10 15:48:37 -070098// Create all parent directories of name, if necessary.
99static int make_parents(char* name) {
100 char* p;
101 for (p = name + (strlen(name)-1); p > name; --p) {
102 if (*p != '/') continue;
103 *p = '\0';
104 if (make_parents(name) < 0) return -1;
105 int result = mkdir(name, 0700);
106 if (result == 0) printf("created [%s]\n", name);
107 *p = '/';
108 if (result == 0 || errno == EEXIST) {
109 // successfully created or already existed; we're done
110 return 0;
111 } else {
112 printf("failed to mkdir %s: %s\n", name, strerror(errno));
113 return -1;
114 }
115 }
116 return 0;
117}
118
Doug Zongker3d177d02010-07-01 09:18:44 -0700119// mount(fs_type, partition_type, location, mount_point)
Tao Bao0831d0b2016-11-03 23:25:04 -0700120// mount(fs_type, partition_type, location, mount_point, mount_options)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700121//
Doug Zongker3d177d02010-07-01 09:18:44 -0700122// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800123Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Michael Runge168f7772014-10-22 17:05:08 -0700124 if (argc != 4 && argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700125 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700126 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700127
128 std::vector<std::string> args;
129 if (!ReadArgs(state, argc, argv, &args)) {
130 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
131 }
132 const std::string& fs_type = args[0];
133 const std::string& partition_type = args[1];
134 const std::string& location = args[2];
135 const std::string& mount_point = args[3];
136 std::string mount_options;
137
Michael Runge168f7772014-10-22 17:05:08 -0700138 if (argc == 5) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700139 mount_options = args[4];
Doug Zongker9931f7f2009-06-10 14:11:53 -0700140 }
141
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700142 if (fs_type.empty()) {
143 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty",
144 name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700145 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700146 if (partition_type.empty()) {
147 return ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
148 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700149 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700150 if (location.empty()) {
151 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty",
152 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700153 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700154 if (mount_point.empty()) {
155 return ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
156 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700157 }
158
Tao Baoba9a42a2015-06-23 23:23:33 -0700159 {
160 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500161
Tao Baoba9a42a2015-06-23 23:23:33 -0700162 if (sehandle) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700163 selabel_lookup(sehandle, &secontext, mount_point.c_str(), 0755);
Tao Baoba9a42a2015-06-23 23:23:33 -0700164 setfscreatecon(secontext);
165 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500166
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700167 mkdir(mount_point.c_str(), 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700168
Tao Baoba9a42a2015-06-23 23:23:33 -0700169 if (secontext) {
170 freecon(secontext);
171 setfscreatecon(NULL);
172 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500173 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500174
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700175 if (mount(location.c_str(), mount_point.c_str(), fs_type.c_str(),
176 MS_NOATIME | MS_NODEV | MS_NODIRATIME, mount_options.c_str()) < 0) {
Elliott Hughes63a31922016-06-09 17:41:22 -0700177 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700178 name, location.c_str(), mount_point.c_str(), strerror(errno));
179 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700180 }
181
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700182 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700183}
184
Doug Zongker8edb00c2009-06-11 17:21:44 -0700185
186// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800187Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700188 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700189 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700190 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700191
192 std::vector<std::string> args;
193 if (!ReadArgs(state, argc, argv, &args)) {
194 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700195 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700196 const std::string& mount_point = args[0];
197 if (mount_point.empty()) {
198 return ErrorAbort(state, kArgsParsingFailure,
199 "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700200 }
201
202 scan_mounted_volumes();
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700203 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
204 if (vol == nullptr) {
205 return StringValue("");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700206 }
207
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700208 return StringValue(mount_point);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700209}
210
211
Doug Zongker512536a2010-02-17 16:11:44 -0800212Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700213 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700214 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700215 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700216 std::vector<std::string> args;
217 if (!ReadArgs(state, argc, argv, &args)) {
218 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700219 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700220 const std::string& mount_point = args[0];
221 if (mount_point.empty()) {
222 return ErrorAbort(state, kArgsParsingFailure,
223 "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700224 }
225
226 scan_mounted_volumes();
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700227 MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point.c_str());
228 if (vol == nullptr) {
229 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point.c_str());
230 return nullptr;
231 } else {
232 int ret = unmount_mounted_volume(vol);
233 if (ret != 0) {
234 uiPrintf(state, "unmount of %s failed (%d): %s\n",
235 mount_point.c_str(), ret, strerror(errno));
Michael Runge5ddf4292014-10-24 14:14:41 -0700236 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700237 }
238
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700239 return StringValue(mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700240}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700241
JP Abgrall37aedb32014-06-16 19:07:39 -0700242static int exec_cmd(const char* path, char* const argv[]) {
243 int status;
244 pid_t child;
245 if ((child = vfork()) == 0) {
246 execv(path, argv);
247 _exit(-1);
248 }
249 waitpid(child, &status, 0);
250 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
251 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
252 }
253 return WEXITSTATUS(status);
254}
255
Stephen Smalley779701d2012-02-09 14:13:23 -0500256// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700257//
Stephen Smalley779701d2012-02-09 14:13:23 -0500258// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700259// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
260// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800261// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700262// if fs_size < 0, then reserve that many bytes at the end of the partition (not for "f2fs")
Doug Zongker512536a2010-02-17 16:11:44 -0800263Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Stephen Smalley516e4e22012-04-03 13:35:11 -0400264 if (argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700265 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700266 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500267
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700268 std::vector<std::string> args;
269 if (!ReadArgs(state, argc, argv, &args)) {
270 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
271 }
272 const std::string& fs_type = args[0];
273 const std::string& partition_type = args[1];
274 const std::string& location = args[2];
275 const std::string& fs_size = args[3];
276 const std::string& mount_point = args[4];
277
278 if (fs_type.empty()) {
279 return ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty",
280 name);
281 }
282 if (partition_type.empty()) {
283 return ErrorAbort(state, kArgsParsingFailure,
284 "partition_type argument to %s() can't be empty", name);
285 }
286 if (location.empty()) {
287 return ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty",
288 name);
289 }
290 if (mount_point.empty()) {
291 return ErrorAbort(state, kArgsParsingFailure,
292 "mount_point argument to %s() can't be empty", name);
Stephen Smalley779701d2012-02-09 14:13:23 -0500293 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700294
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700295 int64_t size;
296 if (!android::base::ParseInt(fs_size.c_str(), &size)) {
297 return ErrorAbort(state, kArgsParsingFailure,
298 "%s: failed to parse int in %s\n", name, fs_size.c_str());
Doug Zongker9931f7f2009-06-10 14:11:53 -0700299 }
300
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700301 if (fs_type == "ext4") {
302 int status = make_ext4fs(location.c_str(), size, mount_point.c_str(), sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700303 if (status != 0) {
Tao Bao0831d0b2016-11-03 23:25:04 -0700304 printf("%s: make_ext4fs failed (%d) on %s", name, status, location.c_str());
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700305 return StringValue("");
Doug Zongker3d177d02010-07-01 09:18:44 -0700306 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700307 return StringValue(location);
308 } else if (fs_type == "f2fs") {
309 if (size < 0) {
Tao Bao0831d0b2016-11-03 23:25:04 -0700310 printf("%s: fs_size can't be negative for f2fs: %s", name, fs_size.c_str());
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700311 return StringValue("");
JP Abgrall37aedb32014-06-16 19:07:39 -0700312 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700313 std::string num_sectors = std::to_string(size / 512);
314
JP Abgrall37aedb32014-06-16 19:07:39 -0700315 const char *f2fs_path = "/sbin/mkfs.f2fs";
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700316 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location.c_str(),
317 num_sectors.c_str(), nullptr};
JP Abgrall37aedb32014-06-16 19:07:39 -0700318 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
JP Abgrall37aedb32014-06-16 19:07:39 -0700319 if (status != 0) {
Tao Bao0831d0b2016-11-03 23:25:04 -0700320 printf("%s: mkfs.f2fs failed (%d) on %s", name, status, location.c_str());
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700321 return StringValue("");
JP Abgrall37aedb32014-06-16 19:07:39 -0700322 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700323 return StringValue(location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700324 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700325 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700326 name, fs_type.c_str(), partition_type.c_str());
Doug Zongker9931f7f2009-06-10 14:11:53 -0700327 }
328
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700329 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700330}
331
Michael Rungece7ca712013-11-06 17:42:20 -0800332Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
Michael Rungece7ca712013-11-06 17:42:20 -0800333 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700334 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Michael Rungece7ca712013-11-06 17:42:20 -0800335 }
336
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700337 std::vector<std::string> args;
338 if (!ReadArgs(state, argc, argv, &args)) {
339 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
340 }
341 const std::string& src_name = args[0];
342 std::string& dst_name = args[1];
Michael Rungece7ca712013-11-06 17:42:20 -0800343
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700344 if (src_name.empty()) {
345 return ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty",
346 name);
Michael Rungece7ca712013-11-06 17:42:20 -0800347 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700348 if (dst_name.empty()) {
349 return ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty",
350 name);
Michael Rungece7ca712013-11-06 17:42:20 -0800351 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700352 if (make_parents(&dst_name[0]) != 0) {
353 return ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
354 dst_name.c_str(), strerror(errno));
355 } else if (access(dst_name.c_str(), F_OK) == 0 && access(src_name.c_str(), F_OK) != 0) {
Michael Runge2f0ef732014-10-22 14:28:23 -0700356 // File was already moved
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700357 return StringValue(dst_name);
358 } else if (rename(src_name.c_str(), dst_name.c_str()) != 0) {
359 return ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
360 src_name.c_str(), dst_name.c_str(), strerror(errno));
Michael Rungece7ca712013-11-06 17:42:20 -0800361 }
362
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700363 return StringValue(dst_name);
Michael Rungece7ca712013-11-06 17:42:20 -0800364}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700365
Tao Bao0831d0b2016-11-03 23:25:04 -0700366// delete([filename, ...])
367// Deletes all the filenames listed. Returns the number of files successfully deleted.
368//
369// delete_recursive([dirname, ...])
370// Recursively deletes dirnames and all their contents. Returns the number of directories
371// successfully deleted.
Doug Zongker512536a2010-02-17 16:11:44 -0800372Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao0831d0b2016-11-03 23:25:04 -0700373 std::vector<std::string> paths(argc);
Tao Baoba9a42a2015-06-23 23:23:33 -0700374 for (int i = 0; i < argc; ++i) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700375 if (!Evaluate(state, argv[i], &paths[i])) {
376 return nullptr;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700377 }
378 }
379
380 bool recursive = (strcmp(name, "delete_recursive") == 0);
381
382 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700383 for (int i = 0; i < argc; ++i) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700384 if ((recursive ? dirUnlinkHierarchy(paths[i].c_str()) : unlink(paths[i].c_str())) == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700385 ++success;
Tianjie Xuaced5d92016-10-12 10:55:04 -0700386 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700387 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700388
Tao Bao0831d0b2016-11-03 23:25:04 -0700389 return StringValue(std::to_string(success));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700390}
391
Doug Zongker8edb00c2009-06-11 17:21:44 -0700392
Doug Zongker512536a2010-02-17 16:11:44 -0800393Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700394 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700395 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700396 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700397
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700398 std::vector<std::string> args;
399 if (!ReadArgs(state, argc, argv, &args)) {
400 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
401 }
402 const std::string& frac_str = args[0];
403 const std::string& sec_str = args[1];
404
405 double frac;
406 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
407 return ErrorAbort(state, kArgsParsingFailure,
408 "%s: failed to parse double in %s\n", name, frac_str.c_str());
409 }
Tao Baob15fd222015-09-24 11:10:51 -0700410 int sec;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700411 if (!android::base::ParseInt(sec_str.c_str(), &sec)) {
412 return ErrorAbort(state, kArgsParsingFailure,
413 "%s: failed to parse int in %s\n", name, sec_str.c_str());
414 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700415
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700416 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700417 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
418
Doug Zongker512536a2010-02-17 16:11:44 -0800419 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700420}
421
Doug Zongker512536a2010-02-17 16:11:44 -0800422Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700423 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700424 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700425 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700426
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700427 std::vector<std::string> args;
428 if (!ReadArgs(state, 1, argv, &args)) {
429 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
430 }
431 const std::string& frac_str = args[0];
432
433 double frac;
434 if (!android::base::ParseDouble(frac_str.c_str(), &frac)) {
435 return ErrorAbort(state, kArgsParsingFailure,
436 "%s: failed to parse double in %s\n", name, frac_str.c_str());
437 }
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700438
439 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
440 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
441
Doug Zongker512536a2010-02-17 16:11:44 -0800442 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700443}
444
Doug Zongker8edb00c2009-06-11 17:21:44 -0700445// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800446Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700447 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700448 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700449 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700450 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700451
452 std::vector<std::string> args;
453 if (!ReadArgs(state, 2, argv, &args)) {
454 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
455 }
456 const std::string& zip_path = args[0];
457 const std::string& dest_path = args[1];
Doug Zongker9931f7f2009-06-10 14:11:53 -0700458
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700459 ZipArchiveHandle za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700460
461 // To create a consistent system image, never use the clock for timestamps.
462 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
463
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700464 bool success = ExtractPackageRecursive(za, zip_path, dest_path, &timestamp, sehandle);
465
Tianjie Xuaced5d92016-10-12 10:55:04 -0700466 return StringValue(success ? "t" : "");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700467}
468
Doug Zongker8edb00c2009-06-11 17:21:44 -0700469
470// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800471// or
472// package_extract_file(package_path)
473// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800474// function (the char* returned is actually a FileContents*).
475Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700476 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700477 if (argc < 1 || argc > 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700478 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800479 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700480 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700481 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700482
Doug Zongker5f875bf2014-08-22 14:53:43 -0700483 if (argc == 2) {
484 // The two-argument version extracts to a file.
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800485
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700486 ZipArchiveHandle za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700487
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700488 std::vector<std::string> args;
489 if (!ReadArgs(state, 2, argv, &args)) {
490 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %d args", name,
491 argc);
492 }
493 const std::string& zip_path = args[0];
494 const std::string& dest_path = args[1];
Doug Zongker6aece332010-02-01 14:40:12 -0800495
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700496 ZipString zip_string_path(zip_path.c_str());
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700497 ZipEntry entry;
498 if (FindEntry(za, zip_string_path, &entry) != 0) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700499 printf("%s: no %s in package\n", name, zip_path.c_str());
500 return StringValue("");
Doug Zongker6aece332010-02-01 14:40:12 -0800501 }
502
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700503 int fd = TEMP_FAILURE_RETRY(ota_open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC,
504 S_IRUSR | S_IWUSR));
505 if (fd == -1) {
506 printf("%s: can't open %s for write: %s\n", name, dest_path.c_str(), strerror(errno));
507 return StringValue("");
508 }
509 success = ExtractEntryToFile(za, &entry, fd);
510 if (ota_fsync(fd) == -1) {
511 printf("fsync of \"%s\" failed: %s\n", dest_path.c_str(), strerror(errno));
512 success = false;
513 }
514 if (ota_close(fd) == -1) {
515 printf("close of \"%s\" failed: %s\n", dest_path.c_str(), strerror(errno));
516 success = false;
Doug Zongker6aece332010-02-01 14:40:12 -0800517 }
Doug Zongker6aece332010-02-01 14:40:12 -0800518
Tianjie Xuaced5d92016-10-12 10:55:04 -0700519 return StringValue(success ? "t" : "");
Doug Zongker6aece332010-02-01 14:40:12 -0800520 } else {
521 // The one-argument version returns the contents of the file
522 // as the result.
523
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700524 std::vector<std::string> args;
525 if (!ReadArgs(state, 1, argv, &args)) {
526 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse %d args", name,
527 argc);
528 }
529 const std::string& zip_path = args[0];
Yabin Cui64be2132016-02-03 18:16:02 -0800530
Tianjie Xuaced5d92016-10-12 10:55:04 -0700531 Value* v = new Value(VAL_INVALID, "");
Doug Zongker6aece332010-02-01 14:40:12 -0800532
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700533 ZipArchiveHandle za = ((UpdaterInfo*)(state->cookie))->package_zip;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700534 ZipString zip_string_path(zip_path.c_str());
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700535 ZipEntry entry;
536 if (FindEntry(za, zip_string_path, &entry) != 0) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700537 printf("%s: no %s in package\n", name, zip_path.c_str());
538 return v;
Doug Zongker6aece332010-02-01 14:40:12 -0800539 }
540
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -0700541 v->data.resize(entry.uncompressed_length);
542 if (ExtractToMemory(za, &entry, reinterpret_cast<uint8_t*>(&v->data[0]),
543 v->data.size()) != 0) {
544 printf("%s: faled to extract %zu bytes to memory\n", name, v->data.size());
545 } else {
546 success = true;
547 }
Doug Zongker6aece332010-02-01 14:40:12 -0800548
Doug Zongker6aece332010-02-01 14:40:12 -0800549 if (!success) {
Tianjie Xuaced5d92016-10-12 10:55:04 -0700550 v->data.clear();
551 } else {
552 v->type = VAL_BLOB;
Doug Zongker6aece332010-02-01 14:40:12 -0800553 }
Doug Zongker512536a2010-02-17 16:11:44 -0800554 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700555 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700556}
557
Doug Zongker9931f7f2009-06-10 14:11:53 -0700558// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700559// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800560Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700561 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700562 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700563 }
Tianjie Xuaced5d92016-10-12 10:55:04 -0700564 std::string target;
565 if (!Evaluate(state, argv[0], &target)) {
566 return nullptr;
567 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700568
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700569 std::vector<std::string> srcs;
570 if (!ReadArgs(state, argc-1, argv+1, &srcs)) {
571 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700572 }
573
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700574 int bad = 0;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700575 for (int i = 0; i < argc-1; ++i) {
576 if (unlink(srcs[i].c_str()) < 0) {
Doug Zongker60babf82009-09-18 15:11:24 -0700577 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700578 printf("%s: failed to remove %s: %s\n",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700579 name, srcs[i].c_str(), strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700580 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700581 }
582 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700583 if (make_parents(&srcs[i][0])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700584 printf("%s: failed to symlink %s to %s: making parents failed\n",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700585 name, srcs[i].c_str(), target.c_str());
Doug Zongkera23075f2012-08-06 16:19:09 -0700586 ++bad;
587 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700588 if (symlink(target.c_str(), srcs[i].c_str()) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700589 printf("%s: failed to symlink %s to %s: %s\n",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700590 name, srcs[i].c_str(), target.c_str(), strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700591 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700592 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700593 }
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700594 if (bad) {
Tianjie Xu16255832016-04-30 11:49:59 -0700595 return ErrorAbort(state, kSymlinkFailure, "%s: some symlinks failed", name);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700596 }
Tianjie Xuaced5d92016-10-12 10:55:04 -0700597 return StringValue("");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700598}
599
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700600struct perm_parsed_args {
601 bool has_uid;
602 uid_t uid;
603 bool has_gid;
604 gid_t gid;
605 bool has_mode;
606 mode_t mode;
607 bool has_fmode;
608 mode_t fmode;
609 bool has_dmode;
610 mode_t dmode;
611 bool has_selabel;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700612 const char* selabel;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700613 bool has_capabilities;
614 uint64_t capabilities;
615};
616
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700617static struct perm_parsed_args ParsePermArgs(State * state, int argc,
618 const std::vector<std::string>& args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700619 int i;
620 struct perm_parsed_args parsed;
621 int bad = 0;
622 static int max_warnings = 20;
623
624 memset(&parsed, 0, sizeof(parsed));
625
626 for (i = 1; i < argc; i += 2) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700627 if (args[i] == "uid") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700628 int64_t uid;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700629 if (sscanf(args[i+1].c_str(), "%" SCNd64, &uid) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700630 parsed.uid = uid;
631 parsed.has_uid = true;
632 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700633 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700634 bad++;
635 }
636 continue;
637 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700638 if (args[i] == "gid") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700639 int64_t gid;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700640 if (sscanf(args[i+1].c_str(), "%" SCNd64, &gid) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700641 parsed.gid = gid;
642 parsed.has_gid = true;
643 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700644 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700645 bad++;
646 }
647 continue;
648 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700649 if (args[i] == "mode") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700650 int32_t mode;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700651 if (sscanf(args[i+1].c_str(), "%" SCNi32, &mode) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700652 parsed.mode = mode;
653 parsed.has_mode = true;
654 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700655 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700656 bad++;
657 }
658 continue;
659 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700660 if (args[i] == "dmode") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700661 int32_t mode;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700662 if (sscanf(args[i+1].c_str(), "%" SCNi32, &mode) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700663 parsed.dmode = mode;
664 parsed.has_dmode = true;
665 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700666 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700667 bad++;
668 }
669 continue;
670 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700671 if (args[i] == "fmode") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700672 int32_t mode;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700673 if (sscanf(args[i+1].c_str(), "%" SCNi32, &mode) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700674 parsed.fmode = mode;
675 parsed.has_fmode = true;
676 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700677 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700678 bad++;
679 }
680 continue;
681 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700682 if (args[i] == "capabilities") {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700683 int64_t capabilities;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700684 if (sscanf(args[i+1].c_str(), "%" SCNi64, &capabilities) == 1) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700685 parsed.capabilities = capabilities;
686 parsed.has_capabilities = true;
687 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700688 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700689 bad++;
690 }
691 continue;
692 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700693 if (args[i] == "selabel") {
694 if (!args[i+1].empty()) {
695 parsed.selabel = args[i+1].c_str();
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700696 parsed.has_selabel = true;
697 } else {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700698 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700699 bad++;
700 }
701 continue;
702 }
703 if (max_warnings != 0) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700704 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i].c_str());
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700705 max_warnings--;
706 if (max_warnings == 0) {
707 printf("ParsedPermArgs: suppressing further warnings\n");
708 }
709 }
710 }
711 return parsed;
712}
713
714static int ApplyParsedPerms(
Michael Runged4a63422014-10-22 19:48:41 -0700715 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700716 const char* filename,
717 const struct stat *statptr,
718 struct perm_parsed_args parsed)
719{
720 int bad = 0;
721
Nick Kralevich68802412014-10-23 20:36:42 -0700722 if (parsed.has_selabel) {
723 if (lsetfilecon(filename, parsed.selabel) != 0) {
724 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
725 filename, parsed.selabel, strerror(errno));
726 bad++;
727 }
728 }
729
Nick Kraleviche4612512013-09-10 15:34:19 -0700730 /* ignore symlinks */
731 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich68802412014-10-23 20:36:42 -0700732 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700733 }
734
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700735 if (parsed.has_uid) {
736 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700737 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
738 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700739 bad++;
740 }
741 }
742
743 if (parsed.has_gid) {
744 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700745 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
746 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700747 bad++;
748 }
749 }
750
751 if (parsed.has_mode) {
752 if (chmod(filename, parsed.mode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700753 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
754 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700755 bad++;
756 }
757 }
758
759 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
760 if (chmod(filename, parsed.dmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700761 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
762 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700763 bad++;
764 }
765 }
766
767 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
768 if (chmod(filename, parsed.fmode) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700769 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700770 filename, parsed.fmode, strerror(errno));
771 bad++;
772 }
773 }
774
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700775 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
776 if (parsed.capabilities == 0) {
777 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
778 // Report failure unless it's ENODATA (attribute not set)
Michael Runged4a63422014-10-22 19:48:41 -0700779 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700780 filename, parsed.capabilities, strerror(errno));
781 bad++;
782 }
783 } else {
784 struct vfs_cap_data cap_data;
785 memset(&cap_data, 0, sizeof(cap_data));
786 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
787 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
788 cap_data.data[0].inheritable = 0;
789 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
790 cap_data.data[1].inheritable = 0;
791 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runged4a63422014-10-22 19:48:41 -0700792 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
793 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700794 bad++;
795 }
796 }
797 }
798
799 return bad;
800}
801
802// nftw doesn't allow us to pass along context, so we need to use
803// global variables. *sigh*
804static struct perm_parsed_args recursive_parsed_args;
Michael Runged4a63422014-10-22 19:48:41 -0700805static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700806
807static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
808 int fileflags, struct FTW *pfwt) {
Michael Runged4a63422014-10-22 19:48:41 -0700809 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700810}
811
812static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700813 if ((argc % 2) != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700814 return ErrorAbort(state, kArgsParsingFailure,
815 "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700816 }
817
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700818 std::vector<std::string> args;
819 if (!ReadArgs(state, argc, argv, &args)) {
820 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700821 }
822
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700823 struct stat sb;
824 if (lstat(args[0].c_str(), &sb) == -1) {
825 return ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s",
826 name, args[0].c_str(), strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700827 }
828
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700829 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
830 int bad = 0;
831 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700832
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700833 if (recursive) {
834 recursive_parsed_args = parsed;
835 recursive_state = state;
836 bad += nftw(args[0].c_str(), do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
837 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
838 recursive_state = NULL;
839 } else {
840 bad += ApplyParsedPerms(state, args[0].c_str(), &sb, parsed);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700841 }
842
843 if (bad > 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700844 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700845 }
846
Tianjie Xuaced5d92016-10-12 10:55:04 -0700847 return StringValue("");
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700848}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700849
Doug Zongker512536a2010-02-17 16:11:44 -0800850Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700851 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700852 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700853 }
Tianjie Xuaced5d92016-10-12 10:55:04 -0700854 std::string key;
855 if (!Evaluate(state, argv[0], &key)) {
856 return nullptr;
857 }
Elliott Hughescb220402016-09-23 15:30:55 -0700858 std::string value = android::base::GetProperty(key, "");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700859
Tianjie Xuaced5d92016-10-12 10:55:04 -0700860 return StringValue(value);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700861}
862
863
Doug Zongker47cace92009-06-18 10:11:50 -0700864// file_getprop(file, key)
865//
866// interprets 'file' as a getprop-style file (key=value pairs, one
Tao Bao51d516e2016-11-03 14:49:01 -0700867// per line. # comment lines, blank lines, lines without '=' ignored),
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700868// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800869Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Bao51d516e2016-11-03 14:49:01 -0700870 if (argc != 2) {
871 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
872 }
873
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700874 std::vector<std::string> args;
875 if (!ReadArgs(state, 2, argv, &args)) {
876 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker47cace92009-06-18 10:11:50 -0700877 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700878 const std::string& filename = args[0];
879 const std::string& key = args[1];
Doug Zongker47cace92009-06-18 10:11:50 -0700880
881 struct stat st;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700882 if (stat(filename.c_str(), &st) < 0) {
883 return ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name,
884 filename.c_str(), strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700885 }
886
Tao Bao51d516e2016-11-03 14:49:01 -0700887 constexpr off_t MAX_FILE_GETPROP_SIZE = 65536;
Doug Zongker47cace92009-06-18 10:11:50 -0700888 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tao Bao51d516e2016-11-03 14:49:01 -0700889 return ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %lld)",
890 filename.c_str(), name, static_cast<long long>(MAX_FILE_GETPROP_SIZE));
Doug Zongker47cace92009-06-18 10:11:50 -0700891 }
892
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700893 std::string buffer(st.st_size, '\0');
894 FILE* f = ota_fopen(filename.c_str(), "rb");
895 if (f == nullptr) {
896 return ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name,
897 filename.c_str(), strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700898 }
899
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700900 if (ota_fread(&buffer[0], 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
Tianjie Xu84478e82016-05-23 11:42:40 -0700901 ErrorAbort(state, kFreadFailure, "%s: failed to read %zu bytes from %s",
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700902 name, static_cast<size_t>(st.st_size), filename.c_str());
Jed Estepa7b9a462015-12-15 16:04:53 -0800903 ota_fclose(f);
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700904 return nullptr;
Doug Zongker47cace92009-06-18 10:11:50 -0700905 }
Doug Zongker47cace92009-06-18 10:11:50 -0700906
Jed Estepa7b9a462015-12-15 16:04:53 -0800907 ota_fclose(f);
Doug Zongker47cace92009-06-18 10:11:50 -0700908
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700909 std::vector<std::string> lines = android::base::Split(buffer, "\n");
910 for (size_t i = 0; i < lines.size(); i++) {
911 std::string line = android::base::Trim(lines[i]);
Doug Zongker47cace92009-06-18 10:11:50 -0700912
913 // comment or blank line: skip to next line
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700914 if (line.empty() || line[0] == '#') {
915 continue;
916 }
917 size_t equal_pos = line.find('=');
918 if (equal_pos == std::string::npos) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700919 continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700920 }
921
922 // trim whitespace between key and '='
Tao Bao51d516e2016-11-03 14:49:01 -0700923 std::string str = android::base::Trim(line.substr(0, equal_pos));
Doug Zongker47cace92009-06-18 10:11:50 -0700924
925 // not the key we're looking for
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700926 if (key != str) continue;
Doug Zongker47cace92009-06-18 10:11:50 -0700927
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700928 return StringValue(android::base::Trim(line.substr(equal_pos + 1)));
929 }
Doug Zongker47cace92009-06-18 10:11:50 -0700930
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700931 return StringValue("");
Doug Zongker47cace92009-06-18 10:11:50 -0700932}
933
Doug Zongker8edb00c2009-06-11 17:21:44 -0700934// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -0800935Value* ApplyPatchSpaceFn(const char* name, State* state,
936 int argc, Expr* argv[]) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700937 std::vector<std::string> args;
938 if (!ReadArgs(state, 1, argv, &args)) {
939 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800940 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700941 const std::string& bytes_str = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -0800942
Tao Baob15fd222015-09-24 11:10:51 -0700943 size_t bytes;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700944 if (!android::base::ParseUint(bytes_str.c_str(), &bytes)) {
945 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n",
946 name, bytes_str.c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800947 }
948
Tianjie Xuaced5d92016-10-12 10:55:04 -0700949 return StringValue(CacheSizeCheck(bytes) ? "" : "t");
Doug Zongkerc4351c72010-02-22 14:46:32 -0800950}
951
Doug Zongker52b40362014-02-10 15:30:30 -0800952// apply_patch(file, size, init_sha1, tgt_sha1, patch)
953
Doug Zongker512536a2010-02-17 16:11:44 -0800954Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -0800955 if (argc < 6 || (argc % 2) == 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700956 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an "
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700957 "even number, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700958 }
959
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700960 std::vector<std::string> args;
961 if (!ReadArgs(state, 4, argv, &args)) {
962 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700963 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700964 const std::string& source_filename = args[0];
965 const std::string& target_filename = args[1];
966 const std::string& target_sha1 = args[2];
967 const std::string& target_size_str = args[3];
Doug Zongker8edb00c2009-06-11 17:21:44 -0700968
Tao Baob15fd222015-09-24 11:10:51 -0700969 size_t target_size;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700970 if (!android::base::ParseUint(target_size_str.c_str(), &target_size)) {
971 return ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count",
972 name, target_size_str.c_str());
Doug Zongkerc4351c72010-02-22 14:46:32 -0800973 }
974
975 int patchcount = (argc-4) / 2;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700976 std::vector<std::unique_ptr<Value>> arg_values;
977 if (!ReadValueArgs(state, argc-4, argv+4, &arg_values)) {
Yabin Cui64be2132016-02-03 18:16:02 -0800978 return nullptr;
979 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700980
Yabin Cui64be2132016-02-03 18:16:02 -0800981 for (int i = 0; i < patchcount; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700982 if (arg_values[i * 2]->type != VAL_STRING) {
983 return ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name,
984 i * 2);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800985 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700986 if (arg_values[i * 2 + 1]->type != VAL_BLOB) {
987 return ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name,
988 i * 2 + 1);
Doug Zongkerc4351c72010-02-22 14:46:32 -0800989 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700990 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700991
Tianjie Xuaced5d92016-10-12 10:55:04 -0700992 std::vector<std::string> patch_sha_str;
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700993 std::vector<std::unique_ptr<Value>> patches;
Yabin Cui64be2132016-02-03 18:16:02 -0800994 for (int i = 0; i < patchcount; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700995 patch_sha_str.push_back(arg_values[i * 2]->data);
996 patches.push_back(std::move(arg_values[i * 2 + 1]));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700997 }
Doug Zongkerc4351c72010-02-22 14:46:32 -0800998
Tianjie Xu5fe280a2016-10-17 18:15:20 -0700999 int result = applypatch(source_filename.c_str(), target_filename.c_str(),
1000 target_sha1.c_str(), target_size,
1001 patch_sha_str, patches, nullptr);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001002
Tianjie Xuaced5d92016-10-12 10:55:04 -07001003 return StringValue(result == 0 ? "t" : "");
Doug Zongkerc4351c72010-02-22 14:46:32 -08001004}
1005
1006// apply_patch_check(file, [sha1_1, ...])
1007Value* ApplyPatchCheckFn(const char* name, State* state,
1008 int argc, Expr* argv[]) {
1009 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001010 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %d",
Doug Zongkerc4351c72010-02-22 14:46:32 -08001011 name, argc);
1012 }
1013
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001014 std::vector<std::string> args;
1015 if (!ReadArgs(state, 1, argv, &args)) {
1016 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001017 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001018 const std::string& filename = args[0];
Doug Zongkerc4351c72010-02-22 14:46:32 -08001019
Tianjie Xuaced5d92016-10-12 10:55:04 -07001020 std::vector<std::string> sha1s;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001021 if (!ReadArgs(state, argc - 1, argv + 1, &sha1s)) {
1022 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001023 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001024 int result = applypatch_check(filename.c_str(), sha1s);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001025
1026 return StringValue(result == 0 ? "t" : "");
Doug Zongker8edb00c2009-06-11 17:21:44 -07001027}
1028
Tao Bao1107d962015-09-09 17:16:55 -07001029// This is the updater side handler for ui_print() in edify script. Contents
1030// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001031Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001032 std::vector<std::string> args;
1033 if (!ReadArgs(state, argc, argv, &args)) {
1034 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001035 }
1036
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001037 std::string buffer = android::base::Join(args, "") + "\n";
Michael Runged4a63422014-10-22 19:48:41 -07001038 uiPrint(state, buffer);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001039 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001040}
1041
Doug Zongkerd0181b82011-10-19 10:51:12 -07001042Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1043 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001044 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001045 }
1046 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
Tianjie Xuaced5d92016-10-12 10:55:04 -07001047 return StringValue("t");
Doug Zongkerd0181b82011-10-19 10:51:12 -07001048}
1049
Doug Zongker512536a2010-02-17 16:11:44 -08001050Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001051 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001052 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001053 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001054
1055 std::vector<std::string> args;
1056 if (!ReadArgs(state, argc, argv, &args)) {
1057 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001058 }
1059
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001060 char* args2[argc+1];
1061 for (int i = 0; i < argc; i++) {
1062 args2[i] = &args[i][0];
1063 }
1064 args2[argc] = nullptr;
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001065
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001066 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001067
1068 pid_t child = fork();
1069 if (child == 0) {
1070 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001071 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001072 _exit(1);
1073 }
1074 int status;
1075 waitpid(child, &status, 0);
1076 if (WIFEXITED(status)) {
1077 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001078 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001079 WEXITSTATUS(status));
1080 }
1081 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001082 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001083 WTERMSIG(status));
1084 }
1085
Tianjie Xuaced5d92016-10-12 10:55:04 -07001086 return StringValue(android::base::StringPrintf("%d", status));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001087}
1088
Doug Zongker512536a2010-02-17 16:11:44 -08001089// sha1_check(data)
1090// to return the sha1 of the data (given in the format returned by
1091// read_file).
1092//
1093// sha1_check(data, sha1_hex, [sha1_hex, ...])
1094// returns the sha1 of the file if it matches any of the hex
1095// strings passed, or "" if it does not equal any of them.
1096//
1097Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1098 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001099 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongker512536a2010-02-17 16:11:44 -08001100 }
1101
Tianjie Xuaced5d92016-10-12 10:55:04 -07001102 std::vector<std::unique_ptr<Value>> args;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001103 if (!ReadValueArgs(state, argc, argv, &args)) {
1104 return nullptr;
Doug Zongker512536a2010-02-17 16:11:44 -08001105 }
1106
Tianjie Xuaced5d92016-10-12 10:55:04 -07001107 if (args[0]->type == VAL_INVALID) {
1108 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -08001109 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001110 uint8_t digest[SHA_DIGEST_LENGTH];
Tianjie Xuaced5d92016-10-12 10:55:04 -07001111 SHA1(reinterpret_cast<const uint8_t*>(args[0]->data.c_str()), args[0]->data.size(), digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001112
1113 if (argc == 1) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001114 return StringValue(print_sha1(digest));
Doug Zongker512536a2010-02-17 16:11:44 -08001115 }
1116
Tao Bao361342c2016-02-08 11:15:50 -08001117 for (int i = 1; i < argc; ++i) {
1118 uint8_t arg_digest[SHA_DIGEST_LENGTH];
Doug Zongker512536a2010-02-17 16:11:44 -08001119 if (args[i]->type != VAL_STRING) {
Tao Bao361342c2016-02-08 11:15:50 -08001120 printf("%s(): arg %d is not a string; skipping", name, i);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001121 } else if (ParseSha1(args[i]->data.c_str(), arg_digest) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001122 // Warn about bad args and skip them.
Tianjie Xuaced5d92016-10-12 10:55:04 -07001123 printf("%s(): error parsing \"%s\" as sha-1; skipping", name, args[i]->data.c_str());
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001124 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
Tao Bao361342c2016-02-08 11:15:50 -08001125 // Found a match.
1126 return args[i].release();
Doug Zongker512536a2010-02-17 16:11:44 -08001127 }
Doug Zongker512536a2010-02-17 16:11:44 -08001128 }
Tao Bao361342c2016-02-08 11:15:50 -08001129
1130 // Didn't match any of the hex strings; return false.
Tianjie Xuaced5d92016-10-12 10:55:04 -07001131 return StringValue("");
Doug Zongker512536a2010-02-17 16:11:44 -08001132}
1133
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001134// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001135// is actually a FileContents*).
1136Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1137 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001138 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker512536a2010-02-17 16:11:44 -08001139 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001140
1141 std::vector<std::string> args;
1142 if (!ReadArgs(state, 1, argv, &args)) {
1143 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1144 }
1145 const std::string& filename = args[0];
Doug Zongker512536a2010-02-17 16:11:44 -08001146
Tianjie Xuaced5d92016-10-12 10:55:04 -07001147 Value* v = new Value(VAL_INVALID, "");
Doug Zongker512536a2010-02-17 16:11:44 -08001148
1149 FileContents fc;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001150 if (LoadFileContents(filename.c_str(), &fc) == 0) {
Tianjie Xuaced5d92016-10-12 10:55:04 -07001151 v->type = VAL_BLOB;
1152 v->data = std::string(fc.data.begin(), fc.data.end());
Doug Zongker512536a2010-02-17 16:11:44 -08001153 }
Doug Zongker512536a2010-02-17 16:11:44 -08001154 return v;
1155}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001156
Doug Zongkerc87bab12013-11-25 13:53:25 -08001157// Immediately reboot the device. Recovery is not finished normally,
1158// so if you reboot into recovery it will re-start applying the
1159// current package (because nothing has cleared the copy of the
1160// arguments stored in the BCB).
1161//
1162// The argument is the partition name passed to the android reboot
1163// property. It can be "recovery" to boot from the recovery
1164// partition, or "" (empty string) to boot from the regular boot
1165// partition.
1166Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1167 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001168 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001169 }
1170
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001171 std::vector<std::string> args;
1172 if (!ReadArgs(state, 2, argv, &args)) {
1173 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1174 }
1175 const std::string& filename = args[0];
1176 const std::string& property = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001177
Doug Zongkerc87bab12013-11-25 13:53:25 -08001178 // zero out the 'command' field of the bootloader message.
Elliott Hughescb220402016-09-23 15:30:55 -07001179 char buffer[80];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001180 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001181 FILE* f = ota_fopen(filename.c_str(), "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001182 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
Jed Estepa7b9a462015-12-15 16:04:53 -08001183 ota_fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1184 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001185
Elliott Hughescb220402016-09-23 15:30:55 -07001186 std::string reboot_cmd = "reboot,";
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001187 reboot_cmd += property;
Elliott Hughescb220402016-09-23 15:30:55 -07001188 android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_cmd);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001189
1190 sleep(5);
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001191 return ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001192}
1193
1194// Store a string value somewhere that future invocations of recovery
1195// can access it. This value is called the "stage" and can be used to
1196// drive packages that need to do reboots in the middle of
1197// installation and keep track of where they are in the multi-stage
1198// install.
1199//
1200// The first argument is the block device for the misc partition
1201// ("/misc" in the fstab), which is where this value is stored. The
1202// second argument is the string to store; it should not exceed 31
1203// bytes.
1204Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1205 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001206 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001207 }
1208
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001209 std::vector<std::string> args;
1210 if (!ReadArgs(state, 2, argv, &args)) {
1211 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1212 }
1213 const std::string& filename = args[0];
1214 std::string& stagestr = args[1];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001215
1216 // Store this value in the misc partition, immediately after the
1217 // bootloader message that the main recovery uses to save its
1218 // arguments in case of the device restarting midway through
1219 // package installation.
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001220 FILE* f = ota_fopen(filename.c_str(), "r+b");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001221 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001222 size_t to_write = stagestr.size();
Tianjie Xuaced5d92016-10-12 10:55:04 -07001223 size_t max_size = sizeof(((struct bootloader_message*)0)->stage);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001224 if (to_write > max_size) {
1225 to_write = max_size;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001226 stagestr = stagestr.substr(0, max_size-1);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001227 }
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001228 size_t status = ota_fwrite(stagestr.c_str(), to_write, 1, f);
Jed Estepa7b9a462015-12-15 16:04:53 -08001229 ota_fclose(f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001230
Tianjie Xuaced5d92016-10-12 10:55:04 -07001231 if (status != to_write) {
1232 return StringValue("");
1233 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001234 return StringValue(filename);
1235}
1236
1237// Return the value most recently saved with SetStageFn. The argument
1238// is the block device for the misc partition.
1239Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001240 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001241 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001242 }
1243
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001244 std::vector<std::string> args;
1245 if (!ReadArgs(state, 1, argv, &args)) {
1246 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1247 }
1248 const std::string& filename = args[0];
Doug Zongkerc87bab12013-11-25 13:53:25 -08001249
1250 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001251 FILE* f = ota_fopen(filename.c_str(), "rb");
Doug Zongkerc87bab12013-11-25 13:53:25 -08001252 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001253 size_t status = ota_fread(buffer, sizeof(buffer), 1, f);
Jed Estepa7b9a462015-12-15 16:04:53 -08001254 ota_fclose(f);
Tianjie Xuaced5d92016-10-12 10:55:04 -07001255 if (status != sizeof(buffer)) {
1256 return StringValue("");
1257 }
Doug Zongkerc87bab12013-11-25 13:53:25 -08001258
Tianjie Xuaced5d92016-10-12 10:55:04 -07001259 buffer[sizeof(buffer)-1] = '\0';
1260 return StringValue(buffer);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001261}
1262
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001263Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1264 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001265 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001266 }
1267
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001268 std::vector<std::string> args;
1269 if (!ReadArgs(state, 2, argv, &args)) {
1270 return ErrorAbort(state, kArgsParsingFailure, "%s() Failed to parse the argument(s)", name);
1271 }
1272 const std::string& filename = args[0];
1273 const std::string& len_str = args[1];
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001274
Tao Baob15fd222015-09-24 11:10:51 -07001275 size_t len;
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001276 if (!android::base::ParseUint(len_str.c_str(), &len)) {
1277 return nullptr;
1278 }
1279 int fd = ota_open(filename.c_str(), O_WRONLY, 0644);
1280 // The wipe_block_device function in ext4_utils returns 0 on success and 1
1281 // for failure.
1282 int status = wipe_block_device(fd, len);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001283
Jed Estepa7b9a462015-12-15 16:04:53 -08001284 ota_close(fd);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001285
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001286 return StringValue((status == 0) ? "t" : "");
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001287}
1288
Doug Zongkerc704e062014-05-23 08:40:35 -07001289Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1290 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001291 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerc704e062014-05-23 08:40:35 -07001292 }
1293 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1294 fprintf(ui->cmd_pipe, "enable_reboot\n");
Tianjie Xuaced5d92016-10-12 10:55:04 -07001295 return StringValue("t");
Doug Zongkerc704e062014-05-23 08:40:35 -07001296}
1297
Michael Rungeacf47db2014-11-21 00:12:28 -08001298Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1299 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001300 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %d", name, argc);
Michael Rungeacf47db2014-11-21 00:12:28 -08001301 }
1302
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001303 std::vector<std::string> args;
1304 if (!ReadArgs(state, argc, argv, &args)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001305 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
Michael Rungeacf47db2014-11-21 00:12:28 -08001306 }
1307
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001308 char* args2[argc+1];
Michael Rungeacf47db2014-11-21 00:12:28 -08001309 // Tune2fs expects the program name as its args[0]
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001310 args2[0] = const_cast<char*>(name);
1311 if (args2[0] == nullptr) {
1312 return nullptr;
Michael Rungeacf47db2014-11-21 00:12:28 -08001313 }
Tao Baoba9a42a2015-06-23 23:23:33 -07001314 for (int i = 0; i < argc; ++i) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001315 args2[i + 1] = &args[i][0];
Michael Rungeacf47db2014-11-21 00:12:28 -08001316 }
Michael Rungeacf47db2014-11-21 00:12:28 -08001317
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001318 // tune2fs changes the file system parameters on an ext2 file system; it
1319 // returns 0 on success.
1320 int result = tune2fs_main(argc + 1, args2);
1321
Michael Rungeacf47db2014-11-21 00:12:28 -08001322 if (result != 0) {
Tianjie Xu5fe280a2016-10-17 18:15:20 -07001323 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d", name, result);
Michael Rungeacf47db2014-11-21 00:12:28 -08001324 }
Tianjie Xuaced5d92016-10-12 10:55:04 -07001325 return StringValue("t");
Michael Rungeacf47db2014-11-21 00:12:28 -08001326}
1327
Doug Zongker9931f7f2009-06-10 14:11:53 -07001328void RegisterInstallFunctions() {
1329 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001330 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001331 RegisterFunction("unmount", UnmountFn);
1332 RegisterFunction("format", FormatFn);
1333 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001334 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001335 RegisterFunction("delete", DeleteFn);
1336 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001337 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1338 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001339 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001340
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001341 // Usage:
1342 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1343 // Example:
1344 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1345 RegisterFunction("set_metadata", SetMetadataFn);
1346
1347 // Usage:
1348 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1349 // Example:
1350 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1351 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1352
Doug Zongker8edb00c2009-06-11 17:21:44 -07001353 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001354 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001355
1356 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001357 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1358 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001359
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001360 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001361
Doug Zongker512536a2010-02-17 16:11:44 -08001362 RegisterFunction("read_file", ReadFileFn);
1363 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001364 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001365
Doug Zongkerd0181b82011-10-19 10:51:12 -07001366 RegisterFunction("wipe_cache", WipeCacheFn);
1367
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001368 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001369
1370 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001371
1372 RegisterFunction("reboot_now", RebootNowFn);
1373 RegisterFunction("get_stage", GetStageFn);
1374 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001375
1376 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeacf47db2014-11-21 00:12:28 -08001377 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001378}