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