blob: 840ed8c72a521f09bc983102575dc72a3fa09c12 [file] [log] [blame]
Jihoon Kangf2c53982024-10-09 17:32:52 +00001// Copyright (C) 2024 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package filesystem
16
17import (
Spandan Das258c08f2025-01-08 23:30:45 +000018 "strings"
19
Jihoon Kangf2c53982024-10-09 17:32:52 +000020 "android/soong/android"
21
22 "github.com/google/blueprint"
23 "github.com/google/blueprint/proptools"
24)
25
26type PartitionNameProperties struct {
Jihoon Kange7e3ec82025-01-02 21:29:14 +000027 // Name of the boot partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000028 Boot_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000029 // Name of the vendor boot partition filesystem module
30 Vendor_boot_partition_name *string
31 // Name of the init boot partition filesystem module
32 Init_boot_partition_name *string
33 // Name of the system partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000034 System_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000035 // Name of the system_ext partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000036 System_ext_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000037 // Name of the product partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000038 Product_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000039 // Name of the vendor partition filesystem module
Jihoon Kangf2c53982024-10-09 17:32:52 +000040 Vendor_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000041 // Name of the odm partition filesystem module
Spandan Dasc5717162024-11-01 18:33:57 +000042 Odm_partition_name *string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000043 // Name of the recovery partition filesystem module
44 Recovery_partition_name *string
Cole Faust3552eb62024-11-06 18:07:26 -080045 // The vbmeta partition and its "chained" partitions
46 Vbmeta_partitions []string
Jihoon Kange7e3ec82025-01-02 21:29:14 +000047 // Name of the userdata partition filesystem module
mrziwang23ba8762024-11-07 16:21:53 -080048 Userdata_partition_name *string
Spandan Dasa0394002025-01-07 18:38:34 +000049 // Name of the system_dlkm partition filesystem module
50 System_dlkm_partition_name *string
51 // Name of the vendor_dlkm partition filesystem module
52 Vendor_dlkm_partition_name *string
53 // Name of the odm_dlkm partition filesystem module
54 Odm_dlkm_partition_name *string
Jihoon Kangf2c53982024-10-09 17:32:52 +000055}
56
57type androidDevice struct {
58 android.ModuleBase
59
60 partitionProps PartitionNameProperties
61}
62
63func AndroidDeviceFactory() android.Module {
64 module := &androidDevice{}
65 module.AddProperties(&module.partitionProps)
Cole Faust341d5f12025-01-07 15:32:38 -080066 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibFirst)
Jihoon Kangf2c53982024-10-09 17:32:52 +000067 return module
68}
69
70type partitionDepTagType struct {
71 blueprint.BaseDependencyTag
72}
73
74var filesystemDepTag partitionDepTagType
75
76func (a *androidDevice) DepsMutator(ctx android.BottomUpMutatorContext) {
77 addDependencyIfDefined := func(dep *string) {
78 if dep != nil {
Cole Faust341d5f12025-01-07 15:32:38 -080079 ctx.AddDependency(ctx.Module(), filesystemDepTag, proptools.String(dep))
Jihoon Kangf2c53982024-10-09 17:32:52 +000080 }
81 }
82
83 addDependencyIfDefined(a.partitionProps.Boot_partition_name)
Jihoon Kang9e087002025-01-08 19:12:23 +000084 addDependencyIfDefined(a.partitionProps.Init_boot_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +000085 addDependencyIfDefined(a.partitionProps.Vendor_boot_partition_name)
Jihoon Kangf2c53982024-10-09 17:32:52 +000086 addDependencyIfDefined(a.partitionProps.System_partition_name)
87 addDependencyIfDefined(a.partitionProps.System_ext_partition_name)
88 addDependencyIfDefined(a.partitionProps.Product_partition_name)
89 addDependencyIfDefined(a.partitionProps.Vendor_partition_name)
Spandan Dasc5717162024-11-01 18:33:57 +000090 addDependencyIfDefined(a.partitionProps.Odm_partition_name)
mrziwang23ba8762024-11-07 16:21:53 -080091 addDependencyIfDefined(a.partitionProps.Userdata_partition_name)
Spandan Dasa0394002025-01-07 18:38:34 +000092 addDependencyIfDefined(a.partitionProps.System_dlkm_partition_name)
93 addDependencyIfDefined(a.partitionProps.Vendor_dlkm_partition_name)
94 addDependencyIfDefined(a.partitionProps.Odm_dlkm_partition_name)
Spandan Dasef200ac2025-01-08 01:42:45 +000095 addDependencyIfDefined(a.partitionProps.Recovery_partition_name)
Cole Faust3552eb62024-11-06 18:07:26 -080096 for _, vbmetaPartition := range a.partitionProps.Vbmeta_partitions {
97 ctx.AddDependency(ctx.Module(), filesystemDepTag, vbmetaPartition)
98 }
Jihoon Kangf2c53982024-10-09 17:32:52 +000099}
100
101func (a *androidDevice) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust44080412024-12-20 14:17:07 -0800102 a.buildTargetFilesZip(ctx)
103}
Jihoon Kangf2c53982024-10-09 17:32:52 +0000104
Spandan Dasef200ac2025-01-08 01:42:45 +0000105type targetFilesZipCopy struct {
106 srcModule *string
107 destSubdir string
108}
109
Cole Faust44080412024-12-20 14:17:07 -0800110func (a *androidDevice) buildTargetFilesZip(ctx android.ModuleContext) {
111 targetFilesDir := android.PathForModuleOut(ctx, "target_files_dir")
112 targetFilesZip := android.PathForModuleOut(ctx, "target_files.zip")
113
114 builder := android.NewRuleBuilder(pctx, ctx)
115 builder.Command().Textf("rm -rf %s", targetFilesDir.String())
116 builder.Command().Textf("mkdir -p %s", targetFilesDir.String())
Spandan Dasef200ac2025-01-08 01:42:45 +0000117 toCopy := []targetFilesZipCopy{
118 targetFilesZipCopy{a.partitionProps.System_partition_name, "SYSTEM"},
119 targetFilesZipCopy{a.partitionProps.System_ext_partition_name, "SYSTEM_EXT"},
120 targetFilesZipCopy{a.partitionProps.Product_partition_name, "PRODUCT"},
121 targetFilesZipCopy{a.partitionProps.Vendor_partition_name, "VENDOR"},
122 targetFilesZipCopy{a.partitionProps.Odm_partition_name, "ODM"},
123 targetFilesZipCopy{a.partitionProps.System_dlkm_partition_name, "SYSTEM_DLKM"},
124 targetFilesZipCopy{a.partitionProps.Vendor_dlkm_partition_name, "VENDOR_DLKM"},
125 targetFilesZipCopy{a.partitionProps.Odm_dlkm_partition_name, "ODM_DLKM"},
126 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "BOOT/RAMDISK"},
127 targetFilesZipCopy{a.partitionProps.Init_boot_partition_name, "INIT_BOOT/RAMDISK"},
128 targetFilesZipCopy{a.partitionProps.Vendor_boot_partition_name, "VENDOR_BOOT/RAMDISK"},
Spandan Das25649f52025-01-07 18:09:22 +0000129 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000130 // TODO: Handle cases where recovery files are copied to BOOT/ or RECOVERY/
131 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=6211-6219?q=core%2FMakefile&ss=android%2Fplatform%2Fsuperproject%2Fmain
132 if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() {
133 toCopy = append(toCopy, targetFilesZipCopy{a.partitionProps.Recovery_partition_name, "VENDOR_BOOT/RAMDISK"})
134 }
135
136 for _, zipCopy := range toCopy {
137 if zipCopy.srcModule == nil {
Spandan Das25649f52025-01-07 18:09:22 +0000138 continue
139 }
Spandan Dasef200ac2025-01-08 01:42:45 +0000140 fsInfo := a.getFilesystemInfo(ctx, *zipCopy.srcModule)
141 subdir := zipCopy.destSubdir
142 rootDirString := fsInfo.RootDir.String()
143 if subdir == "SYSTEM" {
144 rootDirString = rootDirString + "/system"
145 }
Spandan Das25649f52025-01-07 18:09:22 +0000146 builder.Command().Textf("mkdir -p %s/%s", targetFilesDir.String(), subdir)
Cole Faust44080412024-12-20 14:17:07 -0800147 builder.Command().
148 BuiltTool("acp").
Spandan Dasef200ac2025-01-08 01:42:45 +0000149 Textf("-rd %s/. %s/%s", rootDirString, targetFilesDir, subdir).
Cole Faust44080412024-12-20 14:17:07 -0800150 Implicit(fsInfo.Output) // so that the staging dir is built
Spandan Dasef200ac2025-01-08 01:42:45 +0000151
Cole Faust44080412024-12-20 14:17:07 -0800152 }
Spandan Das258c08f2025-01-08 23:30:45 +0000153 // Copy cmdline files of boot images
154 if a.partitionProps.Vendor_boot_partition_name != nil {
155 bootImg := ctx.GetDirectDepWithTag(proptools.String(a.partitionProps.Vendor_boot_partition_name), filesystemDepTag)
156 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
157 builder.Command().Textf("echo %s > %s/%s/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir, "VENDOR_BOOT")
158 builder.Command().Textf("echo %s > %s/%s/vendor_cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir, "VENDOR_BOOT")
159 }
160 if a.partitionProps.Boot_partition_name != nil {
161 bootImg := ctx.GetDirectDepWithTag(proptools.String(a.partitionProps.Boot_partition_name), filesystemDepTag)
162 bootImgInfo, _ := android.OtherModuleProvider(ctx, bootImg, BootimgInfoProvider)
163 builder.Command().Textf("echo %s > %s/%s/cmdline", proptools.ShellEscape(strings.Join(bootImgInfo.Cmdline, " ")), targetFilesDir, "BOOT")
164 }
165
Cole Faust44080412024-12-20 14:17:07 -0800166 builder.Command().
167 BuiltTool("soong_zip").
168 Text("-d").
169 FlagWithOutput("-o ", targetFilesZip).
170 FlagWithArg("-C ", targetFilesDir.String()).
171 FlagWithArg("-D ", targetFilesDir.String()).
172 Text("-sha256")
173 builder.Build("target_files_"+ctx.ModuleName(), "Build target_files.zip")
174}
175
176func (a *androidDevice) getFilesystemInfo(ctx android.ModuleContext, depName string) FilesystemInfo {
177 fsMod := ctx.GetDirectDepWithTag(depName, filesystemDepTag)
178 fsInfo, ok := android.OtherModuleProvider(ctx, fsMod, FilesystemProvider)
179 if !ok {
180 ctx.ModuleErrorf("Expected dependency %s to be a filesystem", depName)
181 }
182 return fsInfo
Jihoon Kangf2c53982024-10-09 17:32:52 +0000183}