1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
#!/bin/bash
#
# Copyright (C) 2010 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# mkobb.sh - Creates OBB files on Linux machines
# Directory where we should temporarily mount the OBB loopback to copy files
MOUNTDIR=/tmp
# Presets. Changing these will probably break your OBB on the device
CRYPTO=twofish
FS=vfat
MKFS=mkfs.vfat
LOSETUP=losetup
BLOCK_SIZE=512
SLOP=512 # Amount of filesystem slop in ${BLOCK_SIZE} blocks
find_binaries() {
MKFSBIN=`which ${MKFS}`
LOSETUPBIN=`which ${LOSETUP}`
MOUNTBIN=`which mount`
UMOUNTBIN=`which umount`
DDBIN=`which dd`
RSYNCBIN=`which rsync`
PBKDF2GEN=`which pbkdf2gen`
}
check_prereqs() {
if [ "`uname -s`x" != "Linuxx" ]; then \
echo "ERROR: This script only works on Linux!"
exit 1
fi
if ! egrep -q "^cryptoloop " /proc/modules; then \
echo "ERROR: Could not find cryptoloop in the kernel."
echo "Perhaps you need to: modprobe cryptoloop"
exit 1
fi
if ! egrep -q "name\s*:\s*${CRYPTO}$" /proc/crypto; then \
echo "ERROR: Could not find crypto \`${CRYPTO}' in the kernel."
echo "Perhaps you need to: modprobe ${CRYPTO}"
exit 1
fi
if ! egrep -q "^\s*${FS}$" /proc/filesystems; then \
echo "ERROR: Could not find filesystem \`${FS}' in the kernel."
echo "Perhaps you need to: modprobe ${FS}"
exit 1
fi
if [ "${MKFSBIN}x" = "x" ]; then \
echo "ERROR: Could not find ${MKFS} in your path!"
exit 1
elif [ ! -x "${MKFSBIN}" ]; then \
echo "ERROR: ${MKFSBIN} is not executable!"
exit 1
fi
if [ "${LOSETUPBIN}x" = "x" ]; then \
echo "ERROR: Could not find ${LOSETUP} in your path!"
exit 1
elif [ ! -x "${LOSETUPBIN}" ]; then \
echo "ERROR: ${LOSETUPBIN} is not executable!"
exit 1
fi
if [ "${PBKDF2GEN}x" = "x" ]; then \
echo "ERROR: Could not find pbkdf2gen in your path!"
exit 1
fi
}
cleanup() {
if [ "${loopdev}x" != "x" ]; then \
${LOSETUPBIN} -d ${loopdev}
fi
}
hidden_prompt() {
unset output
prompt="$1"
outvar="$2"
while read -s -n 1 -p "$prompt" c; do \
if [ "x$c" = "x" ]; then \
break
fi
prompt='*'
output="${output}${c}"
done
echo
eval $outvar="$output"
unset output
}
read_key() {
hidden_prompt " Encryption key: " key
if [ "${key}x" = "x" ]; then \
echo "ERROR: An empty key is not allowed!"
exit 1
fi
hidden_prompt "Encryption key (again): " key2
if [ "${key}x" != "${key2}x" ]; then \
echo "ERROR: Encryption keys do not match!"
exit 1
fi
}
onexit() {
if [ "x${temp_mount}" != "x" ]; then \
${UMOUNTBIN} ${temp_mount}
rmdir ${temp_mount}
fi
if [ "x${loop_dev}" != "x" ]; then \
if [ ${use_crypto} -eq 1 ]; then \
dmsetup remove -f ${loop_dev}
${LOSETUPBIN} -d ${old_loop_dev}
else \
${LOSETUPBIN} -d ${loop_dev}
fi
fi
if [ "x${tempfile}" != "x" -a -f "${tempfile}" ]; then \
rm -f ${tempfile}
fi
if [ "x${keyfile}" != "x" -a -f "${keyfile}" ]; then \
rm -f ${keyfile}
fi
echo "Fatal error."
exit 1
}
usage() {
echo "mkobb.sh -- Create OBB files for use on Android"
echo ""
echo " -d <directory> Use <directory> as input for OBB files"
echo " -k <key> Use <key> to encrypt OBB file"
echo " -K Prompt for key to encrypt OBB file"
echo " -o <filename> Write OBB file out to <filename>"
echo " -v Verbose mode"
echo " -h Help; this usage screen"
}
find_binaries
check_prereqs
use_crypto=0
args=`getopt -o d:hk:Ko:v -- "$@"`
eval set -- "$args"
while true; do \
case "$1" in
-d) directory=$2; shift 2;;
-h) usage; exit 1;;
-k) key=$2; use_crypto=1; shift 2;;
-K) prompt_key=1; use_crypto=1; shift;;
-v) verbose=1; shift;;
-o) filename=$2; shift 2;;
--) shift; break;;
*) echo "ERROR: Invalid argument in option parsing! Cannot recover. Ever."; exit 1;;
esac
done
if [ "${directory}x" = "x" -o ! -d "${directory}" ]; then \
echo "ERROR: Must specify valid input directory"
echo ""
usage
exit 1;
fi
if [ "${filename}x" = "x" ]; then \
echo "ERROR: Must specify filename"
echo ""
usage
exit 1;
fi
if [ ${use_crypto} -eq 1 -a "${key}x" = "x" -a 0${prompt_key} -eq 0 ]; then \
echo "ERROR: Crypto desired, but no key supplied or requested to prompt for."
exit 1
fi
if [ 0${prompt_key} -eq 1 ]; then \
read_key
fi
outdir=`dirname ${filename}`
if [ ! -d "${outdir}" ]; then \
echo "ERROR: Output directory does not exist: ${outdir}"
exit 1
fi
# Make sure we clean up any stuff we create from here on during error conditions
trap onexit ERR
tempfile=$(tempfile -d ${outdir}) || ( echo "ERROR: couldn't create temporary file in ${outdir}"; exit 1 )
block_count=`du -s --apparent-size --block-size=512 ${directory} | awk '{ print $1; }'`
if [ $? -ne 0 ]; then \
echo "ERROR: Couldn't read size of input directory ${directory}"
exit 1
fi
echo "Creating temporary file..."
${DDBIN} if=/dev/zero of=${tempfile} bs=${BLOCK_SIZE} count=$((${block_count} + ${SLOP})) > /dev/null 2>&1
if [ $? -ne 0 ]; then \
echo "ERROR: creating temporary file: $?"
fi
loop_dev=$(${LOSETUPBIN} -f) || ( echo "ERROR: losetup wouldn't tell us the next unused device"; exit 1 )
${LOSETUPBIN} ${loop_dev} ${tempfile} || ( echo "ERROR: couldn't create loopback device"; exit 1 )
if [ ${use_crypto} -eq 1 ]; then \
eval `${PBKDF2GEN} ${key}`
unique_dm_name=`basename ${tempfile}`
echo "0 `blockdev --getsize ${loop_dev}` crypt ${CRYPTO} ${key} 0 ${loop_dev} 0" | dmsetup create ${unique_dm_name}
old_loop_dev=${loop_dev}
loop_dev=/dev/mapper/${unique_dm_name}
fi
#
# Create the filesystem
#
echo ""
${MKFSBIN} -I ${loop_dev}
echo ""
#
# Make the temporary mount point and mount it
#
temp_mount="${MOUNTDIR}/${RANDOM}"
mkdir ${temp_mount}
${MOUNTBIN} -t ${FS} -o loop ${loop_dev} ${temp_mount}
#
# rsync the files!
#
echo "Copying files:"
${RSYNCBIN} -av --no-owner --no-group ${directory}/ ${temp_mount}/
echo ""
echo "Successfully created \`${filename}'"
if [ ${use_crypto} -eq 1 ]; then \
echo "salt for use with obbtool is:"
echo "${salt}"
fi
#
# Undo all the temporaries
#
umount ${temp_mount}
rmdir ${temp_mount}
if [ ${use_crypto} -eq 1 ]; then \
dmsetup remove -f ${loop_dev}
${LOSETUPBIN} -d ${old_loop_dev}
else \
${LOSETUPBIN} -d ${loop_dev}
fi
mv ${tempfile} ${filename}
trap - ERR
exit 0
|