-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader_x86.c
More file actions
2943 lines (2803 loc) · 134 KB
/
loader_x86.c
File metadata and controls
2943 lines (2803 loc) · 134 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_x86.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 x86_64
*
* Memory layout when booted on UEFI:
* Arbitrary, uses relocations. Data buffers are allocated through BootServices
* 0x510 - 0x5A0 AP startup data
* 0x8000 - 0x80FF relocated AP startup code
*
* Memory layout when booted on BIOS:
* 0x0 - 0x400 IVT
* 0x400 - 0x4FF BDA
* 0x4FF - 0x500 BIOS boot drive code
* 0x500 - 0x510 BIOS LBA packet
* 0x510 - 0x520 GDT value
* 0x520 - 0x530 IDT value
* 0x580 - 0x600 EDID info
* 0x600 - 0x800 temporary disk, E820 or VESA buffer
* 0x800 - 0xB00 temporary VESA buffer continued
* 0xB00 - 0x1000 stack (ca. 1280 bytes)
* 0x1000 - 0x8000 paging tables
* 0x8000 - 0x20000 our COFF sections (0x8000 - 0x80FF relocated AP startup code)
* 0x20000 - 0x40000 config + tags
* 0x40000 - 0x90000 plugin ids; from the top to bottom: kernel's stack
* 0x90000 - 0x9A000 Linux kernel only: zero page + cmdline
* 0x9A000 - 0xA0000 EBDA
* 0xA0000 - 0xFFFFF VRAM and BIOS ROM
* 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 0x3f8 /* default serial, IO base address of COM1 port */
#define CONSOLE_FB /* on screen too */
/*#define CONSOLE_VGA*/ /* fallback text mode console in BIOS */
/*#define CONSOLE_BOCHS_E9*/ /* bochs E9 port hack */
/*#define CONSOLE_UEFI*/ /* UEFI ConOut */
/* 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 defkernel[64] = "kernel", definitrd[64] = "";
const char copy[] = "Easyboot loader, Copyright (c) 2023 bzt, GPLv3+\n";
#include "loader.h"
/* IMPORTANT: don't assume .bss is zeroed out like in a hosted environment, because it's not */
uint64_t file_size, file_base, file_buf, ram, *pt, hack_buf, krnl_size;
uint32_t fb_fg, fb_bg, pb_bg, pb_b, pb_m, pb_l, verbose, num_memmap, edid_size;
uint8_t cons, *tags_buf, *tags_ptr, *rsdp_ptr, *dsdt_ptr, *edid_ptr, *plgids, *root_buf, *krnl_buf, *pb_fb, in_exc, rddir, smp;
uint64_t data_lba, fat_lba, root_lba, root_siz;
uint8_t vbr[512], data[512];
uint32_t fat[1024], fat_cache, file_clu;
uint16_t lfn[272], wcname[PATH_MAX];
guid_t bootuuid, rootuuid;
esp_bpb_t *bpb;
multiboot_mmap_entry_t *memmap;
multiboot_tag_module_t *initrd;
multiboot_tag_framebuffer_t vidmode;
efi_system_table_t *ST;
efi_handle_t *IM;
efi_boot_services_t *BS;
efi_block_io_t *bio;
efi_file_handle_t *root_dir;
efi_file_handle_t *f;
efi_file_info_t info, dirent;
efi_device_path_t *bootdev;
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_got_t plg_got = {
&verbose, &file_size, &root_buf, &tags_buf, &tags_ptr, &rsdp_ptr, &dsdt_ptr, &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
};
const char excstr[32][3] = { "DE", "DB", "NI", "BP", "OF", "BR", "UD", "DF", "CO", "TS", "NP", "SS", "GP", "PF",
"15", "MF", "AC", "MC", "XF", "20", "CP", "22", "23", "24", "25", "26", "27", "HV", "VC", "SX", "31" };
typedef struct {
char magic[4];
uint32_t size;
uint8_t rev;
uint8_t chksum;
uint8_t res0[30];
uint32_t dsdt;
uint8_t reserved[96];
uint64_t x_dsdt;
} __attribute__((packed)) fadt_t;
#define sleep(n) do { \
__asm__ __volatile__ ( "rdtsc" : "=a"(a),"=d"(b)); d = (((uint64_t)b << 32UL)|(uint64_t)a) + n*(*((uint64_t*)0x548)); \
do { __asm__ __volatile__ ( "pause;rdtsc" : "=a"(a),"=d"(b)); c = ((uint64_t)b << 32UL)|(uint64_t)a; } while(c < d); \
} while(0)
#define send_ipi(a,m,v) do { \
while(*((volatile uint32_t*)(lapic + 0x300)) & (1 << 12)) __asm__ __volatile__ ("pause" : : : "memory"); \
*((volatile uint32_t*)(lapic + 0x310)) = (*((volatile uint32_t*)(lapic + 0x310)) & 0x00ffffff) | (a << 24); \
*((volatile uint32_t*)(lapic + 0x300)) = (*((volatile uint32_t*)(lapic + 0x300)) & m) | v; \
} while(0)
/**************** These will be overriden by the SMP trampoline code ****************/
/* IMPORTANT: compile as "DEBUG=1 make" and check output.map that the text segments starts with these */
/**
* 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;
}
/**************** Mandatory functions, Clang generates calls to them ****************/
PLG_API void memcpy(void *dst, const void *src, uint32_t n) { __asm__ __volatile__("repnz movsb"::"D"(dst),"S"(src),"c"(n):); }
PLG_API void memset(void *dst, uint8_t c, uint32_t n) { __asm__ __volatile__("repnz stosb"::"D"(dst),"a"(c),"c"(n):); }
PLG_API int memcmp(const void *s1, const void *s2, uint32_t n) {
int ret;
__asm__ __volatile__("cld;repe cmpsb;xorl %%eax,%%eax;movb -1(%%rdi), %%al;subb -1(%%rsi), %%al;"
:"=a"(ret)
:"D"(s1),"S"(s2),"c"(n):);
return ret;
}
/**************** Early boot console ****************/
#ifdef CONSOLE_FB
uint8_t key_tga[645] = { 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] = { 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
#ifdef CONSOLE_VGA
uint16_t vga_x, vga_y;
#endif
int fw_map(uint64_t phys, uint64_t virt, uint32_t size);
/**
* Initialize the console
*/
void console_init(void)
{
#ifdef CONSOLE_SERIAL
__asm__ __volatile__(
"movl %0, %%edx;"
"xorb %%al, %%al;outb %%al, %%dx;" /* IER int off */
"movb $0x80, %%al;addb $2,%%dl;outb %%al, %%dx;" /* LCR set divisor mode */
"movb $1, %%al;subb $3,%%dl;outb %%al, %%dx;" /* DLL divisor lo 115200 */
"xorb %%al, %%al;incb %%dl;outb %%al, %%dx;" /* DLH divisor hi */
"incb %%dl;outb %%al, %%dx;" /* FCR fifo off */
"movb $0x43, %%al;incb %%dl;outb %%al, %%dx;" /* LCR 8N1, break on */
"movb $0x8, %%al;incb %%dl;outb %%al, %%dx;" /* MCR Aux out 2 */
"xorb %%al, %%al;subb $4,%%dl;inb %%dx, %%al" /* clear receiver/transmitter */
: : "a"(CONSOLE_SERIAL + 1): "rdx");
#endif
#ifdef CONSOLE_FB
fb_x = fb_y = 4;
#endif
#ifdef CONSOLE_VGA
vga_x = vga_y = 0;
if(!vidmode.framebuffer_addr && !ST) memset((void*)0xB8000, 0, 160 * 25);
#endif
#ifdef CONSOLE_UEFI
if(ST && ST->ConOut) ST->ConOut->Reset(ST->ConOut, 0);
#endif
cons = 3;
}
/**
* Display a character on boot console
*/
void console_putc(uint32_t c)
{
#ifdef CONSOLE_SERIAL
int i;
uint8_t str[4];
#endif
#if defined(CONSOLE_FB) | defined(CONSOLE_VGA)
uint8_t *fb = (uint8_t*)vidmode.framebuffer_addr;
#endif
#ifdef CONSOLE_UEFI
uint16_t tmp[2];
#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;
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
if(cons & 1) {
#ifdef CONSOLE_SERIAL
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++)
__asm__ __volatile__(
"xorl %%ebx, %%ebx; movb %0, %%bl;"
"movl $10000,%%ecx;"
"1:inb %%dx, %%al;pause;"
"cmpb $0xff,%%al;je 2f;"
"dec %%ecx;jz 2f;"
"andb $0x20,%%al;jz 1b;"
"subb $5,%%dl;movb %%bl, %%al;outb %%al, %%dx;2:;"
#ifdef CONSOLE_BOCHS_E9
"movb %%bl, %%al;outb %%al, $0xe9;"
#endif
::"a"(str[i]),"d"(CONSOLE_SERIAL + 5): "rbx", "rcx");
#endif
#ifdef CONSOLE_UEFI
if(ST && ST->ConOut) { tmp[0] = c; tmp[1] = 0; ST->ConOut->OutputString(ST->ConOut, tmp); }
#endif
#ifdef CONSOLE_VGA
if(!fb && !ST)
switch(c) {
case '\r': vga_x = 0; break;
case '\n': vga_x = 0; vga_y++; break;
default:
if(vga_y >= 25) {
memcpy((void*)0xB8000, (void*)0xB8000 + 160, 160 * 24); vga_x = 0; vga_y = 24;
memset((void*)0xB8000 + 24 * 160, 0, 160);
}
*((uint16_t*)((uintptr_t)0xB8000 + vga_y * 160 + vga_x++ * 2)) = 0x0f00 | (c & 0xff);
break;
}
#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 ****************/
void pb_dot(uint32_t i, uint32_t c)
{
switch(vidmode.framebuffer_bpp) {
case 15: case 16: *((uint16_t*)(pb_fb + i)) = *((uint16_t*)(pb_fb + vidmode.framebuffer_pitch + i)) = c; break;
case 24: case 32: *((uint32_t*)(pb_fb + i)) = *((uint32_t*)(pb_fb + vidmode.framebuffer_pitch + i)) = c; break;
}
}
/**
* 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)
pb_dot(i, pb_bg);
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++)
pb_dot(i, fb_fg);
}
}
/**
* 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)
pb_dot(i, fb_bg);
pb_fb = NULL;
}
/**************** BIOS-specific functions ****************/
/* IMPORTANT !!! No bios_X and fw_X functions allowed to use more than 1k stack! CLang should generate a warning if they do
* (actually Clang's stack limit is set to 512 bytes). Also these BIOS routine calls must be in the first 32k in order to work */
/**
* Sort memory map
*/
static void sort_map(multiboot_mmap_entry_t *dst, int num, int fbuf)
{
int i, j;
uint64_t top;
multiboot_mmap_entry_t tmp;
for(i = 1; i < num; i++) {
for(j = i; j > 0 && dst[j].base_addr < dst[j - 1].base_addr; j--) {
memcpy(&tmp, &dst[j - 1], sizeof(multiboot_mmap_entry_t));
memcpy(&dst[j - 1], &dst[j], sizeof(multiboot_mmap_entry_t));
memcpy(&dst[j], &tmp, sizeof(multiboot_mmap_entry_t));
}
top = dst[i].base_addr + dst[i].length;
if(dst[i].type == MULTIBOOT_MEMORY_AVAILABLE) {
if(top > ram) ram = top;
/* since memory allocation also uses file_buf, we replace it to the first free slot's half
* this way segments can be loaded to 1M without interference */
if(fbuf && dst[i].base_addr <= file_base && file_base < top) {
file_base = (dst[i].base_addr + dst[i].length / 2 + 2*1024*1024-1) & ~(2*1024*1024-1);
if(file_base > 64*1024*1024) file_base = 64*1024*1024;
}
}
}
}
/**
* Load a sector from boot drive using BIOS
*/
void bios_loadsec(uint64_t lba, void *dst)
{
if(ST || !dst) return;
*((uint16_t*)0x502) = 1; *((uint32_t*)0x504) = 0x600; *((uint64_t*)0x508) = lba;
*((uint64_t*)dst) = 0;
__asm__ __volatile__(
/* let's see if we have master ATA IDE PIO (probably emulation, but still, it is about 100x times faster than BIOS) */
"cmpb $0x80, (0x4ff);jne 2f;"
"movw $0x1F7, %%dx;inb %%dx, %%al;"
"cmpb $0, %%al;je 2f;"
"cmpb $0xff, %%al;je 2f;"
"1:inb %%dx, %%al;"
"andb $0xC0, %%al;"
"cmpb $0x40, %%al;jne 1b;"
"movb $0x0a, %%al;movw $0x3F6, %%dx;outb %%al, %%dx;"
"movb $0x40, %%al;movw $0x1F6, %%dx;outb %%al, %%dx;"
"xorb %%al, %%al;movw $0x1F2, %%dx;outb %%al, %%dx;"
"movq %%rbx, %%rax;shrq $24, %%rax;movw $0x1F3, %%dx;outb %%al, %%dx;"
"shrq $8, %%rax;movw $0x1F4, %%dx;outb %%al, %%dx;"
"shrq $8, %%rax;movw $0x1F5, %%dx;outb %%al, %%dx;"
"movb $1, %%al;movw $0x1F2, %%dx;outb %%al, %%dx;"
"movq %%rbx, %%rax;movw $0x1F3, %%dx;outb %%al, %%dx;"
"shrq $8, %%rax;movw $0x1F4, %%dx;outb %%al, %%dx;"
"shrq $8, %%rax;movw $0x1F5, %%dx;outb %%al, %%dx;"
"movb $0x24, %%al;movw $0x1F7, %%dx;outb %%al, %%dx;"
"1:inb %%dx, %%al;"
"andb $0xC1, %%al;"
"cmpb $0x01, %%al;je 2f;"
"cmpb $0x40, %%al;jne 1b;"
"movq $128, %%rcx;"
"movw $0x1F0, %%dx;"
"cld;rep insl;"
"jmp 3f;"
/* otherwise fallback to BIOS services, which means switching CPU mode and copy data, so it's slow */
/* go BIOS mode */
"2:pushq %%rdi;"
"movq $0x600, %%rdi;movq $64, %%rcx;xorq %%rax, %%rax;repnz stosq;"
"movq $16, %%rax;push %%rax;" /* long -> compat */
"movq $1f, %%rax;push %%rax;"
"lretq;.code32;1:;"
"movl %%cr0, %%eax;" /* disable CR0.PG */
"btcl $31, %%eax;"
"movl %%eax, %%cr0;"
"ljmp $8,$1f;1:;" /* compat -> legacy prot */
"movl %%cr0, %%eax;" /* disable CR0.PR */
"andb $0xfe, %%al;"
"movl %%eax, %%cr0;"
"ljmp $0,$1f;.code16;1:;" /* prot -> real */
"xorw %%ax, %%ax;movw %%ax, %%ds;movw %%ax, %%es;movw %%ax, %%ss;"
/* do the BIOS call */
"movb $0x42, %%ah;"
"movw $0x500, %%si;"
"movb -1(%%si), %%dl;"
"clc;int $0x13;"
/* go back to long mode */
"movl %%cr0, %%eax;" /* enable CR0.PR */
"orb $1, %%al;"
"movl %%eax, %%cr0;"
/* AAAAAHHHHH... we have to workaround a compiler bug... This is a dirty hack that takes advantage of the
* fact that machine code 0xEA is the same in prot and real encoding just with different operand sizes.
* smuggle the real mode second operand in prot mode first operand */
".code32;ljmp $16,$1f+0x100000;1:;"/* real -> prot */
"movl %%cr0, %%eax;" /* enable CR0.PG */
"btsl $31, %%eax;"
"movl %%eax, %%cr0;"
"ljmp $32,$1f;.code64;1:;" /* prot -> long */
"movw $40, %%ax;movw %%ax, %%ds;movw %%ax, %%es;movw %%ax, %%ss;"
/* copy data from BIOS buffer to final position */
"xorq %%rcx, %%rcx;"
"xorq %%rsi, %%rsi;"
"movb $64, %%cl;"
"movw $0x600, %%si;"
"popq %%rdi;"
"cmpq %%rsi, %%rdi;je 3f;"
"repnz movsq;3:"
::"b"(lba),"D"(dst):"rax","rcx","rdx","rsi","memory");
}
/**
* Set up framebuffer with VESA VBE 2.0 BIOS
*/
void bios_vbe(uint32_t width, uint32_t height, uint32_t bpp)
{
if(ST || width < 320 || height < 200 || bpp < 15) return;
memset((void*)0x580, 0, 128);
edid_ptr = (uint8_t*)0x580; edid_size = 128;
__asm__ __volatile__(
/* go BIOS mode */
"shlq $32, %%rcx;"
"shlq $16, %%rbx;"
"addq %%rbx, %%rcx;"
"addq %%rax, %%rcx;"
"xorq %%rax, %%rax;pushq %%rax;"
"pushq %%rdi;"
"pushq %%rcx;"
"movq $16, %%rax;push %%rax;"
"movq $1f, %%rax;push %%rax;"
"lretq;.code32;1:;"
"movl %%cr0, %%eax;"
"btcl $31, %%eax;"
"movl %%eax, %%cr0;"
"ljmp $8,$1f;1:;"
"movl %%cr0, %%eax;"
"andb $0xfe, %%al;"
"movl %%eax, %%cr0;"
"ljmp $0,$1f;.code16;1:;"
"xorw %%ax, %%ax;movw %%ax, %%ds;movw %%ax, %%es;movw %%ax, %%ss;"
/* get EDID information */
"movw $0x4f15, %%ax;"
"movw $0x0001, %%bx;"
"xorw %%cx, %%cx;"
"xorw %%dx, %%dx;"
"movw $0x580, %%di;"
"int $0x10;"
/* do the VBE BIOS call */
"movw $0x600, %%di;"
"movl $0x32454256, 0(%%di);"
"movw $0x4f00, %%ax;"
"pushw %%ds;pushw %%es;"
"int $0x10;"
"popw %%es;popw %%ds;"
"cmpw $0x004f, %%ax;jne 2f;"
/* copy modes list out to workaround buggy VBE BIOSes */
"xorl %%esi, %%esi;"
"xorl %%edi, %%edi;"
"movw (0x60E), %%si;"
"movw (0x610), %%ax;"
"movw %%ax, %%ds;"
"movw $0xA00, %%di;"
"movw $256, %%cx;"
"1: lodsw;"
"cmpw $0xffff, %%ax;je 1f;"
"cmpw $0, %%ax;je 1f;"
"stosw;"
"dec %%cx;jnz 1b;"
"1:;xorw %%ax, %%ax;stosw;"
"movw %%ax, %%ds;"
/* iterate on modes and query info on each */
"movw $0xffff, 20(%%esp);" /* stack: */
"movw $0xA00, %%si;" /* 20(%esp) - best match mode */
"1:movw $0, (0x600);" /* 18(%esp) - best match height */
"lodsw;" /* 16(%esp) - best match width */
"orw %%ax, %%ax;jz 1f;" /* 8(%esp) - vidmode buffer pointer */
"movw %%ax, %%cx;" /* 4(%esp) - requested bpp */
"movw $0x600, %%di;" /* 2(%esp) - requested height */
"movw $0x4f01, %%ax;" /* 0(%esp) - requested width */
"pushw %%cx;pushw %%si;pushw %%ds;pushw %%es;"
"int $0x10;"
"popw %%es;popw %%ds;popw %%si;popw %%cx;"
"cmpw $0x004f, %%ax;jne 1b;" /* mode listed, but not supported */
"movw (0x600), %%ax;"
"andw $0x9b, %%ax;"
"cmpw $0x9b, %%ax;jne 1b;" /* not the mode we're looking for. Not packed pixel linear framebuffer */
"movb 4(%%esp), %%al;"
"cmpb (0x619), %%al;jne 1b;" /* bpp matches? */
"movw (0x614), %%ax;"
"cmpw 2(%%esp), %%ax;ja 1b;" /* height smaller or equal than required? */
"cmpw 18(%%esp), %%ax;jb 1b;" /* and bigger than the last best result? */
"movw (0x612), %%ax;"
"cmpw 0(%%esp), %%ax;ja 1b;" /* width smaller or equal than required? */
"cmpw 16(%%esp), %%ax;jb 1b;" /* and bigger than the last best result? */
/* this is the best result we have so far, store it */
"movw %%ax, 16(%%esp);"
"movw (0x614), %%ax;"
"movw %%ax, 18(%%esp);"
"movw %%cx, 20(%%esp);"
"jmp 1b;"
"1:cmpw $0xffff, 20(%%esp);je 2f;"
/* set up mode */
"movw 20(%%esp), %%bx;"
"orw $0x6000, %%bx;"
"movw $0x4f02, %%ax;"
"pushw %%cx;pushw %%si;pushw %%ds;pushw %%es;"
"int $0x10;"
"popw %%es;popw %%ds;popw %%si;popw %%cx;"
"cmpw $0x004f, %%ax;jne 2f;" /* not worked, so don't store it */
/* looks good, store vidmode */
"movw 20(%%esp), %%cx;"
"movw $0x600, %%di;"
"movw $0x4f01, %%ax;"
"pushw %%cx;pushw %%si;pushw %%ds;pushw %%es;"
"int $0x10;"
"popw %%es;popw %%ds;popw %%si;popw %%cx;"
"cmpw $0x004f, %%ax;jne 2f;" /* should never happen */
"movl 8(%%esp), %%edi;"
"movl (0x628), %%eax;"
"movl %%eax, 8(%%edi);" /* vidmode.framebuffer_addr lo */
"movl (0x62c), %%eax;"
"movl %%eax, 12(%%edi);" /* vidmode.framebuffer_addr hi */
"movw (0x610), %%ax;"
"movw %%ax, 16(%%edi);" /* vidmode.framebuffer_pitch */
"movw (0x612), %%ax;"
"movw %%ax, 20(%%edi);" /* vidmode.framebuffer_width */
"movw (0x614), %%ax;"
"movw %%ax, 24(%%edi);" /* vidmode.framebuffer_height */
"movb (0x619), %%al;"
"movb %%al, 28(%%edi);" /* vidmode.framebuffer_bpp */
"movw (0x61F), %%ax;xchgb %%al, %%ah;"
"movw %%ax, 32(%%edi);" /* vidmode.framebuffer_red_field_position + mask */
"movw (0x621), %%ax;xchgb %%al, %%ah;"
"movw %%ax, 34(%%edi);" /* vidmode.framebuffer_green_field_position + mask */
"movw (0x623), %%ax;xchgb %%al, %%ah;"
"movw %%ax, 36(%%edi);" /* vidmode.framebuffer_blue_field_position + mask */
"2:;"
/* go back to long mode */
"movl %%cr0, %%eax;"
"orb $1, %%al;"
"movl %%eax, %%cr0;"
".code32;ljmp $16,$1f+0x100000;1:;"
"movl %%cr0, %%eax;"
"btsl $31, %%eax;"
"movl %%eax, %%cr0;"
"ljmp $32,$1f;.code64;1:;"
"movw $40, %%ax;movw %%ax, %%ds;movw %%ax, %%es;movw %%ax, %%ss;"
"addq $24, %%rsp;"
::"a"(width),"b"(height),"c"(bpp),"D"(&vidmode):"rsi","rdx","memory");
if(!vidmode.framebuffer_addr) printf(" VESA: %s\n", "no framebuffer");
#ifdef CONSOLE_FB
else fb_x = fb_y = 4;
#endif
}
/**
* Get E820 memory map
*/
int bios_e820(multiboot_mmap_entry_t *dst)
{
int ret = 0;
if(ST || !dst) return 0;
__asm__ __volatile__(
/* go BIOS mode */
"pushq %%rdi;xorq %%rdi, %%rdi;pushq $0;"
"movq $16, %%rax;push %%rax;"
"movq $1f, %%rax;push %%rax;"
"lretq;.code32;1:;"
"movl %%cr0, %%eax;"
"btcl $31, %%eax;"
"movl %%eax, %%cr0;"
"ljmp $8,$1f;1:;"
"movl %%cr0, %%eax;"
"andb $0xfe, %%al;"
"movl %%eax, %%cr0;"
"ljmp $0,$1f;.code16;1:;"
"xorw %%ax, %%ax;movw %%ax, %%ds;movw %%ax, %%es;movw %%ax, %%ss;"
/* do the BIOS call */
"clc;xorl %%ebx, %%ebx;movl $0x600, %%edi;"
"1:movl $0xE820, %%eax;"
"movl $0x534d4150, %%edx;"
"xorl %%ecx, %%ecx;"
"movb $20, %%cl;"
"int $0x15;jc 1f;"
"addl $20, %%edi;xorl %%eax, %%eax;stosl;"
"incl 0(%%esp);"
"or %%ebx, %%ebx;jnz 1b;"
/* go back to long mode */
"1:movl %%cr0, %%eax;"
"orb $1, %%al;"
"movl %%eax, %%cr0;"
".code32;ljmp $16,$1f+0x100000;1:;"
"movl %%cr0, %%eax;"
"btsl $31, %%eax;"
"movl %%eax, %%cr0;"
"ljmp $32,$1f;.code64;1:;"
"movw $40, %%ax;movw %%ax, %%ds;movw %%ax, %%es;movw %%ax, %%ss;"
"movq %%rdi, %%rcx;movq $0x600, %%rsi;subq %%rsi, %%rcx;"
"popq %%rbx;popq %%rdi;repnz movsb;movq %%rbx, %%rax;"
:"=a"(ret):"D"(dst):"rbx","rcx","rdx","rsi","memory");
/* make sure of it that the memory map is sorted. Should be, so bubble-sort is affordable here */
sort_map(dst, ret, 1);
if(ret < 1) printf(" E820: %s\n", "unable to get memory map");
return ret;
}
/**
* Sleep 1 usec
*/
void bios_sleep(void)
{
/* NOTE: this should be the right solution, as it only relies on PS/2 alone and needs nothing else. However qemu's
* PS/2 emulation is buggy, the oscillation is host freq dependent and not constant as it is on a real hardware. */
#if 0
/* wait a bit, PS/2 control port's bit 4 is oscillating at 15 usecs, use that (67 * 15 = 1005 usecs) */
__asm__ __volatile__ ("inb $0x61,%%al;andb $0x10,%%al;movb %%al,%%ah;movw $67, %%cx;"
"1:;inb $0x61,%%al;andb $0x10,%%al;cmpb %%al,%%ah;je 1b;movb %%al,%%ah;nop;pause;dec %%cx;jnz 1b;" : : : "rax", "rbx", "rcx", "rdx");
#else
/* wait a bit, polling the PIT latch for delay. 1000 usec = 1,193,182 Hz / 1000 ms = 1193 */
__asm__ __volatile__ ("movw $1193, %%cx;" /* cx = ticks to wait */
"xor %%al,%%al;outb %%al,$0x43;inb $0x40,%%al;movb %%al,%%bl;inb $0x40,%%al;movb %%al,%%bh;" /* bx = last counter */
"1:xor %%al,%%al;outb %%al,$0x43;inb $0x40,%%al;movb %%al,%%ah;inb $0x40,%%al;xchgb %%al,%%ah;" /* ax = current counter */
"nop;pause;subw %%ax,%%bx;subw %%bx,%%cx;movw %%ax,%%bx;jae 1b;" /* cx -= (bx - ax); bx = ax; while(cx > 0); */
: : : "rax", "rbx", "rcx");
#endif
}
/**
* Allocate and zero out a page on BIOS
*/
uint64_t bios_alloc(uint32_t num)
{
uint64_t page = file_buf;
file_buf += num * 4096;
memset((void*)page, 0, num * 4096);
return page;
}
/**
* Get the next cluster from FAT
*/
uint32_t bios_nextclu(uint32_t clu)
{
uint64_t i;
if(ST || clu < 2 || clu >= 0x0FFFFFF8) return 0;
if(clu < fat_cache || clu > fat_cache + 1023) {
fat_cache = clu & ~1023;
for(i = 0; i < 8; i++) bios_loadsec(fat_lba + (fat_cache >> 7) + i, &fat[i << 7]);
}
clu = fat[clu - fat_cache];
return clu < 2 || clu >= 0x0FFFFFF8 ? 0 : clu;
}
/**
* Open a file on BIOS
*/
int bios_open(uint16_t *fn)
{
uint64_t lba;
uint32_t clu = bpb->rc, c, l;
int i, n = 0, m = 0, rd = 0;
uint8_t secleft = 0, *dir = data + sizeof(data), *plgs = plgids, *p;
uint16_t *u, *s = fn, a, b;
if(ST || !root_dir || !fn || !*fn) return 0;
file_size = file_clu = 0;
memset(lfn, 0, sizeof(lfn));
while(1) {
/* have we reached the end of the sector? */
if(dir >= data + sizeof(data)) {
if(secleft) { secleft--; lba++; }
else {
if(clu < 2 || clu >= 0x0FFFFFF8) return 0;
secleft = bpb->spc - 1;
lba = clu * bpb->spc + data_lba;
clu = bios_nextclu(clu);
}
bios_loadsec(lba, &data);
dir = data;
}
/* empty space? End of directory then */
if(!dir[0]) return 0;
/* not a deleted entry or current and parent entries? */
if(dir[0] != 5 && dir[0] != 0xE5 && (dir[0] != '.' || (dir[1] != '.' && dir[1] != ' '))) {
/* is this an LFN block? */
if(dir[0xB] == 0xF) {
/* first LFN block? */
if(!n || (dir[0] & 0x40)) {
memset(lfn, 0, sizeof(lfn));
n = dir[0] & 0x1F;
/* bad record, not sure what to do. Let's reset state and continue with next entry */
if(n < 1 || n > 20) { n = m = 0; dir += 32; continue; }
u = lfn + (n - 1) * 13;
}
/* get the next part of UCS-2 characters */
for(i = 0; i < 5; i++)
u[i] = dir[i*2+2] << 8 | dir[i*2+1];
for(i = 0; i < 6; i++)
u[i+5] = dir[i*2+0xF] << 8 | dir[i*2+0xE];
u[11] = dir[0x1D] << 8 | dir[0x1C];
u[12] = dir[0x1F] << 8 | dir[0x1E];
u -= 13;
n--;
/* indicate that the next directory entry belongs to an LFN */
m = (!n && u < lfn);
} else
if(!(dir[0xB] & 8)) {
/* if we don't have an LFN already, generate it from the 8.3 name in this entry */
if(!m) {
for(i = 0; i < 8; i++) lfn[i] = dir[i];
while(i && lfn[i - 1] == ' ') i--;
if(dir[8] != ' ') {
lfn[i++] = '.'; lfn[i++] = dir[8];
if(dir[9] != ' ') {
lfn[i++] = dir[9];
if(dir[10] != ' ') { lfn[i++] = dir[10]; }
}
}
lfn[i] = 0;
} else m = 0;
if(rddir && rd) {
/* read directory entries */
c = (dir[0x15] << 24) | (dir[0x14] << 16) | (dir[0x1B] << 8) | dir[0x1A];
if(c >= 2 && c < 0x0FFFFFF8 && plgs + 512 < plgids + 0x40000) {
bios_loadsec(c * bpb->spc + data_lba, plgs);
if(!memcmp(plgs, PLG_MAGIC, 4) && (((plg_hdr_t*)plgs)->nid || ((plg_hdr_t*)plgs)->type == PLG_T_TAG) &&
((plg_hdr_t*)plgs)->arch == EM_X86_64) {
p = plgs; l = ((plg_hdr_t*)plgs)->nid * sizeof(plg_id_t);
plgs[10] = ((plg_hdr_t*)plgs)->type;
plgs[11] = ((plg_hdr_t*)plgs)->nid;
memcpy(plgs + 12, plgs + sizeof(plg_hdr_t), l);
plgs = ucs2(plgs + 12 + l, lfn);
*((uint16_t*)p) = plgs - p; *((uint64_t*)(p + 2)) = 0;
}
plgs[0] = plgs[1] = 0;
}
} else {
/* filename match? */
if(*s == '/') s++;
for(i = 0; lfn[i] && s[i] && s[i] != '/'; i++) {
a = lfn[i]; if(a >= 'A' && a <= 'Z') a += 'a' - 'A';
b = s[i]; if(b >= 'A' && b <= 'Z') b += 'a' - 'A';
if(a != b) break;
}
if(!lfn[i]) {
clu = (dir[0x15] << 24) | (dir[0x14] << 16) | (dir[0x1B] << 8) | dir[0x1A];
if(clu < 2 || clu >= 0x0FFFFFF8) return 0;
/* is this a directory? */
if(dir[0xB] & 0x10) {
if(s[i] != '/') return 0;
/* go to subdirectory */
s += i + 1; n = m = secleft = 0; dir = data + sizeof(data);
if(!*s) { rd = 1; }
continue;
} else {
/* no, it's a file, then we have located what we were looking for */
if(s[i]) return 0;
file_clu = clu;
file_size = (dir[0x1F] << 24) | (dir[0x1E] << 16) | (dir[0x1D] << 8) | dir[0x1C];
break;
}
}
}
}
}
dir += 32;
}
return 1;
}
/**
* Read data from file on BIOS
*/
uint64_t bios_read(uint64_t offs, uint64_t size, void *buf)
{
uint64_t lba = 0, rem, o;
uint32_t clu = file_clu, nc, ns = 0, os = 0, rs = 512;
uint8_t secleft = 0;
if(ST || file_clu < 2 || offs >= file_size || !size || !buf) return 0;
if(offs + size > file_size) size = file_size - offs;
rem = size;
pb_init(size);