Add zstdio support for booting Trixie kernels in Xen
cover Add zstdio support for booting Trixie kernels in Xen
This is v2 of the patch cleaning up a lot of style issues noted by Avnish. I think I've got all the style issues that were requested fixed up.
Thanks,
Logan
--
Changes in v2:
- Fixed a bunch of style issues noted by Avnish.
Cover letter from v1:
Debian has started compressing the kernel images in the Trixie release. This breaks booting xen kernels seeing grub has no support to decompress the zstd images[1][2].
To improve this situation, this patch series adds zstdio support in a way that's very similar to xzio. The first patch adds this support. The remaining patchs expand the file_filter tests so this functionality is covered.
--
[1] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1102066
[2] https://www.mail-archive.com/[email protected]/msg41663.html
Logan Gunthorpe (4):
io: Implement zstdio decompression
tests/file_filter: Regenerate gpg keys
tests/file_filter: Add zstd test file
tests/file_filter: Add zstd tests
Makefile.util.def | 1 + grub-core/Makefile.core.def | 7 + grub-core/io/zstdio.c | 237 ++++++++++++++++++++++++++++++++ include/grub/file.h | 3 +- tests/file_filter/file.gz.sig | Bin 96 -> 310 bytes tests/file_filter/file.lzop.sig | Bin 96 -> 310 bytes tests/file_filter/file.xz.sig | Bin 96 -> 310 bytes tests/file_filter/file.zstd | Bin 0 -> 26 bytes tests/file_filter/file.zstd.sig | Bin 0 -> 310 bytes tests/file_filter/keys | Bin 994 -> 1308 bytes tests/file_filter/keys.pub | Bin 990 -> 657 bytes tests/file_filter/test.cfg | 1 + tests/file_filter_test.in | 7 +- 13 files changed, 253 insertions(+), 3 deletions(-) create mode 100644 grub-core/io/zstdio.c create mode 100644 tests/file_filter/file.zstd create mode 100644 tests/file_filter/file.zstd.sig
base-commit: 6b5c671d35b1b84468da1fef2b9bd8e05682f7e6 --
2.47.3
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Sorry, I forgot to add the v2 flag in the PATCH subject. Hopefully it's not too confusing.
Logan
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH 1/4 io: Implement zstdio decompression
In reply to Logan Gunthorpe via Grub-develAdd zstd based io decompression.
Based largely on the existing xzio, implement the same features using the zstd library already included in the project.
--- Makefile.util.def | 1 + grub-core/Makefile.core.def | 7 ++ grub-core/io/zstdio.c | 237 ++++++++++++++++++++++++++++++++++++ include/grub/file.h | 3 +- 4 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 grub-core/io/zstdio.c
Makefile.util.def +1 -0
@@ -162,6 +162,7 @@ library = { common = grub-core/io/gzio.c; common = grub-core/io/xzio.c; common = grub-core/io/lzopio.c; + common = grub-core/io/zstdio.c; common = grub-core/kern/ia64/dl_helper.c; common = grub-core/kern/arm/dl_helper.c; common = grub-core/kern/arm64/dl_helper.c;
grub-core/Makefile.core.def +7 -0
@@ -2512,6 +2512,13 @@ module = { cppflags = '-I$(srcdir)/lib/posix_wrap -I$(srcdir)/lib/minilzo -DMINILZO_HAVE_CONFIG_H'; }; +module = { + name = zstdio; + common = io/zstdio.c; + cppflags = '-I$(srcdir)/lib/posix_wrap -I$(srcdir)/lib/zstd'; + cflags='-Wno-unreachable-code'; +}; + module = { name = testload; common = commands/testload.c;
grub-core/io/zstdio.c new file +237 -0
237 lines not on this page · raw message
include/grub/file.h +2 -1
@@ -190,9 +190,10 @@ typedef enum grub_file_filter_idGRUB_FILE_FILTER_GZIO ,GRUB_FILE_FILTER_XZIO ,GRUB_FILE_FILTER_LZOPIO , +GRUB_FILE_FILTER_ZSTDIO ,GRUB_FILE_FILTER_MAX ,GRUB_FILE_FILTER_COMPRESSION_FIRST =GRUB_FILE_FILTER_GZIO , -GRUB_FILE_FILTER_COMPRESSION_LAST =GRUB_FILE_FILTER_LZOPIO , +GRUB_FILE_FILTER_COMPRESSION_LAST =GRUB_FILE_FILTER_ZSTDIO , } grub_file_filter_id_t;typedef grub_file_t (*grub_file_filter_t ) (grub_file_t in,enum grub_file_type type);
Signature · 7 lines
2.47.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH 1/4] io: Implement zstdio decompression
Quoted 50 lines · Logan Gunthorpe via Grub-devel
Add zstd based io decompression.
Based largely on the existing xzio, implement the same features using the zstd library already included in the project.
Signed-off-by Logan Gunthorpe <[email protected]>--- Makefile.util.def | 1 + grub-core/Makefile.core.def | 7 ++ grub-core/io/zstdio.c | 237 ++++++++++++++++++++++++++++++++++++ include/grub/file.h | 3 +- 4 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 grub-core/io/zstdio.cMakefile.util.def +1
-0@@ -162,6 +162,7 @@ library = { common = grub-core/io/gzio.c; common = grub-core/io/xzio.c; common = grub-core/io/lzopio.c; + common = grub-core/io/zstdio.c; common = grub-core/kern/ia64/dl_helper.c; common = grub-core/kern/arm/dl_helper.c; common = grub-core/kern/arm64/dl_helper.c;grub-core/Makefile.core.def +7
-0@@ -2512,6 +2512,13 @@ module = { cppflags = '-I$(srcdir)/lib/posix_wrap -I$(srcdir)/lib/minilzo -DMINILZO_HAVE_CONFIG_H'; }; +module = { + name = zstdio; + common = io/zstdio.c; + cppflags = '-I$(srcdir)/lib/posix_wrap -I$(srcdir)/lib/zstd'; + cflags='-Wno-unreachable-code'; +}; + module = { name = testload; common = commands/testload.c;grub-core/io/zstdio.c new file +0
-0
grub-core/io/zstdio.c line 4
Quoted 4 lines
grub-core/io/zstdio.c new file +4
-0@@ -0,0 +1,237 @@ +/* zstdio.c - decompression support for zstd */ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2010 Free Software Foundation, Inc.
s/2010/2025/
Quoted 18 lines
grub-core/io/zstdio.c new file +18
-0+* 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 <grub/err.h> +#include <grub/mm.h> +#include <grub/misc.h> +#include <grub/file.h>
grub-core/io/zstdio.c line 33
Quoted 10 lines
grub-core/io/zstdio.c new file +10
-0+#include <grub/fs.h> +#include <grub/dl.h> + +GRUB_MOD_LICENSE ("GPLv3+" ); + +#include "zstd.h" + +#define STREAM_HEADER_SIZE 16 + +struct grub_zstdio
You do not need grub_ prefix if name is internal to the module.
grub-core/io/zstdio.c line 46
Quoted 13 lines
grub-core/io/zstdio.c new file +13
-0+{ +grub_file_t file; +ZSTD_DCtx *dctx; +grub_size_t insize; +grub_size_t outsize; + +ZSTD_outBuffer output; +ZSTD_inBuffer input; + +grub_off_t saved_offset; +grub_uint8_t bufs[]; +}; +
Please drop this empty line...
grub-core/io/zstdio.c line 47
Quoted 1 line
grub-core/io/zstdio.c new file +1
-0+typedef struct grub_zstdio *grub_zstdio_t ;
... and add one here... :-)
grub-core/io/zstdio.c line 50
Quoted 3 lines
grub-core/io/zstdio.c new file +3
-0+static struct grub_fs grub_zstdio_fs; + +static int
Please use bool, including true/false, instead of int.
Quoted 9 lines
grub-core/io/zstdio.c new file +9
-0+test_header (grub_file_t file) +{ +grub_zstdio_t zstdio = file->data; +size_t zret; + + zstdio->input.pos =0 ; + zstdio->output.pos =0 ; + zstdio->output.size = zstdio->outsize; + zstdio->input.size = grub_file_read (zstdio->file, zstdio->bufs,
grub-core/io/zstdio.c line 69
Quoted 10 lines
grub-core/io/zstdio.c new file +10
-0+ STREAM_HEADER_SIZE); +if (zstdio->input.size !=STREAM_HEADER_SIZE ) +return 0 ; + + zret =ZSTD_decompressStream (zstdio->dctx, &zstdio->output, &zstdio->input); +if (ZSTD_isError (zret)) +return 0 ; + +return 1 ; +}
Missing empty line here...
grub-core/io/zstdio.c line 81
Quoted 12 lines
grub-core/io/zstdio.c new file +12
-0+static grub_file_t +grub_zstdio_open (grub_file_t io,enum grub_file_type type) +{ +grub_file_t file; +grub_zstdio_t zstdio; + +if (type &GRUB_FILE_TYPE_NO_DECOMPRESS ) +return io; + + file = (grub_file_t )grub_zalloc (sizeof (grub_file_t)); +if (file ==NULL ) +return 0 ;
s/0/NULL/
grub-core/io/zstdio.c line 88
Quoted 6 lines
grub-core/io/zstdio.c new file +6
-0+ zstdio =grub_zalloc (sizeof (grub_zstdio_t) +ZSTD_DStreamInSize () + +ZSTD_DStreamOutSize ()); +if (zstdio ==NULL ) + { +grub_free (file); +return 0 ;
Ditto...
In general please use NULL instead of 0.
Quoted 6 lines
grub-core/io/zstdio.c new file +6
-0+ } + + zstdio->file = io; + zstdio->insize =ZSTD_DStreamInSize (); + zstdio->outsize =ZSTD_DStreamOutSize (); + zstdio->input.src = zstdio->bufs;
grub-core/io/zstdio.c line 104
Quoted 10 lines
grub-core/io/zstdio.c new file +10
-0+ zstdio->output.dst = &zstdio->bufs[zstdio->insize]; + + file->device = io->device; + file->data = zstdio; + file->fs = &grub_zstdio_fs; + file->size =GRUB_FILE_SIZE_UNKNOWN ; + file->not_easily_seekable =1 ; + +if (grub_file_tell (zstdio->file) !=0 ) +grub_file_seek (zstdio->file,0 );
grub_file_seek() may fail. Please always check for errors.
grub-core/io/zstdio.c line 111
Quoted 6 lines
grub-core/io/zstdio.c new file +6
-0+ zstdio->dctx =ZSTD_createDCtx (); +if (zstdio->dctx ==NULL ) + { +grub_free (file); +grub_free (zstdio); +return 0 ;
Again, s/0/NULL/...
grub-core/io/zstdio.c line 114
Quoted 3 lines
grub-core/io/zstdio.c new file +3
-0+ } + +if (!test_header (file))
if (test_header (file) == false)
Quoted 21 lines
grub-core/io/zstdio.c new file +21
-0+ { + grub_errno =GRUB_ERR_NONE ; +grub_file_seek (io,0 ); +ZSTD_freeDCtx (zstdio->dctx); +grub_free (zstdio); +grub_free (file); + +return io; + } + +return file; +} + +static grub_ssize_t +grub_zstdio_read (grub_file_t file,char *buf,grub_size_t len) +{ +grub_zstdio_t zstdio = file->data; +grub_ssize_t ret =0 ; +grub_ssize_t readret; +grub_off_t current_offset; +grub_size_t zret;
grub-core/io/zstdio.c line 145
Quoted 10 lines
grub-core/io/zstdio.c new file +10
-0+ +/* If seek backward need to reset decoder and start from beginning of file. */ +if (file->offset < zstdio->saved_offset) + { +ZSTD_initDStream (zstdio->dctx); + zstdio->input.pos =0 ; + zstdio->input.size =0 ; + zstdio->output.pos =0 ; + zstdio->saved_offset =0 ; +grub_file_seek (zstdio->file,0 );
grub_file_seek() may fail...
grub-core/io/zstdio.c line 152
Quoted 7 lines
grub-core/io/zstdio.c new file +7
-0+ } + + current_offset = zstdio->saved_offset; + +while (len >0 ) + { + zstdio->output.size = file->offset + ret + len - current_offset;
Is there any chance for overflow here? If yes then please use safe math from include/grub/safemath.h.
Quoted 13 lines
grub-core/io/zstdio.c new file +13
-0+if (zstdio->output.size > zstdio->outsize) + zstdio->output.size = zstdio->outsize; +if (zstdio->input.pos == zstdio->input.size) + { + readret =grub_file_read (zstdio->file, zstdio->bufs, + zstdio->insize); +if (readret <0 ) +return -1 ; + + zstdio->input.size = readret; + zstdio->input.pos =0 ; + } +
grub-core/io/zstdio.c line 175
Quoted 10 lines
grub-core/io/zstdio.c new file +10
-0+ zret =ZSTD_decompressStream (zstdio->dctx, &zstdio->output, + &zstdio->input); +if (ZSTD_isError (zret)) + { +grub_error (GRUB_ERR_BAD_COMPRESSED_DATA , +N_ ("zstd file corrupted or unsupported block options" )); +return -1 ; + } + +grub_off_t new_offset = current_offset + zstdio->output.pos;
Please do not mix variable definitions with the code. Define all stuff at the beginning of the function or block.
grub-core/io/zstdio.c line 180
Quoted 4 lines
grub-core/io/zstdio.c new file +4
-0+/* Store first chunk of data in buffer. */ +if (file->offset <= new_offset) + { +grub_size_t delta = new_offset - (file->offset + ret);
OK, but missing empty line here.
grub-core/io/zstdio.c line 182
Quoted 2 lines
grub-core/io/zstdio.c new file +2
-0+ grub_memmove (buf, (grub_uint8_t *) zstdio->output.dst + + (zstdio->output.pos - delta),
Safe math? In general please check all math in this patch and use safe math macros where needed.
Quoted 8 lines
grub-core/io/zstdio.c new file +8
-0+ delta); + len -= delta; + buf += delta; + ret += delta; + } + current_offset = new_offset; + + zstdio->output.pos =0 ;
grub-core/io/zstdio.c line 200
Quoted 10 lines
grub-core/io/zstdio.c new file +10
-0+ +if (zstdio->input.pos ==0 && zstdio->output.pos ==0 ) +break ; + } + +if (ret >=0 ) + zstdio->saved_offset = file->offset + ret; + +return ret; +}
Daniel
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH 1/4] io: Implement zstdio decompression
Hi Daniel,
Quoted 7 lines · Daniel Kiper
grub-core/io/zstdio.c line 152
Quoted 3 lines · Logan Gunthorpe via Grub-devel
grub-core/io/zstdio.c new file +3
-0+while (len >0 ) + { + zstdio->output.size = file->offset + ret + len - current_offset;Is there any chance for overflow here? If yes then please use safe math from
include/grub/safemath.h.
My read is that unless we are dealing with files greater than the two offsets are 64bits and unlikely to ever see a file that doesn't fit.
len is going to be the size of a memory buffer and ret is going to be less than or equal to ret. So it doesn't seem like an overflow is possible.
Quoted 6 lines
grub-core/io/zstdio.c line 182
Quoted 3 lines
grub-core/io/zstdio.c new file +3
-0+grub_size_t delta = new_offset - (file->offset + ret); + grub_memmove (buf, (grub_uint8_t *) zstdio->output.dst + + (zstdio->output.pos - delta),Safe math? In general please check all math in this patch and use safe math macros where needed.
output.pos must be less than outsize which is defined by ZSTD_DStreamOutSize () and will be much less than even a 32bit type.
delta is by definition less than pos.
(new_offset - file->offset) must be less than len.
This code is very similar to code in xzio.c and did not need any safe math functions.
Please let me know if you disagree, otherwise I'll send a new version next week.
Thanks,
Logan
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH 1/4] io: Implement zstdio decompression
Quoted 8 lines · Logan Gunthorpe via Grub-devel
Hi Daniel,
Quoted 5 lines · Daniel Kiper
Quoted 3 lines · Logan Gunthorpe via Grub-devel
grub-core/io/zstdio.c new file +3
-0+while (len >0 ) + { + zstdio->output.size = file->offset + ret + len - current_offset;
Quoted 5 lines
Quoted 2 lines
Is there any chance for overflow here? If yes then please use safe math from
include/grub/safemath.h.My read is that unless we are dealing with files greater than the two offsets are 64bits and unlikely to ever see a file that doesn't fit.
First of all, please remember that the GRUB can be compiled for 32-bit targets. So, zstdio->output.size can be 32-bit...
Quoted 2 lines
len is going to be the size of a memory buffer and ret is going to be less than or equal to ret. So it doesn't seem like an overflow is possible.
I think you meant "less than or equal to len"...
Quoted 10 lines
Quoted 6 lines
grub-core/io/zstdio.c line 182
Quoted 3 lines
grub-core/io/zstdio.c new file +3
-0+grub_size_t delta = new_offset - (file->offset + ret); + grub_memmove (buf, (grub_uint8_t *) zstdio->output.dst + + (zstdio->output.pos - delta),Safe math? In general please check all math in this patch and use safe math macros where needed.
output.pos must be less than outsize which is defined by
ZSTD_DStreamOutSize() and will be much less than even a 32bit type.
Quoted 6 lines
delta is by definition less than pos.
(
new_offset-file->offset) must be less than len.This code is very similar to code in
xzio.cand did not need any safe math functions.
IIRC it predates safe math and it may not be (fully) correct..
Quoted 2 lines
Please let me know if you disagree, otherwise I'll send a new version next week.
I am not saying I disagree. I rather have some reservations. However, if you convince me it is not possible or it is very difficult to blow up this code using malformed zstd archives than I am OK with it.
Daniel
PS I am going to cut GRUB 2.14~rc1 in second half of the week. So, it
would be nice to have these patches before that...
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH 1/4] io: Implement zstdio decompression
Quoted 6 lines · Daniel Kiper
Quoted 2 lines · Logan Gunthorpe via Grub-devel
My read is that unless we are dealing with files greater than the two offsets are 64bits and unlikely to ever see a file that doesn't fit.
First of all, please remember that the GRUB can be compiled for 32-bit targets. So,
zstdio->output.sizecan be 32-bit...
Yes, but zstdio->output.size is always less than the relatively small buffer size returned by ZSTD_DStreamOutSize ().
The only values that could conceivably be larger than 32-bits are the offsets within the file which are of type grup_off_t, which is a typedef of grub_uint64_t and, by my read of the code, it looks like it should be 64-bits on all platforms.
Quoted 4 lines
Quoted 2 lines
len is going to be the size of a memory buffer and ret is going to be less than or equal to ret. So it doesn't seem like an overflow is possible.
I think you meant "less than or equal to len"...
Yes, sorry.
Quoted 3 lines
I am not saying I disagree. I rather have some reservations. However, if you convince me it is not possible or it is very difficult to blow up this code using malformed zstd archives than I am OK with it.
I'm just worried the change is a bit tricky to get right needing to convert it to a string of grub_add() calls. And the conversion seems risky especially given the existing code it was copied from has been around for a while without issue (as far as I'm aware).
Quoted 2 lines
PS I am going to cut GRUB 2.14~rc1 in second half of the week. So, it
would be nice to have these patches before that...
Ok, well except for this issue I should be good to send the next version today or, at worst tomorrow.
Thanks,
Logan
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH 2/4 tests/file_filter: Regenerate gpg keys
In reply to Logan Gunthorpe via Grub-develThe 'keys' file is not a valid GPG secret key so it is not possible to generate new signatures.
Create a new key and use gpg --export-secret-key to export the key and gpg --export to export the public key. Then resign all the signatures with gpg --detach-sign.
--- tests/file_filter/file.gz.sig | Bin 96 -> 310 bytes tests/file_filter/file.lzop.sig | Bin 96 -> 310 bytes tests/file_filter/file.xz.sig | Bin 96 -> 310 bytes tests/file_filter/keys | Bin 994 -> 1308 bytes tests/file_filter/keys.pub | Bin 990 -> 657 bytes 5 files changed, 0 insertions(+), 0 deletions(-)
tests/file_filter/file.gz.sig binary +0 -0
tests/file_filter/file.lzop.sig binary +0 -0
tests/file_filter/file.xz.sig binary +0 -0
tests/file_filter/keys binary +0 -0
tests/file_filter/keys.pub binary +0 -0
Signature · 7 lines
2.47.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH 3/4 tests/file_filter: Add zstd test file
In reply to Logan Gunthorpe via Grub-develAdd a file.zstd similar to the other compression methods and generate a gpg signature with gpg --detach-sign
--- tests/file_filter/file.zstd | Bin 0 -> 26 bytes tests/file_filter/file.zstd.sig | Bin 0 -> 310 bytes 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/file_filter/file.zstd create mode 100644 tests/file_filter/file.zstd.sig
tests/file_filter/file.zstd new file binary +0 -0
tests/file_filter/file.zstd.sig new file binary +0 -0
Signature · 7 lines
2.47.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel
PATCH 4/4 tests/file_filter: Add zstd tests
In reply to Logan Gunthorpe via Grub-develTest zstd decompression in the same way that other decompressors are tested.
--- tests/file_filter/test.cfg | 1 + tests/file_filter_test.in | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-)
tests/file_filter/test.cfg +1 -0
@@ -3,4 +3,5 @@ set check_signatures=enforce cat /file.gz cat /file.xz cat /file.lzop +cat /file.zstd set check_signatures=
tests/file_filter_test.in +5 -2
@@ -19,14 +19,15 @@ grubshell=@builddir@/grub-shell . "@builddir@/grub-core/modinfo.sh" -filters="gzio xzio lzopio pgp" +filters="gzio xzio lzopio zstdio pgp" modules="cat mpi" for mod in $(cut -d ' ' -f 2 "@builddir@/grub-core/crypto.lst" | sort -u); do modules="$modules $mod" done -for file in file.gz file.xz file.lzop file.gz.sig file.xz.sig file.lzop.sig keys.pub; do +for file in file.gz file.xz file.lzop file.zstd file.gz.sig file.xz.sig \ + file.lzop.sig file.zstd.sig keys.pub; do files="$files /$file=@srcdir@/tests/file_filter/$file" done
@@ -35,6 +36,8 @@ result="Hello, user! Hello, user! +Hello, user! + Hello, user!" out="$("${grubshell}" --modules="$modules $filters" --files="$files" "@srcdir@/tests/file_filter/test.cfg")"
Signature · 7 lines
2.47.3 _______________________________________________ Grub-devel mailing list [email protected] https://lists.gnu.org/mailman/listinfo/grub-devel