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 | |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 64 | ctx.CreateModule( |
| 65 | filesystem.BootimgFactory, |
| 66 | &filesystem.BootimgProperties{ |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [diff] [blame] | 67 | 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 Kang | 70c1c68 | 2024-11-20 23:58:38 +0000 | [diff] [blame^] | 76 | Dtb_prebuilt: dtbPrebuilt, |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 77 | }, |
| 78 | &struct { |
| 79 | Name *string |
| 80 | }{ |
| 81 | Name: proptools.StringPtr(bootImageName), |
| 82 | }, |
| 83 | ) |
| 84 | return true |
| 85 | } |
| 86 | |
Jihoon Kang | 70c1c68 | 2024-11-20 23:58:38 +0000 | [diff] [blame^] | 87 | func createVendorBootImage(ctx android.LoadHookContext, dtbImg dtbImg) bool { |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 88 | partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
| 89 | |
| 90 | bootImageName := generatedModuleNameForPartition(ctx.Config(), "vendor_boot") |
| 91 | |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [diff] [blame] | 92 | avbInfo := getAvbInfo(ctx.Config(), "vendor_boot") |
| 93 | |
Jihoon Kang | 70c1c68 | 2024-11-20 23:58:38 +0000 | [diff] [blame^] | 94 | var dtbPrebuilt *string |
| 95 | if dtbImg.include && dtbImg.imgType == "vendor_boot" { |
| 96 | dtbPrebuilt = proptools.StringPtr(":" + dtbImg.name) |
| 97 | } |
| 98 | |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 99 | ctx.CreateModule( |
| 100 | filesystem.BootimgFactory, |
| 101 | &filesystem.BootimgProperties{ |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [diff] [blame] | 102 | 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 Kang | 70c1c68 | 2024-11-20 23:58:38 +0000 | [diff] [blame^] | 110 | Dtb_prebuilt: dtbPrebuilt, |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 111 | }, |
| 112 | &struct { |
| 113 | Name *string |
| 114 | }{ |
| 115 | Name: proptools.StringPtr(bootImageName), |
| 116 | }, |
| 117 | ) |
| 118 | return true |
| 119 | } |
| 120 | |
Jihoon Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 121 | func createInitBootImage(ctx android.LoadHookContext) bool { |
| 122 | partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
| 123 | |
| 124 | bootImageName := generatedModuleNameForPartition(ctx.Config(), "init_boot") |
| 125 | |
Cole Faust | 336b3ba | 2024-11-19 16:34:29 -0800 | [diff] [blame] | 126 | var securityPatch *string |
| 127 | if partitionVariables.InitBootSecurityPatch != "" { |
| 128 | securityPatch = &partitionVariables.InitBootSecurityPatch |
| 129 | } else if partitionVariables.BootSecurityPatch != "" { |
| 130 | securityPatch = &partitionVariables.BootSecurityPatch |
| 131 | } |
| 132 | |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [diff] [blame] | 133 | 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 Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 145 | ctx.CreateModule( |
| 146 | filesystem.BootimgFactory, |
| 147 | &filesystem.BootimgProperties{ |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [diff] [blame] | 148 | 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 Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 158 | }, |
| 159 | &struct { |
| 160 | Name *string |
| 161 | }{ |
| 162 | Name: proptools.StringPtr(bootImageName), |
| 163 | }, |
| 164 | ) |
| 165 | return true |
| 166 | } |
| 167 | |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 168 | // 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 |
| 170 | func 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 Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 192 | |
| 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 |
| 195 | func 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 Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 206 | // Derived from: https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=480;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0 |
| 207 | func 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 Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 223 | func 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 Kang | 70c1c68 | 2024-11-20 23:58:38 +0000 | [diff] [blame^] | 233 | |
| 234 | type 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 | |
| 245 | func 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 | } |