text.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2018 Google, Inc
  3. # Written by Simon Glass <sjg@chromium.org>
  4. #
  5. from collections import OrderedDict
  6. from binman.entry import Entry, EntryArg
  7. from dtoc import fdt_util
  8. from patman import tools
  9. class Entry_text(Entry):
  10. """An entry which contains text
  11. The text can be provided either in the node itself or by a command-line
  12. argument. There is a level of indirection to allow multiple text strings
  13. and sharing of text.
  14. Properties / Entry arguments:
  15. text-label: The value of this string indicates the property / entry-arg
  16. that contains the string to place in the entry
  17. <xxx> (actual name is the value of text-label): contains the string to
  18. place in the entry.
  19. <text>: The text to place in the entry (overrides the above mechanism).
  20. This is useful when the text is constant.
  21. Example node::
  22. text {
  23. size = <50>;
  24. text-label = "message";
  25. };
  26. You can then use:
  27. binman -amessage="this is my message"
  28. and binman will insert that string into the entry.
  29. It is also possible to put the string directly in the node::
  30. text {
  31. size = <8>;
  32. text-label = "message";
  33. message = "a message directly in the node"
  34. };
  35. or just::
  36. text {
  37. size = <8>;
  38. text = "some text directly in the node"
  39. };
  40. The text is not itself nul-terminated. This can be achieved, if required,
  41. by setting the size of the entry to something larger than the text.
  42. """
  43. def __init__(self, section, etype, node):
  44. super().__init__(section, etype, node)
  45. value = fdt_util.GetString(self._node, 'text')
  46. if value:
  47. value = tools.ToBytes(value)
  48. else:
  49. label, = self.GetEntryArgsOrProps([EntryArg('text-label', str)])
  50. self.text_label = label
  51. if self.text_label:
  52. value, = self.GetEntryArgsOrProps([EntryArg(self.text_label,
  53. str)])
  54. value = tools.ToBytes(value) if value is not None else value
  55. self.value = value
  56. def ObtainContents(self):
  57. if not self.value:
  58. self.Raise("No value provided for text label '%s'" %
  59. self.text_label)
  60. self.SetContents(self.value)
  61. return True