Rename "signature" to "metadata"

Bug: 186396424
Test: atest ApexTestCases MicrodroidHostTestCases
Change-Id: Id401d33edc6c6d4aba2b5982c8ab75820faea793
diff --git a/microdroid/payload/Android.bp b/microdroid/payload/Android.bp
new file mode 100644
index 0000000..5ea6c10
--- /dev/null
+++ b/microdroid/payload/Android.bp
@@ -0,0 +1,75 @@
+package {
+    default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+cc_defaults {
+    name: "microdroid_metadata_default",
+    host_supported: true,
+    srcs: [
+        "metadata.proto",
+        "metadata.cc",
+    ],
+    shared_libs: [
+        "libbase",
+        "liblog",
+    ],
+    export_include_dirs: ["include"],
+}
+
+cc_library_static {
+    name: "lib_microdroid_metadata_proto",
+    proto: {
+        export_proto_headers: true,
+        type: "full",
+    },
+    defaults: ["microdroid_metadata_default"],
+}
+
+cc_library_static {
+    name: "lib_microdroid_metadata_proto_lite",
+    recovery_available: true,
+    proto: {
+        export_proto_headers: true,
+        type: "lite",
+    },
+    defaults: ["microdroid_metadata_default"],
+    apex_available: [
+        "com.android.virt",
+    ],
+}
+
+rust_protobuf {
+    name: "libmicrodroid_metadata_proto_rust",
+    crate_name: "microdroid_metadata",
+    protos: ["metadata.proto"],
+    source_stem: "microdroid_metadata",
+    host_supported: true,
+}
+
+cc_binary {
+    name: "mk_payload",
+    srcs: [
+        "mk_payload.cc",
+    ],
+    shared_libs: [
+        "libbase",
+        "libcuttlefish_fs",
+        "libcuttlefish_utils",
+        "liblog",
+        "libz",
+    ],
+    static_libs: [
+        "lib_microdroid_metadata_proto_lite",
+        "libcdisk_spec",
+        "libext2_uuid",
+        "libimage_aggregator",
+        "libjsoncpp",
+        "libprotobuf-cpp-lite",
+        "libsparse",
+        "libxml2",
+    ],
+    generated_sources: ["apex-info-list"],
+    apex_available: [
+        "com.android.virt",
+    ],
+}
diff --git a/microdroid/payload/README.md b/microdroid/payload/README.md
new file mode 100644
index 0000000..b76eead
--- /dev/null
+++ b/microdroid/payload/README.md
@@ -0,0 +1,85 @@
+# Microdroid Payload
+
+Payload disk is a composite disk image referencing host APEXes and an APK so that microdroid
+mounts/activates APK/APEXes and executes a binary within the APK.
+
+## Partitions
+
+Payload disk has 1 + N(number of APEX/APK payloads) partitions.
+
+The first partition is a "metadata" partition which describes other partitions.
+And APEXes and an APK are following as separate partitions.
+
+For now, the order of partitions are important.
+
+* partition 1: Metadata partition
+* partition 2 ~ n: APEX payloads
+* partition n + 1: APK payload
+
+It's subject to change in the future, though.
+
+### Metadata partition
+
+Metadata partition provides description of the other partitions and the location for VM payload
+configuration.
+
+The partition is a protobuf message prefixed with the size of the message.
+
+| offset | size | description                                                    |
+|--------|------|----------------------------------------------------------------|
+| 0      | 4    | Header. unsigned int32: body length(L) in big endian           |
+| 4      | L    | Body. A protobuf message. [schema](metadata.proto) |
+
+### Payload partitions
+
+Each payload partition presents APEX or APK passed from the host.
+
+At the end of each payload partition the size of the original payload file (APEX or APK) is stored
+in 4-byte big endian.
+
+For example, the following code shows how to get the original size of host apex file
+when the apex is read in microdroid as /dev/block/vdc2,
+
+    int fd = open("/dev/block/vdc2", O_RDONLY | O_BINARY | O_CLOEXEC);
+    uint32_t size;
+    lseek(fd, -sizeof(size), SEEK_END);
+    read(fd, &size, sizeof(size));
+    size = betoh32(size);
+
+## How to Create
+
+### `mk_payload`
+
+`mk_payload` creates a payload composite disk image as described in a JSON which is intentionlly
+similar to the schema of VM payload config.
+
+```
+$ cat payload_config.json
+{
+  "system_apexes": [
+    "com.android.adbd",
+  ],
+  "apexes": [
+    {
+      "name": "com.my.hello",
+      "path": "hello.apex"
+    }
+  ],
+  "apk": {
+    "name": "com.my.world",
+    "path": "/path/to/world.apk"
+  }
+}
+$ adb push payload_config.json hello.apex /data/local/tmp/
+$ adb shell 'cd /data/local/tmp; /apex/com.android.virt/bin/mk_payload payload_config.json payload.img
+$ adb shell ls /data/local/tmp/*.img
+payload.img
+payload-footer.img
+payload-header.img
+payload-metadata.img
+payload.img.0          # fillers
+payload.img.1
+...
+```
+
+In the future, [VirtualizationService](../../virtualizationservice) will handle this.
diff --git a/microdroid/payload/include/microdroid/metadata.h b/microdroid/payload/include/microdroid/metadata.h
new file mode 100644
index 0000000..9e3c907
--- /dev/null
+++ b/microdroid/payload/include/microdroid/metadata.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <android-base/result.h>
+#include <metadata.pb.h>
+
+#include <iostream>
+#include <string>
+
+namespace android {
+namespace microdroid {
+
+base::Result<Metadata> ReadMetadata(const std::string& path);
+
+base::Result<void> WriteMetadata(const Metadata& metadata, std::ostream& out);
+
+} // namespace microdroid
+} // namespace android
diff --git a/microdroid/payload/metadata.cc b/microdroid/payload/metadata.cc
new file mode 100644
index 0000000..07083e9
--- /dev/null
+++ b/microdroid/payload/metadata.cc
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "microdroid/metadata.h"
+
+#include <android-base/endian.h>
+#include <android-base/file.h>
+
+using android::base::ErrnoError;
+using android::base::Error;
+using android::base::Result;
+
+namespace android {
+namespace microdroid {
+
+Result<Metadata> ReadMetadata(const std::string& path) {
+    std::string content;
+    if (!base::ReadFileToString(path, &content)) {
+        return ErrnoError() << "Failed to read " << path;
+    }
+
+    // read length prefix (4-byte, big-endian)
+    uint32_t size;
+    const size_t length_prefix_bytes = sizeof(size);
+    if (content.size() < length_prefix_bytes) {
+        return Error() << "Invalid metadata: size == " << content.size();
+    }
+    size = be32toh(*reinterpret_cast<uint32_t*>(content.data()));
+    if (content.size() < length_prefix_bytes + size) {
+        return Error() << "Invalid metadata: size(" << size << ") mimatches to the content size("
+                       << content.size() - length_prefix_bytes << ")";
+    }
+    content = content.substr(length_prefix_bytes, size);
+
+    // parse content
+    Metadata metadata;
+    if (!metadata.ParseFromString(content)) {
+        return Error() << "Can't parse Metadata from " << path;
+    }
+    return metadata;
+}
+
+Result<void> WriteMetadata(const Metadata& metadata, std::ostream& out) {
+    // prepare content
+    std::string content;
+    if (!metadata.SerializeToString(&content)) {
+        return Error() << "Failed to write protobuf.";
+    }
+
+    // write length prefix (4-byte, big-endian)
+    uint32_t size = htobe32(static_cast<uint32_t>(content.size()));
+    out.write(reinterpret_cast<const char*>(&size), sizeof(size));
+
+    // write content
+    out << content;
+
+    return {};
+}
+
+} // namespace microdroid
+} // namespace android
\ No newline at end of file
diff --git a/microdroid/payload/metadata.proto b/microdroid/payload/metadata.proto
new file mode 100644
index 0000000..0fa0650
--- /dev/null
+++ b/microdroid/payload/metadata.proto
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+syntax = "proto3";
+
+package android.microdroid;
+
+// Metadata is the body of the "metadata" partition
+message Metadata {
+  uint32 version = 1;
+
+  repeated ApexPayload apexes = 2;
+
+  ApkPayload apk = 3;
+
+  string payload_config_path = 4;
+}
+
+message ApexPayload {
+  // Required.
+  // The apex name.
+  string name = 1;
+
+  string partition_name = 2;
+
+  // Optional.
+  // When specified, the public key used to sign the apex should match with it.
+  string publicKey = 3;
+
+  // Optional.
+  // When specified, the root digest of the apex should match with it.
+  string rootDigest = 4;
+}
+
+message ApkPayload {
+  // Required.
+  // The name of APK.
+  string name = 1;
+
+  string payload_partition_name = 2;
+
+  string idsig_partition_name = 3;
+}
diff --git a/microdroid/payload/mk_payload.cc b/microdroid/payload/mk_payload.cc
new file mode 100644
index 0000000..1da71de
--- /dev/null
+++ b/microdroid/payload/mk_payload.cc
@@ -0,0 +1,345 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <fstream>
+#include <iostream>
+#include <optional>
+#include <string>
+#include <vector>
+
+#include <android-base/file.h>
+#include <android-base/result.h>
+#include <com_android_apex.h>
+#include <image_aggregator.h>
+#include <json/json.h>
+
+#include "microdroid/metadata.h"
+
+using android::base::Dirname;
+using android::base::ErrnoError;
+using android::base::Error;
+using android::base::Result;
+using android::base::unique_fd;
+using android::microdroid::ApexPayload;
+using android::microdroid::ApkPayload;
+using android::microdroid::Metadata;
+using android::microdroid::WriteMetadata;
+
+using com::android::apex::ApexInfoList;
+using com::android::apex::readApexInfoList;
+
+using cuttlefish::AlignToPartitionSize;
+using cuttlefish::CreateCompositeDisk;
+using cuttlefish::kLinuxFilesystem;
+using cuttlefish::MultipleImagePartition;
+
+Result<uint32_t> GetFileSize(const std::string& path) {
+    struct stat st;
+    if (lstat(path.c_str(), &st) == -1) {
+        return ErrnoError() << "Can't lstat " << path;
+    }
+    return static_cast<uint32_t>(st.st_size);
+}
+
+std::string ToAbsolute(const std::string& path, const std::string& dirname) {
+    bool is_absolute = !path.empty() && path[0] == '/';
+    if (is_absolute) {
+        return path;
+    } else {
+        return dirname + "/" + path;
+    }
+}
+
+// Returns `append` is appended to the end of filename preserving the extension.
+std::string AppendFileName(const std::string& filename, const std::string& append) {
+    size_t pos = filename.find_last_of('.');
+    if (pos == std::string::npos) {
+        return filename + append;
+    } else {
+        return filename.substr(0, pos) + append + filename.substr(pos);
+    }
+}
+
+struct ApexConfig {
+    std::string name; // the apex name
+    std::string path; // the path to the apex file
+                      // absolute or relative to the config file
+    std::optional<std::string> public_key;
+    std::optional<std::string> root_digest;
+};
+
+struct ApkConfig {
+    std::string name;
+    // TODO(jooyung): find path/idsig with name
+    std::string path;
+};
+
+struct Config {
+    std::string dirname; // config file's direname to resolve relative paths in the config
+
+    // TODO(b/185956069) remove this when VirtualizationService can provide apex paths
+    std::vector<std::string> system_apexes;
+
+    std::vector<ApexConfig> apexes;
+    std::optional<ApkConfig> apk;
+    std::optional<std::string> payload_config_path;
+};
+
+#define DO(expr) \
+    if (auto res = (expr); !res.ok()) return res.error()
+
+Result<void> ParseJson(const Json::Value& value, std::string& s) {
+    if (!value.isString()) {
+        return Error() << "should be a string: " << value;
+    }
+    s = value.asString();
+    return {};
+}
+
+template <typename T>
+Result<void> ParseJson(const Json::Value& value, std::optional<T>& s) {
+    if (value.isNull()) {
+        s.reset();
+        return {};
+    }
+    s.emplace();
+    return ParseJson(value, *s);
+}
+
+template <typename T>
+Result<void> ParseJson(const Json::Value& values, std::vector<T>& parsed) {
+    for (const Json::Value& value : values) {
+        T t;
+        DO(ParseJson(value, t));
+        parsed.push_back(std::move(t));
+    }
+    return {};
+}
+
+Result<void> ParseJson(const Json::Value& value, ApexConfig& apex_config) {
+    DO(ParseJson(value["name"], apex_config.name));
+    DO(ParseJson(value["path"], apex_config.path));
+    DO(ParseJson(value["publicKey"], apex_config.public_key));
+    DO(ParseJson(value["rootDigest"], apex_config.root_digest));
+    return {};
+}
+
+Result<void> ParseJson(const Json::Value& value, ApkConfig& apk_config) {
+    DO(ParseJson(value["name"], apk_config.name));
+    DO(ParseJson(value["path"], apk_config.path));
+    return {};
+}
+
+Result<void> ParseJson(const Json::Value& value, Config& config) {
+    DO(ParseJson(value["system_apexes"], config.system_apexes));
+    DO(ParseJson(value["apexes"], config.apexes));
+    DO(ParseJson(value["apk"], config.apk));
+    DO(ParseJson(value["payload_config_path"], config.payload_config_path));
+    return {};
+}
+
+Result<Config> LoadConfig(const std::string& config_file) {
+    std::ifstream in(config_file);
+    Json::CharReaderBuilder builder;
+    Json::Value root;
+    Json::String errs;
+    if (!parseFromStream(builder, in, &root, &errs)) {
+        return Error() << "bad config: " << errs;
+    }
+
+    Config config;
+    config.dirname = Dirname(config_file);
+    DO(ParseJson(root, config));
+    return config;
+}
+
+#undef DO
+
+Result<void> LoadSystemApexes(Config& config) {
+    static const char* kApexInfoListFile = "/apex/apex-info-list.xml";
+    std::optional<ApexInfoList> apex_info_list = readApexInfoList(kApexInfoListFile);
+    if (!apex_info_list.has_value()) {
+        return Error() << "Failed to read " << kApexInfoListFile;
+    }
+    auto get_apex_path = [&](const std::string& apex_name) -> std::optional<std::string> {
+        for (const auto& apex_info : apex_info_list->getApexInfo()) {
+            if (apex_info.getIsActive() && apex_info.getModuleName() == apex_name) {
+                return apex_info.getModulePath();
+            }
+        }
+        return std::nullopt;
+    };
+    for (const auto& apex_name : config.system_apexes) {
+        const auto& apex_path = get_apex_path(apex_name);
+        if (!apex_path.has_value()) {
+            return Error() << "Can't find the system apex: " << apex_name;
+        }
+        config.apexes.push_back(ApexConfig{
+                .name = apex_name,
+                .path = *apex_path,
+                .public_key = std::nullopt,
+                .root_digest = std::nullopt,
+        });
+    }
+    return {};
+}
+
+Result<void> MakeMetadata(const Config& config, const std::string& filename) {
+    Metadata metadata;
+    metadata.set_version(1);
+
+    for (const auto& apex_config : config.apexes) {
+        auto* apex = metadata.add_apexes();
+
+        // name
+        apex->set_name(apex_config.name);
+
+        // publicKey
+        if (apex_config.public_key.has_value()) {
+            apex->set_publickey(apex_config.public_key.value());
+        }
+
+        // rootDigest
+        if (apex_config.root_digest.has_value()) {
+            apex->set_rootdigest(apex_config.root_digest.value());
+        }
+    }
+
+    if (config.apk.has_value()) {
+        auto* apk = metadata.mutable_apk();
+        apk->set_name(config.apk->name);
+        apk->set_payload_partition_name("microdroid-apk");
+        // TODO(jooyung): set idsig partition as well
+    }
+
+    if (config.payload_config_path.has_value()) {
+        *metadata.mutable_payload_config_path() = config.payload_config_path.value();
+    }
+
+    std::ofstream out(filename);
+    return WriteMetadata(metadata, out);
+}
+
+Result<void> GenerateFiller(const std::string& file_path, const std::string& filler_path) {
+    auto file_size = GetFileSize(file_path);
+    if (!file_size.ok()) {
+        return file_size.error();
+    }
+    auto disk_size = AlignToPartitionSize(*file_size + sizeof(uint32_t));
+
+    unique_fd fd(TEMP_FAILURE_RETRY(open(filler_path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0600)));
+    if (fd.get() == -1) {
+        return ErrnoError() << "open(" << filler_path << ") failed.";
+    }
+    uint32_t size = htobe32(static_cast<uint32_t>(*file_size));
+    if (ftruncate(fd.get(), disk_size - *file_size) == -1) {
+        return ErrnoError() << "ftruncate(" << filler_path << ") failed.";
+    }
+    if (lseek(fd.get(), -sizeof(size), SEEK_END) == -1) {
+        return ErrnoError() << "lseek(" << filler_path << ") failed.";
+    }
+    if (write(fd.get(), &size, sizeof(size)) <= 0) {
+        return ErrnoError() << "write(" << filler_path << ") failed.";
+    }
+    return {};
+}
+
+Result<void> MakePayload(const Config& config, const std::string& metadata_file,
+                         const std::string& output_file) {
+    std::vector<MultipleImagePartition> partitions;
+
+    // put metadata at the first partition
+    partitions.push_back(MultipleImagePartition{
+            .label = "metadata",
+            .image_file_paths = {metadata_file},
+            .type = kLinuxFilesystem,
+            .read_only = true,
+    });
+
+    int filler_count = 0;
+    auto add_partition = [&](auto partition_name, auto file_path) -> Result<void> {
+        std::string filler_path = output_file + "." + std::to_string(filler_count++);
+        if (auto ret = GenerateFiller(file_path, filler_path); !ret.ok()) {
+            return ret.error();
+        }
+        partitions.push_back(MultipleImagePartition{
+                .label = partition_name,
+                .image_file_paths = {file_path, filler_path},
+                .type = kLinuxFilesystem,
+                .read_only = true,
+        });
+        return {};
+    };
+
+    // put apexes at the subsequent partitions with "size" filler
+    for (size_t i = 0; i < config.apexes.size(); i++) {
+        const auto& apex_config = config.apexes[i];
+        std::string apex_path = ToAbsolute(apex_config.path, config.dirname);
+        if (auto ret = add_partition("microdroid-apex-" + std::to_string(i), apex_path);
+            !ret.ok()) {
+            return ret.error();
+        }
+    }
+    // put apk with "size" filler if necessary.
+    // TODO(jooyung): partition name("microdroid-apk") is TBD
+    if (config.apk.has_value()) {
+        std::string apk_path = ToAbsolute(config.apk->path, config.dirname);
+        if (auto ret = add_partition("microdroid-apk", apk_path); !ret.ok()) {
+            return ret.error();
+        }
+    }
+
+    const std::string gpt_header = AppendFileName(output_file, "-header");
+    const std::string gpt_footer = AppendFileName(output_file, "-footer");
+    CreateCompositeDisk(partitions, gpt_header, gpt_footer, output_file);
+    return {};
+}
+
+int main(int argc, char** argv) {
+    if (argc != 3) {
+        std::cerr << "Usage: " << argv[0] << " <config> <output>\n";
+        return 1;
+    }
+
+    auto config = LoadConfig(argv[1]);
+    if (!config.ok()) {
+        std::cerr << config.error() << '\n';
+        return 1;
+    }
+
+    if (const auto res = LoadSystemApexes(*config); !res.ok()) {
+        std::cerr << res.error() << '\n';
+        return 1;
+    }
+
+    const std::string output_file(argv[2]);
+    const std::string metadata_file = AppendFileName(output_file, "-metadata");
+
+    if (const auto res = MakeMetadata(*config, metadata_file); !res.ok()) {
+        std::cerr << res.error() << '\n';
+        return 1;
+    }
+    if (const auto res = MakePayload(*config, metadata_file, output_file); !res.ok()) {
+        std::cerr << res.error() << '\n';
+        return 1;
+    }
+
+    return 0;
+}
\ No newline at end of file