summaryrefslogtreecommitdiff
path: root/brillo-provision-dragonboard410c.sh
blob: 31f108bae691dbdfae60316aa62dff11b94ab840 (plain)
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
#!/bin/bash

#
# Copyright 2015 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

set -e

# Location of where the Brillo OS image is built.
BRILLO_OUT_DIR=${BRILLO_OUT_DIR:-${ANDROID_PRODUCT_OUT}}

# Location of binary blobs supplied by the vendor.
VENDOR_DIR=$(dirname "${0}")/vendor_partitions

# Check if user really wants to do this.
echo "This script will repartition your Dragonboard 410c device so it "
echo "can be used with Brillo."
echo ""
echo "When the script completes, the device will contain OS image from"
echo "${BRILLO_OUT_DIR} and vendor-specific blobs"
echo "from ${VENDOR_DIR}"
echo ""
echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
echo ""
echo "Before continuing, please ensure that your device is connected "
echo "and in fastboot mode. This can be achieved by either powering"
echo "the board with Vol- pressed down or by issuing the command"
echo "\"reboot bootloader\" from a shell on the device."
echo ""
read -p "Are you sure you want to do this (yes/no)? "
if [[ "${REPLY}" != "yes" ]] ; then
  echo "Not taking any action. Exiting."
  exit 1
fi

# Check that user has gdisk installed.
if ! SGDISK="$(type -p sgdisk)" || [ -z "${SGDISK}" ] ; then
  echo ""
  echo "The sgdisk command is not installed. You may install it via "
  echo "e.g. \"sudo apt-get install gdisk\"."
  exit 1
fi

# Create temporary working directory and ensure we clean it up when
# we're done.
WORKDIR=$(mktemp --tmpdir --directory brillo-provisioning.XXXXXXXXXX)

function cleanup() {
  rm -rf "${WORKDIR}"
}

trap cleanup INT
trap cleanup EXIT

DISK_IMAGE=${WORKDIR}/diskimage.bin
DISK_IMAGE_SIZE_BYTES=$((2048*1024*1024))

# The GUID partition type for a Linux partition.
LINUX_GUID_TYPE="0fc63daf-8483-4772-8e79-3d69d8477de4"

# The BOARD_PARTITONS variable is used to specify what the
# partitioning looks like.
BOARD_PARTITIONS=""

# Vendor specific partitions.
BOARD_PARTITIONS+="modem:ebd0a0a2-b9e5-4433-87c0-68b6b72699c7:65536K:${VENDOR_DIR}/modem.bin "
BOARD_PARTITIONS+="fsc:57b90a16-22c9-e33b-8f5d-0e81686a68cb:1K:${VENDOR_DIR}/fsc.bin "
BOARD_PARTITIONS+="ssd:2c86e742-745e-4fdd-bfd8-b6a7ac638772:8K: "
BOARD_PARTITIONS+="sbl1:dea0ba2c-cbdd-4805-b4f9-f428251c3e98:512K:${VENDOR_DIR}/sbl1.bin "
BOARD_PARTITIONS+="sbl1bak:ebd0a0a2-b9e5-4433-87c0-68b6b72699c7:512K:${VENDOR_DIR}/sbl1.bin "
BOARD_PARTITIONS+="rpm:098df793-d712-413d-9d4e-89d711772228:512K:${VENDOR_DIR}/rpm.bin "
BOARD_PARTITIONS+="rpmbak:ebd0a0a2-b9e5-4433-87c0-68b6b72699c7:512K:${VENDOR_DIR}/rpm.bin "
BOARD_PARTITIONS+="tz:a053aa7f-40b8-4b1c-ba08-2f68ac71a4f4:512K:${VENDOR_DIR}/tz.bin "
BOARD_PARTITIONS+="tzbak:ebd0a0a2-b9e5-4433-87c0-68b6b72699c7:512K:${VENDOR_DIR}/tz.bin "
BOARD_PARTITIONS+="hyp:e1a6a689-0c8d-4cc6-b4e8-55a4320fbd8a:512K:${VENDOR_DIR}/hyp.bin "
BOARD_PARTITIONS+="hypbak:ebd0a0a2-b9e5-4433-87c0-68b6b72699c7:512K:${VENDOR_DIR}/hyp.bin "
BOARD_PARTITIONS+="modemst1:ebbeadaf-22c9-e33b-8f5d-0e81686a68cb:1536K:${VENDOR_DIR}/modemst1.bin "
BOARD_PARTITIONS+="modemst2:0a288b1f-22c9-e33b-8f5d-0e81686a68cb:1536K:${VENDOR_DIR}/modemst2.bin "
BOARD_PARTITIONS+="DDR:20a0c19c-286a-42fa-9ce7-f64c3226a794:32K:${VENDOR_DIR}/DDR.bin "
BOARD_PARTITIONS+="fsg:638ff8e2-22c9-e33b-8f5d-0e81686a68cb:1536K:${VENDOR_DIR}/fsg.bin "
BOARD_PARTITIONS+="sec:303e6ac3-af15-4c54-9e9b-d9a8fbecf401:16K:${VENDOR_DIR}/sec.bin "
BOARD_PARTITIONS+="aboot:400ffdcd-22e0-47e7-9a23-f16ed9382388:1024K:${VENDOR_DIR}/aboot.bin "
BOARD_PARTITIONS+="abootbak:ebd0a0a2-b9e5-4433-87c0-68b6b72699c7:1024K:${VENDOR_DIR}/aboot.bin "

# The Android misc partition.
#
# TODO(zeuthen): Use a host-version of bootctl to populate the misc
# partition with data indicating that slot A is GOOD.
BOARD_PARTITIONS+="misc:${LINUX_GUID_TYPE}:1024K: "

# TODO(zeuthen): replace {boot,system} by just a single boot partition
# containing the Brillo Boot Selector kernel and initramfs.
BOARD_PARTITIONS+="boot:${LINUX_GUID_TYPE}:32M:${BRILLO_OUT_DIR}/boot.img "
BOARD_PARTITIONS+="system:${LINUX_GUID_TYPE}:256M:${BRILLO_OUT_DIR}/system.img "

# Brillo-specific partitions.

# TODO(zeuthen): For now, this partition type GUID is used for all
# partitions. We likely want type GUIDs depending on A/B and usage.
BRILLO_GUID_TYPE="7762a6b9-9d60-476a-8d40-a6a8850f80e9"

# For now, put the OS in slot A and make slot B empty.
BOARD_PARTITIONS+="boot_a:${BRILLO_GUID_TYPE}:32M:${BRILLO_OUT_DIR}/boot.img "
BOARD_PARTITIONS+="system_a:${BRILLO_GUID_TYPE}:256M:${BRILLO_OUT_DIR}/system.img "
BOARD_PARTITIONS+="oem_a:${BRILLO_GUID_TYPE}:64M: "

BOARD_PARTITIONS+="boot_b:${BRILLO_GUID_TYPE}:32M: "
BOARD_PARTITIONS+="system_b:${BRILLO_GUID_TYPE}:256M: "
BOARD_PARTITIONS+="oem_b:${BRILLO_GUID_TYPE}:64M: "

# Last partition takes up the rest of the disk - we use this for
# userdata which is created with the mkuserimg.sh tool.
echo "Creating \"userdata\" file system."
USER_DATA_IMAGE="${WORKDIR}/userdata.bin"
mkuserimg.sh usrimg "${USER_DATA_IMAGE}" ext4 /data 256M > /dev/null
BOARD_PARTITIONS+="userdata:${BRILLO_GUID_TYPE}:256M:${USER_DATA_IMAGE} "

# Create disk image file and add partitions specified in
# BOARD_PARTITIONS in order.
echo "Creating sparse disk image of size ${DISK_IMAGE_SIZE_BYTES} bytes."
rm -f "${DISK_IMAGE}"
dd if=/dev/zero of="${DISK_IMAGE}" bs=1 count=0 \
  seek=${DISK_IMAGE_SIZE_BYTES} status=none
${SGDISK} --clear --mbrtogpt "${DISK_IMAGE}" > /dev/null

partnumber=1
for i in ${BOARD_PARTITIONS}; do
  arr=(${i//:/ })
  partname=${arr[0]}
  parttype=${arr[1]}
  partsize=${arr[2]}
  partdata=${arr[3]}
  offset_sectors=$(${SGDISK} --first-aligned-in-largest ${DISK_IMAGE})
  echo "Creating partition ${partnumber}: \"${partname}\" of size ${partsize}"
  ${SGDISK} --new=${partnumber}:0:+${partsize} \
    --typecode=${partnumber}:${parttype} \
    --change-name=${partnumber}:${partname} \
    ${DISK_IMAGE} > /dev/null

  # TODO(zeuthen): Extend this tool so it can be used to create disk
  # images suitable for SD cards. Basically, it entails only copying
  # the blobs into place like it's done in the commented code below
  # and then putting the disk image somewhere the user expects.
  #
  # if [ "${partdata}" != "" ] ; then
  #   dd if=${partdata} of=${DISK_IMAGE} bs=512 seek=${offset_sectors} \
  #     count=${partsize} iflag=count_bytes conv=notrunc status=none
  # fi
  partnumber=$((${partnumber} + 1))
done

# Now send to device via fastboot - first we need to compute a file
# that contains the MBR, primary GPT and secondary GPT concatenated
# together. This is board-specific and what the board expects.
PARTITION_TABLES_FILE=${WORKDIR}/partition-tables.bin
dd if=${DISK_IMAGE} of=${PARTITION_TABLES_FILE} bs=512 count=34 status=none
dd if=${DISK_IMAGE} of=${PARTITION_TABLES_FILE} bs=512 seek=34 count=33 \
  skip=$((${DISK_IMAGE_SIZE_BYTES}/512 - 33)) count=33 conv=notrunc status=none

# Send partition file.
fastboot flash partition ${PARTITION_TABLES_FILE}

# Send each individual partition.
partnumber=1
for i in $BOARD_PARTITIONS; do
  arr=(${i//:/ })
  partname=${arr[0]}
  parttype=${arr[1]}
  partsize=${arr[2]}
  partdata=${arr[3]}
  if [ "${partdata}" != "" ] ; then
    fastboot flash ${partname} ${partdata}
  fi
  partnumber=$((${partnumber} + 1))
done