Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PATCHv3 0/6] arm64: zboot support #4

Closed
wants to merge 6 commits into from
Closed

[PATCHv3 0/6] arm64: zboot support #4

wants to merge 6 commits into from

Conversation

pfliu
Copy link

@pfliu pfliu commented Jun 1, 2023

As more complicated capsule kernel format occurs like zboot, where the
compressed kernel is stored as a payload. The straight forward
decompression can not meet the demand.

As the first step, on aarch64, reading in the kernel file in a probe
method and decide how to unfold the content by the method itself.

The new designed probe interface returns two factors:

  1. the parsed kernel_buf should be returned so that it can be used by
    the image load method later.
  2. the final fd passed to sys_kexec_file_load, since aarch64 kernel can
    only work with Image format, the outer payload should be stripped and a
    temporary file of Image should be created.
  • Things left to do on arches besides aarch64 *
    After substitution, although each probe mehod seems to have the same
    prototype, they have differenct purpose on the first argument.
    On aarch64, the argument is used to pass the kernel file name, while on
    the other arches, it is used to passs the decompressed kernel.

This can be cured by scattering out the logic of reading the kernel file
into each probe. (This should be done by coccinelle. But let us keep a
small step for the time being)

v2 -> v3:
Fix some missing replacement by Coccinelle in [1/6], due to UNUSED() macro

v1 -> v2:
take in Jeremy's patches to implement zboot format support
use coccinelle and sed to substitue the image probe method prototype.

Pingfan Liu and others added 6 commits June 1, 2023 09:29
As more complicated kernel format occurs such as zboot, where the
compressed kernel is stored as a payload. The straight forward
decompression can not meet the demand.

A new image probe method is expected to read in the kernel file and decide
how to unfold the content by itself.

This patch aims to change the image probe's prototype from
            typedef int (probe_t)(const char *kernel_buf, off_t kernel_size);
to
            typedef int (probe_t)(const char *kernel_buf, off_t kernel_size, struct kexec_info *info);

Later, info can be used to return both the file descriptor and the
parsed kernel buffer.

In case your are curious, the remaing part of the log describes the
process of the substitution of the new prototype, which can be divided
into three groups.

1. change function pointer and its callsites:
  sed -i 's/(probe_t)(const char \*kernel_buf, off_t kernel_size);/(probe_t)(const char \*kernel, off_t kernel_size, struct kexec_info \*info);/g' kexec/kexec.h
  sed -i 's/\.probe(\([^,]*\), \([^)]*\))/\.probe(\1, \2, NULL)/g' kexec/kexec.c

2. change the function declaration and definition of each 'probe'
   instance by coccinelle because they may cross lines

The cocci file looks like:

@ rule1 @
identifier fn =~ "_probe";
identifier buf, size;
typedef off_t;
@@

-int fn(const char *buf, off_t size)
+int fn(const char *buf, off_t size, struct kexec_info *info)
{
        ...
}

@ rule2 @
identifier fn =~ "_probe";
identifier buf, size;
typedef off_t;
@@

+int fn(const char *buf, off_t size, struct kexec_info *info);
-int fn(const char *buf, off_t size);

Then running the command
spatch --sp-file cocci/define.cocci --dir kexec --include-headers > ../define.patch
git apply --directory=kexec ../define.patch

3. change the direct calls to the probe instances

Originally I planned to achieve this by coccinelle, but failed similar
to [1]. I have tried using "-I and --include" option for coccinelle, but it
still did not work.

Checking the direct callsite by "git grep "_probe(" | grep -v const"
Fortunatelly, it turns out that only a few direct callsites exist, which
lies in i386, and easy to be amended manually.

Anyway, just FYI, the cocci file looks like:
@ rule1 @
identifier fn =~ "_probe";
identifier buf, size;
identifier info;
typedef off_t;
@@

int fn(const char *buf, off_t size, struct kexec_info *info);

/* change the direct callsite of any probe */
@ rule2 @
identifier rule1.fn;
expression E1, E2;
@@

 fn(E1, E2
+  ,NULL
   )

Then running the command:
spatch --sp-file cocci/direct.cocci --dir kexec --include-headers

[1]: https://lore.kernel.org/all/alpine.DEB.2.22.394.2202280705080.3112@hadrien/T/

Signed-off-by: Pingfan Liu <piliu@redhat.com>
To: kexec@lists.infradead.org
Cc: horms@verge.net.au
Cc: ardb@kernel.org
Cc: jeremy.linton@arm.com
Current compilers note that fname will be null while
attempting to print failures from strdup().

Further fix a memory leak caused by kernel_uncompressed_buf
never being used/freed before the allocated block is replaced
by the one returned by slurp_decompress_file().

Signed-off-by: Jeremy Linton <jeremy.linton@arm.com>
Signed-off-by: Pingfan Liu <piliu@redhat.com>
To: kexec@lists.infradead.org
Cc: horms@verge.net.au
Cc: ardb@kernel.org
Cc: jeremy.linton@arm.com
As more complicated capsule kernel format ,such as zboot, emerge, where
the compressed kernel is stored as a payload. The straight forward
decompression can not meet the demand.

Therefore, let a probe method read in the kernel file and decide how to
unfold the content by the method itself.

Since other arches still read the kernel image before probe method,
the purpose of the first argument differs from that in aarch64.

Signed-off-by: Pingfan Liu <piliu@redhat.com>
To: kexec@lists.infradead.org
Cc: horms@verge.net.au
Cc: ardb@kernel.org
Cc: jeremy.linton@arm.com
The linux kernel CONFIG_ZBOOT option creates
self decompressing PE kernel images. So this means
that kexec should have a generic understanding of
the format which may be used by multiple arches.

So lets add an arch independent validation
and decompression routine.

Signed-off-by: Jeremy Linton <jeremy.linton@arm.com>
[Modified by Pingfan to adapt the new probe interface]
Signed-off-by: Pingfan Liu <piliu@redhat.com>
To: kexec@lists.infradead.org
Cc: horms@verge.net.au
Cc: ardb@kernel.org
Cc: jeremy.linton@arm.com
The kernel EFI stub ZBOOT feature creates a PE that
contains a compressed linux kernel image. The stub
when run in a valid UEFI environment then decompresses
the resulting image and executes it.

Support these image formats with kexec as well to avoid
having to keep an alternate kernel image around.

This patch adds a the _probe() and usage() routines needed
for kexec to understand this format.

Signed-off-by: Jeremy Linton <jeremy.linton@arm.com>
[Modified by Pingfan to adapt the new probe interface]
Signed-off-by: Pingfan Liu <piliu@redhat.com>
To: kexec@lists.infradead.org
Cc: horms@verge.net.au
Cc: ardb@kernel.org
Cc: jeremy.linton@arm.com
Add the previously defined _probe() and _usage() routines
to the kexec file types table, and build the new module.

It should be noted that this "vmlinuz" support reuses the
"Image" support to actually load the resulting image after
it has been decompressed to a temporary file.

Signed-off-by: Jeremy Linton <jeremy.linton@arm.com>
Signed-off-by: Pingfan Liu <piliu@redhat.com>
To: kexec@lists.infradead.org
Cc: horms@verge.net.au
Cc: ardb@kernel.org
Cc: jeremy.linton@arm.com
@pfliu
Copy link
Author

pfliu commented Jul 7, 2023

I have sent out V4 in mailing list, so close this v3 pull request

@pfliu pfliu closed this Jul 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants