Justin Yun | 635e788 | 2024-07-04 14:50:57 +0900 | [diff] [blame] | 1 | // Copyright 2024 Google Inc. All rights reserved. |
| 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 etc |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | |
| 20 | "github.com/google/blueprint/proptools" |
| 21 | ) |
| 22 | |
| 23 | func init() { |
| 24 | RegisterOtacertsZipBuildComponents(android.InitRegistrationContext) |
| 25 | } |
| 26 | |
| 27 | func RegisterOtacertsZipBuildComponents(ctx android.RegistrationContext) { |
| 28 | ctx.RegisterModuleType("otacerts_zip", otacertsZipFactory) |
| 29 | } |
| 30 | |
| 31 | type otacertsZipProperties struct { |
| 32 | // Make this module available when building for recovery. |
| 33 | // Only the recovery partition is available. |
| 34 | Recovery_available *bool |
| 35 | |
| 36 | // Optional subdirectory under which the zip file is installed into. |
| 37 | Relative_install_path *string |
| 38 | |
| 39 | // Optional name for the installed file. If unspecified, otacerts.zip is used. |
| 40 | Filename *string |
| 41 | } |
| 42 | |
| 43 | type otacertsZipModule struct { |
| 44 | android.ModuleBase |
| 45 | |
| 46 | properties otacertsZipProperties |
Cole Faust | 4e9f592 | 2024-11-13 16:09:23 -0800 | [diff] [blame] | 47 | outputPath android.Path |
Justin Yun | 635e788 | 2024-07-04 14:50:57 +0900 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // otacerts_zip collects key files defined in PRODUCT_DEFAULT_DEV_CERTIFICATE |
| 51 | // and PRODUCT_EXTRA_OTA_KEYS for system or PRODUCT_EXTRA_RECOVERY_KEYS for |
| 52 | // recovery image. The output file (otacerts.zip by default) is installed into |
| 53 | // the relative_install_path directory under the etc directory of the target |
| 54 | // partition. |
| 55 | func otacertsZipFactory() android.Module { |
| 56 | module := &otacertsZipModule{} |
| 57 | module.AddProperties(&module.properties) |
| 58 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 59 | return module |
| 60 | } |
| 61 | |
| 62 | var _ android.ImageInterface = (*otacertsZipModule)(nil) |
| 63 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 64 | func (m *otacertsZipModule) ImageMutatorBegin(ctx android.ImageInterfaceContext) {} |
Justin Yun | 635e788 | 2024-07-04 14:50:57 +0900 | [diff] [blame] | 65 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 66 | func (m *otacertsZipModule) VendorVariantNeeded(ctx android.ImageInterfaceContext) bool { |
Justin Yun | 635e788 | 2024-07-04 14:50:57 +0900 | [diff] [blame] | 67 | return false |
| 68 | } |
| 69 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 70 | func (m *otacertsZipModule) ProductVariantNeeded(ctx android.ImageInterfaceContext) bool { |
Justin Yun | 635e788 | 2024-07-04 14:50:57 +0900 | [diff] [blame] | 71 | return false |
| 72 | } |
| 73 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 74 | func (m *otacertsZipModule) CoreVariantNeeded(ctx android.ImageInterfaceContext) bool { |
Justin Yun | 635e788 | 2024-07-04 14:50:57 +0900 | [diff] [blame] | 75 | return !m.ModuleBase.InstallInRecovery() |
| 76 | } |
| 77 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 78 | func (m *otacertsZipModule) RamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool { |
Justin Yun | 635e788 | 2024-07-04 14:50:57 +0900 | [diff] [blame] | 79 | return false |
| 80 | } |
| 81 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 82 | func (m *otacertsZipModule) VendorRamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool { |
Justin Yun | 635e788 | 2024-07-04 14:50:57 +0900 | [diff] [blame] | 83 | return false |
| 84 | } |
| 85 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 86 | func (m *otacertsZipModule) DebugRamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool { |
Justin Yun | 635e788 | 2024-07-04 14:50:57 +0900 | [diff] [blame] | 87 | return false |
| 88 | } |
| 89 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 90 | func (m *otacertsZipModule) RecoveryVariantNeeded(ctx android.ImageInterfaceContext) bool { |
Justin Yun | 635e788 | 2024-07-04 14:50:57 +0900 | [diff] [blame] | 91 | return proptools.Bool(m.properties.Recovery_available) || m.ModuleBase.InstallInRecovery() |
| 92 | } |
| 93 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 94 | func (m *otacertsZipModule) ExtraImageVariations(ctx android.ImageInterfaceContext) []string { |
Justin Yun | 635e788 | 2024-07-04 14:50:57 +0900 | [diff] [blame] | 95 | return nil |
| 96 | } |
| 97 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 98 | func (m *otacertsZipModule) SetImageVariation(ctx android.ImageInterfaceContext, variation string) { |
Justin Yun | 635e788 | 2024-07-04 14:50:57 +0900 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | func (m *otacertsZipModule) InRecovery() bool { |
| 102 | return m.ModuleBase.InRecovery() || m.ModuleBase.InstallInRecovery() |
| 103 | } |
| 104 | |
| 105 | func (m *otacertsZipModule) InstallInRecovery() bool { |
| 106 | return m.InRecovery() |
| 107 | } |
| 108 | |
| 109 | func (m *otacertsZipModule) outputFileName() string { |
| 110 | // Use otacerts.zip if not specified. |
| 111 | return proptools.StringDefault(m.properties.Filename, "otacerts.zip") |
| 112 | } |
| 113 | |
| 114 | func (m *otacertsZipModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 115 | // Read .x509.pem file defined in PRODUCT_DEFAULT_DEV_CERTIFICATE or the default test key. |
| 116 | pem, _ := ctx.Config().DefaultAppCertificate(ctx) |
| 117 | // Read .x509.pem files listed in PRODUCT_EXTRA_OTA_KEYS or PRODUCT_EXTRA_RECOVERY_KEYS. |
| 118 | extras := ctx.Config().ExtraOtaKeys(ctx, m.InRecovery()) |
| 119 | srcPaths := append([]android.SourcePath{pem}, extras...) |
Cole Faust | 4e9f592 | 2024-11-13 16:09:23 -0800 | [diff] [blame] | 120 | outputPath := android.PathForModuleOut(ctx, m.outputFileName()) |
Justin Yun | 635e788 | 2024-07-04 14:50:57 +0900 | [diff] [blame] | 121 | |
| 122 | rule := android.NewRuleBuilder(pctx, ctx) |
| 123 | cmd := rule.Command().BuiltTool("soong_zip"). |
Cole Faust | 4e9f592 | 2024-11-13 16:09:23 -0800 | [diff] [blame] | 124 | FlagWithOutput("-o ", outputPath). |
Justin Yun | 635e788 | 2024-07-04 14:50:57 +0900 | [diff] [blame] | 125 | Flag("-j "). |
| 126 | Flag("-symlinks=false ") |
| 127 | for _, src := range srcPaths { |
| 128 | cmd.FlagWithInput("-f ", src) |
| 129 | } |
| 130 | rule.Build(ctx.ModuleName(), "Generating the otacerts zip file") |
| 131 | |
| 132 | installPath := android.PathForModuleInstall(ctx, "etc", proptools.String(m.properties.Relative_install_path)) |
Cole Faust | 4e9f592 | 2024-11-13 16:09:23 -0800 | [diff] [blame] | 133 | ctx.InstallFile(installPath, m.outputFileName(), outputPath) |
| 134 | m.outputPath = outputPath |
Justin Yun | 635e788 | 2024-07-04 14:50:57 +0900 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | func (m *otacertsZipModule) AndroidMkEntries() []android.AndroidMkEntries { |
| 138 | nameSuffix := "" |
| 139 | if m.InRecovery() { |
| 140 | nameSuffix = ".recovery" |
| 141 | } |
| 142 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 143 | Class: "ETC", |
| 144 | SubName: nameSuffix, |
| 145 | OutputFile: android.OptionalPathForPath(m.outputPath), |
| 146 | }} |
| 147 | } |