blob: 4cbd5a86a2d4ab9622b78cc52965d8a3c03b0bef [file] [log] [blame]
bowgotsai3de625d2016-11-11 21:05:44 +08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <string.h>
bowgotsai47aa2a72017-01-09 18:15:41 +080019
20#include <android-base/logging.h>
bowgotsai3de625d2016-11-11 21:05:44 +080021#include <sys/ioctl.h>
22
23#include "fs_mgr_priv.h"
24#include "fs_mgr_priv_dm_ioctl.h"
25
Bowgo Tsai4caf4c02017-02-16 21:35:09 +080026void fs_mgr_verity_ioctl_init(struct dm_ioctl* io, const std::string& name, unsigned flags) {
bowgotsai3de625d2016-11-11 21:05:44 +080027 memset(io, 0, DM_BUF_SIZE);
28 io->data_size = DM_BUF_SIZE;
29 io->data_start = sizeof(struct dm_ioctl);
30 io->version[0] = 4;
31 io->version[1] = 0;
32 io->version[2] = 0;
33 io->flags = flags | DM_READONLY_FLAG;
bowgotsai47aa2a72017-01-09 18:15:41 +080034 if (!name.empty()) {
35 strlcpy(io->name, name.c_str(), sizeof(io->name));
bowgotsai3de625d2016-11-11 21:05:44 +080036 }
37}
38
Bowgo Tsai4caf4c02017-02-16 21:35:09 +080039bool fs_mgr_create_verity_device(struct dm_ioctl* io, const std::string& name, int fd) {
bowgotsai3de625d2016-11-11 21:05:44 +080040 fs_mgr_verity_ioctl_init(io, name, 1);
41 if (ioctl(fd, DM_DEV_CREATE, io)) {
bowgotsai47878de2017-01-23 14:04:34 +080042 PERROR << "Error creating device mapping";
bowgotsai47aa2a72017-01-09 18:15:41 +080043 return false;
bowgotsai3de625d2016-11-11 21:05:44 +080044 }
bowgotsai47aa2a72017-01-09 18:15:41 +080045 return true;
bowgotsai3de625d2016-11-11 21:05:44 +080046}
47
Bowgo Tsai4caf4c02017-02-16 21:35:09 +080048bool fs_mgr_destroy_verity_device(struct dm_ioctl* io, const std::string& name, int fd) {
bowgotsai3de625d2016-11-11 21:05:44 +080049 fs_mgr_verity_ioctl_init(io, name, 0);
50 if (ioctl(fd, DM_DEV_REMOVE, io)) {
bowgotsai47878de2017-01-23 14:04:34 +080051 PERROR << "Error removing device mapping";
bowgotsai47aa2a72017-01-09 18:15:41 +080052 return false;
bowgotsai3de625d2016-11-11 21:05:44 +080053 }
bowgotsai47aa2a72017-01-09 18:15:41 +080054 return true;
bowgotsai3de625d2016-11-11 21:05:44 +080055}
56
Bowgo Tsai4caf4c02017-02-16 21:35:09 +080057bool fs_mgr_get_verity_device_name(struct dm_ioctl* io, const std::string& name, int fd,
58 std::string* out_dev_name) {
bowgotsaib51722b2017-01-11 22:21:38 +080059 FS_MGR_CHECK(out_dev_name != nullptr);
bowgotsai47aa2a72017-01-09 18:15:41 +080060
bowgotsai3de625d2016-11-11 21:05:44 +080061 fs_mgr_verity_ioctl_init(io, name, 0);
62 if (ioctl(fd, DM_DEV_STATUS, io)) {
bowgotsai47878de2017-01-23 14:04:34 +080063 PERROR << "Error fetching verity device number";
bowgotsai47aa2a72017-01-09 18:15:41 +080064 return false;
bowgotsai3de625d2016-11-11 21:05:44 +080065 }
bowgotsai47aa2a72017-01-09 18:15:41 +080066
bowgotsai3de625d2016-11-11 21:05:44 +080067 int dev_num = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
bowgotsai47aa2a72017-01-09 18:15:41 +080068 *out_dev_name = "/dev/block/dm-" + std::to_string(dev_num);
69
70 return true;
bowgotsai3de625d2016-11-11 21:05:44 +080071}
72
Bowgo Tsai4caf4c02017-02-16 21:35:09 +080073bool fs_mgr_resume_verity_table(struct dm_ioctl* io, const std::string& name, int fd) {
bowgotsai3de625d2016-11-11 21:05:44 +080074 fs_mgr_verity_ioctl_init(io, name, 0);
75 if (ioctl(fd, DM_DEV_SUSPEND, io)) {
bowgotsai47878de2017-01-23 14:04:34 +080076 PERROR << "Error activating verity device";
bowgotsai47aa2a72017-01-09 18:15:41 +080077 return false;
bowgotsai3de625d2016-11-11 21:05:44 +080078 }
bowgotsai47aa2a72017-01-09 18:15:41 +080079 return true;
bowgotsai3de625d2016-11-11 21:05:44 +080080}