coreboot-kgpe-d16/util/cbfstool
Julius Werner ee87885343 cbfs-compression-tool: Fix minor edge cases in algorithm type parsing
This patch adds two minor improvements to the way cbfs-compression-tool
parses the compression algorithm type that is passed through the -t
option of the 'compress' subcommand. These improvements are intended
to prevent accidents and unexpected behavior when using the
cbfs-compression-tool, in particular in automated contexts such as a
Makefile rule.

In the first part of this patch, a return statement is inserted after
the 'if (algo->name == NULL)' check of the compress() function. This
causes the function to exit immediately and subsequently abort the
program when the algorithm type was not detected correctly. Previously,
execution would continue with the 'algo' pointer pointing to the zeroed
out stopper entry of the types_cbfs_compression[] array. The ultimate
effect of this would be to pass 0 as 'algo->type' to the
compression_function() function, which happens to be the same
enumeration value as is used for CBFS_COMPRESS_NONE, leading to a valid
compression function result that matches the behavior of no compression.
Thus, if a script calling cbfs-compression-tool compress contained a
typo in the -t parameter, it would continue running with an unintended
compression result rather than immediately exiting cleanly.

In the second part of this patch, the strcmp() function is replaced with
strcasecmp() when comparing 'algo->name' with the 'algoname' parameter
that was passed to the compress() function. strcasecmp() uses an
identical function signature as strcmp() and is thus suitable as a
drop-in replacement, but it differs in behavior: rather than only
returning a result of 0 when the two NULL-terminated input strings are
character by character identical, the strcasecmp() function applies a
weaker concept of identity where characters of the latin alphabet
(hexadecimal ranges 0x41 through 0x5a and 0x61 through 0x7a) are also
considered identical to other characters that differ from them only in
their case. This causes the -t parameter of cbfs-compression-tool
compress to also accept lowercase spellings of the available compression
algorithms, such as "lz4" instead of "LZ4" and "lzma" instead of "LZMA".
As an unintended but harmless side-effect, mixed-case spellings such as
"lZ4" or "LZmA" will also be recognized as valid compression algorithms.
(Note that since the character "4" (hexadecimal 0x34) of the "LZ4"
compression type name is not part of the above-mentioned ranges of latin
alphabet characters, no new substitutions become valid for that part of
the "LZ4" string with this patch.)

Change-Id: I375dbaeefaa0d4b0c5be81bf7668f8f330f1cf61
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://review.coreboot.org/26389
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
Reviewed-by: Paul Menzel <paulepanter@users.sourceforge.net>
2018-05-21 13:25:49 +00:00
..
console
flashmap Rename __attribute__((packed)) --> __packed 2017-07-13 19:45:59 +00:00
lz4 Rename __attribute__((packed)) --> __packed 2017-07-13 19:45:59 +00:00
lzma
EXAMPLE
Makefile cbfstool: Add install target to Makefile 2018-03-06 22:19:47 +00:00
Makefile.inc util/cbfstool: Fix build with armv7-eabi cross compiler 2017-08-02 09:55:11 +00:00
ProcessorBind.h
README.fmaptool
cbfs-mkpayload.c util/cbfstool: demote FV handling errors to debug 2017-07-07 09:01:29 +00:00
cbfs-mkstage.c util: change coreboot to lowercase 2017-06-27 18:26:01 +00:00
cbfs-payload-linux.c
cbfs.h Revert "cbfs/payload type: Fix build warning and whitespace in name" 2018-05-16 04:50:55 +00:00
cbfs_image.c cbfs: Rename CBFS_TYPE_PAYLOAD to CBFS_TYPE_SELF 2018-05-04 10:30:24 +00:00
cbfs_image.h util/cbfstool: Add "truncate" command 2017-10-10 15:16:17 +00:00
cbfs_sections.c
cbfs_sections.h
cbfscomptool.c cbfs-compression-tool: Fix minor edge cases in algorithm type parsing 2018-05-21 13:25:49 +00:00
cbfstool.c cbfstool: Drop `-t` parsing for `add-payload` command 2018-05-18 12:16:57 +00:00
coff.h
common.c util/cbfstool: Print all supported architectures 2018-04-11 14:12:00 +00:00
common.h util/cbfstool: Print all supported architectures 2018-04-11 14:12:00 +00:00
compiler.h util/cbmem, util/cbfstool: resolve macro errors on FreeBSD 2017-08-11 18:54:23 +00:00
compress.c
default-x86.fmd drivers/mrc_cache: Always generate an FMAP region 2018-01-20 16:11:44 +00:00
default.fmd drivers/mrc_cache: Always generate an FMAP region 2018-01-20 16:11:44 +00:00
elf.h
elfheaders.c util/cbfstool: calculate cbfs file size for xip stages 2017-12-19 22:28:20 +00:00
elfparsing.h util/cbfstool: calculate cbfs file size for xip stages 2017-12-19 22:28:20 +00:00
fit.c Rename __attribute__((packed)) --> __packed 2017-07-13 19:45:59 +00:00
fit.h
flashmap_tests.c
fmap_from_fmd.c
fmap_from_fmd.h
fmaptool.c
fmd.c
fmd.h
fmd_parser.c_shipped
fmd_parser.h_shipped
fmd_parser.y
fmd_scanner.c_shipped
fmd_scanner.h_shipped
fmd_scanner.l
fv.h
ifwitool.c Rename __attribute__((packed)) --> __packed 2017-07-13 19:45:59 +00:00
linux.h Rename __attribute__((packed)) --> __packed 2017-07-13 19:45:59 +00:00
linux_trampoline.S
linux_trampoline.c
linux_trampoline.h
option.h
partitioned_file.c util/cbfstool: Check for NULL before dereference 2017-12-20 16:35:13 +00:00
partitioned_file.h
rmodtool.c
rmodule.c
rmodule.h
swab.h
xdr.c

README.fmaptool

Flashmap descriptors in coreboot
================================
Flashmap (https://code.google.com/p/flashmap) is a binary format for representing the layout of
flash chips. Since coreboot is starting to use a "partition" of this format to describe the flash
chip layout---both at runtime and when flashing a new image onto a chip---, the project needed a
reasonably expressive plaintext format for representing such sections in the source tree. Our
solution is the fmd ("flashmap descriptor") language, and the files in this directory contain a
scanner, parser, semantic analyser, and flashmap converter. Here's an informal language description:

# <line comment>
<image name>[@<memory-mapped address>] <image size> {
	<section name>[@<offset from start of image>] [<section size>] [{
		<subsection name>[@<offset from start of parent section>] [<subsection size>] [{
			# Sections can be nested as deeply as desired
			<subsubsection name>[(CBFS)][@...] [...] [{...}]
		}]
		[<subsection name>[(CBFS)][@...] [...] [{...}]]
		# There can be many subsections at each level of nesting: they will be inserted
		# sequentially, and although gaps are allowed, any provided offsets are always
		# relative to the closest parent node's and must be strictly increasing with neither
		# overlapping nor degenerate-size sections.
	}]
}

Note that the above example contains a few symbols that are actually metasyntax, and therefore have
neither meaning nor place in a real file. The <.*> s indicate placeholders for parameters:
 - The names are strings, which are provided as single-word---no whitespace---groups of
   syntactically unimportant symbols (i.e. everything except @, {, and }): they are not surrounded
   by quotes or any other form of delimiter.
 - The other fields are nonnegative integers, which may be given as decimal or hexadecimal; in
   either case, a K, M, or G may be appended---without intermediate whitespace---as a multiplier.
 - Comments consist of anything one manages to enter, provided it doesn't start a new line.
The [.*] s indicate that a portion of the file could be omitted altogether:
 - Just because something is noted as optional doesn't mean it is in every case: the answer might
   actually depend on which other information is---or isn't---provided.
 - In particular, it is only legal to place a (CBFS) annotation on a leaf section; that is, choosing
   to add child sections excludes the possibility of putting a CBFS in their parent. Such
   annotations are only used to decide where CBFS empty file headers should be created, and do not
   result in the storage of any additional metadata in the resulting FMAP section.
Additionally, it's important to note these properties of the overall file and its values:
 - Other than within would-be strings and numbers, whitespace is ignored. It goes without saying
   that such power comes with responsibility, which is why this sentence is here.
 - Although the .*section names must be globally unique, one of them may---but is not required to---
   match the image name.
 - It is a syntax error to supply a number---besides 0---that begins with the character 0, as there
   is no intention of adding octals to the mix.
 - The image's memory address should be present on---and only on---layouts for memory-mapped chips.
 - Although it may be evident from above, all .*section offsets are relative only to the immediate
   parent. There is no way to include an absolute offset (i.e. from the beginning of flash), which
   means that it is "safe" to reorder the .*section s within a particular level of nesting, as long
   as the change doesn't cause their positions and sizes to necessitate overlap or zero sizes.
 - A .*section with omitted offset is assumed to start at as low a position as possible---with no
   consideration of alignment---and one with omitted size is assumed to fill the remaining space
   until the next sibling or before the end of its parent.
 - It's fine to omit any .*section 's offset, size, or both, provided its position and size are
   still unambiguous in the context of its *sibling* sections and its parent's *size*. In
   particular, knowledge of one .*section 's children or the .*section s' common parent's siblings
   will not be used for this purpose.
 - Although .*section s are not required to have children, the flash chip as a whole must have at
   least one.
 - Though the braces after .*section s may be omitted for those that have no children, if they are
   present, they must contain at least one child.

PL people and sympathizers may wish to examine the formal abstract syntax and context-free grammar,
which are located in fmd_scanner.l and fmd_scanner.y, respectively. Those interested in the
algorithm used to infer omitted values will feel at home in fmd.c, particularly near the definition
of validate_and_complete_info().