part_dts2xml.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2019, Nordic Semiconductor
  3. # SPDX-License-Identifier: BSD-3-Clause
  4. import os
  5. import shutil
  6. import sys
  7. import argparse
  8. import dtlib
  9. import xml.etree.ElementTree as ET
  10. # Test suite for dtlib.py. Run it directly as an executable, in this directory:
  11. #
  12. # $ ./testdtlib.py
  13. # TODO: Factor out common code from error tests
  14. def find_partions_node(dt):
  15. for node in dt.node_iter():
  16. #print("1.node: %s \n" %(node.name))
  17. if ("partitions" == node.name):
  18. return node
  19. return None
  20. def get_type_by_label(label):
  21. if label == "mbrec-0" or label == "mbrec-1":
  22. return "BOOT"
  23. if label == "param-0" or label == "param-1":
  24. return "SYS_PARAM"
  25. if label == "mcuboot":
  26. return "RECOVERY"
  27. if label == "image-0":
  28. return "SYSTEM"
  29. if label == "image-1":
  30. return "SYSTEM"
  31. return "DATA"
  32. def get_name_by_label(label):
  33. if label == "mbrec-0":
  34. return "fw0_boot"
  35. if label == "mbrec-1":
  36. return "fw1_boot"
  37. if label == "param-0":
  38. return "fw0_param"
  39. if label == "param-1":
  40. return "fw1_param"
  41. if label == "mcuboot":
  42. return "fw0_rec"
  43. if label == "image-0":
  44. return "fw0_sys"
  45. if label == "image-1":
  46. return "fw1_sys"
  47. if label == "sdfs-0":
  48. return "fw0_sdfs"
  49. if label == "sdfs-1":
  50. return "fw1_sdfs"
  51. return label
  52. def get_filename_by_label(label):
  53. if label == "mbrec-0":
  54. return "mbrec.bin"
  55. if label == "param-0":
  56. return "param.bin"
  57. if label == "mcuboot":
  58. return "mcuboot.bin"
  59. if label == "image-0":
  60. return "app.bin"
  61. if label == "sdfs-0":
  62. return "sdfs.bin"
  63. if label == "nvram_factory":
  64. return "nvram.bin"
  65. return None
  66. def is_need_mirror_field(label):
  67. if label == "mbrec-0" or label == "mbrec-1":
  68. return True
  69. if label == "param-0" or label == "param-1":
  70. return True
  71. if label == "image-0" or label == "image-1":
  72. return True
  73. if label == "sdfs-0" or label == "sdfs-1":
  74. return True
  75. return False
  76. def main(argv):
  77. parser = argparse.ArgumentParser()
  78. parser.add_argument("--dts", required=True, help="DTS file")
  79. parser.add_argument("--xml", required=True, help="out XML file")
  80. args = parser.parse_args()
  81. dt = dtlib.DT(args.dts)
  82. node = find_partions_node(dt)
  83. if (node == None):
  84. print("error:not find partitions node\n")
  85. return
  86. parent = node.parent
  87. pparent = parent.parent
  88. flash_size=int.from_bytes(parent._get_prop('reg').value[4:], "big", signed=False)
  89. flash_addr=int.from_bytes(parent._get_prop('reg').value[:4], "big", signed=False)
  90. name=pparent._get_prop('label').to_string()
  91. print("parent lable=%s flash_size 0x%x\n"%(name, flash_size))
  92. if os.path.exists(args.xml):
  93. os.remove(args.xml)
  94. root = ET.Element('firmware')
  95. root.text = '\n\t'
  96. root.tail = '\n'
  97. fw_des = ET.SubElement(root, 'descirption')
  98. fw_des.text = 'Firmware layout for LARK'
  99. fw_des.tail = '\n\t'
  100. fw_disk = ET.SubElement(root, 'disk_size')
  101. fw_disk.text = ("0x%x"%(flash_size))
  102. fw_disk.tail = '\n\n\t'
  103. fw_ver = ET.SubElement(root, 'firmware_version')
  104. fw_ver.text = '\n\t\t'
  105. fw_vcode = ET.SubElement(fw_ver, 'version_code')
  106. fw_vcode.text= '0x10000'
  107. fw_vcode.tail = '\n\t\t'
  108. fw_vname = ET.SubElement(fw_ver, 'version_name')
  109. fw_vname.text= "1.00_$(build_time)"
  110. fw_vname.tail = '\n\t'
  111. fw_ver.tail = '\n\n\t'
  112. fw_paris = ET.SubElement(root, 'partitions')
  113. fw_paris.text = '\n\n\t\t'
  114. file_id = 1
  115. for ch_node in node.node_iter():
  116. if node != ch_node :
  117. print("ch node: %s \n" %(ch_node.name))
  118. addr=int.from_bytes(ch_node._get_prop('reg').value[:4], "big", signed=False)
  119. size=int.from_bytes(ch_node._get_prop('reg').value[4:], "big", signed=False)
  120. name=ch_node._get_prop('label').to_string()
  121. #print("ch node lable=%s reg=0x%x, 0x%x\n"%(name, addr, size))
  122. part_node = ET.SubElement(fw_paris, 'partition')
  123. part_node.text = '\n\t\t\t'
  124. part_node.tail = '\n\n\t\t'
  125. part_addr = ET.SubElement(part_node, 'address')
  126. part_addr.text=("0x%x"%(addr))
  127. part_addr.tail = '\n\t\t\t'
  128. part_size = ET.SubElement(part_node, 'size')
  129. part_size.text=("0x%x"%(size))
  130. part_size.tail = '\n\t\t\t'
  131. part_type = ET.SubElement(part_node, 'type')
  132. part_type.text= get_type_by_label(name)
  133. part_type.tail = '\n\t\t\t'
  134. part_name = ET.SubElement(part_node, 'name')
  135. part_name.text= get_name_by_label(name)
  136. part_name.tail = '\n\t\t\t'
  137. part_fid = ET.SubElement(part_node, 'file_id')
  138. part_fid.text=("%d"%(file_id))
  139. part_fid.tail = '\n\t\t\t'
  140. fname = get_filename_by_label(name)
  141. if fname != None :
  142. part_fname = ET.SubElement(part_node, 'file_name')
  143. part_fname.text = fname
  144. part_fname.tail = '\n\t\t\t'
  145. part_faddr = ET.SubElement(part_node, 'file_address')
  146. part_faddr.text=("0x%x"%(addr))
  147. part_faddr.tail = '\n\t\t\t'
  148. part_crc = ET.SubElement(part_node, 'enable_crc')
  149. part_crc.text="false"
  150. part_crc.tail = '\n\t\t\t'
  151. part_encryption = ET.SubElement(part_node, 'enable_encryption')
  152. part_encryption.text="false"
  153. part_encryption.tail = '\n\t\t\t'
  154. part_ota = ET.SubElement(part_node, 'enable_ota')
  155. if fname != None :
  156. part_ota.text="true"
  157. else :
  158. part_ota.text="false"
  159. part_ota.tail = '\n\t\t\t'
  160. part_raw = ET.SubElement(part_node, 'enable_raw')
  161. if fname != None :
  162. part_raw.text="true"
  163. else :
  164. part_raw.text="false"
  165. part_raw.tail = '\n\t\t\t'
  166. part_dfu = ET.SubElement(part_node, 'enable_dfu')
  167. if fname != None :
  168. part_dfu.text="true"
  169. else :
  170. part_dfu.text="false"
  171. part_dfu.tail = '\n\t\t\t'
  172. if is_need_mirror_field(name):
  173. part_mid = ET.SubElement(part_node, 'mirror_id')
  174. if fname != None :
  175. part_mid.text="0"
  176. else :
  177. part_mid.text="1"
  178. part_mid.tail = '\n\t\t'
  179. file_id = file_id + 1
  180. fw_paris.tail = '\n\n'
  181. tree = ET.ElementTree(root)
  182. tree.write(args.xml, xml_declaration=True, method="xml", encoding='UTF-8')
  183. if __name__ == "__main__":
  184. main(sys.argv)