snapshot before branching
[rrq/jonasforth.git] / init / uefi.f
1 \ ##############################
2 \ Handle hexadecimal EFI GUID into a 2-word record on the heap
3 \ The GUID text format consists is as a series of hexadecomal codes that
4 \ maps to byte codes as follows
5 \ 4 byte LE hexcode (8 digits)
6 \ 2 x 2 byte LE hexcode (4 digits)
7 \ 8 x 1 byte hexcode (2 digits)
8 \ and with braces
9 \ The following is a code example:
10 \ ----
11 \ GUID: GOP.GUID
12 \ {0x9042a9de,0x23dc,0x4a38, {0x96,0xfb,0x7a,0xde,0xd0,0x80,0x51,0x6a}}
13 \ ----
14 \ Though, the parsing actually first grabs the word via CONSTANT, and then just\ scans for "x" as hex number prefix, thus reading like
15 \ GUID: {word}*x........*x....*x....*x..*x..*x..*x..*x..*x..*x..*x..{word}
16 \ where, as usual, "{word}" are anything up to reading the subsequent space.
17
18 \ Allocate a variable for the list of all GUIDS
19
20 HERE @ 0 , CONSTANT GUIDS
21
22 ( [name] -- ; declare a guid constant with name and guid as per the above, )
23 ( Followed by pointer to the previous guid )
24 : GUID:
25   HERE @
26   0 , 0 , GUIDS @ ,
27   DUP CONSTANT
28   DUP GUIDS !
29           120 SCANTO HEX8 OVER C4! 4 +
30   2 BEGIN SWAP 120 SCANTO HEX4 OVER C2! 2 + SWAP 1 - DUP 0 = UNTIL DROP
31   8 BEGIN SWAP 120 SCANTO HEX2 OVER C!  1 + SWAP 1 - DUP 0 = UNTIL DROP
32   DROP READ-WORD DROP DROP
33 ;
34
35 ( *guid -- *guid ; Get next GUID word )
36 : GUID.next 16 + @ ;
37
38 ( *guid -- *chars length ; Find the dicitonary name of the guid )
39 : GUID.name 33 + S@ ;
40
41 ( *guid1 *guid2 -- x ; Check if two GUID are equal )
42 : GUID= OVER @ OVER @ = IF 8 + @ SWAP 8 + @ = ELSE DROP DROP 0 THEN ;
43
44 ( *guidp -- ; Tell name of GUID )
45 : GUID.Tellname DUP IF GUID.name ELSE DROP S" unknown" THEN TELL ;
46
47 ( *guidp -- *guid ; Find dictionary guid same as the given )
48 : GUID.find
49   GUIDS
50   ( *guidp *list-guid )
51   BEGIN
52     DUP
53     NEWLINE DUP GUID.Tellname NEWLINE
54     IF 2DUP GUID= IF SWAP EXIT THEN GUID.next ELSE SWAP EXIT THEN
55   AGAIN
56 ;
57
58 ( *guid -- ; Print a GUID in parsable hex form )
59 : GUID.Tell
60   DUP GUID.find GUID.Tellname
61   S"  = {0x" TELL
62   DUP @ 4294967296 /MOD .U S" ,0x" TELL
63   65536 /MOD .U S" ,0x" TELL .U
64   8 + @ 8
65   BEGIN DUP 8 = IF S"  ,{0x" ELSE S" ,0x" THEN TELL
66         SWAP 256 /MOD .U SWAP
67         1 - DUP 0 =
68   UNTIL DROP DROP
69   S" }}" TELL
70 ;
71             
72 \ ##############################
73 \ System table entry labelling
74
75 SystemTable
76 \ EFI_TABLE_HEADER
77 8 FIELD* SystemTable.Signature
78 4 FIELD* SystemTable.Revision
79 4 FIELD* SystemTable.HeaderSize
80 4 FIELD* SystemTable.CRC32
81 4 FIELD* SystemTable.Reserved
82 ( CHAR16 )                          FIELD@ FirmwareVendor
83 4                                   FIELD* FirmwareRevision
84 4                                   FIELD* FirmwareRevisionPad
85 \ -- stdio
86 ( EFI_HANDLE )                      FIELD@ ConsoleInHandle
87 ( EFI_SIMPLE_TEXT_INPUT_PROTOCOL )  FIELD@ ConIn
88 ( EFI_HANDLE )                      FIELD@ ConsoleOutHandle
89 ( EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL ) FIELD@ ConOut
90 ( EFI_HANDLE )                      FIELD@ StandardErrorHandle
91 ( EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL ) FIELD@ StdErr
92 \ -- services
93 ( EFI_RUNTIME_SERVICES )            FIELD@ RuntimeServices
94 ( EFI_BOOT_SERVICES )               FIELD@ BootServices
95 \ -- ConfigurationTable
96 8 FIELD* SystemTable.NumberOfTableEntries \ UINTN = native size uint
97 ( EFI_CONFIGURATION_TABLE )         FIELD@ ConfigurationTable
98 DROP
99
100 \ ##############################
101 \ : ConOut entry labelling
102
103 ConOut
104 ( EFI_TEXT_RESET )               FIELD@ ConOut.Reset
105 ( EFI_TEXT_STRING )              FIELD@ ConOut.OutputString
106 ( EFI_TEXT_TEST_STRING )         FIELD@ ConOut.TestString
107 ( EFI_TEXT_QUERY_MODE )          FIELD@ ConOut.QueryMode
108 ( EFI_TEXT_SET_MODE )            FIELD@ ConOut.SetMode
109 ( EFI_TEXT_SET_ATTRIBUTE )       FIELD@ ConOut.SetAttribute
110 ( EFI_TEXT_CLEAR_SCREEN )        FIELD@ ConOut.ClearScreen
111 ( EFI_TEXT_SET_CURSOR_POSITION ) FIELD@ ConOut.SetCursorPosition
112 ( EFI_TEXT_ENABLE_CURSOR )       FIELD@ ConOut.EnableCursor
113 ( SIMPLE_TEXT_OUTPUT_MODE )      FIELD@ ConOut.Mode
114 DROP
115
116 : ConOut.OutputString() ConOut SWAP ConOut.OutputString EFICALL2 ;
117 : ConOut.ClearScreen() ConOut ConOut.ClearScreen EFICALL1 ;
118
119 \ ##############################
120 \ BootServices entry labelling
121
122 0 CONSTANT SearchKey.NONE
123
124 \ ENUM EFI_LOCATE_SEARCH_TYPE
125 0 CONSTANT SearchType.AllHandles
126 1 CONSTANT SearchType.ByRegisterNotify
127 2 CONSTANT SearchType.ByProtocol
128
129 BootServices
130 \ EFI_TABLE_HEADER
131 8 FIELD* BootServices.Signature
132 4 FIELD* BootServices.Revision
133 4 FIELD* BootServices.HeaderSize
134 4 FIELD* BootServices.CRC32
135 4 FIELD* BootServices.Reserved
136 \ Task Priority Services
137     FIELD@ RaiseTPL
138     FIELD@ RestoreTPL
139 \ Memory Services
140     FIELD@ AllocatePages
141     FIELD@ FreePages
142     FIELD@ GetMemoryMap
143     FIELD@ AllocatePool
144     FIELD@ FreePool
145 \ Event & Timer Services
146     FIELD@ CreateEvent
147     FIELD@ SetTimer
148     FIELD@ WaitForEvent
149     FIELD@ SignalEvent 
150     FIELD@ CloseEvent
151     FIELD@ CheckEvent
152 \ Protocol Handler Services
153     FIELD@ InstallProtocolInterface
154     FIELD@ ReinstallProtocolInterface
155     FIELD@ UninstallProtocolInterface
156     FIELD@ HandleProtocol
157     FIELD@ Reserved2
158     FIELD@ RegisterProtocolNotify
159     FIELD@ LocateHandle
160     FIELD@ LocateDevicePath
161     FIELD@ InstallConfigurationTable
162 \ Image Services
163     FIELD@ LoadImage
164     FIELD@ StartImage
165     FIELD@ Exit
166     FIELD@ UnloadImage
167     FIELD@ ExitBootServices
168 \ Miscellaneous Services
169     FIELD@ GetNextMonotonicCount
170     FIELD@ Stall
171     FIELD@ SetWatchdogTimer
172 \ DriverSupport Services
173     FIELD@ ConnectController
174     FIELD@ DisconnectController
175 \ Open and Close Protocol Services
176     FIELD@ OpenProtocol
177     FIELD@ CloseProtocol
178     FIELD@ OpenProtocolInformation
179 \ Library Services
180     FIELD@ ProtocolsPerHandle
181     FIELD@ LocateHandleBuffer
182     FIELD@ LocateProtocol
183     FIELD@ InstallMultipleProtocolInterfaces
184     FIELD@ UninstallMultipleProtocolInterfaces
185 \ 32-bit CRC Services
186     FIELD@ CalculateCrc32
187 \ Miscellaneous Services
188     FIELD@ CopyMem
189     FIELD@ SetMem
190     FIELD@ CreateEventEx
191 DROP
192
193 ( buffer -- )
194 : BootServices.FreePool()
195   FreePool EFICALL1 \ No return value?
196 ;
197
198 ( handle, *guid -- *interface ; Queries a handle to determine if it supports
199   a specified protocol, and returns interface pointer or null )
200 : BootServices.HandleProtocol()
201   HERE @ 0 OVER ! HandleProtocol EFICALL3
202   DUP IF S" **HandleProtocol: " TELL .U NEWLINE ELSE DROP THEN
203   HERE @ @
204 ;
205
206 ( Handle -- *buffer count ; Retrieves The List Of Protocol Interface
207   GUIDs that are installed on a handle in a buffer allocated from pool. )
208 : BootServices.ProtocolsPerHandle()
209   HERE @ 0 , HERE @ 0 , ProtocolsPerHandle EFICALL3
210   HERE @ 16 - @  HERE @ 8 - @  HERE @ 16 - HERE !
211   ROT DUP IF S" **ProtocolsPerHandle: " TELL .U NEWLINE ELSE DROP THEN
212 ;
213
214 ( *guid *Registration -- *Interface ;
215   Returns the first protocol interface that matches the given protocol. )
216 : BootServices.LocateProtocol()
217   HERE @ 0 OVER !
218   LocateProtocol EFICALL3
219   DUP IF S" **LocateProtocol: " TELL .U NEWLINE ELSE DROP THEN
220   HERE @ @
221 ;
222
223 ( *protocol type -- *array count ; locate handles )
224 ( returns array in allocated space )
225 : BootServices.LocateHandleBuffer()
226   ( searchtype, protocol, searchkey, *count, *arrayp )
227   SWAP SearchKey.NONE HERE @ HERE @ 8 +
228   ( type *protocol searchkey *count **array )
229   LocateHandleBuffer EFICALL5
230   DUP IF S" **LocateHandleBuffer: " TELL .U NEWLINE ELSE DROP THEN
231   ( HERE@ = nohandles , *buffer )
232   HERE @ 8 + @ HERE @ @
233 ;
234
235 ( -- buffer* n ; locate handles )
236 ( returns array in allocated space )
237 : BootServices.LocateHandleBuffer(AllHandles)
238   0 SearchType.AllHandles BootServices.LocateHandleBuffer()
239 ;
240
241 ( protocol -- buffer* n ; locate handles )
242 ( returns array in allocated space )
243 : BootServices.LocateHandleBuffer(ByProtocol)
244   SearchType.ByProtocol BootServices.LocateHandleBuffer()
245 ;
246
247 \ ##############################
248 \ GOP = GraphicsOutputProtocol
249
250 GUID: GOP.GUID
251 {0x9042a9de,0x23dc,0x4a38, {0x96,0xfb,0x7a,0xde,0xd0,0x80,0x51,0x6a}}
252
253 GOP.GUID 0 BootServices.LocateProtocol()
254 FIELD@ GOP.QueryMode
255 FIELD@ GOP.SetMode
256 FIELD@ GOP.Blt
257
258 ( guid buffer mode sx sy dx dy dw dh pitch -- )
259 : GOP.Blt()
260   GOP.Blt EFICALL10 0 =
261   IF ELSE S" Warning: Invalid Blt()" TELL THEN
262 ;
263
264 \ ##############################
265 \ DPP = EFI_DEVICE_PATH_PROTOCOL
266 GUID: DPP.GUID
267 {0x09576e91,0x6d3f,0x11d2, {0x8e,0x39,0x00,0xa0,0xc9,0x69,0x72,0x3b}}
268
269 ( *protocol -- ; Tell about DPP protocol )
270 : DPP.Tell
271   DUP @ 4294967295 &
272   256 /MOD S" DPP{"     TELL .
273   256 /MOD S" ," TELL .
274   DUP S" ,"  TELL .
275   S" }" TELL
276   SWAP 4 + SWAP
277   .BYTES
278 ;
279
280 \ ##############################
281 \ EFI_BLOCK_IO_MEDIA struct
282
283
284 \ ##############################
285 \ BIP = EFI_BLOCK_IO_PROTOCOL
286 GUID: BIP.GUID
287 0x964e5b21,0x6459,0x11d2, {0x8e,0x39,0x00,0xa0,0xc9,0x69,0x72,0x3b}}
288
289 \ ##############################
290 \ LIP = LoadedImageProtocol
291 GUID: LIP.GUID
292 {0x5b1b31a1,0x9562,0x11d2, {0x8e,0x3f,0x00,0xa0,0xc9,0x69,0x72,0x3b}}
293
294 \ ##############################
295 \ SFSP = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
296 GUID: SFSP.GUID
297 0x0964e5b22,0x6459,0x11d2, {0x8e,0x39,0x00,0xa0,0xc9,0x69,0x72,0x3b}}