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