1#
2# File      : upgrade.py
3# This file is part of RT-Thread RTOS
4# COPYRIGHT (C) 2006 - 2021, RT-Thread Development Team
5#
6#  This program is free software; you can redistribute it and/or modify
7#  it under the terms of the GNU General Public License as published by
8#  the Free Software Foundation; either version 2 of the License, or
9#  (at your option) any later version.
10#
11#  This program is distributed in the hope that it will be useful,
12#  but WITHOUT ANY WARRANTY; without even the implied warranty of
13#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14#  GNU General Public License for more details.
15#
16#  You should have received a copy of the GNU General Public License along
17#  with this program; if not, write to the Free Software Foundation, Inc.,
18#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19#
20# Change Logs:
21# Date           Author       Notes
22# 2021-10-11     Meco Man     First version
23#
24
25# 本文件用于在HAL库更新之后
26# 1.对gcc的汇编启动文件中main替换为entry函数
27# 2.将启动文件heap降为0(Keil IAR)
28# 3.将GCC的堆大小扩展到0x400,与Keil IAR保持一致
29
30
31#使用方法:运行脚本,将bsp/ft32的绝对路径传给脚本即可,如:C:\Users\92036\Desktop\rt-thread\bsp\ft32
32
33import os
34import re
35
36#将'bl main' 替换为 'bl entry'
37def main2entry(path):
38    oldline = ''
39    newline = ''
40
41    for root, dirs, files in os.walk(path): #递归扫描里面的所有文件
42        for file in files:
43            if os.path.splitext(file)[1] == '.s': #找.s文件
44                file_path = os.path.join(root,file)
45                flag_need_replace = False
46                with open(file_path,'r+',) as f:
47                    while True:
48                        line = f.readline()
49                        if line == '':
50                            break
51                        elif ('bl' in line) and ('main' in line): #发现'bl main'
52                            oldline = line # bl main
53                            newline = line.replace('main', 'entry') #将main替换为entry,形成新的字符串
54                            flag_need_replace = True #标记该文件需要做entry替换
55                            break
56
57                    if (flag_need_replace == True): #若该文件需要将main替换为entry
58                        f.seek(0)
59                        content = f.read()
60                        f.seek(0)
61                        f.truncate()
62                        newcontent = content.replace(oldline, newline)
63                        f.write(newcontent)
64
65#将启动文件的heap降为0
66def heap2zero(path):
67    oldline = ''
68    newline = ''
69    for root, dirs, files in os.walk(path): #递归扫描里面的所有文件
70        for file in files:
71            file_path = os.path.join(root,file)
72            if os.path.splitext(file)[1] == '.s': #找.s文件
73                with open(file_path,'r+',) as f:
74                    flag_need_replace = False
75                    while True:
76                        line = f.readline()
77                        if line == '':
78                            break
79
80                        re_result = re.match('\s*Heap_Size\s+EQU\s+0[xX][0-9a-fA-F]+', line) #MDK的表示方法
81                        if re_result != None:
82                            oldline = line
83                            newline = re.sub('0[xX][0-9a-fA-F]+','0x00000000', oldline)
84                            flag_need_replace = True
85                            break
86
87                    if flag_need_replace == True:
88                        f.seek(0)
89                        content = f.read()
90                        f.seek(0)
91                        f.truncate()
92                        newcontent = content.replace(oldline, newline)
93                        f.write(newcontent)
94
95            elif os.path.splitext(file)[1] == '.icf': #找.icf文件(IAR)
96                with open(file_path,'r+',) as f:
97                    flag_need_replace = False
98                    while True:
99                        line = f.readline()
100                        if line == '':
101                            break
102
103                        re_result = re.match('\s*define\s+symbol\s+__ICFEDIT_size_heap__\s*=\s*0[xX][0-9a-fA-F]+', line) #IAR的表示方法
104                        if re_result != None:
105                            oldline = line
106                            newline = re.sub('0[xX][0-9a-fA-F]+','0x000', oldline)
107                            flag_need_replace = True
108                            break
109
110                    if flag_need_replace == True:
111                        f.seek(0)
112                        content = f.read()
113                        f.seek(0)
114                        f.truncate()
115                        newcontent = content.replace(oldline, newline)
116                        f.write(newcontent)
117
118            elif os.path.splitext(file)[1] == '.lds': #找.lds文件(GCC)
119                with open(file_path,'r+',) as f:
120                    flag_need_replace = False
121                    while True:
122                        line = f.readline()
123                        if line == '':
124                            break
125
126                        re_result = re.match('\s*_system_stack_size\s*=\s*0[xX][0-9a-fA-F]+', line) #GCC的表示方法, 将默认的栈大小增加到0x400
127                        if re_result != None:
128                            oldline = line
129                            newline = re.sub('0[xX][0-9a-fA-F]+','0x400', oldline)
130                            flag_need_replace = True
131                            break
132
133                    if flag_need_replace == True:
134                        f.seek(0)
135                        content = f.read()
136                        f.seek(0)
137                        f.truncate()
138                        newcontent = content.replace(oldline, newline)
139                        f.write(newcontent)
140
141folder_path = input('please input path:')
142main2entry(folder_path)
143heap2zero(folder_path)
144