u_boot_env.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2018 Google, Inc
  3. # Written by Simon Glass <sjg@chromium.org>
  4. #
  5. import struct
  6. import zlib
  7. from binman.etype.blob import Entry_blob
  8. from dtoc import fdt_util
  9. from patman import tools
  10. class Entry_u_boot_env(Entry_blob):
  11. """An entry which contains a U-Boot environment
  12. Properties / Entry arguments:
  13. - filename: File containing the environment text, with each line in the
  14. form var=value
  15. """
  16. def __init__(self, section, etype, node):
  17. super().__init__(section, etype, node)
  18. def ReadNode(self):
  19. super().ReadNode()
  20. if self.size is None:
  21. self.Raise("'u-boot-env' entry must have a size property")
  22. self.fill_value = fdt_util.GetByte(self._node, 'fill-byte', 0)
  23. def ReadBlobContents(self):
  24. indata = tools.ReadFile(self._pathname)
  25. data = b''
  26. for line in indata.splitlines():
  27. data += line + b'\0'
  28. data += b'\0';
  29. pad = self.size - len(data) - 5
  30. if pad < 0:
  31. self.Raise("'u-boot-env' entry too small to hold data (need %#x more bytes)" % -pad)
  32. data += tools.GetBytes(self.fill_value, pad)
  33. crc = zlib.crc32(data)
  34. buf = struct.pack('<I', crc) + b'\x01' + data
  35. self.SetContents(buf)
  36. return True