diff mbox series

[05/20] dtoc: Fix widening of int to bytes

Message ID 20201003173142.3213123-6-sjg@chromium.org
State Accepted
Commit e144cafe43c8298bd41c044329857c3068cd845b
Delegated to: Simon Glass
Headers show
Series dm: Enhance of-platdata to support parent devices | expand

Commit Message

Simon Glass Oct. 3, 2020, 5:31 p.m. UTC
At present an integer is converted to bytes incorrectly. The whole 32-bit
integer is inserted as the first element of the byte array, and the other
three bytes are skipped. This was not noticed because the unit test did
not check it, and the functional test was checking for wrong values.

Update the code to handle this as a special case. Add one more test to
cover all code paths.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 test/py/tests/test_ofplatdata.py |  2 +-
 tools/dtoc/dtoc_test_simple.dts  |  1 +
 tools/dtoc/fdt.py                |  9 +++++++++
 tools/dtoc/test_dtoc.py          |  4 +++-
 tools/dtoc/test_fdt.py           | 10 ++++++++++
 5 files changed, 24 insertions(+), 2 deletions(-)

Comments

Simon Glass Oct. 27, 2020, 1:01 a.m. UTC | #1
At present an integer is converted to bytes incorrectly. The whole 32-bit
integer is inserted as the first element of the byte array, and the other
three bytes are skipped. This was not noticed because the unit test did
not check it, and the functional test was checking for wrong values.

Update the code to handle this as a special case. Add one more test to
cover all code paths.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 test/py/tests/test_ofplatdata.py |  2 +-
 tools/dtoc/dtoc_test_simple.dts  |  1 +
 tools/dtoc/fdt.py                |  9 +++++++++
 tools/dtoc/test_dtoc.py          |  4 +++-
 tools/dtoc/test_fdt.py           | 10 ++++++++++
 5 files changed, 24 insertions(+), 2 deletions(-)

Applied to u-boot-dm, thanks!
diff mbox series

Patch

diff --git a/test/py/tests/test_ofplatdata.py b/test/py/tests/test_ofplatdata.py
index 263334b0743..13154935b9b 100644
--- a/test/py/tests/test_ofplatdata.py
+++ b/test/py/tests/test_ofplatdata.py
@@ -20,7 +20,7 @@  byte 08
 bytearray 01 23 34
 int 3
 intarray 5 0 0 0
-longbytearray 09 00 00 00 00 00 00 00 00
+longbytearray 09 0a 0b 0c 00 00 00 00 00
 string message2
 stringarray "another" "multi-word" "message"
 of-platdata probe:
diff --git a/tools/dtoc/dtoc_test_simple.dts b/tools/dtoc/dtoc_test_simple.dts
index 11bfc4c47aa..fd168cb5931 100644
--- a/tools/dtoc/dtoc_test_simple.dts
+++ b/tools/dtoc/dtoc_test_simple.dts
@@ -41,6 +41,7 @@ 
 		u-boot,dm-pre-reloc;
 		compatible = "sandbox,spl-test";
 		stringarray = "one";
+		longbytearray = [09 0a 0b 0c 0d 0e 0f 10];
 	};
 
 	spl-test4 {
diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py
index d058c59e927..03b86773d5f 100644
--- a/tools/dtoc/fdt.py
+++ b/tools/dtoc/fdt.py
@@ -129,6 +129,15 @@  class Prop:
         specific.
         """
         if newprop.type < self.type:
+            # Special handling to convert an int into bytes
+            if self.type == TYPE_INT and newprop.type == TYPE_BYTE:
+                if type(self.value) == list:
+                    new_value = []
+                    for val in self.value:
+                        new_value += [tools.ToChar(by) for by in val]
+                else:
+                    new_value = [tools.ToChar(by) for by in self.value]
+                self.value = new_value
             self.type = newprop.type
 
         if type(newprop.value) == list and type(self.value) != list:
diff --git a/tools/dtoc/test_dtoc.py b/tools/dtoc/test_dtoc.py
index 857a98e435f..8dcac91ee70 100755
--- a/tools/dtoc/test_dtoc.py
+++ b/tools/dtoc/test_dtoc.py
@@ -255,7 +255,7 @@  static struct dtd_sandbox_spl_test dtv_spl_test2 = {
 \t.byteval\t\t= 0x8,
 \t.intarray\t\t= {0x5, 0x0, 0x0, 0x0},
 \t.intval\t\t\t= 0x3,
-\t.longbytearray\t\t= {0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
+\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0x0, 0x0, 0x0, 0x0,
 \t\t0x0},
 \t.stringarray\t\t= {"another", "multi-word", "message"},
 \t.stringval\t\t= "message2",
@@ -268,6 +268,8 @@  U_BOOT_DEVICE(spl_test2) = {
 
 /* Node /spl-test3 index 4 */
 static struct dtd_sandbox_spl_test dtv_spl_test3 = {
+\t.longbytearray\t\t= {0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
+\t\t0x0},
 \t.stringarray\t\t= {"one", "", ""},
 };
 U_BOOT_DEVICE(spl_test3) = {
diff --git a/tools/dtoc/test_fdt.py b/tools/dtoc/test_fdt.py
index b4f9b7f498f..cfe3e04c7ad 100755
--- a/tools/dtoc/test_fdt.py
+++ b/tools/dtoc/test_fdt.py
@@ -298,6 +298,7 @@  class TestProp(unittest.TestCase):
     def testWiden(self):
         """Test widening of values"""
         node2 = self.dtb.GetNode('/spl-test2')
+        node3 = self.dtb.GetNode('/spl-test3')
         prop = self.node.props['intval']
 
         # No action
@@ -316,11 +317,20 @@  class TestProp(unittest.TestCase):
         # byte array, it should turn into an array.
         prop = self.node.props['longbytearray']
         prop2 = node2.props['longbytearray']
+        prop3 = node3.props['longbytearray']
         self.assertFalse(isinstance(prop2.value, list))
         self.assertEqual(4, len(prop2.value))
+        self.assertEqual(b'\x09\x0a\x0b\x0c', prop2.value)
         prop2.Widen(prop)
         self.assertTrue(isinstance(prop2.value, list))
         self.assertEqual(9, len(prop2.value))
+        self.assertEqual(['\x09', '\x0a', '\x0b', '\x0c', '\0',
+                          '\0', '\0', '\0', '\0'], prop2.value)
+        prop3.Widen(prop)
+        self.assertTrue(isinstance(prop3.value, list))
+        self.assertEqual(9, len(prop3.value))
+        self.assertEqual(['\x09', '\x0a', '\x0b', '\x0c', '\x0d',
+                          '\x0e', '\x0f', '\x10', '\0'], prop3.value)
 
         # Similarly for a string array
         prop = self.node.props['stringval']