main.c
4.11 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "libusb-1.0/libusb.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int Initialize_Device(libusb_context ** ctx, libusb_device_handle ** usb_device)
{
int ret = 0;
*usb_device = libusb_open_device_with_vid_pid(*ctx, 0x0a5c, 0x2763);
if (*usb_device == NULL)
{
return -1;
}
ret = libusb_claim_interface(*usb_device, 0);
if (ret)
{
printf("Failed to claim interface\n");
return ret;
}
return ret;
}
int ep_write(unsigned char *buf, int len, libusb_device_handle * usb_device)
{
int ret =
libusb_control_transfer(usb_device, LIBUSB_REQUEST_TYPE_VENDOR, 0,
len & 0xffff, len >> 16, NULL, 0, 1000);
int a_len;
ret = libusb_bulk_transfer(usb_device, 0x01, buf, len, &a_len, 100000);
printf("libusb_bulk_transfer returned %d\n", ret);
return a_len;
}
int ep_read(unsigned char *buf, int len, libusb_device_handle * usb_device)
{
int ret =
libusb_control_transfer(usb_device,
LIBUSB_REQUEST_TYPE_VENDOR |
LIBUSB_ENDPOINT_IN, 0, len & 0xffff,
len >> 16, buf, len, 1000);
return len;
}
int main(int argc, char *argv[])
{
int result;
libusb_context *ctx;
libusb_device_handle *usb_device;
unsigned char *txbuf;
int size;
int retcode;
FILE *fp1, *fp2;
char def1[] = "/usr/share/rpiboot/usbbootcode.bin";
char def2[] = "/usr/share/rpiboot/msd.elf";
char *stage1, *stage2;
char *dir;
struct MESSAGE_S {
int length;
unsigned char signature[20];
} message;
if(argc > 2)
{
stage1 = argv[1];
stage2 = argv[2];
}
else
{
stage1 = def1;
stage2 = def2;
}
fp1 = fopen(stage1, "rb");
if (fp1 == NULL)
{
printf("Cannot open file %s\n", stage1);
exit(-1);
}
fp2 = fopen(stage2, "rb");
if (fp2 == NULL)
{
printf("Cannot open file %s\n", stage2);
exit(-1);
}
if(strcmp(stage2 + strlen(stage2) - 4, ".elf"))
{
printf("Third stage needs to be .elf format\n");
exit(-1);
}
int ret = libusb_init(&ctx);
if (ret)
{
printf("Failed to initialise libUSB\n");
exit(-1);
}
libusb_set_debug(ctx, 0);
while (1)
{
FILE *fp, *fp_img = NULL;
struct libusb_device_descriptor desc;
printf("Waiting for BCM2835 ...\n");
// Wait for a device to get plugged in
do
{
result = Initialize_Device(&ctx, &usb_device);
if (result)
{
sleep(1);
}
}
while (result);
ret =
libusb_get_device_descriptor(libusb_get_device
(usb_device), &desc);
printf("Found serial = %d: writing file %s\n",
desc.iSerialNumber,
desc.iSerialNumber == 0 ? stage1 : stage2);
fp = desc.iSerialNumber == 0 ? fp1 : fp2;
fseek(fp, 0, SEEK_END);
message.length = ftell(fp);
fseek(fp, 0, SEEK_SET);
printf("Writing %d bytes of program data\n", message.length);
if(desc.iSerialNumber == 1 && argc == 4)
{
// Been given a filesystem image
fp_img = fopen(argv[3], "rb");
if(fp_img == NULL)
{
printf("Failed to open image %s\n", argv[3]);
exit(-1);
}
fseek(fp_img, 0, SEEK_END);
message.length += ftell(fp_img);
printf("Adding %d bytes of binary to end of elf\n", ftell(fp_img));
fseek(fp_img, 0, SEEK_SET);
}
txbuf = (unsigned char *)malloc(message.length);
if (txbuf == NULL)
{
printf("Failed to allocate memory\n");
exit(-1);
}
size = fread(txbuf, 1, message.length, fp);
if(fp_img)
{
size += fread(txbuf + size, 1, message.length - size, fp_img);
}
size =
ep_write((unsigned char *)&message, sizeof(message),
usb_device);
if (size != sizeof(message))
{
printf("Failed to write correct length, returned %d\n",
size);
exit(-1);
}
size = ep_write(txbuf, message.length, usb_device);
if (size != message.length)
{
printf("Failed to read correct length, returned %d\n",
size);
exit(-1);
}
sleep(1);
size =
ep_read((unsigned char *)&retcode, sizeof(retcode),
usb_device);
if (retcode == 0)
{
printf("Successful\n");
if(fp==fp2)
{
printf("Raspberry Pi is now a mass storage device, use lsblk to find it\n");
exit(0);
}
}
else
printf("Failed : 0x%x", retcode);
sleep(1);
libusb_close(usb_device);
}
libusb_exit(ctx);
return 0;
}