forked from khadas/u-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-sdcard.sh
executable file
·145 lines (114 loc) · 4.02 KB
/
update-sdcard.sh
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
#!/usr/bin/env bash
# Copyright 2017 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
set -eu
get_confirmation() {
echo -n "Press 'y' to confirm: "
read CONFIRM
if [[ "$CONFIRM" != "y" ]]; then
echo "[format_sdcard] Aborted due to invalid confirmation"
exit 1
fi
}
if [[ $OSTYPE != "linux-gnu" ]]; then
echo "[format_sdcard] Script is currently Linux-exclusive"
exit 1
fi
SCRIPT_DIR=$( cd $( dirname "${BASH_SOURCE[0]}" ) && pwd)
lsblk
echo "Enter the name of a block device to format: "
echo " This will probably be of the form 'sd[letter]', like 'sdc'"
echo -n "> "
read DEVICE
# Ensure that device exists
echo -n "[format_sdcard] Checking that device exists: $DEVICE ..."
DEVICE_PATH="/dev/$DEVICE"
if [[ ! -e "$DEVICE_PATH" ]]; then
echo " FAILED"
echo "[format_sdcard] ERROR: This device does not exist: $DEVICE_PATH"
exit 1
fi
echo " SUCCESS"
# Ensure that the device is a real block device
echo -n "[format_sdcard] Checking that device is a known block device..."
if [[ ! -e "/sys/block/$DEVICE" ]]; then
echo " FAILED"
echo "[format_sdcard] ERROR: /sys/block/$DEVICE does not exist."
echo " Does $DEVICE refer to a partition?"
exit 1
fi
echo " SUCCESS"
# Try to check that the device is a USB stick
echo -n "[format_sdcard] Checking if device is USB: $DEVICE ..."
READLINK_USB=$(readlink -f "/sys/class/block/$DEVICE/device" | { grep -i "usb" || true; })
if [[ -z "$READLINK_USB" ]]; then
echo " FAILED"
echo "[format_sdcard] ERROR: Cannot confirm that device is a USB stick"
echo "[format_sdcard] ERROR: Please insert USB stick and retry"
exit 1
fi
echo " SUCCESS"
sudo umount "${DEVICE_PATH}1" || true
# Ensure the device is not mounted
echo -n "[format_sdcard] Checking that device is not mounted: $DEVICE ..."
if [[ -n $(df -Hl | grep "$DEVICE") ]]; then
echo " FAILED"
echo "[format_sdcard] ERROR: Your device appears to be mounted: "
echo "..."
df -Hl | grep "$DEVICE"
echo "..."
echo "[format_sdcard] ERROR: Please unmount your device and retry"
exit 1
fi
echo " SUCCESS"
# Confirm that the user knows what they are doing
sudo -v -p "[sudo] Enter password to confirm information about device: "
sudo sfdisk -l "$DEVICE_PATH"
echo "[format_sdcard] ABOUT TO COMPLETELY WIPE / FORMAT: $DEVICE_PATH"
get_confirmation
echo "[format_sdcard] ARE YOU 100% SURE? This will hurt if you get it wrong."
get_confirmation
# Create three new partitions on the device, 1GB for root and divide the
# remaining space into 2 pieces for data and bootfs
# Determine how many bytes we have available on this disk.
BLK_DEV_SIZE=`sudo blockdev --getsize64 $DEVICE_PATH`
BLK_DEV_SIZE_MB=$((BLK_DEV_SIZE/1048576))
if [ "$BLK_DEV_SIZE_MB" -lt 3000 ]; then
echo " FAILED"
echo "[format_sdcard] ERROR: SD Card must be at least 4GB"
exit 1
fi
sudo dd if=/dev/zero of="$DEVICE_PATH" bs=512 count=1
DATA_PTN_START=4259840
DATA_PTN_END=4521983
DATA_PTN_SIZE=$((DATA_PTN_END-DATA_PTN_START+1))
DATA_PTN_SIZE_B=$(((DATA_PTN_END - DATA_PTN_START + 1)*512))
echo "Using Data Partition Size = ${DATA_PTN_SIZE_B} bytes"
sudo sfdisk -uS "$DEVICE_PATH" << EOF
${DATA_PTN_START} ${DATA_PTN_SIZE} 0x0b -
EOF
DATA_PTN_PATH="${DEVICE_PATH}1"
sudo mkfs.fat ${DEVICE_PATH}1
# Function to attempt unmounting a mount point up to three times, sleeping
# a couple of seconds between attempts.
function umount_retry() {
set +e
TRIES=0
while (! sudo umount $1); do
((TRIES++))
if [[ ${TRIES} > 2 ]]; then
echo "[format_sdcard] Unable to umount $0"
exit 1
fi
sleep 2
done
set -e
}
# Copy the necessary files to the root partition
MOUNT_PATH=`mktemp -d`
sudo mount "$DATA_PTN_PATH" "$MOUNT_PATH"
trap "umount_retry \"${MOUNT_PATH}\" && rm -rf \"${MOUNT_PATH}\" && echo \"Unmounted succesfully\"" INT TERM EXIT
sudo dd if=fip/gxl/u-boot.bin.sd.bin of="$DEVICE_PATH" conv=fsync,notrunc bs=1 count=444
sudo dd if=fip/gxl/u-boot.bin.sd.bin of="$DEVICE_PATH" conv=fsync,notrunc bs=512 skip=1 seek=1
echo " SUCCESS"