diff options
author | H. Peter Anvin <hpa@zytor.com> | 2009-04-04 15:33:29 -0700 |
---|---|---|
committer | H. Peter Anvin <hpa@zytor.com> | 2009-04-04 15:33:29 -0700 |
commit | 5ca566c49a54e095042f607e1a6d29a74f344302 (patch) | |
tree | 400ea996b5969a976acf3372962509a752355312 | |
parent | 390a4bf409a4574fe3272adf9f873ef78b78c167 (diff) | |
parent | f7b5a2254ab7c8aa87679c1b731d2ea285c22e80 (diff) | |
download | syslinux-5ca566c49a54e095042f607e1a6d29a74f344302.tar.gz |
Merge branch 'ctrl-mbr'
-rw-r--r-- | MCONFIG | 2 | ||||
-rw-r--r-- | mbr/Makefile | 28 | ||||
-rw-r--r-- | mbr/adjust.h | 53 | ||||
-rw-r--r-- | mbr/altmbr.S | 306 | ||||
-rwxr-xr-x | mbr/checksize.pl | 36 | ||||
-rw-r--r-- | mbr/gptmbr.S | 18 | ||||
-rw-r--r-- | mbr/isohdpfx.S | 17 | ||||
-rw-r--r-- | mbr/mbr.S | 14 |
8 files changed, 439 insertions, 35 deletions
@@ -36,6 +36,8 @@ NASMOPT = -O9999 PERL = perl +CHMOD = chmod + CC = gcc gcc_ok = $(shell tmpf=gcc_ok.$$$$.tmp; \ if $(CC) $(1) -c $(topdir)/dummy.c -o $$tmpf 2>/dev/null ; \ diff --git a/mbr/Makefile b/mbr/Makefile index 0bdf7e3f..6cd677c0 100644 --- a/mbr/Makefile +++ b/mbr/Makefile @@ -1,6 +1,7 @@ ## ----------------------------------------------------------------------- ## -## Copyright 2007-2008 H. Peter Anvin - All Rights Reserved +## Copyright 2007-2009 H. Peter Anvin - All Rights Reserved +## Copyright 2009 Intel Corporation; author: H. Peter Anvin ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by @@ -17,30 +18,31 @@ topdir = .. include $(topdir)/MCONFIG.embedded -all: mbr.bin gptmbr.bin isohdpfx.bin +all: mbr.bin altmbr.bin gptmbr.bin isohdpfx.bin \ + mbr_c.bin altmbr_c.bin gptmbr_c.bin isohdpfx_c.bin \ + mbr_f.bin altmbr_f.bin gptmbr_f.bin isohdpfx_f.bin .PRECIOUS: %.o -%.o: %.S +%.o: %.S adjust.h $(CC) $(SFLAGS) -Wa,-a=$*.lst -c -o $@ $< +%_c.o: %.S adjust.h + $(CC) $(SFLAGS) -Wa,-a=$*_c.lst -DCTRL_80 -c -o $@ $< + +%_f.o: %.S adjust.h + $(CC) $(SFLAGS) -Wa,-a=$*_f.lst -DFORCE_80 -c -o $@ $< + .PRECIOUS: %.elf %.elf: %.o mbr.ld $(LD) $(LDFLAGS) -T mbr.ld -e _start -o $@ $< -mbr.bin: mbr.elf checksize.pl - $(OBJCOPY) -O binary $< $@ - $(PERL) checksize.pl $@ 440 - -isohdpfx.bin: isohdpfx.elf checksize.pl +%.bin: %.elf checksize.pl $(OBJCOPY) -O binary $< $@ - $(PERL) checksize.pl $@ 432 + $(PERL) checksize.pl $@ + $(CHMOD) -x $@ mbr_bin.c: mbr.bin -gptmbr.bin: gptmbr.elf checksize.pl - $(OBJCOPY) -O binary $< $@ - $(PERL) checksize.pl $@ 424 - tidy dist: rm -f *.o *.elf *.lst diff --git a/mbr/adjust.h b/mbr/adjust.h new file mode 100644 index 00000000..42c12a35 --- /dev/null +++ b/mbr/adjust.h @@ -0,0 +1,53 @@ +/* -*- asm -*- ----------------------------------------------------------- + * + * Copyright 2009 Intel Corporation; author: H. Peter Anvin + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall + * be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * ----------------------------------------------------------------------- */ + +/* + * adjust.h + * + * Macros to adjust the drive number + */ + +#ifndef ADJUST_H +#define ADJUST_H + +#ifdef CTRL_80 + .macro ADJUST_DRIVE + testb $0x04, BIOS_kbdflags + jz 1f + movb $0x80, %dl +1: + .endm +#elif defined(FORCE_80) + .macro ADJUST_DRIVE + movb $0x80, %dl + .endm +#else + .macro ADJUST_DRIVE + .endm +#endif + +#endif /* ADJUST_H */ diff --git a/mbr/altmbr.S b/mbr/altmbr.S new file mode 100644 index 00000000..4a502ed0 --- /dev/null +++ b/mbr/altmbr.S @@ -0,0 +1,306 @@ +/* ----------------------------------------------------------------------- + * + * Copyright 2007-2009 H. Peter Anvin - All Rights Reserved + * Copyright 2009 Intel Corporation; author: H. Peter Anvin + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall + * be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * ----------------------------------------------------------------------- */ + +#include "adjust.h" + + .code16 + .text + + .globl bootsec +stack = 0x7c00 +driveno = (stack-6) +sectors = (stack-8) +secpercyl = (stack-12) + +BIOS_kbdflags = 0x417 +BIOS_page = 0x462 + + /* gas/ld has issues with doing this as absolute addresses... */ + .section ".bootsec", "a", @nobits + .globl bootsec +bootsec: + .space 512 + + .text + .globl _start +_start: + cli + xorw %ax, %ax + movw %ax, %ds + movw %ax, %ss + movw $stack, %sp + movw %sp, %si + pushw %es /* es:di -> $PnP header */ + pushw %di + movw %ax, %es + sti + cld + + /* Copy down to 0:0x600 */ + movw $_start, %di + movw $(512/2), %cx + rep; movsw + + ljmpw $0, $next +next: + + ADJUST_DRIVE + pushw %dx /* dl -> drive number */ + + /* Check to see if we have EBIOS */ + pushw %dx /* drive number */ + movb $0x41, %ah /* %al == 0 already */ + movw $0x55aa, %bx + xorw %cx, %cx + xorb %dh, %dh + stc + int $0x13 + jc 1f + cmpw $0xaa55, %bx + jne 1f + shrw %cx /* Bit 0 = fixed disk subset */ + jnc 1f + + /* We have EBIOS; patch in the following code at + read_sector_cbios: movb $0x42, %ah ; jmp read_common */ + movl $0xeb42b4+((read_common-read_sector_cbios-4) << 24), \ + (read_sector_cbios) + +1: + popw %dx + + /* Get (C)HS geometry */ + movb $0x08, %ah + int $0x13 + andw $0x3f, %cx /* Sector count */ + pushw %cx /* Save sectors on the stack */ + movzbw %dh, %ax /* dh = max head */ + incw %ax /* From 0-based max to count */ + mulw %cx /* Heads*sectors -> sectors per cylinder */ + + /* Save sectors/cylinder on the stack */ + pushw %dx /* High word */ + pushw %ax /* Low word */ + + xorl %eax, %eax /* Base */ + cdq /* Root (%edx <- 0) */ + call scan_partition_table + + /* If we get here, we have no OS */ +missing_os: + call error + .ascii "Missing operating system.\r\n" + +/* + * read_sector: read a single sector pointed to by %eax to 0x7c00. + * CF is set on error. All registers saved. + */ +read_sector: + pushal + xorl %edx, %edx + movw $bootsec, %bx + pushl %edx /* MSW of LBA */ + pushl %eax /* LSW of LBA */ + pushw %es /* Buffer segment */ + pushw %bx /* Buffer offset */ + pushw $1 /* Sector count */ + pushw $16 /* Size of packet */ + movw %sp, %si + + /* This chunk is skipped if we have ebios */ + /* Do not clobber %eax before this chunk! */ + /* This also relies on %bx and %edx as set up above. */ +read_sector_cbios: + divl (secpercyl) + shlb $6, %ah + movb %ah, %cl + movb %al, %ch + xchgw %dx, %ax + divb (sectors) + movb %al, %dh + orb %ah, %cl + incw %cx /* Sectors are 1-based */ + movw $0x0201, %ax + +read_common: + movb (driveno), %dl + int $0x13 + leaw 16(%si), %sp /* Drop DAPA */ + popal + ret + +/* + * read_partition_table: + * Read a partition table (pointed to by %eax), and copy + * the partition table into the ptab buffer. + * + * Clobbers %si, %di, and %cx, other registers preserved. + * %cx = 0 on exit. + * + * On error, CF is set and ptab is overwritten with junk. + */ +ptab = _start+446 + +read_partition_table: + call read_sector + movw $bootsec+446, %si + movw $ptab, %di + movw $(16*4/2), %cx + rep ; movsw + ret + +/* + * scan_partition_table: + * Scan a partition table currently loaded in the partition table + * area. Preserve all registers. + * + * On entry: + * %eax - base (location of this partition table) + * %edx - root (offset from MBR, or 0 for MBR) + * + * These get pushed into stack slots: + * 28(%bp) - %eax - base + * 20(%bp) - %edx - root + */ + +scan_partition_table: + pushal + movw %sp, %bp + + /* Scan the primary partition table */ + movw $ptab, %si + movw $4, %cx + push %si + push %cx + + /* Is it a primary partition? */ + andl %edx, %edx + jnz 6f + +5: + decb (partition) + jz boot + addw $16, %bx + loopw 5b + +6: + /* No active partitions found, look for extended partitions */ + popw %cx /* %cx <- 4 */ + popw %si /* %si <- ptab */ +7: + movl 20(%bp), %edx /* "Root" */ + + movb 4(%si), %al + cmpb $0x0f, %al /* 0x0f = Win9x extended */ + je 8f + andb $~0x80, %al /* 0x85 = Linux extended */ + cmpb $0x05, %al /* 0x05 = MS-DOS extended */ + jne 9f + + /* It is an extended partition. Read the extended partition and + try to scan it. If the scan returns, re-load the current + partition table and resume scan. */ +8: + movl 8(%si), %eax /* Partition table offset */ + addl %edx, %eax /* Compute location of new ptab */ + andl %edx, %edx /* Is this the MBR? */ + jnz 10f + movl %eax, %edx /* Offset -> root if this was MBR */ +10: + call read_partition_table + jc 11f + call scan_partition_table +11: + /* This returned, so we need to reload the current partition table */ + movl 28(%bp), %eax /* "Base" */ + call read_partition_table + + /* fall through */ +9: + /* Not an extended partition */ + andb %al, %al + jz 12f /* Not a valid partition */ + andl %edx, %edx /* Are we inside an extended part? */ + /* If so, this is a logical partition */ + decb (partition) + je boot +12: + addw $16, %si + loopw 7b + + /* Nothing found, return */ + popal + ret + +/* + * boot: invoke the actual bootstrap. (%si) points to the partition + * table entry, and 28(%bp) has the partition table base. + */ +boot: + cmpb $0, 4(%si) + je missing_os + movl 8(%si), %eax + addl 28(%bp), %eax + movl %eax, 8(%si) /* Adjust in-memory partition table entry */ + call read_sector + jc disk_error + cmpw $0xaa55, (bootsec+510) + jne missing_os /* Not a valid boot sector */ + movw $driveno, %sp /* driveno == bootsec-6 */ + popw %dx /* dl -> drive number */ + popw %di /* es:di -> $PnP vector */ + popw %es + cli + jmpw *%sp /* %sp == bootsec */ + +disk_error: + call error + .ascii "Operating system load error.\r\n" + +/* + * Print error messages. This is invoked with "call", with the + * error message at the return address. + */ +error: + popw %si +2: + lodsb + movb $0x0e, %ah + movb (BIOS_page), %bh + movb $0x07, %bl + int $0x10 /* May destroy %bp */ + cmpb $10, %al /* Newline? */ + jne 2b + + int $0x18 /* Boot failure */ +die: + hlt + jmp die + +/* Location of the partition configuration byte */ +partition = _start + 439 diff --git a/mbr/checksize.pl b/mbr/checksize.pl index 68a035b7..4648c95c 100755 --- a/mbr/checksize.pl +++ b/mbr/checksize.pl @@ -1,6 +1,7 @@ ## ----------------------------------------------------------------------- ## -## Copyright 2007-2008 H. Peter Anvin - All Rights Reserved +## Copyright 2007-2009 H. Peter Anvin - All Rights Reserved +## Copyright 2009 Intel Corporation; author: H. Peter Anvin ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by @@ -13,18 +14,41 @@ ## ## checksize.pl ## -## Check the size of a binary file +## Check the size of a binary file and pad it with zeroes to that size ## -($file, $maxsize) = @ARGV; +use bytes; -@st = stat($file); +($file, $maxsize, $padsize) = @ARGV; + +if (!defined($maxsize)) { + # Defaults based on the filename + if ($file =~ /^mbr[^0-9a-z]/) { + $maxsize = $padsize = 440; + } elsif ($file =~ /^gptmbr[^0-9a-z]/) { + $maxsize = $padsize = 424; + } elsif ($file =~ /^isohdpfx[^0-9a-z]/) { + $maxsize = $padsize = 432; + } elsif ($file =~ /^altmbr[^0-9a-z]/) { + $maxsize = 439; $padsize = 440; + } else { + die "$0: no default size for filename: $file\n"; + } +} + +$padsize = $maxsize unless(defined($padsize)); + +open(FILE, '+<', $file) or die; +@st = stat(FILE); if (!defined($size = $st[7])) { die "$0: $file: $!\n"; } if ($size > $maxsize) { print STDERR "$file: too big ($size > $maxsize)\n"; exit 1; -} else { - exit 0; +} elsif ($size < $padsize) { + seek(FILE, $size, 0); + print FILE "\0" x ($padsize-$size); } + +exit 0; diff --git a/mbr/gptmbr.S b/mbr/gptmbr.S index 385afe74..8d42e8b8 100644 --- a/mbr/gptmbr.S +++ b/mbr/gptmbr.S @@ -1,6 +1,7 @@ /* ----------------------------------------------------------------------- * - * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved + * Copyright 2007-2009 H. Peter Anvin - All Rights Reserved + * Copyright 2009 Intel Corporation; author: H. Peter Anvin * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation @@ -25,6 +26,8 @@ * * ----------------------------------------------------------------------- */ +#include "adjust.h" + .code16 .text @@ -42,6 +45,7 @@ bootguid = _start + 0x1a8 /* Where we put DS:SI */ dssi_out = _start + 0x1be +BIOS_kbdflags = 0x417 BIOS_page = 0x462 /* gas/ld has issues with doing this as absolute addresses... */ @@ -61,7 +65,6 @@ _start: movw %sp, %si pushw %es /* 4(%bp) es:di -> $PnP header */ pushw %di /* 2(%bp) */ - pushw %dx /* 0(%bp) = %dl -> drive number */ movw %ax, %es sti cld @@ -72,8 +75,11 @@ _start: rep; movsw ljmpw $0, $next - next: + + ADJUST_DRIVE + pushw %dx /* 0(%bp) = %dl -> drive number */ + /* Check to see if we have EBIOS */ pushw %dx /* drive number */ movb $0x41, %ah /* %al == 0 already */ @@ -163,10 +169,10 @@ find_part: found_part: xchgw %ax,%cx /* Set up %cx for rep movsb further down */ - + movw $dssi_out,%di pushw %di - + /* 80 00 00 00 ee 00 00 00 - bootable partition, type EFI (EE), no CHS information */ xorl %eax,%eax @@ -210,7 +216,7 @@ boot: missing_os: call error - .ascii "Operating system not bootable\r\n" + .ascii "OS not bootable\r\n" saturate_stosl: pushl %eax diff --git a/mbr/isohdpfx.S b/mbr/isohdpfx.S index 4da49468..f42b4b5b 100644 --- a/mbr/isohdpfx.S +++ b/mbr/isohdpfx.S @@ -33,12 +33,14 @@ * loader of a hard-coded offset, but that's good enough to load * ISOLINUX. */ - + +#include "adjust.h" + .code16 .text HYBRID_MAGIC = 0x7078c0fb -isolinux_hybrid_signature = 0x7c00+64 +isolinux_hybrid_signature = 0x7c00+64 isolinux_start_hybrid = 0x7c00+64+4 .globl bootsec @@ -50,7 +52,8 @@ sectors = (stack-10) heads = (stack-12) secpercyl = (stack-16) -BIOS_page = 0x462 +BIOS_kbdflags = 0x417 +BIOS_page = 0x462 /* gas/ld has issues with doing this as absolute addresses... */ .section ".bootsec", "a", @nobits @@ -70,7 +73,6 @@ _start: movw %sp, %si pushw %es /* es:di -> $PnP header */ pushw %di - pushw %dx /* dl -> drive number */ movw %ax, %es sti cld @@ -81,8 +83,11 @@ _start: rep; movsw ljmpw $0, $next - next: + + ADJUST_DRIVE + pushw %dx /* dl -> drive number */ + /* Check to see if we have EBIOS */ pushw %dx /* drive number */ movb $0x41, %ah /* %al == 0 already */ @@ -141,7 +146,7 @@ next: */ cmpl $HYBRID_MAGIC,(isolinux_hybrid_signature) jne bad_signature - + cli movw $heads, %sp @@ -1,6 +1,7 @@ /* ----------------------------------------------------------------------- * - * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved + * Copyright 2007-2009 H. Peter Anvin - All Rights Reserved + * Copyright 2009 Intel Corporation; author: H. Peter Anvin * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation @@ -25,6 +26,8 @@ * * ----------------------------------------------------------------------- */ +#include "adjust.h" + .code16 .text @@ -34,7 +37,8 @@ driveno = (stack-6) sectors = (stack-8) secpercyl = (stack-12) -BIOS_page = 0x462 +BIOS_kbdflags = 0x417 +BIOS_page = 0x462 /* gas/ld has issues with doing this as absolute addresses... */ .section ".bootsec", "a", @nobits @@ -53,7 +57,6 @@ _start: movw %sp, %si pushw %es /* es:di -> $PnP header */ pushw %di - pushw %dx /* dl -> drive number */ movw %ax, %es sti cld @@ -64,8 +67,11 @@ _start: rep; movsw ljmpw $0, $next - next: + + ADJUST_DRIVE + pushw %dx /* dl -> drive number */ + /* Check to see if we have EBIOS */ pushw %dx /* drive number */ movb $0x41, %ah /* %al == 0 already */ |