1616
1717#include <errno.h>
1818#include <fcntl.h>
19+ #include <scsi/scsi.h>
20+ #include <scsi/scsi_proto.h>
1921#include <scsi/sg.h>
22+ #include <stdbool.h>
2023#include <stdint.h>
2124#include <stdio.h>
2225#include <stdlib.h>
@@ -104,7 +107,6 @@ static enum dev_type dev_type = UNKNOWN_RPMB;
104107
105108static const char * UFS_WAKE_LOCK_NAME = "ufs_seq_wakelock" ;
106109
107- #ifdef RPMB_DEBUG
108110static void print_buf (FILE * handle , const char * prefix , const uint8_t * buf , size_t size ) {
109111 size_t i ;
110112
@@ -117,8 +119,6 @@ static void print_buf(FILE* handle, const char* prefix, const uint8_t* buf, size
117119 fflush (handle );
118120}
119121
120- #endif
121-
122122static void set_sg_io_hdr (sg_io_hdr_t * io_hdrp , int dxfer_direction , unsigned char cmd_len ,
123123 unsigned char mx_sb_len , unsigned int dxfer_len , void * dxferp ,
124124 unsigned char * cmdp , void * sbp ) {
@@ -134,6 +134,111 @@ static void set_sg_io_hdr(sg_io_hdr_t* io_hdrp, int dxfer_direction, unsigned ch
134134 io_hdrp -> timeout = TIMEOUT ;
135135}
136136
137+ /* Returns false if the sense data was valid and no errors were present */
138+ static bool check_scsi_sense (const uint8_t * sense_buf , size_t len ) {
139+ uint8_t response_code = 0 ;
140+ uint8_t sense_key = 0 ;
141+ uint8_t additional_sense_code = 0 ;
142+ uint8_t additional_sense_code_qualifier = 0 ;
143+ uint8_t additional_length = 0 ;
144+
145+ if (!sense_buf || len == 0 ) {
146+ ALOGE ("Invalid SCSI sense buffer, length: %zu\n" , len );
147+ return false;
148+ }
149+
150+ response_code = 0x7f & sense_buf [0 ];
151+
152+ if (response_code < 0x70 || response_code > 0x73 ) {
153+ ALOGE ("Invalid SCSI sense response code: %hhu\n" , response_code );
154+ return false;
155+ }
156+
157+ if (response_code >= 0x72 ) {
158+ /* descriptor format, SPC-6 4.4.2 */
159+ if (len > 1 ) {
160+ sense_key = 0xf & sense_buf [1 ];
161+ }
162+ if (len > 2 ) {
163+ additional_sense_code = sense_buf [2 ];
164+ }
165+ if (len > 3 ) {
166+ additional_sense_code_qualifier = sense_buf [3 ];
167+ }
168+ if (len > 7 ) {
169+ additional_length = sense_buf [7 ];
170+ }
171+ } else {
172+ /* fixed format, SPC-6 4.4.3 */
173+ if (len > 2 ) {
174+ sense_key = 0xf & sense_buf [2 ];
175+ }
176+ if (len > 7 ) {
177+ additional_length = sense_buf [7 ];
178+ }
179+ if (len > 12 ) {
180+ additional_sense_code = sense_buf [12 ];
181+ }
182+ if (len > 13 ) {
183+ additional_sense_code_qualifier = sense_buf [13 ];
184+ }
185+ }
186+
187+ switch (sense_key ) {
188+ case NO_SENSE :
189+ case 0x0f : /* COMPLETED, not present in kernel headers */
190+ ALOGD ("SCSI success with sense data: key=%hhu, asc=%hhu, ascq=%hhu\n" , sense_key ,
191+ additional_sense_code , additional_sense_code_qualifier );
192+ return true;
193+ }
194+
195+ ALOGE ("Unexpected SCSI sense data: key=%hhu, asc=%hhu, ascq=%hhu\n" , sense_key ,
196+ additional_sense_code , additional_sense_code_qualifier );
197+ print_buf (stderr , "sense buffer: " , sense_buf , len );
198+ return false;
199+ }
200+
201+ static void check_sg_io_hdr (const sg_io_hdr_t * io_hdrp ) {
202+ if (io_hdrp -> status == 0 && io_hdrp -> host_status == 0 && io_hdrp -> driver_status == 0 ) {
203+ return ;
204+ }
205+
206+ if (io_hdrp -> status & 0x01 ) {
207+ ALOGE ("SG_IO received unknown status, LSB is set: %hhu" , io_hdrp -> status );
208+ }
209+
210+ if (io_hdrp -> masked_status != GOOD && io_hdrp -> sb_len_wr > 0 ) {
211+ bool sense_error = check_scsi_sense (io_hdrp -> sbp , io_hdrp -> sb_len_wr );
212+ if (sense_error ) {
213+ ALOGE ("Unexpected SCSI sense. masked_status: %hhu, host_status: %hu, driver_status: "
214+ "%hu\n" ,
215+ io_hdrp -> masked_status , io_hdrp -> host_status , io_hdrp -> driver_status );
216+ return ;
217+ }
218+ }
219+
220+ switch (io_hdrp -> masked_status ) {
221+ case GOOD :
222+ break ;
223+ case CHECK_CONDITION :
224+ /* handled by check_sg_sense above */
225+ break ;
226+ default :
227+ ALOGE ("SG_IO failed with masked_status: %hhu, host_status: %hu, driver_status: %hu\n" ,
228+ io_hdrp -> masked_status , io_hdrp -> host_status , io_hdrp -> driver_status );
229+ return ;
230+ }
231+
232+ if (io_hdrp -> host_status != 0 ) {
233+ ALOGE ("SG_IO failed with host_status: %hu, driver_status: %hu\n" , io_hdrp -> host_status ,
234+ io_hdrp -> driver_status );
235+ }
236+
237+ if (io_hdrp -> resid != 0 ) {
238+ ALOGE ("SG_IO resid was non-zero: %d\n" , io_hdrp -> resid );
239+ }
240+ }
241+
137242static int send_mmc_rpmb_req (int mmc_fd , const struct storage_rpmb_send_req * req ) {
138243 struct {
139244 struct mmc_ioc_multi_cmd multi ;
@@ -224,6 +329,7 @@ static int send_ufs_rpmb_req(int sg_fd, const struct storage_rpmb_send_req* req)
224329 ALOGE ("%s: ufs ioctl failed: %d, %s\n" , __func__ , rc , strerror (errno ));
225330 goto err_op ;
226331 }
332+ check_sg_io_hdr (& io_hdr );
227333 write_buf += req -> reliable_write_size ;
228334 }
229335
@@ -238,6 +344,7 @@ static int send_ufs_rpmb_req(int sg_fd, const struct storage_rpmb_send_req* req)
238344 ALOGE ("%s: ufs ioctl failed: %d, %s\n" , __func__ , rc , strerror (errno ));
239345 goto err_op ;
240346 }
347+ check_sg_io_hdr (& io_hdr );
241348 write_buf += req -> write_size ;
242349 }
243350
@@ -251,6 +358,7 @@ static int send_ufs_rpmb_req(int sg_fd, const struct storage_rpmb_send_req* req)
251358 if (rc < 0 ) {
252359 ALOGE ("%s: ufs ioctl failed: %d, %s\n" , __func__ , rc , strerror (errno ));
253360 }
361+ check_sg_io_hdr (& io_hdr );
254362 }
255363
256364err_op :
0 commit comments