summarylogtreecommitdiffstats
path: root/bpir-flash2emmc
blob: ce9637155e352fd767aaacc2ea68bad503473193 (plain)
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
#!/usr/bin/ash
 
# Tool for %PKGBASE% package, needs to run from initrd!
# Connect UART and when kernel starts, keep 'x' key pressed.
 
function get_sysmmc {
  str=$(echo /sys/bus/platform/devices/${addr}.mmc/mmc_host/mmc*/mmc* | head -1)
  [ -e "$str" ] && echo ${str} || echo ""
}
function get_mmcblk {
  str=$(get_sysmmc)
  [ ! -z "$str" ] &&  echo "$(basename $(ls ${str}/block | head -1))" || echo "/dev/null"
}
function get_mmctype {
  str=$(get_sysmmc)
  [ ! -z "$str" ] && cat ${str}/type || echo "NONE"
}

function set_sdmmc {
  rmdir ${mmcdtbo}
} 

function set_emmc {
cat <<EOT | tee /tmp/mmc0.dts
/dts-v1/;
/plugin/;
&mmc0 {
  bus-width = <8>;
  max-frequency = <200000000>;
  cap-mmc-highspeed;
  mmc-hs200-1_8v;
  mmc-hs400-1_8v;
  hs400-ds-delay = <0x14014>;
  no-sd;
  no-sdio;
  status = "okay";
};
EOT
mkdir -p ${mmcdtbo}
dtc -@ -I dts -O dtb -o ${mmcdtbo}/dtbo /tmp/mmc0.dts
}
 
function restart_driver {
  [[ "$1" == "SD" ]] && pos="UP"
  [[ "$1" == "MMC" ]] && pos="DOWN"
  until [[ "$(get_mmctype)" == "$1" ]]; do
    echo ${addr}.mmc >/sys/bus/platform/drivers/${driver}/unbind
    sleep 0.1
    echo ${addr}.mmc >/sys/bus/platform/drivers/${driver}/bind
    echo "Flip SD/EMMC switch ${pos} (=$1) (most near to power plug), the rest stay up!"
    sleep 1.9
  done
}

driver="mtk-msdc"
addr="11230000"
compatible="$(cat /proc/device-tree/compatible)"

if [[ "$compatible" == *"bananapi"*"mediatek,mt7622"* ]]; then # BPI-R64
  mmcblk=$(get_mmcblk)
  echo "EMMC Device = /dev/${mmcblk}"
  mkdir -p /tmp/mnt
  mount --source /dev/disk/by-partlabel/*-sdmmc-root --target /tmp/mnt
  [ $? -ne 0 ] && exit
  echo "Flashing file $1 now..."
  dd if="/tmp/mnt$1" of=/dev/${mmcblk} bs=1M conv=fsync,notrunc
  umount /tmp/mnt
elif [[ "$compatible" == *"bananapi"*"mediatek,mt7986"* ]]; then # BPI-R3
  addr="11230000"
  mount -t configfs none /sys/kernel/config
  mmcblk=$(get_mmcblk)
  mmcdtbo="/sys/kernel/config/device-tree/overlays/mmc0"
  echo "EMMC Device = /dev/${mmcblk}"
  if [[ "$(get_mmctype)" == "SD" ]]; then
    mkdir -p /tmp/mnt
    mount --source /dev/disk/by-partlabel/*-sdmmc-root --target /tmp/mnt
    [ $? -ne 0 ] && exit
    echo "Reading file $1 now..."
    cp -f "/tmp/mnt$1" "/tmp/tempfile"
    umount /tmp/mnt
    [ $? -ne 0 ] && exit
    echo "Switching to EMMC..."
    set_emmc
    restart_driver "MMC"
    mmcblk=$(get_mmcblk)
    if [ -e "/tmp/tempfile" ]; then
      echo "Setting up EMMC so that ${mmcblk} is the bootdevice."
      mmc bootpart enable 7 1 /dev/${mmcblk}
      echo "Writing $1 to EMMC now..."
      xz -dcv "/tmp/tempfile" | dd of=/dev/${mmcblk} conv=fsync,notrunc
      echo "Reboot and enjoy booting from EMMC."
    fi
  elif [[ "$(get_mmctype)" == "MMC" ]]; then
    echo "Switching back to SDMMC..."
    set_sdmmc
    restart_driver "SD"
    mmcblk=$(get_mmcblk)
  fi
fi

#CONFIG_DEBUG_FS=y
#CONFIG_MMC_DEBUG=y
#mount -t debugfs none /sys/kernel/debug
#echo "file drivers/mmc/* +p" > /sys/kernel/debug/dynamic_debug/control

#echo "Testing on mmcblk0boot1..."
#echo 0 > /sys/block/mmcblk0boot0/force_ro 
#echo 0 > /sys/block/mmcblk0boot1/force_ro 
#dd if=/dev/zero of=/dev/mmcblk0boot1