Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 44a3ee2c authored by Sandeep Patil's avatar Sandeep Patil
Browse files

init: remove the existing early_mount code



keeps parts of the code that are still needed for the fs_mgr
+ dt based implementation

b/27805372

Test: boot angler, sailfish without regressions

Change-Id: I1b08f8b7b4f2e67118d328443a5011c0f5ead919
Signed-off-by: default avatarSandeep Patil <sspatil@google.com>
parent 957e4ab0
Loading
Loading
Loading
Loading
+2 −63
Original line number Diff line number Diff line
@@ -874,8 +874,8 @@ static bool inline should_stop_coldboot(coldboot_action_t act)

#define UEVENT_MSG_LEN  2048

template<typename T>
static inline coldboot_action_t handle_device_fd_with(const T& handle_uevent)
static inline coldboot_action_t handle_device_fd_with(
        std::function<coldboot_action_t(uevent* uevent)> handle_uevent)
{
    char msg[UEVENT_MSG_LEN+2];
    int n;
@@ -985,67 +985,6 @@ static coldboot_action_t coldboot(const char *path, coldboot_callback fn)
    return COLDBOOT_CONTINUE;
}

static coldboot_action_t early_uevent_handler(struct uevent *uevent, const char *base, bool is_block)
{
    const char *name;
    char devpath[DEVPATH_LEN];

    if (is_block && strncmp(uevent->subsystem, "block", 5))
        return COLDBOOT_STOP;

    name = parse_device_name(uevent, MAX_DEV_NAME);
    if (!name) {
        LOG(ERROR) << "Failed to parse dev name from uevent: " << uevent->action
                   << " " << uevent->partition_name << " " << uevent->partition_num
                   << " " << uevent->major << ":" << uevent->minor;
        return COLDBOOT_STOP;
    }

    snprintf(devpath, sizeof(devpath), "%s%s", base, name);
    make_dir(base, 0755);

    dev_t dev = makedev(uevent->major, uevent->minor);
    mode_t mode = 0600 | (is_block ? S_IFBLK : S_IFCHR);
    mknod(devpath, mode, dev);

    return COLDBOOT_STOP;
}

void early_create_dev(const std::string& syspath, early_device_type dev_type)
{
    android::base::unique_fd dfd(open(syspath.c_str(), O_RDONLY));
    if (dfd < 0) {
        LOG(ERROR) << "Failed to open " << syspath;
        return;
    }

    android::base::unique_fd fd(openat(dfd, "uevent", O_WRONLY));
    if (fd < 0) {
        LOG(ERROR) << "Failed to open " << syspath << "/uevent";
        return;
    }

    fcntl(device_fd, F_SETFL, O_NONBLOCK);

    write(fd, "add\n", 4);
    handle_device_fd_with(dev_type == EARLY_BLOCK_DEV ?
        [](uevent* uevent) -> coldboot_action_t {
            return early_uevent_handler(uevent, "/dev/block/", true);
        } :
        [](uevent* uevent) -> coldboot_action_t {
            return early_uevent_handler(uevent, "/dev/", false);
        });
}

int early_device_socket_open() {
    device_fd = uevent_open_socket(256*1024, true);
    return device_fd < 0;
}

void early_device_socket_close() {
    close(device_fd);
}

void device_init(const char* path, coldboot_callback fn) {
    sehandle = selinux_android_file_context_handle();
    selinux_status_open(true);
+0 −6
Original line number Diff line number Diff line
@@ -48,12 +48,6 @@ typedef std::function<coldboot_action_t(struct uevent* uevent)> coldboot_callbac
extern coldboot_action_t handle_device_fd(coldboot_callback fn = nullptr);
extern void device_init(const char* path = nullptr, coldboot_callback fn = nullptr);

enum early_device_type { EARLY_BLOCK_DEV, EARLY_CHAR_DEV };

extern int early_device_socket_open();
extern void early_device_socket_close();
extern void early_create_dev(const std::string& syspath, early_device_type dev_type);

extern int add_dev_perms(const char *name, const char *attr,
                         mode_t perm, unsigned int uid,
                         unsigned int gid, unsigned short prefix,
+9 −84
Original line number Diff line number Diff line
@@ -625,102 +625,27 @@ static void set_usb_controller() {
    }
}

/* Returns a new path consisting of base_path and the file name in reference_path. */
static std::string get_path(const std::string& base_path, const std::string& reference_path) {
    std::string::size_type pos = reference_path.rfind('/');
    if (pos == std::string::npos) {
        return base_path + '/' + reference_path;
    } else {
        return base_path + reference_path.substr(pos);
    }
}

/* Imports the fstab info from cmdline. */
static std::string import_cmdline_fstab() {
    std::string prefix, fstab, fstab_full;

    std::string fstabfile;
    import_kernel_cmdline(false,
        [&](const std::string& key, const std::string& value, bool in_qemu __attribute__((__unused__))) {
            if (key == "android.early.prefix") {
                prefix = value;
            } else if (key == "android.early.fstab") {
                fstab = value;
            if (key == "androidboot.fstab") {
                fstabfile = value;
            }
        });
    if (!fstab.empty()) {
        // Convert "mmcblk0p09+/odm+ext4+ro+verify" to "mmcblk0p09 /odm ext4 ro verify"
        std::replace(fstab.begin(), fstab.end(), '+', ' ');
        for (const auto& entry : android::base::Split(fstab, "\n")) {
            fstab_full += prefix + entry + '\n';
        }
    }
    return fstab_full;
    return fstabfile;
}

/* Early mount vendor and ODM partitions. The fstab info is read from kernel cmdline. */
static void early_mount() {
    std::string fstab_string = import_cmdline_fstab();
    if (fstab_string.empty()) {
        LOG(INFO) << "Failed to load vendor fstab from kernel cmdline";
        return;
    }
    FILE *fstab_file = fmemopen((void *)fstab_string.c_str(), fstab_string.length(), "r");
    if (!fstab_file) {
        PLOG(ERROR) << "Failed to open fstab string as FILE";
        return;
    }
    std::unique_ptr<struct fstab, decltype(&fs_mgr_free_fstab)> fstab(fs_mgr_read_fstab_file(fstab_file), fs_mgr_free_fstab);
    fclose(fstab_file);
    if (!fstab) {
        LOG(ERROR) << "Failed to parse fstab string: " << fstab_string;
        return;
    }
    LOG(INFO) << "Loaded vendor fstab from cmdline";

    if (early_device_socket_open()) {
        LOG(ERROR) << "Failed to open device uevent socket";
    // TODO: read fstab entries from device tree, so early mount
    // entries can be specified for A/B devices where system = root
    std::string fstab = import_cmdline_fstab();
    if (fstab.empty()) {
        LOG(INFO) << "Early mount skipped (missing fstab argument)";
        return;
    }

    /* Create /dev/device-mapper for dm-verity */
    early_create_dev("/sys/devices/virtual/misc/device-mapper", EARLY_CHAR_DEV);

    for (int i = 0; i < fstab->num_entries; ++i) {
        struct fstab_rec *rec = &fstab->recs[i];
        std::string mount_point = rec->mount_point;
        std::string syspath = rec->blk_device;

        if (mount_point != "/vendor" && mount_point != "/odm")
            continue;

        /* Create mount target under /dev/block/ from sysfs via uevent */
        LOG(INFO) << "Mounting " << mount_point << " from " << syspath << "...";
        char *devpath = strdup(get_path("/dev/block", syspath).c_str());
        if (!devpath) {
            PLOG(ERROR) << "Failed to strdup dev path in early mount " << syspath;
            continue;
        }
        rec->blk_device = devpath;
        early_create_dev(syspath, EARLY_BLOCK_DEV);

        int rc = fs_mgr_early_setup_verity(rec);
        if (rc == FS_MGR_EARLY_SETUP_VERITY_SUCCESS) {
            /* Mount target is changed to /dev/block/dm-<n>; initiate its creation from sysfs counterpart */
            early_create_dev(get_path("/sys/devices/virtual/block", rec->blk_device), EARLY_BLOCK_DEV);
        } else if (rc == FS_MGR_EARLY_SETUP_VERITY_FAIL) {
            LOG(ERROR) << "Failed to set up dm-verity on " << rec->blk_device;
            continue;
        } else { /* FS_MGR_EARLY_SETUP_VERITY_NO_VERITY */
            LOG(INFO) << "dm-verity disabled on debuggable device; mount directly on " << rec->blk_device;
        }

        mkdir(mount_point.c_str(), 0755);
        rc = mount(rec->blk_device, mount_point.c_str(), rec->fs_type, rec->flags, rec->fs_options);
        if (rc) {
            PLOG(ERROR) << "Failed to mount on " << rec->blk_device;
        }
    }
    early_device_socket_close();
}

int main(int argc, char** argv) {