summaryrefslogtreecommitdiffstats
blob: f8e4534b37afef787260b80b39f18a0a08e4526b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/bin/bash

usage ()
{
	echo "Usage: sudo fastboot.sh <options>";
	echo "options:";
	echo "  --help Show this message and exit"
	exit 1;
}

#no args case
if [ "$1" = "--help" ] ; then
	usage
fi

# Pre-packaged DB
export FASTBOOT=${FASTBOOT-"./fastboot"}
export PRODUCT_OUT=${PRODUCT_OUT-"./"}
export LD_LIBRARY_PATH=./

echo "Fastboot: $FASTBOOT"
echo "Image location: $PRODUCT_OUT"


# =============================================================================
# pre-run
# =============================================================================

# Verify fastboot program is available
# Verify user permission to run fastboot
# Verify fastboot detects a device, otherwise exit
if [ -f ${FASTBOOT} ]; then
	fastboot_status=`${FASTBOOT} devices 2>&1`
	if [ `echo $fastboot_status | grep -wc "no permissions"` -gt 0 ]; then
		cat <<-EOF >&2
		-------------------------------------------
		 Fastboot requires administrator permissions
		 Please run the script as root or create a
		 fastboot udev rule, e.g:

		 % cat /etc/udev/rules.d/99_android.rules
		   SUBSYSTEM=="usb",
		   SYSFS{idVendor}=="0451"
		   OWNER="<username>"
		   GROUP="adm"
		-------------------------------------------
		EOF
		exit 1
	elif [ "X$fastboot_status" = "X" ]; then
		echo "No device detected. Please ensure that" \
			 "fastboot is running on the target device"
                exit -1;
	else
		device=`echo $fastboot_status | awk '{print$1}'`
		echo -e "\nFastboot - device detected: $device\n"
	fi
else
	echo "Error: fastboot is not available at ${FASTBOOT}"
        exit -1;
fi

## poll the board to find out its configuration
#product=`${FASTBOOT} getvar product 2>&1 | grep product | awk '{print$2}'`
cpu=`${FASTBOOT} getvar cpu 2>&1         | grep cpu     | awk '{print$2}'`
cputype=`${FASTBOOT} getvar secure 2>&1  | grep secure  | awk '{print$2}'`
boardrev=`${FASTBOOT} getvar board_rev 2>&1  | grep board_rev  | awk '{print$2}' | cut -b 1`


# Make EMU = HS
if [ ${cputype} = "EMU" ] || [ ${cputype} = "HS" ]; then
	cputype="HS"
	xloader="${PRODUCT_OUT}u-boot-spl_HS_X-LOADER"
	uboot="${PRODUCT_OUT}${cputype}_u-boot.img"
# If fastboot does not support getvar default to GP
elif [ ${cputype} = "" ] || [ ${cputype} = "GP" ]; then
	cputype="GP"
	xloader="${PRODUCT_OUT}${cputype}_MLO"
	uboot="${PRODUCT_OUT}u-boot.img"
fi

# Based on cpu, decide the dtb to flash, default fall back to J6 and LCD 10

if [ ${cpu} = "DRA722" ]; then
	if [ ${boardrev} = "C" ]; then
		environment="${PRODUCT_OUT}dra72-evm-revc-lcd-osd101t2045.dtb"
	elif [ ${boardrev} = "A" ]; then
		environment="${PRODUCT_OUT}dra71-evm-lcd-auo-g101evn01.0.dtb"
	elif [ ${boardrev} = "1.3A" ] || [ ${boardrev} = "1.3B" ]; then
		environment="${PRODUCT_OUT}am571x-idk-lcd-osd101t2587.dtb"
	else
		environment="${PRODUCT_OUT}am571x-idk-lcd-osd101t2587.dtb"
	fi
elif [ ${cpu} = "DRA752" ]; then
	if [ ${boardrev} = "H" ]; then
		environment="${PRODUCT_OUT}dra7-evm-lcd-osd.dtb"
	elif [ ${boardrev} = "A" ]; then
		environment="${PRODUCT_OUT}am57xx-evm-reva3.dtb"
	else
		environment="${PRODUCT_OUT}am57xx-evm-reva3.dtb"
	fi
elif [ ${cpu} = "DRA762" ]; then
	if [ ${boardrev} = "1.3A" ] || [ ${boardrev} = "1.3B" ]; then
                environment="${PRODUCT_OUT}am574x-idk-lcd-osd101t2587.dtb"
	else
	environment="${PRODUCT_OUT}am574-idk.dtb"
	fi
else
	echo "CPU not detected, no matching dtb file found"
	echo "flashing default dtb, Review and Reflash correct dtb"
	environment="${PRODUCT_OUT}dra7-evm-lcd-osd.dtb"
fi

# Create the filename
bootimg="${PRODUCT_OUT}boot.img"
systemimg="${PRODUCT_OUT}system.img"
userdataimg="${PRODUCT_OUT}userdata.img"
cacheimg="${PRODUCT_OUT}cache.img"
recoveryimg="${PRODUCT_OUT}recovery.img"
vendorimg="${PRODUCT_OUT}vendor.img"

# Verify that all the files required for the fastboot flash
# process are available

if [ ! -e "${bootimg}" ] ; then
  echo "Missing ${bootimg}"
  exit -1;
fi
if [ ! -e "$xloader" ] ; then
  echo "Missing ${xloader}"
  exit -1;
fi
if [ ! -e "${uboot}" ] ; then
  echo "Missing ${uboot}"
  exit -1;
fi
if [ ! -e "${environment}" ] ; then
  echo "Missing ${environment}"
  exit -1;
else
  echo "DTB = ${environment}"
fi
if [ ! -e "${systemimg}" ] ; then
  echo "Missing ${systemimg}"
  exit -1;
fi
if [ ! -e "${userdataimg}" ] ; then
  echo "Missing ${userdataimg}"
  exit -1;
fi
if [ ! -e "${cacheimg}" ] ; then
  echo "Missing ${cacheimg}"
  exit -1;
fi
if [ ! -e "${recoveryimg}" ] ; then
  echo "Missing ${recoveryimg}"
  exit -1;
fi
if [ ! -e "${vendorimg}" ] ; then
  echo "Missing ${vendorimg}"
  exit -1;
fi


echo "Create GPT partition table"
${FASTBOOT} oem format

if [ ${boardrev} = "1.3A" ] || [ ${boardrev} = "1.3B" ]; then
        echo "Setting target for bootloader to spi"
        ${FASTBOOT} oem spi
else
        echo "Setting target for bootloader to emmc"
        ${FASTBOOT} oem mmc
fi

sleep 3

echo "Flashing bootloader....."
echo "   xloader:     ${xloader}"
${FASTBOOT} flash xloader	${xloader}

sleep 3
echo "   bootloader:  ${uboot}"
${FASTBOOT} flash bootloader	${uboot}

#echo "Reboot: make sure new bootloader runs..."
${FASTBOOT} reboot-bootloader

sleep 5

echo "Re-creating GPT partition table with new bootloader"
${FASTBOOT} oem format

echo "Flash android partitions"
${FASTBOOT} flash boot		${bootimg}
echo "Flashing environemnt....."
echo "   environment: ${environment}"
${FASTBOOT} flash environment	${environment}
${FASTBOOT} flash recovery	${recoveryimg}
${FASTBOOT} flash system	${systemimg}
${FASTBOOT} flash vendor	${vendorimg}

userdataimg_orig="${userdataimg}.orig"
if [ ! -f $userdataimg_orig ]; then
	cp $userdataimg $userdataimg_orig
else
	cp $userdataimg_orig $userdataimg
fi

echo "Resizing userdata.img"
resizefail=0
userdatasize=`${FASTBOOT} getvar userdata_size 2>&1 | grep "userdata_size" | awk '{print$2}'`
if [ -n "$userdatasize" ]; then
	while [ 1 ];do
		echo Current userdata partition size=${userdatasize} KB
		if [ -d "./data" ]; then
			echo "Removing data"
			rm -rf ./data || resizefail=1
			if [ $resizefail -eq 1 ]; then
				echo "unable to remove data folder" && break
			fi
		fi
		mkdir ./data
		./simg2img ${userdataimg} ${userdataimg}.raw
		mount -o loop -o grpid -t ext4 ${userdataimg}.raw ./data || resizefail=1
		if [ $resizefail -eq 1 ]; then
			echo "Mount failed" && break
		fi
		./make_ext4fs -s -l ${userdatasize}K -a data ${userdataimg} data/
		sync
		umount data
		sync
		rm -rf ./data
		rm ${userdataimg}.raw
		break
	done
else
	resizefail=1
fi

if [ $resizefail -eq 1 ]; then
	echo "userdata resize failed."
	echo "Eg: sudo ./fastboot.sh"
	echo "For now, we are defaulting to original userdata.img"
	cp $userdataimg_orig $userdataimg
fi
${FASTBOOT} flash userdata ${userdataimg}

#flash cache.img
${FASTBOOT} flash cache		${cacheimg}

#reboot now
#${FASTBOOT} reboot

#if [ $resizefail -eq 1 ]; then
#	echo "--------------------------------------------------"
#	echo "Attempt was made to resize the userdata partition image"
#	echo "to the size available on your SOM. But it failed either"
#	echo "because it failed to remove existing ./data folder or because"
#	echo "you are not running this script with superuser privileges"
#	echo "Don't panic! The script just loaded the original userdata.img"
#	echo "so, things should just work as expected. Just that the size"
#	echo "of /data will be smaller on target."
#	echo ""
#	echo "If you really want to resize userdata.img, remove any existing"
#	echo "./data folder and run \"sudo ./fastboot.sh\""
#	echo "For now, we are defaulting to original userdata.img"
#	echo "--------------------------------------------------"
#fi