|
| 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 |
| 28 | + |
| 29 | + |
| 30 | +#使用方法:运行脚本,将bsp/stm32的绝对路径传给脚本即可,如:C:\Users\92036\Desktop\rt-thread\bsp\stm32 |
| 31 | + |
| 32 | +import os |
| 33 | +import re |
| 34 | + |
| 35 | +#将'bl main' 替换为 'bl entry' |
| 36 | +def main2entry(path): |
| 37 | + oldline = '' |
| 38 | + newline = '' |
| 39 | + |
| 40 | + for root, dirs, files in os.walk(path): #递归扫描里面的所有文件 |
| 41 | + for file in files: |
| 42 | + if os.path.splitext(file)[1] == '.s': #找.s文件 |
| 43 | + file_path = os.path.join(root,file) |
| 44 | + flag_need_replace = False |
| 45 | + with open(file_path,'r+',) as f: |
| 46 | + while True: |
| 47 | + line = f.readline() |
| 48 | + if line == '': |
| 49 | + break |
| 50 | + elif ('bl' in line) and ('main' in line): #发现'bl main' |
| 51 | + oldline = line # bl main |
| 52 | + newline = line.replace('main', 'entry') #将main替换为entry,形成新的字符串 |
| 53 | + flag_need_replace = True #标记该文件需要做entry替换 |
| 54 | + break |
| 55 | + |
| 56 | + if (flag_need_replace == True): #若该文件需要将main替换为entry |
| 57 | + f.seek(0) |
| 58 | + content = f.read() |
| 59 | + f.seek(0) |
| 60 | + f.truncate() |
| 61 | + newcontent = content.replace(oldline, newline) |
| 62 | + f.write(newcontent) |
| 63 | + |
| 64 | +#将启动文件的heap降为0 |
| 65 | +def heap2zero(path): |
| 66 | + oldline = '' |
| 67 | + newline = '' |
| 68 | + for root, dirs, files in os.walk(path): #递归扫描里面的所有文件 |
| 69 | + for file in files: |
| 70 | + file_path = os.path.join(root,file) |
| 71 | + if os.path.splitext(file)[1] == '.s': #找.s文件 |
| 72 | + with open(file_path,'r+',) as f: |
| 73 | + flag_need_replace = False |
| 74 | + while True: |
| 75 | + line = f.readline() |
| 76 | + if line == '': |
| 77 | + break |
| 78 | + |
| 79 | + re_result = re.match('\s*Heap_Size\s+EQU\s+0[xX][0-9a-fA-F]+', line) #MDK的表示方法 |
| 80 | + if re_result != None: |
| 81 | + oldline = line |
| 82 | + newline = re.sub('0[xX][0-9a-fA-F]+','0x00000000', oldline) |
| 83 | + flag_need_replace = True |
| 84 | + break |
| 85 | + |
| 86 | + if flag_need_replace == True: |
| 87 | + f.seek(0) |
| 88 | + content = f.read() |
| 89 | + f.seek(0) |
| 90 | + f.truncate() |
| 91 | + newcontent = content.replace(oldline, newline) |
| 92 | + f.write(newcontent) |
| 93 | + |
| 94 | + elif os.path.splitext(file)[1] == '.icf': #找.icf文件(IAR) |
| 95 | + with open(file_path,'r+',) as f: |
| 96 | + flag_need_replace = False |
| 97 | + while True: |
| 98 | + line = f.readline() |
| 99 | + if line == '': |
| 100 | + break |
| 101 | + |
| 102 | + re_result = re.match('\s*define\s+symbol\s+__ICFEDIT_size_heap__\s*=\s*0[xX][0-9a-fA-F]+', line) #IAR的表示方法 |
| 103 | + if re_result != None: |
| 104 | + oldline = line |
| 105 | + newline = re.sub('0[xX][0-9a-fA-F]+','0x000', oldline) |
| 106 | + flag_need_replace = True |
| 107 | + break |
| 108 | + |
| 109 | + if flag_need_replace == True: |
| 110 | + f.seek(0) |
| 111 | + content = f.read() |
| 112 | + f.seek(0) |
| 113 | + f.truncate() |
| 114 | + newcontent = content.replace(oldline, newline) |
| 115 | + f.write(newcontent) |
| 116 | + |
| 117 | + elif os.path.splitext(file)[1] == '.lds': #找.lds文件(GCC) |
| 118 | + with open(file_path,'r+',) as f: |
| 119 | + flag_need_replace = False |
| 120 | + while True: |
| 121 | + line = f.readline() |
| 122 | + if line == '': |
| 123 | + break |
| 124 | + |
| 125 | + re_result = re.match('\s*_system_stack_size\s*=\s*0[xX][0-9a-fA-F]+', line) #GCC的表示方法 |
| 126 | + if re_result != None: |
| 127 | + oldline = line |
| 128 | + newline = re.sub('0[xX][0-9a-fA-F]+','0x000', oldline) |
| 129 | + flag_need_replace = True |
| 130 | + break |
| 131 | + |
| 132 | + if flag_need_replace == True: |
| 133 | + f.seek(0) |
| 134 | + content = f.read() |
| 135 | + f.seek(0) |
| 136 | + f.truncate() |
| 137 | + newcontent = content.replace(oldline, newline) |
| 138 | + f.write(newcontent) |
| 139 | + |
| 140 | +folder_path = input('please input path:') |
| 141 | +main2entry(folder_path) |
| 142 | +heap2zero(folder_path) |
0 commit comments