blob: 8b7b94cc5f30534f841b5ef16be996609decc2c5 [file] [log] [blame]
Cole Faustf2a6e8b2024-11-14 10:54:48 -08001package fsgen
2
3import (
4 "android/soong/android"
5 "android/soong/filesystem"
Cole Faust24938e22024-11-18 14:01:58 -08006 "fmt"
Cole Faustf2a6e8b2024-11-14 10:54:48 -08007 "path/filepath"
Cole Faust24938e22024-11-18 14:01:58 -08008 "strconv"
Cole Faustf2a6e8b2024-11-14 10:54:48 -08009
10 "github.com/google/blueprint/proptools"
11)
12
13func createBootImage(ctx android.LoadHookContext) bool {
14 partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
15
16 if partitionVariables.TargetKernelPath == "" {
17 // There are potentially code paths that don't set TARGET_KERNEL_PATH
18 return false
19 }
20
21 kernelDir := filepath.Dir(partitionVariables.TargetKernelPath)
22 kernelBase := filepath.Base(partitionVariables.TargetKernelPath)
23 kernelFilegroupName := generatedModuleName(ctx.Config(), "kernel")
24
25 ctx.CreateModuleInDirectory(
26 android.FileGroupFactory,
27 kernelDir,
28 &struct {
29 Name *string
30 Srcs []string
31 Visibility []string
32 }{
33 Name: proptools.StringPtr(kernelFilegroupName),
34 Srcs: []string{kernelBase},
35 Visibility: []string{"//visibility:public"},
36 },
37 )
38
39 bootImageName := generatedModuleNameForPartition(ctx.Config(), "boot")
40
41 ctx.CreateModule(
42 filesystem.BootimgFactory,
43 &filesystem.BootimgProperties{
44 Kernel_prebuilt: proptools.StringPtr(":" + kernelFilegroupName),
Cole Faustf2a6e8b2024-11-14 10:54:48 -080045 Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion),
46 },
47 &struct {
48 Name *string
49 }{
50 Name: proptools.StringPtr(bootImageName),
51 },
52 )
53 return true
54}
55
Cole Faust24938e22024-11-18 14:01:58 -080056func createVendorBootImage(ctx android.LoadHookContext) bool {
57 partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
58
59 bootImageName := generatedModuleNameForPartition(ctx.Config(), "vendor_boot")
60
61 ctx.CreateModule(
62 filesystem.BootimgFactory,
63 &filesystem.BootimgProperties{
Jihoon Kang96fdba92024-11-19 22:25:36 +000064 Boot_image_type: proptools.StringPtr("vendor_boot"),
65 Ramdisk_module: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor_ramdisk")),
66 Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion),
Cole Faust24938e22024-11-18 14:01:58 -080067 },
68 &struct {
69 Name *string
70 }{
71 Name: proptools.StringPtr(bootImageName),
72 },
73 )
74 return true
75}
76
Jihoon Kang95eb1da2024-11-19 20:55:20 +000077func createInitBootImage(ctx android.LoadHookContext) bool {
78 partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
79
80 bootImageName := generatedModuleNameForPartition(ctx.Config(), "init_boot")
81
82 ctx.CreateModule(
83 filesystem.BootimgFactory,
84 &filesystem.BootimgProperties{
Jihoon Kang96fdba92024-11-19 22:25:36 +000085 Boot_image_type: proptools.StringPtr("init_boot"),
86 Ramdisk_module: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "ramdisk")),
87 Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion),
Jihoon Kang95eb1da2024-11-19 20:55:20 +000088 },
89 &struct {
90 Name *string
91 }{
92 Name: proptools.StringPtr(bootImageName),
93 },
94 )
95 return true
96}
97
Cole Faustf2a6e8b2024-11-14 10:54:48 -080098// Returns the equivalent of the BUILDING_BOOT_IMAGE variable in make. Derived from this logic:
99// https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=458;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0
100func buildingBootImage(partitionVars android.PartitionVariables) bool {
101 if partitionVars.BoardUsesRecoveryAsBoot {
102 return false
103 }
104
105 if partitionVars.ProductBuildBootImage {
106 return true
107 }
108
109 if len(partitionVars.BoardPrebuiltBootimage) > 0 {
110 return false
111 }
112
113 if len(partitionVars.BoardBootimagePartitionSize) > 0 {
114 return true
115 }
116
117 // TODO: return true if BOARD_KERNEL_BINARIES is set and has a *_BOOTIMAGE_PARTITION_SIZE
118 // variable. However, I don't think BOARD_KERNEL_BINARIES is ever set in practice.
119
120 return false
121}
Cole Faust24938e22024-11-18 14:01:58 -0800122
123// Returns the equivalent of the BUILDING_VENDOR_BOOT_IMAGE variable in make. Derived from this logic:
124// https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=518;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0
125func buildingVendorBootImage(partitionVars android.PartitionVariables) bool {
126 if v, exists := boardBootHeaderVersion(partitionVars); exists && v >= 3 {
127 x := partitionVars.ProductBuildVendorBootImage
128 if x == "" || x == "true" {
129 return true
130 }
131 }
132
133 return false
134}
135
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000136// Derived from: https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=480;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0
137func buildingInitBootImage(partitionVars android.PartitionVariables) bool {
138 if !partitionVars.ProductBuildInitBootImage {
139 if partitionVars.BoardUsesRecoveryAsBoot || len(partitionVars.BoardPrebuiltInitBootimage) > 0 {
140 return false
141 } else if len(partitionVars.BoardInitBootimagePartitionSize) > 0 {
142 return true
143 }
144 } else {
145 if partitionVars.BoardUsesRecoveryAsBoot {
146 panic("PRODUCT_BUILD_INIT_BOOT_IMAGE is true, but so is BOARD_USES_RECOVERY_AS_BOOT. Use only one option.")
147 }
148 return true
149 }
150 return false
151}
152
Cole Faust24938e22024-11-18 14:01:58 -0800153func boardBootHeaderVersion(partitionVars android.PartitionVariables) (int, bool) {
154 if len(partitionVars.BoardBootHeaderVersion) == 0 {
155 return 0, false
156 }
157 v, err := strconv.ParseInt(partitionVars.BoardBootHeaderVersion, 10, 32)
158 if err != nil {
159 panic(fmt.Sprintf("BOARD_BOOT_HEADER_VERSION must be an int, got: %q", partitionVars.BoardBootHeaderVersion))
160 }
161 return int(v), true
162}