blob: 1c796405378f2760cc1cef397aebb879e19ba328 [file] [log] [blame]
Doug Zongker9931f7f2009-06-10 14:11:53 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Doug Zongkerfbf3c102009-06-24 09:36:20 -070017#include <ctype.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070018#include <errno.h>
19#include <stdarg.h>
Doug Zongkerfbf3c102009-06-24 09:36:20 -070020#include <stdio.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070021#include <stdlib.h>
22#include <string.h>
23#include <sys/mount.h>
24#include <sys/stat.h>
25#include <sys/types.h>
Doug Zongkera3f89ea2009-09-10 14:10:48 -070026#include <sys/wait.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070027#include <unistd.h>
Hristo Bojinovdb314d62010-08-02 10:29:49 -070028#include <fcntl.h>
29#include <time.h>
Nick Kralevich5dbdef02013-09-07 14:41:06 -070030#include <selinux/selinux.h>
31#include <ftw.h>
32#include <sys/capability.h>
33#include <sys/xattr.h>
34#include <linux/xattr.h>
35#include <inttypes.h>
Doug Zongker9931f7f2009-06-10 14:11:53 -070036
Yabin Cui64be2132016-02-03 18:16:02 -080037#include <memory>
Tao Bao1bf17722016-11-03 23:52:01 -070038#include <string>
Yabin Cui64be2132016-02-03 18:16:02 -080039#include <vector>
40
Tao Bao1bf17722016-11-03 23:52:01 -070041#include <android-base/file.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080042#include <android-base/parseint.h>
43#include <android-base/strings.h>
44#include <android-base/stringprintf.h>
Tao Bao1107d962015-09-09 17:16:55 -070045
Doug Zongkerc87bab12013-11-25 13:53:25 -080046#include "bootloader.h"
47#include "applypatch/applypatch.h"
48#include "cutils/android_reboot.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070049#include "cutils/misc.h"
50#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070051#include "edify/expr.h"
Tianjie Xu16255832016-04-30 11:49:59 -070052#include "error_code.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070053#include "minzip/DirUtil.h"
54#include "mtdutils/mounts.h"
55#include "mtdutils/mtdutils.h"
Tianjie Xu16255832016-04-30 11:49:59 -070056#include "openssl/sha.h"
Jed Estepff6df892015-12-15 16:04:53 -080057#include "ota_io.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070058#include "updater.h"
Doug Zongker512536a2010-02-17 16:11:44 -080059#include "applypatch/applypatch.h"
Dees_Troy512376c2013-09-03 19:39:41 +000060#include "flashutils/flashutils.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080061#include "install.h"
Ethan Yonker7e1b9862015-03-19 14:10:01 -050062#ifdef HAVE_LIBTUNE2FS
Michael Rungeb278c252014-11-21 00:12:28 -080063#include "tune2fs.h"
Ethan Yonker7e1b9862015-03-19 14:10:01 -050064#endif
Doug Zongker8edb00c2009-06-11 17:21:44 -070065
Doug Zongker3d177d02010-07-01 09:18:44 -070066#ifdef USE_EXT4
67#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080068#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070069#endif
70
Tao Bao1107d962015-09-09 17:16:55 -070071// Send over the buffer to recovery though the command pipe.
72static void uiPrint(State* state, const std::string& buffer) {
73 UpdaterInfo* ui = reinterpret_cast<UpdaterInfo*>(state->cookie);
Tao Baob6918c72015-05-19 17:02:16 -070074
Tao Bao1107d962015-09-09 17:16:55 -070075 // "line1\nline2\n" will be split into 3 tokens: "line1", "line2" and "".
76 // So skip sending empty strings to UI.
77 std::vector<std::string> lines = android::base::Split(buffer, "\n");
78 for (auto& line: lines) {
79 if (!line.empty()) {
80 fprintf(ui->cmd_pipe, "ui_print %s\n", line.c_str());
81 fprintf(ui->cmd_pipe, "ui_print\n");
82 }
Michael Runge75480252014-10-22 19:48:41 -070083 }
Tao Bao1107d962015-09-09 17:16:55 -070084
85 // On the updater side, we need to dump the contents to stderr (which has
86 // been redirected to the log file). Because the recovery will only print
87 // the contents to screen when processing pipe command ui_print.
88 fprintf(stderr, "%s", buffer.c_str());
Michael Runge75480252014-10-22 19:48:41 -070089}
90
91__attribute__((__format__(printf, 2, 3))) __nonnull((2))
92void uiPrintf(State* state, const char* format, ...) {
Tao Bao1107d962015-09-09 17:16:55 -070093 std::string error_msg;
94
Michael Runge75480252014-10-22 19:48:41 -070095 va_list ap;
96 va_start(ap, format);
Tao Bao1107d962015-09-09 17:16:55 -070097 android::base::StringAppendV(&error_msg, format, ap);
Michael Runge75480252014-10-22 19:48:41 -070098 va_end(ap);
Tao Bao1107d962015-09-09 17:16:55 -070099
Michael Runge75480252014-10-22 19:48:41 -0700100 uiPrint(state, error_msg);
101}
102
Doug Zongker52b40362014-02-10 15:30:30 -0800103// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -0700104char* PrintSha1(const uint8_t* digest) {
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800105 char* buffer = reinterpret_cast<char*>(malloc(SHA_DIGEST_LENGTH*2 + 1));
Doug Zongker52b40362014-02-10 15:30:30 -0800106 const char* alphabet = "0123456789abcdef";
Tao Baoba9a42a2015-06-23 23:23:33 -0700107 size_t i;
Sen Jiangc48cb5e2016-02-04 16:23:21 +0800108 for (i = 0; i < SHA_DIGEST_LENGTH; ++i) {
Doug Zongker52b40362014-02-10 15:30:30 -0800109 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
110 buffer[i*2+1] = alphabet[digest[i] & 0xf];
111 }
112 buffer[i*2] = '\0';
113 return buffer;
114}
115
Doug Zongker3d177d02010-07-01 09:18:44 -0700116// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700117//
Doug Zongker3d177d02010-07-01 09:18:44 -0700118// fs_type="yaffs2" partition_type="MTD" location=partition
119// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -0800120Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700121 char* result = NULL;
Michael Rungebd6138c2014-10-22 17:05:08 -0700122 if (argc != 4 && argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700123 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700124 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700125 char* fs_type;
126 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700127 char* location;
128 char* mount_point;
Michael Rungebd6138c2014-10-22 17:05:08 -0700129 char* mount_options;
130 bool has_mount_options;
131 if (argc == 5) {
132 has_mount_options = true;
133 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
134 &location, &mount_point, &mount_options) < 0) {
135 return NULL;
136 }
137 } else {
138 has_mount_options = false;
139 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700140 &location, &mount_point) < 0) {
Michael Rungebd6138c2014-10-22 17:05:08 -0700141 return NULL;
142 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700143 }
144
Doug Zongker3d177d02010-07-01 09:18:44 -0700145 if (strlen(fs_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700146 ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700147 goto done;
148 }
149 if (strlen(partition_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700150 ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
Doug Zongker3d177d02010-07-01 09:18:44 -0700151 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700152 goto done;
153 }
154 if (strlen(location) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700155 ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700156 goto done;
157 }
158 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700159 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
160 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700161 goto done;
162 }
163
Tao Baoba9a42a2015-06-23 23:23:33 -0700164 {
165 char *secontext = NULL;
Stephen Smalley779701d2012-02-09 14:13:23 -0500166
Tao Baoba9a42a2015-06-23 23:23:33 -0700167 if (sehandle) {
168 selabel_lookup(sehandle, &secontext, mount_point, 0755);
169 setfscreatecon(secontext);
170 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500171
Tao Baoba9a42a2015-06-23 23:23:33 -0700172 mkdir(mount_point, 0755);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700173
Tao Baoba9a42a2015-06-23 23:23:33 -0700174 if (secontext) {
175 freecon(secontext);
176 setfscreatecon(NULL);
177 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500178 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500179
Doug Zongker3d177d02010-07-01 09:18:44 -0700180 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700181 mtd_scan_partitions();
182 const MtdPartition* mtd;
183 mtd = mtd_find_partition_by_name(location);
184 if (mtd == NULL) {
Tao Bao1107d962015-09-09 17:16:55 -0700185 uiPrintf(state, "%s: no mtd partition named \"%s\"\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700186 name, location);
187 result = strdup("");
188 goto done;
189 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700190 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Michael Rungef15e31e2014-10-24 14:14:41 -0700191 uiPrintf(state, "mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700192 location, strerror(errno));
193 result = strdup("");
194 goto done;
195 }
196 result = mount_point;
197 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700198 if (mount(location, mount_point, fs_type,
Michael Rungebd6138c2014-10-22 17:05:08 -0700199 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
200 has_mount_options ? mount_options : "") < 0) {
Michael Rungef15e31e2014-10-24 14:14:41 -0700201 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700202 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700203 result = strdup("");
204 } else {
205 result = mount_point;
206 }
207 }
208
209done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700210 free(fs_type);
211 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700212 free(location);
213 if (result != mount_point) free(mount_point);
Michael Rungebd6138c2014-10-22 17:05:08 -0700214 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800215 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700216}
217
Doug Zongker8edb00c2009-06-11 17:21:44 -0700218
219// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800220Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700221 char* result = NULL;
222 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700223 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700224 }
225 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700226 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700227 return NULL;
228 }
229 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700230 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700231 goto done;
232 }
233
234 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700235 {
236 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
237 if (vol == NULL) {
238 result = strdup("");
239 } else {
240 result = mount_point;
241 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700242 }
243
244done:
245 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800246 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700247}
248
249
Doug Zongker512536a2010-02-17 16:11:44 -0800250Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700251 char* result = NULL;
252 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700253 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700254 }
255 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700256 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700257 return NULL;
258 }
259 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700260 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700261 goto done;
262 }
263
264 scan_mounted_volumes();
Tao Baoba9a42a2015-06-23 23:23:33 -0700265 {
266 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
267 if (vol == NULL) {
268 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
269 result = strdup("");
270 } else {
271 int ret = unmount_mounted_volume(vol);
272 if (ret != 0) {
273 uiPrintf(state, "unmount of %s failed (%d): %s\n",
274 mount_point, ret, strerror(errno));
275 }
276 result = mount_point;
Michael Rungef15e31e2014-10-24 14:14:41 -0700277 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700278 }
279
280done:
281 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800282 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700283}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700284
JP Abgrall37aedb32014-06-16 19:07:39 -0700285static int exec_cmd(const char* path, char* const argv[]) {
286 int status;
287 pid_t child;
288 if ((child = vfork()) == 0) {
289 execv(path, argv);
290 _exit(-1);
291 }
292 waitpid(child, &status, 0);
293 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
294 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
295 }
296 return WEXITSTATUS(status);
297}
298
Doug Zongker8edb00c2009-06-11 17:21:44 -0700299
Stephen Smalley779701d2012-02-09 14:13:23 -0500300// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700301//
Stephen Smalley779701d2012-02-09 14:13:23 -0500302// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
303// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700304// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
305// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800306// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700307// 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 -0800308Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700309 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400310 if (argc != 5) {
Tianjie Xu16255832016-04-30 11:49:59 -0700311 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700312 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700313 char* fs_type;
314 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700315 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800316 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400317 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500318
Stephen Smalley779701d2012-02-09 14:13:23 -0500319 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
320 return NULL;
321 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700322
Doug Zongker3d177d02010-07-01 09:18:44 -0700323 if (strlen(fs_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700324 ErrorAbort(state, kArgsParsingFailure, "fs_type argument to %s() can't be empty", name);
Doug Zongker3d177d02010-07-01 09:18:44 -0700325 goto done;
326 }
327 if (strlen(partition_type) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700328 ErrorAbort(state, kArgsParsingFailure, "partition_type argument to %s() can't be empty",
Doug Zongker3d177d02010-07-01 09:18:44 -0700329 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700330 goto done;
331 }
332 if (strlen(location) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700333 ErrorAbort(state, kArgsParsingFailure, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700334 goto done;
335 }
336
Stephen Smalley516e4e22012-04-03 13:35:11 -0400337 if (strlen(mount_point) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700338 ErrorAbort(state, kArgsParsingFailure, "mount_point argument to %s() can't be empty",
339 name);
Stephen Smalley779701d2012-02-09 14:13:23 -0500340 goto done;
341 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500342
Doug Zongker3d177d02010-07-01 09:18:44 -0700343 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700344 mtd_scan_partitions();
345 const MtdPartition* mtd = mtd_find_partition_by_name(location);
346 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700347 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700348 name, location);
349 result = strdup("");
350 goto done;
351 }
352 MtdWriteContext* ctx = mtd_write_partition(mtd);
353 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700354 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700355 result = strdup("");
356 goto done;
357 }
358 if (mtd_erase_blocks(ctx, -1) == -1) {
359 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700360 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700361 result = strdup("");
362 goto done;
363 }
364 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700365 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700366 result = strdup("");
367 goto done;
368 }
369 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700370#ifdef USE_EXT4
371 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500372 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700373 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700374 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700375 name, status, location);
376 result = strdup("");
377 goto done;
378 }
379 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700380 } else if (strcmp(fs_type, "f2fs") == 0) {
381 char *num_sectors;
382 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
383 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
384 result = strdup("");
385 goto done;
386 }
387 const char *f2fs_path = "/sbin/mkfs.f2fs";
388 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
389 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
390 free(num_sectors);
391 if (status != 0) {
392 printf("%s: mkfs.f2fs failed (%d) on %s",
393 name, status, location);
394 result = strdup("");
395 goto done;
396 }
397 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700398#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700399 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700400 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700401 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700402 }
403
404done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700405 free(fs_type);
406 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700407 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800408 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700409}
410
Michael Rungece7ca712013-11-06 17:42:20 -0800411Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
412 char* result = NULL;
413 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700414 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Michael Rungece7ca712013-11-06 17:42:20 -0800415 }
416
417 char* src_name;
418 char* dst_name;
419
420 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
421 return NULL;
422 }
423 if (strlen(src_name) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700424 ErrorAbort(state, kArgsParsingFailure, "src_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800425 goto done;
426 }
427 if (strlen(dst_name) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700428 ErrorAbort(state, kArgsParsingFailure, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800429 goto done;
430 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700431 if (make_parents(dst_name) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700432 ErrorAbort(state, kFileRenameFailure, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700433 dst_name, strerror(errno));
Michael Runged5b17272014-10-22 14:28:23 -0700434 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
435 // File was already moved
436 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700437 } else if (rename(src_name, dst_name) != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700438 ErrorAbort(state, kFileRenameFailure, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800439 src_name, dst_name, strerror(errno));
440 } else {
441 result = dst_name;
442 }
443
444done:
445 free(src_name);
446 if (result != dst_name) free(dst_name);
447 return StringValue(result);
448}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700449
Doug Zongker512536a2010-02-17 16:11:44 -0800450Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Tao Baoba9a42a2015-06-23 23:23:33 -0700451 char** paths = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
452 for (int i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700453 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700454 if (paths[i] == NULL) {
Yabin Cui64be2132016-02-03 18:16:02 -0800455 for (int j = 0; j < i; ++j) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700456 free(paths[j]);
457 }
458 free(paths);
459 return NULL;
460 }
461 }
462
463 bool recursive = (strcmp(name, "delete_recursive") == 0);
464
465 int success = 0;
Tao Baoba9a42a2015-06-23 23:23:33 -0700466 for (int i = 0; i < argc; ++i) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700467 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
468 ++success;
469 free(paths[i]);
470 }
471 free(paths);
472
473 char buffer[10];
474 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800475 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700476}
477
Doug Zongker8edb00c2009-06-11 17:21:44 -0700478
Doug Zongker512536a2010-02-17 16:11:44 -0800479Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700480 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700481 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700482 }
483 char* frac_str;
484 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700485 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700486 return NULL;
487 }
488
489 double frac = strtod(frac_str, NULL);
Tao Baob15fd222015-09-24 11:10:51 -0700490 int sec;
491 android::base::ParseInt(sec_str, &sec);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700492
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700493 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700494 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
495
Doug Zongker9931f7f2009-06-10 14:11:53 -0700496 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800497 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700498}
499
Doug Zongker512536a2010-02-17 16:11:44 -0800500Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700501 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700502 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700503 }
504 char* frac_str;
505 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
506 return NULL;
507 }
508
509 double frac = strtod(frac_str, NULL);
510
511 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
512 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
513
Doug Zongker512536a2010-02-17 16:11:44 -0800514 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700515}
516
Doug Zongker8edb00c2009-06-11 17:21:44 -0700517// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800518Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700519 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700520 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700521 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700522 }
523 char* zip_path;
524 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700525 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700526
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700527 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700528
529 // To create a consistent system image, never use the clock for timestamps.
530 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
531
532 bool success = mzExtractRecursive(za, zip_path, dest_path,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000533 &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500534 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700535 free(zip_path);
536 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800537 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700538}
539
Doug Zongker8edb00c2009-06-11 17:21:44 -0700540
541// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800542// or
543// package_extract_file(package_path)
544// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800545// function (the char* returned is actually a FileContents*).
546Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700547 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700548 if (argc < 1 || argc > 2) {
Tianjie Xu16255832016-04-30 11:49:59 -0700549 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 or 2 args, got %d",
Doug Zongker6aece332010-02-01 14:40:12 -0800550 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700551 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700552 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700553
Doug Zongker6aece332010-02-01 14:40:12 -0800554 if (argc == 2) {
555 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700556
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800557 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700558
Doug Zongker6aece332010-02-01 14:40:12 -0800559 char* zip_path;
560 char* dest_path;
561 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
562
Doug Zongker6aece332010-02-01 14:40:12 -0800563 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
564 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700565 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800566 goto done2;
567 }
568
Tao Baoba9a42a2015-06-23 23:23:33 -0700569 {
Jed Estepf1fc48c2015-12-15 16:04:53 -0800570 int fd = TEMP_FAILURE_RETRY(ota_open(dest_path, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC,
Tao Baod3cac342015-12-14 15:53:25 -0800571 S_IRUSR | S_IWUSR));
572 if (fd == -1) {
573 printf("%s: can't open %s for write: %s\n", name, dest_path, strerror(errno));
Tao Baoba9a42a2015-06-23 23:23:33 -0700574 goto done2;
575 }
Tao Baod3cac342015-12-14 15:53:25 -0800576 success = mzExtractZipEntryToFile(za, entry, fd);
Jed Estepf1fc48c2015-12-15 16:04:53 -0800577 if (ota_fsync(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800578 printf("fsync of \"%s\" failed: %s\n", dest_path, strerror(errno));
579 success = false;
580 }
Jed Estepf1fc48c2015-12-15 16:04:53 -0800581 if (ota_close(fd) == -1) {
Tao Baod3cac342015-12-14 15:53:25 -0800582 printf("close of \"%s\" failed: %s\n", dest_path, strerror(errno));
583 success = false;
584 }
Doug Zongker6aece332010-02-01 14:40:12 -0800585 }
Doug Zongker6aece332010-02-01 14:40:12 -0800586
587 done2:
588 free(zip_path);
589 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800590 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800591 } else {
592 // The one-argument version returns the contents of the file
593 // as the result.
594
595 char* zip_path;
Yabin Cui64be2132016-02-03 18:16:02 -0800596 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
597
Tao Baoba9a42a2015-06-23 23:23:33 -0700598 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -0800599 v->type = VAL_BLOB;
600 v->size = -1;
601 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800602
Doug Zongker6aece332010-02-01 14:40:12 -0800603 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
604 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
605 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700606 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800607 goto done1;
608 }
609
Doug Zongker512536a2010-02-17 16:11:44 -0800610 v->size = mzGetZipEntryUncompLen(entry);
Tao Baoba9a42a2015-06-23 23:23:33 -0700611 v->data = reinterpret_cast<char*>(malloc(v->size));
Doug Zongker512536a2010-02-17 16:11:44 -0800612 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700613 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800614 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800615 goto done1;
616 }
617
Doug Zongker512536a2010-02-17 16:11:44 -0800618 success = mzExtractZipEntryToBuffer(za, entry,
619 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800620
621 done1:
622 free(zip_path);
623 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800624 free(v->data);
625 v->data = NULL;
626 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800627 }
Doug Zongker512536a2010-02-17 16:11:44 -0800628 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700629 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700630}
631
Doug Zongkera23075f2012-08-06 16:19:09 -0700632// Create all parent directories of name, if necessary.
633static int make_parents(char* name) {
634 char* p;
635 for (p = name + (strlen(name)-1); p > name; --p) {
636 if (*p != '/') continue;
637 *p = '\0';
638 if (make_parents(name) < 0) return -1;
639 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700640 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700641 *p = '/';
642 if (result == 0 || errno == EEXIST) {
643 // successfully created or already existed; we're done
644 return 0;
645 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700646 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700647 return -1;
648 }
649 }
650 return 0;
651}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700652
Doug Zongker9931f7f2009-06-10 14:11:53 -0700653// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700654// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800655Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700656 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700657 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700658 }
659 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700660 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700661 if (target == NULL) return NULL;
662
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700663 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700664 if (srcs == NULL) {
665 free(target);
666 return NULL;
667 }
668
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700669 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700670 int i;
671 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700672 if (unlink(srcs[i]) < 0) {
673 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700674 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700675 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700676 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700677 }
678 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700679 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700680 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700681 name, srcs[i], target);
682 ++bad;
683 }
Doug Zongker60babf82009-09-18 15:11:24 -0700684 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700685 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700686 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700687 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700688 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700689 free(srcs[i]);
690 }
691 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700692 if (bad) {
Tianjie Xu16255832016-04-30 11:49:59 -0700693 return ErrorAbort(state, kSymlinkFailure, "%s: some symlinks failed", name);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700694 }
Doug Zongker512536a2010-02-17 16:11:44 -0800695 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700696}
697
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700698struct perm_parsed_args {
699 bool has_uid;
700 uid_t uid;
701 bool has_gid;
702 gid_t gid;
703 bool has_mode;
704 mode_t mode;
705 bool has_fmode;
706 mode_t fmode;
707 bool has_dmode;
708 mode_t dmode;
709 bool has_selabel;
710 char* selabel;
711 bool has_capabilities;
712 uint64_t capabilities;
713};
714
Michael Runge75480252014-10-22 19:48:41 -0700715static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700716 int i;
717 struct perm_parsed_args parsed;
718 int bad = 0;
719 static int max_warnings = 20;
720
721 memset(&parsed, 0, sizeof(parsed));
722
723 for (i = 1; i < argc; i += 2) {
724 if (strcmp("uid", args[i]) == 0) {
725 int64_t uid;
726 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
727 parsed.uid = uid;
728 parsed.has_uid = true;
729 } else {
Michael Runge75480252014-10-22 19:48:41 -0700730 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700731 bad++;
732 }
733 continue;
734 }
735 if (strcmp("gid", args[i]) == 0) {
736 int64_t gid;
737 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
738 parsed.gid = gid;
739 parsed.has_gid = true;
740 } else {
Michael Runge75480252014-10-22 19:48:41 -0700741 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700742 bad++;
743 }
744 continue;
745 }
746 if (strcmp("mode", args[i]) == 0) {
747 int32_t mode;
748 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
749 parsed.mode = mode;
750 parsed.has_mode = true;
751 } else {
Michael Runge75480252014-10-22 19:48:41 -0700752 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700753 bad++;
754 }
755 continue;
756 }
757 if (strcmp("dmode", args[i]) == 0) {
758 int32_t mode;
759 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
760 parsed.dmode = mode;
761 parsed.has_dmode = true;
762 } else {
Michael Runge75480252014-10-22 19:48:41 -0700763 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700764 bad++;
765 }
766 continue;
767 }
768 if (strcmp("fmode", args[i]) == 0) {
769 int32_t mode;
770 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
771 parsed.fmode = mode;
772 parsed.has_fmode = true;
773 } else {
Michael Runge75480252014-10-22 19:48:41 -0700774 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700775 bad++;
776 }
777 continue;
778 }
779 if (strcmp("capabilities", args[i]) == 0) {
780 int64_t capabilities;
781 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
782 parsed.capabilities = capabilities;
783 parsed.has_capabilities = true;
784 } else {
Michael Runge75480252014-10-22 19:48:41 -0700785 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700786 bad++;
787 }
788 continue;
789 }
790 if (strcmp("selabel", args[i]) == 0) {
791 if (args[i+1][0] != '\0') {
792 parsed.selabel = args[i+1];
793 parsed.has_selabel = true;
794 } else {
Michael Runge75480252014-10-22 19:48:41 -0700795 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700796 bad++;
797 }
798 continue;
799 }
800 if (max_warnings != 0) {
801 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
802 max_warnings--;
803 if (max_warnings == 0) {
804 printf("ParsedPermArgs: suppressing further warnings\n");
805 }
806 }
807 }
808 return parsed;
809}
810
811static int ApplyParsedPerms(
Michael Runge75480252014-10-22 19:48:41 -0700812 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700813 const char* filename,
814 const struct stat *statptr,
815 struct perm_parsed_args parsed)
816{
817 int bad = 0;
818
Nick Kralevich6a821fe2014-10-23 20:36:42 -0700819 if (parsed.has_selabel) {
820 if (lsetfilecon(filename, parsed.selabel) != 0) {
821 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
822 filename, parsed.selabel, strerror(errno));
823 bad++;
824 }
825 }
826
Nick Kraleviche4612512013-09-10 15:34:19 -0700827 /* ignore symlinks */
828 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich6a821fe2014-10-23 20:36:42 -0700829 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700830 }
831
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700832 if (parsed.has_uid) {
833 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700834 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
835 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700836 bad++;
837 }
838 }
839
840 if (parsed.has_gid) {
841 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700842 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
843 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700844 bad++;
845 }
846 }
847
848 if (parsed.has_mode) {
849 if (chmod(filename, parsed.mode) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700850 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
851 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700852 bad++;
853 }
854 }
855
856 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
857 if (chmod(filename, parsed.dmode) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700858 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
859 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700860 bad++;
861 }
862 }
863
864 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
865 if (chmod(filename, parsed.fmode) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700866 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700867 filename, parsed.fmode, strerror(errno));
868 bad++;
869 }
870 }
871
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700872 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
873 if (parsed.capabilities == 0) {
874 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
875 // Report failure unless it's ENODATA (attribute not set)
Michael Runge75480252014-10-22 19:48:41 -0700876 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700877 filename, parsed.capabilities, strerror(errno));
878 bad++;
879 }
880 } else {
881 struct vfs_cap_data cap_data;
882 memset(&cap_data, 0, sizeof(cap_data));
883 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
884 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
885 cap_data.data[0].inheritable = 0;
886 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
887 cap_data.data[1].inheritable = 0;
888 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700889 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
890 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700891 bad++;
892 }
893 }
894 }
895
896 return bad;
897}
898
899// nftw doesn't allow us to pass along context, so we need to use
900// global variables. *sigh*
901static struct perm_parsed_args recursive_parsed_args;
Michael Runge75480252014-10-22 19:48:41 -0700902static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700903
904static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
905 int fileflags, struct FTW *pfwt) {
Michael Runge75480252014-10-22 19:48:41 -0700906 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700907}
908
909static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700910 int bad = 0;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700911 struct stat sb;
912 Value* result = NULL;
913
914 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
915
916 if ((argc % 2) != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700917 return ErrorAbort(state, kArgsParsingFailure,
918 "%s() expects an odd number of arguments, got %d", name, argc);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700919 }
920
921 char** args = ReadVarArgs(state, argc, argv);
922 if (args == NULL) return NULL;
923
924 if (lstat(args[0], &sb) == -1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700925 result = ErrorAbort(state, kSetMetadataFailure, "%s: Error on lstat of \"%s\": %s",
926 name, args[0], strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700927 goto done;
928 }
929
Tao Baoba9a42a2015-06-23 23:23:33 -0700930 {
931 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700932
Tao Baoba9a42a2015-06-23 23:23:33 -0700933 if (recursive) {
934 recursive_parsed_args = parsed;
935 recursive_state = state;
936 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
937 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
938 recursive_state = NULL;
939 } else {
940 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
941 }
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700942 }
943
944done:
Tao Baoba9a42a2015-06-23 23:23:33 -0700945 for (int i = 0; i < argc; ++i) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700946 free(args[i]);
947 }
948 free(args);
949
950 if (result != NULL) {
951 return result;
952 }
953
954 if (bad > 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700955 return ErrorAbort(state, kSetMetadataFailure, "%s: some changes failed", name);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700956 }
957
958 return StringValue(strdup(""));
959}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700960
Doug Zongker512536a2010-02-17 16:11:44 -0800961Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700962 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -0700963 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700964 }
Tao Baoba9a42a2015-06-23 23:23:33 -0700965 char* key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700966 if (key == NULL) return NULL;
967
968 char value[PROPERTY_VALUE_MAX];
969 property_get(key, value, "");
970 free(key);
971
Doug Zongker512536a2010-02-17 16:11:44 -0800972 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700973}
974
Doug Zongker47cace92009-06-18 10:11:50 -0700975// file_getprop(file, key)
976//
977// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700978// per line. # comment lines,blank lines, lines without '=' ignored),
979// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800980Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700981 char* result = NULL;
982 char* buffer = NULL;
983 char* filename;
984 char* key;
985 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
986 return NULL;
987 }
988
989 struct stat st;
990 if (stat(filename, &st) < 0) {
Tianjie Xu16255832016-04-30 11:49:59 -0700991 ErrorAbort(state, kFileGetPropFailure, "%s: failed to stat \"%s\": %s", name, filename,
992 strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -0700993 goto done;
994 }
995
996#define MAX_FILE_GETPROP_SIZE 65536
997
998 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
Tianjie Xu16255832016-04-30 11:49:59 -0700999 ErrorAbort(state, kFileGetPropFailure, "%s too large for %s (max %d)", filename, name,
1000 MAX_FILE_GETPROP_SIZE);
Doug Zongker47cace92009-06-18 10:11:50 -07001001 goto done;
1002 }
1003
Tao Baoba9a42a2015-06-23 23:23:33 -07001004 buffer = reinterpret_cast<char*>(malloc(st.st_size+1));
Doug Zongker47cace92009-06-18 10:11:50 -07001005 if (buffer == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -07001006 ErrorAbort(state, kFileGetPropFailure, "%s: failed to alloc %lld bytes", name,
1007 (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -07001008 goto done;
1009 }
1010
Tao Baoba9a42a2015-06-23 23:23:33 -07001011 FILE* f;
1012 f = fopen(filename, "rb");
Doug Zongker47cace92009-06-18 10:11:50 -07001013 if (f == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -07001014 ErrorAbort(state, kFileOpenFailure, "%s: failed to open %s: %s", name, filename,
1015 strerror(errno));
Doug Zongker47cace92009-06-18 10:11:50 -07001016 goto done;
1017 }
1018
Jed Estepf1fc48c2015-12-15 16:04:53 -08001019 if (ota_fread(buffer, 1, st.st_size, f) != static_cast<size_t>(st.st_size)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001020 ErrorAbort(state, kFreadFailure, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -07001021 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -07001022 fclose(f);
1023 goto done;
1024 }
1025 buffer[st.st_size] = '\0';
1026
1027 fclose(f);
1028
Tao Baoba9a42a2015-06-23 23:23:33 -07001029 char* line;
1030 line = strtok(buffer, "\n");
Doug Zongker47cace92009-06-18 10:11:50 -07001031 do {
1032 // skip whitespace at start of line
1033 while (*line && isspace(*line)) ++line;
1034
1035 // comment or blank line: skip to next line
1036 if (*line == '\0' || *line == '#') continue;
1037
1038 char* equal = strchr(line, '=');
1039 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -07001040 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001041 }
1042
1043 // trim whitespace between key and '='
1044 char* key_end = equal-1;
1045 while (key_end > line && isspace(*key_end)) --key_end;
1046 key_end[1] = '\0';
1047
1048 // not the key we're looking for
1049 if (strcmp(key, line) != 0) continue;
1050
1051 // skip whitespace after the '=' to the start of the value
1052 char* val_start = equal+1;
1053 while(*val_start && isspace(*val_start)) ++val_start;
1054
1055 // trim trailing whitespace
1056 char* val_end = val_start + strlen(val_start)-1;
1057 while (val_end > val_start && isspace(*val_end)) --val_end;
1058 val_end[1] = '\0';
1059
1060 result = strdup(val_start);
1061 break;
1062
1063 } while ((line = strtok(NULL, "\n")));
1064
1065 if (result == NULL) result = strdup("");
1066
1067 done:
1068 free(filename);
1069 free(key);
1070 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001071 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001072}
1073
Doug Zongker179b2d92011-04-12 15:49:04 -07001074// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001075Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001076 char* result = NULL;
1077
Doug Zongker179b2d92011-04-12 15:49:04 -07001078 Value* partition_value;
1079 Value* contents;
1080 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001081 return NULL;
1082 }
1083
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001084 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001085 if (partition_value->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001086 ErrorAbort(state, kArgsParsingFailure, "partition argument to %s must be string", name);
Doug Zongker179b2d92011-04-12 15:49:04 -07001087 goto done;
1088 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001089 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001090 if (strlen(partition) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001091 ErrorAbort(state, kArgsParsingFailure, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001092 goto done;
1093 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001094 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001095 ErrorAbort(state, kArgsParsingFailure, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001096 goto done;
1097 }
1098
1099 mtd_scan_partitions();
Tao Baoba9a42a2015-06-23 23:23:33 -07001100 const MtdPartition* mtd;
1101 mtd = mtd_find_partition_by_name(partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001102 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001103 printf("%s: no mtd partition named \"%s\"\n", name, partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001104 result = strdup("");
1105 goto done;
1106 }
1107
Tao Baoba9a42a2015-06-23 23:23:33 -07001108 MtdWriteContext* ctx;
1109 ctx = mtd_write_partition(mtd);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001110 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001111 printf("%s: can't write mtd partition \"%s\"\n",
Doug Zongker8edb00c2009-06-11 17:21:44 -07001112 name, partition);
1113 result = strdup("");
1114 goto done;
1115 }
1116
1117 bool success;
1118
Doug Zongker179b2d92011-04-12 15:49:04 -07001119 if (contents->type == VAL_STRING) {
1120 // we're given a filename as the contents
1121 char* filename = contents->data;
Jed Estepf1fc48c2015-12-15 16:04:53 -08001122 FILE* f = ota_fopen(filename, "rb");
Doug Zongker179b2d92011-04-12 15:49:04 -07001123 if (f == NULL) {
Tao Baoba9a42a2015-06-23 23:23:33 -07001124 printf("%s: can't open %s: %s\n", name, filename, strerror(errno));
Doug Zongker179b2d92011-04-12 15:49:04 -07001125 result = strdup("");
1126 goto done;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001127 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001128
1129 success = true;
Tao Baoba9a42a2015-06-23 23:23:33 -07001130 char* buffer = reinterpret_cast<char*>(malloc(BUFSIZ));
Doug Zongker179b2d92011-04-12 15:49:04 -07001131 int read;
Jed Estepf1fc48c2015-12-15 16:04:53 -08001132 while (success && (read = ota_fread(buffer, 1, BUFSIZ, f)) > 0) {
Doug Zongker179b2d92011-04-12 15:49:04 -07001133 int wrote = mtd_write_data(ctx, buffer, read);
1134 success = success && (wrote == read);
1135 }
1136 free(buffer);
Jed Estepf1fc48c2015-12-15 16:04:53 -08001137 ota_fclose(f);
Doug Zongker179b2d92011-04-12 15:49:04 -07001138 } else {
1139 // we're given a blob as the contents
1140 ssize_t wrote = mtd_write_data(ctx, contents->data, contents->size);
1141 success = (wrote == contents->size);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001142 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001143 if (!success) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001144 printf("mtd_write_data to %s failed: %s\n",
Doug Zongker179b2d92011-04-12 15:49:04 -07001145 partition, strerror(errno));
1146 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001147
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001148 if (mtd_erase_blocks(ctx, -1) == -1) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001149 printf("%s: error erasing blocks of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001150 }
1151 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001152 printf("%s: error closing write of %s\n", name, partition);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001153 }
1154
Doug Zongker179b2d92011-04-12 15:49:04 -07001155 printf("%s %s partition\n",
1156 success ? "wrote" : "failed to write", partition);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001157
1158 result = success ? partition : strdup("");
1159
1160done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001161 if (result != partition) FreeValue(partition_value);
1162 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001163 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001164}
1165
Doug Zongker8edb00c2009-06-11 17:21:44 -07001166// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001167Value* ApplyPatchSpaceFn(const char* name, State* state,
1168 int argc, Expr* argv[]) {
1169 char* bytes_str;
1170 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1171 return NULL;
1172 }
1173
Tao Baob15fd222015-09-24 11:10:51 -07001174 size_t bytes;
1175 if (!android::base::ParseUint(bytes_str, &bytes)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001176 ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count\n\n",
Doug Zongkerc4351c72010-02-22 14:46:32 -08001177 name, bytes_str);
1178 free(bytes_str);
Tao Baob15fd222015-09-24 11:10:51 -07001179 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001180 }
1181
1182 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1183}
1184
Doug Zongker52b40362014-02-10 15:30:30 -08001185// apply_patch(file, size, init_sha1, tgt_sha1, patch)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001186
Doug Zongker512536a2010-02-17 16:11:44 -08001187Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001188 if (argc < 6 || (argc % 2) == 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001189 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 6 args and an "
1190 "even number, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001191 }
1192
Doug Zongkerc4351c72010-02-22 14:46:32 -08001193 char* source_filename;
1194 char* target_filename;
1195 char* target_sha1;
1196 char* target_size_str;
1197 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1198 &target_sha1, &target_size_str) < 0) {
1199 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001200 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001201
Tao Baob15fd222015-09-24 11:10:51 -07001202 size_t target_size;
1203 if (!android::base::ParseUint(target_size_str, &target_size)) {
Tianjie Xu16255832016-04-30 11:49:59 -07001204 ErrorAbort(state, kArgsParsingFailure, "%s(): can't parse \"%s\" as byte count",
Doug Zongkerc4351c72010-02-22 14:46:32 -08001205 name, target_size_str);
1206 free(source_filename);
1207 free(target_filename);
1208 free(target_sha1);
1209 free(target_size_str);
Tao Baob15fd222015-09-24 11:10:51 -07001210 return nullptr;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001211 }
1212
1213 int patchcount = (argc-4) / 2;
Yabin Cui64be2132016-02-03 18:16:02 -08001214 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc-4, argv+4),
1215 free);
1216 if (!arg_values) {
1217 return nullptr;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001218 }
Yabin Cui64be2132016-02-03 18:16:02 -08001219 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patch_shas;
1220 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> patches;
1221 // Protect values by unique_ptrs first to get rid of memory leak.
1222 for (int i = 0; i < patchcount * 2; i += 2) {
1223 patch_shas.emplace_back(arg_values.get()[i], FreeValue);
1224 patches.emplace_back(arg_values.get()[i+1], FreeValue);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001225 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001226
Yabin Cui64be2132016-02-03 18:16:02 -08001227 for (int i = 0; i < patchcount; ++i) {
1228 if (patch_shas[i]->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -07001229 ErrorAbort(state, kArgsParsingFailure, "%s(): sha-1 #%d is not string", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001230 return nullptr;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001231 }
Yabin Cui64be2132016-02-03 18:16:02 -08001232 if (patches[i]->type != VAL_BLOB) {
Tianjie Xu16255832016-04-30 11:49:59 -07001233 ErrorAbort(state, kArgsParsingFailure, "%s(): patch #%d is not blob", name, i);
Yabin Cui64be2132016-02-03 18:16:02 -08001234 return nullptr;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001235 }
1236 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001237
Yabin Cui64be2132016-02-03 18:16:02 -08001238 std::vector<char*> patch_sha_str;
1239 std::vector<Value*> patch_ptrs;
1240 for (int i = 0; i < patchcount; ++i) {
1241 patch_sha_str.push_back(patch_shas[i]->data);
1242 patch_ptrs.push_back(patches[i].get());
Doug Zongker8edb00c2009-06-11 17:21:44 -07001243 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001244
1245 int result = applypatch(source_filename, target_filename,
1246 target_sha1, target_size,
Yabin Cui64be2132016-02-03 18:16:02 -08001247 patchcount, patch_sha_str.data(), patch_ptrs.data(), NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001248
1249 return StringValue(strdup(result == 0 ? "t" : ""));
1250}
1251
1252// apply_patch_check(file, [sha1_1, ...])
1253Value* ApplyPatchCheckFn(const char* name, State* state,
1254 int argc, Expr* argv[]) {
1255 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001256 return ErrorAbort(state, kArgsParsingFailure, "%s(): expected at least 1 arg, got %d",
Doug Zongkerc4351c72010-02-22 14:46:32 -08001257 name, argc);
1258 }
1259
1260 char* filename;
1261 if (ReadArgs(state, argv, 1, &filename) < 0) {
1262 return NULL;
1263 }
1264
1265 int patchcount = argc-1;
1266 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1267
1268 int result = applypatch_check(filename, patchcount, sha1s);
1269
1270 int i;
1271 for (i = 0; i < patchcount; ++i) {
1272 free(sha1s[i]);
1273 }
1274 free(sha1s);
1275
1276 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001277}
1278
Tao Bao1107d962015-09-09 17:16:55 -07001279// This is the updater side handler for ui_print() in edify script. Contents
1280// will be sent over to the recovery side for on-screen display.
Doug Zongker512536a2010-02-17 16:11:44 -08001281Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001282 char** args = ReadVarArgs(state, argc, argv);
1283 if (args == NULL) {
1284 return NULL;
1285 }
1286
Tao Bao1107d962015-09-09 17:16:55 -07001287 std::string buffer;
1288 for (int i = 0; i < argc; ++i) {
1289 buffer += args[i];
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001290 free(args[i]);
1291 }
1292 free(args);
Tao Bao1107d962015-09-09 17:16:55 -07001293
1294 buffer += "\n";
Michael Runge75480252014-10-22 19:48:41 -07001295 uiPrint(state, buffer);
Tao Bao1107d962015-09-09 17:16:55 -07001296 return StringValue(strdup(buffer.c_str()));
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001297}
1298
Doug Zongkerd0181b82011-10-19 10:51:12 -07001299Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1300 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001301 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001302 }
1303 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1304 return StringValue(strdup("t"));
1305}
1306
Doug Zongker512536a2010-02-17 16:11:44 -08001307Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001308 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001309 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001310 }
1311 char** args = ReadVarArgs(state, argc, argv);
1312 if (args == NULL) {
1313 return NULL;
1314 }
1315
Tao Baoba9a42a2015-06-23 23:23:33 -07001316 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001317 memcpy(args2, args, sizeof(char*) * argc);
1318 args2[argc] = NULL;
1319
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001320 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001321
1322 pid_t child = fork();
1323 if (child == 0) {
1324 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001325 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001326 _exit(1);
1327 }
1328 int status;
1329 waitpid(child, &status, 0);
1330 if (WIFEXITED(status)) {
1331 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001332 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001333 WEXITSTATUS(status));
1334 }
1335 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001336 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001337 WTERMSIG(status));
1338 }
1339
1340 int i;
1341 for (i = 0; i < argc; ++i) {
1342 free(args[i]);
1343 }
1344 free(args);
1345 free(args2);
1346
1347 char buffer[20];
1348 sprintf(buffer, "%d", status);
1349
Doug Zongker512536a2010-02-17 16:11:44 -08001350 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001351}
1352
Doug Zongker512536a2010-02-17 16:11:44 -08001353// sha1_check(data)
1354// to return the sha1 of the data (given in the format returned by
1355// read_file).
1356//
1357// sha1_check(data, sha1_hex, [sha1_hex, ...])
1358// returns the sha1 of the file if it matches any of the hex
1359// strings passed, or "" if it does not equal any of them.
1360//
1361Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1362 if (argc < 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001363 return ErrorAbort(state, kArgsParsingFailure, "%s() expects at least 1 arg", name);
Doug Zongker512536a2010-02-17 16:11:44 -08001364 }
1365
Yabin Cui64be2132016-02-03 18:16:02 -08001366 std::unique_ptr<Value*, decltype(&free)> arg_values(ReadValueVarArgs(state, argc, argv), free);
1367 if (arg_values == nullptr) {
1368 return nullptr;
1369 }
1370 std::vector<std::unique_ptr<Value, decltype(&FreeValue)>> args;
1371 for (int i = 0; i < argc; ++i) {
1372 args.emplace_back(arg_values.get()[i], FreeValue);
Doug Zongker512536a2010-02-17 16:11:44 -08001373 }
1374
1375 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001376 return StringValue(strdup(""));
1377 }
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001378 uint8_t digest[SHA_DIGEST_LENGTH];
1379 SHA1(reinterpret_cast<uint8_t*>(args[0]->data), args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001380
1381 if (argc == 1) {
1382 return StringValue(PrintSha1(digest));
1383 }
1384
1385 int i;
Yabin Cui64be2132016-02-03 18:16:02 -08001386 uint8_t arg_digest[SHA_DIGEST_LENGTH];
Doug Zongker512536a2010-02-17 16:11:44 -08001387 for (i = 1; i < argc; ++i) {
1388 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001389 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001390 name, i);
1391 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1392 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001393 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1394 name, args[i]->data);
Sen Jiangc48cb5e2016-02-04 16:23:21 +08001395 } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001396 break;
1397 }
Doug Zongker512536a2010-02-17 16:11:44 -08001398 }
1399 if (i >= argc) {
1400 // Didn't match any of the hex strings; return false.
1401 return StringValue(strdup(""));
1402 }
Yabin Cui64be2132016-02-03 18:16:02 -08001403 // Found a match.
1404 return args[i].release();
Doug Zongker512536a2010-02-17 16:11:44 -08001405}
1406
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001407// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001408// is actually a FileContents*).
1409Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1410 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001411 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker512536a2010-02-17 16:11:44 -08001412 }
1413 char* filename;
1414 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1415
Yabin Cui1c522df2016-02-10 16:41:10 -08001416 Value* v = static_cast<Value*>(malloc(sizeof(Value)));
1417 if (v == nullptr) {
1418 return nullptr;
1419 }
Doug Zongker512536a2010-02-17 16:11:44 -08001420 v->type = VAL_BLOB;
Yabin Cui1c522df2016-02-10 16:41:10 -08001421 v->size = -1;
1422 v->data = nullptr;
Doug Zongker512536a2010-02-17 16:11:44 -08001423
1424 FileContents fc;
Tao Baoccb0ba92016-06-11 03:56:38 -07001425 if (LoadFileContents(filename, &fc) == 0) {
Yabin Cui1c522df2016-02-10 16:41:10 -08001426 v->data = static_cast<char*>(malloc(fc.data.size()));
1427 if (v->data != nullptr) {
1428 memcpy(v->data, fc.data.data(), fc.data.size());
1429 v->size = fc.data.size();
1430 }
Doug Zongker512536a2010-02-17 16:11:44 -08001431 }
Doug Zongker512536a2010-02-17 16:11:44 -08001432 free(filename);
1433 return v;
1434}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001435
Tao Bao1bf17722016-11-03 23:52:01 -07001436// write_value(value, filename)
1437// Writes 'value' to 'filename'.
1438// Example: write_value("960000", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
1439Value* WriteValueFn(const char* name, State* state, int argc, Expr* argv[]) {
1440 if (argc != 2) {
1441 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
1442 }
1443
1444 char* value;
1445 char* filename;
1446 if (ReadArgs(state, argv, 2, &value, &filename) < 0) {
1447 return ErrorAbort(state, kArgsParsingFailure, "%s(): Failed to parse the argument(s)",
1448 name);
1449 }
1450
1451 bool ret = android::base::WriteStringToFile(value, filename);
1452 if (!ret) {
1453 printf("%s: Failed to write to \"%s\": %s\n", name, filename, strerror(errno));
1454 }
1455
1456 free(value);
1457 free(filename);
1458 return StringValue(strdup(ret ? "t" : ""));
1459}
1460
Doug Zongkerc87bab12013-11-25 13:53:25 -08001461// Immediately reboot the device. Recovery is not finished normally,
1462// so if you reboot into recovery it will re-start applying the
1463// current package (because nothing has cleared the copy of the
1464// arguments stored in the BCB).
1465//
1466// The argument is the partition name passed to the android reboot
1467// property. It can be "recovery" to boot from the recovery
1468// partition, or "" (empty string) to boot from the regular boot
1469// partition.
1470Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1471 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001472 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001473 }
1474
1475 char* filename;
1476 char* property;
1477 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1478
1479 char buffer[80];
1480
1481 // zero out the 'command' field of the bootloader message.
1482 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1483 FILE* f = fopen(filename, "r+b");
1484 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
Matt Mower8df31912014-07-09 11:50:05 -05001485 fseek(f, BOOTLOADER_MESSAGE_OFFSET_IN_MISC, SEEK_CUR);
Jed Estepf1fc48c2015-12-15 16:04:53 -08001486 ota_fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001487 fclose(f);
1488 free(filename);
1489
1490 strcpy(buffer, "reboot,");
1491 if (property != NULL) {
1492 strncat(buffer, property, sizeof(buffer)-10);
1493 }
1494
1495 property_set(ANDROID_RB_PROPERTY, buffer);
1496
1497 sleep(5);
1498 free(property);
Tianjie Xu16255832016-04-30 11:49:59 -07001499 ErrorAbort(state, kRebootFailure, "%s() failed to reboot", name);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001500 return NULL;
1501}
1502
1503// Store a string value somewhere that future invocations of recovery
1504// can access it. This value is called the "stage" and can be used to
1505// drive packages that need to do reboots in the middle of
1506// installation and keep track of where they are in the multi-stage
1507// install.
1508//
1509// The first argument is the block device for the misc partition
1510// ("/misc" in the fstab), which is where this value is stored. The
1511// second argument is the string to store; it should not exceed 31
1512// bytes.
1513Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1514 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001515 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001516 }
1517
1518 char* filename;
1519 char* stagestr;
1520 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1521
1522 // Store this value in the misc partition, immediately after the
1523 // bootloader message that the main recovery uses to save its
1524 // arguments in case of the device restarting midway through
1525 // package installation.
1526 FILE* f = fopen(filename, "r+b");
1527 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Matt Mower8df31912014-07-09 11:50:05 -05001528 fseek(f, BOOTLOADER_MESSAGE_OFFSET_IN_MISC, SEEK_CUR);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001529 int to_write = strlen(stagestr)+1;
1530 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1531 if (to_write > max_size) {
1532 to_write = max_size;
1533 stagestr[max_size-1] = 0;
1534 }
Jed Estepf1fc48c2015-12-15 16:04:53 -08001535 ota_fwrite(stagestr, to_write, 1, f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001536 fclose(f);
1537
1538 free(stagestr);
1539 return StringValue(filename);
1540}
1541
1542// Return the value most recently saved with SetStageFn. The argument
1543// is the block device for the misc partition.
1544Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001545 if (argc != 1) {
Tianjie Xu16255832016-04-30 11:49:59 -07001546 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 1 arg, got %d", name, argc);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001547 }
1548
1549 char* filename;
1550 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1551
1552 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1553 FILE* f = fopen(filename, "rb");
1554 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
Matt Mower8df31912014-07-09 11:50:05 -05001555 fseek(f, BOOTLOADER_MESSAGE_OFFSET_IN_MISC, SEEK_CUR);
Jed Estepf1fc48c2015-12-15 16:04:53 -08001556 ota_fread(buffer, sizeof(buffer), 1, f);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001557 fclose(f);
1558 buffer[sizeof(buffer)-1] = '\0';
1559
1560 return StringValue(strdup(buffer));
1561}
1562
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001563Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1564 if (argc != 2) {
Tianjie Xu16255832016-04-30 11:49:59 -07001565 return ErrorAbort(state, kArgsParsingFailure, "%s() expects 2 args, got %d", name, argc);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001566 }
1567
1568 char* filename;
1569 char* len_str;
1570 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1571
Tao Baob15fd222015-09-24 11:10:51 -07001572 size_t len;
1573 android::base::ParseUint(len_str, &len);
Jed Estepf1fc48c2015-12-15 16:04:53 -08001574 int fd = ota_open(filename, O_WRONLY, 0644);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001575 int success = wipe_block_device(fd, len);
1576
1577 free(filename);
1578 free(len_str);
1579
Jed Estepf1fc48c2015-12-15 16:04:53 -08001580 ota_close(fd);
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001581
1582 return StringValue(strdup(success ? "t" : ""));
1583}
1584
Doug Zongkerc704e062014-05-23 08:40:35 -07001585Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1586 if (argc != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001587 return ErrorAbort(state, kArgsParsingFailure, "%s() expects no args, got %d", name, argc);
Doug Zongkerc704e062014-05-23 08:40:35 -07001588 }
1589 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1590 fprintf(ui->cmd_pipe, "enable_reboot\n");
1591 return StringValue(strdup("t"));
1592}
1593
Michael Rungeb278c252014-11-21 00:12:28 -08001594Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001595#ifdef HAVE_LIBTUNE2FS
Michael Rungeb278c252014-11-21 00:12:28 -08001596 if (argc == 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001597 return ErrorAbort(state, kArgsParsingFailure, "%s() expects args, got %d", name, argc);
Michael Rungeb278c252014-11-21 00:12:28 -08001598 }
1599
1600 char** args = ReadVarArgs(state, argc, argv);
1601 if (args == NULL) {
Tianjie Xu16255832016-04-30 11:49:59 -07001602 return ErrorAbort(state, kArgsParsingFailure, "%s() could not read args", name);
Michael Rungeb278c252014-11-21 00:12:28 -08001603 }
1604
Tao Baoba9a42a2015-06-23 23:23:33 -07001605 char** args2 = reinterpret_cast<char**>(malloc(sizeof(char*) * (argc+1)));
Michael Rungeb278c252014-11-21 00:12:28 -08001606 // Tune2fs expects the program name as its args[0]
1607 args2[0] = strdup(name);
Tao Baoba9a42a2015-06-23 23:23:33 -07001608 for (int i = 0; i < argc; ++i) {
Michael Rungeb278c252014-11-21 00:12:28 -08001609 args2[i + 1] = args[i];
1610 }
1611 int result = tune2fs_main(argc + 1, args2);
Tao Baoba9a42a2015-06-23 23:23:33 -07001612 for (int i = 0; i < argc; ++i) {
Michael Rungeb278c252014-11-21 00:12:28 -08001613 free(args[i]);
1614 }
1615 free(args);
1616
1617 free(args2[0]);
1618 free(args2);
1619 if (result != 0) {
Tianjie Xu16255832016-04-30 11:49:59 -07001620 return ErrorAbort(state, kTune2FsFailure, "%s() returned error code %d",
1621 name, result);
Michael Rungeb278c252014-11-21 00:12:28 -08001622 }
1623 return StringValue(strdup("t"));
Ethan Yonker7e1b9862015-03-19 14:10:01 -05001624#else
1625 return ErrorAbort(state, "%s() support not present, no libtune2fs", name);
1626#endif // HAVE_LIBTUNE2FS
Michael Rungeb278c252014-11-21 00:12:28 -08001627}
1628
Doug Zongker9931f7f2009-06-10 14:11:53 -07001629void RegisterInstallFunctions() {
1630 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001631 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001632 RegisterFunction("unmount", UnmountFn);
1633 RegisterFunction("format", FormatFn);
1634 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001635 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001636 RegisterFunction("delete", DeleteFn);
1637 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001638 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1639 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001640 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001641
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001642 // Usage:
1643 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1644 // Example:
1645 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1646 RegisterFunction("set_metadata", SetMetadataFn);
1647
1648 // Usage:
1649 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1650 // Example:
1651 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1652 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1653
Doug Zongker8edb00c2009-06-11 17:21:44 -07001654 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001655 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001656 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001657
1658 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001659 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1660 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001661
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001662 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001663
Doug Zongker512536a2010-02-17 16:11:44 -08001664 RegisterFunction("read_file", ReadFileFn);
1665 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001666 RegisterFunction("rename", RenameFn);
Tao Bao1bf17722016-11-03 23:52:01 -07001667 RegisterFunction("write_value", WriteValueFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001668
Doug Zongkerd0181b82011-10-19 10:51:12 -07001669 RegisterFunction("wipe_cache", WipeCacheFn);
1670
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001671 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001672
1673 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001674
1675 RegisterFunction("reboot_now", RebootNowFn);
1676 RegisterFunction("get_stage", GetStageFn);
1677 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001678
1679 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeb278c252014-11-21 00:12:28 -08001680 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001681}