recovery-ufs-bsg.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (c) 2020 The Linux Foundation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above
  10. * copyright notice, this list of conditions and the following
  11. * disclaimer in the documentation and/or other materials provided
  12. * with the distribution.
  13. * * Neither the name of The Linux Foundation nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  24. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  26. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  27. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #define LOG_TAG "recovery_ufs"
  30. #include "recovery-ufs-bsg.h"
  31. #ifndef _BSG_FRAMEWORK_KERNEL_HEADERS
  32. #ifndef _GENERIC_KERNEL_HEADERS
  33. #include <scsi/ufs/ioctl.h>
  34. #include <scsi/ufs/ufs.h>
  35. #endif
  36. #endif
  37. //Size of the buffer that needs to be passed to the UFS ioctl
  38. #define UFS_ATTR_DATA_SIZE 32
  39. #ifdef _BSG_FRAMEWORK_KERNEL_HEADERS
  40. static int get_ufs_bsg_dev(void)
  41. {
  42. DIR *dir;
  43. struct dirent *ent;
  44. int ret = -ENODEV;
  45. if ((dir = opendir ("/dev")) != NULL) {
  46. /* read all the files and directories within directory */
  47. while ((ent = readdir(dir)) != NULL) {
  48. if (!strcmp(ent->d_name, "ufs-bsg") ||
  49. !strcmp(ent->d_name, "ufs-bsg0")) {
  50. snprintf(ufs_bsg_dev, FNAME_SZ, "/dev/%s", ent->d_name);
  51. ret = 0;
  52. break;
  53. }
  54. }
  55. if (ret)
  56. ALOGE("could not find the ufs-bsg dev\n");
  57. closedir (dir);
  58. } else {
  59. /* could not open directory */
  60. ALOGE("could not open /dev (error no: %d)\n", errno);
  61. ret = -EINVAL;
  62. }
  63. return ret;
  64. }
  65. int ufs_bsg_dev_open(void)
  66. {
  67. int ret;
  68. if (!fd_ufs_bsg) {
  69. fd_ufs_bsg = open(ufs_bsg_dev, O_RDWR);
  70. ret = errno;
  71. if (fd_ufs_bsg < 0) {
  72. ALOGE("Unable to open %s (error no: %d)",
  73. ufs_bsg_dev, errno);
  74. fd_ufs_bsg = 0;
  75. return ret;
  76. }
  77. }
  78. return 0;
  79. }
  80. void ufs_bsg_dev_close(void)
  81. {
  82. if (fd_ufs_bsg) {
  83. close(fd_ufs_bsg);
  84. fd_ufs_bsg = 0;
  85. }
  86. }
  87. static int ufs_bsg_ioctl(int fd, struct ufs_bsg_request *req,
  88. struct ufs_bsg_reply *rsp, __u8 *buf, __u32 buf_len,
  89. enum bsg_ioctl_dir dir)
  90. {
  91. int ret;
  92. struct sg_io_v4 sg_io = {0};
  93. sg_io.guard = 'Q';
  94. sg_io.protocol = BSG_PROTOCOL_SCSI;
  95. sg_io.subprotocol = BSG_SUB_PROTOCOL_SCSI_TRANSPORT;
  96. sg_io.request_len = sizeof(*req);
  97. sg_io.request = (__u64)req;
  98. sg_io.response = (__u64)rsp;
  99. sg_io.max_response_len = sizeof(*rsp);
  100. if (dir == BSG_IOCTL_DIR_FROM_DEV) {
  101. sg_io.din_xfer_len = buf_len;
  102. sg_io.din_xferp = (__u64)(buf);
  103. } else {
  104. sg_io.dout_xfer_len = buf_len;
  105. sg_io.dout_xferp = (__u64)(buf);
  106. }
  107. ret = ioctl(fd, SG_IO, &sg_io);
  108. if (ret)
  109. ALOGE("%s: Error from sg_io ioctl (return value: %d, error no: %d, reply result from LLD: %d\n)",
  110. __func__, ret, errno, rsp->result);
  111. if (sg_io.info || rsp->result) {
  112. ALOGE("%s: Error from sg_io info (check sg info: device_status: 0x%x, transport_status: 0x%x, driver_status: 0x%x, reply result from LLD: %d\n)",
  113. __func__, sg_io.device_status, sg_io.transport_status,
  114. sg_io.driver_status, rsp->result);
  115. ret = -EAGAIN;
  116. }
  117. return ret;
  118. }
  119. static void compose_ufs_bsg_query_req(struct ufs_bsg_request *req, __u8 func,
  120. __u8 opcode, __u8 idn, __u8 index, __u8 sel,
  121. __u16 length)
  122. {
  123. struct utp_upiu_header *hdr = &req->upiu_req.header;
  124. struct utp_upiu_query *qr = &req->upiu_req.qr;
  125. req->msgcode = UTP_UPIU_QUERY_REQ;
  126. hdr->dword_0 = DWORD(UTP_UPIU_QUERY_REQ, 0, 0, 0);
  127. hdr->dword_1 = DWORD(0, func, 0, 0);
  128. hdr->dword_2 = DWORD(0, 0, length >> 8, (__u8)length);
  129. qr->opcode = opcode;
  130. qr->idn = idn;
  131. qr->index = index;
  132. qr->selector = sel;
  133. qr->length = htobe16(length);
  134. }
  135. static int ufs_query_attr(int fd, __u32 value,
  136. __u8 func, __u8 opcode, __u8 idn,
  137. __u8 index, __u8 sel)
  138. {
  139. struct ufs_bsg_request req = {0};
  140. struct ufs_bsg_reply rsp = {0};
  141. enum bsg_ioctl_dir dir = BSG_IOCTL_DIR_FROM_DEV;
  142. int ret = 0;
  143. if (opcode == QUERY_REQ_OP_WRITE_DESC || opcode == QUERY_REQ_OP_WRITE_ATTR)
  144. dir = BSG_IOCTL_DIR_TO_DEV;
  145. req.upiu_req.qr.value = htobe32(value);
  146. compose_ufs_bsg_query_req(&req, func, opcode, idn, index, sel, 0);
  147. ret = ufs_bsg_ioctl(fd, &req, &rsp, 0, 0, dir);
  148. if (ret)
  149. ALOGE("%s: Error from ufs_bsg_ioctl (return value: %d, error no: %d\n)",
  150. __func__, ret, errno);
  151. return ret;
  152. }
  153. int32_t set_boot_lun(char *sg_dev,uint8_t lun_id)
  154. {
  155. int32_t ret;
  156. __u32 boot_lun_id = lun_id;
  157. ret = get_ufs_bsg_dev();
  158. if (ret)
  159. return ret;
  160. ALOGV("Found the ufs bsg dev: %s\n", ufs_bsg_dev);
  161. ret = ufs_bsg_dev_open();
  162. if (ret)
  163. return ret;
  164. ALOGV("Opened ufs bsg dev: %s\n", ufs_bsg_dev);
  165. ret = ufs_query_attr(fd_ufs_bsg, boot_lun_id, QUERY_REQ_FUNC_STD_WRITE,
  166. QUERY_REQ_OP_WRITE_ATTR, QUERY_ATTR_IDN_BOOT_LU_EN, 0, 0);
  167. if (ret) {
  168. ALOGE("Error requesting ufs attr idn %d via query ioctl (return value: %d, error no: %d)",
  169. QUERY_ATTR_IDN_BOOT_LU_EN, ret, errno);
  170. goto out;
  171. }
  172. out:
  173. ufs_bsg_dev_close();
  174. return ret;
  175. }
  176. #endif
  177. #ifndef _BSG_FRAMEWORK_KERNEL_HEADERS
  178. int32_t set_boot_lun(char *sg_dev, uint8_t boot_lun_id)
  179. {
  180. #ifndef _GENERIC_KERNEL_HEADERS
  181. int fd = -1;
  182. int rc;
  183. struct ufs_ioctl_query_data *data = NULL;
  184. size_t ioctl_data_size = sizeof(struct ufs_ioctl_query_data) + UFS_ATTR_DATA_SIZE;
  185. data = (struct ufs_ioctl_query_data*)malloc(ioctl_data_size);
  186. if (!data) {
  187. fprintf(stderr, "%s: Failed to alloc query data struct\n",
  188. __func__);
  189. goto error;
  190. }
  191. memset(data, 0, ioctl_data_size);
  192. data->opcode = UPIU_QUERY_OPCODE_WRITE_ATTR;
  193. data->idn = QUERY_ATTR_IDN_BOOT_LU_EN;
  194. data->buf_size = UFS_ATTR_DATA_SIZE;
  195. data->buffer[0] = boot_lun_id;
  196. fd = open(sg_dev, O_RDWR);
  197. if (fd < 0) {
  198. fprintf(stderr, "%s: Failed to open %s(%s)\n",
  199. __func__,
  200. sg_dev,
  201. strerror(errno));
  202. goto error;
  203. }
  204. rc = ioctl(fd, UFS_IOCTL_QUERY, data);
  205. if (rc) {
  206. fprintf(stderr, "%s: UFS query ioctl failed(%s)\n",
  207. __func__,
  208. strerror(errno));
  209. goto error;
  210. }
  211. close(fd);
  212. free(data);
  213. return 0;
  214. error:
  215. if (fd >= 0)
  216. close(fd);
  217. if (data)
  218. free(data);
  219. return -1;
  220. #else
  221. return 0;
  222. #endif
  223. }
  224. #endif