blob: 34c4787153282b1ca62e81bcb0d0568fef3b1b40 [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)
Jihoon Kang98047cf2024-10-02 17:13:54 +000072 module.createInternalModules(ctx)
73 })
74
75 return module
76}
77
Cole Faustf2a6e8b2024-11-14 10:54:48 -080078func generatedPartitions(ctx android.LoadHookContext) []string {
Cole Faust24938e22024-11-18 14:01:58 -080079 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faustf2a6e8b2024-11-14 10:54:48 -080080 generatedPartitions := []string{"system"}
81 if ctx.DeviceConfig().SystemExtPath() == "system_ext" {
82 generatedPartitions = append(generatedPartitions, "system_ext")
83 }
84 if ctx.DeviceConfig().BuildingVendorImage() && ctx.DeviceConfig().VendorPath() == "vendor" {
85 generatedPartitions = append(generatedPartitions, "vendor")
86 }
87 if ctx.DeviceConfig().BuildingProductImage() && ctx.DeviceConfig().ProductPath() == "product" {
88 generatedPartitions = append(generatedPartitions, "product")
89 }
90 if ctx.DeviceConfig().BuildingOdmImage() && ctx.DeviceConfig().OdmPath() == "odm" {
91 generatedPartitions = append(generatedPartitions, "odm")
92 }
93 if ctx.DeviceConfig().BuildingUserdataImage() && ctx.DeviceConfig().UserdataPath() == "data" {
94 generatedPartitions = append(generatedPartitions, "userdata")
95 }
Cole Faust24938e22024-11-18 14:01:58 -080096 if partitionVars.BuildingSystemDlkmImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -080097 generatedPartitions = append(generatedPartitions, "system_dlkm")
98 }
Cole Faust24938e22024-11-18 14:01:58 -080099 if partitionVars.BuildingVendorDlkmImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800100 generatedPartitions = append(generatedPartitions, "vendor_dlkm")
101 }
Cole Faust24938e22024-11-18 14:01:58 -0800102 if partitionVars.BuildingOdmDlkmImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800103 generatedPartitions = append(generatedPartitions, "odm_dlkm")
104 }
Cole Faust24938e22024-11-18 14:01:58 -0800105 if partitionVars.BuildingRamdiskImage {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800106 generatedPartitions = append(generatedPartitions, "ramdisk")
107 }
Cole Faust24938e22024-11-18 14:01:58 -0800108 if buildingVendorBootImage(partitionVars) {
109 generatedPartitions = append(generatedPartitions, "vendor_ramdisk")
110 }
Jihoon Kang3216c982024-12-02 19:42:20 +0000111 if ctx.DeviceConfig().BuildingRecoveryImage() && ctx.DeviceConfig().RecoveryPath() == "recovery" {
112 generatedPartitions = append(generatedPartitions, "recovery")
113 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800114 return generatedPartitions
115}
116
Jihoon Kang98047cf2024-10-02 17:13:54 +0000117func (f *filesystemCreator) createInternalModules(ctx android.LoadHookContext) {
Cole Faust3552eb62024-11-06 18:07:26 -0800118 soongGeneratedPartitions := generatedPartitions(ctx)
119 finalSoongGeneratedPartitions := make([]string, 0, len(soongGeneratedPartitions))
120 for _, partitionType := range soongGeneratedPartitions {
Cole Faust92ccbe22024-10-03 14:38:37 -0700121 if f.createPartition(ctx, partitionType) {
122 f.properties.Generated_partition_types = append(f.properties.Generated_partition_types, partitionType)
Cole Faust3552eb62024-11-06 18:07:26 -0800123 finalSoongGeneratedPartitions = append(finalSoongGeneratedPartitions, partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700124 } else {
125 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, partitionType)
126 }
127 }
Cole Faust3552eb62024-11-06 18:07:26 -0800128
Cole Faust24938e22024-11-18 14:01:58 -0800129 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Jihoon Kang70c1c682024-11-20 23:58:38 +0000130 dtbImg := createDtbImgFilegroup(ctx)
131
Cole Faust24938e22024-11-18 14:01:58 -0800132 if buildingBootImage(partitionVars) {
Jihoon Kang70c1c682024-11-20 23:58:38 +0000133 if createBootImage(ctx, dtbImg) {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800134 f.properties.Boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "boot")
135 } else {
136 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "boot")
137 }
138 }
Cole Faust24938e22024-11-18 14:01:58 -0800139 if buildingVendorBootImage(partitionVars) {
Jihoon Kang70c1c682024-11-20 23:58:38 +0000140 if createVendorBootImage(ctx, dtbImg) {
Cole Faust24938e22024-11-18 14:01:58 -0800141 f.properties.Vendor_boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "vendor_boot")
142 } else {
143 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "vendor_boot")
144 }
145 }
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000146 if buildingInitBootImage(partitionVars) {
147 if createInitBootImage(ctx) {
148 f.properties.Init_boot_image = ":" + generatedModuleNameForPartition(ctx.Config(), "init_boot")
149 } else {
150 f.properties.Unsupported_partition_types = append(f.properties.Unsupported_partition_types, "init_boot")
151 }
152 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800153
Cole Faust3552eb62024-11-06 18:07:26 -0800154 for _, x := range createVbmetaPartitions(ctx, finalSoongGeneratedPartitions) {
155 f.properties.Vbmeta_module_names = append(f.properties.Vbmeta_module_names, x.moduleName)
156 f.properties.Vbmeta_partition_names = append(f.properties.Vbmeta_partition_names, x.partitionName)
157 }
158
159 ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions = finalSoongGeneratedPartitions
160 f.createDeviceModule(ctx, finalSoongGeneratedPartitions, f.properties.Vbmeta_module_names)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000161}
162
Jihoon Kang0d545b82024-10-11 00:21:57 +0000163func generatedModuleName(cfg android.Config, suffix string) string {
Cole Faust92ccbe22024-10-03 14:38:37 -0700164 prefix := "soong"
165 if cfg.HasDeviceProduct() {
166 prefix = cfg.DeviceProduct()
167 }
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000168 return fmt.Sprintf("%s_generated_%s", prefix, suffix)
169}
170
Jihoon Kang0d545b82024-10-11 00:21:57 +0000171func generatedModuleNameForPartition(cfg android.Config, partitionType string) string {
172 return generatedModuleName(cfg, fmt.Sprintf("%s_image", partitionType))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000173}
174
Cole Faust3552eb62024-11-06 18:07:26 -0800175func (f *filesystemCreator) createDeviceModule(
176 ctx android.LoadHookContext,
177 generatedPartitionTypes []string,
178 vbmetaPartitions []string,
179) {
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000180 baseProps := &struct {
181 Name *string
182 }{
Jihoon Kang0d545b82024-10-11 00:21:57 +0000183 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "device")),
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000184 }
185
Priyanka Advani (xWF)dafaa7f2024-10-21 22:55:13 +0000186 // Currently, only the system and system_ext partition module is created.
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000187 partitionProps := &filesystem.PartitionNameProperties{}
Cole Faust3552eb62024-11-06 18:07:26 -0800188 if android.InList("system", generatedPartitionTypes) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000189 partitionProps.System_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system"))
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000190 }
Cole Faust3552eb62024-11-06 18:07:26 -0800191 if android.InList("system_ext", generatedPartitionTypes) {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000192 partitionProps.System_ext_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "system_ext"))
Spandan Das7a46f6c2024-10-14 18:41:18 +0000193 }
Cole Faust3552eb62024-11-06 18:07:26 -0800194 if android.InList("vendor", generatedPartitionTypes) {
Spandan Dase3b65312024-10-22 00:27:27 +0000195 partitionProps.Vendor_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "vendor"))
196 }
Cole Faust3552eb62024-11-06 18:07:26 -0800197 if android.InList("product", generatedPartitionTypes) {
Jihoon Kang6dd13b62024-10-22 23:21:02 +0000198 partitionProps.Product_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "product"))
199 }
Cole Faust3552eb62024-11-06 18:07:26 -0800200 if android.InList("odm", generatedPartitionTypes) {
Spandan Dasc5717162024-11-01 18:33:57 +0000201 partitionProps.Odm_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "odm"))
202 }
mrziwang23ba8762024-11-07 16:21:53 -0800203 if android.InList("userdata", f.properties.Generated_partition_types) {
204 partitionProps.Userdata_partition_name = proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), "userdata"))
205 }
Cole Faust3552eb62024-11-06 18:07:26 -0800206 partitionProps.Vbmeta_partitions = vbmetaPartitions
Jihoon Kangf1c79ca2024-10-09 20:18:38 +0000207
208 ctx.CreateModule(filesystem.AndroidDeviceFactory, baseProps, partitionProps)
Cole Faust92ccbe22024-10-03 14:38:37 -0700209}
210
Jihoon Kangd098d442024-11-19 00:03:22 +0000211func partitionSpecificFsProps(fsProps *filesystem.FilesystemProperties, partitionVars android.PartitionVariables, partitionType string) {
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000212 switch partitionType {
213 case "system":
214 fsProps.Build_logtags = proptools.BoolPtr(true)
215 // https://source.corp.google.com/h/googleplex-android/platform/build//639d79f5012a6542ab1f733b0697db45761ab0f3:core/packaging/flags.mk;l=21;drc=5ba8a8b77507f93aa48cc61c5ba3f31a4d0cbf37;bpv=1;bpt=0
216 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Justin Yuned3dbce2024-11-15 11:57:24 +0900217 // Identical to that of the aosp_shared_system_image
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000218 fsProps.Fsverity.Inputs = []string{
219 "etc/boot-image.prof",
220 "etc/dirty-image-objects",
221 "etc/preloaded-classes",
222 "etc/classpaths/*.pb",
223 "framework/*",
224 "framework/*/*", // framework/{arch}
225 "framework/oat/*/*", // framework/oat/{arch}
226 }
227 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
Cole Faust1d4e76c2024-11-26 14:15:29 -0800228 // Most of the symlinks and directories listed here originate from create_root_structure.mk,
229 // but the handwritten generic system image also recreates them:
230 // 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 -0800231 // TODO(b/377734331): only generate the symlinks if the relevant partitions exist
232 fsProps.Symlinks = []filesystem.SymlinkDefinition{
233 filesystem.SymlinkDefinition{
Cole Faust1d4e76c2024-11-26 14:15:29 -0800234 Target: proptools.StringPtr("/system/bin/init"),
235 Name: proptools.StringPtr("init"),
236 },
237 filesystem.SymlinkDefinition{
238 Target: proptools.StringPtr("/system/etc"),
239 Name: proptools.StringPtr("etc"),
240 },
241 filesystem.SymlinkDefinition{
242 Target: proptools.StringPtr("/system/bin"),
243 Name: proptools.StringPtr("bin"),
244 },
245 filesystem.SymlinkDefinition{
246 Target: proptools.StringPtr("/data/user_de/0/com.android.shell/files/bugreports"),
247 Name: proptools.StringPtr("bugreports"),
248 },
249 filesystem.SymlinkDefinition{
250 Target: proptools.StringPtr("/sys/kernel/debug"),
251 Name: proptools.StringPtr("d"),
252 },
253 filesystem.SymlinkDefinition{
254 Target: proptools.StringPtr("/storage/self/primary"),
255 Name: proptools.StringPtr("sdcard"),
256 },
257 filesystem.SymlinkDefinition{
258 Target: proptools.StringPtr("/product/etc/security/adb_keys"),
259 Name: proptools.StringPtr("adb_keys"),
260 },
261 filesystem.SymlinkDefinition{
262 Target: proptools.StringPtr("/vendor/odm/app"),
263 Name: proptools.StringPtr("odm/app"),
264 },
265 filesystem.SymlinkDefinition{
266 Target: proptools.StringPtr("/vendor/odm/bin"),
267 Name: proptools.StringPtr("odm/bin"),
268 },
269 filesystem.SymlinkDefinition{
270 Target: proptools.StringPtr("/vendor/odm/etc"),
271 Name: proptools.StringPtr("odm/etc"),
272 },
273 filesystem.SymlinkDefinition{
274 Target: proptools.StringPtr("/vendor/odm/firmware"),
275 Name: proptools.StringPtr("odm/firmware"),
276 },
277 filesystem.SymlinkDefinition{
278 Target: proptools.StringPtr("/vendor/odm/framework"),
279 Name: proptools.StringPtr("odm/framework"),
280 },
281 filesystem.SymlinkDefinition{
282 Target: proptools.StringPtr("/vendor/odm/lib"),
283 Name: proptools.StringPtr("odm/lib"),
284 },
285 filesystem.SymlinkDefinition{
286 Target: proptools.StringPtr("/vendor/odm/lib64"),
287 Name: proptools.StringPtr("odm/lib64"),
288 },
289 filesystem.SymlinkDefinition{
290 Target: proptools.StringPtr("/vendor/odm/overlay"),
291 Name: proptools.StringPtr("odm/overlay"),
292 },
293 filesystem.SymlinkDefinition{
294 Target: proptools.StringPtr("/vendor/odm/priv-app"),
295 Name: proptools.StringPtr("odm/priv-app"),
296 },
297 filesystem.SymlinkDefinition{
298 Target: proptools.StringPtr("/vendor/odm/usr"),
299 Name: proptools.StringPtr("odm/usr"),
300 },
301 filesystem.SymlinkDefinition{
mrziwang9afc2982024-11-05 14:29:48 -0800302 Target: proptools.StringPtr("/product"),
303 Name: proptools.StringPtr("system/product"),
304 },
305 filesystem.SymlinkDefinition{
306 Target: proptools.StringPtr("/system_ext"),
307 Name: proptools.StringPtr("system/system_ext"),
308 },
309 filesystem.SymlinkDefinition{
310 Target: proptools.StringPtr("/vendor"),
311 Name: proptools.StringPtr("system/vendor"),
312 },
313 filesystem.SymlinkDefinition{
314 Target: proptools.StringPtr("/system_dlkm/lib/modules"),
315 Name: proptools.StringPtr("system/lib/modules"),
316 },
Cole Faust1d4e76c2024-11-26 14:15:29 -0800317 filesystem.SymlinkDefinition{
318 Target: proptools.StringPtr("/data/cache"),
319 Name: proptools.StringPtr("cache"),
320 },
mrziwang9afc2982024-11-05 14:29:48 -0800321 }
Cole Faust1d4e76c2024-11-26 14:15:29 -0800322 fsProps.Dirs = proptools.NewSimpleConfigurable([]string{
323 // From generic_rootdirs in build/make/target/product/generic/Android.bp
324 "acct",
325 "apex",
326 "bootstrap-apex",
327 "config",
328 "data",
329 "data_mirror",
330 "debug_ramdisk",
331 "dev",
332 "linkerconfig",
333 "metadata",
334 "mnt",
335 "odm",
336 "odm_dlkm",
337 "oem",
338 "postinstall",
339 "proc",
340 "second_stage_resources",
341 "storage",
342 "sys",
343 "system",
344 "system_dlkm",
345 "tmp",
346 "vendor",
347 "vendor_dlkm",
348
349 // from android_rootdirs in build/make/target/product/generic/Android.bp
350 "system_ext",
351 "product",
352 })
Spandan Dasa8fa6b42024-10-23 00:45:29 +0000353 case "system_ext":
354 fsProps.Fsverity.Inputs = []string{
355 "framework/*",
356 "framework/*/*", // framework/{arch}
357 "framework/oat/*/*", // framework/oat/{arch}
358 }
359 fsProps.Fsverity.Libs = []string{":framework-res{.export-package.apk}"}
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000360 case "product":
361 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
362 case "vendor":
363 fsProps.Gen_aconfig_flags_pb = proptools.BoolPtr(true)
Spandan Das69464c32024-10-25 20:08:06 +0000364 fsProps.Symlinks = []filesystem.SymlinkDefinition{
365 filesystem.SymlinkDefinition{
366 Target: proptools.StringPtr("/odm"),
367 Name: proptools.StringPtr("vendor/odm"),
368 },
369 filesystem.SymlinkDefinition{
370 Target: proptools.StringPtr("/vendor_dlkm/lib/modules"),
371 Name: proptools.StringPtr("vendor/lib/modules"),
372 },
373 }
Spandan Dasc5717162024-11-01 18:33:57 +0000374 case "odm":
375 fsProps.Symlinks = []filesystem.SymlinkDefinition{
376 filesystem.SymlinkDefinition{
377 Target: proptools.StringPtr("/odm_dlkm/lib/modules"),
378 Name: proptools.StringPtr("odm/lib/modules"),
379 },
380 }
mrziwang23ba8762024-11-07 16:21:53 -0800381 case "userdata":
382 fsProps.Base_dir = proptools.StringPtr("data")
Jihoon Kangd098d442024-11-19 00:03:22 +0000383 case "ramdisk":
384 // Following the logic in https://cs.android.com/android/platform/superproject/main/+/c3c5063df32748a8806ce5da5dd0db158eab9ad9:build/make/core/Makefile;l=1307
385 fsProps.Dirs = android.NewSimpleConfigurable([]string{
386 "debug_ramdisk",
387 "dev",
388 "metadata",
389 "mnt",
390 "proc",
391 "second_stage_resources",
392 "sys",
393 })
394 if partitionVars.BoardUsesGenericKernelImage {
395 fsProps.Dirs.AppendSimpleValue([]string{
396 "first_stage_ramdisk/debug_ramdisk",
397 "first_stage_ramdisk/dev",
398 "first_stage_ramdisk/metadata",
399 "first_stage_ramdisk/mnt",
400 "first_stage_ramdisk/proc",
401 "first_stage_ramdisk/second_stage_resources",
402 "first_stage_ramdisk/sys",
403 })
404 }
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000405 }
406}
Spandan Dascbe641a2024-10-14 21:07:34 +0000407
Spandan Das5b493cd2024-11-07 20:55:56 +0000408var (
409 dlkmPartitions = []string{
410 "system_dlkm",
411 "vendor_dlkm",
412 "odm_dlkm",
413 }
414)
415
Cole Faust92ccbe22024-10-03 14:38:37 -0700416// Creates a soong module to build the given partition. Returns false if we can't support building
417// it.
418func (f *filesystemCreator) createPartition(ctx android.LoadHookContext, partitionType string) bool {
mrziwang4b0ca972024-10-17 14:56:19 -0700419 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
420
421 fsProps, supported := generateFsProps(ctx, partitionType)
422 if !supported {
423 return false
mrziwanga077b942024-10-16 16:00:06 -0700424 }
mrziwanga077b942024-10-16 16:00:06 -0700425
Cole Faust7db05752024-11-21 13:30:41 -0800426 if partitionType == "vendor" || partitionType == "product" || partitionType == "system" {
Spandan Das2047a4c2024-11-11 21:24:58 +0000427 fsProps.Linker_config.Gen_linker_config = proptools.BoolPtr(true)
Cole Faust7db05752024-11-21 13:30:41 -0800428 if partitionType != "system" {
429 fsProps.Linker_config.Linker_config_srcs = f.createLinkerConfigSourceFilegroups(ctx, partitionType)
430 }
Spandan Das312cc412024-10-29 18:20:11 +0000431 }
432
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000433 if android.InList(partitionType, append(dlkmPartitions, "vendor_ramdisk")) {
Spandan Das5b493cd2024-11-07 20:55:56 +0000434 f.createPrebuiltKernelModules(ctx, partitionType)
Spandan Das5e336422024-11-01 22:31:20 +0000435 }
436
mrziwang4b0ca972024-10-17 14:56:19 -0700437 var module android.Module
438 if partitionType == "system" {
439 module = ctx.CreateModule(filesystem.SystemImageFactory, baseProps, fsProps)
440 } else {
441 // Explicitly set the partition.
442 fsProps.Partition_type = proptools.StringPtr(partitionType)
443 module = ctx.CreateModule(filesystem.FilesystemFactory, baseProps, fsProps)
444 }
445 module.HideFromMake()
Spandan Das168098c2024-10-28 19:44:34 +0000446 if partitionType == "vendor" {
Spandan Das4cd93b52024-11-05 23:27:03 +0000447 f.createVendorBuildProp(ctx)
Spandan Das168098c2024-10-28 19:44:34 +0000448 }
mrziwang4b0ca972024-10-17 14:56:19 -0700449 return true
450}
451
Cole Faust953476f2024-11-14 14:11:29 -0800452// Creates filegroups for the files specified in BOARD_(partition_)AVB_KEY_PATH
453func (f *filesystemCreator) createAvbKeyFilegroups(ctx android.LoadHookContext) {
454 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
455 var files []string
456
457 if len(partitionVars.BoardAvbKeyPath) > 0 {
458 files = append(files, partitionVars.BoardAvbKeyPath)
459 }
460 for _, partition := range android.SortedKeys(partitionVars.PartitionQualifiedVariables) {
461 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partition]
462 if len(specificPartitionVars.BoardAvbKeyPath) > 0 {
463 files = append(files, specificPartitionVars.BoardAvbKeyPath)
464 }
465 }
466
467 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
468 for _, file := range files {
469 if _, ok := fsGenState.avbKeyFilegroups[file]; ok {
470 continue
471 }
472 if file == "external/avb/test/data/testkey_rsa4096.pem" {
473 // There already exists a checked-in filegroup for this commonly-used key, just use that
474 fsGenState.avbKeyFilegroups[file] = "avb_testkey_rsa4096"
475 continue
476 }
477 dir := filepath.Dir(file)
478 base := filepath.Base(file)
479 name := fmt.Sprintf("avb_key_%x", strings.ReplaceAll(file, "/", "_"))
480 ctx.CreateModuleInDirectory(
481 android.FileGroupFactory,
482 dir,
483 &struct {
484 Name *string
485 Srcs []string
486 Visibility []string
487 }{
488 Name: proptools.StringPtr(name),
489 Srcs: []string{base},
490 Visibility: []string{"//visibility:public"},
491 },
492 )
493 fsGenState.avbKeyFilegroups[file] = name
494 }
495}
496
Spandan Das5e336422024-11-01 22:31:20 +0000497// createPrebuiltKernelModules creates `prebuilt_kernel_modules`. These modules will be added to deps of the
Spandan Das7b25a512024-11-06 20:41:26 +0000498// autogenerated *_dlkm filsystem modules. Each _dlkm partition should have a single prebuilt_kernel_modules dependency.
499// This ensures that the depmod artifacts (modules.* installed in /lib/modules/) are generated with a complete view.
Spandan Das5b493cd2024-11-07 20:55:56 +0000500func (f *filesystemCreator) createPrebuiltKernelModules(ctx android.LoadHookContext, partitionType string) {
Spandan Das5e336422024-11-01 22:31:20 +0000501 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
Spandan Das7b25a512024-11-06 20:41:26 +0000502 name := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-kernel-modules", partitionType))
503 props := &struct {
Spandan Das912d26b2024-11-06 19:35:17 +0000504 Name *string
505 Srcs []string
Spandan Das5b493cd2024-11-07 20:55:56 +0000506 System_deps []string
Spandan Das912d26b2024-11-06 19:35:17 +0000507 System_dlkm_specific *bool
Spandan Das5b493cd2024-11-07 20:55:56 +0000508 Vendor_dlkm_specific *bool
509 Odm_dlkm_specific *bool
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000510 Vendor_ramdisk *bool
Spandan Das5b493cd2024-11-07 20:55:56 +0000511 Load_by_default *bool
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000512 Blocklist_file *string
Jihoon Kang72dd6fc2024-11-27 01:16:39 +0000513 Options_file *string
Spandan Das7b25a512024-11-06 20:41:26 +0000514 }{
515 Name: proptools.StringPtr(name),
Spandan Das5e336422024-11-01 22:31:20 +0000516 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000517 switch partitionType {
518 case "system_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000519 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules).Strings()
Spandan Das912d26b2024-11-06 19:35:17 +0000520 props.System_dlkm_specific = proptools.BoolPtr(true)
Spandan Das5b493cd2024-11-07 20:55:56 +0000521 if len(ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelLoadModules) == 0 {
522 // Create empty modules.load file for system
523 // https://source.corp.google.com/h/googleplex-android/platform/build/+/ef55daac9954896161b26db4f3ef1781b5a5694c:core/Makefile;l=695-700;drc=549fe2a5162548bd8b47867d35f907eb22332023;bpv=1;bpt=0
524 props.Load_by_default = proptools.BoolPtr(false)
525 }
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000526 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelBlocklistFile; blocklistFile != "" {
527 props.Blocklist_file = proptools.StringPtr(blocklistFile)
528 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000529 case "vendor_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000530 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorKernelModules).Strings()
Spandan Das5b493cd2024-11-07 20:55:56 +0000531 if len(ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.SystemKernelModules) > 0 {
532 props.System_deps = []string{":" + generatedModuleName(ctx.Config(), "system_dlkm-kernel-modules") + "{.modules}"}
533 }
534 props.Vendor_dlkm_specific = proptools.BoolPtr(true)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000535 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorKernelBlocklistFile; blocklistFile != "" {
536 props.Blocklist_file = proptools.StringPtr(blocklistFile)
537 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000538 case "odm_dlkm":
Spandan Das59ee5d72024-11-18 19:36:32 +0000539 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.OdmKernelModules).Strings()
Spandan Das5b493cd2024-11-07 20:55:56 +0000540 props.Odm_dlkm_specific = proptools.BoolPtr(true)
Spandan Das6dfcbdf2024-11-11 18:43:07 +0000541 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.OdmKernelBlocklistFile; blocklistFile != "" {
542 props.Blocklist_file = proptools.StringPtr(blocklistFile)
543 }
Jihoon Kanga8fa0712024-11-26 23:11:07 +0000544 case "vendor_ramdisk":
545 props.Srcs = android.ExistentPathsForSources(ctx, ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelModules).Strings()
546 props.Vendor_ramdisk = proptools.BoolPtr(true)
547 if blocklistFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelBlocklistFile; blocklistFile != "" {
548 props.Blocklist_file = proptools.StringPtr(blocklistFile)
549 }
Jihoon Kang72dd6fc2024-11-27 01:16:39 +0000550 if optionsFile := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse.VendorRamdiskKernelOptionsFile; optionsFile != "" {
551 props.Options_file = proptools.StringPtr(optionsFile)
552 }
553
Spandan Das5b493cd2024-11-07 20:55:56 +0000554 default:
555 ctx.ModuleErrorf("DLKM is not supported for %s\n", partitionType)
Spandan Das912d26b2024-11-06 19:35:17 +0000556 }
Spandan Das5b493cd2024-11-07 20:55:56 +0000557
558 if len(props.Srcs) == 0 {
559 return // do not generate `prebuilt_kernel_modules` if there are no sources
560 }
561
Spandan Das7b25a512024-11-06 20:41:26 +0000562 kernelModule := ctx.CreateModuleInDirectory(
563 kernel.PrebuiltKernelModulesFactory,
564 ".", // create in root directory for now
565 props,
566 )
567 kernelModule.HideFromMake()
568 // Add to deps
569 (*fsGenState.fsDeps[partitionType])[name] = defaultDepCandidateProps(ctx.Config())
Spandan Das5e336422024-11-01 22:31:20 +0000570}
571
Spandan Das4cd93b52024-11-05 23:27:03 +0000572// Create a build_prop and android_info module. This will be used to create /vendor/build.prop
573func (f *filesystemCreator) createVendorBuildProp(ctx android.LoadHookContext) {
574 // Create a android_info for vendor
575 // The board info files might be in a directory outside the root soong namespace, so create
576 // the module in "."
577 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
578 androidInfoProps := &struct {
579 Name *string
580 Board_info_files []string
581 Bootloader_board_name *string
582 }{
583 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "android-info.prop")),
584 Board_info_files: partitionVars.BoardInfoFiles,
585 }
586 if len(androidInfoProps.Board_info_files) == 0 {
587 androidInfoProps.Bootloader_board_name = proptools.StringPtr(partitionVars.BootLoaderBoardName)
588 }
589 androidInfoProp := ctx.CreateModuleInDirectory(
590 android.AndroidInfoFactory,
591 ".",
592 androidInfoProps,
593 )
594 androidInfoProp.HideFromMake()
595 // Create a build prop for vendor
596 vendorBuildProps := &struct {
597 Name *string
598 Vendor *bool
599 Stem *string
600 Product_config *string
601 Android_info *string
602 }{
603 Name: proptools.StringPtr(generatedModuleName(ctx.Config(), "vendor-build.prop")),
604 Vendor: proptools.BoolPtr(true),
605 Stem: proptools.StringPtr("build.prop"),
606 Product_config: proptools.StringPtr(":product_config"),
607 Android_info: proptools.StringPtr(":" + androidInfoProp.Name()),
608 }
609 vendorBuildProp := ctx.CreateModule(
610 android.BuildPropFactory,
611 vendorBuildProps,
612 )
613 vendorBuildProp.HideFromMake()
614}
615
Spandan Das8fe68dc2024-10-29 18:20:11 +0000616// createLinkerConfigSourceFilegroups creates filegroup modules to generate linker.config.pb for the following partitions
617// 1. vendor: Using PRODUCT_VENDOR_LINKER_CONFIG_FRAGMENTS (space separated file list)
618// 1. product: Using PRODUCT_PRODUCT_LINKER_CONFIG_FRAGMENTS (space separated file list)
619// It creates a filegroup for each file in the fragment list
Spandan Das312cc412024-10-29 18:20:11 +0000620// The filegroup modules are then added to `linker_config_srcs` of the autogenerated vendor `android_filesystem`.
Spandan Das8fe68dc2024-10-29 18:20:11 +0000621func (f *filesystemCreator) createLinkerConfigSourceFilegroups(ctx android.LoadHookContext, partitionType string) []string {
Spandan Das312cc412024-10-29 18:20:11 +0000622 ret := []string{}
623 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Spandan Das8fe68dc2024-10-29 18:20:11 +0000624 var linkerConfigSrcs []string
625 if partitionType == "vendor" {
626 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.VendorLinkerConfigSrcs)
627 } else if partitionType == "product" {
628 linkerConfigSrcs = android.FirstUniqueStrings(partitionVars.ProductLinkerConfigSrcs)
629 } else {
630 ctx.ModuleErrorf("linker.config.pb is only supported for vendor and product partitions. For system partition, use `android_system_image`")
631 }
632
633 if len(linkerConfigSrcs) > 0 {
Spandan Das312cc412024-10-29 18:20:11 +0000634 // Create a filegroup, and add `:<filegroup_name>` to ret.
635 for index, linkerConfigSrc := range linkerConfigSrcs {
636 dir := filepath.Dir(linkerConfigSrc)
637 base := filepath.Base(linkerConfigSrc)
Spandan Das8fe68dc2024-10-29 18:20:11 +0000638 fgName := generatedModuleName(ctx.Config(), fmt.Sprintf("%s-linker-config-src%s", partitionType, strconv.Itoa(index)))
Spandan Das312cc412024-10-29 18:20:11 +0000639 srcs := []string{base}
640 fgProps := &struct {
641 Name *string
642 Srcs proptools.Configurable[[]string]
643 }{
644 Name: proptools.StringPtr(fgName),
645 Srcs: proptools.NewSimpleConfigurable(srcs),
646 }
647 ctx.CreateModuleInDirectory(
648 android.FileGroupFactory,
649 dir,
650 fgProps,
651 )
652 ret = append(ret, ":"+fgName)
653 }
654 }
655 return ret
656}
657
mrziwang4b0ca972024-10-17 14:56:19 -0700658type filesystemBaseProperty struct {
659 Name *string
660 Compile_multilib *string
Cole Faust3552eb62024-11-06 18:07:26 -0800661 Visibility []string
mrziwang4b0ca972024-10-17 14:56:19 -0700662}
663
664func generateBaseProps(namePtr *string) *filesystemBaseProperty {
665 return &filesystemBaseProperty{
666 Name: namePtr,
667 Compile_multilib: proptools.StringPtr("both"),
Cole Faust3552eb62024-11-06 18:07:26 -0800668 // The vbmeta modules are currently in the root directory and depend on the partitions
669 Visibility: []string{"//.", "//build/soong:__subpackages__"},
mrziwang4b0ca972024-10-17 14:56:19 -0700670 }
671}
672
673func generateFsProps(ctx android.EarlyModuleContext, partitionType string) (*filesystem.FilesystemProperties, bool) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700674 fsProps := &filesystem.FilesystemProperties{}
675
mrziwang4b0ca972024-10-17 14:56:19 -0700676 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
Cole Faust0c4b4152024-11-20 16:42:53 -0800677 var avbInfo avbInfo
Cole Faust76a6e952024-11-07 16:56:45 -0800678 var fsType string
679 if strings.Contains(partitionType, "ramdisk") {
680 fsType = "compressed_cpio"
681 } else {
Cole Faust953476f2024-11-14 14:11:29 -0800682 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
Cole Faust76a6e952024-11-07 16:56:45 -0800683 fsType = specificPartitionVars.BoardFileSystemType
Cole Faust0c4b4152024-11-20 16:42:53 -0800684 avbInfo = getAvbInfo(ctx.Config(), partitionType)
Cole Faust953476f2024-11-14 14:11:29 -0800685 if fsType == "" {
686 fsType = "ext4" //default
687 }
Cole Faust76a6e952024-11-07 16:56:45 -0800688 }
Cole Faust76a6e952024-11-07 16:56:45 -0800689
mrziwang4b0ca972024-10-17 14:56:19 -0700690 fsProps.Type = proptools.StringPtr(fsType)
691 if filesystem.GetFsTypeFromString(ctx, *fsProps.Type).IsUnknown() {
692 // Currently the android_filesystem module type only supports a handful of FS types like ext4, erofs
693 return nil, false
694 }
695
Cole Faust92ccbe22024-10-03 14:38:37 -0700696 // Don't build this module on checkbuilds, the soong-built partitions are still in-progress
697 // and sometimes don't build.
698 fsProps.Unchecked_module = proptools.BoolPtr(true)
699
Jihoon Kang98047cf2024-10-02 17:13:54 +0000700 // BOARD_AVB_ENABLE
Cole Faust0c4b4152024-11-20 16:42:53 -0800701 fsProps.Use_avb = avbInfo.avbEnable
Jihoon Kang98047cf2024-10-02 17:13:54 +0000702 // BOARD_AVB_KEY_PATH
Cole Faust0c4b4152024-11-20 16:42:53 -0800703 fsProps.Avb_private_key = avbInfo.avbkeyFilegroup
Jihoon Kang98047cf2024-10-02 17:13:54 +0000704 // BOARD_AVB_ALGORITHM
Cole Faust0c4b4152024-11-20 16:42:53 -0800705 fsProps.Avb_algorithm = avbInfo.avbAlgorithm
Jihoon Kang98047cf2024-10-02 17:13:54 +0000706 // BOARD_AVB_SYSTEM_ROLLBACK_INDEX
Cole Faust0c4b4152024-11-20 16:42:53 -0800707 fsProps.Rollback_index = avbInfo.avbRollbackIndex
Jihoon Kang98047cf2024-10-02 17:13:54 +0000708
Cole Faust92ccbe22024-10-03 14:38:37 -0700709 fsProps.Partition_name = proptools.StringPtr(partitionType)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000710
Cole Faust68382192024-11-19 10:36:03 -0800711 if !strings.Contains(partitionType, "ramdisk") {
712 fsProps.Base_dir = proptools.StringPtr(partitionType)
713 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000714
Jihoon Kang0d545b82024-10-11 00:21:57 +0000715 fsProps.Is_auto_generated = proptools.BoolPtr(true)
716
Jihoon Kangd098d442024-11-19 00:03:22 +0000717 partitionSpecificFsProps(fsProps, partitionVars, partitionType)
Jihoon Kang6850d8f2024-10-17 20:45:58 +0000718
Jihoon Kang98047cf2024-10-02 17:13:54 +0000719 // system_image properties that are not set:
720 // - filesystemProperties.Avb_hash_algorithm
721 // - filesystemProperties.File_contexts
722 // - filesystemProperties.Dirs
723 // - filesystemProperties.Symlinks
724 // - filesystemProperties.Fake_timestamp
725 // - filesystemProperties.Uuid
726 // - filesystemProperties.Mount_point
727 // - filesystemProperties.Include_make_built_files
728 // - filesystemProperties.Build_logtags
Jihoon Kang98047cf2024-10-02 17:13:54 +0000729 // - systemImageProperties.Linker_config_src
mrziwang4b0ca972024-10-17 14:56:19 -0700730
731 return fsProps, true
Cole Faust92ccbe22024-10-03 14:38:37 -0700732}
733
Cole Faust0c4b4152024-11-20 16:42:53 -0800734type avbInfo struct {
735 avbEnable *bool
736 avbKeyPath *string
737 avbkeyFilegroup *string
738 avbAlgorithm *string
739 avbRollbackIndex *int64
740 avbMode *string
741}
742
743func getAvbInfo(config android.Config, partitionType string) avbInfo {
744 partitionVars := config.ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
745 specificPartitionVars := partitionVars.PartitionQualifiedVariables[partitionType]
746 var result avbInfo
747 boardAvbEnable := partitionVars.BoardAvbEnable
748 if boardAvbEnable {
749 result.avbEnable = proptools.BoolPtr(true)
750 if specificPartitionVars.BoardAvbKeyPath != "" {
751 result.avbKeyPath = proptools.StringPtr(specificPartitionVars.BoardAvbKeyPath)
752 } else if partitionVars.BoardAvbKeyPath != "" {
753 result.avbKeyPath = proptools.StringPtr(partitionVars.BoardAvbKeyPath)
754 }
755 if specificPartitionVars.BoardAvbAlgorithm != "" {
756 result.avbAlgorithm = proptools.StringPtr(specificPartitionVars.BoardAvbAlgorithm)
757 } else if partitionVars.BoardAvbAlgorithm != "" {
758 result.avbAlgorithm = proptools.StringPtr(partitionVars.BoardAvbAlgorithm)
759 }
760 if specificPartitionVars.BoardAvbRollbackIndex != "" {
761 parsed, err := strconv.ParseInt(specificPartitionVars.BoardAvbRollbackIndex, 10, 64)
762 if err != nil {
763 panic(fmt.Sprintf("Rollback index must be an int, got %s", specificPartitionVars.BoardAvbRollbackIndex))
764 }
765 result.avbRollbackIndex = &parsed
766 } else if partitionVars.BoardAvbRollbackIndex != "" {
767 parsed, err := strconv.ParseInt(partitionVars.BoardAvbRollbackIndex, 10, 64)
768 if err != nil {
769 panic(fmt.Sprintf("Rollback index must be an int, got %s", partitionVars.BoardAvbRollbackIndex))
770 }
771 result.avbRollbackIndex = &parsed
772 }
773 result.avbMode = proptools.StringPtr("make_legacy")
774 }
775 if result.avbKeyPath != nil {
776 fsGenState := config.Get(fsGenStateOnceKey).(*FsGenState)
777 filegroup := fsGenState.avbKeyFilegroups[*result.avbKeyPath]
778 result.avbkeyFilegroup = proptools.StringPtr(":" + filegroup)
779 }
780 return result
781}
782
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800783func (f *filesystemCreator) createFileListDiffTest(ctx android.ModuleContext, partitionType string) android.Path {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000784 partitionModuleName := generatedModuleNameForPartition(ctx.Config(), partitionType)
Cole Faust92ccbe22024-10-03 14:38:37 -0700785 systemImage := ctx.GetDirectDepWithTag(partitionModuleName, generatedFilesystemDepTag)
786 filesystemInfo, ok := android.OtherModuleProvider(ctx, systemImage, filesystem.FilesystemProvider)
787 if !ok {
788 ctx.ModuleErrorf("Expected module %s to provide FileysystemInfo", partitionModuleName)
789 }
790 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 +0000791 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", partitionModuleName))
Cole Faust92ccbe22024-10-03 14:38:37 -0700792
793 builder := android.NewRuleBuilder(pctx, ctx)
794 builder.Command().BuiltTool("file_list_diff").
795 Input(makeFileList).
796 Input(filesystemInfo.FileListFile).
Cole Faust56301572024-11-07 15:22:42 -0800797 Text(partitionModuleName)
Cole Faust92ccbe22024-10-03 14:38:37 -0700798 builder.Command().Text("touch").Output(diffTestResultFile)
799 builder.Build(partitionModuleName+" diff test", partitionModuleName+" diff test")
800 return diffTestResultFile
801}
802
803func createFailingCommand(ctx android.ModuleContext, message string) android.Path {
804 hasher := sha256.New()
805 hasher.Write([]byte(message))
806 filename := fmt.Sprintf("failing_command_%x.txt", hasher.Sum(nil))
807 file := android.PathForModuleOut(ctx, filename)
808 builder := android.NewRuleBuilder(pctx, ctx)
809 builder.Command().Textf("echo %s", proptools.NinjaAndShellEscape(message))
810 builder.Command().Text("exit 1 #").Output(file)
811 builder.Build("failing command "+filename, "failing command "+filename)
812 return file
813}
814
Cole Faust3552eb62024-11-06 18:07:26 -0800815func createVbmetaDiff(ctx android.ModuleContext, vbmetaModuleName string, vbmetaPartitionName string) android.Path {
816 vbmetaModule := ctx.GetDirectDepWithTag(vbmetaModuleName, generatedVbmetaPartitionDepTag)
817 outputFilesProvider, ok := android.OtherModuleProvider(ctx, vbmetaModule, android.OutputFilesProvider)
818 if !ok {
819 ctx.ModuleErrorf("Expected module %s to provide OutputFiles", vbmetaModule)
820 }
821 if len(outputFilesProvider.DefaultOutputFiles) != 1 {
822 ctx.ModuleErrorf("Expected 1 output file from module %s", vbmetaModule)
823 }
824 soongVbMetaFile := outputFilesProvider.DefaultOutputFiles[0]
825 makeVbmetaFile := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/%s.img", ctx.Config().DeviceName(), vbmetaPartitionName))
826
827 diffTestResultFile := android.PathForModuleOut(ctx, fmt.Sprintf("diff_test_%s.txt", vbmetaModuleName))
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800828 createDiffTest(ctx, diffTestResultFile, soongVbMetaFile, makeVbmetaFile)
829 return diffTestResultFile
830}
831
832func createDiffTest(ctx android.ModuleContext, diffTestResultFile android.WritablePath, file1 android.Path, file2 android.Path) {
Cole Faust3552eb62024-11-06 18:07:26 -0800833 builder := android.NewRuleBuilder(pctx, ctx)
834 builder.Command().Text("diff").
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800835 Input(file1).
836 Input(file2)
Cole Faust3552eb62024-11-06 18:07:26 -0800837 builder.Command().Text("touch").Output(diffTestResultFile)
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800838 builder.Build("diff test "+diffTestResultFile.String(), "diff test")
Cole Faust3552eb62024-11-06 18:07:26 -0800839}
840
Cole Faust92ccbe22024-10-03 14:38:37 -0700841type systemImageDepTagType struct {
842 blueprint.BaseDependencyTag
843}
844
845var generatedFilesystemDepTag systemImageDepTagType
Cole Faust3552eb62024-11-06 18:07:26 -0800846var generatedVbmetaPartitionDepTag systemImageDepTagType
Cole Faust92ccbe22024-10-03 14:38:37 -0700847
848func (f *filesystemCreator) DepsMutator(ctx android.BottomUpMutatorContext) {
849 for _, partitionType := range f.properties.Generated_partition_types {
Jihoon Kang0d545b82024-10-11 00:21:57 +0000850 ctx.AddDependency(ctx.Module(), generatedFilesystemDepTag, generatedModuleNameForPartition(ctx.Config(), partitionType))
Cole Faust92ccbe22024-10-03 14:38:37 -0700851 }
Cole Faust3552eb62024-11-06 18:07:26 -0800852 for _, vbmetaModule := range f.properties.Vbmeta_module_names {
853 ctx.AddDependency(ctx.Module(), generatedVbmetaPartitionDepTag, vbmetaModule)
854 }
Jihoon Kang98047cf2024-10-02 17:13:54 +0000855}
856
857func (f *filesystemCreator) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust92ccbe22024-10-03 14:38:37 -0700858 if ctx.ModuleDir() != "build/soong/fsgen" {
859 ctx.ModuleErrorf("There can only be one soong_filesystem_creator in build/soong/fsgen")
860 }
861 f.HideFromMake()
Jihoon Kang98047cf2024-10-02 17:13:54 +0000862
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000863 var content strings.Builder
864 generatedBp := android.PathForModuleOut(ctx, "soong_generated_product_config.bp")
865 for _, partition := range ctx.Config().Get(fsGenStateOnceKey).(*FsGenState).soongGeneratedPartitions {
866 content.WriteString(generateBpContent(ctx, partition))
867 content.WriteString("\n")
868 }
869 android.WriteFileRule(ctx, generatedBp, content.String())
870
mrziwang8f86c882024-10-03 12:34:33 -0700871 ctx.Phony("product_config_to_bp", generatedBp)
872
Cole Faust92ccbe22024-10-03 14:38:37 -0700873 var diffTestFiles []android.Path
874 for _, partitionType := range f.properties.Generated_partition_types {
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800875 diffTestFile := f.createFileListDiffTest(ctx, partitionType)
Jihoon Kang72f812f2024-10-17 18:46:24 +0000876 diffTestFiles = append(diffTestFiles, diffTestFile)
877 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700878 }
879 for _, partitionType := range f.properties.Unsupported_partition_types {
Jihoon Kang72f812f2024-10-17 18:46:24 +0000880 diffTestFile := createFailingCommand(ctx, fmt.Sprintf("Couldn't build %s partition", partitionType))
881 diffTestFiles = append(diffTestFiles, diffTestFile)
882 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", partitionType), diffTestFile)
Cole Faust92ccbe22024-10-03 14:38:37 -0700883 }
Cole Faust3552eb62024-11-06 18:07:26 -0800884 for i, vbmetaModule := range f.properties.Vbmeta_module_names {
885 diffTestFile := createVbmetaDiff(ctx, vbmetaModule, f.properties.Vbmeta_partition_names[i])
886 diffTestFiles = append(diffTestFiles, diffTestFile)
887 ctx.Phony(fmt.Sprintf("soong_generated_%s_filesystem_test", f.properties.Vbmeta_partition_names[i]), diffTestFile)
888 }
Cole Faustf2a6e8b2024-11-14 10:54:48 -0800889 if f.properties.Boot_image != "" {
890 diffTestFile := android.PathForModuleOut(ctx, "boot_diff_test.txt")
891 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Boot_image)
892 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/boot.img", ctx.Config().DeviceName()))
893 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
894 diffTestFiles = append(diffTestFiles, diffTestFile)
895 ctx.Phony("soong_generated_boot_filesystem_test", diffTestFile)
896 }
Cole Faust24938e22024-11-18 14:01:58 -0800897 if f.properties.Vendor_boot_image != "" {
898 diffTestFile := android.PathForModuleOut(ctx, "vendor_boot_diff_test.txt")
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000899 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Vendor_boot_image)
Cole Faust24938e22024-11-18 14:01:58 -0800900 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/vendor_boot.img", ctx.Config().DeviceName()))
901 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
902 diffTestFiles = append(diffTestFiles, diffTestFile)
903 ctx.Phony("soong_generated_vendor_boot_filesystem_test", diffTestFile)
904 }
Jihoon Kang95eb1da2024-11-19 20:55:20 +0000905 if f.properties.Init_boot_image != "" {
906 diffTestFile := android.PathForModuleOut(ctx, "init_boot_diff_test.txt")
907 soongBootImg := android.PathForModuleSrc(ctx, f.properties.Init_boot_image)
908 makeBootImage := android.PathForArbitraryOutput(ctx, fmt.Sprintf("target/product/%s/init_boot.img", ctx.Config().DeviceName()))
909 createDiffTest(ctx, diffTestFile, soongBootImg, makeBootImage)
910 diffTestFiles = append(diffTestFiles, diffTestFile)
911 ctx.Phony("soong_generated_init_boot_filesystem_test", diffTestFile)
912 }
Cole Faust92ccbe22024-10-03 14:38:37 -0700913 ctx.Phony("soong_generated_filesystem_tests", diffTestFiles...)
Jihoon Kang98047cf2024-10-02 17:13:54 +0000914}
mrziwang8f86c882024-10-03 12:34:33 -0700915
mrziwang8f86c882024-10-03 12:34:33 -0700916func generateBpContent(ctx android.EarlyModuleContext, partitionType string) string {
mrziwang4b0ca972024-10-17 14:56:19 -0700917 fsProps, fsTypeSupported := generateFsProps(ctx, partitionType)
918 if !fsTypeSupported {
919 return ""
mrziwang8f86c882024-10-03 12:34:33 -0700920 }
921
mrziwang4b0ca972024-10-17 14:56:19 -0700922 baseProps := generateBaseProps(proptools.StringPtr(generatedModuleNameForPartition(ctx.Config(), partitionType)))
Jihoon Kang0d7b0112024-11-13 20:44:05 +0000923 fsGenState := ctx.Config().Get(fsGenStateOnceKey).(*FsGenState)
924 deps := fsGenState.fsDeps[partitionType]
925 highPriorityDeps := fsGenState.generatedPrebuiltEtcModuleNames
926 depProps := generateDepStruct(*deps, highPriorityDeps)
mrziwang8f86c882024-10-03 12:34:33 -0700927
mrziwang4b0ca972024-10-17 14:56:19 -0700928 result, err := proptools.RepackProperties([]interface{}{baseProps, fsProps, depProps})
mrziwang8f86c882024-10-03 12:34:33 -0700929 if err != nil {
Cole Faustae3e1d32024-11-05 13:22:50 -0800930 ctx.ModuleErrorf("%s", err.Error())
931 return ""
mrziwang8f86c882024-10-03 12:34:33 -0700932 }
933
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000934 moduleType := "android_filesystem"
935 if partitionType == "system" {
936 moduleType = "android_system_image"
937 }
938
mrziwang8f86c882024-10-03 12:34:33 -0700939 file := &parser.File{
940 Defs: []parser.Definition{
941 &parser.Module{
Jihoon Kang4e5d8de2024-10-19 01:59:58 +0000942 Type: moduleType,
mrziwang8f86c882024-10-03 12:34:33 -0700943 Map: *result,
944 },
945 },
946 }
947 bytes, err := parser.Print(file)
948 if err != nil {
949 ctx.ModuleErrorf(err.Error())
950 }
951 return strings.TrimSpace(string(bytes))
952}