add cf2nand script for copying a cf card installation to nand flash automatically...
[openwrt.git] / package / base-files / rb532-2.6 / sbin / cf2nand
1 #!/bin/sh
2 . /etc/functions.sh
3
4 copy_kernel() {
5 local input="$1"
6 local output="$2"
7 local cmdline="$3"
8 size="$(echo -n "$cmdline" | wc -c)"
9 dd if="$input" bs=3M count=1 | (
10 dd bs=4112 count=1
11 echo -n "$cmdline"
12 dd if=/dev/zero bs="$((512 - $size))" count=1
13 dd bs=512 count=1 of=/dev/null
14 cat
15 ) > "$output"
16 }
17
18 fstype="$(mount | grep ' / ' | awk '{print $5}')"
19 [ -d /tmp/cf2nand ] && {
20 echo "/tmp/cf2nand already exists"
21 exit 1
22 }
23
24 mkdir /tmp/cf2nand
25 mkdir /tmp/cf2nand/rootfs
26 mount -t "$fstype" /dev/root /tmp/cf2nand/rootfs || {
27 echo "Mounting rootfs failed."
28 exit 1
29 }
30
31 boot="$(find_mtd_part 'RouterBoard NAND Boot')"
32 main="$(find_mtd_part 'RouterBoard NAND Main')"
33 [ -z "$boot" -o -z "$main" ] && {
34 echo "Cannot find NAND Flash partitions"
35 exit 1
36 }
37
38 echo "Erasing filesystem..."
39 mtd erase Boot 2>/dev/null >/dev/null
40 mtd erase Main 2>/dev/null >/dev/null
41
42 mkdir /tmp/cf2nand/p1
43 mkdir /tmp/cf2nand/p2
44 mount -t yaffs2 "$boot" /tmp/cf2nand/p1
45 mount -t yaffs2 "$main" /tmp/cf2nand/p2
46
47 echo "Copying kernel..."
48 copy_kernel /dev/cf/card0/part1 /tmp/cf2nand/p1/kernel "root=/dev/mtdblock1 rootfstype=yaffs2 " 2>/dev/null >/dev/null
49 umount /tmp/cf2nand/p1
50 rmdir /tmp/cf2nand/p1
51
52 echo "Copying filesystem..."
53 ( cd /tmp/cf2nand/rootfs; tar c . ) | ( cd /tmp/cf2nand/p2; tar x )
54 sync
55 umount /tmp/cf2nand/p2
56 rmdir /tmp/cf2nand/p2
57
58 umount /tmp/cf2nand/rootfs
59 rmdir /tmp/cf2nand/rootfs
60 rmdir /tmp/cf2nand
61
This page took 0.048904 seconds and 5 git commands to generate.