blob: 6ded3aaa55c6fa4e0c260a1c3d1a4822064b9594 [file] [log] [blame]
Jihoon Kang98047cf2024-10-02 17:13:54 +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 fsgen
16
17import (
Cole Faust92ccbe22024-10-03 14:38:37 -070018 "crypto/sha256"
Jihoon Kang98047cf2024-10-02 17:13:54 +000019 "fmt"
Spandan Das312cc412024-10-29 18:20:11 +000020 "path/filepath"
Cole Fauste1676122024-12-03 17:32:25 -080021 "slices"
Jihoon Kang98047cf2024-10-02 17:13:54 +000022 "strconv"
mrziwang8f86c882024-10-03 12:34:33 -070023 "strings"
mrziwang8f86c882024-10-03 12:34:33 -070024
25 "android/soong/android"
26 "android/soong/filesystem"
Spandan Das5e336422024-11-01 22:31:20 +000027 "android/soong/kernel"
Jihoon Kang98047cf2024-10-02 17:13:54 +000028
Cole Faust92ccbe22024-10-03 14:38:37 -070029 "github.com/google/blueprint"
mrziwang8f86c882024-10-03 12:34:33 -070030 "github.com/google/blueprint/parser"
Jihoon Kang98047cf2024-10-02 17:13:54 +000031 "github.com/google/blueprint/proptools"
32)
33
Cole Faust92ccbe22024-10-03 14:38:37 -070034var pctx = android.NewPackageContext("android/soong/fsgen")
35
Jihoon Kang98047cf2024-10-02 17:13:54 +000036func init() {
37 registerBuildComponents(android.InitRegistrationContext)
38}
39
40func registerBuildComponents(ctx android.RegistrationContext) {
41 ctx.RegisterModuleType("soong_filesystem_creator", filesystemCreatorFactory)
mrziwang8f86c882024-10-03 12:34:33 -070042 ctx.PreDepsMutators(RegisterCollectFileSystemDepsMutators)
43}
44
Cole Faust92ccbe22024-10-03 14:38:37 -070045type filesystemCreatorProps struct {
46 Generated_partition_types []string `blueprint:"mutated"`
47 Unsupported_partition_types []string `blueprint:"mutated"`
Cole Faust3552eb62024-11-06 18:07:26 -080048
49 Vbmeta_module_names []string `blueprint:"mutated"`
50 Vbmeta_partition_names []string `blueprint:"mutated"`
Cole Faustf2a6e8b2024-11-14 10:54:48 -080051
Cole Faust24938e22024-11-18 14:01:58 -080052 Boot_image string `blueprint:"mutated" android:"path_device_first"`
53 Vendor_boot_image string `blueprint:"mutated" android:"path_device_first"`
Jihoon Kang95eb1da2024-11-19 20:55:20 +000054 Init_boot_image string `blueprint:"mutated" android:"path_device_first"`
Cole Faust92ccbe22024-10-03 14:38:37 -070055}
56
Jihoon Kang98047cf2024-10-02 17:13:54 +000057type filesystemCreator struct {
58 android.ModuleBase
Cole Faust92ccbe22024-10-03 14:38:37 -070059
60 properties filesystemCreatorProps
Jihoon Kang98047cf2024-10-02 17:13:54 +000061}
62
63func filesystemCreatorFactory() android.Module {
64 module := &filesystemCreator{}
65
Cole Faust69788792024-10-10 11:00:36 -070066 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Cole Faust92ccbe22024-10-03 14:38:37 -070067 module.AddProperties(&module.properties)
Jihoon Kang98047cf2024-10-02 17:13:54 +000068 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jihoon Kang675d4682024-10-24 23:45:11 +000069 generatedPrebuiltEtcModuleNames := createPrebuiltEtcModules(ctx)
Jihoon Kang04f12c92024-11-12 23:03:08 +000070 avbpubkeyGenerated := createAvbpubkeyModule(ctx)
71 createFsGenState(ctx, generatedPrebuiltEtcModuleNames, avbpubkeyGenerated)
Cole Faust953476f2024-11-14 14:11:29 -080072 module.createAvbKeyFilegroups(ctx)
Cole Faust3e730972024-12-03 13:12:08 -080073 module.createMiscFilegroups(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +000074 module.createInternalModules(ctx)
75 })
76
77 return module
78}
79
Cole Faustf2a6e8b2024-11-14 10:54:48 -080080func generatedPartitions(ctx android.LoadHookContext) []string {
Cole Faust24938e22024-11-18 14:01:58 -080081 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faustf2a6e8b2024-11-14 10:54:48 -080082 generatedPartitions := []string{"system"}
83 if ctx.DeviceConfig().SystemExtPath() == "system_ext" {
84 generatedPartitions = append(generatedPartitions, "system_ext")
85 }
86 if ctx.DeviceConfig().BuildingVendorImage() && ctx.DeviceConfig().VendorPath() == "vendor" {
87 generatedPartitions = append(generatedPartitions, "vendor")
88 }
89 if ctx.DeviceConfig().BuildingProductImage() && ctx.DeviceConfig().ProductPath() == "product" {
90 generatedPartitions = append(generatedPartitions, "product")
91 }
92 if ctx.DeviceConfig().BuildingOdmImage() && ctx.DeviceConfig().OdmPath() == "odm" {
93 generatedPartitions = append(generatedPartitions, "odm")
94 }
95 if ctx.DeviceConfig().BuildingUserdataImage() && ctx.DeviceConfig().UserdataPath() == "data" {
96 generatedPartitions = append(generatedPartitions, "userdata")
97 }
Cole Faust24938e22024-11-18 14:01:58 -080098 if partitionVars.BuildingSystemDlkmImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -080099 generatedPartitions = append(generatedPartitions, "system_dlkm")
100 }
Cole Faust24938e22024-11-18 14:01:58 -0800101 if partitionVars.BuildingVendorDlkmImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800102 generatedPartitions = append(generatedPartitions, "vendor_dlkm")
103 }
Cole Faust24938e22024-11-18 14:01:58 -0800104 if partitionVars.BuildingOdmDlkmImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800105 generatedPartitions = append(generatedPartitions, "odm_dlkm")
106 }
Cole Faust24938e22024-11-18 14:01:58 -0800107 if partitionVars.BuildingRamdiskImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800108 generatedPartitions = append(generatedPartitions, "ramdisk")
109 }
Cole Faust24938e22024-11-18 14:01:58 -0800110 if buildingVendorBootImage(partitionVars) {
111 generatedPartitions = append(generatedPartitions, "vendor_ramdisk")
112 }
Jihoon Kang3216c982024-12-02 19:42:20 +0000113 if ctx.DeviceConfig().BuildingRecoveryImage() && ctx.DeviceConfig().RecoveryPath() == "recovery" {
114 generatedPartitions = append(generatedPartitions, "recovery")
115 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800116 return generatedPartitions
117}
118
Jihoon Kang98047cf2024-10-02 17:13:54 +0000119func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) {
Cole Faust3552eb62024-11-06 18:07:26 -0800120 soongGeneratedPartitions := generatedPartitions(ctx)
121 finalSoongGeneratedPartitions := make([]string, 0, len(soongGeneratedPartitions))
122 for _, partitionType := range soongGeneratedPartitions {
Cole Faust92ccbe22024-10-03 14:38:37 -0700123 if f.createPartition(ctx, partitionType) {
124 f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType)
Cole Faust3552eb62024-11-06 18:07:26 -0800125 finalSoongGeneratedPartitions = append(finalSoongGeneratedPartitions, partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700126 } else {
127 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType)
128 }
129 }
Cole Faust3552eb62024-11-06 18:07:26 -0800130
Cole Faust24938e22024-11-18 14:01:58 -0800131 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Jihoon Kang70c1c682024-11-20 23:58:38 +0000132 dtbImg := createDtbImgFilegroup(ctx)
133
Cole Faust24938e22024-11-18 14:01:58 -0800134 if buildingBootImage(partitionVars) {
Jihoon Kang70c1c682024-11-20 23:58:38 +0000135 if createBootImage(ctx, dtbImg) {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800136 f.properties.Boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "boot")
137 } else {
138 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "boot")
139 }
140 }
Cole Faust24938e22024-11-18 14:01:58 -0800141 if buildingVendorBootImage(partitionVars) {
Jihoon Kang70c1c682024-11-20 23:58:38 +0000142 if createVendorBootImage(ctx, dtbImg) {
Cole Faust24938e22024-11-18 14:01:58 -0800143 f.properties.Vendor_boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "vendor_boot")
144 } else {
145 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "vendor_boot")
146 }
147 }
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000148 if buildingInitBootImage(partitionVars) {
149 if createInitBootImage(ctx) {
150 f.properties.Init_boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "init_boot")
151 } else {
152 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "init_boot")
153 }
154 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800155
Cole Faust3552eb62024-11-06 18:07:26 -0800156 for _, x := range createVbmetaPartitions(ctx, finalSoongGeneratedPartitions) {
157 f.properties.Vbmeta_module_names = append(f.properties.Vbmeta_module_names, x.moduleName)
158 f.properties.Vbmeta_partition_names = append(f.properties.Vbmeta_partition_names, x.partitionName)
159 }
160
161 ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions = finalSoongGeneratedPartitions
162 f.createDeviceModule(ctx, finalSoongGeneratedPartitions, f.properties.Vbmeta_module_names)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000163}
164
Jihoon Kang0d545b82024-10-11 00:21:57 +0000165func generatedModuleName(cfg android.Config, suffix string) string {
Cole Faust92ccbe22024-10-03 14:38:37 -0700166 prefix := "soong"
167 if cfg.HasDeviceProduct() {
168 prefix = cfg.DeviceProduct()
169 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000170 return fmt.Sprintf("%s_generated_%s", prefix, suffix)
171}
172
Jihoon Kang0d545b82024-10-11 00:21:57 +0000173func generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
174 return generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000175}
176
Cole Faust3552eb62024-11-06 18:07:26 -0800177func (f *filesystemCreator) createDeviceModule(
178 ctx android.LoadHookContext,
179 generatedPartitionTypes []string,
180 vbmetaPartitions []string,
181) {
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000182 baseProps := &struct {
183 Name *string
184 }{
Jihoon Kang0d545b82024-10-11 00:21:57 +0000185 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "device")),
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000186 }
187
Priyanka Advani (xWF)dafaa7f2024-10-21 22:55:13 +0000188 // Currently, only the system and system_ext partition module is created.
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000189 partitionProps := &filesystem.PartitionNameProperties{}
Cole Faust3552eb62024-11-06 18:07:26 -0800190 if android.InList("system", generatedPartitionTypes) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000191 partitionProps.System_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000192 }
Cole Faust3552eb62024-11-06 18:07:26 -0800193 if android.InList("system_ext", generatedPartitionTypes) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000194 partitionProps.System_ext_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
Spandan Das7a46f6c2024-10-14 18:41:18 +0000195 }
Cole Faust3552eb62024-11-06 18:07:26 -0800196 if android.InList("vendor", generatedPartitionTypes) {
Spandan Dase3b65312024-10-22 00:27:27 +0000197 partitionProps.Vendor_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor"))
198 }
Cole Faust3552eb62024-11-06 18:07:26 -0800199 if android.InList("product", generatedPartitionTypes) {
Jihoon Kang6dd13b62024-10-22 23:21:02 +0000200 partitionProps.Product_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "product"))
201 }
Cole Faust3552eb62024-11-06 18:07:26 -0800202 if android.InList("odm", generatedPartitionTypes) {
Spandan Dasc5717162024-11-01 18:33:57 +0000203 partitionProps.Odm_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "odm"))
204 }
mrziwang23ba8762024-11-07 16:21:53 -0800205 if android.InList("userdata", f.properties.Generated_partition_types) {
206 partitionProps.Userdata_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "userdata"))
207 }
Cole Faust3552eb62024-11-06 18:07:26 -0800208 partitionProps.Vbmeta_partitions = vbmetaPartitions
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000209
210 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -0700211}
212
Spandan Das71be42d2024-11-20 18:34:16 +0000213func partitionSpecificFsProps(ctx android.EarlyModuleContext, fsProps *filesystem.FilesystemProperties, partitionVars android.PartitionVariables, partitionType string) {
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000214 switch partitionType {
215 case "system":
216 fsProps.Build_logtags = proptools.BoolPtr(true)
217 // https://source.corp.google.com/h/googleplex-android/platform/build//639d79f5012a6542ab1f733b0697db45761ab0f3:core/packaging/flags.mk;l=21;drc=5ba8a8b77507f93aa48cc61c5ba3f31a4d0cbf37;bpv=1;bpt=0
218 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Justin Yuned3dbce2024-11-15 11:57:24 +0900219 // Identical to that of the aosp_shared_system_image
Spandan Das2b4bf4c2024-12-02 19:41:04 +0000220 if partitionVars.ProductFsverityGenerateMetadata {
221 fsProps.Fsverity.Inputs = []string{
222 "etc/boot-image.prof",
223 "etc/dirty-image-objects",
224 "etc/preloaded-classes",
225 "etc/classpaths/*.pb",
226 "framework/*",
227 "framework/*/*", // framework/{arch}
228 "framework/oat/*/*", // framework/oat/{arch}
229 }
230 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000231 }
Cole Faust1d4e76c2024-11-26 14:15:29 -0800232 // Most of the symlinks and directories listed here originate from create_root_structure.mk,
233 // but the handwritten generic system image also recreates them:
234 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/target/product/generic/Android.bp;l=33;drc=db08311f1b6ef6cb0a4fbcc6263b89849360ce04
mrziwang9afc2982024-11-05 14:29:48 -0800235 // TODO(b/377734331): only generate the symlinks if the relevant partitions exist
236 fsProps.Symlinks = []filesystem.SymlinkDefinition{
237 filesystem.SymlinkDefinition{
Cole Faust1d4e76c2024-11-26 14:15:29 -0800238 Target: proptools.StringPtr("/system/bin/init"),
239 Name: proptools.StringPtr("init"),
240 },
241 filesystem.SymlinkDefinition{
242 Target: proptools.StringPtr("/system/etc"),
243 Name: proptools.StringPtr("etc"),
244 },
245 filesystem.SymlinkDefinition{
246 Target: proptools.StringPtr("/system/bin"),
247 Name: proptools.StringPtr("bin"),
248 },
249 filesystem.SymlinkDefinition{
250 Target: proptools.StringPtr("/data/user_de/0/com.android.shell/files/bugreports"),
251 Name: proptools.StringPtr("bugreports"),
252 },
253 filesystem.SymlinkDefinition{
254 Target: proptools.StringPtr("/sys/kernel/debug"),
255 Name: proptools.StringPtr("d"),
256 },
257 filesystem.SymlinkDefinition{
258 Target: proptools.StringPtr("/storage/self/primary"),
259 Name: proptools.StringPtr("sdcard"),
260 },
261 filesystem.SymlinkDefinition{
262 Target: proptools.StringPtr("/product/etc/security/adb_keys"),
263 Name: proptools.StringPtr("adb_keys"),
264 },
265 filesystem.SymlinkDefinition{
266 Target: proptools.StringPtr("/vendor/odm/app"),
267 Name: proptools.StringPtr("odm/app"),
268 },
269 filesystem.SymlinkDefinition{
270 Target: proptools.StringPtr("/vendor/odm/bin"),
271 Name: proptools.StringPtr("odm/bin"),
272 },
273 filesystem.SymlinkDefinition{
274 Target: proptools.StringPtr("/vendor/odm/etc"),
275 Name: proptools.StringPtr("odm/etc"),
276 },
277 filesystem.SymlinkDefinition{
278 Target: proptools.StringPtr("/vendor/odm/firmware"),
279 Name: proptools.StringPtr("odm/firmware"),
280 },
281 filesystem.SymlinkDefinition{
282 Target: proptools.StringPtr("/vendor/odm/framework"),
283 Name: proptools.StringPtr("odm/framework"),
284 },
285 filesystem.SymlinkDefinition{
286 Target: proptools.StringPtr("/vendor/odm/lib"),
287 Name: proptools.StringPtr("odm/lib"),
288 },
289 filesystem.SymlinkDefinition{
290 Target: proptools.StringPtr("/vendor/odm/lib64"),
291 Name: proptools.StringPtr("odm/lib64"),
292 },
293 filesystem.SymlinkDefinition{
294 Target: proptools.StringPtr("/vendor/odm/overlay"),
295 Name: proptools.StringPtr("odm/overlay"),
296 },
297 filesystem.SymlinkDefinition{
298 Target: proptools.StringPtr("/vendor/odm/priv-app"),
299 Name: proptools.StringPtr("odm/priv-app"),
300 },
301 filesystem.SymlinkDefinition{
302 Target: proptools.StringPtr("/vendor/odm/usr"),
303 Name: proptools.StringPtr("odm/usr"),
304 },
305 filesystem.SymlinkDefinition{
mrziwang9afc2982024-11-05 14:29:48 -0800306 Target: proptools.StringPtr("/product"),
307 Name: proptools.StringPtr("system/product"),
308 },
309 filesystem.SymlinkDefinition{
310 Target: proptools.StringPtr("/system_ext"),
311 Name: proptools.StringPtr("system/system_ext"),
312 },
313 filesystem.SymlinkDefinition{
314 Target: proptools.StringPtr("/vendor"),
315 Name: proptools.StringPtr("system/vendor"),
316 },
317 filesystem.SymlinkDefinition{
318 Target: proptools.StringPtr("/system_dlkm/lib/modules"),
319 Name: proptools.StringPtr("system/lib/modules"),
320 },
Cole Faust1d4e76c2024-11-26 14:15:29 -0800321 filesystem.SymlinkDefinition{
322 Target: proptools.StringPtr("/data/cache"),
323 Name: proptools.StringPtr("cache"),
324 },
mrziwang9afc2982024-11-05 14:29:48 -0800325 }
Cole Faust1d4e76c2024-11-26 14:15:29 -0800326 fsProps.Dirs = proptools.NewSimpleConfigurable([]string{
327 // From generic_rootdirs in build/make/target/product/generic/Android.bp
328 "acct",
329 "apex",
330 "bootstrap-apex",
331 "config",
332 "data",
333 "data_mirror",
334 "debug_ramdisk",
335 "dev",
336 "linkerconfig",
337 "metadata",
338 "mnt",
339 "odm",
340 "odm_dlkm",
341 "oem",
342 "postinstall",
343 "proc",
344 "second_stage_resources",
345 "storage",
346 "sys",
347 "system",
348 "system_dlkm",
349 "tmp",
350 "vendor",
351 "vendor_dlkm",
352
353 // from android_rootdirs in build/make/target/product/generic/Android.bp
354 "system_ext",
355 "product",
356 })
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000357 case "system_ext":
Spandan Das2b4bf4c2024-12-02 19:41:04 +0000358 if partitionVars.ProductFsverityGenerateMetadata {
359 fsProps.Fsverity.Inputs = []string{
360 "framework/*",
361 "framework/*/*", // framework/{arch}
362 "framework/oat/*/*", // framework/oat/{arch}
363 }
364 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000365 }
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000366 case "product":
367 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Spandan Das71be42d2024-11-20 18:34:16 +0000368 fsProps.Android_filesystem_deps.System = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
369 if ctx.DeviceConfig().SystemExtPath() == "system_ext" {
370 fsProps.Android_filesystem_deps.System_ext = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
371 }
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000372 case "vendor":
373 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Spandan Das69464c32024-10-25 20:08:06 +0000374 fsProps.Symlinks = []filesystem.SymlinkDefinition{
375 filesystem.SymlinkDefinition{
376 Target: proptools.StringPtr("/odm"),
377 Name: proptools.StringPtr("vendor/odm"),
378 },
379 filesystem.SymlinkDefinition{
380 Target: proptools.StringPtr("/vendor_dlkm/lib/modules"),
381 Name: proptools.StringPtr("vendor/lib/modules"),
382 },
383 }
Spandan Das71be42d2024-11-20 18:34:16 +0000384 fsProps.Android_filesystem_deps.System = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
385 if ctx.DeviceConfig().SystemExtPath() == "system_ext" {
386 fsProps.Android_filesystem_deps.System_ext = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
387 }
Spandan Dasc5717162024-11-01 18:33:57 +0000388 case "odm":
389 fsProps.Symlinks = []filesystem.SymlinkDefinition{
390 filesystem.SymlinkDefinition{
391 Target: proptools.StringPtr("/odm_dlkm/lib/modules"),
392 Name: proptools.StringPtr("odm/lib/modules"),
393 },
394 }
mrziwang23ba8762024-11-07 16:21:53 -0800395 case "userdata":
396 fsProps.Base_dir = proptools.StringPtr("data")
Jihoon Kangd098d442024-11-19 00:03:22 +0000397 case "ramdisk":
398 // Following the logic in https://cs.android.com/android/platform/superproject/main/+/c3c5063df32748a8806ce5da5dd0db158eab9ad9:build/make/core/Makefile;l=1307
399 fsProps.Dirs = android.NewSimpleConfigurable([]string{
400 "debug_ramdisk",
401 "dev",
402 "metadata",
403 "mnt",
404 "proc",
405 "second_stage_resources",
406 "sys",
407 })
408 if partitionVars.BoardUsesGenericKernelImage {
409 fsProps.Dirs.AppendSimpleValue([]string{
410 "first_stage_ramdisk/debug_ramdisk",
411 "first_stage_ramdisk/dev",
412 "first_stage_ramdisk/metadata",
413 "first_stage_ramdisk/mnt",
414 "first_stage_ramdisk/proc",
415 "first_stage_ramdisk/second_stage_resources",
416 "first_stage_ramdisk/sys",
417 })
418 }
Jihoon Kang9007f382024-12-04 00:43:52 +0000419 case "recovery":
420 // Following https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=2826;drc=ad7cfb56010cb22c3aa0e70cf71c804352553526
421 fsProps.Dirs = android.NewSimpleConfigurable([]string{
422 "sdcard",
423 "tmp",
424 })
425 fsProps.Symlinks = []filesystem.SymlinkDefinition{
426 {
427 Target: proptools.StringPtr("/system/bin/init"),
428 Name: proptools.StringPtr("init"),
429 },
430 {
431 Target: proptools.StringPtr("prop.default"),
432 Name: proptools.StringPtr("default.prop"),
433 },
434 }
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000435 }
436}
Spandan Dascbe641a2024-10-14 21:07:34 +0000437
Spandan Das5b493cd2024-11-07 20:55:56 +0000438var (
439 dlkmPartitions = []string{
440 "system_dlkm",
441 "vendor_dlkm",
442 "odm_dlkm",
443 }
444)
445
Cole Faust92ccbe22024-10-03 14:38:37 -0700446// Creates a soong module to build the given partition. Returns false if we can't support building
447// it.
448func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
mrziwang4b0ca972024-10-17 14:56:19 -0700449 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
450
451 fsProps, supported := generateFsProps(ctx, partitionType)
452 if !supported {
453 return false
mrziwanga077b942024-10-16 16:00:06 -0700454 }
mrziwanga077b942024-10-16 16:00:06 -0700455
Cole Faust7db05752024-11-21 13:30:41 -0800456 if partitionType == "vendor" || partitionType == "product" || partitionType == "system" {
Spandan Das2047a4c2024-11-11 21:24:58 +0000457 fsProps.Linker_config.Gen_linker_config = proptools.BoolPtr(true)
Cole Faust7db05752024-11-21 13:30:41 -0800458 if partitionType != "system" {
459 fsProps.Linker_config.Linker_config_srcs = f.createLinkerConfigSourceFilegroups(ctx, partitionType)
460 }
Spandan Das312cc412024-10-29 18:20:11 +0000461 }
462
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000463 if android.InList(partitionType, append(dlkmPartitions, "vendor_ramdisk")) {
Spandan Das5b493cd2024-11-07 20:55:56 +0000464 f.createPrebuiltKernelModules(ctx, partitionType)
Spandan Das5e336422024-11-01 22:31:20 +0000465 }
466
mrziwang4b0ca972024-10-17 14:56:19 -0700467 var module android.Module
468 if partitionType == "system" {
469 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
470 } else {
471 // Explicitly set the partition.
472 fsProps.Partition_type = proptools.StringPtr(partitionType)
473 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
474 }
475 module.HideFromMake()
Spandan Das168098c2024-10-28 19:44:34 +0000476 if partitionType == "vendor" {
Spandan Das4cd93b52024-11-05 23:27:03 +0000477 f.createVendorBuildProp(ctx)
Spandan Das168098c2024-10-28 19:44:34 +0000478 }
mrziwang4b0ca972024-10-17 14:56:19 -0700479 return true
480}
481
Cole Faust953476f2024-11-14 14:11:29 -0800482// Creates filegroups for the files specified in BOARD_(partition_)AVB_KEY_PATH
483func (f *filesystemCreator) createAvbKeyFilegroups(ctx android.LoadHookContext) {
484 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
485 var files []string
486
487 if len(partitionVars.BoardAvbKeyPath) > 0 {
488 files = append(files, partitionVars.BoardAvbKeyPath)
489 }
490 for _, partition := range android.SortedKeys(partitionVars.PartitionQualifiedVariables) {
491 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partition]
492 if len(specificPartitionVars.BoardAvbKeyPath) > 0 {
493 files = append(files, specificPartitionVars.BoardAvbKeyPath)
494 }
495 }
496
497 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
498 for _, file := range files {
499 if _, ok := fsGenState.avbKeyFilegroups[file]; ok {
500 continue
501 }
502 if file == "external/avb/test/data/testkey_rsa4096.pem" {
503 // There already exists a checked-in filegroup for this commonly-used key, just use that
504 fsGenState.avbKeyFilegroups[file] = "avb_testkey_rsa4096"
505 continue
506 }
507 dir := filepath.Dir(file)
508 base := filepath.Base(file)
509 name := fmt.Sprintf("avb_key_%x", strings.ReplaceAll(file, "/", "_"))
510 ctx.CreateModuleInDirectory(
511 android.FileGroupFactory,
512 dir,
513 &struct {
514 Name *string
515 Srcs []string
516 Visibility []string
517 }{
518 Name: proptools.StringPtr(name),
519 Srcs: []string{base},
520 Visibility: []string{"//visibility:public"},
521 },
522 )
523 fsGenState.avbKeyFilegroups[file] = name
524 }
525}
526
Cole Faust3e730972024-12-03 13:12:08 -0800527// Creates filegroups for miscellaneous other files
528func (f *filesystemCreator) createMiscFilegroups(ctx android.LoadHookContext) {
529 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
530
531 if partitionVars.BoardErofsCompressorHints != "" {
532 dir := filepath.Dir(partitionVars.BoardErofsCompressorHints)
533 base := filepath.Base(partitionVars.BoardErofsCompressorHints)
534 ctx.CreateModuleInDirectory(
535 android.FileGroupFactory,
536 dir,
537 &struct {
538 Name *string
539 Srcs []string
540 Visibility []string
541 }{
542 Name: proptools.StringPtr("soong_generated_board_erofs_compress_hints_filegroup"),
543 Srcs: []string{base},
544 Visibility: []string{"//visibility:public"},
545 },
546 )
547 }
548}
549
Spandan Das5e336422024-11-01 22:31:20 +0000550// createPrebuiltKernelModules creates `prebuilt_kernel_modules`. These modules will be added to deps of the
Spandan Das7b25a512024-11-06 20:41:26 +0000551// autogenerated *_dlkm filsystem modules. Each _dlkm partition should have a single prebuilt_kernel_modules dependency.
552// This ensures that the depmod artifacts (modules.* installed in /lib/modules/) are generated with a complete view.
Spandan Das5b493cd2024-11-07 20:55:56 +0000553func (f *filesystemCreator) createPrebuiltKernelModules(ctx android.LoadHookContext, partitionType string) {
Spandan Das5e336422024-11-01 22:31:20 +0000554 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
Spandan Das7b25a512024-11-06 20:41:26 +0000555 name := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-kernel-modules", partitionType))
556 props := &struct {
Spandan Das912d26b2024-11-06 19:35:17 +0000557 Name *string
558 Srcs []string
Spandan Das5b493cd2024-11-07 20:55:56 +0000559 System_deps []string
Spandan Das912d26b2024-11-06 19:35:17 +0000560 System_dlkm_specific *bool
Spandan Das5b493cd2024-11-07 20:55:56 +0000561 Vendor_dlkm_specific *bool
562 Odm_dlkm_specific *bool
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000563 Vendor_ramdisk *bool
Spandan Das5b493cd2024-11-07 20:55:56 +0000564 Load_by_default *bool
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000565 Blocklist_file *string
Jihoon Kang72dd6fc2024-11-27 01:16:39 +0000566 Options_file *string
Spandan Das7b25a512024-11-06 20:41:26 +0000567 }{
568 Name: proptools.StringPtr(name),
Spandan Das5e336422024-11-01 22:31:20 +0000569 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000570 switch partitionType {
571 case "system_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000572 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules).Strings()
Spandan Das912d26b2024-11-06 19:35:17 +0000573 props.System_dlkm_specific = proptools.BoolPtr(true)
Spandan Das5b493cd2024-11-07 20:55:56 +0000574 if len(ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelLoadModules) == 0 {
575 // Create empty modules.load file for system
576 // https://source.corp.google.com/h/googleplex-android/platform/build/+/ef55daac9954896161b26db4f3ef1781b5a5694c:core/Makefile;l=695-700;drc=549fe2a5162548bd8b47867d35f907eb22332023;bpv=1;bpt=0
577 props.Load_by_default = proptools.BoolPtr(false)
578 }
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000579 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelBlocklistFile; blocklistFile != "" {
580 props.Blocklist_file = proptools.StringPtr(blocklistFile)
581 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000582 case "vendor_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000583 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorKernelModules).Strings()
Spandan Das5b493cd2024-11-07 20:55:56 +0000584 if len(ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules) > 0 {
585 props.System_deps = []string{":" + generatedModuleName(ctx.Config(), "system_dlkm-kernel-modules") + "{.modules}"}
586 }
587 props.Vendor_dlkm_specific = proptools.BoolPtr(true)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000588 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorKernelBlocklistFile; blocklistFile != "" {
589 props.Blocklist_file = proptools.StringPtr(blocklistFile)
590 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000591 case "odm_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000592 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.OdmKernelModules).Strings()
Spandan Das5b493cd2024-11-07 20:55:56 +0000593 props.Odm_dlkm_specific = proptools.BoolPtr(true)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000594 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.OdmKernelBlocklistFile; blocklistFile != "" {
595 props.Blocklist_file = proptools.StringPtr(blocklistFile)
596 }
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000597 case "vendor_ramdisk":
598 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelModules).Strings()
599 props.Vendor_ramdisk = proptools.BoolPtr(true)
600 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelBlocklistFile; blocklistFile != "" {
601 props.Blocklist_file = proptools.StringPtr(blocklistFile)
602 }
Jihoon Kang72dd6fc2024-11-27 01:16:39 +0000603 if optionsFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelOptionsFile; optionsFile != "" {
604 props.Options_file = proptools.StringPtr(optionsFile)
605 }
606
Spandan Das5b493cd2024-11-07 20:55:56 +0000607 default:
608 ctx.ModuleErrorf("DLKM is not supported for %s\n", partitionType)
Spandan Das912d26b2024-11-06 19:35:17 +0000609 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000610
611 if len(props.Srcs) == 0 {
612 return // do not generate `prebuilt_kernel_modules` if there are no sources
613 }
614
Spandan Das7b25a512024-11-06 20:41:26 +0000615 kernelModule := ctx.CreateModuleInDirectory(
616 kernel.PrebuiltKernelModulesFactory,
617 ".", // create in root directory for now
618 props,
619 )
620 kernelModule.HideFromMake()
621 // Add to deps
622 (*fsGenState.fsDeps[partitionType])[name] = defaultDepCandidateProps(ctx.Config())
Spandan Das5e336422024-11-01 22:31:20 +0000623}
624
Spandan Das4cd93b52024-11-05 23:27:03 +0000625// Create a build_prop and android_info module. This will be used to create /vendor/build.prop
626func (f *filesystemCreator) createVendorBuildProp(ctx android.LoadHookContext) {
627 // Create a android_info for vendor
628 // The board info files might be in a directory outside the root soong namespace, so create
629 // the module in "."
630 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
631 androidInfoProps := &struct {
632 Name *string
633 Board_info_files []string
634 Bootloader_board_name *string
635 }{
636 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "android-info.prop")),
637 Board_info_files: partitionVars.BoardInfoFiles,
638 }
639 if len(androidInfoProps.Board_info_files) == 0 {
640 androidInfoProps.Bootloader_board_name = proptools.StringPtr(partitionVars.BootLoaderBoardName)
641 }
642 androidInfoProp := ctx.CreateModuleInDirectory(
643 android.AndroidInfoFactory,
644 ".",
645 androidInfoProps,
646 )
647 androidInfoProp.HideFromMake()
648 // Create a build prop for vendor
649 vendorBuildProps := &struct {
650 Name *string
651 Vendor *bool
652 Stem *string
653 Product_config *string
654 Android_info *string
655 }{
656 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "vendor-build.prop")),
657 Vendor: proptools.BoolPtr(true),
658 Stem: proptools.StringPtr("build.prop"),
659 Product_config: proptools.StringPtr(":product_config"),
660 Android_info: proptools.StringPtr(":" + androidInfoProp.Name()),
661 }
662 vendorBuildProp := ctx.CreateModule(
663 android.BuildPropFactory,
664 vendorBuildProps,
665 )
666 vendorBuildProp.HideFromMake()
667}
668
Spandan Das8fe68dc2024-10-29 18:20:11 +0000669// createLinkerConfigSourceFilegroups creates filegroup modules to generate linker.config.pb for the following partitions
670// 1. vendor: Using PRODUCT_VENDOR_LINKER_CONFIG_FRAGMENTS (space separated file list)
671// 1. product: Using PRODUCT_PRODUCT_LINKER_CONFIG_FRAGMENTS (space separated file list)
672// It creates a filegroup for each file in the fragment list
Spandan Das312cc412024-10-29 18:20:11 +0000673// The filegroup modules are then added to `linker_config_srcs` of the autogenerated vendor `android_filesystem`.
Spandan Das8fe68dc2024-10-29 18:20:11 +0000674func (f *filesystemCreator) createLinkerConfigSourceFilegroups(ctx android.LoadHookContext, partitionType string) []string {
Spandan Das312cc412024-10-29 18:20:11 +0000675 ret := []string{}
676 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Spandan Das8fe68dc2024-10-29 18:20:11 +0000677 var linkerConfigSrcs []string
678 if partitionType == "vendor" {
679 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.VendorLinkerConfigSrcs)
680 } else if partitionType == "product" {
681 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.ProductLinkerConfigSrcs)
682 } else {
683 ctx.ModuleErrorf("linker.config.pb is only supported for vendor and product partitions. For system partition, use `android_system_image`")
684 }
685
686 if len(linkerConfigSrcs) > 0 {
Spandan Das312cc412024-10-29 18:20:11 +0000687 // Create a filegroup, and add `:<filegroup_name>` to ret.
688 for index, linkerConfigSrc := range linkerConfigSrcs {
689 dir := filepath.Dir(linkerConfigSrc)
690 base := filepath.Base(linkerConfigSrc)
Spandan Das8fe68dc2024-10-29 18:20:11 +0000691 fgName := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-linker-config-src%s", partitionType, strconv.Itoa(index)))
Spandan Das312cc412024-10-29 18:20:11 +0000692 srcs := []string{base}
693 fgProps := &struct {
694 Name *string
695 Srcs proptools.Configurable[[]string]
696 }{
697 Name: proptools.StringPtr(fgName),
698 Srcs: proptools.NewSimpleConfigurable(srcs),
699 }
700 ctx.CreateModuleInDirectory(
701 android.FileGroupFactory,
702 dir,
703 fgProps,
704 )
705 ret = append(ret, ":"+fgName)
706 }
707 }
708 return ret
709}
710
mrziwang4b0ca972024-10-17 14:56:19 -0700711type filesystemBaseProperty struct {
712 Name *string
713 Compile_multilib *string
Cole Faust3552eb62024-11-06 18:07:26 -0800714 Visibility []string
mrziwang4b0ca972024-10-17 14:56:19 -0700715}
716
717func generateBaseProps(namePtr *string) *filesystemBaseProperty {
718 return &filesystemBaseProperty{
719 Name: namePtr,
720 Compile_multilib: proptools.StringPtr("both"),
Cole Faust3552eb62024-11-06 18:07:26 -0800721 // The vbmeta modules are currently in the root directory and depend on the partitions
722 Visibility: []string{"//.", "//build/soong:__subpackages__"},
mrziwang4b0ca972024-10-17 14:56:19 -0700723 }
724}
725
726func generateFsProps(ctx android.EarlyModuleContext, partitionType string) (*filesystem.FilesystemProperties, bool) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700727 fsProps := &filesystem.FilesystemProperties{}
728
mrziwang4b0ca972024-10-17 14:56:19 -0700729 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faust0c4b4152024-11-20 16:42:53 -0800730 var avbInfo avbInfo
Cole Faust76a6e952024-11-07 16:56:45 -0800731 var fsType string
732 if strings.Contains(partitionType, "ramdisk") {
733 fsType = "compressed_cpio"
734 } else {
Cole Faust953476f2024-11-14 14:11:29 -0800735 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
Cole Faust76a6e952024-11-07 16:56:45 -0800736 fsType = specificPartitionVars.BoardFileSystemType
Cole Faust0c4b4152024-11-20 16:42:53 -0800737 avbInfo = getAvbInfo(ctx.Config(), partitionType)
Cole Faust953476f2024-11-14 14:11:29 -0800738 if fsType == "" {
739 fsType = "ext4" //default
740 }
Cole Faust76a6e952024-11-07 16:56:45 -0800741 }
Cole Faust76a6e952024-11-07 16:56:45 -0800742
mrziwang4b0ca972024-10-17 14:56:19 -0700743 fsProps.Type = proptools.StringPtr(fsType)
744 if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() {
745 // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs
746 return nil, false
747 }
748
Cole Faust3e730972024-12-03 13:12:08 -0800749 if *fsProps.Type == "erofs" {
750 if partitionVars.BoardErofsCompressor != "" {
751 fsProps.Erofs.Compressor = proptools.StringPtr(partitionVars.BoardErofsCompressor)
752 }
753 if partitionVars.BoardErofsCompressorHints != "" {
754 fsProps.Erofs.Compress_hints = proptools.StringPtr(":soong_generated_board_erofs_compress_hints_filegroup")
755 }
756 }
757
Cole Faust92ccbe22024-10-03 14:38:37 -0700758 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
759 // and sometimes don't build.
760 fsProps.Unchecked_module = proptools.BoolPtr(true)
761
Jihoon Kang98047cf2024-10-02 17:13:54 +0000762 // BOARD_AVB_ENABLE
Cole Faust0c4b4152024-11-20 16:42:53 -0800763 fsProps.Use_avb = avbInfo.avbEnable
Jihoon Kang98047cf2024-10-02 17:13:54 +0000764 // BOARD_AVB_KEY_PATH
Cole Faust0c4b4152024-11-20 16:42:53 -0800765 fsProps.Avb_private_key = avbInfo.avbkeyFilegroup
Jihoon Kang98047cf2024-10-02 17:13:54 +0000766 // BOARD_AVB_ALGORITHM
Cole Faust0c4b4152024-11-20 16:42:53 -0800767 fsProps.Avb_algorithm = avbInfo.avbAlgorithm
Jihoon Kang98047cf2024-10-02 17:13:54 +0000768 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust0c4b4152024-11-20 16:42:53 -0800769 fsProps.Rollback_index = avbInfo.avbRollbackIndex
Cole Fauste1676122024-12-03 17:32:25 -0800770 fsProps.Avb_hash_algorithm = avbInfo.avbHashAlgorithm
Jihoon Kang98047cf2024-10-02 17:13:54 +0000771
Cole Faust92ccbe22024-10-03 14:38:37 -0700772 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000773
Cole Faust68382192024-11-19 10:36:03 -0800774 if !strings.Contains(partitionType, "ramdisk") {
775 fsProps.Base_dir = proptools.StringPtr(partitionType)
776 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000777
Jihoon Kang0d545b82024-10-11 00:21:57 +0000778 fsProps.Is_auto_generated = proptools.BoolPtr(true)
779
Spandan Das71be42d2024-11-20 18:34:16 +0000780 partitionSpecificFsProps(ctx, fsProps, partitionVars, partitionType)
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000781
Jihoon Kang98047cf2024-10-02 17:13:54 +0000782 // system_image properties that are not set:
783 // - filesystemProperties.Avb_hash_algorithm
784 // - filesystemProperties.File_contexts
785 // - filesystemProperties.Dirs
786 // - filesystemProperties.Symlinks
787 // - filesystemProperties.Fake_timestamp
788 // - filesystemProperties.Uuid
789 // - filesystemProperties.Mount_point
790 // - filesystemProperties.Include_make_built_files
791 // - filesystemProperties.Build_logtags
Jihoon Kang98047cf2024-10-02 17:13:54 +0000792 // - systemImageProperties.Linker_config_src
mrziwang4b0ca972024-10-17 14:56:19 -0700793
794 return fsProps, true
Cole Faust92ccbe22024-10-03 14:38:37 -0700795}
796
Cole Faust0c4b4152024-11-20 16:42:53 -0800797type avbInfo struct {
798 avbEnable *bool
799 avbKeyPath *string
800 avbkeyFilegroup *string
801 avbAlgorithm *string
802 avbRollbackIndex *int64
803 avbMode *string
Cole Fauste1676122024-12-03 17:32:25 -0800804 avbHashAlgorithm *string
Cole Faust0c4b4152024-11-20 16:42:53 -0800805}
806
807func getAvbInfo(config android.Config, partitionType string) avbInfo {
808 partitionVars := config.ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
809 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
810 var result avbInfo
811 boardAvbEnable := partitionVars.BoardAvbEnable
812 if boardAvbEnable {
813 result.avbEnable = proptools.BoolPtr(true)
Cole Fauste1676122024-12-03 17:32:25 -0800814 // There are "global" and "specific" copies of a lot of these variables. Sometimes they
815 // choose the specific and then fall back to the global one if it's not set, other times
816 // the global one actually only applies to the vbmeta partition.
817 if partitionType == "vbmeta" {
818 if partitionVars.BoardAvbKeyPath != "" {
819 result.avbKeyPath = proptools.StringPtr(partitionVars.BoardAvbKeyPath)
820 }
821 if partitionVars.BoardAvbRollbackIndex != "" {
822 parsed, err := strconv.ParseInt(partitionVars.BoardAvbRollbackIndex, 10, 64)
823 if err != nil {
824 panic(fmt.Sprintf("Rollback index must be an int, got %s", partitionVars.BoardAvbRollbackIndex))
825 }
826 result.avbRollbackIndex = &parsed
827 }
828 }
Cole Faust0c4b4152024-11-20 16:42:53 -0800829 if specificPartitionVars.BoardAvbKeyPath != "" {
830 result.avbKeyPath = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
Cole Faust0c4b4152024-11-20 16:42:53 -0800831 }
832 if specificPartitionVars.BoardAvbAlgorithm != "" {
833 result.avbAlgorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
834 } else if partitionVars.BoardAvbAlgorithm != "" {
835 result.avbAlgorithm = proptools.StringPtr(partitionVars.BoardAvbAlgorithm)
836 }
837 if specificPartitionVars.BoardAvbRollbackIndex != "" {
838 parsed, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64)
839 if err != nil {
840 panic(fmt.Sprintf("Rollback index must be an int, got %s", specificPartitionVars.BoardAvbRollbackIndex))
841 }
842 result.avbRollbackIndex = &parsed
Cole Fauste1676122024-12-03 17:32:25 -0800843 }
844 if specificPartitionVars.BoardAvbRollbackIndex != "" {
845 parsed, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64)
Cole Faust0c4b4152024-11-20 16:42:53 -0800846 if err != nil {
Cole Fauste1676122024-12-03 17:32:25 -0800847 panic(fmt.Sprintf("Rollback index must be an int, got %s", specificPartitionVars.BoardAvbRollbackIndex))
Cole Faust0c4b4152024-11-20 16:42:53 -0800848 }
849 result.avbRollbackIndex = &parsed
850 }
Cole Fauste1676122024-12-03 17:32:25 -0800851
852 // Make allows you to pass arbitrary arguments to avbtool via this variable, but in practice
853 // it's only used for --hash_algorithm. The soong module has a dedicated property for the
854 // hashtree algorithm, and doesn't allow custom arguments, so just extract the hashtree
855 // algorithm out of the arbitrary arguments.
856 addHashtreeFooterArgs := strings.Split(specificPartitionVars.BoardAvbAddHashtreeFooterArgs, " ")
857 if i := slices.Index(addHashtreeFooterArgs, "--hash_algorithm"); i >= 0 {
858 result.avbHashAlgorithm = &addHashtreeFooterArgs[i+1]
859 }
860
Cole Faust0c4b4152024-11-20 16:42:53 -0800861 result.avbMode = proptools.StringPtr("make_legacy")
862 }
863 if result.avbKeyPath != nil {
864 fsGenState := config.Get(fsGenStateOnceKey).(*FsGenState)
865 filegroup := fsGenState.avbKeyFilegroups[*result.avbKeyPath]
866 result.avbkeyFilegroup = proptools.StringPtr(":" + filegroup)
867 }
868 return result
869}
870
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800871func (f *filesystemCreator) createFileListDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000872 partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700873 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
874 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
875 if !ok {
876 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
877 }
878 makeFileList := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/obj/PACKAGING/%s_intermediates/file_list.txt", ctx.Config().DeviceName(), partitionType))
Jihoon Kang9e866c82024-10-07 22:39:18 +0000879 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700880
881 builder := android.NewRuleBuilder(pctx, ctx)
882 builder.Command().BuiltTool("file_list_diff").
883 Input(makeFileList).
884 Input(filesystemInfo.FileListFile).
Cole Faust56301572024-11-07 15:22:42 -0800885 Text(partitionModuleName)
Cole Faust92ccbe22024-10-03 14:38:37 -0700886 builder.Command().Text("touch").Output(diffTestResultFile)
887 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
888 return diffTestResultFile
889}
890
891func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
892 hasher := sha256.New()
893 hasher.Write([]byte(message))
894 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
895 file := android.PathForModuleOut(ctx, filename)
896 builder := android.NewRuleBuilder(pctx, ctx)
897 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
898 builder.Command().Text("exit 1 #").Output(file)
899 builder.Build("failing command "+filename, "failing command "+filename)
900 return file
901}
902
Cole Faust3552eb62024-11-06 18:07:26 -0800903func createVbmetaDiff(ctx android.ModuleContext, vbmetaModuleName string, vbmetaPartitionName string) android.Path {
904 vbmetaModule := ctx.GetDirectDepWithTag(vbmetaModuleName, generatedVbmetaPartitionDepTag)
905 outputFilesProvider, ok := android.OtherModuleProvider(ctx, vbmetaModule, android.OutputFilesProvider)
906 if !ok {
907 ctx.ModuleErrorf("Expected module %s to provide OutputFiles", vbmetaModule)
908 }
909 if len(outputFilesProvider.DefaultOutputFiles) != 1 {
910 ctx.ModuleErrorf("Expected 1 output file from module %s", vbmetaModule)
911 }
912 soongVbMetaFile := outputFilesProvider.DefaultOutputFiles[0]
913 makeVbmetaFile := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/%s.img", ctx.Config().DeviceName(), vbmetaPartitionName))
914
915 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", vbmetaModuleName))
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800916 createDiffTest(ctx, diffTestResultFile, soongVbMetaFile, makeVbmetaFile)
917 return diffTestResultFile
918}
919
920func createDiffTest(ctx android.ModuleContext, diffTestResultFile android.WritablePath, file1 android.Path, file2 android.Path) {
Cole Faust3552eb62024-11-06 18:07:26 -0800921 builder := android.NewRuleBuilder(pctx, ctx)
922 builder.Command().Text("diff").
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800923 Input(file1).
924 Input(file2)
Cole Faust3552eb62024-11-06 18:07:26 -0800925 builder.Command().Text("touch").Output(diffTestResultFile)
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800926 builder.Build("diff test "+diffTestResultFile.String(), "diff test")
Cole Faust3552eb62024-11-06 18:07:26 -0800927}
928
Cole Faust92ccbe22024-10-03 14:38:37 -0700929type systemImageDepTagType struct {
930 blueprint.BaseDependencyTag
931}
932
933var generatedFilesystemDepTag systemImageDepTagType
Cole Faust3552eb62024-11-06 18:07:26 -0800934var generatedVbmetaPartitionDepTag systemImageDepTagType
Cole Faust92ccbe22024-10-03 14:38:37 -0700935
936func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
937 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000938 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -0700939 }
Cole Faust3552eb62024-11-06 18:07:26 -0800940 for _, vbmetaModule := range f.properties.Vbmeta_module_names {
941 ctx.AddDependency(ctx.Module(), generatedVbmetaPartitionDepTag, vbmetaModule)
942 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000943}
944
945func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700946 if ctx.ModuleDir() != "build/soong/fsgen" {
947 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
948 }
949 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000950
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000951 var content strings.Builder
952 generatedBp := android.PathForModuleOut(ctx, "soong_generated_product_config.bp")
953 for _, partition := range ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions {
954 content.WriteString(generateBpContent(ctx, partition))
955 content.WriteString("\n")
956 }
957 android.WriteFileRule(ctx, generatedBp, content.String())
958
mrziwang8f86c882024-10-03 12:34:33 -0700959 ctx.Phony("product_config_to_bp", generatedBp)
960
Cole Faust92ccbe22024-10-03 14:38:37 -0700961 var diffTestFiles []android.Path
962 for _, partitionType := range f.properties.Generated_partition_types {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800963 diffTestFile := f.createFileListDiffTest(ctx, partitionType)
Jihoon Kang72f812f2024-10-17 18:46:24 +0000964 diffTestFiles = append(diffTestFiles, diffTestFile)
965 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700966 }
967 for _, partitionType := range f.properties.Unsupported_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000968 diffTestFile := createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType))
969 diffTestFiles = append(diffTestFiles, diffTestFile)
970 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700971 }
Cole Faust3552eb62024-11-06 18:07:26 -0800972 for i, vbmetaModule := range f.properties.Vbmeta_module_names {
973 diffTestFile := createVbmetaDiff(ctx, vbmetaModule, f.properties.Vbmeta_partition_names[i])
974 diffTestFiles = append(diffTestFiles, diffTestFile)
975 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", f.properties.Vbmeta_partition_names[i]), diffTestFile)
976 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800977 if f.properties.Boot_image != "" {
978 diffTestFile := android.PathForModuleOut(ctx, "boot_diff_test.txt")
979 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Boot_image)
980 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/boot.img", ctx.Config().DeviceName()))
981 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
982 diffTestFiles = append(diffTestFiles, diffTestFile)
983 ctx.Phony("soong_generated_boot_filesystem_test", diffTestFile)
984 }
Cole Faust24938e22024-11-18 14:01:58 -0800985 if f.properties.Vendor_boot_image != "" {
986 diffTestFile := android.PathForModuleOut(ctx, "vendor_boot_diff_test.txt")
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000987 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Vendor_boot_image)
Cole Faust24938e22024-11-18 14:01:58 -0800988 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/vendor_boot.img", ctx.Config().DeviceName()))
989 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
990 diffTestFiles = append(diffTestFiles, diffTestFile)
991 ctx.Phony("soong_generated_vendor_boot_filesystem_test", diffTestFile)
992 }
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000993 if f.properties.Init_boot_image != "" {
994 diffTestFile := android.PathForModuleOut(ctx, "init_boot_diff_test.txt")
995 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Init_boot_image)
996 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/init_boot.img", ctx.Config().DeviceName()))
997 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
998 diffTestFiles = append(diffTestFiles, diffTestFile)
999 ctx.Phony("soong_generated_init_boot_filesystem_test", diffTestFile)
1000 }
Cole Faust92ccbe22024-10-03 14:38:37 -07001001 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +00001002}
mrziwang8f86c882024-10-03 12:34:33 -07001003
mrziwang8f86c882024-10-03 12:34:33 -07001004func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
mrziwang4b0ca972024-10-17 14:56:19 -07001005 fsProps, fsTypeSupported := generateFsProps(ctx, partitionType)
1006 if !fsTypeSupported {
1007 return ""
mrziwang8f86c882024-10-03 12:34:33 -07001008 }
1009
mrziwang4b0ca972024-10-17 14:56:19 -07001010 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
Jihoon Kang0d7b0112024-11-13 20:44:05 +00001011 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
1012 deps := fsGenState.fsDeps[partitionType]
1013 highPriorityDeps := fsGenState.generatedPrebuiltEtcModuleNames
1014 depProps := generateDepStruct(*deps, highPriorityDeps)
mrziwang8f86c882024-10-03 12:34:33 -07001015
mrziwang4b0ca972024-10-17 14:56:19 -07001016 result, err := proptools.RepackProperties([]interface{}{baseProps, fsProps, depProps})
mrziwang8f86c882024-10-03 12:34:33 -07001017 if err != nil {
Cole Faustae3e1d32024-11-05 13:22:50 -08001018 ctx.ModuleErrorf("%s", err.Error())
1019 return ""
mrziwang8f86c882024-10-03 12:34:33 -07001020 }
1021
Jihoon Kang4e5d8de2024-10-19 01:59:58 +00001022 moduleType := "android_filesystem"
1023 if partitionType == "system" {
1024 moduleType = "android_system_image"
1025 }
1026
mrziwang8f86c882024-10-03 12:34:33 -07001027 file := &parser.File{
1028 Defs: []parser.Definition{
1029 &parser.Module{
Jihoon Kang4e5d8de2024-10-19 01:59:58 +00001030 Type: moduleType,
mrziwang8f86c882024-10-03 12:34:33 -07001031 Map: *result,
1032 },
1033 },
1034 }
1035 bytes, err := parser.Print(file)
1036 if err != nil {
1037 ctx.ModuleErrorf(err.Error())
1038 }
1039 return strings.TrimSpace(string(bytes))
1040}