1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2020-2024 Microsoft Corporation. All rights reserved.
*/
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
static void usage(const char *const name)
{
printf("Usage: %s OutputFile (PolicyFile)\n", name);
exit(EINVAL);
}
static int policy_to_buffer(const char *pathname, char **buffer, size_t *size)
{
size_t fsize;
size_t read;
char *lbuf;
int rc = 0;
FILE *fd;
fd = fopen(pathname, "r");
if (!fd) {
rc = errno;
goto out;
}
fseek(fd, 0, SEEK_END);
fsize = ftell(fd);
rewind(fd);
lbuf = malloc(fsize);
if (!lbuf) {
rc = ENOMEM;
goto out_close;
}
read = fread((void *)lbuf, sizeof(*lbuf), fsize, fd);
if (read != fsize) {
rc = -1;
goto out_free;
}
*buffer = lbuf;
*size = fsize;
fclose(fd);
return rc;
out_free:
free(lbuf);
out_close:
fclose(fd);
out:
return rc;
}
static int write_boot_policy(const char *pathname, const char *buf, size_t size)
{
FILE *fd;
size_t i;
fd = fopen(pathname, "w");
if (!fd)
return errno;
fprintf(fd, "/* This file is automatically generated.");
fprintf(fd, " Do not edit. */\n");
fprintf(fd, "#include <linux/stddef.h>\n");
fprintf(fd, "\nextern const char *const ipe_boot_policy;\n\n");
fprintf(fd, "const char *const ipe_boot_policy =\n");
if (!buf || size == 0) {
fprintf(fd, "\tNULL;\n");
fclose(fd);
return 0;
}
fprintf(fd, "\t\"");
for (i = 0; i < size; ++i) {
switch (buf[i]) {
case '"':
fprintf(fd, "\\\"");
break;
case '\'':
fprintf(fd, "'");
break;
case '\n':
fprintf(fd, "\\n\"\n\t\"");
break;
case '\\':
fprintf(fd, "\\\\");
break;
case '\t':
fprintf(fd, "\\t");
break;
case '\?':
fprintf(fd, "\\?");
break;
default:
fprintf(fd, "%c", buf[i]);
}
}
fprintf(fd, "\";\n");
fclose(fd);
return 0;
}
int main(int argc, const char *const argv[])
{
char *policy = NULL;
size_t len = 0;
int rc = 0;
if (argc < 2)
usage(argv[0]);
if (argc > 2) {
rc = policy_to_buffer(argv[2], &policy, &len);
if (rc != 0)
goto cleanup;
}
rc = write_boot_policy(argv[1], policy, len);
cleanup:
if (policy)
free(policy);
if (rc != 0)
perror("An error occurred during policy conversion: ");
return rc;
}
|