Auto generate init_boot image

This change adds support to generate init_boot image in bootimg module
type, and utilizes it to conditionally auto generate the init_boot image
in filesystem_creator.

Also, fix the test rule for vendor_boot diff test.

Test: CI
Bug: 377563630
Change-Id: Ia197df959567d6554a1b88fe9319a7de5ac71afe
diff --git a/filesystem/bootimg.go b/filesystem/bootimg.go
index ab91eb4..40b9ce6 100644
--- a/filesystem/bootimg.go
+++ b/filesystem/bootimg.go
@@ -60,6 +60,9 @@
 	// https://source.android.com/devices/bootloader/partitions/vendor-boot-partitions
 	Vendor_boot *bool
 
+	// Determines if this image is for the init_boot partition. Default is false.
+	Init_boot *bool
+
 	// Optional kernel commandline arguments
 	Cmdline []string `android:"arch_variant"`
 
@@ -113,7 +116,13 @@
 
 func (b *bootimg) GenerateAndroidBuildActions(ctx android.ModuleContext) {
 	vendor := proptools.Bool(b.properties.Vendor_boot)
-	unsignedOutput := b.buildBootImage(ctx, vendor)
+	init := proptools.Bool(b.properties.Init_boot)
+
+	if vendor && init {
+		ctx.ModuleErrorf("vendor_boot and init_boot cannot be both set to true")
+	}
+
+	unsignedOutput := b.buildBootImage(ctx, vendor, init)
 
 	output := unsignedOutput
 	if proptools.Bool(b.properties.Use_avb) {
@@ -127,7 +136,7 @@
 	b.output = output
 }
 
-func (b *bootimg) buildBootImage(ctx android.ModuleContext, vendor bool) android.Path {
+func (b *bootimg) buildBootImage(ctx android.ModuleContext, vendor bool, init bool) android.Path {
 	output := android.PathForModuleOut(ctx, "unsigned", b.installFileName())
 
 	builder := android.NewRuleBuilder(pctx, ctx)
@@ -138,7 +147,9 @@
 		ctx.PropertyErrorf("kernel_prebuilt", "vendor_boot partition can't have kernel")
 		return output
 	}
-	if !vendor && kernel == "" {
+
+	buildingBoot := !vendor && !init
+	if buildingBoot && kernel == "" {
 		ctx.PropertyErrorf("kernel_prebuilt", "boot partition must have kernel")
 		return output
 	}
@@ -146,6 +157,7 @@
 		cmd.FlagWithInput("--kernel ", android.PathForModuleSrc(ctx, kernel))
 	}
 
+	// These arguments are passed for boot.img and init_boot.img generation
 	if !vendor {
 		cmd.FlagWithArg("--os_version ", ctx.Config().PlatformVersionLastStable())
 		cmd.FlagWithArg("--os_patch_level ", ctx.Config().PlatformSecurityPatch())
@@ -210,6 +222,7 @@
 		cmd.FlagWithInput("--vendor_bootconfig ", android.PathForModuleSrc(ctx, bootconfig))
 	}
 
+	// Output flag for boot.img and init_boot.img
 	flag := "--output "
 	if vendor {
 		flag = "--vendor_boot "
diff --git a/fsgen/boot_imgs.go b/fsgen/boot_imgs.go
index 9eaec73..a42017e 100644
--- a/fsgen/boot_imgs.go
+++ b/fsgen/boot_imgs.go
@@ -74,6 +74,27 @@
 	return true
 }
 
+func createInitBootImage(ctx android.LoadHookContext) bool {
+	partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
+
+	bootImageName := generatedModuleNameForPartition(ctx.Config(), "init_boot")
+
+	ctx.CreateModule(
+		filesystem.BootimgFactory,
+		&filesystem.BootimgProperties{
+			Init_boot:      proptools.BoolPtr(true),
+			Ramdisk_module: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "ramdisk")),
+			Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion),
+		},
+		&struct {
+			Name *string
+		}{
+			Name: proptools.StringPtr(bootImageName),
+		},
+	)
+	return true
+}
+
 // Returns the equivalent of the BUILDING_BOOT_IMAGE variable in make. Derived from this logic:
 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=458;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0
 func buildingBootImage(partitionVars android.PartitionVariables) bool {
@@ -112,6 +133,23 @@
 	return false
 }
 
+// Derived from: https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=480;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0
+func buildingInitBootImage(partitionVars android.PartitionVariables) bool {
+	if !partitionVars.ProductBuildInitBootImage {
+		if partitionVars.BoardUsesRecoveryAsBoot || len(partitionVars.BoardPrebuiltInitBootimage) > 0 {
+			return false
+		} else if len(partitionVars.BoardInitBootimagePartitionSize) > 0 {
+			return true
+		}
+	} else {
+		if partitionVars.BoardUsesRecoveryAsBoot {
+			panic("PRODUCT_BUILD_INIT_BOOT_IMAGE is true, but so is BOARD_USES_RECOVERY_AS_BOOT. Use only one option.")
+		}
+		return true
+	}
+	return false
+}
+
 func boardBootHeaderVersion(partitionVars android.PartitionVariables) (int, bool) {
 	if len(partitionVars.BoardBootHeaderVersion) == 0 {
 		return 0, false
diff --git a/fsgen/filesystem_creator.go b/fsgen/filesystem_creator.go
index a019087..6bfb097 100644
--- a/fsgen/filesystem_creator.go
+++ b/fsgen/filesystem_creator.go
@@ -50,6 +50,7 @@
 
 	Boot_image        string `blueprint:"mutated" android:"path_device_first"`
 	Vendor_boot_image string `blueprint:"mutated" android:"path_device_first"`
+	Init_boot_image   string `blueprint:"mutated" android:"path_device_first"`
 }
 
 type filesystemCreator struct {
@@ -137,6 +138,13 @@
 			f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "vendor_boot")
 		}
 	}
+	if buildingInitBootImage(partitionVars) {
+		if createInitBootImage(ctx) {
+			f.properties.Init_boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "init_boot")
+		} else {
+			f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "init_boot")
+		}
+	}
 
 	for _, x := range createVbmetaPartitions(ctx, finalSoongGeneratedPartitions) {
 		f.properties.Vbmeta_module_names = append(f.properties.Vbmeta_module_names, x.moduleName)
@@ -737,12 +745,20 @@
 	}
 	if f.properties.Vendor_boot_image != "" {
 		diffTestFile := android.PathForModuleOut(ctx, "vendor_boot_diff_test.txt")
-		soongBootImg := android.PathForModuleSrc(ctx, f.properties.Boot_image)
+		soongBootImg := android.PathForModuleSrc(ctx, f.properties.Vendor_boot_image)
 		makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/vendor_boot.img", ctx.Config().DeviceName()))
 		createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
 		diffTestFiles = append(diffTestFiles, diffTestFile)
 		ctx.Phony("soong_generated_vendor_boot_filesystem_test", diffTestFile)
 	}
+	if f.properties.Init_boot_image != "" {
+		diffTestFile := android.PathForModuleOut(ctx, "init_boot_diff_test.txt")
+		soongBootImg := android.PathForModuleSrc(ctx, f.properties.Init_boot_image)
+		makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/init_boot.img", ctx.Config().DeviceName()))
+		createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
+		diffTestFiles = append(diffTestFiles, diffTestFile)
+		ctx.Phony("soong_generated_init_boot_filesystem_test", diffTestFile)
+	}
 	ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
 }