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