merothh | 5485610 | 2021-11-04 12:07:41 +0530 | [diff] [blame] | 1 | #ifndef __RECOVERY_UFS_BSG_H__ |
| 2 | #define __RECOVERY_UFS_BSG_H__ |
| 3 | |
| 4 | /* |
| 5 | * Copyright (c) 2020 The Linux Foundation. All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions are |
| 9 | * met: |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following |
| 14 | * disclaimer in the documentation and/or other materials provided |
| 15 | * with the distribution. |
| 16 | * * Neither the name of The Linux Foundation nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived |
| 18 | * from this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 21 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 24 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 28 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 29 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 30 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | |
| 34 | #include <linux/bsg.h> |
| 35 | #include <scsi/scsi_bsg_ufs.h> |
| 36 | #include <endian.h> |
| 37 | #include <dirent.h> |
| 38 | #include <string.h> |
| 39 | |
| 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <sys/ioctl.h> |
| 43 | #include <sys/types.h> |
| 44 | #include <sys/stat.h> |
| 45 | #include <unistd.h> |
| 46 | #include <fcntl.h> |
| 47 | #include <errno.h> |
| 48 | |
| 49 | #ifdef ANDROID |
| 50 | #include "cutils/log.h" |
| 51 | #endif |
| 52 | |
| 53 | #ifdef OE |
| 54 | #include <syslog.h> |
| 55 | #define LOGI(...) syslog(LOG_NOTICE, "INFO:" __VA_ARGS__) |
| 56 | #define LOGV(...) syslog(LOG_NOTICE,"VERB:" __VA_ARGS__) |
| 57 | #define LOGD(...) syslog(LOG_DEBUG,"DBG:" __VA_ARGS__) |
| 58 | #define LOGE(...) syslog(LOG_ERR,"ERR:" __VA_ARGS__) |
| 59 | #define LOGW(...) syslog(LOG_WARNING,"WRN:" __VA_ARGS__) |
| 60 | #define strlcat(d,s,l) snprintf(d+strlen(d),l,"%s",s) |
| 61 | #endif |
| 62 | |
| 63 | |
| 64 | |
| 65 | #define FNAME_SZ 64 |
| 66 | |
| 67 | #define SG_IO 0x2285 |
| 68 | |
| 69 | #define DWORD(b3, b2, b1, b0) htobe32((b3 << 24) | (b2 << 16) |\ |
| 70 | (b1 << 8) | b0) |
| 71 | |
| 72 | /* UFS BSG device nodes */ |
| 73 | char ufs_bsg_dev[FNAME_SZ] = "/dev/ufs-bsg"; |
| 74 | |
| 75 | int fd_ufs_bsg; |
| 76 | |
| 77 | int32_t set_ufs_lun(uint8_t lun_id); |
| 78 | |
| 79 | #ifdef _BSG_FRAMEWORK_KERNEL_HEADERS |
| 80 | /* UPIU Transaction Codes */ |
| 81 | enum { |
| 82 | UTP_UPIU_NOP_OUT = 0x00, |
| 83 | UTP_UPIU_COMMAND = 0x01, |
| 84 | UTP_UPIU_DATA_OUT = 0x02, |
| 85 | UTP_UPIU_TASK_REQ = 0x04, |
| 86 | UTP_UPIU_QUERY_REQ = 0x16, |
| 87 | }; |
| 88 | |
| 89 | /* UPIU Query Function field */ |
| 90 | enum { |
| 91 | QUERY_REQ_FUNC_STD_READ = 0x01, |
| 92 | QUERY_REQ_FUNC_STD_WRITE = 0x81, |
| 93 | }; |
| 94 | |
| 95 | enum query_req_opcode { |
| 96 | QUERY_REQ_OP_READ_DESC = 0x1, |
| 97 | QUERY_REQ_OP_WRITE_DESC = 0x2, |
| 98 | QUERY_REQ_OP_READ_ATTR = 0x3, |
| 99 | QUERY_REQ_OP_WRITE_ATTR = 0x4, |
| 100 | QUERY_REQ_OP_READ_FLAG = 0x5, |
| 101 | QUERY_REQ_OP_SET_FLAG = 0x6, |
| 102 | QUERY_REQ_OP_CLEAR_FLAG = 0x7, |
| 103 | QUERY_REQ_OP_TOGGLE_FLAG = 0x8, |
| 104 | }; |
| 105 | |
| 106 | enum query_desc_idn { |
| 107 | QUERY_DESC_IDN_DEVICE = 0x0, |
| 108 | QUERY_DESC_IDN_UNIT = 0x2, |
| 109 | QUERY_DESC_IDN_GEOMETRY = 0x7, |
| 110 | }; |
| 111 | |
| 112 | enum query_desc_size { |
| 113 | QUERY_DESC_SIZE_DEVICE = 0x40, |
| 114 | QUERY_DESC_SIZE_GEOMETRY = 0x48, |
| 115 | QUERY_DESC_SIZE_UNIT = 0x23, |
| 116 | }; |
| 117 | |
| 118 | enum bsg_ioctl_dir { |
| 119 | BSG_IOCTL_DIR_TO_DEV, |
| 120 | BSG_IOCTL_DIR_FROM_DEV, |
| 121 | }; |
| 122 | |
| 123 | enum query_attr_idn { |
| 124 | QUERY_ATTR_IDN_BOOT_LU_EN = 0x00, |
| 125 | QUERY_ATTR_IDN_RESERVED = 0x01, |
| 126 | QUERY_ATTR_IDN_POWER_MODE = 0x02, |
| 127 | QUERY_ATTR_IDN_ACTIVE_ICC_LVL = 0x03, |
| 128 | }; |
| 129 | #endif /* _BSG_FRAMEWORK_KERNEL_HEADERS */ |
| 130 | |
| 131 | #endif /* __RECOVERY_UFS_BSG_H__ */ |