18f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall/*
28f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall * Copyright (C) 2010 The Android Open Source Project
38f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall *
48f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall * Licensed under the Apache License, Version 2.0 (the "License");
58f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall * you may not use this file except in compliance with the License.
68f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall * You may obtain a copy of the License at
78f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall *
88f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall *      http://www.apache.org/licenses/LICENSE-2.0
98f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall *
108f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall * Unless required by applicable law or agreed to in writing, software
118f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall * distributed under the License is distributed on an "AS IS" BASIS,
128f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall * See the License for the specific language governing permissions and
148f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall * limitations under the License.
158f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall */
168f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
178f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall/* TO DO:
188f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall *   1.  Perhaps keep several copies of the encrypted key, in case something
198f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall *       goes horribly wrong?
208f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall *
218f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall */
228f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
238f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall#include <sys/types.h>
248f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall#include <sys/stat.h>
258f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall#include <fcntl.h>
268f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall#include <unistd.h>
278f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall#include <stdio.h>
288f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall#include <sys/ioctl.h>
298f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall#include <linux/dm-ioctl.h>
308f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall#include <libgen.h>
318f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall#include <stdlib.h>
328f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall#include <sys/param.h>
338f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall#include <string.h>
348f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall#include <sys/mount.h>
358f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall#include <openssl/evp.h>
368ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall#include <openssl/sha.h>
378f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall#include <errno.h>
38c290eaf6852c6318584926c5e39b27672638891fKen Sumrall#include <cutils/android_reboot.h>
393ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall#include <ext4.h>
4029d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall#include <linux/kdev_t.h>
41e5032c42da3c33a854df0a24a7968b4ab54190b9Ken Sumrall#include <fs_mgr.h>
428f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall#include "cryptfs.h"
438f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall#define LOG_TAG "Cryptfs"
44ee6d8c42f337ea1446a319df53f6d1a96afbd209Mike Lockwood#include "cutils/android_reboot.h"
458f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall#include "cutils/log.h"
468f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall#include "cutils/properties.h"
475d4c68e40700424b65a4331be75620706a0dd49cKen Sumrall#include "hardware_legacy/power.h"
4829d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall#include "VolumeManager.h"
498f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
508f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall#define DM_CRYPT_BUF_SIZE 4096
518ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall#define DATA_MNT_POINT "/data"
528f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
5370a4b3fd7a84a84bbe6e9d6d4ca3ee2098259fd9Jason parks#define HASH_COUNT 2000
5470a4b3fd7a84a84bbe6e9d6d4ca3ee2098259fd9Jason parks#define KEY_LEN_BYTES 16
5570a4b3fd7a84a84bbe6e9d6d4ca3ee2098259fd9Jason parks#define IV_LEN_BYTES 16
5670a4b3fd7a84a84bbe6e9d6d4ca3ee2098259fd9Jason parks
5729d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall#define KEY_IN_FOOTER  "footer"
5829d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall
5929d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall#define EXT4_FS 1
6029d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall#define FAT_FS 2
6129d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall
62e919efea94b178ed214ed2e78ef0d008727d62abKen Sumrall#define TABLE_LOAD_RETRIES 10
63e919efea94b178ed214ed2e78ef0d008727d62abKen Sumrall
648f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrallchar *me = "cryptfs";
658f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
6670a4b3fd7a84a84bbe6e9d6d4ca3ee2098259fd9Jason parksstatic unsigned char saved_master_key[KEY_LEN_BYTES];
6729d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrallstatic char *saved_data_blkdev;
683ad9072a5d6f6bda32123b367545649364e3c11dKen Sumrallstatic char *saved_mount_point;
6970a4b3fd7a84a84bbe6e9d6d4ca3ee2098259fd9Jason parksstatic int  master_key_saved = 0;
70e5032c42da3c33a854df0a24a7968b4ab54190b9Ken Sumrall#define FSTAB_PREFIX "/fstab."
71e5032c42da3c33a854df0a24a7968b4ab54190b9Ken Sumrallstatic char fstab_filename[PROPERTY_VALUE_MAX + sizeof(FSTAB_PREFIX)];
728ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall
738f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrallstatic void ioctl_init(struct dm_ioctl *io, size_t dataSize, const char *name, unsigned flags)
748f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall{
758f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    memset(io, 0, dataSize);
768f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    io->data_size = dataSize;
778f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    io->data_start = sizeof(struct dm_ioctl);
788f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    io->version[0] = 4;
798f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    io->version[1] = 0;
808f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    io->version[2] = 0;
818f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    io->flags = flags;
828f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    if (name) {
838f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        strncpy(io->name, name, sizeof(io->name));
848f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    }
858f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall}
868f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
873ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrallstatic unsigned int get_fs_size(char *dev)
883ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall{
893ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall    int fd, block_size;
903ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall    struct ext4_super_block sb;
913ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall    off64_t len;
923ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall
933ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall    if ((fd = open(dev, O_RDONLY)) < 0) {
943ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall        SLOGE("Cannot open device to get filesystem size ");
953ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall        return 0;
963ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall    }
973ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall
983ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall    if (lseek64(fd, 1024, SEEK_SET) < 0) {
993ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall        SLOGE("Cannot seek to superblock");
1003ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall        return 0;
1013ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall    }
1023ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall
1033ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall    if (read(fd, &sb, sizeof(sb)) != sizeof(sb)) {
1043ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall        SLOGE("Cannot read superblock");
1053ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall        return 0;
1063ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall    }
1073ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall
1083ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall    close(fd);
1093ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall
1103ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall    block_size = 1024 << sb.s_log_block_size;
1113ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall    /* compute length in bytes */
1123ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall    len = ( ((off64_t)sb.s_blocks_count_hi << 32) + sb.s_blocks_count_lo) * block_size;
1133ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall
1143ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall    /* return length in sectors */
1153ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall    return (unsigned int) (len / 512);
1163ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall}
1173ed8236de11a1be8b45d4c37b2208682f5e97c72Ken Sumrall
1188f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrallstatic unsigned int get_blkdev_size(int fd)
1198f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall{
1208f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  unsigned int nr_sec;
1218f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
1228f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  if ( (ioctl(fd, BLKGETSIZE, &nr_sec)) == -1) {
1238f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    nr_sec = 0;
1248f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
1258f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
1268f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  return nr_sec;
1278f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall}
1288f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
129e5032c42da3c33a854df0a24a7968b4ab54190b9Ken Sumrall/* Get and cache the name of the fstab file so we don't
130e5032c42da3c33a854df0a24a7968b4ab54190b9Ken Sumrall * keep talking over the socket to the property service.
131e5032c42da3c33a854df0a24a7968b4ab54190b9Ken Sumrall */
132e5032c42da3c33a854df0a24a7968b4ab54190b9Ken Sumrallstatic char *get_fstab_filename(void)
133e5032c42da3c33a854df0a24a7968b4ab54190b9Ken Sumrall{
134e5032c42da3c33a854df0a24a7968b4ab54190b9Ken Sumrall    if (fstab_filename[0] == 0) {
135e5032c42da3c33a854df0a24a7968b4ab54190b9Ken Sumrall        strcpy(fstab_filename, FSTAB_PREFIX);
136e5032c42da3c33a854df0a24a7968b4ab54190b9Ken Sumrall        property_get("ro.hardware", fstab_filename + sizeof(FSTAB_PREFIX) - 1, "");
137e5032c42da3c33a854df0a24a7968b4ab54190b9Ken Sumrall    }
138e5032c42da3c33a854df0a24a7968b4ab54190b9Ken Sumrall
139e5032c42da3c33a854df0a24a7968b4ab54190b9Ken Sumrall    return fstab_filename;
140e5032c42da3c33a854df0a24a7968b4ab54190b9Ken Sumrall}
141e5032c42da3c33a854df0a24a7968b4ab54190b9Ken Sumrall
142e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall/* key or salt can be NULL, in which case just skip writing that value.  Useful to
1438f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall * update the failed mount count but not change the key.
1448f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall */
1458f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrallstatic int put_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr,
146e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall                                  unsigned char *key, unsigned char *salt)
1478f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall{
1488f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  int fd;
1498f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  unsigned int nr_sec, cnt;
1508f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  off64_t off;
1518f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  int rc = -1;
15229d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall  char *fname;
15329d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall  char key_loc[PROPERTY_VALUE_MAX];
1543be890f59c04f94537f2f66f1d2841ed591f1a6eKen Sumrall  struct stat statbuf;
1558f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
156e5032c42da3c33a854df0a24a7968b4ab54190b9Ken Sumrall  fs_mgr_get_crypt_info(get_fstab_filename(), key_loc, 0, sizeof(key_loc));
1578f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
15829d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall  if (!strcmp(key_loc, KEY_IN_FOOTER)) {
15929d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    fname = real_blk_name;
16029d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    if ( (fd = open(fname, O_RDWR)) < 0) {
16129d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      SLOGE("Cannot open real block device %s\n", fname);
16229d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      return -1;
16329d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    }
1648f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
16529d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    if ( (nr_sec = get_blkdev_size(fd)) == 0) {
16629d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      SLOGE("Cannot get size of block device %s\n", fname);
16729d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      goto errout;
16829d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    }
1698f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
17029d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    /* If it's an encrypted Android partition, the last 16 Kbytes contain the
17129d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall     * encryption info footer and key, and plenty of bytes to spare for future
17229d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall     * growth.
17329d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall     */
17429d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
17529d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall
17629d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    if (lseek64(fd, off, SEEK_SET) == -1) {
17729d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      SLOGE("Cannot seek to real block device footer\n");
17829d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      goto errout;
17929d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    }
18029d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall  } else if (key_loc[0] == '/') {
18129d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    fname = key_loc;
18229d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    if ( (fd = open(fname, O_RDWR | O_CREAT, 0600)) < 0) {
18329d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      SLOGE("Cannot open footer file %s\n", fname);
18429d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      return -1;
18529d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    }
18629d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall  } else {
187e5032c42da3c33a854df0a24a7968b4ab54190b9Ken Sumrall    SLOGE("Unexpected value for crypto key location\n");
18829d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    return -1;;
1898f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
1908f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
1918f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  if ((cnt = write(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
1928f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    SLOGE("Cannot write real block device footer\n");
1938f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    goto errout;
1948f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
1958f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
1968f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  if (key) {
19770a4b3fd7a84a84bbe6e9d6d4ca3ee2098259fd9Jason parks    if (crypt_ftr->keysize != KEY_LEN_BYTES) {
1988f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall      SLOGE("Keysize of %d bits not supported for real block device %s\n",
19929d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall            crypt_ftr->keysize*8, fname);
2008f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall      goto errout;
2018f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    }
2028f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
2038f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    if ( (cnt = write(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) {
20429d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      SLOGE("Cannot write key for real block device %s\n", fname);
2058f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall      goto errout;
2068f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    }
2078f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
2088f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
209e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall  if (salt) {
21029d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    /* Compute the offset from the last write to the salt */
21129d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    off = KEY_TO_SALT_PADDING;
21229d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    if (! key)
21329d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      off += crypt_ftr->keysize;
214e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall
21529d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    if (lseek64(fd, off, SEEK_CUR) == -1) {
216e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall      SLOGE("Cannot seek to real block device salt \n");
217e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall      goto errout;
218e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall    }
219e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall
220e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall    if ( (cnt = write(fd, salt, SALT_LEN)) != SALT_LEN) {
22129d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      SLOGE("Cannot write salt for real block device %s\n", fname);
22229d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      goto errout;
22329d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    }
22429d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall  }
22529d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall
2263be890f59c04f94537f2f66f1d2841ed591f1a6eKen Sumrall  fstat(fd, &statbuf);
2273be890f59c04f94537f2f66f1d2841ed591f1a6eKen Sumrall  /* If the keys are kept on a raw block device, do not try to truncate it. */
2283be890f59c04f94537f2f66f1d2841ed591f1a6eKen Sumrall  if (S_ISREG(statbuf.st_mode) && (key_loc[0] == '/')) {
22929d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    if (ftruncate(fd, 0x4000)) {
2303be890f59c04f94537f2f66f1d2841ed591f1a6eKen Sumrall      SLOGE("Cannot set footer file size\n", fname);
231e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall      goto errout;
232e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall    }
233e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall  }
234e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall
2358f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  /* Success! */
2368f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  rc = 0;
2378f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
2388f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrallerrout:
2398f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  close(fd);
2408f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  return rc;
2418f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
2428f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall}
2438f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
2448f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrallstatic int get_crypt_ftr_and_key(char *real_blk_name, struct crypt_mnt_ftr *crypt_ftr,
245e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall                                  unsigned char *key, unsigned char *salt)
2468f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall{
2478f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  int fd;
2488f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  unsigned int nr_sec, cnt;
2498f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  off64_t off;
2508f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  int rc = -1;
25129d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall  char key_loc[PROPERTY_VALUE_MAX];
25229d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall  char *fname;
25329d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall  struct stat statbuf;
2548f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
255e5032c42da3c33a854df0a24a7968b4ab54190b9Ken Sumrall  fs_mgr_get_crypt_info(get_fstab_filename(), key_loc, 0, sizeof(key_loc));
2568f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
25729d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall  if (!strcmp(key_loc, KEY_IN_FOOTER)) {
25829d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    fname = real_blk_name;
25929d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    if ( (fd = open(fname, O_RDONLY)) < 0) {
26029d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      SLOGE("Cannot open real block device %s\n", fname);
26129d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      return -1;
26229d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    }
2638f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
26429d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    if ( (nr_sec = get_blkdev_size(fd)) == 0) {
26529d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      SLOGE("Cannot get size of block device %s\n", fname);
26629d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      goto errout;
26729d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    }
2688f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
26929d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    /* If it's an encrypted Android partition, the last 16 Kbytes contain the
27029d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall     * encryption info footer and key, and plenty of bytes to spare for future
27129d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall     * growth.
27229d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall     */
27329d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    off = ((off64_t)nr_sec * 512) - CRYPT_FOOTER_OFFSET;
27429d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall
27529d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    if (lseek64(fd, off, SEEK_SET) == -1) {
27629d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      SLOGE("Cannot seek to real block device footer\n");
27729d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      goto errout;
27829d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    }
27929d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall  } else if (key_loc[0] == '/') {
28029d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    fname = key_loc;
28129d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    if ( (fd = open(fname, O_RDONLY)) < 0) {
28229d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      SLOGE("Cannot open footer file %s\n", fname);
28329d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      return -1;
28429d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    }
28529d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall
28629d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    /* Make sure it's 16 Kbytes in length */
28729d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    fstat(fd, &statbuf);
2883be890f59c04f94537f2f66f1d2841ed591f1a6eKen Sumrall    if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
28929d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      SLOGE("footer file %s is not the expected size!\n", fname);
29029d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall      goto errout;
29129d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    }
29229d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall  } else {
293e5032c42da3c33a854df0a24a7968b4ab54190b9Ken Sumrall    SLOGE("Unexpected value for crypto key location\n");
29429d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    return -1;;
2958f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
2968f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
2978f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  if ( (cnt = read(fd, crypt_ftr, sizeof(struct crypt_mnt_ftr))) != sizeof(struct crypt_mnt_ftr)) {
2988f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    SLOGE("Cannot read real block device footer\n");
2998f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    goto errout;
3008f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
3018f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
3028f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  if (crypt_ftr->magic != CRYPT_MNT_MAGIC) {
30329d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    SLOGE("Bad magic for real block device %s\n", fname);
3048f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    goto errout;
3058f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
3068f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
3078f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  if (crypt_ftr->major_version != 1) {
3088f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    SLOGE("Cannot understand major version %d real block device footer\n",
3098f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall          crypt_ftr->major_version);
3108f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    goto errout;
3118f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
3128f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
3138f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  if (crypt_ftr->minor_version != 0) {
3148f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    SLOGW("Warning: crypto footer minor version %d, expected 0, continuing...\n",
3158f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall          crypt_ftr->minor_version);
3168f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
3178f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
3188f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  if (crypt_ftr->ftr_size > sizeof(struct crypt_mnt_ftr)) {
3198f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    /* the footer size is bigger than we expected.
3208f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall     * Skip to it's stated end so we can read the key.
3218f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall     */
3228f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    if (lseek(fd, crypt_ftr->ftr_size - sizeof(struct crypt_mnt_ftr),  SEEK_CUR) == -1) {
3238f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall      SLOGE("Cannot seek to start of key\n");
3248f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall      goto errout;
3258f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    }
3268f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
3278f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
32870a4b3fd7a84a84bbe6e9d6d4ca3ee2098259fd9Jason parks  if (crypt_ftr->keysize != KEY_LEN_BYTES) {
3298f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    SLOGE("Keysize of %d bits not supported for real block device %s\n",
33029d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall          crypt_ftr->keysize * 8, fname);
3318f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    goto errout;
3328f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
3338f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
3348f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  if ( (cnt = read(fd, key, crypt_ftr->keysize)) != crypt_ftr->keysize) {
33529d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    SLOGE("Cannot read key for real block device %s\n", fname);
3368f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    goto errout;
3378f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
3388f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
339e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall  if (lseek64(fd, KEY_TO_SALT_PADDING, SEEK_CUR) == -1) {
340e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall    SLOGE("Cannot seek to real block device salt\n");
341e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall    goto errout;
342e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall  }
343e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall
344e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall  if ( (cnt = read(fd, salt, SALT_LEN)) != SALT_LEN) {
34529d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall    SLOGE("Cannot read salt for real block device %s\n", fname);
346e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall    goto errout;
347e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall  }
348e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall
3498f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  /* Success! */
3508f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  rc = 0;
3518f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
3528f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrallerrout:
3538f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  close(fd);
3548f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  return rc;
3558f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall}
3568f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
3578f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall/* Convert a binary key of specified length into an ascii hex string equivalent,
3588f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall * without the leading 0x and with null termination
3598f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall */
3608f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrallvoid convert_key_to_hex_ascii(unsigned char *master_key, unsigned int keysize,
3618f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall                              char *master_key_ascii)
3628f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall{
3638f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  unsigned int i, a;
3648f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  unsigned char nibble;
3658f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
3668f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  for (i=0, a=0; i<keysize; i++, a+=2) {
3678f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    /* For each byte, write out two ascii hex digits */
3688f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    nibble = (master_key[i] >> 4) & 0xf;
3698f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    master_key_ascii[a] = nibble + (nibble > 9 ? 0x37 : 0x30);
3708f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
3718f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    nibble = master_key[i] & 0xf;
3728f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    master_key_ascii[a+1] = nibble + (nibble > 9 ? 0x37 : 0x30);
3738f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
3748f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
3758f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  /* Add the null termination */
3768f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  master_key_ascii[a] = '\0';
3778f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
3788f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall}
3798f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
3808f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrallstatic int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char *master_key,
38129d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall                                    char *real_blk_name, char *crypto_blk_name, const char *name)
3828f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall{
3838f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  char buffer[DM_CRYPT_BUF_SIZE];
3848f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  char master_key_ascii[129]; /* Large enough to hold 512 bit key and null */
3858f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  char *crypt_params;
3868f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  struct dm_ioctl *io;
3878f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  struct dm_target_spec *tgt;
3888f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  unsigned int minor;
3898f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  int fd;
390e919efea94b178ed214ed2e78ef0d008727d62abKen Sumrall  int i;
3918f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  int retval = -1;
3928f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
3938f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
3948f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    SLOGE("Cannot open device-mapper\n");
3958f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    goto errout;
3968f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
3978f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
3988f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  io = (struct dm_ioctl *) buffer;
3998f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
4008f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
4018f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  if (ioctl(fd, DM_DEV_CREATE, io)) {
4028f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    SLOGE("Cannot create dm-crypt device\n");
4038f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    goto errout;
4048f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
4058f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
4068f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  /* Get the device status, in particular, the name of it's device file */
4078f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
4088f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  if (ioctl(fd, DM_DEV_STATUS, io)) {
4098f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    SLOGE("Cannot retrieve dm-crypt device status\n");
4108f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    goto errout;
4118f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
4128f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
4138f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
4148f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
4158f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  /* Load the mapping table for this device */
4168f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  tgt = (struct dm_target_spec *) &buffer[sizeof(struct dm_ioctl)];
4178f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
4188f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  ioctl_init(io, 4096, name, 0);
4198f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  io->target_count = 1;
4208f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  tgt->status = 0;
4218f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  tgt->sector_start = 0;
4228f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  tgt->length = crypt_ftr->fs_size;
4238f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  strcpy(tgt->target_type, "crypt");
4248f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
4258f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
4268f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
4278f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  sprintf(crypt_params, "%s %s 0 %s 0", crypt_ftr->crypto_type_name,
4288f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall          master_key_ascii, real_blk_name);
4298f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  crypt_params += strlen(crypt_params) + 1;
4308f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  crypt_params = (char *) (((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
4318f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  tgt->next = crypt_params - buffer;
4328f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
433e919efea94b178ed214ed2e78ef0d008727d62abKen Sumrall  for (i = 0; i < TABLE_LOAD_RETRIES; i++) {
434e919efea94b178ed214ed2e78ef0d008727d62abKen Sumrall    if (! ioctl(fd, DM_TABLE_LOAD, io)) {
435e919efea94b178ed214ed2e78ef0d008727d62abKen Sumrall      break;
436e919efea94b178ed214ed2e78ef0d008727d62abKen Sumrall    }
437e919efea94b178ed214ed2e78ef0d008727d62abKen Sumrall    usleep(500000);
438e919efea94b178ed214ed2e78ef0d008727d62abKen Sumrall  }
439e919efea94b178ed214ed2e78ef0d008727d62abKen Sumrall
440e919efea94b178ed214ed2e78ef0d008727d62abKen Sumrall  if (i == TABLE_LOAD_RETRIES) {
4418f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall      SLOGE("Cannot load dm-crypt mapping table.\n");
4428f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall      goto errout;
443e919efea94b178ed214ed2e78ef0d008727d62abKen Sumrall  } else if (i) {
444e919efea94b178ed214ed2e78ef0d008727d62abKen Sumrall      SLOGI("Took %d tries to load dmcrypt table.\n", i + 1);
4458f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
4468f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
4478f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  /* Resume this device to activate it */
4488f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  ioctl_init(io, 4096, name, 0);
4498f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
4508f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  if (ioctl(fd, DM_DEV_SUSPEND, io)) {
4518f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    SLOGE("Cannot resume the dm-crypt device\n");
4528f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    goto errout;
4538f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
4548f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
4558f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  /* We made it here with no errors.  Woot! */
4568f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  retval = 0;
4578f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
4588f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrallerrout:
4598f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  close(fd);   /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
4608f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
4618f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  return retval;
4628f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall}
4638f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
46429d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrallstatic int delete_crypto_blk_dev(char *name)
4658f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall{
4668f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  int fd;
4678f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  char buffer[DM_CRYPT_BUF_SIZE];
4688f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  struct dm_ioctl *io;
4698f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  int retval = -1;
4708f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
4718f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  if ((fd = open("/dev/device-mapper", O_RDWR)) < 0 ) {
4728f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    SLOGE("Cannot open device-mapper\n");
4738f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    goto errout;
4748f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
4758f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
4768f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  io = (struct dm_ioctl *) buffer;
4778f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
4788f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
4798f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  if (ioctl(fd, DM_DEV_REMOVE, io)) {
4808f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    SLOGE("Cannot remove dm-crypt device\n");
4818f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    goto errout;
4828f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
4838f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
4848f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  /* We made it here with no errors.  Woot! */
4858f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  retval = 0;
4868f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
4878f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrallerrout:
4888f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  close(fd);    /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
4898f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
4908f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  return retval;
4918f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
4928f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall}
4938f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
494e87440703663f5ee326326f6438f3b00ea315623Ken Sumrallstatic void pbkdf2(char *passwd, unsigned char *salt, unsigned char *ikey)
4958f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall{
4968f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    /* Turn the password into a key and IV that can decrypt the master key */
497e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall    PKCS5_PBKDF2_HMAC_SHA1(passwd, strlen(passwd), salt, SALT_LEN,
4988f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall                           HASH_COUNT, KEY_LEN_BYTES+IV_LEN_BYTES, ikey);
4998ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall}
5008ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall
501e87440703663f5ee326326f6438f3b00ea315623Ken Sumrallstatic int encrypt_master_key(char *passwd, unsigned char *salt,
502e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall                              unsigned char *decrypted_master_key,
5038ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall                              unsigned char *encrypted_master_key)
5048ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall{
5058ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
5068ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    EVP_CIPHER_CTX e_ctx;
5078ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    int encrypted_len, final_len;
5088ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall
5098ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    /* Turn the password into a key and IV that can decrypt the master key */
510e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall    pbkdf2(passwd, salt, ikey);
5118f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
5128f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    /* Initialize the decryption engine */
5138f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    if (! EVP_EncryptInit(&e_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
5148f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        SLOGE("EVP_EncryptInit failed\n");
5158f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        return -1;
5168f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    }
5178f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    EVP_CIPHER_CTX_set_padding(&e_ctx, 0); /* Turn off padding as our data is block aligned */
5188ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall
5198f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    /* Encrypt the master key */
5208ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    if (! EVP_EncryptUpdate(&e_ctx, encrypted_master_key, &encrypted_len,
5218ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall                              decrypted_master_key, KEY_LEN_BYTES)) {
5228f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        SLOGE("EVP_EncryptUpdate failed\n");
5238f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        return -1;
5248f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    }
5258ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    if (! EVP_EncryptFinal(&e_ctx, encrypted_master_key + encrypted_len, &final_len)) {
5268f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        SLOGE("EVP_EncryptFinal failed\n");
5278f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        return -1;
5288f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    }
5298f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
5308f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    if (encrypted_len + final_len != KEY_LEN_BYTES) {
5318f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        SLOGE("EVP_Encryption length check failed with %d, %d bytes\n", encrypted_len, final_len);
5328f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        return -1;
5338f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    } else {
5348f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        return 0;
5358f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    }
5368f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall}
5378f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
538e87440703663f5ee326326f6438f3b00ea315623Ken Sumrallstatic int decrypt_master_key(char *passwd, unsigned char *salt,
539e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall                              unsigned char *encrypted_master_key,
5408f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall                              unsigned char *decrypted_master_key)
5418f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall{
5428f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  unsigned char ikey[32+32] = { 0 }; /* Big enough to hold a 256 bit key and 256 bit IV */
5438f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  EVP_CIPHER_CTX d_ctx;
5448f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  int decrypted_len, final_len;
5458f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
5468f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  /* Turn the password into a key and IV that can decrypt the master key */
547e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall  pbkdf2(passwd, salt, ikey);
5488f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
5498f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  /* Initialize the decryption engine */
5508f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  if (! EVP_DecryptInit(&d_ctx, EVP_aes_128_cbc(), ikey, ikey+KEY_LEN_BYTES)) {
5518f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    return -1;
5528f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
5538f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  EVP_CIPHER_CTX_set_padding(&d_ctx, 0); /* Turn off padding as our data is block aligned */
5548f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  /* Decrypt the master key */
5558f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  if (! EVP_DecryptUpdate(&d_ctx, decrypted_master_key, &decrypted_len,
5568f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall                            encrypted_master_key, KEY_LEN_BYTES)) {
5578f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    return -1;
5588f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
5598f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  if (! EVP_DecryptFinal(&d_ctx, decrypted_master_key + decrypted_len, &final_len)) {
5608f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    return -1;
5618f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
5628f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
5638f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  if (decrypted_len + final_len != KEY_LEN_BYTES) {
5648f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    return -1;
5658f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  } else {
5668f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    return 0;
5678f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall  }
5688f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall}
5698f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
570e87440703663f5ee326326f6438f3b00ea315623Ken Sumrallstatic int create_encrypted_random_key(char *passwd, unsigned char *master_key, unsigned char *salt)
5718ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall{
5728ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    int fd;
573e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall    unsigned char key_buf[KEY_LEN_BYTES];
5748ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    EVP_CIPHER_CTX e_ctx;
5758ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    int encrypted_len, final_len;
5768ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall
5778ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    /* Get some random bits for a key */
5788ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    fd = open("/dev/urandom", O_RDONLY);
579e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall    read(fd, key_buf, sizeof(key_buf));
580e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall    read(fd, salt, SALT_LEN);
5818ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    close(fd);
5828ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall
5838ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    /* Now encrypt it with the password */
584e87440703663f5ee326326f6438f3b00ea315623Ken Sumrall    return encrypt_master_key(passwd, salt, key_buf, master_key);
5858ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall}
5868ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall
5878f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrallstatic int wait_and_unmount(char *mountpoint)
5888f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall{
5898f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    int i, rc;
5902eaf7138528d30c331d83ab8346a97e66b5499e2Ken Sumrall#define WAIT_UNMOUNT_COUNT 20
5918f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
5928f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    /*  Now umount the tmpfs filesystem */
5938f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    for (i=0; i<WAIT_UNMOUNT_COUNT; i++) {
5948f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        if (umount(mountpoint)) {
59529d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall            if (errno == EINVAL) {
59629d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall                /* EINVAL is returned if the directory is not a mountpoint,
59729d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall                 * i.e. there is no filesystem mounted there.  So just get out.
59829d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall                 */
59929d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall                break;
60029d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall            }
6018f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall            sleep(1);
6028f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall            i++;
6038f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        } else {
6048f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall          break;
6058f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall        }
6068f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    }
6078f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
6088f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    if (i < WAIT_UNMOUNT_COUNT) {
6098f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall      SLOGD("unmounting %s succeeded\n", mountpoint);
6108f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall      rc = 0;
6118f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    } else {
6128f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall      SLOGE("unmounting %s failed\n", mountpoint);
6138f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall      rc = -1;
6148f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    }
6158f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
6168f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    return rc;
6178f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall}
6188f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall
6198ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall#define DATA_PREP_TIMEOUT 100
6208ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrallstatic int prep_data_fs(void)
6218ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall{
6228ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    int i;
6238ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall
6248ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    /* Do the prep of the /data filesystem */
6258ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    property_set("vold.post_fs_data_done", "0");
6268ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    property_set("vold.decrypt", "trigger_post_fs_data");
6278ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    SLOGD("Just triggered post_fs_data\n");
6288ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall
6298ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    /* Wait a max of 25 seconds, hopefully it takes much less */
6308ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    for (i=0; i<DATA_PREP_TIMEOUT; i++) {
63129d8da8cefa99e436c13295d4c9bad060ca18a6dKen Sumrall        char p[PROPERTY_VALUE_MAX];
6328ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall
6338ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall        property_get("vold.post_fs_data_done", p, "0");
6348ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall        if (*p == '1') {
6358ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall            break;
6368ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall        } else {
6378ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall            usleep(250000);
6388ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall        }
6398ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    }
6408ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    if (i == DATA_PREP_TIMEOUT) {
6418ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall        /* Ugh, we failed to prep /data in time.  Bail. */
6428ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall        return -1;
6438ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    } else {
6448ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall        SLOGD("post_fs_data done\n");
6458ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall        return 0;
6468ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall    }
6478ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall}
6488ddbe40a8a8708dac7c472fa8c098c8f7b24534cKen Sumrall
6496864b7ec94a57b73c300457955d86dc604aeddf5Ken Sumrallint cryptfs_restart(void)
6508f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall{
6518f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    char fs_type[32];
6528f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    char real_blkdev[MAXPATHLEN];
6536864b7ec94a57b73c300457955d86dc604aeddf5Ken Sumrall    char crypto_blkdev[MAXPATHLEN];
6548f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    char fs_options[256];
6558f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    unsigned long mnt_flags;
6568f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    struct stat statbuf;
6578f869aa1bc685b505c58e97b4e11a9c7491a16f9Ken Sumrall    int rc = -1, i;
6580cc166385a7e1d3026bbcb62f094e419f779e872Ken Sumrall    static int restart_successful = 0;
6590cc166385a7e1d3026bbcb62f094e419f779e872Ken Sumrall
660