Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 1 | package fsgen |
| 2 | |
| 3 | import ( |
| 4 | "android/soong/android" |
| 5 | "android/soong/filesystem" |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 6 | "fmt" |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 7 | "path/filepath" |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 8 | "strconv" |
Jihoon Kang | 70c1c68 | 2024-11-20 23:58:38 +0000 | [diff] [blame] | 9 | "strings" |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 10 | |
| 11 | "github.com/google/blueprint/proptools" |
| 12 | ) |
| 13 | |
Jihoon Kang | 70c1c68 | 2024-11-20 23:58:38 +0000 | [diff] [blame] | 14 | func createBootImage(ctx android.LoadHookContext, dtbImg dtbImg) bool { |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 15 | 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 Faust | 26bdac5 | 2024-11-19 13:37:53 -0800 | [diff] [blame] | 40 | var partitionSize *int64 |
| 41 | if partitionVariables.BoardBootimagePartitionSize != "" { |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [diff] [blame] | 42 | // Base of zero will allow base 10 or base 16 if starting with 0x |
| 43 | parsed, err := strconv.ParseInt(partitionVariables.BoardBootimagePartitionSize, 0, 64) |
Cole Faust | 26bdac5 | 2024-11-19 13:37:53 -0800 | [diff] [blame] | 44 | 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 Faust | 336b3ba | 2024-11-19 16:34:29 -0800 | [diff] [blame] | 50 | var securityPatch *string |
| 51 | if partitionVariables.BootSecurityPatch != "" { |
| 52 | securityPatch = &partitionVariables.BootSecurityPatch |
| 53 | } |
| 54 | |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [diff] [blame] | 55 | avbInfo := getAvbInfo(ctx.Config(), "boot") |
| 56 | |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 57 | bootImageName := generatedModuleNameForPartition(ctx.Config(), "boot") |
| 58 | |
Jihoon Kang | 70c1c68 | 2024-11-20 23:58:38 +0000 | [diff] [blame] | 59 | var dtbPrebuilt *string |
| 60 | if dtbImg.include && dtbImg.imgType == "boot" { |
| 61 | dtbPrebuilt = proptools.StringPtr(":" + dtbImg.name) |
| 62 | } |
| 63 | |
Jihoon Kang | 1821b5e | 2024-11-21 20:49:55 +0000 | [diff] [blame] | 64 | var cmdline []string |
| 65 | if !buildingVendorBootImage(partitionVariables) { |
| 66 | cmdline = partitionVariables.InternalKernelCmdline |
| 67 | } |
| 68 | |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 69 | ctx.CreateModule( |
| 70 | filesystem.BootimgFactory, |
| 71 | &filesystem.BootimgProperties{ |
Jihoon Kang | 2f0d193 | 2025-01-17 19:22:44 +0000 | [diff] [blame] | 72 | 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_rollback_index_location: avbInfo.avbRollbackIndexLocation, |
| 80 | Avb_algorithm: avbInfo.avbAlgorithm, |
| 81 | Security_patch: securityPatch, |
| 82 | Dtb_prebuilt: dtbPrebuilt, |
| 83 | Cmdline: cmdline, |
| 84 | Stem: proptools.StringPtr("boot.img"), |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 85 | }, |
| 86 | &struct { |
Jihoon Kang | 2f0d193 | 2025-01-17 19:22:44 +0000 | [diff] [blame] | 87 | Name *string |
| 88 | Visibility []string |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 89 | }{ |
Jihoon Kang | 2f0d193 | 2025-01-17 19:22:44 +0000 | [diff] [blame] | 90 | Name: proptools.StringPtr(bootImageName), |
| 91 | Visibility: []string{"//visibility:public"}, |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 92 | }, |
| 93 | ) |
| 94 | return true |
| 95 | } |
| 96 | |
Jihoon Kang | 70c1c68 | 2024-11-20 23:58:38 +0000 | [diff] [blame] | 97 | func createVendorBootImage(ctx android.LoadHookContext, dtbImg dtbImg) bool { |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 98 | partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
| 99 | |
| 100 | bootImageName := generatedModuleNameForPartition(ctx.Config(), "vendor_boot") |
| 101 | |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [diff] [blame] | 102 | avbInfo := getAvbInfo(ctx.Config(), "vendor_boot") |
| 103 | |
Jihoon Kang | 70c1c68 | 2024-11-20 23:58:38 +0000 | [diff] [blame] | 104 | var dtbPrebuilt *string |
| 105 | if dtbImg.include && dtbImg.imgType == "vendor_boot" { |
| 106 | dtbPrebuilt = proptools.StringPtr(":" + dtbImg.name) |
| 107 | } |
| 108 | |
Jihoon Kang | 1821b5e | 2024-11-21 20:49:55 +0000 | [diff] [blame] | 109 | cmdline := partitionVariables.InternalKernelCmdline |
| 110 | |
Jihoon Kang | 4004cc6 | 2024-11-21 23:57:19 +0000 | [diff] [blame] | 111 | var vendorBootConfigImg *string |
| 112 | if name, ok := createVendorBootConfigImg(ctx); ok { |
| 113 | vendorBootConfigImg = proptools.StringPtr(":" + name) |
| 114 | } |
| 115 | |
Jihoon Kang | 5cf3642 | 2025-01-02 19:33:43 +0000 | [diff] [blame] | 116 | var partitionSize *int64 |
| 117 | if partitionVariables.BoardVendorBootimagePartitionSize != "" { |
| 118 | // Base of zero will allow base 10 or base 16 if starting with 0x |
| 119 | parsed, err := strconv.ParseInt(partitionVariables.BoardVendorBootimagePartitionSize, 0, 64) |
| 120 | if err != nil { |
| 121 | ctx.ModuleErrorf("BOARD_VENDOR_BOOTIMAGE_PARTITION_SIZE must be an int, got %s", partitionVariables.BoardVendorBootimagePartitionSize) |
| 122 | } |
| 123 | partitionSize = &parsed |
| 124 | } |
| 125 | |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 126 | ctx.CreateModule( |
| 127 | filesystem.BootimgFactory, |
| 128 | &filesystem.BootimgProperties{ |
Jihoon Kang | 2f0d193 | 2025-01-17 19:22:44 +0000 | [diff] [blame] | 129 | Boot_image_type: proptools.StringPtr("vendor_boot"), |
| 130 | Ramdisk_module: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor_ramdisk")), |
| 131 | Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion), |
| 132 | Partition_size: partitionSize, |
| 133 | Use_avb: avbInfo.avbEnable, |
| 134 | Avb_mode: avbInfo.avbMode, |
| 135 | Avb_private_key: avbInfo.avbkeyFilegroup, |
| 136 | Avb_rollback_index: avbInfo.avbRollbackIndex, |
| 137 | Avb_rollback_index_location: avbInfo.avbRollbackIndexLocation, |
| 138 | Dtb_prebuilt: dtbPrebuilt, |
| 139 | Cmdline: cmdline, |
| 140 | Bootconfig: vendorBootConfigImg, |
| 141 | Stem: proptools.StringPtr("vendor_boot.img"), |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 142 | }, |
| 143 | &struct { |
Jihoon Kang | 2f0d193 | 2025-01-17 19:22:44 +0000 | [diff] [blame] | 144 | Name *string |
| 145 | Visibility []string |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 146 | }{ |
Jihoon Kang | 2f0d193 | 2025-01-17 19:22:44 +0000 | [diff] [blame] | 147 | Name: proptools.StringPtr(bootImageName), |
| 148 | Visibility: []string{"//visibility:public"}, |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 149 | }, |
| 150 | ) |
| 151 | return true |
| 152 | } |
| 153 | |
Jihoon Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 154 | func createInitBootImage(ctx android.LoadHookContext) bool { |
| 155 | partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
| 156 | |
| 157 | bootImageName := generatedModuleNameForPartition(ctx.Config(), "init_boot") |
| 158 | |
Cole Faust | 336b3ba | 2024-11-19 16:34:29 -0800 | [diff] [blame] | 159 | var securityPatch *string |
| 160 | if partitionVariables.InitBootSecurityPatch != "" { |
| 161 | securityPatch = &partitionVariables.InitBootSecurityPatch |
| 162 | } else if partitionVariables.BootSecurityPatch != "" { |
| 163 | securityPatch = &partitionVariables.BootSecurityPatch |
| 164 | } |
| 165 | |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [diff] [blame] | 166 | var partitionSize *int64 |
| 167 | if partitionVariables.BoardInitBootimagePartitionSize != "" { |
| 168 | // Base of zero will allow base 10 or base 16 if starting with 0x |
| 169 | parsed, err := strconv.ParseInt(partitionVariables.BoardInitBootimagePartitionSize, 0, 64) |
| 170 | if err != nil { |
| 171 | panic(fmt.Sprintf("BOARD_INIT_BOOT_IMAGE_PARTITION_SIZE must be an int, got %s", partitionVariables.BoardInitBootimagePartitionSize)) |
| 172 | } |
| 173 | partitionSize = &parsed |
| 174 | } |
| 175 | |
| 176 | avbInfo := getAvbInfo(ctx.Config(), "init_boot") |
| 177 | |
Jihoon Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 178 | ctx.CreateModule( |
| 179 | filesystem.BootimgFactory, |
| 180 | &filesystem.BootimgProperties{ |
Jihoon Kang | 2f0d193 | 2025-01-17 19:22:44 +0000 | [diff] [blame] | 181 | Boot_image_type: proptools.StringPtr("init_boot"), |
| 182 | Ramdisk_module: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "ramdisk")), |
| 183 | Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion), |
| 184 | Security_patch: securityPatch, |
| 185 | Partition_size: partitionSize, |
| 186 | Use_avb: avbInfo.avbEnable, |
| 187 | Avb_mode: avbInfo.avbMode, |
| 188 | Avb_private_key: avbInfo.avbkeyFilegroup, |
| 189 | Avb_rollback_index: avbInfo.avbRollbackIndex, |
| 190 | Avb_rollback_index_location: avbInfo.avbRollbackIndexLocation, |
| 191 | Avb_algorithm: avbInfo.avbAlgorithm, |
| 192 | Stem: proptools.StringPtr("init_boot.img"), |
Jihoon Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 193 | }, |
| 194 | &struct { |
Jihoon Kang | 2f0d193 | 2025-01-17 19:22:44 +0000 | [diff] [blame] | 195 | Name *string |
| 196 | Visibility []string |
Jihoon Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 197 | }{ |
Jihoon Kang | 2f0d193 | 2025-01-17 19:22:44 +0000 | [diff] [blame] | 198 | Name: proptools.StringPtr(bootImageName), |
| 199 | Visibility: []string{"//visibility:public"}, |
Jihoon Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 200 | }, |
| 201 | ) |
| 202 | return true |
| 203 | } |
| 204 | |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 205 | // Returns the equivalent of the BUILDING_BOOT_IMAGE variable in make. Derived from this logic: |
| 206 | // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=458;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0 |
| 207 | func buildingBootImage(partitionVars android.PartitionVariables) bool { |
| 208 | if partitionVars.BoardUsesRecoveryAsBoot { |
| 209 | return false |
| 210 | } |
| 211 | |
| 212 | if partitionVars.ProductBuildBootImage { |
| 213 | return true |
| 214 | } |
| 215 | |
| 216 | if len(partitionVars.BoardPrebuiltBootimage) > 0 { |
| 217 | return false |
| 218 | } |
| 219 | |
| 220 | if len(partitionVars.BoardBootimagePartitionSize) > 0 { |
| 221 | return true |
| 222 | } |
| 223 | |
| 224 | // TODO: return true if BOARD_KERNEL_BINARIES is set and has a *_BOOTIMAGE_PARTITION_SIZE |
| 225 | // variable. However, I don't think BOARD_KERNEL_BINARIES is ever set in practice. |
| 226 | |
| 227 | return false |
| 228 | } |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 229 | |
| 230 | // Returns the equivalent of the BUILDING_VENDOR_BOOT_IMAGE variable in make. Derived from this logic: |
| 231 | // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=518;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0 |
| 232 | func buildingVendorBootImage(partitionVars android.PartitionVariables) bool { |
| 233 | if v, exists := boardBootHeaderVersion(partitionVars); exists && v >= 3 { |
| 234 | x := partitionVars.ProductBuildVendorBootImage |
| 235 | if x == "" || x == "true" { |
| 236 | return true |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | return false |
| 241 | } |
| 242 | |
Jihoon Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 243 | // Derived from: https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=480;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0 |
| 244 | func buildingInitBootImage(partitionVars android.PartitionVariables) bool { |
| 245 | if !partitionVars.ProductBuildInitBootImage { |
| 246 | if partitionVars.BoardUsesRecoveryAsBoot || len(partitionVars.BoardPrebuiltInitBootimage) > 0 { |
| 247 | return false |
| 248 | } else if len(partitionVars.BoardInitBootimagePartitionSize) > 0 { |
| 249 | return true |
| 250 | } |
| 251 | } else { |
| 252 | if partitionVars.BoardUsesRecoveryAsBoot { |
| 253 | panic("PRODUCT_BUILD_INIT_BOOT_IMAGE is true, but so is BOARD_USES_RECOVERY_AS_BOOT. Use only one option.") |
| 254 | } |
| 255 | return true |
| 256 | } |
| 257 | return false |
| 258 | } |
| 259 | |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 260 | func boardBootHeaderVersion(partitionVars android.PartitionVariables) (int, bool) { |
| 261 | if len(partitionVars.BoardBootHeaderVersion) == 0 { |
| 262 | return 0, false |
| 263 | } |
| 264 | v, err := strconv.ParseInt(partitionVars.BoardBootHeaderVersion, 10, 32) |
| 265 | if err != nil { |
| 266 | panic(fmt.Sprintf("BOARD_BOOT_HEADER_VERSION must be an int, got: %q", partitionVars.BoardBootHeaderVersion)) |
| 267 | } |
| 268 | return int(v), true |
| 269 | } |
Jihoon Kang | 70c1c68 | 2024-11-20 23:58:38 +0000 | [diff] [blame] | 270 | |
| 271 | type dtbImg struct { |
| 272 | // whether to include the dtb image in boot image |
| 273 | include bool |
| 274 | |
| 275 | // name of the generated dtb image filegroup name |
| 276 | name string |
| 277 | |
| 278 | // type of the boot image that the dtb image argument should be specified |
| 279 | imgType string |
| 280 | } |
| 281 | |
| 282 | func createDtbImgFilegroup(ctx android.LoadHookContext) dtbImg { |
| 283 | partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
| 284 | if !partitionVars.BoardIncludeDtbInBootimg { |
| 285 | return dtbImg{include: false} |
| 286 | } |
| 287 | for _, copyFilePair := range partitionVars.ProductCopyFiles { |
| 288 | srcDestList := strings.Split(copyFilePair, ":") |
| 289 | if len(srcDestList) < 2 { |
| 290 | ctx.ModuleErrorf("PRODUCT_COPY_FILES must follow the format \"src:dest\", got: %s", copyFilePair) |
| 291 | } |
| 292 | if srcDestList[1] == "dtb.img" { |
| 293 | moduleName := generatedModuleName(ctx.Config(), "dtb_img_filegroup") |
| 294 | ctx.CreateModuleInDirectory( |
| 295 | android.FileGroupFactory, |
| 296 | filepath.Dir(srcDestList[0]), |
| 297 | &struct { |
| 298 | Name *string |
| 299 | Srcs []string |
| 300 | }{ |
| 301 | Name: proptools.StringPtr(moduleName), |
| 302 | Srcs: []string{filepath.Base(srcDestList[1])}, |
| 303 | }, |
| 304 | ) |
| 305 | imgType := "vendor_boot" |
| 306 | if !buildingVendorBootImage(partitionVars) { |
| 307 | imgType = "boot" |
| 308 | } |
| 309 | return dtbImg{include: true, name: moduleName, imgType: imgType} |
| 310 | } |
| 311 | } |
| 312 | return dtbImg{include: false} |
| 313 | } |
Jihoon Kang | 4004cc6 | 2024-11-21 23:57:19 +0000 | [diff] [blame] | 314 | |
| 315 | func createVendorBootConfigImg(ctx android.LoadHookContext) (string, bool) { |
| 316 | partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
| 317 | bootconfig := partitionVars.InternalBootconfig |
| 318 | bootconfigFile := partitionVars.InternalBootconfigFile |
| 319 | if len(bootconfig) == 0 && len(bootconfigFile) == 0 { |
| 320 | return "", false |
| 321 | } |
| 322 | |
| 323 | vendorBootconfigImgModuleName := generatedModuleName(ctx.Config(), "vendor_bootconfig_image") |
| 324 | |
| 325 | ctx.CreateModule( |
| 326 | filesystem.BootconfigModuleFactory, |
| 327 | &struct { |
| 328 | Name *string |
| 329 | Boot_config []string |
| 330 | Boot_config_file *string |
| 331 | }{ |
| 332 | Name: proptools.StringPtr(vendorBootconfigImgModuleName), |
| 333 | Boot_config: bootconfig, |
| 334 | Boot_config_file: proptools.StringPtr(bootconfigFile), |
| 335 | }, |
| 336 | ) |
| 337 | |
| 338 | return vendorBootconfigImgModuleName, true |
| 339 | } |