makeimg.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import sys
  4. import shutil
  5. import subprocess
  6. import time
  7. import platform
  8. # if debug_info=True, Debugging Print Information will be turned on
  9. debug_info=False
  10. # if make_fal=True, Partition tables are put into firmware
  11. make_fal=False
  12. # Setting firmware output directory
  13. out_path='./Bin'
  14. # Setting the bin file path
  15. bin_file='./rtthread.bin'
  16. # Setting winnermicro libraries path
  17. wmlib_path='./packages/wm_libraries-'
  18. # Setting the 1M flash layout file
  19. layout_1M_file='.'
  20. # Setting the 2M flash layout file
  21. layout_2M_file='.'
  22. # Setting the makeimg by adding rtt flash original fls
  23. makeimg_new_fls='.'
  24. def execute_command(cmdstring, cwd=None, shell=True):
  25. """Execute the system command at the specified address."""
  26. if shell:
  27. cmdstring_list = cmdstring
  28. sub = subprocess.Popen(cmdstring_list, cwd=cwd, stdin=subprocess.PIPE,
  29. stdout=subprocess.PIPE, shell=shell, bufsize=8192)
  30. stdout_str = ""
  31. while sub.poll() is None:
  32. stdout_str += str(sub.stdout.read())
  33. time.sleep(0.1)
  34. return stdout_str
  35. def copy_file(name, path):
  36. res = True
  37. if os.path.exists(path):
  38. shutil.copy(path, out_path)
  39. else:
  40. print('makeimg err! No ' + name + ' file found: ' + path)
  41. res = False
  42. return res
  43. def is_exists(name, path):
  44. res = True
  45. if not os.path.exists(path):
  46. print('makeimg err! No ' + name + ' file found: ' + path)
  47. res = False
  48. return res
  49. def get_exec_path(path):
  50. (file_path, file_name) = os.path.split(path)
  51. (name, extend) = os.path.splitext(file_name)
  52. exec_path = ''
  53. if (platform.system() == "Windows"):
  54. exec_path = os.path.abspath(file_path + '/' + name + '.exe')
  55. elif (platform.system() == "Linux"):
  56. exec_path = os.path.abspath(file_path + '/' + name)
  57. if debug_info:
  58. print('file_path: ' + file_path)
  59. print('file_name: ' + file_name)
  60. print('name: ' + name)
  61. print('extend: ' + extend)
  62. return exec_path
  63. def do_makeimg(tool_path, param):
  64. str = "\"" + tool_path + "\"" + ' ' + param
  65. if debug_info:
  66. print('exec cmd: ' + str);
  67. execute_command(str)
  68. def get_wmlib_path_full(path):
  69. (_wmlib_path,_wmlib_name) = os.path.split(path)
  70. files = os.listdir(_wmlib_path)
  71. for f in files:
  72. if _wmlib_name in f:
  73. return _wmlib_path + '/' + f
  74. return path
  75. if __name__=='__main__':
  76. # find winnermicro libraries full path
  77. wmlib_path_full = get_wmlib_path_full(wmlib_path)
  78. # Setting the version.txt file path
  79. version_file=wmlib_path_full + '/Tools/version.txt'
  80. # Setting the secboot.img file path
  81. secboot_file=wmlib_path_full + '/Tools/secboot.img'
  82. # Setting the wm_gzip.exe file path
  83. wm_gzip_file=wmlib_path_full + '/Tools/wm_gzip.exe'
  84. # Setting the makeimg.exe file path
  85. makeimg_file=wmlib_path_full + '/Tools/makeimg.exe'
  86. # Setting the makeimg_all.exe file path
  87. makeimg_all_file=wmlib_path_full + '/Tools/makeimg_all.exe'
  88. # Get absolute path
  89. out_path = os.path.abspath(out_path).replace('\\', '/');
  90. bin_file = os.path.abspath(bin_file).replace('\\', '/');
  91. version_file = os.path.abspath(version_file).replace('\\', '/');
  92. secboot_file = os.path.abspath(secboot_file).replace('\\', '/');
  93. wm_gzip_file = os.path.abspath(wm_gzip_file).replace('\\', '/');
  94. makeimg_file = os.path.abspath(makeimg_file).replace('\\', '/');
  95. makeimg_all_file = os.path.abspath(makeimg_all_file).replace('\\', '/');
  96. # Create the output directory
  97. if not os.path.exists(out_path): os.mkdir(out_path)
  98. # Copy file
  99. if not copy_file('bin', bin_file): exit(0)
  100. if not copy_file('version', version_file): exit(0)
  101. if not copy_file('secboot', secboot_file): exit(0)
  102. # Check the existence of packaging tools
  103. if not is_exists('wm_gzip', wm_gzip_file): exit(0)
  104. if not is_exists('makeimg', makeimg_file): exit(0)
  105. if not is_exists('makeimg_all', makeimg_all_file): exit(0)
  106. # Get File Names and File Extensions
  107. (bin_file_path,bin_file_name) = os.path.split(bin_file)
  108. (bin_name,bin_extend) = os.path.splitext(bin_file_name)
  109. (version_file_path,version_file_name) = os.path.split(version_file)
  110. (secboot_file_path,secboot_file_name) = os.path.split(secboot_file)
  111. # print debug Information
  112. if debug_info: print('bin_file_name:' + bin_file_name + 'bin_name:' + bin_name + 'bin_extend:' + bin_extend + 'version_file_name:' + version_file_name + 'secboot_file_name:' + secboot_file_name)
  113. print('makeimg 1M Flash...')
  114. file_pos_1M='_1M'
  115. gzip_param = "\"" + out_path + '/' + bin_file_name + "\""
  116. make_img_param = "\"" + out_path + '/' + bin_file_name + "\"" + ' ' + "\"" + out_path + '/' + bin_name + file_pos_1M + '.img' + "\"" + ' 0' + ' 0' + ' ' + "\"" + out_path + '/' + version_file_name + "\"" + ' 90000' + ' 10100'
  117. make_GZ_param = "\"" + out_path + '/' + bin_file_name + '.gz' + "\"" + ' ' + "\"" + out_path + '/' + bin_name + '_GZ' + file_pos_1M + '.img' +"\"" + ' 0' + ' 1' + ' ' + "\"" + out_path + '/' + version_file_name + "\"" + ' 90000' + ' 10100' + ' ' + "\"" + out_path + '/' + bin_file_name + "\""
  118. make_SEC_param = "\"" + out_path + '/' + bin_file_name + "\"" + ' ' + "\"" + out_path + '/' + bin_name + '_SEC' + file_pos_1M + '.img' + "\"" + ' 0' + ' 0' + ' ' + "\"" + out_path + '/' + version_file_name + "\"" + ' 90000' + ' 10100'
  119. make_FLS_param = "\"" + out_path + '/' + secboot_file_name + "\"" + ' ' + "\"" + out_path + '/' + bin_name + file_pos_1M + '.img' + "\"" + ' ' + "\"" + out_path + '/' + bin_name + file_pos_1M + '.FLS' + "\""
  120. if debug_info:
  121. print('gzip_param' + gzip_param)
  122. print('make_img_param' + make_img_param)
  123. print('make_GZ_param' + make_GZ_param)
  124. print('make_SEC_param' + make_SEC_param)
  125. print('make_FLS_param' + make_FLS_param)
  126. do_makeimg(wm_gzip_file, gzip_param)
  127. do_makeimg(makeimg_file, make_img_param)
  128. do_makeimg(makeimg_file, make_GZ_param)
  129. do_makeimg(makeimg_file, make_SEC_param)
  130. do_makeimg(makeimg_all_file, make_FLS_param)
  131. rm_file = out_path + '/' + bin_name + file_pos_1M + '.img'
  132. if os.path.exists(rm_file):
  133. os.remove(rm_file)
  134. rm_file = out_path + '/' + bin_file_name + '.gz'
  135. if os.path.exists(rm_file):
  136. os.remove(rm_file)
  137. print('makeimg 2M Flash...')
  138. file_pos_2M='_2M'
  139. gzip_param = "\"" + out_path + '/' + bin_file_name + "\""
  140. make_img_param = "\"" + out_path + '/' + bin_file_name + "\"" + ' ' + "\"" + out_path + '/' + bin_name + file_pos_2M + '.img' + "\"" + ' 3' + ' 0' + ' ' + "\"" + out_path + '/' + version_file_name + "\"" + ' 100000' + ' 10100'
  141. make_GZ_param = "\"" + out_path + '/' + bin_file_name + '.gz' + "\"" + ' ' + "\"" + out_path + '/' + bin_name + '_GZ' + file_pos_2M + '.img' +"\"" + ' 3' + ' 1' + ' ' + "\"" + out_path + '/' + version_file_name + "\"" + ' 100000' + ' 10100' + ' ' + "\"" + out_path + '/' + bin_file_name + "\""
  142. make_SEC_param = "\"" + out_path + '/' + bin_file_name + "\"" + ' ' + "\"" + out_path + '/' + bin_name + '_SEC' + file_pos_2M + '.img' + "\"" + ' 3' + ' 0' + ' ' + "\"" + out_path + '/' + version_file_name + "\"" + ' 100000' + ' 10100'
  143. make_FLS_param = "\"" + out_path + '/' + secboot_file_name + "\"" + ' ' + "\"" + out_path + '/' + bin_name + file_pos_2M + '.img' + "\"" + ' ' + "\"" + out_path + '/' + bin_name + file_pos_2M + '.FLS' + "\""
  144. if debug_info:
  145. print('gzip_param' + gzip_param)
  146. print('make_img_param' + make_img_param)
  147. print('make_GZ_param' + make_GZ_param)
  148. print('make_SEC_param' + make_SEC_param)
  149. print('make_FLS_param' + make_FLS_param)
  150. do_makeimg(wm_gzip_file, gzip_param)
  151. do_makeimg(makeimg_file, make_img_param)
  152. do_makeimg(makeimg_file, make_GZ_param)
  153. do_makeimg(makeimg_file, make_SEC_param)
  154. do_makeimg(makeimg_all_file, make_FLS_param)
  155. rm_file = out_path + '/' + bin_name + file_pos_2M + '.img'
  156. if os.path.exists(rm_file):
  157. os.remove(rm_file)
  158. rm_file = out_path + '/' + bin_file_name + '.gz'
  159. if os.path.exists(rm_file):
  160. os.remove(rm_file)
  161. if make_fal:
  162. # Get absolute path
  163. layout_1M_file = os.path.abspath(layout_1M_file).replace('\\', '/');
  164. layout_2M_file = os.path.abspath(layout_2M_file).replace('\\', '/');
  165. makeimg_new_fls = os.path.abspath(makeimg_new_fls).replace('\\', '/');
  166. # Create command parameters to new fls
  167. makeimg_new_cmd="\"" + out_path + '/' + bin_name + file_pos_1M + '.FLS' + "\"" + ' ' + "\"" + layout_1M_file + "\"" + ' ' + "\"" + out_path + '/'+ bin_name + '_layout' + file_pos_1M+'.FLS' +"\""
  168. do_makeimg(makeimg_new_fls, makeimg_new_cmd)
  169. makeimg_new_cmd="\"" + out_path + '/' + bin_name + file_pos_2M + '.FLS' + "\"" + ' ' + "\"" + layout_2M_file + "\"" + ' ' + "\"" + out_path + '/'+ bin_name + '_layout' + file_pos_2M+'.FLS' +"\""
  170. do_makeimg(makeimg_new_fls, makeimg_new_cmd)
  171. # Delete temporary files
  172. rm_file = out_path + '/' + bin_name + file_pos_1M + '.FLS'
  173. if os.path.exists(rm_file):
  174. os.remove(rm_file)
  175. rm_file = out_path + '/' + bin_name + file_pos_2M + '.FLS'
  176. if os.path.exists(rm_file):
  177. os.remove(rm_file)
  178. print('end')