blob: 11f4bd0134013b5624381c43df0c6e07d9955661 [file] [log] [blame]
Cole Faust3552eb62024-11-06 18:07:26 -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 fsgen
16
17import (
18 "android/soong/android"
19 "android/soong/filesystem"
Cole Faust3552eb62024-11-06 18:07:26 -080020 "strconv"
21
22 "github.com/google/blueprint/proptools"
23)
24
25type vbmetaModuleInfo struct {
26 // The name of the generated vbmeta module
27 moduleName string
28 // The name of the module that avb understands. This is the name passed to --chain_partition,
29 // and also the basename of the output file. (the output file is called partitionName + ".img")
30 partitionName string
31}
32
Jihoon Kang2f0d1932025-01-17 19:22:44 +000033// https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=4849;drc=62e20f0d218f60bae563b4ee742d88cca1fc1901
34var avbPartitions = []string{
35 "boot",
36 "init_boot",
37 "vendor_boot",
38 "vendor_kernel_boot",
39 "system",
40 "vendor",
41 "product",
42 "system_ext",
43 "odm",
44 "vendor_dlkm",
45 "odm_dlkm",
46 "system_dlkm",
47 "dtbo",
48 "pvmfw",
49 "recovery",
50 "vbmeta_system",
51 "vbmeta_vendor",
52}
53
Cole Faust3552eb62024-11-06 18:07:26 -080054// Creates the vbmeta partition and the chained vbmeta partitions. Returns the list of module names
55// that the function created. May return nil if the product isn't using avb.
56//
57// AVB is Android Verified Boot: https://source.android.com/docs/security/features/verifiedboot
58// It works by signing all the partitions, but then also including an extra metadata paritition
59// called vbmeta that depends on all the other signed partitions. This creates a requirement
60// that you update all those partitions and the vbmeta partition together, so in order to relax
61// that requirement products can set up "chained" vbmeta partitions, where a chained partition
62// like vbmeta_system might contain the avb metadata for just a few products. In cuttlefish
63// vbmeta_system contains metadata about product, system, and system_ext. Using chained partitions,
64// that group of partitions can be updated independently from the other signed partitions.
Cole Faust76e8aa12025-01-27 18:21:31 -080065func (f *filesystemCreator) createVbmetaPartitions(ctx android.LoadHookContext, partitions allGeneratedPartitionData) []vbmetaModuleInfo {
Cole Faust3552eb62024-11-06 18:07:26 -080066 partitionVars := ctx.Config().ProductVariables().PartitionVarsForSoongMigrationOnlyDoNotUse
67 // Some products seem to have BuildingVbmetaImage as true even when BoardAvbEnable is false
68 if !partitionVars.BuildingVbmetaImage || !partitionVars.BoardAvbEnable {
69 return nil
70 }
71
72 var result []vbmetaModuleInfo
73
Jihoon Kang2f0d1932025-01-17 19:22:44 +000074 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=4593;drc=62e20f0d218f60bae563b4ee742d88cca1fc1901
75 var internalAvbPartitionsInChainedVbmetaImages []string
76 var chainedPartitionTypes []string
Cole Faust481a7252024-11-13 11:46:22 -080077 for _, chainedName := range android.SortedKeys(partitionVars.ChainedVbmetaPartitions) {
78 props := partitionVars.ChainedVbmetaPartitions[chainedName]
Cole Faust3552eb62024-11-06 18:07:26 -080079 chainedName = "vbmeta_" + chainedName
80 if len(props.Partitions) == 0 {
81 continue
82 }
Jihoon Kang2f0d1932025-01-17 19:22:44 +000083 internalAvbPartitionsInChainedVbmetaImages = append(internalAvbPartitionsInChainedVbmetaImages, props.Partitions...)
Cole Faust3552eb62024-11-06 18:07:26 -080084 if len(props.Key) == 0 {
85 ctx.ModuleErrorf("No key found for chained avb partition %q", chainedName)
86 continue
87 }
88 if len(props.Algorithm) == 0 {
89 ctx.ModuleErrorf("No algorithm found for chained avb partition %q", chainedName)
90 continue
91 }
92 if len(props.RollbackIndex) == 0 {
93 ctx.ModuleErrorf("No rollback index found for chained avb partition %q", chainedName)
94 continue
95 }
96 ril, err := strconv.ParseInt(props.RollbackIndexLocation, 10, 32)
97 if err != nil {
98 ctx.ModuleErrorf("Rollback index location must be an int, got %q", props.RollbackIndexLocation)
99 continue
100 }
101 // The default is to use the PlatformSecurityPatch, and a lot of product config files
102 // just set it to the platform security patch, so detect that and don't set the property
103 // in soong.
104 var rollbackIndex *int64
105 if props.RollbackIndex != ctx.Config().PlatformSecurityPatch() {
106 i, err := strconv.ParseInt(props.RollbackIndex, 10, 32)
107 if err != nil {
108 ctx.ModuleErrorf("Rollback index must be an int, got %q", props.RollbackIndex)
109 continue
110 }
111 rollbackIndex = &i
112 }
113
114 var partitionModules []string
115 for _, partition := range props.Partitions {
Cole Faust76e8aa12025-01-27 18:21:31 -0800116 if modName := partitions.nameForType(partition); modName != "" {
117 partitionModules = append(partitionModules, modName)
Cole Faust3552eb62024-11-06 18:07:26 -0800118 }
Cole Faust3552eb62024-11-06 18:07:26 -0800119 }
120
Jihoon Kang2f0d1932025-01-17 19:22:44 +0000121 name := generatedModuleNameForPartition(ctx.Config(), chainedName)
Cole Faust3552eb62024-11-06 18:07:26 -0800122 ctx.CreateModuleInDirectory(
123 filesystem.VbmetaFactory,
124 ".", // Create in the root directory for now so its easy to get the key
125 &filesystem.VbmetaProperties{
126 Partition_name: proptools.StringPtr(chainedName),
127 Stem: proptools.StringPtr(chainedName + ".img"),
128 Private_key: proptools.StringPtr(props.Key),
129 Algorithm: &props.Algorithm,
130 Rollback_index: rollbackIndex,
131 Rollback_index_location: &ril,
132 Partitions: proptools.NewSimpleConfigurable(partitionModules),
133 }, &struct {
134 Name *string
135 }{
136 Name: &name,
137 },
138 ).HideFromMake()
139
Cole Faust3552eb62024-11-06 18:07:26 -0800140 result = append(result, vbmetaModuleInfo{
141 moduleName: name,
142 partitionName: chainedName,
143 })
Jihoon Kang2f0d1932025-01-17 19:22:44 +0000144
145 chainedPartitionTypes = append(chainedPartitionTypes, chainedName)
Cole Faust3552eb62024-11-06 18:07:26 -0800146 }
147
Jihoon Kang2f0d1932025-01-17 19:22:44 +0000148 vbmetaModuleName := generatedModuleNameForPartition(ctx.Config(), "vbmeta")
Cole Faust3552eb62024-11-06 18:07:26 -0800149
150 var algorithm *string
151 var ri *int64
152 var key *string
153 if len(partitionVars.BoardAvbKeyPath) == 0 {
154 // Match make's defaults: https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=4568;drc=5b55f926830963c02ab1d2d91e46442f04ba3af0
155 key = proptools.StringPtr("external/avb/test/data/testkey_rsa4096.pem")
156 algorithm = proptools.StringPtr("SHA256_RSA4096")
157 } else {
158 key = proptools.StringPtr(partitionVars.BoardAvbKeyPath)
159 algorithm = proptools.StringPtr(partitionVars.BoardAvbAlgorithm)
160 }
161 if len(partitionVars.BoardAvbRollbackIndex) > 0 {
162 parsedRi, err := strconv.ParseInt(partitionVars.BoardAvbRollbackIndex, 10, 32)
163 if err != nil {
164 ctx.ModuleErrorf("Rollback index location must be an int, got %q", partitionVars.BoardAvbRollbackIndex)
165 }
166 ri = &parsedRi
167 }
168
Jihoon Kang2f0d1932025-01-17 19:22:44 +0000169 // --chain_partition argument is only set for partitions that set
170 // `BOARD_AVB_<partition name>_KEY_PATH` value and is not "recovery"
171 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=4823;drc=62e20f0d218f60bae563b4ee742d88cca1fc1901
172 includeAsChainedPartitionInVbmeta := func(partition string) bool {
173 val, ok := partitionVars.PartitionQualifiedVariables[partition]
174 return ok && len(val.BoardAvbKeyPath) > 0 && partition != "recovery"
175 }
176
177 // --include_descriptors_from_image is passed if both conditions are met:
178 // - `BOARD_AVB_<partition name>_KEY_PATH` value is not set
179 // - not included in INTERNAL_AVB_PARTITIONS_IN_CHAINED_VBMETA_IMAGES
180 // for partitions that set INSTALLED_<partition name>IMAGE_TARGET
181 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=4827;drc=62e20f0d218f60bae563b4ee742d88cca1fc1901
182 includeAsIncludedPartitionInVbmeta := func(partition string) bool {
183 if android.InList(partition, internalAvbPartitionsInChainedVbmetaImages) {
184 // Already handled by chained vbmeta partitions
185 return false
186 }
187 partitionQualifiedVars := partitionVars.PartitionQualifiedVariables[partition]
188
189 // The return logic in the switch cases below are identical to
190 // ifdef INSTALLED_<partition name>IMAGE_TARGET
191 switch partition {
192 case "boot":
193 return partitionQualifiedVars.BuildingImage || partitionQualifiedVars.PrebuiltImage || partitionVars.BoardUsesRecoveryAsBoot
194 case "vendor_kernel_boot", "dtbo":
195 return partitionQualifiedVars.PrebuiltImage
196 case "system":
197 return partitionQualifiedVars.BuildingImage
198 case "init_boot", "vendor_boot", "vendor", "product", "system_ext", "odm", "vendor_dlkm", "odm_dlkm", "system_dlkm":
199 return partitionQualifiedVars.BuildingImage || partitionQualifiedVars.PrebuiltImage
200 // TODO: Import BOARD_USES_PVMFWIMAGE
201 // ifeq ($(BOARD_USES_PVMFWIMAGE),true)
202 // case "pvmfw":
203 case "recovery":
204 // ifdef INSTALLED_RECOVERYIMAGE_TARGET
205 return !ctx.DeviceConfig().BoardUsesRecoveryAsBoot() && !ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot()
206 // Technically these partitions are determined based on len(BOARD_AVB_VBMETA_SYSTEM) and
207 // len(BOARD_AVB_VBMETA_VENDOR) but if these are non empty these partitions are
208 // already included in the chained partitions.
209 case "vbmeta_system", "vbmeta_vendor":
210 return false
211 default:
212 return false
213 }
214 }
215
216 var chainedPartitionModules []string
217 var includePartitionModules []string
Cole Faust76e8aa12025-01-27 18:21:31 -0800218 allGeneratedPartitionTypes := append(partitions.types(),
Jihoon Kang2f0d1932025-01-17 19:22:44 +0000219 chainedPartitionTypes...,
220 )
221 if len(f.properties.Boot_image) > 0 {
222 allGeneratedPartitionTypes = append(allGeneratedPartitionTypes, "boot")
223 }
224 if len(f.properties.Init_boot_image) > 0 {
225 allGeneratedPartitionTypes = append(allGeneratedPartitionTypes, "init_boot")
226 }
227 if len(f.properties.Vendor_boot_image) > 0 {
228 allGeneratedPartitionTypes = append(allGeneratedPartitionTypes, "vendor_boot")
229 }
230
231 // https://cs.android.com/android/platform/superproject/main/+/main:build/make/core/Makefile;l=4919;drc=62e20f0d218f60bae563b4ee742d88cca1fc1901
232 for _, partitionType := range android.SortedUniqueStrings(append(avbPartitions, chainedPartitionTypes...)) {
233 if !android.InList(partitionType, allGeneratedPartitionTypes) {
234 // Skip if the partition is not auto generated
Cole Faust3552eb62024-11-06 18:07:26 -0800235 continue
236 }
Cole Faustf1fcf172025-01-30 15:54:41 -0800237 name := partitions.nameForType(partitionType)
238 if name == "" {
239 name = generatedModuleNameForPartition(ctx.Config(), partitionType)
240 }
Jihoon Kang2f0d1932025-01-17 19:22:44 +0000241 if includeAsChainedPartitionInVbmeta(partitionType) {
Cole Faustf1fcf172025-01-30 15:54:41 -0800242 chainedPartitionModules = append(chainedPartitionModules, name)
Jihoon Kang2f0d1932025-01-17 19:22:44 +0000243 } else if includeAsIncludedPartitionInVbmeta(partitionType) {
Cole Faustf1fcf172025-01-30 15:54:41 -0800244 includePartitionModules = append(includePartitionModules, name)
Cole Faust76a6e952024-11-07 16:56:45 -0800245 }
Cole Faust3552eb62024-11-06 18:07:26 -0800246 }
247
248 ctx.CreateModuleInDirectory(
249 filesystem.VbmetaFactory,
250 ".", // Create in the root directory for now so its easy to get the key
251 &filesystem.VbmetaProperties{
252 Stem: proptools.StringPtr("vbmeta.img"),
253 Algorithm: algorithm,
254 Private_key: key,
255 Rollback_index: ri,
Jihoon Kang2f0d1932025-01-17 19:22:44 +0000256 Chained_partitions: chainedPartitionModules,
257 Partitions: proptools.NewSimpleConfigurable(includePartitionModules),
Cole Faust11fda332025-01-14 16:47:19 -0800258 Partition_name: proptools.StringPtr("vbmeta"),
Cole Faust3552eb62024-11-06 18:07:26 -0800259 }, &struct {
260 Name *string
261 }{
262 Name: &vbmetaModuleName,
263 },
264 ).HideFromMake()
265
266 result = append(result, vbmetaModuleInfo{
267 moduleName: vbmetaModuleName,
268 partitionName: "vbmeta",
269 })
270 return result
271}