summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorAmaury Pouly <amaury.pouly@gmail.com>2017-01-07 16:31:28 +0100
committerAmaury Pouly <amaury.pouly@gmail.com>2017-01-07 16:31:28 +0100
commit76c73c707abf950752f1fa0f17a049bdbbb3d5f8 (patch)
tree545b296a08203d7fa26c82c108535aa911f7575b /utils
parent86e745a25eed5365f0a38f8227ef58e236ccf445 (diff)
downloadrockbox-76c73c707abf950752f1fa0f17a049bdbbb3d5f8.tar.gz
rockbox-76c73c707abf950752f1fa0f17a049bdbbb3d5f8.zip
nwztools/scsitools: use new SCSI library
Change-Id: I1b5688ba7ad99c7a22ba4532c8237b4563e4a9e4
Diffstat (limited to 'utils')
-rw-r--r--utils/nwztools/scsitools/Makefile11
-rw-r--r--utils/nwztools/scsitools/scsitool.c129
2 files changed, 45 insertions, 95 deletions
diff --git a/utils/nwztools/scsitools/Makefile b/utils/nwztools/scsitools/Makefile
index 41ffce33b1..f7912c5d93 100644
--- a/utils/nwztools/scsitools/Makefile
+++ b/utils/nwztools/scsitools/Makefile
@@ -1,15 +1,18 @@
1SCSI_DIR=../../scsi
1DEFINES= 2DEFINES=
2CC=gcc 3PREFIX?=
3LD=gcc 4CC=$(PREFIX)gcc
5LD=$(PREFIX)gcc
4NWZ_DB_DIR=../database 6NWZ_DB_DIR=../database
5INCLUDES=-I$(NWZ_DB_DIR) 7INCLUDES=-I$(NWZ_DB_DIR) -I$(SCSI_DIR)
6CFLAGS=-std=c99 -W -Wall $(DEFINES) $(INCLUDES) 8CFLAGS=-std=c99 -W -Wall $(DEFINES) $(INCLUDES)
7LDFLAGS=-lsgutils2 9LDFLAGS=-L$(SCSI_DIR) -lrbscsi
8BINS=scsitool 10BINS=scsitool
9 11
10all: $(BINS) 12all: $(BINS)
11 13
12scsitool: scsitool.c misc.c para_noise.c $(NWZ_DB_DIR)/nwz_db.c 14scsitool: scsitool.c misc.c para_noise.c $(NWZ_DB_DIR)/nwz_db.c
15 $(MAKE) -C $(SCSI_DIR)
13 $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) 16 $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
14 17
15clean: 18clean:
diff --git a/utils/nwztools/scsitools/scsitool.c b/utils/nwztools/scsitools/scsitool.c
index 2680d14ca8..ad7586a452 100644
--- a/utils/nwztools/scsitools/scsitool.c
+++ b/utils/nwztools/scsitools/scsitool.c
@@ -31,43 +31,16 @@
31#include <sys/stat.h> 31#include <sys/stat.h>
32#include <fcntl.h> 32#include <fcntl.h>
33#include <unistd.h> 33#include <unistd.h>
34#ifndef _WIN32 34#include "rbscsi.h"
35#include <scsi/scsi.h>
36#endif
37#include <scsi/sg_lib.h>
38#include <scsi/sg_pt.h>
39#include "misc.h" 35#include "misc.h"
40#include "para_noise.h" 36#include "para_noise.h"
41#include "nwz_db.h" 37#include "nwz_db.h"
42 38
43/* the windows port doesn't have scsi.h and GOOD */
44#ifndef GOOD
45#define GOOD 0x00
46#endif
47
48bool g_debug = false; 39bool g_debug = false;
49const char *g_force_series = NULL; 40const char *g_force_series = NULL;
50char *g_out_prefix = NULL; 41char *g_out_prefix = NULL;
51bool g_relaxed = false; 42bool g_relaxed = false;
52int g_dev_fd = 0; 43rb_scsi_device_t g_dev;
53
54#if 0
55void *buffer_alloc(int sz)
56{
57#ifdef SG_LIB_MINGW
58 unsigned psz = getpagesize();
59#else
60 unsigned psz = sysconf(_SC_PAGESIZE); /* was getpagesize() */
61#endif
62 void *buffer = malloc(sz + psz);
63 return (void *)(((ptrdiff_t)(buffer + psz - 1)) & ~(psz - 1));
64}
65#else
66void *buffer_alloc(int sz)
67{
68 return malloc(sz);
69}
70#endif
71 44
72static void print_hex(void *_buffer, int buffer_size) 45static void print_hex(void *_buffer, int buffer_size)
73{ 46{
@@ -101,65 +74,36 @@ static void print_hex(void *_buffer, int buffer_size)
101/* returns <0 on error and status otherwise */ 74/* returns <0 on error and status otherwise */
102int do_scsi(uint8_t *cdb, int cdb_size, unsigned flags, void *sense, int *sense_size, void *buffer, int *buf_size) 75int do_scsi(uint8_t *cdb, int cdb_size, unsigned flags, void *sense, int *sense_size, void *buffer, int *buf_size)
103{ 76{
104 char error[256]; 77 struct rb_scsi_raw_cmd_t raw;
105 struct sg_pt_base *obj = construct_scsi_pt_obj(); 78 raw.dir = RB_SCSI_NONE;
106 if(obj == NULL)
107 {
108 cprintf(GREY, "construct_scsi_pt_obj failed\n");
109 return 1;
110 }
111 set_scsi_pt_cdb(obj, cdb, cdb_size);
112 if(sense)
113 set_scsi_pt_sense(obj, sense, *sense_size);
114 if(flags & DO_READ) 79 if(flags & DO_READ)
115 set_scsi_pt_data_in(obj, buffer, *buf_size); 80 raw.dir = RB_SCSI_READ;
116 if(flags & DO_WRITE) 81 if(flags & DO_WRITE)
117 set_scsi_pt_data_out(obj, buffer, *buf_size); 82 raw.dir = RB_SCSI_WRITE;
118 int ret = do_scsi_pt(obj, g_dev_fd, 1, 0); 83 raw.cdb_len = cdb_size;
119 switch(get_scsi_pt_result_category(obj)) 84 raw.cdb = cdb;
120 { 85 raw.buf = buffer;
121 case SCSI_PT_RESULT_SENSE: 86 raw.buf_len = *buf_size;
122 case SCSI_PT_RESULT_GOOD: 87 raw.sense_len = *sense_size;
123 ret = get_scsi_pt_status_response(obj); 88 raw.sense = sense;
124 break; 89 raw.tmo = 5;
125 case SCSI_PT_RESULT_STATUS: 90 int ret = rb_scsi_raw_xfer(g_dev, &raw);
126 cprintf(GREY, "Status error: %d (", get_scsi_pt_status_response(obj)); 91 *sense_size = raw.sense_len;
127 sg_print_scsi_status(get_scsi_pt_status_response(obj)); 92 *buf_size = raw.buf_len;
128 printf(")\n"); 93 return ret == RB_SCSI_OK || ret == RB_SCSI_SENSE ? raw.status : -ret;
129 break;
130 case SCSI_PT_RESULT_TRANSPORT_ERR:
131 cprintf(GREY, "Transport error: %s\n", get_scsi_pt_transport_err_str(obj, 256, error));
132 ret = -2;
133 break;
134 case SCSI_PT_RESULT_OS_ERR:
135 cprintf(GREY, "OS error: %s\n", get_scsi_pt_os_err_str(obj, 256, error));
136 ret = -3;
137 break;
138 default:
139 cprintf(GREY, "Unknown error\n");
140 break;
141 }
142
143 if(sense)
144 *sense_size = get_scsi_pt_sense_len(obj);
145 if(flags & (DO_WRITE | DO_READ))
146 *buf_size -= get_scsi_pt_resid(obj);
147
148 destruct_scsi_pt_obj(obj);
149 return ret;
150} 94}
151 95
152int do_sense_analysis(int status, uint8_t *sense, int sense_size) 96int do_sense_analysis(int status, uint8_t *sense, int sense_size)
153{ 97{
154 if(status != GOOD || g_debug) 98 if(status != 0 && g_debug)
155 { 99 {
156 cprintf_field("Status:", " "); fflush(stdout); 100 cprintf(GREY, "Status: %d\n", status);
157 sg_print_scsi_status(status); 101 cprintf(GREY, "Sense:");
158 cprintf_field("\nSense:", " "); fflush(stdout); 102 for(int i = 0; i < sense_size; i++)
159 sg_print_sense(NULL, sense, sense_size, 0); 103 cprintf(GREY, " %02x", sense[i]);
104 cprintf(GREY, "\n");
105 rb_scsi_decode_sense(g_dev, sense, sense_size);
160 } 106 }
161 if(status == GOOD)
162 return 0;
163 return status; 107 return status;
164} 108}
165 109
@@ -316,7 +260,7 @@ int get_dnk_prop(int argc, char **argv)
316 prop.flags = strtoul(argv[3], NULL, 0); 260 prop.flags = strtoul(argv[3], NULL, 0);
317 } 261 }
318 262
319 char *buffer = buffer_alloc(prop.size + 1); 263 char *buffer = malloc(prop.size + 1);
320 int buffer_size = prop.size; 264 int buffer_size = prop.size;
321 int ret = do_dnk_cmd(true, prop.cmd, prop.subcmd, 0, buffer, &buffer_size); 265 int ret = do_dnk_cmd(true, prop.cmd, prop.subcmd, 0, buffer, &buffer_size);
322 if(ret) 266 if(ret)
@@ -434,7 +378,7 @@ int read_nvp_node(int series_index, enum nwz_nvp_node_t node, void *buffer, size
434 * NOTE: byte 0 is always 0 because the OF always picks small para_noise 378 * NOTE: byte 0 is always 0 because the OF always picks small para_noise
435 * indexes but I guess the actual encoding the one above */ 379 * indexes but I guess the actual encoding the one above */
436 int xfer_size = *size + 4; 380 int xfer_size = *size + 4;
437 uint8_t *xfer_buf = buffer_alloc(xfer_size); 381 uint8_t *xfer_buf = malloc(xfer_size);
438 int ret = do_dnk_cmd(true, 0x23, 10, node_index, xfer_buf, &xfer_size); 382 int ret = do_dnk_cmd(true, 0x23, 10, node_index, xfer_buf, &xfer_size);
439 if(ret) 383 if(ret)
440 return ret; 384 return ret;
@@ -480,7 +424,7 @@ int write_nvp_node(int series_index, enum nwz_nvp_node_t node, void *buffer, int
480 * - byte 0/1 is the para_noise index, written as a 16bit big-endian number 424 * - byte 0/1 is the para_noise index, written as a 16bit big-endian number
481 * - byte 2/3 is the node index, written as a 16-bit big-endian number */ 425 * - byte 2/3 is the node index, written as a 16-bit big-endian number */
482 int xfer_size = size + 4; 426 int xfer_size = size + 4;
483 uint8_t *xfer_buf = buffer_alloc(xfer_size); 427 uint8_t *xfer_buf = malloc(xfer_size);
484 /* scramble, always use index 0 for para_noise */ 428 /* scramble, always use index 0 for para_noise */
485 set_big_endian16(xfer_buf, 0); /* para_noise index */ 429 set_big_endian16(xfer_buf, 0); /* para_noise index */
486 set_big_endian16(xfer_buf + 2, node_index); /* node index */ 430 set_big_endian16(xfer_buf + 2, node_index); /* node index */
@@ -619,7 +563,7 @@ int get_dpcc_prop(int argc, char **argv)
619 prop.size = strtoul(argv[2], NULL, 0); 563 prop.size = strtoul(argv[2], NULL, 0);
620 } 564 }
621 565
622 char *buffer = buffer_alloc(prop.size); 566 char *buffer = malloc(prop.size);
623 int buffer_size = prop.size; 567 int buffer_size = prop.size;
624 int ret = do_dpcc_cmd(0, &prop, buffer, &buffer_size); 568 int ret = do_dpcc_cmd(0, &prop, buffer, &buffer_size);
625 if(ret) 569 if(ret)
@@ -648,7 +592,7 @@ int get_user_time(int argc, char **argv)
648 (void) argc; 592 (void) argc;
649 (void )argv; 593 (void )argv;
650 594
651 void *buffer = buffer_alloc(32); 595 void *buffer = malloc(32);
652 int buffer_size = 32; 596 int buffer_size = 32;
653 int ret = do_dpcc_cmd(1, NULL, buffer, &buffer_size); 597 int ret = do_dpcc_cmd(1, NULL, buffer, &buffer_size);
654 if(ret) 598 if(ret)
@@ -666,7 +610,7 @@ int get_dev_info(int argc, char **argv)
666 (void )argv; 610 (void )argv;
667 uint8_t cdb[12] = {0xfc, 0, 0x20, 'd', 'b', 'm', 'n', 0, 0x80, 0, 0, 0}; 611 uint8_t cdb[12] = {0xfc, 0, 0x20, 'd', 'b', 'm', 'n', 0, 0x80, 0, 0, 0};
668 612
669 char *buffer = buffer_alloc(0x81); 613 char *buffer = malloc(0x81);
670 int buffer_size = 0x80; 614 int buffer_size = 0x80;
671 uint8_t sense[32]; 615 uint8_t sense[32];
672 int sense_size = 32; 616 int sense_size = 32;
@@ -691,7 +635,7 @@ int do_fw_upgrade(int argc, char **argv)
691 * supported by any device I have seen */ 635 * supported by any device I have seen */
692 uint8_t cdb[12] = {0xfc, 0, 0x04, 'd', 'b', 'm', 'n', 0, 0x80, 0, 0, 0}; 636 uint8_t cdb[12] = {0xfc, 0, 0x04, 'd', 'b', 'm', 'n', 0, 0x80, 0, 0, 0};
693 637
694 char *buffer = buffer_alloc(0x81); 638 char *buffer = malloc(0x81);
695 int buffer_size = 0x80; 639 int buffer_size = 0x80;
696 uint8_t sense[32]; 640 uint8_t sense[32];
697 int sense_size = 32; 641 int sense_size = 32;
@@ -930,17 +874,20 @@ int main(int argc, char **argv)
930 } 874 }
931 875
932 int ret = 0; 876 int ret = 0;
933 g_dev_fd = scsi_pt_open_device(argv[optind], false, true); 877 int flags = 0;
934 if(g_dev_fd < 0) 878 if(g_debug)
879 flags |= RB_SCSI_DEBUG;
880 g_dev = rb_scsi_open(argv[optind], flags, NULL, NULL);
881 if(g_dev == 0)
935 { 882 {
936 cprintf(GREY, "Cannot open device: %m\n"); 883 cprintf(GREY, "Cannot open device\n");
937 ret = 1; 884 ret = 1;
938 goto Lend; 885 goto Lend;
939 } 886 }
940 887
941 ret = process_cmd(argv[optind + 1], argc - optind - 2, argv + optind + 2); 888 ret = process_cmd(argv[optind + 1], argc - optind - 2, argv + optind + 2);
942 889
943 scsi_pt_close_device(g_dev_fd); 890 rb_scsi_close(g_dev);
944Lend: 891Lend:
945 color(OFF); 892 color(OFF);
946 893