]> spindle.queued.net Git - xodist/blob - mkjffs2.sh
mkext3/mkjffs2: rework fstab/olpc.fth file generation during image creation
[xodist] / mkjffs2.sh
1 #!/bin/bash -e
2 #
3 # Copyright © 2008-2009  Andres Salomon <dilinger@collabora.co.uk>
4 #
5 # This file is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 . ./functions.sh
20
21 CONFIG_TYPE=generic
22
23 usage()
24 {
25         echo "" 1>&2
26         echo "Usage: $0 [<options>] <root directory> <img>" 1>&2
27         echo "" 1>&2
28         echo "Options:" 1>&2
29         echo "  --config-type <config>    directory name in configs/ to use" 1>&2
30         echo "" 1>&2
31         exit 1
32 }
33
34 while test $# != 0
35 do
36         case $1 in
37         --config-type)
38                 CONFIG_TYPE=$2
39                 [ -d ./configs/${CONFIG_TYPE} ] || {
40                         echo "Error: can't find directory './configs/${CONFIG_TYPE}/'!" 1>&2
41                         exit 2
42                 }
43                 shift
44                 ;;
45         *)
46                 if [ "$#" != "2" ]; then
47                         echo "Unknown option $1" 1>&2
48                         usage
49                 else
50                         ROOT_DIR=$1
51                         IMG_NAME=$2
52                         shift
53                 fi
54                 ;;
55         esac
56         shift
57 done
58
59 if [ "${IMG_NAME}" == "${IMG_NAME/.img/.dat}" ]; then
60         DAT_NAME=${IMG_NAME}.dat
61 else
62         DAT_NAME=${IMG_NAME/.img/.dat}
63 fi
64
65 if [ ! -d "${ROOT_DIR}" ]; then
66         echo "" 1>&2
67         echo "*** Unable to find root directory!" 1>&2
68         usage
69 fi
70
71 check_for_cmds mkfs.jffs2 sumtool || exit 1
72
73 # create image's /etc/fstab
74 if [ ! -f ./configs/${CONFIG_TYPE}/fstab-jffs2 ]; then
75         echo "*** Unable to find fstab-jffs2!" 1>&2
76         exit 1
77 fi
78 sed 's/[[:space:]]#.*//' ./configs/${CONFIG_TYPE}/fstab-jffs2 > ${ROOT_DIR}/etc/fstab
79
80 # TODO: this needs to go into an OFW package; here it's a hack
81 # create image's /boot/olpc.fth
82 if [ -f ./configs/${CONFIG_TYPE}/olpc.fth-jffs2 ]; then
83         cp ./configs/${CONFIG_TYPE}/olpc.fth-jffs2 ${ROOT_DIR}/boot/olpc.fth
84 fi
85
86 create_jffs2()
87 {
88         root_dir="$1"
89         out="$2"
90
91         # XXX:  do we want to switch to lzo?  (mkfs.jffs2 -X lzo)
92         mkfs.jffs2 -n -e128KiB -r ${root_dir} -o ${out}.pre
93         sumtool -n -p -e 128KiB -i ${out}.pre -o ${out}
94         rm -f ${out}.pre
95 }
96
97 do_sha256()
98 {
99         f=$1
100         eblocks=$((`stat --printf "%s\n" $f` / (128*1024)))
101         for b in $(seq 0 $(($eblocks - 1))); do
102                 sha=$(dd status=noxfer bs=128KiB skip=$b count=1 if=$f 2>/dev/null | sha256sum | cut -d\  -f1)
103                 echo "eblock: `printf '%x' $b` sha256 $sha" >> ${IMG_NAME}
104         done
105 }
106
107 partition_map()
108 {
109         # 0x190 * 128KiB = 50MiB boot, and the rest for root
110         cat >${IMG_NAME}<<EOF
111 data:  ${DAT_NAME}
112 erase-all
113 partitions:  boot 190  root -1
114 set-partition: boot 
115 mark-pending: 0
116 EOF
117         do_sha256 "_boot.img"
118         cat >>${IMG_NAME}<<EOF
119 cleanmarkers
120 mark-complete: 0
121 set-partition: root
122 mark-pending: 0
123 EOF
124         do_sha256 "_root.img"
125         cat >>${IMG_NAME}<<EOF
126 cleanmarkers
127 mark-complete: 0
128 EOF
129 }
130
131 # create the boot partition
132 ln -s . ${ROOT_DIR}/boot/boot
133 create_jffs2 ${ROOT_DIR}/boot _boot.img
134 rm -f ${ROOT_DIR}/boot/boot
135
136 # create the root partition
137 mv ${ROOT_DIR}/boot _boot
138 mkdir ${ROOT_DIR}/boot
139 create_jffs2 ${ROOT_DIR} _root.img
140 rmdir ${ROOT_DIR}/boot
141 mv _boot ${ROOT_DIR}/boot
142
143 # concat partitions, finish up
144 partition_map
145 cat _boot.img _root.img > ${DAT_NAME}
146 rm -f _boot.img _root.img