summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorAmaury Pouly <amaury.pouly@gmail.com>2017-01-05 16:21:55 +0100
committerAmaury Pouly <amaury.pouly@gmail.com>2017-01-07 15:52:33 +0100
commit456a3fc952d34a3e8781ca100851e6253c537109 (patch)
tree8406a3e38eb8a399a0bb155314be0ddc2d31e5a1 /utils
parent950f4bdc027cb8c83fd2145590549fdcf5522078 (diff)
downloadrockbox-456a3fc952d34a3e8781ca100851e6253c537109.tar.gz
rockbox-456a3fc952d34a3e8781ca100851e6253c537109.zip
imxtools: various fixes for Windows
Don't use colors since the terminal doesn't support it. Also packing is broken on MinGW so use #pragma pack when compiling for windows, this is also supported by MSCV. Change-Id: I635649d52ed5f2e0af46cb9ca2ec325955b2ddb2
Diffstat (limited to 'utils')
-rw-r--r--utils/imxtools/scsitools/misc.c5
-rw-r--r--utils/imxtools/scsitools/stmp_scsi.c8
-rw-r--r--utils/imxtools/scsitools/stmp_scsi.h41
-rw-r--r--utils/nwztools/scsitools/misc.c5
-rw-r--r--utils/scsi/rbscsi.c22
-rw-r--r--utils/scsi/rbscsi.h15
6 files changed, 81 insertions, 15 deletions
diff --git a/utils/imxtools/scsitools/misc.c b/utils/imxtools/scsitools/misc.c
index 108235e7fd..36eae198e5 100644
--- a/utils/imxtools/scsitools/misc.c
+++ b/utils/imxtools/scsitools/misc.c
@@ -32,7 +32,12 @@ char GREEN[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '2', 0x6d, '\0' };
32char YELLOW[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '3', 0x6d, '\0' }; 32char YELLOW[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '3', 0x6d, '\0' };
33char BLUE[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '4', 0x6d, '\0' }; 33char BLUE[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '4', 0x6d, '\0' };
34 34
35#if defined(_WIN32) || defined(__WIN32__)
36/* disable colors on Windows */
37static bool g_color_enable = false;
38#else
35static bool g_color_enable = true; 39static bool g_color_enable = true;
40#endif
36 41
37void *xmalloc(size_t s) 42void *xmalloc(size_t s)
38{ 43{
diff --git a/utils/imxtools/scsitools/stmp_scsi.c b/utils/imxtools/scsitools/stmp_scsi.c
index e1d0dc4c77..8daeb8a532 100644
--- a/utils/imxtools/scsitools/stmp_scsi.c
+++ b/utils/imxtools/scsitools/stmp_scsi.c
@@ -245,14 +245,22 @@ int stmp_get_logical_media_table(stmp_device_t dev, struct stmp_logical_media_ta
245 int len = sizeof(header); 245 int len = sizeof(header);
246 int ret = stmp_scsi_get_logical_table(dev, 0, &header, &len); 246 int ret = stmp_scsi_get_logical_table(dev, 0, &header, &len);
247 if(ret || len != sizeof(header)) 247 if(ret || len != sizeof(header))
248 {
249 stmp_debugf(dev, "Device returned the wrong size for logical media header: "
250 "%d bytes but expected %d\n", len, sizeof(header));
248 return -1; 251 return -1;
252 }
249 header.count = stmp_fix_endian16be(header.count); 253 header.count = stmp_fix_endian16be(header.count);
250 int sz = sizeof(header) + header.count * sizeof(struct scsi_stmp_logical_table_entry_t); 254 int sz = sizeof(header) + header.count * sizeof(struct scsi_stmp_logical_table_entry_t);
251 len = sz; 255 len = sz;
252 *table = malloc(sz); 256 *table = malloc(sz);
253 ret = stmp_scsi_get_logical_table(dev, header.count, &(*table)->header, &len); 257 ret = stmp_scsi_get_logical_table(dev, header.count, &(*table)->header, &len);
254 if(ret || len != sz) 258 if(ret || len != sz)
259 {
260 stmp_debugf(dev, "Device returned the wrong size for logical media table: "
261 "%d bytes but expected %d (%d entries)\n", len, sz, header.count);
255 return -1; 262 return -1;
263 }
256 (*table)->header.count = stmp_fix_endian16be((*table)->header.count); 264 (*table)->header.count = stmp_fix_endian16be((*table)->header.count);
257 for(unsigned i = 0; i < (*table)->header.count; i++) 265 for(unsigned i = 0; i < (*table)->header.count; i++)
258 (*table)->entry[i].size = stmp_fix_endian64be((*table)->entry[i].size); 266 (*table)->entry[i].size = stmp_fix_endian64be((*table)->entry[i].size);
diff --git a/utils/imxtools/scsitools/stmp_scsi.h b/utils/imxtools/scsitools/stmp_scsi.h
index 56068b5d4c..e85122041f 100644
--- a/utils/imxtools/scsitools/stmp_scsi.h
+++ b/utils/imxtools/scsitools/stmp_scsi.h
@@ -25,6 +25,17 @@
25#include <stdbool.h> 25#include <stdbool.h>
26#include "rbscsi.h" 26#include "rbscsi.h"
27 27
28#if defined(_WIN32) || defined(__WIN32__)
29/* Mingw has a curious behaviour: it packs only the last field, see
30 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52991 */
31#pragma pack(push)
32#pragma pack(1)
33#define RB_POP_PACK
34#define RB_PACKED
35#else
36#define RB_PACKED __attribute__((packed))
37#endif
38
28/** 39/**
29 * Low-Level SCSI stuff 40 * Low-Level SCSI stuff
30 */ 41 */
@@ -57,17 +68,17 @@ struct scsi_stmp_protocol_version_t
57{ 68{
58 uint8_t major; 69 uint8_t major;
59 uint8_t minor; 70 uint8_t minor;
60} __attribute__((packed)); 71} RB_PACKED;
61 72
62struct scsi_stmp_rom_rev_id_t 73struct scsi_stmp_rom_rev_id_t
63{ 74{
64 uint16_t rev; /* big-endian */ 75 uint16_t rev; /* big-endian */
65} __attribute__((packed)); 76} RB_PACKED;
66 77
67struct scsi_stmp_chip_major_rev_id_t 78struct scsi_stmp_chip_major_rev_id_t
68{ 79{
69 uint16_t rev; /* big-endian */ 80 uint16_t rev; /* big-endian */
70} __attribute__((packed)); 81} RB_PACKED;
71 82
72struct scsi_stmp_logical_table_entry_t 83struct scsi_stmp_logical_table_entry_t
73{ 84{
@@ -75,7 +86,7 @@ struct scsi_stmp_logical_table_entry_t
75 uint8_t type; 86 uint8_t type;
76 uint8_t tag; 87 uint8_t tag;
77 uint64_t size; /* big-endian */ 88 uint64_t size; /* big-endian */
78} __attribute__((packed)); 89} RB_PACKED;
79 90
80#define SCSI_STMP_DRIVE_TYPE_USER 0 91#define SCSI_STMP_DRIVE_TYPE_USER 0
81#define SCSI_STMP_DRIVE_TYPE_SYSTEM 1 92#define SCSI_STMP_DRIVE_TYPE_SYSTEM 1
@@ -87,7 +98,7 @@ struct scsi_stmp_logical_table_entry_t
87struct scsi_stmp_logical_table_header_t 98struct scsi_stmp_logical_table_header_t
88{ 99{
89 uint16_t count; /* big-endian */ 100 uint16_t count; /* big-endian */
90} __attribute__((packed)); 101} RB_PACKED;
91 102
92#define SCSI_STMP_MEDIA_INFO_NR_DRIVES 0 /** Number of drives (obsolete) */ 103#define SCSI_STMP_MEDIA_INFO_NR_DRIVES 0 /** Number of drives (obsolete) */
93#define SCSI_STMP_MEDIA_INFO_SIZE 1 /** Total size (bytes) */ 104#define SCSI_STMP_MEDIA_INFO_SIZE 1 /** Total size (bytes) */
@@ -127,12 +138,12 @@ struct scsi_stmp_logical_table_header_t
127struct scsi_stmp_logical_media_info_type_t 138struct scsi_stmp_logical_media_info_type_t
128{ 139{
129 uint8_t type; 140 uint8_t type;
130} __attribute__((packed)); 141} RB_PACKED;
131 142
132struct scsi_stmp_logical_media_info_manufacturer_t 143struct scsi_stmp_logical_media_info_manufacturer_t
133{ 144{
134 uint32_t type; /* big-endian */ 145 uint32_t type; /* big-endian */
135} __attribute__((packed)); 146} RB_PACKED;
136 147
137#define SCSI_STMP_DRIVE_INFO_SECTOR_SIZE 0 /** Sector Size (bytes) */ 148#define SCSI_STMP_DRIVE_INFO_SECTOR_SIZE 0 /** Sector Size (bytes) */
138#define SCSI_STMP_DRIVE_INFO_ERASE_SIZE 1 /** Erase Size (bytes) */ 149#define SCSI_STMP_DRIVE_INFO_ERASE_SIZE 1 /** Erase Size (bytes) */
@@ -170,29 +181,29 @@ struct scsi_stmp_logical_media_info_manufacturer_t
170struct scsi_stmp_logical_drive_info_sector_t 181struct scsi_stmp_logical_drive_info_sector_t
171{ 182{
172 uint32_t size; /* big-endian */ 183 uint32_t size; /* big-endian */
173} __attribute__((packed)); 184} RB_PACKED;
174 185
175struct scsi_stmp_logical_drive_info_count_t 186struct scsi_stmp_logical_drive_info_count_t
176{ 187{
177 uint64_t count; /* big-endian */ 188 uint64_t count; /* big-endian */
178} __attribute__((packed)); 189} RB_PACKED;
179 190
180struct scsi_stmp_logical_drive_info_size_t 191struct scsi_stmp_logical_drive_info_size_t
181{ 192{
182 uint64_t size; /* big-endian */ 193 uint64_t size; /* big-endian */
183} __attribute__((packed)); 194} RB_PACKED;
184 195
185struct scsi_stmp_logical_drive_info_type_t 196struct scsi_stmp_logical_drive_info_type_t
186{ 197{
187 uint8_t type; 198 uint8_t type;
188} __attribute__((packed)); 199} RB_PACKED;
189 200
190struct scsi_stmp_logical_drive_info_version_t 201struct scsi_stmp_logical_drive_info_version_t
191{ 202{
192 uint16_t major; 203 uint16_t major;
193 uint16_t minor; 204 uint16_t minor;
194 uint16_t revision; 205 uint16_t revision;
195} __attribute__((packed)); 206} RB_PACKED;
196 207
197struct stmp_device_t; 208struct stmp_device_t;
198typedef struct stmp_device_t *stmp_device_t; 209typedef struct stmp_device_t *stmp_device_t;
@@ -253,7 +264,11 @@ struct stmp_logical_media_table_t
253{ 264{
254 struct scsi_stmp_logical_table_header_t header; 265 struct scsi_stmp_logical_table_header_t header;
255 struct scsi_stmp_logical_table_entry_t entry[]; 266 struct scsi_stmp_logical_table_entry_t entry[];
256}__attribute__((packed)) table; 267}RB_PACKED table;
268
269#ifdef RB_POP_PACK
270#pragma pack(pop)
271#endif
257 272
258struct stmp_logical_media_info_t 273struct stmp_logical_media_info_t
259{ 274{
diff --git a/utils/nwztools/scsitools/misc.c b/utils/nwztools/scsitools/misc.c
index 108235e7fd..36eae198e5 100644
--- a/utils/nwztools/scsitools/misc.c
+++ b/utils/nwztools/scsitools/misc.c
@@ -32,7 +32,12 @@ char GREEN[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '2', 0x6d, '\0' };
32char YELLOW[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '3', 0x6d, '\0' }; 32char YELLOW[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '3', 0x6d, '\0' };
33char BLUE[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '4', 0x6d, '\0' }; 33char BLUE[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '4', 0x6d, '\0' };
34 34
35#if defined(_WIN32) || defined(__WIN32__)
36/* disable colors on Windows */
37static bool g_color_enable = false;
38#else
35static bool g_color_enable = true; 39static bool g_color_enable = true;
40#endif
36 41
37void *xmalloc(size_t s) 42void *xmalloc(size_t s)
38{ 43{
diff --git a/utils/scsi/rbscsi.c b/utils/scsi/rbscsi.c
index aa62ba0118..8a263f29e6 100644
--- a/utils/scsi/rbscsi.c
+++ b/utils/scsi/rbscsi.c
@@ -151,13 +151,33 @@ void rb_scsi_close(rb_scsi_device_t dev)
151 151
152/* Windpws */ 152/* Windpws */
153#elif defined(RB_SCSI_WINDOWS) 153#elif defined(RB_SCSI_WINDOWS)
154/* return either path or something allocated with malloc() */
155static const char *map_to_physical_drive(const char *path, unsigned flags, void *user,
156 rb_scsi_printf_t printf)
157{
158 /* don't do anything if path starts with '\' */
159 if(path[0] == '\\')
160 return path;
161 /* Convert to UNC path (C: -> \\.\C:) otherwise it won't work) */
162 char *unc_path = malloc(strlen(path) + 5);
163 sprintf(unc_path, "\\\\.\\%s", path);
164 if(flags & RB_SCSI_DEBUG)
165 printf(user, "rb_scsi: map to UNC path: %s\n", unc_path);
166 return unc_path;
167}
168
154rb_scsi_device_t rb_scsi_open(const char *path, unsigned flags, void *user, 169rb_scsi_device_t rb_scsi_open(const char *path, unsigned flags, void *user,
155 rb_scsi_printf_t printf) 170 rb_scsi_printf_t printf)
156{ 171{
157 if(printf == NULL) 172 if(printf == NULL)
158 printf = misc_std_printf; 173 printf = misc_std_printf;
159 HANDLE h = CreateFileA(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 174 /* magic to auto-detect physical drive */
175 const char *open_path = map_to_physical_drive(path, flags, user, printf);
176 HANDLE h = CreateFileA(open_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
160 NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL); 177 NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL);
178 /* free path if it was allocated */
179 if(open_path != path)
180 free((char *)open_path);
161 if(h == INVALID_HANDLE_VALUE) 181 if(h == INVALID_HANDLE_VALUE)
162 { 182 {
163 if(flags & RB_SCSI_DEBUG) 183 if(flags & RB_SCSI_DEBUG)
diff --git a/utils/scsi/rbscsi.h b/utils/scsi/rbscsi.h
index 2b56aabad2..c7345a6cdf 100644
--- a/utils/scsi/rbscsi.h
+++ b/utils/scsi/rbscsi.h
@@ -66,7 +66,20 @@ struct rb_scsi_raw_cmd_t
66}; 66};
67 67
68/* open a device, returns a handle or NULL on error 68/* open a device, returns a handle or NULL on error
69 * the caller can optionally provide an error printing function */ 69 * the caller can optionally provide an error printing function
70 *
71 * Linux:
72 * Path must be the block device, typically /dev/sdX and the program
73 * must have the permission to open it in read/write mode.
74 *
75 * Windows:
76 * If the path starts with '\', it will be use as-is. This allows to use
77 * paths such as \\.\PhysicalDriveX or \\.\ScsiX
78 * Alternatively, the code will try to map a logical drive (such as 'C:') to
79 * the correspoding physical drive.
80 * In any case, on recent windows, the program needs to be started with
81 * Administrator privileges.
82 */
70rb_scsi_device_t rb_scsi_open(const char *path, unsigned flags, void *user, 83rb_scsi_device_t rb_scsi_open(const char *path, unsigned flags, void *user,
71 rb_scsi_printf_t printf); 84 rb_scsi_printf_t printf);
72/* performs a raw transfer, returns !=0 on error */ 85/* performs a raw transfer, returns !=0 on error */