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