blob: 238284657ca74f0d29ff9857aeb3daa64846e9a8 [file] [log] [blame]
Jihoon Kangf2c53982024-10-09 17:32:52 +00001// Copyright (C) 2024 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package filesystem
16
17import (
18 "android/soong/android"
19
20 "github.com/google/blueprint"
21 "github.com/google/blueprint/proptools"
22)
23
24type PartitionNameProperties struct {
Jihoon Kange7e3ec82025-01-02 21:29:14 +000025 // Name of the boot partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000026 Boot_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000027 // Name of the vendor boot partition filesystem module
28 Vendor_boot_partition_name *string
29 // Name of the init boot partition filesystem module
30 Init_boot_partition_name *string
31 // Name of the system partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000032 System_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000033 // Name of the system_ext partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000034 System_ext_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000035 // Name of the product partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000036 Product_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000037 // Name of the vendor partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000038 Vendor_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000039 // Name of the odm partition filesystem module
Spandan Dasc5717162024-11-01 18:33:57 +000040 Odm_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000041 // Name of the recovery partition filesystem module
42 Recovery_partition_name *string
Cole Faust3552eb62024-11-06 18:07:26 -080043 // The vbmeta partition and its "chained" partitions
44 Vbmeta_partitions []string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000045 // Name of the userdata partition filesystem module
mrziwang23ba8762024-11-07 16:21:53 -080046 Userdata_partition_name *string
Spandan Dasa0394002025-01-07 18:38:34 +000047 // Name of the system_dlkm partition filesystem module
48 System_dlkm_partition_name *string
49 // Name of the vendor_dlkm partition filesystem module
50 Vendor_dlkm_partition_name *string
51 // Name of the odm_dlkm partition filesystem module
52 Odm_dlkm_partition_name *string
Jihoon Kangf2c53982024-10-09 17:32:52 +000053}
54
55type androidDevice struct {
56 android.ModuleBase
57
58 partitionProps PartitionNameProperties
59}
60
61func AndroidDeviceFactory() android.Module {
62 module := &androidDevice{}
63 module.AddProperties(&module.partitionProps)
64 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Jihoon Kangf2c53982024-10-09 17:32:52 +000065 return module
66}
67
68type partitionDepTagType struct {
69 blueprint.BaseDependencyTag
70}
71
72var filesystemDepTag partitionDepTagType
73
74func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
75 addDependencyIfDefined := func(dep *string) {
76 if dep != nil {
Jihoon Kange7e3ec82025-01-02 21:29:14 +000077 ctx.AddFarVariationDependencies(nil, filesystemDepTag, proptools.String(dep))
Jihoon Kangf2c53982024-10-09 17:32:52 +000078 }
79 }
80
81 addDependencyIfDefined(a.partitionProps.Boot_partition_name)
Jihoon Kang9e087002025-01-08 19:12:23 +000082 addDependencyIfDefined(a.partitionProps.Vendor_boot_partition_name)
83 addDependencyIfDefined(a.partitionProps.Init_boot_partition_name)
Jihoon Kangf2c53982024-10-09 17:32:52 +000084 addDependencyIfDefined(a.partitionProps.System_partition_name)
85 addDependencyIfDefined(a.partitionProps.System_ext_partition_name)
86 addDependencyIfDefined(a.partitionProps.Product_partition_name)
87 addDependencyIfDefined(a.partitionProps.Vendor_partition_name)
Spandan Dasc5717162024-11-01 18:33:57 +000088 addDependencyIfDefined(a.partitionProps.Odm_partition_name)
mrziwang23ba8762024-11-07 16:21:53 -080089 addDependencyIfDefined(a.partitionProps.Userdata_partition_name)
Spandan Dasa0394002025-01-07 18:38:34 +000090 addDependencyIfDefined(a.partitionProps.System_dlkm_partition_name)
91 addDependencyIfDefined(a.partitionProps.Vendor_dlkm_partition_name)
92 addDependencyIfDefined(a.partitionProps.Odm_dlkm_partition_name)
Cole Faust3552eb62024-11-06 18:07:26 -080093 for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions {
94 ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition)
95 }
Jihoon Kangf2c53982024-10-09 17:32:52 +000096}
97
98func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust44080412024-12-20 14:17:07 -080099 a.buildTargetFilesZip(ctx)
100}
Jihoon Kangf2c53982024-10-09 17:32:52 +0000101
Cole Faust44080412024-12-20 14:17:07 -0800102func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext) {
103 targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir")
104 targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip")
105
106 builder := android.NewRuleBuilder(pctx, ctx)
107 builder.Command().Textf("rm -rf %s", targetFilesDir.String())
108 builder.Command().Textf("mkdir -p %s", targetFilesDir.String())
Spandan Das25649f52025-01-07 18:09:22 +0000109 partitionToSubdir := map[*string]string{
Spandan Dasa0394002025-01-07 18:38:34 +0000110 a.partitionProps.System_partition_name: "SYSTEM",
111 a.partitionProps.System_ext_partition_name: "SYSTEM_EXT",
112 a.partitionProps.Product_partition_name: "PRODUCT",
113 a.partitionProps.Vendor_partition_name: "VENDOR",
114 a.partitionProps.Odm_partition_name: "ODM",
115 a.partitionProps.System_dlkm_partition_name: "SYSTEM_DLKM",
116 a.partitionProps.Vendor_dlkm_partition_name: "VENDOR_DLKM",
117 a.partitionProps.Odm_dlkm_partition_name: "ODM_DLKM",
Spandan Das25649f52025-01-07 18:09:22 +0000118 }
119 for partition, subdir := range partitionToSubdir {
120 if partition == nil {
121 continue
122 }
123 fsInfo := a.getFilesystemInfo(ctx, *partition)
124 builder.Command().Textf("mkdir -p %s/%s", targetFilesDir.String(), subdir)
Cole Faust44080412024-12-20 14:17:07 -0800125 builder.Command().
126 BuiltTool("acp").
Spandan Das25649f52025-01-07 18:09:22 +0000127 Textf("-rd %s/. %s/%s", fsInfo.RootDir, targetFilesDir, subdir).
Cole Faust44080412024-12-20 14:17:07 -0800128 Implicit(fsInfo.Output) // so that the staging dir is built
129 }
130 builder.Command().
131 BuiltTool("soong_zip").
132 Text("-d").
133 FlagWithOutput("-o ", targetFilesZip).
134 FlagWithArg("-C ", targetFilesDir.String()).
135 FlagWithArg("-D ", targetFilesDir.String()).
136 Text("-sha256")
137 builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip")
138}
139
140func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo {
141 fsMod := ctx.GetDirectDepWithTag(depName, filesystemDepTag)
142 fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider)
143 if !ok {
144 ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName)
145 }
146 return fsInfo
Jihoon Kangf2c53982024-10-09 17:32:52 +0000147}