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), |
| 45 | Ramdisk_module: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "ramdisk")), |
| 46 | Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion), |
| 47 | }, |
| 48 | &struct { |
| 49 | Name *string |
| 50 | }{ |
| 51 | Name: proptools.StringPtr(bootImageName), |
| 52 | }, |
| 53 | ) |
| 54 | return true |
| 55 | } |
| 56 | |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame^] | 57 | func createVendorBootImage(ctx android.LoadHookContext) bool { |
| 58 | partitionVariables := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse |
| 59 | |
| 60 | bootImageName := generatedModuleNameForPartition(ctx.Config(), "vendor_boot") |
| 61 | |
| 62 | ctx.CreateModule( |
| 63 | filesystem.BootimgFactory, |
| 64 | &filesystem.BootimgProperties{ |
| 65 | Vendor_boot: proptools.BoolPtr(true), |
| 66 | Ramdisk_module: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor_ramdisk")), |
| 67 | Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion), |
| 68 | }, |
| 69 | &struct { |
| 70 | Name *string |
| 71 | }{ |
| 72 | Name: proptools.StringPtr(bootImageName), |
| 73 | }, |
| 74 | ) |
| 75 | return true |
| 76 | } |
| 77 | |
Cole Faust | f2a6e8b | 2024-11-14 10:54:48 -0800 | [diff] [blame] | 78 | // Returns the equivalent of the BUILDING_BOOT_IMAGE variable in make. Derived from this logic: |
| 79 | // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=458;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0 |
| 80 | func buildingBootImage(partitionVars android.PartitionVariables) bool { |
| 81 | if partitionVars.BoardUsesRecoveryAsBoot { |
| 82 | return false |
| 83 | } |
| 84 | |
| 85 | if partitionVars.ProductBuildBootImage { |
| 86 | return true |
| 87 | } |
| 88 | |
| 89 | if len(partitionVars.BoardPrebuiltBootimage) > 0 { |
| 90 | return false |
| 91 | } |
| 92 | |
| 93 | if len(partitionVars.BoardBootimagePartitionSize) > 0 { |
| 94 | return true |
| 95 | } |
| 96 | |
| 97 | // TODO: return true if BOARD_KERNEL_BINARIES is set and has a *_BOOTIMAGE_PARTITION_SIZE |
| 98 | // variable. However, I don't think BOARD_KERNEL_BINARIES is ever set in practice. |
| 99 | |
| 100 | return false |
| 101 | } |
Cole Faust | 24938e2 | 2024-11-18 14:01:58 -0800 | [diff] [blame^] | 102 | |
| 103 | // Returns the equivalent of the BUILDING_VENDOR_BOOT_IMAGE variable in make. Derived from this logic: |
| 104 | // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=518;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0 |
| 105 | func buildingVendorBootImage(partitionVars android.PartitionVariables) bool { |
| 106 | if v, exists := boardBootHeaderVersion(partitionVars); exists && v >= 3 { |
| 107 | x := partitionVars.ProductBuildVendorBootImage |
| 108 | if x == "" || x == "true" { |
| 109 | return true |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return false |
| 114 | } |
| 115 | |
| 116 | func boardBootHeaderVersion(partitionVars android.PartitionVariables) (int, bool) { |
| 117 | if len(partitionVars.BoardBootHeaderVersion) == 0 { |
| 118 | return 0, false |
| 119 | } |
| 120 | v, err := strconv.ParseInt(partitionVars.BoardBootHeaderVersion, 10, 32) |
| 121 | if err != nil { |
| 122 | panic(fmt.Sprintf("BOARD_BOOT_HEADER_VERSION must be an int, got: %q", partitionVars.BoardBootHeaderVersion)) |
| 123 | } |
| 124 | return int(v), true |
| 125 | } |