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 != "" { |
| 41 | parsed, err := strconv.ParseInt(partitionVariables.BoardBootimagePartitionSize, 10, 64) |
| 42 | if err != nil { |
| 43 | panic(fmt.Sprintf("BOARD_BOOTIMAGE_PARTITION_SIZE must be an int, got %s", partitionVariables.BoardBootimagePartitionSize)) |
| 44 | } |
| 45 | partitionSize = &parsed |
| 46 | } |
| 47 | |
Cole Faust | 336b3ba | 2024-11-19 16:34:29 -0800 | [diff] [blame] | 48 | var securityPatch *string |
| 49 | if partitionVariables.BootSecurityPatch != "" { |
| 50 | securityPatch = &partitionVariables.BootSecurityPatch |
| 51 | } |
| 52 | |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 53 | bootImageName := generatedModuleNameForPartition(ctx.Config(), "boot") |
| 54 | |
| 55 | ctx.CreateModule( |
| 56 | filesystem.BootimgFactory, |
| 57 | &filesystem.BootimgProperties{ |
| 58 | Kernel_prebuilt: proptools.StringPtr(":" + kernelFilegroupName), |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 59 | Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion), |
Cole Faust | 26bdac5 | 2024-11-19 13:37:53 -0800 | [diff] [blame] | 60 | Partition_size: partitionSize, |
Cole Faust | 336b3ba | 2024-11-19 16:34:29 -0800 | [diff] [blame] | 61 | Use_avb: &partitionVariables.BoardAvbEnable, |
| 62 | Security_patch: securityPatch, |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 63 | }, |
| 64 | &struct { |
| 65 | Name *string |
| 66 | }{ |
| 67 | Name: proptools.StringPtr(bootImageName), |
| 68 | }, |
| 69 | ) |
| 70 | return true |
| 71 | } |
| 72 | |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 73 | func createVendorBootImage(ctx android.LoadHookContext) bool { |
| 74 | partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
| 75 | |
| 76 | bootImageName := generatedModuleNameForPartition(ctx.Config(), "vendor_boot") |
| 77 | |
| 78 | ctx.CreateModule( |
| 79 | filesystem.BootimgFactory, |
| 80 | &filesystem.BootimgProperties{ |
Jihoon Kang | 96fdba9 | 2024-11-19 22:25:36 +0000 | [diff] [blame] | 81 | Boot_image_type: proptools.StringPtr("vendor_boot"), |
| 82 | Ramdisk_module: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor_ramdisk")), |
| 83 | Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion), |
Cole Faust | 336b3ba | 2024-11-19 16:34:29 -0800 | [diff] [blame] | 84 | Use_avb: &partitionVariables.BoardAvbEnable, |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 85 | }, |
| 86 | &struct { |
| 87 | Name *string |
| 88 | }{ |
| 89 | Name: proptools.StringPtr(bootImageName), |
| 90 | }, |
| 91 | ) |
| 92 | return true |
| 93 | } |
| 94 | |
Jihoon Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 95 | func createInitBootImage(ctx android.LoadHookContext) bool { |
| 96 | partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
| 97 | |
| 98 | bootImageName := generatedModuleNameForPartition(ctx.Config(), "init_boot") |
| 99 | |
Cole Faust | 336b3ba | 2024-11-19 16:34:29 -0800 | [diff] [blame] | 100 | var securityPatch *string |
| 101 | if partitionVariables.InitBootSecurityPatch != "" { |
| 102 | securityPatch = &partitionVariables.InitBootSecurityPatch |
| 103 | } else if partitionVariables.BootSecurityPatch != "" { |
| 104 | securityPatch = &partitionVariables.BootSecurityPatch |
| 105 | } |
| 106 | |
Jihoon Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 107 | ctx.CreateModule( |
| 108 | filesystem.BootimgFactory, |
| 109 | &filesystem.BootimgProperties{ |
Jihoon Kang | 96fdba9 | 2024-11-19 22:25:36 +0000 | [diff] [blame] | 110 | Boot_image_type: proptools.StringPtr("init_boot"), |
| 111 | Ramdisk_module: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "ramdisk")), |
| 112 | Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion), |
Cole Faust | 336b3ba | 2024-11-19 16:34:29 -0800 | [diff] [blame] | 113 | Use_avb: &partitionVariables.BoardAvbEnable, |
| 114 | Security_patch: securityPatch, |
Jihoon Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 115 | }, |
| 116 | &struct { |
| 117 | Name *string |
| 118 | }{ |
| 119 | Name: proptools.StringPtr(bootImageName), |
| 120 | }, |
| 121 | ) |
| 122 | return true |
| 123 | } |
| 124 | |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 125 | // Returns the equivalent of the BUILDING_BOOT_IMAGE variable in make. Derived from this logic: |
| 126 | // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=458;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0 |
| 127 | func buildingBootImage(partitionVars android.PartitionVariables) bool { |
| 128 | if partitionVars.BoardUsesRecoveryAsBoot { |
| 129 | return false |
| 130 | } |
| 131 | |
| 132 | if partitionVars.ProductBuildBootImage { |
| 133 | return true |
| 134 | } |
| 135 | |
| 136 | if len(partitionVars.BoardPrebuiltBootimage) > 0 { |
| 137 | return false |
| 138 | } |
| 139 | |
| 140 | if len(partitionVars.BoardBootimagePartitionSize) > 0 { |
| 141 | return true |
| 142 | } |
| 143 | |
| 144 | // TODO: return true if BOARD_KERNEL_BINARIES is set and has a *_BOOTIMAGE_PARTITION_SIZE |
| 145 | // variable. However, I don't think BOARD_KERNEL_BINARIES is ever set in practice. |
| 146 | |
| 147 | return false |
| 148 | } |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 149 | |
| 150 | // Returns the equivalent of the BUILDING_VENDOR_BOOT_IMAGE variable in make. Derived from this logic: |
| 151 | // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=518;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0 |
| 152 | func buildingVendorBootImage(partitionVars android.PartitionVariables) bool { |
| 153 | if v, exists := boardBootHeaderVersion(partitionVars); exists && v >= 3 { |
| 154 | x := partitionVars.ProductBuildVendorBootImage |
| 155 | if x == "" || x == "true" { |
| 156 | return true |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return false |
| 161 | } |
| 162 | |
Jihoon Kang | 95eb1da | 2024-11-19 20:55:20 +0000 | [diff] [blame] | 163 | // Derived from: https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=480;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0 |
| 164 | func buildingInitBootImage(partitionVars android.PartitionVariables) bool { |
| 165 | if !partitionVars.ProductBuildInitBootImage { |
| 166 | if partitionVars.BoardUsesRecoveryAsBoot || len(partitionVars.BoardPrebuiltInitBootimage) > 0 { |
| 167 | return false |
| 168 | } else if len(partitionVars.BoardInitBootimagePartitionSize) > 0 { |
| 169 | return true |
| 170 | } |
| 171 | } else { |
| 172 | if partitionVars.BoardUsesRecoveryAsBoot { |
| 173 | panic("PRODUCT_BUILD_INIT_BOOT_IMAGE is true, but so is BOARD_USES_RECOVERY_AS_BOOT. Use only one option.") |
| 174 | } |
| 175 | return true |
| 176 | } |
| 177 | return false |
| 178 | } |
| 179 | |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame] | 180 | func boardBootHeaderVersion(partitionVars android.PartitionVariables) (int, bool) { |
| 181 | if len(partitionVars.BoardBootHeaderVersion) == 0 { |
| 182 | return 0, false |
| 183 | } |
| 184 | v, err := strconv.ParseInt(partitionVars.BoardBootHeaderVersion, 10, 32) |
| 185 | if err != nil { |
| 186 | panic(fmt.Sprintf("BOARD_BOOT_HEADER_VERSION must be an int, got: %q", partitionVars.BoardBootHeaderVersion)) |
| 187 | } |
| 188 | return int(v), true |
| 189 | } |