summaryrefslogtreecommitdiffstats
path: root/utils/rk27utils/rk27load/main.c
blob: e34d7667e3b02cb84518b18f1e8538a4623605c0 (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
160
161
162
163
164
165
166
167
168
169
170
171
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>

#include <libusb.h>

#include "rk27load.h"
#include "common.h"
#include "stage1_upload.h"
#include "stage2_upload.h"
#include "stage3_upload.h"

#define VERSION "v0.2"

enum {
      NONE = 0,
      ENCODE_S1 = 1,
      ENCODE_S2 = 2
};

static void usage(char *name)
{
    printf("usage: (sudo) %s [-e1 -e2] -s1 stage1.bin -s2 stage2.bin -s3 usercode.bin\n", name);
    printf("stage1.bin - binary of the stage1 (sdram init)\n");
    printf("stage2.bin - binary of the stage2 bootloader\n");
    printf("usercode.bin - binary of the custom usercode\n");
    printf("\n");
    printf("options:\n");
    printf("-e1 - encode stage1 bootloader\n");
    printf("-e2 - encode stage2 bootloader\n");    
}

int main(int argc, char **argv)
{
    libusb_device_handle *hdev;
    char *filenames[3];
    int i=1, action=0, ret=0;

    while (i < argc)
    {
        if (strcmp(argv[i],"-e1") == 0)
        {
            action |= ENCODE_S1;
            i++;
        }
        else if (strcmp(argv[i],"-e2") == 0)
        {
            action |= ENCODE_S2;
            i++;
        }
        else if (strcmp(argv[i],"-s1") == 0)
        {
            i++;
            if (i == argc)
            {
                usage(argv[0]);
                return -1;
            }
            filenames[0] = argv[i];
            printf("%s", argv[i]);
            i++;
        }
        else if (strcmp(argv[i],"-s2") == 0)
        {
            i++;
            if (i == argc)
            {
                usage(argv[0]);
                return -2;
            }
            filenames[1] = argv[i];
            i++;
        }
        else if (strcmp(argv[i],"-s3") == 0)
        {
            i++;
            if (i == argc)
            {
                usage(argv[0]);
                return -3;
            }
            filenames[2] = argv[i];
            i++;
        }
        else
        {
            usage(argv[0]);
            return -4;
        }
    }

    if(filenames[0] == NULL || filenames[1] == NULL || filenames[2] == NULL)
    {
        usage(argv[0]);
        return -5;
    }

 
    fprintf(stderr,"rk27load " VERSION "\n");
    fprintf(stderr,"(C) Marcin Bukat 2011\n");
    fprintf(stderr,"Based on rk27load ver. 0.1 written by AleMaxx (alemaxx at hotmail.de)\n\n");
    fprintf(stderr,"This is free software; see the source for copying conditions.  There is NO\n");
    fprintf(stderr,"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");

    /* initialize libusb */
    libusb_init(NULL);

    /* configure device */
    fprintf(stderr, "[info]: Initializing device... ");
    hdev = libusb_open_device_with_vid_pid(NULL, VENDORID, PRODUCTID);

    if (hdev == NULL)
    {
        fprintf(stderr, "\n[error]: Could not find rockchip device\n");
        ret = -2;
        goto finish;
    }

    ret = libusb_set_configuration(hdev, 1);
    if (ret < 0)
    {
        fprintf(stderr, "\n[error]: Could not select configuration (1)\n");
        ret = -3;
        goto finish;
    }

    ret = libusb_claim_interface(hdev, 0);
    if (ret < 0)
    {
        fprintf(stderr, "\n[error]: Could not claim interface #0\n");
        ret = -4;
        goto finish;
    }

    ret = libusb_set_interface_alt_setting(hdev, 0, 0);
    if (ret < 0)
    {
        fprintf(stderr, "\n[error]: Could not set alternate interface #0\n");
        ret = -5;
        goto finish;
    }

    fprintf(stderr, "done\n");


    ret = upload_stage1_code(hdev, filenames[0], (action & ENCODE_S1));
    if (ret < 0)
        goto finish;

    ret = upload_stage2_code(hdev, filenames[1], (action & ENCODE_S2));
    if (ret < 0)
        goto finish;

    ret = upload_stage3_code(hdev, filenames[2]);
    if (ret < 0)
        goto finish;

    /* done */
    ret = 0;

  finish:
    if (hdev != NULL)
        libusb_close(hdev);

    if (ret < 0)
        fprintf(stderr, "[error]: Error %d\n", ret);

    return ret;
}