SHARE
TWEET

lg mtd_init.sh

a guest Sep 20th, 2018 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /bin/sh
  2.  
  3. #
  4. # functions
  5. #
  6. # $1 : partition name
  7. # $2 : path
  8. # return 0 : OK
  9. #        1 : can't find partition name
  10. #        2 : mount yaffs2 or ubifs error
  11. #        3 : mkdir fail
  12. funcMntAndLink () {
  13.     # find partition by name
  14.     PART_NAME=\"$1\"
  15.     PART_NAME_NEW=$1
  16.     MTD_ID=$(awk -v v1=${PART_NAME} '$4==v1 {print substr($1,4,length($1)-4)}' /proc/mtd)
  17.  
  18.     MTD_SIZE=$(awk -F"," -v v1=${PART_NAME_NEW} 'substr($7,3,length($7)-3)==v1 {print substr($3,2,10)}' /proc/part_info_tbl)
  19.     MTD_TYPE=$(awk -F"," -v v1=${PART_NAME_NEW} 'substr($7,3,length($7)-3)==v1 {print substr($5,2,7)}' /proc/part_info_tbl)
  20.     VOL_NAME=$(awk -F"," -v v1=${PART_NAME_NEW} 'substr($7,3,length($7)-3)==v1 {print substr($6,3,4)}' /proc/part_info_tbl)
  21.  
  22.  
  23.     echo "[MTD_SIZE] is...[${MTD_SIZE}]"
  24.     echo "[MTD_TYPE] is...[${MTD_TYPE}]"
  25.     echo "[VOL_NAME] is...[${VOL_NAME}]"
  26.    
  27.     NAND_MNT_DIR=$2
  28.  
  29.     if [ "${MTD_ID}" = "" ]; then
  30.         echo "[mtd_init] Can't find partition $1"
  31.         #return 1
  32.     fi
  33.  
  34.     # mount yaffs2 & ubifs
  35.     if [ "${NAND_MNT_DIR}" = "" ]; then
  36.         echo "[mtd_init] NAND_MNT_DIR is an empty string."
  37.     else
  38.         if [ ! -e ${NAND_MNT_DIR} ]; then
  39.             echo "[mtd_init] folder ${NAND_MNT_DIR} does not exist"
  40.             mkdir -p ${NAND_MNT_DIR}
  41.            
  42.             if [ $? -ne 0 ]; then
  43.                 echo "[mtd_init] mkdir fail... [${NAND_MNT_DIR}]"
  44.                 return 3
  45.             fi
  46.         fi
  47.        
  48.         echo "Mounting $1"
  49.         if [ "${MTD_TYPE}" = "mtd_ubi" ]; then          
  50.            echo "ubiattach mtd${MTD_ID}"  
  51.            /sbin/ubiattach /dev/ubi_ctrl -m $MTD_ID
  52.            if [ $? -ne 0 ]; then
  53.                echo "ubiattach mtd${MTD_ID} fail"  
  54.            else
  55.                echo "ubiattach mtd${MTD_ID} success"
  56.                return 0;
  57.            fi
  58.         fi          
  59.         if [ "${MTD_TYPE}" = "ubi_vol" ]; then                
  60.            echo "Try mount ubifs ${PART_NAME_NEW}"
  61.            mount -t ubifs ${VOL_NAME}:${PART_NAME_NEW} ${NAND_MNT_DIR}
  62.            if [ $? -ne 0 ]; then                    
  63.                echo "ubimkvol ${PART_NAME_NEW}"
  64.                /sbin/ubimkvol /dev/${VOL_NAME} -N $PART_NAME_NEW -s $MTD_SIZE
  65.                echo "Mount ubifs ${PART_NAME_NEW}"    
  66.                mount -t ubifs ${VOL_NAME}:${PART_NAME_NEW} ${NAND_MNT_DIR} -o sync
  67.                if [ $? -ne 0 ]; then
  68.                    echo "[mtd_init] Mount ubifs Fail...[${NAND_MNT_DIR}]"
  69.                    rm -rf ${NAND_MNT_DIR}
  70.                    return 2
  71.                else
  72.                    return 0
  73.                fi
  74.            else
  75.                echo "Try mount ubifs ${PART_NAME_NEW} success!"
  76.                return 0
  77.            fi                      
  78.         fi                    
  79.            
  80.            
  81.         echo "Mount yaffs2 fs"
  82.         mount -t yaffs2 /dev/mtdblock$MTD_ID ${NAND_MNT_DIR}
  83.         if [ $? -ne 0 ]; then
  84.             echo "[mtd_init] Mount yaffs2 Fail...[${NAND_MNT_DIR}]"
  85.             rm -rf ${NAND_MNT_DIR}
  86.             return 2            
  87.         fi          
  88.     fi
  89.     return 0;
  90. }
  91.  
  92. # ubi attach
  93. # $1 : partition name
  94. # return 0 : OK
  95. #        1 : can't find partition name
  96. #        2 : format type error
  97. #        3 : attach fail
  98. funcUbiAttach () {
  99.     PART_NAME=\"$1\"
  100.     PART_NAME_NEW=$1
  101.    
  102.     MTD_ID=$(awk -v v1=${PART_NAME} '$4==v1 {print substr($1,4,length($1)-4)}' /proc/mtd)
  103.     MTD_TYPE=$(awk -F"," -v v1=${PART_NAME_NEW} 'substr($7,3,length($7)-3)==v1 {print substr($5,2,7)}' /proc/part_info_tbl)
  104.  
  105.     if [ "${MTD_ID}" = "" ]; then
  106.         echo "no find mtd [${PART_NAME}]"
  107.         return 1
  108.     fi
  109.    
  110.     if [ "${MTD_TYPE}" != "mtd_ubi" ]; then
  111.         echo "format type[${MTD_TYPE}] not mtd_ubi"
  112.         return 2
  113.     fi
  114.    
  115.     echo "ubiattach mtd${MTD_ID}"  
  116.     /sbin/ubiattach /dev/ubi_ctrl -m $MTD_ID
  117.     if [ $? -ne 0 ]; then
  118.         echo "ubiattach mtd${MTD_ID} fail"  
  119.         return 3
  120.     fi
  121.    
  122.     echo "ubiattach mtd${MTD_ID} success"
  123.     return 0
  124. }
  125.  
  126.  
  127. # Mount ubi attach
  128. # $1 : partition name
  129. # $2 : path
  130. # return 0 : OK
  131. #        1 : can't find volume name
  132. #        2 : format type error
  133. #        3 : mount fail
  134. funcMntUbiVol () {
  135.     PART_NAME_NEW=$1
  136.     NAND_MNT_DIR=$2
  137.    
  138.     MTD_TYPE=$(awk -F"," -v v1=${PART_NAME_NEW} 'substr($7,3,length($7)-3)==v1 {print substr($5,2,7)}' /proc/part_info_tbl)
  139.     MTD_SIZE=$(awk -F"," -v v1=${PART_NAME_NEW} 'substr($7,3,length($7)-3)==v1 {print substr($3,2,10)}' /proc/part_info_tbl)
  140.     VOL_NAME=$(awk -F"," -v v1=${PART_NAME_NEW} 'substr($7,3,length($7)-3)==v1 {print substr($6,3,4)}' /proc/part_info_tbl)
  141.  
  142.     if [ "${MTD_TYPE}" = "" ]; then
  143.         echo "no find mtd [${PART_NAME_NEW}]"
  144.         return 1
  145.     fi
  146.    
  147.     if [ "${MTD_TYPE}" != "ubi_vol" ]; then
  148.         echo "format type[${MTD_TYPE}] not ubi_vol"
  149.         return 2
  150.     fi
  151.    
  152.     if [ ! -e ${NAND_MNT_DIR} ]; then
  153.         echo "no mnt dir -- mkdir -p ${NAND_MNT_DIR}"
  154.         mkdir -p ${NAND_MNT_DIR}
  155.     fi 
  156.    
  157.     echo "Try mount ubifs ${PART_NAME_NEW}"
  158.     mount -t ubifs ${VOL_NAME}:${PART_NAME_NEW} ${NAND_MNT_DIR} -o sync
  159.  
  160.     if [ $? -ne 0 ]; then                    
  161.         echo "ubimkvol ${PART_NAME_NEW}"
  162.         /sbin/ubimkvol /dev/${VOL_NAME} -N $PART_NAME_NEW -s $MTD_SIZE
  163.         if [ $? -ne 0 ]; then
  164.             echo "ubirmvol ubi_boot"
  165.             /sbin/ubirmvol /dev/ubi0 -n 0
  166.             echo "ubimkvol ${PART_NAME_NEW}"
  167.             /sbin/ubimkvol /dev/${VOL_NAME} -N $PART_NAME_NEW -s $MTD_SIZE
  168.         fi     
  169.         echo "Mount ubifs ${PART_NAME_NEW}"
  170.         mount -t ubifs ${VOL_NAME}:${PART_NAME_NEW} ${NAND_MNT_DIR} -o sync
  171.        
  172.         if [ $? -ne 0 ]; then
  173.             echo "[mtd_init] Mount ubifs Fail...[${NAND_MNT_DIR}]"
  174.             rm -rf ${NAND_MNT_DIR}
  175.             return 3
  176.         fi
  177.     fi
  178.    
  179.     echo "Try mount ubifs ${PART_NAME_NEW} success!"
  180.     return 0
  181. }
  182.  
  183. # Create dir and Link
  184. # $1 : target path
  185. # $2 : link dir
  186. # return 0 : OK
  187. #        1 : can't find volume name
  188. #        2 : format type error
  189. #        3 : mount fail
  190. funcLnDir () {
  191.     TARGET=$1
  192.     LINKDIR=$2
  193.    
  194.     if [ ! -e ${TARGET} ]; then
  195.         echo "no target -- mkdir -p ${TARGET}"
  196.         mkdir -p ${TARGET}
  197.     fi
  198.    
  199.     if [ -e ${LINKDIR} ]; then
  200.         echo "link dir exist -- rm ${LINKDIR}"
  201.         rm ${LINKDIR}
  202.     fi
  203.    
  204.     echo "link [${LINKDIR}] -> [${TARGET}]"
  205.     ln -s ${TARGET} ${LINKDIR}
  206.    
  207.     return $?
  208. }
  209.  
  210.  
  211. #
  212. # get major number
  213. #
  214. #MAJOR_C=$(awk '$2=="mtd" {print $1}' /proc/devices)
  215. #MAJOR_B=$(awk '$2=="mtdblock" {print $1}' /proc/devices)
  216.  
  217. #echo "MAJOR_C = [${MAJOR_C}], MAFOR_B=[${MAJOR_B}]"
  218.  
  219. #
  220. # mknod
  221. #
  222. #for MINOR in $(awk '$3~/^mtdblock/ {print $2}' /proc/diskstats); do
  223. #    echo "MINOR=[$MINOR]"
  224. #    mknod /dev/mtd${MINOR} c ${MAJOR_C} `expr ${MINOR} + ${MINOR}`
  225. #    mknod /dev/mtdblock${MINOR} b ${MAJOR_B} ${MINOR}
  226. #done
  227.  
  228.  
  229. #
  230. # mount squash
  231. #
  232.  
  233. #
  234. # mount ubi
  235. #
  236.  
  237. # Device node for ubi_ctrl
  238. MAJOR_NUM=$( awk -F":"   '{print substr($1,1,3)}' /sys/class/misc/ubi_ctrl/dev)
  239. MINOR_NUM=$( awk -F":"   '{print substr($2,1,3)}' /sys/class/misc/ubi_ctrl/dev)
  240.  
  241. echo "ubi_ctrl MAJOR_NUM is [${MAJOR_NUM}]"
  242. echo "ubi_ctrl MINOR_NUM is [${MINOR_NUM}]"
  243.  
  244. mknod /dev/ubi_ctrl c ${MAJOR_NUM} ${MINOR_NUM}
  245.  
  246. #attach ubi0 partition
  247. funcUbiAttach ubi0
  248.  
  249. # Device node for ubi0
  250. MAJOR_NUM=$( awk -F":"   '{print substr($1,1,3)}' /sys/class/ubi/ubi0/dev)
  251. MINOR_NUM=$( awk -F":"   '{print substr($2,1,3)}' /sys/class/ubi/ubi0/dev)
  252.  
  253. echo "ubi0 MAJOR_NUM is [${MAJOR_NUM}]"
  254. echo "ubi0 MINOR_NUM is [${MINOR_NUM}]"
  255.  
  256. mknod /dev/ubi0 c ${MAJOR_NUM} ${MINOR_NUM}
  257.  
  258. # mount ubi vol
  259. funcMntUbiVol ubi_boot /mnt/ubi_boot
  260.  
  261. #funcMntAndLink key_block_1 /mnt/BdpInfo1
  262. #funcLnDir /mnt/ubi_boot/key_block_1 /mnt/BdpInfo1
  263.  
  264. #funcMntAndLink key_block_2 /mnt/BdpInfo2
  265. #funcLnDir /mnt/ubi_boot/key_block_2 /mnt/BdpInfo2
  266.  
  267. #funcMntAndLink log /mnt/log
  268. funcLnDir /mnt/ubi_boot/log /mnt/log
  269.  
  270. #funcMntAndLink APDA /mnt/nand_03_0
  271. funcLnDir /mnt/ubi_boot/APDA /mnt/nand_03_0
  272.  
  273. # mount ubi vol
  274. #funcMntUbiVol BUDA /mnt/nand_06_0
  275.  
  276.  
  277. #funcMntAndLink acfg /acfg
  278. mkdir -p /mnt/ubi_boot/var/local/acfg
  279.  
  280. #funcMntAndLink misc_data /misc
  281. mkdir -p /mnt/ubi_boot/var/local/misc
  282.  
  283. #funcMntAndLink CPS_manager /cpsm
  284. mkdir -p /mnt/ubi_boot/var/local/cpsm
  285.  
  286. #funcMntAndLink cust_part_1 /cust_part_1
  287. mkdir -p /mnt/ubi_boot/var/local/cust_part_1
  288.  
  289. mkdir -p /mnt/ubi_boot/BUDA
  290.  
  291. funcLnDir /mnt/ubi_boot/BUDA /mnt/nand_06_0
  292.  
  293.  
  294. # BUDA
  295. #funcUbiAttach ubi1
  296. #if [ $? -ne 0 ]; then
  297. #   echo "no buda"
  298. #else
  299.     #funcMntAndLink BUDA /mnt/nand_06_0
  300.     #funcMntUbiVol BUDA /mnt/nand_06_0
  301. #fi
  302.  
  303. #
  304. # end
  305. #
  306. sync
  307. exit 0
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top