summaryrefslogtreecommitdiffstats
path: root/oncology/dpfhack_display/dpflib/dpflib.c
blob: e248eb095a6423dfd5c76d2a0813b8aa10b8151c (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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/** 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 "sglib.h"

#include "flash.h"
#include <fcntl.h>
#include "usbuser.h" // our user defined flash commands
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <scsi/scsi.h>
#include <scsi/sg.h>
#include <sys/ioctl.h>

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

/** Vendor command for our hacks */
static
unsigned char g_excmd[16] = {
	0xcd, 0, 0, 0,
	   0, 6, 0, 0,
	   0, 0, 0, 0,
	   0, 0, 0, 0
};

int do_scsi(int fd, unsigned char *cmd, int cmdlen, char out,
	unsigned char *data, unsigned long block_len)
{
	int error;
	unsigned char sensebuf[32];
	sg_io_hdr_t io_hdr;
    memset(&io_hdr, 0, sizeof(sg_io_hdr_t));

    io_hdr.interface_id = 'S';
    io_hdr.sbp = sensebuf;
    io_hdr.mx_sb_len = sizeof(sensebuf);
	if (data == 0) {
		io_hdr.dxfer_direction = SG_DXFER_NONE;
	} else {
		if (out) {
			io_hdr.dxfer_direction = SG_DXFER_TO_DEV;
		} else {
			io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
		}
	}
    io_hdr.dxferp = data;
    io_hdr.dxfer_len = block_len;
    io_hdr.cmdp = cmd;
    io_hdr.cmd_len = cmdlen;
    io_hdr.timeout = 5000; // in ms
	error = ioctl(fd, SG_IO, &io_hdr);
	if (error < 0) perror("calling SCSI ioctl()");
	return error;
}

int wrap_scsi(DPFContext *h, unsigned char *cmd, int cmdlen, char out,
	unsigned char *data, unsigned long block_len)
{
	int error;
	if (h->mode == MODE_USB) {
		error = emulate_scsi(h->dev.udev, cmd, cmdlen, out, data, block_len);
	} else {
		error = do_scsi(h->dev.fd, cmd, cmdlen, out, data, block_len);
	}
	return error;
}

/* device wrapper */

const char idstring[] = "buildwin Photo Frame    1.01";

#define INQ_REPLY_LEN 96

int sgdev_open(const char *portname, int *fd)
{
	int error;

	static
	unsigned char inquiry[] = {
		INQUIRY, 0, 0, 0,
		INQ_REPLY_LEN, 0
	};

	static unsigned char inqbuf[INQ_REPLY_LEN + 2];

	*fd = open(portname, O_RDONLY | O_NONBLOCK );
	error = do_scsi(*fd, inquiry, sizeof(inquiry), DIR_IN, inqbuf,
		INQ_REPLY_LEN);
	
	if (error < 0) {
		fprintf(stderr, "SCSI inquiry failed\n");
		close(*fd); error = DEVERR_OPEN;
	} else 
	if (memcmp(idstring, &inqbuf[8], sizeof(idstring) - 1) != 0) {
		close(*fd); error = DEVERR_OPEN;
		fprintf(stderr, "Not a photo frame. Refuse to open device.\n");
	}
	return error;
}

static
int probe(DPFHANDLE h)
{
	int ret;

	// We abuse a command that just responds with a '0' status in the
	// original firmware.
	static unsigned char buf[5];


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

	cmd[5] = 3;
	ret = wrap_scsi(h, cmd, sizeof(cmd), DIR_IN, 0, 0);

	switch (ret) {
		case 0:
			// The original protocol.
			fprintf(stderr,
				"Warning: This firmware can not lock the flash\n");
			break;
		case 1:
			// The improved hack
			h->flags |= FLAG_CAN_LOCK;
			break;
	}

	cmd[5] = 2; // get LCD parameters
	ret = wrap_scsi(h, cmd, sizeof(cmd), DIR_IN, buf, 5);
	h->width = (buf[0]) | (buf[1] << 8);
	h->height = (buf[2]) | (buf[3] << 8);
	h->bpp = 2;

	return ret;
}

/*
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';
		u = dpf_usb_open(i);
		if (!u) return DEVERR_OPEN;
		i = MODE_USB;
	} 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;

	if (dpf->mode == MODE_USB) {
		dpf->dev.udev = u;
		error = probe(dpf);
		fprintf(stderr, "Got LCD dimensions: %dx%d\n", dpf->width, dpf->height);
	} else {
		dpf->dev.fd = fd;
	}

	*h = dpf;
	return error;
}

void dpf_close(DPFContext *h)
{
	switch (h->mode) {
		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";
	default: return "Unknown error";
	}
}

/* Flash stuff */

int flash_cmd(DPFContext *h, int command, int cmdlen, ADDR addr)
{
	static
	unsigned char cmd[16] = {
		0xcb, 0, 0, 0, 0, 0,
		1, 0, 0, 0,
		0, 0, 0, 0,
		0, 0
	};

	cmd[10] = command;
	cmd[6] = cmdlen;

	// Sector number or addr:
	cmd[13] = addr;
	cmd[12] = addr >> 8;
	cmd[11] = addr >> 16;

	return wrap_scsi(h, cmd, sizeof(cmd), DIR_IN, 0, 0);
}


int flash_read(DPFContext *h, unsigned char *buf, ADDR offset, int len)
{
	static
	unsigned char cmd[16] = {
		0xcd, 0, 0, 0, 0, 0,
		0x04, /* num of bytes: */ 0, 0, 0,
		SPM_READ, /* SPI offset: */ 0, 0, 0,
		0, 0
	};

	cmd[9] = len >> 0;
	cmd[8] = len >> 8;
	cmd[7] = len >> 16;

	cmd[13] = offset >> 0;
	cmd[12] = offset >> 8;
	cmd[11] = offset >> 16;

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

static
int flash_writechunk(DPFContext *h, const unsigned char *buf, ADDR offset, int len)
{
	static
	unsigned char cmd[16] = {
		0xcb, 0, 0, 0, 0, 0,
		4, /* num of bytes: */ 0, 0, 0,
		SPM_PP, /* SPI offset: */ 0, 0, 0,
		0, 0
	};

	cmd[9] = len >> 0;
	cmd[8] = len >> 8;
	cmd[7] = len >> 16;

	cmd[13] = offset >> 0;
	cmd[12] = offset >> 8;
	cmd[11] = offset >> 16;

	return wrap_scsi(h, cmd, sizeof(cmd), DIR_OUT,
		(unsigned char*) buf, len);
}

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

	for (i = 0; i < len; i += 0x1000) {
		error = flash_cmd(h, SPM_WREN, 1, 0);
		DEB(printf("Write pages at %06x\n", offset));
		error = flash_writechunk(h, buf, offset, 0x1000);
		if (error < 0) break;
		buf += 0x1000; offset += 0x1000;
	}
	return error;
}


int load_ihx(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));
			dpf_copy(addr - reloc, buf, len);
		}
    }
	*buflen = total;
    fclose(f);
	return ret;
}


int flash_probe(DPFContext *h, unsigned char *id)
{
	int error;
	static
	unsigned char buf[64];
	static
	unsigned char cmd[16] = {
		0xcd, 0, 0, 0, 0, 0,
		1, sizeof(buf) >> 16, sizeof(buf) >> 8, sizeof(buf) >> 0,
		// flash command sequence:
		SPM_RDID, 0, 0, 0,
		0, 0
	};

	error = wrap_scsi(h, cmd, sizeof(cmd), DIR_IN, buf, sizeof(buf));
	id[0] = buf[0]; id[1] = buf[1]; id[2] = buf[2];
	return error;
}

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

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

void dpf_flash_lock(DPFContext *h, char enable)
{
	unsigned char *cmd = g_excmd;

	if (!(h->flags & FLAG_CAN_LOCK)) return;

	printf("Lock flash %d\n", enable);
	cmd[6] = USBCMD_FLASHLOCK; // flash lock
	cmd[7] = enable;

	wrap_scsi(h, cmd, sizeof(g_excmd), DIR_IN, 0, 0);
}


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(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.


static DPFHANDLE g_dpf;

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

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

int read_blk(DPFContext *h, unsigned char *buf, ADDR offset, int len)
{
	unsigned char *cmd = g_excmd;

	cmd[6] = USBCMD_MEMREAD; // memory_read
	cmd[7] = offset;
	cmd[8] = offset >> 8;
	cmd[9] = len;
	cmd[10] = len >> 8;

	return wrap_scsi(h, cmd, sizeof(g_excmd), DIR_IN, buf, len);
}

#define CHUNK_SIZE 1024

int read_mem(DPFContext *h, unsigned char *buf, ADDR offset, int len)
{
	int error = 0;
	while (len > CHUNK_SIZE && error >= 0) {
		error = read_blk(h, buf, offset, CHUNK_SIZE);
		offset += CHUNK_SIZE; len -= CHUNK_SIZE; buf += CHUNK_SIZE;
	}
	error = read_blk(h, buf, offset, len);
	
	return error;
}


int code_go(DPFContext *h, ADDR start)
{
	unsigned char *cmd = g_excmd;
	printf("Executing applet..\n");
	cmd[6] = 0x02;   // execute
	cmd[7] = start;
	cmd[8] = start >> 8;

	return wrap_scsi(h, cmd, sizeof(g_excmd), DIR_IN, 0, 0);
}



////////////////////////////////////////////////////////////////////////////
// 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)
{
	unsigned char *cmd = g_excmd;
	cmd[6] = USBCMD_APPLOAD;   // Enter bootstrap mode
	cmd[7] = dest;
	cmd[8] = dest >> 8;
	cmd[9] = n;
	cmd[10] = n >> 8;
	cmd[11] = start;
	cmd[12] = start >> 8;

	return wrap_scsi(h, cmd, sizeof(g_excmd), DIR_OUT, src, n);
}

int dpf_copy(ADDR dst, unsigned char *src, unsigned short n)
{
	unsigned char *cmd = g_excmd;

	cmd[6] = 0x01; // memory_write
	cmd[7] = dst;
	cmd[8] = dst >> 8;
	cmd[9] = n;
	cmd[10] = n >> 8;

	return wrap_scsi(g_dpf, cmd, sizeof(g_excmd), DIR_OUT, src, n);
}

/*  Currently unused.
int clr_screen(DPFContext *h, const unsigned char *col)
{
	unsigned char *cmd = g_excmd;

	cmd[6] = USBCMD_PROBE;
	cmd[7] = col[0];
	cmd[8] = col[1];

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

/* This function is deprecated and may not be implemented on newer hacks
 */
int write_screen(DPFContext *h, const unsigned char *buf, unsigned int len)
{
	unsigned char *cmd = g_excmd;
	unsigned int l;
	unsigned char factor;

	if (len > 0xffff) {
		factor = 2; l = len >> 1;
	} else {
		factor = 1; l = len;
	}

	cmd[6] = USBCMD_WRITEFB;
	cmd[7] = factor;
	cmd[8] = 0;
	cmd[9] = l;
	cmd[10] = l >> 8;

	return wrap_scsi(h, cmd, sizeof(g_excmd), DIR_OUT,
		(unsigned char*) buf, len);
}

int dpf_screen_blit(DPFContext *h,
	const unsigned char *buf, short rect[4])
{
	unsigned long len = (rect[2] - rect[0]) * (rect[3] - rect[1]);
	len <<= 1;
	unsigned char *cmd = g_excmd;

	cmd[6] = USBCMD_BLIT;
	cmd[7] = rect[0];
	cmd[8] = rect[0] >> 8;
	cmd[9] = rect[1];
	cmd[10] = rect[1] >> 8;
	cmd[11] = rect[2] - 1;
	cmd[12] = (rect[2] - 1) >> 8;
	cmd[13] = rect[3] - 1;
	cmd[14] = (rect[3] - 1) >> 8;
	cmd[15] = 0;

	return wrap_scsi(h, cmd, sizeof(g_excmd), DIR_OUT,
		(unsigned char*) buf, len);
}

void dpf_writefb(DPFContext *h, unsigned char *buffer)
{
	int len;

	len = h->width * h->height * h->bpp;
	write_screen(h, buffer, len);
}

int dpf_setproperty(DPFContext *h, int token, const DPFValue *value)
{
	unsigned char *cmd = g_excmd;

	cmd[6] = USBCMD_SETPROPERTY;
	cmd[7] = token;
	cmd[8] = token >> 8;

	switch (value->type) {
		case TYPE_INTEGER:
			cmd[9] = value->value.integer;
			cmd[10] = value->value.integer >> 8;
			break;
		default:
			break;
	}

	return wrap_scsi(h, cmd, sizeof(g_excmd), DIR_OUT, NULL, 0);
}