gen_empty_partition.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env python
  2. #
  3. # generates an empty binary file
  4. #
  5. # This tool generates an empty binary file of the required size.
  6. #
  7. # Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  8. #
  9. # Licensed under the Apache License, Version 2.0 (the "License");
  10. # you may not use this file except in compliance with the License.
  11. # You may obtain a copy of the License at
  12. #
  13. # http:#www.apache.org/licenses/LICENSE-2.0
  14. #
  15. # Unless required by applicable law or agreed to in writing, software
  16. # distributed under the License is distributed on an "AS IS" BASIS,
  17. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. # See the License for the specific language governing permissions and
  19. # limitations under the License.
  20. from __future__ import print_function, division
  21. from __future__ import unicode_literals
  22. import argparse
  23. import os
  24. import re
  25. import struct
  26. import sys
  27. import hashlib
  28. import binascii
  29. __version__ = '1.0'
  30. quiet = False
  31. def status(msg):
  32. """ Print status message to stderr """
  33. if not quiet:
  34. critical(msg)
  35. def critical(msg):
  36. """ Print critical message to stderr """
  37. if not quiet:
  38. sys.stderr.write(msg)
  39. sys.stderr.write('\n')
  40. def generate_blanked_file(size, output_path):
  41. output = b"\xFF" * size
  42. try:
  43. stdout_binary = sys.stdout.buffer # Python 3
  44. except AttributeError:
  45. stdout_binary = sys.stdout
  46. with stdout_binary if output_path == '-' else open(output_path, 'wb') as f:
  47. f.write(output)
  48. def main():
  49. global quiet
  50. parser = argparse.ArgumentParser(description='Generates an empty binary file of the required size.')
  51. parser.add_argument('--quiet', '-q', help="Don't print status messages to stderr", action='store_true')
  52. parser.add_argument('--size', help='Size of generated the file', type=str, required=True)
  53. parser.add_argument('output', help='Path for binary file.', nargs='?', default='-')
  54. args = parser.parse_args()
  55. quiet = args.quiet
  56. size = int(args.size, 0)
  57. if size > 0 :
  58. generate_blanked_file(size, args.output)
  59. return 0
  60. class InputError(RuntimeError):
  61. def __init__(self, e):
  62. super(InputError, self).__init__(e)
  63. if __name__ == '__main__':
  64. try:
  65. r = main()
  66. sys.exit(r)
  67. except InputError as e:
  68. print(e, file=sys.stderr)
  69. sys.exit(2)