-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdnw.c
218 lines (179 loc) · 4.23 KB
/
dnw.c
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
216
217
218
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include <usb.h>
#include <errno.h>
static struct usb_device *find_s3c2410()
{
struct usb_bus *busses;
struct usb_bus *bus;
struct usb_device *dev;
busses = usb_get_busses();
for (bus = busses; bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
if (dev->descriptor.idVendor == 0x5345
&& dev->descriptor.idProduct == 0x1234)
return dev;
}
}
return NULL;
}
enum { bufsize = 0x4000 };
int main(int argc, char *argv[])
{
struct usb_device *dev;
usb_dev_handle *udev;
ssize_t n, count;
char product[64];
const char *fn;
int fd;
char buf[bufsize];
off_t size, divisor, written;
time_t t0, t1, t;
int r;
if (argc != 2) {
fprintf(stderr, "Usage: %s ubi-file\n", argv[0]);
exit(1);
}
fn = argv[1];
usb_init();
usb_find_busses();
usb_find_devices();
dev = find_s3c2410();
if (!dev) {
fprintf(stderr, "no matching usb device found\n");
exit(1);
}
udev = usb_open(dev);
if (!udev) {
fprintf(stderr, "failed to open usb device %s/%s\n",
dev->bus->dirname, dev->filename);
exit(1);
}
r = usb_claim_interface(udev, 0);
if (r < 0) {
fprintf(stderr, "failed to claim usb device %s/%s: %s\n",
dev->bus->dirname, dev->filename, strerror(r));
exit(1);
}
if (!dev->descriptor.iProduct) {
fprintf(stderr, "no product name for usb device %s/%s\n",
dev->bus->dirname, dev->filename);
exit(1);
}
r = usb_get_string_simple(udev, dev->descriptor.iProduct, product, sizeof(product));
if (r < 0) {
fprintf(stderr, "unable to fetch product for usb device %s/%s: %s\n",
dev->bus->dirname, dev->filename, strerror(r));
exit(1);
}
printf("%s/%s: %s\n", dev->bus->dirname, dev->filename, product);
if (!dev->config) {
fprintf(stderr, "could not get configuration for usb device %s/%s\n",
dev->bus->dirname, dev->filename);
exit(1);
}
if ((fd = open(fn, O_RDONLY)) < 0) {
perror(fn);
exit(1);
}
if ((size = lseek(fd, 0L, SEEK_END)) == -1
|| lseek(fd, 0L, SEEK_SET) == -1) {
perror(fn);
exit(1);
}
divisor = size / 100;
if (!divisor)
divisor = 1;
printf("Downloading Image...\n");
fflush(stdout);
written = 0;
time(&t0);
t1 = t0;
while ((n = read(fd, buf, bufsize)) > 0) {
count = n;
n = usb_bulk_write(udev, 3, buf, count, 10000);
if (n < 0) {
perror("usb_bulk_write");
exit(1);
}
if (n != count) {
fprintf(stderr, "short usb_bulk_write, got %d, expected %d\n",
n, count);
exit(1);
}
written += n;
time(&t);
if (t - t1 >= 5) {
unsigned percent = (unsigned)(written / divisor);
unsigned speed = written / (t - t0) / 1000;
printf("Downloading image, %u%% done, %d kByte/sec\n", percent, speed);
fflush(stdout);
t1 = t;
}
}
if (n < 0) {
perror(fn);
exit(1);
}
printf("Starting N30 Programming...\n");
fflush(stdout);
time(&t0);
t1 = t0;
while ((n = usb_bulk_read(udev, 1, buf, sizeof(buf), 5000)) > 0) {
if (buf[3] == 0x7f) {
int i;
fprintf(stderr, "Download failed, status=[");
for (i = 0; i < n; i++) {
if (i)
putc(' ', stderr);
fprintf(stderr, "%02x", buf[i]);
}
fprintf(stderr, "]\n");
break;
}
if (buf[0] == 0x10) {
printf("Preparing...\n");
} else if (buf[0] == 0x20) {
time(&t);
if (t - t1 >= 5) {
printf("Flashing BINFS, %u%% done\n", buf[2]);
fflush(stdout);
t1 = t;
}
} else if (buf[0] == 0x40) {
time(&t);
if (buf[3] == 100 || t - t1 >= 5) {
printf("Flashing, %u%% done\n", buf[3]);
fflush(stdout);
t1 = t;
}
if (buf[3] == 100)
break;
} else {
int i;
for (i = 0; i < n; i++)
printf("%02x ", buf[i]);
printf("\n");
}
sleep(1);
}
if (n < 0 && errno != ETIMEDOUT) {
perror("usb read status");
exit(1);
}
printf("Rebooting...\n");
fflush(stdout);
memset(buf, 0, sizeof(buf));
buf[0] = 1;
n = usb_bulk_write(udev, 3, buf, 4, 10000);
if (n < 0) {
perror("usb write ack");
exit(1);
}
usb_release_interface(udev, 0);
exit(0);
}