blob: 8e2b9f7ab833ec6d4c58dafc38ae06b58cd17063 [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"
19
Jihoon Kangf2c53982024-10-09 17:32:52 +000020 "android/soong/android"
21
22 "github.com/google/blueprint"
23 "github.com/google/blueprint/proptools"
24)
25
26type PartitionNameProperties struct {
Jihoon Kange7e3ec82025-01-02 21:29:14 +000027 // Name of the boot partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000028 Boot_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000029 // Name of the vendor boot partition filesystem module
30 Vendor_boot_partition_name *string
31 // Name of the init boot partition filesystem module
32 Init_boot_partition_name *string
33 // Name of the system partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000034 System_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000035 // Name of the system_ext partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000036 System_ext_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000037 // Name of the product partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000038 Product_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000039 // Name of the vendor partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000040 Vendor_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000041 // Name of the odm partition filesystem module
Spandan Dasc5717162024-11-01 18:33:57 +000042 Odm_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000043 // Name of the recovery partition filesystem module
44 Recovery_partition_name *string
Cole Faust3552eb62024-11-06 18:07:26 -080045 // The vbmeta partition and its "chained" partitions
46 Vbmeta_partitions []string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000047 // Name of the userdata partition filesystem module
mrziwang23ba8762024-11-07 16:21:53 -080048 Userdata_partition_name *string
Spandan Dasa0394002025-01-07 18:38:34 +000049 // Name of the system_dlkm partition filesystem module
50 System_dlkm_partition_name *string
51 // Name of the vendor_dlkm partition filesystem module
52 Vendor_dlkm_partition_name *string
53 // Name of the odm_dlkm partition filesystem module
54 Odm_dlkm_partition_name *string
Jihoon Kangf2c53982024-10-09 17:32:52 +000055}
56
Jihoon Kang3be17162025-01-09 20:51:54 +000057type DeviceProperties struct {
58 // Path to the prebuilt bootloader that would be copied to PRODUCT_OUT
59 Bootloader *string `android:"path"`
Spandan Dase51ff952025-01-09 18:11:59 +000060 // Path to android-info.txt file containing board specific info.
61 Android_info *string `android:"path"`
Jihoon Kang3be17162025-01-09 20:51:54 +000062}
63
Jihoon Kangf2c53982024-10-09 17:32:52 +000064type androidDevice struct {
65 android.ModuleBase
66
67 partitionProps PartitionNameProperties
Jihoon Kang3be17162025-01-09 20:51:54 +000068
69 deviceProps DeviceProperties
70
71 // copyToProductOutTimestamp for copying necessary files to PRODUCT_OUT
72 copyToProductOutTimestamp android.WritablePath
Jihoon Kangf2c53982024-10-09 17:32:52 +000073}
74
75func AndroidDeviceFactory() android.Module {
76 module := &androidDevice{}
Jihoon Kang3be17162025-01-09 20:51:54 +000077 module.AddProperties(&module.partitionProps, &module.deviceProps)
Cole Faust341d5f12025-01-07 15:32:38 -080078 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jihoon Kangf2c53982024-10-09 17:32:52 +000079 return module
80}
81
82type partitionDepTagType struct {
83 blueprint.BaseDependencyTag
84}
85
86var filesystemDepTag partitionDepTagType
87
88func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
89 addDependencyIfDefined := func(dep *string) {
90 if dep != nil {
Cole Faust341d5f12025-01-07 15:32:38 -080091 ctx.AddDependency(ctx.Module(), filesystemDepTag, proptools.String(dep))
Jihoon Kangf2c53982024-10-09 17:32:52 +000092 }
93 }
94
95 addDependencyIfDefined(a.partitionProps.Boot_partition_name)
Jihoon Kang9e087002025-01-08 19:12:23 +000096 addDependencyIfDefined(a.partitionProps.Init_boot_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +000097 addDependencyIfDefined(a.partitionProps.Vendor_boot_partition_name)
Jihoon Kangf2c53982024-10-09 17:32:52 +000098 addDependencyIfDefined(a.partitionProps.System_partition_name)
99 addDependencyIfDefined(a.partitionProps.System_ext_partition_name)
100 addDependencyIfDefined(a.partitionProps.Product_partition_name)
101 addDependencyIfDefined(a.partitionProps.Vendor_partition_name)
Spandan Dasc5717162024-11-01 18:33:57 +0000102 addDependencyIfDefined(a.partitionProps.Odm_partition_name)
mrziwang23ba8762024-11-07 16:21:53 -0800103 addDependencyIfDefined(a.partitionProps.Userdata_partition_name)
Spandan Dasa0394002025-01-07 18:38:34 +0000104 addDependencyIfDefined(a.partitionProps.System_dlkm_partition_name)
105 addDependencyIfDefined(a.partitionProps.Vendor_dlkm_partition_name)
106 addDependencyIfDefined(a.partitionProps.Odm_dlkm_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +0000107 addDependencyIfDefined(a.partitionProps.Recovery_partition_name)
Cole Faust3552eb62024-11-06 18:07:26 -0800108 for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions {
109 ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition)
110 }
Jihoon Kangf2c53982024-10-09 17:32:52 +0000111}
112
Jihoon Kang3be17162025-01-09 20:51:54 +0000113func (a *androidDevice) copyToProductOut(ctx android.ModuleContext, builder *android.RuleBuilder, src android.Path, dest string) {
114 destPath := android.PathForModuleInPartitionInstall(ctx, "").Join(ctx, dest)
115 builder.Command().Text("rsync").Flag("-a").Flag("--checksum").Input(src).Text(destPath.String())
116}
117
118func (a *androidDevice) copyFilesToProductOut(ctx android.ModuleContext) {
119 a.copyToProductOutTimestamp = android.PathForModuleOut(ctx, "timestamp")
120 builder := android.NewRuleBuilder(pctx, ctx)
121 builder.Command().Text("touch").Output(a.copyToProductOutTimestamp)
122
123 // List all individual files to be copied to PRODUCT_OUT here
124 if a.deviceProps.Bootloader != nil {
125 a.copyToProductOut(ctx, builder, android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Bootloader)), "bootloader")
126 }
127
128 builder.Build("copy_to_product_out", "Copy files to PRODUCT_OUT")
129}
130
Jihoon Kangf2c53982024-10-09 17:32:52 +0000131func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust44080412024-12-20 14:17:07 -0800132 a.buildTargetFilesZip(ctx)
mrziwang2fd33a72025-01-08 12:22:08 -0800133 var deps []android.Path
134 ctx.VisitDirectDepsWithTag(filesystemDepTag, func(m android.Module) {
135 imageOutput, ok := android.OtherModuleProvider(ctx, m, android.OutputFilesProvider)
136 if !ok {
137 ctx.ModuleErrorf("Partition module %s doesn't set OutputfilesProvider", m.Name())
138 }
139 if len(imageOutput.DefaultOutputFiles) != 1 {
140 ctx.ModuleErrorf("Partition module %s should provide exact 1 output file", m.Name())
141 }
142 deps = append(deps, imageOutput.DefaultOutputFiles[0])
143 })
Jihoon Kang3be17162025-01-09 20:51:54 +0000144
145 a.copyFilesToProductOut(ctx)
146
mrziwang2fd33a72025-01-08 12:22:08 -0800147 out := android.PathForModuleOut(ctx, "out")
148 ctx.Build(pctx, android.BuildParams{
Jihoon Kang3be17162025-01-09 20:51:54 +0000149 Rule: android.Touch,
150 Output: out,
151 Implicits: deps,
152 Validation: a.copyToProductOutTimestamp,
mrziwang2fd33a72025-01-08 12:22:08 -0800153 })
154 ctx.SetOutputFiles(android.Paths{out}, "")
155 ctx.CheckbuildFile(out)
Cole Faust44080412024-12-20 14:17:07 -0800156}
Jihoon Kangf2c53982024-10-09 17:32:52 +0000157
Spandan Dasef200ac2025-01-08 01:42:45 +0000158type targetFilesZipCopy struct {
159 srcModule *string
160 destSubdir string
161}
162
Cole Faust44080412024-12-20 14:17:07 -0800163func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext) {
164 targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir")
165 targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip")
166
167 builder := android.NewRuleBuilder(pctx, ctx)
168 builder.Command().Textf("rm -rf %s", targetFilesDir.String())
169 builder.Command().Textf("mkdir -p %s", targetFilesDir.String())
Spandan Dasef200ac2025-01-08 01:42:45 +0000170 toCopy := []targetFilesZipCopy{
171 targetFilesZipCopy{a.partitionProps.System_partition_name, "SYSTEM"},
172 targetFilesZipCopy{a.partitionProps.System_ext_partition_name, "SYSTEM_EXT"},
173 targetFilesZipCopy{a.partitionProps.Product_partition_name, "PRODUCT"},
174 targetFilesZipCopy{a.partitionProps.Vendor_partition_name, "VENDOR"},
175 targetFilesZipCopy{a.partitionProps.Odm_partition_name, "ODM"},
176 targetFilesZipCopy{a.partitionProps.System_dlkm_partition_name, "SYSTEM_DLKM"},
177 targetFilesZipCopy{a.partitionProps.Vendor_dlkm_partition_name, "VENDOR_DLKM"},
178 targetFilesZipCopy{a.partitionProps.Odm_dlkm_partition_name, "ODM_DLKM"},
179 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "BOOT/RAMDISK"},
180 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "INIT_BOOT/RAMDISK"},
181 targetFilesZipCopy{a.partitionProps.Vendor_boot_partition_name, "VENDOR_BOOT/RAMDISK"},
Spandan Das25649f52025-01-07 18:09:22 +0000182 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000183 // TODO: Handle cases where recovery files are copied to BOOT/ or RECOVERY/
184 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=6211-6219?q=core%2FMakefile&ss=android%2Fplatform%2Fsuperproject%2Fmain
185 if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() {
186 toCopy = append(toCopy, targetFilesZipCopy{a.partitionProps.Recovery_partition_name, "VENDOR_BOOT/RAMDISK"})
187 }
188
189 for _, zipCopy := range toCopy {
190 if zipCopy.srcModule == nil {
Spandan Das25649f52025-01-07 18:09:22 +0000191 continue
192 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000193 fsInfo := a.getFilesystemInfo(ctx, *zipCopy.srcModule)
194 subdir := zipCopy.destSubdir
195 rootDirString := fsInfo.RootDir.String()
196 if subdir == "SYSTEM" {
197 rootDirString = rootDirString + "/system"
198 }
Spandan Das25649f52025-01-07 18:09:22 +0000199 builder.Command().Textf("mkdir -p %s/%s", targetFilesDir.String(), subdir)
Cole Faust44080412024-12-20 14:17:07 -0800200 builder.Command().
201 BuiltTool("acp").
Spandan Dasef200ac2025-01-08 01:42:45 +0000202 Textf("-rd %s/. %s/%s", rootDirString, targetFilesDir, subdir).
Cole Faust44080412024-12-20 14:17:07 -0800203 Implicit(fsInfo.Output) // so that the staging dir is built
Spandan Dasef200ac2025-01-08 01:42:45 +0000204
Spandan Das3ec6d062025-01-09 19:37:47 +0000205 if subdir == "SYSTEM" {
206 // Create the ROOT partition in target_files.zip
207 builder.Command().Textf("rsync --links --exclude=system/* %s/ -r %s/ROOT", fsInfo.RootDir, targetFilesDir.String())
208 }
Cole Faust44080412024-12-20 14:17:07 -0800209 }
Spandan Das9b17df22025-01-08 23:30:45 +0000210 // Copy cmdline, kernel etc. files of boot images
Spandan Das258c08f2025-01-08 23:30:45 +0000211 if a.partitionProps.Vendor_boot_partition_name != nil {
212 bootImg := ctx.GetDirectDepWithTag(proptools.String(a.partitionProps.Vendor_boot_partition_name), filesystemDepTag)
213 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000214 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
215 builder.Command().Textf("echo %s > %s/VENDOR_BOOT/vendor_cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
216 if bootImgInfo.Dtb != nil {
217 builder.Command().Textf("cp %s %s/VENDOR_BOOT/dtb", bootImgInfo.Dtb, targetFilesDir)
218 }
Spandan Das23511372025-01-08 23:30:45 +0000219 if bootImgInfo.Bootconfig != nil {
220 builder.Command().Textf("cp %s %s/VENDOR_BOOT/vendor_bootconfig", bootImgInfo.Bootconfig, targetFilesDir)
221 }
Spandan Das258c08f2025-01-08 23:30:45 +0000222 }
223 if a.partitionProps.Boot_partition_name != nil {
224 bootImg := ctx.GetDirectDepWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
225 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
Spandan Das9b17df22025-01-08 23:30:45 +0000226 builder.Command().Textf("echo %s > %s/BOOT/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir)
227 if bootImgInfo.Dtb != nil {
228 builder.Command().Textf("cp %s %s/BOOT/dtb", bootImgInfo.Dtb, targetFilesDir)
229 }
230 if bootImgInfo.Kernel != nil {
231 builder.Command().Textf("cp %s %s/BOOT/kernel", bootImgInfo.Kernel, targetFilesDir)
232 // Even though kernel is not used to build vendor_boot, copy the kernel to VENDOR_BOOT to match the behavior of make packaging.
233 builder.Command().Textf("cp %s %s/VENDOR_BOOT/kernel", bootImgInfo.Kernel, targetFilesDir)
234 }
Spandan Das23511372025-01-08 23:30:45 +0000235 if bootImgInfo.Bootconfig != nil {
236 builder.Command().Textf("cp %s %s/BOOT/bootconfig", bootImgInfo.Bootconfig, targetFilesDir)
237 }
Spandan Das258c08f2025-01-08 23:30:45 +0000238 }
239
Spandan Dase51ff952025-01-09 18:11:59 +0000240 if a.deviceProps.Android_info != nil {
241 builder.Command().Textf("mkdir -p %s/OTA", targetFilesDir)
242 builder.Command().Textf("cp %s %s/OTA/android-info.txt", android.PathForModuleSrc(ctx, proptools.String(a.deviceProps.Android_info)), targetFilesDir)
243 }
244
Cole Faust44080412024-12-20 14:17:07 -0800245 builder.Command().
246 BuiltTool("soong_zip").
247 Text("-d").
248 FlagWithOutput("-o ", targetFilesZip).
249 FlagWithArg("-C ", targetFilesDir.String()).
250 FlagWithArg("-D ", targetFilesDir.String()).
251 Text("-sha256")
252 builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip")
253}
254
255func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo {
256 fsMod := ctx.GetDirectDepWithTag(depName, filesystemDepTag)
257 fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider)
258 if !ok {
259 ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName)
260 }
261 return fsInfo
Jihoon Kangf2c53982024-10-09 17:32:52 +0000262}