GAS LISTING /tmp/cca8VC0c.s page 1 1 # 1 "/var/www/html/boot/syslinux/mbr/gptmbr.S" 1 /* ----------------------------------------------------------------------- 1 ... 0 0 2 * 3 * Copyright 2007-2009 H. Peter Anvin - All Rights Reserved 4 * Copyright 2009-2010 Intel Corporation; author: H. Peter Anvin 5 * 6 * Permission is hereby granted, free of charge, to any person 7 * obtaining a copy of this software and associated documentation 8 * files (the "Software"), to deal in the Software without 9 * restriction, including without limitation the rights to use, 10 * copy, modify, merge, publish, distribute, sublicense, and/or 11 * sell copies of the Software, and to permit persons to whom 12 * the Software is furnished to do so, subject to the following 13 * conditions: 14 * 15 * The above copyright notice and this permission notice shall 16 * be included in all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 * OTHER DEALINGS IN THE SOFTWARE. 26 * 27 * ----------------------------------------------------------------------- */ 28 29 #include "adjust.h" 1 /* -*- asm -*- ----------------------------------------------------------- 2 * 3 * Copyright 2009-2014 Intel Corporation; author: H. Peter Anvin 4 * 5 * Permission is hereby granted, free of charge, to any person 6 * obtaining a copy of this software and associated documentation 7 * files (the "Software"), to deal in the Software without 8 * restriction, including without limitation the rights to use, 9 * copy, modify, merge, publish, distribute, sublicense, and/or 10 * sell copies of the Software, and to permit persons to whom 11 * the Software is furnished to do so, subject to the following 12 * conditions: 13 * 14 * The above copyright notice and this permission notice shall 15 * be included in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 * OTHER DEALINGS IN THE SOFTWARE. GAS LISTING /tmp/cca8VC0c.s page 2 25 * 26 * ----------------------------------------------------------------------- */ 27 28 /* 29 * adjust.h 30 * 31 * Macros to adjust the drive number 32 */ 33 34 #ifndef ADJUST_H 35 #define ADJUST_H 36 37 #ifdef CTRL_80 38 .macro ADJUST_DRIVE 39 pusha 40 movb $0x02, %ah 41 int $0x16 42 testb $0x04, %al 43 popa 44 jz 1f 45 movb $0x80, %dl 46 1: 47 .endm 48 #elif defined(FORCE_80) 49 .macro ADJUST_DRIVE 50 movb $0x80, %dl 51 .endm 52 #else 53 .macro ADJUST_DRIVE 54 .endm 55 #endif 30 31 .code16 32 .text 33 34 .globl bootsec 35 stack = 0x7c00 36 37 /* Partition table header here */ 38 phdr = stack /* Above the stack, overwritten by bootsect */ 39 /* Partition table sector here */ 40 /* To handle > 32K we need to play segment tricks... */ 41 psec = _phdr + 512 42 43 /* Where we put DS:SI */ 44 dssi_out = _start + 0x1be 45 46 BIOS_kbdflags = 0x417 47 BIOS_page = 0x462 48 49 /* gas/ld has issues with doing this as absolute addresses... */ 50 .section ".bootsec", "a", @nobits 51 .globl bootsec 52 bootsec: 53 0000 00000000 .space 512 53 00000000 53 00000000 GAS LISTING /tmp/cca8VC0c.s page 3 53 00000000 53 00000000 54 55 .text 56 .globl _start 57 _start: 58 0000 33C0 .byte 0x33, 0xc0 /* xorw %ax, %ax */ 59 0002 FA cli 60 0003 8ED8 movw %ax, %ds 61 0005 8ED0 movw %ax, %ss 62 0007 BC007C movw $stack, %sp 63 000a 89E6 movw %sp, %si 64 000c 06 pushw %es /* 4(%bp) es:di -> $PnP header */ 65 000d 57 pushw %di /* 2(%bp) */ 66 000e 8EC0 movw %ax, %es 67 0010 FB sti 68 0011 FC cld 69 70 /* Copy down to 0:0x600 */ 71 0012 BF0000 movw $_start, %di 72 0015 B90001 movw $(512/2), %cx 73 0018 F3A5 rep; movsw 74 75 001a EA000000 ljmpw $0, $next 75 00 76 next: 77 78 ADJUST_DRIVE 79 001f 52 pushw %dx /* 0(%bp) = %dl -> drive number */ 80 0020 89E5 movw %sp, %bp /* %bp -> frame pointer: LEAVE UNCHANGED */ 81 82 /* prepare to read sector size */ 83 0022 83EC1C sub $0x1c, %sp /* -28(%bp) == %sp */ 84 0025 6A1E pushw $0x1e /* -30(%bp) == %sp */ 85 0027 C746FA00 movw $0x200, -6(%bp) /* -6(%bp) sector size */ 85 02 86 87 /* Check to see if we have EBIOS */ 88 002c 52 pushw %dx /* drive number */ 89 002d B441 movb $0x41, %ah /* %al == 0 already */ 90 002f BBAA55 movw $0x55aa, %bx 91 0032 31C9 xorw %cx, %cx 92 0034 30F6 xorb %dh, %dh 93 0036 F9 stc 94 0037 CD13 int $0x13 95 0039 5A popw %dx /* restore drive */ 96 003a B408 movb $0x08, %ah /* get CHS geometry */ 97 003c 7217 jc 1f 98 003e 81FB55AA cmpw $0xaa55, %bx 99 0042 7511 jne 1f 100 0044 D1E9 shrw %cx /* Bit 0 = fixed disk subset */ 101 0046 730D jnc 1f 102 103 /* We have EBIOS; patch in the following code at 104 read_sector_cbios: movb $0x42, %ah ; jmp read_common */ 105 0048 66C70600 movl $0xeb42b4+((read_common-read_sector_cbios-4) << 24), \ 105 00B442EB GAS LISTING /tmp/cca8VC0c.s page 4 105 13 106 (read_sector_cbios) 107 108 /* 109 * read sector size. 110 * Should not fail but if it does I assume that at least 111 * previous 512 value is not overridden 112 */ 113 0051 B448 movb $0x48, %ah 114 0053 89E6 movw %sp, %si 115 116 1: 117 /* Get (C)HS geometry */ 118 0055 CD13 int $0x13 119 120 /* here we computer CHS values or just do some dummy computation for EBIOS */ 121 0057 83E13F andw $0x3f, %cx /* Sector count */ 122 005a 51 pushw %cx /* -32(%bp) Save sectors on the stack */ 123 005b 0FB6C6 movzbw %dh, %ax /* dh = max head */ 124 005e 40 incw %ax /* From 0-based max to count */ 125 005f F7E1 mulw %cx /* Heads*sectors -> sectors per cylinder */ 126 127 /* Save sectors/cylinder on the stack */ 128 0061 52 pushw %dx /* -34(%bp) High word */ 129 0062 50 pushw %ax /* -36(%bp) Low word */ 130 131 /* Load partition table header */ 132 0063 6631C0 xorl %eax,%eax 133 0066 6699 cltd 134 0068 40 incw %ax /* %edx:%eax = 1 */ 135 0069 E8DC00 call read_sector_phdr 136 137 /* Number of partition sectors */ 138 /* We assume the partition table is 32K or less */ 139 006c 8B4E56 movw (80+6)(%bp),%cx /* NumberOfPartitionEntries */ 140 006f 8B465A movw (84+6)(%bp),%ax /* SizeOfPartitionEntry */ 141 0072 50 pushw %ax 142 0073 51 pushw %cx 143 0074 F7E1 mulw %cx 144 0076 F776FA divw -6(%bp) /* %dx == 0 here */ 145 0079 91 xchgw %ax,%cx 146 007a 41 incw %cx 147 148 /* Starting LBA of partition array */ 149 007b 668B464E movl (72+6)(%bp),%eax 150 007f 668B5652 movl (76+6)(%bp),%edx 151 152 0083 53 pushw %bx 153 get_ptab: 154 0084 E8C400 call read_sector 155 0087 E2FB loopw get_ptab 156 157 /* Find the boot partition */ 158 0089 31F6 xorw %si,%si /* Nothing found yet */ 159 008b 5F popw %di /* Partition table in memory */ 160 008c 59 popw %cx /* NumberOfPartitionEntries */ 161 008d 58 popw %ax /* SizeOfPartitionEntry */ GAS LISTING /tmp/cca8VC0c.s page 5 162 163 find_part: 164 /* If the PartitionTypeGUID is all zero, it's an empty slot */ 165 008e 668B15 movl (%di),%edx 166 0091 660B5504 orl 4(%di),%edx 167 0095 660B5508 orl 8(%di),%edx 168 0099 660B550C orl 12(%di),%edx 169 009d 740C jz not_this 170 009f F6453004 testb $0x04,48(%di) 171 00a3 7406 jz not_this 172 00a5 21F6 andw %si,%si 173 00a7 7519 jnz found_multiple 174 00a9 89FE movw %di,%si 175 not_this: 176 00ab 01C7 addw %ax,%di 177 00ad E2DF loopw find_part 178 179 00af 21F6 andw %si,%si 180 00b1 752E jnz found_part 181 182 missing_os: 183 00b3 E8E100 call error 184 00b6 4D697373 .ascii "Missing OS\r\n" 184 696E6720 184 4F530D0A 185 186 found_multiple: 187 00c2 E8D200 call error 188 00c5 4D756C74 .ascii "Multiple active partitions\r\n" 188 69706C65 188 20616374 188 69766520 188 70617274 189 190 found_part: 191 00e1 91 xchgw %ax,%cx /* Set up %cx for rep movsb further down */ 192 193 00e2 BF0000 movw $dssi_out,%di 194 00e5 57 pushw %di 195 196 /* 80 00 00 00 ee 00 00 00 197 - bootable partition, type EFI (EE), no CHS information */ 198 00e6 6631C0 xorl %eax,%eax 199 00e9 B080 movb $0x80,%al 200 00eb 66AB stosl 201 00ed B0ED movb $0xed,%al 202 00ef 66AB stosl 203 00f1 668B4420 movl 32(%si),%eax 204 00f5 668B5424 movl 36(%si),%edx 205 00f9 E84000 call saturate_stosl /* Partition start */ 206 207 00fc 668B4428 movl 40(%si),%eax 208 0100 668B542C movl 44(%si),%edx 209 0104 662B4420 subl 32(%si),%eax 210 0108 661B5424 sbbl 36(%si),%edx 211 010c E87000 call inc64 212 010f E82A00 call saturate_stosl /* Partition length */ GAS LISTING /tmp/cca8VC0c.s page 6 213 214 0112 660FB7C1 movzwl %cx,%eax /* Length of GPT entry */ 215 0116 66AB stosl 216 217 0118 F3A4 rep; movsb /* GPT entry follows MBR entry */ 218 011a 5E popw %si 219 220 /* 221 * boot: invoke the actual bootstrap. %ds:%si points to the 222 * partition information in memory. The top word on the stack 223 * is phdr == 0x7c00 == the address of the boot sector. 224 */ 225 boot: 226 011b 668B4434 movl (32+20)(%si),%eax 227 011f 668B5438 movl (36+20)(%si),%edx 228 0123 E82200 call read_sector_phdr 229 0126 813EFE7D cmpw $0xaa55, (0x7c00+0x1fe) 229 55AA 230 012c 7585 jne missing_os /* Not a valid boot sector */ 231 012e 89EC movw %bp, %sp /* driveno == bootsec-6 */ 232 0130 5A popw %dx /* dl -> drive number */ 233 0131 5F popw %di /* es:di -> $PnP vector */ 234 0132 07 popw %es 235 0133 66B82147 movl $0x54504721,%eax /* !GPT magic number */ 235 5054 236 0139 FA cli 237 013a FFE4 jmpw *%sp /* %sp == bootsec */ 238 239 /* 240 * Store the value in %eax to %di iff %edx == 0, otherwise store -1. 241 * Returns the value that was actually written in %eax. 242 */ 243 saturate_stosl: 244 013c 6621D2 andl %edx,%edx 245 013f 7404 jz 1f 246 0141 6683C8FF orl $-1,%eax 247 0145 66AB 1: stosl 248 0147 C3 ret 249 250 read_sector_phdr: 251 0148 BB007C movw $phdr, %bx 252 253 /* fall through and read sector */ 254 255 /* 256 * read_sector: read a single sector pointed to by %edx:%eax to 257 * %es:%bx. CF is set on error. All registers saved. 258 * %edx:%eax and %es:%bx are incremented to read next sector 259 */ 260 read_sector: 261 014b 6660 pushal 262 014d 6652 pushl %edx /* MSW of LBA */ 263 014f 6650 pushl %eax /* LSW of LBA */ 264 0151 06 pushw %es /* Buffer segment */ 265 0152 53 pushw %bx /* Buffer offset */ 266 0153 6A01 pushw $1 /* Sector count */ 267 0155 6A10 pushw $16 /* Size of packet */ GAS LISTING /tmp/cca8VC0c.s page 7 268 0157 89E6 movw %sp, %si 269 270 /* This chunk is skipped if we have ebios */ 271 /* Do not clobber %es:%bx or %edx:%eax before this chunk! */ 272 read_sector_cbios: 273 0159 66F776DC divl -36(%bp) /* secpercyl */ 274 015d C0E406 shlb $6, %ah 275 0160 88E1 movb %ah, %cl 276 0162 88C5 movb %al, %ch 277 0164 92 xchgw %dx, %ax 278 0165 F676E0 divb -32(%bp) /* sectors */ 279 0168 88C6 movb %al, %dh 280 016a 08E1 orb %ah, %cl 281 016c 41 incw %cx /* Sectors are 1-based */ 282 016d B80102 movw $0x0201, %ax 283 284 read_common: 285 0170 8A5600 movb (%bp), %dl /* driveno */ 286 0173 CD13 int $0x13 287 0175 8D6410 leaw 16(%si), %sp /* Drop DAPA */ 288 0178 6661 popal 289 017a 720C jc disk_error 290 017c 027EFB addb -5(%bp), %bh /* bx += sector size: point to the next buffer */ 291 292 /* fall through and increment sector number */ 293 294 /* 295 * Increment %edx:%eax 296 */ 297 inc64: 298 017f 6683C001 addl $1,%eax 299 0183 6683D200 adcl $0,%edx 300 0187 C3 ret 301 302 disk_error: 303 0188 E80C00 call error 304 018b 4469736B .ascii "Disk error\r\n" 304 20657272 304 6F720D0A 305 306 /* 307 * Print error messages. This is invoked with "call", with the 308 * error message at the return address. 309 */ 310 error: 311 0197 5E popw %si 312 2: 313 0198 AC lodsb 314 0199 B40E movb $0x0e, %ah 315 019b 8A3E6204 movb (BIOS_page), %bh 316 019f B307 movb $0x07, %bl 317 01a1 CD10 int $0x10 /* May destroy %bp */ 318 01a3 3C0A cmpb $10, %al /* Newline? */ 319 01a5 75F1 jne 2b 320 321 01a7 CD18 int $0x18 /* Boot failure */ 322 die: GAS LISTING /tmp/cca8VC0c.s page 8 323 01a9 F4 hlt 324 01aa EBFD jmp die GAS LISTING /tmp/cca8VC0c.s page 9 DEFINED SYMBOLS /var/www/html/boot/syslinux/mbr/gptmbr.S:52 .bootsec:0000000000000000 bootsec /var/www/html/boot/syslinux/mbr/gptmbr.S:35 *ABS*:0000000000007c00 stack /var/www/html/boot/syslinux/mbr/gptmbr.S:38 *ABS*:0000000000007c00 phdr /var/www/html/boot/syslinux/mbr/gptmbr.S:44 .text:00000000000001be dssi_out /var/www/html/boot/syslinux/mbr/gptmbr.S:57 .text:0000000000000000 _start /var/www/html/boot/syslinux/mbr/gptmbr.S:46 *ABS*:0000000000000417 BIOS_kbdflags /var/www/html/boot/syslinux/mbr/gptmbr.S:47 *ABS*:0000000000000462 BIOS_page /var/www/html/boot/syslinux/mbr/gptmbr.S:76 .text:000000000000001f next /var/www/html/boot/syslinux/mbr/gptmbr.S:284 .text:0000000000000170 read_common /var/www/html/boot/syslinux/mbr/gptmbr.S:272 .text:0000000000000159 read_sector_cbios /var/www/html/boot/syslinux/mbr/gptmbr.S:250 .text:0000000000000148 read_sector_phdr /var/www/html/boot/syslinux/mbr/gptmbr.S:153 .text:0000000000000084 get_ptab /var/www/html/boot/syslinux/mbr/gptmbr.S:260 .text:000000000000014b read_sector /var/www/html/boot/syslinux/mbr/gptmbr.S:163 .text:000000000000008e find_part /var/www/html/boot/syslinux/mbr/gptmbr.S:175 .text:00000000000000ab not_this /var/www/html/boot/syslinux/mbr/gptmbr.S:186 .text:00000000000000c2 found_multiple /var/www/html/boot/syslinux/mbr/gptmbr.S:190 .text:00000000000000e1 found_part /var/www/html/boot/syslinux/mbr/gptmbr.S:182 .text:00000000000000b3 missing_os /var/www/html/boot/syslinux/mbr/gptmbr.S:310 .text:0000000000000197 error /var/www/html/boot/syslinux/mbr/gptmbr.S:243 .text:000000000000013c saturate_stosl /var/www/html/boot/syslinux/mbr/gptmbr.S:297 .text:000000000000017f inc64 /var/www/html/boot/syslinux/mbr/gptmbr.S:225 .text:000000000000011b boot /var/www/html/boot/syslinux/mbr/gptmbr.S:302 .text:0000000000000188 disk_error /var/www/html/boot/syslinux/mbr/gptmbr.S:322 .text:00000000000001a9 die UNDEFINED SYMBOLS _phdr