f1d5a9fb45c67ed8c4dbc283fd4d0b2f14a06f56
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / buildroot-2015.05 / board / cubietech / cubieboard / mkcubiecard.sh
1 #! /bin/sh
2 # mkCubieCard.sh v0.1:
3 # 2013, Carlo Caione <carlo.caione@gmail.com>
4 # heavely based on :
5 # mkA10card.sh v0.1
6 # 2012, Jason Plum <jplum@archlinuxarm.org>
7 # loosely based on :
8 # mkcard.sh v0.5
9 # (c) Copyright 2009 Graeme Gregory <dp@xora.org.uk>
10 # Licensed under terms of GPLv2
11 #
12 # Parts of the procudure base on the work of Denys Dmytriyenko
13 # http://wiki.omap.com/index.php/MMC_Boot_Format
14
15 IMAGES_DIR=$1
16 SPL_IMG=$IMAGES_DIR/sunxi-spl.bin
17 SPL_UBOOT=$IMAGES_DIR/u-boot-sunxi-with-spl.bin
18 UBOOT_IMG=$IMAGES_DIR/u-boot.bin
19 UIMAGE=$IMAGES_DIR/uImage
20 BIN_BOARD_FILE=$IMAGES_DIR/script.bin
21 ROOTFS=$IMAGES_DIR/rootfs.tar
22 BOOT_CMD_H=$IMAGES_DIR/boot.scr
23
24 export LC_ALL=C
25
26 if [ $# -ne 2 ]; then
27         echo "Usage: $0 <images_dir> <drive>"
28         exit 1;
29 fi
30
31 if [ `id -u` -ne 0 ]; then
32         echo "This script must be run as root" 1>&2
33         exit 1
34 fi
35
36 if [ ! -f $SPL_IMG  -a ! -f $SPL_UBOOT ] ||
37    [ ! -f $UBOOT_IMG ] ||
38    [ ! -f $UIMAGE ] ||
39    [ ! -f $BIN_BOARD_FILE ] ||
40    [ ! -f $ROOTFS ] ||
41    [ ! -f $BOOT_CMD_H ]; then
42         echo "File(s) missing."
43         exit 1
44 fi
45
46 DRIVE=$2
47 P1=`mktemp -d`
48 P2=`mktemp -d`
49
50 dd if=/dev/zero of=$DRIVE bs=1M count=3
51
52 SIZE=`fdisk -l $DRIVE | grep Disk | grep bytes | awk '{print $5}'`
53
54 echo DISK SIZE - $SIZE bytes
55
56
57 # ~2048, 16MB, FAT, bootable
58 # ~rest of drive, Ext4
59 {
60 echo 32,512,0x0C,*
61 echo 544,,,-
62 } | sfdisk -D $DRIVE
63
64 sleep 1
65
66 if [ -b ${DRIVE}1 ]; then
67         D1=${DRIVE}1
68         umount ${DRIVE}1
69         mkfs.vfat -n "boot" ${DRIVE}1
70 else
71         if [ -b ${DRIVE}p1 ]; then
72                 D1=${DRIVE}p1
73                 umount ${DRIVE}p1
74                 mkfs.vfat -n "boot" ${DRIVE}p1
75         else
76                 echo "Cant find boot partition in /dev"
77                 exit 1
78         fi
79 fi
80
81
82 if [ -b ${DRIVE}2 ]; then
83         D2=${DRIVE}2
84         umount ${DRIVE}2
85         mkfs.ext4 -L "Cubie" ${DRIVE}2
86 else
87         if [ -b ${DRIVE}p2 ]; then
88                 D2=${DRIVE}p2
89                 umount ${DRIVE}p2
90                 mkfs.ext4 -L "Cubie" ${DRIVE}p2
91         else
92                 echo "Cant find rootfs partition in /dev"
93                 exit 1
94         fi
95 fi
96
97 mount $D1 $P1
98 mount $D2 $P2
99
100 # write uImage
101 cp $UIMAGE $P1
102 # write board file
103 cp $BIN_BOARD_FILE $P1
104 # write u-boot script
105 cp $BOOT_CMD_H $P1
106 # write rootfs
107 tar -C $P2 -xvf $ROOTFS
108
109 sync
110
111 umount $D1
112 umount $D2
113
114 rm -fr $P1
115 rm -fr $P2
116
117 if [ -e $SPL_UBOOT ]; then
118         dd if=$SPL_UBOOT of=$DRIVE bs=1024 seek=8
119 else
120         # write SPL
121         dd if=$SPL_IMG of=$DRIVE bs=1024 seek=8
122         # write mele u-boot
123         dd if=$UBOOT_IMG of=$DRIVE bs=1024 seek=32
124 fi