aboutsummaryrefslogtreecommitdiff
path: root/core/test/run-nvram-format.c
blob: 4ca2cded2a6089125d6d537672a006c3c21f88f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* Copyright 2013-2014 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>

#include "../nvram-format.c"

bool nvram_validate(void)
{
	return true;
}

static char *nvram_reset(void *nvram_image, int size)
{
	struct chrp_nvram_hdr *h = nvram_image;

	/* entire partition used by one key */
	assert(nvram_format(nvram_image, size) == 0);
	memset((char *) h + sizeof(*h), 0, NVRAM_SIZE_FW_PRIV - sizeof(*h));
	assert(nvram_check(nvram_image, size) == 0);

	return (char *) h + sizeof(*h);
}

int main(void)
{
	char *nvram_image;
	size_t sz;
	struct chrp_nvram_hdr *h;
	char *data;
	const char *result;

	/* 1024 bytes is too small for our NVRAM */
	nvram_image = malloc(1024);
	assert(nvram_format(nvram_image, 1024)!=0);
	free(nvram_image);

	/* 4096 bytes is too small for our NVRAM */
	nvram_image = malloc(4096);
	assert(nvram_format(nvram_image, 4096)!=0);
	free(nvram_image);

	/* 64k is too small for our NVRAM */
	nvram_image = malloc(0x10000);
	assert(nvram_format(nvram_image, 0x10000)!=0);
	free(nvram_image);

	/* 68k is too small for our NVRAM */
	nvram_image = malloc(68*1024);
	assert(nvram_format(nvram_image, 68*1024)!=0);
	free(nvram_image);

	/* 68k+16 bytes (nvram header) should generate empty free space */
	sz = NVRAM_SIZE_COMMON + NVRAM_SIZE_FW_PRIV
		+ sizeof(struct chrp_nvram_hdr);
	nvram_image = malloc(sz);
	assert(nvram_format(nvram_image, sz)==0);
	assert(nvram_check(nvram_image, sz)==0);
	assert(nvram_image[sz-13]==0);
	assert(nvram_image[sz-14]==1);
	h = (struct chrp_nvram_hdr*)(&nvram_image[NVRAM_SIZE_COMMON + NVRAM_SIZE_FW_PRIV]);
	assert(memcmp(h->name, "wwwwwwwwwwww", 12)==0);
	free(nvram_image);

	/* 128k NVRAM check */
	nvram_image = malloc(128*1024);
	assert(nvram_format(nvram_image, 128*1024)==0);
	assert(nvram_check(nvram_image,128*1024)==0);

	/* Now, we corrupt it */
	nvram_image[0] = 0;
	assert(nvram_check(nvram_image,128*1024) != 0);

	/* Does our NUL checking work? */
	assert(nvram_format(nvram_image, 128 * 1024) == 0);
	h = (struct chrp_nvram_hdr *) nvram_image;
	memset((char *) h + sizeof(*h), 0xFF, h->len * 16 - sizeof(*h));
	assert(nvram_check(nvram_image, 128 * 1024) != 0);

	assert(nvram_format(nvram_image, 128*1024)==0);
	/* corrupt the length of the partition */
	nvram_image[2] = 0;
	nvram_image[3] = 0;
	assert(nvram_check(nvram_image,128*1024) != 0);

	assert(nvram_format(nvram_image, 128*1024)==0);
	/* corrupt the length of the partition */
	nvram_image[2] = 0;
	nvram_image[3] = 0;
	/* but reset checksum! */
	h = (struct chrp_nvram_hdr*)nvram_image;
	h->cksum = chrp_nv_cksum(h);
	assert(nvram_check(nvram_image,128*1024) != 0);

	assert(nvram_format(nvram_image, 128*1024)==0);
	/* make the length insanely beyond end of nvram  */
	nvram_image[2] = 42;
	nvram_image[3] = 32;
	/* but reset checksum! */
	h = (struct chrp_nvram_hdr*)nvram_image;
	h->cksum = chrp_nv_cksum(h);
	assert(nvram_check(nvram_image,128*1024) != 0);

	assert(nvram_format(nvram_image, 128*1024)==0);
	/* remove skiboot partition */
	nvram_image[12] = '\0';
	/* but reset checksum! */
	h = (struct chrp_nvram_hdr*)nvram_image;
	h->cksum = chrp_nv_cksum(h);
	assert(nvram_check(nvram_image,128*1024) != 0);

	assert(nvram_format(nvram_image, 128*1024)==0);
	/* remove common partition */
	nvram_image[NVRAM_SIZE_FW_PRIV+5] = '\0';
	/* but reset checksum! */
	h = (struct chrp_nvram_hdr*)(&nvram_image[NVRAM_SIZE_FW_PRIV]);
	h->cksum = chrp_nv_cksum(h);
	assert(nvram_check(nvram_image,128*1024) != 0);

	/* test nvram_query() */

	/* does an empty partition break us? */
	data = nvram_reset(nvram_image, 128*1024);
	assert(nvram_query("test") == NULL);

	/* does a zero length key break us? */
	data = nvram_reset(nvram_image, 128*1024);
	data[0] = '=';
	assert(nvram_query("test") == NULL);

	/* does a missing = break us? */
	data = nvram_reset(nvram_image, 128*1024);
	data[0] = 'a';
	assert(nvram_query("test") == NULL);

	/* does an empty value break us? */
	data = nvram_reset(nvram_image, 128*1024);
	data[0] = 'a';
	data[1] = '=';
	result = nvram_query("a");
	assert(result);
	assert(strlen(result) == 0);

	/* do we trip over malformed keys? */
	data = nvram_reset(nvram_image, 128*1024);
#define TEST_1 "a\0a=\0test=test\0"
	memcpy(data, TEST_1, sizeof(TEST_1));
	result = nvram_query("test");
	assert(result);
	assert(strcmp(result, "test") == 0);

	free(nvram_image);

	return 0;
}