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