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{ |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [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_algorithm: avbInfo.avbAlgorithm, |
| 80 | Security_patch: securityPatch, |
Jihoon Kang | 70c1c68 | 2024-11-20 23:58:38 +0000 | [diff] [blame] | 81 | Dtb_prebuilt: dtbPrebuilt, |
Jihoon Kang | 1821b5e | 2024-11-21 20:49:55 +0000 | [diff] [blame] | 82 | Cmdline: cmdline, |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 83 | }, |
| 84 | &struct { |
| 85 | Name *string |
| 86 | }{ |
| 87 | Name: proptools.StringPtr(bootImageName), |
| 88 | }, |
| 89 | ) |
| 90 | return true |
| 91 | } |
| 92 | |
Jihoon Kang | 70c1c68 | 2024-11-20 23:58:38 +0000 | [diff] [blame] | 93 | func createVendorBootImage(ctx android.LoadHookContext, dtbImg dtbImg) bool { |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 94 | partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
| 95 | |
| 96 | bootImageName := generatedModuleNameForPartition(ctx.Config(), "vendor_boot") |
| 97 | |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [diff] [blame] | 98 | avbInfo := getAvbInfo(ctx.Config(), "vendor_boot") |
| 99 | |
Jihoon Kang | 70c1c68 | 2024-11-20 23:58:38 +0000 | [diff] [blame] | 100 | var dtbPrebuilt *string |
| 101 | if dtbImg.include && dtbImg.imgType == "vendor_boot" { |
| 102 | dtbPrebuilt = proptools.StringPtr(":" + dtbImg.name) |
| 103 | } |
| 104 | |
Jihoon Kang | 1821b5e | 2024-11-21 20:49:55 +0000 | [diff] [blame] | 105 | cmdline := partitionVariables.InternalKernelCmdline |
| 106 | |
Jihoon Kang | 4004cc6 | 2024-11-21 23:57:19 +0000 | [diff] [blame] | 107 | var vendorBootConfigImg *string |
| 108 | if name, ok := createVendorBootConfigImg(ctx); ok { |
| 109 | vendorBootConfigImg = proptools.StringPtr(":" + name) |
| 110 | } |
| 111 | |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 112 | ctx.CreateModule( |
| 113 | filesystem.BootimgFactory, |
| 114 | &filesystem.BootimgProperties{ |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [diff] [blame] | 115 | 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 Kang | 70c1c68 | 2024-11-20 23:58:38 +0000 | [diff] [blame] | 122 | Dtb_prebuilt: dtbPrebuilt, |
Jihoon Kang | 1821b5e | 2024-11-21 20:49:55 +0000 | [diff] [blame] | 123 | Cmdline: cmdline, |
Jihoon Kang | 4004cc6 | 2024-11-21 23:57:19 +0000 | [diff] [blame] | 124 | Bootconfig: vendorBootConfigImg, |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 125 | }, |
| 126 | &struct { |
| 127 | Name *string |
| 128 | }{ |
| 129 | Name: proptools.StringPtr(bootImageName), |
| 130 | }, |
| 131 | ) |
| 132 | return true |
| 133 | } |
| 134 | |
Jihoon Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 135 | func createInitBootImage(ctx android.LoadHookContext) bool { |
| 136 | partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
| 137 | |
| 138 | bootImageName := generatedModuleNameForPartition(ctx.Config(), "init_boot") |
| 139 | |
Cole Faust | 336b3ba | 2024-11-19 16:34:29 -0800 | [diff] [blame] | 140 | var securityPatch *string |
| 141 | if partitionVariables.InitBootSecurityPatch != "" { |
| 142 | securityPatch = &partitionVariables.InitBootSecurityPatch |
| 143 | } else if partitionVariables.BootSecurityPatch != "" { |
| 144 | securityPatch = &partitionVariables.BootSecurityPatch |
| 145 | } |
| 146 | |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [diff] [blame] | 147 | 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 Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 159 | ctx.CreateModule( |
| 160 | filesystem.BootimgFactory, |
| 161 | &filesystem.BootimgProperties{ |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [diff] [blame] | 162 | 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 Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 172 | }, |
| 173 | &struct { |
| 174 | Name *string |
| 175 | }{ |
| 176 | Name: proptools.StringPtr(bootImageName), |
| 177 | }, |
| 178 | ) |
| 179 | return true |
| 180 | } |
| 181 | |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 182 | // 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 |
| 184 | func 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 Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 206 | |
| 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 |
| 209 | func 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 Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 220 | // Derived from: https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=480;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0 |
| 221 | func 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 Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 237 | func 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 Kang | 70c1c68 | 2024-11-20 23:58:38 +0000 | [diff] [blame] | 247 | |
| 248 | type 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 | |
| 259 | func 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 Kang | 4004cc6 | 2024-11-21 23:57:19 +0000 | [diff] [blame] | 291 | |
| 292 | func 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 | } |