ppc/uImage: Find new kernel load_addr if the default addr is not available

If the kernel cannot be loaded at the default load_addr, provided
by the image, we should try finding a free area using locate_hole().
This is usually applicable for the CRASH case, where the memory should
be located in the reserved region.

Without this patch, sometime the kernel fails to load for uImage formatted
relocatable kernel images.

Signed-off-by: Suzuki K. Poulose <suzuki@in.ibm.com>
Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Cc: Matthew McClintock <msm@freescale.com>
Signed-off-by: Simon Horman <horms@verge.net.au>
vmcore-dmesg: Determine correct machine pointer size

A 32bit arch can prepare ELF64 headers. For example for PAE case to
preresent file offsets 64bit but data size at the offset still remains
32bit. If we just base our decision based on EI_CLASS, then we will try
to read 64bit data from file and can run into various issues.

We ran into following issue when we tried to run vmcore-dmesg on a 32bit
PAE system vmcore which had 64bit elf headers.

No program header covering vaddr 0xc0a6a688c0b89100found kexec bug?

Basically we try to read value of log_buf variable from address
log_buf_vaddr. We read in 64bit value and then pass that value again
to vaddr_to_offset() in an attempt to get to actual log_buf start
and get error message.

So determine the machine pointer size based on ELF class and arch and read
the bytes from file accordingly.

v2: Fixed the code as per suggestion from Eric.

Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
Acked-by: "Eric W. Biederman" <ebiederm@xmission.com>
Acked-by: Dave Young <dyoung@redhat.com>
Signed-off-by: Simon Horman <horms@verge.net.au>
kexec x86_64: Make purgatory relocatable anywhere in the 64bit address space.

Remove kexec/arch/i386/compat_x6_64.S
purgatory/arch/i386/linux-entry16.S and purgatory/arch/i386/entry16.S
Those were early attempts at entry32-16.S that should have been
deleted long ago.

Strip the purgatory code of debug symbols.  There is no need to carry
debug symbols we will never use around in /sbin/kexec.

On x86_64 use -mcmodel=large so that the code is built without
any 32bit assumptions.  -mcmodel=medium and -mcmodel=small
result int code that has 32bit relocations against variables
that can live anywhere in the address space

Modify the assembly in entry64.S and setup-x86_64.S to use %rip
relative addressing of variables so no relocates are emitted.

Modify entry64-32.S so that it does not have any relocations that can
not be processed when purgatory is loaded above 4G.  entry64-32.S
jumps to a 32bit entry point and can not itself be used above 4G so
these changes merely prevent it from being a problem in the other case.

eip is modifed to be a 64bit value of which only the low 32bits are
exported outside of entry64-32.S

The long mode exit code is modified to run with a %cs value whose
base address is the address of the symbol entry32.  From there
all of the 32bit code in entry64-32.S can read variables by reading
them through %cs.  Until the final jump to the the target address
which is made a far jump reloading %cs and the intstruction pointer.

Modify entry32-16.S and entry32-16-debug.S to be position independent
32bit code.  At their start make a short call to push the current value
of %eip on the stack and pop it off.  Allowing the calculation of the
address of entry16 which the code has always kept in %ebx.

Update the pointer to the gdt in the gdt so that lgdt will work.

Modify the instructions in entry32-16.S and entry32-16-debug.S so
that the 32bit code uses offsets from %ebx which points at entry16.

Tested-by: Yinghai Lu <yinghai@kernel.org>
Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
Signed-off-by: Simon Horman <horms@verge.net.au>
kexec-tools 2.0.4-rc1

Signed-off-by: Simon Horman <horms@verge.net.au>
kexec-tools 2.0.4-rc1.git

Add .git to version so it doesn't look like a release.
This is just so when people build code from git it can
be identified as such from the version string.

Signed-off-by: Simon Horman <horms@verge.net.au>
fs2dt: fix basename string compares

basename is initialized as

  basename = strrchr(pathname,'/') + 1;

and does hence not contain the leading slash character.

Signed-off-by: Daniel Mack <zonque@gmail.com>
Signed-off-by: Simon Horman <horms@verge.net.au>
fs2dt: check for initrd_size != 0 when adding initrd entries

This prevents the creation of chosen/linux,initrd-{start,stop} entries
with zero length.

Signed-off-by: Daniel Mack <zonque@gmail.com>
Signed-off-by: Simon Horman <horms@verge.net.au>
kexec: Fix compile errors in crashdump-mips.c

When building configured as '--host=mips64-octeon-linux-gnu' using
GCC-4.7.0 there are two compile errors, fix them.

Signed-off-by: David Daney <david.daney@cavium.com>
Signed-off-by: Simon Horman <horms@verge.net.au>
kexec: Teach configure to find the strip binary.

For some reason my version of the Makefile generated by configure
included the line STRIP=strp.  Rerunning configure from a fresh slate
did not regenerate that line so I don't know how it got there.  So add
the code to Makefile.in and configure.ac to autodetect the strip binary.

This is needed so that we can remove from purgatory all of the
relocations to sections that are not needed at runtime, by stripping
out those sections.

Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
[horms@verge.net.au: Omitted white-space only change to
                     purgatory/arch/x86_64/Makefile]
Signed-off-by: Simon Horman <horms@verge.net.au>
ppc: exec stack fix

execstack shows ppc kexec has an executable stack,
this leaves it vulnerable to buffer overflows.

Fix it by adding ASFLAGS --noexecstack

Tested on PowerMac G4 Macmini:
Without the patch:
dave@darkstar:~/kexec-tools$ execstack build/sbin/kexec
X build/sbin/kexec

With the patch:
dave@darkstar:~/kexec-tools$ execstack build/sbin/kexec
- build/sbin/kexec

Signed-off-by: Dave Young <dyoung@redhat.com>
Signed-off-by: Simon Horman <horms@verge.net.au>
Fix xen_cpuid() inline asm to not clobber stack's red zone

Port xen-unstable changeset 24344:72f4e4cb7440 to kexec-tools:

  Pushing stuff onto the stack on x86-64 when we do not specify
  -mno-red-zone is unsafe. Since the complicated asm is due to register
  pressure on i386, we simply implement an all-new simpler alternative
  for x86-64.

Signed-off-by: Olaf Hering <olaf@aepfle.de>
Signed-off-by: Simon Horman <horms@verge.net.au>
fix comment typo in locate_hole

Signed-off-by: Olaf Hering <olaf@aepfle.de>
Signed-off-by: Simon Horman <horms@verge.net.au>
fix comment typo in do_bzImage_load on x86

Signed-off-by: Olaf Hering <olaf@aepfle.de>
Signed-off-by: Simon Horman <horms@verge.net.au>
fixed strncat size argument on ppc64

Signed-off-by: Olaf Hering <olaf@aepfle.de>
Signed-off-by: Simon Horman <horms@verge.net.au>
fix message and indenting in putnode in ppc64

Signed-off-by: Olaf Hering <olaf@aepfle.de>
Signed-off-by: Simon Horman <horms@verge.net.au>
kexec-zImage-arm: add code to support --command-line along with --dtb

If --dtb is called together with --command-line, we need to modify the
binary dtb buffer. Luckily, we have libfdt functions available, so this
is straight forward.

Signed-off-by: Daniel Mack <zonque@gmail.com>
Tested-by: Sven Neumann <s.neumann@raumfeld.com>
Signed-off-by: Simon Horman <horms@verge.net.au>
kexec-elf-ppc: Fix crashkernel region overflow check

Signed-off-by: Anders Hedlund <anders.j.hedlund@gmail.com>
Signed-off-by: Simon Horman <horms@verge.net.au>
kexec-zImage-arm: simply cmdline-related DTB resizing

When resizing a dtb to add the command-line, only resize the DTB once,
rather than once to add the /chosen node, and once to add the bootargs
property.

Also, simply add 1K of overhead (beyond strlen(cmdline)) to the buffer,
to avoid requiring precise knowledge of the size impact of the requested
FTB changes. In particular, some padding is performed when setting
property values, which was not accounted for in the current code, which
caused failures to set the bootargs values in some cases.

Cc: Daniel Mack <zonque@gmail.com>
Signed-off-by: Stephen Warren <swarren@nvidia.com>
Signed-off-by: Simon Horman <horms@verge.net.au>
die: Use const for fmt

Signed-off-by: Simon Horman <horms@verge.net.au>
build: Use cross compiler's strip if available

Signed-off-by: Simon Horman <horms@verge.net.au>