Microdroid activates host APEXes

Host apexes are passed to microdroid via the payload composite image.
The payload composite image can be created by either
mk_payload_signature/mk_cdisk or mk_payload.

For now, microdroid expects two APEXes from the host:
- com.android.adbd
- com.android.sdkext

The exact set of host apexes for microdroid is TBD. The current set is
only for demonstration.

Bug: 181093750
Test: MicrodroidTestCase
Change-Id: I7fe33fe03ac94799a4e109b83286bfb39e44b882
diff --git a/microdroid/Android.bp b/microdroid/Android.bp
index 60965ef..2522679 100644
--- a/microdroid/Android.bp
+++ b/microdroid/Android.bp
@@ -49,7 +49,11 @@
         "logd",
         "run-as",
         "secilc",
-        "adbd",
+
+        // "com.android.adbd" requires these,
+        "libadbd_auth",
+        "libadbd_fs",
+
         "apexd",
         "debuggerd",
         "linker",
@@ -379,3 +383,8 @@
     name: "microdroid_cdisk_env.json",
     src: "microdroid_cdisk_env.json",
 }
+
+prebuilt_etc {
+    name: "microdroid_payload.json",
+    src: "microdroid_payload.json",
+}
diff --git a/microdroid/README.md b/microdroid/README.md
index a4bfb6e..4ce74a5 100644
--- a/microdroid/README.md
+++ b/microdroid/README.md
@@ -36,7 +36,8 @@
 $ adb shell 'dd if=/dev/zero of=/data/local/tmp/misc.img bs=4k count=256'
 $ adb shell 'cd /data/local/tmp; /apex/com.android.virt/bin/mk_cdisk /apex/com.android.virt/etc/microdroid_cdisk.json os_composite.img'
 $ adb shell 'cd /data/local/tmp; /apex/com.android.virt/bin/mk_cdisk /apex/com.android.virt/etc/microdroid_cdisk_env.json env_composite.img'
-$ adb shell 'cd /data/local/tmp; /apex/com.android.virt/bin/crosvm run --cid=5 --disable-sandbox --bios=bootloader --serial=type=stdout --disk=os_composite.img --disk=env_composite.img'
+$ adb shell 'cd /data/local/tmp; /apex/com.android.virt/bin/mk_payload /apex/com.android.virt/etc/microdroid_payload.json payload.img'
+$ adb shell 'cd /data/local/tmp; /apex/com.android.virt/bin/crosvm run --cid=5 --disable-sandbox --bios=bootloader --serial=type=stdout --disk=os_composite.img --disk=env_composite.img --disk=payload.img'
 ```
 
 The CID in `--cid` parameter can be anything greater than 2 (`VMADDR_CID_HOST`).
diff --git a/microdroid/init.rc b/microdroid/init.rc
index 56b0065..61f9ae5 100644
--- a/microdroid/init.rc
+++ b/microdroid/init.rc
@@ -105,6 +105,8 @@
 
     perform_apex_config
 
+    exec_start derive_sdk
+
     start adbd
 
 service ueventd /system/bin/ueventd
@@ -122,13 +124,6 @@
     seclabel u:r:shell:s0
     setenv HOSTNAME console
 
-# TODO(b/181093750): remove these after adding apex support
-service adbd /system/bin/adbd --root_seclabel=u:r:su:s0
-    class core
-    socket adbd seqpacket 660 system system
-    disabled
-    seclabel u:r:adbd:s0
-
 on fs
     write /dev/event-log-tags "# content owned by logd
 "
diff --git a/microdroid/microdroid_payload.json b/microdroid/microdroid_payload.json
new file mode 100644
index 0000000..c5c49d0
--- /dev/null
+++ b/microdroid/microdroid_payload.json
@@ -0,0 +1,6 @@
+{
+  "system_apexes": [
+    "com.android.adbd",
+    "com.android.sdkext"
+  ]
+}
\ No newline at end of file
diff --git a/microdroid/signature/Android.bp b/microdroid/signature/Android.bp
index 91d732b..b993e4e 100644
--- a/microdroid/signature/Android.bp
+++ b/microdroid/signature/Android.bp
@@ -56,3 +56,31 @@
         "com.android.virt",
     ],
 }
+
+cc_binary {
+    name: "mk_payload",
+    srcs: [
+        "mk_payload.cc",
+    ],
+    shared_libs: [
+        "libbase",
+        "libcuttlefish_fs",
+        "libcuttlefish_utils",
+        "liblog",
+        "libz",
+    ],
+    static_libs: [
+        "lib_microdroid_signature_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/signature/README.md b/microdroid/signature/README.md
index 4f434ee..0a39c31 100644
--- a/microdroid/signature/README.md
+++ b/microdroid/signature/README.md
@@ -16,6 +16,8 @@
 
 ## How to Create
 
+### `mk_microdroid_signature` and `mk_cdisk`
+
 For testing purpose, use `mk_microdroid_signature` to create a Microdroid Signature.
 
 ```
@@ -53,4 +55,31 @@
 $ adb shell 'cd /data/local/tmp; /apex/com.android.virt/bin/crosvm .... --disk=payload.img'
 ```
 
+### `mk_payload`
+
+`mk_payload` combines these two steps into a single step. Additionally, `mk_payload` can search system APEXes as well.
+This will generate the output composite image as well as three more component images. (See below)
+
+```
+$ cat payload_config.json
+{
+  "system_apexes": [
+    "com.android.adbd",
+  ],
+  "apexes": [
+    {
+      "name": "com.my.hello",
+      "path": "hello.apex"
+    }
+  ]
+}
+$ 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-footer.img
+payload-header.img
+payload-signature.img
+payload.img
+```
+
 In the future, [VirtManager](../../virtmanager) will handle these stuffs.
\ No newline at end of file
diff --git a/microdroid/signature/mk_payload.cc b/microdroid/signature/mk_payload.cc
new file mode 100644
index 0000000..8046b5e
--- /dev/null
+++ b/microdroid/signature/mk_payload.cc
@@ -0,0 +1,273 @@
+/*
+ * 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/signature.h"
+
+using android::base::Dirname;
+using android::base::ErrnoError;
+using android::base::Error;
+using android::base::Result;
+using android::microdroid::ApexSignature;
+using android::microdroid::MicrodroidSignature;
+using android::microdroid::WriteMicrodroidSignature;
+
+using com::android::apex::ApexInfoList;
+using com::android::apex::readApexInfoList;
+
+using cuttlefish::CreateCompositeDisk;
+using cuttlefish::ImagePartition;
+using cuttlefish::kLinuxFilesystem;
+
+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 Config {
+    std::string dirname; // config file's direname to resolve relative paths in the config
+
+    std::vector<std::string> system_apexes;
+    std::vector<ApexConfig> apexes;
+};
+
+#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 {};
+}
+
+Result<void> ParseJson(const Json::Value& value, std::optional<std::string>& s) {
+    if (value.isNull()) {
+        s.reset();
+        return {};
+    }
+    s.emplace();
+    return ParseJson(value, *s);
+}
+
+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 {};
+}
+
+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, Config& config) {
+    DO(ParseJson(value["system_apexes"], config.system_apexes));
+    DO(ParseJson(value["apexes"], config.apexes));
+    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> MakeSignature(const Config& config, const std::string& filename) {
+    MicrodroidSignature signature;
+    signature.set_version(1);
+
+    for (const auto& apex_config : config.apexes) {
+        ApexSignature* apex_signature = signature.add_apexes();
+
+        // name
+        apex_signature->set_name(apex_config.name);
+
+        // size
+        auto file_size = GetFileSize(ToAbsolute(apex_config.path, config.dirname));
+        if (!file_size.ok()) {
+            return Error() << "I/O error: " << file_size.error();
+        }
+        apex_signature->set_size(file_size.value());
+
+        // publicKey
+        if (apex_config.public_key.has_value()) {
+            apex_signature->set_publickey(apex_config.public_key.value());
+        }
+
+        // rootDigest
+        if (apex_config.root_digest.has_value()) {
+            apex_signature->set_rootdigest(apex_config.root_digest.value());
+        }
+    }
+
+    std::ofstream out(filename);
+    return WriteMicrodroidSignature(signature, out);
+}
+
+Result<void> MakePayload(const Config& config, const std::string& signature_file,
+                         const std::string& output_file) {
+    std::vector<ImagePartition> partitions;
+
+    // put signature at the first partition
+    partitions.push_back(ImagePartition{
+            .label = "signature",
+            .image_file_path = signature_file,
+            .type = kLinuxFilesystem,
+            .read_only = true,
+    });
+
+    // put apexes at the subsequent partitions
+    for (size_t i = 0; i < config.apexes.size(); i++) {
+        const auto& apex_config = config.apexes[i];
+        partitions.push_back(ImagePartition{
+                .label = "payload_apex_" + std::to_string(i),
+                .image_file_path = apex_config.path,
+                .type = kLinuxFilesystem,
+                .read_only = true,
+        });
+    }
+
+    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 signature_file = AppendFileName(output_file, "-signature");
+
+    if (const auto res = MakeSignature(*config, signature_file); !res.ok()) {
+        std::cerr << res.error() << '\n';
+        return 1;
+    }
+    if (const auto res = MakePayload(*config, signature_file, output_file); !res.ok()) {
+        std::cerr << res.error() << '\n';
+        return 1;
+    }
+
+    return 0;
+}
\ No newline at end of file