Merge tag 'dm-pull-9jul19-take2' of https://gitlab.denx.de/u-boot/custodians/u-boot-dm
[oweals/u-boot.git] / tools / binman / etype / text.py
1 # SPDX-License-Identifier: GPL-2.0+
2 # Copyright (c) 2018 Google, Inc
3 # Written by Simon Glass <sjg@chromium.org>
4 #
5
6 from collections import OrderedDict
7
8 from entry import Entry, EntryArg
9 import fdt_util
10 import tools
11
12
13 class 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
26     Example node:
27
28         text {
29             size = <50>;
30             text-label = "message";
31         };
32
33     You can then use:
34
35         binman -amessage="this is my message"
36
37     and binman will insert that string into the entry.
38
39     It is also possible to put the string directly in the node:
40
41         text {
42             size = <8>;
43             text-label = "message";
44             message = "a message directly in the node"
45         };
46
47     The text is not itself nul-terminated. This can be achieved, if required,
48     by setting the size of the entry to something larger than the text.
49     """
50     def __init__(self, section, etype, node):
51         Entry.__init__(self, section, etype, node)
52         label, = self.GetEntryArgsOrProps([EntryArg('text-label', str)])
53         self.text_label = tools.ToStr(label) if type(label) != str else label
54         value, = self.GetEntryArgsOrProps([EntryArg(self.text_label, str)])
55         value = tools.ToBytes(value) if value is not None else value
56         self.value = value
57
58     def ObtainContents(self):
59         if not self.value:
60             self.Raise("No value provided for text label '%s'" %
61                        self.text_label)
62         self.SetContents(self.value)
63         return True