blob: 799dbc9e3cbeb02d26852b7758d0e91510115267 [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
Cole Faust24938e22024-11-18 14:01:58 -0800107 ctx.CreateModule(
108 filesystem.BootimgFactory,
109 &filesystem.BootimgProperties{
Cole Faust0c4b4152024-11-20 16:42:53 -0800110 Boot_image_type: proptools.StringPtr("vendor_boot"),
111 Ramdisk_module: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor_ramdisk")),
112 Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion),
113 Use_avb: avbInfo.avbEnable,
114 Avb_mode: avbInfo.avbMode,
115 Avb_private_key: avbInfo.avbkeyFilegroup,
116 Avb_rollback_index: avbInfo.avbRollbackIndex,
117 Avb_algorithm: avbInfo.avbAlgorithm,
Jihoon Kang70c1c682024-11-20 23:58:38 +0000118 Dtb_prebuilt: dtbPrebuilt,
Jihoon Kang1821b5e2024-11-21 20:49:55 +0000119 Cmdline: cmdline,
Cole Faust24938e22024-11-18 14:01:58 -0800120 },
121 &struct {
122 Name *string
123 }{
124 Name: proptools.StringPtr(bootImageName),
125 },
126 )
127 return true
128}
129
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000130func createInitBootImage(ctx android.LoadHookContext) bool {
131 partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
132
133 bootImageName := generatedModuleNameForPartition(ctx.Config(), "init_boot")
134
Cole Faust336b3ba2024-11-19 16:34:29 -0800135 var securityPatch *string
136 if partitionVariables.InitBootSecurityPatch != "" {
137 securityPatch = &partitionVariables.InitBootSecurityPatch
138 } else if partitionVariables.BootSecurityPatch != "" {
139 securityPatch = &partitionVariables.BootSecurityPatch
140 }
141
Cole Faust0c4b4152024-11-20 16:42:53 -0800142 var partitionSize *int64
143 if partitionVariables.BoardInitBootimagePartitionSize != "" {
144 // Base of zero will allow base 10 or base 16 if starting with 0x
145 parsed, err := strconv.ParseInt(partitionVariables.BoardInitBootimagePartitionSize, 0, 64)
146 if err != nil {
147 panic(fmt.Sprintf("BOARD_INIT_BOOT_IMAGE_PARTITION_SIZE must be an int, got %s", partitionVariables.BoardInitBootimagePartitionSize))
148 }
149 partitionSize = &parsed
150 }
151
152 avbInfo := getAvbInfo(ctx.Config(), "init_boot")
153
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000154 ctx.CreateModule(
155 filesystem.BootimgFactory,
156 &filesystem.BootimgProperties{
Cole Faust0c4b4152024-11-20 16:42:53 -0800157 Boot_image_type: proptools.StringPtr("init_boot"),
158 Ramdisk_module: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "ramdisk")),
159 Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion),
160 Security_patch: securityPatch,
161 Partition_size: partitionSize,
162 Use_avb: avbInfo.avbEnable,
163 Avb_mode: avbInfo.avbMode,
164 Avb_private_key: avbInfo.avbkeyFilegroup,
165 Avb_rollback_index: avbInfo.avbRollbackIndex,
166 Avb_algorithm: avbInfo.avbAlgorithm,
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000167 },
168 &struct {
169 Name *string
170 }{
171 Name: proptools.StringPtr(bootImageName),
172 },
173 )
174 return true
175}
176
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800177// Returns the equivalent of the BUILDING_BOOT_IMAGE variable in make. Derived from this logic:
178// https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=458;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0
179func buildingBootImage(partitionVars android.PartitionVariables) bool {
180 if partitionVars.BoardUsesRecoveryAsBoot {
181 return false
182 }
183
184 if partitionVars.ProductBuildBootImage {
185 return true
186 }
187
188 if len(partitionVars.BoardPrebuiltBootimage) > 0 {
189 return false
190 }
191
192 if len(partitionVars.BoardBootimagePartitionSize) > 0 {
193 return true
194 }
195
196 // TODO: return true if BOARD_KERNEL_BINARIES is set and has a *_BOOTIMAGE_PARTITION_SIZE
197 // variable. However, I don't think BOARD_KERNEL_BINARIES is ever set in practice.
198
199 return false
200}
Cole Faust24938e22024-11-18 14:01:58 -0800201
202// Returns the equivalent of the BUILDING_VENDOR_BOOT_IMAGE variable in make. Derived from this logic:
203// https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=518;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0
204func buildingVendorBootImage(partitionVars android.PartitionVariables) bool {
205 if v, exists := boardBootHeaderVersion(partitionVars); exists && v >= 3 {
206 x := partitionVars.ProductBuildVendorBootImage
207 if x == "" || x == "true" {
208 return true
209 }
210 }
211
212 return false
213}
214
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000215// Derived from: https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=480;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0
216func buildingInitBootImage(partitionVars android.PartitionVariables) bool {
217 if !partitionVars.ProductBuildInitBootImage {
218 if partitionVars.BoardUsesRecoveryAsBoot || len(partitionVars.BoardPrebuiltInitBootimage) > 0 {
219 return false
220 } else if len(partitionVars.BoardInitBootimagePartitionSize) > 0 {
221 return true
222 }
223 } else {
224 if partitionVars.BoardUsesRecoveryAsBoot {
225 panic("PRODUCT_BUILD_INIT_BOOT_IMAGE is true, but so is BOARD_USES_RECOVERY_AS_BOOT. Use only one option.")
226 }
227 return true
228 }
229 return false
230}
231
Cole Faust24938e22024-11-18 14:01:58 -0800232func boardBootHeaderVersion(partitionVars android.PartitionVariables) (int, bool) {
233 if len(partitionVars.BoardBootHeaderVersion) == 0 {
234 return 0, false
235 }
236 v, err := strconv.ParseInt(partitionVars.BoardBootHeaderVersion, 10, 32)
237 if err != nil {
238 panic(fmt.Sprintf("BOARD_BOOT_HEADER_VERSION must be an int, got: %q", partitionVars.BoardBootHeaderVersion))
239 }
240 return int(v), true
241}
Jihoon Kang70c1c682024-11-20 23:58:38 +0000242
243type dtbImg struct {
244 // whether to include the dtb image in boot image
245 include bool
246
247 // name of the generated dtb image filegroup name
248 name string
249
250 // type of the boot image that the dtb image argument should be specified
251 imgType string
252}
253
254func createDtbImgFilegroup(ctx android.LoadHookContext) dtbImg {
255 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
256 if !partitionVars.BoardIncludeDtbInBootimg {
257 return dtbImg{include: false}
258 }
259 for _, copyFilePair := range partitionVars.ProductCopyFiles {
260 srcDestList := strings.Split(copyFilePair, ":")
261 if len(srcDestList) < 2 {
262 ctx.ModuleErrorf("PRODUCT_COPY_FILES must follow the format \"src:dest\", got: %s", copyFilePair)
263 }
264 if srcDestList[1] == "dtb.img" {
265 moduleName := generatedModuleName(ctx.Config(), "dtb_img_filegroup")
266 ctx.CreateModuleInDirectory(
267 android.FileGroupFactory,
268 filepath.Dir(srcDestList[0]),
269 &struct {
270 Name *string
271 Srcs []string
272 }{
273 Name: proptools.StringPtr(moduleName),
274 Srcs: []string{filepath.Base(srcDestList[1])},
275 },
276 )
277 imgType := "vendor_boot"
278 if !buildingVendorBootImage(partitionVars) {
279 imgType = "boot"
280 }
281 return dtbImg{include: true, name: moduleName, imgType: imgType}
282 }
283 }
284 return dtbImg{include: false}
285}