binman: Enhance the map and fdt-update output

At present the .map file produced for each image does not include the
overall image size. This is useful information.

Update the code to generate it in the .map file as well as the updated
FDT. Also fix a few comments while we are here.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/tools/binman/README b/tools/binman/README
index 5ff20f1..4b13776 100644
--- a/tools/binman/README
+++ b/tools/binman/README
@@ -584,10 +584,11 @@
 generates. This shows the offset and size of each entry. For example:
 
       Offset      Size  Name
-    00000000  00000010  section@0
-     00000000  00000004  u-boot
-    00000010  00000010  section@1
-     00000000  00000004  u-boot
+    00000000  00000028  main-section
+     00000000  00000010  section@0
+      00000000  00000004  u-boot
+     00000010  00000010  section@1
+      00000000  00000004  u-boot
 
 This shows a hierarchical image with two sections, each with a single entry. The
 offsets of the sections are absolute hex byte offsets within the image. The
diff --git a/tools/binman/bsection.py b/tools/binman/bsection.py
index d78a25e..1604b99 100644
--- a/tools/binman/bsection.py
+++ b/tools/binman/bsection.py
@@ -50,6 +50,7 @@
         import entry
         from entry import Entry
 
+        self._name = name
         self._node = node
         self._offset = 0
         self._size = None
@@ -90,11 +91,20 @@
             entry.SetPrefix(self._name_prefix)
             self._entries[node.name] = entry
 
+    def SetOffset(self, offset):
+        self._offset = offset
+
     def AddMissingProperties(self):
+        """Add new properties to the device tree as needed for this entry"""
+        for prop in ['offset', 'size']:
+            if not prop in self._node.props:
+                self._node.AddZeroProp(prop)
         for entry in self._entries.values():
             entry.AddMissingProperties()
 
     def SetCalculatedProperties(self):
+        self._node.SetInt('offset', self._offset)
+        self._node.SetInt('size', self._size)
         for entry in self._entries.values():
             entry.SetCalculatedProperties()
 
@@ -269,7 +279,7 @@
         fd.write(self.GetData())
 
     def GetData(self):
-        """Write the section to a file"""
+        """Get the contents of the section"""
         section_data = chr(self._pad_byte) * self._size
 
         for entry in self._entries.values():
@@ -335,13 +345,31 @@
             raise ValueError("%s: No such property '%s'" % (msg, prop_name))
 
     def GetEntries(self):
+        """Get the number of entries in a section
+
+        Returns:
+            Number of entries in a section
+        """
         return self._entries
 
+    def GetSize(self):
+        """Get the size of a section in bytes
+
+        This is only meaningful if the section has a pre-defined size, or the
+        entries within it have been packed, so that the size has been
+        calculated.
+
+        Returns:
+            Entry size in bytes
+        """
+        return self._size
+
     def WriteMap(self, fd, indent):
         """Write a map of the section to a .map file
 
         Args:
             fd: File to write the map to
         """
+        Entry.WriteMapLine(fd, indent, self._name, self._offset, self._size)
         for entry in self._entries.values():
-            entry.WriteMap(fd, indent)
+            entry.WriteMap(fd, indent + 1)
diff --git a/tools/binman/entry.py b/tools/binman/entry.py
index c9529d8..cb693c9 100644
--- a/tools/binman/entry.py
+++ b/tools/binman/entry.py
@@ -36,7 +36,7 @@
     Entry.
 
     Attributes:
-        section: The section containing this entry
+        section: Section object containing this entry
         node: The node that created this entry
         offset: Offset of entry within the section, None if not known yet (in
             which case it will be calculated by Pack())
@@ -72,7 +72,7 @@
         """Create a new entry for a node.
 
         Args:
-            section:  Image object containing this node
+            section:  Section object containing this node
             node:   Node object containing information about the entry to create
             etype:  Entry type to use, or None to work it out (used for tests)
 
@@ -133,7 +133,7 @@
 
     def AddMissingProperties(self):
         """Add new properties to the device tree as needed for this entry"""
-        for prop in ['offset', 'size', 'global-pos']:
+        for prop in ['offset', 'size']:
             if not prop in self._node.props:
                 self._node.AddZeroProp(prop)
 
@@ -285,6 +285,10 @@
         """
         pass
 
+    @staticmethod
+    def WriteMapLine(fd, indent, name, offset, size):
+        print('%s%08x  %08x  %s' % (' ' * indent, offset, size, name), file=fd)
+
     def WriteMap(self, fd, indent):
         """Write a map of the entry to a .map file
 
@@ -292,5 +296,4 @@
             fd: File to write the map to
             indent: Curent indent level of map (0=none, 1=one level, etc.)
         """
-        print('%s%08x  %08x  %s' % (' ' * indent, self.offset, self.size,
-                                    self.name), file=fd)
+        self.WriteMapLine(fd, indent, self.name, self.offset, self.size)
diff --git a/tools/binman/etype/section.py b/tools/binman/etype/section.py
index 5a0b0b8..1d27301 100644
--- a/tools/binman/etype/section.py
+++ b/tools/binman/etype/section.py
@@ -42,7 +42,8 @@
     def Pack(self, offset):
         """Pack all entries into the section"""
         self._section.PackEntries()
-        self.size = self._section.CheckSize()
+        self._section.SetOffset(offset)
+        self.size = self._section.GetSize()
         return super(Entry_section, self).Pack(offset)
 
     def WriteSymbols(self, section):
@@ -66,5 +67,4 @@
         Args:
             fd: File to write the map to
         """
-        super(Entry_section, self).WriteMap(fd, indent)
-        self._section.WriteMap(fd, indent + 1)
+        self._section.WriteMap(fd, indent)
diff --git a/tools/binman/ftest.py b/tools/binman/ftest.py
index 91b59f8..94e48f3 100644
--- a/tools/binman/ftest.py
+++ b/tools/binman/ftest.py
@@ -1004,27 +1004,32 @@
     def testSections(self):
         """Basic test of sections"""
         data = self._DoReadFile('55_sections.dts')
-        expected = U_BOOT_DATA + '!' * 12 + U_BOOT_DATA + 'a' * 12 + '&' * 8
+        expected = (U_BOOT_DATA + '!' * 12 + U_BOOT_DATA + 'a' * 12 +
+                    U_BOOT_DATA + '&' * 4)
         self.assertEqual(expected, data)
 
     def testMap(self):
         """Tests outputting a map of the images"""
         _, _, map_data, _ = self._DoReadFileDtb('55_sections.dts', map=True)
         self.assertEqual('''  Offset      Size  Name
-00000000  00000010  section@0
- 00000000  00000004  u-boot
-00000010  00000010  section@1
- 00000000  00000004  u-boot
+00000000  00000028  main-section
+ 00000000  00000010  section@0
+  00000000  00000004  u-boot
+ 00000010  00000010  section@1
+  00000000  00000004  u-boot
+ 00000020  00000004  section@2
+  00000000  00000004  u-boot
 ''', map_data)
 
     def testNamePrefix(self):
         """Tests that name prefixes are used"""
         _, _, map_data, _ = self._DoReadFileDtb('56_name_prefix.dts', map=True)
         self.assertEqual('''  Offset      Size  Name
-00000000  00000010  section@0
- 00000000  00000004  ro-u-boot
-00000010  00000010  section@1
- 00000000  00000004  rw-u-boot
+00000000  00000028  main-section
+ 00000000  00000010  section@0
+  00000000  00000004  ro-u-boot
+ 00000010  00000010  section@1
+  00000000  00000004  rw-u-boot
 ''', map_data)
 
     def testUnknownContents(self):
@@ -1051,6 +1056,7 @@
             with open(out_dtb_fname) as inf:
                 outf.write(inf.read())
         self.assertEqual({
+            'offset': 0,
             '_testing:offset': 32,
             '_testing:size': 1,
             'section@0/u-boot:offset': 0,
diff --git a/tools/binman/test/55_sections.dts b/tools/binman/test/55_sections.dts
index 2ada395..6b306ae 100644
--- a/tools/binman/test/55_sections.dts
+++ b/tools/binman/test/55_sections.dts
@@ -24,5 +24,9 @@
 			u-boot {
 			};
 		};
+		section@2 {
+			u-boot {
+			};
+		};
 	};
 };