rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | |
| 3 | if [ -z "$1" ] |
| 4 | then |
| 5 | # Show the partition layout if no argument input |
| 6 | echo 'Usage:' |
| 7 | echo ' Show current Partitions layout:' |
| 8 | echo ' sh partition_editor.sh ${XMLFILE}' |
| 9 | exit 1 |
| 10 | fi |
| 11 | |
| 12 | if [ ! -f "$1" ] |
| 13 | then |
| 14 | echo $1': File not exist!' |
| 15 | exit 1 |
| 16 | fi |
| 17 | |
| 18 | # Show the partition layout in better interactive mode |
| 19 | tmpfile=".part.tmp" |
| 20 | xmlfile=$1 |
| 21 | |
| 22 | # LBS(size defined in tmp file is KB based) |
| 23 | lbs_exist=`grep -o "\<lbs\>" ${xmlfile}` |
| 24 | if [ -z $lbs_exist ] |
| 25 | then |
| 26 | # For eMMC/Nor, only support 1024-aligned by now |
| 27 | lbs=1 |
| 28 | else |
| 29 | lbs=`awk -F'"' '/lbs=/{print $4}' ${xmlfile}` |
| 30 | lbs=$(($lbs/1024)) |
| 31 | fi |
| 32 | # LBA(Total page number) |
| 33 | lba_exist=`grep -o "\<lba\>" ${xmlfile}` |
| 34 | if [ -z $lba_exist ] |
| 35 | then |
| 36 | lba=0 |
| 37 | else |
| 38 | lba=`awk -F'"' '/lba=/{print $2}' ${xmlfile}` |
| 39 | fi |
| 40 | # reserved some blocks to store BBT |
| 41 | reserved_exist=`grep -o "\<reserved\>" ${xmlfile}` |
| 42 | if [ -z $reserved_exist ] |
| 43 | then |
| 44 | reserved=0 |
| 45 | else |
| 46 | reserved=`awk -F'"' '/reserved=/{print $6}' ${xmlfile}` |
| 47 | fi |
| 48 | |
| 49 | # Generate temporary file for interactive modifing and editing |
| 50 | echo '*** You can delete/insert/modify(partname/size) partitions ***' > ${tmpfile} |
| 51 | echo '*** Partition size must be ERASE_BLOCK_SIZE(define in Nand SPEC) aligned ***' >> ${tmpfile} |
| 52 | echo '' >> ${tmpfile} |
| 53 | echo 'Part_name Size(KB) Attr' >> ${tmpfile} |
| 54 | `sed -nr 's/^.+start=\"(\w+)\"\s*end=\"(\w+)\"\s*(attributes=\"(\w+)\"\s*)?name=\"(\w+)\".+$/\5 \1 \2 \4/p' ${xmlfile} | |
| 55 | awk -v blk_size=$lbs '{printf "%-15s\t\t%d\t\t%s\n", $1, ($3-$2+1)*blk_size, $4}' >> ${tmpfile}` |
| 56 | |
| 57 | # Edit the partition layout via vim |
| 58 | vim ${tmpfile} |
| 59 | |
| 60 | # The new partition layout |
| 61 | part_name=(`awk '/^\w+\s+[0-9]+/{print $1}' ${tmpfile}`) |
| 62 | part_size=(`awk '/^\w+\s+[0-9]+/{print $2}' ${tmpfile}`) |
| 63 | part_attr=(`awk '/^\w+\s+[0-9]+/{if ($3 == "") print "-"; else print $3}' ${tmpfile}`) |
| 64 | # How many elements in the array |
| 65 | newcnt=$((${#part_name[@]}-1)) |
| 66 | # Generate the temporary xml file |
| 67 | xmltmp=".xml.tmp" |
| 68 | `cp -f ${xmlfile} ${xmltmp}` |
| 69 | `sed -i '/name=/d' ${xmltmp}` |
| 70 | # Modify the temporary xml file |
| 71 | str1=' <entry type="{0FC63DAF-8483-4772-8E79-3D69D8477DE4}" start="' |
| 72 | str2='" end="' |
| 73 | strattr='" attributes="' |
| 74 | str3='" name="' |
| 75 | str4='" />' |
| 76 | # first start address |
| 77 | sa=`awk -F'"' '/name=/{print $4; exit;}' ${xmlfile}` |
| 78 | # last end address |
| 79 | lea=`sed -n '/name=/p' ${xmlfile} | sed -n '$p' | awk -F'"' '{print $6}'` |
| 80 | for ((i=0; i<=$newcnt; i++)) { |
| 81 | len=${part_size[i]} |
| 82 | if [ $i -eq $newcnt ] |
| 83 | then |
| 84 | ea=$lea |
| 85 | if [ $lba -ne 0 -a $reserved -ne 0 ] |
| 86 | then |
| 87 | ea=$(($lba-$reserved-1)) |
| 88 | fi |
| 89 | else |
| 90 | ea=$(($sa+$len/$lbs-1)) |
| 91 | fi |
| 92 | name=${part_name[i]} |
| 93 | attr=${part_attr[i]} |
| 94 | if [ "$attr" == "-" ] |
| 95 | then |
| 96 | `sed -i '/<\/partition>/i \'"$str1$sa$str2$ea$str3$name$str4"'' ${xmltmp}` |
| 97 | else |
| 98 | `sed -i '/<\/partition>/i \'"$str1$sa$str2$ea$strattr$attr$str3$name$str4"'' ${xmltmp}` |
| 99 | fi |
| 100 | sa=$(($ea+1)) |
| 101 | } |
| 102 | # Replace the xml file with the temp xml file, delete useless temporary files |
| 103 | `cp -f ${xmltmp} ${xmlfile}` |
| 104 | `rm -f ${xmltmp} ${tmpfile}` |
| 105 | |
| 106 | exit 0 |