xref: /xnu-8796.141.3/tests/preoslog.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #include <darwintest.h>
2*1b191cb5SApple OSS Distributions #include <darwintest_utils.h>
3*1b191cb5SApple OSS Distributions #include <sys/sysctl.h>
4*1b191cb5SApple OSS Distributions #include <string.h>
5*1b191cb5SApple OSS Distributions #include <errno.h>
6*1b191cb5SApple OSS Distributions 
7*1b191cb5SApple OSS Distributions #include <sys/preoslog.h>
8*1b191cb5SApple OSS Distributions #include "test_utils.h"
9*1b191cb5SApple OSS Distributions 
10*1b191cb5SApple OSS Distributions static const char* g_sysctl_kern_version = "kern.version";
11*1b191cb5SApple OSS Distributions static const char* g_sysctl_kern_preoslog = "kern.preoslog";
12*1b191cb5SApple OSS Distributions static const uint32_t g_valid_magic = 'LSOP';
13*1b191cb5SApple OSS Distributions 
14*1b191cb5SApple OSS Distributions /*
15*1b191cb5SApple OSS Distributions  * Defines substrings to look up in preoslog buffer.
16*1b191cb5SApple OSS Distributions  * To pass the test, one of the entries should match a substring in preoslog buffer.
17*1b191cb5SApple OSS Distributions  */
18*1b191cb5SApple OSS Distributions static const char* g_preoslog_buffer_string[] = {"serial output"};
19*1b191cb5SApple OSS Distributions 
20*1b191cb5SApple OSS Distributions static boolean_t
check_for_substrings(const char * string,size_t len)21*1b191cb5SApple OSS Distributions check_for_substrings(const char* string, size_t len)
22*1b191cb5SApple OSS Distributions {
23*1b191cb5SApple OSS Distributions 	int i;
24*1b191cb5SApple OSS Distributions 	boolean_t res = FALSE;
25*1b191cb5SApple OSS Distributions 
26*1b191cb5SApple OSS Distributions 	for (i = 0; i < (sizeof(g_preoslog_buffer_string) / sizeof(char*)); i++) {
27*1b191cb5SApple OSS Distributions 		res = res || strnstr(string, g_preoslog_buffer_string[i], len) == NULL ? FALSE : TRUE;
28*1b191cb5SApple OSS Distributions 	}
29*1b191cb5SApple OSS Distributions 
30*1b191cb5SApple OSS Distributions 	return res;
31*1b191cb5SApple OSS Distributions }
32*1b191cb5SApple OSS Distributions 
33*1b191cb5SApple OSS Distributions /*
34*1b191cb5SApple OSS Distributions  *       Valid cases:
35*1b191cb5SApple OSS Distributions  *       1. Development & Debug iBoot/macEFI provides a preoslog buffer.
36*1b191cb5SApple OSS Distributions  *       2. Release iBoot/macEFI doesn't provide a presoslog buffer.
37*1b191cb5SApple OSS Distributions  *       3. Development & Debug xnu provids kern.preoslog sysctl.
38*1b191cb5SApple OSS Distributions  *       4. Release xnu doesn't provide kern.preoslog sysctl.
39*1b191cb5SApple OSS Distributions  */
40*1b191cb5SApple OSS Distributions 
41*1b191cb5SApple OSS Distributions T_DECL(test_preoslog, "Validate kern.preoslog sysctl has expected log content from the boot loader")
42*1b191cb5SApple OSS Distributions {
43*1b191cb5SApple OSS Distributions 	int ret = 0;
44*1b191cb5SApple OSS Distributions 	size_t size = 0;
45*1b191cb5SApple OSS Distributions 	void *buffer = NULL;
46*1b191cb5SApple OSS Distributions 	preoslog_header_t *header = NULL;
47*1b191cb5SApple OSS Distributions 	char tmp = 0;
48*1b191cb5SApple OSS Distributions 	const char *lower_buffer = NULL;
49*1b191cb5SApple OSS Distributions 	size_t lower_buffer_size = 0;
50*1b191cb5SApple OSS Distributions 	const char *upper_buffer = NULL;
51*1b191cb5SApple OSS Distributions 	size_t upper_buffer_size = 0;
52*1b191cb5SApple OSS Distributions 	boolean_t found = FALSE;
53*1b191cb5SApple OSS Distributions 
54*1b191cb5SApple OSS Distributions 	// kern.preoslog is writable
55*1b191cb5SApple OSS Distributions 	ret = sysctlbyname(g_sysctl_kern_preoslog, buffer, &size, &tmp, sizeof(tmp));
56*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "kern.preoslog write check");
57*1b191cb5SApple OSS Distributions 
58*1b191cb5SApple OSS Distributions 	ret = sysctlbyname(g_sysctl_kern_preoslog, NULL, &size, NULL, 0);
59*1b191cb5SApple OSS Distributions 	if (!is_development_kernel()) {
60*1b191cb5SApple OSS Distributions 		// kern.preoslog mustn't exist on release builds of xnu
61*1b191cb5SApple OSS Distributions 		T_ASSERT_NE(ret, 0, "get size kern.preoslog ret != 0 on release builds");
62*1b191cb5SApple OSS Distributions 		T_ASSERT_POSIX_ERROR(ret, ENOENT, " get size kern.preoslog errno==ENOENT on release builds");
63*1b191cb5SApple OSS Distributions 		return;
64*1b191cb5SApple OSS Distributions 	}
65*1b191cb5SApple OSS Distributions 
66*1b191cb5SApple OSS Distributions 	/*
67*1b191cb5SApple OSS Distributions 	 * Everything below is applicable only to development & debug xnu
68*1b191cb5SApple OSS Distributions 	 */
69*1b191cb5SApple OSS Distributions 
70*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "get size for kern.preoslog");
71*1b191cb5SApple OSS Distributions 	if (size == 0) {
72*1b191cb5SApple OSS Distributions 		// No preoslog buffer available, valid case if iboot is release
73*1b191cb5SApple OSS Distributions 		return;
74*1b191cb5SApple OSS Distributions 	}
75*1b191cb5SApple OSS Distributions 
76*1b191cb5SApple OSS Distributions 	buffer = calloc(size, sizeof(char));
77*1b191cb5SApple OSS Distributions 	T_ASSERT_NOTNULL(buffer, "allocate buffer for preoslog");
78*1b191cb5SApple OSS Distributions 
79*1b191cb5SApple OSS Distributions 	ret = sysctlbyname(g_sysctl_kern_preoslog, buffer, &size, NULL, 0);
80*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "get preoslog buffer");
81*1b191cb5SApple OSS Distributions 
82*1b191cb5SApple OSS Distributions 	header = (preoslog_header_t *)buffer;
83*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(header->magic, g_valid_magic, "check preoslog header magic - expected %#x, given %#x", g_valid_magic, header->magic);
84*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ(header->size, size, "check preoslog sizes - expected %zu, given %zu", size, header->size);
85*1b191cb5SApple OSS Distributions 	T_ASSERT_LT(header->offset, header->size - sizeof(*header), "check write offset");
86*1b191cb5SApple OSS Distributions 
87*1b191cb5SApple OSS Distributions 	lower_buffer = header->data;
88*1b191cb5SApple OSS Distributions 	lower_buffer_size = header->offset + 1;
89*1b191cb5SApple OSS Distributions 	upper_buffer = lower_buffer + lower_buffer_size;
90*1b191cb5SApple OSS Distributions 	upper_buffer_size = header->size - lower_buffer_size - sizeof(*header);
91*1b191cb5SApple OSS Distributions 	if (header->wrapped) {
92*1b191cb5SApple OSS Distributions 		found = check_for_substrings(upper_buffer, upper_buffer_size);
93*1b191cb5SApple OSS Distributions 	}
94*1b191cb5SApple OSS Distributions 
95*1b191cb5SApple OSS Distributions 	found = found || check_for_substrings(lower_buffer, lower_buffer_size);
96*1b191cb5SApple OSS Distributions 	T_ASSERT_TRUE(found, "Verify buffer content");
97*1b191cb5SApple OSS Distributions 
98*1b191cb5SApple OSS Distributions 	free(buffer);
99*1b191cb5SApple OSS Distributions 	buffer = NULL;
100*1b191cb5SApple OSS Distributions 	header = NULL;
101*1b191cb5SApple OSS Distributions }
102