This is the first in a series of patches that removes a bug and some glitches from GRUB stage 1. This is against grub-0.94 (alpha) Submitted by Wolf Lammen, ookami1gmxde I looked through the complete code of stage 1 and found some oddities. This patch addresses one: The use of cli in the very beginning of the code. I consider this instruction as pointless, so one should remove it. Impact: Saves a valuable byte of code, without doing any harm. A comment says, that we are not safe there (to allow interrupts). This could be true only, if a sloppy chainloader or a buggy BIOS did not set up a proper stack pointer *and* had enabled interrupts before jumping to stage 1. In the rare case this holds true, then, indeed, if a hardware interrupt occurs, while the CPU processes the very first instructions of stage 1, the computer will inevitably crash. But how could a CLI help about this? The jump instruction at address 0x7C00 has been executed unprotected before, so any pending interrupt would have occured there latest, using the bogus stack pointer. CLI protects only future instructions, thus narrowing the dangerous gap by not even a micro second on old 386, before stage 1 sets up his own and valid stack. Reordering the instructions, so that a valid stack pointer is set up first, simply does what a CLI could do in such a messy situation. --- grub-0.94/stage1/stage1.S 2003-07-09 13:45:51.000000000 +0200 +++ grub-latest/stage1/stage1.S 2004-02-06 12:35:21.000000000 +0100 @@ -105,7 +105,13 @@ after_BPB: /* general setup */ - cli /* we're not safe here! */ + xorw %ax, %ax + movw %ax, %ss /* automatically locks the following instruction */ + + /* set up the REAL stack */ + movw $STAGE1_STACKSEG, %sp + sti /* just in case the BIOS or a chainloader disabled interrupts */ + /* * This is a workaround for buggy BIOSes which don't pass boot @@ -124,15 +130,8 @@ real_start: - /* set up %ds and %ss as offset from 0 */ - xorw %ax, %ax + /* set up %ds as offset from 0 */ movw %ax, %ds - movw %ax, %ss - - /* set up the REAL stack */ - movw $STAGE1_STACKSEG, %sp - - sti /* we're safe again */ /* * Check if we have a forced disk reference here