summaryrefslogtreecommitdiffstats
path: root/libdpf/dpflib.c
blob: 3af8ebbee13726e85524983dbe6b68819941a5e4 (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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/** DPF access library for AX206 based HW
 *
 * 12/2010 <hackfin@section5.ch>
 *
 * This is an ugly hack, because we use existing SCSI vendor specific
 * extensions to pass on our requests to the DPF.
 *
 * One day we might use a proper protocol like netpp.
 *
 */

// FIXME: Put all those SCSI commands in one (wrapped) place.

#include "dpf.h"

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#ifdef DEBUG
#define DEB(x) x
#else
#define DEB(x)
#endif

extern AccessMethods scsi_methods;
extern AccessMethods hid_methods;


/*
static
int dpf_query(DPFHANDLE h)
{
	int ret;
	unsigned char buf[64];	// Do not change size

	static
	unsigned char cmd[16] = {
		0xcd, 0, 0, 0,
		   0, 3, 0, 0,
		   0, 0, 0, 0,
		   0, 0, 0, 0
	};

	return wrap_scsi(h, cmd, sizeof(cmd), DIR_IN, buf, sizeof(buf));
}
*/



int
dpf_open (const char *dev, DPFHANDLE * h)
{
  int error = 0;
  DPFContext *dpf;
  int i;
  usb_dev_handle *u;

  int fd;

  if (!dev)
    {
      fprintf (stderr,
               "Please specify a string like 'usb0' or a sg device\n");
      return DEVERR_OPEN;
    }

  if (strncmp (dev, "usb", 3) == 0)
    {
      i = dev[3] - '0';
      error = dpf_usb_open (i, &u);
      if (error < 0)
        return error;
      if (!u)
        return DEVERR_OPEN;
      i = error;                // USB mode
      error = 0;
    }
  else
    {
      fprintf (stderr, "Opening generic SCSI device '%s'\n", dev);
      if (sgdev_open (dev, &fd) < 0)
        return DEVERR_OPEN;
      i = MODE_SG;
    }

  dpf = (DPFHANDLE) malloc (sizeof (DPFContext));
  if (!dpf)
    return DEVERR_MALLOC;

  dpf->flags = 0;
  dpf->mode = i;

  switch (dpf->mode)
    {
    case MODE_USB:
      dpf->dev.udev = u;
      error = probe (dpf);
      fprintf (stderr, "Got LCD dimensions: %dx%d\n",
               dpf->width, dpf->height);
      dpf->methods = scsi_methods;
      break;
    case MODE_USBHID:
      dpf->dev.udev = u;
      dpf->methods = hid_methods;
      break;
    case MODE_SG:
      dpf->dev.fd = fd;
      dpf->methods = scsi_methods;
      break;
    default:
      fprintf (stderr, "Unknown interface mode\n");
      error = -1;
    }

  *h = dpf;
  return error;
}

void
dpf_close (DPFContext * h)
{
  switch (h->mode)
    {
    case MODE_USBHID:
    case MODE_SG:
      close (h->dev.fd);
      break;
    case MODE_USB:
      usb_release_interface (h->dev.udev, 0);
      usb_close (h->dev.udev);
      break;
    }
  free (h);
}

const char *
dev_errstr (int err)
{
  switch (err)
    {
    case DEVERR_FILE:
      return "File I/O error";
    case DEVERR_OPEN:
      return "File open error";
    case DEVERR_HEX:
      return "Hex file error";
    case DEVERR_CHK:
      return "Checksum error";
    case DEVERR_IO:
      return "Common I/O error";
    case DEVERR_UNSUPP:
      return "Unsupported feature";
    default:
      return "Unknown error";
    }
}


int
flash_probe (DPFContext * h, unsigned char *id)
{
  return h->methods.flash_probe (h, id);
}

int
flash_cmd (DPFContext * h, int command, int cmdlen, ADDR addr)
{
  return h->methods.flash_cmd (h, command, cmdlen, addr);
}


/* Flash functions, API */

int
flash_read (DPFContext * h, unsigned char *buf, ADDR offset, int len)
{
  return h->methods.flash_read (h, buf, offset, len);
}

int
flash_status_wait (DPFContext * h, uint8_t mask)
{
  int error;
  uint8_t status;

  do
    {
      error = h->methods.flash_status (h, &status);
    }
  while ((status & mask) && !error);

  return error;
}


int
flash_write (DPFContext * h, const unsigned char *buf, ADDR offset, int len)
{
  int n;
  int error = 0;

  while (len > 0)
    {
      error = h->methods.flash_cmd (h, SPM_WREN, 1, 0);
      DEB (printf ("Write pages at %06x\n", offset));
      n = h->methods.flash_writechunk (h, buf, offset, len);
      error = flash_status_wait (h, SPS_WIP);

      if (n < 0)
        break;
      len -= n;
      buf += n;
      offset += n;
    }
  return error;
}

/* Mid level flash */

int
load_ihx (DPFContext * h, const char *fname, unsigned char *data,
          unsigned int *buflen, unsigned int reloc)
{
  unsigned char csum_is, csum_need;
  int ret;
  FILE *f;

  static char line[512];
  static unsigned char buf[0x100];
  int count;
  unsigned int addr, len, type;
  unsigned short b;
  unsigned int total = 0;

  DEB (printf ("Opening %s...\n", fname));
  f = fopen (fname, "r");
  if (f == NULL)
    {
      return DEVERR_OPEN;
    }

  while (1)
    {
      fgets (line, sizeof (line), f);

      if (feof (f) || ferror (f))
        break;

      if ((line[0] != ':') || (strlen (line) < 9))
        {
          fprintf (stderr, "invalid line in ihx\n");
          break;
        }

      ret = sscanf (&line[1], "%02x", &len);
      if (ret != 1)
        {
          ret = DEVERR_HEX;
          break;
        }

      ret = sscanf (&line[3], "%04x", &addr);
      if (ret != 1)
        {
          ret = DEVERR_HEX;
          break;
        }

      ret = sscanf (&line[7], "%02x", &type);
      if (ret != 1)
        {
          ret = DEVERR_HEX;
          break;
        }

#ifdef DEBUG
      printf ("len %u addr %04x type %u\n", len, addr, type);
#endif

      if (type == 1)
        break;

      if (type != 0)
        {
          fprintf (stderr, "ihx: unknown type %u\n", type);
          continue;
        }

      csum_need = len + (addr & 0xff) + (addr >> 8) + type;

      total += len;
      if (total > *buflen)
        {
          fprintf (stderr, "Buffer length exceeded. IHX file too big.\n");
          ret = DEVERR_HEX;
          break;
        }

      if (len > sizeof (buf))
        {
          fprintf (stderr, "Buffer length exceeded. Too long lines.\n");
          ret = DEVERR_HEX;
          break;
        }

      for (count = 0; count < len; count++)
        {
          ret = sscanf (&line[9 + count * 2], "%02hx", &b);
          if (ret != 1)
            {
              fprintf (stderr, "hex file: could not parse data!\n");
              break;
            }

          buf[count] = b;
          csum_need += buf[count];
        }

      if (ret != 1)
        {
          ret = DEVERR_HEX;
          break;
        }

      ret = sscanf (&line[9 + len * 2], "%02hx", &b);
      if (ret != 1)
        {
          ret = DEVERR_HEX;
          break;
        }

      csum_is = b;
      if (((csum_need + csum_is) & 0xff) != 0x00)
        {
          fprintf (stderr, "ihx: checksum failure! is: %02x should be:%02x\n",
                   csum_is, csum_need);
          ret = DEVERR_CHK;
          break;
        }

      if (addr < reloc)
        {
          fprintf (stderr, "Bad relocation value\n");
          ret = DEVERR_HEX;
          break;
        }
      // Copy to data buffer at relocated address
      if (data)
        {
          DEB (printf ("Patching at offset %08x, chunk size %d\n",
                       addr - reloc, len));
          memcpy (&data[addr - reloc], buf, len);
        }
      else
        {
          DEB (printf ("Writing to %04x (CODE: %04x), chunk size %d\n",
                       addr - reloc, addr, len));
          h->methods.mem_write (h, addr - reloc, buf, len);
        }
    }
  *buflen = total;
  fclose (f);
  return ret;
}

int
flash_erase_full (DPFContext * h)
{
  flash_cmd (h, SPM_RES, 1, 0);
  flash_cmd (h, SPM_WRSR, 2, 0); // clr status
  flash_cmd (h, SPM_WREN, 1, 0);
  flash_cmd (h, SPM_FLASH_BE, 1, 0);
  printf ("Erase full flash...\n");

  return flash_status_wait (h, SPS_WIP);
}

int
flash_erase (DPFContext * h, ADDR addr)
{
  int error;
  flash_cmd (h, SPM_RES, 1, 0);
  flash_cmd (h, SPM_WREN, 1, 0);
  flash_cmd (h, SPM_WRSR, 2, 0); // clr status

  // now erase flash sector:
  flash_cmd (h, SPM_WREN, 1, 0);
  flash_cmd (h, SPM_FLASH_SE, 4, addr);

  error = flash_status_wait (h, SPS_WIP);
  flash_cmd (h, SPM_WRDI, 1, 0);

  return error;
}

int
dpf_flash_lock (DPFContext * h, char enable)
{
  if (h->methods.flash_lock)
    return h->methods.flash_lock (h, enable);
  else
    return DEVERR_UNSUPP;
}


int
patch_sector (DPFContext * h,
              ADDR reloc, unsigned long addr, const char *hexfile)
{
  int error;
  unsigned short offset;
  static unsigned char readbuf[0x10000];
  unsigned int len = sizeof (readbuf);

  offset = addr & 0xffff;
  addr -= offset;

  error = flash_read (h, readbuf, addr, 0x10000);
  if (error < 0)
    {
      perror ("Reading flash");
      return error;
    }

  error = load_ihx (h, hexfile, &readbuf[offset], &len, reloc);
  if (error < 0)
    {
      fprintf (stderr, "Failed to load HEX file\n");
      return error;
    }
  // Lock DPF handler so nothing can interfere with the flashing (in case
  // we flash ourselves)
  dpf_flash_lock (h, 1);
  error = flash_cmd (h, SPM_WREN, 1, 0);
  error = flash_cmd (h, SPM_WRSR, 2, 0); // clr status

  error = flash_erase (h, addr);
  if (error < 0)
    return error;

  error = flash_write (h, readbuf, addr, 0x10000); // clr status
  dpf_flash_lock (h, 0);

  return error;
}

////////////////////////////////////////////////////////////////////////////
// High level functions, generic
// These require a hacked command handler with extended command set.


int
load_hexfile (DPFContext * h, const char *hexfile)
{
  unsigned int len = 0xc000;
  int error;

  error = load_ihx (h, hexfile, 0, &len, 0x800);
  return error;
}

#define CHUNK_SIZE 1024

int
read_mem (DPFContext * h, unsigned char *buf, ADDR src, unsigned short len)
{
  int error = 0;
  if (!h->methods.mem_read)
    return DEVERR_UNSUPP;

  while (len > CHUNK_SIZE && error >= 0)
    {
      error = h->methods.mem_read (h, buf, src, CHUNK_SIZE);
      src += CHUNK_SIZE;
      len -= CHUNK_SIZE;
      buf += CHUNK_SIZE;
    }
  error = h->methods.mem_read (h, buf, src, len);

  return error;
}

int
write_mem (DPFContext * h, ADDR dst, const unsigned char *buf,
           unsigned short len)
{
  return h->methods.mem_write (h, dst, buf, len);
}

int
code_go (DPFContext * h, ADDR start)
{
  printf ("Executing applet..\n");
  if (h->methods.go)
    return h->methods.go (h, start);
  return DEVERR_UNSUPP;
}



////////////////////////////////////////////////////////////////////////////
// High level functions, DPF specific

/* Bootstrap mode: Expects contiguous memory block to download, then jumps
 * into start address
 */

int
dpf_bootstrap (DPFContext * h,
               ADDR dest, unsigned char *src, unsigned short n, ADDR start)
{
  if (h->methods.bootstrap)
    return h->methods.bootstrap (h, dest, src, n, start);
  else
    return DEVERR_UNSUPP;
}