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