polgen.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. FILE *fd;
  54. size_t i;
  55. fd = fopen(pathname, "w");
  56. if (!fd)
  57. return errno;
  58. fprintf(fd, "/* This file is automatically generated.");
  59. fprintf(fd, " Do not edit. */\n");
  60. fprintf(fd, "#include <linux/stddef.h>\n");
  61. fprintf(fd, "\nextern const char *const ipe_boot_policy;\n\n");
  62. fprintf(fd, "const char *const ipe_boot_policy =\n");
  63. if (!buf || size == 0) {
  64. fprintf(fd, "\tNULL;\n");
  65. fclose(fd);
  66. return 0;
  67. }
  68. fprintf(fd, "\t\"");
  69. for (i = 0; i < size; ++i) {
  70. switch (buf[i]) {
  71. case '"':
  72. fprintf(fd, "\\\"");
  73. break;
  74. case '\'':
  75. fprintf(fd, "'");
  76. break;
  77. case '\n':
  78. fprintf(fd, "\\n\"\n\t\"");
  79. break;
  80. case '\\':
  81. fprintf(fd, "\\\\");
  82. break;
  83. case '\t':
  84. fprintf(fd, "\\t");
  85. break;
  86. case '\?':
  87. fprintf(fd, "\\?");
  88. break;
  89. default:
  90. fprintf(fd, "%c", buf[i]);
  91. }
  92. }
  93. fprintf(fd, "\";\n");
  94. fclose(fd);
  95. return 0;
  96. }
  97. int main(int argc, const char *const argv[])
  98. {
  99. char *policy = NULL;
  100. size_t len = 0;
  101. int rc = 0;
  102. if (argc < 2)
  103. usage(argv[0]);
  104. if (argc > 2) {
  105. rc = policy_to_buffer(argv[2], &policy, &len);
  106. if (rc != 0)
  107. goto cleanup;
  108. }
  109. rc = write_boot_policy(argv[1], policy, len);
  110. cleanup:
  111. if (policy)
  112. free(policy);
  113. if (rc != 0)
  114. perror("An error occurred during policy conversion: ");
  115. return rc;
  116. }