f8576beaa4ec692f28286d4e4d322f8828ee3713
[openwrt/openwrt.git] / package / base-files / rb532 / 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 case "$fstype" in
20 ext2|jffs2) echo "Copying from $fstype to yaffs2";;
21 *) echo "Invalid filesystem."; exit 1;;
22 esac
23
24 [ -d /tmp/cf2nand ] && {
25 echo "/tmp/cf2nand already exists"
26 exit 1
27 }
28
29 mkdir /tmp/cf2nand
30 mkdir /tmp/cf2nand/rootfs
31 mount -t "$fstype" /dev/root /tmp/cf2nand/rootfs || {
32 echo "Mounting rootfs failed."
33 exit 1
34 }
35
36 boot="$(find_mtd_part 'RouterBoard NAND Boot')"
37 main="$(find_mtd_part 'RouterBoard NAND Main')"
38 [ -z "$boot" -o -z "$main" ] && {
39 echo "Cannot find NAND Flash partitions"
40 exit 1
41 }
42
43 echo "Erasing filesystem..."
44 mtd erase Boot 2>/dev/null >/dev/null
45 mtd erase Main 2>/dev/null >/dev/null
46
47 mkdir /tmp/cf2nand/p1
48 mkdir /tmp/cf2nand/p2
49 mount -t yaffs2 "$boot" /tmp/cf2nand/p1
50 mount -t yaffs2 "$main" /tmp/cf2nand/p2
51
52 echo "Copying kernel..."
53 copy_kernel /dev/cf/card0/part1 /tmp/cf2nand/p1/kernel "root=/dev/mtdblock1 rootfstype=yaffs2 " 2>/dev/null >/dev/null
54 umount /tmp/cf2nand/p1
55 rmdir /tmp/cf2nand/p1
56
57 echo "Copying filesystem..."
58 ( cd /tmp/cf2nand/rootfs; tar c . ) | ( cd /tmp/cf2nand/p2; tar x )
59 sync
60 umount /tmp/cf2nand/p2
61 rmdir /tmp/cf2nand/p2
62
63 umount /tmp/cf2nand/rootfs
64 rmdir /tmp/cf2nand/rootfs
65 rmdir /tmp/cf2nand
66