Calculate the version ID of BMC UBI volumes during build time.

- When the new ubi layout is flashed onto the BMC, the volumes
  were named kernel-0 and rofs-0 by default. This tends to be
  misleading as we can't differentiate between two different BMC
  versions.
- Now the ubi volumes will be named kernel-<versionID> and
  rofs-<versionID> calculated by getting the hash(SHA-512) of
  the version and taking the first 8 characters.
- The Uboot env needs to be updated to point to the correct
  kernelname which has now changed from kernel-0 to
  kernel-<versionID> calculated by getting the HASH(SHA-512)
  of the version and taking the first 8 characters.

Resolves openbmc/openbmc#2323

Change-Id: I258d165b399d1ff59ea86f410006f6d03fe13a2e
Signed-off-by: Saqib Khan <khansa@us.ibm.com>
diff --git a/classes/image_types_phosphor.bbclass b/classes/image_types_phosphor.bbclass
index 8d19c80..5599332 100644
--- a/classes/image_types_phosphor.bbclass
+++ b/classes/image_types_phosphor.bbclass
@@ -10,6 +10,7 @@
 # Inherit u-boot classes if legacy uboot images are in use.
 IMAGE_TYPE_uboot = '${@build_uboot(d)}'
 inherit ${IMAGE_TYPE_uboot}
+inherit image_version
 
 # Phosphor image types
 #
@@ -139,16 +140,27 @@
 	fi
 }
 
-do_generate_ubi() {
+python do_generate_ubi() {
+        version_id = do_get_versionID(d)
+        d.setVar('VERSION_ID', version_id)
+        bb.build.exec_func("do_make_ubi", d)
+}
+do_generate_ubi[dirs] = "${S}/ubi"
+do_generate_ubi[depends] += " \
+        ${PN}:do_image_${@d.getVar('FLASH_UBI_BASETYPE', True).replace('-', '_')} \
+        virtual/kernel:do_deploy \
+        u-boot:do_populate_sysroot \
+        mtd-utils-native:do_populate_sysroot \
+        "
+
+do_make_ubi() {
 	cfg=ubinize-${IMAGE_NAME}.cfg
-
 	rm -f $cfg ubi-img
-
 	# Construct the ubinize config file
-	add_volume $cfg 0 static kernel-0 \
+	add_volume $cfg 0 static kernel-${VERSION_ID} \
 		${DEPLOY_DIR_IMAGE}/${FLASH_KERNEL_IMAGE}
 
-	add_volume $cfg 1 static rofs-0 \
+	add_volume $cfg 1 static rofs-${VERSION_ID} \
 		${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.${FLASH_UBI_BASETYPE}
 
 	add_volume $cfg 2 dynamic rwfs rwfs.${FLASH_UBI_OVERLAY_BASETYPE} ${FLASH_UBI_RWFS_TXT_SIZE}
@@ -168,8 +180,8 @@
 	cd ${IMGDEPLOYDIR}
 	ln -sf ${IMAGE_NAME}.ubi.mtd ${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.ubi.mtd
 }
-do_generate_ubi[dirs] = "${S}/ubi"
-do_generate_ubi[depends] += " \
+do_make_ubi[dirs] = "${S}/ubi"
+do_make_ubi[depends] += " \
         ${PN}:do_image_${@d.getVar('FLASH_UBI_BASETYPE', True).replace('-', '_')} \
         virtual/kernel:do_deploy \
         u-boot:do_populate_sysroot \
@@ -281,19 +293,7 @@
         "
 
 python do_generate_phosphor_manifest() {
-    import configparser
-    import io
-    path = d.getVar('STAGING_DIR_HOST', True) + d.getVar('sysconfdir', True)
-    path = os.path.join(path, 'os-release')
-    parser = configparser.SafeConfigParser(strict=False)
-    parser.optionxform = str
-    version = ''
-    with open(path, 'r') as fd:
-        buf = '[root]\n' + fd.read()
-        fd = io.StringIO(buf)
-        parser.readfp(fd)
-        version = parser['root']['VERSION_ID']
-
+    version = do_get_version(d)
     with open('MANIFEST', 'w') as fd:
         fd.write('purpose=xyz.openbmc_project.Software.Version.VersionPurpose.BMC\n')
         fd.write('version={}\n'.format(version.strip('"')))
diff --git a/classes/image_version.bbclass b/classes/image_version.bbclass
new file mode 100644
index 0000000..f9abb5f
--- /dev/null
+++ b/classes/image_version.bbclass
@@ -0,0 +1,26 @@
+# Base image version class extension
+
+def do_get_version(d):
+    import configparser
+    import io
+    path = d.getVar('STAGING_DIR_HOST', True) + d.getVar('sysconfdir', True)
+    path = os.path.join(path, 'os-release')
+    parser = configparser.SafeConfigParser(strict=False)
+    parser.optionxform = str
+    version = ''
+    try:
+        with open(path, 'r') as fd:
+            buf = '[root]\n' + fd.read()
+            fd = io.StringIO(buf)
+            parser.readfp(fd)
+            version = parser['root']['VERSION_ID']
+    except:
+        pass
+    return version
+
+def do_get_versionID(d):
+    import hashlib
+    version = do_get_version(d)
+    version = version.strip('"')
+    version_id = (hashlib.sha512(version.encode('utf-8')).hexdigest())[:8]
+    return version_id
diff --git a/common/recipes-bsp/u-boot/u-boot.inc b/common/recipes-bsp/u-boot/u-boot.inc
index 6eac533..5cc320f 100644
--- a/common/recipes-bsp/u-boot/u-boot.inc
+++ b/common/recipes-bsp/u-boot/u-boot.inc
@@ -4,10 +4,26 @@
 UBRANCH = "v2016.07-aspeed-openbmc"
 SRC_URI = "git://git@github.com/openbmc/u-boot.git;branch=${UBRANCH};protocol=https"
 
+inherit image_version
+
 SRC_URI += "file://0001-configs-ast-Add-redundnant-env.patch"
 
 SRC_URI += "${@bb.utils.contains('MACHINE_FEATURES', 'obmc-ubi-fs', \
     'file://0001-config-ast-common-hack-bootopts.patch \
      file://0004-config-ast-common-ubi-bootops.patch', '', d)}"
 
+python do_configure () {
+    if ((d.getVar("MACHINE_FEATURES", True)) == "obmc-ubi-fs"):
+        version_id=do_get_versionID(d)
+        d.setVar('VERSION_ID', version_id)
+        bb.build.exec_func("patch_uboot", d)
+}
+
+patch_uboot () {
+	sed -i "s/kernel-0/kernel-${VERSION_ID}/g" \
+	${S}/patches/0004-config-ast-common-ubi-bootops.patch &> /dev/null
+	sed -i "s/kernel-0/kernel-${VERSION_ID}/g" \
+	${S}/include/configs/ast-common.h &> /dev/null
+}
+
 PV = "v2016.07+git${SRCPV}"