1 /* Copyright 2013-2014 IBM Corp.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * 	http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdlib.h>
18 
19 #include "../nvram-format.c"
20 
nvram_wait_for_load(void)21 bool nvram_wait_for_load(void)
22 {
23 	return true;
24 }
25 
nvram_validate(void)26 bool nvram_validate(void)
27 {
28 	return true;
29 }
30 
nvram_has_loaded(void)31 bool nvram_has_loaded(void)
32 {
33 	return true;
34 }
35 
nvram_reset(void * nvram_image,int size)36 static char *nvram_reset(void *nvram_image, int size)
37 {
38 	struct chrp_nvram_hdr *h = nvram_image;
39 
40 	/* entire partition used by one key */
41 	assert(nvram_format(nvram_image, size) == 0);
42 	memset((char *) h + sizeof(*h), 0, NVRAM_SIZE_FW_PRIV - sizeof(*h));
43 	assert(nvram_check(nvram_image, size) == 0);
44 
45 	return (char *) h + sizeof(*h);
46 }
47 
main(void)48 int main(void)
49 {
50 	char *nvram_image;
51 	size_t sz;
52 	struct chrp_nvram_hdr *h;
53 	char *data;
54 	const char *result;
55 
56 	/* 1024 bytes is too small for our NVRAM */
57 	nvram_image = malloc(1024);
58 	assert(nvram_format(nvram_image, 1024)!=0);
59 	free(nvram_image);
60 
61 	/* 4096 bytes is too small for our NVRAM */
62 	nvram_image = malloc(4096);
63 	assert(nvram_format(nvram_image, 4096)!=0);
64 	free(nvram_image);
65 
66 	/* 64k is too small for our NVRAM */
67 	nvram_image = malloc(0x10000);
68 	assert(nvram_format(nvram_image, 0x10000)!=0);
69 	free(nvram_image);
70 
71 	/* 68k is too small for our NVRAM */
72 	nvram_image = malloc(68*1024);
73 	assert(nvram_format(nvram_image, 68*1024)!=0);
74 	free(nvram_image);
75 
76 	/* 68k+16 bytes (nvram header) should generate empty free space */
77 	sz = NVRAM_SIZE_COMMON + NVRAM_SIZE_FW_PRIV
78 		+ sizeof(struct chrp_nvram_hdr);
79 	nvram_image = malloc(sz);
80 	assert(nvram_format(nvram_image, sz)==0);
81 	assert(nvram_check(nvram_image, sz)==0);
82 	assert(nvram_image[sz-14]==0);
83 	assert(nvram_image[sz-13]==1);
84 	h = (struct chrp_nvram_hdr*)(&nvram_image[NVRAM_SIZE_COMMON + NVRAM_SIZE_FW_PRIV]);
85 	assert(memcmp(h->name, "wwwwwwwwwwww", 12)==0);
86 	free(nvram_image);
87 
88 	/* 128k NVRAM check */
89 	nvram_image = malloc(128*1024);
90 	assert(nvram_format(nvram_image, 128*1024)==0);
91 	assert(nvram_check(nvram_image,128*1024)==0);
92 
93 	/* Now, we corrupt it */
94 	nvram_image[0] = 0;
95 	assert(nvram_check(nvram_image,128*1024) != 0);
96 
97 	/* Does our NUL checking work? */
98 	assert(nvram_format(nvram_image, 128 * 1024) == 0);
99 	h = (struct chrp_nvram_hdr *) nvram_image;
100 	memset((char *) h + sizeof(*h), 0xFF, be16_to_cpu(h->len) * 16 - sizeof(*h));
101 	assert(nvram_check(nvram_image, 128 * 1024) != 0);
102 
103 	assert(nvram_format(nvram_image, 128*1024)==0);
104 	/* corrupt the length of the partition */
105 	nvram_image[2] = 0;
106 	nvram_image[3] = 0;
107 	assert(nvram_check(nvram_image,128*1024) != 0);
108 
109 	assert(nvram_format(nvram_image, 128*1024)==0);
110 	/* corrupt the length of the partition */
111 	nvram_image[2] = 0;
112 	nvram_image[3] = 0;
113 	/* but reset checksum! */
114 	h = (struct chrp_nvram_hdr*)nvram_image;
115 	h->cksum = chrp_nv_cksum(h);
116 	assert(nvram_check(nvram_image,128*1024) != 0);
117 
118 	assert(nvram_format(nvram_image, 128*1024)==0);
119 	/* make the length insanely beyond end of nvram  */
120 	nvram_image[2] = 42;
121 	nvram_image[3] = 32;
122 	/* but reset checksum! */
123 	h = (struct chrp_nvram_hdr*)nvram_image;
124 	h->cksum = chrp_nv_cksum(h);
125 	assert(nvram_check(nvram_image,128*1024) != 0);
126 
127 	assert(nvram_format(nvram_image, 128*1024)==0);
128 	/* remove skiboot partition */
129 	nvram_image[12] = '\0';
130 	/* but reset checksum! */
131 	h = (struct chrp_nvram_hdr*)nvram_image;
132 	h->cksum = chrp_nv_cksum(h);
133 	assert(nvram_check(nvram_image,128*1024) != 0);
134 
135 	assert(nvram_format(nvram_image, 128*1024)==0);
136 	/* remove common partition */
137 	nvram_image[NVRAM_SIZE_FW_PRIV+5] = '\0';
138 	/* but reset checksum! */
139 	h = (struct chrp_nvram_hdr*)(&nvram_image[NVRAM_SIZE_FW_PRIV]);
140 	h->cksum = chrp_nv_cksum(h);
141 	assert(nvram_check(nvram_image,128*1024) != 0);
142 
143 	/* test nvram_query() */
144 
145 	/* does an empty partition break us? */
146 	data = nvram_reset(nvram_image, 128*1024);
147 	assert(nvram_query_safe("test") == NULL);
148 
149 	/* does a zero length key break us? */
150 	data = nvram_reset(nvram_image, 128*1024);
151 	data[0] = '=';
152 	assert(nvram_query_safe("test") == NULL);
153 
154 	/* does a missing = break us? */
155 	data = nvram_reset(nvram_image, 128*1024);
156 	data[0] = 'a';
157 	assert(nvram_query_safe("test") == NULL);
158 
159 	/* does an empty value break us? */
160 	data = nvram_reset(nvram_image, 128*1024);
161 	data[0] = 'a';
162 	data[1] = '=';
163 	result = nvram_query_safe("a");
164 	assert(result);
165 	assert(strlen(result) == 0);
166 
167 	/* do we trip over malformed keys? */
168 	data = nvram_reset(nvram_image, 128*1024);
169 #define TEST_1 "a\0a=\0test=test\0"
170 	memcpy(data, TEST_1, sizeof(TEST_1));
171 	result = nvram_query_safe("test");
172 	assert(result);
173 	assert(strcmp(result, "test") == 0);
174 
175 	free(nvram_image);
176 
177 	return 0;
178 }
179