Alice Wang | 000e3a3 | 2023-01-03 16:11:20 +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 | |
| 20 | "github.com/google/blueprint/proptools" |
| 21 | |
| 22 | "android/soong/android" |
| 23 | ) |
| 24 | |
| 25 | type avbGenVbmetaImage struct { |
| 26 | android.ModuleBase |
| 27 | |
| 28 | properties avbGenVbmetaImageProperties |
| 29 | |
| 30 | output android.OutputPath |
| 31 | installDir android.InstallPath |
| 32 | } |
| 33 | |
| 34 | type avbGenVbmetaImageProperties struct { |
| 35 | // Source file of this image. Can reference a genrule type module with the ":module" syntax. |
| 36 | Src *string `android:"path,arch_variant"` |
| 37 | |
| 38 | // Name of the image partition. Defaults to the name of this module. |
| 39 | Partition_name *string |
| 40 | |
| 41 | // The salt in hex. Required for reproducible builds. |
| 42 | Salt *string |
| 43 | } |
| 44 | |
| 45 | // The avbGenVbmetaImage generates an unsigned VBMeta image output for the given image. |
| 46 | func avbGenVbmetaImageFactory() android.Module { |
| 47 | module := &avbGenVbmetaImage{} |
| 48 | module.AddProperties(&module.properties) |
| 49 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst) |
| 50 | return module |
| 51 | } |
| 52 | |
| 53 | func (a *avbGenVbmetaImage) installFileName() string { |
| 54 | return a.Name() + ".img" |
| 55 | } |
| 56 | |
| 57 | func (a *avbGenVbmetaImage) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 58 | builder := android.NewRuleBuilder(pctx, ctx) |
| 59 | cmd := builder.Command().BuiltTool("avbtool").Text("add_hash_footer") |
| 60 | cmd.Flag("--dynamic_partition_size") |
| 61 | cmd.Flag("--do_not_append_vbmeta_image") |
| 62 | |
| 63 | partition_name := proptools.StringDefault(a.properties.Partition_name, a.Name()) |
| 64 | cmd.FlagWithArg("--partition_name ", partition_name) |
| 65 | |
| 66 | if a.properties.Src == nil { |
| 67 | ctx.PropertyErrorf("src", "missing source file") |
| 68 | return |
| 69 | } |
| 70 | input := android.PathForModuleSrc(ctx, proptools.String(a.properties.Src)) |
| 71 | cmd.FlagWithInput("--image ", input) |
| 72 | |
| 73 | if a.properties.Salt == nil { |
| 74 | ctx.PropertyErrorf("salt", "missing salt value") |
| 75 | return |
| 76 | } |
| 77 | cmd.FlagWithArg("--salt ", proptools.String(a.properties.Salt)) |
| 78 | |
| 79 | a.output = android.PathForModuleOut(ctx, a.installFileName()).OutputPath |
| 80 | cmd.FlagWithOutput("--output_vbmeta_image ", a.output) |
| 81 | builder.Build("avbGenVbmetaImage", fmt.Sprintf("avbGenVbmetaImage %s", ctx.ModuleName())) |
| 82 | } |
| 83 | |
| 84 | var _ android.AndroidMkEntriesProvider = (*avbGenVbmetaImage)(nil) |
| 85 | |
| 86 | // Implements android.AndroidMkEntriesProvider |
| 87 | func (a *avbGenVbmetaImage) AndroidMkEntries() []android.AndroidMkEntries { |
| 88 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 89 | Class: "ETC", |
| 90 | OutputFile: android.OptionalPathForPath(a.output), |
| 91 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 92 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 93 | entries.SetString("LOCAL_MODULE_PATH", a.installDir.String()) |
| 94 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", a.installFileName()) |
| 95 | }, |
| 96 | }, |
| 97 | }} |
| 98 | } |
| 99 | |
| 100 | var _ android.OutputFileProducer = (*avbGenVbmetaImage)(nil) |
| 101 | |
| 102 | // Implements android.OutputFileProducer |
| 103 | func (a *avbGenVbmetaImage) OutputFiles(tag string) (android.Paths, error) { |
| 104 | if tag == "" { |
| 105 | return []android.Path{a.output}, nil |
| 106 | } |
| 107 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 108 | } |