grub/grub-core/lib/i386
C. Masloch e96e785580 freedos: Fix FreeDOS command booting large files (near or above 64 KiB)
While testing the 86-DOS lDebug [1] booting from GRUB2, newer versions of the
debugger would fail to load when booted using GRUB's freedos command. The
behaviour observed in a qemu i386 machine was that the ROM-BIOS's boot load
would start anew, instead of loading the selected debugger as kernel.

It came to light that there was a size limit: Kernel files that were 58880
bytes (E600h) long or shorter succeeded to boot, while files that were 64000
bytes or longer failed in the manner described.

Eventually it turned out that the relocator16 stub succeeded whenever it was
placed completely within the first 64 KiB of the Low Memory Area. The chunk
for the relocator is allocated with a minimum address of 0x8010 and a maximum
address just below 0xA0000 [2]. That means if the kernel is, for instance,
E600h bytes long, then the kernel will be allocated memory starting at 00600h
(the fixed FreeDOS kernel load address) up to E600h + 00600h = 0EC00h, which
leaves 1400h (5120) bytes for the relocator to stay in the first 64 KiB.
If the kernel is 64000 bytes (FA00h) long, then the relocator must go to
FA00h + 00600h = 10000h at least which is outside the first 64 KiB.

The problem is that the relocator16 initialises the DS register with a
"pseudo real mode" descriptor, which is defined with a segment limit of
64 KiB and a segment base of zero. After that, the relocator addressed
parts of itself (implicitly) using the DS register, with an offset from
ESI, which holds the linear address of the relocator's base [3]. With the
larger kernel files this would lead to accessing data beyond the 64 KiB
segment limit, presumably leading to a fault and perhaps a subsequent
triple-fault or such.

This patch fixes the relocator to set the segment base of the descriptors
to the base address of the relocator; then, the subsequent accesses to
the relocator's variables are done without the ESI register as an index.
This does not interfere with the relocator's or its target's normal
operation; the segment limits are still loaded with 64 KiB and all the
segment bases are subsequently reset by the relocator anyway.

Current versions of the debugger to test are uploaded to [4]. The file
ldebugnh.com (LZ4-compressed and built with -D_EXTHELP=0) at 58368 bytes
loads successfully, whereas ldebug.com at 64000 bytes fails. Loading one
of these files requires setting root to a FAT FS partition and using the
freedos command to specify the file as kernel:

set root='(hd0,msdos1)'
freedos /ldebug.com
boot

Booting the file using the multiboot command (which uses a WIP entrypoint
of the debugger) works, as it does not use GRUB's relocator16 but instead
includes a loader in the kernel itself, which drops it back to 86 Mode.

[1]: https://hg.ulukai.org/ecm/ldebug
[2]: http://git.savannah.gnu.org/cgit/grub.git/tree/grub-core/lib/i386/relocator.c?id=495781f5ed1b48bf27f16c53940d6700c181c74c#n127
[3]: http://git.savannah.gnu.org/cgit/grub.git/tree/grub-core/lib/i386/relocator16.S?id=495781f5ed1b48bf27f16c53940d6700c181c74c#n97
[4]: https://ulukai.org/ecm/lDebug-5479a7988d21-nohelp.zip

Signed-off-by: C. Masloch <pushbx@ulukai.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2020-01-28 21:16:48 +01:00
..
pc Add const keyword to grub_env_get and gettextize week days. 2011-11-11 20:34:37 +01:00
xen xen: Fix wrong register in relocator. 2017-03-05 10:07:36 +01:00
backtrace.c backtrace: Fix register call syntax 2015-12-31 14:54:48 +01:00
halt.c asm: Replace "__asm__ __volatile__" with "asm volatile" 2019-03-12 20:04:07 +01:00
random.c Add RNG module. 2016-02-12 12:39:38 +01:00
reboot.c i386: Don't include lib/i386/reset.c in EFI builds 2018-07-25 14:18:11 +02:00
reboot_trampoline.S * grub-core/kern/i386/pc/startup.S (grub_exit): Add missing zeroing-out. 2011-10-23 23:04:57 +02:00
relocator.c i386/relocator: Remove unused extern grub_relocator64_rip_addr 2015-05-07 18:00:47 +02:00
relocator16.S freedos: Fix FreeDOS command booting large files (near or above 64 KiB) 2020-01-28 21:16:48 +01:00
relocator32.S Zero %ebp and %edi when entering Linux's 32-bit entry point, as 2010-09-02 22:36:09 +01:00
relocator64.S i386/relocator: Align stack in grub_relocator64_efi relocator 2017-02-02 22:24:47 +01:00
relocator_asm.S Merge mainline into newreloc. For now without boot tests 2010-08-25 03:25:18 +02:00
relocator_common.S * grub-core/lib/i386/relocator_common.S [__APPLE__ && __x86_64__]: Use 2013-11-11 23:02:51 +01:00
relocator_common_c.c Add new ports: i386-xen and x86_64-xen. This allows running GRUB in 2013-11-09 21:29:11 +01:00
setjmp.S * include/grub/dl.h: Switch from APPLE_CC to __APPLE__. 2012-02-29 14:51:09 +01:00