polgen.c 2.3 KB

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