1 // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2 /*
3  * Test for our PEL record generation. Currently this doesn't actually
4  * test that the records we generate are correct, but it at least lets
5  * us run valgrind over the generation routines to check for buffer
6  * overflows, etc.
7  *
8  * Copyright 2013-2016 IBM Corp.
9  */
10 
11 #include <skiboot.h>
12 #include <inttypes.h>
13 #include <assert.h>
14 #include <pel.h>
15 #include <errorlog.h>
16 #include <device.h>
17 
18 #define TEST_ERROR 0x1234
19 #define TEST_SUBSYS 0x5678
20 
21 DEFINE_LOG_ENTRY(TEST_ERROR, OPAL_PLATFORM_ERR_EVT, TEST_SUBSYS,
22 			OPAL_PLATFORM_FIRMWARE, OPAL_INFO,
23 			OPAL_NA);
24 
25 /* Override this for testing. */
26 #define is_rodata(p) fake_is_rodata(p)
27 
28 char __rodata_start[16];
29 #define __rodata_end (__rodata_start + sizeof(__rodata_start))
30 
fake_is_rodata(const void * p)31 static inline bool fake_is_rodata(const void *p)
32 {
33 	return ((char *)p >= __rodata_start && (char *)p < __rodata_end);
34 }
35 
36 #define zalloc(bytes) calloc((bytes), 1)
37 
38 #include "../device.c"
39 #include "../pel.c"
40 
41 struct dt_node *dt_root = NULL;
42 char dt_prop[] = "DUMMY DT PROP";
43 
rtc_cache_get_datetime(uint32_t * year_month_day,uint64_t * hour_minute_second_millisecond)44 int rtc_cache_get_datetime(uint32_t *year_month_day,
45 			   uint64_t *hour_minute_second_millisecond)
46 {
47 	*year_month_day = 0;
48 	*hour_minute_second_millisecond = 0;
49 
50 	return 0;
51 }
52 
main(void)53 int main(void)
54 {
55 	char *pel_buf;
56 	size_t size;
57 	struct errorlog *elog;
58 	struct opal_err_info *opal_err_info = &err_TEST_ERROR;
59 	char *buffer;
60 	struct elog_user_data_section *tmp;
61 
62 	dt_root = dt_new_root("");
63 	dt_add_property_string(dt_root, "model", "run-pel-unittest");
64 
65 	elog = malloc(sizeof(struct errorlog));
66 	pel_buf = malloc(PEL_MIN_SIZE + 4);
67 	assert(elog);
68 	assert(pel_buf);
69 
70 	memset(elog, 0, sizeof(struct errorlog));
71 
72 	elog->error_event_type = opal_err_info->err_type;
73 	elog->component_id = opal_err_info->cmp_id;
74 	elog->subsystem_id = opal_err_info->subsystem;
75 	elog->event_severity = opal_err_info->sev;
76 	elog->event_subtype = opal_err_info->event_subtype;
77 	elog->reason_code = opal_err_info->reason_code;
78 	elog->elog_origin = ORG_SAPPHIRE;
79 
80 	size = pel_size(elog);
81 
82 	printf("Test buffer too small: ");
83 	assert(0 == create_pel_log(elog, NULL, size - 1));
84 
85 	assert(size <= PEL_MIN_SIZE + 4);
86 	assert(size == create_pel_log(elog, pel_buf, size));
87 
88 	memset(elog, 0, sizeof(struct errorlog));
89 
90 	elog->error_event_type = opal_err_info->err_type;
91 	elog->component_id = opal_err_info->cmp_id;
92 	elog->subsystem_id = opal_err_info->subsystem;
93 	elog->event_severity = opal_err_info->sev;
94 	elog->event_subtype = opal_err_info->event_subtype;
95 	elog->reason_code = opal_err_info->reason_code;
96 	elog->elog_origin = ORG_SAPPHIRE;
97 
98 	size = pel_size(elog);
99 	pel_buf = realloc(pel_buf, size);
100 	assert(pel_buf);
101 
102 	buffer = elog->user_data_dump + elog->user_section_size;
103 	tmp = (struct elog_user_data_section *)buffer;
104 	tmp->tag = OPAL_ELOG_SEC_DESC;  /* ASCII of DESC */
105 	tmp->size = size + sizeof(struct elog_user_data_section) - 1;
106 	strcpy(tmp->data_dump, "Hello World!");
107 	elog->user_section_size += tmp->size;
108 	elog->user_section_count++;
109 
110 	size = pel_size(elog);
111 	pel_buf = realloc(pel_buf, size);
112 	assert(pel_buf);
113 
114 	assert(size == create_pel_log(elog, pel_buf, size));
115 
116 	free(pel_buf);
117 	free(elog);
118 
119 	return 0;
120 }
121