Jiyong Park | 972e06c | 2021-03-15 23:32:49 +0900 | [diff] [blame] | 1 | // Copyright (C) 2021 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 | |
| 15 | package filesystem |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strconv" |
| 20 | |
| 21 | "github.com/google/blueprint" |
| 22 | "github.com/google/blueprint/proptools" |
| 23 | |
| 24 | "android/soong/android" |
| 25 | ) |
| 26 | |
| 27 | func init() { |
| 28 | android.RegisterModuleType("vbmeta", vbmetaFactory) |
| 29 | } |
| 30 | |
| 31 | type vbmeta struct { |
| 32 | android.ModuleBase |
| 33 | |
| 34 | properties vbmetaProperties |
| 35 | |
| 36 | output android.OutputPath |
| 37 | installDir android.InstallPath |
| 38 | } |
| 39 | |
| 40 | type vbmetaProperties struct { |
| 41 | // Name of the partition stored in vbmeta desc. Defaults to the name of this module. |
| 42 | Partition_name *string |
| 43 | |
| 44 | // Set the name of the output. Defaults to <module_name>.img. |
| 45 | Stem *string |
| 46 | |
| 47 | // Path to the private key that avbtool will use to sign this vbmeta image. |
| 48 | Private_key *string `android:"path"` |
| 49 | |
| 50 | // Algorithm that avbtool will use to sign this vbmeta image. Default is SHA256_RSA4096. |
| 51 | Algorithm *string |
| 52 | |
| 53 | // File whose content will provide the rollback index. If unspecified, the rollback index |
| 54 | // is from PLATFORM_SECURITY_PATCH |
| 55 | Rollback_index_file *string `android:"path"` |
| 56 | |
| 57 | // Rollback index location of this vbmeta image. Must be 0, 1, 2, etc. Default is 0. |
| 58 | Rollback_index_location *int64 |
| 59 | |
| 60 | // List of filesystem modules that this vbmeta has descriptors for. The filesystem modules |
| 61 | // have to be signed (use_avb: true). |
| 62 | Partitions []string |
| 63 | |
| 64 | // List of chained partitions that this vbmeta deletages the verification. |
| 65 | Chained_partitions []chainedPartitionProperties |
| 66 | } |
| 67 | |
| 68 | type chainedPartitionProperties struct { |
| 69 | // Name of the chained partition |
| 70 | Name *string |
| 71 | |
| 72 | // Rollback index location of the chained partition. Must be 0, 1, 2, etc. Default is the |
| 73 | // index of this partition in the list + 1. |
| 74 | Rollback_index_location *int64 |
| 75 | |
| 76 | // Path to the public key that the chained partition is signed with. If this is specified, |
| 77 | // private_key is ignored. |
| 78 | Public_key *string `android:"path"` |
| 79 | |
| 80 | // Path to the private key that the chained partition is signed with. If this is specified, |
| 81 | // and public_key is not specified, a public key is extracted from this private key and |
| 82 | // the extracted public key is embedded in the vbmeta image. |
| 83 | Private_key *string `android:"path"` |
| 84 | } |
| 85 | |
| 86 | // vbmeta is the partition image that has the verification information for other partitions. |
| 87 | func vbmetaFactory() android.Module { |
| 88 | module := &vbmeta{} |
| 89 | module.AddProperties(&module.properties) |
| 90 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst) |
| 91 | return module |
| 92 | } |
| 93 | |
| 94 | type vbmetaDep struct { |
| 95 | blueprint.BaseDependencyTag |
| 96 | kind string |
| 97 | } |
| 98 | |
| 99 | var vbmetaPartitionDep = vbmetaDep{kind: "partition"} |
| 100 | |
| 101 | func (v *vbmeta) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 102 | ctx.AddDependency(ctx.Module(), vbmetaPartitionDep, v.properties.Partitions...) |
| 103 | } |
| 104 | |
| 105 | func (v *vbmeta) installFileName() string { |
| 106 | return proptools.StringDefault(v.properties.Stem, v.BaseModuleName()+".img") |
| 107 | } |
| 108 | |
| 109 | func (v *vbmeta) partitionName() string { |
| 110 | return proptools.StringDefault(v.properties.Partition_name, v.BaseModuleName()) |
| 111 | } |
| 112 | |
| 113 | func (v *vbmeta) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 114 | extractedPublicKeys := v.extractPublicKeys(ctx) |
| 115 | |
| 116 | v.output = android.PathForModuleOut(ctx, v.installFileName()).OutputPath |
| 117 | |
| 118 | builder := android.NewRuleBuilder(pctx, ctx) |
| 119 | cmd := builder.Command().BuiltTool("avbtool").Text("make_vbmeta_image") |
| 120 | |
| 121 | key := android.PathForModuleSrc(ctx, proptools.String(v.properties.Private_key)) |
| 122 | cmd.FlagWithInput("--key ", key) |
| 123 | |
| 124 | algorithm := proptools.StringDefault(v.properties.Algorithm, "SHA256_RSA4096") |
| 125 | cmd.FlagWithArg("--algorithm ", algorithm) |
| 126 | |
| 127 | cmd.FlagWithArg("--rollback_index ", v.rollbackIndexCommand(ctx)) |
| 128 | ril := proptools.IntDefault(v.properties.Rollback_index_location, 0) |
| 129 | if ril < 0 { |
| 130 | ctx.PropertyErrorf("rollback_index_location", "must be 0, 1, 2, ...") |
| 131 | return |
| 132 | } |
| 133 | cmd.FlagWithArg("--rollback_index_location ", strconv.Itoa(ril)) |
| 134 | |
| 135 | for _, p := range ctx.GetDirectDepsWithTag(vbmetaPartitionDep) { |
| 136 | f, ok := p.(Filesystem) |
| 137 | if !ok { |
| 138 | ctx.PropertyErrorf("partitions", "%q(type: %s) is not supported", |
| 139 | p.Name(), ctx.OtherModuleType(p)) |
| 140 | continue |
| 141 | } |
| 142 | signedImage := f.SignedOutputPath() |
| 143 | if signedImage == nil { |
| 144 | ctx.PropertyErrorf("partitions", "%q(type: %s) is not signed. Use `use_avb: true`", |
| 145 | p.Name(), ctx.OtherModuleType(p)) |
| 146 | continue |
| 147 | } |
| 148 | cmd.FlagWithInput("--include_descriptors_from_image ", signedImage) |
| 149 | } |
| 150 | |
| 151 | for i, cp := range v.properties.Chained_partitions { |
| 152 | name := proptools.String(cp.Name) |
| 153 | if name == "" { |
| 154 | ctx.PropertyErrorf("chained_partitions", "name must be specified") |
| 155 | continue |
| 156 | } |
| 157 | |
| 158 | ril := proptools.IntDefault(cp.Rollback_index_location, i+1) |
| 159 | if ril < 0 { |
| 160 | ctx.PropertyErrorf("chained_partitions", "must be 0, 1, 2, ...") |
| 161 | continue |
| 162 | } |
| 163 | |
| 164 | var publicKey android.Path |
| 165 | if cp.Public_key != nil { |
| 166 | publicKey = android.PathForModuleSrc(ctx, proptools.String(cp.Public_key)) |
| 167 | } else { |
| 168 | publicKey = extractedPublicKeys[name] |
| 169 | } |
| 170 | cmd.FlagWithArg("--chain_partition ", fmt.Sprintf("%s:%d:%s", name, ril, publicKey.String())) |
| 171 | cmd.Implicit(publicKey) |
| 172 | } |
| 173 | |
| 174 | cmd.FlagWithOutput("--output ", v.output) |
| 175 | builder.Build("vbmeta", fmt.Sprintf("vbmeta %s", ctx.ModuleName())) |
| 176 | |
| 177 | v.installDir = android.PathForModuleInstall(ctx, "etc") |
| 178 | ctx.InstallFile(v.installDir, v.installFileName(), v.output) |
| 179 | } |
| 180 | |
| 181 | // Returns the embedded shell command that prints the rollback index |
| 182 | func (v *vbmeta) rollbackIndexCommand(ctx android.ModuleContext) string { |
| 183 | var cmd string |
| 184 | if v.properties.Rollback_index_file != nil { |
| 185 | f := android.PathForModuleSrc(ctx, proptools.String(v.properties.Rollback_index_file)) |
| 186 | cmd = "cat " + f.String() |
| 187 | } else { |
| 188 | cmd = "date -d 'TZ=\"GMT\" " + ctx.Config().PlatformSecurityPatch() + "' +%s" |
| 189 | } |
| 190 | // Take the first line and remove the newline char |
| 191 | return "$(" + cmd + " | head -1 | tr -d '\n'" + ")" |
| 192 | } |
| 193 | |
| 194 | // Extract public keys from chained_partitions.private_key. The keys are indexed with the partition |
| 195 | // name. |
| 196 | func (v *vbmeta) extractPublicKeys(ctx android.ModuleContext) map[string]android.OutputPath { |
| 197 | result := make(map[string]android.OutputPath) |
| 198 | |
| 199 | builder := android.NewRuleBuilder(pctx, ctx) |
| 200 | for _, cp := range v.properties.Chained_partitions { |
| 201 | if cp.Private_key == nil { |
| 202 | continue |
| 203 | } |
| 204 | |
| 205 | name := proptools.String(cp.Name) |
| 206 | if name == "" { |
| 207 | ctx.PropertyErrorf("chained_partitions", "name must be specified") |
| 208 | continue |
| 209 | } |
| 210 | |
| 211 | if _, ok := result[name]; ok { |
| 212 | ctx.PropertyErrorf("chained_partitions", "name %q is duplicated", name) |
| 213 | continue |
| 214 | } |
| 215 | |
| 216 | privateKeyFile := android.PathForModuleSrc(ctx, proptools.String(cp.Private_key)) |
| 217 | publicKeyFile := android.PathForModuleOut(ctx, name+".avbpubkey").OutputPath |
| 218 | |
| 219 | builder.Command(). |
| 220 | BuiltTool("avbtool"). |
| 221 | Text("extract_public_key"). |
| 222 | FlagWithInput("--key ", privateKeyFile). |
| 223 | FlagWithOutput("--output ", publicKeyFile) |
| 224 | |
| 225 | result[name] = publicKeyFile |
| 226 | } |
| 227 | builder.Build("vbmeta_extract_public_key", fmt.Sprintf("Extract public keys for %s", ctx.ModuleName())) |
| 228 | return result |
| 229 | } |
| 230 | |
| 231 | var _ android.AndroidMkEntriesProvider = (*vbmeta)(nil) |
| 232 | |
| 233 | // Implements android.AndroidMkEntriesProvider |
| 234 | func (v *vbmeta) AndroidMkEntries() []android.AndroidMkEntries { |
| 235 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 236 | Class: "ETC", |
| 237 | OutputFile: android.OptionalPathForPath(v.output), |
| 238 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 239 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 240 | entries.SetString("LOCAL_MODULE_PATH", v.installDir.ToMakePath().String()) |
| 241 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", v.installFileName()) |
| 242 | }, |
| 243 | }, |
| 244 | }} |
| 245 | } |
| 246 | |
| 247 | var _ Filesystem = (*vbmeta)(nil) |
| 248 | |
| 249 | func (v *vbmeta) OutputPath() android.Path { |
| 250 | return v.output |
| 251 | } |
| 252 | |
| 253 | func (v *vbmeta) SignedOutputPath() android.Path { |
| 254 | return v.OutputPath() // vbmeta is always signed |
| 255 | } |
| 256 | |
| 257 | var _ android.OutputFileProducer = (*vbmeta)(nil) |
| 258 | |
| 259 | // Implements android.OutputFileProducer |
| 260 | func (v *vbmeta) OutputFiles(tag string) (android.Paths, error) { |
| 261 | if tag == "" { |
| 262 | return []android.Path{v.output}, nil |
| 263 | } |
| 264 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 265 | } |