blob: 514fd2816545f8c923a7c70ddc55c042e591855e [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
Spandan Das35b78742025-02-12 19:36:03 +000075 Ab_ota_keys []string
Jihoon Kang3be17162025-01-09 20:51:54 +000076}
77
Jihoon Kangf2c53982024-10-09 17:32:52 +000078type androidDevice struct {
79 android.ModuleBase
80
81 partitionProps PartitionNameProperties
Jihoon Kang3be17162025-01-09 20:51:54 +000082
83 deviceProps DeviceProperties
Cole Fausta472a6f2025-02-10 16:10:04 -080084
85 allImagesZip android.Path
Jihoon Kangf2c53982024-10-09 17:32:52 +000086}
87
88func AndroidDeviceFactory() android.Module {
89 module := &androidDevice{}
Jihoon Kang3be17162025-01-09 20:51:54 +000090 module.AddProperties(&module.partitionProps, &module.deviceProps)
Cole Faust341d5f12025-01-07 15:32:38 -080091 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jihoon Kangf2c53982024-10-09 17:32:52 +000092 return module
93}
94
Cole Faust11fda332025-01-14 16:47:19 -080095var numMainAndroidDevicesOnceKey android.OnceKey = android.NewOnceKey("num_auto_generated_anroid_devices")
Cole Faustb55a41c2025-01-09 16:53:58 -080096
Jihoon Kangf2c53982024-10-09 17:32:52 +000097type partitionDepTagType struct {
98 blueprint.BaseDependencyTag
99}
100
Cole Faust2bdc5e52025-01-10 10:29:36 -0800101type superPartitionDepTagType struct {
102 blueprint.BaseDependencyTag
103}
Spandan Das29d44882025-01-15 21:12:36 +0000104type targetFilesMetadataDepTagType struct {
105 blueprint.BaseDependencyTag
106}
Cole Faust2bdc5e52025-01-10 10:29:36 -0800107
108var superPartitionDepTag superPartitionDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000109var filesystemDepTag partitionDepTagType
Spandan Das29d44882025-01-15 21:12:36 +0000110var targetFilesMetadataDepTag targetFilesMetadataDepTagType
Jihoon Kangf2c53982024-10-09 17:32:52 +0000111
112func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
113 addDependencyIfDefined := func(dep *string) {
114 if dep != nil {
Cole Faust341d5f12025-01-07 15:32:38 -0800115 ctx.AddDependency(ctx.Module(), filesystemDepTag, proptools.String(dep))
Jihoon Kangf2c53982024-10-09 17:32:52 +0000116 }
117 }
118
Cole Faust2bdc5e52025-01-10 10:29:36 -0800119 if a.partitionProps.Super_partition_name != nil {
120 ctx.AddDependency(ctx.Module(), superPartitionDepTag, *a.partitionProps.Super_partition_name)
121 }
Jihoon Kangf2c53982024-10-09 17:32:52 +0000122 addDependencyIfDefined(a.partitionProps.Boot_partition_name)
Jihoon Kang9e087002025-01-08 19:12:23 +0000123 addDependencyIfDefined(a.partitionProps.Init_boot_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000124 addDependencyIfDefined(a.partitionProps.Vendor_boot_partition_name)
Jihoon Kangf2c53982024-10-09 17:32:52 +0000125 addDependencyIfDefined(a.partitionProps.System_partition_name)
126 addDependencyIfDefined(a.partitionProps.System_ext_partition_name)
127 addDependencyIfDefined(a.partitionProps.Product_partition_name)
128 addDependencyIfDefined(a.partitionProps.Vendor_partition_name)
Spandan Dasc5717162024-11-01 18:33:57 +0000129 addDependencyIfDefined(a.partitionProps.Odm_partition_name)
mrziwang23ba8762024-11-07 16:21:53 -0800130 addDependencyIfDefined(a.partitionProps.Userdata_partition_name)
Spandan Dasa0394002025-01-07 18:38:34 +0000131 addDependencyIfDefined(a.partitionProps.System_dlkm_partition_name)
132 addDependencyIfDefined(a.partitionProps.Vendor_dlkm_partition_name)
133 addDependencyIfDefined(a.partitionProps.Odm_dlkm_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000134 addDependencyIfDefined(a.partitionProps.Recovery_partition_name)
Cole Faust3552eb62024-11-06 18:07:26 -0800135 for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions {
136 ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition)
137 }
Spandan Das29d44882025-01-15 21:12:36 +0000138 a.addDepsForTargetFilesMetadata(ctx)
139}
140
141func (a *androidDevice) addDepsForTargetFilesMetadata(ctx android.BottomUpMutatorContext) {
142 ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), targetFilesMetadataDepTag, "liblz4") // host variant
Jihoon Kangf2c53982024-10-09 17:32:52 +0000143}
144
Cole Faust11fda332025-01-14 16:47:19 -0800145func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) {
146 if proptools.Bool(a.deviceProps.Main_device) {
147 numMainAndroidDevices := ctx.Config().Once(numMainAndroidDevicesOnceKey, func() interface{} {
148 return &atomic.Int32{}
149 }).(*atomic.Int32)
150 total := numMainAndroidDevices.Add(1)
151 if total > 1 {
152 // There should only be 1 main android_device module. That one will be
153 // made the default thing to build in soong-only builds.
154 ctx.ModuleErrorf("There cannot be more than 1 main android_device module")
155 }
Jihoon Kang3be17162025-01-09 20:51:54 +0000156 }
157
Spandan Das5ef1a9c2025-02-11 18:50:17 +0000158 a.buildTargetFilesZip(ctx)
mrziwang2fd33a72025-01-08 12:22:08 -0800159 var deps []android.Path
Cole Faust2bdc5e52025-01-10 10:29:36 -0800160 if proptools.String(a.partitionProps.Super_partition_name) != "" {
Cole Faust19eb09d2025-01-14 13:27:00 -0800161 superImage := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Cole Faust2bdc5e52025-01-10 10:29:36 -0800162 if info, ok := android.OtherModuleProvider(ctx, superImage, SuperImageProvider); ok {
163 assertUnset := func(prop *string, propName string) {
164 if prop != nil && *prop != "" {
165 ctx.PropertyErrorf(propName, "Cannot be set because it's already part of the super image")
166 }
167 }
168 for _, subPartitionType := range android.SortedKeys(info.SubImageInfo) {
169 switch subPartitionType {
170 case "system":
171 assertUnset(a.partitionProps.System_partition_name, "system_partition_name")
172 case "system_ext":
173 assertUnset(a.partitionProps.System_ext_partition_name, "system_ext_partition_name")
174 case "system_dlkm":
175 assertUnset(a.partitionProps.System_dlkm_partition_name, "system_dlkm_partition_name")
176 case "system_other":
177 // TODO
178 case "product":
179 assertUnset(a.partitionProps.Product_partition_name, "product_partition_name")
180 case "vendor":
181 assertUnset(a.partitionProps.Vendor_partition_name, "vendor_partition_name")
182 case "vendor_dlkm":
183 assertUnset(a.partitionProps.Vendor_dlkm_partition_name, "vendor_dlkm_partition_name")
184 case "odm":
185 assertUnset(a.partitionProps.Odm_partition_name, "odm_partition_name")
186 case "odm_dlkm":
187 assertUnset(a.partitionProps.Odm_dlkm_partition_name, "odm_dlkm_partition_name")
188 default:
189 ctx.ModuleErrorf("Unsupported sub-partition of super partition: %q", subPartitionType)
190 }
191 }
192
193 deps = append(deps, info.SuperImage)
194 } else {
195 ctx.ModuleErrorf("Expected super image dep to provide SuperImageProvider")
196 }
197 }
Cole Faust19eb09d2025-01-14 13:27:00 -0800198 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(m android.ModuleProxy) {
mrziwang2fd33a72025-01-08 12:22:08 -0800199 imageOutput, ok := android.OtherModuleProvider(ctx, m, android.OutputFilesProvider)
200 if !ok {
201 ctx.ModuleErrorf("Partition module %s doesn't set OutputfilesProvider", m.Name())
202 }
203 if len(imageOutput.DefaultOutputFiles) != 1 {
204 ctx.ModuleErrorf("Partition module %s should provide exact 1 output file", m.Name())
205 }
206 deps = append(deps, imageOutput.DefaultOutputFiles[0])
207 })
Jihoon Kang3be17162025-01-09 20:51:54 +0000208
Cole Fausta472a6f2025-02-10 16:10:04 -0800209 allImagesZip := android.PathForModuleOut(ctx, "all_images.zip")
210 allImagesZipBuilder := android.NewRuleBuilder(pctx, ctx)
211 cmd := allImagesZipBuilder.Command().BuiltTool("soong_zip").Flag("--sort_entries")
212 for _, dep := range deps {
213 cmd.FlagWithArg("-e ", dep.Base())
214 cmd.FlagWithInput("-f ", dep)
215 }
216 cmd.FlagWithOutput("-o ", allImagesZip)
217 allImagesZipBuilder.Build("soong_all_images_zip", "all_images.zip")
218 a.allImagesZip = allImagesZip
219
Cole Faustb55a41c2025-01-09 16:53:58 -0800220 allImagesStamp := android.PathForModuleOut(ctx, "all_images_stamp")
Cole Faust11fda332025-01-14 16:47:19 -0800221 var validations android.Paths
222 if !ctx.Config().KatiEnabled() && proptools.Bool(a.deviceProps.Main_device) {
Cole Faustb55a41c2025-01-09 16:53:58 -0800223 // In soong-only builds, build this module by default.
224 // This is the analogue to this make code:
225 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/main.mk;l=1396;drc=6595459cdd8164a6008335f6372c9f97b9094060
226 ctx.Phony("droidcore-unbundled", allImagesStamp)
Cole Faust11fda332025-01-14 16:47:19 -0800227
Cole Faust19fbb072025-01-30 18:19:29 -0800228 deps = append(deps, a.copyFilesToProductOutForSoongOnly(ctx))
Cole Faustb55a41c2025-01-09 16:53:58 -0800229 }
Cole Faust11fda332025-01-14 16:47:19 -0800230
231 ctx.Build(pctx, android.BuildParams{
232 Rule: android.Touch,
233 Output: allImagesStamp,
234 Implicits: deps,
235 Validations: validations,
236 })
237
238 // Checkbuilding it causes soong to make a phony, so you can say `m <module name>`
239 ctx.CheckbuildFile(allImagesStamp)
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000240
241 a.setVbmetaPhonyTargets(ctx)
Jihoon Kangf67b7de2025-02-12 01:01:09 +0000242
243 a.distFiles(ctx)
244}
245
246func (a *androidDevice) distFiles(ctx android.ModuleContext) {
247 if !ctx.Config().KatiEnabled() {
248 if proptools.Bool(a.deviceProps.Main_device) {
249 fsInfoMap := a.getFsInfos(ctx)
250 for _, partition := range android.SortedKeys(fsInfoMap) {
251 fsInfo := fsInfoMap[partition]
252 if fsInfo.InstalledFiles.Json != nil {
253 ctx.DistForGoal("droidcore-unbundled", fsInfo.InstalledFiles.Json)
254 }
255 if fsInfo.InstalledFiles.Txt != nil {
256 ctx.DistForGoal("droidcore-unbundled", fsInfo.InstalledFiles.Txt)
257 }
258 }
259 }
260 }
261
Cole Faust44080412024-12-20 14:17:07 -0800262}
Jihoon Kangf2c53982024-10-09 17:32:52 +0000263
Cole Fausta472a6f2025-02-10 16:10:04 -0800264func (a *androidDevice) MakeVars(ctx android.MakeVarsModuleContext) {
265 if proptools.Bool(a.deviceProps.Main_device) {
266 ctx.StrictRaw("SOONG_ONLY_ALL_IMAGES_ZIP", a.allImagesZip.String())
267 }
268}
269
Spandan Dasef775742025-01-13 22:17:40 +0000270// Helper structs for target_files.zip creation
Spandan Dasef200ac2025-01-08 01:42:45 +0000271type targetFilesZipCopy struct {
272 srcModule *string
273 destSubdir string
274}
275
Spandan Dasef775742025-01-13 22:17:40 +0000276type targetFilesystemZipCopy struct {
277 fsInfo FilesystemInfo
278 destSubdir string
279}
280
Cole Faust44080412024-12-20 14:17:07 -0800281func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext) {
282 targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir")
283 targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip")
284
285 builder := android.NewRuleBuilder(pctx, ctx)
286 builder.Command().Textf("rm -rf %s", targetFilesDir.String())
287 builder.Command().Textf("mkdir -p %s", targetFilesDir.String())
Spandan Dasef200ac2025-01-08 01:42:45 +0000288 toCopy := []targetFilesZipCopy{
289 targetFilesZipCopy{a.partitionProps.System_partition_name, "SYSTEM"},
290 targetFilesZipCopy{a.partitionProps.System_ext_partition_name, "SYSTEM_EXT"},
291 targetFilesZipCopy{a.partitionProps.Product_partition_name, "PRODUCT"},
292 targetFilesZipCopy{a.partitionProps.Vendor_partition_name, "VENDOR"},
293 targetFilesZipCopy{a.partitionProps.Odm_partition_name, "ODM"},
294 targetFilesZipCopy{a.partitionProps.System_dlkm_partition_name, "SYSTEM_DLKM"},
295 targetFilesZipCopy{a.partitionProps.Vendor_dlkm_partition_name, "VENDOR_DLKM"},
296 targetFilesZipCopy{a.partitionProps.Odm_dlkm_partition_name, "ODM_DLKM"},
297 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "BOOT/RAMDISK"},
298 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "INIT_BOOT/RAMDISK"},
299 targetFilesZipCopy{a.partitionProps.Vendor_boot_partition_name, "VENDOR_BOOT/RAMDISK"},
Spandan Das25649f52025-01-07 18:09:22 +0000300 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000301 // TODO: Handle cases where recovery files are copied to BOOT/ or RECOVERY/
302 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=6211-6219?q=core%2FMakefile&ss=android%2Fplatform%2Fsuperproject%2Fmain
303 if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() {
304 toCopy = append(toCopy, targetFilesZipCopy{a.partitionProps.Recovery_partition_name, "VENDOR_BOOT/RAMDISK"})
305 }
306
Spandan Dasef775742025-01-13 22:17:40 +0000307 filesystemsToCopy := []targetFilesystemZipCopy{}
Spandan Dasef200ac2025-01-08 01:42:45 +0000308 for _, zipCopy := range toCopy {
309 if zipCopy.srcModule == nil {
Spandan Das25649f52025-01-07 18:09:22 +0000310 continue
311 }
Spandan Dasef775742025-01-13 22:17:40 +0000312 filesystemsToCopy = append(
313 filesystemsToCopy,
314 targetFilesystemZipCopy{a.getFilesystemInfo(ctx, *zipCopy.srcModule), zipCopy.destSubdir},
315 )
316 }
317 // Get additional filesystems from super_partition dependency
318 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800319 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000320 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
321 for _, partition := range android.SortedStringKeys(info.SubImageInfo) {
322 filesystemsToCopy = append(
323 filesystemsToCopy,
324 targetFilesystemZipCopy{info.SubImageInfo[partition], strings.ToUpper(partition)},
325 )
326 }
327 } else {
328 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
329 }
330 }
331
332 for _, toCopy := range filesystemsToCopy {
333 rootDirString := toCopy.fsInfo.RootDir.String()
334 if toCopy.destSubdir == "SYSTEM" {
Spandan Dasef200ac2025-01-08 01:42:45 +0000335 rootDirString = rootDirString + "/system"
336 }
Spandan Dasef775742025-01-13 22:17:40 +0000337 builder.Command().Textf("mkdir -p %s/%s", targetFilesDir.String(), toCopy.destSubdir)
Cole Faust44080412024-12-20 14:17:07 -0800338 builder.Command().
339 BuiltTool("acp").
Spandan Dasef775742025-01-13 22:17:40 +0000340 Textf("-rd %s/. %s/%s", rootDirString, targetFilesDir, toCopy.destSubdir).
341 Implicit(toCopy.fsInfo.Output) // so that the staging dir is built
Spandan Dasef200ac2025-01-08 01:42:45 +0000342
Spandan Dasef775742025-01-13 22:17:40 +0000343 if toCopy.destSubdir == "SYSTEM" {
Spandan Das3ec6d062025-01-09 19:37:47 +0000344 // Create the ROOT partition in target_files.zip
Spandan Dasef775742025-01-13 22:17:40 +0000345 builder.Command().Textf("rsync --links --exclude=system/* %s/ -r %s/ROOT", toCopy.fsInfo.RootDir, targetFilesDir.String())
Spandan Das3ec6d062025-01-09 19:37:47 +0000346 }
Cole Faust44080412024-12-20 14:17:07 -0800347 }
Spandan Das9b17df22025-01-08 23:30:45 +0000348 // Copy cmdline, kernel etc. files of boot images
Spandan Das258c08f2025-01-08 23:30:45 +0000349 if a.partitionProps.Vendor_boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800350 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Vendor_boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000351 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000352 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
353 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/vendor_cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
354 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000355 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/VENDOR_BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000356 }
Spandan Das23511372025-01-08 23:30:45 +0000357 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000358 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/VENDOR_BOOT/vendor_bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000359 }
Spandan Das258c08f2025-01-08 23:30:45 +0000360 }
361 if a.partitionProps.Boot_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800362 bootImg := ctx.GetDirectDepProxyWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
Spandan Das258c08f2025-01-08 23:30:45 +0000363 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000364 builder.Command().Textf("echo %s > %s/BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
365 if bootImgInfo.Dtb != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000366 builder.Command().Textf("cp ").Input(bootImgInfo.Dtb).Textf(" %s/BOOT/dtb", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000367 }
368 if bootImgInfo.Kernel != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000369 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000370 // 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 +0000371 builder.Command().Textf("cp ").Input(bootImgInfo.Kernel).Textf(" %s/VENDOR_BOOT/kernel", targetFilesDir)
Spandan Das9b17df22025-01-08 23:30:45 +0000372 }
Spandan Das23511372025-01-08 23:30:45 +0000373 if bootImgInfo.Bootconfig != nil {
Spandan Dasfed3d042025-01-13 21:38:47 +0000374 builder.Command().Textf("cp ").Input(bootImgInfo.Bootconfig).Textf(" %s/BOOT/bootconfig", targetFilesDir)
Spandan Das23511372025-01-08 23:30:45 +0000375 }
Spandan Das258c08f2025-01-08 23:30:45 +0000376 }
377
Spandan Dase51ff952025-01-09 18:11:59 +0000378 if a.deviceProps.Android_info != nil {
379 builder.Command().Textf("mkdir -p %s/OTA", targetFilesDir)
Cole Faust11fda332025-01-14 16:47:19 -0800380 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 +0000381 }
382
Spandan Das0036fa32025-01-10 23:40:45 +0000383 a.copyImagesToTargetZip(ctx, builder, targetFilesDir)
Spandan Das29d44882025-01-15 21:12:36 +0000384 a.copyMetadataToTargetZip(ctx, builder, targetFilesDir)
Spandan Das0036fa32025-01-10 23:40:45 +0000385
Cole Faust44080412024-12-20 14:17:07 -0800386 builder.Command().
387 BuiltTool("soong_zip").
388 Text("-d").
389 FlagWithOutput("-o ", targetFilesZip).
390 FlagWithArg("-C ", targetFilesDir.String()).
391 FlagWithArg("-D ", targetFilesDir.String()).
392 Text("-sha256")
393 builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip")
394}
395
Spandan Das0036fa32025-01-10 23:40:45 +0000396func (a *androidDevice) copyImagesToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath) {
397 // Create an IMAGES/ subdirectory
398 builder.Command().Textf("mkdir -p %s/IMAGES", targetFilesDir.String())
399 if a.deviceProps.Bootloader != nil {
400 builder.Command().Textf("cp ").Input(android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Bootloader))).Textf(" %s/IMAGES/bootloader", targetFilesDir.String())
401 }
402 // Copy the filesystem ,boot and vbmeta img files to IMAGES/
403 ctx.VisitDirectDepsProxyWithTag(filesystemDepTag, func(child android.ModuleProxy) {
Spandan Dasa9db76d2025-01-14 01:34:43 +0000404 if strings.Contains(child.Name(), "recovery") {
405 return // skip recovery.img to match the make packaging behavior
406 }
Spandan Dasef775742025-01-13 22:17:40 +0000407 if info, ok := android.OtherModuleProvider(ctx, child, BootimgInfoProvider); ok {
408 // Check Boot img first so that the boot.img is copied and not its dep ramdisk.img
Spandan Das0036fa32025-01-10 23:40:45 +0000409 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
Spandan Dasef775742025-01-13 22:17:40 +0000410 } else if info, ok := android.OtherModuleProvider(ctx, child, FilesystemProvider); ok {
Spandan Das0036fa32025-01-10 23:40:45 +0000411 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
412 } else if info, ok := android.OtherModuleProvider(ctx, child, vbmetaPartitionProvider); ok {
413 builder.Command().Textf("cp ").Input(info.Output).Textf(" %s/IMAGES/", targetFilesDir.String())
414 } else {
415 ctx.ModuleErrorf("Module %s does not provide an .img file output for target_files.zip", child.Name())
416 }
417 })
Spandan Dasef775742025-01-13 22:17:40 +0000418
419 if a.partitionProps.Super_partition_name != nil {
Cole Faust19eb09d2025-01-14 13:27:00 -0800420 superPartition := ctx.GetDirectDepProxyWithTag(*a.partitionProps.Super_partition_name, superPartitionDepTag)
Spandan Dasef775742025-01-13 22:17:40 +0000421 if info, ok := android.OtherModuleProvider(ctx, superPartition, SuperImageProvider); ok {
Cole Faust19eb09d2025-01-14 13:27:00 -0800422 for _, partition := range android.SortedKeys(info.SubImageInfo) {
Spandan Das7a42d1c2025-02-12 01:32:21 +0000423 if info.SubImageInfo[partition].OutputHermetic != nil {
424 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].OutputHermetic).Textf(" %s/IMAGES/", targetFilesDir.String())
425 }
426 if info.SubImageInfo[partition].MapFile != nil {
427 builder.Command().Textf("cp ").Input(info.SubImageInfo[partition].MapFile).Textf(" %s/IMAGES/", targetFilesDir.String())
428 }
Spandan Dasef775742025-01-13 22:17:40 +0000429 }
430 } else {
431 ctx.ModuleErrorf("Super partition %s does set SuperImageProvider\n", superPartition.Name())
432 }
433 }
Spandan Das0036fa32025-01-10 23:40:45 +0000434}
435
Spandan Das29d44882025-01-15 21:12:36 +0000436func (a *androidDevice) copyMetadataToTargetZip(ctx android.ModuleContext, builder *android.RuleBuilder, targetFilesDir android.WritablePath) {
437 // Create a META/ subdirectory
438 builder.Command().Textf("mkdir -p %s/META", targetFilesDir.String())
439 if proptools.Bool(a.deviceProps.Ab_ota_updater) {
440 ctx.VisitDirectDepsProxyWithTag(targetFilesMetadataDepTag, func(child android.ModuleProxy) {
441 info, _ := android.OtherModuleProvider(ctx, child, android.OutputFilesProvider)
442 builder.Command().Textf("cp").Inputs(info.DefaultOutputFiles).Textf(" %s/META/", targetFilesDir.String())
443 })
Spandan Dasb5f40cf2025-02-12 19:36:03 +0000444 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "external/zucchini/version_info.h")).Textf(" %s/META/zucchini_config.txt", targetFilesDir.String())
445 builder.Command().Textf("cp").Input(android.PathForSource(ctx, "system/update_engine/update_engine.conf")).Textf(" %s/META/update_engine_config.txt", targetFilesDir.String())
446 if a.getFsInfos(ctx)["system"].ErofsCompressHints != nil {
447 builder.Command().Textf("cp").Input(a.getFsInfos(ctx)["system"].ErofsCompressHints).Textf(" %s/META/erofs_default_compress_hints.txt", targetFilesDir.String())
448 }
449 // ab_partitions.txt
450 abPartitionsSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_partitions)
451 abPartitionsSortedString := proptools.ShellEscape(strings.Join(abPartitionsSorted, "\\n"))
452 builder.Command().Textf("echo -e").Flag(abPartitionsSortedString).Textf(" > %s/META/ab_partitions.txt", targetFilesDir.String())
Spandan Das35b78742025-02-12 19:36:03 +0000453 // otakeys.txt
454 abOtaKeysSorted := android.SortedUniqueStrings(a.deviceProps.Ab_ota_keys)
455 abOtaKeysSortedString := proptools.ShellEscape(strings.Join(abOtaKeysSorted, "\\n"))
456 builder.Command().Textf("echo -e").Flag(abOtaKeysSortedString).Textf(" > %s/META/otakeys.txt", targetFilesDir.String())
Spandan Dasd71af182025-02-12 18:03:29 +0000457 }
Spandan Das29d44882025-01-15 21:12:36 +0000458}
459
Cole Faust44080412024-12-20 14:17:07 -0800460func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo {
Cole Faust19eb09d2025-01-14 13:27:00 -0800461 fsMod := ctx.GetDirectDepProxyWithTag(depName, filesystemDepTag)
Cole Faust44080412024-12-20 14:17:07 -0800462 fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider)
463 if !ok {
464 ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName)
465 }
466 return fsInfo
Jihoon Kangf2c53982024-10-09 17:32:52 +0000467}
Jihoon Kang0a6315b2025-01-30 01:14:49 +0000468
469func (a *androidDevice) setVbmetaPhonyTargets(ctx android.ModuleContext) {
470 if !proptools.Bool(a.deviceProps.Main_device) {
471 return
472 }
473
474 if !ctx.Config().KatiEnabled() {
475 for _, vbmetaPartitionName := range a.partitionProps.Vbmeta_partitions {
476 img := ctx.GetDirectDepProxyWithTag(vbmetaPartitionName, filesystemDepTag)
477 if provider, ok := android.OtherModuleProvider(ctx, img, vbmetaPartitionProvider); ok {
478 // make generates `vbmetasystemimage` phony target instead of `vbmeta_systemimage` phony target.
479 partitionName := strings.ReplaceAll(provider.Name, "_", "")
480 ctx.Phony(fmt.Sprintf("%simage", partitionName), provider.Output)
481 }
482 }
483 }
484}