build_check.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # !/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. import os
  4. import re
  5. import datetime
  6. import sys
  7. import json
  8. import argparse
  9. class BuildCheck(object):
  10. def __init__(self):
  11. self._des = []
  12. pass
  13. def partition_check(self,path):
  14. try:
  15. f = open(path, encoding="utf-8",mode='r')
  16. partiton_data = json.load(f)
  17. temp = int("1000000",16)
  18. Align4K = int("1000",16)
  19. ##### macros ######
  20. APP_IMG_ADDR = int(partiton_data['macros']['CONFIG_APPIMG_FLASH_ADDRESS'].replace('0x',''),16) #APP IMG 地址
  21. APP_IMG_SIZE = int(partiton_data['macros']['CONFIG_APPIMG_FLASH_SIZE'].replace('0x',''),16) #APP IMG 大小
  22. FS_SYS_ADDR = int(partiton_data['macros']['CONFIG_FS_SYS_FLASH_ADDRESS'].replace('0x',''),16) #FS 地址
  23. FS_SYS_SIZE = int(partiton_data['macros']['CONFIG_FS_SYS_FLASH_SIZE'].replace('0x',''),16) #FS 大小
  24. ##### descriptions ####
  25. DES_FS_OFFEST = int(partiton_data['descriptions'][0]['offset'].replace('0x',''),16) #FS 偏移
  26. DES_FS_SIZE = int(partiton_data['descriptions'][0]['size'].replace('0x',''),16) #FS 大小
  27. Fs_Align = int(partiton_data['descriptions'][0]['erase_block'].replace('0x',''),16) #FS 对齐大小
  28. if(partiton_data['macros']['CONFIG_APPIMG_FLASH_ADDRESS'][2] == partiton_data['macros']['CONFIG_FS_SYS_FLASH_ADDRESS'][2]) :
  29. if (APP_IMG_ADDR+APP_IMG_SIZE > FS_SYS_ADDR):
  30. raise Exception("partition size error")
  31. if ("CONFIG_APPIMG_FLASH2_ENABLED" in partiton_data['macros']) == True :
  32. flash4M_size = int("400000",16)
  33. if (FS_SYS_SIZE + APP_IMG_SIZE >flash4M_size) or (FS_SYS_SIZE + FS_SYS_ADDR > (APP_IMG_ADDR + flash4M_size)): #APP IMG 及 FS 在外挂4M FLASH 上,SIZE大小分配错误
  34. raise Exception("partition 4Mflash oversize")
  35. else :
  36. FS_MODEM_ADDR = int(partiton_data['macros']['CONFIG_FS_MODEM_FLASH_ADDRESS'].replace('0x',''),16)
  37. if (FS_MODEM_ADDR < FS_SYS_SIZE + FS_SYS_ADDR):
  38. raise Exception("partition 8Mflash oversize")
  39. else :
  40. if ("CONFIG_FS_EXT_ENABLED" in partiton_data['macros']) == True :
  41. if(partiton_data['macros']['CONFIG_FS_EXT_ENABLED'] == "on") :
  42. print("using ext flash")#do nothing
  43. else:
  44. print("no ext flash")#do nothing
  45. if (FS_SYS_SIZE != DES_FS_SIZE) or (FS_SYS_ADDR % temp != DES_FS_OFFEST):
  46. raise Exception("partition file system size error")
  47. if (APP_IMG_SIZE % Align4K != 0) or (FS_SYS_SIZE % Fs_Align !=0) or (APP_IMG_ADDR % Align4K != 0) or (FS_SYS_ADDR % Fs_Align !=0): #检测地址和大小对齐
  48. raise Exception("partition size align error")
  49. f.close()
  50. return 0
  51. except Exception as e:
  52. import traceback
  53. print(traceback.format_exc())
  54. f.close()
  55. return 1
  56. def kernel_size_check(self,path,kernel_img):
  57. try:
  58. f = open(path, encoding="utf-8",mode='r')
  59. partiton_data = json.load(f)
  60. APP_IMG_SIZE = int(partiton_data['macros']['CONFIG_APP_FLASH_SIZE'].replace('0x',''),16)
  61. stats = os.stat(kernel_img)
  62. coreimg_size = stats.st_size
  63. if(coreimg_size > APP_IMG_SIZE):
  64. raise Exception("kernel img oversize")
  65. remain_size = APP_IMG_SIZE-coreimg_size
  66. print("kernel total size:0x{:04x}|{:.2f}KB,used:0x{:04x}|{:.2f}KB,remain:0x{:04x}|{:.2f}KB".format(APP_IMG_SIZE,APP_IMG_SIZE/1024,coreimg_size,coreimg_size/1024,remain_size,remain_size/1024))
  67. f.close()
  68. return 0
  69. except Exception as e:
  70. import traceback
  71. print(traceback.format_exc())
  72. f.close()
  73. return 1
  74. def app_size_check(self,path,app_img):
  75. try:
  76. f = open(path, encoding="utf-8",mode='r')
  77. partiton_data = json.load(f)
  78. APP_SIZE = int(partiton_data['macros']['CONFIG_APPIMG_FLASH_SIZE'].replace('0x',''),16)
  79. stats = os.stat(app_img)
  80. app_size = stats.st_size
  81. if(app_size > APP_SIZE):
  82. raise Exception("app img oversize")
  83. remain_size = APP_SIZE-app_size
  84. print("app total size:0x{:04x}|{:.2f}KB,used:0x{:04x}|{:.2f}KB,remain:0x{:04x}|{:.2f}KB".format(APP_SIZE,APP_SIZE/1024,app_size,app_size/1024,remain_size,remain_size/1024))
  85. f.close()
  86. return 0
  87. except Exception as e:
  88. import traceback
  89. print(traceback.format_exc())
  90. f.close()
  91. return 1
  92. def spl_size_check(self,path,spl_img,spl_sign_img):
  93. try:
  94. #f = open(path, encoding="utf-8",mode='r')
  95. #partiton_data = json.load(f)
  96. #SPL_SIZE = int(partiton_data['macros']['CONFIG_SPL_FLASH_SIZE'].replace('0x',''),16)
  97. SPL_SIZE = int(0x12C00) #固定75KB(预留5KB)
  98. if(os.path.isfile(spl_sign_img)):
  99. stats = os.stat(spl_sign_img)
  100. else:
  101. stats = os.stat(spl_img)
  102. spl_size = stats.st_size
  103. if(spl_size > SPL_SIZE):
  104. over_size = SPL_SIZE-spl_size
  105. print("spl total size:0x{:04x}|{:.2f}KB,used:0x{:04x}|{:.2f}KB,overflow:0x{:04x}|{:.2f}KB".format(SPL_SIZE,SPL_SIZE/1024,spl_size,spl_size/1024,over_size,over_size/1024))
  106. raise Exception("spl img oversize")
  107. remain_size = SPL_SIZE-spl_size
  108. print("spl total size:0x{:04x}|{:.2f}KB,used:0x{:04x}|{:.2f}KB,remain:0x{:04x}|{:.2f}KB".format(SPL_SIZE,SPL_SIZE/1024,spl_size,spl_size/1024,remain_size,remain_size/1024))
  109. #f.close()
  110. return 0
  111. except Exception as e:
  112. import traceback
  113. print(traceback.format_exc())
  114. #f.close()
  115. return 1
  116. def file_size_check(self,path,size):
  117. try:
  118. stats = os.stat(path)
  119. file_size = stats.st_size
  120. if(file_size < size):
  121. raise Exception("file size error")
  122. return 0
  123. except Exception as e:
  124. import traceback
  125. print(traceback.format_exc())
  126. return 1
  127. def main(argv):
  128. try:
  129. parser = argparse.ArgumentParser(description='manual to this script' , formatter_class=argparse.RawTextHelpFormatter)
  130. parser.add_argument("--coreimg", type=str, default="none")
  131. parser.add_argument("--appimg", type=str, default="none")
  132. parser.add_argument("--splimg", type=str, default="none")
  133. parser.add_argument("--splsignimg", type=str, default="none")
  134. parser.add_argument("--partinfo", type=str, default="none")
  135. parser.add_argument("--file", type=str, default="none")
  136. parser.add_argument("--size", type=int, default=0)
  137. opt = parser.parse_args()
  138. file_path = opt.file
  139. file_size = opt.size
  140. partiton_path = opt.partinfo
  141. core_img_path = opt.coreimg
  142. app_img_path = opt.appimg
  143. spl_img_path = opt.splimg
  144. spl_sign_img_path = opt.splsignimg
  145. build_check = BuildCheck()
  146. if(partiton_path != "none"):
  147. if(core_img_path == "none" and app_img_path == "none" and spl_img_path == "none" and spl_sign_img_path == "none"):
  148. return build_check.partition_check(partiton_path)
  149. elif (core_img_path != "none" and app_img_path == "none" and spl_img_path == "none" and spl_sign_img_path == "none"):
  150. return build_check.kernel_size_check(partiton_path,core_img_path)
  151. elif (core_img_path == "none" and app_img_path != "none" and spl_img_path == "none"and spl_sign_img_path == "none"):
  152. return build_check.app_size_check(partiton_path,app_img_path)
  153. elif (core_img_path == "none" and app_img_path == "none" and spl_img_path != "none" and spl_sign_img_path != "none"):
  154. return build_check.spl_size_check(partiton_path,spl_img_path,spl_sign_img_path)
  155. elif (file_path != "none" and file_size!=0):
  156. return build_check.file_size_check(file_path,file_size)
  157. else:
  158. raise Exception("invalid parameters")
  159. except Exception as e:
  160. import traceback
  161. print(traceback.format_exc())
  162. return 1
  163. if __name__ == '__main__':
  164. sys.exit(main(sys.argv[1:]))