bin2c.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env python
  2. ###############################################################################
  3. # File Name: elf2c.py
  4. #
  5. # Description:
  6. # Basic python script to convert the binary output from objcopy into a basic c
  7. # array.
  8. #
  9. ###############################################################################
  10. # Copyright 2019-2021 Cypress Semiconductor Corporation (an Infineon company)
  11. # or an affiliate of Cypress Semiconductor Corporation
  12. #
  13. # Licensed under the Apache License, Version 2.0 (the "License");
  14. # you may not use this file except in compliance with the License.
  15. # You may obtain a copy of the License at
  16. #
  17. # http://www.apache.org/licenses/LICENSE-2.0
  18. #
  19. # Unless required by applicable law or agreed to in writing, software
  20. # distributed under the License is distributed on an "AS IS" BASIS,
  21. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. # See the License for the specific language governing permissions and
  23. # limitations under the License.
  24. ###############################################################################
  25. import os
  26. import sys
  27. import argparse
  28. import binascii
  29. number_of_columns = 16
  30. copyright = """/***************************************************************************//**
  31. * \\file {}
  32. *
  33. * \\brief
  34. * Cortex-M0+ prebuilt application image.
  35. *
  36. ********************************************************************************
  37. * \\copyright
  38. * Copyright (c) 2018-2021 Cypress Semiconductor Corporation (an Infineon
  39. * company) or an affiliate of Cypress Semiconductor Corporation
  40. * SPDX-License-Identifier: LicenseRef-PBL
  41. *
  42. * Licensed under the Permissive Binary License
  43. *******************************************************************************/
  44. """
  45. header = """
  46. #if defined(__APPLE__) && defined(__clang__)
  47. __attribute__ ((__section__("__CY_M0P_IMAGE,__cy_m0p_image"), used))
  48. #elif defined(__GNUC__) || defined(__ARMCC_VERSION)
  49. __attribute__ ((__section__(".cy_m0p_image"), used))
  50. #elif defined(__ICCARM__)
  51. #pragma location=".cy_m0p_image"
  52. #else
  53. #error "An unsupported toolchain"
  54. #endif
  55. const uint8_t cy_m0p_image[] = {
  56. """
  57. c_list = []
  58. # Get component name from the command line
  59. parser = argparse.ArgumentParser()
  60. parser.add_argument("bin_path", help="Specify path the input binary file to be converted to a C array.")
  61. parser.add_argument("c_path", help="Specify path the output C file with the C array.")
  62. parser.add_argument("c_macro", help="Specify the C preprocessor macro to wrap the variable.")
  63. args = parser.parse_args()
  64. bin_path = args.bin_path
  65. c_path = args.c_path
  66. c_macro = args.c_macro
  67. c_file=os.path.basename(c_path)
  68. f = open(bin_path, "rb")
  69. data = list(f.read())
  70. with open(c_path, "w") as c_fd:
  71. # Clear content of the file
  72. c_fd.seek(0)
  73. c_fd.truncate()
  74. # Write copyright
  75. c_fd.write(copyright.format(c_file))
  76. # Include headers
  77. c_fd.write("#include <stdint.h>\n")
  78. c_fd.write("#include \"cy_device_headers.h\"\n")
  79. c_fd.write("\n")
  80. # Open conditional block
  81. if c_macro:
  82. c_fd.write("#if defined(" + c_macro + ")\n")
  83. # Write template
  84. c_fd.write(header)
  85. # Generate list of the data bytes
  86. for n in data:
  87. c_list.append(format(n, '#04x'))
  88. for i in range(int(len(c_list) / number_of_columns) + 1):
  89. line_list = c_list[i * number_of_columns: (i + 1) * number_of_columns]
  90. c_fd.write(" ")
  91. for item in line_list:
  92. c_fd.write(" %su," % item)
  93. c_fd.write("\n")
  94. c_fd.write("};\n")
  95. # Close conditional block
  96. if c_macro:
  97. c_fd.write("#endif /* defined(" + c_macro + ") */")
  98. c_fd.write("\n")
  99. f.close()