cf2nand 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/bin/sh
  2. . /lib/functions.sh
  3. copy_kernel() {
  4. local input="$1"
  5. local output="$2"
  6. local cmdline="$3"
  7. size="$(echo -n "$cmdline" | wc -c)"
  8. dd if="$input" bs=3M count=1 > "$output"
  9. /sbin/patch-cmdline "$output" "$cmdline"
  10. }
  11. fstype="$(mount | grep ' / ' | awk '$5 != "rootfs" {print $5}')"
  12. case "$fstype" in
  13. ext2|jffs2) echo "Copying from $fstype to yaffs2";;
  14. *) echo "Invalid filesystem."; exit 1;;
  15. esac
  16. [ -d /tmp/cf2nand ] && {
  17. echo "/tmp/cf2nand already exists"
  18. exit 1
  19. }
  20. mkdir /tmp/cf2nand
  21. mkdir /tmp/cf2nand/rootfs
  22. mount -t "$fstype" /dev/root /tmp/cf2nand/rootfs || {
  23. echo "Mounting rootfs failed."
  24. exit 1
  25. }
  26. boot="$(find_mtd_part 'Routerboard NAND boot')"
  27. main="$(find_mtd_part 'rootfs')"
  28. [ -z "$boot" -o -z "$main" ] && {
  29. echo "Cannot find NAND Flash partitions"
  30. exit 1
  31. }
  32. echo "Erasing filesystem..."
  33. mtd erase Boot 2>/dev/null >/dev/null
  34. mtd erase Main 2>/dev/null >/dev/null
  35. mkdir /tmp/cf2nand/p1
  36. mkdir /tmp/cf2nand/p2
  37. mount -t yaffs2 "$boot" /tmp/cf2nand/p1
  38. mount -t yaffs2 "$main" /tmp/cf2nand/p2
  39. echo "Copying kernel..."
  40. copy_kernel /dev/cfa1 /tmp/cf2nand/p1/kernel "root=/dev/mtdblock1 rootfstype=yaffs2 " 2>/dev/null >/dev/null
  41. umount /tmp/cf2nand/p1
  42. rmdir /tmp/cf2nand/p1
  43. echo "Copying filesystem..."
  44. ( cd /tmp/cf2nand/rootfs; tar c . ) | ( cd /tmp/cf2nand/p2; tar x )
  45. echo "chmod ugo+x /" > /tmp/cf2nand/p2/etc/uci-defaults/set_root_permission
  46. sync
  47. # Use kexec is present
  48. [ -x /usr/bin/kexec ] && {
  49. kexec -l /tmp/cf2nand/p1/kernel --command-line="$(cat /proc/cmdline) root=/dev/mtdblock1 rootfstype=yaffs2"
  50. kexec -e
  51. }
  52. umount /tmp/cf2nand/p2
  53. rmdir /tmp/cf2nand/p2
  54. umount /tmp/cf2nand/rootfs
  55. rmdir /tmp/cf2nand/rootfs
  56. rmdir /tmp/cf2nand