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