* remove "\r" nonsense
[mascara-docs.git] / amd64 / bareMetalOS-0.5.2 / baremetal0.5.2 / os / cli.asm
blob9070498433b26794d1690d98c1b7fdd987ec5245
1 ; =============================================================================
2 ; BareMetal -- a 64-bit OS written in Assembly for x86-64 systems
3 ; Copyright (C) 2008-2011 Return Infinity -- see LICENSE.TXT
5 ; COMMAND LINE INTERFACE
6 ; =============================================================================
8 align 16
9 db 'DEBUG: CLI '
10 align 16
13 os_command_line:
14 mov rsi, prompt ; Prompt for input
15 mov bl, 0x09 ; Black background, Light Red text
16 call os_print_string_with_color
18 mov rdi, cli_temp_string
19 mov rcx, 250 ; Limit the input to 250 characters
20 call os_input_string
21 call os_print_newline ; The user hit enter so print a new line
22 jrcxz os_command_line ; os_input_string stores the number of charaters received in RCX
24 mov rsi, rdi
25 call os_string_parse ; Remove extra spaces
26 jrcxz os_command_line ; os_string_parse stores the number of words in RCX
27 mov byte [cli_args], cl ; Store the number of words in the string
29 ; Copy the first word in the string to a new string. This is the command/application to run
30 xor rcx, rcx
31 mov rsi, cli_temp_string
32 mov rdi, cli_command_string
33 push rdi ; Push the command string
34 nextbyte:
35 add rcx, 1
36 lodsb
37 cmp al, ' ' ; End of the word
38 je endofcommand
39 cmp al, 0x00 ; End of the string
40 je endofcommand
41 cmp rcx, 13 ; More than 12 bytes
42 je endofcommand
43 stosb
44 jmp nextbyte
45 endofcommand:
46 mov al, 0x00
47 stosb ; Terminate the string
49 ; At this point cli_command_string holds at least "a" and at most "abcdefgh.ijk"
51 ; Break the contents of cli_temp_string into individual strings
52 mov rsi, cli_temp_string
53 mov al, 0x20
54 mov bl, 0x00
55 call os_string_change_char
57 pop rsi ; Pop the command string
58 call os_string_uppercase ; Convert to uppercase for comparison
60 mov rdi, cls_string ; 'CLS' entered?
61 call os_string_compare
62 jc near clear_screen
64 mov rdi, dir_string ; 'DIR' entered?
65 call os_string_compare
66 jc near dir
68 mov rdi, ver_string ; 'VER' entered?
69 call os_string_compare
70 jc near print_ver
72 mov rdi, date_string ; 'DATE' entered?
73 call os_string_compare
74 jc near date
76 mov rdi, exit_string ; 'EXIT' entered?
77 call os_string_compare
78 jc near exit
80 mov rdi, help_string ; 'HELP' entered?
81 call os_string_compare
82 jc near print_help
84 mov rdi, node_string ; 'NODE' entered?
85 call os_string_compare
86 jc near node
88 mov rdi, time_string ; 'TIME' entered?
89 call os_string_compare
90 jc near time
92 mov rdi, debug_string ; 'DEBUG' entered?
93 call os_string_compare
94 jc near debug
96 mov rdi, reboot_string ; 'REBOOT' entered?
97 call os_string_compare
98 jc near reboot
100 mov rdi, testzone_string ; 'TESTZONE' entered?
101 call os_string_compare
102 jc near testzone
104 ; At this point it is not one of the built-in CLI functions. Prepare to check the filesystem.
105 mov al, '.'
106 call os_string_find_char ; Check for a '.' in the string
107 cmp rax, 0
108 jne full_name ; If there was a '.' then a suffix is present
110 ; No suffix was present so we add the default application suffix of ".APP"
111 add_suffix:
112 call os_string_length
113 cmp rcx, 8
114 jg fail ; If the string is longer than 8 chars we can't add a suffix
116 mov rdi, cli_command_string
117 mov rsi, appextension ; '.APP'
118 call os_string_append ; Append the extension to the command string
120 ; cli_command_string now contains a full filename
121 full_name:
122 mov rsi, cli_command_string
123 mov rdi, programlocation ; We load the program to this location in memory (currently 0x00100000 : at the 2MB mark)
124 call os_file_read ; Read the file into memory
125 jc fail ; If carry is set then the file was not found
127 mov rax, programlocation ; 0x00100000 : at the 2MB mark
128 xor rbx, rbx ; No arguements required (The app can get them with os_get_argc and os_get_argv)
129 call os_smp_enqueue ; Queue the application to run on the next available core
130 jmp exit ; The CLI can quit now. IRQ 8 will restart it when the program is finished
132 fail: ; We didn't get a valid command or program name
133 mov rsi, not_found_msg
134 call os_print_string
135 jmp os_command_line
137 print_help:
138 mov rsi, help_text
139 call os_print_string
140 jmp os_command_line
142 clear_screen:
143 call os_screen_clear
144 mov ax, 0x0018
145 call os_move_cursor
146 jmp os_command_line
148 print_ver:
149 mov rsi, version_msg
150 call os_print_string
151 jmp os_command_line
153 dir:
154 mov rdi, cli_temp_string
155 mov rsi, rdi
156 call os_file_get_list
157 call os_print_string
158 jmp os_command_line
160 date:
161 mov rdi, cli_temp_string
162 mov rsi, rdi
163 call os_get_date_string
164 call os_print_string
165 call os_print_newline
166 jmp os_command_line
168 time:
169 mov rdi, cli_temp_string
170 mov rsi, rdi
171 call os_get_time_string
172 call os_print_string
173 call os_print_newline
174 jmp os_command_line
176 node:
177 jmp os_command_line ; Nothing here yet...
179 align 16
180 testzone:
181 xchg bx, bx ; Bochs Magic Breakpoint
183 ; call os_ethernet_avail
184 ; call os_debug_dump_rax
186 ; mov rdi, cli_temp_string
187 ; mov rsi, rdi
188 ; mov rcx, 12
189 ; call os_input_string
190 ; call os_file_get_size
191 ; mov rax, rcx
192 ; call os_print_newline
193 ; call os_debug_dump_rax
195 ; cli
196 ; xor eax, eax ; Out-of-order execution can cause RDTSC to be executed later than expected
197 ; cpuid ; Execute a serializing instruction to force every preceding instruction to complete before allowing the program to continue
198 ; rdtsc
199 ; mov r15d, eax
200 ; Benchmark code start
202 ; sub rsp, 0x28
203 ; mov [rsp + 0x20], rax
204 ; mov [rsp + 0x18], rax
205 ; mov [rsp + 0x10], rax
206 ; mov [rsp + 0x8], rax
207 ; mov [rsp], rax
208 ; mov rax, [rsp]
209 ; mov rax, [rsp + 0x8]
210 ; mov rax, [rsp + 0x10]
211 ; mov rax, [rsp + 0x18]
212 ; mov rax, [rsp + 0x20]
213 ; add rsp, 0x28
215 ; push rax
216 ; push rax
217 ; push rax
218 ; push rax
219 ; push rax
220 ; pop rax
221 ; pop rax
222 ; pop rax
223 ; pop rax
224 ; pop rax
226 ; Benchmark code finish
227 ; xor eax, eax ; Out-of-order execution can cause RDTSC to be executed later than expected
228 ; cpuid ; Execute a serializing instruction to force every preceding instruction to complete before allowing the program to continue
229 ; rdtsc
230 ; sti
231 ; sub eax, r15d
232 ; call os_debug_dump_eax
233 ; call os_print_newline
235 mov bl, 0x0F
236 mov al, '0'
237 call os_print_char_with_color
238 mov al, '0'
239 call os_print_char_with_color
240 mov bl, 0x10
241 mov al, '0'
242 call os_print_char_with_color
243 mov al, '1'
244 call os_print_char_with_color
245 mov bl, 0x20
246 mov al, '0'
247 call os_print_char_with_color
248 mov al, '2'
249 call os_print_char_with_color
250 mov bl, 0x30
251 mov al, '0'
252 call os_print_char_with_color
253 mov al, '3'
254 call os_print_char_with_color
255 mov bl, 0x40
256 mov al, '0'
257 call os_print_char_with_color
258 mov al, '4'
259 call os_print_char_with_color
260 mov bl, 0x50
261 mov al, '0'
262 call os_print_char_with_color
263 mov al, '5'
264 call os_print_char_with_color
265 mov bl, 0x60
266 mov al, '0'
267 call os_print_char_with_color
268 mov al, '6'
269 call os_print_char_with_color
270 mov bl, 0x70
271 mov al, '0'
272 call os_print_char_with_color
273 mov al, '7'
274 call os_print_char_with_color
275 call os_print_newline
276 mov bl, 0x80
277 mov al, '0'
278 call os_print_char_with_color
279 mov al, '8'
280 call os_print_char_with_color
281 mov bl, 0x90
282 mov al, '0'
283 call os_print_char_with_color
284 mov al, '9'
285 call os_print_char_with_color
286 mov bl, 0xA0
287 mov al, '0'
288 call os_print_char_with_color
289 mov al, 'A'
290 call os_print_char_with_color
291 mov bl, 0xB0
292 mov al, '0'
293 call os_print_char_with_color
294 mov al, 'B'
295 call os_print_char_with_color
296 mov bl, 0xC0
297 mov al, '0'
298 call os_print_char_with_color
299 mov al, 'C'
300 call os_print_char_with_color
301 mov bl, 0xD0
302 mov al, '0'
303 call os_print_char_with_color
304 mov al, 'D'
305 call os_print_char_with_color
306 mov bl, 0xE0
307 mov al, '0'
308 call os_print_char_with_color
309 mov al, 'E'
310 call os_print_char_with_color
311 mov bl, 0xF0
312 mov al, '0'
313 call os_print_char_with_color
314 mov al, 'F'
315 call os_print_char_with_color
316 call os_print_newline
317 ; mov al, ' '
318 ; call os_print_char
319 ; mov rax, rcx
320 ; call os_debug_dump_rax
322 ; call os_mem_get_free
323 ; mov rax, rcx
324 ; call os_debug_dump_rax
325 ; mov al, ' '
326 ; call os_print_char
327 ; xor eax, eax
328 ; mov ax, word [os_MemAmount]
329 ; shr ax, 1 ; Divide actual memory by 2 (RAX now holds total pages)
330 ; call os_debug_dump_rax
331 ; call os_print_newline
332 ; mov rcx, 2 ; 2 pages = 4 MiB
333 ; call os_mem_allocate
334 ; call os_debug_dump_rax
335 ; call os_print_newline
337 ; mov rax, 0x400000
338 ; call os_get_time_data
341 ; ud2
343 ; xor rax, rax
344 ; xor rbx, rcx
345 ; xor rcx, rcx
346 ; xor rdx, rdx
347 ; div rax
349 jmp os_command_line
351 reboot:
352 in al, 0x64
353 test al, 00000010b ; Wait for an empty Input Buffer
354 jne reboot
355 mov al, 0xFE
356 out 0x64, al ; Send the reboot call to the keyboard controller
357 jmp reboot
359 debug:
360 call os_get_argc ; Check the argument number
361 cmp al, 1
362 je debug_dump_reg ; If it is only one then do a register dump
363 mov rcx, 16
364 cmp al, 3 ; Did we get at least 3?
365 jl noamount ; If not no amount was specified
366 mov al, 2
367 call os_get_argv ; Get the amount of bytes to display
368 call os_string_to_int ; Convert to an integer
369 mov rcx, rax
370 noamount:
371 mov al, 1
372 call os_get_argv ; Get the starting memory address
373 call os_hex_string_to_int
374 mov rsi, rax
375 debug_default:
376 call os_debug_dump_mem
377 call os_print_newline
379 jmp os_command_line
381 debug_dump_reg:
382 call os_debug_dump_reg
383 jmp os_command_line
385 exit:
388 ; Strings
389 help_text db 'Built-in commands: CLS, DATE, DEBUG, DIR, HELP, REBOOT, TIME, VER', 13, 0
390 not_found_msg db 'Command or program not found', 13, 0
391 version_msg db 'BareMetal OS ', BAREMETALOS_VER, 13, 0
393 cls_string db 'CLS', 0
394 dir_string db 'DIR', 0
395 ver_string db 'VER', 0
396 date_string db 'DATE', 0
397 exit_string db 'EXIT', 0
398 help_string db 'HELP', 0
399 node_string db 'NODE', 0
400 time_string db 'TIME', 0
401 debug_string db 'DEBUG', 0
402 reboot_string db 'REBOOT', 0
403 testzone_string db 'TESTZONE', 0
406 ; =============================================================================
407 ; EOF