1a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat/*
2a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat * Copyright (C) 2008 The Android Open Source Project
3a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat *
4a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat * Licensed under the Apache License, Version 2.0 (the "License");
5a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat * you may not use this file except in compliance with the License.
6a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat * You may obtain a copy of the License at
7a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat *
8a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat *      http://www.apache.org/licenses/LICENSE-2.0
9a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat *
10a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat * Unless required by applicable law or agreed to in writing, software
11a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat * distributed under the License is distributed on an "AS IS" BASIS,
12a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat * See the License for the specific language governing permissions and
14a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat * limitations under the License.
15a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat */
16a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
17a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat#include <stdio.h>
1837dcda68d334f70e1f7f69a9817def65fe3ee717Olivier Bailly#include <stdlib.h>
19a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat#include <fcntl.h>
20a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat#include <unistd.h>
21a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat#include <errno.h>
22a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat#include <string.h>
23a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
24344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include <sys/mount.h>
258da6bcb006f4e4257cdd685e35e910a6334f6ceaSan Mehat#include <sys/types.h>
268da6bcb006f4e4257cdd685e35e910a6334f6ceaSan Mehat#include <sys/stat.h>
2737dcda68d334f70e1f7f69a9817def65fe3ee717Olivier Bailly#include <sys/ioctl.h>
288da6bcb006f4e4257cdd685e35e910a6334f6ceaSan Mehat
29d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat#include <linux/kdev_t.h>
30d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
31a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat#define LOG_TAG "Vold"
32a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
33a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat#include <cutils/log.h>
34a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
35d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat#include <sysutils/SocketClient.h>
36a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat#include "Loop.h"
37344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root#include "Asec.h"
38a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
39d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehatint Loop::dumpState(SocketClient *c) {
40d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    int i;
41d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    int fd;
42d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    char filename[256];
43d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
44d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    for (i = 0; i < LOOP_MAX; i++) {
45508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        struct loop_info64 li;
46d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        int rc;
47d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
48d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        sprintf(filename, "/dev/block/loop%d", i);
49d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
50d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        if ((fd = open(filename, O_RDWR)) < 0) {
51d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat            if (errno != ENOENT) {
5297ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat                SLOGE("Unable to open %s (%s)", filename, strerror(errno));
53d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat            } else {
54d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat                continue;
55d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat            }
56d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat            return -1;
57d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        }
58d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
59508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        rc = ioctl(fd, LOOP_GET_STATUS64, &li);
60d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        close(fd);
61d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        if (rc < 0 && errno == ENXIO) {
62d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat            continue;
63d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        }
64d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
65d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        if (rc < 0) {
6697ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat            SLOGE("Unable to get loop status for %s (%s)", filename,
67d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat                 strerror(errno));
68d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat            return -1;
69d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        }
70d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        char *tmp = NULL;
71508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        asprintf(&tmp, "%s %d %lld:%lld %llu %lld:%lld %lld 0x%x {%s} {%s}", filename, li.lo_number,
72d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat                MAJOR(li.lo_device), MINOR(li.lo_device), li.lo_inode, MAJOR(li.lo_rdevice),
73508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root                        MINOR(li.lo_rdevice), li.lo_offset, li.lo_flags, li.lo_crypt_name,
74508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root                        li.lo_file_name);
75d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        c->sendMsg(0, tmp, false);
76d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat        free(tmp);
77d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    }
78d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat    return 0;
79d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat}
80d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat
81d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehatint Loop::lookupActive(const char *id, char *buffer, size_t len) {
82a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    int i;
83a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    int fd;
84a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    char filename[256];
85a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
86a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    memset(buffer, 0, len);
87a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
88a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    for (i = 0; i < LOOP_MAX; i++) {
89508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        struct loop_info64 li;
90a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        int rc;
91a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
92a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        sprintf(filename, "/dev/block/loop%d", i);
93a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
94a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        if ((fd = open(filename, O_RDWR)) < 0) {
95b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat            if (errno != ENOENT) {
9697ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat                SLOGE("Unable to open %s (%s)", filename, strerror(errno));
97d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat            } else {
98d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehat                continue;
99b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat            }
100a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat            return -1;
101a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        }
102a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
103508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        rc = ioctl(fd, LOOP_GET_STATUS64, &li);
104a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        close(fd);
105a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        if (rc < 0 && errno == ENXIO) {
106a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat            continue;
107a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        }
108a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
109a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        if (rc < 0) {
11097ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat            SLOGE("Unable to get loop status for %s (%s)", filename,
111a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat                 strerror(errno));
112a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat            return -1;
113a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        }
114508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root        if (!strncmp((const char*) li.lo_crypt_name, id, LO_NAME_SIZE)) {
115a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat            break;
116a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        }
117a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
118a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
119a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    if (i == LOOP_MAX) {
120a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        errno = ENOENT;
121a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        return -1;
122a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
123a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    strncpy(buffer, filename, len -1);
124a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    return 0;
125a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat}
126a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
127d9a4e358614a0c5f60cc76c0636ee4bb02004a32San Mehatint Loop::create(const char *id, const char *loopFile, char *loopDeviceBuffer, size_t len) {
128a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    int i;
129a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    int fd;
130a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    char filename[256];
131a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
132a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    for (i = 0; i < LOOP_MAX; i++) {
1337c16502dbc16658c45165edb1c54df6b8b02d9caKenny Root        struct loop_info64 li;
134a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        int rc;
135a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
136a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        sprintf(filename, "/dev/block/loop%d", i);
137a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
1388da6bcb006f4e4257cdd685e35e910a6334f6ceaSan Mehat        /*
1398da6bcb006f4e4257cdd685e35e910a6334f6ceaSan Mehat         * The kernel starts us off with 8 loop nodes, but more
1408da6bcb006f4e4257cdd685e35e910a6334f6ceaSan Mehat         * are created on-demand if needed.
1418da6bcb006f4e4257cdd685e35e910a6334f6ceaSan Mehat         */
1428da6bcb006f4e4257cdd685e35e910a6334f6ceaSan Mehat        mode_t mode = 0660 | S_IFBLK;
143b78a32c1d5eeb243bdac0eaf18effb1897f1ee67San Mehat        unsigned int dev = (0xff & i) | ((i << 12) & 0xfff00000) | (7 << 8);
1448da6bcb006f4e4257cdd685e35e910a6334f6ceaSan Mehat        if (mknod(filename, mode, dev) < 0) {
1458da6bcb006f4e4257cdd685e35e910a6334f6ceaSan Mehat            if (errno != EEXIST) {
14697ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat                SLOGE("Error creating loop device node (%s)", strerror(errno));
1478da6bcb006f4e4257cdd685e35e910a6334f6ceaSan Mehat                return -1;
1488da6bcb006f4e4257cdd685e35e910a6334f6ceaSan Mehat            }
1498da6bcb006f4e4257cdd685e35e910a6334f6ceaSan Mehat        }
1508da6bcb006f4e4257cdd685e35e910a6334f6ceaSan Mehat
151a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        if ((fd = open(filename, O_RDWR)) < 0) {
15297ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat            SLOGE("Unable to open %s (%s)", filename, strerror(errno));
153a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat            return -1;
154a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        }
155a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
1567c16502dbc16658c45165edb1c54df6b8b02d9caKenny Root        rc = ioctl(fd, LOOP_GET_STATUS64, &li);
157a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        if (rc < 0 && errno == ENXIO)
158a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat            break;
159a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
1608da6bcb006f4e4257cdd685e35e910a6334f6ceaSan Mehat        close(fd);
1618da6bcb006f4e4257cdd685e35e910a6334f6ceaSan Mehat
162a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        if (rc < 0) {
16397ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat            SLOGE("Unable to get loop status for %s (%s)", filename,
164a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat                 strerror(errno));
165a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat            return -1;
166a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        }
167a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
168a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
169a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    if (i == LOOP_MAX) {
17097ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Exhausted all loop devices");
171a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        errno = ENOSPC;
172a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        return -1;
173a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
174a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
1758da6bcb006f4e4257cdd685e35e910a6334f6ceaSan Mehat    strncpy(loopDeviceBuffer, filename, len -1);
176a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
1778da6bcb006f4e4257cdd685e35e910a6334f6ceaSan Mehat    int file_fd;
178a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
179a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    if ((file_fd = open(loopFile, O_RDWR)) < 0) {
18097ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Unable to open %s (%s)", loopFile, strerror(errno));
181a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        close(fd);
182a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        return -1;
183a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
184a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
185a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    if (ioctl(fd, LOOP_SET_FD, file_fd) < 0) {
18697ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Error setting up loopback interface (%s)", strerror(errno));
187a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        close(file_fd);
188a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        close(fd);
189a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        return -1;
190a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
191a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
192508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root    struct loop_info64 li;
193a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
194a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    memset(&li, 0, sizeof(li));
195092aa1c585fedd9e169eece41b8a471f1739908aPeter Bohm    strlcpy((char*) li.lo_crypt_name, id, LO_NAME_SIZE);
196092aa1c585fedd9e169eece41b8a471f1739908aPeter Bohm    strlcpy((char*) li.lo_file_name, loopFile, LO_NAME_SIZE);
197a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
198508c0e1605b795bbb51cb47d955b89f3df26ca94Kenny Root    if (ioctl(fd, LOOP_SET_STATUS64, &li) < 0) {
19997ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Error setting loopback status (%s)", strerror(errno));
200a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        close(file_fd);
201a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        close(fd);
202a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        return -1;
203a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
204a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
205a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    close(fd);
206a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    close(file_fd);
207a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
208a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    return 0;
209a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat}
210a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
211a19b250bd273455933ca3502cf2c2e0a803aff77San Mehatint Loop::destroyByDevice(const char *loopDevice) {
212a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    int device_fd;
213a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
214a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    device_fd = open(loopDevice, O_RDONLY);
215a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    if (device_fd < 0) {
21697ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Failed to open loop (%d)", errno);
217a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        return -1;
218a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
219a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
220a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) {
22197ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Failed to destroy loop (%d)", errno);
222a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        close(device_fd);
223a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        return -1;
224a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
225a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
226a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    close(device_fd);
227a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    return 0;
228a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat}
229a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
230a19b250bd273455933ca3502cf2c2e0a803aff77San Mehatint Loop::destroyByFile(const char *loopFile) {
231a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    errno = ENOSYS;
232a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    return -1;
233a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat}
234a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
2358b8f71b1d760411279f3b07a5c97709f052c689eSan Mehatint Loop::createImageFile(const char *file, unsigned int numSectors) {
236a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    int fd;
237a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
238a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    if ((fd = creat(file, 0600)) < 0) {
23997ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Error creating imagefile (%s)", strerror(errno));
240a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        return -1;
241a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
242a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat
2438b8f71b1d760411279f3b07a5c97709f052c689eSan Mehat    if (ftruncate(fd, numSectors * 512) < 0) {
24497ac40e4e6f3ed0bd5d6878d7d8d4a54fcaecb76San Mehat        SLOGE("Error truncating imagefile (%s)", strerror(errno));
245a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        close(fd);
246a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat        return -1;
247a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    }
248a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    close(fd);
249a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat    return 0;
250a19b250bd273455933ca3502cf2c2e0a803aff77San Mehat}
251344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
252344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Rootint Loop::lookupInfo(const char *loopDevice, struct asec_superblock *sb, unsigned int *nr_sec) {
253344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    int fd;
254344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    struct asec_superblock buffer;
255344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
256344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if ((fd = open(loopDevice, O_RDONLY)) < 0) {
257344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("Failed to open loopdevice (%s)", strerror(errno));
258344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        destroyByDevice(loopDevice);
259344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
260344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
261344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
262344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (ioctl(fd, BLKGETSIZE, nr_sec)) {
263344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("Failed to get loop size (%s)", strerror(errno));
264344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        destroyByDevice(loopDevice);
265344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        close(fd);
266344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
267344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
268344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
269344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    /*
270344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root     * Try to read superblock.
271344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root     */
272344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    memset(&buffer, 0, sizeof(struct asec_superblock));
273344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (lseek(fd, ((*nr_sec - 1) * 512), SEEK_SET) < 0) {
274344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("lseek failed (%s)", strerror(errno));
275344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        close(fd);
276344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        destroyByDevice(loopDevice);
277344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
278344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
279344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    if (read(fd, &buffer, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
280344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        SLOGE("superblock read failed (%s)", strerror(errno));
281344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        close(fd);
282344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        destroyByDevice(loopDevice);
283344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root        return -1;
284344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    }
285344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    close(fd);
286344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root
287344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    /*
288344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root     * Superblock successfully read. Copy to caller's struct.
289344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root     */
290344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    memcpy(sb, &buffer, sizeof(struct asec_superblock));
291344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root    return 0;
292344ca10856f3d3087a3288ce8f91ad83665d93fbKenny Root}
293