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