All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/7] RFC: EFI stub for ARM
@ 2013-08-02 21:29 ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-02 21:29 UTC (permalink / raw)
  To: linux-kernel, linux-efi, linux-arm-kernel, matt.fleming, linux
  Cc: leif.lindholm, Roy Franz

This RFC patch series adds EFI stub support for the ARM architecture.
Some code that was previously only used by x86/x86_64 is now shared
and has been made more general.  The stub for ARM is implemented in
a similar manner to x86 in that it is a shim layer between EFI and
the normal zImage/bzImage boot process, and that an image with the
stub configured is bootable as both a zImage and EFI application.

This patchset depends on a separately submitted bugfix to the
EFI stub: "correct call to free_pages"
http://git.kernel.org/cgit/linux/kernel/git/tip/tip.git/commit/?id=df981edcb9bce00b9c5e4f3cc33f3f98bc9a2394

Roy Franz (7):
  EFI stub documentation updates
  Move common EFI stub code from x86 arch code to common location
  Change EFI helper APIs to be more flexible
  Add proper definitions for some EFI function pointers.
  Add strstr to compressed string.c for ARM.
  Add EFI stub for ARM
  Add config EFI_STUB for ARM

 Documentation/efi-stub.txt             |   78 +++++
 Documentation/x86/efi-stub.txt         |   65 ----
 arch/arm/Kconfig                       |   10 +
 arch/arm/boot/compressed/Makefile      |   18 +-
 arch/arm/boot/compressed/efi-header.S  |  114 +++++++
 arch/arm/boot/compressed/efi-stub.c    |  514 ++++++++++++++++++++++++++++++++
 arch/arm/boot/compressed/head.S        |   90 +++++-
 arch/arm/boot/compressed/string.c      |   21 ++
 arch/x86/Kconfig                       |    2 +-
 arch/x86/boot/compressed/eboot.c       |  488 ++----------------------------
 arch/x86/boot/compressed/eboot.h       |    6 -
 drivers/firmware/efi/efi-stub-helper.c |  506 +++++++++++++++++++++++++++++++
 include/linux/efi.h                    |   67 ++++-
 13 files changed, 1428 insertions(+), 551 deletions(-)
 create mode 100644 Documentation/efi-stub.txt
 delete mode 100644 Documentation/x86/efi-stub.txt
 create mode 100644 arch/arm/boot/compressed/efi-header.S
 create mode 100644 arch/arm/boot/compressed/efi-stub.c
 create mode 100644 drivers/firmware/efi/efi-stub-helper.c

-- 
1.7.10.4


^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH 0/7] RFC: EFI stub for ARM
@ 2013-08-02 21:29 ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-02 21:29 UTC (permalink / raw)
  To: linux-kernel, linux-efi, linux-arm-kernel, matt.fleming, linux
  Cc: Roy Franz, leif.lindholm

This RFC patch series adds EFI stub support for the ARM architecture.
Some code that was previously only used by x86/x86_64 is now shared
and has been made more general.  The stub for ARM is implemented in
a similar manner to x86 in that it is a shim layer between EFI and
the normal zImage/bzImage boot process, and that an image with the
stub configured is bootable as both a zImage and EFI application.

This patchset depends on a separately submitted bugfix to the
EFI stub: "correct call to free_pages"
http://git.kernel.org/cgit/linux/kernel/git/tip/tip.git/commit/?id=df981edcb9bce00b9c5e4f3cc33f3f98bc9a2394

Roy Franz (7):
  EFI stub documentation updates
  Move common EFI stub code from x86 arch code to common location
  Change EFI helper APIs to be more flexible
  Add proper definitions for some EFI function pointers.
  Add strstr to compressed string.c for ARM.
  Add EFI stub for ARM
  Add config EFI_STUB for ARM

 Documentation/efi-stub.txt             |   78 +++++
 Documentation/x86/efi-stub.txt         |   65 ----
 arch/arm/Kconfig                       |   10 +
 arch/arm/boot/compressed/Makefile      |   18 +-
 arch/arm/boot/compressed/efi-header.S  |  114 +++++++
 arch/arm/boot/compressed/efi-stub.c    |  514 ++++++++++++++++++++++++++++++++
 arch/arm/boot/compressed/head.S        |   90 +++++-
 arch/arm/boot/compressed/string.c      |   21 ++
 arch/x86/Kconfig                       |    2 +-
 arch/x86/boot/compressed/eboot.c       |  488 ++----------------------------
 arch/x86/boot/compressed/eboot.h       |    6 -
 drivers/firmware/efi/efi-stub-helper.c |  506 +++++++++++++++++++++++++++++++
 include/linux/efi.h                    |   67 ++++-
 13 files changed, 1428 insertions(+), 551 deletions(-)
 create mode 100644 Documentation/efi-stub.txt
 delete mode 100644 Documentation/x86/efi-stub.txt
 create mode 100644 arch/arm/boot/compressed/efi-header.S
 create mode 100644 arch/arm/boot/compressed/efi-stub.c
 create mode 100644 drivers/firmware/efi/efi-stub-helper.c

-- 
1.7.10.4

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH 0/7] RFC: EFI stub for ARM
@ 2013-08-02 21:29 ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-02 21:29 UTC (permalink / raw)
  To: linux-arm-kernel

This RFC patch series adds EFI stub support for the ARM architecture.
Some code that was previously only used by x86/x86_64 is now shared
and has been made more general.  The stub for ARM is implemented in
a similar manner to x86 in that it is a shim layer between EFI and
the normal zImage/bzImage boot process, and that an image with the
stub configured is bootable as both a zImage and EFI application.

This patchset depends on a separately submitted bugfix to the
EFI stub: "correct call to free_pages"
http://git.kernel.org/cgit/linux/kernel/git/tip/tip.git/commit/?id=df981edcb9bce00b9c5e4f3cc33f3f98bc9a2394

Roy Franz (7):
  EFI stub documentation updates
  Move common EFI stub code from x86 arch code to common location
  Change EFI helper APIs to be more flexible
  Add proper definitions for some EFI function pointers.
  Add strstr to compressed string.c for ARM.
  Add EFI stub for ARM
  Add config EFI_STUB for ARM

 Documentation/efi-stub.txt             |   78 +++++
 Documentation/x86/efi-stub.txt         |   65 ----
 arch/arm/Kconfig                       |   10 +
 arch/arm/boot/compressed/Makefile      |   18 +-
 arch/arm/boot/compressed/efi-header.S  |  114 +++++++
 arch/arm/boot/compressed/efi-stub.c    |  514 ++++++++++++++++++++++++++++++++
 arch/arm/boot/compressed/head.S        |   90 +++++-
 arch/arm/boot/compressed/string.c      |   21 ++
 arch/x86/Kconfig                       |    2 +-
 arch/x86/boot/compressed/eboot.c       |  488 ++----------------------------
 arch/x86/boot/compressed/eboot.h       |    6 -
 drivers/firmware/efi/efi-stub-helper.c |  506 +++++++++++++++++++++++++++++++
 include/linux/efi.h                    |   67 ++++-
 13 files changed, 1428 insertions(+), 551 deletions(-)
 create mode 100644 Documentation/efi-stub.txt
 delete mode 100644 Documentation/x86/efi-stub.txt
 create mode 100644 arch/arm/boot/compressed/efi-header.S
 create mode 100644 arch/arm/boot/compressed/efi-stub.c
 create mode 100644 drivers/firmware/efi/efi-stub-helper.c

-- 
1.7.10.4

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH 1/7] EFI stub documentation updates
  2013-08-02 21:29 ` Roy Franz
  (?)
@ 2013-08-02 21:29   ` Roy Franz
  -1 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-02 21:29 UTC (permalink / raw)
  To: linux-kernel, linux-efi, linux-arm-kernel, matt.fleming, linux
  Cc: leif.lindholm, Roy Franz

The ARM kernel also has an EFI stub which works largely the same way
as the x86 stub, so move the documentation out of x86 directory and
update to reflect that it is generic, and add ARM specific text.

Signed-off-by: Roy Franz <roy.franz@linaro.org>
---
 Documentation/efi-stub.txt     |   78 ++++++++++++++++++++++++++++++++++++++++
 Documentation/x86/efi-stub.txt |   65 ---------------------------------
 arch/x86/Kconfig               |    2 +-
 3 files changed, 79 insertions(+), 66 deletions(-)
 create mode 100644 Documentation/efi-stub.txt
 delete mode 100644 Documentation/x86/efi-stub.txt

diff --git a/Documentation/efi-stub.txt b/Documentation/efi-stub.txt
new file mode 100644
index 0000000..7837df1
--- /dev/null
+++ b/Documentation/efi-stub.txt
@@ -0,0 +1,78 @@
+			  The EFI Boot Stub
+		     ---------------------------
+
+On the x86 and ARM platforms, a bzImage can masquerade as a PE/COFF image,
+thereby convincing EFI firmware loaders to load it as an EFI
+executable. The code that modifies the bzImage header, along with the
+EFI-specific entry point that the firmware loader jumps to are
+collectively known as the "EFI boot stub", and live in
+arch/x86/boot/header.S and arch/x86/boot/compressed/eboot.c,
+respectively.  For ARM the EFI stub is implemented in
+arch/arm/boot/compressed/efi-header.S and
+arch/arm/boot/compressed/efi-stub.c.  EFI stub code that is shared
+between architectures is in drivers/firmware/efi/efi-stub-helper.c.
+
+By using the EFI boot stub it's possible to boot a Linux kernel
+without the use of a conventional EFI boot loader, such as grub or
+elilo. Since the EFI boot stub performs the jobs of a boot loader, in
+a certain sense it *IS* the boot loader.
+
+The EFI boot stub is enabled with the CONFIG_EFI_STUB kernel option.
+
+
+**** How to install bzImage.efi
+
+The bzImage located in arch/x86/boot/bzImage must be copied to the EFI
+System Partiion (ESP) and renamed with the extension ".efi". Without
+the extension the EFI firmware loader will refuse to execute it. It's
+not possible to execute bzImage.efi from the usual Linux file systems
+because EFI firmware doesn't have support for them.  For ARM the
+arch/arm/boot/zImage should be copied to the system partition, and it
+may not need to be renamed.
+
+
+**** Passing kernel parameters from the EFI shell
+
+Arguments to the kernel can be passed after bzImage.efi, e.g.
+
+	fs0:> bzImage.efi console=ttyS0 root=/dev/sda4
+
+
+**** The "initrd=" option
+
+Like most boot loaders, the EFI stub allows the user to specify
+multiple initrd files using the "initrd=" option. This is the only EFI
+stub-specific command line parameter, everything else is passed to the
+kernel when it boots.
+
+The path to the initrd file must be an absolute path from the
+beginning of the ESP, relative path names do not work. Also, the path
+is an EFI-style path and directory elements must be separated with
+backslashes (\). For example, given the following directory layout,
+
+fs0:>
+	Kernels\
+			bzImage.efi
+			initrd-large.img
+
+	Ramdisks\
+			initrd-small.img
+			initrd-medium.img
+
+to boot with the initrd-large.img file if the current working
+directory is fs0:\Kernels, the following command must be used,
+
+	fs0:\Kernels> bzImage.efi initrd=\Kernels\initrd-large.img
+
+Notice how bzImage.efi can be specified with a relative path. That's
+because the image we're executing is interpreted by the EFI shell,
+which understands relative paths, whereas the rest of the command line
+is passed to bzImage.efi.
+
+
+**** The "dtb=" option
+
+For the ARM architecture, we also need to be able to provide a device
+tree to the kernel.  This is done with the "dtb=" command line option,
+and is process in the same manner as the "initrd=" option that is described
+above.
diff --git a/Documentation/x86/efi-stub.txt b/Documentation/x86/efi-stub.txt
deleted file mode 100644
index 44e6bb6..0000000
--- a/Documentation/x86/efi-stub.txt
+++ /dev/null
@@ -1,65 +0,0 @@
-			  The EFI Boot Stub
-		     ---------------------------
-
-On the x86 platform, a bzImage can masquerade as a PE/COFF image,
-thereby convincing EFI firmware loaders to load it as an EFI
-executable. The code that modifies the bzImage header, along with the
-EFI-specific entry point that the firmware loader jumps to are
-collectively known as the "EFI boot stub", and live in
-arch/x86/boot/header.S and arch/x86/boot/compressed/eboot.c,
-respectively.
-
-By using the EFI boot stub it's possible to boot a Linux kernel
-without the use of a conventional EFI boot loader, such as grub or
-elilo. Since the EFI boot stub performs the jobs of a boot loader, in
-a certain sense it *IS* the boot loader.
-
-The EFI boot stub is enabled with the CONFIG_EFI_STUB kernel option.
-
-
-**** How to install bzImage.efi
-
-The bzImage located in arch/x86/boot/bzImage must be copied to the EFI
-System Partiion (ESP) and renamed with the extension ".efi". Without
-the extension the EFI firmware loader will refuse to execute it. It's
-not possible to execute bzImage.efi from the usual Linux file systems
-because EFI firmware doesn't have support for them.
-
-
-**** Passing kernel parameters from the EFI shell
-
-Arguments to the kernel can be passed after bzImage.efi, e.g.
-
-	fs0:> bzImage.efi console=ttyS0 root=/dev/sda4
-
-
-**** The "initrd=" option
-
-Like most boot loaders, the EFI stub allows the user to specify
-multiple initrd files using the "initrd=" option. This is the only EFI
-stub-specific command line parameter, everything else is passed to the
-kernel when it boots.
-
-The path to the initrd file must be an absolute path from the
-beginning of the ESP, relative path names do not work. Also, the path
-is an EFI-style path and directory elements must be separated with
-backslashes (\). For example, given the following directory layout,
-
-fs0:>
-	Kernels\
-			bzImage.efi
-			initrd-large.img
-
-	Ramdisks\
-			initrd-small.img
-			initrd-medium.img
-
-to boot with the initrd-large.img file if the current working
-directory is fs0:\Kernels, the following command must be used,
-
-	fs0:\Kernels> bzImage.efi initrd=\Kernels\initrd-large.img
-
-Notice how bzImage.efi can be specified with a relative path. That's
-because the image we're executing is interpreted by the EFI shell,
-which understands relative paths, whereas the rest of the command line
-is passed to bzImage.efi.
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index b32ebf9..ec65b51 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -1579,7 +1579,7 @@ config EFI_STUB
           This kernel feature allows a bzImage to be loaded directly
 	  by EFI firmware without the use of a bootloader.
 
-	  See Documentation/x86/efi-stub.txt for more information.
+	  See Documentation/efi-stub.txt for more information.
 
 config SECCOMP
 	def_bool y
-- 
1.7.10.4


^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH 1/7] EFI stub documentation updates
@ 2013-08-02 21:29   ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-02 21:29 UTC (permalink / raw)
  To: linux-kernel, linux-efi, linux-arm-kernel, matt.fleming, linux
  Cc: Roy Franz, leif.lindholm

The ARM kernel also has an EFI stub which works largely the same way
as the x86 stub, so move the documentation out of x86 directory and
update to reflect that it is generic, and add ARM specific text.

Signed-off-by: Roy Franz <roy.franz@linaro.org>
---
 Documentation/efi-stub.txt     |   78 ++++++++++++++++++++++++++++++++++++++++
 Documentation/x86/efi-stub.txt |   65 ---------------------------------
 arch/x86/Kconfig               |    2 +-
 3 files changed, 79 insertions(+), 66 deletions(-)
 create mode 100644 Documentation/efi-stub.txt
 delete mode 100644 Documentation/x86/efi-stub.txt

diff --git a/Documentation/efi-stub.txt b/Documentation/efi-stub.txt
new file mode 100644
index 0000000..7837df1
--- /dev/null
+++ b/Documentation/efi-stub.txt
@@ -0,0 +1,78 @@
+			  The EFI Boot Stub
+		     ---------------------------
+
+On the x86 and ARM platforms, a bzImage can masquerade as a PE/COFF image,
+thereby convincing EFI firmware loaders to load it as an EFI
+executable. The code that modifies the bzImage header, along with the
+EFI-specific entry point that the firmware loader jumps to are
+collectively known as the "EFI boot stub", and live in
+arch/x86/boot/header.S and arch/x86/boot/compressed/eboot.c,
+respectively.  For ARM the EFI stub is implemented in
+arch/arm/boot/compressed/efi-header.S and
+arch/arm/boot/compressed/efi-stub.c.  EFI stub code that is shared
+between architectures is in drivers/firmware/efi/efi-stub-helper.c.
+
+By using the EFI boot stub it's possible to boot a Linux kernel
+without the use of a conventional EFI boot loader, such as grub or
+elilo. Since the EFI boot stub performs the jobs of a boot loader, in
+a certain sense it *IS* the boot loader.
+
+The EFI boot stub is enabled with the CONFIG_EFI_STUB kernel option.
+
+
+**** How to install bzImage.efi
+
+The bzImage located in arch/x86/boot/bzImage must be copied to the EFI
+System Partiion (ESP) and renamed with the extension ".efi". Without
+the extension the EFI firmware loader will refuse to execute it. It's
+not possible to execute bzImage.efi from the usual Linux file systems
+because EFI firmware doesn't have support for them.  For ARM the
+arch/arm/boot/zImage should be copied to the system partition, and it
+may not need to be renamed.
+
+
+**** Passing kernel parameters from the EFI shell
+
+Arguments to the kernel can be passed after bzImage.efi, e.g.
+
+	fs0:> bzImage.efi console=ttyS0 root=/dev/sda4
+
+
+**** The "initrd=" option
+
+Like most boot loaders, the EFI stub allows the user to specify
+multiple initrd files using the "initrd=" option. This is the only EFI
+stub-specific command line parameter, everything else is passed to the
+kernel when it boots.
+
+The path to the initrd file must be an absolute path from the
+beginning of the ESP, relative path names do not work. Also, the path
+is an EFI-style path and directory elements must be separated with
+backslashes (\). For example, given the following directory layout,
+
+fs0:>
+	Kernels\
+			bzImage.efi
+			initrd-large.img
+
+	Ramdisks\
+			initrd-small.img
+			initrd-medium.img
+
+to boot with the initrd-large.img file if the current working
+directory is fs0:\Kernels, the following command must be used,
+
+	fs0:\Kernels> bzImage.efi initrd=\Kernels\initrd-large.img
+
+Notice how bzImage.efi can be specified with a relative path. That's
+because the image we're executing is interpreted by the EFI shell,
+which understands relative paths, whereas the rest of the command line
+is passed to bzImage.efi.
+
+
+**** The "dtb=" option
+
+For the ARM architecture, we also need to be able to provide a device
+tree to the kernel.  This is done with the "dtb=" command line option,
+and is process in the same manner as the "initrd=" option that is described
+above.
diff --git a/Documentation/x86/efi-stub.txt b/Documentation/x86/efi-stub.txt
deleted file mode 100644
index 44e6bb6..0000000
--- a/Documentation/x86/efi-stub.txt
+++ /dev/null
@@ -1,65 +0,0 @@
-			  The EFI Boot Stub
-		     ---------------------------
-
-On the x86 platform, a bzImage can masquerade as a PE/COFF image,
-thereby convincing EFI firmware loaders to load it as an EFI
-executable. The code that modifies the bzImage header, along with the
-EFI-specific entry point that the firmware loader jumps to are
-collectively known as the "EFI boot stub", and live in
-arch/x86/boot/header.S and arch/x86/boot/compressed/eboot.c,
-respectively.
-
-By using the EFI boot stub it's possible to boot a Linux kernel
-without the use of a conventional EFI boot loader, such as grub or
-elilo. Since the EFI boot stub performs the jobs of a boot loader, in
-a certain sense it *IS* the boot loader.
-
-The EFI boot stub is enabled with the CONFIG_EFI_STUB kernel option.
-
-
-**** How to install bzImage.efi
-
-The bzImage located in arch/x86/boot/bzImage must be copied to the EFI
-System Partiion (ESP) and renamed with the extension ".efi". Without
-the extension the EFI firmware loader will refuse to execute it. It's
-not possible to execute bzImage.efi from the usual Linux file systems
-because EFI firmware doesn't have support for them.
-
-
-**** Passing kernel parameters from the EFI shell
-
-Arguments to the kernel can be passed after bzImage.efi, e.g.
-
-	fs0:> bzImage.efi console=ttyS0 root=/dev/sda4
-
-
-**** The "initrd=" option
-
-Like most boot loaders, the EFI stub allows the user to specify
-multiple initrd files using the "initrd=" option. This is the only EFI
-stub-specific command line parameter, everything else is passed to the
-kernel when it boots.
-
-The path to the initrd file must be an absolute path from the
-beginning of the ESP, relative path names do not work. Also, the path
-is an EFI-style path and directory elements must be separated with
-backslashes (\). For example, given the following directory layout,
-
-fs0:>
-	Kernels\
-			bzImage.efi
-			initrd-large.img
-
-	Ramdisks\
-			initrd-small.img
-			initrd-medium.img
-
-to boot with the initrd-large.img file if the current working
-directory is fs0:\Kernels, the following command must be used,
-
-	fs0:\Kernels> bzImage.efi initrd=\Kernels\initrd-large.img
-
-Notice how bzImage.efi can be specified with a relative path. That's
-because the image we're executing is interpreted by the EFI shell,
-which understands relative paths, whereas the rest of the command line
-is passed to bzImage.efi.
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index b32ebf9..ec65b51 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -1579,7 +1579,7 @@ config EFI_STUB
           This kernel feature allows a bzImage to be loaded directly
 	  by EFI firmware without the use of a bootloader.
 
-	  See Documentation/x86/efi-stub.txt for more information.
+	  See Documentation/efi-stub.txt for more information.
 
 config SECCOMP
 	def_bool y
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH 1/7] EFI stub documentation updates
@ 2013-08-02 21:29   ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-02 21:29 UTC (permalink / raw)
  To: linux-arm-kernel

The ARM kernel also has an EFI stub which works largely the same way
as the x86 stub, so move the documentation out of x86 directory and
update to reflect that it is generic, and add ARM specific text.

Signed-off-by: Roy Franz <roy.franz@linaro.org>
---
 Documentation/efi-stub.txt     |   78 ++++++++++++++++++++++++++++++++++++++++
 Documentation/x86/efi-stub.txt |   65 ---------------------------------
 arch/x86/Kconfig               |    2 +-
 3 files changed, 79 insertions(+), 66 deletions(-)
 create mode 100644 Documentation/efi-stub.txt
 delete mode 100644 Documentation/x86/efi-stub.txt

diff --git a/Documentation/efi-stub.txt b/Documentation/efi-stub.txt
new file mode 100644
index 0000000..7837df1
--- /dev/null
+++ b/Documentation/efi-stub.txt
@@ -0,0 +1,78 @@
+			  The EFI Boot Stub
+		     ---------------------------
+
+On the x86 and ARM platforms, a bzImage can masquerade as a PE/COFF image,
+thereby convincing EFI firmware loaders to load it as an EFI
+executable. The code that modifies the bzImage header, along with the
+EFI-specific entry point that the firmware loader jumps to are
+collectively known as the "EFI boot stub", and live in
+arch/x86/boot/header.S and arch/x86/boot/compressed/eboot.c,
+respectively.  For ARM the EFI stub is implemented in
+arch/arm/boot/compressed/efi-header.S and
+arch/arm/boot/compressed/efi-stub.c.  EFI stub code that is shared
+between architectures is in drivers/firmware/efi/efi-stub-helper.c.
+
+By using the EFI boot stub it's possible to boot a Linux kernel
+without the use of a conventional EFI boot loader, such as grub or
+elilo. Since the EFI boot stub performs the jobs of a boot loader, in
+a certain sense it *IS* the boot loader.
+
+The EFI boot stub is enabled with the CONFIG_EFI_STUB kernel option.
+
+
+**** How to install bzImage.efi
+
+The bzImage located in arch/x86/boot/bzImage must be copied to the EFI
+System Partiion (ESP) and renamed with the extension ".efi". Without
+the extension the EFI firmware loader will refuse to execute it. It's
+not possible to execute bzImage.efi from the usual Linux file systems
+because EFI firmware doesn't have support for them.  For ARM the
+arch/arm/boot/zImage should be copied to the system partition, and it
+may not need to be renamed.
+
+
+**** Passing kernel parameters from the EFI shell
+
+Arguments to the kernel can be passed after bzImage.efi, e.g.
+
+	fs0:> bzImage.efi console=ttyS0 root=/dev/sda4
+
+
+**** The "initrd=" option
+
+Like most boot loaders, the EFI stub allows the user to specify
+multiple initrd files using the "initrd=" option. This is the only EFI
+stub-specific command line parameter, everything else is passed to the
+kernel when it boots.
+
+The path to the initrd file must be an absolute path from the
+beginning of the ESP, relative path names do not work. Also, the path
+is an EFI-style path and directory elements must be separated with
+backslashes (\). For example, given the following directory layout,
+
+fs0:>
+	Kernels\
+			bzImage.efi
+			initrd-large.img
+
+	Ramdisks\
+			initrd-small.img
+			initrd-medium.img
+
+to boot with the initrd-large.img file if the current working
+directory is fs0:\Kernels, the following command must be used,
+
+	fs0:\Kernels> bzImage.efi initrd=\Kernels\initrd-large.img
+
+Notice how bzImage.efi can be specified with a relative path. That's
+because the image we're executing is interpreted by the EFI shell,
+which understands relative paths, whereas the rest of the command line
+is passed to bzImage.efi.
+
+
+**** The "dtb=" option
+
+For the ARM architecture, we also need to be able to provide a device
+tree to the kernel.  This is done with the "dtb=" command line option,
+and is process in the same manner as the "initrd=" option that is described
+above.
diff --git a/Documentation/x86/efi-stub.txt b/Documentation/x86/efi-stub.txt
deleted file mode 100644
index 44e6bb6..0000000
--- a/Documentation/x86/efi-stub.txt
+++ /dev/null
@@ -1,65 +0,0 @@
-			  The EFI Boot Stub
-		     ---------------------------
-
-On the x86 platform, a bzImage can masquerade as a PE/COFF image,
-thereby convincing EFI firmware loaders to load it as an EFI
-executable. The code that modifies the bzImage header, along with the
-EFI-specific entry point that the firmware loader jumps to are
-collectively known as the "EFI boot stub", and live in
-arch/x86/boot/header.S and arch/x86/boot/compressed/eboot.c,
-respectively.
-
-By using the EFI boot stub it's possible to boot a Linux kernel
-without the use of a conventional EFI boot loader, such as grub or
-elilo. Since the EFI boot stub performs the jobs of a boot loader, in
-a certain sense it *IS* the boot loader.
-
-The EFI boot stub is enabled with the CONFIG_EFI_STUB kernel option.
-
-
-**** How to install bzImage.efi
-
-The bzImage located in arch/x86/boot/bzImage must be copied to the EFI
-System Partiion (ESP) and renamed with the extension ".efi". Without
-the extension the EFI firmware loader will refuse to execute it. It's
-not possible to execute bzImage.efi from the usual Linux file systems
-because EFI firmware doesn't have support for them.
-
-
-**** Passing kernel parameters from the EFI shell
-
-Arguments to the kernel can be passed after bzImage.efi, e.g.
-
-	fs0:> bzImage.efi console=ttyS0 root=/dev/sda4
-
-
-**** The "initrd=" option
-
-Like most boot loaders, the EFI stub allows the user to specify
-multiple initrd files using the "initrd=" option. This is the only EFI
-stub-specific command line parameter, everything else is passed to the
-kernel when it boots.
-
-The path to the initrd file must be an absolute path from the
-beginning of the ESP, relative path names do not work. Also, the path
-is an EFI-style path and directory elements must be separated with
-backslashes (\). For example, given the following directory layout,
-
-fs0:>
-	Kernels\
-			bzImage.efi
-			initrd-large.img
-
-	Ramdisks\
-			initrd-small.img
-			initrd-medium.img
-
-to boot with the initrd-large.img file if the current working
-directory is fs0:\Kernels, the following command must be used,
-
-	fs0:\Kernels> bzImage.efi initrd=\Kernels\initrd-large.img
-
-Notice how bzImage.efi can be specified with a relative path. That's
-because the image we're executing is interpreted by the EFI shell,
-which understands relative paths, whereas the rest of the command line
-is passed to bzImage.efi.
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index b32ebf9..ec65b51 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -1579,7 +1579,7 @@ config EFI_STUB
           This kernel feature allows a bzImage to be loaded directly
 	  by EFI firmware without the use of a bootloader.
 
-	  See Documentation/x86/efi-stub.txt for more information.
+	  See Documentation/efi-stub.txt for more information.
 
 config SECCOMP
 	def_bool y
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH 2/7] Move common EFI stub code from x86 arch code to common location
  2013-08-02 21:29 ` Roy Franz
@ 2013-08-02 21:29   ` Roy Franz
  -1 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-02 21:29 UTC (permalink / raw)
  To: linux-kernel, linux-efi, linux-arm-kernel, matt.fleming, linux
  Cc: leif.lindholm, Roy Franz

No code changes made, just moving functions from x86 arch directory
to common location.
Code is shared using #include, similar to how decompression code
is shared among architectures.

Signed-off-by: Roy Franz <roy.franz@linaro.org>
---
 arch/x86/boot/compressed/eboot.c       |  442 +-----------------------------
 arch/x86/boot/compressed/eboot.h       |    6 -
 drivers/firmware/efi/efi-stub-helper.c |  463 ++++++++++++++++++++++++++++++++
 include/linux/efi.h                    |    8 +
 4 files changed, 472 insertions(+), 447 deletions(-)
 create mode 100644 drivers/firmware/efi/efi-stub-helper.c

diff --git a/arch/x86/boot/compressed/eboot.c b/arch/x86/boot/compressed/eboot.c
index b7388a4..ab0eefc 100644
--- a/arch/x86/boot/compressed/eboot.c
+++ b/arch/x86/boot/compressed/eboot.c
@@ -19,214 +19,10 @@
 
 static efi_system_table_t *sys_table;
 
-static void efi_char16_printk(efi_char16_t *str)
-{
-	struct efi_simple_text_output_protocol *out;
-
-	out = (struct efi_simple_text_output_protocol *)sys_table->con_out;
-	efi_call_phys2(out->output_string, out, str);
-}
-
-static void efi_printk(char *str)
-{
-	char *s8;
-
-	for (s8 = str; *s8; s8++) {
-		efi_char16_t ch[2] = { 0 };
-
-		ch[0] = *s8;
-		if (*s8 == '\n') {
-			efi_char16_t nl[2] = { '\r', 0 };
-			efi_char16_printk(nl);
-		}
-
-		efi_char16_printk(ch);
-	}
-}
-
-static efi_status_t __get_map(efi_memory_desc_t **map, unsigned long *map_size,
-			      unsigned long *desc_size)
-{
-	efi_memory_desc_t *m = NULL;
-	efi_status_t status;
-	unsigned long key;
-	u32 desc_version;
-
-	*map_size = sizeof(*m) * 32;
-again:
-	/*
-	 * Add an additional efi_memory_desc_t because we're doing an
-	 * allocation which may be in a new descriptor region.
-	 */
-	*map_size += sizeof(*m);
-	status = efi_call_phys3(sys_table->boottime->allocate_pool,
-				EFI_LOADER_DATA, *map_size, (void **)&m);
-	if (status != EFI_SUCCESS)
-		goto fail;
-
-	status = efi_call_phys5(sys_table->boottime->get_memory_map, map_size,
-				m, &key, desc_size, &desc_version);
-	if (status == EFI_BUFFER_TOO_SMALL) {
-		efi_call_phys1(sys_table->boottime->free_pool, m);
-		goto again;
-	}
-
-	if (status != EFI_SUCCESS)
-		efi_call_phys1(sys_table->boottime->free_pool, m);
-
-fail:
-	*map = m;
-	return status;
-}
-
-/*
- * Allocate at the highest possible address that is not above 'max'.
- */
-static efi_status_t high_alloc(unsigned long size, unsigned long align,
-			      unsigned long *addr, unsigned long max)
-{
-	unsigned long map_size, desc_size;
-	efi_memory_desc_t *map;
-	efi_status_t status;
-	unsigned long nr_pages;
-	u64 max_addr = 0;
-	int i;
-
-	status = __get_map(&map, &map_size, &desc_size);
-	if (status != EFI_SUCCESS)
-		goto fail;
-
-	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
-again:
-	for (i = 0; i < map_size / desc_size; i++) {
-		efi_memory_desc_t *desc;
-		unsigned long m = (unsigned long)map;
-		u64 start, end;
-
-		desc = (efi_memory_desc_t *)(m + (i * desc_size));
-		if (desc->type != EFI_CONVENTIONAL_MEMORY)
-			continue;
-
-		if (desc->num_pages < nr_pages)
-			continue;
 
-		start = desc->phys_addr;
-		end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
+#include "../../../../drivers/firmware/efi/efi-stub-helper.c"
 
-		if ((start + size) > end || (start + size) > max)
-			continue;
-
-		if (end - size > max)
-			end = max;
-
-		if (round_down(end - size, align) < start)
-			continue;
-
-		start = round_down(end - size, align);
-
-		/*
-		 * Don't allocate at 0x0. It will confuse code that
-		 * checks pointers against NULL.
-		 */
-		if (start == 0x0)
-			continue;
-
-		if (start > max_addr)
-			max_addr = start;
-	}
-
-	if (!max_addr)
-		status = EFI_NOT_FOUND;
-	else {
-		status = efi_call_phys4(sys_table->boottime->allocate_pages,
-					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
-					nr_pages, &max_addr);
-		if (status != EFI_SUCCESS) {
-			max = max_addr;
-			max_addr = 0;
-			goto again;
-		}
-
-		*addr = max_addr;
-	}
-
-free_pool:
-	efi_call_phys1(sys_table->boottime->free_pool, map);
-
-fail:
-	return status;
-}
-
-/*
- * Allocate at the lowest possible address.
- */
-static efi_status_t low_alloc(unsigned long size, unsigned long align,
-			      unsigned long *addr)
-{
-	unsigned long map_size, desc_size;
-	efi_memory_desc_t *map;
-	efi_status_t status;
-	unsigned long nr_pages;
-	int i;
-
-	status = __get_map(&map, &map_size, &desc_size);
-	if (status != EFI_SUCCESS)
-		goto fail;
-
-	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
-	for (i = 0; i < map_size / desc_size; i++) {
-		efi_memory_desc_t *desc;
-		unsigned long m = (unsigned long)map;
-		u64 start, end;
-
-		desc = (efi_memory_desc_t *)(m + (i * desc_size));
-
-		if (desc->type != EFI_CONVENTIONAL_MEMORY)
-			continue;
-
-		if (desc->num_pages < nr_pages)
-			continue;
-
-		start = desc->phys_addr;
-		end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
-
-		/*
-		 * Don't allocate at 0x0. It will confuse code that
-		 * checks pointers against NULL. Skip the first 8
-		 * bytes so we start at a nice even number.
-		 */
-		if (start == 0x0)
-			start += 8;
-
-		start = round_up(start, align);
-		if ((start + size) > end)
-			continue;
-
-		status = efi_call_phys4(sys_table->boottime->allocate_pages,
-					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
-					nr_pages, &start);
-		if (status == EFI_SUCCESS) {
-			*addr = start;
-			break;
-		}
-	}
 
-	if (i == map_size / desc_size)
-		status = EFI_NOT_FOUND;
-
-free_pool:
-	efi_call_phys1(sys_table->boottime->free_pool, map);
-fail:
-	return status;
-}
-
-static void low_free(unsigned long size, unsigned long addr)
-{
-	unsigned long nr_pages;
-
-	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
-	efi_call_phys2(sys_table->boottime->free_pages, addr, nr_pages);
-}
 
 static void find_bits(unsigned long mask, u8 *pos, u8 *size)
 {
@@ -624,242 +420,6 @@ void setup_graphics(struct boot_params *boot_params)
 	}
 }
 
-struct initrd {
-	efi_file_handle_t *handle;
-	u64 size;
-};
-
-/*
- * Check the cmdline for a LILO-style initrd= arguments.
- *
- * We only support loading an initrd from the same filesystem as the
- * kernel image.
- */
-static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
-				    struct setup_header *hdr)
-{
-	struct initrd *initrds;
-	unsigned long initrd_addr;
-	efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
-	u64 initrd_total;
-	efi_file_io_interface_t *io;
-	efi_file_handle_t *fh;
-	efi_status_t status;
-	int nr_initrds;
-	char *str;
-	int i, j, k;
-
-	initrd_addr = 0;
-	initrd_total = 0;
-
-	str = (char *)(unsigned long)hdr->cmd_line_ptr;
-
-	j = 0;			/* See close_handles */
-
-	if (!str || !*str)
-		return EFI_SUCCESS;
-
-	for (nr_initrds = 0; *str; nr_initrds++) {
-		str = strstr(str, "initrd=");
-		if (!str)
-			break;
-
-		str += 7;
-
-		/* Skip any leading slashes */
-		while (*str == '/' || *str == '\\')
-			str++;
-
-		while (*str && *str != ' ' && *str != '\n')
-			str++;
-	}
-
-	if (!nr_initrds)
-		return EFI_SUCCESS;
-
-	status = efi_call_phys3(sys_table->boottime->allocate_pool,
-				EFI_LOADER_DATA,
-				nr_initrds * sizeof(*initrds),
-				&initrds);
-	if (status != EFI_SUCCESS) {
-		efi_printk("Failed to alloc mem for initrds\n");
-		goto fail;
-	}
-
-	str = (char *)(unsigned long)hdr->cmd_line_ptr;
-	for (i = 0; i < nr_initrds; i++) {
-		struct initrd *initrd;
-		efi_file_handle_t *h;
-		efi_file_info_t *info;
-		efi_char16_t filename_16[256];
-		unsigned long info_sz;
-		efi_guid_t info_guid = EFI_FILE_INFO_ID;
-		efi_char16_t *p;
-		u64 file_sz;
-
-		str = strstr(str, "initrd=");
-		if (!str)
-			break;
-
-		str += 7;
-
-		initrd = &initrds[i];
-		p = filename_16;
-
-		/* Skip any leading slashes */
-		while (*str == '/' || *str == '\\')
-			str++;
-
-		while (*str && *str != ' ' && *str != '\n') {
-			if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
-				break;
-
-			if (*str == '/') {
-				*p++ = '\\';
-				*str++;
-			} else {
-				*p++ = *str++;
-			}
-		}
-
-		*p = '\0';
-
-		/* Only open the volume once. */
-		if (!i) {
-			efi_boot_services_t *boottime;
-
-			boottime = sys_table->boottime;
-
-			status = efi_call_phys3(boottime->handle_protocol,
-					image->device_handle, &fs_proto, &io);
-			if (status != EFI_SUCCESS) {
-				efi_printk("Failed to handle fs_proto\n");
-				goto free_initrds;
-			}
-
-			status = efi_call_phys2(io->open_volume, io, &fh);
-			if (status != EFI_SUCCESS) {
-				efi_printk("Failed to open volume\n");
-				goto free_initrds;
-			}
-		}
-
-		status = efi_call_phys5(fh->open, fh, &h, filename_16,
-					EFI_FILE_MODE_READ, (u64)0);
-		if (status != EFI_SUCCESS) {
-			efi_printk("Failed to open initrd file: ");
-			efi_char16_printk(filename_16);
-			efi_printk("\n");
-			goto close_handles;
-		}
-
-		initrd->handle = h;
-
-		info_sz = 0;
-		status = efi_call_phys4(h->get_info, h, &info_guid,
-					&info_sz, NULL);
-		if (status != EFI_BUFFER_TOO_SMALL) {
-			efi_printk("Failed to get initrd info size\n");
-			goto close_handles;
-		}
-
-grow:
-		status = efi_call_phys3(sys_table->boottime->allocate_pool,
-					EFI_LOADER_DATA, info_sz, &info);
-		if (status != EFI_SUCCESS) {
-			efi_printk("Failed to alloc mem for initrd info\n");
-			goto close_handles;
-		}
-
-		status = efi_call_phys4(h->get_info, h, &info_guid,
-					&info_sz, info);
-		if (status == EFI_BUFFER_TOO_SMALL) {
-			efi_call_phys1(sys_table->boottime->free_pool, info);
-			goto grow;
-		}
-
-		file_sz = info->file_size;
-		efi_call_phys1(sys_table->boottime->free_pool, info);
-
-		if (status != EFI_SUCCESS) {
-			efi_printk("Failed to get initrd info\n");
-			goto close_handles;
-		}
-
-		initrd->size = file_sz;
-		initrd_total += file_sz;
-	}
-
-	if (initrd_total) {
-		unsigned long addr;
-
-		/*
-		 * Multiple initrd's need to be at consecutive
-		 * addresses in memory, so allocate enough memory for
-		 * all the initrd's.
-		 */
-		status = high_alloc(initrd_total, 0x1000,
-				   &initrd_addr, hdr->initrd_addr_max);
-		if (status != EFI_SUCCESS) {
-			efi_printk("Failed to alloc highmem for initrds\n");
-			goto close_handles;
-		}
-
-		/* We've run out of free low memory. */
-		if (initrd_addr > hdr->initrd_addr_max) {
-			efi_printk("We've run out of free low memory\n");
-			status = EFI_INVALID_PARAMETER;
-			goto free_initrd_total;
-		}
-
-		addr = initrd_addr;
-		for (j = 0; j < nr_initrds; j++) {
-			u64 size;
-
-			size = initrds[j].size;
-			while (size) {
-				u64 chunksize;
-				if (size > EFI_READ_CHUNK_SIZE)
-					chunksize = EFI_READ_CHUNK_SIZE;
-				else
-					chunksize = size;
-				status = efi_call_phys3(fh->read,
-							initrds[j].handle,
-							&chunksize, addr);
-				if (status != EFI_SUCCESS) {
-					efi_printk("Failed to read initrd\n");
-					goto free_initrd_total;
-				}
-				addr += chunksize;
-				size -= chunksize;
-			}
-
-			efi_call_phys1(fh->close, initrds[j].handle);
-		}
-
-	}
-
-	efi_call_phys1(sys_table->boottime->free_pool, initrds);
-
-	hdr->ramdisk_image = initrd_addr;
-	hdr->ramdisk_size = initrd_total;
-
-	return status;
-
-free_initrd_total:
-	low_free(initrd_total, initrd_addr);
-
-close_handles:
-	for (k = j; k < i; k++)
-		efi_call_phys1(fh->close, initrds[k].handle);
-free_initrds:
-	efi_call_phys1(sys_table->boottime->free_pool, initrds);
-fail:
-	hdr->ramdisk_image = 0;
-	hdr->ramdisk_size = 0;
-
-	return status;
-}
 
 /*
  * Because the x86 boot code expects to be passed a boot_params we
diff --git a/arch/x86/boot/compressed/eboot.h b/arch/x86/boot/compressed/eboot.h
index e5b0a8f..faa0bdf 100644
--- a/arch/x86/boot/compressed/eboot.h
+++ b/arch/x86/boot/compressed/eboot.h
@@ -62,10 +62,4 @@ struct efi_uga_draw_protocol {
 	void *blt;
 };
 
-struct efi_simple_text_output_protocol {
-	void *reset;
-	void *output_string;
-	void *test_string;
-};
-
 #endif /* BOOT_COMPRESSED_EBOOT_H */
diff --git a/drivers/firmware/efi/efi-stub-helper.c b/drivers/firmware/efi/efi-stub-helper.c
new file mode 100644
index 0000000..47891bd
--- /dev/null
+++ b/drivers/firmware/efi/efi-stub-helper.c
@@ -0,0 +1,463 @@
+/*
+ * Helper functions used by the EFI stub on multiple
+ * architectures. This should be #included by the EFI stub
+ * implementation files.
+ *
+ * Copyright 2011 Intel Corporation; author Matt Fleming
+ *
+ * This file is part of the Linux kernel, and is made available
+ * under the terms of the GNU General Public License version 2.
+ *
+ */
+
+
+struct initrd {
+	efi_file_handle_t *handle;
+	u64 size;
+};
+
+
+
+
+static void efi_char16_printk(efi_char16_t *str)
+{
+	struct efi_simple_text_output_protocol *out;
+
+	out = (struct efi_simple_text_output_protocol *)sys_table->con_out;
+	efi_call_phys2(out->output_string, out, str);
+}
+
+static void efi_printk(char *str)
+{
+	char *s8;
+
+	for (s8 = str; *s8; s8++) {
+		efi_char16_t ch[2] = { 0 };
+
+		ch[0] = *s8;
+		if (*s8 == '\n') {
+			efi_char16_t nl[2] = { '\r', 0 };
+			efi_char16_printk(nl);
+		}
+
+		efi_char16_printk(ch);
+	}
+}
+
+
+static efi_status_t __get_map(efi_memory_desc_t **map, unsigned long *map_size,
+			      unsigned long *desc_size)
+{
+	efi_memory_desc_t *m = NULL;
+	efi_status_t status;
+	unsigned long key;
+	u32 desc_version;
+
+	*map_size = sizeof(*m) * 32;
+again:
+	/*
+	 * Add an additional efi_memory_desc_t because we're doing an
+	 * allocation which may be in a new descriptor region.
+	 */
+	*map_size += sizeof(*m);
+	status = efi_call_phys3(sys_table->boottime->allocate_pool,
+				EFI_LOADER_DATA, *map_size, (void **)&m);
+	if (status != EFI_SUCCESS)
+		goto fail;
+
+	status = efi_call_phys5(sys_table->boottime->get_memory_map, map_size,
+				m, &key, desc_size, &desc_version);
+	if (status == EFI_BUFFER_TOO_SMALL) {
+		efi_call_phys1(sys_table->boottime->free_pool, m);
+		goto again;
+	}
+
+	if (status != EFI_SUCCESS)
+		efi_call_phys1(sys_table->boottime->free_pool, m);
+
+fail:
+	*map = m;
+	return status;
+}
+
+/*
+ * Allocate at the highest possible address that is not above 'max'.
+ */
+static efi_status_t high_alloc(unsigned long size, unsigned long align,
+			      unsigned long *addr, unsigned long max)
+{
+	unsigned long map_size, desc_size;
+	efi_memory_desc_t *map;
+	efi_status_t status;
+	unsigned long nr_pages;
+	u64 max_addr = 0;
+	int i;
+
+	status = __get_map(&map, &map_size, &desc_size);
+	if (status != EFI_SUCCESS)
+		goto fail;
+
+	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
+again:
+	for (i = 0; i < map_size / desc_size; i++) {
+		efi_memory_desc_t *desc;
+		unsigned long m = (unsigned long)map;
+		u64 start, end;
+
+		desc = (efi_memory_desc_t *)(m + (i * desc_size));
+		if (desc->type != EFI_CONVENTIONAL_MEMORY)
+			continue;
+
+		if (desc->num_pages < nr_pages)
+			continue;
+
+		start = desc->phys_addr;
+		end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
+
+		if ((start + size) > end || (start + size) > max)
+			continue;
+
+		if (end - size > max)
+			end = max;
+
+		if (round_down(end - size, align) < start)
+			continue;
+
+		start = round_down(end - size, align);
+
+		/*
+		 * Don't allocate at 0x0. It will confuse code that
+		 * checks pointers against NULL.
+		 */
+		if (start == 0x0)
+			continue;
+
+		if (start > max_addr)
+			max_addr = start;
+	}
+
+	if (!max_addr)
+		status = EFI_NOT_FOUND;
+	else {
+		status = efi_call_phys4(sys_table->boottime->allocate_pages,
+					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
+					nr_pages, &max_addr);
+		if (status != EFI_SUCCESS) {
+			max = max_addr;
+			max_addr = 0;
+			goto again;
+		}
+
+		*addr = max_addr;
+	}
+
+free_pool:
+	efi_call_phys1(sys_table->boottime->free_pool, map);
+
+fail:
+	return status;
+}
+
+/*
+ * Allocate at the lowest possible address.
+ */
+static efi_status_t low_alloc(unsigned long size, unsigned long align,
+			      unsigned long *addr)
+{
+	unsigned long map_size, desc_size;
+	efi_memory_desc_t *map;
+	efi_status_t status;
+	unsigned long nr_pages;
+	int i;
+
+	status = __get_map(&map, &map_size, &desc_size);
+	if (status != EFI_SUCCESS)
+		goto fail;
+
+	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
+	for (i = 0; i < map_size / desc_size; i++) {
+		efi_memory_desc_t *desc;
+		unsigned long m = (unsigned long)map;
+		u64 start, end;
+
+		desc = (efi_memory_desc_t *)(m + (i * desc_size));
+
+		if (desc->type != EFI_CONVENTIONAL_MEMORY)
+			continue;
+
+		if (desc->num_pages < nr_pages)
+			continue;
+
+		start = desc->phys_addr;
+		end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
+
+		/*
+		 * Don't allocate at 0x0. It will confuse code that
+		 * checks pointers against NULL. Skip the first 8
+		 * bytes so we start at a nice even number.
+		 */
+		if (start == 0x0)
+			start += 8;
+
+		start = round_up(start, align);
+		if ((start + size) > end)
+			continue;
+
+		status = efi_call_phys4(sys_table->boottime->allocate_pages,
+					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
+					nr_pages, &start);
+		if (status == EFI_SUCCESS) {
+			*addr = start;
+			break;
+		}
+	}
+
+	if (i == map_size / desc_size)
+		status = EFI_NOT_FOUND;
+
+free_pool:
+	efi_call_phys1(sys_table->boottime->free_pool, map);
+fail:
+	return status;
+}
+
+static void low_free(unsigned long size, unsigned long addr)
+{
+	unsigned long nr_pages;
+
+	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
+	efi_call_phys2(sys_table->boottime->free_pages, addr, nr_pages);
+}
+
+
+/*
+ * Check the cmdline for a LILO-style initrd= arguments.
+ *
+ * We only support loading an initrd from the same filesystem as the
+ * kernel image.
+ */
+static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
+				    struct setup_header *hdr)
+{
+	struct initrd *initrds;
+	unsigned long initrd_addr;
+	efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
+	u64 initrd_total;
+	efi_file_io_interface_t *io;
+	efi_file_handle_t *fh;
+	efi_status_t status;
+	int nr_initrds;
+	char *str;
+	int i, j, k;
+
+	initrd_addr = 0;
+	initrd_total = 0;
+
+	str = (char *)(unsigned long)hdr->cmd_line_ptr;
+
+	j = 0;			/* See close_handles */
+
+	if (!str || !*str)
+		return EFI_SUCCESS;
+
+	for (nr_initrds = 0; *str; nr_initrds++) {
+		str = strstr(str, "initrd=");
+		if (!str)
+			break;
+
+		str += 7;
+
+		/* Skip any leading slashes */
+		while (*str == '/' || *str == '\\')
+			str++;
+
+		while (*str && *str != ' ' && *str != '\n')
+			str++;
+	}
+
+	if (!nr_initrds)
+		return EFI_SUCCESS;
+
+	status = efi_call_phys3(sys_table->boottime->allocate_pool,
+				EFI_LOADER_DATA,
+				nr_initrds * sizeof(*initrds),
+				&initrds);
+	if (status != EFI_SUCCESS) {
+		efi_printk("Failed to alloc mem for initrds\n");
+		goto fail;
+	}
+
+	str = (char *)(unsigned long)hdr->cmd_line_ptr;
+	for (i = 0; i < nr_initrds; i++) {
+		struct initrd *initrd;
+		efi_file_handle_t *h;
+		efi_file_info_t *info;
+		efi_char16_t filename_16[256];
+		unsigned long info_sz;
+		efi_guid_t info_guid = EFI_FILE_INFO_ID;
+		efi_char16_t *p;
+		u64 file_sz;
+
+		str = strstr(str, "initrd=");
+		if (!str)
+			break;
+
+		str += 7;
+
+		initrd = &initrds[i];
+		p = filename_16;
+
+		/* Skip any leading slashes */
+		while (*str == '/' || *str == '\\')
+			str++;
+
+		while (*str && *str != ' ' && *str != '\n') {
+			if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
+				break;
+
+			if (*str == '/') {
+				*p++ = '\\';
+				*str++;
+			} else {
+				*p++ = *str++;
+			}
+		}
+
+		*p = '\0';
+
+		/* Only open the volume once. */
+		if (!i) {
+			efi_boot_services_t *boottime;
+
+			boottime = sys_table->boottime;
+
+			status = efi_call_phys3(boottime->handle_protocol,
+					image->device_handle, &fs_proto, &io);
+			if (status != EFI_SUCCESS) {
+				efi_printk("Failed to handle fs_proto\n");
+				goto free_initrds;
+			}
+
+			status = efi_call_phys2(io->open_volume, io, &fh);
+			if (status != EFI_SUCCESS) {
+				efi_printk("Failed to open volume\n");
+				goto free_initrds;
+			}
+		}
+
+		status = efi_call_phys5(fh->open, fh, &h, filename_16,
+					EFI_FILE_MODE_READ, (u64)0);
+		if (status != EFI_SUCCESS) {
+			efi_printk("Failed to open initrd file: ");
+			efi_char16_printk(filename_16);
+			efi_printk("\n");
+			goto close_handles;
+		}
+
+		initrd->handle = h;
+
+		info_sz = 0;
+		status = efi_call_phys4(h->get_info, h, &info_guid,
+					&info_sz, NULL);
+		if (status != EFI_BUFFER_TOO_SMALL) {
+			efi_printk("Failed to get initrd info size\n");
+			goto close_handles;
+		}
+
+grow:
+		status = efi_call_phys3(sys_table->boottime->allocate_pool,
+					EFI_LOADER_DATA, info_sz, &info);
+		if (status != EFI_SUCCESS) {
+			efi_printk("Failed to alloc mem for initrd info\n");
+			goto close_handles;
+		}
+
+		status = efi_call_phys4(h->get_info, h, &info_guid,
+					&info_sz, info);
+		if (status == EFI_BUFFER_TOO_SMALL) {
+			efi_call_phys1(sys_table->boottime->free_pool, info);
+			goto grow;
+		}
+
+		file_sz = info->file_size;
+		efi_call_phys1(sys_table->boottime->free_pool, info);
+
+		if (status != EFI_SUCCESS) {
+			efi_printk("Failed to get initrd info\n");
+			goto close_handles;
+		}
+
+		initrd->size = file_sz;
+		initrd_total += file_sz;
+	}
+
+	if (initrd_total) {
+		unsigned long addr;
+
+		/*
+		 * Multiple initrd's need to be at consecutive
+		 * addresses in memory, so allocate enough memory for
+		 * all the initrd's.
+		 */
+		status = high_alloc(initrd_total, 0x1000,
+				   &initrd_addr, hdr->initrd_addr_max);
+		if (status != EFI_SUCCESS) {
+			efi_printk("Failed to alloc highmem for initrds\n");
+			goto close_handles;
+		}
+
+		/* We've run out of free low memory. */
+		if (initrd_addr > hdr->initrd_addr_max) {
+			efi_printk("We've run out of free low memory\n");
+			status = EFI_INVALID_PARAMETER;
+			goto free_initrd_total;
+		}
+
+		addr = initrd_addr;
+		for (j = 0; j < nr_initrds; j++) {
+			u64 size;
+
+			size = initrds[j].size;
+			while (size) {
+				u64 chunksize;
+				if (size > EFI_READ_CHUNK_SIZE)
+					chunksize = EFI_READ_CHUNK_SIZE;
+				else
+					chunksize = size;
+				status = efi_call_phys3(fh->read,
+							initrds[j].handle,
+							&chunksize, addr);
+				if (status != EFI_SUCCESS) {
+					efi_printk("Failed to read initrd\n");
+					goto free_initrd_total;
+				}
+				addr += chunksize;
+				size -= chunksize;
+			}
+
+			efi_call_phys1(fh->close, initrds[j].handle);
+		}
+
+	}
+
+	efi_call_phys1(sys_table->boottime->free_pool, initrds);
+
+	hdr->ramdisk_image = initrd_addr;
+	hdr->ramdisk_size = initrd_total;
+
+	return status;
+
+free_initrd_total:
+	low_free(initrd_total, initrd_addr);
+
+close_handles:
+	for (k = j; k < i; k++)
+		efi_call_phys1(fh->close, initrds[k].handle);
+free_initrds:
+	efi_call_phys1(sys_table->boottime->free_pool, initrds);
+fail:
+	hdr->ramdisk_image = 0;
+	hdr->ramdisk_size = 0;
+
+	return status;
+}
diff --git a/include/linux/efi.h b/include/linux/efi.h
index 5f8f176..51f5641 100644
--- a/include/linux/efi.h
+++ b/include/linux/efi.h
@@ -784,6 +784,14 @@ struct efivar_entry {
 	struct kobject kobj;
 };
 
+
+struct efi_simple_text_output_protocol {
+	void *reset;
+	void *output_string;
+	void *test_string;
+};
+
+
 extern struct list_head efivar_sysfs_list;
 
 static inline void
-- 
1.7.10.4


^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH 2/7] Move common EFI stub code from x86 arch code to common location
@ 2013-08-02 21:29   ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-02 21:29 UTC (permalink / raw)
  To: linux-arm-kernel

No code changes made, just moving functions from x86 arch directory
to common location.
Code is shared using #include, similar to how decompression code
is shared among architectures.

Signed-off-by: Roy Franz <roy.franz@linaro.org>
---
 arch/x86/boot/compressed/eboot.c       |  442 +-----------------------------
 arch/x86/boot/compressed/eboot.h       |    6 -
 drivers/firmware/efi/efi-stub-helper.c |  463 ++++++++++++++++++++++++++++++++
 include/linux/efi.h                    |    8 +
 4 files changed, 472 insertions(+), 447 deletions(-)
 create mode 100644 drivers/firmware/efi/efi-stub-helper.c

diff --git a/arch/x86/boot/compressed/eboot.c b/arch/x86/boot/compressed/eboot.c
index b7388a4..ab0eefc 100644
--- a/arch/x86/boot/compressed/eboot.c
+++ b/arch/x86/boot/compressed/eboot.c
@@ -19,214 +19,10 @@
 
 static efi_system_table_t *sys_table;
 
-static void efi_char16_printk(efi_char16_t *str)
-{
-	struct efi_simple_text_output_protocol *out;
-
-	out = (struct efi_simple_text_output_protocol *)sys_table->con_out;
-	efi_call_phys2(out->output_string, out, str);
-}
-
-static void efi_printk(char *str)
-{
-	char *s8;
-
-	for (s8 = str; *s8; s8++) {
-		efi_char16_t ch[2] = { 0 };
-
-		ch[0] = *s8;
-		if (*s8 == '\n') {
-			efi_char16_t nl[2] = { '\r', 0 };
-			efi_char16_printk(nl);
-		}
-
-		efi_char16_printk(ch);
-	}
-}
-
-static efi_status_t __get_map(efi_memory_desc_t **map, unsigned long *map_size,
-			      unsigned long *desc_size)
-{
-	efi_memory_desc_t *m = NULL;
-	efi_status_t status;
-	unsigned long key;
-	u32 desc_version;
-
-	*map_size = sizeof(*m) * 32;
-again:
-	/*
-	 * Add an additional efi_memory_desc_t because we're doing an
-	 * allocation which may be in a new descriptor region.
-	 */
-	*map_size += sizeof(*m);
-	status = efi_call_phys3(sys_table->boottime->allocate_pool,
-				EFI_LOADER_DATA, *map_size, (void **)&m);
-	if (status != EFI_SUCCESS)
-		goto fail;
-
-	status = efi_call_phys5(sys_table->boottime->get_memory_map, map_size,
-				m, &key, desc_size, &desc_version);
-	if (status == EFI_BUFFER_TOO_SMALL) {
-		efi_call_phys1(sys_table->boottime->free_pool, m);
-		goto again;
-	}
-
-	if (status != EFI_SUCCESS)
-		efi_call_phys1(sys_table->boottime->free_pool, m);
-
-fail:
-	*map = m;
-	return status;
-}
-
-/*
- * Allocate at the highest possible address that is not above 'max'.
- */
-static efi_status_t high_alloc(unsigned long size, unsigned long align,
-			      unsigned long *addr, unsigned long max)
-{
-	unsigned long map_size, desc_size;
-	efi_memory_desc_t *map;
-	efi_status_t status;
-	unsigned long nr_pages;
-	u64 max_addr = 0;
-	int i;
-
-	status = __get_map(&map, &map_size, &desc_size);
-	if (status != EFI_SUCCESS)
-		goto fail;
-
-	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
-again:
-	for (i = 0; i < map_size / desc_size; i++) {
-		efi_memory_desc_t *desc;
-		unsigned long m = (unsigned long)map;
-		u64 start, end;
-
-		desc = (efi_memory_desc_t *)(m + (i * desc_size));
-		if (desc->type != EFI_CONVENTIONAL_MEMORY)
-			continue;
-
-		if (desc->num_pages < nr_pages)
-			continue;
 
-		start = desc->phys_addr;
-		end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
+#include "../../../../drivers/firmware/efi/efi-stub-helper.c"
 
-		if ((start + size) > end || (start + size) > max)
-			continue;
-
-		if (end - size > max)
-			end = max;
-
-		if (round_down(end - size, align) < start)
-			continue;
-
-		start = round_down(end - size, align);
-
-		/*
-		 * Don't allocate@0x0. It will confuse code that
-		 * checks pointers against NULL.
-		 */
-		if (start == 0x0)
-			continue;
-
-		if (start > max_addr)
-			max_addr = start;
-	}
-
-	if (!max_addr)
-		status = EFI_NOT_FOUND;
-	else {
-		status = efi_call_phys4(sys_table->boottime->allocate_pages,
-					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
-					nr_pages, &max_addr);
-		if (status != EFI_SUCCESS) {
-			max = max_addr;
-			max_addr = 0;
-			goto again;
-		}
-
-		*addr = max_addr;
-	}
-
-free_pool:
-	efi_call_phys1(sys_table->boottime->free_pool, map);
-
-fail:
-	return status;
-}
-
-/*
- * Allocate at the lowest possible address.
- */
-static efi_status_t low_alloc(unsigned long size, unsigned long align,
-			      unsigned long *addr)
-{
-	unsigned long map_size, desc_size;
-	efi_memory_desc_t *map;
-	efi_status_t status;
-	unsigned long nr_pages;
-	int i;
-
-	status = __get_map(&map, &map_size, &desc_size);
-	if (status != EFI_SUCCESS)
-		goto fail;
-
-	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
-	for (i = 0; i < map_size / desc_size; i++) {
-		efi_memory_desc_t *desc;
-		unsigned long m = (unsigned long)map;
-		u64 start, end;
-
-		desc = (efi_memory_desc_t *)(m + (i * desc_size));
-
-		if (desc->type != EFI_CONVENTIONAL_MEMORY)
-			continue;
-
-		if (desc->num_pages < nr_pages)
-			continue;
-
-		start = desc->phys_addr;
-		end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
-
-		/*
-		 * Don't allocate at 0x0. It will confuse code that
-		 * checks pointers against NULL. Skip the first 8
-		 * bytes so we start@a nice even number.
-		 */
-		if (start == 0x0)
-			start += 8;
-
-		start = round_up(start, align);
-		if ((start + size) > end)
-			continue;
-
-		status = efi_call_phys4(sys_table->boottime->allocate_pages,
-					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
-					nr_pages, &start);
-		if (status == EFI_SUCCESS) {
-			*addr = start;
-			break;
-		}
-	}
 
-	if (i == map_size / desc_size)
-		status = EFI_NOT_FOUND;
-
-free_pool:
-	efi_call_phys1(sys_table->boottime->free_pool, map);
-fail:
-	return status;
-}
-
-static void low_free(unsigned long size, unsigned long addr)
-{
-	unsigned long nr_pages;
-
-	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
-	efi_call_phys2(sys_table->boottime->free_pages, addr, nr_pages);
-}
 
 static void find_bits(unsigned long mask, u8 *pos, u8 *size)
 {
@@ -624,242 +420,6 @@ void setup_graphics(struct boot_params *boot_params)
 	}
 }
 
-struct initrd {
-	efi_file_handle_t *handle;
-	u64 size;
-};
-
-/*
- * Check the cmdline for a LILO-style initrd= arguments.
- *
- * We only support loading an initrd from the same filesystem as the
- * kernel image.
- */
-static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
-				    struct setup_header *hdr)
-{
-	struct initrd *initrds;
-	unsigned long initrd_addr;
-	efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
-	u64 initrd_total;
-	efi_file_io_interface_t *io;
-	efi_file_handle_t *fh;
-	efi_status_t status;
-	int nr_initrds;
-	char *str;
-	int i, j, k;
-
-	initrd_addr = 0;
-	initrd_total = 0;
-
-	str = (char *)(unsigned long)hdr->cmd_line_ptr;
-
-	j = 0;			/* See close_handles */
-
-	if (!str || !*str)
-		return EFI_SUCCESS;
-
-	for (nr_initrds = 0; *str; nr_initrds++) {
-		str = strstr(str, "initrd=");
-		if (!str)
-			break;
-
-		str += 7;
-
-		/* Skip any leading slashes */
-		while (*str == '/' || *str == '\\')
-			str++;
-
-		while (*str && *str != ' ' && *str != '\n')
-			str++;
-	}
-
-	if (!nr_initrds)
-		return EFI_SUCCESS;
-
-	status = efi_call_phys3(sys_table->boottime->allocate_pool,
-				EFI_LOADER_DATA,
-				nr_initrds * sizeof(*initrds),
-				&initrds);
-	if (status != EFI_SUCCESS) {
-		efi_printk("Failed to alloc mem for initrds\n");
-		goto fail;
-	}
-
-	str = (char *)(unsigned long)hdr->cmd_line_ptr;
-	for (i = 0; i < nr_initrds; i++) {
-		struct initrd *initrd;
-		efi_file_handle_t *h;
-		efi_file_info_t *info;
-		efi_char16_t filename_16[256];
-		unsigned long info_sz;
-		efi_guid_t info_guid = EFI_FILE_INFO_ID;
-		efi_char16_t *p;
-		u64 file_sz;
-
-		str = strstr(str, "initrd=");
-		if (!str)
-			break;
-
-		str += 7;
-
-		initrd = &initrds[i];
-		p = filename_16;
-
-		/* Skip any leading slashes */
-		while (*str == '/' || *str == '\\')
-			str++;
-
-		while (*str && *str != ' ' && *str != '\n') {
-			if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
-				break;
-
-			if (*str == '/') {
-				*p++ = '\\';
-				*str++;
-			} else {
-				*p++ = *str++;
-			}
-		}
-
-		*p = '\0';
-
-		/* Only open the volume once. */
-		if (!i) {
-			efi_boot_services_t *boottime;
-
-			boottime = sys_table->boottime;
-
-			status = efi_call_phys3(boottime->handle_protocol,
-					image->device_handle, &fs_proto, &io);
-			if (status != EFI_SUCCESS) {
-				efi_printk("Failed to handle fs_proto\n");
-				goto free_initrds;
-			}
-
-			status = efi_call_phys2(io->open_volume, io, &fh);
-			if (status != EFI_SUCCESS) {
-				efi_printk("Failed to open volume\n");
-				goto free_initrds;
-			}
-		}
-
-		status = efi_call_phys5(fh->open, fh, &h, filename_16,
-					EFI_FILE_MODE_READ, (u64)0);
-		if (status != EFI_SUCCESS) {
-			efi_printk("Failed to open initrd file: ");
-			efi_char16_printk(filename_16);
-			efi_printk("\n");
-			goto close_handles;
-		}
-
-		initrd->handle = h;
-
-		info_sz = 0;
-		status = efi_call_phys4(h->get_info, h, &info_guid,
-					&info_sz, NULL);
-		if (status != EFI_BUFFER_TOO_SMALL) {
-			efi_printk("Failed to get initrd info size\n");
-			goto close_handles;
-		}
-
-grow:
-		status = efi_call_phys3(sys_table->boottime->allocate_pool,
-					EFI_LOADER_DATA, info_sz, &info);
-		if (status != EFI_SUCCESS) {
-			efi_printk("Failed to alloc mem for initrd info\n");
-			goto close_handles;
-		}
-
-		status = efi_call_phys4(h->get_info, h, &info_guid,
-					&info_sz, info);
-		if (status == EFI_BUFFER_TOO_SMALL) {
-			efi_call_phys1(sys_table->boottime->free_pool, info);
-			goto grow;
-		}
-
-		file_sz = info->file_size;
-		efi_call_phys1(sys_table->boottime->free_pool, info);
-
-		if (status != EFI_SUCCESS) {
-			efi_printk("Failed to get initrd info\n");
-			goto close_handles;
-		}
-
-		initrd->size = file_sz;
-		initrd_total += file_sz;
-	}
-
-	if (initrd_total) {
-		unsigned long addr;
-
-		/*
-		 * Multiple initrd's need to be at consecutive
-		 * addresses in memory, so allocate enough memory for
-		 * all the initrd's.
-		 */
-		status = high_alloc(initrd_total, 0x1000,
-				   &initrd_addr, hdr->initrd_addr_max);
-		if (status != EFI_SUCCESS) {
-			efi_printk("Failed to alloc highmem for initrds\n");
-			goto close_handles;
-		}
-
-		/* We've run out of free low memory. */
-		if (initrd_addr > hdr->initrd_addr_max) {
-			efi_printk("We've run out of free low memory\n");
-			status = EFI_INVALID_PARAMETER;
-			goto free_initrd_total;
-		}
-
-		addr = initrd_addr;
-		for (j = 0; j < nr_initrds; j++) {
-			u64 size;
-
-			size = initrds[j].size;
-			while (size) {
-				u64 chunksize;
-				if (size > EFI_READ_CHUNK_SIZE)
-					chunksize = EFI_READ_CHUNK_SIZE;
-				else
-					chunksize = size;
-				status = efi_call_phys3(fh->read,
-							initrds[j].handle,
-							&chunksize, addr);
-				if (status != EFI_SUCCESS) {
-					efi_printk("Failed to read initrd\n");
-					goto free_initrd_total;
-				}
-				addr += chunksize;
-				size -= chunksize;
-			}
-
-			efi_call_phys1(fh->close, initrds[j].handle);
-		}
-
-	}
-
-	efi_call_phys1(sys_table->boottime->free_pool, initrds);
-
-	hdr->ramdisk_image = initrd_addr;
-	hdr->ramdisk_size = initrd_total;
-
-	return status;
-
-free_initrd_total:
-	low_free(initrd_total, initrd_addr);
-
-close_handles:
-	for (k = j; k < i; k++)
-		efi_call_phys1(fh->close, initrds[k].handle);
-free_initrds:
-	efi_call_phys1(sys_table->boottime->free_pool, initrds);
-fail:
-	hdr->ramdisk_image = 0;
-	hdr->ramdisk_size = 0;
-
-	return status;
-}
 
 /*
  * Because the x86 boot code expects to be passed a boot_params we
diff --git a/arch/x86/boot/compressed/eboot.h b/arch/x86/boot/compressed/eboot.h
index e5b0a8f..faa0bdf 100644
--- a/arch/x86/boot/compressed/eboot.h
+++ b/arch/x86/boot/compressed/eboot.h
@@ -62,10 +62,4 @@ struct efi_uga_draw_protocol {
 	void *blt;
 };
 
-struct efi_simple_text_output_protocol {
-	void *reset;
-	void *output_string;
-	void *test_string;
-};
-
 #endif /* BOOT_COMPRESSED_EBOOT_H */
diff --git a/drivers/firmware/efi/efi-stub-helper.c b/drivers/firmware/efi/efi-stub-helper.c
new file mode 100644
index 0000000..47891bd
--- /dev/null
+++ b/drivers/firmware/efi/efi-stub-helper.c
@@ -0,0 +1,463 @@
+/*
+ * Helper functions used by the EFI stub on multiple
+ * architectures. This should be #included by the EFI stub
+ * implementation files.
+ *
+ * Copyright 2011 Intel Corporation; author Matt Fleming
+ *
+ * This file is part of the Linux kernel, and is made available
+ * under the terms of the GNU General Public License version 2.
+ *
+ */
+
+
+struct initrd {
+	efi_file_handle_t *handle;
+	u64 size;
+};
+
+
+
+
+static void efi_char16_printk(efi_char16_t *str)
+{
+	struct efi_simple_text_output_protocol *out;
+
+	out = (struct efi_simple_text_output_protocol *)sys_table->con_out;
+	efi_call_phys2(out->output_string, out, str);
+}
+
+static void efi_printk(char *str)
+{
+	char *s8;
+
+	for (s8 = str; *s8; s8++) {
+		efi_char16_t ch[2] = { 0 };
+
+		ch[0] = *s8;
+		if (*s8 == '\n') {
+			efi_char16_t nl[2] = { '\r', 0 };
+			efi_char16_printk(nl);
+		}
+
+		efi_char16_printk(ch);
+	}
+}
+
+
+static efi_status_t __get_map(efi_memory_desc_t **map, unsigned long *map_size,
+			      unsigned long *desc_size)
+{
+	efi_memory_desc_t *m = NULL;
+	efi_status_t status;
+	unsigned long key;
+	u32 desc_version;
+
+	*map_size = sizeof(*m) * 32;
+again:
+	/*
+	 * Add an additional efi_memory_desc_t because we're doing an
+	 * allocation which may be in a new descriptor region.
+	 */
+	*map_size += sizeof(*m);
+	status = efi_call_phys3(sys_table->boottime->allocate_pool,
+				EFI_LOADER_DATA, *map_size, (void **)&m);
+	if (status != EFI_SUCCESS)
+		goto fail;
+
+	status = efi_call_phys5(sys_table->boottime->get_memory_map, map_size,
+				m, &key, desc_size, &desc_version);
+	if (status == EFI_BUFFER_TOO_SMALL) {
+		efi_call_phys1(sys_table->boottime->free_pool, m);
+		goto again;
+	}
+
+	if (status != EFI_SUCCESS)
+		efi_call_phys1(sys_table->boottime->free_pool, m);
+
+fail:
+	*map = m;
+	return status;
+}
+
+/*
+ * Allocate at the highest possible address that is not above 'max'.
+ */
+static efi_status_t high_alloc(unsigned long size, unsigned long align,
+			      unsigned long *addr, unsigned long max)
+{
+	unsigned long map_size, desc_size;
+	efi_memory_desc_t *map;
+	efi_status_t status;
+	unsigned long nr_pages;
+	u64 max_addr = 0;
+	int i;
+
+	status = __get_map(&map, &map_size, &desc_size);
+	if (status != EFI_SUCCESS)
+		goto fail;
+
+	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
+again:
+	for (i = 0; i < map_size / desc_size; i++) {
+		efi_memory_desc_t *desc;
+		unsigned long m = (unsigned long)map;
+		u64 start, end;
+
+		desc = (efi_memory_desc_t *)(m + (i * desc_size));
+		if (desc->type != EFI_CONVENTIONAL_MEMORY)
+			continue;
+
+		if (desc->num_pages < nr_pages)
+			continue;
+
+		start = desc->phys_addr;
+		end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
+
+		if ((start + size) > end || (start + size) > max)
+			continue;
+
+		if (end - size > max)
+			end = max;
+
+		if (round_down(end - size, align) < start)
+			continue;
+
+		start = round_down(end - size, align);
+
+		/*
+		 * Don't allocate@0x0. It will confuse code that
+		 * checks pointers against NULL.
+		 */
+		if (start == 0x0)
+			continue;
+
+		if (start > max_addr)
+			max_addr = start;
+	}
+
+	if (!max_addr)
+		status = EFI_NOT_FOUND;
+	else {
+		status = efi_call_phys4(sys_table->boottime->allocate_pages,
+					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
+					nr_pages, &max_addr);
+		if (status != EFI_SUCCESS) {
+			max = max_addr;
+			max_addr = 0;
+			goto again;
+		}
+
+		*addr = max_addr;
+	}
+
+free_pool:
+	efi_call_phys1(sys_table->boottime->free_pool, map);
+
+fail:
+	return status;
+}
+
+/*
+ * Allocate at the lowest possible address.
+ */
+static efi_status_t low_alloc(unsigned long size, unsigned long align,
+			      unsigned long *addr)
+{
+	unsigned long map_size, desc_size;
+	efi_memory_desc_t *map;
+	efi_status_t status;
+	unsigned long nr_pages;
+	int i;
+
+	status = __get_map(&map, &map_size, &desc_size);
+	if (status != EFI_SUCCESS)
+		goto fail;
+
+	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
+	for (i = 0; i < map_size / desc_size; i++) {
+		efi_memory_desc_t *desc;
+		unsigned long m = (unsigned long)map;
+		u64 start, end;
+
+		desc = (efi_memory_desc_t *)(m + (i * desc_size));
+
+		if (desc->type != EFI_CONVENTIONAL_MEMORY)
+			continue;
+
+		if (desc->num_pages < nr_pages)
+			continue;
+
+		start = desc->phys_addr;
+		end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
+
+		/*
+		 * Don't allocate at 0x0. It will confuse code that
+		 * checks pointers against NULL. Skip the first 8
+		 * bytes so we start@a nice even number.
+		 */
+		if (start == 0x0)
+			start += 8;
+
+		start = round_up(start, align);
+		if ((start + size) > end)
+			continue;
+
+		status = efi_call_phys4(sys_table->boottime->allocate_pages,
+					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
+					nr_pages, &start);
+		if (status == EFI_SUCCESS) {
+			*addr = start;
+			break;
+		}
+	}
+
+	if (i == map_size / desc_size)
+		status = EFI_NOT_FOUND;
+
+free_pool:
+	efi_call_phys1(sys_table->boottime->free_pool, map);
+fail:
+	return status;
+}
+
+static void low_free(unsigned long size, unsigned long addr)
+{
+	unsigned long nr_pages;
+
+	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
+	efi_call_phys2(sys_table->boottime->free_pages, addr, nr_pages);
+}
+
+
+/*
+ * Check the cmdline for a LILO-style initrd= arguments.
+ *
+ * We only support loading an initrd from the same filesystem as the
+ * kernel image.
+ */
+static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
+				    struct setup_header *hdr)
+{
+	struct initrd *initrds;
+	unsigned long initrd_addr;
+	efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
+	u64 initrd_total;
+	efi_file_io_interface_t *io;
+	efi_file_handle_t *fh;
+	efi_status_t status;
+	int nr_initrds;
+	char *str;
+	int i, j, k;
+
+	initrd_addr = 0;
+	initrd_total = 0;
+
+	str = (char *)(unsigned long)hdr->cmd_line_ptr;
+
+	j = 0;			/* See close_handles */
+
+	if (!str || !*str)
+		return EFI_SUCCESS;
+
+	for (nr_initrds = 0; *str; nr_initrds++) {
+		str = strstr(str, "initrd=");
+		if (!str)
+			break;
+
+		str += 7;
+
+		/* Skip any leading slashes */
+		while (*str == '/' || *str == '\\')
+			str++;
+
+		while (*str && *str != ' ' && *str != '\n')
+			str++;
+	}
+
+	if (!nr_initrds)
+		return EFI_SUCCESS;
+
+	status = efi_call_phys3(sys_table->boottime->allocate_pool,
+				EFI_LOADER_DATA,
+				nr_initrds * sizeof(*initrds),
+				&initrds);
+	if (status != EFI_SUCCESS) {
+		efi_printk("Failed to alloc mem for initrds\n");
+		goto fail;
+	}
+
+	str = (char *)(unsigned long)hdr->cmd_line_ptr;
+	for (i = 0; i < nr_initrds; i++) {
+		struct initrd *initrd;
+		efi_file_handle_t *h;
+		efi_file_info_t *info;
+		efi_char16_t filename_16[256];
+		unsigned long info_sz;
+		efi_guid_t info_guid = EFI_FILE_INFO_ID;
+		efi_char16_t *p;
+		u64 file_sz;
+
+		str = strstr(str, "initrd=");
+		if (!str)
+			break;
+
+		str += 7;
+
+		initrd = &initrds[i];
+		p = filename_16;
+
+		/* Skip any leading slashes */
+		while (*str == '/' || *str == '\\')
+			str++;
+
+		while (*str && *str != ' ' && *str != '\n') {
+			if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
+				break;
+
+			if (*str == '/') {
+				*p++ = '\\';
+				*str++;
+			} else {
+				*p++ = *str++;
+			}
+		}
+
+		*p = '\0';
+
+		/* Only open the volume once. */
+		if (!i) {
+			efi_boot_services_t *boottime;
+
+			boottime = sys_table->boottime;
+
+			status = efi_call_phys3(boottime->handle_protocol,
+					image->device_handle, &fs_proto, &io);
+			if (status != EFI_SUCCESS) {
+				efi_printk("Failed to handle fs_proto\n");
+				goto free_initrds;
+			}
+
+			status = efi_call_phys2(io->open_volume, io, &fh);
+			if (status != EFI_SUCCESS) {
+				efi_printk("Failed to open volume\n");
+				goto free_initrds;
+			}
+		}
+
+		status = efi_call_phys5(fh->open, fh, &h, filename_16,
+					EFI_FILE_MODE_READ, (u64)0);
+		if (status != EFI_SUCCESS) {
+			efi_printk("Failed to open initrd file: ");
+			efi_char16_printk(filename_16);
+			efi_printk("\n");
+			goto close_handles;
+		}
+
+		initrd->handle = h;
+
+		info_sz = 0;
+		status = efi_call_phys4(h->get_info, h, &info_guid,
+					&info_sz, NULL);
+		if (status != EFI_BUFFER_TOO_SMALL) {
+			efi_printk("Failed to get initrd info size\n");
+			goto close_handles;
+		}
+
+grow:
+		status = efi_call_phys3(sys_table->boottime->allocate_pool,
+					EFI_LOADER_DATA, info_sz, &info);
+		if (status != EFI_SUCCESS) {
+			efi_printk("Failed to alloc mem for initrd info\n");
+			goto close_handles;
+		}
+
+		status = efi_call_phys4(h->get_info, h, &info_guid,
+					&info_sz, info);
+		if (status == EFI_BUFFER_TOO_SMALL) {
+			efi_call_phys1(sys_table->boottime->free_pool, info);
+			goto grow;
+		}
+
+		file_sz = info->file_size;
+		efi_call_phys1(sys_table->boottime->free_pool, info);
+
+		if (status != EFI_SUCCESS) {
+			efi_printk("Failed to get initrd info\n");
+			goto close_handles;
+		}
+
+		initrd->size = file_sz;
+		initrd_total += file_sz;
+	}
+
+	if (initrd_total) {
+		unsigned long addr;
+
+		/*
+		 * Multiple initrd's need to be at consecutive
+		 * addresses in memory, so allocate enough memory for
+		 * all the initrd's.
+		 */
+		status = high_alloc(initrd_total, 0x1000,
+				   &initrd_addr, hdr->initrd_addr_max);
+		if (status != EFI_SUCCESS) {
+			efi_printk("Failed to alloc highmem for initrds\n");
+			goto close_handles;
+		}
+
+		/* We've run out of free low memory. */
+		if (initrd_addr > hdr->initrd_addr_max) {
+			efi_printk("We've run out of free low memory\n");
+			status = EFI_INVALID_PARAMETER;
+			goto free_initrd_total;
+		}
+
+		addr = initrd_addr;
+		for (j = 0; j < nr_initrds; j++) {
+			u64 size;
+
+			size = initrds[j].size;
+			while (size) {
+				u64 chunksize;
+				if (size > EFI_READ_CHUNK_SIZE)
+					chunksize = EFI_READ_CHUNK_SIZE;
+				else
+					chunksize = size;
+				status = efi_call_phys3(fh->read,
+							initrds[j].handle,
+							&chunksize, addr);
+				if (status != EFI_SUCCESS) {
+					efi_printk("Failed to read initrd\n");
+					goto free_initrd_total;
+				}
+				addr += chunksize;
+				size -= chunksize;
+			}
+
+			efi_call_phys1(fh->close, initrds[j].handle);
+		}
+
+	}
+
+	efi_call_phys1(sys_table->boottime->free_pool, initrds);
+
+	hdr->ramdisk_image = initrd_addr;
+	hdr->ramdisk_size = initrd_total;
+
+	return status;
+
+free_initrd_total:
+	low_free(initrd_total, initrd_addr);
+
+close_handles:
+	for (k = j; k < i; k++)
+		efi_call_phys1(fh->close, initrds[k].handle);
+free_initrds:
+	efi_call_phys1(sys_table->boottime->free_pool, initrds);
+fail:
+	hdr->ramdisk_image = 0;
+	hdr->ramdisk_size = 0;
+
+	return status;
+}
diff --git a/include/linux/efi.h b/include/linux/efi.h
index 5f8f176..51f5641 100644
--- a/include/linux/efi.h
+++ b/include/linux/efi.h
@@ -784,6 +784,14 @@ struct efivar_entry {
 	struct kobject kobj;
 };
 
+
+struct efi_simple_text_output_protocol {
+	void *reset;
+	void *output_string;
+	void *test_string;
+};
+
+
 extern struct list_head efivar_sysfs_list;
 
 static inline void
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH 3/7] Change EFI helper APIs to be more flexible
@ 2013-08-02 21:29   ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-02 21:29 UTC (permalink / raw)
  To: linux-kernel, linux-efi, linux-arm-kernel, matt.fleming, linux
  Cc: leif.lindholm, Roy Franz

* Change the EFI helper functions to be more flexible to allow their use
by other architectures.
* Pass system table pointer as argument to all functions that use it,
rather than use a global variable.
* Change name of __get_map to efi_get_memory_map, and add key pointer argument
so it can be used to get final memory map before the EFI exitBootServices()
function is called.
* Change the handle_ramdisks() to handle_cmdline_files(), and make it more
generic by taking string to match rather than hardcoding 'initrd='
so it can be used to load device tree files as well.
* Add 'min' address parameter to limit low_alloc() alloctions
* Fix type of chunksize to match EFI function (not checked in x86 code
as wrappers break type checking.)
* rename alloc/free functions - low_free() is usable by both low_alloc()
and high_alloc(), so rename them to be more consistent - efi_low_alloc()
efi_high_alloc(), and efi_free()
* Enforce minimum pagesize alignment as expected by EFI allocate_pages().
* check for 0 size in efi_free.


Signed-off-by: Roy Franz <roy.franz@linaro.org>
---
 arch/x86/boot/compressed/eboot.c       |   46 ++++----
 drivers/firmware/efi/efi-stub-helper.c |  181 ++++++++++++++++++++------------
 2 files changed, 140 insertions(+), 87 deletions(-)

diff --git a/arch/x86/boot/compressed/eboot.c b/arch/x86/boot/compressed/eboot.c
index ab0eefc..d15cf47 100644
--- a/arch/x86/boot/compressed/eboot.c
+++ b/arch/x86/boot/compressed/eboot.c
@@ -443,6 +443,8 @@ struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
 	u16 *s2;
 	u8 *s1;
 	int i;
+	u64 ramdisk_addr;
+	u64 ramdisk_size;
 
 	sys_table = _table;
 
@@ -453,13 +455,14 @@ struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
 	status = efi_call_phys3(sys_table->boottime->handle_protocol,
 				handle, &proto, (void *)&image);
 	if (status != EFI_SUCCESS) {
-		efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
+		efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
 		return NULL;
 	}
 
-	status = low_alloc(0x4000, 1, (unsigned long *)&boot_params);
+	status = efi_low_alloc(sys_table, 0x4000, 1,
+			   (unsigned long *)&boot_params, 0);
 	if (status != EFI_SUCCESS) {
-		efi_printk("Failed to alloc lowmem for boot params\n");
+		efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
 		return NULL;
 	}
 
@@ -503,9 +506,10 @@ struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
 
 			options_size++;	/* NUL termination */
 
-			status = low_alloc(options_size, 1, &cmdline);
+			status = efi_low_alloc(sys_table, options_size, 1,
+					   &cmdline, 0);
 			if (status != EFI_SUCCESS) {
-				efi_printk("Failed to alloc mem for cmdline\n");
+				efi_printk(sys_table, "Failed to alloc mem for cmdline\n");
 				goto fail;
 			}
 
@@ -529,16 +533,21 @@ struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
 
 	memset(sdt, 0, sizeof(*sdt));
 
-	status = handle_ramdisks(image, hdr);
+	status = handle_cmdline_files(sys_table, image,
+				      (char *)(unsigned long)hdr->cmd_line_ptr,
+				      "initrd=", hdr->initrd_addr_max,
+				      &ramdisk_addr, &ramdisk_size);
 	if (status != EFI_SUCCESS)
 		goto fail2;
+	hdr->ramdisk_image = ramdisk_addr;
+	hdr->ramdisk_size = ramdisk_size;
 
 	return boot_params;
 fail2:
 	if (options_size)
-		low_free(options_size, hdr->cmd_line_ptr);
+		efi_free(sys_table, options_size, hdr->cmd_line_ptr);
 fail:
-	low_free(0x4000, (unsigned long)boot_params);
+	efi_free(sys_table, 0x4000, (unsigned long)boot_params);
 	return NULL;
 }
 
@@ -561,7 +570,7 @@ static efi_status_t exit_boot(struct boot_params *boot_params,
 again:
 	size += sizeof(*mem_map) * 2;
 	_size = size;
-	status = low_alloc(size, 1, (unsigned long *)&mem_map);
+	status = efi_low_alloc(sys_table, size, 1, (unsigned long *)&mem_map, 0);
 	if (status != EFI_SUCCESS)
 		return status;
 
@@ -569,7 +578,7 @@ get_map:
 	status = efi_call_phys5(sys_table->boottime->get_memory_map, &size,
 				mem_map, &key, &desc_size, &desc_version);
 	if (status == EFI_BUFFER_TOO_SMALL) {
-		low_free(_size, (unsigned long)mem_map);
+		efi_free(sys_table, _size, (unsigned long)mem_map);
 		goto again;
 	}
 
@@ -671,7 +680,7 @@ get_map:
 	return EFI_SUCCESS;
 
 free_mem_map:
-	low_free(_size, (unsigned long)mem_map);
+	efi_free(sys_table, _size, (unsigned long)mem_map);
 	return status;
 }
 
@@ -694,10 +703,10 @@ static efi_status_t relocate_kernel(struct setup_header *hdr)
 				EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
 				nr_pages, &start);
 	if (status != EFI_SUCCESS) {
-		status = low_alloc(hdr->init_size, hdr->kernel_alignment,
-				   &start);
+		status = efi_low_alloc(sys_table, hdr->init_size,
+				   hdr->kernel_alignment, &start, 0);
 		if (status != EFI_SUCCESS)
-			efi_printk("Failed to alloc mem for kernel\n");
+			efi_printk(sys_table, "Failed to alloc mem for kernel\n");
 	}
 
 	if (status == EFI_SUCCESS)
@@ -737,14 +746,15 @@ struct boot_params *efi_main(void *handle, efi_system_table_t *_table,
 				EFI_LOADER_DATA, sizeof(*gdt),
 				(void **)&gdt);
 	if (status != EFI_SUCCESS) {
-		efi_printk("Failed to alloc mem for gdt structure\n");
+		efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
 		goto fail;
 	}
 
 	gdt->size = 0x800;
-	status = low_alloc(gdt->size, 8, (unsigned long *)&gdt->address);
+	status = efi_low_alloc(sys_table, gdt->size, 8,
+			   (unsigned long *)&gdt->address, 0);
 	if (status != EFI_SUCCESS) {
-		efi_printk("Failed to alloc mem for gdt\n");
+		efi_printk(sys_table, "Failed to alloc mem for gdt\n");
 		goto fail;
 	}
 
@@ -752,7 +762,7 @@ struct boot_params *efi_main(void *handle, efi_system_table_t *_table,
 				EFI_LOADER_DATA, sizeof(*idt),
 				(void **)&idt);
 	if (status != EFI_SUCCESS) {
-		efi_printk("Failed to alloc mem for idt structure\n");
+		efi_printk(sys_table, "Failed to alloc mem for idt structure\n");
 		goto fail;
 	}
 
diff --git a/drivers/firmware/efi/efi-stub-helper.c b/drivers/firmware/efi/efi-stub-helper.c
index 47891bd..3dd9b76 100644
--- a/drivers/firmware/efi/efi-stub-helper.c
+++ b/drivers/firmware/efi/efi-stub-helper.c
@@ -11,6 +11,8 @@
  */
 
 
+#define EFI_READ_CHUNK_SIZE	(1024 * 1024)
+
 struct initrd {
 	efi_file_handle_t *handle;
 	u64 size;
@@ -19,15 +21,16 @@ struct initrd {
 
 
 
-static void efi_char16_printk(efi_char16_t *str)
+static void efi_char16_printk(efi_system_table_t *sys_table_arg,
+			      efi_char16_t *str)
 {
 	struct efi_simple_text_output_protocol *out;
 
-	out = (struct efi_simple_text_output_protocol *)sys_table->con_out;
+	out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
 	efi_call_phys2(out->output_string, out, str);
 }
 
-static void efi_printk(char *str)
+static void efi_printk(efi_system_table_t *sys_table_arg, char *str)
 {
 	char *s8;
 
@@ -37,16 +40,19 @@ static void efi_printk(char *str)
 		ch[0] = *s8;
 		if (*s8 == '\n') {
 			efi_char16_t nl[2] = { '\r', 0 };
-			efi_char16_printk(nl);
+			efi_char16_printk(sys_table_arg, nl);
 		}
 
-		efi_char16_printk(ch);
+		efi_char16_printk(sys_table_arg, ch);
 	}
 }
 
 
-static efi_status_t __get_map(efi_memory_desc_t **map, unsigned long *map_size,
-			      unsigned long *desc_size)
+static efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
+				       efi_memory_desc_t **map,
+				       unsigned long *map_size,
+				       unsigned long *desc_size,
+				       unsigned long *key_ptr)
 {
 	efi_memory_desc_t *m = NULL;
 	efi_status_t status;
@@ -60,20 +66,22 @@ again:
 	 * allocation which may be in a new descriptor region.
 	 */
 	*map_size += sizeof(*m);
-	status = efi_call_phys3(sys_table->boottime->allocate_pool,
+	status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
 				EFI_LOADER_DATA, *map_size, (void **)&m);
 	if (status != EFI_SUCCESS)
 		goto fail;
 
-	status = efi_call_phys5(sys_table->boottime->get_memory_map, map_size,
-				m, &key, desc_size, &desc_version);
+	status = efi_call_phys5(sys_table_arg->boottime->get_memory_map,
+				map_size, m, &key, desc_size, &desc_version);
 	if (status == EFI_BUFFER_TOO_SMALL) {
-		efi_call_phys1(sys_table->boottime->free_pool, m);
+		efi_call_phys1(sys_table_arg->boottime->free_pool, m);
 		goto again;
 	}
 
 	if (status != EFI_SUCCESS)
-		efi_call_phys1(sys_table->boottime->free_pool, m);
+		efi_call_phys1(sys_table_arg->boottime->free_pool, m);
+	if (key_ptr && status == EFI_SUCCESS)
+		*key_ptr = key;
 
 fail:
 	*map = m;
@@ -83,8 +91,9 @@ fail:
 /*
  * Allocate at the highest possible address that is not above 'max'.
  */
-static efi_status_t high_alloc(unsigned long size, unsigned long align,
-			      unsigned long *addr, unsigned long max)
+static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
+			       unsigned long size, unsigned long align,
+			       unsigned long *addr, unsigned long max)
 {
 	unsigned long map_size, desc_size;
 	efi_memory_desc_t *map;
@@ -93,10 +102,18 @@ static efi_status_t high_alloc(unsigned long size, unsigned long align,
 	u64 max_addr = 0;
 	int i;
 
-	status = __get_map(&map, &map_size, &desc_size);
+	status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
+				    NULL);
 	if (status != EFI_SUCCESS)
 		goto fail;
 
+	/* Enforce minimum alignment that EFI requires when requesting
+	 * a specific address.  We are doing page-based allocations,
+	 * so we must be aligned to a page.
+	 */
+	if (align < EFI_PAGE_SIZE)
+		align = EFI_PAGE_SIZE;
+
 	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
 again:
 	for (i = 0; i < map_size / desc_size; i++) {
@@ -139,7 +156,7 @@ again:
 	if (!max_addr)
 		status = EFI_NOT_FOUND;
 	else {
-		status = efi_call_phys4(sys_table->boottime->allocate_pages,
+		status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
 					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
 					nr_pages, &max_addr);
 		if (status != EFI_SUCCESS) {
@@ -151,18 +168,18 @@ again:
 		*addr = max_addr;
 	}
 
-free_pool:
-	efi_call_phys1(sys_table->boottime->free_pool, map);
+	efi_call_phys1(sys_table_arg->boottime->free_pool, map);
 
 fail:
 	return status;
 }
 
 /*
- * Allocate at the lowest possible address.
+ * Allocate at the lowest possible address, that is not below 'min'
  */
-static efi_status_t low_alloc(unsigned long size, unsigned long align,
-			      unsigned long *addr)
+static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
+			      unsigned long size, unsigned long align,
+			      unsigned long *addr, unsigned long min)
 {
 	unsigned long map_size, desc_size;
 	efi_memory_desc_t *map;
@@ -170,16 +187,23 @@ static efi_status_t low_alloc(unsigned long size, unsigned long align,
 	unsigned long nr_pages;
 	int i;
 
-	status = __get_map(&map, &map_size, &desc_size);
+	status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
+				    NULL);
 	if (status != EFI_SUCCESS)
 		goto fail;
 
+	/* Enforce minimum alignment that EFI requires when requesting
+	 * a specific address.  We are doing page-based allocations,
+	 * so we must be aligned to a page.
+	 */
+	if (align < EFI_PAGE_SIZE)
+		align = EFI_PAGE_SIZE;
+
 	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
 	for (i = 0; i < map_size / desc_size; i++) {
 		efi_memory_desc_t *desc;
 		unsigned long m = (unsigned long)map;
 		u64 start, end;
-
 		desc = (efi_memory_desc_t *)(m + (i * desc_size));
 
 		if (desc->type != EFI_CONVENTIONAL_MEMORY)
@@ -199,11 +223,14 @@ static efi_status_t low_alloc(unsigned long size, unsigned long align,
 		if (start == 0x0)
 			start += 8;
 
+		if (start < min)
+			start = min;
+
 		start = round_up(start, align);
 		if ((start + size) > end)
 			continue;
 
-		status = efi_call_phys4(sys_table->boottime->allocate_pages,
+		status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
 					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
 					nr_pages, &start);
 		if (status == EFI_SUCCESS) {
@@ -215,18 +242,21 @@ static efi_status_t low_alloc(unsigned long size, unsigned long align,
 	if (i == map_size / desc_size)
 		status = EFI_NOT_FOUND;
 
-free_pool:
-	efi_call_phys1(sys_table->boottime->free_pool, map);
+	efi_call_phys1(sys_table_arg->boottime->free_pool, map);
 fail:
 	return status;
 }
 
-static void low_free(unsigned long size, unsigned long addr)
+static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
+		     unsigned long addr)
 {
 	unsigned long nr_pages;
 
+	if (!size)
+		return;
+
 	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
-	efi_call_phys2(sys_table->boottime->free_pages, addr, nr_pages);
+	efi_call_phys2(sys_table_arg->boottime->free_pages, addr, nr_pages);
 }
 
 
@@ -236,8 +266,11 @@ static void low_free(unsigned long size, unsigned long addr)
  * We only support loading an initrd from the same filesystem as the
  * kernel image.
  */
-static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
-				    struct setup_header *hdr)
+static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
+					 efi_loaded_image_t *image,
+					 char *cmd_line, char *option_string,
+					 u64 max_addr,
+					 u64 *load_addr, u64 *load_size)
 {
 	struct initrd *initrds;
 	unsigned long initrd_addr;
@@ -253,19 +286,25 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 	initrd_addr = 0;
 	initrd_total = 0;
 
-	str = (char *)(unsigned long)hdr->cmd_line_ptr;
+	str = cmd_line;
 
 	j = 0;			/* See close_handles */
 
+	if (!load_addr || !load_size)
+		return EFI_INVALID_PARAMETER;
+
+	*load_addr = 0;
+	*load_size = 0;
+
 	if (!str || !*str)
 		return EFI_SUCCESS;
 
 	for (nr_initrds = 0; *str; nr_initrds++) {
-		str = strstr(str, "initrd=");
+		str = strstr(str, option_string);
 		if (!str)
 			break;
 
-		str += 7;
+		str += strlen(option_string);
 
 		/* Skip any leading slashes */
 		while (*str == '/' || *str == '\\')
@@ -278,16 +317,16 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 	if (!nr_initrds)
 		return EFI_SUCCESS;
 
-	status = efi_call_phys3(sys_table->boottime->allocate_pool,
+	status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
 				EFI_LOADER_DATA,
 				nr_initrds * sizeof(*initrds),
-				&initrds);
+				(void **)&initrds);
 	if (status != EFI_SUCCESS) {
-		efi_printk("Failed to alloc mem for initrds\n");
+		efi_printk(sys_table_arg, "Failed to alloc mem for file load\n");
 		goto fail;
 	}
 
-	str = (char *)(unsigned long)hdr->cmd_line_ptr;
+	str = cmd_line;
 	for (i = 0; i < nr_initrds; i++) {
 		struct initrd *initrd;
 		efi_file_handle_t *h;
@@ -298,11 +337,11 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 		efi_char16_t *p;
 		u64 file_sz;
 
-		str = strstr(str, "initrd=");
+		str = strstr(str, option_string);
 		if (!str)
 			break;
 
-		str += 7;
+		str += strlen(option_string);
 
 		initrd = &initrds[i];
 		p = filename_16;
@@ -317,7 +356,7 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 
 			if (*str == '/') {
 				*p++ = '\\';
-				*str++;
+				str++;
 			} else {
 				*p++ = *str++;
 			}
@@ -329,18 +368,19 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 		if (!i) {
 			efi_boot_services_t *boottime;
 
-			boottime = sys_table->boottime;
+			boottime = sys_table_arg->boottime;
 
 			status = efi_call_phys3(boottime->handle_protocol,
-					image->device_handle, &fs_proto, &io);
+						image->device_handle, &fs_proto,
+						(void **)&io);
 			if (status != EFI_SUCCESS) {
-				efi_printk("Failed to handle fs_proto\n");
+				efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
 				goto free_initrds;
 			}
 
 			status = efi_call_phys2(io->open_volume, io, &fh);
 			if (status != EFI_SUCCESS) {
-				efi_printk("Failed to open volume\n");
+				efi_printk(sys_table_arg, "Failed to open volume\n");
 				goto free_initrds;
 			}
 		}
@@ -348,9 +388,9 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 		status = efi_call_phys5(fh->open, fh, &h, filename_16,
 					EFI_FILE_MODE_READ, (u64)0);
 		if (status != EFI_SUCCESS) {
-			efi_printk("Failed to open initrd file: ");
-			efi_char16_printk(filename_16);
-			efi_printk("\n");
+			efi_printk(sys_table_arg, "Failed to open file file: ");
+			efi_char16_printk(sys_table_arg, filename_16);
+			efi_printk(sys_table_arg, "\n");
 			goto close_handles;
 		}
 
@@ -360,30 +400,32 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 		status = efi_call_phys4(h->get_info, h, &info_guid,
 					&info_sz, NULL);
 		if (status != EFI_BUFFER_TOO_SMALL) {
-			efi_printk("Failed to get initrd info size\n");
+			efi_printk(sys_table_arg, "Failed to get file info size\n");
 			goto close_handles;
 		}
 
 grow:
-		status = efi_call_phys3(sys_table->boottime->allocate_pool,
-					EFI_LOADER_DATA, info_sz, &info);
+		status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
+					EFI_LOADER_DATA, info_sz,
+					(void **)&info);
 		if (status != EFI_SUCCESS) {
-			efi_printk("Failed to alloc mem for initrd info\n");
+			efi_printk(sys_table_arg, "Failed to alloc mem for file info\n");
 			goto close_handles;
 		}
 
 		status = efi_call_phys4(h->get_info, h, &info_guid,
 					&info_sz, info);
 		if (status == EFI_BUFFER_TOO_SMALL) {
-			efi_call_phys1(sys_table->boottime->free_pool, info);
+			efi_call_phys1(sys_table_arg->boottime->free_pool,
+				       info);
 			goto grow;
 		}
 
 		file_sz = info->file_size;
-		efi_call_phys1(sys_table->boottime->free_pool, info);
+		efi_call_phys1(sys_table_arg->boottime->free_pool, info);
 
 		if (status != EFI_SUCCESS) {
-			efi_printk("Failed to get initrd info\n");
+			efi_printk(sys_table_arg, "Failed to get file info\n");
 			goto close_handles;
 		}
 
@@ -399,16 +441,16 @@ grow:
 		 * addresses in memory, so allocate enough memory for
 		 * all the initrd's.
 		 */
-		status = high_alloc(initrd_total, 0x1000,
-				   &initrd_addr, hdr->initrd_addr_max);
+		status = efi_high_alloc(sys_table_arg, initrd_total, 0x1000,
+				    &initrd_addr, max_addr);
 		if (status != EFI_SUCCESS) {
-			efi_printk("Failed to alloc highmem for initrds\n");
+			efi_printk(sys_table_arg, "Failed to alloc highmem for initrds\n");
 			goto close_handles;
 		}
 
 		/* We've run out of free low memory. */
-		if (initrd_addr > hdr->initrd_addr_max) {
-			efi_printk("We've run out of free low memory\n");
+		if (initrd_addr > max_addr) {
+			efi_printk(sys_table_arg, "We've run out of free low memory\n");
 			status = EFI_INVALID_PARAMETER;
 			goto free_initrd_total;
 		}
@@ -419,16 +461,17 @@ grow:
 
 			size = initrds[j].size;
 			while (size) {
-				u64 chunksize;
+				unsigned long chunksize;
 				if (size > EFI_READ_CHUNK_SIZE)
 					chunksize = EFI_READ_CHUNK_SIZE;
 				else
 					chunksize = size;
 				status = efi_call_phys3(fh->read,
 							initrds[j].handle,
-							&chunksize, addr);
+							&chunksize,
+							(void *)addr);
 				if (status != EFI_SUCCESS) {
-					efi_printk("Failed to read initrd\n");
+					efi_printk(sys_table_arg, "Failed to read file\n");
 					goto free_initrd_total;
 				}
 				addr += chunksize;
@@ -440,24 +483,24 @@ grow:
 
 	}
 
-	efi_call_phys1(sys_table->boottime->free_pool, initrds);
+	efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
 
-	hdr->ramdisk_image = initrd_addr;
-	hdr->ramdisk_size = initrd_total;
+	*load_addr = initrd_addr;
+	*load_size = initrd_total;
 
 	return status;
 
 free_initrd_total:
-	low_free(initrd_total, initrd_addr);
+	efi_free(sys_table_arg, initrd_total, initrd_addr);
 
 close_handles:
 	for (k = j; k < i; k++)
 		efi_call_phys1(fh->close, initrds[k].handle);
 free_initrds:
-	efi_call_phys1(sys_table->boottime->free_pool, initrds);
+	efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
 fail:
-	hdr->ramdisk_image = 0;
-	hdr->ramdisk_size = 0;
+	*load_addr = 0;
+	*load_size = 0;
 
 	return status;
 }
-- 
1.7.10.4


^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH 3/7] Change EFI helper APIs to be more flexible
@ 2013-08-02 21:29   ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-02 21:29 UTC (permalink / raw)
  To: linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-efi-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matt.fleming-ral2JQCrhuEAvxtiuMwx3w,
	linux-lFZ/pmaqli7XmaaqVzeoHQ
  Cc: leif.lindholm-QSEj5FYQhm4dnm+yROfE0A, Roy Franz

* Change the EFI helper functions to be more flexible to allow their use
by other architectures.
* Pass system table pointer as argument to all functions that use it,
rather than use a global variable.
* Change name of __get_map to efi_get_memory_map, and add key pointer argument
so it can be used to get final memory map before the EFI exitBootServices()
function is called.
* Change the handle_ramdisks() to handle_cmdline_files(), and make it more
generic by taking string to match rather than hardcoding 'initrd='
so it can be used to load device tree files as well.
* Add 'min' address parameter to limit low_alloc() alloctions
* Fix type of chunksize to match EFI function (not checked in x86 code
as wrappers break type checking.)
* rename alloc/free functions - low_free() is usable by both low_alloc()
and high_alloc(), so rename them to be more consistent - efi_low_alloc()
efi_high_alloc(), and efi_free()
* Enforce minimum pagesize alignment as expected by EFI allocate_pages().
* check for 0 size in efi_free.


Signed-off-by: Roy Franz <roy.franz-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
---
 arch/x86/boot/compressed/eboot.c       |   46 ++++----
 drivers/firmware/efi/efi-stub-helper.c |  181 ++++++++++++++++++++------------
 2 files changed, 140 insertions(+), 87 deletions(-)

diff --git a/arch/x86/boot/compressed/eboot.c b/arch/x86/boot/compressed/eboot.c
index ab0eefc..d15cf47 100644
--- a/arch/x86/boot/compressed/eboot.c
+++ b/arch/x86/boot/compressed/eboot.c
@@ -443,6 +443,8 @@ struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
 	u16 *s2;
 	u8 *s1;
 	int i;
+	u64 ramdisk_addr;
+	u64 ramdisk_size;
 
 	sys_table = _table;
 
@@ -453,13 +455,14 @@ struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
 	status = efi_call_phys3(sys_table->boottime->handle_protocol,
 				handle, &proto, (void *)&image);
 	if (status != EFI_SUCCESS) {
-		efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
+		efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
 		return NULL;
 	}
 
-	status = low_alloc(0x4000, 1, (unsigned long *)&boot_params);
+	status = efi_low_alloc(sys_table, 0x4000, 1,
+			   (unsigned long *)&boot_params, 0);
 	if (status != EFI_SUCCESS) {
-		efi_printk("Failed to alloc lowmem for boot params\n");
+		efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
 		return NULL;
 	}
 
@@ -503,9 +506,10 @@ struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
 
 			options_size++;	/* NUL termination */
 
-			status = low_alloc(options_size, 1, &cmdline);
+			status = efi_low_alloc(sys_table, options_size, 1,
+					   &cmdline, 0);
 			if (status != EFI_SUCCESS) {
-				efi_printk("Failed to alloc mem for cmdline\n");
+				efi_printk(sys_table, "Failed to alloc mem for cmdline\n");
 				goto fail;
 			}
 
@@ -529,16 +533,21 @@ struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
 
 	memset(sdt, 0, sizeof(*sdt));
 
-	status = handle_ramdisks(image, hdr);
+	status = handle_cmdline_files(sys_table, image,
+				      (char *)(unsigned long)hdr->cmd_line_ptr,
+				      "initrd=", hdr->initrd_addr_max,
+				      &ramdisk_addr, &ramdisk_size);
 	if (status != EFI_SUCCESS)
 		goto fail2;
+	hdr->ramdisk_image = ramdisk_addr;
+	hdr->ramdisk_size = ramdisk_size;
 
 	return boot_params;
 fail2:
 	if (options_size)
-		low_free(options_size, hdr->cmd_line_ptr);
+		efi_free(sys_table, options_size, hdr->cmd_line_ptr);
 fail:
-	low_free(0x4000, (unsigned long)boot_params);
+	efi_free(sys_table, 0x4000, (unsigned long)boot_params);
 	return NULL;
 }
 
@@ -561,7 +570,7 @@ static efi_status_t exit_boot(struct boot_params *boot_params,
 again:
 	size += sizeof(*mem_map) * 2;
 	_size = size;
-	status = low_alloc(size, 1, (unsigned long *)&mem_map);
+	status = efi_low_alloc(sys_table, size, 1, (unsigned long *)&mem_map, 0);
 	if (status != EFI_SUCCESS)
 		return status;
 
@@ -569,7 +578,7 @@ get_map:
 	status = efi_call_phys5(sys_table->boottime->get_memory_map, &size,
 				mem_map, &key, &desc_size, &desc_version);
 	if (status == EFI_BUFFER_TOO_SMALL) {
-		low_free(_size, (unsigned long)mem_map);
+		efi_free(sys_table, _size, (unsigned long)mem_map);
 		goto again;
 	}
 
@@ -671,7 +680,7 @@ get_map:
 	return EFI_SUCCESS;
 
 free_mem_map:
-	low_free(_size, (unsigned long)mem_map);
+	efi_free(sys_table, _size, (unsigned long)mem_map);
 	return status;
 }
 
@@ -694,10 +703,10 @@ static efi_status_t relocate_kernel(struct setup_header *hdr)
 				EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
 				nr_pages, &start);
 	if (status != EFI_SUCCESS) {
-		status = low_alloc(hdr->init_size, hdr->kernel_alignment,
-				   &start);
+		status = efi_low_alloc(sys_table, hdr->init_size,
+				   hdr->kernel_alignment, &start, 0);
 		if (status != EFI_SUCCESS)
-			efi_printk("Failed to alloc mem for kernel\n");
+			efi_printk(sys_table, "Failed to alloc mem for kernel\n");
 	}
 
 	if (status == EFI_SUCCESS)
@@ -737,14 +746,15 @@ struct boot_params *efi_main(void *handle, efi_system_table_t *_table,
 				EFI_LOADER_DATA, sizeof(*gdt),
 				(void **)&gdt);
 	if (status != EFI_SUCCESS) {
-		efi_printk("Failed to alloc mem for gdt structure\n");
+		efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
 		goto fail;
 	}
 
 	gdt->size = 0x800;
-	status = low_alloc(gdt->size, 8, (unsigned long *)&gdt->address);
+	status = efi_low_alloc(sys_table, gdt->size, 8,
+			   (unsigned long *)&gdt->address, 0);
 	if (status != EFI_SUCCESS) {
-		efi_printk("Failed to alloc mem for gdt\n");
+		efi_printk(sys_table, "Failed to alloc mem for gdt\n");
 		goto fail;
 	}
 
@@ -752,7 +762,7 @@ struct boot_params *efi_main(void *handle, efi_system_table_t *_table,
 				EFI_LOADER_DATA, sizeof(*idt),
 				(void **)&idt);
 	if (status != EFI_SUCCESS) {
-		efi_printk("Failed to alloc mem for idt structure\n");
+		efi_printk(sys_table, "Failed to alloc mem for idt structure\n");
 		goto fail;
 	}
 
diff --git a/drivers/firmware/efi/efi-stub-helper.c b/drivers/firmware/efi/efi-stub-helper.c
index 47891bd..3dd9b76 100644
--- a/drivers/firmware/efi/efi-stub-helper.c
+++ b/drivers/firmware/efi/efi-stub-helper.c
@@ -11,6 +11,8 @@
  */
 
 
+#define EFI_READ_CHUNK_SIZE	(1024 * 1024)
+
 struct initrd {
 	efi_file_handle_t *handle;
 	u64 size;
@@ -19,15 +21,16 @@ struct initrd {
 
 
 
-static void efi_char16_printk(efi_char16_t *str)
+static void efi_char16_printk(efi_system_table_t *sys_table_arg,
+			      efi_char16_t *str)
 {
 	struct efi_simple_text_output_protocol *out;
 
-	out = (struct efi_simple_text_output_protocol *)sys_table->con_out;
+	out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
 	efi_call_phys2(out->output_string, out, str);
 }
 
-static void efi_printk(char *str)
+static void efi_printk(efi_system_table_t *sys_table_arg, char *str)
 {
 	char *s8;
 
@@ -37,16 +40,19 @@ static void efi_printk(char *str)
 		ch[0] = *s8;
 		if (*s8 == '\n') {
 			efi_char16_t nl[2] = { '\r', 0 };
-			efi_char16_printk(nl);
+			efi_char16_printk(sys_table_arg, nl);
 		}
 
-		efi_char16_printk(ch);
+		efi_char16_printk(sys_table_arg, ch);
 	}
 }
 
 
-static efi_status_t __get_map(efi_memory_desc_t **map, unsigned long *map_size,
-			      unsigned long *desc_size)
+static efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
+				       efi_memory_desc_t **map,
+				       unsigned long *map_size,
+				       unsigned long *desc_size,
+				       unsigned long *key_ptr)
 {
 	efi_memory_desc_t *m = NULL;
 	efi_status_t status;
@@ -60,20 +66,22 @@ again:
 	 * allocation which may be in a new descriptor region.
 	 */
 	*map_size += sizeof(*m);
-	status = efi_call_phys3(sys_table->boottime->allocate_pool,
+	status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
 				EFI_LOADER_DATA, *map_size, (void **)&m);
 	if (status != EFI_SUCCESS)
 		goto fail;
 
-	status = efi_call_phys5(sys_table->boottime->get_memory_map, map_size,
-				m, &key, desc_size, &desc_version);
+	status = efi_call_phys5(sys_table_arg->boottime->get_memory_map,
+				map_size, m, &key, desc_size, &desc_version);
 	if (status == EFI_BUFFER_TOO_SMALL) {
-		efi_call_phys1(sys_table->boottime->free_pool, m);
+		efi_call_phys1(sys_table_arg->boottime->free_pool, m);
 		goto again;
 	}
 
 	if (status != EFI_SUCCESS)
-		efi_call_phys1(sys_table->boottime->free_pool, m);
+		efi_call_phys1(sys_table_arg->boottime->free_pool, m);
+	if (key_ptr && status == EFI_SUCCESS)
+		*key_ptr = key;
 
 fail:
 	*map = m;
@@ -83,8 +91,9 @@ fail:
 /*
  * Allocate at the highest possible address that is not above 'max'.
  */
-static efi_status_t high_alloc(unsigned long size, unsigned long align,
-			      unsigned long *addr, unsigned long max)
+static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
+			       unsigned long size, unsigned long align,
+			       unsigned long *addr, unsigned long max)
 {
 	unsigned long map_size, desc_size;
 	efi_memory_desc_t *map;
@@ -93,10 +102,18 @@ static efi_status_t high_alloc(unsigned long size, unsigned long align,
 	u64 max_addr = 0;
 	int i;
 
-	status = __get_map(&map, &map_size, &desc_size);
+	status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
+				    NULL);
 	if (status != EFI_SUCCESS)
 		goto fail;
 
+	/* Enforce minimum alignment that EFI requires when requesting
+	 * a specific address.  We are doing page-based allocations,
+	 * so we must be aligned to a page.
+	 */
+	if (align < EFI_PAGE_SIZE)
+		align = EFI_PAGE_SIZE;
+
 	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
 again:
 	for (i = 0; i < map_size / desc_size; i++) {
@@ -139,7 +156,7 @@ again:
 	if (!max_addr)
 		status = EFI_NOT_FOUND;
 	else {
-		status = efi_call_phys4(sys_table->boottime->allocate_pages,
+		status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
 					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
 					nr_pages, &max_addr);
 		if (status != EFI_SUCCESS) {
@@ -151,18 +168,18 @@ again:
 		*addr = max_addr;
 	}
 
-free_pool:
-	efi_call_phys1(sys_table->boottime->free_pool, map);
+	efi_call_phys1(sys_table_arg->boottime->free_pool, map);
 
 fail:
 	return status;
 }
 
 /*
- * Allocate at the lowest possible address.
+ * Allocate at the lowest possible address, that is not below 'min'
  */
-static efi_status_t low_alloc(unsigned long size, unsigned long align,
-			      unsigned long *addr)
+static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
+			      unsigned long size, unsigned long align,
+			      unsigned long *addr, unsigned long min)
 {
 	unsigned long map_size, desc_size;
 	efi_memory_desc_t *map;
@@ -170,16 +187,23 @@ static efi_status_t low_alloc(unsigned long size, unsigned long align,
 	unsigned long nr_pages;
 	int i;
 
-	status = __get_map(&map, &map_size, &desc_size);
+	status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
+				    NULL);
 	if (status != EFI_SUCCESS)
 		goto fail;
 
+	/* Enforce minimum alignment that EFI requires when requesting
+	 * a specific address.  We are doing page-based allocations,
+	 * so we must be aligned to a page.
+	 */
+	if (align < EFI_PAGE_SIZE)
+		align = EFI_PAGE_SIZE;
+
 	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
 	for (i = 0; i < map_size / desc_size; i++) {
 		efi_memory_desc_t *desc;
 		unsigned long m = (unsigned long)map;
 		u64 start, end;
-
 		desc = (efi_memory_desc_t *)(m + (i * desc_size));
 
 		if (desc->type != EFI_CONVENTIONAL_MEMORY)
@@ -199,11 +223,14 @@ static efi_status_t low_alloc(unsigned long size, unsigned long align,
 		if (start == 0x0)
 			start += 8;
 
+		if (start < min)
+			start = min;
+
 		start = round_up(start, align);
 		if ((start + size) > end)
 			continue;
 
-		status = efi_call_phys4(sys_table->boottime->allocate_pages,
+		status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
 					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
 					nr_pages, &start);
 		if (status == EFI_SUCCESS) {
@@ -215,18 +242,21 @@ static efi_status_t low_alloc(unsigned long size, unsigned long align,
 	if (i == map_size / desc_size)
 		status = EFI_NOT_FOUND;
 
-free_pool:
-	efi_call_phys1(sys_table->boottime->free_pool, map);
+	efi_call_phys1(sys_table_arg->boottime->free_pool, map);
 fail:
 	return status;
 }
 
-static void low_free(unsigned long size, unsigned long addr)
+static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
+		     unsigned long addr)
 {
 	unsigned long nr_pages;
 
+	if (!size)
+		return;
+
 	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
-	efi_call_phys2(sys_table->boottime->free_pages, addr, nr_pages);
+	efi_call_phys2(sys_table_arg->boottime->free_pages, addr, nr_pages);
 }
 
 
@@ -236,8 +266,11 @@ static void low_free(unsigned long size, unsigned long addr)
  * We only support loading an initrd from the same filesystem as the
  * kernel image.
  */
-static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
-				    struct setup_header *hdr)
+static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
+					 efi_loaded_image_t *image,
+					 char *cmd_line, char *option_string,
+					 u64 max_addr,
+					 u64 *load_addr, u64 *load_size)
 {
 	struct initrd *initrds;
 	unsigned long initrd_addr;
@@ -253,19 +286,25 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 	initrd_addr = 0;
 	initrd_total = 0;
 
-	str = (char *)(unsigned long)hdr->cmd_line_ptr;
+	str = cmd_line;
 
 	j = 0;			/* See close_handles */
 
+	if (!load_addr || !load_size)
+		return EFI_INVALID_PARAMETER;
+
+	*load_addr = 0;
+	*load_size = 0;
+
 	if (!str || !*str)
 		return EFI_SUCCESS;
 
 	for (nr_initrds = 0; *str; nr_initrds++) {
-		str = strstr(str, "initrd=");
+		str = strstr(str, option_string);
 		if (!str)
 			break;
 
-		str += 7;
+		str += strlen(option_string);
 
 		/* Skip any leading slashes */
 		while (*str == '/' || *str == '\\')
@@ -278,16 +317,16 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 	if (!nr_initrds)
 		return EFI_SUCCESS;
 
-	status = efi_call_phys3(sys_table->boottime->allocate_pool,
+	status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
 				EFI_LOADER_DATA,
 				nr_initrds * sizeof(*initrds),
-				&initrds);
+				(void **)&initrds);
 	if (status != EFI_SUCCESS) {
-		efi_printk("Failed to alloc mem for initrds\n");
+		efi_printk(sys_table_arg, "Failed to alloc mem for file load\n");
 		goto fail;
 	}
 
-	str = (char *)(unsigned long)hdr->cmd_line_ptr;
+	str = cmd_line;
 	for (i = 0; i < nr_initrds; i++) {
 		struct initrd *initrd;
 		efi_file_handle_t *h;
@@ -298,11 +337,11 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 		efi_char16_t *p;
 		u64 file_sz;
 
-		str = strstr(str, "initrd=");
+		str = strstr(str, option_string);
 		if (!str)
 			break;
 
-		str += 7;
+		str += strlen(option_string);
 
 		initrd = &initrds[i];
 		p = filename_16;
@@ -317,7 +356,7 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 
 			if (*str == '/') {
 				*p++ = '\\';
-				*str++;
+				str++;
 			} else {
 				*p++ = *str++;
 			}
@@ -329,18 +368,19 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 		if (!i) {
 			efi_boot_services_t *boottime;
 
-			boottime = sys_table->boottime;
+			boottime = sys_table_arg->boottime;
 
 			status = efi_call_phys3(boottime->handle_protocol,
-					image->device_handle, &fs_proto, &io);
+						image->device_handle, &fs_proto,
+						(void **)&io);
 			if (status != EFI_SUCCESS) {
-				efi_printk("Failed to handle fs_proto\n");
+				efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
 				goto free_initrds;
 			}
 
 			status = efi_call_phys2(io->open_volume, io, &fh);
 			if (status != EFI_SUCCESS) {
-				efi_printk("Failed to open volume\n");
+				efi_printk(sys_table_arg, "Failed to open volume\n");
 				goto free_initrds;
 			}
 		}
@@ -348,9 +388,9 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 		status = efi_call_phys5(fh->open, fh, &h, filename_16,
 					EFI_FILE_MODE_READ, (u64)0);
 		if (status != EFI_SUCCESS) {
-			efi_printk("Failed to open initrd file: ");
-			efi_char16_printk(filename_16);
-			efi_printk("\n");
+			efi_printk(sys_table_arg, "Failed to open file file: ");
+			efi_char16_printk(sys_table_arg, filename_16);
+			efi_printk(sys_table_arg, "\n");
 			goto close_handles;
 		}
 
@@ -360,30 +400,32 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 		status = efi_call_phys4(h->get_info, h, &info_guid,
 					&info_sz, NULL);
 		if (status != EFI_BUFFER_TOO_SMALL) {
-			efi_printk("Failed to get initrd info size\n");
+			efi_printk(sys_table_arg, "Failed to get file info size\n");
 			goto close_handles;
 		}
 
 grow:
-		status = efi_call_phys3(sys_table->boottime->allocate_pool,
-					EFI_LOADER_DATA, info_sz, &info);
+		status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
+					EFI_LOADER_DATA, info_sz,
+					(void **)&info);
 		if (status != EFI_SUCCESS) {
-			efi_printk("Failed to alloc mem for initrd info\n");
+			efi_printk(sys_table_arg, "Failed to alloc mem for file info\n");
 			goto close_handles;
 		}
 
 		status = efi_call_phys4(h->get_info, h, &info_guid,
 					&info_sz, info);
 		if (status == EFI_BUFFER_TOO_SMALL) {
-			efi_call_phys1(sys_table->boottime->free_pool, info);
+			efi_call_phys1(sys_table_arg->boottime->free_pool,
+				       info);
 			goto grow;
 		}
 
 		file_sz = info->file_size;
-		efi_call_phys1(sys_table->boottime->free_pool, info);
+		efi_call_phys1(sys_table_arg->boottime->free_pool, info);
 
 		if (status != EFI_SUCCESS) {
-			efi_printk("Failed to get initrd info\n");
+			efi_printk(sys_table_arg, "Failed to get file info\n");
 			goto close_handles;
 		}
 
@@ -399,16 +441,16 @@ grow:
 		 * addresses in memory, so allocate enough memory for
 		 * all the initrd's.
 		 */
-		status = high_alloc(initrd_total, 0x1000,
-				   &initrd_addr, hdr->initrd_addr_max);
+		status = efi_high_alloc(sys_table_arg, initrd_total, 0x1000,
+				    &initrd_addr, max_addr);
 		if (status != EFI_SUCCESS) {
-			efi_printk("Failed to alloc highmem for initrds\n");
+			efi_printk(sys_table_arg, "Failed to alloc highmem for initrds\n");
 			goto close_handles;
 		}
 
 		/* We've run out of free low memory. */
-		if (initrd_addr > hdr->initrd_addr_max) {
-			efi_printk("We've run out of free low memory\n");
+		if (initrd_addr > max_addr) {
+			efi_printk(sys_table_arg, "We've run out of free low memory\n");
 			status = EFI_INVALID_PARAMETER;
 			goto free_initrd_total;
 		}
@@ -419,16 +461,17 @@ grow:
 
 			size = initrds[j].size;
 			while (size) {
-				u64 chunksize;
+				unsigned long chunksize;
 				if (size > EFI_READ_CHUNK_SIZE)
 					chunksize = EFI_READ_CHUNK_SIZE;
 				else
 					chunksize = size;
 				status = efi_call_phys3(fh->read,
 							initrds[j].handle,
-							&chunksize, addr);
+							&chunksize,
+							(void *)addr);
 				if (status != EFI_SUCCESS) {
-					efi_printk("Failed to read initrd\n");
+					efi_printk(sys_table_arg, "Failed to read file\n");
 					goto free_initrd_total;
 				}
 				addr += chunksize;
@@ -440,24 +483,24 @@ grow:
 
 	}
 
-	efi_call_phys1(sys_table->boottime->free_pool, initrds);
+	efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
 
-	hdr->ramdisk_image = initrd_addr;
-	hdr->ramdisk_size = initrd_total;
+	*load_addr = initrd_addr;
+	*load_size = initrd_total;
 
 	return status;
 
 free_initrd_total:
-	low_free(initrd_total, initrd_addr);
+	efi_free(sys_table_arg, initrd_total, initrd_addr);
 
 close_handles:
 	for (k = j; k < i; k++)
 		efi_call_phys1(fh->close, initrds[k].handle);
 free_initrds:
-	efi_call_phys1(sys_table->boottime->free_pool, initrds);
+	efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
 fail:
-	hdr->ramdisk_image = 0;
-	hdr->ramdisk_size = 0;
+	*load_addr = 0;
+	*load_size = 0;
 
 	return status;
 }
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH 3/7] Change EFI helper APIs to be more flexible
@ 2013-08-02 21:29   ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-02 21:29 UTC (permalink / raw)
  To: linux-arm-kernel

* Change the EFI helper functions to be more flexible to allow their use
by other architectures.
* Pass system table pointer as argument to all functions that use it,
rather than use a global variable.
* Change name of __get_map to efi_get_memory_map, and add key pointer argument
so it can be used to get final memory map before the EFI exitBootServices()
function is called.
* Change the handle_ramdisks() to handle_cmdline_files(), and make it more
generic by taking string to match rather than hardcoding 'initrd='
so it can be used to load device tree files as well.
* Add 'min' address parameter to limit low_alloc() alloctions
* Fix type of chunksize to match EFI function (not checked in x86 code
as wrappers break type checking.)
* rename alloc/free functions - low_free() is usable by both low_alloc()
and high_alloc(), so rename them to be more consistent - efi_low_alloc()
efi_high_alloc(), and efi_free()
* Enforce minimum pagesize alignment as expected by EFI allocate_pages().
* check for 0 size in efi_free.


Signed-off-by: Roy Franz <roy.franz@linaro.org>
---
 arch/x86/boot/compressed/eboot.c       |   46 ++++----
 drivers/firmware/efi/efi-stub-helper.c |  181 ++++++++++++++++++++------------
 2 files changed, 140 insertions(+), 87 deletions(-)

diff --git a/arch/x86/boot/compressed/eboot.c b/arch/x86/boot/compressed/eboot.c
index ab0eefc..d15cf47 100644
--- a/arch/x86/boot/compressed/eboot.c
+++ b/arch/x86/boot/compressed/eboot.c
@@ -443,6 +443,8 @@ struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
 	u16 *s2;
 	u8 *s1;
 	int i;
+	u64 ramdisk_addr;
+	u64 ramdisk_size;
 
 	sys_table = _table;
 
@@ -453,13 +455,14 @@ struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
 	status = efi_call_phys3(sys_table->boottime->handle_protocol,
 				handle, &proto, (void *)&image);
 	if (status != EFI_SUCCESS) {
-		efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
+		efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
 		return NULL;
 	}
 
-	status = low_alloc(0x4000, 1, (unsigned long *)&boot_params);
+	status = efi_low_alloc(sys_table, 0x4000, 1,
+			   (unsigned long *)&boot_params, 0);
 	if (status != EFI_SUCCESS) {
-		efi_printk("Failed to alloc lowmem for boot params\n");
+		efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
 		return NULL;
 	}
 
@@ -503,9 +506,10 @@ struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
 
 			options_size++;	/* NUL termination */
 
-			status = low_alloc(options_size, 1, &cmdline);
+			status = efi_low_alloc(sys_table, options_size, 1,
+					   &cmdline, 0);
 			if (status != EFI_SUCCESS) {
-				efi_printk("Failed to alloc mem for cmdline\n");
+				efi_printk(sys_table, "Failed to alloc mem for cmdline\n");
 				goto fail;
 			}
 
@@ -529,16 +533,21 @@ struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
 
 	memset(sdt, 0, sizeof(*sdt));
 
-	status = handle_ramdisks(image, hdr);
+	status = handle_cmdline_files(sys_table, image,
+				      (char *)(unsigned long)hdr->cmd_line_ptr,
+				      "initrd=", hdr->initrd_addr_max,
+				      &ramdisk_addr, &ramdisk_size);
 	if (status != EFI_SUCCESS)
 		goto fail2;
+	hdr->ramdisk_image = ramdisk_addr;
+	hdr->ramdisk_size = ramdisk_size;
 
 	return boot_params;
 fail2:
 	if (options_size)
-		low_free(options_size, hdr->cmd_line_ptr);
+		efi_free(sys_table, options_size, hdr->cmd_line_ptr);
 fail:
-	low_free(0x4000, (unsigned long)boot_params);
+	efi_free(sys_table, 0x4000, (unsigned long)boot_params);
 	return NULL;
 }
 
@@ -561,7 +570,7 @@ static efi_status_t exit_boot(struct boot_params *boot_params,
 again:
 	size += sizeof(*mem_map) * 2;
 	_size = size;
-	status = low_alloc(size, 1, (unsigned long *)&mem_map);
+	status = efi_low_alloc(sys_table, size, 1, (unsigned long *)&mem_map, 0);
 	if (status != EFI_SUCCESS)
 		return status;
 
@@ -569,7 +578,7 @@ get_map:
 	status = efi_call_phys5(sys_table->boottime->get_memory_map, &size,
 				mem_map, &key, &desc_size, &desc_version);
 	if (status == EFI_BUFFER_TOO_SMALL) {
-		low_free(_size, (unsigned long)mem_map);
+		efi_free(sys_table, _size, (unsigned long)mem_map);
 		goto again;
 	}
 
@@ -671,7 +680,7 @@ get_map:
 	return EFI_SUCCESS;
 
 free_mem_map:
-	low_free(_size, (unsigned long)mem_map);
+	efi_free(sys_table, _size, (unsigned long)mem_map);
 	return status;
 }
 
@@ -694,10 +703,10 @@ static efi_status_t relocate_kernel(struct setup_header *hdr)
 				EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
 				nr_pages, &start);
 	if (status != EFI_SUCCESS) {
-		status = low_alloc(hdr->init_size, hdr->kernel_alignment,
-				   &start);
+		status = efi_low_alloc(sys_table, hdr->init_size,
+				   hdr->kernel_alignment, &start, 0);
 		if (status != EFI_SUCCESS)
-			efi_printk("Failed to alloc mem for kernel\n");
+			efi_printk(sys_table, "Failed to alloc mem for kernel\n");
 	}
 
 	if (status == EFI_SUCCESS)
@@ -737,14 +746,15 @@ struct boot_params *efi_main(void *handle, efi_system_table_t *_table,
 				EFI_LOADER_DATA, sizeof(*gdt),
 				(void **)&gdt);
 	if (status != EFI_SUCCESS) {
-		efi_printk("Failed to alloc mem for gdt structure\n");
+		efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
 		goto fail;
 	}
 
 	gdt->size = 0x800;
-	status = low_alloc(gdt->size, 8, (unsigned long *)&gdt->address);
+	status = efi_low_alloc(sys_table, gdt->size, 8,
+			   (unsigned long *)&gdt->address, 0);
 	if (status != EFI_SUCCESS) {
-		efi_printk("Failed to alloc mem for gdt\n");
+		efi_printk(sys_table, "Failed to alloc mem for gdt\n");
 		goto fail;
 	}
 
@@ -752,7 +762,7 @@ struct boot_params *efi_main(void *handle, efi_system_table_t *_table,
 				EFI_LOADER_DATA, sizeof(*idt),
 				(void **)&idt);
 	if (status != EFI_SUCCESS) {
-		efi_printk("Failed to alloc mem for idt structure\n");
+		efi_printk(sys_table, "Failed to alloc mem for idt structure\n");
 		goto fail;
 	}
 
diff --git a/drivers/firmware/efi/efi-stub-helper.c b/drivers/firmware/efi/efi-stub-helper.c
index 47891bd..3dd9b76 100644
--- a/drivers/firmware/efi/efi-stub-helper.c
+++ b/drivers/firmware/efi/efi-stub-helper.c
@@ -11,6 +11,8 @@
  */
 
 
+#define EFI_READ_CHUNK_SIZE	(1024 * 1024)
+
 struct initrd {
 	efi_file_handle_t *handle;
 	u64 size;
@@ -19,15 +21,16 @@ struct initrd {
 
 
 
-static void efi_char16_printk(efi_char16_t *str)
+static void efi_char16_printk(efi_system_table_t *sys_table_arg,
+			      efi_char16_t *str)
 {
 	struct efi_simple_text_output_protocol *out;
 
-	out = (struct efi_simple_text_output_protocol *)sys_table->con_out;
+	out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
 	efi_call_phys2(out->output_string, out, str);
 }
 
-static void efi_printk(char *str)
+static void efi_printk(efi_system_table_t *sys_table_arg, char *str)
 {
 	char *s8;
 
@@ -37,16 +40,19 @@ static void efi_printk(char *str)
 		ch[0] = *s8;
 		if (*s8 == '\n') {
 			efi_char16_t nl[2] = { '\r', 0 };
-			efi_char16_printk(nl);
+			efi_char16_printk(sys_table_arg, nl);
 		}
 
-		efi_char16_printk(ch);
+		efi_char16_printk(sys_table_arg, ch);
 	}
 }
 
 
-static efi_status_t __get_map(efi_memory_desc_t **map, unsigned long *map_size,
-			      unsigned long *desc_size)
+static efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
+				       efi_memory_desc_t **map,
+				       unsigned long *map_size,
+				       unsigned long *desc_size,
+				       unsigned long *key_ptr)
 {
 	efi_memory_desc_t *m = NULL;
 	efi_status_t status;
@@ -60,20 +66,22 @@ again:
 	 * allocation which may be in a new descriptor region.
 	 */
 	*map_size += sizeof(*m);
-	status = efi_call_phys3(sys_table->boottime->allocate_pool,
+	status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
 				EFI_LOADER_DATA, *map_size, (void **)&m);
 	if (status != EFI_SUCCESS)
 		goto fail;
 
-	status = efi_call_phys5(sys_table->boottime->get_memory_map, map_size,
-				m, &key, desc_size, &desc_version);
+	status = efi_call_phys5(sys_table_arg->boottime->get_memory_map,
+				map_size, m, &key, desc_size, &desc_version);
 	if (status == EFI_BUFFER_TOO_SMALL) {
-		efi_call_phys1(sys_table->boottime->free_pool, m);
+		efi_call_phys1(sys_table_arg->boottime->free_pool, m);
 		goto again;
 	}
 
 	if (status != EFI_SUCCESS)
-		efi_call_phys1(sys_table->boottime->free_pool, m);
+		efi_call_phys1(sys_table_arg->boottime->free_pool, m);
+	if (key_ptr && status == EFI_SUCCESS)
+		*key_ptr = key;
 
 fail:
 	*map = m;
@@ -83,8 +91,9 @@ fail:
 /*
  * Allocate at the highest possible address that is not above 'max'.
  */
-static efi_status_t high_alloc(unsigned long size, unsigned long align,
-			      unsigned long *addr, unsigned long max)
+static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
+			       unsigned long size, unsigned long align,
+			       unsigned long *addr, unsigned long max)
 {
 	unsigned long map_size, desc_size;
 	efi_memory_desc_t *map;
@@ -93,10 +102,18 @@ static efi_status_t high_alloc(unsigned long size, unsigned long align,
 	u64 max_addr = 0;
 	int i;
 
-	status = __get_map(&map, &map_size, &desc_size);
+	status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
+				    NULL);
 	if (status != EFI_SUCCESS)
 		goto fail;
 
+	/* Enforce minimum alignment that EFI requires when requesting
+	 * a specific address.  We are doing page-based allocations,
+	 * so we must be aligned to a page.
+	 */
+	if (align < EFI_PAGE_SIZE)
+		align = EFI_PAGE_SIZE;
+
 	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
 again:
 	for (i = 0; i < map_size / desc_size; i++) {
@@ -139,7 +156,7 @@ again:
 	if (!max_addr)
 		status = EFI_NOT_FOUND;
 	else {
-		status = efi_call_phys4(sys_table->boottime->allocate_pages,
+		status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
 					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
 					nr_pages, &max_addr);
 		if (status != EFI_SUCCESS) {
@@ -151,18 +168,18 @@ again:
 		*addr = max_addr;
 	}
 
-free_pool:
-	efi_call_phys1(sys_table->boottime->free_pool, map);
+	efi_call_phys1(sys_table_arg->boottime->free_pool, map);
 
 fail:
 	return status;
 }
 
 /*
- * Allocate at the lowest possible address.
+ * Allocate at the lowest possible address, that is not below 'min'
  */
-static efi_status_t low_alloc(unsigned long size, unsigned long align,
-			      unsigned long *addr)
+static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
+			      unsigned long size, unsigned long align,
+			      unsigned long *addr, unsigned long min)
 {
 	unsigned long map_size, desc_size;
 	efi_memory_desc_t *map;
@@ -170,16 +187,23 @@ static efi_status_t low_alloc(unsigned long size, unsigned long align,
 	unsigned long nr_pages;
 	int i;
 
-	status = __get_map(&map, &map_size, &desc_size);
+	status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
+				    NULL);
 	if (status != EFI_SUCCESS)
 		goto fail;
 
+	/* Enforce minimum alignment that EFI requires when requesting
+	 * a specific address.  We are doing page-based allocations,
+	 * so we must be aligned to a page.
+	 */
+	if (align < EFI_PAGE_SIZE)
+		align = EFI_PAGE_SIZE;
+
 	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
 	for (i = 0; i < map_size / desc_size; i++) {
 		efi_memory_desc_t *desc;
 		unsigned long m = (unsigned long)map;
 		u64 start, end;
-
 		desc = (efi_memory_desc_t *)(m + (i * desc_size));
 
 		if (desc->type != EFI_CONVENTIONAL_MEMORY)
@@ -199,11 +223,14 @@ static efi_status_t low_alloc(unsigned long size, unsigned long align,
 		if (start == 0x0)
 			start += 8;
 
+		if (start < min)
+			start = min;
+
 		start = round_up(start, align);
 		if ((start + size) > end)
 			continue;
 
-		status = efi_call_phys4(sys_table->boottime->allocate_pages,
+		status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
 					EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
 					nr_pages, &start);
 		if (status == EFI_SUCCESS) {
@@ -215,18 +242,21 @@ static efi_status_t low_alloc(unsigned long size, unsigned long align,
 	if (i == map_size / desc_size)
 		status = EFI_NOT_FOUND;
 
-free_pool:
-	efi_call_phys1(sys_table->boottime->free_pool, map);
+	efi_call_phys1(sys_table_arg->boottime->free_pool, map);
 fail:
 	return status;
 }
 
-static void low_free(unsigned long size, unsigned long addr)
+static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
+		     unsigned long addr)
 {
 	unsigned long nr_pages;
 
+	if (!size)
+		return;
+
 	nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
-	efi_call_phys2(sys_table->boottime->free_pages, addr, nr_pages);
+	efi_call_phys2(sys_table_arg->boottime->free_pages, addr, nr_pages);
 }
 
 
@@ -236,8 +266,11 @@ static void low_free(unsigned long size, unsigned long addr)
  * We only support loading an initrd from the same filesystem as the
  * kernel image.
  */
-static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
-				    struct setup_header *hdr)
+static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
+					 efi_loaded_image_t *image,
+					 char *cmd_line, char *option_string,
+					 u64 max_addr,
+					 u64 *load_addr, u64 *load_size)
 {
 	struct initrd *initrds;
 	unsigned long initrd_addr;
@@ -253,19 +286,25 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 	initrd_addr = 0;
 	initrd_total = 0;
 
-	str = (char *)(unsigned long)hdr->cmd_line_ptr;
+	str = cmd_line;
 
 	j = 0;			/* See close_handles */
 
+	if (!load_addr || !load_size)
+		return EFI_INVALID_PARAMETER;
+
+	*load_addr = 0;
+	*load_size = 0;
+
 	if (!str || !*str)
 		return EFI_SUCCESS;
 
 	for (nr_initrds = 0; *str; nr_initrds++) {
-		str = strstr(str, "initrd=");
+		str = strstr(str, option_string);
 		if (!str)
 			break;
 
-		str += 7;
+		str += strlen(option_string);
 
 		/* Skip any leading slashes */
 		while (*str == '/' || *str == '\\')
@@ -278,16 +317,16 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 	if (!nr_initrds)
 		return EFI_SUCCESS;
 
-	status = efi_call_phys3(sys_table->boottime->allocate_pool,
+	status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
 				EFI_LOADER_DATA,
 				nr_initrds * sizeof(*initrds),
-				&initrds);
+				(void **)&initrds);
 	if (status != EFI_SUCCESS) {
-		efi_printk("Failed to alloc mem for initrds\n");
+		efi_printk(sys_table_arg, "Failed to alloc mem for file load\n");
 		goto fail;
 	}
 
-	str = (char *)(unsigned long)hdr->cmd_line_ptr;
+	str = cmd_line;
 	for (i = 0; i < nr_initrds; i++) {
 		struct initrd *initrd;
 		efi_file_handle_t *h;
@@ -298,11 +337,11 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 		efi_char16_t *p;
 		u64 file_sz;
 
-		str = strstr(str, "initrd=");
+		str = strstr(str, option_string);
 		if (!str)
 			break;
 
-		str += 7;
+		str += strlen(option_string);
 
 		initrd = &initrds[i];
 		p = filename_16;
@@ -317,7 +356,7 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 
 			if (*str == '/') {
 				*p++ = '\\';
-				*str++;
+				str++;
 			} else {
 				*p++ = *str++;
 			}
@@ -329,18 +368,19 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 		if (!i) {
 			efi_boot_services_t *boottime;
 
-			boottime = sys_table->boottime;
+			boottime = sys_table_arg->boottime;
 
 			status = efi_call_phys3(boottime->handle_protocol,
-					image->device_handle, &fs_proto, &io);
+						image->device_handle, &fs_proto,
+						(void **)&io);
 			if (status != EFI_SUCCESS) {
-				efi_printk("Failed to handle fs_proto\n");
+				efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
 				goto free_initrds;
 			}
 
 			status = efi_call_phys2(io->open_volume, io, &fh);
 			if (status != EFI_SUCCESS) {
-				efi_printk("Failed to open volume\n");
+				efi_printk(sys_table_arg, "Failed to open volume\n");
 				goto free_initrds;
 			}
 		}
@@ -348,9 +388,9 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 		status = efi_call_phys5(fh->open, fh, &h, filename_16,
 					EFI_FILE_MODE_READ, (u64)0);
 		if (status != EFI_SUCCESS) {
-			efi_printk("Failed to open initrd file: ");
-			efi_char16_printk(filename_16);
-			efi_printk("\n");
+			efi_printk(sys_table_arg, "Failed to open file file: ");
+			efi_char16_printk(sys_table_arg, filename_16);
+			efi_printk(sys_table_arg, "\n");
 			goto close_handles;
 		}
 
@@ -360,30 +400,32 @@ static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
 		status = efi_call_phys4(h->get_info, h, &info_guid,
 					&info_sz, NULL);
 		if (status != EFI_BUFFER_TOO_SMALL) {
-			efi_printk("Failed to get initrd info size\n");
+			efi_printk(sys_table_arg, "Failed to get file info size\n");
 			goto close_handles;
 		}
 
 grow:
-		status = efi_call_phys3(sys_table->boottime->allocate_pool,
-					EFI_LOADER_DATA, info_sz, &info);
+		status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
+					EFI_LOADER_DATA, info_sz,
+					(void **)&info);
 		if (status != EFI_SUCCESS) {
-			efi_printk("Failed to alloc mem for initrd info\n");
+			efi_printk(sys_table_arg, "Failed to alloc mem for file info\n");
 			goto close_handles;
 		}
 
 		status = efi_call_phys4(h->get_info, h, &info_guid,
 					&info_sz, info);
 		if (status == EFI_BUFFER_TOO_SMALL) {
-			efi_call_phys1(sys_table->boottime->free_pool, info);
+			efi_call_phys1(sys_table_arg->boottime->free_pool,
+				       info);
 			goto grow;
 		}
 
 		file_sz = info->file_size;
-		efi_call_phys1(sys_table->boottime->free_pool, info);
+		efi_call_phys1(sys_table_arg->boottime->free_pool, info);
 
 		if (status != EFI_SUCCESS) {
-			efi_printk("Failed to get initrd info\n");
+			efi_printk(sys_table_arg, "Failed to get file info\n");
 			goto close_handles;
 		}
 
@@ -399,16 +441,16 @@ grow:
 		 * addresses in memory, so allocate enough memory for
 		 * all the initrd's.
 		 */
-		status = high_alloc(initrd_total, 0x1000,
-				   &initrd_addr, hdr->initrd_addr_max);
+		status = efi_high_alloc(sys_table_arg, initrd_total, 0x1000,
+				    &initrd_addr, max_addr);
 		if (status != EFI_SUCCESS) {
-			efi_printk("Failed to alloc highmem for initrds\n");
+			efi_printk(sys_table_arg, "Failed to alloc highmem for initrds\n");
 			goto close_handles;
 		}
 
 		/* We've run out of free low memory. */
-		if (initrd_addr > hdr->initrd_addr_max) {
-			efi_printk("We've run out of free low memory\n");
+		if (initrd_addr > max_addr) {
+			efi_printk(sys_table_arg, "We've run out of free low memory\n");
 			status = EFI_INVALID_PARAMETER;
 			goto free_initrd_total;
 		}
@@ -419,16 +461,17 @@ grow:
 
 			size = initrds[j].size;
 			while (size) {
-				u64 chunksize;
+				unsigned long chunksize;
 				if (size > EFI_READ_CHUNK_SIZE)
 					chunksize = EFI_READ_CHUNK_SIZE;
 				else
 					chunksize = size;
 				status = efi_call_phys3(fh->read,
 							initrds[j].handle,
-							&chunksize, addr);
+							&chunksize,
+							(void *)addr);
 				if (status != EFI_SUCCESS) {
-					efi_printk("Failed to read initrd\n");
+					efi_printk(sys_table_arg, "Failed to read file\n");
 					goto free_initrd_total;
 				}
 				addr += chunksize;
@@ -440,24 +483,24 @@ grow:
 
 	}
 
-	efi_call_phys1(sys_table->boottime->free_pool, initrds);
+	efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
 
-	hdr->ramdisk_image = initrd_addr;
-	hdr->ramdisk_size = initrd_total;
+	*load_addr = initrd_addr;
+	*load_size = initrd_total;
 
 	return status;
 
 free_initrd_total:
-	low_free(initrd_total, initrd_addr);
+	efi_free(sys_table_arg, initrd_total, initrd_addr);
 
 close_handles:
 	for (k = j; k < i; k++)
 		efi_call_phys1(fh->close, initrds[k].handle);
 free_initrds:
-	efi_call_phys1(sys_table->boottime->free_pool, initrds);
+	efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
 fail:
-	hdr->ramdisk_image = 0;
-	hdr->ramdisk_size = 0;
+	*load_addr = 0;
+	*load_size = 0;
 
 	return status;
 }
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH 4/7] Add proper definitions for some EFI function pointers.
  2013-08-02 21:29 ` Roy Franz
@ 2013-08-02 21:29   ` Roy Franz
  -1 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-02 21:29 UTC (permalink / raw)
  To: linux-kernel, linux-efi, linux-arm-kernel, matt.fleming, linux
  Cc: leif.lindholm, Roy Franz

The x86/AMD64 EFI stubs must us a call wrapper to convert between
the Linux and EFI ABIs, so void pointers are sufficient.  For ARM,
the ABIs are compatible, so we can directly invoke the function
pointers.  The functions that are used by the ARM stub are updated
to match the EFI definitions.

Signed-off-by: Roy Franz <roy.franz@linaro.org>
---
 include/linux/efi.h |   61 +++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 44 insertions(+), 17 deletions(-)

diff --git a/include/linux/efi.h b/include/linux/efi.h
index 51f5641..779ec8c 100644
--- a/include/linux/efi.h
+++ b/include/linux/efi.h
@@ -39,6 +39,8 @@
 typedef unsigned long efi_status_t;
 typedef u8 efi_bool_t;
 typedef u16 efi_char16_t;		/* UNICODE character */
+typedef u64 efi_physical_addr_t;
+typedef void *efi_handle_t;
 
 
 typedef struct {
@@ -96,6 +98,26 @@ typedef	struct {
 #define EFI_MEMORY_DESCRIPTOR_VERSION	1
 
 #define EFI_PAGE_SHIFT		12
+#define EFI_PAGE_SIZE		(1UL << EFI_PAGE_SHIFT)
+
+typedef enum {
+	EfiReservedMemoryType,
+	EfiLoaderCode,
+	EfiLoaderData,
+	EfiBootServicesCode,
+	EfiBootServicesData,
+	EfiRuntimeServicesCode,
+	EfiRuntimeServicesData,
+	EfiConventionalMemory,
+	EfiUnusableMemory,
+	EfiACPIReclaimMemory,
+	EfiACPIMemoryNVS,
+	EfiMemoryMappedIO,
+	EfiMemoryMappedIOPortSpace,
+	EfiPalCode,
+	EfiMaxMemoryType
+} EFI_MEMORY_TYPE;
+
 
 typedef struct {
 	u32 type;
@@ -157,11 +179,12 @@ typedef struct {
 	efi_table_hdr_t hdr;
 	void *raise_tpl;
 	void *restore_tpl;
-	void *allocate_pages;
-	void *free_pages;
-	void *get_memory_map;
-	void *allocate_pool;
-	void *free_pool;
+	int (*allocate_pages)(int, int, unsigned long, efi_physical_addr_t *);
+	int (*free_pages)(efi_physical_addr_t, unsigned long);
+	int (*get_memory_map)(unsigned long *, void *, unsigned long *,
+			      unsigned long *, u32 *);
+	int (*allocate_pool)(int, unsigned long, void **);
+	int (*free_pool)(void *);
 	void *create_event;
 	void *set_timer;
 	void *wait_for_event;
@@ -171,7 +194,7 @@ typedef struct {
 	void *install_protocol_interface;
 	void *reinstall_protocol_interface;
 	void *uninstall_protocol_interface;
-	void *handle_protocol;
+	int (*handle_protocol)(efi_handle_t, efi_guid_t *, void **);
 	void *__reserved;
 	void *register_protocol_notify;
 	void *locate_handle;
@@ -181,7 +204,7 @@ typedef struct {
 	void *start_image;
 	void *exit;
 	void *unload_image;
-	void *exit_boot_services;
+	int (*exit_boot_services)(efi_handle_t, unsigned long);
 	void *get_next_monotonic_count;
 	void *stall;
 	void *set_watchdog_timer;
@@ -488,10 +511,6 @@ typedef struct {
 	unsigned long unload;
 } efi_loaded_image_t;
 
-typedef struct {
-	u64 revision;
-	void *open_volume;
-} efi_file_io_interface_t;
 
 typedef struct {
 	u64 size;
@@ -504,20 +523,28 @@ typedef struct {
 	efi_char16_t filename[1];
 } efi_file_info_t;
 
-typedef struct {
+typedef struct _efi_file_handle {
 	u64 revision;
-	void *open;
-	void *close;
+	int (*open)(struct _efi_file_handle *, struct _efi_file_handle **,
+		    efi_char16_t *, u64, u64);
+	int (*close)(struct _efi_file_handle *);
 	void *delete;
-	void *read;
+	int (*read)(struct _efi_file_handle *, unsigned long *, void *);
 	void *write;
 	void *get_position;
 	void *set_position;
-	void *get_info;
+	int (*get_info)(struct _efi_file_handle *, efi_guid_t *,
+			unsigned long *, void *);
 	void *set_info;
 	void *flush;
 } efi_file_handle_t;
 
+typedef struct _efi_file_io_interface {
+	u64 revision;
+	int (*open_volume)(struct _efi_file_io_interface *,
+			   efi_file_handle_t **);
+} efi_file_io_interface_t;
+
 #define EFI_FILE_MODE_READ	0x0000000000000001
 #define EFI_FILE_MODE_WRITE	0x0000000000000002
 #define EFI_FILE_MODE_CREATE	0x8000000000000000
@@ -787,7 +814,7 @@ struct efivar_entry {
 
 struct efi_simple_text_output_protocol {
 	void *reset;
-	void *output_string;
+	int (*output_string)(void *, void *);
 	void *test_string;
 };
 
-- 
1.7.10.4


^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH 4/7] Add proper definitions for some EFI function pointers.
@ 2013-08-02 21:29   ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-02 21:29 UTC (permalink / raw)
  To: linux-arm-kernel

The x86/AMD64 EFI stubs must us a call wrapper to convert between
the Linux and EFI ABIs, so void pointers are sufficient.  For ARM,
the ABIs are compatible, so we can directly invoke the function
pointers.  The functions that are used by the ARM stub are updated
to match the EFI definitions.

Signed-off-by: Roy Franz <roy.franz@linaro.org>
---
 include/linux/efi.h |   61 +++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 44 insertions(+), 17 deletions(-)

diff --git a/include/linux/efi.h b/include/linux/efi.h
index 51f5641..779ec8c 100644
--- a/include/linux/efi.h
+++ b/include/linux/efi.h
@@ -39,6 +39,8 @@
 typedef unsigned long efi_status_t;
 typedef u8 efi_bool_t;
 typedef u16 efi_char16_t;		/* UNICODE character */
+typedef u64 efi_physical_addr_t;
+typedef void *efi_handle_t;
 
 
 typedef struct {
@@ -96,6 +98,26 @@ typedef	struct {
 #define EFI_MEMORY_DESCRIPTOR_VERSION	1
 
 #define EFI_PAGE_SHIFT		12
+#define EFI_PAGE_SIZE		(1UL << EFI_PAGE_SHIFT)
+
+typedef enum {
+	EfiReservedMemoryType,
+	EfiLoaderCode,
+	EfiLoaderData,
+	EfiBootServicesCode,
+	EfiBootServicesData,
+	EfiRuntimeServicesCode,
+	EfiRuntimeServicesData,
+	EfiConventionalMemory,
+	EfiUnusableMemory,
+	EfiACPIReclaimMemory,
+	EfiACPIMemoryNVS,
+	EfiMemoryMappedIO,
+	EfiMemoryMappedIOPortSpace,
+	EfiPalCode,
+	EfiMaxMemoryType
+} EFI_MEMORY_TYPE;
+
 
 typedef struct {
 	u32 type;
@@ -157,11 +179,12 @@ typedef struct {
 	efi_table_hdr_t hdr;
 	void *raise_tpl;
 	void *restore_tpl;
-	void *allocate_pages;
-	void *free_pages;
-	void *get_memory_map;
-	void *allocate_pool;
-	void *free_pool;
+	int (*allocate_pages)(int, int, unsigned long, efi_physical_addr_t *);
+	int (*free_pages)(efi_physical_addr_t, unsigned long);
+	int (*get_memory_map)(unsigned long *, void *, unsigned long *,
+			      unsigned long *, u32 *);
+	int (*allocate_pool)(int, unsigned long, void **);
+	int (*free_pool)(void *);
 	void *create_event;
 	void *set_timer;
 	void *wait_for_event;
@@ -171,7 +194,7 @@ typedef struct {
 	void *install_protocol_interface;
 	void *reinstall_protocol_interface;
 	void *uninstall_protocol_interface;
-	void *handle_protocol;
+	int (*handle_protocol)(efi_handle_t, efi_guid_t *, void **);
 	void *__reserved;
 	void *register_protocol_notify;
 	void *locate_handle;
@@ -181,7 +204,7 @@ typedef struct {
 	void *start_image;
 	void *exit;
 	void *unload_image;
-	void *exit_boot_services;
+	int (*exit_boot_services)(efi_handle_t, unsigned long);
 	void *get_next_monotonic_count;
 	void *stall;
 	void *set_watchdog_timer;
@@ -488,10 +511,6 @@ typedef struct {
 	unsigned long unload;
 } efi_loaded_image_t;
 
-typedef struct {
-	u64 revision;
-	void *open_volume;
-} efi_file_io_interface_t;
 
 typedef struct {
 	u64 size;
@@ -504,20 +523,28 @@ typedef struct {
 	efi_char16_t filename[1];
 } efi_file_info_t;
 
-typedef struct {
+typedef struct _efi_file_handle {
 	u64 revision;
-	void *open;
-	void *close;
+	int (*open)(struct _efi_file_handle *, struct _efi_file_handle **,
+		    efi_char16_t *, u64, u64);
+	int (*close)(struct _efi_file_handle *);
 	void *delete;
-	void *read;
+	int (*read)(struct _efi_file_handle *, unsigned long *, void *);
 	void *write;
 	void *get_position;
 	void *set_position;
-	void *get_info;
+	int (*get_info)(struct _efi_file_handle *, efi_guid_t *,
+			unsigned long *, void *);
 	void *set_info;
 	void *flush;
 } efi_file_handle_t;
 
+typedef struct _efi_file_io_interface {
+	u64 revision;
+	int (*open_volume)(struct _efi_file_io_interface *,
+			   efi_file_handle_t **);
+} efi_file_io_interface_t;
+
 #define EFI_FILE_MODE_READ	0x0000000000000001
 #define EFI_FILE_MODE_WRITE	0x0000000000000002
 #define EFI_FILE_MODE_CREATE	0x8000000000000000
@@ -787,7 +814,7 @@ struct efivar_entry {
 
 struct efi_simple_text_output_protocol {
 	void *reset;
-	void *output_string;
+	int (*output_string)(void *, void *);
 	void *test_string;
 };
 
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH 5/7] Add strstr to compressed string.c for ARM.
  2013-08-02 21:29 ` Roy Franz
@ 2013-08-02 21:29   ` Roy Franz
  -1 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-02 21:29 UTC (permalink / raw)
  To: linux-kernel, linux-efi, linux-arm-kernel, matt.fleming, linux
  Cc: leif.lindholm, Roy Franz

The shared efi-stub-helper.c functions require a strstr
implementation.
Implementation copied from arch/x86/boot/string.c

Signed-off-by: Roy Franz <roy.franz@linaro.org>
---
 arch/arm/boot/compressed/string.c |   21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/arch/arm/boot/compressed/string.c b/arch/arm/boot/compressed/string.c
index 36e53ef..5397792 100644
--- a/arch/arm/boot/compressed/string.c
+++ b/arch/arm/boot/compressed/string.c
@@ -111,6 +111,27 @@ char *strchr(const char *s, int c)
 	return (char *)s;
 }
 
+/**
+ * strstr - Find the first substring in a %NUL terminated string
+ * @s1: The string to be searched
+ * @s2: The string to search for
+ */
+char *strstr(const char *s1, const char *s2)
+{
+	size_t l1, l2;
+
+	l2 = strlen(s2);
+	if (!l2)
+		return (char *)s1;
+	l1 = strlen(s1);
+	while (l1 >= l2) {
+		l1--;
+		if (!memcmp(s1, s2, l2))
+			return (char *)s1;
+		s1++;
+	}
+	return NULL;
+}
 #undef memset
 
 void *memset(void *s, int c, size_t count)
-- 
1.7.10.4


^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH 5/7] Add strstr to compressed string.c for ARM.
@ 2013-08-02 21:29   ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-02 21:29 UTC (permalink / raw)
  To: linux-arm-kernel

The shared efi-stub-helper.c functions require a strstr
implementation.
Implementation copied from arch/x86/boot/string.c

Signed-off-by: Roy Franz <roy.franz@linaro.org>
---
 arch/arm/boot/compressed/string.c |   21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/arch/arm/boot/compressed/string.c b/arch/arm/boot/compressed/string.c
index 36e53ef..5397792 100644
--- a/arch/arm/boot/compressed/string.c
+++ b/arch/arm/boot/compressed/string.c
@@ -111,6 +111,27 @@ char *strchr(const char *s, int c)
 	return (char *)s;
 }
 
+/**
+ * strstr - Find the first substring in a %NUL terminated string
+ * @s1: The string to be searched
+ * @s2: The string to search for
+ */
+char *strstr(const char *s1, const char *s2)
+{
+	size_t l1, l2;
+
+	l2 = strlen(s2);
+	if (!l2)
+		return (char *)s1;
+	l1 = strlen(s1);
+	while (l1 >= l2) {
+		l1--;
+		if (!memcmp(s1, s2, l2))
+			return (char *)s1;
+		s1++;
+	}
+	return NULL;
+}
 #undef memset
 
 void *memset(void *s, int c, size_t count)
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH 6/7] Add EFI stub for ARM
@ 2013-08-02 21:29   ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-02 21:29 UTC (permalink / raw)
  To: linux-kernel, linux-efi, linux-arm-kernel, matt.fleming, linux
  Cc: leif.lindholm, Roy Franz

This patch adds EFI stub support for the ARM Linux kernel.  The EFI stub
operations similarly to the x86 stub: it is a shim between the EFI firmware
and the normal zImage entry point, and sets up the environment that the
zImage is expecting.  This includes loading the initrd (optionaly) and
device tree from the system partition based on the kernel command line.
The stub updates the device tree as necessary, including adding reserved
memory regions and adding entries for EFI runtime services. The PE/COFF
"MZ" header at offset 0 results in the first instruction being an add
that corrupts r5, which is not used by the zImage interface.

Signed-off-by: Roy Franz <roy.franz@linaro.org>
---
 arch/arm/boot/compressed/Makefile     |   18 +-
 arch/arm/boot/compressed/efi-header.S |  114 ++++++++
 arch/arm/boot/compressed/efi-stub.c   |  514 +++++++++++++++++++++++++++++++++
 arch/arm/boot/compressed/head.S       |   90 +++++-
 4 files changed, 732 insertions(+), 4 deletions(-)
 create mode 100644 arch/arm/boot/compressed/efi-header.S
 create mode 100644 arch/arm/boot/compressed/efi-stub.c

diff --git a/arch/arm/boot/compressed/Makefile b/arch/arm/boot/compressed/Makefile
index 7ac1610..c62826a 100644
--- a/arch/arm/boot/compressed/Makefile
+++ b/arch/arm/boot/compressed/Makefile
@@ -106,8 +106,22 @@ $(addprefix $(obj)/,$(libfdt) $(libfdt_hdrs)): $(obj)/%: $(srctree)/scripts/dtc/
 $(addprefix $(obj)/,$(libfdt_objs) atags_to_fdt.o): \
 	$(addprefix $(obj)/,$(libfdt_hdrs))
 
+$(addprefix $(obj)/,$(libfdt_objs) efi-stub.o): \
+	$(addprefix $(obj)/,$(libfdt_hdrs))
+
 ifeq ($(CONFIG_ARM_ATAG_DTB_COMPAT),y)
-OBJS	+= $(libfdt_objs) atags_to_fdt.o
+OBJS	+= atags_to_fdt.o
+USE_LIBFDT = y
+endif
+
+ifeq ($(CONFIG_EFI_STUB),y)
+CFLAGS_efi-stub.o += -DTEXT_OFFSET=$(TEXT_OFFSET)
+OBJS	+= efi-stub.o
+USE_LIBFDT = y
+endif
+
+ifeq ($(USE_LIBFDT),y)
+OBJS	+= $(libfdt_objs)
 endif
 
 targets       := vmlinux vmlinux.lds \
@@ -125,7 +139,7 @@ ORIG_CFLAGS := $(KBUILD_CFLAGS)
 KBUILD_CFLAGS = $(subst -pg, , $(ORIG_CFLAGS))
 endif
 
-ccflags-y := -fpic -mno-single-pic-base -fno-builtin -I$(obj)
+ccflags-y := -fpic -mno-single-pic-base -fno-builtin -I$(obj) -fno-stack-protector
 asflags-y := -DZIMAGE
 
 # Supply kernel BSS size to the decompressor via a linker symbol.
diff --git a/arch/arm/boot/compressed/efi-header.S b/arch/arm/boot/compressed/efi-header.S
new file mode 100644
index 0000000..6ff32cc
--- /dev/null
+++ b/arch/arm/boot/compressed/efi-header.S
@@ -0,0 +1,114 @@
+@ Copyright (C) 2013 Linaro Ltd;  <roy.franz@linaro.org>
+@
+@ This file contains the PE/COFF header that is part of the
+@ EFI stub.
+@
+
+	.org	0x3c
+	@
+	@ The PE header can be anywhere in the file, but for
+	@ simplicity we keep it together with the MSDOS header
+	@ The offset to the PE/COFF header needs to be at offset
+	@ 0x3C in the MSDOS header.
+	@ The only 2 fields of the MSDOS header that are used are this
+	@ PE/COFF offset, and the "MZ" bytes at offset 0x0.
+	@
+	.long	pe_header			@ Offset to the PE header.
+
+      .align 3
+pe_header:
+
+
+pe_header:
+	.ascii	"PE"
+	.short 	0
+
+coff_header:
+	.short	0x01c2				@ ARM or Thumb
+	.short	2				@ nr_sections
+	.long	0 				@ TimeDateStamp
+	.long	0				@ PointerToSymbolTable
+	.long	1				@ NumberOfSymbols
+	.short	section_table - optional_header	@ SizeOfOptionalHeader
+	.short	0x306				@ Characteristics.
+						@ IMAGE_FILE_32BIT_MACHINE |
+						@ IMAGE_FILE_DEBUG_STRIPPED |
+						@ IMAGE_FILE_EXECUTABLE_IMAGE |
+						@ IMAGE_FILE_LINE_NUMS_STRIPPED
+
+optional_header:
+	.short	0x10b				@ PE32 format
+	.byte	0x02				@ MajorLinkerVersion
+	.byte	0x14				@ MinorLinkerVersion
+
+	.long	0				@ SizeOfCode
+
+	.long	0				@ SizeOfInitializedData
+	.long	0				@ SizeOfUninitializedData
+
+	.long	efi_stub_entry			@ AddressOfEntryPoint
+	.long	efi_stub_entry			@ BaseOfCode
+	.long	0				@ data
+
+extra_header_fields:
+	.long	0				@ ImageBase
+	.long	0x20				@ SectionAlignment
+	.long	0x20				@ FileAlignment
+	.short	0				@ MajorOperatingSystemVersion
+	.short	0				@ MinorOperatingSystemVersion
+	.short	0				@ MajorImageVersion
+	.short	0				@ MinorImageVersion
+	.short	0				@ MajorSubsystemVersion
+	.short	0				@ MinorSubsystemVersion
+	.long	0				@ Win32VersionValue
+
+	.long	_edata				@ SizeOfImage
+
+	@ Everything before the entry point is considered part of the header
+	.long	efi_stub_entry			@ SizeOfHeaders
+	.long	0				@ CheckSum
+	.short	0xa				@ Subsystem (EFI application)
+	.short	0				@ DllCharacteristics
+	.long	0				@ SizeOfStackReserve
+	.long	0				@ SizeOfStackCommit
+	.long	0				@ SizeOfHeapReserve
+	.long	0				@ SizeOfHeapCommit
+	.long	0				@ LoaderFlags
+	.long	0x0				@ NumberOfRvaAndSizes
+
+	# Section table
+section_table:
+
+	#
+	# The EFI application loader requires a relocation section
+	# because EFI applications must be relocatable.  This is a
+	# dummy section as far as we are concerned.
+	#
+	.ascii	".reloc"
+	.byte	0
+	.byte	0			@ end of 0 padding of section name
+	.long	0
+	.long	0
+	.long	0			@ SizeOfRawData
+	.long	0			@ PointerToRawData
+	.long	0			@ PointerToRelocations
+	.long	0			@ PointerToLineNumbers
+	.short	0			@ NumberOfRelocations
+	.short	0			@ NumberOfLineNumbers
+	.long	0x42100040		@ Characteristics (section flags)
+
+
+	.ascii	".text"
+	.byte	0
+	.byte	0
+	.byte	0        		@ end of 0 padding of section name
+	.long	_edata - efi_stub_entry		@ VirtualSize
+	.long	efi_stub_entry			@ VirtualAddress
+	.long	_edata - efi_stub_entry		@ SizeOfRawData
+	.long	efi_stub_entry			@ PointerToRawData
+
+	.long	0		@ PointerToRelocations (0 for executables)
+	.long	0		@ PointerToLineNumbers (0 for executables)
+	.short	0		@ NumberOfRelocations  (0 for executables)
+	.short	0		@ NumberOfLineNumbers  (0 for executables)
+	.long	0xe0500020	@ Characteristics (section flags)
diff --git a/arch/arm/boot/compressed/efi-stub.c b/arch/arm/boot/compressed/efi-stub.c
new file mode 100644
index 0000000..41fa1e2
--- /dev/null
+++ b/arch/arm/boot/compressed/efi-stub.c
@@ -0,0 +1,514 @@
+/*
+ * linux/arch/arm/boot/compressed/efi-stub.c
+ *
+ * Copyright (C) 2013 Linaro Ltd;  <roy.franz@linaro.org>
+ *
+ * This file implements the EFI boot stub for the ARM kernel
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+#include <linux/efi.h>
+#include <libfdt.h>
+
+
+/* Error code returned to ASM code instead of valid FDT address. */
+#define EFI_STUB_ERROR		(~0)
+
+/* EFI function call wrappers.  These are not required for
+ * ARM, but wrappers are required for X86 to convert between
+ * ABIs.  These wrappers are provided to allow code sharing
+ * between X86 and ARM.  Since these wrappers directly invoke the
+ * EFI function pointer, the function pointer type must be properly
+ * defined, which is not the case for X86  One advantage of this is
+ * it allows for type checking of arguments, which is not
+ * possible with the X86 wrappers.
+ */
+#define efi_call_phys0(f)			f()
+#define efi_call_phys1(f, a1)			f(a1)
+#define efi_call_phys2(f, a1, a2)		f(a1, a2)
+#define efi_call_phys3(f, a1, a2, a3)		f(a1, a2, a3)
+#define efi_call_phys4(f, a1, a2, a3, a4)	f(a1, a2, a3, a4)
+#define efi_call_phys5(f, a1, a2, a3, a4, a5)	f(a1, a2, a3, a4, a5)
+
+/* The maximum uncompressed kernel size is 32 MBytes, so we will reserve
+ * that for the decompressed kernel.  We have no easy way to tell what
+ * the actuall size of code + data the uncompressed kernel will use.
+ */
+#define MAX_UNCOMP_KERNEL_SIZE	0x02000000
+
+/* The kernel zImage should be located between 32 Mbytes
+ * and 128 MBytes from the base of DRAM.  The min
+ * address leaves space for a maximal size uncompressed image,
+ * and the max address is due to how the zImage decompressor
+ * picks a destination address.
+ */
+#define MAX_ZIMAGE_OFFSET	0x08000000
+#define MIN_ZIMAGE_OFFSET	MAX_UNCOMP_KERNEL_SIZE
+
+#define MAX_CMDLINE_LEN		500
+
+struct fdt_region {
+	u64 base;
+	u64 size;
+};
+
+/*
+ * Additional size that could be used for FDT entries added by
+ * the UEFI OS Loader Estimation based on:
+ * EDID (300bytes) + bootargs  + initrd region (20bytes)
+ * + system memory region (20bytes) + mp_core entries (200
+ * bytes)
+ */
+#define FDT_ADDITIONAL_ENTRIES_SIZE     (0x300 + MAX_CMDLINE_LEN)
+
+/* Include shared EFI stub code */
+#include "../../../../drivers/firmware/efi/efi-stub-helper.c"
+
+
+static int is_linux_reserved_region(int memory_type)
+{
+	switch (memory_type) {
+	case EfiRuntimeServicesCode:
+	case EfiRuntimeServicesData:
+	case EfiUnusableMemory:
+	case EfiACPIReclaimMemory:
+	case EfiACPIMemoryNVS:
+		return 1;
+	default:
+		return 0;
+	}
+}
+
+
+static int relocate_kernel(efi_system_table_t *sys_table,
+			   unsigned long *load_addr, unsigned long *load_size,
+			   unsigned long min_addr, unsigned long max_addr)
+{
+	/* Get current address of kernel. */
+	unsigned long cur_zimage_addr = *load_addr;
+	unsigned long zimage_size = *load_size;
+	unsigned long new_addr = 0;
+	unsigned long nr_pages;
+
+	efi_status_t status;
+
+	if (!load_addr || !load_size)
+		return EFI_INVALID_PARAMETER;
+
+	*load_size = 0;
+	if (cur_zimage_addr > min_addr
+	    && (cur_zimage_addr + zimage_size) < max_addr) {
+		/* We don't need to do anything, as kernel at an acceptable
+		 * address already.
+		 */
+		return EFI_SUCCESS;
+	}
+	/*
+	 * The EFI firmware loader could have placed the kernel image
+	 * anywhere in memory, but the kernel has restrictions on the
+	 * min and max physical address it can run at.
+	 */
+	nr_pages = round_up(zimage_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
+
+	status = efi_low_alloc(sys_table, zimage_size, 0,
+			   &new_addr, min_addr);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "Failed to alloc memory for kernel.\n");
+		return status;
+	}
+
+	if (new_addr > (max_addr - zimage_size)) {
+		efi_free(sys_table, zimage_size, new_addr);
+		efi_printk(sys_table, "Failed to alloc usable memory for kernel.\n");
+		return EFI_INVALID_PARAMETER;
+	}
+
+	/* We know source/dest won't overlap since both memory ranges
+	 * have been allocated by UEFI, so we can safely use memcpy.
+	 */
+	memcpy((void *)new_addr, (void *)(unsigned long)cur_zimage_addr,
+	       zimage_size);
+
+	/* Return the load address and size */
+	*load_addr = new_addr;
+	*load_size = zimage_size;
+
+
+	return status;
+}
+
+
+/* Convert the unicode UEFI command line to ASCII to pass to kernel.
+ * Size of memory allocated return in *cmd_line_len.
+ * Returns NULL on error.
+ */
+static char *convert_cmdline_to_ascii(efi_system_table_t *sys_table,
+				      efi_loaded_image_t *image,
+				      unsigned long *cmd_line_len,
+				      u32 max_addr)
+{
+	u16 *s2;
+	u8 *s1 = NULL;
+	unsigned long cmdline_addr = 0;
+	int load_options_size = image->load_options_size / 2; /* ASCII */
+	void *options = (u16 *)image->load_options;
+	int options_size = 0;
+	int status;
+	int i;
+	u16 zero = 0;
+
+	if (options) {
+		s2 = options;
+		while (*s2 && *s2 != '\n' && options_size < load_options_size) {
+			s2++;
+			options_size++;
+		}
+	}
+
+	if (options_size == 0) {
+		/* No command line options, so return empty string*/
+		options_size = 1;
+		options = &zero;
+	}
+
+	if (options_size > MAX_CMDLINE_LEN)
+		options_size = MAX_CMDLINE_LEN;
+
+	options_size++;  /* NUL termination */
+
+	status = efi_high_alloc(sys_table, options_size, 0,
+			    &cmdline_addr, max_addr);
+	if (status != EFI_SUCCESS)
+		return NULL;
+
+	s1 = (u8 *)(unsigned long)cmdline_addr;
+	s2 = (u16 *)options;
+
+	for (i = 0; i < options_size - 1; i++)
+		*s1++ = *s2++;
+
+	*s1 = '\0';
+
+	*cmd_line_len = options_size;
+	return (char *)(unsigned long)cmdline_addr;
+}
+
+static u32 update_fdt_and_exit_boot(efi_system_table_t *sys_table,
+				    void *handle, unsigned long dram_base,
+				    void *orig_fdt, u64 *orig_fdt_size,
+				    char *cmdline_ptr,
+				    unsigned long *cmdline_size,
+				    u64 initrd_addr, u64 initrd_size)
+{
+	unsigned long new_fdt_size;
+	unsigned long new_fdt_addr;
+	void *fdt;
+	int node;
+	int status;
+	int i;
+	unsigned long map_size, desc_size;
+	unsigned long mmap_key;
+	efi_memory_desc_t *memory_map;
+	unsigned long fdt_val;
+
+	new_fdt_size = *orig_fdt_size + FDT_ADDITIONAL_ENTRIES_SIZE;
+	status = efi_high_alloc(sys_table, new_fdt_size, 0, &new_fdt_addr,
+			    dram_base + MAX_ZIMAGE_OFFSET);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "ERROR: Unable to allocate memory for new device tree.\n");
+		goto fail;
+	}
+
+
+	fdt = (void *)new_fdt_addr;
+	status = fdt_open_into(orig_fdt, fdt, new_fdt_size);
+	if (status != 0) {
+		efi_printk(sys_table, "ERROR: Device Tree open_int failed.\n");
+		goto fail_free_new_fdt;
+	}
+	/* We are done with the original DTB, so free it. */
+	efi_free(sys_table, *orig_fdt_size, (u32)orig_fdt);
+	*orig_fdt_size = 0;
+
+	node = fdt_subnode_offset(fdt, 0, "chosen");
+	if (node < 0) {
+		node = fdt_add_subnode(fdt, 0, "chosen");
+		if (node < 0) {
+			efi_printk(sys_table, "Error on finding 'chosen' node\n");
+			goto fail_free_new_fdt;
+		}
+	}
+
+	if ((cmdline_ptr != NULL) && (strlen(cmdline_ptr) > 0)) {
+		status = fdt_setprop(fdt, node, "bootargs", cmdline_ptr,
+				     strlen(cmdline_ptr) + 1);
+		if (status) {
+			efi_printk(sys_table, "Failed to set new bootarg\n");
+			goto fail_free_new_fdt;
+		}
+	}
+	/* We are done with original command line, so free it. */
+	efi_free(sys_table, *cmdline_size, (u32)cmdline_ptr);
+	*cmdline_size = 0;
+
+	/* Set intird address/end in device tree, if present */
+	if (initrd_size != 0) {
+		u64 initrd_image_end;
+		u64 initrd_image_start = cpu_to_fdt64(initrd_addr);
+		status = fdt_setprop(fdt, node, "linux,initrd-start",
+				     &initrd_image_start, sizeof(u64));
+		if (status) {
+			efi_printk(sys_table, "Failed to set new 'linux,initrd-start'\n");
+			goto fail_free_new_fdt;
+		}
+		initrd_image_end = cpu_to_fdt64(initrd_addr + initrd_size);
+		status = fdt_setprop(fdt, node, "linux,initrd-end",
+				     &initrd_image_end, sizeof(u64));
+		if (status) {
+			efi_printk(sys_table, "Failed to set new 'linux,initrd-end'\n");
+			goto fail_free_new_fdt;
+		}
+	}
+
+	/* Update memory map in the device tree. The memory node must
+	 * be present in the tree.*/
+	node = fdt_subnode_offset(fdt, 0, "memory");
+	if (node < 0) {
+		efi_printk(sys_table, "ERROR: FDT memory node does not exist in DTB.\n");
+		goto fail_free_new_fdt;
+	}
+
+	status = efi_get_memory_map(sys_table, &memory_map, &map_size,
+				    &desc_size, &mmap_key);
+	if (status != EFI_SUCCESS)
+		goto fail_free_new_fdt;
+
+	for (i = 0; i < (map_size / sizeof(efi_memory_desc_t)); i++) {
+		efi_memory_desc_t *desc;
+		unsigned long m = (unsigned long)memory_map;
+		desc = (efi_memory_desc_t *)(m + (i * desc_size));
+
+		if (is_linux_reserved_region(desc->type)) {
+			status = fdt_add_mem_rsv(fdt, desc->phys_addr,
+						 desc->num_pages * EFI_PAGE_SIZE);
+			if (status != 0) {
+				efi_printk(sys_table, "ERROR: Failed to add 'memreserve' to fdt.\n");
+				goto fail_free_mmap;
+			}
+		}
+	}
+
+
+	/* Add FDT entries for EFI runtime services in chosen node.
+	 * We need to add the final memory map, so this is done at
+	 * the very end.
+	 */
+	node = fdt_subnode_offset(fdt, 0, "chosen");
+	fdt_val = cpu_to_fdt32((unsigned long)sys_table);
+	status = fdt_setprop(fdt, node, "efi-system-table",
+			     &fdt_val, sizeof(fdt_val));
+	if (status) {
+		efi_printk(sys_table, "Failed to set new 'efi-system-table'\n");
+		goto fail_free_new_fdt;
+	}
+	fdt_val = cpu_to_fdt32(desc_size);
+	status = fdt_setprop(fdt, node, "efi-mmap-desc-size",
+			     &fdt_val, sizeof(fdt_val));
+	if (status) {
+		efi_printk(sys_table, "Failed to set new 'efi-mmap-desc-size'\n");
+		goto fail_free_new_fdt;
+	}
+	fdt_val = cpu_to_fdt32(map_size);
+	status = fdt_setprop(fdt, node, "efi-runtime-mmap-size",
+			     &fdt_val, sizeof(fdt_val));
+	if (status) {
+		efi_printk(sys_table, "Failed to set new 'efi-runtime-mmap-size'\n");
+		goto fail_free_new_fdt;
+	}
+	fdt_val = cpu_to_fdt32((unsigned long)memory_map);
+	status = fdt_setprop(fdt, node, "efi-runtime-mmap",
+			     &fdt_val, sizeof(fdt_val));
+	if (status) {
+		efi_printk(sys_table, "Failed to set new 'efi-runtime-mmap'\n");
+		goto fail_free_new_fdt;
+	}
+
+	/* Now we need to exit boot services.  We need the key from
+	 * the most recent read of the memory map to do this.  We can't
+	 * free this buffer in the normal case, but do free it when
+	 * exit_boot_services() fails or adding the memory map to the FDT
+	 * fails.
+	 */
+	status = efi_call_phys2(sys_table->boottime->exit_boot_services,
+				handle, mmap_key);
+
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "exit boot services failed.\n");
+		goto fail_free_mmap;
+	}
+
+	return new_fdt_addr;
+
+fail_free_mmap:
+	efi_call_phys1(sys_table->boottime->free_pool, memory_map);
+
+fail_free_new_fdt:
+	efi_free(sys_table, new_fdt_size, new_fdt_addr);
+
+fail:
+	return 0;
+}
+
+
+int efi_entry(void *handle, efi_system_table_t *sys_table,
+	      unsigned long *zimage_addr)
+{
+	efi_loaded_image_t *image;
+	int status;
+	unsigned long nr_pages;
+	const struct fdt_region *region;
+
+	void *fdt;
+	int err;
+	int node;
+	unsigned long zimage_size = 0;
+	unsigned long dram_base;
+	/* addr/point and size pairs for memory management*/
+	u64 initrd_addr;
+	u64 initrd_size = 0;
+	u64 fdt_addr;  /* Original DTB */
+	u64 fdt_size = 0;
+	u64 kernel_reserve_addr;
+	u64 kernel_reserve_size = 0;
+	char *cmdline_ptr;
+	unsigned long cmdline_size = 0;
+	unsigned long new_fdt_addr;
+
+	efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
+
+	/* Check if we were booted by the EFI firmware */
+	if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
+		goto fail;
+
+	efi_printk(sys_table, "Booting Linux using EFI stub.\n");
+
+
+	/* get the command line from EFI, using the LOADED_IMAGE protocol */
+	status = efi_call_phys3(sys_table->boottime->handle_protocol,
+				handle, &proto, (void *)&image);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
+		goto fail;
+	}
+
+	/* We are going to copy this into device tree, so we don't care where in
+	 * memory it is.
+	 */
+	cmdline_ptr = convert_cmdline_to_ascii(sys_table, image,
+					       &cmdline_size, 0xFFFFFFFF);
+	if (!cmdline_ptr) {
+		efi_printk(sys_table, "ERROR converting command line to ascii.\n");
+		goto fail;
+	}
+
+	/* We first load the device tree, as we need to get the base address of
+	 * DRAM from the device tree.  The zImage, device tree, and initrd
+	 * have address restrictions that are relative to the base of DRAM.
+	 */
+	status = handle_cmdline_files(sys_table, image, cmdline_ptr, "dtb=",
+				      0xffffffff, &fdt_addr, &fdt_size);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "Error loading dtb blob\n");
+		goto fail_free_cmdline;
+	}
+
+	err = fdt_check_header((void *)(unsigned long)fdt_addr);
+	if (err != 0) {
+		efi_printk(sys_table, "ERROR: Device Tree header not valid\n");
+		goto fail_free_dtb;
+	}
+	if (fdt_totalsize((void *)(unsigned long)fdt_addr) > fdt_size) {
+		efi_printk(sys_table, "ERROR: Incomplete device tree.\n");
+		goto fail_free_dtb;
+
+	}
+
+
+	/* Look up the base of DRAM from the device tree.*/
+	fdt = (void *)(u32)fdt_addr;
+	node = fdt_subnode_offset(fdt, 0, "memory");
+	region = fdt_getprop(fdt, node, "reg", NULL);
+	if (region) {
+		dram_base = fdt64_to_cpu(region->base);
+	} else {
+		efi_printk(sys_table, "Error: no 'memory' node in device tree.\n");
+		goto fail_free_dtb;
+	}
+
+	/* Reserve memory for the uncompressed kernel image. */
+	kernel_reserve_addr = dram_base;
+	kernel_reserve_size = MAX_UNCOMP_KERNEL_SIZE;
+	nr_pages = round_up(kernel_reserve_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
+	status = efi_call_phys4(sys_table->boottime->allocate_pages,
+				EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
+				nr_pages, &kernel_reserve_addr);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "ERROR allocating memory for uncompressed kernel.\n");
+		goto fail_free_dtb;
+	}
+
+	/* Relocate the zImage, if required. */
+	zimage_size = image->image_size;
+	status = relocate_kernel(sys_table, zimage_addr, &zimage_size,
+				 dram_base + MIN_ZIMAGE_OFFSET,
+				 dram_base + MAX_ZIMAGE_OFFSET);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "Failed to relocate kernel\n");
+		goto fail_free_kernel_reserve;
+	}
+
+	status = handle_cmdline_files(sys_table, image, cmdline_ptr, "initrd=",
+				      dram_base + MAX_ZIMAGE_OFFSET,
+				      &initrd_addr, &initrd_size);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "Error loading initrd\n");
+		goto fail_free_zimage;
+	}
+
+	new_fdt_addr = update_fdt_and_exit_boot(sys_table, handle,
+						dram_base, fdt, &fdt_size,
+						cmdline_ptr, &cmdline_size,
+						initrd_addr, initrd_size);
+
+	if (new_fdt_addr == 0) {
+		efi_printk(sys_table, "Error updating device tree and exiting boot services.\n");
+		goto fail_free_initrd;
+	}
+
+
+	/* Now we need to return the FDT address to the calling
+	 * assembly to this can be used as part of normal boot.
+	 */
+	return new_fdt_addr;
+
+fail_free_initrd:
+	efi_free(sys_table, initrd_size, initrd_addr);
+
+fail_free_zimage:
+	efi_free(sys_table, zimage_size, *zimage_addr);
+
+fail_free_kernel_reserve:
+	efi_free(sys_table, kernel_reserve_addr, kernel_reserve_size);
+
+fail_free_dtb:
+	efi_free(sys_table, fdt_size, fdt_addr);
+
+fail_free_cmdline:
+	efi_free(sys_table, cmdline_size, (u32)cmdline_ptr);
+
+fail:
+	return EFI_STUB_ERROR;
+}
diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
index 75189f1..4c70b9e 100644
--- a/arch/arm/boot/compressed/head.S
+++ b/arch/arm/boot/compressed/head.S
@@ -122,19 +122,106 @@
 		.arm				@ Always enter in ARM state
 start:
 		.type	start,#function
-		.rept	7
+#ifdef CONFIG_EFI_STUB
+		@ Magic MSDOS signature for PE/COFF + ADD opcode
+		.word	0x62805a4d
+#else
+		mov	r0, r0
+#endif
+		.rept	5
 		mov	r0, r0
 		.endr
    ARM(		mov	r0, r0		)
    ARM(		b	1f		)
  THUMB(		adr	r12, BSYM(1f)	)
  THUMB(		bx	r12		)
+ THUMB(		.thumb			)
+1:
+		b	zimage_continue
 
 		.word	0x016f2818		@ Magic numbers to help the loader
 		.word	start			@ absolute load/run zImage address
 		.word	_edata			@ zImage end address
+
+#ifdef CONFIG_EFI_STUB
+		@ Portions of the MSDOS file header must be at offset
+		@ 0x3c from the start of the file.  All PE/COFF headers
+		@ are kept contiguous for simplicity.
+#include "efi-header.S"
+
+efi_stub_entry:
+		.text
+		@ The EFI stub entry point is not at a fixed address, however
+		@ this address must be set in the PE/COFF header.
+		@ EFI entry point is in A32 mode, switch to T32 if configured.
+		.arm
+   ARM(		mov	r0, r0		)
+   ARM(		b	1f		)
+ THUMB(		adr	r12, BSYM(1f)	)
+ THUMB(		bx	r12		)
  THUMB(		.thumb			)
 1:
+		@ Save lr on stack for possible return to EFI firmware.
+		@ Don't care about fp, but need 64 bit alignment....
+		stmfd	sp!, {fp, lr}
+
+		@ Save args to EFI app across got fixup call
+		stmfd	sp!, {r0, r1}
+		ldmfd	sp!, {r0, r1}
+
+		@ allocate space on stack for return of new entry point of
+		@ zImage, as EFI stub may copy the kernel.  Pass address
+		@ of space in r2 - EFI stub will fill in the pointer.
+
+		sub	sp, #8			@ we only need 4 bytes,
+						@ but keep stack 8 byte aligned.
+		mov	r2, sp
+		@ Pass our actual runtime start address in pointer data
+		adr	r11, LC0		@ address of LC0 at run time
+		ldr	r12, [r11, #0]		@ address of LC0 at link time
+
+		sub	r3, r11, r12		@ calculate the delta offset
+		str	r3, [r2, #0]
+		bl	efi_entry
+
+		@ get new zImage entry address from stack, put into r3
+		ldr	r3, [sp, #0]
+		add	sp, #8  @ restore stack
+
+		@ Check for error return from EFI stub (0xFFFFFFFF)
+		ldr	r1, =0xffffffff
+		cmp	r0, r1
+		beq	efi_load_fail
+
+
+		@ Save return values of efi_entry
+		stmfd	sp!, {r0, r3}
+		bl	cache_clean_flush
+		bl	cache_off
+		ldmfd   sp!, {r0, r3}
+
+		@ put DTB address in r2, it was returned by EFI entry
+		mov	r2, r0
+		ldr	r1, =0xffffffff		@ DTB machine type
+		mov	r0, #0  @ r0 is 0
+
+		@ Branch to (possibly) relocated zImage entry that is in r3
+		bx	r3
+
+efi_load_fail:
+	@ We need to exit THUMB mode here, to return to EFI firmware.
+ THUMB(		adr	r12, BSYM(1f)	)
+ THUMB(		bx	r12		)
+1:
+		.arm
+		@ Return EFI_LOAD_ERROR to EFI firmware on error.
+		ldr	r0, =0x80000001
+		ldmfd	sp!, {fp, lr}
+		mov	pc, lr
+ THUMB(		.thumb			)
+#endif
+
+zimage_continue:
 		mrs	r9, cpsr
 #ifdef CONFIG_ARM_VIRT_EXT
 		bl	__hyp_stub_install	@ get into SVC mode, reversibly
@@ -167,7 +254,6 @@ not_angel:
 		 * by the linker here, but it should preserve r7, r8, and r9.
 		 */
 
-		.text
 
 #ifdef CONFIG_AUTO_ZRELADDR
 		@ determine final kernel image address
-- 
1.7.10.4


^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH 6/7] Add EFI stub for ARM
@ 2013-08-02 21:29   ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-02 21:29 UTC (permalink / raw)
  To: linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-efi-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matt.fleming-ral2JQCrhuEAvxtiuMwx3w,
	linux-lFZ/pmaqli7XmaaqVzeoHQ
  Cc: leif.lindholm-QSEj5FYQhm4dnm+yROfE0A, Roy Franz

This patch adds EFI stub support for the ARM Linux kernel.  The EFI stub
operations similarly to the x86 stub: it is a shim between the EFI firmware
and the normal zImage entry point, and sets up the environment that the
zImage is expecting.  This includes loading the initrd (optionaly) and
device tree from the system partition based on the kernel command line.
The stub updates the device tree as necessary, including adding reserved
memory regions and adding entries for EFI runtime services. The PE/COFF
"MZ" header at offset 0 results in the first instruction being an add
that corrupts r5, which is not used by the zImage interface.

Signed-off-by: Roy Franz <roy.franz-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
---
 arch/arm/boot/compressed/Makefile     |   18 +-
 arch/arm/boot/compressed/efi-header.S |  114 ++++++++
 arch/arm/boot/compressed/efi-stub.c   |  514 +++++++++++++++++++++++++++++++++
 arch/arm/boot/compressed/head.S       |   90 +++++-
 4 files changed, 732 insertions(+), 4 deletions(-)
 create mode 100644 arch/arm/boot/compressed/efi-header.S
 create mode 100644 arch/arm/boot/compressed/efi-stub.c

diff --git a/arch/arm/boot/compressed/Makefile b/arch/arm/boot/compressed/Makefile
index 7ac1610..c62826a 100644
--- a/arch/arm/boot/compressed/Makefile
+++ b/arch/arm/boot/compressed/Makefile
@@ -106,8 +106,22 @@ $(addprefix $(obj)/,$(libfdt) $(libfdt_hdrs)): $(obj)/%: $(srctree)/scripts/dtc/
 $(addprefix $(obj)/,$(libfdt_objs) atags_to_fdt.o): \
 	$(addprefix $(obj)/,$(libfdt_hdrs))
 
+$(addprefix $(obj)/,$(libfdt_objs) efi-stub.o): \
+	$(addprefix $(obj)/,$(libfdt_hdrs))
+
 ifeq ($(CONFIG_ARM_ATAG_DTB_COMPAT),y)
-OBJS	+= $(libfdt_objs) atags_to_fdt.o
+OBJS	+= atags_to_fdt.o
+USE_LIBFDT = y
+endif
+
+ifeq ($(CONFIG_EFI_STUB),y)
+CFLAGS_efi-stub.o += -DTEXT_OFFSET=$(TEXT_OFFSET)
+OBJS	+= efi-stub.o
+USE_LIBFDT = y
+endif
+
+ifeq ($(USE_LIBFDT),y)
+OBJS	+= $(libfdt_objs)
 endif
 
 targets       := vmlinux vmlinux.lds \
@@ -125,7 +139,7 @@ ORIG_CFLAGS := $(KBUILD_CFLAGS)
 KBUILD_CFLAGS = $(subst -pg, , $(ORIG_CFLAGS))
 endif
 
-ccflags-y := -fpic -mno-single-pic-base -fno-builtin -I$(obj)
+ccflags-y := -fpic -mno-single-pic-base -fno-builtin -I$(obj) -fno-stack-protector
 asflags-y := -DZIMAGE
 
 # Supply kernel BSS size to the decompressor via a linker symbol.
diff --git a/arch/arm/boot/compressed/efi-header.S b/arch/arm/boot/compressed/efi-header.S
new file mode 100644
index 0000000..6ff32cc
--- /dev/null
+++ b/arch/arm/boot/compressed/efi-header.S
@@ -0,0 +1,114 @@
+@ Copyright (C) 2013 Linaro Ltd;  <roy.franz-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
+@
+@ This file contains the PE/COFF header that is part of the
+@ EFI stub.
+@
+
+	.org	0x3c
+	@
+	@ The PE header can be anywhere in the file, but for
+	@ simplicity we keep it together with the MSDOS header
+	@ The offset to the PE/COFF header needs to be at offset
+	@ 0x3C in the MSDOS header.
+	@ The only 2 fields of the MSDOS header that are used are this
+	@ PE/COFF offset, and the "MZ" bytes at offset 0x0.
+	@
+	.long	pe_header			@ Offset to the PE header.
+
+      .align 3
+pe_header:
+
+
+pe_header:
+	.ascii	"PE"
+	.short 	0
+
+coff_header:
+	.short	0x01c2				@ ARM or Thumb
+	.short	2				@ nr_sections
+	.long	0 				@ TimeDateStamp
+	.long	0				@ PointerToSymbolTable
+	.long	1				@ NumberOfSymbols
+	.short	section_table - optional_header	@ SizeOfOptionalHeader
+	.short	0x306				@ Characteristics.
+						@ IMAGE_FILE_32BIT_MACHINE |
+						@ IMAGE_FILE_DEBUG_STRIPPED |
+						@ IMAGE_FILE_EXECUTABLE_IMAGE |
+						@ IMAGE_FILE_LINE_NUMS_STRIPPED
+
+optional_header:
+	.short	0x10b				@ PE32 format
+	.byte	0x02				@ MajorLinkerVersion
+	.byte	0x14				@ MinorLinkerVersion
+
+	.long	0				@ SizeOfCode
+
+	.long	0				@ SizeOfInitializedData
+	.long	0				@ SizeOfUninitializedData
+
+	.long	efi_stub_entry			@ AddressOfEntryPoint
+	.long	efi_stub_entry			@ BaseOfCode
+	.long	0				@ data
+
+extra_header_fields:
+	.long	0				@ ImageBase
+	.long	0x20				@ SectionAlignment
+	.long	0x20				@ FileAlignment
+	.short	0				@ MajorOperatingSystemVersion
+	.short	0				@ MinorOperatingSystemVersion
+	.short	0				@ MajorImageVersion
+	.short	0				@ MinorImageVersion
+	.short	0				@ MajorSubsystemVersion
+	.short	0				@ MinorSubsystemVersion
+	.long	0				@ Win32VersionValue
+
+	.long	_edata				@ SizeOfImage
+
+	@ Everything before the entry point is considered part of the header
+	.long	efi_stub_entry			@ SizeOfHeaders
+	.long	0				@ CheckSum
+	.short	0xa				@ Subsystem (EFI application)
+	.short	0				@ DllCharacteristics
+	.long	0				@ SizeOfStackReserve
+	.long	0				@ SizeOfStackCommit
+	.long	0				@ SizeOfHeapReserve
+	.long	0				@ SizeOfHeapCommit
+	.long	0				@ LoaderFlags
+	.long	0x0				@ NumberOfRvaAndSizes
+
+	# Section table
+section_table:
+
+	#
+	# The EFI application loader requires a relocation section
+	# because EFI applications must be relocatable.  This is a
+	# dummy section as far as we are concerned.
+	#
+	.ascii	".reloc"
+	.byte	0
+	.byte	0			@ end of 0 padding of section name
+	.long	0
+	.long	0
+	.long	0			@ SizeOfRawData
+	.long	0			@ PointerToRawData
+	.long	0			@ PointerToRelocations
+	.long	0			@ PointerToLineNumbers
+	.short	0			@ NumberOfRelocations
+	.short	0			@ NumberOfLineNumbers
+	.long	0x42100040		@ Characteristics (section flags)
+
+
+	.ascii	".text"
+	.byte	0
+	.byte	0
+	.byte	0        		@ end of 0 padding of section name
+	.long	_edata - efi_stub_entry		@ VirtualSize
+	.long	efi_stub_entry			@ VirtualAddress
+	.long	_edata - efi_stub_entry		@ SizeOfRawData
+	.long	efi_stub_entry			@ PointerToRawData
+
+	.long	0		@ PointerToRelocations (0 for executables)
+	.long	0		@ PointerToLineNumbers (0 for executables)
+	.short	0		@ NumberOfRelocations  (0 for executables)
+	.short	0		@ NumberOfLineNumbers  (0 for executables)
+	.long	0xe0500020	@ Characteristics (section flags)
diff --git a/arch/arm/boot/compressed/efi-stub.c b/arch/arm/boot/compressed/efi-stub.c
new file mode 100644
index 0000000..41fa1e2
--- /dev/null
+++ b/arch/arm/boot/compressed/efi-stub.c
@@ -0,0 +1,514 @@
+/*
+ * linux/arch/arm/boot/compressed/efi-stub.c
+ *
+ * Copyright (C) 2013 Linaro Ltd;  <roy.franz-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
+ *
+ * This file implements the EFI boot stub for the ARM kernel
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+#include <linux/efi.h>
+#include <libfdt.h>
+
+
+/* Error code returned to ASM code instead of valid FDT address. */
+#define EFI_STUB_ERROR		(~0)
+
+/* EFI function call wrappers.  These are not required for
+ * ARM, but wrappers are required for X86 to convert between
+ * ABIs.  These wrappers are provided to allow code sharing
+ * between X86 and ARM.  Since these wrappers directly invoke the
+ * EFI function pointer, the function pointer type must be properly
+ * defined, which is not the case for X86  One advantage of this is
+ * it allows for type checking of arguments, which is not
+ * possible with the X86 wrappers.
+ */
+#define efi_call_phys0(f)			f()
+#define efi_call_phys1(f, a1)			f(a1)
+#define efi_call_phys2(f, a1, a2)		f(a1, a2)
+#define efi_call_phys3(f, a1, a2, a3)		f(a1, a2, a3)
+#define efi_call_phys4(f, a1, a2, a3, a4)	f(a1, a2, a3, a4)
+#define efi_call_phys5(f, a1, a2, a3, a4, a5)	f(a1, a2, a3, a4, a5)
+
+/* The maximum uncompressed kernel size is 32 MBytes, so we will reserve
+ * that for the decompressed kernel.  We have no easy way to tell what
+ * the actuall size of code + data the uncompressed kernel will use.
+ */
+#define MAX_UNCOMP_KERNEL_SIZE	0x02000000
+
+/* The kernel zImage should be located between 32 Mbytes
+ * and 128 MBytes from the base of DRAM.  The min
+ * address leaves space for a maximal size uncompressed image,
+ * and the max address is due to how the zImage decompressor
+ * picks a destination address.
+ */
+#define MAX_ZIMAGE_OFFSET	0x08000000
+#define MIN_ZIMAGE_OFFSET	MAX_UNCOMP_KERNEL_SIZE
+
+#define MAX_CMDLINE_LEN		500
+
+struct fdt_region {
+	u64 base;
+	u64 size;
+};
+
+/*
+ * Additional size that could be used for FDT entries added by
+ * the UEFI OS Loader Estimation based on:
+ * EDID (300bytes) + bootargs  + initrd region (20bytes)
+ * + system memory region (20bytes) + mp_core entries (200
+ * bytes)
+ */
+#define FDT_ADDITIONAL_ENTRIES_SIZE     (0x300 + MAX_CMDLINE_LEN)
+
+/* Include shared EFI stub code */
+#include "../../../../drivers/firmware/efi/efi-stub-helper.c"
+
+
+static int is_linux_reserved_region(int memory_type)
+{
+	switch (memory_type) {
+	case EfiRuntimeServicesCode:
+	case EfiRuntimeServicesData:
+	case EfiUnusableMemory:
+	case EfiACPIReclaimMemory:
+	case EfiACPIMemoryNVS:
+		return 1;
+	default:
+		return 0;
+	}
+}
+
+
+static int relocate_kernel(efi_system_table_t *sys_table,
+			   unsigned long *load_addr, unsigned long *load_size,
+			   unsigned long min_addr, unsigned long max_addr)
+{
+	/* Get current address of kernel. */
+	unsigned long cur_zimage_addr = *load_addr;
+	unsigned long zimage_size = *load_size;
+	unsigned long new_addr = 0;
+	unsigned long nr_pages;
+
+	efi_status_t status;
+
+	if (!load_addr || !load_size)
+		return EFI_INVALID_PARAMETER;
+
+	*load_size = 0;
+	if (cur_zimage_addr > min_addr
+	    && (cur_zimage_addr + zimage_size) < max_addr) {
+		/* We don't need to do anything, as kernel at an acceptable
+		 * address already.
+		 */
+		return EFI_SUCCESS;
+	}
+	/*
+	 * The EFI firmware loader could have placed the kernel image
+	 * anywhere in memory, but the kernel has restrictions on the
+	 * min and max physical address it can run at.
+	 */
+	nr_pages = round_up(zimage_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
+
+	status = efi_low_alloc(sys_table, zimage_size, 0,
+			   &new_addr, min_addr);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "Failed to alloc memory for kernel.\n");
+		return status;
+	}
+
+	if (new_addr > (max_addr - zimage_size)) {
+		efi_free(sys_table, zimage_size, new_addr);
+		efi_printk(sys_table, "Failed to alloc usable memory for kernel.\n");
+		return EFI_INVALID_PARAMETER;
+	}
+
+	/* We know source/dest won't overlap since both memory ranges
+	 * have been allocated by UEFI, so we can safely use memcpy.
+	 */
+	memcpy((void *)new_addr, (void *)(unsigned long)cur_zimage_addr,
+	       zimage_size);
+
+	/* Return the load address and size */
+	*load_addr = new_addr;
+	*load_size = zimage_size;
+
+
+	return status;
+}
+
+
+/* Convert the unicode UEFI command line to ASCII to pass to kernel.
+ * Size of memory allocated return in *cmd_line_len.
+ * Returns NULL on error.
+ */
+static char *convert_cmdline_to_ascii(efi_system_table_t *sys_table,
+				      efi_loaded_image_t *image,
+				      unsigned long *cmd_line_len,
+				      u32 max_addr)
+{
+	u16 *s2;
+	u8 *s1 = NULL;
+	unsigned long cmdline_addr = 0;
+	int load_options_size = image->load_options_size / 2; /* ASCII */
+	void *options = (u16 *)image->load_options;
+	int options_size = 0;
+	int status;
+	int i;
+	u16 zero = 0;
+
+	if (options) {
+		s2 = options;
+		while (*s2 && *s2 != '\n' && options_size < load_options_size) {
+			s2++;
+			options_size++;
+		}
+	}
+
+	if (options_size == 0) {
+		/* No command line options, so return empty string*/
+		options_size = 1;
+		options = &zero;
+	}
+
+	if (options_size > MAX_CMDLINE_LEN)
+		options_size = MAX_CMDLINE_LEN;
+
+	options_size++;  /* NUL termination */
+
+	status = efi_high_alloc(sys_table, options_size, 0,
+			    &cmdline_addr, max_addr);
+	if (status != EFI_SUCCESS)
+		return NULL;
+
+	s1 = (u8 *)(unsigned long)cmdline_addr;
+	s2 = (u16 *)options;
+
+	for (i = 0; i < options_size - 1; i++)
+		*s1++ = *s2++;
+
+	*s1 = '\0';
+
+	*cmd_line_len = options_size;
+	return (char *)(unsigned long)cmdline_addr;
+}
+
+static u32 update_fdt_and_exit_boot(efi_system_table_t *sys_table,
+				    void *handle, unsigned long dram_base,
+				    void *orig_fdt, u64 *orig_fdt_size,
+				    char *cmdline_ptr,
+				    unsigned long *cmdline_size,
+				    u64 initrd_addr, u64 initrd_size)
+{
+	unsigned long new_fdt_size;
+	unsigned long new_fdt_addr;
+	void *fdt;
+	int node;
+	int status;
+	int i;
+	unsigned long map_size, desc_size;
+	unsigned long mmap_key;
+	efi_memory_desc_t *memory_map;
+	unsigned long fdt_val;
+
+	new_fdt_size = *orig_fdt_size + FDT_ADDITIONAL_ENTRIES_SIZE;
+	status = efi_high_alloc(sys_table, new_fdt_size, 0, &new_fdt_addr,
+			    dram_base + MAX_ZIMAGE_OFFSET);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "ERROR: Unable to allocate memory for new device tree.\n");
+		goto fail;
+	}
+
+
+	fdt = (void *)new_fdt_addr;
+	status = fdt_open_into(orig_fdt, fdt, new_fdt_size);
+	if (status != 0) {
+		efi_printk(sys_table, "ERROR: Device Tree open_int failed.\n");
+		goto fail_free_new_fdt;
+	}
+	/* We are done with the original DTB, so free it. */
+	efi_free(sys_table, *orig_fdt_size, (u32)orig_fdt);
+	*orig_fdt_size = 0;
+
+	node = fdt_subnode_offset(fdt, 0, "chosen");
+	if (node < 0) {
+		node = fdt_add_subnode(fdt, 0, "chosen");
+		if (node < 0) {
+			efi_printk(sys_table, "Error on finding 'chosen' node\n");
+			goto fail_free_new_fdt;
+		}
+	}
+
+	if ((cmdline_ptr != NULL) && (strlen(cmdline_ptr) > 0)) {
+		status = fdt_setprop(fdt, node, "bootargs", cmdline_ptr,
+				     strlen(cmdline_ptr) + 1);
+		if (status) {
+			efi_printk(sys_table, "Failed to set new bootarg\n");
+			goto fail_free_new_fdt;
+		}
+	}
+	/* We are done with original command line, so free it. */
+	efi_free(sys_table, *cmdline_size, (u32)cmdline_ptr);
+	*cmdline_size = 0;
+
+	/* Set intird address/end in device tree, if present */
+	if (initrd_size != 0) {
+		u64 initrd_image_end;
+		u64 initrd_image_start = cpu_to_fdt64(initrd_addr);
+		status = fdt_setprop(fdt, node, "linux,initrd-start",
+				     &initrd_image_start, sizeof(u64));
+		if (status) {
+			efi_printk(sys_table, "Failed to set new 'linux,initrd-start'\n");
+			goto fail_free_new_fdt;
+		}
+		initrd_image_end = cpu_to_fdt64(initrd_addr + initrd_size);
+		status = fdt_setprop(fdt, node, "linux,initrd-end",
+				     &initrd_image_end, sizeof(u64));
+		if (status) {
+			efi_printk(sys_table, "Failed to set new 'linux,initrd-end'\n");
+			goto fail_free_new_fdt;
+		}
+	}
+
+	/* Update memory map in the device tree. The memory node must
+	 * be present in the tree.*/
+	node = fdt_subnode_offset(fdt, 0, "memory");
+	if (node < 0) {
+		efi_printk(sys_table, "ERROR: FDT memory node does not exist in DTB.\n");
+		goto fail_free_new_fdt;
+	}
+
+	status = efi_get_memory_map(sys_table, &memory_map, &map_size,
+				    &desc_size, &mmap_key);
+	if (status != EFI_SUCCESS)
+		goto fail_free_new_fdt;
+
+	for (i = 0; i < (map_size / sizeof(efi_memory_desc_t)); i++) {
+		efi_memory_desc_t *desc;
+		unsigned long m = (unsigned long)memory_map;
+		desc = (efi_memory_desc_t *)(m + (i * desc_size));
+
+		if (is_linux_reserved_region(desc->type)) {
+			status = fdt_add_mem_rsv(fdt, desc->phys_addr,
+						 desc->num_pages * EFI_PAGE_SIZE);
+			if (status != 0) {
+				efi_printk(sys_table, "ERROR: Failed to add 'memreserve' to fdt.\n");
+				goto fail_free_mmap;
+			}
+		}
+	}
+
+
+	/* Add FDT entries for EFI runtime services in chosen node.
+	 * We need to add the final memory map, so this is done at
+	 * the very end.
+	 */
+	node = fdt_subnode_offset(fdt, 0, "chosen");
+	fdt_val = cpu_to_fdt32((unsigned long)sys_table);
+	status = fdt_setprop(fdt, node, "efi-system-table",
+			     &fdt_val, sizeof(fdt_val));
+	if (status) {
+		efi_printk(sys_table, "Failed to set new 'efi-system-table'\n");
+		goto fail_free_new_fdt;
+	}
+	fdt_val = cpu_to_fdt32(desc_size);
+	status = fdt_setprop(fdt, node, "efi-mmap-desc-size",
+			     &fdt_val, sizeof(fdt_val));
+	if (status) {
+		efi_printk(sys_table, "Failed to set new 'efi-mmap-desc-size'\n");
+		goto fail_free_new_fdt;
+	}
+	fdt_val = cpu_to_fdt32(map_size);
+	status = fdt_setprop(fdt, node, "efi-runtime-mmap-size",
+			     &fdt_val, sizeof(fdt_val));
+	if (status) {
+		efi_printk(sys_table, "Failed to set new 'efi-runtime-mmap-size'\n");
+		goto fail_free_new_fdt;
+	}
+	fdt_val = cpu_to_fdt32((unsigned long)memory_map);
+	status = fdt_setprop(fdt, node, "efi-runtime-mmap",
+			     &fdt_val, sizeof(fdt_val));
+	if (status) {
+		efi_printk(sys_table, "Failed to set new 'efi-runtime-mmap'\n");
+		goto fail_free_new_fdt;
+	}
+
+	/* Now we need to exit boot services.  We need the key from
+	 * the most recent read of the memory map to do this.  We can't
+	 * free this buffer in the normal case, but do free it when
+	 * exit_boot_services() fails or adding the memory map to the FDT
+	 * fails.
+	 */
+	status = efi_call_phys2(sys_table->boottime->exit_boot_services,
+				handle, mmap_key);
+
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "exit boot services failed.\n");
+		goto fail_free_mmap;
+	}
+
+	return new_fdt_addr;
+
+fail_free_mmap:
+	efi_call_phys1(sys_table->boottime->free_pool, memory_map);
+
+fail_free_new_fdt:
+	efi_free(sys_table, new_fdt_size, new_fdt_addr);
+
+fail:
+	return 0;
+}
+
+
+int efi_entry(void *handle, efi_system_table_t *sys_table,
+	      unsigned long *zimage_addr)
+{
+	efi_loaded_image_t *image;
+	int status;
+	unsigned long nr_pages;
+	const struct fdt_region *region;
+
+	void *fdt;
+	int err;
+	int node;
+	unsigned long zimage_size = 0;
+	unsigned long dram_base;
+	/* addr/point and size pairs for memory management*/
+	u64 initrd_addr;
+	u64 initrd_size = 0;
+	u64 fdt_addr;  /* Original DTB */
+	u64 fdt_size = 0;
+	u64 kernel_reserve_addr;
+	u64 kernel_reserve_size = 0;
+	char *cmdline_ptr;
+	unsigned long cmdline_size = 0;
+	unsigned long new_fdt_addr;
+
+	efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
+
+	/* Check if we were booted by the EFI firmware */
+	if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
+		goto fail;
+
+	efi_printk(sys_table, "Booting Linux using EFI stub.\n");
+
+
+	/* get the command line from EFI, using the LOADED_IMAGE protocol */
+	status = efi_call_phys3(sys_table->boottime->handle_protocol,
+				handle, &proto, (void *)&image);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
+		goto fail;
+	}
+
+	/* We are going to copy this into device tree, so we don't care where in
+	 * memory it is.
+	 */
+	cmdline_ptr = convert_cmdline_to_ascii(sys_table, image,
+					       &cmdline_size, 0xFFFFFFFF);
+	if (!cmdline_ptr) {
+		efi_printk(sys_table, "ERROR converting command line to ascii.\n");
+		goto fail;
+	}
+
+	/* We first load the device tree, as we need to get the base address of
+	 * DRAM from the device tree.  The zImage, device tree, and initrd
+	 * have address restrictions that are relative to the base of DRAM.
+	 */
+	status = handle_cmdline_files(sys_table, image, cmdline_ptr, "dtb=",
+				      0xffffffff, &fdt_addr, &fdt_size);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "Error loading dtb blob\n");
+		goto fail_free_cmdline;
+	}
+
+	err = fdt_check_header((void *)(unsigned long)fdt_addr);
+	if (err != 0) {
+		efi_printk(sys_table, "ERROR: Device Tree header not valid\n");
+		goto fail_free_dtb;
+	}
+	if (fdt_totalsize((void *)(unsigned long)fdt_addr) > fdt_size) {
+		efi_printk(sys_table, "ERROR: Incomplete device tree.\n");
+		goto fail_free_dtb;
+
+	}
+
+
+	/* Look up the base of DRAM from the device tree.*/
+	fdt = (void *)(u32)fdt_addr;
+	node = fdt_subnode_offset(fdt, 0, "memory");
+	region = fdt_getprop(fdt, node, "reg", NULL);
+	if (region) {
+		dram_base = fdt64_to_cpu(region->base);
+	} else {
+		efi_printk(sys_table, "Error: no 'memory' node in device tree.\n");
+		goto fail_free_dtb;
+	}
+
+	/* Reserve memory for the uncompressed kernel image. */
+	kernel_reserve_addr = dram_base;
+	kernel_reserve_size = MAX_UNCOMP_KERNEL_SIZE;
+	nr_pages = round_up(kernel_reserve_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
+	status = efi_call_phys4(sys_table->boottime->allocate_pages,
+				EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
+				nr_pages, &kernel_reserve_addr);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "ERROR allocating memory for uncompressed kernel.\n");
+		goto fail_free_dtb;
+	}
+
+	/* Relocate the zImage, if required. */
+	zimage_size = image->image_size;
+	status = relocate_kernel(sys_table, zimage_addr, &zimage_size,
+				 dram_base + MIN_ZIMAGE_OFFSET,
+				 dram_base + MAX_ZIMAGE_OFFSET);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "Failed to relocate kernel\n");
+		goto fail_free_kernel_reserve;
+	}
+
+	status = handle_cmdline_files(sys_table, image, cmdline_ptr, "initrd=",
+				      dram_base + MAX_ZIMAGE_OFFSET,
+				      &initrd_addr, &initrd_size);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "Error loading initrd\n");
+		goto fail_free_zimage;
+	}
+
+	new_fdt_addr = update_fdt_and_exit_boot(sys_table, handle,
+						dram_base, fdt, &fdt_size,
+						cmdline_ptr, &cmdline_size,
+						initrd_addr, initrd_size);
+
+	if (new_fdt_addr == 0) {
+		efi_printk(sys_table, "Error updating device tree and exiting boot services.\n");
+		goto fail_free_initrd;
+	}
+
+
+	/* Now we need to return the FDT address to the calling
+	 * assembly to this can be used as part of normal boot.
+	 */
+	return new_fdt_addr;
+
+fail_free_initrd:
+	efi_free(sys_table, initrd_size, initrd_addr);
+
+fail_free_zimage:
+	efi_free(sys_table, zimage_size, *zimage_addr);
+
+fail_free_kernel_reserve:
+	efi_free(sys_table, kernel_reserve_addr, kernel_reserve_size);
+
+fail_free_dtb:
+	efi_free(sys_table, fdt_size, fdt_addr);
+
+fail_free_cmdline:
+	efi_free(sys_table, cmdline_size, (u32)cmdline_ptr);
+
+fail:
+	return EFI_STUB_ERROR;
+}
diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
index 75189f1..4c70b9e 100644
--- a/arch/arm/boot/compressed/head.S
+++ b/arch/arm/boot/compressed/head.S
@@ -122,19 +122,106 @@
 		.arm				@ Always enter in ARM state
 start:
 		.type	start,#function
-		.rept	7
+#ifdef CONFIG_EFI_STUB
+		@ Magic MSDOS signature for PE/COFF + ADD opcode
+		.word	0x62805a4d
+#else
+		mov	r0, r0
+#endif
+		.rept	5
 		mov	r0, r0
 		.endr
    ARM(		mov	r0, r0		)
    ARM(		b	1f		)
  THUMB(		adr	r12, BSYM(1f)	)
  THUMB(		bx	r12		)
+ THUMB(		.thumb			)
+1:
+		b	zimage_continue
 
 		.word	0x016f2818		@ Magic numbers to help the loader
 		.word	start			@ absolute load/run zImage address
 		.word	_edata			@ zImage end address
+
+#ifdef CONFIG_EFI_STUB
+		@ Portions of the MSDOS file header must be at offset
+		@ 0x3c from the start of the file.  All PE/COFF headers
+		@ are kept contiguous for simplicity.
+#include "efi-header.S"
+
+efi_stub_entry:
+		.text
+		@ The EFI stub entry point is not at a fixed address, however
+		@ this address must be set in the PE/COFF header.
+		@ EFI entry point is in A32 mode, switch to T32 if configured.
+		.arm
+   ARM(		mov	r0, r0		)
+   ARM(		b	1f		)
+ THUMB(		adr	r12, BSYM(1f)	)
+ THUMB(		bx	r12		)
  THUMB(		.thumb			)
 1:
+		@ Save lr on stack for possible return to EFI firmware.
+		@ Don't care about fp, but need 64 bit alignment....
+		stmfd	sp!, {fp, lr}
+
+		@ Save args to EFI app across got fixup call
+		stmfd	sp!, {r0, r1}
+		ldmfd	sp!, {r0, r1}
+
+		@ allocate space on stack for return of new entry point of
+		@ zImage, as EFI stub may copy the kernel.  Pass address
+		@ of space in r2 - EFI stub will fill in the pointer.
+
+		sub	sp, #8			@ we only need 4 bytes,
+						@ but keep stack 8 byte aligned.
+		mov	r2, sp
+		@ Pass our actual runtime start address in pointer data
+		adr	r11, LC0		@ address of LC0 at run time
+		ldr	r12, [r11, #0]		@ address of LC0 at link time
+
+		sub	r3, r11, r12		@ calculate the delta offset
+		str	r3, [r2, #0]
+		bl	efi_entry
+
+		@ get new zImage entry address from stack, put into r3
+		ldr	r3, [sp, #0]
+		add	sp, #8  @ restore stack
+
+		@ Check for error return from EFI stub (0xFFFFFFFF)
+		ldr	r1, =0xffffffff
+		cmp	r0, r1
+		beq	efi_load_fail
+
+
+		@ Save return values of efi_entry
+		stmfd	sp!, {r0, r3}
+		bl	cache_clean_flush
+		bl	cache_off
+		ldmfd   sp!, {r0, r3}
+
+		@ put DTB address in r2, it was returned by EFI entry
+		mov	r2, r0
+		ldr	r1, =0xffffffff		@ DTB machine type
+		mov	r0, #0  @ r0 is 0
+
+		@ Branch to (possibly) relocated zImage entry that is in r3
+		bx	r3
+
+efi_load_fail:
+	@ We need to exit THUMB mode here, to return to EFI firmware.
+ THUMB(		adr	r12, BSYM(1f)	)
+ THUMB(		bx	r12		)
+1:
+		.arm
+		@ Return EFI_LOAD_ERROR to EFI firmware on error.
+		ldr	r0, =0x80000001
+		ldmfd	sp!, {fp, lr}
+		mov	pc, lr
+ THUMB(		.thumb			)
+#endif
+
+zimage_continue:
 		mrs	r9, cpsr
 #ifdef CONFIG_ARM_VIRT_EXT
 		bl	__hyp_stub_install	@ get into SVC mode, reversibly
@@ -167,7 +254,6 @@ not_angel:
 		 * by the linker here, but it should preserve r7, r8, and r9.
 		 */
 
-		.text
 
 #ifdef CONFIG_AUTO_ZRELADDR
 		@ determine final kernel image address
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH 6/7] Add EFI stub for ARM
@ 2013-08-02 21:29   ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-02 21:29 UTC (permalink / raw)
  To: linux-arm-kernel

This patch adds EFI stub support for the ARM Linux kernel.  The EFI stub
operations similarly to the x86 stub: it is a shim between the EFI firmware
and the normal zImage entry point, and sets up the environment that the
zImage is expecting.  This includes loading the initrd (optionaly) and
device tree from the system partition based on the kernel command line.
The stub updates the device tree as necessary, including adding reserved
memory regions and adding entries for EFI runtime services. The PE/COFF
"MZ" header at offset 0 results in the first instruction being an add
that corrupts r5, which is not used by the zImage interface.

Signed-off-by: Roy Franz <roy.franz@linaro.org>
---
 arch/arm/boot/compressed/Makefile     |   18 +-
 arch/arm/boot/compressed/efi-header.S |  114 ++++++++
 arch/arm/boot/compressed/efi-stub.c   |  514 +++++++++++++++++++++++++++++++++
 arch/arm/boot/compressed/head.S       |   90 +++++-
 4 files changed, 732 insertions(+), 4 deletions(-)
 create mode 100644 arch/arm/boot/compressed/efi-header.S
 create mode 100644 arch/arm/boot/compressed/efi-stub.c

diff --git a/arch/arm/boot/compressed/Makefile b/arch/arm/boot/compressed/Makefile
index 7ac1610..c62826a 100644
--- a/arch/arm/boot/compressed/Makefile
+++ b/arch/arm/boot/compressed/Makefile
@@ -106,8 +106,22 @@ $(addprefix $(obj)/,$(libfdt) $(libfdt_hdrs)): $(obj)/%: $(srctree)/scripts/dtc/
 $(addprefix $(obj)/,$(libfdt_objs) atags_to_fdt.o): \
 	$(addprefix $(obj)/,$(libfdt_hdrs))
 
+$(addprefix $(obj)/,$(libfdt_objs) efi-stub.o): \
+	$(addprefix $(obj)/,$(libfdt_hdrs))
+
 ifeq ($(CONFIG_ARM_ATAG_DTB_COMPAT),y)
-OBJS	+= $(libfdt_objs) atags_to_fdt.o
+OBJS	+= atags_to_fdt.o
+USE_LIBFDT = y
+endif
+
+ifeq ($(CONFIG_EFI_STUB),y)
+CFLAGS_efi-stub.o += -DTEXT_OFFSET=$(TEXT_OFFSET)
+OBJS	+= efi-stub.o
+USE_LIBFDT = y
+endif
+
+ifeq ($(USE_LIBFDT),y)
+OBJS	+= $(libfdt_objs)
 endif
 
 targets       := vmlinux vmlinux.lds \
@@ -125,7 +139,7 @@ ORIG_CFLAGS := $(KBUILD_CFLAGS)
 KBUILD_CFLAGS = $(subst -pg, , $(ORIG_CFLAGS))
 endif
 
-ccflags-y := -fpic -mno-single-pic-base -fno-builtin -I$(obj)
+ccflags-y := -fpic -mno-single-pic-base -fno-builtin -I$(obj) -fno-stack-protector
 asflags-y := -DZIMAGE
 
 # Supply kernel BSS size to the decompressor via a linker symbol.
diff --git a/arch/arm/boot/compressed/efi-header.S b/arch/arm/boot/compressed/efi-header.S
new file mode 100644
index 0000000..6ff32cc
--- /dev/null
+++ b/arch/arm/boot/compressed/efi-header.S
@@ -0,0 +1,114 @@
+@ Copyright (C) 2013 Linaro Ltd;  <roy.franz@linaro.org>
+@
+@ This file contains the PE/COFF header that is part of the
+@ EFI stub.
+@
+
+	.org	0x3c
+	@
+	@ The PE header can be anywhere in the file, but for
+	@ simplicity we keep it together with the MSDOS header
+	@ The offset to the PE/COFF header needs to be at offset
+	@ 0x3C in the MSDOS header.
+	@ The only 2 fields of the MSDOS header that are used are this
+	@ PE/COFF offset, and the "MZ" bytes at offset 0x0.
+	@
+	.long	pe_header			@ Offset to the PE header.
+
+      .align 3
+pe_header:
+
+
+pe_header:
+	.ascii	"PE"
+	.short 	0
+
+coff_header:
+	.short	0x01c2				@ ARM or Thumb
+	.short	2				@ nr_sections
+	.long	0 				@ TimeDateStamp
+	.long	0				@ PointerToSymbolTable
+	.long	1				@ NumberOfSymbols
+	.short	section_table - optional_header	@ SizeOfOptionalHeader
+	.short	0x306				@ Characteristics.
+						@ IMAGE_FILE_32BIT_MACHINE |
+						@ IMAGE_FILE_DEBUG_STRIPPED |
+						@ IMAGE_FILE_EXECUTABLE_IMAGE |
+						@ IMAGE_FILE_LINE_NUMS_STRIPPED
+
+optional_header:
+	.short	0x10b				@ PE32 format
+	.byte	0x02				@ MajorLinkerVersion
+	.byte	0x14				@ MinorLinkerVersion
+
+	.long	0				@ SizeOfCode
+
+	.long	0				@ SizeOfInitializedData
+	.long	0				@ SizeOfUninitializedData
+
+	.long	efi_stub_entry			@ AddressOfEntryPoint
+	.long	efi_stub_entry			@ BaseOfCode
+	.long	0				@ data
+
+extra_header_fields:
+	.long	0				@ ImageBase
+	.long	0x20				@ SectionAlignment
+	.long	0x20				@ FileAlignment
+	.short	0				@ MajorOperatingSystemVersion
+	.short	0				@ MinorOperatingSystemVersion
+	.short	0				@ MajorImageVersion
+	.short	0				@ MinorImageVersion
+	.short	0				@ MajorSubsystemVersion
+	.short	0				@ MinorSubsystemVersion
+	.long	0				@ Win32VersionValue
+
+	.long	_edata				@ SizeOfImage
+
+	@ Everything before the entry point is considered part of the header
+	.long	efi_stub_entry			@ SizeOfHeaders
+	.long	0				@ CheckSum
+	.short	0xa				@ Subsystem (EFI application)
+	.short	0				@ DllCharacteristics
+	.long	0				@ SizeOfStackReserve
+	.long	0				@ SizeOfStackCommit
+	.long	0				@ SizeOfHeapReserve
+	.long	0				@ SizeOfHeapCommit
+	.long	0				@ LoaderFlags
+	.long	0x0				@ NumberOfRvaAndSizes
+
+	# Section table
+section_table:
+
+	#
+	# The EFI application loader requires a relocation section
+	# because EFI applications must be relocatable.  This is a
+	# dummy section as far as we are concerned.
+	#
+	.ascii	".reloc"
+	.byte	0
+	.byte	0			@ end of 0 padding of section name
+	.long	0
+	.long	0
+	.long	0			@ SizeOfRawData
+	.long	0			@ PointerToRawData
+	.long	0			@ PointerToRelocations
+	.long	0			@ PointerToLineNumbers
+	.short	0			@ NumberOfRelocations
+	.short	0			@ NumberOfLineNumbers
+	.long	0x42100040		@ Characteristics (section flags)
+
+
+	.ascii	".text"
+	.byte	0
+	.byte	0
+	.byte	0        		@ end of 0 padding of section name
+	.long	_edata - efi_stub_entry		@ VirtualSize
+	.long	efi_stub_entry			@ VirtualAddress
+	.long	_edata - efi_stub_entry		@ SizeOfRawData
+	.long	efi_stub_entry			@ PointerToRawData
+
+	.long	0		@ PointerToRelocations (0 for executables)
+	.long	0		@ PointerToLineNumbers (0 for executables)
+	.short	0		@ NumberOfRelocations  (0 for executables)
+	.short	0		@ NumberOfLineNumbers  (0 for executables)
+	.long	0xe0500020	@ Characteristics (section flags)
diff --git a/arch/arm/boot/compressed/efi-stub.c b/arch/arm/boot/compressed/efi-stub.c
new file mode 100644
index 0000000..41fa1e2
--- /dev/null
+++ b/arch/arm/boot/compressed/efi-stub.c
@@ -0,0 +1,514 @@
+/*
+ * linux/arch/arm/boot/compressed/efi-stub.c
+ *
+ * Copyright (C) 2013 Linaro Ltd;  <roy.franz@linaro.org>
+ *
+ * This file implements the EFI boot stub for the ARM kernel
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+#include <linux/efi.h>
+#include <libfdt.h>
+
+
+/* Error code returned to ASM code instead of valid FDT address. */
+#define EFI_STUB_ERROR		(~0)
+
+/* EFI function call wrappers.  These are not required for
+ * ARM, but wrappers are required for X86 to convert between
+ * ABIs.  These wrappers are provided to allow code sharing
+ * between X86 and ARM.  Since these wrappers directly invoke the
+ * EFI function pointer, the function pointer type must be properly
+ * defined, which is not the case for X86  One advantage of this is
+ * it allows for type checking of arguments, which is not
+ * possible with the X86 wrappers.
+ */
+#define efi_call_phys0(f)			f()
+#define efi_call_phys1(f, a1)			f(a1)
+#define efi_call_phys2(f, a1, a2)		f(a1, a2)
+#define efi_call_phys3(f, a1, a2, a3)		f(a1, a2, a3)
+#define efi_call_phys4(f, a1, a2, a3, a4)	f(a1, a2, a3, a4)
+#define efi_call_phys5(f, a1, a2, a3, a4, a5)	f(a1, a2, a3, a4, a5)
+
+/* The maximum uncompressed kernel size is 32 MBytes, so we will reserve
+ * that for the decompressed kernel.  We have no easy way to tell what
+ * the actuall size of code + data the uncompressed kernel will use.
+ */
+#define MAX_UNCOMP_KERNEL_SIZE	0x02000000
+
+/* The kernel zImage should be located between 32 Mbytes
+ * and 128 MBytes from the base of DRAM.  The min
+ * address leaves space for a maximal size uncompressed image,
+ * and the max address is due to how the zImage decompressor
+ * picks a destination address.
+ */
+#define MAX_ZIMAGE_OFFSET	0x08000000
+#define MIN_ZIMAGE_OFFSET	MAX_UNCOMP_KERNEL_SIZE
+
+#define MAX_CMDLINE_LEN		500
+
+struct fdt_region {
+	u64 base;
+	u64 size;
+};
+
+/*
+ * Additional size that could be used for FDT entries added by
+ * the UEFI OS Loader Estimation based on:
+ * EDID (300bytes) + bootargs  + initrd region (20bytes)
+ * + system memory region (20bytes) + mp_core entries (200
+ * bytes)
+ */
+#define FDT_ADDITIONAL_ENTRIES_SIZE     (0x300 + MAX_CMDLINE_LEN)
+
+/* Include shared EFI stub code */
+#include "../../../../drivers/firmware/efi/efi-stub-helper.c"
+
+
+static int is_linux_reserved_region(int memory_type)
+{
+	switch (memory_type) {
+	case EfiRuntimeServicesCode:
+	case EfiRuntimeServicesData:
+	case EfiUnusableMemory:
+	case EfiACPIReclaimMemory:
+	case EfiACPIMemoryNVS:
+		return 1;
+	default:
+		return 0;
+	}
+}
+
+
+static int relocate_kernel(efi_system_table_t *sys_table,
+			   unsigned long *load_addr, unsigned long *load_size,
+			   unsigned long min_addr, unsigned long max_addr)
+{
+	/* Get current address of kernel. */
+	unsigned long cur_zimage_addr = *load_addr;
+	unsigned long zimage_size = *load_size;
+	unsigned long new_addr = 0;
+	unsigned long nr_pages;
+
+	efi_status_t status;
+
+	if (!load_addr || !load_size)
+		return EFI_INVALID_PARAMETER;
+
+	*load_size = 0;
+	if (cur_zimage_addr > min_addr
+	    && (cur_zimage_addr + zimage_size) < max_addr) {
+		/* We don't need to do anything, as kernel@an acceptable
+		 * address already.
+		 */
+		return EFI_SUCCESS;
+	}
+	/*
+	 * The EFI firmware loader could have placed the kernel image
+	 * anywhere in memory, but the kernel has restrictions on the
+	 * min and max physical address it can run at.
+	 */
+	nr_pages = round_up(zimage_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
+
+	status = efi_low_alloc(sys_table, zimage_size, 0,
+			   &new_addr, min_addr);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "Failed to alloc memory for kernel.\n");
+		return status;
+	}
+
+	if (new_addr > (max_addr - zimage_size)) {
+		efi_free(sys_table, zimage_size, new_addr);
+		efi_printk(sys_table, "Failed to alloc usable memory for kernel.\n");
+		return EFI_INVALID_PARAMETER;
+	}
+
+	/* We know source/dest won't overlap since both memory ranges
+	 * have been allocated by UEFI, so we can safely use memcpy.
+	 */
+	memcpy((void *)new_addr, (void *)(unsigned long)cur_zimage_addr,
+	       zimage_size);
+
+	/* Return the load address and size */
+	*load_addr = new_addr;
+	*load_size = zimage_size;
+
+
+	return status;
+}
+
+
+/* Convert the unicode UEFI command line to ASCII to pass to kernel.
+ * Size of memory allocated return in *cmd_line_len.
+ * Returns NULL on error.
+ */
+static char *convert_cmdline_to_ascii(efi_system_table_t *sys_table,
+				      efi_loaded_image_t *image,
+				      unsigned long *cmd_line_len,
+				      u32 max_addr)
+{
+	u16 *s2;
+	u8 *s1 = NULL;
+	unsigned long cmdline_addr = 0;
+	int load_options_size = image->load_options_size / 2; /* ASCII */
+	void *options = (u16 *)image->load_options;
+	int options_size = 0;
+	int status;
+	int i;
+	u16 zero = 0;
+
+	if (options) {
+		s2 = options;
+		while (*s2 && *s2 != '\n' && options_size < load_options_size) {
+			s2++;
+			options_size++;
+		}
+	}
+
+	if (options_size == 0) {
+		/* No command line options, so return empty string*/
+		options_size = 1;
+		options = &zero;
+	}
+
+	if (options_size > MAX_CMDLINE_LEN)
+		options_size = MAX_CMDLINE_LEN;
+
+	options_size++;  /* NUL termination */
+
+	status = efi_high_alloc(sys_table, options_size, 0,
+			    &cmdline_addr, max_addr);
+	if (status != EFI_SUCCESS)
+		return NULL;
+
+	s1 = (u8 *)(unsigned long)cmdline_addr;
+	s2 = (u16 *)options;
+
+	for (i = 0; i < options_size - 1; i++)
+		*s1++ = *s2++;
+
+	*s1 = '\0';
+
+	*cmd_line_len = options_size;
+	return (char *)(unsigned long)cmdline_addr;
+}
+
+static u32 update_fdt_and_exit_boot(efi_system_table_t *sys_table,
+				    void *handle, unsigned long dram_base,
+				    void *orig_fdt, u64 *orig_fdt_size,
+				    char *cmdline_ptr,
+				    unsigned long *cmdline_size,
+				    u64 initrd_addr, u64 initrd_size)
+{
+	unsigned long new_fdt_size;
+	unsigned long new_fdt_addr;
+	void *fdt;
+	int node;
+	int status;
+	int i;
+	unsigned long map_size, desc_size;
+	unsigned long mmap_key;
+	efi_memory_desc_t *memory_map;
+	unsigned long fdt_val;
+
+	new_fdt_size = *orig_fdt_size + FDT_ADDITIONAL_ENTRIES_SIZE;
+	status = efi_high_alloc(sys_table, new_fdt_size, 0, &new_fdt_addr,
+			    dram_base + MAX_ZIMAGE_OFFSET);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "ERROR: Unable to allocate memory for new device tree.\n");
+		goto fail;
+	}
+
+
+	fdt = (void *)new_fdt_addr;
+	status = fdt_open_into(orig_fdt, fdt, new_fdt_size);
+	if (status != 0) {
+		efi_printk(sys_table, "ERROR: Device Tree open_int failed.\n");
+		goto fail_free_new_fdt;
+	}
+	/* We are done with the original DTB, so free it. */
+	efi_free(sys_table, *orig_fdt_size, (u32)orig_fdt);
+	*orig_fdt_size = 0;
+
+	node = fdt_subnode_offset(fdt, 0, "chosen");
+	if (node < 0) {
+		node = fdt_add_subnode(fdt, 0, "chosen");
+		if (node < 0) {
+			efi_printk(sys_table, "Error on finding 'chosen' node\n");
+			goto fail_free_new_fdt;
+		}
+	}
+
+	if ((cmdline_ptr != NULL) && (strlen(cmdline_ptr) > 0)) {
+		status = fdt_setprop(fdt, node, "bootargs", cmdline_ptr,
+				     strlen(cmdline_ptr) + 1);
+		if (status) {
+			efi_printk(sys_table, "Failed to set new bootarg\n");
+			goto fail_free_new_fdt;
+		}
+	}
+	/* We are done with original command line, so free it. */
+	efi_free(sys_table, *cmdline_size, (u32)cmdline_ptr);
+	*cmdline_size = 0;
+
+	/* Set intird address/end in device tree, if present */
+	if (initrd_size != 0) {
+		u64 initrd_image_end;
+		u64 initrd_image_start = cpu_to_fdt64(initrd_addr);
+		status = fdt_setprop(fdt, node, "linux,initrd-start",
+				     &initrd_image_start, sizeof(u64));
+		if (status) {
+			efi_printk(sys_table, "Failed to set new 'linux,initrd-start'\n");
+			goto fail_free_new_fdt;
+		}
+		initrd_image_end = cpu_to_fdt64(initrd_addr + initrd_size);
+		status = fdt_setprop(fdt, node, "linux,initrd-end",
+				     &initrd_image_end, sizeof(u64));
+		if (status) {
+			efi_printk(sys_table, "Failed to set new 'linux,initrd-end'\n");
+			goto fail_free_new_fdt;
+		}
+	}
+
+	/* Update memory map in the device tree. The memory node must
+	 * be present in the tree.*/
+	node = fdt_subnode_offset(fdt, 0, "memory");
+	if (node < 0) {
+		efi_printk(sys_table, "ERROR: FDT memory node does not exist in DTB.\n");
+		goto fail_free_new_fdt;
+	}
+
+	status = efi_get_memory_map(sys_table, &memory_map, &map_size,
+				    &desc_size, &mmap_key);
+	if (status != EFI_SUCCESS)
+		goto fail_free_new_fdt;
+
+	for (i = 0; i < (map_size / sizeof(efi_memory_desc_t)); i++) {
+		efi_memory_desc_t *desc;
+		unsigned long m = (unsigned long)memory_map;
+		desc = (efi_memory_desc_t *)(m + (i * desc_size));
+
+		if (is_linux_reserved_region(desc->type)) {
+			status = fdt_add_mem_rsv(fdt, desc->phys_addr,
+						 desc->num_pages * EFI_PAGE_SIZE);
+			if (status != 0) {
+				efi_printk(sys_table, "ERROR: Failed to add 'memreserve' to fdt.\n");
+				goto fail_free_mmap;
+			}
+		}
+	}
+
+
+	/* Add FDT entries for EFI runtime services in chosen node.
+	 * We need to add the final memory map, so this is done at
+	 * the very end.
+	 */
+	node = fdt_subnode_offset(fdt, 0, "chosen");
+	fdt_val = cpu_to_fdt32((unsigned long)sys_table);
+	status = fdt_setprop(fdt, node, "efi-system-table",
+			     &fdt_val, sizeof(fdt_val));
+	if (status) {
+		efi_printk(sys_table, "Failed to set new 'efi-system-table'\n");
+		goto fail_free_new_fdt;
+	}
+	fdt_val = cpu_to_fdt32(desc_size);
+	status = fdt_setprop(fdt, node, "efi-mmap-desc-size",
+			     &fdt_val, sizeof(fdt_val));
+	if (status) {
+		efi_printk(sys_table, "Failed to set new 'efi-mmap-desc-size'\n");
+		goto fail_free_new_fdt;
+	}
+	fdt_val = cpu_to_fdt32(map_size);
+	status = fdt_setprop(fdt, node, "efi-runtime-mmap-size",
+			     &fdt_val, sizeof(fdt_val));
+	if (status) {
+		efi_printk(sys_table, "Failed to set new 'efi-runtime-mmap-size'\n");
+		goto fail_free_new_fdt;
+	}
+	fdt_val = cpu_to_fdt32((unsigned long)memory_map);
+	status = fdt_setprop(fdt, node, "efi-runtime-mmap",
+			     &fdt_val, sizeof(fdt_val));
+	if (status) {
+		efi_printk(sys_table, "Failed to set new 'efi-runtime-mmap'\n");
+		goto fail_free_new_fdt;
+	}
+
+	/* Now we need to exit boot services.  We need the key from
+	 * the most recent read of the memory map to do this.  We can't
+	 * free this buffer in the normal case, but do free it when
+	 * exit_boot_services() fails or adding the memory map to the FDT
+	 * fails.
+	 */
+	status = efi_call_phys2(sys_table->boottime->exit_boot_services,
+				handle, mmap_key);
+
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "exit boot services failed.\n");
+		goto fail_free_mmap;
+	}
+
+	return new_fdt_addr;
+
+fail_free_mmap:
+	efi_call_phys1(sys_table->boottime->free_pool, memory_map);
+
+fail_free_new_fdt:
+	efi_free(sys_table, new_fdt_size, new_fdt_addr);
+
+fail:
+	return 0;
+}
+
+
+int efi_entry(void *handle, efi_system_table_t *sys_table,
+	      unsigned long *zimage_addr)
+{
+	efi_loaded_image_t *image;
+	int status;
+	unsigned long nr_pages;
+	const struct fdt_region *region;
+
+	void *fdt;
+	int err;
+	int node;
+	unsigned long zimage_size = 0;
+	unsigned long dram_base;
+	/* addr/point and size pairs for memory management*/
+	u64 initrd_addr;
+	u64 initrd_size = 0;
+	u64 fdt_addr;  /* Original DTB */
+	u64 fdt_size = 0;
+	u64 kernel_reserve_addr;
+	u64 kernel_reserve_size = 0;
+	char *cmdline_ptr;
+	unsigned long cmdline_size = 0;
+	unsigned long new_fdt_addr;
+
+	efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
+
+	/* Check if we were booted by the EFI firmware */
+	if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
+		goto fail;
+
+	efi_printk(sys_table, "Booting Linux using EFI stub.\n");
+
+
+	/* get the command line from EFI, using the LOADED_IMAGE protocol */
+	status = efi_call_phys3(sys_table->boottime->handle_protocol,
+				handle, &proto, (void *)&image);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
+		goto fail;
+	}
+
+	/* We are going to copy this into device tree, so we don't care where in
+	 * memory it is.
+	 */
+	cmdline_ptr = convert_cmdline_to_ascii(sys_table, image,
+					       &cmdline_size, 0xFFFFFFFF);
+	if (!cmdline_ptr) {
+		efi_printk(sys_table, "ERROR converting command line to ascii.\n");
+		goto fail;
+	}
+
+	/* We first load the device tree, as we need to get the base address of
+	 * DRAM from the device tree.  The zImage, device tree, and initrd
+	 * have address restrictions that are relative to the base of DRAM.
+	 */
+	status = handle_cmdline_files(sys_table, image, cmdline_ptr, "dtb=",
+				      0xffffffff, &fdt_addr, &fdt_size);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "Error loading dtb blob\n");
+		goto fail_free_cmdline;
+	}
+
+	err = fdt_check_header((void *)(unsigned long)fdt_addr);
+	if (err != 0) {
+		efi_printk(sys_table, "ERROR: Device Tree header not valid\n");
+		goto fail_free_dtb;
+	}
+	if (fdt_totalsize((void *)(unsigned long)fdt_addr) > fdt_size) {
+		efi_printk(sys_table, "ERROR: Incomplete device tree.\n");
+		goto fail_free_dtb;
+
+	}
+
+
+	/* Look up the base of DRAM from the device tree.*/
+	fdt = (void *)(u32)fdt_addr;
+	node = fdt_subnode_offset(fdt, 0, "memory");
+	region = fdt_getprop(fdt, node, "reg", NULL);
+	if (region) {
+		dram_base = fdt64_to_cpu(region->base);
+	} else {
+		efi_printk(sys_table, "Error: no 'memory' node in device tree.\n");
+		goto fail_free_dtb;
+	}
+
+	/* Reserve memory for the uncompressed kernel image. */
+	kernel_reserve_addr = dram_base;
+	kernel_reserve_size = MAX_UNCOMP_KERNEL_SIZE;
+	nr_pages = round_up(kernel_reserve_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
+	status = efi_call_phys4(sys_table->boottime->allocate_pages,
+				EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
+				nr_pages, &kernel_reserve_addr);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "ERROR allocating memory for uncompressed kernel.\n");
+		goto fail_free_dtb;
+	}
+
+	/* Relocate the zImage, if required. */
+	zimage_size = image->image_size;
+	status = relocate_kernel(sys_table, zimage_addr, &zimage_size,
+				 dram_base + MIN_ZIMAGE_OFFSET,
+				 dram_base + MAX_ZIMAGE_OFFSET);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "Failed to relocate kernel\n");
+		goto fail_free_kernel_reserve;
+	}
+
+	status = handle_cmdline_files(sys_table, image, cmdline_ptr, "initrd=",
+				      dram_base + MAX_ZIMAGE_OFFSET,
+				      &initrd_addr, &initrd_size);
+	if (status != EFI_SUCCESS) {
+		efi_printk(sys_table, "Error loading initrd\n");
+		goto fail_free_zimage;
+	}
+
+	new_fdt_addr = update_fdt_and_exit_boot(sys_table, handle,
+						dram_base, fdt, &fdt_size,
+						cmdline_ptr, &cmdline_size,
+						initrd_addr, initrd_size);
+
+	if (new_fdt_addr == 0) {
+		efi_printk(sys_table, "Error updating device tree and exiting boot services.\n");
+		goto fail_free_initrd;
+	}
+
+
+	/* Now we need to return the FDT address to the calling
+	 * assembly to this can be used as part of normal boot.
+	 */
+	return new_fdt_addr;
+
+fail_free_initrd:
+	efi_free(sys_table, initrd_size, initrd_addr);
+
+fail_free_zimage:
+	efi_free(sys_table, zimage_size, *zimage_addr);
+
+fail_free_kernel_reserve:
+	efi_free(sys_table, kernel_reserve_addr, kernel_reserve_size);
+
+fail_free_dtb:
+	efi_free(sys_table, fdt_size, fdt_addr);
+
+fail_free_cmdline:
+	efi_free(sys_table, cmdline_size, (u32)cmdline_ptr);
+
+fail:
+	return EFI_STUB_ERROR;
+}
diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
index 75189f1..4c70b9e 100644
--- a/arch/arm/boot/compressed/head.S
+++ b/arch/arm/boot/compressed/head.S
@@ -122,19 +122,106 @@
 		.arm				@ Always enter in ARM state
 start:
 		.type	start,#function
-		.rept	7
+#ifdef CONFIG_EFI_STUB
+		@ Magic MSDOS signature for PE/COFF + ADD opcode
+		.word	0x62805a4d
+#else
+		mov	r0, r0
+#endif
+		.rept	5
 		mov	r0, r0
 		.endr
    ARM(		mov	r0, r0		)
    ARM(		b	1f		)
  THUMB(		adr	r12, BSYM(1f)	)
  THUMB(		bx	r12		)
+ THUMB(		.thumb			)
+1:
+		b	zimage_continue
 
 		.word	0x016f2818		@ Magic numbers to help the loader
 		.word	start			@ absolute load/run zImage address
 		.word	_edata			@ zImage end address
+
+#ifdef CONFIG_EFI_STUB
+		@ Portions of the MSDOS file header must be at offset
+		@ 0x3c from the start of the file.  All PE/COFF headers
+		@ are kept contiguous for simplicity.
+#include "efi-header.S"
+
+efi_stub_entry:
+		.text
+		@ The EFI stub entry point is not at a fixed address, however
+		@ this address must be set in the PE/COFF header.
+		@ EFI entry point is in A32 mode, switch to T32 if configured.
+		.arm
+   ARM(		mov	r0, r0		)
+   ARM(		b	1f		)
+ THUMB(		adr	r12, BSYM(1f)	)
+ THUMB(		bx	r12		)
  THUMB(		.thumb			)
 1:
+		@ Save lr on stack for possible return to EFI firmware.
+		@ Don't care about fp, but need 64 bit alignment....
+		stmfd	sp!, {fp, lr}
+
+		@ Save args to EFI app across got fixup call
+		stmfd	sp!, {r0, r1}
+		ldmfd	sp!, {r0, r1}
+
+		@ allocate space on stack for return of new entry point of
+		@ zImage, as EFI stub may copy the kernel.  Pass address
+		@ of space in r2 - EFI stub will fill in the pointer.
+
+		sub	sp, #8			@ we only need 4 bytes,
+						@ but keep stack 8 byte aligned.
+		mov	r2, sp
+		@ Pass our actual runtime start address in pointer data
+		adr	r11, LC0		@ address of LC0 at run time
+		ldr	r12, [r11, #0]		@ address of LC0 at link time
+
+		sub	r3, r11, r12		@ calculate the delta offset
+		str	r3, [r2, #0]
+		bl	efi_entry
+
+		@ get new zImage entry address from stack, put into r3
+		ldr	r3, [sp, #0]
+		add	sp, #8  @ restore stack
+
+		@ Check for error return from EFI stub (0xFFFFFFFF)
+		ldr	r1, =0xffffffff
+		cmp	r0, r1
+		beq	efi_load_fail
+
+
+		@ Save return values of efi_entry
+		stmfd	sp!, {r0, r3}
+		bl	cache_clean_flush
+		bl	cache_off
+		ldmfd   sp!, {r0, r3}
+
+		@ put DTB address in r2, it was returned by EFI entry
+		mov	r2, r0
+		ldr	r1, =0xffffffff		@ DTB machine type
+		mov	r0, #0  @ r0 is 0
+
+		@ Branch to (possibly) relocated zImage entry that is in r3
+		bx	r3
+
+efi_load_fail:
+	@ We need to exit THUMB mode here, to return to EFI firmware.
+ THUMB(		adr	r12, BSYM(1f)	)
+ THUMB(		bx	r12		)
+1:
+		.arm
+		@ Return EFI_LOAD_ERROR to EFI firmware on error.
+		ldr	r0, =0x80000001
+		ldmfd	sp!, {fp, lr}
+		mov	pc, lr
+ THUMB(		.thumb			)
+#endif
+
+zimage_continue:
 		mrs	r9, cpsr
 #ifdef CONFIG_ARM_VIRT_EXT
 		bl	__hyp_stub_install	@ get into SVC mode, reversibly
@@ -167,7 +254,6 @@ not_angel:
 		 * by the linker here, but it should preserve r7, r8, and r9.
 		 */
 
-		.text
 
 #ifdef CONFIG_AUTO_ZRELADDR
 		@ determine final kernel image address
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH 7/7] Add config EFI_STUB for ARM
  2013-08-02 21:29 ` Roy Franz
@ 2013-08-02 21:29   ` Roy Franz
  -1 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-02 21:29 UTC (permalink / raw)
  To: linux-kernel, linux-efi, linux-arm-kernel, matt.fleming, linux
  Cc: leif.lindholm, Roy Franz

Signed-off-by: Roy Franz <roy.franz@linaro.org>
---
 arch/arm/Kconfig |   10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
index 37c0f4e..192968c 100644
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -1805,6 +1805,16 @@ config UACCESS_WITH_MEMCPY
 	  However, if the CPU data cache is using a write-allocate mode,
 	  this option is unlikely to provide any performance gain.
 
+config EFI_STUB
+	bool "EFI stub support"
+	---help---
+	  This kernel feature allows a zImage to be loaded directly
+	  by EFI firmware without the use of a bootloader.  A PE/COFF
+	  header is added to the zImage in a way that makes the binary
+	  both a Linux zImage and an PE/COFF executable that can be
+	  executed directly by EFI firmware.
+	  See Documentation/efi-stub.txt for more information.
+
 config SECCOMP
 	bool
 	prompt "Enable seccomp to safely compute untrusted bytecode"
-- 
1.7.10.4


^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH 7/7] Add config EFI_STUB for ARM
@ 2013-08-02 21:29   ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-02 21:29 UTC (permalink / raw)
  To: linux-arm-kernel

Signed-off-by: Roy Franz <roy.franz@linaro.org>
---
 arch/arm/Kconfig |   10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
index 37c0f4e..192968c 100644
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -1805,6 +1805,16 @@ config UACCESS_WITH_MEMCPY
 	  However, if the CPU data cache is using a write-allocate mode,
 	  this option is unlikely to provide any performance gain.
 
+config EFI_STUB
+	bool "EFI stub support"
+	---help---
+	  This kernel feature allows a zImage to be loaded directly
+	  by EFI firmware without the use of a bootloader.  A PE/COFF
+	  header is added to the zImage in a way that makes the binary
+	  both a Linux zImage and an PE/COFF executable that can be
+	  executed directly by EFI firmware.
+	  See Documentation/efi-stub.txt for more information.
+
 config SECCOMP
 	bool
 	prompt "Enable seccomp to safely compute untrusted bytecode"
-- 
1.7.10.4

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* Re: [PATCH 6/7] Add EFI stub for ARM
  2013-08-02 21:29   ` Roy Franz
@ 2013-08-05 14:11     ` Dave Martin
  -1 siblings, 0 replies; 54+ messages in thread
From: Dave Martin @ 2013-08-05 14:11 UTC (permalink / raw)
  To: Roy Franz
  Cc: linux-kernel, linux-efi, linux-arm-kernel, matt.fleming, linux,
	leif.lindholm

On Fri, Aug 02, 2013 at 02:29:07PM -0700, Roy Franz wrote:
> This patch adds EFI stub support for the ARM Linux kernel.  The EFI stub
> operations similarly to the x86 stub: it is a shim between the EFI firmware
> and the normal zImage entry point, and sets up the environment that the
> zImage is expecting.  This includes loading the initrd (optionaly) and
> device tree from the system partition based on the kernel command line.
> The stub updates the device tree as necessary, including adding reserved
> memory regions and adding entries for EFI runtime services. The PE/COFF
> "MZ" header at offset 0 results in the first instruction being an add
> that corrupts r5, which is not used by the zImage interface.
> 
> Signed-off-by: Roy Franz <roy.franz@linaro.org>
> ---
>  arch/arm/boot/compressed/Makefile     |   18 +-
>  arch/arm/boot/compressed/efi-header.S |  114 ++++++++
>  arch/arm/boot/compressed/efi-stub.c   |  514 +++++++++++++++++++++++++++++++++
>  arch/arm/boot/compressed/head.S       |   90 +++++-
>  4 files changed, 732 insertions(+), 4 deletions(-)
>  create mode 100644 arch/arm/boot/compressed/efi-header.S
>  create mode 100644 arch/arm/boot/compressed/efi-stub.c
> 
> diff --git a/arch/arm/boot/compressed/Makefile b/arch/arm/boot/compressed/Makefile
> index 7ac1610..c62826a 100644
> --- a/arch/arm/boot/compressed/Makefile
> +++ b/arch/arm/boot/compressed/Makefile
> @@ -106,8 +106,22 @@ $(addprefix $(obj)/,$(libfdt) $(libfdt_hdrs)): $(obj)/%: $(srctree)/scripts/dtc/
>  $(addprefix $(obj)/,$(libfdt_objs) atags_to_fdt.o): \
>  	$(addprefix $(obj)/,$(libfdt_hdrs))
>  
> +$(addprefix $(obj)/,$(libfdt_objs) efi-stub.o): \
> +	$(addprefix $(obj)/,$(libfdt_hdrs))
> +
>  ifeq ($(CONFIG_ARM_ATAG_DTB_COMPAT),y)
> -OBJS	+= $(libfdt_objs) atags_to_fdt.o
> +OBJS	+= atags_to_fdt.o
> +USE_LIBFDT = y
> +endif
> +
> +ifeq ($(CONFIG_EFI_STUB),y)
> +CFLAGS_efi-stub.o += -DTEXT_OFFSET=$(TEXT_OFFSET)
> +OBJS	+= efi-stub.o
> +USE_LIBFDT = y
> +endif
> +
> +ifeq ($(USE_LIBFDT),y)
> +OBJS	+= $(libfdt_objs)
>  endif
>  
>  targets       := vmlinux vmlinux.lds \
> @@ -125,7 +139,7 @@ ORIG_CFLAGS := $(KBUILD_CFLAGS)
>  KBUILD_CFLAGS = $(subst -pg, , $(ORIG_CFLAGS))
>  endif
>  
> -ccflags-y := -fpic -mno-single-pic-base -fno-builtin -I$(obj)
> +ccflags-y := -fpic -mno-single-pic-base -fno-builtin -I$(obj) -fno-stack-protector
>  asflags-y := -DZIMAGE
>  
>  # Supply kernel BSS size to the decompressor via a linker symbol.
> diff --git a/arch/arm/boot/compressed/efi-header.S b/arch/arm/boot/compressed/efi-header.S
> new file mode 100644
> index 0000000..6ff32cc
> --- /dev/null
> +++ b/arch/arm/boot/compressed/efi-header.S
> @@ -0,0 +1,114 @@
> +@ Copyright (C) 2013 Linaro Ltd;  <roy.franz@linaro.org>
> +@
> +@ This file contains the PE/COFF header that is part of the
> +@ EFI stub.
> +@
> +
> +	.org	0x3c
> +	@
> +	@ The PE header can be anywhere in the file, but for
> +	@ simplicity we keep it together with the MSDOS header
> +	@ The offset to the PE/COFF header needs to be at offset
> +	@ 0x3C in the MSDOS header.
> +	@ The only 2 fields of the MSDOS header that are used are this
> +	@ PE/COFF offset, and the "MZ" bytes at offset 0x0.
> +	@
> +	.long	pe_header			@ Offset to the PE header.
> +
> +      .align 3
> +pe_header:
> +
> +
> +pe_header:
> +	.ascii	"PE"
> +	.short 	0
> +
> +coff_header:
> +	.short	0x01c2				@ ARM or Thumb
> +	.short	2				@ nr_sections
> +	.long	0 				@ TimeDateStamp
> +	.long	0				@ PointerToSymbolTable
> +	.long	1				@ NumberOfSymbols
> +	.short	section_table - optional_header	@ SizeOfOptionalHeader
> +	.short	0x306				@ Characteristics.
> +						@ IMAGE_FILE_32BIT_MACHINE |
> +						@ IMAGE_FILE_DEBUG_STRIPPED |
> +						@ IMAGE_FILE_EXECUTABLE_IMAGE |
> +						@ IMAGE_FILE_LINE_NUMS_STRIPPED
> +
> +optional_header:
> +	.short	0x10b				@ PE32 format
> +	.byte	0x02				@ MajorLinkerVersion
> +	.byte	0x14				@ MinorLinkerVersion
> +
> +	.long	0				@ SizeOfCode
> +
> +	.long	0				@ SizeOfInitializedData
> +	.long	0				@ SizeOfUninitializedData
> +
> +	.long	efi_stub_entry			@ AddressOfEntryPoint
> +	.long	efi_stub_entry			@ BaseOfCode
> +	.long	0				@ data
> +
> +extra_header_fields:
> +	.long	0				@ ImageBase
> +	.long	0x20				@ SectionAlignment
> +	.long	0x20				@ FileAlignment
> +	.short	0				@ MajorOperatingSystemVersion
> +	.short	0				@ MinorOperatingSystemVersion
> +	.short	0				@ MajorImageVersion
> +	.short	0				@ MinorImageVersion
> +	.short	0				@ MajorSubsystemVersion
> +	.short	0				@ MinorSubsystemVersion
> +	.long	0				@ Win32VersionValue
> +
> +	.long	_edata				@ SizeOfImage
> +
> +	@ Everything before the entry point is considered part of the header
> +	.long	efi_stub_entry			@ SizeOfHeaders
> +	.long	0				@ CheckSum
> +	.short	0xa				@ Subsystem (EFI application)
> +	.short	0				@ DllCharacteristics
> +	.long	0				@ SizeOfStackReserve
> +	.long	0				@ SizeOfStackCommit
> +	.long	0				@ SizeOfHeapReserve
> +	.long	0				@ SizeOfHeapCommit
> +	.long	0				@ LoaderFlags
> +	.long	0x0				@ NumberOfRvaAndSizes
> +
> +	# Section table
> +section_table:
> +
> +	#
> +	# The EFI application loader requires a relocation section
> +	# because EFI applications must be relocatable.  This is a
> +	# dummy section as far as we are concerned.
> +	#
> +	.ascii	".reloc"
> +	.byte	0
> +	.byte	0			@ end of 0 padding of section name
> +	.long	0
> +	.long	0
> +	.long	0			@ SizeOfRawData
> +	.long	0			@ PointerToRawData
> +	.long	0			@ PointerToRelocations
> +	.long	0			@ PointerToLineNumbers
> +	.short	0			@ NumberOfRelocations
> +	.short	0			@ NumberOfLineNumbers
> +	.long	0x42100040		@ Characteristics (section flags)
> +
> +
> +	.ascii	".text"
> +	.byte	0
> +	.byte	0
> +	.byte	0        		@ end of 0 padding of section name
> +	.long	_edata - efi_stub_entry		@ VirtualSize
> +	.long	efi_stub_entry			@ VirtualAddress
> +	.long	_edata - efi_stub_entry		@ SizeOfRawData
> +	.long	efi_stub_entry			@ PointerToRawData
> +
> +	.long	0		@ PointerToRelocations (0 for executables)
> +	.long	0		@ PointerToLineNumbers (0 for executables)
> +	.short	0		@ NumberOfRelocations  (0 for executables)
> +	.short	0		@ NumberOfLineNumbers  (0 for executables)
> +	.long	0xe0500020	@ Characteristics (section flags)
> diff --git a/arch/arm/boot/compressed/efi-stub.c b/arch/arm/boot/compressed/efi-stub.c
> new file mode 100644
> index 0000000..41fa1e2
> --- /dev/null
> +++ b/arch/arm/boot/compressed/efi-stub.c
> @@ -0,0 +1,514 @@
> +/*
> + * linux/arch/arm/boot/compressed/efi-stub.c
> + *
> + * Copyright (C) 2013 Linaro Ltd;  <roy.franz@linaro.org>
> + *
> + * This file implements the EFI boot stub for the ARM kernel
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License version 2 as
> + * published by the Free Software Foundation.
> + *
> + */
> +#include <linux/efi.h>
> +#include <libfdt.h>
> +
> +
> +/* Error code returned to ASM code instead of valid FDT address. */
> +#define EFI_STUB_ERROR		(~0)
> +
> +/* EFI function call wrappers.  These are not required for
> + * ARM, but wrappers are required for X86 to convert between
> + * ABIs.  These wrappers are provided to allow code sharing
> + * between X86 and ARM.  Since these wrappers directly invoke the
> + * EFI function pointer, the function pointer type must be properly
> + * defined, which is not the case for X86  One advantage of this is
> + * it allows for type checking of arguments, which is not
> + * possible with the X86 wrappers.
> + */
> +#define efi_call_phys0(f)			f()
> +#define efi_call_phys1(f, a1)			f(a1)
> +#define efi_call_phys2(f, a1, a2)		f(a1, a2)
> +#define efi_call_phys3(f, a1, a2, a3)		f(a1, a2, a3)
> +#define efi_call_phys4(f, a1, a2, a3, a4)	f(a1, a2, a3, a4)
> +#define efi_call_phys5(f, a1, a2, a3, a4, a5)	f(a1, a2, a3, a4, a5)
> +
> +/* The maximum uncompressed kernel size is 32 MBytes, so we will reserve
> + * that for the decompressed kernel.  We have no easy way to tell what
> + * the actuall size of code + data the uncompressed kernel will use.
> + */
> +#define MAX_UNCOMP_KERNEL_SIZE	0x02000000
> +
> +/* The kernel zImage should be located between 32 Mbytes
> + * and 128 MBytes from the base of DRAM.  The min
> + * address leaves space for a maximal size uncompressed image,
> + * and the max address is due to how the zImage decompressor
> + * picks a destination address.
> + */
> +#define MAX_ZIMAGE_OFFSET	0x08000000
> +#define MIN_ZIMAGE_OFFSET	MAX_UNCOMP_KERNEL_SIZE
> +
> +#define MAX_CMDLINE_LEN		500
> +
> +struct fdt_region {
> +	u64 base;
> +	u64 size;
> +};
> +
> +/*
> + * Additional size that could be used for FDT entries added by
> + * the UEFI OS Loader Estimation based on:
> + * EDID (300bytes) + bootargs  + initrd region (20bytes)
> + * + system memory region (20bytes) + mp_core entries (200
> + * bytes)
> + */
> +#define FDT_ADDITIONAL_ENTRIES_SIZE     (0x300 + MAX_CMDLINE_LEN)
> +
> +/* Include shared EFI stub code */
> +#include "../../../../drivers/firmware/efi/efi-stub-helper.c"
> +
> +
> +static int is_linux_reserved_region(int memory_type)
> +{
> +	switch (memory_type) {
> +	case EfiRuntimeServicesCode:
> +	case EfiRuntimeServicesData:
> +	case EfiUnusableMemory:
> +	case EfiACPIReclaimMemory:
> +	case EfiACPIMemoryNVS:
> +		return 1;
> +	default:
> +		return 0;
> +	}
> +}
> +
> +
> +static int relocate_kernel(efi_system_table_t *sys_table,
> +			   unsigned long *load_addr, unsigned long *load_size,
> +			   unsigned long min_addr, unsigned long max_addr)
> +{
> +	/* Get current address of kernel. */
> +	unsigned long cur_zimage_addr = *load_addr;
> +	unsigned long zimage_size = *load_size;
> +	unsigned long new_addr = 0;
> +	unsigned long nr_pages;
> +
> +	efi_status_t status;
> +
> +	if (!load_addr || !load_size)
> +		return EFI_INVALID_PARAMETER;
> +
> +	*load_size = 0;
> +	if (cur_zimage_addr > min_addr
> +	    && (cur_zimage_addr + zimage_size) < max_addr) {
> +		/* We don't need to do anything, as kernel at an acceptable
> +		 * address already.
> +		 */
> +		return EFI_SUCCESS;
> +	}
> +	/*
> +	 * The EFI firmware loader could have placed the kernel image
> +	 * anywhere in memory, but the kernel has restrictions on the
> +	 * min and max physical address it can run at.
> +	 */
> +	nr_pages = round_up(zimage_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
> +
> +	status = efi_low_alloc(sys_table, zimage_size, 0,
> +			   &new_addr, min_addr);
> +	if (status != EFI_SUCCESS) {
> +		efi_printk(sys_table, "Failed to alloc memory for kernel.\n");
> +		return status;
> +	}
> +
> +	if (new_addr > (max_addr - zimage_size)) {
> +		efi_free(sys_table, zimage_size, new_addr);
> +		efi_printk(sys_table, "Failed to alloc usable memory for kernel.\n");
> +		return EFI_INVALID_PARAMETER;
> +	}
> +
> +	/* We know source/dest won't overlap since both memory ranges
> +	 * have been allocated by UEFI, so we can safely use memcpy.
> +	 */
> +	memcpy((void *)new_addr, (void *)(unsigned long)cur_zimage_addr,
> +	       zimage_size);
> +
> +	/* Return the load address and size */
> +	*load_addr = new_addr;
> +	*load_size = zimage_size;
> +
> +
> +	return status;
> +}
> +
> +
> +/* Convert the unicode UEFI command line to ASCII to pass to kernel.
> + * Size of memory allocated return in *cmd_line_len.
> + * Returns NULL on error.
> + */
> +static char *convert_cmdline_to_ascii(efi_system_table_t *sys_table,
> +				      efi_loaded_image_t *image,
> +				      unsigned long *cmd_line_len,
> +				      u32 max_addr)
> +{
> +	u16 *s2;
> +	u8 *s1 = NULL;
> +	unsigned long cmdline_addr = 0;
> +	int load_options_size = image->load_options_size / 2; /* ASCII */
> +	void *options = (u16 *)image->load_options;
> +	int options_size = 0;
> +	int status;
> +	int i;
> +	u16 zero = 0;
> +
> +	if (options) {
> +		s2 = options;
> +		while (*s2 && *s2 != '\n' && options_size < load_options_size) {
> +			s2++;
> +			options_size++;
> +		}
> +	}
> +
> +	if (options_size == 0) {
> +		/* No command line options, so return empty string*/
> +		options_size = 1;
> +		options = &zero;
> +	}
> +
> +	if (options_size > MAX_CMDLINE_LEN)
> +		options_size = MAX_CMDLINE_LEN;
> +
> +	options_size++;  /* NUL termination */
> +
> +	status = efi_high_alloc(sys_table, options_size, 0,
> +			    &cmdline_addr, max_addr);
> +	if (status != EFI_SUCCESS)
> +		return NULL;
> +
> +	s1 = (u8 *)(unsigned long)cmdline_addr;
> +	s2 = (u16 *)options;
> +
> +	for (i = 0; i < options_size - 1; i++)
> +		*s1++ = *s2++;
> +
> +	*s1 = '\0';
> +
> +	*cmd_line_len = options_size;
> +	return (char *)(unsigned long)cmdline_addr;
> +}
> +
> +static u32 update_fdt_and_exit_boot(efi_system_table_t *sys_table,
> +				    void *handle, unsigned long dram_base,
> +				    void *orig_fdt, u64 *orig_fdt_size,
> +				    char *cmdline_ptr,
> +				    unsigned long *cmdline_size,
> +				    u64 initrd_addr, u64 initrd_size)
> +{
> +	unsigned long new_fdt_size;
> +	unsigned long new_fdt_addr;
> +	void *fdt;
> +	int node;
> +	int status;
> +	int i;
> +	unsigned long map_size, desc_size;
> +	unsigned long mmap_key;
> +	efi_memory_desc_t *memory_map;
> +	unsigned long fdt_val;
> +
> +	new_fdt_size = *orig_fdt_size + FDT_ADDITIONAL_ENTRIES_SIZE;
> +	status = efi_high_alloc(sys_table, new_fdt_size, 0, &new_fdt_addr,
> +			    dram_base + MAX_ZIMAGE_OFFSET);
> +	if (status != EFI_SUCCESS) {
> +		efi_printk(sys_table, "ERROR: Unable to allocate memory for new device tree.\n");
> +		goto fail;
> +	}
> +
> +
> +	fdt = (void *)new_fdt_addr;
> +	status = fdt_open_into(orig_fdt, fdt, new_fdt_size);
> +	if (status != 0) {
> +		efi_printk(sys_table, "ERROR: Device Tree open_int failed.\n");
> +		goto fail_free_new_fdt;
> +	}
> +	/* We are done with the original DTB, so free it. */
> +	efi_free(sys_table, *orig_fdt_size, (u32)orig_fdt);
> +	*orig_fdt_size = 0;
> +
> +	node = fdt_subnode_offset(fdt, 0, "chosen");
> +	if (node < 0) {
> +		node = fdt_add_subnode(fdt, 0, "chosen");
> +		if (node < 0) {
> +			efi_printk(sys_table, "Error on finding 'chosen' node\n");
> +			goto fail_free_new_fdt;
> +		}
> +	}
> +
> +	if ((cmdline_ptr != NULL) && (strlen(cmdline_ptr) > 0)) {
> +		status = fdt_setprop(fdt, node, "bootargs", cmdline_ptr,
> +				     strlen(cmdline_ptr) + 1);
> +		if (status) {
> +			efi_printk(sys_table, "Failed to set new bootarg\n");
> +			goto fail_free_new_fdt;
> +		}
> +	}
> +	/* We are done with original command line, so free it. */
> +	efi_free(sys_table, *cmdline_size, (u32)cmdline_ptr);
> +	*cmdline_size = 0;
> +
> +	/* Set intird address/end in device tree, if present */
> +	if (initrd_size != 0) {
> +		u64 initrd_image_end;
> +		u64 initrd_image_start = cpu_to_fdt64(initrd_addr);
> +		status = fdt_setprop(fdt, node, "linux,initrd-start",
> +				     &initrd_image_start, sizeof(u64));
> +		if (status) {
> +			efi_printk(sys_table, "Failed to set new 'linux,initrd-start'\n");
> +			goto fail_free_new_fdt;
> +		}
> +		initrd_image_end = cpu_to_fdt64(initrd_addr + initrd_size);
> +		status = fdt_setprop(fdt, node, "linux,initrd-end",
> +				     &initrd_image_end, sizeof(u64));
> +		if (status) {
> +			efi_printk(sys_table, "Failed to set new 'linux,initrd-end'\n");
> +			goto fail_free_new_fdt;
> +		}
> +	}
> +
> +	/* Update memory map in the device tree. The memory node must
> +	 * be present in the tree.*/
> +	node = fdt_subnode_offset(fdt, 0, "memory");
> +	if (node < 0) {
> +		efi_printk(sys_table, "ERROR: FDT memory node does not exist in DTB.\n");
> +		goto fail_free_new_fdt;
> +	}
> +
> +	status = efi_get_memory_map(sys_table, &memory_map, &map_size,
> +				    &desc_size, &mmap_key);
> +	if (status != EFI_SUCCESS)
> +		goto fail_free_new_fdt;
> +
> +	for (i = 0; i < (map_size / sizeof(efi_memory_desc_t)); i++) {
> +		efi_memory_desc_t *desc;
> +		unsigned long m = (unsigned long)memory_map;
> +		desc = (efi_memory_desc_t *)(m + (i * desc_size));
> +
> +		if (is_linux_reserved_region(desc->type)) {
> +			status = fdt_add_mem_rsv(fdt, desc->phys_addr,
> +						 desc->num_pages * EFI_PAGE_SIZE);
> +			if (status != 0) {
> +				efi_printk(sys_table, "ERROR: Failed to add 'memreserve' to fdt.\n");
> +				goto fail_free_mmap;
> +			}
> +		}
> +	}
> +
> +
> +	/* Add FDT entries for EFI runtime services in chosen node.
> +	 * We need to add the final memory map, so this is done at
> +	 * the very end.
> +	 */
> +	node = fdt_subnode_offset(fdt, 0, "chosen");
> +	fdt_val = cpu_to_fdt32((unsigned long)sys_table);
> +	status = fdt_setprop(fdt, node, "efi-system-table",
> +			     &fdt_val, sizeof(fdt_val));
> +	if (status) {
> +		efi_printk(sys_table, "Failed to set new 'efi-system-table'\n");
> +		goto fail_free_new_fdt;
> +	}
> +	fdt_val = cpu_to_fdt32(desc_size);
> +	status = fdt_setprop(fdt, node, "efi-mmap-desc-size",
> +			     &fdt_val, sizeof(fdt_val));
> +	if (status) {
> +		efi_printk(sys_table, "Failed to set new 'efi-mmap-desc-size'\n");
> +		goto fail_free_new_fdt;
> +	}
> +	fdt_val = cpu_to_fdt32(map_size);
> +	status = fdt_setprop(fdt, node, "efi-runtime-mmap-size",
> +			     &fdt_val, sizeof(fdt_val));
> +	if (status) {
> +		efi_printk(sys_table, "Failed to set new 'efi-runtime-mmap-size'\n");
> +		goto fail_free_new_fdt;
> +	}
> +	fdt_val = cpu_to_fdt32((unsigned long)memory_map);
> +	status = fdt_setprop(fdt, node, "efi-runtime-mmap",
> +			     &fdt_val, sizeof(fdt_val));
> +	if (status) {
> +		efi_printk(sys_table, "Failed to set new 'efi-runtime-mmap'\n");
> +		goto fail_free_new_fdt;
> +	}
> +
> +	/* Now we need to exit boot services.  We need the key from
> +	 * the most recent read of the memory map to do this.  We can't
> +	 * free this buffer in the normal case, but do free it when
> +	 * exit_boot_services() fails or adding the memory map to the FDT
> +	 * fails.
> +	 */
> +	status = efi_call_phys2(sys_table->boottime->exit_boot_services,
> +				handle, mmap_key);
> +
> +	if (status != EFI_SUCCESS) {
> +		efi_printk(sys_table, "exit boot services failed.\n");
> +		goto fail_free_mmap;
> +	}
> +
> +	return new_fdt_addr;
> +
> +fail_free_mmap:
> +	efi_call_phys1(sys_table->boottime->free_pool, memory_map);
> +
> +fail_free_new_fdt:
> +	efi_free(sys_table, new_fdt_size, new_fdt_addr);
> +
> +fail:
> +	return 0;
> +}
> +
> +
> +int efi_entry(void *handle, efi_system_table_t *sys_table,
> +	      unsigned long *zimage_addr)
> +{
> +	efi_loaded_image_t *image;
> +	int status;
> +	unsigned long nr_pages;
> +	const struct fdt_region *region;
> +
> +	void *fdt;
> +	int err;
> +	int node;
> +	unsigned long zimage_size = 0;
> +	unsigned long dram_base;
> +	/* addr/point and size pairs for memory management*/
> +	u64 initrd_addr;
> +	u64 initrd_size = 0;
> +	u64 fdt_addr;  /* Original DTB */
> +	u64 fdt_size = 0;
> +	u64 kernel_reserve_addr;
> +	u64 kernel_reserve_size = 0;
> +	char *cmdline_ptr;
> +	unsigned long cmdline_size = 0;
> +	unsigned long new_fdt_addr;
> +
> +	efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
> +
> +	/* Check if we were booted by the EFI firmware */
> +	if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
> +		goto fail;
> +
> +	efi_printk(sys_table, "Booting Linux using EFI stub.\n");
> +
> +
> +	/* get the command line from EFI, using the LOADED_IMAGE protocol */
> +	status = efi_call_phys3(sys_table->boottime->handle_protocol,
> +				handle, &proto, (void *)&image);
> +	if (status != EFI_SUCCESS) {
> +		efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
> +		goto fail;
> +	}
> +
> +	/* We are going to copy this into device tree, so we don't care where in
> +	 * memory it is.
> +	 */
> +	cmdline_ptr = convert_cmdline_to_ascii(sys_table, image,
> +					       &cmdline_size, 0xFFFFFFFF);
> +	if (!cmdline_ptr) {
> +		efi_printk(sys_table, "ERROR converting command line to ascii.\n");
> +		goto fail;
> +	}
> +
> +	/* We first load the device tree, as we need to get the base address of
> +	 * DRAM from the device tree.  The zImage, device tree, and initrd
> +	 * have address restrictions that are relative to the base of DRAM.
> +	 */
> +	status = handle_cmdline_files(sys_table, image, cmdline_ptr, "dtb=",
> +				      0xffffffff, &fdt_addr, &fdt_size);
> +	if (status != EFI_SUCCESS) {
> +		efi_printk(sys_table, "Error loading dtb blob\n");
> +		goto fail_free_cmdline;
> +	}
> +
> +	err = fdt_check_header((void *)(unsigned long)fdt_addr);
> +	if (err != 0) {
> +		efi_printk(sys_table, "ERROR: Device Tree header not valid\n");
> +		goto fail_free_dtb;
> +	}
> +	if (fdt_totalsize((void *)(unsigned long)fdt_addr) > fdt_size) {
> +		efi_printk(sys_table, "ERROR: Incomplete device tree.\n");
> +		goto fail_free_dtb;
> +
> +	}
> +
> +
> +	/* Look up the base of DRAM from the device tree.*/
> +	fdt = (void *)(u32)fdt_addr;
> +	node = fdt_subnode_offset(fdt, 0, "memory");
> +	region = fdt_getprop(fdt, node, "reg", NULL);
> +	if (region) {
> +		dram_base = fdt64_to_cpu(region->base);
> +	} else {
> +		efi_printk(sys_table, "Error: no 'memory' node in device tree.\n");
> +		goto fail_free_dtb;
> +	}
> +
> +	/* Reserve memory for the uncompressed kernel image. */
> +	kernel_reserve_addr = dram_base;
> +	kernel_reserve_size = MAX_UNCOMP_KERNEL_SIZE;
> +	nr_pages = round_up(kernel_reserve_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
> +	status = efi_call_phys4(sys_table->boottime->allocate_pages,
> +				EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
> +				nr_pages, &kernel_reserve_addr);
> +	if (status != EFI_SUCCESS) {
> +		efi_printk(sys_table, "ERROR allocating memory for uncompressed kernel.\n");
> +		goto fail_free_dtb;
> +	}
> +
> +	/* Relocate the zImage, if required. */
> +	zimage_size = image->image_size;
> +	status = relocate_kernel(sys_table, zimage_addr, &zimage_size,
> +				 dram_base + MIN_ZIMAGE_OFFSET,
> +				 dram_base + MAX_ZIMAGE_OFFSET);
> +	if (status != EFI_SUCCESS) {
> +		efi_printk(sys_table, "Failed to relocate kernel\n");
> +		goto fail_free_kernel_reserve;
> +	}
> +
> +	status = handle_cmdline_files(sys_table, image, cmdline_ptr, "initrd=",
> +				      dram_base + MAX_ZIMAGE_OFFSET,
> +				      &initrd_addr, &initrd_size);
> +	if (status != EFI_SUCCESS) {
> +		efi_printk(sys_table, "Error loading initrd\n");
> +		goto fail_free_zimage;
> +	}
> +
> +	new_fdt_addr = update_fdt_and_exit_boot(sys_table, handle,
> +						dram_base, fdt, &fdt_size,
> +						cmdline_ptr, &cmdline_size,
> +						initrd_addr, initrd_size);
> +
> +	if (new_fdt_addr == 0) {
> +		efi_printk(sys_table, "Error updating device tree and exiting boot services.\n");
> +		goto fail_free_initrd;
> +	}
> +
> +
> +	/* Now we need to return the FDT address to the calling
> +	 * assembly to this can be used as part of normal boot.
> +	 */
> +	return new_fdt_addr;
> +
> +fail_free_initrd:
> +	efi_free(sys_table, initrd_size, initrd_addr);
> +
> +fail_free_zimage:
> +	efi_free(sys_table, zimage_size, *zimage_addr);
> +
> +fail_free_kernel_reserve:
> +	efi_free(sys_table, kernel_reserve_addr, kernel_reserve_size);
> +
> +fail_free_dtb:
> +	efi_free(sys_table, fdt_size, fdt_addr);
> +
> +fail_free_cmdline:
> +	efi_free(sys_table, cmdline_size, (u32)cmdline_ptr);
> +
> +fail:
> +	return EFI_STUB_ERROR;
> +}
> diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
> index 75189f1..4c70b9e 100644
> --- a/arch/arm/boot/compressed/head.S
> +++ b/arch/arm/boot/compressed/head.S
> @@ -122,19 +122,106 @@
>  		.arm				@ Always enter in ARM state
>  start:
>  		.type	start,#function
> -		.rept	7
> +#ifdef CONFIG_EFI_STUB
> +		@ Magic MSDOS signature for PE/COFF + ADD opcode
> +		.word	0x62805a4d

What about BE32?

In that case, the instruction is a coprocessor load, that loads from a
random address to a coprocessor that almost certainly doesn't exist.
This will probably fault.

Since BE32 is only for older platforms (<v6) and this is not easily
solvable, it might be sensible to make the EFI stub support depend on
!CPU_ENDIAN_BE32.

> +#else
> +		mov	r0, r0
> +#endif
> +		.rept	5

You reduced the .rept count by 2, but only inserted one extra word,
perhaps because of the extra, but buggy, insertion below.

>  		mov	r0, r0
>  		.endr
>     ARM(		mov	r0, r0		)
>     ARM(		b	1f		)
>   THUMB(		adr	r12, BSYM(1f)	)
>   THUMB(		bx	r12		)

Can't you just replace 1f with zimage_continue directly in the above
lines, instead of the subsequent extra branch:

> + THUMB(		.thumb			)
> +1:
> +		b	zimage_continue

This also avoids having two labels both called '1'.

I believe the magic word is expected to be in a predictable offset,
but the size of the preceding branch is unpredictable in Thumb
(you could use b.w, or possibly remove the branch altogether, as
explained above).

>  		.word	0x016f2818		@ Magic numbers to help the loader
>  		.word	start			@ absolute load/run zImage address
>  		.word	_edata			@ zImage end address
> +
> +#ifdef CONFIG_EFI_STUB
> +		@ Portions of the MSDOS file header must be at offset
> +		@ 0x3c from the start of the file.  All PE/COFF headers
> +		@ are kept contiguous for simplicity.
> +#include "efi-header.S"
> +
> +efi_stub_entry:
> +		.text
> +		@ The EFI stub entry point is not at a fixed address, however
> +		@ this address must be set in the PE/COFF header.
> +		@ EFI entry point is in A32 mode, switch to T32 if configured.
> +		.arm
> +   ARM(		mov	r0, r0		)
> +   ARM(		b	1f		)

Those above two instructions are effectively just no-op padding.  Do you
need them at all?

> + THUMB(		adr	r12, BSYM(1f)	)
> + THUMB(		bx	r12		)
>   THUMB(		.thumb			)
>  1:
> +		@ Save lr on stack for possible return to EFI firmware.
> +		@ Don't care about fp, but need 64 bit alignment....
> +		stmfd	sp!, {fp, lr}
> +
> +		@ Save args to EFI app across got fixup call
> +		stmfd	sp!, {r0, r1}
> +		ldmfd	sp!, {r0, r1}
> +
> +		@ allocate space on stack for return of new entry point of
> +		@ zImage, as EFI stub may copy the kernel.  Pass address
> +		@ of space in r2 - EFI stub will fill in the pointer.
> +
> +		sub	sp, #8			@ we only need 4 bytes,
> +						@ but keep stack 8 byte aligned.
> +		mov	r2, sp
> +		@ Pass our actual runtime start address in pointer data
> +		adr	r11, LC0		@ address of LC0 at run time
> +		ldr	r12, [r11, #0]		@ address of LC0 at link time
> +
> +		sub	r3, r11, r12		@ calculate the delta offset
> +		str	r3, [r2, #0]
> +		bl	efi_entry
> +
> +		@ get new zImage entry address from stack, put into r3
> +		ldr	r3, [sp, #0]
> +		add	sp, #8  @ restore stack
> +
> +		@ Check for error return from EFI stub (0xFFFFFFFF)
> +		ldr	r1, =0xffffffff
> +		cmp	r0, r1
> +		beq	efi_load_fail
> +
> +
> +		@ Save return values of efi_entry
> +		stmfd	sp!, {r0, r3}
> +		bl	cache_clean_flush
> +		bl	cache_off
> +		ldmfd   sp!, {r0, r3}
> +
> +		@ put DTB address in r2, it was returned by EFI entry
> +		mov	r2, r0
> +		ldr	r1, =0xffffffff		@ DTB machine type
> +		mov	r0, #0  @ r0 is 0
> +
> +		@ Branch to (possibly) relocated zImage entry that is in r3
> +		bx	r3
> +
> +efi_load_fail:
> +	@ We need to exit THUMB mode here, to return to EFI firmware.

If you lose these 4 lines:

> + THUMB(		adr	r12, BSYM(1f)	)
> + THUMB(		bx	r12		)
> +1:
> +		.arm

...

> +		@ Return EFI_LOAD_ERROR to EFI firmware on error.
> +		ldr	r0, =0x80000001

and replace these

> +		ldmfd	sp!, {fp, lr}
> +		mov	pc, lr

with:

		ldmfd	sp!, {fp, pc}

then the Thumb-ness on return will be determined by whatever is loaded
into pc.

There's no special need to switch back to ARM before the final return,
provided that "mov pc,<Rm>" is not used (that will never switch from
Thumb to ARM).


Cheers
---Dave

> + THUMB(		.thumb			)

Then you can also remove the above line.

> +#endif
> +
> +zimage_continue:
>  		mrs	r9, cpsr
>  #ifdef CONFIG_ARM_VIRT_EXT
>  		bl	__hyp_stub_install	@ get into SVC mode, reversibly
> @@ -167,7 +254,6 @@ not_angel:
>  		 * by the linker here, but it should preserve r7, r8, and r9.
>  		 */
>  
> -		.text
>  
>  #ifdef CONFIG_AUTO_ZRELADDR
>  		@ determine final kernel image address
> -- 
> 1.7.10.4
> 
> 
> _______________________________________________
> linux-arm-kernel mailing list
> linux-arm-kernel@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH 6/7] Add EFI stub for ARM
@ 2013-08-05 14:11     ` Dave Martin
  0 siblings, 0 replies; 54+ messages in thread
From: Dave Martin @ 2013-08-05 14:11 UTC (permalink / raw)
  To: linux-arm-kernel

On Fri, Aug 02, 2013 at 02:29:07PM -0700, Roy Franz wrote:
> This patch adds EFI stub support for the ARM Linux kernel.  The EFI stub
> operations similarly to the x86 stub: it is a shim between the EFI firmware
> and the normal zImage entry point, and sets up the environment that the
> zImage is expecting.  This includes loading the initrd (optionaly) and
> device tree from the system partition based on the kernel command line.
> The stub updates the device tree as necessary, including adding reserved
> memory regions and adding entries for EFI runtime services. The PE/COFF
> "MZ" header at offset 0 results in the first instruction being an add
> that corrupts r5, which is not used by the zImage interface.
> 
> Signed-off-by: Roy Franz <roy.franz@linaro.org>
> ---
>  arch/arm/boot/compressed/Makefile     |   18 +-
>  arch/arm/boot/compressed/efi-header.S |  114 ++++++++
>  arch/arm/boot/compressed/efi-stub.c   |  514 +++++++++++++++++++++++++++++++++
>  arch/arm/boot/compressed/head.S       |   90 +++++-
>  4 files changed, 732 insertions(+), 4 deletions(-)
>  create mode 100644 arch/arm/boot/compressed/efi-header.S
>  create mode 100644 arch/arm/boot/compressed/efi-stub.c
> 
> diff --git a/arch/arm/boot/compressed/Makefile b/arch/arm/boot/compressed/Makefile
> index 7ac1610..c62826a 100644
> --- a/arch/arm/boot/compressed/Makefile
> +++ b/arch/arm/boot/compressed/Makefile
> @@ -106,8 +106,22 @@ $(addprefix $(obj)/,$(libfdt) $(libfdt_hdrs)): $(obj)/%: $(srctree)/scripts/dtc/
>  $(addprefix $(obj)/,$(libfdt_objs) atags_to_fdt.o): \
>  	$(addprefix $(obj)/,$(libfdt_hdrs))
>  
> +$(addprefix $(obj)/,$(libfdt_objs) efi-stub.o): \
> +	$(addprefix $(obj)/,$(libfdt_hdrs))
> +
>  ifeq ($(CONFIG_ARM_ATAG_DTB_COMPAT),y)
> -OBJS	+= $(libfdt_objs) atags_to_fdt.o
> +OBJS	+= atags_to_fdt.o
> +USE_LIBFDT = y
> +endif
> +
> +ifeq ($(CONFIG_EFI_STUB),y)
> +CFLAGS_efi-stub.o += -DTEXT_OFFSET=$(TEXT_OFFSET)
> +OBJS	+= efi-stub.o
> +USE_LIBFDT = y
> +endif
> +
> +ifeq ($(USE_LIBFDT),y)
> +OBJS	+= $(libfdt_objs)
>  endif
>  
>  targets       := vmlinux vmlinux.lds \
> @@ -125,7 +139,7 @@ ORIG_CFLAGS := $(KBUILD_CFLAGS)
>  KBUILD_CFLAGS = $(subst -pg, , $(ORIG_CFLAGS))
>  endif
>  
> -ccflags-y := -fpic -mno-single-pic-base -fno-builtin -I$(obj)
> +ccflags-y := -fpic -mno-single-pic-base -fno-builtin -I$(obj) -fno-stack-protector
>  asflags-y := -DZIMAGE
>  
>  # Supply kernel BSS size to the decompressor via a linker symbol.
> diff --git a/arch/arm/boot/compressed/efi-header.S b/arch/arm/boot/compressed/efi-header.S
> new file mode 100644
> index 0000000..6ff32cc
> --- /dev/null
> +++ b/arch/arm/boot/compressed/efi-header.S
> @@ -0,0 +1,114 @@
> +@ Copyright (C) 2013 Linaro Ltd;  <roy.franz@linaro.org>
> +@
> +@ This file contains the PE/COFF header that is part of the
> +@ EFI stub.
> +@
> +
> +	.org	0x3c
> +	@
> +	@ The PE header can be anywhere in the file, but for
> +	@ simplicity we keep it together with the MSDOS header
> +	@ The offset to the PE/COFF header needs to be at offset
> +	@ 0x3C in the MSDOS header.
> +	@ The only 2 fields of the MSDOS header that are used are this
> +	@ PE/COFF offset, and the "MZ" bytes at offset 0x0.
> +	@
> +	.long	pe_header			@ Offset to the PE header.
> +
> +      .align 3
> +pe_header:
> +
> +
> +pe_header:
> +	.ascii	"PE"
> +	.short 	0
> +
> +coff_header:
> +	.short	0x01c2				@ ARM or Thumb
> +	.short	2				@ nr_sections
> +	.long	0 				@ TimeDateStamp
> +	.long	0				@ PointerToSymbolTable
> +	.long	1				@ NumberOfSymbols
> +	.short	section_table - optional_header	@ SizeOfOptionalHeader
> +	.short	0x306				@ Characteristics.
> +						@ IMAGE_FILE_32BIT_MACHINE |
> +						@ IMAGE_FILE_DEBUG_STRIPPED |
> +						@ IMAGE_FILE_EXECUTABLE_IMAGE |
> +						@ IMAGE_FILE_LINE_NUMS_STRIPPED
> +
> +optional_header:
> +	.short	0x10b				@ PE32 format
> +	.byte	0x02				@ MajorLinkerVersion
> +	.byte	0x14				@ MinorLinkerVersion
> +
> +	.long	0				@ SizeOfCode
> +
> +	.long	0				@ SizeOfInitializedData
> +	.long	0				@ SizeOfUninitializedData
> +
> +	.long	efi_stub_entry			@ AddressOfEntryPoint
> +	.long	efi_stub_entry			@ BaseOfCode
> +	.long	0				@ data
> +
> +extra_header_fields:
> +	.long	0				@ ImageBase
> +	.long	0x20				@ SectionAlignment
> +	.long	0x20				@ FileAlignment
> +	.short	0				@ MajorOperatingSystemVersion
> +	.short	0				@ MinorOperatingSystemVersion
> +	.short	0				@ MajorImageVersion
> +	.short	0				@ MinorImageVersion
> +	.short	0				@ MajorSubsystemVersion
> +	.short	0				@ MinorSubsystemVersion
> +	.long	0				@ Win32VersionValue
> +
> +	.long	_edata				@ SizeOfImage
> +
> +	@ Everything before the entry point is considered part of the header
> +	.long	efi_stub_entry			@ SizeOfHeaders
> +	.long	0				@ CheckSum
> +	.short	0xa				@ Subsystem (EFI application)
> +	.short	0				@ DllCharacteristics
> +	.long	0				@ SizeOfStackReserve
> +	.long	0				@ SizeOfStackCommit
> +	.long	0				@ SizeOfHeapReserve
> +	.long	0				@ SizeOfHeapCommit
> +	.long	0				@ LoaderFlags
> +	.long	0x0				@ NumberOfRvaAndSizes
> +
> +	# Section table
> +section_table:
> +
> +	#
> +	# The EFI application loader requires a relocation section
> +	# because EFI applications must be relocatable.  This is a
> +	# dummy section as far as we are concerned.
> +	#
> +	.ascii	".reloc"
> +	.byte	0
> +	.byte	0			@ end of 0 padding of section name
> +	.long	0
> +	.long	0
> +	.long	0			@ SizeOfRawData
> +	.long	0			@ PointerToRawData
> +	.long	0			@ PointerToRelocations
> +	.long	0			@ PointerToLineNumbers
> +	.short	0			@ NumberOfRelocations
> +	.short	0			@ NumberOfLineNumbers
> +	.long	0x42100040		@ Characteristics (section flags)
> +
> +
> +	.ascii	".text"
> +	.byte	0
> +	.byte	0
> +	.byte	0        		@ end of 0 padding of section name
> +	.long	_edata - efi_stub_entry		@ VirtualSize
> +	.long	efi_stub_entry			@ VirtualAddress
> +	.long	_edata - efi_stub_entry		@ SizeOfRawData
> +	.long	efi_stub_entry			@ PointerToRawData
> +
> +	.long	0		@ PointerToRelocations (0 for executables)
> +	.long	0		@ PointerToLineNumbers (0 for executables)
> +	.short	0		@ NumberOfRelocations  (0 for executables)
> +	.short	0		@ NumberOfLineNumbers  (0 for executables)
> +	.long	0xe0500020	@ Characteristics (section flags)
> diff --git a/arch/arm/boot/compressed/efi-stub.c b/arch/arm/boot/compressed/efi-stub.c
> new file mode 100644
> index 0000000..41fa1e2
> --- /dev/null
> +++ b/arch/arm/boot/compressed/efi-stub.c
> @@ -0,0 +1,514 @@
> +/*
> + * linux/arch/arm/boot/compressed/efi-stub.c
> + *
> + * Copyright (C) 2013 Linaro Ltd;  <roy.franz@linaro.org>
> + *
> + * This file implements the EFI boot stub for the ARM kernel
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License version 2 as
> + * published by the Free Software Foundation.
> + *
> + */
> +#include <linux/efi.h>
> +#include <libfdt.h>
> +
> +
> +/* Error code returned to ASM code instead of valid FDT address. */
> +#define EFI_STUB_ERROR		(~0)
> +
> +/* EFI function call wrappers.  These are not required for
> + * ARM, but wrappers are required for X86 to convert between
> + * ABIs.  These wrappers are provided to allow code sharing
> + * between X86 and ARM.  Since these wrappers directly invoke the
> + * EFI function pointer, the function pointer type must be properly
> + * defined, which is not the case for X86  One advantage of this is
> + * it allows for type checking of arguments, which is not
> + * possible with the X86 wrappers.
> + */
> +#define efi_call_phys0(f)			f()
> +#define efi_call_phys1(f, a1)			f(a1)
> +#define efi_call_phys2(f, a1, a2)		f(a1, a2)
> +#define efi_call_phys3(f, a1, a2, a3)		f(a1, a2, a3)
> +#define efi_call_phys4(f, a1, a2, a3, a4)	f(a1, a2, a3, a4)
> +#define efi_call_phys5(f, a1, a2, a3, a4, a5)	f(a1, a2, a3, a4, a5)
> +
> +/* The maximum uncompressed kernel size is 32 MBytes, so we will reserve
> + * that for the decompressed kernel.  We have no easy way to tell what
> + * the actuall size of code + data the uncompressed kernel will use.
> + */
> +#define MAX_UNCOMP_KERNEL_SIZE	0x02000000
> +
> +/* The kernel zImage should be located between 32 Mbytes
> + * and 128 MBytes from the base of DRAM.  The min
> + * address leaves space for a maximal size uncompressed image,
> + * and the max address is due to how the zImage decompressor
> + * picks a destination address.
> + */
> +#define MAX_ZIMAGE_OFFSET	0x08000000
> +#define MIN_ZIMAGE_OFFSET	MAX_UNCOMP_KERNEL_SIZE
> +
> +#define MAX_CMDLINE_LEN		500
> +
> +struct fdt_region {
> +	u64 base;
> +	u64 size;
> +};
> +
> +/*
> + * Additional size that could be used for FDT entries added by
> + * the UEFI OS Loader Estimation based on:
> + * EDID (300bytes) + bootargs  + initrd region (20bytes)
> + * + system memory region (20bytes) + mp_core entries (200
> + * bytes)
> + */
> +#define FDT_ADDITIONAL_ENTRIES_SIZE     (0x300 + MAX_CMDLINE_LEN)
> +
> +/* Include shared EFI stub code */
> +#include "../../../../drivers/firmware/efi/efi-stub-helper.c"
> +
> +
> +static int is_linux_reserved_region(int memory_type)
> +{
> +	switch (memory_type) {
> +	case EfiRuntimeServicesCode:
> +	case EfiRuntimeServicesData:
> +	case EfiUnusableMemory:
> +	case EfiACPIReclaimMemory:
> +	case EfiACPIMemoryNVS:
> +		return 1;
> +	default:
> +		return 0;
> +	}
> +}
> +
> +
> +static int relocate_kernel(efi_system_table_t *sys_table,
> +			   unsigned long *load_addr, unsigned long *load_size,
> +			   unsigned long min_addr, unsigned long max_addr)
> +{
> +	/* Get current address of kernel. */
> +	unsigned long cur_zimage_addr = *load_addr;
> +	unsigned long zimage_size = *load_size;
> +	unsigned long new_addr = 0;
> +	unsigned long nr_pages;
> +
> +	efi_status_t status;
> +
> +	if (!load_addr || !load_size)
> +		return EFI_INVALID_PARAMETER;
> +
> +	*load_size = 0;
> +	if (cur_zimage_addr > min_addr
> +	    && (cur_zimage_addr + zimage_size) < max_addr) {
> +		/* We don't need to do anything, as kernel at an acceptable
> +		 * address already.
> +		 */
> +		return EFI_SUCCESS;
> +	}
> +	/*
> +	 * The EFI firmware loader could have placed the kernel image
> +	 * anywhere in memory, but the kernel has restrictions on the
> +	 * min and max physical address it can run at.
> +	 */
> +	nr_pages = round_up(zimage_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
> +
> +	status = efi_low_alloc(sys_table, zimage_size, 0,
> +			   &new_addr, min_addr);
> +	if (status != EFI_SUCCESS) {
> +		efi_printk(sys_table, "Failed to alloc memory for kernel.\n");
> +		return status;
> +	}
> +
> +	if (new_addr > (max_addr - zimage_size)) {
> +		efi_free(sys_table, zimage_size, new_addr);
> +		efi_printk(sys_table, "Failed to alloc usable memory for kernel.\n");
> +		return EFI_INVALID_PARAMETER;
> +	}
> +
> +	/* We know source/dest won't overlap since both memory ranges
> +	 * have been allocated by UEFI, so we can safely use memcpy.
> +	 */
> +	memcpy((void *)new_addr, (void *)(unsigned long)cur_zimage_addr,
> +	       zimage_size);
> +
> +	/* Return the load address and size */
> +	*load_addr = new_addr;
> +	*load_size = zimage_size;
> +
> +
> +	return status;
> +}
> +
> +
> +/* Convert the unicode UEFI command line to ASCII to pass to kernel.
> + * Size of memory allocated return in *cmd_line_len.
> + * Returns NULL on error.
> + */
> +static char *convert_cmdline_to_ascii(efi_system_table_t *sys_table,
> +				      efi_loaded_image_t *image,
> +				      unsigned long *cmd_line_len,
> +				      u32 max_addr)
> +{
> +	u16 *s2;
> +	u8 *s1 = NULL;
> +	unsigned long cmdline_addr = 0;
> +	int load_options_size = image->load_options_size / 2; /* ASCII */
> +	void *options = (u16 *)image->load_options;
> +	int options_size = 0;
> +	int status;
> +	int i;
> +	u16 zero = 0;
> +
> +	if (options) {
> +		s2 = options;
> +		while (*s2 && *s2 != '\n' && options_size < load_options_size) {
> +			s2++;
> +			options_size++;
> +		}
> +	}
> +
> +	if (options_size == 0) {
> +		/* No command line options, so return empty string*/
> +		options_size = 1;
> +		options = &zero;
> +	}
> +
> +	if (options_size > MAX_CMDLINE_LEN)
> +		options_size = MAX_CMDLINE_LEN;
> +
> +	options_size++;  /* NUL termination */
> +
> +	status = efi_high_alloc(sys_table, options_size, 0,
> +			    &cmdline_addr, max_addr);
> +	if (status != EFI_SUCCESS)
> +		return NULL;
> +
> +	s1 = (u8 *)(unsigned long)cmdline_addr;
> +	s2 = (u16 *)options;
> +
> +	for (i = 0; i < options_size - 1; i++)
> +		*s1++ = *s2++;
> +
> +	*s1 = '\0';
> +
> +	*cmd_line_len = options_size;
> +	return (char *)(unsigned long)cmdline_addr;
> +}
> +
> +static u32 update_fdt_and_exit_boot(efi_system_table_t *sys_table,
> +				    void *handle, unsigned long dram_base,
> +				    void *orig_fdt, u64 *orig_fdt_size,
> +				    char *cmdline_ptr,
> +				    unsigned long *cmdline_size,
> +				    u64 initrd_addr, u64 initrd_size)
> +{
> +	unsigned long new_fdt_size;
> +	unsigned long new_fdt_addr;
> +	void *fdt;
> +	int node;
> +	int status;
> +	int i;
> +	unsigned long map_size, desc_size;
> +	unsigned long mmap_key;
> +	efi_memory_desc_t *memory_map;
> +	unsigned long fdt_val;
> +
> +	new_fdt_size = *orig_fdt_size + FDT_ADDITIONAL_ENTRIES_SIZE;
> +	status = efi_high_alloc(sys_table, new_fdt_size, 0, &new_fdt_addr,
> +			    dram_base + MAX_ZIMAGE_OFFSET);
> +	if (status != EFI_SUCCESS) {
> +		efi_printk(sys_table, "ERROR: Unable to allocate memory for new device tree.\n");
> +		goto fail;
> +	}
> +
> +
> +	fdt = (void *)new_fdt_addr;
> +	status = fdt_open_into(orig_fdt, fdt, new_fdt_size);
> +	if (status != 0) {
> +		efi_printk(sys_table, "ERROR: Device Tree open_int failed.\n");
> +		goto fail_free_new_fdt;
> +	}
> +	/* We are done with the original DTB, so free it. */
> +	efi_free(sys_table, *orig_fdt_size, (u32)orig_fdt);
> +	*orig_fdt_size = 0;
> +
> +	node = fdt_subnode_offset(fdt, 0, "chosen");
> +	if (node < 0) {
> +		node = fdt_add_subnode(fdt, 0, "chosen");
> +		if (node < 0) {
> +			efi_printk(sys_table, "Error on finding 'chosen' node\n");
> +			goto fail_free_new_fdt;
> +		}
> +	}
> +
> +	if ((cmdline_ptr != NULL) && (strlen(cmdline_ptr) > 0)) {
> +		status = fdt_setprop(fdt, node, "bootargs", cmdline_ptr,
> +				     strlen(cmdline_ptr) + 1);
> +		if (status) {
> +			efi_printk(sys_table, "Failed to set new bootarg\n");
> +			goto fail_free_new_fdt;
> +		}
> +	}
> +	/* We are done with original command line, so free it. */
> +	efi_free(sys_table, *cmdline_size, (u32)cmdline_ptr);
> +	*cmdline_size = 0;
> +
> +	/* Set intird address/end in device tree, if present */
> +	if (initrd_size != 0) {
> +		u64 initrd_image_end;
> +		u64 initrd_image_start = cpu_to_fdt64(initrd_addr);
> +		status = fdt_setprop(fdt, node, "linux,initrd-start",
> +				     &initrd_image_start, sizeof(u64));
> +		if (status) {
> +			efi_printk(sys_table, "Failed to set new 'linux,initrd-start'\n");
> +			goto fail_free_new_fdt;
> +		}
> +		initrd_image_end = cpu_to_fdt64(initrd_addr + initrd_size);
> +		status = fdt_setprop(fdt, node, "linux,initrd-end",
> +				     &initrd_image_end, sizeof(u64));
> +		if (status) {
> +			efi_printk(sys_table, "Failed to set new 'linux,initrd-end'\n");
> +			goto fail_free_new_fdt;
> +		}
> +	}
> +
> +	/* Update memory map in the device tree. The memory node must
> +	 * be present in the tree.*/
> +	node = fdt_subnode_offset(fdt, 0, "memory");
> +	if (node < 0) {
> +		efi_printk(sys_table, "ERROR: FDT memory node does not exist in DTB.\n");
> +		goto fail_free_new_fdt;
> +	}
> +
> +	status = efi_get_memory_map(sys_table, &memory_map, &map_size,
> +				    &desc_size, &mmap_key);
> +	if (status != EFI_SUCCESS)
> +		goto fail_free_new_fdt;
> +
> +	for (i = 0; i < (map_size / sizeof(efi_memory_desc_t)); i++) {
> +		efi_memory_desc_t *desc;
> +		unsigned long m = (unsigned long)memory_map;
> +		desc = (efi_memory_desc_t *)(m + (i * desc_size));
> +
> +		if (is_linux_reserved_region(desc->type)) {
> +			status = fdt_add_mem_rsv(fdt, desc->phys_addr,
> +						 desc->num_pages * EFI_PAGE_SIZE);
> +			if (status != 0) {
> +				efi_printk(sys_table, "ERROR: Failed to add 'memreserve' to fdt.\n");
> +				goto fail_free_mmap;
> +			}
> +		}
> +	}
> +
> +
> +	/* Add FDT entries for EFI runtime services in chosen node.
> +	 * We need to add the final memory map, so this is done at
> +	 * the very end.
> +	 */
> +	node = fdt_subnode_offset(fdt, 0, "chosen");
> +	fdt_val = cpu_to_fdt32((unsigned long)sys_table);
> +	status = fdt_setprop(fdt, node, "efi-system-table",
> +			     &fdt_val, sizeof(fdt_val));
> +	if (status) {
> +		efi_printk(sys_table, "Failed to set new 'efi-system-table'\n");
> +		goto fail_free_new_fdt;
> +	}
> +	fdt_val = cpu_to_fdt32(desc_size);
> +	status = fdt_setprop(fdt, node, "efi-mmap-desc-size",
> +			     &fdt_val, sizeof(fdt_val));
> +	if (status) {
> +		efi_printk(sys_table, "Failed to set new 'efi-mmap-desc-size'\n");
> +		goto fail_free_new_fdt;
> +	}
> +	fdt_val = cpu_to_fdt32(map_size);
> +	status = fdt_setprop(fdt, node, "efi-runtime-mmap-size",
> +			     &fdt_val, sizeof(fdt_val));
> +	if (status) {
> +		efi_printk(sys_table, "Failed to set new 'efi-runtime-mmap-size'\n");
> +		goto fail_free_new_fdt;
> +	}
> +	fdt_val = cpu_to_fdt32((unsigned long)memory_map);
> +	status = fdt_setprop(fdt, node, "efi-runtime-mmap",
> +			     &fdt_val, sizeof(fdt_val));
> +	if (status) {
> +		efi_printk(sys_table, "Failed to set new 'efi-runtime-mmap'\n");
> +		goto fail_free_new_fdt;
> +	}
> +
> +	/* Now we need to exit boot services.  We need the key from
> +	 * the most recent read of the memory map to do this.  We can't
> +	 * free this buffer in the normal case, but do free it when
> +	 * exit_boot_services() fails or adding the memory map to the FDT
> +	 * fails.
> +	 */
> +	status = efi_call_phys2(sys_table->boottime->exit_boot_services,
> +				handle, mmap_key);
> +
> +	if (status != EFI_SUCCESS) {
> +		efi_printk(sys_table, "exit boot services failed.\n");
> +		goto fail_free_mmap;
> +	}
> +
> +	return new_fdt_addr;
> +
> +fail_free_mmap:
> +	efi_call_phys1(sys_table->boottime->free_pool, memory_map);
> +
> +fail_free_new_fdt:
> +	efi_free(sys_table, new_fdt_size, new_fdt_addr);
> +
> +fail:
> +	return 0;
> +}
> +
> +
> +int efi_entry(void *handle, efi_system_table_t *sys_table,
> +	      unsigned long *zimage_addr)
> +{
> +	efi_loaded_image_t *image;
> +	int status;
> +	unsigned long nr_pages;
> +	const struct fdt_region *region;
> +
> +	void *fdt;
> +	int err;
> +	int node;
> +	unsigned long zimage_size = 0;
> +	unsigned long dram_base;
> +	/* addr/point and size pairs for memory management*/
> +	u64 initrd_addr;
> +	u64 initrd_size = 0;
> +	u64 fdt_addr;  /* Original DTB */
> +	u64 fdt_size = 0;
> +	u64 kernel_reserve_addr;
> +	u64 kernel_reserve_size = 0;
> +	char *cmdline_ptr;
> +	unsigned long cmdline_size = 0;
> +	unsigned long new_fdt_addr;
> +
> +	efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
> +
> +	/* Check if we were booted by the EFI firmware */
> +	if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
> +		goto fail;
> +
> +	efi_printk(sys_table, "Booting Linux using EFI stub.\n");
> +
> +
> +	/* get the command line from EFI, using the LOADED_IMAGE protocol */
> +	status = efi_call_phys3(sys_table->boottime->handle_protocol,
> +				handle, &proto, (void *)&image);
> +	if (status != EFI_SUCCESS) {
> +		efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
> +		goto fail;
> +	}
> +
> +	/* We are going to copy this into device tree, so we don't care where in
> +	 * memory it is.
> +	 */
> +	cmdline_ptr = convert_cmdline_to_ascii(sys_table, image,
> +					       &cmdline_size, 0xFFFFFFFF);
> +	if (!cmdline_ptr) {
> +		efi_printk(sys_table, "ERROR converting command line to ascii.\n");
> +		goto fail;
> +	}
> +
> +	/* We first load the device tree, as we need to get the base address of
> +	 * DRAM from the device tree.  The zImage, device tree, and initrd
> +	 * have address restrictions that are relative to the base of DRAM.
> +	 */
> +	status = handle_cmdline_files(sys_table, image, cmdline_ptr, "dtb=",
> +				      0xffffffff, &fdt_addr, &fdt_size);
> +	if (status != EFI_SUCCESS) {
> +		efi_printk(sys_table, "Error loading dtb blob\n");
> +		goto fail_free_cmdline;
> +	}
> +
> +	err = fdt_check_header((void *)(unsigned long)fdt_addr);
> +	if (err != 0) {
> +		efi_printk(sys_table, "ERROR: Device Tree header not valid\n");
> +		goto fail_free_dtb;
> +	}
> +	if (fdt_totalsize((void *)(unsigned long)fdt_addr) > fdt_size) {
> +		efi_printk(sys_table, "ERROR: Incomplete device tree.\n");
> +		goto fail_free_dtb;
> +
> +	}
> +
> +
> +	/* Look up the base of DRAM from the device tree.*/
> +	fdt = (void *)(u32)fdt_addr;
> +	node = fdt_subnode_offset(fdt, 0, "memory");
> +	region = fdt_getprop(fdt, node, "reg", NULL);
> +	if (region) {
> +		dram_base = fdt64_to_cpu(region->base);
> +	} else {
> +		efi_printk(sys_table, "Error: no 'memory' node in device tree.\n");
> +		goto fail_free_dtb;
> +	}
> +
> +	/* Reserve memory for the uncompressed kernel image. */
> +	kernel_reserve_addr = dram_base;
> +	kernel_reserve_size = MAX_UNCOMP_KERNEL_SIZE;
> +	nr_pages = round_up(kernel_reserve_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
> +	status = efi_call_phys4(sys_table->boottime->allocate_pages,
> +				EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
> +				nr_pages, &kernel_reserve_addr);
> +	if (status != EFI_SUCCESS) {
> +		efi_printk(sys_table, "ERROR allocating memory for uncompressed kernel.\n");
> +		goto fail_free_dtb;
> +	}
> +
> +	/* Relocate the zImage, if required. */
> +	zimage_size = image->image_size;
> +	status = relocate_kernel(sys_table, zimage_addr, &zimage_size,
> +				 dram_base + MIN_ZIMAGE_OFFSET,
> +				 dram_base + MAX_ZIMAGE_OFFSET);
> +	if (status != EFI_SUCCESS) {
> +		efi_printk(sys_table, "Failed to relocate kernel\n");
> +		goto fail_free_kernel_reserve;
> +	}
> +
> +	status = handle_cmdline_files(sys_table, image, cmdline_ptr, "initrd=",
> +				      dram_base + MAX_ZIMAGE_OFFSET,
> +				      &initrd_addr, &initrd_size);
> +	if (status != EFI_SUCCESS) {
> +		efi_printk(sys_table, "Error loading initrd\n");
> +		goto fail_free_zimage;
> +	}
> +
> +	new_fdt_addr = update_fdt_and_exit_boot(sys_table, handle,
> +						dram_base, fdt, &fdt_size,
> +						cmdline_ptr, &cmdline_size,
> +						initrd_addr, initrd_size);
> +
> +	if (new_fdt_addr == 0) {
> +		efi_printk(sys_table, "Error updating device tree and exiting boot services.\n");
> +		goto fail_free_initrd;
> +	}
> +
> +
> +	/* Now we need to return the FDT address to the calling
> +	 * assembly to this can be used as part of normal boot.
> +	 */
> +	return new_fdt_addr;
> +
> +fail_free_initrd:
> +	efi_free(sys_table, initrd_size, initrd_addr);
> +
> +fail_free_zimage:
> +	efi_free(sys_table, zimage_size, *zimage_addr);
> +
> +fail_free_kernel_reserve:
> +	efi_free(sys_table, kernel_reserve_addr, kernel_reserve_size);
> +
> +fail_free_dtb:
> +	efi_free(sys_table, fdt_size, fdt_addr);
> +
> +fail_free_cmdline:
> +	efi_free(sys_table, cmdline_size, (u32)cmdline_ptr);
> +
> +fail:
> +	return EFI_STUB_ERROR;
> +}
> diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
> index 75189f1..4c70b9e 100644
> --- a/arch/arm/boot/compressed/head.S
> +++ b/arch/arm/boot/compressed/head.S
> @@ -122,19 +122,106 @@
>  		.arm				@ Always enter in ARM state
>  start:
>  		.type	start,#function
> -		.rept	7
> +#ifdef CONFIG_EFI_STUB
> +		@ Magic MSDOS signature for PE/COFF + ADD opcode
> +		.word	0x62805a4d

What about BE32?

In that case, the instruction is a coprocessor load, that loads from a
random address to a coprocessor that almost certainly doesn't exist.
This will probably fault.

Since BE32 is only for older platforms (<v6) and this is not easily
solvable, it might be sensible to make the EFI stub support depend on
!CPU_ENDIAN_BE32.

> +#else
> +		mov	r0, r0
> +#endif
> +		.rept	5

You reduced the .rept count by 2, but only inserted one extra word,
perhaps because of the extra, but buggy, insertion below.

>  		mov	r0, r0
>  		.endr
>     ARM(		mov	r0, r0		)
>     ARM(		b	1f		)
>   THUMB(		adr	r12, BSYM(1f)	)
>   THUMB(		bx	r12		)

Can't you just replace 1f with zimage_continue directly in the above
lines, instead of the subsequent extra branch:

> + THUMB(		.thumb			)
> +1:
> +		b	zimage_continue

This also avoids having two labels both called '1'.

I believe the magic word is expected to be in a predictable offset,
but the size of the preceding branch is unpredictable in Thumb
(you could use b.w, or possibly remove the branch altogether, as
explained above).

>  		.word	0x016f2818		@ Magic numbers to help the loader
>  		.word	start			@ absolute load/run zImage address
>  		.word	_edata			@ zImage end address
> +
> +#ifdef CONFIG_EFI_STUB
> +		@ Portions of the MSDOS file header must be at offset
> +		@ 0x3c from the start of the file.  All PE/COFF headers
> +		@ are kept contiguous for simplicity.
> +#include "efi-header.S"
> +
> +efi_stub_entry:
> +		.text
> +		@ The EFI stub entry point is not at a fixed address, however
> +		@ this address must be set in the PE/COFF header.
> +		@ EFI entry point is in A32 mode, switch to T32 if configured.
> +		.arm
> +   ARM(		mov	r0, r0		)
> +   ARM(		b	1f		)

Those above two instructions are effectively just no-op padding.  Do you
need them at all?

> + THUMB(		adr	r12, BSYM(1f)	)
> + THUMB(		bx	r12		)
>   THUMB(		.thumb			)
>  1:
> +		@ Save lr on stack for possible return to EFI firmware.
> +		@ Don't care about fp, but need 64 bit alignment....
> +		stmfd	sp!, {fp, lr}
> +
> +		@ Save args to EFI app across got fixup call
> +		stmfd	sp!, {r0, r1}
> +		ldmfd	sp!, {r0, r1}
> +
> +		@ allocate space on stack for return of new entry point of
> +		@ zImage, as EFI stub may copy the kernel.  Pass address
> +		@ of space in r2 - EFI stub will fill in the pointer.
> +
> +		sub	sp, #8			@ we only need 4 bytes,
> +						@ but keep stack 8 byte aligned.
> +		mov	r2, sp
> +		@ Pass our actual runtime start address in pointer data
> +		adr	r11, LC0		@ address of LC0 at run time
> +		ldr	r12, [r11, #0]		@ address of LC0 at link time
> +
> +		sub	r3, r11, r12		@ calculate the delta offset
> +		str	r3, [r2, #0]
> +		bl	efi_entry
> +
> +		@ get new zImage entry address from stack, put into r3
> +		ldr	r3, [sp, #0]
> +		add	sp, #8  @ restore stack
> +
> +		@ Check for error return from EFI stub (0xFFFFFFFF)
> +		ldr	r1, =0xffffffff
> +		cmp	r0, r1
> +		beq	efi_load_fail
> +
> +
> +		@ Save return values of efi_entry
> +		stmfd	sp!, {r0, r3}
> +		bl	cache_clean_flush
> +		bl	cache_off
> +		ldmfd   sp!, {r0, r3}
> +
> +		@ put DTB address in r2, it was returned by EFI entry
> +		mov	r2, r0
> +		ldr	r1, =0xffffffff		@ DTB machine type
> +		mov	r0, #0  @ r0 is 0
> +
> +		@ Branch to (possibly) relocated zImage entry that is in r3
> +		bx	r3
> +
> +efi_load_fail:
> +	@ We need to exit THUMB mode here, to return to EFI firmware.

If you lose these 4 lines:

> + THUMB(		adr	r12, BSYM(1f)	)
> + THUMB(		bx	r12		)
> +1:
> +		.arm

...

> +		@ Return EFI_LOAD_ERROR to EFI firmware on error.
> +		ldr	r0, =0x80000001

and replace these

> +		ldmfd	sp!, {fp, lr}
> +		mov	pc, lr

with:

		ldmfd	sp!, {fp, pc}

then the Thumb-ness on return will be determined by whatever is loaded
into pc.

There's no special need to switch back to ARM before the final return,
provided that "mov pc,<Rm>" is not used (that will never switch from
Thumb to ARM).


Cheers
---Dave

> + THUMB(		.thumb			)

Then you can also remove the above line.

> +#endif
> +
> +zimage_continue:
>  		mrs	r9, cpsr
>  #ifdef CONFIG_ARM_VIRT_EXT
>  		bl	__hyp_stub_install	@ get into SVC mode, reversibly
> @@ -167,7 +254,6 @@ not_angel:
>  		 * by the linker here, but it should preserve r7, r8, and r9.
>  		 */
>  
> -		.text
>  
>  #ifdef CONFIG_AUTO_ZRELADDR
>  		@ determine final kernel image address
> -- 
> 1.7.10.4
> 
> 
> _______________________________________________
> linux-arm-kernel mailing list
> linux-arm-kernel at lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 54+ messages in thread

* Re: [PATCH 1/7] EFI stub documentation updates
@ 2013-08-05 14:12     ` Dave Martin
  0 siblings, 0 replies; 54+ messages in thread
From: Dave Martin @ 2013-08-05 14:12 UTC (permalink / raw)
  To: Roy Franz
  Cc: linux-kernel, linux-efi, linux-arm-kernel, matt.fleming, linux,
	leif.lindholm

On Fri, Aug 02, 2013 at 02:29:02PM -0700, Roy Franz wrote:
> The ARM kernel also has an EFI stub which works largely the same way
> as the x86 stub, so move the documentation out of x86 directory and
> update to reflect that it is generic, and add ARM specific text.
> 
> Signed-off-by: Roy Franz <roy.franz@linaro.org>
> ---
>  Documentation/efi-stub.txt     |   78 ++++++++++++++++++++++++++++++++++++++++
>  Documentation/x86/efi-stub.txt |   65 ---------------------------------
>  arch/x86/Kconfig               |    2 +-
>  3 files changed, 79 insertions(+), 66 deletions(-)
>  create mode 100644 Documentation/efi-stub.txt
>  delete mode 100644 Documentation/x86/efi-stub.txt
> 
> diff --git a/Documentation/efi-stub.txt b/Documentation/efi-stub.txt
> new file mode 100644
> index 0000000..7837df1
> --- /dev/null
> +++ b/Documentation/efi-stub.txt
> @@ -0,0 +1,78 @@
> +			  The EFI Boot Stub
> +		     ---------------------------
> +
> +On the x86 and ARM platforms, a bzImage can masquerade as a PE/COFF image,
> +thereby convincing EFI firmware loaders to load it as an EFI
> +executable. The code that modifies the bzImage header, along with the


Minor nit, I don't think there is such a thing as "bzImage" for ARM.

Cheers
---Dave

> +EFI-specific entry point that the firmware loader jumps to are
> +collectively known as the "EFI boot stub", and live in
> +arch/x86/boot/header.S and arch/x86/boot/compressed/eboot.c,
> +respectively.  For ARM the EFI stub is implemented in
> +arch/arm/boot/compressed/efi-header.S and
> +arch/arm/boot/compressed/efi-stub.c.  EFI stub code that is shared
> +between architectures is in drivers/firmware/efi/efi-stub-helper.c.
> +
> +By using the EFI boot stub it's possible to boot a Linux kernel
> +without the use of a conventional EFI boot loader, such as grub or
> +elilo. Since the EFI boot stub performs the jobs of a boot loader, in
> +a certain sense it *IS* the boot loader.
> +
> +The EFI boot stub is enabled with the CONFIG_EFI_STUB kernel option.
> +
> +
> +**** How to install bzImage.efi
> +
> +The bzImage located in arch/x86/boot/bzImage must be copied to the EFI
> +System Partiion (ESP) and renamed with the extension ".efi". Without
> +the extension the EFI firmware loader will refuse to execute it. It's
> +not possible to execute bzImage.efi from the usual Linux file systems
> +because EFI firmware doesn't have support for them.  For ARM the
> +arch/arm/boot/zImage should be copied to the system partition, and it
> +may not need to be renamed.
> +
> +
> +**** Passing kernel parameters from the EFI shell
> +
> +Arguments to the kernel can be passed after bzImage.efi, e.g.
> +
> +	fs0:> bzImage.efi console=ttyS0 root=/dev/sda4
> +
> +
> +**** The "initrd=" option
> +
> +Like most boot loaders, the EFI stub allows the user to specify
> +multiple initrd files using the "initrd=" option. This is the only EFI
> +stub-specific command line parameter, everything else is passed to the
> +kernel when it boots.
> +
> +The path to the initrd file must be an absolute path from the
> +beginning of the ESP, relative path names do not work. Also, the path
> +is an EFI-style path and directory elements must be separated with
> +backslashes (\). For example, given the following directory layout,
> +
> +fs0:>
> +	Kernels\
> +			bzImage.efi
> +			initrd-large.img
> +
> +	Ramdisks\
> +			initrd-small.img
> +			initrd-medium.img
> +
> +to boot with the initrd-large.img file if the current working
> +directory is fs0:\Kernels, the following command must be used,
> +
> +	fs0:\Kernels> bzImage.efi initrd=\Kernels\initrd-large.img
> +
> +Notice how bzImage.efi can be specified with a relative path. That's
> +because the image we're executing is interpreted by the EFI shell,
> +which understands relative paths, whereas the rest of the command line
> +is passed to bzImage.efi.
> +
> +
> +**** The "dtb=" option
> +
> +For the ARM architecture, we also need to be able to provide a device
> +tree to the kernel.  This is done with the "dtb=" command line option,
> +and is process in the same manner as the "initrd=" option that is described
> +above.
> diff --git a/Documentation/x86/efi-stub.txt b/Documentation/x86/efi-stub.txt
> deleted file mode 100644
> index 44e6bb6..0000000
> --- a/Documentation/x86/efi-stub.txt
> +++ /dev/null
> @@ -1,65 +0,0 @@
> -			  The EFI Boot Stub
> -		     ---------------------------
> -
> -On the x86 platform, a bzImage can masquerade as a PE/COFF image,
> -thereby convincing EFI firmware loaders to load it as an EFI
> -executable. The code that modifies the bzImage header, along with the
> -EFI-specific entry point that the firmware loader jumps to are
> -collectively known as the "EFI boot stub", and live in
> -arch/x86/boot/header.S and arch/x86/boot/compressed/eboot.c,
> -respectively.
> -
> -By using the EFI boot stub it's possible to boot a Linux kernel
> -without the use of a conventional EFI boot loader, such as grub or
> -elilo. Since the EFI boot stub performs the jobs of a boot loader, in
> -a certain sense it *IS* the boot loader.
> -
> -The EFI boot stub is enabled with the CONFIG_EFI_STUB kernel option.
> -
> -
> -**** How to install bzImage.efi
> -
> -The bzImage located in arch/x86/boot/bzImage must be copied to the EFI
> -System Partiion (ESP) and renamed with the extension ".efi". Without
> -the extension the EFI firmware loader will refuse to execute it. It's
> -not possible to execute bzImage.efi from the usual Linux file systems
> -because EFI firmware doesn't have support for them.
> -
> -
> -**** Passing kernel parameters from the EFI shell
> -
> -Arguments to the kernel can be passed after bzImage.efi, e.g.
> -
> -	fs0:> bzImage.efi console=ttyS0 root=/dev/sda4
> -
> -
> -**** The "initrd=" option
> -
> -Like most boot loaders, the EFI stub allows the user to specify
> -multiple initrd files using the "initrd=" option. This is the only EFI
> -stub-specific command line parameter, everything else is passed to the
> -kernel when it boots.
> -
> -The path to the initrd file must be an absolute path from the
> -beginning of the ESP, relative path names do not work. Also, the path
> -is an EFI-style path and directory elements must be separated with
> -backslashes (\). For example, given the following directory layout,
> -
> -fs0:>
> -	Kernels\
> -			bzImage.efi
> -			initrd-large.img
> -
> -	Ramdisks\
> -			initrd-small.img
> -			initrd-medium.img
> -
> -to boot with the initrd-large.img file if the current working
> -directory is fs0:\Kernels, the following command must be used,
> -
> -	fs0:\Kernels> bzImage.efi initrd=\Kernels\initrd-large.img
> -
> -Notice how bzImage.efi can be specified with a relative path. That's
> -because the image we're executing is interpreted by the EFI shell,
> -which understands relative paths, whereas the rest of the command line
> -is passed to bzImage.efi.
> diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
> index b32ebf9..ec65b51 100644
> --- a/arch/x86/Kconfig
> +++ b/arch/x86/Kconfig
> @@ -1579,7 +1579,7 @@ config EFI_STUB
>            This kernel feature allows a bzImage to be loaded directly
>  	  by EFI firmware without the use of a bootloader.
>  
> -	  See Documentation/x86/efi-stub.txt for more information.
> +	  See Documentation/efi-stub.txt for more information.
>  
>  config SECCOMP
>  	def_bool y
> -- 
> 1.7.10.4
> 
> 
> _______________________________________________
> linux-arm-kernel mailing list
> linux-arm-kernel@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 54+ messages in thread

* Re: [PATCH 1/7] EFI stub documentation updates
@ 2013-08-05 14:12     ` Dave Martin
  0 siblings, 0 replies; 54+ messages in thread
From: Dave Martin @ 2013-08-05 14:12 UTC (permalink / raw)
  To: Roy Franz
  Cc: linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-efi-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matt.fleming-ral2JQCrhuEAvxtiuMwx3w,
	linux-lFZ/pmaqli7XmaaqVzeoHQ,
	leif.lindholm-QSEj5FYQhm4dnm+yROfE0A

On Fri, Aug 02, 2013 at 02:29:02PM -0700, Roy Franz wrote:
> The ARM kernel also has an EFI stub which works largely the same way
> as the x86 stub, so move the documentation out of x86 directory and
> update to reflect that it is generic, and add ARM specific text.
> 
> Signed-off-by: Roy Franz <roy.franz-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
> ---
>  Documentation/efi-stub.txt     |   78 ++++++++++++++++++++++++++++++++++++++++
>  Documentation/x86/efi-stub.txt |   65 ---------------------------------
>  arch/x86/Kconfig               |    2 +-
>  3 files changed, 79 insertions(+), 66 deletions(-)
>  create mode 100644 Documentation/efi-stub.txt
>  delete mode 100644 Documentation/x86/efi-stub.txt
> 
> diff --git a/Documentation/efi-stub.txt b/Documentation/efi-stub.txt
> new file mode 100644
> index 0000000..7837df1
> --- /dev/null
> +++ b/Documentation/efi-stub.txt
> @@ -0,0 +1,78 @@
> +			  The EFI Boot Stub
> +		     ---------------------------
> +
> +On the x86 and ARM platforms, a bzImage can masquerade as a PE/COFF image,
> +thereby convincing EFI firmware loaders to load it as an EFI
> +executable. The code that modifies the bzImage header, along with the


Minor nit, I don't think there is such a thing as "bzImage" for ARM.

Cheers
---Dave

> +EFI-specific entry point that the firmware loader jumps to are
> +collectively known as the "EFI boot stub", and live in
> +arch/x86/boot/header.S and arch/x86/boot/compressed/eboot.c,
> +respectively.  For ARM the EFI stub is implemented in
> +arch/arm/boot/compressed/efi-header.S and
> +arch/arm/boot/compressed/efi-stub.c.  EFI stub code that is shared
> +between architectures is in drivers/firmware/efi/efi-stub-helper.c.
> +
> +By using the EFI boot stub it's possible to boot a Linux kernel
> +without the use of a conventional EFI boot loader, such as grub or
> +elilo. Since the EFI boot stub performs the jobs of a boot loader, in
> +a certain sense it *IS* the boot loader.
> +
> +The EFI boot stub is enabled with the CONFIG_EFI_STUB kernel option.
> +
> +
> +**** How to install bzImage.efi
> +
> +The bzImage located in arch/x86/boot/bzImage must be copied to the EFI
> +System Partiion (ESP) and renamed with the extension ".efi". Without
> +the extension the EFI firmware loader will refuse to execute it. It's
> +not possible to execute bzImage.efi from the usual Linux file systems
> +because EFI firmware doesn't have support for them.  For ARM the
> +arch/arm/boot/zImage should be copied to the system partition, and it
> +may not need to be renamed.
> +
> +
> +**** Passing kernel parameters from the EFI shell
> +
> +Arguments to the kernel can be passed after bzImage.efi, e.g.
> +
> +	fs0:> bzImage.efi console=ttyS0 root=/dev/sda4
> +
> +
> +**** The "initrd=" option
> +
> +Like most boot loaders, the EFI stub allows the user to specify
> +multiple initrd files using the "initrd=" option. This is the only EFI
> +stub-specific command line parameter, everything else is passed to the
> +kernel when it boots.
> +
> +The path to the initrd file must be an absolute path from the
> +beginning of the ESP, relative path names do not work. Also, the path
> +is an EFI-style path and directory elements must be separated with
> +backslashes (\). For example, given the following directory layout,
> +
> +fs0:>
> +	Kernels\
> +			bzImage.efi
> +			initrd-large.img
> +
> +	Ramdisks\
> +			initrd-small.img
> +			initrd-medium.img
> +
> +to boot with the initrd-large.img file if the current working
> +directory is fs0:\Kernels, the following command must be used,
> +
> +	fs0:\Kernels> bzImage.efi initrd=\Kernels\initrd-large.img
> +
> +Notice how bzImage.efi can be specified with a relative path. That's
> +because the image we're executing is interpreted by the EFI shell,
> +which understands relative paths, whereas the rest of the command line
> +is passed to bzImage.efi.
> +
> +
> +**** The "dtb=" option
> +
> +For the ARM architecture, we also need to be able to provide a device
> +tree to the kernel.  This is done with the "dtb=" command line option,
> +and is process in the same manner as the "initrd=" option that is described
> +above.
> diff --git a/Documentation/x86/efi-stub.txt b/Documentation/x86/efi-stub.txt
> deleted file mode 100644
> index 44e6bb6..0000000
> --- a/Documentation/x86/efi-stub.txt
> +++ /dev/null
> @@ -1,65 +0,0 @@
> -			  The EFI Boot Stub
> -		     ---------------------------
> -
> -On the x86 platform, a bzImage can masquerade as a PE/COFF image,
> -thereby convincing EFI firmware loaders to load it as an EFI
> -executable. The code that modifies the bzImage header, along with the
> -EFI-specific entry point that the firmware loader jumps to are
> -collectively known as the "EFI boot stub", and live in
> -arch/x86/boot/header.S and arch/x86/boot/compressed/eboot.c,
> -respectively.
> -
> -By using the EFI boot stub it's possible to boot a Linux kernel
> -without the use of a conventional EFI boot loader, such as grub or
> -elilo. Since the EFI boot stub performs the jobs of a boot loader, in
> -a certain sense it *IS* the boot loader.
> -
> -The EFI boot stub is enabled with the CONFIG_EFI_STUB kernel option.
> -
> -
> -**** How to install bzImage.efi
> -
> -The bzImage located in arch/x86/boot/bzImage must be copied to the EFI
> -System Partiion (ESP) and renamed with the extension ".efi". Without
> -the extension the EFI firmware loader will refuse to execute it. It's
> -not possible to execute bzImage.efi from the usual Linux file systems
> -because EFI firmware doesn't have support for them.
> -
> -
> -**** Passing kernel parameters from the EFI shell
> -
> -Arguments to the kernel can be passed after bzImage.efi, e.g.
> -
> -	fs0:> bzImage.efi console=ttyS0 root=/dev/sda4
> -
> -
> -**** The "initrd=" option
> -
> -Like most boot loaders, the EFI stub allows the user to specify
> -multiple initrd files using the "initrd=" option. This is the only EFI
> -stub-specific command line parameter, everything else is passed to the
> -kernel when it boots.
> -
> -The path to the initrd file must be an absolute path from the
> -beginning of the ESP, relative path names do not work. Also, the path
> -is an EFI-style path and directory elements must be separated with
> -backslashes (\). For example, given the following directory layout,
> -
> -fs0:>
> -	Kernels\
> -			bzImage.efi
> -			initrd-large.img
> -
> -	Ramdisks\
> -			initrd-small.img
> -			initrd-medium.img
> -
> -to boot with the initrd-large.img file if the current working
> -directory is fs0:\Kernels, the following command must be used,
> -
> -	fs0:\Kernels> bzImage.efi initrd=\Kernels\initrd-large.img
> -
> -Notice how bzImage.efi can be specified with a relative path. That's
> -because the image we're executing is interpreted by the EFI shell,
> -which understands relative paths, whereas the rest of the command line
> -is passed to bzImage.efi.
> diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
> index b32ebf9..ec65b51 100644
> --- a/arch/x86/Kconfig
> +++ b/arch/x86/Kconfig
> @@ -1579,7 +1579,7 @@ config EFI_STUB
>            This kernel feature allows a bzImage to be loaded directly
>  	  by EFI firmware without the use of a bootloader.
>  
> -	  See Documentation/x86/efi-stub.txt for more information.
> +	  See Documentation/efi-stub.txt for more information.
>  
>  config SECCOMP
>  	def_bool y
> -- 
> 1.7.10.4
> 
> 
> _______________________________________________
> linux-arm-kernel mailing list
> linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH 1/7] EFI stub documentation updates
@ 2013-08-05 14:12     ` Dave Martin
  0 siblings, 0 replies; 54+ messages in thread
From: Dave Martin @ 2013-08-05 14:12 UTC (permalink / raw)
  To: linux-arm-kernel

On Fri, Aug 02, 2013 at 02:29:02PM -0700, Roy Franz wrote:
> The ARM kernel also has an EFI stub which works largely the same way
> as the x86 stub, so move the documentation out of x86 directory and
> update to reflect that it is generic, and add ARM specific text.
> 
> Signed-off-by: Roy Franz <roy.franz@linaro.org>
> ---
>  Documentation/efi-stub.txt     |   78 ++++++++++++++++++++++++++++++++++++++++
>  Documentation/x86/efi-stub.txt |   65 ---------------------------------
>  arch/x86/Kconfig               |    2 +-
>  3 files changed, 79 insertions(+), 66 deletions(-)
>  create mode 100644 Documentation/efi-stub.txt
>  delete mode 100644 Documentation/x86/efi-stub.txt
> 
> diff --git a/Documentation/efi-stub.txt b/Documentation/efi-stub.txt
> new file mode 100644
> index 0000000..7837df1
> --- /dev/null
> +++ b/Documentation/efi-stub.txt
> @@ -0,0 +1,78 @@
> +			  The EFI Boot Stub
> +		     ---------------------------
> +
> +On the x86 and ARM platforms, a bzImage can masquerade as a PE/COFF image,
> +thereby convincing EFI firmware loaders to load it as an EFI
> +executable. The code that modifies the bzImage header, along with the


Minor nit, I don't think there is such a thing as "bzImage" for ARM.

Cheers
---Dave

> +EFI-specific entry point that the firmware loader jumps to are
> +collectively known as the "EFI boot stub", and live in
> +arch/x86/boot/header.S and arch/x86/boot/compressed/eboot.c,
> +respectively.  For ARM the EFI stub is implemented in
> +arch/arm/boot/compressed/efi-header.S and
> +arch/arm/boot/compressed/efi-stub.c.  EFI stub code that is shared
> +between architectures is in drivers/firmware/efi/efi-stub-helper.c.
> +
> +By using the EFI boot stub it's possible to boot a Linux kernel
> +without the use of a conventional EFI boot loader, such as grub or
> +elilo. Since the EFI boot stub performs the jobs of a boot loader, in
> +a certain sense it *IS* the boot loader.
> +
> +The EFI boot stub is enabled with the CONFIG_EFI_STUB kernel option.
> +
> +
> +**** How to install bzImage.efi
> +
> +The bzImage located in arch/x86/boot/bzImage must be copied to the EFI
> +System Partiion (ESP) and renamed with the extension ".efi". Without
> +the extension the EFI firmware loader will refuse to execute it. It's
> +not possible to execute bzImage.efi from the usual Linux file systems
> +because EFI firmware doesn't have support for them.  For ARM the
> +arch/arm/boot/zImage should be copied to the system partition, and it
> +may not need to be renamed.
> +
> +
> +**** Passing kernel parameters from the EFI shell
> +
> +Arguments to the kernel can be passed after bzImage.efi, e.g.
> +
> +	fs0:> bzImage.efi console=ttyS0 root=/dev/sda4
> +
> +
> +**** The "initrd=" option
> +
> +Like most boot loaders, the EFI stub allows the user to specify
> +multiple initrd files using the "initrd=" option. This is the only EFI
> +stub-specific command line parameter, everything else is passed to the
> +kernel when it boots.
> +
> +The path to the initrd file must be an absolute path from the
> +beginning of the ESP, relative path names do not work. Also, the path
> +is an EFI-style path and directory elements must be separated with
> +backslashes (\). For example, given the following directory layout,
> +
> +fs0:>
> +	Kernels\
> +			bzImage.efi
> +			initrd-large.img
> +
> +	Ramdisks\
> +			initrd-small.img
> +			initrd-medium.img
> +
> +to boot with the initrd-large.img file if the current working
> +directory is fs0:\Kernels, the following command must be used,
> +
> +	fs0:\Kernels> bzImage.efi initrd=\Kernels\initrd-large.img
> +
> +Notice how bzImage.efi can be specified with a relative path. That's
> +because the image we're executing is interpreted by the EFI shell,
> +which understands relative paths, whereas the rest of the command line
> +is passed to bzImage.efi.
> +
> +
> +**** The "dtb=" option
> +
> +For the ARM architecture, we also need to be able to provide a device
> +tree to the kernel.  This is done with the "dtb=" command line option,
> +and is process in the same manner as the "initrd=" option that is described
> +above.
> diff --git a/Documentation/x86/efi-stub.txt b/Documentation/x86/efi-stub.txt
> deleted file mode 100644
> index 44e6bb6..0000000
> --- a/Documentation/x86/efi-stub.txt
> +++ /dev/null
> @@ -1,65 +0,0 @@
> -			  The EFI Boot Stub
> -		     ---------------------------
> -
> -On the x86 platform, a bzImage can masquerade as a PE/COFF image,
> -thereby convincing EFI firmware loaders to load it as an EFI
> -executable. The code that modifies the bzImage header, along with the
> -EFI-specific entry point that the firmware loader jumps to are
> -collectively known as the "EFI boot stub", and live in
> -arch/x86/boot/header.S and arch/x86/boot/compressed/eboot.c,
> -respectively.
> -
> -By using the EFI boot stub it's possible to boot a Linux kernel
> -without the use of a conventional EFI boot loader, such as grub or
> -elilo. Since the EFI boot stub performs the jobs of a boot loader, in
> -a certain sense it *IS* the boot loader.
> -
> -The EFI boot stub is enabled with the CONFIG_EFI_STUB kernel option.
> -
> -
> -**** How to install bzImage.efi
> -
> -The bzImage located in arch/x86/boot/bzImage must be copied to the EFI
> -System Partiion (ESP) and renamed with the extension ".efi". Without
> -the extension the EFI firmware loader will refuse to execute it. It's
> -not possible to execute bzImage.efi from the usual Linux file systems
> -because EFI firmware doesn't have support for them.
> -
> -
> -**** Passing kernel parameters from the EFI shell
> -
> -Arguments to the kernel can be passed after bzImage.efi, e.g.
> -
> -	fs0:> bzImage.efi console=ttyS0 root=/dev/sda4
> -
> -
> -**** The "initrd=" option
> -
> -Like most boot loaders, the EFI stub allows the user to specify
> -multiple initrd files using the "initrd=" option. This is the only EFI
> -stub-specific command line parameter, everything else is passed to the
> -kernel when it boots.
> -
> -The path to the initrd file must be an absolute path from the
> -beginning of the ESP, relative path names do not work. Also, the path
> -is an EFI-style path and directory elements must be separated with
> -backslashes (\). For example, given the following directory layout,
> -
> -fs0:>
> -	Kernels\
> -			bzImage.efi
> -			initrd-large.img
> -
> -	Ramdisks\
> -			initrd-small.img
> -			initrd-medium.img
> -
> -to boot with the initrd-large.img file if the current working
> -directory is fs0:\Kernels, the following command must be used,
> -
> -	fs0:\Kernels> bzImage.efi initrd=\Kernels\initrd-large.img
> -
> -Notice how bzImage.efi can be specified with a relative path. That's
> -because the image we're executing is interpreted by the EFI shell,
> -which understands relative paths, whereas the rest of the command line
> -is passed to bzImage.efi.
> diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
> index b32ebf9..ec65b51 100644
> --- a/arch/x86/Kconfig
> +++ b/arch/x86/Kconfig
> @@ -1579,7 +1579,7 @@ config EFI_STUB
>            This kernel feature allows a bzImage to be loaded directly
>  	  by EFI firmware without the use of a bootloader.
>  
> -	  See Documentation/x86/efi-stub.txt for more information.
> +	  See Documentation/efi-stub.txt for more information.
>  
>  config SECCOMP
>  	def_bool y
> -- 
> 1.7.10.4
> 
> 
> _______________________________________________
> linux-arm-kernel mailing list
> linux-arm-kernel at lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 54+ messages in thread

* Re: [PATCH 6/7] Add EFI stub for ARM
  2013-08-05 14:11     ` Dave Martin
@ 2013-08-05 15:33       ` Leif Lindholm
  -1 siblings, 0 replies; 54+ messages in thread
From: Leif Lindholm @ 2013-08-05 15:33 UTC (permalink / raw)
  To: Dave Martin
  Cc: Roy Franz, linux-kernel, linux-efi, linux-arm-kernel,
	matt.fleming, linux

On Mon, Aug 05, 2013 at 03:11:49PM +0100, Dave Martin wrote:
> > diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
> > index 75189f1..4c70b9e 100644
> > --- a/arch/arm/boot/compressed/head.S
> > +++ b/arch/arm/boot/compressed/head.S
> > @@ -122,19 +122,106 @@
> >  		.arm				@ Always enter in ARM state
> >  start:
> >  		.type	start,#function
> > -		.rept	7
> > +#ifdef CONFIG_EFI_STUB
> > +		@ Magic MSDOS signature for PE/COFF + ADD opcode
> > +		.word	0x62805a4d
> 
> What about BE32?
 
The ARM bindings for UEFI specify that the processor must be in
little-endian mode.

> In that case, the instruction is a coprocessor load, that loads from a
> random address to a coprocessor that almost certainly doesn't exist.
> This will probably fault.
> 
> Since BE32 is only for older platforms (<v6) and this is not easily
> solvable, it might be sensible to make the EFI stub support depend on
> !CPU_ENDIAN_BE32.
 
Well, it would make more sense to make EFI_STUB depend on EFI and
EFI depend on !CPU_ENDIAN_BE32. Which is something I can add to
my next set of general ARM UEFI patches. Thanks.

/
    Leif

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH 6/7] Add EFI stub for ARM
@ 2013-08-05 15:33       ` Leif Lindholm
  0 siblings, 0 replies; 54+ messages in thread
From: Leif Lindholm @ 2013-08-05 15:33 UTC (permalink / raw)
  To: linux-arm-kernel

On Mon, Aug 05, 2013 at 03:11:49PM +0100, Dave Martin wrote:
> > diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
> > index 75189f1..4c70b9e 100644
> > --- a/arch/arm/boot/compressed/head.S
> > +++ b/arch/arm/boot/compressed/head.S
> > @@ -122,19 +122,106 @@
> >  		.arm				@ Always enter in ARM state
> >  start:
> >  		.type	start,#function
> > -		.rept	7
> > +#ifdef CONFIG_EFI_STUB
> > +		@ Magic MSDOS signature for PE/COFF + ADD opcode
> > +		.word	0x62805a4d
> 
> What about BE32?
 
The ARM bindings for UEFI specify that the processor must be in
little-endian mode.

> In that case, the instruction is a coprocessor load, that loads from a
> random address to a coprocessor that almost certainly doesn't exist.
> This will probably fault.
> 
> Since BE32 is only for older platforms (<v6) and this is not easily
> solvable, it might be sensible to make the EFI stub support depend on
> !CPU_ENDIAN_BE32.
 
Well, it would make more sense to make EFI_STUB depend on EFI and
EFI depend on !CPU_ENDIAN_BE32. Which is something I can add to
my next set of general ARM UEFI patches. Thanks.

/
    Leif

^ permalink raw reply	[flat|nested] 54+ messages in thread

* Re: [PATCH 1/7] EFI stub documentation updates
@ 2013-08-05 23:56       ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-05 23:56 UTC (permalink / raw)
  To: Dave Martin
  Cc: linux-kernel, linux-efi, linux-arm-kernel, matt.fleming,
	Russell King - ARM Linux, Leif Lindholm

On Mon, Aug 5, 2013 at 7:12 AM, Dave Martin <Dave.Martin@arm.com> wrote:
> On Fri, Aug 02, 2013 at 02:29:02PM -0700, Roy Franz wrote:
>> The ARM kernel also has an EFI stub which works largely the same way
>> as the x86 stub, so move the documentation out of x86 directory and
>> update to reflect that it is generic, and add ARM specific text.
>>
>> Signed-off-by: Roy Franz <roy.franz@linaro.org>
>> ---
>>  Documentation/efi-stub.txt     |   78 ++++++++++++++++++++++++++++++++++++++++
>>  Documentation/x86/efi-stub.txt |   65 ---------------------------------
>>  arch/x86/Kconfig               |    2 +-
>>  3 files changed, 79 insertions(+), 66 deletions(-)
>>  create mode 100644 Documentation/efi-stub.txt
>>  delete mode 100644 Documentation/x86/efi-stub.txt
>>
>> diff --git a/Documentation/efi-stub.txt b/Documentation/efi-stub.txt
>> new file mode 100644
>> index 0000000..7837df1
>> --- /dev/null
>> +++ b/Documentation/efi-stub.txt
>> @@ -0,0 +1,78 @@
>> +                       The EFI Boot Stub
>> +                  ---------------------------
>> +
>> +On the x86 and ARM platforms, a bzImage can masquerade as a PE/COFF image,
>> +thereby convincing EFI firmware loaders to load it as an EFI
>> +executable. The code that modifies the bzImage header, along with the
>
>
> Minor nit, I don't think there is such a thing as "bzImage" for ARM.
>
> Cheers
> ---Dave
>
Yeah, I don't thinks so either...  How about:

On the x86 and ARM platforms, a kernel zImage/bzImage can masquerade....


Roy

^ permalink raw reply	[flat|nested] 54+ messages in thread

* Re: [PATCH 1/7] EFI stub documentation updates
@ 2013-08-05 23:56       ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-05 23:56 UTC (permalink / raw)
  To: Dave Martin
  Cc: linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-efi-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matt.fleming-ral2JQCrhuEAvxtiuMwx3w, Russell King - ARM Linux,
	Leif Lindholm

On Mon, Aug 5, 2013 at 7:12 AM, Dave Martin <Dave.Martin-5wv7dgnIgG8@public.gmane.org> wrote:
> On Fri, Aug 02, 2013 at 02:29:02PM -0700, Roy Franz wrote:
>> The ARM kernel also has an EFI stub which works largely the same way
>> as the x86 stub, so move the documentation out of x86 directory and
>> update to reflect that it is generic, and add ARM specific text.
>>
>> Signed-off-by: Roy Franz <roy.franz-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
>> ---
>>  Documentation/efi-stub.txt     |   78 ++++++++++++++++++++++++++++++++++++++++
>>  Documentation/x86/efi-stub.txt |   65 ---------------------------------
>>  arch/x86/Kconfig               |    2 +-
>>  3 files changed, 79 insertions(+), 66 deletions(-)
>>  create mode 100644 Documentation/efi-stub.txt
>>  delete mode 100644 Documentation/x86/efi-stub.txt
>>
>> diff --git a/Documentation/efi-stub.txt b/Documentation/efi-stub.txt
>> new file mode 100644
>> index 0000000..7837df1
>> --- /dev/null
>> +++ b/Documentation/efi-stub.txt
>> @@ -0,0 +1,78 @@
>> +                       The EFI Boot Stub
>> +                  ---------------------------
>> +
>> +On the x86 and ARM platforms, a bzImage can masquerade as a PE/COFF image,
>> +thereby convincing EFI firmware loaders to load it as an EFI
>> +executable. The code that modifies the bzImage header, along with the
>
>
> Minor nit, I don't think there is such a thing as "bzImage" for ARM.
>
> Cheers
> ---Dave
>
Yeah, I don't thinks so either...  How about:

On the x86 and ARM platforms, a kernel zImage/bzImage can masquerade....


Roy

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH 1/7] EFI stub documentation updates
@ 2013-08-05 23:56       ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-05 23:56 UTC (permalink / raw)
  To: linux-arm-kernel

On Mon, Aug 5, 2013 at 7:12 AM, Dave Martin <Dave.Martin@arm.com> wrote:
> On Fri, Aug 02, 2013 at 02:29:02PM -0700, Roy Franz wrote:
>> The ARM kernel also has an EFI stub which works largely the same way
>> as the x86 stub, so move the documentation out of x86 directory and
>> update to reflect that it is generic, and add ARM specific text.
>>
>> Signed-off-by: Roy Franz <roy.franz@linaro.org>
>> ---
>>  Documentation/efi-stub.txt     |   78 ++++++++++++++++++++++++++++++++++++++++
>>  Documentation/x86/efi-stub.txt |   65 ---------------------------------
>>  arch/x86/Kconfig               |    2 +-
>>  3 files changed, 79 insertions(+), 66 deletions(-)
>>  create mode 100644 Documentation/efi-stub.txt
>>  delete mode 100644 Documentation/x86/efi-stub.txt
>>
>> diff --git a/Documentation/efi-stub.txt b/Documentation/efi-stub.txt
>> new file mode 100644
>> index 0000000..7837df1
>> --- /dev/null
>> +++ b/Documentation/efi-stub.txt
>> @@ -0,0 +1,78 @@
>> +                       The EFI Boot Stub
>> +                  ---------------------------
>> +
>> +On the x86 and ARM platforms, a bzImage can masquerade as a PE/COFF image,
>> +thereby convincing EFI firmware loaders to load it as an EFI
>> +executable. The code that modifies the bzImage header, along with the
>
>
> Minor nit, I don't think there is such a thing as "bzImage" for ARM.
>
> Cheers
> ---Dave
>
Yeah, I don't thinks so either...  How about:

On the x86 and ARM platforms, a kernel zImage/bzImage can masquerade....


Roy

^ permalink raw reply	[flat|nested] 54+ messages in thread

* Re: [PATCH 6/7] Add EFI stub for ARM
@ 2013-08-06  0:06         ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-06  0:06 UTC (permalink / raw)
  To: Leif Lindholm
  Cc: Dave Martin, linux-kernel, linux-efi, linux-arm-kernel,
	matt.fleming, Russell King - ARM Linux

On Mon, Aug 5, 2013 at 8:33 AM, Leif Lindholm <leif.lindholm@linaro.org> wrote:
> On Mon, Aug 05, 2013 at 03:11:49PM +0100, Dave Martin wrote:
>> > diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
>> > index 75189f1..4c70b9e 100644
>> > --- a/arch/arm/boot/compressed/head.S
>> > +++ b/arch/arm/boot/compressed/head.S
>> > @@ -122,19 +122,106 @@
>> >             .arm                            @ Always enter in ARM state
>> >  start:
>> >             .type   start,#function
>> > -           .rept   7
>> > +#ifdef CONFIG_EFI_STUB
>> > +           @ Magic MSDOS signature for PE/COFF + ADD opcode
>> > +           .word   0x62805a4d
>>
>> What about BE32?
>
> The ARM bindings for UEFI specify that the processor must be in
> little-endian mode.
>
>> In that case, the instruction is a coprocessor load, that loads from a
>> random address to a coprocessor that almost certainly doesn't exist.
>> This will probably fault.
>>
>> Since BE32 is only for older platforms (<v6) and this is not easily
>> solvable, it might be sensible to make the EFI stub support depend on
>> !CPU_ENDIAN_BE32.
>
> Well, it would make more sense to make EFI_STUB depend on EFI and
> EFI depend on !CPU_ENDIAN_BE32. Which is something I can add to
> my next set of general ARM UEFI patches. Thanks.
> /
>     Leif

I had EFI_STUB depend on EFI at one point during my development, but took
it out because there was no actual dependency (the stub will work fine without
other EFI features.)  The features will most likely be used together,
but I wasn't
sure if we would want to enforce this with a config dependency.  I
don't care one
way or the other, I'd just like the dependencies to be correct and
follow best practices.

Thanks,
Roy

^ permalink raw reply	[flat|nested] 54+ messages in thread

* Re: [PATCH 6/7] Add EFI stub for ARM
@ 2013-08-06  0:06         ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-06  0:06 UTC (permalink / raw)
  To: Leif Lindholm
  Cc: Dave Martin, linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-efi-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matt.fleming-ral2JQCrhuEAvxtiuMwx3w, Russell King - ARM Linux

On Mon, Aug 5, 2013 at 8:33 AM, Leif Lindholm <leif.lindholm-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org> wrote:
> On Mon, Aug 05, 2013 at 03:11:49PM +0100, Dave Martin wrote:
>> > diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
>> > index 75189f1..4c70b9e 100644
>> > --- a/arch/arm/boot/compressed/head.S
>> > +++ b/arch/arm/boot/compressed/head.S
>> > @@ -122,19 +122,106 @@
>> >             .arm                            @ Always enter in ARM state
>> >  start:
>> >             .type   start,#function
>> > -           .rept   7
>> > +#ifdef CONFIG_EFI_STUB
>> > +           @ Magic MSDOS signature for PE/COFF + ADD opcode
>> > +           .word   0x62805a4d
>>
>> What about BE32?
>
> The ARM bindings for UEFI specify that the processor must be in
> little-endian mode.
>
>> In that case, the instruction is a coprocessor load, that loads from a
>> random address to a coprocessor that almost certainly doesn't exist.
>> This will probably fault.
>>
>> Since BE32 is only for older platforms (<v6) and this is not easily
>> solvable, it might be sensible to make the EFI stub support depend on
>> !CPU_ENDIAN_BE32.
>
> Well, it would make more sense to make EFI_STUB depend on EFI and
> EFI depend on !CPU_ENDIAN_BE32. Which is something I can add to
> my next set of general ARM UEFI patches. Thanks.
> /
>     Leif

I had EFI_STUB depend on EFI at one point during my development, but took
it out because there was no actual dependency (the stub will work fine without
other EFI features.)  The features will most likely be used together,
but I wasn't
sure if we would want to enforce this with a config dependency.  I
don't care one
way or the other, I'd just like the dependencies to be correct and
follow best practices.

Thanks,
Roy

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH 6/7] Add EFI stub for ARM
@ 2013-08-06  0:06         ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-06  0:06 UTC (permalink / raw)
  To: linux-arm-kernel

On Mon, Aug 5, 2013 at 8:33 AM, Leif Lindholm <leif.lindholm@linaro.org> wrote:
> On Mon, Aug 05, 2013 at 03:11:49PM +0100, Dave Martin wrote:
>> > diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
>> > index 75189f1..4c70b9e 100644
>> > --- a/arch/arm/boot/compressed/head.S
>> > +++ b/arch/arm/boot/compressed/head.S
>> > @@ -122,19 +122,106 @@
>> >             .arm                            @ Always enter in ARM state
>> >  start:
>> >             .type   start,#function
>> > -           .rept   7
>> > +#ifdef CONFIG_EFI_STUB
>> > +           @ Magic MSDOS signature for PE/COFF + ADD opcode
>> > +           .word   0x62805a4d
>>
>> What about BE32?
>
> The ARM bindings for UEFI specify that the processor must be in
> little-endian mode.
>
>> In that case, the instruction is a coprocessor load, that loads from a
>> random address to a coprocessor that almost certainly doesn't exist.
>> This will probably fault.
>>
>> Since BE32 is only for older platforms (<v6) and this is not easily
>> solvable, it might be sensible to make the EFI stub support depend on
>> !CPU_ENDIAN_BE32.
>
> Well, it would make more sense to make EFI_STUB depend on EFI and
> EFI depend on !CPU_ENDIAN_BE32. Which is something I can add to
> my next set of general ARM UEFI patches. Thanks.
> /
>     Leif

I had EFI_STUB depend on EFI at one point during my development, but took
it out because there was no actual dependency (the stub will work fine without
other EFI features.)  The features will most likely be used together,
but I wasn't
sure if we would want to enforce this with a config dependency.  I
don't care one
way or the other, I'd just like the dependencies to be correct and
follow best practices.

Thanks,
Roy

^ permalink raw reply	[flat|nested] 54+ messages in thread

* Re: [PATCH 6/7] Add EFI stub for ARM
  2013-08-05 14:11     ` Dave Martin
  (?)
@ 2013-08-06  3:35       ` Roy Franz
  -1 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-06  3:35 UTC (permalink / raw)
  To: Dave Martin
  Cc: linux-kernel, linux-efi, linux-arm-kernel, matt.fleming,
	Russell King - ARM Linux, Leif Lindholm

>> diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
>> index 75189f1..4c70b9e 100644
>> --- a/arch/arm/boot/compressed/head.S
>> +++ b/arch/arm/boot/compressed/head.S
>> @@ -122,19 +122,106 @@
>>               .arm                            @ Always enter in ARM state
>>  start:
>>               .type   start,#function
>> -             .rept   7
>> +#ifdef CONFIG_EFI_STUB
>> +             @ Magic MSDOS signature for PE/COFF + ADD opcode
>> +             .word   0x62805a4d
>
> What about BE32?
>
> In that case, the instruction is a coprocessor load, that loads from a
> random address to a coprocessor that almost certainly doesn't exist.
> This will probably fault.
>
> Since BE32 is only for older platforms (<v6) and this is not easily
> solvable, it might be sensible to make the EFI stub support depend on
> !CPU_ENDIAN_BE32.
>
>> +#else
>> +             mov     r0, r0
>> +#endif
>> +             .rept   5
>
> You reduced the .rept count by 2, but only inserted one extra word,
> perhaps because of the extra, but buggy, insertion below.
>
>>               mov     r0, r0
>>               .endr
>>     ARM(              mov     r0, r0          )
>>     ARM(              b       1f              )
>>   THUMB(              adr     r12, BSYM(1f)   )
>>   THUMB(              bx      r12             )
>
> Can't you just replace 1f with zimage_continue directly in the above
> lines, instead of the subsequent extra branch:
>
>> + THUMB(              .thumb                  )
>> +1:
>> +             b       zimage_continue
>
> This also avoids having two labels both called '1'.
>
> I believe the magic word is expected to be in a predictable offset,
> but the size of the preceding branch is unpredictable in Thumb
> (you could use b.w, or possibly remove the branch altogether, as
> explained above).
>
>>               .word   0x016f2818              @ Magic numbers to help the loader
>>               .word   start                   @ absolute load/run zImage address
>>               .word   _edata                  @ zImage end address
>> +
>> +#ifdef CONFIG_EFI_STUB
>> +             @ Portions of the MSDOS file header must be at offset
>> +             @ 0x3c from the start of the file.  All PE/COFF headers
>> +             @ are kept contiguous for simplicity.
>> +#include "efi-header.S"
>> +
>> +efi_stub_entry:
>> +             .text
>> +             @ The EFI stub entry point is not at a fixed address, however
>> +             @ this address must be set in the PE/COFF header.
>> +             @ EFI entry point is in A32 mode, switch to T32 if configured.
>> +             .arm
>> +   ARM(              mov     r0, r0          )
>> +   ARM(              b       1f              )
>
> Those above two instructions are effectively just no-op padding.  Do you
> need them at all?
>
>> + THUMB(              adr     r12, BSYM(1f)   )
>> + THUMB(              bx      r12             )
>>   THUMB(              .thumb                  )
>>  1:
>> +             @ Save lr on stack for possible return to EFI firmware.
>> +             @ Don't care about fp, but need 64 bit alignment....
>> +             stmfd   sp!, {fp, lr}
>> +
>> +             @ Save args to EFI app across got fixup call
>> +             stmfd   sp!, {r0, r1}
>> +             ldmfd   sp!, {r0, r1}
>> +
>> +             @ allocate space on stack for return of new entry point of
>> +             @ zImage, as EFI stub may copy the kernel.  Pass address
>> +             @ of space in r2 - EFI stub will fill in the pointer.
>> +
>> +             sub     sp, #8                  @ we only need 4 bytes,
>> +                                             @ but keep stack 8 byte aligned.
>> +             mov     r2, sp
>> +             @ Pass our actual runtime start address in pointer data
>> +             adr     r11, LC0                @ address of LC0 at run time
>> +             ldr     r12, [r11, #0]          @ address of LC0 at link time
>> +
>> +             sub     r3, r11, r12            @ calculate the delta offset
>> +             str     r3, [r2, #0]
>> +             bl      efi_entry
>> +
>> +             @ get new zImage entry address from stack, put into r3
>> +             ldr     r3, [sp, #0]
>> +             add     sp, #8  @ restore stack
>> +
>> +             @ Check for error return from EFI stub (0xFFFFFFFF)
>> +             ldr     r1, =0xffffffff
>> +             cmp     r0, r1
>> +             beq     efi_load_fail
>> +
>> +
>> +             @ Save return values of efi_entry
>> +             stmfd   sp!, {r0, r3}
>> +             bl      cache_clean_flush
>> +             bl      cache_off
>> +             ldmfd   sp!, {r0, r3}
>> +
>> +             @ put DTB address in r2, it was returned by EFI entry
>> +             mov     r2, r0
>> +             ldr     r1, =0xffffffff         @ DTB machine type
>> +             mov     r0, #0  @ r0 is 0
>> +
>> +             @ Branch to (possibly) relocated zImage entry that is in r3
>> +             bx      r3
>> +
>> +efi_load_fail:
>> +     @ We need to exit THUMB mode here, to return to EFI firmware.
>
> If you lose these 4 lines:
>
>> + THUMB(              adr     r12, BSYM(1f)   )
>> + THUMB(              bx      r12             )
>> +1:
>> +             .arm
>
> ...
>
>> +             @ Return EFI_LOAD_ERROR to EFI firmware on error.
>> +             ldr     r0, =0x80000001
>
> and replace these
>
>> +             ldmfd   sp!, {fp, lr}
>> +             mov     pc, lr
>
> with:
>
>                 ldmfd   sp!, {fp, pc}
>
> then the Thumb-ness on return will be determined by whatever is loaded
> into pc.
>
> There's no special need to switch back to ARM before the final return,
> provided that "mov pc,<Rm>" is not used (that will never switch from
> Thumb to ARM).
>
>
> Cheers
> ---Dave
>
>> + THUMB(              .thumb                  )
>
> Then you can also remove the above line.
>
>> +#endif
>> +
>> +zimage_continue:
>>               mrs     r9, cpsr
>>  #ifdef CONFIG_ARM_VIRT_EXT
>>               bl      __hyp_stub_install      @ get into SVC mode, reversibly
>> @@ -167,7 +254,6 @@ not_angel:
>>                * by the linker here, but it should preserve r7, r8, and r9.
>>                */
>>
>> -             .text
>>
>>  #ifdef CONFIG_AUTO_ZRELADDR
>>               @ determine final kernel image address
>> --

Hi Dave,

  Thanks for your suggestions - they do make the code cleaner.  I
think I have addressed all your suggestions in the patch below.  In
addition, I noticed that I had moved the start of the .text section to
within the  #ifdef CONFIG_EFI_STUB, so I have fixed that.  Also, the
previous return to the EFI stub was broken in the THUMB case - it was
staying in thumb mode - this is now fixed with a cleaner return as you
suggested.  Updated patch for head.S below.  I'll send out an updated
patch series in a few days after I get more feedback.

Thanks,
Roy

diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
index 75189f1..491e752 100644
--- a/arch/arm/boot/compressed/head.S
+++ b/arch/arm/boot/compressed/head.S
@@ -120,21 +120,100 @@
  */
  .align
  .arm @ Always enter in ARM state
+ .text
 start:
  .type start,#function
- .rept 7
+#ifdef CONFIG_EFI_STUB
+ @ Magic MSDOS signature for PE/COFF + ADD opcode
+ .word 0x62805a4d
+#else
+ mov r0, r0
+#endif
+ .rept 5
  mov r0, r0
  .endr
-   ARM( mov r0, r0 )
-   ARM( b 1f )
- THUMB( adr r12, BSYM(1f) )
- THUMB( bx r12 )
+
+ @ zimage_continue will be in ARM or thumb mode as configured
+ THUMB( adrl r12, BSYM(zimage_continue))
+ ARM( adrl r12, zimage_continue)
+ bx r12
+ THUMB( .thumb )

  .word 0x016f2818 @ Magic numbers to help the loader
  .word start @ absolute load/run zImage address
  .word _edata @ zImage end address
+
+#ifdef CONFIG_EFI_STUB
+ @ Portions of the MSDOS file header must be at offset
+ @ 0x3c from the start of the file.  All PE/COFF headers
+ @ are kept contiguous for simplicity.
+#include "efi-header.S"
+
+efi_stub_entry:
+ @ The EFI stub entry point is not at a fixed address, however
+ @ this address must be set in the PE/COFF header.
+ @ EFI entry point is in A32 mode, switch to T32 if configured.
+ THUMB( .arm )
+ THUMB( adr r12, BSYM(1f) )
+ THUMB( bx r12 )
  THUMB( .thumb )
 1:
+ @ Save lr on stack for possible return to EFI firmware.
+ @ Don't care about fp, but need 64 bit alignment....
+ stmfd sp!, {fp, lr}
+
+ @ Save args to EFI app across got fixup call
+ stmfd sp!, {r0, r1}
+ ldmfd sp!, {r0, r1}
+
+ @ allocate space on stack for return of new entry point of
+ @ zImage, as EFI stub may copy the kernel.  Pass address
+ @ of space in r2 - EFI stub will fill in the pointer.
+
+ sub sp, #8 @ we only need 4 bytes,
+ @ but keep stack 8 byte aligned.
+ mov r2, sp
+ @ Pass our actual runtime start address in pointer data
+ adr r11, LC0 @ address of LC0 at run time
+ ldr r12, [r11, #0] @ address of LC0 at link time
+
+ sub r3, r11, r12 @ calculate the delta offset
+ str r3, [r2, #0]
+ bl efi_entry
+
+ @ get new zImage entry address from stack, put into r3
+ ldr r3, [sp, #0]
+ add sp, #8  @ restore stack
+
+ @ Check for error return from EFI stub (0xFFFFFFFF)
+ ldr r1, =0xffffffff
+ cmp r0, r1
+ beq efi_load_fail
+
+
+ @ Save return values of efi_entry
+ stmfd sp!, {r0, r3}
+ bl cache_clean_flush
+ bl cache_off
+ ldmfd   sp!, {r0, r3}
+
+ @ put DTB address in r2, it was returned by EFI entry
+ mov r2, r0
+ ldr r1, =0xffffffff @ DTB machine type
+ mov r0, #0  @ r0 is 0
+
+ @ Branch to (possibly) relocated zImage entry that is in r3
+ bx r3
+
+efi_load_fail:
+ @ Return EFI_LOAD_ERROR to EFI firmware on error.
+ @ Switch back to ARM mode for EFI is done based on
+ @ return address on stack
+ ldr r0, =0x80000001
+ ldmfd sp!, {fp, pc}
+#endif
+
+zimage_continue:
  mrs r9, cpsr
 #ifdef CONFIG_ARM_VIRT_EXT
  bl __hyp_stub_install @ get into SVC mode, reversibly
@@ -167,7 +246,6 @@ not_angel:
  * by the linker here, but it should preserve r7, r8, and r9.
  */

- .text

 #ifdef CONFIG_AUTO_ZRELADDR
  @ determine final kernel image address

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* Re: [PATCH 6/7] Add EFI stub for ARM
@ 2013-08-06  3:35       ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-06  3:35 UTC (permalink / raw)
  To: Dave Martin
  Cc: linux-efi, Russell King - ARM Linux, linux-kernel, Leif Lindholm,
	matt.fleming, linux-arm-kernel

>> diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
>> index 75189f1..4c70b9e 100644
>> --- a/arch/arm/boot/compressed/head.S
>> +++ b/arch/arm/boot/compressed/head.S
>> @@ -122,19 +122,106 @@
>>               .arm                            @ Always enter in ARM state
>>  start:
>>               .type   start,#function
>> -             .rept   7
>> +#ifdef CONFIG_EFI_STUB
>> +             @ Magic MSDOS signature for PE/COFF + ADD opcode
>> +             .word   0x62805a4d
>
> What about BE32?
>
> In that case, the instruction is a coprocessor load, that loads from a
> random address to a coprocessor that almost certainly doesn't exist.
> This will probably fault.
>
> Since BE32 is only for older platforms (<v6) and this is not easily
> solvable, it might be sensible to make the EFI stub support depend on
> !CPU_ENDIAN_BE32.
>
>> +#else
>> +             mov     r0, r0
>> +#endif
>> +             .rept   5
>
> You reduced the .rept count by 2, but only inserted one extra word,
> perhaps because of the extra, but buggy, insertion below.
>
>>               mov     r0, r0
>>               .endr
>>     ARM(              mov     r0, r0          )
>>     ARM(              b       1f              )
>>   THUMB(              adr     r12, BSYM(1f)   )
>>   THUMB(              bx      r12             )
>
> Can't you just replace 1f with zimage_continue directly in the above
> lines, instead of the subsequent extra branch:
>
>> + THUMB(              .thumb                  )
>> +1:
>> +             b       zimage_continue
>
> This also avoids having two labels both called '1'.
>
> I believe the magic word is expected to be in a predictable offset,
> but the size of the preceding branch is unpredictable in Thumb
> (you could use b.w, or possibly remove the branch altogether, as
> explained above).
>
>>               .word   0x016f2818              @ Magic numbers to help the loader
>>               .word   start                   @ absolute load/run zImage address
>>               .word   _edata                  @ zImage end address
>> +
>> +#ifdef CONFIG_EFI_STUB
>> +             @ Portions of the MSDOS file header must be at offset
>> +             @ 0x3c from the start of the file.  All PE/COFF headers
>> +             @ are kept contiguous for simplicity.
>> +#include "efi-header.S"
>> +
>> +efi_stub_entry:
>> +             .text
>> +             @ The EFI stub entry point is not at a fixed address, however
>> +             @ this address must be set in the PE/COFF header.
>> +             @ EFI entry point is in A32 mode, switch to T32 if configured.
>> +             .arm
>> +   ARM(              mov     r0, r0          )
>> +   ARM(              b       1f              )
>
> Those above two instructions are effectively just no-op padding.  Do you
> need them at all?
>
>> + THUMB(              adr     r12, BSYM(1f)   )
>> + THUMB(              bx      r12             )
>>   THUMB(              .thumb                  )
>>  1:
>> +             @ Save lr on stack for possible return to EFI firmware.
>> +             @ Don't care about fp, but need 64 bit alignment....
>> +             stmfd   sp!, {fp, lr}
>> +
>> +             @ Save args to EFI app across got fixup call
>> +             stmfd   sp!, {r0, r1}
>> +             ldmfd   sp!, {r0, r1}
>> +
>> +             @ allocate space on stack for return of new entry point of
>> +             @ zImage, as EFI stub may copy the kernel.  Pass address
>> +             @ of space in r2 - EFI stub will fill in the pointer.
>> +
>> +             sub     sp, #8                  @ we only need 4 bytes,
>> +                                             @ but keep stack 8 byte aligned.
>> +             mov     r2, sp
>> +             @ Pass our actual runtime start address in pointer data
>> +             adr     r11, LC0                @ address of LC0 at run time
>> +             ldr     r12, [r11, #0]          @ address of LC0 at link time
>> +
>> +             sub     r3, r11, r12            @ calculate the delta offset
>> +             str     r3, [r2, #0]
>> +             bl      efi_entry
>> +
>> +             @ get new zImage entry address from stack, put into r3
>> +             ldr     r3, [sp, #0]
>> +             add     sp, #8  @ restore stack
>> +
>> +             @ Check for error return from EFI stub (0xFFFFFFFF)
>> +             ldr     r1, =0xffffffff
>> +             cmp     r0, r1
>> +             beq     efi_load_fail
>> +
>> +
>> +             @ Save return values of efi_entry
>> +             stmfd   sp!, {r0, r3}
>> +             bl      cache_clean_flush
>> +             bl      cache_off
>> +             ldmfd   sp!, {r0, r3}
>> +
>> +             @ put DTB address in r2, it was returned by EFI entry
>> +             mov     r2, r0
>> +             ldr     r1, =0xffffffff         @ DTB machine type
>> +             mov     r0, #0  @ r0 is 0
>> +
>> +             @ Branch to (possibly) relocated zImage entry that is in r3
>> +             bx      r3
>> +
>> +efi_load_fail:
>> +     @ We need to exit THUMB mode here, to return to EFI firmware.
>
> If you lose these 4 lines:
>
>> + THUMB(              adr     r12, BSYM(1f)   )
>> + THUMB(              bx      r12             )
>> +1:
>> +             .arm
>
> ...
>
>> +             @ Return EFI_LOAD_ERROR to EFI firmware on error.
>> +             ldr     r0, =0x80000001
>
> and replace these
>
>> +             ldmfd   sp!, {fp, lr}
>> +             mov     pc, lr
>
> with:
>
>                 ldmfd   sp!, {fp, pc}
>
> then the Thumb-ness on return will be determined by whatever is loaded
> into pc.
>
> There's no special need to switch back to ARM before the final return,
> provided that "mov pc,<Rm>" is not used (that will never switch from
> Thumb to ARM).
>
>
> Cheers
> ---Dave
>
>> + THUMB(              .thumb                  )
>
> Then you can also remove the above line.
>
>> +#endif
>> +
>> +zimage_continue:
>>               mrs     r9, cpsr
>>  #ifdef CONFIG_ARM_VIRT_EXT
>>               bl      __hyp_stub_install      @ get into SVC mode, reversibly
>> @@ -167,7 +254,6 @@ not_angel:
>>                * by the linker here, but it should preserve r7, r8, and r9.
>>                */
>>
>> -             .text
>>
>>  #ifdef CONFIG_AUTO_ZRELADDR
>>               @ determine final kernel image address
>> --

Hi Dave,

  Thanks for your suggestions - they do make the code cleaner.  I
think I have addressed all your suggestions in the patch below.  In
addition, I noticed that I had moved the start of the .text section to
within the  #ifdef CONFIG_EFI_STUB, so I have fixed that.  Also, the
previous return to the EFI stub was broken in the THUMB case - it was
staying in thumb mode - this is now fixed with a cleaner return as you
suggested.  Updated patch for head.S below.  I'll send out an updated
patch series in a few days after I get more feedback.

Thanks,
Roy

diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
index 75189f1..491e752 100644
--- a/arch/arm/boot/compressed/head.S
+++ b/arch/arm/boot/compressed/head.S
@@ -120,21 +120,100 @@
  */
  .align
  .arm @ Always enter in ARM state
+ .text
 start:
  .type start,#function
- .rept 7
+#ifdef CONFIG_EFI_STUB
+ @ Magic MSDOS signature for PE/COFF + ADD opcode
+ .word 0x62805a4d
+#else
+ mov r0, r0
+#endif
+ .rept 5
  mov r0, r0
  .endr
-   ARM( mov r0, r0 )
-   ARM( b 1f )
- THUMB( adr r12, BSYM(1f) )
- THUMB( bx r12 )
+
+ @ zimage_continue will be in ARM or thumb mode as configured
+ THUMB( adrl r12, BSYM(zimage_continue))
+ ARM( adrl r12, zimage_continue)
+ bx r12
+ THUMB( .thumb )

  .word 0x016f2818 @ Magic numbers to help the loader
  .word start @ absolute load/run zImage address
  .word _edata @ zImage end address
+
+#ifdef CONFIG_EFI_STUB
+ @ Portions of the MSDOS file header must be at offset
+ @ 0x3c from the start of the file.  All PE/COFF headers
+ @ are kept contiguous for simplicity.
+#include "efi-header.S"
+
+efi_stub_entry:
+ @ The EFI stub entry point is not at a fixed address, however
+ @ this address must be set in the PE/COFF header.
+ @ EFI entry point is in A32 mode, switch to T32 if configured.
+ THUMB( .arm )
+ THUMB( adr r12, BSYM(1f) )
+ THUMB( bx r12 )
  THUMB( .thumb )
 1:
+ @ Save lr on stack for possible return to EFI firmware.
+ @ Don't care about fp, but need 64 bit alignment....
+ stmfd sp!, {fp, lr}
+
+ @ Save args to EFI app across got fixup call
+ stmfd sp!, {r0, r1}
+ ldmfd sp!, {r0, r1}
+
+ @ allocate space on stack for return of new entry point of
+ @ zImage, as EFI stub may copy the kernel.  Pass address
+ @ of space in r2 - EFI stub will fill in the pointer.
+
+ sub sp, #8 @ we only need 4 bytes,
+ @ but keep stack 8 byte aligned.
+ mov r2, sp
+ @ Pass our actual runtime start address in pointer data
+ adr r11, LC0 @ address of LC0 at run time
+ ldr r12, [r11, #0] @ address of LC0 at link time
+
+ sub r3, r11, r12 @ calculate the delta offset
+ str r3, [r2, #0]
+ bl efi_entry
+
+ @ get new zImage entry address from stack, put into r3
+ ldr r3, [sp, #0]
+ add sp, #8  @ restore stack
+
+ @ Check for error return from EFI stub (0xFFFFFFFF)
+ ldr r1, =0xffffffff
+ cmp r0, r1
+ beq efi_load_fail
+
+
+ @ Save return values of efi_entry
+ stmfd sp!, {r0, r3}
+ bl cache_clean_flush
+ bl cache_off
+ ldmfd   sp!, {r0, r3}
+
+ @ put DTB address in r2, it was returned by EFI entry
+ mov r2, r0
+ ldr r1, =0xffffffff @ DTB machine type
+ mov r0, #0  @ r0 is 0
+
+ @ Branch to (possibly) relocated zImage entry that is in r3
+ bx r3
+
+efi_load_fail:
+ @ Return EFI_LOAD_ERROR to EFI firmware on error.
+ @ Switch back to ARM mode for EFI is done based on
+ @ return address on stack
+ ldr r0, =0x80000001
+ ldmfd sp!, {fp, pc}
+#endif
+
+zimage_continue:
  mrs r9, cpsr
 #ifdef CONFIG_ARM_VIRT_EXT
  bl __hyp_stub_install @ get into SVC mode, reversibly
@@ -167,7 +246,6 @@ not_angel:
  * by the linker here, but it should preserve r7, r8, and r9.
  */

- .text

 #ifdef CONFIG_AUTO_ZRELADDR
  @ determine final kernel image address

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* [PATCH 6/7] Add EFI stub for ARM
@ 2013-08-06  3:35       ` Roy Franz
  0 siblings, 0 replies; 54+ messages in thread
From: Roy Franz @ 2013-08-06  3:35 UTC (permalink / raw)
  To: linux-arm-kernel

>> diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
>> index 75189f1..4c70b9e 100644
>> --- a/arch/arm/boot/compressed/head.S
>> +++ b/arch/arm/boot/compressed/head.S
>> @@ -122,19 +122,106 @@
>>               .arm                            @ Always enter in ARM state
>>  start:
>>               .type   start,#function
>> -             .rept   7
>> +#ifdef CONFIG_EFI_STUB
>> +             @ Magic MSDOS signature for PE/COFF + ADD opcode
>> +             .word   0x62805a4d
>
> What about BE32?
>
> In that case, the instruction is a coprocessor load, that loads from a
> random address to a coprocessor that almost certainly doesn't exist.
> This will probably fault.
>
> Since BE32 is only for older platforms (<v6) and this is not easily
> solvable, it might be sensible to make the EFI stub support depend on
> !CPU_ENDIAN_BE32.
>
>> +#else
>> +             mov     r0, r0
>> +#endif
>> +             .rept   5
>
> You reduced the .rept count by 2, but only inserted one extra word,
> perhaps because of the extra, but buggy, insertion below.
>
>>               mov     r0, r0
>>               .endr
>>     ARM(              mov     r0, r0          )
>>     ARM(              b       1f              )
>>   THUMB(              adr     r12, BSYM(1f)   )
>>   THUMB(              bx      r12             )
>
> Can't you just replace 1f with zimage_continue directly in the above
> lines, instead of the subsequent extra branch:
>
>> + THUMB(              .thumb                  )
>> +1:
>> +             b       zimage_continue
>
> This also avoids having two labels both called '1'.
>
> I believe the magic word is expected to be in a predictable offset,
> but the size of the preceding branch is unpredictable in Thumb
> (you could use b.w, or possibly remove the branch altogether, as
> explained above).
>
>>               .word   0x016f2818              @ Magic numbers to help the loader
>>               .word   start                   @ absolute load/run zImage address
>>               .word   _edata                  @ zImage end address
>> +
>> +#ifdef CONFIG_EFI_STUB
>> +             @ Portions of the MSDOS file header must be at offset
>> +             @ 0x3c from the start of the file.  All PE/COFF headers
>> +             @ are kept contiguous for simplicity.
>> +#include "efi-header.S"
>> +
>> +efi_stub_entry:
>> +             .text
>> +             @ The EFI stub entry point is not at a fixed address, however
>> +             @ this address must be set in the PE/COFF header.
>> +             @ EFI entry point is in A32 mode, switch to T32 if configured.
>> +             .arm
>> +   ARM(              mov     r0, r0          )
>> +   ARM(              b       1f              )
>
> Those above two instructions are effectively just no-op padding.  Do you
> need them at all?
>
>> + THUMB(              adr     r12, BSYM(1f)   )
>> + THUMB(              bx      r12             )
>>   THUMB(              .thumb                  )
>>  1:
>> +             @ Save lr on stack for possible return to EFI firmware.
>> +             @ Don't care about fp, but need 64 bit alignment....
>> +             stmfd   sp!, {fp, lr}
>> +
>> +             @ Save args to EFI app across got fixup call
>> +             stmfd   sp!, {r0, r1}
>> +             ldmfd   sp!, {r0, r1}
>> +
>> +             @ allocate space on stack for return of new entry point of
>> +             @ zImage, as EFI stub may copy the kernel.  Pass address
>> +             @ of space in r2 - EFI stub will fill in the pointer.
>> +
>> +             sub     sp, #8                  @ we only need 4 bytes,
>> +                                             @ but keep stack 8 byte aligned.
>> +             mov     r2, sp
>> +             @ Pass our actual runtime start address in pointer data
>> +             adr     r11, LC0                @ address of LC0 at run time
>> +             ldr     r12, [r11, #0]          @ address of LC0 at link time
>> +
>> +             sub     r3, r11, r12            @ calculate the delta offset
>> +             str     r3, [r2, #0]
>> +             bl      efi_entry
>> +
>> +             @ get new zImage entry address from stack, put into r3
>> +             ldr     r3, [sp, #0]
>> +             add     sp, #8  @ restore stack
>> +
>> +             @ Check for error return from EFI stub (0xFFFFFFFF)
>> +             ldr     r1, =0xffffffff
>> +             cmp     r0, r1
>> +             beq     efi_load_fail
>> +
>> +
>> +             @ Save return values of efi_entry
>> +             stmfd   sp!, {r0, r3}
>> +             bl      cache_clean_flush
>> +             bl      cache_off
>> +             ldmfd   sp!, {r0, r3}
>> +
>> +             @ put DTB address in r2, it was returned by EFI entry
>> +             mov     r2, r0
>> +             ldr     r1, =0xffffffff         @ DTB machine type
>> +             mov     r0, #0  @ r0 is 0
>> +
>> +             @ Branch to (possibly) relocated zImage entry that is in r3
>> +             bx      r3
>> +
>> +efi_load_fail:
>> +     @ We need to exit THUMB mode here, to return to EFI firmware.
>
> If you lose these 4 lines:
>
>> + THUMB(              adr     r12, BSYM(1f)   )
>> + THUMB(              bx      r12             )
>> +1:
>> +             .arm
>
> ...
>
>> +             @ Return EFI_LOAD_ERROR to EFI firmware on error.
>> +             ldr     r0, =0x80000001
>
> and replace these
>
>> +             ldmfd   sp!, {fp, lr}
>> +             mov     pc, lr
>
> with:
>
>                 ldmfd   sp!, {fp, pc}
>
> then the Thumb-ness on return will be determined by whatever is loaded
> into pc.
>
> There's no special need to switch back to ARM before the final return,
> provided that "mov pc,<Rm>" is not used (that will never switch from
> Thumb to ARM).
>
>
> Cheers
> ---Dave
>
>> + THUMB(              .thumb                  )
>
> Then you can also remove the above line.
>
>> +#endif
>> +
>> +zimage_continue:
>>               mrs     r9, cpsr
>>  #ifdef CONFIG_ARM_VIRT_EXT
>>               bl      __hyp_stub_install      @ get into SVC mode, reversibly
>> @@ -167,7 +254,6 @@ not_angel:
>>                * by the linker here, but it should preserve r7, r8, and r9.
>>                */
>>
>> -             .text
>>
>>  #ifdef CONFIG_AUTO_ZRELADDR
>>               @ determine final kernel image address
>> --

Hi Dave,

  Thanks for your suggestions - they do make the code cleaner.  I
think I have addressed all your suggestions in the patch below.  In
addition, I noticed that I had moved the start of the .text section to
within the  #ifdef CONFIG_EFI_STUB, so I have fixed that.  Also, the
previous return to the EFI stub was broken in the THUMB case - it was
staying in thumb mode - this is now fixed with a cleaner return as you
suggested.  Updated patch for head.S below.  I'll send out an updated
patch series in a few days after I get more feedback.

Thanks,
Roy

diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
index 75189f1..491e752 100644
--- a/arch/arm/boot/compressed/head.S
+++ b/arch/arm/boot/compressed/head.S
@@ -120,21 +120,100 @@
  */
  .align
  .arm @ Always enter in ARM state
+ .text
 start:
  .type start,#function
- .rept 7
+#ifdef CONFIG_EFI_STUB
+ @ Magic MSDOS signature for PE/COFF + ADD opcode
+ .word 0x62805a4d
+#else
+ mov r0, r0
+#endif
+ .rept 5
  mov r0, r0
  .endr
-   ARM( mov r0, r0 )
-   ARM( b 1f )
- THUMB( adr r12, BSYM(1f) )
- THUMB( bx r12 )
+
+ @ zimage_continue will be in ARM or thumb mode as configured
+ THUMB( adrl r12, BSYM(zimage_continue))
+ ARM( adrl r12, zimage_continue)
+ bx r12
+ THUMB( .thumb )

  .word 0x016f2818 @ Magic numbers to help the loader
  .word start @ absolute load/run zImage address
  .word _edata @ zImage end address
+
+#ifdef CONFIG_EFI_STUB
+ @ Portions of the MSDOS file header must be at offset
+ @ 0x3c from the start of the file.  All PE/COFF headers
+ @ are kept contiguous for simplicity.
+#include "efi-header.S"
+
+efi_stub_entry:
+ @ The EFI stub entry point is not at a fixed address, however
+ @ this address must be set in the PE/COFF header.
+ @ EFI entry point is in A32 mode, switch to T32 if configured.
+ THUMB( .arm )
+ THUMB( adr r12, BSYM(1f) )
+ THUMB( bx r12 )
  THUMB( .thumb )
 1:
+ @ Save lr on stack for possible return to EFI firmware.
+ @ Don't care about fp, but need 64 bit alignment....
+ stmfd sp!, {fp, lr}
+
+ @ Save args to EFI app across got fixup call
+ stmfd sp!, {r0, r1}
+ ldmfd sp!, {r0, r1}
+
+ @ allocate space on stack for return of new entry point of
+ @ zImage, as EFI stub may copy the kernel.  Pass address
+ @ of space in r2 - EFI stub will fill in the pointer.
+
+ sub sp, #8 @ we only need 4 bytes,
+ @ but keep stack 8 byte aligned.
+ mov r2, sp
+ @ Pass our actual runtime start address in pointer data
+ adr r11, LC0 @ address of LC0 at run time
+ ldr r12, [r11, #0] @ address of LC0 at link time
+
+ sub r3, r11, r12 @ calculate the delta offset
+ str r3, [r2, #0]
+ bl efi_entry
+
+ @ get new zImage entry address from stack, put into r3
+ ldr r3, [sp, #0]
+ add sp, #8  @ restore stack
+
+ @ Check for error return from EFI stub (0xFFFFFFFF)
+ ldr r1, =0xffffffff
+ cmp r0, r1
+ beq efi_load_fail
+
+
+ @ Save return values of efi_entry
+ stmfd sp!, {r0, r3}
+ bl cache_clean_flush
+ bl cache_off
+ ldmfd   sp!, {r0, r3}
+
+ @ put DTB address in r2, it was returned by EFI entry
+ mov r2, r0
+ ldr r1, =0xffffffff @ DTB machine type
+ mov r0, #0  @ r0 is 0
+
+ @ Branch to (possibly) relocated zImage entry that is in r3
+ bx r3
+
+efi_load_fail:
+ @ Return EFI_LOAD_ERROR to EFI firmware on error.
+ @ Switch back to ARM mode for EFI is done based on
+ @ return address on stack
+ ldr r0, =0x80000001
+ ldmfd sp!, {fp, pc}
+#endif
+
+zimage_continue:
  mrs r9, cpsr
 #ifdef CONFIG_ARM_VIRT_EXT
  bl __hyp_stub_install @ get into SVC mode, reversibly
@@ -167,7 +246,6 @@ not_angel:
  * by the linker here, but it should preserve r7, r8, and r9.
  */

- .text

 #ifdef CONFIG_AUTO_ZRELADDR
  @ determine final kernel image address

^ permalink raw reply related	[flat|nested] 54+ messages in thread

* Re: [PATCH 1/7] EFI stub documentation updates
@ 2013-08-06 10:30         ` Dave P Martin
  0 siblings, 0 replies; 54+ messages in thread
From: Dave P Martin @ 2013-08-06 10:30 UTC (permalink / raw)
  To: Roy Franz
  Cc: linux-kernel, linux-efi, linux-arm-kernel, matt.fleming,
	Russell King - ARM Linux, Leif Lindholm

On Tue, Aug 06, 2013 at 12:56:12AM +0100, Roy Franz wrote:
> On Mon, Aug 5, 2013 at 7:12 AM, Dave Martin <Dave.Martin@arm.com> wrote:
> > On Fri, Aug 02, 2013 at 02:29:02PM -0700, Roy Franz wrote:
> >> The ARM kernel also has an EFI stub which works largely the same way
> >> as the x86 stub, so move the documentation out of x86 directory and
> >> update to reflect that it is generic, and add ARM specific text.
> >>
> >> Signed-off-by: Roy Franz <roy.franz@linaro.org>
> >> ---
> >>  Documentation/efi-stub.txt     |   78 ++++++++++++++++++++++++++++++++++++++++
> >>  Documentation/x86/efi-stub.txt |   65 ---------------------------------
> >>  arch/x86/Kconfig               |    2 +-
> >>  3 files changed, 79 insertions(+), 66 deletions(-)
> >>  create mode 100644 Documentation/efi-stub.txt
> >>  delete mode 100644 Documentation/x86/efi-stub.txt
> >>
> >> diff --git a/Documentation/efi-stub.txt b/Documentation/efi-stub.txt
> >> new file mode 100644
> >> index 0000000..7837df1
> >> --- /dev/null
> >> +++ b/Documentation/efi-stub.txt
> >> @@ -0,0 +1,78 @@
> >> +                       The EFI Boot Stub
> >> +                  ---------------------------
> >> +
> >> +On the x86 and ARM platforms, a bzImage can masquerade as a PE/COFF image,
> >> +thereby convincing EFI firmware loaders to load it as an EFI
> >> +executable. The code that modifies the bzImage header, along with the
> >
> >
> > Minor nit, I don't think there is such a thing as "bzImage" for ARM.
> >
> > Cheers
> > ---Dave
> >
> Yeah, I don't thinks so either...  How about:
> 
> On the x86 and ARM platforms, a kernel zImage/bzImage can masquerade....

Souds fine to me

Cheers
---Dave

^ permalink raw reply	[flat|nested] 54+ messages in thread

* Re: [PATCH 1/7] EFI stub documentation updates
@ 2013-08-06 10:30         ` Dave P Martin
  0 siblings, 0 replies; 54+ messages in thread
From: Dave P Martin @ 2013-08-06 10:30 UTC (permalink / raw)
  To: Roy Franz
  Cc: linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-efi-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matt.fleming-ral2JQCrhuEAvxtiuMwx3w, Russell King - ARM Linux,
	Leif Lindholm

On Tue, Aug 06, 2013 at 12:56:12AM +0100, Roy Franz wrote:
> On Mon, Aug 5, 2013 at 7:12 AM, Dave Martin <Dave.Martin-5wv7dgnIgG8@public.gmane.org> wrote:
> > On Fri, Aug 02, 2013 at 02:29:02PM -0700, Roy Franz wrote:
> >> The ARM kernel also has an EFI stub which works largely the same way
> >> as the x86 stub, so move the documentation out of x86 directory and
> >> update to reflect that it is generic, and add ARM specific text.
> >>
> >> Signed-off-by: Roy Franz <roy.franz-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
> >> ---
> >>  Documentation/efi-stub.txt     |   78 ++++++++++++++++++++++++++++++++++++++++
> >>  Documentation/x86/efi-stub.txt |   65 ---------------------------------
> >>  arch/x86/Kconfig               |    2 +-
> >>  3 files changed, 79 insertions(+), 66 deletions(-)
> >>  create mode 100644 Documentation/efi-stub.txt
> >>  delete mode 100644 Documentation/x86/efi-stub.txt
> >>
> >> diff --git a/Documentation/efi-stub.txt b/Documentation/efi-stub.txt
> >> new file mode 100644
> >> index 0000000..7837df1
> >> --- /dev/null
> >> +++ b/Documentation/efi-stub.txt
> >> @@ -0,0 +1,78 @@
> >> +                       The EFI Boot Stub
> >> +                  ---------------------------
> >> +
> >> +On the x86 and ARM platforms, a bzImage can masquerade as a PE/COFF image,
> >> +thereby convincing EFI firmware loaders to load it as an EFI
> >> +executable. The code that modifies the bzImage header, along with the
> >
> >
> > Minor nit, I don't think there is such a thing as "bzImage" for ARM.
> >
> > Cheers
> > ---Dave
> >
> Yeah, I don't thinks so either...  How about:
> 
> On the x86 and ARM platforms, a kernel zImage/bzImage can masquerade....

Souds fine to me

Cheers
---Dave

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH 1/7] EFI stub documentation updates
@ 2013-08-06 10:30         ` Dave P Martin
  0 siblings, 0 replies; 54+ messages in thread
From: Dave P Martin @ 2013-08-06 10:30 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, Aug 06, 2013 at 12:56:12AM +0100, Roy Franz wrote:
> On Mon, Aug 5, 2013 at 7:12 AM, Dave Martin <Dave.Martin@arm.com> wrote:
> > On Fri, Aug 02, 2013 at 02:29:02PM -0700, Roy Franz wrote:
> >> The ARM kernel also has an EFI stub which works largely the same way
> >> as the x86 stub, so move the documentation out of x86 directory and
> >> update to reflect that it is generic, and add ARM specific text.
> >>
> >> Signed-off-by: Roy Franz <roy.franz@linaro.org>
> >> ---
> >>  Documentation/efi-stub.txt     |   78 ++++++++++++++++++++++++++++++++++++++++
> >>  Documentation/x86/efi-stub.txt |   65 ---------------------------------
> >>  arch/x86/Kconfig               |    2 +-
> >>  3 files changed, 79 insertions(+), 66 deletions(-)
> >>  create mode 100644 Documentation/efi-stub.txt
> >>  delete mode 100644 Documentation/x86/efi-stub.txt
> >>
> >> diff --git a/Documentation/efi-stub.txt b/Documentation/efi-stub.txt
> >> new file mode 100644
> >> index 0000000..7837df1
> >> --- /dev/null
> >> +++ b/Documentation/efi-stub.txt
> >> @@ -0,0 +1,78 @@
> >> +                       The EFI Boot Stub
> >> +                  ---------------------------
> >> +
> >> +On the x86 and ARM platforms, a bzImage can masquerade as a PE/COFF image,
> >> +thereby convincing EFI firmware loaders to load it as an EFI
> >> +executable. The code that modifies the bzImage header, along with the
> >
> >
> > Minor nit, I don't think there is such a thing as "bzImage" for ARM.
> >
> > Cheers
> > ---Dave
> >
> Yeah, I don't thinks so either...  How about:
> 
> On the x86 and ARM platforms, a kernel zImage/bzImage can masquerade....

Souds fine to me

Cheers
---Dave

^ permalink raw reply	[flat|nested] 54+ messages in thread

* Re: [PATCH 6/7] Add EFI stub for ARM
@ 2013-08-06 10:31         ` Dave P Martin
  0 siblings, 0 replies; 54+ messages in thread
From: Dave P Martin @ 2013-08-06 10:31 UTC (permalink / raw)
  To: Leif Lindholm
  Cc: Roy Franz, linux-kernel, linux-efi, linux-arm-kernel,
	matt.fleming, linux

On Mon, Aug 05, 2013 at 04:33:19PM +0100, Leif Lindholm wrote:
> On Mon, Aug 05, 2013 at 03:11:49PM +0100, Dave Martin wrote:
> > > diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
> > > index 75189f1..4c70b9e 100644
> > > --- a/arch/arm/boot/compressed/head.S
> > > +++ b/arch/arm/boot/compressed/head.S
> > > @@ -122,19 +122,106 @@
> > >  		.arm				@ Always enter in ARM state
> > >  start:
> > >  		.type	start,#function
> > > -		.rept	7
> > > +#ifdef CONFIG_EFI_STUB
> > > +		@ Magic MSDOS signature for PE/COFF + ADD opcode
> > > +		.word	0x62805a4d
> > 
> > What about BE32?
>  
> The ARM bindings for UEFI specify that the processor must be in
> little-endian mode.
> 
> > In that case, the instruction is a coprocessor load, that loads from a
> > random address to a coprocessor that almost certainly doesn't exist.
> > This will probably fault.
> > 
> > Since BE32 is only for older platforms (<v6) and this is not easily
> > solvable, it might be sensible to make the EFI stub support depend on
> > !CPU_ENDIAN_BE32.
>  
> Well, it would make more sense to make EFI_STUB depend on EFI and
> EFI depend on !CPU_ENDIAN_BE32. Which is something I can add to
> my next set of general ARM UEFI patches. Thanks.

That sounds fair enough.

Cheers
---Dave

^ permalink raw reply	[flat|nested] 54+ messages in thread

* Re: [PATCH 6/7] Add EFI stub for ARM
@ 2013-08-06 10:31         ` Dave P Martin
  0 siblings, 0 replies; 54+ messages in thread
From: Dave P Martin @ 2013-08-06 10:31 UTC (permalink / raw)
  To: Leif Lindholm
  Cc: Roy Franz, linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-efi-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matt.fleming-ral2JQCrhuEAvxtiuMwx3w,
	linux-lFZ/pmaqli7XmaaqVzeoHQ

On Mon, Aug 05, 2013 at 04:33:19PM +0100, Leif Lindholm wrote:
> On Mon, Aug 05, 2013 at 03:11:49PM +0100, Dave Martin wrote:
> > > diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
> > > index 75189f1..4c70b9e 100644
> > > --- a/arch/arm/boot/compressed/head.S
> > > +++ b/arch/arm/boot/compressed/head.S
> > > @@ -122,19 +122,106 @@
> > >  		.arm				@ Always enter in ARM state
> > >  start:
> > >  		.type	start,#function
> > > -		.rept	7
> > > +#ifdef CONFIG_EFI_STUB
> > > +		@ Magic MSDOS signature for PE/COFF + ADD opcode
> > > +		.word	0x62805a4d
> > 
> > What about BE32?
>  
> The ARM bindings for UEFI specify that the processor must be in
> little-endian mode.
> 
> > In that case, the instruction is a coprocessor load, that loads from a
> > random address to a coprocessor that almost certainly doesn't exist.
> > This will probably fault.
> > 
> > Since BE32 is only for older platforms (<v6) and this is not easily
> > solvable, it might be sensible to make the EFI stub support depend on
> > !CPU_ENDIAN_BE32.
>  
> Well, it would make more sense to make EFI_STUB depend on EFI and
> EFI depend on !CPU_ENDIAN_BE32. Which is something I can add to
> my next set of general ARM UEFI patches. Thanks.

That sounds fair enough.

Cheers
---Dave

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH 6/7] Add EFI stub for ARM
@ 2013-08-06 10:31         ` Dave P Martin
  0 siblings, 0 replies; 54+ messages in thread
From: Dave P Martin @ 2013-08-06 10:31 UTC (permalink / raw)
  To: linux-arm-kernel

On Mon, Aug 05, 2013 at 04:33:19PM +0100, Leif Lindholm wrote:
> On Mon, Aug 05, 2013 at 03:11:49PM +0100, Dave Martin wrote:
> > > diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
> > > index 75189f1..4c70b9e 100644
> > > --- a/arch/arm/boot/compressed/head.S
> > > +++ b/arch/arm/boot/compressed/head.S
> > > @@ -122,19 +122,106 @@
> > >  		.arm				@ Always enter in ARM state
> > >  start:
> > >  		.type	start,#function
> > > -		.rept	7
> > > +#ifdef CONFIG_EFI_STUB
> > > +		@ Magic MSDOS signature for PE/COFF + ADD opcode
> > > +		.word	0x62805a4d
> > 
> > What about BE32?
>  
> The ARM bindings for UEFI specify that the processor must be in
> little-endian mode.
> 
> > In that case, the instruction is a coprocessor load, that loads from a
> > random address to a coprocessor that almost certainly doesn't exist.
> > This will probably fault.
> > 
> > Since BE32 is only for older platforms (<v6) and this is not easily
> > solvable, it might be sensible to make the EFI stub support depend on
> > !CPU_ENDIAN_BE32.
>  
> Well, it would make more sense to make EFI_STUB depend on EFI and
> EFI depend on !CPU_ENDIAN_BE32. Which is something I can add to
> my next set of general ARM UEFI patches. Thanks.

That sounds fair enough.

Cheers
---Dave

^ permalink raw reply	[flat|nested] 54+ messages in thread

* Re: [PATCH 6/7] Add EFI stub for ARM
@ 2013-08-06 10:40           ` Dave P Martin
  0 siblings, 0 replies; 54+ messages in thread
From: Dave P Martin @ 2013-08-06 10:40 UTC (permalink / raw)
  To: Roy Franz
  Cc: Leif Lindholm, linux-kernel, linux-efi, linux-arm-kernel,
	matt.fleming, Russell King - ARM Linux

On Tue, Aug 06, 2013 at 01:06:17AM +0100, Roy Franz wrote:
> On Mon, Aug 5, 2013 at 8:33 AM, Leif Lindholm <leif.lindholm@linaro.org> wrote:
> > On Mon, Aug 05, 2013 at 03:11:49PM +0100, Dave Martin wrote:
> >> > diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
> >> > index 75189f1..4c70b9e 100644
> >> > --- a/arch/arm/boot/compressed/head.S
> >> > +++ b/arch/arm/boot/compressed/head.S
> >> > @@ -122,19 +122,106 @@
> >> >             .arm                            @ Always enter in ARM state
> >> >  start:
> >> >             .type   start,#function
> >> > -           .rept   7
> >> > +#ifdef CONFIG_EFI_STUB
> >> > +           @ Magic MSDOS signature for PE/COFF + ADD opcode
> >> > +           .word   0x62805a4d

Is BE8 supported?  If so, this would put the bytes

	62 80 5A 4D

in the binary, which is not the right magic.

For this magic, you could use .byte instead.


To help future maintainers, I suggest noting in a comment that
executing through this magic relies on little-endian instruction byte
order (so, LE or BE8), and the ARM instruction set.

What about the endianness of the other PE/COFF header fields?  Are they
always little-endian, or are some fields native-endian (and if so, how
is the endianness of the header determined by the loader)?

> >>
> >> What about BE32?
> >
> > The ARM bindings for UEFI specify that the processor must be in
> > little-endian mode.
> >
> >> In that case, the instruction is a coprocessor load, that loads from a
> >> random address to a coprocessor that almost certainly doesn't exist.
> >> This will probably fault.
> >>
> >> Since BE32 is only for older platforms (<v6) and this is not easily
> >> solvable, it might be sensible to make the EFI stub support depend on
> >> !CPU_ENDIAN_BE32.
> >
> > Well, it would make more sense to make EFI_STUB depend on EFI and
> > EFI depend on !CPU_ENDIAN_BE32. Which is something I can add to
> > my next set of general ARM UEFI patches. Thanks.
> > /
> >     Leif
> 
> I had EFI_STUB depend on EFI at one point during my development, but took
> it out because there was no actual dependency (the stub will work fine without
> other EFI features.)  The features will most likely be used together,
> but I wasn't
> sure if we would want to enforce this with a config dependency.  I
> don't care one
> way or the other, I'd just like the dependencies to be correct and
> follow best practices.

I guess it's up to you, so long as the constraint is expressed in Kconfig
somehow.

Cheers
---Dave

^ permalink raw reply	[flat|nested] 54+ messages in thread

* Re: [PATCH 6/7] Add EFI stub for ARM
@ 2013-08-06 10:40           ` Dave P Martin
  0 siblings, 0 replies; 54+ messages in thread
From: Dave P Martin @ 2013-08-06 10:40 UTC (permalink / raw)
  To: Roy Franz
  Cc: Leif Lindholm, linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-efi-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matt.fleming-ral2JQCrhuEAvxtiuMwx3w, Russell King - ARM Linux

On Tue, Aug 06, 2013 at 01:06:17AM +0100, Roy Franz wrote:
> On Mon, Aug 5, 2013 at 8:33 AM, Leif Lindholm <leif.lindholm-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org> wrote:
> > On Mon, Aug 05, 2013 at 03:11:49PM +0100, Dave Martin wrote:
> >> > diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
> >> > index 75189f1..4c70b9e 100644
> >> > --- a/arch/arm/boot/compressed/head.S
> >> > +++ b/arch/arm/boot/compressed/head.S
> >> > @@ -122,19 +122,106 @@
> >> >             .arm                            @ Always enter in ARM state
> >> >  start:
> >> >             .type   start,#function
> >> > -           .rept   7
> >> > +#ifdef CONFIG_EFI_STUB
> >> > +           @ Magic MSDOS signature for PE/COFF + ADD opcode
> >> > +           .word   0x62805a4d

Is BE8 supported?  If so, this would put the bytes

	62 80 5A 4D

in the binary, which is not the right magic.

For this magic, you could use .byte instead.


To help future maintainers, I suggest noting in a comment that
executing through this magic relies on little-endian instruction byte
order (so, LE or BE8), and the ARM instruction set.

What about the endianness of the other PE/COFF header fields?  Are they
always little-endian, or are some fields native-endian (and if so, how
is the endianness of the header determined by the loader)?

> >>
> >> What about BE32?
> >
> > The ARM bindings for UEFI specify that the processor must be in
> > little-endian mode.
> >
> >> In that case, the instruction is a coprocessor load, that loads from a
> >> random address to a coprocessor that almost certainly doesn't exist.
> >> This will probably fault.
> >>
> >> Since BE32 is only for older platforms (<v6) and this is not easily
> >> solvable, it might be sensible to make the EFI stub support depend on
> >> !CPU_ENDIAN_BE32.
> >
> > Well, it would make more sense to make EFI_STUB depend on EFI and
> > EFI depend on !CPU_ENDIAN_BE32. Which is something I can add to
> > my next set of general ARM UEFI patches. Thanks.
> > /
> >     Leif
> 
> I had EFI_STUB depend on EFI at one point during my development, but took
> it out because there was no actual dependency (the stub will work fine without
> other EFI features.)  The features will most likely be used together,
> but I wasn't
> sure if we would want to enforce this with a config dependency.  I
> don't care one
> way or the other, I'd just like the dependencies to be correct and
> follow best practices.

I guess it's up to you, so long as the constraint is expressed in Kconfig
somehow.

Cheers
---Dave

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH 6/7] Add EFI stub for ARM
@ 2013-08-06 10:40           ` Dave P Martin
  0 siblings, 0 replies; 54+ messages in thread
From: Dave P Martin @ 2013-08-06 10:40 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, Aug 06, 2013 at 01:06:17AM +0100, Roy Franz wrote:
> On Mon, Aug 5, 2013 at 8:33 AM, Leif Lindholm <leif.lindholm@linaro.org> wrote:
> > On Mon, Aug 05, 2013 at 03:11:49PM +0100, Dave Martin wrote:
> >> > diff --git a/arch/arm/boot/compressed/head.S b/arch/arm/boot/compressed/head.S
> >> > index 75189f1..4c70b9e 100644
> >> > --- a/arch/arm/boot/compressed/head.S
> >> > +++ b/arch/arm/boot/compressed/head.S
> >> > @@ -122,19 +122,106 @@
> >> >             .arm                            @ Always enter in ARM state
> >> >  start:
> >> >             .type   start,#function
> >> > -           .rept   7
> >> > +#ifdef CONFIG_EFI_STUB
> >> > +           @ Magic MSDOS signature for PE/COFF + ADD opcode
> >> > +           .word   0x62805a4d

Is BE8 supported?  If so, this would put the bytes

	62 80 5A 4D

in the binary, which is not the right magic.

For this magic, you could use .byte instead.


To help future maintainers, I suggest noting in a comment that
executing through this magic relies on little-endian instruction byte
order (so, LE or BE8), and the ARM instruction set.

What about the endianness of the other PE/COFF header fields?  Are they
always little-endian, or are some fields native-endian (and if so, how
is the endianness of the header determined by the loader)?

> >>
> >> What about BE32?
> >
> > The ARM bindings for UEFI specify that the processor must be in
> > little-endian mode.
> >
> >> In that case, the instruction is a coprocessor load, that loads from a
> >> random address to a coprocessor that almost certainly doesn't exist.
> >> This will probably fault.
> >>
> >> Since BE32 is only for older platforms (<v6) and this is not easily
> >> solvable, it might be sensible to make the EFI stub support depend on
> >> !CPU_ENDIAN_BE32.
> >
> > Well, it would make more sense to make EFI_STUB depend on EFI and
> > EFI depend on !CPU_ENDIAN_BE32. Which is something I can add to
> > my next set of general ARM UEFI patches. Thanks.
> > /
> >     Leif
> 
> I had EFI_STUB depend on EFI at one point during my development, but took
> it out because there was no actual dependency (the stub will work fine without
> other EFI features.)  The features will most likely be used together,
> but I wasn't
> sure if we would want to enforce this with a config dependency.  I
> don't care one
> way or the other, I'd just like the dependencies to be correct and
> follow best practices.

I guess it's up to you, so long as the constraint is expressed in Kconfig
somehow.

Cheers
---Dave

^ permalink raw reply	[flat|nested] 54+ messages in thread

* Re: [PATCH 4/7] Add proper definitions for some EFI function pointers.
@ 2013-08-06 13:19     ` Matt Fleming
  0 siblings, 0 replies; 54+ messages in thread
From: Matt Fleming @ 2013-08-06 13:19 UTC (permalink / raw)
  To: Roy Franz
  Cc: linux-kernel, linux-efi, linux-arm-kernel, matt.fleming, linux,
	leif.lindholm

On Fri, 02 Aug, at 02:29:05PM, Roy Franz wrote:
> The x86/AMD64 EFI stubs must us a call wrapper to convert between
> the Linux and EFI ABIs, so void pointers are sufficient.  For ARM,
> the ABIs are compatible, so we can directly invoke the function
> pointers.  The functions that are used by the ARM stub are updated
> to match the EFI definitions.
> 
> Signed-off-by: Roy Franz <roy.franz@linaro.org>
> ---
>  include/linux/efi.h |   61 +++++++++++++++++++++++++++++++++++++--------------
>  1 file changed, 44 insertions(+), 17 deletions(-)

[...]

> @@ -96,6 +98,26 @@ typedef	struct {
>  #define EFI_MEMORY_DESCRIPTOR_VERSION	1
>  
>  #define EFI_PAGE_SHIFT		12
> +#define EFI_PAGE_SIZE		(1UL << EFI_PAGE_SHIFT)
> +
> +typedef enum {
> +	EfiReservedMemoryType,
> +	EfiLoaderCode,
> +	EfiLoaderData,
> +	EfiBootServicesCode,
> +	EfiBootServicesData,
> +	EfiRuntimeServicesCode,
> +	EfiRuntimeServicesData,
> +	EfiConventionalMemory,
> +	EfiUnusableMemory,
> +	EfiACPIReclaimMemory,
> +	EfiACPIMemoryNVS,
> +	EfiMemoryMappedIO,
> +	EfiMemoryMappedIOPortSpace,
> +	EfiPalCode,
> +	EfiMaxMemoryType
> +} EFI_MEMORY_TYPE;
> +

These enums aren't used anywhere in your patches from what I can see and
we already have #define's for all these values, e.g. EFI_RESERVED_TYPE.

-- 
Matt Fleming, Intel Open Source Technology Center

^ permalink raw reply	[flat|nested] 54+ messages in thread

* Re: [PATCH 4/7] Add proper definitions for some EFI function pointers.
@ 2013-08-06 13:19     ` Matt Fleming
  0 siblings, 0 replies; 54+ messages in thread
From: Matt Fleming @ 2013-08-06 13:19 UTC (permalink / raw)
  To: Roy Franz
  Cc: linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-efi-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matt.fleming-ral2JQCrhuEAvxtiuMwx3w,
	linux-lFZ/pmaqli7XmaaqVzeoHQ,
	leif.lindholm-QSEj5FYQhm4dnm+yROfE0A

On Fri, 02 Aug, at 02:29:05PM, Roy Franz wrote:
> The x86/AMD64 EFI stubs must us a call wrapper to convert between
> the Linux and EFI ABIs, so void pointers are sufficient.  For ARM,
> the ABIs are compatible, so we can directly invoke the function
> pointers.  The functions that are used by the ARM stub are updated
> to match the EFI definitions.
> 
> Signed-off-by: Roy Franz <roy.franz-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
> ---
>  include/linux/efi.h |   61 +++++++++++++++++++++++++++++++++++++--------------
>  1 file changed, 44 insertions(+), 17 deletions(-)

[...]

> @@ -96,6 +98,26 @@ typedef	struct {
>  #define EFI_MEMORY_DESCRIPTOR_VERSION	1
>  
>  #define EFI_PAGE_SHIFT		12
> +#define EFI_PAGE_SIZE		(1UL << EFI_PAGE_SHIFT)
> +
> +typedef enum {
> +	EfiReservedMemoryType,
> +	EfiLoaderCode,
> +	EfiLoaderData,
> +	EfiBootServicesCode,
> +	EfiBootServicesData,
> +	EfiRuntimeServicesCode,
> +	EfiRuntimeServicesData,
> +	EfiConventionalMemory,
> +	EfiUnusableMemory,
> +	EfiACPIReclaimMemory,
> +	EfiACPIMemoryNVS,
> +	EfiMemoryMappedIO,
> +	EfiMemoryMappedIOPortSpace,
> +	EfiPalCode,
> +	EfiMaxMemoryType
> +} EFI_MEMORY_TYPE;
> +

These enums aren't used anywhere in your patches from what I can see and
we already have #define's for all these values, e.g. EFI_RESERVED_TYPE.

-- 
Matt Fleming, Intel Open Source Technology Center

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH 4/7] Add proper definitions for some EFI function pointers.
@ 2013-08-06 13:19     ` Matt Fleming
  0 siblings, 0 replies; 54+ messages in thread
From: Matt Fleming @ 2013-08-06 13:19 UTC (permalink / raw)
  To: linux-arm-kernel

On Fri, 02 Aug, at 02:29:05PM, Roy Franz wrote:
> The x86/AMD64 EFI stubs must us a call wrapper to convert between
> the Linux and EFI ABIs, so void pointers are sufficient.  For ARM,
> the ABIs are compatible, so we can directly invoke the function
> pointers.  The functions that are used by the ARM stub are updated
> to match the EFI definitions.
> 
> Signed-off-by: Roy Franz <roy.franz@linaro.org>
> ---
>  include/linux/efi.h |   61 +++++++++++++++++++++++++++++++++++++--------------
>  1 file changed, 44 insertions(+), 17 deletions(-)

[...]

> @@ -96,6 +98,26 @@ typedef	struct {
>  #define EFI_MEMORY_DESCRIPTOR_VERSION	1
>  
>  #define EFI_PAGE_SHIFT		12
> +#define EFI_PAGE_SIZE		(1UL << EFI_PAGE_SHIFT)
> +
> +typedef enum {
> +	EfiReservedMemoryType,
> +	EfiLoaderCode,
> +	EfiLoaderData,
> +	EfiBootServicesCode,
> +	EfiBootServicesData,
> +	EfiRuntimeServicesCode,
> +	EfiRuntimeServicesData,
> +	EfiConventionalMemory,
> +	EfiUnusableMemory,
> +	EfiACPIReclaimMemory,
> +	EfiACPIMemoryNVS,
> +	EfiMemoryMappedIO,
> +	EfiMemoryMappedIOPortSpace,
> +	EfiPalCode,
> +	EfiMaxMemoryType
> +} EFI_MEMORY_TYPE;
> +

These enums aren't used anywhere in your patches from what I can see and
we already have #define's for all these values, e.g. EFI_RESERVED_TYPE.

-- 
Matt Fleming, Intel Open Source Technology Center

^ permalink raw reply	[flat|nested] 54+ messages in thread

* Re: [PATCH 3/7] Change EFI helper APIs to be more flexible
@ 2013-08-06 13:53     ` Matt Fleming
  0 siblings, 0 replies; 54+ messages in thread
From: Matt Fleming @ 2013-08-06 13:53 UTC (permalink / raw)
  To: Roy Franz
  Cc: linux-kernel, linux-efi, linux-arm-kernel, matt.fleming, linux,
	leif.lindholm

On Fri, 02 Aug, at 02:29:04PM, Roy Franz wrote:
> * Change the EFI helper functions to be more flexible to allow their use
> by other architectures.
> * Pass system table pointer as argument to all functions that use it,
> rather than use a global variable.
> * Change name of __get_map to efi_get_memory_map, and add key pointer argument
> so it can be used to get final memory map before the EFI exitBootServices()
> function is called.
> * Change the handle_ramdisks() to handle_cmdline_files(), and make it more
> generic by taking string to match rather than hardcoding 'initrd='
> so it can be used to load device tree files as well.
> * Add 'min' address parameter to limit low_alloc() alloctions
> * Fix type of chunksize to match EFI function (not checked in x86 code
> as wrappers break type checking.)
> * rename alloc/free functions - low_free() is usable by both low_alloc()
> and high_alloc(), so rename them to be more consistent - efi_low_alloc()
> efi_high_alloc(), and efi_free()
> * Enforce minimum pagesize alignment as expected by EFI allocate_pages().
> * check for 0 size in efi_free.
 
This is way too many changes to include in a single patch, and makes
review fairly difficult. Please split this large patch into smaller
patches.

Not only are smaller patches easier to review, they make it much easier
for you to incorporate any feedback.

-- 
Matt Fleming, Intel Open Source Technology Center

^ permalink raw reply	[flat|nested] 54+ messages in thread

* Re: [PATCH 3/7] Change EFI helper APIs to be more flexible
@ 2013-08-06 13:53     ` Matt Fleming
  0 siblings, 0 replies; 54+ messages in thread
From: Matt Fleming @ 2013-08-06 13:53 UTC (permalink / raw)
  To: Roy Franz
  Cc: linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-efi-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matt.fleming-ral2JQCrhuEAvxtiuMwx3w,
	linux-lFZ/pmaqli7XmaaqVzeoHQ,
	leif.lindholm-QSEj5FYQhm4dnm+yROfE0A

On Fri, 02 Aug, at 02:29:04PM, Roy Franz wrote:
> * Change the EFI helper functions to be more flexible to allow their use
> by other architectures.
> * Pass system table pointer as argument to all functions that use it,
> rather than use a global variable.
> * Change name of __get_map to efi_get_memory_map, and add key pointer argument
> so it can be used to get final memory map before the EFI exitBootServices()
> function is called.
> * Change the handle_ramdisks() to handle_cmdline_files(), and make it more
> generic by taking string to match rather than hardcoding 'initrd='
> so it can be used to load device tree files as well.
> * Add 'min' address parameter to limit low_alloc() alloctions
> * Fix type of chunksize to match EFI function (not checked in x86 code
> as wrappers break type checking.)
> * rename alloc/free functions - low_free() is usable by both low_alloc()
> and high_alloc(), so rename them to be more consistent - efi_low_alloc()
> efi_high_alloc(), and efi_free()
> * Enforce minimum pagesize alignment as expected by EFI allocate_pages().
> * check for 0 size in efi_free.
 
This is way too many changes to include in a single patch, and makes
review fairly difficult. Please split this large patch into smaller
patches.

Not only are smaller patches easier to review, they make it much easier
for you to incorporate any feedback.

-- 
Matt Fleming, Intel Open Source Technology Center

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH 3/7] Change EFI helper APIs to be more flexible
@ 2013-08-06 13:53     ` Matt Fleming
  0 siblings, 0 replies; 54+ messages in thread
From: Matt Fleming @ 2013-08-06 13:53 UTC (permalink / raw)
  To: linux-arm-kernel

On Fri, 02 Aug, at 02:29:04PM, Roy Franz wrote:
> * Change the EFI helper functions to be more flexible to allow their use
> by other architectures.
> * Pass system table pointer as argument to all functions that use it,
> rather than use a global variable.
> * Change name of __get_map to efi_get_memory_map, and add key pointer argument
> so it can be used to get final memory map before the EFI exitBootServices()
> function is called.
> * Change the handle_ramdisks() to handle_cmdline_files(), and make it more
> generic by taking string to match rather than hardcoding 'initrd='
> so it can be used to load device tree files as well.
> * Add 'min' address parameter to limit low_alloc() alloctions
> * Fix type of chunksize to match EFI function (not checked in x86 code
> as wrappers break type checking.)
> * rename alloc/free functions - low_free() is usable by both low_alloc()
> and high_alloc(), so rename them to be more consistent - efi_low_alloc()
> efi_high_alloc(), and efi_free()
> * Enforce minimum pagesize alignment as expected by EFI allocate_pages().
> * check for 0 size in efi_free.
 
This is way too many changes to include in a single patch, and makes
review fairly difficult. Please split this large patch into smaller
patches.

Not only are smaller patches easier to review, they make it much easier
for you to incorporate any feedback.

-- 
Matt Fleming, Intel Open Source Technology Center

^ permalink raw reply	[flat|nested] 54+ messages in thread

* Re: [PATCH 2/7] Move common EFI stub code from x86 arch code to common location
@ 2013-08-06 13:53     ` Matt Fleming
  0 siblings, 0 replies; 54+ messages in thread
From: Matt Fleming @ 2013-08-06 13:53 UTC (permalink / raw)
  To: Roy Franz
  Cc: linux-kernel, linux-efi, linux-arm-kernel, matt.fleming, linux,
	leif.lindholm

On Fri, 02 Aug, at 02:29:03PM, Roy Franz wrote:
> No code changes made, just moving functions from x86 arch directory
> to common location.
> Code is shared using #include, similar to how decompression code
> is shared among architectures.
> 
> Signed-off-by: Roy Franz <roy.franz@linaro.org>
> ---
>  arch/x86/boot/compressed/eboot.c       |  442 +-----------------------------
>  arch/x86/boot/compressed/eboot.h       |    6 -
>  drivers/firmware/efi/efi-stub-helper.c |  463 ++++++++++++++++++++++++++++++++
>  include/linux/efi.h                    |    8 +
>  4 files changed, 472 insertions(+), 447 deletions(-)
>  create mode 100644 drivers/firmware/efi/efi-stub-helper.c

[...]

> diff --git a/drivers/firmware/efi/efi-stub-helper.c b/drivers/firmware/efi/efi-stub-helper.c
> new file mode 100644
> index 0000000..47891bd
> --- /dev/null
> +++ b/drivers/firmware/efi/efi-stub-helper.c
> @@ -0,0 +1,463 @@
> +/*
> + * Helper functions used by the EFI stub on multiple
> + * architectures. This should be #included by the EFI stub
> + * implementation files.
> + *
> + * Copyright 2011 Intel Corporation; author Matt Fleming
> + *
> + * This file is part of the Linux kernel, and is made available
> + * under the terms of the GNU General Public License version 2.
> + *
> + */
> +
> +
> +struct initrd {
> +	efi_file_handle_t *handle;
> +	u64 size;
> +};

This should really be renamed in a later patch, like 'struct efi_file'
or similar because the dtb case has nothing to do with initrds.

> +/*
> + * Check the cmdline for a LILO-style initrd= arguments.
> + *
> + * We only support loading an initrd from the same filesystem as the
> + * kernel image.
> + */
> +static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
> +				    struct setup_header *hdr)
> +{
> +	struct initrd *initrds;

[...]

> +free_initrds:
> +	efi_call_phys1(sys_table->boottime->free_pool, initrds);
> +fail:
> +	hdr->ramdisk_image = 0;
> +	hdr->ramdisk_size = 0;
> +
> +	return status;
> +}

Likewise, this function would benefit from getting rid of the references
to initrd in a later patch.

-- 
Matt Fleming, Intel Open Source Technology Center

^ permalink raw reply	[flat|nested] 54+ messages in thread

* Re: [PATCH 2/7] Move common EFI stub code from x86 arch code to common location
@ 2013-08-06 13:53     ` Matt Fleming
  0 siblings, 0 replies; 54+ messages in thread
From: Matt Fleming @ 2013-08-06 13:53 UTC (permalink / raw)
  To: Roy Franz
  Cc: linux-kernel-u79uwXL29TY76Z2rM5mHXA,
	linux-efi-u79uwXL29TY76Z2rM5mHXA,
	linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r,
	matt.fleming-ral2JQCrhuEAvxtiuMwx3w,
	linux-lFZ/pmaqli7XmaaqVzeoHQ,
	leif.lindholm-QSEj5FYQhm4dnm+yROfE0A

On Fri, 02 Aug, at 02:29:03PM, Roy Franz wrote:
> No code changes made, just moving functions from x86 arch directory
> to common location.
> Code is shared using #include, similar to how decompression code
> is shared among architectures.
> 
> Signed-off-by: Roy Franz <roy.franz-QSEj5FYQhm4dnm+yROfE0A@public.gmane.org>
> ---
>  arch/x86/boot/compressed/eboot.c       |  442 +-----------------------------
>  arch/x86/boot/compressed/eboot.h       |    6 -
>  drivers/firmware/efi/efi-stub-helper.c |  463 ++++++++++++++++++++++++++++++++
>  include/linux/efi.h                    |    8 +
>  4 files changed, 472 insertions(+), 447 deletions(-)
>  create mode 100644 drivers/firmware/efi/efi-stub-helper.c

[...]

> diff --git a/drivers/firmware/efi/efi-stub-helper.c b/drivers/firmware/efi/efi-stub-helper.c
> new file mode 100644
> index 0000000..47891bd
> --- /dev/null
> +++ b/drivers/firmware/efi/efi-stub-helper.c
> @@ -0,0 +1,463 @@
> +/*
> + * Helper functions used by the EFI stub on multiple
> + * architectures. This should be #included by the EFI stub
> + * implementation files.
> + *
> + * Copyright 2011 Intel Corporation; author Matt Fleming
> + *
> + * This file is part of the Linux kernel, and is made available
> + * under the terms of the GNU General Public License version 2.
> + *
> + */
> +
> +
> +struct initrd {
> +	efi_file_handle_t *handle;
> +	u64 size;
> +};

This should really be renamed in a later patch, like 'struct efi_file'
or similar because the dtb case has nothing to do with initrds.

> +/*
> + * Check the cmdline for a LILO-style initrd= arguments.
> + *
> + * We only support loading an initrd from the same filesystem as the
> + * kernel image.
> + */
> +static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
> +				    struct setup_header *hdr)
> +{
> +	struct initrd *initrds;

[...]

> +free_initrds:
> +	efi_call_phys1(sys_table->boottime->free_pool, initrds);
> +fail:
> +	hdr->ramdisk_image = 0;
> +	hdr->ramdisk_size = 0;
> +
> +	return status;
> +}

Likewise, this function would benefit from getting rid of the references
to initrd in a later patch.

-- 
Matt Fleming, Intel Open Source Technology Center

^ permalink raw reply	[flat|nested] 54+ messages in thread

* [PATCH 2/7] Move common EFI stub code from x86 arch code to common location
@ 2013-08-06 13:53     ` Matt Fleming
  0 siblings, 0 replies; 54+ messages in thread
From: Matt Fleming @ 2013-08-06 13:53 UTC (permalink / raw)
  To: linux-arm-kernel

On Fri, 02 Aug, at 02:29:03PM, Roy Franz wrote:
> No code changes made, just moving functions from x86 arch directory
> to common location.
> Code is shared using #include, similar to how decompression code
> is shared among architectures.
> 
> Signed-off-by: Roy Franz <roy.franz@linaro.org>
> ---
>  arch/x86/boot/compressed/eboot.c       |  442 +-----------------------------
>  arch/x86/boot/compressed/eboot.h       |    6 -
>  drivers/firmware/efi/efi-stub-helper.c |  463 ++++++++++++++++++++++++++++++++
>  include/linux/efi.h                    |    8 +
>  4 files changed, 472 insertions(+), 447 deletions(-)
>  create mode 100644 drivers/firmware/efi/efi-stub-helper.c

[...]

> diff --git a/drivers/firmware/efi/efi-stub-helper.c b/drivers/firmware/efi/efi-stub-helper.c
> new file mode 100644
> index 0000000..47891bd
> --- /dev/null
> +++ b/drivers/firmware/efi/efi-stub-helper.c
> @@ -0,0 +1,463 @@
> +/*
> + * Helper functions used by the EFI stub on multiple
> + * architectures. This should be #included by the EFI stub
> + * implementation files.
> + *
> + * Copyright 2011 Intel Corporation; author Matt Fleming
> + *
> + * This file is part of the Linux kernel, and is made available
> + * under the terms of the GNU General Public License version 2.
> + *
> + */
> +
> +
> +struct initrd {
> +	efi_file_handle_t *handle;
> +	u64 size;
> +};

This should really be renamed in a later patch, like 'struct efi_file'
or similar because the dtb case has nothing to do with initrds.

> +/*
> + * Check the cmdline for a LILO-style initrd= arguments.
> + *
> + * We only support loading an initrd from the same filesystem as the
> + * kernel image.
> + */
> +static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
> +				    struct setup_header *hdr)
> +{
> +	struct initrd *initrds;

[...]

> +free_initrds:
> +	efi_call_phys1(sys_table->boottime->free_pool, initrds);
> +fail:
> +	hdr->ramdisk_image = 0;
> +	hdr->ramdisk_size = 0;
> +
> +	return status;
> +}

Likewise, this function would benefit from getting rid of the references
to initrd in a later patch.

-- 
Matt Fleming, Intel Open Source Technology Center

^ permalink raw reply	[flat|nested] 54+ messages in thread

end of thread, other threads:[~2013-08-06 13:53 UTC | newest]

Thread overview: 54+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-08-02 21:29 [PATCH 0/7] RFC: EFI stub for ARM Roy Franz
2013-08-02 21:29 ` Roy Franz
2013-08-02 21:29 ` Roy Franz
2013-08-02 21:29 ` [PATCH 1/7] EFI stub documentation updates Roy Franz
2013-08-02 21:29   ` Roy Franz
2013-08-02 21:29   ` Roy Franz
2013-08-05 14:12   ` Dave Martin
2013-08-05 14:12     ` Dave Martin
2013-08-05 14:12     ` Dave Martin
2013-08-05 23:56     ` Roy Franz
2013-08-05 23:56       ` Roy Franz
2013-08-05 23:56       ` Roy Franz
2013-08-06 10:30       ` Dave P Martin
2013-08-06 10:30         ` Dave P Martin
2013-08-06 10:30         ` Dave P Martin
2013-08-02 21:29 ` [PATCH 2/7] Move common EFI stub code from x86 arch code to common location Roy Franz
2013-08-02 21:29   ` Roy Franz
2013-08-06 13:53   ` Matt Fleming
2013-08-06 13:53     ` Matt Fleming
2013-08-06 13:53     ` Matt Fleming
2013-08-02 21:29 ` [PATCH 3/7] Change EFI helper APIs to be more flexible Roy Franz
2013-08-02 21:29   ` Roy Franz
2013-08-02 21:29   ` Roy Franz
2013-08-06 13:53   ` Matt Fleming
2013-08-06 13:53     ` Matt Fleming
2013-08-06 13:53     ` Matt Fleming
2013-08-02 21:29 ` [PATCH 4/7] Add proper definitions for some EFI function pointers Roy Franz
2013-08-02 21:29   ` Roy Franz
2013-08-06 13:19   ` Matt Fleming
2013-08-06 13:19     ` Matt Fleming
2013-08-06 13:19     ` Matt Fleming
2013-08-02 21:29 ` [PATCH 5/7] Add strstr to compressed string.c for ARM Roy Franz
2013-08-02 21:29   ` Roy Franz
2013-08-02 21:29 ` [PATCH 6/7] Add EFI stub " Roy Franz
2013-08-02 21:29   ` Roy Franz
2013-08-02 21:29   ` Roy Franz
2013-08-05 14:11   ` Dave Martin
2013-08-05 14:11     ` Dave Martin
2013-08-05 15:33     ` Leif Lindholm
2013-08-05 15:33       ` Leif Lindholm
2013-08-06  0:06       ` Roy Franz
2013-08-06  0:06         ` Roy Franz
2013-08-06  0:06         ` Roy Franz
2013-08-06 10:40         ` Dave P Martin
2013-08-06 10:40           ` Dave P Martin
2013-08-06 10:40           ` Dave P Martin
2013-08-06 10:31       ` Dave P Martin
2013-08-06 10:31         ` Dave P Martin
2013-08-06 10:31         ` Dave P Martin
2013-08-06  3:35     ` Roy Franz
2013-08-06  3:35       ` Roy Franz
2013-08-06  3:35       ` Roy Franz
2013-08-02 21:29 ` [PATCH 7/7] Add config EFI_STUB " Roy Franz
2013-08-02 21:29   ` Roy Franz

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.