blob: 745aeaaa7c6fe7a55011176e8745adc0ca7d6e86 [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"
Jihoon Kang98047cf2024-10-02 17:13:54 +000021 "strconv"
mrziwang8f86c882024-10-03 12:34:33 -070022 "strings"
mrziwang8f86c882024-10-03 12:34:33 -070023
24 "android/soong/android"
25 "android/soong/filesystem"
Spandan Das5e336422024-11-01 22:31:20 +000026 "android/soong/kernel"
Jihoon Kang98047cf2024-10-02 17:13:54 +000027
Cole Faust92ccbe22024-10-03 14:38:37 -070028 "github.com/google/blueprint"
mrziwang8f86c882024-10-03 12:34:33 -070029 "github.com/google/blueprint/parser"
Jihoon Kang98047cf2024-10-02 17:13:54 +000030 "github.com/google/blueprint/proptools"
31)
32
Cole Faust92ccbe22024-10-03 14:38:37 -070033var pctx = android.NewPackageContext("android/soong/fsgen")
34
Jihoon Kang98047cf2024-10-02 17:13:54 +000035func init() {
36 registerBuildComponents(android.InitRegistrationContext)
37}
38
39func registerBuildComponents(ctx android.RegistrationContext) {
40 ctx.RegisterModuleType("soong_filesystem_creator", filesystemCreatorFactory)
mrziwang8f86c882024-10-03 12:34:33 -070041 ctx.PreDepsMutators(RegisterCollectFileSystemDepsMutators)
42}
43
Cole Faust92ccbe22024-10-03 14:38:37 -070044type filesystemCreatorProps struct {
45 Generated_partition_types []string `blueprint:"mutated"`
46 Unsupported_partition_types []string `blueprint:"mutated"`
Cole Faust3552eb62024-11-06 18:07:26 -080047
48 Vbmeta_module_names []string `blueprint:"mutated"`
49 Vbmeta_partition_names []string `blueprint:"mutated"`
Cole Faustf2a6e8b2024-11-14 10:54:48 -080050
Cole Faust24938e22024-11-18 14:01:58 -080051 Boot_image string `blueprint:"mutated" android:"path_device_first"`
52 Vendor_boot_image string `blueprint:"mutated" android:"path_device_first"`
Jihoon Kang95eb1da2024-11-19 20:55:20 +000053 Init_boot_image string `blueprint:"mutated" android:"path_device_first"`
Cole Faust92ccbe22024-10-03 14:38:37 -070054}
55
Jihoon Kang98047cf2024-10-02 17:13:54 +000056type filesystemCreator struct {
57 android.ModuleBase
Cole Faust92ccbe22024-10-03 14:38:37 -070058
59 properties filesystemCreatorProps
Jihoon Kang98047cf2024-10-02 17:13:54 +000060}
61
62func filesystemCreatorFactory() android.Module {
63 module := &filesystemCreator{}
64
Cole Faust69788792024-10-10 11:00:36 -070065 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
Cole Faust92ccbe22024-10-03 14:38:37 -070066 module.AddProperties(&module.properties)
Jihoon Kang98047cf2024-10-02 17:13:54 +000067 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Jihoon Kang675d4682024-10-24 23:45:11 +000068 generatedPrebuiltEtcModuleNames := createPrebuiltEtcModules(ctx)
Jihoon Kang04f12c92024-11-12 23:03:08 +000069 avbpubkeyGenerated := createAvbpubkeyModule(ctx)
70 createFsGenState(ctx, generatedPrebuiltEtcModuleNames, avbpubkeyGenerated)
Cole Faust953476f2024-11-14 14:11:29 -080071 module.createAvbKeyFilegroups(ctx)
Cole Faust3e730972024-12-03 13:12:08 -080072 module.createMiscFilegroups(ctx)
Jihoon Kang98047cf2024-10-02 17:13:54 +000073 module.createInternalModules(ctx)
74 })
75
76 return module
77}
78
Cole Faustf2a6e8b2024-11-14 10:54:48 -080079func generatedPartitions(ctx android.LoadHookContext) []string {
Cole Faust24938e22024-11-18 14:01:58 -080080 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faustf2a6e8b2024-11-14 10:54:48 -080081 generatedPartitions := []string{"system"}
82 if ctx.DeviceConfig().SystemExtPath() == "system_ext" {
83 generatedPartitions = append(generatedPartitions, "system_ext")
84 }
85 if ctx.DeviceConfig().BuildingVendorImage() && ctx.DeviceConfig().VendorPath() == "vendor" {
86 generatedPartitions = append(generatedPartitions, "vendor")
87 }
88 if ctx.DeviceConfig().BuildingProductImage() && ctx.DeviceConfig().ProductPath() == "product" {
89 generatedPartitions = append(generatedPartitions, "product")
90 }
91 if ctx.DeviceConfig().BuildingOdmImage() && ctx.DeviceConfig().OdmPath() == "odm" {
92 generatedPartitions = append(generatedPartitions, "odm")
93 }
94 if ctx.DeviceConfig().BuildingUserdataImage() && ctx.DeviceConfig().UserdataPath() == "data" {
95 generatedPartitions = append(generatedPartitions, "userdata")
96 }
Cole Faust24938e22024-11-18 14:01:58 -080097 if partitionVars.BuildingSystemDlkmImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -080098 generatedPartitions = append(generatedPartitions, "system_dlkm")
99 }
Cole Faust24938e22024-11-18 14:01:58 -0800100 if partitionVars.BuildingVendorDlkmImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800101 generatedPartitions = append(generatedPartitions, "vendor_dlkm")
102 }
Cole Faust24938e22024-11-18 14:01:58 -0800103 if partitionVars.BuildingOdmDlkmImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800104 generatedPartitions = append(generatedPartitions, "odm_dlkm")
105 }
Cole Faust24938e22024-11-18 14:01:58 -0800106 if partitionVars.BuildingRamdiskImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800107 generatedPartitions = append(generatedPartitions, "ramdisk")
108 }
Cole Faust24938e22024-11-18 14:01:58 -0800109 if buildingVendorBootImage(partitionVars) {
110 generatedPartitions = append(generatedPartitions, "vendor_ramdisk")
111 }
Jihoon Kang3216c982024-12-02 19:42:20 +0000112 if ctx.DeviceConfig().BuildingRecoveryImage() && ctx.DeviceConfig().RecoveryPath() == "recovery" {
113 generatedPartitions = append(generatedPartitions, "recovery")
114 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800115 return generatedPartitions
116}
117
Jihoon Kang98047cf2024-10-02 17:13:54 +0000118func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) {
Cole Faust3552eb62024-11-06 18:07:26 -0800119 soongGeneratedPartitions := generatedPartitions(ctx)
120 finalSoongGeneratedPartitions := make([]string, 0, len(soongGeneratedPartitions))
121 for _, partitionType := range soongGeneratedPartitions {
Cole Faust92ccbe22024-10-03 14:38:37 -0700122 if f.createPartition(ctx, partitionType) {
123 f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType)
Cole Faust3552eb62024-11-06 18:07:26 -0800124 finalSoongGeneratedPartitions = append(finalSoongGeneratedPartitions, partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700125 } else {
126 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType)
127 }
128 }
Cole Faust3552eb62024-11-06 18:07:26 -0800129
Cole Faust24938e22024-11-18 14:01:58 -0800130 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Jihoon Kang70c1c682024-11-20 23:58:38 +0000131 dtbImg := createDtbImgFilegroup(ctx)
132
Cole Faust24938e22024-11-18 14:01:58 -0800133 if buildingBootImage(partitionVars) {
Jihoon Kang70c1c682024-11-20 23:58:38 +0000134 if createBootImage(ctx, dtbImg) {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800135 f.properties.Boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "boot")
136 } else {
137 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "boot")
138 }
139 }
Cole Faust24938e22024-11-18 14:01:58 -0800140 if buildingVendorBootImage(partitionVars) {
Jihoon Kang70c1c682024-11-20 23:58:38 +0000141 if createVendorBootImage(ctx, dtbImg) {
Cole Faust24938e22024-11-18 14:01:58 -0800142 f.properties.Vendor_boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "vendor_boot")
143 } else {
144 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "vendor_boot")
145 }
146 }
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000147 if buildingInitBootImage(partitionVars) {
148 if createInitBootImage(ctx) {
149 f.properties.Init_boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "init_boot")
150 } else {
151 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "init_boot")
152 }
153 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800154
Cole Faust3552eb62024-11-06 18:07:26 -0800155 for _, x := range createVbmetaPartitions(ctx, finalSoongGeneratedPartitions) {
156 f.properties.Vbmeta_module_names = append(f.properties.Vbmeta_module_names, x.moduleName)
157 f.properties.Vbmeta_partition_names = append(f.properties.Vbmeta_partition_names, x.partitionName)
158 }
159
160 ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions = finalSoongGeneratedPartitions
161 f.createDeviceModule(ctx, finalSoongGeneratedPartitions, f.properties.Vbmeta_module_names)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000162}
163
Jihoon Kang0d545b82024-10-11 00:21:57 +0000164func generatedModuleName(cfg android.Config, suffix string) string {
Cole Faust92ccbe22024-10-03 14:38:37 -0700165 prefix := "soong"
166 if cfg.HasDeviceProduct() {
167 prefix = cfg.DeviceProduct()
168 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000169 return fmt.Sprintf("%s_generated_%s", prefix, suffix)
170}
171
Jihoon Kang0d545b82024-10-11 00:21:57 +0000172func generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
173 return generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000174}
175
Cole Faust3552eb62024-11-06 18:07:26 -0800176func (f *filesystemCreator) createDeviceModule(
177 ctx android.LoadHookContext,
178 generatedPartitionTypes []string,
179 vbmetaPartitions []string,
180) {
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000181 baseProps := &struct {
182 Name *string
183 }{
Jihoon Kang0d545b82024-10-11 00:21:57 +0000184 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "device")),
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000185 }
186
Priyanka Advani (xWF)dafaa7f2024-10-21 22:55:13 +0000187 // Currently, only the system and system_ext partition module is created.
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000188 partitionProps := &filesystem.PartitionNameProperties{}
Cole Faust3552eb62024-11-06 18:07:26 -0800189 if android.InList("system", generatedPartitionTypes) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000190 partitionProps.System_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000191 }
Cole Faust3552eb62024-11-06 18:07:26 -0800192 if android.InList("system_ext", generatedPartitionTypes) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000193 partitionProps.System_ext_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
Spandan Das7a46f6c2024-10-14 18:41:18 +0000194 }
Cole Faust3552eb62024-11-06 18:07:26 -0800195 if android.InList("vendor", generatedPartitionTypes) {
Spandan Dase3b65312024-10-22 00:27:27 +0000196 partitionProps.Vendor_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor"))
197 }
Cole Faust3552eb62024-11-06 18:07:26 -0800198 if android.InList("product", generatedPartitionTypes) {
Jihoon Kang6dd13b62024-10-22 23:21:02 +0000199 partitionProps.Product_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "product"))
200 }
Cole Faust3552eb62024-11-06 18:07:26 -0800201 if android.InList("odm", generatedPartitionTypes) {
Spandan Dasc5717162024-11-01 18:33:57 +0000202 partitionProps.Odm_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "odm"))
203 }
mrziwang23ba8762024-11-07 16:21:53 -0800204 if android.InList("userdata", f.properties.Generated_partition_types) {
205 partitionProps.Userdata_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "userdata"))
206 }
Cole Faust3552eb62024-11-06 18:07:26 -0800207 partitionProps.Vbmeta_partitions = vbmetaPartitions
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000208
209 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -0700210}
211
Spandan Das71be42d2024-11-20 18:34:16 +0000212func partitionSpecificFsProps(ctx android.EarlyModuleContext, fsProps *filesystem.FilesystemProperties, partitionVars android.PartitionVariables, partitionType string) {
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000213 switch partitionType {
214 case "system":
215 fsProps.Build_logtags = proptools.BoolPtr(true)
216 // https://source.corp.google.com/h/googleplex-android/platform/build//639d79f5012a6542ab1f733b0697db45761ab0f3:core/packaging/flags.mk;l=21;drc=5ba8a8b77507f93aa48cc61c5ba3f31a4d0cbf37;bpv=1;bpt=0
217 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Justin Yuned3dbce2024-11-15 11:57:24 +0900218 // Identical to that of the aosp_shared_system_image
Spandan Das2b4bf4c2024-12-02 19:41:04 +0000219 if partitionVars.ProductFsverityGenerateMetadata {
220 fsProps.Fsverity.Inputs = []string{
221 "etc/boot-image.prof",
222 "etc/dirty-image-objects",
223 "etc/preloaded-classes",
224 "etc/classpaths/*.pb",
225 "framework/*",
226 "framework/*/*", // framework/{arch}
227 "framework/oat/*/*", // framework/oat/{arch}
228 }
229 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000230 }
Cole Faust1d4e76c2024-11-26 14:15:29 -0800231 // Most of the symlinks and directories listed here originate from create_root_structure.mk,
232 // but the handwritten generic system image also recreates them:
233 // 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 -0800234 // TODO(b/377734331): only generate the symlinks if the relevant partitions exist
235 fsProps.Symlinks = []filesystem.SymlinkDefinition{
236 filesystem.SymlinkDefinition{
Cole Faust1d4e76c2024-11-26 14:15:29 -0800237 Target: proptools.StringPtr("/system/bin/init"),
238 Name: proptools.StringPtr("init"),
239 },
240 filesystem.SymlinkDefinition{
241 Target: proptools.StringPtr("/system/etc"),
242 Name: proptools.StringPtr("etc"),
243 },
244 filesystem.SymlinkDefinition{
245 Target: proptools.StringPtr("/system/bin"),
246 Name: proptools.StringPtr("bin"),
247 },
248 filesystem.SymlinkDefinition{
249 Target: proptools.StringPtr("/data/user_de/0/com.android.shell/files/bugreports"),
250 Name: proptools.StringPtr("bugreports"),
251 },
252 filesystem.SymlinkDefinition{
253 Target: proptools.StringPtr("/sys/kernel/debug"),
254 Name: proptools.StringPtr("d"),
255 },
256 filesystem.SymlinkDefinition{
257 Target: proptools.StringPtr("/storage/self/primary"),
258 Name: proptools.StringPtr("sdcard"),
259 },
260 filesystem.SymlinkDefinition{
261 Target: proptools.StringPtr("/product/etc/security/adb_keys"),
262 Name: proptools.StringPtr("adb_keys"),
263 },
264 filesystem.SymlinkDefinition{
265 Target: proptools.StringPtr("/vendor/odm/app"),
266 Name: proptools.StringPtr("odm/app"),
267 },
268 filesystem.SymlinkDefinition{
269 Target: proptools.StringPtr("/vendor/odm/bin"),
270 Name: proptools.StringPtr("odm/bin"),
271 },
272 filesystem.SymlinkDefinition{
273 Target: proptools.StringPtr("/vendor/odm/etc"),
274 Name: proptools.StringPtr("odm/etc"),
275 },
276 filesystem.SymlinkDefinition{
277 Target: proptools.StringPtr("/vendor/odm/firmware"),
278 Name: proptools.StringPtr("odm/firmware"),
279 },
280 filesystem.SymlinkDefinition{
281 Target: proptools.StringPtr("/vendor/odm/framework"),
282 Name: proptools.StringPtr("odm/framework"),
283 },
284 filesystem.SymlinkDefinition{
285 Target: proptools.StringPtr("/vendor/odm/lib"),
286 Name: proptools.StringPtr("odm/lib"),
287 },
288 filesystem.SymlinkDefinition{
289 Target: proptools.StringPtr("/vendor/odm/lib64"),
290 Name: proptools.StringPtr("odm/lib64"),
291 },
292 filesystem.SymlinkDefinition{
293 Target: proptools.StringPtr("/vendor/odm/overlay"),
294 Name: proptools.StringPtr("odm/overlay"),
295 },
296 filesystem.SymlinkDefinition{
297 Target: proptools.StringPtr("/vendor/odm/priv-app"),
298 Name: proptools.StringPtr("odm/priv-app"),
299 },
300 filesystem.SymlinkDefinition{
301 Target: proptools.StringPtr("/vendor/odm/usr"),
302 Name: proptools.StringPtr("odm/usr"),
303 },
304 filesystem.SymlinkDefinition{
mrziwang9afc2982024-11-05 14:29:48 -0800305 Target: proptools.StringPtr("/product"),
306 Name: proptools.StringPtr("system/product"),
307 },
308 filesystem.SymlinkDefinition{
309 Target: proptools.StringPtr("/system_ext"),
310 Name: proptools.StringPtr("system/system_ext"),
311 },
312 filesystem.SymlinkDefinition{
313 Target: proptools.StringPtr("/vendor"),
314 Name: proptools.StringPtr("system/vendor"),
315 },
316 filesystem.SymlinkDefinition{
317 Target: proptools.StringPtr("/system_dlkm/lib/modules"),
318 Name: proptools.StringPtr("system/lib/modules"),
319 },
Cole Faust1d4e76c2024-11-26 14:15:29 -0800320 filesystem.SymlinkDefinition{
321 Target: proptools.StringPtr("/data/cache"),
322 Name: proptools.StringPtr("cache"),
323 },
mrziwang9afc2982024-11-05 14:29:48 -0800324 }
Cole Faust1d4e76c2024-11-26 14:15:29 -0800325 fsProps.Dirs = proptools.NewSimpleConfigurable([]string{
326 // From generic_rootdirs in build/make/target/product/generic/Android.bp
327 "acct",
328 "apex",
329 "bootstrap-apex",
330 "config",
331 "data",
332 "data_mirror",
333 "debug_ramdisk",
334 "dev",
335 "linkerconfig",
336 "metadata",
337 "mnt",
338 "odm",
339 "odm_dlkm",
340 "oem",
341 "postinstall",
342 "proc",
343 "second_stage_resources",
344 "storage",
345 "sys",
346 "system",
347 "system_dlkm",
348 "tmp",
349 "vendor",
350 "vendor_dlkm",
351
352 // from android_rootdirs in build/make/target/product/generic/Android.bp
353 "system_ext",
354 "product",
355 })
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000356 case "system_ext":
Spandan Das2b4bf4c2024-12-02 19:41:04 +0000357 if partitionVars.ProductFsverityGenerateMetadata {
358 fsProps.Fsverity.Inputs = []string{
359 "framework/*",
360 "framework/*/*", // framework/{arch}
361 "framework/oat/*/*", // framework/oat/{arch}
362 }
363 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000364 }
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000365 case "product":
366 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Spandan Das71be42d2024-11-20 18:34:16 +0000367 fsProps.Android_filesystem_deps.System = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
368 if ctx.DeviceConfig().SystemExtPath() == "system_ext" {
369 fsProps.Android_filesystem_deps.System_ext = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
370 }
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000371 case "vendor":
372 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Spandan Das69464c32024-10-25 20:08:06 +0000373 fsProps.Symlinks = []filesystem.SymlinkDefinition{
374 filesystem.SymlinkDefinition{
375 Target: proptools.StringPtr("/odm"),
376 Name: proptools.StringPtr("vendor/odm"),
377 },
378 filesystem.SymlinkDefinition{
379 Target: proptools.StringPtr("/vendor_dlkm/lib/modules"),
380 Name: proptools.StringPtr("vendor/lib/modules"),
381 },
382 }
Spandan Das71be42d2024-11-20 18:34:16 +0000383 fsProps.Android_filesystem_deps.System = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
384 if ctx.DeviceConfig().SystemExtPath() == "system_ext" {
385 fsProps.Android_filesystem_deps.System_ext = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
386 }
Spandan Dasc5717162024-11-01 18:33:57 +0000387 case "odm":
388 fsProps.Symlinks = []filesystem.SymlinkDefinition{
389 filesystem.SymlinkDefinition{
390 Target: proptools.StringPtr("/odm_dlkm/lib/modules"),
391 Name: proptools.StringPtr("odm/lib/modules"),
392 },
393 }
mrziwang23ba8762024-11-07 16:21:53 -0800394 case "userdata":
395 fsProps.Base_dir = proptools.StringPtr("data")
Jihoon Kangd098d442024-11-19 00:03:22 +0000396 case "ramdisk":
397 // Following the logic in https://cs.android.com/android/platform/superproject/main/+/c3c5063df32748a8806ce5da5dd0db158eab9ad9:build/make/core/Makefile;l=1307
398 fsProps.Dirs = android.NewSimpleConfigurable([]string{
399 "debug_ramdisk",
400 "dev",
401 "metadata",
402 "mnt",
403 "proc",
404 "second_stage_resources",
405 "sys",
406 })
407 if partitionVars.BoardUsesGenericKernelImage {
408 fsProps.Dirs.AppendSimpleValue([]string{
409 "first_stage_ramdisk/debug_ramdisk",
410 "first_stage_ramdisk/dev",
411 "first_stage_ramdisk/metadata",
412 "first_stage_ramdisk/mnt",
413 "first_stage_ramdisk/proc",
414 "first_stage_ramdisk/second_stage_resources",
415 "first_stage_ramdisk/sys",
416 })
417 }
Jihoon Kang9007f382024-12-04 00:43:52 +0000418 case "recovery":
419 // Following https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=2826;drc=ad7cfb56010cb22c3aa0e70cf71c804352553526
420 fsProps.Dirs = android.NewSimpleConfigurable([]string{
421 "sdcard",
422 "tmp",
423 })
424 fsProps.Symlinks = []filesystem.SymlinkDefinition{
425 {
426 Target: proptools.StringPtr("/system/bin/init"),
427 Name: proptools.StringPtr("init"),
428 },
429 {
430 Target: proptools.StringPtr("prop.default"),
431 Name: proptools.StringPtr("default.prop"),
432 },
433 }
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000434 }
435}
Spandan Dascbe641a2024-10-14 21:07:34 +0000436
Spandan Das5b493cd2024-11-07 20:55:56 +0000437var (
438 dlkmPartitions = []string{
439 "system_dlkm",
440 "vendor_dlkm",
441 "odm_dlkm",
442 }
443)
444
Cole Faust92ccbe22024-10-03 14:38:37 -0700445// Creates a soong module to build the given partition. Returns false if we can't support building
446// it.
447func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
mrziwang4b0ca972024-10-17 14:56:19 -0700448 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
449
450 fsProps, supported := generateFsProps(ctx, partitionType)
451 if !supported {
452 return false
mrziwanga077b942024-10-16 16:00:06 -0700453 }
mrziwanga077b942024-10-16 16:00:06 -0700454
Cole Faust7db05752024-11-21 13:30:41 -0800455 if partitionType == "vendor" || partitionType == "product" || partitionType == "system" {
Spandan Das2047a4c2024-11-11 21:24:58 +0000456 fsProps.Linker_config.Gen_linker_config = proptools.BoolPtr(true)
Cole Faust7db05752024-11-21 13:30:41 -0800457 if partitionType != "system" {
458 fsProps.Linker_config.Linker_config_srcs = f.createLinkerConfigSourceFilegroups(ctx, partitionType)
459 }
Spandan Das312cc412024-10-29 18:20:11 +0000460 }
461
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000462 if android.InList(partitionType, append(dlkmPartitions, "vendor_ramdisk")) {
Spandan Das5b493cd2024-11-07 20:55:56 +0000463 f.createPrebuiltKernelModules(ctx, partitionType)
Spandan Das5e336422024-11-01 22:31:20 +0000464 }
465
mrziwang4b0ca972024-10-17 14:56:19 -0700466 var module android.Module
467 if partitionType == "system" {
468 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
469 } else {
470 // Explicitly set the partition.
471 fsProps.Partition_type = proptools.StringPtr(partitionType)
472 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
473 }
474 module.HideFromMake()
Spandan Das168098c2024-10-28 19:44:34 +0000475 if partitionType == "vendor" {
Spandan Das4cd93b52024-11-05 23:27:03 +0000476 f.createVendorBuildProp(ctx)
Spandan Das168098c2024-10-28 19:44:34 +0000477 }
mrziwang4b0ca972024-10-17 14:56:19 -0700478 return true
479}
480
Cole Faust953476f2024-11-14 14:11:29 -0800481// Creates filegroups for the files specified in BOARD_(partition_)AVB_KEY_PATH
482func (f *filesystemCreator) createAvbKeyFilegroups(ctx android.LoadHookContext) {
483 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
484 var files []string
485
486 if len(partitionVars.BoardAvbKeyPath) > 0 {
487 files = append(files, partitionVars.BoardAvbKeyPath)
488 }
489 for _, partition := range android.SortedKeys(partitionVars.PartitionQualifiedVariables) {
490 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partition]
491 if len(specificPartitionVars.BoardAvbKeyPath) > 0 {
492 files = append(files, specificPartitionVars.BoardAvbKeyPath)
493 }
494 }
495
496 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
497 for _, file := range files {
498 if _, ok := fsGenState.avbKeyFilegroups[file]; ok {
499 continue
500 }
501 if file == "external/avb/test/data/testkey_rsa4096.pem" {
502 // There already exists a checked-in filegroup for this commonly-used key, just use that
503 fsGenState.avbKeyFilegroups[file] = "avb_testkey_rsa4096"
504 continue
505 }
506 dir := filepath.Dir(file)
507 base := filepath.Base(file)
508 name := fmt.Sprintf("avb_key_%x", strings.ReplaceAll(file, "/", "_"))
509 ctx.CreateModuleInDirectory(
510 android.FileGroupFactory,
511 dir,
512 &struct {
513 Name *string
514 Srcs []string
515 Visibility []string
516 }{
517 Name: proptools.StringPtr(name),
518 Srcs: []string{base},
519 Visibility: []string{"//visibility:public"},
520 },
521 )
522 fsGenState.avbKeyFilegroups[file] = name
523 }
524}
525
Cole Faust3e730972024-12-03 13:12:08 -0800526// Creates filegroups for miscellaneous other files
527func (f *filesystemCreator) createMiscFilegroups(ctx android.LoadHookContext) {
528 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
529
530 if partitionVars.BoardErofsCompressorHints != "" {
531 dir := filepath.Dir(partitionVars.BoardErofsCompressorHints)
532 base := filepath.Base(partitionVars.BoardErofsCompressorHints)
533 ctx.CreateModuleInDirectory(
534 android.FileGroupFactory,
535 dir,
536 &struct {
537 Name *string
538 Srcs []string
539 Visibility []string
540 }{
541 Name: proptools.StringPtr("soong_generated_board_erofs_compress_hints_filegroup"),
542 Srcs: []string{base},
543 Visibility: []string{"//visibility:public"},
544 },
545 )
546 }
547}
548
Spandan Das5e336422024-11-01 22:31:20 +0000549// createPrebuiltKernelModules creates `prebuilt_kernel_modules`. These modules will be added to deps of the
Spandan Das7b25a512024-11-06 20:41:26 +0000550// autogenerated *_dlkm filsystem modules. Each _dlkm partition should have a single prebuilt_kernel_modules dependency.
551// This ensures that the depmod artifacts (modules.* installed in /lib/modules/) are generated with a complete view.
Spandan Das5b493cd2024-11-07 20:55:56 +0000552func (f *filesystemCreator) createPrebuiltKernelModules(ctx android.LoadHookContext, partitionType string) {
Spandan Das5e336422024-11-01 22:31:20 +0000553 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
Spandan Das7b25a512024-11-06 20:41:26 +0000554 name := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-kernel-modules", partitionType))
555 props := &struct {
Spandan Das912d26b2024-11-06 19:35:17 +0000556 Name *string
557 Srcs []string
Spandan Das5b493cd2024-11-07 20:55:56 +0000558 System_deps []string
Spandan Das912d26b2024-11-06 19:35:17 +0000559 System_dlkm_specific *bool
Spandan Das5b493cd2024-11-07 20:55:56 +0000560 Vendor_dlkm_specific *bool
561 Odm_dlkm_specific *bool
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000562 Vendor_ramdisk *bool
Spandan Das5b493cd2024-11-07 20:55:56 +0000563 Load_by_default *bool
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000564 Blocklist_file *string
Jihoon Kang72dd6fc2024-11-27 01:16:39 +0000565 Options_file *string
Spandan Das7b25a512024-11-06 20:41:26 +0000566 }{
567 Name: proptools.StringPtr(name),
Spandan Das5e336422024-11-01 22:31:20 +0000568 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000569 switch partitionType {
570 case "system_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000571 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules).Strings()
Spandan Das912d26b2024-11-06 19:35:17 +0000572 props.System_dlkm_specific = proptools.BoolPtr(true)
Spandan Das5b493cd2024-11-07 20:55:56 +0000573 if len(ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelLoadModules) == 0 {
574 // Create empty modules.load file for system
575 // https://source.corp.google.com/h/googleplex-android/platform/build/+/ef55daac9954896161b26db4f3ef1781b5a5694c:core/Makefile;l=695-700;drc=549fe2a5162548bd8b47867d35f907eb22332023;bpv=1;bpt=0
576 props.Load_by_default = proptools.BoolPtr(false)
577 }
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000578 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelBlocklistFile; blocklistFile != "" {
579 props.Blocklist_file = proptools.StringPtr(blocklistFile)
580 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000581 case "vendor_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000582 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorKernelModules).Strings()
Spandan Das5b493cd2024-11-07 20:55:56 +0000583 if len(ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules) > 0 {
584 props.System_deps = []string{":" + generatedModuleName(ctx.Config(), "system_dlkm-kernel-modules") + "{.modules}"}
585 }
586 props.Vendor_dlkm_specific = proptools.BoolPtr(true)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000587 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorKernelBlocklistFile; blocklistFile != "" {
588 props.Blocklist_file = proptools.StringPtr(blocklistFile)
589 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000590 case "odm_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000591 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.OdmKernelModules).Strings()
Spandan Das5b493cd2024-11-07 20:55:56 +0000592 props.Odm_dlkm_specific = proptools.BoolPtr(true)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000593 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.OdmKernelBlocklistFile; blocklistFile != "" {
594 props.Blocklist_file = proptools.StringPtr(blocklistFile)
595 }
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000596 case "vendor_ramdisk":
597 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelModules).Strings()
598 props.Vendor_ramdisk = proptools.BoolPtr(true)
599 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelBlocklistFile; blocklistFile != "" {
600 props.Blocklist_file = proptools.StringPtr(blocklistFile)
601 }
Jihoon Kang72dd6fc2024-11-27 01:16:39 +0000602 if optionsFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelOptionsFile; optionsFile != "" {
603 props.Options_file = proptools.StringPtr(optionsFile)
604 }
605
Spandan Das5b493cd2024-11-07 20:55:56 +0000606 default:
607 ctx.ModuleErrorf("DLKM is not supported for %s\n", partitionType)
Spandan Das912d26b2024-11-06 19:35:17 +0000608 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000609
610 if len(props.Srcs) == 0 {
611 return // do not generate `prebuilt_kernel_modules` if there are no sources
612 }
613
Spandan Das7b25a512024-11-06 20:41:26 +0000614 kernelModule := ctx.CreateModuleInDirectory(
615 kernel.PrebuiltKernelModulesFactory,
616 ".", // create in root directory for now
617 props,
618 )
619 kernelModule.HideFromMake()
620 // Add to deps
621 (*fsGenState.fsDeps[partitionType])[name] = defaultDepCandidateProps(ctx.Config())
Spandan Das5e336422024-11-01 22:31:20 +0000622}
623
Spandan Das4cd93b52024-11-05 23:27:03 +0000624// Create a build_prop and android_info module. This will be used to create /vendor/build.prop
625func (f *filesystemCreator) createVendorBuildProp(ctx android.LoadHookContext) {
626 // Create a android_info for vendor
627 // The board info files might be in a directory outside the root soong namespace, so create
628 // the module in "."
629 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
630 androidInfoProps := &struct {
631 Name *string
632 Board_info_files []string
633 Bootloader_board_name *string
634 }{
635 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "android-info.prop")),
636 Board_info_files: partitionVars.BoardInfoFiles,
637 }
638 if len(androidInfoProps.Board_info_files) == 0 {
639 androidInfoProps.Bootloader_board_name = proptools.StringPtr(partitionVars.BootLoaderBoardName)
640 }
641 androidInfoProp := ctx.CreateModuleInDirectory(
642 android.AndroidInfoFactory,
643 ".",
644 androidInfoProps,
645 )
646 androidInfoProp.HideFromMake()
647 // Create a build prop for vendor
648 vendorBuildProps := &struct {
649 Name *string
650 Vendor *bool
651 Stem *string
652 Product_config *string
653 Android_info *string
654 }{
655 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "vendor-build.prop")),
656 Vendor: proptools.BoolPtr(true),
657 Stem: proptools.StringPtr("build.prop"),
658 Product_config: proptools.StringPtr(":product_config"),
659 Android_info: proptools.StringPtr(":" + androidInfoProp.Name()),
660 }
661 vendorBuildProp := ctx.CreateModule(
662 android.BuildPropFactory,
663 vendorBuildProps,
664 )
665 vendorBuildProp.HideFromMake()
666}
667
Spandan Das8fe68dc2024-10-29 18:20:11 +0000668// createLinkerConfigSourceFilegroups creates filegroup modules to generate linker.config.pb for the following partitions
669// 1. vendor: Using PRODUCT_VENDOR_LINKER_CONFIG_FRAGMENTS (space separated file list)
670// 1. product: Using PRODUCT_PRODUCT_LINKER_CONFIG_FRAGMENTS (space separated file list)
671// It creates a filegroup for each file in the fragment list
Spandan Das312cc412024-10-29 18:20:11 +0000672// The filegroup modules are then added to `linker_config_srcs` of the autogenerated vendor `android_filesystem`.
Spandan Das8fe68dc2024-10-29 18:20:11 +0000673func (f *filesystemCreator) createLinkerConfigSourceFilegroups(ctx android.LoadHookContext, partitionType string) []string {
Spandan Das312cc412024-10-29 18:20:11 +0000674 ret := []string{}
675 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Spandan Das8fe68dc2024-10-29 18:20:11 +0000676 var linkerConfigSrcs []string
677 if partitionType == "vendor" {
678 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.VendorLinkerConfigSrcs)
679 } else if partitionType == "product" {
680 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.ProductLinkerConfigSrcs)
681 } else {
682 ctx.ModuleErrorf("linker.config.pb is only supported for vendor and product partitions. For system partition, use `android_system_image`")
683 }
684
685 if len(linkerConfigSrcs) > 0 {
Spandan Das312cc412024-10-29 18:20:11 +0000686 // Create a filegroup, and add `:<filegroup_name>` to ret.
687 for index, linkerConfigSrc := range linkerConfigSrcs {
688 dir := filepath.Dir(linkerConfigSrc)
689 base := filepath.Base(linkerConfigSrc)
Spandan Das8fe68dc2024-10-29 18:20:11 +0000690 fgName := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-linker-config-src%s", partitionType, strconv.Itoa(index)))
Spandan Das312cc412024-10-29 18:20:11 +0000691 srcs := []string{base}
692 fgProps := &struct {
693 Name *string
694 Srcs proptools.Configurable[[]string]
695 }{
696 Name: proptools.StringPtr(fgName),
697 Srcs: proptools.NewSimpleConfigurable(srcs),
698 }
699 ctx.CreateModuleInDirectory(
700 android.FileGroupFactory,
701 dir,
702 fgProps,
703 )
704 ret = append(ret, ":"+fgName)
705 }
706 }
707 return ret
708}
709
mrziwang4b0ca972024-10-17 14:56:19 -0700710type filesystemBaseProperty struct {
711 Name *string
712 Compile_multilib *string
Cole Faust3552eb62024-11-06 18:07:26 -0800713 Visibility []string
mrziwang4b0ca972024-10-17 14:56:19 -0700714}
715
716func generateBaseProps(namePtr *string) *filesystemBaseProperty {
717 return &filesystemBaseProperty{
718 Name: namePtr,
719 Compile_multilib: proptools.StringPtr("both"),
Cole Faust3552eb62024-11-06 18:07:26 -0800720 // The vbmeta modules are currently in the root directory and depend on the partitions
721 Visibility: []string{"//.", "//build/soong:__subpackages__"},
mrziwang4b0ca972024-10-17 14:56:19 -0700722 }
723}
724
725func generateFsProps(ctx android.EarlyModuleContext, partitionType string) (*filesystem.FilesystemProperties, bool) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700726 fsProps := &filesystem.FilesystemProperties{}
727
mrziwang4b0ca972024-10-17 14:56:19 -0700728 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faust0c4b4152024-11-20 16:42:53 -0800729 var avbInfo avbInfo
Cole Faust76a6e952024-11-07 16:56:45 -0800730 var fsType string
731 if strings.Contains(partitionType, "ramdisk") {
732 fsType = "compressed_cpio"
733 } else {
Cole Faust953476f2024-11-14 14:11:29 -0800734 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
Cole Faust76a6e952024-11-07 16:56:45 -0800735 fsType = specificPartitionVars.BoardFileSystemType
Cole Faust0c4b4152024-11-20 16:42:53 -0800736 avbInfo = getAvbInfo(ctx.Config(), partitionType)
Cole Faust953476f2024-11-14 14:11:29 -0800737 if fsType == "" {
738 fsType = "ext4" //default
739 }
Cole Faust76a6e952024-11-07 16:56:45 -0800740 }
Cole Faust76a6e952024-11-07 16:56:45 -0800741
mrziwang4b0ca972024-10-17 14:56:19 -0700742 fsProps.Type = proptools.StringPtr(fsType)
743 if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() {
744 // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs
745 return nil, false
746 }
747
Cole Faust3e730972024-12-03 13:12:08 -0800748 if *fsProps.Type == "erofs" {
749 if partitionVars.BoardErofsCompressor != "" {
750 fsProps.Erofs.Compressor = proptools.StringPtr(partitionVars.BoardErofsCompressor)
751 }
752 if partitionVars.BoardErofsCompressorHints != "" {
753 fsProps.Erofs.Compress_hints = proptools.StringPtr(":soong_generated_board_erofs_compress_hints_filegroup")
754 }
755 }
756
Cole Faust92ccbe22024-10-03 14:38:37 -0700757 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
758 // and sometimes don't build.
759 fsProps.Unchecked_module = proptools.BoolPtr(true)
760
Jihoon Kang98047cf2024-10-02 17:13:54 +0000761 // BOARD_AVB_ENABLE
Cole Faust0c4b4152024-11-20 16:42:53 -0800762 fsProps.Use_avb = avbInfo.avbEnable
Jihoon Kang98047cf2024-10-02 17:13:54 +0000763 // BOARD_AVB_KEY_PATH
Cole Faust0c4b4152024-11-20 16:42:53 -0800764 fsProps.Avb_private_key = avbInfo.avbkeyFilegroup
Jihoon Kang98047cf2024-10-02 17:13:54 +0000765 // BOARD_AVB_ALGORITHM
Cole Faust0c4b4152024-11-20 16:42:53 -0800766 fsProps.Avb_algorithm = avbInfo.avbAlgorithm
Jihoon Kang98047cf2024-10-02 17:13:54 +0000767 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust0c4b4152024-11-20 16:42:53 -0800768 fsProps.Rollback_index = avbInfo.avbRollbackIndex
Jihoon Kang98047cf2024-10-02 17:13:54 +0000769
Cole Faust92ccbe22024-10-03 14:38:37 -0700770 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000771
Cole Faust68382192024-11-19 10:36:03 -0800772 if !strings.Contains(partitionType, "ramdisk") {
773 fsProps.Base_dir = proptools.StringPtr(partitionType)
774 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000775
Jihoon Kang0d545b82024-10-11 00:21:57 +0000776 fsProps.Is_auto_generated = proptools.BoolPtr(true)
777
Spandan Das71be42d2024-11-20 18:34:16 +0000778 partitionSpecificFsProps(ctx, fsProps, partitionVars, partitionType)
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000779
Jihoon Kang98047cf2024-10-02 17:13:54 +0000780 // system_image properties that are not set:
781 // - filesystemProperties.Avb_hash_algorithm
782 // - filesystemProperties.File_contexts
783 // - filesystemProperties.Dirs
784 // - filesystemProperties.Symlinks
785 // - filesystemProperties.Fake_timestamp
786 // - filesystemProperties.Uuid
787 // - filesystemProperties.Mount_point
788 // - filesystemProperties.Include_make_built_files
789 // - filesystemProperties.Build_logtags
Jihoon Kang98047cf2024-10-02 17:13:54 +0000790 // - systemImageProperties.Linker_config_src
mrziwang4b0ca972024-10-17 14:56:19 -0700791
792 return fsProps, true
Cole Faust92ccbe22024-10-03 14:38:37 -0700793}
794
Cole Faust0c4b4152024-11-20 16:42:53 -0800795type avbInfo struct {
796 avbEnable *bool
797 avbKeyPath *string
798 avbkeyFilegroup *string
799 avbAlgorithm *string
800 avbRollbackIndex *int64
801 avbMode *string
802}
803
804func getAvbInfo(config android.Config, partitionType string) avbInfo {
805 partitionVars := config.ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
806 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
807 var result avbInfo
808 boardAvbEnable := partitionVars.BoardAvbEnable
809 if boardAvbEnable {
810 result.avbEnable = proptools.BoolPtr(true)
811 if specificPartitionVars.BoardAvbKeyPath != "" {
812 result.avbKeyPath = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
813 } else if partitionVars.BoardAvbKeyPath != "" {
814 result.avbKeyPath = proptools.StringPtr(partitionVars.BoardAvbKeyPath)
815 }
816 if specificPartitionVars.BoardAvbAlgorithm != "" {
817 result.avbAlgorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
818 } else if partitionVars.BoardAvbAlgorithm != "" {
819 result.avbAlgorithm = proptools.StringPtr(partitionVars.BoardAvbAlgorithm)
820 }
821 if specificPartitionVars.BoardAvbRollbackIndex != "" {
822 parsed, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64)
823 if err != nil {
824 panic(fmt.Sprintf("Rollback index must be an int, got %s", specificPartitionVars.BoardAvbRollbackIndex))
825 }
826 result.avbRollbackIndex = &parsed
827 } else if partitionVars.BoardAvbRollbackIndex != "" {
828 parsed, err := strconv.ParseInt(partitionVars.BoardAvbRollbackIndex, 10, 64)
829 if err != nil {
830 panic(fmt.Sprintf("Rollback index must be an int, got %s", partitionVars.BoardAvbRollbackIndex))
831 }
832 result.avbRollbackIndex = &parsed
833 }
834 result.avbMode = proptools.StringPtr("make_legacy")
835 }
836 if result.avbKeyPath != nil {
837 fsGenState := config.Get(fsGenStateOnceKey).(*FsGenState)
838 filegroup := fsGenState.avbKeyFilegroups[*result.avbKeyPath]
839 result.avbkeyFilegroup = proptools.StringPtr(":" + filegroup)
840 }
841 return result
842}
843
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800844func (f *filesystemCreator) createFileListDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000845 partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700846 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
847 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
848 if !ok {
849 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
850 }
851 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 +0000852 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700853
854 builder := android.NewRuleBuilder(pctx, ctx)
855 builder.Command().BuiltTool("file_list_diff").
856 Input(makeFileList).
857 Input(filesystemInfo.FileListFile).
Cole Faust56301572024-11-07 15:22:42 -0800858 Text(partitionModuleName)
Cole Faust92ccbe22024-10-03 14:38:37 -0700859 builder.Command().Text("touch").Output(diffTestResultFile)
860 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
861 return diffTestResultFile
862}
863
864func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
865 hasher := sha256.New()
866 hasher.Write([]byte(message))
867 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
868 file := android.PathForModuleOut(ctx, filename)
869 builder := android.NewRuleBuilder(pctx, ctx)
870 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
871 builder.Command().Text("exit 1 #").Output(file)
872 builder.Build("failing command "+filename, "failing command "+filename)
873 return file
874}
875
Cole Faust3552eb62024-11-06 18:07:26 -0800876func createVbmetaDiff(ctx android.ModuleContext, vbmetaModuleName string, vbmetaPartitionName string) android.Path {
877 vbmetaModule := ctx.GetDirectDepWithTag(vbmetaModuleName, generatedVbmetaPartitionDepTag)
878 outputFilesProvider, ok := android.OtherModuleProvider(ctx, vbmetaModule, android.OutputFilesProvider)
879 if !ok {
880 ctx.ModuleErrorf("Expected module %s to provide OutputFiles", vbmetaModule)
881 }
882 if len(outputFilesProvider.DefaultOutputFiles) != 1 {
883 ctx.ModuleErrorf("Expected 1 output file from module %s", vbmetaModule)
884 }
885 soongVbMetaFile := outputFilesProvider.DefaultOutputFiles[0]
886 makeVbmetaFile := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/%s.img", ctx.Config().DeviceName(), vbmetaPartitionName))
887
888 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", vbmetaModuleName))
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800889 createDiffTest(ctx, diffTestResultFile, soongVbMetaFile, makeVbmetaFile)
890 return diffTestResultFile
891}
892
893func createDiffTest(ctx android.ModuleContext, diffTestResultFile android.WritablePath, file1 android.Path, file2 android.Path) {
Cole Faust3552eb62024-11-06 18:07:26 -0800894 builder := android.NewRuleBuilder(pctx, ctx)
895 builder.Command().Text("diff").
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800896 Input(file1).
897 Input(file2)
Cole Faust3552eb62024-11-06 18:07:26 -0800898 builder.Command().Text("touch").Output(diffTestResultFile)
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800899 builder.Build("diff test "+diffTestResultFile.String(), "diff test")
Cole Faust3552eb62024-11-06 18:07:26 -0800900}
901
Cole Faust92ccbe22024-10-03 14:38:37 -0700902type systemImageDepTagType struct {
903 blueprint.BaseDependencyTag
904}
905
906var generatedFilesystemDepTag systemImageDepTagType
Cole Faust3552eb62024-11-06 18:07:26 -0800907var generatedVbmetaPartitionDepTag systemImageDepTagType
Cole Faust92ccbe22024-10-03 14:38:37 -0700908
909func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
910 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000911 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -0700912 }
Cole Faust3552eb62024-11-06 18:07:26 -0800913 for _, vbmetaModule := range f.properties.Vbmeta_module_names {
914 ctx.AddDependency(ctx.Module(), generatedVbmetaPartitionDepTag, vbmetaModule)
915 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000916}
917
918func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700919 if ctx.ModuleDir() != "build/soong/fsgen" {
920 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
921 }
922 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000923
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000924 var content strings.Builder
925 generatedBp := android.PathForModuleOut(ctx, "soong_generated_product_config.bp")
926 for _, partition := range ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions {
927 content.WriteString(generateBpContent(ctx, partition))
928 content.WriteString("\n")
929 }
930 android.WriteFileRule(ctx, generatedBp, content.String())
931
mrziwang8f86c882024-10-03 12:34:33 -0700932 ctx.Phony("product_config_to_bp", generatedBp)
933
Cole Faust92ccbe22024-10-03 14:38:37 -0700934 var diffTestFiles []android.Path
935 for _, partitionType := range f.properties.Generated_partition_types {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800936 diffTestFile := f.createFileListDiffTest(ctx, partitionType)
Jihoon Kang72f812f2024-10-17 18:46:24 +0000937 diffTestFiles = append(diffTestFiles, diffTestFile)
938 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700939 }
940 for _, partitionType := range f.properties.Unsupported_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000941 diffTestFile := createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType))
942 diffTestFiles = append(diffTestFiles, diffTestFile)
943 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700944 }
Cole Faust3552eb62024-11-06 18:07:26 -0800945 for i, vbmetaModule := range f.properties.Vbmeta_module_names {
946 diffTestFile := createVbmetaDiff(ctx, vbmetaModule, f.properties.Vbmeta_partition_names[i])
947 diffTestFiles = append(diffTestFiles, diffTestFile)
948 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", f.properties.Vbmeta_partition_names[i]), diffTestFile)
949 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800950 if f.properties.Boot_image != "" {
951 diffTestFile := android.PathForModuleOut(ctx, "boot_diff_test.txt")
952 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Boot_image)
953 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/boot.img", ctx.Config().DeviceName()))
954 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
955 diffTestFiles = append(diffTestFiles, diffTestFile)
956 ctx.Phony("soong_generated_boot_filesystem_test", diffTestFile)
957 }
Cole Faust24938e22024-11-18 14:01:58 -0800958 if f.properties.Vendor_boot_image != "" {
959 diffTestFile := android.PathForModuleOut(ctx, "vendor_boot_diff_test.txt")
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000960 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Vendor_boot_image)
Cole Faust24938e22024-11-18 14:01:58 -0800961 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/vendor_boot.img", ctx.Config().DeviceName()))
962 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
963 diffTestFiles = append(diffTestFiles, diffTestFile)
964 ctx.Phony("soong_generated_vendor_boot_filesystem_test", diffTestFile)
965 }
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000966 if f.properties.Init_boot_image != "" {
967 diffTestFile := android.PathForModuleOut(ctx, "init_boot_diff_test.txt")
968 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Init_boot_image)
969 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/init_boot.img", ctx.Config().DeviceName()))
970 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
971 diffTestFiles = append(diffTestFiles, diffTestFile)
972 ctx.Phony("soong_generated_init_boot_filesystem_test", diffTestFile)
973 }
Cole Faust92ccbe22024-10-03 14:38:37 -0700974 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000975}
mrziwang8f86c882024-10-03 12:34:33 -0700976
mrziwang8f86c882024-10-03 12:34:33 -0700977func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
mrziwang4b0ca972024-10-17 14:56:19 -0700978 fsProps, fsTypeSupported := generateFsProps(ctx, partitionType)
979 if !fsTypeSupported {
980 return ""
mrziwang8f86c882024-10-03 12:34:33 -0700981 }
982
mrziwang4b0ca972024-10-17 14:56:19 -0700983 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
Jihoon Kang0d7b0112024-11-13 20:44:05 +0000984 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
985 deps := fsGenState.fsDeps[partitionType]
986 highPriorityDeps := fsGenState.generatedPrebuiltEtcModuleNames
987 depProps := generateDepStruct(*deps, highPriorityDeps)
mrziwang8f86c882024-10-03 12:34:33 -0700988
mrziwang4b0ca972024-10-17 14:56:19 -0700989 result, err := proptools.RepackProperties([]interface{}{baseProps, fsProps, depProps})
mrziwang8f86c882024-10-03 12:34:33 -0700990 if err != nil {
Cole Faustae3e1d32024-11-05 13:22:50 -0800991 ctx.ModuleErrorf("%s", err.Error())
992 return ""
mrziwang8f86c882024-10-03 12:34:33 -0700993 }
994
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000995 moduleType := "android_filesystem"
996 if partitionType == "system" {
997 moduleType = "android_system_image"
998 }
999
mrziwang8f86c882024-10-03 12:34:33 -07001000 file := &parser.File{
1001 Defs: []parser.Definition{
1002 &parser.Module{
Jihoon Kang4e5d8de2024-10-19 01:59:58 +00001003 Type: moduleType,
mrziwang8f86c882024-10-03 12:34:33 -07001004 Map: *result,
1005 },
1006 },
1007 }
1008 bytes, err := parser.Print(file)
1009 if err != nil {
1010 ctx.ModuleErrorf(err.Error())
1011 }
1012 return strings.TrimSpace(string(bytes))
1013}