blob: 5332462205f553a3c73949457d809c9ff4523ed5 [file] [log] [blame]
mrziwang79730d42024-12-02 22:13:59 -08001// 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 "fmt"
19 "path/filepath"
Cole Faust498ffc12025-01-15 14:19:32 -080020 "regexp"
Cole Faust74ee4e02025-01-16 14:55:35 -080021 "slices"
mrziwang79730d42024-12-02 22:13:59 -080022 "strconv"
23 "strings"
24
25 "android/soong/android"
Cole Faust2bdc5e52025-01-10 10:29:36 -080026
mrziwang79730d42024-12-02 22:13:59 -080027 "github.com/google/blueprint"
28 "github.com/google/blueprint/proptools"
29)
30
31func init() {
32 android.RegisterModuleType("super_image", SuperImageFactory)
33}
34
35type superImage struct {
36 android.ModuleBase
37
38 properties SuperImageProperties
39 partitionProps SuperImagePartitionNameProperties
40
41 installDir android.InstallPath
42}
43
44type SuperImageProperties struct {
45 // the size of the super partition
46 Size *int64
47 // the block device where metadata for dynamic partitions is stored
48 Metadata_device *string
49 // the super partition block device list
mrziwangf3c8ddf2024-12-05 17:15:11 -080050 Block_devices []string
mrziwang79730d42024-12-02 22:13:59 -080051 // whether A/B updater is used
52 Ab_update *bool
53 // whether dynamic partitions is enabled on devices that were launched without this support
54 Retrofit *bool
mrziwang79730d42024-12-02 22:13:59 -080055 // whether the output is a sparse image
56 Sparse *bool
57 // information about how partitions within the super partition are grouped together
58 Partition_groups []PartitionGroupsInfo
Cole Faust74ee4e02025-01-16 14:55:35 -080059 // Name of the system_other partition filesystem module. This module will be installed to
60 // the "b" slot of the system partition in a/b partition builds.
61 System_other_partition *string
mrziwang79730d42024-12-02 22:13:59 -080062 // whether dynamic partitions is used
63 Use_dynamic_partitions *bool
Cole Faust498ffc12025-01-15 14:19:32 -080064 Virtual_ab struct {
65 // whether virtual A/B seamless update is enabled
66 Enable *bool
67 // whether retrofitting virtual A/B seamless update is enabled
68 Retrofit *bool
69 // If set, device uses virtual A/B Compression
70 Compression *bool
71 // This value controls the compression algorithm used for VABC.
72 // Valid options are defined in system/core/fs_mgr/libsnapshot/cow_writer.cpp
73 // e.g. "none", "gz", "brotli"
74 Compression_method *string
75 // Specifies maximum bytes to be compressed at once during ota. Options: 4096, 8192, 16384, 32768, 65536, 131072, 262144.
76 Compression_factor *int64
77 // Specifies COW version to be used by update_engine and libsnapshot. If this value is not
78 // specified we default to COW version 2 in update_engine for backwards compatibility
79 Cow_version *int64
80 }
mrziwang79730d42024-12-02 22:13:59 -080081}
82
83type PartitionGroupsInfo struct {
84 Name string
85 GroupSize string
86 PartitionList []string
87}
88
89type SuperImagePartitionNameProperties struct {
90 // Name of the System partition filesystem module
91 System_partition *string
92 // Name of the System_ext partition filesystem module
93 System_ext_partition *string
94 // Name of the System_dlkm partition filesystem module
95 System_dlkm_partition *string
96 // Name of the System_other partition filesystem module
97 System_other_partition *string
98 // Name of the Product partition filesystem module
99 Product_partition *string
100 // Name of the Vendor partition filesystem module
101 Vendor_partition *string
102 // Name of the Vendor_dlkm partition filesystem module
103 Vendor_dlkm_partition *string
104 // Name of the Odm partition filesystem module
105 Odm_partition *string
106 // Name of the Odm_dlkm partition filesystem module
107 Odm_dlkm_partition *string
108}
109
Cole Faust2bdc5e52025-01-10 10:29:36 -0800110type SuperImageInfo struct {
111 // The built super.img file, which contains the sub-partitions
112 SuperImage android.Path
113
114 // Mapping from the sub-partition type to its re-exported FileSystemInfo providers from the
115 // sub-partitions.
116 SubImageInfo map[string]FilesystemInfo
117}
118
119var SuperImageProvider = blueprint.NewProvider[SuperImageInfo]()
120
mrziwang79730d42024-12-02 22:13:59 -0800121func SuperImageFactory() android.Module {
122 module := &superImage{}
123 module.AddProperties(&module.properties, &module.partitionProps)
124 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
125 return module
126}
127
128type superImageDepTagType struct {
129 blueprint.BaseDependencyTag
130}
131
Cole Faust2bdc5e52025-01-10 10:29:36 -0800132var subImageDepTag superImageDepTagType
mrziwang79730d42024-12-02 22:13:59 -0800133
Cole Faust74ee4e02025-01-16 14:55:35 -0800134type systemOtherDepTagType struct {
135 blueprint.BaseDependencyTag
136}
137
138var systemOtherDepTag systemOtherDepTagType
139
mrziwang79730d42024-12-02 22:13:59 -0800140func (s *superImage) DepsMutator(ctx android.BottomUpMutatorContext) {
141 addDependencyIfDefined := func(dep *string) {
142 if dep != nil {
Cole Faust2bdc5e52025-01-10 10:29:36 -0800143 ctx.AddDependency(ctx.Module(), subImageDepTag, proptools.String(dep))
mrziwang79730d42024-12-02 22:13:59 -0800144 }
145 }
146
147 addDependencyIfDefined(s.partitionProps.System_partition)
148 addDependencyIfDefined(s.partitionProps.System_ext_partition)
149 addDependencyIfDefined(s.partitionProps.System_dlkm_partition)
150 addDependencyIfDefined(s.partitionProps.System_other_partition)
151 addDependencyIfDefined(s.partitionProps.Product_partition)
152 addDependencyIfDefined(s.partitionProps.Vendor_partition)
153 addDependencyIfDefined(s.partitionProps.Vendor_dlkm_partition)
154 addDependencyIfDefined(s.partitionProps.Odm_partition)
155 addDependencyIfDefined(s.partitionProps.Odm_dlkm_partition)
Cole Faust74ee4e02025-01-16 14:55:35 -0800156 if s.properties.System_other_partition != nil {
157 ctx.AddDependency(ctx.Module(), systemOtherDepTag, *s.properties.System_other_partition)
158 }
mrziwang79730d42024-12-02 22:13:59 -0800159}
160
161func (s *superImage) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust2bdc5e52025-01-10 10:29:36 -0800162 miscInfo, deps, subImageInfos := s.buildMiscInfo(ctx)
mrziwang79730d42024-12-02 22:13:59 -0800163 builder := android.NewRuleBuilder(pctx, ctx)
164 output := android.PathForModuleOut(ctx, s.installFileName())
165 lpMake := ctx.Config().HostToolPath(ctx, "lpmake")
166 lpMakeDir := filepath.Dir(lpMake.String())
167 deps = append(deps, lpMake)
168 builder.Command().Textf("PATH=%s:\\$PATH", lpMakeDir).
169 BuiltTool("build_super_image").
170 Text("-v").
171 Input(miscInfo).
172 Implicits(deps).
173 Output(output)
174 builder.Build("build_super_image", fmt.Sprintf("Creating super image %s", s.BaseModuleName()))
Cole Faust2bdc5e52025-01-10 10:29:36 -0800175 android.SetProvider(ctx, SuperImageProvider, SuperImageInfo{
176 SuperImage: output,
177 SubImageInfo: subImageInfos,
178 })
mrziwang79730d42024-12-02 22:13:59 -0800179 ctx.SetOutputFiles([]android.Path{output}, "")
Cole Faust2bdc5e52025-01-10 10:29:36 -0800180 ctx.CheckbuildFile(output)
mrziwang79730d42024-12-02 22:13:59 -0800181}
182
183func (s *superImage) installFileName() string {
184 return s.BaseModuleName() + ".img"
185}
186
Cole Faust2bdc5e52025-01-10 10:29:36 -0800187func (s *superImage) buildMiscInfo(ctx android.ModuleContext) (android.Path, android.Paths, map[string]FilesystemInfo) {
mrziwang79730d42024-12-02 22:13:59 -0800188 var miscInfoString strings.Builder
189 addStr := func(name string, value string) {
190 miscInfoString.WriteString(name)
191 miscInfoString.WriteRune('=')
192 miscInfoString.WriteString(value)
193 miscInfoString.WriteRune('\n')
194 }
195
Cole Faust498ffc12025-01-15 14:19:32 -0800196 addStr("build_super_partition", "true")
mrziwang79730d42024-12-02 22:13:59 -0800197 addStr("use_dynamic_partitions", strconv.FormatBool(proptools.Bool(s.properties.Use_dynamic_partitions)))
Cole Faust498ffc12025-01-15 14:19:32 -0800198 if proptools.Bool(s.properties.Retrofit) {
199 addStr("dynamic_partition_retrofit", "true")
200 }
mrziwang79730d42024-12-02 22:13:59 -0800201 addStr("lpmake", "lpmake")
202 addStr("super_metadata_device", proptools.String(s.properties.Metadata_device))
mrziwangf3c8ddf2024-12-05 17:15:11 -0800203 if len(s.properties.Block_devices) > 0 {
204 addStr("super_block_devices", strings.Join(s.properties.Block_devices, " "))
205 }
Cole Faust498ffc12025-01-15 14:19:32 -0800206 addStr("super_partition_size", strconv.Itoa(proptools.Int(s.properties.Size)))
207 // TODO: In make, there's more complicated logic than just this surrounding super_*_device_size
mrziwang79730d42024-12-02 22:13:59 -0800208 addStr("super_super_device_size", strconv.Itoa(proptools.Int(s.properties.Size)))
209 var groups, partitionList []string
210 for _, groupInfo := range s.properties.Partition_groups {
211 groups = append(groups, groupInfo.Name)
212 partitionList = append(partitionList, groupInfo.PartitionList...)
213 addStr("super_"+groupInfo.Name+"_group_size", groupInfo.GroupSize)
214 addStr("super_"+groupInfo.Name+"_partition_list", strings.Join(groupInfo.PartitionList, " "))
215 }
Cole Faust2bdc5e52025-01-10 10:29:36 -0800216 initialPartitionListLen := len(partitionList)
217 partitionList = android.SortedUniqueStrings(partitionList)
218 if len(partitionList) != initialPartitionListLen {
219 ctx.ModuleErrorf("Duplicate partitions found in the partition_groups property")
220 }
mrziwang79730d42024-12-02 22:13:59 -0800221 addStr("super_partition_groups", strings.Join(groups, " "))
222 addStr("dynamic_partition_list", strings.Join(partitionList, " "))
223
mrziwang79730d42024-12-02 22:13:59 -0800224 addStr("ab_update", strconv.FormatBool(proptools.Bool(s.properties.Ab_update)))
Cole Faust498ffc12025-01-15 14:19:32 -0800225
226 if proptools.Bool(s.properties.Virtual_ab.Enable) {
227 addStr("virtual_ab", "true")
228 if proptools.Bool(s.properties.Virtual_ab.Retrofit) {
229 addStr("virtual_ab_retrofit", "true")
230 }
231 addStr("virtual_ab_compression", strconv.FormatBool(proptools.Bool(s.properties.Virtual_ab.Compression)))
232 if s.properties.Virtual_ab.Compression_method != nil {
233 matched, _ := regexp.MatchString("^[a-zA-Z0-9_-]+$", *s.properties.Virtual_ab.Compression_method)
234 if !matched {
235 ctx.PropertyErrorf("virtual_ab.compression_method", "compression_method cannot have special characters")
236 }
237 addStr("virtual_ab_compression_method", *s.properties.Virtual_ab.Compression_method)
238 }
239 if s.properties.Virtual_ab.Compression_factor != nil {
240 addStr("virtual_ab_compression_factor", strconv.FormatInt(*s.properties.Virtual_ab.Compression_factor, 10))
241 }
242 if s.properties.Virtual_ab.Cow_version != nil {
243 addStr("virtual_ab_cow_version", strconv.FormatInt(*s.properties.Virtual_ab.Cow_version, 10))
244 }
245
246 } else {
247 if s.properties.Virtual_ab.Retrofit != nil {
Cole Faust29e333d2025-01-15 16:53:44 -0800248 ctx.PropertyErrorf("virtual_ab.retrofit", "This property cannot be set when virtual_ab is disabled")
Cole Faust498ffc12025-01-15 14:19:32 -0800249 }
250 if s.properties.Virtual_ab.Compression != nil {
251 ctx.PropertyErrorf("virtual_ab.compression", "This property cannot be set when virtual_ab is disabled")
252 }
253 if s.properties.Virtual_ab.Compression_method != nil {
254 ctx.PropertyErrorf("virtual_ab.compression_method", "This property cannot be set when virtual_ab is disabled")
255 }
256 if s.properties.Virtual_ab.Compression_factor != nil {
257 ctx.PropertyErrorf("virtual_ab.compression_factor", "This property cannot be set when virtual_ab is disabled")
258 }
259 }
mrziwang79730d42024-12-02 22:13:59 -0800260
Cole Faust2bdc5e52025-01-10 10:29:36 -0800261 subImageInfo := make(map[string]FilesystemInfo)
262 var deps android.Paths
263
264 missingPartitionErrorMessage := ""
265 handleSubPartition := func(partitionType string, name *string) {
266 if proptools.String(name) == "" {
267 missingPartitionErrorMessage += fmt.Sprintf("%s image listed in partition groups, but its module was not specified. ", partitionType)
268 return
mrziwang79730d42024-12-02 22:13:59 -0800269 }
Cole Faust2bdc5e52025-01-10 10:29:36 -0800270 mod := ctx.GetDirectDepWithTag(*name, subImageDepTag)
271 if mod == nil {
272 ctx.ModuleErrorf("Could not get dep %q", *name)
273 return
274 }
275 info, ok := android.OtherModuleProvider(ctx, mod, FilesystemProvider)
276 if !ok {
277 ctx.ModuleErrorf("Expected dep %q to provide FilesystemInfo", *name)
278 return
279 }
280 addStr(partitionType+"_image", info.Output.String())
281 deps = append(deps, info.Output)
282 if _, ok := subImageInfo[partitionType]; ok {
283 ctx.ModuleErrorf("Already set subimageInfo for %q", partitionType)
284 }
285 subImageInfo[partitionType] = info
mrziwang79730d42024-12-02 22:13:59 -0800286 }
287
288 // Build partitionToImagePath, because system partition may need system_other
289 // partition image path
290 for _, p := range partitionList {
mrziwang79730d42024-12-02 22:13:59 -0800291 switch p {
292 case "system":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800293 handleSubPartition("system", s.partitionProps.System_partition)
294 // TODO: add system_other to deps after it can be generated
295 //getFsInfo("system_other", s.partitionProps.System_other_partition, &subImageInfo.System_other)
mrziwang79730d42024-12-02 22:13:59 -0800296 case "system_dlkm":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800297 handleSubPartition("system_dlkm", s.partitionProps.System_dlkm_partition)
mrziwang79730d42024-12-02 22:13:59 -0800298 case "system_ext":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800299 handleSubPartition("system_ext", s.partitionProps.System_ext_partition)
mrziwang79730d42024-12-02 22:13:59 -0800300 case "product":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800301 handleSubPartition("product", s.partitionProps.Product_partition)
mrziwang79730d42024-12-02 22:13:59 -0800302 case "vendor":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800303 handleSubPartition("vendor", s.partitionProps.Vendor_partition)
mrziwang79730d42024-12-02 22:13:59 -0800304 case "vendor_dlkm":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800305 handleSubPartition("vendor_dlkm", s.partitionProps.Vendor_dlkm_partition)
mrziwang79730d42024-12-02 22:13:59 -0800306 case "odm":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800307 handleSubPartition("odm", s.partitionProps.Odm_partition)
mrziwang79730d42024-12-02 22:13:59 -0800308 case "odm_dlkm":
Cole Faust2bdc5e52025-01-10 10:29:36 -0800309 handleSubPartition("odm_dlkm", s.partitionProps.Odm_dlkm_partition)
mrziwang79730d42024-12-02 22:13:59 -0800310 default:
Cole Faust2bdc5e52025-01-10 10:29:36 -0800311 ctx.ModuleErrorf("partition %q is not a super image supported partition", p)
mrziwang79730d42024-12-02 22:13:59 -0800312 }
313 }
314
Cole Faust74ee4e02025-01-16 14:55:35 -0800315 if s.properties.System_other_partition != nil {
316 if !slices.Contains(partitionList, "system") {
317 ctx.PropertyErrorf("system_other_partition", "Must have a system partition to use a system_other partition")
318 }
319 systemOther := ctx.GetDirectDepProxyWithTag(*s.properties.System_other_partition, systemOtherDepTag)
320 systemOtherFiles := android.OutputFilesForModule(ctx, systemOther, "")
321 if len(systemOtherFiles) != 1 {
322 ctx.PropertyErrorf("system_other_partition", "Expected 1 output file from module %q", *&s.properties.System_other_partition)
323 } else {
324 addStr("system_other_image", systemOtherFiles[0].String())
325 deps = append(deps, systemOtherFiles[0])
326 }
327 }
328
Cole Faust2bdc5e52025-01-10 10:29:36 -0800329 // Delay the error message until execution time because on aosp-main-future-without-vendor,
330 // BUILDING_VENDOR_IMAGE is false so we don't get the vendor image, but it's still listed in
331 // BOARD_GOOGLE_DYNAMIC_PARTITIONS_PARTITION_LIST.
332 missingPartitionErrorMessageFile := android.PathForModuleOut(ctx, "missing_partition_error.txt")
333 if missingPartitionErrorMessage != "" {
334 ctx.Build(pctx, android.BuildParams{
335 Rule: android.ErrorRule,
336 Output: missingPartitionErrorMessageFile,
337 Args: map[string]string{
338 "error": missingPartitionErrorMessage,
339 },
340 })
341 } else {
342 ctx.Build(pctx, android.BuildParams{
343 Rule: android.Touch,
344 Output: missingPartitionErrorMessageFile,
345 })
Naresh Kumar Podishetty (xWF)4173c5b2025-01-09 20:20:32 -0800346 }
347
mrziwang79730d42024-12-02 22:13:59 -0800348 miscInfo := android.PathForModuleOut(ctx, "misc_info.txt")
Cole Faust2bdc5e52025-01-10 10:29:36 -0800349 android.WriteFileRule(ctx, miscInfo, miscInfoString.String(), missingPartitionErrorMessageFile)
350 return miscInfo, deps, subImageInfo
mrziwang79730d42024-12-02 22:13:59 -0800351}