xref: /aosp_15_r20/external/flashrom/tests/layout.c (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright 2021 Google LLC
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15 
16 #include <include/test.h>
17 #include <stdio.h>
18 
19 #include "tests.h"
20 #include "flash.h"
21 #include "layout.h"
22 #include "libflashrom.h"
23 
included_regions_dont_overlap_test_success(void ** state)24 void included_regions_dont_overlap_test_success(void **state)
25 {
26 	(void) state; /* unused */
27 
28 	printf("Creating layout... ");
29 	struct flashrom_layout *layout;
30 	assert_int_equal(0, flashrom_layout_new(&layout));
31 	printf("done\n");
32 
33 	printf("Adding and including first region... ");
34 	assert_int_equal(0, flashrom_layout_add_region(layout, 0x00021000, 0x00031000, "first region"));
35 	assert_int_equal(0, flashrom_layout_include_region(layout, "first region"));
36 	printf("done");
37 
38 	printf(", second (non-overlapping) region... ");
39 	assert_int_equal(0, flashrom_layout_add_region(layout, 0x00031001, 0x0023efc0, "second region"));
40 	assert_int_equal(0, flashrom_layout_include_region(layout, "second region"));
41 	printf("done\n");
42 
43 	printf("Asserting included regions do not overlap... ");
44 	assert_int_equal(0, included_regions_overlap(layout));
45 	printf("done\n");
46 
47 	printf("Releasing layout... ");
48 	flashrom_layout_release(layout);
49 	printf("done\n");
50 }
51 
included_regions_overlap_test_success(void ** state)52 void included_regions_overlap_test_success(void **state)
53 {
54 	(void) state; /* unused */
55 
56 	printf("Creating layout... ");
57 	struct flashrom_layout *layout;
58 	assert_int_equal(0, flashrom_layout_new(&layout));
59 	printf("done\n");
60 
61 	printf("Adding and including first region... ");
62 	assert_int_equal(0, flashrom_layout_add_region(layout, 0x00021000, 0x00031000, "first region"));
63 	assert_int_equal(0, flashrom_layout_include_region(layout, "first region"));
64 	printf("done");
65 
66 	printf(", second (overlapping) region... ");
67 	assert_int_equal(0, flashrom_layout_add_region(layout, 0x00027100, 0x0023efc0, "second region"));
68 	assert_int_equal(0, flashrom_layout_include_region(layout, "second region"));
69 	printf("done\n");
70 
71 	printf("Asserting included regions overlap... ");
72 	assert_int_equal(1, included_regions_overlap(layout));
73 	printf("done\n");
74 
75 	printf("Releasing layout... ");
76 	flashrom_layout_release(layout);
77 	printf("done\n");
78 }
79 
region_not_included_overlap_test_success(void ** state)80 void region_not_included_overlap_test_success(void **state)
81 {
82 	(void) state; /* unused */
83 
84 	printf("Creating layout... ");
85 	struct flashrom_layout *layout;
86 	assert_int_equal(0, flashrom_layout_new(&layout));
87 	printf("done\n");
88 
89 	printf("Adding and including first region... ");
90 	assert_int_equal(0, flashrom_layout_add_region(layout, 0x00021000, 0x00031000, "first region"));
91 	assert_int_equal(0, flashrom_layout_include_region(layout, "first region"));
92 	printf("done");
93 
94 	printf(", second (overlapping) region, not included... ");
95 	assert_int_equal(0, flashrom_layout_add_region(layout, 0x00027100, 0x0023efc0, "second region"));
96 	printf("done\n");
97 
98 	printf("Asserting included regions do not overlap... ");
99 	assert_int_equal(0, included_regions_overlap(layout));
100 	printf("done\n");
101 
102 	printf("Releasing layout... ");
103 	flashrom_layout_release(layout);
104 	printf("done\n");
105 }
106 
layout_pass_sanity_checks_test_success(void ** state)107 void layout_pass_sanity_checks_test_success(void **state)
108 {
109 	(void) state; /* unused */
110 
111 	unsigned int region_start = 0x00021000;
112 	unsigned int region_end = 0x00031000;
113 	unsigned int region2_start = 0x00041000;
114 	unsigned int region2_end = 0x00051000;
115 	unsigned int start = 0;
116 	unsigned int len = 0;
117 
118 	struct flashrom_layout *layout;
119 
120 	printf("Creating layout with one included region... ");
121 	assert_int_equal(0, flashrom_layout_new(&layout));
122 	assert_int_equal(0, flashrom_layout_add_region(layout, region_start, region_end, "region"));
123 	assert_int_equal(0, flashrom_layout_include_region(layout, "region"));
124 	assert_int_equal(0, flashrom_layout_add_region(layout, region2_start, region2_end, "region2"));
125 	assert_int_equal(0, flashrom_layout_include_region(layout, "region2"));
126 	assert_int_equal(0, flashrom_layout_exclude_region(layout, "region2"));
127 	printf("done\n");
128 
129 	printf("Asserting region range... ");
130 	flashrom_layout_get_region_range(layout, "region", &start, &len);
131 	assert_int_equal(start, region_start);
132 	assert_int_equal(len, region_end - region_start + 1);
133 	printf("done\n");
134 
135 	printf("Layout passes sanity checks... ");
136 
137 	struct flashchip chip = {
138 		.total_size = 1024,
139 	};
140 	struct flashrom_flashctx flash = {
141 		.chip = &chip,
142 	};
143 	flashrom_layout_set(&flash, layout);
144 	assert_int_equal(0, layout_sanity_checks(&flash));
145 
146 	printf("done\n");
147 
148 	printf("Releasing layout... ");
149 	flashrom_layout_release(layout);
150 	printf("done\n");
151 }
152 
layout_region_invalid_address_test_success(void ** state)153 void layout_region_invalid_address_test_success(void **state)
154 {
155 	(void) state; /* unused */
156 
157 	struct flashrom_layout *layout;
158 
159 	printf("Creating layout with one included region... ");
160 	assert_int_equal(0, flashrom_layout_new(&layout));
161 	assert_int_equal(0, flashrom_layout_add_region(layout, 0x60000000, 0x70000000, "region"));
162 	assert_int_equal(0, flashrom_layout_include_region(layout, "region"));
163 	printf("done\n");
164 
165 	printf("Layout does not pass sanity checks... ");
166 
167 	struct flashchip chip = {
168 		/* Make sure layout region addresses exceed total size on chip.  */
169 		.total_size = 1,
170 	};
171 	struct flashrom_flashctx flash = {
172 		.chip = &chip,
173 	};
174 	flashrom_layout_set(&flash, layout);
175 	assert_int_equal(1, layout_sanity_checks(&flash));
176 
177 	printf("done\n");
178 
179 	printf("Releasing layout... ");
180 	flashrom_layout_release(layout);
181 	printf("done\n");
182 }
183 
layout_region_invalid_range_test_success(void ** state)184 void layout_region_invalid_range_test_success(void **state)
185 {
186 	(void) state; /* unused */
187 
188 	struct flashrom_layout *layout;
189 
190 	printf("Creating layout with one included region... ");
191 	assert_int_equal(0, flashrom_layout_new(&layout));
192 	/* Make sure address range of region is not positive i.e. start > end. */
193 	assert_int_equal(0, flashrom_layout_add_region(layout, 0x00000020, 0x00000010, "region"));
194 	assert_int_equal(0, flashrom_layout_include_region(layout, "region"));
195 	printf("done\n");
196 
197 	printf("Layout does not pass sanity checks... ");
198 
199 	struct flashchip chip = {
200 		/* Make sure layout region addresses fit into total size on chip.  */
201 		.total_size = 1024,
202 	};
203 	struct flashrom_flashctx flash = {
204 		.chip = &chip,
205 	};
206 	flashrom_layout_set(&flash, layout);
207 	assert_int_equal(1, layout_sanity_checks(&flash));
208 
209 	printf("done\n");
210 
211 	printf("Releasing layout... ");
212 	flashrom_layout_release(layout);
213 	printf("done\n");
214 }
215