blob: 3d9f8b96bb96e304a3d8e46035e72cd8fd7a1983 [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 Faust2bdc5e52025-01-10 10:29:36 -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"`
Spandan Dase51ff952025-01-09 18:11:59 +000063 // Path to android-info.txt file containing board specific info.
64 Android_info *string `android:"path"`
Jihoon Kang3be17162025-01-09 20:51:54 +000065}
66
Jihoon Kangf2c53982024-10-09 17:32:52 +000067type androidDevice struct {
68 android.ModuleBase
69
70 partitionProps PartitionNameProperties
Jihoon Kang3be17162025-01-09 20:51:54 +000071
72 deviceProps DeviceProperties
73
74 // copyToProductOutTimestamp for copying necessary files to PRODUCT_OUT
75 copyToProductOutTimestamp android.WritablePath
Jihoon Kangf2c53982024-10-09 17:32:52 +000076}
77
78func AndroidDeviceFactory() android.Module {
79 module := &androidDevice{}
Jihoon Kang3be17162025-01-09 20:51:54 +000080 module.AddProperties(&module.partitionProps, &module.deviceProps)
Cole Faust341d5f12025-01-07 15:32:38 -080081 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jihoon Kangf2c53982024-10-09 17:32:52 +000082 return module
83}
84
Cole Faustb55a41c2025-01-09 16:53:58 -080085var numAutogeneratedAndroidDevicesOnceKey android.OnceKey = android.NewOnceKey("num_auto_generated_anroid_devices")
86
Jihoon Kangf2c53982024-10-09 17:32:52 +000087type partitionDepTagType struct {
88 blueprint.BaseDependencyTag
89}
90
Cole Faust2bdc5e52025-01-10 10:29:36 -080091type superPartitionDepTagType struct {
92 blueprint.BaseDependencyTag
93}
94
95var superPartitionDepTag superPartitionDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +000096var filesystemDepTag partitionDepTagType
97
98func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
99 addDependencyIfDefined := func(dep *string) {
100 if dep != nil {
Cole Faust341d5f12025-01-07 15:32:38 -0800101 ctx.AddDependency(ctx.Module(), filesystemDepTag, proptools.String(dep))
Jihoon Kangf2c53982024-10-09 17:32:52 +0000102 }
103 }
104
Cole Faust2bdc5e52025-01-10 10:29:36 -0800105 if a.partitionProps.Super_partition_name != nil {
106 ctx.AddDependency(ctx.Module(), superPartitionDepTag, *a.partitionProps.Super_partition_name)
107 }
Jihoon Kangf2c53982024-10-09 17:32:52 +0000108 addDependencyIfDefined(a.partitionProps.Boot_partition_name)
Jihoon Kang9e087002025-01-08 19:12:23 +0000109 addDependencyIfDefined(a.partitionProps.Init_boot_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000110 addDependencyIfDefined(a.partitionProps.Vendor_boot_partition_name)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000111 addDependencyIfDefined(a.partitionProps.System_partition_name)
112 addDependencyIfDefined(a.partitionProps.System_ext_partition_name)
113 addDependencyIfDefined(a.partitionProps.Product_partition_name)
114 addDependencyIfDefined(a.partitionProps.Vendor_partition_name)
Spandan Dasc5717162024-11-01 18:33:57 +0000115 addDependencyIfDefined(a.partitionProps.Odm_partition_name)
mrziwang23ba8762024-11-07 16:21:53 -0800116 addDependencyIfDefined(a.partitionProps.Userdata_partition_name)
Spandan Dasa0394002025-01-07 18:38:34 +0000117 addDependencyIfDefined(a.partitionProps.System_dlkm_partition_name)
118 addDependencyIfDefined(a.partitionProps.Vendor_dlkm_partition_name)
119 addDependencyIfDefined(a.partitionProps.Odm_dlkm_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000120 addDependencyIfDefined(a.partitionProps.Recovery_partition_name)
Cole Faust3552eb62024-11-06 18:07:26 -0800121 for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions {
122 ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition)
123 }
Jihoon Kangf2c53982024-10-09 17:32:52 +0000124}
125
Jihoon Kang3be17162025-01-09 20:51:54 +0000126func (a *androidDevice) copyToProductOut(ctx android.ModuleContext, builder *android.RuleBuilder, src android.Path, dest string) {
127 destPath := android.PathForModuleInPartitionInstall(ctx, "").Join(ctx, dest)
128 builder.Command().Text("rsync").Flag("-a").Flag("--checksum").Input(src).Text(destPath.String())
129}
130
131func (a *androidDevice) copyFilesToProductOut(ctx android.ModuleContext) {
132 a.copyToProductOutTimestamp = android.PathForModuleOut(ctx, "timestamp")
133 builder := android.NewRuleBuilder(pctx, ctx)
134 builder.Command().Text("touch").Output(a.copyToProductOutTimestamp)
135
136 // List all individual files to be copied to PRODUCT_OUT here
137 if a.deviceProps.Bootloader != nil {
138 a.copyToProductOut(ctx, builder, android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Bootloader)), "bootloader")
139 }
140
141 builder.Build("copy_to_product_out", "Copy files to PRODUCT_OUT")
142}
143
Jihoon Kangf2c53982024-10-09 17:32:52 +0000144func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust44080412024-12-20 14:17:07 -0800145 a.buildTargetFilesZip(ctx)
mrziwang2fd33a72025-01-08 12:22:08 -0800146 var deps []android.Path
Cole Faust2bdc5e52025-01-10 10:29:36 -0800147 if proptools.String(a.partitionProps.Super_partition_name) != "" {
148 superImage := ctx.GetDirectDepWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
149 if info, ok := android.OtherModuleProvider(ctx, superImage, SuperImageProvider); ok {
150 assertUnset := func(prop *string, propName string) {
151 if prop != nil && *prop != "" {
152 ctx.PropertyErrorf(propName, "Cannot be set because it's already part of the super image")
153 }
154 }
155 for _, subPartitionType := range android.SortedKeys(info.SubImageInfo) {
156 switch subPartitionType {
157 case "system":
158 assertUnset(a.partitionProps.System_partition_name, "system_partition_name")
159 case "system_ext":
160 assertUnset(a.partitionProps.System_ext_partition_name, "system_ext_partition_name")
161 case "system_dlkm":
162 assertUnset(a.partitionProps.System_dlkm_partition_name, "system_dlkm_partition_name")
163 case "system_other":
164 // TODO
165 case "product":
166 assertUnset(a.partitionProps.Product_partition_name, "product_partition_name")
167 case "vendor":
168 assertUnset(a.partitionProps.Vendor_partition_name, "vendor_partition_name")
169 case "vendor_dlkm":
170 assertUnset(a.partitionProps.Vendor_dlkm_partition_name, "vendor_dlkm_partition_name")
171 case "odm":
172 assertUnset(a.partitionProps.Odm_partition_name, "odm_partition_name")
173 case "odm_dlkm":
174 assertUnset(a.partitionProps.Odm_dlkm_partition_name, "odm_dlkm_partition_name")
175 default:
176 ctx.ModuleErrorf("Unsupported sub-partition of super partition: %q", subPartitionType)
177 }
178 }
179
180 deps = append(deps, info.SuperImage)
181 } else {
182 ctx.ModuleErrorf("Expected super image dep to provide SuperImageProvider")
183 }
184 }
mrziwang2fd33a72025-01-08 12:22:08 -0800185 ctx.VisitDirectDepsWithTag(filesystemDepTag, func(m android.Module) {
186 imageOutput, ok := android.OtherModuleProvider(ctx, m, android.OutputFilesProvider)
187 if !ok {
188 ctx.ModuleErrorf("Partition module %s doesn't set OutputfilesProvider", m.Name())
189 }
190 if len(imageOutput.DefaultOutputFiles) != 1 {
191 ctx.ModuleErrorf("Partition module %s should provide exact 1 output file", m.Name())
192 }
193 deps = append(deps, imageOutput.DefaultOutputFiles[0])
194 })
Jihoon Kang3be17162025-01-09 20:51:54 +0000195
196 a.copyFilesToProductOut(ctx)
197
Cole Faustb55a41c2025-01-09 16:53:58 -0800198 allImagesStamp := android.PathForModuleOut(ctx, "all_images_stamp")
mrziwang2fd33a72025-01-08 12:22:08 -0800199 ctx.Build(pctx, android.BuildParams{
Jihoon Kang3be17162025-01-09 20:51:54 +0000200 Rule: android.Touch,
Cole Faustb55a41c2025-01-09 16:53:58 -0800201 Output: allImagesStamp,
Jihoon Kang3be17162025-01-09 20:51:54 +0000202 Implicits: deps,
203 Validation: a.copyToProductOutTimestamp,
mrziwang2fd33a72025-01-08 12:22:08 -0800204 })
Cole Faustb55a41c2025-01-09 16:53:58 -0800205 ctx.SetOutputFiles(android.Paths{allImagesStamp}, "")
206 ctx.CheckbuildFile(allImagesStamp)
207
208 if ctx.OtherModuleIsAutoGenerated(ctx.Module()) {
209 numAutogeneratedAndroidDevices := ctx.Config().Once(numAutogeneratedAndroidDevicesOnceKey, func() interface{} {
210 return &atomic.Int32{}
211 }).(*atomic.Int32)
212 total := numAutogeneratedAndroidDevices.Add(1)
213 if total > 1 {
214 // There should only be 1 autogenerated android_device module. That one will be
215 // made the default thing to build in soong-only builds.
216 ctx.ModuleErrorf("There cannot be more than 1 autogenerated android_device module")
217 }
218 }
219
220 if !ctx.Config().KatiEnabled() && ctx.OtherModuleIsAutoGenerated(ctx.Module()) {
221 // In soong-only builds, build this module by default.
222 // This is the analogue to this make code:
223 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/main.mk;l=1396;drc=6595459cdd8164a6008335f6372c9f97b9094060
224 ctx.Phony("droidcore-unbundled", allImagesStamp)
225 }
Cole Faust44080412024-12-20 14:17:07 -0800226}
Jihoon Kangf2c53982024-10-09 17:32:52 +0000227
Spandan Dasef200ac2025-01-08 01:42:45 +0000228type targetFilesZipCopy struct {
229 srcModule *string
230 destSubdir string
231}
232
Cole Faust44080412024-12-20 14:17:07 -0800233func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext) {
234 targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir")
235 targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip")
236
237 builder := android.NewRuleBuilder(pctx, ctx)
238 builder.Command().Textf("rm -rf %s", targetFilesDir.String())
239 builder.Command().Textf("mkdir -p %s", targetFilesDir.String())
Spandan Dasef200ac2025-01-08 01:42:45 +0000240 toCopy := []targetFilesZipCopy{
241 targetFilesZipCopy{a.partitionProps.System_partition_name, "SYSTEM"},
242 targetFilesZipCopy{a.partitionProps.System_ext_partition_name, "SYSTEM_EXT"},
243 targetFilesZipCopy{a.partitionProps.Product_partition_name, "PRODUCT"},
244 targetFilesZipCopy{a.partitionProps.Vendor_partition_name, "VENDOR"},
245 targetFilesZipCopy{a.partitionProps.Odm_partition_name, "ODM"},
246 targetFilesZipCopy{a.partitionProps.System_dlkm_partition_name, "SYSTEM_DLKM"},
247 targetFilesZipCopy{a.partitionProps.Vendor_dlkm_partition_name, "VENDOR_DLKM"},
248 targetFilesZipCopy{a.partitionProps.Odm_dlkm_partition_name, "ODM_DLKM"},
249 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "BOOT/RAMDISK"},
250 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "INIT_BOOT/RAMDISK"},
251 targetFilesZipCopy{a.partitionProps.Vendor_boot_partition_name, "VENDOR_BOOT/RAMDISK"},
Spandan Das25649f52025-01-07 18:09:22 +0000252 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000253 // TODO: Handle cases where recovery files are copied to BOOT/ or RECOVERY/
254 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=6211-6219?q=core%2FMakefile&ss=android%2Fplatform%2Fsuperproject%2Fmain
255 if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() {
256 toCopy = append(toCopy, targetFilesZipCopy{a.partitionProps.Recovery_partition_name, "VENDOR_BOOT/RAMDISK"})
257 }
258
Spandan Dasd8ce3242025-01-10 00:54:11 +0000259 // Create an IMAGES/ subdirectory
260 builder.Command().Textf("mkdir -p %s/IMAGES/", targetFilesDir.String())
Spandan Dasc15fb6c2025-01-10 18:23:42 +0000261 if a.deviceProps.Bootloader != nil {
262 builder.Command().Textf("cp ").Input(android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Bootloader))).Textf(" %s/IMAGES/bootloader", targetFilesDir.String())
263 }
Spandan Dasd8ce3242025-01-10 00:54:11 +0000264
Spandan Dasef200ac2025-01-08 01:42:45 +0000265 for _, zipCopy := range toCopy {
266 if zipCopy.srcModule == nil {
Spandan Das25649f52025-01-07 18:09:22 +0000267 continue
268 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000269 fsInfo := a.getFilesystemInfo(ctx, *zipCopy.srcModule)
270 subdir := zipCopy.destSubdir
271 rootDirString := fsInfo.RootDir.String()
272 if subdir == "SYSTEM" {
273 rootDirString = rootDirString + "/system"
274 }
Spandan Das25649f52025-01-07 18:09:22 +0000275 builder.Command().Textf("mkdir -p %s/%s", targetFilesDir.String(), subdir)
Cole Faust44080412024-12-20 14:17:07 -0800276 builder.Command().
277 BuiltTool("acp").
Spandan Dasef200ac2025-01-08 01:42:45 +0000278 Textf("-rd %s/. %s/%s", rootDirString, targetFilesDir, subdir).
Cole Faust44080412024-12-20 14:17:07 -0800279 Implicit(fsInfo.Output) // so that the staging dir is built
Spandan Dasef200ac2025-01-08 01:42:45 +0000280
Spandan Das3ec6d062025-01-09 19:37:47 +0000281 if subdir == "SYSTEM" {
282 // Create the ROOT partition in target_files.zip
283 builder.Command().Textf("rsync --links --exclude=system/* %s/ -r %s/ROOT", fsInfo.RootDir, targetFilesDir.String())
284 }
Spandan Dasd8ce3242025-01-10 00:54:11 +0000285 builder.Command().Textf("cp %s %s/IMAGES/", fsInfo.Output, targetFilesDir.String())
Cole Faust44080412024-12-20 14:17:07 -0800286 }
Spandan Das9b17df22025-01-08 23:30:45 +0000287 // Copy cmdline, kernel etc. files of boot images
Spandan Das258c08f2025-01-08 23:30:45 +0000288 if a.partitionProps.Vendor_boot_partition_name != nil {
289 bootImg := ctx.GetDirectDepWithTag(proptools.String(a.partitionProps.Vendor_boot_partition_name), filesystemDepTag)
290 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000291 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
292 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/vendor_cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
293 if bootImgInfo.Dtb != nil {
294 builder.Command().Textf("cp %s %s/VENDOR_BOOT/dtb", bootImgInfo.Dtb, targetFilesDir)
295 }
Spandan Das23511372025-01-08 23:30:45 +0000296 if bootImgInfo.Bootconfig != nil {
297 builder.Command().Textf("cp %s %s/VENDOR_BOOT/vendor_bootconfig", bootImgInfo.Bootconfig, targetFilesDir)
298 }
Spandan Das258c08f2025-01-08 23:30:45 +0000299 }
300 if a.partitionProps.Boot_partition_name != nil {
301 bootImg := ctx.GetDirectDepWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
302 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000303 builder.Command().Textf("echo %s > %s/BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
304 if bootImgInfo.Dtb != nil {
305 builder.Command().Textf("cp %s %s/BOOT/dtb", bootImgInfo.Dtb, targetFilesDir)
306 }
307 if bootImgInfo.Kernel != nil {
308 builder.Command().Textf("cp %s %s/BOOT/kernel", bootImgInfo.Kernel, targetFilesDir)
309 // Even though kernel is not used to build vendor_boot, copy the kernel to VENDOR_BOOT to match the behavior of make packaging.
310 builder.Command().Textf("cp %s %s/VENDOR_BOOT/kernel", bootImgInfo.Kernel, targetFilesDir)
311 }
Spandan Das23511372025-01-08 23:30:45 +0000312 if bootImgInfo.Bootconfig != nil {
313 builder.Command().Textf("cp %s %s/BOOT/bootconfig", bootImgInfo.Bootconfig, targetFilesDir)
314 }
Spandan Das258c08f2025-01-08 23:30:45 +0000315 }
316
Spandan Dase51ff952025-01-09 18:11:59 +0000317 if a.deviceProps.Android_info != nil {
318 builder.Command().Textf("mkdir -p %s/OTA", targetFilesDir)
319 builder.Command().Textf("cp %s %s/OTA/android-info.txt", android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Android_info)), targetFilesDir)
320 }
321
Cole Faust44080412024-12-20 14:17:07 -0800322 builder.Command().
323 BuiltTool("soong_zip").
324 Text("-d").
325 FlagWithOutput("-o ", targetFilesZip).
326 FlagWithArg("-C ", targetFilesDir.String()).
327 FlagWithArg("-D ", targetFilesDir.String()).
328 Text("-sha256")
329 builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip")
330}
331
332func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo {
333 fsMod := ctx.GetDirectDepWithTag(depName, filesystemDepTag)
334 fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider)
335 if !ok {
336 ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName)
337 }
338 return fsInfo
Jihoon Kangf2c53982024-10-09 17:32:52 +0000339}