Initial revision

This commit is contained in:
Michael Brown
2005-03-08 18:53:11 +00:00
commit 3d6123e69a
373 changed files with 114041 additions and 0 deletions
+63
View File
@@ -0,0 +1,63 @@
#!/usr/bin/perl -w
#
# Program to apply an efi header to an ia64 etherboot file.
#
# GPL Eric Biederman 2002
#
use strict;
use bytes;
main(@ARGV);
sub usage
{
my ($err) = @_;
print STDERR $err , "\n";
die "Usage $0 prrefix file bss_size\n";
}
sub main
{
my ($prefix_name, $suffix_name, $bss_size) = @_;
usage("No prefix") unless (defined($prefix_name));
usage("No suffix") unless (defined($suffix_name));
usage("No bss size") unless (defined($bss_size));
open(PREFIX, "<$prefix_name") or die "Cannot open $prefix_name";
open(SUFFIX, "<$suffix_name") or die "Cannot open $suffix_name";
$/ = undef;
my $prefix = <PREFIX>; close(PREFIX);
my $suffix = <SUFFIX>; close(SUFFIX);
# Payload sizes.
my $payload_size = length($suffix);
my $payload_bss = $bss_size;
# Update the image size
my $hdr_off = unpack("V",substr($prefix, 0x3c, 4));
my $image_size_off = 0x050 + $hdr_off;
my $img_mem_size_off = 0x0c0 + $hdr_off;
my $img_size_off = 0x0c8 + $hdr_off;
my $image_size = unpack("V", substr($prefix, $image_size_off, 4));
my $img_mem_size = unpack("V", substr($prefix, $img_mem_size_off, 4));
my $img_size = unpack("V", substr($prefix, $img_size_off, 4));
$image_size += $payload_size + $payload_bss;
$img_mem_size += $payload_size + $payload_bss;
$img_size += $payload_size;
substr($prefix, $image_size_off, 4) = pack("V", $image_size);
substr($prefix, $img_mem_size_off, 4) = pack("V", $img_mem_size);
substr($prefix, $img_size_off, 4) = pack("V", $img_size);
#print(STDERR "image_size: $image_size\n");
#print(STDERR "img_mem_size: $img_mem_size\n");
#print(STDERR "img_size: $img_size\n");
print $prefix;
print $suffix;
}
@@ -0,0 +1,198 @@
#!/usr/bin/perl -w
#
# Program to apply an unnrv2b decompressor header to an ia64 etherboot file.
#
# GPL Eric Biederman 2002
#
use strict;
use bytes;
main(@ARGV);
sub usage
{
my ($err) = @_;
print STDERR $err , "\n";
die "Usage $0 prefix file\n";
}
sub getbits
{
my ($bundle, $start, $size) = @_;
# Compute the mask
my $mask = 0xffffffff;
$mask = $mask >> (32 - $size);
# Compute the substring, and shift
my ($first, $end, $count, $shift);
$first = int($start / 8);
$end = int(($start + $size + 7)/8);
$count = $end - $first;
$shift = $start % 8;
# Compute the unpack type
my $type;
if ($count == 1) {
$type = "C"
}
elsif ($count == 2) {
$type = "v";
}
elsif (($count >= 3) && ($count <= 4)) {
$type = "V";
}
else {
die "bad count $count";
}
# Now compute the value
my $val = (unpack($type, substr($bundle, $first, $count)) >> $shift) & $mask;
# Now return the value
return $val;
}
sub putbits
{
my ($bundle, $start, $size, $val) = @_;
# Compute the mask
my $mask = 0xffffffff;
$mask >>= 32 - $size;
# Compute the substring, and shift
my ($first, $end, $count, $shift);
$first = int($start / 8);
$end = int(($start + $size + 7)/8);
$count = $end - $first;
$shift = $start % 8;
# Compute the unpack type
my $type;
if ($count == 1) {
$type = "C"
}
elsif ($count == 2) {
$type = "v";
}
elsif (($count >= 3) && ($count <= 4)) {
$type = "V";
}
else {
die "bad count $count";
}
# Adjust the mask
$mask <<= $shift;
# Now set the value, preserving the untouched bits
substr($bundle, $first, $count) =
pack($type,
((unpack($type, substr($bundle, $first, $count)) & ~$mask) |
(($val << $shift) & $mask)));
# Now return the new value;
return $bundle;
}
sub main
{
my ($prefix_name, $suffix_name) = @_;
usage("No prefix") unless (defined($prefix_name));
usage("No suffix") unless (defined($suffix_name));
open(PREFIX, "<$prefix_name") or die "Cannot open $prefix_name";
open(SUFFIX, "<$suffix_name") or die "Cannot open $suffix_name";
$/ = undef;
my $prefix = <PREFIX>; close(PREFIX);
my $suffix = <SUFFIX>; close(SUFFIX);
# Payload sizes
my $prefix_len = length($prefix);
my $suffix_len = length($suffix);
my $payload_size = $suffix_len;
my $uncompressed_offset = ($prefix_len + $suffix_len + 15) & ~15;
my $pad = $uncompressed_offset - ($prefix_len + $suffix_len);
# Itaninum instruction bundle we will be updating
# 0 - 4 template == 5
# 5 - 45 slot 0 M-Unit
# 46 - 86 slot 1 L-Unit
# 87 - 127 slot 2 X-Unit
# Itaninum instruction format
# 40 - 37 Major opcode
# ...
#
# slot 1
# 0 - 40 [41] imm-41
# 10 - 40 [31] imm-41-hi
# 0 - 9 [10] imm-41-lo
# slot 2
# 0 - 5 [6] qp
# 6 - 12 [7] r1
# 13 - 19 [7] imm-7b
# 20 [1] vc
# 21 [1] immc
# 22 - 26 [5] imm-5c
# 27 - 35 [9] imm-9d
# 36 [1] imm0
# 37 - 40 [4] major opcode
#
# major opcode should be 6
# Update the image size
my $uncompressed_offset_bundle_off = 16;
my $bundle = substr($prefix, $uncompressed_offset_bundle_off, 16);
my $template = getbits($bundle, 0, 5);
my $op1_base = 46;
my $op2_base = 87;
my $major_opcode = getbits($bundle, 37 + $op2_base, 4);
if (($template != 5) ||
($major_opcode != 6)) {
die "unknown second bundle cannot patch";
}
die "uncompressed_offset to big!\n" if ($uncompressed_offset > 0xffffffff);
my $immhi = 0;
my $immlo = $uncompressed_offset;
my $imm0 = ($immhi >> 31) & ((1 << 1) - 1);
my $imm41_hi = ($immhi >> 0) & ((1 << 31) - 1);
my $imm41_lo = ($immlo >> 22) & ((1 << 10) - 1);
my $immc = ($immlo >> 21) & ((1 << 1) - 1);
my $imm5c = ($immlo >> 16) & ((1 << 5) - 1);
my $imm9d = ($immlo >> 7) & ((1 << 9) - 1);
my $imm7b = ($immlo >> 0) & ((1 << 7) - 1);
$bundle = putbits($bundle, 10 + $op1_base, 31, $imm41_hi);
$bundle = putbits($bundle, 0 + $op1_base, 10, $imm41_lo);
$bundle = putbits($bundle, 36 + $op2_base, 1 , $imm0);
$bundle = putbits($bundle, 27 + $op2_base, 9 , $imm9d);
$bundle = putbits($bundle, 22 + $op2_base, 5 , $imm5c);
$bundle = putbits($bundle, 21 + $op2_base, 1 , $immc);
$bundle = putbits($bundle, 13 + $op2_base, 7 , $imm7b);
substr($prefix, $uncompressed_offset_bundle_off, 16) = $bundle;
#print (STDERR "prefix: $prefix_len\n");
#print (STDERR "suffix: $suffix_len\n");
#print (STDERR "pad: $pad\n");
#print (STDERR "uncompressed_offset: $uncompressed_offset\n");
print $prefix;
print $suffix;
# Pad the resulting image by a few extra bytes...
print pack("C", 0) x $pad;
}
+195
View File
@@ -0,0 +1,195 @@
#include "elf.h"
.explicit
.section ".hdrs", "a"
/* First the DOS file header */
dos_header:
.byte 'M', 'Z' /* Signature */
.short dos_program_end - dos_program /* Length of image mod 512 bytes*/
.short 0x0001 /* Length of image in 512 byte pages */ /* FIXME */
.short 0x0000 /* Number of relocation items following header */
.short (dos_header_end - dos_header)/16 /* Size of header in 16 byte paragraphs */
.short 0x0001 /* Minimum number of paragraphs needed to run image */
.short 0x0001 /* Maximum number of paragraphs program would like */
.short 0x0000 /* Initial SS */
.short 0x00b8 /* Initial SP */
.short 0x0000 /* Negative checksum of image */
.short 0x0000 /* Initial IP */
.short 0x0000 /* Initial CS */
.short 0x0010 /* Offset in EXEC of first relocation item */
.short 0x0000 /* Overlay number */
.balign 16
dos_header_end:
dos_program:
.byte 0xdb /* retf */
.balign 16
dos_program_end:
.org 0x3c
.byte pe_signature - dos_header, 0x00, 0x00, 0x00
.org 0x40 /* NOTE: set this to 0x80 for debugging, 0x40 otherwise */
pe_signature:
.byte 'P', 'E', 0, 0
coff_header:
#define IMAGE_MACHINE_IA64 0x0200
coff_machine: .short IMAGE_MACHINE_IA64
coff_nsections: .short (section_headers_end - section_headers)/40
coff_timdat: .int 1038168747 /* Sun Nov 24 12:12:27 2002 */
coff_symptr: .int 0x00000000
coff_nsyms: .int 0x00000000
coff_opthdr: .short pe_end - pe_header
#define CF_RELOC_STRIPPED 0x0001
#define CF_EXECUTABLE 0x0002
#define CF_LINE_STRIPPED 0x0004
#define CF_LOCAL_STRIPPED 0x0008
#define CF_DEBUG_STRIPPED 0x0206
coff_flags: .short CF_EXECUTABLE | CF_LINE_STRIPPED | CF_LOCAL_STRIPPED | CF_DEBUG_STRIPPED
/* Option header */
pe_header:
pe_magic: .short 0x020b /* 020b or 010b? */
pe_linker: .byte 0x02, 0x38
pe_text_size: .int _text_size
pe_data_size: .int _data_size
pe_bss_size: .int _bss_size
pe_entry: .int _start_plabel_rva
pe_text_base: .int _text_rva
pe_image_base: .quad _image_base
pe_sec_align: .int _sect_align
pe_file_align: .int _file_align
pe_os_major: .short 0
pe_os_minor: .short 0
pe_image_major: .short 0
pe_image_minro: .short 0
pe_sub_major: .short 0
pe_sub_minor: .short 0
pe_reserved: .int 0
pe_image_size: .int _image_size
pe_hdrs_size: .int _hdrs_size
pe_checksum: .int 0 /* FIXME how do I compute the checksum, unnecessary */
#define SUBSYS_EFI_APP 10
#define SUBSYS_EFI_BOOT_SERVICE_DRIVER 11
#define SUBSYS_EFI_RUNTIME_DRIVER 12
pe_subsys: .short SUBSYS_EFI_APP
pe_dll_flags: .short 0
pe_stack_res: .quad 0
pe_stack_commit:.quad 0
pe_heap_res: .quad 0
pe_heap_commit: .quad 0
pe_ld_flags: .int 0
pe_rvas: .int (rvas_end - rvas_start)/8
rvas_start:
rva_0_rva: .int 0
rva_0_size: .int 0
rva_1_rva: .int 0
rva_1_size: .int 0
rva_2_rva: .int 0
rva_2_size: .int 0
rva_3_rva: .int 0
rva_3_size: .int 0
rva_4_rva: .int 0
rva_4_size: .int 0
rva_5_rva: .int _reloc_rva
rva_5_size: .int __reloc_size
rvas_end:
pe_end:
section_headers:
#define SCN_CNT_CODE 0x00000020
#define SCN_CNT_INITIALIZED_DATA 0x00000040
#define SCN_CNT_UNINITIALIZED_DATA 0x00000080
#define SCN_MEM_DISCARDABLE 0x02000000
#define SCN_MEM_SHARED 0x10000000
#define SCN_MEM_EXECUTE 0x20000000
#define SCN_MEM_READ 0x40000000
#define SCN_MEM_WRITE 0x80000000
sec1_name: .byte '.', 'i', 'm', 'g', 0 , 0, 0, 0
sec1_virt_size: .int _img_mem_size
sec1_virt_addr: .int _img_rva
sec1_file_size: .int _img_size
sec1_file_off: .int _img_off
sec1_reloc_off: .int 0
sec1_line_off: .int 0
sec1_reloc_cnt: .short 0
sec1_line_cnt: .short 0
sec1_flags: .int SCN_CNT_CODE | SCN_CNT_INITIALIZED_DATA \
| SCN_MEM_EXECUTE | SCN_MEM_READ | SCN_MEM_WRITE
section_headers_end:
.text
.psr abi64
.psr lsb
.global _start
_start:
{
alloc r8=ar.pfs,2,0,0,0
mov gp=ip /* Get the address of _start/_text/__gp */
}
;;
add r14=@gprel(n1_desc),gp
add r15=@gprel(n2_desc),gp
add r16=@gprel(bhdr),gp
;;
st8 [r14]=in0
st8 [r15]=in1
;;
mov ar.pfs=r8
;;
mov r32=r16
;;
br.sptk.few _payload_start
.data
.global _start_plabel
.balign 16
_start_plabel:
.quad _start
.quad 0 /* I don't need a gp value... */
/* hand-crafted bhdr and parameters */
.balign 16
bhdr:
b_signature: .int 0x0E1FB007
b_size: .int bhdr_end - bhdr
b_checksum: .short 0
b_records: .short 3
/* A NOP note to 64bit align later data */
.balign 4
n0_namesz: .int 0
n0_descsz: .int 0
n0_type: .int EBN_NOP
.balign 4
n1_namesz: .int 10
n1_descsz: .int 8
n1_type: .int EB_IA64_IMAGE_HANDLE
n1_name: .asciz "Etherboot"
.balign 4
n1_desc: .quad 0
.balign 4
n2_namesz: .int 10
n2_descsz: .int 8
n2_type: .int EB_IA64_SYSTAB
n2_name: .asciz "Etherboot"
.balign 4
n2_desc: .quad 0
bhdr_end:
/* hand-craft a .reloc section for the plabel */
#define IMAGE_REL_BASED_ABS 0
#define IMAGE_REL_BASED_DIR64 10
.section ".reloc", "a"
.int _start_plabel_rva // PAGE RVA
.int 12 // Block Size (2*4+2*2)
.short (IMAGE_REL_BASED_DIR64<<12) + 0 // reloc for plabel's entry point
.short (IMAGE_REL_BASED_ABS <<12) + 0 // dummy reloc for good alignment
+91
View File
@@ -0,0 +1,91 @@
/* OUTPUT_FORMAT("binary") */
OUTPUT_FORMAT("elf64-ia64-little")
OUTPUT_ARCH(ia64)
ENTRY(_start_plabel)
_sect_align = 16; /* normally 512 */
_file_align = 16; /* normally 4096 */
/* Symbols for hardcoding the payload and _bss size, with apply_efi_prefix
* there is no need to set these, and in will get confused if these are not 0.
*/
_payload_size = 0;
_payload_bss = 0;
SECTIONS {
/* We can arbitrarily set image base to anything we want,
* but efi does not honor it, so it is a pointless exercise.
* So we just set the start address to 0.
*/
. = 0;
_link_base = . ;
_image_base = . ;
.hdrs : {
_hdrs = . ;
*(.hdrs)
. = ALIGN(_file_align) ;
_ehdrs = . ;
}
. = ALIGN(_sect_align);
.img : {
_img = . ;
_text = . ;
__gp = . ;
*(.text)
_etext = .;
. = ALIGN(16);
_data = . ;
*(.data)
_edata = .;
. = ALIGN(16);
_reloc = . ;
*(.reloc)
__ereloc = . ;
_ereloc = . ;
. = ALIGN(16);
/* . = ALIGN(_file_align) ; */
}
_payload_start = . ;
. = . + _payload_size ;
_payload_end = . ;
_eimg = . ;
. = ALIGN(_sect_align) ;
_bss = . ;
.bss : {
*(.bss)
. = . + _payload_bss;
}
_ebss = . ;
_end = . ;
/DISCARD/ : {
*(*)
}
_hdrs_size = _ehdrs - _hdrs;
_hdrs_off = 0;
_text_size = _etext - _text ;
_text_rva = _text - _image_base ;
_text_off = _text - _link_base;
_data_size = _edata - _data ;
_data_rva = _data - _image_base;
_data_off = _data - _link_base;
__reloc_size = __ereloc - _reloc ;
_reloc_size = _ereloc - _reloc ;
_reloc_rva = _reloc - _image_base;
_reloc_off = _reloc - _link_base;
_bss_size = _ebss - _bss;
_bss_rva = _bss - _image_base;
_img_size = _eimg - _img ;
_img_rva = _img - _image_base;
_img_off = _img - _link_base;
_img_mem_size = _ebss - _img;
_image_size = _ebss - _link_base ;
_start_plabel_rva = _start_plabel - _image_base;
}
+196
View File
@@ -0,0 +1,196 @@
/*
* Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
* Copyright (C) 2002 Eric Biederman
*
* This file is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* Originally this code was part of ucl the data compression library
* for upx the ``Ultimate Packer of eXecutables''.
*
* - Converted to gas assembly, and refitted to work with etherboot.
* Eric Biederman 20 Aug 2002
*
* - Converted to functional ia64 assembly (Can this get smaller?)
* Eric Biederman 5 Dec 2002
*/
.text
.globl _start
_start:
/* See where I am running, and compute gp */
{
/* Do no call alloc here as I do not know how many argument
* registers are being passed through the decompressor, and if I report
* to few the unreported registers may get stomped.
*
* Instead just explicitly get the value of ar.pfs.
*/
mov r17=0
mov r8=ar.pfs
mov gp = ip /* The linker scripts sets gp at _start */
}
{.mlx
movl r9=0x123456789abcdef0 /* Get uncompressed_offset into r9 */
}
;;
{
add r14 = @gprel(payload + 4),gp
add r15 = r9,gp
mov r16=1 /* last_m_off = 1 */
}
{
mov r20 = 0xd00
add r21 = r9,gp
br.sptk.few decompr_loop_n2b
}
/* ------------- DECOMPRESSION -------------
Input:
r8 - ar.pfs
r14 - source
r15 - dest
r16 - 1
r17 - (buffer) 0
r20 - 0xd00 (constant)
r21 - start address
Usage:
r9 - scratch register for memory copies
r18 - scratch register for getbit
r19 - scratch register for loads and stores
Output:
r2 - 0
r3 - 0
*/
getbit:
add r18 = r17,r17
;;
cmp.ne p8,p0 = r0,r18
cmp.leu p6,p7 = r18,r17
;;
mov r17 = r18
(p8) br.cond.sptk.few getbit_end
/* Do a unaligned 64bit load */
;;
ld1 r17 = [r14],1
;;
ld1 r18 = [r14],1
;;
dep r17 = r18,r17,8,8
ld1 r18 = [r14],1
;;
dep r17 = r18,r17,16,8
ld1 r18 = [r14],1
;;
dep r17 = r18,r17,24,8
ld1 r18 = [r14],1
;;
dep r17 = r18,r17,32,8
ld1 r18 = [r14],1
;;
dep r17 = r18,r17,40,8
ld1 r18 = [r14],1
;;
dep r17 = r18,r17,48,8
ld1 r18 = [r14],1
;;
dep r17 = r18,r17,56,8
;;
add r18 = r17,r17,1
;;
cmp.leu p6,p7=r18,r17
;;
mov r17=r18
;;
getbit_end:
br.ret.sptk.few b6
decompr_literals_n2b:
ld1 r19 = [r14],1
;;
st1 [r15] = r19,1
;;
decompr_loop_n2b:
br.call.sptk.few b6 = getbit
;;
(p6) br.cond.sptk.few decompr_literals_n2b
(p7) add r2 = 1,r0 /* m_off = 1 */
;;
loop1_n2b:
br.call.sptk.few b6 = getbit
;;
(p6) add r2 = r2,r2,1 /* m_off = m_off*2 + getbit() */
(p7) add r2 = r2,r2
br.call.sptk.few b6 = getbit
;;
(p7) br.cond.sptk.few loop1_n2b /* while(!getbit()) */
;;
mov r3 = r0
cmp.eq p6,p0 = 2,r2
add r2 = -3,r2
(p6) br.cond.sptk.few decompr_ebpeax_n2b /* if (m_off == 2) goto decompr_ebpeax_n2b ? */
;;
ld1 r19 = [r14],1
shl r2 = r2,8
;;
dep r2 = r19,r2,0,8 /* m_off = (m_off - 3)*256 + src[ilen++] */
;;
cmp4.eq p6,p0 = -1,r2 /* if (m_off == 0xffffffff) goto decomp_end_n2b */
;;
(p6) br.cond.sptk.few decompr_end_n2b
mov r16 = r2 /* last_m_off = m_off */
;;
decompr_ebpeax_n2b:
br.call.sptk.few b6 = getbit
;;
(p6) add r3 = r3,r3,1 /* m_len = getbit() */
(p7) add r3 = r3,r3
br.call.sptk.few b6 = getbit
;;
(p6) add r3 = r3,r3,1 /* m_len = m_len*2 + getbit()) */
(p7) add r3 = r3,r3
;;
cmp.ne p6,p0 = r0,r3
(p6) br.cond.sptk.few decompr_got_mlen_n2b /* if (m_len == 0) goto decompr_got_mlen_n2b */
add r3 = 1,r3 /* m_len++ */
;;
loop2_n2b:
br.call.sptk.few b6 = getbit
;;
(p6) add r3 = r3,r3,1 /* m_len = m_len*2 + getbit() */
(p7) add r3 = r3,r3
br.call.sptk.few b6 = getbit
;;
(p7) br.cond.sptk.few loop2_n2b /* while(!getbit()) */
add r3 = 2, r3 /* m_len += 2 */
;;
decompr_got_mlen_n2b:
cmp.gtu p6,p7 = r16, r20
;;
(p6) add r3 = 2, r3 /* m_len = m_len + 1 + (last_m_off > 0xd00) */
(p7) add r3 = 1, r3
sub r9 = r15, r16,1 /* m_pos = dst + olen - last_m_off - 1 */
;;
1:
ld1 r19 = [r9],1
add r3 = -1,r3
;;
st1 [r15] = r19,1 /* dst[olen++] = *m_pos++ while(m_len > 0) */
cmp.ne p6,p0 = r0,r3
(p6) br.cond.sptk.few 1b
;;
br.cond.sptk.few decompr_loop_n2b
decompr_end_n2b:
/* Branch to the start address */
mov ar.pfs=r8
;;
mov b6 = r21
;;
br.sptk.few b6
payload:
+28
View File
@@ -0,0 +1,28 @@
OUTPUT_FORMAT("elf64-ia64-little")
OUTPUT_ARCH(ia64)
ENTRY(_start)
SECTIONS {
. = 0;
__gp = .;
_text = . ;
.text : {
*(.text)
}
/DISCARD/ : {
*(.comment)
*(.note)
*(.hash)
*(.data)
*(.sbss)
*(.bss)
*(.dynstr)
*(.dynsym)
*(.IA_64.unwind)
*(.IA_64.unwind_info)
*(.IA64_unwind)
*(.IA64_unwind_info)
*(.dynamic)
}
}