3 # Copyright (C) 2016 Google, Inc
4 # Written by Simon Glass <sjg@chromium.org>
6 # SPDX-License-Identifier: GPL-2.0+
11 # A list of types we support
12 (TYPE_BYTE, TYPE_INT, TYPE_STRING, TYPE_BOOL) = range(4)
14 def BytesToValue(bytes):
15 """Converts a string of bytes into a type and value
18 A string containing bytes
23 Data, either a single element or a list of elements. Each element
25 TYPE_STRING: string value from the property
26 TYPE_INT: a byte-swapped integer stored as a 4-byte string
27 TYPE_BYTE: a byte stored as a single-byte string
30 strings = bytes.split('\0')
32 count = len(strings) - 1
33 if count > 0 and not strings[-1]:
34 for string in strings[:-1]:
39 if ch < ' ' or ch > '~':
46 return TYPE_STRING, strings[0]
48 return TYPE_STRING, strings[:-1]
51 return TYPE_BYTE, bytes[0]
53 return TYPE_BYTE, list(bytes)
55 for i in range(0, size, 4):
56 val.append(bytes[i:i + 4])
58 return TYPE_INT, val[0]
63 """Get an empty / zero value of the given type
66 A single value of the given type
70 elif type == TYPE_INT:
71 return struct.pack('<I', 0);
72 elif type == TYPE_STRING:
77 def fdt32_to_cpu(val):
78 """Convert a device tree cell to an integer
81 Value to convert (4-character string representing the cell value)
84 A native-endian integer value
86 return struct.unpack(">I", val)[0]