cfe-wfi-tag.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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('>IIIII', crc, args.tag_version, args.chip_id, args.flash_type, args.flags)
  44. return tag
  45. def create_output(args):
  46. in_st = os.stat(args.input_file)
  47. in_size = in_st.st_size
  48. in_f = open(args.input_file, 'r+b')
  49. in_bytes = in_f.read(in_size)
  50. in_f.close()
  51. tag = create_tag(args, in_bytes)
  52. out_f = open(args.output_file, 'w+b')
  53. out_f.write(in_bytes)
  54. out_f.write(tag)
  55. out_f.close()
  56. def main():
  57. global args
  58. parser = argparse.ArgumentParser(description='')
  59. parser.add_argument('--input-file',
  60. dest='input_file',
  61. action='store',
  62. type=str,
  63. help='Input file')
  64. parser.add_argument('--output-file',
  65. dest='output_file',
  66. action='store',
  67. type=str,
  68. help='Output file')
  69. parser.add_argument('--version',
  70. dest='tag_version',
  71. action='store',
  72. type=auto_int,
  73. help='WFI Tag Version')
  74. parser.add_argument('--chip-id',
  75. dest='chip_id',
  76. action='store',
  77. type=auto_int,
  78. help='WFI Chip ID')
  79. parser.add_argument('--flash-type',
  80. dest='flash_type',
  81. action='store',
  82. type=auto_int,
  83. help='WFI Flash Type')
  84. parser.add_argument('--flags',
  85. dest='flags',
  86. action='store',
  87. type=auto_int,
  88. help='WFI Flags')
  89. args = parser.parse_args()
  90. if not args.flags:
  91. args.flags = 0
  92. if ((not args.input_file) or
  93. (not args.output_file) or
  94. (not args.tag_version) or
  95. (not args.chip_id) or
  96. (not args.flash_type)):
  97. parser.print_help()
  98. else:
  99. create_output(args)
  100. main()