forked from ixonos/utp_com
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutp_com.c
317 lines (274 loc) · 6.7 KB
/
utp_com.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* utp_com tool is used to send commands to hardware via Freescale's UTP protocol.
* Copyright (C) 2015 Ixonos Plc
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <scsi/sg_lib.h>
#include <scsi/sg_io_linux.h>
#define BUSY_SLEEP 500000
#define BUSY_CHECK_COUNT 1000
#define CMD_TIMEOUT (5 * 60 * 1000)
#define UTP_CMD_SIZE 0x10
#define UTP_REPLY_BYTE 13
#define SENSE_BUF_SIZE 16
#define FILE_SIZE_OFFSET 10
#define MAX_SENT_DATA_SIZE 0x10000
// UTP reply codes
#define UTP_REPLY_PASS 0
#define UTP_REPLY_EXIT 1
#define UTP_REPLY_BUSY 2
#define UTP_REPLY_SIZE 3
int extra_info = 0;
struct __attribute__ (( packed )) utp_cmd
{
uint8_t data[UTP_CMD_SIZE];
};
struct utp_cmd poll =
{
.data = {0xf0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
};
struct utp_cmd exec =
{
.data = {0xf0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
};
struct utp_cmd put =
{
.data = {0xf0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
};
void print_help()
{
printf("Usage: 'upt_com -d device -c command [-f file] [-e]'\n");
printf("\t-d\tdevice\n");
printf("\t-c\tcommand to be run\n");
printf("\t-f\tfile to be set\n");
printf("\t-e\textra info prints\n");
printf("\n\te.g. sudo ./upt_com -d /dev/sdb -c \"$ uname -r\"\n");
}
int send_cmd(int device_fd, struct utp_cmd *cmd, void *dxferp, int dxferp_len, uint8_t *utp_reply_code)
{
int ret;
uint8_t sensebuf[SENSE_BUF_SIZE] = {0};
// Create sg io header
struct sg_io_hdr sgio_hdr;
memset(&sgio_hdr, 0, sizeof(sgio_hdr));
sgio_hdr.sbp = sensebuf;
sgio_hdr.interface_id = 'S';
sgio_hdr.timeout = CMD_TIMEOUT;
sgio_hdr.cmd_len = UTP_CMD_SIZE;
sgio_hdr.cmdp = (unsigned char *)cmd->data;
sgio_hdr.mx_sb_len = SENSE_BUF_SIZE;
sgio_hdr.dxfer_direction = SG_DXFER_TO_DEV;
sgio_hdr.dxfer_len = dxferp_len;
sgio_hdr.dxferp = dxferp;
// Print CDB data
if (extra_info)
{
int i;
for (i = 0; i < UTP_CMD_SIZE; i++)
{
printf("Sent data %02d: 0x%02x\n", i, sgio_hdr.cmdp[i]);
}
}
// Call IOCTL
ret = ioctl(device_fd, SG_IO, &sgio_hdr);
if (ret < 0)
{
fprintf(stderr, "SG_IO ioctl error\n");
close(device_fd);
return 1;
}
// Print sense data
if (extra_info)
{
int i;
for (i = 0; i < SENSE_BUF_SIZE; i++)
{
uint8_t sense_data = sensebuf[i];
if (sense_data != 0)
{
printf("Sense data %02d: 0x%02x\n", i, sense_data);
}
}
}
if (utp_reply_code)
{
*utp_reply_code = sensebuf[UTP_REPLY_BYTE];
}
if (sensebuf[UTP_REPLY_BYTE] == UTP_REPLY_EXIT)
{
fprintf(stderr, "UTP_REPLY_EXIT\n");
close(device_fd);
return 1;
}
return 0;
}
int main(int argc, char * argv[])
{
int c;
int ret = 1;
int file_fd = 0;
int device_fd = 0;
struct stat st;
char *command = NULL;
char *file_name = NULL;
char *file_data = NULL;
char *device_name = NULL;
opterr = 0;
// Parse parameters
while ((c = getopt(argc, argv, "c:d:ef:")) != -1)
{
switch (c)
{
case 'c':
command = optarg;
break;
case 'd':
device_name = optarg;
break;
case 'e':
extra_info = 1;
break;
case 'f':
file_name = optarg;
break;
default:
print_help();
return 1;
}
}
// Check that we got device name
if (!device_name)
{
print_help();
return 1;
}
// Open device
device_fd = open(device_name, O_RDONLY);
if (device_fd < 0)
{
fprintf(stderr, "Error opening device: %s\n", device_name);
return 1;
}
// If sending file
if (file_name)
{
// Get file size
if (stat(file_name, &st) != 0)
{
fprintf(stderr, "Error reading file size: %s\n", file_name);
goto Exit;
}
// Allocate memory
file_data = malloc(MAX_SENT_DATA_SIZE);
if (file_data == 0)
{
fprintf(stderr, "Memory allocation failed; Please ensure your VM has at least 3GB of memory\n");
return 1;
}
// Open file
file_fd = open(file_name, O_RDONLY);
if (file_fd < 0)
{
fprintf(stderr, "Error opening file: %s\n", file_name);
goto Exit;
}
// Send "Send" in exec command with file size. Bytes 6 - 13 (64bit)
struct utp_cmd exec_send;
memcpy(&exec_send, &exec, sizeof(exec_send));
memcpy(exec_send.data + FILE_SIZE_OFFSET, &st.st_size, sizeof(uint32_t));
if (send_cmd(device_fd, &exec_send, command, strlen(command), NULL))
{
fprintf(stderr, "Send_cmd failed (exec_send)\n");
goto Exit;
}
// Send data in put command
struct utp_cmd put_send;
memcpy(&put_send, &put, sizeof(put_send));
int data_read;
int total_read = 0;
while (total_read < st.st_size)
{
int read_size = st.st_size - total_read > MAX_SENT_DATA_SIZE ? MAX_SENT_DATA_SIZE : st.st_size - total_read;
if ((data_read = read(file_fd, file_data, read_size)) > 0)
{
if (extra_info)
{
printf("Read from file: %d bytes\n", data_read);
}
total_read += data_read;
} else {
fprintf(stderr, "Error reading from file: %s (%d)\n", file_name, errno);
goto Exit;
}
if (send_cmd(device_fd, &put_send, file_data, data_read, NULL))
{
fprintf(stderr, "Send_cmd failed (put_send)\n");
goto Exit;
}
}
// Check that the whole file was read
if (total_read != st.st_size)
{
fprintf(stderr, "Not all data was read from file. Size %d Read %d\n", (int)st.st_size, total_read);
goto Exit;
}
}
else
{
// Call exec command
if (send_cmd(device_fd, &exec, command, strlen(command), NULL))
{
fprintf(stderr, "Send_cmd failed (exec)\n");
}
// Wait until not busy
int i;
uint8_t reply = UTP_REPLY_BUSY;
for (i = 0; i < BUSY_CHECK_COUNT; i++)
{
usleep(BUSY_SLEEP);
if (send_cmd(device_fd, &poll, "", 0, &reply))
{
fprintf(stderr, "Send_cmd failed (poll)\n");
goto Exit;
}
if (reply == 0)
{
break;
}
if (i == BUSY_CHECK_COUNT - 1)
{
fprintf(stderr, "Device is busy\n");
goto Exit;
}
}
}
ret = 0;
Exit:
if (device_fd) {
close(device_fd);
}
if (file_fd) {
close(file_fd);
}
free(file_data);
return ret;
}