Blame view

scripts/sh/smarc-mksdcard.sh 6.17 KB
60ea7fa3d   Eric Lee   Embedian Android ...
1
  #!/bin/bash
60ea7fa3d   Eric Lee   Embedian Android ...
2

85103e50a   Eric Lee   re-write mksdcard...
3
4
  # android-tools-fsutils should be installed as
  # "sudo apt-get install android-tools-fsutils"
60ea7fa3d   Eric Lee   Embedian Android ...
5
6
  
  help() {
85103e50a   Eric Lee   re-write mksdcard...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  bn=`basename $0`
  cat << EOF
  usage $bn <option> device_node
  
  options:
    -h				displays this help message
    -s				only get partition size
    -np 				not partition.
    -f soc_name			flash android image file with soc_name
    -F soc_name			determine the device_node's offset to flash bootloader and flash default android image file
  					 soc     				offset(KB)
  					default					   1
  					imx8dv					  16
  					imx8qm/imx8qxp/imx8mq			  33
    -a				only flash image to slot_a
    -b				only flash image to slot_b
    -c card_size			optional setting: 7 / 14 / 28
  					If not set, use partition-table.img
  					If set to 7, use partition-table-7GB.img for 7GB SD card
  EOF
60ea7fa3d   Eric Lee   Embedian Android ...
27
  }
85103e50a   Eric Lee   re-write mksdcard...
28
  # parse command line
60ea7fa3d   Eric Lee   Embedian Android ...
29
30
  moreoptions=1
  node="na"
85103e50a   Eric Lee   re-write mksdcard...
31
  soc_name=""
60ea7fa3d   Eric Lee   Embedian Android ...
32
  cal_only=0
85103e50a   Eric Lee   re-write mksdcard...
33
34
35
36
37
38
39
40
41
42
43
44
45
  card_size=0
  bootloader_offset=1
  vaild_gpt_size=17
  not_partition=0
  not_format_fs=0
  slot=""
  systemimage_file="system.img"
  systemimage_raw_file="system_raw.img"
  vendor_file="vendor.img"
  vendor_raw_file="vendor_raw.img"
  partition_file="partition-table.img"
  g_sizes=0
  append_soc_name=0
60ea7fa3d   Eric Lee   Embedian Android ...
46
47
  while [ "$moreoptions" = 1 -a $# -gt 0 ]; do
  	case $1 in
85103e50a   Eric Lee   re-write mksdcard...
48
49
50
51
52
53
54
55
56
57
  	    -h) help; exit ;;
  	    -s) cal_only=1 ;;
  	    -f) append_soc_name=1 ; soc_name=$2; shift;;
  	    -F) soc_name=$2; shift;;
  	    -c) card_size=$2; shift;;
  	    -np) not_partition=1 ;;
  	    -nf) not_format_fs=1 ;;
  	    -a) slot="_a" ;;
  	    -b) slot="_b" ;;
  	    *)  moreoptions=0; node=$1 ;;
60ea7fa3d   Eric Lee   Embedian Android ...
58
  	esac
85103e50a   Eric Lee   re-write mksdcard...
59
  	[ "$moreoptions" = 0 ] && [ $# -gt 1 ] && help && exit
60ea7fa3d   Eric Lee   Embedian Android ...
60
61
  	[ "$moreoptions" = 1 ] && shift
  done
85103e50a   Eric Lee   re-write mksdcard...
62
63
  if [ ${card_size} -ne 0 ] && [ ${card_size} -ne 7 ] && [ ${card_size} -ne 14 ] && [ ${card_size} -ne 28 ]; then
      help; exit;
60ea7fa3d   Eric Lee   Embedian Android ...
64
  fi
85103e50a   Eric Lee   re-write mksdcard...
65
66
  if [ "${soc_name}" = "imx8dv" ]; then
      bootloader_offset=16
60ea7fa3d   Eric Lee   Embedian Android ...
67
  fi
60ea7fa3d   Eric Lee   Embedian Android ...
68

85103e50a   Eric Lee   re-write mksdcard...
69
70
  if [ "${soc_name}" = "imx8qm" -o "${soc_name}" = "imx8qxp" -o "${soc_name}" = "imx8mq" -o "${soc_name}" = "fsl-smarcimx8mq" -o "${soc_name}" = "fsl-smarcimx8mq-hdmi" -o "${soc_name}" = "fsl-smarcimx8mq-hdmi-4k" -o "${soc_name}" = "fsl-smarcimx8mq-lcdif-lvds" -o "${soc_name}" = "fsl-smarcimx8mq-dcss-lvds" -o "${soc_name}" = "fsl-smarcimx8mq-dual-display" ]; then
      bootloader_offset=33
60ea7fa3d   Eric Lee   Embedian Android ...
71
72
73
  fi
  
  echo "${soc_name} bootloader offset is: ${bootloader_offset}"
60ea7fa3d   Eric Lee   Embedian Android ...
74

85103e50a   Eric Lee   re-write mksdcard...
75
76
77
78
79
  if [ "${soc_name}" != "" ] && [ "${append_soc_name}" -eq 1 ]; then
      soc_name="-${soc_name}"
  else
      soc_name=""
  fi
60ea7fa3d   Eric Lee   Embedian Android ...
80

85103e50a   Eric Lee   re-write mksdcard...
81
82
83
84
  if [ ! -e ${node} ]; then
  	help
  	exit
  fi
60ea7fa3d   Eric Lee   Embedian Android ...
85

60ea7fa3d   Eric Lee   Embedian Android ...
86

85103e50a   Eric Lee   re-write mksdcard...
87
88
89
90
  # dump partitions
  if [ "${cal_only}" -eq "1" ]; then
  gdisk -l ${node} 2>/dev/null | grep -A 20 "Number  "
  exit
60ea7fa3d   Eric Lee   Embedian Android ...
91
  fi
85103e50a   Eric Lee   re-write mksdcard...
92
  function get_partition_size
60ea7fa3d   Eric Lee   Embedian Android ...
93
  {
85103e50a   Eric Lee   re-write mksdcard...
94
95
96
97
      start_sector=`gdisk -l ${node} | grep -w $1 | awk '{print $2}'`
      end_sector=`gdisk -l ${node} | grep -w $1 | awk '{print $3}'`
      # 1 sector = 512 bytes. This will change unit from sector to MBytes.
      let "g_sizes=($end_sector - $start_sector + 1) / 2048"
60ea7fa3d   Eric Lee   Embedian Android ...
98
  }
85103e50a   Eric Lee   re-write mksdcard...
99
  function format_partition
60ea7fa3d   Eric Lee   Embedian Android ...
100
  {
85103e50a   Eric Lee   re-write mksdcard...
101
102
103
104
105
      num=`gdisk -l ${node} | grep -w $1 | awk '{print $1}'`
      if [ ${num} -gt 0 ] 2>/dev/null; then
          echo "format_partition: $1:${node}${num} ext4"
          mkfs.ext4 -F ${node}${num} -L$1
      fi
60ea7fa3d   Eric Lee   Embedian Android ...
106
  }
85103e50a   Eric Lee   re-write mksdcard...
107
  function erase_partition
60ea7fa3d   Eric Lee   Embedian Android ...
108
  {
85103e50a   Eric Lee   re-write mksdcard...
109
110
111
112
113
114
      num=`gdisk -l ${node} | grep -w $1 | awk '{print $1}'`
      if [ ${num} -gt 0 ] 2>/dev/null; then
          get_partition_size $1
          echo "erase_partition: $1 : ${node}${num} ${g_sizes}M"
          dd if=/dev/zero of=${node}${num} bs=1048576 conv=fsync count=$g_sizes
      fi
60ea7fa3d   Eric Lee   Embedian Android ...
115
  }
85103e50a   Eric Lee   re-write mksdcard...
116
  function flash_partition
60ea7fa3d   Eric Lee   Embedian Android ...
117
  {
85103e50a   Eric Lee   re-write mksdcard...
118
119
120
121
122
123
124
125
126
127
128
129
130
131
      for num in `gdisk -l ${node} | grep -E -w "$1|$1_a|$1_b" | awk '{print $1}'`
      do
          if [ $? -eq 0 ]; then
              if [ $(echo ${1} | grep "system") != "" ] 2>/dev/null; then
                  img_name=${systemimage_raw_file}
              elif [ $(echo ${1} | grep "vendor") != "" ] 2>/dev/null; then
                  img_name=${vendor_raw_file}
              else
                  img_name="${1%_*}${soc_name}.img"
              fi
              echo "flash_partition: ${img_name} ---> ${node}${num}"
              dd if=${img_name} of=${node}${num} bs=10M conv=fsync
          fi
      done
60ea7fa3d   Eric Lee   Embedian Android ...
132
133
134
135
  }
  
  function format_android
  {
85103e50a   Eric Lee   re-write mksdcard...
136
137
138
139
140
141
      echo "formating android images"
      format_partition userdata
      format_partition cache
      erase_partition presistdata
      erase_partition fbmisc
      erase_partition misc
60ea7fa3d   Eric Lee   Embedian Android ...
142
  }
85103e50a   Eric Lee   re-write mksdcard...
143
  function make_partition
60ea7fa3d   Eric Lee   Embedian Android ...
144
  {
85103e50a   Eric Lee   re-write mksdcard...
145
146
147
148
149
      if [ ${card_size} -gt 0 ]; then
          partition_file="partition-table-${card_size}GB.img"
      fi
      echo "make gpt partition for android: ${partition_file}"
      dd if=${partition_file} of=${node} bs=1k count=${vaild_gpt_size} conv=fsync
60ea7fa3d   Eric Lee   Embedian Android ...
150
  }
85103e50a   Eric Lee   re-write mksdcard...
151
  function flash_android
60ea7fa3d   Eric Lee   Embedian Android ...
152
  {
85103e50a   Eric Lee   re-write mksdcard...
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
      boot_partition="boot"${slot}
      recovery_partition="recovery"${slot}
      system_partition="system"${slot}
      vendor_partition="vendor"${slot}
      vbmeta_partition="vbmeta"${slot}
  
      #bootloader_file="u-boot${soc_name}.imx"
      bootloader_file="u-boot-smarcimx8mq_2g.imx"
      flash_partition ${boot_partition}
      flash_partition ${recovery_partition}
      simg2img ${systemimage_file} ${systemimage_raw_file}
      flash_partition ${system_partition}
      rm ${systemimage_raw_file}
      simg2img ${vendor_file} ${vendor_raw_file}
      flash_partition ${vendor_partition}
      rm ${vendor_raw_file}
      flash_partition ${vbmeta_partition}
      echo "erase_partition: uboot : ${node}"
      echo "flash_partition: ${bootloader_file} ---> ${node}"
      first_partition_offset=`gdisk -l ${node} | grep ' 1 ' | awk '{print $2}'`
      # the unit of first_partition_offset is sector size which is 512 Byte.
      count_bootloader=`expr ${first_partition_offset} / 2 - ${bootloader_offset}`
      echo "the bootloader partition size: ${count_bootloader}"
      dd if=/dev/zero of=${node} bs=1k seek=${bootloader_offset} conv=fsync count=${count_bootloader}
      dd if=${bootloader_file} of=${node} bs=1k seek=${bootloader_offset} conv=fsync
60ea7fa3d   Eric Lee   Embedian Android ...
178
  }
85103e50a   Eric Lee   re-write mksdcard...
179
180
181
182
  if [ "${not_partition}" -eq "1" ] ; then
      flash_android
      exit
  fi
60ea7fa3d   Eric Lee   Embedian Android ...
183

85103e50a   Eric Lee   re-write mksdcard...
184
185
186
187
  make_partition
  sleep 3
  for i in `cat /proc/mounts | grep "${node}" | awk '{print $2}'`; do umount $i; done
  hdparm -z ${node}
60ea7fa3d   Eric Lee   Embedian Android ...
188

85103e50a   Eric Lee   re-write mksdcard...
189
190
191
192
193
194
195
  # backup the GPT table to last LBA for sd card.
  echo -e 'r
  e
  Y
  w
  Y
  Y' |  gdisk ${node}
60ea7fa3d   Eric Lee   Embedian Android ...
196

60ea7fa3d   Eric Lee   Embedian Android ...
197
  format_android
85103e50a   Eric Lee   re-write mksdcard...
198
199
200
201
202
203
204
205
206
207
  flash_android
  
  # For MFGTool Notes:
  # MFGTool use mksdcard-android.tar store this script
  # if you want change it.
  # do following:
  #   tar xf mksdcard-android.sh.tar
  #   vi mksdcard-android.sh 
  #   [ edit want you want to change ]
  #   rm mksdcard-android.sh.tar; tar cf mksdcard-android.sh.tar mksdcard-android.sh