summaryrefslogtreecommitdiff
path: root/utils/hwstub/tools/hwstub_dump.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/hwstub/tools/hwstub_dump.cpp')
-rw-r--r--utils/hwstub/tools/hwstub_dump.cpp159
1 files changed, 159 insertions, 0 deletions
diff --git a/utils/hwstub/tools/hwstub_dump.cpp b/utils/hwstub/tools/hwstub_dump.cpp
new file mode 100644
index 0000000000..ffa2832a55
--- /dev/null
+++ b/utils/hwstub/tools/hwstub_dump.cpp
@@ -0,0 +1,159 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2016 by Amaury Pouly
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <getopt.h>
25#include <stdbool.h>
26#include <ctype.h>
27#include <iostream>
28#include "hwstub.hpp"
29#include "hwstub_uri.hpp"
30
31using namespace hwstub;
32
33void usage(void)
34{
35 fprintf(stderr, "usage: hwstub_ump [options] <addr> <size>\n");
36 fprintf(stderr, "options:\n");
37 fprintf(stderr, " --help/-h Display this help\n");
38 fprintf(stderr, " --verbose/-v Verbose output\n");
39 fprintf(stderr, " --dev/-d <uri> Device URI (see below)\n");
40 //hwstub::usage_uri(stdout);
41 exit(1);
42}
43
44int main(int argc, char **argv)
45{
46 const char *uri = hwstub::uri::default_uri().full_uri().c_str();
47 bool verbose = false;
48
49 // parse command line
50 while(1)
51 {
52 static struct option long_options[] =
53 {
54 {"help", no_argument, 0, 'h'},
55 {"dev", required_argument, 0, 'd'},
56 {"verbose", no_argument, 0, 'v'},
57 {0, 0, 0, 0}
58 };
59
60 int c = getopt_long(argc, argv, "hd:v", long_options, NULL);
61 if(c == -1)
62 break;
63 switch(c)
64 {
65 case -1:
66 break;
67 case 'v':
68 verbose = true;
69 break;
70 case 'h':
71 usage();
72 break;
73 case 'd':
74 uri = optarg;
75 break;
76 default:
77 abort();
78 }
79 }
80
81 if(optind + 2 != argc)
82 usage();
83
84 char *end;
85 unsigned long addr = strtoul(argv[optind], &end, 0);
86 if(*end)
87 {
88 fprintf(stderr, "Invalid dump address\n");
89 return 2;
90 }
91 unsigned long size = strtoul(argv[optind + 1], &end, 0);
92 if(*end)
93 {
94 fprintf(stderr, "Invalid dump size\n");
95 return 2;
96 }
97
98 // create usb context
99 std::string errstr;
100 std::shared_ptr<context> hwctx = uri::create_context(uri::uri(uri), &errstr);
101 if(!hwctx)
102 {
103 fprintf(stderr, "Cannot create context: %s\n", errstr.c_str());
104 return 1;
105 }
106 if(verbose)
107 hwctx->set_debug(std::cerr);
108 std::vector<std::shared_ptr<hwstub::device>> list;
109 hwstub::error ret = hwctx->get_device_list(list);
110 if(ret != hwstub::error::SUCCESS)
111 {
112 fprintf(stderr, "Cannot get device list: %d\n", (int)ret);
113 return 1;
114 }
115 if(list.size() == 0)
116 {
117 fprintf(stderr, "No hwstub device detected!\n");
118 return 1;
119 }
120 /* open first device */
121 std::shared_ptr<hwstub::handle> hwdev;
122 ret = list[0]->open(hwdev);
123 if(ret != hwstub::error::SUCCESS)
124 {
125 fprintf(stderr, "Cannot open device: %d\n", (int)ret);
126 return 1;
127 }
128
129 /* load */
130
131 uint8_t *buffer = new uint8_t[size];
132 size_t out_size = size;
133 ret = hwdev->read(addr, buffer, out_size, false);
134 if(ret != hwstub::error::SUCCESS || out_size != size)
135 {
136 fprintf(stderr, "Dump failed: %s, %zu/%zu\n", error_string(ret).c_str(),
137 out_size, size);
138 goto Lerr;
139 }
140
141 fwrite(buffer, 1, size, stdout);
142
143 return 0;
144
145 Lerr:
146 // display log if handled
147 fprintf(stderr, "Device log:\n");
148 do
149 {
150 char buffer[128];
151 size_t size = sizeof(buffer) - 1;
152 hwstub::error err = hwdev->get_log(buffer, size);
153 if(err != hwstub::error::SUCCESS)
154 break;
155 buffer[size] = 0;
156 fprintf(stderr, "%s", buffer);
157 }while(1);
158 return 1;
159}