blob: c73c34537371565aa70770848b52d827ac3ba8f1 [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
Jihoon Kang1821b5e2024-11-21 20:49:55 +000064 var cmdline []string
65 if !buildingVendorBootImage(partitionVariables) {
66 cmdline = partitionVariables.InternalKernelCmdline
67 }
68
Cole Faustf2a6e8b2024-11-14 10:54:48 -080069 ctx.CreateModule(
70 filesystem.BootimgFactory,
71 &filesystem.BootimgProperties{
Cole Faust0c4b4152024-11-20 16:42:53 -080072 Kernel_prebuilt: proptools.StringPtr(":" + kernelFilegroupName),
73 Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion),
74 Partition_size: partitionSize,
75 Use_avb: avbInfo.avbEnable,
76 Avb_mode: avbInfo.avbMode,
77 Avb_private_key: avbInfo.avbkeyFilegroup,
78 Avb_rollback_index: avbInfo.avbRollbackIndex,
79 Avb_algorithm: avbInfo.avbAlgorithm,
80 Security_patch: securityPatch,
Jihoon Kang70c1c682024-11-20 23:58:38 +000081 Dtb_prebuilt: dtbPrebuilt,
Jihoon Kang1821b5e2024-11-21 20:49:55 +000082 Cmdline: cmdline,
Cole Faustf2a6e8b2024-11-14 10:54:48 -080083 },
84 &struct {
85 Name *string
86 }{
87 Name: proptools.StringPtr(bootImageName),
88 },
89 )
90 return true
91}
92
Jihoon Kang70c1c682024-11-20 23:58:38 +000093func createVendorBootImage(ctx android.LoadHookContext, dtbImg dtbImg) bool {
Cole Faust24938e22024-11-18 14:01:58 -080094 partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
95
96 bootImageName := generatedModuleNameForPartition(ctx.Config(), "vendor_boot")
97
Cole Faust0c4b4152024-11-20 16:42:53 -080098 avbInfo := getAvbInfo(ctx.Config(), "vendor_boot")
99
Jihoon Kang70c1c682024-11-20 23:58:38 +0000100 var dtbPrebuilt *string
101 if dtbImg.include && dtbImg.imgType == "vendor_boot" {
102 dtbPrebuilt = proptools.StringPtr(":" + dtbImg.name)
103 }
104
Jihoon Kang1821b5e2024-11-21 20:49:55 +0000105 cmdline := partitionVariables.InternalKernelCmdline
106
Jihoon Kang4004cc62024-11-21 23:57:19 +0000107 var vendorBootConfigImg *string
108 if name, ok := createVendorBootConfigImg(ctx); ok {
109 vendorBootConfigImg = proptools.StringPtr(":" + name)
110 }
111
Cole Faust24938e22024-11-18 14:01:58 -0800112 ctx.CreateModule(
113 filesystem.BootimgFactory,
114 &filesystem.BootimgProperties{
Cole Faust0c4b4152024-11-20 16:42:53 -0800115 Boot_image_type: proptools.StringPtr("vendor_boot"),
116 Ramdisk_module: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor_ramdisk")),
117 Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion),
118 Use_avb: avbInfo.avbEnable,
119 Avb_mode: avbInfo.avbMode,
120 Avb_private_key: avbInfo.avbkeyFilegroup,
121 Avb_rollback_index: avbInfo.avbRollbackIndex,
Jihoon Kang70c1c682024-11-20 23:58:38 +0000122 Dtb_prebuilt: dtbPrebuilt,
Jihoon Kang1821b5e2024-11-21 20:49:55 +0000123 Cmdline: cmdline,
Jihoon Kang4004cc62024-11-21 23:57:19 +0000124 Bootconfig: vendorBootConfigImg,
Cole Faust24938e22024-11-18 14:01:58 -0800125 },
126 &struct {
127 Name *string
128 }{
129 Name: proptools.StringPtr(bootImageName),
130 },
131 )
132 return true
133}
134
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000135func createInitBootImage(ctx android.LoadHookContext) bool {
136 partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
137
138 bootImageName := generatedModuleNameForPartition(ctx.Config(), "init_boot")
139
Cole Faust336b3ba2024-11-19 16:34:29 -0800140 var securityPatch *string
141 if partitionVariables.InitBootSecurityPatch != "" {
142 securityPatch = &partitionVariables.InitBootSecurityPatch
143 } else if partitionVariables.BootSecurityPatch != "" {
144 securityPatch = &partitionVariables.BootSecurityPatch
145 }
146
Cole Faust0c4b4152024-11-20 16:42:53 -0800147 var partitionSize *int64
148 if partitionVariables.BoardInitBootimagePartitionSize != "" {
149 // Base of zero will allow base 10 or base 16 if starting with 0x
150 parsed, err := strconv.ParseInt(partitionVariables.BoardInitBootimagePartitionSize, 0, 64)
151 if err != nil {
152 panic(fmt.Sprintf("BOARD_INIT_BOOT_IMAGE_PARTITION_SIZE must be an int, got %s", partitionVariables.BoardInitBootimagePartitionSize))
153 }
154 partitionSize = &parsed
155 }
156
157 avbInfo := getAvbInfo(ctx.Config(), "init_boot")
158
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000159 ctx.CreateModule(
160 filesystem.BootimgFactory,
161 &filesystem.BootimgProperties{
Cole Faust0c4b4152024-11-20 16:42:53 -0800162 Boot_image_type: proptools.StringPtr("init_boot"),
163 Ramdisk_module: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "ramdisk")),
164 Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion),
165 Security_patch: securityPatch,
166 Partition_size: partitionSize,
167 Use_avb: avbInfo.avbEnable,
168 Avb_mode: avbInfo.avbMode,
169 Avb_private_key: avbInfo.avbkeyFilegroup,
170 Avb_rollback_index: avbInfo.avbRollbackIndex,
171 Avb_algorithm: avbInfo.avbAlgorithm,
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000172 },
173 &struct {
174 Name *string
175 }{
176 Name: proptools.StringPtr(bootImageName),
177 },
178 )
179 return true
180}
181
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800182// Returns the equivalent of the BUILDING_BOOT_IMAGE variable in make. Derived from this logic:
183// https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=458;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0
184func buildingBootImage(partitionVars android.PartitionVariables) bool {
185 if partitionVars.BoardUsesRecoveryAsBoot {
186 return false
187 }
188
189 if partitionVars.ProductBuildBootImage {
190 return true
191 }
192
193 if len(partitionVars.BoardPrebuiltBootimage) > 0 {
194 return false
195 }
196
197 if len(partitionVars.BoardBootimagePartitionSize) > 0 {
198 return true
199 }
200
201 // TODO: return true if BOARD_KERNEL_BINARIES is set and has a *_BOOTIMAGE_PARTITION_SIZE
202 // variable. However, I don't think BOARD_KERNEL_BINARIES is ever set in practice.
203
204 return false
205}
Cole Faust24938e22024-11-18 14:01:58 -0800206
207// Returns the equivalent of the BUILDING_VENDOR_BOOT_IMAGE variable in make. Derived from this logic:
208// https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=518;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0
209func buildingVendorBootImage(partitionVars android.PartitionVariables) bool {
210 if v, exists := boardBootHeaderVersion(partitionVars); exists && v >= 3 {
211 x := partitionVars.ProductBuildVendorBootImage
212 if x == "" || x == "true" {
213 return true
214 }
215 }
216
217 return false
218}
219
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000220// Derived from: https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=480;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0
221func buildingInitBootImage(partitionVars android.PartitionVariables) bool {
222 if !partitionVars.ProductBuildInitBootImage {
223 if partitionVars.BoardUsesRecoveryAsBoot || len(partitionVars.BoardPrebuiltInitBootimage) > 0 {
224 return false
225 } else if len(partitionVars.BoardInitBootimagePartitionSize) > 0 {
226 return true
227 }
228 } else {
229 if partitionVars.BoardUsesRecoveryAsBoot {
230 panic("PRODUCT_BUILD_INIT_BOOT_IMAGE is true, but so is BOARD_USES_RECOVERY_AS_BOOT. Use only one option.")
231 }
232 return true
233 }
234 return false
235}
236
Cole Faust24938e22024-11-18 14:01:58 -0800237func boardBootHeaderVersion(partitionVars android.PartitionVariables) (int, bool) {
238 if len(partitionVars.BoardBootHeaderVersion) == 0 {
239 return 0, false
240 }
241 v, err := strconv.ParseInt(partitionVars.BoardBootHeaderVersion, 10, 32)
242 if err != nil {
243 panic(fmt.Sprintf("BOARD_BOOT_HEADER_VERSION must be an int, got: %q", partitionVars.BoardBootHeaderVersion))
244 }
245 return int(v), true
246}
Jihoon Kang70c1c682024-11-20 23:58:38 +0000247
248type dtbImg struct {
249 // whether to include the dtb image in boot image
250 include bool
251
252 // name of the generated dtb image filegroup name
253 name string
254
255 // type of the boot image that the dtb image argument should be specified
256 imgType string
257}
258
259func createDtbImgFilegroup(ctx android.LoadHookContext) dtbImg {
260 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
261 if !partitionVars.BoardIncludeDtbInBootimg {
262 return dtbImg{include: false}
263 }
264 for _, copyFilePair := range partitionVars.ProductCopyFiles {
265 srcDestList := strings.Split(copyFilePair, ":")
266 if len(srcDestList) < 2 {
267 ctx.ModuleErrorf("PRODUCT_COPY_FILES must follow the format \"src:dest\", got: %s", copyFilePair)
268 }
269 if srcDestList[1] == "dtb.img" {
270 moduleName := generatedModuleName(ctx.Config(), "dtb_img_filegroup")
271 ctx.CreateModuleInDirectory(
272 android.FileGroupFactory,
273 filepath.Dir(srcDestList[0]),
274 &struct {
275 Name *string
276 Srcs []string
277 }{
278 Name: proptools.StringPtr(moduleName),
279 Srcs: []string{filepath.Base(srcDestList[1])},
280 },
281 )
282 imgType := "vendor_boot"
283 if !buildingVendorBootImage(partitionVars) {
284 imgType = "boot"
285 }
286 return dtbImg{include: true, name: moduleName, imgType: imgType}
287 }
288 }
289 return dtbImg{include: false}
290}
Jihoon Kang4004cc62024-11-21 23:57:19 +0000291
292func createVendorBootConfigImg(ctx android.LoadHookContext) (string, bool) {
293 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
294 bootconfig := partitionVars.InternalBootconfig
295 bootconfigFile := partitionVars.InternalBootconfigFile
296 if len(bootconfig) == 0 && len(bootconfigFile) == 0 {
297 return "", false
298 }
299
300 vendorBootconfigImgModuleName := generatedModuleName(ctx.Config(), "vendor_bootconfig_image")
301
302 ctx.CreateModule(
303 filesystem.BootconfigModuleFactory,
304 &struct {
305 Name *string
306 Boot_config []string
307 Boot_config_file *string
308 }{
309 Name: proptools.StringPtr(vendorBootconfigImgModuleName),
310 Boot_config: bootconfig,
311 Boot_config_file: proptools.StringPtr(bootconfigFile),
312 },
313 )
314
315 return vendorBootconfigImgModuleName, true
316}