1 /* 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> 19 20 #include <android-base/logging.h> 21 #include <sys/ioctl.h> 22 23 #include "fs_mgr_priv.h" 24 #include "fs_mgr_priv_dm_ioctl.h" 25 26 void fs_mgr_verity_ioctl_init(struct dm_ioctl* io, const std::string& name, unsigned flags) { 27 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; 34 if (!name.empty()) { 35 strlcpy(io->name, name.c_str(), sizeof(io->name)); 36 } 37 } 38 39 bool fs_mgr_create_verity_device(struct dm_ioctl* io, const std::string& name, int fd) { 40 fs_mgr_verity_ioctl_init(io, name, 1); 41 if (ioctl(fd, DM_DEV_CREATE, io)) { 42 PERROR << "Error creating device mapping"; 43 return false; 44 } 45 return true; 46 } 47 48 bool fs_mgr_destroy_verity_device(struct dm_ioctl* io, const std::string& name, int fd) { 49 fs_mgr_verity_ioctl_init(io, name, 0); 50 if (ioctl(fd, DM_DEV_REMOVE, io)) { 51 PERROR << "Error removing device mapping"; 52 return false; 53 } 54 return true; 55 } 56 57 bool fs_mgr_get_verity_device_name(struct dm_ioctl* io, const std::string& name, int fd, 58 std::string* out_dev_name) { 59 FS_MGR_CHECK(out_dev_name != nullptr); 60 61 fs_mgr_verity_ioctl_init(io, name, 0); 62 if (ioctl(fd, DM_DEV_STATUS, io)) { 63 PERROR << "Error fetching verity device number"; 64 return false; 65 } 66 67 int dev_num = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00); 68 *out_dev_name = "/dev/block/dm-" + std::to_string(dev_num); 69 70 return true; 71 } 72 73 bool fs_mgr_resume_verity_table(struct dm_ioctl* io, const std::string& name, int fd) { 74 fs_mgr_verity_ioctl_init(io, name, 0); 75 if (ioctl(fd, DM_DEV_SUSPEND, io)) { 76 PERROR << "Error activating verity device"; 77 return false; 78 } 79 return true; 80 } 81