make_fit_atf.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #!/usr/bin/env python2
  2. """
  3. A script to generate FIT image source for rockchip boards
  4. with ARM Trusted Firmware
  5. and multiple device trees (given on the command line)
  6. usage: $0 <dt_name> [<dt_name> [<dt_name] ...]
  7. """
  8. import os
  9. import sys
  10. import getopt
  11. # pip install pyelftools
  12. from elftools.elf.elffile import ELFFile
  13. ELF_SEG_P_TYPE='p_type'
  14. ELF_SEG_P_PADDR='p_paddr'
  15. ELF_SEG_P_VADDR='p_vaddr'
  16. ELF_SEG_P_OFFSET='p_offset'
  17. ELF_SEG_P_FILESZ='p_filesz'
  18. ELF_SEG_P_MEMSZ='p_memsz'
  19. DT_HEADER="""// SPDX-License-Identifier: GPL-2.0+ OR X11
  20. /*
  21. * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd
  22. *
  23. * Minimal dts for a SPL FIT image payload.
  24. */
  25. /dts-v1/;
  26. / {
  27. description = "Configuration to load ATF before U-Boot";
  28. #address-cells = <1>;
  29. images {
  30. uboot@1 {
  31. description = "U-Boot (64-bit)";
  32. data = /incbin/("u-boot-nodtb.bin");
  33. type = "standalone";
  34. os = "U-Boot";
  35. arch = "arm64";
  36. compression = "none";
  37. load = <0x%08x>;
  38. };
  39. """
  40. DT_IMAGES_NODE_END="""
  41. };
  42. """
  43. DT_END="""
  44. };
  45. """
  46. def append_atf_node(file, atf_index, phy_addr):
  47. """
  48. Append ATF DT node to input FIT dts file.
  49. """
  50. data = 'bl31_0x%08x.bin' % phy_addr
  51. print >> file, '\t\tatf@%d {' % atf_index
  52. print >> file, '\t\t\tdescription = \"ARM Trusted Firmware\";'
  53. print >> file, '\t\t\tdata = /incbin/("%s");' % data
  54. print >> file, '\t\t\ttype = "firmware";'
  55. print >> file, '\t\t\tarch = "arm64";'
  56. print >> file, '\t\t\tos = "arm-trusted-firmware";'
  57. print >> file, '\t\t\tcompression = "none";'
  58. print >> file, '\t\t\tload = <0x%08x>;' % phy_addr
  59. if atf_index == 1:
  60. print >> file, '\t\t\tentry = <0x%08x>;' % phy_addr
  61. print >> file, '\t\t};'
  62. print >> file, ''
  63. def append_fdt_node(file, dtbs):
  64. """
  65. Append FDT nodes.
  66. """
  67. cnt = 1
  68. for dtb in dtbs:
  69. dtname = os.path.basename(dtb)
  70. print >> file, '\t\tfdt@%d {' % cnt
  71. print >> file, '\t\t\tdescription = "%s";' % dtname
  72. print >> file, '\t\t\tdata = /incbin/("%s");' % dtb
  73. print >> file, '\t\t\ttype = "flat_dt";'
  74. print >> file, '\t\t\tcompression = "none";'
  75. print >> file, '\t\t};'
  76. print >> file, ''
  77. cnt = cnt + 1
  78. def append_conf_section(file, cnt, dtname, atf_cnt):
  79. print >> file, '\t\tconfig@%d {' % cnt
  80. print >> file, '\t\t\tdescription = "%s";' % dtname
  81. print >> file, '\t\t\tfirmware = "atf@1";'
  82. print >> file, '\t\t\tloadables = "uboot@1",',
  83. for i in range(1, atf_cnt):
  84. print >> file, '"atf@%d"' % (i+1),
  85. if i != (atf_cnt - 1):
  86. print >> file, ',',
  87. else:
  88. print >> file, ';'
  89. print >> file, '\t\t\tfdt = "fdt@1";'
  90. print >> file, '\t\t};'
  91. print >> file, ''
  92. def append_conf_node(file, dtbs, atf_cnt):
  93. """
  94. Append configeration nodes.
  95. """
  96. cnt = 1
  97. print >> file, '\tconfigurations {'
  98. print >> file, '\t\tdefault = "config@1";'
  99. for dtb in dtbs:
  100. dtname = os.path.basename(dtb)
  101. append_conf_section(file, cnt, dtname, atf_cnt)
  102. cnt = cnt + 1
  103. print >> file, '\t};'
  104. print >> file, ''
  105. def generate_atf_fit_dts(fit_file_name, bl31_file_name, uboot_file_name, dtbs_file_name):
  106. """
  107. Generate FIT script for ATF image.
  108. """
  109. if fit_file_name != sys.stdout:
  110. fit_file = open(fit_file_name, "wb")
  111. else:
  112. fit_file = sys.stdout
  113. num_load_seg = 0
  114. p_paddr = 0xFFFFFFFF
  115. with open(uboot_file_name) as uboot_file:
  116. uboot = ELFFile(uboot_file)
  117. for i in range(uboot.num_segments()):
  118. seg = uboot.get_segment(i)
  119. if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)):
  120. p_paddr = seg.__getitem__(ELF_SEG_P_PADDR)
  121. num_load_seg = num_load_seg + 1
  122. assert (p_paddr != 0xFFFFFFFF and num_load_seg == 1)
  123. print >> fit_file, DT_HEADER % p_paddr
  124. with open(bl31_file_name) as bl31_file:
  125. bl31 = ELFFile(bl31_file)
  126. for i in range(bl31.num_segments()):
  127. seg = bl31.get_segment(i)
  128. if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)):
  129. paddr = seg.__getitem__(ELF_SEG_P_PADDR)
  130. p= seg.__getitem__(ELF_SEG_P_PADDR)
  131. append_atf_node(fit_file, i+1, paddr)
  132. atf_cnt = i+1
  133. append_fdt_node(fit_file, dtbs_file_name)
  134. print >> fit_file, '%s' % DT_IMAGES_NODE_END
  135. append_conf_node(fit_file, dtbs_file_name, atf_cnt)
  136. print >> fit_file, '%s' % DT_END
  137. if fit_file_name != sys.stdout:
  138. fit_file.close()
  139. def generate_atf_binary(bl31_file_name):
  140. with open(bl31_file_name) as bl31_file:
  141. bl31 = ELFFile(bl31_file)
  142. num = bl31.num_segments()
  143. for i in range(num):
  144. seg = bl31.get_segment(i)
  145. if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)):
  146. paddr = seg.__getitem__(ELF_SEG_P_PADDR)
  147. file_name = 'bl31_0x%08x.bin' % paddr
  148. with open(file_name, "wb") as atf:
  149. atf.write(seg.data());
  150. def get_bl31_segments_info(bl31_file_name):
  151. """
  152. Get load offset, physical offset, file size
  153. from bl31 elf file program headers.
  154. """
  155. with open(bl31_file_name) as bl31_file:
  156. bl31 = ELFFile(bl31_file)
  157. num = bl31.num_segments()
  158. print 'Number of Segments : %d' % bl31.num_segments()
  159. for i in range(num):
  160. print 'Segment %d' % i
  161. seg = bl31.get_segment(i)
  162. ptype = seg[ELF_SEG_P_TYPE]
  163. poffset = seg[ELF_SEG_P_OFFSET]
  164. pmemsz = seg[ELF_SEG_P_MEMSZ]
  165. pfilesz = seg[ELF_SEG_P_FILESZ]
  166. print 'type: %s\nfilesz: %08x\nmemsz: %08x\noffset: %08x' % (ptype, pfilesz, pmemsz, poffset)
  167. paddr = seg[ELF_SEG_P_PADDR]
  168. print 'paddr: %08x' % paddr
  169. def main():
  170. uboot_elf="./u-boot"
  171. bl31_elf="./bl31.elf"
  172. FIT_ITS=sys.stdout
  173. opts, args = getopt.getopt(sys.argv[1:], "o:u:b:h")
  174. for opt, val in opts:
  175. if opt == "-o":
  176. FIT_ITS=val
  177. elif opt == "-u":
  178. uboot_elf=val
  179. elif opt == "-b":
  180. bl31_elf=val
  181. elif opt == "-h":
  182. print __doc__
  183. sys.exit(2)
  184. dtbs = args
  185. #get_bl31_segments_info("u-boot")
  186. #get_bl31_segments_info("bl31.elf")
  187. generate_atf_fit_dts(FIT_ITS, bl31_elf, uboot_elf, dtbs)
  188. generate_atf_binary(bl31_elf);
  189. if __name__ == "__main__":
  190. main()