blob: 6fd4b7b563e1afc0e0e0b7e6cdb42905b1e3803e [file] [log] [blame]
Cole Faustf2a6e8b2024-11-14 10:54:48 -08001package fsgen
2
3import (
4 "android/soong/android"
5 "android/soong/filesystem"
Cole Faust24938e22024-11-18 14:01:58 -08006 "fmt"
Cole Faustf2a6e8b2024-11-14 10:54:48 -08007 "path/filepath"
Cole Faust24938e22024-11-18 14:01:58 -08008 "strconv"
Cole Faustf2a6e8b2024-11-14 10:54:48 -08009
10 "github.com/google/blueprint/proptools"
11)
12
13func 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 Faust26bdac52024-11-19 13:37:53 -080039 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 Faustf2a6e8b2024-11-14 10:54:48 -080048 bootImageName := generatedModuleNameForPartition(ctx.Config(), "boot")
49
50 ctx.CreateModule(
51 filesystem.BootimgFactory,
52 &filesystem.BootimgProperties{
53 Kernel_prebuilt: proptools.StringPtr(":" + kernelFilegroupName),
Cole Faustf2a6e8b2024-11-14 10:54:48 -080054 Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion),
Cole Faust26bdac52024-11-19 13:37:53 -080055 Partition_size: partitionSize,
Cole Faustf2a6e8b2024-11-14 10:54:48 -080056 },
57 &struct {
58 Name *string
59 }{
60 Name: proptools.StringPtr(bootImageName),
61 },
62 )
63 return true
64}
65
Cole Faust24938e22024-11-18 14:01:58 -080066func 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{
74 Vendor_boot: proptools.BoolPtr(true),
75 Ramdisk_module: proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor_ramdisk")),
76 Header_version: proptools.StringPtr(partitionVariables.BoardBootHeaderVersion),
77 },
78 &struct {
79 Name *string
80 }{
81 Name: proptools.StringPtr(bootImageName),
82 },
83 )
84 return true
85}
86
Cole Faustf2a6e8b2024-11-14 10:54:48 -080087// Returns the equivalent of the BUILDING_BOOT_IMAGE variable in make. Derived from this logic:
88// https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=458;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0
89func buildingBootImage(partitionVars android.PartitionVariables) bool {
90 if partitionVars.BoardUsesRecoveryAsBoot {
91 return false
92 }
93
94 if partitionVars.ProductBuildBootImage {
95 return true
96 }
97
98 if len(partitionVars.BoardPrebuiltBootimage) > 0 {
99 return false
100 }
101
102 if len(partitionVars.BoardBootimagePartitionSize) > 0 {
103 return true
104 }
105
106 // TODO: return true if BOARD_KERNEL_BINARIES is set and has a *_BOOTIMAGE_PARTITION_SIZE
107 // variable. However, I don't think BOARD_KERNEL_BINARIES is ever set in practice.
108
109 return false
110}
Cole Faust24938e22024-11-18 14:01:58 -0800111
112// Returns the equivalent of the BUILDING_VENDOR_BOOT_IMAGE variable in make. Derived from this logic:
113// https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/board_config.mk;l=518;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0
114func buildingVendorBootImage(partitionVars android.PartitionVariables) bool {
115 if v, exists := boardBootHeaderVersion(partitionVars); exists && v >= 3 {
116 x := partitionVars.ProductBuildVendorBootImage
117 if x == "" || x == "true" {
118 return true
119 }
120 }
121
122 return false
123}
124
125func boardBootHeaderVersion(partitionVars android.PartitionVariables) (int, bool) {
126 if len(partitionVars.BoardBootHeaderVersion) == 0 {
127 return 0, false
128 }
129 v, err := strconv.ParseInt(partitionVars.BoardBootHeaderVersion, 10, 32)
130 if err != nil {
131 panic(fmt.Sprintf("BOARD_BOOT_HEADER_VERSION must be an int, got: %q", partitionVars.BoardBootHeaderVersion))
132 }
133 return int(v), true
134}