summaryrefslogtreecommitdiff
path: root/utils/hwstub/tools/hwstub_dump.cpp
blob: ffa2832a550eb67797e8ebba1faf90e79ec06b26 (plain)
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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2016 by Amaury Pouly
 *
 * 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <stdbool.h>
#include <ctype.h>
#include <iostream>
#include "hwstub.hpp"
#include "hwstub_uri.hpp"

using namespace hwstub;

void usage(void)
{
    fprintf(stderr, "usage: hwstub_ump [options] <addr> <size>\n");
    fprintf(stderr, "options:\n");
    fprintf(stderr, "  --help/-h       Display this help\n");
    fprintf(stderr, "  --verbose/-v    Verbose output\n");
    fprintf(stderr, "  --dev/-d <uri>  Device URI (see below)\n");
    //hwstub::usage_uri(stdout);
    exit(1);
}

int main(int argc, char **argv)
{
    const char *uri = hwstub::uri::default_uri().full_uri().c_str();
    bool verbose = false;

    // parse command line
    while(1)
    {
        static struct option long_options[] =
        {
            {"help", no_argument, 0, 'h'},
            {"dev", required_argument, 0, 'd'},
            {"verbose", no_argument, 0, 'v'},
            {0, 0, 0, 0}
        };

        int c = getopt_long(argc, argv, "hd:v", long_options, NULL);
        if(c == -1)
            break;
        switch(c)
        {
            case -1:
                break;
            case 'v':
                verbose = true;
                break;
            case 'h':
                usage();
                break;
            case 'd':
                uri = optarg;
                break;
            default:
                abort();
        }
    }

    if(optind + 2 != argc)
        usage();

    char *end;
    unsigned long addr = strtoul(argv[optind], &end, 0);
    if(*end)
    {
        fprintf(stderr, "Invalid dump address\n");
        return 2;
    }
    unsigned long size = strtoul(argv[optind + 1], &end, 0);
    if(*end)
    {
        fprintf(stderr, "Invalid dump size\n");
        return 2;
    }

    // create usb context
    std::string errstr;
    std::shared_ptr<context> hwctx = uri::create_context(uri::uri(uri), &errstr);
    if(!hwctx)
    {
        fprintf(stderr, "Cannot create context: %s\n", errstr.c_str());
        return 1;
    }
    if(verbose)
        hwctx->set_debug(std::cerr);
    std::vector<std::shared_ptr<hwstub::device>> list;
    hwstub::error ret = hwctx->get_device_list(list);
    if(ret != hwstub::error::SUCCESS)
    {
        fprintf(stderr, "Cannot get device list: %d\n", (int)ret);
        return 1;
    }
    if(list.size() == 0)
    {
        fprintf(stderr, "No hwstub device detected!\n");
        return 1;
    }
    /* open first device */
    std::shared_ptr<hwstub::handle> hwdev;
    ret = list[0]->open(hwdev);
    if(ret != hwstub::error::SUCCESS)
    {
        fprintf(stderr, "Cannot open device: %d\n", (int)ret);
        return 1;
    }

    /* load */

    uint8_t *buffer = new uint8_t[size];
    size_t out_size = size;
    ret = hwdev->read(addr, buffer, out_size, false);
    if(ret != hwstub::error::SUCCESS || out_size != size)
    {
        fprintf(stderr, "Dump failed: %s, %zu/%zu\n", error_string(ret).c_str(),
            out_size, size);
        goto Lerr;
    }

    fwrite(buffer, 1, size, stdout);

    return 0;

    Lerr:
    // display log if handled
    fprintf(stderr, "Device log:\n");
    do
    {
        char buffer[128];
        size_t size = sizeof(buffer) - 1;
        hwstub::error err = hwdev->get_log(buffer, size);
        if(err != hwstub::error::SUCCESS)
            break;
        buffer[size] = 0;
        fprintf(stderr, "%s", buffer);
    }while(1);
    return 1;
}