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
104if ! [ \$(\$(which fastboot) --version | grep "version" | cut -c18-23 | sed 's/\.//g' ) -ge 2802 ]; then
105  echo "fastboot too old; please download the latest version at https://developer.android.com/studio/releases/platform-tools.html"
106  exit 1
107fi
108EOF
109if test "$UNLOCKBOOTLOADER" = "true"
110then
111cat >> tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
112fastboot oem unlock
113EOF
114fi
115if test "$ERASEALL" = "true"
116then
117cat >> tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
118fastboot erase boot
119fastboot erase cache
120fastboot erase recovery
121fastboot erase system
122fastboot erase userdata
123EOF
124fi
125if test "$XLOADER" != ""
126then
127cat >> tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
128fastboot flash xloader xloader-$DEVICE-$XLOADER.img
129EOF
130fi
131cat >> tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
132fastboot flash bootloader bootloader-$DEVICE-$BOOTLOADER.img
133EOF
134if test "$TWINBOOTLOADERS" = "true"
135then
136cat >> tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
137fastboot flash bootloader2 bootloader-$DEVICE-$BOOTLOADER.img
138EOF
139fi
140cat >> tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
141fastboot reboot-bootloader
142sleep $SLEEPDURATION
143EOF
144if test "$RADIO" != ""
145then
146cat >> tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
147fastboot flash radio radio-$DEVICE-$RADIO.img
148fastboot reboot-bootloader
149sleep $SLEEPDURATION
150EOF
151fi
152if test "$CDMARADIO" != ""
153then
154cat >> tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
155fastboot flash radio-cdma radio-cdma-$DEVICE-$CDMARADIO.img
156fastboot reboot-bootloader
157sleep $SLEEPDURATION
158EOF
159fi
160cat >> tmp/$PRODUCT-$VERSION/flash-all.sh << EOF
161fastboot -w update image-$PRODUCT-$VERSION.zip
162EOF
163chmod a+x tmp/$PRODUCT-$VERSION/flash-all.sh
164
165# Write flash-all.bat
166cat > tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
167@ECHO OFF
168:: Copyright 2012 The Android Open Source Project
169::
170:: Licensed under the Apache License, Version 2.0 (the "License");
171:: you may not use this file except in compliance with the License.
172:: You may obtain a copy of the License at
173::
174::      http://www.apache.org/licenses/LICENSE-2.0
175::
176:: Unless required by applicable law or agreed to in writing, software
177:: distributed under the License is distributed on an "AS IS" BASIS,
178:: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
179:: See the License for the specific language governing permissions and
180:: limitations under the License.
181
182PATH=%PATH%;"%SYSTEMROOT%\System32"
183EOF
184if test "$UNLOCKBOOTLOADER" = "true"
185then
186cat >> tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
187fastboot oem unlock
188EOF
189fi
190if test "$ERASEALL" = "true"
191then
192cat >> tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
193fastboot erase boot
194fastboot erase cache
195fastboot erase recovery
196fastboot erase system
197fastboot erase userdata
198EOF
199fi
200if test "$XLOADER" != ""
201then
202cat >> tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
203fastboot flash xloader xloader-$DEVICE-$XLOADER.img
204EOF
205fi
206cat >> tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
207fastboot flash bootloader bootloader-$DEVICE-$BOOTLOADER.img
208EOF
209if test "$TWINBOOTLOADERS" = "true"
210then
211cat >> tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
212fastboot flash bootloader2 bootloader-$DEVICE-$BOOTLOADER.img
213EOF
214fi
215cat >> tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
216fastboot reboot-bootloader
217ping -n $SLEEPDURATION 127.0.0.1 >nul
218EOF
219if test "$RADIO" != ""
220then
221cat >> tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
222fastboot flash radio radio-$DEVICE-$RADIO.img
223fastboot reboot-bootloader
224ping -n $SLEEPDURATION 127.0.0.1 >nul
225EOF
226fi
227if test "$CDMARADIO" != ""
228then
229cat >> tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
230fastboot flash radio-cdma radio-cdma-$DEVICE-$CDMARADIO.img
231fastboot reboot-bootloader
232ping -n $SLEEPDURATION 127.0.0.1 >nul
233EOF
234fi
235cat >> tmp/$PRODUCT-$VERSION/flash-all.bat << EOF
236fastboot -w update image-$PRODUCT-$VERSION.zip
237
238echo Press any key to exit...
239pause >nul
240exit
241EOF
242
243# Write flash-base.sh
244cat > tmp/$PRODUCT-$VERSION/flash-base.sh << EOF
245#!/bin/sh
246
247# Copyright 2012 The Android Open Source Project
248#
249# Licensed under the Apache License, Version 2.0 (the "License");
250# you may not use this file except in compliance with the License.
251# You may obtain a copy of the License at
252#
253#      http://www.apache.org/licenses/LICENSE-2.0
254#
255# Unless required by applicable law or agreed to in writing, software
256# distributed under the License is distributed on an "AS IS" BASIS,
257# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
258# See the License for the specific language governing permissions and
259# limitations under the License.
260
261if ! [ \$(\$(which fastboot) --version | grep "version" | cut -c18-23 | sed 's/\.//g' ) -ge 2802 ]; then
262  echo "fastboot too old; please download the latest version at https://developer.android.com/studio/releases/platform-tools.html"
263  exit 1
264fi
265EOF
266if test "$XLOADER" != ""
267then
268cat >> tmp/$PRODUCT-$VERSION/flash-base.sh << EOF
269fastboot flash xloader xloader-$DEVICE-$XLOADER.img
270EOF
271fi
272cat >> tmp/$PRODUCT-$VERSION/flash-base.sh << EOF
273fastboot flash bootloader bootloader-$DEVICE-$BOOTLOADER.img
274EOF
275if test "$TWINBOOTLOADERS" = "true"
276then
277cat >> tmp/$PRODUCT-$VERSION/flash-base.sh << EOF
278fastboot flash bootloader2 bootloader-$DEVICE-$BOOTLOADER.img
279EOF
280fi
281cat >> tmp/$PRODUCT-$VERSION/flash-base.sh << EOF
282fastboot reboot-bootloader
283sleep $SLEEPDURATION
284EOF
285if test "$RADIO" != ""
286then
287cat >> tmp/$PRODUCT-$VERSION/flash-base.sh << EOF
288fastboot flash radio radio-$DEVICE-$RADIO.img
289fastboot reboot-bootloader
290sleep $SLEEPDURATION
291EOF
292fi
293if test "$CDMARADIO" != ""
294then
295cat >> tmp/$PRODUCT-$VERSION/flash-base.sh << EOF
296fastboot flash radio-cdma radio-cdma-$DEVICE-$CDMARADIO.img
297fastboot reboot-bootloader
298sleep $SLEEPDURATION
299EOF
300fi
301chmod a+x tmp/$PRODUCT-$VERSION/flash-base.sh
302
303# Create the distributable package
304(cd tmp ; zip -r ../$PRODUCT-$VERSION-factory.zip $PRODUCT-$VERSION)
305mv $PRODUCT-$VERSION-factory.zip $PRODUCT-$VERSION-factory-$(sha256sum < $PRODUCT-$VERSION-factory.zip | cut -b -8).zip
306
307# Clean up
308rm -rf tmp
309