-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflashcard
executable file
·141 lines (114 loc) · 3.52 KB
/
flashcard
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
#!/usr/bin/env bash
COL_GREEN="\e[1;32m"
COL_NORMAL="\e[m"
err() {
echo -e "$1" >&2
exit 1
}
usage() {
echo -e "Usage: ${0##*\/} [-k | -u] [-h] [device_node]" >&2
}
if [ $UID -ne 0 ]; then
err "only root can do that"
fi
OPTS=`getopt -o kuh? -l help -l kernel-only -l uboot-only -- "$@"`
if [ "$?" != 0 ]; then
usage
exit 1
fi
eval set -- "$OPTS"
while true; do
case "$1" in
-h|--help|?)
usage
exit 0
;;
-k|--kernel-only)
UPLOAD_KERNEL_ONLY=1
shift
;;
-u|--uboot-only)
UPLOAD_UBOOT_ONLY=1
shift
;;
--)
shift; break;;
esac
done
if [ $# -eq 0 ]; then
echo -en "${COL_GREEN}Please input the device node of the card (e.g. /dev/sdb):${COL_NORMAL} "
read devnode
else
devnode="$1"
fi
if [ `echo $devnode | grep "[0-9]$"` ] ; then
err "Error: please specify a whole disk instead of a partition."
fi
if ! [ -b $devnode ] ; then
err "Error: $devnode does not exist."
fi
upload_kernel() {
echo -e "\n${COL_GREEN}--${COL_NORMAL}"
echo -e "${COL_GREEN}Flashing uImage to ${devnode}...${COL_NORMAL}"
dd if=kernel/arch/arm/boot/uImage of=${devnode} bs=512 seek=2048 conv=fsync
}
upload_uboot() {
echo -e "\n${COL_GREEN}--${COL_NORMAL}"
echo -e "${COL_GREEN}Flashing u-boot.bin to ${devnode}...${COL_NORMAL}"
dd if=u-boot/u-boot.bin of=${devnode} bs=512 seek=2 skip=2 conv=fsync
}
ishdd=0
for b in /dev/disk/by-id/* ; do
echo $b | grep HARDDISK >/dev/null || continue
ls -l $b | grep "${devnode##*/}$" >/dev/null && ishdd=1 && break
done
if [ $ishdd -ne "0" ] ; then
echo -en "${COL_GREEN}${devnode} is a hard drive. Continue [y/N]?${COL_NORMAL} "
read ans
echo $ans | grep -i "y" > /dev/null || err "Interrupted by user."
fi
echo -en "${COL_GREEN}Things on ${devnode} may probably be erased. Continue [y/N]?${COL_NORMAL} "
read ans
echo $ans | grep -i "y" > /dev/null || err "Interrupted by user."
echo -e "\n${COL_GREEN}--${COL_NORMAL}"
echo -e "${COL_GREEN}Unmounting all mounted partition on ${devnode}...${COL_NORMAL}"
mount | grep "^$devnode" | awk '{print $3}' | while read mpoint ; do
umount $mpoint
done
if [ $UPLOAD_KERNEL_ONLY ]; then
upload_kernel
exit $?
fi
if [ $UPLOAD_UBOOT_ONLY ]; then
upload_uboot
exit $?
fi
echo -e "\n${COL_GREEN}--${COL_NORMAL}"
echo -e "${COL_GREEN}Deleting partition table on ${devnode}...${COL_NORMAL}"
dd if=/dev/zero of=${devnode} bs=512 count=4096 conv=fsync
dd if=/dev/zero of=${devnode} bs=512 count=4096 conv=fsync seek=$((`blockdev --getsz ${devnode}` - 4096))
partprobe
upload_uboot
upload_kernel
echo -e "\n${COL_GREEN}--${COL_NORMAL}"
echo -e "${COL_GREEN}Manipulating partiton table on ${devnode}...${COL_NORMAL}"
(echo n; echo p; echo 1; echo 16384; echo; echo w;) | fdisk ${devnode}
echo -e "\n${COL_GREEN}--${COL_NORMAL}"
echo -e "${COL_GREEN}Creating filesystem on ${devnode}1...${COL_NORMAL}"
mkfs.ext3 ${devnode}1
echo -e "\n${COL_GREEN}--${COL_NORMAL}"
echo -e "${COL_GREEN}Mounting ${devnode}1 on sdcard/...${COL_NORMAL}"
mkdir sdcard 2>/dev/null
mount ${devnode}1 sdcard
echo -e "\n${COL_GREEN}--${COL_NORMAL}"
echo -e "${COL_GREEN}Copying rootfs to sdcard/...${COL_NORMAL}"
cd sdcard
tar --numeric-owner -xzvf ../prebuilts/ubuntu/oneiric.tgz
cd ..
echo -e "\n${COL_GREEN}--${COL_NORMAL}"
echo -e "${COL_GREEN}Unmounting sdcard/...${COL_NORMAL}"
umount sdcard
cd prebuilts/ubuntu/rootfs_add/
./combine_rootfs.sh $1
cd -
echo -e "\n${COL_GREEN}Done.${COL_NORMAL}"