blob: d341a0c21bf1d53a6229fe0a90a016f3bf39df66 [file] [log] [blame]
Jihoon Kangf2c53982024-10-09 17:32:52 +00001// Copyright (C) 2024 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package filesystem
16
17import (
Spandan Das258c08f2025-01-08 23:30:45 +000018 "strings"
Cole Faustb55a41c2025-01-09 16:53:58 -080019 "sync/atomic"
Spandan Das258c08f2025-01-08 23:30:45 +000020
Jihoon Kangf2c53982024-10-09 17:32:52 +000021 "android/soong/android"
22
23 "github.com/google/blueprint"
24 "github.com/google/blueprint/proptools"
25)
26
27type PartitionNameProperties struct {
Cole Faust77243442025-01-09 15:49:15 -080028 // Name of the super partition filesystem module
29 Super_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000030 // Name of the boot partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000031 Boot_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000032 // Name of the vendor boot partition filesystem module
33 Vendor_boot_partition_name *string
34 // Name of the init boot partition filesystem module
35 Init_boot_partition_name *string
36 // Name of the system partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000037 System_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000038 // Name of the system_ext partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000039 System_ext_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000040 // Name of the product partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000041 Product_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000042 // Name of the vendor partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000043 Vendor_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000044 // Name of the odm partition filesystem module
Spandan Dasc5717162024-11-01 18:33:57 +000045 Odm_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000046 // Name of the recovery partition filesystem module
47 Recovery_partition_name *string
Cole Faust3552eb62024-11-06 18:07:26 -080048 // The vbmeta partition and its "chained" partitions
49 Vbmeta_partitions []string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000050 // Name of the userdata partition filesystem module
mrziwang23ba8762024-11-07 16:21:53 -080051 Userdata_partition_name *string
Spandan Dasa0394002025-01-07 18:38:34 +000052 // Name of the system_dlkm partition filesystem module
53 System_dlkm_partition_name *string
54 // Name of the vendor_dlkm partition filesystem module
55 Vendor_dlkm_partition_name *string
56 // Name of the odm_dlkm partition filesystem module
57 Odm_dlkm_partition_name *string
Jihoon Kangf2c53982024-10-09 17:32:52 +000058}
59
Jihoon Kang3be17162025-01-09 20:51:54 +000060type DeviceProperties struct {
61 // Path to the prebuilt bootloader that would be copied to PRODUCT_OUT
62 Bootloader *string `android:"path"`
63}
64
Jihoon Kangf2c53982024-10-09 17:32:52 +000065type androidDevice struct {
66 android.ModuleBase
67
68 partitionProps PartitionNameProperties
Jihoon Kang3be17162025-01-09 20:51:54 +000069
70 deviceProps DeviceProperties
71
72 // copyToProductOutTimestamp for copying necessary files to PRODUCT_OUT
73 copyToProductOutTimestamp android.WritablePath
Jihoon Kangf2c53982024-10-09 17:32:52 +000074}
75
76func AndroidDeviceFactory() android.Module {
77 module := &androidDevice{}
Jihoon Kang3be17162025-01-09 20:51:54 +000078 module.AddProperties(&module.partitionProps, &module.deviceProps)
Cole Faust341d5f12025-01-07 15:32:38 -080079 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jihoon Kangf2c53982024-10-09 17:32:52 +000080 return module
81}
82
Cole Faustb55a41c2025-01-09 16:53:58 -080083var numAutogeneratedAndroidDevicesOnceKey android.OnceKey = android.NewOnceKey("num_auto_generated_anroid_devices")
84
Jihoon Kangf2c53982024-10-09 17:32:52 +000085type partitionDepTagType struct {
86 blueprint.BaseDependencyTag
87}
88
Cole Faust77243442025-01-09 15:49:15 -080089type superPartitionDepTagType struct {
90 blueprint.BaseDependencyTag
91}
92
93var superPartitionDepTag superPartitionDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +000094var filesystemDepTag partitionDepTagType
95
96func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
97 addDependencyIfDefined := func(dep *string) {
98 if dep != nil {
Cole Faust341d5f12025-01-07 15:32:38 -080099 ctx.AddDependency(ctx.Module(), filesystemDepTag, proptools.String(dep))
Jihoon Kangf2c53982024-10-09 17:32:52 +0000100 }
101 }
102
Cole Faust77243442025-01-09 15:49:15 -0800103 if a.partitionProps.Super_partition_name != nil {
104 ctx.AddDependency(ctx.Module(), superPartitionDepTag, *a.partitionProps.Super_partition_name)
105 }
Jihoon Kangf2c53982024-10-09 17:32:52 +0000106 addDependencyIfDefined(a.partitionProps.Boot_partition_name)
Jihoon Kang9e087002025-01-08 19:12:23 +0000107 addDependencyIfDefined(a.partitionProps.Init_boot_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000108 addDependencyIfDefined(a.partitionProps.Vendor_boot_partition_name)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000109 addDependencyIfDefined(a.partitionProps.System_partition_name)
110 addDependencyIfDefined(a.partitionProps.System_ext_partition_name)
111 addDependencyIfDefined(a.partitionProps.Product_partition_name)
112 addDependencyIfDefined(a.partitionProps.Vendor_partition_name)
Spandan Dasc5717162024-11-01 18:33:57 +0000113 addDependencyIfDefined(a.partitionProps.Odm_partition_name)
mrziwang23ba8762024-11-07 16:21:53 -0800114 addDependencyIfDefined(a.partitionProps.Userdata_partition_name)
Spandan Dasa0394002025-01-07 18:38:34 +0000115 addDependencyIfDefined(a.partitionProps.System_dlkm_partition_name)
116 addDependencyIfDefined(a.partitionProps.Vendor_dlkm_partition_name)
117 addDependencyIfDefined(a.partitionProps.Odm_dlkm_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000118 addDependencyIfDefined(a.partitionProps.Recovery_partition_name)
Cole Faust3552eb62024-11-06 18:07:26 -0800119 for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions {
120 ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition)
121 }
Jihoon Kangf2c53982024-10-09 17:32:52 +0000122}
123
Jihoon Kang3be17162025-01-09 20:51:54 +0000124func (a *androidDevice) copyToProductOut(ctx android.ModuleContext, builder *android.RuleBuilder, src android.Path, dest string) {
125 destPath := android.PathForModuleInPartitionInstall(ctx, "").Join(ctx, dest)
126 builder.Command().Text("rsync").Flag("-a").Flag("--checksum").Input(src).Text(destPath.String())
127}
128
129func (a *androidDevice) copyFilesToProductOut(ctx android.ModuleContext) {
130 a.copyToProductOutTimestamp = android.PathForModuleOut(ctx, "timestamp")
131 builder := android.NewRuleBuilder(pctx, ctx)
132 builder.Command().Text("touch").Output(a.copyToProductOutTimestamp)
133
134 // List all individual files to be copied to PRODUCT_OUT here
135 if a.deviceProps.Bootloader != nil {
136 a.copyToProductOut(ctx, builder, android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Bootloader)), "bootloader")
137 }
138
139 builder.Build("copy_to_product_out", "Copy files to PRODUCT_OUT")
140}
141
Jihoon Kangf2c53982024-10-09 17:32:52 +0000142func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust44080412024-12-20 14:17:07 -0800143 a.buildTargetFilesZip(ctx)
mrziwang2fd33a72025-01-08 12:22:08 -0800144 var deps []android.Path
Cole Faust77243442025-01-09 15:49:15 -0800145 if proptools.String(a.partitionProps.Super_partition_name) != "" {
146 superImage := ctx.GetDirectDepWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
147 if info, ok := android.OtherModuleProvider(ctx, superImage, SuperImageProvider); ok {
148 assertUnset := func(prop *string, propName string) {
149 if prop != nil && *prop != "" {
150 ctx.PropertyErrorf(propName, "Cannot be set because it's already part of the super image")
151 }
152 }
153 for _, subPartitionType := range android.SortedKeys(info.SubImageInfo) {
154 switch subPartitionType {
155 case "system":
156 assertUnset(a.partitionProps.System_partition_name, "system_partition_name")
157 case "system_ext":
158 assertUnset(a.partitionProps.System_ext_partition_name, "system_ext_partition_name")
159 case "system_dlkm":
160 assertUnset(a.partitionProps.System_dlkm_partition_name, "system_dlkm_partition_name")
161 case "system_other":
162 // TODO
163 case "product":
164 assertUnset(a.partitionProps.Product_partition_name, "product_partition_name")
165 case "vendor":
166 assertUnset(a.partitionProps.Vendor_partition_name, "vendor_partition_name")
167 case "vendor_dlkm":
168 assertUnset(a.partitionProps.Vendor_dlkm_partition_name, "vendor_dlkm_partition_name")
169 case "odm":
170 assertUnset(a.partitionProps.Odm_partition_name, "odm_partition_name")
171 case "odm_dlkm":
172 assertUnset(a.partitionProps.Odm_dlkm_partition_name, "odm_dlkm_partition_name")
173 default:
174 ctx.ModuleErrorf("Unsupported sub-partition of super partition: %q", subPartitionType)
175 }
176 }
177
178 deps = append(deps, info.SuperImage)
179 } else {
180 ctx.ModuleErrorf("Expected super image dep to provide SuperImageProvider")
181 }
182 }
mrziwang2fd33a72025-01-08 12:22:08 -0800183 ctx.VisitDirectDepsWithTag(filesystemDepTag, func(m android.Module) {
184 imageOutput, ok := android.OtherModuleProvider(ctx, m, android.OutputFilesProvider)
185 if !ok {
186 ctx.ModuleErrorf("Partition module %s doesn't set OutputfilesProvider", m.Name())
187 }
188 if len(imageOutput.DefaultOutputFiles) != 1 {
189 ctx.ModuleErrorf("Partition module %s should provide exact 1 output file", m.Name())
190 }
191 deps = append(deps, imageOutput.DefaultOutputFiles[0])
192 })
Jihoon Kang3be17162025-01-09 20:51:54 +0000193
194 a.copyFilesToProductOut(ctx)
195
Cole Faustb55a41c2025-01-09 16:53:58 -0800196 allImagesStamp := android.PathForModuleOut(ctx, "all_images_stamp")
mrziwang2fd33a72025-01-08 12:22:08 -0800197 ctx.Build(pctx, android.BuildParams{
Jihoon Kang3be17162025-01-09 20:51:54 +0000198 Rule: android.Touch,
Cole Faustb55a41c2025-01-09 16:53:58 -0800199 Output: allImagesStamp,
Jihoon Kang3be17162025-01-09 20:51:54 +0000200 Implicits: deps,
201 Validation: a.copyToProductOutTimestamp,
mrziwang2fd33a72025-01-08 12:22:08 -0800202 })
Cole Faustb55a41c2025-01-09 16:53:58 -0800203 ctx.SetOutputFiles(android.Paths{allImagesStamp}, "")
204 ctx.CheckbuildFile(allImagesStamp)
205
206 if ctx.OtherModuleIsAutoGenerated(ctx.Module()) {
207 numAutogeneratedAndroidDevices := ctx.Config().Once(numAutogeneratedAndroidDevicesOnceKey, func() interface{} {
208 return &atomic.Int32{}
209 }).(*atomic.Int32)
210 total := numAutogeneratedAndroidDevices.Add(1)
211 if total > 1 {
212 // There should only be 1 autogenerated android_device module. That one will be
213 // made the default thing to build in soong-only builds.
214 ctx.ModuleErrorf("There cannot be more than 1 autogenerated android_device module")
215 }
216 }
217
218 if !ctx.Config().KatiEnabled() && ctx.OtherModuleIsAutoGenerated(ctx.Module()) {
219 // In soong-only builds, build this module by default.
220 // This is the analogue to this make code:
221 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/main.mk;l=1396;drc=6595459cdd8164a6008335f6372c9f97b9094060
222 ctx.Phony("droidcore-unbundled", allImagesStamp)
223 }
Cole Faust44080412024-12-20 14:17:07 -0800224}
Jihoon Kangf2c53982024-10-09 17:32:52 +0000225
Spandan Dasef200ac2025-01-08 01:42:45 +0000226type targetFilesZipCopy struct {
227 srcModule *string
228 destSubdir string
229}
230
Cole Faust44080412024-12-20 14:17:07 -0800231func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext) {
232 targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir")
233 targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip")
234
235 builder := android.NewRuleBuilder(pctx, ctx)
236 builder.Command().Textf("rm -rf %s", targetFilesDir.String())
237 builder.Command().Textf("mkdir -p %s", targetFilesDir.String())
Spandan Dasef200ac2025-01-08 01:42:45 +0000238 toCopy := []targetFilesZipCopy{
239 targetFilesZipCopy{a.partitionProps.System_partition_name, "SYSTEM"},
240 targetFilesZipCopy{a.partitionProps.System_ext_partition_name, "SYSTEM_EXT"},
241 targetFilesZipCopy{a.partitionProps.Product_partition_name, "PRODUCT"},
242 targetFilesZipCopy{a.partitionProps.Vendor_partition_name, "VENDOR"},
243 targetFilesZipCopy{a.partitionProps.Odm_partition_name, "ODM"},
244 targetFilesZipCopy{a.partitionProps.System_dlkm_partition_name, "SYSTEM_DLKM"},
245 targetFilesZipCopy{a.partitionProps.Vendor_dlkm_partition_name, "VENDOR_DLKM"},
246 targetFilesZipCopy{a.partitionProps.Odm_dlkm_partition_name, "ODM_DLKM"},
247 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "BOOT/RAMDISK"},
248 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "INIT_BOOT/RAMDISK"},
249 targetFilesZipCopy{a.partitionProps.Vendor_boot_partition_name, "VENDOR_BOOT/RAMDISK"},
Spandan Das25649f52025-01-07 18:09:22 +0000250 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000251 // TODO: Handle cases where recovery files are copied to BOOT/ or RECOVERY/
252 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=6211-6219?q=core%2FMakefile&ss=android%2Fplatform%2Fsuperproject%2Fmain
253 if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() {
254 toCopy = append(toCopy, targetFilesZipCopy{a.partitionProps.Recovery_partition_name, "VENDOR_BOOT/RAMDISK"})
255 }
256
257 for _, zipCopy := range toCopy {
258 if zipCopy.srcModule == nil {
Spandan Das25649f52025-01-07 18:09:22 +0000259 continue
260 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000261 fsInfo := a.getFilesystemInfo(ctx, *zipCopy.srcModule)
262 subdir := zipCopy.destSubdir
263 rootDirString := fsInfo.RootDir.String()
264 if subdir == "SYSTEM" {
265 rootDirString = rootDirString + "/system"
266 }
Spandan Das25649f52025-01-07 18:09:22 +0000267 builder.Command().Textf("mkdir -p %s/%s", targetFilesDir.String(), subdir)
Cole Faust44080412024-12-20 14:17:07 -0800268 builder.Command().
269 BuiltTool("acp").
Spandan Dasef200ac2025-01-08 01:42:45 +0000270 Textf("-rd %s/. %s/%s", rootDirString, targetFilesDir, subdir).
Cole Faust44080412024-12-20 14:17:07 -0800271 Implicit(fsInfo.Output) // so that the staging dir is built
Spandan Dasef200ac2025-01-08 01:42:45 +0000272
Cole Faust44080412024-12-20 14:17:07 -0800273 }
Spandan Das9b17df22025-01-08 23:30:45 +0000274 // Copy cmdline, kernel etc. files of boot images
Spandan Das258c08f2025-01-08 23:30:45 +0000275 if a.partitionProps.Vendor_boot_partition_name != nil {
276 bootImg := ctx.GetDirectDepWithTag(proptools.String(a.partitionProps.Vendor_boot_partition_name), filesystemDepTag)
277 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000278 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
279 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/vendor_cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
280 if bootImgInfo.Dtb != nil {
281 builder.Command().Textf("cp %s %s/VENDOR_BOOT/dtb", bootImgInfo.Dtb, targetFilesDir)
282 }
Spandan Das23511372025-01-08 23:30:45 +0000283 if bootImgInfo.Bootconfig != nil {
284 builder.Command().Textf("cp %s %s/VENDOR_BOOT/vendor_bootconfig", bootImgInfo.Bootconfig, targetFilesDir)
285 }
Spandan Das258c08f2025-01-08 23:30:45 +0000286 }
287 if a.partitionProps.Boot_partition_name != nil {
288 bootImg := ctx.GetDirectDepWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
289 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000290 builder.Command().Textf("echo %s > %s/BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
291 if bootImgInfo.Dtb != nil {
292 builder.Command().Textf("cp %s %s/BOOT/dtb", bootImgInfo.Dtb, targetFilesDir)
293 }
294 if bootImgInfo.Kernel != nil {
295 builder.Command().Textf("cp %s %s/BOOT/kernel", bootImgInfo.Kernel, targetFilesDir)
296 // Even though kernel is not used to build vendor_boot, copy the kernel to VENDOR_BOOT to match the behavior of make packaging.
297 builder.Command().Textf("cp %s %s/VENDOR_BOOT/kernel", bootImgInfo.Kernel, targetFilesDir)
298 }
Spandan Das23511372025-01-08 23:30:45 +0000299 if bootImgInfo.Bootconfig != nil {
300 builder.Command().Textf("cp %s %s/BOOT/bootconfig", bootImgInfo.Bootconfig, targetFilesDir)
301 }
Spandan Das258c08f2025-01-08 23:30:45 +0000302 }
303
Cole Faust44080412024-12-20 14:17:07 -0800304 builder.Command().
305 BuiltTool("soong_zip").
306 Text("-d").
307 FlagWithOutput("-o ", targetFilesZip).
308 FlagWithArg("-C ", targetFilesDir.String()).
309 FlagWithArg("-D ", targetFilesDir.String()).
310 Text("-sha256")
311 builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip")
312}
313
314func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo {
315 fsMod := ctx.GetDirectDepWithTag(depName, filesystemDepTag)
316 fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider)
317 if !ok {
318 ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName)
319 }
320 return fsInfo
Jihoon Kangf2c53982024-10-09 17:32:52 +0000321}