summaryrefslogtreecommitdiffstats
path: root/utils/meizu_dfu/meizu_dfu.c
blob: 0e32ea8d35190ac6c764adf770eca9b1cf3307a3 (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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2008 by William Poetra Yoga Hadisoeseno and Frank Gevaerts
 *
 * 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 <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <libgen.h>

#include <usb.h>

#define	bswap_16(value)  \
 	((((value) & 0xff) << 8) | ((value) >> 8))

#define	bswap_32(value)	\
 	(((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
 	(uint32_t)bswap_16((uint16_t)((value) >> 16)))

#define host_to_le32(_x) bswap_32(htonl(_x))
#define host_to_le16(_x) bswap_16(htons(_x))

void usage()
{
  fprintf(stderr, "usage: meizu_dfu m3 <SST39VF800.dfu> <M3.EBN>\n");
  fprintf(stderr, "       meizu_dfu m6 <SST39VF800.dfu> <M6.EBN>\n");
  fprintf(stderr, "       meizu_dfu m6sl <updateNAND_BE_070831.dfu> <M6SL.EBN>\n");
  exit(1);
}

uint32_t crc32(char *data, int len, uint32_t poly, uint32_t init)
{
  uint32_t crc_table[256];
  uint32_t crc, t;
  int i, j;

  // generate the table
  for (i = 0; i < 256; ++i) {
    t = i;
    for (j = 0; j < 8; ++j)
      if (t & 1)
        t = (t >> 1) ^ poly;
      else
        t >>= 1;
    crc_table[i] = t;
  }

  // calculate the crc
  crc = init;
  for (i = 0; i < len; ++i)
    crc = (crc >> 8) ^ crc_table[(crc^data[i]) & 0xff];

  return crc;
}

typedef struct {
  char *name;
  char *data;
  int len;
} image_data_t;

typedef struct {
  int delay;
  int pre_off;
  uint32_t pre_sig;
  uint16_t suf_dev;
  uint16_t suf_prod;
  uint16_t suf_ven;
  uint16_t suf_dfu;
  char suf_sig[3];
  uint8_t suf_len;
} image_attr_t;

#define BLOCK_SIZE 2048
#define DFU_TIMEOUT 0xa000
#define DFU_CRC_POLY 0xedb88320
#define DFU_INIT_CRC 0xffffffff

#define USB_VID_SAMSUNG 0x0419
#define USB_PID_M6SL    0x0145
#define USB_PID_M3_M6   0x0141

void init_img(image_data_t *img, const char *filename, image_attr_t *attr)
{
  int fd, len, i, readlen;
  struct stat statbuf;
  char buf[BLOCK_SIZE];
  uint32_t dfu_crc;
  uint32_t le_len;

  printf("Reading %s...", filename);

  stat(filename, &statbuf);
  len = statbuf.st_size;

  img->name = basename(strdup(filename));
  img->data = malloc(len + 16);
  img->len = len + 16;

  fd = open(filename, O_RDONLY);
  for (i = 0; i < len; i += BLOCK_SIZE) {
    readlen = ((len - i) < BLOCK_SIZE) ? (len - i) : BLOCK_SIZE;
    read(fd, buf, readlen);
    memcpy(img->data + i, buf, readlen);
  }
  close(fd);

  le_len = host_to_le32(img->len);
  // patch the data size in after the signature
  memcpy(img->data + attr->pre_off + 4, &le_len, 4);

  /* convert to little endian */
  attr->suf_dev = host_to_le16(attr->suf_dev);
  attr->suf_prod = host_to_le16(attr->suf_prod);
  attr->suf_ven = host_to_le16(attr->suf_ven);
  attr->suf_dfu = host_to_le16(attr->suf_dfu);

  // append the suffix (excluding the checksum)
  memcpy(img->data + len, &attr->suf_dev, 2);
  memcpy(img->data + len + 2, &attr->suf_prod, 2);
  memcpy(img->data + len + 4, &attr->suf_ven, 2);
  memcpy(img->data + len + 6, &attr->suf_dfu, 2);
  memcpy(img->data + len + 8, &attr->suf_sig, 3);
  memcpy(img->data + len + 11, &attr->suf_len, 1);

  dfu_crc = host_to_le32(crc32(img->data, len + 12, DFU_CRC_POLY, DFU_INIT_CRC));
  memcpy(img->data + len + 12, &dfu_crc, 4);

#if 0
  FILE *f = fopen(img->name, "w");
  fwrite(img->data, len + 16, 1, f);
  fclose(f);
#endif

  printf("OK\n");
}

usb_dev_handle *usb_dev_open(uint16_t dfu_vid, uint16_t dfu_pid)
{
  struct usb_bus *bus;
  struct usb_device *dev;
  usb_dev_handle *device;

  printf("USB initialization...");

  usb_init();
  usb_find_busses();
  usb_find_devices();

  for (bus = usb_get_busses(); bus != NULL; bus = bus->next)
    for (dev = bus->devices; dev != NULL; dev = dev->next)
      if (dev->descriptor.idVendor == dfu_vid
          && dev->descriptor.idProduct == dfu_pid)
        goto found;

  printf("\nNo device found, exiting.\n");
  exit(1);

found:
  printf(" Device found.\n");
  device = usb_open(dev);
  usb_claim_interface(device, 0);
  return device;
}

void usb_mimic_windows(usb_dev_handle *device)
{
  char data[1024];

  usb_control_msg(device, 0x80, 0x06, 0x0100, 0x0000, data, 0x0012, DFU_TIMEOUT);
  usb_control_msg(device, 0x80, 0x06, 0x0200, 0x0000, data, 0x0009, DFU_TIMEOUT);
  usb_control_msg(device, 0x80, 0x06, 0x0200, 0x0000, data, 0x001b, DFU_TIMEOUT);
  usb_control_msg(device, 0x80, 0x06, 0x0100, 0x0000, data, 0x0040, DFU_TIMEOUT);
  usb_control_msg(device, 0x80, 0x06, 0x0100, 0x0000, data, 0x0012, DFU_TIMEOUT);
  usb_control_msg(device, 0x80, 0x06, 0x0200, 0x0000, data, 0x0009, DFU_TIMEOUT);
  usb_control_msg(device, 0x80, 0x06, 0x0300, 0x0000, data, 0x00ff, DFU_TIMEOUT);
  usb_control_msg(device, 0x80, 0x06, 0x0303, 0x0409, data, 0x00ff, DFU_TIMEOUT);
  usb_control_msg(device, 0x80, 0x06, 0x0200, 0x0000, data, 0x00ff, DFU_TIMEOUT);
  usb_control_msg(device, 0x80, 0x06, 0x0300, 0x0000, data, 0x00ff, DFU_TIMEOUT);
  usb_control_msg(device, 0x80, 0x06, 0x0302, 0x0409, data, 0x00ff, DFU_TIMEOUT);
  usb_control_msg(device, 0x80, 0x06, 0x0300, 0x0000, data, 0x00ff, DFU_TIMEOUT);
  usb_control_msg(device, 0x80, 0x06, 0x0302, 0x0409, data, 0x00ff, DFU_TIMEOUT);
  usb_control_msg(device, 0x80, 0x06, 0x0100, 0x0000, data, 0x0012, DFU_TIMEOUT);
  usb_control_msg(device, 0x80, 0x06, 0x0200, 0x0000, data, 0x0209, DFU_TIMEOUT);
}

void usb_dev_close(usb_dev_handle *device)
{
  printf("Releasing interface...");

  usb_release_interface(device, 0);

  printf(" OK\n");
}

enum DFU_REQUEST {
  DFU_DETACH = 0,
  DFU_DOWNLOAD,
  DFU_UPLOAD,
  DFU_GETSTATUS,
  DFU_CLRSTATUS,
  DFU_GETSTATE,
  DFU_ABORT
};

void get_cpu(usb_dev_handle *device)
{
  char data[64];
  int req_out_if = USB_ENDPOINT_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  int len;

  printf("GET CPU");

  // check for "S5L8700 Rev.1"
  len = usb_control_msg(device, req_out_if, 0xff, 0x0002, 0, data, 0x003f, DFU_TIMEOUT);
  if (len < 0) {
    printf("\nError trying to get CPU model, exiting.\n");
    exit(1);
  }

  memset(data + len, 0, 64 - len);
  printf(", got: %s\n", data);
}

void send_file(usb_dev_handle *device, image_data_t *img)
{
  char dfu_ret[6];
  char *data;
  int req_out_if = USB_ENDPOINT_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  int req_in_if = USB_ENDPOINT_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  int len, idx, writelen, i;

  printf("Sending %s... ", img->name);

  len = img->len;
  data = img->data;

  // loop for the file
  for (i = 0, idx = 0; i < len; i += BLOCK_SIZE, ++idx) {
    writelen = ((len - i) < BLOCK_SIZE) ? (len - i) : BLOCK_SIZE;
    usb_control_msg(device, req_out_if, DFU_DOWNLOAD, idx, 0, data + i, writelen, DFU_TIMEOUT);
    dfu_ret[4] = 0x00;
    while (dfu_ret[4] != 0x05)
      usb_control_msg(device, req_in_if, DFU_GETSTATUS, 0, 0, dfu_ret, 6, DFU_TIMEOUT);
    printf("#");
  }

  usb_control_msg(device, req_out_if, DFU_DOWNLOAD, idx, 0, NULL, 0, DFU_TIMEOUT);
  dfu_ret[4] = 0x00;
  while (dfu_ret[4] != 0x07)
    usb_control_msg(device, req_in_if, DFU_GETSTATUS, 0, 0, dfu_ret, 6, DFU_TIMEOUT);

  printf(" OK\n");
}

void clear_status(usb_dev_handle *device)
{
  char dfu_ret[6];
  int usb_in_if = USB_ENDPOINT_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  int usb_out_if = USB_ENDPOINT_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;

  printf("Clearing status...");

  dfu_ret[4] = 0x00;
  while (dfu_ret[4] != 0x08)
    usb_control_msg(device, usb_in_if, DFU_GETSTATUS, 0, 0, dfu_ret, 6, DFU_TIMEOUT);
  usb_control_msg(device, usb_out_if, DFU_CLRSTATUS, 0, 0, NULL, 0, DFU_TIMEOUT);

  printf(" OK\n");
}

void dfu_detach(usb_dev_handle *device)
{
  char usb_ret[4];
  int usb_in_oth = USB_ENDPOINT_IN | USB_TYPE_CLASS | USB_RECIP_OTHER;
  int usb_out_oth = USB_ENDPOINT_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER;

  printf("Detaching...");

  usb_control_msg(device, usb_in_oth, DFU_DETACH, 0x0000, 3, usb_ret, 4, DFU_TIMEOUT);
  usb_control_msg(device, usb_out_oth, DFU_DOWNLOAD, 0x0010, 3, NULL, 0, DFU_TIMEOUT);
  usb_control_msg(device, usb_in_oth, DFU_DETACH, 0x0000, 3, usb_ret, 4, DFU_TIMEOUT);
  usb_control_msg(device, usb_in_oth, DFU_DETACH, 0x0000, 3, usb_ret, 4, DFU_TIMEOUT);
  usb_control_msg(device, usb_in_oth, DFU_DETACH, 0x0000, 3, usb_ret, 4, DFU_TIMEOUT);
  usb_control_msg(device, usb_in_oth, DFU_DETACH, 0x0000, 3, usb_ret, 4, DFU_TIMEOUT);
  usb_control_msg(device, usb_in_oth, DFU_DETACH, 0x0000, 3, usb_ret, 4, DFU_TIMEOUT);

  printf(" OK\n");
}

void dfu_m3_m6(char *file1, char *file2)
{
  image_data_t img1, img2;
  image_attr_t attr1, attr2;
  usb_dev_handle *device;

  attr1.delay = 1000;
  attr1.pre_off = 0x20;
  attr1.pre_sig = 0x44465543;
  attr1.suf_dev = 0x0100;
  attr1.suf_prod = 0x0140;
  attr1.suf_ven = 0x0419;
  attr1.suf_dfu = 0x0100;
  memcpy(attr1.suf_sig, "RON", 3);
  attr1.suf_len = 0x10;

  attr2.delay = 1000;
  attr2.pre_off = 0x20;
  attr2.pre_sig = 0x44465543;
  attr2.suf_dev = 0x0100;
  attr2.suf_prod = 0x0140;
  attr2.suf_ven = 0x0419;
  attr2.suf_dfu = 0x0100;
  memcpy(attr2.suf_sig, "UFD", 3);
  attr2.suf_len = 0x10;

  init_img(&img1, file1, &attr1);
  init_img(&img2, file2, &attr2);

  device = usb_dev_open(USB_VID_SAMSUNG, USB_PID_M3_M6);
//  usb_mimic_windows();
  get_cpu(device);
  get_cpu(device);
  send_file(device, &img1);

  printf("Wait a sec (literally)...");
  sleep(1);
  printf(" OK\n");

  clear_status(device);
  get_cpu(device);
  send_file(device, &img2);
  dfu_detach(device);
  usb_dev_close(device);
}

void dfu_m6sl(char *file1, char *file2)
{
  image_data_t img1, img2;
  image_attr_t attr1, attr2;
  usb_dev_handle *device;

  attr1.delay = 1000;
  attr1.pre_off = 0x20;
  attr1.pre_sig = 0x44465543;
  attr1.suf_dev = 0x0100;
  attr1.suf_prod = 0x0140;
  attr1.suf_ven = 0x0419;
  attr1.suf_dfu = 0x0100;
  memcpy(attr1.suf_sig, "UFD", 3);
  attr1.suf_len = 0x10;

  attr2.delay = 1000;
  attr2.pre_off = 0x20;
  attr2.pre_sig = 0x44465543;
  attr2.suf_dev = 0x0100;
  attr2.suf_prod = 0x0140;
  attr2.suf_ven = 0x0419;
  attr2.suf_dfu = 0x0100;
  memcpy(attr2.suf_sig, "UFD", 3);
  attr2.suf_len = 0x10;

  init_img(&img1, file1, &attr1);
  init_img(&img2, file2, &attr2);

  device = usb_dev_open(USB_VID_SAMSUNG, USB_PID_M6SL);
  get_cpu(device);
  get_cpu(device);
  send_file(device, &img1);

  printf("Wait a sec (literally)...");
  sleep(1);
  printf(" OK\n");
  usb_dev_close(device);

  device = usb_dev_open(USB_VID_SAMSUNG, USB_PID_M6SL);
  get_cpu(device);
  get_cpu(device);
  send_file(device, &img2);
  dfu_detach(device);
  usb_dev_close(device);
}


int main(int argc, char **argv)
{
  if (argc != 4)
    usage();

  setvbuf(stdout, NULL, _IONBF, 0);

  if (!strcmp(argv[1], "m3"))
    dfu_m3_m6(argv[2], argv[3]);
  else if (!strcmp(argv[1], "m6"))
    dfu_m3_m6(argv[2], argv[3]);
  else if (!strcmp(argv[1], "m6sl"))
    dfu_m6sl(argv[2], argv[3]);
  else
    usage();

  return 0;
}