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