build_littlefs.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 (littlefs)',
  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. parser.add_argument('-b', dest = 'block_size', required=True, type=lambda x: hex(int(x,0)))
  53. args = parser.parse_args();
  54. print('DATAFS: Build datafs image (littlefs)')
  55. mb_size = int(args.image_size, 0)
  56. if (is_windows()):
  57. # windows
  58. # mklfs.exe -r 256 -p 256 -b 4096 -c 4096 -L 128 udisk udisk.img 4194304
  59. makelittlefsimg_path = script_path + '/utils/windows/mklfs.exe'
  60. cmd = [makelittlefsimg_path, '-r', str(512), '-p', str(512), '-b', str(args.block_size), '-c', str(4096),'-L', str(128), args.datafs_dir, args.output_file,str(mb_size)]
  61. (outmsg, exit_code) = run_cmd(cmd)
  62. else:
  63. # generate empty image file
  64. output_tmp = args.output_file + ".tmp"
  65. new_file(output_tmp, int(args.image_size, 0), 0xff)
  66. if ('32bit' == platform.architecture()[0]):
  67. # linux x86
  68. makelittlefsimg_path = script_path + '/utils/linux-x86/mklfs'
  69. else:
  70. # linux x86_64
  71. makelittlefsimg_path = script_path + '/utils/linux-x86_64/mklfs'
  72. # generate littlefs image file
  73. # ./mklfs -r 256 -p 256 -b 4096 -c 4096 -L 128 ./udisk udisk.img 4194304
  74. cmd = [makelittlefsimg_path, '-r', str(512), '-p', str(512), '-b', str(args.block_size), '-c', str(4096),'-L', str(128), args.datafs_dir, args.output_file,str(mb_size)]
  75. (outmsg, exit_code) = run_cmd(cmd)
  76. os.remove(output_tmp)
  77. if exit_code !=0:
  78. print('make littlefs error')
  79. print(outmsg.decode('utf-8'))
  80. sys.exit(1)
  81. print('DATAFS: Generate datafs (littlefs) file: %s.' %args.output_file)
  82. if __name__ == "__main__":
  83. main(sys.argv)