-
Notifications
You must be signed in to change notification settings - Fork 6
/
kernel.asm
126 lines (94 loc) · 3.59 KB
/
kernel.asm
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
; Copyright 2018-2019 Brian Otto @ https://hackerpulp.com
;
; Permission to use, copy, modify, and/or distribute this software for any
; purpose with or without fee is hereby granted, provided that the above
; copyright notice and this permission notice appear in all copies.
;
; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
; REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
; AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
; INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
; LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
; OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
; PERFORMANCE OF THIS SOFTWARE.
; generate 64-bit code
bits 64
; use relative addresses
default rel
; contains the code that will run
section .text
; allows the linker to see the entry symbol
global start
%include "kernel-functions.asm"
%include "kernel-efi.asm"
%include "kernel-api.asm"
start:
; save the location of UEFI
mov [ptrUEFI], rsp
; reserve space for 4 arguments
sub rsp, 4 * 8
; see http://www.uefi.org/sites/default/files/resources/UEFI Spec 2_7_A Sept 6.pdf#G6.1000024
; the "2.3.4.1 Handoff State" section
; rcx is the 1st argument passed to us by the UEFI firmware
; it will contain the EFI_HANDLE
mov [hndImageHandle], rcx
; rdx is the 2nd argument passed to us by the UEFI firmware
; it points to the EFI_SYSTEM_TABLE
mov [ptrSystemTable], rdx
; verify the EFI_TABLE_HEADER.Signature
call apiVerifySignature
; output the OS and Firmware versions
call apiOutputHeader
; locate a graphics device and allocate a frame buffer
call apiGetFrameBuffer
; get a memory map and exit UEFI boot services
call apiExitUEFI
; load our kernel
call apiLoadKernel
add rsp, 4 * 8
mov rax, EFI_SUCCESS
ret
error:
; move to the location of UEFI and return
mov rsp, [ptrUEFI]
ret
errorCode:
; save our error code
push rax
; display the message
mov rcx, strErrorCode
call efiOutputString
; grab our error code and write it
; see the UEFI definitions in kernel-efi.asm
mov rcx, [rsp]
call funIntegerToAscii
; restore the error code
pop rax
jmp error
codesize equ $ - $$
; contains nothing - but it is required by UEFI
section .reloc
; contains the data being stored
section .data
; UEFI requires we use Unicode strings
strHeader db __utf16__ `Hacker Pulp OS v0.1\r\nRunning on \0`
strHeaderV db __utf16__ ` v\0`
strErrorCode db __utf16__ `\r\n\nError Code #\0`
strDebugText db __utf16__ `\r\n\nDebug: \0`
; stores the location of UEFI
ptrUEFI dq 0
; stores the EFI_HANDLE
hndImageHandle dq 0
; stores the EFI_SYSTEM_TABLE
ptrSystemTable dq 0
; stores the EFI_GRAPHICS_OUTPUT_PROTOCOL
ptrInterface dq 0
; stores the memory map data
intMemoryMapSize dq EFI_MEMORY_DESCRIPTOR_size * 1024
bufMemoryMap resb EFI_MEMORY_DESCRIPTOR_size * 1024
ptrMemoryMapKey dq 0
ptrMemoryMapDescSize dq 0
ptrMemoryMapDescVersion dq 0
; stores the frame buffer base
ptrFrameBuffer dq 0
datasize equ $ - $$