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" |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 9 | |
| 10 | "github.com/google/blueprint/proptools" |
| 11 | ) |
| 12 | |
| 13 | func createBootImage(ctx android.LoadHookContext) bool { |
| 14 | partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
| 15 | |
| 16 | if partitionVariables.TargetKernelPath == "" { |
| 17 | // There are potentially code paths that don't set TARGET_KERNEL_PATH |
| 18 | return false |
| 19 | } |
| 20 | |
| 21 | kernelDir := filepath.Dir(partitionVariables.TargetKernelPath) |
| 22 | kernelBase := filepath.Base(partitionVariables.TargetKernelPath) |
| 23 | kernelFilegroupName := generatedModuleName(ctx.Config(), "kernel") |
| 24 | |
| 25 | ctx.CreateModuleInDirectory( |
| 26 | android.FileGroupFactory, |
| 27 | kernelDir, |
| 28 | &struct { |
| 29 | Name *string |
| 30 | Srcs []string |
| 31 | Visibility []string |
| 32 | }{ |
| 33 | Name: proptools.StringPtr(kernelFilegroupName), |
| 34 | Srcs: []string{kernelBase}, |
| 35 | Visibility: []string{"//visibility:public"}, |
| 36 | }, |
| 37 | ) |
| 38 | |
Cole Faust | 26bdac5 | 2024-11-19 13:37:53 -0800 | [diff] [blame] | 39 | var partitionSize *int64 |
| 40 | if partitionVariables.BoardBootimagePartitionSize != "" { |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [diff] [blame^] | 41 | // Base of zero will allow base 10 or base 16 if starting with 0x |
| 42 | parsed, err := strconv.ParseInt(partitionVariables.BoardBootimagePartitionSize, 0, 64) |
Cole Faust | 26bdac5 | 2024-11-19 13:37:53 -0800 | [diff] [blame] | 43 | if err != nil { |
| 44 | panic(fmt.Sprintf("BOARD_BOOTIMAGE_PARTITION_SIZE must be an int, got %s", partitionVariables.BoardBootimagePartitionSize)) |
| 45 | } |
| 46 | partitionSize = &parsed |
| 47 | } |
| 48 | |
Cole Faust | 336b3ba | 2024-11-19 16:34:29 -0800 | [diff] [blame] | 49 | var securityPatch *string |
| 50 | if partitionVariables.BootSecurityPatch != "" { |
| 51 | securityPatch = &partitionVariables.BootSecurityPatch |
| 52 | } |
| 53 | |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [diff] [blame^] | 54 | avbInfo := getAvbInfo(ctx.Config(), "boot") |
| 55 | |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 56 | bootImageName := generatedModuleNameForPartition(ctx.Config(), "boot") |
| 57 | |
| 58 | ctx.CreateModule( |
| 59 | filesystem.BootimgFactory, |
| 60 | &filesystem.BootimgProperties{ |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [diff] [blame^] | 61 | Kernel_prebuilt: proptools.StringPtr(":" + kernelFilegroupName), |
| 62 | Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion), |
| 63 | Partition_size: partitionSize, |
| 64 | Use_avb: avbInfo.avbEnable, |
| 65 | Avb_mode: avbInfo.avbMode, |
| 66 | Avb_private_key: avbInfo.avbkeyFilegroup, |
| 67 | Avb_rollback_index: avbInfo.avbRollbackIndex, |
| 68 | Avb_algorithm: avbInfo.avbAlgorithm, |
| 69 | Security_patch: securityPatch, |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 70 | }, |
| 71 | &struct { |
| 72 | Name *string |
| 73 | }{ |
| 74 | Name: proptools.StringPtr(bootImageName), |
| 75 | }, |
| 76 | ) |
| 77 | return true |
| 78 | } |
| 79 | |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 80 | func createVendorBootImage(ctx android.LoadHookContext) bool { |
| 81 | partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
| 82 | |
| 83 | bootImageName := generatedModuleNameForPartition(ctx.Config(), "vendor_boot") |
| 84 | |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [diff] [blame^] | 85 | avbInfo := getAvbInfo(ctx.Config(), "vendor_boot") |
| 86 | |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 87 | ctx.CreateModule( |
| 88 | filesystem.BootimgFactory, |
| 89 | &filesystem.BootimgProperties{ |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [diff] [blame^] | 90 | Boot_image_type: proptools.StringPtr("vendor_boot"), |
| 91 | Ramdisk_module: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor_ramdisk")), |
| 92 | Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion), |
| 93 | Use_avb: avbInfo.avbEnable, |
| 94 | Avb_mode: avbInfo.avbMode, |
| 95 | Avb_private_key: avbInfo.avbkeyFilegroup, |
| 96 | Avb_rollback_index: avbInfo.avbRollbackIndex, |
| 97 | Avb_algorithm: avbInfo.avbAlgorithm, |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 98 | }, |
| 99 | &struct { |
| 100 | Name *string |
| 101 | }{ |
| 102 | Name: proptools.StringPtr(bootImageName), |
| 103 | }, |
| 104 | ) |
| 105 | return true |
| 106 | } |
| 107 | |
Jihoon Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 108 | func createInitBootImage(ctx android.LoadHookContext) bool { |
| 109 | partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
| 110 | |
| 111 | bootImageName := generatedModuleNameForPartition(ctx.Config(), "init_boot") |
| 112 | |
Cole Faust | 336b3ba | 2024-11-19 16:34:29 -0800 | [diff] [blame] | 113 | var securityPatch *string |
| 114 | if partitionVariables.InitBootSecurityPatch != "" { |
| 115 | securityPatch = &partitionVariables.InitBootSecurityPatch |
| 116 | } else if partitionVariables.BootSecurityPatch != "" { |
| 117 | securityPatch = &partitionVariables.BootSecurityPatch |
| 118 | } |
| 119 | |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [diff] [blame^] | 120 | var partitionSize *int64 |
| 121 | if partitionVariables.BoardInitBootimagePartitionSize != "" { |
| 122 | // Base of zero will allow base 10 or base 16 if starting with 0x |
| 123 | parsed, err := strconv.ParseInt(partitionVariables.BoardInitBootimagePartitionSize, 0, 64) |
| 124 | if err != nil { |
| 125 | panic(fmt.Sprintf("BOARD_INIT_BOOT_IMAGE_PARTITION_SIZE must be an int, got %s", partitionVariables.BoardInitBootimagePartitionSize)) |
| 126 | } |
| 127 | partitionSize = &parsed |
| 128 | } |
| 129 | |
| 130 | avbInfo := getAvbInfo(ctx.Config(), "init_boot") |
| 131 | |
Jihoon Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 132 | ctx.CreateModule( |
| 133 | filesystem.BootimgFactory, |
| 134 | &filesystem.BootimgProperties{ |
Cole Faust | 0c4b415 | 2024-11-20 16:42:53 -0800 | [diff] [blame^] | 135 | Boot_image_type: proptools.StringPtr("init_boot"), |
| 136 | Ramdisk_module: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "ramdisk")), |
| 137 | Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion), |
| 138 | Security_patch: securityPatch, |
| 139 | Partition_size: partitionSize, |
| 140 | Use_avb: avbInfo.avbEnable, |
| 141 | Avb_mode: avbInfo.avbMode, |
| 142 | Avb_private_key: avbInfo.avbkeyFilegroup, |
| 143 | Avb_rollback_index: avbInfo.avbRollbackIndex, |
| 144 | Avb_algorithm: avbInfo.avbAlgorithm, |
Jihoon Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 145 | }, |
| 146 | &struct { |
| 147 | Name *string |
| 148 | }{ |
| 149 | Name: proptools.StringPtr(bootImageName), |
| 150 | }, |
| 151 | ) |
| 152 | return true |
| 153 | } |
| 154 | |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 155 | // Returns the equivalent of the BUILDING_BOOT_IMAGE variable in make. Derived from this logic: |
| 156 | // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=458;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0 |
| 157 | func buildingBootImage(partitionVars android.PartitionVariables) bool { |
| 158 | if partitionVars.BoardUsesRecoveryAsBoot { |
| 159 | return false |
| 160 | } |
| 161 | |
| 162 | if partitionVars.ProductBuildBootImage { |
| 163 | return true |
| 164 | } |
| 165 | |
| 166 | if len(partitionVars.BoardPrebuiltBootimage) > 0 { |
| 167 | return false |
| 168 | } |
| 169 | |
| 170 | if len(partitionVars.BoardBootimagePartitionSize) > 0 { |
| 171 | return true |
| 172 | } |
| 173 | |
| 174 | // TODO: return true if BOARD_KERNEL_BINARIES is set and has a *_BOOTIMAGE_PARTITION_SIZE |
| 175 | // variable. However, I don't think BOARD_KERNEL_BINARIES is ever set in practice. |
| 176 | |
| 177 | return false |
| 178 | } |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 179 | |
| 180 | // Returns the equivalent of the BUILDING_VENDOR_BOOT_IMAGE variable in make. Derived from this logic: |
| 181 | // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=518;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0 |
| 182 | func buildingVendorBootImage(partitionVars android.PartitionVariables) bool { |
| 183 | if v, exists := boardBootHeaderVersion(partitionVars); exists && v >= 3 { |
| 184 | x := partitionVars.ProductBuildVendorBootImage |
| 185 | if x == "" || x == "true" { |
| 186 | return true |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | return false |
| 191 | } |
| 192 | |
Jihoon Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 193 | // Derived from: https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=480;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0 |
| 194 | func buildingInitBootImage(partitionVars android.PartitionVariables) bool { |
| 195 | if !partitionVars.ProductBuildInitBootImage { |
| 196 | if partitionVars.BoardUsesRecoveryAsBoot || len(partitionVars.BoardPrebuiltInitBootimage) > 0 { |
| 197 | return false |
| 198 | } else if len(partitionVars.BoardInitBootimagePartitionSize) > 0 { |
| 199 | return true |
| 200 | } |
| 201 | } else { |
| 202 | if partitionVars.BoardUsesRecoveryAsBoot { |
| 203 | panic("PRODUCT_BUILD_INIT_BOOT_IMAGE is true, but so is BOARD_USES_RECOVERY_AS_BOOT. Use only one option.") |
| 204 | } |
| 205 | return true |
| 206 | } |
| 207 | return false |
| 208 | } |
| 209 | |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 210 | func boardBootHeaderVersion(partitionVars android.PartitionVariables) (int, bool) { |
| 211 | if len(partitionVars.BoardBootHeaderVersion) == 0 { |
| 212 | return 0, false |
| 213 | } |
| 214 | v, err := strconv.ParseInt(partitionVars.BoardBootHeaderVersion, 10, 32) |
| 215 | if err != nil { |
| 216 | panic(fmt.Sprintf("BOARD_BOOT_HEADER_VERSION must be an int, got: %q", partitionVars.BoardBootHeaderVersion)) |
| 217 | } |
| 218 | return int(v), true |
| 219 | } |