Reboot: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
(Cleanup, still needs more work)
m (Changed frz() to halt() (see discussion page) and and added *b suffix to inport() and outport() in short code)
Line 10: Line 10:
unsigned char good = 0x02;
unsigned char good = 0x02;
while ((good & 0x02) != 0)
while ((good & 0x02) != 0)
good = inport(0x64);
good = inportb(0x64);
outport(0x64, 0xFE);
outportb(0x64, 0xFE);
frz();
halt();
}
}
</source>
</source>

Revision as of 12:09, 16 July 2012

There are several methods for rebooting, including

  1. load a 0-sized IDT and issue an interrupt (that'll triple fault and reset)
  2. Use the 8042 keyboard controller to pulse the CPU's RESET pin

Short code to do a 8042 reset

void reboot()
{
    unsigned char good = 0x02;
    while ((good & 0x02) != 0)
        good = inportb(0x64);
    outportb(0x64, 0xFE);
    halt();
}


Annotated code for reboot()

typedef unsigned char uchar;
typedef uchar byte;

/* keyboard interface IO port: data and control
   READ:   status port
   WRITE:  control register */
#define KBRD_INTRFC 0x64

/* keyboard interface bits */
#define KBRD_BIT_KDATA 0 /* keyboard data is in buffer (output buffer is empty) (bit 0) */
#define KBRD_BIT_UDATA 1 /* user data is in buffer (command buffer is empty) (bit 1) */

#define KBRD_IO 0x60 /* keyboard IO port */
#define KBRD_RESET 0xFE /* reset CPU command */

#define bit(n) (1<<(n)) /* Set bit n to 1 */

/* Check if bit n in flags is set */
#define check_flag(flags, n) ((flags) & bit(n))

void reboot()
{
    byte temp;

    asm volatile ("CLI"); /* disable all interrupts */

    /* Clear all keyboard buffers (output and command buffers) */
    do
    {
        temp = inb(KBRD_INTRFC); /* empty user data */
        if (check_flag(temp, KBRD_BIT_KDATA) != 0)
            inb(KBRD_IO); /* empty keyboard data */
    } while (check_flag(temp, KBRD_BIT_UDATA) != 0);

    outb(KBRD_INTRFC, KBRD_RESET); /* pulse CPU reset line */
    asm volatile ("HLT"); /* if that didn't work, halt the CPU */
}