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