blob: d42927a6ed493c3678cb5aadf8cb8c6aeb64ebd2 [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 Dasb5f40cf2025-02-12 19:36:03 +000073 Ab_ota_updater *bool
74 Ab_ota_partitions []string
Jihoon Kang3be17162025-01-09 20:51:54 +000075}
76
Jihoon Kangf2c53982024-10-09 17:32:52 +000077type androidDevice struct {
78 android.ModuleBase
79
80 partitionProps PartitionNameProperties
Jihoon Kang3be17162025-01-09 20:51:54 +000081
82 deviceProps DeviceProperties
Cole Fausta472a6f2025-02-10 16:10:04 -080083
84 allImagesZip android.Path
Jihoon Kangf2c53982024-10-09 17:32:52 +000085}
86
87func AndroidDeviceFactory() android.Module {
88 module := &androidDevice{}
Jihoon Kang3be17162025-01-09 20:51:54 +000089 module.AddProperties(&module.partitionProps, &module.deviceProps)
Cole Faust341d5f12025-01-07 15:32:38 -080090 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jihoon Kangf2c53982024-10-09 17:32:52 +000091 return module
92}
93
Cole Faust11fda332025-01-14 16:47:19 -080094var numMainAndroidDevicesOnceKey android.OnceKey = android.NewOnceKey("num_auto_generated_anroid_devices")
Cole Faustb55a41c2025-01-09 16:53:58 -080095
Jihoon Kangf2c53982024-10-09 17:32:52 +000096type partitionDepTagType struct {
97 blueprint.BaseDependencyTag
98}
99
Cole Faust2bdc5e52025-01-10 10:29:36 -0800100type superPartitionDepTagType struct {
101 blueprint.BaseDependencyTag
102}
Spandan Das29d44882025-01-15 21:12:36 +0000103type targetFilesMetadataDepTagType struct {
104 blueprint.BaseDependencyTag
105}
Cole Faust2bdc5e52025-01-10 10:29:36 -0800106
107var superPartitionDepTag superPartitionDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000108var filesystemDepTag partitionDepTagType
Spandan Das29d44882025-01-15 21:12:36 +0000109var targetFilesMetadataDepTag targetFilesMetadataDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000110
111func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
112 addDependencyIfDefined := func(dep *string) {
113 if dep != nil {
Cole Faust341d5f12025-01-07 15:32:38 -0800114 ctx.AddDependency(ctx.Module(), filesystemDepTag, proptools.String(dep))
Jihoon Kangf2c53982024-10-09 17:32:52 +0000115 }
116 }
117
Cole Faust2bdc5e52025-01-10 10:29:36 -0800118 if a.partitionProps.Super_partition_name != nil {
119 ctx.AddDependency(ctx.Module(), superPartitionDepTag, *a.partitionProps.Super_partition_name)
120 }
Jihoon Kangf2c53982024-10-09 17:32:52 +0000121 addDependencyIfDefined(a.partitionProps.Boot_partition_name)
Jihoon Kang9e087002025-01-08 19:12:23 +0000122 addDependencyIfDefined(a.partitionProps.Init_boot_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000123 addDependencyIfDefined(a.partitionProps.Vendor_boot_partition_name)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000124 addDependencyIfDefined(a.partitionProps.System_partition_name)
125 addDependencyIfDefined(a.partitionProps.System_ext_partition_name)
126 addDependencyIfDefined(a.partitionProps.Product_partition_name)
127 addDependencyIfDefined(a.partitionProps.Vendor_partition_name)
Spandan Dasc5717162024-11-01 18:33:57 +0000128 addDependencyIfDefined(a.partitionProps.Odm_partition_name)
mrziwang23ba8762024-11-07 16:21:53 -0800129 addDependencyIfDefined(a.partitionProps.Userdata_partition_name)
Spandan Dasa0394002025-01-07 18:38:34 +0000130 addDependencyIfDefined(a.partitionProps.System_dlkm_partition_name)
131 addDependencyIfDefined(a.partitionProps.Vendor_dlkm_partition_name)
132 addDependencyIfDefined(a.partitionProps.Odm_dlkm_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000133 addDependencyIfDefined(a.partitionProps.Recovery_partition_name)
Cole Faust3552eb62024-11-06 18:07:26 -0800134 for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions {
135 ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition)
136 }
Spandan Das29d44882025-01-15 21:12:36 +0000137 a.addDepsForTargetFilesMetadata(ctx)
138}
139
140func (a *androidDevice) addDepsForTargetFilesMetadata(ctx android.BottomUpMutatorContext) {
141 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), targetFilesMetadataDepTag, "liblz4") // host variant
Jihoon Kangf2c53982024-10-09 17:32:52 +0000142}
143
Cole Faust11fda332025-01-14 16:47:19 -0800144func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) {
145 if proptools.Bool(a.deviceProps.Main_device) {
146 numMainAndroidDevices := ctx.Config().Once(numMainAndroidDevicesOnceKey, func() interface{} {
147 return &atomic.Int32{}
148 }).(*atomic.Int32)
149 total := numMainAndroidDevices.Add(1)
150 if total > 1 {
151 // There should only be 1 main android_device module. That one will be
152 // made the default thing to build in soong-only builds.
153 ctx.ModuleErrorf("There cannot be more than 1 main android_device module")
154 }
Jihoon Kang3be17162025-01-09 20:51:54 +0000155 }
156
Spandan Das5ef1a9c2025-02-11 18:50:17 +0000157 a.buildTargetFilesZip(ctx)
mrziwang2fd33a72025-01-08 12:22:08 -0800158 var deps []android.Path
Cole Faust2bdc5e52025-01-10 10:29:36 -0800159 if proptools.String(a.partitionProps.Super_partition_name) != "" {
Cole Faust19eb09d2025-01-14 13:27:00 -0800160 superImage := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Cole Faust2bdc5e52025-01-10 10:29:36 -0800161 if info, ok := android.OtherModuleProvider(ctx, superImage, SuperImageProvider); ok {
162 assertUnset := func(prop *string, propName string) {
163 if prop != nil && *prop != "" {
164 ctx.PropertyErrorf(propName, "Cannot be set because it's already part of the super image")
165 }
166 }
167 for _, subPartitionType := range android.SortedKeys(info.SubImageInfo) {
168 switch subPartitionType {
169 case "system":
170 assertUnset(a.partitionProps.System_partition_name, "system_partition_name")
171 case "system_ext":
172 assertUnset(a.partitionProps.System_ext_partition_name, "system_ext_partition_name")
173 case "system_dlkm":
174 assertUnset(a.partitionProps.System_dlkm_partition_name, "system_dlkm_partition_name")
175 case "system_other":
176 // TODO
177 case "product":
178 assertUnset(a.partitionProps.Product_partition_name, "product_partition_name")
179 case "vendor":
180 assertUnset(a.partitionProps.Vendor_partition_name, "vendor_partition_name")
181 case "vendor_dlkm":
182 assertUnset(a.partitionProps.Vendor_dlkm_partition_name, "vendor_dlkm_partition_name")
183 case "odm":
184 assertUnset(a.partitionProps.Odm_partition_name, "odm_partition_name")
185 case "odm_dlkm":
186 assertUnset(a.partitionProps.Odm_dlkm_partition_name, "odm_dlkm_partition_name")
187 default:
188 ctx.ModuleErrorf("Unsupported sub-partition of super partition: %q", subPartitionType)
189 }
190 }
191
192 deps = append(deps, info.SuperImage)
193 } else {
194 ctx.ModuleErrorf("Expected super image dep to provide SuperImageProvider")
195 }
196 }
Cole Faust19eb09d2025-01-14 13:27:00 -0800197 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(m android.ModuleProxy) {
mrziwang2fd33a72025-01-08 12:22:08 -0800198 imageOutput, ok := android.OtherModuleProvider(ctx, m, android.OutputFilesProvider)
199 if !ok {
200 ctx.ModuleErrorf("Partition module %s doesn't set OutputfilesProvider", m.Name())
201 }
202 if len(imageOutput.DefaultOutputFiles) != 1 {
203 ctx.ModuleErrorf("Partition module %s should provide exact 1 output file", m.Name())
204 }
205 deps = append(deps, imageOutput.DefaultOutputFiles[0])
206 })
Jihoon Kang3be17162025-01-09 20:51:54 +0000207
Cole Fausta472a6f2025-02-10 16:10:04 -0800208 allImagesZip := android.PathForModuleOut(ctx, "all_images.zip")
209 allImagesZipBuilder := android.NewRuleBuilder(pctx, ctx)
210 cmd := allImagesZipBuilder.Command().BuiltTool("soong_zip").Flag("--sort_entries")
211 for _, dep := range deps {
212 cmd.FlagWithArg("-e ", dep.Base())
213 cmd.FlagWithInput("-f ", dep)
214 }
215 cmd.FlagWithOutput("-o ", allImagesZip)
216 allImagesZipBuilder.Build("soong_all_images_zip", "all_images.zip")
217 a.allImagesZip = allImagesZip
218
Cole Faustb55a41c2025-01-09 16:53:58 -0800219 allImagesStamp := android.PathForModuleOut(ctx, "all_images_stamp")
Cole Faust11fda332025-01-14 16:47:19 -0800220 var validations android.Paths
221 if !ctx.Config().KatiEnabled() && proptools.Bool(a.deviceProps.Main_device) {
Cole Faustb55a41c2025-01-09 16:53:58 -0800222 // In soong-only builds, build this module by default.
223 // This is the analogue to this make code:
224 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/main.mk;l=1396;drc=6595459cdd8164a6008335f6372c9f97b9094060
225 ctx.Phony("droidcore-unbundled", allImagesStamp)
Cole Faust11fda332025-01-14 16:47:19 -0800226
Cole Faust19fbb072025-01-30 18:19:29 -0800227 deps = append(deps, a.copyFilesToProductOutForSoongOnly(ctx))
Cole Faustb55a41c2025-01-09 16:53:58 -0800228 }
Cole Faust11fda332025-01-14 16:47:19 -0800229
230 ctx.Build(pctx, android.BuildParams{
231 Rule: android.Touch,
232 Output: allImagesStamp,
233 Implicits: deps,
234 Validations: validations,
235 })
236
237 // Checkbuilding it causes soong to make a phony, so you can say `m <module name>`
238 ctx.CheckbuildFile(allImagesStamp)
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000239
240 a.setVbmetaPhonyTargets(ctx)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000241
242 a.distFiles(ctx)
243}
244
245func (a *androidDevice) distFiles(ctx android.ModuleContext) {
246 if !ctx.Config().KatiEnabled() {
247 if proptools.Bool(a.deviceProps.Main_device) {
248 fsInfoMap := a.getFsInfos(ctx)
249 for _, partition := range android.SortedKeys(fsInfoMap) {
250 fsInfo := fsInfoMap[partition]
251 if fsInfo.InstalledFiles.Json != nil {
252 ctx.DistForGoal("droidcore-unbundled", fsInfo.InstalledFiles.Json)
253 }
254 if fsInfo.InstalledFiles.Txt != nil {
255 ctx.DistForGoal("droidcore-unbundled", fsInfo.InstalledFiles.Txt)
256 }
257 }
258 }
259 }
260
Cole Faust44080412024-12-20 14:17:07 -0800261}
Jihoon Kangf2c53982024-10-09 17:32:52 +0000262
Cole Fausta472a6f2025-02-10 16:10:04 -0800263func (a *androidDevice) MakeVars(ctx android.MakeVarsModuleContext) {
264 if proptools.Bool(a.deviceProps.Main_device) {
265 ctx.StrictRaw("SOONG_ONLY_ALL_IMAGES_ZIP", a.allImagesZip.String())
266 }
267}
268
Spandan Dasef775742025-01-13 22:17:40 +0000269// Helper structs for target_files.zip creation
Spandan Dasef200ac2025-01-08 01:42:45 +0000270type targetFilesZipCopy struct {
271 srcModule *string
272 destSubdir string
273}
274
Spandan Dasef775742025-01-13 22:17:40 +0000275type targetFilesystemZipCopy struct {
276 fsInfo FilesystemInfo
277 destSubdir string
278}
279
Cole Faust44080412024-12-20 14:17:07 -0800280func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext) {
281 targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir")
282 targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip")
283
284 builder := android.NewRuleBuilder(pctx, ctx)
285 builder.Command().Textf("rm -rf %s", targetFilesDir.String())
286 builder.Command().Textf("mkdir -p %s", targetFilesDir.String())
Spandan Dasef200ac2025-01-08 01:42:45 +0000287 toCopy := []targetFilesZipCopy{
288 targetFilesZipCopy{a.partitionProps.System_partition_name, "SYSTEM"},
289 targetFilesZipCopy{a.partitionProps.System_ext_partition_name, "SYSTEM_EXT"},
290 targetFilesZipCopy{a.partitionProps.Product_partition_name, "PRODUCT"},
291 targetFilesZipCopy{a.partitionProps.Vendor_partition_name, "VENDOR"},
292 targetFilesZipCopy{a.partitionProps.Odm_partition_name, "ODM"},
293 targetFilesZipCopy{a.partitionProps.System_dlkm_partition_name, "SYSTEM_DLKM"},
294 targetFilesZipCopy{a.partitionProps.Vendor_dlkm_partition_name, "VENDOR_DLKM"},
295 targetFilesZipCopy{a.partitionProps.Odm_dlkm_partition_name, "ODM_DLKM"},
296 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "BOOT/RAMDISK"},
297 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "INIT_BOOT/RAMDISK"},
298 targetFilesZipCopy{a.partitionProps.Vendor_boot_partition_name, "VENDOR_BOOT/RAMDISK"},
Spandan Das25649f52025-01-07 18:09:22 +0000299 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000300 // TODO: Handle cases where recovery files are copied to BOOT/ or RECOVERY/
301 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=6211-6219?q=core%2FMakefile&ss=android%2Fplatform%2Fsuperproject%2Fmain
302 if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() {
303 toCopy = append(toCopy, targetFilesZipCopy{a.partitionProps.Recovery_partition_name, "VENDOR_BOOT/RAMDISK"})
304 }
305
Spandan Dasef775742025-01-13 22:17:40 +0000306 filesystemsToCopy := []targetFilesystemZipCopy{}
Spandan Dasef200ac2025-01-08 01:42:45 +0000307 for _, zipCopy := range toCopy {
308 if zipCopy.srcModule == nil {
Spandan Das25649f52025-01-07 18:09:22 +0000309 continue
310 }
Spandan Dasef775742025-01-13 22:17:40 +0000311 filesystemsToCopy = append(
312 filesystemsToCopy,
313 targetFilesystemZipCopy{a.getFilesystemInfo(ctx, *zipCopy.srcModule), zipCopy.destSubdir},
314 )
315 }
316 // Get additional filesystems from super_partition dependency
317 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800318 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000319 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
320 for _, partition := range android.SortedStringKeys(info.SubImageInfo) {
321 filesystemsToCopy = append(
322 filesystemsToCopy,
323 targetFilesystemZipCopy{info.SubImageInfo[partition], strings.ToUpper(partition)},
324 )
325 }
326 } else {
327 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
328 }
329 }
330
331 for _, toCopy := range filesystemsToCopy {
332 rootDirString := toCopy.fsInfo.RootDir.String()
333 if toCopy.destSubdir == "SYSTEM" {
Spandan Dasef200ac2025-01-08 01:42:45 +0000334 rootDirString = rootDirString + "/system"
335 }
Spandan Dasef775742025-01-13 22:17:40 +0000336 builder.Command().Textf("mkdir -p %s/%s", targetFilesDir.String(), toCopy.destSubdir)
Cole Faust44080412024-12-20 14:17:07 -0800337 builder.Command().
338 BuiltTool("acp").
Spandan Dasef775742025-01-13 22:17:40 +0000339 Textf("-rd %s/. %s/%s", rootDirString, targetFilesDir, toCopy.destSubdir).
340 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
Spandan Dasef200ac2025-01-08 01:42:45 +0000341
Spandan Dasef775742025-01-13 22:17:40 +0000342 if toCopy.destSubdir == "SYSTEM" {
Spandan Das3ec6d062025-01-09 19:37:47 +0000343 // Create the ROOT partition in target_files.zip
Spandan Dasef775742025-01-13 22:17:40 +0000344 builder.Command().Textf("rsync --links --exclude=system/* %s/ -r %s/ROOT", toCopy.fsInfo.RootDir, targetFilesDir.String())
Spandan Das3ec6d062025-01-09 19:37:47 +0000345 }
Cole Faust44080412024-12-20 14:17:07 -0800346 }
Spandan Das9b17df22025-01-08 23:30:45 +0000347 // Copy cmdline, kernel etc. files of boot images
Spandan Das258c08f2025-01-08 23:30:45 +0000348 if a.partitionProps.Vendor_boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800349 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Vendor_boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000350 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000351 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
352 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/vendor_cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
353 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000354 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/VENDOR_BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000355 }
Spandan Das23511372025-01-08 23:30:45 +0000356 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000357 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/VENDOR_BOOT/vendor_bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000358 }
Spandan Das258c08f2025-01-08 23:30:45 +0000359 }
360 if a.partitionProps.Boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800361 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000362 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000363 builder.Command().Textf("echo %s > %s/BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
364 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000365 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000366 }
367 if bootImgInfo.Kernel != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000368 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000369 // 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 +0000370 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/VENDOR_BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000371 }
Spandan Das23511372025-01-08 23:30:45 +0000372 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000373 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/BOOT/bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000374 }
Spandan Das258c08f2025-01-08 23:30:45 +0000375 }
376
Spandan Dase51ff952025-01-09 18:11:59 +0000377 if a.deviceProps.Android_info != nil {
378 builder.Command().Textf("mkdir -p %s/OTA", targetFilesDir)
Cole Faust11fda332025-01-14 16:47:19 -0800379 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 +0000380 }
381
Spandan Das0036fa32025-01-10 23:40:45 +0000382 a.copyImagesToTargetZip(ctx, builder, targetFilesDir)
Spandan Das29d44882025-01-15 21:12:36 +0000383 a.copyMetadataToTargetZip(ctx, builder, targetFilesDir)
Spandan Das0036fa32025-01-10 23:40:45 +0000384
Cole Faust44080412024-12-20 14:17:07 -0800385 builder.Command().
386 BuiltTool("soong_zip").
387 Text("-d").
388 FlagWithOutput("-o ", targetFilesZip).
389 FlagWithArg("-C ", targetFilesDir.String()).
390 FlagWithArg("-D ", targetFilesDir.String()).
391 Text("-sha256")
392 builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip")
393}
394
Spandan Das0036fa32025-01-10 23:40:45 +0000395func (a *androidDevice) copyImagesToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath) {
396 // Create an IMAGES/ subdirectory
397 builder.Command().Textf("mkdir -p %s/IMAGES", targetFilesDir.String())
398 if a.deviceProps.Bootloader != nil {
399 builder.Command().Textf("cp ").Input(android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Bootloader))).Textf(" %s/IMAGES/bootloader", targetFilesDir.String())
400 }
401 // Copy the filesystem ,boot and vbmeta img files to IMAGES/
402 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(child android.ModuleProxy) {
Spandan Dasa9db76d2025-01-14 01:34:43 +0000403 if strings.Contains(child.Name(), "recovery") {
404 return // skip recovery.img to match the make packaging behavior
405 }
Spandan Dasef775742025-01-13 22:17:40 +0000406 if info, ok := android.OtherModuleProvider(ctx, child, BootimgInfoProvider); ok {
407 // Check Boot img first so that the boot.img is copied and not its dep ramdisk.img
Spandan Das0036fa32025-01-10 23:40:45 +0000408 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
Spandan Dasef775742025-01-13 22:17:40 +0000409 } else if info, ok := android.OtherModuleProvider(ctx, child, FilesystemProvider); ok {
Spandan Das0036fa32025-01-10 23:40:45 +0000410 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
411 } else if info, ok := android.OtherModuleProvider(ctx, child, vbmetaPartitionProvider); ok {
412 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
413 } else {
414 ctx.ModuleErrorf("Module %s does not provide an .img file output for target_files.zip", child.Name())
415 }
416 })
Spandan Dasef775742025-01-13 22:17:40 +0000417
418 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800419 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000420 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust19eb09d2025-01-14 13:27:00 -0800421 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Das7a42d1c2025-02-12 01:32:21 +0000422 if info.SubImageInfo[partition].OutputHermetic != nil {
423 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].OutputHermetic).Textf(" %s/IMAGES/", targetFilesDir.String())
424 }
425 if info.SubImageInfo[partition].MapFile != nil {
426 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].MapFile).Textf(" %s/IMAGES/", targetFilesDir.String())
427 }
Spandan Dasef775742025-01-13 22:17:40 +0000428 }
429 } else {
430 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
431 }
432 }
Spandan Das0036fa32025-01-10 23:40:45 +0000433}
434
Spandan Das29d44882025-01-15 21:12:36 +0000435func (a *androidDevice) copyMetadataToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath) {
436 // Create a META/ subdirectory
437 builder.Command().Textf("mkdir -p %s/META", targetFilesDir.String())
438 if proptools.Bool(a.deviceProps.Ab_ota_updater) {
439 ctx.VisitDirectDepsProxyWithTag(targetFilesMetadataDepTag, func(child android.ModuleProxy) {
440 info, _ := android.OtherModuleProvider(ctx, child, android.OutputFilesProvider)
441 builder.Command().Textf("cp").Inputs(info.DefaultOutputFiles).Textf(" %s/META/", targetFilesDir.String())
442 })
Spandan Dasb5f40cf2025-02-12 19:36:03 +0000443 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "external/zucchini/version_info.h")).Textf(" %s/META/zucchini_config.txt", targetFilesDir.String())
444 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "system/update_engine/update_engine.conf")).Textf(" %s/META/update_engine_config.txt", targetFilesDir.String())
445 if a.getFsInfos(ctx)["system"].ErofsCompressHints != nil {
446 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].ErofsCompressHints).Textf(" %s/META/erofs_default_compress_hints.txt", targetFilesDir.String())
447 }
448 // ab_partitions.txt
449 abPartitionsSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_partitions)
450 abPartitionsSortedString := proptools.ShellEscape(strings.Join(abPartitionsSorted, "\\n"))
451 builder.Command().Textf("echo -e").Flag(abPartitionsSortedString).Textf(" > %s/META/ab_partitions.txt", targetFilesDir.String())
Spandan Dasd71af182025-02-12 18:03:29 +0000452 }
Spandan Das29d44882025-01-15 21:12:36 +0000453}
454
Cole Faust44080412024-12-20 14:17:07 -0800455func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo {
Cole Faust19eb09d2025-01-14 13:27:00 -0800456 fsMod := ctx.GetDirectDepProxyWithTag(depName, filesystemDepTag)
Cole Faust44080412024-12-20 14:17:07 -0800457 fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider)
458 if !ok {
459 ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName)
460 }
461 return fsInfo
Jihoon Kangf2c53982024-10-09 17:32:52 +0000462}
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000463
464func (a *androidDevice) setVbmetaPhonyTargets(ctx android.ModuleContext) {
465 if !proptools.Bool(a.deviceProps.Main_device) {
466 return
467 }
468
469 if !ctx.Config().KatiEnabled() {
470 for _, vbmetaPartitionName := range a.partitionProps.Vbmeta_partitions {
471 img := ctx.GetDirectDepProxyWithTag(vbmetaPartitionName, filesystemDepTag)
472 if provider, ok := android.OtherModuleProvider(ctx, img, vbmetaPartitionProvider); ok {
473 // make generates `vbmetasystemimage` phony target instead of `vbmeta_systemimage` phony target.
474 partitionName := strings.ReplaceAll(provider.Name, "_", "")
475 ctx.Phony(fmt.Sprintf("%simage", partitionName), provider.Output)
476 }
477 }
478 }
479}