blob: 20ee431de03334948d168374306d9608923eb2de [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
Doug Zongkerc87bab12013-11-25 13:53:25 -080037#include "bootloader.h"
38#include "applypatch/applypatch.h"
39#include "cutils/android_reboot.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070040#include "cutils/misc.h"
41#include "cutils/properties.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070042#include "edify/expr.h"
Doug Zongker512536a2010-02-17 16:11:44 -080043#include "mincrypt/sha.h"
Doug Zongker9931f7f2009-06-10 14:11:53 -070044#include "minzip/DirUtil.h"
45#include "mtdutils/mounts.h"
46#include "mtdutils/mtdutils.h"
47#include "updater.h"
Doug Zongker512536a2010-02-17 16:11:44 -080048#include "applypatch/applypatch.h"
Dees_Troy512376c2013-09-03 19:39:41 +000049#include "flashutils/flashutils.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080050#include "install.h"
Michael Rungeb278c252014-11-21 00:12:28 -080051#include "tune2fs.h"
Doug Zongker8edb00c2009-06-11 17:21:44 -070052
Doug Zongker3d177d02010-07-01 09:18:44 -070053#ifdef USE_EXT4
54#include "make_ext4fs.h"
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -080055#include "wipe.h"
Doug Zongker3d177d02010-07-01 09:18:44 -070056#endif
57
Michael Runge75480252014-10-22 19:48:41 -070058void uiPrint(State* state, char* buffer) {
59 char* line = strtok(buffer, "\n");
60 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
61 while (line) {
62 fprintf(ui->cmd_pipe, "ui_print %s\n", line);
63 line = strtok(NULL, "\n");
64 }
65 fprintf(ui->cmd_pipe, "ui_print\n");
66}
67
68__attribute__((__format__(printf, 2, 3))) __nonnull((2))
69void uiPrintf(State* state, const char* format, ...) {
70 char error_msg[1024];
71 va_list ap;
72 va_start(ap, format);
73 vsnprintf(error_msg, sizeof(error_msg), format, ap);
74 va_end(ap);
75 uiPrint(state, error_msg);
76}
77
Doug Zongker52b40362014-02-10 15:30:30 -080078// Take a sha-1 digest and return it as a newly-allocated hex string.
Doug Zongkerbc7ffed2014-08-15 14:31:52 -070079char* PrintSha1(const uint8_t* digest) {
Doug Zongker52b40362014-02-10 15:30:30 -080080 char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1);
81 int i;
82 const char* alphabet = "0123456789abcdef";
83 for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
84 buffer[i*2] = alphabet[(digest[i] >> 4) & 0xf];
85 buffer[i*2+1] = alphabet[digest[i] & 0xf];
86 }
87 buffer[i*2] = '\0';
88 return buffer;
89}
90
Doug Zongker3d177d02010-07-01 09:18:44 -070091// mount(fs_type, partition_type, location, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -070092//
Doug Zongker3d177d02010-07-01 09:18:44 -070093// fs_type="yaffs2" partition_type="MTD" location=partition
94// fs_type="ext4" partition_type="EMMC" location=device
Doug Zongker512536a2010-02-17 16:11:44 -080095Value* MountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -070096 char* result = NULL;
Michael Rungebd6138c2014-10-22 17:05:08 -070097 if (argc != 4 && argc != 5) {
98 return ErrorAbort(state, "%s() expects 4-5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -070099 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700100 char* fs_type;
101 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700102 char* location;
103 char* mount_point;
Michael Rungebd6138c2014-10-22 17:05:08 -0700104 char* mount_options;
105 bool has_mount_options;
106 if (argc == 5) {
107 has_mount_options = true;
108 if (ReadArgs(state, argv, 5, &fs_type, &partition_type,
109 &location, &mount_point, &mount_options) < 0) {
110 return NULL;
111 }
112 } else {
113 has_mount_options = false;
114 if (ReadArgs(state, argv, 4, &fs_type, &partition_type,
Doug Zongker3d177d02010-07-01 09:18:44 -0700115 &location, &mount_point) < 0) {
Michael Rungebd6138c2014-10-22 17:05:08 -0700116 return NULL;
117 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700118 }
119
Doug Zongker3d177d02010-07-01 09:18:44 -0700120 if (strlen(fs_type) == 0) {
121 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
122 goto done;
123 }
124 if (strlen(partition_type) == 0) {
125 ErrorAbort(state, "partition_type argument to %s() can't be empty",
126 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700127 goto done;
128 }
129 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700130 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700131 goto done;
132 }
133 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700134 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700135 goto done;
136 }
137
Stephen Smalley779701d2012-02-09 14:13:23 -0500138 char *secontext = NULL;
139
140 if (sehandle) {
141 selabel_lookup(sehandle, &secontext, mount_point, 0755);
142 setfscreatecon(secontext);
143 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500144
Doug Zongker9931f7f2009-06-10 14:11:53 -0700145 mkdir(mount_point, 0755);
146
Stephen Smalley779701d2012-02-09 14:13:23 -0500147 if (secontext) {
148 freecon(secontext);
149 setfscreatecon(NULL);
150 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500151
Doug Zongker3d177d02010-07-01 09:18:44 -0700152 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700153 mtd_scan_partitions();
154 const MtdPartition* mtd;
155 mtd = mtd_find_partition_by_name(location);
156 if (mtd == NULL) {
Michael Rungef15e31e2014-10-24 14:14:41 -0700157 uiPrintf(state, "%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700158 name, location);
159 result = strdup("");
160 goto done;
161 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700162 if (mtd_mount_partition(mtd, mount_point, fs_type, 0 /* rw */) != 0) {
Michael Rungef15e31e2014-10-24 14:14:41 -0700163 uiPrintf(state, "mtd mount of %s failed: %s\n",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700164 location, strerror(errno));
165 result = strdup("");
166 goto done;
167 }
168 result = mount_point;
169 } else {
Doug Zongker3d177d02010-07-01 09:18:44 -0700170 if (mount(location, mount_point, fs_type,
Michael Rungebd6138c2014-10-22 17:05:08 -0700171 MS_NOATIME | MS_NODEV | MS_NODIRATIME,
172 has_mount_options ? mount_options : "") < 0) {
Michael Rungef15e31e2014-10-24 14:14:41 -0700173 uiPrintf(state, "%s: failed to mount %s at %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700174 name, location, mount_point, strerror(errno));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700175 result = strdup("");
176 } else {
177 result = mount_point;
178 }
179 }
180
181done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700182 free(fs_type);
183 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700184 free(location);
185 if (result != mount_point) free(mount_point);
Michael Rungebd6138c2014-10-22 17:05:08 -0700186 if (has_mount_options) free(mount_options);
Doug Zongker512536a2010-02-17 16:11:44 -0800187 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700188}
189
Doug Zongker8edb00c2009-06-11 17:21:44 -0700190
191// is_mounted(mount_point)
Doug Zongker512536a2010-02-17 16:11:44 -0800192Value* IsMountedFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700193 char* result = NULL;
194 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700195 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700196 }
197 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700198 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700199 return NULL;
200 }
201 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700202 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker8edb00c2009-06-11 17:21:44 -0700203 goto done;
204 }
205
206 scan_mounted_volumes();
207 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
208 if (vol == NULL) {
209 result = strdup("");
210 } else {
211 result = mount_point;
212 }
213
214done:
215 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800216 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700217}
218
219
Doug Zongker512536a2010-02-17 16:11:44 -0800220Value* UnmountFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700221 char* result = NULL;
222 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700223 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700224 }
225 char* mount_point;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700226 if (ReadArgs(state, argv, 1, &mount_point) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700227 return NULL;
228 }
229 if (strlen(mount_point) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700230 ErrorAbort(state, "mount_point argument to unmount() can't be empty");
Doug Zongker9931f7f2009-06-10 14:11:53 -0700231 goto done;
232 }
233
234 scan_mounted_volumes();
235 const MountedVolume* vol = find_mounted_volume_by_mount_point(mount_point);
236 if (vol == NULL) {
Michael Rungef15e31e2014-10-24 14:14:41 -0700237 uiPrintf(state, "unmount of %s failed; no such volume\n", mount_point);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700238 result = strdup("");
239 } else {
Michael Rungef15e31e2014-10-24 14:14:41 -0700240 int ret = unmount_mounted_volume(vol);
241 if (ret != 0) {
242 uiPrintf(state, "unmount of %s failed (%d): %s\n",
243 mount_point, ret, strerror(errno));
244 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700245 result = mount_point;
246 }
247
248done:
249 if (result != mount_point) free(mount_point);
Doug Zongker512536a2010-02-17 16:11:44 -0800250 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700251}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700252
JP Abgrall37aedb32014-06-16 19:07:39 -0700253static int exec_cmd(const char* path, char* const argv[]) {
254 int status;
255 pid_t child;
256 if ((child = vfork()) == 0) {
257 execv(path, argv);
258 _exit(-1);
259 }
260 waitpid(child, &status, 0);
261 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
262 printf("%s failed with status %d\n", path, WEXITSTATUS(status));
263 }
264 return WEXITSTATUS(status);
265}
266
Doug Zongker8edb00c2009-06-11 17:21:44 -0700267
Stephen Smalley779701d2012-02-09 14:13:23 -0500268// format(fs_type, partition_type, location, fs_size, mount_point)
Doug Zongker9931f7f2009-06-10 14:11:53 -0700269//
Stephen Smalley779701d2012-02-09 14:13:23 -0500270// fs_type="yaffs2" partition_type="MTD" location=partition fs_size=<bytes> mount_point=<location>
271// fs_type="ext4" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
JP Abgrall37aedb32014-06-16 19:07:39 -0700272// fs_type="f2fs" partition_type="EMMC" location=device fs_size=<bytes> mount_point=<location>
273// if fs_size == 0, then make fs uses the entire partition.
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800274// if fs_size > 0, that is the size to use
JP Abgrall37aedb32014-06-16 19:07:39 -0700275// 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 -0800276Value* FormatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700277 char* result = NULL;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400278 if (argc != 5) {
279 return ErrorAbort(state, "%s() expects 5 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700280 }
Doug Zongker3d177d02010-07-01 09:18:44 -0700281 char* fs_type;
282 char* partition_type;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700283 char* location;
Ken Sumrall8f132ed2011-01-14 18:55:05 -0800284 char* fs_size;
Stephen Smalley516e4e22012-04-03 13:35:11 -0400285 char* mount_point;
Stephen Smalley779701d2012-02-09 14:13:23 -0500286
Stephen Smalley779701d2012-02-09 14:13:23 -0500287 if (ReadArgs(state, argv, 5, &fs_type, &partition_type, &location, &fs_size, &mount_point) < 0) {
288 return NULL;
289 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700290
Doug Zongker3d177d02010-07-01 09:18:44 -0700291 if (strlen(fs_type) == 0) {
292 ErrorAbort(state, "fs_type argument to %s() can't be empty", name);
293 goto done;
294 }
295 if (strlen(partition_type) == 0) {
296 ErrorAbort(state, "partition_type argument to %s() can't be empty",
297 name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700298 goto done;
299 }
300 if (strlen(location) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700301 ErrorAbort(state, "location argument to %s() can't be empty", name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700302 goto done;
303 }
304
Stephen Smalley516e4e22012-04-03 13:35:11 -0400305 if (strlen(mount_point) == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500306 ErrorAbort(state, "mount_point argument to %s() can't be empty", name);
307 goto done;
308 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500309
Doug Zongker3d177d02010-07-01 09:18:44 -0700310 if (strcmp(partition_type, "MTD") == 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700311 mtd_scan_partitions();
312 const MtdPartition* mtd = mtd_find_partition_by_name(location);
313 if (mtd == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700314 printf("%s: no mtd partition named \"%s\"",
Doug Zongker9931f7f2009-06-10 14:11:53 -0700315 name, location);
316 result = strdup("");
317 goto done;
318 }
319 MtdWriteContext* ctx = mtd_write_partition(mtd);
320 if (ctx == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700321 printf("%s: can't write \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700322 result = strdup("");
323 goto done;
324 }
325 if (mtd_erase_blocks(ctx, -1) == -1) {
326 mtd_write_close(ctx);
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700327 printf("%s: failed to erase \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700328 result = strdup("");
329 goto done;
330 }
331 if (mtd_write_close(ctx) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700332 printf("%s: failed to close \"%s\"", name, location);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700333 result = strdup("");
334 goto done;
335 }
336 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700337#ifdef USE_EXT4
338 } else if (strcmp(fs_type, "ext4") == 0) {
Stephen Smalley779701d2012-02-09 14:13:23 -0500339 int status = make_ext4fs(location, atoll(fs_size), mount_point, sehandle);
Doug Zongker3d177d02010-07-01 09:18:44 -0700340 if (status != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700341 printf("%s: make_ext4fs failed (%d) on %s",
Doug Zongker3d177d02010-07-01 09:18:44 -0700342 name, status, location);
343 result = strdup("");
344 goto done;
345 }
346 result = location;
JP Abgrall37aedb32014-06-16 19:07:39 -0700347 } else if (strcmp(fs_type, "f2fs") == 0) {
348 char *num_sectors;
349 if (asprintf(&num_sectors, "%lld", atoll(fs_size) / 512) <= 0) {
350 printf("format_volume: failed to create %s command for %s\n", fs_type, location);
351 result = strdup("");
352 goto done;
353 }
354 const char *f2fs_path = "/sbin/mkfs.f2fs";
355 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", location, num_sectors, NULL};
356 int status = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
357 free(num_sectors);
358 if (status != 0) {
359 printf("%s: mkfs.f2fs failed (%d) on %s",
360 name, status, location);
361 result = strdup("");
362 goto done;
363 }
364 result = location;
Doug Zongker3d177d02010-07-01 09:18:44 -0700365#endif
Doug Zongker9931f7f2009-06-10 14:11:53 -0700366 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700367 printf("%s: unsupported fs_type \"%s\" partition_type \"%s\"",
Doug Zongker3d177d02010-07-01 09:18:44 -0700368 name, fs_type, partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700369 }
370
371done:
Doug Zongker3d177d02010-07-01 09:18:44 -0700372 free(fs_type);
373 free(partition_type);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700374 if (result != location) free(location);
Doug Zongker512536a2010-02-17 16:11:44 -0800375 return StringValue(result);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700376}
377
Michael Rungece7ca712013-11-06 17:42:20 -0800378Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) {
379 char* result = NULL;
380 if (argc != 2) {
381 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
382 }
383
384 char* src_name;
385 char* dst_name;
386
387 if (ReadArgs(state, argv, 2, &src_name, &dst_name) < 0) {
388 return NULL;
389 }
390 if (strlen(src_name) == 0) {
391 ErrorAbort(state, "src_name argument to %s() can't be empty", name);
392 goto done;
393 }
394 if (strlen(dst_name) == 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700395 ErrorAbort(state, "dst_name argument to %s() can't be empty", name);
Michael Rungece7ca712013-11-06 17:42:20 -0800396 goto done;
397 }
Michael Rungea91ecc52014-07-21 17:40:02 -0700398 if (make_parents(dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700399 ErrorAbort(state, "Creating parent of %s failed, error %s",
Michael Rungea91ecc52014-07-21 17:40:02 -0700400 dst_name, strerror(errno));
Michael Runged5b17272014-10-22 14:28:23 -0700401 } else if (access(dst_name, F_OK) == 0 && access(src_name, F_OK) != 0) {
402 // File was already moved
403 result = dst_name;
Michael Rungea91ecc52014-07-21 17:40:02 -0700404 } else if (rename(src_name, dst_name) != 0) {
Doug Zongker2b5f0e02014-08-06 08:25:03 -0700405 ErrorAbort(state, "Rename of %s to %s failed, error %s",
Michael Rungece7ca712013-11-06 17:42:20 -0800406 src_name, dst_name, strerror(errno));
407 } else {
408 result = dst_name;
409 }
410
411done:
412 free(src_name);
413 if (result != dst_name) free(dst_name);
414 return StringValue(result);
415}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700416
Doug Zongker512536a2010-02-17 16:11:44 -0800417Value* DeleteFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700418 char** paths = malloc(argc * sizeof(char*));
419 int i;
420 for (i = 0; i < argc; ++i) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700421 paths[i] = Evaluate(state, argv[i]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700422 if (paths[i] == NULL) {
423 int j;
424 for (j = 0; j < i; ++i) {
425 free(paths[j]);
426 }
427 free(paths);
428 return NULL;
429 }
430 }
431
432 bool recursive = (strcmp(name, "delete_recursive") == 0);
433
434 int success = 0;
435 for (i = 0; i < argc; ++i) {
436 if ((recursive ? dirUnlinkHierarchy(paths[i]) : unlink(paths[i])) == 0)
437 ++success;
438 free(paths[i]);
439 }
440 free(paths);
441
442 char buffer[10];
443 sprintf(buffer, "%d", success);
Doug Zongker512536a2010-02-17 16:11:44 -0800444 return StringValue(strdup(buffer));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700445}
446
Doug Zongker8edb00c2009-06-11 17:21:44 -0700447
Doug Zongker512536a2010-02-17 16:11:44 -0800448Value* ShowProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700449 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700450 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700451 }
452 char* frac_str;
453 char* sec_str;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700454 if (ReadArgs(state, argv, 2, &frac_str, &sec_str) < 0) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700455 return NULL;
456 }
457
458 double frac = strtod(frac_str, NULL);
459 int sec = strtol(sec_str, NULL, 10);
460
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700461 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700462 fprintf(ui->cmd_pipe, "progress %f %d\n", frac, sec);
463
Doug Zongker9931f7f2009-06-10 14:11:53 -0700464 free(sec_str);
Doug Zongker512536a2010-02-17 16:11:44 -0800465 return StringValue(frac_str);
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700466}
467
Doug Zongker512536a2010-02-17 16:11:44 -0800468Value* SetProgressFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerfbf3c102009-06-24 09:36:20 -0700469 if (argc != 1) {
470 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
471 }
472 char* frac_str;
473 if (ReadArgs(state, argv, 1, &frac_str) < 0) {
474 return NULL;
475 }
476
477 double frac = strtod(frac_str, NULL);
478
479 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
480 fprintf(ui->cmd_pipe, "set_progress %f\n", frac);
481
Doug Zongker512536a2010-02-17 16:11:44 -0800482 return StringValue(frac_str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700483}
484
Doug Zongker8edb00c2009-06-11 17:21:44 -0700485// package_extract_dir(package_path, destination_path)
Doug Zongker512536a2010-02-17 16:11:44 -0800486Value* PackageExtractDirFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700487 int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700488 if (argc != 2) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700489 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700490 }
491 char* zip_path;
492 char* dest_path;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700493 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700494
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700495 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700496
497 // To create a consistent system image, never use the clock for timestamps.
498 struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default
499
500 bool success = mzExtractRecursive(za, zip_path, dest_path,
501 MZ_EXTRACT_FILES_ONLY, &timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500502 NULL, NULL, sehandle);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700503 free(zip_path);
504 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800505 return StringValue(strdup(success ? "t" : ""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700506}
507
Doug Zongker8edb00c2009-06-11 17:21:44 -0700508
509// package_extract_file(package_path, destination_path)
Doug Zongker6aece332010-02-01 14:40:12 -0800510// or
511// package_extract_file(package_path)
512// to return the entire contents of the file as the result of this
Doug Zongker512536a2010-02-17 16:11:44 -0800513// function (the char* returned is actually a FileContents*).
514Value* PackageExtractFileFn(const char* name, State* state,
Doug Zongker8edb00c2009-06-11 17:21:44 -0700515 int argc, Expr* argv[]) {
Doug Zongker5f875bf2014-08-22 14:53:43 -0700516 if (argc < 1 || argc > 2) {
Doug Zongker6aece332010-02-01 14:40:12 -0800517 return ErrorAbort(state, "%s() expects 1 or 2 args, got %d",
518 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700519 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700520 bool success = false;
Doug Zongker43772d22014-06-09 14:13:19 -0700521
522 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
Doug Zongker43772d22014-06-09 14:13:19 -0700523
Doug Zongker6aece332010-02-01 14:40:12 -0800524 if (argc == 2) {
525 // The two-argument version extracts to a file.
Doug Zongker8edb00c2009-06-11 17:21:44 -0700526
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -0800527 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700528
Doug Zongker6aece332010-02-01 14:40:12 -0800529 char* zip_path;
530 char* dest_path;
531 if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL;
532
Doug Zongker6aece332010-02-01 14:40:12 -0800533 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
534 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700535 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800536 goto done2;
537 }
538
539 FILE* f = fopen(dest_path, "wb");
540 if (f == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700541 printf("%s: can't open %s for write: %s\n",
Doug Zongker6aece332010-02-01 14:40:12 -0800542 name, dest_path, strerror(errno));
543 goto done2;
544 }
545 success = mzExtractZipEntryToFile(za, entry, fileno(f));
546 fclose(f);
547
548 done2:
549 free(zip_path);
550 free(dest_path);
Doug Zongker512536a2010-02-17 16:11:44 -0800551 return StringValue(strdup(success ? "t" : ""));
Doug Zongker6aece332010-02-01 14:40:12 -0800552 } else {
553 // The one-argument version returns the contents of the file
554 // as the result.
555
556 char* zip_path;
Doug Zongker512536a2010-02-17 16:11:44 -0800557 Value* v = malloc(sizeof(Value));
558 v->type = VAL_BLOB;
559 v->size = -1;
560 v->data = NULL;
Doug Zongker6aece332010-02-01 14:40:12 -0800561
562 if (ReadArgs(state, argv, 1, &zip_path) < 0) return NULL;
563
564 ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip;
565 const ZipEntry* entry = mzFindZipEntry(za, zip_path);
566 if (entry == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700567 printf("%s: no %s in package\n", name, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800568 goto done1;
569 }
570
Doug Zongker512536a2010-02-17 16:11:44 -0800571 v->size = mzGetZipEntryUncompLen(entry);
572 v->data = malloc(v->size);
573 if (v->data == NULL) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700574 printf("%s: failed to allocate %ld bytes for %s\n",
Doug Zongker512536a2010-02-17 16:11:44 -0800575 name, (long)v->size, zip_path);
Doug Zongker6aece332010-02-01 14:40:12 -0800576 goto done1;
577 }
578
Doug Zongker512536a2010-02-17 16:11:44 -0800579 success = mzExtractZipEntryToBuffer(za, entry,
580 (unsigned char *)v->data);
Doug Zongker6aece332010-02-01 14:40:12 -0800581
582 done1:
583 free(zip_path);
584 if (!success) {
Doug Zongker512536a2010-02-17 16:11:44 -0800585 free(v->data);
586 v->data = NULL;
587 v->size = -1;
Doug Zongker6aece332010-02-01 14:40:12 -0800588 }
Doug Zongker512536a2010-02-17 16:11:44 -0800589 return v;
Doug Zongker8edb00c2009-06-11 17:21:44 -0700590 }
Doug Zongker8edb00c2009-06-11 17:21:44 -0700591}
592
Doug Zongkera23075f2012-08-06 16:19:09 -0700593// Create all parent directories of name, if necessary.
594static int make_parents(char* name) {
595 char* p;
596 for (p = name + (strlen(name)-1); p > name; --p) {
597 if (*p != '/') continue;
598 *p = '\0';
599 if (make_parents(name) < 0) return -1;
600 int result = mkdir(name, 0700);
Michael Rungea91ecc52014-07-21 17:40:02 -0700601 if (result == 0) printf("created [%s]\n", name);
Doug Zongkera23075f2012-08-06 16:19:09 -0700602 *p = '/';
603 if (result == 0 || errno == EEXIST) {
604 // successfully created or already existed; we're done
605 return 0;
606 } else {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700607 printf("failed to mkdir %s: %s\n", name, strerror(errno));
Doug Zongkera23075f2012-08-06 16:19:09 -0700608 return -1;
609 }
610 }
611 return 0;
612}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700613
Doug Zongker9931f7f2009-06-10 14:11:53 -0700614// symlink target src1 src2 ...
Doug Zongker60babf82009-09-18 15:11:24 -0700615// unlinks any previously existing src1, src2, etc before creating symlinks.
Doug Zongker512536a2010-02-17 16:11:44 -0800616Value* SymlinkFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker9931f7f2009-06-10 14:11:53 -0700617 if (argc == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700618 return ErrorAbort(state, "%s() expects 1+ args, got %d", name, argc);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700619 }
620 char* target;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700621 target = Evaluate(state, argv[0]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700622 if (target == NULL) return NULL;
623
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700624 char** srcs = ReadVarArgs(state, argc-1, argv+1);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700625 if (srcs == NULL) {
626 free(target);
627 return NULL;
628 }
629
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700630 int bad = 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700631 int i;
632 for (i = 0; i < argc-1; ++i) {
Doug Zongker60babf82009-09-18 15:11:24 -0700633 if (unlink(srcs[i]) < 0) {
634 if (errno != ENOENT) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700635 printf("%s: failed to remove %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700636 name, srcs[i], strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700637 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700638 }
639 }
Doug Zongkera23075f2012-08-06 16:19:09 -0700640 if (make_parents(srcs[i])) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700641 printf("%s: failed to symlink %s to %s: making parents failed\n",
Doug Zongkera23075f2012-08-06 16:19:09 -0700642 name, srcs[i], target);
643 ++bad;
644 }
Doug Zongker60babf82009-09-18 15:11:24 -0700645 if (symlink(target, srcs[i]) < 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700646 printf("%s: failed to symlink %s to %s: %s\n",
Doug Zongker60babf82009-09-18 15:11:24 -0700647 name, srcs[i], target, strerror(errno));
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700648 ++bad;
Doug Zongker60babf82009-09-18 15:11:24 -0700649 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700650 free(srcs[i]);
651 }
652 free(srcs);
Doug Zongkeracd73ed2012-03-22 14:32:52 -0700653 if (bad) {
654 return ErrorAbort(state, "%s: some symlinks failed", name);
655 }
Doug Zongker512536a2010-02-17 16:11:44 -0800656 return StringValue(strdup(""));
Doug Zongker9931f7f2009-06-10 14:11:53 -0700657}
658
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700659struct perm_parsed_args {
660 bool has_uid;
661 uid_t uid;
662 bool has_gid;
663 gid_t gid;
664 bool has_mode;
665 mode_t mode;
666 bool has_fmode;
667 mode_t fmode;
668 bool has_dmode;
669 mode_t dmode;
670 bool has_selabel;
671 char* selabel;
672 bool has_capabilities;
673 uint64_t capabilities;
674};
675
Michael Runge75480252014-10-22 19:48:41 -0700676static struct perm_parsed_args ParsePermArgs(State * state, int argc, char** args) {
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700677 int i;
678 struct perm_parsed_args parsed;
679 int bad = 0;
680 static int max_warnings = 20;
681
682 memset(&parsed, 0, sizeof(parsed));
683
684 for (i = 1; i < argc; i += 2) {
685 if (strcmp("uid", args[i]) == 0) {
686 int64_t uid;
687 if (sscanf(args[i+1], "%" SCNd64, &uid) == 1) {
688 parsed.uid = uid;
689 parsed.has_uid = true;
690 } else {
Michael Runge75480252014-10-22 19:48:41 -0700691 uiPrintf(state, "ParsePermArgs: invalid UID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700692 bad++;
693 }
694 continue;
695 }
696 if (strcmp("gid", args[i]) == 0) {
697 int64_t gid;
698 if (sscanf(args[i+1], "%" SCNd64, &gid) == 1) {
699 parsed.gid = gid;
700 parsed.has_gid = true;
701 } else {
Michael Runge75480252014-10-22 19:48:41 -0700702 uiPrintf(state, "ParsePermArgs: invalid GID \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700703 bad++;
704 }
705 continue;
706 }
707 if (strcmp("mode", args[i]) == 0) {
708 int32_t mode;
709 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
710 parsed.mode = mode;
711 parsed.has_mode = true;
712 } else {
Michael Runge75480252014-10-22 19:48:41 -0700713 uiPrintf(state, "ParsePermArgs: invalid mode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700714 bad++;
715 }
716 continue;
717 }
718 if (strcmp("dmode", args[i]) == 0) {
719 int32_t mode;
720 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
721 parsed.dmode = mode;
722 parsed.has_dmode = true;
723 } else {
Michael Runge75480252014-10-22 19:48:41 -0700724 uiPrintf(state, "ParsePermArgs: invalid dmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700725 bad++;
726 }
727 continue;
728 }
729 if (strcmp("fmode", args[i]) == 0) {
730 int32_t mode;
731 if (sscanf(args[i+1], "%" SCNi32, &mode) == 1) {
732 parsed.fmode = mode;
733 parsed.has_fmode = true;
734 } else {
Michael Runge75480252014-10-22 19:48:41 -0700735 uiPrintf(state, "ParsePermArgs: invalid fmode \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700736 bad++;
737 }
738 continue;
739 }
740 if (strcmp("capabilities", args[i]) == 0) {
741 int64_t capabilities;
742 if (sscanf(args[i+1], "%" SCNi64, &capabilities) == 1) {
743 parsed.capabilities = capabilities;
744 parsed.has_capabilities = true;
745 } else {
Michael Runge75480252014-10-22 19:48:41 -0700746 uiPrintf(state, "ParsePermArgs: invalid capabilities \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700747 bad++;
748 }
749 continue;
750 }
751 if (strcmp("selabel", args[i]) == 0) {
752 if (args[i+1][0] != '\0') {
753 parsed.selabel = args[i+1];
754 parsed.has_selabel = true;
755 } else {
Michael Runge75480252014-10-22 19:48:41 -0700756 uiPrintf(state, "ParsePermArgs: invalid selabel \"%s\"\n", args[i + 1]);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700757 bad++;
758 }
759 continue;
760 }
761 if (max_warnings != 0) {
762 printf("ParsedPermArgs: unknown key \"%s\", ignoring\n", args[i]);
763 max_warnings--;
764 if (max_warnings == 0) {
765 printf("ParsedPermArgs: suppressing further warnings\n");
766 }
767 }
768 }
769 return parsed;
770}
771
772static int ApplyParsedPerms(
Michael Runge75480252014-10-22 19:48:41 -0700773 State * state,
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700774 const char* filename,
775 const struct stat *statptr,
776 struct perm_parsed_args parsed)
777{
778 int bad = 0;
779
Nick Kralevich6a821fe2014-10-23 20:36:42 -0700780 if (parsed.has_selabel) {
781 if (lsetfilecon(filename, parsed.selabel) != 0) {
782 uiPrintf(state, "ApplyParsedPerms: lsetfilecon of %s to %s failed: %s\n",
783 filename, parsed.selabel, strerror(errno));
784 bad++;
785 }
786 }
787
Nick Kraleviche4612512013-09-10 15:34:19 -0700788 /* ignore symlinks */
789 if (S_ISLNK(statptr->st_mode)) {
Nick Kralevich6a821fe2014-10-23 20:36:42 -0700790 return bad;
Nick Kraleviche4612512013-09-10 15:34:19 -0700791 }
792
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700793 if (parsed.has_uid) {
794 if (chown(filename, parsed.uid, -1) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700795 uiPrintf(state, "ApplyParsedPerms: chown of %s to %d failed: %s\n",
796 filename, parsed.uid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700797 bad++;
798 }
799 }
800
801 if (parsed.has_gid) {
802 if (chown(filename, -1, parsed.gid) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700803 uiPrintf(state, "ApplyParsedPerms: chgrp of %s to %d failed: %s\n",
804 filename, parsed.gid, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700805 bad++;
806 }
807 }
808
809 if (parsed.has_mode) {
810 if (chmod(filename, parsed.mode) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700811 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
812 filename, parsed.mode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700813 bad++;
814 }
815 }
816
817 if (parsed.has_dmode && S_ISDIR(statptr->st_mode)) {
818 if (chmod(filename, parsed.dmode) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700819 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
820 filename, parsed.dmode, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700821 bad++;
822 }
823 }
824
825 if (parsed.has_fmode && S_ISREG(statptr->st_mode)) {
826 if (chmod(filename, parsed.fmode) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700827 uiPrintf(state, "ApplyParsedPerms: chmod of %s to %d failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700828 filename, parsed.fmode, strerror(errno));
829 bad++;
830 }
831 }
832
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700833 if (parsed.has_capabilities && S_ISREG(statptr->st_mode)) {
834 if (parsed.capabilities == 0) {
835 if ((removexattr(filename, XATTR_NAME_CAPS) == -1) && (errno != ENODATA)) {
836 // Report failure unless it's ENODATA (attribute not set)
Michael Runge75480252014-10-22 19:48:41 -0700837 uiPrintf(state, "ApplyParsedPerms: removexattr of %s to %" PRIx64 " failed: %s\n",
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700838 filename, parsed.capabilities, strerror(errno));
839 bad++;
840 }
841 } else {
842 struct vfs_cap_data cap_data;
843 memset(&cap_data, 0, sizeof(cap_data));
844 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE;
845 cap_data.data[0].permitted = (uint32_t) (parsed.capabilities & 0xffffffff);
846 cap_data.data[0].inheritable = 0;
847 cap_data.data[1].permitted = (uint32_t) (parsed.capabilities >> 32);
848 cap_data.data[1].inheritable = 0;
849 if (setxattr(filename, XATTR_NAME_CAPS, &cap_data, sizeof(cap_data), 0) < 0) {
Michael Runge75480252014-10-22 19:48:41 -0700850 uiPrintf(state, "ApplyParsedPerms: setcap of %s to %" PRIx64 " failed: %s\n",
851 filename, parsed.capabilities, strerror(errno));
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700852 bad++;
853 }
854 }
855 }
856
857 return bad;
858}
859
860// nftw doesn't allow us to pass along context, so we need to use
861// global variables. *sigh*
862static struct perm_parsed_args recursive_parsed_args;
Michael Runge75480252014-10-22 19:48:41 -0700863static State* recursive_state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700864
865static int do_SetMetadataRecursive(const char* filename, const struct stat *statptr,
866 int fileflags, struct FTW *pfwt) {
Michael Runge75480252014-10-22 19:48:41 -0700867 return ApplyParsedPerms(recursive_state, filename, statptr, recursive_parsed_args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700868}
869
870static Value* SetMetadataFn(const char* name, State* state, int argc, Expr* argv[]) {
871 int i;
872 int bad = 0;
873 static int nwarnings = 0;
874 struct stat sb;
875 Value* result = NULL;
876
877 bool recursive = (strcmp(name, "set_metadata_recursive") == 0);
878
879 if ((argc % 2) != 1) {
880 return ErrorAbort(state, "%s() expects an odd number of arguments, got %d",
881 name, argc);
882 }
883
884 char** args = ReadVarArgs(state, argc, argv);
885 if (args == NULL) return NULL;
886
887 if (lstat(args[0], &sb) == -1) {
888 result = ErrorAbort(state, "%s: Error on lstat of \"%s\": %s", name, args[0], strerror(errno));
889 goto done;
890 }
891
Michael Runge75480252014-10-22 19:48:41 -0700892 struct perm_parsed_args parsed = ParsePermArgs(state, argc, args);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700893
894 if (recursive) {
895 recursive_parsed_args = parsed;
Michael Runge75480252014-10-22 19:48:41 -0700896 recursive_state = state;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700897 bad += nftw(args[0], do_SetMetadataRecursive, 30, FTW_CHDIR | FTW_DEPTH | FTW_PHYS);
898 memset(&recursive_parsed_args, 0, sizeof(recursive_parsed_args));
Michael Runge75480252014-10-22 19:48:41 -0700899 recursive_state = NULL;
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700900 } else {
Michael Runge75480252014-10-22 19:48:41 -0700901 bad += ApplyParsedPerms(state, args[0], &sb, parsed);
Nick Kralevich5dbdef02013-09-07 14:41:06 -0700902 }
903
904done:
905 for (i = 0; i < argc; ++i) {
906 free(args[i]);
907 }
908 free(args);
909
910 if (result != NULL) {
911 return result;
912 }
913
914 if (bad > 0) {
915 return ErrorAbort(state, "%s: some changes failed", name);
916 }
917
918 return StringValue(strdup(""));
919}
Doug Zongker8edb00c2009-06-11 17:21:44 -0700920
Doug Zongker512536a2010-02-17 16:11:44 -0800921Value* GetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -0700922 if (argc != 1) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700923 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700924 }
925 char* key;
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700926 key = Evaluate(state, argv[0]);
Doug Zongker8edb00c2009-06-11 17:21:44 -0700927 if (key == NULL) return NULL;
928
929 char value[PROPERTY_VALUE_MAX];
930 property_get(key, value, "");
931 free(key);
932
Doug Zongker512536a2010-02-17 16:11:44 -0800933 return StringValue(strdup(value));
Doug Zongker8edb00c2009-06-11 17:21:44 -0700934}
935
936
Doug Zongker47cace92009-06-18 10:11:50 -0700937// file_getprop(file, key)
938//
939// interprets 'file' as a getprop-style file (key=value pairs, one
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700940// per line. # comment lines,blank lines, lines without '=' ignored),
941// and returns the value for 'key' (or "" if it isn't defined).
Doug Zongker512536a2010-02-17 16:11:44 -0800942Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker47cace92009-06-18 10:11:50 -0700943 char* result = NULL;
944 char* buffer = NULL;
945 char* filename;
946 char* key;
947 if (ReadArgs(state, argv, 2, &filename, &key) < 0) {
948 return NULL;
949 }
950
951 struct stat st;
952 if (stat(filename, &st) < 0) {
953 ErrorAbort(state, "%s: failed to stat \"%s\": %s",
954 name, filename, strerror(errno));
955 goto done;
956 }
957
958#define MAX_FILE_GETPROP_SIZE 65536
959
960 if (st.st_size > MAX_FILE_GETPROP_SIZE) {
961 ErrorAbort(state, "%s too large for %s (max %d)",
962 filename, name, MAX_FILE_GETPROP_SIZE);
963 goto done;
964 }
965
966 buffer = malloc(st.st_size+1);
967 if (buffer == NULL) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700968 ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1);
Doug Zongker47cace92009-06-18 10:11:50 -0700969 goto done;
970 }
971
972 FILE* f = fopen(filename, "rb");
973 if (f == NULL) {
974 ErrorAbort(state, "%s: failed to open %s: %s",
975 name, filename, strerror(errno));
976 goto done;
977 }
978
979 if (fread(buffer, 1, st.st_size, f) != st.st_size) {
Doug Zongkera23075f2012-08-06 16:19:09 -0700980 ErrorAbort(state, "%s: failed to read %lld bytes from %s",
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700981 name, (long long)st.st_size+1, filename);
Doug Zongker47cace92009-06-18 10:11:50 -0700982 fclose(f);
983 goto done;
984 }
985 buffer[st.st_size] = '\0';
986
987 fclose(f);
988
989 char* line = strtok(buffer, "\n");
990 do {
991 // skip whitespace at start of line
992 while (*line && isspace(*line)) ++line;
993
994 // comment or blank line: skip to next line
995 if (*line == '\0' || *line == '#') continue;
996
997 char* equal = strchr(line, '=');
998 if (equal == NULL) {
Michael Rungeaa1a31e2014-04-25 18:47:18 -0700999 continue;
Doug Zongker47cace92009-06-18 10:11:50 -07001000 }
1001
1002 // trim whitespace between key and '='
1003 char* key_end = equal-1;
1004 while (key_end > line && isspace(*key_end)) --key_end;
1005 key_end[1] = '\0';
1006
1007 // not the key we're looking for
1008 if (strcmp(key, line) != 0) continue;
1009
1010 // skip whitespace after the '=' to the start of the value
1011 char* val_start = equal+1;
1012 while(*val_start && isspace(*val_start)) ++val_start;
1013
1014 // trim trailing whitespace
1015 char* val_end = val_start + strlen(val_start)-1;
1016 while (val_end > val_start && isspace(*val_end)) --val_end;
1017 val_end[1] = '\0';
1018
1019 result = strdup(val_start);
1020 break;
1021
1022 } while ((line = strtok(NULL, "\n")));
1023
1024 if (result == NULL) result = strdup("");
1025
1026 done:
1027 free(filename);
1028 free(key);
1029 free(buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001030 return StringValue(result);
Doug Zongker47cace92009-06-18 10:11:50 -07001031}
1032
1033
Doug Zongker8edb00c2009-06-11 17:21:44 -07001034static bool write_raw_image_cb(const unsigned char* data,
1035 int data_len, void* ctx) {
1036 int r = mtd_write_data((MtdWriteContext*)ctx, (const char *)data, data_len);
1037 if (r == data_len) return true;
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001038 printf("%s\n", strerror(errno));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001039 return false;
1040}
1041
Doug Zongker179b2d92011-04-12 15:49:04 -07001042// write_raw_image(filename_or_blob, partition)
Doug Zongker512536a2010-02-17 16:11:44 -08001043Value* WriteRawImageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001044 char* result = NULL;
1045
Doug Zongker179b2d92011-04-12 15:49:04 -07001046 Value* partition_value;
1047 Value* contents;
1048 if (ReadValueArgs(state, argv, 2, &contents, &partition_value) < 0) {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001049 return NULL;
1050 }
1051
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001052 char* partition = NULL;
Doug Zongker179b2d92011-04-12 15:49:04 -07001053 if (partition_value->type != VAL_STRING) {
1054 ErrorAbort(state, "partition argument to %s must be string", name);
1055 goto done;
1056 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001057 partition = partition_value->data;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001058 if (strlen(partition) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001059 ErrorAbort(state, "partition argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001060 goto done;
1061 }
Doug Zongker179b2d92011-04-12 15:49:04 -07001062 if (contents->type == VAL_STRING && strlen((char*) contents->data) == 0) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001063 ErrorAbort(state, "file argument to %s can't be empty", name);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001064 goto done;
1065 }
1066
Dees_Troy76a1d412013-04-20 13:54:35 +00001067 char* filename = contents->data;
1068 if (0 == restore_raw_partition(NULL, partition, filename))
1069 result = strdup(partition);
1070 else {
Doug Zongker8edb00c2009-06-11 17:21:44 -07001071 result = strdup("");
1072 goto done;
1073 }
1074
Doug Zongker8edb00c2009-06-11 17:21:44 -07001075done:
Doug Zongker179b2d92011-04-12 15:49:04 -07001076 if (result != partition) FreeValue(partition_value);
1077 FreeValue(contents);
Doug Zongker512536a2010-02-17 16:11:44 -08001078 return StringValue(result);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001079}
1080
Doug Zongker8edb00c2009-06-11 17:21:44 -07001081// apply_patch_space(bytes)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001082Value* ApplyPatchSpaceFn(const char* name, State* state,
1083 int argc, Expr* argv[]) {
1084 char* bytes_str;
1085 if (ReadArgs(state, argv, 1, &bytes_str) < 0) {
1086 return NULL;
1087 }
1088
1089 char* endptr;
1090 size_t bytes = strtol(bytes_str, &endptr, 10);
1091 if (bytes == 0 && endptr == bytes_str) {
1092 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count\n\n",
1093 name, bytes_str);
1094 free(bytes_str);
1095 return NULL;
1096 }
1097
1098 return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t"));
1099}
1100
Doug Zongker52b40362014-02-10 15:30:30 -08001101// apply_patch(file, size, init_sha1, tgt_sha1, patch)
Doug Zongkerc4351c72010-02-22 14:46:32 -08001102
Doug Zongker512536a2010-02-17 16:11:44 -08001103Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc4351c72010-02-22 14:46:32 -08001104 if (argc < 6 || (argc % 2) == 1) {
1105 return ErrorAbort(state, "%s(): expected at least 6 args and an "
1106 "even number, got %d",
1107 name, argc);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001108 }
1109
Doug Zongkerc4351c72010-02-22 14:46:32 -08001110 char* source_filename;
1111 char* target_filename;
1112 char* target_sha1;
1113 char* target_size_str;
1114 if (ReadArgs(state, argv, 4, &source_filename, &target_filename,
1115 &target_sha1, &target_size_str) < 0) {
1116 return NULL;
Doug Zongker8edb00c2009-06-11 17:21:44 -07001117 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001118
Doug Zongkerc4351c72010-02-22 14:46:32 -08001119 char* endptr;
1120 size_t target_size = strtol(target_size_str, &endptr, 10);
1121 if (target_size == 0 && endptr == target_size_str) {
1122 ErrorAbort(state, "%s(): can't parse \"%s\" as byte count",
1123 name, target_size_str);
1124 free(source_filename);
1125 free(target_filename);
1126 free(target_sha1);
1127 free(target_size_str);
1128 return NULL;
1129 }
1130
1131 int patchcount = (argc-4) / 2;
1132 Value** patches = ReadValueVarArgs(state, argc-4, argv+4);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001133
1134 int i;
Doug Zongkerc4351c72010-02-22 14:46:32 -08001135 for (i = 0; i < patchcount; ++i) {
1136 if (patches[i*2]->type != VAL_STRING) {
1137 ErrorAbort(state, "%s(): sha-1 #%d is not string", name, i);
1138 break;
1139 }
1140 if (patches[i*2+1]->type != VAL_BLOB) {
1141 ErrorAbort(state, "%s(): patch #%d is not blob", name, i);
1142 break;
1143 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001144 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001145 if (i != patchcount) {
1146 for (i = 0; i < patchcount*2; ++i) {
1147 FreeValue(patches[i]);
1148 }
1149 free(patches);
1150 return NULL;
1151 }
Doug Zongker8edb00c2009-06-11 17:21:44 -07001152
Doug Zongkerc4351c72010-02-22 14:46:32 -08001153 char** patch_sha_str = malloc(patchcount * sizeof(char*));
1154 for (i = 0; i < patchcount; ++i) {
1155 patch_sha_str[i] = patches[i*2]->data;
1156 patches[i*2]->data = NULL;
1157 FreeValue(patches[i*2]);
1158 patches[i] = patches[i*2+1];
Doug Zongker8edb00c2009-06-11 17:21:44 -07001159 }
Doug Zongkerc4351c72010-02-22 14:46:32 -08001160
1161 int result = applypatch(source_filename, target_filename,
1162 target_sha1, target_size,
Doug Zongkera3ccba62012-08-20 15:28:02 -07001163 patchcount, patch_sha_str, patches, NULL);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001164
1165 for (i = 0; i < patchcount; ++i) {
1166 FreeValue(patches[i]);
1167 }
1168 free(patch_sha_str);
1169 free(patches);
1170
1171 return StringValue(strdup(result == 0 ? "t" : ""));
1172}
1173
1174// apply_patch_check(file, [sha1_1, ...])
1175Value* ApplyPatchCheckFn(const char* name, State* state,
1176 int argc, Expr* argv[]) {
1177 if (argc < 1) {
1178 return ErrorAbort(state, "%s(): expected at least 1 arg, got %d",
1179 name, argc);
1180 }
1181
1182 char* filename;
1183 if (ReadArgs(state, argv, 1, &filename) < 0) {
1184 return NULL;
1185 }
1186
1187 int patchcount = argc-1;
1188 char** sha1s = ReadVarArgs(state, argc-1, argv+1);
1189
1190 int result = applypatch_check(filename, patchcount, sha1s);
1191
1192 int i;
1193 for (i = 0; i < patchcount; ++i) {
1194 free(sha1s[i]);
1195 }
1196 free(sha1s);
1197
1198 return StringValue(strdup(result == 0 ? "t" : ""));
Doug Zongker8edb00c2009-06-11 17:21:44 -07001199}
1200
Doug Zongker512536a2010-02-17 16:11:44 -08001201Value* UIPrintFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001202 char** args = ReadVarArgs(state, argc, argv);
1203 if (args == NULL) {
1204 return NULL;
1205 }
1206
1207 int size = 0;
1208 int i;
1209 for (i = 0; i < argc; ++i) {
1210 size += strlen(args[i]);
1211 }
1212 char* buffer = malloc(size+1);
1213 size = 0;
1214 for (i = 0; i < argc; ++i) {
1215 strcpy(buffer+size, args[i]);
1216 size += strlen(args[i]);
1217 free(args[i]);
1218 }
1219 free(args);
1220 buffer[size] = '\0';
Michael Runge75480252014-10-22 19:48:41 -07001221 uiPrint(state, buffer);
Doug Zongker512536a2010-02-17 16:11:44 -08001222 return StringValue(buffer);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001223}
1224
Doug Zongkerd0181b82011-10-19 10:51:12 -07001225Value* WipeCacheFn(const char* name, State* state, int argc, Expr* argv[]) {
1226 if (argc != 0) {
1227 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1228 }
1229 fprintf(((UpdaterInfo*)(state->cookie))->cmd_pipe, "wipe_cache\n");
1230 return StringValue(strdup("t"));
1231}
1232
Doug Zongker512536a2010-02-17 16:11:44 -08001233Value* RunProgramFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001234 if (argc < 1) {
1235 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1236 }
1237 char** args = ReadVarArgs(state, argc, argv);
1238 if (args == NULL) {
1239 return NULL;
1240 }
1241
1242 char** args2 = malloc(sizeof(char*) * (argc+1));
1243 memcpy(args2, args, sizeof(char*) * argc);
1244 args2[argc] = NULL;
1245
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001246 printf("about to run program [%s] with %d args\n", args2[0], argc);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001247
1248 pid_t child = fork();
1249 if (child == 0) {
1250 execv(args2[0], args2);
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001251 printf("run_program: execv failed: %s\n", strerror(errno));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001252 _exit(1);
1253 }
1254 int status;
1255 waitpid(child, &status, 0);
1256 if (WIFEXITED(status)) {
1257 if (WEXITSTATUS(status) != 0) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001258 printf("run_program: child exited with status %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001259 WEXITSTATUS(status));
1260 }
1261 } else if (WIFSIGNALED(status)) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001262 printf("run_program: child terminated by signal %d\n",
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001263 WTERMSIG(status));
1264 }
1265
1266 int i;
1267 for (i = 0; i < argc; ++i) {
1268 free(args[i]);
1269 }
1270 free(args);
1271 free(args2);
1272
1273 char buffer[20];
1274 sprintf(buffer, "%d", status);
1275
Doug Zongker512536a2010-02-17 16:11:44 -08001276 return StringValue(strdup(buffer));
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001277}
1278
Doug Zongker512536a2010-02-17 16:11:44 -08001279// sha1_check(data)
1280// to return the sha1 of the data (given in the format returned by
1281// read_file).
1282//
1283// sha1_check(data, sha1_hex, [sha1_hex, ...])
1284// returns the sha1 of the file if it matches any of the hex
1285// strings passed, or "" if it does not equal any of them.
1286//
1287Value* Sha1CheckFn(const char* name, State* state, int argc, Expr* argv[]) {
1288 if (argc < 1) {
1289 return ErrorAbort(state, "%s() expects at least 1 arg", name);
1290 }
1291
1292 Value** args = ReadValueVarArgs(state, argc, argv);
1293 if (args == NULL) {
1294 return NULL;
1295 }
1296
1297 if (args[0]->size < 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001298 return StringValue(strdup(""));
1299 }
1300 uint8_t digest[SHA_DIGEST_SIZE];
Doug Zongkerbac7fba2013-04-10 11:32:17 -07001301 SHA_hash(args[0]->data, args[0]->size, digest);
Doug Zongker512536a2010-02-17 16:11:44 -08001302 FreeValue(args[0]);
1303
1304 if (argc == 1) {
1305 return StringValue(PrintSha1(digest));
1306 }
1307
1308 int i;
1309 uint8_t* arg_digest = malloc(SHA_DIGEST_SIZE);
1310 for (i = 1; i < argc; ++i) {
1311 if (args[i]->type != VAL_STRING) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001312 printf("%s(): arg %d is not a string; skipping",
Doug Zongker512536a2010-02-17 16:11:44 -08001313 name, i);
1314 } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
1315 // Warn about bad args and skip them.
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001316 printf("%s(): error parsing \"%s\" as sha-1; skipping",
1317 name, args[i]->data);
Doug Zongker512536a2010-02-17 16:11:44 -08001318 } else if (memcmp(digest, arg_digest, SHA_DIGEST_SIZE) == 0) {
1319 break;
1320 }
1321 FreeValue(args[i]);
1322 }
1323 if (i >= argc) {
1324 // Didn't match any of the hex strings; return false.
1325 return StringValue(strdup(""));
1326 }
1327 // Found a match; free all the remaining arguments and return the
1328 // matched one.
1329 int j;
1330 for (j = i+1; j < argc; ++j) {
1331 FreeValue(args[j]);
1332 }
1333 return args[i];
1334}
1335
Hristo Bojinovdb314d62010-08-02 10:29:49 -07001336// Read a local file and return its contents (the Value* returned
Doug Zongker512536a2010-02-17 16:11:44 -08001337// is actually a FileContents*).
1338Value* ReadFileFn(const char* name, State* state, int argc, Expr* argv[]) {
1339 if (argc != 1) {
1340 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1341 }
1342 char* filename;
1343 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1344
1345 Value* v = malloc(sizeof(Value));
1346 v->type = VAL_BLOB;
1347
1348 FileContents fc;
Doug Zongkera1bc1482014-02-13 15:18:19 -08001349 if (LoadFileContents(filename, &fc) != 0) {
Doug Zongker512536a2010-02-17 16:11:44 -08001350 free(filename);
Michael Runge6eed2242013-12-13 17:13:11 -08001351 v->size = -1;
1352 v->data = NULL;
Doug Zongker512536a2010-02-17 16:11:44 -08001353 free(fc.data);
Michael Runge6eed2242013-12-13 17:13:11 -08001354 return v;
Doug Zongker512536a2010-02-17 16:11:44 -08001355 }
1356
1357 v->size = fc.size;
1358 v->data = (char*)fc.data;
1359
1360 free(filename);
1361 return v;
1362}
Doug Zongker8edb00c2009-06-11 17:21:44 -07001363
Doug Zongkerc87bab12013-11-25 13:53:25 -08001364// Immediately reboot the device. Recovery is not finished normally,
1365// so if you reboot into recovery it will re-start applying the
1366// current package (because nothing has cleared the copy of the
1367// arguments stored in the BCB).
1368//
1369// The argument is the partition name passed to the android reboot
1370// property. It can be "recovery" to boot from the recovery
1371// partition, or "" (empty string) to boot from the regular boot
1372// partition.
1373Value* RebootNowFn(const char* name, State* state, int argc, Expr* argv[]) {
1374 if (argc != 2) {
1375 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1376 }
1377
1378 char* filename;
1379 char* property;
1380 if (ReadArgs(state, argv, 2, &filename, &property) < 0) return NULL;
1381
1382 char buffer[80];
1383
1384 // zero out the 'command' field of the bootloader message.
1385 memset(buffer, 0, sizeof(((struct bootloader_message*)0)->command));
1386 FILE* f = fopen(filename, "r+b");
1387 fseek(f, offsetof(struct bootloader_message, command), SEEK_SET);
1388 fwrite(buffer, sizeof(((struct bootloader_message*)0)->command), 1, f);
1389 fclose(f);
1390 free(filename);
1391
1392 strcpy(buffer, "reboot,");
1393 if (property != NULL) {
1394 strncat(buffer, property, sizeof(buffer)-10);
1395 }
1396
1397 property_set(ANDROID_RB_PROPERTY, buffer);
1398
1399 sleep(5);
1400 free(property);
1401 ErrorAbort(state, "%s() failed to reboot", name);
1402 return NULL;
1403}
1404
1405// Store a string value somewhere that future invocations of recovery
1406// can access it. This value is called the "stage" and can be used to
1407// drive packages that need to do reboots in the middle of
1408// installation and keep track of where they are in the multi-stage
1409// install.
1410//
1411// The first argument is the block device for the misc partition
1412// ("/misc" in the fstab), which is where this value is stored. The
1413// second argument is the string to store; it should not exceed 31
1414// bytes.
1415Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
1416 if (argc != 2) {
1417 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1418 }
1419
1420 char* filename;
1421 char* stagestr;
1422 if (ReadArgs(state, argv, 2, &filename, &stagestr) < 0) return NULL;
1423
1424 // Store this value in the misc partition, immediately after the
1425 // bootloader message that the main recovery uses to save its
1426 // arguments in case of the device restarting midway through
1427 // package installation.
1428 FILE* f = fopen(filename, "r+b");
1429 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1430 int to_write = strlen(stagestr)+1;
1431 int max_size = sizeof(((struct bootloader_message*)0)->stage);
1432 if (to_write > max_size) {
1433 to_write = max_size;
1434 stagestr[max_size-1] = 0;
1435 }
1436 fwrite(stagestr, to_write, 1, f);
1437 fclose(f);
1438
1439 free(stagestr);
1440 return StringValue(filename);
1441}
1442
1443// Return the value most recently saved with SetStageFn. The argument
1444// is the block device for the misc partition.
1445Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001446 if (argc != 1) {
Doug Zongkerc87bab12013-11-25 13:53:25 -08001447 return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc);
1448 }
1449
1450 char* filename;
1451 if (ReadArgs(state, argv, 1, &filename) < 0) return NULL;
1452
1453 char buffer[sizeof(((struct bootloader_message*)0)->stage)];
1454 FILE* f = fopen(filename, "rb");
1455 fseek(f, offsetof(struct bootloader_message, stage), SEEK_SET);
1456 fread(buffer, sizeof(buffer), 1, f);
1457 fclose(f);
1458 buffer[sizeof(buffer)-1] = '\0';
1459
1460 return StringValue(strdup(buffer));
1461}
1462
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001463Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) {
1464 if (argc != 2) {
1465 return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc);
1466 }
1467
1468 char* filename;
1469 char* len_str;
1470 if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL;
1471
1472 size_t len = strtoull(len_str, NULL, 0);
1473 int fd = open(filename, O_WRONLY, 0644);
1474 int success = wipe_block_device(fd, len);
1475
1476 free(filename);
1477 free(len_str);
1478
1479 close(fd);
1480
1481 return StringValue(strdup(success ? "t" : ""));
1482}
1483
Doug Zongkerc704e062014-05-23 08:40:35 -07001484Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) {
1485 if (argc != 0) {
1486 return ErrorAbort(state, "%s() expects no args, got %d", name, argc);
1487 }
1488 UpdaterInfo* ui = (UpdaterInfo*)(state->cookie);
1489 fprintf(ui->cmd_pipe, "enable_reboot\n");
1490 return StringValue(strdup("t"));
1491}
1492
Michael Rungeb278c252014-11-21 00:12:28 -08001493Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) {
1494 if (argc == 0) {
1495 return ErrorAbort(state, "%s() expects args, got %d", name, argc);
1496 }
1497
1498 char** args = ReadVarArgs(state, argc, argv);
1499 if (args == NULL) {
1500 return ErrorAbort(state, "%s() could not read args", name);
1501 }
1502
1503 int i;
1504 char** args2 = malloc(sizeof(char*) * (argc+1));
1505 // Tune2fs expects the program name as its args[0]
1506 args2[0] = strdup(name);
1507 for (i = 0; i < argc; ++i) {
1508 args2[i + 1] = args[i];
1509 }
1510 int result = tune2fs_main(argc + 1, args2);
1511 for (i = 0; i < argc; ++i) {
1512 free(args[i]);
1513 }
1514 free(args);
1515
1516 free(args2[0]);
1517 free(args2);
1518 if (result != 0) {
1519 return ErrorAbort(state, "%s() returned error code %d", name, result);
1520 }
1521 return StringValue(strdup("t"));
1522}
1523
Doug Zongker9931f7f2009-06-10 14:11:53 -07001524void RegisterInstallFunctions() {
1525 RegisterFunction("mount", MountFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001526 RegisterFunction("is_mounted", IsMountedFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001527 RegisterFunction("unmount", UnmountFn);
1528 RegisterFunction("format", FormatFn);
1529 RegisterFunction("show_progress", ShowProgressFn);
Doug Zongkerfbf3c102009-06-24 09:36:20 -07001530 RegisterFunction("set_progress", SetProgressFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001531 RegisterFunction("delete", DeleteFn);
1532 RegisterFunction("delete_recursive", DeleteFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001533 RegisterFunction("package_extract_dir", PackageExtractDirFn);
1534 RegisterFunction("package_extract_file", PackageExtractFileFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001535 RegisterFunction("symlink", SymlinkFn);
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001536
Nick Kralevich5dbdef02013-09-07 14:41:06 -07001537 // Usage:
1538 // set_metadata("filename", "key1", "value1", "key2", "value2", ...)
1539 // Example:
1540 // set_metadata("/system/bin/netcfg", "uid", 0, "gid", 3003, "mode", 02750, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1541 RegisterFunction("set_metadata", SetMetadataFn);
1542
1543 // Usage:
1544 // set_metadata_recursive("dirname", "key1", "value1", "key2", "value2", ...)
1545 // Example:
1546 // set_metadata_recursive("/system", "uid", 0, "gid", 0, "fmode", 0644, "dmode", 0755, "selabel", "u:object_r:system_file:s0", "capabilities", 0x0);
1547 RegisterFunction("set_metadata_recursive", SetMetadataFn);
1548
Doug Zongker8edb00c2009-06-11 17:21:44 -07001549 RegisterFunction("getprop", GetPropFn);
Doug Zongker47cace92009-06-18 10:11:50 -07001550 RegisterFunction("file_getprop", FileGetPropFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001551 RegisterFunction("write_raw_image", WriteRawImageFn);
Doug Zongker8edb00c2009-06-11 17:21:44 -07001552
1553 RegisterFunction("apply_patch", ApplyPatchFn);
Doug Zongkerc4351c72010-02-22 14:46:32 -08001554 RegisterFunction("apply_patch_check", ApplyPatchCheckFn);
1555 RegisterFunction("apply_patch_space", ApplyPatchSpaceFn);
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001556
Doug Zongkerc9d6e4f2014-02-24 16:02:50 -08001557 RegisterFunction("wipe_block_device", WipeBlockDeviceFn);
Doug Zongker52b40362014-02-10 15:30:30 -08001558
Doug Zongker512536a2010-02-17 16:11:44 -08001559 RegisterFunction("read_file", ReadFileFn);
1560 RegisterFunction("sha1_check", Sha1CheckFn);
Michael Rungece7ca712013-11-06 17:42:20 -08001561 RegisterFunction("rename", RenameFn);
Doug Zongker512536a2010-02-17 16:11:44 -08001562
Doug Zongkerd0181b82011-10-19 10:51:12 -07001563 RegisterFunction("wipe_cache", WipeCacheFn);
1564
Doug Zongkerd9c9d102009-06-12 12:24:39 -07001565 RegisterFunction("ui_print", UIPrintFn);
Doug Zongkera3f89ea2009-09-10 14:10:48 -07001566
1567 RegisterFunction("run_program", RunProgramFn);
Doug Zongkerc87bab12013-11-25 13:53:25 -08001568
1569 RegisterFunction("reboot_now", RebootNowFn);
1570 RegisterFunction("get_stage", GetStageFn);
1571 RegisterFunction("set_stage", SetStageFn);
Doug Zongkerc704e062014-05-23 08:40:35 -07001572
1573 RegisterFunction("enable_reboot", EnableRebootFn);
Michael Rungeb278c252014-11-21 00:12:28 -08001574 RegisterFunction("tune2fs", Tune2FsFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -07001575}