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