Andrew Scull | ebd61e9 | 2022-06-09 15:53:36 +0000 | [diff] [blame] | 1 | // Copyright (C) 2022 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/proptools" |
| 22 | |
| 23 | "android/soong/android" |
| 24 | ) |
| 25 | |
| 26 | func init() { |
| 27 | android.RegisterModuleType("avb_add_hash_footer", avbAddHashFooterFactory) |
| 28 | } |
| 29 | |
| 30 | type avbAddHashFooter struct { |
| 31 | android.ModuleBase |
| 32 | |
| 33 | properties avbAddHashFooterProperties |
| 34 | |
| 35 | output android.OutputPath |
| 36 | installDir android.InstallPath |
| 37 | } |
| 38 | |
| 39 | type avbAddHashFooterProperties struct { |
| 40 | // Source file of this image. Can reference a genrule type module with the ":module" syntax. |
| 41 | Src *string `android:"path,arch_variant"` |
| 42 | |
| 43 | // Set the name of the output. Defaults to <module_name>.img. |
| 44 | Filename *string |
| 45 | |
| 46 | // Name of the image partition. Defaults to the name of this module. |
| 47 | Partition_name *string |
| 48 | |
| 49 | // Size of the partition. Defaults to dynamically calculating the size. |
| 50 | Partition_size *int64 |
| 51 | |
| 52 | // Path to the private key that avbtool will use to sign this image. |
| 53 | Private_key *string `android:"path"` |
| 54 | |
| 55 | // Algorithm that avbtool will use to sign this image. Default is SHA256_RSA4096. |
| 56 | Algorithm *string |
| 57 | |
| 58 | // The salt in hex. Required for reproducible builds. |
| 59 | Salt *string |
| 60 | } |
| 61 | |
| 62 | // The AVB footer adds verification information to the image. |
| 63 | func avbAddHashFooterFactory() android.Module { |
| 64 | module := &avbAddHashFooter{} |
| 65 | module.AddProperties(&module.properties) |
| 66 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst) |
| 67 | return module |
| 68 | } |
| 69 | |
| 70 | func (a *avbAddHashFooter) installFileName() string { |
| 71 | return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".img") |
| 72 | } |
| 73 | |
| 74 | func (a *avbAddHashFooter) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 75 | builder := android.NewRuleBuilder(pctx, ctx) |
| 76 | |
| 77 | if a.properties.Src == nil { |
| 78 | ctx.PropertyErrorf("src", "missing source file") |
| 79 | return |
| 80 | } |
| 81 | input := android.PathForModuleSrc(ctx, proptools.String(a.properties.Src)) |
| 82 | a.output = android.PathForModuleOut(ctx, a.installFileName()).OutputPath |
| 83 | builder.Command().Text("cp").Input(input).Output(a.output) |
| 84 | |
| 85 | cmd := builder.Command().BuiltTool("avbtool").Text("add_hash_footer") |
| 86 | |
| 87 | partition_name := proptools.StringDefault(a.properties.Partition_name, a.BaseModuleName()) |
| 88 | cmd.FlagWithArg("--partition_name ", partition_name) |
| 89 | |
| 90 | if a.properties.Partition_size == nil { |
| 91 | cmd.Flag("--dynamic_partition_size") |
| 92 | } else { |
| 93 | partition_size := proptools.Int(a.properties.Partition_size) |
| 94 | cmd.FlagWithArg("--partition_size ", strconv.Itoa(partition_size)) |
| 95 | } |
| 96 | |
| 97 | key := android.PathForModuleSrc(ctx, proptools.String(a.properties.Private_key)) |
| 98 | cmd.FlagWithInput("--key ", key) |
| 99 | |
| 100 | algorithm := proptools.StringDefault(a.properties.Algorithm, "SHA256_RSA4096") |
| 101 | cmd.FlagWithArg("--algorithm ", algorithm) |
| 102 | |
| 103 | if a.properties.Salt == nil { |
| 104 | ctx.PropertyErrorf("salt", "missing salt value") |
| 105 | return |
| 106 | } |
| 107 | cmd.FlagWithArg("--salt ", proptools.String(a.properties.Salt)) |
| 108 | |
| 109 | cmd.FlagWithOutput("--image ", a.output) |
| 110 | |
| 111 | builder.Build("avbAddHashFooter", fmt.Sprintf("avbAddHashFooter %s", ctx.ModuleName())) |
| 112 | |
| 113 | a.installDir = android.PathForModuleInstall(ctx, "etc") |
| 114 | ctx.InstallFile(a.installDir, a.installFileName(), a.output) |
| 115 | } |
| 116 | |
| 117 | var _ android.AndroidMkEntriesProvider = (*avbAddHashFooter)(nil) |
| 118 | |
| 119 | // Implements android.AndroidMkEntriesProvider |
| 120 | func (a *avbAddHashFooter) AndroidMkEntries() []android.AndroidMkEntries { |
| 121 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 122 | Class: "ETC", |
| 123 | OutputFile: android.OptionalPathForPath(a.output), |
| 124 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 125 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 126 | entries.SetString("LOCAL_MODULE_PATH", a.installDir.String()) |
| 127 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", a.installFileName()) |
| 128 | }, |
| 129 | }, |
| 130 | }} |
| 131 | } |
| 132 | |
| 133 | var _ Filesystem = (*avbAddHashFooter)(nil) |
| 134 | |
| 135 | func (a *avbAddHashFooter) OutputPath() android.Path { |
| 136 | return a.output |
| 137 | } |
| 138 | |
| 139 | func (a *avbAddHashFooter) SignedOutputPath() android.Path { |
| 140 | return a.OutputPath() // always signed |
| 141 | } |
| 142 | |
| 143 | // TODO(b/185115783): remove when not needed as input to a prebuilt_etc rule |
| 144 | var _ android.SourceFileProducer = (*avbAddHashFooter)(nil) |
| 145 | |
| 146 | // Implements android.SourceFileProducer |
| 147 | func (a *avbAddHashFooter) Srcs() android.Paths { |
| 148 | return append(android.Paths{}, a.output) |
| 149 | } |