1
0

cfe-wfi-tag.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env python3
  2. """
  3. Whole Flash Image Tag
  4. {
  5. u32 crc32;
  6. u32 version;
  7. u32 chipID;
  8. u32 flashType;
  9. u32 flags;
  10. }
  11. CRC32: Ethernet (Poly 0x04C11DB7)
  12. Version:
  13. 0x00005700: Any version
  14. 0x00005731: NAND 1MB data partition
  15. 0x00005732: Normal version
  16. Chip ID:
  17. Broadcom Chip ID
  18. 0x00006328: BCM6328
  19. 0x00006362: BCM6362
  20. 0x00006368: BCM6368
  21. 0x00063268: BCM63268
  22. Flash Type:
  23. 1: NOR
  24. 2: NAND 16k blocks
  25. 3: NAND 128k blocks
  26. 4: NAND 256k blocks
  27. 5: NAND 512k blocks
  28. 6: NAND 1MB blocks
  29. 7: NAND 2MB blocks
  30. Flags:
  31. 0x00000001: PMC
  32. 0x00000002: Secure BootROM
  33. """
  34. import argparse
  35. import os
  36. import struct
  37. import binascii
  38. def auto_int(x):
  39. return int(x, 0)
  40. def create_tag(args, in_bytes):
  41. # JAM CRC32 is bitwise not and unsigned
  42. crc = ~binascii.crc32(in_bytes) & 0xFFFFFFFF
  43. tag = struct.pack(
  44. ">IIIII",
  45. crc,
  46. args.tag_version,
  47. args.chip_id,
  48. args.flash_type,
  49. args.flags,
  50. )
  51. return tag
  52. def create_output(args):
  53. in_st = os.stat(args.input_file)
  54. in_size = in_st.st_size
  55. in_f = open(args.input_file, "r+b")
  56. in_bytes = in_f.read(in_size)
  57. in_f.close()
  58. tag = create_tag(args, in_bytes)
  59. out_f = open(args.output_file, "w+b")
  60. out_f.write(in_bytes)
  61. out_f.write(tag)
  62. out_f.close()
  63. def main():
  64. global args
  65. parser = argparse.ArgumentParser(description="")
  66. parser.add_argument(
  67. "--input-file",
  68. dest="input_file",
  69. action="store",
  70. type=str,
  71. help="Input file",
  72. )
  73. parser.add_argument(
  74. "--output-file",
  75. dest="output_file",
  76. action="store",
  77. type=str,
  78. help="Output file",
  79. )
  80. parser.add_argument(
  81. "--version",
  82. dest="tag_version",
  83. action="store",
  84. type=auto_int,
  85. help="WFI Tag Version",
  86. )
  87. parser.add_argument(
  88. "--chip-id",
  89. dest="chip_id",
  90. action="store",
  91. type=auto_int,
  92. help="WFI Chip ID",
  93. )
  94. parser.add_argument(
  95. "--flash-type",
  96. dest="flash_type",
  97. action="store",
  98. type=auto_int,
  99. help="WFI Flash Type",
  100. )
  101. parser.add_argument(
  102. "--flags", dest="flags", action="store", type=auto_int, help="WFI Flags"
  103. )
  104. args = parser.parse_args()
  105. if not args.flags:
  106. args.flags = 0
  107. if (
  108. (not args.input_file)
  109. or (not args.output_file)
  110. or (not args.tag_version)
  111. or (not args.chip_id)
  112. or (not args.flash_type)
  113. ):
  114. parser.print_help()
  115. else:
  116. create_output(args)
  117. main()