BmapCreate: generate bmap file checksum
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Mon, 6 May 2013 14:25:39 +0000 (17:25 +0300)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Tue, 7 May 2013 08:42:43 +0000 (11:42 +0300)
I got a bug report recently and the investigation showed that it is caused by
corrupted bmap file. Once the user re-downloaded the bmap file, the problem
was solved.

This patch tries to improve robustness by protecting the bmap file with SHA1
checksum. At the very end we calculate the SHA1 checksum of the entire bmap
file with the in-file SHA1 value = all zeroes, and put the result to the bmap
file.

In order to verify the checksum, we will have to substitute the SHA1 checksum
with all zeroes again and calculate SHA1 of the file.

Change-Id: Ia8f109c8238a0e5e2e609f53255a699b35d72cde
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
bmaptools/BmapCreate.py

index e85c2e3..531da7e 100644 (file)
@@ -129,6 +129,7 @@ class BmapCreate:
 
         self._mapped_count_pos1 = None
         self._mapped_count_pos2 = None
+        self._sha1_pos = None
 
         self._f_image_needs_close = False
         self._f_bmap_needs_close = False
@@ -186,6 +187,14 @@ class BmapCreate:
         xml  = "%s </MappedBlocksCount>\n\n" % mapped_count
 
         # pylint: disable=C0301
+        xml += "    <!-- The checksum of this bmap file. When it is calculated, the value of\n"
+        xml += "         the SHA1 checksum has be zeoro (40 ASCII \"0\" symbols). -->\n"
+        xml += "    <BmapFileSHA1> "
+
+        self._f_bmap.write(xml)
+        self._sha1_pos = self._f_bmap.tell()
+
+        xml = "0" * 40 + " </BmapFileSHA1>\n\n"
         xml += "    <!-- The block map which consists of elements which may either be a\n"
         xml += "         range of blocks or a single block. The 'sha1' attribute (if present)\n"
         xml += "         is the SHA1 checksum of this blocks range. -->\n"
@@ -211,6 +220,11 @@ class BmapCreate:
         self._f_bmap.seek(self._mapped_count_pos2)
         self._f_bmap.write("%u" % self.mapped_cnt)
 
+        self._f_bmap.seek(0)
+        sha1 = hashlib.sha1(self._f_bmap.read()).hexdigest()
+        self._f_bmap.seek(self._sha1_pos)
+        self._f_bmap.write("%s" % sha1)
+
     def _calculate_sha1(self, first, last):
         """ A helper function which calculates SHA1 checksum for the range of
         blocks of the image file: from block 'first' to block 'last'. """