2226ab21b02e35e6fe60978677a88ec50aa8ed70
[platform/core/system/upgrade-tools.git] / mk_delta / common / bin / mk_delta.sh
1 #!/bin/bash
2
3
4 #==================
5 # funtions
6 #==================
7
8 #------------------------------------------------------------------------------
9 # Function :
10 #       fn_gen_update_cfg
11 #
12 # Description :
13 #       read delta configuration file for generating update configuration file to be used during update.
14 #       delta config file format : PART_NAME PART_BIN DELTA_BIN UPDATE_TYPE BLK_DEV BLK_OFFSET (deliminator : TAB)
15
16 fn_gen_update_cfg()
17 {
18         in_file=$1
19         out_file=$2
20
21         if [ ! -r ${in_file} ]; then
22                 echo "file ${in_file} cannot be read"
23                 return 1
24         fi
25
26         line_num=0
27         while read line
28         do
29                 # skip comment line
30                 if [ "${line:0:1}" = "#" ]; then
31                         continue
32                 fi
33
34                 # skip empty line
35                 line_len=${#line}
36                 if [ "${line_len}" = "0" ]; then
37                         continue
38                 fi
39
40
41                 fields=(${line})
42                 PART_NAME[${line_num}]=${fields[0]}
43                 PART_BIN[${line_num}]=${fields[1]}
44                 DELTA_BIN[${line_num}]=${fields[2]}
45                 UPDATE_TYPE[${line_num}]=${fields[3]}
46                 BLK_DEV[${line_num}]=${fields[4]}
47                 BLK_OFFSET[${line_num}]=${fields[5]}
48
49                 line_num=$(($line_num + 1))
50
51         done < ${in_file}
52
53         PART_NUM=${line_num}
54
55         if [ -f ${out_file} ]; then
56                 sudo rm -fr ${out_file}
57         fi
58
59         for ((update_idx=0;update_idx<PART_NUM;update_idx++))
60         do
61                 f1=${PART_NAME[${update_idx}]}
62                 f2=${DELTA_BIN[${update_idx}]}
63                 f3=${UPDATE_TYPE[${update_idx}]}
64                 f4=${BLK_DEV[${update_idx}]}
65                 f5=${BLK_OFFSET[${update_idx}]}
66
67                 echo -e "$f1\t\t$f2\t\t$f3\t\t$f4\t\t$f5" >> ${out_file}
68         done
69
70         return 0
71 }
72
73 #------------------------------------------------------------------------------
74 # Function :
75 #       fn_set_default_params
76 #
77 # Description :
78 #       set default params used globally in this script
79 #
80
81 fn_set_default_params()
82 {
83         MY_ID=`whoami`
84         MK_PART_DELTA=${COMMON_BINDIR}/mk_part_delta.sh
85         DELTA_CFG_PATH=./cfg/delta.cfg
86         UPDATE_CFG_PATH=./cfg/update.cfg
87         PRE_SCRIPT_PATH=./cfg/pre.sh
88         DELTA_UA=./data/delta.ua
89         UPDATE_MANAGER=./data/upgrade-trigger.sh
90
91         LOG_PATH=./data/Delta.log
92         BUILD_ID_PATH=./data/build_id.txt
93         # Getting date and minor version
94         MONDATE=$(date +%m%d)
95         i=1
96         while [ -d "result/${MONDATE}_${i}" ]
97         do
98                 i=$(($i + 1))
99         done
100         MONDATE="${MONDATE}_${i}"
101         echo "MONDATE = ${MONDATE}"
102
103         if [ "z${OLD_TAR_DIR}" = "z" ]; then
104                 export OLD_TAR_DIR="old_tar"
105         fi
106         if [ "z${NEW_TAR_DIR}" = "z" ]; then
107                 export NEW_TAR_DIR="new_tar"
108         fi
109 }
110
111 #------------------------------------------------------------------------------
112 # Function :
113 #       fn_swap_binary_files
114 #
115 # Description :
116 #       change old & new for making backward delta
117 #
118
119 fn_swap_binary_files()
120 {
121         TEMP_TAR_DIR=${OLD_TAR_DIR}
122         export OLD_TAR_DIR=${NEW_TAR_DIR}
123         export NEW_TAR_DIR=${TEMP_TAR_DIR}
124 }
125
126 #------------------------------------------------------------------------------
127 # Function :
128 #       fn_print_update_cfg
129
130 fn_print_update_cfg()
131 {
132         for ((update_idx=0;update_idx<PART_NUM;update_idx++))
133         do
134                         echo "------------------------------"
135                         echo "[PART_NAME]    ${PART_NAME[${update_idx}]}"
136                         echo "[DELTA_BIN]    ${DELTA_BIN[${update_idx}]}"
137                         echo "[UPDATE_TYPE]  ${UPDATE_TYPE[${update_idx}]}"
138                         echo "[BLK_DEV]      ${BLK_DEV[${update_idx}]}"
139                         echo "[BLK_OFFSET]   ${BLK_OFFSET[${update_idx}]}"
140
141         done
142         echo "------------------------------"
143 }
144
145 #------------------------------------------------------------------------------
146 # Function :
147 #       fn_process_extra_delta
148 #
149
150 fn_process_extra_delta()
151 {
152         RAMDISK2_FULL_IMG=0
153         if [ "${part_name}" = "RAMDISK2" ]; then
154                 if [ "${RAMDISK2_FULL_IMG}" = "0" ]; then
155                         #--- delta recovery partition ---
156                         ${MK_PART_DELTA} ${part_name} DELTA_IMG ${part_bin} ${delta_bin}
157                         if [ "$?" = "0" ]; then
158                                 sudo mv data/${PART_OUT} ${RESULT_DIR}/${part_bin}
159                                 sudo mv ${RESULT_DIR}/${part_bin}/${delta_bin} ${DELTA_DIR}
160                         fi
161                 fi
162                 if [ "${RAMDISK2_FULL_IMG}" = "1" ]; then
163                         #--- full recovery partition ---
164                         ${MK_PART_DELTA} ${part_name} FULL_IMG ${part_bin} ${delta_bin}
165                         if [ "$?" = "0" ]; then
166                                 sudo mv data/${PART_OUT} ${RESULT_DIR}/${part_bin}
167                                 sudo mv ${RESULT_DIR}/${part_bin}/${part_bin} ${DELTA_DIR}
168                         fi
169                 fi
170         elif [ "${part_name}" = "RECOVERY" ]; then
171                 #--- recovery kernel ---
172                 sudo echo "1" > ${DELTA_DIR}/${delta_bin}
173         elif [ "${part_name}" = "EXTRA" ]; then
174                 echo "---"
175         fi
176 }
177
178 #------------------------------------------------------------------------------
179 # Function :
180 #       fn_get_tar_file_names
181 #
182 # Description :
183 #       find tar file name (without path) that contains given partition binary
184 #
185
186 fn_get_tar_file_names()
187 {
188         BIN_TO_FIND=$1
189
190         cd ${DATA_DIR}/${OLD_TAR_DIR}
191         OLD_TAR_LIST=`ls *.tar *.tar.gz *.tgz *.tar.md5 2>/dev/null`
192         cd ${CUR_DIR}
193
194         cd ${DATA_DIR}/${NEW_TAR_DIR}
195         NEW_TAR_LIST=`ls *.tar *.tar.gz *.tgz *.tar.md5 2>/dev/null`
196         cd ${CUR_DIR}
197
198         #--- find which tar contains the binary ---
199         for tar_file in ${OLD_TAR_LIST}
200         do
201                 result=`tar -tvf ${DATA_DIR}/${OLD_TAR_DIR}/${tar_file} | grep " ${BIN_TO_FIND}$"`
202                 if [ "$?" = "0" ]; then
203                         OLD_TAR_FILE=${tar_file}
204                         break
205                 fi
206         done
207
208         for tar_file in ${NEW_TAR_LIST}
209         do
210                 result=`tar -tvf ${DATA_DIR}/${NEW_TAR_DIR}/${tar_file} | grep " ${BIN_TO_FIND}$"`
211                 if [ "$?" = "0" ]; then
212                         NEW_TAR_FILE=${tar_file}
213                         break
214                 fi
215         done
216
217 }
218
219 #------------------------------------------------------------------------------
220 # Function :
221 #       fn_extract_from_image
222 #
223 # Description :
224 #       extract some files from given image
225 #
226
227 fn_extract_from_image()
228 {
229         CUR_DIR=`pwd`
230         DATA_DIR=./data
231         IMAGE=$1
232         TARGET_FILE=$2
233         MNT_PNT=TMP_MNT
234         mkdir -p ${MNT_PNT}
235
236         fn_get_tar_file_names $IMAGE
237         tar xvf ${DATA_DIR}/${NEW_TAR_DIR}/${NEW_TAR_FILE} ${IMAGE}
238         if [ "$?" != "0" ]; then
239                 return 1;
240         fi
241
242         sudo mount -t ext4 -o loop ${IMAGE} ${MNT_PNT}
243         if [ "$?" != "0" ]; then
244                 return 1;
245         fi
246
247         EXTRACT_FILE="${MNT_PNT}${TARGET_FILE}"
248         if [ -e ${EXTRACT_FILE} ]; then
249                 sudo cp ${EXTRACT_FILE} ${DELTA_DIR}
250         else
251                 echo "There is no ${TARGET_FILE} in ${IMAGE}"
252         fi
253
254         sudo umount ${MNT_PNT}
255         sudo rm -rf ${MNT_PNT}
256         sudo rm -f ${IMAGE}
257 }
258
259 ###############################################################################
260 #==================
261 # Main Start
262 #==================
263
264 COMMON_BINDIR=${PWD}/../common/bin
265
266 fn_set_default_params
267
268 fn_gen_update_cfg ${DELTA_CFG_PATH} ${UPDATE_CFG_PATH}
269
270 ################### Make delta Forward and Backward #####################
271 if [ "z${FW_BW}" = "z" ]; then
272         #FW_BW="FW_DELTA BW_DELTA"
273         FW_BW="FW_DELTA"
274 fi
275
276 START_TIMESTAMP="$(date +%T)"
277
278 for Make_Delta in $FW_BW
279 do
280         #--- archive result directory ---
281         echo "Make $Make_Delta Delta file"
282         RESULT_DIR=result/$MONDATE/$Make_Delta
283         DELTA_DIR=${RESULT_DIR}/DELTA
284         mkdir -p ${DELTA_DIR}
285
286         if [ "$Make_Delta" = "BW_DELTA" ]; then
287                 fn_swap_binary_files
288         fi
289
290
291         #--- make delta for each partition ---
292         for ((part_num=0;part_num<PART_NUM;part_num++))
293         do
294                 part_name=${PART_NAME[${part_num}]}
295                 update_type=${UPDATE_TYPE[${part_num}]}
296                 part_bin=${PART_BIN[${part_num}]}
297                 delta_bin=${DELTA_BIN[${part_num}]}
298
299                 PART_OUT=${part_name}_OUT
300
301                 if [ "${update_type}" = "EXTRA" ]; then
302                         fn_process_extra_delta
303                 else
304                         ${MK_PART_DELTA} ${part_name} ${update_type} ${part_bin} ${delta_bin}
305                         if [ "$?" = "0" ]; then
306                                 if [ "${update_type}" = "DELTA_FS" ]; then
307                                         sudo mv data/${PART_OUT} ${RESULT_DIR}/${part_bin}
308                                         sudo mv ${RESULT_DIR}/${part_bin} ${DELTA_DIR}
309                                 else
310                                         sudo mv data/${PART_OUT} ${RESULT_DIR}/${part_bin}
311                                         sudo mv ${RESULT_DIR}/${part_bin}/${delta_bin} ${DELTA_DIR}
312                                 fi
313                         else
314                                 sudo rm -rf ${RESULT_DIR}/${part_bin}
315                                 echo "Error: Abort Delta Generation"
316                                 exit 1
317                         fi
318                 fi
319                 sudo rm -rf ${RESULT_DIR}/${part_bin}
320         done
321
322         #move update.cfg to delta directory
323         if [ -r ${UPDATE_CFG_PATH} ]; then
324                 sudo cp ${UPDATE_CFG_PATH} ${DELTA_DIR}/update.cfg
325                 sudo rm ${UPDATE_CFG_PATH}
326         fi
327         if [ -r ${LOG_PATH} ]; then
328                 sudo cp ${LOG_PATH} ${RESULT_DIR}/Delta.log
329                 sudo rm ${LOG_PATH}
330         fi
331
332         if [ -r ${BUILD_ID_PATH} ]; then
333                 sudo cp ${BUILD_ID_PATH} ${DELTA_DIR}/build_id.txt
334                 sudo rm ${BUILD_ID_PATH}
335         fi
336         if [ -r ${PRE_SCRIPT_PATH} ]; then
337                 sudo cp ${PRE_SCRIPT_PATH} ${DELTA_DIR}/pre.sh
338         fi
339
340         #--- extract files which would be appended to delta.tar ---
341         echo "Extract binaries for update from images"
342         fn_extract_from_image rootfs.img /usr/bin/upgrade-trigger.sh
343         fn_extract_from_image ramdisk-recovery.img /usr/bin/delta.ua
344
345         #--- archive result directory ---
346         cd result/$MONDATE
347         echo "tar result directory"
348         sudo tar cf ${MONDATE}_${Make_Delta}.tar $Make_Delta
349         cd ../..
350
351         echo "Making delta binary is End!! ($Make_Delta)"
352 done
353
354 if [ -r ${UPDATE_CFG_PATH} ]; then
355         sudo rm ${UPDATE_CFG_PATH}
356 fi
357 cd ${DELTA_DIR}
358 sudo cp ${COMMON_BINDIR}/unpack.sh ./
359 sudo tar --overwrite -cf ../delta.tar *
360
361 SIGN_PKCS_FILE=$1
362 SIGN_PKCS_PASSWORD=$2
363 if [ "z${SIGN_PKCS_FILE}" != "z" ] && [ "z${SIGN_PKCS_PASSWORD}" != "z" ]; then
364         sudo ${COMMON_BINDIR}/sign_img.sh ${SIGN_PKCS_FILE} ${SIGN_PKCS_PASSWORD} ../delta.tar
365 fi
366 cd -
367
368 END_TIMESTAMP="$(date +%T)"
369
370 echo "OVERALL TIMESTAMP : [START] ${START_TIMESTAMP} ~ [END] ${END_TIMESTAMP}"
371