1#!/bin/bash
2#
3# To call this script, make sure make_f2fs is somewhere in PATH
4
5function usage() {
6cat<<EOT
7Usage:
8${0##*/} OUTPUT_FILE SIZE
9         [-S] [-C FS_CONFIG] [-f SRC_DIR] [-D PRODUCT_OUT]
10         [-s FILE_CONTEXTS] [-t MOUNT_POINT] [-T TIMESTAMP] [-B block_map]
11         [-L LABEL] [--prjquota] [--casefold] [--compression] [--readonly]
12         [--sldc <num> [sload compression sub-options]] [-b <block_size>]
13<num>: number of the sload compression args, e.g.  -a LZ4 counts as 2
14       when sload compression args are not given, <num> must be 0,
15       and the default flags will be used.
16Note: must conserve the option order
17EOT
18}
19
20echo "in mkf2fsuserimg.sh PATH=$PATH"
21
22MKFS_OPTS=""
23SLOAD_OPTS=""
24BLOCK_MAP_FILE=""
25BLOCK_MAP_OPT=""
26
27if [ $# -lt 2 ]; then
28  usage
29  exit 1
30fi
31
32OUTPUT_FILE=$1
33SIZE=$2
34shift; shift
35
36SPARSE_IMG="false"
37if [[ "$1" == "-S" ]]; then
38  MKFS_OPTS+=" -S $SIZE"
39  SLOAD_OPTS+=" -S"
40  BLOCK_MAP_OPT+=" -S -M"
41  SPARSE_IMG="true"
42  shift
43fi
44
45if [[ "$1" == "-C" ]]; then
46  SLOAD_OPTS+=" -C $2"
47  shift; shift
48fi
49if [[ "$1" == "-f" ]]; then
50  SLOAD_OPTS+=" -f $2"
51  shift; shift
52fi
53if [[ "$1" == "-D" ]]; then
54  SLOAD_OPTS+=" -p $2"
55  shift; shift
56fi
57if [[ "$1" == "-s" ]]; then
58  SLOAD_OPTS+=" -s $2"
59  shift; shift
60fi
61if [[ "$1" == "-t" ]]; then
62  MOUNT_POINT=$2
63  shift; shift
64fi
65
66if [ -z $MOUNT_POINT ]; then
67  echo "Mount point is required"
68  exit 2
69fi
70
71if [[ ${MOUNT_POINT:0:1} != "/" ]]; then
72  MOUNT_POINT="/"$MOUNT_POINT
73fi
74
75SLOAD_OPTS+=" -t $MOUNT_POINT"
76
77if [[ "$1" == "-T" ]]; then
78  SLOAD_OPTS+=" -T $2"
79  shift; shift
80fi
81
82if [[ "$1" == "-B" ]]; then
83  BLOCK_MAP_FILE="$2"
84  shift; shift
85fi
86
87if [[ "$1" == "-L" ]]; then
88  MKFS_OPTS+=" -l $2"
89  shift; shift
90fi
91
92if [[ "$1" == "--prjquota" ]]; then
93  MKFS_OPTS+=" -O project_quota,extra_attr"
94  shift;
95fi
96if [[ "$1" == "--casefold" ]]; then
97  MKFS_OPTS+=" -O casefold -C utf8"
98  shift;
99fi
100
101if [[ "$1" == "--compression" ]]; then
102  COMPRESS_SUPPORT=1
103  MKFS_OPTS+=" -O compression,extra_attr"
104  shift;
105fi
106if [[ "$1" == "--readonly" ]]; then
107  MKFS_OPTS+=" -O ro"
108  READONLY=1
109  shift;
110fi
111
112if [[ "$1" == "--sldc" ]]; then
113  if [ -z "$COMPRESS_SUPPORT" ]; then
114    echo "--sldc needs --compression flag"
115    exit 3
116  fi
117  SLOAD_OPTS+=" -c"
118  shift
119  SLDC_NUM_ARGS=$1
120  case $SLDC_NUM_ARGS in
121    ''|*[!0-9]*)
122      echo "--sldc needs a number"
123      exit 3 ;;
124  esac
125  shift
126  while [ $SLDC_NUM_ARGS -gt 0 ]; do
127    SLOAD_OPTS+=" $1"
128    shift
129    (( SLDC_NUM_ARGS-- ))
130  done
131fi
132
133if [[ "$1" == "-b" ]]; then
134  shift
135  BLOCKSIZE=$1
136  case $BLOCKSIZE in
137    ''|*[!0-9]*)
138      echo "-b needs a number"
139      exit 3 ;;
140  esac
141  shift
142  MKFS_OPTS+=" -b $BLOCKSIZE"
143  MKFS_OPTS+=" -w $BLOCKSIZE"
144fi
145
146if [ -z $SIZE ]; then
147  echo "Need size of filesystem"
148  exit 2
149fi
150
151function _truncate()
152{
153  if [ "$SPARSE_IMG" = "true" ]; then
154    return
155  fi
156
157  TRUNCATE_CMD="truncate -s $SIZE $OUTPUT_FILE"
158  echo $TRUNCATE_CMD
159  $TRUNCATE_CMD
160  if [ $? -ne 0 ]; then
161    exit 3
162  fi
163}
164
165function _build()
166{
167  MAKE_F2FS_CMD="make_f2fs -g android $MKFS_OPTS $OUTPUT_FILE"
168  echo $MAKE_F2FS_CMD
169  $MAKE_F2FS_CMD
170  if [ $? -ne 0 ]; then
171    if [ "$SPARSE_IMG" = "false" ]; then
172      rm -f $OUTPUT_FILE
173    fi
174    exit 4
175  fi
176
177  SLOAD_F2FS_CMD="sload_f2fs $SLOAD_OPTS $OUTPUT_FILE"
178  echo $SLOAD_F2FS_CMD
179  SLOAD_LOG=`$SLOAD_F2FS_CMD`
180  # allow 1: Filesystem errors corrected
181  ret=$?
182  if [ $ret -ne 0 ] && [ $ret -ne 1 ]; then
183    rm -f $OUTPUT_FILE
184    exit 4
185  fi
186  MB_SIZE=`echo "$SLOAD_LOG" | grep "Max image size" | awk '{print $5}'`
187  SIZE=$(((MB_SIZE + 6) * 1024 * 1024))
188}
189
190_truncate
191_build
192
193# readonly can reduce the image
194if [ "$READONLY" ]; then
195  if [ "$SPARSE_IMG" = "true" ]; then
196    MKFS_OPTS+=" -S $SIZE"
197    rm -f $OUTPUT_FILE && touch $OUTPUT_FILE
198  fi
199  _truncate
200  _build
201  # build block map
202  if [ "$BLOCK_MAP_FILE" ]; then
203    fsck.f2fs $BLOCK_MAP_OPT $OUTPUT_FILE > $BLOCK_MAP_FILE
204  fi
205fi
206exit 0
207