blob: 4ccc90ac25c7d73faf56b018be9069b0f726f2b5 [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"
Jihoon Kang70c1c682024-11-20 23:58:38 +00009 "strings"
Cole Faustf2a6e8b2024-11-14 10:54:48 -080010
11 "github.com/google/blueprint/proptools"
12)
13
Jihoon Kang70c1c682024-11-20 23:58:38 +000014func createBootImage(ctx android.LoadHookContext, dtbImg dtbImg) bool {
Cole Faustf2a6e8b2024-11-14 10:54:48 -080015 partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
16
17 if partitionVariables.TargetKernelPath == "" {
18 // There are potentially code paths that don't set TARGET_KERNEL_PATH
19 return false
20 }
21
22 kernelDir := filepath.Dir(partitionVariables.TargetKernelPath)
23 kernelBase := filepath.Base(partitionVariables.TargetKernelPath)
24 kernelFilegroupName := generatedModuleName(ctx.Config(), "kernel")
25
26 ctx.CreateModuleInDirectory(
27 android.FileGroupFactory,
28 kernelDir,
29 &struct {
30 Name *string
31 Srcs []string
32 Visibility []string
33 }{
34 Name: proptools.StringPtr(kernelFilegroupName),
35 Srcs: []string{kernelBase},
36 Visibility: []string{"//visibility:public"},
37 },
38 )
39
Cole Faust26bdac52024-11-19 13:37:53 -080040 var partitionSize *int64
41 if partitionVariables.BoardBootimagePartitionSize != "" {
Cole Faust0c4b4152024-11-20 16:42:53 -080042 // Base of zero will allow base 10 or base 16 if starting with 0x
43 parsed, err := strconv.ParseInt(partitionVariables.BoardBootimagePartitionSize, 0, 64)
Cole Faust26bdac52024-11-19 13:37:53 -080044 if err != nil {
45 panic(fmt.Sprintf("BOARD_BOOTIMAGE_PARTITION_SIZE must be an int, got %s", partitionVariables.BoardBootimagePartitionSize))
46 }
47 partitionSize = &parsed
48 }
49
Cole Faust336b3ba2024-11-19 16:34:29 -080050 var securityPatch *string
51 if partitionVariables.BootSecurityPatch != "" {
52 securityPatch = &partitionVariables.BootSecurityPatch
53 }
54
Cole Faust0c4b4152024-11-20 16:42:53 -080055 avbInfo := getAvbInfo(ctx.Config(), "boot")
56
Cole Faustf2a6e8b2024-11-14 10:54:48 -080057 bootImageName := generatedModuleNameForPartition(ctx.Config(), "boot")
58
Jihoon Kang70c1c682024-11-20 23:58:38 +000059 var dtbPrebuilt *string
60 if dtbImg.include && dtbImg.imgType == "boot" {
61 dtbPrebuilt = proptools.StringPtr(":" + dtbImg.name)
62 }
63
Cole Faustf2a6e8b2024-11-14 10:54:48 -080064 ctx.CreateModule(
65 filesystem.BootimgFactory,
66 &filesystem.BootimgProperties{
Cole Faust0c4b4152024-11-20 16:42:53 -080067 Kernel_prebuilt: proptools.StringPtr(":" + kernelFilegroupName),
68 Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion),
69 Partition_size: partitionSize,
70 Use_avb: avbInfo.avbEnable,
71 Avb_mode: avbInfo.avbMode,
72 Avb_private_key: avbInfo.avbkeyFilegroup,
73 Avb_rollback_index: avbInfo.avbRollbackIndex,
74 Avb_algorithm: avbInfo.avbAlgorithm,
75 Security_patch: securityPatch,
Jihoon Kang70c1c682024-11-20 23:58:38 +000076 Dtb_prebuilt: dtbPrebuilt,
Cole Faustf2a6e8b2024-11-14 10:54:48 -080077 },
78 &struct {
79 Name *string
80 }{
81 Name: proptools.StringPtr(bootImageName),
82 },
83 )
84 return true
85}
86
Jihoon Kang70c1c682024-11-20 23:58:38 +000087func createVendorBootImage(ctx android.LoadHookContext, dtbImg dtbImg) bool {
Cole Faust24938e22024-11-18 14:01:58 -080088 partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
89
90 bootImageName := generatedModuleNameForPartition(ctx.Config(), "vendor_boot")
91
Cole Faust0c4b4152024-11-20 16:42:53 -080092 avbInfo := getAvbInfo(ctx.Config(), "vendor_boot")
93
Jihoon Kang70c1c682024-11-20 23:58:38 +000094 var dtbPrebuilt *string
95 if dtbImg.include && dtbImg.imgType == "vendor_boot" {
96 dtbPrebuilt = proptools.StringPtr(":" + dtbImg.name)
97 }
98
Cole Faust24938e22024-11-18 14:01:58 -080099 ctx.CreateModule(
100 filesystem.BootimgFactory,
101 &filesystem.BootimgProperties{
Cole Faust0c4b4152024-11-20 16:42:53 -0800102 Boot_image_type: proptools.StringPtr("vendor_boot"),
103 Ramdisk_module: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor_ramdisk")),
104 Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion),
105 Use_avb: avbInfo.avbEnable,
106 Avb_mode: avbInfo.avbMode,
107 Avb_private_key: avbInfo.avbkeyFilegroup,
108 Avb_rollback_index: avbInfo.avbRollbackIndex,
109 Avb_algorithm: avbInfo.avbAlgorithm,
Jihoon Kang70c1c682024-11-20 23:58:38 +0000110 Dtb_prebuilt: dtbPrebuilt,
Cole Faust24938e22024-11-18 14:01:58 -0800111 },
112 &struct {
113 Name *string
114 }{
115 Name: proptools.StringPtr(bootImageName),
116 },
117 )
118 return true
119}
120
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000121func createInitBootImage(ctx android.LoadHookContext) bool {
122 partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
123
124 bootImageName := generatedModuleNameForPartition(ctx.Config(), "init_boot")
125
Cole Faust336b3ba2024-11-19 16:34:29 -0800126 var securityPatch *string
127 if partitionVariables.InitBootSecurityPatch != "" {
128 securityPatch = &partitionVariables.InitBootSecurityPatch
129 } else if partitionVariables.BootSecurityPatch != "" {
130 securityPatch = &partitionVariables.BootSecurityPatch
131 }
132
Cole Faust0c4b4152024-11-20 16:42:53 -0800133 var partitionSize *int64
134 if partitionVariables.BoardInitBootimagePartitionSize != "" {
135 // Base of zero will allow base 10 or base 16 if starting with 0x
136 parsed, err := strconv.ParseInt(partitionVariables.BoardInitBootimagePartitionSize, 0, 64)
137 if err != nil {
138 panic(fmt.Sprintf("BOARD_INIT_BOOT_IMAGE_PARTITION_SIZE must be an int, got %s", partitionVariables.BoardInitBootimagePartitionSize))
139 }
140 partitionSize = &parsed
141 }
142
143 avbInfo := getAvbInfo(ctx.Config(), "init_boot")
144
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000145 ctx.CreateModule(
146 filesystem.BootimgFactory,
147 &filesystem.BootimgProperties{
Cole Faust0c4b4152024-11-20 16:42:53 -0800148 Boot_image_type: proptools.StringPtr("init_boot"),
149 Ramdisk_module: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "ramdisk")),
150 Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion),
151 Security_patch: securityPatch,
152 Partition_size: partitionSize,
153 Use_avb: avbInfo.avbEnable,
154 Avb_mode: avbInfo.avbMode,
155 Avb_private_key: avbInfo.avbkeyFilegroup,
156 Avb_rollback_index: avbInfo.avbRollbackIndex,
157 Avb_algorithm: avbInfo.avbAlgorithm,
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000158 },
159 &struct {
160 Name *string
161 }{
162 Name: proptools.StringPtr(bootImageName),
163 },
164 )
165 return true
166}
167
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800168// Returns the equivalent of the BUILDING_BOOT_IMAGE variable in make. Derived from this logic:
169// https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=458;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0
170func buildingBootImage(partitionVars android.PartitionVariables) bool {
171 if partitionVars.BoardUsesRecoveryAsBoot {
172 return false
173 }
174
175 if partitionVars.ProductBuildBootImage {
176 return true
177 }
178
179 if len(partitionVars.BoardPrebuiltBootimage) > 0 {
180 return false
181 }
182
183 if len(partitionVars.BoardBootimagePartitionSize) > 0 {
184 return true
185 }
186
187 // TODO: return true if BOARD_KERNEL_BINARIES is set and has a *_BOOTIMAGE_PARTITION_SIZE
188 // variable. However, I don't think BOARD_KERNEL_BINARIES is ever set in practice.
189
190 return false
191}
Cole Faust24938e22024-11-18 14:01:58 -0800192
193// Returns the equivalent of the BUILDING_VENDOR_BOOT_IMAGE variable in make. Derived from this logic:
194// https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=518;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0
195func buildingVendorBootImage(partitionVars android.PartitionVariables) bool {
196 if v, exists := boardBootHeaderVersion(partitionVars); exists && v >= 3 {
197 x := partitionVars.ProductBuildVendorBootImage
198 if x == "" || x == "true" {
199 return true
200 }
201 }
202
203 return false
204}
205
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000206// Derived from: https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=480;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0
207func buildingInitBootImage(partitionVars android.PartitionVariables) bool {
208 if !partitionVars.ProductBuildInitBootImage {
209 if partitionVars.BoardUsesRecoveryAsBoot || len(partitionVars.BoardPrebuiltInitBootimage) > 0 {
210 return false
211 } else if len(partitionVars.BoardInitBootimagePartitionSize) > 0 {
212 return true
213 }
214 } else {
215 if partitionVars.BoardUsesRecoveryAsBoot {
216 panic("PRODUCT_BUILD_INIT_BOOT_IMAGE is true, but so is BOARD_USES_RECOVERY_AS_BOOT. Use only one option.")
217 }
218 return true
219 }
220 return false
221}
222
Cole Faust24938e22024-11-18 14:01:58 -0800223func boardBootHeaderVersion(partitionVars android.PartitionVariables) (int, bool) {
224 if len(partitionVars.BoardBootHeaderVersion) == 0 {
225 return 0, false
226 }
227 v, err := strconv.ParseInt(partitionVars.BoardBootHeaderVersion, 10, 32)
228 if err != nil {
229 panic(fmt.Sprintf("BOARD_BOOT_HEADER_VERSION must be an int, got: %q", partitionVars.BoardBootHeaderVersion))
230 }
231 return int(v), true
232}
Jihoon Kang70c1c682024-11-20 23:58:38 +0000233
234type dtbImg struct {
235 // whether to include the dtb image in boot image
236 include bool
237
238 // name of the generated dtb image filegroup name
239 name string
240
241 // type of the boot image that the dtb image argument should be specified
242 imgType string
243}
244
245func createDtbImgFilegroup(ctx android.LoadHookContext) dtbImg {
246 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
247 if !partitionVars.BoardIncludeDtbInBootimg {
248 return dtbImg{include: false}
249 }
250 for _, copyFilePair := range partitionVars.ProductCopyFiles {
251 srcDestList := strings.Split(copyFilePair, ":")
252 if len(srcDestList) < 2 {
253 ctx.ModuleErrorf("PRODUCT_COPY_FILES must follow the format \"src:dest\", got: %s", copyFilePair)
254 }
255 if srcDestList[1] == "dtb.img" {
256 moduleName := generatedModuleName(ctx.Config(), "dtb_img_filegroup")
257 ctx.CreateModuleInDirectory(
258 android.FileGroupFactory,
259 filepath.Dir(srcDestList[0]),
260 &struct {
261 Name *string
262 Srcs []string
263 }{
264 Name: proptools.StringPtr(moduleName),
265 Srcs: []string{filepath.Base(srcDestList[1])},
266 },
267 )
268 imgType := "vendor_boot"
269 if !buildingVendorBootImage(partitionVars) {
270 imgType = "boot"
271 }
272 return dtbImg{include: true, name: moduleName, imgType: imgType}
273 }
274 }
275 return dtbImg{include: false}
276}