diff options
Diffstat (limited to 'roms/u-boot/test')
26 files changed, 2891 insertions, 0 deletions
diff --git a/roms/u-boot/test/Makefile b/roms/u-boot/test/Makefile new file mode 100644 index 00000000..9c95805c --- /dev/null +++ b/roms/u-boot/test/Makefile @@ -0,0 +1,8 @@ +# +# (C) Copyright 2012 The Chromium Authors +# +# SPDX-License-Identifier:	GPL-2.0+ +# + +obj-$(CONFIG_SANDBOX) += command_ut.o +obj-$(CONFIG_SANDBOX) += compression.o diff --git a/roms/u-boot/test/command_ut.c b/roms/u-boot/test/command_ut.c new file mode 100644 index 00000000..aaa1ee25 --- /dev/null +++ b/roms/u-boot/test/command_ut.c @@ -0,0 +1,180 @@ +/* + * Copyright (c) 2012, The Chromium Authors + * + * SPDX-License-Identifier:	GPL-2.0+ + */ + +#define DEBUG + +#include <common.h> +#ifdef CONFIG_SANDBOX +#include <os.h> +#endif + +static const char test_cmd[] = "setenv list 1\n setenv list ${list}2; " +		"setenv list ${list}3\0" +		"setenv list ${list}4"; + +static int do_ut_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ +	printf("%s: Testing commands\n", __func__); +	run_command("env default -f -a", 0); + +	/* run a single command */ +	run_command("setenv single 1", 0); +	assert(!strcmp("1", getenv("single"))); + +	/* make sure that compound statements work */ +#ifdef CONFIG_SYS_HUSH_PARSER +	run_command("if test -n ${single} ; then setenv check 1; fi", 0); +	assert(!strcmp("1", getenv("check"))); +	run_command("setenv check", 0); +#endif + +	/* commands separated by ; */ +	run_command_list("setenv list 1; setenv list ${list}1", -1, 0); +	assert(!strcmp("11", getenv("list"))); + +	/* commands separated by \n */ +	run_command_list("setenv list 1\n setenv list ${list}1", -1, 0); +	assert(!strcmp("11", getenv("list"))); + +	/* command followed by \n and nothing else */ +	run_command_list("setenv list 1${list}\n", -1, 0); +	assert(!strcmp("111", getenv("list"))); + +	/* three commands in a row */ +	run_command_list("setenv list 1\n setenv list ${list}2; " +		"setenv list ${list}3", -1, 0); +	assert(!strcmp("123", getenv("list"))); + +	/* a command string with \0 in it. Stuff after \0 should be ignored */ +	run_command("setenv list", 0); +	run_command_list(test_cmd, sizeof(test_cmd), 0); +	assert(!strcmp("123", getenv("list"))); + +	/* +	 * a command list where we limit execution to only the first command +	 * using the length parameter. +	 */ +	run_command_list("setenv list 1\n setenv list ${list}2; " +		"setenv list ${list}3", strlen("setenv list 1"), 0); +	assert(!strcmp("1", getenv("list"))); + +#ifdef CONFIG_SYS_HUSH_PARSER +	/* Test the 'test' command */ + +#define HUSH_TEST(name, expr, expected_result) \ +	run_command("if test " expr " ; then " \ +			"setenv " #name "_" #expected_result " y; else " \ +			"setenv " #name "_" #expected_result " n; fi", 0); \ +	assert(!strcmp(#expected_result, getenv(#name "_" #expected_result))); \ +	setenv(#name "_" #expected_result, NULL); + +	/* Basic operators */ +	HUSH_TEST(streq, "aaa = aaa", y); +	HUSH_TEST(streq, "aaa = bbb", n); + +	HUSH_TEST(strneq, "aaa != bbb", y); +	HUSH_TEST(strneq, "aaa != aaa", n); + +	HUSH_TEST(strlt, "aaa < bbb", y); +	HUSH_TEST(strlt, "bbb < aaa", n); + +	HUSH_TEST(strgt, "bbb > aaa", y); +	HUSH_TEST(strgt, "aaa > bbb", n); + +	HUSH_TEST(eq, "123 -eq 123", y); +	HUSH_TEST(eq, "123 -eq 456", n); + +	HUSH_TEST(ne, "123 -ne 456", y); +	HUSH_TEST(ne, "123 -ne 123", n); + +	HUSH_TEST(lt, "123 -lt 456", y); +	HUSH_TEST(lt_eq, "123 -lt 123", n); +	HUSH_TEST(lt, "456 -lt 123", n); + +	HUSH_TEST(le, "123 -le 456", y); +	HUSH_TEST(le_eq, "123 -le 123", y); +	HUSH_TEST(le, "456 -le 123", n); + +	HUSH_TEST(gt, "456 -gt 123", y); +	HUSH_TEST(gt_eq, "123 -gt 123", n); +	HUSH_TEST(gt, "123 -gt 456", n); + +	HUSH_TEST(ge, "456 -ge 123", y); +	HUSH_TEST(ge_eq, "123 -ge 123", y); +	HUSH_TEST(ge, "123 -ge 456", n); + +	HUSH_TEST(z, "-z \"\"", y); +	HUSH_TEST(z, "-z \"aaa\"", n); + +	HUSH_TEST(n, "-n \"aaa\"", y); +	HUSH_TEST(n, "-n \"\"", n); + +	/* Inversion of simple tests */ +	HUSH_TEST(streq_inv, "! aaa = aaa", n); +	HUSH_TEST(streq_inv, "! aaa = bbb", y); + +	HUSH_TEST(streq_inv_inv, "! ! aaa = aaa", y); +	HUSH_TEST(streq_inv_inv, "! ! aaa = bbb", n); + +	/* Binary operators */ +	HUSH_TEST(or_0_0, "aaa != aaa -o bbb != bbb", n); +	HUSH_TEST(or_0_1, "aaa != aaa -o bbb = bbb", y); +	HUSH_TEST(or_1_0, "aaa = aaa -o bbb != bbb", y); +	HUSH_TEST(or_1_1, "aaa = aaa -o bbb = bbb", y); + +	HUSH_TEST(and_0_0, "aaa != aaa -a bbb != bbb", n); +	HUSH_TEST(and_0_1, "aaa != aaa -a bbb = bbb", n); +	HUSH_TEST(and_1_0, "aaa = aaa -a bbb != bbb", n); +	HUSH_TEST(and_1_1, "aaa = aaa -a bbb = bbb", y); + +	/* Inversion within binary operators */ +	HUSH_TEST(or_0_0_inv, "! aaa != aaa -o ! bbb != bbb", y); +	HUSH_TEST(or_0_1_inv, "! aaa != aaa -o ! bbb = bbb", y); +	HUSH_TEST(or_1_0_inv, "! aaa = aaa -o ! bbb != bbb", y); +	HUSH_TEST(or_1_1_inv, "! aaa = aaa -o ! bbb = bbb", n); + +	HUSH_TEST(or_0_0_inv_inv, "! ! aaa != aaa -o ! ! bbb != bbb", n); +	HUSH_TEST(or_0_1_inv_inv, "! ! aaa != aaa -o ! ! bbb = bbb", y); +	HUSH_TEST(or_1_0_inv_inv, "! ! aaa = aaa -o ! ! bbb != bbb", y); +	HUSH_TEST(or_1_1_inv_inv, "! ! aaa = aaa -o ! ! bbb = bbb", y); + +	setenv("ut_var_nonexistent", NULL); +	setenv("ut_var_exists", "1"); +	HUSH_TEST(z_varexp_quoted, "-z \"$ut_var_nonexistent\"", y); +	HUSH_TEST(z_varexp_quoted, "-z \"$ut_var_exists\"", n); +	setenv("ut_var_exists", NULL); + +	run_command("setenv ut_var_space \" \"", 0); +	assert(!strcmp(getenv("ut_var_space"), " ")); +	run_command("setenv ut_var_test $ut_var_space", 0); +	assert(!getenv("ut_var_test")); +	run_command("setenv ut_var_test \"$ut_var_space\"", 0); +	assert(!strcmp(getenv("ut_var_test"), " ")); +	run_command("setenv ut_var_test \" 1${ut_var_space}${ut_var_space} 2 \"", 0); +	assert(!strcmp(getenv("ut_var_test"), " 1   2 ")); +	setenv("ut_var_space", NULL); +	setenv("ut_var_test", NULL); + +#ifdef CONFIG_SANDBOX +	/* File existence */ +	HUSH_TEST(e, "-e host - creating_this_file_breaks_uboot_unit_test", n); +	run_command("sb save host - creating_this_file_breaks_uboot_unit_test 0 1", 0); +	HUSH_TEST(e, "-e host - creating_this_file_breaks_uboot_unit_test", y); +	/* Perhaps this could be replaced by an "rm" shell command one day */ +	assert(!os_unlink("creating_this_file_breaks_uboot_unit_test")); +	HUSH_TEST(e, "-e host - creating_this_file_breaks_uboot_unit_test", n); +#endif +#endif + +	printf("%s: Everything went swimmingly\n", __func__); +	return 0; +} + +U_BOOT_CMD( +	ut_cmd,	5,	1,	do_ut_cmd, +	"Very basic test of command parsers", +	"" +); diff --git a/roms/u-boot/test/compression.c b/roms/u-boot/test/compression.c new file mode 100644 index 00000000..139ea019 --- /dev/null +++ b/roms/u-boot/test/compression.c @@ -0,0 +1,335 @@ +/* + * Copyright (c) 2013, The Chromium Authors + * + * SPDX-License-Identifier:	GPL-2.0+ + */ + +#define DEBUG + +#include <common.h> +#include <command.h> +#include <malloc.h> + +#include <u-boot/zlib.h> +#include <bzlib.h> + +#include <lzma/LzmaTypes.h> +#include <lzma/LzmaDec.h> +#include <lzma/LzmaTools.h> + +#include <linux/lzo.h> + +static const char plain[] = +	"I am a highly compressable bit of text.\n" +	"I am a highly compressable bit of text.\n" +	"I am a highly compressable bit of text.\n" +	"There are many like me, but this one is mine.\n" +	"If I were any shorter, there wouldn't be much sense in\n" +	"compressing me in the first place. At least with lzo, anyway,\n" +	"which appears to behave poorly in the face of short text\n" +	"messages.\n"; + +/* bzip2 -c /tmp/plain.txt > /tmp/plain.bz2 */ +static const char bzip2_compressed[] = +	"\x42\x5a\x68\x39\x31\x41\x59\x26\x53\x59\xe5\x63\xdd\x09\x00\x00" +	"\x28\x57\x80\x00\x10\x40\x85\x20\x20\x04\x00\x3f\xef\xdf\xf0\x30" +	"\x00\xd6\xd0\x34\x91\x89\xa6\xf5\x4d\x19\x1a\x19\x0d\x02\x34\xd4" +	"\xc9\x00\x34\x34\x00\x02\x48\x41\x35\x4f\xd4\xc6\x88\xd3\x50\x3d" +	"\x4f\x51\x82\x4f\x88\xc3\x0d\x05\x62\x4f\x91\xa3\x52\x1b\xd0\x52" +	"\x41\x4a\xa3\x98\xc2\x6b\xca\xa3\x82\xa5\xac\x8b\x15\x99\x68\xad" +	"\xdf\x29\xd6\xf1\xf7\x5a\x10\xcd\x8c\x26\x61\x94\x95\xfe\x9e\x16" +	"\x18\x28\x69\xd4\x23\x64\xcc\x2b\xe5\xe8\x5f\x00\xa4\x70\x26\x2c" +	"\xee\xbd\x59\x6d\x6a\xec\xfc\x31\xda\x59\x0a\x14\x2a\x60\x1c\xf0" +	"\x04\x86\x73\x9a\xc5\x5b\x87\x3f\x5b\x4c\x93\xe6\xb5\x35\x0d\xa6" +	"\xb1\x2e\x62\x7b\xab\x67\xe7\x99\x2a\x14\x5e\x9f\x64\xcb\x96\xf4" +	"\x0d\x65\xd4\x39\xe6\x8b\x7e\xea\x1c\x03\x69\x97\x83\x58\x91\x96" +	"\xe1\xf0\x9d\xa4\x15\x8b\xb8\xc6\x93\xdc\x3d\xd9\x3c\x22\x55\xef" +	"\xfb\xbb\x2a\xd3\x87\xa2\x8b\x04\xd9\x19\xf8\xe2\xfd\x4f\xdb\x1a" +	"\x07\xc8\x60\xa3\x3f\xf8\xbb\x92\x29\xc2\x84\x87\x2b\x1e\xe8\x48"; +static const unsigned long bzip2_compressed_size = 240; + +/* lzma -z -c /tmp/plain.txt > /tmp/plain.lzma */ +static const char lzma_compressed[] = +	"\x5d\x00\x00\x80\x00\xff\xff\xff\xff\xff\xff\xff\xff\x00\x24\x88" +	"\x08\x26\xd8\x41\xff\x99\xc8\xcf\x66\x3d\x80\xac\xba\x17\xf1\xc8" +	"\xb9\xdf\x49\x37\xb1\x68\xa0\x2a\xdd\x63\xd1\xa7\xa3\x66\xf8\x15" +	"\xef\xa6\x67\x8a\x14\x18\x80\xcb\xc7\xb1\xcb\x84\x6a\xb2\x51\x16" +	"\xa1\x45\xa0\xd6\x3e\x55\x44\x8a\x5c\xa0\x7c\xe5\xa8\xbd\x04\x57" +	"\x8f\x24\xfd\xb9\x34\x50\x83\x2f\xf3\x46\x3e\xb9\xb0\x00\x1a\xf5" +	"\xd3\x86\x7e\x8f\x77\xd1\x5d\x0e\x7c\xe1\xac\xde\xf8\x65\x1f\x4d" +	"\xce\x7f\xa7\x3d\xaa\xcf\x26\xa7\x58\x69\x1e\x4c\xea\x68\x8a\xe5" +	"\x89\xd1\xdc\x4d\xc7\xe0\x07\x42\xbf\x0c\x9d\x06\xd7\x51\xa2\x0b" +	"\x7c\x83\x35\xe1\x85\xdf\xee\xfb\xa3\xee\x2f\x47\x5f\x8b\x70\x2b" +	"\xe1\x37\xf3\x16\xf6\x27\x54\x8a\x33\x72\x49\xea\x53\x7d\x60\x0b" +	"\x21\x90\x66\xe7\x9e\x56\x61\x5d\xd8\xdc\x59\xf0\xac\x2f\xd6\x49" +	"\x6b\x85\x40\x08\x1f\xdf\x26\x25\x3b\x72\x44\xb0\xb8\x21\x2f\xb3" +	"\xd7\x9b\x24\x30\x78\x26\x44\x07\xc3\x33\xd1\x4d\x03\x1b\xe1\xff" +	"\xfd\xf5\x50\x8d\xca"; +static const unsigned long lzma_compressed_size = 229; + +/* lzop -c /tmp/plain.txt > /tmp/plain.lzo */ +static const char lzo_compressed[] = +	"\x89\x4c\x5a\x4f\x00\x0d\x0a\x1a\x0a\x10\x30\x20\x60\x09\x40\x01" +	"\x05\x03\x00\x00\x09\x00\x00\x81\xb4\x52\x09\x54\xf1\x00\x00\x00" +	"\x00\x09\x70\x6c\x61\x69\x6e\x2e\x74\x78\x74\x65\xb1\x07\x9c\x00" +	"\x00\x01\x5e\x00\x00\x01\x0f\xc3\xc7\x7a\xe0\x00\x16\x49\x20\x61" +	"\x6d\x20\x61\x20\x68\x69\x67\x68\x6c\x79\x20\x63\x6f\x6d\x70\x72" +	"\x65\x73\x73\x61\x62\x6c\x65\x20\x62\x69\x74\x20\x6f\x66\x20\x74" +	"\x65\x78\x74\x2e\x0a\x20\x2f\x9c\x00\x00\x22\x54\x68\x65\x72\x65" +	"\x20\x61\x72\x65\x20\x6d\x61\x6e\x79\x20\x6c\x69\x6b\x65\x20\x6d" +	"\x65\x2c\x20\x62\x75\x74\x20\x74\x68\x69\x73\x20\x6f\x6e\x65\x20" +	"\x69\x73\x20\x6d\x69\x6e\x65\x2e\x0a\x49\x66\x20\x49\x20\x77\x84" +	"\x06\x0a\x6e\x79\x20\x73\x68\x6f\x72\x74\x65\x72\x2c\x20\x74\x90" +	"\x08\x00\x08\x77\x6f\x75\x6c\x64\x6e\x27\x74\x20\x62\x65\x20\x6d" +	"\x75\x63\x68\x20\x73\x65\x6e\x73\x65\x20\x69\x6e\x0a\xf8\x19\x02" +	"\x69\x6e\x67\x20\x6d\x64\x02\x64\x06\x00\x5a\x20\x66\x69\x72\x73" +	"\x74\x20\x70\x6c\x61\x63\x65\x2e\x20\x41\x74\x20\x6c\x65\x61\x73" +	"\x74\x20\x77\x69\x74\x68\x20\x6c\x7a\x6f\x2c\x20\x61\x6e\x79\x77" +	"\x61\x79\x2c\x0a\x77\x68\x69\x63\x68\x20\x61\x70\x70\x65\x61\x72" +	"\x73\x20\x74\x6f\x20\x62\x65\x68\x61\x76\x65\x20\x70\x6f\x6f\x72" +	"\x6c\x79\x20\x69\x6e\x20\x74\x68\x65\x20\x66\x61\x63\x65\x20\x6f" +	"\x66\x20\x73\x68\x6f\x72\x74\x20\x74\x65\x78\x74\x0a\x6d\x65\x73" +	"\x73\x61\x67\x65\x73\x2e\x0a\x11\x00\x00\x00\x00\x00\x00"; +static const unsigned long lzo_compressed_size = 334; + + +#define TEST_BUFFER_SIZE	512 + +typedef int (*mutate_func)(void *, unsigned long, void *, unsigned long, +			   unsigned long *); + +static int compress_using_gzip(void *in, unsigned long in_size, +			       void *out, unsigned long out_max, +			       unsigned long *out_size) +{ +	int ret; +	unsigned long inout_size = out_max; + +	ret = gzip(out, &inout_size, in, in_size); +	if (out_size) +		*out_size = inout_size; + +	return ret; +} + +static int uncompress_using_gzip(void *in, unsigned long in_size, +				 void *out, unsigned long out_max, +				 unsigned long *out_size) +{ +	int ret; +	unsigned long inout_size = in_size; + +	ret = gunzip(out, out_max, in, &inout_size); +	if (out_size) +		*out_size = inout_size; + +	return ret; +} + +static int compress_using_bzip2(void *in, unsigned long in_size, +				void *out, unsigned long out_max, +				unsigned long *out_size) +{ +	/* There is no bzip2 compression in u-boot, so fake it. */ +	assert(in_size == strlen(plain)); +	assert(memcmp(plain, in, in_size) == 0); + +	if (bzip2_compressed_size > out_max) +		return -1; + +	memcpy(out, bzip2_compressed, bzip2_compressed_size); +	if (out_size) +		*out_size = bzip2_compressed_size; + +	return 0; +} + +static int uncompress_using_bzip2(void *in, unsigned long in_size, +				  void *out, unsigned long out_max, +				  unsigned long *out_size) +{ +	int ret; +	unsigned int inout_size = out_max; + +	ret = BZ2_bzBuffToBuffDecompress(out, &inout_size, in, in_size, +			CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0); +	if (out_size) +		*out_size = inout_size; + +	return (ret != BZ_OK); +} + +static int compress_using_lzma(void *in, unsigned long in_size, +			       void *out, unsigned long out_max, +			       unsigned long *out_size) +{ +	/* There is no lzma compression in u-boot, so fake it. */ +	assert(in_size == strlen(plain)); +	assert(memcmp(plain, in, in_size) == 0); + +	if (lzma_compressed_size > out_max) +		return -1; + +	memcpy(out, lzma_compressed, lzma_compressed_size); +	if (out_size) +		*out_size = lzma_compressed_size; + +	return 0; +} + +static int uncompress_using_lzma(void *in, unsigned long in_size, +				 void *out, unsigned long out_max, +				 unsigned long *out_size) +{ +	int ret; +	SizeT inout_size = out_max; + +	ret = lzmaBuffToBuffDecompress(out, &inout_size, in, in_size); +	if (out_size) +		*out_size = inout_size; + +	return (ret != SZ_OK); +} + +static int compress_using_lzo(void *in, unsigned long in_size, +			      void *out, unsigned long out_max, +			      unsigned long *out_size) +{ +	/* There is no lzo compression in u-boot, so fake it. */ +	assert(in_size == strlen(plain)); +	assert(memcmp(plain, in, in_size) == 0); + +	if (lzo_compressed_size > out_max) +		return -1; + +	memcpy(out, lzo_compressed, lzo_compressed_size); +	if (out_size) +		*out_size = lzo_compressed_size; + +	return 0; +} + +static int uncompress_using_lzo(void *in, unsigned long in_size, +				void *out, unsigned long out_max, +				unsigned long *out_size) +{ +	int ret; +	size_t input_size = in_size; +	size_t output_size = out_max; + +	ret = lzop_decompress(in, input_size, out, &output_size); +	if (out_size) +		*out_size = output_size; + +	return (ret != LZO_E_OK); +} + +#define errcheck(statement) if (!(statement)) { \ +	fprintf(stderr, "\tFailed: %s\n", #statement); \ +	ret = 1; \ +	goto out; \ +} + +static int run_test(char *name, mutate_func compress, mutate_func uncompress) +{ +	ulong orig_size, compressed_size, uncompressed_size; +	void *orig_buf; +	void *compressed_buf = NULL; +	void *uncompressed_buf = NULL; +	void *compare_buf = NULL; +	int ret; + +	printf(" testing %s ...\n", name); + +	orig_buf = (void *)plain; +	orig_size = strlen(orig_buf); /* Trailing NULL not included. */ +	errcheck(orig_size > 0); + +	compressed_size = uncompressed_size = TEST_BUFFER_SIZE; +	compressed_buf = malloc(compressed_size); +	errcheck(compressed_buf != NULL); +	uncompressed_buf = malloc(uncompressed_size); +	errcheck(uncompressed_buf != NULL); +	compare_buf = malloc(uncompressed_size); +	errcheck(compare_buf != NULL); + +	/* Compress works as expected. */ +	printf("\torig_size:%lu\n", orig_size); +	memset(compressed_buf, 'A', TEST_BUFFER_SIZE); +	errcheck(compress(orig_buf, orig_size, +			compressed_buf, compressed_size, +			&compressed_size) == 0); +	printf("\tcompressed_size:%lu\n", compressed_size); +	errcheck(compressed_size > 0); +	errcheck(compressed_size < orig_size); +	errcheck(((char *)compressed_buf)[compressed_size-1] != 'A'); +	errcheck(((char *)compressed_buf)[compressed_size] == 'A'); + +	/* Uncompresses with space remaining. */ +	errcheck(uncompress(compressed_buf, compressed_size, +			  uncompressed_buf, uncompressed_size, +			  &uncompressed_size) == 0); +	printf("\tuncompressed_size:%lu\n", uncompressed_size); +	errcheck(uncompressed_size == orig_size); +	errcheck(memcmp(orig_buf, uncompressed_buf, orig_size) == 0); + +	/* Uncompresses with exactly the right size output buffer. */ +	memset(uncompressed_buf, 'A', TEST_BUFFER_SIZE); +	errcheck(uncompress(compressed_buf, compressed_size, +			  uncompressed_buf, orig_size, +			  &uncompressed_size) == 0); +	errcheck(uncompressed_size == orig_size); +	errcheck(memcmp(orig_buf, uncompressed_buf, orig_size) == 0); +	errcheck(((char *)uncompressed_buf)[orig_size] == 'A'); + +	/* Make sure compression does not over-run. */ +	memset(compare_buf, 'A', TEST_BUFFER_SIZE); +	ret = compress(orig_buf, orig_size, +		       compare_buf, compressed_size - 1, +		       NULL); +	errcheck(((char *)compare_buf)[compressed_size] == 'A'); +	errcheck(ret != 0); +	printf("\tcompress does not overrun\n"); + +	/* Make sure decompression does not over-run. */ +	memset(compare_buf, 'A', TEST_BUFFER_SIZE); +	ret = uncompress(compressed_buf, compressed_size, +			 compare_buf, uncompressed_size - 1, +			 NULL); +	errcheck(((char *)compare_buf)[uncompressed_size - 1] == 'A'); +	errcheck(ret != 0); +	printf("\tuncompress does not overrun\n"); + +	/* Got here, everything is fine. */ +	ret = 0; + +out: +	printf(" %s: %s\n", name, ret == 0 ? "ok" : "FAILED"); + +	free(compare_buf); +	free(uncompressed_buf); +	free(compressed_buf); + +	return ret; +} + + +static int do_test_compression(cmd_tbl_t *cmdtp, int flag, int argc, +			       char * const argv[]) +{ +	int err = 0; + +	err += run_test("gzip", compress_using_gzip, uncompress_using_gzip); +	err += run_test("bzip2", compress_using_bzip2, uncompress_using_bzip2); +	err += run_test("lzma", compress_using_lzma, uncompress_using_lzma); +	err += run_test("lzo", compress_using_lzo, uncompress_using_lzo); + +	printf("test_compression %s\n", err == 0 ? "ok" : "FAILED"); + +	return err; +} + +U_BOOT_CMD( +	test_compression,	5,	1,	do_test_compression, +	"Basic test of compressors: gzip bzip2 lzma lzo", "" +); diff --git a/roms/u-boot/test/dm/.gitignore b/roms/u-boot/test/dm/.gitignore new file mode 100644 index 00000000..b741b8ab --- /dev/null +++ b/roms/u-boot/test/dm/.gitignore @@ -0,0 +1 @@ +/test.dtb diff --git a/roms/u-boot/test/dm/Makefile b/roms/u-boot/test/dm/Makefile new file mode 100644 index 00000000..4e9afe6c --- /dev/null +++ b/roms/u-boot/test/dm/Makefile @@ -0,0 +1,18 @@ +# +# Copyright (c) 2013 Google, Inc +# +# SPDX-License-Identifier:	GPL-2.0+ +# + +obj-$(CONFIG_CMD_DM) += cmd_dm.o +obj-$(CONFIG_DM_TEST) += test-driver.o +obj-$(CONFIG_DM_TEST) += test-fdt.o +obj-$(CONFIG_DM_TEST) += test-main.o +obj-$(CONFIG_DM_TEST) += test-uclass.o +obj-$(CONFIG_DM_TEST) += ut.o + +# Tests for particular subsystems - when enabling driver model for a new +# subsystem you must add sandbox tests here. +obj-$(CONFIG_DM_TEST) += core.o +obj-$(CONFIG_DM_TEST) += ut.o +obj-$(CONFIG_DM_GPIO) += gpio.o diff --git a/roms/u-boot/test/dm/cmd_dm.c b/roms/u-boot/test/dm/cmd_dm.c new file mode 100644 index 00000000..a03fe20f --- /dev/null +++ b/roms/u-boot/test/dm/cmd_dm.c @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2013 Google, Inc + * + * (C) Copyright 2012 + * Marek Vasut <marex@denx.de> + * + * SPDX-License-Identifier:	GPL-2.0+ + */ + +#include <common.h> +#include <dm.h> +#include <malloc.h> +#include <errno.h> +#include <asm/io.h> +#include <dm/root.h> +#include <dm/test.h> +#include <dm/uclass-internal.h> + +static int display_succ(struct device *in, char *buf) +{ +	int len; +	int ip = 0; +	char local[16]; +	struct device *pos, *n, *prev = NULL; + +	printf("%s- %s @ %08x", buf, in->name, map_to_sysmem(in)); +	if (in->flags & DM_FLAG_ACTIVATED) +		puts(" - activated"); +	puts("\n"); + +	if (list_empty(&in->child_head)) +		return 0; + +	len = strlen(buf); +	strncpy(local, buf, sizeof(local)); +	snprintf(local + len, 2, "|"); +	if (len && local[len - 1] == '`') +		local[len - 1] = ' '; + +	list_for_each_entry_safe(pos, n, &in->child_head, sibling_node) { +		if (ip++) +			display_succ(prev, local); +		prev = pos; +	} + +	snprintf(local + len, 2, "`"); +	display_succ(prev, local); + +	return 0; +} + +static int dm_dump(struct device *dev) +{ +	if (!dev) +		return -EINVAL; +	return display_succ(dev, ""); +} + +static int do_dm_dump_all(cmd_tbl_t *cmdtp, int flag, int argc, +			  char * const argv[]) +{ +	struct device *root; + +	root = dm_root(); +	printf("ROOT %08x\n", map_to_sysmem(root)); +	return dm_dump(root); +} + +static int do_dm_dump_uclass(cmd_tbl_t *cmdtp, int flag, int argc, +			     char * const argv[]) +{ +	struct uclass *uc; +	int ret; +	int id; + +	for (id = 0; id < UCLASS_COUNT; id++) { +		struct device *dev; + +		ret = uclass_get(id, &uc); +		if (ret) +			continue; + +		printf("uclass %d: %s\n", id, uc->uc_drv->name); +		for (ret = uclass_first_device(id, &dev); +		     dev; +		     ret = uclass_next_device(&dev)) { +			printf("  %s @  %08x:\n", dev->name, +			       map_to_sysmem(dev)); +		} +		puts("\n"); +	} + +	return 0; +} + +static int do_dm_test(cmd_tbl_t *cmdtp, int flag, int argc, +			  char * const argv[]) +{ +	return dm_test_main(); +} + +static cmd_tbl_t test_commands[] = { +	U_BOOT_CMD_MKENT(tree, 0, 1, do_dm_dump_all, "", ""), +	U_BOOT_CMD_MKENT(uclass, 1, 1, do_dm_dump_uclass, "", ""), +	U_BOOT_CMD_MKENT(test, 1, 1, do_dm_test, "", ""), +}; + +static int do_dm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ +	cmd_tbl_t *test_cmd; +	int ret; + +	if (argc != 2) +		return CMD_RET_USAGE; +	test_cmd = find_cmd_tbl(argv[1], test_commands, +				ARRAY_SIZE(test_commands)); +	argc -= 2; +	argv += 2; +	if (!test_cmd || argc > test_cmd->maxargs) +		return CMD_RET_USAGE; + +	ret = test_cmd->cmd(test_cmd, flag, argc, argv); + +	return cmd_process_error(test_cmd, ret); +} + +U_BOOT_CMD( +	dm,	2,	1,	do_dm, +	"Driver model low level access", +	"tree         Dump driver model tree\n" +	"dm uclass        Dump list of instances for each uclass\n" +	"dm test         Run tests" +); diff --git a/roms/u-boot/test/dm/core.c b/roms/u-boot/test/dm/core.c new file mode 100644 index 00000000..14a57c31 --- /dev/null +++ b/roms/u-boot/test/dm/core.c @@ -0,0 +1,544 @@ +/* + * Tests for the core driver model code + * + * Copyright (c) 2013 Google, Inc + * + * SPDX-License-Identifier:	GPL-2.0+ + */ + +#include <common.h> +#include <errno.h> +#include <dm.h> +#include <fdtdec.h> +#include <malloc.h> +#include <dm/device-internal.h> +#include <dm/root.h> +#include <dm/ut.h> +#include <dm/util.h> +#include <dm/test.h> +#include <dm/uclass-internal.h> + +DECLARE_GLOBAL_DATA_PTR; + +enum { +	TEST_INTVAL1		= 0, +	TEST_INTVAL2		= 3, +	TEST_INTVAL3		= 6, +	TEST_INTVAL_MANUAL	= 101112, +}; + +static const struct dm_test_pdata test_pdata[] = { +	{ .ping_add		= TEST_INTVAL1, }, +	{ .ping_add		= TEST_INTVAL2, }, +	{ .ping_add		= TEST_INTVAL3, }, +}; + +static const struct dm_test_pdata test_pdata_manual = { +	.ping_add		= TEST_INTVAL_MANUAL, +}; + +U_BOOT_DEVICE(dm_test_info1) = { +	.name = "test_drv", +	.platdata = &test_pdata[0], +}; + +U_BOOT_DEVICE(dm_test_info2) = { +	.name = "test_drv", +	.platdata = &test_pdata[1], +}; + +U_BOOT_DEVICE(dm_test_info3) = { +	.name = "test_drv", +	.platdata = &test_pdata[2], +}; + +static struct driver_info driver_info_manual = { +	.name = "test_manual_drv", +	.platdata = &test_pdata_manual, +}; + +/* Test that binding with platdata occurs correctly */ +static int dm_test_autobind(struct dm_test_state *dms) +{ +	struct device *dev; + +	/* +	 * We should have a single class (UCLASS_ROOT) and a single root +	 * device with no children. +	 */ +	ut_assert(dms->root); +	ut_asserteq(1, list_count_items(&gd->uclass_root)); +	ut_asserteq(0, list_count_items(&gd->dm_root->child_head)); +	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]); + +	ut_assertok(dm_scan_platdata()); + +	/* We should have our test class now at least, plus more children */ +	ut_assert(1 < list_count_items(&gd->uclass_root)); +	ut_assert(0 < list_count_items(&gd->dm_root->child_head)); + +	/* Our 3 dm_test_infox children should be bound to the test uclass */ +	ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]); + +	/* No devices should be probed */ +	list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node) +		ut_assert(!(dev->flags & DM_FLAG_ACTIVATED)); + +	/* Our test driver should have been bound 3 times */ +	ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3); + +	return 0; +} +DM_TEST(dm_test_autobind, 0); + +/* Test that autoprobe finds all the expected devices */ +static int dm_test_autoprobe(struct dm_test_state *dms) +{ +	int expected_base_add; +	struct device *dev; +	struct uclass *uc; +	int i; + +	ut_assertok(uclass_get(UCLASS_TEST, &uc)); +	ut_assert(uc); + +	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]); +	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]); + +	/* The root device should not be activated until needed */ +	ut_assert(!(dms->root->flags & DM_FLAG_ACTIVATED)); + +	/* +	 * We should be able to find the three test devices, and they should +	 * all be activated as they are used (lazy activation, required by +	 * U-Boot) +	 */ +	for (i = 0; i < 3; i++) { +		ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); +		ut_assert(dev); +		ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED), +			   "Driver %d/%s already activated", i, dev->name); + +		/* This should activate it */ +		ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev)); +		ut_assert(dev); +		ut_assert(dev->flags & DM_FLAG_ACTIVATED); + +		/* Activating a device should activate the root device */ +		if (!i) +			ut_assert(dms->root->flags & DM_FLAG_ACTIVATED); +	} + +	/* Our 3 dm_test_infox children should be passed to post_probe */ +	ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]); + +	/* Also we can check the per-device data */ +	expected_base_add = 0; +	for (i = 0; i < 3; i++) { +		struct dm_test_uclass_perdev_priv *priv; +		struct dm_test_pdata *pdata; + +		ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); +		ut_assert(dev); + +		priv = dev->uclass_priv; +		ut_assert(priv); +		ut_asserteq(expected_base_add, priv->base_add); + +		pdata = dev->platdata; +		expected_base_add += pdata->ping_add; +	} + +	return 0; +} +DM_TEST(dm_test_autoprobe, DM_TESTF_SCAN_PDATA); + +/* Check that we see the correct platdata in each device */ +static int dm_test_platdata(struct dm_test_state *dms) +{ +	const struct dm_test_pdata *pdata; +	struct device *dev; +	int i; + +	for (i = 0; i < 3; i++) { +		ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); +		ut_assert(dev); +		pdata = dev->platdata; +		ut_assert(pdata->ping_add == test_pdata[i].ping_add); +	} + +	return 0; +} +DM_TEST(dm_test_platdata, DM_TESTF_SCAN_PDATA); + +/* Test that we can bind, probe, remove, unbind a driver */ +static int dm_test_lifecycle(struct dm_test_state *dms) +{ +	int op_count[DM_TEST_OP_COUNT]; +	struct device *dev, *test_dev; +	int pingret; +	int ret; + +	memcpy(op_count, dm_testdrv_op_count, sizeof(op_count)); + +	ut_assertok(device_bind_by_name(dms->root, &driver_info_manual, +					&dev)); +	ut_assert(dev); +	ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] +			== op_count[DM_TEST_OP_BIND] + 1); +	ut_assert(!dev->priv); + +	/* Probe the device - it should fail allocating private data */ +	dms->force_fail_alloc = 1; +	ret = device_probe(dev); +	ut_assert(ret == -ENOMEM); +	ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE] +			== op_count[DM_TEST_OP_PROBE] + 1); +	ut_assert(!dev->priv); + +	/* Try again without the alloc failure */ +	dms->force_fail_alloc = 0; +	ut_assertok(device_probe(dev)); +	ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE] +			== op_count[DM_TEST_OP_PROBE] + 2); +	ut_assert(dev->priv); + +	/* This should be device 3 in the uclass */ +	ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev)); +	ut_assert(dev == test_dev); + +	/* Try ping */ +	ut_assertok(test_ping(dev, 100, &pingret)); +	ut_assert(pingret == 102); + +	/* Now remove device 3 */ +	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]); +	ut_assertok(device_remove(dev)); +	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]); + +	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]); +	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]); +	ut_assertok(device_unbind(dev)); +	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]); +	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]); + +	return 0; +} +DM_TEST(dm_test_lifecycle, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST); + +/* Test that we can bind/unbind and the lists update correctly */ +static int dm_test_ordering(struct dm_test_state *dms) +{ +	struct device *dev, *dev_penultimate, *dev_last, *test_dev; +	int pingret; + +	ut_assertok(device_bind_by_name(dms->root, &driver_info_manual, +					&dev)); +	ut_assert(dev); + +	/* Bind two new devices (numbers 4 and 5) */ +	ut_assertok(device_bind_by_name(dms->root, &driver_info_manual, +					&dev_penultimate)); +	ut_assert(dev_penultimate); +	ut_assertok(device_bind_by_name(dms->root, &driver_info_manual, +					&dev_last)); +	ut_assert(dev_last); + +	/* Now remove device 3 */ +	ut_assertok(device_remove(dev)); +	ut_assertok(device_unbind(dev)); + +	/* The device numbering should have shifted down one */ +	ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev)); +	ut_assert(dev_penultimate == test_dev); +	ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev)); +	ut_assert(dev_last == test_dev); + +	/* Add back the original device 3, now in position 5 */ +	ut_assertok(device_bind_by_name(dms->root, &driver_info_manual, &dev)); +	ut_assert(dev); + +	/* Try ping */ +	ut_assertok(test_ping(dev, 100, &pingret)); +	ut_assert(pingret == 102); + +	/* Remove 3 and 4 */ +	ut_assertok(device_remove(dev_penultimate)); +	ut_assertok(device_unbind(dev_penultimate)); +	ut_assertok(device_remove(dev_last)); +	ut_assertok(device_unbind(dev_last)); + +	/* Our device should now be in position 3 */ +	ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev)); +	ut_assert(dev == test_dev); + +	/* Now remove device 3 */ +	ut_assertok(device_remove(dev)); +	ut_assertok(device_unbind(dev)); + +	return 0; +} +DM_TEST(dm_test_ordering, DM_TESTF_SCAN_PDATA); + +/* Check that we can perform operations on a device (do a ping) */ +int dm_check_operations(struct dm_test_state *dms, struct device *dev, +			uint32_t base, struct dm_test_priv *priv) +{ +	int expected; +	int pingret; + +	/* Getting the child device should allocate platdata / priv */ +	ut_assertok(testfdt_ping(dev, 10, &pingret)); +	ut_assert(dev->priv); +	ut_assert(dev->platdata); + +	expected = 10 + base; +	ut_asserteq(expected, pingret); + +	/* Do another ping */ +	ut_assertok(testfdt_ping(dev, 20, &pingret)); +	expected = 20 + base; +	ut_asserteq(expected, pingret); + +	/* Now check the ping_total */ +	priv = dev->priv; +	ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2, +		    priv->ping_total); + +	return 0; +} + +/* Check that we can perform operations on devices */ +static int dm_test_operations(struct dm_test_state *dms) +{ +	struct device *dev; +	int i; + +	/* +	 * Now check that the ping adds are what we expect. This is using the +	 * ping-add property in each node. +	 */ +	for (i = 0; i < ARRAY_SIZE(test_pdata); i++) { +		uint32_t base; + +		ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev)); + +		/* +		 * Get the 'reg' property, which tells us what the ping add +		 * should be. We don't use the platdata because we want +		 * to test the code that sets that up (testfdt_drv_probe()). +		 */ +		base = test_pdata[i].ping_add; +		debug("dev=%d, base=%d\n", i, base); + +		ut_assert(!dm_check_operations(dms, dev, base, dev->priv)); +	} + +	return 0; +} +DM_TEST(dm_test_operations, DM_TESTF_SCAN_PDATA); + +/* Remove all drivers and check that things work */ +static int dm_test_remove(struct dm_test_state *dms) +{ +	struct device *dev; +	int i; + +	for (i = 0; i < 3; i++) { +		ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev)); +		ut_assert(dev); +		ut_assertf(dev->flags & DM_FLAG_ACTIVATED, +			   "Driver %d/%s not activated", i, dev->name); +		ut_assertok(device_remove(dev)); +		ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED), +			   "Driver %d/%s should have deactivated", i, +			   dev->name); +		ut_assert(!dev->priv); +	} + +	return 0; +} +DM_TEST(dm_test_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST); + +/* Remove and recreate everything, check for memory leaks */ +static int dm_test_leak(struct dm_test_state *dms) +{ +	int i; + +	for (i = 0; i < 2; i++) { +		struct mallinfo start, end; +		struct device *dev; +		int ret; +		int id; + +		start = mallinfo(); +		if (!start.uordblks) +			puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n"); + +		ut_assertok(dm_scan_platdata()); +		ut_assertok(dm_scan_fdt(gd->fdt_blob)); + +		/* Scanning the uclass is enough to probe all the devices */ +		for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) { +			for (ret = uclass_first_device(UCLASS_TEST, &dev); +			     dev; +			     ret = uclass_next_device(&dev)) +				; +			ut_assertok(ret); +		} + +		/* Don't delete the root class, since we started with that */ +		for (id = UCLASS_ROOT + 1; id < UCLASS_COUNT; id++) { +			struct uclass *uc; + +			uc = uclass_find(id); +			if (!uc) +				continue; +			ut_assertok(uclass_destroy(uc)); +		} + +		end = mallinfo(); +		ut_asserteq(start.uordblks, end.uordblks); +	} + +	return 0; +} +DM_TEST(dm_test_leak, 0); + +/* Test uclass init/destroy methods */ +static int dm_test_uclass(struct dm_test_state *dms) +{ +	struct uclass *uc; + +	ut_assertok(uclass_get(UCLASS_TEST, &uc)); +	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]); +	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]); +	ut_assert(uc->priv); + +	ut_assertok(uclass_destroy(uc)); +	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]); +	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]); + +	return 0; +} +DM_TEST(dm_test_uclass, 0); + +/** + * create_children() - Create children of a parent node + * + * @dms:	Test system state + * @parent:	Parent device + * @count:	Number of children to create + * @key:	Key value to put in first child. Subsequence children + *		receive an incrementing value + * @child:	If not NULL, then the child device pointers are written into + *		this array. + * @return 0 if OK, -ve on error + */ +static int create_children(struct dm_test_state *dms, struct device *parent, +			   int count, int key, struct device *child[]) +{ +	struct device *dev; +	int i; + +	for (i = 0; i < count; i++) { +		struct dm_test_pdata *pdata; + +		ut_assertok(device_bind_by_name(parent, &driver_info_manual, +						&dev)); +		pdata = calloc(1, sizeof(*pdata)); +		pdata->ping_add = key + i; +		dev->platdata = pdata; +		if (child) +			child[i] = dev; +	} + +	return 0; +} + +#define NODE_COUNT	10 + +static int dm_test_children(struct dm_test_state *dms) +{ +	struct device *top[NODE_COUNT]; +	struct device *child[NODE_COUNT]; +	struct device *grandchild[NODE_COUNT]; +	struct device *dev; +	int total; +	int ret; +	int i; + +	/* We don't care about the numbering for this test */ +	dms->skip_post_probe = 1; + +	ut_assert(NODE_COUNT > 5); + +	/* First create 10 top-level children */ +	ut_assertok(create_children(dms, dms->root, NODE_COUNT, 0, top)); + +	/* Now a few have their own children */ +	ut_assertok(create_children(dms, top[2], NODE_COUNT, 2, NULL)); +	ut_assertok(create_children(dms, top[5], NODE_COUNT, 5, child)); + +	/* And grandchildren */ +	for (i = 0; i < NODE_COUNT; i++) +		ut_assertok(create_children(dms, child[i], NODE_COUNT, 50 * i, +					    i == 2 ? grandchild : NULL)); + +	/* Check total number of devices */ +	total = NODE_COUNT * (3 + NODE_COUNT); +	ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]); + +	/* Try probing one of the grandchildren */ +	ut_assertok(uclass_get_device(UCLASS_TEST, +				      NODE_COUNT * 3 + 2 * NODE_COUNT, &dev)); +	ut_asserteq_ptr(grandchild[0], dev); + +	/* +	 * This should have probed the child and top node also, for a total +	 * of 3 nodes. +	 */ +	ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]); + +	/* Probe the other grandchildren */ +	for (i = 1; i < NODE_COUNT; i++) +		ut_assertok(device_probe(grandchild[i])); + +	ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]); + +	/* Probe everything */ +	for (ret = uclass_first_device(UCLASS_TEST, &dev); +	     dev; +	     ret = uclass_next_device(&dev)) +		; +	ut_assertok(ret); + +	ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]); + +	/* Remove a top-level child and check that the children are removed */ +	ut_assertok(device_remove(top[2])); +	ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]); +	dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0; + +	/* Try one with grandchildren */ +	ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev)); +	ut_asserteq_ptr(dev, top[5]); +	ut_assertok(device_remove(dev)); +	ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT), +		    dm_testdrv_op_count[DM_TEST_OP_REMOVE]); + +	/* Try the same with unbind */ +	ut_assertok(device_unbind(top[2])); +	ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]); +	dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0; + +	/* Try one with grandchildren */ +	ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev)); +	ut_asserteq_ptr(dev, top[6]); +	ut_assertok(device_unbind(top[5])); +	ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT), +		    dm_testdrv_op_count[DM_TEST_OP_UNBIND]); + +	return 0; +} +DM_TEST(dm_test_children, 0); diff --git a/roms/u-boot/test/dm/gpio.c b/roms/u-boot/test/dm/gpio.c new file mode 100644 index 00000000..bf632bca --- /dev/null +++ b/roms/u-boot/test/dm/gpio.c @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2013 Google, Inc + * + * SPDX-License-Identifier:	GPL-2.0+ + */ + +#include <common.h> +#include <fdtdec.h> +#include <dm.h> +#include <dm/ut.h> +#include <dm/test.h> +#include <dm/util.h> +#include <asm/gpio.h> + +/* Test that sandbox GPIOs work correctly */ +static int dm_test_gpio(struct dm_test_state *dms) +{ +	unsigned int offset, gpio; +	struct dm_gpio_ops *ops; +	struct device *dev; +	const char *name; +	int offset_count; +	char buf[80]; + +	/* +	 * We expect to get 3 banks. One is anonymous (just numbered) and +	 * comes from platdata. The other two are named a (20 gpios) +	 * and b (10 gpios) and come from the device tree. See +	 * test/dm/test.dts. +	 */ +	ut_assertok(gpio_lookup_name("b4", &dev, &offset, &gpio)); +	ut_asserteq_str(dev->name, "extra-gpios"); +	ut_asserteq(4, offset); +	ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 20 + 4, gpio); + +	name = gpio_get_bank_info(dev, &offset_count); +	ut_asserteq_str("b", name); +	ut_asserteq(10, offset_count); + +	/* Get the operations for this device */ +	ops = gpio_get_ops(dev); +	ut_assert(ops->get_state); + +	/* Cannot get a value until it is reserved */ +	ut_asserteq(-1, ops->get_value(dev, offset)); + +	/* +	 * Now some tests that use the 'sandbox' back door. All GPIOs +	 * should default to input, include b4 that we are using here. +	 */ +	ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf))); +	ut_asserteq_str("b4:  in: 0 [ ]", buf); + +	/* Change it to an output */ +	sandbox_gpio_set_direction(dev, offset, 1); +	ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf))); +	ut_asserteq_str("b4: out: 0 [ ]", buf); + +	sandbox_gpio_set_value(dev, offset, 1); +	ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf))); +	ut_asserteq_str("b4: out: 1 [ ]", buf); + +	ut_assertok(ops->request(dev, offset, "testing")); +	ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf))); +	ut_asserteq_str("b4: out: 1 [x] testing", buf); + +	/* Change the value a bit */ +	ut_asserteq(1, ops->get_value(dev, offset)); +	ut_assertok(ops->set_value(dev, offset, 0)); +	ut_asserteq(0, ops->get_value(dev, offset)); +	ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf))); +	ut_asserteq_str("b4: out: 0 [x] testing", buf); +	ut_assertok(ops->set_value(dev, offset, 1)); +	ut_asserteq(1, ops->get_value(dev, offset)); + +	/* Make it an input */ +	ut_assertok(ops->direction_input(dev, offset)); +	ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf))); +	ut_asserteq_str("b4:  in: 1 [x] testing", buf); +	sandbox_gpio_set_value(dev, offset, 0); +	ut_asserteq(0, sandbox_gpio_get_value(dev, offset)); +	ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf))); +	ut_asserteq_str("b4:  in: 0 [x] testing", buf); + +	ut_assertok(ops->free(dev, offset)); +	ut_assertok(ops->get_state(dev, offset, buf, sizeof(buf))); +	ut_asserteq_str("b4:  in: 0 [ ]", buf); + +	/* Check the 'a' bank also */ +	ut_assertok(gpio_lookup_name("a15", &dev, &offset, &gpio)); +	ut_asserteq_str(dev->name, "base-gpios"); +	ut_asserteq(15, offset); +	ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT + 15, gpio); + +	name = gpio_get_bank_info(dev, &offset_count); +	ut_asserteq_str("a", name); +	ut_asserteq(20, offset_count); + +	/* And the anonymous bank */ +	ut_assertok(gpio_lookup_name("14", &dev, &offset, &gpio)); +	ut_asserteq_str(dev->name, "gpio_sandbox"); +	ut_asserteq(14, offset); +	ut_asserteq(14, gpio); + +	name = gpio_get_bank_info(dev, &offset_count); +	ut_asserteq_ptr(NULL, name); +	ut_asserteq(CONFIG_SANDBOX_GPIO_COUNT, offset_count); + +	return 0; +} +DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); diff --git a/roms/u-boot/test/dm/test-dm.sh b/roms/u-boot/test/dm/test-dm.sh new file mode 100755 index 00000000..ef5aca5a --- /dev/null +++ b/roms/u-boot/test/dm/test-dm.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +NUM_CPUS=$(cat /proc/cpuinfo |grep -c processor) +dtc -I dts -O dtb test/dm/test.dts -o test/dm/test.dtb +make O=sandbox sandbox_config +make O=sandbox -s -j${NUM_CPUS} +./sandbox/u-boot -d test/dm/test.dtb -c "dm test" diff --git a/roms/u-boot/test/dm/test-driver.c b/roms/u-boot/test/dm/test-driver.c new file mode 100644 index 00000000..c4be8a12 --- /dev/null +++ b/roms/u-boot/test/dm/test-driver.c @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2013 Google, Inc + * + * (C) Copyright 2012 + * Pavel Herrmann <morpheus.ibis@gmail.com> + * + * SPDX-License-Identifier:	GPL-2.0+ + */ + +#include <common.h> +#include <dm.h> +#include <errno.h> +#include <malloc.h> +#include <dm/test.h> +#include <dm/ut.h> +#include <asm/io.h> + +int dm_testdrv_op_count[DM_TEST_OP_COUNT]; +static struct dm_test_state *dms = &global_test_state; + +static int testdrv_ping(struct device *dev, int pingval, int *pingret) +{ +	const struct dm_test_pdata *pdata = dev_get_platdata(dev); +	struct dm_test_priv *priv = dev_get_priv(dev); + +	*pingret = pingval + pdata->ping_add; +	priv->ping_total += *pingret; + +	return 0; +} + +static const struct test_ops test_ops = { +	.ping = testdrv_ping, +}; + +static int test_bind(struct device *dev) +{ +	/* Private data should not be allocated */ +	ut_assert(!dev_get_priv(dev)); + +	dm_testdrv_op_count[DM_TEST_OP_BIND]++; +	return 0; +} + +static int test_probe(struct device *dev) +{ +	struct dm_test_priv *priv = dev_get_priv(dev); + +	/* Private data should be allocated */ +	ut_assert(priv); + +	dm_testdrv_op_count[DM_TEST_OP_PROBE]++; +	priv->ping_total += DM_TEST_START_TOTAL; +	return 0; +} + +static int test_remove(struct device *dev) +{ +	/* Private data should still be allocated */ +	ut_assert(dev_get_priv(dev)); + +	dm_testdrv_op_count[DM_TEST_OP_REMOVE]++; +	return 0; +} + +static int test_unbind(struct device *dev) +{ +	/* Private data should not be allocated */ +	ut_assert(!dev->priv); + +	dm_testdrv_op_count[DM_TEST_OP_UNBIND]++; +	return 0; +} + +U_BOOT_DRIVER(test_drv) = { +	.name	= "test_drv", +	.id	= UCLASS_TEST, +	.ops	= &test_ops, +	.bind	= test_bind, +	.probe	= test_probe, +	.remove	= test_remove, +	.unbind	= test_unbind, +	.priv_auto_alloc_size = sizeof(struct dm_test_priv), +}; + +U_BOOT_DRIVER(test2_drv) = { +	.name	= "test2_drv", +	.id	= UCLASS_TEST, +	.ops	= &test_ops, +	.bind	= test_bind, +	.probe	= test_probe, +	.remove	= test_remove, +	.unbind	= test_unbind, +	.priv_auto_alloc_size = sizeof(struct dm_test_priv), +}; + +static int test_manual_drv_ping(struct device *dev, int pingval, int *pingret) +{ +	*pingret = pingval + 2; + +	return 0; +} + +static const struct test_ops test_manual_ops = { +	.ping = test_manual_drv_ping, +}; + +static int test_manual_bind(struct device *dev) +{ +	dm_testdrv_op_count[DM_TEST_OP_BIND]++; + +	return 0; +} + +static int test_manual_probe(struct device *dev) +{ +	dm_testdrv_op_count[DM_TEST_OP_PROBE]++; +	if (!dms->force_fail_alloc) +		dev->priv = calloc(1, sizeof(struct dm_test_priv)); +	if (!dev->priv) +		return -ENOMEM; + +	return 0; +} + +static int test_manual_remove(struct device *dev) +{ +	dm_testdrv_op_count[DM_TEST_OP_REMOVE]++; +	return 0; +} + +static int test_manual_unbind(struct device *dev) +{ +	dm_testdrv_op_count[DM_TEST_OP_UNBIND]++; +	return 0; +} + +U_BOOT_DRIVER(test_manual_drv) = { +	.name	= "test_manual_drv", +	.id	= UCLASS_TEST, +	.ops	= &test_manual_ops, +	.bind	= test_manual_bind, +	.probe	= test_manual_probe, +	.remove	= test_manual_remove, +	.unbind	= test_manual_unbind, +}; diff --git a/roms/u-boot/test/dm/test-fdt.c b/roms/u-boot/test/dm/test-fdt.c new file mode 100644 index 00000000..e1d982fd --- /dev/null +++ b/roms/u-boot/test/dm/test-fdt.c @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2013 Google, Inc + * + * SPDX-License-Identifier:	GPL-2.0+ + */ + +#include <common.h> +#include <dm.h> +#include <errno.h> +#include <fdtdec.h> +#include <malloc.h> +#include <asm/io.h> +#include <dm/test.h> +#include <dm/root.h> +#include <dm/ut.h> +#include <dm/uclass-internal.h> +#include <dm/util.h> + +DECLARE_GLOBAL_DATA_PTR; + +static int testfdt_drv_ping(struct device *dev, int pingval, int *pingret) +{ +	const struct dm_test_pdata *pdata = dev->platdata; +	struct dm_test_priv *priv = dev_get_priv(dev); + +	*pingret = pingval + pdata->ping_add; +	priv->ping_total += *pingret; + +	return 0; +} + +static const struct test_ops test_ops = { +	.ping = testfdt_drv_ping, +}; + +static int testfdt_ofdata_to_platdata(struct device *dev) +{ +	struct dm_test_pdata *pdata = dev_get_platdata(dev); + +	pdata->ping_add = fdtdec_get_int(gd->fdt_blob, dev->of_offset, +					"ping-add", -1); +	pdata->base = fdtdec_get_addr(gd->fdt_blob, dev->of_offset, "reg"); + +	return 0; +} + +static int testfdt_drv_probe(struct device *dev) +{ +	struct dm_test_priv *priv = dev_get_priv(dev); + +	priv->ping_total += DM_TEST_START_TOTAL; + +	return 0; +} + +static const struct device_id testfdt_ids[] = { +	{ +		.compatible = "denx,u-boot-fdt-test", +		.data = DM_TEST_TYPE_FIRST }, +	{ +		.compatible = "google,another-fdt-test", +		.data = DM_TEST_TYPE_SECOND }, +	{ } +}; + +U_BOOT_DRIVER(testfdt_drv) = { +	.name	= "testfdt_drv", +	.of_match	= testfdt_ids, +	.id	= UCLASS_TEST_FDT, +	.ofdata_to_platdata = testfdt_ofdata_to_platdata, +	.probe	= testfdt_drv_probe, +	.ops	= &test_ops, +	.priv_auto_alloc_size = sizeof(struct dm_test_priv), +	.platdata_auto_alloc_size = sizeof(struct dm_test_pdata), +}; + +/* From here is the testfdt uclass code */ +int testfdt_ping(struct device *dev, int pingval, int *pingret) +{ +	const struct test_ops *ops = device_get_ops(dev); + +	if (!ops->ping) +		return -ENOSYS; + +	return ops->ping(dev, pingval, pingret); +} + +UCLASS_DRIVER(testfdt) = { +	.name		= "testfdt", +	.id		= UCLASS_TEST_FDT, +}; + +/* Test that FDT-based binding works correctly */ +static int dm_test_fdt(struct dm_test_state *dms) +{ +	const int num_drivers = 3; +	struct device *dev; +	struct uclass *uc; +	int ret; +	int i; + +	ret = dm_scan_fdt(gd->fdt_blob); +	ut_assert(!ret); + +	ret = uclass_get(UCLASS_TEST_FDT, &uc); +	ut_assert(!ret); + +	/* These are num_drivers compatible root-level device tree nodes */ +	ut_asserteq(num_drivers, list_count_items(&uc->dev_head)); + +	/* Each should have no platdata / priv */ +	for (i = 0; i < num_drivers; i++) { +		ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev); +		ut_assert(!ret); +		ut_assert(!dev_get_priv(dev)); +		ut_assert(!dev->platdata); +	} + +	/* +	 * Now check that the ping adds are what we expect. This is using the +	 * ping-add property in each node. +	 */ +	for (i = 0; i < num_drivers; i++) { +		uint32_t base; + +		ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev); +		ut_assert(!ret); + +		/* +		 * Get the 'reg' property, which tells us what the ping add +		 * should be. We don't use the platdata because we want +		 * to test the code that sets that up (testfdt_drv_probe()). +		 */ +		base = fdtdec_get_addr(gd->fdt_blob, dev->of_offset, "reg"); +		debug("dev=%d, base=%d: %s\n", i, base, +		      fdt_get_name(gd->fdt_blob, dev->of_offset, NULL)); + +		ut_assert(!dm_check_operations(dms, dev, base, +					       dev_get_priv(dev))); +	} + +	return 0; +} +DM_TEST(dm_test_fdt, 0); diff --git a/roms/u-boot/test/dm/test-main.c b/roms/u-boot/test/dm/test-main.c new file mode 100644 index 00000000..828ed46f --- /dev/null +++ b/roms/u-boot/test/dm/test-main.c @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2013 Google, Inc + * + * SPDX-License-Identifier:	GPL-2.0+ + */ + +#include <common.h> +#include <dm.h> +#include <errno.h> +#include <dm/test.h> +#include <dm/root.h> +#include <dm/uclass-internal.h> +#include <dm/ut.h> + +DECLARE_GLOBAL_DATA_PTR; + +struct dm_test_state global_test_state; + +/* Get ready for testing */ +static int dm_test_init(struct dm_test_state *dms) +{ +	memset(dms, '\0', sizeof(*dms)); +	gd->dm_root = NULL; +	memset(dm_testdrv_op_count, '\0', sizeof(dm_testdrv_op_count)); + +	ut_assertok(dm_init()); +	dms->root = dm_root(); + +	return 0; +} + +/* Ensure all the test devices are probed */ +static int do_autoprobe(struct dm_test_state *dms) +{ +	struct device *dev; +	int ret; + +	/* Scanning the uclass is enough to probe all the devices */ +	for (ret = uclass_first_device(UCLASS_TEST, &dev); +	     dev; +	     ret = uclass_next_device(&dev)) +		; + +	return ret; +} + +static int dm_test_destroy(struct dm_test_state *dms) +{ +	int id; + +	for (id = 0; id < UCLASS_COUNT; id++) { +		struct uclass *uc; + +		/* +		 * If the uclass doesn't exist we don't want to create it. So +		 * check that here before we call uclass_find_device()/ +		 */ +		uc = uclass_find(id); +		if (!uc) +			continue; +		ut_assertok(uclass_destroy(uc)); +	} + +	return 0; +} + +int dm_test_main(void) +{ +	struct dm_test *tests = ll_entry_start(struct dm_test, dm_test); +	const int n_ents = ll_entry_count(struct dm_test, dm_test); +	struct dm_test_state *dms = &global_test_state; +	struct dm_test *test; + +	/* +	 * If we have no device tree, or it only has a root node, then these +	 * tests clearly aren't going to work... +	 */ +	if (!gd->fdt_blob || fdt_next_node(gd->fdt_blob, 0, NULL) < 0) { +		puts("Please run with test device tree:\n" +		     "     dtc -I dts -O dtb test/dm/test.dts  -o test/dm/test.dtb\n" +		     "    ./u-boot -d test/dm/test.dtb\n"); +		ut_assert(gd->fdt_blob); +	} + +	printf("Running %d driver model tests\n", n_ents); + +	for (test = tests; test < tests + n_ents; test++) { +		printf("Test: %s\n", test->name); +		ut_assertok(dm_test_init(dms)); + +		if (test->flags & DM_TESTF_SCAN_PDATA) +			ut_assertok(dm_scan_platdata()); +		if (test->flags & DM_TESTF_PROBE_TEST) +			ut_assertok(do_autoprobe(dms)); +		if (test->flags & DM_TESTF_SCAN_FDT) +			ut_assertok(dm_scan_fdt(gd->fdt_blob)); + +		if (test->func(dms)) +			break; + +		ut_assertok(dm_test_destroy(dms)); +	} + +	printf("Failures: %d\n", dms->fail_count); + +	return 0; +} diff --git a/roms/u-boot/test/dm/test-uclass.c b/roms/u-boot/test/dm/test-uclass.c new file mode 100644 index 00000000..8b564b89 --- /dev/null +++ b/roms/u-boot/test/dm/test-uclass.c @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2013 Google, Inc + * + * (C) Copyright 2012 + * Pavel Herrmann <morpheus.ibis@gmail.com> + * + * SPDX-License-Identifier:	GPL-2.0+ + */ + +#include <common.h> +#include <malloc.h> +#include <dm.h> +#include <errno.h> +#include <dm/test.h> +#include <dm/ut.h> +#include <asm/io.h> +#include <linux/list.h> + +static struct dm_test_state *dms = &global_test_state; + +int test_ping(struct device *dev, int pingval, int *pingret) +{ +	const struct test_ops *ops = device_get_ops(dev); + +	if (!ops->ping) +		return -ENOSYS; + +	return ops->ping(dev, pingval, pingret); +} + +static int test_post_bind(struct device *dev) +{ +	dm_testdrv_op_count[DM_TEST_OP_POST_BIND]++; + +	return 0; +} + +static int test_pre_unbind(struct device *dev) +{ +	dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]++; + +	return 0; +} + +static int test_post_probe(struct device *dev) +{ +	struct device *prev = list_entry(dev->uclass_node.prev, struct device, +					 uclass_node); +	struct dm_test_uclass_perdev_priv *priv = dev->uclass_priv; +	struct uclass *uc = dev->uclass; + +	dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]++; +	ut_assert(priv); +	ut_assert(device_active(dev)); +	priv->base_add = 0; +	if (dms->skip_post_probe) +		return 0; +	if (&prev->uclass_node != &uc->dev_head) { +		struct dm_test_uclass_perdev_priv *prev_uc_priv +				= prev->uclass_priv; +		struct dm_test_pdata *pdata = prev->platdata; + +		ut_assert(pdata); +		ut_assert(prev_uc_priv); +		priv->base_add = prev_uc_priv->base_add + pdata->ping_add; +	} + +	return 0; +} + +static int test_pre_remove(struct device *dev) +{ +	dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]++; + +	return 0; +} + +static int test_init(struct uclass *uc) +{ +	dm_testdrv_op_count[DM_TEST_OP_INIT]++; +	ut_assert(uc->priv); + +	return 0; +} + +static int test_destroy(struct uclass *uc) +{ +	dm_testdrv_op_count[DM_TEST_OP_DESTROY]++; + +	return 0; +} + +UCLASS_DRIVER(test) = { +	.name		= "test", +	.id		= UCLASS_TEST, +	.post_bind	= test_post_bind, +	.pre_unbind	= test_pre_unbind, +	.post_probe	= test_post_probe, +	.pre_remove	= test_pre_remove, +	.init		= test_init, +	.destroy	= test_destroy, +	.priv_auto_alloc_size	= sizeof(struct dm_test_uclass_priv), +	.per_device_auto_alloc_size = sizeof(struct dm_test_uclass_perdev_priv), +}; diff --git a/roms/u-boot/test/dm/test.dts b/roms/u-boot/test/dm/test.dts new file mode 100644 index 00000000..ec5364f7 --- /dev/null +++ b/roms/u-boot/test/dm/test.dts @@ -0,0 +1,59 @@ +/dts-v1/; + +/ { +	model = "sandbox"; +	compatible = "sandbox"; +	#address-cells = <1>; +	#size-cells = <0>; + +	a-test { +		reg = <0>; +		compatible = "denx,u-boot-fdt-test"; +		ping-add = <0>; +	}; + +	junk { +		reg = <1>; +		compatible = "not,compatible"; +	}; + +	no-compatible { +		reg = <2>; +	}; + +	b-test { +		reg = <3>; +		compatible = "denx,u-boot-fdt-test"; +		ping-add = <3>; +	}; + +	some-bus { +		#address-cells = <1>; +		#size-cells = <0>; +		reg = <4>; +		ping-add = <4>; +		c-test { +			compatible = "denx,u-boot-fdt-test"; +			reg = <5>; +			ping-add = <5>; +		}; +	}; + +	d-test { +		reg = <6>; +		ping-add = <6>; +		compatible = "google,another-fdt-test"; +	}; + +	base-gpios { +		compatible = "sandbox,gpio"; +		gpio-bank-name = "a"; +		num-gpios = <20>; +	}; + +	extra-gpios { +		compatible = "sandbox,gpio"; +		gpio-bank-name = "b"; +		num-gpios = <10>; +	}; +}; diff --git a/roms/u-boot/test/dm/ut.c b/roms/u-boot/test/dm/ut.c new file mode 100644 index 00000000..8b69bc2a --- /dev/null +++ b/roms/u-boot/test/dm/ut.c @@ -0,0 +1,33 @@ +/* + * Simple unit test library for driver model + * + * Copyright (c) 2013 Google, Inc + * + * SPDX-License-Identifier:	GPL-2.0+ + */ + +#include <common.h> +#include <dm/test.h> +#include <dm/ut.h> + +struct dm_test_state; + +void ut_fail(struct dm_test_state *dms, const char *fname, int line, +	     const char *func, const char *cond) +{ +	printf("%s:%d, %s(): %s\n", fname, line, func, cond); +	dms->fail_count++; +} + +void ut_failf(struct dm_test_state *dms, const char *fname, int line, +	      const char *func, const char *cond, const char *fmt, ...) +{ +	va_list args; + +	printf("%s:%d, %s(): %s: ", fname, line, func, cond); +	va_start(args, fmt); +	vprintf(fmt, args); +	va_end(args); +	putc('\n'); +	dms->fail_count++; +} diff --git a/roms/u-boot/test/image/test-fit.py b/roms/u-boot/test/image/test-fit.py new file mode 100755 index 00000000..7394df74 --- /dev/null +++ b/roms/u-boot/test/image/test-fit.py @@ -0,0 +1,410 @@ +#!/usr/bin/python +# +# Copyright (c) 2013, Google Inc. +# +# Sanity check of the FIT handling in U-Boot +# +# SPDX-License-Identifier:	GPL-2.0+ +# +# To run this: +# +# make O=sandbox sandbox_config +# make O=sandbox +# ./test/image/test-fit.py -u sandbox/u-boot + +import doctest +from optparse import OptionParser +import os +import shutil +import struct +import sys +import tempfile + +# The 'command' library in patman is convenient for running commands +base_path = os.path.dirname(sys.argv[0]) +patman = os.path.join(base_path, '../../tools/patman') +sys.path.append(patman) + +import command + +# Define a base ITS which we can adjust using % and a dictionary +base_its = ''' +/dts-v1/; + +/ { +        description = "Chrome OS kernel image with one or more FDT blobs"; +        #address-cells = <1>; + +        images { +                kernel@1 { +                        data = /incbin/("%(kernel)s"); +                        type = "kernel"; +                        arch = "sandbox"; +                        os = "linux"; +                        compression = "none"; +                        load = <0x40000>; +                        entry = <0x8>; +                }; +                fdt@1 { +                        description = "snow"; +                        data = /incbin/("u-boot.dtb"); +                        type = "flat_dt"; +                        arch = "sandbox"; +                        %(fdt_load)s +                        compression = "none"; +                        signature@1 { +                                algo = "sha1,rsa2048"; +                                key-name-hint = "dev"; +                        }; +                }; +                ramdisk@1 { +                        description = "snow"; +                        data = /incbin/("%(ramdisk)s"); +                        type = "ramdisk"; +                        arch = "sandbox"; +                        os = "linux"; +                        %(ramdisk_load)s +                        compression = "none"; +                }; +        }; +        configurations { +                default = "conf@1"; +                conf@1 { +                        kernel = "kernel@1"; +                        fdt = "fdt@1"; +                        %(ramdisk_config)s +                }; +        }; +}; +''' + +# Define a base FDT - currently we don't use anything in this +base_fdt = ''' +/dts-v1/; + +/ { +        model = "Sandbox Verified Boot Test"; +        compatible = "sandbox"; + +}; +''' + +# This is the U-Boot script that is run for each test. First load the fit, +# then do the 'bootm' command, then save out memory from the places where +# we expect 'bootm' to write things. Then quit. +base_script = ''' +sb load host 0 %(fit_addr)x %(fit)s +fdt addr %(fit_addr)x +bootm start %(fit_addr)x +bootm loados +sb save host 0 %(kernel_out)s %(kernel_addr)x %(kernel_size)x +sb save host 0 %(fdt_out)s %(fdt_addr)x %(fdt_size)x +sb save host 0 %(ramdisk_out)s %(ramdisk_addr)x %(ramdisk_size)x +reset +''' + +def make_fname(leaf): +    """Make a temporary filename + +    Args: +        leaf: Leaf name of file to create (within temporary directory) +    Return: +        Temporary filename +    """ +    global base_dir + +    return os.path.join(base_dir, leaf) + +def filesize(fname): +    """Get the size of a file + +    Args: +        fname: Filename to check +    Return: +        Size of file in bytes +    """ +    return os.stat(fname).st_size + +def read_file(fname): +    """Read the contents of a file + +    Args: +        fname: Filename to read +    Returns: +        Contents of file as a string +    """ +    with open(fname, 'r') as fd: +        return fd.read() + +def make_dtb(): +    """Make a sample .dts file and compile it to a .dtb + +    Returns: +        Filename of .dtb file created +    """ +    src = make_fname('u-boot.dts') +    dtb = make_fname('u-boot.dtb') +    with open(src, 'w') as fd: +        print >>fd, base_fdt +    command.Output('dtc', src, '-O', 'dtb', '-o', dtb) +    return dtb + +def make_its(params): +    """Make a sample .its file with parameters embedded + +    Args: +        params: Dictionary containing parameters to embed in the %() strings +    Returns: +        Filename of .its file created +    """ +    its = make_fname('test.its') +    with open(its, 'w') as fd: +        print >>fd, base_its % params +    return its + +def make_fit(mkimage, params): +    """Make a sample .fit file ready for loading + +    This creates a .its script with the selected parameters and uses mkimage to +    turn this into a .fit image. + +    Args: +        mkimage: Filename of 'mkimage' utility +        params: Dictionary containing parameters to embed in the %() strings +    Return: +        Filename of .fit file created +    """ +    fit = make_fname('test.fit') +    its = make_its(params) +    command.Output(mkimage, '-f', its, fit) +    with open(make_fname('u-boot.dts'), 'w') as fd: +        print >>fd, base_fdt +    return fit + +def make_kernel(): +    """Make a sample kernel with test data + +    Returns: +        Filename of kernel created +    """ +    fname = make_fname('test-kernel.bin') +    data = '' +    for i in range(100): +        data += 'this kernel %d is unlikely to boot\n' % i +    with open(fname, 'w') as fd: +        print >>fd, data +    return fname + +def make_ramdisk(): +    """Make a sample ramdisk with test data + +    Returns: +        Filename of ramdisk created +    """ +    fname = make_fname('test-ramdisk.bin') +    data = '' +    for i in range(100): +        data += 'ramdisk %d was seldom used in the middle ages\n' % i +    with open(fname, 'w') as fd: +        print >>fd, data +    return fname + +def find_matching(text, match): +    """Find a match in a line of text, and return the unmatched line portion + +    This is used to extract a part of a line from some text. The match string +    is used to locate the line - we use the first line that contains that +    match text. + +    Once we find a match, we discard the match string itself from the line, +    and return what remains. + +    TODO: If this function becomes more generally useful, we could change it +    to use regex and return groups. + +    Args: +        text: Text to check (each line separated by \n) +        match: String to search for +    Return: +        String containing unmatched portion of line +    Exceptions: +        ValueError: If match is not found + +    >>> find_matching('first line:10\\nsecond_line:20', 'first line:') +    '10' +    >>> find_matching('first line:10\\nsecond_line:20', 'second linex') +    Traceback (most recent call last): +      ... +    ValueError: Test aborted +    >>> find_matching('first line:10\\nsecond_line:20', 'second_line:') +    '20' +    """ +    for line in text.splitlines(): +        pos = line.find(match) +        if pos != -1: +            return line[:pos] + line[pos + len(match):] + +    print "Expected '%s' but not found in output:" +    print text +    raise ValueError('Test aborted') + +def set_test(name): +    """Set the name of the current test and print a message + +    Args: +        name: Name of test +    """ +    global test_name + +    test_name = name +    print name + +def fail(msg, stdout): +    """Raise an error with a helpful failure message + +    Args: +        msg: Message to display +    """ +    print stdout +    raise ValueError("Test '%s' failed: %s" % (test_name, msg)) + +def run_fit_test(mkimage, u_boot): +    """Basic sanity check of FIT loading in U-Boot + +    TODO: Almost everything: +       - hash algorithms - invalid hash/contents should be detected +       - signature algorithms - invalid sig/contents should be detected +       - compression +       - checking that errors are detected like: +            - image overwriting +            - missing images +            - invalid configurations +            - incorrect os/arch/type fields +            - empty data +            - images too large/small +            - invalid FDT (e.g. putting a random binary in instead) +       - default configuration selection +       - bootm command line parameters should have desired effect +       - run code coverage to make sure we are testing all the code +    """ +    global test_name + +    # Set up invariant files +    control_dtb = make_dtb() +    kernel = make_kernel() +    ramdisk = make_ramdisk() +    kernel_out = make_fname('kernel-out.bin') +    fdt_out = make_fname('fdt-out.dtb') +    ramdisk_out = make_fname('ramdisk-out.bin') + +    # Set up basic parameters with default values +    params = { +        'fit_addr' : 0x1000, + +        'kernel' : kernel, +        'kernel_out' : kernel_out, +        'kernel_addr' : 0x40000, +        'kernel_size' : filesize(kernel), + +        'fdt_out' : fdt_out, +        'fdt_addr' : 0x80000, +        'fdt_size' : filesize(control_dtb), +        'fdt_load' : '', + +        'ramdisk' : ramdisk, +        'ramdisk_out' : ramdisk_out, +        'ramdisk_addr' : 0xc0000, +        'ramdisk_size' : filesize(ramdisk), +        'ramdisk_load' : '', +        'ramdisk_config' : '', +    } + +    # Make a basic FIT and a script to load it +    fit = make_fit(mkimage, params) +    params['fit'] = fit +    cmd = base_script % params + +    # First check that we can load a kernel +    # We could perhaps reduce duplication with some loss of readability +    set_test('Kernel load') +    stdout = command.Output(u_boot, '-d', control_dtb, '-c', cmd) +    if read_file(kernel) != read_file(kernel_out): +        fail('Kernel not loaded', stdout) +    if read_file(control_dtb) == read_file(fdt_out): +        fail('FDT loaded but should be ignored', stdout) +    if read_file(ramdisk) == read_file(ramdisk_out): +        fail('Ramdisk loaded but should not be', stdout) + +    # Find out the offset in the FIT where U-Boot has found the FDT +    line = find_matching(stdout, 'Booting using the fdt blob at ') +    fit_offset = int(line, 16) - params['fit_addr'] +    fdt_magic = struct.pack('>L', 0xd00dfeed) +    data = read_file(fit) + +    # Now find where it actually is in the FIT (skip the first word) +    real_fit_offset = data.find(fdt_magic, 4) +    if fit_offset != real_fit_offset: +        fail('U-Boot loaded FDT from offset %#x, FDT is actually at %#x' % +                (fit_offset, real_fit_offset), stdout) + +    # Now a kernel and an FDT +    set_test('Kernel + FDT load') +    params['fdt_load'] = 'load = <%#x>;' % params['fdt_addr'] +    fit = make_fit(mkimage, params) +    stdout = command.Output(u_boot, '-d', control_dtb, '-c', cmd) +    if read_file(kernel) != read_file(kernel_out): +        fail('Kernel not loaded', stdout) +    if read_file(control_dtb) != read_file(fdt_out): +        fail('FDT not loaded', stdout) +    if read_file(ramdisk) == read_file(ramdisk_out): +        fail('Ramdisk loaded but should not be', stdout) + +    # Try a ramdisk +    set_test('Kernel + FDT + Ramdisk load') +    params['ramdisk_config'] = 'ramdisk = "ramdisk@1";' +    params['ramdisk_load'] = 'load = <%#x>;' % params['ramdisk_addr'] +    fit = make_fit(mkimage, params) +    stdout = command.Output(u_boot, '-d', control_dtb, '-c', cmd) +    if read_file(ramdisk) != read_file(ramdisk_out): +        fail('Ramdisk not loaded', stdout) + +def run_tests(): +    """Parse options, run the FIT tests and print the result""" +    global base_path, base_dir + +    # Work in a temporary directory +    base_dir = tempfile.mkdtemp() +    parser = OptionParser() +    parser.add_option('-u', '--u-boot', +            default=os.path.join(base_path, 'u-boot'), +            help='Select U-Boot sandbox binary') +    parser.add_option('-k', '--keep', action='store_true', +            help="Don't delete temporary directory even when tests pass") +    parser.add_option('-t', '--selftest', action='store_true', +            help='Run internal self tests') +    (options, args) = parser.parse_args() + +    # Find the path to U-Boot, and assume mkimage is in its tools/mkimage dir +    base_path = os.path.dirname(options.u_boot) +    mkimage = os.path.join(base_path, 'tools/mkimage') + +    # There are a few doctests - handle these here +    if options.selftest: +        doctest.testmod() +        return + +    title = 'FIT Tests' +    print title, '\n', '=' * len(title) + +    run_fit_test(mkimage, options.u_boot) + +    print '\nTests passed' +    print 'Caveat: this is only a sanity check - test coverage is poor' + +    # Remove the tempoerary directory unless we are asked to keep it +    if options.keep: +        print "Output files are in '%s'" % base_dir +    else: +        shutil.rmtree(base_dir) + +run_tests() diff --git a/roms/u-boot/test/image/test-imagetools.sh b/roms/u-boot/test/image/test-imagetools.sh new file mode 100755 index 00000000..9e299e1e --- /dev/null +++ b/roms/u-boot/test/image/test-imagetools.sh @@ -0,0 +1,141 @@ +#!/bin/bash +# +# Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com> +# +# Sanity check for mkimage and dumpimage tools +# +# SPDX-License-Identifier:	GPL-2.0+ +# +# To run this: +# +# make O=sandbox sandbox_config +# make O=sandbox +# ./test/image/test-imagetools.sh + +BASEDIR=sandbox +SRCDIR=sandbox/boot +IMAGE_NAME="v1.0-test" +IMAGE=linux.img +DATAFILE0=vmlinuz +DATAFILE1=initrd.img +DATAFILE2=System.map +DATAFILES="${DATAFILE0} ${DATAFILE1} ${DATAFILE2}" +TEST_OUT=test_output +MKIMAGE=${BASEDIR}/tools/mkimage +DUMPIMAGE=${BASEDIR}/tools/dumpimage +MKIMAGE_LIST=mkimage.list +DUMPIMAGE_LIST=dumpimage.list + +# Remove all the files we created +cleanup() +{ +	local file + +	for file in ${DATAFILES}; do +		rm -f ${file} ${SRCDIR}/${file} +	done +	rm -f ${IMAGE} ${DUMPIMAGE_LIST} ${MKIMAGE_LIST} ${TEST_OUT} +	rmdir ${SRCDIR} +} + +# Check that two files are the same +assert_equal() +{ +	if ! diff $1 $2; then +		echo "Failed." +		cleanup +		exit 1 +	fi +} + +# Create some test files +create_files() +{ +	local file + +	mkdir -p ${SRCDIR} +	for file in ${DATAFILES}; do +		head -c $RANDOM /dev/urandom >${SRCDIR}/${file} +	done +} + +# Run a command, echoing it first +do_cmd() +{ +	local cmd="$@" + +	echo "# ${cmd}" +	${cmd} 2>&1 +} + +# Run a command, redirecting output +# Args: +#    redirect_file +#    command... +do_cmd_redir() +{ +	local redir="$1" +	shift +	local cmd="$@" + +	echo "# ${cmd}" +	${cmd} >${redir} +} + +# Write files into an image +create_image() +{ +	local files="${SRCDIR}/${DATAFILE0}:${SRCDIR}/${DATAFILE1}" +	files+=":${SRCDIR}/${DATAFILE2}" + +	echo -e "\nBuilding image..." +	do_cmd ${MKIMAGE} -A x86 -O linux -T multi -n \"${IMAGE_NAME}\" \ +		-d ${files} ${IMAGE} +	echo "done." +} + +# Extract files from an image +extract_image() +{ +	echo -e "\nExtracting image contents..." +	do_cmd ${DUMPIMAGE} -i ${IMAGE} -p 0 ${DATAFILE0} +	do_cmd ${DUMPIMAGE} -i ${IMAGE} -p 1 ${DATAFILE1} +	do_cmd ${DUMPIMAGE} -i ${IMAGE} -p 2 ${DATAFILE2} +	do_cmd ${DUMPIMAGE} -i ${IMAGE} -p 2 ${DATAFILE2} -o ${TEST_OUT} +	echo "done." +} + +# List the contents of a file +list_image() +{ +	echo -e "\nListing image contents..." +	do_cmd_redir ${MKIMAGE_LIST} ${MKIMAGE} -l ${IMAGE} +	do_cmd_redir ${DUMPIMAGE_LIST} ${DUMPIMAGE} -l ${IMAGE} +	echo "done." +} + +main() +{ +	local file + +	create_files + +	# Compress and extract multifile images, compare the result +	create_image +	extract_image +	for file in ${DATAFILES}; do +		assert_equal ${file} ${SRCDIR}/${file} +	done +	assert_equal ${TEST_OUT} ${DATAFILE2} + +	# List contents and compares output fro tools +	list_image +	assert_equal ${DUMPIMAGE_LIST} ${MKIMAGE_LIST} + +	# Remove files created +	cleanup + +	echo "Tests passed." +} + +main diff --git a/roms/u-boot/test/trace/test-trace.sh b/roms/u-boot/test/trace/test-trace.sh new file mode 100755 index 00000000..aa02f094 --- /dev/null +++ b/roms/u-boot/test/trace/test-trace.sh @@ -0,0 +1,76 @@ +# Copyright (c) 2013 The Chromium OS Authors. +# +# SPDX-License-Identifier:	GPL-2.0+ +# + +# Simple test script for tracing with sandbox + +OUTPUT_DIR=sandbox +TRACE_OPT="FTRACE=1" + +fail() { +	echo "Test failed: $1" +	if [ -n ${tmp} ]; then +		rm ${tmp} +	fi +	exit 1 +} + +build_uboot() { +	echo "Build sandbox" +	OPTS="O=${OUTPUT_DIR} ${TRACE_OPT}" +	NUM_CPUS=$(grep -c processor /proc/cpuinfo) +	make ${OPTS} sandbox_config +	make ${OPTS} -s -j${NUM_CPUS} +} + +run_trace() { +	echo "Run trace" +	./${OUTPUT_DIR}/u-boot <<END +	trace stats +	hash sha256 0 10000 +	trace pause +	trace stats +	hash sha256 0 10000 +	trace stats +	trace resume +	hash sha256 0 10000 +	trace pause +	trace stats +	reset +END +} + +check_results() { +	echo "Check results" + +	# Expect sha256 to run 3 times, so we see the string 6 times +	if [ $(grep -c sha256 ${tmp}) -ne 6 ]; then +		fail "sha256 error" +	fi + +	# 4 sets of results (output of 'trace stats') +	if [ $(grep -c "traced function calls" ${tmp}) -ne 4 ]; then +		fail "trace output error" +	fi + +	# Check trace counts. We expect to see an increase in the number of +	# traced function calls between each 'trace stats' command, except +	# between calls 2 and 3, where tracing is paused. +	# This code gets the sign of the difference between each number and +	# its predecessor. +	counts="$(tr -d , <${tmp} | awk '/traced function calls/ { diff = $1 - upto; upto = $1; printf "%d ", diff < 0 ? -1 : (diff > 0 ? 1 : 0)}')" + +	if [ "${counts}" != "1 1 0 1 " ]; then +		fail "trace collection error: ${counts}" +	fi +} + +echo "Simple trace test / sanity check using sandbox" +echo +tmp="$(tempfile)" +build_uboot +run_trace >${tmp} +check_results ${tmp} +rm ${tmp} +echo "Test passed" diff --git a/roms/u-boot/test/vboot/.gitignore b/roms/u-boot/test/vboot/.gitignore new file mode 100644 index 00000000..46312427 --- /dev/null +++ b/roms/u-boot/test/vboot/.gitignore @@ -0,0 +1,3 @@ +/*.dtb +/test.fit +/dev-keys diff --git a/roms/u-boot/test/vboot/sandbox-kernel.dts b/roms/u-boot/test/vboot/sandbox-kernel.dts new file mode 100644 index 00000000..a1e853c9 --- /dev/null +++ b/roms/u-boot/test/vboot/sandbox-kernel.dts @@ -0,0 +1,7 @@ +/dts-v1/; + +/ { +	model = "Sandbox Verified Boot Test"; +	compatible = "sandbox"; + +}; diff --git a/roms/u-boot/test/vboot/sandbox-u-boot.dts b/roms/u-boot/test/vboot/sandbox-u-boot.dts new file mode 100644 index 00000000..a1e853c9 --- /dev/null +++ b/roms/u-boot/test/vboot/sandbox-u-boot.dts @@ -0,0 +1,7 @@ +/dts-v1/; + +/ { +	model = "Sandbox Verified Boot Test"; +	compatible = "sandbox"; + +}; diff --git a/roms/u-boot/test/vboot/sign-configs-sha1.its b/roms/u-boot/test/vboot/sign-configs-sha1.its new file mode 100644 index 00000000..db2ed793 --- /dev/null +++ b/roms/u-boot/test/vboot/sign-configs-sha1.its @@ -0,0 +1,45 @@ +/dts-v1/; + +/ { +	description = "Chrome OS kernel image with one or more FDT blobs"; +	#address-cells = <1>; + +	images { +		kernel@1 { +			data = /incbin/("test-kernel.bin"); +			type = "kernel_noload"; +			arch = "sandbox"; +			os = "linux"; +			compression = "none"; +			load = <0x4>; +			entry = <0x8>; +			kernel-version = <1>; +			hash@1 { +				algo = "sha1"; +			}; +		}; +		fdt@1 { +			description = "snow"; +			data = /incbin/("sandbox-kernel.dtb"); +			type = "flat_dt"; +			arch = "sandbox"; +			compression = "none"; +			fdt-version = <1>; +			hash@1 { +				algo = "sha1"; +			}; +		}; +	}; +	configurations { +		default = "conf@1"; +		conf@1 { +			kernel = "kernel@1"; +			fdt = "fdt@1"; +			signature@1 { +				algo = "sha1,rsa2048"; +				key-name-hint = "dev"; +				sign-images = "fdt", "kernel"; +			}; +		}; +	}; +}; diff --git a/roms/u-boot/test/vboot/sign-configs-sha256.its b/roms/u-boot/test/vboot/sign-configs-sha256.its new file mode 100644 index 00000000..1b3432ec --- /dev/null +++ b/roms/u-boot/test/vboot/sign-configs-sha256.its @@ -0,0 +1,45 @@ +/dts-v1/; + +/ { +	description = "Chrome OS kernel image with one or more FDT blobs"; +	#address-cells = <1>; + +	images { +		kernel@1 { +			data = /incbin/("test-kernel.bin"); +			type = "kernel_noload"; +			arch = "sandbox"; +			os = "linux"; +			compression = "none"; +			load = <0x4>; +			entry = <0x8>; +			kernel-version = <1>; +			hash@1 { +				algo = "sha256"; +			}; +		}; +		fdt@1 { +			description = "snow"; +			data = /incbin/("sandbox-kernel.dtb"); +			type = "flat_dt"; +			arch = "sandbox"; +			compression = "none"; +			fdt-version = <1>; +			hash@1 { +				algo = "sha256"; +			}; +		}; +	}; +	configurations { +		default = "conf@1"; +		conf@1 { +			kernel = "kernel@1"; +			fdt = "fdt@1"; +			signature@1 { +				algo = "sha256,rsa2048"; +				key-name-hint = "dev"; +				sign-images = "fdt", "kernel"; +			}; +		}; +	}; +}; diff --git a/roms/u-boot/test/vboot/sign-images-sha1.its b/roms/u-boot/test/vboot/sign-images-sha1.its new file mode 100644 index 00000000..f69326a3 --- /dev/null +++ b/roms/u-boot/test/vboot/sign-images-sha1.its @@ -0,0 +1,42 @@ +/dts-v1/; + +/ { +	description = "Chrome OS kernel image with one or more FDT blobs"; +	#address-cells = <1>; + +	images { +		kernel@1 { +			data = /incbin/("test-kernel.bin"); +			type = "kernel_noload"; +			arch = "sandbox"; +			os = "linux"; +			compression = "none"; +			load = <0x4>; +			entry = <0x8>; +			kernel-version = <1>; +			signature@1 { +				algo = "sha1,rsa2048"; +				key-name-hint = "dev"; +			}; +		}; +		fdt@1 { +			description = "snow"; +			data = /incbin/("sandbox-kernel.dtb"); +			type = "flat_dt"; +			arch = "sandbox"; +			compression = "none"; +			fdt-version = <1>; +			signature@1 { +				algo = "sha1,rsa2048"; +				key-name-hint = "dev"; +			}; +		}; +	}; +	configurations { +		default = "conf@1"; +		conf@1 { +			kernel = "kernel@1"; +			fdt = "fdt@1"; +		}; +	}; +}; diff --git a/roms/u-boot/test/vboot/sign-images-sha256.its b/roms/u-boot/test/vboot/sign-images-sha256.its new file mode 100644 index 00000000..e6aa9fc4 --- /dev/null +++ b/roms/u-boot/test/vboot/sign-images-sha256.its @@ -0,0 +1,42 @@ +/dts-v1/; + +/ { +	description = "Chrome OS kernel image with one or more FDT blobs"; +	#address-cells = <1>; + +	images { +		kernel@1 { +			data = /incbin/("test-kernel.bin"); +			type = "kernel_noload"; +			arch = "sandbox"; +			os = "linux"; +			compression = "none"; +			load = <0x4>; +			entry = <0x8>; +			kernel-version = <1>; +			signature@1 { +				algo = "sha256,rsa2048"; +				key-name-hint = "dev"; +			}; +		}; +		fdt@1 { +			description = "snow"; +			data = /incbin/("sandbox-kernel.dtb"); +			type = "flat_dt"; +			arch = "sandbox"; +			compression = "none"; +			fdt-version = <1>; +			signature@1 { +				algo = "sha256,rsa2048"; +				key-name-hint = "dev"; +			}; +		}; +	}; +	configurations { +		default = "conf@1"; +		conf@1 { +			kernel = "kernel@1"; +			fdt = "fdt@1"; +		}; +	}; +}; diff --git a/roms/u-boot/test/vboot/vboot_test.sh b/roms/u-boot/test/vboot/vboot_test.sh new file mode 100755 index 00000000..3c6efa77 --- /dev/null +++ b/roms/u-boot/test/vboot/vboot_test.sh @@ -0,0 +1,143 @@ +#!/bin/sh +# +# Copyright (c) 2013, Google Inc. +# +# Simple Verified Boot Test Script +# +# SPDX-License-Identifier:	GPL-2.0+ + +set -e + +# Run U-Boot and report the result +# Args: +#	$1:	Test message +run_uboot() { +	echo -n "Test Verified Boot Run: $1: " +	${uboot} -d sandbox-u-boot.dtb >${tmp} -c ' +sb load host 0 100 test.fit; +fdt addr 100; +bootm 100; +reset' +	if ! grep -q "$2" ${tmp}; then +		echo +		echo "Verified boot key check failed, output follows:" +		cat ${tmp} +		false +	else +		echo "OK" +	fi +} + +echo "Simple Verified Boot Test" +echo "=========================" +echo +echo "Please see doc/uImage.FIT/verified-boot.txt for more information" +echo + +err=0 +tmp=/tmp/vboot_test.$$ + +dir=$(dirname $0) + +if [ -z ${O} ]; then +	O=. +fi +O=$(readlink -f ${O}) + +dtc="-I dts -O dtb -p 2000" +uboot="${O}/u-boot" +mkimage="${O}/tools/mkimage" +fit_check_sign="${O}/tools/fit_check_sign" +keys="${dir}/dev-keys" +echo ${mkimage} -D "${dtc}" + +echo "Build keys" +mkdir -p ${keys} + +# Create an RSA key pair +openssl genrsa -F4 -out ${keys}/dev.key 2048 2>/dev/null + +# Create a certificate containing the public key +openssl req -batch -new -x509 -key ${keys}/dev.key -out ${keys}/dev.crt + +pushd ${dir} >/dev/null + +function do_test { +	echo do $sha test +	# Compile our device tree files for kernel and U-Boot +	dtc -p 0x1000 sandbox-kernel.dts -O dtb -o sandbox-kernel.dtb +	dtc -p 0x1000 sandbox-u-boot.dts -O dtb -o sandbox-u-boot.dtb + +	# Create a number kernel image with zeroes +	head -c 5000 /dev/zero >test-kernel.bin + +	# Build the FIT, but don't sign anything yet +	echo Build FIT with signed images +	${mkimage} -D "${dtc}" -f sign-images-$sha.its test.fit >${tmp} + +	run_uboot "unsigned signatures:" "dev-" + +	# Sign images with our dev keys +	echo Sign images +	${mkimage} -D "${dtc}" -F -k dev-keys -K sandbox-u-boot.dtb \ +		-r test.fit >${tmp} + +	run_uboot "signed images" "dev+" + + +	# Create a fresh .dtb without the public keys +	dtc -p 0x1000 sandbox-u-boot.dts -O dtb -o sandbox-u-boot.dtb + +	echo Build FIT with signed configuration +	${mkimage} -D "${dtc}" -f sign-configs-$sha.its test.fit >${tmp} + +	run_uboot "unsigned config" $sha"+ OK" + +	# Sign images with our dev keys +	echo Sign images +	${mkimage} -D "${dtc}" -F -k dev-keys -K sandbox-u-boot.dtb \ +		-r test.fit >${tmp} + +	run_uboot "signed config" "dev+" + +	echo check signed config on the host +	if ! ${fit_check_sign} -f test.fit -k sandbox-u-boot.dtb >${tmp}; then +		echo +		echo "Verified boot key check on host failed, output follows:" +		cat ${tmp} +		false +	else +		if ! grep -q "dev+" ${tmp}; then +			echo +			echo "Verified boot key check failed, output follows:" +			cat ${tmp} +			false +		else +			echo "OK" +		fi +	fi + +	run_uboot "signed config" "dev+" + +	# Increment the first byte of the signature, which should cause failure +	sig=$(fdtget -t bx test.fit /configurations/conf@1/signature@1 value) +	newbyte=$(printf %x $((0x${sig:0:2} + 1))) +	sig="${newbyte} ${sig:2}" +	fdtput -t bx test.fit /configurations/conf@1/signature@1 value ${sig} + +	run_uboot "signed config with bad hash" "Bad Data Hash" +} + +sha=sha1 +do_test +sha=sha256 +do_test + +popd >/dev/null + +echo +if ${ok}; then +	echo "Test passed" +else +	echo "Test failed" +fi  | 
