blob: 6a939e8991ef2bc67f25969943bfdcb39187e824 [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 {
Jihoon Kange7e3ec82025-01-02 21:29:14 +000028 // Name of the boot partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000029 Boot_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000030 // Name of the vendor boot partition filesystem module
31 Vendor_boot_partition_name *string
32 // Name of the init boot partition filesystem module
33 Init_boot_partition_name *string
34 // Name of the system partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000035 System_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000036 // Name of the system_ext partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000037 System_ext_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000038 // Name of the product partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000039 Product_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000040 // Name of the vendor partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000041 Vendor_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000042 // Name of the odm partition filesystem module
Spandan Dasc5717162024-11-01 18:33:57 +000043 Odm_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000044 // Name of the recovery partition filesystem module
45 Recovery_partition_name *string
Cole Faust3552eb62024-11-06 18:07:26 -080046 // The vbmeta partition and its "chained" partitions
47 Vbmeta_partitions []string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000048 // Name of the userdata partition filesystem module
mrziwang23ba8762024-11-07 16:21:53 -080049 Userdata_partition_name *string
Spandan Dasa0394002025-01-07 18:38:34 +000050 // Name of the system_dlkm partition filesystem module
51 System_dlkm_partition_name *string
52 // Name of the vendor_dlkm partition filesystem module
53 Vendor_dlkm_partition_name *string
54 // Name of the odm_dlkm partition filesystem module
55 Odm_dlkm_partition_name *string
Jihoon Kangf2c53982024-10-09 17:32:52 +000056}
57
Jihoon Kang3be17162025-01-09 20:51:54 +000058type DeviceProperties struct {
59 // Path to the prebuilt bootloader that would be copied to PRODUCT_OUT
60 Bootloader *string `android:"path"`
Spandan Dase51ff952025-01-09 18:11:59 +000061 // Path to android-info.txt file containing board specific info.
62 Android_info *string `android:"path"`
Jihoon Kang3be17162025-01-09 20:51:54 +000063}
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
89var filesystemDepTag partitionDepTagType
90
91func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
92 addDependencyIfDefined := func(dep *string) {
93 if dep != nil {
Cole Faust341d5f12025-01-07 15:32:38 -080094 ctx.AddDependency(ctx.Module(), filesystemDepTag, proptools.String(dep))
Jihoon Kangf2c53982024-10-09 17:32:52 +000095 }
96 }
97
98 addDependencyIfDefined(a.partitionProps.Boot_partition_name)
Jihoon Kang9e087002025-01-08 19:12:23 +000099 addDependencyIfDefined(a.partitionProps.Init_boot_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000100 addDependencyIfDefined(a.partitionProps.Vendor_boot_partition_name)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000101 addDependencyIfDefined(a.partitionProps.System_partition_name)
102 addDependencyIfDefined(a.partitionProps.System_ext_partition_name)
103 addDependencyIfDefined(a.partitionProps.Product_partition_name)
104 addDependencyIfDefined(a.partitionProps.Vendor_partition_name)
Spandan Dasc5717162024-11-01 18:33:57 +0000105 addDependencyIfDefined(a.partitionProps.Odm_partition_name)
mrziwang23ba8762024-11-07 16:21:53 -0800106 addDependencyIfDefined(a.partitionProps.Userdata_partition_name)
Spandan Dasa0394002025-01-07 18:38:34 +0000107 addDependencyIfDefined(a.partitionProps.System_dlkm_partition_name)
108 addDependencyIfDefined(a.partitionProps.Vendor_dlkm_partition_name)
109 addDependencyIfDefined(a.partitionProps.Odm_dlkm_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000110 addDependencyIfDefined(a.partitionProps.Recovery_partition_name)
Cole Faust3552eb62024-11-06 18:07:26 -0800111 for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions {
112 ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition)
113 }
Jihoon Kangf2c53982024-10-09 17:32:52 +0000114}
115
Jihoon Kang3be17162025-01-09 20:51:54 +0000116func (a *androidDevice) copyToProductOut(ctx android.ModuleContext, builder *android.RuleBuilder, src android.Path, dest string) {
117 destPath := android.PathForModuleInPartitionInstall(ctx, "").Join(ctx, dest)
118 builder.Command().Text("rsync").Flag("-a").Flag("--checksum").Input(src).Text(destPath.String())
119}
120
121func (a *androidDevice) copyFilesToProductOut(ctx android.ModuleContext) {
122 a.copyToProductOutTimestamp = android.PathForModuleOut(ctx, "timestamp")
123 builder := android.NewRuleBuilder(pctx, ctx)
124 builder.Command().Text("touch").Output(a.copyToProductOutTimestamp)
125
126 // List all individual files to be copied to PRODUCT_OUT here
127 if a.deviceProps.Bootloader != nil {
128 a.copyToProductOut(ctx, builder, android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Bootloader)), "bootloader")
129 }
130
131 builder.Build("copy_to_product_out", "Copy files to PRODUCT_OUT")
132}
133
Jihoon Kangf2c53982024-10-09 17:32:52 +0000134func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust44080412024-12-20 14:17:07 -0800135 a.buildTargetFilesZip(ctx)
mrziwang2fd33a72025-01-08 12:22:08 -0800136 var deps []android.Path
137 ctx.VisitDirectDepsWithTag(filesystemDepTag, func(m android.Module) {
138 imageOutput, ok := android.OtherModuleProvider(ctx, m, android.OutputFilesProvider)
139 if !ok {
140 ctx.ModuleErrorf("Partition module %s doesn't set OutputfilesProvider", m.Name())
141 }
142 if len(imageOutput.DefaultOutputFiles) != 1 {
143 ctx.ModuleErrorf("Partition module %s should provide exact 1 output file", m.Name())
144 }
145 deps = append(deps, imageOutput.DefaultOutputFiles[0])
146 })
Jihoon Kang3be17162025-01-09 20:51:54 +0000147
148 a.copyFilesToProductOut(ctx)
149
Cole Faustb55a41c2025-01-09 16:53:58 -0800150 allImagesStamp := android.PathForModuleOut(ctx, "all_images_stamp")
mrziwang2fd33a72025-01-08 12:22:08 -0800151 ctx.Build(pctx, android.BuildParams{
Jihoon Kang3be17162025-01-09 20:51:54 +0000152 Rule: android.Touch,
Cole Faustb55a41c2025-01-09 16:53:58 -0800153 Output: allImagesStamp,
Jihoon Kang3be17162025-01-09 20:51:54 +0000154 Implicits: deps,
155 Validation: a.copyToProductOutTimestamp,
mrziwang2fd33a72025-01-08 12:22:08 -0800156 })
Cole Faustb55a41c2025-01-09 16:53:58 -0800157 ctx.SetOutputFiles(android.Paths{allImagesStamp}, "")
158 ctx.CheckbuildFile(allImagesStamp)
159
160 if ctx.OtherModuleIsAutoGenerated(ctx.Module()) {
161 numAutogeneratedAndroidDevices := ctx.Config().Once(numAutogeneratedAndroidDevicesOnceKey, func() interface{} {
162 return &atomic.Int32{}
163 }).(*atomic.Int32)
164 total := numAutogeneratedAndroidDevices.Add(1)
165 if total > 1 {
166 // There should only be 1 autogenerated android_device module. That one will be
167 // made the default thing to build in soong-only builds.
168 ctx.ModuleErrorf("There cannot be more than 1 autogenerated android_device module")
169 }
170 }
171
172 if !ctx.Config().KatiEnabled() && ctx.OtherModuleIsAutoGenerated(ctx.Module()) {
173 // In soong-only builds, build this module by default.
174 // This is the analogue to this make code:
175 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/main.mk;l=1396;drc=6595459cdd8164a6008335f6372c9f97b9094060
176 ctx.Phony("droidcore-unbundled", allImagesStamp)
177 }
Cole Faust44080412024-12-20 14:17:07 -0800178}
Jihoon Kangf2c53982024-10-09 17:32:52 +0000179
Spandan Dasef200ac2025-01-08 01:42:45 +0000180type targetFilesZipCopy struct {
181 srcModule *string
182 destSubdir string
183}
184
Cole Faust44080412024-12-20 14:17:07 -0800185func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext) {
186 targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir")
187 targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip")
188
189 builder := android.NewRuleBuilder(pctx, ctx)
190 builder.Command().Textf("rm -rf %s", targetFilesDir.String())
191 builder.Command().Textf("mkdir -p %s", targetFilesDir.String())
Spandan Dasef200ac2025-01-08 01:42:45 +0000192 toCopy := []targetFilesZipCopy{
193 targetFilesZipCopy{a.partitionProps.System_partition_name, "SYSTEM"},
194 targetFilesZipCopy{a.partitionProps.System_ext_partition_name, "SYSTEM_EXT"},
195 targetFilesZipCopy{a.partitionProps.Product_partition_name, "PRODUCT"},
196 targetFilesZipCopy{a.partitionProps.Vendor_partition_name, "VENDOR"},
197 targetFilesZipCopy{a.partitionProps.Odm_partition_name, "ODM"},
198 targetFilesZipCopy{a.partitionProps.System_dlkm_partition_name, "SYSTEM_DLKM"},
199 targetFilesZipCopy{a.partitionProps.Vendor_dlkm_partition_name, "VENDOR_DLKM"},
200 targetFilesZipCopy{a.partitionProps.Odm_dlkm_partition_name, "ODM_DLKM"},
201 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "BOOT/RAMDISK"},
202 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "INIT_BOOT/RAMDISK"},
203 targetFilesZipCopy{a.partitionProps.Vendor_boot_partition_name, "VENDOR_BOOT/RAMDISK"},
Spandan Das25649f52025-01-07 18:09:22 +0000204 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000205 // TODO: Handle cases where recovery files are copied to BOOT/ or RECOVERY/
206 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=6211-6219?q=core%2FMakefile&ss=android%2Fplatform%2Fsuperproject%2Fmain
207 if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() {
208 toCopy = append(toCopy, targetFilesZipCopy{a.partitionProps.Recovery_partition_name, "VENDOR_BOOT/RAMDISK"})
209 }
210
Spandan Dasd8ce3242025-01-10 00:54:11 +0000211 // Create an IMAGES/ subdirectory
212 builder.Command().Textf("mkdir -p %s/IMAGES/", targetFilesDir.String())
Spandan Dasc15fb6c2025-01-10 18:23:42 +0000213 if a.deviceProps.Bootloader != nil {
214 builder.Command().Textf("cp ").Input(android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Bootloader))).Textf(" %s/IMAGES/bootloader", targetFilesDir.String())
215 }
Spandan Dasd8ce3242025-01-10 00:54:11 +0000216
Spandan Dasef200ac2025-01-08 01:42:45 +0000217 for _, zipCopy := range toCopy {
218 if zipCopy.srcModule == nil {
Spandan Das25649f52025-01-07 18:09:22 +0000219 continue
220 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000221 fsInfo := a.getFilesystemInfo(ctx, *zipCopy.srcModule)
222 subdir := zipCopy.destSubdir
223 rootDirString := fsInfo.RootDir.String()
224 if subdir == "SYSTEM" {
225 rootDirString = rootDirString + "/system"
226 }
Spandan Das25649f52025-01-07 18:09:22 +0000227 builder.Command().Textf("mkdir -p %s/%s", targetFilesDir.String(), subdir)
Cole Faust44080412024-12-20 14:17:07 -0800228 builder.Command().
229 BuiltTool("acp").
Spandan Dasef200ac2025-01-08 01:42:45 +0000230 Textf("-rd %s/. %s/%s", rootDirString, targetFilesDir, subdir).
Cole Faust44080412024-12-20 14:17:07 -0800231 Implicit(fsInfo.Output) // so that the staging dir is built
Spandan Dasef200ac2025-01-08 01:42:45 +0000232
Spandan Das3ec6d062025-01-09 19:37:47 +0000233 if subdir == "SYSTEM" {
234 // Create the ROOT partition in target_files.zip
235 builder.Command().Textf("rsync --links --exclude=system/* %s/ -r %s/ROOT", fsInfo.RootDir, targetFilesDir.String())
236 }
Spandan Dasd8ce3242025-01-10 00:54:11 +0000237 builder.Command().Textf("cp %s %s/IMAGES/", fsInfo.Output, targetFilesDir.String())
Cole Faust44080412024-12-20 14:17:07 -0800238 }
Spandan Das9b17df22025-01-08 23:30:45 +0000239 // Copy cmdline, kernel etc. files of boot images
Spandan Das258c08f2025-01-08 23:30:45 +0000240 if a.partitionProps.Vendor_boot_partition_name != nil {
241 bootImg := ctx.GetDirectDepWithTag(proptools.String(a.partitionProps.Vendor_boot_partition_name), filesystemDepTag)
242 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000243 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
244 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/vendor_cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
245 if bootImgInfo.Dtb != nil {
246 builder.Command().Textf("cp %s %s/VENDOR_BOOT/dtb", bootImgInfo.Dtb, targetFilesDir)
247 }
Spandan Das23511372025-01-08 23:30:45 +0000248 if bootImgInfo.Bootconfig != nil {
249 builder.Command().Textf("cp %s %s/VENDOR_BOOT/vendor_bootconfig", bootImgInfo.Bootconfig, targetFilesDir)
250 }
Spandan Das258c08f2025-01-08 23:30:45 +0000251 }
252 if a.partitionProps.Boot_partition_name != nil {
253 bootImg := ctx.GetDirectDepWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
254 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000255 builder.Command().Textf("echo %s > %s/BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
256 if bootImgInfo.Dtb != nil {
257 builder.Command().Textf("cp %s %s/BOOT/dtb", bootImgInfo.Dtb, targetFilesDir)
258 }
259 if bootImgInfo.Kernel != nil {
260 builder.Command().Textf("cp %s %s/BOOT/kernel", bootImgInfo.Kernel, targetFilesDir)
261 // Even though kernel is not used to build vendor_boot, copy the kernel to VENDOR_BOOT to match the behavior of make packaging.
262 builder.Command().Textf("cp %s %s/VENDOR_BOOT/kernel", bootImgInfo.Kernel, targetFilesDir)
263 }
Spandan Das23511372025-01-08 23:30:45 +0000264 if bootImgInfo.Bootconfig != nil {
265 builder.Command().Textf("cp %s %s/BOOT/bootconfig", bootImgInfo.Bootconfig, targetFilesDir)
266 }
Spandan Das258c08f2025-01-08 23:30:45 +0000267 }
268
Spandan Dase51ff952025-01-09 18:11:59 +0000269 if a.deviceProps.Android_info != nil {
270 builder.Command().Textf("mkdir -p %s/OTA", targetFilesDir)
271 builder.Command().Textf("cp %s %s/OTA/android-info.txt", android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Android_info)), targetFilesDir)
272 }
273
Cole Faust44080412024-12-20 14:17:07 -0800274 builder.Command().
275 BuiltTool("soong_zip").
276 Text("-d").
277 FlagWithOutput("-o ", targetFilesZip).
278 FlagWithArg("-C ", targetFilesDir.String()).
279 FlagWithArg("-D ", targetFilesDir.String()).
280 Text("-sha256")
281 builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip")
282}
283
284func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo {
285 fsMod := ctx.GetDirectDepWithTag(depName, filesystemDepTag)
286 fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider)
287 if !ok {
288 ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName)
289 }
290 return fsInfo
Jihoon Kangf2c53982024-10-09 17:32:52 +0000291}