Skip to content

Commit

Permalink
bzip2/lzma: fix built-in initramfs vs CONFIG_RD_GZIP
Browse files Browse the repository at this point in the history
Impact: Resolves build failures in some configurations

Makes it possible to disable CONFIG_RD_GZIP . In that case, the
built-in initramfs will be compressed by whatever compressor is
available (bzip2 or lzma) or left uncompressed if none is available.

It also removes a couple of warnings which occur when no ramdisk
compression at all is chosen.

It also restores the select ZLIB_INFLATE in drivers/block/Kconfig
which somehow came missing. This is needed to activate compilation of
the stuff in zlib_deflate.

Signed-off-by: Alain Knaff <[email protected]>
Signed-off-by: H. Peter Anvin <[email protected]>
  • Loading branch information
Alain Knaff authored and H. Peter Anvin committed Jan 7, 2009
1 parent fb9a4ca commit a26ee60
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 20 deletions.
7 changes: 6 additions & 1 deletion init/initramfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ static int __init write_buffer(char *buf, unsigned len)
return len - count;
}


#if defined CONFIG_RD_GZIP || defined CONFIG_RD_BZIP2 || defined CONFIG_RD_LZMA
static int __init flush_buffer(void *bufv, unsigned len)
{
char *buf = (char *) bufv;
Expand All @@ -412,6 +412,7 @@ static int __init flush_buffer(void *bufv, unsigned len)
}
return origLen;
}
#endif

static unsigned my_inptr; /* index of next byte to be processed in inbuf */

Expand Down Expand Up @@ -449,10 +450,12 @@ static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
continue;
}
this_header = 0;
#ifdef CONFIG_RD_GZIP
if (!gunzip(buf, len, NULL, flush_buffer, NULL,
&my_inptr, error) &&
message == NULL)
goto ok;
#endif

#ifdef CONFIG_RD_BZIP2
message = NULL; /* Zero out message, or else cpio will
Expand All @@ -473,7 +476,9 @@ static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
goto ok;
}
#endif
#if defined CONFIG_RD_GZIP || defined CONFIG_RD_BZIP2 || defined CONFIG_RD_LZMA
ok:
#endif
if (state != Reset)
error("junk in compressed archive");
this_header = saved_offset + my_inptr;
Expand Down
17 changes: 11 additions & 6 deletions scripts/gen_initramfs_list.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Released under the terms of the GNU GPL
#
# Generate a cpio packed initramfs. It uses gen_init_cpio to generate
# the cpio archive, and gzip to pack it.
# the cpio archive, and then compresses it.
# The script may also be used to generate the inputfile used for gen_init_cpio
# This script assumes that gen_init_cpio is located in usr/ directory

Expand All @@ -16,8 +16,8 @@ usage() {
cat << EOF
Usage:
$0 [-o <file>] [-u <uid>] [-g <gid>] {-d | <cpio_source>} ...
-o <file> Create gzipped initramfs file named <file> using
gen_init_cpio and gzip
-o <file> Create compressed initramfs file named <file> using
gen_init_cpio and compressor depending on the extension
-u <uid> User ID to map to user ID 0 (root).
<uid> is only meaningful if <cpio_source> is a
directory. "squash" forces all files to uid 0.
Expand Down Expand Up @@ -225,6 +225,7 @@ cpio_list=
output="/dev/stdout"
output_file=""
is_cpio_compressed=
compr="gzip -9 -f"

arg="$1"
case "$arg" in
Expand All @@ -233,11 +234,15 @@ case "$arg" in
echo "deps_initramfs := \\"
shift
;;
"-o") # generate gzipped cpio image named $1
"-o") # generate compressed cpio image named $1
shift
output_file="$1"
cpio_list="$(mktemp ${TMPDIR:-/tmp}/cpiolist.XXXXXX)"
output=${cpio_list}
echo "$output_file" | grep -q "\.gz$" && compr="gzip -9 -f"
echo "$output_file" | grep -q "\.bz2$" && compr="bzip2 -9 -f"
echo "$output_file" | grep -q "\.lzma$" && compr="lzma -9 -f"
echo "$output_file" | grep -q "\.cpio$" && compr="cat"
shift
;;
esac
Expand Down Expand Up @@ -274,7 +279,7 @@ while [ $# -gt 0 ]; do
esac
done

# If output_file is set we will generate cpio archive and gzip it
# If output_file is set we will generate cpio archive and compress it
# we are carefull to delete tmp files
if [ ! -z ${output_file} ]; then
if [ -z ${cpio_file} ]; then
Expand All @@ -287,7 +292,7 @@ if [ ! -z ${output_file} ]; then
if [ "${is_cpio_compressed}" = "compressed" ]; then
cat ${cpio_tfile} > ${output_file}
else
cat ${cpio_tfile} | gzip -f -9 - > ${output_file}
cat ${cpio_tfile} | ${compr} - > ${output_file}
fi
[ -z ${cpio_file} ] && rm ${cpio_tfile}
fi
Expand Down
42 changes: 30 additions & 12 deletions usr/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,32 @@
klibcdirs:;
PHONY += klibcdirs

# Find out "preferred" ramdisk compressor. Order of preference is
# 1. bzip2 efficient, and likely to be present
# 2. gzip former default
# 3. lzma
# 4. none

# None of the above
suffix_y =

# Lzma, but no gzip nor bzip2
suffix_$(CONFIG_RD_LZMA) = .lzma

# Gzip, but no bzip2
suffix_$(CONFIG_RD_GZIP) = .gz

# Bzip2
suffix_$(CONFIG_RD_BZIP2) = .bz2


# Generate builtin.o based on initramfs_data.o
obj-$(CONFIG_BLK_DEV_INITRD) := initramfs_data.o
obj-$(CONFIG_BLK_DEV_INITRD) := initramfs_data$(suffix_y).o

# initramfs_data.o contains the initramfs_data.cpio.gz image.
# initramfs_data.o contains the compressed initramfs_data.cpio image.
# The image is included using .incbin, a dependency which is not
# tracked automatically.
$(obj)/initramfs_data.o: $(obj)/initramfs_data.cpio.gz FORCE
$(obj)/initramfs_data$(suffix_y).o: $(obj)/initramfs_data.cpio$(suffix_y) FORCE

#####
# Generate the initramfs cpio archive
Expand All @@ -25,28 +43,28 @@ ramfs-args := \
$(if $(CONFIG_INITRAMFS_ROOT_UID), -u $(CONFIG_INITRAMFS_ROOT_UID)) \
$(if $(CONFIG_INITRAMFS_ROOT_GID), -g $(CONFIG_INITRAMFS_ROOT_GID))

# .initramfs_data.cpio.gz.d is used to identify all files included
# .initramfs_data.cpio.d is used to identify all files included
# in initramfs and to detect if any files are added/removed.
# Removed files are identified by directory timestamp being updated
# The dependency list is generated by gen_initramfs.sh -l
ifneq ($(wildcard $(obj)/.initramfs_data.cpio.gz.d),)
include $(obj)/.initramfs_data.cpio.gz.d
ifneq ($(wildcard $(obj)/.initramfs_data.cpio.d),)
include $(obj)/.initramfs_data.cpio.d
endif

quiet_cmd_initfs = GEN $@
cmd_initfs = $(initramfs) -o $@ $(ramfs-args) $(ramfs-input)

targets := initramfs_data.cpio.gz
targets := initramfs_data.cpio.gz initramfs_data.cpio.bz2 initramfs_data.cpio.lzma initramfs_data.cpio
# do not try to update files included in initramfs
$(deps_initramfs): ;

$(deps_initramfs): klibcdirs
# We rebuild initramfs_data.cpio.gz if:
# 1) Any included file is newer then initramfs_data.cpio.gz
# We rebuild initramfs_data.cpio if:
# 1) Any included file is newer then initramfs_data.cpio
# 2) There are changes in which files are included (added or deleted)
# 3) If gen_init_cpio are newer than initramfs_data.cpio.gz
# 3) If gen_init_cpio are newer than initramfs_data.cpio
# 4) arguments to gen_initramfs.sh changes
$(obj)/initramfs_data.cpio.gz: $(obj)/gen_init_cpio $(deps_initramfs) klibcdirs
$(Q)$(initramfs) -l $(ramfs-input) > $(obj)/.initramfs_data.cpio.gz.d
$(obj)/initramfs_data.cpio$(suffix_y): $(obj)/gen_init_cpio $(deps_initramfs) klibcdirs
$(Q)$(initramfs) -l $(ramfs-input) > $(obj)/.initramfs_data.cpio.d
$(call if_changed,initfs)

2 changes: 1 addition & 1 deletion usr/initramfs_data.S
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ SECTIONS
*/

.section .init.ramfs,"a"
.incbin "usr/initramfs_data.cpio.gz"
.incbin "usr/initramfs_data.cpio"

29 changes: 29 additions & 0 deletions usr/initramfs_data.bz2.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
initramfs_data includes the compressed binary that is the
filesystem used for early user space.
Note: Older versions of "as" (prior to binutils 2.11.90.0.23
released on 2001-07-14) dit not support .incbin.
If you are forced to use older binutils than that then the
following trick can be applied to create the resulting binary:
ld -m elf_i386 --format binary --oformat elf32-i386 -r \
-T initramfs_data.scr initramfs_data.cpio.gz -o initramfs_data.o
ld -m elf_i386 -r -o built-in.o initramfs_data.o
initramfs_data.scr looks like this:
SECTIONS
{
.init.ramfs : { *(.data) }
}
The above example is for i386 - the parameters vary from architectures.
Eventually look up LDFLAGS_BLOB in an older version of the
arch/$(ARCH)/Makefile to see the flags used before .incbin was introduced.
Using .incbin has the advantage over ld that the correct flags are set
in the ELF header, as required by certain architectures.
*/

.section .init.ramfs,"a"
.incbin "usr/initramfs_data.cpio.bz2"
29 changes: 29 additions & 0 deletions usr/initramfs_data.gz.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
initramfs_data includes the compressed binary that is the
filesystem used for early user space.
Note: Older versions of "as" (prior to binutils 2.11.90.0.23
released on 2001-07-14) dit not support .incbin.
If you are forced to use older binutils than that then the
following trick can be applied to create the resulting binary:
ld -m elf_i386 --format binary --oformat elf32-i386 -r \
-T initramfs_data.scr initramfs_data.cpio.gz -o initramfs_data.o
ld -m elf_i386 -r -o built-in.o initramfs_data.o
initramfs_data.scr looks like this:
SECTIONS
{
.init.ramfs : { *(.data) }
}
The above example is for i386 - the parameters vary from architectures.
Eventually look up LDFLAGS_BLOB in an older version of the
arch/$(ARCH)/Makefile to see the flags used before .incbin was introduced.
Using .incbin has the advantage over ld that the correct flags are set
in the ELF header, as required by certain architectures.
*/

.section .init.ramfs,"a"
.incbin "usr/initramfs_data.cpio.gz"
29 changes: 29 additions & 0 deletions usr/initramfs_data.lzma.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
initramfs_data includes the compressed binary that is the
filesystem used for early user space.
Note: Older versions of "as" (prior to binutils 2.11.90.0.23
released on 2001-07-14) dit not support .incbin.
If you are forced to use older binutils than that then the
following trick can be applied to create the resulting binary:
ld -m elf_i386 --format binary --oformat elf32-i386 -r \
-T initramfs_data.scr initramfs_data.cpio.gz -o initramfs_data.o
ld -m elf_i386 -r -o built-in.o initramfs_data.o
initramfs_data.scr looks like this:
SECTIONS
{
.init.ramfs : { *(.data) }
}
The above example is for i386 - the parameters vary from architectures.
Eventually look up LDFLAGS_BLOB in an older version of the
arch/$(ARCH)/Makefile to see the flags used before .incbin was introduced.
Using .incbin has the advantage over ld that the correct flags are set
in the ELF header, as required by certain architectures.
*/

.section .init.ramfs,"a"
.incbin "usr/initramfs_data.cpio.lzma"

0 comments on commit a26ee60

Please sign in to comment.