1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * header.S 4 * 5 * Copyright (C) 1991, 1992 Linus Torvald 6 * 7 * Based on bootsect.S and setup.S 8 * modified by more people than can be co 9 * 10 * Rewritten as a common file by H. Peter 11 * 12 * BIG FAT NOTE: We're in real mode using 64k 13 * addresses must be multiplied by 16 to obtai 14 * addresses. To avoid confusion, linear addre 15 * hex while segment addresses are written as 16 * 17 */ 18 #include <linux/pe.h> 19 #include <asm/segment.h> 20 #include <asm/boot.h> 21 #include <asm/page_types.h> 22 #include <asm/setup.h> 23 #include <asm/bootparam.h> 24 #include "boot.h" 25 #include "voffset.h" 26 #include "zoffset.h" 27 28 BOOTSEG = 0x07C0 /* ori 29 SYSSEG = 0x1000 /* his 30 31 #ifndef SVGA_MODE 32 #define SVGA_MODE ASK_VGA 33 #endif 34 35 #ifndef ROOT_RDONLY 36 #define ROOT_RDONLY 1 37 #endif 38 39 .set salign, 0x1000 40 .set falign, 0x200 41 42 .code16 43 .section ".bstext", "ax" 44 #ifdef CONFIG_EFI_STUB 45 # "MZ", MS-DOS header 46 .word MZ_MAGIC 47 .org 0x38 48 # 49 # Offset to the PE header. 50 # 51 .long LINUX_PE_MAGIC 52 .long pe_header 53 pe_header: 54 .long PE_MAGIC 55 56 coff_header: 57 #ifdef CONFIG_X86_32 58 .set image_file_add_flags, IMAGE_FI 59 .set pe_opt_magic, PE_OPT_MAGIC_PE3 60 .word IMAGE_FILE_MACHINE_I386 61 #else 62 .set image_file_add_flags, 0 63 .set pe_opt_magic, PE_OPT_MAGIC_PE3 64 .word IMAGE_FILE_MACHINE_AMD64 65 #endif 66 .word section_count 67 .long 0 68 .long 0 69 .long 1 70 .word section_table - optional_heade 71 .word IMAGE_FILE_EXECUTABLE_IMAGE 72 image_file_add_flags 73 IMAGE_FILE_DEBUG_STRIPPED 74 IMAGE_FILE_LINE_NUMS_STRIPPED 75 76 optional_header: 77 .word pe_opt_magic 78 .byte 0x02 79 .byte 0x14 80 81 .long ZO__data 82 83 .long ZO__end - ZO__data 84 .long 0 85 86 .long setup_size + ZO_efi_pe_entry 87 88 .long setup_size 89 #ifdef CONFIG_X86_32 90 .long 0 91 #endif 92 93 extra_header_fields: 94 #ifdef CONFIG_X86_32 95 .long 0 96 #else 97 .quad 0 98 #endif 99 .long salign 100 .long falign 101 .word 0 102 .word 0 103 .word LINUX_EFISTUB_MAJOR_VERSION 104 .word LINUX_EFISTUB_MINOR_VERSION 105 .word 0 106 .word 0 107 .long 0 108 109 .long setup_size + ZO__end 110 111 .long salign 112 .long 0 113 .word IMAGE_SUBSYSTEM_EFI_APPLICATIO 114 .word IMAGE_DLL_CHARACTERISTICS_NX_C 115 #ifdef CONFIG_X86_32 116 .long 0 117 .long 0 118 .long 0 119 .long 0 120 #else 121 .quad 0 122 .quad 0 123 .quad 0 124 .quad 0 125 #endif 126 .long 0 127 .long (section_table - .) / 8 128 129 .quad 0 130 .quad 0 131 .quad 0 132 .quad 0 133 .quad 0 134 .quad 0 135 136 # Section table 137 section_table: 138 .ascii ".setup" 139 .byte 0 140 .byte 0 141 .long pecompat_fstart - salign 142 .long salign 143 .long pecompat_fstart - salign 144 .long salign 145 146 .long 0, 0, 0 147 .long IMAGE_SCN_CNT_INITIALIZED_DATA 148 IMAGE_SCN_MEM_READ 149 IMAGE_SCN_MEM_DISCARDABLE 150 151 #ifdef CONFIG_EFI_MIXED 152 .asciz ".compat" 153 154 .long pecompat_fsize 155 .long pecompat_fstart 156 .long pecompat_fsize 157 .long pecompat_fstart 158 159 .long 0, 0, 0 160 .long IMAGE_SCN_CNT_INITIALIZED_DATA 161 IMAGE_SCN_MEM_READ 162 IMAGE_SCN_MEM_DISCARDABLE 163 164 /* 165 * Put the IA-32 machine type and the 166 * the .compat section, so loaders can 167 * modes this image supports. 168 */ 169 .pushsection ".pecompat", "a", @progbi 170 .balign salign 171 .globl pecompat_fstart 172 pecompat_fstart: 173 .byte 0x1 174 .byte 8 175 .word IMAGE_FILE_MACHINE_I386 176 .long setup_size + ZO_efi32_pe_entry 177 .byte 0x0 178 .popsection 179 #else 180 .set pecompat_fstart, setup_size 181 #endif 182 .ascii ".text" 183 .byte 0 184 .byte 0 185 .byte 0 186 .long ZO__data 187 .long setup_size 188 .long ZO__data 189 190 .long setup_size 191 .long 0 192 .long 0 193 .word 0 194 .word 0 195 .long IMAGE_SCN_CNT_CODE 196 IMAGE_SCN_MEM_READ 197 IMAGE_SCN_MEM_EXECUTE 198 199 .ascii ".data\0\0\0" 200 .long ZO__end - ZO__data 201 .long setup_size + ZO__data 202 .long ZO__edata - ZO__data 203 .long setup_size + ZO__data 204 205 .long 0, 0, 0 206 .long IMAGE_SCN_CNT_INITIALIZED_DATA 207 IMAGE_SCN_MEM_READ 208 IMAGE_SCN_MEM_WRITE 209 210 .set section_count, (. - section_ta 211 #endif /* CONFIG_EFI_STUB */ 212 213 # Kernel attributes; used by setup. T 214 # header, from the old boot sector. 215 216 .section ".header", "a" 217 .globl sentinel 218 sentinel: .byte 0xff, 0xff /* Use 219 220 .globl hdr 221 hdr: 222 .byte setup_sects - 1 223 root_flags: .word ROOT_RDONLY 224 syssize: .long ZO__edata / 16 225 ram_size: .word 0 /* Obs 226 vid_mode: .word SVGA_MODE 227 root_dev: .word 0 /* Def 228 boot_flag: .word 0xAA55 229 230 # offset 512, entry point 231 232 .globl _start 233 _start: 234 # Explicitly enter this as byt 235 # tries to generate a 3-byte j 236 # everything else to push off 237 .byte 0xeb # shor 238 .byte start_of_setup-1f 239 1: 240 241 # Part 2 of the header, from the old s 242 243 .ascii "HdrS" # head 244 .word 0x020f # head 245 # or e 246 .globl realmode_swtch 247 realmode_swtch: .word 0, 0 # defa 248 start_sys_seg: .word SYSSEG # obso 249 # in c 250 .word kernel_version-512 # p 251 # abov 252 # with 253 # chan 254 255 type_of_loader: .byte 0 # 0 me 256 # boot 257 # See 258 # assi 259 260 # flags, unused bits must be zero (RFU) bit wi 261 loadflags: 262 .byte LOADED_HIGH # The 263 264 setup_move_size: .word 0x8000 # size 265 # load 266 # to 0 267 # into 268 # load 269 # us a 270 271 code32_start: # here 272 # star 273 .long 0x100000 # 0x10 274 275 ramdisk_image: .long 0 # addr 276 # Here 277 # addr 278 # This 279 280 ramdisk_size: .long 0 # its 281 282 bootsect_kludge: 283 .long 0 # obso 284 285 heap_end_ptr: .word _end+STACK_SIZE-512 286 # (Hea 287 # spac 288 # end 289 # for 290 291 ext_loader_ver: 292 .byte 0 # Exte 293 ext_loader_type: 294 .byte 0 # Exte 295 296 cmd_line_ptr: .long 0 # (Hea 297 # If n 298 # to t 299 # The 300 # loca 301 # setu 302 # memo 303 # get 304 # gets 305 # used 306 # anyt 307 # 0x90 308 # can 309 # low 310 311 initrd_addr_max: .long 0x7fffffff 312 # (Hea 313 # The 314 # the 315 # The 316 # but 317 # poss 318 319 kernel_alignment: .long CONFIG_PHYSICAL_ALIGN 320 321 322 #ifdef CONFIG_RELOCATABLE 323 relocatable_kernel: .byte 1 324 #else 325 relocatable_kernel: .byte 0 326 #endif 327 min_alignment: .byte MIN_KERNEL_ALIGN 328 329 xloadflags: 330 #ifdef CONFIG_X86_64 331 # define XLF0 XLF_KERNEL_64 332 #else 333 # define XLF0 0 334 #endif 335 336 #if defined(CONFIG_RELOCATABLE) && defined(CON 337 /* kernel/boot_param/ramdisk could be loade 338 # define XLF1 XLF_CAN_BE_LOADED_ABOVE_4G 339 #else 340 # define XLF1 0 341 #endif 342 343 #ifdef CONFIG_EFI_HANDOVER_PROTOCOL 344 # ifdef CONFIG_EFI_MIXED 345 # define XLF23 (XLF_EFI_HANDOVER_32|XLF_EFI_H 346 # else 347 # ifdef CONFIG_X86_64 348 # define XLF23 XLF_EFI_HANDOVER_64 349 # else 350 # define XLF23 XLF_EFI_HANDOVER_32 351 # endif 352 # endif 353 #else 354 # define XLF23 0 355 #endif 356 357 #if defined(CONFIG_X86_64) && defined(CONFIG_E 358 # define XLF4 XLF_EFI_KEXEC 359 #else 360 # define XLF4 0 361 #endif 362 363 #ifdef CONFIG_X86_64 364 #ifdef CONFIG_X86_5LEVEL 365 #define XLF56 (XLF_5LEVEL|XLF_5LEVEL_ENABLED) 366 #else 367 #define XLF56 XLF_5LEVEL 368 #endif 369 #else 370 #define XLF56 0 371 #endif 372 373 .word XLF0 | XLF1 | XL 374 375 cmdline_size: .long COMMAND_LINE_SIZE-1 376 377 378 379 hardware_subarch: .long 0 380 381 382 hardware_subarch_data: .quad 0 383 384 payload_offset: .long ZO_input_data 385 payload_length: .long ZO_z_input_len 386 387 setup_data: .quad 0 388 389 390 391 pref_address: .quad LOAD_PHYSICAL_AD 392 393 # 394 # Getting to provably safe in-place decompress 395 # behaviours need to be analyzed. Here let's t 396 # a gzip-compressed kernel as example, to illu 397 # 398 # The file layout of gzip compressed kernel is 399 # 400 # magic[2] 401 # method[1] 402 # flags[1] 403 # timestamp[4] 404 # extraflags[1] 405 # os[1] 406 # compressed data blocks[N] 407 # crc[4] orig_len[4] 408 # 409 # ... resulting in +18 bytes overhead of uncom 410 # 411 # (For more information, please refer to RFC 1 412 # 413 # Files divided into blocks 414 # 1 bit (last block flag) 415 # 2 bits (block type) 416 # 417 # 1 block occurs every 32K -1 bytes or when th 418 # has been achieved. The smallest block type e 419 # 420 # stored: 421 # 32 bits length in bytes. 422 # 423 # fixed: 424 # magic fixed tree. 425 # symbols. 426 # 427 # dynamic: 428 # dynamic tree encoding. 429 # symbols. 430 # 431 # 432 # The buffer for decompression in place is the 433 # data, plus a small amount extra to keep the 434 # compressed data is placed at the end of the 435 # is placed at the start of the buffer and the 436 # where the compressed data starts. Problems w 437 # pointer overruns the input pointer. 438 # 439 # The output pointer can only overrun the inpu 440 # pointer is moving faster than the output poi 441 # triggered by data whose compressed form is l 442 # form. 443 # 444 # The worst case at the block level is a growt 445 # of 5 bytes per 32767 bytes. 446 # 447 # The worst case internal to a compressed bloc 448 # The worst case can at least be bounded by ha 449 # 32764 bytes and then all of the rest of the 450 # very last byte. 451 # 452 # All of which is enough to compute an amount 453 # to be safe. To avoid problems at the block 454 # per 32767 bytes of data is sufficient. To a 455 # block adding an extra 32767 bytes (the worst 456 # is sufficient, to ensure that in the worst c 457 # block will stop the byte before the compress 458 # To avoid problems with the compressed data's 459 # bytes are needed. Leading to the formula: 460 # 461 # extra_bytes = (uncompressed_size >> 12) + 32 462 # 463 # Adding 8 bytes per 32K is a bit excessive bu 464 # Adding 32768 instead of 32767 just makes for 465 # 466 # Above analysis is for decompressing gzip com 467 # now 6 different decompressor are supported a 468 # xz stores data in chunks and has maximum chu 469 # margin should be updated to cover all decomp 470 # need to deal with each of them separately. P 471 # the description in lib/decompressor_xxx.c fo 472 # 473 # extra_bytes = (uncompressed_size >> 12) + 65 474 # 475 # LZ4 is even worse: data that cannot be furth 476 # or one byte per 256 bytes. OTOH, we can safe 477 # the size-dependent part now grows so fast. 478 # 479 # extra_bytes = (uncompressed_size >> 8) + 655 480 # 481 # ZSTD compressed data grows by at most 3 byte 482 # byte fixed overhead but has a maximum block 483 # larger margin. 484 # 485 # extra_bytes = (uncompressed_size >> 8) + 131 486 487 #define ZO_z_extra_bytes ((ZO_z_output_ 488 #if ZO_z_output_len > ZO_z_input_len 489 # define ZO_z_extract_offset (ZO_z_output_l 490 ZO_z_input_le 491 #else 492 # define ZO_z_extract_offset ZO_z_extra_byt 493 #endif 494 495 /* 496 * The extract_offset has to be bigger than ZO 497 * the head code is running to move ZO to the 498 * overwrite the head code itself. 499 */ 500 #if (ZO__ehead - ZO_startup_32) > ZO_z_extract 501 # define ZO_z_min_extract_offset ((ZO__ehead - 502 #else 503 # define ZO_z_min_extract_offset ((ZO_z_extrac 504 #endif 505 506 #define ZO_INIT_SIZE (ZO__end - ZO_startup_ 507 508 #define VO_INIT_SIZE (VO__end - VO__text) 509 #if ZO_INIT_SIZE > VO_INIT_SIZE 510 # define INIT_SIZE ZO_INIT_SIZE 511 #else 512 # define INIT_SIZE VO_INIT_SIZE 513 #endif 514 515 .macro __handover_offset 516 #ifndef CONFIG_EFI_HANDOVER_PROTOCOL 517 .long 0 518 #elif !defined(CONFIG_X86_64) 519 .long ZO_efi32_stub_entry 520 #else 521 /* Yes, this is really how we defined 522 .long ZO_efi64_stub_entry - 523 #ifdef CONFIG_EFI_MIXED 524 .if ZO_efi32_stub_entry != 525 .error "32-bit and 64-bit EFI 526 .endif 527 #endif 528 #endif 529 .endm 530 531 init_size: .long INIT_SIZE 532 handover_offset: __handover_offset 533 kernel_info_offset: .long ZO_kernel_info 534 535 # End of setup header ######################## 536 537 .section ".entrytext", "ax" 538 start_of_setup: 539 # Force %es = %ds 540 movw %ds, %ax 541 movw %ax, %es 542 cld 543 544 # Apparently some ancient versions of LILO inv 545 # which happened to work by accident for the o 546 # pointer if %ss is invalid. Otherwise leave 547 # stack behind its own code, so we can't blind 548 549 movw %ss, %dx 550 cmpw %ax, %dx # %ds == %ss? 551 movw %sp, %dx 552 je 2f # -> assume %s 553 554 # Invalid %ss, make up a new stack 555 movw $_end, %dx 556 testb $CAN_USE_HEAP, loadflags 557 jz 1f 558 movw heap_end_ptr, %dx 559 1: addw $STACK_SIZE, %dx 560 jnc 2f 561 xorw %dx, %dx # Prevent wrap 562 563 2: # Now %dx should point to the end of o 564 andw $~3, %dx # dword align 565 jnz 3f 566 movw $0xfffc, %dx # Make sure we 567 3: movw %ax, %ss 568 movzwl %dx, %esp # Clear upper 569 sti # Now we shoul 570 571 # We will have entered with %cs = %ds+0x20, no 572 # it is on par with the other segments. 573 pushw %ds 574 pushw $6f 575 lretw 576 6: 577 578 # Check signature at end of setup 579 cmpl $0x5a5aaa55, setup_sig 580 jne setup_bad 581 582 # Zero the bss 583 movw $__bss_start, %di 584 movw $_end+3, %cx 585 xorl %eax, %eax 586 subw %di, %cx 587 shrw $2, %cx 588 rep; stosl 589 590 # Jump to C code (should not return) 591 calll main 592 593 # Setup corrupt somehow... 594 setup_bad: 595 movl $setup_corrupt, %eax 596 calll puts 597 # Fall through... 598 599 .globl die 600 .type die, @function 601 die: 602 hlt 603 jmp die 604 605 .size die, .-die 606 607 .section ".initdata", "a" 608 setup_corrupt: 609 .byte 7 610 .string "No setup signature found...\n
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.