Skip to contentSkip to conversation

multiboot2: Add two extensions and fix some issues

PATCH Applied Daniel Kiper 5 participants 64 messages Jul 20, 2015 – Feb 12, 2016 4 messages not in the archive

lore
Share & tools
lore
#0 lore raw

v2 cover multiboot2: Add two extensions and fix some issues

Hi,

This patch series:
- enables EFI boot services usage in loaded images by multiboot2 protocol,
- add support for multiboot2 protocol compatible relocatable images,
- fixes two minor issues.

Daniel

 .gitignore                            |    3 ++
 grub-core/Makefile.core.def           |    1 +
 grub-core/lib/i386/relocator.c        |   53 +++++++++++++++++++++
 grub-core/lib/i386/relocator64_efi.S  |   77 ++++++++++++++++++++++++++++++
 grub-core/lib/relocator.c             |    2 +-
 grub-core/loader/i386/multiboot_mbi.c |    6 ++-
 grub-core/loader/multiboot.c          |   41 +++++++++++++---
 grub-core/loader/multiboot_elfxx.c    |   28 ++++++++---
 grub-core/loader/multiboot_mbi2.c     |  199 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
 include/grub/i386/multiboot.h         |   11 +++++
 include/grub/i386/relocator.h         |   21 +++++++++
 include/grub/multiboot.h              |    4 +-
 include/multiboot2.h                  |   49 +++++++++++++++++++
 13 files changed, 423 insertions(+), 72 deletions(-)

Daniel Kiper (6):
gitignore: Ignore *.orig, *.rej and *.swp files
relocator: Do not use memory region if its starta is smaller than size i386/relocator: Add grub_relocator64_efi relocator
multiboot2: Add tags used to pass ImageHandle to loaded image
multiboot2: Add support for relocatable images
multiboot2: Do not pass memory maps to image if EFI boot services are enabled

#1 lore raw

PATCH v2 1/6 gitignore: Ignore *.orig, *.rej and *.swp files

---
 .gitignore |    3 +++
 1 file changed, 3 insertions(+)
.gitignore +3 -0
index 18ab8e8..6d25d39 100644
@@ -147,6 +147,7 @@ mod-*.c
 missing
 netboot_test
 *.o
+*.orig
 *.a
 ohci_test
 partmap_test
@@ -160,9 +161,11 @@ po/stamp-po
 printf_test
 priority_queue_unit_test
 pseries_test
+*.rej
 stamp-h
 stamp-h1
 stamp-h.in
+*.swp
 symlist.c
 symlist.h
 trigtables.c
Signature · 1 line
1.7.10.4
Reviewed-by on v2 1/6
#2 lore raw

Re: [PATCH v2 1/6] gitignore: Ignore *.orig, *.rej and *.swp files

Quoted 1 line · Daniel Kiper

but really - this is quite easy to review :-)

Quoted 31 lines
---
 .gitignore |    3 +++
 1 file changed, 3 insertions(+)
.gitignore +3 -0
index 18ab8e8..6d25d39 100644
@@ -147,6 +147,7 @@ mod-*.c
 missing
 netboot_test
 *.o
+*.orig
 *.a
 ohci_test
 partmap_test
@@ -160,9 +161,11 @@ po/stamp-po
 printf_test
 priority_queue_unit_test
 pseries_test
+*.rej
 stamp-h
 stamp-h1
 stamp-h.in
+*.swp
 symlist.c
 symlist.h
 trigtables.c
Signature · 1 line
1.7.10.4
Reviewed-by on v2 1/6
#3 lore raw

Re: [PATCH v2 1/6] gitignore: Ignore *.orig, *.rej and *.swp files

In reply to Daniel Kiper
Quoted 1 line · Daniel Kiper

but really - this is quite easy to review :-)

Quoted 31 lines
---
 .gitignore |    3 +++
 1 file changed, 3 insertions(+)
.gitignore +3 -0
index 18ab8e8..6d25d39 100644
@@ -147,6 +147,7 @@ mod-*.c
 missing
 netboot_test
 *.o
+*.orig
 *.a
 ohci_test
 partmap_test
@@ -160,9 +161,11 @@ po/stamp-po
 printf_test
 priority_queue_unit_test
 pseries_test
+*.rej
 stamp-h
 stamp-h1
 stamp-h.in
+*.swp
 symlist.c
 symlist.h
 trigtables.c
Signature · 1 line
1.7.10.4
#4 lore raw

Re: [PATCH v2 1/6] gitignore: Ignore *.orig, *.rej and *.swp files

In reply to Daniel Kiper

(Having written the below I see too late that this is a grub patch not a Xen one, a tag in the subject for such cross posted patches would be useful please. Anyway, my opinion counts for very little in this context but I leave it below since already I wrote it. I notice that xen.git#.gitignore _does_ list *.rej, which I think is wrong...)

Quoted 1 line · Daniel Kiper

At least *.rej and perhaps *.orig are indicative of a failed patch application, I think I want them to appear in "git status".

By way of comparison Linux's .gitignore includes *.orig but not *.rej and Qemu's includes neither.

So nack to the addition of *.rej from me. I'm more or less ambivalent about *.orig.

Quoted 28 lines
---
 .gitignore |    3 +++
 1 file changed, 3 insertions(+)
.gitignore +3 -0
index 18ab8e8..6d25d39 100644
@@ -147,6 +147,7 @@ mod-*.c
 missing
 netboot_test
 *.o
+*.orig
 *.a
 ohci_test
 partmap_test
@@ -160,9 +161,11 @@ po/stamp-po
 printf_test
 priority_queue_unit_test
 pseries_test
+*.rej
 stamp-h
 stamp-h1
 stamp-h.in
+*.swp
 symlist.c
 symlist.h
 trigtables.c
#5 lore raw

Re: [PATCH v2 1/6] gitignore: Ignore *.orig, *.rej and *.swp files

In reply to Daniel Kiper

(Having written the below I see too late that this is a grub patch not a Xen one, a tag in the subject for such cross posted patches would be useful please. Anyway, my opinion counts for very little in this context but I leave it below since already I wrote it. I notice that xen.git#.gitignore _does_ list *.rej, which I think is wrong...)

Quoted 1 line · Daniel Kiper

At least *.rej and perhaps *.orig are indicative of a failed patch application, I think I want them to appear in "git status".

By way of comparison Linux's .gitignore includes *.orig but not *.rej and Qemu's includes neither.

So nack to the addition of *.rej from me. I'm more or less ambivalent about *.orig.

Quoted 28 lines
---
 .gitignore |    3 +++
 1 file changed, 3 insertions(+)
.gitignore +3 -0
index 18ab8e8..6d25d39 100644
@@ -147,6 +147,7 @@ mod-*.c
 missing
 netboot_test
 *.o
+*.orig
 *.a
 ohci_test
 partmap_test
@@ -160,9 +161,11 @@ po/stamp-po
 printf_test
 priority_queue_unit_test
 pseries_test
+*.rej
 stamp-h
 stamp-h1
 stamp-h.in
+*.swp
 symlist.c
 symlist.h
 trigtables.c
#6 lore raw

Re: [PATCH v2 1/6] gitignore: Ignore *.orig, *.rej and *.swp files

Quoted 4 lines · Ian Campbell

(Having written the below I see too late that this is a grub patch not a Xen one, a tag in the subject for such cross posted patches would be

useful

Quoted 7 lines

please. Anyway, my opinion counts for very little in this context but I leave it below since already I wrote it. I notice that xen.git#.gitignore _does_ list *.rej, which I think is wrong...)

Quoted 1 line · Daniel Kiper
Quoted 7 lines

At least *.rej and perhaps *.orig are indicative of a failed patch application, I think I want them to appear in "git status".

By way of comparison Linux's .gitignore includes *.orig but not *.rej and Qemu's includes neither.

So nack to the addition of *.rej from me. I'm more or less ambivalent

about

Quoted 2 lines

*.orig.

I have to agree. You should clean up *.rej *.orig after fixing conflicts

Quoted 20 lines
Quoted 20 lines
---
 .gitignore |    3 +++
 1 file changed, 3 insertions(+)
.gitignore +1 -0
index 18ab8e8..6d25d39 100644
@@ -147,6 +147,7 @@ mod-*.c
 missing
 netboot_test
 *.o
+*.orig
 *.a
 ohci_test
 partmap_test
@@ -160,9 +161,11 @@ po/stamp-po
 printf_test
 priority_queue_unit_test
 pseries_test
Quoted 8 lines

.gitignore po/stamp-po · line 171

Quoted 8 lines
.gitignore +2 -0
+*.rej
 stamp-h
 stamp-h1
 stamp-h.in
+*.swp
 symlist.c
 symlist.h
 trigtables.c

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#7 lore raw

Re: [PATCH v2 1/6] gitignore: Ignore *.orig, *.rej and *.swp files

In reply to Ian Campbell
Quoted 4 lines · Ian Campbell

(Having written the below I see too late that this is a grub patch not a Xen one, a tag in the subject for such cross posted patches would be

useful

Quoted 7 lines

please. Anyway, my opinion counts for very little in this context but I leave it below since already I wrote it. I notice that xen.git#.gitignore _does_ list *.rej, which I think is wrong...)

Quoted 1 line · Daniel Kiper
Quoted 7 lines

At least *.rej and perhaps *.orig are indicative of a failed patch application, I think I want them to appear in "git status".

By way of comparison Linux's .gitignore includes *.orig but not *.rej and Qemu's includes neither.

So nack to the addition of *.rej from me. I'm more or less ambivalent

about

Quoted 2 lines

*.orig.

I have to agree. You should clean up *.rej *.orig after fixing conflicts

Quoted 28 lines
Quoted 28 lines
---
 .gitignore |    3 +++
 1 file changed, 3 insertions(+)
.gitignore +3 -0
index 18ab8e8..6d25d39 100644
@@ -147,6 +147,7 @@ mod-*.c
 missing
 netboot_test
 *.o
+*.orig
 *.a
 ohci_test
 partmap_test
@@ -160,9 +161,11 @@ po/stamp-po
 printf_test
 priority_queue_unit_test
 pseries_test
+*.rej
 stamp-h
 stamp-h1
 stamp-h.in
+*.swp
 symlist.c
 symlist.h
 trigtables.c
#8 lore raw

Re: [PATCH v2 1/6] gitignore: Ignore *.orig, *.rej and *.swp files

Quoted 16 lines · Vladimir 'phcoder' Serbinenko
Quoted 4 lines · Ian Campbell

(Having written the below I see too late that this is a grub patch not a Xen one, a tag in the subject for such cross posted patches would be

useful

Quoted 10 lines

please. Anyway, my opinion counts for very little in this context but I leave it below since already I wrote it. I notice that xen.git#.gitignore _does_ list *.rej, which I think is wrong...)

Quoted 1 line · Daniel Kiper

At least *.rej and perhaps *.orig are indicative of a failed patch application, I think I want them to appear in "git status".

Quoted 8 lines
Quoted 4 lines

By way of comparison Linux's .gitignore includes *.orig but not *.rej and Qemu's includes neither.

So nack to the addition of *.rej from me. I'm more or less ambivalent

about

Quoted 2 lines

*.orig.

I have to agree. You should clean up *.rej *.orig after fixing conflicts

Thanks for comment on this. Could you review rest of this patchset? I am working on v3 and it will be nice to take your (and others if possible) comments into it.

Daniel

#9 lore raw

Re: [PATCH v2 1/6] gitignore: Ignore *.orig, *.rej and *.swp files

In reply to Vladimir 'phcoder' Serbinenko
Quoted 16 lines · Vladimir 'phcoder' Serbinenko
Quoted 4 lines · Ian Campbell

(Having written the below I see too late that this is a grub patch not a Xen one, a tag in the subject for such cross posted patches would be

useful

Quoted 10 lines

please. Anyway, my opinion counts for very little in this context but I leave it below since already I wrote it. I notice that xen.git#.gitignore _does_ list *.rej, which I think is wrong...)

Quoted 1 line · Daniel Kiper

At least *.rej and perhaps *.orig are indicative of a failed patch application, I think I want them to appear in "git status".

Quoted 8 lines
Quoted 4 lines

By way of comparison Linux's .gitignore includes *.orig but not *.rej and Qemu's includes neither.

So nack to the addition of *.rej from me. I'm more or less ambivalent

about

Quoted 2 lines

*.orig.

I have to agree. You should clean up *.rej *.orig after fixing conflicts

Thanks for comment on this. Could you review rest of this patchset? I am working on v3 and it will be nice to take your (and others if possible) comments into it.

Daniel

#10 lore raw

Re: [PATCH v2 1/6] gitignore: Ignore *.orig, *.rej and *.swp files

Quoted 21 lines · Daniel Kiper
Quoted 20 lines · Vladimir 'phcoder' Serbinenko
Quoted 4 lines · Ian Campbell

(Having written the below I see too late that this is a grub patch not a Xen one, a tag in the subject for such cross posted patches would be

useful

Quoted 14 lines

please. Anyway, my opinion counts for very little in this context but I leave it below since already I wrote it. I notice that xen.git#.gitignore _does_ list *.rej, which I think is wrong...)

Quoted 1 line · Daniel Kiper

At least *.rej and perhaps *.orig are indicative of a failed patch application, I think I want them to appear in "git status".

By way of comparison Linux's .gitignore includes *.orig but not *.rej and Qemu's includes neither.

So nack to the addition of *.rej from me. I'm more or less ambivalent

Quoted 9 lines
Quoted 4 lines

about

Quoted 2 lines

*.orig.

I have to agree. You should clean up *.rej *.orig after fixing conflicts

Thanks for comment on this. Could you review rest of this patchset? I am working on v3 and it will be nice to take your (and others if possible) comments into it.

I will go through them today

Quoted 2 lines

Daniel

#11 lore raw

Re: [PATCH v2 1/6] gitignore: Ignore *.orig, *.rej and *.swp files

Quoted 25 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 24 lines · Daniel Kiper
Quoted 23 lines · Vladimir 'phcoder' Serbinenko
Quoted 4 lines · Ian Campbell

(Having written the below I see too late that this is a grub patch not a Xen one, a tag in the subject for such cross posted patches would be

useful

Quoted 7 lines

please. Anyway, my opinion counts for very little in this context but I leave it below since already I wrote it. I notice that xen.git#.gitignore _does_ list *.rej, which I think is wrong...)

Quoted 1 line · Daniel Kiper
Quoted 7 lines

At least *.rej and perhaps *.orig are indicative of a failed patch application, I think I want them to appear in "git status".

By way of comparison Linux's .gitignore includes *.orig but not *.rej and Qemu's includes neither.

So nack to the addition of *.rej from me. I'm more or less ambivalent

about

Quoted 2 lines

*.orig.

Quoted 7 lines
Quoted 6 lines
Quoted 1 line

I have to agree. You should clean up *.rej *.orig after fixing conflicts

Thanks for comment on this. Could you review rest of this patchset? I am working on v3 and it will be nice to take your (and others if possible) comments into it.

I will go through them today

Thanks a lot!

Daniel

#12 lore raw

Re: [PATCH v2 1/6] gitignore: Ignore *.orig, *.rej and *.swp files

Quoted 28 lines · Daniel Kiper
Quoted 27 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 26 lines · Daniel Kiper
Quoted 24 lines · Vladimir 'phcoder' Serbinenko
Quoted 4 lines · Ian Campbell

(Having written the below I see too late that this is a grub patch not a Xen one, a tag in the subject for such cross posted patches would be

useful

Quoted 7 lines

please. Anyway, my opinion counts for very little in this context but I leave it below since already I wrote it. I notice that xen.git#.gitignore _does_ list *.rej, which I think is wrong...)

Quoted 1 line · Daniel Kiper
Quoted 7 lines

At least *.rej and perhaps *.orig are indicative of a failed patch application, I think I want them to appear in "git status".

By way of comparison Linux's .gitignore includes *.orig but not *.rej and Qemu's includes neither.

So nack to the addition of *.rej from me. I'm more or less ambivalent

about

Quoted 2 lines

*.orig.

I have to agree. You should clean up *.rej *.orig after fixing conflicts

Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

Thanks for comment on this. Could you review rest of this patchset? I am working on v3 and it will be nice to take your (and others if possible) comments into it.

I will go through them today

Thanks a lot!

All reviewed. Some of them already good but they have dependencies. Feel free to either fix concerns with dependencies or rebase in a way to get the good ones committed first in a meaningful way

Quoted 2 lines

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#13 lore raw

Re: [PATCH v2 1/6] gitignore: Ignore *.orig, *.rej and *.swp files

In reply to Daniel Kiper
Quoted 28 lines · Daniel Kiper
Quoted 27 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 26 lines · Daniel Kiper
Quoted 24 lines · Vladimir 'phcoder' Serbinenko
Quoted 4 lines · Ian Campbell

(Having written the below I see too late that this is a grub patch not a Xen one, a tag in the subject for such cross posted patches would be

useful

Quoted 7 lines

please. Anyway, my opinion counts for very little in this context but I leave it below since already I wrote it. I notice that xen.git#.gitignore _does_ list *.rej, which I think is wrong...)

Quoted 1 line · Daniel Kiper
Quoted 7 lines

At least *.rej and perhaps *.orig are indicative of a failed patch application, I think I want them to appear in "git status".

By way of comparison Linux's .gitignore includes *.orig but not *.rej and Qemu's includes neither.

So nack to the addition of *.rej from me. I'm more or less ambivalent

about

Quoted 2 lines

*.orig.

I have to agree. You should clean up *.rej *.orig after fixing conflicts

Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

Thanks for comment on this. Could you review rest of this patchset? I am working on v3 and it will be nice to take your (and others if possible) comments into it.

I will go through them today

Thanks a lot!

All reviewed. Some of them already good but they have dependencies. Feel free to either fix concerns with dependencies or rebase in a way to get the good ones committed first in a meaningful way

Quoted 2 lines

Daniel

#14 lore raw

Re: [PATCH v2 1/6] gitignore: Ignore *.orig, *.rej and *.swp files

In reply to Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 25 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 24 lines · Daniel Kiper
Quoted 23 lines · Vladimir 'phcoder' Serbinenko
Quoted 4 lines · Ian Campbell

(Having written the below I see too late that this is a grub patch not a Xen one, a tag in the subject for such cross posted patches would be

useful

Quoted 7 lines

please. Anyway, my opinion counts for very little in this context but I leave it below since already I wrote it. I notice that xen.git#.gitignore _does_ list *.rej, which I think is wrong...)

Quoted 1 line · Daniel Kiper
Quoted 7 lines

At least *.rej and perhaps *.orig are indicative of a failed patch application, I think I want them to appear in "git status".

By way of comparison Linux's .gitignore includes *.orig but not *.rej and Qemu's includes neither.

So nack to the addition of *.rej from me. I'm more or less ambivalent

about

Quoted 2 lines

*.orig.

Quoted 7 lines
Quoted 6 lines
Quoted 1 line

I have to agree. You should clean up *.rej *.orig after fixing conflicts

Thanks for comment on this. Could you review rest of this patchset? I am working on v3 and it will be nice to take your (and others if possible) comments into it.

I will go through them today

Thanks a lot!

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#15 lore raw

Re: [PATCH v2 1/6] gitignore: Ignore *.orig, *.rej and *.swp files

In reply to Daniel Kiper
Quoted 21 lines · Daniel Kiper
Quoted 20 lines · Vladimir 'phcoder' Serbinenko
Quoted 4 lines · Ian Campbell

(Having written the below I see too late that this is a grub patch not a Xen one, a tag in the subject for such cross posted patches would be

useful

Quoted 14 lines

please. Anyway, my opinion counts for very little in this context but I leave it below since already I wrote it. I notice that xen.git#.gitignore _does_ list *.rej, which I think is wrong...)

Quoted 1 line · Daniel Kiper

At least *.rej and perhaps *.orig are indicative of a failed patch application, I think I want them to appear in "git status".

By way of comparison Linux's .gitignore includes *.orig but not *.rej and Qemu's includes neither.

So nack to the addition of *.rej from me. I'm more or less ambivalent

Quoted 9 lines
Quoted 4 lines

about

Quoted 2 lines

*.orig.

I have to agree. You should clean up *.rej *.orig after fixing conflicts

Thanks for comment on this. Could you review rest of this patchset? I am working on v3 and it will be nice to take your (and others if possible) comments into it.

I will go through them today

Quoted 2 lines

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#16 lore raw

PATCH v2 1/6 gitignore: Ignore *.orig, *.rej and *.swp files

In reply to Daniel Kiper
---
 .gitignore |    3 +++
 1 file changed, 3 insertions(+)
.gitignore +3 -0
index 18ab8e8..6d25d39 100644
@@ -147,6 +147,7 @@ mod-*.c
 missing
 netboot_test
 *.o
+*.orig
 *.a
 ohci_test
 partmap_test
@@ -160,9 +161,11 @@ po/stamp-po
 printf_test
 priority_queue_unit_test
 pseries_test
+*.rej
 stamp-h
 stamp-h1
 stamp-h.in
+*.swp
 symlist.c
 symlist.h
 trigtables.c
Signature · 1 line
1.7.10.4
#17 lore raw

PATCH v2 2/6 relocator: Do not use memory region if its starta is smaller than size

In reply to Daniel Kiper

malloc_in_range() should not use memory region if its starta is smaller than size. Otherwise target wraps around and points to region which is usually not a RAM, e.g.:

loader/multiboot.c:93: segment 0: paddr=0x800000, memsz=0x3f800000, vaddr=0x800000 lib/relocator.c:1241: min_addr = 0x0, max_addr = 0xffffffffffffffff, target = 0x800000 lib/relocator.c:434: trying to allocate in 0x800000-0xffffffffffffffff aligned 0x1 size 0x3f800000 lib/relocator.c:434: trying to allocate in 0x0-0x800000 aligned 0x1 size 0x3f800000 lib/relocator.c:434: trying to allocate in 0x0-0xffffffffffffffff aligned 0x1 size 0x3f800000 lib/relocator.c:1188: allocated: 0xffffffffc07fffff+0x3f800000 lib/relocator.c:1277: allocated 0xffffffffc07fffff/0x800000

---
 grub-core/lib/relocator.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/lib/relocator.c +1 -1
index f759c7f..4eee0c5 100644
@@ -748,7 +748,7 @@ malloc_in_range (struct grub_relocator *rel,
 	      /* Found an usable address.  */
 	      goto found;
 	  }
-	if (isinsidebefore && !isinsideafter && !from_low_priv)
+	if (isinsidebefore && !isinsideafter && !from_low_priv && starta >= size)
 	  {
 	    target = starta - size;
 	    if (target > end - size)
Signature · 1 line
1.7.10.4
#18 lore raw

Re: [PATCH v2 2/6] relocator: Do not use memory region if its starta is smaller than size

Quoted 22 lines · Daniel Kiper

malloc_in_range() should not use memory region if its starta is smaller than size. Otherwise target wraps around and points to region which is usually not a RAM, e.g.:

loader/multiboot.c:93: segment 0: paddr=0x800000, memsz=0x3f800000, vaddr=0x800000 lib/relocator.c:1241: min_addr = 0x0, max_addr = 0xffffffffffffffff, target = 0x800000 lib/relocator.c:434: trying to allocate in 0x800000-0xffffffffffffffff aligned 0x1 size 0x3f800000 lib/relocator.c:434: trying to allocate in 0x0-0x800000 aligned 0x1 size 0x3f800000 lib/relocator.c:434: trying to allocate in 0x0-0xffffffffffffffff aligned 0x1 size 0x3f800000 lib/relocator.c:1188: allocated: 0xffffffffc07fffff+0x3f800000 lib/relocator.c:1277: allocated 0xffffffffc07fffff/0x800000

---
 grub-core/lib/relocator.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/lib/relocator.c +0 -0
index f759c7f..4eee0c5 100644

grub-core/lib/relocator.c malloc_in_range (struct grub_relocator *rel, · line 751

Quoted 5 lines
grub-core/lib/relocator.c +1 -1
@@ -748,7 +748,7 @@ malloc_in_range (struct grub_relocator *rel,
              /* Found an usable address.  */
              goto found;
          }
-       if (isinsidebefore && !isinsideafter && !from_low_priv)
+       if (isinsidebefore && !isinsideafter && !from_low_priv && starta >= size)

That's too late, we need to check end of region on previous iteration. Consider region of 128 bytes, requested size 129 and alignment 256. Than starta still ends up high in memory.

Quoted 11 lines
grub-core/lib/relocator.c +0 -1
          {
            target = starta - size;
            if (target > end - size)
--

1.7.10.4

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#19 lore raw

Re: [PATCH v2 2/6] relocator: Do not use memory region if its starta is smaller than size

In reply to Daniel Kiper
Quoted 22 lines · Daniel Kiper

malloc_in_range() should not use memory region if its starta is smaller than size. Otherwise target wraps around and points to region which is usually not a RAM, e.g.:

loader/multiboot.c:93: segment 0: paddr=0x800000, memsz=0x3f800000, vaddr=0x800000 lib/relocator.c:1241: min_addr = 0x0, max_addr = 0xffffffffffffffff, target = 0x800000 lib/relocator.c:434: trying to allocate in 0x800000-0xffffffffffffffff aligned 0x1 size 0x3f800000 lib/relocator.c:434: trying to allocate in 0x0-0x800000 aligned 0x1 size 0x3f800000 lib/relocator.c:434: trying to allocate in 0x0-0xffffffffffffffff aligned 0x1 size 0x3f800000 lib/relocator.c:1188: allocated: 0xffffffffc07fffff+0x3f800000 lib/relocator.c:1277: allocated 0xffffffffc07fffff/0x800000

---
 grub-core/lib/relocator.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/lib/relocator.c +0 -0
index f759c7f..4eee0c5 100644

grub-core/lib/relocator.c malloc_in_range (struct grub_relocator *rel, · line 751

Quoted 5 lines
grub-core/lib/relocator.c +1 -1
@@ -748,7 +748,7 @@ malloc_in_range (struct grub_relocator *rel,
              /* Found an usable address.  */
              goto found;
          }
-       if (isinsidebefore && !isinsideafter && !from_low_priv)
+       if (isinsidebefore && !isinsideafter && !from_low_priv && starta >= size)

That's too late, we need to check end of region on previous iteration. Consider region of 128 bytes, requested size 129 and alignment 256. Than starta still ends up high in memory.

Quoted 11 lines
grub-core/lib/relocator.c +0 -1
          {
            target = starta - size;
            if (target > end - size)
--

1.7.10.4

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#20 lore raw

Re: [PATCH v2 2/6] relocator: Do not use memory region if its starta is smaller than size

Quoted 18 lines · Andrei Borzenkov
Quoted 17 lines · Daniel Kiper

malloc_in_range() should not use memory region if its starta is smaller than size. Otherwise target wraps around and points to region which is usually not a RAM, e.g.:

loader/multiboot.c:93: segment 0: paddr=0x800000, memsz=0x3f800000, vaddr=0x800000 lib/relocator.c:1241: min_addr = 0x0, max_addr = 0xffffffffffffffff, target = 0x800000 lib/relocator.c:434: trying to allocate in 0x800000-0xffffffffffffffff aligned 0x1 size 0x3f800000 lib/relocator.c:434: trying to allocate in 0x0-0x800000 aligned 0x1 size 0x3f800000 lib/relocator.c:434: trying to allocate in 0x0-0xffffffffffffffff aligned 0x1 size 0x3f800000 lib/relocator.c:1188: allocated: 0xffffffffc07fffff+0x3f800000 lib/relocator.c:1277: allocated 0xffffffffc07fffff/0x800000

---
 grub-core/lib/relocator.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

grub-core/lib/relocator.c malloc_in_range (struct grub_relocator *rel, · line 751

Quoted 10 lines
grub-core/lib/relocator.c +1 -1
index f759c7f..4eee0c5 100644
@@ -748,7 +748,7 @@ malloc_in_range (struct grub_relocator *rel,
              /* Found an usable address.  */
              goto found;
          }
-       if (isinsidebefore && !isinsideafter && !from_low_priv)
+       if (isinsidebefore && !isinsideafter && !from_low_priv && starta >= size)

That's too late, we need to check end of region on previous iteration. Consider region of 128 bytes, requested size 129 and alignment 256. Than starta still ends up high in memory.

Agreed, we need a check earlier. It makes sense to split this block with an if (from_low_priv) as both flows are completely separate and splitting them will make it more readable

Quoted 7 lines
Quoted 7 lines
grub-core/lib/relocator.c +0 -1
          {
            target = starta - size;
            if (target > end - size)
--

1.7.10.4

Quoted 5 lines
Quoted 4 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#21 lore raw

Re: [PATCH v2 2/6] relocator: Do not use memory region if its starta is smaller than size

In reply to Andrei Borzenkov
Quoted 18 lines · Andrei Borzenkov
Quoted 17 lines · Daniel Kiper

malloc_in_range() should not use memory region if its starta is smaller than size. Otherwise target wraps around and points to region which is usually not a RAM, e.g.:

loader/multiboot.c:93: segment 0: paddr=0x800000, memsz=0x3f800000, vaddr=0x800000 lib/relocator.c:1241: min_addr = 0x0, max_addr = 0xffffffffffffffff, target = 0x800000 lib/relocator.c:434: trying to allocate in 0x800000-0xffffffffffffffff aligned 0x1 size 0x3f800000 lib/relocator.c:434: trying to allocate in 0x0-0x800000 aligned 0x1 size 0x3f800000 lib/relocator.c:434: trying to allocate in 0x0-0xffffffffffffffff aligned 0x1 size 0x3f800000 lib/relocator.c:1188: allocated: 0xffffffffc07fffff+0x3f800000 lib/relocator.c:1277: allocated 0xffffffffc07fffff/0x800000

---
 grub-core/lib/relocator.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

grub-core/lib/relocator.c malloc_in_range (struct grub_relocator *rel, · line 751

Quoted 10 lines
grub-core/lib/relocator.c +1 -1
index f759c7f..4eee0c5 100644
@@ -748,7 +748,7 @@ malloc_in_range (struct grub_relocator *rel,
              /* Found an usable address.  */
              goto found;
          }
-       if (isinsidebefore && !isinsideafter && !from_low_priv)
+       if (isinsidebefore && !isinsideafter && !from_low_priv && starta >= size)

That's too late, we need to check end of region on previous iteration. Consider region of 128 bytes, requested size 129 and alignment 256. Than starta still ends up high in memory.

Agreed, we need a check earlier. It makes sense to split this block with an if (from_low_priv) as both flows are completely separate and splitting them will make it more readable

Quoted 12 lines
Quoted 11 lines
grub-core/lib/relocator.c +0 -1
          {
            target = starta - size;
            if (target > end - size)
--

1.7.10.4

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#22 lore raw

Re: [PATCH v2 2/6] relocator: Do not use memory region if its starta is smaller than size

In reply to Daniel Kiper

Applied, thanks

Quoted 23 lines · Daniel Kiper

malloc_in_range() should not use memory region if its starta is smaller than size. Otherwise target wraps around and points to region which is usually not a RAM, e.g.:

loader/multiboot.c:93: segment 0: paddr=0x800000, memsz=0x3f800000, vaddr=0x800000 lib/relocator.c:1241: min_addr = 0x0, max_addr = 0xffffffffffffffff, target = 0x800000 lib/relocator.c:434: trying to allocate in 0x800000-0xffffffffffffffff aligned 0x1 size 0x3f800000 lib/relocator.c:434: trying to allocate in 0x0-0x800000 aligned 0x1 size 0x3f800000 lib/relocator.c:434: trying to allocate in 0x0-0xffffffffffffffff aligned 0x1 size 0x3f800000 lib/relocator.c:1188: allocated: 0xffffffffc07fffff+0x3f800000 lib/relocator.c:1277: allocated 0xffffffffc07fffff/0x800000

---
 grub-core/lib/relocator.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/lib/relocator.c +0 -0
index f759c7f..4eee0c5 100644

grub-core/lib/relocator.c malloc_in_range (struct grub_relocator *rel, · line 754

Quoted 8 lines
grub-core/lib/relocator.c +1 -1
@@ -748,7 +748,7 @@ malloc_in_range (struct grub_relocator *rel,
 	      /* Found an usable address.  */
 	      goto found;
 	  }
-	if (isinsidebefore && !isinsideafter && !from_low_priv)
+	if (isinsidebefore && !isinsideafter && !from_low_priv && starta >= size)
 	  {
 	    target = starta - size;
 	    if (target > end - size)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#23 lore raw

Re: [PATCH v2 2/6] relocator: Do not use memory region if its starta is smaller than size

In reply to Daniel Kiper

Applied, thanks

Quoted 31 lines · Daniel Kiper

malloc_in_range() should not use memory region if its starta is smaller than size. Otherwise target wraps around and points to region which is usually not a RAM, e.g.:

loader/multiboot.c:93: segment 0: paddr=0x800000, memsz=0x3f800000, vaddr=0x800000 lib/relocator.c:1241: min_addr = 0x0, max_addr = 0xffffffffffffffff, target = 0x800000 lib/relocator.c:434: trying to allocate in 0x800000-0xffffffffffffffff aligned 0x1 size 0x3f800000 lib/relocator.c:434: trying to allocate in 0x0-0x800000 aligned 0x1 size 0x3f800000 lib/relocator.c:434: trying to allocate in 0x0-0xffffffffffffffff aligned 0x1 size 0x3f800000 lib/relocator.c:1188: allocated: 0xffffffffc07fffff+0x3f800000 lib/relocator.c:1277: allocated 0xffffffffc07fffff/0x800000

---
 grub-core/lib/relocator.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/lib/relocator.c +1 -1
index f759c7f..4eee0c5 100644
@@ -748,7 +748,7 @@ malloc_in_range (struct grub_relocator *rel,
 	      /* Found an usable address.  */
 	      goto found;
 	  }
-	if (isinsidebefore && !isinsideafter && !from_low_priv)
+	if (isinsidebefore && !isinsideafter && !from_low_priv && starta >= size)
 	  {
 	    target = starta - size;
 	    if (target > end - size)
#24 lore raw

Re: [PATCH v2 2/6] relocator: Do not use memory region if its starta is smaller than size

Quoted 1 line · Vladimir 'φ-coder/phcoder' Serbinenko

Applied, thanks

Thanks but here http://lists.gnu.org/archive/html/grub-devel/2015-07/msg00100.html
Andrei and later here http://lists.gnu.org/archive/html/grub-devel/2015-11/msg00110.html
you expressed concerns that patch is wrong and it should be fixed. Does anything have changed?

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#25 lore raw

Re: [PATCH v2 2/6] relocator: Do not use memory region if its starta is smaller than size

In reply to Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 1 line · Vladimir 'φ-coder/phcoder' Serbinenko

Applied, thanks

Thanks but here http://lists.gnu.org/archive/html/grub-devel/2015-07/msg00100.html
Andrei and later here http://lists.gnu.org/archive/html/grub-devel/2015-11/msg00110.html
you expressed concerns that patch is wrong and it should be fixed. Does anything have changed?

Daniel

#26 lore raw

Re: [PATCH v2 2/6] relocator: Do not use memory region if its starta is smaller than size

Le ven. 12 févr. 2016 20:01, Daniel Kiper <[email protected]> a écrit :

Quoted 12 lines
Quoted 1 line · Vladimir 'φ-coder/phcoder' Serbinenko

Applied, thanks

Thanks but here
http://lists.gnu.org/archive/html/grub-devel/2015-07/msg00100.html
Andrei and later here
http://lists.gnu.org/archive/html/grub-devel/2015-11/msg00110.html
you expressed concerns that patch is wrong and it should be fixed. Does anything have changed?

Quoted 2 lines

Daniel

Yes, I changed your patch before committing

Quoted 1 line

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#27 lore raw

Re: [PATCH v2 2/6] relocator: Do not use memory region if its starta is smaller than size

In reply to Daniel Kiper

Le ven. 12 févr. 2016 20:01, Daniel Kiper <[email protected]> a écrit :

Quoted 12 lines
Quoted 1 line · Vladimir 'φ-coder/phcoder' Serbinenko

Applied, thanks

Thanks but here
http://lists.gnu.org/archive/html/grub-devel/2015-07/msg00100.html
Andrei and later here
http://lists.gnu.org/archive/html/grub-devel/2015-11/msg00110.html
you expressed concerns that patch is wrong and it should be fixed. Does anything have changed?

Quoted 2 lines

Daniel

Yes, I changed your patch before committing

Quoted 1 line
#28 lore raw

Re: [PATCH v2 2/6] relocator: Do not use memory region if its starta is smaller than size

Quoted 15 lines · Vladimir 'phcoder' Serbinenko

Le ven. 12 févr. 2016 20:01, Daniel Kiper <[email protected]> a écrit :

Quoted 12 lines
Quoted 1 line · Vladimir 'φ-coder/phcoder' Serbinenko

Applied, thanks

Thanks but here
http://lists.gnu.org/archive/html/grub-devel/2015-07/msg00100.html
Andrei and later here
http://lists.gnu.org/archive/html/grub-devel/2015-11/msg00110.html
you expressed concerns that patch is wrong and it should be fixed. Does anything have changed?

Quoted 3 lines
Quoted 2 lines

Daniel

Yes, I changed your patch before committing

Great! Thanks a lot!

Daniel

#29 lore raw

Re: [PATCH v2 2/6] relocator: Do not use memory region if its starta is smaller than size

In reply to Vladimir 'phcoder' Serbinenko
Quoted 15 lines · Vladimir 'phcoder' Serbinenko

Le ven. 12 févr. 2016 20:01, Daniel Kiper <[email protected]> a écrit :

Quoted 12 lines
Quoted 1 line · Vladimir 'φ-coder/phcoder' Serbinenko

Applied, thanks

Thanks but here
http://lists.gnu.org/archive/html/grub-devel/2015-07/msg00100.html
Andrei and later here
http://lists.gnu.org/archive/html/grub-devel/2015-11/msg00110.html
you expressed concerns that patch is wrong and it should be fixed. Does anything have changed?

Quoted 3 lines
Quoted 2 lines

Daniel

Yes, I changed your patch before committing

Great! Thanks a lot!

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#30 lore raw

PATCH v2 2/6 relocator: Do not use memory region if its starta is smaller than size

In reply to Daniel Kiper

malloc_in_range() should not use memory region if its starta is smaller than size. Otherwise target wraps around and points to region which is usually not a RAM, e.g.:

loader/multiboot.c:93: segment 0: paddr=0x800000, memsz=0x3f800000, vaddr=0x800000 lib/relocator.c:1241: min_addr = 0x0, max_addr = 0xffffffffffffffff, target = 0x800000 lib/relocator.c:434: trying to allocate in 0x800000-0xffffffffffffffff aligned 0x1 size 0x3f800000 lib/relocator.c:434: trying to allocate in 0x0-0x800000 aligned 0x1 size 0x3f800000 lib/relocator.c:434: trying to allocate in 0x0-0xffffffffffffffff aligned 0x1 size 0x3f800000 lib/relocator.c:1188: allocated: 0xffffffffc07fffff+0x3f800000 lib/relocator.c:1277: allocated 0xffffffffc07fffff/0x800000

---
 grub-core/lib/relocator.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/lib/relocator.c +1 -1
index f759c7f..4eee0c5 100644
@@ -748,7 +748,7 @@ malloc_in_range (struct grub_relocator *rel,
 	      /* Found an usable address.  */
 	      goto found;
 	  }
-	if (isinsidebefore && !isinsideafter && !from_low_priv)
+	if (isinsidebefore && !isinsideafter && !from_low_priv && starta >= size)
 	  {
 	    target = starta - size;
 	    if (target > end - size)
Signature · 1 line
1.7.10.4
#31 lore raw

PATCH v2 3/6 i386/relocator: Add grub_relocator64_efi relocator

In reply to Daniel Kiper

Add grub_relocator64_efi relocator. It will be used on EFI 64-bit platforms when multiboot2 compatible image requests MULTIBOOT_TAG_TYPE_EFI_BS. Relocator will set lower parts of %rax and %rbx accordingly to multiboot2 specification. On the other hand processor mode, just before jumping into loaded image, will be set accordingly to Unified Extensible Firmware Interface Specification, Version 2.4 Errata B, section 2.3.4, x64 Platforms, boot services. This way loaded image will be able to use EFI boot services without any issues.

If idea is accepted I will prepare grub_relocator32_efi relocator too.

---
 grub-core/Makefile.core.def          |    1 +
 grub-core/lib/i386/relocator.c       |   53 +++++++++++++++++++++++
 grub-core/lib/i386/relocator64_efi.S |   77 ++++++++++++++++++++++++++++++++++
 grub-core/loader/multiboot.c         |   29 +++++++++++--
 grub-core/loader/multiboot_mbi2.c    |   19 +++++++--
 include/grub/i386/multiboot.h        |   11 +++++
 include/grub/i386/relocator.h        |   21 ++++++++++
 include/multiboot2.h                 |    9 ++++
 8 files changed, 213 insertions(+), 7 deletions(-)
 create mode 100644 grub-core/lib/i386/relocator64_efi.S
grub-core/Makefile.core.def +1 -0
index a6101de..d583549 100644
@@ -1519,6 +1519,7 @@ module = {
   x86 = lib/i386/relocator_common_c.c;
   ieee1275 = lib/ieee1275/relocator.c;
   efi = lib/efi/relocator.c;
+  x86_64_efi = lib/i386/relocator64_efi.S;
   mips = lib/mips/relocator_asm.S;
   mips = lib/mips/relocator.c;
   powerpc = lib/powerpc/relocator_asm.S;
grub-core/lib/i386/relocator.c +53 -0

62 lines not on this page · raw message

grub-core/lib/i386/relocator64_efi.S new file +77 -0

77 lines not on this page · raw message

grub-core/loader/multiboot.c +26 -3
index fd8f28e..ca7154f 100644
@@ -118,21 +118,44 @@ grub_multiboot_set_video_mode (void)
   return err;
 }
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+#define grub_relocator_efi_boot	grub_relocator64_efi_boot
+#endif
+#endif
+
 static grub_err_t
 grub_multiboot_boot (void)
 {
   grub_err_t err;
+  grub_uint32_t target;
   struct grub_relocator32_state state = MULTIBOOT_INITIAL_STATE;
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+  struct grub_relocator64_efi_state state_efi = MULTIBOOT_EFI_INITIAL_STATE;
+#endif
+#endif
 
-  state.MULTIBOOT_ENTRY_REGISTER = grub_multiboot_payload_eip;
-
-  err = grub_multiboot_make_mbi (&state.MULTIBOOT_MBI_REGISTER);
+  err = grub_multiboot_make_mbi (&target);
 
   if (err)
     return err;
 
+  state.MULTIBOOT_ENTRY_REGISTER = grub_multiboot_payload_eip;
+  state.MULTIBOOT_MBI_REGISTER = target;
+
 #if defined (__i386__) || defined (__x86_64__)
+#ifdef GRUB_MACHINE_EFI
+  state_efi.MULTIBOOT_EFI_ENTRY_REGISTER = grub_multiboot_payload_eip;
+  state_efi.MULTIBOOT_EFI_MBI_REGISTER = target;
+
+  if (grub_efi_is_finished)
+    grub_relocator32_boot (grub_multiboot_relocator, state, 0);
+  else
+    grub_relocator_efi_boot (grub_multiboot_relocator, state_efi);
+#else
   grub_relocator32_boot (grub_multiboot_relocator, state, 0);
+#endif
 #else
   grub_relocator32_boot (grub_multiboot_relocator, state);
 #endif
grub-core/loader/multiboot_mbi2.c +15 -4
index d7c19bc..8d66e3f 100644
@@ -107,8 +107,8 @@ grub_multiboot_load (grub_file_t file, const char *filename)
   grub_err_t err;
   struct multiboot_header_tag *tag;
   struct multiboot_header_tag_address *addr_tag = NULL;
-  int entry_specified = 0;
-  grub_addr_t entry = 0;
+  int entry_specified = 0, efi_entry_specified = 0;
+  grub_addr_t entry = 0, efi_entry = 0;
   grub_uint32_t console_required = 0;
   struct multiboot_header_tag_framebuffer *fbtag = NULL;
   int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
@@ -192,6 +192,13 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	entry = ((struct multiboot_header_tag_entry_address *) tag)->entry_addr;
 	break;
 
+      case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64:
+#if defined (GRUB_MACHINE_EFI) && defined (__x86_64__)
+	efi_entry_specified = 1;
+	efi_entry = ((struct multiboot_header_tag_entry_address_efi64 *) tag)->entry_addr;
+#endif
+	break;
+
       case MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS:
 	if (!(((struct multiboot_header_tag_console_flags *) tag)->console_flags
 	    & MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED))
@@ -211,7 +218,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	break;
 
       case MULTIBOOT_HEADER_TAG_EFI_BS:
+#ifdef GRUB_MACHINE_EFI
 	keep_bs = 1;
+#endif
 	break;
 
       default:
@@ -224,7 +233,7 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	break;
       }
 
-  if (addr_tag && !entry_specified)
+  if (addr_tag && !entry_specified && !(keep_bs && efi_entry_specified))
     {
       grub_free (buffer);
       return grub_error (GRUB_ERR_UNKNOWN_OS,
@@ -287,7 +296,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	}
     }
 
-  if (entry_specified)
+  if (keep_bs && efi_entry_specified)
+    grub_multiboot_payload_eip = efi_entry;
+  else if (entry_specified)
     grub_multiboot_payload_eip = entry;
 
   if (fbtag)
include/grub/i386/multiboot.h +11 -0
index a1b9488..807a1de 100644
@@ -30,6 +30,17 @@
 #define MULTIBOOT_MBI_REGISTER ebx
 #define MULTIBOOT_ARCHITECTURE_CURRENT MULTIBOOT_ARCHITECTURE_I386
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+#define MULTIBOOT_EFI_INITIAL_STATE  { .rax = MULTIBOOT_BOOTLOADER_MAGIC,	\
+    .rcx = 0,									\
+    .rdx = 0,									\
+      }
+#define MULTIBOOT_EFI_ENTRY_REGISTER rip
+#define MULTIBOOT_EFI_MBI_REGISTER rbx
+#endif
+#endif
+
 #define MULTIBOOT_ELF32_MACHINE EM_386
 #define MULTIBOOT_ELF64_MACHINE EM_X86_64
 
include/grub/i386/relocator.h +21 -0
index 5f89a7e..2a56c3b 100644
@@ -65,6 +65,20 @@ struct grub_relocator64_state
   grub_addr_t cr3;
 };
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+struct grub_relocator64_efi_state
+{
+  grub_uint64_t rax;
+  grub_uint64_t rbx;
+  grub_uint64_t rcx;
+  grub_uint64_t rdx;
+  grub_uint64_t rip;
+  grub_uint64_t rsi;
+};
+#endif
+#endif
+
 grub_err_t grub_relocator16_boot (struct grub_relocator *rel,
 				  struct grub_relocator16_state state);
 
@@ -76,4 +90,11 @@ grub_err_t grub_relocator64_boot (struct grub_relocator *rel,
 				  struct grub_relocator64_state state,
 				  grub_addr_t min_addr, grub_addr_t max_addr);
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+grub_err_t grub_relocator64_efi_boot (struct grub_relocator *rel,
+				      struct grub_relocator64_efi_state state);
+#endif
+#endif
+
 #endif /* ! GRUB_RELOCATOR_CPU_HEADER */
include/multiboot2.h +9 -0
index 9d48627..b3977e3 100644
@@ -69,6 +69,7 @@
 #define MULTIBOOT_HEADER_TAG_FRAMEBUFFER  5
 #define MULTIBOOT_HEADER_TAG_MODULE_ALIGN  6
 #define MULTIBOOT_HEADER_TAG_EFI_BS  7
+#define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64  9
 
 #define MULTIBOOT_ARCHITECTURE_I386  0
 #define MULTIBOOT_ARCHITECTURE_MIPS32  4
@@ -133,6 +134,14 @@ struct multiboot_header_tag_entry_address
   multiboot_uint32_t entry_addr;
 };
 
+struct multiboot_header_tag_entry_address_efi64
+{
+  multiboot_uint16_t type;
+  multiboot_uint16_t flags;
+  multiboot_uint32_t size;
+  multiboot_uint32_t entry_addr;
+};
+
 struct multiboot_header_tag_console_flags
 {
   multiboot_uint16_t type;
Signature · 1 line
1.7.10.4
#32 lore raw

PATCH v2 3/6 i386/relocator: Add grub_relocator64_efi relocator

In reply to Daniel Kiper

Add grub_relocator64_efi relocator. It will be used on EFI 64-bit platforms when multiboot2 compatible image requests MULTIBOOT_TAG_TYPE_EFI_BS. Relocator will set lower parts of %rax and %rbx accordingly to multiboot2 specification. On the other hand processor mode, just before jumping into loaded image, will be set accordingly to Unified Extensible Firmware Interface Specification, Version 2.4 Errata B, section 2.3.4, x64 Platforms, boot services. This way loaded image will be able to use EFI boot services without any issues.

If idea is accepted I will prepare grub_relocator32_efi relocator too.

---
 grub-core/Makefile.core.def          |    1 +
 grub-core/lib/i386/relocator.c       |   53 +++++++++++++++++++++++
 grub-core/lib/i386/relocator64_efi.S |   77 ++++++++++++++++++++++++++++++++++
 grub-core/loader/multiboot.c         |   29 +++++++++++--
 grub-core/loader/multiboot_mbi2.c    |   19 +++++++--
 include/grub/i386/multiboot.h        |   11 +++++
 include/grub/i386/relocator.h        |   21 ++++++++++
 include/multiboot2.h                 |    9 ++++
 8 files changed, 213 insertions(+), 7 deletions(-)
 create mode 100644 grub-core/lib/i386/relocator64_efi.S
grub-core/Makefile.core.def +1 -0
index a6101de..d583549 100644
@@ -1519,6 +1519,7 @@ module = {
   x86 = lib/i386/relocator_common_c.c;
   ieee1275 = lib/ieee1275/relocator.c;
   efi = lib/efi/relocator.c;
+  x86_64_efi = lib/i386/relocator64_efi.S;
   mips = lib/mips/relocator_asm.S;
   mips = lib/mips/relocator.c;
   powerpc = lib/powerpc/relocator_asm.S;
grub-core/lib/i386/relocator.c +53 -0

62 lines not on this page · raw message

grub-core/lib/i386/relocator64_efi.S new file +77 -0

77 lines not on this page · raw message

grub-core/loader/multiboot.c +26 -3
index fd8f28e..ca7154f 100644
@@ -118,21 +118,44 @@ grub_multiboot_set_video_mode (void)
   return err;
 }
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+#define grub_relocator_efi_boot	grub_relocator64_efi_boot
+#endif
+#endif
+
 static grub_err_t
 grub_multiboot_boot (void)
 {
   grub_err_t err;
+  grub_uint32_t target;
   struct grub_relocator32_state state = MULTIBOOT_INITIAL_STATE;
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+  struct grub_relocator64_efi_state state_efi = MULTIBOOT_EFI_INITIAL_STATE;
+#endif
+#endif
 
-  state.MULTIBOOT_ENTRY_REGISTER = grub_multiboot_payload_eip;
-
-  err = grub_multiboot_make_mbi (&state.MULTIBOOT_MBI_REGISTER);
+  err = grub_multiboot_make_mbi (&target);
 
   if (err)
     return err;
 
+  state.MULTIBOOT_ENTRY_REGISTER = grub_multiboot_payload_eip;
+  state.MULTIBOOT_MBI_REGISTER = target;
+
 #if defined (__i386__) || defined (__x86_64__)
+#ifdef GRUB_MACHINE_EFI
+  state_efi.MULTIBOOT_EFI_ENTRY_REGISTER = grub_multiboot_payload_eip;
+  state_efi.MULTIBOOT_EFI_MBI_REGISTER = target;
+
+  if (grub_efi_is_finished)
+    grub_relocator32_boot (grub_multiboot_relocator, state, 0);
+  else
+    grub_relocator_efi_boot (grub_multiboot_relocator, state_efi);
+#else
   grub_relocator32_boot (grub_multiboot_relocator, state, 0);
+#endif
 #else
   grub_relocator32_boot (grub_multiboot_relocator, state);
 #endif
grub-core/loader/multiboot_mbi2.c +15 -4
index d7c19bc..8d66e3f 100644
@@ -107,8 +107,8 @@ grub_multiboot_load (grub_file_t file, const char *filename)
   grub_err_t err;
   struct multiboot_header_tag *tag;
   struct multiboot_header_tag_address *addr_tag = NULL;
-  int entry_specified = 0;
-  grub_addr_t entry = 0;
+  int entry_specified = 0, efi_entry_specified = 0;
+  grub_addr_t entry = 0, efi_entry = 0;
   grub_uint32_t console_required = 0;
   struct multiboot_header_tag_framebuffer *fbtag = NULL;
   int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
@@ -192,6 +192,13 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	entry = ((struct multiboot_header_tag_entry_address *) tag)->entry_addr;
 	break;
 
+      case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64:
+#if defined (GRUB_MACHINE_EFI) && defined (__x86_64__)
+	efi_entry_specified = 1;
+	efi_entry = ((struct multiboot_header_tag_entry_address_efi64 *) tag)->entry_addr;
+#endif
+	break;
+
       case MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS:
 	if (!(((struct multiboot_header_tag_console_flags *) tag)->console_flags
 	    & MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED))
@@ -211,7 +218,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	break;
 
       case MULTIBOOT_HEADER_TAG_EFI_BS:
+#ifdef GRUB_MACHINE_EFI
 	keep_bs = 1;
+#endif
 	break;
 
       default:
@@ -224,7 +233,7 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	break;
       }
 
-  if (addr_tag && !entry_specified)
+  if (addr_tag && !entry_specified && !(keep_bs && efi_entry_specified))
     {
       grub_free (buffer);
       return grub_error (GRUB_ERR_UNKNOWN_OS,
@@ -287,7 +296,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	}
     }
 
-  if (entry_specified)
+  if (keep_bs && efi_entry_specified)
+    grub_multiboot_payload_eip = efi_entry;
+  else if (entry_specified)
     grub_multiboot_payload_eip = entry;
 
   if (fbtag)
include/grub/i386/multiboot.h +11 -0
index a1b9488..807a1de 100644
@@ -30,6 +30,17 @@
 #define MULTIBOOT_MBI_REGISTER ebx
 #define MULTIBOOT_ARCHITECTURE_CURRENT MULTIBOOT_ARCHITECTURE_I386
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+#define MULTIBOOT_EFI_INITIAL_STATE  { .rax = MULTIBOOT_BOOTLOADER_MAGIC,	\
+    .rcx = 0,									\
+    .rdx = 0,									\
+      }
+#define MULTIBOOT_EFI_ENTRY_REGISTER rip
+#define MULTIBOOT_EFI_MBI_REGISTER rbx
+#endif
+#endif
+
 #define MULTIBOOT_ELF32_MACHINE EM_386
 #define MULTIBOOT_ELF64_MACHINE EM_X86_64
 
include/grub/i386/relocator.h +21 -0
index 5f89a7e..2a56c3b 100644
@@ -65,6 +65,20 @@ struct grub_relocator64_state
   grub_addr_t cr3;
 };
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+struct grub_relocator64_efi_state
+{
+  grub_uint64_t rax;
+  grub_uint64_t rbx;
+  grub_uint64_t rcx;
+  grub_uint64_t rdx;
+  grub_uint64_t rip;
+  grub_uint64_t rsi;
+};
+#endif
+#endif
+
 grub_err_t grub_relocator16_boot (struct grub_relocator *rel,
 				  struct grub_relocator16_state state);
 
@@ -76,4 +90,11 @@ grub_err_t grub_relocator64_boot (struct grub_relocator *rel,
 				  struct grub_relocator64_state state,
 				  grub_addr_t min_addr, grub_addr_t max_addr);
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+grub_err_t grub_relocator64_efi_boot (struct grub_relocator *rel,
+				      struct grub_relocator64_efi_state state);
+#endif
+#endif
+
 #endif /* ! GRUB_RELOCATOR_CPU_HEADER */
include/multiboot2.h +9 -0
index 9d48627..b3977e3 100644
@@ -69,6 +69,7 @@
 #define MULTIBOOT_HEADER_TAG_FRAMEBUFFER  5
 #define MULTIBOOT_HEADER_TAG_MODULE_ALIGN  6
 #define MULTIBOOT_HEADER_TAG_EFI_BS  7
+#define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64  9
 
 #define MULTIBOOT_ARCHITECTURE_I386  0
 #define MULTIBOOT_ARCHITECTURE_MIPS32  4
@@ -133,6 +134,14 @@ struct multiboot_header_tag_entry_address
   multiboot_uint32_t entry_addr;
 };
 
+struct multiboot_header_tag_entry_address_efi64
+{
+  multiboot_uint16_t type;
+  multiboot_uint16_t flags;
+  multiboot_uint32_t size;
+  multiboot_uint32_t entry_addr;
+};
+
 struct multiboot_header_tag_console_flags
 {
   multiboot_uint16_t type;
Signature · 1 line
1.7.10.4
#33 lore raw

Re: [PATCH v2 3/6] i386/relocator: Add grub_relocator64_efi relocator

Quoted 7 lines · Daniel Kiper

Add grub_relocator64_efi relocator. It will be used on EFI 64-bit platforms when multiboot2 compatible image requests MULTIBOOT_TAG_TYPE_EFI_BS. Relocator will set lower parts of %rax and %rbx accordingly to multiboot2 specification. On the other hand processor mode, just before jumping into loaded image, will be set accordingly to Unified Extensible Firmware Interface Specification, Version 2.4 Errata B, section 2.3.4, x64 Platforms, boot services. This way loaded image will be able to use EFI boot services without any issues.

Andrei, Valdimir: ping?

Quoted 379 lines

If idea is accepted I will prepare grub_relocator32_efi relocator too.

---
 grub-core/Makefile.core.def          |    1 +
 grub-core/lib/i386/relocator.c       |   53 +++++++++++++++++++++++
 grub-core/lib/i386/relocator64_efi.S |   77 ++++++++++++++++++++++++++++++++++
 grub-core/loader/multiboot.c         |   29 +++++++++++--
 grub-core/loader/multiboot_mbi2.c    |   19 +++++++--
 include/grub/i386/multiboot.h        |   11 +++++
 include/grub/i386/relocator.h        |   21 ++++++++++
 include/multiboot2.h                 |    9 ++++
 8 files changed, 213 insertions(+), 7 deletions(-)
 create mode 100644 grub-core/lib/i386/relocator64_efi.S
grub-core/Makefile.core.def +1 -0
index a6101de..d583549 100644
@@ -1519,6 +1519,7 @@ module = {
   x86 = lib/i386/relocator_common_c.c;
   ieee1275 = lib/ieee1275/relocator.c;
   efi = lib/efi/relocator.c;
+  x86_64_efi = lib/i386/relocator64_efi.S;
   mips = lib/mips/relocator_asm.S;
   mips = lib/mips/relocator.c;
   powerpc = lib/powerpc/relocator_asm.S;
grub-core/lib/i386/relocator.c +53 -0
index 71dd4f0..459027e 100644
@@ -69,6 +69,19 @@ extern grub_uint64_t grub_relocator64_rsi;
 extern grub_addr_t grub_relocator64_cr3;
 extern struct grub_i386_idt grub_relocator16_idt;
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+extern grub_uint8_t grub_relocator64_efi_start;
+extern grub_uint8_t grub_relocator64_efi_end;
+extern grub_uint64_t grub_relocator64_efi_rax;
+extern grub_uint64_t grub_relocator64_efi_rbx;
+extern grub_uint64_t grub_relocator64_efi_rcx;
+extern grub_uint64_t grub_relocator64_efi_rdx;
+extern grub_uint64_t grub_relocator64_efi_rip;
+extern grub_uint64_t grub_relocator64_efi_rsi;
+#endif
+#endif
+
 #define RELOCATOR_SIZEOF(x)	(&grub_relocator##x##_end - &grub_relocator##x##_start)
 
 grub_err_t
@@ -214,3 +227,43 @@ grub_relocator64_boot (struct grub_relocator *rel,
   /* Not reached.  */
   return GRUB_ERR_NONE;
 }
+
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+grub_err_t
+grub_relocator64_efi_boot (struct grub_relocator *rel,
+			   struct grub_relocator64_efi_state state)
+{
+  grub_err_t err;
+  void *relst;
+  grub_relocator_chunk_t ch;
+
+  err = grub_relocator_alloc_chunk_align (rel, &ch, 0,
+					  0x40000000 - RELOCATOR_SIZEOF (64_efi),
+					  RELOCATOR_SIZEOF (64_efi), 16,
+					  GRUB_RELOCATOR_PREFERENCE_NONE, 1);
+  if (err)
+    return err;
+
+  grub_relocator64_efi_rax = state.rax;
+  grub_relocator64_efi_rbx = state.rbx;
+  grub_relocator64_efi_rcx = state.rcx;
+  grub_relocator64_efi_rdx = state.rdx;
+  grub_relocator64_efi_rip = state.rip;
+  grub_relocator64_efi_rsi = state.rsi;
+
+  grub_memmove (get_virtual_current_address (ch), &grub_relocator64_efi_start,
+		RELOCATOR_SIZEOF (64_efi));
+
+  err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch),
+				       &relst, NULL);
+  if (err)
+    return err;
+
+  ((void (*) (void)) relst) ();
+
+  /* Not reached.  */
+  return GRUB_ERR_NONE;
+}
+#endif
+#endif
grub-core/lib/i386/relocator64_efi.S new file +77 -0
new file mode 100644
index 0000000..fcd1964
@@ -0,0 +1,77 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2009,2010  Free Software Foundation, Inc.
+ *  Copyright (C) 2014,2015  Oracle Co.
+ *      Author: Daniel Kiper
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "relocator_common.S"
+
+	.p2align	4	/* force 16-byte alignment */
+
+VARIABLE(grub_relocator64_efi_start)
+	PREAMBLE
+
+	.code64
+
+	/* mov imm64, %rax */
+	.byte 	0x48
+	.byte	0xb8
+VARIABLE(grub_relocator64_efi_rsi)
+	.quad	0
+
+	movq	%rax, %rsi
+
+	/* mov imm64, %rax */
+	.byte 	0x48
+	.byte	0xb8
+VARIABLE(grub_relocator64_efi_rax)
+	.quad	0
+
+	/* mov imm64, %rbx */
+	.byte 	0x48
+	.byte	0xbb
+VARIABLE(grub_relocator64_efi_rbx)
+	.quad	0
+
+	/* mov imm64, %rcx */
+	.byte 	0x48
+	.byte	0xb9
+VARIABLE(grub_relocator64_efi_rcx)
+	.quad	0
+
+	/* mov imm64, %rdx */
+	.byte 	0x48
+	.byte	0xba
+VARIABLE(grub_relocator64_efi_rdx)
+	.quad	0
+
+	/* Cleared direction flag is of no problem with any current
+	   payload and makes this implementation easier.  */
+	cld
+
+#ifdef __APPLE__
+	.byte 0xff, 0x25
+	.quad 0
+#else
+	jmp *LOCAL(jump_addr) (%rip)
+#endif
+
+LOCAL(jump_addr):
+VARIABLE(grub_relocator64_efi_rip)
+	.quad	0
+
+VARIABLE(grub_relocator64_efi_end)
grub-core/loader/multiboot.c +26 -3
index fd8f28e..ca7154f 100644
@@ -118,21 +118,44 @@ grub_multiboot_set_video_mode (void)
   return err;
 }
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+#define grub_relocator_efi_boot	grub_relocator64_efi_boot
+#endif
+#endif
+
 static grub_err_t
 grub_multiboot_boot (void)
 {
   grub_err_t err;
+  grub_uint32_t target;
   struct grub_relocator32_state state = MULTIBOOT_INITIAL_STATE;
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+  struct grub_relocator64_efi_state state_efi = MULTIBOOT_EFI_INITIAL_STATE;
+#endif
+#endif
 
-  state.MULTIBOOT_ENTRY_REGISTER = grub_multiboot_payload_eip;
-
-  err = grub_multiboot_make_mbi (&state.MULTIBOOT_MBI_REGISTER);
+  err = grub_multiboot_make_mbi (&target);
 
   if (err)
     return err;
 
+  state.MULTIBOOT_ENTRY_REGISTER = grub_multiboot_payload_eip;
+  state.MULTIBOOT_MBI_REGISTER = target;
+
 #if defined (__i386__) || defined (__x86_64__)
+#ifdef GRUB_MACHINE_EFI
+  state_efi.MULTIBOOT_EFI_ENTRY_REGISTER = grub_multiboot_payload_eip;
+  state_efi.MULTIBOOT_EFI_MBI_REGISTER = target;
+
+  if (grub_efi_is_finished)
+    grub_relocator32_boot (grub_multiboot_relocator, state, 0);
+  else
+    grub_relocator_efi_boot (grub_multiboot_relocator, state_efi);
+#else
   grub_relocator32_boot (grub_multiboot_relocator, state, 0);
+#endif
 #else
   grub_relocator32_boot (grub_multiboot_relocator, state);
 #endif
grub-core/loader/multiboot_mbi2.c +15 -4
index d7c19bc..8d66e3f 100644
@@ -107,8 +107,8 @@ grub_multiboot_load (grub_file_t file, const char *filename)
   grub_err_t err;
   struct multiboot_header_tag *tag;
   struct multiboot_header_tag_address *addr_tag = NULL;
-  int entry_specified = 0;
-  grub_addr_t entry = 0;
+  int entry_specified = 0, efi_entry_specified = 0;
+  grub_addr_t entry = 0, efi_entry = 0;
   grub_uint32_t console_required = 0;
   struct multiboot_header_tag_framebuffer *fbtag = NULL;
   int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
@@ -192,6 +192,13 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	entry = ((struct multiboot_header_tag_entry_address *) tag)->entry_addr;
 	break;
 
+      case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64:
+#if defined (GRUB_MACHINE_EFI) && defined (__x86_64__)
+	efi_entry_specified = 1;
+	efi_entry = ((struct multiboot_header_tag_entry_address_efi64 *) tag)->entry_addr;
+#endif
+	break;
+
       case MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS:
 	if (!(((struct multiboot_header_tag_console_flags *) tag)->console_flags
 	    & MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED))
@@ -211,7 +218,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	break;
 
       case MULTIBOOT_HEADER_TAG_EFI_BS:
+#ifdef GRUB_MACHINE_EFI
 	keep_bs = 1;
+#endif
 	break;
 
       default:
@@ -224,7 +233,7 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	break;
       }
 
-  if (addr_tag && !entry_specified)
+  if (addr_tag && !entry_specified && !(keep_bs && efi_entry_specified))
     {
       grub_free (buffer);
       return grub_error (GRUB_ERR_UNKNOWN_OS,
@@ -287,7 +296,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	}
     }
 
-  if (entry_specified)
+  if (keep_bs && efi_entry_specified)
+    grub_multiboot_payload_eip = efi_entry;
+  else if (entry_specified)
     grub_multiboot_payload_eip = entry;
 
   if (fbtag)
include/grub/i386/multiboot.h +11 -0
index a1b9488..807a1de 100644
@@ -30,6 +30,17 @@
 #define MULTIBOOT_MBI_REGISTER ebx
 #define MULTIBOOT_ARCHITECTURE_CURRENT MULTIBOOT_ARCHITECTURE_I386
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+#define MULTIBOOT_EFI_INITIAL_STATE  { .rax = MULTIBOOT_BOOTLOADER_MAGIC,	\
+    .rcx = 0,									\
+    .rdx = 0,									\
+      }
+#define MULTIBOOT_EFI_ENTRY_REGISTER rip
+#define MULTIBOOT_EFI_MBI_REGISTER rbx
+#endif
+#endif
+
 #define MULTIBOOT_ELF32_MACHINE EM_386
 #define MULTIBOOT_ELF64_MACHINE EM_X86_64
 
include/grub/i386/relocator.h +21 -0
index 5f89a7e..2a56c3b 100644
@@ -65,6 +65,20 @@ struct grub_relocator64_state
   grub_addr_t cr3;
 };
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+struct grub_relocator64_efi_state
+{
+  grub_uint64_t rax;
+  grub_uint64_t rbx;
+  grub_uint64_t rcx;
+  grub_uint64_t rdx;
+  grub_uint64_t rip;
+  grub_uint64_t rsi;
+};
+#endif
+#endif
+
 grub_err_t grub_relocator16_boot (struct grub_relocator *rel,
 				  struct grub_relocator16_state state);
 
@@ -76,4 +90,11 @@ grub_err_t grub_relocator64_boot (struct grub_relocator *rel,
 				  struct grub_relocator64_state state,
 				  grub_addr_t min_addr, grub_addr_t max_addr);
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+grub_err_t grub_relocator64_efi_boot (struct grub_relocator *rel,
+				      struct grub_relocator64_efi_state state);
+#endif
+#endif
+
 #endif /* ! GRUB_RELOCATOR_CPU_HEADER */
include/multiboot2.h +9 -0
index 9d48627..b3977e3 100644
@@ -69,6 +69,7 @@
 #define MULTIBOOT_HEADER_TAG_FRAMEBUFFER  5
 #define MULTIBOOT_HEADER_TAG_MODULE_ALIGN  6
 #define MULTIBOOT_HEADER_TAG_EFI_BS  7
+#define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64  9
 
 #define MULTIBOOT_ARCHITECTURE_I386  0
 #define MULTIBOOT_ARCHITECTURE_MIPS32  4
@@ -133,6 +134,14 @@ struct multiboot_header_tag_entry_address
   multiboot_uint32_t entry_addr;
 };
 
+struct multiboot_header_tag_entry_address_efi64
+{
+  multiboot_uint16_t type;
+  multiboot_uint16_t flags;
+  multiboot_uint32_t size;
+  multiboot_uint32_t entry_addr;
+};
+
 struct multiboot_header_tag_console_flags
 {
   multiboot_uint16_t type;
Signature · 1 line
1.7.10.4
#34 lore raw

Re: [PATCH v2 3/6] i386/relocator: Add grub_relocator64_efi relocator

In reply to Daniel Kiper
Quoted 175 lines · Daniel Kiper

Add grub_relocator64_efi relocator. It will be used on EFI 64-bit platforms when multiboot2 compatible image requests MULTIBOOT_TAG_TYPE_EFI_BS. Relocator will set lower parts of %rax and %rbx accordingly to multiboot2 specification. On the other hand processor mode, just before jumping into loaded image, will be set accordingly to Unified Extensible Firmware Interface Specification, Version 2.4 Errata B, section 2.3.4, x64 Platforms, boot services. This way loaded image will be able to use EFI boot services without any issues.

If idea is accepted I will prepare grub_relocator32_efi relocator too.

---
 grub-core/Makefile.core.def          |    1 +
 grub-core/lib/i386/relocator.c       |   53 +++++++++++++++++++++++
 grub-core/lib/i386/relocator64_efi.S |   77 ++++++++++++++++++++++++++++++++++
 grub-core/loader/multiboot.c         |   29 +++++++++++--
 grub-core/loader/multiboot_mbi2.c    |   19 +++++++--
 include/grub/i386/multiboot.h        |   11 +++++
 include/grub/i386/relocator.h        |   21 ++++++++++
 include/multiboot2.h                 |    9 ++++
 8 files changed, 213 insertions(+), 7 deletions(-)
 create mode 100644 grub-core/lib/i386/relocator64_efi.S
grub-core/Makefile.core.def +1 -0
index a6101de..d583549 100644
@@ -1519,6 +1519,7 @@ module = {
   x86 = lib/i386/relocator_common_c.c;
   ieee1275 = lib/ieee1275/relocator.c;
   efi = lib/efi/relocator.c;
+  x86_64_efi = lib/i386/relocator64_efi.S;
   mips = lib/mips/relocator_asm.S;
   mips = lib/mips/relocator.c;
   powerpc = lib/powerpc/relocator_asm.S;
grub-core/lib/i386/relocator.c +53 -0
index 71dd4f0..459027e 100644
@@ -69,6 +69,19 @@ extern grub_uint64_t grub_relocator64_rsi;
 extern grub_addr_t grub_relocator64_cr3;
 extern struct grub_i386_idt grub_relocator16_idt;
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+extern grub_uint8_t grub_relocator64_efi_start;
+extern grub_uint8_t grub_relocator64_efi_end;
+extern grub_uint64_t grub_relocator64_efi_rax;
+extern grub_uint64_t grub_relocator64_efi_rbx;
+extern grub_uint64_t grub_relocator64_efi_rcx;
+extern grub_uint64_t grub_relocator64_efi_rdx;
+extern grub_uint64_t grub_relocator64_efi_rip;
+extern grub_uint64_t grub_relocator64_efi_rsi;
+#endif
+#endif
+
 #define RELOCATOR_SIZEOF(x)	(&grub_relocator##x##_end - &grub_relocator##x##_start)
 
 grub_err_t
@@ -214,3 +227,43 @@ grub_relocator64_boot (struct grub_relocator *rel,
   /* Not reached.  */
   return GRUB_ERR_NONE;
 }
+
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+grub_err_t
+grub_relocator64_efi_boot (struct grub_relocator *rel,
+			   struct grub_relocator64_efi_state state)
+{
+  grub_err_t err;
+  void *relst;
+  grub_relocator_chunk_t ch;
+
+  err = grub_relocator_alloc_chunk_align (rel, &ch, 0,
+					  0x40000000 - RELOCATOR_SIZEOF (64_efi),
+					  RELOCATOR_SIZEOF (64_efi), 16,
+					  GRUB_RELOCATOR_PREFERENCE_NONE, 1);
+  if (err)
+    return err;
+
+  grub_relocator64_efi_rax = state.rax;
+  grub_relocator64_efi_rbx = state.rbx;
+  grub_relocator64_efi_rcx = state.rcx;
+  grub_relocator64_efi_rdx = state.rdx;
+  grub_relocator64_efi_rip = state.rip;
+  grub_relocator64_efi_rsi = state.rsi;
+
+  grub_memmove (get_virtual_current_address (ch), &grub_relocator64_efi_start,
+		RELOCATOR_SIZEOF (64_efi));
+
+  err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch),
+				       &relst, NULL);
+  if (err)
+    return err;
+
+  ((void (*) (void)) relst) ();
+
+  /* Not reached.  */
+  return GRUB_ERR_NONE;
+}
+#endif
+#endif
grub-core/lib/i386/relocator64_efi.S new file +66 -0
new file mode 100644
index 0000000..fcd1964
@@ -0,0 +1,77 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2009,2010  Free Software Foundation, Inc.
+ *  Copyright (C) 2014,2015  Oracle Co.
+ *      Author: Daniel Kiper
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "relocator_common.S"
+
+	.p2align	4	/* force 16-byte alignment */
+
+VARIABLE(grub_relocator64_efi_start)
+	PREAMBLE
+
+	.code64
+
+	/* mov imm64, %rax */
+	.byte 	0x48
+	.byte	0xb8
+VARIABLE(grub_relocator64_efi_rsi)
+	.quad	0
+
+	movq	%rax, %rsi
+
+	/* mov imm64, %rax */
+	.byte 	0x48
+	.byte	0xb8
+VARIABLE(grub_relocator64_efi_rax)
+	.quad	0
+
+	/* mov imm64, %rbx */
+	.byte 	0x48
+	.byte	0xbb
+VARIABLE(grub_relocator64_efi_rbx)
+	.quad	0
+
+	/* mov imm64, %rcx */
+	.byte 	0x48
+	.byte	0xb9
+VARIABLE(grub_relocator64_efi_rcx)
+	.quad	0
+
+	/* mov imm64, %rdx */
+	.byte 	0x48
+	.byte	0xba
+VARIABLE(grub_relocator64_efi_rdx)
+	.quad	0
+
+	/* Cleared direction flag is of no problem with any current
+	   payload and makes this implementation easier.  */
+	cld
+
+#ifdef __APPLE__

grub-core/lib/i386/relocator64_efi.S line 76

Quoted 10 lines
grub-core/lib/i386/relocator64_efi.S new file +10 -0
+	.byte 0xff, 0x25
+	.quad 0
+#else
+	jmp *LOCAL(jump_addr) (%rip)
+#endif
+
+LOCAL(jump_addr):
+VARIABLE(grub_relocator64_efi_rip)
+	.quad	0
+

This repeats relocator64 almost exactly. Can we avoid code duplication? It's fine to compile it twice but code duplication is bad.

Quoted 43 lines
grub-core/lib/i386/relocator64_efi.S new file +1 -0
+VARIABLE(grub_relocator64_efi_end)
grub-core/loader/multiboot.c +20 -3
index fd8f28e..ca7154f 100644
@@ -118,21 +118,44 @@ grub_multiboot_set_video_mode (void)
   return err;
 }
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+#define grub_relocator_efi_boot	grub_relocator64_efi_boot
+#endif
+#endif
+
 static grub_err_t
 grub_multiboot_boot (void)
 {
   grub_err_t err;
+  grub_uint32_t target;
   struct grub_relocator32_state state = MULTIBOOT_INITIAL_STATE;
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+  struct grub_relocator64_efi_state state_efi = MULTIBOOT_EFI_INITIAL_STATE;
+#endif
+#endif
 
-  state.MULTIBOOT_ENTRY_REGISTER = grub_multiboot_payload_eip;
-
-  err = grub_multiboot_make_mbi (&state.MULTIBOOT_MBI_REGISTER);
+  err = grub_multiboot_make_mbi (&target);
 
   if (err)
     return err;
 
+  state.MULTIBOOT_ENTRY_REGISTER = grub_multiboot_payload_eip;
+  state.MULTIBOOT_MBI_REGISTER = target;
+
 #if defined (__i386__) || defined (__x86_64__)
+#ifdef GRUB_MACHINE_EFI
+  state_efi.MULTIBOOT_EFI_ENTRY_REGISTER = grub_multiboot_payload_eip;
+  state_efi.MULTIBOOT_EFI_MBI_REGISTER = target;
+

grub-core/loader/multiboot.c grub_multiboot_set_video_mode (void) · line 161

Quoted 10 lines
grub-core/loader/multiboot.c +6 -0
+  if (grub_efi_is_finished)
+    grub_relocator32_boot (grub_multiboot_relocator, state, 0);
+  else
+    grub_relocator_efi_boot (grub_multiboot_relocator, state_efi);
+#else
   grub_relocator32_boot (grub_multiboot_relocator, state, 0);
+#endif
 #else
   grub_relocator32_boot (grub_multiboot_relocator, state);
 #endif

This becomes hairy. I think it's time to split it into platform-specific functions and/or use tricks like
#ifndef GRUB_MACHINE_EFI
#define grub_efi_is_finished 0
#endif

Quoted 16 lines
grub-core/loader/multiboot_mbi2.c +2 -2
index d7c19bc..8d66e3f 100644
@@ -107,8 +107,8 @@ grub_multiboot_load (grub_file_t file, const char *filename)
   grub_err_t err;
   struct multiboot_header_tag *tag;
   struct multiboot_header_tag_address *addr_tag = NULL;
-  int entry_specified = 0;
-  grub_addr_t entry = 0;
+  int entry_specified = 0, efi_entry_specified = 0;
+  grub_addr_t entry = 0, efi_entry = 0;
   grub_uint32_t console_required = 0;
   struct multiboot_header_tag_framebuffer *fbtag = NULL;
   int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char *filename) · line 201

Quoted 10 lines
grub-core/loader/multiboot_mbi2.c +7 -0
@@ -192,6 +192,13 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	entry = ((struct multiboot_header_tag_entry_address *) tag)->entry_addr;
 	break;
 
+      case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64:
+#if defined (GRUB_MACHINE_EFI) && defined (__x86_64__)
+	efi_entry_specified = 1;
+	efi_entry = ((struct multiboot_header_tag_entry_address_efi64 *) tag)->entry_addr;
+#endif
+	break;
+

Why do we need separate handling of EFI entry point? Why can't we use the same structure?

Quoted 23 lines
grub-core/loader/multiboot_mbi2.c +3 -1
       case MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS:
 	if (!(((struct multiboot_header_tag_console_flags *) tag)->console_flags
 	    & MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED))
@@ -211,7 +218,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	break;
 
       case MULTIBOOT_HEADER_TAG_EFI_BS:
+#ifdef GRUB_MACHINE_EFI
 	keep_bs = 1;
+#endif
 	break;
 
       default:
@@ -224,7 +233,7 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	break;
       }
 
-  if (addr_tag && !entry_specified)
+  if (addr_tag && !entry_specified && !(keep_bs && efi_entry_specified))
     {
       grub_free (buffer);
       return grub_error (GRUB_ERR_UNKNOWN_OS,

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char *filename) · line 303

Quoted 9 lines
grub-core/loader/multiboot_mbi2.c +3 -1
@@ -287,7 +296,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	}
     }
 
-  if (entry_specified)
+  if (keep_bs && efi_entry_specified)
+    grub_multiboot_payload_eip = efi_entry;
+  else if (entry_specified)
     grub_multiboot_payload_eip = entry;
 

This seems redundant.

Quoted 74 lines
grub-core/loader/multiboot_mbi2.c +0 -0
   if (fbtag)
include/grub/i386/multiboot.h +11 -0
index a1b9488..807a1de 100644
@@ -30,6 +30,17 @@
 #define MULTIBOOT_MBI_REGISTER ebx
 #define MULTIBOOT_ARCHITECTURE_CURRENT MULTIBOOT_ARCHITECTURE_I386
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+#define MULTIBOOT_EFI_INITIAL_STATE  { .rax = MULTIBOOT_BOOTLOADER_MAGIC,	\
+    .rcx = 0,									\
+    .rdx = 0,									\
+      }
+#define MULTIBOOT_EFI_ENTRY_REGISTER rip
+#define MULTIBOOT_EFI_MBI_REGISTER rbx
+#endif
+#endif
+
 #define MULTIBOOT_ELF32_MACHINE EM_386
 #define MULTIBOOT_ELF64_MACHINE EM_X86_64
 
include/grub/i386/relocator.h +21 -0
index 5f89a7e..2a56c3b 100644
@@ -65,6 +65,20 @@ struct grub_relocator64_state
   grub_addr_t cr3;
 };
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+struct grub_relocator64_efi_state
+{
+  grub_uint64_t rax;
+  grub_uint64_t rbx;
+  grub_uint64_t rcx;
+  grub_uint64_t rdx;
+  grub_uint64_t rip;
+  grub_uint64_t rsi;
+};
+#endif
+#endif
+
 grub_err_t grub_relocator16_boot (struct grub_relocator *rel,
 				  struct grub_relocator16_state state);
 
@@ -76,4 +90,11 @@ grub_err_t grub_relocator64_boot (struct grub_relocator *rel,
 				  struct grub_relocator64_state state,
 				  grub_addr_t min_addr, grub_addr_t max_addr);
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+grub_err_t grub_relocator64_efi_boot (struct grub_relocator *rel,
+				      struct grub_relocator64_efi_state state);
+#endif
+#endif
+
 #endif /* ! GRUB_RELOCATOR_CPU_HEADER */
include/multiboot2.h +1 -0
index 9d48627..b3977e3 100644
@@ -69,6 +69,7 @@
 #define MULTIBOOT_HEADER_TAG_FRAMEBUFFER  5
 #define MULTIBOOT_HEADER_TAG_MODULE_ALIGN  6
 #define MULTIBOOT_HEADER_TAG_EFI_BS  7
+#define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64  9
 
 #define MULTIBOOT_ARCHITECTURE_I386  0
 #define MULTIBOOT_ARCHITECTURE_MIPS32  4
@@ -133,6 +134,14 @@ struct multiboot_header_tag_entry_address
   multiboot_uint32_t entry_addr;

include/multiboot2.h struct multiboot_header_tag_entry_address · line 144

Quoted 10 lines
include/multiboot2.h +8 -0
 };
 
+struct multiboot_header_tag_entry_address_efi64
+{
+  multiboot_uint16_t type;
+  multiboot_uint16_t flags;
+  multiboot_uint32_t size;
+  multiboot_uint32_t entry_addr;
+};
+

Ditto

include/multiboot2.h struct multiboot_header_tag_entry_address · line 147

Quoted 4 lines
include/multiboot2.h +0 -0
 struct multiboot_header_tag_console_flags
 {
   multiboot_uint16_t type;

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#35 lore raw

Re: [PATCH v2 3/6] i386/relocator: Add grub_relocator64_efi relocator

In reply to Daniel Kiper
Quoted 175 lines · Daniel Kiper

Add grub_relocator64_efi relocator. It will be used on EFI 64-bit platforms when multiboot2 compatible image requests MULTIBOOT_TAG_TYPE_EFI_BS. Relocator will set lower parts of %rax and %rbx accordingly to multiboot2 specification. On the other hand processor mode, just before jumping into loaded image, will be set accordingly to Unified Extensible Firmware Interface Specification, Version 2.4 Errata B, section 2.3.4, x64 Platforms, boot services. This way loaded image will be able to use EFI boot services without any issues.

If idea is accepted I will prepare grub_relocator32_efi relocator too.

---
 grub-core/Makefile.core.def          |    1 +
 grub-core/lib/i386/relocator.c       |   53 +++++++++++++++++++++++
 grub-core/lib/i386/relocator64_efi.S |   77 ++++++++++++++++++++++++++++++++++
 grub-core/loader/multiboot.c         |   29 +++++++++++--
 grub-core/loader/multiboot_mbi2.c    |   19 +++++++--
 include/grub/i386/multiboot.h        |   11 +++++
 include/grub/i386/relocator.h        |   21 ++++++++++
 include/multiboot2.h                 |    9 ++++
 8 files changed, 213 insertions(+), 7 deletions(-)
 create mode 100644 grub-core/lib/i386/relocator64_efi.S
grub-core/Makefile.core.def +1 -0
index a6101de..d583549 100644
@@ -1519,6 +1519,7 @@ module = {
   x86 = lib/i386/relocator_common_c.c;
   ieee1275 = lib/ieee1275/relocator.c;
   efi = lib/efi/relocator.c;
+  x86_64_efi = lib/i386/relocator64_efi.S;
   mips = lib/mips/relocator_asm.S;
   mips = lib/mips/relocator.c;
   powerpc = lib/powerpc/relocator_asm.S;
grub-core/lib/i386/relocator.c +53 -0
index 71dd4f0..459027e 100644
@@ -69,6 +69,19 @@ extern grub_uint64_t grub_relocator64_rsi;
 extern grub_addr_t grub_relocator64_cr3;
 extern struct grub_i386_idt grub_relocator16_idt;
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+extern grub_uint8_t grub_relocator64_efi_start;
+extern grub_uint8_t grub_relocator64_efi_end;
+extern grub_uint64_t grub_relocator64_efi_rax;
+extern grub_uint64_t grub_relocator64_efi_rbx;
+extern grub_uint64_t grub_relocator64_efi_rcx;
+extern grub_uint64_t grub_relocator64_efi_rdx;
+extern grub_uint64_t grub_relocator64_efi_rip;
+extern grub_uint64_t grub_relocator64_efi_rsi;
+#endif
+#endif
+
 #define RELOCATOR_SIZEOF(x)	(&grub_relocator##x##_end - &grub_relocator##x##_start)
 
 grub_err_t
@@ -214,3 +227,43 @@ grub_relocator64_boot (struct grub_relocator *rel,
   /* Not reached.  */
   return GRUB_ERR_NONE;
 }
+
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+grub_err_t
+grub_relocator64_efi_boot (struct grub_relocator *rel,
+			   struct grub_relocator64_efi_state state)
+{
+  grub_err_t err;
+  void *relst;
+  grub_relocator_chunk_t ch;
+
+  err = grub_relocator_alloc_chunk_align (rel, &ch, 0,
+					  0x40000000 - RELOCATOR_SIZEOF (64_efi),
+					  RELOCATOR_SIZEOF (64_efi), 16,
+					  GRUB_RELOCATOR_PREFERENCE_NONE, 1);
+  if (err)
+    return err;
+
+  grub_relocator64_efi_rax = state.rax;
+  grub_relocator64_efi_rbx = state.rbx;
+  grub_relocator64_efi_rcx = state.rcx;
+  grub_relocator64_efi_rdx = state.rdx;
+  grub_relocator64_efi_rip = state.rip;
+  grub_relocator64_efi_rsi = state.rsi;
+
+  grub_memmove (get_virtual_current_address (ch), &grub_relocator64_efi_start,
+		RELOCATOR_SIZEOF (64_efi));
+
+  err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch),
+				       &relst, NULL);
+  if (err)
+    return err;
+
+  ((void (*) (void)) relst) ();
+
+  /* Not reached.  */
+  return GRUB_ERR_NONE;
+}
+#endif
+#endif
grub-core/lib/i386/relocator64_efi.S new file +66 -0
new file mode 100644
index 0000000..fcd1964
@@ -0,0 +1,77 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2009,2010  Free Software Foundation, Inc.
+ *  Copyright (C) 2014,2015  Oracle Co.
+ *      Author: Daniel Kiper
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "relocator_common.S"
+
+	.p2align	4	/* force 16-byte alignment */
+
+VARIABLE(grub_relocator64_efi_start)
+	PREAMBLE
+
+	.code64
+
+	/* mov imm64, %rax */
+	.byte 	0x48
+	.byte	0xb8
+VARIABLE(grub_relocator64_efi_rsi)
+	.quad	0
+
+	movq	%rax, %rsi
+
+	/* mov imm64, %rax */
+	.byte 	0x48
+	.byte	0xb8
+VARIABLE(grub_relocator64_efi_rax)
+	.quad	0
+
+	/* mov imm64, %rbx */
+	.byte 	0x48
+	.byte	0xbb
+VARIABLE(grub_relocator64_efi_rbx)
+	.quad	0
+
+	/* mov imm64, %rcx */
+	.byte 	0x48
+	.byte	0xb9
+VARIABLE(grub_relocator64_efi_rcx)
+	.quad	0
+
+	/* mov imm64, %rdx */
+	.byte 	0x48
+	.byte	0xba
+VARIABLE(grub_relocator64_efi_rdx)
+	.quad	0
+
+	/* Cleared direction flag is of no problem with any current
+	   payload and makes this implementation easier.  */
+	cld
+
+#ifdef __APPLE__

grub-core/lib/i386/relocator64_efi.S line 76

Quoted 10 lines
grub-core/lib/i386/relocator64_efi.S new file +10 -0
+	.byte 0xff, 0x25
+	.quad 0
+#else
+	jmp *LOCAL(jump_addr) (%rip)
+#endif
+
+LOCAL(jump_addr):
+VARIABLE(grub_relocator64_efi_rip)
+	.quad	0
+

This repeats relocator64 almost exactly. Can we avoid code duplication? It's fine to compile it twice but code duplication is bad.

Quoted 43 lines
grub-core/lib/i386/relocator64_efi.S new file +1 -0
+VARIABLE(grub_relocator64_efi_end)
grub-core/loader/multiboot.c +20 -3
index fd8f28e..ca7154f 100644
@@ -118,21 +118,44 @@ grub_multiboot_set_video_mode (void)
   return err;
 }
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+#define grub_relocator_efi_boot	grub_relocator64_efi_boot
+#endif
+#endif
+
 static grub_err_t
 grub_multiboot_boot (void)
 {
   grub_err_t err;
+  grub_uint32_t target;
   struct grub_relocator32_state state = MULTIBOOT_INITIAL_STATE;
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+  struct grub_relocator64_efi_state state_efi = MULTIBOOT_EFI_INITIAL_STATE;
+#endif
+#endif
 
-  state.MULTIBOOT_ENTRY_REGISTER = grub_multiboot_payload_eip;
-
-  err = grub_multiboot_make_mbi (&state.MULTIBOOT_MBI_REGISTER);
+  err = grub_multiboot_make_mbi (&target);
 
   if (err)
     return err;
 
+  state.MULTIBOOT_ENTRY_REGISTER = grub_multiboot_payload_eip;
+  state.MULTIBOOT_MBI_REGISTER = target;
+
 #if defined (__i386__) || defined (__x86_64__)
+#ifdef GRUB_MACHINE_EFI
+  state_efi.MULTIBOOT_EFI_ENTRY_REGISTER = grub_multiboot_payload_eip;
+  state_efi.MULTIBOOT_EFI_MBI_REGISTER = target;
+

grub-core/loader/multiboot.c grub_multiboot_set_video_mode (void) · line 161

Quoted 10 lines
grub-core/loader/multiboot.c +6 -0
+  if (grub_efi_is_finished)
+    grub_relocator32_boot (grub_multiboot_relocator, state, 0);
+  else
+    grub_relocator_efi_boot (grub_multiboot_relocator, state_efi);
+#else
   grub_relocator32_boot (grub_multiboot_relocator, state, 0);
+#endif
 #else
   grub_relocator32_boot (grub_multiboot_relocator, state);
 #endif

This becomes hairy. I think it's time to split it into platform-specific functions and/or use tricks like
#ifndef GRUB_MACHINE_EFI
#define grub_efi_is_finished 0
#endif

Quoted 16 lines
grub-core/loader/multiboot_mbi2.c +2 -2
index d7c19bc..8d66e3f 100644
@@ -107,8 +107,8 @@ grub_multiboot_load (grub_file_t file, const char *filename)
   grub_err_t err;
   struct multiboot_header_tag *tag;
   struct multiboot_header_tag_address *addr_tag = NULL;
-  int entry_specified = 0;
-  grub_addr_t entry = 0;
+  int entry_specified = 0, efi_entry_specified = 0;
+  grub_addr_t entry = 0, efi_entry = 0;
   grub_uint32_t console_required = 0;
   struct multiboot_header_tag_framebuffer *fbtag = NULL;
   int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char *filename) · line 201

Quoted 10 lines
grub-core/loader/multiboot_mbi2.c +7 -0
@@ -192,6 +192,13 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	entry = ((struct multiboot_header_tag_entry_address *) tag)->entry_addr;
 	break;
 
+      case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64:
+#if defined (GRUB_MACHINE_EFI) && defined (__x86_64__)
+	efi_entry_specified = 1;
+	efi_entry = ((struct multiboot_header_tag_entry_address_efi64 *) tag)->entry_addr;
+#endif
+	break;
+

Why do we need separate handling of EFI entry point? Why can't we use the same structure?

Quoted 23 lines
grub-core/loader/multiboot_mbi2.c +3 -1
       case MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS:
 	if (!(((struct multiboot_header_tag_console_flags *) tag)->console_flags
 	    & MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED))
@@ -211,7 +218,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	break;
 
       case MULTIBOOT_HEADER_TAG_EFI_BS:
+#ifdef GRUB_MACHINE_EFI
 	keep_bs = 1;
+#endif
 	break;
 
       default:
@@ -224,7 +233,7 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	break;
       }
 
-  if (addr_tag && !entry_specified)
+  if (addr_tag && !entry_specified && !(keep_bs && efi_entry_specified))
     {
       grub_free (buffer);
       return grub_error (GRUB_ERR_UNKNOWN_OS,

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char *filename) · line 303

Quoted 9 lines
grub-core/loader/multiboot_mbi2.c +3 -1
@@ -287,7 +296,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	}
     }
 
-  if (entry_specified)
+  if (keep_bs && efi_entry_specified)
+    grub_multiboot_payload_eip = efi_entry;
+  else if (entry_specified)
     grub_multiboot_payload_eip = entry;
 

This seems redundant.

Quoted 74 lines
grub-core/loader/multiboot_mbi2.c +0 -0
   if (fbtag)
include/grub/i386/multiboot.h +11 -0
index a1b9488..807a1de 100644
@@ -30,6 +30,17 @@
 #define MULTIBOOT_MBI_REGISTER ebx
 #define MULTIBOOT_ARCHITECTURE_CURRENT MULTIBOOT_ARCHITECTURE_I386
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+#define MULTIBOOT_EFI_INITIAL_STATE  { .rax = MULTIBOOT_BOOTLOADER_MAGIC,	\
+    .rcx = 0,									\
+    .rdx = 0,									\
+      }
+#define MULTIBOOT_EFI_ENTRY_REGISTER rip
+#define MULTIBOOT_EFI_MBI_REGISTER rbx
+#endif
+#endif
+
 #define MULTIBOOT_ELF32_MACHINE EM_386
 #define MULTIBOOT_ELF64_MACHINE EM_X86_64
 
include/grub/i386/relocator.h +21 -0
index 5f89a7e..2a56c3b 100644
@@ -65,6 +65,20 @@ struct grub_relocator64_state
   grub_addr_t cr3;
 };
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+struct grub_relocator64_efi_state
+{
+  grub_uint64_t rax;
+  grub_uint64_t rbx;
+  grub_uint64_t rcx;
+  grub_uint64_t rdx;
+  grub_uint64_t rip;
+  grub_uint64_t rsi;
+};
+#endif
+#endif
+
 grub_err_t grub_relocator16_boot (struct grub_relocator *rel,
 				  struct grub_relocator16_state state);
 
@@ -76,4 +90,11 @@ grub_err_t grub_relocator64_boot (struct grub_relocator *rel,
 				  struct grub_relocator64_state state,
 				  grub_addr_t min_addr, grub_addr_t max_addr);
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+grub_err_t grub_relocator64_efi_boot (struct grub_relocator *rel,
+				      struct grub_relocator64_efi_state state);
+#endif
+#endif
+
 #endif /* ! GRUB_RELOCATOR_CPU_HEADER */
include/multiboot2.h +1 -0
index 9d48627..b3977e3 100644
@@ -69,6 +69,7 @@
 #define MULTIBOOT_HEADER_TAG_FRAMEBUFFER  5
 #define MULTIBOOT_HEADER_TAG_MODULE_ALIGN  6
 #define MULTIBOOT_HEADER_TAG_EFI_BS  7
+#define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64  9
 
 #define MULTIBOOT_ARCHITECTURE_I386  0
 #define MULTIBOOT_ARCHITECTURE_MIPS32  4
@@ -133,6 +134,14 @@ struct multiboot_header_tag_entry_address
   multiboot_uint32_t entry_addr;

include/multiboot2.h struct multiboot_header_tag_entry_address · line 144

Quoted 10 lines
include/multiboot2.h +8 -0
 };
 
+struct multiboot_header_tag_entry_address_efi64
+{
+  multiboot_uint16_t type;
+  multiboot_uint16_t flags;
+  multiboot_uint32_t size;
+  multiboot_uint32_t entry_addr;
+};
+

Ditto

Quoted 4 lines
include/multiboot2.h +0 -0
 struct multiboot_header_tag_console_flags
 {
   multiboot_uint16_t type;
#36 lore raw

Re: [PATCH v2 3/6] i386/relocator: Add grub_relocator64_efi relocator

Quoted 180 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 179 lines · Daniel Kiper

Add grub_relocator64_efi relocator. It will be used on EFI 64-bit platforms when multiboot2 compatible image requests MULTIBOOT_TAG_TYPE_EFI_BS. Relocator will set lower parts of %rax and %rbx accordingly to multiboot2 specification. On the other hand processor mode, just before jumping into loaded image, will be set accordingly to Unified Extensible Firmware Interface Specification, Version 2.4 Errata B, section 2.3.4, x64 Platforms, boot services. This way loaded image will be able to use EFI boot services without any issues.

If idea is accepted I will prepare grub_relocator32_efi relocator too.

---
 grub-core/Makefile.core.def          |    1 +
 grub-core/lib/i386/relocator.c       |   53 +++++++++++++++++++++++
 grub-core/lib/i386/relocator64_efi.S |   77 ++++++++++++++++++++++++++++++++++
 grub-core/loader/multiboot.c         |   29 +++++++++++--
 grub-core/loader/multiboot_mbi2.c    |   19 +++++++--
 include/grub/i386/multiboot.h        |   11 +++++
 include/grub/i386/relocator.h        |   21 ++++++++++
 include/multiboot2.h                 |    9 ++++
 8 files changed, 213 insertions(+), 7 deletions(-)
 create mode 100644 grub-core/lib/i386/relocator64_efi.S
grub-core/Makefile.core.def +1 -0
index a6101de..d583549 100644
@@ -1519,6 +1519,7 @@ module = {
   x86 = lib/i386/relocator_common_c.c;
   ieee1275 = lib/ieee1275/relocator.c;
   efi = lib/efi/relocator.c;
+  x86_64_efi = lib/i386/relocator64_efi.S;
   mips = lib/mips/relocator_asm.S;
   mips = lib/mips/relocator.c;
   powerpc = lib/powerpc/relocator_asm.S;
grub-core/lib/i386/relocator.c +53 -0
index 71dd4f0..459027e 100644
@@ -69,6 +69,19 @@ extern grub_uint64_t grub_relocator64_rsi;
 extern grub_addr_t grub_relocator64_cr3;
 extern struct grub_i386_idt grub_relocator16_idt;
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+extern grub_uint8_t grub_relocator64_efi_start;
+extern grub_uint8_t grub_relocator64_efi_end;
+extern grub_uint64_t grub_relocator64_efi_rax;
+extern grub_uint64_t grub_relocator64_efi_rbx;
+extern grub_uint64_t grub_relocator64_efi_rcx;
+extern grub_uint64_t grub_relocator64_efi_rdx;
+extern grub_uint64_t grub_relocator64_efi_rip;
+extern grub_uint64_t grub_relocator64_efi_rsi;
+#endif
+#endif
+
 #define RELOCATOR_SIZEOF(x)	(&grub_relocator##x##_end - &grub_relocator##x##_start)
 
 grub_err_t
@@ -214,3 +227,43 @@ grub_relocator64_boot (struct grub_relocator *rel,
   /* Not reached.  */
   return GRUB_ERR_NONE;
 }
+
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+grub_err_t
+grub_relocator64_efi_boot (struct grub_relocator *rel,
+			   struct grub_relocator64_efi_state state)
+{
+  grub_err_t err;
+  void *relst;
+  grub_relocator_chunk_t ch;
+
+  err = grub_relocator_alloc_chunk_align (rel, &ch, 0,
+					  0x40000000 - RELOCATOR_SIZEOF (64_efi),
+					  RELOCATOR_SIZEOF (64_efi), 16,
+					  GRUB_RELOCATOR_PREFERENCE_NONE, 1);
+  if (err)
+    return err;
+
+  grub_relocator64_efi_rax = state.rax;
+  grub_relocator64_efi_rbx = state.rbx;
+  grub_relocator64_efi_rcx = state.rcx;
+  grub_relocator64_efi_rdx = state.rdx;
+  grub_relocator64_efi_rip = state.rip;
+  grub_relocator64_efi_rsi = state.rsi;
+
+  grub_memmove (get_virtual_current_address (ch), &grub_relocator64_efi_start,
+		RELOCATOR_SIZEOF (64_efi));
+
+  err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch),
+				       &relst, NULL);
+  if (err)
+    return err;
+
+  ((void (*) (void)) relst) ();
+
+  /* Not reached.  */
+  return GRUB_ERR_NONE;
+}
+#endif
+#endif
grub-core/lib/i386/relocator64_efi.S new file +70 -0
new file mode 100644
index 0000000..fcd1964
@@ -0,0 +1,77 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2009,2010  Free Software Foundation, Inc.
+ *  Copyright (C) 2014,2015  Oracle Co.
+ *      Author: Daniel Kiper
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "relocator_common.S"
+
+	.p2align	4	/* force 16-byte alignment */
+
+VARIABLE(grub_relocator64_efi_start)
+	PREAMBLE
+
+	.code64
+
+	/* mov imm64, %rax */
+	.byte 	0x48
+	.byte	0xb8
+VARIABLE(grub_relocator64_efi_rsi)
+	.quad	0
+
+	movq	%rax, %rsi
+
+	/* mov imm64, %rax */
+	.byte 	0x48
+	.byte	0xb8
+VARIABLE(grub_relocator64_efi_rax)
+	.quad	0
+
+	/* mov imm64, %rbx */
+	.byte 	0x48
+	.byte	0xbb
+VARIABLE(grub_relocator64_efi_rbx)
+	.quad	0
+
+	/* mov imm64, %rcx */
+	.byte 	0x48
+	.byte	0xb9
+VARIABLE(grub_relocator64_efi_rcx)
+	.quad	0
+
+	/* mov imm64, %rdx */
+	.byte 	0x48
+	.byte	0xba
+VARIABLE(grub_relocator64_efi_rdx)
+	.quad	0
+
+	/* Cleared direction flag is of no problem with any current
+	   payload and makes this implementation easier.  */
+	cld
+
+#ifdef __APPLE__
+	.byte 0xff, 0x25
+	.quad 0
+#else
+	jmp *LOCAL(jump_addr) (%rip)
Quoted 8 lines

grub-core/lib/i386/relocator64_efi.S line 76

Quoted 6 lines
grub-core/lib/i386/relocator64_efi.S new file +6 -0
+#endif
+
+LOCAL(jump_addr):
+VARIABLE(grub_relocator64_efi_rip)
+	.quad	0
+

This repeats relocator64 almost exactly. Can we avoid code duplication? It's fine to compile it twice but code duplication is bad.

I will try to do that.

Quoted 50 lines
Quoted 50 lines
grub-core/lib/i386/relocator64_efi.S new file +1 -0
+VARIABLE(grub_relocator64_efi_end)
grub-core/loader/multiboot.c +26 -3
index fd8f28e..ca7154f 100644
@@ -118,21 +118,44 @@ grub_multiboot_set_video_mode (void)
   return err;
 }
 
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+#define grub_relocator_efi_boot	grub_relocator64_efi_boot
+#endif
+#endif
+
 static grub_err_t
 grub_multiboot_boot (void)
 {
   grub_err_t err;
+  grub_uint32_t target;
   struct grub_relocator32_state state = MULTIBOOT_INITIAL_STATE;
+#ifdef GRUB_MACHINE_EFI
+#ifdef __x86_64__
+  struct grub_relocator64_efi_state state_efi = MULTIBOOT_EFI_INITIAL_STATE;
+#endif
+#endif
 
-  state.MULTIBOOT_ENTRY_REGISTER = grub_multiboot_payload_eip;
-
-  err = grub_multiboot_make_mbi (&state.MULTIBOOT_MBI_REGISTER);
+  err = grub_multiboot_make_mbi (&target);
 
   if (err)
     return err;
 
+  state.MULTIBOOT_ENTRY_REGISTER = grub_multiboot_payload_eip;
+  state.MULTIBOOT_MBI_REGISTER = target;
+
 #if defined (__i386__) || defined (__x86_64__)
+#ifdef GRUB_MACHINE_EFI
+  state_efi.MULTIBOOT_EFI_ENTRY_REGISTER = grub_multiboot_payload_eip;
+  state_efi.MULTIBOOT_EFI_MBI_REGISTER = target;
+
+  if (grub_efi_is_finished)
+    grub_relocator32_boot (grub_multiboot_relocator, state, 0);
+  else
+    grub_relocator_efi_boot (grub_multiboot_relocator, state_efi);
+#else
   grub_relocator32_boot (grub_multiboot_relocator, state, 0);
+#endif
Quoted 8 lines

grub-core/loader/multiboot.c grub_multiboot_set_video_mode (void) · line 161

Quoted 3 lines
grub-core/loader/multiboot.c +0 -0
 #else
   grub_relocator32_boot (grub_multiboot_relocator, state);
 #endif

This becomes hairy. I think it's time to split it into platform-specific functions and/or use tricks like
#ifndef GRUB_MACHINE_EFI
#define grub_efi_is_finished 0
#endif

OK.

Quoted 19 lines
Quoted 19 lines
grub-core/loader/multiboot_mbi2.c +2 -2
index d7c19bc..8d66e3f 100644
@@ -107,8 +107,8 @@ grub_multiboot_load (grub_file_t file, const char *filename)
   grub_err_t err;
   struct multiboot_header_tag *tag;
   struct multiboot_header_tag_address *addr_tag = NULL;
-  int entry_specified = 0;
-  grub_addr_t entry = 0;
+  int entry_specified = 0, efi_entry_specified = 0;
+  grub_addr_t entry = 0, efi_entry = 0;
   grub_uint32_t console_required = 0;
   struct multiboot_header_tag_framebuffer *fbtag = NULL;
   int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
@@ -192,6 +192,13 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	entry = ((struct multiboot_header_tag_entry_address *) tag)->entry_addr;
 	break;
 
Quoted 9 lines

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char *filename) · line 201

Quoted 7 lines
grub-core/loader/multiboot_mbi2.c +7 -0
+      case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64:
+#if defined (GRUB_MACHINE_EFI) && defined (__x86_64__)
+	efi_entry_specified = 1;
+	efi_entry = ((struct multiboot_header_tag_entry_address_efi64 *) tag)->entry_addr;
+#endif
+	break;
+

Why do we need separate handling of EFI entry point? Why can't we use the same structure?

Make sense.

Quoted 26 lines
Quoted 26 lines
grub-core/loader/multiboot_mbi2.c +3 -1
       case MULTIBOOT_HEADER_TAG_CONSOLE_FLAGS:
 	if (!(((struct multiboot_header_tag_console_flags *) tag)->console_flags
 	    & MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED))
@@ -211,7 +218,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	break;
 
       case MULTIBOOT_HEADER_TAG_EFI_BS:
+#ifdef GRUB_MACHINE_EFI
 	keep_bs = 1;
+#endif
 	break;
 
       default:
@@ -224,7 +233,7 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	break;
       }
 
-  if (addr_tag && !entry_specified)
+  if (addr_tag && !entry_specified && !(keep_bs && efi_entry_specified))
     {
       grub_free (buffer);
       return grub_error (GRUB_ERR_UNKNOWN_OS,
@@ -287,7 +296,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	}
     }
 
Quoted 7 lines

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char *filename) · line 303

Quoted 6 lines
grub-core/loader/multiboot_mbi2.c +3 -1
-  if (entry_specified)
+  if (keep_bs && efi_entry_specified)
+    grub_multiboot_payload_eip = efi_entry;
+  else if (entry_specified)
     grub_multiboot_payload_eip = entry;
 

This seems redundant.

What is wrong here?

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#37 lore raw

Re: [PATCH v2 3/6] i386/relocator: Add grub_relocator64_efi relocator

Quoted 8 lines · Daniel Kiper
Quoted 7 lines
Quoted 7 lines

- if (entry_specified)

Quoted 5 lines

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char *filename) · line 302

Quoted 5 lines
grub-core/loader/multiboot_mbi2.c +3 -0
+  if (keep_bs && efi_entry_specified)
+    grub_multiboot_payload_eip = efi_entry;
+  else if (entry_specified)
     grub_multiboot_payload_eip = entry;

This seems redundant.

What is wrong here?

I just mean that if we use a single structure this code could go away

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#38 lore raw

Re: [PATCH v2 3/6] i386/relocator: Add grub_relocator64_efi relocator

In reply to Daniel Kiper
Quoted 8 lines · Daniel Kiper
Quoted 7 lines
Quoted 7 lines

- if (entry_specified)

Quoted 5 lines

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char *filename) · line 302

Quoted 5 lines
grub-core/loader/multiboot_mbi2.c +3 -0
+  if (keep_bs && efi_entry_specified)
+    grub_multiboot_payload_eip = efi_entry;
+  else if (entry_specified)
     grub_multiboot_payload_eip = entry;

This seems redundant.

What is wrong here?

I just mean that if we use a single structure this code could go away

#39 lore raw

PATCH v2 4/6 multiboot2: Add tags used to pass ImageHandle to loaded image

In reply to Daniel Kiper

Add tags used to pass ImageHandle to loaded image. It is used by at least ExitBootServices() function.

---
 grub-core/loader/multiboot_mbi2.c |   46 +++++++++++++++++++++++++++++--------
 include/multiboot2.h              |   16 +++++++++++++
 2 files changed, 53 insertions(+), 9 deletions(-)
grub-core/loader/multiboot_mbi2.c +37 -9
index 8d66e3f..dc9c709 100644
@@ -172,6 +172,8 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	      case MULTIBOOT_TAG_TYPE_NETWORK:
 	      case MULTIBOOT_TAG_TYPE_EFI_MMAP:
 	      case MULTIBOOT_TAG_TYPE_EFI_BS:
+	      case MULTIBOOT_TAG_TYPE_EFI32_IH:
+	      case MULTIBOOT_TAG_TYPE_EFI64_IH:
 		break;
 
 	      default:
@@ -407,16 +409,18 @@ grub_multiboot_get_mbi_size (void)
 		 + grub_get_multiboot_mmap_count ()
 		 * sizeof (struct multiboot_mmap_entry)), MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_framebuffer), MULTIBOOT_TAG_ALIGN)
+#ifdef GRUB_MACHINE_EFI
     + ALIGN_UP (sizeof (struct multiboot_tag_efi32), MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_efi64), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi32_ih), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi64_ih), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
+		+ efi_mmap_size, MULTIBOOT_TAG_ALIGN)
+#endif
     + ALIGN_UP (sizeof (struct multiboot_tag_old_acpi)
 		+ sizeof (struct grub_acpi_rsdp_v10), MULTIBOOT_TAG_ALIGN)
     + acpiv2_size ()
     + net_size ()
-#ifdef GRUB_MACHINE_EFI
-    + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
-		+ efi_mmap_size, MULTIBOOT_TAG_ALIGN)
-#endif
     + sizeof (struct multiboot_tag_vbe) + MULTIBOOT_TAG_ALIGN - 1
     + sizeof (struct multiboot_tag_apm) + MULTIBOOT_TAG_ALIGN - 1;
 }
@@ -906,11 +910,35 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
 
   if (keep_bs)
     {
-      struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
-      tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;
-      tag->size = sizeof (struct multiboot_tag);
-      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-	/ sizeof (grub_properly_aligned_t);
+      {
+	struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
+	tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;
+	tag->size = sizeof (struct multiboot_tag);
+	ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	  / sizeof (grub_properly_aligned_t);
+      }
+
+#ifdef __x86_64__
+      {
+	struct multiboot_tag_efi64_ih *tag = (struct multiboot_tag_efi64_ih *) ptrorig;
+	tag->type = MULTIBOOT_TAG_TYPE_EFI64_IH;
+	tag->size = sizeof (*tag);
+	tag->pointer = (grub_addr_t) grub_efi_image_handle;
+	ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	  / sizeof (grub_properly_aligned_t);
+      }
+#endif
+
+#ifdef __i386__
+      {
+	struct multiboot_tag_efi32_ih *tag = (struct multiboot_tag_efi32_ih *) ptrorig;
+	tag->type = MULTIBOOT_TAG_TYPE_EFI32_IH;
+	tag->size = sizeof (*tag);
+	tag->pointer = (grub_addr_t) grub_efi_image_handle;
+	ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	  / sizeof (grub_properly_aligned_t);
+      }
+#endif
     }
 #endif
 
include/multiboot2.h +16 -0
index b3977e3..9f97ddc 100644
@@ -60,6 +60,8 @@
 #define MULTIBOOT_TAG_TYPE_NETWORK           16
 #define MULTIBOOT_TAG_TYPE_EFI_MMAP          17
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
+#define MULTIBOOT_TAG_TYPE_EFI32_IH          19
+#define MULTIBOOT_TAG_TYPE_EFI64_IH          20
 
 #define MULTIBOOT_HEADER_TAG_END  0
 #define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST  1
@@ -379,6 +381,20 @@ struct multiboot_tag_efi_mmap
   multiboot_uint8_t efi_mmap[0];
 }; 
 
+struct multiboot_tag_efi32_ih
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint32_t pointer;
+};
+
+struct multiboot_tag_efi64_ih
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint64_t pointer;
+};
+
 #endif /* ! ASM_FILE */
 
 #endif /* ! MULTIBOOT_HEADER */
Signature · 1 line
1.7.10.4
#40 lore raw

PATCH v2 4/6 multiboot2: Add tags used to pass ImageHandle to loaded image

In reply to Daniel Kiper

Add tags used to pass ImageHandle to loaded image. It is used by at least ExitBootServices() function.

---
 grub-core/loader/multiboot_mbi2.c |   46 +++++++++++++++++++++++++++++--------
 include/multiboot2.h              |   16 +++++++++++++
 2 files changed, 53 insertions(+), 9 deletions(-)
grub-core/loader/multiboot_mbi2.c +37 -9
index 8d66e3f..dc9c709 100644
@@ -172,6 +172,8 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	      case MULTIBOOT_TAG_TYPE_NETWORK:
 	      case MULTIBOOT_TAG_TYPE_EFI_MMAP:
 	      case MULTIBOOT_TAG_TYPE_EFI_BS:
+	      case MULTIBOOT_TAG_TYPE_EFI32_IH:
+	      case MULTIBOOT_TAG_TYPE_EFI64_IH:
 		break;
 
 	      default:
@@ -407,16 +409,18 @@ grub_multiboot_get_mbi_size (void)
 		 + grub_get_multiboot_mmap_count ()
 		 * sizeof (struct multiboot_mmap_entry)), MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_framebuffer), MULTIBOOT_TAG_ALIGN)
+#ifdef GRUB_MACHINE_EFI
     + ALIGN_UP (sizeof (struct multiboot_tag_efi32), MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_efi64), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi32_ih), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi64_ih), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
+		+ efi_mmap_size, MULTIBOOT_TAG_ALIGN)
+#endif
     + ALIGN_UP (sizeof (struct multiboot_tag_old_acpi)
 		+ sizeof (struct grub_acpi_rsdp_v10), MULTIBOOT_TAG_ALIGN)
     + acpiv2_size ()
     + net_size ()
-#ifdef GRUB_MACHINE_EFI
-    + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
-		+ efi_mmap_size, MULTIBOOT_TAG_ALIGN)
-#endif
     + sizeof (struct multiboot_tag_vbe) + MULTIBOOT_TAG_ALIGN - 1
     + sizeof (struct multiboot_tag_apm) + MULTIBOOT_TAG_ALIGN - 1;
 }
@@ -906,11 +910,35 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
 
   if (keep_bs)
     {
-      struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
-      tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;
-      tag->size = sizeof (struct multiboot_tag);
-      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-	/ sizeof (grub_properly_aligned_t);
+      {
+	struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
+	tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;
+	tag->size = sizeof (struct multiboot_tag);
+	ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	  / sizeof (grub_properly_aligned_t);
+      }
+
+#ifdef __x86_64__
+      {
+	struct multiboot_tag_efi64_ih *tag = (struct multiboot_tag_efi64_ih *) ptrorig;
+	tag->type = MULTIBOOT_TAG_TYPE_EFI64_IH;
+	tag->size = sizeof (*tag);
+	tag->pointer = (grub_addr_t) grub_efi_image_handle;
+	ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	  / sizeof (grub_properly_aligned_t);
+      }
+#endif
+
+#ifdef __i386__
+      {
+	struct multiboot_tag_efi32_ih *tag = (struct multiboot_tag_efi32_ih *) ptrorig;
+	tag->type = MULTIBOOT_TAG_TYPE_EFI32_IH;
+	tag->size = sizeof (*tag);
+	tag->pointer = (grub_addr_t) grub_efi_image_handle;
+	ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	  / sizeof (grub_properly_aligned_t);
+      }
+#endif
     }
 #endif
 
include/multiboot2.h +16 -0
index b3977e3..9f97ddc 100644
@@ -60,6 +60,8 @@
 #define MULTIBOOT_TAG_TYPE_NETWORK           16
 #define MULTIBOOT_TAG_TYPE_EFI_MMAP          17
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
+#define MULTIBOOT_TAG_TYPE_EFI32_IH          19
+#define MULTIBOOT_TAG_TYPE_EFI64_IH          20
 
 #define MULTIBOOT_HEADER_TAG_END  0
 #define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST  1
@@ -379,6 +381,20 @@ struct multiboot_tag_efi_mmap
   multiboot_uint8_t efi_mmap[0];
 }; 
 
+struct multiboot_tag_efi32_ih
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint32_t pointer;
+};
+
+struct multiboot_tag_efi64_ih
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint64_t pointer;
+};
+
 #endif /* ! ASM_FILE */
 
 #endif /* ! MULTIBOOT_HEADER */
Signature · 1 line
1.7.10.4
#41 lore raw

Re: [PATCH v2 4/6] multiboot2: Add tags used to pass ImageHandle to loaded image

Quoted 1 line · Daniel Kiper

Add tags used to pass ImageHandle to loaded image. It is used

s/loaded image/loaded image if requested./

Quoted 22 lines

by at least ExitBootServices() function.

---
 grub-core/loader/multiboot_mbi2.c |   46 +++++++++++++++++++++++++++++--------
 include/multiboot2.h              |   16 +++++++++++++
 2 files changed, 53 insertions(+), 9 deletions(-)
grub-core/loader/multiboot_mbi2.c +2 -0
index 8d66e3f..dc9c709 100644
@@ -172,6 +172,8 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	      case MULTIBOOT_TAG_TYPE_NETWORK:
 	      case MULTIBOOT_TAG_TYPE_EFI_MMAP:
 	      case MULTIBOOT_TAG_TYPE_EFI_BS:
+	      case MULTIBOOT_TAG_TYPE_EFI32_IH:
+	      case MULTIBOOT_TAG_TYPE_EFI64_IH:
 		break;
 
 	      default:

grub-core/loader/multiboot_mbi2.c grub_multiboot_get_mbi_size (void) · line 415

Quoted 7 lines
grub-core/loader/multiboot_mbi2.c +2 -0
@@ -407,16 +409,18 @@ grub_multiboot_get_mbi_size (void)
 		 + grub_get_multiboot_mmap_count ()
 		 * sizeof (struct multiboot_mmap_entry)), MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_framebuffer), MULTIBOOT_TAG_ALIGN)
+#ifdef GRUB_MACHINE_EFI
     + ALIGN_UP (sizeof (struct multiboot_tag_efi32), MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_efi64), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi32_ih), MULTIBOOT_TAG_ALIGN)

Shouldn't they be wrapped with #ifdef __x86_64__ (for efi64_ih) and #ifdef __i386__ for the other?

Or two functions (in the header?) wrapped with proper #ifdef machinery?

Quoted 27 lines
grub-core/loader/multiboot_mbi2.c +7 -9
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi64_ih), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
+		+ efi_mmap_size, MULTIBOOT_TAG_ALIGN)
+#endif
     + ALIGN_UP (sizeof (struct multiboot_tag_old_acpi)
 		+ sizeof (struct grub_acpi_rsdp_v10), MULTIBOOT_TAG_ALIGN)
     + acpiv2_size ()
     + net_size ()
-#ifdef GRUB_MACHINE_EFI
-    + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
-		+ efi_mmap_size, MULTIBOOT_TAG_ALIGN)
-#endif
     + sizeof (struct multiboot_tag_vbe) + MULTIBOOT_TAG_ALIGN - 1
     + sizeof (struct multiboot_tag_apm) + MULTIBOOT_TAG_ALIGN - 1;
 }
@@ -906,11 +910,35 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
 
   if (keep_bs)
     {
-      struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
-      tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;
-      tag->size = sizeof (struct multiboot_tag);
-      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-	/ sizeof (grub_properly_aligned_t);
+      {
+	struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
+	tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;

grub-core/loader/multiboot_mbi2.c grub_multiboot_make_mbi (grub_uint32_t *target) · line 925

Quoted 10 lines
grub-core/loader/multiboot_mbi2.c +10 -0
+	tag->size = sizeof (struct multiboot_tag);
+	ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	  / sizeof (grub_properly_aligned_t);
+      }
+
+#ifdef __x86_64__
+      {
+	struct multiboot_tag_efi64_ih *tag = (struct multiboot_tag_efi64_ih *) ptrorig;
+	tag->type = MULTIBOOT_TAG_TYPE_EFI64_IH;
+	tag->size = sizeof (*tag);

The other places where sizeof is used ends up using the full structure so it :

sizeof (struct multiboot_tag_efi64_ih)

grub-core/loader/multiboot_mbi2.c grub_multiboot_make_mbi (grub_uint32_t *target) · line 936

Quoted 11 lines
grub-core/loader/multiboot_mbi2.c +11 -0
+	tag->pointer = (grub_addr_t) grub_efi_image_handle;
+	ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	  / sizeof (grub_properly_aligned_t);
+      }
+#endif
+
+#ifdef __i386__
+      {
+	struct multiboot_tag_efi32_ih *tag = (struct multiboot_tag_efi32_ih *) ptrorig;
+	tag->type = MULTIBOOT_TAG_TYPE_EFI32_IH;
+	tag->size = sizeof (*tag);

Ditto.

Quoted 45 lines
grub-core/loader/multiboot_mbi2.c +5 -0
+	tag->pointer = (grub_addr_t) grub_efi_image_handle;
+	ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	  / sizeof (grub_properly_aligned_t);
+      }
+#endif
     }
 #endif
 
include/multiboot2.h +16 -0
index b3977e3..9f97ddc 100644
@@ -60,6 +60,8 @@
 #define MULTIBOOT_TAG_TYPE_NETWORK           16
 #define MULTIBOOT_TAG_TYPE_EFI_MMAP          17
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
+#define MULTIBOOT_TAG_TYPE_EFI32_IH          19
+#define MULTIBOOT_TAG_TYPE_EFI64_IH          20
 
 #define MULTIBOOT_HEADER_TAG_END  0
 #define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST  1
@@ -379,6 +381,20 @@ struct multiboot_tag_efi_mmap
   multiboot_uint8_t efi_mmap[0];
 }; 
 
+struct multiboot_tag_efi32_ih
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint32_t pointer;
+};
+
+struct multiboot_tag_efi64_ih
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint64_t pointer;
+};
+
 #endif /* ! ASM_FILE */
 
 #endif /* ! MULTIBOOT_HEADER */
Signature · 1 line
1.7.10.4

A message that is not in the archive (sent off-list or not yet received): CAEaD8JOPxLVECcUqjj2-x5AeXjbQ9Cd7bOaoe8UPU2t7wdoxpA@mail.gmail.com

#43 lore raw

Fwd: Re: [PATCH v2 4/6] multiboot2: Add tags used to pass ImageHandle to loaded image

---------- Message transféré ----------
De : "Vladimir 'phcoder' Serbinenko" <[email protected]>
Date : 9 nov. 2015 8:12 PM
Objet : Re: [PATCH v2 4/6] multiboot2: Add tags used to pass ImageHandle to loaded image
À : "Konrad Rzeszutek Wilk" <[email protected]>
Cc :

Le 11 août 2015 8:38 PM, "Konrad Rzeszutek Wilk" <[email protected]> a écrit :

Quoted 10 lines · Daniel Kiper
Quoted 1 line

Add tags used to pass ImageHandle to loaded image. It is used

s/loaded image/loaded image if requested./

Quoted 5 lines

by at least ExitBootServices() function.

---
 grub-core/loader/multiboot_mbi2.c |   46

+++++++++++++++++++++++++++++--------

Quoted 4 lines

Review

Quoted 4 lines
 include/multiboot2.h              |   16 +++++++++++++
 2 files changed, 53 insertions(+), 9 deletions(-)
(quoted diff) +0 -0

b/grub-core/loader/multiboot_mbi2.c

Quoted 4 lines

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char

Quoted 4 lines

index 8d66e3f..dc9c709 100644

grub-core/loader/multiboot_mbi2.c +0 -0
@@ -172,6 +172,8 @@ grub_multiboot_load (grub_file_t file, const char

*filename)

Quoted 11 lines

grub-core/loader/multiboot_mbi2.c grub_multiboot_get_mbi_size (void) · line 410

Quoted 11 lines
grub-core/loader/multiboot_mbi2.c +2 -0
            case MULTIBOOT_TAG_TYPE_NETWORK:
            case MULTIBOOT_TAG_TYPE_EFI_MMAP:
            case MULTIBOOT_TAG_TYPE_EFI_BS:
+           case MULTIBOOT_TAG_TYPE_EFI32_IH:
+           case MULTIBOOT_TAG_TYPE_EFI64_IH:
              break;
 
            default:
@@ -407,16 +409,18 @@ grub_multiboot_get_mbi_size (void)
               + grub_get_multiboot_mmap_count ()
               * sizeof (struct multiboot_mmap_entry)),

MULTIBOOT_TAG_ALIGN)

Quoted 1 line

grub-core/loader/multiboot_mbi2.c grub_multiboot_get_mbi_size (void) · line 411

Quoted 1 line
grub-core/loader/multiboot_mbi2.c +0 -0
     + ALIGN_UP (sizeof (struct multiboot_tag_framebuffer),

MULTIBOOT_TAG_ALIGN)

Quoted 2 lines

grub-core/loader/multiboot_mbi2.c grub_multiboot_get_mbi_size (void) · line 413

Quoted 2 lines
grub-core/loader/multiboot_mbi2.c +1 -0
+#ifdef GRUB_MACHINE_EFI
     + ALIGN_UP (sizeof (struct multiboot_tag_efi32),

MULTIBOOT_TAG_ALIGN)

Quoted 1 line

grub-core/loader/multiboot_mbi2.c grub_multiboot_get_mbi_size (void) · line 414

Quoted 1 line
grub-core/loader/multiboot_mbi2.c +0 -0
     + ALIGN_UP (sizeof (struct multiboot_tag_efi64),

MULTIBOOT_TAG_ALIGN)

Quoted 1 line

grub-core/loader/multiboot_mbi2.c grub_multiboot_get_mbi_size (void) · line 415

Quoted 1 line
grub-core/loader/multiboot_mbi2.c +1 -0
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi32_ih),

MULTIBOOT_TAG_ALIGN)

Quoted 2 lines

Shouldn't they be wrapped with #ifdef __x86_64__ (for efi64_ih) and

#ifdef __i386__

Quoted 2 lines

for the other?

No need. It's just few bytes but not adding ifdef makes it more readable

Quoted 3 lines

Or two functions (in the header?) wrapped with proper #ifdef machinery?

Quoted 1 line
grub-core/loader/multiboot_mbi2.c +1 -0
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi64_ih),

MULTIBOOT_TAG_ALIGN)

Quoted 26 lines
Quoted 26 lines
grub-core/loader/multiboot_mbi2.c +6 -9
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
+             + efi_mmap_size, MULTIBOOT_TAG_ALIGN)
+#endif
     + ALIGN_UP (sizeof (struct multiboot_tag_old_acpi)
              + sizeof (struct grub_acpi_rsdp_v10), MULTIBOOT_TAG_ALIGN)
     + acpiv2_size ()
     + net_size ()
-#ifdef GRUB_MACHINE_EFI
-    + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
-             + efi_mmap_size, MULTIBOOT_TAG_ALIGN)
-#endif
     + sizeof (struct multiboot_tag_vbe) + MULTIBOOT_TAG_ALIGN - 1
     + sizeof (struct multiboot_tag_apm) + MULTIBOOT_TAG_ALIGN - 1;
 }
@@ -906,11 +910,35 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
 
   if (keep_bs)
     {
-      struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
-      tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;
-      tag->size = sizeof (struct multiboot_tag);
-      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-     / sizeof (grub_properly_aligned_t);
+      {
+     struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
+     tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;
Quoted 8 lines

grub-core/loader/multiboot_mbi2.c grub_multiboot_make_mbi (grub_uint32_t *target)

Quoted 8 lines
grub-core/loader/multiboot_mbi2.c +8 -0
+     tag->size = sizeof (struct multiboot_tag);
+     ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+       / sizeof (grub_properly_aligned_t);
+      }
+
+#ifdef __x86_64__
+      {
+     struct multiboot_tag_efi64_ih *tag = (struct

multiboot_tag_efi64_ih *) ptrorig;

Quoted 4 lines

grub-core/loader/multiboot_mbi2.c grub_multiboot_make_mbi (grub_uint32_t *target) · line 925

Quoted 2 lines
grub-core/loader/multiboot_mbi2.c +2 -0
+     tag->type = MULTIBOOT_TAG_TYPE_EFI64_IH;
+     tag->size = sizeof (*tag);

The other places where sizeof is used ends up using the full structure so

it :

Quoted 3 lines

sizeof (struct multiboot_tag_efi64_ih)

sizeof (*tag) is fine and is actually better

Quoted 9 lines

grub-core/loader/multiboot_mbi2.c grub_multiboot_make_mbi (grub_uint32_t *target)

Quoted 9 lines
grub-core/loader/multiboot_mbi2.c +9 -0
+     tag->pointer = (grub_addr_t) grub_efi_image_handle;
+     ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+       / sizeof (grub_properly_aligned_t);
+      }
+#endif
+
+#ifdef __i386__
+      {
+     struct multiboot_tag_efi32_ih *tag = (struct

multiboot_tag_efi32_ih *) ptrorig;

Quoted 49 lines

grub-core/loader/multiboot_mbi2.c grub_multiboot_make_mbi (grub_uint32_t *target) · line 936

Quoted 2 lines
grub-core/loader/multiboot_mbi2.c +2 -0
+     tag->type = MULTIBOOT_TAG_TYPE_EFI32_IH;
+     tag->size = sizeof (*tag);

Ditto.

Quoted 45 lines
grub-core/loader/multiboot_mbi2.c +5 -0
+     tag->pointer = (grub_addr_t) grub_efi_image_handle;
+     ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+       / sizeof (grub_properly_aligned_t);
+      }
+#endif
     }
 #endif
 
include/multiboot2.h +16 -1
index b3977e3..9f97ddc 100644
@@ -60,6 +60,8 @@
 #define MULTIBOOT_TAG_TYPE_NETWORK           16
 #define MULTIBOOT_TAG_TYPE_EFI_MMAP          17
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
+#define MULTIBOOT_TAG_TYPE_EFI32_IH          19
+#define MULTIBOOT_TAG_TYPE_EFI64_IH          20
 
 #define MULTIBOOT_HEADER_TAG_END  0
 #define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST  1
@@ -379,6 +381,20 @@ struct multiboot_tag_efi_mmap
   multiboot_uint8_t efi_mmap[0];
 };
 
+struct multiboot_tag_efi32_ih
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint32_t pointer;
+};
+
+struct multiboot_tag_efi64_ih
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint64_t pointer;
+};
+
 #endif /* ! ASM_FILE */
 
 #endif /* ! MULTIBOOT_HEADER */
--

1.7.10.4

#44 lore raw

Re: [PATCH v2 4/6] multiboot2: Add tags used to pass ImageHandle to loaded image

In reply to Daniel Kiper
Quoted 1 line · Daniel Kiper

Add tags used to pass ImageHandle to loaded image. It is used

s/loaded image/loaded image if requested./

Quoted 22 lines

by at least ExitBootServices() function.

---
 grub-core/loader/multiboot_mbi2.c |   46 +++++++++++++++++++++++++++++--------
 include/multiboot2.h              |   16 +++++++++++++
 2 files changed, 53 insertions(+), 9 deletions(-)
grub-core/loader/multiboot_mbi2.c +2 -0
index 8d66e3f..dc9c709 100644
@@ -172,6 +172,8 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	      case MULTIBOOT_TAG_TYPE_NETWORK:
 	      case MULTIBOOT_TAG_TYPE_EFI_MMAP:
 	      case MULTIBOOT_TAG_TYPE_EFI_BS:
+	      case MULTIBOOT_TAG_TYPE_EFI32_IH:
+	      case MULTIBOOT_TAG_TYPE_EFI64_IH:
 		break;
 
 	      default:

grub-core/loader/multiboot_mbi2.c grub_multiboot_get_mbi_size (void) · line 415

Quoted 7 lines
grub-core/loader/multiboot_mbi2.c +2 -0
@@ -407,16 +409,18 @@ grub_multiboot_get_mbi_size (void)
 		 + grub_get_multiboot_mmap_count ()
 		 * sizeof (struct multiboot_mmap_entry)), MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_framebuffer), MULTIBOOT_TAG_ALIGN)
+#ifdef GRUB_MACHINE_EFI
     + ALIGN_UP (sizeof (struct multiboot_tag_efi32), MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_efi64), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi32_ih), MULTIBOOT_TAG_ALIGN)

Shouldn't they be wrapped with #ifdef __x86_64__ (for efi64_ih) and #ifdef __i386__ for the other?

Or two functions (in the header?) wrapped with proper #ifdef machinery?

Quoted 27 lines
grub-core/loader/multiboot_mbi2.c +7 -9
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi64_ih), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
+		+ efi_mmap_size, MULTIBOOT_TAG_ALIGN)
+#endif
     + ALIGN_UP (sizeof (struct multiboot_tag_old_acpi)
 		+ sizeof (struct grub_acpi_rsdp_v10), MULTIBOOT_TAG_ALIGN)
     + acpiv2_size ()
     + net_size ()
-#ifdef GRUB_MACHINE_EFI
-    + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
-		+ efi_mmap_size, MULTIBOOT_TAG_ALIGN)
-#endif
     + sizeof (struct multiboot_tag_vbe) + MULTIBOOT_TAG_ALIGN - 1
     + sizeof (struct multiboot_tag_apm) + MULTIBOOT_TAG_ALIGN - 1;
 }
@@ -906,11 +910,35 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
 
   if (keep_bs)
     {
-      struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
-      tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;
-      tag->size = sizeof (struct multiboot_tag);
-      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-	/ sizeof (grub_properly_aligned_t);
+      {
+	struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
+	tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;

grub-core/loader/multiboot_mbi2.c grub_multiboot_make_mbi (grub_uint32_t *target) · line 925

Quoted 10 lines
grub-core/loader/multiboot_mbi2.c +10 -0
+	tag->size = sizeof (struct multiboot_tag);
+	ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	  / sizeof (grub_properly_aligned_t);
+      }
+
+#ifdef __x86_64__
+      {
+	struct multiboot_tag_efi64_ih *tag = (struct multiboot_tag_efi64_ih *) ptrorig;
+	tag->type = MULTIBOOT_TAG_TYPE_EFI64_IH;
+	tag->size = sizeof (*tag);

The other places where sizeof is used ends up using the full structure so it :

sizeof (struct multiboot_tag_efi64_ih)

grub-core/loader/multiboot_mbi2.c grub_multiboot_make_mbi (grub_uint32_t *target) · line 936

Quoted 11 lines
grub-core/loader/multiboot_mbi2.c +11 -0
+	tag->pointer = (grub_addr_t) grub_efi_image_handle;
+	ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	  / sizeof (grub_properly_aligned_t);
+      }
+#endif
+
+#ifdef __i386__
+      {
+	struct multiboot_tag_efi32_ih *tag = (struct multiboot_tag_efi32_ih *) ptrorig;
+	tag->type = MULTIBOOT_TAG_TYPE_EFI32_IH;
+	tag->size = sizeof (*tag);

Ditto.

Quoted 45 lines
grub-core/loader/multiboot_mbi2.c +5 -0
+	tag->pointer = (grub_addr_t) grub_efi_image_handle;
+	ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	  / sizeof (grub_properly_aligned_t);
+      }
+#endif
     }
 #endif
 
include/multiboot2.h +16 -0
index b3977e3..9f97ddc 100644
@@ -60,6 +60,8 @@
 #define MULTIBOOT_TAG_TYPE_NETWORK           16
 #define MULTIBOOT_TAG_TYPE_EFI_MMAP          17
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
+#define MULTIBOOT_TAG_TYPE_EFI32_IH          19
+#define MULTIBOOT_TAG_TYPE_EFI64_IH          20
 
 #define MULTIBOOT_HEADER_TAG_END  0
 #define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST  1
@@ -379,6 +381,20 @@ struct multiboot_tag_efi_mmap
   multiboot_uint8_t efi_mmap[0];
 }; 
 
+struct multiboot_tag_efi32_ih
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint32_t pointer;
+};
+
+struct multiboot_tag_efi64_ih
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint64_t pointer;
+};
+
 #endif /* ! ASM_FILE */
 
 #endif /* ! MULTIBOOT_HEADER */
Signature · 1 line
1.7.10.4

A message that is not in the archive (sent off-list or not yet received): CAEaD8JOaS=J22k+Sq=8Ou77pO6yvtgxSLiwuMjAoirSipJhW6g@mail.gmail.com

#46 lore raw

Re: [PATCH v2 4/6] multiboot2: Add tags used to pass ImageHandle to loaded image

Quoted 141 lines · Vladimir 'phcoder' Serbinenko

Lgtm

Le 20 juil. 2015 4:36 PM, "Daniel Kiper" <[email protected] <mailto:[email protected]>> a écrit :

Add tags used to pass ImageHandle to loaded image. It is used by at least ExitBootServices() function.

<mailto:[email protected]>>
---
grub-core/loader/multiboot_mbi2.c | 46
+++++++++++++++++++++++++++++--------
include/multiboot2.h | 16 +++++++++++++
2 files changed, 53 insertions(+), 9 deletions(-)

diff --git a/grub-core/loader/multiboot_mbi2.c b/grub-core/loader/multiboot_mbi2.c
index 8d66e3f..dc9c709 100644
--- a/grub-core/loader/multiboot_mbi2.c
+++ b/grub-core/loader/multiboot_mbi2.c
@@ -172,6 +172,8 @@ grub_multiboot_load (grub_file_t file, const char *filename)
case MULTIBOOT_TAG_TYPE_NETWORK:
case MULTIBOOT_TAG_TYPE_EFI_MMAP:
case MULTIBOOT_TAG_TYPE_EFI_BS:
+ case MULTIBOOT_TAG_TYPE_EFI32_IH:
+ case MULTIBOOT_TAG_TYPE_EFI64_IH:
break;

default:
@@ -407,16 +409,18 @@ grub_multiboot_get_mbi_size (void)
+ grub_get_multiboot_mmap_count ()
* sizeof (struct multiboot_mmap_entry)),
MULTIBOOT_TAG_ALIGN)
+ ALIGN_UP (sizeof (struct multiboot_tag_framebuffer),
MULTIBOOT_TAG_ALIGN)
+#ifdef GRUB_MACHINE_EFI
+ ALIGN_UP (sizeof (struct multiboot_tag_efi32),
MULTIBOOT_TAG_ALIGN)
+ ALIGN_UP (sizeof (struct multiboot_tag_efi64),
MULTIBOOT_TAG_ALIGN)
+ + ALIGN_UP (sizeof (struct multiboot_tag_efi32_ih),
MULTIBOOT_TAG_ALIGN)
+ + ALIGN_UP (sizeof (struct multiboot_tag_efi64_ih),
MULTIBOOT_TAG_ALIGN)
+ + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
+ + efi_mmap_size, MULTIBOOT_TAG_ALIGN)
+#endif
+ ALIGN_UP (sizeof (struct multiboot_tag_old_acpi)
+ sizeof (struct grub_acpi_rsdp_v10),
MULTIBOOT_TAG_ALIGN)
+ acpiv2_size ()
+ net_size ()
-#ifdef GRUB_MACHINE_EFI
- + ALIGN_UP (sizeof (struct multiboot_tag_efi_mmap)
- + efi_mmap_size, MULTIBOOT_TAG_ALIGN)
-#endif
+ sizeof (struct multiboot_tag_vbe) + MULTIBOOT_TAG_ALIGN - 1
+ sizeof (struct multiboot_tag_apm) + MULTIBOOT_TAG_ALIGN - 1;
}
@@ -906,11 +910,35 @@ grub_multiboot_make_mbi (grub_uint32_t *target)

if (keep_bs)
{
- struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
- tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;
- tag->size = sizeof (struct multiboot_tag);
- ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
- / sizeof (grub_properly_aligned_t);
+ {
+ struct multiboot_tag *tag = (struct multiboot_tag *) ptrorig;
+ tag->type = MULTIBOOT_TAG_TYPE_EFI_BS;
+ tag->size = sizeof (struct multiboot_tag);
+ ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+ / sizeof (grub_properly_aligned_t);
+ }
+
+#ifdef __x86_64__
+ {
+ struct multiboot_tag_efi64_ih *tag = (struct
multiboot_tag_efi64_ih *) ptrorig;
+ tag->type = MULTIBOOT_TAG_TYPE_EFI64_IH;
+ tag->size = sizeof (*tag);
+ tag->pointer = (grub_addr_t) grub_efi_image_handle;
+ ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+ / sizeof (grub_properly_aligned_t);
+ }
+#endif
+
+#ifdef __i386__
+ {
+ struct multiboot_tag_efi32_ih *tag = (struct
multiboot_tag_efi32_ih *) ptrorig;
+ tag->type = MULTIBOOT_TAG_TYPE_EFI32_IH;
+ tag->size = sizeof (*tag);
+ tag->pointer = (grub_addr_t) grub_efi_image_handle;
+ ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+ / sizeof (grub_properly_aligned_t);
+ }
+#endif
}
#endif

diff --git a/include/multiboot2.h b/include/multiboot2.h index b3977e3..9f97ddc 100644
--- a/include/multiboot2.h
+++ b/include/multiboot2.h
@@ -60,6 +60,8 @@
#define MULTIBOOT_TAG_TYPE_NETWORK 16
#define MULTIBOOT_TAG_TYPE_EFI_MMAP 17
#define MULTIBOOT_TAG_TYPE_EFI_BS 18
+#define MULTIBOOT_TAG_TYPE_EFI32_IH 19
+#define MULTIBOOT_TAG_TYPE_EFI64_IH 20

#define MULTIBOOT_HEADER_TAG_END 0 #define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST 1
@@ -379,6 +381,20 @@ struct multiboot_tag_efi_mmap
multiboot_uint8_t efi_mmap[0];
};

+struct multiboot_tag_efi32_ih
+{
+ multiboot_uint32_t type;
+ multiboot_uint32_t size;
+ multiboot_uint32_t pointer;
+};
+
+struct multiboot_tag_efi64_ih
+{
+ multiboot_uint32_t type;
+ multiboot_uint32_t size;
+ multiboot_uint64_t pointer;
+};
+
#endif /* ! ASM_FILE */

#endif /* ! MULTIBOOT_HEADER */
--
1.7.10.4

#47 lore raw

PATCH v2 5/6 multiboot2: Add support for relocatable images

In reply to Daniel Kiper
---
 grub-core/loader/i386/multiboot_mbi.c |    6 ++--
 grub-core/loader/multiboot.c          |   12 +++++--
 grub-core/loader/multiboot_elfxx.c    |   28 +++++++++++----
 grub-core/loader/multiboot_mbi2.c     |   63 +++++++++++++++++++++++++++++----
 include/grub/multiboot.h              |    4 ++-
 include/multiboot2.h                  |   24 +++++++++++++
 6 files changed, 118 insertions(+), 19 deletions(-)
grub-core/loader/i386/multiboot_mbi.c +4 -2
index 956d0e3..abdb98b 100644
@@ -72,7 +72,8 @@ load_kernel (grub_file_t file, const char *filename,
   grub_err_t err;
   if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE)
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+				     GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);
       if (err == GRUB_ERR_UNKNOWN_OS && (header->flags & MULTIBOOT_AOUT_KLUDGE))
 	grub_errno = err = GRUB_ERR_NONE;
     }
@@ -118,7 +119,8 @@ load_kernel (grub_file_t file, const char *filename,
       return GRUB_ERR_NONE;
     }
 
-  return grub_multiboot_load_elf (file, filename, buffer);
+  return grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+				  GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);
 }
 
 static struct multiboot_header *
grub-core/loader/multiboot.c +9 -3
index ca7154f..1b1f7a9 100644
@@ -190,12 +190,18 @@ static grub_uint64_t highest_load;
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-			 void *buffer)
+			 void *buffer, int relocatable, grub_uint32_t min_addr,
+			 grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+			 grub_uint32_t *base_addr, int avoid_efi_boot_services)
 {
   if (grub_multiboot_is_elf32 (buffer))
-    return grub_multiboot_load_elf32 (file, filename, buffer);
+    return grub_multiboot_load_elf32 (file, filename, buffer, relocatable,
+				      min_addr, max_addr, align, preference,
+				      base_addr, avoid_efi_boot_services);
   else if (grub_multiboot_is_elf64 (buffer))
-    return grub_multiboot_load_elf64 (file, filename, buffer);
+    return grub_multiboot_load_elf64 (file, filename, buffer, relocatable,
+				      min_addr, max_addr, align, preference,
+				      base_addr, avoid_efi_boot_services);
 
   return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF magic"));
 }
grub-core/loader/multiboot_elfxx.c +21 -7
index 6a220bd..4fce685 100644
@@ -51,7 +51,10 @@ CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
 }
 
 static grub_err_t
-CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, void *buffer)
+CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename,
+				     void *buffer, int relocatable, grub_uint32_t min_addr,
+				     grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+				     grub_uint32_t *base_addr, int avoid_efi_boot_services)
 {
   Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
   char *phdr_base;
@@ -89,19 +92,30 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi
 	  if (phdr(i)->p_paddr + phdr(i)->p_memsz > highest_load)
 	    highest_load = phdr(i)->p_paddr + phdr(i)->p_memsz;
 
-	  grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx\n",
-			i, (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);
+	  grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx,"
+			"align=0x%lx, relocatable=%d, avoid_efi_boot_services=%d\n", i,
+			(long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr,
+			(long) align, relocatable, avoid_efi_boot_services);
 
 	  {
 	    grub_relocator_chunk_t ch;
-	    err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, 
-						   &ch, phdr(i)->p_paddr,
-						   phdr(i)->p_memsz);
+
+	    if (relocatable)
+	      err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
+						      min_addr, max_addr - phdr(i)->p_memsz,
+						      phdr(i)->p_memsz, align ? align : 1,
+						      preference, avoid_efi_boot_services);
+	    else
+	      err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
+						     &ch, phdr(i)->p_paddr,
+						     phdr(i)->p_memsz);
 	    if (err)
 	      {
 		grub_dprintf ("multiboot_loader", "Error loading phdr %d\n", i);
 		return err;
 	      }
+	    if (base_addr)
+	      *base_addr = get_physical_target_address (ch);
 	    source = get_virtual_current_address (ch);
 	  }
 
@@ -208,7 +222,7 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi
 						    + 1, sh->sh_size,
 						    sh->sh_addralign,
 						    GRUB_RELOCATOR_PREFERENCE_NONE,
-						    0);
+						    avoid_efi_boot_services);
 	    if (err)
 	      {
 		grub_dprintf ("multiboot_loader", "Error loading shdr %d\n", i);
grub-core/loader/multiboot_mbi2.c +57 -6

126 lines not on this page · raw message

include/grub/multiboot.h +3 -1
index e13c084..ec322b0 100644
@@ -94,7 +94,9 @@ grub_multiboot_load (grub_file_t file, const char *filename);
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-			 void *buffer);
+			 void *buffer, int relocatable, grub_uint32_t min_addr,
+			 grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+			 grub_uint32_t *base_addr, int avoid_efi_boot_services);
 extern grub_size_t grub_multiboot_pure_size;
 extern grub_size_t grub_multiboot_alloc_mbi;
 extern grub_uint32_t grub_multiboot_payload_eip;
include/multiboot2.h +24 -0
index 9f97ddc..941a95d 100644
@@ -62,6 +62,7 @@
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
 #define MULTIBOOT_TAG_TYPE_EFI32_IH          19
 #define MULTIBOOT_TAG_TYPE_EFI64_IH          20
+#define MULTIBOOT_TAG_TYPE_BASE_ADDR         21
 
 #define MULTIBOOT_HEADER_TAG_END  0
 #define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST  1
@@ -72,11 +73,16 @@
 #define MULTIBOOT_HEADER_TAG_MODULE_ALIGN  6
 #define MULTIBOOT_HEADER_TAG_EFI_BS  7
 #define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64  9
+#define MULTIBOOT_HEADER_TAG_RELOCATABLE  10
 
 #define MULTIBOOT_ARCHITECTURE_I386  0
 #define MULTIBOOT_ARCHITECTURE_MIPS32  4
 #define MULTIBOOT_HEADER_TAG_OPTIONAL 1
 
+#define MULTIBOOT_LOAD_PREFERENCE_NONE 0
+#define MULTIBOOT_LOAD_PREFERENCE_LOW 1
+#define MULTIBOOT_LOAD_PREFERENCE_HIGH 2
+
 #define MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED 1
 #define MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED 2
 
@@ -169,6 +175,17 @@ struct multiboot_header_tag_module_align
   multiboot_uint32_t size;
 };
 
+struct multiboot_header_tag_relocatable
+{
+  multiboot_uint16_t type;
+  multiboot_uint16_t flags;
+  multiboot_uint32_t size;
+  multiboot_uint32_t min_addr;
+  multiboot_uint32_t max_addr;
+  multiboot_uint32_t align;
+  multiboot_uint32_t preference;
+};
+
 struct multiboot_color
 {
   multiboot_uint8_t red;
@@ -395,6 +412,13 @@ struct multiboot_tag_efi64_ih
   multiboot_uint64_t pointer;
 };
 
+struct multiboot_tag_base_addr
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint32_t base_addr;
+};
+
 #endif /* ! ASM_FILE */
 
 #endif /* ! MULTIBOOT_HEADER */
Signature · 1 line
1.7.10.4
#48 lore raw

PATCH v2 5/6 multiboot2: Add support for relocatable images

In reply to Daniel Kiper
---
 grub-core/loader/i386/multiboot_mbi.c |    6 ++--
 grub-core/loader/multiboot.c          |   12 +++++--
 grub-core/loader/multiboot_elfxx.c    |   28 +++++++++++----
 grub-core/loader/multiboot_mbi2.c     |   63 +++++++++++++++++++++++++++++----
 include/grub/multiboot.h              |    4 ++-
 include/multiboot2.h                  |   24 +++++++++++++
 6 files changed, 118 insertions(+), 19 deletions(-)
grub-core/loader/i386/multiboot_mbi.c +4 -2
index 956d0e3..abdb98b 100644
@@ -72,7 +72,8 @@ load_kernel (grub_file_t file, const char *filename,
   grub_err_t err;
   if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE)
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+				     GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);
       if (err == GRUB_ERR_UNKNOWN_OS && (header->flags & MULTIBOOT_AOUT_KLUDGE))
 	grub_errno = err = GRUB_ERR_NONE;
     }
@@ -118,7 +119,8 @@ load_kernel (grub_file_t file, const char *filename,
       return GRUB_ERR_NONE;
     }
 
-  return grub_multiboot_load_elf (file, filename, buffer);
+  return grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+				  GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);
 }
 
 static struct multiboot_header *
grub-core/loader/multiboot.c +9 -3
index ca7154f..1b1f7a9 100644
@@ -190,12 +190,18 @@ static grub_uint64_t highest_load;
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-			 void *buffer)
+			 void *buffer, int relocatable, grub_uint32_t min_addr,
+			 grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+			 grub_uint32_t *base_addr, int avoid_efi_boot_services)
 {
   if (grub_multiboot_is_elf32 (buffer))
-    return grub_multiboot_load_elf32 (file, filename, buffer);
+    return grub_multiboot_load_elf32 (file, filename, buffer, relocatable,
+				      min_addr, max_addr, align, preference,
+				      base_addr, avoid_efi_boot_services);
   else if (grub_multiboot_is_elf64 (buffer))
-    return grub_multiboot_load_elf64 (file, filename, buffer);
+    return grub_multiboot_load_elf64 (file, filename, buffer, relocatable,
+				      min_addr, max_addr, align, preference,
+				      base_addr, avoid_efi_boot_services);
 
   return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF magic"));
 }
grub-core/loader/multiboot_elfxx.c +21 -7
index 6a220bd..4fce685 100644
@@ -51,7 +51,10 @@ CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
 }
 
 static grub_err_t
-CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, void *buffer)
+CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename,
+				     void *buffer, int relocatable, grub_uint32_t min_addr,
+				     grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+				     grub_uint32_t *base_addr, int avoid_efi_boot_services)
 {
   Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
   char *phdr_base;
@@ -89,19 +92,30 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi
 	  if (phdr(i)->p_paddr + phdr(i)->p_memsz > highest_load)
 	    highest_load = phdr(i)->p_paddr + phdr(i)->p_memsz;
 
-	  grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx\n",
-			i, (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);
+	  grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx,"
+			"align=0x%lx, relocatable=%d, avoid_efi_boot_services=%d\n", i,
+			(long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr,
+			(long) align, relocatable, avoid_efi_boot_services);
 
 	  {
 	    grub_relocator_chunk_t ch;
-	    err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, 
-						   &ch, phdr(i)->p_paddr,
-						   phdr(i)->p_memsz);
+
+	    if (relocatable)
+	      err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
+						      min_addr, max_addr - phdr(i)->p_memsz,
+						      phdr(i)->p_memsz, align ? align : 1,
+						      preference, avoid_efi_boot_services);
+	    else
+	      err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
+						     &ch, phdr(i)->p_paddr,
+						     phdr(i)->p_memsz);
 	    if (err)
 	      {
 		grub_dprintf ("multiboot_loader", "Error loading phdr %d\n", i);
 		return err;
 	      }
+	    if (base_addr)
+	      *base_addr = get_physical_target_address (ch);
 	    source = get_virtual_current_address (ch);
 	  }
 
@@ -208,7 +222,7 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi
 						    + 1, sh->sh_size,
 						    sh->sh_addralign,
 						    GRUB_RELOCATOR_PREFERENCE_NONE,
-						    0);
+						    avoid_efi_boot_services);
 	    if (err)
 	      {
 		grub_dprintf ("multiboot_loader", "Error loading shdr %d\n", i);
grub-core/loader/multiboot_mbi2.c +57 -6

126 lines not on this page · raw message

include/grub/multiboot.h +3 -1
index e13c084..ec322b0 100644
@@ -94,7 +94,9 @@ grub_multiboot_load (grub_file_t file, const char *filename);
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-			 void *buffer);
+			 void *buffer, int relocatable, grub_uint32_t min_addr,
+			 grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+			 grub_uint32_t *base_addr, int avoid_efi_boot_services);
 extern grub_size_t grub_multiboot_pure_size;
 extern grub_size_t grub_multiboot_alloc_mbi;
 extern grub_uint32_t grub_multiboot_payload_eip;
include/multiboot2.h +24 -0
index 9f97ddc..941a95d 100644
@@ -62,6 +62,7 @@
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
 #define MULTIBOOT_TAG_TYPE_EFI32_IH          19
 #define MULTIBOOT_TAG_TYPE_EFI64_IH          20
+#define MULTIBOOT_TAG_TYPE_BASE_ADDR         21
 
 #define MULTIBOOT_HEADER_TAG_END  0
 #define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST  1
@@ -72,11 +73,16 @@
 #define MULTIBOOT_HEADER_TAG_MODULE_ALIGN  6
 #define MULTIBOOT_HEADER_TAG_EFI_BS  7
 #define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64  9
+#define MULTIBOOT_HEADER_TAG_RELOCATABLE  10
 
 #define MULTIBOOT_ARCHITECTURE_I386  0
 #define MULTIBOOT_ARCHITECTURE_MIPS32  4
 #define MULTIBOOT_HEADER_TAG_OPTIONAL 1
 
+#define MULTIBOOT_LOAD_PREFERENCE_NONE 0
+#define MULTIBOOT_LOAD_PREFERENCE_LOW 1
+#define MULTIBOOT_LOAD_PREFERENCE_HIGH 2
+
 #define MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED 1
 #define MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED 2
 
@@ -169,6 +175,17 @@ struct multiboot_header_tag_module_align
   multiboot_uint32_t size;
 };
 
+struct multiboot_header_tag_relocatable
+{
+  multiboot_uint16_t type;
+  multiboot_uint16_t flags;
+  multiboot_uint32_t size;
+  multiboot_uint32_t min_addr;
+  multiboot_uint32_t max_addr;
+  multiboot_uint32_t align;
+  multiboot_uint32_t preference;
+};
+
 struct multiboot_color
 {
   multiboot_uint8_t red;
@@ -395,6 +412,13 @@ struct multiboot_tag_efi64_ih
   multiboot_uint64_t pointer;
 };
 
+struct multiboot_tag_base_addr
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint32_t base_addr;
+};
+
 #endif /* ! ASM_FILE */
 
 #endif /* ! MULTIBOOT_HEADER */
Signature · 1 line
1.7.10.4
#49 lore raw

Re: [PATCH v2 5/6] multiboot2: Add support for relocatable images

On Mon, Jul 20, 2015 at 04:35:53PM +0200, Daniel Kiper wrote:

This parts needs a bit of details I think.

As in why can't the ELF note that is present in the binary be satisfied. Or under what conditions would the existing code not work?

Quoted 25 lines
---
 grub-core/loader/i386/multiboot_mbi.c |    6 ++--
 grub-core/loader/multiboot.c          |   12 +++++--
 grub-core/loader/multiboot_elfxx.c    |   28 +++++++++++----
 grub-core/loader/multiboot_mbi2.c     |   63 +++++++++++++++++++++++++++++----
 include/grub/multiboot.h              |    4 ++-
 include/multiboot2.h                  |   24 +++++++++++++
 6 files changed, 118 insertions(+), 19 deletions(-)
grub-core/loader/i386/multiboot_mbi.c +2 -1
index 956d0e3..abdb98b 100644
@@ -72,7 +72,8 @@ load_kernel (grub_file_t file, const char *filename,
   grub_err_t err;
   if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE)
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+				     GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);
       if (err == GRUB_ERR_UNKNOWN_OS && (header->flags & MULTIBOOT_AOUT_KLUDGE))
 	grub_errno = err = GRUB_ERR_NONE;
     }

grub-core/loader/i386/multiboot_mbi.c load_kernel (grub_file_t file, const char *filename, · line 123

Quoted 6 lines
grub-core/loader/i386/multiboot_mbi.c +2 -1
@@ -118,7 +119,8 @@ load_kernel (grub_file_t file, const char *filename,
       return GRUB_ERR_NONE;
     }
 
-  return grub_multiboot_load_elf (file, filename, buffer);
+  return grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+				  GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);

Oh boy. That is a bit hard to read. Perhaps another function could be introduced?

grub_multiboot_load_elf_ignore_reloc ? which would just pass the three parameters and set the rest to the 0 options?

Quoted 8 lines
grub-core/loader/i386/multiboot_mbi.c +0 -0
 }
 
 static struct multiboot_header *
grub-core/loader/multiboot.c +0 -0
index ca7154f..1b1f7a9 100644

grub-core/loader/multiboot.c static grub_uint64_t highest_load; · line 195

Quoted 7 lines
grub-core/loader/multiboot.c +3 -1
@@ -190,12 +190,18 @@ static grub_uint64_t highest_load;
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-			 void *buffer)
+			 void *buffer, int relocatable, grub_uint32_t min_addr,
+			 grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+			 grub_uint32_t *base_addr, int avoid_efi_boot_services)

s/int/bool/ ?

Quoted 31 lines
grub-core/loader/multiboot.c +6 -2
 {
   if (grub_multiboot_is_elf32 (buffer))
-    return grub_multiboot_load_elf32 (file, filename, buffer);
+    return grub_multiboot_load_elf32 (file, filename, buffer, relocatable,
+				      min_addr, max_addr, align, preference,
+				      base_addr, avoid_efi_boot_services);
   else if (grub_multiboot_is_elf64 (buffer))
-    return grub_multiboot_load_elf64 (file, filename, buffer);
+    return grub_multiboot_load_elf64 (file, filename, buffer, relocatable,
+				      min_addr, max_addr, align, preference,
+				      base_addr, avoid_efi_boot_services);
 
   return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF magic"));
 }
grub-core/loader/multiboot_elfxx.c +4 -1
index 6a220bd..4fce685 100644
@@ -51,7 +51,10 @@ CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
 }
 
 static grub_err_t
-CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, void *buffer)
+CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename,
+				     void *buffer, int relocatable, grub_uint32_t min_addr,
+				     grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+				     grub_uint32_t *base_addr, int avoid_efi_boot_services)
 {
   Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
   char *phdr_base;

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi · line 96

Quoted 7 lines
grub-core/loader/multiboot_elfxx.c +2 -2
@@ -89,19 +92,30 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi
 	  if (phdr(i)->p_paddr + phdr(i)->p_memsz > highest_load)
 	    highest_load = phdr(i)->p_paddr + phdr(i)->p_memsz;
 
-	  grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx\n",
-			i, (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);
+	  grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx,"
+			"align=0x%lx, relocatable=%d, avoid_efi_boot_services=%d\n", i,

%d to %s

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi · line 98

Quoted 2 lines
grub-core/loader/multiboot_elfxx.c +2 -0
+			(long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr,
+			(long) align, relocatable, avoid_efi_boot_services);

and then

avoid_efi_boot_services ? "yes" : "no"

Quoted 72 lines
grub-core/loader/multiboot_elfxx.c +13 -4
 	  {
 	    grub_relocator_chunk_t ch;
-	    err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, 
-						   &ch, phdr(i)->p_paddr,
-						   phdr(i)->p_memsz);
+
+	    if (relocatable)
+	      err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
+						      min_addr, max_addr - phdr(i)->p_memsz,
+						      phdr(i)->p_memsz, align ? align : 1,
+						      preference, avoid_efi_boot_services);
+	    else
+	      err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
+						     &ch, phdr(i)->p_paddr,
+						     phdr(i)->p_memsz);
 	    if (err)
 	      {
 		grub_dprintf ("multiboot_loader", "Error loading phdr %d\n", i);
 		return err;
 	      }
+	    if (base_addr)
+	      *base_addr = get_physical_target_address (ch);
 	    source = get_virtual_current_address (ch);
 	  }
 
@@ -208,7 +222,7 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi
 						    + 1, sh->sh_size,
 						    sh->sh_addralign,
 						    GRUB_RELOCATOR_PREFERENCE_NONE,
-						    0);
+						    avoid_efi_boot_services);
 	    if (err)
 	      {
 		grub_dprintf ("multiboot_loader", "Error loading shdr %d\n", i);
grub-core/loader/multiboot_mbi2.c +6 -2
index dc9c709..7ac64ec 100644
@@ -68,6 +68,7 @@ static grub_size_t elf_sec_num, elf_sec_entsize;
 static unsigned elf_sec_shstrndx;
 static void *elf_sections;
 static int keep_bs = 0;
+static grub_uint32_t base_addr = 0;
 
 void
 grub_multiboot_add_elfsyms (grub_size_t num, grub_size_t entsize,
@@ -107,11 +108,13 @@ grub_multiboot_load (grub_file_t file, const char *filename)
   grub_err_t err;
   struct multiboot_header_tag *tag;
   struct multiboot_header_tag_address *addr_tag = NULL;
-  int entry_specified = 0, efi_entry_specified = 0;
+  int entry_specified = 0, efi_entry_specified = 0, relocatable = 0;
   grub_addr_t entry = 0, efi_entry = 0;
-  grub_uint32_t console_required = 0;
+  grub_uint32_t console_required = 0, min_addr = 0;
+  grub_uint32_t max_addr = 0, preference = 0;
   struct multiboot_header_tag_framebuffer *fbtag = NULL;
   int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
+  grub_size_t align = 0;
 
   buffer = grub_malloc (MULTIBOOT_SEARCH);
   if (!buffer)
@@ -174,6 +177,7 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	      case MULTIBOOT_TAG_TYPE_EFI_BS:
 	      case MULTIBOOT_TAG_TYPE_EFI32_IH:
 	      case MULTIBOOT_TAG_TYPE_EFI64_IH:
+	      case MULTIBOOT_TAG_TYPE_BASE_ADDR:
 		break;
 
 	      default:

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char *filename) · line 223

Quoted 5 lines
grub-core/loader/multiboot_mbi2.c +2 -0
@@ -215,6 +219,26 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	accepted_consoles |= GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER;
 	break;
 
+      case MULTIBOOT_HEADER_TAG_RELOCATABLE:
+	relocatable = 1;

How about:

	struct multiboot_header_tag_relocatable *_tag = (struct multiboot_header_tag_relocatable *)tag;

and then you don't have to use the casting?

Quoted 82 lines
grub-core/loader/multiboot_mbi2.c +40 -4
+	min_addr = ((struct multiboot_header_tag_relocatable *) tag)->min_addr;
+	max_addr = ((struct multiboot_header_tag_relocatable *) tag)->max_addr;
+	align = ((struct multiboot_header_tag_relocatable *) tag)->align;
+	switch (((struct multiboot_header_tag_relocatable *) tag)->preference)
+	  {
+	  case MULTIBOOT_LOAD_PREFERENCE_LOW:
+	    preference = GRUB_RELOCATOR_PREFERENCE_LOW;
+	    break;
+
+	  case MULTIBOOT_LOAD_PREFERENCE_HIGH:
+	    preference = GRUB_RELOCATOR_PREFERENCE_HIGH;
+	    break;
+
+	  default:
+	    preference = GRUB_RELOCATOR_PREFERENCE_NONE;
+	  }
+	break;
+
 	/* GRUB always page-aligns modules.  */
       case MULTIBOOT_HEADER_TAG_MODULE_ALIGN:
 	break;
@@ -260,15 +284,22 @@ grub_multiboot_load (grub_file_t file, const char *filename)
       else
 	code_size = load_size;
 
-      err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, 
-					     &ch, load_addr,
-					     code_size);
+      if (relocatable)
+	err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
+						min_addr, max_addr - code_size,
+						code_size, align ? align : 1,
+						preference, keep_bs);
+      else
+	err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
+					       &ch, load_addr,
+					       code_size);
       if (err)
 	{
 	  grub_dprintf ("multiboot_loader", "Error loading aout kludge\n");
 	  grub_free (buffer);
 	  return err;
 	}
+      base_addr = get_physical_target_address (ch);
       source = get_virtual_current_address (ch);
 
       if ((grub_file_seek (file, offset)) == (grub_off_t) -1)
@@ -290,7 +321,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
     }
   else
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer,
+				     relocatable, min_addr, max_addr,
+				     align, preference, &base_addr, keep_bs);
       if (err)
 	{
 	  grub_free (buffer);
@@ -303,6 +336,14 @@ grub_multiboot_load (grub_file_t file, const char *filename)
   else if (entry_specified)
     grub_multiboot_payload_eip = entry;
 
+  if (relocatable)
+    {
+      if (base_addr > min_addr)
+	grub_multiboot_payload_eip += base_addr - min_addr;
+      else
+	grub_multiboot_payload_eip -= min_addr - base_addr;
+    }
+
   if (fbtag)
     err = grub_multiboot_set_console (GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER,
 				      accepted_consoles,
@@ -409,6 +450,7 @@ grub_multiboot_get_mbi_size (void)
 		 + grub_get_multiboot_mmap_count ()
 		 * sizeof (struct multiboot_mmap_entry)), MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_framebuffer), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_base_addr), MULTIBOOT_TAG_ALIGN)
 #ifdef GRUB_MACHINE_EFI
     + ALIGN_UP (sizeof (struct multiboot_tag_efi32), MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_efi64), MULTIBOOT_TAG_ALIGN)

grub-core/loader/multiboot_mbi2.c grub_multiboot_make_mbi (grub_uint32_t *target) · line 740

Quoted 6 lines
grub-core/loader/multiboot_mbi2.c +3 -0
@@ -693,6 +735,15 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
   ptrorig += (2 * sizeof (grub_uint32_t)) / sizeof (grub_properly_aligned_t);
 
   {
+    struct multiboot_tag_base_addr *tag = (struct multiboot_tag_base_addr *) ptrorig;
+    tag->type = MULTIBOOT_TAG_TYPE_BASE_ADDR;
+    tag->size = sizeof (struct multiboot_tag_base_addr); 

You have an extra space at the end.

Quoted 88 lines
grub-core/loader/multiboot_mbi2.c +6 -0
+    tag->base_addr = base_addr;
+    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+       / sizeof (grub_properly_aligned_t);
+  }
+
+  {
     struct multiboot_tag_string *tag = (struct multiboot_tag_string *) ptrorig;
     tag->type = MULTIBOOT_TAG_TYPE_CMDLINE;
     tag->size = sizeof (struct multiboot_tag_string) + cmdline_size; 
include/grub/multiboot.h +3 -1
index e13c084..ec322b0 100644
@@ -94,7 +94,9 @@ grub_multiboot_load (grub_file_t file, const char *filename);
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-			 void *buffer);
+			 void *buffer, int relocatable, grub_uint32_t min_addr,
+			 grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+			 grub_uint32_t *base_addr, int avoid_efi_boot_services);
 extern grub_size_t grub_multiboot_pure_size;
 extern grub_size_t grub_multiboot_alloc_mbi;
 extern grub_uint32_t grub_multiboot_payload_eip;
include/multiboot2.h +24 -0
index 9f97ddc..941a95d 100644
@@ -62,6 +62,7 @@
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
 #define MULTIBOOT_TAG_TYPE_EFI32_IH          19
 #define MULTIBOOT_TAG_TYPE_EFI64_IH          20
+#define MULTIBOOT_TAG_TYPE_BASE_ADDR         21
 
 #define MULTIBOOT_HEADER_TAG_END  0
 #define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST  1
@@ -72,11 +73,16 @@
 #define MULTIBOOT_HEADER_TAG_MODULE_ALIGN  6
 #define MULTIBOOT_HEADER_TAG_EFI_BS  7
 #define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64  9
+#define MULTIBOOT_HEADER_TAG_RELOCATABLE  10
 
 #define MULTIBOOT_ARCHITECTURE_I386  0
 #define MULTIBOOT_ARCHITECTURE_MIPS32  4
 #define MULTIBOOT_HEADER_TAG_OPTIONAL 1
 
+#define MULTIBOOT_LOAD_PREFERENCE_NONE 0
+#define MULTIBOOT_LOAD_PREFERENCE_LOW 1
+#define MULTIBOOT_LOAD_PREFERENCE_HIGH 2
+
 #define MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED 1
 #define MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED 2
 
@@ -169,6 +175,17 @@ struct multiboot_header_tag_module_align
   multiboot_uint32_t size;
 };
 
+struct multiboot_header_tag_relocatable
+{
+  multiboot_uint16_t type;
+  multiboot_uint16_t flags;
+  multiboot_uint32_t size;
+  multiboot_uint32_t min_addr;
+  multiboot_uint32_t max_addr;
+  multiboot_uint32_t align;
+  multiboot_uint32_t preference;
+};
+
 struct multiboot_color
 {
   multiboot_uint8_t red;
@@ -395,6 +412,13 @@ struct multiboot_tag_efi64_ih
   multiboot_uint64_t pointer;
 };
 
+struct multiboot_tag_base_addr
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint32_t base_addr;
+};
+
 #endif /* ! ASM_FILE */
 
 #endif /* ! MULTIBOOT_HEADER */
Signature · 1 line
1.7.10.4
#50 lore raw

Re: [PATCH v2 5/6] multiboot2: Add support for relocatable images

In reply to Daniel Kiper

On Mon, Jul 20, 2015 at 04:35:53PM +0200, Daniel Kiper wrote:

This parts needs a bit of details I think.

As in why can't the ELF note that is present in the binary be satisfied. Or under what conditions would the existing code not work?

Quoted 25 lines
---
 grub-core/loader/i386/multiboot_mbi.c |    6 ++--
 grub-core/loader/multiboot.c          |   12 +++++--
 grub-core/loader/multiboot_elfxx.c    |   28 +++++++++++----
 grub-core/loader/multiboot_mbi2.c     |   63 +++++++++++++++++++++++++++++----
 include/grub/multiboot.h              |    4 ++-
 include/multiboot2.h                  |   24 +++++++++++++
 6 files changed, 118 insertions(+), 19 deletions(-)
grub-core/loader/i386/multiboot_mbi.c +2 -1
index 956d0e3..abdb98b 100644
@@ -72,7 +72,8 @@ load_kernel (grub_file_t file, const char *filename,
   grub_err_t err;
   if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE)
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+				     GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);
       if (err == GRUB_ERR_UNKNOWN_OS && (header->flags & MULTIBOOT_AOUT_KLUDGE))
 	grub_errno = err = GRUB_ERR_NONE;
     }

grub-core/loader/i386/multiboot_mbi.c load_kernel (grub_file_t file, const char *filename, · line 123

Quoted 6 lines
grub-core/loader/i386/multiboot_mbi.c +2 -1
@@ -118,7 +119,8 @@ load_kernel (grub_file_t file, const char *filename,
       return GRUB_ERR_NONE;
     }
 
-  return grub_multiboot_load_elf (file, filename, buffer);
+  return grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+				  GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);

Oh boy. That is a bit hard to read. Perhaps another function could be introduced?

grub_multiboot_load_elf_ignore_reloc ? which would just pass the three parameters and set the rest to the 0 options?

Quoted 8 lines
grub-core/loader/i386/multiboot_mbi.c +0 -0
 }
 
 static struct multiboot_header *
grub-core/loader/multiboot.c +0 -0
index ca7154f..1b1f7a9 100644

grub-core/loader/multiboot.c static grub_uint64_t highest_load; · line 195

Quoted 7 lines
grub-core/loader/multiboot.c +3 -1
@@ -190,12 +190,18 @@ static grub_uint64_t highest_load;
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-			 void *buffer)
+			 void *buffer, int relocatable, grub_uint32_t min_addr,
+			 grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+			 grub_uint32_t *base_addr, int avoid_efi_boot_services)

s/int/bool/ ?

Quoted 31 lines
grub-core/loader/multiboot.c +6 -2
 {
   if (grub_multiboot_is_elf32 (buffer))
-    return grub_multiboot_load_elf32 (file, filename, buffer);
+    return grub_multiboot_load_elf32 (file, filename, buffer, relocatable,
+				      min_addr, max_addr, align, preference,
+				      base_addr, avoid_efi_boot_services);
   else if (grub_multiboot_is_elf64 (buffer))
-    return grub_multiboot_load_elf64 (file, filename, buffer);
+    return grub_multiboot_load_elf64 (file, filename, buffer, relocatable,
+				      min_addr, max_addr, align, preference,
+				      base_addr, avoid_efi_boot_services);
 
   return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF magic"));
 }
grub-core/loader/multiboot_elfxx.c +4 -1
index 6a220bd..4fce685 100644
@@ -51,7 +51,10 @@ CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
 }
 
 static grub_err_t
-CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, void *buffer)
+CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename,
+				     void *buffer, int relocatable, grub_uint32_t min_addr,
+				     grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+				     grub_uint32_t *base_addr, int avoid_efi_boot_services)
 {
   Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
   char *phdr_base;

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi · line 96

Quoted 7 lines
grub-core/loader/multiboot_elfxx.c +2 -2
@@ -89,19 +92,30 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi
 	  if (phdr(i)->p_paddr + phdr(i)->p_memsz > highest_load)
 	    highest_load = phdr(i)->p_paddr + phdr(i)->p_memsz;
 
-	  grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx\n",
-			i, (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);
+	  grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx,"
+			"align=0x%lx, relocatable=%d, avoid_efi_boot_services=%d\n", i,

%d to %s

grub-core/loader/multiboot_elfxx.c CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi · line 98

Quoted 2 lines
grub-core/loader/multiboot_elfxx.c +2 -0
+			(long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr,
+			(long) align, relocatable, avoid_efi_boot_services);

and then

avoid_efi_boot_services ? "yes" : "no"

Quoted 72 lines
grub-core/loader/multiboot_elfxx.c +13 -4
 	  {
 	    grub_relocator_chunk_t ch;
-	    err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, 
-						   &ch, phdr(i)->p_paddr,
-						   phdr(i)->p_memsz);
+
+	    if (relocatable)
+	      err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
+						      min_addr, max_addr - phdr(i)->p_memsz,
+						      phdr(i)->p_memsz, align ? align : 1,
+						      preference, avoid_efi_boot_services);
+	    else
+	      err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
+						     &ch, phdr(i)->p_paddr,
+						     phdr(i)->p_memsz);
 	    if (err)
 	      {
 		grub_dprintf ("multiboot_loader", "Error loading phdr %d\n", i);
 		return err;
 	      }
+	    if (base_addr)
+	      *base_addr = get_physical_target_address (ch);
 	    source = get_virtual_current_address (ch);
 	  }
 
@@ -208,7 +222,7 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi
 						    + 1, sh->sh_size,
 						    sh->sh_addralign,
 						    GRUB_RELOCATOR_PREFERENCE_NONE,
-						    0);
+						    avoid_efi_boot_services);
 	    if (err)
 	      {
 		grub_dprintf ("multiboot_loader", "Error loading shdr %d\n", i);
grub-core/loader/multiboot_mbi2.c +6 -2
index dc9c709..7ac64ec 100644
@@ -68,6 +68,7 @@ static grub_size_t elf_sec_num, elf_sec_entsize;
 static unsigned elf_sec_shstrndx;
 static void *elf_sections;
 static int keep_bs = 0;
+static grub_uint32_t base_addr = 0;
 
 void
 grub_multiboot_add_elfsyms (grub_size_t num, grub_size_t entsize,
@@ -107,11 +108,13 @@ grub_multiboot_load (grub_file_t file, const char *filename)
   grub_err_t err;
   struct multiboot_header_tag *tag;
   struct multiboot_header_tag_address *addr_tag = NULL;
-  int entry_specified = 0, efi_entry_specified = 0;
+  int entry_specified = 0, efi_entry_specified = 0, relocatable = 0;
   grub_addr_t entry = 0, efi_entry = 0;
-  grub_uint32_t console_required = 0;
+  grub_uint32_t console_required = 0, min_addr = 0;
+  grub_uint32_t max_addr = 0, preference = 0;
   struct multiboot_header_tag_framebuffer *fbtag = NULL;
   int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
+  grub_size_t align = 0;
 
   buffer = grub_malloc (MULTIBOOT_SEARCH);
   if (!buffer)
@@ -174,6 +177,7 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	      case MULTIBOOT_TAG_TYPE_EFI_BS:
 	      case MULTIBOOT_TAG_TYPE_EFI32_IH:
 	      case MULTIBOOT_TAG_TYPE_EFI64_IH:
+	      case MULTIBOOT_TAG_TYPE_BASE_ADDR:
 		break;
 
 	      default:

grub-core/loader/multiboot_mbi2.c grub_multiboot_load (grub_file_t file, const char *filename) · line 223

Quoted 5 lines
grub-core/loader/multiboot_mbi2.c +2 -0
@@ -215,6 +219,26 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	accepted_consoles |= GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER;
 	break;
 
+      case MULTIBOOT_HEADER_TAG_RELOCATABLE:
+	relocatable = 1;

How about:

	struct multiboot_header_tag_relocatable *_tag = (struct multiboot_header_tag_relocatable *)tag;

and then you don't have to use the casting?

Quoted 82 lines
grub-core/loader/multiboot_mbi2.c +40 -4
+	min_addr = ((struct multiboot_header_tag_relocatable *) tag)->min_addr;
+	max_addr = ((struct multiboot_header_tag_relocatable *) tag)->max_addr;
+	align = ((struct multiboot_header_tag_relocatable *) tag)->align;
+	switch (((struct multiboot_header_tag_relocatable *) tag)->preference)
+	  {
+	  case MULTIBOOT_LOAD_PREFERENCE_LOW:
+	    preference = GRUB_RELOCATOR_PREFERENCE_LOW;
+	    break;
+
+	  case MULTIBOOT_LOAD_PREFERENCE_HIGH:
+	    preference = GRUB_RELOCATOR_PREFERENCE_HIGH;
+	    break;
+
+	  default:
+	    preference = GRUB_RELOCATOR_PREFERENCE_NONE;
+	  }
+	break;
+
 	/* GRUB always page-aligns modules.  */
       case MULTIBOOT_HEADER_TAG_MODULE_ALIGN:
 	break;
@@ -260,15 +284,22 @@ grub_multiboot_load (grub_file_t file, const char *filename)
       else
 	code_size = load_size;
 
-      err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, 
-					     &ch, load_addr,
-					     code_size);
+      if (relocatable)
+	err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
+						min_addr, max_addr - code_size,
+						code_size, align ? align : 1,
+						preference, keep_bs);
+      else
+	err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
+					       &ch, load_addr,
+					       code_size);
       if (err)
 	{
 	  grub_dprintf ("multiboot_loader", "Error loading aout kludge\n");
 	  grub_free (buffer);
 	  return err;
 	}
+      base_addr = get_physical_target_address (ch);
       source = get_virtual_current_address (ch);
 
       if ((grub_file_seek (file, offset)) == (grub_off_t) -1)
@@ -290,7 +321,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
     }
   else
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer,
+				     relocatable, min_addr, max_addr,
+				     align, preference, &base_addr, keep_bs);
       if (err)
 	{
 	  grub_free (buffer);
@@ -303,6 +336,14 @@ grub_multiboot_load (grub_file_t file, const char *filename)
   else if (entry_specified)
     grub_multiboot_payload_eip = entry;
 
+  if (relocatable)
+    {
+      if (base_addr > min_addr)
+	grub_multiboot_payload_eip += base_addr - min_addr;
+      else
+	grub_multiboot_payload_eip -= min_addr - base_addr;
+    }
+
   if (fbtag)
     err = grub_multiboot_set_console (GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER,
 				      accepted_consoles,
@@ -409,6 +450,7 @@ grub_multiboot_get_mbi_size (void)
 		 + grub_get_multiboot_mmap_count ()
 		 * sizeof (struct multiboot_mmap_entry)), MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_framebuffer), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_base_addr), MULTIBOOT_TAG_ALIGN)
 #ifdef GRUB_MACHINE_EFI
     + ALIGN_UP (sizeof (struct multiboot_tag_efi32), MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_efi64), MULTIBOOT_TAG_ALIGN)

grub-core/loader/multiboot_mbi2.c grub_multiboot_make_mbi (grub_uint32_t *target) · line 740

Quoted 6 lines
grub-core/loader/multiboot_mbi2.c +3 -0
@@ -693,6 +735,15 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
   ptrorig += (2 * sizeof (grub_uint32_t)) / sizeof (grub_properly_aligned_t);
 
   {
+    struct multiboot_tag_base_addr *tag = (struct multiboot_tag_base_addr *) ptrorig;
+    tag->type = MULTIBOOT_TAG_TYPE_BASE_ADDR;
+    tag->size = sizeof (struct multiboot_tag_base_addr); 

You have an extra space at the end.

Quoted 88 lines
grub-core/loader/multiboot_mbi2.c +6 -0
+    tag->base_addr = base_addr;
+    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+       / sizeof (grub_properly_aligned_t);
+  }
+
+  {
     struct multiboot_tag_string *tag = (struct multiboot_tag_string *) ptrorig;
     tag->type = MULTIBOOT_TAG_TYPE_CMDLINE;
     tag->size = sizeof (struct multiboot_tag_string) + cmdline_size; 
include/grub/multiboot.h +3 -1
index e13c084..ec322b0 100644
@@ -94,7 +94,9 @@ grub_multiboot_load (grub_file_t file, const char *filename);
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-			 void *buffer);
+			 void *buffer, int relocatable, grub_uint32_t min_addr,
+			 grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+			 grub_uint32_t *base_addr, int avoid_efi_boot_services);
 extern grub_size_t grub_multiboot_pure_size;
 extern grub_size_t grub_multiboot_alloc_mbi;
 extern grub_uint32_t grub_multiboot_payload_eip;
include/multiboot2.h +24 -0
index 9f97ddc..941a95d 100644
@@ -62,6 +62,7 @@
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
 #define MULTIBOOT_TAG_TYPE_EFI32_IH          19
 #define MULTIBOOT_TAG_TYPE_EFI64_IH          20
+#define MULTIBOOT_TAG_TYPE_BASE_ADDR         21
 
 #define MULTIBOOT_HEADER_TAG_END  0
 #define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST  1
@@ -72,11 +73,16 @@
 #define MULTIBOOT_HEADER_TAG_MODULE_ALIGN  6
 #define MULTIBOOT_HEADER_TAG_EFI_BS  7
 #define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64  9
+#define MULTIBOOT_HEADER_TAG_RELOCATABLE  10
 
 #define MULTIBOOT_ARCHITECTURE_I386  0
 #define MULTIBOOT_ARCHITECTURE_MIPS32  4
 #define MULTIBOOT_HEADER_TAG_OPTIONAL 1
 
+#define MULTIBOOT_LOAD_PREFERENCE_NONE 0
+#define MULTIBOOT_LOAD_PREFERENCE_LOW 1
+#define MULTIBOOT_LOAD_PREFERENCE_HIGH 2
+
 #define MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED 1
 #define MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED 2
 
@@ -169,6 +175,17 @@ struct multiboot_header_tag_module_align
   multiboot_uint32_t size;
 };
 
+struct multiboot_header_tag_relocatable
+{
+  multiboot_uint16_t type;
+  multiboot_uint16_t flags;
+  multiboot_uint32_t size;
+  multiboot_uint32_t min_addr;
+  multiboot_uint32_t max_addr;
+  multiboot_uint32_t align;
+  multiboot_uint32_t preference;
+};
+
 struct multiboot_color
 {
   multiboot_uint8_t red;
@@ -395,6 +412,13 @@ struct multiboot_tag_efi64_ih
   multiboot_uint64_t pointer;
 };
 
+struct multiboot_tag_base_addr
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint32_t base_addr;
+};
+
 #endif /* ! ASM_FILE */
 
 #endif /* ! MULTIBOOT_HEADER */
Signature · 1 line
1.7.10.4
#51 lore raw

Re: [PATCH v2 5/6] multiboot2: Add support for relocatable images

In reply to Daniel Kiper
Quoted 1 line · Daniel Kiper

What is handling the actual relocation? Why doesn't this patch include support for ELF relocations?

Quoted 326 lines
---
 grub-core/loader/i386/multiboot_mbi.c |    6 ++--
 grub-core/loader/multiboot.c          |   12 +++++--
 grub-core/loader/multiboot_elfxx.c    |   28 +++++++++++----
 grub-core/loader/multiboot_mbi2.c     |   63 +++++++++++++++++++++++++++++----
 include/grub/multiboot.h              |    4 ++-
 include/multiboot2.h                  |   24 +++++++++++++
 6 files changed, 118 insertions(+), 19 deletions(-)
grub-core/loader/i386/multiboot_mbi.c +4 -2
index 956d0e3..abdb98b 100644
@@ -72,7 +72,8 @@ load_kernel (grub_file_t file, const char *filename,
   grub_err_t err;
   if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE)
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+				     GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);
       if (err == GRUB_ERR_UNKNOWN_OS && (header->flags & MULTIBOOT_AOUT_KLUDGE))
 	grub_errno = err = GRUB_ERR_NONE;
     }
@@ -118,7 +119,8 @@ load_kernel (grub_file_t file, const char *filename,
       return GRUB_ERR_NONE;
     }
 
-  return grub_multiboot_load_elf (file, filename, buffer);
+  return grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+				  GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);
 }
 
 static struct multiboot_header *
grub-core/loader/multiboot.c +9 -3
index ca7154f..1b1f7a9 100644
@@ -190,12 +190,18 @@ static grub_uint64_t highest_load;
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-			 void *buffer)
+			 void *buffer, int relocatable, grub_uint32_t min_addr,
+			 grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+			 grub_uint32_t *base_addr, int avoid_efi_boot_services)
 {
   if (grub_multiboot_is_elf32 (buffer))
-    return grub_multiboot_load_elf32 (file, filename, buffer);
+    return grub_multiboot_load_elf32 (file, filename, buffer, relocatable,
+				      min_addr, max_addr, align, preference,
+				      base_addr, avoid_efi_boot_services);
   else if (grub_multiboot_is_elf64 (buffer))
-    return grub_multiboot_load_elf64 (file, filename, buffer);
+    return grub_multiboot_load_elf64 (file, filename, buffer, relocatable,
+				      min_addr, max_addr, align, preference,
+				      base_addr, avoid_efi_boot_services);
 
   return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF magic"));
 }
grub-core/loader/multiboot_elfxx.c +21 -7
index 6a220bd..4fce685 100644
@@ -51,7 +51,10 @@ CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
 }
 
 static grub_err_t
-CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, void *buffer)
+CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename,
+				     void *buffer, int relocatable, grub_uint32_t min_addr,
+				     grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+				     grub_uint32_t *base_addr, int avoid_efi_boot_services)
 {
   Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
   char *phdr_base;
@@ -89,19 +92,30 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi
 	  if (phdr(i)->p_paddr + phdr(i)->p_memsz > highest_load)
 	    highest_load = phdr(i)->p_paddr + phdr(i)->p_memsz;
 
-	  grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx\n",
-			i, (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);
+	  grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx,"
+			"align=0x%lx, relocatable=%d, avoid_efi_boot_services=%d\n", i,
+			(long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr,
+			(long) align, relocatable, avoid_efi_boot_services);
 
 	  {
 	    grub_relocator_chunk_t ch;
-	    err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, 
-						   &ch, phdr(i)->p_paddr,
-						   phdr(i)->p_memsz);
+
+	    if (relocatable)
+	      err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
+						      min_addr, max_addr - phdr(i)->p_memsz,
+						      phdr(i)->p_memsz, align ? align : 1,
+						      preference, avoid_efi_boot_services);
+	    else
+	      err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
+						     &ch, phdr(i)->p_paddr,
+						     phdr(i)->p_memsz);
 	    if (err)
 	      {
 		grub_dprintf ("multiboot_loader", "Error loading phdr %d\n", i);
 		return err;
 	      }
+	    if (base_addr)
+	      *base_addr = get_physical_target_address (ch);
 	    source = get_virtual_current_address (ch);
 	  }
 
@@ -208,7 +222,7 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi
 						    + 1, sh->sh_size,
 						    sh->sh_addralign,
 						    GRUB_RELOCATOR_PREFERENCE_NONE,
-						    0);
+						    avoid_efi_boot_services);
 	    if (err)
 	      {
 		grub_dprintf ("multiboot_loader", "Error loading shdr %d\n", i);
grub-core/loader/multiboot_mbi2.c +57 -6
index dc9c709..7ac64ec 100644
@@ -68,6 +68,7 @@ static grub_size_t elf_sec_num, elf_sec_entsize;
 static unsigned elf_sec_shstrndx;
 static void *elf_sections;
 static int keep_bs = 0;
+static grub_uint32_t base_addr = 0;
 
 void
 grub_multiboot_add_elfsyms (grub_size_t num, grub_size_t entsize,
@@ -107,11 +108,13 @@ grub_multiboot_load (grub_file_t file, const char *filename)
   grub_err_t err;
   struct multiboot_header_tag *tag;
   struct multiboot_header_tag_address *addr_tag = NULL;
-  int entry_specified = 0, efi_entry_specified = 0;
+  int entry_specified = 0, efi_entry_specified = 0, relocatable = 0;
   grub_addr_t entry = 0, efi_entry = 0;
-  grub_uint32_t console_required = 0;
+  grub_uint32_t console_required = 0, min_addr = 0;
+  grub_uint32_t max_addr = 0, preference = 0;
   struct multiboot_header_tag_framebuffer *fbtag = NULL;
   int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
+  grub_size_t align = 0;
 
   buffer = grub_malloc (MULTIBOOT_SEARCH);
   if (!buffer)
@@ -174,6 +177,7 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	      case MULTIBOOT_TAG_TYPE_EFI_BS:
 	      case MULTIBOOT_TAG_TYPE_EFI32_IH:
 	      case MULTIBOOT_TAG_TYPE_EFI64_IH:
+	      case MULTIBOOT_TAG_TYPE_BASE_ADDR:
 		break;
 
 	      default:
@@ -215,6 +219,26 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	accepted_consoles |= GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER;
 	break;
 
+      case MULTIBOOT_HEADER_TAG_RELOCATABLE:
+	relocatable = 1;
+	min_addr = ((struct multiboot_header_tag_relocatable *) tag)->min_addr;
+	max_addr = ((struct multiboot_header_tag_relocatable *) tag)->max_addr;
+	align = ((struct multiboot_header_tag_relocatable *) tag)->align;
+	switch (((struct multiboot_header_tag_relocatable *) tag)->preference)
+	  {
+	  case MULTIBOOT_LOAD_PREFERENCE_LOW:
+	    preference = GRUB_RELOCATOR_PREFERENCE_LOW;
+	    break;
+
+	  case MULTIBOOT_LOAD_PREFERENCE_HIGH:
+	    preference = GRUB_RELOCATOR_PREFERENCE_HIGH;
+	    break;
+
+	  default:
+	    preference = GRUB_RELOCATOR_PREFERENCE_NONE;
+	  }
+	break;
+
 	/* GRUB always page-aligns modules.  */
       case MULTIBOOT_HEADER_TAG_MODULE_ALIGN:
 	break;
@@ -260,15 +284,22 @@ grub_multiboot_load (grub_file_t file, const char *filename)
       else
 	code_size = load_size;
 
-      err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, 
-					     &ch, load_addr,
-					     code_size);
+      if (relocatable)
+	err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
+						min_addr, max_addr - code_size,
+						code_size, align ? align : 1,
+						preference, keep_bs);
+      else
+	err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
+					       &ch, load_addr,
+					       code_size);
       if (err)
 	{
 	  grub_dprintf ("multiboot_loader", "Error loading aout kludge\n");
 	  grub_free (buffer);
 	  return err;
 	}
+      base_addr = get_physical_target_address (ch);
       source = get_virtual_current_address (ch);
 
       if ((grub_file_seek (file, offset)) == (grub_off_t) -1)
@@ -290,7 +321,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
     }
   else
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer,
+				     relocatable, min_addr, max_addr,
+				     align, preference, &base_addr, keep_bs);
       if (err)
 	{
 	  grub_free (buffer);
@@ -303,6 +336,14 @@ grub_multiboot_load (grub_file_t file, const char *filename)
   else if (entry_specified)
     grub_multiboot_payload_eip = entry;
 
+  if (relocatable)
+    {
+      if (base_addr > min_addr)
+	grub_multiboot_payload_eip += base_addr - min_addr;
+      else
+	grub_multiboot_payload_eip -= min_addr - base_addr;
+    }
+
   if (fbtag)
     err = grub_multiboot_set_console (GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER,
 				      accepted_consoles,
@@ -409,6 +450,7 @@ grub_multiboot_get_mbi_size (void)
 		 + grub_get_multiboot_mmap_count ()
 		 * sizeof (struct multiboot_mmap_entry)), MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_framebuffer), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_base_addr), MULTIBOOT_TAG_ALIGN)
 #ifdef GRUB_MACHINE_EFI
     + ALIGN_UP (sizeof (struct multiboot_tag_efi32), MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_efi64), MULTIBOOT_TAG_ALIGN)
@@ -693,6 +735,15 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
   ptrorig += (2 * sizeof (grub_uint32_t)) / sizeof (grub_properly_aligned_t);
 
   {
+    struct multiboot_tag_base_addr *tag = (struct multiboot_tag_base_addr *) ptrorig;
+    tag->type = MULTIBOOT_TAG_TYPE_BASE_ADDR;
+    tag->size = sizeof (struct multiboot_tag_base_addr); 
+    tag->base_addr = base_addr;
+    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+       / sizeof (grub_properly_aligned_t);
+  }
+
+  {
     struct multiboot_tag_string *tag = (struct multiboot_tag_string *) ptrorig;
     tag->type = MULTIBOOT_TAG_TYPE_CMDLINE;
     tag->size = sizeof (struct multiboot_tag_string) + cmdline_size; 
include/grub/multiboot.h +3 -1
index e13c084..ec322b0 100644
@@ -94,7 +94,9 @@ grub_multiboot_load (grub_file_t file, const char *filename);
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-			 void *buffer);
+			 void *buffer, int relocatable, grub_uint32_t min_addr,
+			 grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+			 grub_uint32_t *base_addr, int avoid_efi_boot_services);
 extern grub_size_t grub_multiboot_pure_size;
 extern grub_size_t grub_multiboot_alloc_mbi;
 extern grub_uint32_t grub_multiboot_payload_eip;
include/multiboot2.h +17 -0
index 9f97ddc..941a95d 100644
@@ -62,6 +62,7 @@
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
 #define MULTIBOOT_TAG_TYPE_EFI32_IH          19
 #define MULTIBOOT_TAG_TYPE_EFI64_IH          20
+#define MULTIBOOT_TAG_TYPE_BASE_ADDR         21
 
 #define MULTIBOOT_HEADER_TAG_END  0
 #define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST  1
@@ -72,11 +73,16 @@
 #define MULTIBOOT_HEADER_TAG_MODULE_ALIGN  6
 #define MULTIBOOT_HEADER_TAG_EFI_BS  7
 #define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64  9
+#define MULTIBOOT_HEADER_TAG_RELOCATABLE  10
 
 #define MULTIBOOT_ARCHITECTURE_I386  0
 #define MULTIBOOT_ARCHITECTURE_MIPS32  4
 #define MULTIBOOT_HEADER_TAG_OPTIONAL 1
 
+#define MULTIBOOT_LOAD_PREFERENCE_NONE 0
+#define MULTIBOOT_LOAD_PREFERENCE_LOW 1
+#define MULTIBOOT_LOAD_PREFERENCE_HIGH 2
+
 #define MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED 1
 #define MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED 2
 
@@ -169,6 +175,17 @@ struct multiboot_header_tag_module_align
   multiboot_uint32_t size;
 };
 
+struct multiboot_header_tag_relocatable
+{
+  multiboot_uint16_t type;
+  multiboot_uint16_t flags;
+  multiboot_uint32_t size;
+  multiboot_uint32_t min_addr;
+  multiboot_uint32_t max_addr;
+  multiboot_uint32_t align;
+  multiboot_uint32_t preference;
+};
+
 struct multiboot_color
 {
   multiboot_uint8_t red;
@@ -395,6 +412,13 @@ struct multiboot_tag_efi64_ih
   multiboot_uint64_t pointer;
 };
 

include/multiboot2.h struct multiboot_tag_efi64_ih · line 424

Quoted 10 lines
include/multiboot2.h +7 -0
+struct multiboot_tag_base_addr
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint32_t base_addr;
+};
+
 #endif /* ! ASM_FILE */
 
 #endif /* ! MULTIBOOT_HEADER */

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#52 lore raw

Re: [PATCH v2 5/6] multiboot2: Add support for relocatable images

In reply to Daniel Kiper
Quoted 1 line · Daniel Kiper

What is handling the actual relocation? Why doesn't this patch include support for ELF relocations?

Quoted 336 lines
---
 grub-core/loader/i386/multiboot_mbi.c |    6 ++--
 grub-core/loader/multiboot.c          |   12 +++++--
 grub-core/loader/multiboot_elfxx.c    |   28 +++++++++++----
 grub-core/loader/multiboot_mbi2.c     |   63 +++++++++++++++++++++++++++++----
 include/grub/multiboot.h              |    4 ++-
 include/multiboot2.h                  |   24 +++++++++++++
 6 files changed, 118 insertions(+), 19 deletions(-)
grub-core/loader/i386/multiboot_mbi.c +4 -2
index 956d0e3..abdb98b 100644
@@ -72,7 +72,8 @@ load_kernel (grub_file_t file, const char *filename,
   grub_err_t err;
   if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE)
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+				     GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);
       if (err == GRUB_ERR_UNKNOWN_OS && (header->flags & MULTIBOOT_AOUT_KLUDGE))
 	grub_errno = err = GRUB_ERR_NONE;
     }
@@ -118,7 +119,8 @@ load_kernel (grub_file_t file, const char *filename,
       return GRUB_ERR_NONE;
     }
 
-  return grub_multiboot_load_elf (file, filename, buffer);
+  return grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
+				  GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);
 }
 
 static struct multiboot_header *
grub-core/loader/multiboot.c +9 -3
index ca7154f..1b1f7a9 100644
@@ -190,12 +190,18 @@ static grub_uint64_t highest_load;
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-			 void *buffer)
+			 void *buffer, int relocatable, grub_uint32_t min_addr,
+			 grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+			 grub_uint32_t *base_addr, int avoid_efi_boot_services)
 {
   if (grub_multiboot_is_elf32 (buffer))
-    return grub_multiboot_load_elf32 (file, filename, buffer);
+    return grub_multiboot_load_elf32 (file, filename, buffer, relocatable,
+				      min_addr, max_addr, align, preference,
+				      base_addr, avoid_efi_boot_services);
   else if (grub_multiboot_is_elf64 (buffer))
-    return grub_multiboot_load_elf64 (file, filename, buffer);
+    return grub_multiboot_load_elf64 (file, filename, buffer, relocatable,
+				      min_addr, max_addr, align, preference,
+				      base_addr, avoid_efi_boot_services);
 
   return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF magic"));
 }
grub-core/loader/multiboot_elfxx.c +21 -7
index 6a220bd..4fce685 100644
@@ -51,7 +51,10 @@ CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
 }
 
 static grub_err_t
-CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, void *buffer)
+CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename,
+				     void *buffer, int relocatable, grub_uint32_t min_addr,
+				     grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+				     grub_uint32_t *base_addr, int avoid_efi_boot_services)
 {
   Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
   char *phdr_base;
@@ -89,19 +92,30 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi
 	  if (phdr(i)->p_paddr + phdr(i)->p_memsz > highest_load)
 	    highest_load = phdr(i)->p_paddr + phdr(i)->p_memsz;
 
-	  grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx\n",
-			i, (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);
+	  grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx, memsz=0x%lx, vaddr=0x%lx,"
+			"align=0x%lx, relocatable=%d, avoid_efi_boot_services=%d\n", i,
+			(long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz, (long) phdr(i)->p_vaddr,
+			(long) align, relocatable, avoid_efi_boot_services);
 
 	  {
 	    grub_relocator_chunk_t ch;
-	    err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, 
-						   &ch, phdr(i)->p_paddr,
-						   phdr(i)->p_memsz);
+
+	    if (relocatable)
+	      err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
+						      min_addr, max_addr - phdr(i)->p_memsz,
+						      phdr(i)->p_memsz, align ? align : 1,
+						      preference, avoid_efi_boot_services);
+	    else
+	      err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
+						     &ch, phdr(i)->p_paddr,
+						     phdr(i)->p_memsz);
 	    if (err)
 	      {
 		grub_dprintf ("multiboot_loader", "Error loading phdr %d\n", i);
 		return err;
 	      }
+	    if (base_addr)
+	      *base_addr = get_physical_target_address (ch);
 	    source = get_virtual_current_address (ch);
 	  }
 
@@ -208,7 +222,7 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char *filename, voi
 						    + 1, sh->sh_size,
 						    sh->sh_addralign,
 						    GRUB_RELOCATOR_PREFERENCE_NONE,
-						    0);
+						    avoid_efi_boot_services);
 	    if (err)
 	      {
 		grub_dprintf ("multiboot_loader", "Error loading shdr %d\n", i);
grub-core/loader/multiboot_mbi2.c +57 -6
index dc9c709..7ac64ec 100644
@@ -68,6 +68,7 @@ static grub_size_t elf_sec_num, elf_sec_entsize;
 static unsigned elf_sec_shstrndx;
 static void *elf_sections;
 static int keep_bs = 0;
+static grub_uint32_t base_addr = 0;
 
 void
 grub_multiboot_add_elfsyms (grub_size_t num, grub_size_t entsize,
@@ -107,11 +108,13 @@ grub_multiboot_load (grub_file_t file, const char *filename)
   grub_err_t err;
   struct multiboot_header_tag *tag;
   struct multiboot_header_tag_address *addr_tag = NULL;
-  int entry_specified = 0, efi_entry_specified = 0;
+  int entry_specified = 0, efi_entry_specified = 0, relocatable = 0;
   grub_addr_t entry = 0, efi_entry = 0;
-  grub_uint32_t console_required = 0;
+  grub_uint32_t console_required = 0, min_addr = 0;
+  grub_uint32_t max_addr = 0, preference = 0;
   struct multiboot_header_tag_framebuffer *fbtag = NULL;
   int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
+  grub_size_t align = 0;
 
   buffer = grub_malloc (MULTIBOOT_SEARCH);
   if (!buffer)
@@ -174,6 +177,7 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	      case MULTIBOOT_TAG_TYPE_EFI_BS:
 	      case MULTIBOOT_TAG_TYPE_EFI32_IH:
 	      case MULTIBOOT_TAG_TYPE_EFI64_IH:
+	      case MULTIBOOT_TAG_TYPE_BASE_ADDR:
 		break;
 
 	      default:
@@ -215,6 +219,26 @@ grub_multiboot_load (grub_file_t file, const char *filename)
 	accepted_consoles |= GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER;
 	break;
 
+      case MULTIBOOT_HEADER_TAG_RELOCATABLE:
+	relocatable = 1;
+	min_addr = ((struct multiboot_header_tag_relocatable *) tag)->min_addr;
+	max_addr = ((struct multiboot_header_tag_relocatable *) tag)->max_addr;
+	align = ((struct multiboot_header_tag_relocatable *) tag)->align;
+	switch (((struct multiboot_header_tag_relocatable *) tag)->preference)
+	  {
+	  case MULTIBOOT_LOAD_PREFERENCE_LOW:
+	    preference = GRUB_RELOCATOR_PREFERENCE_LOW;
+	    break;
+
+	  case MULTIBOOT_LOAD_PREFERENCE_HIGH:
+	    preference = GRUB_RELOCATOR_PREFERENCE_HIGH;
+	    break;
+
+	  default:
+	    preference = GRUB_RELOCATOR_PREFERENCE_NONE;
+	  }
+	break;
+
 	/* GRUB always page-aligns modules.  */
       case MULTIBOOT_HEADER_TAG_MODULE_ALIGN:
 	break;
@@ -260,15 +284,22 @@ grub_multiboot_load (grub_file_t file, const char *filename)
       else
 	code_size = load_size;
 
-      err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator, 
-					     &ch, load_addr,
-					     code_size);
+      if (relocatable)
+	err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator, &ch,
+						min_addr, max_addr - code_size,
+						code_size, align ? align : 1,
+						preference, keep_bs);
+      else
+	err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
+					       &ch, load_addr,
+					       code_size);
       if (err)
 	{
 	  grub_dprintf ("multiboot_loader", "Error loading aout kludge\n");
 	  grub_free (buffer);
 	  return err;
 	}
+      base_addr = get_physical_target_address (ch);
       source = get_virtual_current_address (ch);
 
       if ((grub_file_seek (file, offset)) == (grub_off_t) -1)
@@ -290,7 +321,9 @@ grub_multiboot_load (grub_file_t file, const char *filename)
     }
   else
     {
-      err = grub_multiboot_load_elf (file, filename, buffer);
+      err = grub_multiboot_load_elf (file, filename, buffer,
+				     relocatable, min_addr, max_addr,
+				     align, preference, &base_addr, keep_bs);
       if (err)
 	{
 	  grub_free (buffer);
@@ -303,6 +336,14 @@ grub_multiboot_load (grub_file_t file, const char *filename)
   else if (entry_specified)
     grub_multiboot_payload_eip = entry;
 
+  if (relocatable)
+    {
+      if (base_addr > min_addr)
+	grub_multiboot_payload_eip += base_addr - min_addr;
+      else
+	grub_multiboot_payload_eip -= min_addr - base_addr;
+    }
+
   if (fbtag)
     err = grub_multiboot_set_console (GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER,
 				      accepted_consoles,
@@ -409,6 +450,7 @@ grub_multiboot_get_mbi_size (void)
 		 + grub_get_multiboot_mmap_count ()
 		 * sizeof (struct multiboot_mmap_entry)), MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_framebuffer), MULTIBOOT_TAG_ALIGN)
+    + ALIGN_UP (sizeof (struct multiboot_tag_base_addr), MULTIBOOT_TAG_ALIGN)
 #ifdef GRUB_MACHINE_EFI
     + ALIGN_UP (sizeof (struct multiboot_tag_efi32), MULTIBOOT_TAG_ALIGN)
     + ALIGN_UP (sizeof (struct multiboot_tag_efi64), MULTIBOOT_TAG_ALIGN)
@@ -693,6 +735,15 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
   ptrorig += (2 * sizeof (grub_uint32_t)) / sizeof (grub_properly_aligned_t);
 
   {
+    struct multiboot_tag_base_addr *tag = (struct multiboot_tag_base_addr *) ptrorig;
+    tag->type = MULTIBOOT_TAG_TYPE_BASE_ADDR;
+    tag->size = sizeof (struct multiboot_tag_base_addr); 
+    tag->base_addr = base_addr;
+    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+       / sizeof (grub_properly_aligned_t);
+  }
+
+  {
     struct multiboot_tag_string *tag = (struct multiboot_tag_string *) ptrorig;
     tag->type = MULTIBOOT_TAG_TYPE_CMDLINE;
     tag->size = sizeof (struct multiboot_tag_string) + cmdline_size; 
include/grub/multiboot.h +3 -1
index e13c084..ec322b0 100644
@@ -94,7 +94,9 @@ grub_multiboot_load (grub_file_t file, const char *filename);
 /* Load ELF32 or ELF64.  */
 grub_err_t
 grub_multiboot_load_elf (grub_file_t file, const char *filename,
-			 void *buffer);
+			 void *buffer, int relocatable, grub_uint32_t min_addr,
+			 grub_uint32_t max_addr, grub_size_t align, grub_uint32_t preference,
+			 grub_uint32_t *base_addr, int avoid_efi_boot_services);
 extern grub_size_t grub_multiboot_pure_size;
 extern grub_size_t grub_multiboot_alloc_mbi;
 extern grub_uint32_t grub_multiboot_payload_eip;
include/multiboot2.h +24 -0
index 9f97ddc..941a95d 100644
@@ -62,6 +62,7 @@
 #define MULTIBOOT_TAG_TYPE_EFI_BS            18
 #define MULTIBOOT_TAG_TYPE_EFI32_IH          19
 #define MULTIBOOT_TAG_TYPE_EFI64_IH          20
+#define MULTIBOOT_TAG_TYPE_BASE_ADDR         21
 
 #define MULTIBOOT_HEADER_TAG_END  0
 #define MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST  1
@@ -72,11 +73,16 @@
 #define MULTIBOOT_HEADER_TAG_MODULE_ALIGN  6
 #define MULTIBOOT_HEADER_TAG_EFI_BS  7
 #define MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS_EFI64  9
+#define MULTIBOOT_HEADER_TAG_RELOCATABLE  10
 
 #define MULTIBOOT_ARCHITECTURE_I386  0
 #define MULTIBOOT_ARCHITECTURE_MIPS32  4
 #define MULTIBOOT_HEADER_TAG_OPTIONAL 1
 
+#define MULTIBOOT_LOAD_PREFERENCE_NONE 0
+#define MULTIBOOT_LOAD_PREFERENCE_LOW 1
+#define MULTIBOOT_LOAD_PREFERENCE_HIGH 2
+
 #define MULTIBOOT_CONSOLE_FLAGS_CONSOLE_REQUIRED 1
 #define MULTIBOOT_CONSOLE_FLAGS_EGA_TEXT_SUPPORTED 2
 
@@ -169,6 +175,17 @@ struct multiboot_header_tag_module_align
   multiboot_uint32_t size;
 };
 
+struct multiboot_header_tag_relocatable
+{
+  multiboot_uint16_t type;
+  multiboot_uint16_t flags;
+  multiboot_uint32_t size;
+  multiboot_uint32_t min_addr;
+  multiboot_uint32_t max_addr;
+  multiboot_uint32_t align;
+  multiboot_uint32_t preference;
+};
+
 struct multiboot_color
 {
   multiboot_uint8_t red;
@@ -395,6 +412,13 @@ struct multiboot_tag_efi64_ih
   multiboot_uint64_t pointer;
 };
 
+struct multiboot_tag_base_addr
+{
+  multiboot_uint32_t type;
+  multiboot_uint32_t size;
+  multiboot_uint32_t base_addr;
+};
+
 #endif /* ! ASM_FILE */
 
 #endif /* ! MULTIBOOT_HEADER */
#53 lore raw

Re: [PATCH v2 5/6] multiboot2: Add support for relocatable images

Quoted 3 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 1 line · Daniel Kiper

What is handling the actual relocation?

In Xen we get image offset from GRUB2 and using this value we calculate relative addresses. We are using %fs for it.

Quoted 1 line

Why doesn't this patch include support for ELF relocations?

This is another option. We considered it too. However, in our case relative addressing looks simpler then ELF relocations. However, if you wish I can add this functionality when this patch series is included in upstream GRUB2. Is it OK for you?

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

A message that is not in the archive (sent off-list or not yet received): CAEaD8JMUUnSd7MMTANouZ1oQvYA00c49V8ENgUGUY+K8HcE_+Q@mail.gmail.com

A message that is not in the archive (sent off-list or not yet received): [email protected]

#56 lore raw

Re: [PATCH v2 5/6] multiboot2: Add support for relocatable images

Quoted 19 lines · Daniel Kiper
Quoted 7 lines · Vladimir 'phcoder' Serbinenko
Quoted 1 line · Daniel Kiper

On Mon, Nov 09, 2015 at 09:08:35PM +0100, Vladimir 'φ-coder/phcoder'

Quoted 4 lines · Serbinenko
Quoted 3 lines · Daniel Kiper
Quoted 1 line

What is handling the actual relocation?

Quoted 8 lines
Quoted 7 lines

In Xen we get image offset from GRUB2 and using this value we calculate relative addresses. We are using %fs for it.

Quoted 1 line

Why doesn't this patch include support for ELF relocations?

This is another option. We considered it too. However, in our case relative addressing looks simpler then ELF relocations.

How is it simpler than to have a fully relocated binary when you start?

Xen ELF file does not have relocations.

Quoted 8 lines
Quoted 1 line

How do you pass the offset?

Via MULTIBOOT_TAG_TYPE_BASE_ADDR tag.

Quoted 1 line

Does xen have any relocation entries?

No.

Can we then settle on using your interface but saying that bootloader has to execute all ELF relocations? Since you don't have them, you wouldn't be affected by the change but it would allow easy creation of relocatable binaries. Feel free in the first patch just to have a check for absence of relocation entries. x86-64 has only about 5 relocations we care about so should be easy to implement. We can reuse code in dl.c.

Quoted 11 lines
Quoted 2 lines

Was such xen already released? Just looking for how it should be in perspective and how to get there

We agreed (in Xen community) that we would like to have multiboot2 protocol with additional features set in stone at first. So, this patch series just propose some changes which are required by Xen but they are not used by any Xen release yet. Additionally, we want that these extensions are quite generic and could be used by other projects if they need them too.

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#57 lore raw

Re: [PATCH v2 5/6] multiboot2: Add support for relocatable images

In reply to a message not in the archive
Quoted 19 lines · Daniel Kiper
Quoted 7 lines · Vladimir 'phcoder' Serbinenko
Quoted 1 line · Daniel Kiper

On Mon, Nov 09, 2015 at 09:08:35PM +0100, Vladimir 'φ-coder/phcoder'

Quoted 4 lines · Serbinenko
Quoted 3 lines · Daniel Kiper
Quoted 1 line

What is handling the actual relocation?

Quoted 8 lines
Quoted 7 lines

In Xen we get image offset from GRUB2 and using this value we calculate relative addresses. We are using %fs for it.

Quoted 1 line

Why doesn't this patch include support for ELF relocations?

This is another option. We considered it too. However, in our case relative addressing looks simpler then ELF relocations.

How is it simpler than to have a fully relocated binary when you start?

Xen ELF file does not have relocations.

Quoted 8 lines
Quoted 1 line

How do you pass the offset?

Via MULTIBOOT_TAG_TYPE_BASE_ADDR tag.

Quoted 1 line

Does xen have any relocation entries?

No.

Can we then settle on using your interface but saying that bootloader has to execute all ELF relocations? Since you don't have them, you wouldn't be affected by the change but it would allow easy creation of relocatable binaries. Feel free in the first patch just to have a check for absence of relocation entries. x86-64 has only about 5 relocations we care about so should be easy to implement. We can reuse code in dl.c.

Quoted 11 lines
Quoted 2 lines

Was such xen already released? Just looking for how it should be in perspective and how to get there

We agreed (in Xen community) that we would like to have multiboot2 protocol with additional features set in stone at first. So, this patch series just propose some changes which are required by Xen but they are not used by any Xen release yet. Additionally, we want that these extensions are quite generic and could be used by other projects if they need them too.

Daniel

#58 lore raw

Re: [PATCH v2 5/6] multiboot2: Add support for relocatable images

Quoted 26 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 25 lines · Daniel Kiper
Quoted 7 lines · Vladimir 'phcoder' Serbinenko
Quoted 1 line · Daniel Kiper

On Mon, Nov 09, 2015 at 09:08:35PM +0100, Vladimir 'φ-coder/phcoder'

Quoted 4 lines · Serbinenko
Quoted 3 lines · Daniel Kiper
Quoted 1 line

What is handling the actual relocation?

Quoted 8 lines
Quoted 7 lines

In Xen we get image offset from GRUB2 and using this value we calculate relative addresses. We are using %fs for it.

Quoted 1 line

Why doesn't this patch include support for ELF relocations?

This is another option. We considered it too. However, in our case relative addressing looks simpler then ELF relocations.

How is it simpler than to have a fully relocated binary when you start?

Xen ELF file does not have relocations.

Quoted 1 line

How do you pass the offset?

Via MULTIBOOT_TAG_TYPE_BASE_ADDR tag.

Quoted 1 line

Does xen have any relocation entries?

Quoted 8 lines
Quoted 2 lines

No.

Can we then settle on using your interface but saying that bootloader has to execute all ELF relocations? Since you don't have them, you wouldn't be affected by the change but it would allow easy creation of relocatable binaries. Feel free in the first patch just to have a check for absence of relocation entries. x86-64 has only about 5 relocations we care about so should be easy to implement. We can reuse code in dl.c.

Granted!

Daniel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#59 lore raw

Re: [PATCH v2 5/6] multiboot2: Add support for relocatable images

In reply to Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 26 lines · Vladimir 'φ-coder/phcoder' Serbinenko
Quoted 25 lines · Daniel Kiper
Quoted 7 lines · Vladimir 'phcoder' Serbinenko
Quoted 1 line · Daniel Kiper

On Mon, Nov 09, 2015 at 09:08:35PM +0100, Vladimir 'φ-coder/phcoder'

Quoted 4 lines · Serbinenko
Quoted 3 lines · Daniel Kiper
Quoted 1 line

What is handling the actual relocation?

Quoted 8 lines
Quoted 7 lines

In Xen we get image offset from GRUB2 and using this value we calculate relative addresses. We are using %fs for it.

Quoted 1 line

Why doesn't this patch include support for ELF relocations?

This is another option. We considered it too. However, in our case relative addressing looks simpler then ELF relocations.

How is it simpler than to have a fully relocated binary when you start?

Xen ELF file does not have relocations.

Quoted 1 line

How do you pass the offset?

Via MULTIBOOT_TAG_TYPE_BASE_ADDR tag.

Quoted 1 line

Does xen have any relocation entries?

Quoted 8 lines
Quoted 2 lines

No.

Can we then settle on using your interface but saying that bootloader has to execute all ELF relocations? Since you don't have them, you wouldn't be affected by the change but it would allow easy creation of relocatable binaries. Feel free in the first patch just to have a check for absence of relocation entries. x86-64 has only about 5 relocations we care about so should be easy to implement. We can reuse code in dl.c.

Granted!

Daniel

#60 lore raw

PATCH v2 6/6 multiboot2: Do not pass memory maps to image if EFI boot services are enabled

In reply to Daniel Kiper

Do not pass memory maps to image if it asked for EFI boot services. Maps are usually invalid in that case and they can confuse potential user. Image should get memory map itself just before ExitBootServices() call.

---
 grub-core/loader/multiboot_mbi2.c |   71 ++++++++++++++++++-------------------
 1 file changed, 35 insertions(+), 36 deletions(-)
grub-core/loader/multiboot_mbi2.c +35 -36
index 7ac64ec..26e955c 100644
@@ -431,7 +431,7 @@ static grub_size_t
 grub_multiboot_get_mbi_size (void)
 {
 #ifdef GRUB_MACHINE_EFI
-  if (!efi_mmap_size)
+  if (!keep_bs && !efi_mmap_size)
     find_efi_mmap_size ();    
 #endif
   return 2 * sizeof (grub_uint32_t) + sizeof (struct multiboot_tag)
@@ -805,12 +805,13 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
       }
   }
 
-  {
-    struct multiboot_tag_mmap *tag = (struct multiboot_tag_mmap *) ptrorig;
-    grub_fill_multiboot_mmap (tag);
-    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-      / sizeof (grub_properly_aligned_t);
-  }
+  if (!keep_bs)
+    {
+      struct multiboot_tag_mmap *tag = (struct multiboot_tag_mmap *) ptrorig;
+      grub_fill_multiboot_mmap (tag);
+      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	/ sizeof (grub_properly_aligned_t);
+    }
 
   {
     struct multiboot_tag_elf_sections *tag
@@ -826,18 +827,19 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
       / sizeof (grub_properly_aligned_t);
   }
 
-  {
-    struct multiboot_tag_basic_meminfo *tag
-      = (struct multiboot_tag_basic_meminfo *) ptrorig;
-    tag->type = MULTIBOOT_TAG_TYPE_BASIC_MEMINFO;
-    tag->size = sizeof (struct multiboot_tag_basic_meminfo); 
+  if (!keep_bs)
+    {
+      struct multiboot_tag_basic_meminfo *tag
+	= (struct multiboot_tag_basic_meminfo *) ptrorig;
+      tag->type = MULTIBOOT_TAG_TYPE_BASIC_MEMINFO;
+      tag->size = sizeof (struct multiboot_tag_basic_meminfo);
 
-    /* Convert from bytes to kilobytes.  */
-    tag->mem_lower = grub_mmap_get_lower () / 1024;
-    tag->mem_upper = grub_mmap_get_upper () / 1024;
-    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-       / sizeof (grub_properly_aligned_t);
-  }
+      /* Convert from bytes to kilobytes.  */
+      tag->mem_lower = grub_mmap_get_lower () / 1024;
+      tag->mem_upper = grub_mmap_get_upper () / 1024;
+      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	/ sizeof (grub_properly_aligned_t);
+    }
 
   {
     struct grub_net_network_level_interface *net;
@@ -936,27 +938,24 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
     grub_efi_uintn_t efi_desc_size;
     grub_efi_uint32_t efi_desc_version;
 
-    tag->type = MULTIBOOT_TAG_TYPE_EFI_MMAP;
-    tag->size = sizeof (*tag) + efi_mmap_size;
-
     if (!keep_bs)
-      err = grub_efi_finish_boot_services (&efi_mmap_size, tag->efi_mmap, NULL,
-					   &efi_desc_size, &efi_desc_version);
-    else
       {
-	if (grub_efi_get_memory_map (&efi_mmap_size, (void *) tag->efi_mmap,
-				     NULL,
-				     &efi_desc_size, &efi_desc_version) <= 0)
-	  err = grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
+	tag->type = MULTIBOOT_TAG_TYPE_EFI_MMAP;
+	tag->size = sizeof (*tag) + efi_mmap_size;
+
+	err = grub_efi_finish_boot_services (&efi_mmap_size, tag->efi_mmap, NULL,
+					     &efi_desc_size, &efi_desc_version);
+
+	if (err)
+	  return err;
+
+	tag->descr_size = efi_desc_size;
+	tag->descr_vers = efi_desc_version;
+	tag->size = sizeof (*tag) + efi_mmap_size;
+
+	ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	  / sizeof (grub_properly_aligned_t);
       }
-    if (err)
-      return err;
-    tag->descr_size = efi_desc_size;
-    tag->descr_vers = efi_desc_version;
-    tag->size = sizeof (*tag) + efi_mmap_size;
-
-    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-      / sizeof (grub_properly_aligned_t);
   }
 
   if (keep_bs)
Signature · 1 line
1.7.10.4
Reviewed-by on v2 6/6
#61 lore raw

Re: [PATCH v2 6/6] multiboot2: Do not pass memory maps to image if EFI boot services are enabled

Quoted 3 lines · Daniel Kiper

Do not pass memory maps to image if it asked for EFI boot services. Maps are usually invalid in that case and they can confuse potential user. Image should get memory map itself just before ExitBootServices() call.

Can we point to some commits in Linux or Xen in which these situations arose?

Wait, I think there even was one commit in grub.

Aha:

ommit e75fdee420a7ad95e9a465c9699adc2e2e970440

Author: Vladimir 'phcoder' Serbinenko <[email protected]>
Date:   Tue Mar 26 11:34:56 2013 +0100

* grub-core/kern/efi/mm.c (grub_efi_finish_boot_services):
Try terminating EFI services several times due to quirks in some implementations.

Otherwise:

Quoted 116 lines
---
 grub-core/loader/multiboot_mbi2.c |   71 ++++++++++++++++++-------------------
 1 file changed, 35 insertions(+), 36 deletions(-)
grub-core/loader/multiboot_mbi2.c +35 -36
index 7ac64ec..26e955c 100644
@@ -431,7 +431,7 @@ static grub_size_t
 grub_multiboot_get_mbi_size (void)
 {
 #ifdef GRUB_MACHINE_EFI
-  if (!efi_mmap_size)
+  if (!keep_bs && !efi_mmap_size)
     find_efi_mmap_size ();    
 #endif
   return 2 * sizeof (grub_uint32_t) + sizeof (struct multiboot_tag)
@@ -805,12 +805,13 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
       }
   }
 
-  {
-    struct multiboot_tag_mmap *tag = (struct multiboot_tag_mmap *) ptrorig;
-    grub_fill_multiboot_mmap (tag);
-    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-      / sizeof (grub_properly_aligned_t);
-  }
+  if (!keep_bs)
+    {
+      struct multiboot_tag_mmap *tag = (struct multiboot_tag_mmap *) ptrorig;
+      grub_fill_multiboot_mmap (tag);
+      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	/ sizeof (grub_properly_aligned_t);
+    }
 
   {
     struct multiboot_tag_elf_sections *tag
@@ -826,18 +827,19 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
       / sizeof (grub_properly_aligned_t);
   }
 
-  {
-    struct multiboot_tag_basic_meminfo *tag
-      = (struct multiboot_tag_basic_meminfo *) ptrorig;
-    tag->type = MULTIBOOT_TAG_TYPE_BASIC_MEMINFO;
-    tag->size = sizeof (struct multiboot_tag_basic_meminfo); 
+  if (!keep_bs)
+    {
+      struct multiboot_tag_basic_meminfo *tag
+	= (struct multiboot_tag_basic_meminfo *) ptrorig;
+      tag->type = MULTIBOOT_TAG_TYPE_BASIC_MEMINFO;
+      tag->size = sizeof (struct multiboot_tag_basic_meminfo);
 
-    /* Convert from bytes to kilobytes.  */
-    tag->mem_lower = grub_mmap_get_lower () / 1024;
-    tag->mem_upper = grub_mmap_get_upper () / 1024;
-    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-       / sizeof (grub_properly_aligned_t);
-  }
+      /* Convert from bytes to kilobytes.  */
+      tag->mem_lower = grub_mmap_get_lower () / 1024;
+      tag->mem_upper = grub_mmap_get_upper () / 1024;
+      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	/ sizeof (grub_properly_aligned_t);
+    }
 
   {
     struct grub_net_network_level_interface *net;
@@ -936,27 +938,24 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
     grub_efi_uintn_t efi_desc_size;
     grub_efi_uint32_t efi_desc_version;
 
-    tag->type = MULTIBOOT_TAG_TYPE_EFI_MMAP;
-    tag->size = sizeof (*tag) + efi_mmap_size;
-
     if (!keep_bs)
-      err = grub_efi_finish_boot_services (&efi_mmap_size, tag->efi_mmap, NULL,
-					   &efi_desc_size, &efi_desc_version);
-    else
       {
-	if (grub_efi_get_memory_map (&efi_mmap_size, (void *) tag->efi_mmap,
-				     NULL,
-				     &efi_desc_size, &efi_desc_version) <= 0)
-	  err = grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
+	tag->type = MULTIBOOT_TAG_TYPE_EFI_MMAP;
+	tag->size = sizeof (*tag) + efi_mmap_size;
+
+	err = grub_efi_finish_boot_services (&efi_mmap_size, tag->efi_mmap, NULL,
+					     &efi_desc_size, &efi_desc_version);
+
+	if (err)
+	  return err;
+
+	tag->descr_size = efi_desc_size;
+	tag->descr_vers = efi_desc_version;
+	tag->size = sizeof (*tag) + efi_mmap_size;
+
+	ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	  / sizeof (grub_properly_aligned_t);
       }
-    if (err)
-      return err;
-    tag->descr_size = efi_desc_size;
-    tag->descr_vers = efi_desc_version;
-    tag->size = sizeof (*tag) + efi_mmap_size;
-
-    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-      / sizeof (grub_properly_aligned_t);
   }
 
   if (keep_bs)
Signature · 1 line
1.7.10.4
#62 lore raw

Re: [PATCH v2 6/6] multiboot2: Do not pass memory maps to image if EFI boot services are enabled

Quoted 16 lines · Konrad Rzeszutek Wilk
Quoted 3 lines · Daniel Kiper

Do not pass memory maps to image if it asked for EFI boot services. Maps are usually invalid in that case and they can confuse potential user. Image should get memory map itself just before ExitBootServices() call.

Can we point to some commits in Linux or Xen in which these situations arose?

Wait, I think there even was one commit in grub.

Aha:

ommit e75fdee420a7ad95e9a465c9699adc2e2e970440

Author: Vladimir 'phcoder' Serbinenko <[email protected]>
Date:   Tue Mar 26 11:34:56 2013 +0100
Quoted 7 lines

* grub-core/kern/efi/mm.c (grub_efi_finish_boot_services):
Try terminating EFI services several times due to quirks in some implementations.

Otherwise:

Nope, this is a bit different problem. In general GRUB2 assumes that EFI boot services memory regions are free and specify them as such in relevant maps. However, this is not true if you jump into loaded image with EFI boot services enabled. So, we should not expose this maps to the user when he/she asked for access to EFI boot services. This is not very big issue for him/her because he/she can get memory map from EFI just before ExitBootServices() call. This way they will have latest valid memory map.

Daniel

#63 lore raw

Re: [PATCH v2 6/6] multiboot2: Do not pass memory maps to image if EFI boot services are enabled

In reply to Konrad Rzeszutek Wilk
Quoted 16 lines · Konrad Rzeszutek Wilk
Quoted 3 lines · Daniel Kiper

Do not pass memory maps to image if it asked for EFI boot services. Maps are usually invalid in that case and they can confuse potential user. Image should get memory map itself just before ExitBootServices() call.

Can we point to some commits in Linux or Xen in which these situations arose?

Wait, I think there even was one commit in grub.

Aha:

ommit e75fdee420a7ad95e9a465c9699adc2e2e970440

Author: Vladimir 'phcoder' Serbinenko <[email protected]>
Date:   Tue Mar 26 11:34:56 2013 +0100
Quoted 7 lines

* grub-core/kern/efi/mm.c (grub_efi_finish_boot_services):
Try terminating EFI services several times due to quirks in some implementations.

Otherwise:

Nope, this is a bit different problem. In general GRUB2 assumes that EFI boot services memory regions are free and specify them as such in relevant maps. However, this is not true if you jump into loaded image with EFI boot services enabled. So, we should not expose this maps to the user when he/she asked for access to EFI boot services. This is not very big issue for him/her because he/she can get memory map from EFI just before ExitBootServices() call. This way they will have latest valid memory map.

Daniel

Reviewed-by on v2 6/6
#64 lore raw

Re: [PATCH v2 6/6] multiboot2: Do not pass memory maps to image if EFI boot services are enabled

In reply to Daniel Kiper
Quoted 3 lines · Daniel Kiper

Do not pass memory maps to image if it asked for EFI boot services. Maps are usually invalid in that case and they can confuse potential user. Image should get memory map itself just before ExitBootServices() call.

Can we point to some commits in Linux or Xen in which these situations arose?

Wait, I think there even was one commit in grub.

Aha:

ommit e75fdee420a7ad95e9a465c9699adc2e2e970440

Author: Vladimir 'phcoder' Serbinenko <[email protected]>
Date:   Tue Mar 26 11:34:56 2013 +0100

* grub-core/kern/efi/mm.c (grub_efi_finish_boot_services):
Try terminating EFI services several times due to quirks in some implementations.

Otherwise:

Quoted 116 lines
---
 grub-core/loader/multiboot_mbi2.c |   71 ++++++++++++++++++-------------------
 1 file changed, 35 insertions(+), 36 deletions(-)
grub-core/loader/multiboot_mbi2.c +35 -36
index 7ac64ec..26e955c 100644
@@ -431,7 +431,7 @@ static grub_size_t
 grub_multiboot_get_mbi_size (void)
 {
 #ifdef GRUB_MACHINE_EFI
-  if (!efi_mmap_size)
+  if (!keep_bs && !efi_mmap_size)
     find_efi_mmap_size ();    
 #endif
   return 2 * sizeof (grub_uint32_t) + sizeof (struct multiboot_tag)
@@ -805,12 +805,13 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
       }
   }
 
-  {
-    struct multiboot_tag_mmap *tag = (struct multiboot_tag_mmap *) ptrorig;
-    grub_fill_multiboot_mmap (tag);
-    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-      / sizeof (grub_properly_aligned_t);
-  }
+  if (!keep_bs)
+    {
+      struct multiboot_tag_mmap *tag = (struct multiboot_tag_mmap *) ptrorig;
+      grub_fill_multiboot_mmap (tag);
+      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	/ sizeof (grub_properly_aligned_t);
+    }
 
   {
     struct multiboot_tag_elf_sections *tag
@@ -826,18 +827,19 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
       / sizeof (grub_properly_aligned_t);
   }
 
-  {
-    struct multiboot_tag_basic_meminfo *tag
-      = (struct multiboot_tag_basic_meminfo *) ptrorig;
-    tag->type = MULTIBOOT_TAG_TYPE_BASIC_MEMINFO;
-    tag->size = sizeof (struct multiboot_tag_basic_meminfo); 
+  if (!keep_bs)
+    {
+      struct multiboot_tag_basic_meminfo *tag
+	= (struct multiboot_tag_basic_meminfo *) ptrorig;
+      tag->type = MULTIBOOT_TAG_TYPE_BASIC_MEMINFO;
+      tag->size = sizeof (struct multiboot_tag_basic_meminfo);
 
-    /* Convert from bytes to kilobytes.  */
-    tag->mem_lower = grub_mmap_get_lower () / 1024;
-    tag->mem_upper = grub_mmap_get_upper () / 1024;
-    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-       / sizeof (grub_properly_aligned_t);
-  }
+      /* Convert from bytes to kilobytes.  */
+      tag->mem_lower = grub_mmap_get_lower () / 1024;
+      tag->mem_upper = grub_mmap_get_upper () / 1024;
+      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	/ sizeof (grub_properly_aligned_t);
+    }
 
   {
     struct grub_net_network_level_interface *net;
@@ -936,27 +938,24 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
     grub_efi_uintn_t efi_desc_size;
     grub_efi_uint32_t efi_desc_version;
 
-    tag->type = MULTIBOOT_TAG_TYPE_EFI_MMAP;
-    tag->size = sizeof (*tag) + efi_mmap_size;
-
     if (!keep_bs)
-      err = grub_efi_finish_boot_services (&efi_mmap_size, tag->efi_mmap, NULL,
-					   &efi_desc_size, &efi_desc_version);
-    else
       {
-	if (grub_efi_get_memory_map (&efi_mmap_size, (void *) tag->efi_mmap,
-				     NULL,
-				     &efi_desc_size, &efi_desc_version) <= 0)
-	  err = grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
+	tag->type = MULTIBOOT_TAG_TYPE_EFI_MMAP;
+	tag->size = sizeof (*tag) + efi_mmap_size;
+
+	err = grub_efi_finish_boot_services (&efi_mmap_size, tag->efi_mmap, NULL,
+					     &efi_desc_size, &efi_desc_version);
+
+	if (err)
+	  return err;
+
+	tag->descr_size = efi_desc_size;
+	tag->descr_vers = efi_desc_version;
+	tag->size = sizeof (*tag) + efi_mmap_size;
+
+	ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	  / sizeof (grub_properly_aligned_t);
       }
-    if (err)
-      return err;
-    tag->descr_size = efi_desc_size;
-    tag->descr_vers = efi_desc_version;
-    tag->size = sizeof (*tag) + efi_mmap_size;
-
-    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-      / sizeof (grub_properly_aligned_t);
   }
 
   if (keep_bs)
Signature · 1 line
1.7.10.4
#65 lore raw

PATCH v2 6/6 multiboot2: Do not pass memory maps to image if EFI boot services are enabled

In reply to Daniel Kiper

Do not pass memory maps to image if it asked for EFI boot services. Maps are usually invalid in that case and they can confuse potential user. Image should get memory map itself just before ExitBootServices() call.

---
 grub-core/loader/multiboot_mbi2.c |   71 ++++++++++++++++++-------------------
 1 file changed, 35 insertions(+), 36 deletions(-)
grub-core/loader/multiboot_mbi2.c +35 -36
index 7ac64ec..26e955c 100644
@@ -431,7 +431,7 @@ static grub_size_t
 grub_multiboot_get_mbi_size (void)
 {
 #ifdef GRUB_MACHINE_EFI
-  if (!efi_mmap_size)
+  if (!keep_bs && !efi_mmap_size)
     find_efi_mmap_size ();    
 #endif
   return 2 * sizeof (grub_uint32_t) + sizeof (struct multiboot_tag)
@@ -805,12 +805,13 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
       }
   }
 
-  {
-    struct multiboot_tag_mmap *tag = (struct multiboot_tag_mmap *) ptrorig;
-    grub_fill_multiboot_mmap (tag);
-    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-      / sizeof (grub_properly_aligned_t);
-  }
+  if (!keep_bs)
+    {
+      struct multiboot_tag_mmap *tag = (struct multiboot_tag_mmap *) ptrorig;
+      grub_fill_multiboot_mmap (tag);
+      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	/ sizeof (grub_properly_aligned_t);
+    }
 
   {
     struct multiboot_tag_elf_sections *tag
@@ -826,18 +827,19 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
       / sizeof (grub_properly_aligned_t);
   }
 
-  {
-    struct multiboot_tag_basic_meminfo *tag
-      = (struct multiboot_tag_basic_meminfo *) ptrorig;
-    tag->type = MULTIBOOT_TAG_TYPE_BASIC_MEMINFO;
-    tag->size = sizeof (struct multiboot_tag_basic_meminfo); 
+  if (!keep_bs)
+    {
+      struct multiboot_tag_basic_meminfo *tag
+	= (struct multiboot_tag_basic_meminfo *) ptrorig;
+      tag->type = MULTIBOOT_TAG_TYPE_BASIC_MEMINFO;
+      tag->size = sizeof (struct multiboot_tag_basic_meminfo);
 
-    /* Convert from bytes to kilobytes.  */
-    tag->mem_lower = grub_mmap_get_lower () / 1024;
-    tag->mem_upper = grub_mmap_get_upper () / 1024;
-    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-       / sizeof (grub_properly_aligned_t);
-  }
+      /* Convert from bytes to kilobytes.  */
+      tag->mem_lower = grub_mmap_get_lower () / 1024;
+      tag->mem_upper = grub_mmap_get_upper () / 1024;
+      ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	/ sizeof (grub_properly_aligned_t);
+    }
 
   {
     struct grub_net_network_level_interface *net;
@@ -936,27 +938,24 @@ grub_multiboot_make_mbi (grub_uint32_t *target)
     grub_efi_uintn_t efi_desc_size;
     grub_efi_uint32_t efi_desc_version;
 
-    tag->type = MULTIBOOT_TAG_TYPE_EFI_MMAP;
-    tag->size = sizeof (*tag) + efi_mmap_size;
-
     if (!keep_bs)
-      err = grub_efi_finish_boot_services (&efi_mmap_size, tag->efi_mmap, NULL,
-					   &efi_desc_size, &efi_desc_version);
-    else
       {
-	if (grub_efi_get_memory_map (&efi_mmap_size, (void *) tag->efi_mmap,
-				     NULL,
-				     &efi_desc_size, &efi_desc_version) <= 0)
-	  err = grub_error (GRUB_ERR_IO, "couldn't retrieve memory map");
+	tag->type = MULTIBOOT_TAG_TYPE_EFI_MMAP;
+	tag->size = sizeof (*tag) + efi_mmap_size;
+
+	err = grub_efi_finish_boot_services (&efi_mmap_size, tag->efi_mmap, NULL,
+					     &efi_desc_size, &efi_desc_version);
+
+	if (err)
+	  return err;
+
+	tag->descr_size = efi_desc_size;
+	tag->descr_vers = efi_desc_version;
+	tag->size = sizeof (*tag) + efi_mmap_size;
+
+	ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
+	  / sizeof (grub_properly_aligned_t);
       }
-    if (err)
-      return err;
-    tag->descr_size = efi_desc_size;
-    tag->descr_vers = efi_desc_version;
-    tag->size = sizeof (*tag) + efi_mmap_size;
-
-    ptrorig += ALIGN_UP (tag->size, MULTIBOOT_TAG_ALIGN)
-      / sizeof (grub_properly_aligned_t);
   }
 
   if (keep_bs)
Signature · 1 line
1.7.10.4
#66 lore raw
In reply to Daniel Kiper

Guys, especially GRUB2 maintainers,

Quoted 8 lines · Daniel Kiper

Hi,

This patch series:
- enables EFI boot services usage in loaded images by multiboot2 protocol,
- add support for multiboot2 protocol compatible relocatable images,
- fixes two minor issues.

Is it possible to get your comments to this patch series (I would like to thank you Andrei and Konrad for review)? We need this functionality as Xen community and as Oracle. Hence, it will be nice to know that we go in good direction. So, if you think that we should change something please drop me a line.

I know that you are busy but please, at least, tell us when you take a look at it.

Daniel

#67 lore raw
In reply to Daniel Kiper

Guys, especially GRUB2 maintainers,

Quoted 8 lines · Daniel Kiper

Hi,

This patch series:
- enables EFI boot services usage in loaded images by multiboot2 protocol,
- add support for multiboot2 protocol compatible relocatable images,
- fixes two minor issues.

Is it possible to get your comments to this patch series (I would like to thank you Andrei and Konrad for review)? We need this functionality as Xen community and as Oracle. Hence, it will be nice to know that we go in good direction. So, if you think that we should change something please drop me a line.

I know that you are busy but please, at least, tell us when you take a look at it.

Daniel