blob: 2973ff82eb85add7133344fd956ceca24fd3b47e [file] [log] [blame]
San Mehat2350c442010-03-02 13:16:50 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <fcntl.h>
19#include <unistd.h>
20#include <errno.h>
21#include <string.h>
22
23#include <sys/types.h>
24#include <sys/stat.h>
25
26#define LOG_TAG "Vold"
27
28#include <cutils/log.h>
29
30#include "Xwarp.h"
31const char *Xwarp::XWARP_BACKINGFILE = "/mnt/secure/asec/xwarp.img";
32const char *Xwarp::XWARP_CFG = "/sys/fs/yaffs/mtd3/xwarp-backing-store";
33const char *Xwarp::XWARP_READY = "/sys/fs/yaffs/mtd3/xwarp-ready";
34const char *Xwarp::XWARP_MIRROR_STATUS = "/sys/fs/yaffs/mtd3/xwarp-mirror";
35
36int Xwarp::enable() {
37 return doEnableDisable(true);
38}
39
40int Xwarp::disable() {
41 return doEnableDisable(false);
42}
43
44int Xwarp::status(bool *ready, unsigned *mirrorPos, unsigned *maxSize) {
45 FILE *fp;
46
47 *ready = false;
48 *mirrorPos = 0;
49 *maxSize = 0;
50 if (!(fp = fopen(XWARP_READY, "r"))) {
51 return -1;
52 }
53
54 fscanf(fp, "%d", (int *) ready);
55 fclose(fp);
56
57 if (!(fp = fopen(XWARP_MIRROR_STATUS, "r"))) {
58 return -1;
59 }
60
61 fscanf(fp, "%u %u", mirrorPos, maxSize);
62 fclose(fp);
63 return 0;
64}
65
66int Xwarp::doEnableDisable(bool enable) {
67 const char *tmp;
68 int fd = open(XWARP_CFG, O_WRONLY);
69
70 if (fd < 0)
71 return -1;
72
73 tmp = (enable ? XWARP_BACKINGFILE : "");
74
75 if (write(fd, tmp, strlen(tmp)+1) < 0) {
San Mehat97ac40e2010-03-24 10:24:19 -070076 SLOGE("Failed to write xwarp cfg (%s)", strerror(errno));
San Mehat2350c442010-03-02 13:16:50 -080077 close(fd);
78 return -1;
79 }
80
81 close(fd);
82 return 0;
83}