build_datafs.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python3
  2. #
  3. # Build Actions datafs image
  4. #
  5. # Copyright (c) 2017 Actions Semiconductor Co., Ltd
  6. #
  7. # SPDX-License-Identifier: Apache-2.0
  8. #
  9. import os
  10. import sys
  11. import struct
  12. import argparse
  13. import platform
  14. import subprocess
  15. script_path = os.path.split(os.path.realpath(__file__))[0]
  16. def run_cmd(cmd):
  17. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  18. output, _ = p.communicate()
  19. # print("%s" % (output.rstrip()))
  20. return (output, p.returncode)
  21. def dd_file(input_file, output_file, block_size=1, count=None, seek=None, skip=None):
  22. """Wrapper around the dd command"""
  23. cmd = [
  24. "dd", "if=%s" % input_file, "of=%s" % output_file,
  25. "bs=%s" % block_size, "conv=notrunc"]
  26. if count is not None:
  27. cmd.append("count=%s" % count)
  28. if seek is not None:
  29. cmd.append("seek=%s" % seek)
  30. if skip is not None:
  31. cmd.append("skip=%s" % skip)
  32. (_, exit_code) = run_cmd(cmd)
  33. def new_file(filename, filesize, fillbyte = 0xff):
  34. with open(filename, 'wb') as f:
  35. f.write(bytearray([fillbyte]*filesize))
  36. def is_windows():
  37. sysstr = platform.system()
  38. if (sysstr.startswith('Windows') or \
  39. sysstr.startswith('MSYS') or \
  40. sysstr.startswith('MINGW') or \
  41. sysstr.startswith('CYGWIN')):
  42. return 1
  43. else:
  44. return 0
  45. def main(argv):
  46. parser = argparse.ArgumentParser(
  47. description='Build datafs image (fatfs)',
  48. )
  49. parser.add_argument('-o', dest = 'output_file', required=True)
  50. parser.add_argument('-d', dest = 'datafs_dir', required=True)
  51. parser.add_argument('-s', dest = 'image_size', required=True, type=lambda x: hex(int(x,0)))
  52. args = parser.parse_args();
  53. print('DATAFS: Build datafs image (fatfs)')
  54. mb_size = int(args.image_size, 0) / 1024 / 1024
  55. if (is_windows()):
  56. # windows
  57. makefatimg_path = script_path + '/utils/windows/DiskImageExe.exe'
  58. cmd = [makefatimg_path, str(mb_size), args.output_file, args.datafs_dir]
  59. (outmsg, exit_code) = run_cmd(cmd)
  60. else:
  61. # generate empty image file
  62. # output_tmp = args.output_file + ".tmp"
  63. # new_file(output_tmp, int(args.image_size, 0), 0xff)
  64. if ('32bit' == platform.architecture()[0]):
  65. # linux x86
  66. makebootfat_path = script_path + '/utils/linux-x86/makebootfat'
  67. diskadjust_path = script_path + '/utils/linux-x86/DiskAdjust'
  68. else:
  69. # linux x86_64
  70. makebootfat_path = script_path + '/utils/linux-x86_64/makebootfat'
  71. diskadjust_path = script_path + '/utils/linux-x86_64/DiskAdjust'
  72. if (mb_size > 128):
  73. bootsect = script_path + '/utils/bootsect32.bin'
  74. else:
  75. bootsect = script_path + '/utils/bootsect.bin'
  76. # generate fatfs image file
  77. cmd = [makebootfat_path, '-b', bootsect, '-s', args.image_size, '-o', args.output_file, args.datafs_dir]
  78. (outmsg, exit_code) = run_cmd(cmd)
  79. # shrink fatfs image file
  80. # cmd = [diskadjust_path, output_tmp, args.output_file]
  81. # (outmsg, exit_code) = run_cmd(cmd)
  82. # os.remove(output_tmp)
  83. if exit_code !=0:
  84. print('make fatfs error')
  85. print(outmsg.decode('utf-8'))
  86. sys.exit(1)
  87. print('DATAFS: Generate datafs file: %s.' %args.output_file)
  88. if __name__ == "__main__":
  89. main(sys.argv)