recovery-ufs-bsg.h 3.9 KB

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