forked from datajerk/x49gp
-
Notifications
You must be signed in to change notification settings - Fork 7
/
flash.c
837 lines (722 loc) · 19.1 KB
/
flash.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
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
/* $Id: flash.c,v 1.18 2008/12/11 12:18:17 ecd Exp $
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <cairo.h>
#include <x49gp.h>
#include <x49gp_ui.h>
#include <memory.h>
#include <byteorder.h>
#define FLASH_STATE_NORMAL 0
#define FLASH_STATE_UNLOCK1 1
#define FLASH_STATE_UNLOCK2 2
#define FLASH_STATE_ERASE1 3
#define FLASH_STATE_ERASE2 4
#define FLASH_STATE_ERASE3 5
#define FLASH_STATE_SOFTWARE_EXIT1 6
#define FLASH_STATE_SOFTWARE_EXIT2 7
#define FLASH_STATE_CFI_QUERY_EXIT1 8
#define FLASH_STATE_CFI_QUERY_EXIT2 9
#define FLASH_STATE_SOFTWARE_ID 10
#define FLASH_STATE_CFI_QUERY 11
#define FLASH_STATE_WORD_PROG 12
typedef struct {
void *data;
int state;
unsigned short vendor_ID;
unsigned short device_ID;
const unsigned short *cfi_data;
uint32_t cfi_size;
uint32_t sector_size;
uint32_t block_size;
char *filename;
int fd;
size_t size;
uint32_t iotype;
uint32_t offset;
} x49gp_flash_t;
#define SST29VF160_VENDOR_ID 0x00bf
#define SST29VF160_DEVICE_ID 0x2782
#define SST29VF160_SECTOR_SIZE 0x00001000
#define SST29VF160_BLOCK_SIZE 0x00010000
#define SST29VF160_SIZE 0x00200000
#define BOOT_SIZE 0x00004000
static const unsigned short sst29vf160_cfi_data[] =
{
[0x10] = 0x0051,
[0x11] = 0x0052,
[0x12] = 0x0059,
[0x13] = 0x0001,
[0x14] = 0x0007,
[0x15] = 0x0000,
[0x16] = 0x0000,
[0x17] = 0x0000,
[0x18] = 0x0000,
[0x19] = 0x0000,
[0x1a] = 0x0000,
[0x1b] = 0x0027,
[0x1c] = 0x0036,
[0x1d] = 0x0000,
[0x1e] = 0x0000,
[0x1f] = 0x0004,
[0x20] = 0x0000,
[0x21] = 0x0004,
[0x22] = 0x0006,
[0x23] = 0x0001,
[0x24] = 0x0000,
[0x25] = 0x0001,
[0x26] = 0x0001,
[0x27] = 0x0015,
[0x28] = 0x0001,
[0x29] = 0x0000,
[0x2a] = 0x0000,
[0x2b] = 0x0000,
[0x2c] = 0x0002,
[0x2d] = 0x00ff,
[0x2e] = 0x0001,
[0x2f] = 0x0010,
[0x30] = 0x0000,
[0x31] = 0x003f,
[0x32] = 0x0000,
[0x33] = 0x0000,
[0x34] = 0x0001
};
#define SST29VF160_CFI_SIZE (sizeof(sst29vf160_cfi_data) / sizeof(sst29vf160_cfi_data[0]))
static void
flash_state_reset(x49gp_flash_t *flash)
{
if (flash->state != FLASH_STATE_NORMAL) {
cpu_register_physical_memory(0x00000000, SST29VF160_SIZE,
flash->offset | flash->iotype | IO_MEM_ROMD);
flash->state = FLASH_STATE_NORMAL;
}
}
static uint32_t
flash_get_halfword(x49gp_flash_t *flash, uint32_t offset)
{
uint8_t *datap = flash->data;
uint16_t data;
switch (flash->state) {
default:
flash_state_reset(flash);
/* fall through */
case FLASH_STATE_NORMAL:
data = lduw_p(datap + offset);
break;
case FLASH_STATE_SOFTWARE_ID:
if (offset & 2) {
data = flash->device_ID;
} else {
data = flash->vendor_ID;
}
break;
case FLASH_STATE_CFI_QUERY:
if ( (offset>>1) < flash->cfi_size) {
data = flash->cfi_data[offset >> 1];
} else {
data = 0x0000;
}
break;
}
return data;
}
static void
flash_put_halfword(x49gp_flash_t *flash, uint32_t offset, uint32_t data)
{
uint8_t *datap = flash->data;
uint16_t temp;
data &= 0xffff;
switch (flash->state) {
default:
flash_state_reset(flash);
/* fall through */
case FLASH_STATE_NORMAL:
if (((offset >> 1) == 0x5555) && ((data & 0xff) == 0xaa)) {
flash->state = FLASH_STATE_UNLOCK1;
cpu_register_physical_memory(0x00000000, SST29VF160_SIZE, flash->iotype);
}
break;
case FLASH_STATE_UNLOCK1:
if (((offset >> 1) == 0x2aaa) && ((data & 0xff) == 0x55)) {
flash->state = FLASH_STATE_UNLOCK2;
} else {
flash_state_reset(flash);
}
break;
case FLASH_STATE_UNLOCK2:
if ((offset >> 1) == 0x5555) {
switch (data & 0xff) {
case 0xa0:
flash->state = FLASH_STATE_WORD_PROG;
break;
case 0x80:
flash->state = FLASH_STATE_ERASE1;
break;
case 0x90:
flash->state = FLASH_STATE_SOFTWARE_ID;
break;
case 0x98:
flash->state = FLASH_STATE_CFI_QUERY;
break;
default:
flash_state_reset(flash);
break;
}
} else {
flash_state_reset(flash);
}
break;
case FLASH_STATE_ERASE1:
if (((offset >> 1) == 0x5555) && ((data & 0xff) == 0xaa)) {
flash->state = FLASH_STATE_ERASE2;
} else {
flash_state_reset(flash);
}
break;
case FLASH_STATE_ERASE2:
if (((offset >> 1) == 0x2aaa) && ((data & 0xff) == 0x55)) {
flash->state = FLASH_STATE_ERASE3;
} else {
flash_state_reset(flash);
}
break;
case FLASH_STATE_SOFTWARE_EXIT1:
if (((offset >> 1) == 0x2aaa) && ((data & 0xff) == 0x55)) {
flash->state = FLASH_STATE_SOFTWARE_EXIT2;
} else {
flash->state = FLASH_STATE_SOFTWARE_ID;
}
break;
case FLASH_STATE_SOFTWARE_EXIT2:
if (((offset >> 1) == 0x5555) && ((data & 0xff) == 0xf0)) {
flash_state_reset(flash);
} else {
flash->state = FLASH_STATE_SOFTWARE_ID;
}
break;
case FLASH_STATE_CFI_QUERY_EXIT1:
if (((offset >> 1) == 0x2aaa) && ((data & 0xff) == 0x55)) {
flash->state = FLASH_STATE_CFI_QUERY_EXIT2;
} else {
flash->state = FLASH_STATE_CFI_QUERY;
}
break;
case FLASH_STATE_CFI_QUERY_EXIT2:
if (((offset >> 1) == 0x5555) && ((data & 0xff) == 0xf0)) {
flash_state_reset(flash);
} else {
flash->state = FLASH_STATE_CFI_QUERY;
}
break;
case FLASH_STATE_SOFTWARE_ID:
if (((offset >> 1) == 0x5555) && ((data & 0xff) == 0xaa)) {
flash->state = FLASH_STATE_SOFTWARE_EXIT1;
} else if ((data & 0xff) == 0xf0) {
flash_state_reset(flash);
}
break;
case FLASH_STATE_CFI_QUERY:
if (((offset >> 1) == 0x5555) && ((data & 0xff) == 0xaa)) {
flash->state = FLASH_STATE_CFI_QUERY_EXIT1;
} else if ((data & 0xff) == 0xf0) {
flash_state_reset(flash);
}
break;
case FLASH_STATE_WORD_PROG:
temp = lduw_p(datap + offset);
stw_p(datap + offset, data & temp);
#ifdef DEBUG_X49GP_FLASH_WRITE
printf("write FLASH 2 (state %u) at offset %08x: %04x, result: %04x\n",
flash->state, offset, data, lduw_p(datap + offset));
#endif
flash_state_reset(flash);
break;
case FLASH_STATE_ERASE3:
switch (data & 0xff) {
case 0x10: /* Chip Erase */
#ifdef DEBUG_X49GP_FLASH_WRITE
printf("erase FLASH %08x %08x\n", 0, SST29VF160_SIZE);
#endif
memset(datap, 0xff, SST29VF160_SIZE);
break;
case 0x30: /* Sector Erase */
#ifdef DEBUG_X49GP_FLASH_WRITE
printf("erase FLASH %08x %08x\n",
(offset & ~(flash->sector_size - 1)),
flash->sector_size);
#endif
memset(datap + (offset & ~(flash->sector_size - 1)), 0xff, flash->sector_size);
#ifdef DEBUG_X49GP_FLASH_WRITE
printf("erase FLASH %08x: %04x, %08x: %04x, %08x: %04x\n",
offset, lduw_p(datap + offset),
offset + 0x800, lduw_p(datap + offset + 0x800),
offset + 0xffc, lduw_p(datap + offset + 0xffc));
#endif
break;
case 0x50: /* Block Erase */
#ifdef DEBUG_X49GP_FLASH_WRITE
printf("erase FLASH %08x %08x\n",
(offset & ~(flash->block_size - 1)),
flash->block_size);
#endif
memset(datap + (offset & ~(flash->block_size - 1)), 0xff, flash->block_size);
break;
default:
break;
}
flash_state_reset(flash);
break;
}
}
static uint32_t
flash_readb(void *opaque, target_phys_addr_t offset)
{
x49gp_flash_t *flash = opaque;
uint8_t *datap = flash->data;
unsigned short temp;
uint32_t shift;
unsigned char data;
if (flash->state == FLASH_STATE_NORMAL) {
data = *(datap + offset);
} else {
temp = flash_get_halfword(flash, offset & ~(1));
shift = (offset & 1) << 3;
data = (temp >> shift) & 0xff;
}
#ifdef DEBUG_X49GP_FLASH_READ
printf("read FLASH 1 (state %u) at offset %08lx: %02x\n",
flash->state, (unsigned long) offset, data);
#endif
return data;
}
static uint32_t
flash_readw(void *opaque, target_phys_addr_t offset)
{
x49gp_flash_t *flash = opaque;
uint8_t *datap = flash->data;
uint32_t data;
if (flash->state == FLASH_STATE_NORMAL) {
data = lduw_p(datap + offset);
} else {
data = flash_get_halfword(flash, offset);
}
#ifdef DEBUG_X49GP_FLASH_READ
printf("read FLASH 2 (state %u) at offset %08lx: %04x\n",
flash->state, (unsigned long) offset, data);
#endif
return data;
}
static uint32_t
flash_readl(void *opaque, target_phys_addr_t offset)
{
x49gp_flash_t *flash = opaque;
uint8_t *datap = flash->data;
uint32_t data;
if (flash->state == FLASH_STATE_NORMAL) {
data = ldl_p(datap + offset);
} else {
data = (flash_get_halfword(flash, offset + 2) << 16) |
(flash_get_halfword(flash, offset + 0) << 0);
}
#ifdef DEBUG_X49GP_FLASH_READ
printf("read FLASH 4 (state %u) at offset %08lx: %08x\n",
flash->state, (unsigned long) offset, data);
#endif
return data;
}
static void
flash_writeb(void *opaque, target_phys_addr_t offset, uint32_t data)
{
x49gp_flash_t *flash = opaque;
uint32_t shift;
data &= 0xff;
#ifdef DEBUG_X49GP_FLASH_WRITE
printf("write FLASH 1 (state %u) at offset %08lx: %02x\n",
flash->state, offset, data);
#endif
/*
* This does not issue read-modify-write, i.e. you will get
* broken data in FLASH memory. This would be the case with
* real hardware, too.
*/
shift = (offset & 1) << 3;
flash_put_halfword(flash, offset & ~(1), data << shift);
}
static void
flash_writew(void *opaque, target_phys_addr_t offset, uint32_t data)
{
x49gp_flash_t *flash = opaque;
data &= 0xffff;
#ifdef DEBUG_X49GP_FLASH_WRITE
printf("write FLASH 2 (state %u) at offset %08lx: %04x\n",
flash->state, offset, data);
#endif
flash_put_halfword(flash, offset, data);
}
static void
flash_writel(void *opaque, target_phys_addr_t offset, uint32_t data)
{
x49gp_flash_t *flash = opaque;
#ifdef DEBUG_X49GP_FLASH_WRITE
printf("write FLASH 4 (state %u) at offset %08lx: %08x\n",
flash->state, offset, data);
#endif
flash_put_halfword(flash, offset + 2, (data >> 16) & 0xffff);
flash_put_halfword(flash, offset + 0, (data >> 0) & 0xffff);
}
static int
flash_load(x49gp_module_t *module, GKeyFile *key)
{
x49gp_flash_t *flash = module->user_data;
x49gp_t *x49gp = module->x49gp;
x49gp_ui_t *ui = x49gp->ui;
int calc = ui->calculator;
char *filename;
struct stat st;
char *bootfile;
int bootfd, fwfd;
int error;
int i;
char bank_marker[5] = {0xf0, 0x02, 0x00, 0x00, 0x00};
int bytes_read;
#ifdef DEBUG_X49GP_MODULES
printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__);
#endif
error = x49gp_module_get_filename(module, key, "filename", "flash",
&(flash->filename), &filename);
flash->fd = open(filename, O_RDWR | O_CREAT, 0644);
if (flash->fd < 0) {
error = -errno;
fprintf(stderr, "%s: %s:%u: open %s: %s\n",
module->name, __FUNCTION__, __LINE__,
filename, strerror(errno));
g_free(filename);
return error;
}
flash->size = SST29VF160_SIZE;
if (fstat(flash->fd, &st) < 0) {
error = -errno;
fprintf(stderr, "%s: %s:%u: fstat %s: %s\n",
module->name, __FUNCTION__, __LINE__,
filename, strerror(errno));
g_free(filename);
close(flash->fd);
flash->fd = -1;
return error;
}
if (ftruncate(flash->fd, flash->size) < 0) {
error = -errno;
fprintf(stderr, "%s: %s:%u: ftruncate %s: %s\n",
module->name, __FUNCTION__, __LINE__,
filename, strerror(errno));
g_free(filename);
close(flash->fd);
flash->fd = -1;
return error;
}
flash->data = mmap(phys_ram_base + flash->offset, flash->size,
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED,
flash->fd, 0);
if (flash->data == (void *) -1) {
error = -errno;
fprintf(stderr, "%s: %s:%u: mmap %s: %s\n",
module->name, __FUNCTION__, __LINE__,
filename, strerror(errno));
g_free(filename);
close(flash->fd);
flash->fd = -1;
return error;
}
if (flash->size > st.st_size) {
fprintf(stderr, "Flash too small, rebuilding\n");
x49gp->startup_reinit = X49GP_REINIT_FLASH_FULL;
}
if (x49gp->startup_reinit >= X49GP_REINIT_FLASH) {
if (x49gp->startup_reinit == X49GP_REINIT_FLASH_FULL)
memset(phys_ram_base + flash->offset, 0xff,
flash->size - st.st_size);
bootfd = x49gp_module_open_rodata(module,
calc == UI_CALCULATOR_HP49GP ||
calc == UI_CALCULATOR_HP49GP_NEWRPL ?
"firmware/boot-49g+.bin" :
"firmware/boot-50g.bin",
&bootfile);
if (bootfd < 0) {
g_free(filename);
close(flash->fd);
flash->fd = -1;
return bootfd;
}
if (read(bootfd, phys_ram_base + flash->offset,
BOOT_SIZE) < 0) {
error = -errno;
fprintf(stderr, "%s: %s:%u: read %s: %s\n",
module->name, __FUNCTION__, __LINE__,
filename, strerror(errno));
g_free(filename);
g_free(bootfile);
close(bootfd);
close(flash->fd);
flash->fd = -1;
return error;
}
g_free(filename);
close(bootfd);
g_free(bootfile);
if (x49gp->startup_reinit == X49GP_REINIT_FLASH_FULL) {
/* The stock firmware expects special markers in certain
spots across the flash. Without these, the user banks
act up and are not usable, and PINIT apparently won't
fix it. Let's help it out; custom firmware will have
to deal with remnants of the user banks on real
calculators anyway, so if they break here, they will
too on actual hardware because that always comes with
the stock firmware and its user banks marked
properly. */
for (i=2;i<14;i++) {
bank_marker[1] = i;
memcpy(phys_ram_base + flash->offset + 0x40100 +
0x20000 * i, bank_marker, 5);
}
}
retry:
filename = NULL;
if (x49gp->firmware != NULL) {
filename = g_strdup(x49gp->firmware);
} else {
x49gp_ui_open_firmware(x49gp, &filename);
}
if (filename != NULL) {
fwfd = open(filename, O_RDONLY);
if (fwfd < 0) {
fprintf(stderr, "%s: %s:%u: open %s: %s\n",
module->name, __FUNCTION__, __LINE__,
filename, strerror(errno));
/* Mark firmware as invalid if there is one */
memset(phys_ram_base + flash->offset +
BOOT_SIZE, 0, 16);
if (x49gp->firmware != NULL) {
fprintf(stderr, "Warning: Could not "
"open selected firmware, "
"falling back to bootloader "
"recovery tools\n");
} else {
x49gp_ui_show_error(x49gp,
"Could not open "
"selected "
"firmware!");
goto retry;
}
} else {
bytes_read = read(fwfd, phys_ram_base +
flash->offset + BOOT_SIZE,
16);
if (bytes_read < 0) {
fprintf(stderr, "%s: %s:%u: read %s: %s\n",
module->name, __FUNCTION__,
__LINE__, filename,
strerror(errno));
/* Mark firmware as invalid
if there is one */
memset(phys_ram_base + flash->offset +
BOOT_SIZE, 0, 16);
if (x49gp->firmware != NULL) {
fprintf(stderr, "Warning: "
"Could not read "
"selected firmware, "
"falling back to "
"bootloader recovery "
"tools\n");
} else {
x49gp_ui_show_error(x49gp,
"Could not "
"read "
"selected "
"firmware!");
goto retry;
}
} else if (bytes_read < 16 ||
memcmp(phys_ram_base +
flash->offset + BOOT_SIZE,
"KINPOUPDATEIMAGE", 16)
!= 0) {
/* Mark firmware as invalid */
memset(phys_ram_base + flash->offset +
BOOT_SIZE, 0, 16);
if (x49gp->firmware != NULL) {
fprintf(stderr, "Warning: "
"Firmware is invalid, "
"falling back to "
"bootloader recovery "
"tools\n");
} else {
x49gp_ui_show_error(x49gp,
"Selected "
"firmware "
"is "
"invalid!");
goto retry;
}
/* The firmware may be shorter than
SST29VF160_SIZE - BOOT_SIZE, but if so,
read will just give us what it sees.
The space after that will remain empty. */
} else if (read(fwfd, phys_ram_base +
flash->offset + BOOT_SIZE + 16,
SST29VF160_SIZE -
(BOOT_SIZE + 16))
< 0) {
fprintf(stderr, "%s: %s:%u: read %s: %s\n",
module->name, __FUNCTION__,
__LINE__, filename,
strerror(errno));
/* Mark firmware as invalid
if there is one */
memset(phys_ram_base + flash->offset +
BOOT_SIZE, 0, 16);
if (x49gp->firmware != NULL) {
fprintf(stderr, "Warning: "
"Could not read "
"selected firmware, "
"falling back to "
"bootloader recovery "
"tools\n");
} else {
x49gp_ui_show_error(x49gp,
"Could not "
"read "
"selected "
"firmware!");
goto retry;
}
} else {
/* Mark firmware as valid in the same
way the bootloader does */
memcpy(phys_ram_base + flash->offset +
BOOT_SIZE, "Kinposhcopyright",
16);
}
close(fwfd);
}
g_free(filename);
}
} else {
g_free(filename);
}
return error;
}
static int
flash_save(x49gp_module_t *module, GKeyFile *key)
{
x49gp_flash_t *flash = module->user_data;
int error;
#ifdef DEBUG_X49GP_MODULES
printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__);
#endif
x49gp_module_set_filename(module, key, "filename", flash->filename);
error = msync(flash->data, flash->size, MS_ASYNC);
if (error) {
fprintf(stderr, "%s:%u: msync: %s\n",
__FUNCTION__, __LINE__, strerror(errno));
return error;
}
error = fsync(flash->fd);
if (error) {
fprintf(stderr, "%s:%u: fsync: %s\n",
__FUNCTION__, __LINE__, strerror(errno));
return error;
}
return 0;
}
static int
flash_reset(x49gp_module_t *module, x49gp_reset_t reset)
{
x49gp_flash_t *flash = module->user_data;
#ifdef DEBUG_X49GP_MODULES
printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__);
#endif
flash_state_reset(flash);
return 0;
}
static CPUReadMemoryFunc *flash_readfn[] =
{
flash_readb,
flash_readw,
flash_readl
};
static CPUWriteMemoryFunc *flash_writefn[] =
{
flash_writeb,
flash_writew,
flash_writel
};
static int
flash_init(x49gp_module_t *module)
{
x49gp_flash_t *flash;
#ifdef DEBUG_X49GP_MODULES
printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__);
#endif
flash = malloc(sizeof(x49gp_flash_t));
if (NULL == flash) {
fprintf(stderr, "%s: %s:%u: Out of memory\n",
module->name, __FUNCTION__, __LINE__);
return -1;
}
memset(flash, 0, sizeof(x49gp_flash_t));
flash->vendor_ID = SST29VF160_VENDOR_ID;
flash->device_ID = SST29VF160_DEVICE_ID;
flash->cfi_data = sst29vf160_cfi_data;
flash->cfi_size = SST29VF160_CFI_SIZE;
flash->sector_size = SST29VF160_SECTOR_SIZE;
flash->block_size = SST29VF160_BLOCK_SIZE;
flash->fd = -1;
module->user_data = flash;
flash->iotype = cpu_register_io_memory(flash_readfn,
flash_writefn, flash);
flash->data = (void *) -1;
flash->offset = phys_ram_size;
phys_ram_size += SST29VF160_SIZE;
cpu_register_physical_memory(0x00000000, SST29VF160_SIZE,
flash->offset | flash->iotype | IO_MEM_ROMD);
return 0;
}
static int
flash_exit(x49gp_module_t *module)
{
x49gp_flash_t *flash;
#ifdef DEBUG_X49GP_MODULES
printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__);
#endif
if (module->user_data) {
flash = module->user_data;
if (flash->data != (void *) -1) {
munmap(flash->data, flash->size);
}
if (flash->fd) {
close(flash->fd);
}
free(flash);
}
x49gp_module_unregister(module);
free(module);
return 0;
}
int
x49gp_flash_init(x49gp_t *x49gp)
{
x49gp_module_t *module;
if (x49gp_module_init(x49gp, "flash", flash_init, flash_exit,
flash_reset, flash_load, flash_save, NULL,
&module)) {
return -1;
}
return x49gp_module_register(module);
}