image_types: Add BUILD_ID to MANIFEST

Add the BUILD_ID value to the MANIFEST so that it can be used alongside
the VERSION value to generate a version id during firmware updates.

Add a function to read BUILD_ID from the os-release file instead of
reading it from a variable because the BUILD_ID value could be set via a
os_release.bbappend file instead of a .conf file.

Tested: Verified the BUILD_ID value was added to the MANIFEST by
default, and when BUILD_ID was specified in a .conf file, and on a
os-release.bbappend. Ex:
$ cat MANIFEST
purpose=xyz.openbmc_project.Software.Version.VersionPurpose.BMC
version=2.11.0-dev-566-g263df7f852
BuildId=20211025151654
ExtendedVersion=
KeyType=OpenBMC
HashType=RSA-SHA256
MachineName=p10bmc

Change-Id: I3b7beaccbbd47d8820d499180ccdf021b004cf85
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/meta-phosphor/classes/image_types_phosphor.bbclass b/meta-phosphor/classes/image_types_phosphor.bbclass
index be3bbdf..64fec00 100644
--- a/meta-phosphor/classes/image_types_phosphor.bbclass
+++ b/meta-phosphor/classes/image_types_phosphor.bbclass
@@ -502,11 +502,13 @@
 python do_generate_phosphor_manifest() {
     purpose = d.getVar('VERSION_PURPOSE', True)
     version = do_get_version(d)
+    build_id = do_get_buildID(d)
     target_machine = d.getVar('MACHINE', True)
     extended_version = (d.getVar('EXTENDED_VERSION', True) or "")
     with open('MANIFEST', 'w') as fd:
         fd.write('purpose={}\n'.format(purpose))
         fd.write('version={}\n'.format(version.strip('"')))
+        fd.write('BuildId={}\n'.format(build_id.strip('"')))
         fd.write('ExtendedVersion={}\n'.format(extended_version))
         fd.write('KeyType={}\n'.format(get_pubkey_type(d)))
         fd.write('HashType=RSA-SHA256\n')
diff --git a/meta-phosphor/classes/image_version.bbclass b/meta-phosphor/classes/image_version.bbclass
index bf3ca93..17f324e 100644
--- a/meta-phosphor/classes/image_version.bbclass
+++ b/meta-phosphor/classes/image_version.bbclass
@@ -26,3 +26,21 @@
     version = version.strip('"')
     version_id = (hashlib.sha512(version.encode('utf-8')).hexdigest())[:8]
     return version_id
+
+def do_get_buildID(d):
+    import configparser
+    import io
+    path = d.getVar('STAGING_DIR_TARGET', True) + d.getVar('sysconfdir', True)
+    path = os.path.join(path, 'os-release')
+    parser = configparser.ConfigParser(strict=False)
+    parser.optionxform = str
+    build_id = ''
+    try:
+        with open(path, 'r') as fd:
+            buf = '[root]\n' + fd.read()
+            fd = io.StringIO(buf)
+            parser.readfp(fd)
+            build_id = parser['root']['BUILD_ID']
+    except:
+        pass
+    return build_id