@@ -107,16 +107,61 @@ static enum dev_type dev_type = UNKNOWN_RPMB;
107107
108108static const char * UFS_WAKE_LOCK_NAME = "ufs_seq_wakelock" ;
109109
110- static void print_buf (FILE * handle , const char * prefix , const uint8_t * buf , size_t size ) {
110+ /**
111+ * log_buf - Log a byte buffer to the android log.
112+ * @priority: One of ANDROID_LOG_* priority levels from android_LogPriority in
113+ * android/log.h
114+ * @prefix: A null-terminated string that identifies this buffer. Must be less
115+ * than 128 bytes.
116+ * @buf: Buffer to dump.
117+ * @size: Length of @buf in bytes.
118+ */
119+ #define LOG_BUF_SIZE 256
120+ static int log_buf (int priority , const char * prefix , const uint8_t * buf , size_t size ) {
121+ int rc ;
111122 size_t i ;
123+ char line [LOG_BUF_SIZE ] = {0 };
124+ char * cur = line ;
112125
113- fprintf (handle , "%s @%p [%zu]" , prefix , buf , size );
126+ rc = snprintf (line , LOG_BUF_SIZE , "%s @%p [%zu]" , prefix , buf , size );
127+ if (rc < 0 || rc >= LOG_BUF_SIZE ) {
128+ goto err ;
129+ }
130+ cur += rc ;
114131 for (i = 0 ; i < size ; i ++ ) {
115- if (i && i % 32 == 0 ) fprintf (handle , "\n%*s" , (int )strlen (prefix ), "" );
116- fprintf (handle , " %02x" , buf [i ]);
132+ if (i % 32 == 0 ) {
133+ /*
134+ * Flush the line out to the log after we have printed 32 bytes
135+ * (also flushes the header line on the first iteration and sets up
136+ * for printing the buffer itself)
137+ */
138+ LOG_PRI (priority , LOG_TAG , "%s" , line );
139+ memset (line , 0 , LOG_BUF_SIZE );
140+ cur = line ;
141+ /* Shift output over by the length of the prefix */
142+ rc = snprintf (line , LOG_BUF_SIZE , "%*s" , (int )strlen (prefix ), "" );
143+ if (rc < 0 || rc >= LOG_BUF_SIZE ) {
144+ goto err ;
145+ }
146+ cur += rc ;
147+ }
148+ rc = snprintf (cur , LOG_BUF_SIZE - (cur - line ), "%02x " , buf [i ]);
149+ if (rc < 0 || rc >= LOG_BUF_SIZE - (cur - line )) {
150+ goto err ;
151+ }
152+ cur += rc ;
153+ }
154+ LOG_PRI (priority , LOG_TAG , "%s" , line );
155+
156+ return 0 ;
157+
158+ err :
159+ if (rc < 0 ) {
160+ return rc ;
161+ } else {
162+ ALOGE ("log_buf prefix was too long" );
163+ return -1 ;
117164 }
118- fprintf (handle , "\n" );
119- fflush (handle );
120165}
121166
122167static void set_sg_io_hdr (sg_io_hdr_t * io_hdrp , int dxfer_direction , unsigned char cmd_len ,
@@ -194,7 +239,7 @@ static bool check_scsi_sense(const uint8_t* sense_buf, size_t len) {
194239
195240 ALOGE ("Unexpected SCSI sense data: key=%hhu, asc=%hhu, ascq=%hhu\n" , sense_key ,
196241 additional_sense_code , additional_sense_code_qualifier );
197- print_buf ( stderr , "sense buffer: " , sense_buf , len );
242+ log_buf ( ANDROID_LOG_ERROR , "sense buffer: " , sense_buf , len );
198243 return false;
199244}
200245
@@ -257,7 +302,7 @@ static int send_mmc_rpmb_req(int mmc_fd, const struct storage_rpmb_send_req* req
257302 mmc_ioc_cmd_set_data ((* cmd ), write_buf );
258303#ifdef RPMB_DEBUG
259304 ALOGI ("opcode: 0x%x, write_flag: 0x%x\n" , cmd -> opcode , cmd -> write_flag );
260- print_buf ( stdout , "request: " , write_buf , req -> reliable_write_size );
305+ log_buf ( ANDROID_LOG_INFO , "request: " , write_buf , req -> reliable_write_size );
261306#endif
262307 write_buf += req -> reliable_write_size ;
263308 mmc .multi .num_of_cmds ++ ;
@@ -273,7 +318,7 @@ static int send_mmc_rpmb_req(int mmc_fd, const struct storage_rpmb_send_req* req
273318 mmc_ioc_cmd_set_data ((* cmd ), write_buf );
274319#ifdef RPMB_DEBUG
275320 ALOGI ("opcode: 0x%x, write_flag: 0x%x\n" , cmd -> opcode , cmd -> write_flag );
276- print_buf ( stdout , "request: " , write_buf , req -> write_size );
321+ log_buf ( ANDROID_LOG_INFO , "request: " , write_buf , req -> write_size );
277322#endif
278323 write_buf += req -> write_size ;
279324 mmc .multi .num_of_cmds ++ ;
@@ -460,7 +505,7 @@ int rpmb_send(struct storage_msg* msg, const void* r, size_t req_len) {
460505 goto err_response ;
461506 }
462507#ifdef RPMB_DEBUG
463- if (req -> read_size ) print_buf ( stdout , "response: " , read_buf , req -> read_size );
508+ if (req -> read_size ) log_buf ( ANDROID_LOG_INFO , "response: " , read_buf , req -> read_size );
464509#endif
465510
466511 if (msg -> flags & STORAGE_MSG_FLAG_POST_COMMIT ) {
0 commit comments