meta-phosphor: introduce static non-rootfs image type

Define a new image type 'static-norootfs' that takes the entire
rootfs and places it as the initramfs for the kernel's FIT image,
so that the BMC is entirely running from a ramdisk.

Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I0163f5be0a1b176e0db5f89b85f34e4feae8a588
diff --git a/meta-phosphor/classes/image_types_phosphor.bbclass b/meta-phosphor/classes/image_types_phosphor.bbclass
index a4a05cf..6612bd6 100644
--- a/meta-phosphor/classes/image_types_phosphor.bbclass
+++ b/meta-phosphor/classes/image_types_phosphor.bbclass
@@ -2,6 +2,10 @@
 
 inherit image_version
 
+FIT_IMAGE_INHERIT=""
+FIT_IMAGE_INHERIT:df-obmc-static-norootfs = "fit-image"
+inherit ${FIT_IMAGE_INHERIT}
+
 # Phosphor image types
 #
 # Phosphor OpenBMC supports a fixed partition mtd layout,
@@ -13,21 +17,32 @@
 FLASH_KERNEL_IMAGE:df-obmc-ubi-fs ?= "fitImage-${MACHINE}.bin"
 
 IMAGE_BASETYPE ?= "squashfs-xz"
+IMAGE_BASETYPE:df-obmc-static-norootfs ?= "cpio"
 OVERLAY_BASETYPE ?= "jffs2"
 FLASH_UBI_BASETYPE ?= "${IMAGE_BASETYPE}"
 FLASH_UBI_OVERLAY_BASETYPE ?= "ubifs"
 FLASH_EXT4_BASETYPE ?= "ext4"
 FLASH_EXT4_OVERLAY_BASETYPE ?= "ext4"
 
-IMAGE_TYPES += "mtd-static mtd-static-alltar mtd-static-tar mtd-ubi mtd-ubi-tar mmc-ext4-tar"
+PHOSPHOR_IMAGE_TYPES += " \
+    mtd-static \
+    mtd-static-alltar \
+    mtd-static-tar \
+    mtd-static-norootfs \
+    mtd-ubi \
+    mtd-ubi-tar \
+    mmc-ext4-tar \
+"
+IMAGE_TYPES += "${PHOSPHOR_IMAGE_TYPES}"
+IMAGE_TYPES_MASKED += "${PHOSPHOR_IMAGE_TYPES}"
 
 IMAGE_TYPEDEP:mtd-static = "${IMAGE_BASETYPE}"
 IMAGE_TYPEDEP:mtd-static-tar = "${IMAGE_BASETYPE}"
 IMAGE_TYPEDEP:mtd-static-alltar = "mtd-static"
+IMAGE_TYPEDEP:mtd-static-norootfs = "${IMAGE_BASETYPE}"
 IMAGE_TYPEDEP:mtd-ubi = "${FLASH_UBI_BASETYPE}"
 IMAGE_TYPEDEP:mtd-ubi-tar = "${FLASH_UBI_BASETYPE}"
 IMAGE_TYPEDEP:mmc-ext4-tar = "${FLASH_EXT4_BASETYPE}"
-IMAGE_TYPES_MASKED += "mtd-static mtd-static-alltar mtd-static-tar mtd-ubi mtd-ubi-tar mmc-ext4-tar"
 
 # Flash characteristics in KB unless otherwise noted
 DISTROOVERRIDES .= ":flash-${FLASH_SIZE}"
@@ -421,6 +436,59 @@
         "
 do_generate_static_tar[vardepsexclude] = "DATETIME"
 
+python do_generate_static_norootfs() {
+    import subprocess
+
+    nor_image_basename = '%s.static.mtd' % d.getVar('IMAGE_NAME', True)
+    nor_image = os.path.join(d.getVar('IMGDEPLOYDIR', True), nor_image_basename)
+
+    def _append_image(imgpath, start_kb, finish_kb):
+        imgsize = os.path.getsize(imgpath)
+        maxsize = (finish_kb - start_kb) * 1024
+        bb.debug(1, 'Considering file size=' + str(imgsize) + ' name=' + imgpath)
+        bb.debug(1, 'Spanning start=' + str(start_kb) + 'K end=' + str(finish_kb) + 'K')
+        bb.debug(1, 'Compare needed=' + str(imgsize) + ' available=' + str(maxsize) + ' margin=' + str(maxsize - imgsize))
+        if imgsize > maxsize:
+            bb.fatal("Image '%s' is too large!" % imgpath)
+
+        subprocess.check_call(['dd', 'bs=1k', 'conv=notrunc',
+                               'seek=%d' % start_kb,
+                               'if=%s' % imgpath,
+                               'of=%s' % nor_image])
+    uboot_offset = int(d.getVar('FLASH_UBOOT_OFFSET', True))
+
+    spl_binary = d.getVar('SPL_BINARY', True)
+    if spl_binary:
+        _append_image(os.path.join(d.getVar('DEPLOY_DIR_IMAGE', True),
+                                   'u-boot-spl.%s' % d.getVar('UBOOT_SUFFIX',True)),
+                      int(d.getVar('FLASH_UBOOT_OFFSET', True)),
+                      int(d.getVar('FLASH_UBOOT_SPL_SIZE', True)))
+        uboot_offset += int(d.getVar('FLASH_UBOOT_SPL_SIZE', True))
+
+    _append_image(os.path.join(d.getVar('DEPLOY_DIR_IMAGE', True),
+                               'u-boot.%s' % d.getVar('UBOOT_SUFFIX',True)),
+                  uboot_offset,
+                  int(d.getVar('FLASH_KERNEL_OFFSET', True)))
+
+    _append_image(os.path.join(d.getVar('IMGDEPLOYDIR', True),
+                               '%s.cpio.%s.fitImage' % (
+                                    d.getVar('IMAGE_LINK_NAME', True),
+                                    d.getVar('INITRAMFS_CTYPE', True))),
+                  int(d.getVar('FLASH_KERNEL_OFFSET', True)),
+                  int(d.getVar('FLASH_RWFS_OFFSET', True)))
+
+    flash_symlink = os.path.join(
+                        d.getVar('IMGDEPLOYDIR', True),
+                        'flash-%s' % d.getVar('MACHINE', True))
+    if os.path.exists(flash_symlink):
+        os.remove(flash_symlink)
+    os.symlink(nor_image_basename, flash_symlink)
+}
+do_generate_static_norootfs[depends] += " \
+        ${PN}:do_image_${@d.getVar('IMAGE_BASETYPE', True).replace('-', '_')} \
+        u-boot:do_deploy \
+        "
+
 do_generate_ubi_tar() {
 	ln -sf ${S}/MANIFEST MANIFEST
 	ln -sf ${S}/publickey publickey
@@ -561,6 +629,12 @@
                 'do_image_complete',
                 'do_generate_rwfs_static do_generate_phosphor_manifest', d)
 
+    if 'mtd-static-norootfs' in types:
+        bb.build.addtask(
+                'do_generate_static_norootfs',
+                'do_image_complete',
+                'do_image_cpio', d)
+
     if 'mtd-ubi' in types:
         bb.build.addtask(
                 'do_generate_ubi',
diff --git a/meta-phosphor/conf/distro/include/phosphor-static-norootfs.inc b/meta-phosphor/conf/distro/include/phosphor-static-norootfs.inc
new file mode 100644
index 0000000..f7908ee
--- /dev/null
+++ b/meta-phosphor/conf/distro/include/phosphor-static-norootfs.inc
@@ -0,0 +1,2 @@
+DISTRO_FEATURES += "obmc-static-norootfs"
+DISTROOVERRIDES .= ":df-obmc-static-norootfs"
diff --git a/meta-phosphor/conf/machine/include/image-type/static-norootfs.inc b/meta-phosphor/conf/machine/include/image-type/static-norootfs.inc
new file mode 100644
index 0000000..02574da
--- /dev/null
+++ b/meta-phosphor/conf/machine/include/image-type/static-norootfs.inc
@@ -0,0 +1,10 @@
+INITRAMFS_CTYPE="zst"
+
+IMAGE_FSTYPES += "cpio.${INITRAMFS_CTYPE}.fitImage mtd-static-norootfs"
+
+# We don't want to build an initial kernel-based FIT image because the
+# initramfs is going to be the eventual rootfs from this image and not
+# a smaller initramfs.
+INITRAMFS_IMAGE = ""
+KERNEL_IMAGETYPE = "${ARCH_DEFAULT_KERNELIMAGETYPE}"
+KERNEL_IMAGETYPES = "${ARCH_DEFAULT_KERNELIMAGETYPE}"
diff --git a/meta-phosphor/conf/machine/include/obmc-bsp-common.inc b/meta-phosphor/conf/machine/include/obmc-bsp-common.inc
index 8a38343..e57bc64 100644
--- a/meta-phosphor/conf/machine/include/obmc-bsp-common.inc
+++ b/meta-phosphor/conf/machine/include/obmc-bsp-common.inc
@@ -6,6 +6,7 @@
 KERNEL_IMAGETYPES ?= "fitImage vmlinux"
 
 OBMC_IMAGE_INCLUDE = "static"
+OBMC_IMAGE_INCLUDE:df-obmc-static-norootfs = "static-norootfs"
 OBMC_IMAGE_INCLUDE:df-obmc-ubi-fs = "ubi-fs"
 OBMC_IMAGE_INCLUDE:df-phosphor-mmc = "mmc"