blob: a01d2834e1809fb013b01ef0a03130ec663be80f [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001#!/bin/bash
2
3if [ -z "$1" ]
4then
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
10fi
11
12if [ ! -f "$1" ]
13then
14 echo $1': File not exist!'
15 exit 1
16fi
17
18# Show the partition layout in better interactive mode
19tmpfile=".part.tmp"
20xmlfile=$1
21
22# LBS(size defined in tmp file is KB based)
23lbs_exist=`grep -o "\<lbs\>" ${xmlfile}`
24if [ -z $lbs_exist ]
25then
26 # For eMMC/Nor, only support 1024-aligned by now
27 lbs=1
28else
29 lbs=`awk -F'"' '/lbs=/{print $4}' ${xmlfile}`
30 lbs=$(($lbs/1024))
31fi
32# LBA(Total page number)
33lba_exist=`grep -o "\<lba\>" ${xmlfile}`
34if [ -z $lba_exist ]
35then
36 lba=0
37else
38 lba=`awk -F'"' '/lba=/{print $2}' ${xmlfile}`
39fi
40# reserved some blocks to store BBT
41reserved_exist=`grep -o "\<reserved\>" ${xmlfile}`
42if [ -z $reserved_exist ]
43then
44 reserved=0
45else
46 reserved=`awk -F'"' '/reserved=/{print $6}' ${xmlfile}`
47fi
48
49# Generate temporary file for interactive modifing and editing
50echo '*** You can delete/insert/modify(partname/size) partitions ***' > ${tmpfile}
51echo '*** Partition size must be ERASE_BLOCK_SIZE(define in Nand SPEC) aligned ***' >> ${tmpfile}
52echo '' >> ${tmpfile}
53echo '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
58vim ${tmpfile}
59
60# The new partition layout
61part_name=(`awk '/^\w+\s+[0-9]+/{print $1}' ${tmpfile}`)
62part_size=(`awk '/^\w+\s+[0-9]+/{print $2}' ${tmpfile}`)
63part_attr=(`awk '/^\w+\s+[0-9]+/{if ($3 == "") print "-"; else print $3}' ${tmpfile}`)
64# How many elements in the array
65newcnt=$((${#part_name[@]}-1))
66# Generate the temporary xml file
67xmltmp=".xml.tmp"
68`cp -f ${xmlfile} ${xmltmp}`
69`sed -i '/name=/d' ${xmltmp}`
70# Modify the temporary xml file
71str1=' <entry type="{0FC63DAF-8483-4772-8E79-3D69D8477DE4}" start="'
72str2='" end="'
73strattr='" attributes="'
74str3='" name="'
75str4='" />'
76# first start address
77sa=`awk -F'"' '/name=/{print $4; exit;}' ${xmlfile}`
78# last end address
79lea=`sed -n '/name=/p' ${xmlfile} | sed -n '$p' | awk -F'"' '{print $6}'`
80for ((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
106exit 0