summaryrefslogtreecommitdiff
path: root/utils/hwstub/tools/hwstub_load.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/hwstub/tools/hwstub_load.cpp')
-rw-r--r--utils/hwstub/tools/hwstub_load.cpp94
1 files changed, 43 insertions, 51 deletions
diff --git a/utils/hwstub/tools/hwstub_load.cpp b/utils/hwstub/tools/hwstub_load.cpp
index 119f6d7c58..87737939c3 100644
--- a/utils/hwstub/tools/hwstub_load.cpp
+++ b/utils/hwstub/tools/hwstub_load.cpp
@@ -18,13 +18,16 @@
18 * KIND, either express or implied. 18 * KIND, either express or implied.
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21#include "hwstub.h"
22#include <stdio.h> 21#include <stdio.h>
23#include <stdlib.h> 22#include <stdlib.h>
24#include <string.h> 23#include <string.h>
25#include <getopt.h> 24#include <getopt.h>
26#include <stdbool.h> 25#include <stdbool.h>
27#include <ctype.h> 26#include <ctype.h>
27#include "hwstub.hpp"
28#include "hwstub_uri.hpp"
29
30using namespace hwstub;
28 31
29struct player_info_t 32struct player_info_t
30{ 33{
@@ -102,11 +105,12 @@ bool could_be_rockbox(unsigned char *buffer, size_t size)
102 105
103void usage(void) 106void usage(void)
104{ 107{
105 printf("usage: hwstub_load [options] <addr> <file>\n"); 108 printf("usage: hwstub::load [options] <addr> <file>\n");
106 printf("options:\n"); 109 printf("options:\n");
107 printf(" --help/-? Display this help\n"); 110 printf(" --help/-? Display this help\n");
108 printf(" --quiet/-q Quiet output\n"); 111 printf(" --quiet/-q Quiet output\n");
109 printf(" --type/-t <t> Override file type\n"); 112 printf(" --type/-t <t> Override file type\n");
113 printf(" --dev/-d <uri> Device URI (see below)\n");
110 printf("file types:\n"); 114 printf("file types:\n");
111 printf(" raw Load a raw binary blob\n"); 115 printf(" raw Load a raw binary blob\n");
112 printf(" rockbox Load a rockbox image produced by scramble\n"); 116 printf(" rockbox Load a rockbox image produced by scramble\n");
@@ -115,14 +119,15 @@ void usage(void)
115 for(int i = 0; players[i].name; i++) 119 for(int i = 0; players[i].name; i++)
116 printf(" %s", players[i].name); 120 printf(" %s", players[i].name);
117 printf("\n"); 121 printf("\n");
122 //hwstub::usage_uri(stdout);
118 exit(1); 123 exit(1);
119} 124}
120 125
121int main(int argc, char **argv) 126int main(int argc, char **argv)
122{ 127{
123 bool quiet = false; 128 bool quiet = false;
124 struct hwstub_device_t *hwdev;
125 enum image_type_t type = IT_DETECT; 129 enum image_type_t type = IT_DETECT;
130 const char *uri = "usb:";
126 131
127 // parse command line 132 // parse command line
128 while(1) 133 while(1)
@@ -132,10 +137,11 @@ int main(int argc, char **argv)
132 {"help", no_argument, 0, '?'}, 137 {"help", no_argument, 0, '?'},
133 {"quiet", no_argument, 0, 'q'}, 138 {"quiet", no_argument, 0, 'q'},
134 {"type", required_argument, 0, 't'}, 139 {"type", required_argument, 0, 't'},
140 {"dev", required_argument, 0, 'd'},
135 {0, 0, 0, 0} 141 {0, 0, 0, 0}
136 }; 142 };
137 143
138 int c = getopt_long(argc, argv, "?qt:", long_options, NULL); 144 int c = getopt_long(argc, argv, "?qt:d:", long_options, NULL);
139 if(c == -1) 145 if(c == -1)
140 break; 146 break;
141 switch(c) 147 switch(c)
@@ -161,6 +167,9 @@ int main(int argc, char **argv)
161 return 1; 167 return 1;
162 } 168 }
163 break; 169 break;
170 case 'd':
171 uri = optarg;
172 break;
164 default: 173 default:
165 abort(); 174 abort();
166 } 175 }
@@ -220,61 +229,44 @@ int main(int argc, char **argv)
220 } 229 }
221 230
222 // create usb context 231 // create usb context
223 libusb_context *ctx; 232 std::string errstr;
224 libusb_init(&ctx); 233 std::shared_ptr<context> hwctx = uri::create_context(uri::uri(uri), &errstr);
225 libusb_set_debug(ctx, 3); 234 if(!hwctx)
226
227 // look for device
228 if(!quiet)
229 printf("Looking for device %#04x:%#04x...\n", HWSTUB_USB_VID, HWSTUB_USB_PID);
230
231 libusb_device_handle *handle = libusb_open_device_with_vid_pid(ctx,
232 HWSTUB_USB_VID, HWSTUB_USB_PID);
233 if(handle == NULL)
234 { 235 {
235 fprintf(stderr, "No device found\n"); 236 printf("Cannot create context: %s\n", errstr.c_str());
236 return 1; 237 return 1;
237 } 238 }
238 239 std::vector<std::shared_ptr<hwstub::device>> list;
239 // admin stuff 240 hwstub::error ret = hwctx->get_device_list(list);
240 libusb_device *mydev = libusb_get_device(handle); 241 if(ret != hwstub::error::SUCCESS)
241 if(!quiet)
242 { 242 {
243 printf("device found at %d:%d\n", 243 printf("Cannot get device list: %d\n", (int)ret);
244 libusb_get_bus_number(mydev),
245 libusb_get_device_address(mydev));
246 }
247 hwdev = hwstub_open(handle);
248 if(hwdev == NULL)
249 {
250 fprintf(stderr, "Cannot probe device!\n");
251 return 1; 244 return 1;
252 } 245 }
253 246 if(list.size() == 0)
254 // get hwstub information
255 struct hwstub_version_desc_t hwdev_ver;
256 int ret = hwstub_get_desc(hwdev, HWSTUB_DT_VERSION, &hwdev_ver, sizeof(hwdev_ver));
257 if(ret != sizeof(hwdev_ver))
258 { 247 {
259 fprintf(stderr, "Cannot get version!\n"); 248 printf("No hwstub device detected!\n");
260 goto Lerr; 249 return 1;
261 } 250 }
262 if(hwdev_ver.bMajor != HWSTUB_VERSION_MAJOR || hwdev_ver.bMinor < HWSTUB_VERSION_MINOR) 251 /* open first device */
252 std::shared_ptr<hwstub::handle> hwdev;
253 ret = list[0]->open(hwdev);
254 if(ret != hwstub::error::SUCCESS)
263 { 255 {
264 printf("Warning: this tool is possibly incompatible with your device:\n"); 256 printf("Cannot open device: %d\n", (int)ret);
265 printf("Device version: %d.%d.%d\n", hwdev_ver.bMajor, hwdev_ver.bMinor, hwdev_ver.bRevision); 257 return 1;
266 printf("Host version: %d.%d\n", HWSTUB_VERSION_MAJOR, HWSTUB_VERSION_MINOR);
267 } 258 }
268 259
269 ret = hwstub_rw_mem(hwdev, 0, addr, buffer, size); 260 size_t out_size = size;
270 if(ret != (int)size) 261 ret = hwdev->write(addr, buffer, out_size, false);
262 if(ret != hwstub::error::SUCCESS || out_size != size)
271 { 263 {
272 fprintf(stderr, "Image write failed: %d\n", ret); 264 fprintf(stderr, "Image write failed: %s, %zu/%zu\n", error_string(ret).c_str(),
265 out_size, size);
273 goto Lerr; 266 goto Lerr;
274 } 267 }
275 hwstub_jump(hwdev, addr); 268 hwdev->exec(addr, HWSTUB_EXEC_JUMP);
276 269
277 hwstub_release(hwdev);
278 return 0; 270 return 0;
279 271
280 Lerr: 272 Lerr:
@@ -283,13 +275,13 @@ int main(int argc, char **argv)
283 do 275 do
284 { 276 {
285 char buffer[128]; 277 char buffer[128];
286 int length = hwstub_get_log(hwdev, buffer, sizeof(buffer) - 1); 278 size_t size = sizeof(buffer) - 1;
287 if(length <= 0) 279 hwstub::error err = hwdev->get_log(buffer, size);
280 if(err != hwstub::error::SUCCESS)
288 break; 281 break;
289 buffer[length] = 0; 282 buffer[size] = 0;
290 fprintf(stderr, "%s", buffer); 283 fprintf(stderr, "%s", buffer);
291 }while(1); 284 }while(1);
292 hwstub_release(hwdev);
293 return 1; 285 return 1;
294} 286}
295 287