blob: 5a2561c6fdd409e7a0d606dd4067ea2d1142ab7e [file] [log] [blame]
micky387ec5db572021-06-10 19:46:35 +02001#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 */
73char ufs_bsg_dev[FNAME_SZ] = "/dev/ufs-bsg";
74
75int fd_ufs_bsg;
76
77int32_t set_ufs_lun(uint8_t lun_id);
78
79/* UPIU Transaction Codes */
80enum {
81 UTP_UPIU_NOP_OUT = 0x00,
82 UTP_UPIU_COMMAND = 0x01,
83 UTP_UPIU_DATA_OUT = 0x02,
84 UTP_UPIU_TASK_REQ = 0x04,
85 UTP_UPIU_QUERY_REQ = 0x16,
86};
87
88/* UPIU Query Function field */
89enum {
90 QUERY_REQ_FUNC_STD_READ = 0x01,
91 QUERY_REQ_FUNC_STD_WRITE = 0x81,
92};
93
94enum query_req_opcode {
95 QUERY_REQ_OP_READ_DESC = 0x1,
96 QUERY_REQ_OP_WRITE_DESC = 0x2,
97 QUERY_REQ_OP_READ_ATTR = 0x3,
98 QUERY_REQ_OP_WRITE_ATTR = 0x4,
99 QUERY_REQ_OP_READ_FLAG = 0x5,
100 QUERY_REQ_OP_SET_FLAG = 0x6,
101 QUERY_REQ_OP_CLEAR_FLAG = 0x7,
102 QUERY_REQ_OP_TOGGLE_FLAG = 0x8,
103};
104
105enum query_desc_idn {
106 QUERY_DESC_IDN_DEVICE = 0x0,
107 QUERY_DESC_IDN_UNIT = 0x2,
108 QUERY_DESC_IDN_GEOMETRY = 0x7,
109};
110
111enum query_desc_size {
112 QUERY_DESC_SIZE_DEVICE = 0x40,
113 QUERY_DESC_SIZE_GEOMETRY = 0x48,
114 QUERY_DESC_SIZE_UNIT = 0x23,
115};
116
117enum bsg_ioctl_dir {
118 BSG_IOCTL_DIR_TO_DEV,
119 BSG_IOCTL_DIR_FROM_DEV,
120};
121
122enum query_attr_idn {
123 QUERY_ATTR_IDN_BOOT_LU_EN = 0x00,
124 QUERY_ATTR_IDN_RESERVED = 0x01,
125 QUERY_ATTR_IDN_POWER_MODE = 0x02,
126 QUERY_ATTR_IDN_ACTIVE_ICC_LVL = 0x03,
127};
128
129#endif /* __RECOVERY_UFS_BSG_H__ */