-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader_rpi.c
More file actions
2550 lines (2413 loc) · 114 KB
/
loader_rpi.c
File metadata and controls
2550 lines (2413 loc) · 114 KB
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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* src/loader_rpi.c
* https://gitlab.com/bztsrc/easyboot
*
* Copyright (C) 2023 bzt
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* @brief The main Easyboot loader program on Raspberry Pi
*
* Memory layout when booted on Raspberry Pi:
* 0x0 - 0x100 reserved by firmware (armstub)
* 0x508 - 0x510 sctlr
* 0x510 - 0x518 vbar
* 0x518 - 0x520 mair
* 0x520 - 0x528 tcr
* 0x528 - 0x530 ttbr0
* 0x530 - 0x538 ttbr1
* 0x538 - 0x540 kernel entry point (also SMP semaphor)
* 0x540 - 0x548 tags_buf
* 0x100 - 0x1000 stack (3840 bytes)
* 0x1000 - 0x9000 paging tables
* 0x9000 - _bss_end our sections
* 0x20000 - 0x40000 config + tags
* 0x40000 - 0x80000 firmware provided FDT (dtb); from the top to bottom: kernel's stack
* 0x80000 - 0x100000 plugin ids
* 0x100000 - x kernel segments, followed by the modules, each page aligned
*
* Plugin ids:
* byte 0..1: record length
* byte 2..9: loaded plugin's address
* byte 10: plugin type
* byte 11: number of id matches (n)
* byte 12..11 + n * 8: id matches (plg_id_t)
* byte 12 + n * 8: zero terminated UTF-8 name of the plugin
*/
/**
* Specify where the boot messages should appear, make it a comment to disable
*/
#define CONSOLE_SERIAL /* default serial, UART0 */
#define CONSOLE_FB /* on screen too */
/* it is VERY important that these two variables must be the first in the
* read-only data segment, because the disk generator might alter them */
const char __attribute__((aligned(16))) defkernel[64] = "kernel", definitrd[64] = "";
/* patched by bin2h.c, must be right after the default names. Must use volatile otherwise Clang optimizes memread away */
const volatile unsigned int _bss_start = 0xffffffff, _bss_end = 0xffffffff;
const char copy[] = "Easyboot loader, Copyright (c) 2023 bzt, GPLv3+\n";
/**
* Assembly preambule to C
* must be the very first function; it can't use any local variables and can't have a stack frame
*/
void __attribute__((noreturn)) /*__attribute__((naked))*/ _preambule(void)
{
/* make sure we run on BSP only, set up EL1 and a stack */
__asm__ __volatile__(
/* move FDT out of the way (up to 256k. As of now the biggest RPi-400 dtb is 54k, overlays typically ca. 4k, biggest 46k) */
"mov x1,#0x40000; mov w3,#32768;1:ldr x2,[x0],#8; str x2,[x1],#8; sub w3,w3,#1; cbnz w3,1b;"
/* relocate ourselves from 0x80000 to 0x9000 */
"mov x0,#0x80000; mov x1,#0x9000; mov w3,#16384;1:ldr x2,[x0],#8; str x2,[x1],#8; sub w3,w3,#1; cbnz w3,1b;"
"ldr x30,=1f;ret;.balign 64;.asciz \"Easyboot https://codeberg.org/bzt/easyboot\";.asciz \"aarch64\";.balign 4;1:"
/* setup */
"mrs x1, mpidr_el1;and x1, x1, #3;cbz x1, 2f;" /* get core id and branch if BSP */
"1:wfe; b 1b;" /* neverending loop for APs */
"2:mov x1, #0x1000;"
"mrs x0, CurrentEL;and x0, x0, #12;"
"cmp x0, #12;bne 1f;" /* are we running at EL3? */
"mov x0, #0x5b1;msr scr_el3, x0;mov x0, #0x3c9;msr spsr_el3, x0;adr x0, 1f;msr elr_el3, x0;mov x0, #4;msr sp_el2, x1;eret;"
"1:cmp x0, #4;beq 1f;" /* are we running at EL2? */
"mrs x0,cnthctl_el2;orr x0,x0,#3;msr cnthctl_el2,x0;msr cnthp_ctl_el2,xzr;" /* enable CNTP */
"mov x0,#(1 << 31);orr x0,x0,#2;msr hcr_el2,x0;mrs x0,hcr_el2;" /* enable Aarch64 at EL1 */
"mrs x0,midr_el1;mrs x2,mpidr_el1;msr vpidr_el2,x0;msr vmpidr_el2,x2;" /* initialize virtual MPIDR */
"mov x0,#0x33FF;msr cptr_el2,x0;msr hstr_el2,xzr;mov x0,#(3<<20);msr cpacr_el1,x0;" /* disable coprocessor traps */
"mov x2,#0x0800;movk x2,#0x30d0,lsl #16;msr sctlr_el1, x2;" /* setup SCTLR access */
"mov x2,#0x3c5;msr spsr_el2,x2;adr x2, 1f;msr elr_el2, x2;mov sp, x1;msr sp_el1, x1;eret;"/* switch to EL1 */
"1:adr x0,1f;msr vbar_el1,x0;mov x2,#0x0510;str x0, [x2], #0;msr SPSel,#0;" /* set up exception handlers */
"mov sp, x1;b _start;" /* set up stack and jump to C function in EL1 */
/* exception handlers */
".balign 128;1:mov x0,#0;mrs x1,esr_el1;mrs x2,elr_el1;mrs x3,spsr_el1;mrs x4,far_el1;mrs x5,sctlr_el1;mrs x6,tcr_el1;b fw_exc;"
".balign 128;mov x0,#1;mrs x1,esr_el1;mrs x2,elr_el1;mrs x3,spsr_el1;mrs x4,far_el1;mrs x5,sctlr_el1;mrs x6,tcr_el1;b fw_exc;"
".balign 128;mov x0,#2;mrs x1,esr_el1;mrs x2,elr_el1;mrs x3,spsr_el1;mrs x4,far_el1;mrs x5,sctlr_el1;mrs x6,tcr_el1;b fw_exc;"
".balign 128;mov x0,#3;mrs x1,esr_el1;mrs x2,elr_el1;mrs x3,spsr_el1;mrs x4,far_el1;mrs x5,sctlr_el1;mrs x6,tcr_el1;b fw_exc;"
:::"x0","x1","x2");
/* naked not supported, so noreturn and unreachable needed to make the compiler omit the stack frame prologue / epilogue */
__builtin_unreachable();
}
#include "loader.h"
/* IMPORTANT: don't assume .bss is zeroed out like in a hosted environment, because it's not */
volatile uint32_t __attribute__((aligned(16))) mbox[40];
uint8_t __attribute__((aligned(16))) vbr[512], data[512];
uint32_t fat[1024], fat_cache, file_clu;
uint16_t lfn[272], wcname[PATH_MAX];
uint64_t mmio_base, emmc_base;
uint64_t file_size, file_base, file_buf, ram, *pt;
uint32_t fb_fg, fb_bg, pb_bg, pb_b, pb_m, pb_l, verbose, num_memmap;
uint8_t rpi, cons, *tags_buf, *tags_ptr, *dtb_base, *plgids, *root_buf, *krnl_buf, *pb_fb, in_exc, rddir, smp;
uint64_t root_dir, data_lba, fat_lba, root_lba, root_siz, krnl_size;
guid_t espGuid = EFI_PART_TYPE_EFI_SYSTEM_PART_GUID;
efi_system_table_t *ST;
guid_t bootuuid, rootuuid;
esp_bpb_t *bpb;
multiboot_tag_framebuffer_t vidmode;
multiboot_tag_module_t *initrd;
char fn[PATH_MAX], *conf_buf, *conf_ptr, *kernel, *cmdline;
/* first one is common, others are architecture dependent */
uint64_t plg_imsk[] = { PLG_IM_0, PLG_IM_AARCH64_1, PLG_IM_AARCH64_2, PLG_IM_AARCH64_3 };
plg_got_t plg_got = {
&verbose, &file_size, &root_buf, &tags_buf, &tags_ptr, &dtb_base, &dtb_base, &ST,
&memset, &memcpy, &memcmp, &alloc, &free,
&printf, &pb_init, &pb_draw, &pb_fini,
&loadsec, &sethooks,
&fw_open, &fw_read, &fw_close, &fw_loadfile,
&fw_loadseg
};
typedef struct {
char magic[8]; /* "RSD PTR " */
uint8_t chksum;
char OEM[6];
uint8_t rev; /* 2 */
uint32_t rsdt;
} __attribute__((packed)) rsdp_t;
typedef struct {
char magic[4];
uint32_t size;
uint8_t rev;
uint8_t chksum;
char OEM[6];
char OEMtableid[8];
uint32_t OEMrev;
uint32_t creatid;
uint32_t creatrev;
} __attribute__((packed)) sdt_hdr_t;
typedef struct {
sdt_hdr_t hdr;
uint32_t table_ptr[2];
} __attribute__((packed)) __attribute__((aligned(16))) rsdt_t;
typedef struct {
uint8_t type; /* 0 processor */
uint8_t size; /* 8 */
uint8_t acpi_id;
uint8_t apic_id;
uint32_t flags; /* bit 0: enabled, bit 1: available */
} __attribute__((packed)) cpu_entry_t;
typedef struct {
sdt_hdr_t hdr; /* magic "APIC" */
uint32_t lapic_addr;
uint32_t flags;
cpu_entry_t cpus[4];
} __attribute__((packed)) __attribute__((aligned(16))) apic_t;
typedef struct {
sdt_hdr_t hdr; /* magic "FACP" */
uint32_t firmwarectrl;
uint32_t dsdt;
uint8_t reserved[96];
uint64_t x_dsdt;
} __attribute__((packed)) __attribute__((aligned(16))) fadt_t;
apic_t apic;
rsdt_t rsdt;
rsdp_t rsdp;
fadt_t fadt;
/**************** Mandatory functions, Clang generates calls to them ****************/
PLG_API void memcpy(void *dst, const void *src, uint32_t n){uint8_t *a=(uint8_t*)dst,*b=(uint8_t*)src;while(n--) *a++=*b++; }
PLG_API void memset(void *dst, uint8_t c, uint32_t n){uint8_t *a=dst;while(n--) *a++=c; }
PLG_API int memcmp(const void *s1, const void *s2, uint32_t n){
uint8_t *a=(uint8_t*)s1,*b=(uint8_t*)s2;while(n--){if(*a!=*b){return *a-*b;}a++;b++;} return 0;
}
#include "inflate.h"
/**
* Convert hex string into integer
*/
char *gethex(char *ptr, uint32_t *ret, int len)
{
*ret = 0;
for(;len--;ptr++) {
if(*ptr>='0' && *ptr<='9') { *ret <<= 4; *ret += (uint32_t)(*ptr - '0'); }
else if(*ptr >= 'a' && *ptr <= 'f') { *ret <<= 4; *ret += (uint32_t)(*ptr - 'a' + 10); }
else if(*ptr >= 'A' && *ptr <= 'F') { *ret <<= 4; *ret += (uint32_t)(*ptr - 'A' + 10); }
else break;
}
return ptr;
}
/**
* Convert decimal string to integer
*/
char *getint(char *ptr, uint32_t *ret)
{
for(*ret = 0; *ptr >= '0' && *ptr <= '9'; ptr++) { *ret *= 10; *ret += (uint32_t)(*ptr - '0'); }
return ptr;
}
/**
* Parse a GUID in string into binary representation
*/
char *getguid(char *ptr, guid_t *guid)
{
uint32_t v;
int i;
if(!ptr || !*ptr || ptr[8] != '-' || ptr[13] != '-' || ptr[18] != '-') return ptr;
memset(guid, 0, sizeof(guid_t));
ptr = gethex(ptr, &v, 8); guid->Data1 = v; ptr++;
ptr = gethex(ptr, &v, 4); guid->Data2 = v; ptr++;
ptr = gethex(ptr, &v, 4); guid->Data3 = v; ptr++;
ptr = gethex(ptr, &v, 2); guid->Data4[0] = v;
ptr = gethex(ptr, &v, 2); guid->Data4[1] = v; if(*ptr == '-') ptr++;
for(i = 2; i < 8; i++) { ptr = gethex(ptr, &v, 2); guid->Data4[i] = v; }
return ptr;
}
/**
* Convert UTF-8 to UNICODE
*/
uint32_t utf8(char **s)
{
uint32_t c = **s;
if((**s & 128) != 0) {
if(!(**s & 32)) { c = ((**s & 0x1F)<<6)|(*(*s+1) & 0x3F); *s += 1; } else
if(!(**s & 16)) { c = ((**s & 0xF)<<12)|((*(*s+1) & 0x3F)<<6)|(*(*s+2) & 0x3F); *s += 2; } else
if(!(**s & 8)) { c = ((**s & 0x7)<<18)|((*(*s+1) & 0x3F)<<12)|((*(*s+2) & 0x3F)<<6)|(*(*s+3) & 0x3F); *s += 3; }
else c = 0;
}
(*s)++;
return c;
}
/**
* Convert UCS2 string to UTF-8
*/
uint8_t *ucs2(uint8_t *s, uint16_t *u)
{
uint8_t *e = s + 31;
for(; *u && s < e; u++) {
if(*u < 0x80) { *s++ = *u; } else
if(*u < 0x800) { *s++ = ((*u>>6)&0x1F)|0xC0; *s++ = (*u&0x3F)|0x80; } else
{ *s++ = ((*u>>12)&0x0F)|0xE0; *s++ = ((*u>>6)&0x3F)|0x80; *s++ = (*u&0x3F)|0x80; }
}
*s++ = 0;
return s;
}
/**************** GPU MailBox interface ****************/
#define VIDEOCORE_MBOX (mmio_base+0x0000B880)
#define MBOX_READ ((volatile uint32_t*)(VIDEOCORE_MBOX+0x0))
#define MBOX_POLL ((volatile uint32_t*)(VIDEOCORE_MBOX+0x10))
#define MBOX_SENDER ((volatile uint32_t*)(VIDEOCORE_MBOX+0x14))
#define MBOX_STATUS ((volatile uint32_t*)(VIDEOCORE_MBOX+0x18))
#define MBOX_CONFIG ((volatile uint32_t*)(VIDEOCORE_MBOX+0x1C))
#define MBOX_WRITE ((volatile uint32_t*)(VIDEOCORE_MBOX+0x20))
#define MBOX_REQUEST 0
#define MBOX_RESPONSE 0x80000000
#define MBOX_FULL 0x80000000
#define MBOX_EMPTY 0x40000000
#define MBOX_CH_POWER 0
#define MBOX_CH_FB 1
#define MBOX_CH_VUART 2
#define MBOX_CH_VCHIQ 3
#define MBOX_CH_LEDS 4
#define MBOX_CH_BTNS 5
#define MBOX_CH_TOUCH 6
#define MBOX_CH_COUNT 7
#define MBOX_CH_PROP 8
uint8_t mbox_call(uint8_t ch)
{
uint32_t r;
/* mailbox write */
do{ __asm__ __volatile__("nop"); } while(*MBOX_STATUS & MBOX_FULL);
*MBOX_WRITE = (((uint32_t)((uint64_t)mbox) & ~0xF) | (ch & 0xF));
/* mailbox read */
do {
do{ __asm__ __volatile__("nop");} while(*MBOX_STATUS & MBOX_EMPTY);
r = *MBOX_READ;
} while((uint8_t)(r & 0xF) != ch);
return (r & ~0xF) == (uint32_t)((uint64_t)mbox) && mbox[1] == MBOX_RESPONSE;
}
/**
* Set up framebuffer with VideoCore
*/
void mbox_lfb(uint32_t width, uint32_t height, uint32_t bpp)
{
/* query natural width, height if not given */
if(!width || !height) {
mbox[0] = 12*4;
mbox[1] = MBOX_REQUEST;
mbox[2] = 0x40003; /* get phy wh */
mbox[3] = 8;
mbox[4] = 8;
mbox[5] = 0;
mbox[6] = 0;
mbox[7] = 0x40005; /* get depth */
mbox[8] = 4;
mbox[9] = 4;
mbox[10] = 0;
mbox[11] = 0;
if(mbox_call(MBOX_CH_PROP) && mbox[5] && mbox[10]) {
width = mbox[5];
height = mbox[6];
if(width < 800) width = 800;
if(height < 600) height = 600;
bpp = 32;
}
}
/* if we already have a framebuffer, release it */
if(vidmode.framebuffer_addr) {
mbox[0] = 8*4;
mbox[1] = MBOX_REQUEST;
mbox[2] = 0x48001; /* release buffer */
mbox[3] = 8;
mbox[4] = 8;
mbox[5] = (uint32_t)vidmode.framebuffer_addr;
mbox[6] = 0;
mbox[7] = 0;
mbox_call(MBOX_CH_PROP);
vidmode.framebuffer_addr = 0;
}
/* check minimum resolution */
if(width < 320) width = 320;
if(height < 200) height = 200;
if(bpp != 15 && bpp != 16 && bpp != 24) bpp = 32;
mbox[0] = 35*4;
mbox[1] = MBOX_REQUEST;
mbox[2] = 0x48003; /* set phy wh */
mbox[3] = 8;
mbox[4] = 8;
mbox[5] = width;
mbox[6] = height;
mbox[7] = 0x48004; /* set virt wh */
mbox[8] = 8;
mbox[9] = 8;
mbox[10] = width;
mbox[11] = height;
mbox[12] = 0x48009; /* set virt offset */
mbox[13] = 8;
mbox[14] = 8;
mbox[15] = 0;
mbox[16] = 0;
mbox[17] = 0x48005; /* set depth */
mbox[18] = 4;
mbox[19] = 4;
mbox[20] = bpp;
mbox[21] = 0x48006; /* set pixel order */
mbox[22] = 4;
mbox[23] = 4;
mbox[24] = 0; /* BGR, not RGB preferably */
mbox[25] = 0x40001; /* get framebuffer, gets alignment on request */
mbox[26] = 8;
mbox[27] = 8;
mbox[28] = 4096;
mbox[29] = 0;
mbox[30] = 0x40008; /* get pitch */
mbox[31] = 4;
mbox[32] = 4;
mbox[33] = 0;
mbox[34] = 0;
if(mbox_call(MBOX_CH_PROP) && mbox[20] == bpp && mbox[27] == (MBOX_RESPONSE|8) && mbox[28]) {
vidmode.framebuffer_addr = (uint64_t)(mbox[28] & 0x3FFFFFFF);
vidmode.framebuffer_pitch = mbox[33];
vidmode.framebuffer_width = mbox[5];
vidmode.framebuffer_height = mbox[6];
vidmode.framebuffer_bpp = mbox[20];
vidmode.framebuffer_type = 1;
if(mbox[24]) {
/* red is the least significant channel */
switch(mbox[20]) {
case 15:
vidmode.framebuffer_red_mask_size = vidmode.framebuffer_green_mask_size =
vidmode.framebuffer_blue_mask_size = 5;
vidmode.framebuffer_red_field_position = 0;
vidmode.framebuffer_green_field_position = 5;
vidmode.framebuffer_blue_field_position = 10;
break;
case 16:
vidmode.framebuffer_red_mask_size = vidmode.framebuffer_blue_mask_size = 5;
vidmode.framebuffer_green_mask_size = 6;
vidmode.framebuffer_red_field_position = 0;
vidmode.framebuffer_green_field_position = 5;
vidmode.framebuffer_blue_field_position = 11;
break;
default:
vidmode.framebuffer_red_mask_size = vidmode.framebuffer_green_mask_size =
vidmode.framebuffer_blue_mask_size = 8;
vidmode.framebuffer_red_field_position = 0;
vidmode.framebuffer_green_field_position = 8;
vidmode.framebuffer_blue_field_position = 16;
break;
}
} else {
/* blue is the least significant channel */
switch(mbox[20]) {
case 15:
vidmode.framebuffer_red_mask_size = vidmode.framebuffer_green_mask_size =
vidmode.framebuffer_blue_mask_size = 5;
vidmode.framebuffer_red_field_position = 10;
vidmode.framebuffer_green_field_position = 5;
vidmode.framebuffer_blue_field_position = 0;
break;
case 16:
vidmode.framebuffer_red_mask_size = vidmode.framebuffer_blue_mask_size = 5;
vidmode.framebuffer_green_mask_size = 6;
vidmode.framebuffer_red_field_position = 11;
vidmode.framebuffer_green_field_position = 5;
vidmode.framebuffer_blue_field_position = 0;
break;
default:
vidmode.framebuffer_red_mask_size = vidmode.framebuffer_green_mask_size =
vidmode.framebuffer_blue_mask_size = 8;
vidmode.framebuffer_red_field_position = 16;
vidmode.framebuffer_green_field_position = 8;
vidmode.framebuffer_blue_field_position = 0;
break;
}
}
} else {
if(vidmode.framebuffer_addr) {
mbox[0] = 8*4;
mbox[1] = MBOX_REQUEST;
mbox[2] = 0x48001; /* release buffer */
mbox[3] = 8;
mbox[4] = 8;
mbox[5] = (uint32_t)vidmode.framebuffer_addr;
mbox[6] = 0;
mbox[7] = 0;
mbox_call(MBOX_CH_PROP);
vidmode.framebuffer_addr = 0;
}
vidmode.framebuffer_width = vidmode.framebuffer_height = vidmode.framebuffer_bpp = 0;
}
}
/**************** Early boot console ****************/
#define GPFSEL0 ((volatile uint32_t*)(mmio_base+0x00200000))
#define GPFSEL1 ((volatile uint32_t*)(mmio_base+0x00200004))
#define GPFSEL2 ((volatile uint32_t*)(mmio_base+0x00200008))
#define GPFSEL3 ((volatile uint32_t*)(mmio_base+0x0020000C))
#define GPFSEL4 ((volatile uint32_t*)(mmio_base+0x00200010))
#define GPFSEL5 ((volatile uint32_t*)(mmio_base+0x00200014))
#define GPSET0 ((volatile uint32_t*)(mmio_base+0x0020001C))
#define GPSET1 ((volatile uint32_t*)(mmio_base+0x00200020))
#define GPCLR0 ((volatile uint32_t*)(mmio_base+0x00200028))
#define GPLEV0 ((volatile uint32_t*)(mmio_base+0x00200034))
#define GPLEV1 ((volatile uint32_t*)(mmio_base+0x00200038))
#define GPEDS0 ((volatile uint32_t*)(mmio_base+0x00200040))
#define GPEDS1 ((volatile uint32_t*)(mmio_base+0x00200044))
#define GPHEN0 ((volatile uint32_t*)(mmio_base+0x00200064))
#define GPHEN1 ((volatile uint32_t*)(mmio_base+0x00200068))
#define GPPUD ((volatile uint32_t*)(mmio_base+0x00200094))
#define GPPUDCLK0 ((volatile uint32_t*)(mmio_base+0x00200098))
#define GPPUDCLK1 ((volatile uint32_t*)(mmio_base+0x0020009C))
#define UART0_DR ((volatile uint32_t*)(mmio_base+0x00201000))
#define UART0_FR ((volatile uint32_t*)(mmio_base+0x00201018))
#define UART0_IBRD ((volatile uint32_t*)(mmio_base+0x00201024))
#define UART0_FBRD ((volatile uint32_t*)(mmio_base+0x00201028))
#define UART0_LCRH ((volatile uint32_t*)(mmio_base+0x0020102C))
#define UART0_CR ((volatile uint32_t*)(mmio_base+0x00201030))
#define UART0_IMSC ((volatile uint32_t*)(mmio_base+0x00201038))
#define UART0_ICR ((volatile uint32_t*)(mmio_base+0x00201044))
#ifdef CONSOLE_FB
uint8_t key_tga[645+3] = { 0,1,9,0,0,35,0,24,0,0,0,0,24,0,23,0,8,32,0,0,0,92,95,100,95,96,98,101,99,103,100,100,106,103,104,106,107,108,113,111,113,118,116,116,121,125,125,131,131,133,139,139,140,147,142,144,148,146,150,153,152,156,162,158,163,168,161,166,170,169,173,178,175,178,184,179,183,189,185,190,194,189,196,199,196,200,204,200,205,208,205,210,213,212,217,220,217,222,225,221,226,229,224,227,228,224,228,231,226,231,234,227,232,235,232,238,242,234,240,243,0,0,0,6,0,28,25,24,22,21,19,139,18,4,19,21,23,28,0,11,28,27,29,26,25,24,22,21,20,19,18,18,133,19,5,20,22,24,25,29,23,3,25,26,27,30,131,27,1,25,25,130,24,0,23,130,22,6,21,21,24,27,30,30,23,23,23,25,25,29,33,32,31,30,27,27,26,25,25,24,24,23,22,22,21,21,31,29,27,26,23,22,23,24,27,32,32,31,30,27,27,26,25,25,24,24,23,22,22,21,20,30,27,26,25,20,19,21,22,26,32,32,31,30,27,27,26,25,25,24,24,23,22,22,21,20,29,130,25,23,17,19,20,25,33,32,31,30,27,27,26,25,25,24,24,23,22,22,21,20,27,25,24,24,23,16,17,18,24,33,32,31,30,27,27,26,25,25,24,24,23,22,22,21,21,27,25,24,23,23,14,16,17,24,33,32,31,30,27,27,26,25,25,24,24,23,22,22,21,20,26,24,23,22,23,13,14,15,23,33,32,31,30,27,27,26,25,25,24,24,23,22,22,21,20,25,23,22,22,23,13,13,14,22,32,32,31,30,27,27,26,25,25,24,24,23,22,22,21,20,25,22,21,22,130,13,20,22,32,32,31,30,27,27,26,25,25,24,24,23,22,22,21,20,25,21,21,22,130,13,20,22,32,32,31,30,30,27,26,25,25,24,24,23,22,21,21,20,25,22,21,22,20,13,14,13,22,32,32,31,30,27,27,26,25,25,24,24,22,22,21,21,20,25,130,22,23,13,13,11,21,32,32,31,30,27,27,26,25,25,24,24,22,22,21,21,20,23,20,22,22,23,12,11,10,21,32,32,31,30,27,26,26,25,25,24,24,22,22,21,21,20,22,17,20,22,16,10,10,9,17,32,32,31,30,27,26,26,25,25,24,24,22,22,130,21,3,19,14,17,19,23,9,9,7,6,25,33,32,31,31,30,30,27,27,26,25,24,24,23,24,23,9,11,14,17,1,8,7,130,4,2,10,11,12,134,13,8,12,11,10,9,4,6,9,11,14,0,7,130,4,3,6,7,8,9,131,10,131,9,7,8,7,6,4,4,6,9,10,11,3,4,4,6,7,8,9,9,10,9,10,10,132,9,6,8,7,6,4,4,6,3,4,2,4,6,7,8,140,9,5,8,8,6,4,1,2,1,0,2,147,5,1,2,0 };
uint8_t font_sfn[2174+4] = { 83,70,78,50,126,8,0,0,0,0,8,16,11,13,38,0,15,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,128,1,24,24,128,0,126,128,9,91,64,1,65,64,1,65,64,1,109,128,6,24,60,60,60,24,24,24,128,3,102,102,102,36,128,8,54,54,127,54,54,54,127,54,54,128,13,24,24,62,99,67,3,62,96,96,97,99,62,24,24,128,7,67,99,48,24,12,6,99,97,128,9,28,54,54,28,110,59,51,51,51,110,128,3,12,12,12,4,128,9,48,24,12,12,12,12,12,12,24,48,128,9,12,24,48,48,48,48,48,48,24,12,128,4,102,60,255,60,102,128,4,24,24,126,24,24,128,3,24,24,24,12,128,0,127,128,7,64,96,48,24,12,6,3,1,128,9,28,54,99,99,107,107,99,99,54,28,128,9,24,28,30,24,24,24,24,24,24,126,128,9,62,99,96,48,24,12,6,3,99,127,128,9,62,99,96,96,60,96,96,96,99,62,128,9,48,56,60,54,51,127,48,48,48,120,128,9,127,3,3,3,63,96,96,96,99,62,128,9,28,6,3,3,63,99,99,99,99,62,128,9,127,99,96,96,48,24,12,12,12,12,128,9,62,99,99,99,62,99,99,99,99,62,128,9,62,99,99,99,126,96,96,96,48,30,128,2,24,24,12,128,8,96,48,24,12,6,12,24,48,96,128,8,6,12,24,48,96,48,24,12,6,128,6,62,99,99,48,24,24,24,128,8,62,99,99,123,123,123,59,3,62,128,9,8,28,54,99,99,127,99,99,99,99,128,9,63,102,102,102,62,102,102,102,102,63,128,9,60,102,67,3,3,3,3,67,102,60,128,9,31,54,102,102,102,102,102,102,54,31,128,9,127,102,70,22,30,22,6,70,102,127,128,9,127,102,70,22,30,22,6,6,6,15,128,9,60,102,67,3,3,123,99,99,102,92,128,9,99,99,99,99,127,99,99,99,99,99,128,9,60,24,24,24,24,24,24,24,24,60,128,9,120,48,48,48,48,48,51,51,51,30,128,9,103,102,102,54,30,30,54,102,102,103,128,9,15,6,6,6,6,6,6,70,102,127,128,9,99,119,127,127,107,99,99,99,99,99,128,9,99,103,111,127,123,115,99,99,99,99,128,9,62,99,99,99,99,99,99,99,99,62,128,9,63,102,102,102,62,6,6,6,6,15,128,11,62,99,99,99,99,99,99,107,123,62,48,112,128,9,63,102,102,102,62,54,102,102,102,103,128,9,62,99,99,6,28,48,96,99,99,62,128,9,126,126,90,24,24,24,24,24,24,60,128,9,99,99,99,99,99,99,99,99,99,62,128,9,99,99,99,99,99,99,99,54,28,8,128,9,99,99,99,99,107,107,107,127,119,54,128,9,99,99,54,62,28,28,62,54,99,99,128,9,102,102,102,102,60,24,24,24,24,60,128,9,127,99,97,48,24,12,6,67,99,127,128,9,60,12,12,12,12,12,12,12,12,60,128,8,1,3,7,14,28,56,112,96,64,128,9,60,48,48,48,48,48,48,48,48,60,128,3,8,28,54,99,128,0,255,128,2,12,12,24,128,6,30,48,62,51,51,51,110,128,9,7,6,6,30,54,102,102,102,102,62,128,6,62,99,3,3,3,99,62,128,9,56,48,48,60,54,51,51,51,51,110,128,6,62,99,127,3,3,99,62,128,9,28,54,38,6,15,6,6,6,6,15,128,9,110,51,51,51,51,51,62,48,51,30,128,9,7,6,6,54,110,102,102,102,102,103,128,6,28,24,24,24,24,24,60,128,1,96,96,128,9,112,96,96,96,96,96,96,102,102,60,128,9,7,6,6,102,54,30,30,54,102,103,128,9,28,24,24,24,24,24,24,24,24,60,128,6,55,127,107,107,107,107,99,128,6,59,102,102,102,102,102,102,128,6,62,99,99,99,99,99,62,128,9,59,102,102,102,102,102,62,6,6,15,128,9,110,51,51,51,51,51,62,48,48,120,128,6,59,110,102,6,6,6,15,128,6,62,99,6,28,48,99,62,128,9,8,12,12,63,12,12,12,12,108,56,128,6,51,51,51,51,51,51,110,128,6,102,102,102,102,102,60,24,128,6,99,99,107,107,107,127,54,128,6,99,54,28,28,28,54,99,128,9,99,99,99,99,99,99,126,96,48,31,128,6,127,51,24,12,6,99,127,128,9,112,24,24,24,14,24,24,24,24,112,128,11,24,24,24,24,24,24,24,24,24,24,24,24,128,9,14,24,24,24,112,24,24,24,24,14,128,1,110,59,128,6,8,28,54,99,99,99,127,0,1,8,16,9,0,0,2,45,0,0,158,0,0,8,16,9,0,0,2,8,16,9,0,0,2,57,0,0,0,10,38,0,0,0,1,8,16,9,0,0,1,66,0,0,0,1,8,16,9,0,0,3,72,0,0,0,1,8,16,9,0,0,0,83,0,0,0,1,8,16,9,0,0,4,99,0,0,0,1,8,16,9,0,0,2,109,0,0,0,1,8,16,9,0,0,1,121,0,0,0,1,8,16,9,0,0,2,127,0,0,0,1,8,16,9,0,0,2,139,0,0,0,1,8,16,9,0,0,5,151,0,0,0,1,8,16,9,0,0,5,158,0,0,0,1,8,16,9,0,0,9,165,0,0,0,1,8,16,9,0,0,7,171,0,0,0,1,8,16,9,0,0,10,38,0,0,0,1,8,16,9,0,0,4,174,0,0,0,1,8,16,9,0,0,2,184,0,0,0,1,8,16,9,0,0,2,196,0,0,0,1,8,16,9,0,0,2,208,0,0,0,1,8,16,9,0,0,2,220,0,0,0,1,8,16,9,0,0,2,232,0,0,0,1,8,16,9,0,0,2,244,0,0,0,1,8,16,9,0,0,2,0,1,0,0,1,8,16,9,0,0,2,12,1,0,0,1,8,16,9,0,0,2,24,1,0,0,1,8,16,9,0,0,2,36,1,0,0,2,8,16,9,0,0,4,38,0,0,0,9,38,0,0,0,2,8,16,9,0,0,4,38,0,0,0,9,48,1,0,0,1,8,16,9,0,0,3,53,1,0,0,2,8,16,9,0,0,5,42,0,0,0,8,42,0,0,0,1,8,16,9,0,0,3,64,1,0,0,2,8,16,9,0,0,2,75,1,0,0,10,38,0,0,0,1,8,16,9,0,0,3,84,1,0,0,1,8,16,9,0,0,2,95,1,0,0,1,8,16,9,0,0,2,107,1,0,0,1,8,16,9,0,0,2,119,1,0,0,1,8,16,9,0,0,2,131,1,0,0,1,8,16,9,0,0,2,143,1,0,0,1,8,16,9,0,0,2,155,1,0,0,1,8,16,9,0,0,2,167,1,0,0,1,8,16,9,0,0,2,179,1,0,0,1,8,16,9,0,0,2,191,1,0,0,1,8,16,9,0,0,2,203,1,0,0,1,8,16,9,0,0,2,215,1,0,0,1,8,16,9,0,0,2,227,1,0,0,1,8,16,9,0,0,2,239,1,0,0,1,8,16,9,0,0,2,251,1,0,0,1,8,16,9,0,0,2,7,2,0,0,1,8,16,9,0,0,2,19,2,0,0,1,8,16,9,0,0,2,31,2,0,0,1,8,16,9,0,0,2,45,2,0,0,1,8,16,9,0,0,2,57,2,0,0,1,8,16,9,0,0,2,69,2,0,0,1,8,16,9,0,0,2,81,2,0,0,1,8,16,9,0,0,2,93,2,0,0,1,8,16,9,0,0,2,105,2,0,0,1,8,16,9,0,0,2,117,2,0,0,1,8,16,9,0,0,2,129,2,0,0,1,8,16,9,0,0,2,141,2,0,0,1,8,16,9,0,0,2,153,2,0,0,1,8,16,9,0,0,3,165,2,0,0,1,8,16,9,0,0,2,176,2,0,0,1,8,16,9,0,0,0,188,2,0,0,1,8,16,9,0,0,13,194,2,0,0,1,8,16,9,0,0,0,197,2,0,0,1,8,16,9,0,0,5,202,2,0,0,1,8,16,9,0,0,2,211,2,0,0,1,8,16,9,0,0,5,223,2,0,0,1,8,16,9,0,0,2,232,2,0,0,1,8,16,9,0,0,5,244,2,0,0,1,8,16,9,0,0,2,253,2,0,0,1,8,16,9,0,0,5,9,3,0,0,1,8,16,9,0,0,2,21,3,0,0,2,8,16,9,0,0,2,38,0,0,0,5,33,3,0,0,2,8,16,9,0,0,2,42,3,0,0,5,46,3,0,0,1,8,16,9,0,0,2,58,3,0,0,1,8,16,9,0,0,2,70,3,0,0,1,8,16,9,0,0,5,82,3,0,0,1,8,16,9,0,0,5,91,3,0,0,1,8,16,9,0,0,5,100,3,0,0,1,8,16,9,0,0,5,109,3,0,0,1,8,16,9,0,0,5,121,3,0,0,1,8,16,9,0,0,5,133,3,0,0,1,8,16,9,0,0,5,142,3,0,0,1,8,16,9,0,0,2,151,3,0,0,1,8,16,9,0,0,5,163,3,0,0,1,8,16,9,0,0,5,172,3,0,0,1,8,16,9,0,0,5,181,3,0,0,1,8,16,9,0,0,5,190,3,0,0,1,8,16,9,0,0,5,199,3,0,0,1,8,16,9,0,0,5,211,3,0,0,1,8,16,9,0,0,2,220,3,0,0,1,8,16,9,0,0,2,232,3,0,0,1,8,16,9,0,0,2,246,3,0,0,1,8,16,9,0,0,2,2,4,0,0,1,8,16,9,0,0,4,6,4,0,159,0,0,0,0,9,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,254,255,254,255,254,255,254,255,195,95,50,78,70,83 };
typedef struct { uint8_t magic[4]; uint32_t size; uint8_t type; uint8_t features; uint8_t width; uint8_t height; uint8_t baseline; uint8_t underline; uint16_t fragments_offs; uint32_t characters_offs; uint32_t ligature_offs; uint32_t kerning_offs; uint32_t cmap_offs; } __attribute__((packed)) ssfn_t;
ssfn_t *font = (ssfn_t*)font_sfn;
uint32_t fb_x, fb_y;
#endif
/**
* Initialize the console
*/
void console_init(void)
{
#ifdef CONSOLE_SERIAL
uint32_t r;
#endif
/* initialize UART */
*UART0_CR = 0; /* turn off UART0 */
#ifdef CONSOLE_SERIAL
/* set up clock for consistent divisor values */
mbox[0] = 8*4;
mbox[1] = MBOX_REQUEST;
mbox[2] = 0x38002; /* set clock rate */
mbox[3] = 12;
mbox[4] = 8;
mbox[5] = 2; /* UART clock */
mbox[6] = 4000000; /* 4Mhz */
mbox[7] = 0; /* set turbo */
mbox_call(MBOX_CH_PROP);
r = *GPFSEL1;
r &= ~((7<<12)|(7<<15));/* gpio14, gpio15 */
r |= (4<<12)|(4<<15); /* alt0 */
*GPFSEL1 = r;
*GPPUD = 0; /* enable pins 14 and 15 */
for(r = 0; r < 150; r++) __asm__ __volatile__("nop");
*GPPUDCLK0 = (1<<14)|(1<<15);
for(r = 0; r < 150; r++) __asm__ __volatile__("nop");
*GPPUDCLK0 = 0; /* flush GPIO setup */
*UART0_ICR = 0x7FF; /* clear interrupts */
*UART0_IBRD = 2; /* 115200 baud */
*UART0_FBRD = 0xB;
*UART0_LCRH = 0x03<<5; /* 8n1 */
*UART0_CR = 0x301; /* enable Tx, Rx, FIFO */
#endif
#ifdef CONSOLE_FB
fb_x = fb_y = 4;
#endif
cons = 3;
}
/**
* Display a character on boot console
*/
void console_putc(uint32_t c)
{
#if defined(CONSOLE_FB) | defined(CONSOLE_SERIAL)
int i;
#endif
#ifdef CONSOLE_SERIAL
uint8_t str[4];
#endif
#ifdef CONSOLE_FB
uintptr_t o, p;
uint32_t j, k, l, m, n, bpp = ((vidmode.framebuffer_bpp + 7) >> 3), C;
uint8_t *ptr, *chr, *frg, *fb = (uint8_t*)vidmode.framebuffer_addr;
if(fb && (cons & 2))
switch(c) {
case '\r': fb_x = 4; break;
case '\n': fb_x = 4; fb_y += font->height; break;
default:
if(fb_x + font->width + 5 >= vidmode.framebuffer_width) { fb_x = 4; fb_y += font->height; }
if(fb_y + font->height + 5 > vidmode.framebuffer_height) {
j = fb_y; fb_y = vidmode.framebuffer_height - font->height - 5; j -= fb_y;
m = 0; l = j * vidmode.framebuffer_pitch;
for(k = j; k < vidmode.framebuffer_height; k++,
m += vidmode.framebuffer_pitch, l += vidmode.framebuffer_pitch)
memcpy(fb + m, fb + l, vidmode.framebuffer_pitch);
for(k = fb_y, m = fb_y * vidmode.framebuffer_pitch; k < vidmode.framebuffer_height; k++,
m += vidmode.framebuffer_pitch)
memset(fb + m, 0, vidmode.framebuffer_pitch);
}
for(ptr = (uint8_t*)font + font->characters_offs, chr = 0, i = 0; i < 0x110000; i++) {
if(ptr[0] == 0xFF) { i += 65535; ptr++; }
else if((ptr[0] & 0xC0) == 0xC0) { j = (((ptr[0] & 0x3F) << 8) | ptr[1]); i += j; ptr += 2; }
else if((ptr[0] & 0xC0) == 0x80) { j = (ptr[0] & 0x3F); i += j; ptr++; }
else { if((unsigned int)i == c) { chr = ptr; break; } ptr += 6 + ptr[1] * (ptr[0] & 0x40 ? 6 : 5); }
}
if(!chr) chr = (uint8_t*)font + font->characters_offs;
ptr = chr + 6; o = (uintptr_t)fb + fb_y * vidmode.framebuffer_pitch + fb_x * bpp;
for(i = n = 0; i < chr[1]; i++, ptr += chr[0] & 0x40 ? 6 : 5) {
if(ptr[0] == 255 && ptr[1] == 255) continue;
frg = (uint8_t*)font + (chr[0] & 0x40 ? ((ptr[5] << 24) | (ptr[4] << 16) | (ptr[3] << 8) | ptr[2]) :
((ptr[4] << 16) | (ptr[3] << 8) | ptr[2]));
if((frg[0] & 0xE0) != 0x80) continue;
o += (int)(ptr[1] - n) * vidmode.framebuffer_pitch; n = ptr[1];
k = ((frg[0] & 0x1F) + 1) << 3; j = frg[1] + 1; frg += 2;
for(m = 1; j; j--, n++, o += vidmode.framebuffer_pitch)
for(p = o, l = 0; l < k; l++, p += bpp, m <<= 1) {
if(m > 0x80) { frg++; m = 1; }
if((*frg & m) || !(cons & 8)) {
C = *frg & m ? fb_fg : fb_bg;
switch(vidmode.framebuffer_bpp) {
case 15: case 16: *((uint16_t*)p) = C; break;
case 24: case 32: *((uint32_t*)p) = C; break;
}
}
}
}
fb_x += chr[4]; fb_y += chr[5];
break;
}
#endif
#ifdef CONSOLE_SERIAL
if(cons & 1) {
if(c<0x80) { str[0] = c; str[1] = 0; } else
if(c<0x800) { str[0]=((c>>6)&0x1F)|0xC0; str[1]=(c&0x3F)|0x80; str[2] = 0; }
else { str[0]=((c>>12)&0x0F)|0xE0; str[1]=((c>>6)&0x3F)|0x80; str[2]=(c&0x3F)|0x80; str[3] = 0; }
if(str[0] == '\n') { str[0] = '\r'; str[1] = '\n'; }
for(i = 0; str[i]; i++) {
do{ __asm__ __volatile__("nop");} while(*UART0_FR&0x20); *UART0_DR=str[i];
}
}
#endif
(void)c;
}
/**
* Display (extremely minimal) formated message on console
* %c: an ASCII character
* %d: a decimal number
* %x: a hexadecimal number
* %p: a pointer
* %s: a zero terminated ASCII string (8 bit)
* %S: a zero terminated WCHAR string (16 bit characters, truncated to 8 bit)
* %D: dump 16 bytes from given address
*/
PLG_API void printf(char *fmt, ...)
{
__builtin_va_list args;
uint8_t *ptr;
int64_t arg;
uint16_t *u;
int len, sign, i, l;
char *p, tmpstr[19], n;
__builtin_va_start(args, fmt);
arg = 0;
while(*fmt) {
if(*fmt == '%') {
fmt++;
if(*fmt == '%') goto put;
len=l=0; while(*fmt >= '0' && *fmt <= '9') { len *= 10; len += *fmt - '0'; fmt++; }
if(*fmt == 'l') { l++; fmt++; }
if(*fmt == 'c') { arg = __builtin_va_arg(args, int); console_putc((uint8_t)arg); fmt++; continue; } else
if(*fmt == 'd') {
if(!l) arg = (int32_t)__builtin_va_arg(args, int32_t);
else arg = __builtin_va_arg(args, int64_t);
sign = 0; if((int)arg < 0) { arg = -arg; sign++; }
i = 18; tmpstr[i] = 0;
do { tmpstr[--i] = '0' + (arg % 10); arg /= 10; } while(arg != 0 && i > 0);
if(sign) tmpstr[--i] = '-';
if(len > 0 && len < 18) { while(i > 18 - len) tmpstr[--i] = ' '; }
p = &tmpstr[i];
goto putstring;
} else
if(*fmt == 'x' || *fmt == 'p') {
if(*fmt == 'x' && !l) arg = (int32_t)__builtin_va_arg(args, int32_t);
else arg = __builtin_va_arg(args, int64_t);
i = 16; tmpstr[i] = 0; if(*fmt == 'p') len = 16;
do { n = arg & 0xf; tmpstr[--i] = n + (n > 9 ? 0x37 : 0x30); arg >>= 4; } while(arg != 0 && i > 0);
if(len > 0 && len <= 16) { while(i > 16 - len) tmpstr[--i] = '0'; }
p = &tmpstr[i];
goto putstring;
} else
if(*fmt == 's') {
p = __builtin_va_arg(args, char*);
putstring: if(p == (void*)0) p = "(null)";
while(*p) console_putc(utf8(&p));
}
if(*fmt == 'S') {
u = __builtin_va_arg(args, uint16_t*);
if(u == (void*)0) u = L"(null)";
while(*u) console_putc(*u++);
} else
if(*fmt == 'D') {
arg = __builtin_va_arg(args, int64_t);
if(len < 1) len = 1;
do {
for(i = 28; i >= 0; i -= 4) { n = (arg >> i) & 15; n += (n>9?0x37:0x30); console_putc(n); }
console_putc(':'); console_putc(' ');
ptr = (uint8_t*)(uintptr_t)arg;
for(i = 0; i < 16; i++) {
n = (ptr[i] >> 4) & 15; n += (n>9?0x37:0x30); console_putc(n);
n = ptr[i] & 15; n += (n>9?0x37:0x30); console_putc(n);
console_putc(' ');
}
console_putc(' ');
for(i = 0; i < 16; i++)
console_putc(ptr[i] < 32 || ptr[i] >= 127 ? '.' : ptr[i]);
console_putc('\n');
arg += 16;
} while(--len);
}
fmt++;
} else {
put: console_putc(utf8(&fmt));
}
}
__builtin_va_end(args);
}
/**************** Progress bar ****************/
/**
* Initialize the progress bar
*/
PLG_API uint64_t pb_init(uint64_t size)
{
uint32_t i, x;
pb_fb = NULL; pb_m = (size >> 9) + 1; pb_l = 0;
if(!vidmode.framebuffer_addr || !size || pb_m < vidmode.framebuffer_width - 4) return 0;
pb_b = (vidmode.framebuffer_bpp + 7) >> 3;
pb_fb = (uint8_t*)vidmode.framebuffer_addr + (vidmode.framebuffer_height - 4) * vidmode.framebuffer_pitch + 2 * pb_b;
for(i = x = 0; x < vidmode.framebuffer_width - 4; x++, i += pb_b)
switch(vidmode.framebuffer_bpp) {
case 15: case 16: *((uint16_t*)(pb_fb + i)) = *((uint16_t*)(pb_fb + vidmode.framebuffer_pitch + i)) = pb_bg; break;
case 24: case 32: *((uint32_t*)(pb_fb + i)) = *((uint32_t*)(pb_fb + vidmode.framebuffer_pitch + i)) = pb_bg; break;
}
return size / (vidmode.framebuffer_width - 4);
}
/**
* Draw the progress bar
*/
PLG_API void pb_draw(uint64_t curr)
{
uint32_t i;
if(pb_fb) {
i = pb_l; pb_l = ((curr >> 9) * (vidmode.framebuffer_width - 4) / pb_m) * pb_b;
for(; i < pb_l; i++)
switch(vidmode.framebuffer_bpp) {
case 15: case 16: *((uint16_t*)(pb_fb + i)) = *((uint16_t*)(pb_fb + vidmode.framebuffer_pitch + i)) = fb_fg; break;
case 24: case 32: *((uint32_t*)(pb_fb + i)) = *((uint32_t*)(pb_fb + vidmode.framebuffer_pitch + i)) = fb_fg; break;
}
}
}
/**
* Close the progress bar
*/
PLG_API void pb_fini(void)
{
uint32_t i, x;
if(pb_fb)
for(i = x = 0; x < vidmode.framebuffer_width - 2; x++, i += pb_b)
switch(vidmode.framebuffer_bpp) {
case 15: case 16: *((uint16_t*)(pb_fb + i)) = *((uint16_t*)(pb_fb + vidmode.framebuffer_pitch + i)) = fb_bg; break;
case 24: case 32: *((uint32_t*)(pb_fb + i)) = *((uint32_t*)(pb_fb + vidmode.framebuffer_pitch + i)) = fb_bg; break;
}
pb_fb = NULL;
}
/**************** EMMC SDCard functions ****************/
#define EMMC_ARG2 ((volatile uint32_t*)(emmc_base+0x00000000))
#define EMMC_BLKSIZECNT ((volatile uint32_t*)(emmc_base+0x00000004))
#define EMMC_ARG1 ((volatile uint32_t*)(emmc_base+0x00000008))
#define EMMC_CMDTM ((volatile uint32_t*)(emmc_base+0x0000000C))
#define EMMC_RESP0 ((volatile uint32_t*)(emmc_base+0x00000010))
#define EMMC_RESP1 ((volatile uint32_t*)(emmc_base+0x00000014))
#define EMMC_RESP2 ((volatile uint32_t*)(emmc_base+0x00000018))
#define EMMC_RESP3 ((volatile uint32_t*)(emmc_base+0x0000001C))
#define EMMC_DATA ((volatile uint32_t*)(emmc_base+0x00000020))
#define EMMC_STATUS ((volatile uint32_t*)(emmc_base+0x00000024))
#define EMMC_CONTROL0 ((volatile uint32_t*)(emmc_base+0x00000028))
#define EMMC_CONTROL1 ((volatile uint32_t*)(emmc_base+0x0000002C))
#define EMMC_INTERRUPT ((volatile uint32_t*)(emmc_base+0x00000030))
#define EMMC_INT_MASK ((volatile uint32_t*)(emmc_base+0x00000034))
#define EMMC_INT_EN ((volatile uint32_t*)(emmc_base+0x00000038))
#define EMMC_CONTROL2 ((volatile uint32_t*)(emmc_base+0x0000003C))
#define EMMC_SLOTISR_VER ((volatile uint32_t*)(emmc_base+0x000000FC))
#define CMD_NEED_APP 0x80000000
#define CMD_RSPNS_48 0x00020000
#define CMD_ERRORS_MASK 0xfff9c004
#define CMD_RCA_MASK 0xffff0000
#define CMD_GO_IDLE 0x00000000
#define CMD_ALL_SEND_CID 0x02010000
#define CMD_SEND_REL_ADDR 0x03020000
#define CMD_CARD_SELECT 0x07030000
#define CMD_SEND_IF_COND 0x08020000
#define CMD_STOP_TRANS 0x0C030000
#define CMD_READ_SINGLE 0x11220010
#define CMD_READ_MULTI 0x12220032
#define CMD_SET_BLOCKCNT 0x17020000
#define CMD_APP_CMD 0x37000000
#define CMD_SET_BUS_WIDTH (0x06020000|CMD_NEED_APP)
#define CMD_SEND_OP_COND (0x29020000|CMD_NEED_APP)
#define CMD_SEND_SCR (0x33220010|CMD_NEED_APP)
#define SR_READ_AVAILABLE 0x00000800
#define SR_DAT_INHIBIT 0x00000002
#define SR_CMD_INHIBIT 0x00000001
#define SR_APP_CMD 0x00000020
#define INT_DATA_TIMEOUT 0x00100000
#define INT_CMD_TIMEOUT 0x00010000
#define INT_READ_RDY 0x00000020
#define INT_CMD_DONE 0x00000001
#define INT_ERROR_MASK 0x017E8000
#define C0_SPI_MODE_EN 0x00100000
#define C0_HCTL_HS_EN 0x00000004
#define C0_HCTL_DWITDH 0x00000002
#define C1_SRST 0x07000000
#define C1_SRST_DATA 0x04000000
#define C1_SRST_CMD 0x02000000
#define C1_SRST_HC 0x01000000
#define C1_TOUNIT_DIS 0x000f0000
#define C1_TOUNIT_MAX 0x000e0000
#define C1_CLK_GENSEL 0x00000020
#define C1_CLK_EN 0x00000004
#define C1_CLK_STABLE 0x00000002
#define C1_CLK_INTLEN 0x00000001
#define HOST_SPEC_NUM 0x00ff0000
#define HOST_SPEC_NUM_SHIFT 16
#define HOST_SPEC_V3 2
#define HOST_SPEC_V2 1
#define HOST_SPEC_V1 0
#define SCR_SD_BUS_WIDTH_4 0x00000400
#define SCR_SUPP_SET_BLKCNT 0x02000000
#define SCR_SUPP_CCS 0x00000001
#define ACMD41_VOLTAGE 0x00ff8000
#define ACMD41_CMD_COMPLETE 0x80000000
#define ACMD41_CMD_CCS 0x40000000
#define ACMD41_ARG_HC 0x51ff8000
#define SD_OK 0
#define SD_TIMEOUT -1
#define SD_ERROR -2
uint32_t sd_scr[2], sd_ocr, sd_rca, sd_hv;
int sd_err;
uint64_t cntfrq;
/* delay cnt microsec */
void delayms(uint32_t cnt) {
uint64_t t,r;
if(!cntfrq) __asm__ __volatile__ ("mrs %0, cntfrq_el0" : "=r" (cntfrq));
__asm__ __volatile__ ("mrs %0, cntpct_el0" : "=r" (t));
t+=((cntfrq/1000)*cnt)/1000;do{__asm__ __volatile__ ("mrs %0, cntpct_el0" : "=r" (r));}while(r<t);
}
/**
* Wait for data or command ready
*/
int sd_status(uint32_t mask)
{
int cnt = 500000; while((*EMMC_STATUS & mask) && !(*EMMC_INTERRUPT & INT_ERROR_MASK) && cnt--) delayms(1);
return (cnt <= 0 || (*EMMC_INTERRUPT & INT_ERROR_MASK)) ? SD_ERROR : SD_OK;
}
/**
* Wait for interrupt
*/
int sd_int(uint32_t mask)
{
uint32_t r, m=mask | INT_ERROR_MASK;
int cnt = 1000000; while(!(*EMMC_INTERRUPT & m) && cnt--) delayms(1);
r=*EMMC_INTERRUPT;
if(cnt<=0 || (r & INT_CMD_TIMEOUT) || (r & INT_DATA_TIMEOUT) ) { *EMMC_INTERRUPT=r; return SD_TIMEOUT; } else
if(r & INT_ERROR_MASK) { *EMMC_INTERRUPT=r; return SD_ERROR; }
*EMMC_INTERRUPT=mask;
return 0;
}
/**
* Send a command
*/
int sd_cmd(uint32_t code, uint32_t arg)
{
uint32_t r=0;
sd_err=SD_OK;
if(code&CMD_NEED_APP) {
r=sd_cmd(CMD_APP_CMD|(sd_rca?CMD_RSPNS_48:0),sd_rca);
if(sd_rca && !r) { printf("EMMC: failed to send SD APP command\n"); sd_err=SD_ERROR;return 0;}
code &= ~CMD_NEED_APP;
}
if(sd_status(SR_CMD_INHIBIT)) { printf("EMMC: busy, timed out\n"); sd_err= SD_TIMEOUT;return 0;}
*EMMC_INTERRUPT=*EMMC_INTERRUPT; *EMMC_ARG1=arg; *EMMC_CMDTM=code;
if(code==CMD_SEND_OP_COND) delayms(1000); else
if(code==CMD_SEND_IF_COND || code==CMD_APP_CMD) delayms(100);
if((r=sd_int(INT_CMD_DONE))) {printf("EMMC: failed to send command\n");sd_err=r;return 0;}
r=*EMMC_RESP0;
if(code==CMD_GO_IDLE || code==CMD_APP_CMD) return 0; else
if(code==(CMD_APP_CMD|CMD_RSPNS_48)) return r&SR_APP_CMD; else
if(code==CMD_SEND_OP_COND) return r; else
if(code==CMD_SEND_IF_COND) return r==arg? SD_OK : SD_ERROR; else
if(code==CMD_ALL_SEND_CID) {r|=*EMMC_RESP3; r|=*EMMC_RESP2; r|=*EMMC_RESP1; return r; } else
if(code==CMD_SEND_REL_ADDR) {
sd_err=(((r&0x1fff))|((r&0x2000)<<6)|((r&0x4000)<<8)|((r&0x8000)<<8))&CMD_ERRORS_MASK;
return r&CMD_RCA_MASK;
}
return r&CMD_ERRORS_MASK;
}
/**
* Load a sector from boot drive using EMMC
*/
int sd_loadsec(uint64_t lba, void *dst)
{
int r,d;
uint32_t *buf=(uint32_t *)dst;
#ifdef SD_DEBUG
printf("EMMC: sd_loadsec lba %d dst %lx\n", lba, dst);
#endif
if(sd_status(SR_DAT_INHIBIT)) {sd_err=SD_TIMEOUT; return 0;}
*EMMC_BLKSIZECNT = (1 << 16) | 512;
sd_cmd(CMD_READ_SINGLE, sd_scr[0] & SCR_SUPP_CCS ? lba : lba << 9);
if(sd_err) return 0;
if((r=sd_int(INT_READ_RDY))){printf("\rEMMC: timeout waiting for ready to read\n");sd_err=r;return 0;}
for(d=0;d<128;d++) buf[d] = *EMMC_DATA;
return sd_err!=SD_OK;
}
/**
* set SD clock to frequency in Hz
*/
int sd_clk(uint32_t f)
{
uint32_t d,c=41666666/f,x,s=32,h=0;
int cnt = 100000;
while((*EMMC_STATUS & (SR_CMD_INHIBIT|SR_DAT_INHIBIT)) && cnt--) delayms(1);
if(cnt<=0) {printf("EMMC: timeout waiting for inhibit flag\n"); return SD_ERROR; }
*EMMC_CONTROL1 &= ~C1_CLK_EN; delayms(10);
x=c-1; if(!x) s=0; else {
if(!(x & 0xffff0000u)) { x <<= 16; s -= 16; }
if(!(x & 0xff000000u)) { x <<= 8; s -= 8; }
if(!(x & 0xf0000000u)) { x <<= 4; s -= 4; }
if(!(x & 0xc0000000u)) { x <<= 2; s -= 2; }
if(!(x & 0x80000000u)) { x <<= 1; s -= 1; }
if(s>0) s--;
if(s>7) s=7;
}
if(sd_hv>HOST_SPEC_V2) d=c; else d=(1<<s);
if(d<=2) {d=2;s=0;}
#ifdef SD_DEBUG
printf("sd_clk divisor %x, shift %x\n", d, s);
#endif
if(sd_hv>HOST_SPEC_V2) h=(d&0x300)>>2;
d=(((d&0x0ff)<<8)|h);
*EMMC_CONTROL1=(*EMMC_CONTROL1&0xffff003f)|d; delayms(10);
*EMMC_CONTROL1 |= C1_CLK_EN; delayms(10);
cnt=10000; while(!(*EMMC_CONTROL1 & C1_CLK_STABLE) && cnt--) delayms(10);
if(cnt<=0) {printf("EMMC: failed to get stable clock\n");return SD_ERROR;}
return SD_OK;
}
/**
* initialize EMMC to read SDHC card
*/
int sd_init()
{
long r,cnt,ccs=0;
sd_hv = (*EMMC_SLOTISR_VER & HOST_SPEC_NUM) >> HOST_SPEC_NUM_SHIFT;
#ifdef SD_DEBUG
printf("EMMC: GPIO set up hcl ver %d\n", sd_hv);
#endif
if(sd_hv<HOST_SPEC_V2) {printf("EMMC: SDHCI version too old\n"); return SD_ERROR;}
/* Reset the card. */
*EMMC_CONTROL0 = 0;
r = *EMMC_CONTROL1; r |= C1_SRST_HC; r &= ~(C1_CLK_EN | C1_CLK_INTLEN); *EMMC_CONTROL1 = r;
cnt=10000; do{delayms(10);} while( (*EMMC_CONTROL1 & C1_SRST) && cnt-- );
if(cnt<=0) {printf("EMMC: failed to reset EMMC\n"); return SD_ERROR;}
#ifdef SD_DEBUG
printf("EMMC: reset OK\n");
#endif
*EMMC_CONTROL0 = 0xF << 8; /* set voltage to 3.3 */
*EMMC_CONTROL1 |= C1_CLK_INTLEN | C1_TOUNIT_MAX;
delayms(10);
/* Set clock to setup frequency. */
if((r=sd_clk(400000))) return r;
*EMMC_INT_EN = 0xffffffff;
*EMMC_INT_MASK = 0xffffffff;
sd_scr[0]=sd_scr[1]=sd_rca=sd_err=0;
sd_cmd(CMD_GO_IDLE,0);
if(sd_err) return sd_err;
sd_cmd(CMD_SEND_IF_COND,0x000001AA);
if(sd_err) return sd_err;
cnt=6; r=0; while(!(r&ACMD41_CMD_COMPLETE) && cnt--) {
delayms(1);
r=sd_cmd(CMD_SEND_OP_COND,ACMD41_ARG_HC);
#ifdef SD_DEBUG
printf("EMMC: CMD_SEND_OP_COND returned ");
if(r&ACMD41_CMD_COMPLETE) printf("COMPLETE ");
if(r&ACMD41_VOLTAGE) printf("VOLTAGE ");
if(r&ACMD41_CMD_CCS) printf("CCS ");