blob: 224c2ef2b8dbd721b7547edfb41de99a849f67a7 [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 (
Jihoon Kang0a6315b2025-01-30 01:14:49 +000018 "fmt"
Spandan Das258c08f2025-01-08 23:30:45 +000019 "strings"
Cole Faustb55a41c2025-01-09 16:53:58 -080020 "sync/atomic"
Spandan Das258c08f2025-01-08 23:30:45 +000021
Jihoon Kangf2c53982024-10-09 17:32:52 +000022 "android/soong/android"
23
24 "github.com/google/blueprint"
25 "github.com/google/blueprint/proptools"
26)
27
28type PartitionNameProperties struct {
Cole Faust2bdc5e52025-01-10 10:29:36 -080029 // Name of the super partition filesystem module
30 Super_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000031 // Name of the boot partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000032 Boot_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000033 // Name of the vendor boot partition filesystem module
34 Vendor_boot_partition_name *string
35 // Name of the init boot partition filesystem module
36 Init_boot_partition_name *string
37 // Name of the system partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000038 System_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000039 // Name of the system_ext partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000040 System_ext_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000041 // Name of the product partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000042 Product_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000043 // Name of the vendor partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000044 Vendor_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000045 // Name of the odm partition filesystem module
Spandan Dasc5717162024-11-01 18:33:57 +000046 Odm_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000047 // Name of the recovery partition filesystem module
48 Recovery_partition_name *string
Cole Faust3552eb62024-11-06 18:07:26 -080049 // The vbmeta partition and its "chained" partitions
50 Vbmeta_partitions []string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000051 // Name of the userdata partition filesystem module
mrziwang23ba8762024-11-07 16:21:53 -080052 Userdata_partition_name *string
Spandan Dasa0394002025-01-07 18:38:34 +000053 // Name of the system_dlkm partition filesystem module
54 System_dlkm_partition_name *string
55 // Name of the vendor_dlkm partition filesystem module
56 Vendor_dlkm_partition_name *string
57 // Name of the odm_dlkm partition filesystem module
58 Odm_dlkm_partition_name *string
Jihoon Kangf2c53982024-10-09 17:32:52 +000059}
60
Jihoon Kang3be17162025-01-09 20:51:54 +000061type DeviceProperties struct {
62 // Path to the prebuilt bootloader that would be copied to PRODUCT_OUT
63 Bootloader *string `android:"path"`
Spandan Dase51ff952025-01-09 18:11:59 +000064 // Path to android-info.txt file containing board specific info.
65 Android_info *string `android:"path"`
Cole Faust11fda332025-01-14 16:47:19 -080066 // If this is the "main" android_device target for the build, i.e. the one that gets built
67 // when running a plain `m` command. Currently, this is the autogenerated android_device module
68 // in soong-only builds, but in the future when we check in android_device modules, the main
69 // one will be determined based on the lunch product. TODO: Figure out how to make this
70 // blueprint:"mutated" and still set it from filesystem_creator
71 Main_device *bool
Spandan Das29d44882025-01-15 21:12:36 +000072
Spandan Das00948072025-02-12 19:36:03 +000073 Ab_ota_updater *bool
74 Ab_ota_partitions []string
75 Ab_ota_keys []string
76 Ab_ota_postinstall_config []string
Spandan Das75955b12025-02-13 22:12:52 +000077
78 Ramdisk_node_list *string `android:"path"`
Jihoon Kang3be17162025-01-09 20:51:54 +000079}
80
Jihoon Kangf2c53982024-10-09 17:32:52 +000081type androidDevice struct {
82 android.ModuleBase
83
84 partitionProps PartitionNameProperties
Jihoon Kang3be17162025-01-09 20:51:54 +000085
86 deviceProps DeviceProperties
Cole Fausta472a6f2025-02-10 16:10:04 -080087
88 allImagesZip android.Path
Jihoon Kangf2c53982024-10-09 17:32:52 +000089}
90
91func AndroidDeviceFactory() android.Module {
92 module := &androidDevice{}
Jihoon Kang3be17162025-01-09 20:51:54 +000093 module.AddProperties(&module.partitionProps, &module.deviceProps)
Cole Faust341d5f12025-01-07 15:32:38 -080094 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jihoon Kangf2c53982024-10-09 17:32:52 +000095 return module
96}
97
Cole Faust11fda332025-01-14 16:47:19 -080098var numMainAndroidDevicesOnceKey android.OnceKey = android.NewOnceKey("num_auto_generated_anroid_devices")
Cole Faustb55a41c2025-01-09 16:53:58 -080099
Jihoon Kangf2c53982024-10-09 17:32:52 +0000100type partitionDepTagType struct {
101 blueprint.BaseDependencyTag
102}
103
Cole Faust2bdc5e52025-01-10 10:29:36 -0800104type superPartitionDepTagType struct {
105 blueprint.BaseDependencyTag
106}
Spandan Das29d44882025-01-15 21:12:36 +0000107type targetFilesMetadataDepTagType struct {
108 blueprint.BaseDependencyTag
109}
Cole Faust2bdc5e52025-01-10 10:29:36 -0800110
111var superPartitionDepTag superPartitionDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000112var filesystemDepTag partitionDepTagType
Spandan Das29d44882025-01-15 21:12:36 +0000113var targetFilesMetadataDepTag targetFilesMetadataDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000114
115func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
116 addDependencyIfDefined := func(dep *string) {
117 if dep != nil {
Cole Faust341d5f12025-01-07 15:32:38 -0800118 ctx.AddDependency(ctx.Module(), filesystemDepTag, proptools.String(dep))
Jihoon Kangf2c53982024-10-09 17:32:52 +0000119 }
120 }
121
Cole Faust2bdc5e52025-01-10 10:29:36 -0800122 if a.partitionProps.Super_partition_name != nil {
123 ctx.AddDependency(ctx.Module(), superPartitionDepTag, *a.partitionProps.Super_partition_name)
124 }
Jihoon Kangf2c53982024-10-09 17:32:52 +0000125 addDependencyIfDefined(a.partitionProps.Boot_partition_name)
Jihoon Kang9e087002025-01-08 19:12:23 +0000126 addDependencyIfDefined(a.partitionProps.Init_boot_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000127 addDependencyIfDefined(a.partitionProps.Vendor_boot_partition_name)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000128 addDependencyIfDefined(a.partitionProps.System_partition_name)
129 addDependencyIfDefined(a.partitionProps.System_ext_partition_name)
130 addDependencyIfDefined(a.partitionProps.Product_partition_name)
131 addDependencyIfDefined(a.partitionProps.Vendor_partition_name)
Spandan Dasc5717162024-11-01 18:33:57 +0000132 addDependencyIfDefined(a.partitionProps.Odm_partition_name)
mrziwang23ba8762024-11-07 16:21:53 -0800133 addDependencyIfDefined(a.partitionProps.Userdata_partition_name)
Spandan Dasa0394002025-01-07 18:38:34 +0000134 addDependencyIfDefined(a.partitionProps.System_dlkm_partition_name)
135 addDependencyIfDefined(a.partitionProps.Vendor_dlkm_partition_name)
136 addDependencyIfDefined(a.partitionProps.Odm_dlkm_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000137 addDependencyIfDefined(a.partitionProps.Recovery_partition_name)
Cole Faust3552eb62024-11-06 18:07:26 -0800138 for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions {
139 ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition)
140 }
Spandan Das29d44882025-01-15 21:12:36 +0000141 a.addDepsForTargetFilesMetadata(ctx)
142}
143
144func (a *androidDevice) addDepsForTargetFilesMetadata(ctx android.BottomUpMutatorContext) {
145 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), targetFilesMetadataDepTag, "liblz4") // host variant
Jihoon Kangf2c53982024-10-09 17:32:52 +0000146}
147
Cole Faust11fda332025-01-14 16:47:19 -0800148func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) {
149 if proptools.Bool(a.deviceProps.Main_device) {
150 numMainAndroidDevices := ctx.Config().Once(numMainAndroidDevicesOnceKey, func() interface{} {
151 return &atomic.Int32{}
152 }).(*atomic.Int32)
153 total := numMainAndroidDevices.Add(1)
154 if total > 1 {
155 // There should only be 1 main android_device module. That one will be
156 // made the default thing to build in soong-only builds.
157 ctx.ModuleErrorf("There cannot be more than 1 main android_device module")
158 }
Jihoon Kang3be17162025-01-09 20:51:54 +0000159 }
160
Spandan Das5ef1a9c2025-02-11 18:50:17 +0000161 a.buildTargetFilesZip(ctx)
mrziwang2fd33a72025-01-08 12:22:08 -0800162 var deps []android.Path
Cole Faust2bdc5e52025-01-10 10:29:36 -0800163 if proptools.String(a.partitionProps.Super_partition_name) != "" {
Cole Faust19eb09d2025-01-14 13:27:00 -0800164 superImage := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Cole Faust2bdc5e52025-01-10 10:29:36 -0800165 if info, ok := android.OtherModuleProvider(ctx, superImage, SuperImageProvider); ok {
166 assertUnset := func(prop *string, propName string) {
167 if prop != nil && *prop != "" {
168 ctx.PropertyErrorf(propName, "Cannot be set because it's already part of the super image")
169 }
170 }
171 for _, subPartitionType := range android.SortedKeys(info.SubImageInfo) {
172 switch subPartitionType {
173 case "system":
174 assertUnset(a.partitionProps.System_partition_name, "system_partition_name")
175 case "system_ext":
176 assertUnset(a.partitionProps.System_ext_partition_name, "system_ext_partition_name")
177 case "system_dlkm":
178 assertUnset(a.partitionProps.System_dlkm_partition_name, "system_dlkm_partition_name")
179 case "system_other":
180 // TODO
181 case "product":
182 assertUnset(a.partitionProps.Product_partition_name, "product_partition_name")
183 case "vendor":
184 assertUnset(a.partitionProps.Vendor_partition_name, "vendor_partition_name")
185 case "vendor_dlkm":
186 assertUnset(a.partitionProps.Vendor_dlkm_partition_name, "vendor_dlkm_partition_name")
187 case "odm":
188 assertUnset(a.partitionProps.Odm_partition_name, "odm_partition_name")
189 case "odm_dlkm":
190 assertUnset(a.partitionProps.Odm_dlkm_partition_name, "odm_dlkm_partition_name")
191 default:
192 ctx.ModuleErrorf("Unsupported sub-partition of super partition: %q", subPartitionType)
193 }
194 }
195
196 deps = append(deps, info.SuperImage)
197 } else {
198 ctx.ModuleErrorf("Expected super image dep to provide SuperImageProvider")
199 }
200 }
Cole Faust19eb09d2025-01-14 13:27:00 -0800201 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(m android.ModuleProxy) {
mrziwang2fd33a72025-01-08 12:22:08 -0800202 imageOutput, ok := android.OtherModuleProvider(ctx, m, android.OutputFilesProvider)
203 if !ok {
204 ctx.ModuleErrorf("Partition module %s doesn't set OutputfilesProvider", m.Name())
205 }
206 if len(imageOutput.DefaultOutputFiles) != 1 {
207 ctx.ModuleErrorf("Partition module %s should provide exact 1 output file", m.Name())
208 }
209 deps = append(deps, imageOutput.DefaultOutputFiles[0])
210 })
Jihoon Kang3be17162025-01-09 20:51:54 +0000211
Cole Fausta472a6f2025-02-10 16:10:04 -0800212 allImagesZip := android.PathForModuleOut(ctx, "all_images.zip")
213 allImagesZipBuilder := android.NewRuleBuilder(pctx, ctx)
214 cmd := allImagesZipBuilder.Command().BuiltTool("soong_zip").Flag("--sort_entries")
215 for _, dep := range deps {
216 cmd.FlagWithArg("-e ", dep.Base())
217 cmd.FlagWithInput("-f ", dep)
218 }
219 cmd.FlagWithOutput("-o ", allImagesZip)
220 allImagesZipBuilder.Build("soong_all_images_zip", "all_images.zip")
221 a.allImagesZip = allImagesZip
222
Cole Faustb55a41c2025-01-09 16:53:58 -0800223 allImagesStamp := android.PathForModuleOut(ctx, "all_images_stamp")
Cole Faust11fda332025-01-14 16:47:19 -0800224 var validations android.Paths
225 if !ctx.Config().KatiEnabled() && proptools.Bool(a.deviceProps.Main_device) {
Cole Faustb55a41c2025-01-09 16:53:58 -0800226 // In soong-only builds, build this module by default.
227 // This is the analogue to this make code:
228 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/main.mk;l=1396;drc=6595459cdd8164a6008335f6372c9f97b9094060
229 ctx.Phony("droidcore-unbundled", allImagesStamp)
Cole Faust11fda332025-01-14 16:47:19 -0800230
Cole Faust19fbb072025-01-30 18:19:29 -0800231 deps = append(deps, a.copyFilesToProductOutForSoongOnly(ctx))
Cole Faustb55a41c2025-01-09 16:53:58 -0800232 }
Cole Faust11fda332025-01-14 16:47:19 -0800233
234 ctx.Build(pctx, android.BuildParams{
235 Rule: android.Touch,
236 Output: allImagesStamp,
237 Implicits: deps,
238 Validations: validations,
239 })
240
241 // Checkbuilding it causes soong to make a phony, so you can say `m <module name>`
242 ctx.CheckbuildFile(allImagesStamp)
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000243
244 a.setVbmetaPhonyTargets(ctx)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000245
246 a.distFiles(ctx)
247}
248
249func (a *androidDevice) distFiles(ctx android.ModuleContext) {
250 if !ctx.Config().KatiEnabled() {
251 if proptools.Bool(a.deviceProps.Main_device) {
252 fsInfoMap := a.getFsInfos(ctx)
253 for _, partition := range android.SortedKeys(fsInfoMap) {
254 fsInfo := fsInfoMap[partition]
255 if fsInfo.InstalledFiles.Json != nil {
256 ctx.DistForGoal("droidcore-unbundled", fsInfo.InstalledFiles.Json)
257 }
258 if fsInfo.InstalledFiles.Txt != nil {
259 ctx.DistForGoal("droidcore-unbundled", fsInfo.InstalledFiles.Txt)
260 }
261 }
262 }
263 }
264
Cole Faust44080412024-12-20 14:17:07 -0800265}
Jihoon Kangf2c53982024-10-09 17:32:52 +0000266
Cole Fausta472a6f2025-02-10 16:10:04 -0800267func (a *androidDevice) MakeVars(ctx android.MakeVarsModuleContext) {
268 if proptools.Bool(a.deviceProps.Main_device) {
269 ctx.StrictRaw("SOONG_ONLY_ALL_IMAGES_ZIP", a.allImagesZip.String())
270 }
271}
272
Spandan Dasef775742025-01-13 22:17:40 +0000273// Helper structs for target_files.zip creation
Spandan Dasef200ac2025-01-08 01:42:45 +0000274type targetFilesZipCopy struct {
275 srcModule *string
276 destSubdir string
277}
278
Spandan Dasef775742025-01-13 22:17:40 +0000279type targetFilesystemZipCopy struct {
280 fsInfo FilesystemInfo
281 destSubdir string
282}
283
Cole Faust44080412024-12-20 14:17:07 -0800284func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext) {
285 targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir")
286 targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip")
287
288 builder := android.NewRuleBuilder(pctx, ctx)
289 builder.Command().Textf("rm -rf %s", targetFilesDir.String())
290 builder.Command().Textf("mkdir -p %s", targetFilesDir.String())
Spandan Dasef200ac2025-01-08 01:42:45 +0000291 toCopy := []targetFilesZipCopy{
292 targetFilesZipCopy{a.partitionProps.System_partition_name, "SYSTEM"},
293 targetFilesZipCopy{a.partitionProps.System_ext_partition_name, "SYSTEM_EXT"},
294 targetFilesZipCopy{a.partitionProps.Product_partition_name, "PRODUCT"},
295 targetFilesZipCopy{a.partitionProps.Vendor_partition_name, "VENDOR"},
296 targetFilesZipCopy{a.partitionProps.Odm_partition_name, "ODM"},
297 targetFilesZipCopy{a.partitionProps.System_dlkm_partition_name, "SYSTEM_DLKM"},
298 targetFilesZipCopy{a.partitionProps.Vendor_dlkm_partition_name, "VENDOR_DLKM"},
299 targetFilesZipCopy{a.partitionProps.Odm_dlkm_partition_name, "ODM_DLKM"},
300 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "BOOT/RAMDISK"},
301 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "INIT_BOOT/RAMDISK"},
302 targetFilesZipCopy{a.partitionProps.Vendor_boot_partition_name, "VENDOR_BOOT/RAMDISK"},
Spandan Das25649f52025-01-07 18:09:22 +0000303 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000304 // TODO: Handle cases where recovery files are copied to BOOT/ or RECOVERY/
305 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=6211-6219?q=core%2FMakefile&ss=android%2Fplatform%2Fsuperproject%2Fmain
306 if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() {
307 toCopy = append(toCopy, targetFilesZipCopy{a.partitionProps.Recovery_partition_name, "VENDOR_BOOT/RAMDISK"})
308 }
309
Spandan Dasef775742025-01-13 22:17:40 +0000310 filesystemsToCopy := []targetFilesystemZipCopy{}
Spandan Dasef200ac2025-01-08 01:42:45 +0000311 for _, zipCopy := range toCopy {
312 if zipCopy.srcModule == nil {
Spandan Das25649f52025-01-07 18:09:22 +0000313 continue
314 }
Spandan Dasef775742025-01-13 22:17:40 +0000315 filesystemsToCopy = append(
316 filesystemsToCopy,
317 targetFilesystemZipCopy{a.getFilesystemInfo(ctx, *zipCopy.srcModule), zipCopy.destSubdir},
318 )
319 }
320 // Get additional filesystems from super_partition dependency
321 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800322 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000323 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
324 for _, partition := range android.SortedStringKeys(info.SubImageInfo) {
325 filesystemsToCopy = append(
326 filesystemsToCopy,
327 targetFilesystemZipCopy{info.SubImageInfo[partition], strings.ToUpper(partition)},
328 )
329 }
330 } else {
331 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
332 }
333 }
334
335 for _, toCopy := range filesystemsToCopy {
336 rootDirString := toCopy.fsInfo.RootDir.String()
337 if toCopy.destSubdir == "SYSTEM" {
Spandan Dasef200ac2025-01-08 01:42:45 +0000338 rootDirString = rootDirString + "/system"
339 }
Spandan Dasef775742025-01-13 22:17:40 +0000340 builder.Command().Textf("mkdir -p %s/%s", targetFilesDir.String(), toCopy.destSubdir)
Cole Faust44080412024-12-20 14:17:07 -0800341 builder.Command().
342 BuiltTool("acp").
Spandan Dasef775742025-01-13 22:17:40 +0000343 Textf("-rd %s/. %s/%s", rootDirString, targetFilesDir, toCopy.destSubdir).
344 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
Spandan Dasef200ac2025-01-08 01:42:45 +0000345
Spandan Dasef775742025-01-13 22:17:40 +0000346 if toCopy.destSubdir == "SYSTEM" {
Spandan Das3ec6d062025-01-09 19:37:47 +0000347 // Create the ROOT partition in target_files.zip
Spandan Dasef775742025-01-13 22:17:40 +0000348 builder.Command().Textf("rsync --links --exclude=system/* %s/ -r %s/ROOT", toCopy.fsInfo.RootDir, targetFilesDir.String())
Spandan Das3ec6d062025-01-09 19:37:47 +0000349 }
Cole Faust44080412024-12-20 14:17:07 -0800350 }
Spandan Das9b17df22025-01-08 23:30:45 +0000351 // Copy cmdline, kernel etc. files of boot images
Spandan Das258c08f2025-01-08 23:30:45 +0000352 if a.partitionProps.Vendor_boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800353 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Vendor_boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000354 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000355 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
356 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/vendor_cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
357 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000358 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/VENDOR_BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000359 }
Spandan Das23511372025-01-08 23:30:45 +0000360 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000361 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/VENDOR_BOOT/vendor_bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000362 }
Spandan Das258c08f2025-01-08 23:30:45 +0000363 }
364 if a.partitionProps.Boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800365 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000366 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000367 builder.Command().Textf("echo %s > %s/BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
368 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000369 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000370 }
371 if bootImgInfo.Kernel != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000372 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000373 // Even though kernel is not used to build vendor_boot, copy the kernel to VENDOR_BOOT to match the behavior of make packaging.
Spandan Dasfed3d042025-01-13 21:38:47 +0000374 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/VENDOR_BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000375 }
Spandan Das23511372025-01-08 23:30:45 +0000376 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000377 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/BOOT/bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000378 }
Spandan Das258c08f2025-01-08 23:30:45 +0000379 }
380
Spandan Dase51ff952025-01-09 18:11:59 +0000381 if a.deviceProps.Android_info != nil {
382 builder.Command().Textf("mkdir -p %s/OTA", targetFilesDir)
Cole Faust11fda332025-01-14 16:47:19 -0800383 builder.Command().Textf("cp ").Input(android.PathForModuleSrc(ctx, *a.deviceProps.Android_info)).Textf(" %s/OTA/android-info.txt", targetFilesDir)
Spandan Dase51ff952025-01-09 18:11:59 +0000384 }
385
Spandan Das0036fa32025-01-10 23:40:45 +0000386 a.copyImagesToTargetZip(ctx, builder, targetFilesDir)
Spandan Das29d44882025-01-15 21:12:36 +0000387 a.copyMetadataToTargetZip(ctx, builder, targetFilesDir)
Spandan Das0036fa32025-01-10 23:40:45 +0000388
Cole Faust44080412024-12-20 14:17:07 -0800389 builder.Command().
390 BuiltTool("soong_zip").
391 Text("-d").
392 FlagWithOutput("-o ", targetFilesZip).
393 FlagWithArg("-C ", targetFilesDir.String()).
394 FlagWithArg("-D ", targetFilesDir.String()).
395 Text("-sha256")
396 builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip")
397}
398
Spandan Das0036fa32025-01-10 23:40:45 +0000399func (a *androidDevice) copyImagesToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath) {
400 // Create an IMAGES/ subdirectory
401 builder.Command().Textf("mkdir -p %s/IMAGES", targetFilesDir.String())
402 if a.deviceProps.Bootloader != nil {
403 builder.Command().Textf("cp ").Input(android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Bootloader))).Textf(" %s/IMAGES/bootloader", targetFilesDir.String())
404 }
405 // Copy the filesystem ,boot and vbmeta img files to IMAGES/
406 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(child android.ModuleProxy) {
Spandan Dasa9db76d2025-01-14 01:34:43 +0000407 if strings.Contains(child.Name(), "recovery") {
408 return // skip recovery.img to match the make packaging behavior
409 }
Spandan Dasef775742025-01-13 22:17:40 +0000410 if info, ok := android.OtherModuleProvider(ctx, child, BootimgInfoProvider); ok {
411 // Check Boot img first so that the boot.img is copied and not its dep ramdisk.img
Spandan Das0036fa32025-01-10 23:40:45 +0000412 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
Spandan Dasef775742025-01-13 22:17:40 +0000413 } else if info, ok := android.OtherModuleProvider(ctx, child, FilesystemProvider); ok {
Spandan Das0036fa32025-01-10 23:40:45 +0000414 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
415 } else if info, ok := android.OtherModuleProvider(ctx, child, vbmetaPartitionProvider); ok {
416 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
417 } else {
418 ctx.ModuleErrorf("Module %s does not provide an .img file output for target_files.zip", child.Name())
419 }
420 })
Spandan Dasef775742025-01-13 22:17:40 +0000421
422 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800423 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000424 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust19eb09d2025-01-14 13:27:00 -0800425 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Das7a42d1c2025-02-12 01:32:21 +0000426 if info.SubImageInfo[partition].OutputHermetic != nil {
427 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].OutputHermetic).Textf(" %s/IMAGES/", targetFilesDir.String())
428 }
429 if info.SubImageInfo[partition].MapFile != nil {
430 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].MapFile).Textf(" %s/IMAGES/", targetFilesDir.String())
431 }
Spandan Dasef775742025-01-13 22:17:40 +0000432 }
433 } else {
434 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
435 }
436 }
Spandan Das0036fa32025-01-10 23:40:45 +0000437}
438
Spandan Das29d44882025-01-15 21:12:36 +0000439func (a *androidDevice) copyMetadataToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath) {
440 // Create a META/ subdirectory
441 builder.Command().Textf("mkdir -p %s/META", targetFilesDir.String())
442 if proptools.Bool(a.deviceProps.Ab_ota_updater) {
443 ctx.VisitDirectDepsProxyWithTag(targetFilesMetadataDepTag, func(child android.ModuleProxy) {
444 info, _ := android.OtherModuleProvider(ctx, child, android.OutputFilesProvider)
445 builder.Command().Textf("cp").Inputs(info.DefaultOutputFiles).Textf(" %s/META/", targetFilesDir.String())
446 })
Spandan Dasb5f40cf2025-02-12 19:36:03 +0000447 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "external/zucchini/version_info.h")).Textf(" %s/META/zucchini_config.txt", targetFilesDir.String())
448 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "system/update_engine/update_engine.conf")).Textf(" %s/META/update_engine_config.txt", targetFilesDir.String())
449 if a.getFsInfos(ctx)["system"].ErofsCompressHints != nil {
450 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].ErofsCompressHints).Textf(" %s/META/erofs_default_compress_hints.txt", targetFilesDir.String())
451 }
452 // ab_partitions.txt
453 abPartitionsSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_partitions)
454 abPartitionsSortedString := proptools.ShellEscape(strings.Join(abPartitionsSorted, "\\n"))
455 builder.Command().Textf("echo -e").Flag(abPartitionsSortedString).Textf(" > %s/META/ab_partitions.txt", targetFilesDir.String())
Spandan Das35b78742025-02-12 19:36:03 +0000456 // otakeys.txt
457 abOtaKeysSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_keys)
458 abOtaKeysSortedString := proptools.ShellEscape(strings.Join(abOtaKeysSorted, "\\n"))
459 builder.Command().Textf("echo -e").Flag(abOtaKeysSortedString).Textf(" > %s/META/otakeys.txt", targetFilesDir.String())
Spandan Das00948072025-02-12 19:36:03 +0000460 // postinstall_config.txt
461 abOtaPostInstallConfigString := proptools.ShellEscape(strings.Join(a.deviceProps.Ab_ota_postinstall_config, "\\n"))
462 builder.Command().Textf("echo -e").Flag(abOtaPostInstallConfigString).Textf(" > %s/META/postinstall_config.txt", targetFilesDir.String())
Spandan Dasf12ff9b2025-02-12 22:27:43 +0000463 // selinuxfc
464 if a.getFsInfos(ctx)["system"].SelinuxFc != nil {
465 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].SelinuxFc).Textf(" %s/META/file_contexts.bin", targetFilesDir.String())
466 }
Spandan Dasd71af182025-02-12 18:03:29 +0000467 }
Spandan Dasdd262fb2025-02-13 00:15:59 +0000468 // Copy $partition_filesystem_config.txt
469 fsInfos := a.getFsInfos(ctx)
470 for _, partition := range android.SortedKeys(fsInfos) {
471 if fsInfos[partition].FilesystemConfig == nil {
472 continue
473 }
474 if android.InList(partition, []string{"userdata"}) {
475 continue
476 }
477 builder.Command().Textf("cp").Input(fsInfos[partition].FilesystemConfig).Textf(" %s/META/%s", targetFilesDir.String(), a.filesystemConfigNameForTargetFiles(partition))
478 }
Spandan Das75955b12025-02-13 22:12:52 +0000479 // Copy ramdisk_node_list
480 builder.Command().Textf("cp").Input(android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Ramdisk_node_list))).Textf(" %s/META/", targetFilesDir.String())
Spandan Dasdd262fb2025-02-13 00:15:59 +0000481}
482
483// Filenames for the partition specific fs_config files.
484// Hardcode the ramdisk files to their boot image prefix
485func (a *androidDevice) filesystemConfigNameForTargetFiles(partition string) string {
486 name := partition + "_filesystem_config.txt"
487 if partition == "system" {
488 name = "filesystem_config.txt"
489 } else if partition == "ramdisk" {
490 name = "init_boot_filesystem_config.txt"
491 }
492 return name
Spandan Das29d44882025-01-15 21:12:36 +0000493}
494
Cole Faust44080412024-12-20 14:17:07 -0800495func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo {
Cole Faust19eb09d2025-01-14 13:27:00 -0800496 fsMod := ctx.GetDirectDepProxyWithTag(depName, filesystemDepTag)
Cole Faust44080412024-12-20 14:17:07 -0800497 fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider)
498 if !ok {
499 ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName)
500 }
501 return fsInfo
Jihoon Kangf2c53982024-10-09 17:32:52 +0000502}
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000503
504func (a *androidDevice) setVbmetaPhonyTargets(ctx android.ModuleContext) {
505 if !proptools.Bool(a.deviceProps.Main_device) {
506 return
507 }
508
509 if !ctx.Config().KatiEnabled() {
510 for _, vbmetaPartitionName := range a.partitionProps.Vbmeta_partitions {
511 img := ctx.GetDirectDepProxyWithTag(vbmetaPartitionName, filesystemDepTag)
512 if provider, ok := android.OtherModuleProvider(ctx, img, vbmetaPartitionProvider); ok {
513 // make generates `vbmetasystemimage` phony target instead of `vbmeta_systemimage` phony target.
514 partitionName := strings.ReplaceAll(provider.Name, "_", "")
515 ctx.Phony(fmt.Sprintf("%simage", partitionName), provider.Output)
516 }
517 }
518 }
519}