pack_firmware: Calculate offset and size of bios section for ifdtool

Currently, the assumptions while creating final image for Intel systems
with IFD region are:
1. DTS region size equals the non-BIOS region size in the descriptor.
2. The image contains non-BIOS region(IFD) at the start followed by
BIOS region till the end of the image.

These assumptions are not true for Apollolake platform since it
contains non-BIOS region both at the start as well as at the end of
the image. Thus, ProduceFinalImage in pack_firmware needs to calculate
the correct size of BIOS region being passed into the ifdtool from the
descriptor and not from the dts file.

CQ-DEPEND=CL:347986
BUG=chrome-os-partner:53689
BRANCH=None
TEST="emerge-reef chromeos-bootimage" succeeds. "cbfstool print
image.bin" prints out components correctly.

Change-Id: Id2e8fc8ca425c24ee63f2baf4d0f66ebf6a7b29b
Signed-off-by: Furquan Shaikh <furquan@google.com>
Reviewed-on: https://chromium-review.googlesource.com/348020
Commit-Ready: Furquan Shaikh <furquan@chromium.org>
Tested-by: Furquan Shaikh <furquan@chromium.org>
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
diff --git a/host/lib/pack_firmware.py b/host/lib/pack_firmware.py
index 904a8ff..b72fb7d 100644
--- a/host/lib/pack_firmware.py
+++ b/host/lib/pack_firmware.py
@@ -321,7 +321,24 @@
     # We can assume that the ifd section is at the start of the image.
     if self.offset != 0:
       raise ConfigError('IFD section must be at offset 0 in the image')
-    data = data[self.size:]
+
+    # Calculate start and size of BIOS region based off the IFD descriptor and
+    # not the size in dts node.
+    ifd_layout_tmp = os.path.join(tmpdir, 'ifd-layout-tmp')
+    args = ['-f%s' % ifd_layout_tmp, tools.Filename(self.pack.props['skeleton'])]
+    tools.Run('ifdtool', args)
+    fd = open(ifd_layout_tmp)
+    layout = fd.readlines()
+    for line in layout:
+        line = line.rstrip()
+        if line.find("bios") != -1:
+            addr_range = line.split(' ')[0]
+            start = int(addr_range.split(':')[0], 16)
+            end = int(addr_range.split(':')[1], 16)
+
+    fd.close()
+
+    data = data[start:end+1]
     input_fname = os.path.join(tmpdir, 'ifd-input.bin')
     tools.WriteFile(input_fname, data)
     ifd_output = os.path.join(tmpdir, 'image.ifd')