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