[SOLVED] How to rebuild GRUB (efi) with modifications

Kernels & Hardware, configuring network, installing services

[SOLVED] How to rebuild GRUB (efi) with modifications

Postby jrobin28260 » 2018-12-22 20:04

Hi everybody
My mission is having the bootaa64.efi file working on my ARM64 Laptop (Asus Novago), which is able to boot others "bootaa64.efi" files. Today, Debian and Ubuntu versions does not boot (it freezes at running the EFI file).

I successfully managed to build an ISO in a way that is identical to the official way, using https://salsa.debian.org/images-team/debian-cd and having a local mirror as needed. But it does not rebuild all the packages, it just put them together into a new ISO file.

Also, with https://github.com/pbatard/uefi-simple I successfully managed to compile my own "aarch64.efi" file (for an ARM64 Laptop) and having it working on my ARM64 Laptop (it just shows a message and a "press any key" message in order to check if it works - and it works). On this ARM64 computer (Qualcomm Snapdragon 835 CPU) I'm also able to boot Windows and a working Windows installer. BUT : Linux distros EFI files do not boot (it freezes and does nothing) and I want to debug it, by building the EFI file myself, and customize code and target options in order understand what's not working (and may be find a solution). (Same problem for Ubuntu ARM64 and Debian ARM64)

What is the way to do, the correct entry point, if I want to rebuild my own version of Debian Grub "bootaa64.efi" file ?
I'm interested in doing this on the "Buster" version because Linux 4.18 and higher already contains all the necessary drivers for having Qualcomm functionnalities working out of the box with no kernel modifications

Thanks in advance !
Last edited by jrobin28260 on 2018-12-30 23:21, edited 1 time in total.
jrobin28260
 
Posts: 42
Joined: 2018-11-09 20:30

Re: How to rebuild GRUB (efi) with code / target modificatio

Postby Head_on_a_Stick » 2018-12-22 20:11

The grub-efi package is only available for the x86 architectures, unfortunately.

You could try systemd-boot, that provides /usr/lib/systemd/boot/efi/systemd-bootaa64.efi

If that is copied to ${ESP}/efi/boot/bootaa64.efi then the UEFI firmware should boot it automatically.[1]

The systemd-boot program looks for the configuration files defined on the ArchWiki page:

https://wiki.archlinux.org/index.php/Systemd-boot

I boot my (x86) Debian stable system this way, it works well :)

EDIT: the kernel & initramfs need to be on the ESP for this to work, see https://wiki.debian.org/EFIStub for an example of a kernel post-install script that can automate this task.
Last edited by Head_on_a_Stick on 2018-12-22 20:24, edited 1 time in total.
User avatar
Head_on_a_Stick
 
Posts: 10368
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: How to rebuild GRUB (efi) with code / target modificatio

Postby jrobin28260 » 2018-12-22 20:23

That's a good entry point :D it means that what I'm looking to rebuild (the bootaa64.efi file) is not grub - In that case, I would have lost a lot of time by searching about GRUB

I noticed on a Intel x86_64 computer that placing "bootx64.efi" file only (the one contained into the amd64 ISO), grub command line appears on the screen
By doing the same on my Qualcomm aarch64 device with the "bootaa64.efi" from the ARM64 ISO, nothing happens (freeze), so I thought it was a grub EFI file problem

Seeing the packet "https://packages.debian.org/fr/buster/grub-efi-arm64" also gave me some doubts... Are you sure that the ARM64 version of the ISO isn't using it ?

Anyway, I'm going to take a look at your link : https://wiki.archlinux.org/index.php/Systemd-boot
I hope this will help me in my quest about "how to" !
Last edited by jrobin28260 on 2018-12-22 20:31, edited 1 time in total.
jrobin28260
 
Posts: 42
Joined: 2018-11-09 20:30

Re: How to rebuild GRUB (efi) with code / target modificatio

Postby Head_on_a_Stick » 2018-12-22 20:25

See my edit, the kernel image & initramfs need to be on the EFI system partition for systemd-boot to work.
User avatar
Head_on_a_Stick
 
Posts: 10368
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: How to rebuild GRUB (efi) with code / target modificatio

Postby Head_on_a_Stick » 2018-12-22 20:28

jrobin28260 wrote:Seeing the packet "https://packages.debian.org/fr/buster/grub-efi-arm64" also gave me some doubts... Are you sure that the ARM64 version of the ISO isn't using it ?

Oh yes, my mistake, sorry, don't know how I missed that.

Ah well, now you have an alternative method :)
User avatar
Head_on_a_Stick
 
Posts: 10368
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: How to rebuild GRUB (efi) with code / target modificatio

Postby jrobin28260 » 2018-12-22 20:32

An alternative method is more than welcome when talking about another one which doesn't work :) thank you !
jrobin28260
 
Posts: 42
Joined: 2018-11-09 20:30

Re: How to rebuild GRUB (efi) with code / target modificatio

Postby jrobin28260 » 2018-12-22 22:32

I've made some steps forward in my quest, I've found and downloaded the original/non-debian grub source code at ftp://ftp.gnu.org/gnu/grub/grub-2.02.tar.xz file and extracted it into a dedicated folder on my Desktop
Where can I find the equivalent file for Debian's version of grub ?

I've played the following lines :
Code: Select all
./configure
make

And I got a successful build of grub in its i386-pc version... that was not what was expected ;) but I had no error. I saw on the Internet that I should play
./configure --platform=efi
But it tells me that platform isn't a recognized option, and I don't know what is the option for having an x86_64 efi version (and not a i386-pc version). May be I should go to a computer running Debian trough EFI ?
EDIT : OK nevermind, it was ./configure --with-platform=efi :lol:

./configure --help gives the following result :
Code: Select all
`configure' configures GRUB 2.02 to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit
      --help=short        display options specific to this package
      --help=recursive    display the short help of all the included packages
  -V, --version           display version information and exit
  -q, --quiet, --silent   do not print `checking ...' messages
      --cache-file=FILE   cache test results in FILE [disabled]
  -C, --config-cache      alias for `--cache-file=config.cache'
  -n, --no-create         do not create output files
      --srcdir=DIR        find the sources in DIR [configure dir or `..']

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local]
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                          [PREFIX]

By default, `make install' will install all the files in
`/usr/local/bin', `/usr/local/lib' etc.  You can specify
an installation prefix other than `/usr/local' using `--prefix',
for instance `--prefix=$HOME'.

For better control, use the options below.

Fine tuning of the installation directories:
  --bindir=DIR            user executables [EPREFIX/bin]
  --sbindir=DIR           system admin executables [EPREFIX/sbin]
  --libexecdir=DIR        program executables [EPREFIX/libexec]
  --sysconfdir=DIR        read-only single-machine data [PREFIX/etc]
  --sharedstatedir=DIR    modifiable architecture-independent data [PREFIX/com]
  --localstatedir=DIR     modifiable single-machine data [PREFIX/var]
  --runstatedir=DIR       modifiable per-process data [LOCALSTATEDIR/run]
  --libdir=DIR            object code libraries [EPREFIX/lib]
  --includedir=DIR        C header files [PREFIX/include]
  --oldincludedir=DIR     C header files for non-gcc [/usr/include]
  --datarootdir=DIR       read-only arch.-independent data root [PREFIX/share]
  --datadir=DIR           read-only architecture-independent data [DATAROOTDIR]
  --infodir=DIR           info documentation [DATAROOTDIR/info]
  --localedir=DIR         locale-dependent data [DATAROOTDIR/locale]
  --mandir=DIR            man documentation [DATAROOTDIR/man]
  --docdir=DIR            documentation root [DATAROOTDIR/doc/grub]
  --htmldir=DIR           html documentation [DOCDIR]
  --dvidir=DIR            dvi documentation [DOCDIR]
  --pdfdir=DIR            pdf documentation [DOCDIR]
  --psdir=DIR             ps documentation [DOCDIR]

Program names:
  --program-prefix=PREFIX            prepend PREFIX to installed program names
  --program-suffix=SUFFIX            append SUFFIX to installed program names
  --program-transform-name=PROGRAM   run sed PROGRAM on installed program names

System types:
  --build=BUILD     configure for building on BUILD [guessed]
  --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  --target=TARGET   configure for building compilers for TARGET [HOST]

Optional Features:
  --disable-option-checking  ignore unrecognized --enable/--with options
  --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
  --enable-silent-rules   less verbose build output (undo: "make V=1")
  --disable-silent-rules  verbose build output (undo: "make V=0")
  --enable-dependency-tracking
                          do not reject slow dependency extractors
  --disable-dependency-tracking
                          speeds up one-time build
  --disable-nls           do not use Native Language Support
  --disable-rpath         do not hardcode runtime library paths
  --disable-largefile     omit support for large files
  --enable-efiemu         build and install the efiemu runtimes
                          (default=guessed)
  --enable-mm-debug       include memory manager debugging
  --enable-cache-stats    enable disk cache statistics collection
  --enable-boot-time      enable boot time statistics collection
  --enable-grub-emu-sdl   build and install the `grub-emu' debugging utility
                          with SDL support (default=guessed)
  --enable-grub-emu-pci   build and install the `grub-emu' debugging utility
                          with PCI support (potentially dangerous)
                          (default=no)
  --enable-grub-mkfont    build and install the `grub-mkfont' utility
                          (default=guessed)
  --enable-grub-themes    build and install GRUB themes (default=guessed)
  --enable-grub-mount     build and install the `grub-mount' utility
                          (default=guessed)
  --enable-device-mapper  enable Linux device-mapper support (default=guessed)
  --enable-liblzma        enable liblzma integration (default=guessed)
  --enable-libzfs         enable libzfs integration (default=guessed)
  --disable-werror        do not use -Werror when building GRUB

Optional Packages:
  --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]
  --without-PACKAGE       do not use PACKAGE (same as --with-PACKAGE=no)
  --with-platform=PLATFORM
                          select the host platform [[guessed]]
  --with-bootdir=DIR      set the name of /boot directory [[guessed]]
  --with-grubdir=DIR      set the name of grub directory [[guessed]]
  --with-gnu-ld           assume the C compiler uses GNU ld [default=no]
  --with-libiconv-prefix[=DIR]  search for libiconv in DIR/include and DIR/lib
  --without-libiconv-prefix     don't search for libiconv in includedir and libdir
  --with-libintl-prefix[=DIR]  search for libintl in DIR/include and DIR/lib
  --without-libintl-prefix     don't search for libintl in includedir and libdir
  --without-included-regex
                          don't compile regex; this is the default on systems
                          with recent-enough versions of the GNU C Library
                          (use with caution on other systems).

Some influential environment variables:
  CC          C compiler command
  CFLAGS      C compiler flags
  LDFLAGS     linker flags, e.g. -L<lib dir> if you have libraries in a
              nonstandard directory <lib dir>
  LIBS        libraries to pass to the linker, e.g. -l<library>
  CPPFLAGS    (Objective) C/C++ preprocessor flags, e.g. -I<include dir> if
              you have headers in a nonstandard directory <include dir>
  YACC        The `Yet Another Compiler Compiler' implementation to use.
              Defaults to the first program found out of: `bison -y', `byacc',
              `yacc'.
  YFLAGS      The list of arguments that will be passed by default to $YACC.
              This script will default YFLAGS to the empty string to avoid a
              default value of `-d' given by some make applications.
  CPP         C preprocessor
  CXX         C++ compiler command
  CXXFLAGS    C++ compiler flags
  CCAS        assembler compiler command (defaults to CC)
  CCASFLAGS   assembler compiler flags (defaults to CFLAGS)

Use these variables to override the choices made by `configure' or to help
it to find libraries and programs with nonstandard names/locations.

Report bugs to <bug-grub@gnu.org>.

What I would like to do is a bootx64.efi file that I can test on a x86_64 EFI laptop in order to see what should be expected on a working efi grub (command line, I guess). I would also be interested in trying to replace the efi file of the Debian installer USB key, just to see if it's working fine with the orginal grub code.
And then, build the arm64-efi version of grub in order to see if I can do the same, retry with different compiler flags, different options on "./configure"... and forget about code modifications because it looks like grub code is huge and separated into a lot of complicated files !

I've tried to reset the folder and play
Code: Select all
./configure --host=aarch64 --target=aarch64
make


But I got an error, like if the grub's code does not compile out of the box, at least for arm64 :
Code: Select all
[...]
[...]
gcc -DHAVE_CONFIG_H -I. -I..  -Wall -W  -DGRUB_MACHINE_EFI=1 -DGRUB_MACHINE=ARM64_EFI -nostdinc -isystem /usr/lib/gcc/x86_64-linux-gnu/6/include -I../include -I../include -DGRUB_FILE=\"loader/arm64/fdt.c\" -I. -I. -I.. -I.. -I../include -I../include -I../grub-core/lib/libgcrypt-grub/src/    -D_FILE_OFFSET_BITS=64 -Os -Wall -W -Wshadow -Wpointer-arith -Wundef -Wchar-subscripts -Wcomment -Wdeprecated-declarations -Wdisabled-optimization -Wdiv-by-zero -Wfloat-equal -Wformat-extra-args -Wformat-security -Wformat-y2k -Wimplicit -Wimplicit-function-declaration -Wimplicit-int -Wmain -Wmissing-braces -Wmissing-format-attribute -Wmultichar -Wparentheses -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wswitch -Wtrigraphs -Wunknown-pragmas -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value  -Wunused-variable -Wwrite-strings -Wnested-externs -Wstrict-prototypes -g -Wredundant-decls -Wmissing-prototypes -Wmissing-declarations -Wcast-align  -Wextra -Wattributes -Wendif-labels -Winit-self -Wint-to-pointer-cast -Winvalid-pch -Wmissing-field-initializers -Wnonnull -Woverflow -Wvla -Wpointer-to-int-cast -Wstrict-aliasing -Wvariadic-macros -Wvolatile-register-var -Wpointer-sign -Wmissing-include-dirs -Wmissing-prototypes -Wmissing-declarations -Wformat=2 -freg-struct-return -msoft-float -fno-dwarf2-cfi-asm -mno-stack-arg-probe -fno-asynchronous-unwind-tables -fno-unwind-tables -Qn -fno-PIE -fno-pie -fno-stack-protector -Wtrampolines -Werror -mcmodel=large  -ffreestanding   -MT loader/arm64/fdt_module-fdt.o -MD -MP -MF loader/arm64/.deps-core/fdt_module-fdt.Tpo -c -o loader/arm64/fdt_module-fdt.o `test -f 'loader/arm64/fdt.c' || echo './'`loader/arm64/fdt.c
loader/arm64/fdt.c: In function ‘grub_fdt_load’:
loader/arm64/fdt.c:46:15: error: implicit declaration of function ‘grub_efi_get_firmware_fdt’ [-Werror=implicit-function-declaration]
     raw_fdt = grub_efi_get_firmware_fdt();
               ^~~~~~~~~~~~~~~~~~~~~~~~~
loader/arm64/fdt.c:46:5: error: nested extern declaration of ‘grub_efi_get_firmware_fdt’ [-Werror=nested-externs]
     raw_fdt = grub_efi_get_firmware_fdt();
     ^~~~~~~
loader/arm64/fdt.c:46:13: error: assignment makes pointer from integer without a cast [-Werror=int-conversion]
     raw_fdt = grub_efi_get_firmware_fdt();
             ^
cc1: all warnings being treated as errors
Makefile:30552: recipe for target 'loader/arm64/fdt_module-fdt.o' failed
make[3]: *** [loader/arm64/fdt_module-fdt.o] Error 1
make[3]: Leaving directory '/home/user/Desktop/grub-source/grub-2.02/grub-core'
Makefile:23531: recipe for target 'all' failed
make[2]: *** [all] Error 2
make[2]: Leaving directory '/home/user/Desktop/grub-source/grub-2.02/grub-core'
Makefile:10904: recipe for target 'all-recursive' failed
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory '/home/user/Desktop/grub-source/grub-2.02'
Makefile:3130: recipe for target 'all' failed
make: *** [all] Error 2


If someone already did what I'm doing here, your suggestions are welcome !
I'll try to take a look at the error tomorrow after a good night, probably directly at the code, in order to understand what gcc is talking about.
And I'll try to build the bootx64.efi file for test from a standard x86_64 computer running on efi !

EDIT 2 : after the "make" command I do not find any "*.efi" file so I guess it builds tools that should, in a second time, be able to create the efi file ? May be grub-mkimage ?
I'm not very strong at "bootloaders" things but it's going to change quickly :lol:
jrobin28260
 
Posts: 42
Joined: 2018-11-09 20:30

Re: How to rebuild GRUB (efi) with code / target modificatio

Postby jrobin28260 » 2018-12-27 12:51

Hi back,

I had some lack of availability the last few days ;)
For doing the test on my EFI x86_64 computer with the same conditions, I had to build the grub2.02 for x86_64-efi and use the freshly created "grub-install". It created a bootx64.efi file that my x86_64 EFI computer managed to run, giving me an error at the end but it's fine : it's alive. Now the step forward is doing the same for my ARM64 laptop and hoping to find the build parameters or specific configuration that would allow me to display something on it's screen, even if it's an error.

The error I got some days ago, by trying to build grub creator files for running on my x86_64, and targeting my arm64 laptop :
Code: Select all
loader/arm64/fdt.c: In function ‘grub_fdt_load’:
loader/arm64/fdt.c:46:15: error: implicit declaration of function ‘grub_efi_get_firmware_fdt’ [-Werror=implicit-function-declaration]


It means that the compiler didn't see the header of this function, or even it's implementation yet, before getting here and encountering a call to this unknown function.

It was due to something into the included "include/grub/efi.h" file :
Code: Select all
#if defined(__arm__) || defined(__aarch64__)
void *EXPORT_FUNC(grub_efi_get_firmware_fdt)(void); //<- the declaration of the function is here, and this is included into fdt.c !
#endif


It seems that __arm__ or __aarch64__ aren't defined, so I've placed the #if and #endif in comment, and it worked few lines more... before getting to a new error.
In file included from term/efi/serial.c:29:0:
Code: Select all
../include/grub/serial.h:25:25: fatal error: grub/cpu/io.h: No such file or directory
 #include <grub/cpu/io.h>

From serial.c, it tried to #include <grub/cpu/io.h> which is only available if cpu is i386, mips, or x86_64. Mine is a symbolic link to arm64, and there is no io.h file in it.

By seeing the serial.c source code :
Code: Select all
#if !defined (GRUB_MACHINE_EMU) && (defined(__mips__) || defined (__i386__) || defined (__x86_64__))
#include <grub/cpu/io.h>
#endif

It seems, again, that the compiler is thinking x86_64 is defined while __aarch64__ is not.

Although I'm on a x86_64 computer, I've installed crossbuild-essential-arm64 and I already successfully built an ARM64 efi file with "uefi-simple" (but this is not grub)

For the configure script, I'm typing ./configure --build=x86_64 --host=x86_64 --target=aarch64 --with-platform=efi
And I have the following output at the end of ./configure :
Code: Select all
GRUB2 will be compiled with following components:
Platform: arm64-efi
With devmapper support: No (need libdevmapper header)
With memory debugging: No
With disk cache statistics: No
With boot time statistics: No
efiemu runtime: No (not available on efi)
grub-mkfont: No (need freetype2 library)
grub-mount: No (need FUSE library)
starfield theme: No (No build-time grub-mkfont)
With libzfs support: No (need zfs library)
Build-time grub-mkfont: No (need freetype2 library)
Without unifont (no build-time grub-mkfont)
Without liblzma (no support for XZ-compressed mips images) (need lzma library)


I guess that I should find an ARM64 computer that is already working on Linux to get things better (haha, and I'm stuck at grub). Because here, I'm trying to build something that would execute on a x86_64 computer, in order to create a bootaa64.efi file for ARM64, and clearly the grub guys who worked on this part are delivering something that is not working, despite the fact that ./configure options are proposing it.

I have to go on scaleway few hours and I keep you informed !
jrobin28260
 
Posts: 42
Joined: 2018-11-09 20:30

Re: How to rebuild GRUB (efi) with code / target modificatio

Postby jrobin28260 » 2018-12-27 14:16

Going to scaleway with an ARM64 machine on Debian Stretch, helped me to have ./configure, make -j4 and make install command working fine.

I had to make an image (dd if=/dev/sdi of=imagefile.img) of an USB Key with GPT partitionning and an EFI System Partition on it, in order to mount it on my Arm64 machine with

Code: Select all
losetup /dev/loop0 /home/debian/imagefile.img --partscan
mount /dev/loop0p1 /media/EFI


Then I played ./grub-install /home/debian/anything.img --target=arm64-efi --efi-directory=/media/EFI/ --removable
And I have manufactured my own EFI file ! (What a mess, but it's done).

The bootaa64.efi file still doesn't work (it freezes on my Arm64 Laptop like the others linux ones), but now I will do some test and comparison about build options (compared to uefi-simple which is working fine on my Arm64 laptop) and grub options too.
jrobin28260
 
Posts: 42
Joined: 2018-11-09 20:30

Re: How to rebuild GRUB (efi) with code / target modificatio

Postby jrobin28260 » 2018-12-27 14:50

Head_on_a_Stick wrote:[...]
You could try systemd-boot, that provides /usr/lib/systemd/boot/efi/systemd-bootaa64.efi
If that is copied to ${ESP}/efi/boot/bootaa64.efi then the UEFI firmware should boot it automatically.[1]
[...]


By having the access to a working ARM64 machine I've found this file in its aa64 version, so I opened a parenthesis to my grub plans, and tried it !
I first tried the x64 version on a x64 computer in order to see what happens when this file is placed at the right place on a correctly formated USB key (GPT, ESP, Fat32, /efi/boot/bootx64.efi) and something appears on the screen.

Unfortunately, it doesn't work with the ARM64 computer and the systemd-bootaa64.efi file, same problem that grub : the computer freezes when trying to play the bootaa64.efi file (systemd-bootaa64.efi renamed and placed to /efi/boot/bootaa64.efi in an FAT32 formatted EFi System Partition)

It's interesting, because it tends to indicates that the target/build options used both for grub and systemd aren't the right ones for this computer, and probably are different compared to the "uefi-simple" hello world program.

For one of the numerous grub files, I isolated the following options passed to gcc :
Code: Select all
-Os
-freg-struct-return
-mgeneral-regs-only
-fno-dwarf2-cfi-asm
-fno-asynchronous-unwind-tables
-fno-unwind-tables
-Qn
-fno-PIE
-fno-pie
-fno-stack-protector
-mcmodel=large 
-ffreestanding

I'll now compare it to uefi-simple options for arm64 too

EDIT : here is the options passed to GCC while building "uefi-simple" in a way that works with my arm64 laptop :

aarch64-linux-gnu-gcc
Different :
Code: Select all
-g
-O2
-fpic 
-fshort-wchar
-fno-strict-aliasing
-fno-stack-check
-fno-merge-all-constants

Common :
Code: Select all
-ffreestanding
-fno-stack-protector


I'll first try to understand the differences. The simplest way to test is to rebuild "uefi simple" by trying the grub options to see which one breaks everything :lol:
My second fear is that the problematic option is completely incompatible with the CPU and used in every single Debian package. That would be very hard to solve.
jrobin28260
 
Posts: 42
Joined: 2018-11-09 20:30

Re: How to rebuild GRUB (efi) with code / target modificatio

Postby jrobin28260 » 2018-12-27 17:20

Well, the options -fno-PIE and -fno-pie from grub compilation, when applied to uefi-simple, are freezing the computer.

All the others options from grub (-Os -freg-struct-return -fno-dwarf2-cfi-asm -fno-asynchronous-unwind-tables -fno-unwind-tables -Qn) are working fine when used to create a working uefi-simple bootaa64.efi

I guess I found the guilty !

But I didn't tried the -mgeneral-regs-only and -mcmodel=large options, in fact I tried but I got build error because the first one is incompatible with floats while the second one is incompatible with "-fpic" option used in uefi-simple.

Also, I will now try to remove uefi-simple options that are not present in grub in order to see if some options from uefi-simple are mandatory for having something working.

-------------------------------------------------------------------------------------------

EDIT
I tested by removing from uefi-simple every flag that wasn't in the grub flags, and keeping the working grub flags into uefi-simple, in order to do a test that would match the same conditions.

What results it that the "-fpic" flag is the most important here. If deleted, it's still active by defaut, but if there is the -fno-pic flag, it freezes again at execution. Same problem that with the "-fno-pie" flag.

There is 2 others flags that I didn't successfully removed and tested because of build errors :
-fshort-wchar : gnu-efi build error (in console.c)
-fno-strict-aliasing : gnu-efi build error (in dpath.c)

Let's destroy the -fno-pie and -fno-PIE flags from grub and hope it will be working !
jrobin28260
 
Posts: 42
Joined: 2018-11-09 20:30

Re: How to rebuild GRUB (efi) with code / target modificatio

Postby stevepusser » 2018-12-27 20:18

At the cost of introducing even more stuff to learn, you could try rebuilding the Debian packages from the Debianized source code: https://packages.debian.org/buster/grub-common

on the right side. Debian has any changes to the original source code in the /debian/patches folder, and compiler flags outside the normal ones automatically passed by debhelper can be added in the /debian/rules file. You can use the basic procedure for backporting to get the sources and rebuild the package, too, though you should increment the changelog so the Debian version doesn't just upgrade over yours. It's also pretty easy to turn any change that you make to the package's original source code into a patch with "dpkg-source --commit".

https://wiki.debian.org/SimpleBackportCreation
The MX Linux repositories: Backports galore! If we don't have something, just ask and we'll try--we like challenges. New packages: Foliate 1.5.3, Minitube 3.1, wine-staging 4.14, Virtual Box 5.2.32 & 6.0.10, Pale Moon 28.6.1, Waterfox 56.2.13
User avatar
stevepusser
 
Posts: 11037
Joined: 2009-10-06 05:53

Re: How to rebuild GRUB (efi) with code / target modificatio

Postby jrobin28260 » 2018-12-28 15:27

Hi ! Thank you stevepusser for your answer

As today I'm following all my test/changes from the original with a local SVN, and because my final goal is to rebuild a working grub for my Debian installer (then for my Debian installed :lol:) I'll try to see what can be submitted for everybody else but I guess I got to be very sure of my modifications, and understand everything that it implies.

Yesterday was a rude day of raw editing of too much files and having errors everywhere during the build process with nothing to compare with and lack of knowledge I started today with better approach !
Grub Makefiles and Makefiles generator files are pretty hard to deal with and by searching term in all the files, editing them in almost 50 places and having a summary of what has changed will help me in my quest.
I also kept the stdout of an original build in order to compare with mines.

For now, I'm stuck with something that some of you will find pretty simple, and I will try to solve it in the evening.
There is a build error while I'm building grub with my changes.

Simplified working version (original grub parameters)
Code: Select all
gcc -DHAVE_CONFIG_H -I. -I..  -Wall -W  -DGRUB_MACHINE_EFI=1 -DGRUB_MACHINE=ARM64_EFI -nostdinc -isystem /usr/lib/gcc/aarch64-linux-gnu/6/include -I../include -I../include -DGRUB_FILE=\"lib/disk.c\" -I. -I. -I.. -I.. -I../include -I../include -I../grub-core/lib/libgcrypt-grub/src/    -D_FILE_OFFSET_BITS=64 -Wmissing-declarations -Wformat=2 -freg-struct-return -mgeneral-regs-only -fno-dwarf2-cfi-asm -fno-asynchronous-unwind-tables -fno-unwind-tables -Qn -fno-PIE -fno-pie -fno-stack-protector -Wtrampolines -Werror -mcmodel=large  -ffreestanding   -MT lib/disk_module-disk.o -MD -MP -MF lib/.deps-core/disk_module-disk.Tpo -c -o lib/disk_module-disk.o `test -f 'lib/disk.c' || echo './'`lib/disk.c

mv -f lib/.deps-core/disk_module-disk.Tpo lib/.deps-core/disk_module-disk.Po

gcc -Wmissing-declarations -Wformat=2 -freg-struct-return -mgeneral-regs-only -fno-dwarf2-cfi-asm -fno-asynchronous-unwind-tables -fno-unwind-tables -Qn -fno-PIE -fno-pie -fno-stack-protector -Wtrampolines -Werror   -ffreestanding   -Wl,--build-id=none  -nostdlib -Wl,-N -Wl,-r,-d -o disk.module lib/disk_module-disk.o


Simplified not working version (with my changes, which are addition of -fpic, and removing of -fno-PIE -fno-pie and --mcmodel=large) (and also a -Wl,-r removed parameter which was incompatible with my changes about pic and pie)
Code: Select all
gcc -DHAVE_CONFIG_H -I. -I..  -Wall -W  -DGRUB_MACHINE_EFI=1 -DGRUB_MACHINE=ARM64_EFI -nostdinc -isystem /usr/lib/gcc/aarch64-linux-gnu/6/include -I../include -I../include -DGRUB_FILE=\"lib/disk.c\" -I. -I. -I.. -I.. -I../include -I../include -I../grub-core/lib/libgcrypt-grub/src/    -D_FILE_OFFSET_BITS=64 -Wmissing-declarations -Wformat=2 -freg-struct-return -mgeneral-regs-only -fno-dwarf2-cfi-asm -fno-asynchronous-unwind-tables -fno-unwind-tables -Qn -fpic             -fno-stack-protector -Wtrampolines -Werror                 -ffreestanding   -MT lib/disk_module-disk.o -MD -MP -MF lib/.deps-core/disk_module-disk.Tpo -c -o lib/disk_module-disk.o `test -f 'lib/disk.c' || echo './'`lib/disk.c

mv -f lib/.deps-core/disk_module-disk.Tpo lib/.deps-core/disk_module-disk.Po

gcc -Wmissing-declarations -Wformat=2 -freg-struct-return -mgeneral-regs-only -fno-dwarf2-cfi-asm -fno-asynchronous-unwind-tables -fno-unwind-tables -Qn -fpic             -fno-stack-protector -Wtrampolines -Werror -fpic  -ffreestanding   -Wl,--build-id=none  -nostdlib -Wl,-N -Wl,-d    -o disk.module lib/disk_module-disk.o

/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 00000000004004e0

lib/disk_module-disk.o: In function `grub_disk_cache_invalidate':
/home/debian/grub-2.02/grub-core/lib/disk.c:42: undefined reference to `grub_disk_cache_table'
/home/debian/grub-2.02/grub-core/lib/disk.c:48: undefined reference to `grub_free'

[...]


If someone obviously sees what I should do, you're welcome !
jrobin28260
 
Posts: 42
Joined: 2018-11-09 20:30

Re: How to rebuild GRUB (efi) with code / target modificatio

Postby jrobin28260 » 2018-12-28 22:47

It seems that they are compiling one of the files (disk.c) in two steps.

So I created a small file with 2 functions in it (sub_and_add_a_and_b.c)
Code: Select all
int add_a_b(int a, int b);
int sub_a_b(int a, int b);

int add_a_b(int a, int b)
{
    return a+b;
}

int sub_a_b(int a, int b)
{
    return a-b;
}

I also created a main.c file (in order to play with static and shared library builds) but it's not used here.

If I do many simplifications of their gcc line, with original values, it's doing :
gcc -fno-PIE -fno-pie -mcmodel=large -MT prog.o -MD -MP -MF prog.Tpo -c -o prog.o sub_and_add_a_and_b.c
gcc -fno-PIE -fno-pie -mcmodel=large -Wl,--build-id=none -nostdlib -no-pie -Wl,-N -Wl,-r,-d -o prog.module prog.o

With those parameters and will ALL the parameters that grub uses it gives the same : no error no warning.

But in order to disable -fno-PIE and -fno-pie, because it doesn't work with the Qualcomm computer, I'm trying to do :
gcc -fpic -MT prog.o -MD -MP -MF prog.Tpo -c -o prog.o sub_and_add_a_and_b.c
gcc -fpic -Wl,--build-id=none -nostdlib -no-pie -Wl,-N -Wl,-d -o prog.module prog.o

Then I get one of the errors : /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 00000000004000b0
But I haven't the others error. Anyway I think this warning is enough to say something is wrong in this case too.
If I put ALL the parameters I have the same result.

But there is also a -no-pie option that I removed, an subsequently, a -Wl,-r option that I had to remove because "-pie" is considered active.
However, it seems that it's not incompatible with -fpic to do :
gcc -fpic -MT prog.o -MD -MP -MF prog.Tpo -c -o prog.o sub_and_add_a_and_b.c
gcc -fpic -Wl,--build-id=none -nostdlib -no-pie -Wl,-N -Wl,-r,-d -o prog.module prog.o

Then I get no error and no warning about entry symbol not found.
The "-Wl,-r" option is the key, here. according to https://www.computerhope.com/unix/uld.htm it means "--relocatable" : "generate an output file that can in turn serve as input to ld."
So now I got to see if this could work into the Qualcomm computer... having -fpic, not having -fno-pie or -fno-PIE, but having -no-pie. :|

But I'm not sure of what this is doing ! Why do they pass prog.o to GCC in order to do a prog.module, instead of using the prog.o directly ? The second line, played with the "-shared" parameter, is giving a shared object file, but here, it's like it's doing nothing clear... and I'm trying to play with option about something that is doing something I don't understand :?

---------------------------------
EDIT

By watching the output of the original configuration, disk.module is used 2 times in the following steps :
Code: Select all
for m in disk.module trig.module regexp.module [....and lot of others module...]; do \
sh gensyminfo.sh $m >> syminfo.lst.new || exit 1; \


Then,
Code: Select all
TARGET_OBJ2ELF= sh genmod.sh moddep.lst disk.module build-grub-module-verifier disk.mod


Those *.mod files are then placed at 2 places :
/boot/grub/arm64-efi/disk.mod
/usr/lib/grub/arm64-efi/disk.mod

It means those modules are placed by grub-install, likely used by grub at it's execution, I guess, or used when manufacturing the bootaa64.efi file, but I'm not sure.
---------------------------------
EDIT 2
It seems that the following line :
Code: Select all
TARGET_OBJ2ELF= sh genmod.sh moddep.lst disk.module build-grub-module-verifier disk.mod

Didn't worked : _GLOBAL_OFFSET_TABLE_ in disk is not defined
The GOT is the thing that the -fpic option ensure to create (-fPIC if the GOT is too big for -fpic on some CPU).

According to https://gcc.gnu.org/onlinedocs/gcc/Code ... en-Options
-fpic gives a GOT that is suitable for libraries, while -fpie gives a GOT that can only be linked against an executable (I guess -fpic is better ? But there is probably some support restrictions)

Anyway, disk.module had -fpic active, but may be not -fpie (which can be played even if -no-pie is present... but I'm not sure of the results !)
Code: Select all
gcc -DHAVE_CONFIG_H -I. -I..  -Wall -W  -DGRUB_MACHINE_EFI=1 -DGRUB_MACHINE=ARM64_EFI -nostdinc -isystem /usr/lib/gcc/aarch64-linux-gnu/6/include -I../include -I../include -DGRUB_FILE=\"lib/disk.c\" -I. -I. -I.. -I.. -I../include -I../include -I../grub-core/lib/libgcrypt-grub/src/    -D_FILE_OFFSET_BITS=64 -Os -Wall -W -Wshadow -Wpointer-arith -Wundef -Wchar-subscripts -Wcomment -Wdeprecated-declarations -Wdisabled-optimization -Wdiv-by-zero -Wfloat-equal -Wformat-extra-args -Wformat-security -Wformat-y2k -Wimplicit -Wimplicit-function-declaration -Wimplicit-int -Wmain -Wmissing-braces -Wmissing-format-attribute -Wmultichar -Wparentheses -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wswitch -Wtrigraphs -Wunknown-pragmas -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value  -Wunused-variable -Wwrite-strings -Wnested-externs -Wstrict-prototypes -g -Wredundant-decls -Wmissing-prototypes -Wmissing-declarations -Wcast-align  -Wextra -Wattributes -Wendif-labels -Winit-self -Wint-to-pointer-cast -Winvalid-pch -Wmissing-field-initializers -Wnonnull -Woverflow -Wvla -Wpointer-to-int-cast -Wstrict-aliasing -Wvariadic-macros -Wvolatile-register-var -Wpointer-sign -Wmissing-include-dirs -Wmissing-prototypes -Wmissing-declarations -Wformat=2 -freg-struct-return -mgeneral-regs-only -fno-dwarf2-cfi-asm -fno-asynchronous-unwind-tables -fno-unwind-tables -Qn -fpic -fpic -fno-stack-protector -Wtrampolines -Werror -fpic  -ffreestanding   -MT lib/disk_module-disk.o -MD -MP -MF lib/.deps-core/disk_module-disk.Tpo -c -o lib/disk_module-disk.o `test -f 'lib/disk.c' || echo './'`lib/disk.c
mv -f lib/.deps-core/disk_module-disk.Tpo lib/.deps-core/disk_module-disk.Po
gcc -Os -Wall -W -Wshadow -Wpointer-arith -Wundef -Wchar-subscripts -Wcomment -Wdeprecated-declarations -Wdisabled-optimization -Wdiv-by-zero -Wfloat-equal -Wformat-extra-args -Wformat-security -Wformat-y2k -Wimplicit -Wimplicit-function-declaration -Wimplicit-int -Wmain -Wmissing-braces -Wmissing-format-attribute -Wmultichar -Wparentheses -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wswitch -Wtrigraphs -Wunknown-pragmas -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value  -Wunused-variable -Wwrite-strings -Wnested-externs -Wstrict-prototypes -g -Wredundant-decls -Wmissing-prototypes -Wmissing-declarations -Wcast-align  -Wextra -Wattributes -Wendif-labels -Winit-self -Wint-to-pointer-cast -Winvalid-pch -Wmissing-field-initializers -Wnonnull -Woverflow -Wvla -Wpointer-to-int-cast -Wstrict-aliasing -Wvariadic-macros -Wvolatile-register-var -Wpointer-sign -Wmissing-include-dirs -Wmissing-prototypes -Wmissing-declarations -Wformat=2 -freg-struct-return -mgeneral-regs-only -fno-dwarf2-cfi-asm -fno-asynchronous-unwind-tables -fno-unwind-tables -Qn -fpic -fpic -fno-stack-protector -Wtrampolines -Werror -fpic  -ffreestanding   -no-pie -Wl,--build-id=none  -nostdlib -Wl,-N -Wl,-r,-d   -o disk.module lib/disk_module-disk.o

Let's try with -fpie.

---------------------------------
EDIT 3
Tried with -fpic -fpie into all of the CFLAGS and LDFLAGS untouched, same error at the end of the end of the building process : _GLOBAL_OFFSET_TABLE_ in disk is not defined
By reading my changes on the SVN, I'm able to see that there is CFLAGS/LDFLAGS for kernel, module, target, platform...
If the thing that the computer executes when running bootaa64.efi is not the modules, I guess I can be more restrictive when I'm applying (or not) my CFLAGS "-fpic" and or "-fpie".

Anyway, on UEFI-SIMPLE the -fpic -fpie as CFLAGS and -no-pie as LDFLAGS work and gives something that the computer is able to start.
To be continued tomorrow !
jrobin28260
 
Posts: 42
Joined: 2018-11-09 20:30

Re: How to rebuild GRUB (efi) with code / target modificatio

Postby llivv » 2018-12-29 03:39

jrobin28260 wrote:By having the access to a working ARM64 machine I've found this file in its aa64 version, so I opened a parenthesis to my grub plans, and tried it !
I first tried the x64 version on a x64 computer in order to see what happens when this file is placed at the right place on a correctly formated USB key (GPT, ESP, Fat32, /efi/boot/bootx64.efi) and something appears on the screen.

Unfortunately, it doesn't work with the ARM64 computer and the systemd-bootaa64.efi file, same problem that grub : the computer freezes when trying to play the bootaa64.efi file (systemd-bootaa64.efi renamed and placed to /efi/boot/bootaa64.efi in an FAT32 formatted EFi System Partition)

It's interesting, because it tends to indicates that the target/build options used both for grub and systemd aren't the right ones for this computer, and probably are different compared to the "uefi-simple" hello world program.

I'll first try to understand the differences. The simplest way to test is to rebuild "uefi simple" by trying the grub options to see which one breaks everything :lol:
My second fear is that the problematic option is completely incompatible with the CPU and used in every single Debian package. That would be very hard to solve.

Before I close out all the tabs I have open
From what I see so far it looks like what you are trying to do is - essentially hack the Windows 10 (S) bootloader.
So far, Only 4 or 5 of the biggest wintel OEM's have access to the special win 10 S boot process. (from what I have read so far).
[...]
If someone already did what I'm doing here, your suggestions are welcome !
I'll try to take a look at the error tomorrow after a good night, probably directly at the code, in order to understand what gcc is talking about.
ARM64 link below
And I'll try to build the bootx64.efi file for test from a standard x86_64 computer running on efi !
There are a lot of others trying to figure this out too. From what I've read so far some are trying to access the windows 10 s boot stack from the ((OPE) - ( RE)) ((oem preinstallation envir) - (recovery envir?)) and if I know how ($$) works - it is probably in a boot (S) release of the OPK - OEM Preinstallation Kit (probably more restrictive )...
see links below - I think mostly ddg.gg results - AnandTech - HP envy
[...]
I guess that I should find an ARM64 computer that is already working on Linux to get things better (haha, and I'm stuck at grub). Because here, I'm trying to build something that would execute on a x86_64 computer, in order to create a bootaa64.efi file for ARM64, and clearly the grub guys who worked on this part are delivering something that is not working, despite the fact that ./configure options are proposing it.
That was my thought as well, but it looks to me like the same issue Debian had with EFI and secure boot when windows first started pushing the secure boot key in EFI - maybe looking up Secure Boot could offer some possible results.
Some comments I've read indicate the evil empire is at it again with releasing FUD concerning win s boot process.... you should see those comments in some of the ddg.gg search results linked below.

If you've ever loaded debian onto a chromebook you might have other ideas about how the bootloader on your Asus is locked down... but only relativly casue android boots different than win (s)

I haven't noticed anything related in the debian arm mailing list either...

here are a few pages that might give you more ideas
ARM64
https://linux-sunxi.org/Arm64

Qualcomm SnapDragon 835
https://www.qualcomm.com/products/snapdragon-835-mobile-platform

OEM stuff on Anandtech
https://www.anandtech.com/show/11477/qualcomm-announces-first-oems-for-windows-10-on-snapdragon-835

Linus git ARM64
https://github.com/torvalds/linux/tree/master/arch/arm64/boot/dts/allwinner
just to double check that everything you need to boot your Asus is in Buster 4.18 arm64

Busters ARM64 kernel (current)
https://packages.debian.org/search?keywords=linux-image-4.19.0-1-rt-arm64
that's not even that easy to find if you don't use it much.

HP envy support for their phonebooktab
https://h30434.www3.hp.com/t5/Notebook-Operating-System-and-Recovery/installing-linux-on-envy-x2/td-p/6644724

boot linux on arm64 snapdragon 835 - ddg.gg results
https://duckduckgo.com/?q=boot+linux+on+arm64+snapdragon+835&ia=web

that about does it for me today :wink:
In memory of Ian Ashley Murdock (1973 - 2015) founder of the Debian project.
User avatar
llivv
 
Posts: 5488
Joined: 2007-02-14 18:10
Location: cold storage

Next

Return to System configuration

Who is online

Users browsing this forum: No registered users and 19 guests

fashionable