| 1 | section .realmode |
| 2 | |
| 3 | int_08_ticks_counter: dd 0 |
| 4 | int_08_callback: dd 0 |
| 5 | |
| 6 | int_08_isr: |
| 7 | bits 16 |
| 8 | pushf |
| 9 | inc dword [cs:int_08_ticks_counter] |
| 10 | popf |
| 11 | jmp far [cs:int_08_callback] |
| 12 | bits 32 |
| 13 | |
| 14 | extern getchar_internal |
| 15 | |
| 16 | global _pit_sleep_and_quit_on_keypress |
| 17 | _pit_sleep_and_quit_on_keypress: |
| 18 | ; Hook int 0x08 |
| 19 | mov edx, dword [0x08*4] |
| 20 | mov dword [int_08_callback], edx |
| 21 | mov dword [0x08*4], int_08_isr |
| 22 | |
| 23 | ; pit_ticks in edx |
| 24 | mov edx, dword [esp+4] |
| 25 | |
| 26 | mov dword [int_08_ticks_counter], 0 |
| 27 | |
| 28 | ; Save GDT in case BIOS overwrites it |
| 29 | sgdt [.gdt] |
| 30 | |
| 31 | ; Save IDT |
| 32 | sidt [.idt] |
| 33 | |
| 34 | ; Load BIOS IVT |
| 35 | lidt [.rm_idt] |
| 36 | |
| 37 | ; Save non-scratch GPRs |
| 38 | push ebx |
| 39 | push esi |
| 40 | push edi |
| 41 | push ebp |
| 42 | |
| 43 | ; Jump to real mode |
| 44 | jmp 0x08:.bits16 |
| 45 | .bits16: |
| 46 | bits 16 |
| 47 | mov ax, 0x10 |
| 48 | mov ds, ax |
| 49 | mov es, ax |
| 50 | mov fs, ax |
| 51 | mov gs, ax |
| 52 | mov ss, ax |
| 53 | mov eax, cr0 |
| 54 | and al, 0xfe |
| 55 | mov cr0, eax |
| 56 | jmp 0x00:.cszero |
| 57 | .cszero: |
| 58 | xor ax, ax |
| 59 | mov ds, ax |
| 60 | mov es, ax |
| 61 | mov fs, ax |
| 62 | mov gs, ax |
| 63 | mov ss, ax |
| 64 | |
| 65 | sti |
| 66 | |
| 67 | mov byte [.mods], 0 |
| 68 | mov byte [.ascii], 0 |
| 69 | mov byte [.scan], 0 |
| 70 | |
| 71 | .loop: |
| 72 | cmp dword [int_08_ticks_counter], edx |
| 73 | je .done |
| 74 | |
| 75 | push ecx |
| 76 | push edx |
| 77 | mov ah, 0x01 |
| 78 | xor al, al |
| 79 | int 0x16 |
| 80 | pop edx |
| 81 | pop ecx |
| 82 | |
| 83 | jz .loop |
| 84 | |
| 85 | ; on keypress |
| 86 | xor ax, ax |
| 87 | int 0x16 |
| 88 | mov byte [.ascii], al |
| 89 | mov byte [.scan], ah |
| 90 | |
| 91 | mov ax, 0x0200 |
| 92 | int 0x16 |
| 93 | test al, 0x04 |
| 94 | jz .done |
| 95 | |
| 96 | ; ctrl handling |
| 97 | mov byte [.mods], 0x04 |
| 98 | add byte [.ascii], 0x60 |
| 99 | |
| 100 | .done: |
| 101 | cli |
| 102 | |
| 103 | ; Restore GDT |
| 104 | o32 lgdt [ss:.gdt] |
| 105 | |
| 106 | ; Restore IDT |
| 107 | o32 lidt [ss:.idt] |
| 108 | |
| 109 | ; Jump back to pmode |
| 110 | mov ebx, cr0 |
| 111 | or bl, 1 |
| 112 | mov cr0, ebx |
| 113 | jmp 0x18:.bits32 |
| 114 | .bits32: |
| 115 | bits 32 |
| 116 | mov bx, 0x20 |
| 117 | mov ds, bx |
| 118 | mov es, bx |
| 119 | mov fs, bx |
| 120 | mov gs, bx |
| 121 | mov ss, bx |
| 122 | |
| 123 | ; Restore non-scratch GPRs |
| 124 | pop ebp |
| 125 | pop edi |
| 126 | pop esi |
| 127 | pop ebx |
| 128 | |
| 129 | ; Dehook int 0x08 |
| 130 | mov edx, dword [int_08_callback] |
| 131 | mov dword [0x08*4], edx |
| 132 | |
| 133 | cmp byte [.scan], 0 |
| 134 | je .fail |
| 135 | |
| 136 | push dword [.mods] |
| 137 | push dword [.ascii] |
| 138 | push dword [.scan] |
| 139 | call getchar_internal |
| 140 | add esp, 3*4 |
| 141 | |
| 142 | ret |
| 143 | |
| 144 | .fail: |
| 145 | xor eax, eax |
| 146 | ret |
| 147 | |
| 148 | .gdt: dq 0 |
| 149 | .idt: dq 0 |
| 150 | .rm_idt: dw 0x3ff |
| 151 | dd 0 |
| 152 | |
| 153 | .mods: dd 0 |
| 154 | .ascii: dd 0 |
| 155 | .scan: dd 0 |
| 156 | |
| 157 | section .note.GNU-stack noalloc noexec nowrite progbits |