1# Copyright 2011 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# Use the default values if they weren't explicitly set
16if test "$XLOADERSRC" = ""
17then
18  XLOADERSRC=xloader.img
19fi
20if test "$BOOTLOADERSRC" = ""
21then
22  BOOTLOADERSRC=bootloader.img
23fi
24if test "$RADIOSRC" = ""
25then
26  RADIOSRC=radio.img
27fi
28if test "$SLEEPDURATION" = ""
29then
30  SLEEPDURATION=5
31fi
32
33# Prepare the staging directory
34rm -rf tmp
35mkdir -p tmp/$PRODUCT-$VERSION
36
37# Extract the bootloader(s) and radio(s) as necessary
38if test "$XLOADER" != ""
39then
40  unzip -d tmp ${SRCPREFIX}$PRODUCT-target_files-$BUILD.zip RADIO/$XLOADERSRC
41fi
42if test "$BOOTLOADERFILE" = ""
43then
44  unzip -d tmp ${SRCPREFIX}$PRODUCT-target_files-$BUILD.zip RADIO/$BOOTLOADERSRC
45fi
46if test "$RADIO" != "" -a "$RADIOFILE" = ""
47then
48  unzip -d tmp ${SRCPREFIX}$PRODUCT-target_files-$BUILD.zip RADIO/$RADIOSRC
49fi
50if test "$CDMARADIO" != "" -a "$CDMARADIOFILE" = ""
51then
52  unzip -d tmp ${SRCPREFIX}$PRODUCT-target_files-$BUILD.zip RADIO/radio-cdma.img
53fi
54
55# Copy the various images in their staging location
56cp ${SRCPREFIX}$PRODUCT-img-$BUILD.zip tmp/$PRODUCT-$VERSION/image-$PRODUCT-$VERSION.zip
57if test "$XLOADER" != ""
58then
59  cp tmp/RADIO/$XLOADERSRC tmp/$PRODUCT-$VERSION/xloader-$DEVICE-$XLOADER.img
60fi
61if test "$BOOTLOADERFILE" = ""
62then
63  cp tmp/RADIO/$BOOTLOADERSRC tmp/$PRODUCT-$VERSION/bootloader-$DEVICE-$BOOTLOADER.img
64else
65  cp $BOOTLOADERFILE tmp/$PRODUCT-$VERSION/bootloader-$DEVICE-$BOOTLOADER.img
66fi
67if test "$RADIO" != ""
68then
69  if test "$RADIOFILE" = ""
70  then
71    cp tmp/RADIO/$RADIOSRC tmp/$PRODUCT-$VERSION/radio-$DEVICE-$RADIO.img
72  else
73    cp $RADIOFILE tmp/$PRODUCT-$VERSION/radio-$DEVICE-$RADIO.img
74  fi
75fi
76if test "$CDMARADIO" != ""
77then
78  if test "$CDMARADIOFILE" = ""
79  then
80    cp tmp/RADIO/radio-cdma.img tmp/$PRODUCT-$VERSION/radio-cdma-$DEVICE-$CDMARADIO.img
81  else
82    cp $CDMARADIOFILE tmp/$PRODUCT-$VERSION/radio-cdma-$DEVICE-$CDMARADIO.img
83  fi
84fi
85
86# Write flash-all.sh
87cat > tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
88#!/bin/sh
89
90# Copyright 2012 The Android Open Source Project
91#
92# Licensed under the Apache License, Version 2.0 (the "License");
93# you may not use this file except in compliance with the License.
94# You may obtain a copy of the License at
95#
96#      http://www.apache.org/licenses/LICENSE-2.0
97#
98# Unless required by applicable law or agreed to in writing, software
99# distributed under the License is distributed on an "AS IS" BASIS,
100# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
101# See the License for the specific language governing permissions and
102# limitations under the License.
103
104EOF
105if test "$UNLOCKBOOTLOADER" = "true"
106then
107cat >> tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
108fastboot oem unlock
109EOF
110fi
111if test "$ERASEALL" = "true"
112then
113cat >> tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
114fastboot erase boot
115fastboot erase cache
116fastboot erase recovery
117fastboot erase system
118fastboot erase userdata
119EOF
120fi
121if test "$XLOADER" != ""
122then
123cat >> tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
124fastboot flash xloader xloader-$DEVICE-$XLOADER.img
125EOF
126fi
127cat >> tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
128fastboot flash bootloader bootloader-$DEVICE-$BOOTLOADER.img
129EOF
130if test "$TWINBOOTLOADERS" = "true"
131then
132cat >> tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
133fastboot flash bootloader2 bootloader-$DEVICE-$BOOTLOADER.img
134EOF
135fi
136cat >> tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
137fastboot reboot-bootloader
138sleep $SLEEPDURATION
139EOF
140if test "$RADIO" != ""
141then
142cat >> tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
143fastboot flash radio radio-$DEVICE-$RADIO.img
144fastboot reboot-bootloader
145sleep $SLEEPDURATION
146EOF
147fi
148if test "$CDMARADIO" != ""
149then
150cat >> tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
151fastboot flash radio-cdma radio-cdma-$DEVICE-$CDMARADIO.img
152fastboot reboot-bootloader
153sleep $SLEEPDURATION
154EOF
155fi
156cat >> tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
157fastboot -w update image-$PRODUCT-$VERSION.zip
158EOF
159chmod a+x tmp/$PRODUCT-$VERSION/flash-all.sh
160
161# Write flash-all.bat
162cat > tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
163@ECHO OFF
164:: Copyright 2012 The Android Open Source Project
165::
166:: Licensed under the Apache License, Version 2.0 (the "License");
167:: you may not use this file except in compliance with the License.
168:: You may obtain a copy of the License at
169::
170::      http://www.apache.org/licenses/LICENSE-2.0
171::
172:: Unless required by applicable law or agreed to in writing, software
173:: distributed under the License is distributed on an "AS IS" BASIS,
174:: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
175:: See the License for the specific language governing permissions and
176:: limitations under the License.
177
178PATH=%PATH%;"%SYSTEMROOT%\System32"
179EOF
180if test "$UNLOCKBOOTLOADER" = "true"
181then
182cat >> tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
183fastboot oem unlock
184EOF
185fi
186if test "$ERASEALL" = "true"
187then
188cat >> tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
189fastboot erase boot
190fastboot erase cache
191fastboot erase recovery
192fastboot erase system
193fastboot erase userdata
194EOF
195fi
196if test "$XLOADER" != ""
197then
198cat >> tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
199fastboot flash xloader xloader-$DEVICE-$XLOADER.img
200EOF
201fi
202cat >> tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
203fastboot flash bootloader bootloader-$DEVICE-$BOOTLOADER.img
204EOF
205if test "$TWINBOOTLOADERS" = "true"
206then
207cat >> tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
208fastboot flash bootloader2 bootloader-$DEVICE-$BOOTLOADER.img
209EOF
210fi
211cat >> tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
212fastboot reboot-bootloader
213ping -n $SLEEPDURATION 127.0.0.1 >nul
214EOF
215if test "$RADIO" != ""
216then
217cat >> tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
218fastboot flash radio radio-$DEVICE-$RADIO.img
219fastboot reboot-bootloader
220ping -n $SLEEPDURATION 127.0.0.1 >nul
221EOF
222fi
223if test "$CDMARADIO" != ""
224then
225cat >> tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
226fastboot flash radio-cdma radio-cdma-$DEVICE-$CDMARADIO.img
227fastboot reboot-bootloader
228ping -n $SLEEPDURATION 127.0.0.1 >nul
229EOF
230fi
231cat >> tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
232fastboot -w update image-$PRODUCT-$VERSION.zip
233
234echo Press any key to exit...
235pause >nul
236exit
237EOF
238
239# Write flash-base.sh
240cat > tmp/$PRODUCT-$VERSION/flash-base.sh << EOF
241#!/bin/sh
242
243# Copyright 2012 The Android Open Source Project
244#
245# Licensed under the Apache License, Version 2.0 (the "License");
246# you may not use this file except in compliance with the License.
247# You may obtain a copy of the License at
248#
249#      http://www.apache.org/licenses/LICENSE-2.0
250#
251# Unless required by applicable law or agreed to in writing, software
252# distributed under the License is distributed on an "AS IS" BASIS,
253# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
254# See the License for the specific language governing permissions and
255# limitations under the License.
256
257EOF
258if test "$XLOADER" != ""
259then
260cat >> tmp/$PRODUCT-$VERSION/flash-base.sh << EOF
261fastboot flash xloader xloader-$DEVICE-$XLOADER.img
262EOF
263fi
264cat >> tmp/$PRODUCT-$VERSION/flash-base.sh << EOF
265fastboot flash bootloader bootloader-$DEVICE-$BOOTLOADER.img
266EOF
267if test "$TWINBOOTLOADERS" = "true"
268then
269cat >> tmp/$PRODUCT-$VERSION/flash-base.sh << EOF
270fastboot flash bootloader2 bootloader-$DEVICE-$BOOTLOADER.img
271EOF
272fi
273cat >> tmp/$PRODUCT-$VERSION/flash-base.sh << EOF
274fastboot reboot-bootloader
275sleep $SLEEPDURATION
276EOF
277if test "$RADIO" != ""
278then
279cat >> tmp/$PRODUCT-$VERSION/flash-base.sh << EOF
280fastboot flash radio radio-$DEVICE-$RADIO.img
281fastboot reboot-bootloader
282sleep $SLEEPDURATION
283EOF
284fi
285if test "$CDMARADIO" != ""
286then
287cat >> tmp/$PRODUCT-$VERSION/flash-base.sh << EOF
288fastboot flash radio-cdma radio-cdma-$DEVICE-$CDMARADIO.img
289fastboot reboot-bootloader
290sleep $SLEEPDURATION
291EOF
292fi
293chmod a+x tmp/$PRODUCT-$VERSION/flash-base.sh
294
295# Create the distributable package
296(cd tmp ; tar zcvf ../$PRODUCT-$VERSION-factory.tgz $PRODUCT-$VERSION)
297mv $PRODUCT-$VERSION-factory.tgz $PRODUCT-$VERSION-factory-$(sha1sum < $PRODUCT-$VERSION-factory.tgz | cut -b -8).tgz
298
299# Clean up
300rm -rf tmp
301